2012-04-21 21:13:50 +00:00
|
|
|
/* RetroArch - A frontend for libretro.
|
2013-01-01 00:37:37 +00:00
|
|
|
* Copyright (C) 2010-2013 - Hans-Kristian Arntzen
|
2011-05-18 18:22:27 +00:00
|
|
|
*
|
2012-04-21 21:13:50 +00:00
|
|
|
* RetroArch is free software: you can redistribute it and/or modify it under the terms
|
2011-05-18 18:22:27 +00: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 21:13:50 +00:00
|
|
|
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
2011-05-18 18:22:27 +00: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 21:31:57 +00:00
|
|
|
* You should have received a copy of the GNU General Public License along with RetroArch.
|
2011-05-18 18:22:27 +00:00
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2011-05-18 19:49:23 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2011-12-02 23:49:09 +00:00
|
|
|
#include "../config.h"
|
2011-05-18 19:49:23 +00:00
|
|
|
#endif
|
2011-05-18 18:22:27 +00:00
|
|
|
|
|
|
|
#include "image.h"
|
2011-12-02 23:49:09 +00:00
|
|
|
#include "../file.h"
|
2011-05-18 19:49:23 +00:00
|
|
|
|
2011-05-18 18:22:27 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stddef.h>
|
2011-12-02 23:49:09 +00:00
|
|
|
#include "../general.h"
|
2011-05-18 19:49:23 +00:00
|
|
|
|
2011-06-11 14:55:53 +00:00
|
|
|
#ifdef HAVE_SDL_IMAGE
|
2011-05-18 18:22:27 +00:00
|
|
|
|
2011-06-11 14:55:53 +00:00
|
|
|
#include "SDL_image.h"
|
2011-05-18 18:22:27 +00:00
|
|
|
bool texture_image_load(const char *path, struct texture_image *out_img)
|
|
|
|
{
|
2011-06-11 14:55:53 +00:00
|
|
|
SDL_Surface *img = IMG_Load(path);
|
2011-05-18 18:22:27 +00:00
|
|
|
if (!img)
|
|
|
|
return false;
|
|
|
|
|
2011-06-11 14:55:53 +00:00
|
|
|
out_img->width = img->w;
|
|
|
|
out_img->height = img->h;
|
2011-05-18 18:22:27 +00:00
|
|
|
|
|
|
|
size_t size = out_img->width * out_img->height * sizeof(uint32_t);
|
2011-12-24 12:46:12 +00:00
|
|
|
out_img->pixels = (uint32_t*)malloc(size);
|
2011-05-18 18:22:27 +00:00
|
|
|
if (!out_img->pixels)
|
|
|
|
{
|
2011-06-11 14:55:53 +00:00
|
|
|
SDL_FreeSurface(img);
|
2011-05-18 18:22:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-06-11 16:05:52 +00:00
|
|
|
const SDL_PixelFormat *fmt = img->format;
|
|
|
|
|
2012-04-21 21:25:32 +00:00
|
|
|
RARCH_LOG("SDL_image: %dx%d @ %d bpp\n", img->w, img->h, img->format->BitsPerPixel);
|
2011-06-11 16:05:52 +00:00
|
|
|
if (img->format->BitsPerPixel == 32)
|
|
|
|
{
|
2011-12-24 12:46:12 +00:00
|
|
|
for (int y = 0; y < img->h; y++)
|
2011-06-11 16:05:52 +00:00
|
|
|
{
|
|
|
|
uint32_t *dst = out_img->pixels + y * img->w;
|
|
|
|
const uint32_t *src = (const uint32_t*)img->pixels + y * img->pitch / sizeof(uint32_t);
|
|
|
|
|
2011-12-24 12:46:12 +00:00
|
|
|
for (int x = 0; x < img->w; x++)
|
2011-06-11 16:05:52 +00:00
|
|
|
{
|
|
|
|
uint32_t r = (src[x] & fmt->Rmask) >> fmt->Rshift;
|
|
|
|
uint32_t g = (src[x] & fmt->Gmask) >> fmt->Gshift;
|
|
|
|
uint32_t b = (src[x] & fmt->Bmask) >> fmt->Bshift;
|
|
|
|
uint32_t a = (src[x] & fmt->Amask) >> fmt->Ashift;
|
2012-09-15 21:59:52 +00:00
|
|
|
dst[x] = (a << 24) | (r << 16) | (g << 8) | b;
|
2011-06-11 16:05:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (img->format->BitsPerPixel == 24)
|
|
|
|
{
|
2011-12-24 12:46:12 +00:00
|
|
|
for (int y = 0; y < img->h; y++)
|
2011-06-11 16:05:52 +00:00
|
|
|
{
|
|
|
|
uint32_t *dst = out_img->pixels + y * img->w;
|
|
|
|
const uint8_t *src = (const uint8_t*)img->pixels + y * img->pitch;
|
|
|
|
|
2011-12-24 12:46:12 +00:00
|
|
|
for (int x = 0; x < img->w; x++)
|
2011-06-11 16:05:52 +00:00
|
|
|
{
|
|
|
|
// Correct?
|
|
|
|
uint32_t color = 0;
|
|
|
|
color |= src[3 * x + 0] << 0;
|
|
|
|
color |= src[3 * x + 1] << 8;
|
|
|
|
color |= src[3 * x + 2] << 16;
|
|
|
|
uint32_t r = (color & fmt->Rmask) >> fmt->Rshift;
|
|
|
|
uint32_t g = (color & fmt->Gmask) >> fmt->Gshift;
|
|
|
|
uint32_t b = (color & fmt->Bmask) >> fmt->Bshift;
|
2012-09-15 21:59:52 +00:00
|
|
|
dst[x] = (0xff << 24) | (r << 16) | (g << 8) | b;
|
2011-06-11 16:05:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-04-21 21:25:32 +00:00
|
|
|
RARCH_ERR("8-bit and 16-bit image support are not implemented.\n");
|
2011-06-11 16:05:52 +00:00
|
|
|
SDL_FreeSurface(img);
|
|
|
|
return false;
|
|
|
|
}
|
2011-05-18 18:22:27 +00:00
|
|
|
|
2011-06-11 14:55:53 +00:00
|
|
|
SDL_FreeSurface(img);
|
|
|
|
|
2011-05-18 18:22:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
2011-05-18 19:49:23 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
bool texture_image_load(const char *path, struct texture_image *out_img)
|
|
|
|
{
|
|
|
|
// TODO: Check more gracefully.
|
|
|
|
if (!strstr(path, ".tga"))
|
2012-12-20 20:44:20 +00:00
|
|
|
{
|
|
|
|
RARCH_ERR("Extension is not .tga.\n");
|
2011-05-18 19:49:23 +00:00
|
|
|
return false;
|
2012-12-20 20:44:20 +00:00
|
|
|
}
|
2011-05-18 19:49:23 +00:00
|
|
|
|
2011-05-19 20:51:23 +00:00
|
|
|
void *raw_buf = NULL;
|
|
|
|
ssize_t len = read_file(path, &raw_buf);
|
2011-05-18 19:49:23 +00:00
|
|
|
if (len < 0)
|
2012-12-20 20:44:20 +00:00
|
|
|
{
|
|
|
|
RARCH_ERR("Failed to read image: %s.\n", path);
|
2011-05-18 19:49:23 +00:00
|
|
|
return false;
|
2012-12-20 20:44:20 +00:00
|
|
|
}
|
2011-05-18 19:49:23 +00:00
|
|
|
|
2011-12-24 12:46:12 +00:00
|
|
|
uint8_t *buf = (uint8_t*)raw_buf;
|
2011-05-19 20:51:23 +00:00
|
|
|
|
2011-05-18 19:49:23 +00:00
|
|
|
if (buf[2] != 2) // Uncompressed RGB
|
|
|
|
{
|
2012-12-20 20:44:20 +00:00
|
|
|
RARCH_ERR("TGA image is not uncompressed RGB.\n");
|
2011-05-18 19:49:23 +00:00
|
|
|
free(buf);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned width = 0;
|
|
|
|
unsigned height = 0;
|
|
|
|
|
|
|
|
uint8_t info[6];
|
|
|
|
memcpy(info, buf + 12, 6);
|
|
|
|
|
|
|
|
width = info[0] + ((unsigned)info[1] * 256);
|
|
|
|
height = info[2] + ((unsigned)info[3] * 256);
|
|
|
|
unsigned bits = info[4];
|
|
|
|
|
2012-04-21 21:25:32 +00:00
|
|
|
RARCH_LOG("Loaded TGA: (%ux%u @ %u bpp)\n", width, height, bits);
|
2011-05-18 19:49:23 +00:00
|
|
|
|
|
|
|
unsigned size = width * height * sizeof(uint32_t);
|
2011-12-24 12:46:12 +00:00
|
|
|
out_img->pixels = (uint32_t*)malloc(size);
|
2011-05-18 19:49:23 +00:00
|
|
|
out_img->width = width;
|
|
|
|
out_img->height = height;
|
|
|
|
if (!out_img->pixels)
|
|
|
|
{
|
2012-12-20 20:44:20 +00:00
|
|
|
RARCH_ERR("Failed to allocate TGA pixels.\n");
|
2011-05-18 19:49:23 +00:00
|
|
|
free(buf);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t *tmp = buf + 18;
|
|
|
|
if (bits == 32)
|
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < width * height; i++)
|
|
|
|
{
|
|
|
|
uint32_t b = tmp[i * 4 + 0];
|
2011-08-29 15:23:49 +00:00
|
|
|
uint32_t g = tmp[i * 4 + 1];
|
|
|
|
uint32_t r = tmp[i * 4 + 2];
|
2011-05-18 19:49:23 +00:00
|
|
|
uint32_t a = tmp[i * 4 + 3];
|
|
|
|
|
2012-09-15 21:59:52 +00:00
|
|
|
out_img->pixels[i] = (a << 24) | (r << 16) | (g << 8) | b;
|
2011-05-18 19:49:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (bits == 24)
|
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < width * height; i++)
|
|
|
|
{
|
|
|
|
uint32_t b = tmp[i * 3 + 0];
|
2011-08-29 15:23:49 +00:00
|
|
|
uint32_t g = tmp[i * 3 + 1];
|
|
|
|
uint32_t r = tmp[i * 3 + 2];
|
2011-05-18 19:49:23 +00:00
|
|
|
uint32_t a = 0xff;
|
|
|
|
|
2012-09-15 21:59:52 +00:00
|
|
|
out_img->pixels[i] = (a << 24) | (r << 16) | (g << 8) | b;
|
2011-05-18 19:49:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-12-20 20:44:20 +00:00
|
|
|
RARCH_ERR("Bit depth of TGA image is wrong. Only 32-bit and 24-bit supported.\n");
|
2011-05-18 19:49:23 +00:00
|
|
|
free(buf);
|
|
|
|
free(out_img->pixels);
|
|
|
|
out_img->pixels = NULL;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(buf);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|