|
@@ -11,76 +11,24 @@
|
|
|
#include "SDLImage.h"
|
|
|
|
|
|
#include "SDLImageLoader.h"
|
|
|
+#include "SDLImageScaler.h"
|
|
|
#include "SDL_Extensions.h"
|
|
|
|
|
|
#include "../render/ColorFilter.h"
|
|
|
-#include "../render/Colors.h"
|
|
|
#include "../render/CBitmapHandler.h"
|
|
|
#include "../render/CDefFile.h"
|
|
|
-#include "../render/Graphics.h"
|
|
|
-#include "../xBRZ/xbrz.h"
|
|
|
#include "../gui/CGuiHandler.h"
|
|
|
#include "../render/IScreenHandler.h"
|
|
|
|
|
|
#include <tbb/parallel_for.h>
|
|
|
-#include <SDL_surface.h>
|
|
|
+#include <tbb/task_arena.h>
|
|
|
+
|
|
|
#include <SDL_image.h>
|
|
|
+#include <SDL_surface.h>
|
|
|
+#include <SDL_version.h>
|
|
|
|
|
|
class SDLImageLoader;
|
|
|
|
|
|
-//First 8 colors in def palette used for transparency
|
|
|
-static constexpr std::array<SDL_Color, 8> sourcePalette = {{
|
|
|
- {0, 255, 255, SDL_ALPHA_OPAQUE},
|
|
|
- {255, 150, 255, SDL_ALPHA_OPAQUE},
|
|
|
- {255, 100, 255, SDL_ALPHA_OPAQUE},
|
|
|
- {255, 50, 255, SDL_ALPHA_OPAQUE},
|
|
|
- {255, 0, 255, SDL_ALPHA_OPAQUE},
|
|
|
- {255, 255, 0, SDL_ALPHA_OPAQUE},
|
|
|
- {180, 0, 255, SDL_ALPHA_OPAQUE},
|
|
|
- {0, 255, 0, SDL_ALPHA_OPAQUE}
|
|
|
-}};
|
|
|
-
|
|
|
-static constexpr std::array<ColorRGBA, 8> targetPalette = {{
|
|
|
- {0, 0, 0, 0 }, // 0 - transparency ( used in most images )
|
|
|
- {0, 0, 0, 64 }, // 1 - shadow border ( used in battle, adventure map def's )
|
|
|
- {0, 0, 0, 64 }, // 2 - shadow border ( used in fog-of-war def's )
|
|
|
- {0, 0, 0, 128}, // 3 - shadow body ( used in fog-of-war def's )
|
|
|
- {0, 0, 0, 128}, // 4 - shadow body ( used in battle, adventure map def's )
|
|
|
- {0, 0, 0, 0 }, // 5 - selection / owner flag ( used in battle, adventure map def's )
|
|
|
- {0, 0, 0, 128}, // 6 - shadow body below selection ( used in battle def's )
|
|
|
- {0, 0, 0, 64 } // 7 - shadow border below selection ( used in battle def's )
|
|
|
-}};
|
|
|
-
|
|
|
-static ui8 mixChannels(ui8 c1, ui8 c2, ui8 a1, ui8 a2)
|
|
|
-{
|
|
|
- return c1*a1 / 256 + c2*a2*(255 - a1) / 256 / 256;
|
|
|
-}
|
|
|
-
|
|
|
-static ColorRGBA addColors(const ColorRGBA & base, const ColorRGBA & over)
|
|
|
-{
|
|
|
- return ColorRGBA(
|
|
|
- mixChannels(over.r, base.r, over.a, base.a),
|
|
|
- mixChannels(over.g, base.g, over.a, base.a),
|
|
|
- mixChannels(over.b, base.b, over.a, base.a),
|
|
|
- static_cast<ui8>(over.a + base.a * (255 - over.a) / 256)
|
|
|
- );
|
|
|
-}
|
|
|
-
|
|
|
-static bool colorsSimilar (const SDL_Color & lhs, const SDL_Color & rhs)
|
|
|
-{
|
|
|
- // it seems that H3 does not requires exact match to replace colors -> (255, 103, 255) gets interpreted as shadow
|
|
|
- // exact logic is not clear and requires extensive testing with image editing
|
|
|
- // potential reason is that H3 uses 16-bit color format (565 RGB bits), meaning that 3 least significant bits are lost in red and blue component
|
|
|
- static const int threshold = 8;
|
|
|
-
|
|
|
- int diffR = static_cast<int>(lhs.r) - rhs.r;
|
|
|
- int diffG = static_cast<int>(lhs.g) - rhs.g;
|
|
|
- int diffB = static_cast<int>(lhs.b) - rhs.b;
|
|
|
- int diffA = static_cast<int>(lhs.a) - rhs.a;
|
|
|
-
|
|
|
- return std::abs(diffR) < threshold && std::abs(diffG) < threshold && std::abs(diffB) < threshold && std::abs(diffA) < threshold;
|
|
|
-}
|
|
|
-
|
|
|
int IImage::width() const
|
|
|
{
|
|
|
return dimensions().x;
|
|
@@ -91,12 +39,11 @@ int IImage::height() const
|
|
|
return dimensions().y;
|
|
|
}
|
|
|
|
|
|
-SDLImageShared::SDLImageShared(const CDefFile * data, size_t frame, size_t group, int preScaleFactor)
|
|
|
+SDLImageShared::SDLImageShared(const CDefFile * data, size_t frame, size_t group)
|
|
|
: surf(nullptr),
|
|
|
margins(0, 0),
|
|
|
fullSize(0, 0),
|
|
|
- originalPalette(nullptr),
|
|
|
- preScaleFactor(preScaleFactor)
|
|
|
+ originalPalette(nullptr)
|
|
|
{
|
|
|
SDLImageLoader loader(this);
|
|
|
data->loadFrame(frame, group, loader);
|
|
@@ -104,12 +51,11 @@ SDLImageShared::SDLImageShared(const CDefFile * data, size_t frame, size_t group
|
|
|
savePalette();
|
|
|
}
|
|
|
|
|
|
-SDLImageShared::SDLImageShared(SDL_Surface * from, int preScaleFactor)
|
|
|
+SDLImageShared::SDLImageShared(SDL_Surface * from)
|
|
|
: surf(nullptr),
|
|
|
margins(0, 0),
|
|
|
fullSize(0, 0),
|
|
|
- originalPalette(nullptr),
|
|
|
- preScaleFactor(preScaleFactor)
|
|
|
+ originalPalette(nullptr)
|
|
|
{
|
|
|
surf = from;
|
|
|
if (surf == nullptr)
|
|
@@ -122,12 +68,11 @@ SDLImageShared::SDLImageShared(SDL_Surface * from, int preScaleFactor)
|
|
|
fullSize.y = surf->h;
|
|
|
}
|
|
|
|
|
|
-SDLImageShared::SDLImageShared(const ImagePath & filename, int preScaleFactor)
|
|
|
+SDLImageShared::SDLImageShared(const ImagePath & filename)
|
|
|
: surf(nullptr),
|
|
|
margins(0, 0),
|
|
|
fullSize(0, 0),
|
|
|
- originalPalette(nullptr),
|
|
|
- preScaleFactor(preScaleFactor)
|
|
|
+ originalPalette(nullptr)
|
|
|
{
|
|
|
surf = BitmapHandler::loadBitmap(filename);
|
|
|
|
|
@@ -146,9 +91,70 @@ SDLImageShared::SDLImageShared(const ImagePath & filename, int preScaleFactor)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+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
|
|
|
+{
|
|
|
+ assert(upscalingInProgress == false);
|
|
|
+ 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);
|
|
|
+}
|
|
|
|
|
|
void SDLImageShared::draw(SDL_Surface * where, SDL_Palette * palette, const Point & dest, const Rect * src, const ColorRGBA & colorMultiplier, uint8_t alpha, EImageBlitMode mode) const
|
|
|
{
|
|
|
+ assert(upscalingInProgress == false);
|
|
|
if (!surf)
|
|
|
return;
|
|
|
|
|
@@ -199,89 +205,23 @@ void SDLImageShared::draw(SDL_Surface * where, SDL_Palette * palette, const Poin
|
|
|
|
|
|
void SDLImageShared::optimizeSurface()
|
|
|
{
|
|
|
+ assert(upscalingInProgress == false);
|
|
|
if (!surf)
|
|
|
return;
|
|
|
|
|
|
- int left = surf->w;
|
|
|
- int top = surf->h;
|
|
|
- int right = 0;
|
|
|
- int bottom = 0;
|
|
|
+ SDLImageOptimizer optimizer(surf, Rect(margins, fullSize));
|
|
|
|
|
|
- // 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)
|
|
|
- {
|
|
|
- const uint8_t * row = static_cast<uint8_t *>(surf->pixels) + y * surf->pitch;
|
|
|
- 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);
|
|
|
-
|
|
|
- if (SDL_HasColorKey(surf))
|
|
|
- {
|
|
|
- uint32_t colorKey;
|
|
|
- SDL_GetColorKey(surf, &colorKey);
|
|
|
- SDL_SetColorKey(newSurface, SDL_TRUE, colorKey);
|
|
|
- }
|
|
|
-
|
|
|
- SDL_FreeSurface(surf);
|
|
|
- surf = newSurface;
|
|
|
+ optimizer.optimizeSurface(surf);
|
|
|
+ SDL_FreeSurface(surf);
|
|
|
|
|
|
- margins.x += left;
|
|
|
- margins.y += top;
|
|
|
- }
|
|
|
+ surf = optimizer.acquireResultSurface();
|
|
|
+ margins = optimizer.getResultDimensions().topLeft();
|
|
|
+ fullSize = optimizer.getResultDimensions().dimensions();
|
|
|
}
|
|
|
|
|
|
std::shared_ptr<const ISharedImage> SDLImageShared::scaleInteger(int factor, SDL_Palette * palette, EImageBlitMode mode) const
|
|
|
{
|
|
|
+ assert(upscalingInProgress == false);
|
|
|
if (factor <= 0)
|
|
|
throw std::runtime_error("Unable to scale by integer value of " + std::to_string(factor));
|
|
|
|
|
@@ -291,47 +231,58 @@ std::shared_ptr<const ISharedImage> SDLImageShared::scaleInteger(int factor, SDL
|
|
|
if (palette && surf->format->palette)
|
|
|
SDL_SetSurfacePalette(surf, palette);
|
|
|
|
|
|
- SDL_Surface * scaled = nullptr;
|
|
|
- if(preScaleFactor == factor)
|
|
|
- return shared_from_this();
|
|
|
- else if(preScaleFactor == 1)
|
|
|
- {
|
|
|
- // 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);
|
|
|
- else
|
|
|
- scaled = CSDL_Ext::scaleSurfaceIntegerFactor(surf, factor, EScalingAlgorithm::XBRZ_ALPHA);
|
|
|
- }
|
|
|
+ // simple heuristics to differentiate tileable UI elements from map object / combat assets
|
|
|
+ EScalingAlgorithm algorithm;
|
|
|
+ if (mode == EImageBlitMode::OPAQUE || mode == EImageBlitMode::COLORKEY || mode == EImageBlitMode::SIMPLE)
|
|
|
+ algorithm = EScalingAlgorithm::XBRZ_OPAQUE;
|
|
|
else
|
|
|
- scaled = CSDL_Ext::scaleSurface(surf, (int) round((float)surf->w * factor / preScaleFactor), (int) round((float)surf->h * factor / preScaleFactor));
|
|
|
+ algorithm = EScalingAlgorithm::XBRZ_ALPHA;
|
|
|
|
|
|
- auto ret = std::make_shared<SDLImageShared>(scaled, preScaleFactor);
|
|
|
+ auto result = std::make_shared<SDLImageShared>(this, factor, algorithm);
|
|
|
|
|
|
- ret->fullSize.x = fullSize.x * factor;
|
|
|
- ret->fullSize.y = fullSize.y * factor;
|
|
|
+ if (surf->format->palette)
|
|
|
+ SDL_SetSurfacePalette(surf, originalPalette);
|
|
|
|
|
|
- ret->margins.x = (int) round((float)margins.x * factor / preScaleFactor);
|
|
|
- ret->margins.y = (int) round((float)margins.y * factor / preScaleFactor);
|
|
|
- ret->optimizeSurface();
|
|
|
+ return result;
|
|
|
+}
|
|
|
|
|
|
- // erase our own reference
|
|
|
- SDL_FreeSurface(scaled);
|
|
|
+SDLImageShared::SDLImageShared(const SDLImageShared * from, int integerScaleFactor, EScalingAlgorithm algorithm)
|
|
|
+{
|
|
|
+ static tbb::task_arena upscalingArena;
|
|
|
|
|
|
- if (surf->format->palette)
|
|
|
- SDL_SetSurfacePalette(surf, originalPalette);
|
|
|
+ upscalingInProgress = true;
|
|
|
|
|
|
- return ret;
|
|
|
+ auto scaler = std::make_shared<SDLImageScaler>(from->surf, Rect(from->margins, from->fullSize));
|
|
|
+
|
|
|
+ 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;
|
|
|
+ };
|
|
|
+
|
|
|
+ upscalingArena.enqueue(scalingTask);
|
|
|
}
|
|
|
|
|
|
-std::shared_ptr<const ISharedImage> SDLImageShared::scaleTo(const Point & size, SDL_Palette * palette) const
|
|
|
+bool SDLImageShared::isLoading() const
|
|
|
{
|
|
|
- float scaleX = static_cast<float>(size.x) / fullSize.x;
|
|
|
- float scaleY = static_cast<float>(size.y) / fullSize.y;
|
|
|
+ return upscalingInProgress;
|
|
|
+}
|
|
|
|
|
|
+std::shared_ptr<const ISharedImage> SDLImageShared::scaleTo(const Point & size, SDL_Palette * palette) const
|
|
|
+{
|
|
|
+ assert(upscalingInProgress == false);
|
|
|
if (palette && surf->format->palette)
|
|
|
SDL_SetSurfacePalette(surf, palette);
|
|
|
|
|
|
- auto scaled = CSDL_Ext::scaleSurface(surf, (int)(surf->w * scaleX), (int)(surf->h * scaleY));
|
|
|
+ SDLImageScaler scaler(surf, Rect(margins, fullSize));
|
|
|
+
|
|
|
+ scaler.scaleSurface(size, EScalingAlgorithm::XBRZ_ALPHA);
|
|
|
+
|
|
|
+ auto scaled = scaler.acquireResultSurface();
|
|
|
|
|
|
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]);
|
|
@@ -340,13 +291,9 @@ std::shared_ptr<const ISharedImage> SDLImageShared::scaleTo(const Point & size,
|
|
|
else
|
|
|
CSDL_Ext::setDefaultColorKey(scaled);//just in case
|
|
|
|
|
|
- auto ret = std::make_shared<SDLImageShared>(scaled, preScaleFactor);
|
|
|
-
|
|
|
- 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);
|
|
|
+ auto ret = std::make_shared<SDLImageShared>(scaled);
|
|
|
+ ret->fullSize = scaler.getResultDimensions().dimensions();
|
|
|
+ ret->margins = scaler.getResultDimensions().topLeft();
|
|
|
|
|
|
// erase our own reference
|
|
|
SDL_FreeSurface(scaled);
|
|
@@ -359,6 +306,7 @@ std::shared_ptr<const ISharedImage> SDLImageShared::scaleTo(const Point & size,
|
|
|
|
|
|
void SDLImageShared::exportBitmap(const boost::filesystem::path& path, SDL_Palette * palette) const
|
|
|
{
|
|
|
+ assert(upscalingInProgress == false);
|
|
|
if (!surf)
|
|
|
return;
|
|
|
|
|
@@ -369,13 +317,9 @@ void SDLImageShared::exportBitmap(const boost::filesystem::path& path, SDL_Palet
|
|
|
SDL_SetSurfacePalette(surf, originalPalette);
|
|
|
}
|
|
|
|
|
|
-void SDLImageIndexed::playerColored(PlayerColor player)
|
|
|
-{
|
|
|
- graphics->setPlayerPalette(currentPalette, player);
|
|
|
-}
|
|
|
-
|
|
|
bool SDLImageShared::isTransparent(const Point & coords) const
|
|
|
{
|
|
|
+ assert(upscalingInProgress == false);
|
|
|
if (surf)
|
|
|
return CSDL_Ext::isTransparent(surf, coords.x - margins.x, coords.y - margins.y);
|
|
|
else
|
|
@@ -384,31 +328,34 @@ bool SDLImageShared::isTransparent(const Point & coords) const
|
|
|
|
|
|
Rect SDLImageShared::contentRect() const
|
|
|
{
|
|
|
- auto tmpMargins = margins / preScaleFactor;
|
|
|
- auto tmpSize = Point(surf->w, surf->h) / preScaleFactor;
|
|
|
+ assert(upscalingInProgress == false);
|
|
|
+ auto tmpMargins = margins;
|
|
|
+ auto tmpSize = Point(surf->w, surf->h);
|
|
|
return Rect(tmpMargins, tmpSize);
|
|
|
}
|
|
|
|
|
|
-Point SDLImageShared::dimensions() const
|
|
|
+const SDL_Palette * SDLImageShared::getPalette() const
|
|
|
{
|
|
|
- return fullSize / preScaleFactor;
|
|
|
+ assert(upscalingInProgress == false);
|
|
|
+ if (!surf)
|
|
|
+ return nullptr;
|
|
|
+ return surf->format->palette;
|
|
|
}
|
|
|
|
|
|
-std::shared_ptr<IImage> SDLImageShared::createImageReference(EImageBlitMode mode) const
|
|
|
+Point SDLImageShared::dimensions() const
|
|
|
{
|
|
|
- if (surf && surf->format->palette)
|
|
|
- return std::make_shared<SDLImageIndexed>(shared_from_this(), originalPalette, mode);
|
|
|
- else
|
|
|
- return std::make_shared<SDLImageRGB>(shared_from_this(), mode);
|
|
|
+ assert(upscalingInProgress == false);
|
|
|
+ return fullSize;
|
|
|
}
|
|
|
|
|
|
std::shared_ptr<const ISharedImage> SDLImageShared::horizontalFlip() const
|
|
|
{
|
|
|
+ assert(upscalingInProgress == false);
|
|
|
if (!surf)
|
|
|
return shared_from_this();
|
|
|
|
|
|
SDL_Surface * flipped = CSDL_Ext::horizontalFlip(surf);
|
|
|
- auto ret = std::make_shared<SDLImageShared>(flipped, preScaleFactor);
|
|
|
+ auto ret = std::make_shared<SDLImageShared>(flipped);
|
|
|
ret->fullSize = fullSize;
|
|
|
ret->margins.x = margins.x;
|
|
|
ret->margins.y = fullSize.y - surf->h - margins.y;
|
|
@@ -419,11 +366,12 @@ std::shared_ptr<const ISharedImage> SDLImageShared::horizontalFlip() const
|
|
|
|
|
|
std::shared_ptr<const ISharedImage> SDLImageShared::verticalFlip() const
|
|
|
{
|
|
|
+ assert(upscalingInProgress == false);
|
|
|
if (!surf)
|
|
|
return shared_from_this();
|
|
|
|
|
|
SDL_Surface * flipped = CSDL_Ext::verticalFlip(surf);
|
|
|
- auto ret = std::make_shared<SDLImageShared>(flipped, preScaleFactor);
|
|
|
+ auto ret = std::make_shared<SDLImageShared>(flipped);
|
|
|
ret->fullSize = fullSize;
|
|
|
ret->margins.x = fullSize.x - surf->w - margins.x;
|
|
|
ret->margins.y = margins.y;
|
|
@@ -435,6 +383,7 @@ std::shared_ptr<const ISharedImage> SDLImageShared::verticalFlip() const
|
|
|
// Keep the original palette, in order to do color switching operation
|
|
|
void SDLImageShared::savePalette()
|
|
|
{
|
|
|
+ assert(upscalingInProgress == false);
|
|
|
// For some images that don't have palette, skip this
|
|
|
if(surf->format->palette == nullptr)
|
|
|
return;
|
|
@@ -445,219 +394,8 @@ void SDLImageShared::savePalette()
|
|
|
SDL_SetPaletteColors(originalPalette, surf->format->palette->colors, 0, surf->format->palette->ncolors);
|
|
|
}
|
|
|
|
|
|
-void SDLImageIndexed::shiftPalette(uint32_t firstColorID, uint32_t colorsToMove, uint32_t distanceToMove)
|
|
|
-{
|
|
|
- std::vector<SDL_Color> shifterColors(colorsToMove);
|
|
|
-
|
|
|
- for(uint32_t i=0; i<colorsToMove; ++i)
|
|
|
- shifterColors[(i+distanceToMove)%colorsToMove] = originalPalette->colors[firstColorID + i];
|
|
|
-
|
|
|
- SDL_SetPaletteColors(currentPalette, shifterColors.data(), firstColorID, colorsToMove);
|
|
|
-}
|
|
|
-
|
|
|
-void SDLImageIndexed::adjustPalette(const ColorFilter & shifter, uint32_t colorsToSkipMask)
|
|
|
-{
|
|
|
- // If shadow is enabled, following colors must be skipped unconditionally
|
|
|
- if (blitMode == EImageBlitMode::WITH_SHADOW || blitMode == EImageBlitMode::WITH_SHADOW_AND_OVERLAY)
|
|
|
- colorsToSkipMask |= (1 << 0) + (1 << 1) + (1 << 4);
|
|
|
-
|
|
|
- // Note: here we skip first colors in the palette that are predefined in H3 images
|
|
|
- for(int i = 0; i < currentPalette->ncolors; i++)
|
|
|
- {
|
|
|
- if (i < std::size(sourcePalette) && colorsSimilar(sourcePalette[i], originalPalette->colors[i]))
|
|
|
- continue;
|
|
|
-
|
|
|
- if(i < std::numeric_limits<uint32_t>::digits && ((colorsToSkipMask >> i) & 1) == 1)
|
|
|
- continue;
|
|
|
-
|
|
|
- currentPalette->colors[i] = CSDL_Ext::toSDL(shifter.shiftColor(CSDL_Ext::fromSDL(originalPalette->colors[i])));
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-SDLImageIndexed::SDLImageIndexed(const std::shared_ptr<const ISharedImage> & image, SDL_Palette * originalPalette, EImageBlitMode mode)
|
|
|
- :SDLImageBase::SDLImageBase(image, mode)
|
|
|
- ,originalPalette(originalPalette)
|
|
|
-{
|
|
|
- currentPalette = SDL_AllocPalette(originalPalette->ncolors);
|
|
|
- SDL_SetPaletteColors(currentPalette, originalPalette->colors, 0, originalPalette->ncolors);
|
|
|
-
|
|
|
- preparePalette();
|
|
|
-}
|
|
|
-
|
|
|
-SDLImageIndexed::~SDLImageIndexed()
|
|
|
-{
|
|
|
- SDL_FreePalette(currentPalette);
|
|
|
-}
|
|
|
-
|
|
|
-void SDLImageIndexed::setShadowTransparency(float factor)
|
|
|
-{
|
|
|
- ColorRGBA shadow50(0, 0, 0, 128 * factor);
|
|
|
- ColorRGBA shadow25(0, 0, 0, 64 * factor);
|
|
|
-
|
|
|
- std::array<SDL_Color, 5> colorsSDL = {
|
|
|
- originalPalette->colors[0],
|
|
|
- originalPalette->colors[1],
|
|
|
- originalPalette->colors[2],
|
|
|
- originalPalette->colors[3],
|
|
|
- originalPalette->colors[4]
|
|
|
- };
|
|
|
-
|
|
|
- // seems to be used unconditionally
|
|
|
- colorsSDL[0] = CSDL_Ext::toSDL(Colors::TRANSPARENCY);
|
|
|
- colorsSDL[1] = CSDL_Ext::toSDL(shadow25);
|
|
|
- colorsSDL[4] = CSDL_Ext::toSDL(shadow50);
|
|
|
-
|
|
|
- // seems to be used only if color matches
|
|
|
- if (colorsSimilar(originalPalette->colors[2], sourcePalette[2]))
|
|
|
- colorsSDL[2] = CSDL_Ext::toSDL(shadow25);
|
|
|
-
|
|
|
- if (colorsSimilar(originalPalette->colors[3], sourcePalette[3]))
|
|
|
- colorsSDL[3] = CSDL_Ext::toSDL(shadow50);
|
|
|
-
|
|
|
- SDL_SetPaletteColors(currentPalette, colorsSDL.data(), 0, colorsSDL.size());
|
|
|
-}
|
|
|
-
|
|
|
-void SDLImageIndexed::setOverlayColor(const ColorRGBA & color)
|
|
|
-{
|
|
|
- currentPalette->colors[5] = CSDL_Ext::toSDL(addColors(targetPalette[5], color));
|
|
|
-
|
|
|
- for (int i : {6,7})
|
|
|
- {
|
|
|
- if (colorsSimilar(originalPalette->colors[i], sourcePalette[i]))
|
|
|
- currentPalette->colors[i] = CSDL_Ext::toSDL(addColors(targetPalette[i], color));
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-void SDLImageIndexed::preparePalette()
|
|
|
-{
|
|
|
- switch(blitMode)
|
|
|
- {
|
|
|
- case EImageBlitMode::ONLY_SHADOW:
|
|
|
- case EImageBlitMode::ONLY_OVERLAY:
|
|
|
- adjustPalette(ColorFilter::genAlphaShifter(0), 0);
|
|
|
- break;
|
|
|
- }
|
|
|
-
|
|
|
- switch(blitMode)
|
|
|
- {
|
|
|
- case EImageBlitMode::SIMPLE:
|
|
|
- case EImageBlitMode::WITH_SHADOW:
|
|
|
- case EImageBlitMode::ONLY_SHADOW:
|
|
|
- case EImageBlitMode::WITH_SHADOW_AND_OVERLAY:
|
|
|
- setShadowTransparency(1.0);
|
|
|
- break;
|
|
|
- case EImageBlitMode::ONLY_BODY:
|
|
|
- case EImageBlitMode::ONLY_BODY_IGNORE_OVERLAY:
|
|
|
- case EImageBlitMode::ONLY_OVERLAY:
|
|
|
- setShadowTransparency(0.0);
|
|
|
- break;
|
|
|
- }
|
|
|
-
|
|
|
- switch(blitMode)
|
|
|
- {
|
|
|
- case EImageBlitMode::ONLY_OVERLAY:
|
|
|
- case EImageBlitMode::WITH_SHADOW_AND_OVERLAY:
|
|
|
- setOverlayColor(Colors::WHITE_TRUE);
|
|
|
- break;
|
|
|
- case EImageBlitMode::ONLY_SHADOW:
|
|
|
- case EImageBlitMode::ONLY_BODY:
|
|
|
- setOverlayColor(Colors::TRANSPARENCY);
|
|
|
- break;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
SDLImageShared::~SDLImageShared()
|
|
|
{
|
|
|
SDL_FreeSurface(surf);
|
|
|
SDL_FreePalette(originalPalette);
|
|
|
}
|
|
|
-
|
|
|
-SDLImageBase::SDLImageBase(const std::shared_ptr<const ISharedImage> & image, EImageBlitMode mode)
|
|
|
- :image(image)
|
|
|
- , alphaValue(SDL_ALPHA_OPAQUE)
|
|
|
- , blitMode(mode)
|
|
|
-{}
|
|
|
-
|
|
|
-std::shared_ptr<const ISharedImage> SDLImageBase::getSharedImage() const
|
|
|
-{
|
|
|
- return image;
|
|
|
-}
|
|
|
-
|
|
|
-void SDLImageRGB::draw(SDL_Surface * where, const Point & pos, const Rect * src) const
|
|
|
-{
|
|
|
- image->draw(where, nullptr, pos, src, Colors::WHITE_TRUE, alphaValue, blitMode);
|
|
|
-}
|
|
|
-
|
|
|
-void SDLImageIndexed::draw(SDL_Surface * where, const Point & pos, const Rect * src) const
|
|
|
-{
|
|
|
- image->draw(where, currentPalette, pos, src, Colors::WHITE_TRUE, alphaValue, blitMode);
|
|
|
-}
|
|
|
-
|
|
|
-void SDLImageIndexed::exportBitmap(const boost::filesystem::path & path) const
|
|
|
-{
|
|
|
- image->exportBitmap(path, currentPalette);
|
|
|
-}
|
|
|
-
|
|
|
-void SDLImageIndexed::scaleTo(const Point & size)
|
|
|
-{
|
|
|
- image = image->scaleTo(size, currentPalette);
|
|
|
-}
|
|
|
-
|
|
|
-void SDLImageRGB::scaleTo(const Point & size)
|
|
|
-{
|
|
|
- image = image->scaleTo(size, nullptr);
|
|
|
-}
|
|
|
-
|
|
|
-void SDLImageIndexed::scaleInteger(int factor)
|
|
|
-{
|
|
|
- image = image->scaleInteger(factor, currentPalette, blitMode);
|
|
|
-}
|
|
|
-
|
|
|
-void SDLImageRGB::scaleInteger(int factor)
|
|
|
-{
|
|
|
- image = image->scaleInteger(factor, nullptr, blitMode);
|
|
|
-}
|
|
|
-
|
|
|
-void SDLImageRGB::exportBitmap(const boost::filesystem::path & path) const
|
|
|
-{
|
|
|
- image->exportBitmap(path, nullptr);
|
|
|
-}
|
|
|
-
|
|
|
-bool SDLImageBase::isTransparent(const Point & coords) const
|
|
|
-{
|
|
|
- return image->isTransparent(coords);
|
|
|
-}
|
|
|
-
|
|
|
-Rect SDLImageBase::contentRect() const
|
|
|
-{
|
|
|
- return image->contentRect();
|
|
|
-}
|
|
|
-
|
|
|
-Point SDLImageBase::dimensions() const
|
|
|
-{
|
|
|
- return image->dimensions();
|
|
|
-}
|
|
|
-
|
|
|
-void SDLImageBase::setAlpha(uint8_t value)
|
|
|
-{
|
|
|
- alphaValue = value;
|
|
|
-}
|
|
|
-
|
|
|
-void SDLImageBase::setBlitMode(EImageBlitMode mode)
|
|
|
-{
|
|
|
- blitMode = mode;
|
|
|
-}
|
|
|
-
|
|
|
-void SDLImageRGB::setOverlayColor(const ColorRGBA & color)
|
|
|
-{}
|
|
|
-
|
|
|
-void SDLImageRGB::playerColored(PlayerColor player)
|
|
|
-{}
|
|
|
-
|
|
|
-void SDLImageRGB::shiftPalette(uint32_t firstColorID, uint32_t colorsToMove, uint32_t distanceToMove)
|
|
|
-{}
|
|
|
-
|
|
|
-void SDLImageRGB::adjustPalette(const ColorFilter & shifter, uint32_t colorsToSkipMask)
|
|
|
-{}
|
|
|
-
|
|
|
-
|