X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=common%2Freadpng.c;h=50e883484f6627a994d7a405b286a1f90df70da1;hb=f6eaae4f09c6abab99692900a31c1df2a06b99af;hp=c93b36fd86a5b12965407e68fa4e7ac3c797c839;hpb=703e4c7bbb2cfd549797e2092e4d863547b3e87c;p=libpicofe.git diff --git a/common/readpng.c b/common/readpng.c index c93b36f..50e8834 100644 --- a/common/readpng.c +++ b/common/readpng.c @@ -4,31 +4,24 @@ #include "readpng.h" #include "lprintf.h" -#ifdef PSP -#define BG_WIDTH 480 -#define BG_HEIGHT 272 -#else -#define BG_WIDTH 320 -#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, int req_w, int req_h) { 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 +29,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); @@ -70,9 +63,11 @@ void readpng(void *dest, const char *fname, readpng_what what) break; } height = info_ptr->height; - if (height > BG_HEIGHT) height = BG_HEIGHT; + if (height > req_h) + height = req_h; width = info_ptr->width; - if (width > BG_WIDTH) width = BG_WIDTH; + if (width > req_w) + width = req_w; for (h = 0; h < height; h++) { @@ -87,7 +82,7 @@ void readpng(void *dest, const char *fname, readpng_what what) #endif src += 3; } - dst += BG_WIDTH - width; + dst += req_w - width; } break; } @@ -96,10 +91,10 @@ void readpng(void *dest, const char *fname, readpng_what what) { int x, y, x1, y1; unsigned char *dst = dest; - if (info_ptr->width != 128 || info_ptr->height != 160) + if (info_ptr->width != req_w || info_ptr->height != req_h) { - lprintf(__FILE__ ": unexpected font image size %ix%i, needed 128x160\n", - (int)info_ptr->width, (int)info_ptr->height); + lprintf(__FILE__ ": unexpected font image size %dx%d, needed %dx%d\n", + (int)info_ptr->width, (int)info_ptr->height, req_w, req_h); break; } if (info_ptr->pixel_depth != 8) @@ -111,10 +106,13 @@ void readpng(void *dest, const char *fname, readpng_what what) { for (x = 0; x < 16; x++) { - for (y1 = 0; y1 < 10; y1++) + /* 16x16 grid of syms */ + int sym_w = req_w / 16; + int sym_h = req_h / 16; + for (y1 = 0; y1 < sym_h; y1++) { - unsigned char *src = row_ptr[y*10 + y1] + x*8; - for (x1 = 8/2; x1 > 0; x1--, src+=2) + unsigned char *src = row_ptr[y*sym_h + y1] + x*sym_w; + for (x1 = sym_w/2; x1 > 0; x1--, src+=2) *dst++ = ((src[0]^0xff) & 0xf0) | ((src[1]^0xff) >> 4); } } @@ -126,10 +124,10 @@ void readpng(void *dest, const char *fname, readpng_what what) { int x1, y1; unsigned char *dst = dest; - if (info_ptr->width != 8 || info_ptr->height != 10) + if (info_ptr->width != req_w || info_ptr->height != req_h) { - lprintf(__FILE__ ": unexpected selector image size %ix%i, needed 8x10\n", - (int)info_ptr->width, (int)info_ptr->height); + lprintf(__FILE__ ": unexpected selector image size %ix%i, needed %dx%d\n", + (int)info_ptr->width, (int)info_ptr->height, req_w, req_h); break; } if (info_ptr->pixel_depth != 8) @@ -137,20 +135,49 @@ void readpng(void *dest, const char *fname, readpng_what what) lprintf(__FILE__ ": selector image uses %ibpp, needed 8bpp\n", info_ptr->pixel_depth); break; } - for (y1 = 0; y1 < 10; y1++) + for (y1 = 0; y1 < req_h; y1++) { unsigned char *src = row_ptr[y1]; - for (x1 = 8/2; x1 > 0; x1--, src+=2) + for (x1 = req_w/2; x1 > 0; x1--, src+=2) *dst++ = ((src[0]^0xff) & 0xf0) | ((src[1]^0xff) >> 4); } break; } + + case READPNG_24: + { + int height, width, h; + 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 > req_h) + height = req_h; + width = info_ptr->width; + if (width > req_w) + width = req_w; + + for (h = 0; h < height; h++) + { + int len = width; + unsigned char *src = row_ptr[h]; + dst += (req_w - 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; }