1693
|
1
|
|
2 #include <string.h>
|
|
3 #include <stdio.h>
|
|
4 #include <stdlib.h>
|
|
5
|
|
6 #include "tga.h"
|
|
7 #include "../../error.h"
|
|
8
|
|
9 int tgaRead( char * filename,txSample * bf )
|
|
10 {
|
|
11 FILE * BMP;
|
|
12 unsigned long i;
|
|
13 char tmp[255];
|
|
14 unsigned char * line;
|
|
15 int linesize;
|
|
16 char * comment;
|
|
17 tgaHeadert tgaHeader;
|
|
18
|
|
19 strcpy( tmp,filename );
|
|
20 if ( !strstr( tmp,".tga" ) ) strcat( tmp,".tga" );
|
|
21 if ( (BMP=fopen( tmp,"rb" )) == NULL )
|
|
22 {
|
|
23 #ifdef DEBUG
|
|
24 dbprintf( 4,"[tga] File not found ( %s ).\n",tmp );
|
|
25 #endif
|
|
26 return 1;
|
|
27 }
|
|
28 if ( (i=fread( &tgaHeader,sizeof( tgaHeader ),1,BMP )) != 1 )
|
|
29 {
|
|
30 #ifdef DEBUG
|
|
31 dbprintf( 4,"[tga] Header read error ( %s ).\n",tmp );
|
|
32 #endif
|
|
33 return 2;
|
|
34 }
|
|
35 if ( tgaHeader.depth < 24 )
|
|
36 {
|
|
37 #ifdef DEBUG
|
|
38 dbprintf( 4,"[tga] Sorry, this loader not supported 16 bit or less ...\n" );
|
|
39 #endif
|
|
40 return 3;
|
|
41 }
|
|
42 bf->Width=tgaHeader.sx;
|
|
43 bf->Height=tgaHeader.sy;
|
|
44 bf->BPP=tgaHeader.depth;
|
|
45 bf->ImageSize=bf->Width * bf->Height * ( bf->BPP / 8 );
|
|
46
|
|
47 if ( ( bf->Image=malloc( bf->ImageSize ) ) == NULL )
|
|
48 {
|
|
49 #ifdef DEBUG
|
|
50 dbprintf( 4,"[tga] Not enough memory for image buffer.\n" );
|
|
51 #endif
|
|
52 return 4;
|
|
53 }
|
|
54
|
|
55 comment=NULL;
|
|
56 if ( tgaHeader.tmp[0] != 0 )
|
|
57 {
|
|
58 if ( ( comment=malloc( tgaHeader.tmp[0] + 1 ) ) == NULL )
|
|
59 {
|
|
60 #ifdef DEBUG
|
|
61 dbprintf( 4,"[tga] Not enough memory for comment string.\n" );
|
|
62 #endif
|
|
63 return 5;
|
|
64 }
|
|
65 memset( comment,0,tgaHeader.tmp[0] + 1 );
|
|
66 if ( fread( comment,tgaHeader.tmp[0],1,BMP ) != 1 )
|
|
67 {
|
|
68 #ifdef DEBUG
|
|
69 dbprintf( 4,"[tga] Comment read error.\n" );
|
|
70 #endif
|
|
71 return 6;
|
|
72 }
|
|
73 }
|
|
74
|
|
75 #ifdef DEBUG
|
|
76 dbprintf( 4,"[tga] filename ( read ): %s\n",tmp );
|
|
77 dbprintf( 4,"[tga] size: %dx%d bits: %d\n",bf->Width,bf->Height,bf->BPP );
|
|
78 dbprintf( 4,"[tga] imagesize: %lu\n",bf->ImageSize );
|
|
79 if ( comment ) dbprintf( 4,"[tga] comment: %s\n",comment );
|
|
80 #endif
|
|
81
|
|
82 if ( comment ) free( comment );
|
|
83
|
|
84 if ( fread( bf->Image,bf->ImageSize,1,BMP ) != 1 )
|
|
85 {
|
|
86 #ifdef DEBUG
|
|
87 dbprintf( 4,"[tga] Image read error.\n" );
|
|
88 #endif
|
|
89 return 7;
|
|
90 }
|
|
91
|
|
92 fclose( BMP );
|
|
93
|
|
94 if ( tgaHeader.ctmp == 0 )
|
|
95 {
|
|
96 linesize=bf->Width * ( bf->BPP / 8 );
|
|
97 if ( (line=malloc( linesize )) == NULL )
|
|
98 {
|
|
99 #ifdef DEBUG
|
|
100 dbprintf( 4,"[tga] Not enough memory for flipping.\n" );
|
|
101 #endif
|
|
102 return 8;
|
|
103 }
|
|
104
|
|
105 for ( i=0;i < bf->Height / 2;i++ )
|
|
106 {
|
|
107 memcpy( line,&bf->Image[ i * linesize ],linesize );
|
|
108 memcpy( &bf->Image[ i * linesize ],&bf->Image[ ( bf->Height - i - 1 ) * linesize ],linesize );
|
|
109 memcpy( &bf->Image[ ( bf->Height - i - 1 ) * linesize ],line,linesize );
|
|
110 }
|
|
111 free( line );
|
|
112 }
|
|
113
|
|
114 return 0;
|
|
115 }
|
|
116
|
|
117 char comment[] = "fresh!mindworkz's TGA Filter. v0.1";
|
|
118
|
|
119 void tgaWriteTexture( char * filename,txSample * bf )
|
|
120 {
|
|
121 FILE * BMP;
|
|
122 int i;
|
|
123 unsigned char * line;
|
|
124 int linesize;
|
|
125 tgaHeadert tgaHeader;
|
|
126 char tmp[255];
|
|
127
|
|
128 strcpy( tmp,filename );
|
|
129 if ( !strstr( tmp,".tga" ) ) strcat( tmp,".tga" );
|
|
130 if ( ( BMP=fopen( tmp,"wb+" ) ) == NULL )
|
|
131 {
|
|
132 dbprintf( 0,"[tga] File not open ( %s ).\n",tmp );
|
|
133 exit( 0 );
|
|
134 }
|
|
135 memset( &tgaHeader,0,sizeof( tgaHeader ) );
|
|
136 tgaHeader.sx=bf->Width;
|
|
137 tgaHeader.sy=bf->Height;
|
|
138 tgaHeader.depth=bf->BPP;
|
|
139 tgaHeader.ctmp=0;
|
|
140 tgaHeader.tmp[0]=strlen( comment );
|
|
141 if ( bf->BPP != 8 ) tgaHeader.tmp[2]=2;
|
|
142 else tgaHeader.tmp[2]=3;
|
|
143
|
|
144 #ifdef DEBUG
|
|
145 dbprintf( 4,"\n[tga] filename ( write ): %s\n",tmp );
|
|
146 dbprintf( 4,"[tga] size: %dx%d\n",bf->Width,bf->Height );
|
|
147 dbprintf( 4,"[tga] bits: %d\n",bf->BPP );
|
|
148 dbprintf( 4,"[tga] imagesize: %lu\n",bf->ImageSize );
|
|
149 dbprintf( 4,"[tga] comment: %s\n",comment );
|
|
150 dbprintf( 4,"\n" );
|
|
151 #endif
|
|
152
|
|
153 if ( tgaHeader.ctmp == 0 )
|
|
154 {
|
|
155 linesize=bf->Width * ( bf->BPP / 8 );
|
|
156 if ( (line=malloc( linesize )) == NULL )
|
|
157 {
|
|
158 dbprintf( 0,"[tga] Not enough memory for flipping.\n" );
|
|
159 exit( 0 );
|
|
160 }
|
|
161
|
|
162 for ( i=0;i < bf->Height / 2;i++ )
|
|
163 {
|
|
164 memcpy( line,&bf->Image[ i * linesize ],linesize );
|
|
165 memcpy( &bf->Image[ i * linesize ],&bf->Image[ ( bf->Height - i - 1 ) * linesize ],linesize );
|
|
166 memcpy( &bf->Image[ ( bf->Height - i - 1 ) * linesize ],line,linesize );
|
|
167 }
|
|
168 free( line );
|
|
169 }
|
|
170
|
|
171 fwrite( &tgaHeader,sizeof( tgaHeader ),1,BMP );
|
|
172 fwrite( comment,strlen( comment ),1,BMP );
|
|
173 fwrite( bf->Image,bf->ImageSize,1,BMP );
|
|
174
|
|
175 fclose( BMP );
|
|
176 }
|
|
177
|
|
178 void tgaWriteBuffer( char * fname,unsigned char * Buffer,int sx,int sy,int BPP )
|
|
179 {
|
|
180 txSample tmp;
|
|
181
|
|
182 memset( &tmp,0,sizeof( tmp ) );
|
|
183 tmp.Width=sx;
|
|
184 tmp.Height=sy;
|
|
185 tmp.BPP=BPP;
|
|
186 tmp.ImageSize=sx * sy * ( BPP / 8 );
|
|
187 tmp.Image=Buffer;
|
|
188 tgaWriteTexture( fname,&tmp );
|
2082
|
189 }
|
|
190
|