2024-06-04 11:46:45 +00:00
|
|
|
/*
|
2023-10-19 16:19:09 +02:00
|
|
|
* SDLImage.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "StdInc.h"
|
|
|
|
#include "SDLImage.h"
|
|
|
|
|
|
|
|
#include "SDLImageLoader.h"
|
2025-01-21 16:36:57 +00:00
|
|
|
#include "SDLImageScaler.h"
|
2023-10-19 16:19:09 +02:00
|
|
|
#include "SDL_Extensions.h"
|
|
|
|
|
|
|
|
#include "../render/ColorFilter.h"
|
|
|
|
#include "../render/CBitmapHandler.h"
|
|
|
|
#include "../render/CDefFile.h"
|
2024-12-15 04:44:08 +01:00
|
|
|
#include "../gui/CGuiHandler.h"
|
|
|
|
#include "../render/IScreenHandler.h"
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2025-02-21 21:59:00 +01:00
|
|
|
#include "../../lib/CConfigHandler.h"
|
|
|
|
|
2024-07-25 18:22:27 +00:00
|
|
|
#include <tbb/parallel_for.h>
|
2025-01-21 21:14:49 +00:00
|
|
|
#include <tbb/task_arena.h>
|
|
|
|
|
2024-09-12 16:11:03 +00:00
|
|
|
#include <SDL_image.h>
|
2025-01-21 21:14:49 +00:00
|
|
|
#include <SDL_surface.h>
|
|
|
|
#include <SDL_version.h>
|
2023-10-19 16:19:09 +02:00
|
|
|
|
|
|
|
class SDLImageLoader;
|
|
|
|
|
|
|
|
int IImage::width() const
|
|
|
|
{
|
|
|
|
return dimensions().x;
|
|
|
|
}
|
|
|
|
|
|
|
|
int IImage::height() const
|
|
|
|
{
|
|
|
|
return dimensions().y;
|
|
|
|
}
|
|
|
|
|
2025-01-15 17:05:45 +00:00
|
|
|
SDLImageShared::SDLImageShared(const CDefFile * data, size_t frame, size_t group)
|
2023-10-19 16:19:09 +02:00
|
|
|
: surf(nullptr),
|
|
|
|
margins(0, 0),
|
|
|
|
fullSize(0, 0),
|
2025-01-15 17:05:45 +00:00
|
|
|
originalPalette(nullptr)
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
|
|
|
SDLImageLoader loader(this);
|
|
|
|
data->loadFrame(frame, group, loader);
|
|
|
|
|
|
|
|
savePalette();
|
|
|
|
}
|
|
|
|
|
2025-01-15 17:05:45 +00:00
|
|
|
SDLImageShared::SDLImageShared(SDL_Surface * from)
|
2023-10-19 16:19:09 +02:00
|
|
|
: surf(nullptr),
|
|
|
|
margins(0, 0),
|
|
|
|
fullSize(0, 0),
|
2025-01-15 17:05:45 +00:00
|
|
|
originalPalette(nullptr)
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
|
|
|
surf = from;
|
|
|
|
if (surf == nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
savePalette();
|
|
|
|
|
|
|
|
surf->refcount++;
|
|
|
|
fullSize.x = surf->w;
|
|
|
|
fullSize.y = surf->h;
|
|
|
|
}
|
|
|
|
|
2025-01-15 17:05:45 +00:00
|
|
|
SDLImageShared::SDLImageShared(const ImagePath & filename)
|
2023-10-19 16:19:09 +02:00
|
|
|
: surf(nullptr),
|
|
|
|
margins(0, 0),
|
|
|
|
fullSize(0, 0),
|
2025-01-15 17:05:45 +00:00
|
|
|
originalPalette(nullptr)
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
|
|
|
surf = BitmapHandler::loadBitmap(filename);
|
|
|
|
|
|
|
|
if(surf == nullptr)
|
|
|
|
{
|
|
|
|
logGlobal->error("Error: failed to load image %s", filename.getOriginalName());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
savePalette();
|
|
|
|
fullSize.x = surf->w;
|
|
|
|
fullSize.y = surf->h;
|
2024-11-17 20:41:12 +00:00
|
|
|
|
|
|
|
optimizeSurface();
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-21 21:14:49 +00:00
|
|
|
void SDLImageShared::scaledDraw(SDL_Surface * where, SDL_Palette * palette, const Point & scaleTo, const Point & dest, const Rect * src, const ColorRGBA & colorMultiplier, uint8_t alpha, EImageBlitMode mode) const
|
|
|
|
{
|
2025-02-22 17:16:18 +00:00
|
|
|
if(upscalingInProgress)
|
|
|
|
throw std::runtime_error("Attempt to access images that is still being loaded!");
|
|
|
|
|
2025-01-21 21:14:49 +00:00
|
|
|
if (!surf)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Rect sourceRect(0, 0, surf->w, surf->h);
|
|
|
|
Point destShift(0, 0);
|
|
|
|
Point destScale = Point(surf->w, surf->h) * scaleTo / dimensions();
|
|
|
|
Point marginsScaled = margins * scaleTo / dimensions();
|
|
|
|
|
|
|
|
if(src)
|
|
|
|
{
|
|
|
|
Rect srcUnscaled(Point(src->topLeft() * dimensions() / scaleTo), Point(src->dimensions() * dimensions() / scaleTo));
|
|
|
|
|
|
|
|
if(srcUnscaled.x < margins.x)
|
|
|
|
destShift.x += marginsScaled.x - src->x;
|
|
|
|
|
|
|
|
if(srcUnscaled.y < margins.y)
|
|
|
|
destShift.y += marginsScaled.y - src->y;
|
|
|
|
|
|
|
|
sourceRect = Rect(srcUnscaled).intersect(Rect(margins.x, margins.y, surf->w, surf->h));
|
|
|
|
|
|
|
|
destScale.x = std::min(destScale.x, sourceRect.w * scaleTo.x / dimensions().x);
|
|
|
|
destScale.y = std::min(destScale.y, sourceRect.h * scaleTo.y / dimensions().y);
|
|
|
|
|
|
|
|
sourceRect -= margins;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
destShift = marginsScaled;
|
|
|
|
|
|
|
|
destShift += dest;
|
|
|
|
|
|
|
|
SDL_SetSurfaceColorMod(surf, colorMultiplier.r, colorMultiplier.g, colorMultiplier.b);
|
|
|
|
SDL_SetSurfaceAlphaMod(surf, alpha);
|
|
|
|
|
|
|
|
if (alpha != SDL_ALPHA_OPAQUE || (mode != EImageBlitMode::OPAQUE && surf->format->Amask != 0))
|
|
|
|
SDL_SetSurfaceBlendMode(surf, SDL_BLENDMODE_BLEND);
|
|
|
|
else
|
|
|
|
SDL_SetSurfaceBlendMode(surf, SDL_BLENDMODE_NONE);
|
|
|
|
|
|
|
|
if (palette && surf->format->palette)
|
|
|
|
SDL_SetSurfacePalette(surf, palette);
|
|
|
|
|
|
|
|
SDL_Rect srcRect = CSDL_Ext::toSDL(sourceRect);
|
|
|
|
SDL_Rect dstRect = CSDL_Ext::toSDL(Rect(destShift, destScale));
|
|
|
|
|
|
|
|
if (sourceRect.dimensions() * scaleTo / dimensions() != destScale)
|
|
|
|
logGlobal->info("???");
|
|
|
|
|
|
|
|
SDL_Surface * tempSurface = SDL_ConvertSurface(surf, where->format, 0);
|
|
|
|
int result = SDL_BlitScaled(tempSurface, &srcRect, where, &dstRect);
|
|
|
|
|
|
|
|
SDL_FreeSurface(tempSurface);
|
|
|
|
if (result != 0)
|
|
|
|
logGlobal->error("SDL_BlitScaled failed! %s", SDL_GetError());
|
|
|
|
|
|
|
|
if (surf->format->palette)
|
|
|
|
SDL_SetSurfacePalette(surf, originalPalette);
|
|
|
|
}
|
|
|
|
|
2024-07-25 10:38:48 +00:00
|
|
|
void SDLImageShared::draw(SDL_Surface * where, SDL_Palette * palette, const Point & dest, const Rect * src, const ColorRGBA & colorMultiplier, uint8_t alpha, EImageBlitMode mode) const
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
2025-02-22 17:16:18 +00:00
|
|
|
if(upscalingInProgress)
|
|
|
|
throw std::runtime_error("Attempt to access images that is still being loaded!");
|
|
|
|
|
2023-10-19 16:19:09 +02:00
|
|
|
if (!surf)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Rect sourceRect(0, 0, surf->w, surf->h);
|
|
|
|
|
|
|
|
Point destShift(0, 0);
|
|
|
|
|
|
|
|
if(src)
|
|
|
|
{
|
|
|
|
if(src->x < margins.x)
|
|
|
|
destShift.x += margins.x - src->x;
|
|
|
|
|
|
|
|
if(src->y < margins.y)
|
|
|
|
destShift.y += margins.y - src->y;
|
|
|
|
|
|
|
|
sourceRect = Rect(*src).intersect(Rect(margins.x, margins.y, surf->w, surf->h));
|
|
|
|
|
|
|
|
sourceRect -= margins;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
destShift = margins;
|
|
|
|
|
2024-06-04 11:46:45 +00:00
|
|
|
destShift += dest;
|
|
|
|
|
2024-07-25 10:38:48 +00:00
|
|
|
SDL_SetSurfaceColorMod(surf, colorMultiplier.r, colorMultiplier.g, colorMultiplier.b);
|
2024-06-04 11:46:45 +00:00
|
|
|
SDL_SetSurfaceAlphaMod(surf, alpha);
|
|
|
|
|
2024-07-19 10:46:20 +00:00
|
|
|
if (alpha != SDL_ALPHA_OPAQUE || (mode != EImageBlitMode::OPAQUE && surf->format->Amask != 0))
|
2024-06-04 11:46:45 +00:00
|
|
|
SDL_SetSurfaceBlendMode(surf, SDL_BLENDMODE_BLEND);
|
2024-06-08 07:35:13 +00:00
|
|
|
else
|
|
|
|
SDL_SetSurfaceBlendMode(surf, SDL_BLENDMODE_NONE);
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-06-04 11:46:45 +00:00
|
|
|
if (palette && surf->format->palette)
|
|
|
|
SDL_SetSurfacePalette(surf, palette);
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-11-17 17:54:55 +00:00
|
|
|
if(surf->format->palette && mode != EImageBlitMode::OPAQUE && mode != EImageBlitMode::COLORKEY)
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
2024-07-19 12:03:29 +00:00
|
|
|
CSDL_Ext::blit8bppAlphaTo24bpp(surf, sourceRect, where, destShift, alpha);
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CSDL_Ext::blitSurface(surf, sourceRect, where, destShift);
|
|
|
|
}
|
|
|
|
|
2024-07-25 10:38:48 +00:00
|
|
|
if (surf->format->palette)
|
|
|
|
SDL_SetSurfacePalette(surf, originalPalette);
|
2024-06-04 11:46:45 +00:00
|
|
|
}
|
|
|
|
|
2024-07-25 18:22:27 +00:00
|
|
|
void SDLImageShared::optimizeSurface()
|
|
|
|
{
|
2025-01-21 21:14:49 +00:00
|
|
|
assert(upscalingInProgress == false);
|
2024-07-25 18:22:27 +00:00
|
|
|
if (!surf)
|
|
|
|
return;
|
|
|
|
|
2025-01-21 16:36:57 +00:00
|
|
|
SDLImageOptimizer optimizer(surf, Rect(margins, fullSize));
|
2024-07-25 18:22:27 +00:00
|
|
|
|
2025-01-21 16:36:57 +00:00
|
|
|
optimizer.optimizeSurface(surf);
|
|
|
|
SDL_FreeSurface(surf);
|
2024-07-25 18:22:27 +00:00
|
|
|
|
2025-01-21 16:36:57 +00:00
|
|
|
surf = optimizer.acquireResultSurface();
|
|
|
|
margins = optimizer.getResultDimensions().topLeft();
|
|
|
|
fullSize = optimizer.getResultDimensions().dimensions();
|
2024-07-25 18:22:27 +00:00
|
|
|
}
|
|
|
|
|
2024-12-10 14:20:55 +00:00
|
|
|
std::shared_ptr<const ISharedImage> SDLImageShared::scaleInteger(int factor, SDL_Palette * palette, EImageBlitMode mode) const
|
2024-07-25 18:22:27 +00:00
|
|
|
{
|
2025-02-22 17:16:18 +00:00
|
|
|
if(upscalingInProgress)
|
|
|
|
throw std::runtime_error("Attempt to access images that is still being loaded!");
|
|
|
|
|
2024-07-25 18:22:27 +00:00
|
|
|
if (factor <= 0)
|
|
|
|
throw std::runtime_error("Unable to scale by integer value of " + std::to_string(factor));
|
|
|
|
|
2024-11-20 16:06:38 +00:00
|
|
|
if (!surf)
|
|
|
|
return shared_from_this();
|
|
|
|
|
|
|
|
if (palette && surf->format->palette)
|
2024-07-25 18:22:27 +00:00
|
|
|
SDL_SetSurfacePalette(surf, palette);
|
|
|
|
|
2025-01-21 21:14:49 +00:00
|
|
|
// simple heuristics to differentiate tileable UI elements from map object / combat assets
|
|
|
|
EScalingAlgorithm algorithm;
|
2025-01-15 17:05:45 +00:00
|
|
|
if (mode == EImageBlitMode::OPAQUE || mode == EImageBlitMode::COLORKEY || mode == EImageBlitMode::SIMPLE)
|
2025-01-21 21:14:49 +00:00
|
|
|
algorithm = EScalingAlgorithm::XBRZ_OPAQUE;
|
2024-11-03 13:38:15 +01:00
|
|
|
else
|
2025-01-21 21:14:49 +00:00
|
|
|
algorithm = EScalingAlgorithm::XBRZ_ALPHA;
|
2024-07-25 18:22:27 +00:00
|
|
|
|
2025-01-21 21:14:49 +00:00
|
|
|
auto result = std::make_shared<SDLImageShared>(this, factor, algorithm);
|
2024-07-25 18:22:27 +00:00
|
|
|
|
2025-01-21 21:14:49 +00:00
|
|
|
if (surf->format->palette)
|
|
|
|
SDL_SetSurfacePalette(surf, originalPalette);
|
2024-07-25 18:22:27 +00:00
|
|
|
|
2025-01-21 21:14:49 +00:00
|
|
|
return result;
|
|
|
|
}
|
2024-07-25 18:22:27 +00:00
|
|
|
|
2025-01-21 21:14:49 +00:00
|
|
|
SDLImageShared::SDLImageShared(const SDLImageShared * from, int integerScaleFactor, EScalingAlgorithm algorithm)
|
|
|
|
{
|
|
|
|
static tbb::task_arena upscalingArena;
|
2024-07-25 18:22:27 +00:00
|
|
|
|
2025-01-21 21:14:49 +00:00
|
|
|
upscalingInProgress = true;
|
2024-07-25 18:22:27 +00:00
|
|
|
|
2025-01-26 11:18:21 +00:00
|
|
|
auto scaler = std::make_shared<SDLImageScaler>(from->surf, Rect(from->margins, from->fullSize), true);
|
2025-01-21 21:14:49 +00:00
|
|
|
|
|
|
|
const auto & scalingTask = [this, algorithm, scaler]()
|
|
|
|
{
|
|
|
|
scaler->scaleSurfaceIntegerFactor(GH.screenHandler().getScalingFactor(), algorithm);
|
|
|
|
surf = scaler->acquireResultSurface();
|
|
|
|
fullSize = scaler->getResultDimensions().dimensions();
|
|
|
|
margins = scaler->getResultDimensions().topLeft();
|
|
|
|
|
|
|
|
upscalingInProgress = false;
|
|
|
|
};
|
|
|
|
|
2025-02-21 21:59:00 +01:00
|
|
|
if(settings["video"]["asyncUpscaling"].Bool())
|
|
|
|
upscalingArena.enqueue(scalingTask);
|
|
|
|
else
|
|
|
|
scalingTask();
|
2025-01-21 21:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SDLImageShared::isLoading() const
|
|
|
|
{
|
|
|
|
return upscalingInProgress;
|
2024-07-25 18:22:27 +00:00
|
|
|
}
|
|
|
|
|
2024-11-16 15:40:05 +00:00
|
|
|
std::shared_ptr<const ISharedImage> SDLImageShared::scaleTo(const Point & size, SDL_Palette * palette) const
|
2024-06-04 11:46:45 +00:00
|
|
|
{
|
2025-02-22 17:16:18 +00:00
|
|
|
if(upscalingInProgress)
|
|
|
|
throw std::runtime_error("Attempt to access images that is still being loaded!");
|
|
|
|
|
2024-07-25 10:38:48 +00:00
|
|
|
if (palette && surf->format->palette)
|
|
|
|
SDL_SetSurfacePalette(surf, palette);
|
|
|
|
|
2025-01-26 11:18:21 +00:00
|
|
|
SDLImageScaler scaler(surf, Rect(margins, fullSize), true);
|
2025-01-21 16:36:57 +00:00
|
|
|
|
|
|
|
scaler.scaleSurface(size, EScalingAlgorithm::XBRZ_ALPHA);
|
|
|
|
|
|
|
|
auto scaled = scaler.acquireResultSurface();
|
2023-10-19 16:19:09 +02:00
|
|
|
|
|
|
|
if (scaled->format && scaled->format->palette) // fix color keying, because SDL loses it at this point
|
|
|
|
CSDL_Ext::setColorKey(scaled, scaled->format->palette->colors[0]);
|
|
|
|
else if(scaled->format && scaled->format->Amask)
|
|
|
|
SDL_SetSurfaceBlendMode(scaled, SDL_BLENDMODE_BLEND);//just in case
|
|
|
|
else
|
|
|
|
CSDL_Ext::setDefaultColorKey(scaled);//just in case
|
|
|
|
|
2025-01-15 17:05:45 +00:00
|
|
|
auto ret = std::make_shared<SDLImageShared>(scaled);
|
2025-01-21 16:36:57 +00:00
|
|
|
ret->fullSize = scaler.getResultDimensions().dimensions();
|
|
|
|
ret->margins = scaler.getResultDimensions().topLeft();
|
2023-10-19 16:19:09 +02:00
|
|
|
|
|
|
|
// erase our own reference
|
|
|
|
SDL_FreeSurface(scaled);
|
|
|
|
|
2024-07-25 10:38:48 +00:00
|
|
|
if (surf->format->palette)
|
|
|
|
SDL_SetSurfacePalette(surf, originalPalette);
|
|
|
|
|
2024-06-04 11:46:45 +00:00
|
|
|
return ret;
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
|
2024-09-12 16:11:03 +00:00
|
|
|
void SDLImageShared::exportBitmap(const boost::filesystem::path& path, SDL_Palette * palette) const
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
2025-01-29 10:03:58 +00:00
|
|
|
auto directory = path;
|
|
|
|
directory.remove_filename();
|
|
|
|
boost::filesystem::create_directories(directory);
|
|
|
|
|
2025-02-22 17:16:18 +00:00
|
|
|
if(upscalingInProgress)
|
|
|
|
throw std::runtime_error("Attempt to access images that is still being loaded!");
|
|
|
|
|
2024-09-13 12:26:31 +00:00
|
|
|
if (!surf)
|
|
|
|
return;
|
|
|
|
|
2024-09-12 16:11:03 +00:00
|
|
|
if (palette && surf->format->palette)
|
|
|
|
SDL_SetSurfacePalette(surf, palette);
|
|
|
|
IMG_SavePNG(surf, path.string().c_str());
|
|
|
|
if (palette && surf->format->palette)
|
|
|
|
SDL_SetSurfacePalette(surf, originalPalette);
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
|
2024-07-24 13:04:45 +00:00
|
|
|
bool SDLImageShared::isTransparent(const Point & coords) const
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
2025-02-22 17:16:18 +00:00
|
|
|
if(upscalingInProgress)
|
|
|
|
throw std::runtime_error("Attempt to access images that is still being loaded!");
|
|
|
|
|
2024-06-17 17:09:19 +00:00
|
|
|
if (surf)
|
2024-11-18 10:19:48 +00:00
|
|
|
return CSDL_Ext::isTransparent(surf, coords.x - margins.x, coords.y - margins.y);
|
2024-06-17 17:09:19 +00:00
|
|
|
else
|
|
|
|
return true;
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
|
2024-12-26 14:18:25 +01:00
|
|
|
Rect SDLImageShared::contentRect() const
|
|
|
|
{
|
2025-02-22 17:16:18 +00:00
|
|
|
if(upscalingInProgress)
|
|
|
|
throw std::runtime_error("Attempt to access images that is still being loaded!");
|
|
|
|
|
2025-01-15 17:05:45 +00:00
|
|
|
auto tmpMargins = margins;
|
|
|
|
auto tmpSize = Point(surf->w, surf->h);
|
2024-12-26 14:18:25 +01:00
|
|
|
return Rect(tmpMargins, tmpSize);
|
|
|
|
}
|
|
|
|
|
2025-01-15 17:05:45 +00:00
|
|
|
const SDL_Palette * SDLImageShared::getPalette() const
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
2025-02-22 17:16:18 +00:00
|
|
|
if(upscalingInProgress)
|
|
|
|
throw std::runtime_error("Attempt to access images that is still being loaded!");
|
|
|
|
|
2025-01-15 17:05:45 +00:00
|
|
|
if (!surf)
|
|
|
|
return nullptr;
|
|
|
|
return surf->format->palette;
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
|
2025-01-15 17:05:45 +00:00
|
|
|
Point SDLImageShared::dimensions() const
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
2025-02-22 17:16:18 +00:00
|
|
|
if(upscalingInProgress)
|
|
|
|
throw std::runtime_error("Attempt to access images that is still being loaded!");
|
|
|
|
|
2025-01-15 17:05:45 +00:00
|
|
|
return fullSize;
|
2024-06-04 11:46:45 +00:00
|
|
|
}
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-11-16 15:40:05 +00:00
|
|
|
std::shared_ptr<const ISharedImage> SDLImageShared::horizontalFlip() const
|
2024-06-04 11:46:45 +00:00
|
|
|
{
|
2025-02-22 17:16:18 +00:00
|
|
|
if(upscalingInProgress)
|
|
|
|
throw std::runtime_error("Attempt to access images that is still being loaded!");
|
|
|
|
|
2025-01-01 21:00:43 +00:00
|
|
|
if (!surf)
|
|
|
|
return shared_from_this();
|
|
|
|
|
2023-10-19 16:19:09 +02:00
|
|
|
SDL_Surface * flipped = CSDL_Ext::horizontalFlip(surf);
|
2025-01-15 17:05:45 +00:00
|
|
|
auto ret = std::make_shared<SDLImageShared>(flipped);
|
2024-06-04 11:46:45 +00:00
|
|
|
ret->fullSize = fullSize;
|
|
|
|
ret->margins.x = margins.x;
|
|
|
|
ret->margins.y = fullSize.y - surf->h - margins.y;
|
|
|
|
ret->fullSize = fullSize;
|
|
|
|
|
|
|
|
return ret;
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
|
2024-11-16 15:40:05 +00:00
|
|
|
std::shared_ptr<const ISharedImage> SDLImageShared::verticalFlip() const
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
2025-02-22 17:16:18 +00:00
|
|
|
if(upscalingInProgress)
|
|
|
|
throw std::runtime_error("Attempt to access images that is still being loaded!");
|
|
|
|
|
2025-01-01 21:00:43 +00:00
|
|
|
if (!surf)
|
|
|
|
return shared_from_this();
|
|
|
|
|
2023-10-19 16:19:09 +02:00
|
|
|
SDL_Surface * flipped = CSDL_Ext::verticalFlip(surf);
|
2025-01-15 17:05:45 +00:00
|
|
|
auto ret = std::make_shared<SDLImageShared>(flipped);
|
2024-06-04 11:46:45 +00:00
|
|
|
ret->fullSize = fullSize;
|
|
|
|
ret->margins.x = fullSize.x - surf->w - margins.x;
|
|
|
|
ret->margins.y = margins.y;
|
|
|
|
ret->fullSize = fullSize;
|
|
|
|
|
|
|
|
return ret;
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Keep the original palette, in order to do color switching operation
|
2024-07-24 13:04:45 +00:00
|
|
|
void SDLImageShared::savePalette()
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
2025-02-22 17:16:18 +00:00
|
|
|
if(upscalingInProgress)
|
|
|
|
throw std::runtime_error("Attempt to access images that is still being loaded!");
|
|
|
|
|
2023-10-19 16:19:09 +02:00
|
|
|
// For some images that don't have palette, skip this
|
|
|
|
if(surf->format->palette == nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(originalPalette == nullptr)
|
2024-06-04 16:09:25 +00:00
|
|
|
originalPalette = SDL_AllocPalette(surf->format->palette->ncolors);
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-06-04 16:09:25 +00:00
|
|
|
SDL_SetPaletteColors(originalPalette, surf->format->palette->colors, 0, surf->format->palette->ncolors);
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
|
2024-07-24 13:04:45 +00:00
|
|
|
SDLImageShared::~SDLImageShared()
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
|
|
|
SDL_FreeSurface(surf);
|
2024-06-02 19:31:56 +00:00
|
|
|
SDL_FreePalette(originalPalette);
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|