chunky_png 1.3.8 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (73) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/ruby.yml +35 -0
  3. data/.standard.yml +16 -0
  4. data/.yardopts +1 -1
  5. data/CHANGELOG.rdoc +16 -4
  6. data/CONTRIBUTING.rdoc +17 -8
  7. data/Gemfile +12 -4
  8. data/LICENSE +1 -1
  9. data/README.md +15 -9
  10. data/Rakefile +5 -3
  11. data/benchmarks/decoding_benchmark.rb +17 -17
  12. data/benchmarks/encoding_benchmark.rb +22 -19
  13. data/benchmarks/filesize_benchmark.rb +6 -6
  14. data/bin/rake +29 -0
  15. data/bin/standardrb +29 -0
  16. data/chunky_png.gemspec +21 -13
  17. data/docs/.gitignore +3 -0
  18. data/docs/CNAME +1 -0
  19. data/docs/_config.yml +9 -0
  20. data/docs/_posts/2010-01-14-memory-efficiency-when-using-ruby.md +136 -0
  21. data/docs/_posts/2010-01-17-ode-to-array-pack-and-string-unpack.md +82 -0
  22. data/docs/_posts/2014-11-07-the-value-of-a-pure-ruby-library.md +61 -0
  23. data/docs/index.md +88 -0
  24. data/lib/chunky_png/canvas/adam7_interlacing.rb +16 -10
  25. data/lib/chunky_png/canvas/data_url_exporting.rb +3 -3
  26. data/lib/chunky_png/canvas/data_url_importing.rb +3 -3
  27. data/lib/chunky_png/canvas/drawing.rb +30 -43
  28. data/lib/chunky_png/canvas/masking.rb +14 -14
  29. data/lib/chunky_png/canvas/operations.rb +28 -24
  30. data/lib/chunky_png/canvas/png_decoding.rb +39 -33
  31. data/lib/chunky_png/canvas/png_encoding.rb +111 -103
  32. data/lib/chunky_png/canvas/resampling.rb +27 -32
  33. data/lib/chunky_png/canvas/stream_exporting.rb +8 -8
  34. data/lib/chunky_png/canvas/stream_importing.rb +8 -8
  35. data/lib/chunky_png/canvas.rb +31 -28
  36. data/lib/chunky_png/chunk.rb +142 -69
  37. data/lib/chunky_png/color.rb +218 -212
  38. data/lib/chunky_png/datastream.rb +24 -30
  39. data/lib/chunky_png/dimension.rb +18 -11
  40. data/lib/chunky_png/image.rb +11 -11
  41. data/lib/chunky_png/palette.rb +13 -14
  42. data/lib/chunky_png/point.rb +27 -26
  43. data/lib/chunky_png/rmagick.rb +10 -10
  44. data/lib/chunky_png/vector.rb +28 -29
  45. data/lib/chunky_png/version.rb +3 -1
  46. data/lib/chunky_png.rb +46 -45
  47. data/spec/chunky_png/canvas/adam7_interlacing_spec.rb +20 -21
  48. data/spec/chunky_png/canvas/data_url_exporting_spec.rb +8 -5
  49. data/spec/chunky_png/canvas/data_url_importing_spec.rb +5 -6
  50. data/spec/chunky_png/canvas/drawing_spec.rb +46 -38
  51. data/spec/chunky_png/canvas/masking_spec.rb +15 -16
  52. data/spec/chunky_png/canvas/operations_spec.rb +68 -67
  53. data/spec/chunky_png/canvas/png_decoding_spec.rb +37 -38
  54. data/spec/chunky_png/canvas/png_encoding_spec.rb +59 -50
  55. data/spec/chunky_png/canvas/resampling_spec.rb +19 -21
  56. data/spec/chunky_png/canvas/stream_exporting_spec.rb +47 -27
  57. data/spec/chunky_png/canvas/stream_importing_spec.rb +10 -11
  58. data/spec/chunky_png/canvas_spec.rb +63 -52
  59. data/spec/chunky_png/color_spec.rb +115 -114
  60. data/spec/chunky_png/datastream_spec.rb +98 -19
  61. data/spec/chunky_png/dimension_spec.rb +10 -10
  62. data/spec/chunky_png/image_spec.rb +11 -14
  63. data/spec/chunky_png/point_spec.rb +21 -23
  64. data/spec/chunky_png/rmagick_spec.rb +7 -8
  65. data/spec/chunky_png/vector_spec.rb +21 -17
  66. data/spec/chunky_png_spec.rb +2 -2
  67. data/spec/png_suite_spec.rb +35 -40
  68. data/spec/resources/itxt_chunk.png +0 -0
  69. data/spec/spec_helper.rb +15 -9
  70. data/tasks/benchmarks.rake +7 -8
  71. metadata +65 -25
  72. data/.travis.yml +0 -16
  73. data/lib/chunky_png/compatibility.rb +0 -15
@@ -1,22 +1,22 @@
1
- require 'spec_helper'
1
+ require "spec_helper"
2
2
 
3
- describe 'ChunyPNG.Color' do
4
- it 'should interpret 4 arguments as RGBA values' do
3
+ describe "ChunyPNG.Color" do
4
+ it "should interpret 4 arguments as RGBA values" do
5
5
  expect(ChunkyPNG::Color(1, 2, 3, 4)).to eql ChunkyPNG::Color.rgba(1, 2, 3, 4)
6
6
  end
7
7
 
8
- it 'should interpret 3 arguments as RGBA values' do
8
+ it "should interpret 3 arguments as RGBA values" do
9
9
  expect(ChunkyPNG::Color(1, 2, 3)).to eql ChunkyPNG::Color.rgb(1, 2, 3)
10
10
  end
11
11
 
12
- it 'should interpret 2 arguments as a color to parse and an opacity value' do
13
- expect(ChunkyPNG::Color('0x0a649664', 0xaa)).to eql 0x0a6496aa
14
- expect(ChunkyPNG::Color('spring green @ 0.6666', 0xff)).to eql 0x00ff7fff
12
+ it "should interpret 2 arguments as a color to parse and an opacity value" do
13
+ expect(ChunkyPNG::Color("0x0a649664", 0xaa)).to eql 0x0a6496aa
14
+ expect(ChunkyPNG::Color("spring green @ 0.6666", 0xff)).to eql 0x00ff7fff
15
15
  end
16
16
 
17
- it 'should interpret 1 argument as a color to parse' do
18
- expect(ChunkyPNG::Color).to receive(:parse).with('0x0a649664')
19
- ChunkyPNG::Color('0x0a649664')
17
+ it "should interpret 1 argument as a color to parse" do
18
+ expect(ChunkyPNG::Color).to receive(:parse).with("0x0a649664")
19
+ ChunkyPNG::Color("0x0a649664")
20
20
  end
21
21
  end
22
22
 
@@ -34,85 +34,87 @@ describe ChunkyPNG::Color do
34
34
  @blue = 0x0000ffff
35
35
  end
36
36
 
37
- describe '#parse' do
38
- it 'should interpret a hex string correctly' do
39
- expect(parse('0x0a649664')).to eql ChunkyPNG::Color.from_hex('#0a649664')
37
+ describe "#parse" do
38
+ it "should interpret a hex string correctly" do
39
+ expect(parse("0x0a649664")).to eql ChunkyPNG::Color.from_hex("#0a649664")
40
40
  end
41
41
 
42
- it 'should interpret a color name correctly' do
42
+ it "should interpret a color name correctly" do
43
43
  expect(parse(:spring_green)).to eql 0x00ff7fff
44
- expect(parse('spring green')).to eql 0x00ff7fff
45
- expect(parse('spring green @ 0.6666')).to eql 0x00ff7faa
44
+ expect(parse("spring green")).to eql 0x00ff7fff
45
+ expect(parse("spring green @ 0.6666")).to eql 0x00ff7faa
46
46
  end
47
47
 
48
- it 'should return numbers as is' do
49
- expect(parse('12345')).to eql 12345
48
+ it "should return numbers as is" do
49
+ expect(parse("12345")).to eql 12345
50
50
  expect(parse(12345)).to eql 12345
51
51
  end
52
52
  end
53
53
 
54
- describe '#pixel_bytesize' do
55
- it 'should return the normal amount of bytes with a bit depth of 8' do
54
+ describe "#pixel_bytesize" do
55
+ it "should return the normal amount of bytes with a bit depth of 8" do
56
56
  expect(pixel_bytesize(ChunkyPNG::COLOR_TRUECOLOR, 8)).to eql 3
57
57
  end
58
58
 
59
- it 'should return a multiple of the normal amount of bytes with a bit depth greater than 8' do
59
+ it "should return a multiple of the normal amount of bytes with a bit depth greater than 8" do
60
60
  expect(pixel_bytesize(ChunkyPNG::COLOR_TRUECOLOR, 16)).to eql 6
61
61
  expect(pixel_bytesize(ChunkyPNG::COLOR_TRUECOLOR_ALPHA, 16)).to eql 8
62
62
  expect(pixel_bytesize(ChunkyPNG::COLOR_GRAYSCALE_ALPHA, 16)).to eql 4
63
63
  end
64
64
 
65
- it 'should return 1 with a bit depth lower than 0' do
65
+ it "should return 1 with a bit depth lower than 0" do
66
66
  expect(pixel_bytesize(ChunkyPNG::COLOR_TRUECOLOR, 4)).to eql 1
67
67
  expect(pixel_bytesize(ChunkyPNG::COLOR_INDEXED, 2)).to eql 1
68
68
  expect(pixel_bytesize(ChunkyPNG::COLOR_GRAYSCALE_ALPHA, 1)).to eql 1
69
69
  end
70
70
  end
71
71
 
72
- describe '#pass_bytesize' do
73
- it 'should calculate a pass size correctly' do
72
+ describe "#pass_bytesize" do
73
+ it "should calculate a pass size correctly" do
74
74
  expect(pass_bytesize(ChunkyPNG::COLOR_TRUECOLOR, 8, 10, 10)).to eql 310
75
75
  end
76
76
 
77
- it 'should return 0 if one of the dimensions is zero' do
77
+ it "should return 0 if one of the dimensions is zero" do
78
78
  expect(pass_bytesize(ChunkyPNG::COLOR_TRUECOLOR, 8, 0, 10)).to eql 0
79
79
  expect(pass_bytesize(ChunkyPNG::COLOR_TRUECOLOR, 8, 10, 0)).to eql 0
80
80
  end
81
81
  end
82
82
 
83
- describe '#rgba' do
84
- it 'should represent pixels as the correct number' do
83
+ describe "#rgba" do
84
+ it "should represent pixels as the correct number" do
85
+ # rubocop:disable Layout/ExtraSpacing, Layout/SpaceInsideParens
85
86
  expect(rgba(255, 255, 255, 255)).to eql @white
86
87
  expect(rgba( 0, 0, 0, 255)).to eql @black
87
88
  expect(rgba( 10, 100, 150, 255)).to eql @opaque
88
89
  expect(rgba( 10, 100, 150, 100)).to eql @non_opaque
89
90
  expect(rgba( 10, 100, 150, 0)).to eql @fully_transparent
91
+ # rubocop:enable Layout/ExtraSpacing, Layout/SpaceInsideParens
90
92
  end
91
93
  end
92
94
 
93
- describe '#from_hex' do
94
- it 'should load colors correctly from hex notation' do
95
- expect(from_hex('0a649664')).to eql @non_opaque
96
- expect(from_hex('#0a649664')).to eql @non_opaque
97
- expect(from_hex('0x0a649664')).to eql @non_opaque
98
- expect(from_hex('0a6496')).to eql @opaque
99
- expect(from_hex('#0a6496')).to eql @opaque
100
- expect(from_hex('0x0a6496')).to eql @opaque
101
- expect(from_hex('abc')).to eql 0xaabbccff
102
- expect(from_hex('#abc')).to eql 0xaabbccff
103
- expect(from_hex('0xabc')).to eql 0xaabbccff
104
- end
105
-
106
- it 'should allow setting opacity explicitly' do
107
- expect(from_hex('0x0a6496', 0x64)).to eql @non_opaque
108
- expect(from_hex('#0a6496', 0x64)).to eql @non_opaque
109
- expect(from_hex('0xabc', 0xdd)).to eql 0xaabbccdd
110
- expect(from_hex('#abc', 0xdd)).to eql 0xaabbccdd
95
+ describe "#from_hex" do
96
+ it "should load colors correctly from hex notation" do
97
+ expect(from_hex("0a649664")).to eql @non_opaque
98
+ expect(from_hex("#0a649664")).to eql @non_opaque
99
+ expect(from_hex("0x0a649664")).to eql @non_opaque
100
+ expect(from_hex("0a6496")).to eql @opaque
101
+ expect(from_hex("#0a6496")).to eql @opaque
102
+ expect(from_hex("0x0a6496")).to eql @opaque
103
+ expect(from_hex("abc")).to eql 0xaabbccff
104
+ expect(from_hex("#abc")).to eql 0xaabbccff
105
+ expect(from_hex("0xabc")).to eql 0xaabbccff
106
+ end
107
+
108
+ it "should allow setting opacity explicitly" do
109
+ expect(from_hex("0x0a6496", 0x64)).to eql @non_opaque
110
+ expect(from_hex("#0a6496", 0x64)).to eql @non_opaque
111
+ expect(from_hex("0xabc", 0xdd)).to eql 0xaabbccdd
112
+ expect(from_hex("#abc", 0xdd)).to eql 0xaabbccdd
111
113
  end
112
114
  end
113
115
 
114
- describe '#from_hsv' do
115
- it 'should load colors correctly from an HSV triple' do
116
+ describe "#from_hsv" do
117
+ it "should load colors correctly from an HSV triple" do
116
118
  # At 0 brightness, should be @black independent of hue or sat
117
119
  expect(from_hsv(0, 0, 0)).to eql @black
118
120
  expect(from_hsv(100, 1, 0)).to eql @black
@@ -135,7 +137,7 @@ describe ChunkyPNG::Color do
135
137
  expect(from_hsv(0, 0.5, 0.5)).to eql from_hsv(360.0, 0.5, 0.5)
136
138
  end
137
139
 
138
- it 'should optionally accept a fourth param for alpha' do
140
+ it "should optionally accept a fourth param for alpha" do
139
141
  expect(from_hsv(0, 1, 1, 255)).to eql @red
140
142
  expect(from_hsv(120, 1, 1, 255)).to eql @green
141
143
  expect(from_hsv(240, 1, 1, 255)).to eql @blue
@@ -145,8 +147,8 @@ describe ChunkyPNG::Color do
145
147
  end
146
148
  end
147
149
 
148
- describe '#from_hsl' do
149
- it 'should load colors correctly from an HSL triple' do
150
+ describe "#from_hsl" do
151
+ it "should load colors correctly from an HSL triple" do
150
152
  # At 0 lightness, should always be black
151
153
  expect(from_hsl(0, 0, 0)).to eql @black
152
154
  expect(from_hsl(100, 0, 0)).to eql @black
@@ -163,16 +165,16 @@ describe ChunkyPNG::Color do
163
165
  expect(from_hsl(240, 1, 0.5)).to eql @blue
164
166
 
165
167
  # Random colors
166
- from_hsl(87.27, 0.5, 0.5686) == 0x96c85aff
167
- from_hsl(271.83, 0.5399, 0.4176) == 0x6e31a4ff
168
- from_hsl(63.6, 0.5984, 0.4882) == 0xbec732ff
168
+ expect(from_hsl(87.27, 0.5, 0.5686)).to eql 0x95c759ff
169
+ expect(from_hsl(271.83, 0.5399, 0.4176)).to eql 0x6d30a3ff
170
+ expect(from_hsl(63.6, 0.5984, 0.4882)).to eql 0xbec631ff
169
171
 
170
172
  # Hue 0 and hue 360 should be equivalent
171
173
  expect(from_hsl(0, 0.5, 0.5)).to eql from_hsl(360, 0.5, 0.5)
172
174
  expect(from_hsl(0, 0.5, 0.5)).to eql from_hsl(360.0, 0.5, 0.5)
173
175
  end
174
176
 
175
- it 'should optionally accept a fourth param for alpha' do
177
+ it "should optionally accept a fourth param for alpha" do
176
178
  expect(from_hsl(0, 1, 0.5, 255)).to eql @red
177
179
  expect(from_hsl(120, 1, 0.5, 255)).to eql @green
178
180
  expect(from_hsl(240, 1, 0.5, 255)).to eql @blue
@@ -182,35 +184,35 @@ describe ChunkyPNG::Color do
182
184
  end
183
185
  end
184
186
 
185
- describe '#html_color' do
186
- it 'should find the correct color value' do
187
+ describe "#html_color" do
188
+ it "should find the correct color value" do
187
189
  expect(html_color(:springgreen)).to eql 0x00ff7fff
188
190
  expect(html_color(:spring_green)).to eql 0x00ff7fff
189
- expect(html_color('springgreen')).to eql 0x00ff7fff
190
- expect(html_color('spring green')).to eql 0x00ff7fff
191
- expect(html_color('SpringGreen')).to eql 0x00ff7fff
192
- expect(html_color('SPRING_GREEN')).to eql 0x00ff7fff
191
+ expect(html_color("springgreen")).to eql 0x00ff7fff
192
+ expect(html_color("spring green")).to eql 0x00ff7fff
193
+ expect(html_color("SpringGreen")).to eql 0x00ff7fff
194
+ expect(html_color("SPRING_GREEN")).to eql 0x00ff7fff
193
195
  end
194
196
 
195
- it 'should set the opacity level explicitly' do
197
+ it "should set the opacity level explicitly" do
196
198
  expect(html_color(:springgreen, 0xff)).to eql 0x00ff7fff
197
199
  expect(html_color(:springgreen, 0xaa)).to eql 0x00ff7faa
198
200
  expect(html_color(:springgreen, 0x00)).to eql 0x00ff7f00
199
201
  end
200
202
 
201
- it 'should set opacity levels from the color name' do
202
- expect(html_color('Spring green @ 1.0')).to eql 0x00ff7fff
203
- expect(html_color('Spring green @ 0.666')).to eql 0x00ff7faa
204
- expect(html_color('Spring green @ 0.0')).to eql 0x00ff7f00
203
+ it "should set opacity levels from the color name" do
204
+ expect(html_color("Spring green @ 1.0")).to eql 0x00ff7fff
205
+ expect(html_color("Spring green @ 0.666")).to eql 0x00ff7faa
206
+ expect(html_color("Spring green @ 0.0")).to eql 0x00ff7f00
205
207
  end
206
208
 
207
- it 'should raise for an unkown color name' do
209
+ it "should raise for an unkown color name" do
208
210
  expect { html_color(:nonsense) }.to raise_error(ArgumentError)
209
211
  end
210
212
  end
211
213
 
212
- describe '#opaque?' do
213
- it 'should correctly check for opaqueness' do
214
+ describe "#opaque?" do
215
+ it "should correctly check for opaqueness" do
214
216
  expect(opaque?(@white)).to eql true
215
217
  expect(opaque?(@black)).to eql true
216
218
  expect(opaque?(@opaque)).to eql true
@@ -219,8 +221,8 @@ describe ChunkyPNG::Color do
219
221
  end
220
222
  end
221
223
 
222
- describe 'extraction of separate color channels' do
223
- it 'should extract components from a color correctly' do
224
+ describe "extraction of separate color channels" do
225
+ it "should extract components from a color correctly" do
224
226
  expect(r(@opaque)).to eql 10
225
227
  expect(g(@opaque)).to eql 100
226
228
  expect(b(@opaque)).to eql 150
@@ -228,47 +230,47 @@ describe ChunkyPNG::Color do
228
230
  end
229
231
  end
230
232
 
231
- describe '#grayscale_teint' do
232
- it 'should calculate the correct grayscale teint' do
233
+ describe "#grayscale_teint" do
234
+ it "should calculate the correct grayscale teint" do
233
235
  expect(grayscale_teint(@opaque)).to eql 79
234
236
  expect(grayscale_teint(@non_opaque)).to eql 79
235
237
  end
236
238
  end
237
239
 
238
- describe '#to_grayscale' do
239
- it 'should use the grayscale teint for r, g and b' do
240
+ describe "#to_grayscale" do
241
+ it "should use the grayscale teint for r, g and b" do
240
242
  gs = to_grayscale(@non_opaque)
241
243
  expect(r(gs)).to eql grayscale_teint(@non_opaque)
242
244
  expect(g(gs)).to eql grayscale_teint(@non_opaque)
243
245
  expect(b(gs)).to eql grayscale_teint(@non_opaque)
244
246
  end
245
247
 
246
- it 'should preserve the alpha channel' do
248
+ it "should preserve the alpha channel" do
247
249
  expect(a(to_grayscale(@non_opaque))).to eql a(@non_opaque)
248
250
  expect(a(to_grayscale(@opaque))).to eql ChunkyPNG::Color::MAX
249
251
  end
250
252
  end
251
253
 
252
- describe '#to_hex' do
253
- it 'should represent colors correcly using hex notation' do
254
- expect(to_hex(@white)).to eql '#ffffffff'
255
- expect(to_hex(@black)).to eql '#000000ff'
256
- expect(to_hex(@opaque)).to eql '#0a6496ff'
257
- expect(to_hex(@non_opaque)).to eql '#0a649664'
258
- expect(to_hex(@fully_transparent)).to eql '#0a649600'
254
+ describe "#to_hex" do
255
+ it "should represent colors correcly using hex notation" do
256
+ expect(to_hex(@white)).to eql "#ffffffff"
257
+ expect(to_hex(@black)).to eql "#000000ff"
258
+ expect(to_hex(@opaque)).to eql "#0a6496ff"
259
+ expect(to_hex(@non_opaque)).to eql "#0a649664"
260
+ expect(to_hex(@fully_transparent)).to eql "#0a649600"
259
261
  end
260
262
 
261
- it 'should represent colors correcly using hex notation without alpha channel' do
262
- expect(to_hex(@white, false)).to eql '#ffffff'
263
- expect(to_hex(@black, false)).to eql '#000000'
264
- expect(to_hex(@opaque, false)).to eql '#0a6496'
265
- expect(to_hex(@non_opaque, false)).to eql '#0a6496'
266
- expect(to_hex(@fully_transparent, false)).to eql '#0a6496'
263
+ it "should represent colors correcly using hex notation without alpha channel" do
264
+ expect(to_hex(@white, false)).to eql "#ffffff"
265
+ expect(to_hex(@black, false)).to eql "#000000"
266
+ expect(to_hex(@opaque, false)).to eql "#0a6496"
267
+ expect(to_hex(@non_opaque, false)).to eql "#0a6496"
268
+ expect(to_hex(@fully_transparent, false)).to eql "#0a6496"
267
269
  end
268
270
  end
269
271
 
270
- describe '#to_hsv' do
271
- it 'should return a [hue, saturation, value] array' do
272
+ describe "#to_hsv" do
273
+ it "should return a [hue, saturation, value] array" do
272
274
  expect(to_hsv(@white)).to eql [0, 0.0, 1.0]
273
275
  expect(to_hsv(@black)).to eql [0, 0.0, 0.0]
274
276
  expect(to_hsv(@red)).to eql [0, 1.0, 1.0]
@@ -279,7 +281,7 @@ describe ChunkyPNG::Color do
279
281
  expect(to_hsv(0x805440ff)[2]).to be_within(0.01).of(0.5)
280
282
  end
281
283
 
282
- it 'should optionally include the alpha channel' do
284
+ it "should optionally include the alpha channel" do
283
285
  expect(to_hsv(@white, true)).to eql [0, 0.0, 1.0, 255]
284
286
  expect(to_hsv(@red, true)).to eql [0, 1.0, 1.0, 255]
285
287
  expect(to_hsv(@blue, true)).to eql [240, 1.0, 1.0, 255]
@@ -289,8 +291,8 @@ describe ChunkyPNG::Color do
289
291
  end
290
292
  end
291
293
 
292
- describe '#to_hsl' do
293
- it 'should return a [hue, saturation, lightness] array' do
294
+ describe "#to_hsl" do
295
+ it "should return a [hue, saturation, lightness] array" do
294
296
  expect(to_hsl(@white)).to eql [0, 0.0, 1.0]
295
297
  expect(to_hsl(@black)).to eql [0, 0.0, 0.0]
296
298
  expect(to_hsl(@red)).to eql [0, 1.0, 0.5]
@@ -298,7 +300,7 @@ describe ChunkyPNG::Color do
298
300
  expect(to_hsl(@green)).to eql [120, 1.0, 0.5]
299
301
  end
300
302
 
301
- it 'should optionally include the alpha channel in the returned array' do
303
+ it "should optionally include the alpha channel in the returned array" do
302
304
  expect(to_hsl(@white, true)).to eql [0, 0.0, 1.0, 255]
303
305
  expect(to_hsl(@black, true)).to eql [0, 0.0, 0.0, 255]
304
306
  expect(to_hsl(@red, true)).to eql [0, 1.0, 0.5, 255]
@@ -309,79 +311,78 @@ describe ChunkyPNG::Color do
309
311
  end
310
312
  end
311
313
 
312
- describe 'conversion to other formats' do
313
- it 'should convert the individual color values back correctly' do
314
+ describe "conversion to other formats" do
315
+ it "should convert the individual color values back correctly" do
314
316
  expect(to_truecolor_bytes(@opaque)).to eql [10, 100, 150]
315
317
  expect(to_truecolor_alpha_bytes(@non_opaque)).to eql [10, 100, 150, 100]
316
318
  end
317
319
  end
318
320
 
319
- describe '#compose' do
320
-
321
- it 'should use the foregorund color as is when the background color is fully transparent' do
321
+ describe "#compose" do
322
+ it "should use the foregorund color as is when the background color is fully transparent" do
322
323
  expect(compose(@non_opaque, @fully_transparent)).to eql @non_opaque
323
324
  end
324
325
 
325
- it 'should use the foregorund color as is when an opaque color is given as foreground color' do
326
+ it "should use the foregorund color as is when an opaque color is given as foreground color" do
326
327
  expect(compose(@opaque, @white)).to eql @opaque
327
328
  end
328
329
 
329
- it 'should use the background color as is when a fully transparent pixel is given as foreground color' do
330
+ it "should use the background color as is when a fully transparent pixel is given as foreground color" do
330
331
  expect(compose(@fully_transparent, @white)).to eql @white
331
332
  end
332
333
 
333
- it 'should compose pixels correctly with both algorithms' do
334
+ it "should compose pixels correctly with both algorithms" do
334
335
  expect(compose_quick(@non_opaque, @white)).to eql 0x9fc2d6ff
335
336
  expect(compose_precise(@non_opaque, @white)).to eql 0x9fc2d6ff
336
337
  end
337
338
  end
338
339
 
339
- describe '#decompose_alpha' do
340
- it 'should decompose the alpha channel correctly' do
340
+ describe "#decompose_alpha" do
341
+ it "should decompose the alpha channel correctly" do
341
342
  expect(decompose_alpha(0x9fc2d6ff, @opaque, @white)).to eql 0x00000064
342
343
  end
343
344
 
344
- it 'should return fully transparent if the background channel matches the resulting color' do
345
+ it "should return fully transparent if the background channel matches the resulting color" do
345
346
  expect(decompose_alpha(0xabcdefff, 0xff000000, 0xabcdefff)).to eql 0x00
346
347
  end
347
348
 
348
- it 'should return fully opaque if the background channel matches the mask color' do
349
+ it "should return fully opaque if the background channel matches the mask color" do
349
350
  expect(decompose_alpha(0xff000000, 0xabcdefff, 0xabcdefff)).to eql 0xff
350
351
  end
351
352
 
352
- it 'should return fully opaque if the resulting color matches the mask color' do
353
+ it "should return fully opaque if the resulting color matches the mask color" do
353
354
  expect(decompose_alpha(0xabcdefff, 0xabcdefff, 0xffffffff)).to eql 255
354
355
  end
355
356
  end
356
357
 
357
- describe '#blend' do
358
- it 'should blend colors correctly' do
358
+ describe "#blend" do
359
+ it "should blend colors correctly" do
359
360
  expect(blend(@opaque, @black)).to eql 0x05324bff
360
361
  end
361
362
 
362
- it 'should not matter what color is used as foreground, and what as background' do
363
+ it "should not matter what color is used as foreground, and what as background" do
363
364
  expect(blend(@opaque, @black)).to eql blend(@black, @opaque)
364
365
  end
365
366
  end
366
367
 
367
- describe '#euclidean_distance_rgba' do
368
+ describe "#euclidean_distance_rgba" do
368
369
  subject { euclidean_distance_rgba(color_a, color_b) }
369
370
 
370
- context 'with white and black' do
371
+ context "with white and black" do
371
372
  let(:color_a) { @white }
372
373
  let(:color_b) { @black }
373
374
 
374
375
  it { should == Math.sqrt(195_075) } # sqrt(255^2 * 3)
375
376
  end
376
377
 
377
- context 'with black and white' do
378
+ context "with black and white" do
378
379
  let(:color_a) { @black }
379
380
  let(:color_b) { @white }
380
381
 
381
382
  it { should == Math.sqrt(195_075) } # sqrt(255^2 * 3)
382
383
  end
383
384
 
384
- context 'with the same colors' do
385
+ context "with the same colors" do
385
386
  let(:color_a) { @white }
386
387
  let(:color_b) { @white }
387
388
 
@@ -1,15 +1,14 @@
1
- require 'spec_helper'
1
+ require "spec_helper"
2
2
 
3
3
  describe ChunkyPNG::Datastream do
4
-
5
- describe '.from_io'do
4
+ describe ".from_io" do
6
5
  it "should raise an error when loading a file with a bad signature" do
7
- filename = resource_file('damaged_signature.png')
6
+ filename = resource_file("damaged_signature.png")
8
7
  expect { ChunkyPNG::Datastream.from_file(filename) }.to raise_error(ChunkyPNG::SignatureMismatch)
9
8
  end
10
9
 
11
10
  it "should raise an error if the CRC of a chunk is incorrect" do
12
- filename = resource_file('damaged_chunk.png')
11
+ filename = resource_file("damaged_chunk.png")
13
12
  expect { ChunkyPNG::Datastream.from_file(filename) }.to raise_error(ChunkyPNG::CRCMismatch)
14
13
  end
15
14
 
@@ -19,31 +18,37 @@ describe ChunkyPNG::Datastream do
19
18
  end
20
19
 
21
20
  it "should read a stream with trailing data without failing" do
22
- filename = resource_file('trailing_bytes_after_iend_chunk.png')
21
+ filename = resource_file("trailing_bytes_after_iend_chunk.png")
23
22
  image = ChunkyPNG::Datastream.from_file(filename)
24
23
  expect(image).to be_instance_of(ChunkyPNG::Datastream)
25
24
  end
26
25
  end
27
26
 
28
- describe '#metadata' do
27
+ describe "#metadata" do
29
28
  it "should load uncompressed tXTt chunks correctly" do
30
- filename = resource_file('text_chunk.png')
29
+ filename = resource_file("text_chunk.png")
31
30
  ds = ChunkyPNG::Datastream.from_file(filename)
32
- expect(ds.metadata['Title']).to eql 'My amazing icon!'
33
- expect(ds.metadata['Author']).to eql "Willem van Bergen"
31
+ expect(ds.metadata["Title"]).to eql "My amazing icon!"
32
+ expect(ds.metadata["Author"]).to eql "Willem van Bergen"
34
33
  end
35
34
 
36
35
  it "should load compressed zTXt chunks correctly" do
37
- filename = resource_file('ztxt_chunk.png')
36
+ filename = resource_file("ztxt_chunk.png")
37
+ ds = ChunkyPNG::Datastream.from_file(filename)
38
+ expect(ds.metadata["Title"]).to eql "PngSuite"
39
+ expect(ds.metadata["Copyright"]).to eql "Copyright Willem van Schaik, Singapore 1995-96"
40
+ end
41
+
42
+ it "ignores iTXt chunks" do
43
+ filename = resource_file("itxt_chunk.png")
38
44
  ds = ChunkyPNG::Datastream.from_file(filename)
39
- expect(ds.metadata['Title']).to eql 'PngSuite'
40
- expect(ds.metadata['Copyright']).to eql "Copyright Willem van Schaik, Singapore 1995-96"
45
+ expect(ds.metadata).to be_empty
41
46
  end
42
47
  end
43
48
 
44
- describe '#physical_chunk' do
45
- it 'should read and write pHYs chunks correctly' do
46
- filename = resource_file('clock.png')
49
+ describe "#physical_chunk" do
50
+ it "should read and write pHYs chunks correctly" do
51
+ filename = resource_file("clock.png")
47
52
  ds = ChunkyPNG::Datastream.from_file(filename)
48
53
  expect(ds.physical_chunk.unit).to eql :meters
49
54
  expect(ds.physical_chunk.dpix.round).to eql 72
@@ -52,10 +57,84 @@ describe ChunkyPNG::Datastream do
52
57
  expect(ds.physical_chunk).not_to be_nil
53
58
  end
54
59
 
55
- it 'should raise ChunkyPNG::UnitsUnknown if we request dpi but the units are unknown' do
60
+ it "should raise ChunkyPNG::UnitsUnknown if we request dpi but the units are unknown" do
56
61
  physical_chunk = ChunkyPNG::Chunk::Physical.new(2835, 2835, :unknown)
57
- expect{physical_chunk.dpix}.to raise_error(ChunkyPNG::UnitsUnknown)
58
- expect{physical_chunk.dpiy}.to raise_error(ChunkyPNG::UnitsUnknown)
62
+ expect { physical_chunk.dpix }.to raise_error(ChunkyPNG::UnitsUnknown)
63
+ expect { physical_chunk.dpiy }.to raise_error(ChunkyPNG::UnitsUnknown)
64
+ end
65
+ end
66
+
67
+ describe "#iTXt_chunk" do
68
+ it "should read iTXt chunks correctly" do
69
+ filename = resource_file("itxt_chunk.png")
70
+ ds = ChunkyPNG::Datastream.from_file(filename)
71
+ int_text_chunks = ds.chunks.select { |chunk| chunk.is_a?(ChunkyPNG::Chunk::InternationalText) }
72
+ expect(int_text_chunks.length).to eq(2)
73
+
74
+ coach_uk = int_text_chunks.find { |chunk| chunk.language_tag == "en-gb" }
75
+ coach_us = int_text_chunks.find { |chunk| chunk.language_tag == "en-us" }
76
+ expect(coach_uk).to_not be_nil
77
+ expect(coach_us).to_not be_nil
78
+
79
+ expect(coach_uk.keyword).to eq("coach")
80
+ expect(coach_uk.text).to eq("bus with of higher standard of comfort, usually chartered or used for longer journeys")
81
+ expect(coach_uk.translated_keyword).to eq("bus")
82
+ expect(coach_uk.compressed).to eq(ChunkyPNG::UNCOMPRESSED_CONTENT)
83
+ expect(coach_uk.compression).to eq(ChunkyPNG::COMPRESSION_DEFAULT)
84
+
85
+ expect(coach_us.keyword).to eq("coach")
86
+ expect(coach_us.text).to eq("US extracurricular sports teacher at a school (UK: PE teacher) lowest class on a passenger aircraft (UK: economy)")
87
+ expect(coach_us.translated_keyword).to eq("trainer")
88
+ expect(coach_us.compressed).to eq(ChunkyPNG::COMPRESSED_CONTENT)
89
+ expect(coach_us.compression).to eq(ChunkyPNG::COMPRESSION_DEFAULT)
90
+ end
91
+
92
+ it "should write iTXt chunks correctly" do
93
+ expected_hex = %w[0000 001d 6954 5874 436f 6d6d 656e 7400 0000 0000 4372 6561 7465 6420 7769 7468 2047 494d 5064 2e65 07].join("")
94
+ stream = StringIO.new
95
+ itext = ChunkyPNG::Chunk::InternationalText.new("Comment", "Created with GIMP")
96
+ itext.write(stream)
97
+ generated_hex = stream.string.unpack("H*").join("")
98
+
99
+ expect(generated_hex).to eq(expected_hex)
100
+ end
101
+
102
+ it "should handle incorrect UTF-8 encoding in iTXt chunks" do
103
+ incorrect_text_encoding = [0, 0, 0, 14, 105, 84, 88, 116, 67, 111, 109, 109, 101, 110, 116, 0, 0, 0, 0, 0, 195, 40, 17, 87, 97, 213].pack("C*")
104
+ incorrect_translated_keyword_encoding = [0, 0, 0, 19, 105, 84, 88, 116, 67, 111, 109, 109, 101, 110, 116, 0, 0, 0, 0, 226, 130, 40, 0, 116, 101, 115, 116, 228, 53, 113, 182].pack("C*")
105
+
106
+ expect { ChunkyPNG::Chunk.read(StringIO.new(incorrect_text_encoding)) }.to raise_error(ChunkyPNG::InvalidUTF8)
107
+ expect { ChunkyPNG::Chunk.read(StringIO.new(incorrect_translated_keyword_encoding)) }.to raise_error(ChunkyPNG::InvalidUTF8)
108
+ end
109
+
110
+ it "should handle UTF-8 in iTXt compressed chunks correctly" do
111
+ parsed = serialized_chunk(ChunkyPNG::Chunk::InternationalText.new("Comment", "✨", "", "💩", ChunkyPNG::COMPRESSED_CONTENT))
112
+
113
+ expect(parsed.text).to eq("✨")
114
+ expect(parsed.text.encoding).to eq(Encoding::UTF_8)
115
+
116
+ expect(parsed.translated_keyword).to eq("💩")
117
+ expect(parsed.translated_keyword.encoding).to eq(Encoding::UTF_8)
118
+ end
119
+
120
+ it "should handle UTF-8 in iTXt chunks correctly" do
121
+ parsed = serialized_chunk(ChunkyPNG::Chunk::InternationalText.new("Comment", "✨", "", "💩"))
122
+
123
+ expect(parsed.text).to eq("✨")
124
+ expect(parsed.text.encoding).to eq(Encoding::UTF_8)
125
+
126
+ expect(parsed.translated_keyword).to eq("💩")
127
+ expect(parsed.translated_keyword.encoding).to eq(Encoding::UTF_8)
128
+ end
129
+
130
+ it "should transform non UTF-8 iTXt fields to UTF-8 on write" do
131
+ parsed = serialized_chunk(ChunkyPNG::Chunk::InternationalText.new("Comment", "®".encode("Windows-1252"), "", "ƒ".encode("Windows-1252")))
132
+
133
+ expect(parsed.text).to eq("®")
134
+ expect(parsed.text.encoding).to eq(Encoding::UTF_8)
135
+
136
+ expect(parsed.translated_keyword).to eq("ƒ")
137
+ expect(parsed.translated_keyword.encoding).to eq(Encoding::UTF_8)
59
138
  end
60
139
  end
61
140
  end
@@ -1,4 +1,4 @@
1
- require 'spec_helper'
1
+ require "spec_helper"
2
2
 
3
3
  describe ChunkyPNG::Dimension do
4
4
  subject { ChunkyPNG::Dimension.new(2, 3) }
@@ -6,31 +6,31 @@ describe ChunkyPNG::Dimension do
6
6
  it { should respond_to(:width) }
7
7
  it { should respond_to(:height) }
8
8
 
9
- describe '#area' do
9
+ describe "#area" do
10
10
  it "should calculate the area correctly" do
11
11
  expect(subject.area).to eql 6
12
12
  end
13
13
  end
14
14
  end
15
15
 
16
- describe 'ChunkyPNG.Dimension' do
16
+ describe "ChunkyPNG.Dimension" do
17
17
  subject { ChunkyPNG::Dimension.new(1, 2) }
18
18
 
19
19
  it "should create a dimension from a 2-item array" do
20
20
  expect(ChunkyPNG::Dimension([1, 2])).to eql subject
21
- expect(ChunkyPNG::Dimension(['1', '2'])).to eql subject
21
+ expect(ChunkyPNG::Dimension(["1", "2"])).to eql subject
22
22
  end
23
23
 
24
- it "should create a dimension from a hash with x and y keys" do
25
- expect(ChunkyPNG::Dimension(:width => 1, :height => 2)).to eql subject
26
- expect(ChunkyPNG::Dimension('width' => '1', 'height' => '2')).to eql subject
24
+ it "should create a dimension from a hash with width and height keys" do
25
+ expect(ChunkyPNG::Dimension(width: 1, height: 2)).to eql subject
26
+ expect(ChunkyPNG::Dimension("width" => "1", "height" => "2")).to eql subject
27
27
  end
28
28
 
29
29
  it "should create a dimension from a point-like string" do
30
30
  [
31
- ChunkyPNG::Dimension('1,2'),
32
- ChunkyPNG::Dimension('1 2'),
33
- ChunkyPNG::Dimension('(1 , 2)'),
31
+ ChunkyPNG::Dimension("1,2"),
32
+ ChunkyPNG::Dimension("1 2"),
33
+ ChunkyPNG::Dimension("(1 , 2)"),
34
34
  ChunkyPNG::Dimension("{1x2}"),
35
35
  ChunkyPNG::Dimension("[1\t2}"),
36
36
  ].all? { |point| point == subject }