lib/prawn/images/png.rb in prawn-2.1.0 vs lib/prawn/images/png.rb in prawn-2.2.0

- old
+ new

@@ -24,42 +24,42 @@ attr_reader :color_type, :compression_method, :filter_method attr_reader :interlace_method, :alpha_channel attr_accessor :scaled_width, :scaled_height def self.can_render?(image_blob) - image_blob[0, 8].unpack("C*") == [137, 80, 78, 71, 13, 10, 26, 10] + image_blob[0, 8].unpack('C*') == [137, 80, 78, 71, 13, 10, 26, 10] end # Process a new PNG image # # <tt>data</tt>:: A binary string of PNG data # def initialize(data) data = StringIO.new(data.dup) - data.read(8) # Skip the default header + data.read(8) # Skip the default header - @palette = "" - @img_data = "" + @palette = '' + @img_data = '' @transparency = {} loop do - chunk_size = data.read(4).unpack("N")[0] - section = data.read(4) + 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") + values = data.read(chunk_size).unpack('NNCCCCC') - @width = values[0] - @height = values[1] - @bits = values[2] - @color_type = values[3] + @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] + @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' @@ -70,34 +70,34 @@ when 3 @transparency[:palette] = data.read(chunk_size).unpack('C*') 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 + 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") + @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 + data.read(4) # Skip the CRC end @img_data = Zlib::Inflate.inflate(@img_data) end # number of color components to each pixel # def colors - case self.color_type + case color_type when 0, 3, 4 return 1 when 2, 6 return 3 end @@ -126,22 +126,22 @@ # Build a PDF object representing this image in +document+, and return # a Reference to it. # def build_pdf_object(document) if compression_method != 0 - fail Errors::UnsupportedImageType, - 'PNG uses an unsupported compression method' + raise Errors::UnsupportedImageType, + 'PNG uses an unsupported compression method' end if filter_method != 0 - fail Errors::UnsupportedImageType, - 'PNG uses an unsupported filter method' + raise Errors::UnsupportedImageType, + 'PNG uses an unsupported filter method' end if interlace_method != 0 - fail Errors::UnsupportedImageType, - 'PNG uses unsupported interlace method' + raise Errors::UnsupportedImageType, + 'PNG uses unsupported interlace method' end # some PNG types store the colour and alpha channel data together, # which the PDF spec doesn't like, so split it out. split_alpha_channel! @@ -150,32 +150,32 @@ when 1 color = :DeviceGray when 3 color = :DeviceRGB else - fail Errors::UnsupportedImageType, - "PNG uses an unsupported number of colors (#{png.colors})" + raise Errors::UnsupportedImageType, + "PNG uses an unsupported number of colors (#{png.colors})" end # build the image dict obj = document.ref!( - :Type => :XObject, - :Subtype => :Image, - :Height => height, - :Width => width, - :BitsPerComponent => bits + Type: :XObject, + Subtype: :Image, + Height: height, + Width: width, + BitsPerComponent: bits ) # append the actual image data to the object as a stream obj << img_data obj.stream.filters << { - :FlateDecode => { - :Predictor => 15, - :Colors => colors, - :BitsPerComponent => bits, - :Columns => width + FlateDecode: { + Predictor: 15, + Colors: colors, + BitsPerComponent: bits, + Columns: width } } # sort out the colours of the image if palette.empty? @@ -184,14 +184,16 @@ # embed the colour palette in the PDF as a object stream palette_obj = document.ref!({}) palette_obj << palette # build the color space array for the image - obj.data[:ColorSpace] = [:Indexed, - :DeviceRGB, - (palette.size / 3) - 1, - palette_obj] + obj.data[:ColorSpace] = [ + :Indexed, + :DeviceRGB, + (palette.size / 3) - 1, + palette_obj + ] end # ************************************* # add transparency data if necessary # ************************************* @@ -211,31 +213,32 @@ # components. rgb = transparency[:rgb] obj.data[:Mask] = rgb.collect { |x| [x, x] }.flatten end - # For PNG color types 4 and 6, the transparency data is stored as a alpha - # channel mixed in with the main image data. The PNG class seperates - # it out for us and makes it available via the alpha_channel attribute + # For PNG color types 4 and 6, the transparency data is stored as + # a alpha channel mixed in with the main image data. The PNG class + # seperates it out for us and makes it available via the alpha_channel + # attribute if alpha_channel? smask_obj = document.ref!( - :Type => :XObject, - :Subtype => :Image, - :Height => height, - :Width => width, - :BitsPerComponent => bits, - :ColorSpace => :DeviceGray, - :Decode => [0, 1] + Type: :XObject, + Subtype: :Image, + Height: height, + Width: width, + BitsPerComponent: bits, + ColorSpace: :DeviceGray, + Decode: [0, 1] ) smask_obj.stream << alpha_channel smask_obj.stream.filters << { - :FlateDecode => { - :Predictor => 15, - :Colors => 1, - :BitsPerComponent => bits, - :Columns => width + FlateDecode: { + Predictor: 15, + Colors: 1, + BitsPerComponent: bits, + Columns: width } } obj.data[:SMask] = smask_obj end @@ -259,13 +262,13 @@ def split_image_data alpha_bytes = bits / 8 color_bytes = colors * bits / 8 - scanline_length = (color_bytes + alpha_bytes) * self.width + 1 + scanline_length = (color_bytes + alpha_bytes) * width + 1 scanlines = @img_data.bytesize / scanline_length - pixels = self.width * self.height + pixels = width * height data = StringIO.new(@img_data) data.binmode color_data = [0x00].pack('C') * (pixels * color_bytes + scanlines) @@ -282,10 +285,10 @@ filter = data.getbyte color.putc filter alpha.putc filter - self.width.times do + width.times do color.write data.read(color_bytes) alpha.write data.read(alpha_bytes) end end