bitbank2 / JPEGDEC

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

callbacks require global variables. #39

Closed geiseri closed 1 year ago

geiseri commented 2 years ago

I have my display code encapsulated and the drawing callback lacks the ability to pass context to the callback. It would be nice if a context pointer could be passed in.

bitbank2 commented 2 years ago

I just added it; please give it a try. Set your context pointer by calling setContext(void *). In the draw callback, it is part of the JPEGDRAW structure (pDraw->pContext). If all works as you expect, I'll do an official release.

geiseri commented 2 years ago

It works like a charm! I am wondering though if it should be called fHandle since that is more or less what the context is in the case of jpeg_file_tag. Granted the open callback could suffer the same issue as draw just because you might have an instance that provides the "bootstrap" information for the jpeg_file_tag that is returned by open. The other alternative is to have the context from setContext passed in as a final argument for each callback function. This might not be a viable option though as it would break the published API. Thoughts?

bitbank2 commented 2 years ago

It's hard for me to say one way or another; I'm the worst "customer" for my libraries. Maybe I can change the name to "setDrawContext" to make it more evident that it relates to the DRAW callback. I can ask on Twitter; there will certainly be plenty of opinions shared there...

geiseri commented 1 year ago

Actually, in pngdecode you call it pUser. That seems like a good name.

bitbank2 commented 1 year ago

ok, I'll change it