annotate mpng.c @ 5282:a5e0d4c469e0

dlfcn.h shouldn't be included if XANIM support disabled
author arpi
date Sat, 23 Mar 2002 18:38:41 +0000
parents af6fe94d455b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4656
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
1 #include <stdlib.h>
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
2
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
3 #include "config.h"
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
4 #include "bswap.h"
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
5 #include "postproc/rgb2rgb.h"
4671
91a1ce283aa8 32bpp support, allocating palette based on used colors by file (possible overflow fix)
alex
parents: 4656
diff changeset
6 #include "libvo/fastmemcpy.h"
4656
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
7 #include "mp_msg.h"
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
8 #include "png.h"
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
9
4707
af6fe94d455b +static
arpi
parents: 4671
diff changeset
10 static int pngPointer;
4656
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
11
4707
af6fe94d455b +static
arpi
parents: 4671
diff changeset
12 static void pngReadFN( png_structp pngstr,png_bytep buffer,png_size_t size )
4656
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
13 {
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
14 char * p = pngstr->io_ptr;
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
15 memcpy( buffer,(char *)&p[pngPointer],size );
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
16 pngPointer+=size;
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
17 }
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
18
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
19 void decode_mpng(
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
20 unsigned char *encoded,
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
21 int encoded_size,
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
22 unsigned char *decoded,
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
23 int width,
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
24 int height,
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
25 int bytes_per_pixel)
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
26 {
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
27 png_structp png;
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
28 png_infop info;
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
29 png_infop endinfo;
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
30 png_bytep data;
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
31 png_bytep * row_p;
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
32 png_uint_32 png_width,png_height;
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
33 char * palette = NULL;
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
34 int depth,color;
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
35 png_uint_32 i;
4671
91a1ce283aa8 32bpp support, allocating palette based on used colors by file (possible overflow fix)
alex
parents: 4656
diff changeset
36
91a1ce283aa8 32bpp support, allocating palette based on used colors by file (possible overflow fix)
alex
parents: 4656
diff changeset
37 /* currently supporting only 24 and 32bpp */
91a1ce283aa8 32bpp support, allocating palette based on used colors by file (possible overflow fix)
alex
parents: 4656
diff changeset
38 if ((bytes_per_pixel != 3) && (bytes_per_pixel != 4))
91a1ce283aa8 32bpp support, allocating palette based on used colors by file (possible overflow fix)
alex
parents: 4656
diff changeset
39 {
91a1ce283aa8 32bpp support, allocating palette based on used colors by file (possible overflow fix)
alex
parents: 4656
diff changeset
40 /* is this memset really needed? */
91a1ce283aa8 32bpp support, allocating palette based on used colors by file (possible overflow fix)
alex
parents: 4656
diff changeset
41 memset(decoded, 0, width*height*bytes_per_pixel);
91a1ce283aa8 32bpp support, allocating palette based on used colors by file (possible overflow fix)
alex
parents: 4656
diff changeset
42 return;
91a1ce283aa8 32bpp support, allocating palette based on used colors by file (possible overflow fix)
alex
parents: 4656
diff changeset
43 }
91a1ce283aa8 32bpp support, allocating palette based on used colors by file (possible overflow fix)
alex
parents: 4656
diff changeset
44
4656
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
45 png=png_create_read_struct( PNG_LIBPNG_VER_STRING,NULL,NULL,NULL );
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
46 info=png_create_info_struct( png );
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
47 endinfo=png_create_info_struct( png );
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
48
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
49 pngPointer=8;
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
50 png_set_read_fn( png,encoded,pngReadFN );
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
51 png_set_sig_bytes( png,8 );
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
52 png_read_info( png,info );
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
53 png_get_IHDR( png,info,&png_width,&png_height,&depth,&color,NULL,NULL,NULL );
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
54
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
55 png_set_bgr( png );
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
56
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
57 #if 0
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
58 switch( info->color_type )
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
59 {
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
60 case PNG_COLOR_TYPE_GRAY_ALPHA: printf( "[png] used GrayA -> stripping alpha channel\n" ); break;
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
61 case PNG_COLOR_TYPE_GRAY: printf( "[png] used Gray -> rgb\n" ); break;
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
62 case PNG_COLOR_TYPE_PALETTE: printf( "[png] used palette -> rgb\n" ); break;
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
63 case PNG_COLOR_TYPE_RGB_ALPHA: printf( "[png] used RGBA -> stripping alpha channel\n" ); break;
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
64 case PNG_COLOR_TYPE_RGB: printf( "[png] read rgb datas.\n" ); break;
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
65 }
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
66 #endif
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
67
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
68 if ( info->color_type == PNG_COLOR_TYPE_RGB ) data=decoded;
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
69 else data=(png_bytep)malloc( png_get_rowbytes( png,info ) * height );
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
70
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
71 row_p=(png_bytep*)malloc( sizeof( png_bytep ) * png_height );
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
72 for ( i=0; i < png_height; i++ ) row_p[i]=&data[png_get_rowbytes( png,info ) * i];
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
73 png_read_image( png,row_p );
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
74 free( row_p );
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
75
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
76 switch( info->color_type )
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
77 {
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
78 case PNG_COLOR_TYPE_GRAY_ALPHA:
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
79 mp_msg( MSGT_DECVIDEO,MSGL_INFO,"Sorry gray scaled png with alpha channel not supported at moment.\n" );
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
80 free( data );
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
81 break;
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
82 case PNG_COLOR_TYPE_GRAY:
4671
91a1ce283aa8 32bpp support, allocating palette based on used colors by file (possible overflow fix)
alex
parents: 4656
diff changeset
83 /* constant 256 colors */
4656
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
84 palette=malloc( 1024 );
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
85 for ( i=0;i < 256;i++ ) palette[(i*4)]=palette[(i*4)+1]=palette[(i*4)+2]=(char)i;
4671
91a1ce283aa8 32bpp support, allocating palette based on used colors by file (possible overflow fix)
alex
parents: 4656
diff changeset
86 if (bytes_per_pixel == 4)
91a1ce283aa8 32bpp support, allocating palette based on used colors by file (possible overflow fix)
alex
parents: 4656
diff changeset
87 palette8torgb32( data,decoded,png_width * png_height,palette );
91a1ce283aa8 32bpp support, allocating palette based on used colors by file (possible overflow fix)
alex
parents: 4656
diff changeset
88 else
91a1ce283aa8 32bpp support, allocating palette based on used colors by file (possible overflow fix)
alex
parents: 4656
diff changeset
89 palette8torgb24( data,decoded,png_width * png_height,palette );
4656
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
90 free( data );
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
91 break;
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
92 case PNG_COLOR_TYPE_PALETTE:
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
93 {
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
94 int cols;
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
95 unsigned char * pal;
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
96 png_get_PLTE( png,info,(png_colorp*)&pal,&cols );
4671
91a1ce283aa8 32bpp support, allocating palette based on used colors by file (possible overflow fix)
alex
parents: 4656
diff changeset
97 palette=calloc( 1,cols*4 );
91a1ce283aa8 32bpp support, allocating palette based on used colors by file (possible overflow fix)
alex
parents: 4656
diff changeset
98 mp_dbg(MSGT_DECVIDEO, MSGL_DBG2, "[mPNG] palette. used colors: %d\n", cols);
4656
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
99 for ( i=0;i < cols;i++ )
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
100 {
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
101 palette[(i*4) ]=pal[(i*3)+2];
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
102 palette[(i*4)+1]=pal[(i*3)+1];
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
103 palette[(i*4)+2]=pal[(i*3) ];
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
104 }
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
105 }
4671
91a1ce283aa8 32bpp support, allocating palette based on used colors by file (possible overflow fix)
alex
parents: 4656
diff changeset
106 if (bytes_per_pixel == 4)
91a1ce283aa8 32bpp support, allocating palette based on used colors by file (possible overflow fix)
alex
parents: 4656
diff changeset
107 palette8torgb32( data,decoded,png_width * png_height,palette );
91a1ce283aa8 32bpp support, allocating palette based on used colors by file (possible overflow fix)
alex
parents: 4656
diff changeset
108 else
91a1ce283aa8 32bpp support, allocating palette based on used colors by file (possible overflow fix)
alex
parents: 4656
diff changeset
109 palette8torgb24( data,decoded,png_width * png_height,palette );
4656
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
110 free( data );
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
111 break;
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
112 case PNG_COLOR_TYPE_RGB_ALPHA:
4671
91a1ce283aa8 32bpp support, allocating palette based on used colors by file (possible overflow fix)
alex
parents: 4656
diff changeset
113 if (bytes_per_pixel == 4)
91a1ce283aa8 32bpp support, allocating palette based on used colors by file (possible overflow fix)
alex
parents: 4656
diff changeset
114 memcpy(decoded, data, png_width * png_height * 4);
91a1ce283aa8 32bpp support, allocating palette based on used colors by file (possible overflow fix)
alex
parents: 4656
diff changeset
115 else
91a1ce283aa8 32bpp support, allocating palette based on used colors by file (possible overflow fix)
alex
parents: 4656
diff changeset
116 rgb32to24( data,decoded,png_width * png_height * 4 );
4656
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
117 free( data );
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
118 break;
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
119 }
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
120
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
121 if ( palette ) free( palette );
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
122
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
123 png_read_end( png,endinfo );
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
124 png_destroy_read_struct( &png,&info,&endinfo );
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
125 }
04880518728d add initial mPNG support
pontscho
parents:
diff changeset
126