2 libimago - a multi-format image file input/output library.
3 Copyright (C) 2010-2015 John Tsiombikas <nuclear@member.fsf.org>
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.
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.
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/>.
19 /* -- Targa (tga) module -- */
28 #if defined(__i386__) || defined(__ia64__) || defined(WIN32) || \
29 (defined(__alpha__) || defined(__alpha)) || \
31 (defined(__mips__) && defined(__MIPSEL__)) || \
32 defined(__SYMBIAN32__) || \
33 defined(__x86_64__) || \
34 defined(__LITTLE_ENDIAN__)
36 #define read_int16_le(f) read_int16(f)
39 #define read_int16_le(f) read_int16_inv(f)
40 #endif /* endian check */
54 #define IS_RLE(x) ((x) >= IMG_RLE_CMAP)
55 #define IS_RGBA(x) ((x) == IMG_RGBA || (x) == IMG_RLE_RGBA)
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:
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 */
81 uint32_t ext_off; /* extension area offset */
82 uint32_t devdir_off; /* developer directory offset */
83 char sig[18]; /* signature with . and \0 */
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);
94 int img_register_tga(void)
96 static struct ftype_module mod = {".tga", check, read, write};
97 return img_register_module(&mod);
101 static int check(struct img_io *io)
103 struct tga_footer foot;
105 long pos = io->seek(0, SEEK_CUR, io->uptr);
106 io->seek(-18, SEEK_END, io->uptr);
108 if(io->read(foot.sig, 17, io->uptr) < 17) {
109 io->seek(pos, SEEK_SET, io->uptr);
113 if(memcmp(foot.sig, "TRUEVISION-XFILE.", 17) == 0) {
116 io->seek(pos, SEEK_SET, io->uptr);
120 static int iofgetc(struct img_io *io)
123 return io->read(&c, 1, io->uptr) < 1 ? -1 : c;
126 static int read(struct img_pixmap *img, struct img_io *io)
128 struct tga_header hdr;
132 int rle_mode = 0, rle_pix_left = 0;
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) {
152 if(!IS_RGBA(hdr.img_type)) {
153 fprintf(stderr, "only true color tga images supported\n");
157 io->seek(hdr.idlen, SEEK_CUR, io); /* skip the image ID */
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);
166 rdalpha = hdr.img_desc & 0xf;
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) {
177 ptr = (uint32_t*)img->pixels + ((hdr.img_desc & 0x20) ? i : y - (i + 1)) * x;
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) {
186 /* otherwise, for RLE... */
188 /* if we have pixels left in the packet ... */
190 /* if it's a raw packet, read the next pixel, otherwise keep the same */
192 if(read_pixel(io, rdalpha, &ppixel) == -1) {
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) {
216 static int write(struct img_pixmap *img, struct img_io *io)
218 return -1; /* TODO */
221 #define PACK_COLOR32(r,g,b,a) \
222 ((((a) & 0xff) << 24) | \
223 (((r) & 0xff) << 0) | \
224 (((g) & 0xff) << 8) | \
225 (((b) & 0xff) << 16))
227 static int read_pixel(struct img_io *io, int rdalpha, uint32_t *pix)
233 a = rdalpha ? iofgetc(io) : 0xff;
234 *pix = PACK_COLOR32(r, g, b, a);
235 return a == -1 || r == -1 ? -1 : 0;
238 static int16_t read_int16(struct img_io *io)
245 static int16_t read_int16_inv(struct img_io *io)
249 return ((v >> 8) & 0xff) | (v << 8);