-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_bmp_file_reader.py
209 lines (154 loc) · 6.87 KB
/
test_bmp_file_reader.py
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
import unittest
import bmp_file_reader as bmpr
class BMPFileReaderTest(unittest.TestCase):
def test_read_bmp_file_header(self):
image_path = "images/single_white_pixel.bmp"
with open(image_path, "rb") as file_handle:
reader = bmpr.BMPFileReader(file_handle)
actual = reader.read_bmp_file_header()
expected = bmpr.BMPHeader(bmpr.BMPType.BM, 126, b"\x00\x00", b"\x00\x00", 122)
self.assertEqual(expected, actual)
def test_read_dib_header(self):
image_path = "images/single_white_pixel.bmp"
with open(image_path, "rb") as file_handle:
reader = bmpr.BMPFileReader(file_handle)
actual = reader.read_dib_header()
expected = bmpr.DIBHeader(
width=1,
height=1,
num_color_planes=1,
bits_per_pixel=24,
compression_type=bmpr.CompressionType.BI_RGB,
raw_bitmap_size=4,
horizontal_resolution_ppm=11811,
vertical_resolution_ppm=11811,
num_colors_in_palette=0,
num_important_colors_used=0,
)
self.assertEqual(expected, actual)
def test_get_width_and_height(self):
image_path = "images/single_white_pixel.bmp"
with open(image_path, "rb") as file_handle:
reader = bmpr.BMPFileReader(file_handle)
actual_width = reader.get_width()
actual_height = reader.get_height()
expected_width = 1
expected_height = 1
self.assertEqual(expected_width, actual_width)
self.assertEqual(expected_height, actual_height)
def test_get_width_and_height_2(self):
image_path = "images/single_green_pixel.bmp"
with open(image_path, "rb") as file_handle:
reader = bmpr.BMPFileReader(file_handle)
actual_width = reader.get_width()
actual_height = reader.get_height()
expected_width = 1
expected_height = 1
self.assertEqual(expected_width, actual_width)
self.assertEqual(expected_height, actual_height)
def test_get_width_and_height_3(self):
image_path = "images/small_image_with_colors.bmp"
with open(image_path, "rb") as file_handle:
reader = bmpr.BMPFileReader(file_handle)
actual_width = reader.get_width()
actual_height = reader.get_height()
expected_width = 30
expected_height = 20
self.assertEqual(expected_width, actual_width)
self.assertEqual(expected_height, actual_height)
def test_get_row(self):
image_path = "images/single_white_pixel.bmp"
with open(image_path, "rb") as file_handle:
reader = bmpr.BMPFileReader(file_handle)
actual = reader.get_row(0)
expected = [bmpr.Color(255, 255, 255)]
self.assertEqual(expected, actual)
def test_get_row_2(self):
image_path = "images/single_green_pixel.bmp"
with open(image_path, "rb") as file_handle:
reader = bmpr.BMPFileReader(file_handle)
actual = reader.get_row(0)
expected = [bmpr.Color(red=0, green=255, blue=0)]
self.assertEqual(expected, actual)
def test_get_row_3(self):
image_path = "images/small_image_with_colors.bmp"
with open(image_path, "rb") as file_handle:
reader = bmpr.BMPFileReader(file_handle)
actual = reader.get_row(0)
expected = [
bmpr.Color(red=255, green=255, blue=255),
bmpr.Color(red=255, green=255, blue=255),
bmpr.Color(red=255, green=255, blue=255),
bmpr.Color(red=255, green=255, blue=255),
bmpr.Color(red=255, green=255, blue=255),
bmpr.Color(red=255, green=255, blue=255),
bmpr.Color(red=255, green=255, blue=255),
bmpr.Color(red=255, green=255, blue=255),
bmpr.Color(red=255, green=255, blue=255),
bmpr.Color(red=255, green=255, blue=255),
bmpr.Color(red=255, green=255, blue=255),
bmpr.Color(red=255, green=255, blue=255),
bmpr.Color(red=255, green=255, blue=255),
bmpr.Color(red=255, green=255, blue=255),
bmpr.Color(red=255, green=255, blue=255),
bmpr.Color(red=255, green=255, blue=255),
bmpr.Color(red=255, green=255, blue=255),
bmpr.Color(red=255, green=255, blue=255),
bmpr.Color(red=255, green=255, blue=255),
bmpr.Color(red=255, green=255, blue=255),
bmpr.Color(red=255, green=255, blue=255),
bmpr.Color(red=255, green=255, blue=255),
bmpr.Color(red=255, green=255, blue=255),
bmpr.Color(red=255, green=255, blue=255),
bmpr.Color(red=20, green=145, blue=113),
bmpr.Color(red=255, green=255, blue=255),
bmpr.Color(red=255, green=255, blue=255),
bmpr.Color(red=255, green=255, blue=255),
bmpr.Color(red=255, green=255, blue=255),
bmpr.Color(red=255, green=255, blue=255),
]
self.assertEqual(expected, actual)
def test_get_row_16bit_colors(self):
image_path = "images/16_bit_colors.bmp"
with open(image_path, "rb") as file_handle:
reader = bmpr.BMPFileReader(file_handle)
with self.assertRaises(ValueError) as context:
reader.get_row(0)
expected_msg = "This parser does not currently support BMP files with 16 bits per pixel. Currently only 24-bit color values are supported."
self.assertEquals(expected_msg, str(context.exception))
def test_get_row_16bit_colors(self):
image_path = "images/32_bit_colors.bmp"
with open(image_path, "rb") as file_handle:
reader = bmpr.BMPFileReader(file_handle)
with self.assertRaises(ValueError) as context:
reader.get_row(0)
expected_msg = "This parser does not currently support BMP files with 32 bits per pixel. Currently only 24-bit color values are supported."
self.assertEquals(expected_msg, str(context.exception))
class DIBHeaderTest(unittest.TestCase):
def test_repr_simple(self):
header = bmpr.DIBHeader(
width=1,
height=1,
num_color_planes=1,
bits_per_pixel=24,
compression_type=bmpr.CompressionType.BI_RGB,
raw_bitmap_size=4,
horizontal_resolution_ppm=11811,
vertical_resolution_ppm=11811,
num_colors_in_palette=0,
num_important_colors_used=0,
)
actual = repr(header)
expected = """DIBHeader(
width=1,
height=1,
num_color_planes=1,
bits_per_pixel=24,
compression_type=BI_RGB,
raw_bitmap_size=4,
horizontal_resolution_ppm=11811,
vertical_resolution_ppm=11811,
num_colors_in_palette=0,
num_important_colors_used=0,
)"""
self.assertEquals(expected, actual)