From a7a6b41e6053137d174b4c3cb540345a64c05644 Mon Sep 17 00:00:00 2001 From: dborth Date: Tue, 29 Jun 2010 07:50:37 +0000 Subject: [PATCH] sync with snes9xgx changes --- source/gui/gui_imagedata.cpp | 2 +- source/utils/pngu.c | 95 +++++++++++++++++++++++++----------- source/utils/pngu.h | 39 +++++++-------- 3 files changed, 85 insertions(+), 51 deletions(-) diff --git a/source/gui/gui_imagedata.cpp b/source/gui/gui_imagedata.cpp index 41d357c..97e7513 100644 --- a/source/gui/gui_imagedata.cpp +++ b/source/gui/gui_imagedata.cpp @@ -20,7 +20,7 @@ GuiImageData::GuiImageData(const u8 * i, int maxw, int maxh) height = 0; if(i) - data = DecodePNG(i, &width, &height, maxw, maxh); + data = DecodePNG(i, &width, &height, data, maxw, maxh); } /** diff --git a/source/utils/pngu.c b/source/utils/pngu.c index 57408f5..127017d 100644 --- a/source/utils/pngu.c +++ b/source/utils/pngu.c @@ -1,15 +1,14 @@ -/******************************************************************************************** -* -* PNGU -* -* Original author: frontier (http://frontier-dev.net) -* Modified by Tantric, 2009-2010 -* -********************************************************************************************/ +/**************************************************************************** + * + * PNGU + * + * Original author: frontier (http://frontier-dev.net) + * Modified by Tantric, 2009-2010 + * + ***************************************************************************/ #include #include -#include #include "pngu.h" #include @@ -43,12 +42,12 @@ struct _IMGCTX int source; void *buffer; char *filename; - PNGU_u32 cursor; + u32 cursor; - PNGU_u32 propRead; + u32 propRead; PNGUPROP prop; - PNGU_u32 infoRead; + u32 infoRead; png_structp png_ptr; png_infop info_ptr; FILE *fd; @@ -294,7 +293,7 @@ static int pngu_info (IMGCTX ctx) return PNGU_OK; } -static int pngu_decode (IMGCTX ctx, PNGU_u32 width, PNGU_u32 height, PNGU_u32 stripAlpha) +static int pngu_decode (IMGCTX ctx, u32 width, u32 height, u32 stripAlpha) { png_uint_32 rowbytes; png_uint_32 i, propImgHeight; @@ -369,14 +368,14 @@ static int pngu_decode (IMGCTX ctx, PNGU_u32 width, PNGU_u32 height, PNGU_u32 st return PNGU_OK; } -static inline PNGU_u32 coordsRGBA8(PNGU_u32 x, PNGU_u32 y, PNGU_u32 w) +static inline u32 coordsRGBA8(u32 x, u32 y, u32 w) { return ((((y >> 2) * (w >> 2) + (x >> 2)) << 5) + ((y & 3) << 2) + (x & 3)) << 1; } -static u8 * PNGU_DecodeTo4x4RGBA8 (IMGCTX ctx, PNGU_u32 width, PNGU_u32 height, int * dstWidth, int * dstHeight, int maxWidth, int maxHeight) +static u8 * PNGU_DecodeTo4x4RGBA8 (IMGCTX ctx, u32 width, u32 height, int * dstWidth, int * dstHeight, u8 *dstPtr, int maxWidth, int maxHeight) { - PNGU_u8 default_alpha = 255; + u8 default_alpha = 255; u8 *dst; int x, y, x2, y2, offset; int xRatio = 0, yRatio = 0; @@ -412,7 +411,10 @@ static u8 * PNGU_DecodeTo4x4RGBA8 (IMGCTX ctx, PNGU_u32 width, PNGU_u32 height, int len = (padWidth * padHeight) << 2; if(len%32) len += (32-len%32); - dst = memalign (32, len); + if(dstPtr) + dst = dstPtr; // use existing allocation + else + dst = memalign (32, len); if(!dst) return NULL; @@ -557,7 +559,7 @@ int PNGU_GetImageProperties (IMGCTX ctx, PNGUPROP *imgprop) return PNGU_OK; } -PNGU_u8 * DecodePNG(const PNGU_u8 *src, int * width, int * height, int maxwidth, int maxheight) +u8 * DecodePNG(const u8 *src, int * width, int * height, u8 *dstPtr, int maxwidth, int maxheight) { PNGUPROP imgProp; IMGCTX ctx = PNGU_SelectImageFromBuffer(src); @@ -567,16 +569,16 @@ PNGU_u8 * DecodePNG(const PNGU_u8 *src, int * width, int * height, int maxwidth, return NULL; if(PNGU_GetImageProperties(ctx, &imgProp) == PNGU_OK) - dst = PNGU_DecodeTo4x4RGBA8 (ctx, imgProp.imgWidth, imgProp.imgHeight, width, height, maxwidth, maxheight); + dst = PNGU_DecodeTo4x4RGBA8 (ctx, imgProp.imgWidth, imgProp.imgHeight, width, height, dstPtr, maxwidth, maxheight); PNGU_ReleaseImageContext (ctx); return dst; } -int PNGU_EncodeFromRGB (IMGCTX ctx, PNGU_u32 width, PNGU_u32 height, void *buffer, PNGU_u32 stride) +int PNGU_EncodeFromRGB (IMGCTX ctx, u32 width, u32 height, void *buffer, u32 stride) { png_uint_32 rowbytes; - PNGU_u32 y; + u32 y; // Erase from the context any readed info pngu_free_info (ctx); @@ -633,10 +635,9 @@ int PNGU_EncodeFromRGB (IMGCTX ctx, PNGU_u32 width, PNGU_u32 height, void *buffe rowbytes = width * 3; if (rowbytes % 4) rowbytes = ((rowbytes >>2) + 1) <<2; // Add extra padding so each row starts in a 4 byte boundary - + ctx->img_data = malloc(rowbytes * height); - memset(ctx->img_data, 0, rowbytes * height); - + if (!ctx->img_data) { png_destroy_write_struct (&(ctx->png_ptr), (png_infopp)NULL); @@ -645,9 +646,9 @@ int PNGU_EncodeFromRGB (IMGCTX ctx, PNGU_u32 width, PNGU_u32 height, void *buffe return PNGU_LIB_ERROR; } + memset(ctx->img_data, 0, rowbytes * height); ctx->row_pointers = malloc (sizeof (png_bytep) * height); - memset(ctx->row_pointers, 0, sizeof (png_bytep) * height); - + if (!ctx->row_pointers) { png_destroy_write_struct (&(ctx->png_ptr), (png_infopp)NULL); @@ -656,6 +657,8 @@ int PNGU_EncodeFromRGB (IMGCTX ctx, PNGU_u32 width, PNGU_u32 height, void *buffe return PNGU_LIB_ERROR; } + memset(ctx->row_pointers, 0, sizeof (png_bytep) * height); + for (y = 0; y < height; ++y) { ctx->row_pointers[y] = buffer + (y * rowbytes); @@ -681,13 +684,17 @@ int PNGU_EncodeFromRGB (IMGCTX ctx, PNGU_u32 width, PNGU_u32 height, void *buffe return ctx->cursor; } -int PNGU_EncodeFromGXTexture (IMGCTX ctx, PNGU_u32 width, PNGU_u32 height, void *buffer, PNGU_u32 stride) +int PNGU_EncodeFromGXTexture (IMGCTX ctx, u32 width, u32 height, void *buffer, u32 stride) { int res; - PNGU_u32 x,y, tmpy1, tmpy2, tmpyWid, tmpxy; + u32 x, y, tmpy1, tmpy2, tmpyWid, tmpxy; unsigned char * ptr = (unsigned char*)buffer; - unsigned char * tmpbuffer = (unsigned char *)malloc(width*height*3); + unsigned char * tmpbuffer = malloc(width*height*3); + + if(!tmpbuffer) + return PNGU_LIB_ERROR; + memset(tmpbuffer, 0, width*height*3); png_uint_32 offset; @@ -712,3 +719,33 @@ int PNGU_EncodeFromGXTexture (IMGCTX ctx, PNGU_u32 width, PNGU_u32 height, void free(tmpbuffer); return res; } + +int PNGU_EncodeFromEFB (IMGCTX ctx, u32 width, u32 height) +{ + int res; + u32 x, y, tmpy1, tmpxy; + GXColor color; + + unsigned char * tmpbuffer = malloc(width*height*3); + + if(!tmpbuffer) + return PNGU_LIB_ERROR; + + for(y=0; y < height; y++) + { + tmpy1 = y * width * 3; + + for(x=0; x < width; x++) + { + tmpxy = x * 3 + tmpy1; + GX_PeekARGB(x, y, &color); + tmpbuffer[tmpxy ] = color.r; // R + tmpbuffer[tmpxy+1] = color.g; // G + tmpbuffer[tmpxy+2] = color.b; // B + } + } + + res = PNGU_EncodeFromRGB (ctx, width, height, tmpbuffer, 0); + free(tmpbuffer); + return res; +} diff --git a/source/utils/pngu.h b/source/utils/pngu.h index 8024b96..fa89650 100644 --- a/source/utils/pngu.h +++ b/source/utils/pngu.h @@ -10,32 +10,28 @@ #ifndef __PNGU__ #define __PNGU__ +#include + #ifdef __cplusplus extern "C" { #endif -// Types -typedef unsigned char PNGU_u8; -typedef unsigned short PNGU_u16; -typedef unsigned int PNGU_u32; -typedef unsigned long long PNGU_u64; - typedef struct { - PNGU_u8 r; - PNGU_u8 g; - PNGU_u8 b; + u8 r; + u8 g; + u8 b; } PNGUCOLOR; typedef struct { - PNGU_u32 imgWidth; // In pixels - PNGU_u32 imgHeight; // In pixels - PNGU_u32 imgBitDepth; // In bitx - PNGU_u32 imgColorType; // PNGU_COLOR_TYPE_* - PNGU_u32 validBckgrnd; // Non zero if there is a background color - PNGUCOLOR bckgrnd; // Backgroun color - PNGU_u32 numTrans; // Number of transparent colors + u32 imgWidth; // In pixels + u32 imgHeight; // In pixels + u32 imgBitDepth; // In bitx + u32 imgColorType; // PNGU_COLOR_TYPE_* + u32 validBckgrnd; // Non zero if there is a background color + PNGUCOLOR bckgrnd; // Background color + u32 numTrans; // Number of transparent colors PNGUCOLOR *trans; // Transparent colors } PNGUPROP; @@ -47,10 +43,10 @@ typedef struct _IMGCTX *IMGCTX; * Image context handling * ****************************************************************************/ -// Selects a PNG file, previosly loaded into a buffer, and creates an image context for subsequent procesing. +// Selects a PNG file, previously loaded into a buffer, and creates an image context for subsequent processing. IMGCTX PNGU_SelectImageFromBuffer (const void *buffer); -// Selects a PNG file, from any devoptab device, and creates an image context for subsequent procesing. +// Selects a PNG file, from any devoptab device, and creates an image context for subsequent processing. IMGCTX PNGU_SelectImageFromDevice (const char *filename); // Frees resources associated with an image context. Always call this function when you no longer need the IMGCTX. @@ -67,9 +63,10 @@ int PNGU_GetImageProperties (IMGCTX ctx, PNGUPROP *fileproperties); * Image conversion * ****************************************************************************/ -PNGU_u8 * DecodePNG(const PNGU_u8 *src, int *width, int *height, int maxwidth, int maxheight); -int PNGU_EncodeFromRGB (IMGCTX ctx, PNGU_u32 width, PNGU_u32 height, void *buffer, PNGU_u32 stride); -int PNGU_EncodeFromGXTexture (IMGCTX ctx, PNGU_u32 width, PNGU_u32 height, void *buffer, PNGU_u32 stride); +u8 * DecodePNG(const u8 *src, int *width, int *height, u8 *dst, int maxwidth, int maxheight); +int PNGU_EncodeFromRGB (IMGCTX ctx, u32 width, u32 height, void *buffer, u32 stride); +int PNGU_EncodeFromGXTexture (IMGCTX ctx, u32 width, u32 height, void *buffer, u32 stride); +int PNGU_EncodeFromEFB (IMGCTX ctx, u32 width, u32 height); #ifdef __cplusplus }