Mercurial > mplayer.hg
annotate Gui/bitmap.c @ 5522:545c13161589
swscale filter
author | arpi |
---|---|
date | Sun, 07 Apr 2002 20:21:37 +0000 |
parents | 3473ca9ef158 |
children | 35689e1e6745 |
rev | line source |
---|---|
1693 | 1 |
2080 | 2 #include <stdio.h> |
1693 | 3 #include <stdlib.h> |
2080 | 4 #include <string.h> |
1693 | 5 |
6 #include "bitmap.h" | |
7 | |
8 #define BMP 1 | |
9 #define TGA 2 | |
10 #define PNG 3 | |
11 #define TGAPACKED 4 | |
12 | |
13 extern char * strcat( char * dest,const char * src ); | |
14 | |
15 int conv24to32( txSample * bf ) | |
16 { | |
17 unsigned char * tmpImage; | |
18 int i,c; | |
19 | |
20 if ( bf->BPP == 24 ) | |
21 { | |
22 tmpImage=bf->Image; | |
23 bf->ImageSize=bf->Width * bf->Height * 4; | |
24 bf->BPP=32; | |
25 if ( ( bf->Image=malloc( bf->ImageSize ) ) == NULL ) | |
26 { | |
4818
3473ca9ef158
new gui interface, and gtk moved into mplayer process. fork ... bleh :)
pontscho
parents:
2717
diff
changeset
|
27 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[bitmap] Not enough memory for image.\n" ); |
1693 | 28 return 1; |
29 } | |
30 memset( bf->Image,0,bf->ImageSize ); | |
4818
3473ca9ef158
new gui interface, and gtk moved into mplayer process. fork ... bleh :)
pontscho
parents:
2717
diff
changeset
|
31 for ( c=0,i=0;i < (int)(bf->Width * bf->Height * 3); ) |
1693 | 32 { |
33 bf->Image[c++]=tmpImage[i++]; | |
34 bf->Image[c++]=tmpImage[i++]; | |
35 bf->Image[c++]=tmpImage[i++]; c++; | |
36 } | |
37 free( tmpImage ); | |
38 } | |
39 return 0; | |
40 } | |
41 | |
42 void bgr2rgb( txSample * bf ) | |
43 { | |
44 unsigned char c; | |
45 int i; | |
46 | |
4818
3473ca9ef158
new gui interface, and gtk moved into mplayer process. fork ... bleh :)
pontscho
parents:
2717
diff
changeset
|
47 for ( i=0;i < (int)bf->ImageSize;i+=4 ) |
1693 | 48 { |
49 c=bf->Image[i]; | |
50 bf->Image[i]=bf->Image[i+2]; | |
51 bf->Image[i+2]=c; | |
52 } | |
53 } | |
54 | |
55 void Normalize( txSample * bf ) | |
56 { | |
57 int i; | |
4818
3473ca9ef158
new gui interface, and gtk moved into mplayer process. fork ... bleh :)
pontscho
parents:
2717
diff
changeset
|
58 for ( i=0;i < (int)bf->ImageSize;i+=4 ) bf->Image[i+3]=0; |
1693 | 59 } |
60 | |
61 unsigned char tmp[512]; | |
62 | |
63 unsigned char * fExist( unsigned char * fname ) | |
64 { | |
65 FILE * fl; | |
66 unsigned char ext[][6] = { ".tga\0",".TGA\0",".png\0",".PNG\0",".bmp\0",".BMP\0" }; | |
67 int i; | |
68 | |
69 fl=fopen( fname,"rb" ); | |
70 if ( fl != NULL ) | |
71 { | |
72 fclose( fl ); | |
73 return fname; | |
74 } | |
75 for ( i=0;i<10;i++ ) | |
76 { | |
77 strcpy( tmp,fname ); | |
78 strcat( tmp,ext[i] ); | |
79 fl=fopen( tmp,"rb" ); | |
80 if ( fl != NULL ) | |
81 { | |
82 fclose( fl ); | |
83 return tmp; | |
84 } | |
85 } | |
86 return NULL; | |
87 } | |
88 | |
89 int aComp( unsigned char * b1,unsigned char * b2,int size ) | |
90 { | |
91 int i; | |
92 for( i=0;i<size;i++ ) if ( b1[i] != b2[i] ) return 0; | |
93 return 1; | |
94 } | |
95 | |
96 int GetFileType( char * fname ) | |
97 { | |
98 FILE * fl; | |
99 unsigned char buffer[10]; | |
100 unsigned char bmp[2] = { 0x42,0x4d }; | |
101 unsigned char tga[7] = { 0x00,0x02,0x00,0x00,0x00,0x00,0x00 }; | |
102 unsigned char ptga[7] = { 0x00,0x0a,0x00,0x00,0x00,0x00,0x00 }; | |
103 unsigned char png[8] = { 0x89,0x50,0x4e,0x47,0x0d,0x0a,0x1a,0x0a }; | |
104 | |
105 if ( ( fl=fopen( fname,"rb" ) ) == NULL ) return -1; | |
106 fread( buffer,1,10,fl ); | |
107 fclose( fl ); | |
108 | |
109 if ( aComp( buffer,bmp,2 ) ) return BMP; // --- bmp | |
110 if ( aComp( &buffer[1],tga,8 ) ) return TGA; // --- tga | |
111 if ( aComp( &buffer[1],ptga,7 ) ) return TGAPACKED; // --- tga | |
112 if ( aComp( buffer,png,8 ) ) return PNG; // --- png | |
113 return 0; // --- others | |
114 } | |
115 | |
116 int bpRead( char * fname, txSample * bf ) | |
117 { | |
118 int bgr = 0; | |
119 int i; | |
120 | |
121 fname=fExist( fname ); | |
122 if ( fname == NULL ) return -2; | |
123 switch ( GetFileType( fname ) ) | |
124 { | |
125 case BMP: | |
126 i=bmpRead( fname,bf ); | |
127 switch ( i ) | |
128 { | |
129 case 0: break; | |
130 case 3: return -1; | |
131 default: return -3; | |
132 } | |
133 break; | |
134 case TGA: | |
135 i=tgaRead( fname,bf ); | |
136 switch ( i ) | |
137 { | |
138 case 0: break; | |
139 case 3: return -1; | |
140 default: return -4; | |
141 } | |
142 break; | |
143 case PNG: | |
144 if ( pngRead( fname,bf ) ) return -5; | |
145 bgr=1; | |
146 break; | |
147 case TGAPACKED: | |
4818
3473ca9ef158
new gui interface, and gtk moved into mplayer process. fork ... bleh :)
pontscho
parents:
2717
diff
changeset
|
148 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[bitmap] sorry, packed TGA not supported.\n" ); |
1693 | 149 return -6; |
150 default: | |
151 { | |
4818
3473ca9ef158
new gui interface, and gtk moved into mplayer process. fork ... bleh :)
pontscho
parents:
2717
diff
changeset
|
152 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[bitmap] Unknown file type ( %s ).\n",fname ); |
1693 | 153 return -7; |
154 } | |
155 } | |
156 if ( bf->BPP < 24 ) | |
157 { | |
4818
3473ca9ef158
new gui interface, and gtk moved into mplayer process. fork ... bleh :)
pontscho
parents:
2717
diff
changeset
|
158 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[bitmap] sorry, 16 or less bitmaps not supported.\n" ); |
1693 | 159 return -1; |
160 } | |
161 if ( conv24to32( bf ) ) return -8; | |
162 if ( bgr ) bgr2rgb( bf ); | |
163 Normalize( bf ); | |
164 return 0; | |
165 } | |
2717 | 166 |
167 void Convert32to1( txSample * in,txSample * out,int adaptivlimit ) | |
168 { | |
169 out->Width=in->Width; | |
170 out->Height=in->Height; | |
171 out->BPP=1; | |
172 out->ImageSize=out->Width * out->Height / 8; | |
4818
3473ca9ef158
new gui interface, and gtk moved into mplayer process. fork ... bleh :)
pontscho
parents:
2717
diff
changeset
|
173 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[c1to32] imagesize: %d\n",out->ImageSize ); |
2717 | 174 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
|
175 if ( out->Image == NULL ) mp_msg( MSGT_GPLAYER,MSGL_STATUS,"nem van ram baze\n" ); |
2717 | 176 { |
177 int i,b,c=0; unsigned long * buf = NULL; unsigned char tmp = 0; int nothaveshape = 1; | |
178 buf=(unsigned long *)in->Image; | |
4818
3473ca9ef158
new gui interface, and gtk moved into mplayer process. fork ... bleh :)
pontscho
parents:
2717
diff
changeset
|
179 for ( b=0,i=0;i < (int)(out->Width * out->Height);i++ ) |
2717 | 180 { |
4818
3473ca9ef158
new gui interface, and gtk moved into mplayer process. fork ... bleh :)
pontscho
parents:
2717
diff
changeset
|
181 if ( (int)buf[i] != adaptivlimit ) tmp=( tmp >> 1 )|128; |
2717 | 182 else { tmp=tmp >> 1; buf[i]=nothaveshape=0; } |
183 if ( b++ == 7 ) { out->Image[c++]=tmp; tmp=b=0; } | |
184 } | |
185 if ( b ) out->Image[c]=tmp; | |
186 if ( nothaveshape ) { free( out->Image ); out->Image=NULL; } | |
187 } | |
188 } | |
189 | |
190 void Convert1to32( txSample * in,txSample * out ) | |
191 { | |
192 if ( in->Image == NULL ) return; | |
193 out->Width=in->Width; | |
194 out->Height=in->Height; | |
195 out->BPP=32; | |
196 out->ImageSize=out->Width * out->Height * 4; | |
197 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
|
198 mp_dbg( MSGT_GPLAYER,MSGL_DBG2,"[c32to1] imagesize: %d\n",out->ImageSize ); |
3473ca9ef158
new gui interface, and gtk moved into mplayer process. fork ... bleh :)
pontscho
parents:
2717
diff
changeset
|
199 if ( (int)out->Image == NULL ) mp_msg( MSGT_GPLAYER,MSGL_STATUS,"nem van ram baze\n" ); |
2717 | 200 { |
201 int i,b,c=0; unsigned long * buf = NULL; unsigned char tmp = 0; | |
202 buf=(unsigned long *)out->Image; | |
4818
3473ca9ef158
new gui interface, and gtk moved into mplayer process. fork ... bleh :)
pontscho
parents:
2717
diff
changeset
|
203 for ( c=0,i=0;i < (int)(in->Width * in->Height / 8);i++ ) |
2717 | 204 { |
205 tmp=in->Image[i]; | |
206 for ( b=0;b<8;b++ ) | |
207 { | |
208 buf[c]=0; | |
209 if ( tmp&0x1 ) buf[c]=0xffffffff; | |
210 c++; tmp=tmp>>1; | |
211 } | |
212 } | |
213 } | |
214 } |