瀏覽代碼

optimize running the algorithms on smaller image

Laserlicht 3 月之前
父節點
當前提交
be56d6eff4

+ 4 - 4
client/renderSDL/RenderHandler.cpp

@@ -295,7 +295,7 @@ std::shared_ptr<SDLImageShared> RenderHandler::loadScaledImage(const ImageLocato
 	bool generateOverlay = locator.generateOverlay && (*locator.generateOverlay) != SharedImageLocator::OverlayMode::OVERLAY_NONE;
 	bool generateOverlay = locator.generateOverlay && (*locator.generateOverlay) != SharedImageLocator::OverlayMode::OVERLAY_NONE;
 	bool isShadow = locator.layer == EImageBlitMode::ONLY_SHADOW_HIDE_SELECTION || locator.layer == EImageBlitMode::ONLY_SHADOW_HIDE_FLAG_COLOR;
 	bool isShadow = locator.layer == EImageBlitMode::ONLY_SHADOW_HIDE_SELECTION || locator.layer == EImageBlitMode::ONLY_SHADOW_HIDE_FLAG_COLOR;
 	bool isOverlay = locator.layer == EImageBlitMode::ONLY_FLAG_COLOR || locator.layer == EImageBlitMode::ONLY_SELECTION;
 	bool isOverlay = locator.layer == EImageBlitMode::ONLY_FLAG_COLOR || locator.layer == EImageBlitMode::ONLY_SELECTION;
-	bool optimizeImage = !(isShadow && generateShadow) && !(isOverlay && generateOverlay); // images needs to expanded
+	int requiredBorderAfterOptimize = (isShadow && generateShadow) || (isOverlay && generateOverlay) ? 50 * locator.scalingFactor : 0;
 
 
 	if(isOverlay && !generateOverlay)
 	if(isOverlay && !generateOverlay)
 		imagePathString += "-OVERLAY";
 		imagePathString += "-OVERLAY";
@@ -313,11 +313,11 @@ std::shared_ptr<SDLImageShared> RenderHandler::loadScaledImage(const ImageLocato
 	std::shared_ptr<SDLImageShared> img = nullptr;
 	std::shared_ptr<SDLImageShared> img = nullptr;
 
 
 	if(CResourceHandler::get()->existsResource(imagePathSprites))
 	if(CResourceHandler::get()->existsResource(imagePathSprites))
-		img = std::make_shared<SDLImageShared>(imagePathSprites, optimizeImage);
+		img = std::make_shared<SDLImageShared>(imagePathSprites, requiredBorderAfterOptimize);
 	else if(CResourceHandler::get()->existsResource(imagePathData))
 	else if(CResourceHandler::get()->existsResource(imagePathData))
-		img = std::make_shared<SDLImageShared>(imagePathData, optimizeImage);
+		img = std::make_shared<SDLImageShared>(imagePathData, requiredBorderAfterOptimize);
 	else if(CResourceHandler::get()->existsResource(imagePath))
 	else if(CResourceHandler::get()->existsResource(imagePath))
-		img = std::make_shared<SDLImageShared>(imagePath, optimizeImage);
+		img = std::make_shared<SDLImageShared>(imagePath, requiredBorderAfterOptimize);
 
 
 	if(img)
 	if(img)
 	{
 	{

+ 4 - 5
client/renderSDL/SDLImage.cpp

@@ -70,7 +70,7 @@ SDLImageShared::SDLImageShared(SDL_Surface * from)
 	fullSize.y = surf->h;
 	fullSize.y = surf->h;
 }
 }
 
 
-SDLImageShared::SDLImageShared(const ImagePath & filename, bool optimizeImage)
+SDLImageShared::SDLImageShared(const ImagePath & filename, int keepBorder)
 	: surf(nullptr),
 	: surf(nullptr),
 	margins(0, 0),
 	margins(0, 0),
 	fullSize(0, 0),
 	fullSize(0, 0),
@@ -89,8 +89,7 @@ SDLImageShared::SDLImageShared(const ImagePath & filename, bool optimizeImage)
 		fullSize.x = surf->w;
 		fullSize.x = surf->w;
 		fullSize.y = surf->h;
 		fullSize.y = surf->h;
 
 
-		if(optimizeImage)
-			optimizeSurface();
+		optimizeSurface(keepBorder);
 	}
 	}
 }
 }
 
 
@@ -210,7 +209,7 @@ void SDLImageShared::draw(SDL_Surface * where, SDL_Palette * palette, const Poin
 		SDL_SetSurfacePalette(surf, originalPalette);
 		SDL_SetSurfacePalette(surf, originalPalette);
 }
 }
 
 
-void SDLImageShared::optimizeSurface()
+void SDLImageShared::optimizeSurface(int keepBorder)
 {
 {
 	assert(upscalingInProgress == false);
 	assert(upscalingInProgress == false);
 	if (!surf)
 	if (!surf)
@@ -218,7 +217,7 @@ void SDLImageShared::optimizeSurface()
 
 
 	SDLImageOptimizer optimizer(surf, Rect(margins, fullSize));
 	SDLImageOptimizer optimizer(surf, Rect(margins, fullSize));
 
 
-	optimizer.optimizeSurface(surf);
+	optimizer.optimizeSurface(surf, keepBorder);
 	SDL_FreeSurface(surf);
 	SDL_FreeSurface(surf);
 
 
 	surf = optimizer.acquireResultSurface();
 	surf = optimizer.acquireResultSurface();

+ 2 - 2
client/renderSDL/SDLImage.h

@@ -40,13 +40,13 @@ class SDLImageShared final : public ISharedImage, public std::enable_shared_from
 	// Keep the original palette, in order to do color switching operation
 	// Keep the original palette, in order to do color switching operation
 	void savePalette();
 	void savePalette();
 
 
-	void optimizeSurface();
+	void optimizeSurface(int keepBorder = 0);
 
 
 public:
 public:
 	//Load image from def file
 	//Load image from def file
 	SDLImageShared(const CDefFile *data, size_t frame, size_t group=0);
 	SDLImageShared(const CDefFile *data, size_t frame, size_t group=0);
 	//Load from bitmap file
 	//Load from bitmap file
-	SDLImageShared(const ImagePath & filename, bool optimizeImage=true);
+	SDLImageShared(const ImagePath & filename, int keepBorder = 0);
 	//Create using existing surface, extraRef will increase refcount on SDL_Surface
 	//Create using existing surface, extraRef will increase refcount on SDL_Surface
 	SDLImageShared(SDL_Surface * from);
 	SDLImageShared(SDL_Surface * from);
 	~SDLImageShared();
 	~SDLImageShared();

+ 6 - 1
client/renderSDL/SDLImageScaler.cpp

@@ -24,7 +24,7 @@ SDLImageOptimizer::SDLImageOptimizer(SDL_Surface * surf, const Rect & virtualDim
 {
 {
 }
 }
 
 
-void SDLImageOptimizer::optimizeSurface(SDL_Surface * formatSourceSurface)
+void SDLImageOptimizer::optimizeSurface(SDL_Surface * formatSourceSurface, int keepBorder)
 {
 {
 	if (!surf)
 	if (!surf)
 		return;
 		return;
@@ -79,6 +79,11 @@ void SDLImageOptimizer::optimizeSurface(SDL_Surface * formatSourceSurface)
 	if (left == surf->w)
 	if (left == surf->w)
 		return;
 		return;
 
 
+	left = std::max(0, left - keepBorder);
+	top = std::max(0, top - keepBorder);
+	right = std::min(surf->w - 1, right + keepBorder);
+	bottom = std::min(surf->h - 1, bottom + keepBorder);
+
 	if (left != 0 || top != 0 || right != surf->w - 1 || bottom != surf->h - 1)
 	if (left != 0 || top != 0 || right != surf->w - 1 || bottom != surf->h - 1)
 	{
 	{
 		// non-zero border found
 		// non-zero border found

+ 1 - 1
client/renderSDL/SDLImageScaler.h

@@ -21,7 +21,7 @@ class SDLImageOptimizer : boost::noncopyable
 public:
 public:
 	SDLImageOptimizer(SDL_Surface * surf, const Rect & virtualDimensions);
 	SDLImageOptimizer(SDL_Surface * surf, const Rect & virtualDimensions);
 
 
-	void optimizeSurface(SDL_Surface * formatSourceSurface);
+	void optimizeSurface(SDL_Surface * formatSourceSurface, int keepBorder = 0);
 
 
 	/// Aquires resulting surface and transfers surface ownership to the caller
 	/// Aquires resulting surface and transfers surface ownership to the caller
 	/// May return nullptr if input image was empty
 	/// May return nullptr if input image was empty

+ 3 - 6
client/renderSDL/SDL_Extensions.cpp

@@ -777,6 +777,9 @@ void applyAffineTransform(SDL_Surface* src, SDL_Surface* dst, double a, double b
 	double ic = -c * invDet;
 	double ic = -c * invDet;
 	double id =  a * invDet;
 	double id =  a * invDet;
 
 
+	auto srcPixels = (Uint32*)src->pixels;
+	auto dstPixels = (Uint32*)dst->pixels;
+
 	tbb::parallel_for(tbb::blocked_range<size_t>(0, dst->h), [&](const tbb::blocked_range<size_t>& r)
 	tbb::parallel_for(tbb::blocked_range<size_t>(0, dst->h), [&](const tbb::blocked_range<size_t>& r)
 	{
 	{
 		// For each pixel in the destination image
 		// For each pixel in the destination image
@@ -795,18 +798,12 @@ void applyAffineTransform(SDL_Surface* src, SDL_Surface* dst, double a, double b
 				// Check bounds
 				// Check bounds
 				if (srcXi >= 0 && srcXi < src->w && srcYi >= 0 && srcYi < src->h)
 				if (srcXi >= 0 && srcXi < src->w && srcYi >= 0 && srcYi < src->h)
 				{
 				{
-					auto srcPixels = (Uint32*)src->pixels;
-					auto dstPixels = (Uint32*)dst->pixels;
 
 
 					Uint32 pixel = srcPixels[srcYi * src->w + srcXi];
 					Uint32 pixel = srcPixels[srcYi * src->w + srcXi];
 					dstPixels[y * dst->w + x] = pixel;
 					dstPixels[y * dst->w + x] = pixel;
 				}
 				}
 				else
 				else
-				{
-					// Outside source bounds: set transparent or black
-					auto dstPixels = (Uint32*)dst->pixels;
 					dstPixels[y * dst->w + x] = 0x00000000;  // transparent black
 					dstPixels[y * dst->w + x] = 0x00000000;  // transparent black
-				}
 			}
 			}
 		}
 		}
 	});
 	});