From 7a87643e0702bf156527e066c2a963b6db2b4fa5 Mon Sep 17 00:00:00 2001 From: notaz Date: Wed, 21 May 2008 19:05:55 +0000 Subject: [PATCH] error checking for png git-svn-id: file:///home/notaz/opt/svn/PicoDrive@451 be3aeb3a-fb24-0410-a615-afba39da0efa --- platform/common/emu.c | 2 +- platform/common/readpng.c | 38 ++++++++++++++++++++++++++++++++++---- platform/common/readpng.h | 13 +++++++++++-- 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/platform/common/emu.c b/platform/common/emu.c index e88859b6..0b8fdf72 100644 --- a/platform/common/emu.c +++ b/platform/common/emu.c @@ -224,7 +224,7 @@ int emu_cdCheck(int *pregion) return type; } -static int extract_text(char *dest, unsigned char *src, int len, int swab) +static int extract_text(char *dest, const unsigned char *src, int len, int swab) { char *p = dest; int i; diff --git a/platform/common/readpng.c b/platform/common/readpng.c index c93b36fd..a437a526 100644 --- a/platform/common/readpng.c +++ b/platform/common/readpng.c @@ -12,23 +12,24 @@ #define BG_HEIGHT 240 #endif -void readpng(void *dest, const char *fname, readpng_what what) +int readpng(void *dest, const char *fname, readpng_what what) { FILE *fp; png_structp png_ptr = NULL; png_infop info_ptr = NULL; png_bytepp row_ptr = NULL; + int ret = -1; if (dest == NULL || fname == NULL) { - return; + return -1; } fp = fopen(fname, "rb"); if (fp == NULL) { lprintf(__FILE__ ": failed to open: %s\n", fname); - return; + return -1; } png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); @@ -36,7 +37,7 @@ void readpng(void *dest, const char *fname, readpng_what what) { lprintf(__FILE__ ": png_create_read_struct() failed\n"); fclose(fp); - return; + return -1; } info_ptr = png_create_info_struct(png_ptr); @@ -145,12 +146,41 @@ void readpng(void *dest, const char *fname, readpng_what what) } break; } + + case READPNG_320_24: + case READPNG_480_24: + { + int height, width, h; + int needw = (what == READPNG_480_24) ? 480 : 320; + unsigned char *dst = dest; + if (info_ptr->pixel_depth != 24) + { + lprintf(__FILE__ ": image uses %ibpp, needed 24bpp\n", info_ptr->pixel_depth); + break; + } + height = info_ptr->height; + if (height > 240) height = 240; + width = info_ptr->width; + if (width > needw) width = needw; + + for (h = 0; h < height; h++) + { + int len = width; + unsigned char *src = row_ptr[h]; + dst += (needw - width) * 3; + for (len = width; len > 0; len--, dst+=3, src+=3) + dst[0] = src[2], dst[1] = src[1], dst[2] = src[0]; + } + break; + } } + ret = 0; done: png_destroy_read_struct(&png_ptr, info_ptr ? &info_ptr : NULL, (png_infopp)NULL); fclose(fp); + return ret; } diff --git a/platform/common/readpng.h b/platform/common/readpng.h index 542a7747..7d74fee2 100644 --- a/platform/common/readpng.h +++ b/platform/common/readpng.h @@ -2,9 +2,18 @@ typedef enum { READPNG_BG = 1, READPNG_FONT, - READPNG_SELECTOR + READPNG_SELECTOR, + READPNG_320_24, + READPNG_480_24 } readpng_what; -void readpng(void *dest, const char *fname, readpng_what what); +#ifdef __cplusplus +extern "C" { +#endif +int readpng(void *dest, const char *fname, readpng_what what); + +#ifdef __cplusplus +} +#endif -- 2.39.5