0
0
mirror of https://github.com/mpv-player/mpv.git synced 2024-09-20 12:02:23 +02:00

32bpp support, allocating palette based on used colors by file (possible overflow fix)

git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@4672 b3059339-0415-0410-9bf9-f77b7e298cf2
This commit is contained in:
alex 2002-02-12 17:04:13 +00:00
parent 65750b3de5
commit ed93a4d20c

30
mpng.c
View File

@ -1,9 +1,9 @@
#include <stdlib.h> #include <stdlib.h>
#include "config.h" #include "config.h"
#include "bswap.h" #include "bswap.h"
#include "postproc/rgb2rgb.h" #include "postproc/rgb2rgb.h"
#include "libvo/fastmemcpy.h"
#include "mp_msg.h" #include "mp_msg.h"
#include "png.h" #include "png.h"
@ -33,6 +33,15 @@ void decode_mpng(
char * palette = NULL; char * palette = NULL;
int depth,color; int depth,color;
png_uint_32 i; png_uint_32 i;
/* currently supporting only 24 and 32bpp */
if ((bytes_per_pixel != 3) && (bytes_per_pixel != 4))
{
/* is this memset really needed? */
memset(decoded, 0, width*height*bytes_per_pixel);
return;
}
png=png_create_read_struct( PNG_LIBPNG_VER_STRING,NULL,NULL,NULL ); png=png_create_read_struct( PNG_LIBPNG_VER_STRING,NULL,NULL,NULL );
info=png_create_info_struct( png ); info=png_create_info_struct( png );
endinfo=png_create_info_struct( png ); endinfo=png_create_info_struct( png );
@ -71,9 +80,13 @@ void decode_mpng(
free( data ); free( data );
break; break;
case PNG_COLOR_TYPE_GRAY: case PNG_COLOR_TYPE_GRAY:
/* constant 256 colors */
palette=malloc( 1024 ); palette=malloc( 1024 );
for ( i=0;i < 256;i++ ) palette[(i*4)]=palette[(i*4)+1]=palette[(i*4)+2]=(char)i; for ( i=0;i < 256;i++ ) palette[(i*4)]=palette[(i*4)+1]=palette[(i*4)+2]=(char)i;
palette8torgb24( data,decoded,png_width * png_height,palette ); if (bytes_per_pixel == 4)
palette8torgb32( data,decoded,png_width * png_height,palette );
else
palette8torgb24( data,decoded,png_width * png_height,palette );
free( data ); free( data );
break; break;
case PNG_COLOR_TYPE_PALETTE: case PNG_COLOR_TYPE_PALETTE:
@ -81,7 +94,8 @@ void decode_mpng(
int cols; int cols;
unsigned char * pal; unsigned char * pal;
png_get_PLTE( png,info,(png_colorp*)&pal,&cols ); png_get_PLTE( png,info,(png_colorp*)&pal,&cols );
palette=calloc( 1,1024 ); palette=calloc( 1,cols*4 );
mp_dbg(MSGT_DECVIDEO, MSGL_DBG2, "[mPNG] palette. used colors: %d\n", cols);
for ( i=0;i < cols;i++ ) for ( i=0;i < cols;i++ )
{ {
palette[(i*4) ]=pal[(i*3)+2]; palette[(i*4) ]=pal[(i*3)+2];
@ -89,11 +103,17 @@ void decode_mpng(
palette[(i*4)+2]=pal[(i*3) ]; palette[(i*4)+2]=pal[(i*3) ];
} }
} }
palette8torgb24( data,decoded,png_width * png_height,palette ); if (bytes_per_pixel == 4)
palette8torgb32( data,decoded,png_width * png_height,palette );
else
palette8torgb24( data,decoded,png_width * png_height,palette );
free( data ); free( data );
break; break;
case PNG_COLOR_TYPE_RGB_ALPHA: case PNG_COLOR_TYPE_RGB_ALPHA:
rgb32to24( data,decoded,png_width * png_height * 4 ); if (bytes_per_pixel == 4)
memcpy(decoded, data, png_width * png_height * 4);
else
rgb32to24( data,decoded,png_width * png_height * 4 );
free( data ); free( data );
break; break;
} }