2012-04-21 23:13:50 +02:00
|
|
|
/* RetroArch - A frontend for libretro.
|
2014-01-01 01:50:59 +01:00
|
|
|
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
|
2015-01-07 18:06:50 +01:00
|
|
|
* Copyright (C) 2011-2015 - Daniel De Matteis
|
2011-05-18 20:22:27 +02:00
|
|
|
*
|
2012-04-21 23:13:50 +02:00
|
|
|
* RetroArch is free software: you can redistribute it and/or modify it under the terms
|
2011-05-18 20:22:27 +02:00
|
|
|
* of the GNU General Public License as published by the Free Software Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
2012-04-21 23:13:50 +02:00
|
|
|
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
2011-05-18 20:22:27 +02:00
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
|
|
*
|
2012-04-21 23:31:57 +02:00
|
|
|
* You should have received a copy of the GNU General Public License along with RetroArch.
|
2011-05-18 20:22:27 +02:00
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2011-05-18 21:49:23 +02:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2014-02-25 21:24:15 +01:00
|
|
|
#include "../../config.h"
|
2011-05-18 21:49:23 +02:00
|
|
|
#endif
|
2011-05-18 20:22:27 +02:00
|
|
|
|
2014-05-28 21:14:11 +02:00
|
|
|
#include "image.h"
|
2014-10-21 23:53:09 +02:00
|
|
|
#include "../../file_ops.h"
|
2011-05-18 21:49:23 +02:00
|
|
|
|
2011-05-18 20:22:27 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stddef.h>
|
2014-02-25 21:24:15 +01:00
|
|
|
#include "../../general.h"
|
2015-02-11 02:30:56 +01:00
|
|
|
#include <formats/rpng.h>
|
2011-05-18 21:49:23 +02:00
|
|
|
|
2015-02-11 07:12:21 +01:00
|
|
|
static bool rtga_image_load_shift(uint8_t *buf,
|
2014-09-09 06:59:17 +02:00
|
|
|
struct texture_image *out_img,
|
|
|
|
unsigned a_shift, unsigned r_shift,
|
|
|
|
unsigned g_shift, unsigned b_shift)
|
2011-05-18 21:49:23 +02:00
|
|
|
{
|
2015-01-26 18:41:33 +01:00
|
|
|
unsigned i, bits, size, bits_mul;
|
2015-02-11 07:12:21 +01:00
|
|
|
uint8_t info[6];
|
2015-01-24 03:37:15 +01:00
|
|
|
unsigned width = 0;
|
|
|
|
unsigned height = 0;
|
|
|
|
const uint8_t *tmp = NULL;
|
2011-05-19 22:51:23 +02:00
|
|
|
|
2014-09-09 06:59:17 +02:00
|
|
|
if (buf[2] != 2)
|
2011-05-18 21:49:23 +02:00
|
|
|
{
|
2012-12-20 21:44:20 +01:00
|
|
|
RARCH_ERR("TGA image is not uncompressed RGB.\n");
|
2011-05-18 21:49:23 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(info, buf + 12, 6);
|
|
|
|
|
2015-01-24 03:37:15 +01:00
|
|
|
width = info[0] + ((unsigned)info[1] * 256);
|
2011-05-18 21:49:23 +02:00
|
|
|
height = info[2] + ((unsigned)info[3] * 256);
|
2015-01-24 03:37:15 +01:00
|
|
|
bits = info[4];
|
2011-05-18 21:49:23 +02:00
|
|
|
|
2012-04-21 23:25:32 +02:00
|
|
|
RARCH_LOG("Loaded TGA: (%ux%u @ %u bpp)\n", width, height, bits);
|
2011-05-18 21:49:23 +02:00
|
|
|
|
2015-01-24 03:37:15 +01:00
|
|
|
size = width * height * sizeof(uint32_t);
|
2011-12-24 13:46:12 +01:00
|
|
|
out_img->pixels = (uint32_t*)malloc(size);
|
2015-01-24 03:37:15 +01:00
|
|
|
out_img->width = width;
|
2011-05-18 21:49:23 +02:00
|
|
|
out_img->height = height;
|
2015-01-24 03:37:15 +01:00
|
|
|
|
2011-05-18 21:49:23 +02:00
|
|
|
if (!out_img->pixels)
|
|
|
|
{
|
2012-12-20 21:44:20 +01:00
|
|
|
RARCH_ERR("Failed to allocate TGA pixels.\n");
|
2011-05-18 21:49:23 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-26 18:41:33 +01:00
|
|
|
tmp = buf + 18;
|
|
|
|
bits_mul = 3;
|
2015-01-24 03:37:15 +01:00
|
|
|
|
2015-01-27 15:35:31 +01:00
|
|
|
if (bits != 32 && bits != 24)
|
2011-05-18 21:49:23 +02:00
|
|
|
{
|
2012-12-20 21:44:20 +01:00
|
|
|
RARCH_ERR("Bit depth of TGA image is wrong. Only 32-bit and 24-bit supported.\n");
|
2011-05-18 21:49:23 +02:00
|
|
|
free(out_img->pixels);
|
|
|
|
out_img->pixels = NULL;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-26 18:41:33 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-05-18 21:49:23 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-26 19:26:06 +01:00
|
|
|
#ifdef HAVE_ZLIB
|
2014-09-09 06:59:17 +02:00
|
|
|
static bool rpng_image_load_argb_shift(const char *path,
|
|
|
|
struct texture_image *out_img,
|
|
|
|
unsigned a_shift, unsigned r_shift,
|
|
|
|
unsigned g_shift, unsigned b_shift)
|
2013-01-12 14:05:05 +01:00
|
|
|
{
|
2015-01-26 19:26:06 +01:00
|
|
|
bool ret = rpng_load_image_argb(path,
|
|
|
|
&out_img->pixels, &out_img->width, &out_img->height);
|
2015-01-26 18:46:39 +01:00
|
|
|
|
2015-01-26 19:26:06 +01:00
|
|
|
if (!ret)
|
|
|
|
return false;
|
2015-01-24 03:37:15 +01:00
|
|
|
|
2015-01-26 19:26:06 +01:00
|
|
|
/* This is quite uncommon. */
|
|
|
|
if (a_shift != 24 || r_shift != 16 || g_shift != 8 || b_shift != 0)
|
|
|
|
{
|
|
|
|
uint32_t i;
|
|
|
|
uint32_t num_pixels = out_img->width * out_img->height;
|
|
|
|
uint32_t *pixels = (uint32_t*)out_img->pixels;
|
2013-01-12 14:05:05 +01:00
|
|
|
|
2015-01-26 19:26:06 +01:00
|
|
|
for (i = 0; i < num_pixels; i++)
|
2013-01-12 14:05:05 +01:00
|
|
|
{
|
2015-01-26 19:26:06 +01:00
|
|
|
uint32_t col = pixels[i];
|
|
|
|
uint8_t a = (uint8_t)(col >> 24);
|
|
|
|
uint8_t r = (uint8_t)(col >> 16);
|
|
|
|
uint8_t g = (uint8_t)(col >> 8);
|
|
|
|
uint8_t b = (uint8_t)(col >> 0);
|
|
|
|
pixels[i] = (a << a_shift) |
|
|
|
|
(r << r_shift) | (g << g_shift) | (b << b_shift);
|
2013-01-12 14:05:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-26 19:26:06 +01:00
|
|
|
return true;
|
2013-01-12 14:05:05 +01:00
|
|
|
}
|
2015-01-26 19:26:06 +01:00
|
|
|
#endif
|
2013-01-12 14:05:05 +01:00
|
|
|
|
2014-02-18 18:20:23 -05:00
|
|
|
#ifdef GEKKO
|
|
|
|
|
|
|
|
#define GX_BLIT_LINE_32(off) \
|
|
|
|
{ \
|
|
|
|
const uint16_t *tmp_src = src; \
|
|
|
|
uint16_t *tmp_dst = dst; \
|
2014-02-20 23:50:13 -05:00
|
|
|
for (unsigned x = 0; x < width2 >> 3; x++, tmp_src += 8, tmp_dst += 32) \
|
2014-02-18 18:20:23 -05:00
|
|
|
{ \
|
|
|
|
tmp_dst[ 0 + off] = tmp_src[0]; \
|
|
|
|
tmp_dst[ 16 + off] = tmp_src[1]; \
|
|
|
|
tmp_dst[ 1 + off] = tmp_src[2]; \
|
|
|
|
tmp_dst[ 17 + off] = tmp_src[3]; \
|
|
|
|
tmp_dst[ 2 + off] = tmp_src[4]; \
|
|
|
|
tmp_dst[ 18 + off] = tmp_src[5]; \
|
|
|
|
tmp_dst[ 3 + off] = tmp_src[6]; \
|
|
|
|
tmp_dst[ 19 + off] = tmp_src[7]; \
|
|
|
|
} \
|
|
|
|
src += tmp_pitch; \
|
|
|
|
}
|
|
|
|
|
2014-05-10 20:26:58 +02:00
|
|
|
static bool rpng_gx_convert_texture32(struct texture_image *image)
|
2014-02-18 18:20:23 -05:00
|
|
|
{
|
2015-01-24 03:37:15 +01:00
|
|
|
unsigned tmp_pitch, width2, i;
|
|
|
|
const uint16_t *src;
|
|
|
|
uint16_t *dst;
|
2014-09-09 06:59:17 +02:00
|
|
|
/* Memory allocation in libogc is extremely primitive so try
|
|
|
|
* to avoid gaps in memory when converting by copying over to
|
|
|
|
* a temporary buffer first, then converting over into
|
|
|
|
* main buffer again. */
|
2014-02-19 17:52:47 -05:00
|
|
|
void *tmp = malloc(image->width * image->height * sizeof(uint32_t));
|
2014-02-18 18:20:23 -05:00
|
|
|
|
|
|
|
if (!tmp)
|
|
|
|
{
|
|
|
|
RARCH_ERR("Failed to create temp buffer for conversion.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(tmp, image->pixels, image->width * image->height * sizeof(uint32_t));
|
2015-01-24 03:37:15 +01:00
|
|
|
tmp_pitch = (image->width * sizeof(uint32_t)) >> 1;
|
|
|
|
|
2014-02-18 18:20:23 -05:00
|
|
|
image->width &= ~3;
|
|
|
|
image->height &= ~3;
|
|
|
|
|
2015-01-24 03:37:15 +01:00
|
|
|
width2 = image->width << 1;
|
|
|
|
|
|
|
|
src = (uint16_t *) tmp;
|
|
|
|
dst = (uint16_t *) image->pixels;
|
|
|
|
|
|
|
|
for (i = 0; i < image->height; i += 4, dst += 4 * width2)
|
2014-02-18 18:20:23 -05:00
|
|
|
{
|
|
|
|
GX_BLIT_LINE_32(0)
|
|
|
|
GX_BLIT_LINE_32(4)
|
|
|
|
GX_BLIT_LINE_32(8)
|
|
|
|
GX_BLIT_LINE_32(12)
|
|
|
|
}
|
|
|
|
|
|
|
|
free(tmp);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2014-06-17 17:44:48 +02:00
|
|
|
void texture_image_free(struct texture_image *img)
|
2014-05-10 20:26:58 +02:00
|
|
|
{
|
2014-09-15 08:17:16 -04:00
|
|
|
if (!img)
|
|
|
|
return;
|
|
|
|
|
2014-05-10 20:26:58 +02:00
|
|
|
free(img->pixels);
|
|
|
|
memset(img, 0, sizeof(*img));
|
|
|
|
}
|
|
|
|
|
2014-06-17 17:44:48 +02:00
|
|
|
bool texture_image_load(struct texture_image *out_img, const char *path)
|
2013-01-03 01:18:19 +01:00
|
|
|
{
|
2014-09-09 06:59:17 +02:00
|
|
|
/* This interface "leak" is very ugly. FIXME: Fix this properly ... */
|
2015-01-26 19:26:06 +01:00
|
|
|
bool ret = false;
|
|
|
|
bool use_rgba = driver.gfx_use_rgba;
|
|
|
|
unsigned a_shift = 24;
|
|
|
|
unsigned r_shift = use_rgba ? 0 : 16;
|
|
|
|
unsigned g_shift = 8;
|
|
|
|
unsigned b_shift = use_rgba ? 16 : 0;
|
|
|
|
|
|
|
|
if (strstr(path, ".tga"))
|
2015-02-11 07:12:21 +01:00
|
|
|
{
|
|
|
|
void *raw_buf = NULL;
|
|
|
|
uint8_t *buf = NULL;
|
|
|
|
ssize_t len = read_file(path, &raw_buf);
|
|
|
|
|
|
|
|
if (len < 0)
|
|
|
|
{
|
|
|
|
RARCH_ERR("Failed to read image: %s.\n", path);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = (uint8_t*)raw_buf;
|
|
|
|
|
|
|
|
ret = rtga_image_load_shift(buf, out_img,
|
2015-01-26 19:26:06 +01:00
|
|
|
a_shift, r_shift, g_shift, b_shift);
|
2015-02-11 07:12:21 +01:00
|
|
|
|
|
|
|
if (buf)
|
|
|
|
free(buf);
|
|
|
|
}
|
2015-01-26 19:26:06 +01:00
|
|
|
#ifdef HAVE_ZLIB
|
|
|
|
else if (strstr(path, ".png"))
|
2015-02-11 07:12:21 +01:00
|
|
|
{
|
2015-01-26 19:26:06 +01:00
|
|
|
ret = rpng_image_load_argb_shift(path, out_img,
|
|
|
|
a_shift, r_shift, g_shift, b_shift);
|
2015-02-11 07:12:21 +01:00
|
|
|
}
|
2015-01-26 19:26:06 +01:00
|
|
|
#endif
|
2014-02-18 18:20:23 -05:00
|
|
|
|
|
|
|
#ifdef GEKKO
|
|
|
|
if (ret)
|
|
|
|
{
|
2014-05-10 20:26:58 +02:00
|
|
|
if (!rpng_gx_convert_texture32(out_img))
|
2014-02-18 18:20:23 -05:00
|
|
|
{
|
2014-06-17 17:44:48 +02:00
|
|
|
texture_image_free(out_img);
|
2015-01-26 18:46:39 +01:00
|
|
|
return false;
|
2014-02-18 18:20:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return ret;
|
2013-01-03 01:18:19 +01:00
|
|
|
}
|