97504cac433cdeb90b5dd7f8b01b1db8809a0ba6
[dosdemo] / libs / imago / src / filetga.c
1 /*
2 libimago - a multi-format image file input/output library.
3 Copyright (C) 2010-2015 John Tsiombikas <nuclear@member.fsf.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published
7 by the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /* -- Targa (tga) module -- */
20
21 #include <string.h>
22 #include <stdlib.h>
23 #include "inttypes.h"
24 #include "imago2.h"
25 #include "ftmodule.h"
26
27
28 #if  defined(__i386__) || defined(__ia64__) || defined(WIN32) || \
29     (defined(__alpha__) || defined(__alpha)) || \
30      defined(__arm__) || \
31     (defined(__mips__) && defined(__MIPSEL__)) || \
32      defined(__SYMBIAN32__) || \
33      defined(__x86_64__) || \
34      defined(__LITTLE_ENDIAN__)
35 /* little endian */
36 #define read_int16_le(f)        read_int16(f)
37 #else
38 /* big endian */
39 #define read_int16_le(f)        read_int16_inv(f)
40 #endif  /* endian check */
41
42
43 enum {
44         IMG_NONE,
45         IMG_CMAP,
46         IMG_RGBA,
47         IMG_BW,
48
49         IMG_RLE_CMAP = 9,
50         IMG_RLE_RGBA,
51         IMG_RLE_BW
52 };
53
54 #define IS_RLE(x)       ((x) >= IMG_RLE_CMAP)
55 #define IS_RGBA(x)      ((x) == IMG_RGBA || (x) == IMG_RLE_RGBA)
56
57
58 struct tga_header {
59         uint8_t idlen;                  /* id field length */
60         uint8_t cmap_type;              /* color map type (0:no color map, 1:color map present) */
61         uint8_t img_type;               /* image type:
62                                                          * 0: no image data
63                                                          *      1: uncomp. color-mapped          9: RLE color-mapped
64                                                          *      2: uncomp. true color           10: RLE true color
65                                                          *      3: uncomp. black/white          11: RLE black/white */
66         uint16_t cmap_first;    /* color map first entry index */
67         uint16_t cmap_len;              /* color map length */
68         uint8_t cmap_entry_sz;  /* color map entry size */
69         uint16_t img_x;                 /* X-origin of the image */
70         uint16_t img_y;                 /* Y-origin of the image */
71         uint16_t img_width;             /* image width */
72         uint16_t img_height;    /* image height */
73         uint8_t img_bpp;                /* bits per pixel */
74         uint8_t img_desc;               /* descriptor:
75                                                          * bits 0 - 3: alpha or overlay bits
76                                                          * bits 5 & 4: origin (0 = bottom/left, 1 = top/right)
77                                                          * bits 7 & 6: data interleaving */
78 };
79
80 struct tga_footer {
81         uint32_t ext_off;               /* extension area offset */
82         uint32_t devdir_off;    /* developer directory offset */
83         char sig[18];                           /* signature with . and \0 */
84 };
85
86
87 static int check(struct img_io *io);
88 static int read(struct img_pixmap *img, struct img_io *io);
89 static int write(struct img_pixmap *img, struct img_io *io);
90 static int read_pixel(struct img_io *io, int rdalpha, uint32_t *pix);
91 static int16_t read_int16(struct img_io *io);
92 static int16_t read_int16_inv(struct img_io *io);
93
94 int img_register_tga(void)
95 {
96         static struct ftype_module mod = {".tga", check, read, write};
97         return img_register_module(&mod);
98 }
99
100
101 static int check(struct img_io *io)
102 {
103         struct tga_footer foot;
104         int res = -1;
105         long pos = io->seek(0, SEEK_CUR, io->uptr);
106         io->seek(-18, SEEK_END, io->uptr);
107
108         if(io->read(foot.sig, 17, io->uptr) < 17) {
109                 io->seek(pos, SEEK_SET, io->uptr);
110                 return -1;
111         }
112
113         if(memcmp(foot.sig, "TRUEVISION-XFILE.", 17) == 0) {
114                 res = 0;
115         }
116         io->seek(pos, SEEK_SET, io->uptr);
117         return res;
118 }
119
120 static int iofgetc(struct img_io *io)
121 {
122         char c;
123         return io->read(&c, 1, io->uptr) < 1 ? -1 : c;
124 }
125
126 static int read(struct img_pixmap *img, struct img_io *io)
127 {
128         struct tga_header hdr;
129         int x, y;
130         int i, c;
131         uint32_t ppixel = 0;
132         int rle_mode = 0, rle_pix_left = 0;
133         int rdalpha;
134
135         /* read header */
136         hdr.idlen = iofgetc(io);
137         hdr.cmap_type = iofgetc(io);
138         hdr.img_type = iofgetc(io);
139         hdr.cmap_first = read_int16_le(io);
140         hdr.cmap_len = read_int16_le(io);
141         hdr.cmap_entry_sz = iofgetc(io);
142         hdr.img_x = read_int16_le(io);
143         hdr.img_y = read_int16_le(io);
144         hdr.img_width = read_int16_le(io);
145         hdr.img_height = read_int16_le(io);
146         hdr.img_bpp = iofgetc(io);
147         if((c = iofgetc(io)) == -1) {
148                 return -1;
149         }
150         hdr.img_desc = c;
151
152         if(!IS_RGBA(hdr.img_type)) {
153                 fprintf(stderr, "only true color tga images supported\n");
154                 return -1;
155         }
156
157         io->seek(hdr.idlen, SEEK_CUR, io);      /* skip the image ID */
158
159         /* skip the color map if it exists */
160         if(hdr.cmap_type == 1) {
161                 io->seek(hdr.cmap_len * hdr.cmap_entry_sz / 8, SEEK_CUR, io);
162         }
163
164         x = hdr.img_width;
165         y = hdr.img_height;
166         rdalpha = hdr.img_desc & 0xf;
167
168         /* TODO make this IMG_FMT_RGB24 if there's no alpha channel */
169         if(img_set_pixels(img, x, y, IMG_FMT_RGBA32, 0) == -1) {
170                 return -1;
171         }
172
173         for(i=0; i<y; i++) {
174                 uint32_t *ptr;
175                 int j;
176
177                 ptr = (uint32_t*)img->pixels + ((hdr.img_desc & 0x20) ? i : y - (i + 1)) * x;
178
179                 for(j=0; j<x; j++) {
180                         /* if the image is raw, then just read the next pixel */
181                         if(!IS_RLE(hdr.img_type)) {
182                                 if(read_pixel(io, rdalpha, &ppixel) == -1) {
183                                         return -1;
184                                 }
185                         } else {
186                                 /* otherwise, for RLE... */
187
188                                 /* if we have pixels left in the packet ... */
189                                 if(rle_pix_left) {
190                                         /* if it's a raw packet, read the next pixel, otherwise keep the same */
191                                         if(!rle_mode) {
192                                                 if(read_pixel(io, rdalpha, &ppixel) == -1) {
193                                                         return -1;
194                                                 }
195                                         }
196                                         --rle_pix_left;
197                                 } else {
198                                         /* read RLE packet header */
199                                         unsigned char phdr = iofgetc(io);
200                                         rle_mode = (phdr & 128);                /* last bit shows the mode for this packet (1: rle, 0: raw) */
201                                         rle_pix_left = (phdr & ~128);   /* the rest gives the count of pixels minus one (we also read one here, so no +1) */
202                                         /* and read the first pixel of the packet */
203                                         if(read_pixel(io, rdalpha, &ppixel) == -1) {
204                                                 return -1;
205                                         }
206                                 }
207                         }
208
209                         *ptr++ = ppixel;
210                 }
211         }
212
213         return 0;
214 }
215
216 static int write(struct img_pixmap *img, struct img_io *io)
217 {
218         return -1;      /* TODO */
219 }
220
221 #define PACK_COLOR32(r,g,b,a) \
222         ((((a) & 0xff) << 24) | \
223          (((r) & 0xff) << 0) | \
224          (((g) & 0xff) << 8) | \
225          (((b) & 0xff) << 16))
226
227 static int read_pixel(struct img_io *io, int rdalpha, uint32_t *pix)
228 {
229         int r, g, b, a;
230         b = iofgetc(io);
231         g = iofgetc(io);
232         r = iofgetc(io);
233         a = rdalpha ? iofgetc(io) : 0xff;
234         *pix = PACK_COLOR32(r, g, b, a);
235         return a == -1 || r == -1 ? -1 : 0;
236 }
237
238 static int16_t read_int16(struct img_io *io)
239 {
240         int16_t v;
241         io->read(&v, 2, io);
242         return v;
243 }
244
245 static int16_t read_int16_inv(struct img_io *io)
246 {
247         int16_t v;
248         io->read(&v, 2, io);
249         return ((v >> 8) & 0xff) | (v << 8);
250 }