1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
|
// xImaInt.cpp : interpolation functions
/* 02/2004 - Branko Brevensek
* CxImage version 7.0.0 31/Dec/2010 - Davide Pizzolato - www.xdp.it
*/
#include "ximage.h"
#include "ximath.h"
#if CXIMAGE_SUPPORT_INTERPOLATION
////////////////////////////////////////////////////////////////////////////////
/**
* Recalculates coordinates according to specified overflow method.
* If pixel (x,y) lies within image, nothing changes.
*
* \param x, y - coordinates of pixel
* \param ofMethod - overflow method
*
* \return x, y - new coordinates (pixel (x,y) now lies inside image)
*
* \author ***bd*** 2.2004
*/
void CxImage::OverflowCoordinates(int32_t &x, int32_t &y, OverflowMethod const ofMethod)
{
if (IsInside(x,y)) return; //if pixel is within bounds, no change
switch (ofMethod) {
case OM_REPEAT:
//clip coordinates
x=max(x,0); x=min(x, head.biWidth-1);
y=max(y,0); y=min(y, head.biHeight-1);
break;
case OM_WRAP:
//wrap coordinates
x = x % head.biWidth;
y = y % head.biHeight;
if (x<0) x = head.biWidth + x;
if (y<0) y = head.biHeight + y;
break;
case OM_MIRROR:
//mirror pixels near border
if (x<0) x=((-x) % head.biWidth);
else if (x>=head.biWidth) x=head.biWidth-(x % head.biWidth + 1);
if (y<0) y=((-y) % head.biHeight);
else if (y>=head.biHeight) y=head.biHeight-(y % head.biHeight + 1);
break;
default:
return;
}//switch
}
////////////////////////////////////////////////////////////////////////////////
/**
* See OverflowCoordinates for integer version
* \author ***bd*** 2.2004
*/
void CxImage::OverflowCoordinates(float &x, float &y, OverflowMethod const ofMethod)
{
if (x>=0 && x<head.biWidth && y>=0 && y<head.biHeight) return; //if pixel is within bounds, no change
switch (ofMethod) {
case OM_REPEAT:
//clip coordinates
x=max(x,0); x=min(x, head.biWidth-1);
y=max(y,0); y=min(y, head.biHeight-1);
break;
case OM_WRAP:
//wrap coordinates
x = (float)fmod(x, (float) head.biWidth);
y = (float)fmod(y, (float) head.biHeight);
if (x<0) x = head.biWidth + x;
if (y<0) y = head.biHeight + y;
break;
case OM_MIRROR:
//mirror pixels near border
if (x<0) x=(float)fmod(-x, (float) head.biWidth);
else if (x>=head.biWidth) x=head.biWidth-((float)fmod(x, (float) head.biWidth) + 1);
if (y<0) y=(float)fmod(-y, (float) head.biHeight);
else if (y>=head.biHeight) y=head.biHeight-((float)fmod(y, (float) head.biHeight) + 1);
break;
default:
return;
}//switch
}
////////////////////////////////////////////////////////////////////////////////
/**
* Method return pixel color. Different methods are implemented for out of bounds pixels.
* If an image has alpha channel, alpha value is returned in .RGBReserved.
*
* \param x,y : pixel coordinates
* \param ofMethod : out-of-bounds method:
* - OF_WRAP - wrap over to pixels on other side of the image
* - OF_REPEAT - repeat last pixel on the edge
* - OF_COLOR - return input value of color
* - OF_BACKGROUND - return background color (if not set, return input color)
* - OF_TRANSPARENT - return transparent pixel
*
* \param rplColor : input color (returned for out-of-bound coordinates in OF_COLOR mode and if other mode is not applicable)
*
* \return color : color of pixel
* \author ***bd*** 2.2004
*/
RGBQUAD CxImage::GetPixelColorWithOverflow(int32_t x, int32_t y, OverflowMethod const ofMethod, RGBQUAD* const rplColor)
{
RGBQUAD color; //color to return
if ((!IsInside(x,y)) || pDib==NULL) { //is pixel within bouns?:
//pixel is out of bounds or no DIB
if (rplColor!=NULL)
color=*rplColor;
else {
color.rgbRed=color.rgbGreen=color.rgbBlue=255; color.rgbReserved=0; //default replacement colour: white transparent
}//if
if (pDib==NULL) return color;
//pixel is out of bounds:
switch (ofMethod) {
case OM_TRANSPARENT:
#if CXIMAGE_SUPPORT_ALPHA
if (AlphaIsValid()) {
//alpha transparency is supported and image has alpha layer
color.rgbReserved=0;
} else {
#endif //CXIMAGE_SUPPORT_ALPHA
//no alpha transparency
if (GetTransIndex()>=0) {
color=GetTransColor(); //single color transparency enabled (return transparent color)
}//if
#if CXIMAGE_SUPPORT_ALPHA
}//if
#endif //CXIMAGE_SUPPORT_ALPHA
return color;
case OM_BACKGROUND:
//return background color (if it exists, otherwise input value)
if (info.nBkgndIndex >= 0) {
if (head.biBitCount<24) color = GetPaletteColor((uint8_t)info.nBkgndIndex);
else color = info.nBkgndColor;
}//if
return color;
case OM_REPEAT:
case OM_WRAP:
case OM_MIRROR:
OverflowCoordinates(x,y,ofMethod);
break;
default:
//simply return replacement color (OM_COLOR and others)
return color;
}//switch
}//if
//just return specified pixel (it's within bounds)
return BlindGetPixelColor(x,y);
}
////////////////////////////////////////////////////////////////////////////////
/**
* This method reconstructs image according to chosen interpolation method and then returns pixel (x,y).
* (x,y) can lie between actual image pixels. If (x,y) lies outside of image, method returns value
* according to overflow method.
* This method is very useful for geometrical image transformations, where destination pixel
* can often assume color value lying between source pixels.
*
* \param (x,y) - coordinates of pixel to return
* GPCI method recreates "analogue" image back from digital data, so x and y
* are float values and color value of point (1.1,1) will generally not be same
* as (1,1). Center of first pixel is at (0,0) and center of pixel right to it is (1,0).
* (0.5,0) is half way between these two pixels.
* \param inMethod - interpolation (reconstruction) method (kernel) to use:
* - IM_NEAREST_NEIGHBOUR - returns colour of nearest lying pixel (causes stairy look of
* processed images)
* - IM_BILINEAR - interpolates colour from four neighbouring pixels (softens image a bit)
* - IM_BICUBIC - interpolates from 16 neighbouring pixels (can produce "halo" artifacts)
* - IM_BICUBIC2 - interpolates from 16 neighbouring pixels (perhaps a bit less halo artifacts
than IM_BICUBIC)
* - IM_BSPLINE - interpolates from 16 neighbouring pixels (softens image, washes colours)
* (As far as I know, image should be prefiltered for this method to give
* good results... some other time :) )
* This method uses bicubic interpolation kernel from CXImage 5.99a and older
* versions.
* - IM_LANCZOS - interpolates from 12*12 pixels (slow, ringing artifacts)
*
* \param ofMethod - overflow method (see comments at GetPixelColorWithOverflow)
* \param rplColor - pointer to color used for out of borders pixels in OM_COLOR mode
* (and other modes if colour can't calculated in a specified way)
*
* \return interpolated color value (including interpolated alpha value, if image has alpha layer)
*
* \author ***bd*** 2.2004
*/
RGBQUAD CxImage::GetPixelColorInterpolated(
float x,float y,
InterpolationMethod const inMethod,
OverflowMethod const ofMethod,
RGBQUAD* const rplColor)
{
//calculate nearest pixel
int32_t xi=(int32_t)(x); if (x<0) xi--; //these replace (incredibly slow) floor (Visual c++ 2003, AMD Athlon)
int32_t yi=(int32_t)(y); if (y<0) yi--;
RGBQUAD color; //calculated colour
switch (inMethod) {
case IM_NEAREST_NEIGHBOUR:
return GetPixelColorWithOverflow((int32_t)(x+0.5f), (int32_t)(y+0.5f), ofMethod, rplColor);
default: {
//IM_BILINEAR: bilinear interpolation
if (xi<-1 || xi>=head.biWidth || yi<-1 || yi>=head.biHeight) { //all 4 points are outside bounds?:
switch (ofMethod) {
case OM_COLOR: case OM_TRANSPARENT: case OM_BACKGROUND:
//we don't need to interpolate anything with all points outside in this case
return GetPixelColorWithOverflow(-999, -999, ofMethod, rplColor);
default:
//recalculate coordinates and use faster method later on
OverflowCoordinates(x,y,ofMethod);
xi=(int32_t)(x); if (x<0) xi--; //x and/or y have changed ... recalculate xi and yi
yi=(int32_t)(y); if (y<0) yi--;
}//switch
}//if
//get four neighbouring pixels
if ((xi+1)<head.biWidth && xi>=0 && (yi+1)<head.biHeight && yi>=0 && head.biClrUsed==0) {
//all pixels are inside RGB24 image... optimize reading (and use fixed point arithmetic)
uint16_t wt1=(uint16_t)((x-xi)*256.0f), wt2=(uint16_t)((y-yi)*256.0f);
uint16_t wd=wt1*wt2>>8;
uint16_t wb=wt1-wd;
uint16_t wc=wt2-wd;
uint16_t wa=256-wt1-wc;
uint16_t wrr,wgg,wbb;
uint8_t *pxptr=(uint8_t*)info.pImage+yi*info.dwEffWidth+xi*3;
wbb=wa*(*pxptr++); wgg=wa*(*pxptr++); wrr=wa*(*pxptr++);
wbb+=wb*(*pxptr++); wgg+=wb*(*pxptr++); wrr+=wb*(*pxptr);
pxptr+=(info.dwEffWidth-5); //move to next row
wbb+=wc*(*pxptr++); wgg+=wc*(*pxptr++); wrr+=wc*(*pxptr++);
wbb+=wd*(*pxptr++); wgg+=wd*(*pxptr++); wrr+=wd*(*pxptr);
color.rgbRed=(uint8_t) (wrr>>8); color.rgbGreen=(uint8_t) (wgg>>8); color.rgbBlue=(uint8_t) (wbb>>8);
#if CXIMAGE_SUPPORT_ALPHA
if (pAlpha) {
uint16_t waa;
//image has alpha layer... we have to do the same for alpha data
pxptr=AlphaGetPointer(xi,yi); //pointer to first byte
waa=wa*(*pxptr++); waa+=wb*(*pxptr); //first two pixels
pxptr+=(head.biWidth-1); //move to next row
waa+=wc*(*pxptr++); waa+=wd*(*pxptr); //and second row pixels
color.rgbReserved=(uint8_t) (waa>>8);
} else
#endif
{ //Alpha not supported or no alpha at all
color.rgbReserved = 0;
}
return color;
} else {
//default (slower) way to get pixels (not RGB24 or some pixels out of borders)
float t1=x-xi, t2=y-yi;
float d=t1*t2;
float b=t1-d;
float c=t2-d;
float a=1-t1-c;
RGBQUAD rgb11,rgb21,rgb12,rgb22;
rgb11=GetPixelColorWithOverflow(xi, yi, ofMethod, rplColor);
rgb21=GetPixelColorWithOverflow(xi+1, yi, ofMethod, rplColor);
rgb12=GetPixelColorWithOverflow(xi, yi+1, ofMethod, rplColor);
rgb22=GetPixelColorWithOverflow(xi+1, yi+1, ofMethod, rplColor);
//calculate linear interpolation
color.rgbRed=(uint8_t) (a*rgb11.rgbRed+b*rgb21.rgbRed+c*rgb12.rgbRed+d*rgb22.rgbRed);
color.rgbGreen=(uint8_t) (a*rgb11.rgbGreen+b*rgb21.rgbGreen+c*rgb12.rgbGreen+d*rgb22.rgbGreen);
color.rgbBlue=(uint8_t) (a*rgb11.rgbBlue+b*rgb21.rgbBlue+c*rgb12.rgbBlue+d*rgb22.rgbBlue);
#if CXIMAGE_SUPPORT_ALPHA
color.rgbReserved=(uint8_t) (a*rgb11.rgbReserved+b*rgb21.rgbReserved+c*rgb12.rgbReserved+d*rgb22.rgbReserved);
#else
color.rgbReserved = 0;
#endif
return color;
}//if
}//default
case IM_BICUBIC:
case IM_BICUBIC2:
case IM_BSPLINE:
case IM_BOX:
case IM_HERMITE:
case IM_HAMMING:
case IM_SINC:
case IM_BLACKMAN:
case IM_BESSEL:
case IM_GAUSSIAN:
case IM_QUADRATIC:
case IM_MITCHELL:
case IM_CATROM:
case IM_HANNING:
case IM_POWER:
//bicubic interpolation(s)
if (((xi+2)<0) || ((xi-1)>=head.biWidth) || ((yi+2)<0) || ((yi-1)>=head.biHeight)) { //all points are outside bounds?:
switch (ofMethod) {
case OM_COLOR: case OM_TRANSPARENT: case OM_BACKGROUND:
//we don't need to interpolate anything with all points outside in this case
return GetPixelColorWithOverflow(-999, -999, ofMethod, rplColor);
break;
default:
//recalculate coordinates and use faster method later on
OverflowCoordinates(x,y,ofMethod);
xi=(int32_t)(x); if (x<0) xi--; //x and/or y have changed ... recalculate xi and yi
yi=(int32_t)(y); if (y<0) yi--;
}//switch
}//if
//some variables needed from here on
int32_t xii,yii; //x any y integer indexes for loops
float kernel, kernelyc; //kernel cache
float kernelx[12], kernely[4]; //precalculated kernel values
float rr,gg,bb,aa; //accumulated color values
//calculate multiplication factors for all pixels
int32_t i;
switch (inMethod) {
case IM_BICUBIC:
for (i=0; i<4; i++) {
kernelx[i]=KernelCubic((float)(xi+i-1-x));
kernely[i]=KernelCubic((float)(yi+i-1-y));
}//for i
break;
case IM_BICUBIC2:
for (i=0; i<4; i++) {
kernelx[i]=KernelGeneralizedCubic((float)(xi+i-1-x), -0.5);
kernely[i]=KernelGeneralizedCubic((float)(yi+i-1-y), -0.5);
}//for i
break;
case IM_BSPLINE:
for (i=0; i<4; i++) {
kernelx[i]=KernelBSpline((float)(xi+i-1-x));
kernely[i]=KernelBSpline((float)(yi+i-1-y));
}//for i
break;
case IM_BOX:
for (i=0; i<4; i++) {
kernelx[i]=KernelBox((float)(xi+i-1-x));
kernely[i]=KernelBox((float)(yi+i-1-y));
}//for i
break;
case IM_HERMITE:
for (i=0; i<4; i++) {
kernelx[i]=KernelHermite((float)(xi+i-1-x));
kernely[i]=KernelHermite((float)(yi+i-1-y));
}//for i
break;
case IM_HAMMING:
for (i=0; i<4; i++) {
kernelx[i]=KernelHamming((float)(xi+i-1-x));
kernely[i]=KernelHamming((float)(yi+i-1-y));
}//for i
break;
case IM_SINC:
for (i=0; i<4; i++) {
kernelx[i]=KernelSinc((float)(xi+i-1-x));
kernely[i]=KernelSinc((float)(yi+i-1-y));
}//for i
break;
case IM_BLACKMAN:
for (i=0; i<4; i++) {
kernelx[i]=KernelBlackman((float)(xi+i-1-x));
kernely[i]=KernelBlackman((float)(yi+i-1-y));
}//for i
break;
case IM_BESSEL:
for (i=0; i<4; i++) {
kernelx[i]=KernelBessel((float)(xi+i-1-x));
kernely[i]=KernelBessel((float)(yi+i-1-y));
}//for i
break;
case IM_GAUSSIAN:
for (i=0; i<4; i++) {
kernelx[i]=KernelGaussian((float)(xi+i-1-x));
kernely[i]=KernelGaussian((float)(yi+i-1-y));
}//for i
break;
case IM_QUADRATIC:
for (i=0; i<4; i++) {
kernelx[i]=KernelQuadratic((float)(xi+i-1-x));
kernely[i]=KernelQuadratic((float)(yi+i-1-y));
}//for i
break;
case IM_MITCHELL:
for (i=0; i<4; i++) {
kernelx[i]=KernelMitchell((float)(xi+i-1-x));
kernely[i]=KernelMitchell((float)(yi+i-1-y));
}//for i
break;
case IM_CATROM:
for (i=0; i<4; i++) {
kernelx[i]=KernelCatrom((float)(xi+i-1-x));
kernely[i]=KernelCatrom((float)(yi+i-1-y));
}//for i
break;
case IM_HANNING:
for (i=0; i<4; i++) {
kernelx[i]=KernelHanning((float)(xi+i-1-x));
kernely[i]=KernelHanning((float)(yi+i-1-y));
}//for i
break;
case IM_POWER:
for (i=0; i<4; i++) {
kernelx[i]=KernelPower((float)(xi+i-1-x));
kernely[i]=KernelPower((float)(yi+i-1-y));
}//for i
break;
default:break;
}//switch
rr=gg=bb=aa=0;
if (((xi+2)<head.biWidth) && xi>=1 && ((yi+2)<head.biHeight) && (yi>=1) && !IsIndexed()) {
//optimized interpolation (faster pixel reads) for RGB24 images with all pixels inside bounds
for (yii=yi-1; yii<yi+3; yii++) {
uint8_t *pxptr=(uint8_t *)BlindGetPixelPointer(xi-1, yii); //calculate pointer to first byte in row
kernelyc=kernely[yii-(yi-1)];
#if CXIMAGE_SUPPORT_ALPHA
if (AlphaIsValid()) {
//alpha is supported and valid (optimized bicubic int32_t. for image with alpha)
uint8_t *pxptra=AlphaGetPointer(xi-1, yii);
kernel=kernelyc*kernelx[0];
bb+=kernel*(*pxptr++); gg+=kernel*(*pxptr++); rr+=kernel*(*pxptr++); aa+=kernel*(*pxptra++);
kernel=kernelyc*kernelx[1];
bb+=kernel*(*pxptr++); gg+=kernel*(*pxptr++); rr+=kernel*(*pxptr++); aa+=kernel*(*pxptra++);
kernel=kernelyc*kernelx[2];
bb+=kernel*(*pxptr++); gg+=kernel*(*pxptr++); rr+=kernel*(*pxptr++); aa+=kernel*(*pxptra++);
kernel=kernelyc*kernelx[3];
bb+=kernel*(*pxptr++); gg+=kernel*(*pxptr++); rr+=kernel*(*pxptr); aa+=kernel*(*pxptra);
} else
#endif
//alpha not supported or valid (optimized bicubic int32_t. for no alpha channel)
{
kernel=kernelyc*kernelx[0];
bb+=kernel*(*pxptr++); gg+=kernel*(*pxptr++); rr+=kernel*(*pxptr++);
kernel=kernelyc*kernelx[1];
bb+=kernel*(*pxptr++); gg+=kernel*(*pxptr++); rr+=kernel*(*pxptr++);
kernel=kernelyc*kernelx[2];
bb+=kernel*(*pxptr++); gg+=kernel*(*pxptr++); rr+=kernel*(*pxptr++);
kernel=kernelyc*kernelx[3];
bb+=kernel*(*pxptr++); gg+=kernel*(*pxptr++); rr+=kernel*(*pxptr);
}
}//yii
} else {
//slower more flexible interpolation for border pixels and paletted images
RGBQUAD rgbs;
for (yii=yi-1; yii<yi+3; yii++) {
kernelyc=kernely[yii-(yi-1)];
for (xii=xi-1; xii<xi+3; xii++) {
kernel=kernelyc*kernelx[xii-(xi-1)];
rgbs=GetPixelColorWithOverflow(xii, yii, ofMethod, rplColor);
rr+=kernel*rgbs.rgbRed;
gg+=kernel*rgbs.rgbGreen;
bb+=kernel*rgbs.rgbBlue;
#if CXIMAGE_SUPPORT_ALPHA
aa+=kernel*rgbs.rgbReserved;
#endif
}//xii
}//yii
}//if
//for all colors, clip to 0..255 and assign to RGBQUAD
if (rr>255) rr=255; if (rr<0) rr=0; color.rgbRed=(uint8_t) rr;
if (gg>255) gg=255; if (gg<0) gg=0; color.rgbGreen=(uint8_t) gg;
if (bb>255) bb=255; if (bb<0) bb=0; color.rgbBlue=(uint8_t) bb;
#if CXIMAGE_SUPPORT_ALPHA
if (aa>255) aa=255; if (aa<0) aa=0; color.rgbReserved=(uint8_t) aa;
#else
color.rgbReserved = 0;
#endif
return color;
case IM_LANCZOS:
//lanczos window (16*16) sinc interpolation
if (((xi+6)<0) || ((xi-5)>=head.biWidth) || ((yi+6)<0) || ((yi-5)>=head.biHeight)) {
//all points are outside bounds
switch (ofMethod) {
case OM_COLOR: case OM_TRANSPARENT: case OM_BACKGROUND:
//we don't need to interpolate anything with all points outside in this case
return GetPixelColorWithOverflow(-999, -999, ofMethod, rplColor);
break;
default:
//recalculate coordinates and use faster method later on
OverflowCoordinates(x,y,ofMethod);
xi=(int32_t)(x); if (x<0) xi--; //x and/or y have changed ... recalculate xi and yi
yi=(int32_t)(y); if (y<0) yi--;
}//switch
}//if
for (xii=xi-5; xii<xi+7; xii++) kernelx[xii-(xi-5)]=KernelLanczosSinc((float)(xii-x), 6.0f);
rr=gg=bb=aa=0;
if (((xi+6)<head.biWidth) && ((xi-5)>=0) && ((yi+6)<head.biHeight) && ((yi-5)>=0) && !IsIndexed()) {
//optimized interpolation (faster pixel reads) for RGB24 images with all pixels inside bounds
for (yii=yi-5; yii<yi+7; yii++) {
uint8_t *pxptr=(uint8_t *)BlindGetPixelPointer(xi-5, yii); //calculate pointer to first byte in row
kernelyc=KernelLanczosSinc((float)(yii-y),6.0f);
#if CXIMAGE_SUPPORT_ALPHA
if (AlphaIsValid()) {
//alpha is supported and valid
uint8_t *pxptra=AlphaGetPointer(xi-1, yii);
for (xii=0; xii<12; xii++) {
kernel=kernelyc*kernelx[xii];
bb+=kernel*(*pxptr++); gg+=kernel*(*pxptr++); rr+=kernel*(*pxptr++); aa+=kernel*(*pxptra++);
}//for xii
} else
#endif
//alpha not supported or valid
{
for (xii=0; xii<12; xii++) {
kernel=kernelyc*kernelx[xii];
bb+=kernel*(*pxptr++); gg+=kernel*(*pxptr++); rr+=kernel*(*pxptr++);
}//for xii
}
}//yii
} else {
//slower more flexible interpolation for border pixels and paletted images
RGBQUAD rgbs;
for (yii=yi-5; yii<yi+7; yii++) {
kernelyc=KernelLanczosSinc((float)(yii-y),6.0f);
for (xii=xi-5; xii<xi+7; xii++) {
kernel=kernelyc*kernelx[xii-(xi-5)];
rgbs=GetPixelColorWithOverflow(xii, yii, ofMethod, rplColor);
rr+=kernel*rgbs.rgbRed;
gg+=kernel*rgbs.rgbGreen;
bb+=kernel*rgbs.rgbBlue;
#if CXIMAGE_SUPPORT_ALPHA
aa+=kernel*rgbs.rgbReserved;
#endif
}//xii
}//yii
}//if
//for all colors, clip to 0..255 and assign to RGBQUAD
if (rr>255) rr=255; if (rr<0) rr=0; color.rgbRed=(uint8_t) rr;
if (gg>255) gg=255; if (gg<0) gg=0; color.rgbGreen=(uint8_t) gg;
if (bb>255) bb=255; if (bb<0) bb=0; color.rgbBlue=(uint8_t) bb;
#if CXIMAGE_SUPPORT_ALPHA
if (aa>255) aa=255; if (aa<0) aa=0; color.rgbReserved=(uint8_t) aa;
#else
color.rgbReserved = 0;
#endif
return color;
}//switch
}
////////////////////////////////////////////////////////////////////////////////
/**
* Helper function for GetAreaColorInterpolated.
* Adds 'surf' portion of image pixel with color 'color' to (rr,gg,bb,aa).
*/
void CxImage::AddAveragingCont(RGBQUAD const &color, float const surf, float &rr, float &gg, float &bb, float &aa)
{
rr+=color.rgbRed*surf;
gg+=color.rgbGreen*surf;
bb+=color.rgbBlue*surf;
#if CXIMAGE_SUPPORT_ALPHA
aa+=color.rgbReserved*surf;
#endif
}
////////////////////////////////////////////////////////////////////////////////
/**
* This method is similar to GetPixelColorInterpolated, but this method also properly handles
* subsampling.
* If you need to sample original image with interval of more than 1 pixel (as when shrinking an image),
* you should use this method instead of GetPixelColorInterpolated or aliasing will occur.
* When area width and height are both less than pixel, this method gets pixel color by interpolating
* color of frame center with selected (inMethod) interpolation by calling GetPixelColorInterpolated.
* If width and height are more than 1, method calculates color by averaging color of pixels within area.
* Interpolation method is not used in this case. Pixel color is interpolated by averaging instead.
* If only one of both is more than 1, method uses combination of interpolation and averaging.
* Chosen interpolation method is used, but since it is averaged later on, there is little difference
* between IM_BILINEAR (perhaps best for this case) and better methods. IM_NEAREST_NEIGHBOUR again
* leads to aliasing artifacts.
* This method is a bit slower than GetPixelColorInterpolated and when aliasing is not a problem, you should
* simply use the later.
*
* \param xc, yc - center of (rectangular) area
* \param w, h - width and height of area
* \param inMethod - interpolation method that is used, when interpolation is used (see above)
* \param ofMethod - overflow method used when retrieving individual pixel colors
* \param rplColor - replacement colour to use, in OM_COLOR
*
* \author ***bd*** 2.2004
*/
RGBQUAD CxImage::GetAreaColorInterpolated(
float const xc, float const yc, float const w, float const h,
InterpolationMethod const inMethod,
OverflowMethod const ofMethod,
RGBQUAD* const rplColor)
{
RGBQUAD color; //calculated colour
if (h<=1 && w<=1) {
//both width and height are less than one... we will use interpolation of center point
return GetPixelColorInterpolated(xc, yc, inMethod, ofMethod, rplColor);
} else {
//area is wider and/or taller than one pixel:
CxRect2 area(xc-w/2.0f, yc-h/2.0f, xc+w/2.0f, yc+h/2.0f); //area
int32_t xi1=(int32_t)(area.botLeft.x+0.49999999f); //low x
int32_t yi1=(int32_t)(area.botLeft.y+0.49999999f); //low y
int32_t xi2=(int32_t)(area.topRight.x+0.5f); //top x
int32_t yi2=(int32_t)(area.topRight.y+0.5f); //top y (for loops)
float rr,gg,bb,aa; //red, green, blue and alpha components
rr=gg=bb=aa=0;
int32_t x,y; //loop counters
float s=0; //surface of all pixels
float cps; //surface of current crosssection
if (h>1 && w>1) {
//width and height of area are greater than one pixel, so we can employ "ordinary" averaging
CxRect2 intBL, intTR; //bottom left and top right intersection
intBL=area.CrossSection(CxRect2(((float)xi1)-0.5f, ((float)yi1)-0.5f, ((float)xi1)+0.5f, ((float)yi1)+0.5f));
intTR=area.CrossSection(CxRect2(((float)xi2)-0.5f, ((float)yi2)-0.5f, ((float)xi2)+0.5f, ((float)yi2)+0.5f));
float wBL, wTR, hBL, hTR;
wBL=intBL.Width(); //width of bottom left pixel-area intersection
hBL=intBL.Height(); //height of bottom left...
wTR=intTR.Width(); //width of top right...
hTR=intTR.Height(); //height of top right...
AddAveragingCont(GetPixelColorWithOverflow(xi1,yi1,ofMethod,rplColor), wBL*hBL, rr, gg, bb, aa); //bottom left pixel
AddAveragingCont(GetPixelColorWithOverflow(xi2,yi1,ofMethod,rplColor), wTR*hBL, rr, gg, bb, aa); //bottom right pixel
AddAveragingCont(GetPixelColorWithOverflow(xi1,yi2,ofMethod,rplColor), wBL*hTR, rr, gg, bb, aa); //top left pixel
AddAveragingCont(GetPixelColorWithOverflow(xi2,yi2,ofMethod,rplColor), wTR*hTR, rr, gg, bb, aa); //top right pixel
//bottom and top row
for (x=xi1+1; x<xi2; x++) {
AddAveragingCont(GetPixelColorWithOverflow(x,yi1,ofMethod,rplColor), hBL, rr, gg, bb, aa); //bottom row
AddAveragingCont(GetPixelColorWithOverflow(x,yi2,ofMethod,rplColor), hTR, rr, gg, bb, aa); //top row
}
//leftmost and rightmost column
for (y=yi1+1; y<yi2; y++) {
AddAveragingCont(GetPixelColorWithOverflow(xi1,y,ofMethod,rplColor), wBL, rr, gg, bb, aa); //left column
AddAveragingCont(GetPixelColorWithOverflow(xi2,y,ofMethod,rplColor), wTR, rr, gg, bb, aa); //right column
}
for (y=yi1+1; y<yi2; y++) {
for (x=xi1+1; x<xi2; x++) {
color=GetPixelColorWithOverflow(x,y,ofMethod,rplColor);
rr+=color.rgbRed;
gg+=color.rgbGreen;
bb+=color.rgbBlue;
#if CXIMAGE_SUPPORT_ALPHA
aa+=color.rgbReserved;
#endif
}//for x
}//for y
} else {
//width or height greater than one:
CxRect2 intersect; //intersection with current pixel
CxPoint2 center;
for (y=yi1; y<=yi2; y++) {
for (x=xi1; x<=xi2; x++) {
intersect=area.CrossSection(CxRect2(((float)x)-0.5f, ((float)y)-0.5f, ((float)x)+0.5f, ((float)y)+0.5f));
center=intersect.Center();
color=GetPixelColorInterpolated(center.x, center.y, inMethod, ofMethod, rplColor);
cps=intersect.Surface();
rr+=color.rgbRed*cps;
gg+=color.rgbGreen*cps;
bb+=color.rgbBlue*cps;
#if CXIMAGE_SUPPORT_ALPHA
aa+=color.rgbReserved*cps;
#endif
}//for x
}//for y
}//if
s=area.Surface();
rr/=s; gg/=s; bb/=s; aa/=s;
if (rr>255) rr=255; if (rr<0) rr=0; color.rgbRed=(uint8_t) rr;
if (gg>255) gg=255; if (gg<0) gg=0; color.rgbGreen=(uint8_t) gg;
if (bb>255) bb=255; if (bb<0) bb=0; color.rgbBlue=(uint8_t) bb;
#if CXIMAGE_SUPPORT_ALPHA
if (aa>255) aa=255; if (aa<0) aa=0; color.rgbReserved=(uint8_t) aa;
#else
color.rgbReserved = 0;
#endif
}//if
return color;
}
////////////////////////////////////////////////////////////////////////////////
float CxImage::KernelBSpline(const float x)
{
if (x>2.0f) return 0.0f;
// thanks to Kristian Kratzenstein
float a, b, c, d;
float xm1 = x - 1.0f; // Was calculatet anyway cause the "if((x-1.0f) < 0)"
float xp1 = x + 1.0f;
float xp2 = x + 2.0f;
if ((xp2) <= 0.0f) a = 0.0f; else a = xp2*xp2*xp2; // Only float, not float -> double -> float
if ((xp1) <= 0.0f) b = 0.0f; else b = xp1*xp1*xp1;
if (x <= 0) c = 0.0f; else c = x*x*x;
if ((xm1) <= 0.0f) d = 0.0f; else d = xm1*xm1*xm1;
return (0.16666666666666666667f * (a - (4.0f * b) + (6.0f * c) - (4.0f * d)));
/* equivalent <Vladim�r Kloucek>
if (x < -2.0)
return(0.0f);
if (x < -1.0)
return((2.0f+x)*(2.0f+x)*(2.0f+x)*0.16666666666666666667f);
if (x < 0.0)
return((4.0f+x*x*(-6.0f-3.0f*x))*0.16666666666666666667f);
if (x < 1.0)
return((4.0f+x*x*(-6.0f+3.0f*x))*0.16666666666666666667f);
if (x < 2.0)
return((2.0f-x)*(2.0f-x)*(2.0f-x)*0.16666666666666666667f);
return(0.0f);
*/
}
////////////////////////////////////////////////////////////////////////////////
/**
* Bilinear interpolation kernel:
\verbatim
/
| 1-t , if 0 <= t <= 1
h(t) = | t+1 , if -1 <= t < 0
| 0 , otherwise
\
\endverbatim
* ***bd*** 2.2004
*/
float CxImage::KernelLinear(const float t)
{
// if (0<=t && t<=1) return 1-t;
// if (-1<=t && t<0) return 1+t;
// return 0;
//<Vladim�r Kloucek>
if (t < -1.0f)
return 0.0f;
if (t < 0.0f)
return 1.0f+t;
if (t < 1.0f)
return 1.0f-t;
return 0.0f;
}
////////////////////////////////////////////////////////////////////////////////
/**
* Bicubic interpolation kernel (a=-1):
\verbatim
/
| 1-2|t|**2+|t|**3 , if |t| < 1
h(t) = | 4-8|t|+5|t|**2-|t|**3 , if 1<=|t|<2
| 0 , otherwise
\
\endverbatim
* ***bd*** 2.2004
*/
float CxImage::KernelCubic(const float t)
{
float abs_t = (float)fabs(t);
float abs_t_sq = abs_t * abs_t;
if (abs_t<1) return 1-2*abs_t_sq+abs_t_sq*abs_t;
if (abs_t<2) return 4 - 8*abs_t +5*abs_t_sq - abs_t_sq*abs_t;
return 0;
}
////////////////////////////////////////////////////////////////////////////////
/**
* Bicubic kernel (for a=-1 it is the same as BicubicKernel):
\verbatim
/
| (a+2)|t|**3 - (a+3)|t|**2 + 1 , |t| <= 1
h(t) = | a|t|**3 - 5a|t|**2 + 8a|t| - 4a , 1 < |t| <= 2
| 0 , otherwise
\
\endverbatim
* Often used values for a are -1 and -1/2.
*/
float CxImage::KernelGeneralizedCubic(const float t, const float a)
{
float abs_t = (float)fabs(t);
float abs_t_sq = abs_t * abs_t;
if (abs_t<1) return (a+2)*abs_t_sq*abs_t - (a+3)*abs_t_sq + 1;
if (abs_t<2) return a*abs_t_sq*abs_t - 5*a*abs_t_sq + 8*a*abs_t - 4*a;
return 0;
}
////////////////////////////////////////////////////////////////////////////////
/**
* Lanczos windowed sinc interpolation kernel with radius r.
\verbatim
/
h(t) = | sinc(t)*sinc(t/r) , if |t|<r
| 0 , otherwise
\
\endverbatim
* ***bd*** 2.2004
*/
float CxImage::KernelLanczosSinc(const float t, const float r)
{
if (fabs(t) > r) return 0;
if (t==0) return 1;
float pit=PI*t;
float pitd=pit/r;
return (float)((sin(pit)/pit) * (sin(pitd)/pitd));
}
////////////////////////////////////////////////////////////////////////////////
float CxImage::KernelBox(const float x)
{
if (x < -0.5f)
return 0.0f;
if (x < 0.5f)
return 1.0f;
return 0.0f;
}
////////////////////////////////////////////////////////////////////////////////
float CxImage::KernelHermite(const float x)
{
if (x < -1.0f)
return 0.0f;
if (x < 0.0f)
return (-2.0f*x-3.0f)*x*x+1.0f;
if (x < 1.0f)
return (2.0f*x-3.0f)*x*x+1.0f;
return 0.0f;
// if (fabs(x)>1) return 0.0f;
// return(0.5f+0.5f*(float)cos(PI*x));
}
////////////////////////////////////////////////////////////////////////////////
float CxImage::KernelHanning(const float x)
{
if (fabs(x)>1) return 0.0f;
return (0.5f+0.5f*(float)cos(PI*x))*((float)sin(PI*x)/(PI*x));
}
////////////////////////////////////////////////////////////////////////////////
float CxImage::KernelHamming(const float x)
{
if (x < -1.0f)
return 0.0f;
if (x < 0.0f)
return 0.92f*(-2.0f*x-3.0f)*x*x+1.0f;
if (x < 1.0f)
return 0.92f*(2.0f*x-3.0f)*x*x+1.0f;
return 0.0f;
// if (fabs(x)>1) return 0.0f;
// return(0.54f+0.46f*(float)cos(PI*x));
}
////////////////////////////////////////////////////////////////////////////////
float CxImage::KernelSinc(const float x)
{
if (x == 0.0)
return(1.0);
return((float)sin(PI*x)/(PI*x));
}
////////////////////////////////////////////////////////////////////////////////
float CxImage::KernelBlackman(const float x)
{
//if (fabs(x)>1) return 0.0f;
return (0.42f+0.5f*(float)cos(PI*x)+0.08f*(float)cos(2.0f*PI*x));
}
////////////////////////////////////////////////////////////////////////////////
float CxImage::KernelBessel_J1(const float x)
{
double p, q;
register int32_t i;
static const double
Pone[] =
{
0.581199354001606143928050809e+21,
-0.6672106568924916298020941484e+20,
0.2316433580634002297931815435e+19,
-0.3588817569910106050743641413e+17,
0.2908795263834775409737601689e+15,
-0.1322983480332126453125473247e+13,
0.3413234182301700539091292655e+10,
-0.4695753530642995859767162166e+7,
0.270112271089232341485679099e+4
},
Qone[] =
{
0.11623987080032122878585294e+22,
0.1185770712190320999837113348e+20,
0.6092061398917521746105196863e+17,
0.2081661221307607351240184229e+15,
0.5243710262167649715406728642e+12,
0.1013863514358673989967045588e+10,
0.1501793594998585505921097578e+7,
0.1606931573481487801970916749e+4,
0.1e+1
};
p = Pone[8];
q = Qone[8];
for (i=7; i >= 0; i--)
{
p = p*x*x+Pone[i];
q = q*x*x+Qone[i];
}
return (float)(p/q);
}
////////////////////////////////////////////////////////////////////////////////
float CxImage::KernelBessel_P1(const float x)
{
double p, q;
register int32_t i;
static const double
Pone[] =
{
0.352246649133679798341724373e+5,
0.62758845247161281269005675e+5,
0.313539631109159574238669888e+5,
0.49854832060594338434500455e+4,
0.2111529182853962382105718e+3,
0.12571716929145341558495e+1
},
Qone[] =
{
0.352246649133679798068390431e+5,
0.626943469593560511888833731e+5,
0.312404063819041039923015703e+5,
0.4930396490181088979386097e+4,
0.2030775189134759322293574e+3,
0.1e+1
};
p = Pone[5];
q = Qone[5];
for (i=4; i >= 0; i--)
{
p = p*(8.0/x)*(8.0/x)+Pone[i];
q = q*(8.0/x)*(8.0/x)+Qone[i];
}
return (float)(p/q);
}
////////////////////////////////////////////////////////////////////////////////
float CxImage::KernelBessel_Q1(const float x)
{
double p, q;
register int32_t i;
static const double
Pone[] =
{
0.3511751914303552822533318e+3,
0.7210391804904475039280863e+3,
0.4259873011654442389886993e+3,
0.831898957673850827325226e+2,
0.45681716295512267064405e+1,
0.3532840052740123642735e-1
},
Qone[] =
{
0.74917374171809127714519505e+4,
0.154141773392650970499848051e+5,
0.91522317015169922705904727e+4,
0.18111867005523513506724158e+4,
0.1038187585462133728776636e+3,
0.1e+1
};
p = Pone[5];
q = Qone[5];
for (i=4; i >= 0; i--)
{
p = p*(8.0/x)*(8.0/x)+Pone[i];
q = q*(8.0/x)*(8.0/x)+Qone[i];
}
return (float)(p/q);
}
////////////////////////////////////////////////////////////////////////////////
float CxImage::KernelBessel_Order1(float x)
{
float p, q;
if (x == 0.0)
return (0.0f);
p = x;
if (x < 0.0)
x=(-x);
if (x < 8.0)
return(p*KernelBessel_J1(x));
q = (float)sqrt(2.0f/(PI*x))*(float)(KernelBessel_P1(x)*(1.0f/sqrt(2.0f)*(sin(x)-cos(x)))-8.0f/x*KernelBessel_Q1(x)*
(-1.0f/sqrt(2.0f)*(sin(x)+cos(x))));
if (p < 0.0f)
q = (-q);
return (q);
}
////////////////////////////////////////////////////////////////////////////////
float CxImage::KernelBessel(const float x)
{
if (x == 0.0f)
return(PI/4.0f);
return(KernelBessel_Order1(PI*x)/(2.0f*x));
}
////////////////////////////////////////////////////////////////////////////////
float CxImage::KernelGaussian(const float x)
{
return (float)(exp(-2.0f*x*x)*0.79788456080287f/*sqrt(2.0f/PI)*/);
}
////////////////////////////////////////////////////////////////////////////////
float CxImage::KernelQuadratic(const float x)
{
if (x < -1.5f)
return(0.0f);
if (x < -0.5f)
return(0.5f*(x+1.5f)*(x+1.5f));
if (x < 0.5f)
return(0.75f-x*x);
if (x < 1.5f)
return(0.5f*(x-1.5f)*(x-1.5f));
return(0.0f);
}
////////////////////////////////////////////////////////////////////////////////
float CxImage::KernelMitchell(const float x)
{
#define KM_B (1.0f/3.0f)
#define KM_C (1.0f/3.0f)
#define KM_P0 (( 6.0f - 2.0f * KM_B ) / 6.0f)
#define KM_P2 ((-18.0f + 12.0f * KM_B + 6.0f * KM_C) / 6.0f)
#define KM_P3 (( 12.0f - 9.0f * KM_B - 6.0f * KM_C) / 6.0f)
#define KM_Q0 (( 8.0f * KM_B + 24.0f * KM_C) / 6.0f)
#define KM_Q1 ((-12.0f * KM_B - 48.0f * KM_C) / 6.0f)
#define KM_Q2 (( 6.0f * KM_B + 30.0f * KM_C) / 6.0f)
#define KM_Q3 (( -1.0f * KM_B - 6.0f * KM_C) / 6.0f)
if (x < -2.0)
return(0.0f);
if (x < -1.0)
return(KM_Q0-x*(KM_Q1-x*(KM_Q2-x*KM_Q3)));
if (x < 0.0f)
return(KM_P0+x*x*(KM_P2-x*KM_P3));
if (x < 1.0f)
return(KM_P0+x*x*(KM_P2+x*KM_P3));
if (x < 2.0f)
return(KM_Q0+x*(KM_Q1+x*(KM_Q2+x*KM_Q3)));
return(0.0f);
}
////////////////////////////////////////////////////////////////////////////////
float CxImage::KernelCatrom(const float x)
{
if (x < -2.0)
return(0.0f);
if (x < -1.0)
return(0.5f*(4.0f+x*(8.0f+x*(5.0f+x))));
if (x < 0.0)
return(0.5f*(2.0f+x*x*(-5.0f-3.0f*x)));
if (x < 1.0)
return(0.5f*(2.0f+x*x*(-5.0f+3.0f*x)));
if (x < 2.0)
return(0.5f*(4.0f+x*(-8.0f+x*(5.0f-x))));
return(0.0f);
}
////////////////////////////////////////////////////////////////////////////////
float CxImage::KernelPower(const float x, const float a)
{
if (fabs(x)>1) return 0.0f;
return (1.0f - (float)fabs(pow(x,a)));
}
////////////////////////////////////////////////////////////////////////////////
#endif
|