1 /* Copyright  (C) 2010-2020 The RetroArch team
2  *
3  * ---------------------------------------------------------------------------------------
4  * The following license statement only applies to this file (rtga.c).
5  * ---------------------------------------------------------------------------------------
6  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
14  *
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.
21  */
22 
23 /* Modified version of stb_image's TGA sources. */
24 
25 #include <stdio.h>
26 #include <stdint.h>
27 #include <stdarg.h>
28 #include <stddef.h> /* ptrdiff_t on osx */
29 #include <stdlib.h>
30 #include <string.h>
31 
32 #include <retro_assert.h>
33 #include <retro_inline.h>
34 
35 #include <formats/image.h>
36 #include <formats/rtga.h>
37 
38 #define RTGA_COMPUTE_Y(r, g, b) ((uint8_t)((((r) * 77) + ((g) * 150) +  (29 * (b))) >> 8))
39 
40 struct rtga
41 {
42    uint8_t *buff_data;
43    uint32_t *output_image;
44 };
45 
46 typedef struct
47 {
48    uint8_t *img_buffer;
49    uint8_t *img_buffer_end;
50    uint8_t *img_buffer_original;
51    int buflen;
52    int img_n, img_out_n;
53    uint32_t img_x, img_y;
54    uint8_t buffer_start[128];
55 } rtga_context;
56 
rtga_get8(rtga_context * s)57 static INLINE uint8_t rtga_get8(rtga_context *s)
58 {
59    if (s->img_buffer < s->img_buffer_end)
60       return *s->img_buffer++;
61    return 0;
62 }
63 
rtga_skip(rtga_context * s,int n)64 static void rtga_skip(rtga_context *s, int n)
65 {
66    if (n < 0)
67    {
68       s->img_buffer = s->img_buffer_end;
69       return;
70    }
71    s->img_buffer += n;
72 }
73 
rtga_get16le(rtga_context * s)74 static int rtga_get16le(rtga_context *s)
75 {
76    return rtga_get8(s) + (rtga_get8(s) << 8);
77 }
78 
rtga_convert_format(unsigned char * data,int img_n,int req_comp,unsigned int x,unsigned int y)79 static unsigned char *rtga_convert_format(
80       unsigned char *data,
81       int img_n,
82       int req_comp,
83       unsigned int x,
84       unsigned int y)
85 {
86    int i,j;
87    unsigned char *good = (unsigned char *) malloc(req_comp * x * y);
88 
89    if (!good)
90    {
91       free(data);
92       return NULL;
93    }
94 
95    for (j=0; j < (int) y; ++j)
96    {
97       unsigned char *src  = data + j * x * img_n   ;
98       unsigned char *dest = good + j * x * req_comp;
99 
100       switch (((img_n)*8+(req_comp)))
101       {
102          case ((1)*8+(2)):
103             for (i=x-1; i >= 0; --i, src += 1, dest += 2)
104             {
105                dest[0]=src[0];
106                dest[1]=255;
107             }
108             break;
109          case ((1)*8+(3)):
110             for (i=x-1; i >= 0; --i, src += 1, dest += 3)
111                dest[0]=dest[1]=dest[2]=src[0];
112             break;
113          case ((1)*8+(4)):
114             for (i=x-1; i >= 0; --i, src += 1, dest += 4)
115             {
116                dest[0]=dest[1]=dest[2]=src[0];
117                dest[3]=255;
118             }
119             break;
120          case ((2)*8+(1)):
121             for (i=x-1; i >= 0; --i, src += 2, dest += 1)
122                dest[0]=src[0];
123             break;
124          case ((2)*8+(3)):
125             for (i=x-1; i >= 0; --i, src += 2, dest += 3)
126                dest[0]=dest[1]=dest[2]=src[0];
127             break;
128          case ((2)*8+(4)):
129             for (i=x-1; i >= 0; --i, src += 2, dest += 4)
130             {
131                dest[0]=dest[1]=dest[2]=src[0];
132                dest[3]=src[1];
133             }
134             break;
135          case ((3)*8+(4)):
136             for (i=x-1; i >= 0; --i, src += 3, dest += 4)
137             {
138                dest[0]=src[0];
139                dest[1]=src[1];
140                dest[2]=src[2];
141                dest[3]=255;
142             }
143             break;
144          case ((3)*8+(1)):
145             for (i=x-1; i >= 0; --i, src += 3, dest += 1)
146                dest[0] = RTGA_COMPUTE_Y(src[0],src[1],src[2]);
147             break;
148          case ((3)*8+(2)):
149             for (i=x-1; i >= 0; --i, src += 3, dest += 2)
150             {
151                dest[0] = RTGA_COMPUTE_Y(src[0],src[1],src[2]);
152                dest[1] = 255;
153             }
154             break;
155          case ((4)*8+(1)):
156             for (i=x-1; i >= 0; --i, src += 4, dest += 1)
157                dest[0] = RTGA_COMPUTE_Y(src[0],src[1],src[2]);
158             break;
159          case ((4)*8+(2)):
160             for (i=x-1; i >= 0; --i, src += 4, dest += 2)
161             {
162                dest[0] = RTGA_COMPUTE_Y(src[0],src[1],src[2]);
163                dest[1] = src[3];
164             }
165             break;
166          case ((4)*8+(3)):
167             for (i=x-1; i >= 0; --i, src += 4, dest += 3)
168             {
169                dest[0]=src[0];
170                dest[1]=src[1];
171                dest[2]=src[2];
172             }
173             break;
174          default:
175             break;
176       }
177    }
178 
179    free(data);
180    return good;
181 }
182 
rtga_tga_load(rtga_context * s,unsigned * x,unsigned * y,int * comp,int req_comp)183 static uint8_t *rtga_tga_load(rtga_context *s,
184       unsigned *x, unsigned *y, int *comp, int req_comp)
185 {
186    /* Read in the TGA header stuff */
187    int tga_offset          = rtga_get8(s);
188    int tga_indexed         = rtga_get8(s);
189    int tga_image_type      = rtga_get8(s);
190    int tga_is_RLE          = 0;
191    int tga_palette_start   = rtga_get16le(s);
192    int tga_palette_len     = rtga_get16le(s);
193    int tga_palette_bits    = rtga_get8(s);
194    int tga_x_origin        = rtga_get16le(s);
195    int tga_y_origin        = rtga_get16le(s);
196    int tga_width           = rtga_get16le(s);
197    int tga_height          = rtga_get16le(s);
198    int tga_bits_per_pixel  = rtga_get8(s);
199    int tga_comp            = tga_bits_per_pixel / 8;
200    int tga_inverted        = rtga_get8(s);
201 
202    /*   image data */
203    unsigned char *tga_data = NULL;
204 
205    (void)tga_palette_start;
206    (void)tga_x_origin;
207    (void)tga_y_origin;
208 
209    /*   do a tiny bit of precessing */
210    if (tga_image_type >= 8)
211    {
212       tga_image_type -= 8;
213       tga_is_RLE = 1;
214    }
215 
216    /* int tga_alpha_bits = tga_inverted & 15; */
217    tga_inverted = 1 - ((tga_inverted >> 5) & 1);
218 
219    /*   error check */
220    if (
221          (tga_width < 1) || (tga_height < 1) ||
222          (tga_image_type < 1) || (tga_image_type > 3) ||
223          (
224           (tga_bits_per_pixel != 8)  && (tga_bits_per_pixel != 16) &&
225           (tga_bits_per_pixel != 24) && (tga_bits_per_pixel != 32)
226          )
227       )
228       return NULL; /* we don't report this as a bad TGA because we don't even know if it's TGA */
229 
230    /*   If paletted, then we will use the number of bits from the palette */
231    if (tga_indexed)
232       tga_comp = tga_palette_bits / 8;
233 
234    /*   TGA info */
235    *x = tga_width;
236    *y = tga_height;
237    if (comp)
238       *comp = tga_comp;
239 
240    tga_data = (unsigned char*)malloc((size_t)tga_width * tga_height * tga_comp);
241    if (!tga_data)
242       return NULL;
243 
244    /* skip to the data's starting position (offset usually = 0) */
245    rtga_skip(s, tga_offset );
246 
247    if (!tga_indexed && !tga_is_RLE)
248    {
249       int i;
250       for (i=0; i < tga_height; ++i)
251       {
252          int _y           = tga_inverted ? (tga_height -i - 1) : i;
253          uint8_t *tga_row = tga_data + _y * tga_width * tga_comp;
254          int n            = tga_width * tga_comp;
255 
256          if (s->img_buffer + n <= s->img_buffer_end)
257          {
258             memcpy(tga_row, s->img_buffer, n);
259             s->img_buffer += n;
260          }
261       }
262    }
263    else
264    {
265       int i, j;
266       int RLE_repeating          = 0;
267       int RLE_count              = 0;
268       int read_next_pixel        = 1;
269       unsigned char raw_data[4]  = {0};
270       unsigned char *tga_palette = NULL;
271 
272       /*   Do I need to load a palette? */
273       if (tga_indexed)
274       {
275          int n;
276          /* Any data to skip? (offset usually = 0) */
277          rtga_skip(s, tga_palette_start );
278          /* Load the palette */
279          tga_palette = (unsigned char*)malloc(tga_palette_len * tga_palette_bits / 8);
280 
281          if (!tga_palette)
282          {
283             free(tga_data);
284             return NULL;
285          }
286 
287          n = tga_palette_len * tga_palette_bits / 8;
288 
289          if (s->img_buffer+n <= s->img_buffer_end)
290          {
291             memcpy(tga_palette, s->img_buffer, n);
292             s->img_buffer += n;
293          }
294          else
295          {
296             free(tga_data);
297             free(tga_palette);
298             return NULL;
299          }
300       }
301 
302       /*   load the data */
303       for (i=0; i < tga_width * tga_height; ++i)
304       {
305          /*   if I'm in RLE mode, do I need to get a RLE rtga_png chunk? */
306          if (tga_is_RLE)
307          {
308             if (RLE_count == 0)
309             {
310                /*   yep, get the next byte as a RLE command */
311                int RLE_cmd     = rtga_get8(s);
312                RLE_count       = 1 + (RLE_cmd & 127);
313                RLE_repeating   = RLE_cmd >> 7;
314                read_next_pixel = 1;
315             }
316             else if (!RLE_repeating)
317                read_next_pixel = 1;
318          }
319          else
320             read_next_pixel = 1;
321 
322          /*   OK, if I need to read a pixel, do it now */
323          if (read_next_pixel)
324          {
325             /*   load however much data we did have */
326             if (tga_indexed)
327             {
328                /*   read in 1 byte, then perform the lookup */
329                int pal_idx = rtga_get8(s);
330                if (pal_idx >= tga_palette_len) /* invalid index */
331                   pal_idx = 0;
332                pal_idx *= tga_bits_per_pixel / 8;
333                for (j = 0; j*8 < tga_bits_per_pixel; ++j)
334                   raw_data[j] = tga_palette[pal_idx+j];
335             }
336             else
337             {
338                /* read in the data raw */
339                for (j = 0; j*8 < tga_bits_per_pixel; ++j)
340                   raw_data[j] = rtga_get8(s);
341             }
342 
343             /*   clear the reading flag for the next pixel */
344             read_next_pixel = 0;
345          } /* end of reading a pixel */
346 
347          /* copy data */
348          for (j = 0; j < tga_comp; ++j)
349             tga_data[i*tga_comp+j] = raw_data[j];
350 
351          /*   in case we're in RLE mode, keep counting down */
352          --RLE_count;
353       }
354 
355       /*   do I need to invert the image? */
356       if (tga_inverted)
357       {
358          if (tga_data)
359          {
360             for (j = 0; j*2 < tga_height; ++j)
361             {
362                int index1 = j * tga_width * tga_comp;
363                int index2 = (tga_height - 1 - j) * tga_width * tga_comp;
364 
365                for (i = tga_width * tga_comp; i > 0; --i)
366                {
367                   unsigned char temp = tga_data[index1];
368                   tga_data[index1]   = tga_data[index2];
369                   tga_data[index2]   = temp;
370                   ++index1;
371                   ++index2;
372                }
373             }
374          }
375       }
376 
377       /* Clear my palette, if I had one */
378       if (tga_palette)
379          free(tga_palette);
380    }
381 
382    /* swap RGB */
383    if (tga_comp >= 3)
384    {
385       int i;
386       unsigned char* tga_pixel = tga_data;
387 
388       for (i = 0; i < tga_width * tga_height; ++i)
389       {
390          unsigned char temp  = tga_pixel[0];
391          tga_pixel[0]        = tga_pixel[2];
392          tga_pixel[2]        = temp;
393          tga_pixel          += tga_comp;
394       }
395    }
396 
397    /* convert to target component count */
398    if (     (req_comp)
399          && (req_comp >= 1 && req_comp <= 4)
400          && (req_comp != tga_comp))
401    {
402       tga_data = rtga_convert_format(tga_data, tga_comp, req_comp, tga_width, tga_height);
403    }
404 
405    return tga_data;
406 }
407 
rtga_load_from_memory(uint8_t const * buffer,int len,unsigned * x,unsigned * y,int * comp,int req_comp)408 static uint8_t *rtga_load_from_memory(uint8_t const *buffer, int len,
409       unsigned *x, unsigned *y, int *comp, int req_comp)
410 {
411    rtga_context s;
412 
413    s.img_buffer          = (uint8_t *)buffer;
414    s.img_buffer_original = (uint8_t *) buffer;
415    s.img_buffer_end      = (uint8_t *) buffer+len;
416 
417    return rtga_tga_load(&s,x,y,comp,req_comp);
418 }
419 
rtga_process_image(rtga_t * rtga,void ** buf_data,size_t size,unsigned * width,unsigned * height)420 int rtga_process_image(rtga_t *rtga, void **buf_data,
421       size_t size, unsigned *width, unsigned *height)
422 {
423    int comp;
424    unsigned size_tex     = 0;
425 
426    if (!rtga)
427       return IMAGE_PROCESS_ERROR;
428 
429    rtga->output_image   = (uint32_t*)rtga_load_from_memory(rtga->buff_data,
430                            (int)size, width, height, &comp, 4);
431    *buf_data             = rtga->output_image;
432    size_tex              = (*width) * (*height);
433 
434    /* Convert RGBA to ARGB */
435    while (size_tex--)
436    {
437       unsigned int texel = rtga->output_image[size_tex];
438       unsigned int A     = texel & 0xFF000000;
439       unsigned int B     = texel & 0x00FF0000;
440       unsigned int G     = texel & 0x0000FF00;
441       unsigned int R     = texel & 0x000000FF;
442       ((unsigned int*)rtga->output_image)[size_tex] = A | (R << 16) | G | (B >> 16);
443    };
444 
445    return IMAGE_PROCESS_END;
446 }
447 
rtga_set_buf_ptr(rtga_t * rtga,void * data)448 bool rtga_set_buf_ptr(rtga_t *rtga, void *data)
449 {
450    if (!rtga)
451       return false;
452 
453    rtga->buff_data = (uint8_t*)data;
454 
455    return true;
456 }
457 
rtga_free(rtga_t * rtga)458 void rtga_free(rtga_t *rtga)
459 {
460    if (!rtga)
461       return;
462 
463    free(rtga);
464 }
465 
rtga_alloc(void)466 rtga_t *rtga_alloc(void)
467 {
468    rtga_t *rtga = (rtga_t*)calloc(1, sizeof(*rtga));
469    if (!rtga)
470       return NULL;
471    return rtga;
472 }
473