- // ------------------------------------------------------------------------- 
- // | xBRZ: "Scale by rules" - high quality image upscaling filter by Zenju | 
- // ------------------------------------------------------------------------- 
- // using a modified approach of xBR: 
- // http://board.byuu.org/viewtopic.php?f=10&t=2248 
- //  - new rule set preserving small image features 
- //  - highly optimized for performance 
- //  - support alpha channel 
- //  - support multithreading 
- //  - support 64-bit architectures 
- //  - support processing image slices 
- //  - support scaling up to 6xBRZ 
-   
- // -> map source (srcWidth * srcHeight) to target (scale * width x scale * height) image, optionally processing a half-open slice of rows [yFirst, yLast) only 
- // -> support for source/target pitch in bytes! 
- // -> if your emulator changes only a few image slices during each cycle (e.g. DOSBox) then there's no need to run xBRZ on the complete image: 
- //    Just make sure you enlarge the source image slice by 2 rows on top and 2 on bottom (this is the additional range the xBRZ algorithm is using during analysis) 
- //    CAVEAT: If there are multiple changed slices, make sure they do not overlap after adding these additional rows in order to avoid a memory race condition 
- //    in the target image data if you are using multiple threads for processing each enlarged slice! 
- //  
- // THREAD-SAFETY: - parts of the same image may be scaled by multiple threads as long as the [yFirst, yLast) ranges do not overlap! 
- //                - there is a minor inefficiency for the first row of a slice, so avoid processing single rows only; suggestion: process at least 8-16 rows 
-   
-   
- #include <stddef.h> // for size_t 
- #include <stdint.h> // for uint32_t 
- #include <stdbool.h> // for bool 
- #include <memory.h> // for memset() 
- #include <limits.h> 
- #include <math.h> 
-   
-   
- // prototypes of exported functions 
- void xbrz_scale (size_t factor, const uint32_t *src, uint32_t *trg, int srcWidth, int srcHeight, bool has_alpha_channel); 
- void nearest_neighbor_scale (const uint32_t *src, int srcWidth, int srcHeight, uint32_t *trg, int trgWidth, int trgHeight); 
-   
-   
- // algorithm configuration 
- #define XBRZ_CFG_LUMINANCE_WEIGHT 1 
- #define XBRZ_CFG_EQUAL_COLOR_TOLERANCE 30 
- #define XBRZ_CFG_DOMINANT_DIRECTION_THRESHOLD 3.6 
- #define XBRZ_CFG_STEEP_DIRECTION_THRESHOLD 2.2 
-   
-   
- // blend types 
- #define BLEND_NONE     0 
- #define BLEND_NORMAL   1 // a normal indication to blend 
- #define BLEND_DOMINANT 2 // a strong indication to blend 
-   
-   
- // handy macros 
- #ifndef MIN 
- #define MIN(a,b) ((a) < (b) ? (a) : (b)) 
- #endif // MIN 
- #ifndef MAX 
- #define MAX(a,b) ((a) > (b) ? (a) : (b)) 
- #endif // MAX 
- #define GET_BYTE(val,byteno) ((uint8_t) (((val) >> ((byteno) << 3)) & 0xff)) 
- #define GET_BLUE(val)  GET_BYTE (val, 0) 
- #define GET_GREEN(val) GET_BYTE (val, 1) 
- #define GET_RED(val)   GET_BYTE (val, 2) 
- #define GET_ALPHA(val) GET_BYTE (val, 3) 
- #define CALC_COLOR24(colFront,colBack,M,N) (uint8_t) ((((uint8_t) (colFront)) * ((unsigned int) (M)) + ((uint8_t) (colBack)) * (((unsigned int) (N)) - ((unsigned int) (M)))) / ((unsigned int) (N))) 
- #define CALC_COLOR32(colFront,colBack,weightFront,weightBack,weightSum) ((uint8_t) ((((uint8_t) (colFront)) * ((unsigned int) (weightFront)) + ((uint8_t) (colBack)) * ((unsigned int) (weightBack))) / ((unsigned int) (weightSum)))) 
- #define BYTE_ADVANCE(buffer,offset) (((char *) buffer) + (offset)) 
-   
-   
- // compress four blend types into a single byte 
- #define getTopL(b)    ((uint8_t) (0x3 & ((uint8_t) (b) >> 0))) 
- #define getTopR(b)    ((uint8_t) (0x3 & ((uint8_t) (b) >> 2))) 
- #define getBottomR(b) ((uint8_t) (0x3 & ((uint8_t) (b) >> 4))) 
- #define getBottomL(b) ((uint8_t) (0x3 & ((uint8_t) (b) >> 6))) 
- #define setTopL(b,blend_type)    *(b) |= (((uint8_t) (blend_type)) << 0) // buffer is assumed to be initialized before preprocessing! 
- #define setTopR(b,blend_type)    *(b) |= (((uint8_t) (blend_type)) << 2) 
- #define setBottomR(b,blend_type) *(b) |= (((uint8_t) (blend_type)) << 4) 
- #define setBottomL(b,blend_type) *(b) |= (((uint8_t) (blend_type)) << 6) 
-   
-   
- typedef struct blendresult_s 
- { 
-    uint8_t 
-       blend_f, blend_g, 
-       blend_j, blend_k; 
- } blendresult_t; 
-   
-   
- typedef struct kernel_3x3_s 
- { 
-    uint32_t 
-       a, b, c, 
-       d, e, f, 
-       g, h, i; 
- } kernel_3x3_t; 
-   
-   
- typedef struct kernel_4x4_s //kernel for preprocessing step 
- { 
-    uint32_t 
-       a, b, c, d, 
-       e, f, g, h, 
-       i, j, k, l, 
-       m, n, o, p; 
- } kernel_4x4_t; 
-   
-   
- typedef struct colorformat_s 
- { 
-    int bpp; 
-    void (*alphagrad) (uint32_t *pixBack, uint32_t pixFront, unsigned int M, unsigned int N); 
-    double (*dist) (uint32_t pix1, uint32_t pix2); 
- } colorformat_t; 
-   
-   
- typedef struct outmatrix_s 
- { 
-    size_t size; 
-    uint32_t* ptr; 
-    int stride; 
- } outmatrix_t; 
-   
-   
- typedef uint32_t *(outmatrixreffunc_t) (outmatrix_t *mat, size_t I, size_t J); 
-   
-   
- typedef struct scaler_s 
- { 
-    int factor; 
-    void (*blend_line_shallow)           (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref); 
-    void (*blend_line_steep)             (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref); 
-    void (*blend_line_steep_and_shallow) (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref); 
-    void (*blend_line_diagonal)          (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref); 
-    void (*blend_corner)                 (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref); 
- } scaler_t; 
-   
-   
- ///////////////////////////////// 
- // shallow line scaling functions 
-   
- static void blend_line_shallow_2x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref) 
- { 
-    color_format->alphagrad (outmatrix_ref (out, 2 - 1, 0), col, 1, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 2 - 1, 1), col, 3, 4); 
- } 
- static void blend_line_shallow_3x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref) 
- { 
-    color_format->alphagrad (outmatrix_ref (out, 3 - 1, 0), col, 1, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 3 - 2, 2), col, 1, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 3 - 1, 1), col, 3, 4); 
-    *outmatrix_ref (out, 3 - 1, 2) = col; 
- } 
- static void blend_line_shallow_4x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref) 
- { 
-    color_format->alphagrad (outmatrix_ref (out, 4 - 1, 0), col, 1, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 4 - 2, 2), col, 1, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 4 - 1, 1), col, 3, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 4 - 2, 3), col, 3, 4); 
-    *outmatrix_ref (out, 4 - 1, 2) = col; 
-    *outmatrix_ref (out, 4 - 1, 3) = col; 
- } 
- static void blend_line_shallow_5x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref) 
- { 
-    color_format->alphagrad (outmatrix_ref (out, 5 - 1, 0), col, 1, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 5 - 2, 2), col, 1, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 5 - 3, 4), col, 1, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 5 - 1, 1), col, 3, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 5 - 2, 3), col, 3, 4); 
-    *outmatrix_ref (out, 5 - 1, 2) = col; 
-    *outmatrix_ref (out, 5 - 1, 3) = col; 
-    *outmatrix_ref (out, 5 - 1, 4) = col; 
-    *outmatrix_ref (out, 5 - 2, 4) = col; 
- } 
- static void blend_line_shallow_6x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref) 
- { 
-    color_format->alphagrad (outmatrix_ref (out, 6 - 1, 0), col, 1, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 6 - 2, 2), col, 1, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 6 - 3, 4), col, 1, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 6 - 1, 1), col, 3, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 6 - 2, 3), col, 3, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 6 - 3, 5), col, 3, 4); 
-    *outmatrix_ref (out, 6 - 1, 2) = col; 
-    *outmatrix_ref (out, 6 - 1, 3) = col; 
-    *outmatrix_ref (out, 6 - 1, 4) = col; 
-    *outmatrix_ref (out, 6 - 1, 5) = col; 
-    *outmatrix_ref (out, 6 - 2, 4) = col; 
-    *outmatrix_ref (out, 6 - 2, 5) = col; 
- } 
-   
- /////////////////////////////// 
- // steep line scaling functions 
-   
- static void blend_line_steep_2x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref) 
- { 
-    color_format->alphagrad (outmatrix_ref (out, 0, 2 - 1), col, 1, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 1, 2 - 1), col, 3, 4); 
- } 
- static void blend_line_steep_3x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref) 
- { 
-    color_format->alphagrad (outmatrix_ref (out, 0, 3 - 1), col, 1, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 2, 3 - 2), col, 1, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 1, 3 - 1), col, 3, 4); 
-    *outmatrix_ref (out, 2, 3 - 1) = col; 
- } 
- static void blend_line_steep_4x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref) 
- { 
-    color_format->alphagrad (outmatrix_ref (out, 0, 4 - 1), col, 1, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 2, 4 - 2), col, 1, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 1, 4 - 1), col, 3, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 3, 4 - 2), col, 3, 4); 
-    *outmatrix_ref (out, 2, 4 - 1) = col; 
-    *outmatrix_ref (out, 3, 4 - 1) = col; 
- } 
- static void blend_line_steep_5x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref) 
- { 
-    color_format->alphagrad (outmatrix_ref (out, 0, 5 - 1), col, 1, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 2, 5 - 2), col, 1, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 4, 5 - 3), col, 1, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 1, 5 - 1), col, 3, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 3, 5 - 2), col, 3, 4); 
-    *outmatrix_ref (out, 2, 5 - 1) = col; 
-    *outmatrix_ref (out, 3, 5 - 1) = col; 
-    *outmatrix_ref (out, 4, 5 - 1) = col; 
-    *outmatrix_ref (out, 4, 5 - 2) = col; 
- } 
- static void blend_line_steep_6x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref) 
- { 
-    color_format->alphagrad (outmatrix_ref (out, 0, 6 - 1), col, 1, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 2, 6 - 2), col, 1, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 4, 6 - 3), col, 1, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 1, 6 - 1), col, 3, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 3, 6 - 2), col, 3, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 5, 6 - 3), col, 3, 4); 
-    *outmatrix_ref (out, 2, 6 - 1) = col; 
-    *outmatrix_ref (out, 3, 6 - 1) = col; 
-    *outmatrix_ref (out, 4, 6 - 1) = col; 
-    *outmatrix_ref (out, 5, 6 - 1) = col; 
-    *outmatrix_ref (out, 4, 6 - 2) = col; 
-    *outmatrix_ref (out, 5, 6 - 2) = col; 
- } 
-   
- /////////////////////////////////////////// 
- // steep and shallow line scaling functions 
-   
- static void blend_line_steep_and_shallow_2x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref) 
- { 
-    color_format->alphagrad (outmatrix_ref (out, 1, 0), col, 1, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 0, 1), col, 1, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 1, 1), col, 5, 6); // [!] fixes 7/8 used in xBR 
- } 
- static void blend_line_steep_and_shallow_3x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref) 
- { 
-    color_format->alphagrad (outmatrix_ref (out, 2, 0), col, 1, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 0, 2), col, 1, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 2, 1), col, 3, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 1, 2), col, 3, 4); 
-    *outmatrix_ref (out, 2, 2) = col; 
- } 
- static void blend_line_steep_and_shallow_4x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref) 
- { 
-    color_format->alphagrad (outmatrix_ref (out, 3, 1), col, 3, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 1, 3), col, 3, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 3, 0), col, 1, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 0, 3), col, 1, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 2, 2), col, 1, 3); // [!] fixes 1/4 used in xBR 
-    *outmatrix_ref (out, 3, 3) = col; 
-    *outmatrix_ref (out, 3, 2) = col; 
-    *outmatrix_ref (out, 2, 3) = col; 
- } 
- static void blend_line_steep_and_shallow_5x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref) 
- { 
-    color_format->alphagrad (outmatrix_ref (out, 0, 5 - 1), col, 1, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 2, 5 - 2), col, 1, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 1, 5 - 1), col, 3, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 5 - 1, 0), col, 1, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 5 - 2, 2), col, 1, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 5 - 1, 1), col, 3, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 3, 3), col, 2, 3); 
-    *outmatrix_ref (out, 2, 5 - 1) = col; 
-    *outmatrix_ref (out, 3, 5 - 1) = col; 
-    *outmatrix_ref (out, 4, 5 - 1) = col; 
-    *outmatrix_ref (out, 5 - 1, 2) = col; 
-    *outmatrix_ref (out, 5 - 1, 3) = col; 
- } 
- static void blend_line_steep_and_shallow_6x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref) 
- { 
-    color_format->alphagrad (outmatrix_ref (out, 0, 6 - 1), col, 1, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 2, 6 - 2), col, 1, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 1, 6 - 1), col, 3, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 3, 6 - 2), col, 3, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 6 - 1, 0), col, 1, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 6 - 2, 2), col, 1, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 6 - 1, 1), col, 3, 4); 
-    color_format->alphagrad (outmatrix_ref (out, 6 - 2, 3), col, 3, 4); 
-    *outmatrix_ref (out, 2, 6 - 1) = col; 
-    *outmatrix_ref (out, 3, 6 - 1) = col; 
-    *outmatrix_ref (out, 4, 6 - 1) = col; 
-    *outmatrix_ref (out, 5, 6 - 1) = col; 
-    *outmatrix_ref (out, 4, 6 - 2) = col; 
-    *outmatrix_ref (out, 5, 6 - 2) = col; 
-    *outmatrix_ref (out, 6 - 1, 2) = col; 
-    *outmatrix_ref (out, 6 - 1, 3) = col; 
- } 
-   
- ////////////////////////////////// 
- // diagonal line scaling functions 
-   
- static void blend_line_diagonal_2x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref) 
- { 
-    color_format->alphagrad (outmatrix_ref (out, 1, 1), col, 1, 2); 
- } 
- static void blend_line_diagonal_3x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref) 
- { 
-    color_format->alphagrad (outmatrix_ref (out, 1, 2), col, 1, 8); // conflict with other rotations for this odd scale 
-    color_format->alphagrad (outmatrix_ref (out, 2, 1), col, 1, 8); 
-    color_format->alphagrad (outmatrix_ref (out, 2, 2), col, 7, 8); 
- } 
- static void blend_line_diagonal_4x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref) 
- { 
-    color_format->alphagrad (outmatrix_ref (out, 4 - 1, 4 / 2), col, 1, 2); 
-    color_format->alphagrad (outmatrix_ref (out, 4 - 2, 4 / 2 + 1), col, 1, 2); 
-    *outmatrix_ref (out, 4 - 1, 4 - 1) = col; 
- } 
- static void blend_line_diagonal_5x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref) 
- { 
-    color_format->alphagrad (outmatrix_ref (out, 5 - 1, 5 / 2 + 0), col, 1, 8); // conflict with other rotations for this odd scale 
-    color_format->alphagrad (outmatrix_ref (out, 5 - 2, 5 / 2 + 1), col, 1, 8); 
-    color_format->alphagrad (outmatrix_ref (out, 5 - 3, 5 / 2 + 2), col, 1, 8); 
-    color_format->alphagrad (outmatrix_ref (out, 4, 3), col, 7, 8); 
-    color_format->alphagrad (outmatrix_ref (out, 3, 4), col, 7, 8); 
-    *outmatrix_ref (out, 4, 4) = col; 
- } 
- static void blend_line_diagonal_6x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref) 
- { 
-    color_format->alphagrad (outmatrix_ref (out, 6 - 1, 6 / 2 + 0), col, 1, 2); 
-    color_format->alphagrad (outmatrix_ref (out, 6 - 2, 6 / 2 + 1), col, 1, 2); 
-    color_format->alphagrad (outmatrix_ref (out, 6 - 3, 6 / 2 + 2), col, 1, 2); 
-    *outmatrix_ref (out, 6 - 2, 6 - 1) = col; 
-    *outmatrix_ref (out, 6 - 1, 6 - 1) = col; 
-    *outmatrix_ref (out, 6 - 1, 6 - 2) = col; 
- } 
-   
- /////////////////////////// 
- // corner scaling functions 
-   
- static void blend_corner_2x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref) 
- { 
-    // model a round corner 
-    color_format->alphagrad (outmatrix_ref (out, 1, 1), col, 21, 100); // exact: 1 - pi/4 = 0.2146018366 
- } 
- static void blend_corner_3x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref) 
- { 
-    // model a round corner 
-    color_format->alphagrad (outmatrix_ref (out, 2, 2), col, 45, 100); // exact: 0.4545939598 
-    //color_format->alphagrad (outmatrix_ref (out, 2, 1), col,  7, 256); // 0.02826017254 -> negligible + avoid conflicts with other rotations for this odd scale 
-    //color_format->alphagrad (outmatrix_ref (out, 1, 2), col,  7, 256); // 0.02826017254 
- } 
- static void blend_corner_4x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref) 
- { 
-    // model a round corner 
-    color_format->alphagrad (outmatrix_ref (out, 3, 3), col, 68, 100); // exact: 0.6848532563 
-    color_format->alphagrad (outmatrix_ref (out, 3, 2), col,  9, 100); // 0.08677704501 
-    color_format->alphagrad (outmatrix_ref (out, 2, 3), col,  9, 100); // 0.08677704501 
- } 
- static void blend_corner_5x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref) 
- { 
-    // model a round corner 
-    color_format->alphagrad (outmatrix_ref (out, 4, 4), col, 86, 100); // exact: 0.8631434088 
-    color_format->alphagrad (outmatrix_ref (out, 4, 3), col, 23, 100); // 0.2306749731 
-    color_format->alphagrad (outmatrix_ref (out, 3, 4), col, 23, 100); // 0.2306749731 
-    //color_format->alphagrad (outmatrix_ref (out, 4, 2), col,  1,  64); // 0.01676812367 -> negligible + avoid conflicts with other rotations for this odd scale 
-    //color_format->alphagrad (outmatrix_ref (out, 2, 4), col,  1,  64); // 0.01676812367 
- } 
- static void blend_corner_6x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref) 
- { 
-    // model a round corner 
-    color_format->alphagrad (outmatrix_ref (out, 5, 5), col, 97, 100); // exact: 0.9711013910 
-    color_format->alphagrad (outmatrix_ref (out, 4, 5), col, 42, 100); // 0.4236372243 
-    color_format->alphagrad (outmatrix_ref (out, 5, 4), col, 42, 100); // 0.4236372243 
-    color_format->alphagrad (outmatrix_ref (out, 5, 3), col,  6, 100); // 0.05652034508 
-    color_format->alphagrad (outmatrix_ref (out, 3, 5), col,  6, 100); // 0.05652034508 
- } 
-   
- ///////////////////////////////////// 
- // scaler objects for various factors 
-   
- static const scaler_t scalers[] = 
- { 
-    { 2, blend_line_shallow_2x, blend_line_steep_2x, blend_line_steep_and_shallow_2x, blend_line_diagonal_2x, blend_corner_2x }, 
-    { 3, blend_line_shallow_3x, blend_line_steep_3x, blend_line_steep_and_shallow_3x, blend_line_diagonal_3x, blend_corner_3x }, 
-    { 4, blend_line_shallow_4x, blend_line_steep_4x, blend_line_steep_and_shallow_4x, blend_line_diagonal_4x, blend_corner_4x }, 
-    { 5, blend_line_shallow_5x, blend_line_steep_5x, blend_line_steep_and_shallow_5x, blend_line_diagonal_5x, blend_corner_5x }, 
-    { 6, blend_line_shallow_6x, blend_line_steep_6x, blend_line_steep_and_shallow_6x, blend_line_diagonal_6x, blend_corner_6x }, 
- }; 
-   
- ///////////////////////////////////////////////////// 
- // alpha gradient functions for various color formats 
-   
- static void alphagrad24 (uint32_t *pixBack, uint32_t pixFront, unsigned int M, unsigned int N) 
- { 
-    // blend front color with opacity M / N over opaque background: http://en.wikipedia.org/wiki/Alpha_compositing#Alpha_blending 
-    *pixBack = ((CALC_COLOR24 (GET_RED   (pixFront), GET_RED   (*pixBack), M, N) << 16) 
-              | (CALC_COLOR24 (GET_GREEN (pixFront), GET_GREEN (*pixBack), M, N) <<  8) 
-              | (CALC_COLOR24 (GET_BLUE  (pixFront), GET_BLUE  (*pixBack), M, N) <<  0)); 
- } 
- static void alphagrad32 (uint32_t *pixBack, uint32_t pixFront, unsigned int M, unsigned int N) 
- { 
-    // find intermediate color between two colors with alpha channels (=> NO alpha blending!!!) 
-    const unsigned int weightFront = GET_ALPHA (pixFront) * M; 
-    const unsigned int weightBack = GET_ALPHA (*pixBack) * (N - M); 
-    const unsigned int weightSum = weightFront + weightBack; 
-    *pixBack = (weightSum == 0 ? 0 : 
-                (((uint8_t) (weightSum / N))                                                                     << 24) 
-                | (CALC_COLOR32 (GET_RED   (pixFront), GET_RED   (*pixBack), weightFront, weightBack, weightSum) << 16) 
-                | (CALC_COLOR32 (GET_GREEN (pixFront), GET_GREEN (*pixBack), weightFront, weightBack, weightSum) <<  8) 
-                | (CALC_COLOR32 (GET_BLUE  (pixFront), GET_BLUE  (*pixBack), weightFront, weightBack, weightSum) <<  0)); 
- } 
-   
- ///////////////////////////////////////////////////// 
- // color distance functions for various color formats 
-   
- static double dist24 (uint32_t pix1, uint32_t pix2) 
- { 
-    // 30% perf boost compared to plain distYCbCr()! 
-    // consumes 64 MB memory; using double is only 2% faster, but takes 128 MB 
-    static float diffToDist[256 * 256 * 256] = { 0 }; 
-    static bool is_initialized = false; 
-    if (!is_initialized) 
-    { 
-       for (uint32_t i = 0; i < 256 * 256 * 256; ++i) //startup time: 114 ms on Intel Core i5 (four cores) 
-       { 
-          const int r_diff = GET_RED (i) * 2 - 0xFF; 
-          const int g_diff = GET_GREEN (i) * 2 - 0xFF; 
-          const int b_diff = GET_BLUE (i) * 2 - 0xFF; 
-   
-          const double k_b = 0.0593; //ITU-R BT.2020 conversion 
-          const double k_r = 0.2627; // 
-          const double k_g = 1 - k_b - k_r; 
-   
-          const double scale_b = 0.5 / (1 - k_b); 
-          const double scale_r = 0.5 / (1 - k_r); 
-   
-          const double y = k_r * r_diff + k_g * g_diff + k_b * b_diff; //[!], analog YCbCr! 
-          const double c_b = scale_b * (b_diff - y); 
-          const double c_r = scale_r * (r_diff - y); 
-   
-          diffToDist [- i ] = (float) (sqrt ((- y  *-  y ) + (- c_b  *-  c_b ) + (- c_r  *-  c_r )));
-       } 
-       is_initialized = true; 
-    } 
-   
-    const int r_diff = (int) GET_RED (pix1) - (int) GET_RED (pix2); 
-    const int g_diff = (int) GET_GREEN (pix1) - (int) GET_GREEN (pix2); 
-    const int b_diff = (int) GET_BLUE (pix1) - (int) GET_BLUE (pix2); 
-   
-    return (diffToDist[  (((r_diff + 0xFF) / 2) << 16) // slightly reduce precision (division by 2) to squeeze value into single byte 
-                       | (((g_diff + 0xFF) / 2) <<  8) 
-                       | (((b_diff + 0xFF) / 2) <<  0)]); 
- } 
- static double dist32 (uint32_t pix1, uint32_t pix2) 
- { 
-    // Requirements for a color distance handling alpha channel: with a1, a2 in [0, 1] 
-    //    1. if a1 = a2, distance should be: a1 * distYCbCr() 
-    //    2. if a1 = 0,  distance should be: a2 * distYCbCr(black, white) = a2 * 255 
-    //    3. if a1 = 1,  ??? maybe: 255 * (1 - a2) + a2 * distYCbCr() 
-    // return MIN (a1, a2) * distYCbCrBuffered(pix1, pix2) + 255 * abs(a1 - a2); 
-    // => following code is 15% faster: 
-    const double d = dist24 (pix1, pix2); 
-    const double a1 = GET_ALPHA (pix1) / 255.0; 
-    const double a2 = GET_ALPHA (pix2) / 255.0; 
-    return (a1 < a2 ? a1 * d + 255 * (a2 - a1) : a2 * d + 255 * (a1 - a2)); 
- } 
-   
- /////////////////////////////////////// 
- // color format objects for various bpp 
-   
- static colorformat_t color_format_24 = { 24, alphagrad24, dist24 }; 
- static colorformat_t color_format_32 = { 32, alphagrad32, dist32 }; 
-   
- ////////////////////////////////////////////////////////// 
- // output matrix reference functions for various rotations 
-   
- static uint32_t *outmatrixref_0   (outmatrix_t *mat, size_t I, size_t J) { return (mat->ptr + I * mat->stride + J); } 
- static uint32_t *outmatrixref_90  (outmatrix_t *mat, size_t I, size_t J) { return (mat->ptr + (mat->size - 1 - J) * mat->stride + I); } 
- static uint32_t *outmatrixref_180 (outmatrix_t *mat, size_t I, size_t J) { return (mat->ptr + (mat->size - 1 - I) * mat->stride + (mat->size - 1 - J)); } 
- static uint32_t *outmatrixref_270 (outmatrix_t *mat, size_t I, size_t J) { return (mat->ptr + J * mat->stride + (mat->size - 1 - I)); } 
-   
-   
- /////////////////////////// 
- // core algorithm functions 
-   
-   
- #ifdef _MSC_VER 
- #define FORCE_INLINE __forceinline 
- #elif defined __GNUC__ 
- #define FORCE_INLINE __attribute__((always_inline)) inline 
- #else 
- #define FORCE_INLINE inline 
- #endif 
-   
-   
- static FORCE_INLINE void preprocess_corners (blendresult_t *result, const kernel_4x4_t *ker, colorformat_t *color_format) 
- { 
-    // detect blend direction 
-    // result: F, G, J, K corners of "GradientType" 
-   
-    // input kernel area naming convention: 
-    // ----------------- 
-    // | A | B | C | D | 
-    // ----|---|---|---| 
-    // | E | F | G | H |   //evaluate the four corners between F, G, J, K 
-    // ----|---|---|---|   //input pixel is at position F 
-    // | I | J | K | L | 
-    // ----|---|---|---| 
-    // | M | N | O | P | 
-    // ----------------- 
-   
-    memset (- result , 0, sizeof (- blendresult_t ));
 
-   
-    if (((ker->f == ker->g) && (ker->j == ker->k)) || ((ker->f == ker->j) && (ker->g == ker->k))) 
-       return; 
-   
-    const int weight = 4; 
-    double jg = color_format->dist (ker->i, ker->f) + color_format->dist (ker->f, ker->c) + color_format->dist (ker->n, ker->k) + color_format->dist (ker->k, ker->h) + weight * color_format->dist (ker->j, ker->g); 
-    double fk = color_format->dist (ker->e, ker->j) + color_format->dist (ker->j, ker->o) + color_format->dist (ker->b, ker->g) + color_format->dist (ker->g, ker->l) + weight * color_format->dist (ker->f, ker->k); 
-   
-    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 
-    { 
-       const bool dominantGradient = XBRZ_CFG_DOMINANT_DIRECTION_THRESHOLD * jg < fk; 
-       if (ker->f != ker->g && ker->f != ker->j) 
-          result->blend_f = dominantGradient ? BLEND_DOMINANT : BLEND_NORMAL; 
-   
-       if (ker->k != ker->j && ker->k != ker->g) 
-          result->blend_k = dominantGradient ? BLEND_DOMINANT : BLEND_NORMAL; 
-    } 
-    else if (fk < jg) 
-    { 
-       const bool dominantGradient = XBRZ_CFG_DOMINANT_DIRECTION_THRESHOLD * fk < jg; 
-       if (ker->j != ker->f && ker->j != ker->k) 
-          result->blend_j = dominantGradient ? BLEND_DOMINANT : BLEND_NORMAL; 
-   
-       if (ker->g != ker->f && ker->g != ker->k) 
-          result->blend_g = dominantGradient ? BLEND_DOMINANT : BLEND_NORMAL; 
-    } 
-   
-    return; 
- } 
-   
-   
- static FORCE_INLINE void blend_pixel (const scaler_t *scaler, const kernel_3x3_t *ker, uint32_t *target, int trgWidth, uint8_t blendInfo, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref) //result of preprocessing all four corners of pixel "e" 
- { 
-    // input kernel area naming convention: 
-    // ------------- 
-    // | A | B | C | 
-    // ----|---|---| 
-    // | D | E | F | // input pixel is at position E 
-    // ----|---|---| 
-    // | G | H | I | 
-    // ------------- 
-   
-    uint32_t 
-       a, b, c, 
-       d, e, f, 
-       g, h, i; 
-    uint8_t blend; 
-   
-    if      (outmatrix_ref == outmatrixref_270) { a = ker->c; b = ker->f; c = ker->i; d = ker->b; e = ker->e; f = ker->h; g = ker->a; h = ker->d; i = ker->g; blend = ((blendInfo << 6) | (blendInfo >> 2)) & 0xff; } 
-    else if (outmatrix_ref == outmatrixref_180) { a = ker->i; b = ker->h; c = ker->g; d = ker->f; e = ker->e; f = ker->d; g = ker->c; h = ker->b; i = ker->a; blend = ((blendInfo << 4) | (blendInfo >> 4)) & 0xff; } 
-    else if (outmatrix_ref == outmatrixref_90)  { a = ker->g; b = ker->d; c = ker->a; d = ker->h; e = ker->e; f = ker->b; g = ker->i; h = ker->f; i = ker->c; blend = ((blendInfo << 2) | (blendInfo >> 6)) & 0xff; } 
-    else                                        { a = ker->a; b = ker->b; c = ker->c; d = ker->d; e = ker->e; f = ker->f; g = ker->g; h = ker->h; i = ker->i; blend = blendInfo; } // blendInfo here is equivalent to ((blendInfo << 0) | (blendInfo >> 8)) & 0xff 
-   
-    if (getBottomR (blend) >= BLEND_NORMAL) 
-    { 
-       uint32_t px; 
-       bool doLineBlend; 
-   
-       if (getBottomR (blend) >= BLEND_DOMINANT) 
-          doLineBlend = true; 
-       else if (getTopR (blend) != BLEND_NONE && (color_format->dist (e, g) >= XBRZ_CFG_EQUAL_COLOR_TOLERANCE)) //but support double-blending for 90° corners 
-          doLineBlend = false; // make sure there is no second blending in an adjacent rotation for this pixel: handles insular pixels, mario eyes 
-       else if (getBottomL (blend) != BLEND_NONE && (color_format->dist (e, c) >= XBRZ_CFG_EQUAL_COLOR_TOLERANCE)) 
-          doLineBlend = false; // make sure there is no second blending in an adjacent rotation for this pixel: handles insular pixels, mario eyes 
-       else if ((color_format->dist (e, i) >= XBRZ_CFG_EQUAL_COLOR_TOLERANCE) 
-                && (color_format->dist (g, h) < XBRZ_CFG_EQUAL_COLOR_TOLERANCE) 
-                && (color_format->dist (h, i) < XBRZ_CFG_EQUAL_COLOR_TOLERANCE) 
-                && (color_format->dist (i, f) < XBRZ_CFG_EQUAL_COLOR_TOLERANCE) 
-                && (color_format->dist (f, c) < XBRZ_CFG_EQUAL_COLOR_TOLERANCE)) 
-          doLineBlend = false; // no full blending for L-shapes; blend corner only (handles "mario mushroom eyes") 
-       else 
-          doLineBlend = true; 
-   
-       outmatrix_t out; 
-       out.size = scaler->factor; 
-       out.ptr = target; 
-       out.stride = trgWidth; 
-   
-       px = (color_format->dist (e, f) <= color_format->dist (e, h) ? f : h); // choose most similar color 
-   
-       if (doLineBlend) 
-       { 
-          const double fg = color_format->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 
-          const double hc = color_format->dist (h, c); 
-          const bool haveShallowLine = (XBRZ_CFG_STEEP_DIRECTION_THRESHOLD * fg <= hc) && (e != g) && (d != g); 
-          const bool haveSteepLine   = (XBRZ_CFG_STEEP_DIRECTION_THRESHOLD * hc <= fg) && (e != c) && (b != c); 
-   
-          if (haveShallowLine) 
-          { 
-             if (haveSteepLine) 
-                scaler->blend_line_steep_and_shallow (px, &out, color_format, outmatrix_ref); 
-             else 
-                scaler->blend_line_shallow (px, &out, color_format, outmatrix_ref); 
-          } 
-          else 
-          { 
-             if (haveSteepLine) 
-                scaler->blend_line_steep (px, &out, color_format, outmatrix_ref); 
-             else 
-                scaler->blend_line_diagonal (px, &out, color_format, outmatrix_ref); 
-          } 
-       } 
-       else 
-          scaler->blend_corner (px, &out, color_format, outmatrix_ref); 
-    } 
- } 
-   
-   
- static void scale_image (const scaler_t *scaler, const uint32_t *src, uint32_t *trg, int srcWidth, int srcHeight, int yFirst, int yLast, colorformat_t *color_format) 
- { 
-    yFirst = MAX (yFirst, 0); 
-    yLast = MIN (yLast, srcHeight); 
-    if (yFirst >= yLast || srcWidth <= 0) 
-       return; 
-   
-    const int trgWidth = srcWidth * scaler->factor; 
-   
-    // "use" space at the end of the image as temporary buffer for "on the fly preprocessing": we even could use larger area of 
-    // "sizeof(uint32_t) * srcWidth * (yLast - yFirst)" bytes without risk of accidental overwriting before accessing 
-    const int bufferSize = srcWidth; 
-    uint8_t *preProcBuffer = (uint8_t *) (trg + yLast * scaler->factor * trgWidth) - bufferSize; 
-    memset (- preProcBuffer , 0,-  bufferSize );
 
-   
-    // initialize preprocessing buffer for first row of current stripe: detect upper left and right corner blending 
-    // this cannot be optimized for adjacent processing stripes; we must not allow for a memory race condition! 
-    if (yFirst > 0) 
-    { 
-       const int y = yFirst - 1; 
-   
-       const uint32_t *s_m1 = src + srcWidth * MAX (y - 1, 0); 
-       const uint32_t *s_0 = src + srcWidth * y; //center line 
-       const uint32_t *s_p1 = src + srcWidth * MIN (y + 1, srcHeight - 1); 
-       const uint32_t *s_p2 = src + srcWidth * MIN (y + 2, srcHeight - 1); 
-   
-       for (int x = 0; x < srcWidth; ++x) 
-       { 
-          blendresult_t res; 
-          const int x_m1 = MAX (x - 1, 0); 
-          const int x_p1 = MIN (x + 1, srcWidth - 1); 
-          const int x_p2 = MIN (x + 2, srcWidth - 1); 
-   
-          kernel_4x4_t ker; // perf: initialization is negligible 
-          ker.a = s_m1[x_m1]; ker.b = s_m1[x]; ker.c = s_m1[x_p1]; ker.d = s_m1[x_p2]; // read sequentially from memory as far as possible 
-          ker.e = s_0[x_m1];  ker.f = s_0[x];  ker.g = s_0[x_p1];  ker.h = s_0[x_p2]; 
-          ker.i = s_p1[x_m1]; ker.j = s_p1[x]; ker.k = s_p1[x_p1]; ker.l = s_p1[x_p2]; 
-          ker.m = s_p2[x_m1]; ker.n = s_p2[x]; ker.o = s_p2[x_p1]; ker.p = s_p2[x_p2]; 
-   
-          preprocess_corners (&res, &ker, color_format); 
-   
-          // preprocessing blend result: 
-          // --------- 
-          // | F | G |   // evalute corner between F, G, J, K 
-          // ----|---|   // input pixel is at position F 
-          // | J | K | 
-          // --------- 
-   
-          setTopR (&preProcBuffer[x], res.blend_j); 
-          if (x + 1 < bufferSize) 
-             setTopL (&preProcBuffer[x + 1], res.blend_k); 
-       } 
-    } 
-    //------------------------------------------------------------------------------------ 
-   
-    for (int y = yFirst; y < yLast; ++y) 
-    { 
-       uint32_t *out = trg + scaler->factor * y * trgWidth; // consider MT "striped" access 
-   
-       const uint32_t* s_m1 = src + srcWidth * MAX (y - 1, 0); 
-       const uint32_t* s_0 = src + srcWidth * y; // center line 
-       const uint32_t* s_p1 = src + srcWidth * MIN (y + 1, srcHeight - 1); 
-       const uint32_t* s_p2 = src + srcWidth * MIN (y + 2, srcHeight - 1); 
-   
-       uint8_t blend_xy1 = 0; // corner blending for current (x, y + 1) position 
-   
-       for (int x = 0; x < srcWidth; ++x, out += scaler->factor) 
-       { 
-          // all those bounds checks have only insignificant impact on performance! 
-          const int x_m1 = MAX (x - 1, 0); // perf: prefer array indexing to additional pointers! 
-          const int x_p1 = MIN (x + 1, srcWidth - 1); 
-          const int x_p2 = MIN (x + 2, srcWidth - 1); 
-   
-          kernel_4x4_t ker4; // perf: initialization is negligible 
-          ker4.a = s_m1[x_m1]; ker4.b = s_m1[x]; ker4.c = s_m1[x_p1]; ker4.d = s_m1[x_p2]; // read sequentially from memory as far as possible 
-          ker4.e = s_0[x_m1];  ker4.f = s_0[x];  ker4.g = s_0[x_p1];  ker4.h = s_0[x_p2]; 
-          ker4.i = s_p1[x_m1]; ker4.j = s_p1[x]; ker4.k = s_p1[x_p1]; ker4.l = s_p1[x_p2]; 
-          ker4.m = s_p2[x_m1]; ker4.n = s_p2[x]; ker4.o = s_p2[x_p1]; ker4.p = s_p2[x_p2]; 
-   
-          // evaluate the four corners on bottom-right of current pixel 
-          uint8_t blend_xy = 0; // for current (x, y) position 
-          { 
-             blendresult_t res; 
-             preprocess_corners (&res, &ker4, color_format); 
-   
-             // preprocessing blend result: 
-             // --------- 
-             // | F | G |   // evalute corner between F, G, J, K 
-             // ----|---|   // current input pixel is at position F 
-             // | J | K | 
-             // --------- 
-   
-             blend_xy = preProcBuffer[x]; 
-             setBottomR (&blend_xy, res.blend_f); // all four corners of (x, y) have been determined at this point due to processing sequence! 
-   
-             setTopR (&blend_xy1, res.blend_j); // set 2nd known corner for (x, y + 1) 
-             preProcBuffer[x] = blend_xy1; // store on current buffer position for use on next row 
-   
-             blend_xy1 = 0; 
-             setTopL (&blend_xy1, res.blend_k); // set 1st known corner for (x + 1, y + 1) and buffer for use on next column 
-   
-             if (x + 1 < bufferSize) // set 3rd known corner for (x + 1, y) 
-                setBottomL (&preProcBuffer[x + 1], res.blend_g); 
-          } 
-   
-          // fill block of size scale * scale with the given color 
-          uint32_t *blk = out; 
-          for (int _blk_y = 0; _blk_y < scaler->factor; ++_blk_y, blk = (uint32_t *) BYTE_ADVANCE (blk, trgWidth * sizeof (uint32_t))) 
-             for (int _blk_x = 0; _blk_x < scaler->factor; ++_blk_x) 
-                blk[_blk_x] = ker4.f; 
-   
-          // place *after* preprocessing step, to not overwrite the results while processing the the last pixel! 
-   
-          // blend four corners of current pixel 
-          if (blend_xy != 0) // good 5% perf-improvement 
-          { 
-             kernel_3x3_t ker3; // perf: initialization is negligible 
-             ker3.a = ker4.a; ker3.b = ker4.b; ker3.c = ker4.c; 
-             ker3.d = ker4.e; ker3.e = ker4.f; ker3.f = ker4.g; 
-             ker3.g = ker4.i; ker3.h = ker4.j; ker3.i = ker4.k; 
-   
-             blend_pixel (scaler, &ker3, out, trgWidth, blend_xy, color_format, outmatrixref_0); 
-             blend_pixel (scaler, &ker3, out, trgWidth, blend_xy, color_format, outmatrixref_90); 
-             blend_pixel (scaler, &ker3, out, trgWidth, blend_xy, color_format, outmatrixref_180); 
-             blend_pixel (scaler, &ker3, out, trgWidth, blend_xy, color_format, outmatrixref_270); 
-          } 
-       } 
-    } 
- } 
-   
-   
- ///////////////////// 
- // exported functions 
-   
-   
- void nearest_neighbor_scale (const uint32_t *src, int srcWidth, int srcHeight, uint32_t *trg, int trgWidth, int trgHeight) 
- { 
-    int srcPitch = srcWidth * sizeof (uint32_t); 
-    int trgPitch = trgWidth * sizeof (uint32_t); 
-    int yFirst; 
-    int yLast; 
-   
- #if 0 // going over source image - fast for upscaling, since source is read only once 
-    yFirst = 0; 
-    yLast = MIN (trgHeight, srcHeight); 
-   
-    if (yFirst >= yLast || trgWidth <= 0 || trgHeight <= 0) 
-       return; // consistency check 
-   
-    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 yTrg_first = (y      * trgHeight + srcHeight - 1) / srcHeight; // = ceil(y * trgHeight / srcHeight) 
-       const int yTrg_last = ((y + 1) * trgHeight + srcHeight - 1) / srcHeight; // = ceil(((y + 1) * trgHeight) / srcHeight) 
-       const int blockHeight = yTrg_last - yTrg_first; 
-   
-       if (blockHeight > 0) 
-       { 
-          const uint32_t *srcLine = (const uint32_t *) BYTE_ADVANCE (src, y * srcPitch); 
-          /**/  uint32_t *trgLine = (uint32_t *) BYTE_ADVANCE (trg, yTrg_first * trgPitch); 
-          int xTrg_first = 0; 
-   
-          for (int x = 0; x < srcWidth; ++x) 
-          { 
-             const int xTrg_last = ((x + 1) * trgWidth + srcWidth - 1) / srcWidth; 
-             const int blockWidth = xTrg_last - xTrg_first; 
-             if (blockWidth > 0) 
-             { 
-                const uint32_t trgColor = srcLine[x]; 
-                uint32_t *blkLine = trgLine; 
-   
-                xTrg_first = xTrg_last; 
-   
-                for (int blk_y = 0; blk_y < blockHeight; ++blk_y, blkLine = (uint32_t *) BYTE_ADVANCE (blkLine, trgPitch)) 
-                   for (int blk_x = 0; blk_x < blockWidth; ++blk_x) 
-                      blkLine[blk_x] = trgColor; 
-   
-                trgLine += blockWidth; 
-             } 
-          } 
-       } 
-    } 
- #else // going over target image - slow for upscaling, since source is read multiple times missing out on cache! Fast for similar image sizes! 
-    yFirst = 0; 
-    yLast = trgHeight; 
-   
-    if (yFirst >= yLast || srcHeight <= 0 || srcWidth <= 0) 
-       return; // consistency check 
-   
-    for (int y = yFirst; y < yLast; ++y) 
-    { 
-       /**/  uint32_t *trgLine = (uint32_t *) BYTE_ADVANCE (trg, y * trgPitch); 
-       const int ySrc = srcHeight * y / trgHeight; 
-       const uint32_t *srcLine = (const uint32_t *) BYTE_ADVANCE (src, ySrc * srcPitch); 
-       for (int x = 0; x < trgWidth; ++x) 
-       { 
-          const int xSrc = srcWidth * x / trgWidth; 
-          trgLine[x] = srcLine[xSrc]; 
-       } 
-    } 
- #endif // going over source or target 
-   
-    return; 
- } 
-   
-   
- void xbrz_scale (size_t factor, const uint32_t *src, uint32_t *trg, int srcWidth, int srcHeight, bool has_alpha_channel) 
- { 
-    if ((factor < 2) || (factor > 6)) 
-       return; // consistency check 
-   
-    scale_image (&scalers[factor - 2], src, trg, srcWidth, srcHeight, 0, srcHeight, (has_alpha_channel ? &color_format_32 : &color_format_24)); 
-    return; 
- } 
-