obvious gcc warning fix, approved by Nico
[mplayer/glamo.git] / Gui / bitmap.c
blobe3c36d5359c0593ede4066fd233df52dbf73978a
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
5 #include <png.h>
7 #include "../mp_msg.h"
8 #include "../help_mp.h"
9 #include "bitmap.h"
11 int pngRead( unsigned char * fname,txSample * bf )
13 unsigned char header[8];
14 png_structp png;
15 png_infop info;
16 png_infop endinfo;
17 png_bytep * row_p;
18 png_bytep palette = NULL;
19 int color;
20 png_uint_32 i;
22 FILE *fp=fopen( fname,"rb" );
23 if ( !fp )
25 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[png] file read error ( %s )\n",fname );
26 return 1;
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 );
42 if ( !row_p )
44 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[png] not enough memory for row buffer\n" );
45 return 2;
47 bf->Image=(png_bytep)malloc( png_get_rowbytes( png,info ) * bf->Height );
48 if ( !bf->Image )
50 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[png] not enough memory for image buffer\n" );
51 return 2;
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 );
56 free( row_p );
58 #if 0
59 if ( color == PNG_COLOR_TYPE_PALETTE )
61 int cols;
62 png_get_PLTE( png,info,(png_colorp *)&palette,&cols );
64 #endif
66 if ( color&PNG_COLOR_MASK_ALPHA )
68 if ( color&PNG_COLOR_MASK_PALETTE || color == PNG_COLOR_TYPE_GRAY_ALPHA ) bf->BPP*=2;
69 else bf->BPP*=4;
71 else
73 if ( color&PNG_COLOR_MASK_PALETTE || color == PNG_COLOR_TYPE_GRAY ) bf->BPP*=1;
74 else bf->BPP*=3;
77 png_read_end( png,endinfo );
78 png_destroy_read_struct( &png,&info,&endinfo );
80 if ( fclose( fp ) != 0 )
82 free( bf->Image );
83 free( palette );
84 return 1;
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 );
91 return 0;
94 int conv24to32( txSample * bf )
96 unsigned char * tmpImage;
97 int i,c;
99 if ( bf->BPP == 24 )
101 tmpImage=bf->Image;
102 bf->ImageSize=bf->Width * bf->Height * 4;
103 bf->BPP=32;
104 if ( ( bf->Image=malloc( bf->ImageSize ) ) == NULL )
106 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[bitmap] not enough memory for image\n" );
107 return 1;
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
116 free( tmpImage );
118 return 0;
121 void bgr2rgb( txSample * bf )
123 unsigned char c;
124 int i;
126 for ( i=0;i < (int)bf->ImageSize;i+=4 )
128 c=bf->Image[i];
129 bf->Image[i]=bf->Image[i+2];
130 bf->Image[i+2]=c;
134 void Normalize( txSample * bf )
136 int i;
137 #ifndef WORDS_BIGENDIAN
138 for ( i=0;i < (int)bf->ImageSize;i+=4 ) bf->Image[i+3]=0;
139 #else
140 for ( i=0;i < (int)bf->ImageSize;i+=4 ) bf->Image[i]=0;
141 #endif
144 unsigned char tmp[512];
146 unsigned char * fExist( unsigned char * fname )
148 FILE * fl;
149 unsigned char ext[][6] = { ".png\0",".PNG\0" };
150 int i;
152 fl=fopen( fname,"rb" );
153 if ( fl != NULL )
155 fclose( fl );
156 return fname;
158 for ( i=0;i<2;i++ )
160 snprintf( tmp,511,"%s%s",fname,ext[i] );
161 fl=fopen( tmp,"rb" );
162 if ( fl != NULL )
164 fclose( fl );
165 return tmp;
168 return NULL;
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 );
178 return -5;
180 if ( bf->BPP < 24 )
182 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[bitmap] Sorry, only 24 and 32 bpp bitmaps are supported.\n" );
183 return -1;
185 if ( conv24to32( bf ) ) return -8;
186 #ifdef WORDS_BIGENDIAN
187 swab(bf->Image, bf->Image, bf->ImageSize);
188 #endif
189 bgr2rgb( bf );
190 Normalize( bf );
191 return 0;
194 void Convert32to1( txSample * in,txSample * out,int adaptivlimit )
196 out->Width=in->Width;
197 out->Height=in->Height;
198 out->BPP=1;
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;
222 out->BPP=32;
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++ )
232 tmp=in->Image[i];
233 for ( b=0;b<8;b++ )
235 buf[c]=0;
236 if ( tmp&0x1 ) buf[c]=0xffffffff;
237 c++; tmp=tmp>>1;