adafruit / Adafruit_CircuitPython_ImageLoad

Super-slim image decoder that loads code as needed to minimize memory use
MIT License
15 stars 20 forks source link

BytesIO support #53

Closed FoamyGuy closed 3 years ago

FoamyGuy commented 3 years ago

resolves #51 and originally stemmed from #4831

These changes begin the process of adding support for BytesIO (and possibly other "file-like" objects).

In order to achieve full support for this on modern versions we will need to modify bitmap.readinto() in the core to be able to work with file-like objects.

I was able to successfully test this using the python readinto code from here: https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad/blob/681a60401c0ae3f44a5bd14e06bb8cb384aa2991/adafruit_imageload/bmp/indexed.py#L118

For the purposes of testing I just modified the if statement to force it to use the python readinto code instead of the core bitmaptools.readinto(). My test was run on a FunHouse with 7.0.0-alpha.3 and it did show the downloaded image.

As-is in the core this line ends up raising a TypeError:

https://github.com/adafruit/circuitpython/blob/5f81f9ed1cb9ba98436ebdc54756ef1702681703/shared-bindings/bitmaptools/__init__.c#L479

So for full support we'll need to modify that check and the code that comes after it to be okay with accepting a BytesIO object instead of a file. I poked around with this for a bit, but the C code in question is a bit beyond my capabilities at this point. @jepler I think you may have written this function originally do you have any thoughts on whether it would be possible to modify it to accept BytesIO in addition to file type objects?