1 /* Copyright (C) 2010-2020 The RetroArch team
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (rtga.c).
5 * ---------------------------------------------------------------------------------------
7 * Permission is hereby granted, free of charge,
8 * to any person obtaining a copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 /* Modified version of stb_image's TGA sources. */
28 #include <stddef.h> /* ptrdiff_t on osx */
32 #include <retro_inline.h>
34 #include <formats/image.h>
35 #include <formats/rtga.h>
37 #define RTGA_COMPUTE_Y(r, g, b) ((uint8_t)((((r) * 77) + ((g) * 150) + (29 * (b))) >> 8))
42 uint32_t *output_image;
48 uint8_t *img_buffer_end;
49 uint8_t *img_buffer_original;
52 uint32_t img_x, img_y;
53 uint8_t buffer_start[128];
56 static INLINE uint8_t rtga_get8(rtga_context *s)
58 if (s->img_buffer < s->img_buffer_end)
59 return *s->img_buffer++;
63 static void rtga_skip(rtga_context *s, int n)
67 s->img_buffer = s->img_buffer_end;
73 static int rtga_get16le(rtga_context *s)
75 return rtga_get8(s) + (rtga_get8(s) << 8);
78 static unsigned char *rtga_convert_format(
86 unsigned char *good = (unsigned char *) malloc(req_comp * x * y);
94 for (j=0; j < (int) y; ++j)
96 unsigned char *src = data + j * x * img_n ;
97 unsigned char *dest = good + j * x * req_comp;
99 switch (((img_n)*8+(req_comp)))
102 for (i=x-1; i >= 0; --i, src += 1, dest += 2)
109 for (i=x-1; i >= 0; --i, src += 1, dest += 3)
110 dest[0]=dest[1]=dest[2]=src[0];
113 for (i=x-1; i >= 0; --i, src += 1, dest += 4)
115 dest[0]=dest[1]=dest[2]=src[0];
120 for (i=x-1; i >= 0; --i, src += 2, dest += 1)
124 for (i=x-1; i >= 0; --i, src += 2, dest += 3)
125 dest[0]=dest[1]=dest[2]=src[0];
128 for (i=x-1; i >= 0; --i, src += 2, dest += 4)
130 dest[0]=dest[1]=dest[2]=src[0];
135 for (i=x-1; i >= 0; --i, src += 3, dest += 4)
144 for (i=x-1; i >= 0; --i, src += 3, dest += 1)
145 dest[0] = RTGA_COMPUTE_Y(src[0],src[1],src[2]);
148 for (i=x-1; i >= 0; --i, src += 3, dest += 2)
150 dest[0] = RTGA_COMPUTE_Y(src[0],src[1],src[2]);
155 for (i=x-1; i >= 0; --i, src += 4, dest += 1)
156 dest[0] = RTGA_COMPUTE_Y(src[0],src[1],src[2]);
159 for (i=x-1; i >= 0; --i, src += 4, dest += 2)
161 dest[0] = RTGA_COMPUTE_Y(src[0],src[1],src[2]);
166 for (i=x-1; i >= 0; --i, src += 4, dest += 3)
182 static uint8_t *rtga_tga_load(rtga_context *s,
183 unsigned *x, unsigned *y, int *comp, int req_comp)
185 /* Read in the TGA header stuff */
186 int tga_offset = rtga_get8(s);
187 int tga_indexed = rtga_get8(s);
188 int tga_image_type = rtga_get8(s);
190 int tga_palette_start = rtga_get16le(s);
191 int tga_palette_len = rtga_get16le(s);
192 int tga_palette_bits = rtga_get8(s);
193 int tga_x_origin = rtga_get16le(s);
194 int tga_y_origin = rtga_get16le(s);
195 int tga_width = rtga_get16le(s);
196 int tga_height = rtga_get16le(s);
197 int tga_bits_per_pixel = rtga_get8(s);
198 int tga_comp = tga_bits_per_pixel / 8;
199 int tga_inverted = rtga_get8(s);
202 unsigned char *tga_data = NULL;
204 (void)tga_palette_start;
208 /* do a tiny bit of precessing */
209 if (tga_image_type >= 8)
215 /* int tga_alpha_bits = tga_inverted & 15; */
216 tga_inverted = 1 - ((tga_inverted >> 5) & 1);
220 (tga_width < 1) || (tga_height < 1) ||
221 (tga_image_type < 1) || (tga_image_type > 3) ||
223 (tga_bits_per_pixel != 8) && (tga_bits_per_pixel != 16) &&
224 (tga_bits_per_pixel != 24) && (tga_bits_per_pixel != 32)
227 return NULL; /* we don't report this as a bad TGA because we don't even know if it's TGA */
229 /* If paletted, then we will use the number of bits from the palette */
231 tga_comp = tga_palette_bits / 8;
239 tga_data = (unsigned char*)malloc((size_t)tga_width * tga_height * tga_comp);
243 /* skip to the data's starting position (offset usually = 0) */
244 rtga_skip(s, tga_offset );
246 if (!tga_indexed && !tga_is_RLE)
249 for (i=0; i < tga_height; ++i)
251 int _y = tga_inverted ? (tga_height -i - 1) : i;
252 uint8_t *tga_row = tga_data + _y * tga_width * tga_comp;
253 int n = tga_width * tga_comp;
255 if (s->img_buffer + n <= s->img_buffer_end)
257 memcpy(tga_row, s->img_buffer, n);
265 int RLE_repeating = 0;
267 int read_next_pixel = 1;
268 /* Needs to be at least 33 bytes to silence a GCC warning,
269 * only 4 are actually used */
270 unsigned char raw_data[33] = {0};
271 unsigned char *tga_palette = NULL;
273 /* Do I need to load a palette? */
277 /* Any data to skip? (offset usually = 0) */
278 rtga_skip(s, tga_palette_start );
279 /* Load the palette */
280 tga_palette = (unsigned char*)malloc(tga_palette_len * tga_palette_bits / 8);
288 n = tga_palette_len * tga_palette_bits / 8;
290 if (s->img_buffer+n <= s->img_buffer_end)
292 memcpy(tga_palette, s->img_buffer, n);
304 for (i=0; i < tga_width * tga_height; ++i)
306 /* if I'm in RLE mode, do I need to get a RLE rtga_png chunk? */
311 /* yep, get the next byte as a RLE command */
312 int RLE_cmd = rtga_get8(s);
313 RLE_count = 1 + (RLE_cmd & 127);
314 RLE_repeating = RLE_cmd >> 7;
317 else if (!RLE_repeating)
323 /* OK, if I need to read a pixel, do it now */
326 /* load however much data we did have */
329 /* read in 1 byte, then perform the lookup */
330 int pal_idx = rtga_get8(s);
331 if (pal_idx >= tga_palette_len) /* invalid index */
333 pal_idx *= tga_bits_per_pixel / 8;
334 for (j = 0; j*8 < tga_bits_per_pixel; ++j)
335 raw_data[j] = tga_palette[pal_idx+j];
339 /* read in the data raw */
340 for (j = 0; j*8 < tga_bits_per_pixel; ++j)
341 raw_data[j] = rtga_get8(s);
344 /* clear the reading flag for the next pixel */
346 } /* end of reading a pixel */
349 for (j = 0; j < tga_comp; ++j)
350 tga_data[i*tga_comp+j] = raw_data[j];
352 /* in case we're in RLE mode, keep counting down */
356 /* do I need to invert the image? */
361 for (j = 0; j*2 < tga_height; ++j)
363 int index1 = j * tga_width * tga_comp;
364 int index2 = (tga_height - 1 - j) * tga_width * tga_comp;
366 for (i = tga_width * tga_comp; i > 0; --i)
368 unsigned char temp = tga_data[index1];
369 tga_data[index1] = tga_data[index2];
370 tga_data[index2] = temp;
378 /* Clear my palette, if I had one */
387 unsigned char* tga_pixel = tga_data;
389 for (i = 0; i < tga_width * tga_height; ++i)
391 unsigned char temp = tga_pixel[0];
392 tga_pixel[0] = tga_pixel[2];
394 tga_pixel += tga_comp;
398 /* convert to target component count */
400 && (req_comp >= 1 && req_comp <= 4)
401 && (req_comp != tga_comp))
403 tga_data = rtga_convert_format(tga_data, tga_comp, req_comp, tga_width, tga_height);
409 static uint8_t *rtga_load_from_memory(uint8_t const *buffer, int len,
410 unsigned *x, unsigned *y, int *comp, int req_comp)
414 s.img_buffer = (uint8_t *)buffer;
415 s.img_buffer_original = (uint8_t *) buffer;
416 s.img_buffer_end = (uint8_t *) buffer+len;
418 return rtga_tga_load(&s,x,y,comp,req_comp);
421 int rtga_process_image(rtga_t *rtga, void **buf_data,
422 size_t size, unsigned *width, unsigned *height)
425 unsigned size_tex = 0;
428 return IMAGE_PROCESS_ERROR;
430 rtga->output_image = (uint32_t*)rtga_load_from_memory(rtga->buff_data,
431 (int)size, width, height, &comp, 4);
432 *buf_data = rtga->output_image;
433 size_tex = (*width) * (*height);
435 /* Convert RGBA to ARGB */
438 unsigned int texel = rtga->output_image[size_tex];
439 unsigned int A = texel & 0xFF000000;
440 unsigned int B = texel & 0x00FF0000;
441 unsigned int G = texel & 0x0000FF00;
442 unsigned int R = texel & 0x000000FF;
443 ((unsigned int*)rtga->output_image)[size_tex] = A | (R << 16) | G | (B >> 16);
446 return IMAGE_PROCESS_END;
449 bool rtga_set_buf_ptr(rtga_t *rtga, void *data)
454 rtga->buff_data = (uint8_t*)data;
459 void rtga_free(rtga_t *rtga)
467 rtga_t *rtga_alloc(void)
469 rtga_t *rtga = (rtga_t*)calloc(1, sizeof(*rtga));