1 // ****************************************************************************
2 // * This file is part of the xBRZ project. It is distributed under *
3 // * GNU General Public License: https://www.gnu.org/licenses/gpl-3.0 *
4 // * Copyright (C) Zenju (zenju AT gmx DOT de) - All Rights Reserved *
5 // * *
6 // * Additionally and as a special exception, the author gives permission *
7 // * to link the code of this program with the following libraries *
8 // * (or with modified versions that use the same licenses), and distribute *
9 // * linked combinations including the two: MAME, FreeFileSync, Snes9x *
10 // * You must obey the GNU General Public License in all respects for all of *
11 // * the code used other than MAME, FreeFileSync, Snes9x. *
12 // * If you modify this file, you may extend this exception to your version *
13 // * of the file, but you are not obligated to do so. If you do not wish to *
14 // * do so, delete this exception statement from your version. *
15 // ****************************************************************************
16
17 #include "xbrz.h"
18 #include <cassert>
19 #include <vector>
20 #include <algorithm>
21 #include <cmath> //std::sqrt
22 #include "xbrz_tools.h"
23
24 using namespace xbrz;
25
26
27 namespace
28 {
29 template <unsigned int M, unsigned int N> inline
gradientRGB(uint32_t pixFront,uint32_t pixBack)30 uint32_t gradientRGB(uint32_t pixFront, uint32_t pixBack) //blend front color with opacity M / N over opaque background: http://en.wikipedia.org/wiki/Alpha_compositing#Alpha_blending
31 {
32 static_assert(0 < M && M < N && N <= 1000, "");
33
34 auto calcColor = [](unsigned char colFront, unsigned char colBack) -> unsigned char { return (colFront * M + colBack * (N - M)) / N; };
35
36 return makePixel(calcColor(getRed (pixFront), getRed (pixBack)),
37 calcColor(getGreen(pixFront), getGreen(pixBack)),
38 calcColor(getBlue (pixFront), getBlue (pixBack)));
39 }
40
41
42 template <unsigned int M, unsigned int N> inline
gradientARGB(uint32_t pixFront,uint32_t pixBack)43 uint32_t gradientARGB(uint32_t pixFront, uint32_t pixBack) //find intermediate color between two colors with alpha channels (=> NO alpha blending!!!)
44 {
45 static_assert(0 < M && M < N && N <= 1000, "");
46
47 const unsigned int weightFront = getAlpha(pixFront) * M;
48 const unsigned int weightBack = getAlpha(pixBack) * (N - M);
49 const unsigned int weightSum = weightFront + weightBack;
50 if (weightSum == 0)
51 return 0;
52
53 auto calcColor = [=](unsigned char colFront, unsigned char colBack)
54 {
55 return static_cast<unsigned char>((colFront * weightFront + colBack * weightBack) / weightSum);
56 };
57
58 return makePixel(static_cast<unsigned char>(weightSum / N),
59 calcColor(getRed (pixFront), getRed (pixBack)),
60 calcColor(getGreen(pixFront), getGreen(pixBack)),
61 calcColor(getBlue (pixFront), getBlue (pixBack)));
62 }
63
64
65 //inline
66 //double fastSqrt(double n)
67 //{
68 // __asm //speeds up xBRZ by about 9% compared to std::sqrt which internally uses the same assembler instructions but adds some "fluff"
69 // {
70 // fld n
71 // fsqrt
72 // }
73 //}
74 //
75
76
77 enum RotationDegree //clock-wise
78 {
79 ROT_0,
80 ROT_90,
81 ROT_180,
82 ROT_270
83 };
84
85 //calculate input matrix coordinates after rotation at compile time
86 template <RotationDegree rotDeg, size_t I, size_t J, size_t N>
87 struct MatrixRotation;
88
89 template <size_t I, size_t J, size_t N>
90 struct MatrixRotation<ROT_0, I, J, N>
91 {
92 static const size_t I_old = I;
93 static const size_t J_old = J;
94 };
95
96 template <RotationDegree rotDeg, size_t I, size_t J, size_t N> //(i, j) = (row, col) indices, N = size of (square) matrix
97 struct MatrixRotation
98 {
99 static const size_t I_old = N - 1 - MatrixRotation<static_cast<RotationDegree>(rotDeg - 1), I, J, N>::J_old; //old coordinates before rotation!
100 static const size_t J_old = MatrixRotation<static_cast<RotationDegree>(rotDeg - 1), I, J, N>::I_old; //
101 };
102
103
104 template <size_t N, RotationDegree rotDeg>
105 class OutputMatrix
106 {
107 public:
OutputMatrix(uint32_t * out,int outWidth)108 OutputMatrix(uint32_t* out, int outWidth) : //access matrix area, top-left at position "out" for image with given width
109 out_(out),
110 outWidth_(outWidth) {}
111
112 template <size_t I, size_t J>
ref() const113 uint32_t& ref() const
114 {
115 static const size_t I_old = MatrixRotation<rotDeg, I, J, N>::I_old;
116 static const size_t J_old = MatrixRotation<rotDeg, I, J, N>::J_old;
117 return *(out_ + J_old + I_old * outWidth_);
118 }
119
120 private:
121 uint32_t* out_;
122 const int outWidth_;
123 };
124
125
126 template <class T> inline
square(T value)127 T square(T value) { return value * value; }
128
129
130 #if 0
131 inline
132 double distRGB(uint32_t pix1, uint32_t pix2)
133 {
134 const double r_diff = static_cast<int>(getRed (pix1)) - getRed (pix2);
135 const double g_diff = static_cast<int>(getGreen(pix1)) - getGreen(pix2);
136 const double b_diff = static_cast<int>(getBlue (pix1)) - getBlue (pix2);
137
138 //euklidean RGB distance
139 return std::sqrt(square(r_diff) + square(g_diff) + square(b_diff));
140 }
141 #endif
142
143
144 inline
distYCbCr(uint32_t pix1,uint32_t pix2,double lumaWeight)145 double distYCbCr(uint32_t pix1, uint32_t pix2, double lumaWeight)
146 {
147 //http://en.wikipedia.org/wiki/YCbCr#ITU-R_BT.601_conversion
148 //YCbCr conversion is a matrix multiplication => take advantage of linearity by subtracting first!
149 const int r_diff = static_cast<int>(getRed (pix1)) - getRed (pix2); //we may delay division by 255 to after matrix multiplication
150 const int g_diff = static_cast<int>(getGreen(pix1)) - getGreen(pix2); //
151 const int b_diff = static_cast<int>(getBlue (pix1)) - getBlue (pix2); //substraction for int is noticeable faster than for double!
152
153 //const double k_b = 0.0722; //ITU-R BT.709 conversion
154 //const double k_r = 0.2126; //
155 const double k_b = 0.0593; //ITU-R BT.2020 conversion
156 const double k_r = 0.2627; //
157 const double k_g = 1 - k_b - k_r;
158
159 const double scale_b = 0.5 / (1 - k_b);
160 const double scale_r = 0.5 / (1 - k_r);
161
162 const double y = k_r * r_diff + k_g * g_diff + k_b * b_diff; //[!], analog YCbCr!
163 const double c_b = scale_b * (b_diff - y);
164 const double c_r = scale_r * (r_diff - y);
165
166 //we skip division by 255 to have similar range like other distance functions
167 return std::sqrt(square(lumaWeight * y) + square(c_b) + square(c_r));
168 }
169
170
171 inline
distYCbCrBuffered(uint32_t pix1,uint32_t pix2)172 double distYCbCrBuffered(uint32_t pix1, uint32_t pix2)
173 {
174 //30% perf boost compared to plain distYCbCr()!
175 //consumes 64 MB memory; using double is only 2% faster, but takes 128 MB
176 static const std::vector<float> diffToDist = []
177 {
178 std::vector<float> tmp;
179
180 for (uint32_t i = 0; i < 256 * 256 * 256; ++i) //startup time: 114 ms on Intel Core i5 (four cores)
181 {
182 const int r_diff = getByte<2>(i) * 2 - 0xFF;
183 const int g_diff = getByte<1>(i) * 2 - 0xFF;
184 const int b_diff = getByte<0>(i) * 2 - 0xFF;
185
186 const double k_b = 0.0593; //ITU-R BT.2020 conversion
187 const double k_r = 0.2627; //
188 const double k_g = 1 - k_b - k_r;
189
190 const double scale_b = 0.5 / (1 - k_b);
191 const double scale_r = 0.5 / (1 - k_r);
192
193 const double y = k_r * r_diff + k_g * g_diff + k_b * b_diff; //[!], analog YCbCr!
194 const double c_b = scale_b * (b_diff - y);
195 const double c_r = scale_r * (r_diff - y);
196
197 tmp.push_back(static_cast<float>(std::sqrt(square(y) + square(c_b) + square(c_r))));
198 }
199 return tmp;
200 }();
201
202 //if (pix1 == pix2) -> 8% perf degradation!
203 // return 0;
204 //if (pix1 < pix2)
205 // std::swap(pix1, pix2); -> 30% perf degradation!!!
206 #if 1
207 const int r_diff = static_cast<int>(getRed (pix1)) - getRed (pix2);
208 const int g_diff = static_cast<int>(getGreen(pix1)) - getGreen(pix2);
209 const int b_diff = static_cast<int>(getBlue (pix1)) - getBlue (pix2);
210
211 return diffToDist[(((r_diff + 0xFF) / 2) << 16) | //slightly reduce precision (division by 2) to squeeze value into single byte
212 (((g_diff + 0xFF) / 2) << 8) |
213 (( b_diff + 0xFF) / 2)];
214 #else //not noticeably faster:
215 const int r_diff_tmp = ((pix1 & 0xFF0000) + 0xFF0000 - (pix2 & 0xFF0000)) / 2;
216 const int g_diff_tmp = ((pix1 & 0x00FF00) + 0x00FF00 - (pix2 & 0x00FF00)) / 2; //slightly reduce precision (division by 2) to squeeze value into single byte
217 const int b_diff_tmp = ((pix1 & 0x0000FF) + 0x0000FF - (pix2 & 0x0000FF)) / 2;
218
219 return diffToDist[(r_diff_tmp & 0xFF0000) | (g_diff_tmp & 0x00FF00) | (b_diff_tmp & 0x0000FF)];
220 #endif
221 }
222
223
224 enum BlendType
225 {
226 BLEND_NONE = 0,
227 BLEND_NORMAL, //a normal indication to blend
228 BLEND_DOMINANT, //a strong indication to blend
229 //attention: BlendType must fit into the value range of 2 bit!!!
230 };
231
232 struct BlendResult
233 {
234 BlendType
235 /**/blend_f, blend_g,
236 /**/blend_j, blend_k;
237 };
238
239
240 struct Kernel_4x4 //kernel for preprocessing step
241 {
242 uint32_t
243 /**/a, b, c, d,
244 /**/e, f, g, h,
245 /**/i, j, k, l,
246 /**/m, n, o, p;
247 };
248
249 /*
250 input kernel area naming convention:
251 -----------------
252 | A | B | C | D |
253 ----|---|---|---|
254 | E | F | G | H | //evaluate the four corners between F, G, J, K
255 ----|---|---|---| //input pixel is at position F
256 | I | J | K | L |
257 ----|---|---|---|
258 | M | N | O | P |
259 -----------------
260 */
261 template <class ColorDistance>
262 alwaysinline //detect blend direction
preProcessCorners(const Kernel_4x4 & ker,const xbrz::ScalerCfg & cfg)263 BlendResult preProcessCorners(const Kernel_4x4& ker, const xbrz::ScalerCfg& cfg) //result: F, G, J, K corners of "GradientType"
264 {
265 BlendResult result = {};
266
267 if ((ker.f == ker.g &&
268 ker.j == ker.k) ||
269 (ker.f == ker.j &&
270 ker.g == ker.k))
271 return result;
272
273 auto dist = [&](uint32_t pix1, uint32_t pix2) { return ColorDistance::dist(pix1, pix2, cfg.luminanceWeight); };
274
275 const int weight = 4;
276 double jg = dist(ker.i, ker.f) + dist(ker.f, ker.c) + dist(ker.n, ker.k) + dist(ker.k, ker.h) + weight * dist(ker.j, ker.g);
277 double fk = dist(ker.e, ker.j) + dist(ker.j, ker.o) + dist(ker.b, ker.g) + dist(ker.g, ker.l) + weight * dist(ker.f, ker.k);
278
279 if (jg < fk) //test sample: 70% of values max(jg, fk) / min(jg, fk) are between 1.1 and 3.7 with median being 1.8
280 {
281 const bool dominantGradient = cfg.dominantDirectionThreshold * jg < fk;
282 if (ker.f != ker.g && ker.f != ker.j)
283 result.blend_f = dominantGradient ? BLEND_DOMINANT : BLEND_NORMAL;
284
285 if (ker.k != ker.j && ker.k != ker.g)
286 result.blend_k = dominantGradient ? BLEND_DOMINANT : BLEND_NORMAL;
287 }
288 else if (fk < jg)
289 {
290 const bool dominantGradient = cfg.dominantDirectionThreshold * fk < jg;
291 if (ker.j != ker.f && ker.j != ker.k)
292 result.blend_j = dominantGradient ? BLEND_DOMINANT : BLEND_NORMAL;
293
294 if (ker.g != ker.f && ker.g != ker.k)
295 result.blend_g = dominantGradient ? BLEND_DOMINANT : BLEND_NORMAL;
296 }
297 return result;
298 }
299
300 struct Kernel_3x3
301 {
302 uint32_t
303 /**/a, b, c,
304 /**/d, e, f,
305 /**/g, h, i;
306 };
307
308 #define DEF_GETTER(x) template <RotationDegree rotDeg> uint32_t inline get_##x(const Kernel_3x3& ker) { return ker.x; }
309 //we cannot and NEED NOT write "ker.##x" since ## concatenates preprocessor tokens but "." is not a token
DEF_GETTER(b)310 DEF_GETTER(a) DEF_GETTER(b) DEF_GETTER(c)
311 DEF_GETTER(d) DEF_GETTER(e) DEF_GETTER(f)
312 DEF_GETTER(g) DEF_GETTER(h) DEF_GETTER(i)
313 #undef DEF_GETTER
314
315 #define DEF_GETTER(x, y) template <> inline uint32_t get_##x<ROT_90>(const Kernel_3x3& ker) { return ker.y; }
316 DEF_GETTER(b, d) DEF_GETTER(c, a)
317 DEF_GETTER(d, h) DEF_GETTER(e, e) DEF_GETTER(f, b)
318 DEF_GETTER(g, i) DEF_GETTER(h, f) DEF_GETTER(i, c)
319 #undef DEF_GETTER
320
321 #define DEF_GETTER(x, y) template <> inline uint32_t get_##x<ROT_180>(const Kernel_3x3& ker) { return ker.y; }
322 DEF_GETTER(b, h) DEF_GETTER(c, g)
323 DEF_GETTER(d, f) DEF_GETTER(e, e) DEF_GETTER(f, d)
324 DEF_GETTER(g, c) DEF_GETTER(h, b) DEF_GETTER(i, a)
325 #undef DEF_GETTER
326
327 #define DEF_GETTER(x, y) template <> inline uint32_t get_##x<ROT_270>(const Kernel_3x3& ker) { return ker.y; }
328 DEF_GETTER(b, f) DEF_GETTER(c, i)
329 DEF_GETTER(d, b) DEF_GETTER(e, e) DEF_GETTER(f, h)
330 DEF_GETTER(g, a) DEF_GETTER(h, d) DEF_GETTER(i, g)
331 #undef DEF_GETTER
332
333
334 //compress four blend types into a single byte
335 //inline BlendType getTopL (unsigned char b) { return static_cast<BlendType>(0x3 & b); }
336 inline BlendType getTopR (unsigned char b) { return static_cast<BlendType>(0x3 & (b >> 2)); }
getBottomR(unsigned char b)337 inline BlendType getBottomR(unsigned char b) { return static_cast<BlendType>(0x3 & (b >> 4)); }
getBottomL(unsigned char b)338 inline BlendType getBottomL(unsigned char b) { return static_cast<BlendType>(0x3 & (b >> 6)); }
339
setTopL(unsigned char & b,BlendType bt)340 inline void setTopL (unsigned char& b, BlendType bt) { b |= bt; } //buffer is assumed to be initialized before preprocessing!
setTopR(unsigned char & b,BlendType bt)341 inline void setTopR (unsigned char& b, BlendType bt) { b |= (bt << 2); }
setBottomR(unsigned char & b,BlendType bt)342 inline void setBottomR(unsigned char& b, BlendType bt) { b |= (bt << 4); }
setBottomL(unsigned char & b,BlendType bt)343 inline void setBottomL(unsigned char& b, BlendType bt) { b |= (bt << 6); }
344
blendingNeeded(unsigned char b)345 inline bool blendingNeeded(unsigned char b) { return b != 0; }
346
347 template <RotationDegree rotDeg> inline
rotateBlendInfo(unsigned char b)348 unsigned char rotateBlendInfo(unsigned char b) { return b; }
rotateBlendInfo(unsigned char b)349 template <> inline unsigned char rotateBlendInfo<ROT_90 >(unsigned char b) { return ((b << 2) | (b >> 6)) & 0xff; }
rotateBlendInfo(unsigned char b)350 template <> inline unsigned char rotateBlendInfo<ROT_180>(unsigned char b) { return ((b << 4) | (b >> 4)) & 0xff; }
rotateBlendInfo(unsigned char b)351 template <> inline unsigned char rotateBlendInfo<ROT_270>(unsigned char b) { return ((b << 6) | (b >> 2)) & 0xff; }
352
353 #ifdef WIN32
354 #ifndef NDEBUG
355 int debugPixelX = -1;
356 int debugPixelY = 12;
357 __declspec(thread) bool breakIntoDebugger = false;
358 #endif
359 #endif
360
361 /*
362 input kernel area naming convention:
363 -------------
364 | A | B | C |
365 ----|---|---|
366 | D | E | F | //input pixel is at position E
367 ----|---|---|
368 | G | H | I |
369 -------------
370 */
371 template <class Scaler, class ColorDistance, RotationDegree rotDeg>
372 alwaysinline //perf: quite worth it!
blendPixel(const Kernel_3x3 & ker,uint32_t * target,int trgWidth,unsigned char blendInfo,const xbrz::ScalerCfg & cfg)373 void blendPixel(const Kernel_3x3& ker,
374 uint32_t* target, int trgWidth,
375 unsigned char blendInfo, //result of preprocessing all four corners of pixel "e"
376 const xbrz::ScalerCfg& cfg)
377 {
378 #define a get_a<rotDeg>(ker)
379 #define b get_b<rotDeg>(ker)
380 #define c get_c<rotDeg>(ker)
381 #define d get_d<rotDeg>(ker)
382 #define e get_e<rotDeg>(ker)
383 #define f get_f<rotDeg>(ker)
384 #define g get_g<rotDeg>(ker)
385 #define h get_h<rotDeg>(ker)
386 #define i get_i<rotDeg>(ker)
387
388 #ifdef WIN32
389 #ifndef NDEBUG
390 if (breakIntoDebugger)
391 __debugbreak(); //__asm int 3;
392 #endif
393 #endif
394
395 const unsigned char blend = rotateBlendInfo<rotDeg>(blendInfo);
396
397 if (getBottomR(blend) >= BLEND_NORMAL)
398 {
399 auto eq = [&](uint32_t pix1, uint32_t pix2) { return ColorDistance::dist(pix1, pix2, cfg.luminanceWeight) < cfg.equalColorTolerance; };
400 auto dist = [&](uint32_t pix1, uint32_t pix2) { return ColorDistance::dist(pix1, pix2, cfg.luminanceWeight); };
401
402 const bool doLineBlend = [&]() -> bool
403 {
404 if (getBottomR(blend) >= BLEND_DOMINANT)
405 return true;
406
407 //make sure there is no second blending in an adjacent rotation for this pixel: handles insular pixels, mario eyes
408 if (getTopR(blend) != BLEND_NONE && !eq(e, g)) //but support double-blending for 90 degree corners
409 return false;
410 if (getBottomL(blend) != BLEND_NONE && !eq(e, c))
411 return false;
412
413 //no full blending for L-shapes; blend corner only (handles "mario mushroom eyes")
414 if (!eq(e, i) && eq(g, h) && eq(h, i) && eq(i, f) && eq(f, c))
415 return false;
416
417 return true;
418 }();
419
420 const uint32_t px = dist(e, f) <= dist(e, h) ? f : h; //choose most similar color
421
422 OutputMatrix<Scaler::scale, rotDeg> out(target, trgWidth);
423
424 if (doLineBlend)
425 {
426 const double fg = dist(f, g); //test sample: 70% of values max(fg, hc) / min(fg, hc) are between 1.1 and 3.7 with median being 1.9
427 const double hc = dist(h, c); //
428
429 const bool haveShallowLine = cfg.steepDirectionThreshold * fg <= hc && e != g && d != g;
430 const bool haveSteepLine = cfg.steepDirectionThreshold * hc <= fg && e != c && b != c;
431
432 if (haveShallowLine)
433 {
434 if (haveSteepLine)
435 Scaler::blendLineSteepAndShallow(px, out);
436 else
437 Scaler::blendLineShallow(px, out);
438 }
439 else
440 {
441 if (haveSteepLine)
442 Scaler::blendLineSteep(px, out);
443 else
444 Scaler::blendLineDiagonal(px, out);
445 }
446 }
447 else
448 Scaler::blendCorner(px, out);
449 }
450
451 #undef a
452 #undef b
453 #undef c
454 #undef d
455 #undef e
456 #undef f
457 #undef g
458 #undef h
459 #undef i
460 }
461
462
463 template <class Scaler, class ColorDistance> //scaler policy: see "Scaler2x" reference implementation
scaleImage(const uint32_t * src,uint32_t * trg,int srcWidth,int srcHeight,const xbrz::ScalerCfg & cfg,int yFirst,int yLast)464 void scaleImage(const uint32_t* src, uint32_t* trg, int srcWidth, int srcHeight, const xbrz::ScalerCfg& cfg, int yFirst, int yLast)
465 {
466 yFirst = std::max(yFirst, 0);
467 yLast = std::min(yLast, srcHeight);
468 if (yFirst >= yLast || srcWidth <= 0)
469 return;
470
471 const int trgWidth = srcWidth * Scaler::scale;
472
473 //"use" space at the end of the image as temporary buffer for "on the fly preprocessing": we even could use larger area of
474 //"sizeof(uint32_t) * srcWidth * (yLast - yFirst)" bytes without risk of accidental overwriting before accessing
475 const int bufferSize = srcWidth;
476 unsigned char* preProcBuffer = reinterpret_cast<unsigned char*>(trg + yLast * Scaler::scale * trgWidth) - bufferSize;
477 std::fill(preProcBuffer, preProcBuffer + bufferSize, '\0');
478 static_assert(BLEND_NONE == 0, "");
479
480 //initialize preprocessing buffer for first row of current stripe: detect upper left and right corner blending
481 //this cannot be optimized for adjacent processing stripes; we must not allow for a memory race condition!
482 if (yFirst > 0)
483 {
484 const int y = yFirst - 1;
485
486 const uint32_t* s_m1 = src + srcWidth * std::max(y - 1, 0);
487 const uint32_t* s_0 = src + srcWidth * y; //center line
488 const uint32_t* s_p1 = src + srcWidth * std::min(y + 1, srcHeight - 1);
489 const uint32_t* s_p2 = src + srcWidth * std::min(y + 2, srcHeight - 1);
490
491 for (int x = 0; x < srcWidth; ++x)
492 {
493 const int x_m1 = std::max(x - 1, 0);
494 const int x_p1 = std::min(x + 1, srcWidth - 1);
495 const int x_p2 = std::min(x + 2, srcWidth - 1);
496
497 Kernel_4x4 ker = {}; //perf: initialization is negligible
498 ker.a = s_m1[x_m1]; //read sequentially from memory as far as possible
499 ker.b = s_m1[x];
500 ker.c = s_m1[x_p1];
501 ker.d = s_m1[x_p2];
502
503 ker.e = s_0[x_m1];
504 ker.f = s_0[x];
505 ker.g = s_0[x_p1];
506 ker.h = s_0[x_p2];
507
508 ker.i = s_p1[x_m1];
509 ker.j = s_p1[x];
510 ker.k = s_p1[x_p1];
511 ker.l = s_p1[x_p2];
512
513 ker.m = s_p2[x_m1];
514 ker.n = s_p2[x];
515 ker.o = s_p2[x_p1];
516 ker.p = s_p2[x_p2];
517
518 const BlendResult res = preProcessCorners<ColorDistance>(ker, cfg);
519 /*
520 preprocessing blend result:
521 ---------
522 | F | G | //evalute corner between F, G, J, K
523 ----|---| //input pixel is at position F
524 | J | K |
525 ---------
526 */
527 setTopR(preProcBuffer[x], res.blend_j);
528
529 if (x + 1 < bufferSize)
530 setTopL(preProcBuffer[x + 1], res.blend_k);
531 }
532 }
533 //------------------------------------------------------------------------------------
534
535 for (int y = yFirst; y < yLast; ++y)
536 {
537 uint32_t* out = trg + Scaler::scale * y * trgWidth; //consider MT "striped" access
538
539 const uint32_t* s_m1 = src + srcWidth * std::max(y - 1, 0);
540 const uint32_t* s_0 = src + srcWidth * y; //center line
541 const uint32_t* s_p1 = src + srcWidth * std::min(y + 1, srcHeight - 1);
542 const uint32_t* s_p2 = src + srcWidth * std::min(y + 2, srcHeight - 1);
543
544 unsigned char blend_xy1 = 0; //corner blending for current (x, y + 1) position
545
546 for (int x = 0; x < srcWidth; ++x, out += Scaler::scale)
547 {
548 #ifdef WIN32
549 #ifndef NDEBUG
550 breakIntoDebugger = debugPixelX == x && debugPixelY == y;
551 #endif
552 #endif
553 //all those bounds checks have only insignificant impact on performance!
554 const int x_m1 = std::max(x - 1, 0); //perf: prefer array indexing to additional pointers!
555 const int x_p1 = std::min(x + 1, srcWidth - 1);
556 const int x_p2 = std::min(x + 2, srcWidth - 1);
557
558 Kernel_4x4 ker4 = {}; //perf: initialization is negligible
559
560 ker4.a = s_m1[x_m1]; //read sequentially from memory as far as possible
561 ker4.b = s_m1[x];
562 ker4.c = s_m1[x_p1];
563 ker4.d = s_m1[x_p2];
564
565 ker4.e = s_0[x_m1];
566 ker4.f = s_0[x];
567 ker4.g = s_0[x_p1];
568 ker4.h = s_0[x_p2];
569
570 ker4.i = s_p1[x_m1];
571 ker4.j = s_p1[x];
572 ker4.k = s_p1[x_p1];
573 ker4.l = s_p1[x_p2];
574
575 ker4.m = s_p2[x_m1];
576 ker4.n = s_p2[x];
577 ker4.o = s_p2[x_p1];
578 ker4.p = s_p2[x_p2];
579
580 //evaluate the four corners on bottom-right of current pixel
581 unsigned char blend_xy = 0; //for current (x, y) position
582 {
583 const BlendResult res = preProcessCorners<ColorDistance>(ker4, cfg);
584 /*
585 preprocessing blend result:
586 ---------
587 | F | G | //evalute corner between F, G, J, K
588 ----|---| //current input pixel is at position F
589 | J | K |
590 ---------
591 */
592 blend_xy = preProcBuffer[x];
593 setBottomR(blend_xy, res.blend_f); //all four corners of (x, y) have been determined at this point due to processing sequence!
594
595 setTopR(blend_xy1, res.blend_j); //set 2nd known corner for (x, y + 1)
596 preProcBuffer[x] = blend_xy1; //store on current buffer position for use on next row
597
598 blend_xy1 = 0;
599 setTopL(blend_xy1, res.blend_k); //set 1st known corner for (x + 1, y + 1) and buffer for use on next column
600
601 if (x + 1 < bufferSize) //set 3rd known corner for (x + 1, y)
602 setBottomL(preProcBuffer[x + 1], res.blend_g);
603 }
604
605 //fill block of size scale * scale with the given color
606 fillBlock(out, trgWidth * sizeof(uint32_t), ker4.f, Scaler::scale, Scaler::scale);
607 //place *after* preprocessing step, to not overwrite the results while processing the the last pixel!
608
609 //blend four corners of current pixel
610 if (blendingNeeded(blend_xy)) //good 5% perf-improvement
611 {
612 Kernel_3x3 ker3 = {}; //perf: initialization is negligible
613
614 ker3.a = ker4.a;
615 ker3.b = ker4.b;
616 ker3.c = ker4.c;
617
618 ker3.d = ker4.e;
619 ker3.e = ker4.f;
620 ker3.f = ker4.g;
621
622 ker3.g = ker4.i;
623 ker3.h = ker4.j;
624 ker3.i = ker4.k;
625
626 blendPixel<Scaler, ColorDistance, ROT_0 >(ker3, out, trgWidth, blend_xy, cfg);
627 blendPixel<Scaler, ColorDistance, ROT_90 >(ker3, out, trgWidth, blend_xy, cfg);
628 blendPixel<Scaler, ColorDistance, ROT_180>(ker3, out, trgWidth, blend_xy, cfg);
629 blendPixel<Scaler, ColorDistance, ROT_270>(ker3, out, trgWidth, blend_xy, cfg);
630 }
631 }
632 }
633 }
634
635 //------------------------------------------------------------------------------------
636
637 template <class ColorGradient>
638 struct Scaler2x : public ColorGradient
639 {
640 static const int scale = 2;
641
642 template <unsigned int M, unsigned int N> //bring template function into scope for GCC
alphaGrad__anon56ded9ec0111::Scaler2x643 static void alphaGrad(uint32_t& pixBack, uint32_t pixFront) { ColorGradient::template alphaGrad<M, N>(pixBack, pixFront); }
644
645
646 template <class OutputMatrix>
blendLineShallow__anon56ded9ec0111::Scaler2x647 static void blendLineShallow(uint32_t col, OutputMatrix& out)
648 {
649 alphaGrad<1, 4>(out.template ref<scale - 1, 0>(), col);
650 alphaGrad<3, 4>(out.template ref<scale - 1, 1>(), col);
651 }
652
653 template <class OutputMatrix>
blendLineSteep__anon56ded9ec0111::Scaler2x654 static void blendLineSteep(uint32_t col, OutputMatrix& out)
655 {
656 alphaGrad<1, 4>(out.template ref<0, scale - 1>(), col);
657 alphaGrad<3, 4>(out.template ref<1, scale - 1>(), col);
658 }
659
660 template <class OutputMatrix>
blendLineSteepAndShallow__anon56ded9ec0111::Scaler2x661 static void blendLineSteepAndShallow(uint32_t col, OutputMatrix& out)
662 {
663 alphaGrad<1, 4>(out.template ref<1, 0>(), col);
664 alphaGrad<1, 4>(out.template ref<0, 1>(), col);
665 alphaGrad<5, 6>(out.template ref<1, 1>(), col); //[!] fixes 7/8 used in xBR
666 }
667
668 template <class OutputMatrix>
blendLineDiagonal__anon56ded9ec0111::Scaler2x669 static void blendLineDiagonal(uint32_t col, OutputMatrix& out)
670 {
671 alphaGrad<1, 2>(out.template ref<1, 1>(), col);
672 }
673
674 template <class OutputMatrix>
blendCorner__anon56ded9ec0111::Scaler2x675 static void blendCorner(uint32_t col, OutputMatrix& out)
676 {
677 //model a round corner
678 alphaGrad<21, 100>(out.template ref<1, 1>(), col); //exact: 1 - pi/4 = 0.2146018366
679 }
680 };
681
682
683 template <class ColorGradient>
684 struct Scaler3x : public ColorGradient
685 {
686 static const int scale = 3;
687
688 template <unsigned int M, unsigned int N> //bring template function into scope for GCC
alphaGrad__anon56ded9ec0111::Scaler3x689 static void alphaGrad(uint32_t& pixBack, uint32_t pixFront) { ColorGradient::template alphaGrad<M, N>(pixBack, pixFront); }
690
691
692 template <class OutputMatrix>
blendLineShallow__anon56ded9ec0111::Scaler3x693 static void blendLineShallow(uint32_t col, OutputMatrix& out)
694 {
695 alphaGrad<1, 4>(out.template ref<scale - 1, 0>(), col);
696 alphaGrad<1, 4>(out.template ref<scale - 2, 2>(), col);
697
698 alphaGrad<3, 4>(out.template ref<scale - 1, 1>(), col);
699 out.template ref<scale - 1, 2>() = col;
700 }
701
702 template <class OutputMatrix>
blendLineSteep__anon56ded9ec0111::Scaler3x703 static void blendLineSteep(uint32_t col, OutputMatrix& out)
704 {
705 alphaGrad<1, 4>(out.template ref<0, scale - 1>(), col);
706 alphaGrad<1, 4>(out.template ref<2, scale - 2>(), col);
707
708 alphaGrad<3, 4>(out.template ref<1, scale - 1>(), col);
709 out.template ref<2, scale - 1>() = col;
710 }
711
712 template <class OutputMatrix>
blendLineSteepAndShallow__anon56ded9ec0111::Scaler3x713 static void blendLineSteepAndShallow(uint32_t col, OutputMatrix& out)
714 {
715 alphaGrad<1, 4>(out.template ref<2, 0>(), col);
716 alphaGrad<1, 4>(out.template ref<0, 2>(), col);
717 alphaGrad<3, 4>(out.template ref<2, 1>(), col);
718 alphaGrad<3, 4>(out.template ref<1, 2>(), col);
719 out.template ref<2, 2>() = col;
720 }
721
722 template <class OutputMatrix>
blendLineDiagonal__anon56ded9ec0111::Scaler3x723 static void blendLineDiagonal(uint32_t col, OutputMatrix& out)
724 {
725 alphaGrad<1, 8>(out.template ref<1, 2>(), col); //conflict with other rotations for this odd scale
726 alphaGrad<1, 8>(out.template ref<2, 1>(), col);
727 alphaGrad<7, 8>(out.template ref<2, 2>(), col); //
728 }
729
730 template <class OutputMatrix>
blendCorner__anon56ded9ec0111::Scaler3x731 static void blendCorner(uint32_t col, OutputMatrix& out)
732 {
733 //model a round corner
734 alphaGrad<45, 100>(out.template ref<2, 2>(), col); //exact: 0.4545939598
735 //alphaGrad<7, 256>(out.template ref<2, 1>(), col); //0.02826017254 -> negligible + avoid conflicts with other rotations for this odd scale
736 //alphaGrad<7, 256>(out.template ref<1, 2>(), col); //0.02826017254
737 }
738 };
739
740
741 template <class ColorGradient>
742 struct Scaler4x : public ColorGradient
743 {
744 static const int scale = 4;
745
746 template <unsigned int M, unsigned int N> //bring template function into scope for GCC
alphaGrad__anon56ded9ec0111::Scaler4x747 static void alphaGrad(uint32_t& pixBack, uint32_t pixFront) { ColorGradient::template alphaGrad<M, N>(pixBack, pixFront); }
748
749
750 template <class OutputMatrix>
blendLineShallow__anon56ded9ec0111::Scaler4x751 static void blendLineShallow(uint32_t col, OutputMatrix& out)
752 {
753 alphaGrad<1, 4>(out.template ref<scale - 1, 0>(), col);
754 alphaGrad<1, 4>(out.template ref<scale - 2, 2>(), col);
755
756 alphaGrad<3, 4>(out.template ref<scale - 1, 1>(), col);
757 alphaGrad<3, 4>(out.template ref<scale - 2, 3>(), col);
758
759 out.template ref<scale - 1, 2>() = col;
760 out.template ref<scale - 1, 3>() = col;
761 }
762
763 template <class OutputMatrix>
blendLineSteep__anon56ded9ec0111::Scaler4x764 static void blendLineSteep(uint32_t col, OutputMatrix& out)
765 {
766 alphaGrad<1, 4>(out.template ref<0, scale - 1>(), col);
767 alphaGrad<1, 4>(out.template ref<2, scale - 2>(), col);
768
769 alphaGrad<3, 4>(out.template ref<1, scale - 1>(), col);
770 alphaGrad<3, 4>(out.template ref<3, scale - 2>(), col);
771
772 out.template ref<2, scale - 1>() = col;
773 out.template ref<3, scale - 1>() = col;
774 }
775
776 template <class OutputMatrix>
blendLineSteepAndShallow__anon56ded9ec0111::Scaler4x777 static void blendLineSteepAndShallow(uint32_t col, OutputMatrix& out)
778 {
779 alphaGrad<3, 4>(out.template ref<3, 1>(), col);
780 alphaGrad<3, 4>(out.template ref<1, 3>(), col);
781 alphaGrad<1, 4>(out.template ref<3, 0>(), col);
782 alphaGrad<1, 4>(out.template ref<0, 3>(), col);
783
784 alphaGrad<1, 3>(out.template ref<2, 2>(), col); //[!] fixes 1/4 used in xBR
785
786 out.template ref<3, 3>() = col;
787 out.template ref<3, 2>() = col;
788 out.template ref<2, 3>() = col;
789 }
790
791 template <class OutputMatrix>
blendLineDiagonal__anon56ded9ec0111::Scaler4x792 static void blendLineDiagonal(uint32_t col, OutputMatrix& out)
793 {
794 alphaGrad<1, 2>(out.template ref<scale - 1, scale / 2 >(), col);
795 alphaGrad<1, 2>(out.template ref<scale - 2, scale / 2 + 1>(), col);
796 out.template ref<scale - 1, scale - 1>() = col;
797 }
798
799 template <class OutputMatrix>
blendCorner__anon56ded9ec0111::Scaler4x800 static void blendCorner(uint32_t col, OutputMatrix& out)
801 {
802 //model a round corner
803 alphaGrad<68, 100>(out.template ref<3, 3>(), col); //exact: 0.6848532563
804 alphaGrad< 9, 100>(out.template ref<3, 2>(), col); //0.08677704501
805 alphaGrad< 9, 100>(out.template ref<2, 3>(), col); //0.08677704501
806 }
807 };
808
809
810 template <class ColorGradient>
811 struct Scaler5x : public ColorGradient
812 {
813 static const int scale = 5;
814
815 template <unsigned int M, unsigned int N> //bring template function into scope for GCC
alphaGrad__anon56ded9ec0111::Scaler5x816 static void alphaGrad(uint32_t& pixBack, uint32_t pixFront) { ColorGradient::template alphaGrad<M, N>(pixBack, pixFront); }
817
818
819 template <class OutputMatrix>
blendLineShallow__anon56ded9ec0111::Scaler5x820 static void blendLineShallow(uint32_t col, OutputMatrix& out)
821 {
822 alphaGrad<1, 4>(out.template ref<scale - 1, 0>(), col);
823 alphaGrad<1, 4>(out.template ref<scale - 2, 2>(), col);
824 alphaGrad<1, 4>(out.template ref<scale - 3, 4>(), col);
825
826 alphaGrad<3, 4>(out.template ref<scale - 1, 1>(), col);
827 alphaGrad<3, 4>(out.template ref<scale - 2, 3>(), col);
828
829 out.template ref<scale - 1, 2>() = col;
830 out.template ref<scale - 1, 3>() = col;
831 out.template ref<scale - 1, 4>() = col;
832 out.template ref<scale - 2, 4>() = col;
833 }
834
835 template <class OutputMatrix>
blendLineSteep__anon56ded9ec0111::Scaler5x836 static void blendLineSteep(uint32_t col, OutputMatrix& out)
837 {
838 alphaGrad<1, 4>(out.template ref<0, scale - 1>(), col);
839 alphaGrad<1, 4>(out.template ref<2, scale - 2>(), col);
840 alphaGrad<1, 4>(out.template ref<4, scale - 3>(), col);
841
842 alphaGrad<3, 4>(out.template ref<1, scale - 1>(), col);
843 alphaGrad<3, 4>(out.template ref<3, scale - 2>(), col);
844
845 out.template ref<2, scale - 1>() = col;
846 out.template ref<3, scale - 1>() = col;
847 out.template ref<4, scale - 1>() = col;
848 out.template ref<4, scale - 2>() = col;
849 }
850
851 template <class OutputMatrix>
blendLineSteepAndShallow__anon56ded9ec0111::Scaler5x852 static void blendLineSteepAndShallow(uint32_t col, OutputMatrix& out)
853 {
854 alphaGrad<1, 4>(out.template ref<0, scale - 1>(), col);
855 alphaGrad<1, 4>(out.template ref<2, scale - 2>(), col);
856 alphaGrad<3, 4>(out.template ref<1, scale - 1>(), col);
857
858 alphaGrad<1, 4>(out.template ref<scale - 1, 0>(), col);
859 alphaGrad<1, 4>(out.template ref<scale - 2, 2>(), col);
860 alphaGrad<3, 4>(out.template ref<scale - 1, 1>(), col);
861
862 alphaGrad<2, 3>(out.template ref<3, 3>(), col);
863
864 out.template ref<2, scale - 1>() = col;
865 out.template ref<3, scale - 1>() = col;
866 out.template ref<4, scale - 1>() = col;
867
868 out.template ref<scale - 1, 2>() = col;
869 out.template ref<scale - 1, 3>() = col;
870 }
871
872 template <class OutputMatrix>
blendLineDiagonal__anon56ded9ec0111::Scaler5x873 static void blendLineDiagonal(uint32_t col, OutputMatrix& out)
874 {
875 alphaGrad<1, 8>(out.template ref<scale - 1, scale / 2 >(), col); //conflict with other rotations for this odd scale
876 alphaGrad<1, 8>(out.template ref<scale - 2, scale / 2 + 1>(), col);
877 alphaGrad<1, 8>(out.template ref<scale - 3, scale / 2 + 2>(), col); //
878
879 alphaGrad<7, 8>(out.template ref<4, 3>(), col);
880 alphaGrad<7, 8>(out.template ref<3, 4>(), col);
881
882 out.template ref<4, 4>() = col;
883 }
884
885 template <class OutputMatrix>
blendCorner__anon56ded9ec0111::Scaler5x886 static void blendCorner(uint32_t col, OutputMatrix& out)
887 {
888 //model a round corner
889 alphaGrad<86, 100>(out.template ref<4, 4>(), col); //exact: 0.8631434088
890 alphaGrad<23, 100>(out.template ref<4, 3>(), col); //0.2306749731
891 alphaGrad<23, 100>(out.template ref<3, 4>(), col); //0.2306749731
892 //alphaGrad<1, 64>(out.template ref<4, 2>(), col); //0.01676812367 -> negligible + avoid conflicts with other rotations for this odd scale
893 //alphaGrad<1, 64>(out.template ref<2, 4>(), col); //0.01676812367
894 }
895 };
896
897
898 template <class ColorGradient>
899 struct Scaler6x : public ColorGradient
900 {
901 static const int scale = 6;
902
903 template <unsigned int M, unsigned int N> //bring template function into scope for GCC
alphaGrad__anon56ded9ec0111::Scaler6x904 static void alphaGrad(uint32_t& pixBack, uint32_t pixFront) { ColorGradient::template alphaGrad<M, N>(pixBack, pixFront); }
905
906
907 template <class OutputMatrix>
blendLineShallow__anon56ded9ec0111::Scaler6x908 static void blendLineShallow(uint32_t col, OutputMatrix& out)
909 {
910 alphaGrad<1, 4>(out.template ref<scale - 1, 0>(), col);
911 alphaGrad<1, 4>(out.template ref<scale - 2, 2>(), col);
912 alphaGrad<1, 4>(out.template ref<scale - 3, 4>(), col);
913
914 alphaGrad<3, 4>(out.template ref<scale - 1, 1>(), col);
915 alphaGrad<3, 4>(out.template ref<scale - 2, 3>(), col);
916 alphaGrad<3, 4>(out.template ref<scale - 3, 5>(), col);
917
918 out.template ref<scale - 1, 2>() = col;
919 out.template ref<scale - 1, 3>() = col;
920 out.template ref<scale - 1, 4>() = col;
921 out.template ref<scale - 1, 5>() = col;
922
923 out.template ref<scale - 2, 4>() = col;
924 out.template ref<scale - 2, 5>() = col;
925 }
926
927 template <class OutputMatrix>
blendLineSteep__anon56ded9ec0111::Scaler6x928 static void blendLineSteep(uint32_t col, OutputMatrix& out)
929 {
930 alphaGrad<1, 4>(out.template ref<0, scale - 1>(), col);
931 alphaGrad<1, 4>(out.template ref<2, scale - 2>(), col);
932 alphaGrad<1, 4>(out.template ref<4, scale - 3>(), col);
933
934 alphaGrad<3, 4>(out.template ref<1, scale - 1>(), col);
935 alphaGrad<3, 4>(out.template ref<3, scale - 2>(), col);
936 alphaGrad<3, 4>(out.template ref<5, scale - 3>(), col);
937
938 out.template ref<2, scale - 1>() = col;
939 out.template ref<3, scale - 1>() = col;
940 out.template ref<4, scale - 1>() = col;
941 out.template ref<5, scale - 1>() = col;
942
943 out.template ref<4, scale - 2>() = col;
944 out.template ref<5, scale - 2>() = col;
945 }
946
947 template <class OutputMatrix>
blendLineSteepAndShallow__anon56ded9ec0111::Scaler6x948 static void blendLineSteepAndShallow(uint32_t col, OutputMatrix& out)
949 {
950 alphaGrad<1, 4>(out.template ref<0, scale - 1>(), col);
951 alphaGrad<1, 4>(out.template ref<2, scale - 2>(), col);
952 alphaGrad<3, 4>(out.template ref<1, scale - 1>(), col);
953 alphaGrad<3, 4>(out.template ref<3, scale - 2>(), col);
954
955 alphaGrad<1, 4>(out.template ref<scale - 1, 0>(), col);
956 alphaGrad<1, 4>(out.template ref<scale - 2, 2>(), col);
957 alphaGrad<3, 4>(out.template ref<scale - 1, 1>(), col);
958 alphaGrad<3, 4>(out.template ref<scale - 2, 3>(), col);
959
960 out.template ref<2, scale - 1>() = col;
961 out.template ref<3, scale - 1>() = col;
962 out.template ref<4, scale - 1>() = col;
963 out.template ref<5, scale - 1>() = col;
964
965 out.template ref<4, scale - 2>() = col;
966 out.template ref<5, scale - 2>() = col;
967
968 out.template ref<scale - 1, 2>() = col;
969 out.template ref<scale - 1, 3>() = col;
970 }
971
972 template <class OutputMatrix>
blendLineDiagonal__anon56ded9ec0111::Scaler6x973 static void blendLineDiagonal(uint32_t col, OutputMatrix& out)
974 {
975 alphaGrad<1, 2>(out.template ref<scale - 1, scale / 2 >(), col);
976 alphaGrad<1, 2>(out.template ref<scale - 2, scale / 2 + 1>(), col);
977 alphaGrad<1, 2>(out.template ref<scale - 3, scale / 2 + 2>(), col);
978
979 out.template ref<scale - 2, scale - 1>() = col;
980 out.template ref<scale - 1, scale - 1>() = col;
981 out.template ref<scale - 1, scale - 2>() = col;
982 }
983
984 template <class OutputMatrix>
blendCorner__anon56ded9ec0111::Scaler6x985 static void blendCorner(uint32_t col, OutputMatrix& out)
986 {
987 //model a round corner
988 alphaGrad<97, 100>(out.template ref<5, 5>(), col); //exact: 0.9711013910
989 alphaGrad<42, 100>(out.template ref<4, 5>(), col); //0.4236372243
990 alphaGrad<42, 100>(out.template ref<5, 4>(), col); //0.4236372243
991 alphaGrad< 6, 100>(out.template ref<5, 3>(), col); //0.05652034508
992 alphaGrad< 6, 100>(out.template ref<3, 5>(), col); //0.05652034508
993 }
994 };
995
996 //------------------------------------------------------------------------------------
997
998 struct ColorDistanceRGB
999 {
dist__anon56ded9ec0111::ColorDistanceRGB1000 static double dist(uint32_t pix1, uint32_t pix2, double luminanceWeight)
1001 {
1002 return distYCbCrBuffered(pix1, pix2);
1003
1004 //if (pix1 == pix2) //about 4% perf boost
1005 // return 0;
1006 //return distYCbCr(pix1, pix2, luminanceWeight);
1007 }
1008 };
1009
1010 struct ColorDistanceARGB
1011 {
dist__anon56ded9ec0111::ColorDistanceARGB1012 static double dist(uint32_t pix1, uint32_t pix2, double luminanceWeight)
1013 {
1014 const double a1 = getAlpha(pix1) / 255.0 ;
1015 const double a2 = getAlpha(pix2) / 255.0 ;
1016 /*
1017 Requirements for a color distance handling alpha channel: with a1, a2 in [0, 1]
1018
1019 1. if a1 = a2, distance should be: a1 * distYCbCr()
1020 2. if a1 = 0, distance should be: a2 * distYCbCr(black, white) = a2 * 255
1021 3. if a1 = 1, ??? maybe: 255 * (1 - a2) + a2 * distYCbCr()
1022 */
1023
1024 //return std::min(a1, a2) * distYCbCrBuffered(pix1, pix2) + 255 * abs(a1 - a2);
1025 //=> following code is 15% faster:
1026 const double d = distYCbCrBuffered(pix1, pix2);
1027 if (a1 < a2)
1028 return a1 * d + 255 * (a2 - a1);
1029 else
1030 return a2 * d + 255 * (a1 - a2);
1031
1032 //alternative? return std::sqrt(a1 * a2 * square(distYCbCrBuffered(pix1, pix2)) + square(255 * (a1 - a2)));
1033 }
1034 };
1035
1036
1037 struct ColorDistanceUnbufferedARGB
1038 {
dist__anon56ded9ec0111::ColorDistanceUnbufferedARGB1039 static double dist(uint32_t pix1, uint32_t pix2, double luminanceWeight)
1040 {
1041 const double a1 = getAlpha(pix1) / 255.0 ;
1042 const double a2 = getAlpha(pix2) / 255.0 ;
1043
1044 const double d = distYCbCr(pix1, pix2, luminanceWeight);
1045 if (a1 < a2)
1046 return a1 * d + 255 * (a2 - a1);
1047 else
1048 return a2 * d + 255 * (a1 - a2);
1049 }
1050 };
1051
1052
1053 struct ColorGradientRGB
1054 {
1055 template <unsigned int M, unsigned int N>
alphaGrad__anon56ded9ec0111::ColorGradientRGB1056 static void alphaGrad(uint32_t& pixBack, uint32_t pixFront)
1057 {
1058 pixBack = gradientRGB<M, N>(pixFront, pixBack);
1059 }
1060 };
1061
1062 struct ColorGradientARGB
1063 {
1064 template <unsigned int M, unsigned int N>
alphaGrad__anon56ded9ec0111::ColorGradientARGB1065 static void alphaGrad(uint32_t& pixBack, uint32_t pixFront)
1066 {
1067 pixBack = gradientARGB<M, N>(pixFront, pixBack);
1068 }
1069 };
1070 }
1071
1072
scale(size_t factor,const uint32_t * src,uint32_t * trg,int srcWidth,int srcHeight,ColorFormat colFmt,const xbrz::ScalerCfg & cfg,int yFirst,int yLast)1073 void xbrz::scale(size_t factor, const uint32_t* src, uint32_t* trg, int srcWidth, int srcHeight, ColorFormat colFmt, const xbrz::ScalerCfg& cfg, int yFirst, int yLast)
1074 {
1075 static_assert(SCALE_FACTOR_MAX == 6, "");
1076 switch (colFmt)
1077 {
1078 case ColorFormat::RGB:
1079 switch (factor)
1080 {
1081 case 2:
1082 return scaleImage<Scaler2x<ColorGradientRGB>, ColorDistanceRGB>(src, trg, srcWidth, srcHeight, cfg, yFirst, yLast);
1083 case 3:
1084 return scaleImage<Scaler3x<ColorGradientRGB>, ColorDistanceRGB>(src, trg, srcWidth, srcHeight, cfg, yFirst, yLast);
1085 case 4:
1086 return scaleImage<Scaler4x<ColorGradientRGB>, ColorDistanceRGB>(src, trg, srcWidth, srcHeight, cfg, yFirst, yLast);
1087 case 5:
1088 return scaleImage<Scaler5x<ColorGradientRGB>, ColorDistanceRGB>(src, trg, srcWidth, srcHeight, cfg, yFirst, yLast);
1089 case 6:
1090 return scaleImage<Scaler6x<ColorGradientRGB>, ColorDistanceRGB>(src, trg, srcWidth, srcHeight, cfg, yFirst, yLast);
1091 }
1092 break;
1093
1094 case ColorFormat::ARGB:
1095 switch (factor)
1096 {
1097 case 2:
1098 return scaleImage<Scaler2x<ColorGradientARGB>, ColorDistanceARGB>(src, trg, srcWidth, srcHeight, cfg, yFirst, yLast);
1099 case 3:
1100 return scaleImage<Scaler3x<ColorGradientARGB>, ColorDistanceARGB>(src, trg, srcWidth, srcHeight, cfg, yFirst, yLast);
1101 case 4:
1102 return scaleImage<Scaler4x<ColorGradientARGB>, ColorDistanceARGB>(src, trg, srcWidth, srcHeight, cfg, yFirst, yLast);
1103 case 5:
1104 return scaleImage<Scaler5x<ColorGradientARGB>, ColorDistanceARGB>(src, trg, srcWidth, srcHeight, cfg, yFirst, yLast);
1105 case 6:
1106 return scaleImage<Scaler6x<ColorGradientARGB>, ColorDistanceARGB>(src, trg, srcWidth, srcHeight, cfg, yFirst, yLast);
1107 }
1108 break;
1109
1110 case ColorFormat::ARGB_UNBUFFERED:
1111 switch (factor)
1112 {
1113 case 2:
1114 return scaleImage<Scaler2x<ColorGradientARGB>, ColorDistanceUnbufferedARGB>(src, trg, srcWidth, srcHeight, cfg, yFirst, yLast);
1115 case 3:
1116 return scaleImage<Scaler3x<ColorGradientARGB>, ColorDistanceUnbufferedARGB>(src, trg, srcWidth, srcHeight, cfg, yFirst, yLast);
1117 case 4:
1118 return scaleImage<Scaler4x<ColorGradientARGB>, ColorDistanceUnbufferedARGB>(src, trg, srcWidth, srcHeight, cfg, yFirst, yLast);
1119 case 5:
1120 return scaleImage<Scaler5x<ColorGradientARGB>, ColorDistanceUnbufferedARGB>(src, trg, srcWidth, srcHeight, cfg, yFirst, yLast);
1121 case 6:
1122 return scaleImage<Scaler6x<ColorGradientARGB>, ColorDistanceUnbufferedARGB>(src, trg, srcWidth, srcHeight, cfg, yFirst, yLast);
1123 }
1124 break;
1125 }
1126 assert(false);
1127 }
1128
1129
equalColorTest(uint32_t col1,uint32_t col2,ColorFormat colFmt,double luminanceWeight,double equalColorTolerance)1130 bool xbrz::equalColorTest(uint32_t col1, uint32_t col2, ColorFormat colFmt, double luminanceWeight, double equalColorTolerance)
1131 {
1132 switch (colFmt)
1133 {
1134 case ColorFormat::RGB:
1135 return ColorDistanceRGB::dist(col1, col2, luminanceWeight) < equalColorTolerance;
1136 case ColorFormat::ARGB:
1137 return ColorDistanceARGB::dist(col1, col2, luminanceWeight) < equalColorTolerance;
1138 case ColorFormat::ARGB_UNBUFFERED:
1139 return ColorDistanceUnbufferedARGB::dist(col1, col2, luminanceWeight) < equalColorTolerance;
1140 }
1141 assert(false);
1142 return false;
1143 }
1144
1145
bilinearScale(const uint32_t * src,int srcWidth,int srcHeight,uint32_t * trg,int trgWidth,int trgHeight)1146 void xbrz::bilinearScale(const uint32_t* src, int srcWidth, int srcHeight,
1147 /**/ uint32_t* trg, int trgWidth, int trgHeight)
1148 {
1149 bilinearScale(src, srcWidth, srcHeight, srcWidth * sizeof(uint32_t),
1150 trg, trgWidth, trgHeight, trgWidth * sizeof(uint32_t),
1151 0, trgHeight, [](uint32_t pix) { return pix; });
1152 }
1153
1154
nearestNeighborScale(const uint32_t * src,int srcWidth,int srcHeight,uint32_t * trg,int trgWidth,int trgHeight)1155 void xbrz::nearestNeighborScale(const uint32_t* src, int srcWidth, int srcHeight,
1156 /**/ uint32_t* trg, int trgWidth, int trgHeight)
1157 {
1158 nearestNeighborScale(src, srcWidth, srcHeight, srcWidth * sizeof(uint32_t),
1159 trg, trgWidth, trgHeight, trgWidth * sizeof(uint32_t),
1160 0, trgHeight, [](uint32_t pix) { return pix; });
1161 }
1162
1163
1164 #if 0
1165 //#include <ppl.h>
1166 void bilinearScaleCpu(const uint32_t* src, int srcWidth, int srcHeight,
1167 /**/ uint32_t* trg, int trgWidth, int trgHeight)
1168 {
1169 const int TASK_GRANULARITY = 16;
1170
1171 concurrency::task_group tg;
1172
1173 for (int i = 0; i < trgHeight; i += TASK_GRANULARITY)
1174 tg.run([=]
1175 {
1176 const int iLast = std::min(i + TASK_GRANULARITY, trgHeight);
1177 xbrz::bilinearScale(src, srcWidth, srcHeight, srcWidth * sizeof(uint32_t),
1178 trg, trgWidth, trgHeight, trgWidth * sizeof(uint32_t),
1179 i, iLast, [](uint32_t pix) { return pix; });
1180 });
1181 tg.wait();
1182 }
1183
1184
1185 //Perf: AMP vs CPU: merely ~10% shorter runtime (scaling 1280x800 -> 1920x1080)
1186 //#include <amp.h>
1187 void bilinearScaleAmp(const uint32_t* src, int srcWidth, int srcHeight, //throw concurrency::runtime_exception
1188 /**/ uint32_t* trg, int trgWidth, int trgHeight)
1189 {
1190 //C++ AMP reference: https://msdn.microsoft.com/en-us/library/hh289390.aspx
1191 //introduction to C++ AMP: https://msdn.microsoft.com/en-us/magazine/hh882446.aspx
1192 using namespace concurrency;
1193 //TODO: pitch
1194
1195 if (srcHeight <= 0 || srcWidth <= 0) return;
1196
1197 const float scaleX = static_cast<float>(trgWidth ) / srcWidth;
1198 const float scaleY = static_cast<float>(trgHeight) / srcHeight;
1199
1200 array_view<const uint32_t, 2> srcView(srcHeight, srcWidth, src);
1201 array_view< uint32_t, 2> trgView(trgHeight, trgWidth, trg);
1202 trgView.discard_data();
1203
1204 parallel_for_each(trgView.extent, [=](index<2> idx) restrict(amp) //throw ?
1205 {
1206 const int y = idx[0];
1207 const int x = idx[1];
1208 //Perf notes:
1209 // -> float-based calculation is (almost 2x) faster than double!
1210 // -> no noticeable improvement via tiling: https://msdn.microsoft.com/en-us/magazine/hh882447.aspx
1211 // -> no noticeable improvement with restrict(amp,cpu)
1212 // -> iterating over y-axis only is significantly slower!
1213 // -> pre-calculating x,y-dependent variables in a buffer + array_view<> is ~ 20 % slower!
1214 const int y1 = srcHeight * y / trgHeight;
1215 int y2 = y1 + 1;
1216 if (y2 == srcHeight) --y2;
1217
1218 const float yy1 = y / scaleY - y1;
1219 const float y2y = 1 - yy1;
1220 //-------------------------------------
1221 const int x1 = srcWidth * x / trgWidth;
1222 int x2 = x1 + 1;
1223 if (x2 == srcWidth) --x2;
1224
1225 const float xx1 = x / scaleX - x1;
1226 const float x2x = 1 - xx1;
1227 //-------------------------------------
1228 const float x2xy2y = x2x * y2y;
1229 const float xx1y2y = xx1 * y2y;
1230 const float x2xyy1 = x2x * yy1;
1231 const float xx1yy1 = xx1 * yy1;
1232
1233 auto interpolate = [=](int offset)
1234 {
1235 /*
1236 https://en.wikipedia.org/wiki/Bilinear_interpolation
1237 (c11(x2 - x) + c21(x - x1)) * (y2 - y ) +
1238 (c12(x2 - x) + c22(x - x1)) * (y - y1)
1239 */
1240 const auto c11 = (srcView(y1, x1) >> (8 * offset)) & 0xff;
1241 const auto c21 = (srcView(y1, x2) >> (8 * offset)) & 0xff;
1242 const auto c12 = (srcView(y2, x1) >> (8 * offset)) & 0xff;
1243 const auto c22 = (srcView(y2, x2) >> (8 * offset)) & 0xff;
1244
1245 return c11 * x2xy2y + c21 * xx1y2y +
1246 c12 * x2xyy1 + c22 * xx1yy1;
1247 };
1248
1249 const float bi = interpolate(0);
1250 const float gi = interpolate(1);
1251 const float ri = interpolate(2);
1252 const float ai = interpolate(3);
1253
1254 const auto b = static_cast<uint32_t>(bi + 0.5f);
1255 const auto g = static_cast<uint32_t>(gi + 0.5f);
1256 const auto r = static_cast<uint32_t>(ri + 0.5f);
1257 const auto a = static_cast<uint32_t>(ai + 0.5f);
1258
1259 trgView(y, x) = (a << 24) | (r << 16) | (g << 8) | b;
1260 });
1261 trgView.synchronize(); //throw ?
1262 }
1263 #endif
1264