jzimagepng.cpp
来自「君正早期ucos系统(只有早期的才不没有打包成库),MPLAYER,文件系统,图」· C++ 代码 · 共 664 行 · 第 1/2 页
CPP
664 行
// JzImagePng.cpp: implementation of the CJzImagePng class.
//
//////////////////////////////////////////////////////////////////////
#include "JzImagePng.h"
#include "JzImageInfo.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
extern "C"{
#if 1
void *ex_png_malloc(unsigned int size)
{
return CImageMemMgr::malloc(size);
}
void ex_png_free(void * mem)
{
CImageMemMgr::free(mem);
}
#endif
}
////////////////////////////////////////////////////////////////////////////////
void CJzImagePng::ima_png_error(png_struct *png_ptr, char *message)
{
strcpy(info.szLastError,message);
longjmp(png_ptr->jmpbuf, 1);
}
////////////////////////////////////////////////////////////////////////////////
void CJzImagePng::expand2to4bpp(BYTE* prow)
{
BYTE *psrc,*pdst;
BYTE pos,idx;
for(long x=info.dwWidth-1;x>=0;x--){
psrc = prow + ((2*x)>>3);
pdst = prow + ((4*x)>>3);
pos = (BYTE)(2*(3-x%4));
idx = (BYTE)((*psrc & (0x03<<pos))>>pos);
pos = (BYTE)(4*(1-x%2));
*pdst &= ~(0x0F<<pos);
*pdst |= (idx & 0x0F)<<pos;
}
}
////////////////////////////////////////////////////////////////////////////////
void CJzImagePng::expand2to4bpp(BYTE* prow,int width)
{
BYTE *psrc,*pdst;
BYTE pos,idx;
for(long x=width-1;x>=0;x--){
psrc = prow + ((2*x)>>3);
pdst = prow + ((4*x)>>3);
pos = (BYTE)(2*(3-x%4));
idx = (BYTE)((*psrc & (0x03<<pos))>>pos);
pos = (BYTE)(4*(1-x%2));
*pdst &= ~(0x0F<<pos);
*pdst |= (idx & 0x0F)<<pos;
}
}
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CJzImagePng::CJzImagePng()
{
}
CJzImagePng::~CJzImagePng()
{
}
////////////////////////////////////////////////////////////////////////////////
bool CJzImagePng::Decode(CxFile *hFile)
{
DWORD ip;
DWORD dwcount;
DWORD biClrUsed;
row_pointers = NULL;
pRgb = NULL;
png_color_16 my_background={ 0, 192, 192, 192, 0 };
png_color_16 *image_background;
//try {
/* Create and initialize the png_struct with the desired error handler
* functions. If you want to use the default stderr and longjump method,
* you can supply NULL for the last three parameters. We also supply the
* the compiler header file version, so that we know if the application
* was compiled with a compatible version of the library. REQUIRED
*/
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,(void *)NULL,NULL,NULL);
if (png_ptr == NULL){
printf("Failed to create PNG structure\n");
goto EXIT;
}
/* Allocate/initialize the memory for image information. REQUIRED. */
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
printf("Failed to initialize PNG info structure\n");
goto EXIT;
}
/* Set error handling if you are using the setjmp/longjmp method (this is
* the normal method of doing things with libpng). REQUIRED unless you
* set up your own error handlers in the png_create_read_struct() earlier.
*/
if (setjmp(png_ptr->jmpbuf)) {
/* Free all of the memory associated with the png_ptr and info_ptr */
if (row_pointers) delete[] row_pointers;
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
printf("%s setjmp error!\n",__FILE__);
goto EXIT;
}
/* set up the input control */
//png_init_io(png_ptr, hFile);
// use custom I/O functions
png_set_read_fn(png_ptr, hFile, (png_rw_ptr)user_read_data);
png_set_error_fn(png_ptr,info.szLastError,(png_error_ptr)user_error_fn,NULL);
/* read the file information */
png_read_info(png_ptr, info_ptr);
//png_set_sCAL_s(png_ptr, info_ptr,2,480 /2 ,272 / 2);
/* allocate the memory to hold the image using the fields of png_info. */
#if 0
if (info_ptr->pixel_depth != 32){
//<yeonjun jeong> preserve original background info.
if (png_get_bKGD(png_ptr, info_ptr, &image_background))
png_set_background(png_ptr, image_background,PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
else
png_set_background(png_ptr, &my_background,PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
// <vho> - we call png_set_bgr() below
// <vho> //<yeonjun jeong> safe check
// <vho> if (info_ptr->pixel_depth > 16 ) info_ptr->color_type = COLORTYPE_COLOR;
}
#endif
/* tell libpng to strip 16 bit depth files down to 8 bits */
if (info_ptr->bit_depth == 16) png_set_strip_16(png_ptr);
pixel_depth=info_ptr->pixel_depth;
//printf("width = %d,height = %d pixel_depth = %d\n",info_ptr->width,info_ptr->height,pixel_depth);
#if 1
if( pixel_depth >= 64 ) pixel_depth = 32;
else if( pixel_depth >= 48 ) pixel_depth = 24;
else if (pixel_depth >= 32 ) pixel_depth=32;
else if(pixel_depth >= 24 ) pixel_depth=24;
else if(pixel_depth >= 16 ) pixel_depth=16;
else if(pixel_depth >= 8 ) pixel_depth=8;
if( pixel_depth < 24 ) return FALSE;
//printf("width = %d,height = %d pixel_depth = %d\n",info_ptr->width,info_ptr->height,pixel_depth);
#endif
#if 0
if (pixel_depth > 16 ) pixel_depth=24;
if (pixel_depth == 16 ) pixel_depth=8;
#endif
{
IVSETIMAGEINFO ivSetImageInfo = ivGetImageInfoCallback();
if( ivSetImageInfo )
{
IVIMAGEINFO ImageInfo;
ImageInfo.w = info_ptr->width;
ImageInfo.h = info_ptr->height;
ImageInfo.bpp = pixel_depth;
ivSetImageInfo( &ImageInfo );
info.dwimaWidth = ImageInfo.w;
info.dwimaHeight = ImageInfo.h;
info.pImage = ImageInfo.pImage;
ivscale = ImageInfo.ivscale;
if( info.pImage == NULL ) return FALSE;
}
}
if(info.nEscape == -1 )
{
info.dwWidth = info_ptr->width;
info.dwHeight = info_ptr->height;
info.wBpp = pixel_depth;
return FALSE;
}
if (!CreateImageInfo(info_ptr->width, info_ptr->height, pixel_depth, CXIMAGE_FORMAT_PNG))
{
MEM_ERROR;
printf("Can't allocate memory!");
goto EXIT;
}
/* get metrics */
//printf("=================== Decode png ================\n");
//info.wBpp = info_ptr->pixel_depth;
biClrUsed = (1 << info.wBpp);
dwcount = biClrUsed;
if (info_ptr->num_palette>0)
{
if(dwcount > info_ptr->num_palette) dwcount = info_ptr->num_palette;
if(!CreatePalette(biClrUsed))
{
MEM_ERROR;
printf("Can't allocate memory!");
goto EXIT;
}
SetPalette((rgb_color*)info_ptr->palette,dwcount);
}
else if (info_ptr->bit_depth ==2) { //<DP> needed for 2 bpp grayscale PNGs
if(!CreatePalette(biClrUsed))
{
MEM_ERROR;
printf("Can't allocate memory!");
goto EXIT;
}
SetPaletteColor(0,0,0,0);
SetPaletteColor(1,85,85,85);
SetPaletteColor(2,170,170,170);
SetPaletteColor(3,255,255,255);
} else if(info.wBpp < 16){
if(CreateGrayPalette(pixel_depth)== NULL)
{
MEM_ERROR;
printf("Can't allocate memory pixel_depth = %d!",pixel_depth);
goto EXIT;
}
}
// simple transparency (the real PGN transparency is more complex)
if (info_ptr->num_trans!=0){
//palette transparency
pal=pRgb;
if (pal){
for (ip=0;ip<min(biClrUsed,(unsigned long)info_ptr->num_trans);ip++)
pal[ip].rgbReserved=info_ptr->trans[ip];
if (info_ptr->num_trans==1 && pal[0].rgbReserved==0){
//info.nBkgndIndex = 0;
} else {
//info.bAlphaPaletteEnabled=true;
for (;ip<biClrUsed;ip++)
pal[ip].rgbReserved=255;
}
}
}
//printf("color_type = %d PNG_COLOR_TYPE_RGB_ALPHA = %d\n",info_ptr->color_type,PNG_COLOR_TYPE_RGB_ALPHA);
//printf("pixel_depth = %d PNG_COLOR_TYPE_GRAY_ALPHA = %d\n",info_ptr->pixel_depth,PNG_COLOR_TYPE_GRAY_ALPHA);
if (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA || //Alpha channel
(info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA && info_ptr->pixel_depth == 32)){
if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA){
png_set_gray_to_rgb(png_ptr);
png_set_expand(png_ptr);
}
#if CXIMAGE_SUPPORT_ALPHA // <vho>
png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);
#else
png_set_strip_alpha(png_ptr);
#endif //CXIMAGE_SUPPORT_ALPHA
}
// <vho> - flip the RGB pixels to BGR (or RGBA to BGRA)
if (info_ptr->color_type & PNG_COLOR_MASK_COLOR) png_set_bgr(png_ptr);
// <vho> - handle cancel
if (info.nEscape) longjmp(png_ptr->jmpbuf, 1);
//allocate the buffer
row_stride = info_ptr->width * ((info_ptr->pixel_depth+7)>>3);
row_pointers = (BYTE *)malloc(10 + row_stride);
if(row_pointers == 0)
{
printf("Can't allocate memory!");
goto EXIT;
}
// turn on interlace handling
number_passes = png_set_interlace_handling(png_ptr);
#if 0
if (number_passes>1){
SetCodecOption(1);
} else {
SetCodecOption(0);
}
#endif
printf("********** 33info_ptr->width: %d\n", info_ptr->width);
printf("********** 33info_ptr->height: %d\n", info_ptr->height);
deflate(info.wBpp);
free ( row_pointers );
//printf("width %d\n",head.biWidth);
/* read the rest of the file, getting any additional chunks in info_ptr */
png_read_end(png_ptr, info_ptr);
/* clean up after the read, and free any memory allocated - REQUIRED */
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
/*
} catch (char *message) {
if (strcmp(message,"")) strncpy(info.szLastError,message,255);
return FALSE;
}
*/
/* that's it */
if(pRgb)
delete[] pRgb;
return TRUE;
EXIT:
if(pRgb)
delete[] pRgb;
return FALSE;
}
#if 0
void CJzImagePng::ivReadRow(unsigned char *line, unsigned char *row_pointers)
{
int dx,a, j
switch(pixel_depth)
{
case 32:
{
a = row_pointers[dx*4 + 3];
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?