Adding nullpointer checks to GDISP image functions

release/v2.9
Joel Bodenmann 2017-10-01 13:21:56 +02:00
parent 82047b1ac6
commit ea158a836d
3 changed files with 31 additions and 18 deletions

View File

@ -60,6 +60,7 @@ FEATURE: Added AHTML2COLOR() and ARGB2COLOR() to support alpha. This is currentl
FEATURE: Added the new color GFXTRANSPARENT - only available for RGB888 pixel format on alpha capable displays. FEATURE: Added the new color GFXTRANSPARENT - only available for RGB888 pixel format on alpha capable displays.
NOTE: Alpha support in RGB888 requies an alpha capable display (STM32LTDC 2nd display only currently) NOTE: Alpha support in RGB888 requies an alpha capable display (STM32LTDC 2nd display only currently)
NOTE: Alpha support in RGB888 is NOT the standard ARGB8888 format. Only use AHTML2COLOR() and ARGB2COLOR() to create alpha colors. NOTE: Alpha support in RGB888 is NOT the standard ARGB8888 format. Only use AHTML2COLOR() and ARGB2COLOR() to create alpha colors.
FEATURE: Added nullpointer checks to GDISP image functions (with new error code GDISP_IMAGE_ERR_NULLPOINTER)
*** Release 2.7 *** *** Release 2.7 ***

View File

@ -110,6 +110,8 @@ void gdispImageInit(gdispImage *img) {
gdispImageError gdispImageOpenGFile(gdispImage *img, GFILE *f) { gdispImageError gdispImageOpenGFile(gdispImage *img, GFILE *f) {
gdispImageError err; gdispImageError err;
if (!img)
return GDISP_IMAGE_ERR_NULLPOINTER;
if (!f) if (!f)
return GDISP_IMAGE_ERR_NOSUCHFILE; return GDISP_IMAGE_ERR_NOSUCHFILE;
img->f = f; img->f = f;
@ -141,6 +143,8 @@ unrecoverable:
} }
void gdispImageClose(gdispImage *img) { void gdispImageClose(gdispImage *img) {
if (!img)
return;
if (img->fns) if (img->fns)
img->fns->close(img); img->fns->close(img);
gfileClose(img->f); gfileClose(img->f);
@ -151,19 +155,25 @@ void gdispImageClose(gdispImage *img) {
} }
bool_t gdispImageIsOpen(gdispImage *img) { bool_t gdispImageIsOpen(gdispImage *img) {
if (!img)
return FALSE;
return img->type != GDISP_IMAGE_TYPE_UNKNOWN && img->fns != 0; return img->type != GDISP_IMAGE_TYPE_UNKNOWN && img->fns != 0;
} }
void gdispImageSetBgColor(gdispImage *img, color_t bgcolor) { void gdispImageSetBgColor(gdispImage *img, color_t bgcolor) {
if (!img)
return;
img->bgcolor = bgcolor; img->bgcolor = bgcolor;
} }
gdispImageError gdispImageCache(gdispImage *img) { gdispImageError gdispImageCache(gdispImage *img) {
if (!img) return GDISP_IMAGE_ERR_NULLPOINTER;
if (!img->fns) return GDISP_IMAGE_ERR_BADFORMAT; if (!img->fns) return GDISP_IMAGE_ERR_BADFORMAT;
return img->fns->cache(img); return img->fns->cache(img);
} }
gdispImageError gdispGImageDraw(GDisplay *g, gdispImage *img, coord_t x, coord_t y, coord_t cx, coord_t cy, coord_t sx, coord_t sy) { gdispImageError gdispGImageDraw(GDisplay *g, gdispImage *img, coord_t x, coord_t y, coord_t cx, coord_t cy, coord_t sx, coord_t sy) {
if (!img) return GDISP_IMAGE_ERR_NULLPOINTER;
if (!img->fns) return GDISP_IMAGE_ERR_BADFORMAT; if (!img->fns) return GDISP_IMAGE_ERR_BADFORMAT;
// Check on window // Check on window
@ -179,24 +189,25 @@ gdispImageError gdispGImageDraw(GDisplay *g, gdispImage *img, coord_t x, coord_t
} }
delaytime_t gdispImageNext(gdispImage *img) { delaytime_t gdispImageNext(gdispImage *img) {
if (!img) return GDISP_IMAGE_ERR_NULLPOINTER;
if (!img->fns) return GDISP_IMAGE_ERR_BADFORMAT; if (!img->fns) return GDISP_IMAGE_ERR_BADFORMAT;
return img->fns->next(img); return img->fns->next(img);
} }
uint16_t gdispImageGetPaletteSize(gdispImage *img) { uint16_t gdispImageGetPaletteSize(gdispImage *img) {
if (!img->fns) return 0; if (!img || !img->fns) return 0;
if (!img->fns->getPaletteSize) return 0; if (!img->fns->getPaletteSize) return 0;
return img->fns->getPaletteSize(img); return img->fns->getPaletteSize(img);
} }
color_t gdispImageGetPalette(gdispImage *img, uint16_t index) { color_t gdispImageGetPalette(gdispImage *img, uint16_t index) {
if (!img->fns) return 0; if (!img || !img->fns) return 0;
if (!img->fns->getPalette) return 0; if (!img->fns->getPalette) return 0;
return img->fns->getPalette(img, index); return img->fns->getPalette(img, index);
} }
bool_t gdispImageAdjustPalette(gdispImage *img, uint16_t index, color_t newColor) { bool_t gdispImageAdjustPalette(gdispImage *img, uint16_t index, color_t newColor) {
if (!img->fns) return FALSE; if (!img || !img->fns) return FALSE;
if (!img->fns->adjustPalette) return FALSE; if (!img->fns->adjustPalette) return FALSE;
return img->fns->adjustPalette(img, index, newColor); return img->fns->adjustPalette(img, index, newColor);
} }

View File

@ -43,6 +43,7 @@ typedef uint16_t gdispImageError;
#define GDISP_IMAGE_ERR_UNSUPPORTED_OK 3 #define GDISP_IMAGE_ERR_UNSUPPORTED_OK 3
#define GDISP_IMAGE_ERR_NOMEMORY (GDISP_IMAGE_ERR_UNRECOVERABLE+4) #define GDISP_IMAGE_ERR_NOMEMORY (GDISP_IMAGE_ERR_UNRECOVERABLE+4)
#define GDISP_IMAGE_ERR_NOSUCHFILE (GDISP_IMAGE_ERR_UNRECOVERABLE+5) #define GDISP_IMAGE_ERR_NOSUCHFILE (GDISP_IMAGE_ERR_UNRECOVERABLE+5)
#define GDISP_IMAGE_ERR_NULLPOINTER (GDISP_IMAGE_ERR_UNRECOVERABLE+6)
/** /**
* @brief Image flags * @brief Image flags
@ -68,7 +69,7 @@ typedef struct gdispImage {
const struct gdispImageHandlers * fns; /* @< Don't mess with this! */ const struct gdispImageHandlers * fns; /* @< Don't mess with this! */
void * priv; /* @< Don't mess with this! */ void * priv; /* @< Don't mess with this! */
} gdispImage; } gdispImage;
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
@ -90,7 +91,7 @@ extern "C" {
* @param[in] f The open GFILE stream. * @param[in] f The open GFILE stream.
* *
* @pre The GFILE must be open for reading. * @pre The GFILE must be open for reading.
* *
* @note This determines which decoder to use and then initialises all other fields * @note This determines which decoder to use and then initialises all other fields
* in the gdispImage structure. * in the gdispImage structure.
* @note The image background color is set to White. * @note The image background color is set to White.
@ -152,9 +153,9 @@ extern "C" {
/** /**
* @brief Close an image and release any dynamically allocated working storage. * @brief Close an image and release any dynamically allocated working storage.
* *
* @param[in] img The image structure * @param[in] img The image structure
* *
* @pre gdispImageOpenFile() must have returned successfully. * @pre gdispImageOpenFile() must have returned successfully.
* *
* @note Also calls the IO close function (if it hasn't already been called). * @note Also calls the IO close function (if it hasn't already been called).
@ -186,7 +187,7 @@ extern "C" {
* continuing with drawing that includes transparency eg some GIF animations. * continuing with drawing that includes transparency eg some GIF animations.
*/ */
void gdispImageSetBgColor(gdispImage *img, color_t bgcolor); void gdispImageSetBgColor(gdispImage *img, color_t bgcolor);
/** /**
* @brief Cache the image * @brief Cache the image
* @details Decodes and caches the current frame into RAM. * @details Decodes and caches the current frame into RAM.
@ -208,13 +209,13 @@ extern "C" {
/** /**
* @brief Draw the image * @brief Draw the image
* @return GDISP_IMAGE_ERR_OK (0) on success or an error code. * @return GDISP_IMAGE_ERR_OK (0) on success or an error code.
* *
* @param[in] g The display to draw on * @param[in] g The display to draw on
* @param[in] img The image structure * @param[in] img The image structure
* @param[in] x,y The screen location to draw the image * @param[in] x,y The screen location to draw the image
* @param[in] cx,cy The area on the screen to draw * @param[in] cx,cy The area on the screen to draw
* @param[in] sx,sy The image position to start drawing at * @param[in] sx,sy The image position to start drawing at
* *
* @pre gdispImageOpen() must have returned successfully. * @pre gdispImageOpen() must have returned successfully.
* *
* @note If sx,sy + cx,cy is outside the image boundaries the area outside the image * @note If sx,sy + cx,cy is outside the image boundaries the area outside the image
@ -231,9 +232,9 @@ extern "C" {
* @brief Prepare for the next frame/page in the image file. * @brief Prepare for the next frame/page in the image file.
* @return A time in milliseconds to keep displaying the current frame before trying to draw * @return A time in milliseconds to keep displaying the current frame before trying to draw
* the next frame. Watch out for the special values TIME_IMMEDIATE and TIME_INFINITE. * the next frame. Watch out for the special values TIME_IMMEDIATE and TIME_INFINITE.
* *
* @param[in] img The image structure * @param[in] img The image structure
* *
* @pre gdispImageOpen() must have returned successfully. * @pre gdispImageOpen() must have returned successfully.
* *
* @note It will return TIME_IMMEDIATE if the first frame/page hasn't been drawn or if the next frame * @note It will return TIME_IMMEDIATE if the first frame/page hasn't been drawn or if the next frame
@ -253,24 +254,24 @@ extern "C" {
* @return The number of entries in the color palette or 0 if the image doesn't use a 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 * @param[in] img The image structure
* *
* @pre gdispImageOpen() must have returned successfully. * @pre gdispImageOpen() must have returned successfully.
*/ */
uint16_t gdispImageGetPaletteSize(gdispImage *img); uint16_t gdispImageGetPaletteSize(gdispImage *img);
/** /**
* @brief Get an entry in the color palette. * @brief Get an entry in the color palette.
* @return The color value at a given position in the color palette. * @return The color value at a given position in the color palette.
* *
* @param[in] img The image structure * @param[in] img The image structure
* @param[in] index The index of the color palette entry * @param[in] index The index of the color palette entry
* *
* @pre gdispImageOpen() must have returned successfully. * @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. * @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); color_t gdispImageGetPalette(gdispImage *img, uint16_t index);
/** /**
* @brief Modify an entry in the color palette. * @brief Modify an entry in the color palette.
* @return @p TRUE on success, @p FALSE otherwise. * @return @p TRUE on success, @p FALSE otherwise.
@ -278,12 +279,12 @@ extern "C" {
* @param[in] img The image structure * @param[in] img The image structure
* @param[in] index The index of the color palette entry * @param[in] index The index of the color palette entry
* @param[in] newColor The new color value of the specified entry * @param[in] newColor The new color value of the specified entry
* *
* @pre gdispImageOpen() must have returned successfully. * @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. * @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); bool_t gdispImageAdjustPalette(gdispImage *img, uint16_t index, color_t newColor);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif