blend & dither tests

This commit is contained in:
Jeff Epler 2021-11-20 21:13:45 -05:00
parent f4e68e8fae
commit db44df5dbf
No known key found for this signature in database
GPG Key ID: D5BF15AB975AB4DE
4 changed files with 116 additions and 31 deletions

View File

@ -0,0 +1,53 @@
import ulab.numpy as np
import displayio
import bitmaptools
try:
import struct
except:
import ustruct as struct
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"
def writebmp16(filename, bitmap):
header = bytearray(base_header)
header[18:26] = struct.pack("<II", bitmap.width, bitmap.height)
with open(filename, "wb") as f:
f.write(header)
b = np.frombuffer(bitmap, dtype=np.uint16)
for i in range(bitmap.height):
j = (bitmap.height - i - 1) * bitmap.width
f.write(b[j : j + bitmap.width])
def loadbmp16(filename, width=320, height=240):
"""This specialized routine loads 16bpp uncompressed bmp files with a
70-byte header. It is not appropriate for generic bmp files."""
bitmap = displayio.Bitmap(width, height, 65536)
with open(filename, "rb") as f:
f.seek(70)
bitmaptools.readinto(
bitmap,
f,
bits_per_pixel=16,
element_size=2,
reverse_rows=True,
)
return bitmap
if __name__ == "__main__":
if "/" in __file__:
here = __file__.rsplit("/", 1)[0]
else:
here = "."
b = loadbmp16(here + "/minerva16.bmp")
print(b[0, 0])
print(b[160, 160])
for i, p in enumerate(sorted(set(memoryview(b)))):
print("%04x" % p, end="\n" if (i % 8) == 7 else " ")
if i % 8 != 7:
print()

View File

@ -1,31 +0,0 @@
import displayio
import bitmaptools
def loadbmp16(filename, width=320, height=240):
"""This specialized routine loads 16bpp uncompressed bmp files with a
70-byte header. It is not appropriate for generic bmp files."""
bitmap = displayio.Bitmap(width, height, 65536)
with open(filename, "rb") as f:
f.seek(70)
bitmaptools.readinto(
bitmap,
f,
bits_per_pixel=16,
element_size=2,
swap_bytes_in_element=True,
reverse_rows=True,
)
return bitmap
if __name__ == "__main__":
if "/" in __file__:
here = __file__.rsplit("/", 1)[0]
else:
here = "."
b = loadbmp16(here + "/minerva16.bmp")
print(b[0, 0])
print(b[160, 160])

View File

@ -0,0 +1,32 @@
import bitmaptools
import displayio
import _bmp16
if "/" in __file__:
here = __file__.rsplit("/", 1)[0]
else:
here = "."
c = displayio.Colorspace.BGR565
b1 = _bmp16.loadbmp16(here + "/minerva16.bmp")
b2 = _bmp16.loadbmp16(here + "/blinka16.bmp")
b3 = displayio.Bitmap(320, 240, 65536)
for i in (
0,
1 / 64,
3 / 64,
3 / 32,
3 / 16,
0.5,
1 - 3 / 16,
1 - 3 / 32,
1 - 3 / 64,
1 - 1 / 64,
1,
):
bitmaptools.alphablend(b3, b1, b2, c, i)
_bmp16.writebmp16(f"blend-{i:.2f}.bmp", b3)
bitmaptools.alphablend(b3, b1, b2, c, i, 0)
_bmp16.writebmp16(f"fade-{i:.2f}.bmp", b3)

View File

@ -0,0 +1,31 @@
import bitmaptools
import displayio
import _bmp16
if "/" in __file__:
here = __file__.rsplit("/", 1)[0]
else:
here = "."
c = displayio.Colorspace.BGR565
b1 = _bmp16.loadbmp16(here + "/minerva16.bmp")
b3 = displayio.Bitmap(320, 240, 65536)
for i in (
0,
1 / 64,
3 / 64,
3 / 32,
3 / 16,
0.5,
1 - 3 / 16,
1 - 3 / 32,
1 - 3 / 64,
1 - 1 / 64,
1,
):
bitmaptools.dither(b3, b1, c)
_bmp16.writebmp16(f"dither-atkinson.bmp", b3)
bitmaptools.dither(b3, b1, c, bitmaptools.DitherAlgorithm.FloydStenberg)
_bmp16.writebmp16(f"dither-floydstenberg.bmp", b3)