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"
|
|
|
|
#include "SDL_Extensions.h"
|
|
|
|
|
|
|
|
#include "../render/ColorFilter.h"
|
2024-07-25 10:38:48 +00:00
|
|
|
#include "../render/Colors.h"
|
2023-10-19 16:19:09 +02:00
|
|
|
#include "../render/CBitmapHandler.h"
|
|
|
|
#include "../render/CDefFile.h"
|
|
|
|
#include "../render/Graphics.h"
|
2024-07-25 18:22:27 +00:00
|
|
|
#include "../xBRZ/xbrz.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
|
|
|
|
2024-07-25 18:22:27 +00:00
|
|
|
#include <tbb/parallel_for.h>
|
2023-10-19 16:19:09 +02:00
|
|
|
#include <SDL_surface.h>
|
2024-09-12 16:11:03 +00:00
|
|
|
#include <SDL_image.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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
if (!surf)
|
|
|
|
return;
|
|
|
|
|
|
|
|
int left = surf->w;
|
|
|
|
int top = surf->h;
|
|
|
|
int right = 0;
|
|
|
|
int bottom = 0;
|
|
|
|
|
|
|
|
// locate fully-transparent area around image
|
|
|
|
// H3 hadles this on format level, but mods or images scaled in runtime do not
|
|
|
|
if (surf->format->palette)
|
|
|
|
{
|
|
|
|
for (int y = 0; y < surf->h; ++y)
|
|
|
|
{
|
2024-08-17 20:54:29 +00:00
|
|
|
const uint8_t * row = static_cast<uint8_t *>(surf->pixels) + y * surf->pitch;
|
2024-07-25 18:22:27 +00:00
|
|
|
for (int x = 0; x < surf->w; ++x)
|
|
|
|
{
|
|
|
|
if (row[x] != 0)
|
|
|
|
{
|
|
|
|
// opaque or can be opaque (e.g. disabled shadow)
|
|
|
|
top = std::min(top, y);
|
|
|
|
left = std::min(left, x);
|
|
|
|
right = std::max(right, x);
|
|
|
|
bottom = std::max(bottom, y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (int y = 0; y < surf->h; ++y)
|
|
|
|
{
|
|
|
|
for (int x = 0; x < surf->w; ++x)
|
|
|
|
{
|
|
|
|
ColorRGBA color;
|
|
|
|
SDL_GetRGBA(CSDL_Ext::getPixel(surf, x, y), surf->format, &color.r, &color.g, &color.b, &color.a);
|
|
|
|
|
|
|
|
if (color.a != SDL_ALPHA_TRANSPARENT)
|
|
|
|
{
|
|
|
|
// opaque
|
|
|
|
top = std::min(top, y);
|
|
|
|
left = std::min(left, x);
|
|
|
|
right = std::max(right, x);
|
|
|
|
bottom = std::max(bottom, y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (left == surf->w)
|
|
|
|
{
|
|
|
|
// empty image - simply delete it
|
|
|
|
SDL_FreeSurface(surf);
|
|
|
|
surf = nullptr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (left != 0 || top != 0 || right != surf->w - 1 || bottom != surf->h - 1)
|
|
|
|
{
|
|
|
|
// non-zero border found
|
|
|
|
Rect newDimensions(left, top, right - left + 1, bottom - top + 1);
|
|
|
|
SDL_Rect rectSDL = CSDL_Ext::toSDL(newDimensions);
|
|
|
|
auto newSurface = CSDL_Ext::newSurface(newDimensions.dimensions(), surf);
|
|
|
|
SDL_SetSurfaceBlendMode(surf, SDL_BLENDMODE_NONE);
|
|
|
|
SDL_BlitSurface(surf, &rectSDL, newSurface, nullptr);
|
|
|
|
|
2024-11-18 10:19:48 +00:00
|
|
|
if (SDL_HasColorKey(surf))
|
|
|
|
{
|
|
|
|
uint32_t colorKey;
|
|
|
|
SDL_GetColorKey(surf, &colorKey);
|
|
|
|
SDL_SetColorKey(newSurface, SDL_TRUE, colorKey);
|
|
|
|
}
|
|
|
|
|
2024-07-25 18:22:27 +00:00
|
|
|
SDL_FreeSurface(surf);
|
|
|
|
surf = newSurface;
|
|
|
|
|
|
|
|
margins.x += left;
|
|
|
|
margins.y += top;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
2024-11-03 13:38:15 +01:00
|
|
|
SDL_Surface * scaled = nullptr;
|
2025-01-15 17:05:45 +00:00
|
|
|
|
|
|
|
// dump heuristics to differentiate tileable UI elements from map object / combat assets
|
|
|
|
if (mode == EImageBlitMode::OPAQUE || mode == EImageBlitMode::COLORKEY || mode == EImageBlitMode::SIMPLE)
|
|
|
|
scaled = CSDL_Ext::scaleSurfaceIntegerFactor(surf, factor, EScalingAlgorithm::XBRZ_OPAQUE);
|
2024-11-03 13:38:15 +01:00
|
|
|
else
|
2025-01-15 17:05:45 +00:00
|
|
|
scaled = CSDL_Ext::scaleSurfaceIntegerFactor(surf, factor, EScalingAlgorithm::XBRZ_ALPHA);
|
2024-07-25 18:22:27 +00:00
|
|
|
|
2025-01-15 17:05:45 +00:00
|
|
|
auto ret = std::make_shared<SDLImageShared>(scaled);
|
2024-07-25 18:22:27 +00:00
|
|
|
|
|
|
|
ret->fullSize.x = fullSize.x * factor;
|
|
|
|
ret->fullSize.y = fullSize.y * factor;
|
|
|
|
|
2025-01-15 17:05:45 +00:00
|
|
|
ret->margins.x = (int) round((float)margins.x * factor);
|
|
|
|
ret->margins.y = (int) round((float)margins.y * factor);
|
2024-07-25 18:22:27 +00:00
|
|
|
ret->optimizeSurface();
|
|
|
|
|
|
|
|
// erase our own reference
|
|
|
|
SDL_FreeSurface(scaled);
|
|
|
|
|
2024-11-20 16:06:38 +00:00
|
|
|
if (surf->format->palette)
|
2024-07-25 18:22:27 +00:00
|
|
|
SDL_SetSurfacePalette(surf, originalPalette);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2024-11-20 16:06:38 +00:00
|
|
|
float scaleX = static_cast<float>(size.x) / fullSize.x;
|
|
|
|
float scaleY = static_cast<float>(size.y) / fullSize.y;
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-07-25 10:38:48 +00:00
|
|
|
if (palette && surf->format->palette)
|
|
|
|
SDL_SetSurfacePalette(surf, palette);
|
|
|
|
|
2025-01-16 15:09:03 +00:00
|
|
|
auto scaled = CSDL_Ext::scaleSurface(surf, (int)(surf->w * scaleX), (int)(surf->h * scaleY), EScalingAlgorithm::BILINEAR);
|
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);
|
2023-10-19 16:19:09 +02:00
|
|
|
|
|
|
|
ret->fullSize.x = (int) round((float)fullSize.x * scaleX);
|
|
|
|
ret->fullSize.y = (int) round((float)fullSize.y * scaleY);
|
|
|
|
|
|
|
|
ret->margins.x = (int) round((float)margins.x * scaleX);
|
|
|
|
ret->margins.y = (int) round((float)margins.y * scaleY);
|
|
|
|
|
|
|
|
// 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
|
|
|
{
|
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
|
|
|
{
|
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-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-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-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-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-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
|
|
|
{
|
|
|
|
// 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
|
|
|
}
|