Skip to content
Browse files

added specs for the JPG and PNG classes

  • Loading branch information...
1 parent 66147de commit 5f26cf235044e02b44d062b1a837a75893fefec5 @yob yob committed
Showing with 66 additions and 7 deletions.
  1. BIN data/images/ruport_data.dat
  2. +1 −1 lib/prawn/images.rb
  3. +2 −3 lib/prawn/images/jpg.rb
  4. +3 −3 lib/prawn/images/png.rb
  5. +25 −0 spec/jpg_spec.rb
  6. +35 −0 spec/png_spec.rb
View
BIN data/images/ruport_data.dat
Binary file not shown.
View
2 lib/prawn/images.rb
@@ -33,7 +33,7 @@ def image(filename, options={})
# build the image object and embed the raw data
image_obj = case detect_image_format(image_content)
when :jpg then
- info = Prawn::Images::Jpg.new(image_content)
+ info = Prawn::Images::JPG.new(image_content)
build_jpg_object(image_content, info)
when :png then
info = Prawn::Images::PNG.new(image_content)
View
5 lib/prawn/images/jpg.rb
@@ -12,9 +12,8 @@ module Prawn
module Images
# A convenience class that wraps the logic for extracting the parts
# of a PNG image that we need to embed them in a PDF
- class Jpg #nodoc
- attr_reader :width, :height, :bits
- attr_reader :color_space, :channels
+ class JPG #nodoc
+ attr_reader :width, :height, :bits, :channels
JPEG_SOF_BLOCKS = %W(\xc0 \xc1 \xc2 \xc3 \xc5 \xc6 \xc7 \xc9 \xca \xcb \xcd \xce \xcf)
JPEG_APP_BLOCKS = %W(\xe0 \xe1 \xe2 \xe3 \xe4 \xe5 \xe6 \xe7 \xe8 \xe9 \xea \xeb \xec \xed \xee \xef)
View
6 lib/prawn/images/png.rb
@@ -57,19 +57,19 @@ def initialize(data)
# PLTE chunk and before the IDAT chunk
@transparency = {}
case @color_type
- when :indexed
+ 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[:type] = 'indexed'
@transparency[:data] = data.read(chunk_size).unpack("C*")
- when :greyscale
+ when 0
# Greyscale. Corresponding to entries in the PLTE chunk.
# Grey is two bytes, range 0 .. (2 ^ bit-depth) - 1
@transparency[:grayscale] = data.read(2).unpack("n")
@transparency[:type] = 'indexed'
- when :rgb
+ when 2
# True colour with proper alpha channel.
@transparency[:rgb] = data.read(6).unpack("nnn")
end
View
25 spec/jpg_spec.rb
@@ -0,0 +1,25 @@
+# encoding: utf-8
+
+# Spec'ing the PNG class. Not complete yet - still needs to check the
+# contents of palette and transparency to ensure they're correct.
+# Need to find files that have these sections first.
+
+require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
+
+describe "When reading a JPEG file" do
+
+ before(:each) do
+ @filename = "#{Prawn::BASEDIR}/data/images/pigs.jpg"
+ @img_data = File.open(@filename, "rb") { |f| f.read }
+ end
+
+ it "should read the basic attributes correctly" do
+ jpg = Prawn::Images::JPG.new(@img_data)
+
+ jpg.width.should eql(604)
+ jpg.height.should eql(453)
+ jpg.bits.should eql(8)
+ jpg.channels.should eql(3)
+ end
+end
+
View
35 spec/png_spec.rb
@@ -0,0 +1,35 @@
+# encoding: utf-8
+
+# Spec'ing the PNG class. Not complete yet - still needs to check the
+# contents of palette and transparency to ensure they're correct.
+# Need to find files that have these sections first.
+
+require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
+
+describe "When reading an RGB PNG file" do
+
+ before(:each) do
+ @filename = "#{Prawn::BASEDIR}/data/images/ruport.png"
+ @data_filename = "#{Prawn::BASEDIR}/data/images/ruport_data.dat"
+ @img_data = File.open(@filename, "rb") { |f| f.read }
+ end
+
+ it "should read the attributes from the header chunk correctly" do
+ png = Prawn::Images::PNG.new(@img_data)
+
+ png.width.should eql(258)
+ png.height.should eql(105)
+ png.bits.should eql(8)
+ png.color_type.should eql(2)
+ png.compression_method.should eql(0)
+ png.filter_method.should eql(0)
+ png.interlace_method.should eql(0)
+ end
+
+ it "should read the image data chunk correctly" do
+ png = Prawn::Images::PNG.new(@img_data)
+ data = File.open(@data_filename) { |f| f.read }
+ png.img_data.should eql(data)
+ end
+end
+

0 comments on commit 5f26cf2

Please sign in to comment.
Something went wrong with that request. Please try again.