blob: dd87262a7faa2270af004663788fbbffe7e3ade8 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
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 <trace.h>
33#include <assert.h>
34#include <compiler.h>
35#include <lib/tga.h>
36
37#define LOCAL_TRACE 0
38
39struct tga_header {
40 uint8_t idlength;
41 uint8_t colormaptype;
42 uint8_t datatypecode;
43 uint16_t colormaporigin;
44 uint16_t colormaplength;
45 uint8_t colormapdepth;
46 uint16_t x_origin;
47 uint16_t y_origin;
48 uint16_t width;
49 uint16_t height;
50 uint8_t bitsperpixel;
51 uint8_t imagedescriptor;
52} __PACKED;
53
54static void print_tga_info(const struct tga_header *header)
55{
56 LTRACEF("idlength %hhd\n", header->idlength);
57 LTRACEF("colormaptype %hhd\n", header->colormaptype);
58 LTRACEF("datatypecode %hhd\n", header->datatypecode);
59 LTRACEF("colormaporigin %hd\n", header->colormaporigin);
60 LTRACEF("colormaplength %hd\n", header->colormaplength);
61 LTRACEF("colormapdepth %hhd\n", header->colormapdepth);
62 LTRACEF("x_origin %hd\n", header->x_origin);
63 LTRACEF("y_origin %hd\n", header->y_origin);
64 LTRACEF("width %hd\n", header->width);
65 LTRACEF("height %hd\n", header->height);
66 LTRACEF("bitsperpixel %hhd\n", header->bitsperpixel);
67 LTRACEF("imagedescriptor %hhd\n", header->imagedescriptor);
68
69}
70
71static void decode_2byte(gfx_surface *surface, uint x, uint y, const void *input)
72{
73 const uint8_t *in = (const uint8_t *)input;
74
75// printf("in 0x%hhx 0x%hhx\n", in[0], in[1]);
76 uint r,g,b;
77
78 b = (in[0] & 0x1f) << 3;
79 g = (((in[0] >> 5) & 0x7) | ((in[1] & 0x3) << 3)) << 3;
80 r = ((in[1] >> 2) & 0x1f) << 3;
81
82 gfx_putpixel(surface, x, y, 0xff000000 | r << 16 | g << 8 | b);
83}
84
85static void decode_3byte(gfx_surface *surface, uint x, uint y, const void *input)
86{
87 const uint8_t *in = (const uint8_t *)input;
88
89// printf("in 0x%hhx 0x%hhx\n", in[0], in[1]);
90
91 gfx_putpixel(surface, x, y, 0xff000000 | in[2] << 16 | in[1] << 8 | in[0]);
92}
93
94static void decode_4byte(gfx_surface *surface, uint x, uint y, const void *input)
95{
96 const uint8_t *in = (const uint8_t *)input;
97
98// printf("in 0x%hhx 0x%hhx 0x%hhx 0x%hhx\n", in[0], in[1], in[2], in[3]);
99
100 if (in[3] == 0)
101 gfx_putpixel(surface, x, y, 0);
102 else
103 gfx_putpixel(surface, x, y, in[3] << 24 | in[2] << 16 | in[1] << 8 | in[0]);
104}
105
106/**
107 * @brief Decode a tga image
108 *
109 * @param ptr Pointer to tga data in memory
110 * @param len Length of tga data
111 * @param format Desired format of returned graphics surface
112 *
113 * @return Graphics surface or NULL on error.
114 *
115 * @ingroup graphics
116 */
117gfx_surface *tga_decode(const void *ptr, size_t len, gfx_format format)
118{
119 const struct tga_header *header = (const struct tga_header *)ptr;
120
121 LTRACEF("ptr %p, len %zu\n", ptr, len);
122
123#if LOCAL_TRACE > 0
124 print_tga_info(header);
125#endif
126
127 /* do some sanity checks */
128 if (header->datatypecode != 2 && header->datatypecode != 10) {
129 dprintf(INFO, "tga_decode: unknown data type %d\n", header->datatypecode);
130 return NULL;
131 }
132 if (header->bitsperpixel != 16 && header->bitsperpixel != 24 && header->bitsperpixel != 32) {
133 dprintf(INFO, "tga_decode: unsupported bits per pixel %d\n", header->bitsperpixel);
134 return NULL;
135 }
136 if (header->colormaptype != 0) {
137 dprintf(INFO, "tga_decode: has colormap, can't handle\n");
138 return NULL;
139 }
140
141 const void *imagestart = ((const uint8_t *)ptr + sizeof(struct tga_header) + header->idlength);
142
143 /* create a surface to hold the decoded bits */
144 gfx_surface *surface = gfx_create_surface(NULL, header->width, header->height, header->width, format);
145 DEBUG_ASSERT(surface);
146
147 /* copy the bits out */
148 void (*decodefunc)(gfx_surface *, uint x, uint y, const void *) = NULL;
149
150 uint step = 1;
151 if (header->bitsperpixel == 16) {
152 step = 2;
153 decodefunc = decode_2byte;
154 } else if (header->bitsperpixel == 24) {
155 step = 3;
156 decodefunc = decode_3byte;
157 } else if (header->bitsperpixel == 32) {
158 step = 4;
159 decodefunc = decode_4byte;
160 }
161
162 if (header->datatypecode == 2) {
163 /* no RLE */
164 uint pos = 0;
165 uint x, y;
166 uint surfacey;
167
168 for (y = 0; y < header->height; y++) {
169
170 if ((header->imagedescriptor & (1 << 5)) == 0)
171 surfacey = (surface->height - 1) - y;
172 else
173 surfacey = y;
174
175 for (x = 0; x < header->width; x++) {
176 decodefunc(surface, x, surfacey, (const uint8_t *)imagestart + pos);
177 pos += step;
178 }
179 }
180 } else if (header->datatypecode == 10) {
181 /* RLE compression */
182 uint pos = 0;
183 uint count = 0;
184 uint x, y;
185
186 x = 0;
187 if ((header->imagedescriptor & (1 << 5)) == 0)
188 y = header->height - 1;
189 else
190 y = 0;
191
192 while (count < (uint)header->height * (uint)header->width) {
193 uint runpos;
194
195 uint8_t run = *((const uint8_t *)imagestart + pos);
196 bool repeat_run = (run & 0x80);
197 uint runlen = (run & 0x7f) + 1;
198
199// printf("pos 0x%x count %u run 0x%hhx runtype %d runlen %u\n", pos, count, run, run & 0x80, runlen);
200
201 /* consume the run byte */
202 pos++;
203
204 /* start of a run */
205 for (runpos = 0; runpos < runlen; runpos++) {
206 decodefunc(surface, x, y, (const uint8_t *)imagestart + pos);
207 count++;
208
209 x++;
210 if (x == surface->width) {
211 if ((header->imagedescriptor & (1 << 5)) == 0)
212 y--;
213 else
214 y++;
215 x = 0;
216 }
217
218 /* if a run of raw pixels, consume an input pixel */
219 if (!repeat_run)
220 pos += step;
221 }
222 /* if this was a run of repeated pixels, consume the one input pixel we repeated */
223 if (repeat_run)
224 pos += step;
225
226 }
227// printf("done with RLE: x %d, y %d, pos %d, count %d\n", x, y, pos, count);
228 }
229
230 return surface;
231}
232