Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for RGB, RGBA and Grayscale PNGs #83

Merged
merged 1 commit into from
Aug 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add support for RGB, RGBA and Grayscale PNGs
This adds support for filters, which lets us decode grayscale and RGB
images. I only support 8bit depth for now (and 24bit for RGB) but with
better color conversion code, other depths could be supported.
  • Loading branch information
deshipu committed Aug 3, 2024
commit 8e6af9bdd08a317978e4a2aa7881459e1b2ff050
83 changes: 74 additions & 9 deletions adafruit_imageload/png.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def load(
:param object palette: Type to store the palette. Must have API similar to
`displayio.Palette`. Will be skipped if None.
"""
# pylint: disable=too-many-locals,too-many-branches, consider-using-enumerate, too-many-statements
# pylint: disable=too-many-locals, too-many-branches, consider-using-enumerate, too-many-statements, import-outside-toplevel, invalid-name
header = file.read(8)
if header != b"\x89PNG\r\n\x1a\n":
raise ValueError("Not a PNG file")
Expand Down Expand Up @@ -103,15 +103,80 @@ def load(
file.seek(size, 1) # skip unknown chunks
file.seek(4, 1) # skip CRC
data_bytes = zlib.decompress(data)
bmp = bitmap(width, height, 1 << depth)
scanline = (width * depth + 7) // 8
mem = memoryview(bmp)
unit = (1, 0, 3, 1, 2, 0, 4)[mode]
scanline = (width * depth * unit + 7) // 8
colors = 1 << (depth * unit)
if mode == 3: # indexed
bmp = bitmap(width, height, colors)
mem = memoryview(bmp)
for y in range(height):
dst = y * scanline
src = y * (scanline + 1) + 1
mem[dst : dst + scanline] = data_bytes[src : src + scanline]
return bmp, pal
# RGB, RGBA or Grayscale
import displayio

if depth != 8:
raise ValueError("Must be 8bit depth.")
pal = displayio.ColorConverter(input_colorspace=displayio.Colorspace.RGB888)
bmp = bitmap(width, height, 65536)
prev = bytearray(scanline)
line = bytearray(scanline)
for y in range(height):
dst = y * scanline
src = y * (scanline + 1) + 1
filter_ = data_bytes[src - 1]
src = y * (scanline + 1)
filter_ = data_bytes[src]
src += 1
if filter_ == 0:
mem[dst : dst + scanline] = data_bytes[src : src + scanline]
line[0:scanline] = data_bytes[src : src + scanline]
elif filter_ == 1: # sub
for i in range(scanline):
a = line[i - unit] if i >= unit else 0
line[i] = (data_bytes[src] + a) & 0xFF
src += 1
elif filter_ == 2: # up
for i in range(scanline):
b = prev[i]
line[i] = (data_bytes[src] + b) & 0xFF
src += 1
elif filter_ == 3: # average
for i in range(scanline):
a = line[i - unit] if i >= unit else 0
b = prev[i]
line[i] = (data_bytes[src] + ((a + b) >> 1)) & 0xFF
src += 1
elif filter_ == 4: # paeth
for i in range(scanline):
a = line[i - unit] if i >= unit else 0
b = prev[i]
c = prev[i - unit] if i >= unit else 0
p = a + b - c
pa = abs(p - a)
pb = abs(p - b)
pc = abs(p - c)
if pa <= pb and pa <= pc:
p = a
elif pb <= pc:
p = b
else:
p = c
line[i] = (data_bytes[src] + p) & 0xFF
src += 1
else:
raise ValueError("Wrong filter.")
prev, line = line, prev
if mode in (0, 4): # grayscale
for x in range(width):
c = line[x * unit]
bmp[x, y] = pal.convert((c << 16) | (c << 8) | c)
elif mode in {2, 6}: # rgb
for x in range(width):
bmp[x, y] = pal.convert(
(line[x * unit + 0] << 16)
| (line[x * unit + 1] << 8)
| line[x * unit + 2]
)
else:
raise NotImplementedError("Filters not supported")
raise ValueError("Unsupported color mode.")
pal = displayio.ColorConverter(input_colorspace=displayio.Colorspace.RGB565)
return bmp, pal