[3662b5]: infinity / formats / bmp.py Maximize Restore History

Download this file

bmp.py    273 lines (207 with data), 8.2 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
# -*-python-*-
# ie_shell.py - Simple shell for Infinity Engine-based game files
# Copyright (C) 2004 by Jaroslav Benkovsky, <edheldil@users.sf.net>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# Conforms to GemRB e31133f8 (2012-08-23), BMPImporter
import sys
from infinity.format import Format, register_format
from infinity.image import Image
class BMP_Format (Format, Image):
header_desc = (
{ 'key': 'signature',
'type': 'STR2',
'off': 0x0000,
'label': 'Signature' },
{ 'key': 'size',
'type': 'DWORD',
'off':0x0002,
'label': 'File size'},
{ 'key': 'reserved',
'type': 'DWORD',
'off': 0x0006,
'label': 'Reserved(=0)'},
{ 'key': 'data_off',
'type': 'DWORD',
'off': 0x000A,
'label': 'Data offset'},
{ 'key': 'info_size',
'type': 'DWORD',
'off': 0x000E,
'label': 'Info header size'},
{ 'key': 'width',
'type': 'DWORD',
'off': 0x0012,
'label': 'Width'},
{ 'key': 'height',
'type': 'DWORD',
'off': 0x0016,
'label': 'Height'},
{ 'key': 'planes',
'type': 'WORD',
'off': 0x001A,
'label': 'Num of planes(=1)'},
{ 'key': 'bpp',
'type': 'WORD',
'off': 0x001C,
'label': 'Bits per pixel'},
{ 'key': 'compression',
'type': 'DWORD',
'off': 0x001E,
'label': 'Compression'},
{ 'key': 'data_size',
'type': 'DWORD',
'off': 0x0022,
'label': 'Raster data size'},
{ 'key': 'x_resolution',
'type': 'DWORD',
'off': 0x0026,
'label': 'Horizontal Resolution [pixels/meter]'},
{ 'key': 'y_resolution',
'type': 'DWORD',
'off': 0x002A,
'label': 'Vertical Resolution [pixels/meter]'},
{ 'key': 'colors',
'type': 'DWORD',
'off': 0x002E,
'label': 'Number of colors used'},
{ 'key': 'important_colors',
'type': 'DWORD',
'off': 0x0032,
'label': 'Number of important colors(0=all)'},
)
palette_entry_desc = (
{ 'key': 'b',
'type': 'BYTE',
'off': 0x0000,
'label': 'B'},
{ 'key': 'g',
'type': 'BYTE',
'off': 0x0001,
'label': 'G'},
{ 'key': 'r',
'type': 'BYTE',
'off': 0x0002,
'label': 'R'},
{ 'key': 'a',
'type': 'BYTE',
'off': 0x0003,
'label': 'A'},
)
def __init__ (self):
Format.__init__ (self)
Image.__init__ (self)
self.expect_signature = 'BM'
self.palette_entry_list = []
def read (self, stream):
self.read_header (stream)
if self.header['info_size'] < 24:
raise ValueError ("OS/2 bitmaps not supported")
if self.header['compression']:
raise ValueError ("Compressed bitmaps not supported")
width = self.header['width']
height = self.header['height']
bpp = self.header['bpp']
if bpp <= 8:
off = self.get_struc_size (self.header_desc)
self.read_palette (stream, off)
if bpp == 32:
padded_len = width * 4
elif bpp == 24:
padded_len = width * 3
elif bpp == 16:
padded_len = width * 2
elif bpp == 8:
padded_len = width
elif bpp == 4:
padded_len = width >> 1
else:
raise ValueError ("Bpp %d not supported" %bpp)
if padded_len & 3:
padded_len += (4 - (padded_len & 3))
size = padded_len * height
self.pixels_raw = stream.read_blob (self.header['data_off'], size)
data = [ '\0\0\0\0' ] * (width * height)
if bpp == 32:
self.decode_32bit_uncompressed (width, height, padded_len, data)
elif bpp == 24:
self.decode_24bit_uncompressed (width, height, padded_len, data)
#elif bpp == 16:
# self.decode_16bit_uncompressed (width, height, padded_len, data)
elif bpp == 8:
self.decode_8bit_uncompressed (width, height, padded_len, data)
elif bpp == 4:
self.decode_4bit_uncompressed (width, height, padded_len, data)
self.pixels = ''.join (data)
def decode_4bit_uncompressed (self, width, height, padded_len, data):
src = 0
for y in range (height):
for x in range (width):
if x & 1:
nib = ord (self.pixels_raw[src+x/2]) & 15
else:
nib = (ord (self.pixels_raw[src+x/2]) >> 4) & 15
c = self.palette_entry_list[nib]
pix = '%c%c%c\xff' %(c['r'], c['g'], c['b'])
data[(height-y-1) * width + x] = pix
src += padded_len
def decode_8bit_uncompressed (self, width, height, padded_len, data):
src = 0
for y in range (height):
for x in range (width):
c = self.palette_entry_list[ord (self.pixels_raw[src+x])]
pix = '%c%c%c\xff' %(c['r'], c['g'], c['b'])
data[(height-y-1) * width + x] = pix
src += padded_len
def decode_24bit_uncompressed (self, width, height, padded_len, data):
src = 0
p = self.pixels_raw
for y in range (height):
for x in range (width):
pix = '%c%c%c\xff' %(p[src+3*x+2], p[src+3*x+1], p[src+3*x])
data[(height-y-1) * width + x] = pix
src += padded_len
def decode_32bit_uncompressed (self, width, height, padded_len, data):
src = 0
p = self.pixels_raw
for y in range (height):
for x in range (width):
pix = '%c%c%c%c' %(p[src+4*x+2], p[src+4*x+1], p[src+4*x], p[src+4*x+3])
data[(height-y-1) * width + x] = pix
src += padded_len
def printme (self):
self.print_header ()
self.print_palette()
if self.get_option ('format.bmp.print_palette'):
self.print_palette ()
if self.get_option ('format.bmp.print_bitmap'):
self.print_bitmap ()
print()
def read_palette (self, stream, offset):
if self.header['colors']:
numcolors = self.header['colors']
elif self.header['bpp'] == 8:
numcolors = 256
else:
numcolors = 16
for i in range (numcolors):
obj = {}
self.read_struc (stream, offset, self.palette_entry_desc, obj)
self.palette_entry_list.append (obj)
offset = offset + 4
def print_palette (self):
for i, obj in enumerate (self.palette_entry_list):
print("%3d: %3d %3d %3d %3d (#%02x%02x%02x%02x)" %(i, obj['r'], obj['g'], obj['b'], obj['a'], obj['r'], obj['g'], obj['b'], obj['a']))
register_format ('BM', '', BMP_Format)