forked from adafruit/Adafruit_CircuitPython_BitmapSaver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadafruit_bitmapsaver.py
152 lines (122 loc) · 5.36 KB
/
adafruit_bitmapsaver.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
# The MIT License (MIT)
#
# Copyright (c) 2019 Dave Astels for Adafruit Industries
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""
`adafruit_bitmapsaver`
================================================================================
Save a displayio.Bitmap (and associated displayio.Palette) in a BMP file.
Make a screenshot (the contents of a displayio.Display) and save in a BMP file.
* Author(s): Dave Astels
Implementation Notes
--------------------
**Hardware:**
**Software and Dependencies:**
* Adafruit CircuitPython firmware for the supported boards:
https://github.com/adafruit/circuitpython/releases
"""
# imports
import gc
import struct
import board
from displayio import Bitmap, Palette, Display
__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BitmapSaver.git"
def _write_bmp_header(output_file, filesize):
output_file.write(bytes("BM", "ascii"))
output_file.write(struct.pack("<I", filesize))
output_file.write(b"\00\x00")
output_file.write(b"\00\x00")
output_file.write(struct.pack("<I", 54))
def _write_dib_header(output_file, width, height):
output_file.write(struct.pack("<I", 40))
output_file.write(struct.pack("<I", width))
output_file.write(struct.pack("<I", height))
output_file.write(struct.pack("<H", 1))
output_file.write(struct.pack("<H", 24))
for _ in range(24):
output_file.write(b"\x00")
def _bytes_per_row(source_width):
pixel_bytes = 3 * source_width
padding_bytes = (4 - (pixel_bytes % 4)) % 4
return pixel_bytes + padding_bytes
def _rotated_height_and_width(pixel_source):
# flip axis if the display is rotated
if isinstance(pixel_source, Display) and (pixel_source.rotation % 180 != 0):
return (pixel_source.height, pixel_source.width)
return (pixel_source.width, pixel_source.height)
def _rgb565_to_bgr_tuple(color):
blue = (color << 3) & 0x00F8 # extract each of the RGB tripple into it's own byte
green = (color >> 3) & 0x00FC
red = (color >> 8) & 0x00F8
return (blue, green, red)
# pylint:disable=too-many-locals
def _write_pixels(output_file, pixel_source, palette):
saving_bitmap = isinstance(pixel_source, Bitmap)
width, height = _rotated_height_and_width(pixel_source)
row_buffer = bytearray(_bytes_per_row(width))
result_buffer = bytearray(2048)
for y in range(height, 0, -1):
buffer_index = 0
if saving_bitmap:
for x in range(width):
pixel = pixel_source[x, y - 1]
color = palette[pixel]
for _ in range(3):
row_buffer[buffer_index] = color & 0xFF
color >>= 8
buffer_index += 1
else:
data = pixel_source.fill_row(y - 1, result_buffer)
for i in range(width):
pixel565 = (data[i * 2] << 8) + data[i * 2 + 1]
for b in _rgb565_to_bgr_tuple(pixel565):
row_buffer[buffer_index] = b & 0xFF
buffer_index += 1
output_file.write(row_buffer)
gc.collect()
# pylint:enable=too-many-locals
def save_pixels(file_or_filename, pixel_source=board.DISPLAY, palette=None):
"""Save pixels to a 24 bit per pixel BMP file.
If pixel_source if a displayio.Bitmap, save it's pixels through palette.
If it's a displayio.Display, a palette isn't required.
:param file_or_filename: either the file to save to, or it's absolute name
:param pixel_source: the Bitmap or Display to save
:param palette: the Palette to use for looking up colors in the bitmap
"""
if isinstance(pixel_source, Bitmap):
if not isinstance(palette, Palette):
raise ValueError("Third argument must be a Palette for a Bitmap save")
elif not isinstance(pixel_source, Display):
raise ValueError("Second argument must be a Bitmap or Display")
try:
if isinstance(file_or_filename, str):
output_file = open(file_or_filename, "wb")
else:
output_file = file_or_filename
width, height = _rotated_height_and_width(pixel_source)
filesize = 54 + height * _bytes_per_row(width)
_write_bmp_header(output_file, filesize)
_write_dib_header(output_file, width, height)
_write_pixels(output_file, pixel_source, palette)
except Exception as ex:
raise ex
else:
output_file.close()