Permalink
Browse files

working on support for RGB PNGs with an alpha channel.

- not working yet
  • Loading branch information...
1 parent 5f26cf2 commit ad44ae630ad1142fbdeef30c1bdfb1942f61a68e @yob yob committed Jul 28, 2008
Showing with 47 additions and 5 deletions.
  1. BIN data/images/ruport_transparent.png
  2. +6 −0 examples/image.rb
  3. +24 −4 lib/prawn/images.rb
  4. +17 −1 lib/prawn/images/png.rb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
@@ -13,5 +13,11 @@
text "Please enjoy the pigs", :size => 36, :at => [200,15]
ruport = "#{Prawn::BASEDIR}/data/images/ruport.png"
+ image ruport, :at => [400,200]
+
+ fill_color "ffff33"
+ fill_rectangle [400,400], 250, 95
+
+ ruport = "#{Prawn::BASEDIR}/data/images/ruport_transparent.png"
image ruport, :at => [400,400]
end
View
@@ -78,7 +78,6 @@ def build_jpg_object(data, jpg)
end
def build_png_object(data, png)
- png = Prawn::Images::PNG.new(data)
if png.compression_method != 0
raise ArgumentError, 'PNG uses an unsupported compression method'
@@ -106,6 +105,9 @@ def build_png_object(data, png)
when 3
ncolor = 1
color = :DeviceRGB
+ when 6
+ ncolor = 3
+ color = :DeviceRGB
else
raise ArgumentError, "PNG has unsupported color type"
end
@@ -117,13 +119,16 @@ def build_png_object(data, png)
:Width => png.width,
:BitsPerComponent => png.bits,
:Length => png.img_data.size,
- :DecodeParms => {:Predictor => 15,
- :Colors => ncolor,
- :Columns => png.width},
:Filter => :FlateDecode
)
+ unless png.alpha_channel
+ obj.data[:DecodeParms] = {:Predictor => 15,
+ :Colors => ncolor,
+ :Columns => png.width}
+ end
+
# append the actual image data to the object as a stream
obj << png.img_data
@@ -147,6 +152,21 @@ def build_png_object(data, png)
#end
end
+ if png.alpha_channel
+ smask_obj = ref(:Type => :XObject,
+ :Subtype => :Image,
+ :Height => png.height,
+ :Width => png.width,
+ :BitsPerComponent => 8,
+ :Length => png.alpha_channel.size,
+ :Filter => :FlateDecode,
+ :ColorSpace => :DeviceGray,
+ :Decode => [0, 1]
+ )
+ smask_obj << png.alpha_channel
+ obj.data[:SMask] = smask_obj
+ end
+
return obj
end
View
@@ -18,7 +18,7 @@ class PNG #nodoc
attr_reader :palette, :img_data, :transparency
attr_reader :width, :height, :bits
attr_reader :color_type, :compression_method, :filter_method
- attr_reader :interlace_method
+ attr_reader :interlace_method, :alpha_channel
# Process a new PNG image
#
@@ -83,6 +83,22 @@ def initialize(data)
data.read(4) # Skip the CRC
end
+
+ # if our img_data contains alpha channel data, split it out
+ # TODO: this is currently somewhat broken. Needs to be
+ # expanded to handle filters. See the PNG gem for
+ # sample code.
+ if @color_type == 6
+ uncompressed_data = Zlib::Inflate.new.inflate(@img_data)
+ @img_data = ""
+ @alpha_channel = ""
+ uncompressed_data.unpack("C*").each_slice(4) do |pixel|
+ @img_data << pixel[0,3].pack("C*")
+ @alpha_channel << pixel[3] if pixel[3]
+ end
+ @img_data = Zlib::Deflate.deflate(@img_data)
+ @alpha_channel = Zlib::Deflate.deflate(@alpha_channel)
+ end
end
end
end

0 comments on commit ad44ae6

Please sign in to comment.