Mercurial > mplayer.hg
annotate Gui/bitmap.c @ 13731:199feba24d65
sync by Mizda Gabor <gabrov at freemail dot hu>
author | diego |
---|---|
date | Thu, 21 Oct 2004 21:54:20 +0000 |
parents | 46badb3d0b1c |
children | c30e193ac112 |
rev | line source |
---|---|
2080 | 1 #include <stdio.h> |
1693 | 2 #include <stdlib.h> |
2080 | 3 #include <string.h> |
1693 | 4 |
8046 | 5 #include <png.h> |
6 | |
7 #include "../../mp_msg.h" | |
1693 | 8 #include "bitmap.h" |
9 | |
8046 | 10 int pngRead( unsigned char * fname,txSample * bf ) |
11 { | |
12 unsigned char header[8]; | |
13 png_structp png; | |
14 png_infop info; | |
15 png_infop endinfo; | |
16 png_bytep * row_p; | |
17 png_bytep palette = NULL; | |
18 int color; | |
19 png_uint_32 i; | |
20 | |
21 FILE *fp=fopen( fname,"rb" ); | |
22 if ( !fp ) | |
23 { | |
24 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[png] file read error ( %s ).\n",fname ); | |
25 return 1; | |
26 } | |
27 | |
28 fread( header,1,8,fp ); | |
29 if ( !png_check_sig( header,8 ) ) return 1; | |
30 | |
31 png=png_create_read_struct( PNG_LIBPNG_VER_STRING,NULL,NULL,NULL ); | |
32 info=png_create_info_struct( png ); | |
33 endinfo=png_create_info_struct( png ); | |
34 | |
35 png_init_io( png,fp ); | |
36 png_set_sig_bytes( png,8 ); | |
37 png_read_info( png,info ); | |
38 png_get_IHDR( png,info,&bf->Width,&bf->Height,&bf->BPP,&color,NULL,NULL,NULL ); | |
39 | |
40 row_p=(png_bytep *)malloc( sizeof( png_bytep ) * bf->Height ); | |
41 if ( !row_p ) | |
42 { | |
43 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[png] Not enough memory for row buffer.\n" ); | |
44 return 2; | |
45 } | |
46 bf->Image=(png_bytep)malloc( png_get_rowbytes( png,info ) * bf->Height ); | |
47 if ( !bf->Image ) | |
48 { | |
49 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[png] Not enough memory for image buffer.\n" ); | |
50 return 2; | |
51 } | |
52 for ( i=0; i < bf->Height; i++ ) row_p[i]=&bf->Image[png_get_rowbytes( png,info ) * i]; | |
53 | |
54 png_read_image( png,row_p ); | |
55 free( row_p ); | |
56 | |
57 #if 0 | |
58 if ( color == PNG_COLOR_TYPE_PALETTE ) | |
59 { | |
60 int cols; | |
61 png_get_PLTE( png,info,(png_colorp *)&palette,&cols ); | |
62 } | |
63 #endif | |
64 | |
65 if ( color&PNG_COLOR_MASK_ALPHA ) | |
66 { | |
67 if ( color&PNG_COLOR_MASK_PALETTE || color == PNG_COLOR_TYPE_GRAY_ALPHA ) bf->BPP*=2; | |
68 else bf->BPP*=4; | |
69 } | |
70 else | |
71 { | |
72 if ( color&PNG_COLOR_MASK_PALETTE || color == PNG_COLOR_TYPE_GRAY ) bf->BPP*=1; | |
73 else bf->BPP*=3; | |
74 } | |
75 | |
76 png_read_end( png,endinfo ); | |
77 png_destroy_read_struct( &png,&info,&endinfo ); | |
78 | |
79 if ( fclose( fp ) != 0 ) | |
80 { | |
81 free( bf->Image ); | |
82 free( palette ); | |
83 return 1; | |
84 } | |
85 bf->ImageSize=bf->Width * bf->Height * ( bf->BPP / 8 ); | |
86 | |
87 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[png] filename: %s.\n",fname ); | |
88 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[png] size: %dx%d bits: %d\n",bf->Width,bf->Height,bf->BPP ); | |
89 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[png] imagesize: %lu\n",bf->ImageSize ); | |
90 return 0; | |
91 } | |
92 | |
1693 | 93 int conv24to32( txSample * bf ) |
94 { | |
95 unsigned char * tmpImage; | |
96 int i,c; | |
97 | |
98 if ( bf->BPP == 24 ) | |
99 { | |
100 tmpImage=bf->Image; | |
101 bf->ImageSize=bf->Width * bf->Height * 4; | |
102 bf->BPP=32; | |
103 if ( ( bf->Image=malloc( bf->ImageSize ) ) == NULL ) | |
104 { | |
4818
3473ca9ef158
new gui interface, and gtk moved into mplayer process. fork ... bleh :)
pontscho
parents:
2717
diff
changeset
|
105 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[bitmap] Not enough memory for image.\n" ); |
1693 | 106 return 1; |
107 } | |
108 memset( bf->Image,0,bf->ImageSize ); | |
4818
3473ca9ef158
new gui interface, and gtk moved into mplayer process. fork ... bleh :)
pontscho
parents:
2717
diff
changeset
|
109 for ( c=0,i=0;i < (int)(bf->Width * bf->Height * 3); ) |
1693 | 110 { |
6054 | 111 bf->Image[c++]=tmpImage[i++]; //red |
112 bf->Image[c++]=tmpImage[i++]; //green | |
113 bf->Image[c++]=tmpImage[i++]; c++; //blue | |
1693 | 114 } |
115 free( tmpImage ); | |
116 } | |
117 return 0; | |
118 } | |
119 | |
120 void bgr2rgb( txSample * bf ) | |
121 { | |
122 unsigned char c; | |
123 int i; | |
124 | |
4818
3473ca9ef158
new gui interface, and gtk moved into mplayer process. fork ... bleh :)
pontscho
parents:
2717
diff
changeset
|
125 for ( i=0;i < (int)bf->ImageSize;i+=4 ) |
1693 | 126 { |
127 c=bf->Image[i]; | |
128 bf->Image[i]=bf->Image[i+2]; | |
129 bf->Image[i+2]=c; | |
130 } | |
131 } | |
132 | |
133 void Normalize( txSample * bf ) | |
134 { | |
135 int i; | |
6054 | 136 #ifndef WORDS_BIGENDIAN |
4818
3473ca9ef158
new gui interface, and gtk moved into mplayer process. fork ... bleh :)
pontscho
parents:
2717
diff
changeset
|
137 for ( i=0;i < (int)bf->ImageSize;i+=4 ) bf->Image[i+3]=0; |
6054 | 138 #else |
139 for ( i=0;i < (int)bf->ImageSize;i+=4 ) bf->Image[i]=0; | |
140 #endif | |
1693 | 141 } |
142 | |
143 unsigned char tmp[512]; | |
144 | |
145 unsigned char * fExist( unsigned char * fname ) | |
146 { | |
147 FILE * fl; | |
7265 | 148 unsigned char ext[][6] = { ".png\0",".PNG\0" }; |
1693 | 149 int i; |
150 | |
151 fl=fopen( fname,"rb" ); | |
152 if ( fl != NULL ) | |
153 { | |
154 fclose( fl ); | |
155 return fname; | |
156 } | |
7265 | 157 for ( i=0;i<2;i++ ) |
1693 | 158 { |
10168 | 159 snprintf( tmp,511,"%s%s",fname,ext[i] ); |
1693 | 160 fl=fopen( tmp,"rb" ); |
161 if ( fl != NULL ) | |
162 { | |
163 fclose( fl ); | |
164 return tmp; | |
165 } | |
166 } | |
167 return NULL; | |
168 } | |
169 | |
170 int bpRead( char * fname, txSample * bf ) | |
171 { | |
172 fname=fExist( fname ); | |
173 if ( fname == NULL ) return -2; | |
7265 | 174 if ( pngRead( fname,bf ) ) |
1693 | 175 { |
7265 | 176 mp_dbg( MSGT_GPLAYER,MSGL_FATAL,"[bitmap] Unknown file type ( %s ).\n",fname ); |
177 return -5; | |
1693 | 178 } |
179 if ( bf->BPP < 24 ) | |
180 { | |
10168 | 181 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[bitmap] Sorry, only 24 and 32 bpp bitmaps are supported.\n" ); |
1693 | 182 return -1; |
183 } | |
10169
46badb3d0b1c
simplyfied it and fixed some 10ls (but sadly xshape still don't works)
alex
parents:
10168
diff
changeset
|
184 if ( conv24to32( bf ) ) return -8; |
10168 | 185 #ifdef WORDS_BIGENDIAN |
10169
46badb3d0b1c
simplyfied it and fixed some 10ls (but sadly xshape still don't works)
alex
parents:
10168
diff
changeset
|
186 swab(bf->Image, bf->Image, bf->ImageSize); |
10168 | 187 #endif |
7265 | 188 bgr2rgb( bf ); |
1693 | 189 Normalize( bf ); |
190 return 0; | |
191 } | |
2717 | 192 |
193 void Convert32to1( txSample * in,txSample * out,int adaptivlimit ) | |
194 { | |
195 out->Width=in->Width; | |
196 out->Height=in->Height; | |
197 out->BPP=1; | |
7352
757e876d36fe
Off-by-one error allocating bitmap, when (width*height) % 8 != 0
jkeil
parents:
7265
diff
changeset
|
198 out->ImageSize=(out->Width * out->Height + 7) / 8; |
10169
46badb3d0b1c
simplyfied it and fixed some 10ls (but sadly xshape still don't works)
alex
parents:
10168
diff
changeset
|
199 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[c32to1] imagesize: %d\n",out->ImageSize ); |
2717 | 200 out->Image=(char *)calloc( 1,out->ImageSize ); |
10168 | 201 if ( out->Image == NULL ) mp_msg( MSGT_GPLAYER,MSGL_STATUS,"[c32to1] Not enough memory for image.\n" ); |
2717 | 202 { |
6145
26cb8736927b
Gui and 64-bit issues patch from Gui and 64-bit issues
pontscho
parents:
6054
diff
changeset
|
203 int i,b,c=0; unsigned int * buf = NULL; unsigned char tmp = 0; int nothaveshape = 1; |
26cb8736927b
Gui and 64-bit issues patch from Gui and 64-bit issues
pontscho
parents:
6054
diff
changeset
|
204 buf=(unsigned int *)in->Image; |
4818
3473ca9ef158
new gui interface, and gtk moved into mplayer process. fork ... bleh :)
pontscho
parents:
2717
diff
changeset
|
205 for ( b=0,i=0;i < (int)(out->Width * out->Height);i++ ) |
2717 | 206 { |
4818
3473ca9ef158
new gui interface, and gtk moved into mplayer process. fork ... bleh :)
pontscho
parents:
2717
diff
changeset
|
207 if ( (int)buf[i] != adaptivlimit ) tmp=( tmp >> 1 )|128; |
2717 | 208 else { tmp=tmp >> 1; buf[i]=nothaveshape=0; } |
209 if ( b++ == 7 ) { out->Image[c++]=tmp; tmp=b=0; } | |
210 } | |
211 if ( b ) out->Image[c]=tmp; | |
212 if ( nothaveshape ) { free( out->Image ); out->Image=NULL; } | |
213 } | |
214 } | |
215 | |
216 void Convert1to32( txSample * in,txSample * out ) | |
217 { | |
218 if ( in->Image == NULL ) return; | |
219 out->Width=in->Width; | |
220 out->Height=in->Height; | |
221 out->BPP=32; | |
222 out->ImageSize=out->Width * out->Height * 4; | |
223 out->Image=(char *)calloc( 1,out->ImageSize ); | |
10169
46badb3d0b1c
simplyfied it and fixed some 10ls (but sadly xshape still don't works)
alex
parents:
10168
diff
changeset
|
224 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[c1to32] imagesize: %d\n",out->ImageSize ); |
10168 | 225 if ( out->Image == NULL ) mp_msg( MSGT_GPLAYER,MSGL_STATUS,"[c1to32] Not enough memory for image.\n" ); |
2717 | 226 { |
6145
26cb8736927b
Gui and 64-bit issues patch from Gui and 64-bit issues
pontscho
parents:
6054
diff
changeset
|
227 int i,b,c=0; unsigned int * buf = NULL; unsigned char tmp = 0; |
26cb8736927b
Gui and 64-bit issues patch from Gui and 64-bit issues
pontscho
parents:
6054
diff
changeset
|
228 buf=(unsigned int *)out->Image; |
4818
3473ca9ef158
new gui interface, and gtk moved into mplayer process. fork ... bleh :)
pontscho
parents:
2717
diff
changeset
|
229 for ( c=0,i=0;i < (int)(in->Width * in->Height / 8);i++ ) |
2717 | 230 { |
231 tmp=in->Image[i]; | |
232 for ( b=0;b<8;b++ ) | |
233 { | |
234 buf[c]=0; | |
235 if ( tmp&0x1 ) buf[c]=0xffffffff; | |
236 c++; tmp=tmp>>1; | |
237 } | |
238 } | |
239 } | |
240 } |