mirror of
https://github.com/libretro/RetroArch
synced 2025-04-25 09:02:44 +00:00
Start reimplementing TGA image load support
This commit is contained in:
parent
248b99e8e7
commit
9bc11731fb
@ -26,9 +26,6 @@
|
|||||||
#include <boolean.h>
|
#include <boolean.h>
|
||||||
#include <formats/image.h>
|
#include <formats/image.h>
|
||||||
#include <file/nbio.h>
|
#include <file/nbio.h>
|
||||||
#ifdef HAVE_RTGA
|
|
||||||
#include <formats/tga.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "../general.h"
|
#include "../general.h"
|
||||||
|
|
||||||
@ -178,6 +175,12 @@ static bool video_texture_image_load_internal(
|
|||||||
#endif
|
#endif
|
||||||
case IMAGE_TYPE_BMP:
|
case IMAGE_TYPE_BMP:
|
||||||
break;
|
break;
|
||||||
|
case IMAGE_TYPE_TGA:
|
||||||
|
#ifdef HAVE_RTGA
|
||||||
|
break;
|
||||||
|
#else
|
||||||
|
return false;
|
||||||
|
#endif
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -261,8 +264,10 @@ static enum image_type_enum video_texture_image_convert_fmt_to_type(enum video_i
|
|||||||
return IMAGE_TYPE_JPEG;
|
return IMAGE_TYPE_JPEG;
|
||||||
case IMAGE_FORMAT_BMP:
|
case IMAGE_FORMAT_BMP:
|
||||||
return IMAGE_TYPE_BMP;
|
return IMAGE_TYPE_BMP;
|
||||||
default:
|
case IMAGE_FORMAT_TGA:
|
||||||
|
return IMAGE_TYPE_TGA;
|
||||||
case IMAGE_FORMAT_NONE:
|
case IMAGE_FORMAT_NONE:
|
||||||
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -281,11 +286,8 @@ bool video_texture_image_load(struct texture_image *out_img,
|
|||||||
video_texture_image_set_color_shifts(&r_shift, &g_shift, &b_shift,
|
video_texture_image_set_color_shifts(&r_shift, &g_shift, &b_shift,
|
||||||
&a_shift);
|
&a_shift);
|
||||||
|
|
||||||
switch (fmt)
|
if (fmt != IMAGE_FORMAT_NONE)
|
||||||
{
|
{
|
||||||
case IMAGE_FORMAT_NONE:
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
handle = (struct nbio_t*)nbio_open(path, NBIO_READ);
|
handle = (struct nbio_t*)nbio_open(path, NBIO_READ);
|
||||||
if (!handle)
|
if (!handle)
|
||||||
goto error;
|
goto error;
|
||||||
@ -297,30 +299,12 @@ bool video_texture_image_load(struct texture_image *out_img,
|
|||||||
|
|
||||||
if (!ptr)
|
if (!ptr)
|
||||||
goto error;
|
goto error;
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (fmt)
|
|
||||||
{
|
|
||||||
case IMAGE_FORMAT_TGA:
|
|
||||||
#ifdef HAVE_RTGA
|
|
||||||
if (rtga_image_load_shift((uint8_t*)ptr, out_img,
|
|
||||||
a_shift, r_shift, g_shift, b_shift))
|
|
||||||
goto success;
|
|
||||||
#endif
|
|
||||||
break;
|
|
||||||
case IMAGE_FORMAT_BMP:
|
|
||||||
case IMAGE_FORMAT_PNG:
|
|
||||||
case IMAGE_FORMAT_JPEG:
|
|
||||||
if (video_texture_image_load_internal(
|
if (video_texture_image_load_internal(
|
||||||
video_texture_image_convert_fmt_to_type(fmt),
|
video_texture_image_convert_fmt_to_type(fmt),
|
||||||
ptr,out_img,
|
ptr,out_img,
|
||||||
a_shift, r_shift, g_shift, b_shift))
|
a_shift, r_shift, g_shift, b_shift))
|
||||||
goto success;
|
goto success;
|
||||||
break;
|
|
||||||
default:
|
|
||||||
case IMAGE_FORMAT_NONE:
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
error:
|
error:
|
||||||
|
@ -8,6 +8,9 @@
|
|||||||
#ifdef HAVE_RJPEG
|
#ifdef HAVE_RJPEG
|
||||||
#include <formats/rjpeg.h>
|
#include <formats/rjpeg.h>
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef HAVE_RTGA
|
||||||
|
#include <formats/rtga.h>
|
||||||
|
#endif
|
||||||
#include <formats/rbmp.h>
|
#include <formats/rbmp.h>
|
||||||
|
|
||||||
#include <formats/image.h>
|
#include <formats/image.h>
|
||||||
@ -16,6 +19,11 @@ void image_transfer_free(void *data, enum image_type_enum type)
|
|||||||
{
|
{
|
||||||
switch (type)
|
switch (type)
|
||||||
{
|
{
|
||||||
|
case IMAGE_TYPE_TGA:
|
||||||
|
#ifdef HAVE_RTGA
|
||||||
|
rtga_free((rtga_t*)data);
|
||||||
|
#endif
|
||||||
|
break;
|
||||||
case IMAGE_TYPE_PNG:
|
case IMAGE_TYPE_PNG:
|
||||||
#ifdef HAVE_RPNG
|
#ifdef HAVE_RPNG
|
||||||
rpng_free((rpng_t*)data);
|
rpng_free((rpng_t*)data);
|
||||||
@ -75,6 +83,11 @@ bool image_transfer_start(void *data, enum image_type_enum type)
|
|||||||
case IMAGE_TYPE_JPEG:
|
case IMAGE_TYPE_JPEG:
|
||||||
#ifdef HAVE_RJPEG
|
#ifdef HAVE_RJPEG
|
||||||
return true;
|
return true;
|
||||||
|
#endif
|
||||||
|
break;
|
||||||
|
case IMAGE_TYPE_TGA:
|
||||||
|
#ifdef HAVE_RTGA
|
||||||
|
return true;
|
||||||
#endif
|
#endif
|
||||||
break;
|
break;
|
||||||
case IMAGE_TYPE_BMP:
|
case IMAGE_TYPE_BMP:
|
||||||
@ -103,6 +116,12 @@ bool image_transfer_is_valid(
|
|||||||
return true;
|
return true;
|
||||||
#else
|
#else
|
||||||
break;
|
break;
|
||||||
|
#endif
|
||||||
|
case IMAGE_TYPE_TGA:
|
||||||
|
#ifdef HAVE_RTGA
|
||||||
|
return true;
|
||||||
|
#else
|
||||||
|
break;
|
||||||
#endif
|
#endif
|
||||||
case IMAGE_TYPE_BMP:
|
case IMAGE_TYPE_BMP:
|
||||||
return true;
|
return true;
|
||||||
@ -128,6 +147,11 @@ void image_transfer_set_buffer_ptr(
|
|||||||
case IMAGE_TYPE_JPEG:
|
case IMAGE_TYPE_JPEG:
|
||||||
#ifdef HAVE_RJPEG
|
#ifdef HAVE_RJPEG
|
||||||
rjpeg_set_buf_ptr((rjpeg_t*)data, (uint8_t*)ptr);
|
rjpeg_set_buf_ptr((rjpeg_t*)data, (uint8_t*)ptr);
|
||||||
|
#endif
|
||||||
|
break;
|
||||||
|
case IMAGE_TYPE_TGA:
|
||||||
|
#ifdef HAVE_RTGA
|
||||||
|
rtga_set_buf_ptr((rtga_t*)data, (uint8_t*)ptr);
|
||||||
#endif
|
#endif
|
||||||
break;
|
break;
|
||||||
case IMAGE_TYPE_BMP:
|
case IMAGE_TYPE_BMP:
|
||||||
@ -163,6 +187,13 @@ int image_transfer_process(
|
|||||||
(void**)buf, len, width, height);
|
(void**)buf, len, width, height);
|
||||||
#else
|
#else
|
||||||
break;
|
break;
|
||||||
|
#endif
|
||||||
|
case IMAGE_TYPE_TGA:
|
||||||
|
#ifdef HAVE_RTGA
|
||||||
|
return rtga_process_image((rtga_t*)data,
|
||||||
|
(void**)buf, len, width, height);
|
||||||
|
#else
|
||||||
|
break;
|
||||||
#endif
|
#endif
|
||||||
case IMAGE_TYPE_BMP:
|
case IMAGE_TYPE_BMP:
|
||||||
return rbmp_process_image((rbmp_t*)data,
|
return rbmp_process_image((rbmp_t*)data,
|
||||||
@ -190,6 +221,12 @@ bool image_transfer_iterate(void *data, enum image_type_enum type)
|
|||||||
return false;
|
return false;
|
||||||
#else
|
#else
|
||||||
break;
|
break;
|
||||||
|
#endif
|
||||||
|
case IMAGE_TYPE_TGA:
|
||||||
|
#ifdef HAVE_RTGA
|
||||||
|
return false;
|
||||||
|
#else
|
||||||
|
break;
|
||||||
#endif
|
#endif
|
||||||
case IMAGE_TYPE_BMP:
|
case IMAGE_TYPE_BMP:
|
||||||
return false;
|
return false;
|
||||||
|
@ -1,103 +1,480 @@
|
|||||||
/* 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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <stddef.h> /* ptrdiff_t on osx */
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include <formats/tga.h>
|
#include <retro_assert.h>
|
||||||
|
#include <retro_inline.h>
|
||||||
|
|
||||||
#include <formats/image.h>
|
#include <formats/image.h>
|
||||||
|
#include <formats/rtga.h>
|
||||||
|
|
||||||
bool rtga_image_load_shift(uint8_t *buf,
|
struct rtga
|
||||||
void *data,
|
|
||||||
unsigned a_shift, unsigned r_shift,
|
|
||||||
unsigned g_shift, unsigned b_shift)
|
|
||||||
{
|
{
|
||||||
unsigned i, bits, size, bits_mul;
|
uint8_t *buff_data;
|
||||||
uint8_t info[6] = {0};
|
uint32_t *output_image;
|
||||||
unsigned width = 0;
|
void *empty;
|
||||||
unsigned height = 0;
|
};
|
||||||
const uint8_t *tmp = NULL;
|
|
||||||
struct texture_image *out_img = (struct texture_image*)data;
|
|
||||||
|
|
||||||
if (!buf || buf[2] != 2)
|
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_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)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "TGA image is not uncompressed RGB.\n");
|
/* at end of file, treat same as if from memory, but need to handle case
|
||||||
goto error;
|
* 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void rtga__skip(rtga__context *s, int n)
|
||||||
|
{
|
||||||
|
if (n < 0) {
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(info, buf + 12, 6);
|
if (s->img_buffer+n <= s->img_buffer_end) {
|
||||||
|
memcpy(buffer, s->img_buffer, n);
|
||||||
|
s->img_buffer += n;
|
||||||
|
return 1;
|
||||||
|
} else
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
width = info[0] + ((unsigned)info[1] * 256);
|
static int rtga__get16le(rtga__context *s)
|
||||||
height = info[2] + ((unsigned)info[3] * 256);
|
{
|
||||||
bits = info[4];
|
int z = rtga__get8(s);
|
||||||
|
return z + (rtga__get8(s) << 8);
|
||||||
|
}
|
||||||
|
|
||||||
fprintf(stderr, "Loaded TGA: (%ux%u @ %u bpp)\n", width, height, bits);
|
static uint8_t rtga__compute_y(int r, int g, int b)
|
||||||
|
{
|
||||||
|
return (uint8_t) (((r*77) + (g*150) + (29*b)) >> 8);
|
||||||
|
}
|
||||||
|
|
||||||
size = width * height * sizeof(uint32_t);
|
static unsigned char *rtga__convert_format(
|
||||||
out_img->pixels = (uint32_t*)malloc(size);
|
unsigned char *data,
|
||||||
out_img->width = width;
|
int img_n,
|
||||||
out_img->height = height;
|
int req_comp,
|
||||||
|
unsigned int x,
|
||||||
|
unsigned int y)
|
||||||
|
{
|
||||||
|
int i,j;
|
||||||
|
unsigned char *good;
|
||||||
|
|
||||||
if (!out_img->pixels)
|
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)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Failed to allocate TGA pixels.\n");
|
free(data);
|
||||||
goto error;
|
return rtga__errpuc("outofmem", "Out of memory");
|
||||||
}
|
}
|
||||||
|
|
||||||
tmp = buf + 18;
|
for (j=0; j < (int) y; ++j)
|
||||||
bits_mul = 3;
|
|
||||||
|
|
||||||
if (bits != 32 && bits != 24)
|
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Bit depth of TGA image is wrong. Only 32-bit and 24-bit supported.\n");
|
unsigned char *src = data + j * x * img_n ;
|
||||||
goto error;
|
unsigned char *dest = good + j * x * req_comp;
|
||||||
|
|
||||||
|
switch (((img_n)*8+(req_comp)))
|
||||||
|
{
|
||||||
|
case ((1)*8+(2)):
|
||||||
|
for(i=x-1; i >= 0; --i, src += 1, dest += 2)
|
||||||
|
dest[0]=src[0], dest[1]=255;
|
||||||
|
break;
|
||||||
|
case ((1)*8+(3)):
|
||||||
|
for(i=x-1; i >= 0; --i, src += 1, dest += 3)
|
||||||
|
dest[0]=dest[1]=dest[2]=src[0];
|
||||||
|
break;
|
||||||
|
case ((1)*8+(4)):
|
||||||
|
for(i=x-1; i >= 0; --i, src += 1, dest += 4)
|
||||||
|
dest[0]=dest[1]=dest[2]=src[0], dest[3]=255;
|
||||||
|
break;
|
||||||
|
case ((2)*8+(1)):
|
||||||
|
for(i=x-1; i >= 0; --i, src += 2, dest += 1)
|
||||||
|
dest[0]=src[0];
|
||||||
|
break;
|
||||||
|
case ((2)*8+(3)):
|
||||||
|
for(i=x-1; i >= 0; --i, src += 2, dest += 3)
|
||||||
|
dest[0]=dest[1]=dest[2]=src[0];
|
||||||
|
break;
|
||||||
|
case ((2)*8+(4)):
|
||||||
|
for(i=x-1; i >= 0; --i, src += 2, dest += 4)
|
||||||
|
dest[0]=dest[1]=dest[2]=src[0], dest[3]=src[1];
|
||||||
|
break;
|
||||||
|
case ((3)*8+(4)):
|
||||||
|
for(i=x-1; i >= 0; --i, src += 3, dest += 4)
|
||||||
|
dest[0]=src[0],dest[1]=src[1],dest[2]=src[2],dest[3]=255;
|
||||||
|
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]);
|
||||||
|
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;
|
||||||
|
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]);
|
||||||
|
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];
|
||||||
|
break;
|
||||||
|
case ((4)*8+(3)):
|
||||||
|
for(i=x-1; i >= 0; --i, src += 4, dest += 3)
|
||||||
|
dest[0]=src[0],dest[1]=src[1],dest[2]=src[2];
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
retro_assert(0);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bits == 32)
|
|
||||||
bits_mul = 4;
|
|
||||||
|
|
||||||
for (i = 0; i < width * height; i++)
|
|
||||||
{
|
|
||||||
uint32_t b = tmp[i * bits_mul + 0];
|
|
||||||
uint32_t g = tmp[i * bits_mul + 1];
|
|
||||||
uint32_t r = tmp[i * bits_mul + 2];
|
|
||||||
uint32_t a = tmp[i * bits_mul + 3];
|
|
||||||
|
|
||||||
if (bits == 24)
|
|
||||||
a = 0xff;
|
|
||||||
|
|
||||||
out_img->pixels[i] = (a << a_shift) |
|
|
||||||
(r << r_shift) | (g << g_shift) | (b << b_shift);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
free(data);
|
||||||
|
return good;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
/* 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;
|
||||||
|
|
||||||
|
/* do a tiny bit of precessing */
|
||||||
|
if ( tga_image_type >= 8 )
|
||||||
|
{
|
||||||
|
tga_image_type -= 8;
|
||||||
|
tga_is_RLE = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* int tga_alpha_bits = tga_inverted & 15; */
|
||||||
|
tga_inverted = 1 - ((tga_inverted >> 5) & 1);
|
||||||
|
|
||||||
|
/* 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))
|
||||||
|
)
|
||||||
|
return NULL; /* we don't report this as a bad TGA because we don't even know if it's TGA */
|
||||||
|
|
||||||
|
/* If paletted, then we will use the number of bits from the palette */
|
||||||
|
if ( tga_indexed )
|
||||||
|
tga_comp = tga_palette_bits / 8;
|
||||||
|
|
||||||
|
/* TGA info */
|
||||||
|
*x = tga_width;
|
||||||
|
*y = tga_height;
|
||||||
|
if (comp) *comp = tga_comp;
|
||||||
|
|
||||||
|
tga_data = (unsigned char*)malloc( (size_t)tga_width * tga_height * tga_comp );
|
||||||
|
if (!tga_data)
|
||||||
|
return rtga__errpuc("outofmem", "Out of memory");
|
||||||
|
|
||||||
|
/* skip to the data's starting position (offset usually = 0) */
|
||||||
|
rtga__skip(s, tga_offset );
|
||||||
|
|
||||||
|
if ( !tga_indexed && !tga_is_RLE)
|
||||||
|
{
|
||||||
|
for (i=0; i < tga_height; ++i)
|
||||||
|
{
|
||||||
|
int y = tga_inverted ? tga_height -i - 1 : i;
|
||||||
|
uint8_t *tga_row = tga_data + y*tga_width*tga_comp;
|
||||||
|
rtga__getn(s, tga_row, tga_width * tga_comp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Do I need to load a palette? */
|
||||||
|
if ( tga_indexed)
|
||||||
|
{
|
||||||
|
/* any data to skip? (offset usually = 0) */
|
||||||
|
rtga__skip(s, tga_palette_start );
|
||||||
|
/* load the palette */
|
||||||
|
tga_palette = (unsigned char*)malloc( tga_palette_len * tga_palette_bits / 8 );
|
||||||
|
|
||||||
|
if (!tga_palette)
|
||||||
|
{
|
||||||
|
free(tga_data);
|
||||||
|
return rtga__errpuc("outofmem", "Out of memory");
|
||||||
|
}
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* load the data */
|
||||||
|
for (i=0; i < tga_width * tga_height; ++i)
|
||||||
|
{
|
||||||
|
/* if I'm in RLE mode, do I need to get a RLE rtga__pngchunk? */
|
||||||
|
if ( tga_is_RLE )
|
||||||
|
{
|
||||||
|
if ( RLE_count == 0 )
|
||||||
|
{
|
||||||
|
/* yep, get the next byte as a RLE command */
|
||||||
|
int RLE_cmd = rtga__get8(s);
|
||||||
|
RLE_count = 1 + (RLE_cmd & 127);
|
||||||
|
RLE_repeating = RLE_cmd >> 7;
|
||||||
|
read_next_pixel = 1;
|
||||||
|
}
|
||||||
|
else if ( !RLE_repeating )
|
||||||
|
read_next_pixel = 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
read_next_pixel = 1;
|
||||||
|
|
||||||
|
/* OK, if I need to read a pixel, do it now */
|
||||||
|
if ( read_next_pixel )
|
||||||
|
{
|
||||||
|
/* load however much data we did have */
|
||||||
|
if ( tga_indexed )
|
||||||
|
{
|
||||||
|
/* read in 1 byte, then perform the lookup */
|
||||||
|
int pal_idx = rtga__get8(s);
|
||||||
|
if ( pal_idx >= tga_palette_len ) /* invalid index */
|
||||||
|
pal_idx = 0;
|
||||||
|
pal_idx *= tga_bits_per_pixel / 8;
|
||||||
|
for (j = 0; j*8 < tga_bits_per_pixel; ++j)
|
||||||
|
raw_data[j] = tga_palette[pal_idx+j];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* read in the data raw */
|
||||||
|
for (j = 0; j*8 < tga_bits_per_pixel; ++j)
|
||||||
|
raw_data[j] = rtga__get8(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* clear the reading flag for the next pixel */
|
||||||
|
read_next_pixel = 0;
|
||||||
|
} /* end of reading a pixel */
|
||||||
|
|
||||||
|
/* copy data */
|
||||||
|
for (j = 0; j < tga_comp; ++j)
|
||||||
|
tga_data[i*tga_comp+j] = raw_data[j];
|
||||||
|
|
||||||
|
/* in case we're in RLE mode, keep counting down */
|
||||||
|
--RLE_count;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* do I need to invert the image? */
|
||||||
|
if ( tga_inverted )
|
||||||
|
{
|
||||||
|
for (j = 0; j*2 < tga_height; ++j)
|
||||||
|
{
|
||||||
|
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];
|
||||||
|
tga_data[index1] = tga_data[index2];
|
||||||
|
tga_data[index2] = temp;
|
||||||
|
++index1;
|
||||||
|
++index2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Clear my palette, if I had one */
|
||||||
|
if ( tga_palette != NULL )
|
||||||
|
free( tga_palette );
|
||||||
|
}
|
||||||
|
|
||||||
|
/* swap RGB */
|
||||||
|
if (tga_comp >= 3)
|
||||||
|
{
|
||||||
|
unsigned char* tga_pixel = tga_data;
|
||||||
|
|
||||||
|
for (i=0; i < tga_width * tga_height; ++i)
|
||||||
|
{
|
||||||
|
unsigned char temp = tga_pixel[0];
|
||||||
|
tga_pixel[0] = tga_pixel[2];
|
||||||
|
tga_pixel[2] = temp;
|
||||||
|
tga_pixel += tga_comp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* convert to target component count */
|
||||||
|
if (req_comp && req_comp != tga_comp)
|
||||||
|
tga_data = rtga__convert_format(tga_data, tga_comp, req_comp, tga_width, tga_height);
|
||||||
|
|
||||||
|
tga_palette_start = tga_palette_len = tga_palette_bits =
|
||||||
|
tga_x_origin = tga_y_origin = 0;
|
||||||
|
|
||||||
|
return tga_data;
|
||||||
|
}
|
||||||
|
|
||||||
|
int rtga_process_image(rtga_t *rtga, void **buf_data,
|
||||||
|
size_t size, unsigned *width, unsigned *height)
|
||||||
|
{
|
||||||
|
int comp;
|
||||||
|
unsigned size_tex = 0;
|
||||||
|
|
||||||
|
if (!rtga)
|
||||||
|
return IMAGE_PROCESS_ERROR;
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
printf("GETS HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
|
||||||
|
#if 0
|
||||||
|
/* Convert RGBA to ARGB */
|
||||||
|
do
|
||||||
|
{
|
||||||
|
unsigned int texel = rtga->output_image[size_tex];
|
||||||
|
unsigned int A = texel & 0xFF000000;
|
||||||
|
unsigned int B = texel & 0x00FF0000;
|
||||||
|
unsigned int G = texel & 0x0000FF00;
|
||||||
|
unsigned int R = texel & 0x000000FF;
|
||||||
|
((unsigned int*)rtga->output_image)[size_tex] = A | (R << 16) | G | (B >> 16);
|
||||||
|
}while(size_tex--);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return IMAGE_PROCESS_END;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool rtga_set_buf_ptr(rtga_t *rtga, void *data)
|
||||||
|
{
|
||||||
|
if (!rtga)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
rtga->buff_data = (uint8_t*)data;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
error:
|
|
||||||
if (out_img->pixels)
|
void rtga_free(rtga_t *rtga)
|
||||||
free(out_img->pixels);
|
{
|
||||||
|
if (!rtga)
|
||||||
out_img->pixels = NULL;
|
return;
|
||||||
out_img->width = out_img->height = 0;
|
|
||||||
return false;
|
free(rtga);
|
||||||
|
}
|
||||||
|
|
||||||
|
rtga_t *rtga_alloc(void)
|
||||||
|
{
|
||||||
|
rtga_t *rtga = (rtga_t*)calloc(1, sizeof(*rtga));
|
||||||
|
if (!rtga)
|
||||||
|
return NULL;
|
||||||
|
return rtga;
|
||||||
}
|
}
|
||||||
|
@ -45,7 +45,8 @@ enum image_type_enum
|
|||||||
IMAGE_TYPE_NONE = 0,
|
IMAGE_TYPE_NONE = 0,
|
||||||
IMAGE_TYPE_PNG,
|
IMAGE_TYPE_PNG,
|
||||||
IMAGE_TYPE_JPEG,
|
IMAGE_TYPE_JPEG,
|
||||||
IMAGE_TYPE_BMP
|
IMAGE_TYPE_BMP,
|
||||||
|
IMAGE_TYPE_TGA
|
||||||
};
|
};
|
||||||
|
|
||||||
bool video_texture_image_set_color_shifts(unsigned *r_shift, unsigned *g_shift,
|
bool video_texture_image_set_color_shifts(unsigned *r_shift, unsigned *g_shift,
|
||||||
|
@ -36,12 +36,17 @@
|
|||||||
|
|
||||||
RETRO_BEGIN_DECLS
|
RETRO_BEGIN_DECLS
|
||||||
|
|
||||||
bool rtga_image_load_shift(uint8_t *buf,
|
typedef struct rtga rtga_t;
|
||||||
void *data,
|
|
||||||
unsigned a_shift, unsigned r_shift,
|
int rtga_process_image(rtga_t *rtga, void **buf,
|
||||||
unsigned g_shift, unsigned b_shift);
|
size_t size, unsigned *width, unsigned *height);
|
||||||
|
|
||||||
|
bool rtga_set_buf_ptr(rtga_t *rtga, void *data);
|
||||||
|
|
||||||
|
void rtga_free(rtga_t *rtga);
|
||||||
|
|
||||||
|
rtga_t *rtga_alloc(void);
|
||||||
|
|
||||||
RETRO_END_DECLS
|
RETRO_END_DECLS
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
@ -4011,7 +4011,7 @@ bool menu_displaylist_ctl(enum menu_displaylist_ctl_state type, void *data)
|
|||||||
break;
|
break;
|
||||||
case DISPLAYLIST_IMAGES:
|
case DISPLAYLIST_IMAGES:
|
||||||
info->type_default = MENU_FILE_IMAGE;
|
info->type_default = MENU_FILE_IMAGE;
|
||||||
strlcpy(info->exts, "png|jpg|jpeg|bmp", sizeof(info->exts));
|
strlcpy(info->exts, "png|jpg|jpeg|bmp|tga", sizeof(info->exts));
|
||||||
break;
|
break;
|
||||||
case DISPLAYLIST_AUDIO_FILTERS:
|
case DISPLAYLIST_AUDIO_FILTERS:
|
||||||
info->type_default = MENU_FILE_AUDIOFILTER;
|
info->type_default = MENU_FILE_AUDIOFILTER;
|
||||||
|
@ -117,6 +117,7 @@ static int task_image_process(
|
|||||||
unsigned *height)
|
unsigned *height)
|
||||||
{
|
{
|
||||||
nbio_image_handle_t *image = (nbio_image_handle_t*)nbio->image;
|
nbio_image_handle_t *image = (nbio_image_handle_t*)nbio->image;
|
||||||
|
|
||||||
int retval = image_transfer_process(
|
int retval = image_transfer_process(
|
||||||
image->handle,
|
image->handle,
|
||||||
nbio->image_type,
|
nbio->image_type,
|
||||||
@ -366,6 +367,8 @@ bool rarch_task_push_image_load(const char *fullpath,
|
|||||||
nbio->image_type = IMAGE_TYPE_JPEG;
|
nbio->image_type = IMAGE_TYPE_JPEG;
|
||||||
else if (strstr(fullpath, ".bmp"))
|
else if (strstr(fullpath, ".bmp"))
|
||||||
nbio->image_type = IMAGE_TYPE_BMP;
|
nbio->image_type = IMAGE_TYPE_BMP;
|
||||||
|
else if (strstr(fullpath, ".tga"))
|
||||||
|
nbio->image_type = IMAGE_TYPE_TGA;
|
||||||
|
|
||||||
switch (cb_type_hash)
|
switch (cb_type_hash)
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user