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