From aa6ee82de288eecdbca26ac82e965f79212495a9 Mon Sep 17 00:00:00 2001 From: Chris Twomey Date: Wed, 5 Jul 2023 21:34:29 +0100 Subject: [PATCH] Add drawPngFromBuffer --- src/include/Image.h | 2 ++ src/include/ImagePNG.cpp | 47 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/src/include/Image.h b/src/include/Image.h index f318f45c..6ac1d66c 100644 --- a/src/include/Image.h +++ b/src/include/Image.h @@ -96,6 +96,8 @@ class Image : virtual public NetworkClient, virtual public Adafruit_GFX bool drawJpegFromWeb(const char *url, int x, int y, bool dither = 0, bool invert = 0); bool drawJpegFromWeb(WiFiClient *s, int x, int y, int32_t len, bool dither = 0, bool invert = 0); + bool drawPngFromBuffer(uint8_t *buf, int32_t len, int x, int y, bool dither, bool invert); + bool drawPngFromSd(const char *fileName, int x, int y, bool dither = 0, bool invert = 0); bool drawPngFromSd(SdFile *p, int x, int y, bool dither = 0, bool invert = 0); diff --git a/src/include/ImagePNG.cpp b/src/include/ImagePNG.cpp index 5aee015a..879ea70a 100644 --- a/src/include/ImagePNG.cpp +++ b/src/include/ImagePNG.cpp @@ -244,6 +244,53 @@ bool Image::drawPngFromWeb(const char *url, int x, int y, bool dither, bool inve return ret; } +/** + * @brief drawPngFromBuffer function draws png image from buffer + * + * @param int32_t len + * size of buffer + * @param int x + * x position for top left image corner + * @param int y + * y position for top left image corner + * @param bool dither + * 1 if using dither, 0 if not + * @param bool invert + * 1 if using invert, 0 if not + * + * @return 1 if drawn successfully, 0 if not + */ +bool Image::drawPngFromBuffer(uint8_t *buf, int32_t len, int x, int y, bool dither, bool invert) +{ + if (!buf) + return 0; + + _pngDither = dither; + _pngInvert = invert; + lastY = y; + + bool ret = 1; + + if (dither) + memset(ditherBuffer, 0, sizeof ditherBuffer); + + pngle_t *pngle = pngle_new(); + _pngX = x; + _pngY = y; + pngle_set_draw_callback(pngle, pngle_on_draw); + + if (!buf) + return 0; + + if (pngle_feed(pngle, buf, len) < 0) + ret = 0; + + pngle_destroy(pngle); + free(buf); + return ret; +} + + /** * @brief drawPngFromWeb function draws png image from sd file *