X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=common%2Freadpng.c;h=cad121704998307c171a576a8aa5b45e9ace1f72;hb=e99d80480196f551f7e6c06deae3750de19b4c0e;hp=a437a526276da8a1ba9ec1493e27caa302cef50b;hpb=ff63afa1b6f6f291d466a870b8e6d25fe9a74296;p=libpicofe.git diff --git a/common/readpng.c b/common/readpng.c index a437a52..cad1217 100644 --- a/common/readpng.c +++ b/common/readpng.c @@ -1,18 +1,21 @@ +/* + * (C) Gražvydas "notaz" Ignotas, 2008-2011 + * + * This work is licensed under the terms of any of these licenses + * (at your option): + * - GNU GPL, version 2 or later. + * - GNU LGPL, version 2.1 or later. + * See the COPYING file in the top-level directory. + */ + #include +#include #include #include #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 - -int 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; @@ -71,9 +74,11 @@ int 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++) { @@ -88,7 +93,7 @@ int readpng(void *dest, const char *fname, readpng_what what) #endif src += 3; } - dst += BG_WIDTH - width; + dst += req_w - width; } break; } @@ -97,10 +102,10 @@ int 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) @@ -112,10 +117,13 @@ int 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); } } @@ -127,10 +135,10 @@ int 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) @@ -138,20 +146,18 @@ int 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_320_24: - case READPNG_480_24: + case READPNG_24: { int height, width, h; - int needw = (what == READPNG_480_24) ? 480 : 320; unsigned char *dst = dest; if (info_ptr->pixel_depth != 24) { @@ -159,15 +165,17 @@ int readpng(void *dest, const char *fname, readpng_what what) break; } height = info_ptr->height; - if (height > 240) height = 240; + if (height > req_h) + height = req_h; width = info_ptr->width; - if (width > needw) width = needw; + if (width > req_w) + width = req_w; for (h = 0; h < height; h++) { int len = width; unsigned char *src = row_ptr[h]; - dst += (needw - width) * 3; + 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]; } @@ -183,4 +191,76 @@ done: return ret; } +int writepng(const char *fname, unsigned short *src, int w, int h) +{ + png_structp png_ptr = NULL; + png_infop info_ptr = NULL; + png_bytepp row_pointers; + int i, j, ret = -1; + FILE *f; + + f = fopen(fname, "wb"); + if (f == NULL) { + lprintf(__FILE__ ": failed to open \"%s\"\n", fname); + return -1; + } + + row_pointers = calloc(h, sizeof(row_pointers[0])); + if (row_pointers == NULL) + goto end1; + + for (i = 0; i < h; i++) { + unsigned char *dst = malloc(w * 3); + if (dst == NULL) + goto end2; + row_pointers[i] = dst; + for (j = 0; j < w; j++, src++, dst += 3) { + dst[0] = (*src & 0xf800) >> 8; + dst[1] = (*src & 0x07e0) >> 3; + dst[2] = (*src & 0x001f) << 3; + } + } + + /* initialize stuff */ + png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); + if (png_ptr == NULL) { + fprintf(stderr, "png_create_write_struct() failed"); + goto end2; + } + + info_ptr = png_create_info_struct(png_ptr); + if (info_ptr == NULL) { + fprintf(stderr, "png_create_info_struct() failed"); + goto end3; + } + + if (setjmp(png_jmpbuf(png_ptr)) != 0) { + fprintf(stderr, "error in png code\n"); + goto end4; + } + + png_init_io(png_ptr, f); + + png_set_IHDR(png_ptr, info_ptr, w, h, + 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, + PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); + + png_write_info(png_ptr, info_ptr); + png_write_image(png_ptr, row_pointers); + png_write_end(png_ptr, NULL); + + ret = 0; + +end4: +// png_destroy_info_struct(png_ptr, &info_ptr); // freed below +end3: + png_destroy_write_struct(&png_ptr, &info_ptr); +end2: + for (i = 0; i < h; i++) + free(row_pointers[i]); + free(row_pointers); +end1: + fclose(f); + return ret; +}