Skip to content

Commit db44df5

Browse files
committed
blend & dither tests
1 parent f4e68e8 commit db44df5

File tree

4 files changed

+116
-31
lines changed

4 files changed

+116
-31
lines changed

tests/circuitpython/_bmp16.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import ulab.numpy as np
2+
import displayio
3+
import bitmaptools
4+
5+
try:
6+
import struct
7+
except:
8+
import ustruct as struct
9+
10+
base_header = b"BMFX\x02\x00\x00\x00\x00\x00F\x00\x00\x008\x00\x00\x00@\x01\x00\x00\xf0\x00\x00\x00\x01\x00\x10\x00\x03\x00\x00\x00\x00X\x02\x00\xd7\r\x00\x00\xd7\r\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf8\x00\x00\xe0\x07\x00\x00\x1f\x00\x00\x00\x00\x00\x00\x00"
11+
12+
13+
def writebmp16(filename, bitmap):
14+
header = bytearray(base_header)
15+
header[18:26] = struct.pack("<II", bitmap.width, bitmap.height)
16+
with open(filename, "wb") as f:
17+
f.write(header)
18+
b = np.frombuffer(bitmap, dtype=np.uint16)
19+
for i in range(bitmap.height):
20+
j = (bitmap.height - i - 1) * bitmap.width
21+
f.write(b[j : j + bitmap.width])
22+
23+
24+
def loadbmp16(filename, width=320, height=240):
25+
"""This specialized routine loads 16bpp uncompressed bmp files with a
26+
70-byte header. It is not appropriate for generic bmp files."""
27+
28+
bitmap = displayio.Bitmap(width, height, 65536)
29+
with open(filename, "rb") as f:
30+
f.seek(70)
31+
bitmaptools.readinto(
32+
bitmap,
33+
f,
34+
bits_per_pixel=16,
35+
element_size=2,
36+
reverse_rows=True,
37+
)
38+
39+
return bitmap
40+
41+
42+
if __name__ == "__main__":
43+
if "/" in __file__:
44+
here = __file__.rsplit("/", 1)[0]
45+
else:
46+
here = "."
47+
b = loadbmp16(here + "/minerva16.bmp")
48+
print(b[0, 0])
49+
print(b[160, 160])
50+
for i, p in enumerate(sorted(set(memoryview(b)))):
51+
print("%04x" % p, end="\n" if (i % 8) == 7 else " ")
52+
if i % 8 != 7:
53+
print()

tests/circuitpython/_loadbmp16.py

Lines changed: 0 additions & 31 deletions
This file was deleted.

tests/circuitpython/blend.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import bitmaptools
2+
import displayio
3+
import _bmp16
4+
5+
if "/" in __file__:
6+
here = __file__.rsplit("/", 1)[0]
7+
else:
8+
here = "."
9+
10+
c = displayio.Colorspace.BGR565
11+
12+
b1 = _bmp16.loadbmp16(here + "/minerva16.bmp")
13+
b2 = _bmp16.loadbmp16(here + "/blinka16.bmp")
14+
b3 = displayio.Bitmap(320, 240, 65536)
15+
16+
for i in (
17+
0,
18+
1 / 64,
19+
3 / 64,
20+
3 / 32,
21+
3 / 16,
22+
0.5,
23+
1 - 3 / 16,
24+
1 - 3 / 32,
25+
1 - 3 / 64,
26+
1 - 1 / 64,
27+
1,
28+
):
29+
bitmaptools.alphablend(b3, b1, b2, c, i)
30+
_bmp16.writebmp16(f"blend-{i:.2f}.bmp", b3)
31+
bitmaptools.alphablend(b3, b1, b2, c, i, 0)
32+
_bmp16.writebmp16(f"fade-{i:.2f}.bmp", b3)

tests/circuitpython/dither.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import bitmaptools
2+
import displayio
3+
import _bmp16
4+
5+
if "/" in __file__:
6+
here = __file__.rsplit("/", 1)[0]
7+
else:
8+
here = "."
9+
10+
c = displayio.Colorspace.BGR565
11+
12+
b1 = _bmp16.loadbmp16(here + "/minerva16.bmp")
13+
b3 = displayio.Bitmap(320, 240, 65536)
14+
15+
for i in (
16+
0,
17+
1 / 64,
18+
3 / 64,
19+
3 / 32,
20+
3 / 16,
21+
0.5,
22+
1 - 3 / 16,
23+
1 - 3 / 32,
24+
1 - 3 / 64,
25+
1 - 1 / 64,
26+
1,
27+
):
28+
bitmaptools.dither(b3, b1, c)
29+
_bmp16.writebmp16(f"dither-atkinson.bmp", b3)
30+
bitmaptools.dither(b3, b1, c, bitmaptools.DitherAlgorithm.FloydStenberg)
31+
_bmp16.writebmp16(f"dither-floydstenberg.bmp", b3)

0 commit comments

Comments
 (0)