bitbank2 / JPEGDEC

An optimized JPEG decoder for Arduino
Apache License 2.0
365 stars 47 forks source link

Feature Request: Allow cropping on decode #52

Closed andrewleek closed 1 year ago

andrewleek commented 1 year ago

I have the following scenario: I am capturing an image at a slightly larger size than what i am displaying on on lcd. I am using both your JPEGDEC library as well as your bb_spi_lcd library. I am capturing at 400x296 but the display is only 320x240.

While drawing this image to the display, artifacts appear on the right of the screen, im guessing this is due to the extra 80 horizontal pixels that are pushed to the lcd display. (perhaps this is more an issue for bb_spi_lcd???).

To solve this (in pixel drawing callback) I am checking for pixels that are out of the range of the lcd and returning a new buffer with them trimmed.

*In full transparency I am actually trying to center the image on the display so i am calling decode with (-40,-28,0) which results in trimming on all sides of the image.

Now while this is working, it feels like it would be better to have this cropping done while the jpg was being decoded. Additionally, I think by trimming some of these pPixels buffers down later in the process, I am sometimes sending a less than optimal chunk of data to the display.

My proposal would be to add an optional rectangle definition to the decode function (x, y, width, height), perhaps under iOptions, where only pixels within that rectangle would be passed to the pixel drawing callback.

Please let me know what you think of this idea and if its viable, where should i be looking to implement it?

bitbank2 commented 1 year ago

It's a good idea and I've already implemented it in other permutations of this code. It will speed up the decoding a bit since you can avoid doing the IDCT and color conversion calculations on the MCUs that are not visible. I don't have time to make this change, but you're welcome to do a PR. Take a look in jpeg.inl at the loop starting at line 3316.

bitbank2 commented 2 weeks ago

I added this feature

andrewleek-droplab commented 5 days ago

Thank you so much for adding this feature!