annotate Gui/bitmap/tga/tga.c @ 5580:6366586083bb

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