2013-05-16 19:26:19 +00:00
|
|
|
#include "ImageIO.h"
|
|
|
|
|
2013-05-16 21:25:54 +00:00
|
|
|
#include <memory.h>
|
|
|
|
|
2013-05-16 19:26:19 +00:00
|
|
|
#include "Log.h"
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<unsigned char> ImageIO::loadFromMemoryRGBA32(const unsigned char * data, const size_t size, size_t & width, size_t & height)
|
|
|
|
{
|
|
|
|
std::vector<unsigned char> rawData;
|
|
|
|
width = 0;
|
|
|
|
height = 0;
|
|
|
|
FIMEMORY * fiMemory = FreeImage_OpenMemory((BYTE *)data, size);
|
|
|
|
if (fiMemory != nullptr) {
|
|
|
|
//detect the filetype from data
|
|
|
|
FREE_IMAGE_FORMAT format = FreeImage_GetFileTypeFromMemory(fiMemory);
|
|
|
|
if (format != FIF_UNKNOWN && FreeImage_FIFSupportsReading(format))
|
|
|
|
{
|
|
|
|
//file type is supported. load image
|
|
|
|
FIBITMAP * fiBitmap = FreeImage_LoadFromMemory(format, fiMemory);
|
|
|
|
if (fiBitmap != nullptr)
|
|
|
|
{
|
2013-05-29 17:49:28 +00:00
|
|
|
//loaded. convert to 32bit if necessary
|
|
|
|
if (FreeImage_GetBPP(fiBitmap) != 32)
|
2013-05-16 19:26:19 +00:00
|
|
|
{
|
2013-05-29 17:49:28 +00:00
|
|
|
FIBITMAP * fiConverted = FreeImage_ConvertTo32Bits(fiBitmap);
|
|
|
|
if (fiConverted != nullptr)
|
|
|
|
{
|
|
|
|
//free original bitmap data
|
|
|
|
FreeImage_Unload(fiBitmap);
|
|
|
|
fiBitmap = fiConverted;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fiBitmap != nullptr)
|
|
|
|
{
|
|
|
|
width = FreeImage_GetWidth(fiBitmap);
|
|
|
|
height = FreeImage_GetHeight(fiBitmap);
|
2013-05-16 19:26:19 +00:00
|
|
|
//loop through scanlines and add all pixel data to the return vector
|
|
|
|
//this is necessary, because width*height*bpp might not be == pitch
|
|
|
|
unsigned char * tempData = new unsigned char[width * height * 4];
|
|
|
|
for (size_t i = 0; i < height; i++)
|
|
|
|
{
|
2013-05-29 17:49:28 +00:00
|
|
|
const BYTE * scanLine = FreeImage_GetScanLine(fiBitmap, i);
|
2013-05-16 19:26:19 +00:00
|
|
|
memcpy(tempData + (i * width * 4), scanLine, width * 4);
|
|
|
|
}
|
|
|
|
//convert from BGRA to RGBA
|
|
|
|
for(size_t i = 0; i < width*height; i++)
|
|
|
|
{
|
|
|
|
RGBQUAD bgra = ((RGBQUAD *)tempData)[i];
|
|
|
|
RGBQUAD rgba;
|
|
|
|
rgba.rgbBlue = bgra.rgbRed;
|
|
|
|
rgba.rgbGreen = bgra.rgbGreen;
|
|
|
|
rgba.rgbRed = bgra.rgbBlue;
|
|
|
|
rgba.rgbReserved = bgra.rgbReserved;
|
|
|
|
((RGBQUAD *)tempData)[i] = rgba;
|
|
|
|
}
|
|
|
|
rawData = std::vector<unsigned char>(tempData, tempData + width * height * 4);
|
2013-05-29 17:49:28 +00:00
|
|
|
//free bitmap data
|
|
|
|
FreeImage_Unload(fiBitmap);
|
2013-07-03 08:17:34 +00:00
|
|
|
delete[] tempData;
|
2013-05-16 19:26:19 +00:00
|
|
|
}
|
2013-05-29 17:49:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOG(LogError) << "Error - Failed to load image from memory!";
|
2013-05-16 19:26:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-03-22 19:31:13 +00:00
|
|
|
LOG(LogError) << "Error - File type " << (format == FIF_UNKNOWN ? "unknown" : "unsupported") << "!";
|
2013-05-16 19:26:19 +00:00
|
|
|
}
|
|
|
|
//free FIMEMORY again
|
|
|
|
FreeImage_CloseMemory(fiMemory);
|
|
|
|
}
|
|
|
|
return rawData;
|
|
|
|
}
|
2014-05-28 21:43:23 +00:00
|
|
|
|
|
|
|
void ImageIO::flipPixelsVert(unsigned char* imagePx, const size_t& width, const size_t& height)
|
|
|
|
{
|
|
|
|
unsigned int temp;
|
|
|
|
unsigned int* arr = (unsigned int*)imagePx;
|
|
|
|
for(size_t y = 0; y < height / 2; y++)
|
|
|
|
{
|
|
|
|
for(size_t x = 0; x < width; x++)
|
|
|
|
{
|
|
|
|
temp = arr[x + (y * width)];
|
|
|
|
arr[x + (y * width)] = arr[x + (height * width) - ((y + 1) * width)];
|
|
|
|
arr[x + (height * width) - ((y + 1) * width)] = temp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|