1 /* Copyright  (C) 2010-2018 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    uint32_t img_x, img_y;
49    int img_n, img_out_n;
50 
51    int buflen;
52    uint8_t buffer_start[128];
53 
54    uint8_t *img_buffer;
55    uint8_t *img_buffer_end;
56    uint8_t *img_buffer_original;
57 } rtga__context;
58 
rtga__get8(rtga__context * s)59 static INLINE uint8_t rtga__get8(rtga__context *s)
60 {
61    if (s->img_buffer < s->img_buffer_end)
62       return *s->img_buffer++;
63    return 0;
64 }
65 
rtga__skip(rtga__context * s,int n)66 static void rtga__skip(rtga__context *s, int n)
67 {
68    if (n < 0)
69    {
70       s->img_buffer = s->img_buffer_end;
71       return;
72    }
73    s->img_buffer += n;
74 }
75 
rtga__getn(rtga__context * s,uint8_t * buffer,int n)76 static int rtga__getn(rtga__context *s, uint8_t *buffer, int n)
77 {
78    if (s->img_buffer+n <= s->img_buffer_end)
79    {
80       memcpy(buffer, s->img_buffer, n);
81       s->img_buffer += n;
82       return 1;
83    }
84 
85    return 0;
86 }
87 
rtga__get16le(rtga__context * s)88 static int rtga__get16le(rtga__context *s)
89 {
90    int z = rtga__get8(s);
91    return z + (rtga__get8(s) << 8);
92 }
93 
rtga__convert_format(unsigned char * data,int img_n,int req_comp,unsigned int x,unsigned int y)94 static unsigned char *rtga__convert_format(
95       unsigned char *data,
96       int img_n,
97       int req_comp,
98       unsigned int x,
99       unsigned int y)
100 {
101    int i,j;
102    unsigned char *good = (unsigned char *) malloc(req_comp * x * y);
103 
104    if (!good)
105    {
106       free(data);
107       return NULL;
108    }
109 
110    for (j=0; j < (int) y; ++j)
111    {
112       unsigned char *src  = data + j * x * img_n   ;
113       unsigned char *dest = good + j * x * req_comp;
114 
115       switch (((img_n)*8+(req_comp)))
116       {
117          case ((1)*8+(2)):
118             for(i=x-1; i >= 0; --i, src += 1, dest += 2)
119             {
120                dest[0]=src[0];
121                dest[1]=255;
122             }
123             break;
124          case ((1)*8+(3)):
125             for(i=x-1; i >= 0; --i, src += 1, dest += 3)
126                dest[0]=dest[1]=dest[2]=src[0];
127             break;
128          case ((1)*8+(4)):
129             for(i=x-1; i >= 0; --i, src += 1, dest += 4)
130             {
131                dest[0]=dest[1]=dest[2]=src[0];
132                dest[3]=255;
133             }
134             break;
135          case ((2)*8+(1)):
136             for(i=x-1; i >= 0; --i, src += 2, dest += 1)
137                dest[0]=src[0];
138             break;
139          case ((2)*8+(3)):
140             for(i=x-1; i >= 0; --i, src += 2, dest += 3)
141                dest[0]=dest[1]=dest[2]=src[0];
142             break;
143          case ((2)*8+(4)):
144             for(i=x-1; i >= 0; --i, src += 2, dest += 4)
145             {
146                dest[0]=dest[1]=dest[2]=src[0];
147                dest[3]=src[1];
148             }
149             break;
150          case ((3)*8+(4)):
151             for(i=x-1; i >= 0; --i, src += 3, dest += 4)
152             {
153                dest[0]=src[0];
154                dest[1]=src[1];
155                dest[2]=src[2];
156                dest[3]=255;
157             }
158             break;
159          case ((3)*8+(1)):
160             for(i=x-1; i >= 0; --i, src += 3, dest += 1)
161                dest[0] = RTGA_COMPUTE_Y(src[0],src[1],src[2]);
162             break;
163          case ((3)*8+(2)):
164             for(i=x-1; i >= 0; --i, src += 3, dest += 2)
165             {
166                dest[0] = RTGA_COMPUTE_Y(src[0],src[1],src[2]);
167                dest[1] = 255;
168             }
169             break;
170          case ((4)*8+(1)):
171             for(i=x-1; i >= 0; --i, src += 4, dest += 1)
172                dest[0] = RTGA_COMPUTE_Y(src[0],src[1],src[2]);
173             break;
174          case ((4)*8+(2)):
175             for(i=x-1; i >= 0; --i, src += 4, dest += 2)
176             {
177                dest[0] = RTGA_COMPUTE_Y(src[0],src[1],src[2]);
178                dest[1] = src[3];
179             }
180             break;
181          case ((4)*8+(3)):
182             for(i=x-1; i >= 0; --i, src += 4, dest += 3)
183             {
184                dest[0]=src[0];
185                dest[1]=src[1];
186                dest[2]=src[2];
187             }
188             break;
189          default:
190             break;
191       }
192    }
193 
194    free(data);
195    return good;
196 }
197 
rtga__tga_load(rtga__context * s,unsigned * x,unsigned * y,int * comp,int req_comp)198 static uint8_t *rtga__tga_load(rtga__context *s,
199       unsigned *x, unsigned *y, int *comp, int req_comp)
200 {
201    /* Read in the TGA header stuff */
202    int tga_offset          = rtga__get8(s);
203    int tga_indexed         = rtga__get8(s);
204    int tga_image_type      = rtga__get8(s);
205    int tga_is_RLE          = 0;
206    int tga_palette_start   = rtga__get16le(s);
207    int tga_palette_len     = rtga__get16le(s);
208    int tga_palette_bits    = rtga__get8(s);
209    int tga_x_origin        = rtga__get16le(s);
210    int tga_y_origin        = rtga__get16le(s);
211    int tga_width           = rtga__get16le(s);
212    int tga_height          = rtga__get16le(s);
213    int tga_bits_per_pixel  = rtga__get8(s);
214    int tga_comp            = tga_bits_per_pixel / 8;
215    int tga_inverted        = rtga__get8(s);
216 
217    /*   image data */
218    unsigned char *tga_data = NULL;
219 
220    (void)tga_palette_start;
221    (void)tga_x_origin;
222    (void)tga_y_origin;
223 
224    /*   do a tiny bit of precessing */
225    if (tga_image_type >= 8)
226    {
227       tga_image_type -= 8;
228       tga_is_RLE = 1;
229    }
230 
231    /* int tga_alpha_bits = tga_inverted & 15; */
232    tga_inverted = 1 - ((tga_inverted >> 5) & 1);
233 
234    /*   error check */
235    if (
236          (tga_width < 1) || (tga_height < 1) ||
237          (tga_image_type < 1) || (tga_image_type > 3) ||
238          (
239           (tga_bits_per_pixel != 8)  && (tga_bits_per_pixel != 16) &&
240           (tga_bits_per_pixel != 24) && (tga_bits_per_pixel != 32)
241          )
242       )
243       return NULL; /* we don't report this as a bad TGA because we don't even know if it's TGA */
244 
245    /*   If paletted, then we will use the number of bits from the palette */
246    if (tga_indexed)
247       tga_comp = tga_palette_bits / 8;
248 
249    /*   TGA info */
250    *x = tga_width;
251    *y = tga_height;
252    if (comp)
253       *comp = tga_comp;
254 
255    tga_data = (unsigned char*)malloc((size_t)tga_width * tga_height * tga_comp);
256    if (!tga_data)
257       return NULL;
258 
259    /* skip to the data's starting position (offset usually = 0) */
260    rtga__skip(s, tga_offset );
261 
262    if (!tga_indexed && !tga_is_RLE)
263    {
264       int i;
265       for (i=0; i < tga_height; ++i)
266       {
267          int _y           = tga_inverted ? (tga_height -i - 1) : i;
268          uint8_t *tga_row = tga_data + _y * tga_width * tga_comp;
269          rtga__getn(s, tga_row, tga_width * tga_comp);
270       }
271    }
272    else
273    {
274       int i, j;
275       int RLE_repeating          = 0;
276       int RLE_count              = 0;
277       int read_next_pixel        = 1;
278       unsigned char raw_data[4]  = {0};
279       unsigned char *tga_palette = NULL;
280 
281       /*   Do I need to load a palette? */
282       if (tga_indexed)
283       {
284          /*   any data to skip? (offset usually = 0) */
285          rtga__skip(s, tga_palette_start );
286          /*   load the palette */
287          tga_palette = (unsigned char*)malloc(tga_palette_len * tga_palette_bits / 8);
288 
289          if (!tga_palette)
290          {
291             free(tga_data);
292             return NULL;
293          }
294 
295          if (!rtga__getn(s, tga_palette, tga_palette_len * tga_palette_bits / 8 ))
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__pngchunk? */
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          for (j = 0; j*2 < tga_height; ++j)
360          {
361             int index1 = j * tga_width * tga_comp;
362             int index2 = (tga_height - 1 - j) * tga_width * tga_comp;
363 
364             for (i = tga_width * tga_comp; i > 0; --i)
365             {
366                unsigned char temp = tga_data[index1];
367                tga_data[index1] = tga_data[index2];
368                tga_data[index2] = temp;
369                ++index1;
370                ++index2;
371             }
372          }
373       }
374 
375       /* Clear my palette, if I had one */
376       if (tga_palette)
377          free(tga_palette);
378    }
379 
380    /* swap RGB */
381    if (tga_comp >= 3)
382    {
383       int i;
384       unsigned char* tga_pixel = tga_data;
385 
386       for (i = 0; i < tga_width * tga_height; ++i)
387       {
388          unsigned char temp  = tga_pixel[0];
389          tga_pixel[0]        = tga_pixel[2];
390          tga_pixel[2]        = temp;
391          tga_pixel          += tga_comp;
392       }
393    }
394 
395    /* convert to target component count */
396    if (     (req_comp)
397          && (req_comp >= 1 && req_comp <= 4)
398          && (req_comp != tga_comp))
399    {
400       tga_data = rtga__convert_format(tga_data, tga_comp, req_comp, tga_width, tga_height);
401    }
402 
403    return tga_data;
404 }
405 
rtga_load_from_memory(uint8_t const * buffer,int len,unsigned * x,unsigned * y,int * comp,int req_comp)406 static uint8_t *rtga_load_from_memory(uint8_t const *buffer, int len,
407       unsigned *x, unsigned *y, int *comp, int req_comp)
408 {
409    rtga__context s;
410 
411    s.img_buffer          = (uint8_t *)buffer;
412    s.img_buffer_original = (uint8_t *) buffer;
413    s.img_buffer_end      = (uint8_t *) buffer+len;
414 
415    return rtga__tga_load(&s,x,y,comp,req_comp);
416 }
417 
rtga_process_image(rtga_t * rtga,void ** buf_data,size_t size,unsigned * width,unsigned * height)418 int rtga_process_image(rtga_t *rtga, void **buf_data,
419       size_t size, unsigned *width, unsigned *height)
420 {
421    int comp;
422    unsigned size_tex     = 0;
423 
424    if (!rtga)
425       return IMAGE_PROCESS_ERROR;
426 
427    rtga->output_image   = (uint32_t*)rtga_load_from_memory(rtga->buff_data,
428                            (int)size, width, height, &comp, 4);
429    *buf_data             = rtga->output_image;
430    size_tex              = (*width) * (*height);
431 
432    /* Convert RGBA to ARGB */
433    while(size_tex--)
434    {
435       unsigned int texel = rtga->output_image[size_tex];
436       unsigned int A     = texel & 0xFF000000;
437       unsigned int B     = texel & 0x00FF0000;
438       unsigned int G     = texel & 0x0000FF00;
439       unsigned int R     = texel & 0x000000FF;
440       ((unsigned int*)rtga->output_image)[size_tex] = A | (R << 16) | G | (B >> 16);
441    };
442 
443    return IMAGE_PROCESS_END;
444 }
445 
rtga_set_buf_ptr(rtga_t * rtga,void * data)446 bool rtga_set_buf_ptr(rtga_t *rtga, void *data)
447 {
448    if (!rtga)
449       return false;
450 
451    rtga->buff_data = (uint8_t*)data;
452 
453    return true;
454 }
455 
rtga_free(rtga_t * rtga)456 void rtga_free(rtga_t *rtga)
457 {
458    if (!rtga)
459       return;
460 
461    free(rtga);
462 }
463 
rtga_alloc(void)464 rtga_t *rtga_alloc(void)
465 {
466    rtga_t *rtga = (rtga_t*)calloc(1, sizeof(*rtga));
467    if (!rtga)
468       return NULL;
469    return rtga;
470 }
471