forked from Aloshi/EmulationStation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageIO.cpp
More file actions
77 lines (73 loc) · 2.3 KB
/
ImageIO.cpp
File metadata and controls
77 lines (73 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "ImageIO.h"
#include <memory.h>
#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)
{
//loaded. convert to 32bit if necessary
FIBITMAP * fiConverted = nullptr;
if (FreeImage_GetBPP(fiBitmap) != 32)
{
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);
unsigned int pitch = FreeImage_GetPitch(fiBitmap);
//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++)
{
const BYTE * scanLine = FreeImage_GetScanLine(fiBitmap, i);
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);
//free bitmap data
FreeImage_Unload(fiBitmap);
}
}
else
{
LOG(LogError) << "Error - Failed to load image from memory!";
}
}
else
{
LOG(LogError) << "Error - File type unknown/unsupported!";
}
//free FIMEMORY again
FreeImage_CloseMemory(fiMemory);
}
return rawData;
}