-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbitmap.py
59 lines (45 loc) · 1.83 KB
/
bitmap.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
from array import array
class BitplaneImage(object):
def __init__(self, byte_width, height, bitplanes):
self.byte_width = byte_width
self.height = height
self.bitplanes = bitplanes
def image_to_bitplanes(image, bit_depth):
"""
Convert a PIL image into bitplanes.
Returns a bitmap image.
"""
bitplanes = [array('c') for _ in xrange(bit_depth)]
width, height = image.size
byte_width = (width + 7) // 8
for row in xrange(height):
for byte in xrange(byte_width):
planes = [0] * bit_depth
for bit in xrange(8):
palette_index = image.getpixel((byte * 8 + 7 - bit, row))
for plane_index in range(bit_depth):
planes[plane_index] |= ((palette_index >> plane_index) & 1) << bit
for plane_index in range(bit_depth):
bitplanes[plane_index].append(chr(planes[plane_index]))
return BitplaneImage(byte_width, height, bitplanes)
def write_interleaved(file, image):
"""Write all bitplanes in interleaved mode to a file."""
for row in xrange(image.height):
offset = row * image.byte_width
for plane in image.bitplanes:
plane[offset:offset + image.byte_width].write(file)
def write_bitplane(file, image, bitplane_index):
"""Write a single bitplane to a file."""
image.bitplanes[bitplane_index].write(file)
def to_amiga_colors(palette):
"""
Convert 24 bit colors to Amiga 12 bit colors.
palette is a sequence of integers: [r, g, b, ...]
"""
number_of_colors = len(palette) // 3
amiga_colors = []
for color_index in xrange(number_of_colors):
r, g, b = [c for c in palette[color_index * 3:(color_index + 1) * 3]]
rgb = ((r >> 4) << 8) | ((g >> 4) << 4) | (b >> 4)
amiga_colors.append(rgb)
return amiga_colors