43#include "magick/studio.h"
44#include "magick/artifact.h"
45#include "magick/cache.h"
46#include "magick/cache-view.h"
47#include "magick/channel.h"
48#include "magick/color-private.h"
49#include "magick/colorspace.h"
50#include "magick/colorspace-private.h"
51#include "magick/composite-private.h"
52#include "magick/distort.h"
53#include "magick/exception.h"
54#include "magick/exception-private.h"
55#include "magick/gem.h"
56#include "magick/hashmap.h"
57#include "magick/image.h"
58#include "magick/list.h"
59#include "magick/matrix.h"
60#include "magick/memory_.h"
61#include "magick/monitor-private.h"
62#include "magick/option.h"
63#include "magick/pixel.h"
64#include "magick/pixel-accessor.h"
65#include "magick/pixel-private.h"
66#include "magick/resample.h"
67#include "magick/resample-private.h"
68#include "magick/registry.h"
69#include "magick/resource_.h"
70#include "magick/semaphore.h"
71#include "magick/shear.h"
72#include "magick/string_.h"
73#include "magick/string-private.h"
74#include "magick/thread-private.h"
75#include "magick/token.h"
76#include "magick/transform.h"
81static inline void AffineArgsToCoefficients(
double *affine)
85 tmp[0]=affine[1]; tmp[1]=affine[2]; tmp[2]=affine[3]; tmp[3]=affine[4];
86 affine[3]=tmp[0]; affine[1]=tmp[1]; affine[4]=tmp[2]; affine[2]=tmp[3];
89static inline void CoefficientsToAffineArgs(
double *coeff)
93 tmp[0]=coeff[3]; tmp[1]=coeff[1]; tmp[2]=coeff[4]; tmp[3]=coeff[2];
94 coeff[1]=tmp[0]; coeff[2]=tmp[1]; coeff[3]=tmp[2]; coeff[4]=tmp[3];
96static void InvertAffineCoefficients(
const double *coeff,
double *inverse)
101 determinant=MagickSafeReciprocal(coeff[0]*coeff[4]-coeff[1]*coeff[3]);
102 inverse[0]=determinant*coeff[4];
103 inverse[1]=determinant*(-coeff[1]);
104 inverse[2]=determinant*(coeff[1]*coeff[5]-coeff[2]*coeff[4]);
105 inverse[3]=determinant*(-coeff[3]);
106 inverse[4]=determinant*coeff[0];
107 inverse[5]=determinant*(coeff[2]*coeff[3]-coeff[0]*coeff[5]);
110static void InvertPerspectiveCoefficients(
const double *coeff,
116 determinant=MagickSafeReciprocal(coeff[0]*coeff[4]-coeff[3]*coeff[1]);
117 inverse[0]=determinant*(coeff[4]-coeff[7]*coeff[5]);
118 inverse[1]=determinant*(coeff[7]*coeff[2]-coeff[1]);
119 inverse[2]=determinant*(coeff[1]*coeff[5]-coeff[4]*coeff[2]);
120 inverse[3]=determinant*(coeff[6]*coeff[5]-coeff[3]);
121 inverse[4]=determinant*(coeff[0]-coeff[6]*coeff[2]);
122 inverse[5]=determinant*(coeff[3]*coeff[2]-coeff[0]*coeff[5]);
123 inverse[6]=determinant*(coeff[3]*coeff[7]-coeff[6]*coeff[4]);
124 inverse[7]=determinant*(coeff[6]*coeff[1]-coeff[0]*coeff[7]);
146static size_t poly_number_terms(
double order)
149 if ( order < 1 || order > 5 ||
150 ( order != floor(order) && (order-1.5) > MagickEpsilon) )
152 return(CastDoubleToSizeT(floor((order+1.0)*(order+2.0)/2.0)));
155static double poly_basis_fn(ssize_t n,
double x,
double y)
159 case 0:
return( 1.0 );
162 case 3:
return( x*y );
163 case 4:
return( x*x );
164 case 5:
return( y*y );
165 case 6:
return( x*x*x );
166 case 7:
return( x*x*y );
167 case 8:
return( x*y*y );
168 case 9:
return( y*y*y );
169 case 10:
return( x*x*x*x );
170 case 11:
return( x*x*x*y );
171 case 12:
return( x*x*y*y );
172 case 13:
return( x*y*y*y );
173 case 14:
return( y*y*y*y );
174 case 15:
return( x*x*x*x*x );
175 case 16:
return( x*x*x*x*y );
176 case 17:
return( x*x*x*y*y );
177 case 18:
return( x*x*y*y*y );
178 case 19:
return( x*y*y*y*y );
179 case 20:
return( y*y*y*y*y );
183static const char *poly_basis_str(ssize_t n)
188 case 1:
return(
"*ii");
189 case 2:
return(
"*jj");
190 case 3:
return(
"*ii*jj");
191 case 4:
return(
"*ii*ii");
192 case 5:
return(
"*jj*jj");
193 case 6:
return(
"*ii*ii*ii");
194 case 7:
return(
"*ii*ii*jj");
195 case 8:
return(
"*ii*jj*jj");
196 case 9:
return(
"*jj*jj*jj");
197 case 10:
return(
"*ii*ii*ii*ii");
198 case 11:
return(
"*ii*ii*ii*jj");
199 case 12:
return(
"*ii*ii*jj*jj");
200 case 13:
return(
"*ii*jj*jj*jj");
201 case 14:
return(
"*jj*jj*jj*jj");
202 case 15:
return(
"*ii*ii*ii*ii*ii");
203 case 16:
return(
"*ii*ii*ii*ii*jj");
204 case 17:
return(
"*ii*ii*ii*jj*jj");
205 case 18:
return(
"*ii*ii*jj*jj*jj");
206 case 19:
return(
"*ii*jj*jj*jj*jj");
207 case 20:
return(
"*jj*jj*jj*jj*jj");
211static double poly_basis_dx(ssize_t n,
double x,
double y)
215 case 0:
return( 0.0 );
216 case 1:
return( 1.0 );
217 case 2:
return( 0.0 );
220 case 5:
return( 0.0 );
221 case 6:
return( x*x );
222 case 7:
return( x*y );
223 case 8:
return( y*y );
224 case 9:
return( 0.0 );
225 case 10:
return( x*x*x );
226 case 11:
return( x*x*y );
227 case 12:
return( x*y*y );
228 case 13:
return( y*y*y );
229 case 14:
return( 0.0 );
230 case 15:
return( x*x*x*x );
231 case 16:
return( x*x*x*y );
232 case 17:
return( x*x*y*y );
233 case 18:
return( x*y*y*y );
234 case 19:
return( y*y*y*y );
235 case 20:
return( 0.0 );
239static double poly_basis_dy(ssize_t n,
double x,
double y)
243 case 0:
return( 0.0 );
244 case 1:
return( 0.0 );
245 case 2:
return( 1.0 );
247 case 4:
return( 0.0 );
249 default:
return( poly_basis_dx(n-1,x,y) );
285MagickExport Image *AffineTransformImage(
const Image *image,
286 const AffineMatrix *affine_matrix,ExceptionInfo *exception)
297 assert(image->signature == MagickCoreSignature);
298 assert(affine_matrix != (AffineMatrix *) NULL);
299 assert(exception != (ExceptionInfo *) NULL);
300 assert(exception->signature == MagickCoreSignature);
301 if (IsEventLogging() != MagickFalse)
302 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
303 distort[0]=affine_matrix->sx;
304 distort[1]=affine_matrix->rx;
305 distort[2]=affine_matrix->ry;
306 distort[3]=affine_matrix->sy;
307 distort[4]=affine_matrix->tx;
308 distort[5]=affine_matrix->ty;
309 deskew_image=DistortImage(image,AffineProjectionDistortion,6,distort,
310 MagickTrue,exception);
311 return(deskew_image);
364static inline double MagickRound(
double x)
369 if ((x-floor(x)) < (ceil(x)-x))
374static double *GenerateCoefficients(
const Image *image,
375 DistortImageMethod *method,
const size_t number_arguments,
376 const double *arguments,
size_t number_values,ExceptionInfo *exception)
391 if ( number_values == 0 ) {
407 cp_size = number_values+2;
412 if ( number_arguments < 4*cp_size &&
413 ( *method == BilinearForwardDistortion
414 || *method == BilinearReverseDistortion
415 || *method == PerspectiveDistortion
417 *method = AffineDistortion;
421 case AffineDistortion:
423 number_coeff=3*number_values;
425 case PolynomialDistortion:
427 i = poly_number_terms(arguments[0]);
428 number_coeff = 2 + i*number_values;
430 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
431 "InvalidArgument",
"%s : '%s'",
"Polynomial",
432 "Invalid order, should be integer 1 to 5, or 1.5");
433 return((
double *) NULL);
435 if ((number_arguments < (1+i*cp_size)) ||
436 (((number_arguments-1) % cp_size) != 0)) {
437 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
438 "InvalidArgument",
"%s : 'require at least %.20g CPs'",
439 "Polynomial", (
double) i);
440 return((
double *) NULL);
443 case BilinearReverseDistortion:
444 number_coeff=4*number_values;
449 case BilinearForwardDistortion:
456 case QuadraterialDistortion:
460 case ShepardsDistortion:
466 case ScaleRotateTranslateDistortion:
467 case AffineProjectionDistortion:
468 case Plane2CylinderDistortion:
469 case Cylinder2PlaneDistortion:
472 case PolarDistortion:
473 case DePolarDistortion:
476 case PerspectiveDistortion:
477 case PerspectiveProjectionDistortion:
480 case BarrelDistortion:
481 case BarrelInverseDistortion:
485 perror(
"unknown method given");
489 coeff = (
double *) AcquireQuantumMemory(number_coeff,
sizeof(*coeff));
490 if (coeff == (
double *) NULL) {
491 (void) ThrowMagickException(exception,GetMagickModule(),
492 ResourceLimitError,
"MemoryAllocationFailed",
493 "%s",
"GenerateCoefficients");
494 return((
double *) NULL);
498 for (i=0; i < number_coeff; i++)
503 case AffineDistortion:
513 if ( number_arguments%cp_size != 0 ||
514 number_arguments < cp_size ) {
515 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
516 "InvalidArgument",
"%s : 'require at least %.20g CPs'",
518 coeff=(
double *) RelinquishMagickMemory(coeff);
519 return((
double *) NULL);
522 if ( number_arguments == cp_size ) {
524 if ( cp_values == 0 ) {
527 coeff[2] = arguments[0] - arguments[2];
529 coeff[5] = arguments[1] - arguments[3];
533 for (i=0; i<number_values; i++)
534 coeff[i*3+2] = arguments[cp_values+i];
550 matrix = AcquireMagickMatrix(3UL,3UL);
551 vectors = (
double **) AcquireQuantumMemory(number_values,
sizeof(*vectors));
552 if (matrix == (
double **) NULL || vectors == (
double **) NULL)
554 matrix = RelinquishMagickMatrix(matrix, 3UL);
555 vectors = (
double **) RelinquishMagickMemory(vectors);
556 coeff = (
double *) RelinquishMagickMemory(coeff);
557 (void) ThrowMagickException(exception,GetMagickModule(),
558 ResourceLimitError,
"MemoryAllocationFailed",
559 "%s",
"DistortCoefficients");
560 return((
double *) NULL);
563 for (i=0; i < number_values; i++)
564 vectors[i] = &(coeff[i*3]);
566 for (i=0; i < number_arguments; i+=cp_size) {
567 terms[0] = arguments[i+cp_x];
568 terms[1] = arguments[i+cp_y];
570 LeastSquaresAddTerms(matrix,vectors,terms,
571 &(arguments[i+cp_values]),3UL,number_values);
573 if ( number_arguments == 2*cp_size ) {
578 terms[0] = arguments[cp_x]
579 - ( arguments[cp_size+cp_y] - arguments[cp_y] );
580 terms[1] = arguments[cp_y] +
581 + ( arguments[cp_size+cp_x] - arguments[cp_x] );
583 if ( cp_values == 0 ) {
587 uv2[0] = arguments[0] - arguments[5] + arguments[1];
588 uv2[1] = arguments[1] + arguments[4] - arguments[0];
589 LeastSquaresAddTerms(matrix,vectors,terms,uv2,3UL,2UL);
593 LeastSquaresAddTerms(matrix,vectors,terms,
594 &(arguments[cp_values]),3UL,number_values);
598 status=GaussJordanElimination(matrix,vectors,3UL,number_values);
599 matrix = RelinquishMagickMatrix(matrix, 3UL);
600 vectors = (
double **) RelinquishMagickMemory(vectors);
601 if ( status == MagickFalse ) {
602 coeff = (
double *) RelinquishMagickMemory(coeff);
603 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
604 "InvalidArgument",
"%s : 'Unsolvable Matrix'",
605 CommandOptionToMnemonic(MagickDistortOptions, *method) );
606 return((
double *) NULL);
611 case AffineProjectionDistortion:
627 if (number_arguments != 6) {
628 coeff = (
double *) RelinquishMagickMemory(coeff);
629 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
630 "InvalidArgument",
"%s : 'Needs 6 coeff values'",
631 CommandOptionToMnemonic(MagickDistortOptions, *method) );
632 return((
double *) NULL);
635 for(i=0; i<6UL; i++ )
636 inverse[i] = arguments[i];
637 AffineArgsToCoefficients(inverse);
638 InvertAffineCoefficients(inverse, coeff);
639 *method = AffineDistortion;
643 case ScaleRotateTranslateDistortion:
672 x = nx = (double)(image->columns)/2.0 + (double)image->page.x;
673 y = ny = (double)(image->rows)/2.0 + (double)image->page.y;
675 switch ( number_arguments ) {
677 coeff = (
double *) RelinquishMagickMemory(coeff);
678 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
679 "InvalidArgument",
"%s : 'Needs at least 1 argument'",
680 CommandOptionToMnemonic(MagickDistortOptions, *method) );
681 return((
double *) NULL);
686 sx = sy = arguments[0];
690 x = nx = arguments[0];
691 y = ny = arguments[1];
692 switch ( number_arguments ) {
697 sx = sy = arguments[2];
706 sx = sy = arguments[2];
719 coeff = (
double *) RelinquishMagickMemory(coeff);
720 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
721 "InvalidArgument",
"%s : 'Too Many Arguments (7 or less)'",
722 CommandOptionToMnemonic(MagickDistortOptions, *method) );
723 return((
double *) NULL);
728 if ( fabs(sx) < MagickEpsilon || fabs(sy) < MagickEpsilon ) {
729 coeff = (
double *) RelinquishMagickMemory(coeff);
730 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
731 "InvalidArgument",
"%s : 'Zero Scale Given'",
732 CommandOptionToMnemonic(MagickDistortOptions, *method) );
733 return((
double *) NULL);
736 a=DegreesToRadians(a); cosine=cos(a); sine=sin(a);
738 *method = AffineDistortion;
741 coeff[2]=x-nx*coeff[0]-ny*coeff[1];
744 coeff[5]=y-nx*coeff[3]-ny*coeff[4];
747 case PerspectiveDistortion:
790 if ( number_arguments%cp_size != 0 ||
791 number_arguments < cp_size*4 ) {
792 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
793 "InvalidArgument",
"%s : 'require at least %.20g CPs'",
794 CommandOptionToMnemonic(MagickDistortOptions, *method), 4.0);
795 coeff=(
double *) RelinquishMagickMemory(coeff);
796 return((
double *) NULL);
799 vectors[0] = &(coeff[0]);
801 matrix = AcquireMagickMatrix(8UL,8UL);
802 if (matrix == (
double **) NULL) {
803 coeff=(
double *) RelinquishMagickMemory(coeff);
804 (void) ThrowMagickException(exception,GetMagickModule(),
805 ResourceLimitError,
"MemoryAllocationFailed",
806 "%s",
"DistortCoefficients");
807 return((
double *) NULL);
810 for (i=0; i < number_arguments; i+=4) {
811 terms[0]=arguments[i+cp_x];
812 terms[1]=arguments[i+cp_y];
817 terms[6]=-terms[0]*arguments[i+cp_u];
818 terms[7]=-terms[1]*arguments[i+cp_u];
819 LeastSquaresAddTerms(matrix,vectors,terms,&(arguments[i+cp_u]),
825 terms[3]=arguments[i+cp_x];
826 terms[4]=arguments[i+cp_y];
828 terms[6]=-terms[3]*arguments[i+cp_v];
829 terms[7]=-terms[4]*arguments[i+cp_v];
830 LeastSquaresAddTerms(matrix,vectors,terms,&(arguments[i+cp_v]),
834 status=GaussJordanElimination(matrix,vectors,8UL,1UL);
835 matrix = RelinquishMagickMatrix(matrix, 8UL);
836 if ( status == MagickFalse ) {
837 coeff = (
double *) RelinquishMagickMemory(coeff);
838 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
839 "InvalidArgument",
"%s : 'Unsolvable Matrix'",
840 CommandOptionToMnemonic(MagickDistortOptions, *method) );
841 return((
double *) NULL);
849 coeff[8] = coeff[6]*arguments[cp_x]
850 + coeff[7]*arguments[cp_y] + 1.0;
851 coeff[8] = (coeff[8] < 0.0) ? -1.0 : +1.0;
855 case PerspectiveProjectionDistortion:
860 if (number_arguments != 8) {
861 coeff = (
double *) RelinquishMagickMemory(coeff);
862 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
863 "InvalidArgument",
"%s : 'Needs 8 coefficient values'",
864 CommandOptionToMnemonic(MagickDistortOptions, *method));
865 return((
double *) NULL);
868 InvertPerspectiveCoefficients(arguments, coeff);
876 coeff[8] = coeff[6]*arguments[2]
877 + coeff[7]*arguments[5] + 1.0;
878 coeff[8] = (coeff[8] < 0.0) ? -1.0 : +1.0;
879 *method = PerspectiveDistortion;
883 case BilinearForwardDistortion:
884 case BilinearReverseDistortion:
908 if ( number_arguments%cp_size != 0 ||
909 number_arguments < cp_size*4 ) {
910 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
911 "InvalidArgument",
"%s : 'require at least %.20g CPs'",
912 CommandOptionToMnemonic(MagickDistortOptions, *method), 4.0);
913 coeff=(
double *) RelinquishMagickMemory(coeff);
914 return((
double *) NULL);
917 matrix = AcquireMagickMatrix(4UL,4UL);
918 vectors = (
double **) AcquireQuantumMemory(number_values,
sizeof(*vectors));
919 if (matrix == (
double **) NULL || vectors == (
double **) NULL)
921 matrix = RelinquishMagickMatrix(matrix, 4UL);
922 vectors = (
double **) RelinquishMagickMemory(vectors);
923 coeff = (
double *) RelinquishMagickMemory(coeff);
924 (void) ThrowMagickException(exception,GetMagickModule(),
925 ResourceLimitError,
"MemoryAllocationFailed",
926 "%s",
"DistortCoefficients");
927 return((
double *) NULL);
930 for (i=0; i < number_values; i++)
931 vectors[i] = &(coeff[i*4]);
933 for (i=0; i < number_arguments; i+=cp_size) {
934 terms[0] = arguments[i+cp_x];
935 terms[1] = arguments[i+cp_y];
936 terms[2] = terms[0]*terms[1];
938 LeastSquaresAddTerms(matrix,vectors,terms,
939 &(arguments[i+cp_values]),4UL,number_values);
942 status=GaussJordanElimination(matrix,vectors,4UL,number_values);
943 matrix = RelinquishMagickMatrix(matrix, 4UL);
944 vectors = (
double **) RelinquishMagickMemory(vectors);
945 if ( status == MagickFalse ) {
946 coeff = (
double *) RelinquishMagickMemory(coeff);
947 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
948 "InvalidArgument",
"%s : 'Unsolvable Matrix'",
949 CommandOptionToMnemonic(MagickDistortOptions, *method) );
950 return((
double *) NULL);
952 if ( *method == BilinearForwardDistortion ) {
992 coeff[8] = coeff[0]*coeff[5] - coeff[1]*coeff[4];
993 coeff[9] = 2*(coeff[2]*coeff[5] - coeff[1]*coeff[6]);
998 case QuadrilateralDistortion:
1014 case PolynomialDistortion:
1052 coeff[0] = arguments[0];
1053 coeff[1] = (double) poly_number_terms(arguments[0]);
1054 nterms = CastDoubleToSizeT(coeff[1]);
1057 matrix = AcquireMagickMatrix(nterms,nterms);
1058 vectors = (
double **) AcquireQuantumMemory(number_values,
sizeof(*vectors));
1059 terms = (
double *) AcquireQuantumMemory(nterms,
sizeof(*terms));
1060 if (matrix == (
double **) NULL ||
1061 vectors == (
double **) NULL ||
1062 terms == (
double *) NULL )
1064 matrix = RelinquishMagickMatrix(matrix, nterms);
1065 vectors = (
double **) RelinquishMagickMemory(vectors);
1066 terms = (
double *) RelinquishMagickMemory(terms);
1067 coeff = (
double *) RelinquishMagickMemory(coeff);
1068 (void) ThrowMagickException(exception,GetMagickModule(),
1069 ResourceLimitError,
"MemoryAllocationFailed",
1070 "%s",
"DistortCoefficients");
1071 return((
double *) NULL);
1074 for (i=0; i < number_values; i++)
1075 vectors[i] = &(coeff[2+i*nterms]);
1077 for (i=1; i < number_arguments; i+=cp_size) {
1078 for (j=0; j < (ssize_t) nterms; j++)
1079 terms[j] = poly_basis_fn(j,arguments[i+cp_x],arguments[i+cp_y]);
1080 LeastSquaresAddTerms(matrix,vectors,terms,
1081 &(arguments[i+cp_values]),nterms,number_values);
1083 terms = (
double *) RelinquishMagickMemory(terms);
1085 status=GaussJordanElimination(matrix,vectors,nterms,number_values);
1086 matrix = RelinquishMagickMatrix(matrix, nterms);
1087 vectors = (
double **) RelinquishMagickMemory(vectors);
1088 if ( status == MagickFalse ) {
1089 coeff = (
double *) RelinquishMagickMemory(coeff);
1090 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1091 "InvalidArgument",
"%s : 'Unsolvable Matrix'",
1092 CommandOptionToMnemonic(MagickDistortOptions, *method) );
1093 return((
double *) NULL);
1133 if ( number_arguments >= 1 && arguments[0] < MagickEpsilon ) {
1134 coeff = (
double *) RelinquishMagickMemory(coeff);
1135 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1136 "InvalidArgument",
"%s : 'Arc Angle Too Small'",
1137 CommandOptionToMnemonic(MagickDistortOptions, *method) );
1138 return((
double *) NULL);
1140 if ( number_arguments >= 3 && arguments[2] < MagickEpsilon ) {
1141 coeff = (
double *) RelinquishMagickMemory(coeff);
1142 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1143 "InvalidArgument",
"%s : 'Outer Radius Too Small'",
1144 CommandOptionToMnemonic(MagickDistortOptions, *method) );
1145 return((
double *) NULL);
1147 coeff[0] = -MagickPI2;
1148 if ( number_arguments >= 1 )
1149 coeff[1] = DegreesToRadians(arguments[0]);
1151 coeff[1] = MagickPI2;
1152 if ( number_arguments >= 2 )
1153 coeff[0] += DegreesToRadians(arguments[1]);
1154 coeff[0] /= Magick2PI;
1155 coeff[0] -= MagickRound(coeff[0]);
1156 coeff[0] *= Magick2PI;
1157 coeff[3] = (double)image->rows-1;
1158 coeff[2] = (double)image->columns/coeff[1] + coeff[3]/2.0;
1159 if ( number_arguments >= 3 ) {
1160 if ( number_arguments >= 4 )
1161 coeff[3] = arguments[2] - arguments[3];
1163 coeff[3] *= arguments[2]/coeff[2];
1164 coeff[2] = arguments[2];
1166 coeff[4] = ((double)image->columns-1.0)/2.0;
1170 case PolarDistortion:
1171 case DePolarDistortion:
1183 if ( number_arguments == 3
1184 || ( number_arguments > 6 && *method == PolarDistortion )
1185 || number_arguments > 8 ) {
1186 (void) ThrowMagickException(exception,GetMagickModule(),
1187 OptionError,
"InvalidArgument",
"%s : number of arguments",
1188 CommandOptionToMnemonic(MagickDistortOptions, *method) );
1189 coeff=(
double *) RelinquishMagickMemory(coeff);
1190 return((
double *) NULL);
1193 if ( number_arguments >= 1 )
1194 coeff[0] = arguments[0];
1198 coeff[1] = number_arguments >= 2 ? arguments[1] : 0.0;
1200 if ( number_arguments >= 4 ) {
1201 coeff[2] = arguments[2];
1202 coeff[3] = arguments[3];
1205 coeff[2] = (double)(image->columns)/2.0+image->page.x;
1206 coeff[3] = (double)(image->rows)/2.0+image->page.y;
1209 coeff[4] = -MagickPI;
1210 if ( number_arguments >= 5 )
1211 coeff[4] = DegreesToRadians(arguments[4]);
1212 coeff[5] = coeff[4];
1213 if ( number_arguments >= 6 )
1214 coeff[5] = DegreesToRadians(arguments[5]);
1215 if ( fabs(coeff[4]-coeff[5]) < MagickEpsilon )
1216 coeff[5] += Magick2PI;
1218 if ( coeff[0] < MagickEpsilon ) {
1220 if ( fabs(coeff[0]) < MagickEpsilon ) {
1221 coeff[0]=MagickMin(fabs(coeff[2]-image->page.x),
1222 fabs(coeff[3]-image->page.y));
1223 coeff[0]=MagickMin(coeff[0],
1224 fabs(coeff[2]-image->page.x-image->columns));
1225 coeff[0]=MagickMin(coeff[0],
1226 fabs(coeff[3]-image->page.y-image->rows));
1229 if ( fabs(-1.0-coeff[0]) < MagickEpsilon ) {
1231 rx = coeff[2]-image->page.x;
1232 ry = coeff[3]-image->page.y;
1233 coeff[0] = rx*rx+ry*ry;
1234 ry = coeff[3]-image->page.y-image->rows;
1235 coeff[0] = MagickMax(coeff[0],rx*rx+ry*ry);
1236 rx = coeff[2]-image->page.x-image->columns;
1237 coeff[0] = MagickMax(coeff[0],rx*rx+ry*ry);
1238 ry = coeff[3]-image->page.y;
1239 coeff[0] = MagickMax(coeff[0],rx*rx+ry*ry);
1240 coeff[0] = sqrt(coeff[0]);
1244 if ( coeff[0] < MagickEpsilon || coeff[1] < -MagickEpsilon
1245 || (coeff[0]-coeff[1]) < MagickEpsilon ) {
1246 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1247 "InvalidArgument",
"%s : Invalid Radius",
1248 CommandOptionToMnemonic(MagickDistortOptions, *method) );
1249 coeff=(
double *) RelinquishMagickMemory(coeff);
1250 return((
double *) NULL);
1253 if ( *method == PolarDistortion ) {
1254 coeff[6]=(double) image->columns/(coeff[5]-coeff[4]);
1255 coeff[7]=(double) image->rows/(coeff[0]-coeff[1]);
1258 coeff[6]=(coeff[5]-coeff[4])/image->columns;
1259 coeff[7]=(coeff[0]-coeff[1])/image->rows;
1263 case Cylinder2PlaneDistortion:
1264 case Plane2CylinderDistortion:
1288 if (number_arguments < 1) {
1289 coeff = (
double *) RelinquishMagickMemory(coeff);
1290 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1291 "InvalidArgument",
"%s : 'Needs at least 1 argument'",
1292 CommandOptionToMnemonic(MagickDistortOptions, *method) );
1293 return((
double *) NULL);
1295 if ( arguments[0] < MagickEpsilon || arguments[0] > 160.0 ) {
1296 coeff=(
double *) RelinquishMagickMemory(coeff);
1297 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1298 "InvalidArgument",
"%s : Invalid FOV Angle",
1299 CommandOptionToMnemonic(MagickDistortOptions, *method) );
1300 return((
double *) NULL);
1302 coeff[0] = DegreesToRadians(arguments[0]);
1303 if ( *method == Cylinder2PlaneDistortion )
1307 coeff[1] = (double) image->columns/coeff[0];
1310 coeff[1] = (double) image->columns / ( 2 * tan(coeff[0]/2) );
1312 coeff[2] = (double)(image->columns)/2.0+image->page.x;
1313 coeff[3] = (double)(image->rows)/2.0+image->page.y;
1314 coeff[4] = coeff[2];
1315 coeff[5] = coeff[3];
1318 case BarrelDistortion:
1319 case BarrelInverseDistortion:
1340 rscale = 2.0/MagickMin((
double) image->columns,(
double) image->rows);
1343 if ( (number_arguments < 3) || (number_arguments == 7) ||
1344 (number_arguments == 9) || (number_arguments > 10) )
1346 coeff=(
double *) RelinquishMagickMemory(coeff);
1347 (void) ThrowMagickException(exception,GetMagickModule(),
1348 OptionError,
"InvalidArgument",
"%s : number of arguments",
1349 CommandOptionToMnemonic(MagickDistortOptions, *method) );
1350 return((
double *) NULL);
1353 coeff[0] = arguments[0];
1354 coeff[1] = arguments[1];
1355 coeff[2] = arguments[2];
1356 if ((number_arguments == 3) || (number_arguments == 5) )
1357 coeff[3] = 1.0 - coeff[0] - coeff[1] - coeff[2];
1359 coeff[3] = arguments[3];
1361 coeff[0] *= pow(rscale,3.0);
1362 coeff[1] *= rscale*rscale;
1365 if ( number_arguments >= 8 ) {
1366 coeff[4] = arguments[4] * pow(rscale,3.0);
1367 coeff[5] = arguments[5] * rscale*rscale;
1368 coeff[6] = arguments[6] * rscale;
1369 coeff[7] = arguments[7];
1372 coeff[4] = coeff[0];
1373 coeff[5] = coeff[1];
1374 coeff[6] = coeff[2];
1375 coeff[7] = coeff[3];
1378 if ( number_arguments == 5 ) {
1379 coeff[8] = arguments[3];
1380 coeff[9] = arguments[4];
1382 else if ( number_arguments == 6 ) {
1383 coeff[8] = arguments[4];
1384 coeff[9] = arguments[5];
1386 else if ( number_arguments == 10 ) {
1387 coeff[8] = arguments[8];
1388 coeff[9] = arguments[9];
1392 coeff[8] = (double)image->columns/2.0 + image->page.x;
1393 coeff[9] = (double)image->rows/2.0 + image->page.y;
1397 case ShepardsDistortion:
1404 if ( number_arguments%cp_size != 0 ||
1405 number_arguments < cp_size ) {
1406 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1407 "InvalidArgument",
"%s : 'requires CP's (4 numbers each)'",
1408 CommandOptionToMnemonic(MagickDistortOptions, *method));
1409 coeff=(
double *) RelinquishMagickMemory(coeff);
1410 return((
double *) NULL);
1413 {
const char *artifact=GetImageArtifact(image,
"shepards:power");
1414 if ( artifact != (
const char *) NULL ) {
1415 coeff[0]=StringToDouble(artifact,(
char **) NULL) / 2.0;
1416 if ( coeff[0] < MagickEpsilon ) {
1417 (void) ThrowMagickException(exception,GetMagickModule(),
1418 OptionError,
"InvalidArgument",
"%s",
"-define shepards:power" );
1419 coeff=(
double *) RelinquishMagickMemory(coeff);
1420 return((
double *) NULL);
1432 perror(
"no method handler");
1433 return((
double *) NULL);
1471MagickExport Image *DistortResizeImage(
const Image *image,
1472 const size_t columns,
const size_t rows,ExceptionInfo *exception)
1474#define DistortResizeImageTag "Distort/Image"
1492 assert(image != (
const Image *) NULL);
1493 assert(image->signature == MagickCoreSignature);
1494 assert(exception != (ExceptionInfo *) NULL);
1495 assert(exception->signature == MagickCoreSignature);
1496 if (IsEventLogging() != MagickFalse)
1497 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1498 if ((columns == 0) || (rows == 0))
1499 return((Image *) NULL);
1502 (void) memset(distort_args,0,12*
sizeof(
double));
1503 distort_args[4]=(double) image->columns;
1504 distort_args[6]=(double) columns;
1505 distort_args[9]=(double) image->rows;
1506 distort_args[11]=(double) rows;
1508 vp_save=GetImageVirtualPixelMethod(image);
1510 tmp_image=CloneImage(image,0,0,MagickTrue,exception);
1511 if ( tmp_image == (Image *) NULL )
1512 return((Image *) NULL);
1513 (void) SetImageVirtualPixelMethod(tmp_image,TransparentVirtualPixelMethod);
1515 if (image->matte == MagickFalse)
1520 (void) SetImageAlphaChannel(tmp_image,SetAlphaChannel);
1521 resize_image=DistortImage(tmp_image,AffineDistortion,12,distort_args,
1522 MagickTrue,exception),
1524 tmp_image=DestroyImage(tmp_image);
1525 if ( resize_image == (Image *) NULL )
1526 return((Image *) NULL);
1528 (void) SetImageAlphaChannel(resize_image,DeactivateAlphaChannel);
1529 InheritException(exception,&image->exception);
1542 (void) SeparateImageChannel(tmp_image,TrueAlphaChannel);
1543 (void) SetImageAlphaChannel(tmp_image,OpaqueAlphaChannel);
1544 resize_alpha=DistortImage(tmp_image,AffineDistortion,12,distort_args,
1545 MagickTrue,exception),
1546 tmp_image=DestroyImage(tmp_image);
1547 if ( resize_alpha == (Image *) NULL )
1548 return((Image *) NULL);
1551 tmp_image=CloneImage(image,0,0,MagickTrue,exception);
1552 if ( tmp_image == (Image *) NULL )
1553 return((Image *) NULL);
1554 (void) SetImageVirtualPixelMethod(tmp_image,
1555 TransparentVirtualPixelMethod);
1556 (void) SetImageVirtualPixelMethod(tmp_image,
1557 TransparentVirtualPixelMethod);
1558 resize_image=DistortImage(tmp_image,AffineDistortion,12,distort_args,
1559 MagickTrue,exception),
1560 tmp_image=DestroyImage(tmp_image);
1561 if ( resize_image == (Image *) NULL)
1563 resize_alpha=DestroyImage(resize_alpha);
1564 return((Image *) NULL);
1568 (void) SetImageAlphaChannel(resize_image,DeactivateAlphaChannel);
1569 (void) SetImageAlphaChannel(resize_alpha,DeactivateAlphaChannel);
1570 (void) CompositeImage(resize_image,CopyOpacityCompositeOp,resize_alpha,
1572 InheritException(exception,&resize_image->exception);
1573 resize_image->matte=image->matte;
1574 resize_image->compose=image->compose;
1575 resize_alpha=DestroyImage(resize_alpha);
1577 (void) SetImageVirtualPixelMethod(resize_image,vp_save);
1582 crop_area.width=columns;
1583 crop_area.height=rows;
1587 tmp_image=resize_image;
1588 resize_image=CropImage(tmp_image,&crop_area,exception);
1589 tmp_image=DestroyImage(tmp_image);
1590 if (resize_image != (Image *) NULL)
1592 resize_image->page.width=0;
1593 resize_image->page.height=0;
1595 return(resize_image);
1687MagickExport Image *DistortImage(
const Image *image,DistortImageMethod method,
1688 const size_t number_arguments,
const double *arguments,
1689 MagickBooleanType bestfit,ExceptionInfo *exception)
1691#define DistortImageTag "Distort/Image"
1706 assert(image != (Image *) NULL);
1707 assert(image->signature == MagickCoreSignature);
1708 assert(exception != (ExceptionInfo *) NULL);
1709 assert(exception->signature == MagickCoreSignature);
1710 if (IsEventLogging() != MagickFalse)
1711 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1715 if (method == ResizeDistortion)
1717 if (number_arguments != 2)
1719 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1720 "InvalidArgument",
"%s : '%s'",
"Resize",
1721 "Invalid number of args: 2 only");
1722 return((Image *) NULL);
1724 distort_image=DistortResizeImage(image,CastDoubleToSizeT(arguments[0]),
1725 CastDoubleToSizeT(arguments[1]),exception);
1726 return(distort_image);
1736 coeff=GenerateCoefficients(image,&method,number_arguments,arguments,0,
1738 if (coeff == (
double *) NULL)
1739 return((Image *) NULL);
1747 geometry.width=image->columns;
1748 geometry.height=image->rows;
1752 if ( method == ArcDistortion ) {
1753 bestfit = MagickTrue;
1762 fix_bounds = MagickTrue;
1764 s.x=s.y=min.x=max.x=min.y=max.y=0.0;
1767#define InitalBounds(p) \
1770 min.x = max.x = p.x; \
1771 min.y = max.y = p.y; \
1773#define ExpandBounds(p) \
1776 min.x = MagickMin(min.x,p.x); \
1777 max.x = MagickMax(max.x,p.x); \
1778 min.y = MagickMin(min.y,p.y); \
1779 max.y = MagickMax(max.y,p.y); \
1784 case AffineDistortion:
1785 {
double inverse[6];
1786 InvertAffineCoefficients(coeff, inverse);
1787 s.x = (double) image->page.x;
1788 s.y = (double) image->page.y;
1789 d.x = inverse[0]*s.x+inverse[1]*s.y+inverse[2];
1790 d.y = inverse[3]*s.x+inverse[4]*s.y+inverse[5];
1792 s.x = (double) image->page.x+image->columns;
1793 s.y = (double) image->page.y;
1794 d.x = inverse[0]*s.x+inverse[1]*s.y+inverse[2];
1795 d.y = inverse[3]*s.x+inverse[4]*s.y+inverse[5];
1797 s.x = (double) image->page.x;
1798 s.y = (double) image->page.y+image->rows;
1799 d.x = inverse[0]*s.x+inverse[1]*s.y+inverse[2];
1800 d.y = inverse[3]*s.x+inverse[4]*s.y+inverse[5];
1802 s.x = (double) image->page.x+image->columns;
1803 s.y = (double) image->page.y+image->rows;
1804 d.x = inverse[0]*s.x+inverse[1]*s.y+inverse[2];
1805 d.y = inverse[3]*s.x+inverse[4]*s.y+inverse[5];
1809 case PerspectiveDistortion:
1810 {
double inverse[8], scale;
1811 InvertPerspectiveCoefficients(coeff, inverse);
1812 s.x = (double) image->page.x;
1813 s.y = (double) image->page.y;
1814 scale=inverse[6]*s.x+inverse[7]*s.y+1.0;
1815 scale=MagickSafeReciprocal(scale);
1816 d.x = scale*(inverse[0]*s.x+inverse[1]*s.y+inverse[2]);
1817 d.y = scale*(inverse[3]*s.x+inverse[4]*s.y+inverse[5]);
1819 s.x = (double) image->page.x+image->columns;
1820 s.y = (double) image->page.y;
1821 scale=inverse[6]*s.x+inverse[7]*s.y+1.0;
1822 scale=MagickSafeReciprocal(scale);
1823 d.x = scale*(inverse[0]*s.x+inverse[1]*s.y+inverse[2]);
1824 d.y = scale*(inverse[3]*s.x+inverse[4]*s.y+inverse[5]);
1826 s.x = (double) image->page.x;
1827 s.y = (double) image->page.y+image->rows;
1828 scale=inverse[6]*s.x+inverse[7]*s.y+1.0;
1829 scale=MagickSafeReciprocal(scale);
1830 d.x = scale*(inverse[0]*s.x+inverse[1]*s.y+inverse[2]);
1831 d.y = scale*(inverse[3]*s.x+inverse[4]*s.y+inverse[5]);
1833 s.x = (double) image->page.x+image->columns;
1834 s.y = (double) image->page.y+image->rows;
1835 scale=inverse[6]*s.x+inverse[7]*s.y+1.0;
1836 scale=MagickSafeReciprocal(scale);
1837 d.x = scale*(inverse[0]*s.x+inverse[1]*s.y+inverse[2]);
1838 d.y = scale*(inverse[3]*s.x+inverse[4]*s.y+inverse[5]);
1845 a = coeff[0]-coeff[1]/2; ca = cos(a); sa = sin(a);
1849 d.x = (coeff[2]-coeff[3])*ca;
1850 d.y = (coeff[2]-coeff[3])*sa;
1852 a = coeff[0]+coeff[1]/2; ca = cos(a); sa = sin(a);
1856 d.x = (coeff[2]-coeff[3])*ca;
1857 d.y = (coeff[2]-coeff[3])*sa;
1860 for( a=(
double) (ceil((
double) ((coeff[0]-coeff[1]/2.0)/MagickPI2))*MagickPI2);
1861 a<(coeff[0]+coeff[1]/2.0); a+=MagickPI2 ) {
1862 ca = cos(a); sa = sin(a);
1872 coeff[1] = (double) (Magick2PI*image->columns/coeff[1]);
1873 coeff[3] = (double)image->rows/coeff[3];
1876 case PolarDistortion:
1878 if (number_arguments < 2)
1879 coeff[2] = coeff[3] = 0.0;
1880 min.x = coeff[2]-coeff[0];
1881 max.x = coeff[2]+coeff[0];
1882 min.y = coeff[3]-coeff[0];
1883 max.y = coeff[3]+coeff[0];
1885 coeff[7]=(double) geometry.height/(coeff[0]-coeff[1]);
1888 case DePolarDistortion:
1892 fix_bounds = MagickFalse;
1893 geometry.x = geometry.y = 0;
1894 geometry.height = CastDoubleToSizeT(ceil(coeff[0]-coeff[1]));
1895 geometry.width = CastDoubleToSizeT(ceil((coeff[0]-coeff[1])*
1896 (coeff[5]-coeff[4])*0.5));
1898 coeff[6]=(coeff[5]-coeff[4])*MagickSafeReciprocal(geometry.width);
1899 coeff[7]=(coeff[0]-coeff[1])*MagickSafeReciprocal(geometry.height);
1902 case Cylinder2PlaneDistortion:
1907 geometry.x = geometry.y = 0;
1908 geometry.width = CastDoubleToSizeT(ceil( 2.0*coeff[1]*tan(coeff[0]/2.0) ));
1909 geometry.height = CastDoubleToSizeT(ceil( 2.0*coeff[3]/cos(coeff[0]/2.0) ));
1911 coeff[4] = (double) geometry.width/2.0;
1912 coeff[5] = (double) geometry.height/2.0;
1913 fix_bounds = MagickFalse;
1916 case Plane2CylinderDistortion:
1920 geometry.x = geometry.y = 0;
1921 geometry.width = CastDoubleToSizeT(ceil(coeff[0]*coeff[1]));
1922 geometry.height = CastDoubleToSizeT(2.0*coeff[3]);
1924 coeff[4] = (double) geometry.width/2.0;
1925 coeff[5] = (double) geometry.height/2.0;
1926 fix_bounds = MagickFalse;
1930 case ShepardsDistortion:
1931 case BilinearForwardDistortion:
1932 case BilinearReverseDistortion:
1934 case QuadrilateralDistortion:
1936 case PolynomialDistortion:
1937 case BarrelDistortion:
1938 case BarrelInverseDistortion:
1941 bestfit = MagickFalse;
1942 fix_bounds = MagickFalse;
1951 geometry.x = (ssize_t) floor(min.x-0.5);
1952 geometry.y = (ssize_t) floor(min.y-0.5);
1953 geometry.width=CastDoubleToSizeT(ceil(max.x-geometry.x+0.5));
1954 geometry.height=CastDoubleToSizeT(ceil(max.y-geometry.y+0.5));
1963 {
const char *artifact=GetImageArtifact(image,
"distort:viewport");
1964 viewport_given = MagickFalse;
1965 if ( artifact != (
const char *) NULL ) {
1966 MagickStatusType flags=ParseAbsoluteGeometry(artifact,&geometry);
1968 (void) ThrowMagickException(exception,GetMagickModule(),
1969 OptionWarning,
"InvalidGeometry",
"`%s' `%s'",
1970 "distort:viewport",artifact);
1972 viewport_given = MagickTrue;
1977 if ( GetImageArtifact(image,
"verbose") != (
const char *) NULL ) {
1980 char image_gen[MaxTextExtent];
1984 if ( bestfit || viewport_given ) {
1985 (void) FormatLocaleString(image_gen, MaxTextExtent,
" -size %.20gx%.20g "
1986 "-page %+.20g%+.20g xc: +insert \\\n",(
double) geometry.width,
1987 (
double) geometry.height,(
double) geometry.x,(
double) geometry.y);
1988 lookup=
"v.p{ xx-v.page.x-.5, yy-v.page.y-.5 }";
1991 image_gen[0] =
'\0';
1992 lookup =
"p{ xx-page.x-.5, yy-page.y-.5 }";
1997 case AffineDistortion:
2002 inverse=(
double *) AcquireQuantumMemory(6,
sizeof(*inverse));
2003 if (inverse == (
double *) NULL)
2005 coeff=(
double *) RelinquishMagickMemory(coeff);
2006 (void) ThrowMagickException(exception,GetMagickModule(),
2007 ResourceLimitError,
"MemoryAllocationFailed",
"%s",
"DistortImages");
2008 return((Image *) NULL);
2010 InvertAffineCoefficients(coeff, inverse);
2011 CoefficientsToAffineArgs(inverse);
2012 (void) FormatLocaleFile(stderr,
"Affine Projection:\n");
2013 (void) FormatLocaleFile(stderr,
2014 " -distort AffineProjection \\\n '");
2015 for (i=0; i < 5; i++)
2016 (
void) FormatLocaleFile(stderr,
"%lf,", inverse[i]);
2017 (void) FormatLocaleFile(stderr,
"%lf'\n", inverse[5]);
2018 inverse=(
double *) RelinquishMagickMemory(inverse);
2019 (void) FormatLocaleFile(stderr,
"Affine Distort, FX Equivelent:\n");
2020 (void) FormatLocaleFile(stderr,
"%s", image_gen);
2021 (void) FormatLocaleFile(stderr,
2022 " -fx 'ii=i+page.x+0.5; jj=j+page.y+0.5;\n");
2023 (void) FormatLocaleFile(stderr,
" xx=%+lf*ii %+lf*jj %+lf;\n",
2024 coeff[0],coeff[1],coeff[2]);
2025 (void) FormatLocaleFile(stderr,
" yy=%+lf*ii %+lf*jj %+lf;\n",
2026 coeff[3],coeff[4],coeff[5]);
2027 (void) FormatLocaleFile(stderr,
" %s' \\\n",lookup);
2030 case PerspectiveDistortion:
2035 inverse=(
double *) AcquireQuantumMemory(8,
sizeof(*inverse));
2036 if (inverse == (
double *) NULL)
2038 coeff=(
double *) RelinquishMagickMemory(coeff);
2039 (void) ThrowMagickException(exception,GetMagickModule(),
2040 ResourceLimitError,
"MemoryAllocationFailed",
"%s",
2041 "DistortCoefficients");
2042 return((Image *) NULL);
2044 InvertPerspectiveCoefficients(coeff, inverse);
2045 (void) FormatLocaleFile(stderr,
"Perspective Projection:\n");
2046 (void) FormatLocaleFile(stderr,
2047 " -distort PerspectiveProjection \\\n '");
2048 for (i=0; i < 4; i++)
2049 (
void) FormatLocaleFile(stderr,
"%.*g, ",GetMagickPrecision(),
2051 (void) FormatLocaleFile(stderr,
"\n ");
2053 (
void) FormatLocaleFile(stderr,
"%.*g, ",GetMagickPrecision(),
2055 (void) FormatLocaleFile(stderr,
"%.*g'\n",GetMagickPrecision(),
2057 inverse=(
double *) RelinquishMagickMemory(inverse);
2058 (void) FormatLocaleFile(stderr,
"Perspective Distort, FX Equivalent:\n");
2059 (void) FormatLocaleFile(stderr,
"%.1024s",image_gen);
2060 (void) FormatLocaleFile(stderr,
2061 " -fx 'ii=i+page.x+0.5; jj=j+page.y+0.5;\n");
2062 (void) FormatLocaleFile(stderr,
" rr=%+.*g*ii %+.*g*jj + 1;\n",
2063 GetMagickPrecision(),coeff[6],GetMagickPrecision(),coeff[7]);
2064 (void) FormatLocaleFile(stderr,
2065 " xx=(%+.*g*ii %+.*g*jj %+.*g)/rr;\n",
2066 GetMagickPrecision(),coeff[0],GetMagickPrecision(),coeff[1],
2067 GetMagickPrecision(),coeff[2]);
2068 (void) FormatLocaleFile(stderr,
2069 " yy=(%+.*g*ii %+.*g*jj %+.*g)/rr;\n",
2070 GetMagickPrecision(),coeff[3],GetMagickPrecision(),coeff[4],
2071 GetMagickPrecision(),coeff[5]);
2072 (void) FormatLocaleFile(stderr,
" rr%s0 ? %s : blue' \\\n",
2073 coeff[8] < 0.0 ?
"<" :
">", lookup);
2076 case BilinearForwardDistortion:
2078 (void) FormatLocaleFile(stderr,
"BilinearForward Mapping Equations:\n");
2079 (void) FormatLocaleFile(stderr,
"%s", image_gen);
2080 (void) FormatLocaleFile(stderr,
" i = %+lf*x %+lf*y %+lf*x*y %+lf;\n",
2081 coeff[0],coeff[1],coeff[2],coeff[3]);
2082 (void) FormatLocaleFile(stderr,
" j = %+lf*x %+lf*y %+lf*x*y %+lf;\n",
2083 coeff[4],coeff[5],coeff[6],coeff[7]);
2086 (void) FormatLocaleFile(stderr,
" c8 = %+lf c9 = 2*a = %+lf;\n",
2087 coeff[8], coeff[9]);
2089 (void) FormatLocaleFile(stderr,
2090 "BilinearForward Distort, FX Equivalent:\n");
2091 (void) FormatLocaleFile(stderr,
"%s", image_gen);
2092 (void) FormatLocaleFile(stderr,
2093 " -fx 'ii=i+page.x%+lf; jj=j+page.y%+lf;\n",0.5-coeff[3],0.5-
2095 (void) FormatLocaleFile(stderr,
" bb=%lf*ii %+lf*jj %+lf;\n",
2096 coeff[6], -coeff[2], coeff[8]);
2100 (void) FormatLocaleFile(stderr,
2101 " rt=bb*bb %+lf*(%lf*ii%+lf*jj);\n",-2*coeff[9],coeff[4],
2103 (void) FormatLocaleFile(stderr,
2104 " yy=( -bb + sqrt(rt) ) / %lf;\n",coeff[9]);
2107 (
void) FormatLocaleFile(stderr,
" yy=(%lf*ii%+lf*jj)/bb;\n",
2108 -coeff[4],coeff[0]);
2109 (void) FormatLocaleFile(stderr,
2110 " xx=(ii %+lf*yy)/(%lf %+lf*yy);\n",-coeff[1],coeff[0],
2112 if ( coeff[9] != 0 )
2113 (void) FormatLocaleFile(stderr,
" (rt < 0 ) ? red : %s'\n",
2116 (
void) FormatLocaleFile(stderr,
" %s' \\\n", lookup);
2119 case BilinearReverseDistortion:
2122 (void) FormatLocaleFile(stderr,
"Polynomial Projection Distort:\n");
2123 (void) FormatLocaleFile(stderr,
" -distort PolynomialProjection \\\n");
2124 (void) FormatLocaleFile(stderr,
" '1.5, %lf, %lf, %lf, %lf,\n",
2125 coeff[3], coeff[0], coeff[1], coeff[2]);
2126 (void) FormatLocaleFile(stderr,
" %lf, %lf, %lf, %lf'\n",
2127 coeff[7], coeff[4], coeff[5], coeff[6]);
2129 (void) FormatLocaleFile(stderr,
2130 "BilinearReverse Distort, FX Equivalent:\n");
2131 (void) FormatLocaleFile(stderr,
"%s", image_gen);
2132 (void) FormatLocaleFile(stderr,
2133 " -fx 'ii=i+page.x+0.5; jj=j+page.y+0.5;\n");
2134 (void) FormatLocaleFile(stderr,
2135 " xx=%+lf*ii %+lf*jj %+lf*ii*jj %+lf;\n",coeff[0],coeff[1],
2136 coeff[2], coeff[3]);
2137 (void) FormatLocaleFile(stderr,
2138 " yy=%+lf*ii %+lf*jj %+lf*ii*jj %+lf;\n",coeff[4],coeff[5],
2139 coeff[6], coeff[7]);
2140 (void) FormatLocaleFile(stderr,
" %s' \\\n", lookup);
2143 case PolynomialDistortion:
2145 size_t nterms = CastDoubleToSizeT(coeff[1]);
2146 (void) FormatLocaleFile(stderr,
2147 "Polynomial (order %lg, terms %lu), FX Equivalent\n",coeff[0],
2148 (
unsigned long) nterms);
2149 (void) FormatLocaleFile(stderr,
"%s", image_gen);
2150 (void) FormatLocaleFile(stderr,
2151 " -fx 'ii=i+page.x+0.5; jj=j+page.y+0.5;\n");
2152 (void) FormatLocaleFile(stderr,
" xx =");
2153 for (i=0; i < (ssize_t) nterms; i++)
2155 if ((i != 0) && (i%4 == 0))
2156 (void) FormatLocaleFile(stderr,
"\n ");
2157 (void) FormatLocaleFile(stderr,
" %+lf%s",coeff[2+i],
2160 (void) FormatLocaleFile(stderr,
";\n yy =");
2161 for (i=0; i < (ssize_t) nterms; i++)
2163 if ((i != 0) && (i%4 == 0))
2164 (void) FormatLocaleFile(stderr,
"\n ");
2165 (void) FormatLocaleFile(stderr,
" %+lf%s",coeff[2+i+nterms],
2168 (void) FormatLocaleFile(stderr,
";\n %s' \\\n", lookup);
2173 (void) FormatLocaleFile(stderr,
"Arc Distort, Internal Coefficients:\n");
2174 for (i=0; i < 5; i++)
2175 (
void) FormatLocaleFile(stderr,
2176 " c%.20g = %+lf\n",(
double) i,coeff[i]);
2177 (void) FormatLocaleFile(stderr,
"Arc Distort, FX Equivalent:\n");
2178 (void) FormatLocaleFile(stderr,
"%s", image_gen);
2179 (void) FormatLocaleFile(stderr,
" -fx 'ii=i+page.x; jj=j+page.y;\n");
2180 (void) FormatLocaleFile(stderr,
" xx=(atan2(jj,ii)%+lf)/(2*pi);\n",
2182 (void) FormatLocaleFile(stderr,
" xx=xx-round(xx);\n");
2183 (void) FormatLocaleFile(stderr,
" xx=xx*%lf %+lf;\n",coeff[1],
2185 (void) FormatLocaleFile(stderr,
2186 " yy=(%lf - hypot(ii,jj)) * %lf;\n",coeff[2],coeff[3]);
2187 (void) FormatLocaleFile(stderr,
" v.p{xx-.5,yy-.5}' \\\n");
2190 case PolarDistortion:
2192 (void) FormatLocaleFile(stderr,
"Polar Distort, Internal Coefficients\n");
2193 for (i=0; i < 8; i++)
2194 (
void) FormatLocaleFile(stderr,
" c%.20g = %+lf\n",(
double) i,
2196 (void) FormatLocaleFile(stderr,
"Polar Distort, FX Equivalent:\n");
2197 (void) FormatLocaleFile(stderr,
"%s", image_gen);
2198 (void) FormatLocaleFile(stderr,
2199 " -fx 'ii=i+page.x%+lf; jj=j+page.y%+lf;\n",-coeff[2],-coeff[3]);
2200 (void) FormatLocaleFile(stderr,
" xx=(atan2(ii,jj)%+lf)/(2*pi);\n",
2201 -(coeff[4]+coeff[5])/2 );
2202 (void) FormatLocaleFile(stderr,
" xx=xx-round(xx);\n");
2203 (void) FormatLocaleFile(stderr,
" xx=xx*2*pi*%lf + v.w/2;\n",
2205 (void) FormatLocaleFile(stderr,
" yy=(hypot(ii,jj)%+lf)*%lf;\n",
2206 -coeff[1],coeff[7] );
2207 (void) FormatLocaleFile(stderr,
" v.p{xx-.5,yy-.5}' \\\n");
2210 case DePolarDistortion:
2212 (void) FormatLocaleFile(stderr,
2213 "DePolar Distort, Internal Coefficients\n");
2214 for (i=0; i < 8; i++)
2215 (
void) FormatLocaleFile(stderr,
" c%.20g = %+lf\n",(
double) i,
2217 (void) FormatLocaleFile(stderr,
"DePolar Distort, FX Equivalent:\n");
2218 (void) FormatLocaleFile(stderr,
"%s", image_gen);
2219 (void) FormatLocaleFile(stderr,
" -fx 'aa=(i+.5)*%lf %+lf;\n",
2220 coeff[6],+coeff[4]);
2221 (void) FormatLocaleFile(stderr,
" rr=(j+.5)*%lf %+lf;\n",
2222 coeff[7],+coeff[1]);
2223 (void) FormatLocaleFile(stderr,
" xx=rr*sin(aa) %+lf;\n",
2225 (void) FormatLocaleFile(stderr,
" yy=rr*cos(aa) %+lf;\n",
2227 (void) FormatLocaleFile(stderr,
" v.p{xx-.5,yy-.5}' \\\n");
2230 case Cylinder2PlaneDistortion:
2232 (void) FormatLocaleFile(stderr,
2233 "Cylinder to Plane Distort, Internal Coefficients\n");
2234 (void) FormatLocaleFile(stderr,
" cylinder_radius = %+lf\n",coeff[1]);
2235 (void) FormatLocaleFile(stderr,
2236 "Cylinder to Plane Distort, FX Equivalent:\n");
2237 (void) FormatLocaleFile(stderr,
"%s", image_gen);
2238 (void) FormatLocaleFile(stderr,
2239 " -fx 'ii=i+page.x%+lf+0.5; jj=j+page.y%+lf+0.5;\n",-coeff[4],
2241 (void) FormatLocaleFile(stderr,
" aa=atan(ii/%+lf);\n",coeff[1]);
2242 (void) FormatLocaleFile(stderr,
" xx=%lf*aa%+lf;\n",
2244 (void) FormatLocaleFile(stderr,
" yy=jj*cos(aa)%+lf;\n",coeff[3]);
2245 (void) FormatLocaleFile(stderr,
" %s' \\\n", lookup);
2248 case Plane2CylinderDistortion:
2250 (void) FormatLocaleFile(stderr,
2251 "Plane to Cylinder Distort, Internal Coefficients\n");
2252 (void) FormatLocaleFile(stderr,
" cylinder_radius = %+lf\n",coeff[1]);
2253 (void) FormatLocaleFile(stderr,
2254 "Plane to Cylinder Distort, FX Equivalent:\n");
2255 (void) FormatLocaleFile(stderr,
"%s", image_gen);
2256 (void) FormatLocaleFile(stderr,
2257 " -fx 'ii=i+page.x%+lf+0.5; jj=j+page.y%+lf+0.5;\n",-coeff[4],
2259 (void) FormatLocaleFile(stderr,
" ii=ii/%+lf;\n",coeff[1]);
2260 (void) FormatLocaleFile(stderr,
" xx=%lf*tan(ii)%+lf;\n",coeff[1],
2262 (void) FormatLocaleFile(stderr,
" yy=jj/cos(ii)%+lf;\n",coeff[3]);
2263 (void) FormatLocaleFile(stderr,
" %s' \\\n", lookup);
2266 case BarrelDistortion:
2267 case BarrelInverseDistortion:
2278 xc=((double)image->columns-1.0)/2.0+image->page.x;
2279 yc=((double)image->rows-1.0)/2.0+image->page.y;
2280 (void) FormatLocaleFile(stderr,
"Barrel%s Distort, FX Equivalent:\n",
2281 method == BarrelDistortion ?
"" :
"Inv");
2282 (void) FormatLocaleFile(stderr,
"%s", image_gen);
2283 if ( fabs(coeff[8]-xc-0.5) < 0.1 && fabs(coeff[9]-yc-0.5) < 0.1 )
2284 (void) FormatLocaleFile(stderr,
" -fx 'xc=(w-1)/2; yc=(h-1)/2;\n");
2286 (
void) FormatLocaleFile(stderr,
" -fx 'xc=%lf; yc=%lf;\n",coeff[8]-
2288 (void) FormatLocaleFile(stderr,
2289 " ii=i-xc; jj=j-yc; rr=hypot(ii,jj);\n");
2290 (void) FormatLocaleFile(stderr,
2291 " ii=ii%s(%lf*rr*rr*rr %+lf*rr*rr %+lf*rr %+lf);\n",
2292 method == BarrelDistortion ?
"*" :
"/",coeff[0],coeff[1],coeff[2],
2294 (void) FormatLocaleFile(stderr,
2295 " jj=jj%s(%lf*rr*rr*rr %+lf*rr*rr %+lf*rr %+lf);\n",
2296 method == BarrelDistortion ?
"*" :
"/",coeff[4],coeff[5],coeff[6],
2298 (void) FormatLocaleFile(stderr,
" p{ii+xc,jj+yc}' \\\n");
2309 {
const char *artifact;
2310 artifact=GetImageArtifact(image,
"distort:scale");
2311 output_scaling = 1.0;
2312 if (artifact != (
const char *) NULL) {
2313 output_scaling = fabs(StringToDouble(artifact,(
char **) NULL));
2314 geometry.width=CastDoubleToSizeT(output_scaling*geometry.width+0.5);
2315 geometry.height=CastDoubleToSizeT(output_scaling*geometry.height+0.5);
2316 geometry.x=CastDoubleToSsizeT(output_scaling*geometry.x+0.5);
2317 geometry.y=CastDoubleToSsizeT(output_scaling*geometry.y+0.5);
2318 if ( output_scaling < 0.1 ) {
2319 coeff = (
double *) RelinquishMagickMemory(coeff);
2320 (void) ThrowMagickException(exception,GetMagickModule(),
2321 OptionError,
"InvalidArgument",
"%s",
"-define distort:scale" );
2322 return((Image *) NULL);
2324 output_scaling = 1/output_scaling;
2327#define ScaleFilter(F,A,B,C,D) \
2328 ScaleResampleFilter( (F), \
2329 output_scaling*(A), output_scaling*(B), \
2330 output_scaling*(C), output_scaling*(D) )
2335 distort_image=CloneImage(image,geometry.width,geometry.height,MagickTrue,
2337 if (distort_image == (Image *) NULL)
2339 coeff=(
double *) RelinquishMagickMemory(coeff);
2340 return((Image *) NULL);
2343 if (SetImageStorageClass(distort_image,DirectClass) == MagickFalse)
2345 coeff=(
double *) RelinquishMagickMemory(coeff);
2346 InheritException(exception,&distort_image->exception);
2347 distort_image=DestroyImage(distort_image);
2348 return((Image *) NULL);
2350 if ((IsPixelGray(&distort_image->background_color) == MagickFalse) &&
2351 (IsGrayColorspace(distort_image->colorspace) != MagickFalse))
2352 (void) SetImageColorspace(distort_image,sRGBColorspace);
2353 if (distort_image->background_color.opacity != OpaqueOpacity)
2354 distort_image->matte=MagickTrue;
2355 distort_image->page.x=geometry.x;
2356 distort_image->page.y=geometry.y;
2374 **magick_restrict resample_filter;
2381 GetMagickPixelPacket(distort_image,&zero);
2382 resample_filter=AcquireResampleFilterTLS(image,UndefinedVirtualPixelMethod,
2383 MagickFalse,exception);
2384 distort_view=AcquireAuthenticCacheView(distort_image,exception);
2385#if defined(MAGICKCORE_OPENMP_SUPPORT)
2386 #pragma omp parallel for schedule(static) shared(progress,status) \
2387 magick_number_threads(image,distort_image,distort_image->rows,1)
2389 for (j=0; j < (ssize_t) distort_image->rows; j++)
2392 id = GetOpenMPThreadId();
2409 *magick_restrict indexes;
2417 q=QueueCacheViewAuthenticPixels(distort_view,0,j,distort_image->columns,1,
2419 if (q == (PixelPacket *) NULL)
2424 indexes=GetCacheViewAuthenticIndexQueue(distort_view);
2432 case AffineDistortion:
2433 ScaleFilter( resample_filter[
id],
2435 coeff[3], coeff[4] );
2448 GetMagickPixelPacket(distort_image,&invalid);
2449 SetMagickPixelPacket(distort_image,&distort_image->matte_color,
2450 (IndexPacket *) NULL, &invalid);
2451 if (distort_image->colorspace == CMYKColorspace)
2452 ConvertRGBToCMYK(&invalid);
2454 for (i=0; i < (ssize_t) distort_image->columns; i++)
2457 d.x = (double) (geometry.x+i+0.5)*output_scaling;
2458 d.y = (double) (geometry.y+j+0.5)*output_scaling;
2462 case AffineDistortion:
2464 s.x=coeff[0]*d.x+coeff[1]*d.y+coeff[2];
2465 s.y=coeff[3]*d.x+coeff[4]*d.y+coeff[5];
2469 case PerspectiveDistortion:
2472 p,q,r,abs_r,abs_c6,abs_c7,scale;
2474 p=coeff[0]*d.x+coeff[1]*d.y+coeff[2];
2475 q=coeff[3]*d.x+coeff[4]*d.y+coeff[5];
2476 r=coeff[6]*d.x+coeff[7]*d.y+1.0;
2478 validity = (r*coeff[8] < 0.0) ? 0.0 : 1.0;
2481 abs_c6 = fabs(coeff[6]);
2482 abs_c7 = fabs(coeff[7]);
2483 if ( abs_c6 > abs_c7 ) {
2484 if ( abs_r < abs_c6*output_scaling )
2485 validity = 0.5 - coeff[8]*r/(coeff[6]*output_scaling);
2487 else if ( abs_r < abs_c7*output_scaling )
2488 validity = 0.5 - coeff[8]*r/(coeff[7]*output_scaling);
2490 if ( validity > 0.0 ) {
2497 ScaleFilter( resample_filter[
id],
2498 (r*coeff[0] - p*coeff[6])*scale,
2499 (r*coeff[1] - p*coeff[7])*scale,
2500 (r*coeff[3] - q*coeff[6])*scale,
2501 (r*coeff[4] - q*coeff[7])*scale );
2505 case BilinearReverseDistortion:
2508 s.x=coeff[0]*d.x+coeff[1]*d.y+coeff[2]*d.x*d.y+coeff[3];
2509 s.y=coeff[4]*d.x+coeff[5]*d.y
2510 +coeff[6]*d.x*d.y+coeff[7];
2512 ScaleFilter( resample_filter[
id],
2513 coeff[0] + coeff[2]*d.y,
2514 coeff[1] + coeff[2]*d.x,
2515 coeff[4] + coeff[6]*d.y,
2516 coeff[5] + coeff[6]*d.x );
2519 case BilinearForwardDistortion:
2524 d.x -= coeff[3]; d.y -= coeff[7];
2525 b = coeff[6]*d.x - coeff[2]*d.y + coeff[8];
2526 c = coeff[4]*d.x - coeff[0]*d.y;
2531 if ( fabs(coeff[9]) < MagickEpsilon )
2534 c = b*b - 2*coeff[9]*c;
2538 s.y = ( -b + sqrt(c) )/coeff[9];
2540 if ( validity > 0.0 )
2541 s.x = ( d.x - coeff[1]*s.y) / ( coeff[0] + coeff[2]*s.y );
2551 case BilinearDistortion:
2556 case PolynomialDistortion:
2563 nterms=(ssize_t)coeff[1];
2568 s.x=s.y=du.x=du.y=dv.x=dv.y=0.0;
2569 for(k=0; k < nterms; k++) {
2570 s.x += poly_basis_fn(k,d.x,d.y)*coeff[2+k];
2571 du.x += poly_basis_dx(k,d.x,d.y)*coeff[2+k];
2572 du.y += poly_basis_dy(k,d.x,d.y)*coeff[2+k];
2573 s.y += poly_basis_fn(k,d.x,d.y)*coeff[2+k+nterms];
2574 dv.x += poly_basis_dx(k,d.x,d.y)*coeff[2+k+nterms];
2575 dv.y += poly_basis_dy(k,d.x,d.y)*coeff[2+k+nterms];
2577 ScaleFilter( resample_filter[
id], du.x,du.y,dv.x,dv.y );
2583 s.x = (double) ((atan2(d.y,d.x) - coeff[0])/Magick2PI);
2584 s.x -= MagickRound(s.x);
2585 s.y = hypot(d.x,d.y);
2592 if ( s.y > MagickEpsilon )
2593 ScaleFilter( resample_filter[
id],
2594 (
double) (coeff[1]/(Magick2PI*s.y)), 0, 0, coeff[3] );
2596 ScaleFilter( resample_filter[
id],
2597 distort_image->columns*2, 0, 0, coeff[3] );
2600 s.x = s.x*coeff[1] + coeff[4] + image->page.x +0.5;
2601 s.y = (coeff[2] - s.y) * coeff[3] + image->page.y;
2604 case PolarDistortion:
2608 s.x = atan2(d.x,d.y) - (coeff[4]+coeff[5])/2;
2610 s.x -= MagickRound(s.x);
2612 s.y = hypot(d.x,d.y);
2617 if ( s.y > MagickEpsilon )
2618 ScaleFilter( resample_filter[
id],
2619 (
double) (coeff[6]/(Magick2PI*s.y)), 0, 0, coeff[7] );
2621 ScaleFilter( resample_filter[
id],
2622 distort_image->columns*2, 0, 0, coeff[7] );
2625 s.x = s.x*coeff[6] + (double)image->columns/2.0 + image->page.x;
2626 s.y = (s.y-coeff[1])*coeff[7] + image->page.y;
2629 case DePolarDistortion:
2632 d.x = ((double)i+0.5)*output_scaling*coeff[6]+coeff[4];
2633 d.y = ((double)j+0.5)*output_scaling*coeff[7]+coeff[1];
2634 s.x = d.y*sin(d.x) + coeff[2];
2635 s.y = d.y*cos(d.x) + coeff[3];
2639 case Cylinder2PlaneDistortion:
2643 d.x -= coeff[4]; d.y -= coeff[5];
2650 ScaleFilter( resample_filter[
id],
2651 1.0/(1.0+d.x*d.x), 0.0, -d.x*s.y*cx*cx/coeff[1], s.y/d.y );
2653if ( i == 0 && j == 0 ) {
2654 fprintf(stderr,
"x=%lf y=%lf u=%lf v=%lf\n", d.x*coeff[1], d.y, s.x, s.y);
2655 fprintf(stderr,
"phi = %lf\n", (
double)(ax * 180.0/MagickPI) );
2656 fprintf(stderr,
"du/dx=%lf du/dx=%lf dv/dx=%lf dv/dy=%lf\n",
2657 1.0/(1.0+d.x*d.x), 0.0, -d.x*s.y*cx*cx/coeff[1], s.y/d.y );
2661 s.x += coeff[2]; s.y += coeff[3];
2664 case Plane2CylinderDistortion:
2667 d.x -= coeff[4]; d.y -= coeff[5];
2671 validity = (double) ((coeff[1]*MagickPI2 - fabs(d.x))/output_scaling + 0.5);
2673 if ( validity > 0.0 ) {
2681 ScaleFilter( resample_filter[
id],
2682 cx*cx, 0.0, s.y*cx/coeff[1], cx );
2685if ( d.x == 0.5 && d.y == 0.5 ) {
2686 fprintf(stderr,
"x=%lf y=%lf u=%lf v=%lf\n", d.x*coeff[1], d.y, s.x, s.y);
2687 fprintf(stderr,
"radius = %lf phi = %lf validity = %lf\n",
2688 coeff[1], (
double)(d.x * 180.0/MagickPI), validity );
2689 fprintf(stderr,
"du/dx=%lf du/dx=%lf dv/dx=%lf dv/dy=%lf\n",
2690 cx*cx, 0.0, s.y*cx/coeff[1], cx);
2695 s.x += coeff[2]; s.y += coeff[3];
2698 case BarrelDistortion:
2699 case BarrelInverseDistortion:
2701 double r,fx,fy,gx,gy;
2705 r = sqrt(d.x*d.x+d.y*d.y);
2706 if ( r > MagickEpsilon ) {
2707 fx = ((coeff[0]*r + coeff[1])*r + coeff[2])*r + coeff[3];
2708 fy = ((coeff[4]*r + coeff[5])*r + coeff[6])*r + coeff[7];
2709 gx = ((3*coeff[0]*r + 2*coeff[1])*r + coeff[2])/r;
2710 gy = ((3*coeff[4]*r + 2*coeff[5])*r + coeff[6])/r;
2712 if ( method == BarrelInverseDistortion ) {
2713 fx = 1/fx; fy = 1/fy;
2714 gx *= -fx*fx; gy *= -fy*fy;
2717 s.x = d.x*fx + coeff[8];
2718 s.y = d.y*fy + coeff[9];
2719 ScaleFilter( resample_filter[
id],
2720 gx*d.x*d.x + fx, gx*d.x*d.y,
2721 gy*d.x*d.y, gy*d.y*d.y + fy );
2730 if ( method == BarrelDistortion )
2731 ScaleFilter( resample_filter[
id],
2732 coeff[3], 0, 0, coeff[7] );
2735 ScaleFilter( resample_filter[
id],
2736 1.0/coeff[3], 0, 0, 1.0/coeff[7] );
2740 case ShepardsDistortion:
2755 denominator = s.x = s.y = 0;
2756 for(i=0; i<number_arguments; i+=4) {
2758 ((double)d.x-arguments[i+2])*((
double)d.x-arguments[i+2])
2759 + ((double)d.y-arguments[i+3])*((
double)d.y-arguments[i+3]);
2760 weight = pow(weight,coeff[0]);
2761 weight = ( weight < 1.0 ) ? 1.0 : 1.0/weight;
2763 s.x += (arguments[ i ]-arguments[i+2])*weight;
2764 s.y += (arguments[i+1]-arguments[i+3])*weight;
2765 denominator += weight;
2777 if ( bestfit && method != ArcDistortion ) {
2778 s.x -= image->page.x;
2779 s.y -= image->page.y;
2784 if ( validity <= 0.0 ) {
2786 SetPixelPacket(distort_image,&invalid,q,indexes);
2790 status=ResamplePixelColor(resample_filter[
id],s.x,s.y,&pixel);
2791 if (status == MagickFalse)
2792 SetPixelPacket(distort_image,&invalid,q,indexes);
2796 if ( validity < 1.0 ) {
2799 MagickPixelCompositeBlend(&pixel,validity,&invalid,(1.0-validity),
2803 SetPixelPacket(distort_image,&pixel,q,indexes);
2808 sync=SyncCacheViewAuthenticPixels(distort_view,exception);
2809 if (sync == MagickFalse)
2811 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2816#if defined(MAGICKCORE_OPENMP_SUPPORT)
2820 proceed=SetImageProgress(image,DistortImageTag,progress,image->rows);
2821 if (proceed == MagickFalse)
2825 distort_view=DestroyCacheView(distort_view);
2826 resample_filter=DestroyResampleFilterTLS(resample_filter);
2828 if (status == MagickFalse)
2829 distort_image=DestroyImage(distort_image);
2835 if ( method == ArcDistortion && !bestfit && !viewport_given ) {
2836 distort_image->page.x = 0;
2837 distort_image->page.y = 0;
2839 coeff=(
double *) RelinquishMagickMemory(coeff);
2840 return(distort_image);
2877MagickExport Image *RotateImage(
const Image *image,
const double degrees,
2878 ExceptionInfo *exception)
2896 assert(image != (Image *) NULL);
2897 assert(image->signature == MagickCoreSignature);
2898 assert(exception != (ExceptionInfo *) NULL);
2899 assert(exception->signature == MagickCoreSignature);
2900 if (IsEventLogging() != MagickFalse)
2901 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2902 angle=fmod(degrees,360.0);
2903 while (angle < -45.0)
2905 for (rotations=0; angle > 45.0; rotations++)
2908 shear.x=(-tan((
double) DegreesToRadians(angle)/2.0));
2909 shear.y=sin((
double) DegreesToRadians(angle));
2910 if ((fabs(shear.x) < MagickEpsilon) && (fabs(shear.y) < MagickEpsilon))
2911 return(IntegralRotateImage(image,rotations,exception));
2912 distort_image=CloneImage(image,0,0,MagickTrue,exception);
2913 if (distort_image == (Image *) NULL)
2914 return((Image *) NULL);
2915 (void) SetImageVirtualPixelMethod(distort_image,BackgroundVirtualPixelMethod);
2916 rotate_image=DistortImage(distort_image,ScaleRotateTranslateDistortion,1,
2917 °rees,MagickTrue,exception);
2918 distort_image=DestroyImage(distort_image);
2919 return(rotate_image);
2964MagickExport Image *SparseColorImage(
const Image *image,
2965 const ChannelType channel,
const SparseColorMethod method,
2966 const size_t number_arguments,
const double *arguments,
2967 ExceptionInfo *exception)
2969#define SparseColorTag "Distort/SparseColor"
2983 assert(image != (Image *) NULL);
2984 assert(image->signature == MagickCoreSignature);
2985 assert(exception != (ExceptionInfo *) NULL);
2986 assert(exception->signature == MagickCoreSignature);
2987 if (IsEventLogging() != MagickFalse)
2988 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2993 if ( channel & RedChannel ) number_colors++;
2994 if ( channel & GreenChannel ) number_colors++;
2995 if ( channel & BlueChannel ) number_colors++;
2996 if ( channel & IndexChannel ) number_colors++;
2997 if ( channel & OpacityChannel ) number_colors++;
3003 { DistortImageMethod
3006 distort_method=(DistortImageMethod) method;
3007 if ( distort_method >= SentinelDistortion )
3008 distort_method = ShepardsDistortion;
3009 coeff = GenerateCoefficients(image, &distort_method, number_arguments,
3010 arguments, number_colors, exception);
3011 if ( coeff == (
double *) NULL )
3012 return((Image *) NULL);
3019 sparse_method = (SparseColorMethod) distort_method;
3020 if ( distort_method == ShepardsDistortion )
3021 sparse_method = method;
3022 if ( sparse_method == InverseColorInterpolate )
3027 if ( GetImageArtifact(image,
"verbose") != (
const char *) NULL ) {
3029 switch (sparse_method) {
3030 case BarycentricColorInterpolate:
3033 (void) FormatLocaleFile(stderr,
"Barycentric Sparse Color:\n");
3034 if ( channel & RedChannel )
3035 (void) FormatLocaleFile(stderr,
" -channel R -fx '%+lf*i %+lf*j %+lf' \\\n",
3036 coeff[x], coeff[x+1], coeff[x+2]),x+=3;
3037 if ( channel & GreenChannel )
3038 (void) FormatLocaleFile(stderr,
" -channel G -fx '%+lf*i %+lf*j %+lf' \\\n",
3039 coeff[x], coeff[x+1], coeff[x+2]),x+=3;
3040 if ( channel & BlueChannel )
3041 (void) FormatLocaleFile(stderr,
" -channel B -fx '%+lf*i %+lf*j %+lf' \\\n",
3042 coeff[x], coeff[x+1], coeff[x+2]),x+=3;
3043 if ( channel & IndexChannel )
3044 (void) FormatLocaleFile(stderr,
" -channel K -fx '%+lf*i %+lf*j %+lf' \\\n",
3045 coeff[x], coeff[x+1], coeff[x+2]),x+=3;
3046 if ( channel & OpacityChannel )
3047 (void) FormatLocaleFile(stderr,
" -channel A -fx '%+lf*i %+lf*j %+lf' \\\n",
3048 coeff[x], coeff[x+1], coeff[x+2]),x+=3;
3051 case BilinearColorInterpolate:
3054 (void) FormatLocaleFile(stderr,
"Bilinear Sparse Color\n");
3055 if ( channel & RedChannel )
3056 (void) FormatLocaleFile(stderr,
" -channel R -fx '%+lf*i %+lf*j %+lf*i*j %+lf;\n",
3057 coeff[ x ], coeff[x+1],
3058 coeff[x+2], coeff[x+3]),x+=4;
3059 if ( channel & GreenChannel )
3060 (void) FormatLocaleFile(stderr,
" -channel G -fx '%+lf*i %+lf*j %+lf*i*j %+lf;\n",
3061 coeff[ x ], coeff[x+1],
3062 coeff[x+2], coeff[x+3]),x+=4;
3063 if ( channel & BlueChannel )
3064 (void) FormatLocaleFile(stderr,
" -channel B -fx '%+lf*i %+lf*j %+lf*i*j %+lf;\n",
3065 coeff[ x ], coeff[x+1],
3066 coeff[x+2], coeff[x+3]),x+=4;
3067 if ( channel & IndexChannel )
3068 (void) FormatLocaleFile(stderr,
" -channel K -fx '%+lf*i %+lf*j %+lf*i*j %+lf;\n",
3069 coeff[ x ], coeff[x+1],
3070 coeff[x+2], coeff[x+3]),x+=4;
3071 if ( channel & OpacityChannel )
3072 (void) FormatLocaleFile(stderr,
" -channel A -fx '%+lf*i %+lf*j %+lf*i*j %+lf;\n",
3073 coeff[ x ], coeff[x+1],
3074 coeff[x+2], coeff[x+3]),x+=4;
3089 sparse_image=CloneImage(image,0,0,MagickTrue,exception);
3090 if (sparse_image == (Image *) NULL)
3091 return((Image *) NULL);
3092 if (SetImageStorageClass(sparse_image,DirectClass) == MagickFalse)
3094 InheritException(exception,&image->exception);
3095 sparse_image=DestroyImage(sparse_image);
3096 return((Image *) NULL);
3113 sparse_view=AcquireAuthenticCacheView(sparse_image,exception);
3114#if defined(MAGICKCORE_OPENMP_SUPPORT)
3115 #pragma omp parallel for schedule(static) shared(progress,status) \
3116 magick_number_threads(image,sparse_image,sparse_image->rows,1)
3118 for (j=0; j < (ssize_t) sparse_image->rows; j++)
3127 *magick_restrict indexes;
3135 q=GetCacheViewAuthenticPixels(sparse_view,0,j,sparse_image->columns,
3137 if (q == (PixelPacket *) NULL)
3142 indexes=GetCacheViewAuthenticIndexQueue(sparse_view);
3143 GetMagickPixelPacket(sparse_image,&pixel);
3144 for (i=0; i < (ssize_t) image->columns; i++)
3146 SetMagickPixelPacket(image,q,indexes,&pixel);
3147 switch (sparse_method)
3149 case BarycentricColorInterpolate:
3152 if ( channel & RedChannel )
3153 pixel.red = coeff[x]*i +coeff[x+1]*j
3155 if ( channel & GreenChannel )
3156 pixel.green = coeff[x]*i +coeff[x+1]*j
3158 if ( channel & BlueChannel )
3159 pixel.blue = coeff[x]*i +coeff[x+1]*j
3161 if ( channel & IndexChannel )
3162 pixel.index = coeff[x]*i +coeff[x+1]*j
3164 if ( channel & OpacityChannel )
3165 pixel.opacity = coeff[x]*i +coeff[x+1]*j
3169 case BilinearColorInterpolate:
3172 if ( channel & RedChannel )
3173 pixel.red = coeff[x]*i + coeff[x+1]*j +
3174 coeff[x+2]*i*j + coeff[x+3], x+=4;
3175 if ( channel & GreenChannel )
3176 pixel.green = coeff[x]*i + coeff[x+1]*j +
3177 coeff[x+2]*i*j + coeff[x+3], x+=4;
3178 if ( channel & BlueChannel )
3179 pixel.blue = coeff[x]*i + coeff[x+1]*j +
3180 coeff[x+2]*i*j + coeff[x+3], x+=4;
3181 if ( channel & IndexChannel )
3182 pixel.index = coeff[x]*i + coeff[x+1]*j +
3183 coeff[x+2]*i*j + coeff[x+3], x+=4;
3184 if ( channel & OpacityChannel )
3185 pixel.opacity = coeff[x]*i + coeff[x+1]*j +
3186 coeff[x+2]*i*j + coeff[x+3], x+=4;
3189 case InverseColorInterpolate:
3190 case ShepardsColorInterpolate:
3197 if ( channel & RedChannel ) pixel.red = 0.0;
3198 if ( channel & GreenChannel ) pixel.green = 0.0;
3199 if ( channel & BlueChannel ) pixel.blue = 0.0;
3200 if ( channel & IndexChannel ) pixel.index = 0.0;
3201 if ( channel & OpacityChannel ) pixel.opacity = 0.0;
3203 for(k=0; k<number_arguments; k+=2+number_colors) {
3204 ssize_t x=(ssize_t) k+2;
3206 ((double)i-arguments[ k ])*((
double)i-arguments[ k ])
3207 + ((double)j-arguments[k+1])*((
double)j-arguments[k+1]);
3208 weight = pow(weight,coeff[0]);
3209 weight = ( weight < 1.0 ) ? 1.0 : 1.0/weight;
3210 if ( channel & RedChannel )
3211 pixel.red += arguments[x++]*weight;
3212 if ( channel & GreenChannel )
3213 pixel.green += arguments[x++]*weight;
3214 if ( channel & BlueChannel )
3215 pixel.blue += arguments[x++]*weight;
3216 if ( channel & IndexChannel )
3217 pixel.index += arguments[x++]*weight;
3218 if ( channel & OpacityChannel )
3219 pixel.opacity += arguments[x++]*weight;
3220 denominator += weight;
3222 if ( channel & RedChannel ) pixel.red /= denominator;
3223 if ( channel & GreenChannel ) pixel.green /= denominator;
3224 if ( channel & BlueChannel ) pixel.blue /= denominator;
3225 if ( channel & IndexChannel ) pixel.index /= denominator;
3226 if ( channel & OpacityChannel ) pixel.opacity /= denominator;
3229 case ManhattanColorInterpolate:
3235 minimum = MagickMaximumValue;
3240 for(k=0; k<number_arguments; k+=2+number_colors) {
3242 fabs((
double)i-arguments[ k ])
3243 + fabs((
double)j-arguments[k+1]);
3244 if ( distance < minimum ) {
3245 ssize_t x=(ssize_t) k+2;
3246 if ( channel & RedChannel ) pixel.red = arguments[x++];
3247 if ( channel & GreenChannel ) pixel.green = arguments[x++];
3248 if ( channel & BlueChannel ) pixel.blue = arguments[x++];
3249 if ( channel & IndexChannel ) pixel.index = arguments[x++];
3250 if ( channel & OpacityChannel ) pixel.opacity = arguments[x++];
3256 case VoronoiColorInterpolate:
3263 minimum = MagickMaximumValue;
3268 for(k=0; k<number_arguments; k+=2+number_colors) {
3270 ((double)i-arguments[ k ])*((
double)i-arguments[ k ])
3271 + ((double)j-arguments[k+1])*((
double)j-arguments[k+1]);
3272 if ( distance < minimum ) {
3273 ssize_t x=(ssize_t) k+2;
3274 if ( channel & RedChannel ) pixel.red = arguments[x++];
3275 if ( channel & GreenChannel ) pixel.green = arguments[x++];
3276 if ( channel & BlueChannel ) pixel.blue = arguments[x++];
3277 if ( channel & IndexChannel ) pixel.index = arguments[x++];
3278 if ( channel & OpacityChannel ) pixel.opacity = arguments[x++];
3286 if ( channel & RedChannel )
3287 pixel.red=ClampPixel((MagickRealType) QuantumRange*pixel.red);
3288 if ( channel & GreenChannel )
3289 pixel.green=ClampPixel((MagickRealType) QuantumRange*pixel.green);
3290 if ( channel & BlueChannel )
3291 pixel.blue=ClampPixel((MagickRealType) QuantumRange*pixel.blue);
3292 if ( channel & IndexChannel )
3293 pixel.index=ClampPixel((MagickRealType) QuantumRange*pixel.index);
3294 if ( channel & OpacityChannel )
3295 pixel.opacity=ClampPixel((MagickRealType) QuantumRange*pixel.opacity);
3296 SetPixelPacket(sparse_image,&pixel,q,indexes);
3300 sync=SyncCacheViewAuthenticPixels(sparse_view,exception);
3301 if (sync == MagickFalse)
3303 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3308#if defined(MAGICKCORE_OPENMP_SUPPORT)
3312 proceed=SetImageProgress(image,SparseColorTag,progress,image->rows);
3313 if (proceed == MagickFalse)
3317 sparse_view=DestroyCacheView(sparse_view);
3318 if (status == MagickFalse)
3319 sparse_image=DestroyImage(sparse_image);
3321 coeff = (
double *) RelinquishMagickMemory(coeff);
3322 return(sparse_image);