Changeset 11628 for ImageMagick/trunk

Show
Ignore:
Timestamp:
07/14/08 07:32:22 (3 months ago)
Author:
anthony
Message:
 
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • ImageMagick/trunk/magick/distort.c

    r11627 r11628  
    395395        return((double *) NULL); 
    396396      } 
    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 ) 
    399400      { 
    400401        (void) ThrowMagickException(exception,GetMagickModule(),OptionError, 
     
    403404        return((double *) NULL); 
    404405      } 
    405       if ( number_arguments < number_coefficients*4+1) 
     406      if ( number_arguments < i*4+1) 
    406407      { 
    407408        (void) ThrowMagickException(exception,GetMagickModule(),OptionError, 
     
    410411        return((double *) NULL); 
    411412      } 
    412       number_coefficients = number_coefficients*2+1; 
    413413      break; 
    414414    case ArcDistortion: 
     
    523523      /* Solve for LeastSquares Coefficients */ 
    524524        status=GaussJordanElimination(matrix,vectors,3UL,2UL); 
     525        matrix = RelinquishMagickMatrix(matrix, 3UL); 
    525526        if ( status == MagickTrue ) { 
    526527          /* Affine is a bit weird in its ordering of coefficients. 
     
    539540          coefficients[5] = vectors[1][2]; /* ty */ 
    540541        } 
    541         matrix = RelinquishMagickMatrix(matrix, 3UL); 
    542542        vectors = RelinquishMagickMatrix(vectors, 2UL); 
    543543        if ( status == MagickFalse ) { 
     
    812812    { 
    813813      /* 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 + ... 
    817818            v = .... 
    818819 
     
    835836        status; 
    836837 
    837       nterms = coefficients[0] = poly_number_terms(arguments[0]); 
     838      coefficients[0] = arguments[0]; 
     839      coefficients[1] = nterms = poly_number_terms(arguments[0]); 
    838840 
    839841      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); 
    841846        coefficients = RelinquishMagickMemory(coefficients); 
    842847        (void) ThrowMagickException(exception,GetMagickModule(), 
     
    845850        return((double *) NULL); 
    846851      } 
    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       } 
    856852      /* 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]); 
    859855      /* 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) { 
    861857        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]); 
    863859        LeastSquaresAddTerms(matrix,vectors,terms,&(arguments[i]),nterms,2UL); 
    864860      } 
     
    875871      } 
    876872      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 ="); 
    879876        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 ="); 
    884881        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"); 
    889886      } 
    890887      return(coefficients); 
     
    14951492          register long 
    14961493            k; 
     1494          long 
     1495            nterms=(long)coefficients[1]; 
    14971496 
    14981497          double 
     
    15001499 
    15011500          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]; 
    15091508          } 
    15101509          ScaleResampleFilter( resample_filter[id], dudx,dudy,dvdx,dvdy );