Skip to content

IndexError with some PNG files #95

Closed
@FoamyGuy

Description

@FoamyGuy

This image file:

Image

results in this exception being raised:

Traceback (most recent call last):
  File "code.py", line 34, in <module>
  File "/lib/adafruit_imageload/__init__.py", line 92, in load
  File "/lib/adafruit_imageload/png.py", line 116, in load
IndexError: x must be 0-8

It traces to this line: https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad/blob/main/adafruit_imageload/png.py#L116

bmp[x + pixel, y] = (byte >> ((pixels_per_byte - pixel - 1) * depth)) & pixmask

I think the root cause for this might be related to something mentioned here in section 2.3: https://www.w3.org/TR/PNG-DataRep.html

Scanlines always begin on byte boundaries. When pixels have fewer than 8 bits and the scanline width is not evenly divisible by the number of pixels per byte, the low-order bits in the last byte of each scanline are wasted. The contents of these wasted bits are unspecified.

It seems to me our implementation doesn't account for this because we always try to access the pixel at x index x + pixel without regard for the width. This image has width 9, and the pixels per byte is 4, so the last byte of the scanline will contain 3 "extra" pixels worth of data that should be ignored.

modifying png load like this allows it to load and render correctly:

if x + pixel < width:
    bmp[x + pixel, y] = (byte >> ((pixels_per_byte - pixel - 1) * depth)) & pixmask

However I haven't done much testing with other images to ensure it doesn't introduce other problems.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy