-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrszr_spec.rb
333 lines (257 loc) · 9.96 KB
/
rszr_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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
RSpec.describe 'Rszr' do
it 'has a version number' do
expect(Rszr::VERSION).to_not be_nil
end
it 'instantiates new images' do
expect(Rszr::Image.new(300, 400)).to be_kind_of(Rszr::Image)
end
context 'Loading' do
it 'loads images from disk' do
expect(Rszr::Image.load(RSpec.root.join('images/bacon.png'))).to be_kind_of(Rszr::Image)
end
it 'loads image from memory' do
image = Rszr::Image.load_data(RSpec.root.join('images/test.jpg').binread)
expect(image.format).to eq('jpeg')
expect(image[1,1]).to have_attributes(green: 93, blue: 112, red: 78)
end
it 'loads images with uppercase extension' do
expect(Rszr::Image.load(RSpec.root.join('images/CHUNKY.PNG'))).to be_kind_of(Rszr::Image)
end
it 'raises an error when trying to load a non-existing image' do
expect { Rszr::Image.load(RSpec.root.join('images/foo.jpg')) }.to raise_error(Rszr::FileNotFound)
end
it 'raises an error when trying to load a non-supported image' do
expect { Rszr::Image.load(RSpec.root.join('images/broken.jpg')) }.to raise_error(Rszr::LoadError)
end
end
context 'Images' do
before(:each) do
# 1500 997
@image = Rszr::Image.load(RSpec.root.join('images/test.jpg'))
end
it 'provide the image format as lowercase' do
expect(Rszr::Image.load(RSpec.root.join('images/CHUNKY.PNG')).format).to eq('png')
end
it 'always return jpeg for JPEG format' do
expect(Rszr::Image.load(RSpec.root.join('images/test.jpeg')).format).to eq('jpeg')
expect(Rszr::Image.load(RSpec.root.join('images/test.jpg')).format).to eq('jpeg')
end
it 'provide width and height' do
expect(@image.width).to eq(1500)
expect(@image.height).to eq(997)
end
it 'provide pixel RGBA value' do
expect(@image[0, 0].to_hex).to eq('#4c5c6cff')
end
it 'provide pixel RGB value' do
expect(@image[0, 0].to_hex(alpha: false)).to eq('#4c5c6c')
end
it 'return nil if pixel out of bounds' do
expect(@image[1000, 1000]).to be_nil
end
end
context 'Resizing' do
before(:each) do
# 1500 997
@image = Rszr::Image.load(RSpec.root.join('images/test.jpg'))
end
it 'creates a new instance' do
expect(@image.resize(0.5)).not_to be(@image)
end
it 'modifies instance in place' do
expect(@image.resize!(0.5)).to be(@image)
end
it 'by scale' do
expect(@image.resize(0.5).dimensions).to eq([750, 499])
expect(@image.resize(Rational(1, 3)).dimensions).to eq([500, 332])
expect(@image.resize(0.75).dimensions).to eq([1125, 748])
end
it 'by max width' do
expect(@image.resize(500, :auto).dimensions).to eq([500, 332])
end
it 'by max height' do
expect(@image.resize(:auto, 100).dimensions).to eq([150, 100])
end
it 'by narrower bounding box' do
expect(@image.resize(100, 400).dimensions).to eq([100, 66])
end
it 'by wider bounding box' do
expect(@image.resize(400, 100).dimensions).to eq([150, 100])
end
it 'skew to dimensions' do
expect(@image.resize(100, 100, skew: true).dimensions).to eq([100, 100])
end
it 'raises on scale larger than one' do
expect { @image.resize(-0.5) }.to raise_error(ArgumentError, 'scale factor -0.5 out of range')
end
it 'raises on too many arguments' do
expect { @image.resize(20, 30, 40) }.to raise_error(ArgumentError, 'wrong number of arguments (3 for 1..2)')
end
it 'raises on too few arguments' do
expect { @image.resize }.to raise_error(ArgumentError, 'wrong number of arguments (0 for 1..2)')
end
it 'raises on nonsense arguments' do
expect { @image.resize('foo', 'bar') }.to raise_error(ArgumentError)
end
end
context 'Cropping' do
before(:each) do
@image = Rszr::Image.load(RSpec.root.join('images/test.jpg')).resize(0.1)
end
it 'crops images' do
expect(@image.crop(40, 50, 60, 70).dimensions).to eq([60, 70])
end
it 'crops images in place' do
expect(@image.crop!(40, 50, 60, 70)).to be(@image)
end
end
context 'Turning' do
before(:each) do
@image = Rszr::Image.load(RSpec.root.join('images/test.jpg'))
end
it 'turns images clockwise' do
expect(@image.turn!(3).dimensions).to eq([997, 1500])
end
it 'turns images counterclockwise' do
expect(@image.turn!(-3).dimensions).to eq([997, 1500])
end
it 'turns images in place' do
expect(@image.turn!(3)).to be(@image)
end
end
context 'Duplicating' do
before(:each) do
@image = Rszr::Image.load(RSpec.root.join('images/test.jpg'))
end
it 'duplicates images' do
expect(@image.dup).not_to be(@image)
expect(@image.dup.dimensions).to eq(@image.dimensions)
end
end
context 'Saving' do
before(:each) do
@image = Rszr::Image.load(RSpec.root.join('images/test.jpg')).resize(0.1)
end
it 'saves images' do
Dir.mktmpdir do |dir|
resized_file = Pathname.new(File.join(dir, 'resized.jpg'))
resized_file.unlink if resized_file.exist?
expect(resized_file.exist?).to be(false)
expect(@image.save(resized_file.to_s)).to be(true)
expect(resized_file.exist?).to be(true)
end
end
it 'saves images when given path does not have extension' do
Dir.mktmpdir do |dir|
resized_file = Pathname.new(File.join(dir, 'resized_jpg'))
resized_file.unlink if resized_file.exist?
expect(resized_file.exist?).to be(false)
expect(@image.save(resized_file.to_s)).to be(true)
expect(resized_file.exist?).to be(true)
end
end
it 'saves with given format when saving without extension' do
Dir.mktmpdir do |dir|
resized_file = Pathname.new(File.join(dir, 'resized'))
resized_file.unlink if resized_file.exist?
expect(resized_file.exist?).to be(false)
expect(@image.save(resized_file.to_s, format: :png)).to be(true)
expect(resized_file).to have_format('png')
end
end
it 'saves using specified format, overriding extension' do
Dir.mktmpdir do |dir|
resized_file = Pathname.new(File.join(dir, 'resized.jpg'))
resized_file.unlink if resized_file.exist?
expect(resized_file.exist?).to be(false)
expect(@image.save(resized_file.to_s, format: :png)).to be(true)
expect(resized_file).to have_format('png')
end
end
it 'raises save errors' do
Dir.mktmpdir do |dir|
resized_file = Pathname.new(File.join(dir, 'foo', 'bar', 'resized.jpg'))
expect(resized_file.exist?).to be(false)
expect { @image.save(resized_file.to_s) }.to raise_error(Rszr::SaveError, 'Non-existant path component')
end
end
it 'accepts uppercase extensions' do
Dir.mktmpdir do |dir|
%w[JPG JPEG PNG].each do |format|
resized_file = Pathname.new(File.join(dir, "resized.#{format}"))
expect(@image.save(resized_file.to_s)).to be(true)
expect(resized_file.exist?).to be(true)
end
end
end
it 'saves image to memory' do
expect(Rszr::Image.load(RSpec.root.join('images/test.jpg')).save_data(format: 'png')).to start_with("\x89PNG".force_encoding('BINARY'))
end
it 'saves interlaced PNGs but clears interlacing flag' do
Dir.mktmpdir do |dir|
resized_file = Pathname.new(File.join(dir, 'interlace.png'))
resized_file.unlink if resized_file.exist?
@image.save(resized_file.to_s, interlace: true)
expect(`file #{resized_file}`).to include('interlaced')
resized_file.unlink
@image.save(resized_file.to_s, interlace: false)
expect(`file #{resized_file}`).to include('non-interlaced')
end
end
end
context 'Autorotation' do
%w[jpg tiff].each do |format|
it "autorotates #{format.upcase} images" do
1.upto(8) do |orientation|
expect(Rszr::Image.load(RSpec.root.join('images', 'orientation', "#{orientation}.#{format}"), autorotate: true).original_orientation).to eq(orientation)
end
end
it "ignores #{format.upcase} images without EXIF orientation" do
expect(Rszr::Image.load(RSpec.root.join('images', 'orientation', "none.#{format}"), autorotate: true).original_orientation).to be_nil
end
it "ignores #{format.upcase} images with invalid EXIF orientation" do
expect(Rszr::Image.load(RSpec.root.join('images', 'orientation', "invalid.#{format}"), autorotate: true).original_orientation).to be_nil
end
end
end
context 'Transformations' do
before(:each) do
@image = Rszr::Image.load(RSpec.root.join('images/test.jpg'))
end
it 'flips' do
expect(@image.flip.dimensions).to eq(@image.dimensions)
end
it 'flops' do
expect(@image.flop.dimensions).to eq(@image.dimensions)
end
it 'sharpens' do
expect(@image.sharpen(2).dimensions).to eq(@image.dimensions)
end
it 'blurs' do
expect(@image.blur(3).dimensions).to eq(@image.dimensions)
end
it 'brightens' do
expect(@image.brighten(0.1).dimensions).to eq(@image.dimensions)
end
it 'darkens' do
expect(@image.brighten(-0.1).dimensions).to eq(@image.dimensions)
end
it 'contrasts' do
expect(@image.contrast(0.1).dimensions).to eq(@image.dimensions)
end
it 'gammas' do
expect(@image.gamma(1.1).dimensions).to eq(@image.dimensions)
end
it 'filters' do
expect(@image.filter('bump_map( map=tint(red=50,tint=200), blue=10 );').dimensions).to eq(@image.dimensions)
end
%i[dynamic luminosity lightness average].each do |mode|
it "desaturates in mode #{mode}" do
@image.desaturate!(mode)
pixel = @image[500, 500]
expect(pixel.red).to eq(pixel.green)
expect(pixel.blue).to eq(pixel.red)
end
end
end
end