Class: Prawn::Images::PNG
- Inherits:
-
Object
- Object
- Prawn::Images::PNG
- Defined in:
- lib/prawn/images/png.rb
Overview
A convenience class that wraps the logic for extracting the parts of a PNG image that we need to embed them in a PDF
Instance Attribute Summary collapse
-
#alpha_channel ⇒ Object
readonly
Returns the value of attribute alpha_channel.
-
#bits ⇒ Object
readonly
Returns the value of attribute bits.
-
#color_type ⇒ Object
readonly
Returns the value of attribute color_type.
-
#compression_method ⇒ Object
readonly
Returns the value of attribute compression_method.
-
#filter_method ⇒ Object
readonly
Returns the value of attribute filter_method.
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#img_data ⇒ Object
readonly
:nodoc:.
-
#interlace_method ⇒ Object
readonly
Returns the value of attribute interlace_method.
-
#palette ⇒ Object
readonly
:nodoc:.
-
#transparency ⇒ Object
readonly
:nodoc:.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
-
#initialize(data) ⇒ PNG
constructor
Process a new PNG image.
- #pixel_bytes ⇒ Object
Constructor Details
#initialize(data) ⇒ PNG
Process a new PNG image
:data
-
A string containing a full PNG file
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/prawn/images/png.rb', line 26 def initialize(data) data = StringIO.new(data.dup) data.read(8) # Skip the default header @palette = "" @img_data = "" @transparency = {} loop do chunk_size = data.read(4).unpack("N")[0] section = data.read(4) case section when 'IHDR' # we can grab other interesting values from here (like width, # height, etc) values = data.read(chunk_size).unpack("NNCCCCC") @width = values[0] @height = values[1] @bits = values[2] @color_type = values[3] @compression_method = values[4] @filter_method = values[5] @interlace_method = values[6] when 'PLTE' @palette << data.read(chunk_size) when 'IDAT' @img_data << data.read(chunk_size) when 'tRNS' # This chunk can only occur once and it must occur after the # PLTE chunk and before the IDAT chunk @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 when 0 # Greyscale. Corresponding to entries in the PLTE chunk. # Grey is two bytes, range 0 .. (2 ^ bit-depth) - 1 grayval = data.read(chunk_size).unpack("n").first @transparency[:grayscale] = grayval when 2 # True colour with proper alpha channel. @transparency[:rgb] = data.read(chunk_size).unpack("nnn") end when 'IEND' # we've got everything we need, exit the loop break else # unknown (or un-important) section, skip over it data.seek(data.pos + chunk_size) end data.read(4) # Skip the CRC end # if our img_data contains alpha channel data, split it out unfilter_image_data if alpha_channel? end |
Instance Attribute Details
#alpha_channel ⇒ Object (readonly)
Returns the value of attribute alpha_channel.
21 22 23 |
# File 'lib/prawn/images/png.rb', line 21 def alpha_channel @alpha_channel end |
#bits ⇒ Object (readonly)
Returns the value of attribute bits.
19 20 21 |
# File 'lib/prawn/images/png.rb', line 19 def bits @bits end |
#color_type ⇒ Object (readonly)
Returns the value of attribute color_type.
20 21 22 |
# File 'lib/prawn/images/png.rb', line 20 def color_type @color_type end |
#compression_method ⇒ Object (readonly)
Returns the value of attribute compression_method.
20 21 22 |
# File 'lib/prawn/images/png.rb', line 20 def compression_method @compression_method end |
#filter_method ⇒ Object (readonly)
Returns the value of attribute filter_method.
20 21 22 |
# File 'lib/prawn/images/png.rb', line 20 def filter_method @filter_method end |
#height ⇒ Object (readonly)
Returns the value of attribute height.
19 20 21 |
# File 'lib/prawn/images/png.rb', line 19 def height @height end |
#img_data ⇒ Object (readonly)
:nodoc:
18 19 20 |
# File 'lib/prawn/images/png.rb', line 18 def img_data @img_data end |
#interlace_method ⇒ Object (readonly)
Returns the value of attribute interlace_method.
21 22 23 |
# File 'lib/prawn/images/png.rb', line 21 def interlace_method @interlace_method end |
#palette ⇒ Object (readonly)
:nodoc:
18 19 20 |
# File 'lib/prawn/images/png.rb', line 18 def palette @palette end |
#transparency ⇒ Object (readonly)
:nodoc:
18 19 20 |
# File 'lib/prawn/images/png.rb', line 18 def transparency @transparency end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
19 20 21 |
# File 'lib/prawn/images/png.rb', line 19 def width @width end |
Instance Method Details
#pixel_bytes ⇒ Object
92 93 94 95 96 97 |
# File 'lib/prawn/images/png.rb', line 92 def pixel_bytes @pixel_bytes ||= case @color_type when 0, 3, 4 then 1 when 1, 2, 6 then 3 end end |