-
Notifications
You must be signed in to change notification settings - Fork 0
/
bmp.py
200 lines (170 loc) · 7.62 KB
/
bmp.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
from Structure.BitmapInfo.bitmapCoreHeader import BitmapCoreHeader
from Structure.BitmapInfo.bitmapInfoHeader import BitmapInfoHeader
from Structure.BitmapInfo.bitmapV2InfoHeader import BitmapV2InfoHeader
from Structure.BitmapInfo.bitmapV3InfoHeader import BitmapV3InfoHeader
from Structure.BitmapInfo.bitmapV4Header import BitmapV4Header
from Structure.BitmapInfo.bitmapV5Header import BitmapV5Header
from Structure.BitmapInfo.os22XBitmapHeader import Os22XBitmapHeader
from Structure.bitmapFileHeader import BitmapFileHeader
from Structure.pixelData import PixelData
from Structure.decode_bmp_rle import BI_RLE
from struct import unpack
import os
from sys import path
class NotBmpFormat(Exception):
"""path points to a file does not bmp format"""
pass
class BMP(object):
"""creates a detailed description of the internal structure "
"of the BMP file and image"""
def __init__(self, path):
super(BMP, self).__init__()
if path[-4:] != ".bmp":
raise NotBmpFormat("path points to a file does not bmp format")
self._path = path
self._byte_array = self._open(self._path)
self._bitmap_file_header = BitmapFileHeader(self._byte_array)
size_bitmap_info = unpack("i", self._byte_array[14:14 + 4])[0]
self._bitmapInfo = self._get_actual_version(size_bitmap_info)
self._bitmapInfo.fill_color_table(self._byte_array)
self._pixel_data = PixelData(
self._byte_array,
self._bitmap_file_header.get_off_bits().get_unpack_value(),
self._bitmapInfo)
if self._bitmapInfo.get_size_header().get_unpack_value() != 12 and \
self._bitmapInfo.get_compression().get_unpack_value() \
in (1, 2):
rle = BI_RLE(path)
self._pixel_data.set_pixel_data(rle.Decode())
def _get_actual_version(self, size):
"""returns an appropriate version of the BMP file"""
if size == 12:
return(BitmapCoreHeader(self._byte_array))
elif size == 40:
return(BitmapInfoHeader(self._byte_array))
elif size == 52:
return(BitmapV2InfoHeader(self._byte_array))
elif size == 56:
return(BitmapV3InfoHeader(self._byte_array))
elif size == 64:
return(Os22XBitmapHeader(self._byte_array))
elif size == 108:
return(BitmapV4Header(self._byte_array))
elif size == 124:
return(BitmapV5Header(self._byte_array))
else:
return(None)
def get_pixel_data(self):
"""returns pixel data"""
return(self._pixel_data)
def get_color_table(self):
"""returns the color table"""
return(self._bitmapInfo.get_color_table())
def _open(self, path):
"""opens the file with an image"""
with open(path, "rb") as bmpFile:
return(bmpFile.read())
def get_byte_array(self):
"""returns an array of bytes"""
return(self._byte_array)
def get_bitmap_file_header(self):
"""returns the title BMP file"""
return(self._bitmap_file_header)
def get_bitmap_info(self):
"""returns information about the BMP file"""
return(self._bitmapInfo)
@staticmethod
def test():
lever = False
with open(os.path.join(path[0], "test", "coreHeader.txt")) as f:
bmp = BMP(os.path.join(path[0], "test", "coreHeader.bmp"))
information = bmp.get_bitmap_file_header().get_all_info() +\
bmp.get_bitmap_info().get_all_info()
print("Test bmp version coreHeader:")
if information == f.read():
print("Verification was successful")
else:
print("Validation fails")
lever = True
with open(os.path.join(path[0], "test", "infoHeader.txt")) as f:
bmp = BMP(os.path.join(path[0], "test", "infoHeader.bmp"))
information = bmp.get_bitmap_file_header().get_all_info() +\
bmp.get_bitmap_info().get_all_info()
print("Test bmp version infoHeader:")
if information == f.read():
print("Verification was successful")
else:
print("Validation fails")
lever = True
with open(os.path.join(path[0], "test", "os21.txt")) as f:
bmp = BMP(os.path.join(path[0], "test", "os21.bmp"))
information = bmp.get_bitmap_file_header().get_all_info() +\
bmp.get_bitmap_info().get_all_info()
print("Test bmp version os21:")
if information == f.read():
print("Verification was successful")
else:
print("Validation fails")
lever = True
with open(os.path.join(path[0], "test", "os22.txt")) as f:
bmp = BMP(os.path.join(path[0], "test", "os22.bmp"))
information = bmp.get_bitmap_file_header().get_all_info() +\
bmp.get_bitmap_info().get_all_info()
print("Test bmp version os22:")
if information == f.read():
print("Verification was successful")
else:
print("Validation fails")
lever = True
with open(os.path.join(path[0], "test", "v2header.txt")) as f:
bmp = BMP(os.path.join(path[0], "test", "v2header.bmp"))
information = bmp.get_bitmap_file_header().get_all_info() +\
bmp.get_bitmap_info().get_all_info()
print("Test bmp version v2header:")
if information == f.read():
print("Verification was successful")
else:
print("Validation fails")
lever = True
with open(os.path.join(path[0], "test", "v3header.txt")) as f:
bmp = BMP(os.path.join(path[0], "test", "v3header.bmp"))
information = bmp.get_bitmap_file_header().get_all_info() +\
bmp.get_bitmap_info().get_all_info()
print("Test bmp version v3header:")
if information == f.read():
print("Verification was successful")
else:
print("Validation fails")
lever = True
with open(os.path.join(path[0], "test", "v4header.txt")) as f:
bmp = BMP(os.path.join(path[0], "test", "v4header.bmp"))
information = bmp.get_bitmap_file_header().get_all_info() +\
bmp.get_bitmap_info().get_all_info()
print("Test bmp version v4header:")
if information == f.read():
print("Verification was successful")
else:
print("Validation fails")
lever = True
with open(os.path.join(path[0], "test", "v5header.txt")) as f:
bmp = BMP(os.path.join(path[0], "test", "v5header.bmp"))
information = bmp.get_bitmap_file_header().get_all_info() +\
bmp.get_bitmap_info().get_all_info()
print("Test bmp version v5header:")
if information == f.read():
print("Verification was successful")
else:
print("Validation fails")
lever = True
with open(os.path.join(
path[0], "test", "pixelDataInfoHeader.txt")) as f:
bmp = BMP(os.path.join(path[0], "test", "infoHeader.bmp"))
pixelData = str(bmp.get_pixel_data().get_pixel_data())
print("Test bmp pixelData:")
if pixelData == f.read():
print("Verification was successful")
else:
print("Validation fails")
lever = True
print("Complete test")
return lever