mirror of
https://github.com/libretro/RetroArch
synced 2025-03-29 22:20:21 +00:00
(RTGA) Cleanups
This commit is contained in:
parent
a4ca4a7eb4
commit
80d8a90bce
@ -1,3 +1,27 @@
|
||||
/* Copyright (C) 2010-2016 The RetroArch team
|
||||
*
|
||||
* ---------------------------------------------------------------------------------------
|
||||
* The following license statement only applies to this file (rtga.c).
|
||||
* ---------------------------------------------------------------------------------------
|
||||
*
|
||||
* Permission is hereby granted, free of charge,
|
||||
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
/* Modified version of stb_image's TGA sources. */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdarg.h>
|
||||
@ -11,84 +35,32 @@
|
||||
#include <formats/image.h>
|
||||
#include <formats/rtga.h>
|
||||
|
||||
#define RTGA_COMPUTE_Y(r, g, b) ((uint8_t)((((r) * 77) + ((g) * 150) + (29 * (b))) >> 8))
|
||||
|
||||
|
||||
struct rtga
|
||||
{
|
||||
uint8_t *buff_data;
|
||||
uint32_t *output_image;
|
||||
void *empty;
|
||||
};
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int (*read) (void *user,char *data,int size); /* fill 'data' with 'size' bytes. return number of bytes actually read */
|
||||
void (*skip) (void *user,int n); /* skip the next 'n' bytes, or 'unget' the last -n bytes if negative */
|
||||
int (*eof) (void *user); /* returns nonzero if we are at end of file/data */
|
||||
} rtga_io_callbacks;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint32_t img_x, img_y;
|
||||
int img_n, img_out_n;
|
||||
|
||||
rtga_io_callbacks io;
|
||||
void *io_user_data;
|
||||
|
||||
int read_from_callbacks;
|
||||
int buflen;
|
||||
uint8_t buffer_start[128];
|
||||
|
||||
uint8_t *img_buffer, *img_buffer_end;
|
||||
uint8_t *img_buffer;
|
||||
uint8_t *img_buffer_end;
|
||||
uint8_t *img_buffer_original;
|
||||
} rtga__context;
|
||||
|
||||
static void rtga__refill_buffer(rtga__context *s);
|
||||
|
||||
static void rtga__start_mem(rtga__context *s, uint8_t const *buffer, int len)
|
||||
{
|
||||
s->io.read = NULL;
|
||||
s->read_from_callbacks = 0;
|
||||
s->img_buffer = s->img_buffer_original = (uint8_t *) buffer;
|
||||
s->img_buffer_end = (uint8_t *) buffer+len;
|
||||
}
|
||||
|
||||
static uint8_t *rtga__tga_load(rtga__context *s, unsigned *x, unsigned *y, int *comp, int req_comp);
|
||||
|
||||
#define rtga__err(x,y) 0
|
||||
#define rtga__errpf(x,y) ((float *) (rtga__err(x,y)?NULL:NULL))
|
||||
#define rtga__errpuc(x,y) ((unsigned char *) (rtga__err(x,y)?NULL:NULL))
|
||||
|
||||
static uint8_t *rtga_load_from_memory(uint8_t const *buffer, int len, unsigned *x, unsigned *y, int *comp, int req_comp)
|
||||
{
|
||||
rtga__context s;
|
||||
rtga__start_mem(&s,buffer,len);
|
||||
return rtga__tga_load(&s,x,y,comp,req_comp);
|
||||
}
|
||||
|
||||
static void rtga__refill_buffer(rtga__context *s)
|
||||
{
|
||||
int n = (s->io.read)(s->io_user_data,(char*)s->buffer_start,s->buflen);
|
||||
if (n == 0)
|
||||
{
|
||||
/* at end of file, treat same as if from memory, but need to handle case
|
||||
* where s->img_buffer isn't pointing to safe memory, e.g. 0-byte file */
|
||||
s->read_from_callbacks = 0;
|
||||
s->img_buffer = s->buffer_start;
|
||||
s->img_buffer_end = s->buffer_start+1;
|
||||
*s->img_buffer = 0;
|
||||
} else {
|
||||
s->img_buffer = s->buffer_start;
|
||||
s->img_buffer_end = s->buffer_start + n;
|
||||
}
|
||||
}
|
||||
|
||||
static INLINE uint8_t rtga__get8(rtga__context *s)
|
||||
{
|
||||
if (s->img_buffer < s->img_buffer_end)
|
||||
return *s->img_buffer++;
|
||||
if (s->read_from_callbacks) {
|
||||
rtga__refill_buffer(s);
|
||||
return *s->img_buffer++;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -98,39 +70,19 @@ static void rtga__skip(rtga__context *s, int n)
|
||||
s->img_buffer = s->img_buffer_end;
|
||||
return;
|
||||
}
|
||||
if (s->io.read) {
|
||||
int blen = (int) (s->img_buffer_end - s->img_buffer);
|
||||
if (blen < n) {
|
||||
s->img_buffer = s->img_buffer_end;
|
||||
(s->io.skip)(s->io_user_data, n - blen);
|
||||
return;
|
||||
}
|
||||
}
|
||||
s->img_buffer += n;
|
||||
}
|
||||
|
||||
static int rtga__getn(rtga__context *s, uint8_t *buffer, int n)
|
||||
{
|
||||
if (s->io.read) {
|
||||
int blen = (int) (s->img_buffer_end - s->img_buffer);
|
||||
if (blen < n) {
|
||||
int res, count;
|
||||
|
||||
memcpy(buffer, s->img_buffer, blen);
|
||||
|
||||
count = (s->io.read)(s->io_user_data, (char*) buffer + blen, n - blen);
|
||||
res = (count == (n-blen));
|
||||
s->img_buffer = s->img_buffer_end;
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
if (s->img_buffer+n <= s->img_buffer_end) {
|
||||
if (s->img_buffer+n <= s->img_buffer_end)
|
||||
{
|
||||
memcpy(buffer, s->img_buffer, n);
|
||||
s->img_buffer += n;
|
||||
return 1;
|
||||
} else
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int rtga__get16le(rtga__context *s)
|
||||
@ -139,11 +91,6 @@ static int rtga__get16le(rtga__context *s)
|
||||
return z + (rtga__get8(s) << 8);
|
||||
}
|
||||
|
||||
static uint8_t rtga__compute_y(int r, int g, int b)
|
||||
{
|
||||
return (uint8_t) (((r*77) + (g*150) + (29*b)) >> 8);
|
||||
}
|
||||
|
||||
static unsigned char *rtga__convert_format(
|
||||
unsigned char *data,
|
||||
int img_n,
|
||||
@ -152,16 +99,12 @@ static unsigned char *rtga__convert_format(
|
||||
unsigned int y)
|
||||
{
|
||||
int i,j;
|
||||
unsigned char *good;
|
||||
unsigned char *good = (unsigned char *) malloc(req_comp * x * y);
|
||||
|
||||
if (req_comp == img_n) return data;
|
||||
retro_assert(req_comp >= 1 && req_comp <= 4);
|
||||
|
||||
good = (unsigned char *) malloc(req_comp * x * y);
|
||||
if (good == NULL)
|
||||
if (!good)
|
||||
{
|
||||
free(data);
|
||||
return rtga__errpuc("outofmem", "Out of memory");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (j=0; j < (int) y; ++j)
|
||||
@ -201,19 +144,19 @@ static unsigned char *rtga__convert_format(
|
||||
break;
|
||||
case ((3)*8+(1)):
|
||||
for(i=x-1; i >= 0; --i, src += 3, dest += 1)
|
||||
dest[0]=rtga__compute_y(src[0],src[1],src[2]);
|
||||
dest[0] = RTGA_COMPUTE_Y(src[0],src[1],src[2]);
|
||||
break;
|
||||
case ((3)*8+(2)):
|
||||
for(i=x-1; i >= 0; --i, src += 3, dest += 2)
|
||||
dest[0]=rtga__compute_y(src[0],src[1],src[2]), dest[1] = 255;
|
||||
dest[0] = RTGA_COMPUTE_Y(src[0],src[1],src[2]), dest[1] = 255;
|
||||
break;
|
||||
case ((4)*8+(1)):
|
||||
for(i=x-1; i >= 0; --i, src += 4, dest += 1)
|
||||
dest[0]=rtga__compute_y(src[0],src[1],src[2]);
|
||||
dest[0] = RTGA_COMPUTE_Y(src[0],src[1],src[2]);
|
||||
break;
|
||||
case ((4)*8+(2)):
|
||||
for(i=x-1; i >= 0; --i, src += 4, dest += 2)
|
||||
dest[0]=rtga__compute_y(src[0],src[1],src[2]), dest[1] = src[3];
|
||||
dest[0] = RTGA_COMPUTE_Y(src[0],src[1],src[2]), dest[1] = src[3];
|
||||
break;
|
||||
case ((4)*8+(3)):
|
||||
for(i=x-1; i >= 0; --i, src += 4, dest += 3)
|
||||
@ -223,39 +166,33 @@ static unsigned char *rtga__convert_format(
|
||||
retro_assert(0);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
free(data);
|
||||
return good;
|
||||
}
|
||||
|
||||
static uint8_t *rtga__tga_load(rtga__context *s, unsigned *x, unsigned *y, int *comp, int req_comp)
|
||||
static uint8_t *rtga__tga_load(rtga__context *s,
|
||||
unsigned *x, unsigned *y, int *comp, int req_comp)
|
||||
{
|
||||
/* Read in the TGA header stuff */
|
||||
int tga_offset = rtga__get8(s);
|
||||
int tga_indexed = rtga__get8(s);
|
||||
int tga_image_type = rtga__get8(s);
|
||||
int tga_is_RLE = 0;
|
||||
int tga_palette_start = rtga__get16le(s);
|
||||
int tga_palette_len = rtga__get16le(s);
|
||||
int tga_palette_bits = rtga__get8(s);
|
||||
int tga_x_origin = rtga__get16le(s);
|
||||
int tga_y_origin = rtga__get16le(s);
|
||||
int tga_width = rtga__get16le(s);
|
||||
int tga_height = rtga__get16le(s);
|
||||
int tga_bits_per_pixel = rtga__get8(s);
|
||||
int tga_comp = tga_bits_per_pixel / 8;
|
||||
int tga_inverted = rtga__get8(s);
|
||||
int tga_offset = rtga__get8(s);
|
||||
int tga_indexed = rtga__get8(s);
|
||||
int tga_image_type = rtga__get8(s);
|
||||
int tga_is_RLE = 0;
|
||||
int tga_palette_start = rtga__get16le(s);
|
||||
int tga_palette_len = rtga__get16le(s);
|
||||
int tga_palette_bits = rtga__get8(s);
|
||||
int tga_x_origin = rtga__get16le(s);
|
||||
int tga_y_origin = rtga__get16le(s);
|
||||
int tga_width = rtga__get16le(s);
|
||||
int tga_height = rtga__get16le(s);
|
||||
int tga_bits_per_pixel = rtga__get8(s);
|
||||
int tga_comp = tga_bits_per_pixel / 8;
|
||||
int tga_inverted = rtga__get8(s);
|
||||
|
||||
/* image data */
|
||||
unsigned char *tga_data;
|
||||
unsigned char *tga_palette = NULL;
|
||||
int i, j;
|
||||
unsigned char raw_data[4] = {0};
|
||||
int RLE_count = 0;
|
||||
int RLE_repeating = 0;
|
||||
int read_next_pixel = 1;
|
||||
unsigned char *tga_data = NULL;
|
||||
|
||||
(void)tga_palette_start;
|
||||
(void)tga_x_origin;
|
||||
@ -273,10 +210,12 @@ static uint8_t *rtga__tga_load(rtga__context *s, unsigned *x, unsigned *y, int *
|
||||
|
||||
/* error check */
|
||||
if (
|
||||
(tga_width < 1) || (tga_height < 1) ||
|
||||
(tga_image_type < 1) || (tga_image_type > 3) ||
|
||||
((tga_bits_per_pixel != 8) && (tga_bits_per_pixel != 16) &&
|
||||
(tga_bits_per_pixel != 24) && (tga_bits_per_pixel != 32))
|
||||
(tga_width < 1) || (tga_height < 1) ||
|
||||
(tga_image_type < 1) || (tga_image_type > 3) ||
|
||||
(
|
||||
(tga_bits_per_pixel != 8) && (tga_bits_per_pixel != 16) &&
|
||||
(tga_bits_per_pixel != 24) && (tga_bits_per_pixel != 32)
|
||||
)
|
||||
)
|
||||
return NULL; /* we don't report this as a bad TGA because we don't even know if it's TGA */
|
||||
|
||||
@ -291,13 +230,14 @@ static uint8_t *rtga__tga_load(rtga__context *s, unsigned *x, unsigned *y, int *
|
||||
|
||||
tga_data = (unsigned char*)malloc( (size_t)tga_width * tga_height * tga_comp );
|
||||
if (!tga_data)
|
||||
return rtga__errpuc("outofmem", "Out of memory");
|
||||
return NULL;
|
||||
|
||||
/* skip to the data's starting position (offset usually = 0) */
|
||||
rtga__skip(s, tga_offset );
|
||||
|
||||
if ( !tga_indexed && !tga_is_RLE)
|
||||
{
|
||||
unsigned i;
|
||||
for (i=0; i < tga_height; ++i)
|
||||
{
|
||||
int y = tga_inverted ? tga_height -i - 1 : i;
|
||||
@ -307,6 +247,13 @@ static uint8_t *rtga__tga_load(rtga__context *s, unsigned *x, unsigned *y, int *
|
||||
}
|
||||
else
|
||||
{
|
||||
unsigned i, j;
|
||||
int RLE_repeating = 0;
|
||||
int RLE_count = 0;
|
||||
int read_next_pixel = 1;
|
||||
unsigned char raw_data[4] = {0};
|
||||
unsigned char *tga_palette = NULL;
|
||||
|
||||
/* Do I need to load a palette? */
|
||||
if ( tga_indexed)
|
||||
{
|
||||
@ -318,12 +265,14 @@ static uint8_t *rtga__tga_load(rtga__context *s, unsigned *x, unsigned *y, int *
|
||||
if (!tga_palette)
|
||||
{
|
||||
free(tga_data);
|
||||
return rtga__errpuc("outofmem", "Out of memory");
|
||||
return NULL;
|
||||
}
|
||||
if (!rtga__getn(s, tga_palette, tga_palette_len * tga_palette_bits / 8 )) {
|
||||
|
||||
if (!rtga__getn(s, tga_palette, tga_palette_len * tga_palette_bits / 8 ))
|
||||
{
|
||||
free(tga_data);
|
||||
free(tga_palette);
|
||||
return rtga__errpuc("bad palette", "Corrupt TGA");
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
@ -374,7 +323,7 @@ static uint8_t *rtga__tga_load(rtga__context *s, unsigned *x, unsigned *y, int *
|
||||
|
||||
/* copy data */
|
||||
for (j = 0; j < tga_comp; ++j)
|
||||
tga_data[i*tga_comp+j] = raw_data[j];
|
||||
tga_data[i*tga_comp+j] = raw_data[j];
|
||||
|
||||
/* in case we're in RLE mode, keep counting down */
|
||||
--RLE_count;
|
||||
@ -387,6 +336,7 @@ static uint8_t *rtga__tga_load(rtga__context *s, unsigned *x, unsigned *y, int *
|
||||
{
|
||||
int index1 = j * tga_width * tga_comp;
|
||||
int index2 = (tga_height - 1 - j) * tga_width * tga_comp;
|
||||
|
||||
for (i = tga_width * tga_comp; i > 0; --i)
|
||||
{
|
||||
unsigned char temp = tga_data[index1];
|
||||
@ -406,9 +356,10 @@ static uint8_t *rtga__tga_load(rtga__context *s, unsigned *x, unsigned *y, int *
|
||||
/* swap RGB */
|
||||
if (tga_comp >= 3)
|
||||
{
|
||||
unsigned i;
|
||||
unsigned char* tga_pixel = tga_data;
|
||||
|
||||
for (i=0; i < tga_width * tga_height; ++i)
|
||||
for (i = 0; i < tga_width * tga_height; ++i)
|
||||
{
|
||||
unsigned char temp = tga_pixel[0];
|
||||
tga_pixel[0] = tga_pixel[2];
|
||||
@ -418,12 +369,28 @@ static uint8_t *rtga__tga_load(rtga__context *s, unsigned *x, unsigned *y, int *
|
||||
}
|
||||
|
||||
/* convert to target component count */
|
||||
if (req_comp && req_comp != tga_comp)
|
||||
if ( (req_comp)
|
||||
&& (req_comp >= 1 && req_comp <= 4)
|
||||
&& (req_comp != tga_comp))
|
||||
{
|
||||
tga_data = rtga__convert_format(tga_data, tga_comp, req_comp, tga_width, tga_height);
|
||||
}
|
||||
|
||||
return tga_data;
|
||||
}
|
||||
|
||||
static uint8_t *rtga_load_from_memory(uint8_t const *buffer, int len,
|
||||
unsigned *x, unsigned *y, int *comp, int req_comp)
|
||||
{
|
||||
rtga__context s;
|
||||
|
||||
s.img_buffer = (uint8_t *)buffer;
|
||||
s.img_buffer_original = (uint8_t *) buffer;
|
||||
s.img_buffer_end = (uint8_t *) buffer+len;
|
||||
|
||||
return rtga__tga_load(&s,x,y,comp,req_comp);
|
||||
}
|
||||
|
||||
int rtga_process_image(rtga_t *rtga, void **buf_data,
|
||||
size_t size, unsigned *width, unsigned *height)
|
||||
{
|
||||
@ -433,7 +400,8 @@ int rtga_process_image(rtga_t *rtga, void **buf_data,
|
||||
if (!rtga)
|
||||
return IMAGE_PROCESS_ERROR;
|
||||
|
||||
rtga->output_image = (uint32_t*)rtga_load_from_memory(rtga->buff_data, size, width, height, &comp, 4);
|
||||
rtga->output_image = (uint32_t*)rtga_load_from_memory(rtga->buff_data,
|
||||
size, width, height, &comp, 4);
|
||||
*buf_data = rtga->output_image;
|
||||
size_tex = (*width) * (*height);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user