annotate Gui/bitmap/bitmap.c @ 2365:93616490c0dd

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