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