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