forked from elixir-image/image
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_test.exs
335 lines (246 loc) · 10.4 KB
/
image_test.exs
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
334
335
defmodule Image.Test do
use ExUnit.Case, async: false
import Image.TestSupport
alias Vix.Vips.Operation
alias Vix.Vips.Image, as: Vimage
doctest Image
doctest Image.Classification
doctest Image.Color
doctest Image.BandFormat
doctest Image.Blurhash
setup do
Temp.track!()
dir = Temp.mkdir!()
{:ok, %{dir: dir}}
end
test "Minimise metadata", %{dir: dir} do
image = image_path("Kip_small.jpg")
{:ok, input_info} = File.stat(image)
{:ok, kip} = Vimage.new_from_file(image)
{:ok, minimised} = Image.minimize_metadata(kip)
out_path = Temp.path!(suffix: ".jpg", basedir: dir)
validate_path = validate_path("Kip_small_with_simple_exif.jpg")
assert :ok = Vimage.write_to_file(minimised, out_path)
{:ok, output_info} = File.stat(validate_path)
assert_images_equal(out_path, validate_path("Kip_small_with_simple_exif.jpg"))
assert input_info.size > output_info.size * 2
end
test "Circular Image", %{dir: dir} do
image = image_path("Kip_small.jpg")
{:ok, kip} = Vimage.new_from_file(image)
{:ok, circle} = Image.circle(kip)
out_path = Temp.path!(suffix: ".png", basedir: dir)
assert :ok = Vimage.write_to_file(circle, out_path)
assert_images_equal(out_path, validate_path("Kip_small_circle_mask.png"))
end
test "Rounded Image", %{dir: dir} do
image = image_path("Kip_small.jpg")
{:ok, kip} = Vimage.new_from_file(image)
{:ok, rounded} = Image.rounded(kip)
out_path = Temp.path!(suffix: ".png", basedir: dir)
assert :ok = Vimage.write_to_file(rounded, out_path)
assert_images_equal(out_path, validate_path("Kip_small_rounded_mask.png"))
end
test "Squircled Image", %{dir: dir} do
image = image_path("Kip_small.jpg")
{:ok, kip} = Vimage.new_from_file(image)
{:ok, squircled} = Image.squircle(kip)
out_path = Temp.path!(suffix: ".png", basedir: dir)
assert :ok = Vimage.write_to_file(squircled, out_path)
assert_images_equal(out_path, validate_path("Kip_small_squircle_mask.png"))
end
test "Image composition", %{dir: dir} do
image = image_path("lamborghini-forsennato-concept.jpg")
{:ok, lambo} = Vimage.new_from_file(image)
{:ok, grad} = Image.linear_gradient(lambo)
{:ok, composite} = Operation.composite2(lambo, grad, :VIPS_BLEND_MODE_OVER)
out_path = Temp.path!(suffix: ".png", basedir: dir)
assert :ok = Vimage.write_to_file(composite, out_path)
assert_images_equal(out_path, validate_path("composite_with_gradient.png"))
end
test "Circular Image Gradient 1", %{dir: dir} do
use Image.Math
validate_path = validate_path("radial_gradient_1.png")
start = [100, 50, 0]
finish = [50, 0, 50]
size = 512
{:ok, x} = Operation.xyz(size, size)
{:ok, x} = subtract(x, [Image.width(x) / 2, Image.height(x) / 2])
{:ok, x0} = Operation.extract_band(x, 0)
{:ok, x1} = Operation.extract_band(x, 1)
d =
pow!(x0, 2)
|> add!(pow!(x1, 2))
|> pow!(0.5)
|> divide!(2 ** 0.5 * size / 2)
out =
d
|> multiply!(finish)
|> add!(multiply!(d, -1) |> add!(1) |> multiply!(start))
{:ok, out} = Operation.copy(out, interpretation: :VIPS_INTERPRETATION_LAB)
out_path = Temp.path!(suffix: ".png", basedir: dir)
assert :ok = Vimage.write_to_file(out, out_path)
# Image.write!(out, validate_path)
assert_images_equal(out_path, validate_path, 2.0)
end
test "Circular Image Gradient 2", %{dir: dir} do
use Image.Math
start = [255, 0, 0]
finish = [0, 0, 255]
{:ok, x} = Operation.xyz(100, 200)
{:ok, x} = subtract(x, [Image.width(x) / 2, Image.height(x) / 2])
{:ok, x0} = Operation.extract_band(x, 0)
{:ok, x1} = Operation.extract_band(x, 1)
d =
x0
|> pow!(2)
|> add!(pow!(x1, 2))
|> pow!(0.5)
d =
d
|> multiply!(10)
|> cos!()
|> divide!(2)
|> add!(0.5)
out =
d
|> multiply!(finish)
|> add!(multiply!(d, -1) |> add!(1) |> multiply!(start))
{:ok, out} = Operation.copy(out, interpretation: :VIPS_INTERPRETATION_sRGB)
out_path = Temp.path!(suffix: ".png", basedir: dir)
assert :ok = Vimage.write_to_file(out, out_path)
assert_images_equal(out_path, validate_path("radial_gradient_2.png"))
end
# This test validates the expected operation of infix operators
# working on Image data. This behaviour is driven by `use Image.Math`.
test "Math operators to create a white circle", %{dir: dir} do
use Image.Math
{:ok, im} = Operation.xyz(200, 200)
# move the origin to the centre
im = im - [Image.width(im) / 2, Image.height(im) / 2]
# a one-band image where pixels are distance from the centre
im = (im[0] ** 2 + im[1] ** 2) ** 0.5
# relational operations make uchar images with 0 for false, 255 for true
im = im < Image.width(im) / 3
out_path = Temp.path!(suffix: ".png", basedir: dir)
assert :ok = Vimage.write_to_file(im, out_path)
assert_images_equal(out_path, validate_path("image_circle_white.png"))
end
test "Rotating an image 90 degrees", %{dir: dir} do
image = image_path("Kip_small.jpg")
{:ok, kip} = Vimage.new_from_file(image)
{:ok, rotated} = Image.rotate(kip, 90.0)
out_path = Temp.path!(suffix: ".jpg", basedir: dir)
assert :ok = Vimage.write_to_file(rotated, out_path)
assert_images_equal(out_path, validate_path("Kip_small_rotate90.jpg"))
end
test "Rotating an image -90 degrees", %{dir: dir} do
image = image_path("Kip_small.jpg")
{:ok, kip} = Vimage.new_from_file(image)
{:ok, rotated} = Image.rotate(kip, -90.0)
out_path = Temp.path!(suffix: ".jpg", basedir: dir)
assert :ok = Vimage.write_to_file(rotated, out_path)
assert_images_equal(out_path, validate_path("Kip_small_rotate-90.jpg"))
end
test "Rotating an image 45 degrees", %{dir: dir} do
image = image_path("Kip_small.jpg")
{:ok, kip} = Vimage.new_from_file(image)
{:ok, rotated} = Image.rotate(kip, 45)
out_path = Temp.path!(suffix: ".jpg", basedir: dir)
assert :ok = Vimage.write_to_file(rotated, out_path)
assert_images_equal(out_path, validate_path("Kip_small_rotate45.jpg"))
end
test "Rotating an image 45 degrees with white background", %{dir: dir} do
image = image_path("Kip_small.jpg")
{:ok, kip} = Vimage.new_from_file(image)
{:ok, rotated} = Image.rotate(kip, 45, background: [255, 255, 255])
out_path = Temp.path!(suffix: ".jpg", basedir: dir)
assert :ok = Vimage.write_to_file(rotated, out_path)
assert_images_equal(out_path, validate_path("Kip_small_rotate45_white.jpg"))
end
test "Rotating an image 45 degrees with displacement", %{dir: dir} do
image = image_path("Kip_small.jpg")
{:ok, kip} = Vimage.new_from_file(image)
{:ok, rotated} = Image.rotate(kip, 45, odx: 10, ody: 10, background: [255, 255, 255])
out_path = Temp.path!(suffix: ".jpg", basedir: dir)
assert :ok = Vimage.write_to_file(rotated, out_path)
assert_images_equal(out_path, validate_path("Kip_small_rotate45_displaced.jpg"))
end
test "Convert to polar coordinates", %{dir: dir} do
image = image_path("San-Francisco-2018-04-2549.jpg")
{:ok, image} = Vimage.new_from_file(image)
{:ok, polar} = Image.to_polar_coordinates(image)
out_path = Temp.path!(suffix: ".jpg", basedir: dir)
assert :ok = Vimage.write_to_file(polar, out_path)
assert_images_equal(out_path, validate_path("polar.jpg"))
end
test "Convert to rectangular coordinates", %{dir: dir} do
image = validate_path("polar.jpg")
{:ok, image} = Vimage.new_from_file(image)
{:ok, rectangular} = Image.to_rectangular_coordinates(image)
out_path = Temp.path!(suffix: ".jpg", basedir: dir)
assert :ok = Vimage.write_to_file(rectangular, out_path)
assert_images_equal(out_path, validate_path("rectangular.jpg"))
end
test "Ripple Effect", %{dir: dir} do
image = image_path("San-Francisco-2018-04-2549.jpg")
{:ok, image} = Vimage.new_from_file(image)
{:ok, ripple} = Image.ripple(image)
out_path = Temp.path!(suffix: ".jpg", basedir: dir)
assert :ok = Vimage.write_to_file(ripple, out_path)
assert_images_equal(out_path, validate_path("ripple.jpg"))
end
test "Autorotate an image", %{dir: dir} do
image = image_path("Kip_small_rotated.jpg")
{:ok, image} = Vimage.new_from_file(image)
{:ok, {autorotated, flags}} = Image.autorotate(image)
out_path = Temp.path!(suffix: ".jpg", basedir: dir)
assert :ok = Vimage.write_to_file(autorotated, out_path)
assert {:ok, 180} = Map.fetch(flags, :angle)
assert_images_equal(out_path, validate_path("autorotated.jpg"))
end
test "Image.new/3" do
# 100x100 pixel image of dark blue slate color
assert {:ok, _image} = Image.new(100, 100, color: :dark_slate_blue)
# 100x100 pixel green image, fully transparent
assert {:ok, _image} = Image.new(100, 100, color: [0, 255, 0, 1], bands: 4)
end
test "Image.open/2 for different file formats" do
jpeg_binary = File.read!(image_path("Kip_small.jpg"))
assert {:ok, _i} = Image.open(jpeg_binary)
png_binary = File.read!(image_path("Kip_small.png"))
assert {:ok, _i} = Image.open(png_binary)
webp_binary = File.read!(image_path("example.webp"))
assert {:ok, _i} = Image.open(webp_binary)
end
test "Splitting an image into its bands" do
image = Image.open!(image_path("/2x2-maze.png"))
assert [_, _, _, _] = Image.split_bands(image)
end
test "Split alpha band" do
image = Image.open!(image_path("2x2-maze.png"))
{bands, alpha} = Image.split_alpha(image)
assert Image.bands(bands) == 3
assert Image.bands(alpha) == 1
end
test "Image/new 3 default bands" do
assert Image.bands(Image.new!(20, 20, color: [0, 0, 0, 0])) == 4
assert Image.bands(Image.new!(20, 20, color: [0, 0, 0])) == 3
end
test "Image color replacement" do
validate_path = validate_path("green.tiff")
{:ok, red} = Image.new(20, 20, color: :red)
{:ok, replaced} = Image.replace_color(red, color: :red, replace_with: :green)
assert_images_equal(replaced, validate_path)
end
test "Joining bands results in the same image" do
image = Image.open!("./test/support/images/Singapore-2016-09-5887.jpg")
bands = Image.split_bands(image)
joined = Image.join_bands!(bands)
assert {:ok, +0.0, _} = Image.compare(image, joined)
end
test "Opening an HEIC binary" do
heic = File.read!("./test/support/images/sample1.heic")
assert {:ok, _image} = Image.open(heic)
end
end