From c95b3937fef3e2c63768f1b3b1dc2c898f23d91d Mon Sep 17 00:00:00 2001 From: B Stack Date: Wed, 22 Jul 2020 11:37:03 -0400 Subject: add upstream 11.0 --- xBRZ/src/xbrz_tools.h | 247 ++++++++++++++++++++++---------------------------- 1 file changed, 108 insertions(+), 139 deletions(-) (limited to 'xBRZ/src/xbrz_tools.h') 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(((pix & inline uint16_t rgb888to565(uint32_t pix) { return static_cast(((pix & 0xF80000) >> 8) | ((pix & 0x00FC00) >> 5) | ((pix & 0x0000F8) >> 3)); } -template inline -Pix* byteAdvance(Pix* ptr, int bytes) -{ - using PixNonConst = typename std::remove_cv::type; - using PixByte = typename std::conditional::value, char, const char>::type; - - static_assert(std::is_integral::value, "Pix* is expected to be cast-able to char*"); +using BytePixel = unsigned char[4]; //unspecified byte order +static_assert(std::alignment_of_v == 1); // :) - return reinterpret_cast(reinterpret_cast(ptr) + bytes); -} - -//fill block with the given color -template inline -void fillBlock(Pix* trg, int pitch /*[bytes]*/, Pix col, int blockWidth, int blockHeight) +template 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 -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 +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::value, "PixSrc* is expected to be cast-able to char*"); - static_assert(std::is_integral::value, "PixTrg* is expected to be cast-able to char*"); - - static_assert(std::is_same::value, "PixConverter returning wrong pixel format"); - - if (srcPitch < srcWidth * static_cast(sizeof(PixSrc)) || - trgPitch < trgWidth * static_cast(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 -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::value, "PixSrc* is expected to be cast-able to char*"); - static_assert(std::is_integral::value, "PixTrg* is expected to be cast-able to char*"); - - static_assert(std::is_same::value, "PixConverter returning wrong pixel format"); - - if (srcPitch < srcWidth * static_cast(sizeof(PixSrc)) || - trgPitch < trgWidth * static_cast(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 -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 +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::value, "PixTrg* is expected to be cast-able to char*"); - static_assert(std::is_same::value, "PixConverter returning wrong pixel format"); - - if (srcPitch < srcWidth * static_cast(sizeof(uint32_t)) || - trgPitch < trgWidth * static_cast(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(trgWidth ) / srcWidth; const double scaleY = static_cast(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(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(bi + 0.5); - const auto g = static_cast(gi + 0.5); - const auto r = static_cast(ri + 0.5); - const auto a = static_cast(ai + 0.5); +#if 0 +//nearest-neighbor (going over source image - fast for upscaling, since source is read only once +template +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::value, "PixSrc* is expected to be cast-able to char*"); + static_assert(std::is_integral::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::value, "PixConverter returning wrong pixel format"); - trgLine[x] = pixCvrt(trgPix); + if (srcPitch < srcWidth * static_cast(sizeof(PixSrc)) || + trgPitch < trgWidth * static_cast(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 -- cgit