File: SDLImageScaler.cpp

package info (click to toggle)
vcmi 1.7.2%2Bdfsg-1
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 43,732 kB
  • sloc: cpp: 275,851; ansic: 734; sh: 235; xml: 163; objc: 61; makefile: 50; python: 41
file content (255 lines) | stat: -rw-r--r-- 7,329 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
 * SDLImageScaler.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 "SDLImageScaler.h"

#include "SDL_Extensions.h"

#include "../GameEngine.h"
#include "../xBRZ/xbrz.h"

#include <tbb/parallel_for.h>
#include <SDL_surface.h>

SDLImageOptimizer::SDLImageOptimizer(SDL_Surface * surf, const Rect & virtualDimensions)
	: surf(surf)
	, virtualDimensions(virtualDimensions)
{
}

void SDLImageOptimizer::optimizeSurface(SDL_Surface * formatSourceSurface)
{
	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);
				}
			}
		}
	}

	// empty image
	if (left == surf->w)
		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(), formatSourceSurface);
		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);
		}
		output = newSurface;

		virtualDimensions.x += left;
		virtualDimensions.y += top;
	}
	else
	{
		output = surf;
		output->refcount += 1;
	}
}

SDL_Surface * SDLImageOptimizer::acquireResultSurface()
{
	SDL_Surface * result = output;
	output = nullptr;
	return result;
}

const Rect & SDLImageOptimizer::getResultDimensions() const
{
	return virtualDimensions;
}

void SDLImageScaler::scaleSurface(Point targetDimensions, EScalingAlgorithm algorithm)
{
	if (!intermediate)
		return; // may happen on scaling of empty images

	if(!targetDimensions.x || !targetDimensions.y)
		throw std::runtime_error("invalid scaling dimensions!");

	Point inputSurfaceSize(intermediate->w, intermediate->h);
	Point outputSurfaceSize = targetDimensions * inputSurfaceSize / virtualDimensionsInput.dimensions();
	Point outputMargins = targetDimensions * virtualDimensionsInput.topLeft() / virtualDimensionsInput.dimensions();

	// TODO: use xBRZ if possible? E.g. when scaling to 150% do 100% -> 200% via xBRZ and then linear downscale 200% -> 150%?
	// Need to investigate which is optimal	for performance and for visuals
	ret = CSDL_Ext::newSurface(Point(outputSurfaceSize.x, outputSurfaceSize.y), intermediate);

	virtualDimensionsOutput = Rect(outputMargins, targetDimensions); // TODO: account for input virtual size

	const uint32_t * srcPixels = static_cast<const uint32_t*>(intermediate->pixels);
	uint32_t * dstPixels = static_cast<uint32_t*>(ret->pixels);

	if (algorithm == EScalingAlgorithm::NEAREST)
		xbrz::nearestNeighborScale(srcPixels, intermediate->w, intermediate->h, dstPixels, ret->w, ret->h);
	else
		xbrz::bilinearScale(srcPixels, intermediate->w, intermediate->h, dstPixels, ret->w, ret->h);
}

void SDLImageScaler::scaleSurfaceIntegerFactor(int factor, EScalingAlgorithm algorithm)
{
	if (!intermediate)
		return; // may happen on scaling of empty images

	if(factor == 0)
		throw std::runtime_error("invalid scaling factor!");

	int newWidth = intermediate->w * factor;
	int newHight = intermediate->h * factor;

	virtualDimensionsOutput = virtualDimensionsInput * factor;

	ret = CSDL_Ext::newSurface(Point(newWidth, newHight), intermediate);

	assert(intermediate->pitch == intermediate->w * 4);
	assert(ret->pitch == ret->w * 4);

	const uint32_t * srcPixels = static_cast<const uint32_t*>(intermediate->pixels);
	uint32_t * dstPixels = static_cast<uint32_t*>(ret->pixels);

	switch (algorithm)
	{
		case EScalingAlgorithm::NEAREST:
			xbrz::nearestNeighborScale(srcPixels, intermediate->w, intermediate->h, dstPixels, ret->w, ret->h);
			break;
		case EScalingAlgorithm::BILINEAR:
			xbrz::bilinearScale(srcPixels, intermediate->w, intermediate->h, dstPixels, ret->w, ret->h);
			break;
		case EScalingAlgorithm::XBRZ_ALPHA:
		case EScalingAlgorithm::XBRZ_OPAQUE:
		{
			auto format = algorithm == EScalingAlgorithm::XBRZ_OPAQUE ? xbrz::ColorFormat::ARGB_CLAMPED : xbrz::ColorFormat::ARGB;

			if(intermediate->h < 32)
			{
				// for tiny images tbb incurs too high overhead
				xbrz::scale(factor, srcPixels, dstPixels, intermediate->w, intermediate->h, format, {});
			}
			else
			{
				// xbrz recommends granulation of 16, but according to tests, for smaller images granulation of 4 is actually the best option
				const int granulation = intermediate->h > 400 ? 16 : 4;
				tbb::parallel_for(tbb::blocked_range<size_t>(0, intermediate->h, granulation), [this, factor, srcPixels, dstPixels, format](const tbb::blocked_range<size_t> & r)
								  {
									  xbrz::scale(factor, srcPixels, dstPixels, intermediate->w, intermediate->h, format, {}, r.begin(), r.end());
								  });
			}

			break;
		}
		default:
			throw std::runtime_error("invalid scaling algorithm!");
	}
}

SDLImageScaler::SDLImageScaler(SDL_Surface * surf)
	:SDLImageScaler(surf, Rect(0,0,surf->w, surf->h), false)
{
}

SDLImageScaler::SDLImageScaler(SDL_Surface * surf, const Rect & virtualDimensions, bool optimizeImage)
{
	if (optimizeImage)
	{
		SDLImageOptimizer optimizer(surf, virtualDimensions);
		optimizer.optimizeSurface(nullptr);
		intermediate = optimizer.acquireResultSurface();
		virtualDimensionsInput = optimizer.getResultDimensions();
	}
	else
	{
		intermediate = surf;
		intermediate->refcount += 1;
		virtualDimensionsInput = virtualDimensions;
	}

	if (intermediate == surf)
	{
		SDL_FreeSurface(intermediate);
		intermediate = SDL_ConvertSurfaceFormat(surf, SDL_PIXELFORMAT_ARGB8888, 0);
	}

	if (intermediate == surf)
		throw std::runtime_error("Scaler uses same surface as input!");
}

SDLImageScaler::~SDLImageScaler()
{
	ENGINE->dispatchMainThread([surface = intermediate]()
	{
		// SDL_FreeSurface must be executed in main thread to avoid thread races to its internal state
		// will be no longer necessary in SDL 3
		SDL_FreeSurface(surface);
	});
}

SDL_Surface * SDLImageScaler::acquireResultSurface()
{
	SDL_Surface * result = ret;
	ret = nullptr;
	return result;
}

const Rect & SDLImageScaler::getResultDimensions() const
{
	return virtualDimensionsOutput;
}