annotate Gui/bitmap/tga/tga.c @ 3866:50d7eec1d4b9

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