123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383 |
- /*
- * 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"
- #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 <SDL_image.h>
- class SDLImageLoader;
- int IImage::width() const
- {
- return dimensions().x;
- }
- int IImage::height() const
- {
- return dimensions().y;
- }
- SDLImageShared::SDLImageShared(const CDefFile * data, size_t frame, size_t group)
- : surf(nullptr),
- margins(0, 0),
- fullSize(0, 0),
- originalPalette(nullptr)
- {
- SDLImageLoader loader(this);
- data->loadFrame(frame, group, loader);
- savePalette();
- }
- SDLImageShared::SDLImageShared(SDL_Surface * from)
- : surf(nullptr),
- margins(0, 0),
- fullSize(0, 0),
- originalPalette(nullptr)
- {
- surf = from;
- if (surf == nullptr)
- return;
- savePalette();
- surf->refcount++;
- fullSize.x = surf->w;
- fullSize.y = surf->h;
- }
- SDLImageShared::SDLImageShared(const ImagePath & filename)
- : surf(nullptr),
- margins(0, 0),
- fullSize(0, 0),
- originalPalette(nullptr)
- {
- 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;
- optimizeSurface();
- }
- }
- void SDLImageShared::draw(SDL_Surface * where, SDL_Palette * palette, const Point & dest, const Rect * src, const ColorRGBA & colorMultiplier, uint8_t alpha, EImageBlitMode mode) const
- {
- 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;
- 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);
- if(surf->format->palette && mode != EImageBlitMode::OPAQUE && mode != EImageBlitMode::COLORKEY)
- {
- CSDL_Ext::blit8bppAlphaTo24bpp(surf, sourceRect, where, destShift, alpha);
- }
- else
- {
- CSDL_Ext::blitSurface(surf, sourceRect, where, destShift);
- }
- if (surf->format->palette)
- SDL_SetSurfacePalette(surf, originalPalette);
- }
- 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)
- {
- 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;
- margins.x += left;
- margins.y += top;
- }
- }
- std::shared_ptr<const ISharedImage> SDLImageShared::scaleInteger(int factor, SDL_Palette * palette, EImageBlitMode mode) const
- {
- if (factor <= 0)
- throw std::runtime_error("Unable to scale by integer value of " + std::to_string(factor));
- if (!surf)
- return shared_from_this();
- if (palette && surf->format->palette)
- SDL_SetSurfacePalette(surf, palette);
- SDL_Surface * scaled = nullptr;
- // 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);
- auto ret = std::make_shared<SDLImageShared>(scaled);
- ret->fullSize.x = fullSize.x * factor;
- ret->fullSize.y = fullSize.y * factor;
- ret->margins.x = (int) round((float)margins.x * factor);
- ret->margins.y = (int) round((float)margins.y * factor);
- ret->optimizeSurface();
- // erase our own reference
- SDL_FreeSurface(scaled);
- if (surf->format->palette)
- SDL_SetSurfacePalette(surf, originalPalette);
- return ret;
- }
- std::shared_ptr<const ISharedImage> SDLImageShared::scaleTo(const Point & size, SDL_Palette * palette) const
- {
- float scaleX = static_cast<float>(size.x) / fullSize.x;
- float scaleY = static_cast<float>(size.y) / fullSize.y;
- if (palette && surf->format->palette)
- SDL_SetSurfacePalette(surf, palette);
- auto scaled = CSDL_Ext::scaleSurface(surf, (int)(surf->w * scaleX), (int)(surf->h * scaleY));
- 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
- auto ret = std::make_shared<SDLImageShared>(scaled);
- 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);
- if (surf->format->palette)
- SDL_SetSurfacePalette(surf, originalPalette);
- return ret;
- }
- void SDLImageShared::exportBitmap(const boost::filesystem::path& path, SDL_Palette * palette) const
- {
- if (!surf)
- return;
- 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);
- }
- bool SDLImageShared::isTransparent(const Point & coords) const
- {
- if (surf)
- return CSDL_Ext::isTransparent(surf, coords.x - margins.x, coords.y - margins.y);
- else
- return true;
- }
- Rect SDLImageShared::contentRect() const
- {
- auto tmpMargins = margins;
- auto tmpSize = Point(surf->w, surf->h);
- return Rect(tmpMargins, tmpSize);
- }
- const SDL_Palette * SDLImageShared::getPalette() const
- {
- if (!surf)
- return nullptr;
- return surf->format->palette;
- }
- Point SDLImageShared::dimensions() const
- {
- return fullSize;
- }
- std::shared_ptr<const ISharedImage> SDLImageShared::horizontalFlip() const
- {
- if (!surf)
- return shared_from_this();
- SDL_Surface * flipped = CSDL_Ext::horizontalFlip(surf);
- auto ret = std::make_shared<SDLImageShared>(flipped);
- ret->fullSize = fullSize;
- ret->margins.x = margins.x;
- ret->margins.y = fullSize.y - surf->h - margins.y;
- ret->fullSize = fullSize;
- return ret;
- }
- std::shared_ptr<const ISharedImage> SDLImageShared::verticalFlip() const
- {
- if (!surf)
- return shared_from_this();
- SDL_Surface * flipped = CSDL_Ext::verticalFlip(surf);
- auto ret = std::make_shared<SDLImageShared>(flipped);
- ret->fullSize = fullSize;
- ret->margins.x = fullSize.x - surf->w - margins.x;
- ret->margins.y = margins.y;
- ret->fullSize = fullSize;
- return ret;
- }
- // Keep the original palette, in order to do color switching operation
- void SDLImageShared::savePalette()
- {
- // For some images that don't have palette, skip this
- if(surf->format->palette == nullptr)
- return;
- if(originalPalette == nullptr)
- originalPalette = SDL_AllocPalette(surf->format->palette->ncolors);
- SDL_SetPaletteColors(originalPalette, surf->format->palette->colors, 0, surf->format->palette->ncolors);
- }
- SDLImageShared::~SDLImageShared()
- {
- SDL_FreeSurface(surf);
- SDL_FreePalette(originalPalette);
- }
|