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