1 /************************************************************************
3 * voxelands - 3d voxel world sandbox game
4 * Copyright (C) Lisa 'darkrose' Milne 2016 <lisa@ltmnet.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>
18 ************************************************************************/
27 static unsigned char tga_uncompressed
[12] = {0,0,2,0,0,0,0,0,0,0,0,0};
28 static unsigned char tga_compressed
[12] = {0,0,10,0,0,0,0,0,0,0,0,0};
30 /* is it a tga image? */
31 int image_is_tga(file_t
*f
)
33 if (!memcmp(f
->data
,tga_compressed
,12))
35 if (!memcmp(f
->data
,tga_uncompressed
,12))
40 /* load a tga image to pixel data */
41 int image_load_tga(file_t
*f
, image_t
*p
)
48 unsigned char header
[18];
50 file_read(f
,header
,18);
52 if (!memcmp(header
,tga_compressed
,12)) {
54 }else if (memcmp(header
,tga_uncompressed
,12)) {
58 p
->w
= (header
[13]*256)+header
[12];
59 p
->h
= (header
[15]*256)+header
[14];
61 if (bpp
< 3 || bpp
> 4)
66 p
->pixels
= malloc(p
->w
*p
->h
*4);
69 unsigned int pixelcount
= p
->w
*p
->h
;
70 unsigned int currentpixel
= 0;
71 unsigned char chunkheader
;
72 unsigned char colour
[4];
75 file_read(f
,&chunkheader
,1);
77 if(chunkheader
< 128) {
80 for (i
=0; i
<chunkheader
; i
++) {
81 file_read(f
,colour
,bpp
);
84 p
->pixels
[k
] = colour
[2];
85 p
->pixels
[k
+1] = colour
[1];
86 p
->pixels
[k
+2] = colour
[0];
88 /* ensure there's an alpha value */
90 p
->pixels
[k
+ 3] = colour
[3];
92 p
->pixels
[k
+ 3] = 255;
98 if (currentpixel
> pixelcount
) {
106 file_read(f
,colour
,bpp
);
108 for (i
=0; i
<chunkheader
; i
++) {
110 p
->pixels
[k
] = colour
[2];
111 p
->pixels
[k
+1] = colour
[1];
112 p
->pixels
[k
+2] = colour
[0];
114 /* ensure there's an alpha value */
116 p
->pixels
[k
+ 3] = colour
[3];
118 p
->pixels
[k
+ 3] = 255;
124 if (currentpixel
> pixelcount
) {
130 } while (currentpixel
< pixelcount
);
132 file_read(f
,p
->pixels
,size
);
136 unsigned char* data
= malloc(p
->w
*p
->h
*4);
137 for (i
=0,k
=0; i
<size
; i
+=3,k
+=4) {
138 data
[k
] = p
->pixels
[i
+2];
139 data
[k
+1] = p
->pixels
[i
+1];
140 data
[k
+2] = p
->pixels
[i
];
147 for (i
=0; i
<size
; i
+=bpp
) {
148 p
->pixels
[i
] ^= p
->pixels
[i
+2];
149 p
->pixels
[i
+2] ^= p
->pixels
[i
];
150 p
->pixels
[i
] ^= p
->pixels
[i
+2];
158 /* write pixel data to a tga image */
159 int image_save_tga(image_t
*p
, char* file
)
162 unsigned char header
[18] = {0,0,2,0,0,0,0,0,0,0,0,0, 0,0,0,0,32,0};
165 int sz
= p
->w
*p
->h
*4;
167 header
[13] = (p
->w
&0xFF00)>>8;
168 header
[12] = p
->w
&0x00FF;
169 header
[15] = (p
->h
&0xFF00)>>8;
170 header
[14] = p
->h
&0x00FF;
172 if (!path_get(NULL
,file
,0,buff
,2048))
174 f
= fopen(buff
,"wb");
178 fwrite(header
,18,1,f
);
180 /* tga expects BGRA so change it from RGBA */
181 for (i
=0; i
<sz
; i
+=4) {
182 fwrite(&p
->pixels
[i
+2],1,1,f
);
183 fwrite(&p
->pixels
[i
+1],1,1,f
);
184 fwrite(&p
->pixels
[i
],1,1,f
);
185 fwrite(&p
->pixels
[i
+3],1,1,f
);