% MagickCore Image Distortion Methods %
% %
% Software Design %
-% John Cristy %
+% Cristy %
% Anthony Thyssen %
% June 2007 %
% %
% %
-% Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
+% Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization %
% dedicated to making software imaging solutions freely available. %
% %
% You may not use this file except in compliance with the License. You may %
#include "MagickCore/artifact.h"
#include "MagickCore/cache.h"
#include "MagickCore/cache-view.h"
+#include "MagickCore/channel.h"
#include "MagickCore/colorspace-private.h"
#include "MagickCore/composite-private.h"
#include "MagickCore/distort.h"
inverse[7]=determinant*(coeff[6]*coeff[1]-coeff[0]*coeff[7]);
}
-static inline double MagickRound(double x)
-{
- /*
- Round the fraction to nearest integer.
- */
- if (x >= 0.0)
- return((double) ((ssize_t) (x+0.5)));
- return((double) ((ssize_t) (x-0.5)));
-}
-
/*
* Polynomial Term Defining Functions
*
% outside other MagickCore library methods.
*/
+static inline double MagickRound(double x)
+{
+ /*
+ Round the fraction to nearest integer.
+ */
+ if ((x-floor(x)) < (ceil(x)-x))
+ return(floor(x));
+ return(ceil(x));
+}
+
static double *GenerateCoefficients(const Image *image,
DistortImageMethod *method,const size_t number_arguments,
const double *arguments,size_t number_values,ExceptionInfo *exception)
#endif
break;
case ShepardsDistortion:
- number_coeff=1; /* not used, but provide some type of return */
+ number_coeff=1; /* The power factor to use */
break;
case ArcDistortion:
number_coeff=5;
number_coeff=10;
break;
default:
- assert(! "Unknown Method Given"); /* just fail assertion */
+ perror("unknown method given"); /* just fail assertion */
}
/* allocate the array of coefficients needed */
if ( number_arguments%cp_size != 0 ||
number_arguments < cp_size ) {
(void) ThrowMagickException(exception,GetMagickModule(),OptionError,
- "InvalidArgument", "%s : 'require at least %.20g CPs'",
- CommandOptionToMnemonic(MagickDistortOptions, *method), 1.0);
+ "InvalidArgument", "%s : 'requires CP's (4 numbers each)'",
+ CommandOptionToMnemonic(MagickDistortOptions, *method));
coeff=(double *) RelinquishMagickMemory(coeff);
return((double *) NULL);
}
+ /* User defined weighting power for Shepard's Method */
+ { const char *artifact=GetImageArtifact(image,"shepards:power");
+ if ( artifact != (const char *) NULL ) {
+ coeff[0]=StringToDouble(artifact,(char **) NULL) / 2.0;
+ if ( coeff[0] < MagickEpsilon ) {
+ (void) ThrowMagickException(exception,GetMagickModule(),
+ OptionError,"InvalidArgument","%s", "-define shepards:power" );
+ coeff=(double *) RelinquishMagickMemory(coeff);
+ return((double *) NULL);
+ }
+ }
+ else
+ coeff[0]=1.0; /* Default power of 2 (Inverse Squared) */
+ }
return(coeff);
}
default:
break;
}
/* you should never reach this point */
- assert(! "No Method Handler"); /* just fail assertion */
+ perror("no method handler"); /* just fail assertion */
return((double *) NULL);
}
\f
{
#define DistortResizeImageTag "Distort/Image"
- double
- distort_args[12];
-
Image
*resize_image,
*tmp_image;
RectangleInfo
crop_area;
+ double
+ distort_args[12];
+
VirtualPixelMethod
vp_save;
return((Image *) NULL);
(void) SetImageVirtualPixelMethod(tmp_image,TransparentVirtualPixelMethod,
exception);
+ tmp_image->image_info=image->image_info; /* preserve global options */
if (image->alpha_trait != BlendPixelTrait)
{
tmp_image=CloneImage(image,0,0,MagickTrue,exception);
if ( tmp_image == (Image *) NULL )
return((Image *) NULL);
+ tmp_image->image_info=image->image_info; /* preserve global options */
(void) SetImageVirtualPixelMethod(tmp_image,
TransparentVirtualPixelMethod,exception);
resize_image=DistortImage(tmp_image,AffineDistortion,12,distort_args,
tmp_image=resize_image;
resize_image=CropImage(tmp_image,&crop_area,exception);
tmp_image=DestroyImage(tmp_image);
-
- if ( resize_image == (Image *) NULL )
- return((Image *) NULL);
-
return(resize_image);
}
\f
assert(exception != (ExceptionInfo *) NULL);
assert(exception->signature == MagickSignature);
-
/*
Handle Special Compound Distortions
*/
(void) FormatLocaleFile(stderr, " c%.20g = %+lf\n", (double) i, coeff[i]);
(void) FormatLocaleFile(stderr, "DePolar Distort, FX Equivelent:\n");
(void) FormatLocaleFile(stderr, "%s", image_gen);
- (void) FormatLocaleFile(stderr, " -fx 'aa=(i+.5)*%lf %+lf;\n", coeff[6], -coeff[4] );
+ (void) FormatLocaleFile(stderr, " -fx 'aa=(i+.5)*%lf %+lf;\n", coeff[6], +coeff[4] );
(void) FormatLocaleFile(stderr, " rr=(j+.5)*%lf %+lf;\n", coeff[7], +coeff[1] );
(void) FormatLocaleFile(stderr, " xx=rr*sin(aa) %+lf;\n", coeff[2] );
(void) FormatLocaleFile(stderr, " yy=rr*cos(aa) %+lf;\n", coeff[3] );
}
if ((IsPixelInfoGray(&distort_image->background_color) == MagickFalse) &&
(IsGrayColorspace(distort_image->colorspace) != MagickFalse))
- (void) TransformImageColorspace(distort_image,RGBColorspace,exception);
+ (void) SetImageColorspace(distort_image,sRGBColorspace,exception);
if (distort_image->background_color.alpha_trait == BlendPixelTrait)
distort_image->alpha_trait=BlendPixelTrait;
distort_image->page.x=geometry.x;
distort_view=AcquireAuthenticCacheView(distort_image,exception);
#if defined(MAGICKCORE_OPENMP_SUPPORT)
#pragma omp parallel for schedule(static,4) shared(progress,status) \
- dynamic_number_threads(image,image->columns,image->rows,1)
+ magick_threads(image,distort_image,distort_image->rows,1)
#endif
for (j=0; j < (ssize_t) distort_image->rows; j++)
{
case DePolarDistortion:
{ /* @D Polar to Carteasain */
/* ignore all destination virtual offsets */
- d.x = ((double)i+0.5)*output_scaling*coeff[6]-coeff[4];
+ d.x = ((double)i+0.5)*output_scaling*coeff[6]+coeff[4];
d.y = ((double)j+0.5)*output_scaling*coeff[7]+coeff[1];
s.x = d.y*sin(d.x) + coeff[2];
s.y = d.y*cos(d.x) + coeff[3];
}
case ShepardsDistortion:
{ /* Shepards Method, or Inverse Weighted Distance for
- displacement around the destination image control points
- The input arguments are the coefficents to the function.
- This is more of a 'displacement' function rather than an
- absolute distortion function.
+ displacement around the destination image control points
+ The input arguments are the coefficents to the function.
+ This is more of a 'displacement' function rather than an
+ absolute distortion function.
+
+ Note: We can not determine derivatives using shepards method
+ so only a point sample interpolatation can be used.
*/
size_t
i;
double weight =
((double)d.x-arguments[i+2])*((double)d.x-arguments[i+2])
+ ((double)d.y-arguments[i+3])*((double)d.y-arguments[i+3]);
- if ( weight != 0 )
- weight = 1/weight;
- else
- weight = 1;
+ weight = pow(weight,coeff[0]); /* shepards power factor */
+ weight = ( weight < 1.0 ) ? 1.0 : 1.0/weight;
s.x += (arguments[ i ]-arguments[i+2])*weight;
s.y += (arguments[i+1]-arguments[i+3])*weight;
}
s.x /= denominator;
s.y /= denominator;
- s.x += d.x;
+ s.x += d.x; /* make it as relative displacement */
s.y += d.y;
-
- /* We can not determine derivatives using shepards method
- only color interpolatation, not area-resampling */
break;
}
default:
*/
sparse_method = (SparseColorMethod) distort_method;
if ( distort_method == ShepardsDistortion )
- sparse_method = method; /* return non-distiort methods to normal */
+ sparse_method = method; /* return non-distort methods to normal */
+ if ( sparse_method == InverseColorInterpolate )
+ coeff[0]=0.5; /* sqrt() the squared distance for inverse */
}
/* Verbose output */
sparse_view=AcquireAuthenticCacheView(sparse_image,exception);
#if defined(MAGICKCORE_OPENMP_SUPPORT)
#pragma omp parallel for schedule(static,4) shared(progress,status) \
- dynamic_number_threads(image,image->columns,image->rows,1)
+ magick_threads(image,sparse_image,sparse_image->rows,1)
#endif
for (j=0; j < (ssize_t) sparse_image->rows; j++)
{
double weight =
((double)i-arguments[ k ])*((double)i-arguments[ k ])
+ ((double)j-arguments[k+1])*((double)j-arguments[k+1]);
- if ( method == InverseColorInterpolate )
- weight = sqrt(weight); /* inverse, not inverse squared */
+ weight = pow(weight,coeff[0]); /* inverse of power factor */
weight = ( weight < 1.0 ) ? 1.0 : 1.0/weight;
if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
pixel.red += arguments[x++]*weight;
}
case VoronoiColorInterpolate:
default:
- { /* Just use the closest control point you can find! */
+ {
size_t
k;
+
double
- minimum = MagickHuge;
+ minimum = MagickMaximumValue;
- for(k=0; k<number_arguments; k+=2+number_colors) {
+ /*
+ Just use the closest control point you can find!
+ */
+ for (k=0; k<number_arguments; k+=2+number_colors) {
double distance =
((double)i-arguments[ k ])*((double)i-arguments[ k ])
+ ((double)j-arguments[k+1])*((double)j-arguments[k+1]);