blob: 7b38deda390341ff6cf30837c93ced2a67bfcf69 [file] [log] [blame]
Travis Geiselbrecht97b53822010-05-21 22:59:47 -07001/*
2 * Copyright (c) 2008-2010 Travis Geiselbrecht
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24/**
25 * @file
26 * @brief Parse tga format files
27 *
28 * @ingroup graphics
29 */
30
31#include <debug.h>
32#include <compiler.h>
33#include <lib/tga.h>
34
35#define LOCAL_TRACE 0
36
37struct tga_header {
38 uint8_t idlength;
39 uint8_t colormaptype;
40 uint8_t datatypecode;
41 uint16_t colormaporigin;
42 uint16_t colormaplength;
43 uint8_t colormapdepth;
44 uint16_t x_origin;
45 uint16_t y_origin;
46 uint16_t width;
47 uint16_t height;
48 uint8_t bitsperpixel;
49 uint8_t imagedescriptor;
50} __PACKED;
51
52static void print_tga_info(const struct tga_header *header)
53{
54 LTRACEF("idlength %hhd\n", header->idlength);
55 LTRACEF("colormaptype %hhd\n", header->colormaptype);
56 LTRACEF("datatypecode %hhd\n", header->datatypecode);
57 LTRACEF("colormaporigin %hd\n", header->colormaporigin);
58 LTRACEF("colormaplength %hd\n", header->colormaplength);
59 LTRACEF("colormapdepth %hhd\n", header->colormapdepth);
60 LTRACEF("x_origin %hd\n", header->x_origin);
61 LTRACEF("y_origin %hd\n", header->y_origin);
62 LTRACEF("width %hd\n", header->width);
63 LTRACEF("height %hd\n", header->height);
64 LTRACEF("bitsperpixel %hhd\n", header->bitsperpixel);
65 LTRACEF("imagedescriptor %hhd\n", header->imagedescriptor);
66
67}
68
69static void decode_2byte(gfx_surface *surface, uint x, uint y, const void *input)
70{
71 const uint8_t *in = (const uint8_t *)input;
72
73// printf("in 0x%hhx 0x%hhx\n", in[0], in[1]);
74 uint r,g,b;
75
76 b = (in[0] & 0x1f) << 3;
77 g = (((in[0] >> 5) & 0x7) | ((in[1] & 0x3) << 3)) << 3;
78 r = ((in[1] >> 2) & 0x1f) << 3;
79
80 gfx_putpixel(surface, x, y, 0xff000000 | r << 16 | g << 8 | b);
81}
82
83static void decode_3byte(gfx_surface *surface, uint x, uint y, const void *input)
84{
85 const uint8_t *in = (const uint8_t *)input;
86
87// printf("in 0x%hhx 0x%hhx\n", in[0], in[1]);
88
89 gfx_putpixel(surface, x, y, 0xff000000 | in[2] << 16 | in[1] << 8 | in[0]);
90}
91
92static void decode_4byte(gfx_surface *surface, uint x, uint y, const void *input)
93{
94 const uint8_t *in = (const uint8_t *)input;
95
96// printf("in 0x%hhx 0x%hhx 0x%hhx 0x%hhx\n", in[0], in[1], in[2], in[3]);
97
98 if (in[3] == 0)
99 gfx_putpixel(surface, x, y, 0);
100 else
101 gfx_putpixel(surface, x, y, in[3] << 24 | in[2] << 16 | in[1] << 8 | in[0]);
102}
103
104/**
105 * @brief Decode a tga image
106 *
107 * @param ptr Pointer to tga data in memory
108 * @param len Length of tga data
109 * @param format Desired format of returned graphics surface
110 *
111 * @return Graphics surface or NULL on error.
112 *
113 * @ingroup graphics
114 */
115gfx_surface *tga_decode(const void *ptr, size_t len, gfx_format format)
116{
117 const struct tga_header *header = (const struct tga_header *)ptr;
118
119 LTRACEF("ptr %p, len %zu\n", ptr, len);
120
121#if LOCAL_TRACE > 0
122 print_tga_info(header);
123#endif
124
125 /* do some sanity checks */
126 if (header->datatypecode != 2 && header->datatypecode != 10) {
127 dprintf(INFO, "tga_decode: unknown data type %d\n", header->datatypecode);
128 return NULL;
129 }
130 if (header->bitsperpixel != 16 && header->bitsperpixel != 24 && header->bitsperpixel != 32) {
131 dprintf(INFO, "tga_decode: unsupported bits per pixel %d\n", header->bitsperpixel);
132 return NULL;
133 }
134 if (header->colormaptype != 0) {
135 dprintf(INFO, "tga_decode: has colormap, can't handle\n");
136 return NULL;
137 }
138
139 const void *imagestart = ((const uint8_t *)ptr + sizeof(struct tga_header) + header->idlength);
140
141 /* create a surface to hold the decoded bits */
142 gfx_surface *surface = gfx_create_surface(NULL, header->width, header->height, header->width, format);
143 DEBUG_ASSERT(surface);
144
145 /* copy the bits out */
146 void (*decodefunc)(gfx_surface *, uint x, uint y, const void *) = NULL;
147
148 uint step = 1;
149 if (header->bitsperpixel == 16) {
150 step = 2;
151 decodefunc = decode_2byte;
152 } else if (header->bitsperpixel == 24) {
153 step = 3;
154 decodefunc = decode_3byte;
155 } else if (header->bitsperpixel == 32) {
156 step = 4;
157 decodefunc = decode_4byte;
158 }
159
160 if (header->datatypecode == 2) {
161 /* no RLE */
162 uint pos = 0;
163 uint x, y;
164 uint surfacey;
165
166 for (y = 0; y < header->height; y++) {
167
168 if ((header->imagedescriptor & (1 << 5)) == 0)
169 surfacey = (surface->height - 1) - y;
170 else
171 surfacey = y;
172
173 for (x = 0; x < header->width; x++) {
174 decodefunc(surface, x, surfacey, (const uint8_t *)imagestart + pos);
175 pos += step;
176 }
177 }
178 } else if (header->datatypecode == 10) {
179 /* RLE compression */
180 uint pos = 0;
181 uint count = 0;
182 uint x, y;
183
184 x = 0;
185 if ((header->imagedescriptor & (1 << 5)) == 0)
186 y = header->height - 1;
187 else
188 y = 0;
189
190 while (count < (uint)header->height * (uint)header->width) {
191 uint runpos;
192
193 uint8_t run = *((const uint8_t *)imagestart + pos);
194 bool repeat_run = (run & 0x80);
195 uint runlen = (run & 0x7f) + 1;
196
197// printf("pos 0x%x count %u run 0x%hhx runtype %d runlen %u\n", pos, count, run, run & 0x80, runlen);
198
199 /* consume the run byte */
200 pos++;
201
202 /* start of a run */
203 for (runpos = 0; runpos < runlen; runpos++) {
204 decodefunc(surface, x, y, (const uint8_t *)imagestart + pos);
205 count++;
206
207 x++;
208 if (x == surface->width) {
209 if ((header->imagedescriptor & (1 << 5)) == 0)
210 y--;
211 else
212 y++;
213 x = 0;
214 }
215
216 /* if a run of raw pixels, consume an input pixel */
217 if (!repeat_run)
218 pos += step;
219 }
220 /* if this was a run of repeated pixels, consume the one input pixel we repeated */
221 if (repeat_run)
222 pos += step;
223
224 }
225// printf("done with RLE: x %d, y %d, pos %d, count %d\n", x, y, pos, count);
226 }
227
228 return surface;
229}
230