forked from mapnik/python-mapnik
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopy_test.py
More file actions
104 lines (87 loc) · 2.96 KB
/
copy_test.py
File metadata and controls
104 lines (87 loc) · 2.96 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
from nose.tools import eq_
import mapnik
from .utilities import execution_path, run_all
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('.'))
def test_image_16_8_simple():
im = mapnik.Image(2, 2, mapnik.ImageType.gray16)
im.set_pixel(0, 0, 256)
im.set_pixel(0, 1, 999)
im.set_pixel(1, 0, 5)
im.set_pixel(1, 1, 2)
im2 = im.copy(mapnik.ImageType.gray8)
eq_(im2.get_pixel(0, 0), 255)
eq_(im2.get_pixel(0, 1), 255)
eq_(im2.get_pixel(1, 0), 5)
eq_(im2.get_pixel(1, 1), 2)
# Cast back!
im = im2.copy(mapnik.ImageType.gray16)
eq_(im.get_pixel(0, 0), 255)
eq_(im.get_pixel(0, 1), 255)
eq_(im.get_pixel(1, 0), 5)
eq_(im.get_pixel(1, 1), 2)
def test_image_32f_8_simple():
im = mapnik.Image(2, 2, mapnik.ImageType.gray32f)
im.set_pixel(0, 0, 120.1234)
im.set_pixel(0, 1, -23.4)
im.set_pixel(1, 0, 120.6)
im.set_pixel(1, 1, 360.2)
im2 = im.copy(mapnik.ImageType.gray8)
eq_(im2.get_pixel(0, 0), 120)
eq_(im2.get_pixel(0, 1), 0)
eq_(im2.get_pixel(1, 0), 120) # Notice this is truncated!
eq_(im2.get_pixel(1, 1), 255)
def test_image_offset_and_scale():
im = mapnik.Image(2, 2, mapnik.ImageType.gray16)
eq_(im.offset, 0.0)
eq_(im.scaling, 1.0)
im.offset = 1.0
im.scaling = 2.0
eq_(im.offset, 1.0)
eq_(im.scaling, 2.0)
def test_image_16_8_scale_and_offset():
im = mapnik.Image(2, 2, mapnik.ImageType.gray16)
im.set_pixel(0, 0, 256)
im.set_pixel(0, 1, 258)
im.set_pixel(1, 0, 99999)
im.set_pixel(1, 1, 615)
offset = 255
scaling = 3
im2 = im.copy(mapnik.ImageType.gray8, offset, scaling)
eq_(im2.get_pixel(0, 0), 0)
eq_(im2.get_pixel(0, 1), 1)
eq_(im2.get_pixel(1, 0), 255)
eq_(im2.get_pixel(1, 1), 120)
# pixels will be a little off due to offsets in reverting!
im3 = im2.copy(mapnik.ImageType.gray16)
eq_(im3.get_pixel(0, 0), 255) # Rounding error with ints
eq_(im3.get_pixel(0, 1), 258) # same
# The other one was way out of range for our scale/offset
eq_(im3.get_pixel(1, 0), 1020)
eq_(im3.get_pixel(1, 1), 615) # same
def test_image_16_32f_scale_and_offset():
im = mapnik.Image(2, 2, mapnik.ImageType.gray16)
im.set_pixel(0, 0, 256)
im.set_pixel(0, 1, 258)
im.set_pixel(1, 0, 0)
im.set_pixel(1, 1, 615)
offset = 255
scaling = 3.2
im2 = im.copy(mapnik.ImageType.gray32f, offset, scaling)
eq_(im2.get_pixel(0, 0), 0.3125)
eq_(im2.get_pixel(0, 1), 0.9375)
eq_(im2.get_pixel(1, 0), -79.6875)
eq_(im2.get_pixel(1, 1), 112.5)
im3 = im2.copy(mapnik.ImageType.gray16)
eq_(im3.get_pixel(0, 0), 256)
eq_(im3.get_pixel(0, 1), 258)
eq_(im3.get_pixel(1, 0), 0)
eq_(im3.get_pixel(1, 1), 615)
if __name__ == "__main__":
setup()
exit(run_all(eval(x) for x in dir() if x.startswith("test_")))