Skip to content

Commit

Permalink
Raise an error when an unsupported transparency type is found in PNGs #…
Browse files Browse the repository at this point in the history
  • Loading branch information
practicingruby committed Oct 9, 2014
1 parent 0f3ff6d commit 491a90c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
15 changes: 3 additions & 12 deletions lib/prawn/images/png.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,9 @@ def initialize(data)
@transparency = {}
case @color_type
when 3
# Indexed colour, RGB. Each byte in this chunk is an alpha for
# the palette index in the PLTE ("palette") chunk up until the
# last non-opaque entry. Set up an array, stretching over all
# palette entries which will be 0 (opaque) or 1 (transparent).
@transparency[:indexed] = data.read(chunk_size).unpack("C*")
short = 255 - @transparency[:indexed].size
@transparency[:indexed] += ([255] * short) if short > 0
raise Errors::UnsupportedImageType,
"Pallete-based transparency in PNG is not currently supported.\n" +
"See https://github.com/prawnpdf/prawn/issues/783"
when 0
# Greyscale. Corresponding to entries in the PLTE chunk.
# Grey is two bytes, range 0 .. (2 ^ bit-depth) - 1
Expand Down Expand Up @@ -208,11 +204,6 @@ def build_pdf_object(document)
# components.
rgb = transparency[:rgb]
obj.data[:Mask] = rgb.collect { |x| [x,x] }.flatten
elsif transparency[:indexed]
# TODO: broken. I was attempting to us Color Key Masking, but I think
# we need to construct an SMask i think. Maybe do it inside
# the PNG class, and store it in alpha_channel
#obj.data[:Mask] = transparency[:indexed]
end

# For PNG color types 4 and 6, the transparency data is stored as a alpha
Expand Down
10 changes: 9 additions & 1 deletion spec/png_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,15 @@
end
end

# TODO: describe "When reading an indexed color PNG file wiih transparency (color type 3)"
describe "When reading an indexed color PNG file "+
"wiih transparency (color type 3)" do

it "raises a not supported error" do
bin = File.binread("#{Prawn::DATADIR}/images/pal_bk.png")
expect { Prawn::Images::PNG.new(bin)}.to(
raise_error(Prawn::Errors::UnsupportedImageType))
end
end

describe "When reading an indexed color PNG file (color type 3)" do

Expand Down

0 comments on commit 491a90c

Please sign in to comment.