Mercurial > mplayer.hg
annotate Gui/bitmap.c @ 9367:f107fcf6fdea
10l
author | jonas |
---|---|
date | Sun, 09 Feb 2003 13:13:00 +0000 |
parents | 30a9a6358ee9 |
children | 22572f1dafa3 |
rev | line source |
---|---|
1693 | 1 |
2080 | 2 #include <stdio.h> |
1693 | 3 #include <stdlib.h> |
2080 | 4 #include <string.h> |
1693 | 5 |
8046 | 6 #include <png.h> |
7 | |
8 #include "../../mp_msg.h" | |
1693 | 9 #include "bitmap.h" |
10 | |
8046 | 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=(png_bytep *)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 | |
1693 | 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 { | |
4818
3473ca9ef158
new gui interface, and gtk moved into mplayer process. fork ... bleh :)
pontscho
parents:
2717
diff
changeset
|
106 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[bitmap] Not enough memory for image.\n" ); |
1693 | 107 return 1; |
108 } | |
109 memset( bf->Image,0,bf->ImageSize ); | |
4818
3473ca9ef158
new gui interface, and gtk moved into mplayer process. fork ... bleh :)
pontscho
parents:
2717
diff
changeset
|
110 for ( c=0,i=0;i < (int)(bf->Width * bf->Height * 3); ) |
1693 | 111 { |
6054 | 112 #ifndef WORDS_BIGENDIAN |
113 bf->Image[c++]=tmpImage[i++]; //red | |
114 bf->Image[c++]=tmpImage[i++]; //green | |
115 bf->Image[c++]=tmpImage[i++]; c++; //blue | |
116 #else | |
117 unsigned char t=tmpImage[i++]; | |
118 bf->Image[c++]=tmpImage[i++]; //green | |
119 bf->Image[c++]=t; c++; //red | |
120 bf->Image[c++]=tmpImage[i++]; //blue | |
121 #endif | |
1693 | 122 } |
123 free( tmpImage ); | |
124 } | |
125 return 0; | |
126 } | |
127 | |
128 void bgr2rgb( txSample * bf ) | |
129 { | |
130 unsigned char c; | |
131 int i; | |
132 | |
4818
3473ca9ef158
new gui interface, and gtk moved into mplayer process. fork ... bleh :)
pontscho
parents:
2717
diff
changeset
|
133 for ( i=0;i < (int)bf->ImageSize;i+=4 ) |
1693 | 134 { |
135 c=bf->Image[i]; | |
136 bf->Image[i]=bf->Image[i+2]; | |
137 bf->Image[i+2]=c; | |
138 } | |
139 } | |
140 | |
141 void Normalize( txSample * bf ) | |
142 { | |
143 int i; | |
6054 | 144 #ifndef WORDS_BIGENDIAN |
4818
3473ca9ef158
new gui interface, and gtk moved into mplayer process. fork ... bleh :)
pontscho
parents:
2717
diff
changeset
|
145 for ( i=0;i < (int)bf->ImageSize;i+=4 ) bf->Image[i+3]=0; |
6054 | 146 #else |
147 for ( i=0;i < (int)bf->ImageSize;i+=4 ) bf->Image[i]=0; | |
148 #endif | |
1693 | 149 } |
150 | |
151 unsigned char tmp[512]; | |
152 | |
153 unsigned char * fExist( unsigned char * fname ) | |
154 { | |
155 FILE * fl; | |
7265 | 156 unsigned char ext[][6] = { ".png\0",".PNG\0" }; |
1693 | 157 int i; |
158 | |
159 fl=fopen( fname,"rb" ); | |
160 if ( fl != NULL ) | |
161 { | |
162 fclose( fl ); | |
163 return fname; | |
164 } | |
7265 | 165 for ( i=0;i<2;i++ ) |
1693 | 166 { |
7265 | 167 sprintf( tmp,"%s%s",fname,ext[i] ); |
1693 | 168 fl=fopen( tmp,"rb" ); |
169 if ( fl != NULL ) | |
170 { | |
171 fclose( fl ); | |
172 return tmp; | |
173 } | |
174 } | |
175 return NULL; | |
176 } | |
177 | |
178 int bpRead( char * fname, txSample * bf ) | |
179 { | |
180 fname=fExist( fname ); | |
181 if ( fname == NULL ) return -2; | |
7265 | 182 if ( pngRead( fname,bf ) ) |
1693 | 183 { |
7265 | 184 mp_dbg( MSGT_GPLAYER,MSGL_FATAL,"[bitmap] Unknown file type ( %s ).\n",fname ); |
185 return -5; | |
1693 | 186 } |
187 if ( bf->BPP < 24 ) | |
188 { | |
7265 | 189 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[bitmap] sorry, 16 or less bitmaps not supported.\n" ); |
1693 | 190 return -1; |
191 } | |
192 if ( conv24to32( bf ) ) return -8; | |
7265 | 193 bgr2rgb( bf ); |
1693 | 194 Normalize( bf ); |
195 return 0; | |
196 } | |
2717 | 197 |
198 void Convert32to1( txSample * in,txSample * out,int adaptivlimit ) | |
199 { | |
200 out->Width=in->Width; | |
201 out->Height=in->Height; | |
202 out->BPP=1; | |
7352
757e876d36fe
Off-by-one error allocating bitmap, when (width*height) % 8 != 0
jkeil
parents:
7265
diff
changeset
|
203 out->ImageSize=(out->Width * out->Height + 7) / 8; |
4818
3473ca9ef158
new gui interface, and gtk moved into mplayer process. fork ... bleh :)
pontscho
parents:
2717
diff
changeset
|
204 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[c1to32] imagesize: %d\n",out->ImageSize ); |
2717 | 205 out->Image=(char *)calloc( 1,out->ImageSize ); |
4818
3473ca9ef158
new gui interface, and gtk moved into mplayer process. fork ... bleh :)
pontscho
parents:
2717
diff
changeset
|
206 if ( out->Image == NULL ) mp_msg( MSGT_GPLAYER,MSGL_STATUS,"nem van ram baze\n" ); |
2717 | 207 { |
6145
26cb8736927b
Gui and 64-bit issues patch from Gui and 64-bit issues
pontscho
parents:
6054
diff
changeset
|
208 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
|
209 buf=(unsigned int *)in->Image; |
4818
3473ca9ef158
new gui interface, and gtk moved into mplayer process. fork ... bleh :)
pontscho
parents:
2717
diff
changeset
|
210 for ( b=0,i=0;i < (int)(out->Width * out->Height);i++ ) |
2717 | 211 { |
4818
3473ca9ef158
new gui interface, and gtk moved into mplayer process. fork ... bleh :)
pontscho
parents:
2717
diff
changeset
|
212 if ( (int)buf[i] != adaptivlimit ) tmp=( tmp >> 1 )|128; |
2717 | 213 else { tmp=tmp >> 1; buf[i]=nothaveshape=0; } |
214 if ( b++ == 7 ) { out->Image[c++]=tmp; tmp=b=0; } | |
215 } | |
216 if ( b ) out->Image[c]=tmp; | |
217 if ( nothaveshape ) { free( out->Image ); out->Image=NULL; } | |
218 } | |
219 } | |
220 | |
221 void Convert1to32( txSample * in,txSample * out ) | |
222 { | |
223 if ( in->Image == NULL ) return; | |
224 out->Width=in->Width; | |
225 out->Height=in->Height; | |
226 out->BPP=32; | |
227 out->ImageSize=out->Width * out->Height * 4; | |
228 out->Image=(char *)calloc( 1,out->ImageSize ); | |
4818
3473ca9ef158
new gui interface, and gtk moved into mplayer process. fork ... bleh :)
pontscho
parents:
2717
diff
changeset
|
229 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[c32to1] imagesize: %d\n",out->ImageSize ); |
6159
dcf195b784bf
applied 64bit patch from Ulrich Hecht <uli@suse.de>
pontscho
parents:
6145
diff
changeset
|
230 if ( out->Image == NULL ) mp_msg( MSGT_GPLAYER,MSGL_STATUS,"nem van ram baze\n" ); |
2717 | 231 { |
6145
26cb8736927b
Gui and 64-bit issues patch from Gui and 64-bit issues
pontscho
parents:
6054
diff
changeset
|
232 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
|
233 buf=(unsigned int *)out->Image; |
4818
3473ca9ef158
new gui interface, and gtk moved into mplayer process. fork ... bleh :)
pontscho
parents:
2717
diff
changeset
|
234 for ( c=0,i=0;i < (int)(in->Width * in->Height / 8);i++ ) |
2717 | 235 { |
236 tmp=in->Image[i]; | |
237 for ( b=0;b<8;b++ ) | |
238 { | |
239 buf[c]=0; | |
240 if ( tmp&0x1 ) buf[c]=0xffffffff; | |
241 c++; tmp=tmp>>1; | |
242 } | |
243 } | |
244 } | |
245 } |