X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=common%2Freadpng.c;h=a437a526276da8a1ba9ec1493e27caa302cef50b;hb=93c18cb44bf9794c7c9bc93411c68880723320d1;hp=2a7466882edcabfd2b1ff0aad38e18098657a35d;hpb=c7a4ff64287b12487c7e9cc13ce3b7d2aa6e1f06;p=libpicofe.git diff --git a/common/readpng.c b/common/readpng.c index 2a74668..a437a52 100644 --- a/common/readpng.c +++ b/common/readpng.c @@ -4,23 +4,32 @@ #include "readpng.h" #include "lprintf.h" -void readpng(void *dest, const char *fname, readpng_what what) +#ifdef PSP +#define BG_WIDTH 480 +#define BG_HEIGHT 272 +#else +#define BG_WIDTH 320 +#define BG_HEIGHT 240 +#endif + +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); @@ -28,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); @@ -62,9 +71,9 @@ void readpng(void *dest, const char *fname, readpng_what what) break; } height = info_ptr->height; - if (height > 240) height = 240; + if (height > BG_HEIGHT) height = BG_HEIGHT; width = info_ptr->width; - if (width > 320) width = 320; + if (width > BG_WIDTH) width = BG_WIDTH; for (h = 0; h < height; h++) { @@ -72,10 +81,14 @@ void readpng(void *dest, const char *fname, readpng_what what) int len = width; while (len--) { - *dst++ = ((src[0]&0xf8)<<8) | ((src[1]&0xf8)<<3) | (src[2] >> 3); +#ifdef PSP + *dst++ = ((src[2]&0xf8)<<8) | ((src[1]&0xf8)<<3) | (src[0] >> 3); // BGR +#else + *dst++ = ((src[0]&0xf8)<<8) | ((src[1]&0xf8)<<3) | (src[2] >> 3); // RGB +#endif src += 3; } - dst += 320 - width; + dst += BG_WIDTH - width; } break; } @@ -133,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; }