-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathimage_test.py
More file actions
377 lines (316 loc) · 11.7 KB
/
Copy pathimage_test.py
File metadata and controls
377 lines (316 loc) · 11.7 KB
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import os
import mapnik
import pytest
from .utilities import READ_FLAGS, get_unique_colors, execution_path
@pytest.fixture(scope="module")
def setup():
# All of the paths used are relative, if we run the tests
# from another directory we need to chdir()
os.chdir(execution_path('.'))
yield
def test_type(setup):
im = mapnik.Image(256, 256)
assert im.get_type() == mapnik.ImageType.rgba8
im = mapnik.Image(256, 256, mapnik.ImageType.gray8)
assert im.get_type() == mapnik.ImageType.gray8
def test_image_premultiply():
im = mapnik.Image(256, 256)
assert im.premultiplied() == False
# Premultiply should return true that it worked
assert im.premultiply() == True
assert im.premultiplied() == True
# Premultipling again should return false as nothing should happen
assert im.premultiply() == False
assert im.premultiplied() == True
# Demultiply should return true that it worked
assert im.demultiply() == True
assert im.premultiplied() == False
# Demultiply again should not work and return false as it did nothing
assert im.demultiply() == False
assert im.premultiplied() == False
def test_image_premultiply_values():
im = mapnik.Image(256, 256)
im.fill(mapnik.Color(16, 33, 255, 128))
im.premultiply()
c = im.get_pixel_color(0, 0)
assert c.r == 8
assert c.g == 17
assert c.b == 128
assert c.a == 128
im.demultiply()
# Do to the nature of this operation the result will not be exactly the
# same
c = im.get_pixel_color(0, 0)
assert c.r == 15
assert c.g == 33
assert c.b == 255
assert c.a == 128
def test_apply_opacity():
im = mapnik.Image(4, 4)
im.fill(mapnik.Color(128, 128, 128, 128))
im.apply_opacity(0.75)
c = im.get_pixel_color(0, 0)
assert c.r == 128
assert c.g == 128
assert c.b == 128
assert c.a == 96
def test_background():
im = mapnik.Image(256, 256)
assert im.premultiplied() == False
im.fill(mapnik.Color(32, 64, 125, 128))
assert im.premultiplied() == False
c = im.get_pixel_color(0, 0)
assert c.get_premultiplied() == False
assert c.r == 32
assert c.g == 64
assert c.b == 125
assert c.a == 128
# Now again with a premultiplied alpha
im.fill(mapnik.Color(32, 64, 125, 128, True))
assert im.premultiplied() == True
c = im.get_pixel_color(0, 0)
assert c.get_premultiplied() == True
assert c.r == 32
assert c.g == 64
assert c.b == 125
assert c.a == 128
def test_set_and_get_pixel():
# Create an image that is not premultiplied
im = mapnik.Image(256, 256)
c0 = mapnik.Color(16, 33, 255, 128)
c0_pre = mapnik.Color(16, 33, 255, 128, True)
im.set_pixel(0, 0, c0)
im.set_pixel(1, 1, c0_pre)
# No differences for non premultiplied pixels
c1_int = mapnik.Color(im.get_pixel(0, 0))
assert c0.r == c1_int.r
assert c0.g == c1_int.g
assert c0.b == c1_int.b
assert c0.a == c1_int.a
c1 = im.get_pixel_color(0, 0)
assert c0.r == c1.r
assert c0.g == c1.g
assert c0.b == c1.b
assert c0.a == c1.a
# The premultiplied Color should be demultiplied before being applied.
c0_pre.demultiply()
c1_int = mapnik.Color(im.get_pixel(1, 1))
assert c0_pre.r == c1_int.r
assert c0_pre.g == c1_int.g
assert c0_pre.b == c1_int.b
assert c0_pre.a == c1_int.a
c1 = im.get_pixel_color(1, 1)
assert c0_pre.r == c1.r
assert c0_pre.g == c1.g
assert c0_pre.b == c1.b
assert c0_pre.a == c1.a
# Now create a new image that is premultiplied
im = mapnik.Image(256, 256, mapnik.ImageType.rgba8, True, True)
c0 = mapnik.Color(16, 33, 255, 128)
c0_pre = mapnik.Color(16, 33, 255, 128, True)
im.set_pixel(0, 0, c0)
im.set_pixel(1, 1, c0_pre)
# It should have put pixels that are the same as premultiplied so
# premultiply c0
c0.premultiply()
c1_int = mapnik.Color(im.get_pixel(0, 0))
assert c0.r == c1_int.r
assert c0.g == c1_int.g
assert c0.b == c1_int.b
assert c0.a == c1_int.a
c1 = im.get_pixel_color(0, 0)
assert c0.r == c1.r
assert c0.g == c1.g
assert c0.b == c1.b
assert c0.a == c1.a
# The premultiplied Color should be the same though
c1_int = mapnik.Color(im.get_pixel(1, 1))
assert c0_pre.r == c1_int.r
assert c0_pre.g == c1_int.g
assert c0_pre.b == c1_int.b
assert c0_pre.a == c1_int.a
c1 = im.get_pixel_color(1, 1)
assert c0_pre.r == c1.r
assert c0_pre.g == c1.g
assert c0_pre.b == c1.b
assert c0_pre.a == c1.a
def test_pixel_gray8():
im = mapnik.Image(4, 4, mapnik.ImageType.gray8)
val_list = range(20)
for v in val_list:
im.set_pixel(0, 0, v)
assert im.get_pixel(0, 0) == v
im.set_pixel(0, 0, -v)
assert im.get_pixel(0, 0) == 0
def test_pixel_gray8s():
im = mapnik.Image(4, 4, mapnik.ImageType.gray8s)
val_list = range(20)
for v in val_list:
im.set_pixel(0, 0, v)
assert im.get_pixel(0, 0) == v
im.set_pixel(0, 0, -v)
assert im.get_pixel(0, 0) == -v
def test_pixel_gray16():
im = mapnik.Image(4, 4, mapnik.ImageType.gray16)
val_list = range(20)
for v in val_list:
im.set_pixel(0, 0, v)
assert im.get_pixel(0, 0) == v
im.set_pixel(0, 0, -v)
assert im.get_pixel(0, 0) == 0
def test_pixel_gray16s():
im = mapnik.Image(4, 4, mapnik.ImageType.gray16s)
val_list = range(20)
for v in val_list:
im.set_pixel(0, 0, v)
assert im.get_pixel(0, 0) == v
im.set_pixel(0, 0, -v)
assert im.get_pixel(0, 0) == -v
def test_pixel_gray32():
im = mapnik.Image(4, 4, mapnik.ImageType.gray32)
val_list = range(20)
for v in val_list:
im.set_pixel(0, 0, v)
assert im.get_pixel(0, 0) == v
im.set_pixel(0, 0, -v)
assert im.get_pixel(0, 0) == 0
def test_pixel_gray32s():
im = mapnik.Image(4, 4, mapnik.ImageType.gray32s)
val_list = range(20)
for v in val_list:
im.set_pixel(0, 0, v)
assert im.get_pixel(0, 0) == v
im.set_pixel(0, 0, -v)
assert im.get_pixel(0, 0) == -v
def test_pixel_gray64():
im = mapnik.Image(4, 4, mapnik.ImageType.gray64)
val_list = range(20)
for v in val_list:
im.set_pixel(0, 0, v)
assert im.get_pixel(0, 0) == v
im.set_pixel(0, 0, -v)
assert im.get_pixel(0, 0) == 0
def test_pixel_gray64s():
im = mapnik.Image(4, 4, mapnik.ImageType.gray64s)
val_list = range(20)
for v in val_list:
im.set_pixel(0, 0, v)
assert im.get_pixel(0, 0) == v
im.set_pixel(0, 0, -v)
assert im.get_pixel(0, 0) == -v
def test_pixel_floats():
im = mapnik.Image(4, 4, mapnik.ImageType.gray32f)
val_list = [0.9, 0.99, 0.999, 0.9999, 0.99999, 1, 1.0001, 1.001, 1.01, 1.1]
for v in val_list:
im.set_pixel(0, 0, v)
assert im.get_pixel(0, 0) == pytest.approx(v)
im.set_pixel(0, 0, -v)
assert im.get_pixel(0, 0) == pytest.approx(-v)
def test_pixel_doubles():
im = mapnik.Image(4, 4, mapnik.ImageType.gray64f)
val_list = [0.9, 0.99, 0.999, 0.9999, 0.99999, 1, 1.0001, 1.001, 1.01, 1.1]
for v in val_list:
im.set_pixel(0, 0, v)
assert im.get_pixel(0, 0) == pytest.approx(v)
im.set_pixel(0, 0, -v)
assert im.get_pixel(0, 0) == pytest.approx(-v)
def test_pixel_overflow():
im = mapnik.Image(4, 4, mapnik.ImageType.gray8)
im.set_pixel(0, 0, 256)
assert im.get_pixel(0, 0) == 255
def test_pixel_underflow():
im = mapnik.Image(4, 4, mapnik.ImageType.gray8)
im.set_pixel(0, 0, -1)
assert im.get_pixel(0, 0) == 0
im = mapnik.Image(4, 4, mapnik.ImageType.gray16)
im.set_pixel(0, 0, -1)
assert im.get_pixel(0, 0) == 0
def test_set_pixel_out_of_range_1():
with pytest.raises(IndexError):
im = mapnik.Image(4, 4)
c = mapnik.Color('blue')
im.set_pixel(5, 5, c)
def test_set_pixel_out_of_range_2():
with pytest.raises(IndexError):
im = mapnik.Image(4, 4)
c = mapnik.Color('blue')
im.set_pixel(-1, 1, c)
def test_get_pixel_out_of_range_1():
with pytest.raises(IndexError):
im = mapnik.Image(4, 4)
c = im.get_pixel(5, 5)
def test_get_pixel_out_of_range_2():
with pytest.raises(IndexError):
im = mapnik.Image(4, 4)
c = im.get_pixel(-1, 1)
def test_get_pixel_color_out_of_range_1():
with pytest.raises(IndexError):
im = mapnik.Image(4, 4)
c = im.get_pixel_color(5, 5)
def test_get_pixel_color_out_of_range_2():
with pytest.raises(IndexError):
im = mapnik.Image(4, 4)
c = im.get_pixel_color(-1, 1)
def test_set_color_to_alpha():
im = mapnik.Image(256, 256)
im.fill(mapnik.Color('rgba(12,12,12,255)'))
assert get_unique_colors(im), ['rgba(12,12,12 == 255)']
im.set_color_to_alpha(mapnik.Color('rgba(12,12,12,0)'))
assert get_unique_colors(im), ['rgba(0,0,0 == 0)']
def test_negative_image_dimensions():
with pytest.raises(RuntimeError):
# TODO - this may have regressed in
# https://github.com/mapnik/mapnik/commit/4f3521ac24b61fc8ae8fd344a16dc3a5fdf15af7
im = mapnik.Image(-40, 40)
# should not get here
assert im.width() == 0
assert im.height() == 0
def test_jpeg_round_trip():
filepath = '/tmp/mapnik-jpeg-io.jpeg'
im = mapnik.Image(255, 267)
im.fill(mapnik.Color('rgba(1,2,3,.5)'))
im.save(filepath, 'jpeg')
im2 = mapnik.Image.open(filepath)
with open(filepath, READ_FLAGS) as f:
im3 = mapnik.Image.from_string(f.read())
assert im.width() == im2.width()
assert im.height() == im2.height()
assert im.width() == im3.width()
assert im.height() == im3.height()
assert len(im.to_string()) == len(im2.to_string())
assert len(im.to_string('jpeg')) == len(im2.to_string('jpeg'))
assert len(im.to_string()) == len(im3.to_string())
assert len(im.to_string('jpeg')) == len(im3.to_string('jpeg'))
def test_png_round_trip():
filepath = '/tmp/mapnik-png-io.png'
im = mapnik.Image(255, 267)
im.fill(mapnik.Color('rgba(1,2,3,.5)'))
im.save(filepath, 'png')
im2 = mapnik.Image.open(filepath)
with open(filepath, READ_FLAGS) as f:
im3 = mapnik.Image.from_string(f.read())
assert im.width() == im2.width()
assert im.height() == im2.height()
assert im.width() == im3.width()
assert im.height() == im3.height()
assert len(im.to_string()) == len(im2.to_string())
assert len(im.to_string('png')) == len(im2.to_string('png'))
assert len(im.to_string('png8')) == len(im2.to_string('png8'))
assert len(im.to_string()) == len(im3.to_string())
assert len(im.to_string('png')) == len(im3.to_string('png'))
assert len(im.to_string('png8')) == len(im3.to_string('png8'))
def test_image_open_from_string():
filepath = '../data/images/dummy.png'
im1 = mapnik.Image.open(filepath)
with open(filepath, READ_FLAGS) as f:
im2 = mapnik.Image.from_string(f.read())
assert im1.width() == im2.width()
length = len(im1.to_string())
assert length == len(im2.to_string())
assert len(mapnik.Image.from_string(im1.to_string('png')).to_string()) == length
assert len(mapnik.Image.from_string(im1.to_string('jpeg')).to_string()) == length
assert len(mapnik.Image.from_memoryview(memoryview(im1.to_string('png'))).to_string()) == length
assert len(mapnik.Image.from_memoryview(memoryview(im1.to_string('jpeg'))).to_string()) == length
# TODO - https://github.com/mapnik/mapnik/issues/1831
assert len(mapnik.Image.from_string(im1.to_string('tiff')).to_string()) == length
assert len(mapnik.Image.from_memoryview(memoryview(im1.to_string('tiff'))).to_string()) == length