forked from prawnpdf/prawn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpng_spec.rb
237 lines (195 loc) · 7.44 KB
/
png_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# encoding: ASCII-8BIT
# 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.
#
# see http://www.w3.org/TR/PNG/ for a detailed description of the PNG spec,
# particuarly Table 11.1 for the different color types
require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
describe "When reading a greyscale PNG file (color type 0)" do
before(:each) do
@filename = "#{Prawn::DATADIR}/images/web-links.png"
@data_filename = "#{Prawn::DATADIR}/images/web-links.dat"
@img_data = File.binread(@filename)
end
it "should read the attributes from the header chunk correctly" do
png = Prawn::Images::PNG.new(@img_data)
png.width.should == 21
png.height.should == 14
png.bits.should == 8
png.color_type.should == 0
png.compression_method.should == 0
png.filter_method.should == 0
png.interlace_method.should == 0
end
it "should read the image data chunk correctly" do
png = Prawn::Images::PNG.new(@img_data)
data = Zlib::Inflate.inflate(File.binread(@data_filename))
png.img_data.should == data
end
end
describe "When reading a greyscale PNG file with transparency (color type 0)" do
before(:each) do
@filename = "#{Prawn::DATADIR}/images/ruport_type0.png"
@img_data = File.binread(@filename)
end
# In a greyscale type 0 PNG image, the tRNS chunk should contain a single value
# that indicates the color that should be interpreted as transparent.
#
# http://www.w3.org/TR/PNG/#11tRNS
it "should read the tRNS chunk correctly" do
png = Prawn::Images::PNG.new(@img_data)
png.transparency[:grayscale].should == 255
end
end
describe "When reading an RGB PNG file (color type 2)" do
before(:each) do
@filename = "#{Prawn::DATADIR}/images/ruport.png"
@data_filename = "#{Prawn::DATADIR}/images/ruport_data.dat"
@img_data = File.binread(@filename)
end
it "should read the attributes from the header chunk correctly" do
png = Prawn::Images::PNG.new(@img_data)
png.width.should == 258
png.height.should == 105
png.bits.should == 8
png.color_type.should == 2
png.compression_method.should == 0
png.filter_method.should == 0
png.interlace_method.should == 0
end
it "should read the image data chunk correctly" do
png = Prawn::Images::PNG.new(@img_data)
data = Zlib::Inflate.inflate(File.binread(@data_filename))
png.img_data.should == data
end
end
describe "When reading an RGB PNG file with transparency (color type 2)" do
before(:each) do
@filename = "#{Prawn::DATADIR}/images/arrow2.png"
@img_data = File.binread(@filename)
end
# In a RGB type 2 PNG image, the tRNS chunk should contain a single RGB value
# that indicates the color that should be interpreted as transparent. In this
# case it's green.
#
# http://www.w3.org/TR/PNG/#11tRNS
it "should read the tRNS chunk correctly" do
png = Prawn::Images::PNG.new(@img_data)
png.transparency[:rgb].should == [0, 255, 0]
end
end
# TODO: describe "When reading an indexed color PNG file wiih transparency (color type 3)"
describe "When reading an indexed color PNG file (color type 3)" do
before(:each) do
@filename = "#{Prawn::DATADIR}/images/indexed_color.png"
@data_filename = "#{Prawn::DATADIR}/images/indexed_color.dat"
@img_data = File.binread(@filename)
end
it "should read the attributes from the header chunk correctly" do
png = Prawn::Images::PNG.new(@img_data)
png.width.should == 150
png.height.should == 200
png.bits.should == 8
png.color_type.should == 3
png.compression_method.should == 0
png.filter_method.should == 0
png.interlace_method.should == 0
end
it "should read the image data chunk correctly" do
png = Prawn::Images::PNG.new(@img_data)
data = Zlib::Inflate.inflate(File.binread(@data_filename))
png.img_data.should == data
end
end
describe "When reading a greyscale+alpha PNG file (color type 4)" do
before(:each) do
@filename = "#{Prawn::DATADIR}/images/page_white_text.png"
@color_data_filename = "#{Prawn::DATADIR}/images/page_white_text.color"
@alpha_data_filename = "#{Prawn::DATADIR}/images/page_white_text.alpha"
@img_data = File.binread(@filename)
end
it "should read the attributes from the header chunk correctly" do
png = Prawn::Images::PNG.new(@img_data)
png.width.should == 16
png.height.should == 16
png.bits.should == 8
png.color_type.should == 4
png.compression_method.should == 0
png.filter_method.should == 0
png.interlace_method.should == 0
end
it "should correctly return the raw image data (with no alpha channel) from the image data chunk" do
png = Prawn::Images::PNG.new(@img_data)
png.split_alpha_channel!
data = File.binread(@color_data_filename)
png.img_data.should == data
end
it "should correctly extract the alpha channel data from the image data chunk" do
png = Prawn::Images::PNG.new(@img_data)
png.split_alpha_channel!
data = File.binread(@alpha_data_filename)
png.alpha_channel.should == data
end
end
describe "When reading an RGB+alpha PNG file (color type 6)" do
before(:each) do
@filename = "#{Prawn::DATADIR}/images/dice.png"
@color_data_filename = "#{Prawn::DATADIR}/images/dice.color"
@alpha_data_filename = "#{Prawn::DATADIR}/images/dice.alpha"
@img_data = File.binread(@filename)
end
it "should read the attributes from the header chunk correctly" do
png = Prawn::Images::PNG.new(@img_data)
png.width.should == 320
png.height.should == 240
png.bits.should == 8
png.color_type.should == 6
png.compression_method.should == 0
png.filter_method.should == 0
png.interlace_method.should == 0
end
it "should correctly return the raw image data (with no alpha channel) from the image data chunk" do
png = Prawn::Images::PNG.new(@img_data)
png.split_alpha_channel!
data = File.binread(@color_data_filename)
png.img_data.should == data
end
it "should correctly extract the alpha channel data from the image data chunk" do
png = Prawn::Images::PNG.new(@img_data)
png.split_alpha_channel!
data = File.binread(@alpha_data_filename)
png.alpha_channel.should == data
end
end
describe "When reading a 16bit RGB+alpha PNG file (color type 6)" do
before(:each) do
@filename = "#{Prawn::DATADIR}/images/16bit.png"
@color_data_filename = "#{Prawn::DATADIR}/images/16bit.color"
# alpha channel truncated to 8-bit
@alpha_data_filename = "#{Prawn::DATADIR}/images/16bit.alpha"
@img_data = File.binread(@filename)
end
it "should read the attributes from the header chunk correctly" do
png = Prawn::Images::PNG.new(@img_data)
png.width.should == 32
png.height.should == 32
png.bits.should == 16
png.color_type.should == 6
png.compression_method.should == 0
png.filter_method.should == 0
png.interlace_method.should == 0
end
it "should correctly return the raw image data (with no alpha channel) from the image data chunk" do
png = Prawn::Images::PNG.new(@img_data)
png.split_alpha_channel!
data = File.binread(@color_data_filename)
png.img_data.should == data
end
it "should correctly extract the alpha channel data from the image data chunk" do
png = Prawn::Images::PNG.new(@img_data)
png.split_alpha_channel!
data = File.binread(@alpha_data_filename)
png.alpha_channel.should == data
end
end