Mercurial > mplayer.hg
comparison gui/bitmap.c @ 23077:17bf4f4b0715
Gui --> gui
author | diego |
---|---|
date | Mon, 23 Apr 2007 07:42:42 +0000 |
parents | |
children | 7857af1ca50b |
comparison
equal
deleted
inserted
replaced
23076:39dd908375b2 | 23077:17bf4f4b0715 |
---|---|
1 #include <stdio.h> | |
2 #include <stdlib.h> | |
3 #include <string.h> | |
4 | |
5 #include <png.h> | |
6 | |
7 #include "mp_msg.h" | |
8 #include "help_mp.h" | |
9 #include "bitmap.h" | |
10 | |
11 int pngRead( unsigned char * fname,txSample * bf ) | |
12 { | |
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; | |
21 | |
22 FILE *fp=fopen( fname,"rb" ); | |
23 if ( !fp ) | |
24 { | |
25 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[png] file read error ( %s )\n",fname ); | |
26 return 1; | |
27 } | |
28 | |
29 fread( header,1,8,fp ); | |
30 if ( !png_check_sig( header,8 ) ) return 1; | |
31 | |
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 ); | |
35 | |
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 ); | |
40 | |
41 row_p=malloc( sizeof( png_bytep ) * bf->Height ); | |
42 if ( !row_p ) | |
43 { | |
44 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[png] not enough memory for row buffer\n" ); | |
45 return 2; | |
46 } | |
47 bf->Image=(png_bytep)malloc( png_get_rowbytes( png,info ) * bf->Height ); | |
48 if ( !bf->Image ) | |
49 { | |
50 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[png] not enough memory for image buffer\n" ); | |
51 return 2; | |
52 } | |
53 for ( i=0; i < bf->Height; i++ ) row_p[i]=&bf->Image[png_get_rowbytes( png,info ) * i]; | |
54 | |
55 png_read_image( png,row_p ); | |
56 free( row_p ); | |
57 | |
58 #if 0 | |
59 if ( color == PNG_COLOR_TYPE_PALETTE ) | |
60 { | |
61 int cols; | |
62 png_get_PLTE( png,info,(png_colorp *)&palette,&cols ); | |
63 } | |
64 #endif | |
65 | |
66 if ( color&PNG_COLOR_MASK_ALPHA ) | |
67 { | |
68 if ( color&PNG_COLOR_MASK_PALETTE || color == PNG_COLOR_TYPE_GRAY_ALPHA ) bf->BPP*=2; | |
69 else bf->BPP*=4; | |
70 } | |
71 else | |
72 { | |
73 if ( color&PNG_COLOR_MASK_PALETTE || color == PNG_COLOR_TYPE_GRAY ) bf->BPP*=1; | |
74 else bf->BPP*=3; | |
75 } | |
76 | |
77 png_read_end( png,endinfo ); | |
78 png_destroy_read_struct( &png,&info,&endinfo ); | |
79 | |
80 if ( fclose( fp ) != 0 ) | |
81 { | |
82 free( bf->Image ); | |
83 free( palette ); | |
84 return 1; | |
85 } | |
86 bf->ImageSize=bf->Width * bf->Height * ( bf->BPP / 8 ); | |
87 | |
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; | |
92 } | |
93 | |
94 int conv24to32( txSample * bf ) | |
95 { | |
96 unsigned char * tmpImage; | |
97 int i,c; | |
98 | |
99 if ( bf->BPP == 24 ) | |
100 { | |
101 tmpImage=bf->Image; | |
102 bf->ImageSize=bf->Width * bf->Height * 4; | |
103 bf->BPP=32; | |
104 if ( ( bf->Image=malloc( bf->ImageSize ) ) == NULL ) | |
105 { | |
106 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[bitmap] not enough memory for image\n" ); | |
107 return 1; | |
108 } | |
109 memset( bf->Image,0,bf->ImageSize ); | |
110 for ( c=0,i=0;i < (int)(bf->Width * bf->Height * 3); ) | |
111 { | |
112 bf->Image[c++]=tmpImage[i++]; //red | |
113 bf->Image[c++]=tmpImage[i++]; //green | |
114 bf->Image[c++]=tmpImage[i++]; c++; //blue | |
115 } | |
116 free( tmpImage ); | |
117 } | |
118 return 0; | |
119 } | |
120 | |
121 void bgr2rgb( txSample * bf ) | |
122 { | |
123 unsigned char c; | |
124 int i; | |
125 | |
126 for ( i=0;i < (int)bf->ImageSize;i+=4 ) | |
127 { | |
128 c=bf->Image[i]; | |
129 bf->Image[i]=bf->Image[i+2]; | |
130 bf->Image[i+2]=c; | |
131 } | |
132 } | |
133 | |
134 void Normalize( txSample * bf ) | |
135 { | |
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 | |
142 } | |
143 | |
144 unsigned char tmp[512]; | |
145 | |
146 unsigned char * fExist( unsigned char * fname ) | |
147 { | |
148 FILE * fl; | |
149 unsigned char ext[][6] = { ".png\0",".PNG\0" }; | |
150 int i; | |
151 | |
152 fl=fopen( fname,"rb" ); | |
153 if ( fl != NULL ) | |
154 { | |
155 fclose( fl ); | |
156 return fname; | |
157 } | |
158 for ( i=0;i<2;i++ ) | |
159 { | |
160 snprintf( tmp,511,"%s%s",fname,ext[i] ); | |
161 fl=fopen( tmp,"rb" ); | |
162 if ( fl != NULL ) | |
163 { | |
164 fclose( fl ); | |
165 return tmp; | |
166 } | |
167 } | |
168 return NULL; | |
169 } | |
170 | |
171 int bpRead( char * fname, txSample * bf ) | |
172 { | |
173 fname=fExist( fname ); | |
174 if ( fname == NULL ) return -2; | |
175 if ( pngRead( fname,bf ) ) | |
176 { | |
177 mp_dbg( MSGT_GPLAYER,MSGL_FATAL,"[bitmap] unknown file type ( %s )\n",fname ); | |
178 return -5; | |
179 } | |
180 if ( bf->BPP < 24 ) | |
181 { | |
182 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[bitmap] Sorry, only 24 and 32 bpp bitmaps are supported.\n" ); | |
183 return -1; | |
184 } | |
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; | |
192 } | |
193 | |
194 void Convert32to1( txSample * in,txSample * out,int adaptivlimit ) | |
195 { | |
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=calloc( 1,out->ImageSize ); | |
202 if ( out->Image == NULL ) mp_msg( MSGT_GPLAYER,MSGL_WARN,MSGTR_NotEnoughMemoryC32To1 ); | |
203 { | |
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++ ) | |
207 { | |
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; } | |
211 } | |
212 if ( b ) out->Image[c]=tmp; | |
213 if ( nothaveshape ) { free( out->Image ); out->Image=NULL; } | |
214 } | |
215 } | |
216 | |
217 void Convert1to32( txSample * in,txSample * out ) | |
218 { | |
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=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 ); | |
227 { | |
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++ ) | |
231 { | |
232 tmp=in->Image[i]; | |
233 for ( b=0;b<8;b++ ) | |
234 { | |
235 buf[c]=0; | |
236 if ( tmp&0x1 ) buf[c]=0xffffffff; | |
237 c++; tmp=tmp>>1; | |
238 } | |
239 } | |
240 } | |
241 } |