summaryrefslogtreecommitdiff
path: root/xBRZ
diff options
context:
space:
mode:
authorB Stack <bgstack15@gmail.com>2020-07-22 16:56:03 +0000
committerB Stack <bgstack15@gmail.com>2020-07-22 16:56:03 +0000
commite5633fb1c0db91f01ab967330b76baf4ecdb0512 (patch)
tree10260e25ae905564f7978b83fc4e316670f987c6 /xBRZ
parentMerge branch '10.25' into 'master' (diff)
parentadd upstream 11.0 (diff)
downloadFreeFileSync-e5633fb1c0db91f01ab967330b76baf4ecdb0512.tar.gz
FreeFileSync-e5633fb1c0db91f01ab967330b76baf4ecdb0512.tar.bz2
FreeFileSync-e5633fb1c0db91f01ab967330b76baf4ecdb0512.zip
Merge branch '11.0' into 'master'11.0
add upstream 11.0 See merge request opensource-tracking/FreeFileSync!24
Diffstat (limited to 'xBRZ')
-rw-r--r--xBRZ/src/xbrz.cpp57
-rw-r--r--xBRZ/src/xbrz_tools.h247
2 files changed, 154 insertions, 150 deletions
diff --git a/xBRZ/src/xbrz.cpp b/xBRZ/src/xbrz.cpp
index 4e9fe6dc..50660b84 100644
--- a/xBRZ/src/xbrz.cpp
+++ b/xBRZ/src/xbrz.cpp
@@ -467,7 +467,10 @@ public:
void readPonm(Kernel_4x4& ker, int x) const //(x, y) is at kernel position E
{
- [[likely]] if (const int x_p2 = x + 2; 0 <= x_p2 && x_p2 < srcWidth_)
+#if __has_cpp_attribute(likely)
+ [[likely]]
+#endif
+ if (const int x_p2 = x + 2; 0 <= x_p2 && x_p2 < srcWidth_)
{
ker.p = s_m1 ? s_m1[x_p2] : 0;
ker.o = s_0 ? s_0 [x_p2] : 0;
@@ -520,6 +523,16 @@ private:
};
+inline
+void fillBlock(uint32_t* trg, int trgWidth, uint32_t col, int blockSize)
+{
+ for (int y = 0; y < blockSize; ++y, trg += trgWidth)
+ // std::fill(trg, trg + blockSize, col);
+ for (int x = 0; x < blockSize; ++x)
+ trg[x] = col;
+}
+
+
template <class Scaler, class ColorDistance, class OobReader> //scaler policy: see "Scaler2x" reference implementation
void scaleImage(const uint32_t* src, uint32_t* trg, int srcWidth, int srcHeight, const xbrz::ScalerCfg& cfg, int yFirst, int yLast)
{
@@ -670,7 +683,10 @@ void scaleImage(const uint32_t* src, uint32_t* trg, int srcWidth, int srcHeight,
addTopR(blend_xy1, res.blend_h); //set 2nd known corner for (x, y + 1)
preProcBuf[x] = blend_xy1; //store on current buffer position for use on next row
- [[likely]] if (x + 1 < srcWidth)
+#if __has_cpp_attribute(likely)
+ [[likely]]
+#endif
+ if (x + 1 < srcWidth)
{
//blend_xy1 -> blend_x1y1
clearAddTopL(blend_xy1, res.blend_i); //set 1st known corner for (x + 1, y + 1) and buffer for use on next column
@@ -680,7 +696,8 @@ void scaleImage(const uint32_t* src, uint32_t* trg, int srcWidth, int srcHeight,
}
//fill block of size scale * scale with the given color
- fillBlock(out, trgWidth * sizeof(uint32_t), ker4.e, Scaler::scale, Scaler::scale);
+ fillBlock(out, trgWidth, ker4.e, Scaler::scale);
+
//place *after* preprocessing step, to not overwrite the results while processing the last pixel!
//blend all four corners of current pixel
@@ -1145,6 +1162,7 @@ void xbrz::scale(size_t factor, const uint32_t* src, uint32_t* trg, int srcWidth
static_assert(SCALE_FACTOR_MAX == 6);
switch (colFmt)
{
+ //*INDENT-OFF*
case ColorFormat::RGB:
switch (factor)
{
@@ -1177,6 +1195,7 @@ void xbrz::scale(size_t factor, const uint32_t* src, uint32_t* trg, int srcWidth
case 6: return scaleImage<Scaler6x<ColorGradientARGB>, ColorDistanceUnbufferedARGB, OobReaderTransparent>(src, trg, srcWidth, srcHeight, cfg, yFirst, yLast);
}
break;
+ //*INDENT-ON*
}
assert(false);
}
@@ -1186,8 +1205,10 @@ bool xbrz::equalColorTest(uint32_t col1, uint32_t col2, ColorFormat colFmt, doub
{
switch (colFmt)
{
- case ColorFormat::RGB: return ColorDistanceRGB::dist(col1, col2, luminanceWeight) < equalColorTolerance;
- case ColorFormat::ARGB: return ColorDistanceARGB::dist(col1, col2, luminanceWeight) < equalColorTolerance;
+ case ColorFormat::RGB:
+ return ColorDistanceRGB::dist(col1, col2, luminanceWeight) < equalColorTolerance;
+ case ColorFormat::ARGB:
+ return ColorDistanceARGB::dist(col1, col2, luminanceWeight) < equalColorTolerance;
case ColorFormat::ARGB_UNBUFFERED:
return ColorDistanceUnbufferedARGB::dist(col1, col2, luminanceWeight) < equalColorTolerance;
}
@@ -1199,18 +1220,32 @@ bool xbrz::equalColorTest(uint32_t col1, uint32_t col2, ColorFormat colFmt, doub
void xbrz::bilinearScale(const uint32_t* src, int srcWidth, int srcHeight,
/**/ uint32_t* trg, int trgWidth, int trgHeight)
{
- bilinearScale(src, srcWidth, srcHeight, srcWidth * sizeof(uint32_t),
- trg, trgWidth, trgHeight, trgWidth * sizeof(uint32_t),
- 0, trgHeight, [](uint32_t pix) { return pix; });
+ const auto imgReader = [src, srcWidth](int x, int y, BytePixel& pix)
+ {
+ static_assert(sizeof(pix) == sizeof(uint32_t));
+ std::memcpy(pix, src + y * srcWidth + x, sizeof(pix));
+ };
+
+ const auto imgWriter = [trg](const xbrz::BytePixel& pix) mutable { std::memcpy(trg++, pix, sizeof(pix)); };
+
+ bilinearScale(imgReader, srcWidth, srcHeight,
+ imgWriter, trgWidth, trgHeight, 0, trgHeight);
}
void xbrz::nearestNeighborScale(const uint32_t* src, int srcWidth, int srcHeight,
/**/ uint32_t* trg, int trgWidth, int trgHeight)
{
- nearestNeighborScale(src, srcWidth, srcHeight, srcWidth * sizeof(uint32_t),
- trg, trgWidth, trgHeight, trgWidth * sizeof(uint32_t),
- 0, trgHeight, [](uint32_t pix) { return pix; });
+ const auto imgReader = [src, srcWidth](int x, int y, BytePixel& pix)
+ {
+ static_assert(sizeof(pix) == sizeof(uint32_t));
+ std::memcpy(pix, src + y * srcWidth + x, sizeof(pix));
+ };
+
+ const auto imgWriter = [trg](const xbrz::BytePixel& pix) mutable { std::memcpy(trg++, pix, sizeof(pix)); };
+
+ nearestNeighborScale(imgReader, srcWidth, srcHeight,
+ imgWriter, trgWidth, trgHeight, 0, trgHeight);
}
diff --git a/xBRZ/src/xbrz_tools.h b/xBRZ/src/xbrz_tools.h
index 9d8e8247..404849ca 100644
--- a/xBRZ/src/xbrz_tools.h
+++ b/xBRZ/src/xbrz_tools.h
@@ -33,8 +33,8 @@ inline unsigned char getRed (uint32_t pix) { return getByte<2>(pix); }
inline unsigned char getGreen(uint32_t pix) { return getByte<1>(pix); }
inline unsigned char getBlue (uint32_t pix) { return getByte<0>(pix); }
-inline uint32_t makePixel(unsigned char a, unsigned char r, unsigned char g, unsigned char b) { return (a << 24) | (r << 16) | (g << 8) | b; }
-inline uint32_t makePixel( unsigned char r, unsigned char g, unsigned char b) { return (r << 16) | (g << 8) | b; }
+inline uint32_t makePixel(uint32_t a, uint32_t r, uint32_t g, uint32_t b) { return (a << 24) | (r << 16) | (g << 8) | b; }
+inline uint32_t makePixel( uint32_t r, uint32_t g, uint32_t b) { return (r << 16) | (g << 8) | b; }
inline uint32_t rgb555to888(uint16_t pix) { return ((pix & 0x7C00) << 9) | ((pix & 0x03E0) << 6) | ((pix & 0x001F) << 3); }
inline uint32_t rgb565to888(uint16_t pix) { return ((pix & 0xF800) << 8) | ((pix & 0x07E0) << 5) | ((pix & 0x001F) << 3); }
@@ -43,49 +43,30 @@ inline uint16_t rgb888to555(uint32_t pix) { return static_cast<uint16_t>(((pix &
inline uint16_t rgb888to565(uint32_t pix) { return static_cast<uint16_t>(((pix & 0xF80000) >> 8) | ((pix & 0x00FC00) >> 5) | ((pix & 0x0000F8) >> 3)); }
-template <class Pix> inline
-Pix* byteAdvance(Pix* ptr, int bytes)
-{
- using PixNonConst = typename std::remove_cv<Pix>::type;
- using PixByte = typename std::conditional<std::is_same<Pix, PixNonConst>::value, char, const char>::type;
-
- static_assert(std::is_integral<PixNonConst>::value, "Pix* is expected to be cast-able to char*");
+using BytePixel = unsigned char[4]; //unspecified byte order
+static_assert(std::alignment_of_v<BytePixel> == 1); // :)
- return reinterpret_cast<Pix*>(reinterpret_cast<PixByte*>(ptr) + bytes);
-}
-
-//fill block with the given color
-template <class Pix> inline
-void fillBlock(Pix* trg, int pitch /*[bytes]*/, Pix col, int blockWidth, int blockHeight)
+template <class PixReader, class PixWriter> inline
+void unscaledCopy(PixReader srcReader /* (int x, int y, BytePixel& pix) */,
+ PixWriter trgWriter /* (const BytePixel& pix) */, int width, int height)
{
- //for (int y = 0; y < blockHeight; ++y, trg = byteAdvance(trg, pitch))
- // std::fill(trg, trg + blockWidth, col);
-
- for (int y = 0; y < blockHeight; ++y, trg = byteAdvance(trg, pitch))
- for (int x = 0; x < blockWidth; ++x)
- trg[x] = col;
+ for (int y = 0; y < height; ++y)
+ for (int x = 0; x < width; ++x)
+ {
+ BytePixel pix; //uninitialized
+ srcReader(x, y, pix);
+ trgWriter(pix);
+ }
}
//nearest-neighbor (going over target image - slow for upscaling, since source is read multiple times missing out on cache! Fast for similar image sizes!)
-template <class PixSrc, class PixTrg, class PixConverter>
-void nearestNeighborScale(const PixSrc* src, int srcWidth, int srcHeight, int srcPitch /*[bytes]*/,
- /**/ PixTrg* trg, int trgWidth, int trgHeight, int trgPitch /*[bytes]*/,
- int yFirst, int yLast, PixConverter pixCvrt /*convert PixSrc to PixTrg*/)
+template <class PixReader, class PixWriter>
+void nearestNeighborScale(PixReader srcReader /* (int x, int y, BytePixel& pix) */, int srcWidth, int srcHeight,
+ PixWriter trgWriter /* (const BytePixel& pix) */, int trgWidth, int trgHeight,
+ int yFirst, int yLast)
{
- static_assert(std::is_integral<PixSrc>::value, "PixSrc* is expected to be cast-able to char*");
- static_assert(std::is_integral<PixTrg>::value, "PixTrg* is expected to be cast-able to char*");
-
- static_assert(std::is_same<decltype(pixCvrt(PixSrc())), PixTrg>::value, "PixConverter returning wrong pixel format");
-
- if (srcPitch < srcWidth * static_cast<int>(sizeof(PixSrc)) ||
- trgPitch < trgWidth * static_cast<int>(sizeof(PixTrg)))
- {
- assert(false);
- return;
- }
-
yFirst = std::max(yFirst, 0);
yLast = std::min(yLast, trgHeight);
if (yFirst >= yLast || srcHeight <= 0 || srcWidth <= 0) return;
@@ -93,92 +74,28 @@ void nearestNeighborScale(const PixSrc* src, int srcWidth, int srcHeight, int sr
for (int y = yFirst; y < yLast; ++y)
{
const int ySrc = srcHeight * y / trgHeight;
- const PixSrc* const srcLine = byteAdvance(src, ySrc * srcPitch);
- /**/ PixTrg* const trgLine = byteAdvance(trg, y * trgPitch);
for (int x = 0; x < trgWidth; ++x)
{
const int xSrc = srcWidth * x / trgWidth;
- trgLine[x] = pixCvrt(srcLine[xSrc]);
- }
- }
-}
-
-
-//nearest-neighbor (going over source image - fast for upscaling, since source is read only once
-template <class PixSrc, class PixTrg, class PixConverter>
-void nearestNeighborScaleOverSource(const PixSrc* src, int srcWidth, int srcHeight, int srcPitch /*[bytes]*/,
- /**/ PixTrg* trg, int trgWidth, int trgHeight, int trgPitch /*[bytes]*/,
- int yFirst, int yLast, PixConverter pixCvrt /*convert PixSrc to PixTrg*/)
-{
- static_assert(std::is_integral<PixSrc>::value, "PixSrc* is expected to be cast-able to char*");
- static_assert(std::is_integral<PixTrg>::value, "PixTrg* is expected to be cast-able to char*");
-
- static_assert(std::is_same<decltype(pixCvrt(PixSrc())), PixTrg>::value, "PixConverter returning wrong pixel format");
-
- if (srcPitch < srcWidth * static_cast<int>(sizeof(PixSrc)) ||
- trgPitch < trgWidth * static_cast<int>(sizeof(PixTrg)))
- {
- assert(false);
- return;
- }
-
- yFirst = std::max(yFirst, 0);
- yLast = std::min(yLast, srcHeight);
- if (yFirst >= yLast || trgWidth <= 0 || trgHeight <= 0) return;
-
- for (int y = yFirst; y < yLast; ++y)
- {
- //mathematically: ySrc = floor(srcHeight * yTrg / trgHeight)
- // => search for integers in: [ySrc, ySrc + 1) * trgHeight / srcHeight
-
- //keep within for loop to support MT input slices!
- const int yTrgFirst = ( y * trgHeight + srcHeight - 1) / srcHeight; //=ceil(y * trgHeight / srcHeight)
- const int yTrgLast = ((y + 1) * trgHeight + srcHeight - 1) / srcHeight; //=ceil(((y + 1) * trgHeight) / srcHeight)
- const int blockHeight = yTrgLast - yTrgFirst;
-
- if (blockHeight > 0)
- {
- const PixSrc* srcLine = byteAdvance(src, y * srcPitch);
- /**/ PixTrg* trgLine = byteAdvance(trg, yTrgFirst * trgPitch);
- int xTrgFirst = 0;
-
- for (int x = 0; x < srcWidth; ++x)
- {
- const int xTrgLast = ((x + 1) * trgWidth + srcWidth - 1) / srcWidth;
- const int blockWidth = xTrgLast - xTrgFirst;
- if (blockWidth > 0)
- {
- xTrgFirst = xTrgLast;
- const auto trgPix = pixCvrt(srcLine[x]);
- fillBlock(trgLine, trgPitch, trgPix, blockWidth, blockHeight);
- trgLine += blockWidth;
- }
- }
+ BytePixel pix; //uninitialized
+ srcReader(xSrc, ySrc, pix);
+ trgWriter(pix);
}
}
}
-template <class PixTrg, class PixConverter>
-void bilinearScale(const uint32_t* src, int srcWidth, int srcHeight, int srcPitch,
- /**/ PixTrg* trg, int trgWidth, int trgHeight, int trgPitch,
- int yFirst, int yLast, PixConverter pixCvrt /*convert uint32_t to PixTrg*/)
+template <class PixReader, class PixWriter>
+void bilinearScale(PixReader srcReader /* (int x, int y, BytePixel& pix) */, int srcWidth, int srcHeight,
+ PixWriter trgWriter /* (const BytePixel& pix) */, int trgWidth, int trgHeight,
+ int yFirst, int yLast)
{
- static_assert(std::is_integral<PixTrg>::value, "PixTrg* is expected to be cast-able to char*");
- static_assert(std::is_same<decltype(pixCvrt(uint32_t())), PixTrg>::value, "PixConverter returning wrong pixel format");
-
- if (srcPitch < srcWidth * static_cast<int>(sizeof(uint32_t)) ||
- trgPitch < trgWidth * static_cast<int>(sizeof(PixTrg)))
- {
- assert(false);
- return;
- }
-
yFirst = std::max(yFirst, 0);
yLast = std::min(yLast, trgHeight);
- if (yFirst >= yLast || srcHeight <= 0 || srcWidth <= 0) return;
+ if (yFirst >= yLast || srcHeight <= 0 || srcWidth <= 0)
+ return;
const double scaleX = static_cast<double>(trgWidth ) / srcWidth;
const double scaleY = static_cast<double>(trgHeight) / srcHeight;
@@ -198,7 +115,8 @@ void bilinearScale(const uint32_t* src, int srcWidth, int srcHeight, int srcPitc
{
const int x1 = srcWidth * x / trgWidth;
int x2 = x1 + 1;
- if (x2 == srcWidth) --x2;
+ if (x2 == srcWidth)
+ --x2;
const double xx1 = x / scaleX - x1;
const double x2x = 1 - xx1;
@@ -210,58 +128,109 @@ void bilinearScale(const uint32_t* src, int srcWidth, int srcHeight, int srcPitc
{
const int y1 = srcHeight * y / trgHeight;
int y2 = y1 + 1;
- if (y2 == srcHeight) --y2;
+ if (y2 == srcHeight)
+ --y2;
const double yy1 = y / scaleY - y1;
const double y2y = 1 - yy1;
- const uint32_t* const srcLine = byteAdvance(src, y1 * srcPitch);
- const uint32_t* const srcLineNext = byteAdvance(src, y2 * srcPitch);
- PixTrg* const trgLine = byteAdvance(trg, y * trgPitch);
-
for (int x = 0; x < trgWidth; ++x)
{
//perf: do NOT "simplify" the variable layout without measurement!
- const int x1 = buf[x].x1;
- const int x2 = buf[x].x2;
- const double xx1 = buf[x].xx1;
- const double x2x = buf[x].x2x;
+ const CoeffsX& bufX = buf[x];
+ const int x1 = bufX.x1;
+ const int x2 = bufX.x2;
+ const double xx1 = bufX.xx1;
+ const double x2x = bufX.x2x;
const double x2xy2y = x2x * y2y;
const double xx1y2y = xx1 * y2y;
const double x2xyy1 = x2x * yy1;
const double xx1yy1 = xx1 * yy1;
- auto interpolate = [=](int offset)
+ BytePixel pix11; //
+ BytePixel pix21; //uninitialized
+ BytePixel pix12; //
+ BytePixel pix22; //
+
+ srcReader(x1, y1, pix11); //
+ srcReader(x2, y1, pix21); //perf: srcReader has to (re-)calculate row using y
+ srcReader(x1, y2, pix12); // => ~7% additional runtime
+ srcReader(x2, y2, pix22); //
+
+ const auto interpolate = [&](int offset)
{
/* https://en.wikipedia.org/wiki/Bilinear_interpolation
(c11(x2 - x) + c21(x - x1)) * (y2 - y ) +
- (c12(x2 - x) + c22(x - x1)) * (y - y1) */
- const auto c11 = (srcLine [x1] >> (8 * offset)) & 0xff;
- const auto c21 = (srcLine [x2] >> (8 * offset)) & 0xff;
- const auto c12 = (srcLineNext[x1] >> (8 * offset)) & 0xff;
- const auto c22 = (srcLineNext[x2] >> (8 * offset)) & 0xff;
-
- return c11 * x2xy2y + c21 * xx1y2y +
- c12 * x2xyy1 + c22 * xx1yy1;
+ (c12(x2 - x) + c22(x - x1)) * (y - y1) */
+ return static_cast<unsigned char>(pix11[offset] * x2xy2y + pix21[offset] * xx1y2y +
+ pix12[offset] * x2xyy1 + pix22[offset] * xx1yy1 + 0.5);
};
+ trgWriter(BytePixel{interpolate(0),
+ interpolate(1),
+ interpolate(2),
+ interpolate(3)});
+ }
+ }
+}
- const double bi = interpolate(0);
- const double gi = interpolate(1);
- const double ri = interpolate(2);
- const double ai = interpolate(3);
- const auto b = static_cast<uint32_t>(bi + 0.5);
- const auto g = static_cast<uint32_t>(gi + 0.5);
- const auto r = static_cast<uint32_t>(ri + 0.5);
- const auto a = static_cast<uint32_t>(ai + 0.5);
+#if 0
+//nearest-neighbor (going over source image - fast for upscaling, since source is read only once
+template <class PixSrc, class PixTrg, class PixConverter>
+void nearestNeighborScaleOverSource(const PixSrc* src, int srcWidth, int srcHeight, int srcPitch /*[bytes]*/,
+ /**/ PixTrg* trg, int trgWidth, int trgHeight, int trgPitch /*[bytes]*/,
+ int yFirst, int yLast, PixConverter pixCvrt /*convert PixSrc to PixTrg*/)
+{
+ static_assert(std::is_integral<PixSrc>::value, "PixSrc* is expected to be cast-able to char*");
+ static_assert(std::is_integral<PixTrg>::value, "PixTrg* is expected to be cast-able to char*");
- const uint32_t trgPix = (a << 24) | (r << 16) | (g << 8) | b;
+ static_assert(std::is_same<decltype(pixCvrt(PixSrc())), PixTrg>::value, "PixConverter returning wrong pixel format");
- trgLine[x] = pixCvrt(trgPix);
+ if (srcPitch < srcWidth * static_cast<int>(sizeof(PixSrc)) ||
+ trgPitch < trgWidth * static_cast<int>(sizeof(PixTrg)))
+ {
+ assert(false);
+ return;
+ }
+
+ yFirst = std::max(yFirst, 0);
+ yLast = std::min(yLast, srcHeight);
+ if (yFirst >= yLast || trgWidth <= 0 || trgHeight <= 0) return;
+
+ for (int y = yFirst; y < yLast; ++y)
+ {
+ //mathematically: ySrc = floor(srcHeight * yTrg / trgHeight)
+ // => search for integers in: [ySrc, ySrc + 1) * trgHeight / srcHeight
+
+ //keep within for loop to support MT input slices!
+ const int yTrgFirst = ( y * trgHeight + srcHeight - 1) / srcHeight; //=ceil(y * trgHeight / srcHeight)
+ const int yTrgLast = ((y + 1) * trgHeight + srcHeight - 1) / srcHeight; //=ceil(((y + 1) * trgHeight) / srcHeight)
+ const int blockHeight = yTrgLast - yTrgFirst;
+
+ if (blockHeight > 0)
+ {
+ const PixSrc* srcLine = byteAdvance(src, y * srcPitch);
+ /**/ PixTrg* trgLine = byteAdvance(trg, yTrgFirst * trgPitch);
+ int xTrgFirst = 0;
+
+ for (int x = 0; x < srcWidth; ++x)
+ {
+ const int xTrgLast = ((x + 1) * trgWidth + srcWidth - 1) / srcWidth;
+ const int blockWidth = xTrgLast - xTrgFirst;
+ if (blockWidth > 0)
+ {
+ xTrgFirst = xTrgLast;
+
+ const auto trgPix = pixCvrt(srcLine[x]);
+ fillBlock(trgLine, trgPitch, trgPix, blockWidth, blockHeight);
+ trgLine += blockWidth;
+ }
+ }
}
}
}
+#endif
}
#endif //XBRZ_TOOLS_H_825480175091875
bgstack15