sync with snes9xgx changes

This commit is contained in:
dborth 2010-06-29 07:50:37 +00:00
parent a5c83a32fd
commit a7a6b41e60
3 changed files with 85 additions and 51 deletions

View File

@ -20,7 +20,7 @@ GuiImageData::GuiImageData(const u8 * i, int maxw, int maxh)
height = 0; height = 0;
if(i) if(i)
data = DecodePNG(i, &width, &height, maxw, maxh); data = DecodePNG(i, &width, &height, data, maxw, maxh);
} }
/** /**

View File

@ -1,15 +1,14 @@
/******************************************************************************************** /****************************************************************************
* *
* PNGU * PNGU
* *
* Original author: frontier (http://frontier-dev.net) * Original author: frontier (http://frontier-dev.net)
* Modified by Tantric, 2009-2010 * Modified by Tantric, 2009-2010
* *
********************************************************************************************/ ***************************************************************************/
#include <stdio.h> #include <stdio.h>
#include <malloc.h> #include <malloc.h>
#include <gccore.h>
#include "pngu.h" #include "pngu.h"
#include <png.h> #include <png.h>
@ -43,12 +42,12 @@ struct _IMGCTX
int source; int source;
void *buffer; void *buffer;
char *filename; char *filename;
PNGU_u32 cursor; u32 cursor;
PNGU_u32 propRead; u32 propRead;
PNGUPROP prop; PNGUPROP prop;
PNGU_u32 infoRead; u32 infoRead;
png_structp png_ptr; png_structp png_ptr;
png_infop info_ptr; png_infop info_ptr;
FILE *fd; FILE *fd;
@ -294,7 +293,7 @@ static int pngu_info (IMGCTX ctx)
return PNGU_OK; 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 rowbytes;
png_uint_32 i, propImgHeight; 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; 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; 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; u8 *dst;
int x, y, x2, y2, offset; int x, y, x2, y2, offset;
int xRatio = 0, yRatio = 0; 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; int len = (padWidth * padHeight) << 2;
if(len%32) len += (32-len%32); 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) if(!dst)
return NULL; return NULL;
@ -557,7 +559,7 @@ int PNGU_GetImageProperties (IMGCTX ctx, PNGUPROP *imgprop)
return PNGU_OK; 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; PNGUPROP imgProp;
IMGCTX ctx = PNGU_SelectImageFromBuffer(src); IMGCTX ctx = PNGU_SelectImageFromBuffer(src);
@ -567,16 +569,16 @@ PNGU_u8 * DecodePNG(const PNGU_u8 *src, int * width, int * height, int maxwidth,
return NULL; return NULL;
if(PNGU_GetImageProperties(ctx, &imgProp) == PNGU_OK) 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); PNGU_ReleaseImageContext (ctx);
return dst; 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; png_uint_32 rowbytes;
PNGU_u32 y; u32 y;
// Erase from the context any readed info // Erase from the context any readed info
pngu_free_info (ctx); pngu_free_info (ctx);
@ -635,7 +637,6 @@ int PNGU_EncodeFromRGB (IMGCTX ctx, PNGU_u32 width, PNGU_u32 height, void *buffe
rowbytes = ((rowbytes >>2) + 1) <<2; // Add extra padding so each row starts in a 4 byte boundary rowbytes = ((rowbytes >>2) + 1) <<2; // Add extra padding so each row starts in a 4 byte boundary
ctx->img_data = malloc(rowbytes * height); ctx->img_data = malloc(rowbytes * height);
memset(ctx->img_data, 0, rowbytes * height);
if (!ctx->img_data) if (!ctx->img_data)
{ {
@ -645,8 +646,8 @@ int PNGU_EncodeFromRGB (IMGCTX ctx, PNGU_u32 width, PNGU_u32 height, void *buffe
return PNGU_LIB_ERROR; return PNGU_LIB_ERROR;
} }
memset(ctx->img_data, 0, rowbytes * height);
ctx->row_pointers = malloc (sizeof (png_bytep) * height); ctx->row_pointers = malloc (sizeof (png_bytep) * height);
memset(ctx->row_pointers, 0, sizeof (png_bytep) * height);
if (!ctx->row_pointers) if (!ctx->row_pointers)
{ {
@ -656,6 +657,8 @@ int PNGU_EncodeFromRGB (IMGCTX ctx, PNGU_u32 width, PNGU_u32 height, void *buffe
return PNGU_LIB_ERROR; return PNGU_LIB_ERROR;
} }
memset(ctx->row_pointers, 0, sizeof (png_bytep) * height);
for (y = 0; y < height; ++y) for (y = 0; y < height; ++y)
{ {
ctx->row_pointers[y] = buffer + (y * rowbytes); 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; 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; 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 * 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); memset(tmpbuffer, 0, width*height*3);
png_uint_32 offset; png_uint_32 offset;
@ -712,3 +719,33 @@ int PNGU_EncodeFromGXTexture (IMGCTX ctx, PNGU_u32 width, PNGU_u32 height, void
free(tmpbuffer); free(tmpbuffer);
return res; 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;
}

View File

@ -10,32 +10,28 @@
#ifndef __PNGU__ #ifndef __PNGU__
#define __PNGU__ #define __PNGU__
#include <gccore.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #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 typedef struct
{ {
PNGU_u8 r; u8 r;
PNGU_u8 g; u8 g;
PNGU_u8 b; u8 b;
} PNGUCOLOR; } PNGUCOLOR;
typedef struct typedef struct
{ {
PNGU_u32 imgWidth; // In pixels u32 imgWidth; // In pixels
PNGU_u32 imgHeight; // In pixels u32 imgHeight; // In pixels
PNGU_u32 imgBitDepth; // In bitx u32 imgBitDepth; // In bitx
PNGU_u32 imgColorType; // PNGU_COLOR_TYPE_* u32 imgColorType; // PNGU_COLOR_TYPE_*
PNGU_u32 validBckgrnd; // Non zero if there is a background color u32 validBckgrnd; // Non zero if there is a background color
PNGUCOLOR bckgrnd; // Backgroun color PNGUCOLOR bckgrnd; // Background color
PNGU_u32 numTrans; // Number of transparent colors u32 numTrans; // Number of transparent colors
PNGUCOLOR *trans; // Transparent colors PNGUCOLOR *trans; // Transparent colors
} PNGUPROP; } PNGUPROP;
@ -47,10 +43,10 @@ typedef struct _IMGCTX *IMGCTX;
* Image context handling * * 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); 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); IMGCTX PNGU_SelectImageFromDevice (const char *filename);
// Frees resources associated with an image context. Always call this function when you no longer need the IMGCTX. // 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 * * Image conversion *
****************************************************************************/ ****************************************************************************/
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 *dst, int maxwidth, int maxheight);
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);
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 PNGU_EncodeFromEFB (IMGCTX ctx, u32 width, u32 height);
#ifdef __cplusplus #ifdef __cplusplus
} }