libgui/source/gui/GuiImageData.cpp

192 lines
6.5 KiB
C++
Raw Normal View History

2017-10-29 10:28:14 +01:00
/****************************************************************************
* Copyright (C) 2015 Dimok
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#include <malloc.h>
#include <string.h>
2018-06-21 20:44:58 +02:00
#include <stdint.h>
#include <gui/GuiImageData.h>
#include <gui/memory.h>
2020-08-13 12:38:07 +02:00
2017-10-29 10:28:14 +01:00
/**
* Constructor for the GuiImageData class.
*/
2018-06-21 20:44:58 +02:00
GuiImageData::GuiImageData() {
2017-10-29 10:28:14 +01:00
texture = NULL;
sampler = NULL;
2018-06-21 20:44:58 +02:00
memoryType = eMemTypeMEM2;
2017-10-29 10:28:14 +01:00
}
/**
* Constructor for the GuiImageData class.
*/
2020-08-13 12:38:07 +02:00
GuiImageData::GuiImageData(const uint8_t *img, int32_t imgSize, GX2TexClampMode textureClamp, GX2SurfaceFormat textureFormat) {
2017-10-29 10:28:14 +01:00
texture = NULL;
sampler = NULL;
2018-06-21 20:44:58 +02:00
loadImage(img, imgSize, textureClamp, textureFormat);
2017-10-29 10:28:14 +01:00
}
/**
* Destructor for the GuiImageData class.
*/
2018-06-21 20:44:58 +02:00
GuiImageData::~GuiImageData() {
2017-10-29 10:28:14 +01:00
releaseData();
}
2018-06-21 20:44:58 +02:00
void GuiImageData::releaseData(void) {
2020-08-13 12:38:07 +02:00
if (texture) {
if (texture->surface.image) {
switch (memoryType) {
default:
case eMemTypeMEM2:
free(texture->surface.image);
break;
case eMemTypeMEM1:
MEM1_free(texture->surface.image);
break;
case eMemTypeMEMBucket:
MEMBucket_free(texture->surface.image);
break;
2017-10-29 10:28:14 +01:00
}
}
delete texture;
texture = NULL;
}
2020-08-13 12:38:07 +02:00
if (sampler) {
2017-10-29 10:28:14 +01:00
delete sampler;
sampler = NULL;
}
}
2018-06-21 20:44:58 +02:00
void GuiImageData::loadImage(const uint8_t *img, int32_t imgSize, GX2TexClampMode textureClamp, GX2SurfaceFormat textureFormat) {
2020-08-13 12:58:19 +02:00
if (!img || (imgSize < 8)) {
2018-06-21 20:44:58 +02:00
return;
2020-08-13 12:58:19 +02:00
}
2018-06-21 20:44:58 +02:00
releaseData();
gdImagePtr gdImg = 0;
if (img[0] == 0xFF && img[1] == 0xD8) {
//! not needed for now therefore comment out to safe ELF size
//! if needed uncomment, adds 200 kb to the ELF size
// IMAGE_JPEG
2020-08-13 12:38:07 +02:00
gdImg = gdImageCreateFromJpegPtr(imgSize, (uint8_t *) img);
2018-06-21 20:44:58 +02:00
} else if (img[0] == 'B' && img[1] == 'M') {
// IMAGE_BMP
2020-08-13 12:38:07 +02:00
gdImg = gdImageCreateFromBmpPtr(imgSize, (uint8_t *) img);
2018-06-21 20:44:58 +02:00
} else if (img[0] == 0x89 && img[1] == 'P' && img[2] == 'N' && img[3] == 'G') {
// IMAGE_PNG
2020-08-13 12:38:07 +02:00
gdImg = gdImageCreateFromPngPtr(imgSize, (uint8_t *) img);
2018-06-21 20:44:58 +02:00
}
2020-08-13 12:38:07 +02:00
//!This must be last since it can also intefere with outher formats
else if (img[0] == 0x00) {
2018-06-21 20:44:58 +02:00
// Try loading TGA image
2020-08-13 12:38:07 +02:00
gdImg = gdImageCreateFromTgaPtr(imgSize, (uint8_t *) img);
2018-06-21 20:44:58 +02:00
}
2020-08-13 12:58:19 +02:00
if (gdImg == 0) {
2018-06-21 20:44:58 +02:00
return;
2020-08-13 12:58:19 +02:00
}
2018-06-21 20:44:58 +02:00
uint32_t width = (gdImageSX(gdImg));
uint32_t height = (gdImageSY(gdImg));
2017-10-29 10:28:14 +01:00
//! Initialize texture
texture = new GX2Texture;
2020-08-13 12:38:07 +02:00
GX2InitTexture(texture, width, height, 1, 0, textureFormat, GX2_SURFACE_DIM_TEXTURE_2D, GX2_TILE_MODE_LINEAR_ALIGNED);
2017-10-29 10:28:14 +01:00
//! if this fails something went horribly wrong
2020-08-13 12:38:07 +02:00
if (texture->surface.imageSize == 0) {
2017-10-29 10:28:14 +01:00
delete texture;
texture = NULL;
gdImageDestroy(gdImg);
return;
}
//! allocate memory for the surface
2018-06-21 20:44:58 +02:00
memoryType = eMemTypeMEM2;
texture->surface.image = memalign(texture->surface.alignment, texture->surface.imageSize);
2017-10-29 10:28:14 +01:00
//! try MEM1 on failure
2020-08-13 12:38:07 +02:00
if (!texture->surface.image) {
2017-10-29 10:28:14 +01:00
memoryType = eMemTypeMEM1;
2018-06-21 20:44:58 +02:00
texture->surface.image = MEM1_alloc(texture->surface.imageSize, texture->surface.alignment);
2017-10-29 10:28:14 +01:00
}
//! try MEM bucket on failure
2020-08-13 12:38:07 +02:00
if (!texture->surface.image) {
2017-10-29 10:28:14 +01:00
memoryType = eMemTypeMEMBucket;
2018-06-21 20:44:58 +02:00
texture->surface.image = MEMBucket_alloc(texture->surface.imageSize, texture->surface.alignment);
2017-10-29 10:28:14 +01:00
}
//! check if memory is available for image
2020-08-13 12:38:07 +02:00
if (!texture->surface.image) {
2017-10-29 10:28:14 +01:00
gdImageDestroy(gdImg);
delete texture;
texture = NULL;
return;
}
//! set mip map data pointer
2018-06-21 20:44:58 +02:00
texture->surface.mipmaps = NULL;
2017-10-29 10:28:14 +01:00
//! convert image to texture
2020-08-13 12:38:07 +02:00
switch (textureFormat) {
default:
case GX2_SURFACE_FORMAT_UNORM_R8_G8_B8_A8:
gdImageToUnormR8G8B8A8(gdImg, (uint32_t *) texture->surface.image, texture->surface.width, texture->surface.height, texture->surface.pitch);
break;
case GX2_SURFACE_FORMAT_UNORM_R5_G6_B5:
gdImageToUnormR5G6B5(gdImg, (uint16_t *) texture->surface.image, texture->surface.width, texture->surface.height, texture->surface.pitch);
break;
2017-10-29 10:28:14 +01:00
}
2018-06-21 20:44:58 +02:00
//! free memory of image as its not needed anymore
gdImageDestroy(gdImg);
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
//! invalidate the memory
GX2Invalidate(GX2_INVALIDATE_MODE_CPU_TEXTURE, texture->surface.image, texture->surface.imageSize);
2017-10-29 10:28:14 +01:00
//! initialize the sampler
sampler = new GX2Sampler;
2018-06-21 20:44:58 +02:00
GX2InitSampler(sampler, textureClamp, GX2_TEX_XY_FILTER_MODE_LINEAR);
2017-10-29 10:28:14 +01:00
}
2018-06-21 20:44:58 +02:00
void GuiImageData::gdImageToUnormR8G8B8A8(gdImagePtr gdImg, uint32_t *imgBuffer, uint32_t width, uint32_t height, uint32_t pitch) {
2020-08-13 12:38:07 +02:00
for (uint32_t y = 0; y < height; ++y) {
for (uint32_t x = 0; x < width; ++x) {
2018-06-21 20:44:58 +02:00
uint32_t pixel = gdImageGetPixel(gdImg, x, y);
2017-10-29 10:28:14 +01:00
2020-08-13 12:38:07 +02:00
uint8_t a = 254 - 2 * ((uint8_t) gdImageAlpha(gdImg, pixel));
2020-08-13 12:58:19 +02:00
if (a == 254) { a++; }
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
uint8_t r = gdImageRed(gdImg, pixel);
uint8_t g = gdImageGreen(gdImg, pixel);
uint8_t b = gdImageBlue(gdImg, pixel);
2017-10-29 10:28:14 +01:00
imgBuffer[y * pitch + x] = (r << 24) | (g << 16) | (b << 8) | (a);
}
}
}
//! TODO: figure out why this seems to not work correct yet
2018-06-21 20:44:58 +02:00
void GuiImageData::gdImageToUnormR5G6B5(gdImagePtr gdImg, uint16_t *imgBuffer, uint32_t width, uint32_t height, uint32_t pitch) {
2020-08-13 12:38:07 +02:00
for (uint32_t y = 0; y < height; ++y) {
for (uint32_t x = 0; x < width; ++x) {
2018-06-21 20:44:58 +02:00
uint32_t pixel = gdImageGetPixel(gdImg, x, y);
uint8_t r = gdImageRed(gdImg, pixel);
uint8_t g = gdImageGreen(gdImg, pixel);
uint8_t b = gdImageBlue(gdImg, pixel);
2017-10-29 10:28:14 +01:00
imgBuffer[y * pitch + x] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
}
}
}