Subversion Repositories Games.Prince of Persia

Rev

Rev 5 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5 Rev 6
Line 1... Line -...
1
// ****************************************************************************
-
 
2
// * This file is part of the HqMAME 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 MAME library (or with modified *
-
 
8
// * versions of MAME that use the same license as MAME), and distribute      *
-
 
9
// * linked combinations including the two. You must obey the GNU General     *
-
 
10
// * Public License in all respects for all of the code used other than MAME. *
-
 
11
// * If you modify this file, you may extend this exception to your version   *
-
 
12
// * of the file, but you are not obligated to do so. If you do not wish to   *
-
 
13
// * do so, delete this exception statement from your version.                *
-
 
14
// ****************************************************************************
-
 
15
 
-
 
16
// -------------------------------------------------------------------------
1
// -------------------------------------------------------------------------
17
// | xBRZ: "Scale by rules" - high quality image upscaling filter by Zenju |
2
// | xBRZ: "Scale by rules" - high quality image upscaling filter by Zenju |
18
// -------------------------------------------------------------------------
3
// -------------------------------------------------------------------------
19
// using a modified approach of xBR:
4
// using a modified approach of xBR:
20
// http://board.byuu.org/viewtopic.php?f=10&t=2248
5
// http://board.byuu.org/viewtopic.php?f=10&t=2248
Line 37... Line 22...
37
//                - 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
22
//                - 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
38
 
23
 
39
 
24
 
40
#include <stddef.h> // for size_t
25
#include <stddef.h> // for size_t
41
#include <stdint.h> // for uint32_t
26
#include <stdint.h> // for uint32_t
-
 
27
#include <stdbool.h> // for bool
42
#include <memory.h> // for memset()
28
#include <memory.h> // for memset()
43
#include <limits.h>
29
#include <limits.h>
44
#include <math.h>
30
#include <math.h>
45
 
31
 
46
 
32
 
47
#ifdef __cplusplus
-
 
48
#define EXTERN_C extern "C"
33
// prototypes of exported functions
49
#else // !__cplusplus
34
void xbrz_scale (size_t factor, const uint32_t *src, uint32_t *trg, int srcWidth, int srcHeight, bool has_alpha_channel);
50
#define EXTERN_C
-
 
51
#endif // __cplusplus
35
void nearest_neighbor_scale (const uint32_t *src, int srcWidth, int srcHeight, uint32_t *trg, int trgWidth, int trgHeight);
52
 
36
 
53
 
37
 
54
#ifdef _MSC_VER
-
 
55
#define FORCE_INLINE __forceinline
-
 
56
#elif defined __GNUC__
-
 
57
#define FORCE_INLINE __attribute__((always_inline)) inline
-
 
58
#else
-
 
59
#define FORCE_INLINE inline
-
 
60
#endif
-
 
61
 
-
 
62
 
-
 
63
// scaler configuration
38
// algorithm configuration
64
#define XBRZ_CFG_LUMINANCE_WEIGHT 1
39
#define XBRZ_CFG_LUMINANCE_WEIGHT 1
65
#define XBRZ_CFG_EQUAL_COLOR_TOLERANCE 30
40
#define XBRZ_CFG_EQUAL_COLOR_TOLERANCE 30
66
#define XBRZ_CFG_DOMINANT_DIRECTION_THRESHOLD 3.6
41
#define XBRZ_CFG_DOMINANT_DIRECTION_THRESHOLD 3.6
67
#define XBRZ_CFG_STEEP_DIRECTION_THRESHOLD 2.2
42
#define XBRZ_CFG_STEEP_DIRECTION_THRESHOLD 2.2
68
 
43
 
69
 
44
 
70
// slice types
45
// blend types
71
#define XBRZ_SLICETYPE_SOURCE 1
46
#define BLEND_NONE 0
72
#define XBRZ_SLICETYPE_TARGET 2
47
#define BLEND_NORMAL 1 // a normal indication to blend
-
 
48
#define BLEND_DOMINANT 2 // a strong indication to blend
73
 
49
 
74
 
50
 
75
// handy macros
51
// handy macros
76
#define GET_BYTE(val,byteno) ((unsigned char) (((val) >> ((byteno) << 3)) & 0xff))
-
 
77
#define GET_BLUE(val)  GET_BYTE (val, 0)
-
 
78
#define GET_GREEN(val) GET_BYTE (val, 1)
-
 
79
#define GET_RED(val)   GET_BYTE (val, 2)
-
 
80
#define GET_ALPHA(val) GET_BYTE (val, 3)
-
 
81
#define CALC_COLOR24(colFront,colBack,M,N) (unsigned char) ((((unsigned char) (colFront)) * ((unsigned int) (M)) + ((unsigned char) (colBack)) * (((unsigned int) (N)) - ((unsigned int) (M)))) / ((unsigned int) (N)))
-
 
82
#define CALC_COLOR32(colFront,colBack,weightFront,weightBack,weightSum) ((unsigned char) ((((unsigned char) (colFront)) * ((unsigned int) (weightFront)) + ((unsigned char) (colBack)) * ((unsigned int) (weightBack))) / ((unsigned int) (weightSum))))
-
 
83
#define BYTE_ADVANCE(buffer,offset) (((char *) buffer) + (offset))
-
 
84
#ifndef MIN
52
#ifndef MIN
85
#define MIN(a,b) ((a) < (b) ? (a) : (b))
53
#define MIN(a,b) ((a) < (b) ? (a) : (b))
86
#endif // MIN
54
#endif // MIN
87
#ifndef MAX
55
#ifndef MAX
88
#define MAX(a,b) ((a) > (b) ? (a) : (b))
56
#define MAX(a,b) ((a) > (b) ? (a) : (b))
89
#endif // MAX
57
#endif // MAX
-
 
58
#define GET_BYTE(val,byteno) ((uint8_t) (((val) >> ((byteno) << 3)) & 0xff))
-
 
59
#define GET_BLUE(val)  GET_BYTE (val, 0)
-
 
60
#define GET_GREEN(val) GET_BYTE (val, 1)
-
 
61
#define GET_RED(val)   GET_BYTE (val, 2)
-
 
62
#define GET_ALPHA(val) GET_BYTE (val, 3)
-
 
63
#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)))
-
 
64
#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))))
-
 
65
#define BYTE_ADVANCE(buffer,offset) (((char *) buffer) + (offset))
90
 
66
 
91
 
67
 
92
enum BlendType
68
// compress four blend types into a single byte
93
{
-
 
94
   BLEND_NONE = 0,
69
#define getTopL(b)    ((uint8_t) (0x3 & ((uint8_t) (b) >> 0)))
95
   BLEND_NORMAL,   //a normal indication to blend
70
#define getTopR(b)    ((uint8_t) (0x3 & ((uint8_t) (b) >> 2)))
-
 
71
#define getBottomR(b) ((uint8_t) (0x3 & ((uint8_t) (b) >> 4)))
96
   BLEND_DOMINANT, //a strong indication to blend
72
#define getBottomL(b) ((uint8_t) (0x3 & ((uint8_t) (b) >> 6)))
-
 
73
#define setTopL(b,blend_type)    *(b) |= (((uint8_t) (blend_type)) << 0) // buffer is assumed to be initialized before preprocessing!
-
 
74
#define setTopR(b,blend_type)    *(b) |= (((uint8_t) (blend_type)) << 2)
-
 
75
#define setBottomR(b,blend_type) *(b) |= (((uint8_t) (blend_type)) << 4)
97
   //attention: BlendType must fit into the value range of 2 bit!!!
76
#define setBottomL(b,blend_type) *(b) |= (((uint8_t) (blend_type)) << 6)
98
};
-
 
99
 
77
 
100
 
78
 
101
typedef struct blendresult_s
79
typedef struct blendresult_s
102
{
80
{
103
   BlendType
81
   uint8_t
104
      /**/blend_f, blend_g,
82
      blend_f, blend_g,
105
      /**/blend_j, blend_k;
83
      blend_j, blend_k;
106
} blendresult_t;
84
} blendresult_t;
107
 
85
 
108
 
86
 
109
typedef struct kernel_3x3_s
87
typedef struct kernel_3x3_s
110
{
88
{
111
   uint32_t
89
   uint32_t
112
      /**/a, b, c,
90
      a, b, c,
113
      /**/d, e, f,
91
      d, e, f,
114
      /**/g, h, i;
92
      g, h, i;
115
} kernel_3x3_t;
93
} kernel_3x3_t;
116
 
94
 
117
 
95
 
118
typedef struct kernel_4x4_s //kernel for preprocessing step
96
typedef struct kernel_4x4_s //kernel for preprocessing step
119
{
97
{
120
   uint32_t
98
   uint32_t
121
      /**/a, b, c, d,
99
      a, b, c, d,
122
      /**/e, f, g, h,
100
      e, f, g, h,
123
      /**/i, j, k, l,
101
      i, j, k, l,
124
      /**/m, n, o, p;
102
      m, n, o, p;
125
} kernel_4x4_t;
103
} kernel_4x4_t;
-
 
104
 
-
 
105
 
-
 
106
typedef struct colorformat_s
-
 
107
{
-
 
108
   int bpp;
-
 
109
   void (*alphagrad) (uint32_t *pixBack, uint32_t pixFront, unsigned int M, unsigned int N);
-
 
110
   double (*dist) (uint32_t pix1, uint32_t pix2);
-
 
111
} colorformat_t;
126
 
112
 
127
 
113
 
128
typedef struct outmatrix_s
114
typedef struct outmatrix_s
129
{
115
{
130
   size_t size;
116
   size_t size;
131
   uint32_t* ptr;
117
   uint32_t* ptr;
132
   int stride;
118
   int stride;
133
   int rotDeg; // either 0, 90, 180 or 270
-
 
134
} outmatrix_t;
119
} outmatrix_t;
135
 
120
 
136
 
121
 
137
static void outmatrix_create (outmatrix_t *mat, size_t size, uint32_t *ptr, int stride, int rotDeg) //access matrix area, top-left at position "out" for image with given width
-
 
138
{
-
 
139
   mat->size = size;
-
 
140
   mat->ptr = ptr;
-
 
141
   mat->stride = stride;
-
 
142
   mat->rotDeg = rotDeg;
-
 
143
}
-
 
144
 
-
 
145
 
-
 
146
static uint32_t *outmatrix_ref (outmatrix_t *mat, size_t I, size_t J)
122
typedef uint32_t *(outmatrixreffunc_t) (outmatrix_t *mat, size_t I, size_t J);
147
{
-
 
148
   size_t I_old;
-
 
149
   size_t J_old;
-
 
150
   // calculate input matrix coordinates after rotation: (i, j) = (row, col) indices, N = size of (square) matrix
-
 
151
   if (mat->rotDeg == 270) { I_old = J;                 J_old = mat->size - 1 - I; }
-
 
152
   else if (mat->rotDeg == 180) { I_old = mat->size - 1 - I; J_old = mat->size - 1 - J; }
-
 
153
   else if (mat->rotDeg == 90) { I_old = mat->size - 1 - J; J_old = I; }
-
 
154
   else { I_old = I;                 J_old = J; }
-
 
155
 
-
 
156
   return (mat->ptr + I_old * mat->stride + J_old);
-
 
157
}
-
 
158
 
-
 
159
 
-
 
160
typedef void (alphagrad_func) (uint32_t *pixBack, uint32_t pixFront, unsigned int M, unsigned int N);
-
 
161
typedef double (dist_func) (uint32_t pix1, uint32_t pix2);
-
 
162
 
123
 
163
 
124
 
164
typedef struct scaler_s
125
typedef struct scaler_s
165
{
126
{
166
   int factor;
127
   int factor;
167
   void (*blend_line_shallow) (uint32_t col, outmatrix_t *out, alphagrad_func alphagrad);
128
   void (*blend_line_shallow)           (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref);
168
   void (*blend_line_steep) (uint32_t col, outmatrix_t *out, alphagrad_func alphagrad);
129
   void (*blend_line_steep)             (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref);
169
   void (*blend_line_steep_and_shallow) (uint32_t col, outmatrix_t *out, alphagrad_func alphagrad);
130
   void (*blend_line_steep_and_shallow) (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref);
170
   void (*blend_line_diagonal) (uint32_t col, outmatrix_t *out, alphagrad_func alphagrad);
131
   void (*blend_line_diagonal)          (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref);
171
   void (*blend_corner) (uint32_t col, outmatrix_t *out, alphagrad_func alphagrad);
132
   void (*blend_corner)                 (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref);
172
} scaler_t;
133
} scaler_t;
173
 
134
 
174
 
135
 
175
/////////////////////////////////
136
/////////////////////////////////
176
// shallow line scaling functions
137
// shallow line scaling functions
177
 
138
 
178
static void blend_line_shallow_2x (uint32_t col, outmatrix_t *out, alphagrad_func alphagrad)
139
static void blend_line_shallow_2x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref)
179
{
140
{
180
   alphagrad (outmatrix_ref (out, 2 - 1, 0), col, 1, 4);
141
   color_format->alphagrad (outmatrix_ref (out, 2 - 1, 0), col, 1, 4);
181
   alphagrad (outmatrix_ref (out, 2 - 1, 1), col, 3, 4);
142
   color_format->alphagrad (outmatrix_ref (out, 2 - 1, 1), col, 3, 4);
182
}
143
}
183
static void blend_line_shallow_3x (uint32_t col, outmatrix_t *out, alphagrad_func alphagrad)
144
static void blend_line_shallow_3x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref)
184
{
145
{
185
   alphagrad (outmatrix_ref (out, 3 - 1, 0), col, 1, 4);
146
   color_format->alphagrad (outmatrix_ref (out, 3 - 1, 0), col, 1, 4);
186
   alphagrad (outmatrix_ref (out, 3 - 2, 2), col, 1, 4);
147
   color_format->alphagrad (outmatrix_ref (out, 3 - 2, 2), col, 1, 4);
187
   alphagrad (outmatrix_ref (out, 3 - 1, 1), col, 3, 4);
148
   color_format->alphagrad (outmatrix_ref (out, 3 - 1, 1), col, 3, 4);
188
   *outmatrix_ref (out, 3 - 1, 2) = col;
149
   *outmatrix_ref (out, 3 - 1, 2) = col;
189
}
150
}
190
static void blend_line_shallow_4x (uint32_t col, outmatrix_t *out, alphagrad_func alphagrad)
151
static void blend_line_shallow_4x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref)
191
{
152
{
192
   alphagrad (outmatrix_ref (out, 4 - 1, 0), col, 1, 4);
153
   color_format->alphagrad (outmatrix_ref (out, 4 - 1, 0), col, 1, 4);
193
   alphagrad (outmatrix_ref (out, 4 - 2, 2), col, 1, 4);
154
   color_format->alphagrad (outmatrix_ref (out, 4 - 2, 2), col, 1, 4);
194
   alphagrad (outmatrix_ref (out, 4 - 1, 1), col, 3, 4);
155
   color_format->alphagrad (outmatrix_ref (out, 4 - 1, 1), col, 3, 4);
195
   alphagrad (outmatrix_ref (out, 4 - 2, 3), col, 3, 4);
156
   color_format->alphagrad (outmatrix_ref (out, 4 - 2, 3), col, 3, 4);
196
   *outmatrix_ref (out, 4 - 1, 2) = col;
157
   *outmatrix_ref (out, 4 - 1, 2) = col;
197
   *outmatrix_ref (out, 4 - 1, 3) = col;
158
   *outmatrix_ref (out, 4 - 1, 3) = col;
198
}
159
}
199
static void blend_line_shallow_5x (uint32_t col, outmatrix_t *out, alphagrad_func alphagrad)
160
static void blend_line_shallow_5x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref)
200
{
161
{
201
   alphagrad (outmatrix_ref (out, 5 - 1, 0), col, 1, 4);
162
   color_format->alphagrad (outmatrix_ref (out, 5 - 1, 0), col, 1, 4);
202
   alphagrad (outmatrix_ref (out, 5 - 2, 2), col, 1, 4);
163
   color_format->alphagrad (outmatrix_ref (out, 5 - 2, 2), col, 1, 4);
203
   alphagrad (outmatrix_ref (out, 5 - 3, 4), col, 1, 4);
164
   color_format->alphagrad (outmatrix_ref (out, 5 - 3, 4), col, 1, 4);
204
   alphagrad (outmatrix_ref (out, 5 - 1, 1), col, 3, 4);
165
   color_format->alphagrad (outmatrix_ref (out, 5 - 1, 1), col, 3, 4);
205
   alphagrad (outmatrix_ref (out, 5 - 2, 3), col, 3, 4);
166
   color_format->alphagrad (outmatrix_ref (out, 5 - 2, 3), col, 3, 4);
206
   *outmatrix_ref (out, 5 - 1, 2) = col;
167
   *outmatrix_ref (out, 5 - 1, 2) = col;
207
   *outmatrix_ref (out, 5 - 1, 3) = col;
168
   *outmatrix_ref (out, 5 - 1, 3) = col;
208
   *outmatrix_ref (out, 5 - 1, 4) = col;
169
   *outmatrix_ref (out, 5 - 1, 4) = col;
209
   *outmatrix_ref (out, 5 - 2, 4) = col;
170
   *outmatrix_ref (out, 5 - 2, 4) = col;
210
}
171
}
211
static void blend_line_shallow_6x (uint32_t col, outmatrix_t *out, alphagrad_func alphagrad)
172
static void blend_line_shallow_6x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref)
212
{
173
{
213
   alphagrad (outmatrix_ref (out, 6 - 1, 0), col, 1, 4);
174
   color_format->alphagrad (outmatrix_ref (out, 6 - 1, 0), col, 1, 4);
214
   alphagrad (outmatrix_ref (out, 6 - 2, 2), col, 1, 4);
175
   color_format->alphagrad (outmatrix_ref (out, 6 - 2, 2), col, 1, 4);
215
   alphagrad (outmatrix_ref (out, 6 - 3, 4), col, 1, 4);
176
   color_format->alphagrad (outmatrix_ref (out, 6 - 3, 4), col, 1, 4);
216
   alphagrad (outmatrix_ref (out, 6 - 1, 1), col, 3, 4);
177
   color_format->alphagrad (outmatrix_ref (out, 6 - 1, 1), col, 3, 4);
217
   alphagrad (outmatrix_ref (out, 6 - 2, 3), col, 3, 4);
178
   color_format->alphagrad (outmatrix_ref (out, 6 - 2, 3), col, 3, 4);
218
   alphagrad (outmatrix_ref (out, 6 - 3, 5), col, 3, 4);
179
   color_format->alphagrad (outmatrix_ref (out, 6 - 3, 5), col, 3, 4);
219
   *outmatrix_ref (out, 6 - 1, 2) = col;
180
   *outmatrix_ref (out, 6 - 1, 2) = col;
220
   *outmatrix_ref (out, 6 - 1, 3) = col;
181
   *outmatrix_ref (out, 6 - 1, 3) = col;
221
   *outmatrix_ref (out, 6 - 1, 4) = col;
182
   *outmatrix_ref (out, 6 - 1, 4) = col;
222
   *outmatrix_ref (out, 6 - 1, 5) = col;
183
   *outmatrix_ref (out, 6 - 1, 5) = col;
223
   *outmatrix_ref (out, 6 - 2, 4) = col;
184
   *outmatrix_ref (out, 6 - 2, 4) = col;
Line 225... Line 186...
225
}
186
}
226
 
187
 
227
///////////////////////////////
188
///////////////////////////////
228
// steep line scaling functions
189
// steep line scaling functions
229
 
190
 
230
static void blend_line_steep_2x (uint32_t col, outmatrix_t *out, alphagrad_func alphagrad)
191
static void blend_line_steep_2x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref)
231
{
192
{
232
   alphagrad (outmatrix_ref (out, 0, 2 - 1), col, 1, 4);
193
   color_format->alphagrad (outmatrix_ref (out, 0, 2 - 1), col, 1, 4);
233
   alphagrad (outmatrix_ref (out, 1, 2 - 1), col, 3, 4);
194
   color_format->alphagrad (outmatrix_ref (out, 1, 2 - 1), col, 3, 4);
234
}
195
}
235
static void blend_line_steep_3x (uint32_t col, outmatrix_t *out, alphagrad_func alphagrad)
196
static void blend_line_steep_3x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref)
236
{
197
{
237
   alphagrad (outmatrix_ref (out, 0, 3 - 1), col, 1, 4);
198
   color_format->alphagrad (outmatrix_ref (out, 0, 3 - 1), col, 1, 4);
238
   alphagrad (outmatrix_ref (out, 2, 3 - 2), col, 1, 4);
199
   color_format->alphagrad (outmatrix_ref (out, 2, 3 - 2), col, 1, 4);
239
   alphagrad (outmatrix_ref (out, 1, 3 - 1), col, 3, 4);
200
   color_format->alphagrad (outmatrix_ref (out, 1, 3 - 1), col, 3, 4);
240
   *outmatrix_ref (out, 2, 3 - 1) = col;
201
   *outmatrix_ref (out, 2, 3 - 1) = col;
241
}
202
}
242
static void blend_line_steep_4x (uint32_t col, outmatrix_t *out, alphagrad_func alphagrad)
203
static void blend_line_steep_4x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref)
243
{
204
{
244
   alphagrad (outmatrix_ref (out, 0, 4 - 1), col, 1, 4);
205
   color_format->alphagrad (outmatrix_ref (out, 0, 4 - 1), col, 1, 4);
245
   alphagrad (outmatrix_ref (out, 2, 4 - 2), col, 1, 4);
206
   color_format->alphagrad (outmatrix_ref (out, 2, 4 - 2), col, 1, 4);
246
   alphagrad (outmatrix_ref (out, 1, 4 - 1), col, 3, 4);
207
   color_format->alphagrad (outmatrix_ref (out, 1, 4 - 1), col, 3, 4);
247
   alphagrad (outmatrix_ref (out, 3, 4 - 2), col, 3, 4);
208
   color_format->alphagrad (outmatrix_ref (out, 3, 4 - 2), col, 3, 4);
248
   *outmatrix_ref (out, 2, 4 - 1) = col;
209
   *outmatrix_ref (out, 2, 4 - 1) = col;
249
   *outmatrix_ref (out, 3, 4 - 1) = col;
210
   *outmatrix_ref (out, 3, 4 - 1) = col;
250
}
211
}
251
static void blend_line_steep_5x (uint32_t col, outmatrix_t *out, alphagrad_func alphagrad)
212
static void blend_line_steep_5x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref)
252
{
213
{
253
   alphagrad (outmatrix_ref (out, 0, 5 - 1), col, 1, 4);
214
   color_format->alphagrad (outmatrix_ref (out, 0, 5 - 1), col, 1, 4);
254
   alphagrad (outmatrix_ref (out, 2, 5 - 2), col, 1, 4);
215
   color_format->alphagrad (outmatrix_ref (out, 2, 5 - 2), col, 1, 4);
255
   alphagrad (outmatrix_ref (out, 4, 5 - 3), col, 1, 4);
216
   color_format->alphagrad (outmatrix_ref (out, 4, 5 - 3), col, 1, 4);
256
   alphagrad (outmatrix_ref (out, 1, 5 - 1), col, 3, 4);
217
   color_format->alphagrad (outmatrix_ref (out, 1, 5 - 1), col, 3, 4);
257
   alphagrad (outmatrix_ref (out, 3, 5 - 2), col, 3, 4);
218
   color_format->alphagrad (outmatrix_ref (out, 3, 5 - 2), col, 3, 4);
258
   *outmatrix_ref (out, 2, 5 - 1) = col;
219
   *outmatrix_ref (out, 2, 5 - 1) = col;
259
   *outmatrix_ref (out, 3, 5 - 1) = col;
220
   *outmatrix_ref (out, 3, 5 - 1) = col;
260
   *outmatrix_ref (out, 4, 5 - 1) = col;
221
   *outmatrix_ref (out, 4, 5 - 1) = col;
261
   *outmatrix_ref (out, 4, 5 - 2) = col;
222
   *outmatrix_ref (out, 4, 5 - 2) = col;
262
}
223
}
263
static void blend_line_steep_6x (uint32_t col, outmatrix_t *out, alphagrad_func alphagrad)
224
static void blend_line_steep_6x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref)
264
{
225
{
265
   alphagrad (outmatrix_ref (out, 0, 6 - 1), col, 1, 4);
226
   color_format->alphagrad (outmatrix_ref (out, 0, 6 - 1), col, 1, 4);
266
   alphagrad (outmatrix_ref (out, 2, 6 - 2), col, 1, 4);
227
   color_format->alphagrad (outmatrix_ref (out, 2, 6 - 2), col, 1, 4);
267
   alphagrad (outmatrix_ref (out, 4, 6 - 3), col, 1, 4);
228
   color_format->alphagrad (outmatrix_ref (out, 4, 6 - 3), col, 1, 4);
268
   alphagrad (outmatrix_ref (out, 1, 6 - 1), col, 3, 4);
229
   color_format->alphagrad (outmatrix_ref (out, 1, 6 - 1), col, 3, 4);
269
   alphagrad (outmatrix_ref (out, 3, 6 - 2), col, 3, 4);
230
   color_format->alphagrad (outmatrix_ref (out, 3, 6 - 2), col, 3, 4);
270
   alphagrad (outmatrix_ref (out, 5, 6 - 3), col, 3, 4);
231
   color_format->alphagrad (outmatrix_ref (out, 5, 6 - 3), col, 3, 4);
271
   *outmatrix_ref (out, 2, 6 - 1) = col;
232
   *outmatrix_ref (out, 2, 6 - 1) = col;
272
   *outmatrix_ref (out, 3, 6 - 1) = col;
233
   *outmatrix_ref (out, 3, 6 - 1) = col;
273
   *outmatrix_ref (out, 4, 6 - 1) = col;
234
   *outmatrix_ref (out, 4, 6 - 1) = col;
274
   *outmatrix_ref (out, 5, 6 - 1) = col;
235
   *outmatrix_ref (out, 5, 6 - 1) = col;
275
   *outmatrix_ref (out, 4, 6 - 2) = col;
236
   *outmatrix_ref (out, 4, 6 - 2) = col;
Line 277... Line 238...
277
}
238
}
278
 
239
 
279
///////////////////////////////////////////
240
///////////////////////////////////////////
280
// steep and shallow line scaling functions
241
// steep and shallow line scaling functions
281
 
242
 
282
static void blend_line_steep_and_shallow_2x (uint32_t col, outmatrix_t *out, alphagrad_func alphagrad)
243
static void blend_line_steep_and_shallow_2x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref)
283
{
244
{
284
   alphagrad (outmatrix_ref (out, 1, 0), col, 1, 4);
245
   color_format->alphagrad (outmatrix_ref (out, 1, 0), col, 1, 4);
285
   alphagrad (outmatrix_ref (out, 0, 1), col, 1, 4);
246
   color_format->alphagrad (outmatrix_ref (out, 0, 1), col, 1, 4);
286
   alphagrad (outmatrix_ref (out, 1, 1), col, 5, 6); //[!] fixes 7/8 used in xBR
247
   color_format->alphagrad (outmatrix_ref (out, 1, 1), col, 5, 6); //[!] fixes 7/8 used in xBR
287
}
248
}
288
static void blend_line_steep_and_shallow_3x (uint32_t col, outmatrix_t *out, alphagrad_func alphagrad)
249
static void blend_line_steep_and_shallow_3x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref)
289
{
250
{
290
   alphagrad (outmatrix_ref (out, 2, 0), col, 1, 4);
251
   color_format->alphagrad (outmatrix_ref (out, 2, 0), col, 1, 4);
291
   alphagrad (outmatrix_ref (out, 0, 2), col, 1, 4);
252
   color_format->alphagrad (outmatrix_ref (out, 0, 2), col, 1, 4);
292
   alphagrad (outmatrix_ref (out, 2, 1), col, 3, 4);
253
   color_format->alphagrad (outmatrix_ref (out, 2, 1), col, 3, 4);
293
   alphagrad (outmatrix_ref (out, 1, 2), col, 3, 4);
254
   color_format->alphagrad (outmatrix_ref (out, 1, 2), col, 3, 4);
294
   *outmatrix_ref (out, 2, 2) = col;
255
   *outmatrix_ref (out, 2, 2) = col;
295
}
256
}
296
static void blend_line_steep_and_shallow_4x (uint32_t col, outmatrix_t *out, alphagrad_func alphagrad)
257
static void blend_line_steep_and_shallow_4x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref)
297
{
258
{
298
   alphagrad (outmatrix_ref (out, 3, 1), col, 3, 4);
259
   color_format->alphagrad (outmatrix_ref (out, 3, 1), col, 3, 4);
299
   alphagrad (outmatrix_ref (out, 1, 3), col, 3, 4);
260
   color_format->alphagrad (outmatrix_ref (out, 1, 3), col, 3, 4);
300
   alphagrad (outmatrix_ref (out, 3, 0), col, 1, 4);
261
   color_format->alphagrad (outmatrix_ref (out, 3, 0), col, 1, 4);
301
   alphagrad (outmatrix_ref (out, 0, 3), col, 1, 4);
262
   color_format->alphagrad (outmatrix_ref (out, 0, 3), col, 1, 4);
302
   alphagrad (outmatrix_ref (out, 2, 2), col, 1, 3); //[!] fixes 1/4 used in xBR
263
   color_format->alphagrad (outmatrix_ref (out, 2, 2), col, 1, 3); //[!] fixes 1/4 used in xBR
303
   *outmatrix_ref (out, 3, 3) = col;
264
   *outmatrix_ref (out, 3, 3) = col;
304
   *outmatrix_ref (out, 3, 2) = col;
265
   *outmatrix_ref (out, 3, 2) = col;
305
   *outmatrix_ref (out, 2, 3) = col;
266
   *outmatrix_ref (out, 2, 3) = col;
306
}
267
}
307
static void blend_line_steep_and_shallow_5x (uint32_t col, outmatrix_t *out, alphagrad_func alphagrad)
268
static void blend_line_steep_and_shallow_5x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref)
308
{
269
{
309
   alphagrad (outmatrix_ref (out, 0, 5 - 1), col, 1, 4);
270
   color_format->alphagrad (outmatrix_ref (out, 0, 5 - 1), col, 1, 4);
310
   alphagrad (outmatrix_ref (out, 2, 5 - 2), col, 1, 4);
271
   color_format->alphagrad (outmatrix_ref (out, 2, 5 - 2), col, 1, 4);
311
   alphagrad (outmatrix_ref (out, 1, 5 - 1), col, 3, 4);
272
   color_format->alphagrad (outmatrix_ref (out, 1, 5 - 1), col, 3, 4);
312
   alphagrad (outmatrix_ref (out, 5 - 1, 0), col, 1, 4);
273
   color_format->alphagrad (outmatrix_ref (out, 5 - 1, 0), col, 1, 4);
313
   alphagrad (outmatrix_ref (out, 5 - 2, 2), col, 1, 4);
274
   color_format->alphagrad (outmatrix_ref (out, 5 - 2, 2), col, 1, 4);
314
   alphagrad (outmatrix_ref (out, 5 - 1, 1), col, 3, 4);
275
   color_format->alphagrad (outmatrix_ref (out, 5 - 1, 1), col, 3, 4);
315
   alphagrad (outmatrix_ref (out, 3, 3), col, 2, 3);
276
   color_format->alphagrad (outmatrix_ref (out, 3, 3), col, 2, 3);
316
   *outmatrix_ref (out, 2, 5 - 1) = col;
277
   *outmatrix_ref (out, 2, 5 - 1) = col;
317
   *outmatrix_ref (out, 3, 5 - 1) = col;
278
   *outmatrix_ref (out, 3, 5 - 1) = col;
318
   *outmatrix_ref (out, 4, 5 - 1) = col;
279
   *outmatrix_ref (out, 4, 5 - 1) = col;
319
   *outmatrix_ref (out, 5 - 1, 2) = col;
280
   *outmatrix_ref (out, 5 - 1, 2) = col;
320
   *outmatrix_ref (out, 5 - 1, 3) = col;
281
   *outmatrix_ref (out, 5 - 1, 3) = col;
321
}
282
}
322
static void blend_line_steep_and_shallow_6x (uint32_t col, outmatrix_t *out, alphagrad_func alphagrad)
283
static void blend_line_steep_and_shallow_6x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref)
323
{
284
{
324
   alphagrad (outmatrix_ref (out, 0, 6 - 1), col, 1, 4);
285
   color_format->alphagrad (outmatrix_ref (out, 0, 6 - 1), col, 1, 4);
325
   alphagrad (outmatrix_ref (out, 2, 6 - 2), col, 1, 4);
286
   color_format->alphagrad (outmatrix_ref (out, 2, 6 - 2), col, 1, 4);
326
   alphagrad (outmatrix_ref (out, 1, 6 - 1), col, 3, 4);
287
   color_format->alphagrad (outmatrix_ref (out, 1, 6 - 1), col, 3, 4);
327
   alphagrad (outmatrix_ref (out, 3, 6 - 2), col, 3, 4);
288
   color_format->alphagrad (outmatrix_ref (out, 3, 6 - 2), col, 3, 4);
328
   alphagrad (outmatrix_ref (out, 6 - 1, 0), col, 1, 4);
289
   color_format->alphagrad (outmatrix_ref (out, 6 - 1, 0), col, 1, 4);
329
   alphagrad (outmatrix_ref (out, 6 - 2, 2), col, 1, 4);
290
   color_format->alphagrad (outmatrix_ref (out, 6 - 2, 2), col, 1, 4);
330
   alphagrad (outmatrix_ref (out, 6 - 1, 1), col, 3, 4);
291
   color_format->alphagrad (outmatrix_ref (out, 6 - 1, 1), col, 3, 4);
331
   alphagrad (outmatrix_ref (out, 6 - 2, 3), col, 3, 4);
292
   color_format->alphagrad (outmatrix_ref (out, 6 - 2, 3), col, 3, 4);
332
   *outmatrix_ref (out, 2, 6 - 1) = col;
293
   *outmatrix_ref (out, 2, 6 - 1) = col;
333
   *outmatrix_ref (out, 3, 6 - 1) = col;
294
   *outmatrix_ref (out, 3, 6 - 1) = col;
334
   *outmatrix_ref (out, 4, 6 - 1) = col;
295
   *outmatrix_ref (out, 4, 6 - 1) = col;
335
   *outmatrix_ref (out, 5, 6 - 1) = col;
296
   *outmatrix_ref (out, 5, 6 - 1) = col;
336
   *outmatrix_ref (out, 4, 6 - 2) = col;
297
   *outmatrix_ref (out, 4, 6 - 2) = col;
Line 340... Line 301...
340
}
301
}
341
 
302
 
342
//////////////////////////////////
303
//////////////////////////////////
343
// diagonal line scaling functions
304
// diagonal line scaling functions
344
 
305
 
345
static void blend_line_diagonal_2x (uint32_t col, outmatrix_t *out, alphagrad_func alphagrad)
306
static void blend_line_diagonal_2x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref)
346
{
307
{
347
   alphagrad (outmatrix_ref (out, 1, 1), col, 1, 2);
308
   color_format->alphagrad (outmatrix_ref (out, 1, 1), col, 1, 2);
348
}
309
}
349
static void blend_line_diagonal_3x (uint32_t col, outmatrix_t *out, alphagrad_func alphagrad)
310
static void blend_line_diagonal_3x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref)
350
{
311
{
351
   alphagrad (outmatrix_ref (out, 1, 2), col, 1, 8); //conflict with other rotations for this odd scale
312
   color_format->alphagrad (outmatrix_ref (out, 1, 2), col, 1, 8); //conflict with other rotations for this odd scale
352
   alphagrad (outmatrix_ref (out, 2, 1), col, 1, 8);
313
   color_format->alphagrad (outmatrix_ref (out, 2, 1), col, 1, 8);
353
   alphagrad (outmatrix_ref (out, 2, 2), col, 7, 8); //
314
   color_format->alphagrad (outmatrix_ref (out, 2, 2), col, 7, 8); //
354
}
315
}
355
static void blend_line_diagonal_4x (uint32_t col, outmatrix_t *out, alphagrad_func alphagrad)
316
static void blend_line_diagonal_4x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref)
356
{
317
{
357
   alphagrad (outmatrix_ref (out, 4 - 1, 4 / 2), col, 1, 2);
318
   color_format->alphagrad (outmatrix_ref (out, 4 - 1, 4 / 2), col, 1, 2);
358
   alphagrad (outmatrix_ref (out, 4 - 2, 4 / 2 + 1), col, 1, 2);
319
   color_format->alphagrad (outmatrix_ref (out, 4 - 2, 4 / 2 + 1), col, 1, 2);
359
   *outmatrix_ref (out, 4 - 1, 4 - 1) = col;
320
   *outmatrix_ref (out, 4 - 1, 4 - 1) = col;
360
}
321
}
361
static void blend_line_diagonal_5x (uint32_t col, outmatrix_t *out, alphagrad_func alphagrad)
322
static void blend_line_diagonal_5x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref)
362
{
323
{
363
   alphagrad (outmatrix_ref (out, 5 - 1, 5 / 2 + 0), col, 1, 8); //conflict with other rotations for this odd scale
324
   color_format->alphagrad (outmatrix_ref (out, 5 - 1, 5 / 2 + 0), col, 1, 8); //conflict with other rotations for this odd scale
364
   alphagrad (outmatrix_ref (out, 5 - 2, 5 / 2 + 1), col, 1, 8);
325
   color_format->alphagrad (outmatrix_ref (out, 5 - 2, 5 / 2 + 1), col, 1, 8);
365
   alphagrad (outmatrix_ref (out, 5 - 3, 5 / 2 + 2), col, 1, 8); //
326
   color_format->alphagrad (outmatrix_ref (out, 5 - 3, 5 / 2 + 2), col, 1, 8); //
366
   alphagrad (outmatrix_ref (out, 4, 3), col, 7, 8);
327
   color_format->alphagrad (outmatrix_ref (out, 4, 3), col, 7, 8);
367
   alphagrad (outmatrix_ref (out, 3, 4), col, 7, 8);
328
   color_format->alphagrad (outmatrix_ref (out, 3, 4), col, 7, 8);
368
   *outmatrix_ref (out, 4, 4) = col;
329
   *outmatrix_ref (out, 4, 4) = col;
369
}
330
}
370
static void blend_line_diagonal_6x (uint32_t col, outmatrix_t *out, alphagrad_func alphagrad)
331
static void blend_line_diagonal_6x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref)
371
{
332
{
372
   alphagrad (outmatrix_ref (out, 6 - 1, 6 / 2 + 0), col, 1, 2);
333
   color_format->alphagrad (outmatrix_ref (out, 6 - 1, 6 / 2 + 0), col, 1, 2);
373
   alphagrad (outmatrix_ref (out, 6 - 2, 6 / 2 + 1), col, 1, 2);
334
   color_format->alphagrad (outmatrix_ref (out, 6 - 2, 6 / 2 + 1), col, 1, 2);
374
   alphagrad (outmatrix_ref (out, 6 - 3, 6 / 2 + 2), col, 1, 2);
335
   color_format->alphagrad (outmatrix_ref (out, 6 - 3, 6 / 2 + 2), col, 1, 2);
375
   *outmatrix_ref (out, 6 - 2, 6 - 1) = col;
336
   *outmatrix_ref (out, 6 - 2, 6 - 1) = col;
376
   *outmatrix_ref (out, 6 - 1, 6 - 1) = col;
337
   *outmatrix_ref (out, 6 - 1, 6 - 1) = col;
377
   *outmatrix_ref (out, 6 - 1, 6 - 2) = col;
338
   *outmatrix_ref (out, 6 - 1, 6 - 2) = col;
378
}
339
}
379
 
340
 
380
///////////////////////////
341
///////////////////////////
381
// corner scaling functions
342
// corner scaling functions
382
 
343
 
383
static void blend_corner_2x (uint32_t col, outmatrix_t *out, alphagrad_func alphagrad)
344
static void blend_corner_2x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref)
384
{
345
{
385
   //model a round corner
346
   // model a round corner
386
   alphagrad (outmatrix_ref (out, 1, 1), col, 21, 100); //exact: 1 - pi/4 = 0.2146018366
347
   color_format->alphagrad (outmatrix_ref (out, 1, 1), col, 21, 100); //exact: 1 - pi/4 = 0.2146018366
387
}
348
}
388
static void blend_corner_3x (uint32_t col, outmatrix_t *out, alphagrad_func alphagrad)
349
static void blend_corner_3x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref)
389
{
350
{
390
   //model a round corner
351
   // model a round corner
391
   alphagrad (outmatrix_ref (out, 2, 2), col, 45, 100); //exact: 0.4545939598
352
   color_format->alphagrad (outmatrix_ref (out, 2, 2), col, 45, 100); //exact: 0.4545939598
392
   //alphagrad (outmatrix_ref (out, 2, 1), col, 7, 256); //0.02826017254 -> negligible + avoid conflicts with other rotations for this odd scale
353
   //color_format->alphagrad (outmatrix_ref (out, 2, 1), col, 7, 256); //0.02826017254 -> negligible + avoid conflicts with other rotations for this odd scale
393
   //alphagrad (outmatrix_ref (out, 1, 2), col, 7, 256); //0.02826017254
354
   //color_format->alphagrad (outmatrix_ref (out, 1, 2), col, 7, 256); //0.02826017254
394
}
355
}
395
static void blend_corner_4x (uint32_t col, outmatrix_t *out, alphagrad_func alphagrad)
356
static void blend_corner_4x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref)
396
{
357
{
397
   //model a round corner
358
   // model a round corner
398
   alphagrad (outmatrix_ref (out, 3, 3), col, 68, 100); //exact: 0.6848532563
359
   color_format->alphagrad (outmatrix_ref (out, 3, 3), col, 68, 100); //exact: 0.6848532563
399
   alphagrad (outmatrix_ref (out, 3, 2), col, 9, 100); //0.08677704501
360
   color_format->alphagrad (outmatrix_ref (out, 3, 2), col, 9, 100); //0.08677704501
400
   alphagrad (outmatrix_ref (out, 2, 3), col, 9, 100); //0.08677704501
361
   color_format->alphagrad (outmatrix_ref (out, 2, 3), col, 9, 100); //0.08677704501
401
}
362
}
402
static void blend_corner_5x (uint32_t col, outmatrix_t *out, alphagrad_func alphagrad)
363
static void blend_corner_5x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref)
403
{
364
{
404
   // model a round corner
365
   // model a round corner
405
   alphagrad (outmatrix_ref (out, 4, 4), col, 86, 100); //exact: 0.8631434088
366
   color_format->alphagrad (outmatrix_ref (out, 4, 4), col, 86, 100); //exact: 0.8631434088
406
   alphagrad (outmatrix_ref (out, 4, 3), col, 23, 100); //0.2306749731
367
   color_format->alphagrad (outmatrix_ref (out, 4, 3), col, 23, 100); //0.2306749731
407
   alphagrad (outmatrix_ref (out, 3, 4), col, 23, 100); //0.2306749731
368
   color_format->alphagrad (outmatrix_ref (out, 3, 4), col, 23, 100); //0.2306749731
408
   //alphagrad (outmatrix_ref (out, 4, 2), col, 1, 64); //0.01676812367 -> negligible + avoid conflicts with other rotations for this odd scale
369
   //color_format->alphagrad (outmatrix_ref (out, 4, 2), col, 1, 64); //0.01676812367 -> negligible + avoid conflicts with other rotations for this odd scale
409
   //alphagrad (outmatrix_ref (out, 2, 4), col, 1, 64); //0.01676812367
370
   //color_format->alphagrad (outmatrix_ref (out, 2, 4), col, 1, 64); //0.01676812367
410
}
371
}
411
static void blend_corner_6x (uint32_t col, outmatrix_t *out, alphagrad_func alphagrad)
372
static void blend_corner_6x (uint32_t col, outmatrix_t *out, colorformat_t *color_format, outmatrixreffunc_t outmatrix_ref)
412
{
373
{
413
   //model a round corner
374
   // model a round corner
414
   alphagrad (outmatrix_ref (out, 5, 5), col, 97, 100); //exact: 0.9711013910
375
   color_format->alphagrad (outmatrix_ref (out, 5, 5), col, 97, 100); //exact: 0.9711013910
415
   alphagrad (outmatrix_ref (out, 4, 5), col, 42, 100); //0.4236372243
376
   color_format->alphagrad (outmatrix_ref (out, 4, 5), col, 42, 100); //0.4236372243
416
   alphagrad (outmatrix_ref (out, 5, 4), col, 42, 100); //0.4236372243
377
   color_format->alphagrad (outmatrix_ref (out, 5, 4), col, 42, 100); //0.4236372243
417
   alphagrad (outmatrix_ref (out, 5, 3), col, 6, 100); //0.05652034508
378
   color_format->alphagrad (outmatrix_ref (out, 5, 3), col, 6, 100); //0.05652034508
418
   alphagrad (outmatrix_ref (out, 3, 5), col, 6, 100); //0.05652034508
379
   color_format->alphagrad (outmatrix_ref (out, 3, 5), col, 6, 100); //0.05652034508
419
}
380
}
420
 
381
 
421
/////////////////////////////////////
382
/////////////////////////////////////
422
// scaler objects for various factors
383
// scaler objects for various factors
423
 
384
 
Line 428... Line 389...
428
   { 4, blend_line_shallow_4x, blend_line_steep_4x, blend_line_steep_and_shallow_4x, blend_line_diagonal_4x, blend_corner_4x },
389
   { 4, blend_line_shallow_4x, blend_line_steep_4x, blend_line_steep_and_shallow_4x, blend_line_diagonal_4x, blend_corner_4x },
429
   { 5, blend_line_shallow_5x, blend_line_steep_5x, blend_line_steep_and_shallow_5x, blend_line_diagonal_5x, blend_corner_5x },
390
   { 5, blend_line_shallow_5x, blend_line_steep_5x, blend_line_steep_and_shallow_5x, blend_line_diagonal_5x, blend_corner_5x },
430
   { 6, blend_line_shallow_6x, blend_line_steep_6x, blend_line_steep_and_shallow_6x, blend_line_diagonal_6x, blend_corner_6x },
391
   { 6, blend_line_shallow_6x, blend_line_steep_6x, blend_line_steep_and_shallow_6x, blend_line_diagonal_6x, blend_corner_6x },
431
};
392
};
432
 
393
 
-
 
394
/////////////////////////////////////////////////////
-
 
395
// alpha gradient functions for various color formats
433
 
396
 
-
 
397
static void alphagrad24 (uint32_t *pixBack, uint32_t pixFront, unsigned int M, unsigned int N)
-
 
398
{
-
 
399
   // blend front color with opacity M / N over opaque background: http://en.wikipedia.org/wiki/Alpha_compositing#Alpha_blending
-
 
400
   *pixBack = ((CALC_COLOR24 (GET_RED   (pixFront), GET_RED   (*pixBack), M, N) << 16)
-
 
401
             | (CALC_COLOR24 (GET_GREEN (pixFront), GET_GREEN (*pixBack), M, N) <<  8)
-
 
402
             | (CALC_COLOR24 (GET_BLUE  (pixFront), GET_BLUE  (*pixBack), M, N) <<  0));
-
 
403
}
-
 
404
static void alphagrad32 (uint32_t *pixBack, uint32_t pixFront, unsigned int M, unsigned int N)
-
 
405
{
-
 
406
   // find intermediate color between two colors with alpha channels (=> NO alpha blending!!!)
-
 
407
   const unsigned int weightFront = GET_ALPHA (pixFront) * M;
-
 
408
   const unsigned int weightBack = GET_ALPHA (*pixBack) * (N - M);
-
 
409
   const unsigned int weightSum = weightFront + weightBack;
-
 
410
   *pixBack = (weightSum == 0 ? 0 :
-
 
411
               (((uint8_t) (weightSum / N))                                                                     << 24)
-
 
412
               | (CALC_COLOR32 (GET_RED   (pixFront), GET_RED   (*pixBack), weightFront, weightBack, weightSum) << 16)
-
 
413
               | (CALC_COLOR32 (GET_GREEN (pixFront), GET_GREEN (*pixBack), weightFront, weightBack, weightSum) <<  8)
-
 
414
               | (CALC_COLOR32 (GET_BLUE  (pixFront), GET_BLUE  (*pixBack), weightFront, weightBack, weightSum) <<  0));
-
 
415
}
-
 
416
 
-
 
417
/////////////////////////////////////////////////////
-
 
418
// color distance functions for various color formats
-
 
419
 
-
 
420
static double dist24 (uint32_t pix1, uint32_t pix2)
-
 
421
{
-
 
422
   //30% perf boost compared to plain distYCbCr()!
-
 
423
   //consumes 64 MB memory; using double is only 2% faster, but takes 128 MB
-
 
424
   static float diffToDist[256 * 256 * 256];
-
 
425
   static bool is_initialized = false;
-
 
426
   if (!is_initialized)
-
 
427
   {
-
 
428
      for (uint32_t i = 0; i < 256 * 256 * 256; ++i) //startup time: 114 ms on Intel Core i5 (four cores)
-
 
429
      {
-
 
430
         const int r_diff = GET_RED (i) * 2 - 0xFF;
-
 
431
         const int g_diff = GET_GREEN (i) * 2 - 0xFF;
-
 
432
         const int b_diff = GET_BLUE (i) * 2 - 0xFF;
-
 
433
 
-
 
434
         const double k_b = 0.0593; //ITU-R BT.2020 conversion
-
 
435
         const double k_r = 0.2627; //
-
 
436
         const double k_g = 1 - k_b - k_r;
-
 
437
 
-
 
438
         const double scale_b = 0.5 / (1 - k_b);
-
 
439
         const double scale_r = 0.5 / (1 - k_r);
-
 
440
 
-
 
441
         const double y = k_r * r_diff + k_g * g_diff + k_b * b_diff; //[!], analog YCbCr!
-
 
442
         const double c_b = scale_b * (b_diff - y);
-
 
443
         const double c_r = scale_r * (r_diff - y);
-
 
444
 
-
 
445
         diffToDist[i] = (float) (sqrt ((y * y) + (c_b * c_b) + (c_r * c_r)));
-
 
446
      }
-
 
447
      is_initialized = true;
-
 
448
   }
-
 
449
 
-
 
450
   const int r_diff = (int) GET_RED (pix1) - (int) GET_RED (pix2);
-
 
451
   const int g_diff = (int) GET_GREEN (pix1) - (int) GET_GREEN (pix2);
-
 
452
   const int b_diff = (int) GET_BLUE (pix1) - (int) GET_BLUE (pix2);
-
 
453
 
-
 
454
   return diffToDist[(((r_diff + 0xFF) / 2) << 16) | //slightly reduce precision (division by 2) to squeeze value into single byte
-
 
455
      (((g_diff + 0xFF) / 2) << 8) |
-
 
456
      (((b_diff + 0xFF) / 2) << 0)];
-
 
457
}
-
 
458
static double dist32 (uint32_t pix1, uint32_t pix2)
-
 
459
{
-
 
460
   // Requirements for a color distance handling alpha channel: with a1, a2 in [0, 1]
-
 
461
   //    1. if a1 = a2, distance should be: a1 * distYCbCr()
-
 
462
   //    2. if a1 = 0,  distance should be: a2 * distYCbCr(black, white) = a2 * 255
-
 
463
   //    3. if a1 = 1,  ??? maybe: 255 * (1 - a2) + a2 * distYCbCr()
-
 
464
   //return MIN (a1, a2) * distYCbCrBuffered(pix1, pix2) + 255 * abs(a1 - a2);
-
 
465
   //=> following code is 15% faster:
-
 
466
   const double d = dist24 (pix1, pix2);
-
 
467
   const double a1 = GET_ALPHA (pix1) / 255.0;
-
 
468
   const double a2 = GET_ALPHA (pix2) / 255.0;
-
 
469
   return (a1 < a2 ? a1 * d + 255 * (a2 - a1) : a2 * d + 255 * (a1 - a2));
-
 
470
}
-
 
471
 
-
 
472
///////////////////////////////////////
-
 
473
// color format objects for various bpp
-
 
474
 
-
 
475
static colorformat_t color_format_24 = { 24, alphagrad24, dist24 };
-
 
476
static colorformat_t color_format_32 = { 32, alphagrad32, dist32 };
-
 
477
 
-
 
478
//////////////////////////////////////////////////////////
-
 
479
// output matrix reference functions for various rotations
-
 
480
 
-
 
481
static uint32_t *outmatrixref_0   (outmatrix_t *mat, size_t I, size_t J) { return (mat->ptr + I * mat->stride + J); }
-
 
482
static uint32_t *outmatrixref_90  (outmatrix_t *mat, size_t I, size_t J) { return (mat->ptr + (mat->size - 1 - J) * mat->stride + I); }
-
 
483
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)); }
-
 
484
static uint32_t *outmatrixref_270 (outmatrix_t *mat, size_t I, size_t J) { return (mat->ptr + J * mat->stride + (mat->size - 1 - I)); }
-
 
485
 
-
 
486
 
-
 
487
///////////////////////////
-
 
488
// core algorithm functions
-
 
489
 
-
 
490
 
-
 
491
#ifdef _MSC_VER
-
 
492
#define FORCE_INLINE __forceinline
-
 
493
#elif defined __GNUC__
-
 
494
#define FORCE_INLINE __attribute__((always_inline)) inline
-
 
495
#else
-
 
496
#define FORCE_INLINE inline
-
 
497
#endif
-
 
498
 
-
 
499
 
434
static FORCE_INLINE void preProcessCorners (blendresult_t *result, const kernel_4x4_t *ker, dist_func dist)
500
static FORCE_INLINE void preprocess_corners (blendresult_t *result, const kernel_4x4_t *ker, colorformat_t *color_format)
435
{
501
{
436
   // detect blend direction
502
   // detect blend direction
437
   // result: F, G, J, K corners of "GradientType"
503
   // result: F, G, J, K corners of "GradientType"
438
 
504
 
439
   // input kernel area naming convention:
505
   // input kernel area naming convention:
Line 451... Line 517...
451
 
517
 
452
   if (((ker->f == ker->g) && (ker->j == ker->k)) || ((ker->f == ker->j) && (ker->g == ker->k)))
518
   if (((ker->f == ker->g) && (ker->j == ker->k)) || ((ker->f == ker->j) && (ker->g == ker->k)))
453
      return;
519
      return;
454
 
520
 
455
   const int weight = 4;
521
   const int weight = 4;
456
   double jg = dist (ker->i, ker->f) + dist (ker->f, ker->c) + dist (ker->n, ker->k) + dist (ker->k, ker->h) + weight * dist (ker->j, ker->g);
522
   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);
457
   double fk = dist (ker->e, ker->j) + dist (ker->j, ker->o) + dist (ker->b, ker->g) + dist (ker->g, ker->l) + weight * dist (ker->f, ker->k);
523
   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);
458
 
524
 
459
   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
525
   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
460
   {
526
   {
461
      const bool dominantGradient = XBRZ_CFG_DOMINANT_DIRECTION_THRESHOLD * jg < fk;
527
      const bool dominantGradient = XBRZ_CFG_DOMINANT_DIRECTION_THRESHOLD * jg < fk;
462
      if (ker->f != ker->g && ker->f != ker->j)
528
      if (ker->f != ker->g && ker->f != ker->j)
Line 472... Line 538...
472
         result->blend_j = dominantGradient ? BLEND_DOMINANT : BLEND_NORMAL;
538
         result->blend_j = dominantGradient ? BLEND_DOMINANT : BLEND_NORMAL;
473
 
539
 
474
      if (ker->g != ker->f && ker->g != ker->k)
540
      if (ker->g != ker->f && ker->g != ker->k)
475
         result->blend_g = dominantGradient ? BLEND_DOMINANT : BLEND_NORMAL;
541
         result->blend_g = dominantGradient ? BLEND_DOMINANT : BLEND_NORMAL;
476
   }
542
   }
-
 
543
 
477
   return;
544
   return;
478
}
545
}
479
 
-
 
480
// compress four blend types into a single byte
-
 
481
#define getTopL(b)    ((BlendType) (0x3 & ((unsigned char) (b) >> 0)))
-
 
482
#define getTopR(b)    ((BlendType) (0x3 & ((unsigned char) (b) >> 2)))
-
 
483
#define getBottomR(b) ((BlendType) (0x3 & ((unsigned char) (b) >> 4)))
-
 
484
#define getBottomL(b) ((BlendType) (0x3 & ((unsigned char) (b) >> 6)))
-
 
485
 
-
 
486
static inline void setTopL (unsigned char& b, BlendType bt) { b |= (((BlendType) (bt)) << 0); } //buffer is assumed to be initialized before preprocessing!
-
 
487
static inline void setTopR (unsigned char& b, BlendType bt) { b |= (((BlendType) (bt)) << 2); }
-
 
488
static inline void setBottomR (unsigned char& b, BlendType bt) { b |= (((BlendType) (bt)) << 4); }
-
 
489
static inline void setBottomL (unsigned char& b, BlendType bt) { b |= (((BlendType) (bt)) << 6); }
-
 
490
 
546
 
491
 
547
 
492
FORCE_INLINE void blend_pixel (const scaler_t *scaler, const kernel_3x3_t *ker, uint32_t *target, int trgWidth, unsigned char blendInfo, alphagrad_func alphagrad, dist_func dist, int rotDeg) //result of preprocessing all four corners of pixel "e"
548
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"
493
{
549
{
494
   // input kernel area naming convention:
550
   // input kernel area naming convention:
495
   // -------------
551
   // -------------
496
   // | A | B | C |
552
   // | A | B | C |
497
   // ----|---|---|
553
   // ----|---|---|
Line 502... Line 558...
502
 
558
 
503
   uint32_t
559
   uint32_t
504
      a, b, c,
560
      a, b, c,
505
      d, e, f,
561
      d, e, f,
506
      g, h, i;
562
      g, h, i;
507
   unsigned char blend;
563
   uint8_t blend;
508
 
564
 
509
   if      (rotDeg == 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; }
565
   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; }
510
   else if (rotDeg == 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; }
566
   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; }
511
   else if (rotDeg == 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; }
567
   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; }
512
   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 << 0) | (blendInfo >> 8)) & 0xff; }
568
   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 << 0) | (blendInfo >> 8)) & 0xff; }
513
 
569
 
514
   if (getBottomR (blend) >= BLEND_NORMAL)
570
   if (getBottomR (blend) >= BLEND_NORMAL)
515
   {
571
   {
516
      outmatrix_t out;
-
 
517
      uint32_t px;
572
      uint32_t px;
518
      bool doLineBlend;
573
      bool doLineBlend;
519
 
574
 
520
      if (getBottomR (blend) >= BLEND_DOMINANT)
575
      if (getBottomR (blend) >= BLEND_DOMINANT)
521
         doLineBlend = true;
576
         doLineBlend = true;
522
      else if (getTopR (blend) != BLEND_NONE && (dist (e, g) >= XBRZ_CFG_EQUAL_COLOR_TOLERANCE)) //but support double-blending for 90° corners
577
      else if (getTopR (blend) != BLEND_NONE && (color_format->dist (e, g) >= XBRZ_CFG_EQUAL_COLOR_TOLERANCE)) //but support double-blending for 90° corners
523
         doLineBlend = false; // make sure there is no second blending in an adjacent rotation for this pixel: handles insular pixels, mario eyes
578
         doLineBlend = false; // make sure there is no second blending in an adjacent rotation for this pixel: handles insular pixels, mario eyes
524
      else if (getBottomL (blend) != BLEND_NONE && (dist (e, c) >= XBRZ_CFG_EQUAL_COLOR_TOLERANCE))
579
      else if (getBottomL (blend) != BLEND_NONE && (color_format->dist (e, c) >= XBRZ_CFG_EQUAL_COLOR_TOLERANCE))
525
         doLineBlend = false; // make sure there is no second blending in an adjacent rotation for this pixel: handles insular pixels, mario eyes
580
         doLineBlend = false; // make sure there is no second blending in an adjacent rotation for this pixel: handles insular pixels, mario eyes
526
      else if ((dist (e, i) >= XBRZ_CFG_EQUAL_COLOR_TOLERANCE)
581
      else if ((color_format->dist (e, i) >= XBRZ_CFG_EQUAL_COLOR_TOLERANCE)
527
         && (dist (g, h) < XBRZ_CFG_EQUAL_COLOR_TOLERANCE)
582
               && (color_format->dist (g, h) < XBRZ_CFG_EQUAL_COLOR_TOLERANCE)
528
         && (dist (h, i) < XBRZ_CFG_EQUAL_COLOR_TOLERANCE)
583
               && (color_format->dist (h, i) < XBRZ_CFG_EQUAL_COLOR_TOLERANCE)
529
         && (dist (i, f) < XBRZ_CFG_EQUAL_COLOR_TOLERANCE)
584
               && (color_format->dist (i, f) < XBRZ_CFG_EQUAL_COLOR_TOLERANCE)
530
         && (dist (f, c) < XBRZ_CFG_EQUAL_COLOR_TOLERANCE))
585
               && (color_format->dist (f, c) < XBRZ_CFG_EQUAL_COLOR_TOLERANCE))
531
         doLineBlend = false; // no full blending for L-shapes; blend corner only (handles "mario mushroom eyes")
586
         doLineBlend = false; // no full blending for L-shapes; blend corner only (handles "mario mushroom eyes")
532
      else
587
      else
533
         doLineBlend = true;
588
         doLineBlend = true;
534
 
589
 
-
 
590
      outmatrix_t out;
535
      outmatrix_create (&out, scaler->factor, target, trgWidth, rotDeg);
591
      out.size = scaler->factor;
-
 
592
      out.ptr = target;
-
 
593
      out.stride = trgWidth;
-
 
594
 
536
      px = (dist (e, f) <= dist (e, h) ? f : h); //choose most similar color
595
      px = (color_format->dist (e, f) <= color_format->dist (e, h) ? f : h); //choose most similar color
537
 
596
 
538
      if (doLineBlend)
597
      if (doLineBlend)
539
      {
598
      {
540
         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
599
         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
541
         const double hc = dist (h, c); //
600
         const double hc = color_format->dist (h, c); //
542
         const bool haveShallowLine = (XBRZ_CFG_STEEP_DIRECTION_THRESHOLD * fg <= hc) && (e != g) && (d != g);
601
         const bool haveShallowLine = (XBRZ_CFG_STEEP_DIRECTION_THRESHOLD * fg <= hc) && (e != g) && (d != g);
543
         const bool haveSteepLine   = (XBRZ_CFG_STEEP_DIRECTION_THRESHOLD * hc <= fg) && (e != c) && (b != c);
602
         const bool haveSteepLine   = (XBRZ_CFG_STEEP_DIRECTION_THRESHOLD * hc <= fg) && (e != c) && (b != c);
544
 
603
 
545
         if (haveShallowLine)
604
         if (haveShallowLine)
546
         {
605
         {
547
            if (haveSteepLine)
606
            if (haveSteepLine)
548
               scaler->blend_line_steep_and_shallow (px, &out, alphagrad);
607
               scaler->blend_line_steep_and_shallow (px, &out, color_format, outmatrix_ref);
549
            else
608
            else
550
               scaler->blend_line_shallow (px, &out, alphagrad);
609
               scaler->blend_line_shallow (px, &out, color_format, outmatrix_ref);
551
         }
610
         }
552
         else
611
         else
553
         {
612
         {
554
            if (haveSteepLine)
613
            if (haveSteepLine)
555
               scaler->blend_line_steep (px, &out, alphagrad);
614
               scaler->blend_line_steep (px, &out, color_format, outmatrix_ref);
556
            else
615
            else
557
               scaler->blend_line_diagonal (px, &out, alphagrad);
616
               scaler->blend_line_diagonal (px, &out, color_format, outmatrix_ref);
558
         }
617
         }
559
      }
618
      }
560
      else
619
      else
561
         scaler->blend_corner (px, &out, alphagrad);
620
         scaler->blend_corner (px, &out, color_format, outmatrix_ref);
562
   }
621
   }
563
}
622
}
564
 
623
 
565
 
624
 
566
void scale_image (const scaler_t *scaler, const uint32_t *src, uint32_t *trg, int srcWidth, int srcHeight, int yFirst, int yLast, alphagrad_func alphagrad, dist_func dist)
625
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)
567
{
626
{
568
   yFirst = MAX (yFirst, 0);
627
   yFirst = MAX (yFirst, 0);
569
   yLast = MIN (yLast, srcHeight);
628
   yLast = MIN (yLast, srcHeight);
570
   if (yFirst >= yLast || srcWidth <= 0)
629
   if (yFirst >= yLast || srcWidth <= 0)
571
      return;
630
      return;
572
 
631
 
573
   const int trgWidth = srcWidth * scaler->factor;
632
   const int trgWidth = srcWidth * scaler->factor;
574
 
633
 
575
   //"use" space at the end of the image as temporary buffer for "on the fly preprocessing": we even could use larger area of
634
   // "use" space at the end of the image as temporary buffer for "on the fly preprocessing": we even could use larger area of
576
   //"sizeof(uint32_t) * srcWidth * (yLast - yFirst)" bytes without risk of accidental overwriting before accessing
635
   // "sizeof(uint32_t) * srcWidth * (yLast - yFirst)" bytes without risk of accidental overwriting before accessing
577
   const int bufferSize = srcWidth;
636
   const int bufferSize = srcWidth;
578
   unsigned char *preProcBuffer = (unsigned char *) (trg + yLast * scaler->factor * trgWidth) - bufferSize;
637
   uint8_t *preProcBuffer = (uint8_t *) (trg + yLast * scaler->factor * trgWidth) - bufferSize;
579
   memset (preProcBuffer, 0, bufferSize);
638
   memset (preProcBuffer, 0, bufferSize);
580
   static_assert(BLEND_NONE == 0, "");
-
 
581
 
639
 
582
   //initialize preprocessing buffer for first row of current stripe: detect upper left and right corner blending
640
   // initialize preprocessing buffer for first row of current stripe: detect upper left and right corner blending
583
   //this cannot be optimized for adjacent processing stripes; we must not allow for a memory race condition!
641
   // this cannot be optimized for adjacent processing stripes; we must not allow for a memory race condition!
584
   if (yFirst > 0)
642
   if (yFirst > 0)
585
   {
643
   {
586
      const int y = yFirst - 1;
644
      const int y = yFirst - 1;
587
 
645
 
588
      const uint32_t* s_m1 = src + srcWidth * MAX (y - 1, 0);
646
      const uint32_t *s_m1 = src + srcWidth * MAX (y - 1, 0);
589
      const uint32_t* s_0 = src + srcWidth * y; //center line
647
      const uint32_t *s_0 = src + srcWidth * y; //center line
590
      const uint32_t* s_p1 = src + srcWidth * MIN (y + 1, srcHeight - 1);
648
      const uint32_t *s_p1 = src + srcWidth * MIN (y + 1, srcHeight - 1);
591
      const uint32_t* s_p2 = src + srcWidth * MIN (y + 2, srcHeight - 1);
649
      const uint32_t *s_p2 = src + srcWidth * MIN (y + 2, srcHeight - 1);
592
 
650
 
593
      for (int x = 0; x < srcWidth; ++x)
651
      for (int x = 0; x < srcWidth; ++x)
594
      {
652
      {
595
         blendresult_t res;
653
         blendresult_t res;
596
         const int x_m1 = MAX (x - 1, 0);
654
         const int x_m1 = MAX (x - 1, 0);
597
         const int x_p1 = MIN (x + 1, srcWidth - 1);
655
         const int x_p1 = MIN (x + 1, srcWidth - 1);
598
         const int x_p2 = MIN (x + 2, srcWidth - 1);
656
         const int x_p2 = MIN (x + 2, srcWidth - 1);
599
 
657
 
600
         kernel_4x4_t ker; //perf: initialization is negligible
658
         kernel_4x4_t ker; // perf: initialization is negligible
601
         ker.a = s_m1[x_m1]; //read sequentially from memory as far as possible
659
         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
602
         ker.b = s_m1[x];
660
         ker.e = s_0[x_m1];  ker.f = s_0[x];  ker.g = s_0[x_p1];  ker.h = s_0[x_p2];
603
         ker.c = s_m1[x_p1];
661
         ker.i = s_p1[x_m1]; ker.j = s_p1[x]; ker.k = s_p1[x_p1]; ker.l = s_p1[x_p2];
604
         ker.d = s_m1[x_p2];
662
         ker.m = s_p2[x_m1]; ker.n = s_p2[x]; ker.o = s_p2[x_p1]; ker.p = s_p2[x_p2];
605
 
663
 
606
         ker.e = s_0[x_m1];
664
         preprocess_corners (&res, &ker, color_format);
607
         ker.f = s_0[x];
-
 
608
         ker.g = s_0[x_p1];
-
 
609
         ker.h = s_0[x_p2];
-
 
610
 
665
 
611
         ker.i = s_p1[x_m1];
-
 
612
         ker.j = s_p1[x];
-
 
613
         ker.k = s_p1[x_p1];
-
 
614
         ker.l = s_p1[x_p2];
-
 
615
 
-
 
616
         ker.m = s_p2[x_m1];
-
 
617
         ker.n = s_p2[x];
-
 
618
         ker.o = s_p2[x_p1];
-
 
619
         ker.p = s_p2[x_p2];
-
 
620
 
-
 
621
         preProcessCorners (&res, &ker, dist);
-
 
622
         /*
-
 
623
         preprocessing blend result:
666
         // preprocessing blend result:
624
         ---------
667
         // ---------
625
         | F | G |   //evalute corner between F, G, J, K
668
         // | F | G |   //evalute corner between F, G, J, K
626
         ----|---|   //input pixel is at position F
669
         // ----|---|   //input pixel is at position F
627
         | J | K |
670
         // | J | K |
628
         ---------
671
         // ---------
629
         */
-
 
630
         setTopR (preProcBuffer[x], res.blend_j);
-
 
631
 
672
 
-
 
673
         setTopR (&preProcBuffer[x], res.blend_j);
632
         if (x + 1 < bufferSize)
674
         if (x + 1 < bufferSize)
633
            setTopL (preProcBuffer[x + 1], res.blend_k);
675
            setTopL (&preProcBuffer[x + 1], res.blend_k);
634
      }
676
      }
635
   }
677
   }
636
   //------------------------------------------------------------------------------------
678
   //------------------------------------------------------------------------------------
637
 
679
 
638
   for (int y = yFirst; y < yLast; ++y)
680
   for (int y = yFirst; y < yLast; ++y)
Line 642... Line 684...
642
      const uint32_t* s_m1 = src + srcWidth * MAX (y - 1, 0);
684
      const uint32_t* s_m1 = src + srcWidth * MAX (y - 1, 0);
643
      const uint32_t* s_0 = src + srcWidth * y; //center line
685
      const uint32_t* s_0 = src + srcWidth * y; //center line
644
      const uint32_t* s_p1 = src + srcWidth * MIN (y + 1, srcHeight - 1);
686
      const uint32_t* s_p1 = src + srcWidth * MIN (y + 1, srcHeight - 1);
645
      const uint32_t* s_p2 = src + srcWidth * MIN (y + 2, srcHeight - 1);
687
      const uint32_t* s_p2 = src + srcWidth * MIN (y + 2, srcHeight - 1);
646
 
688
 
647
      unsigned char blend_xy1 = 0; //corner blending for current (x, y + 1) position
689
      uint8_t blend_xy1 = 0; // corner blending for current (x, y + 1) position
648
 
690
 
649
      for (int x = 0; x < srcWidth; ++x, out += scaler->factor)
691
      for (int x = 0; x < srcWidth; ++x, out += scaler->factor)
650
      {
692
      {
651
         //all those bounds checks have only insignificant impact on performance!
693
         // all those bounds checks have only insignificant impact on performance!
652
         const int x_m1 = MAX (x - 1, 0); //perf: prefer array indexing to additional pointers!
694
         const int x_m1 = MAX (x - 1, 0); //perf: prefer array indexing to additional pointers!
653
         const int x_p1 = MIN (x + 1, srcWidth - 1);
695
         const int x_p1 = MIN (x + 1, srcWidth - 1);
654
         const int x_p2 = MIN (x + 2, srcWidth - 1);
696
         const int x_p2 = MIN (x + 2, srcWidth - 1);
-
 
697
 
655
         kernel_4x4_t ker4; //perf: initialization is negligible
698
         kernel_4x4_t ker4; //perf: initialization is negligible
-
 
699
         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
-
 
700
         ker4.e = s_0[x_m1];  ker4.f = s_0[x];  ker4.g = s_0[x_p1];  ker4.h = s_0[x_p2];
-
 
701
         ker4.i = s_p1[x_m1]; ker4.j = s_p1[x]; ker4.k = s_p1[x_p1]; ker4.l = s_p1[x_p2];
-
 
702
         ker4.m = s_p2[x_m1]; ker4.n = s_p2[x]; ker4.o = s_p2[x_p1]; ker4.p = s_p2[x_p2];
656
 
703
 
657
         ker4.a = s_m1[x_m1]; //read sequentially from memory as far as possible
704
         // evaluate the four corners on bottom-right of current pixel
-
 
705
         uint8_t blend_xy = 0; //for current (x, y) position
658
         ker4.b = s_m1[x];
706
         {
659
         ker4.c = s_m1[x_p1];
707
            blendresult_t res;
660
         ker4.d = s_m1[x_p2];
708
            preprocess_corners (&res, &ker4, color_format);
661
 
709
 
662
         ker4.e = s_0[x_m1];
710
            // preprocessing blend result:
663
         ker4.f = s_0[x];
711
            // ---------
-
 
712
            // | F | G |   //evalute corner between F, G, J, K
-
 
713
            // ----|---|   //current input pixel is at position F
664
         ker4.g = s_0[x_p1];
714
            // | J | K |
665
         ker4.h = s_0[x_p2];
715
            // ---------
666
 
716
 
667
         ker4.i = s_p1[x_m1];
-
 
668
         ker4.j = s_p1[x];
-
 
669
         ker4.k = s_p1[x_p1];
-
 
670
         ker4.l = s_p1[x_p2];
-
 
671
 
-
 
672
         ker4.m = s_p2[x_m1];
-
 
673
         ker4.n = s_p2[x];
-
 
674
         ker4.o = s_p2[x_p1];
-
 
675
         ker4.p = s_p2[x_p2];
-
 
676
 
-
 
677
         //evaluate the four corners on bottom-right of current pixel
-
 
678
         unsigned char blend_xy = 0; //for current (x, y) position
-
 
679
         {
-
 
680
            blendresult_t res;
-
 
681
            preProcessCorners (&res, &ker4, dist);
-
 
682
            /*
-
 
683
            preprocessing blend result:
-
 
684
            ---------
-
 
685
            | F | G |   //evalute corner between F, G, J, K
-
 
686
            ----|---|   //current input pixel is at position F
-
 
687
            | J | K |
-
 
688
            ---------
-
 
689
            */
-
 
690
            blend_xy = preProcBuffer[x];
717
            blend_xy = preProcBuffer[x];
691
            setBottomR (blend_xy, res.blend_f); //all four corners of (x, y) have been determined at this point due to processing sequence!
718
            setBottomR (&blend_xy, res.blend_f); //all four corners of (x, y) have been determined at this point due to processing sequence!
692
 
719
 
693
            setTopR (blend_xy1, res.blend_j); //set 2nd known corner for (x, y + 1)
720
            setTopR (&blend_xy1, res.blend_j); //set 2nd known corner for (x, y + 1)
694
            preProcBuffer[x] = blend_xy1; //store on current buffer position for use on next row
721
            preProcBuffer[x] = blend_xy1; //store on current buffer position for use on next row
695
 
722
 
696
            blend_xy1 = 0;
723
            blend_xy1 = 0;
697
            setTopL (blend_xy1, res.blend_k); //set 1st known corner for (x + 1, y + 1) and buffer for use on next column
724
            setTopL (&blend_xy1, res.blend_k); //set 1st known corner for (x + 1, y + 1) and buffer for use on next column
698
 
725
 
699
            if (x + 1 < bufferSize) //set 3rd known corner for (x + 1, y)
726
            if (x + 1 < bufferSize) //set 3rd known corner for (x + 1, y)
700
               setBottomL (preProcBuffer[x + 1], res.blend_g);
727
               setBottomL (&preProcBuffer[x + 1], res.blend_g);
701
         }
728
         }
702
 
729
 
703
         //fill block of size scale * scale with the given color
730
         //fill block of size scale * scale with the given color
704
         {
-
 
705
            uint32_t *blk = out;
731
         uint32_t *blk = out;
706
            for (int _blk_y = 0; _blk_y < scaler->factor; ++_blk_y, blk = (uint32_t *) BYTE_ADVANCE (blk, trgWidth * sizeof (uint32_t)))
732
         for (int _blk_y = 0; _blk_y < scaler->factor; ++_blk_y, blk = (uint32_t *) BYTE_ADVANCE (blk, trgWidth * sizeof (uint32_t)))
707
               for (int _blk_x = 0; _blk_x < scaler->factor; ++_blk_x)
733
            for (int _blk_x = 0; _blk_x < scaler->factor; ++_blk_x)
708
                  blk[_blk_x] = ker4.f;
734
               blk[_blk_x] = ker4.f;
709
         }
735
 
710
         //place *after* preprocessing step, to not overwrite the results while processing the the last pixel!
736
         //place *after* preprocessing step, to not overwrite the results while processing the the last pixel!
711
 
737
 
712
         //blend four corners of current pixel
738
         //blend four corners of current pixel
713
         if (blend_xy != 0) //good 5% perf-improvement
739
         if (blend_xy != 0) //good 5% perf-improvement
714
         {
740
         {
715
            kernel_3x3_t ker3; //perf: initialization is negligible
741
            kernel_3x3_t ker3; //perf: initialization is negligible
-
 
742
            ker3.a = ker4.a; ker3.b = ker4.b; ker3.c = ker4.c;
-
 
743
            ker3.d = ker4.e; ker3.e = ker4.f; ker3.f = ker4.g;
-
 
744
            ker3.g = ker4.i; ker3.h = ker4.j; ker3.i = ker4.k;
716
 
745
 
717
            ker3.a = ker4.a;
-
 
718
            ker3.b = ker4.b;
-
 
719
            ker3.c = ker4.c;
-
 
720
 
-
 
721
            ker3.d = ker4.e;
-
 
722
            ker3.e = ker4.f;
-
 
723
            ker3.f = ker4.g;
-
 
724
 
-
 
725
            ker3.g = ker4.i;
-
 
726
            ker3.h = ker4.j;
-
 
727
            ker3.i = ker4.k;
-
 
728
 
-
 
729
            blend_pixel (scaler, &ker3, out, trgWidth, blend_xy, alphagrad, dist, 0);
746
            blend_pixel (scaler, &ker3, out, trgWidth, blend_xy, color_format, outmatrixref_0);
730
            blend_pixel (scaler, &ker3, out, trgWidth, blend_xy, alphagrad, dist, 90);
747
            blend_pixel (scaler, &ker3, out, trgWidth, blend_xy, color_format, outmatrixref_90);
731
            blend_pixel (scaler, &ker3, out, trgWidth, blend_xy, alphagrad, dist, 180);
748
            blend_pixel (scaler, &ker3, out, trgWidth, blend_xy, color_format, outmatrixref_180);
732
            blend_pixel (scaler, &ker3, out, trgWidth, blend_xy, alphagrad, dist, 270);
749
            blend_pixel (scaler, &ker3, out, trgWidth, blend_xy, color_format, outmatrixref_270);
733
         }
750
         }
734
      }
751
      }
735
   }
752
   }
736
}
753
}
737
 
754
 
738
 
755
 
739
static double dist24 (uint32_t pix1, uint32_t pix2)
-
 
740
{
-
 
741
   //30% perf boost compared to plain distYCbCr()!
-
 
742
   //consumes 64 MB memory; using double is only 2% faster, but takes 128 MB
-
 
743
   static float diffToDist[256 * 256 * 256];
-
 
744
   static bool is_initialized = false;
756
/////////////////////
745
   if (!is_initialized)
757
// exported functions
746
   {
-
 
747
      for (uint32_t i = 0; i < 256 * 256 * 256; ++i) //startup time: 114 ms on Intel Core i5 (four cores)
-
 
748
      {
-
 
749
         const int r_diff = GET_RED (i) * 2 - 0xFF;
-
 
750
         const int g_diff = GET_GREEN (i) * 2 - 0xFF;
-
 
751
         const int b_diff = GET_BLUE (i) * 2 - 0xFF;
-
 
752
 
758
 
753
         const double k_b = 0.0593; //ITU-R BT.2020 conversion
-
 
754
         const double k_r = 0.2627; //
-
 
755
         const double k_g = 1 - k_b - k_r;
-
 
756
 
759
 
757
         const double scale_b = 0.5 / (1 - k_b);
-
 
758
         const double scale_r = 0.5 / (1 - k_r);
-
 
759
 
-
 
760
         const double y = k_r * r_diff + k_g * g_diff + k_b * b_diff; //[!], analog YCbCr!
-
 
761
         const double c_b = scale_b * (b_diff - y);
-
 
762
         const double c_r = scale_r * (r_diff - y);
-
 
763
 
-
 
764
         diffToDist[i] = (float) (sqrt ((y * y) + (c_b * c_b) + (c_r * c_r)));
-
 
765
      }
-
 
766
      is_initialized = true;
-
 
767
   }
-
 
768
 
-
 
769
   const int r_diff = (int) GET_RED (pix1) - (int) GET_RED (pix2);
-
 
770
   const int g_diff = (int) GET_GREEN (pix1) - (int) GET_GREEN (pix2);
-
 
771
   const int b_diff = (int) GET_BLUE (pix1) - (int) GET_BLUE (pix2);
-
 
772
 
-
 
773
   return diffToDist[(((r_diff + 0xFF) / 2) << 16) | //slightly reduce precision (division by 2) to squeeze value into single byte
760
void nearest_neighbor_scale (const uint32_t *src, int srcWidth, int srcHeight, uint32_t *trg, int trgWidth, int trgHeight)
774
      (((g_diff + 0xFF) / 2) << 8) |
-
 
775
      (((b_diff + 0xFF) / 2) << 0)];
-
 
776
}
-
 
777
 
-
 
778
 
-
 
779
static double dist32 (uint32_t pix1, uint32_t pix2)
-
 
780
{
761
{
781
   const double a1 = GET_ALPHA (pix1) / 255.0;
-
 
782
   const double a2 = GET_ALPHA (pix2) / 255.0;
-
 
783
   /*
-
 
784
   Requirements for a color distance handling alpha channel: with a1, a2 in [0, 1]
-
 
785
 
-
 
786
       1. if a1 = a2, distance should be: a1 * distYCbCr()
-
 
787
       2. if a1 = 0,  distance should be: a2 * distYCbCr(black, white) = a2 * 255
-
 
788
       3. if a1 = 1,  ??? maybe: 255 * (1 - a2) + a2 * distYCbCr()
-
 
789
   */
-
 
790
 
-
 
791
   //return MIN (a1, a2) * distYCbCrBuffered(pix1, pix2) + 255 * abs(a1 - a2);
-
 
792
   //=> following code is 15% faster:
-
 
793
   const double d = dist24 (pix1, pix2);
-
 
794
   return (a1 < a2 ? a1 * d + 255 * (a2 - a1) : a2 * d + 255 * (a1 - a2));
-
 
795
}
-
 
796
 
-
 
797
 
-
 
798
static void alphagrad24 (uint32_t *pixBack, uint32_t pixFront, unsigned int M, unsigned int N)
-
 
799
{
-
 
800
   // blend front color with opacity M / N over opaque background: http://en.wikipedia.org/wiki/Alpha_compositing#Alpha_blending
-
 
801
   *pixBack = ((CALC_COLOR24 (GET_RED (pixFront), GET_RED (*pixBack), M, N) << 16)
-
 
802
      | (CALC_COLOR24 (GET_GREEN (pixFront), GET_GREEN (*pixBack), M, N) << 8)
-
 
803
      | (CALC_COLOR24 (GET_BLUE (pixFront), GET_BLUE (*pixBack), M, N) << 0));
-
 
804
}
-
 
805
 
-
 
806
 
-
 
807
static void alphagrad32 (uint32_t *pixBack, uint32_t pixFront, unsigned int M, unsigned int N)
-
 
808
{
-
 
809
   // find intermediate color between two colors with alpha channels (=> NO alpha blending!!!)
-
 
810
   const unsigned int weightFront = GET_ALPHA (pixFront) * M;
-
 
811
   const unsigned int weightBack = GET_ALPHA (*pixBack) * (N - M);
-
 
812
   const unsigned int weightSum = weightFront + weightBack;
-
 
813
   *pixBack = (weightSum == 0 ? 0 :
-
 
814
      (((unsigned char) (weightSum / N)) << 24)
-
 
815
      | (CALC_COLOR32 (GET_RED (pixFront), GET_RED (*pixBack), weightFront, weightBack, weightSum) << 16)
-
 
816
      | (CALC_COLOR32 (GET_GREEN (pixFront), GET_GREEN (*pixBack), weightFront, weightBack, weightSum) << 8)
-
 
817
      | (CALC_COLOR32 (GET_BLUE (pixFront), GET_BLUE (*pixBack), weightFront, weightBack, weightSum) << 0));
-
 
818
}
-
 
819
 
-
 
820
 
-
 
821
EXTERN_C void nearestNeighborScale (const uint32_t *src, int srcWidth, int srcHeight, uint32_t *trg, int trgWidth, int trgHeight)
-
 
822
{
-
 
823
   //    nearestNeighborScale (src, srcWidth, srcHeight, srcWidth * sizeof (uint32_t), trg, trgWidth, trgHeight, trgWidth * sizeof (uint32_t), XBRZ_SLICETYPE_TARGET, 0, trgHeight, [](uint32_t pix) { return pix; });
-
 
824
       //static_assert(std::is_integral<PixSrc>::value, "PixSrc* is expected to be cast-able to char*");
-
 
825
       //static_assert(std::is_integral<PixTrg>::value, "PixTrg* is expected to be cast-able to char*");
-
 
826
       //static_assert(std::is_same<decltype(pixCvrt(PixSrc())), PixTrg>::value, "PixConverter returning wrong pixel format");
-
 
827
 
-
 
828
   int srcPitch = srcWidth * sizeof (uint32_t);
762
   int srcPitch = srcWidth * sizeof (uint32_t);
829
   int trgPitch = trgWidth * sizeof (uint32_t);
763
   int trgPitch = trgWidth * sizeof (uint32_t);
830
   int yFirst;
764
   int yFirst;
831
   int yLast;
765
   int yLast;
832
 
766
 
Line 895... Line 829...
895
 
829
 
896
   return;
830
   return;
897
}
831
}
898
 
832
 
899
 
833
 
900
EXTERN_C bool xbrz_equalcolortest24 (uint32_t col1, uint32_t col2, double luminanceWeight, double equalColorTolerance)
834
void xbrz_scale (size_t factor, const uint32_t *src, uint32_t *trg, int srcWidth, int srcHeight, bool has_alpha_channel)
901
{
835
{
902
   return (dist24 (col1, col2) < equalColorTolerance);
836
   if ((factor < 2) || (factor > 6))
903
}
-
 
-
 
837
      return; // consistency check
904
 
838
 
905
 
-
 
906
EXTERN_C bool xbrz_equalcolortest32 (uint32_t col1, uint32_t col2, double luminanceWeight, double equalColorTolerance)
-
 
907
{
-
 
908
   return (dist32 (col1, col2) < equalColorTolerance);
-
 
909
}
-
 
910
 
-
 
911
 
-
 
912
EXTERN_C void xbrz_scale24 (size_t factor, const uint32_t *src, uint32_t *trg, int srcWidth, int srcHeight)
-
 
913
{
-
 
914
   if (factor < 7)
-
 
915
      return scale_image (&scalers[factor - 2], src, trg, srcWidth, srcHeight, 0, srcHeight, alphagrad24, dist24);
839
   scale_image (&scalers[factor - 2], src, trg, srcWidth, srcHeight, 0, srcHeight, (has_alpha_channel ? &color_format_32 : &color_format_24));
916
}
-
 
917
 
-
 
918
 
-
 
919
EXTERN_C void xbrz_scale32 (size_t factor, const uint32_t *src, uint32_t *trg, int srcWidth, int srcHeight)
-
 
920
{
-
 
921
   if (factor < 7)
840
   return;
922
      return scale_image (&scalers[factor - 2], src, trg, srcWidth, srcHeight, 0, srcHeight, alphagrad32, dist32);
-
 
923
}
841
}