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 ( arguments[0] < MagickEpsilon || arguments[0] > 160.0 ) {
1289 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1290 "InvalidArgument",
"%s : Invalid FOV Angle",
1291 CommandOptionToMnemonic(MagickDistortOptions, *method) );
1292 coeff=(
double *) RelinquishMagickMemory(coeff);
1293 return((
double *) NULL);
1295 coeff[0] = DegreesToRadians(arguments[0]);
1296 if ( *method == Cylinder2PlaneDistortion )
1300 coeff[1] = (double) image->columns/coeff[0];
1303 coeff[1] = (double) image->columns / ( 2 * tan(coeff[0]/2) );
1305 coeff[2] = (double)(image->columns)/2.0+image->page.x;
1306 coeff[3] = (double)(image->rows)/2.0+image->page.y;
1307 coeff[4] = coeff[2];
1308 coeff[5] = coeff[3];
1311 case BarrelDistortion:
1312 case BarrelInverseDistortion:
1333 rscale = 2.0/MagickMin((
double) image->columns,(
double) image->rows);
1336 if ( (number_arguments < 3) || (number_arguments == 7) ||
1337 (number_arguments == 9) || (number_arguments > 10) )
1339 coeff=(
double *) RelinquishMagickMemory(coeff);
1340 (void) ThrowMagickException(exception,GetMagickModule(),
1341 OptionError,
"InvalidArgument",
"%s : number of arguments",
1342 CommandOptionToMnemonic(MagickDistortOptions, *method) );
1343 return((
double *) NULL);
1346 coeff[0] = arguments[0];
1347 coeff[1] = arguments[1];
1348 coeff[2] = arguments[2];
1349 if ((number_arguments == 3) || (number_arguments == 5) )
1350 coeff[3] = 1.0 - coeff[0] - coeff[1] - coeff[2];
1352 coeff[3] = arguments[3];
1354 coeff[0] *= pow(rscale,3.0);
1355 coeff[1] *= rscale*rscale;
1358 if ( number_arguments >= 8 ) {
1359 coeff[4] = arguments[4] * pow(rscale,3.0);
1360 coeff[5] = arguments[5] * rscale*rscale;
1361 coeff[6] = arguments[6] * rscale;
1362 coeff[7] = arguments[7];
1365 coeff[4] = coeff[0];
1366 coeff[5] = coeff[1];
1367 coeff[6] = coeff[2];
1368 coeff[7] = coeff[3];
1371 if ( number_arguments == 5 ) {
1372 coeff[8] = arguments[3];
1373 coeff[9] = arguments[4];
1375 else if ( number_arguments == 6 ) {
1376 coeff[8] = arguments[4];
1377 coeff[9] = arguments[5];
1379 else if ( number_arguments == 10 ) {
1380 coeff[8] = arguments[8];
1381 coeff[9] = arguments[9];
1385 coeff[8] = (double)image->columns/2.0 + image->page.x;
1386 coeff[9] = (double)image->rows/2.0 + image->page.y;
1390 case ShepardsDistortion:
1397 if ( number_arguments%cp_size != 0 ||
1398 number_arguments < cp_size ) {
1399 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1400 "InvalidArgument",
"%s : 'requires CP's (4 numbers each)'",
1401 CommandOptionToMnemonic(MagickDistortOptions, *method));
1402 coeff=(
double *) RelinquishMagickMemory(coeff);
1403 return((
double *) NULL);
1406 {
const char *artifact=GetImageArtifact(image,
"shepards:power");
1407 if ( artifact != (
const char *) NULL ) {
1408 coeff[0]=StringToDouble(artifact,(
char **) NULL) / 2.0;
1409 if ( coeff[0] < MagickEpsilon ) {
1410 (void) ThrowMagickException(exception,GetMagickModule(),
1411 OptionError,
"InvalidArgument",
"%s",
"-define shepards:power" );
1412 coeff=(
double *) RelinquishMagickMemory(coeff);
1413 return((
double *) NULL);
1425 perror(
"no method handler");
1426 return((
double *) NULL);
1464MagickExport Image *DistortResizeImage(
const Image *image,
1465 const size_t columns,
const size_t rows,ExceptionInfo *exception)
1467#define DistortResizeImageTag "Distort/Image"
1485 assert(image != (
const Image *) NULL);
1486 assert(image->signature == MagickCoreSignature);
1487 assert(exception != (ExceptionInfo *) NULL);
1488 assert(exception->signature == MagickCoreSignature);
1489 if (IsEventLogging() != MagickFalse)
1490 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1491 if ((columns == 0) || (rows == 0))
1492 return((Image *) NULL);
1495 (void) memset(distort_args,0,12*
sizeof(
double));
1496 distort_args[4]=(double) image->columns;
1497 distort_args[6]=(double) columns;
1498 distort_args[9]=(double) image->rows;
1499 distort_args[11]=(double) rows;
1501 vp_save=GetImageVirtualPixelMethod(image);
1503 tmp_image=CloneImage(image,0,0,MagickTrue,exception);
1504 if ( tmp_image == (Image *) NULL )
1505 return((Image *) NULL);
1506 (void) SetImageVirtualPixelMethod(tmp_image,TransparentVirtualPixelMethod);
1508 if (image->matte == MagickFalse)
1513 (void) SetImageAlphaChannel(tmp_image,SetAlphaChannel);
1514 resize_image=DistortImage(tmp_image,AffineDistortion,12,distort_args,
1515 MagickTrue,exception),
1517 tmp_image=DestroyImage(tmp_image);
1518 if ( resize_image == (Image *) NULL )
1519 return((Image *) NULL);
1521 (void) SetImageAlphaChannel(resize_image,DeactivateAlphaChannel);
1522 InheritException(exception,&image->exception);
1535 (void) SeparateImageChannel(tmp_image,TrueAlphaChannel);
1536 (void) SetImageAlphaChannel(tmp_image,OpaqueAlphaChannel);
1537 resize_alpha=DistortImage(tmp_image,AffineDistortion,12,distort_args,
1538 MagickTrue,exception),
1539 tmp_image=DestroyImage(tmp_image);
1540 if ( resize_alpha == (Image *) NULL )
1541 return((Image *) NULL);
1544 tmp_image=CloneImage(image,0,0,MagickTrue,exception);
1545 if ( tmp_image == (Image *) NULL )
1546 return((Image *) NULL);
1547 (void) SetImageVirtualPixelMethod(tmp_image,
1548 TransparentVirtualPixelMethod);
1549 (void) SetImageVirtualPixelMethod(tmp_image,
1550 TransparentVirtualPixelMethod);
1551 resize_image=DistortImage(tmp_image,AffineDistortion,12,distort_args,
1552 MagickTrue,exception),
1553 tmp_image=DestroyImage(tmp_image);
1554 if ( resize_image == (Image *) NULL)
1556 resize_alpha=DestroyImage(resize_alpha);
1557 return((Image *) NULL);
1561 (void) SetImageAlphaChannel(resize_image,DeactivateAlphaChannel);
1562 (void) SetImageAlphaChannel(resize_alpha,DeactivateAlphaChannel);
1563 (void) CompositeImage(resize_image,CopyOpacityCompositeOp,resize_alpha,
1565 InheritException(exception,&resize_image->exception);
1566 resize_image->matte=image->matte;
1567 resize_image->compose=image->compose;
1568 resize_alpha=DestroyImage(resize_alpha);
1570 (void) SetImageVirtualPixelMethod(resize_image,vp_save);
1575 crop_area.width=columns;
1576 crop_area.height=rows;
1580 tmp_image=resize_image;
1581 resize_image=CropImage(tmp_image,&crop_area,exception);
1582 tmp_image=DestroyImage(tmp_image);
1583 if (resize_image != (Image *) NULL)
1585 resize_image->page.width=0;
1586 resize_image->page.height=0;
1588 return(resize_image);
1680MagickExport Image *DistortImage(
const Image *image,DistortImageMethod method,
1681 const size_t number_arguments,
const double *arguments,
1682 MagickBooleanType bestfit,ExceptionInfo *exception)
1684#define DistortImageTag "Distort/Image"
1699 assert(image != (Image *) NULL);
1700 assert(image->signature == MagickCoreSignature);
1701 assert(exception != (ExceptionInfo *) NULL);
1702 assert(exception->signature == MagickCoreSignature);
1703 if (IsEventLogging() != MagickFalse)
1704 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1708 if (method == ResizeDistortion)
1710 if (number_arguments != 2)
1712 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1713 "InvalidArgument",
"%s : '%s'",
"Resize",
1714 "Invalid number of args: 2 only");
1715 return((Image *) NULL);
1717 distort_image=DistortResizeImage(image,CastDoubleToSizeT(arguments[0]),
1718 CastDoubleToSizeT(arguments[1]),exception);
1719 return(distort_image);
1729 coeff=GenerateCoefficients(image,&method,number_arguments,arguments,0,
1731 if (coeff == (
double *) NULL)
1732 return((Image *) NULL);
1740 geometry.width=image->columns;
1741 geometry.height=image->rows;
1745 if ( method == ArcDistortion ) {
1746 bestfit = MagickTrue;
1755 fix_bounds = MagickTrue;
1757 s.x=s.y=min.x=max.x=min.y=max.y=0.0;
1760#define InitalBounds(p) \
1763 min.x = max.x = p.x; \
1764 min.y = max.y = p.y; \
1766#define ExpandBounds(p) \
1769 min.x = MagickMin(min.x,p.x); \
1770 max.x = MagickMax(max.x,p.x); \
1771 min.y = MagickMin(min.y,p.y); \
1772 max.y = MagickMax(max.y,p.y); \
1777 case AffineDistortion:
1778 {
double inverse[6];
1779 InvertAffineCoefficients(coeff, inverse);
1780 s.x = (double) image->page.x;
1781 s.y = (double) image->page.y;
1782 d.x = inverse[0]*s.x+inverse[1]*s.y+inverse[2];
1783 d.y = inverse[3]*s.x+inverse[4]*s.y+inverse[5];
1785 s.x = (double) image->page.x+image->columns;
1786 s.y = (double) image->page.y;
1787 d.x = inverse[0]*s.x+inverse[1]*s.y+inverse[2];
1788 d.y = inverse[3]*s.x+inverse[4]*s.y+inverse[5];
1790 s.x = (double) image->page.x;
1791 s.y = (double) image->page.y+image->rows;
1792 d.x = inverse[0]*s.x+inverse[1]*s.y+inverse[2];
1793 d.y = inverse[3]*s.x+inverse[4]*s.y+inverse[5];
1795 s.x = (double) image->page.x+image->columns;
1796 s.y = (double) image->page.y+image->rows;
1797 d.x = inverse[0]*s.x+inverse[1]*s.y+inverse[2];
1798 d.y = inverse[3]*s.x+inverse[4]*s.y+inverse[5];
1802 case PerspectiveDistortion:
1803 {
double inverse[8], scale;
1804 InvertPerspectiveCoefficients(coeff, inverse);
1805 s.x = (double) image->page.x;
1806 s.y = (double) image->page.y;
1807 scale=inverse[6]*s.x+inverse[7]*s.y+1.0;
1808 scale=MagickSafeReciprocal(scale);
1809 d.x = scale*(inverse[0]*s.x+inverse[1]*s.y+inverse[2]);
1810 d.y = scale*(inverse[3]*s.x+inverse[4]*s.y+inverse[5]);
1812 s.x = (double) image->page.x+image->columns;
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;
1820 s.y = (double) image->page.y+image->rows;
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+image->columns;
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]);
1838 a = coeff[0]-coeff[1]/2; ca = cos(a); sa = sin(a);
1842 d.x = (coeff[2]-coeff[3])*ca;
1843 d.y = (coeff[2]-coeff[3])*sa;
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;
1853 for( a=(
double) (ceil((
double) ((coeff[0]-coeff[1]/2.0)/MagickPI2))*MagickPI2);
1854 a<(coeff[0]+coeff[1]/2.0); a+=MagickPI2 ) {
1855 ca = cos(a); sa = sin(a);
1865 coeff[1] = (double) (Magick2PI*image->columns/coeff[1]);
1866 coeff[3] = (double)image->rows/coeff[3];
1869 case PolarDistortion:
1871 if (number_arguments < 2)
1872 coeff[2] = coeff[3] = 0.0;
1873 min.x = coeff[2]-coeff[0];
1874 max.x = coeff[2]+coeff[0];
1875 min.y = coeff[3]-coeff[0];
1876 max.y = coeff[3]+coeff[0];
1878 coeff[7]=(double) geometry.height/(coeff[0]-coeff[1]);
1881 case DePolarDistortion:
1885 fix_bounds = MagickFalse;
1886 geometry.x = geometry.y = 0;
1887 geometry.height = CastDoubleToSizeT(ceil(coeff[0]-coeff[1]));
1888 geometry.width = CastDoubleToSizeT(ceil((coeff[0]-coeff[1])*
1889 (coeff[5]-coeff[4])*0.5));
1891 coeff[6]=(coeff[5]-coeff[4])*MagickSafeReciprocal(geometry.width);
1892 coeff[7]=(coeff[0]-coeff[1])*MagickSafeReciprocal(geometry.height);
1895 case Cylinder2PlaneDistortion:
1900 geometry.x = geometry.y = 0;
1901 geometry.width = CastDoubleToSizeT(ceil( 2.0*coeff[1]*tan(coeff[0]/2.0) ));
1902 geometry.height = CastDoubleToSizeT(ceil( 2.0*coeff[3]/cos(coeff[0]/2.0) ));
1904 coeff[4] = (double) geometry.width/2.0;
1905 coeff[5] = (double) geometry.height/2.0;
1906 fix_bounds = MagickFalse;
1909 case Plane2CylinderDistortion:
1913 geometry.x = geometry.y = 0;
1914 geometry.width = CastDoubleToSizeT(ceil(coeff[0]*coeff[1]));
1915 geometry.height = CastDoubleToSizeT(2.0*coeff[3]);
1917 coeff[4] = (double) geometry.width/2.0;
1918 coeff[5] = (double) geometry.height/2.0;
1919 fix_bounds = MagickFalse;
1923 case ShepardsDistortion:
1924 case BilinearForwardDistortion:
1925 case BilinearReverseDistortion:
1927 case QuadrilateralDistortion:
1929 case PolynomialDistortion:
1930 case BarrelDistortion:
1931 case BarrelInverseDistortion:
1934 bestfit = MagickFalse;
1935 fix_bounds = MagickFalse;
1944 geometry.x = (ssize_t) floor(min.x-0.5);
1945 geometry.y = (ssize_t) floor(min.y-0.5);
1946 geometry.width=CastDoubleToSizeT(ceil(max.x-geometry.x+0.5));
1947 geometry.height=CastDoubleToSizeT(ceil(max.y-geometry.y+0.5));
1956 {
const char *artifact=GetImageArtifact(image,
"distort:viewport");
1957 viewport_given = MagickFalse;
1958 if ( artifact != (
const char *) NULL ) {
1959 MagickStatusType flags=ParseAbsoluteGeometry(artifact,&geometry);
1961 (void) ThrowMagickException(exception,GetMagickModule(),
1962 OptionWarning,
"InvalidGeometry",
"`%s' `%s'",
1963 "distort:viewport",artifact);
1965 viewport_given = MagickTrue;
1970 if ( GetImageArtifact(image,
"verbose") != (
const char *) NULL ) {
1973 char image_gen[MaxTextExtent];
1977 if ( bestfit || viewport_given ) {
1978 (void) FormatLocaleString(image_gen, MaxTextExtent,
" -size %.20gx%.20g "
1979 "-page %+.20g%+.20g xc: +insert \\\n",(
double) geometry.width,
1980 (
double) geometry.height,(
double) geometry.x,(
double) geometry.y);
1981 lookup=
"v.p{ xx-v.page.x-.5, yy-v.page.y-.5 }";
1984 image_gen[0] =
'\0';
1985 lookup =
"p{ xx-page.x-.5, yy-page.y-.5 }";
1990 case AffineDistortion:
1995 inverse=(
double *) AcquireQuantumMemory(6,
sizeof(*inverse));
1996 if (inverse == (
double *) NULL)
1998 coeff=(
double *) RelinquishMagickMemory(coeff);
1999 (void) ThrowMagickException(exception,GetMagickModule(),
2000 ResourceLimitError,
"MemoryAllocationFailed",
"%s",
"DistortImages");
2001 return((Image *) NULL);
2003 InvertAffineCoefficients(coeff, inverse);
2004 CoefficientsToAffineArgs(inverse);
2005 (void) FormatLocaleFile(stderr,
"Affine Projection:\n");
2006 (void) FormatLocaleFile(stderr,
2007 " -distort AffineProjection \\\n '");
2008 for (i=0; i < 5; i++)
2009 (
void) FormatLocaleFile(stderr,
"%lf,", inverse[i]);
2010 (void) FormatLocaleFile(stderr,
"%lf'\n", inverse[5]);
2011 inverse=(
double *) RelinquishMagickMemory(inverse);
2012 (void) FormatLocaleFile(stderr,
"Affine Distort, FX Equivelent:\n");
2013 (void) FormatLocaleFile(stderr,
"%s", image_gen);
2014 (void) FormatLocaleFile(stderr,
2015 " -fx 'ii=i+page.x+0.5; jj=j+page.y+0.5;\n");
2016 (void) FormatLocaleFile(stderr,
" xx=%+lf*ii %+lf*jj %+lf;\n",
2017 coeff[0],coeff[1],coeff[2]);
2018 (void) FormatLocaleFile(stderr,
" yy=%+lf*ii %+lf*jj %+lf;\n",
2019 coeff[3],coeff[4],coeff[5]);
2020 (void) FormatLocaleFile(stderr,
" %s' \\\n",lookup);
2023 case PerspectiveDistortion:
2028 inverse=(
double *) AcquireQuantumMemory(8,
sizeof(*inverse));
2029 if (inverse == (
double *) NULL)
2031 coeff=(
double *) RelinquishMagickMemory(coeff);
2032 (void) ThrowMagickException(exception,GetMagickModule(),
2033 ResourceLimitError,
"MemoryAllocationFailed",
"%s",
2034 "DistortCoefficients");
2035 return((Image *) NULL);
2037 InvertPerspectiveCoefficients(coeff, inverse);
2038 (void) FormatLocaleFile(stderr,
"Perspective Projection:\n");
2039 (void) FormatLocaleFile(stderr,
2040 " -distort PerspectiveProjection \\\n '");
2041 for (i=0; i < 4; i++)
2042 (
void) FormatLocaleFile(stderr,
"%.*g, ",GetMagickPrecision(),
2044 (void) FormatLocaleFile(stderr,
"\n ");
2046 (
void) FormatLocaleFile(stderr,
"%.*g, ",GetMagickPrecision(),
2048 (void) FormatLocaleFile(stderr,
"%.*g'\n",GetMagickPrecision(),
2050 inverse=(
double *) RelinquishMagickMemory(inverse);
2051 (void) FormatLocaleFile(stderr,
"Perspective Distort, FX Equivalent:\n");
2052 (void) FormatLocaleFile(stderr,
"%.1024s",image_gen);
2053 (void) FormatLocaleFile(stderr,
2054 " -fx 'ii=i+page.x+0.5; jj=j+page.y+0.5;\n");
2055 (void) FormatLocaleFile(stderr,
" rr=%+.*g*ii %+.*g*jj + 1;\n",
2056 GetMagickPrecision(),coeff[6],GetMagickPrecision(),coeff[7]);
2057 (void) FormatLocaleFile(stderr,
2058 " xx=(%+.*g*ii %+.*g*jj %+.*g)/rr;\n",
2059 GetMagickPrecision(),coeff[0],GetMagickPrecision(),coeff[1],
2060 GetMagickPrecision(),coeff[2]);
2061 (void) FormatLocaleFile(stderr,
2062 " yy=(%+.*g*ii %+.*g*jj %+.*g)/rr;\n",
2063 GetMagickPrecision(),coeff[3],GetMagickPrecision(),coeff[4],
2064 GetMagickPrecision(),coeff[5]);
2065 (void) FormatLocaleFile(stderr,
" rr%s0 ? %s : blue' \\\n",
2066 coeff[8] < 0.0 ?
"<" :
">", lookup);
2069 case BilinearForwardDistortion:
2071 (void) FormatLocaleFile(stderr,
"BilinearForward Mapping Equations:\n");
2072 (void) FormatLocaleFile(stderr,
"%s", image_gen);
2073 (void) FormatLocaleFile(stderr,
" i = %+lf*x %+lf*y %+lf*x*y %+lf;\n",
2074 coeff[0],coeff[1],coeff[2],coeff[3]);
2075 (void) FormatLocaleFile(stderr,
" j = %+lf*x %+lf*y %+lf*x*y %+lf;\n",
2076 coeff[4],coeff[5],coeff[6],coeff[7]);
2079 (void) FormatLocaleFile(stderr,
" c8 = %+lf c9 = 2*a = %+lf;\n",
2080 coeff[8], coeff[9]);
2082 (void) FormatLocaleFile(stderr,
2083 "BilinearForward Distort, FX Equivalent:\n");
2084 (void) FormatLocaleFile(stderr,
"%s", image_gen);
2085 (void) FormatLocaleFile(stderr,
2086 " -fx 'ii=i+page.x%+lf; jj=j+page.y%+lf;\n",0.5-coeff[3],0.5-
2088 (void) FormatLocaleFile(stderr,
" bb=%lf*ii %+lf*jj %+lf;\n",
2089 coeff[6], -coeff[2], coeff[8]);
2093 (void) FormatLocaleFile(stderr,
2094 " rt=bb*bb %+lf*(%lf*ii%+lf*jj);\n",-2*coeff[9],coeff[4],
2096 (void) FormatLocaleFile(stderr,
2097 " yy=( -bb + sqrt(rt) ) / %lf;\n",coeff[9]);
2100 (
void) FormatLocaleFile(stderr,
" yy=(%lf*ii%+lf*jj)/bb;\n",
2101 -coeff[4],coeff[0]);
2102 (void) FormatLocaleFile(stderr,
2103 " xx=(ii %+lf*yy)/(%lf %+lf*yy);\n",-coeff[1],coeff[0],
2105 if ( coeff[9] != 0 )
2106 (void) FormatLocaleFile(stderr,
" (rt < 0 ) ? red : %s'\n",
2109 (
void) FormatLocaleFile(stderr,
" %s' \\\n", lookup);
2112 case BilinearReverseDistortion:
2115 (void) FormatLocaleFile(stderr,
"Polynomial Projection Distort:\n");
2116 (void) FormatLocaleFile(stderr,
" -distort PolynomialProjection \\\n");
2117 (void) FormatLocaleFile(stderr,
" '1.5, %lf, %lf, %lf, %lf,\n",
2118 coeff[3], coeff[0], coeff[1], coeff[2]);
2119 (void) FormatLocaleFile(stderr,
" %lf, %lf, %lf, %lf'\n",
2120 coeff[7], coeff[4], coeff[5], coeff[6]);
2122 (void) FormatLocaleFile(stderr,
2123 "BilinearReverse Distort, FX Equivalent:\n");
2124 (void) FormatLocaleFile(stderr,
"%s", image_gen);
2125 (void) FormatLocaleFile(stderr,
2126 " -fx 'ii=i+page.x+0.5; jj=j+page.y+0.5;\n");
2127 (void) FormatLocaleFile(stderr,
2128 " xx=%+lf*ii %+lf*jj %+lf*ii*jj %+lf;\n",coeff[0],coeff[1],
2129 coeff[2], coeff[3]);
2130 (void) FormatLocaleFile(stderr,
2131 " yy=%+lf*ii %+lf*jj %+lf*ii*jj %+lf;\n",coeff[4],coeff[5],
2132 coeff[6], coeff[7]);
2133 (void) FormatLocaleFile(stderr,
" %s' \\\n", lookup);
2136 case PolynomialDistortion:
2138 size_t nterms = CastDoubleToSizeT(coeff[1]);
2139 (void) FormatLocaleFile(stderr,
2140 "Polynomial (order %lg, terms %lu), FX Equivalent\n",coeff[0],
2141 (
unsigned long) nterms);
2142 (void) FormatLocaleFile(stderr,
"%s", 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,
" xx =");
2146 for (i=0; i < (ssize_t) nterms; i++)
2148 if ((i != 0) && (i%4 == 0))
2149 (void) FormatLocaleFile(stderr,
"\n ");
2150 (void) FormatLocaleFile(stderr,
" %+lf%s",coeff[2+i],
2153 (void) FormatLocaleFile(stderr,
";\n yy =");
2154 for (i=0; i < (ssize_t) nterms; i++)
2156 if ((i != 0) && (i%4 == 0))
2157 (void) FormatLocaleFile(stderr,
"\n ");
2158 (void) FormatLocaleFile(stderr,
" %+lf%s",coeff[2+i+nterms],
2161 (void) FormatLocaleFile(stderr,
";\n %s' \\\n", lookup);
2166 (void) FormatLocaleFile(stderr,
"Arc Distort, Internal Coefficients:\n");
2167 for (i=0; i < 5; i++)
2168 (
void) FormatLocaleFile(stderr,
2169 " c%.20g = %+lf\n",(
double) i,coeff[i]);
2170 (void) FormatLocaleFile(stderr,
"Arc Distort, FX Equivalent:\n");
2171 (void) FormatLocaleFile(stderr,
"%s", image_gen);
2172 (void) FormatLocaleFile(stderr,
" -fx 'ii=i+page.x; jj=j+page.y;\n");
2173 (void) FormatLocaleFile(stderr,
" xx=(atan2(jj,ii)%+lf)/(2*pi);\n",
2175 (void) FormatLocaleFile(stderr,
" xx=xx-round(xx);\n");
2176 (void) FormatLocaleFile(stderr,
" xx=xx*%lf %+lf;\n",coeff[1],
2178 (void) FormatLocaleFile(stderr,
2179 " yy=(%lf - hypot(ii,jj)) * %lf;\n",coeff[2],coeff[3]);
2180 (void) FormatLocaleFile(stderr,
" v.p{xx-.5,yy-.5}' \\\n");
2183 case PolarDistortion:
2185 (void) FormatLocaleFile(stderr,
"Polar Distort, Internal Coefficients\n");
2186 for (i=0; i < 8; i++)
2187 (
void) FormatLocaleFile(stderr,
" c%.20g = %+lf\n",(
double) i,
2189 (void) FormatLocaleFile(stderr,
"Polar Distort, FX Equivalent:\n");
2190 (void) FormatLocaleFile(stderr,
"%s", image_gen);
2191 (void) FormatLocaleFile(stderr,
2192 " -fx 'ii=i+page.x%+lf; jj=j+page.y%+lf;\n",-coeff[2],-coeff[3]);
2193 (void) FormatLocaleFile(stderr,
" xx=(atan2(ii,jj)%+lf)/(2*pi);\n",
2194 -(coeff[4]+coeff[5])/2 );
2195 (void) FormatLocaleFile(stderr,
" xx=xx-round(xx);\n");
2196 (void) FormatLocaleFile(stderr,
" xx=xx*2*pi*%lf + v.w/2;\n",
2198 (void) FormatLocaleFile(stderr,
" yy=(hypot(ii,jj)%+lf)*%lf;\n",
2199 -coeff[1],coeff[7] );
2200 (void) FormatLocaleFile(stderr,
" v.p{xx-.5,yy-.5}' \\\n");
2203 case DePolarDistortion:
2205 (void) FormatLocaleFile(stderr,
2206 "DePolar Distort, Internal Coefficients\n");
2207 for (i=0; i < 8; i++)
2208 (
void) FormatLocaleFile(stderr,
" c%.20g = %+lf\n",(
double) i,
2210 (void) FormatLocaleFile(stderr,
"DePolar Distort, FX Equivalent:\n");
2211 (void) FormatLocaleFile(stderr,
"%s", image_gen);
2212 (void) FormatLocaleFile(stderr,
" -fx 'aa=(i+.5)*%lf %+lf;\n",
2213 coeff[6],+coeff[4]);
2214 (void) FormatLocaleFile(stderr,
" rr=(j+.5)*%lf %+lf;\n",
2215 coeff[7],+coeff[1]);
2216 (void) FormatLocaleFile(stderr,
" xx=rr*sin(aa) %+lf;\n",
2218 (void) FormatLocaleFile(stderr,
" yy=rr*cos(aa) %+lf;\n",
2220 (void) FormatLocaleFile(stderr,
" v.p{xx-.5,yy-.5}' \\\n");
2223 case Cylinder2PlaneDistortion:
2225 (void) FormatLocaleFile(stderr,
2226 "Cylinder to Plane Distort, Internal Coefficients\n");
2227 (void) FormatLocaleFile(stderr,
" cylinder_radius = %+lf\n",coeff[1]);
2228 (void) FormatLocaleFile(stderr,
2229 "Cylinder to Plane Distort, FX Equivalent:\n");
2230 (void) FormatLocaleFile(stderr,
"%s", image_gen);
2231 (void) FormatLocaleFile(stderr,
2232 " -fx 'ii=i+page.x%+lf+0.5; jj=j+page.y%+lf+0.5;\n",-coeff[4],
2234 (void) FormatLocaleFile(stderr,
" aa=atan(ii/%+lf);\n",coeff[1]);
2235 (void) FormatLocaleFile(stderr,
" xx=%lf*aa%+lf;\n",
2237 (void) FormatLocaleFile(stderr,
" yy=jj*cos(aa)%+lf;\n",coeff[3]);
2238 (void) FormatLocaleFile(stderr,
" %s' \\\n", lookup);
2241 case Plane2CylinderDistortion:
2243 (void) FormatLocaleFile(stderr,
2244 "Plane to Cylinder Distort, Internal Coefficients\n");
2245 (void) FormatLocaleFile(stderr,
" cylinder_radius = %+lf\n",coeff[1]);
2246 (void) FormatLocaleFile(stderr,
2247 "Plane to Cylinder Distort, FX Equivalent:\n");
2248 (void) FormatLocaleFile(stderr,
"%s", image_gen);
2249 (void) FormatLocaleFile(stderr,
2250 " -fx 'ii=i+page.x%+lf+0.5; jj=j+page.y%+lf+0.5;\n",-coeff[4],
2252 (void) FormatLocaleFile(stderr,
" ii=ii/%+lf;\n",coeff[1]);
2253 (void) FormatLocaleFile(stderr,
" xx=%lf*tan(ii)%+lf;\n",coeff[1],
2255 (void) FormatLocaleFile(stderr,
" yy=jj/cos(ii)%+lf;\n",coeff[3]);
2256 (void) FormatLocaleFile(stderr,
" %s' \\\n", lookup);
2259 case BarrelDistortion:
2260 case BarrelInverseDistortion:
2271 xc=((double)image->columns-1.0)/2.0+image->page.x;
2272 yc=((double)image->rows-1.0)/2.0+image->page.y;
2273 (void) FormatLocaleFile(stderr,
"Barrel%s Distort, FX Equivalent:\n",
2274 method == BarrelDistortion ?
"" :
"Inv");
2275 (void) FormatLocaleFile(stderr,
"%s", image_gen);
2276 if ( fabs(coeff[8]-xc-0.5) < 0.1 && fabs(coeff[9]-yc-0.5) < 0.1 )
2277 (void) FormatLocaleFile(stderr,
" -fx 'xc=(w-1)/2; yc=(h-1)/2;\n");
2279 (
void) FormatLocaleFile(stderr,
" -fx 'xc=%lf; yc=%lf;\n",coeff[8]-
2281 (void) FormatLocaleFile(stderr,
2282 " ii=i-xc; jj=j-yc; rr=hypot(ii,jj);\n");
2283 (void) FormatLocaleFile(stderr,
2284 " ii=ii%s(%lf*rr*rr*rr %+lf*rr*rr %+lf*rr %+lf);\n",
2285 method == BarrelDistortion ?
"*" :
"/",coeff[0],coeff[1],coeff[2],
2287 (void) FormatLocaleFile(stderr,
2288 " jj=jj%s(%lf*rr*rr*rr %+lf*rr*rr %+lf*rr %+lf);\n",
2289 method == BarrelDistortion ?
"*" :
"/",coeff[4],coeff[5],coeff[6],
2291 (void) FormatLocaleFile(stderr,
" p{ii+xc,jj+yc}' \\\n");
2302 {
const char *artifact;
2303 artifact=GetImageArtifact(image,
"distort:scale");
2304 output_scaling = 1.0;
2305 if (artifact != (
const char *) NULL) {
2306 output_scaling = fabs(StringToDouble(artifact,(
char **) NULL));
2307 geometry.width=CastDoubleToSizeT(output_scaling*geometry.width+0.5);
2308 geometry.height=CastDoubleToSizeT(output_scaling*geometry.height+0.5);
2309 geometry.x=CastDoubleToSsizeT(output_scaling*geometry.x+0.5);
2310 geometry.y=CastDoubleToSsizeT(output_scaling*geometry.y+0.5);
2311 if ( output_scaling < 0.1 ) {
2312 coeff = (
double *) RelinquishMagickMemory(coeff);
2313 (void) ThrowMagickException(exception,GetMagickModule(),
2314 OptionError,
"InvalidArgument",
"%s",
"-define distort:scale" );
2315 return((Image *) NULL);
2317 output_scaling = 1/output_scaling;
2320#define ScaleFilter(F,A,B,C,D) \
2321 ScaleResampleFilter( (F), \
2322 output_scaling*(A), output_scaling*(B), \
2323 output_scaling*(C), output_scaling*(D) )
2328 distort_image=CloneImage(image,geometry.width,geometry.height,MagickTrue,
2330 if (distort_image == (Image *) NULL)
2332 coeff=(
double *) RelinquishMagickMemory(coeff);
2333 return((Image *) NULL);
2336 if (SetImageStorageClass(distort_image,DirectClass) == MagickFalse)
2338 coeff=(
double *) RelinquishMagickMemory(coeff);
2339 InheritException(exception,&distort_image->exception);
2340 distort_image=DestroyImage(distort_image);
2341 return((Image *) NULL);
2343 if ((IsPixelGray(&distort_image->background_color) == MagickFalse) &&
2344 (IsGrayColorspace(distort_image->colorspace) != MagickFalse))
2345 (void) SetImageColorspace(distort_image,sRGBColorspace);
2346 if (distort_image->background_color.opacity != OpaqueOpacity)
2347 distort_image->matte=MagickTrue;
2348 distort_image->page.x=geometry.x;
2349 distort_image->page.y=geometry.y;
2367 **magick_restrict resample_filter;
2374 GetMagickPixelPacket(distort_image,&zero);
2375 resample_filter=AcquireResampleFilterTLS(image,UndefinedVirtualPixelMethod,
2376 MagickFalse,exception);
2377 distort_view=AcquireAuthenticCacheView(distort_image,exception);
2378#if defined(MAGICKCORE_OPENMP_SUPPORT)
2379 #pragma omp parallel for schedule(static) shared(progress,status) \
2380 magick_number_threads(image,distort_image,distort_image->rows,1)
2382 for (j=0; j < (ssize_t) distort_image->rows; j++)
2385 id = GetOpenMPThreadId();
2402 *magick_restrict indexes;
2410 q=QueueCacheViewAuthenticPixels(distort_view,0,j,distort_image->columns,1,
2412 if (q == (PixelPacket *) NULL)
2417 indexes=GetCacheViewAuthenticIndexQueue(distort_view);
2425 case AffineDistortion:
2426 ScaleFilter( resample_filter[
id],
2428 coeff[3], coeff[4] );
2441 GetMagickPixelPacket(distort_image,&invalid);
2442 SetMagickPixelPacket(distort_image,&distort_image->matte_color,
2443 (IndexPacket *) NULL, &invalid);
2444 if (distort_image->colorspace == CMYKColorspace)
2445 ConvertRGBToCMYK(&invalid);
2447 for (i=0; i < (ssize_t) distort_image->columns; i++)
2450 d.x = (double) (geometry.x+i+0.5)*output_scaling;
2451 d.y = (double) (geometry.y+j+0.5)*output_scaling;
2455 case AffineDistortion:
2457 s.x=coeff[0]*d.x+coeff[1]*d.y+coeff[2];
2458 s.y=coeff[3]*d.x+coeff[4]*d.y+coeff[5];
2462 case PerspectiveDistortion:
2465 p,q,r,abs_r,abs_c6,abs_c7,scale;
2467 p=coeff[0]*d.x+coeff[1]*d.y+coeff[2];
2468 q=coeff[3]*d.x+coeff[4]*d.y+coeff[5];
2469 r=coeff[6]*d.x+coeff[7]*d.y+1.0;
2471 validity = (r*coeff[8] < 0.0) ? 0.0 : 1.0;
2474 abs_c6 = fabs(coeff[6]);
2475 abs_c7 = fabs(coeff[7]);
2476 if ( abs_c6 > abs_c7 ) {
2477 if ( abs_r < abs_c6*output_scaling )
2478 validity = 0.5 - coeff[8]*r/(coeff[6]*output_scaling);
2480 else if ( abs_r < abs_c7*output_scaling )
2481 validity = 0.5 - coeff[8]*r/(coeff[7]*output_scaling);
2483 if ( validity > 0.0 ) {
2490 ScaleFilter( resample_filter[
id],
2491 (r*coeff[0] - p*coeff[6])*scale,
2492 (r*coeff[1] - p*coeff[7])*scale,
2493 (r*coeff[3] - q*coeff[6])*scale,
2494 (r*coeff[4] - q*coeff[7])*scale );
2498 case BilinearReverseDistortion:
2501 s.x=coeff[0]*d.x+coeff[1]*d.y+coeff[2]*d.x*d.y+coeff[3];
2502 s.y=coeff[4]*d.x+coeff[5]*d.y
2503 +coeff[6]*d.x*d.y+coeff[7];
2505 ScaleFilter( resample_filter[
id],
2506 coeff[0] + coeff[2]*d.y,
2507 coeff[1] + coeff[2]*d.x,
2508 coeff[4] + coeff[6]*d.y,
2509 coeff[5] + coeff[6]*d.x );
2512 case BilinearForwardDistortion:
2517 d.x -= coeff[3]; d.y -= coeff[7];
2518 b = coeff[6]*d.x - coeff[2]*d.y + coeff[8];
2519 c = coeff[4]*d.x - coeff[0]*d.y;
2524 if ( fabs(coeff[9]) < MagickEpsilon )
2527 c = b*b - 2*coeff[9]*c;
2531 s.y = ( -b + sqrt(c) )/coeff[9];
2533 if ( validity > 0.0 )
2534 s.x = ( d.x - coeff[1]*s.y) / ( coeff[0] + coeff[2]*s.y );
2544 case BilinearDistortion:
2549 case PolynomialDistortion:
2556 nterms=(ssize_t)coeff[1];
2561 s.x=s.y=du.x=du.y=dv.x=dv.y=0.0;
2562 for(k=0; k < nterms; k++) {
2563 s.x += poly_basis_fn(k,d.x,d.y)*coeff[2+k];
2564 du.x += poly_basis_dx(k,d.x,d.y)*coeff[2+k];
2565 du.y += poly_basis_dy(k,d.x,d.y)*coeff[2+k];
2566 s.y += poly_basis_fn(k,d.x,d.y)*coeff[2+k+nterms];
2567 dv.x += poly_basis_dx(k,d.x,d.y)*coeff[2+k+nterms];
2568 dv.y += poly_basis_dy(k,d.x,d.y)*coeff[2+k+nterms];
2570 ScaleFilter( resample_filter[
id], du.x,du.y,dv.x,dv.y );
2576 s.x = (double) ((atan2(d.y,d.x) - coeff[0])/Magick2PI);
2577 s.x -= MagickRound(s.x);
2578 s.y = hypot(d.x,d.y);
2585 if ( s.y > MagickEpsilon )
2586 ScaleFilter( resample_filter[
id],
2587 (
double) (coeff[1]/(Magick2PI*s.y)), 0, 0, coeff[3] );
2589 ScaleFilter( resample_filter[
id],
2590 distort_image->columns*2, 0, 0, coeff[3] );
2593 s.x = s.x*coeff[1] + coeff[4] + image->page.x +0.5;
2594 s.y = (coeff[2] - s.y) * coeff[3] + image->page.y;
2597 case PolarDistortion:
2601 s.x = atan2(d.x,d.y) - (coeff[4]+coeff[5])/2;
2603 s.x -= MagickRound(s.x);
2605 s.y = hypot(d.x,d.y);
2610 if ( s.y > MagickEpsilon )
2611 ScaleFilter( resample_filter[
id],
2612 (
double) (coeff[6]/(Magick2PI*s.y)), 0, 0, coeff[7] );
2614 ScaleFilter( resample_filter[
id],
2615 distort_image->columns*2, 0, 0, coeff[7] );
2618 s.x = s.x*coeff[6] + (double)image->columns/2.0 + image->page.x;
2619 s.y = (s.y-coeff[1])*coeff[7] + image->page.y;
2622 case DePolarDistortion:
2625 d.x = ((double)i+0.5)*output_scaling*coeff[6]+coeff[4];
2626 d.y = ((double)j+0.5)*output_scaling*coeff[7]+coeff[1];
2627 s.x = d.y*sin(d.x) + coeff[2];
2628 s.y = d.y*cos(d.x) + coeff[3];
2632 case Cylinder2PlaneDistortion:
2636 d.x -= coeff[4]; d.y -= coeff[5];
2643 ScaleFilter( resample_filter[
id],
2644 1.0/(1.0+d.x*d.x), 0.0, -d.x*s.y*cx*cx/coeff[1], s.y/d.y );
2646if ( i == 0 && j == 0 ) {
2647 fprintf(stderr,
"x=%lf y=%lf u=%lf v=%lf\n", d.x*coeff[1], d.y, s.x, s.y);
2648 fprintf(stderr,
"phi = %lf\n", (
double)(ax * 180.0/MagickPI) );
2649 fprintf(stderr,
"du/dx=%lf du/dx=%lf dv/dx=%lf dv/dy=%lf\n",
2650 1.0/(1.0+d.x*d.x), 0.0, -d.x*s.y*cx*cx/coeff[1], s.y/d.y );
2654 s.x += coeff[2]; s.y += coeff[3];
2657 case Plane2CylinderDistortion:
2660 d.x -= coeff[4]; d.y -= coeff[5];
2664 validity = (double) ((coeff[1]*MagickPI2 - fabs(d.x))/output_scaling + 0.5);
2666 if ( validity > 0.0 ) {
2674 ScaleFilter( resample_filter[
id],
2675 cx*cx, 0.0, s.y*cx/coeff[1], cx );
2678if ( d.x == 0.5 && d.y == 0.5 ) {
2679 fprintf(stderr,
"x=%lf y=%lf u=%lf v=%lf\n", d.x*coeff[1], d.y, s.x, s.y);
2680 fprintf(stderr,
"radius = %lf phi = %lf validity = %lf\n",
2681 coeff[1], (
double)(d.x * 180.0/MagickPI), validity );
2682 fprintf(stderr,
"du/dx=%lf du/dx=%lf dv/dx=%lf dv/dy=%lf\n",
2683 cx*cx, 0.0, s.y*cx/coeff[1], cx);
2688 s.x += coeff[2]; s.y += coeff[3];
2691 case BarrelDistortion:
2692 case BarrelInverseDistortion:
2694 double r,fx,fy,gx,gy;
2698 r = sqrt(d.x*d.x+d.y*d.y);
2699 if ( r > MagickEpsilon ) {
2700 fx = ((coeff[0]*r + coeff[1])*r + coeff[2])*r + coeff[3];
2701 fy = ((coeff[4]*r + coeff[5])*r + coeff[6])*r + coeff[7];
2702 gx = ((3*coeff[0]*r + 2*coeff[1])*r + coeff[2])/r;
2703 gy = ((3*coeff[4]*r + 2*coeff[5])*r + coeff[6])/r;
2705 if ( method == BarrelInverseDistortion ) {
2706 fx = 1/fx; fy = 1/fy;
2707 gx *= -fx*fx; gy *= -fy*fy;
2710 s.x = d.x*fx + coeff[8];
2711 s.y = d.y*fy + coeff[9];
2712 ScaleFilter( resample_filter[
id],
2713 gx*d.x*d.x + fx, gx*d.x*d.y,
2714 gy*d.x*d.y, gy*d.y*d.y + fy );
2723 if ( method == BarrelDistortion )
2724 ScaleFilter( resample_filter[
id],
2725 coeff[3], 0, 0, coeff[7] );
2728 ScaleFilter( resample_filter[
id],
2729 1.0/coeff[3], 0, 0, 1.0/coeff[7] );
2733 case ShepardsDistortion:
2748 denominator = s.x = s.y = 0;
2749 for(i=0; i<number_arguments; i+=4) {
2751 ((double)d.x-arguments[i+2])*((
double)d.x-arguments[i+2])
2752 + ((double)d.y-arguments[i+3])*((
double)d.y-arguments[i+3]);
2753 weight = pow(weight,coeff[0]);
2754 weight = ( weight < 1.0 ) ? 1.0 : 1.0/weight;
2756 s.x += (arguments[ i ]-arguments[i+2])*weight;
2757 s.y += (arguments[i+1]-arguments[i+3])*weight;
2758 denominator += weight;
2770 if ( bestfit && method != ArcDistortion ) {
2771 s.x -= image->page.x;
2772 s.y -= image->page.y;
2777 if ( validity <= 0.0 ) {
2779 SetPixelPacket(distort_image,&invalid,q,indexes);
2783 status=ResamplePixelColor(resample_filter[
id],s.x,s.y,&pixel);
2784 if (status == MagickFalse)
2785 SetPixelPacket(distort_image,&invalid,q,indexes);
2789 if ( validity < 1.0 ) {
2792 MagickPixelCompositeBlend(&pixel,validity,&invalid,(1.0-validity),
2796 SetPixelPacket(distort_image,&pixel,q,indexes);
2801 sync=SyncCacheViewAuthenticPixels(distort_view,exception);
2802 if (sync == MagickFalse)
2804 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2809#if defined(MAGICKCORE_OPENMP_SUPPORT)
2813 proceed=SetImageProgress(image,DistortImageTag,progress,image->rows);
2814 if (proceed == MagickFalse)
2818 distort_view=DestroyCacheView(distort_view);
2819 resample_filter=DestroyResampleFilterTLS(resample_filter);
2821 if (status == MagickFalse)
2822 distort_image=DestroyImage(distort_image);
2828 if ( method == ArcDistortion && !bestfit && !viewport_given ) {
2829 distort_image->page.x = 0;
2830 distort_image->page.y = 0;
2832 coeff=(
double *) RelinquishMagickMemory(coeff);
2833 return(distort_image);
2870MagickExport Image *RotateImage(
const Image *image,
const double degrees,
2871 ExceptionInfo *exception)
2889 assert(image != (Image *) NULL);
2890 assert(image->signature == MagickCoreSignature);
2891 assert(exception != (ExceptionInfo *) NULL);
2892 assert(exception->signature == MagickCoreSignature);
2893 if (IsEventLogging() != MagickFalse)
2894 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2895 angle=fmod(degrees,360.0);
2896 while (angle < -45.0)
2898 for (rotations=0; angle > 45.0; rotations++)
2901 shear.x=(-tan((
double) DegreesToRadians(angle)/2.0));
2902 shear.y=sin((
double) DegreesToRadians(angle));
2903 if ((fabs(shear.x) < MagickEpsilon) && (fabs(shear.y) < MagickEpsilon))
2904 return(IntegralRotateImage(image,rotations,exception));
2905 distort_image=CloneImage(image,0,0,MagickTrue,exception);
2906 if (distort_image == (Image *) NULL)
2907 return((Image *) NULL);
2908 (void) SetImageVirtualPixelMethod(distort_image,BackgroundVirtualPixelMethod);
2909 rotate_image=DistortImage(distort_image,ScaleRotateTranslateDistortion,1,
2910 °rees,MagickTrue,exception);
2911 distort_image=DestroyImage(distort_image);
2912 return(rotate_image);
2957MagickExport Image *SparseColorImage(
const Image *image,
2958 const ChannelType channel,
const SparseColorMethod method,
2959 const size_t number_arguments,
const double *arguments,
2960 ExceptionInfo *exception)
2962#define SparseColorTag "Distort/SparseColor"
2976 assert(image != (Image *) NULL);
2977 assert(image->signature == MagickCoreSignature);
2978 assert(exception != (ExceptionInfo *) NULL);
2979 assert(exception->signature == MagickCoreSignature);
2980 if (IsEventLogging() != MagickFalse)
2981 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2986 if ( channel & RedChannel ) number_colors++;
2987 if ( channel & GreenChannel ) number_colors++;
2988 if ( channel & BlueChannel ) number_colors++;
2989 if ( channel & IndexChannel ) number_colors++;
2990 if ( channel & OpacityChannel ) number_colors++;
2996 { DistortImageMethod
2999 distort_method=(DistortImageMethod) method;
3000 if ( distort_method >= SentinelDistortion )
3001 distort_method = ShepardsDistortion;
3002 coeff = GenerateCoefficients(image, &distort_method, number_arguments,
3003 arguments, number_colors, exception);
3004 if ( coeff == (
double *) NULL )
3005 return((Image *) NULL);
3012 sparse_method = (SparseColorMethod) distort_method;
3013 if ( distort_method == ShepardsDistortion )
3014 sparse_method = method;
3015 if ( sparse_method == InverseColorInterpolate )
3020 if ( GetImageArtifact(image,
"verbose") != (
const char *) NULL ) {
3022 switch (sparse_method) {
3023 case BarycentricColorInterpolate:
3026 (void) FormatLocaleFile(stderr,
"Barycentric Sparse Color:\n");
3027 if ( channel & RedChannel )
3028 (void) FormatLocaleFile(stderr,
" -channel R -fx '%+lf*i %+lf*j %+lf' \\\n",
3029 coeff[x], coeff[x+1], coeff[x+2]),x+=3;
3030 if ( channel & GreenChannel )
3031 (void) FormatLocaleFile(stderr,
" -channel G -fx '%+lf*i %+lf*j %+lf' \\\n",
3032 coeff[x], coeff[x+1], coeff[x+2]),x+=3;
3033 if ( channel & BlueChannel )
3034 (void) FormatLocaleFile(stderr,
" -channel B -fx '%+lf*i %+lf*j %+lf' \\\n",
3035 coeff[x], coeff[x+1], coeff[x+2]),x+=3;
3036 if ( channel & IndexChannel )
3037 (void) FormatLocaleFile(stderr,
" -channel K -fx '%+lf*i %+lf*j %+lf' \\\n",
3038 coeff[x], coeff[x+1], coeff[x+2]),x+=3;
3039 if ( channel & OpacityChannel )
3040 (void) FormatLocaleFile(stderr,
" -channel A -fx '%+lf*i %+lf*j %+lf' \\\n",
3041 coeff[x], coeff[x+1], coeff[x+2]),x+=3;
3044 case BilinearColorInterpolate:
3047 (void) FormatLocaleFile(stderr,
"Bilinear Sparse Color\n");
3048 if ( channel & RedChannel )
3049 (void) FormatLocaleFile(stderr,
" -channel R -fx '%+lf*i %+lf*j %+lf*i*j %+lf;\n",
3050 coeff[ x ], coeff[x+1],
3051 coeff[x+2], coeff[x+3]),x+=4;
3052 if ( channel & GreenChannel )
3053 (void) FormatLocaleFile(stderr,
" -channel G -fx '%+lf*i %+lf*j %+lf*i*j %+lf;\n",
3054 coeff[ x ], coeff[x+1],
3055 coeff[x+2], coeff[x+3]),x+=4;
3056 if ( channel & BlueChannel )
3057 (void) FormatLocaleFile(stderr,
" -channel B -fx '%+lf*i %+lf*j %+lf*i*j %+lf;\n",
3058 coeff[ x ], coeff[x+1],
3059 coeff[x+2], coeff[x+3]),x+=4;
3060 if ( channel & IndexChannel )
3061 (void) FormatLocaleFile(stderr,
" -channel K -fx '%+lf*i %+lf*j %+lf*i*j %+lf;\n",
3062 coeff[ x ], coeff[x+1],
3063 coeff[x+2], coeff[x+3]),x+=4;
3064 if ( channel & OpacityChannel )
3065 (void) FormatLocaleFile(stderr,
" -channel A -fx '%+lf*i %+lf*j %+lf*i*j %+lf;\n",
3066 coeff[ x ], coeff[x+1],
3067 coeff[x+2], coeff[x+3]),x+=4;
3082 sparse_image=CloneImage(image,0,0,MagickTrue,exception);
3083 if (sparse_image == (Image *) NULL)
3084 return((Image *) NULL);
3085 if (SetImageStorageClass(sparse_image,DirectClass) == MagickFalse)
3087 InheritException(exception,&image->exception);
3088 sparse_image=DestroyImage(sparse_image);
3089 return((Image *) NULL);
3106 sparse_view=AcquireAuthenticCacheView(sparse_image,exception);
3107#if defined(MAGICKCORE_OPENMP_SUPPORT)
3108 #pragma omp parallel for schedule(static) shared(progress,status) \
3109 magick_number_threads(image,sparse_image,sparse_image->rows,1)
3111 for (j=0; j < (ssize_t) sparse_image->rows; j++)
3120 *magick_restrict indexes;
3128 q=GetCacheViewAuthenticPixels(sparse_view,0,j,sparse_image->columns,
3130 if (q == (PixelPacket *) NULL)
3135 indexes=GetCacheViewAuthenticIndexQueue(sparse_view);
3136 GetMagickPixelPacket(sparse_image,&pixel);
3137 for (i=0; i < (ssize_t) image->columns; i++)
3139 SetMagickPixelPacket(image,q,indexes,&pixel);
3140 switch (sparse_method)
3142 case BarycentricColorInterpolate:
3145 if ( channel & RedChannel )
3146 pixel.red = coeff[x]*i +coeff[x+1]*j
3148 if ( channel & GreenChannel )
3149 pixel.green = coeff[x]*i +coeff[x+1]*j
3151 if ( channel & BlueChannel )
3152 pixel.blue = coeff[x]*i +coeff[x+1]*j
3154 if ( channel & IndexChannel )
3155 pixel.index = coeff[x]*i +coeff[x+1]*j
3157 if ( channel & OpacityChannel )
3158 pixel.opacity = coeff[x]*i +coeff[x+1]*j
3162 case BilinearColorInterpolate:
3165 if ( channel & RedChannel )
3166 pixel.red = coeff[x]*i + coeff[x+1]*j +
3167 coeff[x+2]*i*j + coeff[x+3], x+=4;
3168 if ( channel & GreenChannel )
3169 pixel.green = coeff[x]*i + coeff[x+1]*j +
3170 coeff[x+2]*i*j + coeff[x+3], x+=4;
3171 if ( channel & BlueChannel )
3172 pixel.blue = coeff[x]*i + coeff[x+1]*j +
3173 coeff[x+2]*i*j + coeff[x+3], x+=4;
3174 if ( channel & IndexChannel )
3175 pixel.index = coeff[x]*i + coeff[x+1]*j +
3176 coeff[x+2]*i*j + coeff[x+3], x+=4;
3177 if ( channel & OpacityChannel )
3178 pixel.opacity = coeff[x]*i + coeff[x+1]*j +
3179 coeff[x+2]*i*j + coeff[x+3], x+=4;
3182 case InverseColorInterpolate:
3183 case ShepardsColorInterpolate:
3190 if ( channel & RedChannel ) pixel.red = 0.0;
3191 if ( channel & GreenChannel ) pixel.green = 0.0;
3192 if ( channel & BlueChannel ) pixel.blue = 0.0;
3193 if ( channel & IndexChannel ) pixel.index = 0.0;
3194 if ( channel & OpacityChannel ) pixel.opacity = 0.0;
3196 for(k=0; k<number_arguments; k+=2+number_colors) {
3197 ssize_t x=(ssize_t) k+2;
3199 ((double)i-arguments[ k ])*((
double)i-arguments[ k ])
3200 + ((double)j-arguments[k+1])*((
double)j-arguments[k+1]);
3201 weight = pow(weight,coeff[0]);
3202 weight = ( weight < 1.0 ) ? 1.0 : 1.0/weight;
3203 if ( channel & RedChannel )
3204 pixel.red += arguments[x++]*weight;
3205 if ( channel & GreenChannel )
3206 pixel.green += arguments[x++]*weight;
3207 if ( channel & BlueChannel )
3208 pixel.blue += arguments[x++]*weight;
3209 if ( channel & IndexChannel )
3210 pixel.index += arguments[x++]*weight;
3211 if ( channel & OpacityChannel )
3212 pixel.opacity += arguments[x++]*weight;
3213 denominator += weight;
3215 if ( channel & RedChannel ) pixel.red /= denominator;
3216 if ( channel & GreenChannel ) pixel.green /= denominator;
3217 if ( channel & BlueChannel ) pixel.blue /= denominator;
3218 if ( channel & IndexChannel ) pixel.index /= denominator;
3219 if ( channel & OpacityChannel ) pixel.opacity /= denominator;
3222 case ManhattanColorInterpolate:
3228 minimum = MagickMaximumValue;
3233 for(k=0; k<number_arguments; k+=2+number_colors) {
3235 fabs((
double)i-arguments[ k ])
3236 + fabs((
double)j-arguments[k+1]);
3237 if ( distance < minimum ) {
3238 ssize_t x=(ssize_t) k+2;
3239 if ( channel & RedChannel ) pixel.red = arguments[x++];
3240 if ( channel & GreenChannel ) pixel.green = arguments[x++];
3241 if ( channel & BlueChannel ) pixel.blue = arguments[x++];
3242 if ( channel & IndexChannel ) pixel.index = arguments[x++];
3243 if ( channel & OpacityChannel ) pixel.opacity = arguments[x++];
3249 case VoronoiColorInterpolate:
3256 minimum = MagickMaximumValue;
3261 for(k=0; k<number_arguments; k+=2+number_colors) {
3263 ((double)i-arguments[ k ])*((
double)i-arguments[ k ])
3264 + ((double)j-arguments[k+1])*((
double)j-arguments[k+1]);
3265 if ( distance < minimum ) {
3266 ssize_t x=(ssize_t) k+2;
3267 if ( channel & RedChannel ) pixel.red = arguments[x++];
3268 if ( channel & GreenChannel ) pixel.green = arguments[x++];
3269 if ( channel & BlueChannel ) pixel.blue = arguments[x++];
3270 if ( channel & IndexChannel ) pixel.index = arguments[x++];
3271 if ( channel & OpacityChannel ) pixel.opacity = arguments[x++];
3279 if ( channel & RedChannel )
3280 pixel.red=ClampPixel((MagickRealType) QuantumRange*pixel.red);
3281 if ( channel & GreenChannel )
3282 pixel.green=ClampPixel((MagickRealType) QuantumRange*pixel.green);
3283 if ( channel & BlueChannel )
3284 pixel.blue=ClampPixel((MagickRealType) QuantumRange*pixel.blue);
3285 if ( channel & IndexChannel )
3286 pixel.index=ClampPixel((MagickRealType) QuantumRange*pixel.index);
3287 if ( channel & OpacityChannel )
3288 pixel.opacity=ClampPixel((MagickRealType) QuantumRange*pixel.opacity);
3289 SetPixelPacket(sparse_image,&pixel,q,indexes);
3293 sync=SyncCacheViewAuthenticPixels(sparse_view,exception);
3294 if (sync == MagickFalse)
3296 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3301#if defined(MAGICKCORE_OPENMP_SUPPORT)
3305 proceed=SetImageProgress(image,SparseColorTag,progress,image->rows);
3306 if (proceed == MagickFalse)
3310 sparse_view=DestroyCacheView(sparse_view);
3311 if (status == MagickFalse)
3312 sparse_image=DestroyImage(sparse_image);
3314 coeff = (
double *) RelinquishMagickMemory(coeff);
3315 return(sparse_image);