Changeset 11628 for ImageMagick/trunk
- Timestamp:
- 07/14/08 07:32:22 (3 months ago)
- Files:
-
- 1 modified
-
ImageMagick/trunk/magick/distort.c (modified) (11 diffs)
Legend:
- Unmodified
- Added
- Removed
-
ImageMagick/trunk/magick/distort.c
r11627 r11628 395 395 return((double *) NULL); 396 396 } 397 number_coefficients = poly_number_terms(arguments[0]); 398 if ( number_coefficients == 0 ) 397 i = poly_number_terms(arguments[0]); 398 number_coefficients = i*2 + 2; 399 if ( i == 0 ) 399 400 { 400 401 (void) ThrowMagickException(exception,GetMagickModule(),OptionError, … … 403 404 return((double *) NULL); 404 405 } 405 if ( number_arguments < number_coefficients*4+1)406 if ( number_arguments < i*4+1) 406 407 { 407 408 (void) ThrowMagickException(exception,GetMagickModule(),OptionError, … … 410 411 return((double *) NULL); 411 412 } 412 number_coefficients = number_coefficients*2+1;413 413 break; 414 414 case ArcDistortion: … … 523 523 /* Solve for LeastSquares Coefficients */ 524 524 status=GaussJordanElimination(matrix,vectors,3UL,2UL); 525 matrix = RelinquishMagickMatrix(matrix, 3UL); 525 526 if ( status == MagickTrue ) { 526 527 /* Affine is a bit weird in its ordering of coefficients. … … 539 540 coefficients[5] = vectors[1][2]; /* ty */ 540 541 } 541 matrix = RelinquishMagickMatrix(matrix, 3UL);542 542 vectors = RelinquishMagickMatrix(vectors, 2UL); 543 543 if ( status == MagickFalse ) { … … 812 812 { 813 813 /* Polynomial Distortion 814 c0 = number of terms in one polynomial equation 815 816 u = c1 + c1*x + c2*y + c3*x*y + ... + c8*x^3 + c9*y^3 814 c0 = Order of the polynimial being created 815 c1 = number of terms in one polynomial equation 816 817 u = c2 + c3*x + c4*y + c5*x*y + c6*x^2 + c7*y^2 + c8*x^3 + ... 817 818 v = .... 818 819 … … 835 836 status; 836 837 837 nterms = coefficients[0] = poly_number_terms(arguments[0]); 838 coefficients[0] = arguments[0]; 839 coefficients[1] = nterms = poly_number_terms(arguments[0]); 838 840 839 841 terms = AcquireQuantumMemory(nterms, sizeof(*terms)); 840 if (terms == (double *) NULL) { 842 matrix = AcquireMagickMatrix(nterms,nterms); 843 if (terms == (double *) NULL && matrix == (double **) NULL) { 844 terms = RelinquishMagickMemory(terms); 845 matrix = RelinquishMagickMatrix(matrix, nterms); 841 846 coefficients = RelinquishMagickMemory(coefficients); 842 847 (void) ThrowMagickException(exception,GetMagickModule(), … … 845 850 return((double *) NULL); 846 851 } 847 matrix = AcquireMagickMatrix(nterms,nterms);848 if (matrix == (double **) NULL) {849 terms = RelinquishMagickMemory(terms);850 coefficients = RelinquishMagickMemory(coefficients);851 (void) ThrowMagickException(exception,GetMagickModule(),852 ResourceLimitError,"MemoryAllocationFailed",853 "%s", "DistortCoefficients");854 return((double *) NULL);855 }856 852 /* fake a 2xN vectors matrix from rest of coefficients array */ 857 vectors[0] = &(coefficients[ 1]);858 vectors[1] = &(coefficients[ nterms+1]);853 vectors[0] = &(coefficients[2]); 854 vectors[1] = &(coefficients[2+nterms]); 859 855 /* Add control points for least squares solving */ 860 for (i= 0; i < (long)number_arguments; i+=4) {856 for (i=2; i < number_arguments; i+=4) { 861 857 for (j=0; j < nterms; j++) 862 terms[j] = poly_term(j,arguments[i =2],arguments[i+3]);858 terms[j] = poly_term(j,arguments[i+2],arguments[i+3]); 863 859 LeastSquaresAddTerms(matrix,vectors,terms,&(arguments[i]),nterms,2UL); 864 860 } … … 875 871 } 876 872 if ( GetImageArtifact(image,"distort:verbose") != (const char *) NULL ) { 877 fprintf(stderr, "Polynomial (%ld terms) Reverse Map\n", (long)nterms); 878 fprintf(stderr, " -fx 'xx="); 873 fprintf(stderr, "Polynomial (order %lg, terms %lg) Reverse Map\n", 874 coefficients[0], coefficients[1]); 875 fprintf(stderr, " -fx 'xx ="); 879 876 for (i=0; i<nterms; i++) { 880 if ( i != 0 && i%4 == 0 ) fprintf(stderr, "\n ");881 fprintf(stderr, " % lf%s", coefficients[i+1], poly_term_str(i));882 } 883 fprintf(stderr, " \n yy=");877 if ( i != 0 && i%4 == 0 ) fprintf(stderr, "\n "); 878 fprintf(stderr, " %+lf%s", coefficients[i+2], poly_term_str(i)); 879 } 880 fprintf(stderr, ";\n yy ="); 884 881 for (i=0; i<nterms; i++) { 885 if ( i != 0 && i%4 == 0 ) fprintf(stderr, "\n ");886 fprintf(stderr, " % lf%s", coefficients[i+1+nterms], poly_term_str(i));887 } 888 fprintf(stderr, " \n p{xx,yy}'\n");882 if ( i != 0 && i%4 == 0 ) fprintf(stderr, "\n "); 883 fprintf(stderr, " %+lf%s", coefficients[i+2+nterms], poly_term_str(i)); 884 } 885 fprintf(stderr, ";\n p{xx,yy}'\n"); 889 886 } 890 887 return(coefficients); … … 1495 1492 register long 1496 1493 k; 1494 long 1495 nterms=(long)coefficients[1]; 1497 1496 1498 1497 double … … 1500 1499 1501 1500 point.x=point.y=dudx=dudy=dvdx=dvdy=0.0; 1502 for(k=0; k < (long)coefficients[0]; k++) {1503 point.x += poly_term(k,x,y)*coefficients[k+ 1];1504 dudx += poly_term_dx(k,x,y)*coefficients[k+ 1];1505 dudy += poly_term_dy(k,x,y)*coefficients[k+ 1];1506 point.y += poly_term(k,x,y)*coefficients[k+ 1+(long)coefficients[0]];1507 dvdx += poly_term_dx(k,x,y)*coefficients[k+ 1+(long)coefficients[0]];1508 dvdy += poly_term_dy(k,x,y)*coefficients[k+ 1+(long)coefficients[0]];1501 for(k=0; k < nterms; k++) { 1502 point.x += poly_term(k,x,y)*coefficients[k+2]; 1503 dudx += poly_term_dx(k,x,y)*coefficients[k+2]; 1504 dudy += poly_term_dy(k,x,y)*coefficients[k+2]; 1505 point.y += poly_term(k,x,y)*coefficients[k+2+nterms]; 1506 dvdx += poly_term_dx(k,x,y)*coefficients[k+2+nterms]; 1507 dvdy += poly_term_dy(k,x,y)*coefficients[k+2+nterms]; 1509 1508 } 1510 1509 ScaleResampleFilter( resample_filter[id], dudx,dudy,dvdx,dvdy );
