annotate Gui/bitmap.c @ 1976:4c73c398bdcb

2 typos fixed
author gabucino
date Wed, 26 Sep 2001 23:57:41 +0000
parents d237c5d4b216
children f91ad6d23ce9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1693
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
1
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
2 #include <stdlib.h>
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
3 #include <stdio.h>
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
4
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
5 #include "bitmap.h"
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
6 #include "../error.h"
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
7
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
8 #define BMP 1
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
9 #define TGA 2
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
10 #define PNG 3
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
11 #define TGAPACKED 4
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
12
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
13 extern char * strcat( char * dest,const char * src );
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
14
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
15 int conv24to32( txSample * bf )
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
16 {
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
17 unsigned char * tmpImage;
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
18 int i,c;
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
19
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
20 if ( bf->BPP == 24 )
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
21 {
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
22 tmpImage=bf->Image;
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
23 bf->ImageSize=bf->Width * bf->Height * 4;
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
24 bf->BPP=32;
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
25 if ( ( bf->Image=malloc( bf->ImageSize ) ) == NULL )
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
26 {
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
27 #ifdef DEBUG
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
28 dbprintf( 4,"[bitmap] Not enough memory for image.\n" );
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
29 #endif
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
30 return 1;
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
31 }
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
32 memset( bf->Image,0,bf->ImageSize );
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
33 for ( c=0,i=0;i < bf->Width * bf->Height * 3; )
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
34 {
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
35 bf->Image[c++]=tmpImage[i++];
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
36 bf->Image[c++]=tmpImage[i++];
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
37 bf->Image[c++]=tmpImage[i++]; c++;
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
38 }
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
39 free( tmpImage );
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
40 }
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
41 return 0;
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
42 }
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
43
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
44 void bgr2rgb( txSample * bf )
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
45 {
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
46 unsigned char c;
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
47 int i;
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
48
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
49 for ( i=0;i < bf->ImageSize;i+=4 )
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
50 {
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
51 c=bf->Image[i];
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
52 bf->Image[i]=bf->Image[i+2];
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
53 bf->Image[i+2]=c;
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
54 }
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
55 }
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
56
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
57 void Normalize( txSample * bf )
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
58 {
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
59 int i;
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
60
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
61 for ( i=0;i < bf->ImageSize;i+=4 ) bf->Image[i+3]=0;
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
62 }
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
63
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
64 unsigned char tmp[512];
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
65
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
66 unsigned char * fExist( unsigned char * fname )
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
67 {
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
68 FILE * fl;
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
69 unsigned char ext[][6] = { ".tga\0",".TGA\0",".png\0",".PNG\0",".bmp\0",".BMP\0" };
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
70 int i;
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
71
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
72 fl=fopen( fname,"rb" );
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
73 if ( fl != NULL )
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
74 {
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
75 fclose( fl );
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
76 return fname;
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
77 }
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
78 for ( i=0;i<10;i++ )
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
79 {
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
80 strcpy( tmp,fname );
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
81 strcat( tmp,ext[i] );
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
82 fl=fopen( tmp,"rb" );
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
83 if ( fl != NULL )
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
84 {
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
85 fclose( fl );
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
86 return tmp;
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
87 }
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
88 }
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
89 return NULL;
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
90 }
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
91
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
92 int aComp( unsigned char * b1,unsigned char * b2,int size )
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
93 {
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
94 int i;
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
95 for( i=0;i<size;i++ ) if ( b1[i] != b2[i] ) return 0;
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
96 return 1;
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
97 }
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
98
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
99 int GetFileType( char * fname )
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
100 {
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
101 FILE * fl;
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
102 unsigned char buffer[10];
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
103 unsigned char bmp[2] = { 0x42,0x4d };
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
104 unsigned char tga[7] = { 0x00,0x02,0x00,0x00,0x00,0x00,0x00 };
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
105 unsigned char ptga[7] = { 0x00,0x0a,0x00,0x00,0x00,0x00,0x00 };
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
106 unsigned char png[8] = { 0x89,0x50,0x4e,0x47,0x0d,0x0a,0x1a,0x0a };
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
107
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
108 if ( ( fl=fopen( fname,"rb" ) ) == NULL ) return -1;
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
109 fread( buffer,1,10,fl );
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
110 fclose( fl );
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
111
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
112 if ( aComp( buffer,bmp,2 ) ) return BMP; // --- bmp
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
113 if ( aComp( &buffer[1],tga,8 ) ) return TGA; // --- tga
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
114 if ( aComp( &buffer[1],ptga,7 ) ) return TGAPACKED; // --- tga
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
115 if ( aComp( buffer,png,8 ) ) return PNG; // --- png
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
116 return 0; // --- others
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
117 }
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
118
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
119 int bpRead( char * fname, txSample * bf )
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
120 {
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
121 int bgr = 0;
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
122 int i;
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
123
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
124 fname=fExist( fname );
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
125 if ( fname == NULL ) return -2;
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
126 switch ( GetFileType( fname ) )
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
127 {
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
128 case BMP:
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
129 i=bmpRead( fname,bf );
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
130 switch ( i )
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
131 {
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
132 case 0: break;
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
133 case 3: return -1;
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
134 default: return -3;
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
135 }
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
136 break;
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
137 case TGA:
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
138 i=tgaRead( fname,bf );
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
139 switch ( i )
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
140 {
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
141 case 0: break;
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
142 case 3: return -1;
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
143 default: return -4;
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
144 }
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
145 break;
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
146 case PNG:
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
147 if ( pngRead( fname,bf ) ) return -5;
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
148 bgr=1;
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
149 break;
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
150 case TGAPACKED:
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
151 #ifdef DEBUG
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
152 dbprintf( 4,"[bitmap] sorry, packed TGA not supported.\n" );
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
153 #endif
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
154 return -6;
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
155 default:
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
156 {
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
157 #ifdef DEBUG
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
158 dbprintf( 4,"[bitmap] Unknown file type ( %s ).\n",fname );
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
159 #endif
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
160 return -7;
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
161 }
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
162 }
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
163 if ( bf->BPP < 24 )
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
164 {
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
165 #ifdef DEBUG
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
166 dbprintf( 4,"[bitmap] sorry, 16 or less bitmaps not supported.\n" );
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
167 #endif
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
168 return -1;
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
169 }
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
170 if ( conv24to32( bf ) ) return -8;
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
171 if ( bgr ) bgr2rgb( bf );
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
172 Normalize( bf );
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
173 return 0;
d237c5d4b216 GUI version n-1
arpi
parents:
diff changeset
174 }