Some minor code cleanup.

This commit is contained in:
Leon Styhre 2021-01-15 18:47:01 +01:00
parent a99d32f596
commit ee8e0a0c89
3 changed files with 16 additions and 17 deletions

View file

@ -14,25 +14,25 @@
#include <string.h> #include <string.h>
std::vector<unsigned char> ImageIO::loadFromMemoryRGBA32(const unsigned char* data, std::vector<unsigned char> ImageIO::loadFromMemoryRGBA32(const unsigned char* data,
const size_t size, size_t & width, size_t & height) const size_t size, size_t& width, size_t& height)
{ {
std::vector<unsigned char> rawData; std::vector<unsigned char> rawData;
width = 0; width = 0;
height = 0; height = 0;
FIMEMORY * fiMemory = FreeImage_OpenMemory((BYTE *)data, (DWORD)size); FIMEMORY* fiMemory = FreeImage_OpenMemory(const_cast<BYTE*>(data), static_cast<DWORD>(size));
if (fiMemory != nullptr) { if (fiMemory != nullptr) {
// Detect the filetype from data. // Detect the filetype from data.
FREE_IMAGE_FORMAT format = FreeImage_GetFileTypeFromMemory(fiMemory); FREE_IMAGE_FORMAT format = FreeImage_GetFileTypeFromMemory(fiMemory);
if (format != FIF_UNKNOWN && FreeImage_FIFSupportsReading(format)) { if (format != FIF_UNKNOWN && FreeImage_FIFSupportsReading(format)) {
// File type is supported. load image, // File type is supported, load image.
FIBITMAP * fiBitmap = FreeImage_LoadFromMemory(format, fiMemory); FIBITMAP* fiBitmap = FreeImage_LoadFromMemory(format, fiMemory);
if (fiBitmap != nullptr) { if (fiBitmap != nullptr) {
// Loaded. convert to 32bit if necessary. // Loaded. convert to 32-bit if necessary.
if (FreeImage_GetBPP(fiBitmap) != 32) { if (FreeImage_GetBPP(fiBitmap) != 32) {
FIBITMAP * fiConverted = FreeImage_ConvertTo32Bits(fiBitmap); FIBITMAP* fiConverted = FreeImage_ConvertTo32Bits(fiBitmap);
if (fiConverted != nullptr) { if (fiConverted != nullptr) {
//free original bitmap data // Free original bitmap data.
FreeImage_Unload(fiBitmap); FreeImage_Unload(fiBitmap);
fiBitmap = fiConverted; fiBitmap = fiConverted;
} }
@ -42,14 +42,14 @@ std::vector<unsigned char> ImageIO::loadFromMemoryRGBA32(const unsigned char* da
height = FreeImage_GetHeight(fiBitmap); height = FreeImage_GetHeight(fiBitmap);
// Loop through scanlines and add all pixel data to the return vector. // Loop through scanlines and add all pixel data to the return vector.
// This is necessary, because width*height*bpp might not be == pitch. // This is necessary, because width*height*bpp might not be == pitch.
unsigned char * tempData = new unsigned char[width * height * 4]; unsigned char* tempData = new unsigned char[width * height * 4];
for (size_t i = 0; i < height; i++) { for (size_t i = 0; i < height; i++) {
const BYTE * scanLine = FreeImage_GetScanLine(fiBitmap, const BYTE* scanLine =
static_cast<int>(i)); FreeImage_GetScanLine(fiBitmap, static_cast<int>(i));
memcpy(tempData + (i * width * 4), scanLine, width * 4); memcpy(tempData + (i * width * 4), scanLine, width * 4);
} }
// Convert from BGRA to RGBA. // Convert from BGRA to RGBA.
for (size_t i = 0; i < width*height; i++) { for (size_t i = 0; i < width * height; i++) {
RGBQUAD bgra = reinterpret_cast<RGBQUAD*>(tempData)[i]; RGBQUAD bgra = reinterpret_cast<RGBQUAD*>(tempData)[i];
RGBQUAD rgba; RGBQUAD rgba;
rgba.rgbBlue = bgra.rgbRed; rgba.rgbBlue = bgra.rgbRed;
@ -65,15 +65,14 @@ std::vector<unsigned char> ImageIO::loadFromMemoryRGBA32(const unsigned char* da
} }
} }
else { else {
LOG(LogError) << "Failed to load image from memory!"; LOG(LogError) << "Failed to load image from memory";
} }
} }
else { else {
LOG(LogError) << "Couldn't load image, file is missing or the file type is " << LOG(LogError) << "Couldn't load image, file is missing or the file type is " <<
// it's not existing or the file type " << (format == FIF_UNKNOWN ? "unknown" : "unsupported");
(format == FIF_UNKNOWN ? "unknown" : "unsupported") << "!";
} }
// Free fiMemory again // Free fiMemory again.
FreeImage_CloseMemory(fiMemory); FreeImage_CloseMemory(fiMemory);
} }
return rawData; return rawData;

View file

@ -52,7 +52,7 @@ void NinePatchComponent::buildVertices()
if (mVertices != nullptr) if (mVertices != nullptr)
delete[] mVertices; delete[] mVertices;
// Scale the corner size according to the screen resolution. // Scale the corner size relative to the screen resolution.
mTexture = TextureResource::get(mPath, false, false, true, Renderer::getScreenWidthModifier()); mTexture = TextureResource::get(mPath, false, false, true, Renderer::getScreenWidthModifier());
if (mTexture->getSize() == Vector2i::Zero()) { if (mTexture->getSize() == Vector2i::Zero()) {

View file

@ -335,7 +335,7 @@ namespace Renderer
void popClipRect() void popClipRect()
{ {
if (clipStack.empty()) { if (clipStack.empty()) {
LOG(LogError) << "Tried to popClipRect while the stack was empty!"; LOG(LogError) << "Tried to popClipRect while the stack was empty";
return; return;
} }