chunky_png 1.1.0 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/chunky_png.gemspec CHANGED
@@ -3,8 +3,8 @@ Gem::Specification.new do |s|
3
3
 
4
4
  # Do not change the version and date fields by hand. This will be done
5
5
  # automatically by the gem release script.
6
- s.version = "1.1.0"
7
- s.date = "2011-03-19"
6
+ s.version = "1.1.1"
7
+ s.date = "2011-04-22"
8
8
 
9
9
  s.summary = "Pure ruby library for read/write, chunk-level access to PNG files"
10
10
  s.description = <<-EOT
data/lib/chunky_png.rb CHANGED
@@ -25,7 +25,7 @@ module ChunkyPNG
25
25
 
26
26
  # The current version of ChunkyPNG. This value will be updated
27
27
  # automatically by them <tt>gem:release</tt> rake task.
28
- VERSION = "1.1.0"
28
+ VERSION = "1.1.1"
29
29
 
30
30
  ###################################################
31
31
  # PNG international standard defined constants
@@ -23,7 +23,24 @@ module ChunkyPNG
23
23
  #
24
24
  # @return [String] The RGB-formatted pixel data.
25
25
  def to_rgb_stream
26
- pixels.pack('NX' * (width * height))
26
+ pixels.pack('NX' * pixels.length)
27
+ end
28
+
29
+ # Creates a stream of the alpha channel of this canvas.
30
+ #
31
+ # @return [String] The 0-255 alpha values of all pixels packed as string
32
+ def to_alpha_channel_stream
33
+ pixels.pack('C*')
34
+ end
35
+
36
+ # Creates a grayscale stream of this canvas.
37
+ #
38
+ # This method assume sthat this image is fully grayscale, i.e. R = G = B for
39
+ # every pixel. The alpha channel will not be included in the stream.
40
+ #
41
+ # @return [String] The 0-255 grayscale values of all pixels packed as string.
42
+ def to_grayscale_stream
43
+ pixels.pack('nX' * pixels.length)
27
44
  end
28
45
 
29
46
  # Creates an ABGR-formatted pixelstream with the pixel data from this canvas.
@@ -3,18 +3,57 @@ require 'spec_helper'
3
3
  describe ChunkyPNG::Canvas do
4
4
 
5
5
  describe '#to_rgba_stream' do
6
- before { File.open(resource_file('pixelstream.rgba'), 'rb') { |f| @reference_data = f.read } }
7
-
8
- it "should load an image correctly from a datastream" do
9
- reference_canvas('pixelstream_reference').to_rgba_stream.should == @reference_data
6
+ it "should export a sample canvas to an RGBA stream correctly" do
7
+ canvas = ChunkyPNG::Canvas.new(2, 2, [ChunkyPNG::Color.rgba(1,2,3,4), ChunkyPNG::Color.rgba(5,6,7,8),
8
+ ChunkyPNG::Color.rgba(4,3,2,1), ChunkyPNG::Color.rgba(8,7,6,5)])
9
+
10
+ canvas.to_rgba_stream.should == [1,2,3,4,5,6,7,8,4,3,2,1,8,7,6,5].pack('C16')
10
11
  end
12
+
13
+ it "should export an image to an RGBA datastream correctly" do
14
+ reference_canvas('pixelstream_reference').to_rgba_stream.should == resource_data('pixelstream.rgba')
15
+ end
11
16
  end
12
17
 
13
18
  describe '#to_rgb_stream' do
14
- before { File.open(resource_file('pixelstream.rgb'), 'rb') { |f| @reference_data = f.read } }
19
+ it "should export a sample canvas to an RGBA stream correctly" do
20
+ canvas = ChunkyPNG::Canvas.new(2, 2, [ChunkyPNG::Color.rgba(1,2,3,4), ChunkyPNG::Color.rgba(5,6,7,8),
21
+ ChunkyPNG::Color.rgba(4,3,2,1), ChunkyPNG::Color.rgba(8,7,6,5)])
22
+
23
+ canvas.to_rgb_stream.should == [1,2,3,5,6,7,4,3,2,8,7,6].pack('C12')
24
+ end
25
+
26
+ it "should export an image to an RGB datastream correctly" do
27
+ reference_canvas('pixelstream_reference').to_rgb_stream.should == resource_data('pixelstream.rgb')
28
+ end
29
+ end
30
+
31
+ describe '#to_grayscale_stream' do
32
+
33
+ it "should export a grayscale image to a grayscale datastream correctly" do
34
+ canvas = ChunkyPNG::Canvas.new(2, 2, [ChunkyPNG::Color.grayscale(1), ChunkyPNG::Color.grayscale(2),
35
+ ChunkyPNG::Color.grayscale(3), ChunkyPNG::Color.grayscale(4)])
36
+ canvas.to_grayscale_stream.should == [1,2,3,4].pack('C4')
37
+ end
38
+
39
+
40
+ it "should export a color image to a grayscale datastream, using B values" do
41
+ canvas = ChunkyPNG::Canvas.new(2, 2, [ChunkyPNG::Color.rgba(1,2,3,4), ChunkyPNG::Color.rgba(5,6,7,8),
42
+ ChunkyPNG::Color.rgba(4,3,2,1), ChunkyPNG::Color.rgba(8,7,6,5)])
43
+ canvas.to_grayscale_stream.should == [3,7,2,6].pack('C4')
44
+ end
45
+ end
46
+
47
+ describe '#to_alpha_channel_stream' do
48
+ it "should export an opaque image to an alpha channel datastream correctly" do
49
+ grayscale_array = Array.new(reference_canvas('pixelstream_reference').pixels.length, 255)
50
+ reference_canvas('pixelstream_reference').to_alpha_channel_stream.should == grayscale_array.pack('C*')
51
+ end
15
52
 
16
- it "should load an image correctly from a datastream" do
17
- reference_canvas('pixelstream_reference').to_rgb_stream.should == @reference_data
53
+ it "should export a transparent image to an alpha channel datastream correctly" do
54
+ canvas = ChunkyPNG::Canvas.new(2, 2, [ChunkyPNG::Color.rgba(1,2,3,4), ChunkyPNG::Color.rgba(5,6,7,8),
55
+ ChunkyPNG::Color.rgba(4,3,2,1), ChunkyPNG::Color.rgba(8,7,6,5)])
56
+ canvas.to_alpha_channel_stream.should == [4,8,1,5].pack('C4')
18
57
  end
19
58
  end
20
59
  end
@@ -1,37 +1,8 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe ChunkyPNG do
4
-
5
- # it "should create reference images for all color modes" do
6
- # image = ChunkyPNG::Image.new(10, 10, ChunkyPNG::Color.rgb(100, 100, 100))
7
- # [:indexed, :grayscale, :grayscale_alpha, :truecolor, :truecolor_alpha].each do |color_mode|
8
- #
9
- # color_mode_id = ChunkyPNG.const_get("COLOR_#{color_mode.to_s.upcase}")
10
- # filename = resource_file("gray_10x10_#{color_mode}.png")
11
- # image.save(filename, :color_mode => color_mode_id)
12
- # end
13
- # end
14
-
15
- # it "should create a reference image for operations" do
16
- # image = ChunkyPNG::Image.new(16, 16, ChunkyPNG::Color::WHITE)
17
- # r = 0
18
- # image.width.times do |x|
19
- # g = 0
20
- # image.height.times do |y|
21
- # image[x, y] = ChunkyPNG::Color.rgb(r, g, 255)
22
- # g += 16
23
- # end
24
- # r += 16
25
- # end
26
- # filename = resource_file('operations.png')
27
- # image.save(filename)
28
- # # `open #{filename}`
29
- # end
30
4
 
31
- # it "should create damaged CRC values" do
32
- # Zlib.stub!(:crc32).and_return(12345)
33
- # image = ChunkyPNG::Image.new(10, 10, ChunkyPNG::Color::BLACK)
34
- # image.save(resource_file('damaged_chunk.png'))
35
- # end
5
+ it "should have a VERSION constant" do
6
+ ChunkyPNG.const_defined?('VERSION').should be_true
7
+ end
36
8
  end
37
-
@@ -14,20 +14,6 @@ describe 'PNG testuite' do
14
14
  end
15
15
  end
16
16
 
17
- context 'Decoding unsupported images' do
18
-
19
- # TODO: we eventually want to support these!
20
-
21
- png_suite_files(:basic_not_supported).each do |file|
22
- color_mode = file.match(/[in](\d)[apgc](\d\d)\.png$/)[1].to_i
23
- bit_depth = file.match(/[in](\d)[apgc](\d\d)\.png$/)[2].to_i
24
-
25
- it "should report #{File.basename(file)} (color mode: #{color_mode}, bit depth: #{bit_depth}) as unsupported" do
26
- lambda { ChunkyPNG::Image.from_file(file) }.should raise_error(ChunkyPNG::NotSupported)
27
- end
28
- end
29
- end
30
-
31
17
  context 'Decoding supported images' do
32
18
  png_suite_files(:basic, '*.png').each do |file|
33
19
 
data/spec/spec_helper.rb CHANGED
@@ -24,6 +24,12 @@ module ResourceFileHelper
24
24
  File.expand_path("./resources/#{name}", File.dirname(__FILE__))
25
25
  end
26
26
 
27
+ def resource_data(name)
28
+ data = nil
29
+ File.open(resource_file(name), 'rb') { |f| data = f.read }
30
+ data
31
+ end
32
+
27
33
  def reference_canvas(name)
28
34
  ChunkyPNG::Canvas.from_file(resource_file("#{name}.png"))
29
35
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 1
8
- - 0
9
- version: 1.1.0
8
+ - 1
9
+ version: 1.1.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Willem van Bergen
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-03-19 00:00:00 -04:00
17
+ date: 2011-04-22 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency