Debian Codegraph @

src/pycolmap/sensor/bitmap_test.py

raw
1 import numpy as np
2
3 import pycolmap
4
5
6 def test_bitmap_rescale_filter_enum():
7 assert {
8 k: int(v) for k, v in pycolmap.BitmapRescaleFilter.__members__.items()
9 } == {
10 "BILINEAR": 0,
11 "BOX": 1,
12 }
13
14
15 def test_bitmap_default_init():
16 bitmap = pycolmap.Bitmap()
17 assert bitmap is not None
18 assert bitmap.is_empty
19
20
21 def test_bitmap_init_width_height_rgb():
22 bitmap = pycolmap.Bitmap(width=64, height=48, as_rgb=True)
23 assert bitmap.width == 64
24 assert bitmap.height == 48
25 assert bitmap.is_rgb
26
27
28 def test_bitmap_init_width_height_grey():
29 bitmap = pycolmap.Bitmap(width=64, height=48, as_rgb=False)
30 assert bitmap.width == 64
31 assert bitmap.height == 48
32 assert bitmap.is_grey
33
34
35 def test_bitmap_init_with_linear_colorspace():
36 bitmap = pycolmap.Bitmap(
37 width=64, height=48, as_rgb=True, linear_colorspace=True
38 )
39 assert bitmap.width == 64
40 assert bitmap.height == 48
41
42
43 def test_bitmap_readonly_props():
44 bitmap = pycolmap.Bitmap(width=32, height=24, as_rgb=True)
45 assert isinstance(bitmap.width, int)
46 assert isinstance(bitmap.height, int)
47 assert isinstance(bitmap.channels, int)
48 assert isinstance(bitmap.is_rgb, bool)
49 assert isinstance(bitmap.is_grey, bool)
50 assert isinstance(bitmap.is_empty, bool)
51 assert isinstance(bitmap.bits_per_pixel, int)
52 assert isinstance(bitmap.pitch, int)
53 assert bitmap.channels == 3
54 assert bitmap.bits_per_pixel == 24
55 assert not bitmap.is_empty
56
57
58 def test_bitmap_readonly_props_grey():
59 bitmap = pycolmap.Bitmap(width=32, height=24, as_rgb=False)
60 assert bitmap.channels == 1
61 assert bitmap.bits_per_pixel == 8
62 assert bitmap.is_grey
63 assert not bitmap.is_rgb
64
65
66 def test_bitmap_from_array_grey():
67 array = np.zeros((48, 64), dtype=np.uint8)
68 bitmap = pycolmap.Bitmap.from_array(array)
69 assert bitmap.width == 64
70 assert bitmap.height == 48
71 assert bitmap.is_grey
72
73
74 def test_bitmap_from_array_rgb():
75 array = np.zeros((48, 64, 3), dtype=np.uint8)
76 bitmap = pycolmap.Bitmap.from_array(array)
77 assert bitmap.width == 64
78 assert bitmap.height == 48
79 assert bitmap.is_rgb
80
81
82 def test_bitmap_to_array_roundtrip_grey():
83 array_in = np.random.randint(0, 256, (48, 64), dtype=np.uint8)
84 bitmap = pycolmap.Bitmap.from_array(array_in)
85 array_out = bitmap.to_array()
86 np.testing.assert_array_equal(array_in, array_out)
87
88
89 def test_bitmap_to_array_roundtrip_rgb():
90 array_in = np.random.randint(0, 256, (48, 64, 3), dtype=np.uint8)
91 bitmap = pycolmap.Bitmap.from_array(array_in)
92 array_out = bitmap.to_array()
93 np.testing.assert_array_equal(array_in, array_out)
94
95
96 def test_bitmap_from_array_linear_colorspace():
97 array = np.zeros((48, 64, 3), dtype=np.uint8)
98 bitmap = pycolmap.Bitmap.from_array(array, linear_colorspace=True)
99 assert bitmap.is_rgb
100
101
102 def test_bitmap_clone():
103 bitmap = pycolmap.Bitmap(width=32, height=24, as_rgb=True)
104 cloned = bitmap.clone()
105 assert cloned.width == bitmap.width
106 assert cloned.height == bitmap.height
107 assert cloned.is_rgb == bitmap.is_rgb
108
109
110 def test_bitmap_clone_as_grey():
111 bitmap = pycolmap.Bitmap(width=32, height=24, as_rgb=True)
112 grey = bitmap.clone_as_grey()
113 assert grey.is_grey
114 assert grey.width == bitmap.width
115 assert grey.height == bitmap.height
116
117
118 def test_bitmap_clone_as_rgb():
119 bitmap = pycolmap.Bitmap(width=32, height=24, as_rgb=False)
120 rgb = bitmap.clone_as_rgb()
121 assert rgb.is_rgb
122 assert rgb.width == bitmap.width
123 assert rgb.height == bitmap.height
124
125
126 def test_bitmap_rescale_default_filter():
127 bitmap = pycolmap.Bitmap(width=64, height=48, as_rgb=True)
128 bitmap.rescale(32, 24)
129 assert bitmap.width == 32
130 assert bitmap.height == 24
131
132
133 def test_bitmap_rescale_explicit_filter():
134 bitmap = pycolmap.Bitmap(width=64, height=48, as_rgb=True)
135 bitmap.rescale(32, 24, filter=pycolmap.BitmapRescaleFilter.BOX)
136 assert bitmap.width == 32
137 assert bitmap.height == 24
138
139
140 def test_bitmap_rot90_once():
141 bitmap = pycolmap.Bitmap(width=64, height=48, as_rgb=True)
142 bitmap.rot90(1)
143 assert bitmap.width == 48
144 assert bitmap.height == 64
145
146
147 def test_bitmap_rot90_twice():
148 bitmap = pycolmap.Bitmap(width=64, height=48, as_rgb=True)
149 bitmap.rot90(2)
150 assert bitmap.width == 64
151 assert bitmap.height == 48
152
153
154 def test_bitmap_write_read(tmp_path):
155 array = np.random.randint(0, 256, (48, 64, 3), dtype=np.uint8)
156 bitmap = pycolmap.Bitmap.from_array(array)
157 filepath = str(tmp_path / "test.png")
158 bitmap.write(filepath)
159 loaded = pycolmap.Bitmap.read(filepath, as_rgb=True)
160 assert loaded is not None
161 assert loaded.width == 64
162 assert loaded.height == 48
163 np.testing.assert_array_equal(loaded.to_array(), array)
164
165
166 def test_bitmap_set_jpeg_quality():
167 bitmap = pycolmap.Bitmap(width=32, height=24, as_rgb=True)
168 bitmap.set_jpeg_quality(85)
169
170
171 def test_bitmap_exif_orientation():
172 bitmap = pycolmap.Bitmap(width=32, height=24, as_rgb=True)
173 result = bitmap.exif_orientation()
174 # Synthetic bitmap has no EXIF data, so expect None.
175 assert result is None
176
177
178 def test_bitmap_exif_camera_model():
179 bitmap = pycolmap.Bitmap(width=32, height=24, as_rgb=True)
180 result = bitmap.exif_camera_model()
181 assert result is None
182
183
184 def test_bitmap_exif_focal_length():
185 bitmap = pycolmap.Bitmap(width=32, height=24, as_rgb=True)
186 result = bitmap.exif_focal_length()
187 assert result is None
188
189
190 def test_bitmap_exif_latitude():
191 bitmap = pycolmap.Bitmap(width=32, height=24, as_rgb=True)
192 result = bitmap.exif_latitude()
193 assert result is None
194
195
196 def test_bitmap_exif_longitude():
197 bitmap = pycolmap.Bitmap(width=32, height=24, as_rgb=True)
198 result = bitmap.exif_longitude()
199 assert result is None
200
201
202 def test_bitmap_exif_altitude():
203 bitmap = pycolmap.Bitmap(width=32, height=24, as_rgb=True)
204 result = bitmap.exif_altitude()
205 assert result is None
206
207
208 def test_bitmap_thumbnail():
209 bitmap = pycolmap.Bitmap(width=200, height=100, as_rgb=True)
210 scale = bitmap.thumbnail(max_image_size=50)
211 # The longest side is scaled to fit max_image_size: 50 / max(200, 100).
212 assert scale == 0.25
213 assert bitmap.width == 50
214 assert bitmap.height == 25
215
216
217 def test_bitmap_thumbnail_noop():
218 bitmap = pycolmap.Bitmap(width=40, height=30, as_rgb=True)
219 scale = bitmap.thumbnail(max_image_size=100)
220 assert scale == 1.0
221 assert bitmap.width == 40
222 assert bitmap.height == 30