26
26
return result;
27
27
}
28
28
29
+ template <typename T, typename D>
30
+ inline D PromoteTo (T t, float maxColors) {
31
+ D result = static_cast <D>((float )t / maxColors);
32
+ return result;
33
+ }
34
+
29
35
template <typename T>
30
36
inline T DemoteHalfTo (half t, float maxColors) {
31
37
return (T)clamp (((float )t * (float )maxColors), 0 .0f , (float )maxColors);
32
38
}
33
39
40
+ template <typename T, typename D>
41
+ inline D DemoteTo (T t, float maxColors) {
42
+ return (D)clamp (((float )t * (float )maxColors), 0 .0f , (float )maxColors);
43
+ }
44
+
34
45
template <typename T>
35
46
inline T CubicBSpline (T t) {
36
47
T absX = abs (t);
@@ -131,8 +142,8 @@ void scaleImageFloat16(uint16_t* input,
131
142
int y1 = static_cast <int >(srcY);
132
143
133
144
if (option == bilinear) {
134
- int x2 = std:: min (x1 + 1 , inputWidth - 1 );
135
- int y2 = std:: min (y1 + 1 , inputHeight - 1 );
145
+ int x2 = min (x1 + 1 , inputWidth - 1 );
146
+ int y2 = min (y1 + 1 , inputHeight - 1 );
136
147
137
148
half dx (x2 - x1);
138
149
half dy (y2 - y1);
@@ -150,9 +161,6 @@ void scaleImageFloat16(uint16_t* input,
150
161
dst16[x*components + c] = result.data_ ;
151
162
}
152
163
} else if (option == cubic) {
153
- half dx = half (srcX - x);
154
- half dy = half (srcY - y);
155
-
156
164
half rgb[components];
157
165
158
166
for (int j = -1 ; j <= 2 ; j++) {
@@ -163,7 +171,7 @@ void scaleImageFloat16(uint16_t* input,
163
171
half weight = CubicBSpline (half (srcX - xi)) * CubicBSpline (half (srcY - yj));
164
172
165
173
for (int c = 0 ; c < components; ++c) {
166
- half clrf = castU16 (reinterpret_cast <const uint16_t *>(src8 + clamp (yj, 0 , inputHeight - 1 ) * srcStride)[clamp (x , 0 , inputWidth - 1 )*components + c]);
174
+ half clrf = castU16 (reinterpret_cast <const uint16_t *>(src8 + clamp (yj, 0 , inputHeight - 1 ) * srcStride)[clamp (xi , 0 , inputWidth - 1 )*components + c]);
167
175
half clr = clrf * weight;
168
176
rgb[c] += clr;
169
177
}
@@ -174,9 +182,6 @@ void scaleImageFloat16(uint16_t* input,
174
182
dst16[x*components + c] = rgb[c].data_ ;
175
183
}
176
184
} else if (option == mitchell) {
177
- half dx = half (srcX - x);
178
- half dy = half (srcY - y);
179
-
180
185
half rgb[components];
181
186
182
187
for (int j = -1 ; j <= 2 ; j++) {
@@ -214,37 +219,38 @@ void scaleImageFloat16(uint16_t* input,
214
219
dst16[x*components + c] += clr.data_ ;
215
220
}
216
221
} else if (option == lanczos) {
217
- half rgb[components];
222
+ float rgb[components];
223
+ memset (&rgb[0 ], 0 .0f , sizeof (float ) * components);
218
224
219
225
int a = 3 ;
220
226
float lanczosFA = float (3 .0f );
221
227
222
228
float kx1 = floor (srcX);
223
229
float ky1 = floor (srcY);
224
230
225
- half weightSum (0 .0f );
231
+ float weightSum (0 .0f );
226
232
227
233
for (int j = -a + 1 ; j <= a; j++) {
228
234
for (int i = -a + 1 ; i <= a; i++) {
229
235
int xi = kx1 + i;
230
236
int yj = ky1 + j;
231
- half dx = half (srcX) - (half (kx1) + (half )i);
232
- half dy = half (srcY) - (half (ky1) + (half )j);
233
- half weight = lanczosWindow (dx, (half) lanczosFA) * lanczosWindow (dy, (half) lanczosFA);
237
+ float dx = float (srcX) - (float (kx1) + (float )i);
238
+ float dy = float (srcY) - (float (ky1) + (float )j);
239
+ float weight = lanczosWindow (dx, lanczosFA) * lanczosWindow (dy, lanczosFA);
234
240
weightSum += weight;
235
241
for (int c = 0 ; c < components; ++c) {
236
242
half clrf = castU16 (reinterpret_cast <const uint16_t *>(src8 + clamp (yj, 0 , inputHeight - 1 ) * srcStride)[clamp (xi, 0 , inputWidth - 1 )*components + c]);
237
- half clr = half ( clrf * weight) ;
243
+ float clr = ( float ) clrf * weight;
238
244
rgb[c] += clr;
239
245
}
240
246
}
241
247
}
242
248
243
249
for (int c = 0 ; c < components; ++c) {
244
250
if (weightSum == 0 ) {
245
- dst16[x*components + c] = rgb[c].data_ ;
251
+ dst16[x*components + c] = half ( rgb[c]) .data_ ;
246
252
} else {
247
- dst16[x*components + c] = (rgb[c] / weightSum).data_ ;
253
+ dst16[x*components + c] = half (rgb[c] / weightSum).data_ ;
248
254
}
249
255
}
250
256
} else {
@@ -382,37 +388,39 @@ void scaleImageU16(uint16_t* input,
382
388
dst16[x*components + c] += clr;
383
389
}
384
390
} else if (option == lanczos) {
385
- half rgb[components];
391
+ float rgb[components];
392
+ memset (&rgb[0 ], 0 .0f , sizeof (float ) * components);
386
393
387
- int a = 3 ;
388
- half lanczosFA = half (3 .0f );
394
+ float lanczosFA = float (3 .0f );
389
395
390
- half weightSum ( 0 . 0f ) ;
396
+ int a = 3 ;
391
397
392
398
float kx1 = floor (srcX);
393
399
float ky1 = floor (srcY);
394
400
401
+ float weightSum (0 .0f );
402
+
395
403
for (int j = -a + 1 ; j <= a; j++) {
396
404
for (int i = -a + 1 ; i <= a; i++) {
397
405
int xi = kx1 + i;
398
406
int yj = ky1 + j;
399
- half dx = half (srcX) - (half (kx1) + (half )i);
400
- half dy = half (srcY) - (half (ky1) + (half )j);
401
- half weight = lanczosWindow (dx, (half )lanczosFA) * lanczosWindow (dy, (half )lanczosFA);
407
+ float dx = float (srcX) - (float (kx1) + (float )i);
408
+ float dy = float (srcY) - (float (ky1) + (float )j);
409
+ float weight = lanczosWindow (dx, (float )lanczosFA) * lanczosWindow (dy, (float )lanczosFA);
402
410
weightSum += weight;
403
411
for (int c = 0 ; c < components; ++c) {
404
- half clrf = PromoteToHalf (reinterpret_cast <const uint16_t *>(src8 + clamp (yj, 0 , inputHeight - 1 ) * srcStride)[clamp (xi, 0 , inputWidth - 1 )*components + c], maxColors);
405
- half clr = half ( clrf * weight) ;
412
+ float clrf = PromoteTo< uint16_t , float > (reinterpret_cast <const uint16_t *>(src8 + clamp (yj, 0 , inputHeight - 1 ) * srcStride)[clamp (xi, 0 , inputWidth - 1 )*components + c], maxColors);
413
+ float clr = clrf * weight;
406
414
rgb[c] += clr;
407
415
}
408
416
}
409
417
}
410
418
411
419
for (int c = 0 ; c < components; ++c) {
412
420
if (weightSum == 0 ) {
413
- dst16[x*components + c] = DemoteHalfTo< uint16_t >(rgb[c], maxColors);
421
+ dst16[x*components + c] = DemoteTo< float , uint16_t >(rgb[c], maxColors);
414
422
} else {
415
- dst16[x*components + c] = DemoteHalfTo< uint16_t >(rgb[c] / weightSum, maxColors);
423
+ dst16[x*components + c] = DemoteTo< float , uint16_t >(rgb[c] / weightSum, maxColors);
416
424
}
417
425
}
418
426
} else {
@@ -543,38 +551,39 @@ void scaleImageU8(uint8_t* input,
543
551
dst[x*components + c] += clr;
544
552
}
545
553
} else if (option == lanczos) {
546
- half rgb[components];
554
+ float rgb[components];
555
+ memset (&rgb[0 ], 0 .0f , sizeof (float ) * components);
547
556
548
- half lanczosFA = half (3 .0f );
557
+ float lanczosFA = float (3 .0f );
549
558
550
559
int a = 3 ;
551
560
552
561
float kx1 = floor (srcX);
553
562
float ky1 = floor (srcY);
554
563
555
- half weightSum (0 .0f );
564
+ float weightSum (0 .0f );
556
565
557
566
for (int j = -a + 1 ; j <= a; j++) {
558
567
for (int i = -a + 1 ; i <= a; i++) {
559
568
int xi = kx1 + i;
560
569
int yj = ky1 + j;
561
- half dx = half (srcX) - (half (kx1) + (half )i);
562
- half dy = half (srcY) - (half (ky1) + (half )j);
563
- half weight = lanczosWindow (dx, (half )lanczosFA) * lanczosWindow (dy, (half )lanczosFA);
570
+ float dx = float (srcX) - (float (kx1) + (float )i);
571
+ float dy = float (srcY) - (float (ky1) + (float )j);
572
+ float weight = lanczosWindow (dx, (float )lanczosFA) * lanczosWindow (dy, (float )lanczosFA);
564
573
weightSum += weight;
565
574
for (int c = 0 ; c < components; ++c) {
566
- half clrf = PromoteToHalf (reinterpret_cast <const uint8_t *>(src8 + clamp (yj, 0 , inputHeight - 1 ) * srcStride)[clamp (xi, 0 , inputWidth - 1 )*components + c], maxColors);
567
- half clr = half ( clrf * weight) ;
575
+ float clrf = PromoteTo< uint8_t , float > (reinterpret_cast <const uint8_t *>(src8 + clamp (yj, 0 , inputHeight - 1 ) * srcStride)[clamp (xi, 0 , inputWidth - 1 )*components + c], maxColors);
576
+ float clr = clrf * weight;
568
577
rgb[c] += clr;
569
578
}
570
579
}
571
580
}
572
581
573
582
for (int c = 0 ; c < components; ++c) {
574
583
if (weightSum == 0 ) {
575
- dst[x*components + c] = DemoteHalfTo< uint8_t >(rgb[c], maxColors);
584
+ dst[x*components + c] = DemoteTo< float , uint8_t >(rgb[c], maxColors);
576
585
} else {
577
- dst[x*components + c] = DemoteHalfTo< uint8_t >(rgb[c] / weightSum, maxColors);
586
+ dst[x*components + c] = DemoteTo< float , uint8_t >(rgb[c] / weightSum, maxColors);
578
587
}
579
588
}
580
589
} else {
0 commit comments