43#include "MagickCore/studio.h"
44#include "MagickCore/artifact.h"
45#include "MagickCore/cache.h"
46#include "MagickCore/cache-view.h"
47#include "MagickCore/channel.h"
48#include "MagickCore/colorspace-private.h"
49#include "MagickCore/composite-private.h"
50#include "MagickCore/distort.h"
51#include "MagickCore/exception.h"
52#include "MagickCore/exception-private.h"
53#include "MagickCore/gem.h"
54#include "MagickCore/image.h"
55#include "MagickCore/linked-list.h"
56#include "MagickCore/list.h"
57#include "MagickCore/matrix.h"
58#include "MagickCore/matrix-private.h"
59#include "MagickCore/memory_.h"
60#include "MagickCore/monitor-private.h"
61#include "MagickCore/option.h"
62#include "MagickCore/pixel.h"
63#include "MagickCore/pixel-accessor.h"
64#include "MagickCore/resample.h"
65#include "MagickCore/resample-private.h"
66#include "MagickCore/registry.h"
67#include "MagickCore/resource_.h"
68#include "MagickCore/semaphore.h"
69#include "MagickCore/shear.h"
70#include "MagickCore/string_.h"
71#include "MagickCore/string-private.h"
72#include "MagickCore/thread-private.h"
73#include "MagickCore/token.h"
74#include "MagickCore/transform.h"
79static inline void AffineArgsToCoefficients(
double *affine)
83 tmp[0]=affine[1]; tmp[1]=affine[2]; tmp[2]=affine[3]; tmp[3]=affine[4];
84 affine[3]=tmp[0]; affine[1]=tmp[1]; affine[4]=tmp[2]; affine[2]=tmp[3];
87static inline void CoefficientsToAffineArgs(
double *coeff)
91 tmp[0]=coeff[3]; tmp[1]=coeff[1]; tmp[2]=coeff[4]; tmp[3]=coeff[2];
92 coeff[1]=tmp[0]; coeff[2]=tmp[1]; coeff[3]=tmp[2]; coeff[4]=tmp[3];
94static void InvertAffineCoefficients(
const double *coeff,
double *inverse)
99 determinant=PerceptibleReciprocal(coeff[0]*coeff[4]-coeff[1]*coeff[3]);
100 inverse[0]=determinant*coeff[4];
101 inverse[1]=determinant*(-coeff[1]);
102 inverse[2]=determinant*(coeff[1]*coeff[5]-coeff[2]*coeff[4]);
103 inverse[3]=determinant*(-coeff[3]);
104 inverse[4]=determinant*coeff[0];
105 inverse[5]=determinant*(coeff[2]*coeff[3]-coeff[0]*coeff[5]);
108static void InvertPerspectiveCoefficients(
const double *coeff,
114 determinant=PerceptibleReciprocal(coeff[0]*coeff[4]-coeff[3]*coeff[1]);
115 inverse[0]=determinant*(coeff[4]-coeff[7]*coeff[5]);
116 inverse[1]=determinant*(coeff[7]*coeff[2]-coeff[1]);
117 inverse[2]=determinant*(coeff[1]*coeff[5]-coeff[4]*coeff[2]);
118 inverse[3]=determinant*(coeff[6]*coeff[5]-coeff[3]);
119 inverse[4]=determinant*(coeff[0]-coeff[6]*coeff[2]);
120 inverse[5]=determinant*(coeff[3]*coeff[2]-coeff[0]*coeff[5]);
121 inverse[6]=determinant*(coeff[3]*coeff[7]-coeff[6]*coeff[4]);
122 inverse[7]=determinant*(coeff[6]*coeff[1]-coeff[0]*coeff[7]);
144static size_t poly_number_terms(
double order)
147 if ( order < 1 || order > 5 ||
148 ( order != floor(order) && (order-1.5) > MagickEpsilon) )
150 return((
size_t) floor((order+1)*(order+2)/2));
153static double poly_basis_fn(ssize_t n,
double x,
double y)
157 case 0:
return( 1.0 );
160 case 3:
return( x*y );
161 case 4:
return( x*x );
162 case 5:
return( y*y );
163 case 6:
return( x*x*x );
164 case 7:
return( x*x*y );
165 case 8:
return( x*y*y );
166 case 9:
return( y*y*y );
167 case 10:
return( x*x*x*x );
168 case 11:
return( x*x*x*y );
169 case 12:
return( x*x*y*y );
170 case 13:
return( x*y*y*y );
171 case 14:
return( y*y*y*y );
172 case 15:
return( x*x*x*x*x );
173 case 16:
return( x*x*x*x*y );
174 case 17:
return( x*x*x*y*y );
175 case 18:
return( x*x*y*y*y );
176 case 19:
return( x*y*y*y*y );
177 case 20:
return( y*y*y*y*y );
181static const char *poly_basis_str(ssize_t n)
186 case 1:
return(
"*ii");
187 case 2:
return(
"*jj");
188 case 3:
return(
"*ii*jj");
189 case 4:
return(
"*ii*ii");
190 case 5:
return(
"*jj*jj");
191 case 6:
return(
"*ii*ii*ii");
192 case 7:
return(
"*ii*ii*jj");
193 case 8:
return(
"*ii*jj*jj");
194 case 9:
return(
"*jj*jj*jj");
195 case 10:
return(
"*ii*ii*ii*ii");
196 case 11:
return(
"*ii*ii*ii*jj");
197 case 12:
return(
"*ii*ii*jj*jj");
198 case 13:
return(
"*ii*jj*jj*jj");
199 case 14:
return(
"*jj*jj*jj*jj");
200 case 15:
return(
"*ii*ii*ii*ii*ii");
201 case 16:
return(
"*ii*ii*ii*ii*jj");
202 case 17:
return(
"*ii*ii*ii*jj*jj");
203 case 18:
return(
"*ii*ii*jj*jj*jj");
204 case 19:
return(
"*ii*jj*jj*jj*jj");
205 case 20:
return(
"*jj*jj*jj*jj*jj");
209static double poly_basis_dx(ssize_t n,
double x,
double y)
213 case 0:
return( 0.0 );
214 case 1:
return( 1.0 );
215 case 2:
return( 0.0 );
218 case 5:
return( 0.0 );
219 case 6:
return( x*x );
220 case 7:
return( x*y );
221 case 8:
return( y*y );
222 case 9:
return( 0.0 );
223 case 10:
return( x*x*x );
224 case 11:
return( x*x*y );
225 case 12:
return( x*y*y );
226 case 13:
return( y*y*y );
227 case 14:
return( 0.0 );
228 case 15:
return( x*x*x*x );
229 case 16:
return( x*x*x*y );
230 case 17:
return( x*x*y*y );
231 case 18:
return( x*y*y*y );
232 case 19:
return( y*y*y*y );
233 case 20:
return( 0.0 );
237static double poly_basis_dy(ssize_t n,
double x,
double y)
241 case 0:
return( 0.0 );
242 case 1:
return( 0.0 );
243 case 2:
return( 1.0 );
245 case 4:
return( 0.0 );
247 default:
return( poly_basis_dx(n-1,x,y) );
283MagickExport
Image *AffineTransformImage(
const Image *image,
295 assert(image->signature == MagickCoreSignature);
298 assert(exception->signature == MagickCoreSignature);
299 if (IsEventLogging() != MagickFalse)
300 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
301 distort[0]=affine_matrix->sx;
302 distort[1]=affine_matrix->rx;
303 distort[2]=affine_matrix->ry;
304 distort[3]=affine_matrix->sy;
305 distort[4]=affine_matrix->tx;
306 distort[5]=affine_matrix->ty;
307 deskew_image=DistortImage(image,AffineProjectionDistortion,6,distort,
308 MagickTrue,exception);
309 return(deskew_image);
362static inline double MagickRound(
double x)
367 if ((x-floor(x)) < (ceil(x)-x))
372static double *GenerateCoefficients(
const Image *image,
373 DistortMethod *method,
const size_t number_arguments,
const double *arguments,
389 if ( number_values == 0 ) {
405 cp_size = number_values+2;
410 if ( number_arguments < 4*cp_size &&
411 ( *method == BilinearForwardDistortion
412 || *method == BilinearReverseDistortion
413 || *method == PerspectiveDistortion
415 *method = AffineDistortion;
417 number_coefficients=0;
419 case AffineDistortion:
420 case RigidAffineDistortion:
422 number_coefficients=3*number_values;
424 case PolynomialDistortion:
426 i = poly_number_terms(arguments[0]);
427 number_coefficients = 2 + i*number_values;
429 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
430 "InvalidArgument",
"%s : '%s'",
"Polynomial",
431 "Invalid order, should be integer 1 to 5, or 1.5");
432 return((
double *) NULL);
434 if ( number_arguments < 1+i*cp_size ) {
435 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
436 "InvalidArgument",
"%s : 'require at least %.20g CPs'",
437 "Polynomial", (
double) i);
438 return((
double *) NULL);
441 case BilinearReverseDistortion:
442 number_coefficients=4*number_values;
447 case BilinearForwardDistortion:
448 number_coefficients=10;
454 case QuadrilateralDistortion:
455 number_coefficients=19;
458 case ShepardsDistortion:
459 number_coefficients=1;
462 number_coefficients=5;
464 case ScaleRotateTranslateDistortion:
465 case AffineProjectionDistortion:
466 case Plane2CylinderDistortion:
467 case Cylinder2PlaneDistortion:
468 number_coefficients=6;
470 case PolarDistortion:
471 case DePolarDistortion:
472 number_coefficients=8;
474 case PerspectiveDistortion:
475 case PerspectiveProjectionDistortion:
476 number_coefficients=9;
478 case BarrelDistortion:
479 case BarrelInverseDistortion:
480 number_coefficients=10;
483 perror(
"unknown method given");
487 coeff=(
double *) AcquireQuantumMemory(number_coefficients,
sizeof(*coeff));
488 if (coeff == (
double *) NULL)
490 (void) ThrowMagickException(exception,GetMagickModule(),
491 ResourceLimitError,
"MemoryAllocationFailed",
"%s",
492 "GenerateCoefficients");
493 return((
double *) NULL);
497 for (i=0; i < number_coefficients; i++)
502 case AffineDistortion:
512 if ( number_arguments%cp_size != 0 ||
513 number_arguments < cp_size ) {
514 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
515 "InvalidArgument",
"%s : 'require at least %.20g CPs'",
517 coeff=(
double *) RelinquishMagickMemory(coeff);
518 return((
double *) NULL);
521 if ( number_arguments == cp_size ) {
523 if ( cp_values == 0 ) {
526 coeff[2] = arguments[0] - arguments[2];
528 coeff[5] = arguments[1] - arguments[3];
532 for (i=0; i<number_values; i++)
533 coeff[i*3+2] = arguments[cp_values+i];
549 matrix=AcquireMagickMatrix(3UL,3UL);
550 vectors=(
double **) AcquireQuantumMemory(number_values,
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 RigidAffineDistortion:
627 if (((number_arguments % cp_size) != 0) || (number_arguments < cp_size))
629 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
630 "InvalidArgument",
"%s : 'require at least %.20g CPs'",
631 CommandOptionToMnemonic(MagickDistortOptions,*method),2.0);
632 coeff=(
double *) RelinquishMagickMemory(coeff);
633 return((
double *) NULL);
638 matrix=AcquireMagickMatrix(4UL,4UL);
639 if (matrix == (
double **) NULL)
641 coeff=(
double *) RelinquishMagickMemory(coeff);
642 (void) ThrowMagickException(exception,GetMagickModule(),
643 ResourceLimitError,
"MemoryAllocationFailed",
"%s",
644 CommandOptionToMnemonic(MagickDistortOptions,*method));
645 return((
double *) NULL);
650 vectors[0]=(&(coeff[0]));
651 for (i=0; i < number_arguments; i+=4)
653 terms[0]=arguments[i+0];
654 terms[1]=(-arguments[i+1]);
657 LeastSquaresAddTerms(matrix,vectors,terms,&(arguments[i+2]),4UL,1UL);
658 terms[0]=arguments[i+1];
659 terms[1]=arguments[i+0];
662 LeastSquaresAddTerms(matrix,vectors,terms,&(arguments[i+3]),4UL,1UL);
667 status=GaussJordanElimination(matrix,vectors,4UL,1UL);
668 matrix=RelinquishMagickMatrix(matrix,4UL);
669 if (status == MagickFalse)
671 coeff=(
double *) RelinquishMagickMemory(coeff);
672 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
673 "InvalidArgument",
"%s : 'Unsolvable Matrix'",
674 CommandOptionToMnemonic(MagickDistortOptions,*method));
675 return((
double *) NULL);
682 inverse[2]=(-coeff[1]);
686 AffineArgsToCoefficients(inverse);
687 InvertAffineCoefficients(inverse,coeff);
688 *method=AffineDistortion;
691 case AffineProjectionDistortion:
707 if (number_arguments != 6) {
708 coeff = (
double *) RelinquishMagickMemory(coeff);
709 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
710 "InvalidArgument",
"%s : 'Needs 6 coeff values'",
711 CommandOptionToMnemonic(MagickDistortOptions, *method) );
712 return((
double *) NULL);
715 for(i=0; i<6UL; i++ )
716 inverse[i] = arguments[i];
717 AffineArgsToCoefficients(inverse);
718 InvertAffineCoefficients(inverse, coeff);
719 *method = AffineDistortion;
723 case ScaleRotateTranslateDistortion:
752 x = nx = (double)(image->columns)/2.0 + (double)image->page.x;
753 y = ny = (double)(image->rows)/2.0 + (double)image->page.y;
755 switch ( number_arguments ) {
757 coeff = (
double *) RelinquishMagickMemory(coeff);
758 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
759 "InvalidArgument",
"%s : 'Needs at least 1 argument'",
760 CommandOptionToMnemonic(MagickDistortOptions, *method) );
761 return((
double *) NULL);
766 sx = sy = arguments[0];
770 x = nx = arguments[0];
771 y = ny = arguments[1];
772 switch ( number_arguments ) {
777 sx = sy = arguments[2];
786 sx = sy = arguments[2];
799 coeff = (
double *) RelinquishMagickMemory(coeff);
800 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
801 "InvalidArgument",
"%s : 'Too Many Arguments (7 or less)'",
802 CommandOptionToMnemonic(MagickDistortOptions, *method) );
803 return((
double *) NULL);
808 if ( fabs(sx) < MagickEpsilon || fabs(sy) < MagickEpsilon ) {
809 coeff = (
double *) RelinquishMagickMemory(coeff);
810 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
811 "InvalidArgument",
"%s : 'Zero Scale Given'",
812 CommandOptionToMnemonic(MagickDistortOptions, *method) );
813 return((
double *) NULL);
816 a=DegreesToRadians(a); cosine=cos(a); sine=sin(a);
818 *method = AffineDistortion;
821 coeff[2]=x-nx*coeff[0]-ny*coeff[1];
824 coeff[5]=y-nx*coeff[3]-ny*coeff[4];
827 case PerspectiveDistortion:
870 if ( number_arguments%cp_size != 0 ||
871 number_arguments < cp_size*4 ) {
872 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
873 "InvalidArgument",
"%s : 'require at least %.20g CPs'",
874 CommandOptionToMnemonic(MagickDistortOptions, *method), 4.0);
875 coeff=(
double *) RelinquishMagickMemory(coeff);
876 return((
double *) NULL);
879 vectors[0] = &(coeff[0]);
881 matrix = AcquireMagickMatrix(8UL,8UL);
882 if (matrix == (
double **) NULL) {
883 coeff=(
double *) RelinquishMagickMemory(coeff);
884 (void) ThrowMagickException(exception,GetMagickModule(),
885 ResourceLimitError,
"MemoryAllocationFailed",
886 "%s",
"DistortCoefficients");
887 return((
double *) NULL);
890 for (i=0; i < number_arguments; i+=4) {
891 terms[0]=arguments[i+cp_x];
892 terms[1]=arguments[i+cp_y];
897 terms[6]=-terms[0]*arguments[i+cp_u];
898 terms[7]=-terms[1]*arguments[i+cp_u];
899 LeastSquaresAddTerms(matrix,vectors,terms,&(arguments[i+cp_u]),
905 terms[3]=arguments[i+cp_x];
906 terms[4]=arguments[i+cp_y];
908 terms[6]=-terms[3]*arguments[i+cp_v];
909 terms[7]=-terms[4]*arguments[i+cp_v];
910 LeastSquaresAddTerms(matrix,vectors,terms,&(arguments[i+cp_v]),
914 status=GaussJordanElimination(matrix,vectors,8UL,1UL);
915 matrix = RelinquishMagickMatrix(matrix, 8UL);
916 if ( status == MagickFalse ) {
917 coeff = (
double *) RelinquishMagickMemory(coeff);
918 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
919 "InvalidArgument",
"%s : 'Unsolvable Matrix'",
920 CommandOptionToMnemonic(MagickDistortOptions, *method) );
921 return((
double *) NULL);
929 coeff[8] = coeff[6]*arguments[cp_x]
930 + coeff[7]*arguments[cp_y] + 1.0;
931 coeff[8] = (coeff[8] < 0.0) ? -1.0 : +1.0;
935 case PerspectiveProjectionDistortion:
940 if (number_arguments != 8) {
941 coeff = (
double *) RelinquishMagickMemory(coeff);
942 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
943 "InvalidArgument",
"%s : 'Needs 8 coefficient values'",
944 CommandOptionToMnemonic(MagickDistortOptions, *method));
945 return((
double *) NULL);
948 InvertPerspectiveCoefficients(arguments, coeff);
956 coeff[8] = coeff[6]*arguments[2]
957 + coeff[7]*arguments[5] + 1.0;
958 coeff[8] = (coeff[8] < 0.0) ? -1.0 : +1.0;
959 *method = PerspectiveDistortion;
963 case BilinearForwardDistortion:
964 case BilinearReverseDistortion:
988 if ( number_arguments%cp_size != 0 ||
989 number_arguments < cp_size*4 ) {
990 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
991 "InvalidArgument",
"%s : 'require at least %.20g CPs'",
992 CommandOptionToMnemonic(MagickDistortOptions, *method), 4.0);
993 coeff=(
double *) RelinquishMagickMemory(coeff);
994 return((
double *) NULL);
997 matrix=AcquireMagickMatrix(4UL,4UL);
998 vectors=(
double **) AcquireQuantumMemory(number_values,
sizeof(*vectors));
999 if (matrix == (
double **) NULL || vectors == (
double **) NULL)
1001 matrix = RelinquishMagickMatrix(matrix, 4UL);
1002 vectors = (
double **) RelinquishMagickMemory(vectors);
1003 coeff = (
double *) RelinquishMagickMemory(coeff);
1004 (void) ThrowMagickException(exception,GetMagickModule(),
1005 ResourceLimitError,
"MemoryAllocationFailed",
1006 "%s",
"DistortCoefficients");
1007 return((
double *) NULL);
1010 for (i=0; i < number_values; i++)
1011 vectors[i] = &(coeff[i*4]);
1013 for (i=0; i < number_arguments; i+=cp_size) {
1014 terms[0] = arguments[i+cp_x];
1015 terms[1] = arguments[i+cp_y];
1016 terms[2] = terms[0]*terms[1];
1018 LeastSquaresAddTerms(matrix,vectors,terms,
1019 &(arguments[i+cp_values]),4UL,number_values);
1022 status=GaussJordanElimination(matrix,vectors,4UL,number_values);
1023 matrix = RelinquishMagickMatrix(matrix, 4UL);
1024 vectors = (
double **) RelinquishMagickMemory(vectors);
1025 if ( status == MagickFalse ) {
1026 coeff = (
double *) RelinquishMagickMemory(coeff);
1027 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1028 "InvalidArgument",
"%s : 'Unsolvable Matrix'",
1029 CommandOptionToMnemonic(MagickDistortOptions, *method) );
1030 return((
double *) NULL);
1032 if ( *method == BilinearForwardDistortion ) {
1072 coeff[8] = coeff[0]*coeff[5] - coeff[1]*coeff[4];
1073 coeff[9] = 2*(coeff[2]*coeff[5] - coeff[1]*coeff[6]);
1078 case QuadrilateralDistortion:
1094 case PolynomialDistortion:
1132 coeff[0] = arguments[0];
1133 coeff[1] = (double) poly_number_terms(arguments[0]);
1134 nterms = (size_t) coeff[1];
1137 matrix=AcquireMagickMatrix(nterms,nterms);
1138 vectors=(
double **) AcquireQuantumMemory(number_values,
1140 terms=(
double *) AcquireQuantumMemory(nterms,
sizeof(*terms));
1141 if ((matrix == (
double **) NULL) || (vectors == (
double **) NULL) ||
1142 (terms == (
double *) NULL))
1144 matrix = RelinquishMagickMatrix(matrix, nterms);
1145 vectors = (
double **) RelinquishMagickMemory(vectors);
1146 terms = (
double *) RelinquishMagickMemory(terms);
1147 coeff = (
double *) RelinquishMagickMemory(coeff);
1148 (void) ThrowMagickException(exception,GetMagickModule(),
1149 ResourceLimitError,
"MemoryAllocationFailed",
1150 "%s",
"DistortCoefficients");
1151 return((
double *) NULL);
1154 for (i=0; i < number_values; i++)
1155 vectors[i] = &(coeff[2+i*nterms]);
1157 for (i=1; i < number_arguments; i+=cp_size) {
1158 for (j=0; j < (ssize_t) nterms; j++)
1159 terms[j] = poly_basis_fn(j,arguments[i+cp_x],arguments[i+cp_y]);
1160 LeastSquaresAddTerms(matrix,vectors,terms,
1161 &(arguments[i+cp_values]),nterms,number_values);
1163 terms = (
double *) RelinquishMagickMemory(terms);
1165 status=GaussJordanElimination(matrix,vectors,nterms,number_values);
1166 matrix = RelinquishMagickMatrix(matrix, nterms);
1167 vectors = (
double **) RelinquishMagickMemory(vectors);
1168 if ( status == MagickFalse ) {
1169 coeff = (
double *) RelinquishMagickMemory(coeff);
1170 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1171 "InvalidArgument",
"%s : 'Unsolvable Matrix'",
1172 CommandOptionToMnemonic(MagickDistortOptions, *method) );
1173 return((
double *) NULL);
1213 if ( number_arguments >= 1 && arguments[0] < MagickEpsilon ) {
1214 coeff = (
double *) RelinquishMagickMemory(coeff);
1215 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1216 "InvalidArgument",
"%s : 'Arc Angle Too Small'",
1217 CommandOptionToMnemonic(MagickDistortOptions, *method) );
1218 return((
double *) NULL);
1220 if ( number_arguments >= 3 && arguments[2] < MagickEpsilon ) {
1221 coeff = (
double *) RelinquishMagickMemory(coeff);
1222 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1223 "InvalidArgument",
"%s : 'Outer Radius Too Small'",
1224 CommandOptionToMnemonic(MagickDistortOptions, *method) );
1225 return((
double *) NULL);
1227 coeff[0] = -MagickPI2;
1228 if ( number_arguments >= 1 )
1229 coeff[1] = DegreesToRadians(arguments[0]);
1231 coeff[1] = MagickPI2;
1232 if ( number_arguments >= 2 )
1233 coeff[0] += DegreesToRadians(arguments[1]);
1234 coeff[0] /= Magick2PI;
1235 coeff[0] -= MagickRound(coeff[0]);
1236 coeff[0] *= Magick2PI;
1237 coeff[3] = (double)image->rows-1;
1238 coeff[2] = (double)image->columns/coeff[1] + coeff[3]/2.0;
1239 if ( number_arguments >= 3 ) {
1240 if ( number_arguments >= 4 )
1241 coeff[3] = arguments[2] - arguments[3];
1243 coeff[3] *= arguments[2]/coeff[2];
1244 coeff[2] = arguments[2];
1246 coeff[4] = ((double)image->columns-1.0)/2.0;
1250 case PolarDistortion:
1251 case DePolarDistortion:
1263 if ( number_arguments == 3
1264 || ( number_arguments > 6 && *method == PolarDistortion )
1265 || number_arguments > 8 ) {
1266 (void) ThrowMagickException(exception,GetMagickModule(),
1267 OptionError,
"InvalidArgument",
"%s : number of arguments",
1268 CommandOptionToMnemonic(MagickDistortOptions, *method) );
1269 coeff=(
double *) RelinquishMagickMemory(coeff);
1270 return((
double *) NULL);
1273 if ( number_arguments >= 1 )
1274 coeff[0] = arguments[0];
1278 coeff[1] = number_arguments >= 2 ? arguments[1] : 0.0;
1280 if ( number_arguments >= 4 ) {
1281 coeff[2] = arguments[2];
1282 coeff[3] = arguments[3];
1285 coeff[2] = (double)(image->columns)/2.0+image->page.x;
1286 coeff[3] = (double)(image->rows)/2.0+image->page.y;
1289 coeff[4] = -MagickPI;
1290 if ( number_arguments >= 5 )
1291 coeff[4] = DegreesToRadians(arguments[4]);
1292 coeff[5] = coeff[4];
1293 if ( number_arguments >= 6 )
1294 coeff[5] = DegreesToRadians(arguments[5]);
1295 if ( fabs(coeff[4]-coeff[5]) < MagickEpsilon )
1296 coeff[5] += Magick2PI;
1298 if ( coeff[0] < MagickEpsilon ) {
1300 if ( fabs(coeff[0]) < MagickEpsilon ) {
1301 coeff[0]=MagickMin(fabs(coeff[2]-image->page.x),
1302 fabs(coeff[3]-image->page.y));
1303 coeff[0]=MagickMin(coeff[0],
1304 fabs(coeff[2]-image->page.x-image->columns));
1305 coeff[0]=MagickMin(coeff[0],
1306 fabs(coeff[3]-image->page.y-image->rows));
1309 if ( fabs(-1.0-coeff[0]) < MagickEpsilon ) {
1311 rx = coeff[2]-image->page.x;
1312 ry = coeff[3]-image->page.y;
1313 coeff[0] = rx*rx+ry*ry;
1314 ry = coeff[3]-image->page.y-image->rows;
1315 coeff[0] = MagickMax(coeff[0],rx*rx+ry*ry);
1316 rx = coeff[2]-image->page.x-image->columns;
1317 coeff[0] = MagickMax(coeff[0],rx*rx+ry*ry);
1318 ry = coeff[3]-image->page.y;
1319 coeff[0] = MagickMax(coeff[0],rx*rx+ry*ry);
1320 coeff[0] = sqrt(coeff[0]);
1324 if ( coeff[0] < MagickEpsilon || coeff[1] < -MagickEpsilon
1325 || (coeff[0]-coeff[1]) < MagickEpsilon ) {
1326 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1327 "InvalidArgument",
"%s : Invalid Radius",
1328 CommandOptionToMnemonic(MagickDistortOptions, *method) );
1329 coeff=(
double *) RelinquishMagickMemory(coeff);
1330 return((
double *) NULL);
1333 if ( *method == PolarDistortion ) {
1334 coeff[6]=(double) image->columns/(coeff[5]-coeff[4]);
1335 coeff[7]=(double) image->rows/(coeff[0]-coeff[1]);
1338 coeff[6]=(coeff[5]-coeff[4])/image->columns;
1339 coeff[7]=(coeff[0]-coeff[1])/image->rows;
1343 case Cylinder2PlaneDistortion:
1344 case Plane2CylinderDistortion:
1368 if ( arguments[0] < MagickEpsilon || arguments[0] > 160.0 ) {
1369 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1370 "InvalidArgument",
"%s : Invalid FOV Angle",
1371 CommandOptionToMnemonic(MagickDistortOptions, *method) );
1372 coeff=(
double *) RelinquishMagickMemory(coeff);
1373 return((
double *) NULL);
1375 coeff[0] = DegreesToRadians(arguments[0]);
1376 if ( *method == Cylinder2PlaneDistortion )
1380 coeff[1] = (double) image->columns/coeff[0];
1383 coeff[1] = (double) image->columns / ( 2 * tan(coeff[0]/2) );
1385 coeff[2] = (double)(image->columns)/2.0+image->page.x;
1386 coeff[3] = (double)(image->rows)/2.0+image->page.y;
1387 coeff[4] = coeff[2];
1388 coeff[5] = coeff[3];
1391 case BarrelDistortion:
1392 case BarrelInverseDistortion:
1413 rscale = 2.0/MagickMin((
double) image->columns,(
double) image->rows);
1416 if ( (number_arguments < 3) || (number_arguments == 7) ||
1417 (number_arguments == 9) || (number_arguments > 10) )
1419 coeff=(
double *) RelinquishMagickMemory(coeff);
1420 (void) ThrowMagickException(exception,GetMagickModule(),
1421 OptionError,
"InvalidArgument",
"%s : number of arguments",
1422 CommandOptionToMnemonic(MagickDistortOptions, *method) );
1423 return((
double *) NULL);
1426 coeff[0] = arguments[0];
1427 coeff[1] = arguments[1];
1428 coeff[2] = arguments[2];
1429 if ((number_arguments == 3) || (number_arguments == 5) )
1430 coeff[3] = 1.0 - coeff[0] - coeff[1] - coeff[2];
1432 coeff[3] = arguments[3];
1434 coeff[0] *= pow(rscale,3.0);
1435 coeff[1] *= rscale*rscale;
1438 if ( number_arguments >= 8 ) {
1439 coeff[4] = arguments[4] * pow(rscale,3.0);
1440 coeff[5] = arguments[5] * rscale*rscale;
1441 coeff[6] = arguments[6] * rscale;
1442 coeff[7] = arguments[7];
1445 coeff[4] = coeff[0];
1446 coeff[5] = coeff[1];
1447 coeff[6] = coeff[2];
1448 coeff[7] = coeff[3];
1451 if ( number_arguments == 5 ) {
1452 coeff[8] = arguments[3];
1453 coeff[9] = arguments[4];
1455 else if ( number_arguments == 6 ) {
1456 coeff[8] = arguments[4];
1457 coeff[9] = arguments[5];
1459 else if ( number_arguments == 10 ) {
1460 coeff[8] = arguments[8];
1461 coeff[9] = arguments[9];
1465 coeff[8] = (double)image->columns/2.0 + image->page.x;
1466 coeff[9] = (double)image->rows/2.0 + image->page.y;
1470 case ShepardsDistortion:
1477 if ( number_arguments%cp_size != 0 ||
1478 number_arguments < cp_size ) {
1479 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1480 "InvalidArgument",
"%s : 'requires CP's (4 numbers each)'",
1481 CommandOptionToMnemonic(MagickDistortOptions, *method));
1482 coeff=(
double *) RelinquishMagickMemory(coeff);
1483 return((
double *) NULL);
1486 {
const char *artifact=GetImageArtifact(image,
"shepards:power");
1487 if ( artifact != (
const char *) NULL ) {
1488 coeff[0]=StringToDouble(artifact,(
char **) NULL) / 2.0;
1489 if ( coeff[0] < MagickEpsilon ) {
1490 (void) ThrowMagickException(exception,GetMagickModule(),
1491 OptionError,
"InvalidArgument",
"%s",
"-define shepards:power" );
1492 coeff=(
double *) RelinquishMagickMemory(coeff);
1493 return((
double *) NULL);
1505 perror(
"no method handler");
1506 return((
double *) NULL);
1544MagickExport
Image *DistortResizeImage(
const Image *image,
const size_t columns,
1547#define DistortResizeImageTag "Distort/Image"
1565 assert(image != (
const Image *) NULL);
1566 assert(image->signature == MagickCoreSignature);
1568 assert(exception->signature == MagickCoreSignature);
1569 if (IsEventLogging() != MagickFalse)
1570 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1571 if ((columns == 0) || (rows == 0))
1572 return((
Image *) NULL);
1575 (void) memset(distort_args,0,
sizeof(distort_args));
1576 distort_args[4]=(double) image->columns;
1577 distort_args[6]=(double) columns;
1578 distort_args[9]=(double) image->rows;
1579 distort_args[11]=(double) rows;
1581 vp_save=GetImageVirtualPixelMethod(image);
1583 tmp_image=CloneImage(image,0,0,MagickTrue,exception);
1584 if (tmp_image == (
Image *) NULL)
1585 return((
Image *) NULL);
1586 (void) SetImageVirtualPixelMethod(tmp_image,TransparentVirtualPixelMethod,
1589 if ((image->alpha_trait & BlendPixelTrait) == 0)
1594 (void) SetImageAlphaChannel(tmp_image,SetAlphaChannel,exception);
1595 resize_image=DistortImage(tmp_image,AffineDistortion,12,distort_args,
1596 MagickTrue,exception),
1597 tmp_image=DestroyImage(tmp_image);
1598 if (resize_image == (
Image *) NULL)
1599 return((
Image *) NULL);
1600 (void) SetImageAlphaChannel(resize_image,OffAlphaChannel,exception);
1614 (void) SetImageAlphaChannel(tmp_image,ExtractAlphaChannel,exception);
1615 (void) SetImageAlphaChannel(tmp_image,OpaqueAlphaChannel,exception);
1616 resize_alpha=DistortImage(tmp_image,AffineDistortion,12,distort_args,
1617 MagickTrue,exception),
1618 tmp_image=DestroyImage(tmp_image);
1619 if (resize_alpha == (
Image *) NULL)
1620 return((
Image *) NULL);
1623 tmp_image=CloneImage(image,0,0,MagickTrue,exception);
1624 if (tmp_image == (
Image *) NULL)
1625 return((
Image *) NULL);
1626 (void) SetImageVirtualPixelMethod(tmp_image,
1627 TransparentVirtualPixelMethod,exception);
1628 resize_image=DistortImage(tmp_image,AffineDistortion,12,distort_args,
1629 MagickTrue,exception),
1630 tmp_image=DestroyImage(tmp_image);
1631 if (resize_image == (
Image *) NULL)
1633 resize_alpha=DestroyImage(resize_alpha);
1634 return((
Image *) NULL);
1637 (void) SetImageAlphaChannel(resize_image,OffAlphaChannel,exception);
1638 (void) SetImageAlphaChannel(resize_alpha,OffAlphaChannel,exception);
1639 (void) CompositeImage(resize_image,resize_alpha,CopyAlphaCompositeOp,
1640 MagickTrue,0,0,exception);
1641 resize_alpha=DestroyImage(resize_alpha);
1642 resize_image->alpha_trait=image->alpha_trait;
1643 resize_image->compose=image->compose;
1645 (void) SetImageVirtualPixelMethod(resize_image,vp_save,exception);
1650 crop_area.width=columns;
1651 crop_area.height=rows;
1655 tmp_image=resize_image;
1656 resize_image=CropImage(tmp_image,&crop_area,exception);
1657 tmp_image=DestroyImage(tmp_image);
1658 if (resize_image != (
Image *) NULL)
1660 resize_image->page.width=0;
1661 resize_image->page.height=0;
1663 return(resize_image);
1754MagickExport
Image *DistortImage(
const Image *image, DistortMethod method,
1755 const size_t number_arguments,
const double *arguments,
1758#define DistortImageTag "Distort/Image"
1776 assert(image != (
Image *) NULL);
1777 assert(image->signature == MagickCoreSignature);
1779 assert(exception->signature == MagickCoreSignature);
1780 if (IsEventLogging() != MagickFalse)
1781 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1785 if ( method == ResizeDistortion )
1787 if ( number_arguments != 2 )
1789 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1790 "InvalidArgument",
"%s : '%s'",
"Resize",
1791 "Invalid number of args: 2 only");
1792 return((
Image *) NULL);
1794 distort_image=DistortResizeImage(image,(
size_t)arguments[0],
1795 (
size_t)arguments[1], exception);
1796 return(distort_image);
1806 coeff = GenerateCoefficients(image, &method, number_arguments,
1807 arguments, 0, exception);
1808 if ( coeff == (
double *) NULL )
1809 return((
Image *) NULL);
1817 geometry.width=image->columns;
1818 geometry.height=image->rows;
1822 if ( method == ArcDistortion ) {
1823 bestfit = MagickTrue;
1832 fix_bounds = MagickTrue;
1834 s.x=s.y=min.x=max.x=min.y=max.y=0.0;
1837#define InitalBounds(p) \
1840 min.x = max.x = p.x; \
1841 min.y = max.y = p.y; \
1843#define ExpandBounds(p) \
1846 min.x = MagickMin(min.x,p.x); \
1847 max.x = MagickMax(max.x,p.x); \
1848 min.y = MagickMin(min.y,p.y); \
1849 max.y = MagickMax(max.y,p.y); \
1854 case AffineDistortion:
1855 case RigidAffineDistortion:
1856 {
double inverse[6];
1857 InvertAffineCoefficients(coeff, inverse);
1858 s.x = (double) image->page.x;
1859 s.y = (double) image->page.y;
1860 d.x = inverse[0]*s.x+inverse[1]*s.y+inverse[2];
1861 d.y = inverse[3]*s.x+inverse[4]*s.y+inverse[5];
1863 s.x = (double) image->page.x+image->columns;
1864 s.y = (double) image->page.y;
1865 d.x = inverse[0]*s.x+inverse[1]*s.y+inverse[2];
1866 d.y = inverse[3]*s.x+inverse[4]*s.y+inverse[5];
1868 s.x = (double) image->page.x;
1869 s.y = (double) image->page.y+image->rows;
1870 d.x = inverse[0]*s.x+inverse[1]*s.y+inverse[2];
1871 d.y = inverse[3]*s.x+inverse[4]*s.y+inverse[5];
1873 s.x = (double) image->page.x+image->columns;
1874 s.y = (double) image->page.y+image->rows;
1875 d.x = inverse[0]*s.x+inverse[1]*s.y+inverse[2];
1876 d.y = inverse[3]*s.x+inverse[4]*s.y+inverse[5];
1880 case PerspectiveDistortion:
1881 {
double inverse[8], scale;
1882 InvertPerspectiveCoefficients(coeff, inverse);
1883 s.x = (double) image->page.x;
1884 s.y = (double) image->page.y;
1885 scale=inverse[6]*s.x+inverse[7]*s.y+1.0;
1886 scale=PerceptibleReciprocal(scale);
1887 d.x = scale*(inverse[0]*s.x+inverse[1]*s.y+inverse[2]);
1888 d.y = scale*(inverse[3]*s.x+inverse[4]*s.y+inverse[5]);
1890 s.x = (double) image->page.x+image->columns;
1891 s.y = (double) image->page.y;
1892 scale=inverse[6]*s.x+inverse[7]*s.y+1.0;
1893 scale=PerceptibleReciprocal(scale);
1894 d.x = scale*(inverse[0]*s.x+inverse[1]*s.y+inverse[2]);
1895 d.y = scale*(inverse[3]*s.x+inverse[4]*s.y+inverse[5]);
1897 s.x = (double) image->page.x;
1898 s.y = (double) image->page.y+image->rows;
1899 scale=inverse[6]*s.x+inverse[7]*s.y+1.0;
1900 scale=PerceptibleReciprocal(scale);
1901 d.x = scale*(inverse[0]*s.x+inverse[1]*s.y+inverse[2]);
1902 d.y = scale*(inverse[3]*s.x+inverse[4]*s.y+inverse[5]);
1904 s.x = (double) image->page.x+image->columns;
1905 s.y = (double) image->page.y+image->rows;
1906 scale=inverse[6]*s.x+inverse[7]*s.y+1.0;
1907 scale=PerceptibleReciprocal(scale);
1908 d.x = scale*(inverse[0]*s.x+inverse[1]*s.y+inverse[2]);
1909 d.y = scale*(inverse[3]*s.x+inverse[4]*s.y+inverse[5]);
1916 a = coeff[0]-coeff[1]/2; ca = cos(a); sa = sin(a);
1920 d.x = (coeff[2]-coeff[3])*ca;
1921 d.y = (coeff[2]-coeff[3])*sa;
1923 a = coeff[0]+coeff[1]/2; ca = cos(a); sa = sin(a);
1927 d.x = (coeff[2]-coeff[3])*ca;
1928 d.y = (coeff[2]-coeff[3])*sa;
1931 for( a=(
double) (ceil((
double) ((coeff[0]-coeff[1]/2.0)/MagickPI2))*MagickPI2);
1932 a<(coeff[0]+coeff[1]/2.0); a+=MagickPI2 ) {
1933 ca = cos(a); sa = sin(a);
1943 coeff[1] = (double) (Magick2PI*image->columns/coeff[1]);
1944 coeff[3] = (double)image->rows/coeff[3];
1947 case PolarDistortion:
1949 if (number_arguments < 2)
1950 coeff[2] = coeff[3] = 0.0;
1951 min.x = coeff[2]-coeff[0];
1952 max.x = coeff[2]+coeff[0];
1953 min.y = coeff[3]-coeff[0];
1954 max.y = coeff[3]+coeff[0];
1956 coeff[7]=(double) geometry.height/(coeff[0]-coeff[1]);
1959 case DePolarDistortion:
1963 fix_bounds = MagickFalse;
1964 geometry.x = geometry.y = 0;
1965 geometry.height = (size_t) ceil(coeff[0]-coeff[1]);
1966 geometry.width = (size_t) ceil((coeff[0]-coeff[1])*
1967 (coeff[5]-coeff[4])*0.5);
1969 coeff[6]=(coeff[5]-coeff[4])*PerceptibleReciprocal(geometry.width);
1970 coeff[7]=(coeff[0]-coeff[1])*PerceptibleReciprocal(geometry.height);
1973 case Cylinder2PlaneDistortion:
1978 geometry.x = geometry.y = 0;
1979 geometry.width = (size_t) ceil( 2.0*coeff[1]*tan(coeff[0]/2.0) );
1980 geometry.height = (size_t) ceil( 2.0*coeff[3]/cos(coeff[0]/2.0) );
1982 coeff[4] = (double) geometry.width/2.0;
1983 coeff[5] = (double) geometry.height/2.0;
1984 fix_bounds = MagickFalse;
1987 case Plane2CylinderDistortion:
1991 geometry.x = geometry.y = 0;
1992 geometry.width = (size_t) ceil(coeff[0]*coeff[1]);
1993 geometry.height = (size_t) (2*coeff[3]);
1995 coeff[4] = (double) geometry.width/2.0;
1996 coeff[5] = (double) geometry.height/2.0;
1997 fix_bounds = MagickFalse;
2000 case ShepardsDistortion:
2001 case BilinearForwardDistortion:
2002 case BilinearReverseDistortion:
2004 case QuadrilateralDistortion:
2006 case PolynomialDistortion:
2007 case BarrelDistortion:
2008 case BarrelInverseDistortion:
2011 bestfit = MagickFalse;
2012 fix_bounds = MagickFalse;
2021 geometry.x = (ssize_t) floor(min.x-0.5);
2022 geometry.y = (ssize_t) floor(min.y-0.5);
2023 geometry.width=(size_t) ceil(max.x-geometry.x+0.5);
2024 geometry.height=(size_t) ceil(max.y-geometry.y+0.5);
2033 {
const char *artifact=GetImageArtifact(image,
"distort:viewport");
2034 viewport_given = MagickFalse;
2035 if ( artifact != (
const char *) NULL ) {
2036 MagickStatusType flags=ParseAbsoluteGeometry(artifact,&geometry);
2038 (void) ThrowMagickException(exception,GetMagickModule(),
2039 OptionWarning,
"InvalidSetting",
"'%s' '%s'",
2040 "distort:viewport",artifact);
2042 viewport_given = MagickTrue;
2047 if (IsStringTrue(GetImageArtifact(image,
"verbose")) != MagickFalse) {
2050 char image_gen[MagickPathExtent];
2054 if ( bestfit || viewport_given ) {
2055 (void) FormatLocaleString(image_gen,MagickPathExtent,
2056 " -size %.20gx%.20g -page %+.20g%+.20g xc: +insert \\\n",
2057 (
double) geometry.width,(
double) geometry.height,(
double) geometry.x,
2058 (
double) geometry.y);
2059 lookup=
"v.p{xx-v.page.x-0.5,yy-v.page.y-0.5}";
2062 image_gen[0] =
'\0';
2063 lookup =
"p{xx-page.x-0.5,yy-page.y-0.5}";
2068 case AffineDistortion:
2069 case RigidAffineDistortion:
2074 inverse=(
double *) AcquireQuantumMemory(6,
sizeof(*inverse));
2075 if (inverse == (
double *) NULL)
2077 coeff=(
double *) RelinquishMagickMemory(coeff);
2078 (void) ThrowMagickException(exception,GetMagickModule(),
2079 ResourceLimitError,
"MemoryAllocationFailed",
"%s",
"DistortImages");
2080 return((
Image *) NULL);
2082 InvertAffineCoefficients(coeff, inverse);
2083 CoefficientsToAffineArgs(inverse);
2084 (void) FormatLocaleFile(stderr,
"Affine projection:\n");
2085 (void) FormatLocaleFile(stderr,
2086 " -distort AffineProjection \\\n '");
2087 for (i=0; i < 5; i++)
2088 (
void) FormatLocaleFile(stderr,
"%.*g,",GetMagickPrecision(),
2090 (void) FormatLocaleFile(stderr,
"%.*g'\n",GetMagickPrecision(),
2092 (void) FormatLocaleFile(stderr,
2093 "Equivalent scale, rotation(deg), translation:\n");
2094 (void) FormatLocaleFile(stderr,
" %.*g,%.*g,%.*g,%.*g\n",
2095 GetMagickPrecision(),sqrt(inverse[0]*inverse[0]+
2096 inverse[1]*inverse[1]),GetMagickPrecision(),
2097 RadiansToDegrees(atan2(inverse[1],inverse[0])),
2098 GetMagickPrecision(),inverse[4],GetMagickPrecision(),inverse[5]);
2099 inverse=(
double *) RelinquishMagickMemory(inverse);
2100 (void) FormatLocaleFile(stderr,
"Affine distort, FX equivalent:\n");
2101 (void) FormatLocaleFile(stderr,
"%s", image_gen);
2102 (void) FormatLocaleFile(stderr,
2103 " -fx 'ii=i+page.x+0.5; jj=j+page.y+0.5;\n");
2104 (void) FormatLocaleFile(stderr,
" xx=%+.*g*ii %+.*g*jj %+.*g;\n",
2105 GetMagickPrecision(),coeff[0],GetMagickPrecision(),coeff[1],
2106 GetMagickPrecision(),coeff[2]);
2107 (void) FormatLocaleFile(stderr,
" yy=%+.*g*ii %+.*g*jj %+.*g;\n",
2108 GetMagickPrecision(),coeff[3],GetMagickPrecision(),coeff[4],
2109 GetMagickPrecision(),coeff[5]);
2110 (void) FormatLocaleFile(stderr,
" %s' \\\n",lookup);
2113 case PerspectiveDistortion:
2118 inverse=(
double *) AcquireQuantumMemory(8,
sizeof(*inverse));
2119 if (inverse == (
double *) NULL)
2121 coeff=(
double *) RelinquishMagickMemory(coeff);
2122 (void) ThrowMagickException(exception,GetMagickModule(),
2123 ResourceLimitError,
"MemoryAllocationFailed",
"%s",
2124 "DistortCoefficients");
2125 return((
Image *) NULL);
2127 InvertPerspectiveCoefficients(coeff, inverse);
2128 (void) FormatLocaleFile(stderr,
"Perspective Projection:\n");
2129 (void) FormatLocaleFile(stderr,
2130 " -distort PerspectiveProjection \\\n '");
2131 for (i=0; i < 4; i++)
2132 (
void) FormatLocaleFile(stderr,
"%.*g, ",GetMagickPrecision(),
2134 (void) FormatLocaleFile(stderr,
"\n ");
2136 (
void) FormatLocaleFile(stderr,
"%.*g, ",GetMagickPrecision(),
2138 (void) FormatLocaleFile(stderr,
"%.*g'\n",GetMagickPrecision(),
2140 inverse=(
double *) RelinquishMagickMemory(inverse);
2141 (void) FormatLocaleFile(stderr,
"Perspective Distort, FX Equivalent:\n");
2142 (void) FormatLocaleFile(stderr,
"%.1024s",image_gen);
2143 (void) FormatLocaleFile(stderr,
2144 " -fx 'ii=i+page.x+0.5; jj=j+page.y+0.5;\n");
2145 (void) FormatLocaleFile(stderr,
" rr=%+.*g*ii %+.*g*jj + 1;\n",
2146 GetMagickPrecision(),coeff[6],GetMagickPrecision(),coeff[7]);
2147 (void) FormatLocaleFile(stderr,
2148 " xx=(%+.*g*ii %+.*g*jj %+.*g)/rr;\n",
2149 GetMagickPrecision(),coeff[0],GetMagickPrecision(),coeff[1],
2150 GetMagickPrecision(),coeff[2]);
2151 (void) FormatLocaleFile(stderr,
2152 " yy=(%+.*g*ii %+.*g*jj %+.*g)/rr;\n",
2153 GetMagickPrecision(),coeff[3],GetMagickPrecision(),coeff[4],
2154 GetMagickPrecision(),coeff[5]);
2155 (void) FormatLocaleFile(stderr,
" rr%s0 ? %s : blue' \\\n",
2156 coeff[8] < 0.0 ?
"<" :
">", lookup);
2159 case BilinearForwardDistortion:
2161 (void) FormatLocaleFile(stderr,
"BilinearForward Mapping Equations:\n");
2162 (void) FormatLocaleFile(stderr,
"%s", image_gen);
2163 (void) FormatLocaleFile(stderr,
" i = %+lf*x %+lf*y %+lf*x*y %+lf;\n",
2164 coeff[0],coeff[1],coeff[2],coeff[3]);
2165 (void) FormatLocaleFile(stderr,
" j = %+lf*x %+lf*y %+lf*x*y %+lf;\n",
2166 coeff[4],coeff[5],coeff[6],coeff[7]);
2169 (void) FormatLocaleFile(stderr,
" c8 = %+lf c9 = 2*a = %+lf;\n",
2170 coeff[8], coeff[9]);
2172 (void) FormatLocaleFile(stderr,
2173 "BilinearForward Distort, FX Equivalent:\n");
2174 (void) FormatLocaleFile(stderr,
"%s", image_gen);
2175 (void) FormatLocaleFile(stderr,
2176 " -fx 'ii=i+page.x%+lf; jj=j+page.y%+lf;\n",0.5-coeff[3],0.5-
2178 (void) FormatLocaleFile(stderr,
" bb=%lf*ii %+lf*jj %+lf;\n",
2179 coeff[6], -coeff[2], coeff[8]);
2183 (void) FormatLocaleFile(stderr,
2184 " rt=bb*bb %+lf*(%lf*ii%+lf*jj);\n",-2*coeff[9],coeff[4],
2186 (void) FormatLocaleFile(stderr,
2187 " yy=( -bb + sqrt(rt) ) / %lf;\n",coeff[9]);
2190 (
void) FormatLocaleFile(stderr,
" yy=(%lf*ii%+lf*jj)/bb;\n",
2191 -coeff[4],coeff[0]);
2192 (void) FormatLocaleFile(stderr,
2193 " xx=(ii %+lf*yy)/(%lf %+lf*yy);\n",-coeff[1],coeff[0],
2195 if ( coeff[9] != 0 )
2196 (void) FormatLocaleFile(stderr,
" (rt < 0 ) ? red : %s'\n",
2199 (
void) FormatLocaleFile(stderr,
" %s' \\\n", lookup);
2202 case BilinearReverseDistortion:
2205 (void) FormatLocaleFile(stderr,
"Polynomial Projection Distort:\n");
2206 (void) FormatLocaleFile(stderr,
" -distort PolynomialProjection \\\n");
2207 (void) FormatLocaleFile(stderr,
" '1.5, %lf, %lf, %lf, %lf,\n",
2208 coeff[3], coeff[0], coeff[1], coeff[2]);
2209 (void) FormatLocaleFile(stderr,
" %lf, %lf, %lf, %lf'\n",
2210 coeff[7], coeff[4], coeff[5], coeff[6]);
2212 (void) FormatLocaleFile(stderr,
2213 "BilinearReverse Distort, FX Equivalent:\n");
2214 (void) FormatLocaleFile(stderr,
"%s", image_gen);
2215 (void) FormatLocaleFile(stderr,
2216 " -fx 'ii=i+page.x+0.5; jj=j+page.y+0.5;\n");
2217 (void) FormatLocaleFile(stderr,
2218 " xx=%+lf*ii %+lf*jj %+lf*ii*jj %+lf;\n",coeff[0],coeff[1],
2219 coeff[2], coeff[3]);
2220 (void) FormatLocaleFile(stderr,
2221 " yy=%+lf*ii %+lf*jj %+lf*ii*jj %+lf;\n",coeff[4],coeff[5],
2222 coeff[6], coeff[7]);
2223 (void) FormatLocaleFile(stderr,
" %s' \\\n", lookup);
2226 case PolynomialDistortion:
2228 size_t nterms = (size_t) coeff[1];
2229 (void) FormatLocaleFile(stderr,
2230 "Polynomial (order %lg, terms %lu), FX Equivalent\n",coeff[0],
2231 (
unsigned long) nterms);
2232 (void) FormatLocaleFile(stderr,
"%s", image_gen);
2233 (void) FormatLocaleFile(stderr,
2234 " -fx 'ii=i+page.x+0.5; jj=j+page.y+0.5;\n");
2235 (void) FormatLocaleFile(stderr,
" xx =");
2236 for (i=0; i < (ssize_t) nterms; i++)
2238 if ((i != 0) && (i%4 == 0))
2239 (void) FormatLocaleFile(stderr,
"\n ");
2240 (void) FormatLocaleFile(stderr,
" %+lf%s",coeff[2+i],
2243 (void) FormatLocaleFile(stderr,
";\n yy =");
2244 for (i=0; i < (ssize_t) nterms; i++)
2246 if ((i != 0) && (i%4 == 0))
2247 (void) FormatLocaleFile(stderr,
"\n ");
2248 (void) FormatLocaleFile(stderr,
" %+lf%s",coeff[2+i+(
int) nterms],
2251 (void) FormatLocaleFile(stderr,
";\n %s' \\\n", lookup);
2256 (void) FormatLocaleFile(stderr,
"Arc Distort, Internal Coefficients:\n");
2257 for (i=0; i < 5; i++)
2258 (
void) FormatLocaleFile(stderr,
2259 " c%.20g = %+lf\n",(
double) i,coeff[i]);
2260 (void) FormatLocaleFile(stderr,
"Arc Distort, FX Equivalent:\n");
2261 (void) FormatLocaleFile(stderr,
"%s", image_gen);
2262 (void) FormatLocaleFile(stderr,
" -fx 'ii=i+page.x; jj=j+page.y;\n");
2263 (void) FormatLocaleFile(stderr,
" xx=(atan2(jj,ii)%+lf)/(2*pi);\n",
2265 (void) FormatLocaleFile(stderr,
" xx=xx-round(xx);\n");
2266 (void) FormatLocaleFile(stderr,
" xx=xx*%lf %+lf;\n",coeff[1],
2268 (void) FormatLocaleFile(stderr,
2269 " yy=(%lf - hypot(ii,jj)) * %lf;\n",coeff[2],coeff[3]);
2270 (void) FormatLocaleFile(stderr,
" v.p{xx-.5,yy-.5}' \\\n");
2273 case PolarDistortion:
2275 (void) FormatLocaleFile(stderr,
"Polar Distort, Internal Coefficients\n");
2276 for (i=0; i < 8; i++)
2277 (
void) FormatLocaleFile(stderr,
" c%.20g = %+lf\n",(
double) i,
2279 (void) FormatLocaleFile(stderr,
"Polar Distort, FX Equivalent:\n");
2280 (void) FormatLocaleFile(stderr,
"%s", image_gen);
2281 (void) FormatLocaleFile(stderr,
2282 " -fx 'ii=i+page.x%+lf; jj=j+page.y%+lf;\n",-coeff[2],-coeff[3]);
2283 (void) FormatLocaleFile(stderr,
" xx=(atan2(ii,jj)%+lf)/(2*pi);\n",
2284 -(coeff[4]+coeff[5])/2 );
2285 (void) FormatLocaleFile(stderr,
" xx=xx-round(xx);\n");
2286 (void) FormatLocaleFile(stderr,
" xx=xx*2*pi*%lf + v.w/2;\n",
2288 (void) FormatLocaleFile(stderr,
" yy=(hypot(ii,jj)%+lf)*%lf;\n",
2289 -coeff[1],coeff[7] );
2290 (void) FormatLocaleFile(stderr,
" v.p{xx-.5,yy-.5}' \\\n");
2293 case DePolarDistortion:
2295 (void) FormatLocaleFile(stderr,
2296 "DePolar Distort, Internal Coefficients\n");
2297 for (i=0; i < 8; i++)
2298 (
void) FormatLocaleFile(stderr,
" c%.20g = %+lf\n",(
double) i,
2300 (void) FormatLocaleFile(stderr,
"DePolar Distort, FX Equivalent:\n");
2301 (void) FormatLocaleFile(stderr,
"%s", image_gen);
2302 (void) FormatLocaleFile(stderr,
" -fx 'aa=(i+.5)*%lf %+lf;\n",
2303 coeff[6],+coeff[4]);
2304 (void) FormatLocaleFile(stderr,
" rr=(j+.5)*%lf %+lf;\n",
2305 coeff[7],+coeff[1]);
2306 (void) FormatLocaleFile(stderr,
" xx=rr*sin(aa) %+lf;\n",
2308 (void) FormatLocaleFile(stderr,
" yy=rr*cos(aa) %+lf;\n",
2310 (void) FormatLocaleFile(stderr,
" v.p{xx-.5,yy-.5}' \\\n");
2313 case Cylinder2PlaneDistortion:
2315 (void) FormatLocaleFile(stderr,
2316 "Cylinder to Plane Distort, Internal Coefficients\n");
2317 (void) FormatLocaleFile(stderr,
" cylinder_radius = %+lf\n",coeff[1]);
2318 (void) FormatLocaleFile(stderr,
2319 "Cylinder to Plane Distort, FX Equivalent:\n");
2320 (void) FormatLocaleFile(stderr,
"%s", image_gen);
2321 (void) FormatLocaleFile(stderr,
2322 " -fx 'ii=i+page.x%+lf+0.5; jj=j+page.y%+lf+0.5;\n",-coeff[4],
2324 (void) FormatLocaleFile(stderr,
" aa=atan(ii/%+lf);\n",coeff[1]);
2325 (void) FormatLocaleFile(stderr,
" xx=%lf*aa%+lf;\n",
2327 (void) FormatLocaleFile(stderr,
" yy=jj*cos(aa)%+lf;\n",coeff[3]);
2328 (void) FormatLocaleFile(stderr,
" %s' \\\n", lookup);
2331 case Plane2CylinderDistortion:
2333 (void) FormatLocaleFile(stderr,
2334 "Plane to Cylinder Distort, Internal Coefficients\n");
2335 (void) FormatLocaleFile(stderr,
" cylinder_radius = %+lf\n",coeff[1]);
2336 (void) FormatLocaleFile(stderr,
2337 "Plane to Cylinder Distort, FX Equivalent:\n");
2338 (void) FormatLocaleFile(stderr,
"%s", image_gen);
2339 (void) FormatLocaleFile(stderr,
2340 " -fx 'ii=i+page.x%+lf+0.5; jj=j+page.y%+lf+0.5;\n",-coeff[4],
2342 (void) FormatLocaleFile(stderr,
" ii=ii/%+lf;\n",coeff[1]);
2343 (void) FormatLocaleFile(stderr,
" xx=%lf*tan(ii)%+lf;\n",coeff[1],
2345 (void) FormatLocaleFile(stderr,
" yy=jj/cos(ii)%+lf;\n",coeff[3]);
2346 (void) FormatLocaleFile(stderr,
" %s' \\\n", lookup);
2349 case BarrelDistortion:
2350 case BarrelInverseDistortion:
2361 xc=((double)image->columns-1.0)/2.0+image->page.x;
2362 yc=((double)image->rows-1.0)/2.0+image->page.y;
2363 (void) FormatLocaleFile(stderr,
"Barrel%s Distort, FX Equivalent:\n",
2364 method == BarrelDistortion ?
"" :
"Inv");
2365 (void) FormatLocaleFile(stderr,
"%s", image_gen);
2366 if ( fabs(coeff[8]-xc-0.5) < 0.1 && fabs(coeff[9]-yc-0.5) < 0.1 )
2367 (void) FormatLocaleFile(stderr,
" -fx 'xc=(w-1)/2; yc=(h-1)/2;\n");
2369 (
void) FormatLocaleFile(stderr,
" -fx 'xc=%lf; yc=%lf;\n",coeff[8]-
2371 (void) FormatLocaleFile(stderr,
2372 " ii=i-xc; jj=j-yc; rr=hypot(ii,jj);\n");
2373 (void) FormatLocaleFile(stderr,
2374 " ii=ii%s(%lf*rr*rr*rr %+lf*rr*rr %+lf*rr %+lf);\n",
2375 method == BarrelDistortion ?
"*" :
"/",coeff[0],coeff[1],coeff[2],
2377 (void) FormatLocaleFile(stderr,
2378 " jj=jj%s(%lf*rr*rr*rr %+lf*rr*rr %+lf*rr %+lf);\n",
2379 method == BarrelDistortion ?
"*" :
"/",coeff[4],coeff[5],coeff[6],
2381 (void) FormatLocaleFile(stderr,
" p{ii+xc,jj+yc}' \\\n");
2392 {
const char *artifact;
2393 artifact=GetImageArtifact(image,
"distort:scale");
2394 output_scaling = 1.0;
2395 if (artifact != (
const char *) NULL) {
2396 output_scaling = fabs(StringToDouble(artifact,(
char **) NULL));
2397 geometry.width=(size_t) (output_scaling*geometry.width+0.5);
2398 geometry.height=(size_t) (output_scaling*geometry.height+0.5);
2399 geometry.x=(ssize_t) (output_scaling*geometry.x+0.5);
2400 geometry.y=(ssize_t) (output_scaling*geometry.y+0.5);
2401 if ( output_scaling < 0.1 ) {
2402 coeff = (
double *) RelinquishMagickMemory(coeff);
2403 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
2404 "InvalidArgument",
"%s",
"-set option:distort:scale" );
2405 return((
Image *) NULL);
2407 output_scaling = 1/output_scaling;
2410#define ScaleFilter(F,A,B,C,D) \
2411 ScaleResampleFilter( (F), \
2412 output_scaling*(A), output_scaling*(B), \
2413 output_scaling*(C), output_scaling*(D) )
2418 distort_image=CloneImage(image,geometry.width,geometry.height,MagickTrue,
2420 if (distort_image == (
Image *) NULL)
2422 coeff=(
double *) RelinquishMagickMemory(coeff);
2423 return((
Image *) NULL);
2426 if (SetImageStorageClass(distort_image,DirectClass,exception) == MagickFalse)
2428 coeff=(
double *) RelinquishMagickMemory(coeff);
2429 distort_image=DestroyImage(distort_image);
2430 return((
Image *) NULL);
2432 if ((IsPixelInfoGray(&distort_image->background_color) == MagickFalse) &&
2433 (IsGrayColorspace(distort_image->colorspace) != MagickFalse))
2434 (void) SetImageColorspace(distort_image,sRGBColorspace,exception);
2435 if (distort_image->background_color.alpha_trait != UndefinedPixelTrait)
2436 distort_image->alpha_trait=BlendPixelTrait;
2437 distort_image->page.x=geometry.x;
2438 distort_image->page.y=geometry.y;
2439 ConformPixelInfo(distort_image,&distort_image->matte_color,&invalid,
2458 **magick_restrict resample_filter;
2465 GetPixelInfo(distort_image,&zero);
2466 resample_filter=AcquireResampleFilterTLS(image,UndefinedVirtualPixelMethod,
2467 MagickFalse,exception);
2468 distort_view=AcquireAuthenticCacheView(distort_image,exception);
2469#if defined(MAGICKCORE_OPENMP_SUPPORT)
2470 #pragma omp parallel for schedule(static) shared(progress,status) \
2471 magick_number_threads(image,distort_image,distort_image->rows,1)
2473 for (j=0; j < (ssize_t) distort_image->rows; j++)
2476 id = GetOpenMPThreadId();
2497 q=QueueCacheViewAuthenticPixels(distort_view,0,j,distort_image->columns,1,
2499 if (q == (Quantum *) NULL)
2511 case AffineDistortion:
2512 case RigidAffineDistortion:
2513 ScaleFilter( resample_filter[
id],
2515 coeff[3], coeff[4] );
2528 for (i=0; i < (ssize_t) distort_image->columns; i++)
2531 d.x = (double) (geometry.x+i+0.5)*output_scaling;
2532 d.y = (double) (geometry.y+j+0.5)*output_scaling;
2536 case AffineDistortion:
2537 case RigidAffineDistortion:
2539 s.x=coeff[0]*d.x+coeff[1]*d.y+coeff[2];
2540 s.y=coeff[3]*d.x+coeff[4]*d.y+coeff[5];
2544 case PerspectiveDistortion:
2547 p,n,r,abs_r,abs_c6,abs_c7,scale;
2549 p=coeff[0]*d.x+coeff[1]*d.y+coeff[2];
2550 n=coeff[3]*d.x+coeff[4]*d.y+coeff[5];
2551 r=coeff[6]*d.x+coeff[7]*d.y+1.0;
2553 validity = (r*coeff[8] < 0.0) ? 0.0 : 1.0;
2556 abs_c6 = fabs(coeff[6]);
2557 abs_c7 = fabs(coeff[7]);
2558 if ( abs_c6 > abs_c7 ) {
2559 if ( abs_r < abs_c6*output_scaling )
2560 validity = 0.5 - coeff[8]*r/(coeff[6]*output_scaling);
2562 else if ( abs_r < abs_c7*output_scaling )
2563 validity = 0.5 - coeff[8]*r/(coeff[7]*output_scaling);
2565 if ( validity > 0.0 ) {
2572 ScaleFilter( resample_filter[
id],
2573 (r*coeff[0] - p*coeff[6])*scale,
2574 (r*coeff[1] - p*coeff[7])*scale,
2575 (r*coeff[3] - n*coeff[6])*scale,
2576 (r*coeff[4] - n*coeff[7])*scale );
2580 case BilinearReverseDistortion:
2583 s.x=coeff[0]*d.x+coeff[1]*d.y+coeff[2]*d.x*d.y+coeff[3];
2584 s.y=coeff[4]*d.x+coeff[5]*d.y
2585 +coeff[6]*d.x*d.y+coeff[7];
2587 ScaleFilter( resample_filter[
id],
2588 coeff[0] + coeff[2]*d.y,
2589 coeff[1] + coeff[2]*d.x,
2590 coeff[4] + coeff[6]*d.y,
2591 coeff[5] + coeff[6]*d.x );
2594 case BilinearForwardDistortion:
2599 d.x -= coeff[3]; d.y -= coeff[7];
2600 b = coeff[6]*d.x - coeff[2]*d.y + coeff[8];
2601 c = coeff[4]*d.x - coeff[0]*d.y;
2606 if ( fabs(coeff[9]) < MagickEpsilon )
2609 c = b*b - 2*coeff[9]*c;
2613 s.y = ( -b + sqrt(c) )/coeff[9];
2615 if ( validity > 0.0 )
2616 s.x = ( d.x - coeff[1]*s.y) / ( coeff[0] + coeff[2]*s.y );
2626 case BilinearDistortion:
2631 case PolynomialDistortion:
2638 nterms=(ssize_t)coeff[1];
2643 s.x=s.y=du.x=du.y=dv.x=dv.y=0.0;
2644 for(k=0; k < nterms; k++) {
2645 s.x += poly_basis_fn(k,d.x,d.y)*coeff[2+k];
2646 du.x += poly_basis_dx(k,d.x,d.y)*coeff[2+k];
2647 du.y += poly_basis_dy(k,d.x,d.y)*coeff[2+k];
2648 s.y += poly_basis_fn(k,d.x,d.y)*coeff[2+k+nterms];
2649 dv.x += poly_basis_dx(k,d.x,d.y)*coeff[2+k+nterms];
2650 dv.y += poly_basis_dy(k,d.x,d.y)*coeff[2+k+nterms];
2652 ScaleFilter( resample_filter[
id], du.x,du.y,dv.x,dv.y );
2658 s.x = (double) ((atan2(d.y,d.x) - coeff[0])/Magick2PI);
2659 s.x -= MagickRound(s.x);
2660 s.y = hypot(d.x,d.y);
2667 if ( s.y > MagickEpsilon )
2668 ScaleFilter( resample_filter[
id],
2669 (
double) (coeff[1]/(Magick2PI*s.y)), 0, 0, coeff[3] );
2671 ScaleFilter( resample_filter[
id],
2672 distort_image->columns*2, 0, 0, coeff[3] );
2675 s.x = s.x*coeff[1] + coeff[4] + image->page.x +0.5;
2676 s.y = (coeff[2] - s.y) * coeff[3] + image->page.y;
2679 case PolarDistortion:
2683 s.x = atan2(d.x,d.y) - (coeff[4]+coeff[5])/2;
2685 s.x -= MagickRound(s.x);
2687 s.y = hypot(d.x,d.y);
2692 if ( s.y > MagickEpsilon )
2693 ScaleFilter( resample_filter[
id],
2694 (
double) (coeff[6]/(Magick2PI*s.y)), 0, 0, coeff[7] );
2696 ScaleFilter( resample_filter[
id],
2697 distort_image->columns*2, 0, 0, coeff[7] );
2700 s.x = s.x*coeff[6] + (double)image->columns/2.0 + image->page.x;
2701 s.y = (s.y-coeff[1])*coeff[7] + image->page.y;
2704 case DePolarDistortion:
2707 d.x = ((double)i+0.5)*output_scaling*coeff[6]+coeff[4];
2708 d.y = ((double)j+0.5)*output_scaling*coeff[7]+coeff[1];
2709 s.x = d.y*sin(d.x) + coeff[2];
2710 s.y = d.y*cos(d.x) + coeff[3];
2714 case Cylinder2PlaneDistortion:
2718 d.x -= coeff[4]; d.y -= coeff[5];
2725 ScaleFilter( resample_filter[
id],
2726 1.0/(1.0+d.x*d.x), 0.0, -d.x*s.y*cx*cx/coeff[1], s.y/d.y );
2728if ( i == 0 && j == 0 ) {
2729 fprintf(stderr,
"x=%lf y=%lf u=%lf v=%lf\n", d.x*coeff[1], d.y, s.x, s.y);
2730 fprintf(stderr,
"phi = %lf\n", (
double)(ax * 180.0/MagickPI) );
2731 fprintf(stderr,
"du/dx=%lf du/dx=%lf dv/dx=%lf dv/dy=%lf\n",
2732 1.0/(1.0+d.x*d.x), 0.0, -d.x*s.y*cx*cx/coeff[1], s.y/d.y );
2736 s.x += coeff[2]; s.y += coeff[3];
2739 case Plane2CylinderDistortion:
2742 d.x -= coeff[4]; d.y -= coeff[5];
2746 validity = (double) (coeff[1]*MagickPI2 - fabs(d.x))/output_scaling + 0.5;
2748 if ( validity > 0.0 ) {
2756 ScaleFilter( resample_filter[
id],
2757 cx*cx, 0.0, s.y*cx/coeff[1], cx );
2760if ( d.x == 0.5 && d.y == 0.5 ) {
2761 fprintf(stderr,
"x=%lf y=%lf u=%lf v=%lf\n", d.x*coeff[1], d.y, s.x, s.y);
2762 fprintf(stderr,
"radius = %lf phi = %lf validity = %lf\n",
2763 coeff[1], (
double)(d.x * 180.0/MagickPI), validity );
2764 fprintf(stderr,
"du/dx=%lf du/dx=%lf dv/dx=%lf dv/dy=%lf\n",
2765 cx*cx, 0.0, s.y*cx/coeff[1], cx);
2770 s.x += coeff[2]; s.y += coeff[3];
2773 case BarrelDistortion:
2774 case BarrelInverseDistortion:
2776 double r,fx,fy,gx,gy;
2780 r = sqrt(d.x*d.x+d.y*d.y);
2781 if ( r > MagickEpsilon ) {
2782 fx = ((coeff[0]*r + coeff[1])*r + coeff[2])*r + coeff[3];
2783 fy = ((coeff[4]*r + coeff[5])*r + coeff[6])*r + coeff[7];
2784 gx = ((3*coeff[0]*r + 2*coeff[1])*r + coeff[2])/r;
2785 gy = ((3*coeff[4]*r + 2*coeff[5])*r + coeff[6])/r;
2787 if ( method == BarrelInverseDistortion ) {
2788 fx = 1/fx; fy = 1/fy;
2789 gx *= -fx*fx; gy *= -fy*fy;
2792 s.x = d.x*fx + coeff[8];
2793 s.y = d.y*fy + coeff[9];
2794 ScaleFilter( resample_filter[
id],
2795 gx*d.x*d.x + fx, gx*d.x*d.y,
2796 gy*d.x*d.y, gy*d.y*d.y + fy );
2805 if ( method == BarrelDistortion )
2806 ScaleFilter( resample_filter[
id],
2807 coeff[3], 0, 0, coeff[7] );
2810 ScaleFilter( resample_filter[
id],
2811 1.0/coeff[3], 0, 0, 1.0/coeff[7] );
2815 case ShepardsDistortion:
2831 denominator = s.x = s.y = 0;
2832 for(k=0; k<number_arguments; k+=4) {
2834 ((double)d.x-arguments[k+2])*((
double)d.x-arguments[k+2])
2835 + ((double)d.y-arguments[k+3])*((
double)d.y-arguments[k+3]);
2836 weight = pow(weight,coeff[0]);
2837 weight = ( weight < 1.0 ) ? 1.0 : 1.0/weight;
2839 s.x += (arguments[ k ]-arguments[k+2])*weight;
2840 s.y += (arguments[k+1]-arguments[k+3])*weight;
2841 denominator += weight;
2853 if ( bestfit && method != ArcDistortion ) {
2854 s.x -= image->page.x;
2855 s.y -= image->page.y;
2860 if ( validity <= 0.0 ) {
2862 SetPixelViaPixelInfo(distort_image,&invalid,q);
2866 status=ResamplePixelColor(resample_filter[
id],s.x,s.y,&pixel,
2868 if (status == MagickFalse)
2869 SetPixelViaPixelInfo(distort_image,&invalid,q);
2873 if ( validity < 1.0 ) {
2876 CompositePixelInfoBlend(&pixel,validity,&invalid,(1.0-validity),
2879 SetPixelViaPixelInfo(distort_image,&pixel,q);
2882 q+=(ptrdiff_t) GetPixelChannels(distort_image);
2884 sync=SyncCacheViewAuthenticPixels(distort_view,exception);
2885 if (sync == MagickFalse)
2887 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2892#if defined(MAGICKCORE_OPENMP_SUPPORT)
2896 proceed=SetImageProgress(image,DistortImageTag,progress,image->rows);
2897 if (proceed == MagickFalse)
2901 distort_view=DestroyCacheView(distort_view);
2902 resample_filter=DestroyResampleFilterTLS(resample_filter);
2904 if (status == MagickFalse)
2905 distort_image=DestroyImage(distort_image);
2911 if ( method == ArcDistortion && !bestfit && !viewport_given ) {
2912 distort_image->page.x = 0;
2913 distort_image->page.y = 0;
2915 coeff=(
double *) RelinquishMagickMemory(coeff);
2916 return(distort_image);
2953MagickExport
Image *RotateImage(
const Image *image,
const double degrees,
2972 assert(image != (
Image *) NULL);
2973 assert(image->signature == MagickCoreSignature);
2974 if (IsEventLogging() != MagickFalse)
2975 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2977 assert(exception->signature == MagickCoreSignature);
2978 angle=fmod(degrees,360.0);
2979 while (angle < -45.0)
2981 for (rotations=0; angle > 45.0; rotations++)
2984 shear.x=(-tan((
double) DegreesToRadians(angle)/2.0));
2985 shear.y=sin((
double) DegreesToRadians(angle));
2986 if ((fabs(shear.x) < MagickEpsilon) && (fabs(shear.y) < MagickEpsilon))
2987 return(IntegralRotateImage(image,rotations,exception));
2988 distort_image=CloneImage(image,0,0,MagickTrue,exception);
2989 if (distort_image == (
Image *) NULL)
2990 return((
Image *) NULL);
2991 (void) SetImageVirtualPixelMethod(distort_image,BackgroundVirtualPixelMethod,
2993 rotate_image=DistortImage(distort_image,ScaleRotateTranslateDistortion,1,
2994 °rees,MagickTrue,exception);
2995 distort_image=DestroyImage(distort_image);
2996 return(rotate_image);
3038MagickExport
Image *SparseColorImage(
const Image *image,
3039 const SparseColorMethod method,
const size_t number_arguments,
3042#define SparseColorTag "Distort/SparseColor"
3056 assert(image != (
Image *) NULL);
3057 assert(image->signature == MagickCoreSignature);
3059 assert(exception->signature == MagickCoreSignature);
3060 if (IsEventLogging() != MagickFalse)
3061 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
3065 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
3067 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
3069 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
3071 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
3072 (image->colorspace == CMYKColorspace))
3074 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
3075 (image->alpha_trait != UndefinedPixelTrait))
3085 distort_method=(DistortMethod) method;
3086 if ( distort_method >= SentinelDistortion )
3087 distort_method = ShepardsDistortion;
3088 coeff = GenerateCoefficients(image, &distort_method, number_arguments,
3089 arguments, number_colors, exception);
3090 if ( coeff == (
double *) NULL )
3091 return((
Image *) NULL);
3098 sparse_method = (SparseColorMethod) distort_method;
3099 if ( distort_method == ShepardsDistortion )
3100 sparse_method = method;
3101 if ( sparse_method == InverseColorInterpolate )
3106 if (IsStringTrue(GetImageArtifact(image,
"verbose")) != MagickFalse) {
3108 switch (sparse_method) {
3109 case BarycentricColorInterpolate:
3112 (void) FormatLocaleFile(stderr,
"Barycentric Sparse Color:\n");
3113 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
3114 (void) FormatLocaleFile(stderr,
" -channel R -fx '%+lf*i %+lf*j %+lf' \\\n",
3115 coeff[x], coeff[x+1], coeff[x+2]),x+=3;
3116 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
3117 (void) FormatLocaleFile(stderr,
" -channel G -fx '%+lf*i %+lf*j %+lf' \\\n",
3118 coeff[x], coeff[x+1], coeff[x+2]),x+=3;
3119 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
3120 (void) FormatLocaleFile(stderr,
" -channel B -fx '%+lf*i %+lf*j %+lf' \\\n",
3121 coeff[x], coeff[x+1], coeff[x+2]),x+=3;
3122 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
3123 (image->colorspace == CMYKColorspace))
3124 (void) FormatLocaleFile(stderr,
" -channel K -fx '%+lf*i %+lf*j %+lf' \\\n",
3125 coeff[x], coeff[x+1], coeff[x+2]),x+=3;
3126 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
3127 (image->alpha_trait != UndefinedPixelTrait))
3128 (void) FormatLocaleFile(stderr,
" -channel A -fx '%+lf*i %+lf*j %+lf' \\\n",
3129 coeff[x], coeff[x+1], coeff[x+2]),x+=3;
3132 case BilinearColorInterpolate:
3135 (void) FormatLocaleFile(stderr,
"Bilinear Sparse Color\n");
3136 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
3137 (void) FormatLocaleFile(stderr,
" -channel R -fx '%+lf*i %+lf*j %+lf*i*j %+lf;\n",
3138 coeff[ x ], coeff[x+1],
3139 coeff[x+2], coeff[x+3]),x+=4;
3140 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
3141 (void) FormatLocaleFile(stderr,
" -channel G -fx '%+lf*i %+lf*j %+lf*i*j %+lf;\n",
3142 coeff[ x ], coeff[x+1],
3143 coeff[x+2], coeff[x+3]),x+=4;
3144 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
3145 (void) FormatLocaleFile(stderr,
" -channel B -fx '%+lf*i %+lf*j %+lf*i*j %+lf;\n",
3146 coeff[ x ], coeff[x+1],
3147 coeff[x+2], coeff[x+3]),x+=4;
3148 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
3149 (image->colorspace == CMYKColorspace))
3150 (void) FormatLocaleFile(stderr,
" -channel K -fx '%+lf*i %+lf*j %+lf*i*j %+lf;\n",
3151 coeff[ x ], coeff[x+1],
3152 coeff[x+2], coeff[x+3]),x+=4;
3153 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
3154 (image->alpha_trait != UndefinedPixelTrait))
3155 (void) FormatLocaleFile(stderr,
" -channel A -fx '%+lf*i %+lf*j %+lf*i*j %+lf;\n",
3156 coeff[ x ], coeff[x+1],
3157 coeff[x+2], coeff[x+3]),x+=4;
3172 sparse_image=CloneImage(image,0,0,MagickTrue,exception);
3173 if (sparse_image == (
Image *) NULL)
3174 return((
Image *) NULL);
3175 if (SetImageStorageClass(sparse_image,DirectClass,exception) == MagickFalse)
3177 sparse_image=DestroyImage(sparse_image);
3178 return((
Image *) NULL);
3180 if (IsGrayColorspace(sparse_image->colorspace) != MagickFalse)
3181 (void) SetImageColorspace(sparse_image,sRGBColorspace,exception);
3197 sparse_view=AcquireAuthenticCacheView(sparse_image,exception);
3198#if defined(MAGICKCORE_OPENMP_SUPPORT)
3199 #pragma omp parallel for schedule(static) shared(progress,status) \
3200 magick_number_threads(image,sparse_image,sparse_image->rows,1)
3202 for (j=0; j < (ssize_t) sparse_image->rows; j++)
3216 q=GetCacheViewAuthenticPixels(sparse_view,0,j,sparse_image->columns,1,
3218 if (q == (Quantum *) NULL)
3223 GetPixelInfo(sparse_image,&pixel);
3224 for (i=0; i < (ssize_t) sparse_image->columns; i++)
3226 GetPixelInfoPixel(sparse_image,q,&pixel);
3227 switch (sparse_method)
3229 case BarycentricColorInterpolate:
3232 if ((GetPixelRedTraits(sparse_image) & UpdatePixelTrait) != 0)
3233 pixel.red = coeff[x]*i +coeff[x+1]*j
3235 if ((GetPixelGreenTraits(sparse_image) & UpdatePixelTrait) != 0)
3236 pixel.green = coeff[x]*i +coeff[x+1]*j
3238 if ((GetPixelBlueTraits(sparse_image) & UpdatePixelTrait) != 0)
3239 pixel.blue = coeff[x]*i +coeff[x+1]*j
3241 if (((GetPixelBlackTraits(sparse_image) & UpdatePixelTrait) != 0) &&
3242 (sparse_image->colorspace == CMYKColorspace))
3243 pixel.black = coeff[x]*i +coeff[x+1]*j
3245 if (((GetPixelAlphaTraits(sparse_image) & UpdatePixelTrait) != 0) &&
3246 (sparse_image->alpha_trait != UndefinedPixelTrait))
3247 pixel.alpha = coeff[x]*i +coeff[x+1]*j
3251 case BilinearColorInterpolate:
3254 if ((GetPixelRedTraits(sparse_image) & UpdatePixelTrait) != 0)
3255 pixel.red = coeff[x]*i + coeff[x+1]*j +
3256 coeff[x+2]*i*j + coeff[x+3], x+=4;
3257 if ((GetPixelGreenTraits(sparse_image) & UpdatePixelTrait) != 0)
3258 pixel.green = coeff[x]*i + coeff[x+1]*j +
3259 coeff[x+2]*i*j + coeff[x+3], x+=4;
3260 if ((GetPixelBlueTraits(sparse_image) & UpdatePixelTrait) != 0)
3261 pixel.blue = coeff[x]*i + coeff[x+1]*j +
3262 coeff[x+2]*i*j + coeff[x+3], x+=4;
3263 if (((GetPixelBlackTraits(sparse_image) & UpdatePixelTrait) != 0) &&
3264 (image->colorspace == CMYKColorspace))
3265 pixel.black = coeff[x]*i + coeff[x+1]*j +
3266 coeff[x+2]*i*j + coeff[x+3], x+=4;
3267 if (((GetPixelAlphaTraits(sparse_image) & UpdatePixelTrait) != 0) &&
3268 (sparse_image->alpha_trait != UndefinedPixelTrait))
3269 pixel.alpha = coeff[x]*i + coeff[x+1]*j +
3270 coeff[x+2]*i*j + coeff[x+3], x+=4;
3273 case InverseColorInterpolate:
3274 case ShepardsColorInterpolate:
3282 if ((GetPixelRedTraits(sparse_image) & UpdatePixelTrait) != 0)
3284 if ((GetPixelGreenTraits(sparse_image) & UpdatePixelTrait) != 0)
3286 if ((GetPixelBlueTraits(sparse_image) & UpdatePixelTrait) != 0)
3288 if (((GetPixelBlackTraits(sparse_image) & UpdatePixelTrait) != 0) &&
3289 (image->colorspace == CMYKColorspace))
3291 if (((GetPixelAlphaTraits(sparse_image) & UpdatePixelTrait) != 0) &&
3292 (sparse_image->alpha_trait != UndefinedPixelTrait))
3295 for (k=0; k<number_arguments; k+=2+number_colors)
3298 ((double) i-arguments[ k ])*((
double) i-arguments[ k ])
3299 + ((double) j-arguments[k+1])*((
double) j-arguments[k+1]);
3300 ssize_t x = (ssize_t) k+2;
3302 weight = pow(weight,coeff[0]);
3303 weight = ( weight < 1.0 ) ? 1.0 : 1.0/weight;
3304 if ((GetPixelRedTraits(sparse_image) & UpdatePixelTrait) != 0)
3305 pixel.red += arguments[x++]*weight;
3306 if ((GetPixelGreenTraits(sparse_image) & UpdatePixelTrait) != 0)
3307 pixel.green += arguments[x++]*weight;
3308 if ((GetPixelBlueTraits(sparse_image) & UpdatePixelTrait) != 0)
3309 pixel.blue += arguments[x++]*weight;
3310 if (((GetPixelBlackTraits(sparse_image) & UpdatePixelTrait) != 0) &&
3311 (image->colorspace == CMYKColorspace))
3312 pixel.black += arguments[x++]*weight;
3313 if (((GetPixelAlphaTraits(sparse_image) & UpdatePixelTrait) != 0) &&
3314 (sparse_image->alpha_trait != UndefinedPixelTrait))
3315 pixel.alpha += arguments[x++]*weight;
3316 denominator += weight;
3318 if ((GetPixelRedTraits(sparse_image) & UpdatePixelTrait) != 0)
3319 pixel.red/=denominator;
3320 if ((GetPixelGreenTraits(sparse_image) & UpdatePixelTrait) != 0)
3321 pixel.green/=denominator;
3322 if ((GetPixelBlueTraits(sparse_image) & UpdatePixelTrait) != 0)
3323 pixel.blue/=denominator;
3324 if (((GetPixelBlackTraits(sparse_image) & UpdatePixelTrait) != 0) &&
3325 (image->colorspace == CMYKColorspace))
3326 pixel.black/=denominator;
3327 if (((GetPixelAlphaTraits(sparse_image) & UpdatePixelTrait) != 0) &&
3328 (sparse_image->alpha_trait != UndefinedPixelTrait))
3329 pixel.alpha/=denominator;
3332 case ManhattanColorInterpolate:
3335 minimum = MagickMaximumValue;
3343 for (k=0; k<number_arguments; k+=2+number_colors)
3345 double distance = fabs((
double)i-arguments[ k ])+
3346 fabs((
double)j-arguments[k+1]);
3347 if ( distance < minimum ) {
3348 ssize_t x=(ssize_t) k+2;
3349 if ((GetPixelRedTraits(sparse_image) & UpdatePixelTrait) != 0)
3350 pixel.red=arguments[x++];
3351 if ((GetPixelGreenTraits(sparse_image) & UpdatePixelTrait) != 0)
3352 pixel.green=arguments[x++];
3353 if ((GetPixelBlueTraits(sparse_image) & UpdatePixelTrait) != 0)
3354 pixel.blue=arguments[x++];
3355 if (((GetPixelBlackTraits(sparse_image) & UpdatePixelTrait) != 0) &&
3356 (image->colorspace == CMYKColorspace))
3357 pixel.black=arguments[x++];
3358 if (((GetPixelAlphaTraits(sparse_image) & UpdatePixelTrait) != 0) &&
3359 (sparse_image->alpha_trait != UndefinedPixelTrait))
3360 pixel.alpha=arguments[x++];
3366 case VoronoiColorInterpolate:
3370 minimum = MagickMaximumValue;
3378 for (k=0; k<number_arguments; k+=2+number_colors) {
3380 ((double) i-arguments[ k ])*((
double) i-arguments[ k ])
3381 + ((double) j-arguments[k+1])*((
double) j-arguments[k+1]);
3382 if ( distance < minimum ) {
3383 ssize_t x = (ssize_t) k+2;
3384 if ((GetPixelRedTraits(sparse_image) & UpdatePixelTrait) != 0)
3385 pixel.red=arguments[x++];
3386 if ((GetPixelGreenTraits(sparse_image) & UpdatePixelTrait) != 0)
3387 pixel.green=arguments[x++];
3388 if ((GetPixelBlueTraits(sparse_image) & UpdatePixelTrait) != 0)
3389 pixel.blue=arguments[x++];
3390 if (((GetPixelBlackTraits(sparse_image) & UpdatePixelTrait) != 0) &&
3391 (image->colorspace == CMYKColorspace))
3392 pixel.black=arguments[x++];
3393 if (((GetPixelAlphaTraits(sparse_image) & UpdatePixelTrait) != 0) &&
3394 (sparse_image->alpha_trait != UndefinedPixelTrait))
3395 pixel.alpha=arguments[x++];
3403 if ((GetPixelRedTraits(sparse_image) & UpdatePixelTrait) != 0)
3404 pixel.red=(MagickRealType) ClampPixel((
double) QuantumRange*
3406 if ((GetPixelGreenTraits(sparse_image) & UpdatePixelTrait) != 0)
3407 pixel.green=(MagickRealType) ClampPixel((
double) QuantumRange*
3409 if ((GetPixelBlueTraits(sparse_image) & UpdatePixelTrait) != 0)
3410 pixel.blue=(MagickRealType) ClampPixel((
double) QuantumRange*
3412 if (((GetPixelBlackTraits(sparse_image) & UpdatePixelTrait) != 0) &&
3413 (image->colorspace == CMYKColorspace))
3414 pixel.black=(MagickRealType) ClampPixel((
double) QuantumRange*
3416 if (((GetPixelAlphaTraits(sparse_image) & UpdatePixelTrait) != 0) &&
3417 (image->alpha_trait != UndefinedPixelTrait))
3418 pixel.alpha=(MagickRealType) ClampPixel((
double) QuantumRange*
3420 SetPixelViaPixelInfo(sparse_image,&pixel,q);
3421 q+=(ptrdiff_t) GetPixelChannels(sparse_image);
3423 sync=SyncCacheViewAuthenticPixels(sparse_view,exception);
3424 if (sync == MagickFalse)
3426 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3431#if defined(MAGICKCORE_OPENMP_SUPPORT)
3435 proceed=SetImageProgress(image,SparseColorTag,progress,image->rows);
3436 if (proceed == MagickFalse)
3440 sparse_view=DestroyCacheView(sparse_view);
3441 if (status == MagickFalse)
3442 sparse_image=DestroyImage(sparse_image);
3444 coeff = (
double *) RelinquishMagickMemory(coeff);
3445 return(sparse_image);