From 386e49480d449596b6f878c572241470a1cf9577 Mon Sep 17 00:00:00 2001 From: Joel Bodenmann Date: Thu, 10 Nov 2016 23:00:31 +0100 Subject: Adding ability to modify the color palette of BMP images (untested) --- src/gdisp/gdisp_image.c | 25 +++++++++++----- src/gdisp/gdisp_image.h | 4 +++ src/gdisp/gdisp_image_bmp.c | 69 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/gdisp/gdisp_image.c b/src/gdisp/gdisp_image.c index 356ba64f..13001d5c 100644 --- a/src/gdisp/gdisp_image.c +++ b/src/gdisp/gdisp_image.c @@ -33,6 +33,9 @@ extern gdispImageError gdispImageCache_BMP(gdispImage *img); extern gdispImageError gdispGImageDraw_BMP(GDisplay *g, gdispImage *img, coord_t x, coord_t y, coord_t cx, coord_t cy, coord_t sx, coord_t sy); extern delaytime_t gdispImageNext_BMP(gdispImage *img); + extern int gdispImageGetPaletteSize_BMP(gdispImage *img); + extern color_t gdispImageGetPalette_BMP(gdispImage *img, int index); + extern bool_t gdispImageAdjustPalette_BMP(gdispImage *img, int index, color_t newColor); #endif #if GDISP_NEED_IMAGE_JPG @@ -53,41 +56,49 @@ /* The structure defining the routines for image drawing */ typedef struct gdispImageHandlers { - gdispImageError (*open)(gdispImage *img); /* The open function */ - void (*close)(gdispImage *img); /* The close function */ - gdispImageError (*cache)(gdispImage *img); /* The cache function */ + gdispImageError (*open)(gdispImage *img); /* The open function */ + void (*close)(gdispImage *img); /* The close function */ + gdispImageError (*cache)(gdispImage *img); /* The cache function */ gdispImageError (*draw)(GDisplay *g, gdispImage *img, coord_t x, coord_t y, coord_t cx, coord_t cy, - coord_t sx, coord_t sy); /* The draw function */ - delaytime_t (*next)(gdispImage *img); /* The next frame function */ + coord_t sx, coord_t sy); /* The draw function */ + delaytime_t (*next)(gdispImage *img); /* The next frame function */ + int (*getPaletteSize)(gdispImage *img); /* Retrieve the size of the palette (number of entries) */ + color_t (*getPalette)(gdispImage *img, int index); /* Retrieve a specific color value of the palette */ + bool_t (*adjustPalette)(gdispImage *img, int index, color_t newColor); /* Replace a color value in the palette */ } gdispImageHandlers; static gdispImageHandlers ImageHandlers[] = { #if GDISP_NEED_IMAGE_NATIVE { gdispImageOpen_NATIVE, gdispImageClose_NATIVE, gdispImageCache_NATIVE, gdispGImageDraw_NATIVE, gdispImageNext_NATIVE, + 0, 0, 0 }, #endif #if GDISP_NEED_IMAGE_GIF { gdispImageOpen_GIF, gdispImageClose_GIF, gdispImageCache_GIF, gdispGImageDraw_GIF, gdispImageNext_GIF, + 0, 0, 0 }, #endif #if GDISP_NEED_IMAGE_BMP - { gdispImageOpen_BMP, gdispImageClose_BMP, - gdispImageCache_BMP, gdispGImageDraw_BMP, gdispImageNext_BMP, + { gdispImageOpen_BMP, gdispImageClose_BMP, + gdispImageCache_BMP, gdispGImageDraw_BMP, gdispImageNext_BMP, + gdispImageGetPaletteSize_BMP, gdispImageGetPalette_BMP, gdispImageAdjustPalette_BMP }, #endif #if GDISP_NEED_IMAGE_JPG { gdispImageOpen_JPG, gdispImageClose_JPG, gdispImageCache_JPG, gdispGImageDraw_JPG, gdispImageNext_JPG, + 0, 0, 0 }, #endif #if GDISP_NEED_IMAGE_PNG { gdispImageOpen_PNG, gdispImageClose_PNG, gdispImageCache_PNG, gdispGImageDraw_PNG, gdispImageNext_PNG, + 0, 0, 0 }, #endif }; diff --git a/src/gdisp/gdisp_image.h b/src/gdisp/gdisp_image.h index 2b6fdf19..a07a633d 100644 --- a/src/gdisp/gdisp_image.h +++ b/src/gdisp/gdisp_image.h @@ -248,6 +248,10 @@ extern "C" { */ delaytime_t gdispImageNext(gdispImage *img); + int gdispImageGetPaletteSize(gdispImage *img); + color_t gdispImageGetPalette(gdispImage *img, int index); + bool_t gdispImageAdjustPalette(gdispImage *img, int index, color_t newColor); + #ifdef __cplusplus } #endif diff --git a/src/gdisp/gdisp_image_bmp.c b/src/gdisp/gdisp_image_bmp.c index 371fdf2d..f8ac07ab 100644 --- a/src/gdisp/gdisp_image_bmp.c +++ b/src/gdisp/gdisp_image_bmp.c @@ -829,4 +829,73 @@ delaytime_t gdispImageNext_BMP(gdispImage *img) { return TIME_INFINITE; } +int gdispImageGetPaletteSize_BMP(gdispImage *img) { + #if GDISP_NEED_IMAGE_BMP_1 || GDISP_NEED_IMAGE_BMP_4 || GDISP_NEED_IMAGE_BMP_8 + gdispImagePrivate_BMP *priv; + + priv = (gdispImagePrivate_BMP *)img->priv; + if (!priv) { + return 0; + } + + if (!(priv->bmpflags & BMP_PALETTE)) { + return 0; + } + + return priv->palsize; + #else + return 0; + #endif +} + +color_t gdispImageGetPalette_BMP(gdispImage *img, int index) { + #if GDISP_NEED_IMAGE_BMP_1 || GDISP_NEED_IMAGE_BMP_4 || GDISP_NEED_IMAGE_BMP_8 + gdispImagePrivate_BMP *priv; + + priv = (gdispImagePrivate_BMP *)img->priv; + if (!priv) { + return 0; + } + + if (!(priv->bmpflags & BMP_PALETTE)) { + return 0; + } + + if (index < 0 || index >= priv->palsize) { + return 0; + } + + return priv->palette[index]; + + #else + return 0; + #endif +} + +bool_t gdispImageAdjustPalette_BMP(gdispImage *img, int index, color_t newColor) { + #if GDISP_NEED_IMAGE_BMP_1 || GDISP_NEED_IMAGE_BMP_4 || GDISP_NEED_IMAGE_BMP_8 + gdispImagePrivate_BMP *priv; + + priv = (gdispImagePrivate_BMP *)img->priv; + if (!priv) { + return FALSE; + } + + if (!(priv->bmpflags & BMP_PALETTE)) { + return FALSE; + } + + if (index < 0 || index >= priv->palsize) { + return FALSE; + } + + priv->palette[index] = newColor; + + return TRUE; + + #else + return 0; + #endif +} + #endif /* GFX_USE_GDISP && GDISP_NEED_IMAGE && GDISP_NEED_IMAGE_BMP */ -- cgit v1.2.3 From ebfe1e95a240cdd29d295522f002325c65df622b Mon Sep 17 00:00:00 2001 From: Joel Bodenmann Date: Fri, 11 Nov 2016 18:28:48 +0100 Subject: Minor changes & improvements on image color palletization handling --- src/gdisp/gdisp_image.c | 31 +++++++++++++++++++++++++------ src/gdisp/gdisp_image.h | 8 ++++---- src/gdisp/gdisp_image_bmp.c | 34 +++++++++++++--------------------- 3 files changed, 42 insertions(+), 31 deletions(-) (limited to 'src') diff --git a/src/gdisp/gdisp_image.c b/src/gdisp/gdisp_image.c index 13001d5c..347ed6aa 100644 --- a/src/gdisp/gdisp_image.c +++ b/src/gdisp/gdisp_image.c @@ -33,9 +33,9 @@ extern gdispImageError gdispImageCache_BMP(gdispImage *img); extern gdispImageError gdispGImageDraw_BMP(GDisplay *g, gdispImage *img, coord_t x, coord_t y, coord_t cx, coord_t cy, coord_t sx, coord_t sy); extern delaytime_t gdispImageNext_BMP(gdispImage *img); - extern int gdispImageGetPaletteSize_BMP(gdispImage *img); - extern color_t gdispImageGetPalette_BMP(gdispImage *img, int index); - extern bool_t gdispImageAdjustPalette_BMP(gdispImage *img, int index, color_t newColor); + extern uint16_t gdispImageGetPaletteSize_BMP(gdispImage *img); + extern color_t gdispImageGetPalette_BMP(gdispImage *img, uint16_t index); + extern bool_t gdispImageAdjustPalette_BMP(gdispImage *img, uint16_t index, color_t newColor); #endif #if GDISP_NEED_IMAGE_JPG @@ -65,9 +65,9 @@ typedef struct gdispImageHandlers { coord_t cx, coord_t cy, coord_t sx, coord_t sy); /* The draw function */ delaytime_t (*next)(gdispImage *img); /* The next frame function */ - int (*getPaletteSize)(gdispImage *img); /* Retrieve the size of the palette (number of entries) */ - color_t (*getPalette)(gdispImage *img, int index); /* Retrieve a specific color value of the palette */ - bool_t (*adjustPalette)(gdispImage *img, int index, color_t newColor); /* Replace a color value in the palette */ + uint16_t (*getPaletteSize)(gdispImage *img); /* Retrieve the size of the palette (number of entries) */ + color_t (*getPalette)(gdispImage *img, uint16_t index); /* Retrieve a specific color value of the palette */ + bool_t (*adjustPalette)(gdispImage *img, uint16_t index, color_t newColor); /* Replace a color value in the palette */ } gdispImageHandlers; static gdispImageHandlers ImageHandlers[] = { @@ -183,6 +183,25 @@ delaytime_t gdispImageNext(gdispImage *img) { return img->fns->next(img); } +uint16_t gdispImageGetPaletteSize(gdispImage *img) { + if (!img->fns) return 0; + if (!img->fns->getPaletteSize) return 0; + return img->fns->getPaletteSize(img); +} + +color_t gdispImageGetPalette(gdispImage *img, uint16_t index) { + if (!img->fns) return 0; + if (!img->fns->getPalette) return 0; + return img->fns->getPalette(img, index); +} + +bool_t gdispImageAdjustPalette(gdispImage *img, uint16_t index, color_t newColor) { + if (!img->fns) return FALSE; + if (!img->fns->adjustPalette) return FALSE; + return img->fns->adjustPalette(img, index, newColor); +} + + // Helper Routines void *gdispImageAlloc(gdispImage *img, size_t sz) { #if GDISP_NEED_IMAGE_ACCOUNTING diff --git a/src/gdisp/gdisp_image.h b/src/gdisp/gdisp_image.h index a07a633d..41ed7531 100644 --- a/src/gdisp/gdisp_image.h +++ b/src/gdisp/gdisp_image.h @@ -247,10 +247,10 @@ extern "C" { * frame/page. */ delaytime_t gdispImageNext(gdispImage *img); - - int gdispImageGetPaletteSize(gdispImage *img); - color_t gdispImageGetPalette(gdispImage *img, int index); - bool_t gdispImageAdjustPalette(gdispImage *img, int index, color_t newColor); + + uint16_t gdispImageGetPaletteSize(gdispImage *img); + color_t gdispImageGetPalette(gdispImage *img, uint16_t index); + bool_t gdispImageAdjustPalette(gdispImage *img, uint16_t index, color_t newColor); #ifdef __cplusplus } diff --git a/src/gdisp/gdisp_image_bmp.c b/src/gdisp/gdisp_image_bmp.c index f8ac07ab..2ea4e95f 100644 --- a/src/gdisp/gdisp_image_bmp.c +++ b/src/gdisp/gdisp_image_bmp.c @@ -829,18 +829,16 @@ delaytime_t gdispImageNext_BMP(gdispImage *img) { return TIME_INFINITE; } -int gdispImageGetPaletteSize_BMP(gdispImage *img) { +uint16_t gdispImageGetPaletteSize_BMP(gdispImage *img) { #if GDISP_NEED_IMAGE_BMP_1 || GDISP_NEED_IMAGE_BMP_4 || GDISP_NEED_IMAGE_BMP_8 gdispImagePrivate_BMP *priv; priv = (gdispImagePrivate_BMP *)img->priv; - if (!priv) { + if (!priv) return 0; - } - if (!(priv->bmpflags & BMP_PALETTE)) { + if (!(priv->bmpflags & BMP_PALETTE)) return 0; - } return priv->palsize; #else @@ -848,48 +846,42 @@ int gdispImageGetPaletteSize_BMP(gdispImage *img) { #endif } -color_t gdispImageGetPalette_BMP(gdispImage *img, int index) { +color_t gdispImageGetPalette_BMP(gdispImage *img, uint16_t index) { #if GDISP_NEED_IMAGE_BMP_1 || GDISP_NEED_IMAGE_BMP_4 || GDISP_NEED_IMAGE_BMP_8 gdispImagePrivate_BMP *priv; priv = (gdispImagePrivate_BMP *)img->priv; - if (!priv) { + if (!priv) return 0; - } - if (!(priv->bmpflags & BMP_PALETTE)) { + if (!(priv->bmpflags & BMP_PALETTE)) return 0; - } - if (index < 0 || index >= priv->palsize) { + if (index >= priv->palsize) return 0; - } - return priv->palette[index]; + return priv->palette[(uint8_t)index]; #else return 0; #endif } -bool_t gdispImageAdjustPalette_BMP(gdispImage *img, int index, color_t newColor) { +bool_t gdispImageAdjustPalette_BMP(gdispImage *img, uint16_t index, color_t newColor) { #if GDISP_NEED_IMAGE_BMP_1 || GDISP_NEED_IMAGE_BMP_4 || GDISP_NEED_IMAGE_BMP_8 gdispImagePrivate_BMP *priv; priv = (gdispImagePrivate_BMP *)img->priv; - if (!priv) { + if (!priv) return FALSE; - } - if (!(priv->bmpflags & BMP_PALETTE)) { + if (!(priv->bmpflags & BMP_PALETTE)) return FALSE; - } - if (index < 0 || index >= priv->palsize) { + if (index >= priv->palsize) return FALSE; - } - priv->palette[index] = newColor; + priv->palette[(uint8_t)index] = newColor; return TRUE; -- cgit v1.2.3 From 73a110eed634e757389c647ad988fe750e9be0ee Mon Sep 17 00:00:00 2001 From: Joel Bodenmann Date: Fri, 11 Nov 2016 18:35:09 +0100 Subject: Adding API documentation for new image color palette functions --- src/gdisp/gdisp_image.h | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'src') diff --git a/src/gdisp/gdisp_image.h b/src/gdisp/gdisp_image.h index 41ed7531..76773887 100644 --- a/src/gdisp/gdisp_image.h +++ b/src/gdisp/gdisp_image.h @@ -248,8 +248,40 @@ extern "C" { */ delaytime_t gdispImageNext(gdispImage *img); + /** + * @brief Get the number of entries in the color palette. + * @return The number of entries in the color palette or 0 if the image doesn't use a color palette. + * + * @param[in] img The image structure + * + * @pre gdispImageOpen() must have returned successfully. + */ uint16_t gdispImageGetPaletteSize(gdispImage *img); + + /** + * @brief Get an entry in the color palette. + * @return The color value at a given position in the color palette. + * + * @param[in] img The image structure + * @param[in] index The index of the color palette entry + * + * @pre gdispImageOpen() must have returned successfully. + * + * @note This function will return 0 if the index is out of bounds or if the image doesn't use a color palette. + */ color_t gdispImageGetPalette(gdispImage *img, uint16_t index); + + /** + * @brief Modify an entry in the color palette. + * @return @p TRUE on success, @p FALSE otherwise. + * + * @param[in] img The image structure + * @param[in] index The index of the color palette entry + * @param[in] newColor The new color value of the specified entry + * + * @pre gdispImageOpen() must have returned successfully. + * @note This function will return @p FALSE if the index is out of bounds or if the image doesn't use a color palette. + */ bool_t gdispImageAdjustPalette(gdispImage *img, uint16_t index, color_t newColor); #ifdef __cplusplus -- cgit v1.2.3