8 #include "../help_mp.h"
11 int pngRead( unsigned char * fname
,txSample
* bf
)
13 unsigned char header
[8];
18 png_bytep palette
= NULL
;
22 FILE *fp
=fopen( fname
,"rb" );
25 mp_dbg( MSGT_GPLAYER
,MSGL_DBG2
,"[png] file read error ( %s )\n",fname
);
29 fread( header
,1,8,fp
);
30 if ( !png_check_sig( header
,8 ) ) return 1;
32 png
=png_create_read_struct( PNG_LIBPNG_VER_STRING
,NULL
,NULL
,NULL
);
33 info
=png_create_info_struct( png
);
34 endinfo
=png_create_info_struct( png
);
36 png_init_io( png
,fp
);
37 png_set_sig_bytes( png
,8 );
38 png_read_info( png
,info
);
39 png_get_IHDR( png
,info
,&bf
->Width
,&bf
->Height
,&bf
->BPP
,&color
,NULL
,NULL
,NULL
);
41 row_p
=(png_bytep
*)malloc( sizeof( png_bytep
) * bf
->Height
);
44 mp_dbg( MSGT_GPLAYER
,MSGL_DBG2
,"[png] not enough memory for row buffer\n" );
47 bf
->Image
=(png_bytep
)malloc( png_get_rowbytes( png
,info
) * bf
->Height
);
50 mp_dbg( MSGT_GPLAYER
,MSGL_DBG2
,"[png] not enough memory for image buffer\n" );
53 for ( i
=0; i
< bf
->Height
; i
++ ) row_p
[i
]=&bf
->Image
[png_get_rowbytes( png
,info
) * i
];
55 png_read_image( png
,row_p
);
59 if ( color
== PNG_COLOR_TYPE_PALETTE
)
62 png_get_PLTE( png
,info
,(png_colorp
*)&palette
,&cols
);
66 if ( color
&PNG_COLOR_MASK_ALPHA
)
68 if ( color
&PNG_COLOR_MASK_PALETTE
|| color
== PNG_COLOR_TYPE_GRAY_ALPHA
) bf
->BPP
*=2;
73 if ( color
&PNG_COLOR_MASK_PALETTE
|| color
== PNG_COLOR_TYPE_GRAY
) bf
->BPP
*=1;
77 png_read_end( png
,endinfo
);
78 png_destroy_read_struct( &png
,&info
,&endinfo
);
80 if ( fclose( fp
) != 0 )
86 bf
->ImageSize
=bf
->Width
* bf
->Height
* ( bf
->BPP
/ 8 );
88 mp_dbg( MSGT_GPLAYER
,MSGL_DBG2
,"[png] filename: %s.\n",fname
);
89 mp_dbg( MSGT_GPLAYER
,MSGL_DBG2
,"[png] size: %dx%d bits: %d\n",bf
->Width
,bf
->Height
,bf
->BPP
);
90 mp_dbg( MSGT_GPLAYER
,MSGL_DBG2
,"[png] imagesize: %lu\n",bf
->ImageSize
);
94 int conv24to32( txSample
* bf
)
96 unsigned char * tmpImage
;
102 bf
->ImageSize
=bf
->Width
* bf
->Height
* 4;
104 if ( ( bf
->Image
=malloc( bf
->ImageSize
) ) == NULL
)
106 mp_dbg( MSGT_GPLAYER
,MSGL_DBG2
,"[bitmap] not enough memory for image\n" );
109 memset( bf
->Image
,0,bf
->ImageSize
);
110 for ( c
=0,i
=0;i
< (int)(bf
->Width
* bf
->Height
* 3); )
112 bf
->Image
[c
++]=tmpImage
[i
++]; //red
113 bf
->Image
[c
++]=tmpImage
[i
++]; //green
114 bf
->Image
[c
++]=tmpImage
[i
++]; c
++; //blue
121 void bgr2rgb( txSample
* bf
)
126 for ( i
=0;i
< (int)bf
->ImageSize
;i
+=4 )
129 bf
->Image
[i
]=bf
->Image
[i
+2];
134 void Normalize( txSample
* bf
)
137 #ifndef WORDS_BIGENDIAN
138 for ( i
=0;i
< (int)bf
->ImageSize
;i
+=4 ) bf
->Image
[i
+3]=0;
140 for ( i
=0;i
< (int)bf
->ImageSize
;i
+=4 ) bf
->Image
[i
]=0;
144 unsigned char tmp
[512];
146 unsigned char * fExist( unsigned char * fname
)
149 unsigned char ext
[][6] = { ".png\0",".PNG\0" };
152 fl
=fopen( fname
,"rb" );
160 snprintf( tmp
,511,"%s%s",fname
,ext
[i
] );
161 fl
=fopen( tmp
,"rb" );
171 int bpRead( char * fname
, txSample
* bf
)
173 fname
=fExist( fname
);
174 if ( fname
== NULL
) return -2;
175 if ( pngRead( fname
,bf
) )
177 mp_dbg( MSGT_GPLAYER
,MSGL_FATAL
,"[bitmap] unknown file type ( %s )\n",fname
);
182 mp_dbg( MSGT_GPLAYER
,MSGL_DBG2
,"[bitmap] Sorry, only 24 and 32 bpp bitmaps are supported.\n" );
185 if ( conv24to32( bf
) ) return -8;
186 #ifdef WORDS_BIGENDIAN
187 swab(bf
->Image
, bf
->Image
, bf
->ImageSize
);
194 void Convert32to1( txSample
* in
,txSample
* out
,int adaptivlimit
)
196 out
->Width
=in
->Width
;
197 out
->Height
=in
->Height
;
199 out
->ImageSize
=(out
->Width
* out
->Height
+ 7) / 8;
200 mp_dbg( MSGT_GPLAYER
,MSGL_DBG2
,"[c32to1] imagesize: %d\n",out
->ImageSize
);
201 out
->Image
=(char *)calloc( 1,out
->ImageSize
);
202 if ( out
->Image
== NULL
) mp_msg( MSGT_GPLAYER
,MSGL_WARN
,MSGTR_NotEnoughMemoryC32To1
);
204 int i
,b
,c
=0; unsigned int * buf
= NULL
; unsigned char tmp
= 0; int nothaveshape
= 1;
205 buf
=(unsigned int *)in
->Image
;
206 for ( b
=0,i
=0;i
< (int)(out
->Width
* out
->Height
);i
++ )
208 if ( (int)buf
[i
] != adaptivlimit
) tmp
=( tmp
>> 1 )|128;
209 else { tmp
=tmp
>> 1; buf
[i
]=nothaveshape
=0; }
210 if ( b
++ == 7 ) { out
->Image
[c
++]=tmp
; tmp
=b
=0; }
212 if ( b
) out
->Image
[c
]=tmp
;
213 if ( nothaveshape
) { free( out
->Image
); out
->Image
=NULL
; }
217 void Convert1to32( txSample
* in
,txSample
* out
)
219 if ( in
->Image
== NULL
) return;
220 out
->Width
=in
->Width
;
221 out
->Height
=in
->Height
;
223 out
->ImageSize
=out
->Width
* out
->Height
* 4;
224 out
->Image
=(char *)calloc( 1,out
->ImageSize
);
225 mp_dbg( MSGT_GPLAYER
,MSGL_DBG2
,"[c1to32] imagesize: %d\n",out
->ImageSize
);
226 if ( out
->Image
== NULL
) mp_msg( MSGT_GPLAYER
,MSGL_WARN
,MSGTR_NotEnoughMemoryC1To32
);
228 int i
,b
,c
=0; unsigned int * buf
= NULL
; unsigned char tmp
= 0;
229 buf
=(unsigned int *)out
->Image
;
230 for ( c
=0,i
=0;i
< (int)(in
->Width
* in
->Height
/ 8);i
++ )
236 if ( tmp
&0x1 ) buf
[c
]=0xffffffff;