2010-09-26 10:33:43 +02:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (C) 2010
|
|
|
|
* by Dimok
|
2009-10-01 01:10:58 +02:00
|
|
|
*
|
2010-09-26 10:33:43 +02:00
|
|
|
* This software is provided 'as-is', without any express or implied
|
|
|
|
* warranty. In no event will the authors be held liable for any
|
|
|
|
* damages arising from the use of this software.
|
2009-10-01 01:10:58 +02:00
|
|
|
*
|
2010-09-26 10:33:43 +02:00
|
|
|
* Permission is granted to anyone to use this software for any
|
|
|
|
* purpose, including commercial applications, and to alter it and
|
|
|
|
* redistribute it freely, subject to the following restrictions:
|
2009-10-01 01:10:58 +02:00
|
|
|
*
|
2010-09-26 10:33:43 +02:00
|
|
|
* 1. The origin of this software must not be misrepresented; you
|
|
|
|
* must not claim that you wrote the original software. If you use
|
|
|
|
* this software in a product, an acknowledgment in the product
|
|
|
|
* documentation would be appreciated but is not required.
|
2009-10-21 21:32:46 +02:00
|
|
|
*
|
2010-09-26 10:33:43 +02:00
|
|
|
* 2. Altered source versions must be plainly marked as such, and
|
|
|
|
* must not be misrepresented as being the original software.
|
|
|
|
*
|
|
|
|
* 3. This notice may not be removed or altered from any source
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* for WiiXplorer 2010
|
2009-10-01 01:10:58 +02:00
|
|
|
***************************************************************************/
|
|
|
|
#include "gui.h"
|
2010-09-26 10:33:43 +02:00
|
|
|
#include "ImageOperations/TextureConverter.h"
|
|
|
|
#include "ImageOperations/TplImage.h"
|
|
|
|
#include "FileOperations/fileops.h"
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2010-09-26 10:33:43 +02:00
|
|
|
#define ALIGN32(x) (((x) + 31) & ~31)
|
2009-10-21 21:32:46 +02:00
|
|
|
|
2009-10-01 01:10:58 +02:00
|
|
|
/**
|
|
|
|
* Constructor for the GuiImageData class.
|
|
|
|
*/
|
2010-09-26 10:33:43 +02:00
|
|
|
GuiImageData::GuiImageData(const char * filepath)
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2010-09-26 10:33:43 +02:00
|
|
|
data = NULL;
|
|
|
|
width = 0;
|
|
|
|
height = 0;
|
|
|
|
format = GX_TF_RGBA8;
|
2010-09-19 01:16:05 +02:00
|
|
|
|
2010-09-26 10:33:43 +02:00
|
|
|
u8 *buffer = NULL;
|
|
|
|
u64 size = 0;
|
2010-09-19 01:16:05 +02:00
|
|
|
|
2010-09-26 10:33:43 +02:00
|
|
|
if(LoadFileToMem(filepath, &buffer, &size) < 0)
|
|
|
|
return;
|
2010-09-19 01:16:05 +02:00
|
|
|
|
2010-09-26 10:33:43 +02:00
|
|
|
LoadImage(buffer, size);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
2010-09-26 10:33:43 +02:00
|
|
|
if(buffer)
|
|
|
|
free(buffer);
|
2009-10-01 01:10:58 +02:00
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
GuiImageData::GuiImageData(const u8 * img, int imgSize)
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2010-09-26 10:33:43 +02:00
|
|
|
data = NULL;
|
|
|
|
width = 0;
|
|
|
|
height = 0;
|
|
|
|
format = GX_TF_RGBA8;
|
2010-09-19 01:16:05 +02:00
|
|
|
|
2010-09-26 10:33:43 +02:00
|
|
|
LoadImage(img, imgSize);
|
2009-10-21 21:32:46 +02:00
|
|
|
}
|
2010-09-26 10:33:43 +02:00
|
|
|
|
2009-10-01 01:10:58 +02:00
|
|
|
/**
|
2010-09-26 10:33:43 +02:00
|
|
|
* Destructor for the GuiImageData class.
|
2009-10-01 01:10:58 +02:00
|
|
|
*/
|
2010-09-26 10:33:43 +02:00
|
|
|
GuiImageData::~GuiImageData()
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2010-09-26 10:33:43 +02:00
|
|
|
if(data)
|
|
|
|
{
|
|
|
|
free(data);
|
|
|
|
data = NULL;
|
|
|
|
}
|
|
|
|
}
|
2010-09-19 01:16:05 +02:00
|
|
|
|
2010-09-26 10:33:43 +02:00
|
|
|
void GuiImageData::LoadImage(const u8 * img, int imgSize)
|
|
|
|
{
|
|
|
|
if(!img)
|
|
|
|
return;
|
2010-09-19 01:16:05 +02:00
|
|
|
|
2010-09-26 10:33:43 +02:00
|
|
|
if(data)
|
|
|
|
{
|
|
|
|
free(data);
|
|
|
|
data = NULL;
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
|
|
|
|
2010-09-26 10:33:43 +02:00
|
|
|
if (imgSize < 8)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-26 10:33:43 +02:00
|
|
|
return;
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
2010-09-26 10:33:43 +02:00
|
|
|
else if (img[0] == 0x89 && img[1] == 'P' && img[2] == 'N' && img[3] == 'G')
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-26 10:33:43 +02:00
|
|
|
// IMAGE_PNG
|
|
|
|
LoadPNG(img, imgSize);
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
2010-09-26 10:33:43 +02:00
|
|
|
else if (img[0] == 0xFF && img[1] == 0xD8)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-26 10:33:43 +02:00
|
|
|
// IMAGE_JPEG
|
|
|
|
LoadJpeg(img, imgSize);
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
2010-09-26 10:33:43 +02:00
|
|
|
else if (img[0] == 'B' && img[1] == 'M')
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-26 10:33:43 +02:00
|
|
|
// IMAGE_BMP
|
|
|
|
LoadBMP(img, imgSize);
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
2010-09-26 10:33:43 +02:00
|
|
|
else if (img[0] == 'G' && img[1] == 'I' && img[2] == 'F')
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-26 10:33:43 +02:00
|
|
|
// IMAGE_GIF
|
|
|
|
LoadGIF(img, imgSize);
|
|
|
|
}
|
|
|
|
else if (img[0] == 0x00 && img[1] == 0x20 && img[2] == 0xAF && img[3] == 0x30)
|
|
|
|
{
|
|
|
|
// IMAGE_TPL
|
|
|
|
LoadTPL(img, imgSize);
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
2009-10-01 01:10:58 +02:00
|
|
|
}
|
|
|
|
|
2010-09-26 10:33:43 +02:00
|
|
|
void GuiImageData::LoadPNG(const u8 *img, int imgSize)
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2010-09-26 10:33:43 +02:00
|
|
|
gdImagePtr gdImg = gdImageCreateFromPngPtr(imgSize, (u8*) img);
|
|
|
|
if(gdImg == 0)
|
|
|
|
return;
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2010-09-26 10:33:43 +02:00
|
|
|
data = GDImageToRGBA8(&gdImg, &width, &height);
|
|
|
|
gdImageDestroy(gdImg);
|
2009-10-01 01:10:58 +02:00
|
|
|
}
|
|
|
|
|
2010-09-26 10:33:43 +02:00
|
|
|
void GuiImageData::LoadJpeg(const u8 *img, int imgSize)
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2010-09-26 10:33:43 +02:00
|
|
|
gdImagePtr gdImg = gdImageCreateFromJpegPtr(imgSize, (u8*) img);
|
|
|
|
if(gdImg == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
data = GDImageToRGBA8(&gdImg, &width, &height);
|
|
|
|
gdImageDestroy(gdImg);
|
2009-10-01 01:10:58 +02:00
|
|
|
}
|
2010-09-26 10:33:43 +02:00
|
|
|
|
|
|
|
void GuiImageData::LoadGIF(const u8 *img, int imgSize)
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2010-09-26 10:33:43 +02:00
|
|
|
gdImagePtr gdImg = gdImageCreateFromGifPtr(imgSize, (u8*) img);
|
|
|
|
if(gdImg == 0)
|
|
|
|
return;
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2010-09-26 10:33:43 +02:00
|
|
|
data = GDImageToRGBA8(&gdImg, &width, &height);
|
|
|
|
gdImageDestroy(gdImg);
|
|
|
|
}
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2010-09-26 10:33:43 +02:00
|
|
|
void GuiImageData::LoadBMP(const u8 *img, int imgSize)
|
|
|
|
{
|
|
|
|
gdImagePtr gdImg = gdImageCreateFromBmpPtr(imgSize, (u8*) img);
|
|
|
|
if(gdImg == 0)
|
|
|
|
return;
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2010-09-26 10:33:43 +02:00
|
|
|
data = GDImageToRGBA8(&gdImg, &width, &height);
|
|
|
|
gdImageDestroy(gdImg);
|
2009-10-01 01:10:58 +02:00
|
|
|
}
|
2009-10-21 21:32:46 +02:00
|
|
|
|
2010-09-26 10:33:43 +02:00
|
|
|
void GuiImageData::LoadTPL(const u8 *img, int imgSize)
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2010-09-26 10:33:43 +02:00
|
|
|
TplImage TplFile(img, imgSize);
|
2009-10-21 21:32:46 +02:00
|
|
|
|
2010-09-26 10:33:43 +02:00
|
|
|
width = TplFile.GetWidth(0);
|
|
|
|
height = TplFile.GetHeight(0);
|
|
|
|
format = (u8) TplFile.GetFormat(0);
|
2009-10-21 21:32:46 +02:00
|
|
|
|
2010-09-26 10:33:43 +02:00
|
|
|
const u8 * ImgPtr = TplFile.GetTextureBuffer(0);
|
2010-09-19 01:16:05 +02:00
|
|
|
|
2010-09-26 10:33:43 +02:00
|
|
|
if(ImgPtr)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-26 10:33:43 +02:00
|
|
|
int len = ALIGN32(TplFile.GetTextureSize(0));
|
2009-10-21 21:32:46 +02:00
|
|
|
|
2010-09-26 10:33:43 +02:00
|
|
|
data = (u8 *) memalign(32, len);
|
|
|
|
if(!data)
|
|
|
|
return;
|
2009-10-21 21:32:46 +02:00
|
|
|
|
2010-09-26 10:33:43 +02:00
|
|
|
memcpy(data, ImgPtr, len);
|
|
|
|
DCFlushRange(data, len);
|
2009-10-21 21:32:46 +02:00
|
|
|
}
|
|
|
|
}
|