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=PerceptibleReciprocal(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=PerceptibleReciprocal(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((
size_t) floor((order+1)*(order+2)/2));
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,
297 assert(image->signature == MagickCoreSignature);
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 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
437 "InvalidArgument",
"%s : 'require at least %.20g CPs'",
438 "Polynomial", (
double) i);
439 return((
double *) NULL);
442 case BilinearReverseDistortion:
443 number_coeff=4*number_values;
448 case BilinearForwardDistortion:
455 case QuadraterialDistortion:
459 case ShepardsDistortion:
465 case ScaleRotateTranslateDistortion:
466 case AffineProjectionDistortion:
467 case Plane2CylinderDistortion:
468 case Cylinder2PlaneDistortion:
471 case PolarDistortion:
472 case DePolarDistortion:
475 case PerspectiveDistortion:
476 case PerspectiveProjectionDistortion:
479 case BarrelDistortion:
480 case BarrelInverseDistortion:
484 perror(
"unknown method given");
488 coeff = (
double *) AcquireQuantumMemory(number_coeff,
sizeof(*coeff));
489 if (coeff == (
double *) NULL) {
490 (void) ThrowMagickException(exception,GetMagickModule(),
491 ResourceLimitError,
"MemoryAllocationFailed",
492 "%s",
"GenerateCoefficients");
493 return((
double *) NULL);
497 for (i=0; i < number_coeff; 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,
sizeof(*vectors));
551 if (matrix == (
double **) NULL || vectors == (
double **) NULL)
553 matrix = RelinquishMagickMatrix(matrix, 3UL);
554 vectors = (
double **) RelinquishMagickMemory(vectors);
555 coeff = (
double *) RelinquishMagickMemory(coeff);
556 (void) ThrowMagickException(exception,GetMagickModule(),
557 ResourceLimitError,
"MemoryAllocationFailed",
558 "%s",
"DistortCoefficients");
559 return((
double *) NULL);
562 for (i=0; i < number_values; i++)
563 vectors[i] = &(coeff[i*3]);
565 for (i=0; i < number_arguments; i+=cp_size) {
566 terms[0] = arguments[i+cp_x];
567 terms[1] = arguments[i+cp_y];
569 LeastSquaresAddTerms(matrix,vectors,terms,
570 &(arguments[i+cp_values]),3UL,number_values);
572 if ( number_arguments == 2*cp_size ) {
577 terms[0] = arguments[cp_x]
578 - ( arguments[cp_size+cp_y] - arguments[cp_y] );
579 terms[1] = arguments[cp_y] +
580 + ( arguments[cp_size+cp_x] - arguments[cp_x] );
582 if ( cp_values == 0 ) {
586 uv2[0] = arguments[0] - arguments[5] + arguments[1];
587 uv2[1] = arguments[1] + arguments[4] - arguments[0];
588 LeastSquaresAddTerms(matrix,vectors,terms,uv2,3UL,2UL);
592 LeastSquaresAddTerms(matrix,vectors,terms,
593 &(arguments[cp_values]),3UL,number_values);
597 status=GaussJordanElimination(matrix,vectors,3UL,number_values);
598 matrix = RelinquishMagickMatrix(matrix, 3UL);
599 vectors = (
double **) RelinquishMagickMemory(vectors);
600 if ( status == MagickFalse ) {
601 coeff = (
double *) RelinquishMagickMemory(coeff);
602 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
603 "InvalidArgument",
"%s : 'Unsolvable Matrix'",
604 CommandOptionToMnemonic(MagickDistortOptions, *method) );
605 return((
double *) NULL);
610 case AffineProjectionDistortion:
626 if (number_arguments != 6) {
627 coeff = (
double *) RelinquishMagickMemory(coeff);
628 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
629 "InvalidArgument",
"%s : 'Needs 6 coeff values'",
630 CommandOptionToMnemonic(MagickDistortOptions, *method) );
631 return((
double *) NULL);
634 for(i=0; i<6UL; i++ )
635 inverse[i] = arguments[i];
636 AffineArgsToCoefficients(inverse);
637 InvertAffineCoefficients(inverse, coeff);
638 *method = AffineDistortion;
642 case ScaleRotateTranslateDistortion:
671 x = nx = (double)(image->columns)/2.0 + (double)image->page.x;
672 y = ny = (double)(image->rows)/2.0 + (double)image->page.y;
674 switch ( number_arguments ) {
676 coeff = (
double *) RelinquishMagickMemory(coeff);
677 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
678 "InvalidArgument",
"%s : 'Needs at least 1 argument'",
679 CommandOptionToMnemonic(MagickDistortOptions, *method) );
680 return((
double *) NULL);
685 sx = sy = arguments[0];
689 x = nx = arguments[0];
690 y = ny = arguments[1];
691 switch ( number_arguments ) {
696 sx = sy = arguments[2];
705 sx = sy = arguments[2];
718 coeff = (
double *) RelinquishMagickMemory(coeff);
719 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
720 "InvalidArgument",
"%s : 'Too Many Arguments (7 or less)'",
721 CommandOptionToMnemonic(MagickDistortOptions, *method) );
722 return((
double *) NULL);
727 if ( fabs(sx) < MagickEpsilon || fabs(sy) < MagickEpsilon ) {
728 coeff = (
double *) RelinquishMagickMemory(coeff);
729 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
730 "InvalidArgument",
"%s : 'Zero Scale Given'",
731 CommandOptionToMnemonic(MagickDistortOptions, *method) );
732 return((
double *) NULL);
735 a=DegreesToRadians(a); cosine=cos(a); sine=sin(a);
737 *method = AffineDistortion;
740 coeff[2]=x-nx*coeff[0]-ny*coeff[1];
743 coeff[5]=y-nx*coeff[3]-ny*coeff[4];
746 case PerspectiveDistortion:
789 if ( number_arguments%cp_size != 0 ||
790 number_arguments < cp_size*4 ) {
791 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
792 "InvalidArgument",
"%s : 'require at least %.20g CPs'",
793 CommandOptionToMnemonic(MagickDistortOptions, *method), 4.0);
794 coeff=(
double *) RelinquishMagickMemory(coeff);
795 return((
double *) NULL);
798 vectors[0] = &(coeff[0]);
800 matrix = AcquireMagickMatrix(8UL,8UL);
801 if (matrix == (
double **) NULL) {
802 coeff=(
double *) RelinquishMagickMemory(coeff);
803 (void) ThrowMagickException(exception,GetMagickModule(),
804 ResourceLimitError,
"MemoryAllocationFailed",
805 "%s",
"DistortCoefficients");
806 return((
double *) NULL);
809 for (i=0; i < number_arguments; i+=4) {
810 terms[0]=arguments[i+cp_x];
811 terms[1]=arguments[i+cp_y];
816 terms[6]=-terms[0]*arguments[i+cp_u];
817 terms[7]=-terms[1]*arguments[i+cp_u];
818 LeastSquaresAddTerms(matrix,vectors,terms,&(arguments[i+cp_u]),
824 terms[3]=arguments[i+cp_x];
825 terms[4]=arguments[i+cp_y];
827 terms[6]=-terms[3]*arguments[i+cp_v];
828 terms[7]=-terms[4]*arguments[i+cp_v];
829 LeastSquaresAddTerms(matrix,vectors,terms,&(arguments[i+cp_v]),
833 status=GaussJordanElimination(matrix,vectors,8UL,1UL);
834 matrix = RelinquishMagickMatrix(matrix, 8UL);
835 if ( status == MagickFalse ) {
836 coeff = (
double *) RelinquishMagickMemory(coeff);
837 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
838 "InvalidArgument",
"%s : 'Unsolvable Matrix'",
839 CommandOptionToMnemonic(MagickDistortOptions, *method) );
840 return((
double *) NULL);
848 coeff[8] = coeff[6]*arguments[cp_x]
849 + coeff[7]*arguments[cp_y] + 1.0;
850 coeff[8] = (coeff[8] < 0.0) ? -1.0 : +1.0;
854 case PerspectiveProjectionDistortion:
859 if (number_arguments != 8) {
860 coeff = (
double *) RelinquishMagickMemory(coeff);
861 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
862 "InvalidArgument",
"%s : 'Needs 8 coefficient values'",
863 CommandOptionToMnemonic(MagickDistortOptions, *method));
864 return((
double *) NULL);
867 InvertPerspectiveCoefficients(arguments, coeff);
875 coeff[8] = coeff[6]*arguments[2]
876 + coeff[7]*arguments[5] + 1.0;
877 coeff[8] = (coeff[8] < 0.0) ? -1.0 : +1.0;
878 *method = PerspectiveDistortion;
882 case BilinearForwardDistortion:
883 case BilinearReverseDistortion:
907 if ( number_arguments%cp_size != 0 ||
908 number_arguments < cp_size*4 ) {
909 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
910 "InvalidArgument",
"%s : 'require at least %.20g CPs'",
911 CommandOptionToMnemonic(MagickDistortOptions, *method), 4.0);
912 coeff=(
double *) RelinquishMagickMemory(coeff);
913 return((
double *) NULL);
916 matrix = AcquireMagickMatrix(4UL,4UL);
917 vectors = (
double **) AcquireQuantumMemory(number_values,
sizeof(*vectors));
918 if (matrix == (
double **) NULL || vectors == (
double **) NULL)
920 matrix = RelinquishMagickMatrix(matrix, 4UL);
921 vectors = (
double **) RelinquishMagickMemory(vectors);
922 coeff = (
double *) RelinquishMagickMemory(coeff);
923 (void) ThrowMagickException(exception,GetMagickModule(),
924 ResourceLimitError,
"MemoryAllocationFailed",
925 "%s",
"DistortCoefficients");
926 return((
double *) NULL);
929 for (i=0; i < number_values; i++)
930 vectors[i] = &(coeff[i*4]);
932 for (i=0; i < number_arguments; i+=cp_size) {
933 terms[0] = arguments[i+cp_x];
934 terms[1] = arguments[i+cp_y];
935 terms[2] = terms[0]*terms[1];
937 LeastSquaresAddTerms(matrix,vectors,terms,
938 &(arguments[i+cp_values]),4UL,number_values);
941 status=GaussJordanElimination(matrix,vectors,4UL,number_values);
942 matrix = RelinquishMagickMatrix(matrix, 4UL);
943 vectors = (
double **) RelinquishMagickMemory(vectors);
944 if ( status == MagickFalse ) {
945 coeff = (
double *) RelinquishMagickMemory(coeff);
946 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
947 "InvalidArgument",
"%s : 'Unsolvable Matrix'",
948 CommandOptionToMnemonic(MagickDistortOptions, *method) );
949 return((
double *) NULL);
951 if ( *method == BilinearForwardDistortion ) {
991 coeff[8] = coeff[0]*coeff[5] - coeff[1]*coeff[4];
992 coeff[9] = 2*(coeff[2]*coeff[5] - coeff[1]*coeff[6]);
997 case QuadrilateralDistortion:
1013 case PolynomialDistortion:
1051 coeff[0] = arguments[0];
1052 coeff[1] = (double) poly_number_terms(arguments[0]);
1053 nterms = (size_t) coeff[1];
1056 matrix = AcquireMagickMatrix(nterms,nterms);
1057 vectors = (
double **) AcquireQuantumMemory(number_values,
sizeof(*vectors));
1058 terms = (
double *) AcquireQuantumMemory(nterms,
sizeof(*terms));
1059 if (matrix == (
double **) NULL ||
1060 vectors == (
double **) NULL ||
1061 terms == (
double *) NULL )
1063 matrix = RelinquishMagickMatrix(matrix, nterms);
1064 vectors = (
double **) RelinquishMagickMemory(vectors);
1065 terms = (
double *) RelinquishMagickMemory(terms);
1066 coeff = (
double *) RelinquishMagickMemory(coeff);
1067 (void) ThrowMagickException(exception,GetMagickModule(),
1068 ResourceLimitError,
"MemoryAllocationFailed",
1069 "%s",
"DistortCoefficients");
1070 return((
double *) NULL);
1073 for (i=0; i < number_values; i++)
1074 vectors[i] = &(coeff[2+i*nterms]);
1076 for (i=1; i < number_arguments; i+=cp_size) {
1077 for (j=0; j < (ssize_t) nterms; j++)
1078 terms[j] = poly_basis_fn(j,arguments[i+cp_x],arguments[i+cp_y]);
1079 LeastSquaresAddTerms(matrix,vectors,terms,
1080 &(arguments[i+cp_values]),nterms,number_values);
1082 terms = (
double *) RelinquishMagickMemory(terms);
1084 status=GaussJordanElimination(matrix,vectors,nterms,number_values);
1085 matrix = RelinquishMagickMatrix(matrix, nterms);
1086 vectors = (
double **) RelinquishMagickMemory(vectors);
1087 if ( status == MagickFalse ) {
1088 coeff = (
double *) RelinquishMagickMemory(coeff);
1089 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1090 "InvalidArgument",
"%s : 'Unsolvable Matrix'",
1091 CommandOptionToMnemonic(MagickDistortOptions, *method) );
1092 return((
double *) NULL);
1132 if ( number_arguments >= 1 && arguments[0] < MagickEpsilon ) {
1133 coeff = (
double *) RelinquishMagickMemory(coeff);
1134 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1135 "InvalidArgument",
"%s : 'Arc Angle Too Small'",
1136 CommandOptionToMnemonic(MagickDistortOptions, *method) );
1137 return((
double *) NULL);
1139 if ( number_arguments >= 3 && arguments[2] < MagickEpsilon ) {
1140 coeff = (
double *) RelinquishMagickMemory(coeff);
1141 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1142 "InvalidArgument",
"%s : 'Outer Radius Too Small'",
1143 CommandOptionToMnemonic(MagickDistortOptions, *method) );
1144 return((
double *) NULL);
1146 coeff[0] = -MagickPI2;
1147 if ( number_arguments >= 1 )
1148 coeff[1] = DegreesToRadians(arguments[0]);
1150 coeff[1] = MagickPI2;
1151 if ( number_arguments >= 2 )
1152 coeff[0] += DegreesToRadians(arguments[1]);
1153 coeff[0] /= Magick2PI;
1154 coeff[0] -= MagickRound(coeff[0]);
1155 coeff[0] *= Magick2PI;
1156 coeff[3] = (double)image->rows-1;
1157 coeff[2] = (double)image->columns/coeff[1] + coeff[3]/2.0;
1158 if ( number_arguments >= 3 ) {
1159 if ( number_arguments >= 4 )
1160 coeff[3] = arguments[2] - arguments[3];
1162 coeff[3] *= arguments[2]/coeff[2];
1163 coeff[2] = arguments[2];
1165 coeff[4] = ((double)image->columns-1.0)/2.0;
1169 case PolarDistortion:
1170 case DePolarDistortion:
1182 if ( number_arguments == 3
1183 || ( number_arguments > 6 && *method == PolarDistortion )
1184 || number_arguments > 8 ) {
1185 (void) ThrowMagickException(exception,GetMagickModule(),
1186 OptionError,
"InvalidArgument",
"%s : number of arguments",
1187 CommandOptionToMnemonic(MagickDistortOptions, *method) );
1188 coeff=(
double *) RelinquishMagickMemory(coeff);
1189 return((
double *) NULL);
1192 if ( number_arguments >= 1 )
1193 coeff[0] = arguments[0];
1197 coeff[1] = number_arguments >= 2 ? arguments[1] : 0.0;
1199 if ( number_arguments >= 4 ) {
1200 coeff[2] = arguments[2];
1201 coeff[3] = arguments[3];
1204 coeff[2] = (double)(image->columns)/2.0+image->page.x;
1205 coeff[3] = (double)(image->rows)/2.0+image->page.y;
1208 coeff[4] = -MagickPI;
1209 if ( number_arguments >= 5 )
1210 coeff[4] = DegreesToRadians(arguments[4]);
1211 coeff[5] = coeff[4];
1212 if ( number_arguments >= 6 )
1213 coeff[5] = DegreesToRadians(arguments[5]);
1214 if ( fabs(coeff[4]-coeff[5]) < MagickEpsilon )
1215 coeff[5] += Magick2PI;
1217 if ( coeff[0] < MagickEpsilon ) {
1219 if ( fabs(coeff[0]) < MagickEpsilon ) {
1220 coeff[0]=MagickMin(fabs(coeff[2]-image->page.x),
1221 fabs(coeff[3]-image->page.y));
1222 coeff[0]=MagickMin(coeff[0],
1223 fabs(coeff[2]-image->page.x-image->columns));
1224 coeff[0]=MagickMin(coeff[0],
1225 fabs(coeff[3]-image->page.y-image->rows));
1228 if ( fabs(-1.0-coeff[0]) < MagickEpsilon ) {
1230 rx = coeff[2]-image->page.x;
1231 ry = coeff[3]-image->page.y;
1232 coeff[0] = rx*rx+ry*ry;
1233 ry = coeff[3]-image->page.y-image->rows;
1234 coeff[0] = MagickMax(coeff[0],rx*rx+ry*ry);
1235 rx = coeff[2]-image->page.x-image->columns;
1236 coeff[0] = MagickMax(coeff[0],rx*rx+ry*ry);
1237 ry = coeff[3]-image->page.y;
1238 coeff[0] = MagickMax(coeff[0],rx*rx+ry*ry);
1239 coeff[0] = sqrt(coeff[0]);
1243 if ( coeff[0] < MagickEpsilon || coeff[1] < -MagickEpsilon
1244 || (coeff[0]-coeff[1]) < MagickEpsilon ) {
1245 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1246 "InvalidArgument",
"%s : Invalid Radius",
1247 CommandOptionToMnemonic(MagickDistortOptions, *method) );
1248 coeff=(
double *) RelinquishMagickMemory(coeff);
1249 return((
double *) NULL);
1252 if ( *method == PolarDistortion ) {
1253 coeff[6]=(double) image->columns/(coeff[5]-coeff[4]);
1254 coeff[7]=(double) image->rows/(coeff[0]-coeff[1]);
1257 coeff[6]=(coeff[5]-coeff[4])/image->columns;
1258 coeff[7]=(coeff[0]-coeff[1])/image->rows;
1262 case Cylinder2PlaneDistortion:
1263 case Plane2CylinderDistortion:
1287 if ( arguments[0] < MagickEpsilon || arguments[0] > 160.0 ) {
1288 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1289 "InvalidArgument",
"%s : Invalid FOV Angle",
1290 CommandOptionToMnemonic(MagickDistortOptions, *method) );
1291 coeff=(
double *) RelinquishMagickMemory(coeff);
1292 return((
double *) NULL);
1294 coeff[0] = DegreesToRadians(arguments[0]);
1295 if ( *method == Cylinder2PlaneDistortion )
1299 coeff[1] = (double) image->columns/coeff[0];
1302 coeff[1] = (double) image->columns / ( 2 * tan(coeff[0]/2) );
1304 coeff[2] = (double)(image->columns)/2.0+image->page.x;
1305 coeff[3] = (double)(image->rows)/2.0+image->page.y;
1306 coeff[4] = coeff[2];
1307 coeff[5] = coeff[3];
1310 case BarrelDistortion:
1311 case BarrelInverseDistortion:
1332 rscale = 2.0/MagickMin((
double) image->columns,(
double) image->rows);
1335 if ( (number_arguments < 3) || (number_arguments == 7) ||
1336 (number_arguments == 9) || (number_arguments > 10) )
1338 coeff=(
double *) RelinquishMagickMemory(coeff);
1339 (void) ThrowMagickException(exception,GetMagickModule(),
1340 OptionError,
"InvalidArgument",
"%s : number of arguments",
1341 CommandOptionToMnemonic(MagickDistortOptions, *method) );
1342 return((
double *) NULL);
1345 coeff[0] = arguments[0];
1346 coeff[1] = arguments[1];
1347 coeff[2] = arguments[2];
1348 if ((number_arguments == 3) || (number_arguments == 5) )
1349 coeff[3] = 1.0 - coeff[0] - coeff[1] - coeff[2];
1351 coeff[3] = arguments[3];
1353 coeff[0] *= pow(rscale,3.0);
1354 coeff[1] *= rscale*rscale;
1357 if ( number_arguments >= 8 ) {
1358 coeff[4] = arguments[4] * pow(rscale,3.0);
1359 coeff[5] = arguments[5] * rscale*rscale;
1360 coeff[6] = arguments[6] * rscale;
1361 coeff[7] = arguments[7];
1364 coeff[4] = coeff[0];
1365 coeff[5] = coeff[1];
1366 coeff[6] = coeff[2];
1367 coeff[7] = coeff[3];
1370 if ( number_arguments == 5 ) {
1371 coeff[8] = arguments[3];
1372 coeff[9] = arguments[4];
1374 else if ( number_arguments == 6 ) {
1375 coeff[8] = arguments[4];
1376 coeff[9] = arguments[5];
1378 else if ( number_arguments == 10 ) {
1379 coeff[8] = arguments[8];
1380 coeff[9] = arguments[9];
1384 coeff[8] = (double)image->columns/2.0 + image->page.x;
1385 coeff[9] = (double)image->rows/2.0 + image->page.y;
1389 case ShepardsDistortion:
1396 if ( number_arguments%cp_size != 0 ||
1397 number_arguments < cp_size ) {
1398 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1399 "InvalidArgument",
"%s : 'requires CP's (4 numbers each)'",
1400 CommandOptionToMnemonic(MagickDistortOptions, *method));
1401 coeff=(
double *) RelinquishMagickMemory(coeff);
1402 return((
double *) NULL);
1405 {
const char *artifact=GetImageArtifact(image,
"shepards:power");
1406 if ( artifact != (
const char *) NULL ) {
1407 coeff[0]=StringToDouble(artifact,(
char **) NULL) / 2.0;
1408 if ( coeff[0] < MagickEpsilon ) {
1409 (void) ThrowMagickException(exception,GetMagickModule(),
1410 OptionError,
"InvalidArgument",
"%s",
"-define shepards:power" );
1411 coeff=(
double *) RelinquishMagickMemory(coeff);
1412 return((
double *) NULL);
1424 perror(
"no method handler");
1425 return((
double *) NULL);
1463MagickExport
Image *DistortResizeImage(
const Image *image,
1464 const size_t columns,
const size_t rows,
ExceptionInfo *exception)
1466#define DistortResizeImageTag "Distort/Image"
1484 assert(image != (
const Image *) NULL);
1485 assert(image->signature == MagickCoreSignature);
1487 assert(exception->signature == MagickCoreSignature);
1488 if (IsEventLogging() != MagickFalse)
1489 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1490 if ((columns == 0) || (rows == 0))
1491 return((
Image *) NULL);
1494 (void) memset(distort_args,0,12*
sizeof(
double));
1495 distort_args[4]=(double) image->columns;
1496 distort_args[6]=(double) columns;
1497 distort_args[9]=(double) image->rows;
1498 distort_args[11]=(double) rows;
1500 vp_save=GetImageVirtualPixelMethod(image);
1502 tmp_image=CloneImage(image,0,0,MagickTrue,exception);
1503 if ( tmp_image == (
Image *) NULL )
1504 return((
Image *) NULL);
1505 (void) SetImageVirtualPixelMethod(tmp_image,TransparentVirtualPixelMethod);
1507 if (image->matte == MagickFalse)
1512 (void) SetImageAlphaChannel(tmp_image,SetAlphaChannel);
1513 resize_image=DistortImage(tmp_image,AffineDistortion,12,distort_args,
1514 MagickTrue,exception),
1516 tmp_image=DestroyImage(tmp_image);
1517 if ( resize_image == (
Image *) NULL )
1518 return((
Image *) NULL);
1520 (void) SetImageAlphaChannel(resize_image,DeactivateAlphaChannel);
1521 InheritException(exception,&image->exception);
1534 (void) SeparateImageChannel(tmp_image,TrueAlphaChannel);
1535 (void) SetImageAlphaChannel(tmp_image,OpaqueAlphaChannel);
1536 resize_alpha=DistortImage(tmp_image,AffineDistortion,12,distort_args,
1537 MagickTrue,exception),
1538 tmp_image=DestroyImage(tmp_image);
1539 if ( resize_alpha == (
Image *) NULL )
1540 return((
Image *) NULL);
1543 tmp_image=CloneImage(image,0,0,MagickTrue,exception);
1544 if ( tmp_image == (
Image *) NULL )
1545 return((
Image *) NULL);
1546 (void) SetImageVirtualPixelMethod(tmp_image,
1547 TransparentVirtualPixelMethod);
1548 (void) SetImageVirtualPixelMethod(tmp_image,
1549 TransparentVirtualPixelMethod);
1550 resize_image=DistortImage(tmp_image,AffineDistortion,12,distort_args,
1551 MagickTrue,exception),
1552 tmp_image=DestroyImage(tmp_image);
1553 if ( resize_image == (
Image *) NULL)
1555 resize_alpha=DestroyImage(resize_alpha);
1556 return((
Image *) NULL);
1560 (void) SetImageAlphaChannel(resize_image,DeactivateAlphaChannel);
1561 (void) SetImageAlphaChannel(resize_alpha,DeactivateAlphaChannel);
1562 (void) CompositeImage(resize_image,CopyOpacityCompositeOp,resize_alpha,
1564 InheritException(exception,&resize_image->exception);
1565 resize_image->matte=image->matte;
1566 resize_image->compose=image->compose;
1567 resize_alpha=DestroyImage(resize_alpha);
1569 (void) SetImageVirtualPixelMethod(resize_image,vp_save);
1574 crop_area.width=columns;
1575 crop_area.height=rows;
1579 tmp_image=resize_image;
1580 resize_image=CropImage(tmp_image,&crop_area,exception);
1581 tmp_image=DestroyImage(tmp_image);
1582 if (resize_image != (
Image *) NULL)
1584 resize_image->page.width=0;
1585 resize_image->page.height=0;
1587 return(resize_image);
1679MagickExport
Image *DistortImage(
const Image *image,DistortImageMethod method,
1680 const size_t number_arguments,
const double *arguments,
1683#define DistortImageTag "Distort/Image"
1698 assert(image != (
Image *) NULL);
1699 assert(image->signature == MagickCoreSignature);
1701 assert(exception->signature == MagickCoreSignature);
1702 if (IsEventLogging() != MagickFalse)
1703 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1707 if (method == ResizeDistortion)
1709 if (number_arguments != 2)
1711 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1712 "InvalidArgument",
"%s : '%s'",
"Resize",
1713 "Invalid number of args: 2 only");
1714 return((
Image *) NULL);
1716 distort_image=DistortResizeImage(image,(
size_t) arguments[0],
1717 (
size_t) arguments[1],exception);
1718 return(distort_image);
1728 coeff=GenerateCoefficients(image,&method,number_arguments,arguments,0,
1730 if (coeff == (
double *) NULL)
1731 return((
Image *) NULL);
1739 geometry.width=image->columns;
1740 geometry.height=image->rows;
1744 if ( method == ArcDistortion ) {
1745 bestfit = MagickTrue;
1754 fix_bounds = MagickTrue;
1756 s.x=s.y=min.x=max.x=min.y=max.y=0.0;
1759#define InitalBounds(p) \
1762 min.x = max.x = p.x; \
1763 min.y = max.y = p.y; \
1765#define ExpandBounds(p) \
1768 min.x = MagickMin(min.x,p.x); \
1769 max.x = MagickMax(max.x,p.x); \
1770 min.y = MagickMin(min.y,p.y); \
1771 max.y = MagickMax(max.y,p.y); \
1776 case AffineDistortion:
1777 {
double inverse[6];
1778 InvertAffineCoefficients(coeff, inverse);
1779 s.x = (double) image->page.x;
1780 s.y = (double) image->page.y;
1781 d.x = inverse[0]*s.x+inverse[1]*s.y+inverse[2];
1782 d.y = inverse[3]*s.x+inverse[4]*s.y+inverse[5];
1784 s.x = (double) image->page.x+image->columns;
1785 s.y = (double) image->page.y;
1786 d.x = inverse[0]*s.x+inverse[1]*s.y+inverse[2];
1787 d.y = inverse[3]*s.x+inverse[4]*s.y+inverse[5];
1789 s.x = (double) image->page.x;
1790 s.y = (double) image->page.y+image->rows;
1791 d.x = inverse[0]*s.x+inverse[1]*s.y+inverse[2];
1792 d.y = inverse[3]*s.x+inverse[4]*s.y+inverse[5];
1794 s.x = (double) image->page.x+image->columns;
1795 s.y = (double) image->page.y+image->rows;
1796 d.x = inverse[0]*s.x+inverse[1]*s.y+inverse[2];
1797 d.y = inverse[3]*s.x+inverse[4]*s.y+inverse[5];
1801 case PerspectiveDistortion:
1802 {
double inverse[8], scale;
1803 InvertPerspectiveCoefficients(coeff, inverse);
1804 s.x = (double) image->page.x;
1805 s.y = (double) image->page.y;
1806 scale=inverse[6]*s.x+inverse[7]*s.y+1.0;
1807 scale=PerceptibleReciprocal(scale);
1808 d.x = scale*(inverse[0]*s.x+inverse[1]*s.y+inverse[2]);
1809 d.y = scale*(inverse[3]*s.x+inverse[4]*s.y+inverse[5]);
1811 s.x = (double) image->page.x+image->columns;
1812 s.y = (double) image->page.y;
1813 scale=inverse[6]*s.x+inverse[7]*s.y+1.0;
1814 scale=PerceptibleReciprocal(scale);
1815 d.x = scale*(inverse[0]*s.x+inverse[1]*s.y+inverse[2]);
1816 d.y = scale*(inverse[3]*s.x+inverse[4]*s.y+inverse[5]);
1818 s.x = (double) image->page.x;
1819 s.y = (double) image->page.y+image->rows;
1820 scale=inverse[6]*s.x+inverse[7]*s.y+1.0;
1821 scale=PerceptibleReciprocal(scale);
1822 d.x = scale*(inverse[0]*s.x+inverse[1]*s.y+inverse[2]);
1823 d.y = scale*(inverse[3]*s.x+inverse[4]*s.y+inverse[5]);
1825 s.x = (double) image->page.x+image->columns;
1826 s.y = (double) image->page.y+image->rows;
1827 scale=inverse[6]*s.x+inverse[7]*s.y+1.0;
1828 scale=PerceptibleReciprocal(scale);
1829 d.x = scale*(inverse[0]*s.x+inverse[1]*s.y+inverse[2]);
1830 d.y = scale*(inverse[3]*s.x+inverse[4]*s.y+inverse[5]);
1837 a = coeff[0]-coeff[1]/2; ca = cos(a); sa = sin(a);
1841 d.x = (coeff[2]-coeff[3])*ca;
1842 d.y = (coeff[2]-coeff[3])*sa;
1844 a = coeff[0]+coeff[1]/2; ca = cos(a); sa = sin(a);
1848 d.x = (coeff[2]-coeff[3])*ca;
1849 d.y = (coeff[2]-coeff[3])*sa;
1852 for( a=(
double) (ceil((
double) ((coeff[0]-coeff[1]/2.0)/MagickPI2))*MagickPI2);
1853 a<(coeff[0]+coeff[1]/2.0); a+=MagickPI2 ) {
1854 ca = cos(a); sa = sin(a);
1864 coeff[1] = (double) (Magick2PI*image->columns/coeff[1]);
1865 coeff[3] = (double)image->rows/coeff[3];
1868 case PolarDistortion:
1870 if (number_arguments < 2)
1871 coeff[2] = coeff[3] = 0.0;
1872 min.x = coeff[2]-coeff[0];
1873 max.x = coeff[2]+coeff[0];
1874 min.y = coeff[3]-coeff[0];
1875 max.y = coeff[3]+coeff[0];
1877 coeff[7]=(double) geometry.height/(coeff[0]-coeff[1]);
1880 case DePolarDistortion:
1884 fix_bounds = MagickFalse;
1885 geometry.x = geometry.y = 0;
1886 geometry.height = (size_t) ceil(coeff[0]-coeff[1]);
1887 geometry.width = (size_t) ceil((coeff[0]-coeff[1])*
1888 (coeff[5]-coeff[4])*0.5);
1890 coeff[6]=(coeff[5]-coeff[4])*PerceptibleReciprocal(geometry.width);
1891 coeff[7]=(coeff[0]-coeff[1])*PerceptibleReciprocal(geometry.height);
1894 case Cylinder2PlaneDistortion:
1899 geometry.x = geometry.y = 0;
1900 geometry.width = (size_t) ceil( 2.0*coeff[1]*tan(coeff[0]/2.0) );
1901 geometry.height = (size_t) ceil( 2.0*coeff[3]/cos(coeff[0]/2.0) );
1903 coeff[4] = (double) geometry.width/2.0;
1904 coeff[5] = (double) geometry.height/2.0;
1905 fix_bounds = MagickFalse;
1908 case Plane2CylinderDistortion:
1912 geometry.x = geometry.y = 0;
1913 geometry.width = (size_t) ceil(coeff[0]*coeff[1]);
1914 geometry.height = (size_t) (2*coeff[3]);
1916 coeff[4] = (double) geometry.width/2.0;
1917 coeff[5] = (double) geometry.height/2.0;
1918 fix_bounds = MagickFalse;
1922 case ShepardsDistortion:
1923 case BilinearForwardDistortion:
1924 case BilinearReverseDistortion:
1926 case QuadrilateralDistortion:
1928 case PolynomialDistortion:
1929 case BarrelDistortion:
1930 case BarrelInverseDistortion:
1933 bestfit = MagickFalse;
1934 fix_bounds = MagickFalse;
1943 geometry.x = (ssize_t) floor(min.x-0.5);
1944 geometry.y = (ssize_t) floor(min.y-0.5);
1945 geometry.width=(size_t) ceil(max.x-geometry.x+0.5);
1946 geometry.height=(size_t) ceil(max.y-geometry.y+0.5);
1955 {
const char *artifact=GetImageArtifact(image,
"distort:viewport");
1956 viewport_given = MagickFalse;
1957 if ( artifact != (
const char *) NULL ) {
1958 MagickStatusType flags=ParseAbsoluteGeometry(artifact,&geometry);
1960 (void) ThrowMagickException(exception,GetMagickModule(),
1961 OptionWarning,
"InvalidGeometry",
"`%s' `%s'",
1962 "distort:viewport",artifact);
1964 viewport_given = MagickTrue;
1969 if ( GetImageArtifact(image,
"verbose") != (
const char *) NULL ) {
1972 char image_gen[MaxTextExtent];
1976 if ( bestfit || viewport_given ) {
1977 (void) FormatLocaleString(image_gen, MaxTextExtent,
" -size %.20gx%.20g "
1978 "-page %+.20g%+.20g xc: +insert \\\n",(
double) geometry.width,
1979 (
double) geometry.height,(
double) geometry.x,(
double) geometry.y);
1980 lookup=
"v.p{ xx-v.page.x-.5, yy-v.page.y-.5 }";
1983 image_gen[0] =
'\0';
1984 lookup =
"p{ xx-page.x-.5, yy-page.y-.5 }";
1989 case AffineDistortion:
1994 inverse=(
double *) AcquireQuantumMemory(6,
sizeof(*inverse));
1995 if (inverse == (
double *) NULL)
1997 coeff=(
double *) RelinquishMagickMemory(coeff);
1998 (void) ThrowMagickException(exception,GetMagickModule(),
1999 ResourceLimitError,
"MemoryAllocationFailed",
"%s",
"DistortImages");
2000 return((
Image *) NULL);
2002 InvertAffineCoefficients(coeff, inverse);
2003 CoefficientsToAffineArgs(inverse);
2004 (void) FormatLocaleFile(stderr,
"Affine Projection:\n");
2005 (void) FormatLocaleFile(stderr,
2006 " -distort AffineProjection \\\n '");
2007 for (i=0; i < 5; i++)
2008 (
void) FormatLocaleFile(stderr,
"%lf,", inverse[i]);
2009 (void) FormatLocaleFile(stderr,
"%lf'\n", inverse[5]);
2010 inverse=(
double *) RelinquishMagickMemory(inverse);
2011 (void) FormatLocaleFile(stderr,
"Affine Distort, FX Equivelent:\n");
2012 (void) FormatLocaleFile(stderr,
"%s", image_gen);
2013 (void) FormatLocaleFile(stderr,
2014 " -fx 'ii=i+page.x+0.5; jj=j+page.y+0.5;\n");
2015 (void) FormatLocaleFile(stderr,
" xx=%+lf*ii %+lf*jj %+lf;\n",
2016 coeff[0],coeff[1],coeff[2]);
2017 (void) FormatLocaleFile(stderr,
" yy=%+lf*ii %+lf*jj %+lf;\n",
2018 coeff[3],coeff[4],coeff[5]);
2019 (void) FormatLocaleFile(stderr,
" %s' \\\n",lookup);
2022 case PerspectiveDistortion:
2027 inverse=(
double *) AcquireQuantumMemory(8,
sizeof(*inverse));
2028 if (inverse == (
double *) NULL)
2030 coeff=(
double *) RelinquishMagickMemory(coeff);
2031 (void) ThrowMagickException(exception,GetMagickModule(),
2032 ResourceLimitError,
"MemoryAllocationFailed",
"%s",
2033 "DistortCoefficients");
2034 return((
Image *) NULL);
2036 InvertPerspectiveCoefficients(coeff, inverse);
2037 (void) FormatLocaleFile(stderr,
"Perspective Projection:\n");
2038 (void) FormatLocaleFile(stderr,
2039 " -distort PerspectiveProjection \\\n '");
2040 for (i=0; i < 4; i++)
2041 (
void) FormatLocaleFile(stderr,
"%.*g, ",GetMagickPrecision(),
2043 (void) FormatLocaleFile(stderr,
"\n ");
2045 (
void) FormatLocaleFile(stderr,
"%.*g, ",GetMagickPrecision(),
2047 (void) FormatLocaleFile(stderr,
"%.*g'\n",GetMagickPrecision(),
2049 inverse=(
double *) RelinquishMagickMemory(inverse);
2050 (void) FormatLocaleFile(stderr,
"Perspective Distort, FX Equivalent:\n");
2051 (void) FormatLocaleFile(stderr,
"%.1024s",image_gen);
2052 (void) FormatLocaleFile(stderr,
2053 " -fx 'ii=i+page.x+0.5; jj=j+page.y+0.5;\n");
2054 (void) FormatLocaleFile(stderr,
" rr=%+.*g*ii %+.*g*jj + 1;\n",
2055 GetMagickPrecision(),coeff[6],GetMagickPrecision(),coeff[7]);
2056 (void) FormatLocaleFile(stderr,
2057 " xx=(%+.*g*ii %+.*g*jj %+.*g)/rr;\n",
2058 GetMagickPrecision(),coeff[0],GetMagickPrecision(),coeff[1],
2059 GetMagickPrecision(),coeff[2]);
2060 (void) FormatLocaleFile(stderr,
2061 " yy=(%+.*g*ii %+.*g*jj %+.*g)/rr;\n",
2062 GetMagickPrecision(),coeff[3],GetMagickPrecision(),coeff[4],
2063 GetMagickPrecision(),coeff[5]);
2064 (void) FormatLocaleFile(stderr,
" rr%s0 ? %s : blue' \\\n",
2065 coeff[8] < 0.0 ?
"<" :
">", lookup);
2068 case BilinearForwardDistortion:
2070 (void) FormatLocaleFile(stderr,
"BilinearForward Mapping Equations:\n");
2071 (void) FormatLocaleFile(stderr,
"%s", image_gen);
2072 (void) FormatLocaleFile(stderr,
" i = %+lf*x %+lf*y %+lf*x*y %+lf;\n",
2073 coeff[0],coeff[1],coeff[2],coeff[3]);
2074 (void) FormatLocaleFile(stderr,
" j = %+lf*x %+lf*y %+lf*x*y %+lf;\n",
2075 coeff[4],coeff[5],coeff[6],coeff[7]);
2078 (void) FormatLocaleFile(stderr,
" c8 = %+lf c9 = 2*a = %+lf;\n",
2079 coeff[8], coeff[9]);
2081 (void) FormatLocaleFile(stderr,
2082 "BilinearForward Distort, FX Equivalent:\n");
2083 (void) FormatLocaleFile(stderr,
"%s", image_gen);
2084 (void) FormatLocaleFile(stderr,
2085 " -fx 'ii=i+page.x%+lf; jj=j+page.y%+lf;\n",0.5-coeff[3],0.5-
2087 (void) FormatLocaleFile(stderr,
" bb=%lf*ii %+lf*jj %+lf;\n",
2088 coeff[6], -coeff[2], coeff[8]);
2092 (void) FormatLocaleFile(stderr,
2093 " rt=bb*bb %+lf*(%lf*ii%+lf*jj);\n",-2*coeff[9],coeff[4],
2095 (void) FormatLocaleFile(stderr,
2096 " yy=( -bb + sqrt(rt) ) / %lf;\n",coeff[9]);
2099 (
void) FormatLocaleFile(stderr,
" yy=(%lf*ii%+lf*jj)/bb;\n",
2100 -coeff[4],coeff[0]);
2101 (void) FormatLocaleFile(stderr,
2102 " xx=(ii %+lf*yy)/(%lf %+lf*yy);\n",-coeff[1],coeff[0],
2104 if ( coeff[9] != 0 )
2105 (void) FormatLocaleFile(stderr,
" (rt < 0 ) ? red : %s'\n",
2108 (
void) FormatLocaleFile(stderr,
" %s' \\\n", lookup);
2111 case BilinearReverseDistortion:
2114 (void) FormatLocaleFile(stderr,
"Polynomial Projection Distort:\n");
2115 (void) FormatLocaleFile(stderr,
" -distort PolynomialProjection \\\n");
2116 (void) FormatLocaleFile(stderr,
" '1.5, %lf, %lf, %lf, %lf,\n",
2117 coeff[3], coeff[0], coeff[1], coeff[2]);
2118 (void) FormatLocaleFile(stderr,
" %lf, %lf, %lf, %lf'\n",
2119 coeff[7], coeff[4], coeff[5], coeff[6]);
2121 (void) FormatLocaleFile(stderr,
2122 "BilinearReverse Distort, FX Equivalent:\n");
2123 (void) FormatLocaleFile(stderr,
"%s", image_gen);
2124 (void) FormatLocaleFile(stderr,
2125 " -fx 'ii=i+page.x+0.5; jj=j+page.y+0.5;\n");
2126 (void) FormatLocaleFile(stderr,
2127 " xx=%+lf*ii %+lf*jj %+lf*ii*jj %+lf;\n",coeff[0],coeff[1],
2128 coeff[2], coeff[3]);
2129 (void) FormatLocaleFile(stderr,
2130 " yy=%+lf*ii %+lf*jj %+lf*ii*jj %+lf;\n",coeff[4],coeff[5],
2131 coeff[6], coeff[7]);
2132 (void) FormatLocaleFile(stderr,
" %s' \\\n", lookup);
2135 case PolynomialDistortion:
2137 size_t nterms = (size_t) coeff[1];
2138 (void) FormatLocaleFile(stderr,
2139 "Polynomial (order %lg, terms %lu), FX Equivalent\n",coeff[0],
2140 (
unsigned long) nterms);
2141 (void) FormatLocaleFile(stderr,
"%s", image_gen);
2142 (void) FormatLocaleFile(stderr,
2143 " -fx 'ii=i+page.x+0.5; jj=j+page.y+0.5;\n");
2144 (void) FormatLocaleFile(stderr,
" xx =");
2145 for (i=0; i < (ssize_t) nterms; i++)
2147 if ((i != 0) && (i%4 == 0))
2148 (void) FormatLocaleFile(stderr,
"\n ");
2149 (void) FormatLocaleFile(stderr,
" %+lf%s",coeff[2+i],
2152 (void) FormatLocaleFile(stderr,
";\n yy =");
2153 for (i=0; i < (ssize_t) nterms; i++)
2155 if ((i != 0) && (i%4 == 0))
2156 (void) FormatLocaleFile(stderr,
"\n ");
2157 (void) FormatLocaleFile(stderr,
" %+lf%s",coeff[2+i+nterms],
2160 (void) FormatLocaleFile(stderr,
";\n %s' \\\n", lookup);
2165 (void) FormatLocaleFile(stderr,
"Arc Distort, Internal Coefficients:\n");
2166 for (i=0; i < 5; i++)
2167 (
void) FormatLocaleFile(stderr,
2168 " c%.20g = %+lf\n",(
double) i,coeff[i]);
2169 (void) FormatLocaleFile(stderr,
"Arc Distort, FX Equivalent:\n");
2170 (void) FormatLocaleFile(stderr,
"%s", image_gen);
2171 (void) FormatLocaleFile(stderr,
" -fx 'ii=i+page.x; jj=j+page.y;\n");
2172 (void) FormatLocaleFile(stderr,
" xx=(atan2(jj,ii)%+lf)/(2*pi);\n",
2174 (void) FormatLocaleFile(stderr,
" xx=xx-round(xx);\n");
2175 (void) FormatLocaleFile(stderr,
" xx=xx*%lf %+lf;\n",coeff[1],
2177 (void) FormatLocaleFile(stderr,
2178 " yy=(%lf - hypot(ii,jj)) * %lf;\n",coeff[2],coeff[3]);
2179 (void) FormatLocaleFile(stderr,
" v.p{xx-.5,yy-.5}' \\\n");
2182 case PolarDistortion:
2184 (void) FormatLocaleFile(stderr,
"Polar Distort, Internal Coefficients\n");
2185 for (i=0; i < 8; i++)
2186 (
void) FormatLocaleFile(stderr,
" c%.20g = %+lf\n",(
double) i,
2188 (void) FormatLocaleFile(stderr,
"Polar Distort, FX Equivalent:\n");
2189 (void) FormatLocaleFile(stderr,
"%s", image_gen);
2190 (void) FormatLocaleFile(stderr,
2191 " -fx 'ii=i+page.x%+lf; jj=j+page.y%+lf;\n",-coeff[2],-coeff[3]);
2192 (void) FormatLocaleFile(stderr,
" xx=(atan2(ii,jj)%+lf)/(2*pi);\n",
2193 -(coeff[4]+coeff[5])/2 );
2194 (void) FormatLocaleFile(stderr,
" xx=xx-round(xx);\n");
2195 (void) FormatLocaleFile(stderr,
" xx=xx*2*pi*%lf + v.w/2;\n",
2197 (void) FormatLocaleFile(stderr,
" yy=(hypot(ii,jj)%+lf)*%lf;\n",
2198 -coeff[1],coeff[7] );
2199 (void) FormatLocaleFile(stderr,
" v.p{xx-.5,yy-.5}' \\\n");
2202 case DePolarDistortion:
2204 (void) FormatLocaleFile(stderr,
2205 "DePolar Distort, Internal Coefficients\n");
2206 for (i=0; i < 8; i++)
2207 (
void) FormatLocaleFile(stderr,
" c%.20g = %+lf\n",(
double) i,
2209 (void) FormatLocaleFile(stderr,
"DePolar Distort, FX Equivalent:\n");
2210 (void) FormatLocaleFile(stderr,
"%s", image_gen);
2211 (void) FormatLocaleFile(stderr,
" -fx 'aa=(i+.5)*%lf %+lf;\n",
2212 coeff[6],+coeff[4]);
2213 (void) FormatLocaleFile(stderr,
" rr=(j+.5)*%lf %+lf;\n",
2214 coeff[7],+coeff[1]);
2215 (void) FormatLocaleFile(stderr,
" xx=rr*sin(aa) %+lf;\n",
2217 (void) FormatLocaleFile(stderr,
" yy=rr*cos(aa) %+lf;\n",
2219 (void) FormatLocaleFile(stderr,
" v.p{xx-.5,yy-.5}' \\\n");
2222 case Cylinder2PlaneDistortion:
2224 (void) FormatLocaleFile(stderr,
2225 "Cylinder to Plane Distort, Internal Coefficients\n");
2226 (void) FormatLocaleFile(stderr,
" cylinder_radius = %+lf\n",coeff[1]);
2227 (void) FormatLocaleFile(stderr,
2228 "Cylinder to Plane Distort, FX Equivalent:\n");
2229 (void) FormatLocaleFile(stderr,
"%s", image_gen);
2230 (void) FormatLocaleFile(stderr,
2231 " -fx 'ii=i+page.x%+lf+0.5; jj=j+page.y%+lf+0.5;\n",-coeff[4],
2233 (void) FormatLocaleFile(stderr,
" aa=atan(ii/%+lf);\n",coeff[1]);
2234 (void) FormatLocaleFile(stderr,
" xx=%lf*aa%+lf;\n",
2236 (void) FormatLocaleFile(stderr,
" yy=jj*cos(aa)%+lf;\n",coeff[3]);
2237 (void) FormatLocaleFile(stderr,
" %s' \\\n", lookup);
2240 case Plane2CylinderDistortion:
2242 (void) FormatLocaleFile(stderr,
2243 "Plane to Cylinder Distort, Internal Coefficients\n");
2244 (void) FormatLocaleFile(stderr,
" cylinder_radius = %+lf\n",coeff[1]);
2245 (void) FormatLocaleFile(stderr,
2246 "Plane to Cylinder Distort, FX Equivalent:\n");
2247 (void) FormatLocaleFile(stderr,
"%s", image_gen);
2248 (void) FormatLocaleFile(stderr,
2249 " -fx 'ii=i+page.x%+lf+0.5; jj=j+page.y%+lf+0.5;\n",-coeff[4],
2251 (void) FormatLocaleFile(stderr,
" ii=ii/%+lf;\n",coeff[1]);
2252 (void) FormatLocaleFile(stderr,
" xx=%lf*tan(ii)%+lf;\n",coeff[1],
2254 (void) FormatLocaleFile(stderr,
" yy=jj/cos(ii)%+lf;\n",coeff[3]);
2255 (void) FormatLocaleFile(stderr,
" %s' \\\n", lookup);
2258 case BarrelDistortion:
2259 case BarrelInverseDistortion:
2270 xc=((double)image->columns-1.0)/2.0+image->page.x;
2271 yc=((double)image->rows-1.0)/2.0+image->page.y;
2272 (void) FormatLocaleFile(stderr,
"Barrel%s Distort, FX Equivalent:\n",
2273 method == BarrelDistortion ?
"" :
"Inv");
2274 (void) FormatLocaleFile(stderr,
"%s", image_gen);
2275 if ( fabs(coeff[8]-xc-0.5) < 0.1 && fabs(coeff[9]-yc-0.5) < 0.1 )
2276 (void) FormatLocaleFile(stderr,
" -fx 'xc=(w-1)/2; yc=(h-1)/2;\n");
2278 (
void) FormatLocaleFile(stderr,
" -fx 'xc=%lf; yc=%lf;\n",coeff[8]-
2280 (void) FormatLocaleFile(stderr,
2281 " ii=i-xc; jj=j-yc; rr=hypot(ii,jj);\n");
2282 (void) FormatLocaleFile(stderr,
2283 " ii=ii%s(%lf*rr*rr*rr %+lf*rr*rr %+lf*rr %+lf);\n",
2284 method == BarrelDistortion ?
"*" :
"/",coeff[0],coeff[1],coeff[2],
2286 (void) FormatLocaleFile(stderr,
2287 " jj=jj%s(%lf*rr*rr*rr %+lf*rr*rr %+lf*rr %+lf);\n",
2288 method == BarrelDistortion ?
"*" :
"/",coeff[4],coeff[5],coeff[6],
2290 (void) FormatLocaleFile(stderr,
" p{ii+xc,jj+yc}' \\\n");
2301 {
const char *artifact;
2302 artifact=GetImageArtifact(image,
"distort:scale");
2303 output_scaling = 1.0;
2304 if (artifact != (
const char *) NULL) {
2305 output_scaling = fabs(StringToDouble(artifact,(
char **) NULL));
2306 geometry.width=(size_t) (output_scaling*geometry.width+0.5);
2307 geometry.height=(size_t) (output_scaling*geometry.height+0.5);
2308 geometry.x=(ssize_t) (output_scaling*geometry.x+0.5);
2309 geometry.y=(ssize_t) (output_scaling*geometry.y+0.5);
2310 if ( output_scaling < 0.1 ) {
2311 coeff = (
double *) RelinquishMagickMemory(coeff);
2312 (void) ThrowMagickException(exception,GetMagickModule(),
2313 OptionError,
"InvalidArgument",
"%s",
"-define distort:scale" );
2314 return((
Image *) NULL);
2316 output_scaling = 1/output_scaling;
2319#define ScaleFilter(F,A,B,C,D) \
2320 ScaleResampleFilter( (F), \
2321 output_scaling*(A), output_scaling*(B), \
2322 output_scaling*(C), output_scaling*(D) )
2327 distort_image=CloneImage(image,geometry.width,geometry.height,MagickTrue,
2329 if (distort_image == (
Image *) NULL)
2331 coeff=(
double *) RelinquishMagickMemory(coeff);
2332 return((
Image *) NULL);
2335 if (SetImageStorageClass(distort_image,DirectClass) == MagickFalse)
2337 coeff=(
double *) RelinquishMagickMemory(coeff);
2338 InheritException(exception,&distort_image->exception);
2339 distort_image=DestroyImage(distort_image);
2340 return((
Image *) NULL);
2342 if ((IsPixelGray(&distort_image->background_color) == MagickFalse) &&
2343 (IsGrayColorspace(distort_image->colorspace) != MagickFalse))
2344 (void) SetImageColorspace(distort_image,sRGBColorspace);
2345 if (distort_image->background_color.opacity != OpaqueOpacity)
2346 distort_image->matte=MagickTrue;
2347 distort_image->page.x=geometry.x;
2348 distort_image->page.y=geometry.y;
2366 **magick_restrict resample_filter;
2373 GetMagickPixelPacket(distort_image,&zero);
2374 resample_filter=AcquireResampleFilterTLS(image,UndefinedVirtualPixelMethod,
2375 MagickFalse,exception);
2376 distort_view=AcquireAuthenticCacheView(distort_image,exception);
2377#if defined(MAGICKCORE_OPENMP_SUPPORT)
2378 #pragma omp parallel for schedule(static) shared(progress,status) \
2379 magick_number_threads(image,distort_image,distort_image->rows,1)
2381 for (j=0; j < (ssize_t) distort_image->rows; j++)
2384 id = GetOpenMPThreadId();
2401 *magick_restrict indexes;
2409 q=QueueCacheViewAuthenticPixels(distort_view,0,j,distort_image->columns,1,
2416 indexes=GetCacheViewAuthenticIndexQueue(distort_view);
2424 case AffineDistortion:
2425 ScaleFilter( resample_filter[
id],
2427 coeff[3], coeff[4] );
2440 GetMagickPixelPacket(distort_image,&invalid);
2441 SetMagickPixelPacket(distort_image,&distort_image->matte_color,
2442 (IndexPacket *) NULL, &invalid);
2443 if (distort_image->colorspace == CMYKColorspace)
2444 ConvertRGBToCMYK(&invalid);
2446 for (i=0; i < (ssize_t) distort_image->columns; i++)
2449 d.x = (double) (geometry.x+i+0.5)*output_scaling;
2450 d.y = (double) (geometry.y+j+0.5)*output_scaling;
2454 case AffineDistortion:
2456 s.x=coeff[0]*d.x+coeff[1]*d.y+coeff[2];
2457 s.y=coeff[3]*d.x+coeff[4]*d.y+coeff[5];
2461 case PerspectiveDistortion:
2464 p,q,r,abs_r,abs_c6,abs_c7,scale;
2466 p=coeff[0]*d.x+coeff[1]*d.y+coeff[2];
2467 q=coeff[3]*d.x+coeff[4]*d.y+coeff[5];
2468 r=coeff[6]*d.x+coeff[7]*d.y+1.0;
2470 validity = (r*coeff[8] < 0.0) ? 0.0 : 1.0;
2473 abs_c6 = fabs(coeff[6]);
2474 abs_c7 = fabs(coeff[7]);
2475 if ( abs_c6 > abs_c7 ) {
2476 if ( abs_r < abs_c6*output_scaling )
2477 validity = 0.5 - coeff[8]*r/(coeff[6]*output_scaling);
2479 else if ( abs_r < abs_c7*output_scaling )
2480 validity = 0.5 - coeff[8]*r/(coeff[7]*output_scaling);
2482 if ( validity > 0.0 ) {
2489 ScaleFilter( resample_filter[
id],
2490 (r*coeff[0] - p*coeff[6])*scale,
2491 (r*coeff[1] - p*coeff[7])*scale,
2492 (r*coeff[3] - q*coeff[6])*scale,
2493 (r*coeff[4] - q*coeff[7])*scale );
2497 case BilinearReverseDistortion:
2500 s.x=coeff[0]*d.x+coeff[1]*d.y+coeff[2]*d.x*d.y+coeff[3];
2501 s.y=coeff[4]*d.x+coeff[5]*d.y
2502 +coeff[6]*d.x*d.y+coeff[7];
2504 ScaleFilter( resample_filter[
id],
2505 coeff[0] + coeff[2]*d.y,
2506 coeff[1] + coeff[2]*d.x,
2507 coeff[4] + coeff[6]*d.y,
2508 coeff[5] + coeff[6]*d.x );
2511 case BilinearForwardDistortion:
2516 d.x -= coeff[3]; d.y -= coeff[7];
2517 b = coeff[6]*d.x - coeff[2]*d.y + coeff[8];
2518 c = coeff[4]*d.x - coeff[0]*d.y;
2523 if ( fabs(coeff[9]) < MagickEpsilon )
2526 c = b*b - 2*coeff[9]*c;
2530 s.y = ( -b + sqrt(c) )/coeff[9];
2532 if ( validity > 0.0 )
2533 s.x = ( d.x - coeff[1]*s.y) / ( coeff[0] + coeff[2]*s.y );
2543 case BilinearDistortion:
2548 case PolynomialDistortion:
2555 nterms=(ssize_t)coeff[1];
2560 s.x=s.y=du.x=du.y=dv.x=dv.y=0.0;
2561 for(k=0; k < nterms; k++) {
2562 s.x += poly_basis_fn(k,d.x,d.y)*coeff[2+k];
2563 du.x += poly_basis_dx(k,d.x,d.y)*coeff[2+k];
2564 du.y += poly_basis_dy(k,d.x,d.y)*coeff[2+k];
2565 s.y += poly_basis_fn(k,d.x,d.y)*coeff[2+k+nterms];
2566 dv.x += poly_basis_dx(k,d.x,d.y)*coeff[2+k+nterms];
2567 dv.y += poly_basis_dy(k,d.x,d.y)*coeff[2+k+nterms];
2569 ScaleFilter( resample_filter[
id], du.x,du.y,dv.x,dv.y );
2575 s.x = (double) ((atan2(d.y,d.x) - coeff[0])/Magick2PI);
2576 s.x -= MagickRound(s.x);
2577 s.y = hypot(d.x,d.y);
2584 if ( s.y > MagickEpsilon )
2585 ScaleFilter( resample_filter[
id],
2586 (
double) (coeff[1]/(Magick2PI*s.y)), 0, 0, coeff[3] );
2588 ScaleFilter( resample_filter[
id],
2589 distort_image->columns*2, 0, 0, coeff[3] );
2592 s.x = s.x*coeff[1] + coeff[4] + image->page.x +0.5;
2593 s.y = (coeff[2] - s.y) * coeff[3] + image->page.y;
2596 case PolarDistortion:
2600 s.x = atan2(d.x,d.y) - (coeff[4]+coeff[5])/2;
2602 s.x -= MagickRound(s.x);
2604 s.y = hypot(d.x,d.y);
2609 if ( s.y > MagickEpsilon )
2610 ScaleFilter( resample_filter[
id],
2611 (
double) (coeff[6]/(Magick2PI*s.y)), 0, 0, coeff[7] );
2613 ScaleFilter( resample_filter[
id],
2614 distort_image->columns*2, 0, 0, coeff[7] );
2617 s.x = s.x*coeff[6] + (double)image->columns/2.0 + image->page.x;
2618 s.y = (s.y-coeff[1])*coeff[7] + image->page.y;
2621 case DePolarDistortion:
2624 d.x = ((double)i+0.5)*output_scaling*coeff[6]+coeff[4];
2625 d.y = ((double)j+0.5)*output_scaling*coeff[7]+coeff[1];
2626 s.x = d.y*sin(d.x) + coeff[2];
2627 s.y = d.y*cos(d.x) + coeff[3];
2631 case Cylinder2PlaneDistortion:
2635 d.x -= coeff[4]; d.y -= coeff[5];
2642 ScaleFilter( resample_filter[
id],
2643 1.0/(1.0+d.x*d.x), 0.0, -d.x*s.y*cx*cx/coeff[1], s.y/d.y );
2645if ( i == 0 && j == 0 ) {
2646 fprintf(stderr,
"x=%lf y=%lf u=%lf v=%lf\n", d.x*coeff[1], d.y, s.x, s.y);
2647 fprintf(stderr,
"phi = %lf\n", (
double)(ax * 180.0/MagickPI) );
2648 fprintf(stderr,
"du/dx=%lf du/dx=%lf dv/dx=%lf dv/dy=%lf\n",
2649 1.0/(1.0+d.x*d.x), 0.0, -d.x*s.y*cx*cx/coeff[1], s.y/d.y );
2653 s.x += coeff[2]; s.y += coeff[3];
2656 case Plane2CylinderDistortion:
2659 d.x -= coeff[4]; d.y -= coeff[5];
2663 validity = (double) ((coeff[1]*MagickPI2 - fabs(d.x))/output_scaling + 0.5);
2665 if ( validity > 0.0 ) {
2673 ScaleFilter( resample_filter[
id],
2674 cx*cx, 0.0, s.y*cx/coeff[1], cx );
2677if ( d.x == 0.5 && d.y == 0.5 ) {
2678 fprintf(stderr,
"x=%lf y=%lf u=%lf v=%lf\n", d.x*coeff[1], d.y, s.x, s.y);
2679 fprintf(stderr,
"radius = %lf phi = %lf validity = %lf\n",
2680 coeff[1], (
double)(d.x * 180.0/MagickPI), validity );
2681 fprintf(stderr,
"du/dx=%lf du/dx=%lf dv/dx=%lf dv/dy=%lf\n",
2682 cx*cx, 0.0, s.y*cx/coeff[1], cx);
2687 s.x += coeff[2]; s.y += coeff[3];
2690 case BarrelDistortion:
2691 case BarrelInverseDistortion:
2693 double r,fx,fy,gx,gy;
2697 r = sqrt(d.x*d.x+d.y*d.y);
2698 if ( r > MagickEpsilon ) {
2699 fx = ((coeff[0]*r + coeff[1])*r + coeff[2])*r + coeff[3];
2700 fy = ((coeff[4]*r + coeff[5])*r + coeff[6])*r + coeff[7];
2701 gx = ((3*coeff[0]*r + 2*coeff[1])*r + coeff[2])/r;
2702 gy = ((3*coeff[4]*r + 2*coeff[5])*r + coeff[6])/r;
2704 if ( method == BarrelInverseDistortion ) {
2705 fx = 1/fx; fy = 1/fy;
2706 gx *= -fx*fx; gy *= -fy*fy;
2709 s.x = d.x*fx + coeff[8];
2710 s.y = d.y*fy + coeff[9];
2711 ScaleFilter( resample_filter[
id],
2712 gx*d.x*d.x + fx, gx*d.x*d.y,
2713 gy*d.x*d.y, gy*d.y*d.y + fy );
2722 if ( method == BarrelDistortion )
2723 ScaleFilter( resample_filter[
id],
2724 coeff[3], 0, 0, coeff[7] );
2727 ScaleFilter( resample_filter[
id],
2728 1.0/coeff[3], 0, 0, 1.0/coeff[7] );
2732 case ShepardsDistortion:
2747 denominator = s.x = s.y = 0;
2748 for(i=0; i<number_arguments; i+=4) {
2750 ((double)d.x-arguments[i+2])*((
double)d.x-arguments[i+2])
2751 + ((double)d.y-arguments[i+3])*((
double)d.y-arguments[i+3]);
2752 weight = pow(weight,coeff[0]);
2753 weight = ( weight < 1.0 ) ? 1.0 : 1.0/weight;
2755 s.x += (arguments[ i ]-arguments[i+2])*weight;
2756 s.y += (arguments[i+1]-arguments[i+3])*weight;
2757 denominator += weight;
2769 if ( bestfit && method != ArcDistortion ) {
2770 s.x -= image->page.x;
2771 s.y -= image->page.y;
2776 if ( validity <= 0.0 ) {
2778 SetPixelPacket(distort_image,&invalid,q,indexes);
2782 status=ResamplePixelColor(resample_filter[
id],s.x,s.y,&pixel);
2783 if (status == MagickFalse)
2784 SetPixelPacket(distort_image,&invalid,q,indexes);
2788 if ( validity < 1.0 ) {
2791 MagickPixelCompositeBlend(&pixel,validity,&invalid,(1.0-validity),
2795 SetPixelPacket(distort_image,&pixel,q,indexes);
2800 sync=SyncCacheViewAuthenticPixels(distort_view,exception);
2801 if (sync == MagickFalse)
2803 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2808#if defined(MAGICKCORE_OPENMP_SUPPORT)
2812 proceed=SetImageProgress(image,DistortImageTag,progress,image->rows);
2813 if (proceed == MagickFalse)
2817 distort_view=DestroyCacheView(distort_view);
2818 resample_filter=DestroyResampleFilterTLS(resample_filter);
2820 if (status == MagickFalse)
2821 distort_image=DestroyImage(distort_image);
2827 if ( method == ArcDistortion && !bestfit && !viewport_given ) {
2828 distort_image->page.x = 0;
2829 distort_image->page.y = 0;
2831 coeff=(
double *) RelinquishMagickMemory(coeff);
2832 return(distort_image);
2869MagickExport
Image *RotateImage(
const Image *image,
const double degrees,
2888 assert(image != (
Image *) NULL);
2889 assert(image->signature == MagickCoreSignature);
2891 assert(exception->signature == MagickCoreSignature);
2892 if (IsEventLogging() != MagickFalse)
2893 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2894 angle=fmod(degrees,360.0);
2895 while (angle < -45.0)
2897 for (rotations=0; angle > 45.0; rotations++)
2900 shear.x=(-tan((
double) DegreesToRadians(angle)/2.0));
2901 shear.y=sin((
double) DegreesToRadians(angle));
2902 if ((fabs(shear.x) < MagickEpsilon) && (fabs(shear.y) < MagickEpsilon))
2903 return(IntegralRotateImage(image,rotations,exception));
2904 distort_image=CloneImage(image,0,0,MagickTrue,exception);
2905 if (distort_image == (
Image *) NULL)
2906 return((
Image *) NULL);
2907 (void) SetImageVirtualPixelMethod(distort_image,BackgroundVirtualPixelMethod);
2908 rotate_image=DistortImage(distort_image,ScaleRotateTranslateDistortion,1,
2909 °rees,MagickTrue,exception);
2910 distort_image=DestroyImage(distort_image);
2911 return(rotate_image);
2956MagickExport
Image *SparseColorImage(
const Image *image,
2957 const ChannelType channel,
const SparseColorMethod method,
2958 const size_t number_arguments,
const double *arguments,
2961#define SparseColorTag "Distort/SparseColor"
2975 assert(image != (
Image *) NULL);
2976 assert(image->signature == MagickCoreSignature);
2978 assert(exception->signature == MagickCoreSignature);
2979 if (IsEventLogging() != MagickFalse)
2980 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2985 if ( channel & RedChannel ) number_colors++;
2986 if ( channel & GreenChannel ) number_colors++;
2987 if ( channel & BlueChannel ) number_colors++;
2988 if ( channel & IndexChannel ) number_colors++;
2989 if ( channel & OpacityChannel ) number_colors++;
2995 { DistortImageMethod
2998 distort_method=(DistortImageMethod) method;
2999 if ( distort_method >= SentinelDistortion )
3000 distort_method = ShepardsDistortion;
3001 coeff = GenerateCoefficients(image, &distort_method, number_arguments,
3002 arguments, number_colors, exception);
3003 if ( coeff == (
double *) NULL )
3004 return((
Image *) NULL);
3011 sparse_method = (SparseColorMethod) distort_method;
3012 if ( distort_method == ShepardsDistortion )
3013 sparse_method = method;
3014 if ( sparse_method == InverseColorInterpolate )
3019 if ( GetImageArtifact(image,
"verbose") != (
const char *) NULL ) {
3021 switch (sparse_method) {
3022 case BarycentricColorInterpolate:
3025 (void) FormatLocaleFile(stderr,
"Barycentric Sparse Color:\n");
3026 if ( channel & RedChannel )
3027 (void) FormatLocaleFile(stderr,
" -channel R -fx '%+lf*i %+lf*j %+lf' \\\n",
3028 coeff[x], coeff[x+1], coeff[x+2]),x+=3;
3029 if ( channel & GreenChannel )
3030 (void) FormatLocaleFile(stderr,
" -channel G -fx '%+lf*i %+lf*j %+lf' \\\n",
3031 coeff[x], coeff[x+1], coeff[x+2]),x+=3;
3032 if ( channel & BlueChannel )
3033 (void) FormatLocaleFile(stderr,
" -channel B -fx '%+lf*i %+lf*j %+lf' \\\n",
3034 coeff[x], coeff[x+1], coeff[x+2]),x+=3;
3035 if ( channel & IndexChannel )
3036 (void) FormatLocaleFile(stderr,
" -channel K -fx '%+lf*i %+lf*j %+lf' \\\n",
3037 coeff[x], coeff[x+1], coeff[x+2]),x+=3;
3038 if ( channel & OpacityChannel )
3039 (void) FormatLocaleFile(stderr,
" -channel A -fx '%+lf*i %+lf*j %+lf' \\\n",
3040 coeff[x], coeff[x+1], coeff[x+2]),x+=3;
3043 case BilinearColorInterpolate:
3046 (void) FormatLocaleFile(stderr,
"Bilinear Sparse Color\n");
3047 if ( channel & RedChannel )
3048 (void) FormatLocaleFile(stderr,
" -channel R -fx '%+lf*i %+lf*j %+lf*i*j %+lf;\n",
3049 coeff[ x ], coeff[x+1],
3050 coeff[x+2], coeff[x+3]),x+=4;
3051 if ( channel & GreenChannel )
3052 (void) FormatLocaleFile(stderr,
" -channel G -fx '%+lf*i %+lf*j %+lf*i*j %+lf;\n",
3053 coeff[ x ], coeff[x+1],
3054 coeff[x+2], coeff[x+3]),x+=4;
3055 if ( channel & BlueChannel )
3056 (void) FormatLocaleFile(stderr,
" -channel B -fx '%+lf*i %+lf*j %+lf*i*j %+lf;\n",
3057 coeff[ x ], coeff[x+1],
3058 coeff[x+2], coeff[x+3]),x+=4;
3059 if ( channel & IndexChannel )
3060 (void) FormatLocaleFile(stderr,
" -channel K -fx '%+lf*i %+lf*j %+lf*i*j %+lf;\n",
3061 coeff[ x ], coeff[x+1],
3062 coeff[x+2], coeff[x+3]),x+=4;
3063 if ( channel & OpacityChannel )
3064 (void) FormatLocaleFile(stderr,
" -channel A -fx '%+lf*i %+lf*j %+lf*i*j %+lf;\n",
3065 coeff[ x ], coeff[x+1],
3066 coeff[x+2], coeff[x+3]),x+=4;
3081 sparse_image=CloneImage(image,0,0,MagickTrue,exception);
3082 if (sparse_image == (
Image *) NULL)
3083 return((
Image *) NULL);
3084 if (SetImageStorageClass(sparse_image,DirectClass) == MagickFalse)
3086 InheritException(exception,&image->exception);
3087 sparse_image=DestroyImage(sparse_image);
3088 return((
Image *) NULL);
3105 sparse_view=AcquireAuthenticCacheView(sparse_image,exception);
3106#if defined(MAGICKCORE_OPENMP_SUPPORT)
3107 #pragma omp parallel for schedule(static) shared(progress,status) \
3108 magick_number_threads(image,sparse_image,sparse_image->rows,1)
3110 for (j=0; j < (ssize_t) sparse_image->rows; j++)
3119 *magick_restrict indexes;
3127 q=GetCacheViewAuthenticPixels(sparse_view,0,j,sparse_image->columns,
3134 indexes=GetCacheViewAuthenticIndexQueue(sparse_view);
3135 GetMagickPixelPacket(sparse_image,&pixel);
3136 for (i=0; i < (ssize_t) image->columns; i++)
3138 SetMagickPixelPacket(image,q,indexes,&pixel);
3139 switch (sparse_method)
3141 case BarycentricColorInterpolate:
3144 if ( channel & RedChannel )
3145 pixel.red = coeff[x]*i +coeff[x+1]*j
3147 if ( channel & GreenChannel )
3148 pixel.green = coeff[x]*i +coeff[x+1]*j
3150 if ( channel & BlueChannel )
3151 pixel.blue = coeff[x]*i +coeff[x+1]*j
3153 if ( channel & IndexChannel )
3154 pixel.index = coeff[x]*i +coeff[x+1]*j
3156 if ( channel & OpacityChannel )
3157 pixel.opacity = coeff[x]*i +coeff[x+1]*j
3161 case BilinearColorInterpolate:
3164 if ( channel & RedChannel )
3165 pixel.red = coeff[x]*i + coeff[x+1]*j +
3166 coeff[x+2]*i*j + coeff[x+3], x+=4;
3167 if ( channel & GreenChannel )
3168 pixel.green = coeff[x]*i + coeff[x+1]*j +
3169 coeff[x+2]*i*j + coeff[x+3], x+=4;
3170 if ( channel & BlueChannel )
3171 pixel.blue = coeff[x]*i + coeff[x+1]*j +
3172 coeff[x+2]*i*j + coeff[x+3], x+=4;
3173 if ( channel & IndexChannel )
3174 pixel.index = coeff[x]*i + coeff[x+1]*j +
3175 coeff[x+2]*i*j + coeff[x+3], x+=4;
3176 if ( channel & OpacityChannel )
3177 pixel.opacity = coeff[x]*i + coeff[x+1]*j +
3178 coeff[x+2]*i*j + coeff[x+3], x+=4;
3181 case InverseColorInterpolate:
3182 case ShepardsColorInterpolate:
3189 if ( channel & RedChannel ) pixel.red = 0.0;
3190 if ( channel & GreenChannel ) pixel.green = 0.0;
3191 if ( channel & BlueChannel ) pixel.blue = 0.0;
3192 if ( channel & IndexChannel ) pixel.index = 0.0;
3193 if ( channel & OpacityChannel ) pixel.opacity = 0.0;
3195 for(k=0; k<number_arguments; k+=2+number_colors) {
3196 ssize_t x=(ssize_t) k+2;
3198 ((double)i-arguments[ k ])*((
double)i-arguments[ k ])
3199 + ((double)j-arguments[k+1])*((
double)j-arguments[k+1]);
3200 weight = pow(weight,coeff[0]);
3201 weight = ( weight < 1.0 ) ? 1.0 : 1.0/weight;
3202 if ( channel & RedChannel )
3203 pixel.red += arguments[x++]*weight;
3204 if ( channel & GreenChannel )
3205 pixel.green += arguments[x++]*weight;
3206 if ( channel & BlueChannel )
3207 pixel.blue += arguments[x++]*weight;
3208 if ( channel & IndexChannel )
3209 pixel.index += arguments[x++]*weight;
3210 if ( channel & OpacityChannel )
3211 pixel.opacity += arguments[x++]*weight;
3212 denominator += weight;
3214 if ( channel & RedChannel ) pixel.red /= denominator;
3215 if ( channel & GreenChannel ) pixel.green /= denominator;
3216 if ( channel & BlueChannel ) pixel.blue /= denominator;
3217 if ( channel & IndexChannel ) pixel.index /= denominator;
3218 if ( channel & OpacityChannel ) pixel.opacity /= denominator;
3221 case ManhattanColorInterpolate:
3227 minimum = MagickMaximumValue;
3232 for(k=0; k<number_arguments; k+=2+number_colors) {
3234 fabs((
double)i-arguments[ k ])
3235 + fabs((
double)j-arguments[k+1]);
3236 if ( distance < minimum ) {
3237 ssize_t x=(ssize_t) k+2;
3238 if ( channel & RedChannel ) pixel.red = arguments[x++];
3239 if ( channel & GreenChannel ) pixel.green = arguments[x++];
3240 if ( channel & BlueChannel ) pixel.blue = arguments[x++];
3241 if ( channel & IndexChannel ) pixel.index = arguments[x++];
3242 if ( channel & OpacityChannel ) pixel.opacity = arguments[x++];
3248 case VoronoiColorInterpolate:
3255 minimum = MagickMaximumValue;
3260 for(k=0; k<number_arguments; k+=2+number_colors) {
3262 ((double)i-arguments[ k ])*((
double)i-arguments[ k ])
3263 + ((double)j-arguments[k+1])*((
double)j-arguments[k+1]);
3264 if ( distance < minimum ) {
3265 ssize_t x=(ssize_t) k+2;
3266 if ( channel & RedChannel ) pixel.red = arguments[x++];
3267 if ( channel & GreenChannel ) pixel.green = arguments[x++];
3268 if ( channel & BlueChannel ) pixel.blue = arguments[x++];
3269 if ( channel & IndexChannel ) pixel.index = arguments[x++];
3270 if ( channel & OpacityChannel ) pixel.opacity = arguments[x++];
3278 if ( channel & RedChannel )
3279 pixel.red=ClampPixel((MagickRealType) QuantumRange*pixel.red);
3280 if ( channel & GreenChannel )
3281 pixel.green=ClampPixel((MagickRealType) QuantumRange*pixel.green);
3282 if ( channel & BlueChannel )
3283 pixel.blue=ClampPixel((MagickRealType) QuantumRange*pixel.blue);
3284 if ( channel & IndexChannel )
3285 pixel.index=ClampPixel((MagickRealType) QuantumRange*pixel.index);
3286 if ( channel & OpacityChannel )
3287 pixel.opacity=ClampPixel((MagickRealType) QuantumRange*pixel.opacity);
3288 SetPixelPacket(sparse_image,&pixel,q,indexes);
3292 sync=SyncCacheViewAuthenticPixels(sparse_view,exception);
3293 if (sync == MagickFalse)
3295 if (image->progress_monitor != (MagickProgressMonitor) NULL)
3300#if defined(MAGICKCORE_OPENMP_SUPPORT)
3304 proceed=SetImageProgress(image,SparseColorTag,progress,image->rows);
3305 if (proceed == MagickFalse)
3309 sparse_view=DestroyCacheView(sparse_view);
3310 if (status == MagickFalse)
3311 sparse_image=DestroyImage(sparse_image);
3313 coeff = (
double *) RelinquishMagickMemory(coeff);
3314 return(sparse_image);