-
Notifications
You must be signed in to change notification settings - Fork 7
/
resample.c
executable file
·2703 lines (2267 loc) · 89.1 KB
/
resample.c
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
/* Panorama_Tools - Generate, Edit and Convert Panoramic Images
Copyright (C) 1998,1999 - Helmut Dersch [email protected]
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this software; see the file COPYING. If not, a copy
can be downloaded from http://www.gnu.org/licenses/gpl.html, or
obtained by writing to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
/*------------------------------------------------------------*/
// Added Fix of Kekus Kratzke: March.2004
// Radial Shift, when colors channels have different values and
// d is > 1 would give incorrect results around the edge of the image
// Modified by Fulvio Senore: June.2004
// Added linear interpolation between pixels in the geometric transform phase
// to speed up computation.
// Rik Littlefield added interface to morpher.c in July 2004 to avoid local errors caused
// by morphing.
// Changes are bracketed between
//
// // FS+
//
// and
//
// // FS-
//
// comments
/*------------------------------------------------------------
JMW - merged in Rob Platt changes Oct 18, 2005
11-June-2004 Rob Platt - Modified MyTransForm() for multithreading.
launch one child task per CPU.
Since this does no file or net I/O, there is no advantage to more tasks than CPUs.
12-June-2004 .. - Run one instance of MyTransFormBody() in the context of the parent
and launch (n-1) child tasks. The parent updates the Progress indicator
- transForm() now calls MyTransForm(); this makes transForm multithreaded, too.
Eliminated duplicate code.
26-July-2004 Rob Platt - Get version from the version.h rather than always updating it here...
.. - 1st step: Add Handling of 2-color resampling (Use same method as
3-color, just display a different progress indicator)
Define 3 new 'composite' colors (R+G, R+B, G+B). See correct.c
27-July-2004 - K.K. sent me the bugfix if different factors were set for each
color channel, merged with my mods.
28-July-2004 .. - Clean handling of 2-color resampling: The third color is left untouched.
17-Aug-2004 R.Platt - test out of memory condition (Just to be safe)
- #ifdef the parts not needed on Mac
10-Sep-2005 R.Platt - Testing for alphamasked pixels to we can interpolate
right up to the edge of an alphamasked region
without introducing contributions of non-existent pixels
at the edge of the interpolated input
13-Sep-2005 R.Platt - Renormalization when sampling a region that some pixels are masked and others not.
15-Oct-2005 R.Platt - Changing alphamask threshold to 94% (15/16ths) due to strange masks from Photoshop
------------------------------------------------------------*/
// Program specific includes
#include "version.h"
#include "filter.h"
// Standard C includes
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <assert.h>
#if _MSC_VER > 1000
#pragma warning(disable: 4100) // disable unreferenced formal parameter warning
#endif
/*------------------------------------------------------------
PROTOTYPES:
*/
//static OSStatus MyTransFormBody( MyTransFormCmdPara_s *MyTransFormCmdPara);
void MyTransForm( TrformStr *TrPtr, fDesc *fD, int color, int imageNum);
//static void FindYbounds( MyTransFormCmdPara_s *FindYboundsPara);
void transForm_aa( TrformStr *TrPtr, fDesc *fD,fDesc *finvD, int color, int imageNum);
// This file uses functions of type
// resample( unsigned char *dst, unsigned char **rgb,
// register double Dx,
// register double Dy,
// int color, int SamplesPerPixel);
//
// dst - output pixel
// rgb - input pixels, may be Lab as well.
// Dx - offset of output pixel position in x-direction
// Dy - offset of output pixel position in y-direction
// color = 0: all rgb colors; color = 1,2,3: one of r,g,b
// color=4,5,6:process 2 channels (4:r+g, 5:r+b, 6:g+b)
// BytesPerPixel = 3,4. Using color != 0, any value should (?) work.
// Arrays used for Gamma correction
PTGamma glu; // Lookup table
// prototype to avoid a warning: the function is defined in morpher.c
int getLastCurTriangle();
// Some locally needed math functions
static double sinc ( double x );
static double cubic01 ( double x );
static double cubic12 ( double x );
// Interpolators
static void nn( unsigned char *dst, unsigned char **rgb,
register double Dx, register double Dy, int color, int SamplesPerPixel);
static void bil( unsigned char *dst, unsigned char **rgb,
register double Dx, register double Dy, int color, int SamplesPerPixel);
static void poly3( unsigned char *dst, unsigned char **rgb,
register double Dx, register double Dy, int color, int SamplesPerPixel);
static void spline16( unsigned char *dst, unsigned char **rgb,
register double Dx, register double Dy, int color, int SamplesPerPixel);
static void spline36( unsigned char *dst, unsigned char **rgb,
register double Dx, register double Dy, int color, int SamplesPerPixel);
static void spline64( unsigned char *dst, unsigned char **rgb,
register double Dx, register double Dy, int color, int SamplesPerPixel);
static void sinc256( unsigned char *dst, unsigned char **rgb,
register double Dx, register double Dy, int color, int SamplesPerPixel);
static void sinc1024( unsigned char *dst, unsigned char **rgb,
register double Dx, register double Dy, int color, int SamplesPerPixel);
static void nn_16( unsigned char *dst, unsigned char **rgb,
register double Dx, register double Dy, int color, int SamplesPerPixel);
static void bil_16( unsigned char *dst, unsigned char **rgb,
register double Dx, register double Dy, int color, int SamplesPerPixel);
static void poly3_16( unsigned char *dst, unsigned char **rgb,
register double Dx, register double Dy, int color, int SamplesPerPixel);
static void spline16_16( unsigned char *dst, unsigned char **rgb,
register double Dx, register double Dy, int color, int SamplesPerPixel);
static void spline36_16( unsigned char *dst, unsigned char **rgb,
register double Dx, register double Dy, int color, int SamplesPerPixel);
static void spline64_16( unsigned char *dst, unsigned char **rgb,
register double Dx, register double Dy, int color, int SamplesPerPixel);
static void sinc256_16( unsigned char *dst, unsigned char **rgb,
register double Dx, register double Dy, int color, int SamplesPerPixel);
static void sinc1024_16( unsigned char *dst, unsigned char **rgb,
register double Dx, register double Dy, int color, int SamplesPerPixel);
// Various interpolators; a[] is array of coeeficients; 0 <= x < 1
#define NNEIGHBOR(x, a , NDIM ) \
a[0] = 1.0;
#define BILINEAR(x, a, NDIM ) \
a[1] = x; \
a[0] = 1.0 - x;
// Unused; has been replaced by 'CUBIC'.
#define POLY3( x, a , NDIM ) \
a[3] = ( x * x - 1.0) * x / 6.0; \
a[2] = ( (1.0 - x) * x / 2.0 + 1.0) * x; \
a[1] = ( ( 1.0/2.0 * x - 1.0 ) * x - 1.0/2.0 ) * x + 1.0; \
a[0] = ( ( -1.0/6.0 * x + 1.0/2.0 ) * x - 1.0/3.0 ) * x ;
#define SPLINE16( x, a, NDIM ) \
a[3] = ( ( 1.0/3.0 * x - 1.0/5.0 ) * x - 2.0/15.0 ) * x; \
a[2] = ( ( 6.0/5.0 - x ) * x + 4.0/5.0 ) * x; \
a[1] = ( ( x - 9.0/5.0 ) * x - 1.0/5.0 ) * x + 1.0; \
a[0] = ( ( -1.0/3.0 * x + 4.0/5.0 ) * x - 7.0/15.0 ) * x ;
#define CUBIC( x, a, NDIM ) \
a[3] = cubic12( 2.0 - x); \
a[2] = cubic01( 1.0 - x); \
a[1] = cubic01( x ); \
a[0] = cubic12( x + 1.0); \
#define SPLINE36( x, a , NDIM ) \
a[5] = ( ( - 1.0/11.0 * x + 12.0/ 209.0 ) * x + 7.0/ 209.0 ) * x; \
a[4] = ( ( 6.0/11.0 * x - 72.0/ 209.0 ) * x - 42.0/ 209.0 ) * x; \
a[3] = ( ( - 13.0/11.0 * x + 288.0/ 209.0 ) * x + 168.0/ 209.0 ) * x; \
a[2] = ( ( 13.0/11.0 * x - 453.0/ 209.0 ) * x - 3.0/ 209.0 ) * x + 1.0; \
a[1] = ( ( - 6.0/11.0 * x + 270.0/ 209.0 ) * x - 156.0/ 209.0 ) * x; \
a[0] = ( ( 1.0/11.0 * x - 45.0/ 209.0 ) * x + 26.0/ 209.0 ) * x;
#define SPLINE64( x, a , NDIM ) \
a[7] = (( 1.0/41.0 * x - 45.0/2911.0) * x - 26.0/2911.0) * x; \
a[6] = ((- 6.0/41.0 * x + 270.0/2911.0) * x + 156.0/2911.0) * x; \
a[5] = (( 24.0/41.0 * x - 1080.0/2911.0) * x - 624.0/2911.0) * x; \
a[4] = ((-49.0/41.0 * x + 4050.0/2911.0) * x + 2340.0/2911.0) * x; \
a[3] = (( 49.0/41.0 * x - 6387.0/2911.0) * x - 3.0/2911.0) * x + 1.0; \
a[2] = ((-24.0/41.0 * x + 4032.0/2911.0) * x - 2328.0/2911.0) * x; \
a[1] = (( 6.0/41.0 * x - 1008.0/2911.0) * x + 582.0/2911.0) * x; \
a[0] = ((- 1.0/41.0 * x + 168.0/2911.0) * x - 97.0/2911.0) * x;
#define SINC( x, a, NDIM ) \
{ \
register int idx; \
register double xadd; \
for( idx = 0, xadd = NDIM / 2 - 1.0 + x; \
idx < NDIM / 2; \
xadd-=1.0) \
{ \
a[idx++] = sinc( xadd ) * sinc( xadd / ( NDIM / 2 )); \
} \
for( xadd = 1.0 - x; \
idx < NDIM; \
xadd+=1.0) \
{ \
a[idx++] = sinc( xadd ) * sinc( xadd / ( NDIM / 2 )); \
} \
} \
// Set up the arrays for gamma correction
int SetUpGamma( double pgamma, unsigned int psize)
{
int i;
double gnorm, xg, rgamma = 1.0/pgamma;
if( psize == 1 )
{
glu.ChannelSize = 256;
glu.ChannelStretch = 16;
}
else if( psize == 2 )
{
glu.ChannelSize = 65536;
glu.ChannelStretch = 4;
}
else
return -1;
glu.GammaSize = glu.ChannelSize * glu.ChannelStretch;
glu.DeGamma = NULL;
glu.Gamma = NULL;
glu.DeGamma = (double*) malloc( glu.ChannelSize * sizeof( double ) );
glu.Gamma = (unsigned short*) malloc( glu.GammaSize * sizeof( unsigned short) );
if( glu.DeGamma == NULL || glu.Gamma == NULL )
{
PrintError("Not enough memory");
return -1;
}
glu.DeGamma[0] = 0.0;
gnorm = (glu.ChannelSize-1) / pow( glu.ChannelSize-1 , pgamma ) ;
for(i=1; i<glu.ChannelSize; i++)
{
glu.DeGamma[i] = pow( (double)i , pgamma ) * gnorm;
}
glu.Gamma[0] = 0;
gnorm = (glu.ChannelSize-1) / pow( glu.ChannelSize-1 , rgamma ) ;
if( psize == 1 )
{
for(i=1; i<glu.GammaSize; i++)
{
xg = pow( ((double)i) / glu.ChannelStretch , rgamma ) * gnorm;
DBL_TO_UC( glu.Gamma[i], xg );
}
}
else
{
for(i=1; i<glu.GammaSize; i++)
{
xg = pow( ((double)i) / glu.ChannelStretch , rgamma ) * gnorm;
DBL_TO_US( glu.Gamma[i], xg );
}
}
return 0;
}
unsigned short gamma_correct( double pix )
{
int k = (int)(glu.ChannelStretch * pix);
if( k < 0 )
return 0;
if( k > glu.GammaSize - 1 )
return glu.ChannelSize - 1;
return (glu.Gamma)[ k ] ;
}
#define gamma_char(pix) (char)(gamma_correct(pix))
#define gamma_short(pix) (short)(gamma_correct(pix))
#define gamma_float(pix) (float)(pix)
#define degamma_char(pix) glu.DeGamma[pix]
#define degamma_short(pix) glu.DeGamma[pix]
#define degamma_float(pix) pix
/////////// N x N Sampler /////////////////////////////////////////////
#define RESAMPLE_N( intpol, ndim, psize ) \
double ya[ndim]; \
double yr[ndim], yg[ndim], yb[ndim], w[ndim]; \
register double ad; \
register double rd, gd, bd, weight ; \
register int k,i; \
register unsigned psize *r, *ri; \
register unsigned psize *tdst; \
int alpha_ok = TRUE; \
\
intpol( Dx, w, ndim ) \
if( color == 0) \
{ \
for(k=0; k<ndim; k++) \
{ \
r = ((unsigned psize**)rgb)[k]; \
ad = 0.0; \
rd = gd = bd = 0.0; \
\
for(i=0; i<ndim; i++) \
{ \
weight = w[ i ]; \
ri = r + i * SamplesPerPixel; \
if(SamplesPerPixel==4) \
{ \
if( ((int)*ri++) < threshold) \
alpha_ok = FALSE; \
else \
{ \
ad += weight; \
rd += degamma_##psize((int)*ri++) * weight; \
gd += degamma_##psize((int)*ri++) * weight; \
bd += degamma_##psize((int)*ri) * weight; \
} \
} \
else \
{ \
rd += degamma_##psize((int)*ri++) * weight; \
gd += degamma_##psize((int)*ri++) * weight; \
bd += degamma_##psize((int)*ri) * weight; \
} \
} \
ya[k] = ad; \
yr[k] = rd; yg[k] = gd; yb[k] = bd; \
} \
\
intpol( Dy, w, ndim ) \
ad = 0.0; \
rd = gd = bd = 0.0; \
\
for(i=0; i<ndim; i++) \
{ \
weight = w[ i ]; \
ad += ya[i] * weight; \
rd += yr[i] * weight; \
gd += yg[i] * weight; \
bd += yb[i] * weight; \
} \
\
if(!alpha_ok) \
{ \
if(ad>0.5) \
{ /* Renormalize */ \
weight = 1.0/ad; \
rd *= weight; \
gd *= weight; \
bd *= weight; \
alpha_ok = TRUE; \
} \
else \
{ \
rd=gd=bd=0.0; \
} \
} \
\
tdst = (unsigned psize *)dst; \
if(SamplesPerPixel==4) \
{ \
if(alpha_ok) \
*tdst++ = maxalpha; \
else \
*tdst++ = 0; \
} \
*tdst++ = gamma_##psize( rd ); \
*tdst++ = gamma_##psize( gd ); \
*tdst = gamma_##psize( bd ); \
} \
else if (color < 4) \
{ \
color-=1; \
for(k=0; k<ndim; k++) \
{ \
r = ((unsigned psize**)rgb)[k] + SamplesPerPixel - 3 + color; \
yr[k] = 0.0; \
\
for(i=0; i<ndim; i++) \
{ \
yr[k] += degamma_##psize((int)r[i*SamplesPerPixel]) * w[i]; \
} \
} \
\
intpol( Dy, w, ndim ) \
rd = 0.0; \
\
for(i=0; i<ndim; i++) \
{ \
rd += yr[i] * w[ i ]; \
} \
tdst = (unsigned psize *)dst; \
if(SamplesPerPixel==4) \
*tdst++ = maxalpha; \
\
*(tdst+color) = gamma_##psize( rd ); \
} \
else \
{ \
for(k=0; k<ndim; k++) \
{ \
r = ((unsigned psize**)rgb)[k] + SamplesPerPixel - 3; \
rd = gd = bd = 0.0; \
\
for(i=0; i<ndim; i++) \
{ \
weight = w[ i ]; \
ri = r + i * SamplesPerPixel; \
rd += degamma_##psize((int)*ri++) * weight; \
gd += degamma_##psize((int)*ri++) * weight; \
bd += degamma_##psize((int)*ri) * weight; \
} \
yr[k] = rd; yg[k] = gd; yb[k] = bd; \
} \
\
intpol( Dy, w, ndim ) \
rd = gd = bd = 0.0; \
\
for(i=0; i<ndim; i++) \
{ \
weight = w[ i ]; \
rd += yr[i] * weight; \
gd += yg[i] * weight; \
bd += yb[i] * weight; \
} \
\
tdst = (unsigned psize *)dst; \
if(SamplesPerPixel==4) \
*tdst++ = maxalpha; \
\
if (color==4) /* Red+Grn */ \
{ \
*tdst++ = gamma_##psize( rd ); \
*tdst = gamma_##psize( gd ); \
/* blue untouched */ \
} \
else \
if (color==5) /* Red+Blue */ \
{ \
*tdst++ = gamma_##psize( rd ); \
tdst++; /* green untouched */ \
*tdst = gamma_##psize( bd ); \
} \
else /* (color=6) Green+Blue */ \
{ \
tdst++; /* red untouched */ \
*tdst++ = gamma_##psize( gd ); \
*tdst = gamma_##psize( bd ); \
} \
\
} \
;
static double sinc( double x )
{
x *= PI;
if(x != 0.0)
return(sin(x) / x);
return(1.0);
}
// Cubic polynomial with parameter A
// A = -1: sharpen; A = - 0.5 homogeneous
// make sure x >= 0
#define A (-0.75)
// 0 <= x < 1
static double cubic01( double x )
{
return (( A + 2.0 )*x - ( A + 3.0 ))*x*x +1.0;
}
// 1 <= x < 2
static double cubic12( double x )
{
return (( A * x - 5.0 * A ) * x + 8.0 * A ) * x - 4.0 * A;
}
#undef A
// ---------- Sampling functions ----------------------------------
#define maxalpha 255
#define threshold (maxalpha / 16)
// Nearest neighbor sampling, nowhere used (yet)
static void nn( unsigned char *dst, unsigned char **rgb,
register double Dx PT_UNUSED, register double Dy PT_UNUSED,
int color, int SamplesPerPixel)
{
RESAMPLE_N( NNEIGHBOR, 1, char) }
// Bilinear sampling, nowhere used (yet).
static void bil( unsigned char *dst, unsigned char **rgb,
register double Dx, register double Dy, int color, int SamplesPerPixel)
{ RESAMPLE_N( BILINEAR, 2, char) }
// Lowest quality sampler in distribution; since version 1.8b1 changed to closely
// resemble Photoshop's bicubic interpolation
static void poly3( unsigned char *dst, unsigned char **rgb,
register double Dx, register double Dy, int color, int SamplesPerPixel)
{ RESAMPLE_N( CUBIC, 4, char) }
// Spline using 16 pixels; smoother and less artefacts than poly3, softer; same speed
static void spline16( unsigned char *dst, unsigned char **rgb,
register double Dx, register double Dy, int color, int SamplesPerPixel)
{ RESAMPLE_N( SPLINE16, 4, char) }
// Spline using 36 pixels; significantly sharper than both poly3 and spline16,
// almost no artefacts
static void spline36( unsigned char *dst, unsigned char **rgb,
register double Dx, register double Dy, int color, int SamplesPerPixel)
{ RESAMPLE_N( SPLINE36, 6, char) }
// Not used anymore
static void spline64( unsigned char *dst, unsigned char **rgb,
register double Dx, register double Dy, int color, int SamplesPerPixel)
{ RESAMPLE_N( SPLINE64, 8, char) }
// Highest quality sampler since version 1.8b1
// Extremely slow, but defintely worth every second.
static void sinc256( unsigned char *dst, unsigned char **rgb,
register double Dx, register double Dy, int color, int SamplesPerPixel)
{ RESAMPLE_N( SINC, 16, char) }
// Highest quality sampler since version 1.8b1
// Extremely slow, but defintely worth every second.
static void sinc1024( unsigned char *dst, unsigned char **rgb,
register double Dx, register double Dy, int color, int SamplesPerPixel)
{ RESAMPLE_N( SINC, 32, char) }
//--------------- Same as above, for shorts (16 bit channel size-------------------
#undef maxalpha
#define maxalpha 65535
// Nearest neighbor sampling, nowhere used (yet)
static void nn_16( unsigned char *dst, unsigned char **rgb,
register double Dx PT_UNUSED, register double Dy PT_UNUSED,
int color, int SamplesPerPixel)
{
RESAMPLE_N( NNEIGHBOR, 1, short) }
// Bilinear sampling, nowhere used (yet).
static void bil_16( unsigned char *dst, unsigned char **rgb,
register double Dx, register double Dy, int color, int SamplesPerPixel)
{ RESAMPLE_N( BILINEAR, 2, short) }
// Lowest quality sampler in distribution; since version 1.8b1 changed to closely
// resemble Photoshop's bicubic interpolation
static void poly3_16( unsigned char *dst, unsigned char **rgb,
register double Dx, register double Dy, int color, int SamplesPerPixel)
{ RESAMPLE_N( CUBIC, 4, short) }
// Spline using 16 pixels; smoother and less artefacts than poly3, softer; same speed
static void spline16_16( unsigned char *dst, unsigned char **rgb,
register double Dx, register double Dy, int color, int SamplesPerPixel)
{ RESAMPLE_N( SPLINE16, 4, short) }
// Spline using 36 pixels; significantly sharper than both poly3 and spline16,
// almost no artefacts
static void spline36_16( unsigned char *dst, unsigned char **rgb,
register double Dx, register double Dy, int color, int SamplesPerPixel)
{ RESAMPLE_N( SPLINE36, 6, short) }
// Not used anymore
static void spline64_16( unsigned char *dst, unsigned char **rgb,
register double Dx, register double Dy, int color, int SamplesPerPixel)
{ RESAMPLE_N( SPLINE64, 8, short) }
// Highest quality sampler since version 1.8b1
// Extremely slow, but defintely worth every second.
static void sinc256_16( unsigned char *dst, unsigned char **rgb,
register double Dx, register double Dy, int color, int SamplesPerPixel)
{ RESAMPLE_N( SINC, 16, short) }
// Highest quality sampler since version 1.8b1
// Extremely slow, but defintely worth every second.
static void sinc1024_16( unsigned char *dst, unsigned char **rgb,
register double Dx, register double Dy, int color, int SamplesPerPixel)
{ RESAMPLE_N( SINC, 32, short) }
//--------------- Same as above, for float -------------------
/*
A note about the use of undef signed below.
When RESAMPLE_N uses its third parameter sometimes it prefixes
it with unsigned. But floats can be unsigned. We previously
hacked it by removing "unsigned" via the preprocessor. We
originally wrapped all the functiosn that used RESAMPLE_N with
float with a single undef unsigned. Unfortunately this also
wrapped other (correct) uses of unsigned. What I did in this
change is wrap only the invocation to RESAMPLE_N. This should
improve readability and maintanability.
Another alternative (suggested by Walter Harms) is to do something
like this:
#define MACRO( psize , sign ) \
sign psize *r; \
call##psize ( r );
MACRO(float, )
MACRO(int, unsigned )
This in fact works, but it requires far more changed lines than
the current fix. Furthermore RESAMPLE_N is one of those macros
that nobody (ok, I) wants to debug if something goes wrong.
*/
#undef maxalpha
#define maxalpha 1.0
// Nearest neighbor sampling, nowhere used (yet)
static void nn_32( unsigned char *dst, unsigned char **rgb,
register double Dx PT_UNUSED, register double Dy PT_UNUSED,
int color, int SamplesPerPixel)
{
#define unsigned
RESAMPLE_N( NNEIGHBOR, 1, float);
#undef unsigned
}
// Bilinear sampling, nowhere used (yet).
static void bil_32( unsigned char *dst, unsigned char **rgb,
register double Dx, register double Dy, int color, int SamplesPerPixel)
{
#define unsigned
RESAMPLE_N( BILINEAR, 2, float);
#undef unsigned
}
// Lowest quality sampler in distribution; since version 1.8b1 changed to closely
// resemble Photoshop's bicubic interpolation
static void poly3_32( unsigned char *dst, unsigned char **rgb,
register double Dx, register double Dy, int color, int SamplesPerPixel)
{
#define unsigned
RESAMPLE_N( CUBIC, 4, float);
#undef unsigned
}
// Spline using 16 pixels; smoother and less artefacts than poly3, softer; same speed
static void spline16_32( unsigned char *dst, unsigned char **rgb,
register double Dx, register double Dy, int color, int SamplesPerPixel)
{
#define unsigned
RESAMPLE_N( SPLINE16, 4, float) ;
#undef unsigned
}
// Spline using 36 pixels; significantly sharper than both poly3 and spline16,
// almost no artefacts
static void spline36_32( unsigned char *dst, unsigned char **rgb,
register double Dx, register double Dy, int color, int SamplesPerPixel)
{
#define unsigned
RESAMPLE_N( SPLINE36, 6, float) ;
#undef unsigned
}
// Not used anymore
static void spline64_32( unsigned char *dst, unsigned char **rgb,
register double Dx, register double Dy, int color, int SamplesPerPixel)
{
#define unsigned
RESAMPLE_N( SPLINE64, 8, float) ;
#undef unsigned
}
// Highest quality sampler since version 1.8b1
// Extremely slow, but defintely worth every second.
static void sinc256_32( unsigned char *dst, unsigned char **rgb,
register double Dx, register double Dy, int color, int SamplesPerPixel)
{
#define unsigned
RESAMPLE_N( SINC, 16, float) ;
#undef unsigned
}
// Highest quality sampler since version 1.8b1
// Extremely slow, but defintely worth every second.
static void sinc1024_32( unsigned char *dst, unsigned char **rgb,
register double Dx, register double Dy, int color, int SamplesPerPixel)
{
#define unsigned
RESAMPLE_N( SINC, 32, float) ;
#undef unsigned
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// FS+ start of functions used to compute the pixel tranform from dest to source using linear interpolation
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// computes the source coordinates of a single pixel at position x using the math transforms
void ComputePixelCoords( double *ax, double *ay, int *trinum, char *avalid, uint32_t x, long offset, double w2, double y_d,
fDesc *fD, double sw2, double sh2, double min_x, double max_x, double min_y, double max_y ) {
double x_d, Dx, Dy;
int tvalid;
// Convert destination screen coordinates to cartesian coordinates.
// Offset is the distance between the left edge of the ROI and the left edge of the full output canvas (always less than or equal to 0)
x_d = (double) (x - offset) - w2;
// Get source cartesian coordinates
tvalid = fD->func( x_d, y_d , &Dx, &Dy, fD->param);
// Convert source cartesian coordinates to screen coordinates
Dx += sw2;
Dy = sh2 + Dy;
// stores the computed pixel
ax[x] = Dx;
ay[x] = Dy;
trinum[x] = getLastCurTriangle();
// Is the pixel valid, i.e. from within source image?
if( (Dx >= max_x) || (Dy >= max_y) || (Dx < min_x) || (Dy < min_y) || (tvalid==0))
avalid[x] = FALSE;
else
avalid[x] = TRUE;
}
// fills a part of the arrays with the coordinates in the source image for every pixel
// xl is the left border of the array, xr is the right border. The array values have already been
// computed in xl and xr.
void ComputePartialRowCoords( double *ax, double *ay, int *trinum, char *avalid, uint32_t xl, uint32_t xr, long offset, double w2, double y_d,
fDesc *fD, double sw2, double sh2, double min_x, double max_x, double min_y, double max_y ) {
uint32_t xm, idx;
double srcX_lin, srcY_lin;
double deltaX, deltaY, tmpX, tmpY;
////////////////////////////////////////////
// maximum estimated error to be accepted: higher values produce a faster execution but a more distorted image
// the real maximum error seems to be much lower, about 1/4 of MAX_ERR
double MAX_ERR = 1;
if( xl >= (xr - 1) ) return;
if( !avalid[xl] && !avalid[xr] ) {
// first and last pixel are not valid, assume that others are not valid too
// ax[] and ay[] values are not set since thay will not be used
for( idx = xl + 1; idx < xr; idx++ ) {
avalid[idx] = FALSE;
}
return;
}
// computes the source coords of the middle point of [xl, xr] using the transformation
xm = (xl + xr)/2;
ComputePixelCoords( ax, ay, trinum, avalid, xm, offset, w2, y_d, fD, sw2, sh2, min_x, max_x, min_y, max_y );
// computes the coords of the same point with linear interpolation
srcX_lin = ax[xl] + ((ax[xr] - ax[xl])/(xr - xl))*(xm - xl);
srcY_lin = ay[xl] + ((ay[xr] - ay[xl])/(xr - xl))*(xm - xl);
if( fabs(srcX_lin - ax[xm]) > MAX_ERR || fabs(srcY_lin - ay[xm]) > MAX_ERR ||
trinum[xl] != trinum[xr] || trinum[xl] != trinum[xm]) {
// the error is still too large or the points are in different morph triangles: recursion
ComputePartialRowCoords( ax, ay, trinum, avalid, xl, xm, offset, w2, y_d, fD, sw2, sh2, min_x, max_x, min_y, max_y );
ComputePartialRowCoords( ax, ay, trinum, avalid, xm, xr, offset, w2, y_d, fD, sw2, sh2, min_x, max_x, min_y, max_y );
return;
}
// fills the array, first the left half...
if( !avalid[xl] || !avalid[xm] ) {
// one end is valid and the other is not: computes every pixel with math transform
for( idx = xl + 1; idx < xm; idx++ ) {
ComputePixelCoords( ax, ay, trinum, avalid, idx, offset, w2, y_d, fD, sw2, sh2, min_x, max_x, min_y, max_y );
}
}
else {
// linear interpolation
deltaX = (ax[xm] - ax[xl]) / (xm - xl);
deltaY = (ay[xm] - ay[xl]) / (xm - xl);
tmpX = ax[xl];
tmpY = ay[xl];
for( idx = xl + 1; idx < xm; idx++ ) {
tmpX += deltaX;
tmpY += deltaY;
ax[idx] = tmpX;
ay[idx] = tmpY;
if( (tmpX >= max_x) || (tmpY >= max_y) || (tmpX < min_x) || (tmpY < min_y) )
avalid[idx] = FALSE;
else
avalid[idx] = TRUE;
trinum[idx] = trinum[xl];
}
}
// ...then the right half
if( !avalid[xm] || !avalid[xr] ) {
// one end is valid and the other is not: computes every pixel with math transform
for( idx = xm + 1; idx < xr; idx++ ) {
ComputePixelCoords( ax, ay, trinum, avalid, idx, offset, w2, y_d, fD, sw2, sh2, min_x, max_x, min_y, max_y );
}
}
else {
// linear interpolation
deltaX = (ax[xr] - ax[xm]) / (xr - xm);
deltaY = (ay[xr] - ay[xm]) / (xr - xm);
tmpX = ax[xm];
tmpY = ay[xm];
for( idx = xm + 1; idx < xr; idx++ ) {
tmpX += deltaX;
tmpY += deltaY;
ax[idx] = tmpX;
ay[idx] = tmpY;
if( (tmpX >= max_x) || (tmpY >= max_y) || (tmpX < min_x) || (tmpY < min_y) )
avalid[idx] = FALSE;
else
avalid[idx] = TRUE;
trinum[idx] = trinum[xr];
}
}
}
// fills the arrays with the source coords computed using linear interpolation
// asize is the number of elements of the arrays
// the array elements lie in the interval [0, asize], the image elements in [destRect.left, destRect.right]: the offset parameter
// is used for the conversion
void ComputeRowCoords( double *ax, double *ay, int *trinum, char *avalid, int32_t asize, long offset, double w2, double y_d,
fDesc *fD, double sw2, double sh2, double min_x, double max_x, double min_y, double max_y, int STEP_WIDTH) {
// STEP_WIDTH is initial distance betwen correctly computed points. The distance will be reduced if needed.
uint32_t x;
x = 0;
ComputePixelCoords( ax, ay, trinum, avalid, x, offset, w2, y_d, fD, sw2, sh2, min_x, max_x, min_y, max_y );
x += STEP_WIDTH;
while( x < asize ) {
ComputePixelCoords( ax, ay, trinum, avalid, x, offset, w2, y_d, fD, sw2, sh2, min_x, max_x, min_y, max_y );
ComputePartialRowCoords( ax, ay, trinum, avalid, x - STEP_WIDTH, x, offset, w2, y_d, fD, sw2, sh2, min_x, max_x, min_y, max_y );
x += STEP_WIDTH;
}
// compute the last pixels, if any
x -= STEP_WIDTH;
if( x < asize - 1 ) {
ComputePixelCoords( ax, ay, trinum, avalid, asize - 1, offset, w2, y_d, fD, sw2, sh2, min_x, max_x, min_y, max_y );
ComputePartialRowCoords( ax, ay, trinum, avalid, x, asize - 1, offset, w2, y_d, fD, sw2, sh2, min_x, max_x, min_y, max_y );
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// FS- end of functions used to compute the pixel transform from dest to source using linear interpolation
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Main transformation function. Destination image is calculated using transformation
// Function "func". Either all colors (color = 0) or one of rgb (color =1,2,3) are
// determined. If successful, TrPtr->success = 1. Memory for destination image
// must have been allocated and locked!
//
// MODIFICATIONS:
// June 2004 - R.Platt - moved body of transForm into MyTransForm and MyTransformBody to eliminate code duplication.
// - This was also needed for multithreading.
void transForm( TrformStr *TrPtr, fDesc *fD, int color)
{
int imageNum = 1;
MyTransForm( TrPtr, fD, color, imageNum );
}
void transFormEx( TrformStr *TrPtr, fDesc *fD, fDesc *finvD, int color, int imageNum )
{
if (TrPtr->interpolator<_aabox)
{
MyTransForm(TrPtr, fD, color, imageNum);
}
else
{
transForm_aa(TrPtr, fD, finvD, color, imageNum);
}
}
/*This function was added by Kekus Digital on 18/9/2002.
This function takes the parameter 'imageNum' which repesents the
index of the image that has to be converted.*/
void MyTransForm( TrformStr *TrPtr, fDesc *fD, int color, int imageNum)
{