Mercurial > mplayer.hg
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 |
rev | line source |
---|---|
1693 | 1 |
2 #include <string.h> | |
3 #include <stdio.h> | |
4 #include <stdlib.h> | |
5 | |
6 #include "tga.h" | |
7 | |
8 int tgaRead( char * filename,txSample * bf ) | |
9 { | |
10 FILE * BMP; | |
11 unsigned long i; | |
12 char tmp[255]; | |
13 unsigned char * line; | |
14 int linesize; | |
15 char * comment; | |
16 tgaHeadert tgaHeader; | |
17 | |
18 strcpy( tmp,filename ); | |
19 if ( !strstr( tmp,".tga" ) ) strcat( tmp,".tga" ); | |
20 if ( (BMP=fopen( tmp,"rb" )) == NULL ) | |
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 | 23 return 1; |
24 } | |
25 if ( (i=fread( &tgaHeader,sizeof( tgaHeader ),1,BMP )) != 1 ) | |
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 | 28 return 2; |
29 } | |
30 if ( tgaHeader.depth < 24 ) | |
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 | 33 return 3; |
34 } | |
35 bf->Width=tgaHeader.sx; | |
36 bf->Height=tgaHeader.sy; | |
37 bf->BPP=tgaHeader.depth; | |
38 bf->ImageSize=bf->Width * bf->Height * ( bf->BPP / 8 ); | |
39 | |
40 if ( ( bf->Image=malloc( bf->ImageSize ) ) == NULL ) | |
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 | 43 return 4; |
44 } | |
45 | |
46 comment=NULL; | |
47 if ( tgaHeader.tmp[0] != 0 ) | |
48 { | |
49 if ( ( comment=malloc( tgaHeader.tmp[0] + 1 ) ) == NULL ) | |
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 | 52 return 5; |
53 } | |
54 memset( comment,0,tgaHeader.tmp[0] + 1 ); | |
55 if ( fread( comment,tgaHeader.tmp[0],1,BMP ) != 1 ) | |
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 | 59 } |
60 } | |
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 | 67 #endif |
68 | |
69 if ( comment ) free( comment ); | |
70 | |
71 if ( fread( bf->Image,bf->ImageSize,1,BMP ) != 1 ) | |
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 | 74 return 7; |
75 } | |
76 | |
77 fclose( BMP ); | |
78 | |
79 if ( tgaHeader.ctmp == 0 ) | |
80 { | |
81 linesize=bf->Width * ( bf->BPP / 8 ); | |
82 if ( (line=malloc( linesize )) == NULL ) | |
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 | 85 return 8; |
86 } | |
87 | |
88 for ( i=0;i < bf->Height / 2;i++ ) | |
89 { | |
90 memcpy( line,&bf->Image[ i * linesize ],linesize ); | |
91 memcpy( &bf->Image[ i * linesize ],&bf->Image[ ( bf->Height - i - 1 ) * linesize ],linesize ); | |
92 memcpy( &bf->Image[ ( bf->Height - i - 1 ) * linesize ],line,linesize ); | |
93 } | |
94 free( line ); | |
95 } | |
96 | |
97 return 0; | |
98 } | |
99 | |
100 char comment[] = "fresh!mindworkz's TGA Filter. v0.1"; | |
101 | |
102 void tgaWriteTexture( char * filename,txSample * bf ) | |
103 { | |
104 FILE * BMP; | |
105 int i; | |
106 unsigned char * line; | |
107 int linesize; | |
108 tgaHeadert tgaHeader; | |
109 char tmp[255]; | |
110 | |
111 strcpy( tmp,filename ); | |
112 if ( !strstr( tmp,".tga" ) ) strcat( tmp,".tga" ); | |
113 if ( ( BMP=fopen( tmp,"wb+" ) ) == NULL ) | |
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 | 116 exit( 0 ); |
117 } | |
118 memset( &tgaHeader,0,sizeof( tgaHeader ) ); | |
119 tgaHeader.sx=bf->Width; | |
120 tgaHeader.sy=bf->Height; | |
121 tgaHeader.depth=bf->BPP; | |
122 tgaHeader.ctmp=0; | |
123 tgaHeader.tmp[0]=strlen( comment ); | |
124 if ( bf->BPP != 8 ) tgaHeader.tmp[2]=2; | |
125 else tgaHeader.tmp[2]=3; | |
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 | 133 |
134 if ( tgaHeader.ctmp == 0 ) | |
135 { | |
136 linesize=bf->Width * ( bf->BPP / 8 ); | |
137 if ( (line=malloc( linesize )) == NULL ) | |
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 | 140 exit( 0 ); |
141 } | |
142 | |
143 for ( i=0;i < bf->Height / 2;i++ ) | |
144 { | |
145 memcpy( line,&bf->Image[ i * linesize ],linesize ); | |
146 memcpy( &bf->Image[ i * linesize ],&bf->Image[ ( bf->Height - i - 1 ) * linesize ],linesize ); | |
147 memcpy( &bf->Image[ ( bf->Height - i - 1 ) * linesize ],line,linesize ); | |
148 } | |
149 free( line ); | |
150 } | |
151 | |
152 fwrite( &tgaHeader,sizeof( tgaHeader ),1,BMP ); | |
153 fwrite( comment,strlen( comment ),1,BMP ); | |
154 fwrite( bf->Image,bf->ImageSize,1,BMP ); | |
155 | |
156 fclose( BMP ); | |
157 } | |
158 | |
159 void tgaWriteBuffer( char * fname,unsigned char * Buffer,int sx,int sy,int BPP ) | |
160 { | |
161 txSample tmp; | |
162 | |
163 memset( &tmp,0,sizeof( tmp ) ); | |
164 tmp.Width=sx; | |
165 tmp.Height=sy; | |
166 tmp.BPP=BPP; | |
167 tmp.ImageSize=sx * sy * ( BPP / 8 ); | |
168 tmp.Image=Buffer; | |
169 tgaWriteTexture( fname,&tmp ); | |
2082 | 170 } |
171 |