Mercurial > mplayer.hg
annotate mpng.c @ 5092:3cc4aaa3724b
10l
author | arpi |
---|---|
date | Fri, 15 Mar 2002 16:36:45 +0000 |
parents | af6fe94d455b |
children |
rev | line source |
---|---|
4656 | 1 #include <stdlib.h> |
2 | |
3 #include "config.h" | |
4 #include "bswap.h" | |
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 | 7 #include "mp_msg.h" |
8 #include "png.h" | |
9 | |
4707 | 10 static int pngPointer; |
4656 | 11 |
4707 | 12 static void pngReadFN( png_structp pngstr,png_bytep buffer,png_size_t size ) |
4656 | 13 { |
14 char * p = pngstr->io_ptr; | |
15 memcpy( buffer,(char *)&p[pngPointer],size ); | |
16 pngPointer+=size; | |
17 } | |
18 | |
19 void decode_mpng( | |
20 unsigned char *encoded, | |
21 int encoded_size, | |
22 unsigned char *decoded, | |
23 int width, | |
24 int height, | |
25 int bytes_per_pixel) | |
26 { | |
27 png_structp png; | |
28 png_infop info; | |
29 png_infop endinfo; | |
30 png_bytep data; | |
31 png_bytep * row_p; | |
32 png_uint_32 png_width,png_height; | |
33 char * palette = NULL; | |
34 int depth,color; | |
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 | 45 png=png_create_read_struct( PNG_LIBPNG_VER_STRING,NULL,NULL,NULL ); |
46 info=png_create_info_struct( png ); | |
47 endinfo=png_create_info_struct( png ); | |
48 | |
49 pngPointer=8; | |
50 png_set_read_fn( png,encoded,pngReadFN ); | |
51 png_set_sig_bytes( png,8 ); | |
52 png_read_info( png,info ); | |
53 png_get_IHDR( png,info,&png_width,&png_height,&depth,&color,NULL,NULL,NULL ); | |
54 | |
55 png_set_bgr( png ); | |
56 | |
57 #if 0 | |
58 switch( info->color_type ) | |
59 { | |
60 case PNG_COLOR_TYPE_GRAY_ALPHA: printf( "[png] used GrayA -> stripping alpha channel\n" ); break; | |
61 case PNG_COLOR_TYPE_GRAY: printf( "[png] used Gray -> rgb\n" ); break; | |
62 case PNG_COLOR_TYPE_PALETTE: printf( "[png] used palette -> rgb\n" ); break; | |
63 case PNG_COLOR_TYPE_RGB_ALPHA: printf( "[png] used RGBA -> stripping alpha channel\n" ); break; | |
64 case PNG_COLOR_TYPE_RGB: printf( "[png] read rgb datas.\n" ); break; | |
65 } | |
66 #endif | |
67 | |
68 if ( info->color_type == PNG_COLOR_TYPE_RGB ) data=decoded; | |
69 else data=(png_bytep)malloc( png_get_rowbytes( png,info ) * height ); | |
70 | |
71 row_p=(png_bytep*)malloc( sizeof( png_bytep ) * png_height ); | |
72 for ( i=0; i < png_height; i++ ) row_p[i]=&data[png_get_rowbytes( png,info ) * i]; | |
73 png_read_image( png,row_p ); | |
74 free( row_p ); | |
75 | |
76 switch( info->color_type ) | |
77 { | |
78 case PNG_COLOR_TYPE_GRAY_ALPHA: | |
79 mp_msg( MSGT_DECVIDEO,MSGL_INFO,"Sorry gray scaled png with alpha channel not supported at moment.\n" ); | |
80 free( data ); | |
81 break; | |
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 | 84 palette=malloc( 1024 ); |
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 | 90 free( data ); |
91 break; | |
92 case PNG_COLOR_TYPE_PALETTE: | |
93 { | |
94 int cols; | |
95 unsigned char * pal; | |
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 | 99 for ( i=0;i < cols;i++ ) |
100 { | |
101 palette[(i*4) ]=pal[(i*3)+2]; | |
102 palette[(i*4)+1]=pal[(i*3)+1]; | |
103 palette[(i*4)+2]=pal[(i*3) ]; | |
104 } | |
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 | 110 free( data ); |
111 break; | |
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 | 117 free( data ); |
118 break; | |
119 } | |
120 | |
121 if ( palette ) free( palette ); | |
122 | |
123 png_read_end( png,endinfo ); | |
124 png_destroy_read_struct( &png,&info,&endinfo ); | |
125 } | |
126 |