% July 1992 %
% %
% %
-% Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
+% Copyright 1999-2012 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 %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
-% The RotateImage, XShearImage, and YShearImage methods are based on the
-% paper "A Fast Algorithm for General Raster Rotatation" by Alan W. Paeth,
-% Graphics Interface '86 (Vancouver). RotateImage is adapted from a similar
+% The XShearImage() and YShearImage() methods are based on the paper "A Fast
+% Algorithm for General Raster Rotatation" by Alan W. Paeth, Graphics
+% Interface '86 (Vancouver). ShearRotateImage() is adapted from a similar
% method based on the Paeth paper written by Michael Halle of the Spatial
% Imaging Group, MIT Media Lab.
%
-%
*/
\f
/*
% %
% %
% %
-% A f f i n e T r a n s f o r m I m a g e %
-% %
-% %
-% %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%
-% AffineTransformImage() transforms an image as dictated by the affine matrix.
-% It allocates the memory necessary for the new Image structure and returns
-% a pointer to the new image.
-%
-% The format of the AffineTransformImage method is:
-%
-% Image *AffineTransformImage(const Image *image,
-% AffineMatrix *affine_matrix,ExceptionInfo *exception)
-%
-% A description of each parameter follows:
-%
-% o image: the image.
-%
-% o affine_matrix: the affine matrix.
-%
-% o exception: return any errors or warnings in this structure.
-%
-*/
-MagickExport Image *AffineTransformImage(const Image *image,
- const AffineMatrix *affine_matrix,ExceptionInfo *exception)
-{
- double
- distort[6];
-
- Image
- *deskew_image;
-
- /*
- Affine transform image.
- */
- assert(image->signature == MagickSignature);
- if (image->debug != MagickFalse)
- (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
- assert(affine_matrix != (AffineMatrix *) NULL);
- assert(exception != (ExceptionInfo *) NULL);
- assert(exception->signature == MagickSignature);
- distort[0]=affine_matrix->sx;
- distort[1]=affine_matrix->rx;
- distort[2]=affine_matrix->ry;
- distort[3]=affine_matrix->sy;
- distort[4]=affine_matrix->tx;
- distort[5]=affine_matrix->ty;
- deskew_image=DistortImage(image,AffineProjectionDistortion,6,distort,
- MagickTrue,exception);
- return(deskew_image);
-}
-\f
-/*
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-% %
-% %
-% %
+ C r o p T o F i t I m a g e %
% %
% %
if (status == MagickFalse)
{
(void) ThrowMagickException(exception,GetMagickModule(),CacheError,
- "CacheResourcesExhausted","`%s'",image->filename);
+ "CacheResourcesExhausted","'%s'",image->filename);
return(DestroyRadonInfo(radon_info));
}
radon_info->type=DiskCache;
q=swap;
}
#if defined(MAGICKCORE_OPENMP_SUPPORT)
- #pragma omp parallel for schedule(dynamic,4)
+ #pragma omp parallel for schedule(static,4)
#endif
for (x=0; x < (ssize_t) p->width; x++)
{
bits[i]=(unsigned short) count;
}
status=MagickTrue;
- image_view=AcquireCacheView(image);
+ image_view=AcquireVirtualCacheView(image,exception);
#if defined(MAGICKCORE_OPENMP_SUPPORT)
- #pragma omp parallel for schedule(dynamic,4) shared(status)
+ #pragma omp parallel for schedule(static,4) shared(status)
#endif
for (y=0; y < (ssize_t) image->rows; y++)
{
RadonProjection(source_cells,destination_cells,-1,projection);
(void) ResetRadonCells(source_cells);
#if defined(MAGICKCORE_OPENMP_SUPPORT)
- #pragma omp parallel for schedule(dynamic,4) shared(status)
+ #pragma omp parallel for schedule(static,4) shared(status)
#endif
for (y=0; y < (ssize_t) image->rows; y++)
{
return;
GetPixelInfo(image,&background);
count=0.0;
- image_view=AcquireCacheView(image);
+ image_view=AcquireVirtualCacheView(image,exception);
for (y=0; y < (ssize_t) image->rows; y++)
{
register const Quantum
clone_image=CloneImage(image,0,0,MagickTrue,exception);
if (clone_image == (Image *) NULL)
return((Image *) NULL);
- (void) SetImageVirtualPixelMethod(clone_image,BackgroundVirtualPixelMethod);
+ (void) SetImageVirtualPixelMethod(clone_image,BackgroundVirtualPixelMethod,
+ exception);
degrees=RadiansToDegrees(-atan((double) skew/width/8));
if (image->debug != MagickFalse)
(void) LogMagickEvent(TransformEvent,GetMagickModule(),
% %
% %
% %
-+ I n t e g r a l R o t a t e I m a g e %
+% I n t e g r a l R o t a t e I m a g e %
% %
% %
% %
% o rotations: Specifies the number of 90 degree rotations.
%
*/
-static Image *IntegralRotateImage(const Image *image,size_t rotations,
+MagickExport Image *IntegralRotateImage(const Image *image,size_t rotations,
ExceptionInfo *exception)
{
#define RotateImageTag "Rotate/Image"
*/
status=MagickTrue;
progress=0;
- image_view=AcquireCacheView(image);
- rotate_view=AcquireCacheView(rotate_image);
+ image_view=AcquireVirtualCacheView(image,exception);
+ rotate_view=AcquireAuthenticCacheView(rotate_image,exception);
switch (rotations)
{
case 0:
Rotate 90 degrees.
*/
GetPixelCacheTileSize(image,&tile_width,&tile_height);
- tile_width=image->columns;
#if defined(MAGICKCORE_OPENMP_SUPPORT)
- #pragma omp parallel for schedule(dynamic,4) shared(progress, status) omp_throttle(1)
+ #pragma omp parallel for schedule(static,4) shared(progress,status)
#endif
for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
{
register ssize_t
i;
+ if (GetPixelMask(image,tile_pixels) != 0)
+ {
+ tile_pixels-=width*GetPixelChannels(image);
+ q+=GetPixelChannels(rotate_image);
+ continue;
+ }
for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
{
PixelChannel
rotate_traits,
traits;
- traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
- channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
+ channel=GetPixelChannelMapChannel(image,i);
+ traits=GetPixelChannelMapTraits(image,channel);
rotate_traits=GetPixelChannelMapTraits(rotate_image,channel);
if ((traits == UndefinedPixelTrait) ||
(rotate_traits == UndefinedPixelTrait))
Rotate 180 degrees.
*/
#if defined(MAGICKCORE_OPENMP_SUPPORT)
- #pragma omp parallel for schedule(dynamic,4) shared(progress,status) omp_throttle(1)
+ #pragma omp parallel for schedule(static) shared(progress,status)
#endif
for (y=0; y < (ssize_t) image->rows; y++)
{
i;
q-=GetPixelChannels(rotate_image);
+ if (GetPixelMask(image,p) != 0)
+ {
+ p+=GetPixelChannels(image);
+ continue;
+ }
for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
{
PixelChannel
rotate_traits,
traits;
- traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
- channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
+ channel=GetPixelChannelMapChannel(image,i);
+ traits=GetPixelChannelMapTraits(image,channel);
rotate_traits=GetPixelChannelMapTraits(rotate_image,channel);
if ((traits == UndefinedPixelTrait) ||
(rotate_traits == UndefinedPixelTrait))
Rotate 270 degrees.
*/
GetPixelCacheTileSize(image,&tile_width,&tile_height);
- tile_width=image->columns;
#if defined(MAGICKCORE_OPENMP_SUPPORT)
- #pragma omp parallel for schedule(dynamic,4) shared(progress,status) omp_throttle(1)
+ #pragma omp parallel for schedule(static,4) shared(progress,status)
#endif
for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
{
register ssize_t
i;
+ if (GetPixelMask(image,tile_pixels) != 0)
+ {
+ tile_pixels+=width*GetPixelChannels(image);
+ q+=GetPixelChannels(rotate_image);
+ continue;
+ }
for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
{
PixelChannel
rotate_traits,
traits;
- traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
- channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
+ channel=GetPixelChannelMapChannel(image,i);
+ traits=GetPixelChannelMapTraits(image,channel);
rotate_traits=GetPixelChannelMapTraits(rotate_image,channel);
if ((traits == UndefinedPixelTrait) ||
(rotate_traits == UndefinedPixelTrait))
status=MagickTrue;
background=image->background_color;
progress=0;
- image_view=AcquireCacheView(image);
+ image_view=AcquireAuthenticCacheView(image,exception);
#if defined(MAGICKCORE_OPENMP_SUPPORT)
- #pragma omp parallel for schedule(dynamic,4) shared(progress, status)
+ #pragma omp parallel for schedule(static,4) shared(progress,status)
#endif
for (y=0; y < (ssize_t) height; y++)
{
if ((x_offset+i) < step)
{
p+=GetPixelChannels(image);
- SetPixelInfo(image,p,&pixel);
+ GetPixelInfoPixel(image,p,&pixel);
q+=GetPixelChannels(image);
continue;
}
- SetPixelInfo(image,p,&source);
+ GetPixelInfoPixel(image,p,&source);
CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
&source,(MagickRealType) GetPixelAlpha(image,p),area,&destination);
- SetPixelPixelInfo(image,&destination,q);
- SetPixelInfo(image,p,&pixel);
+ SetPixelInfoPixel(image,&destination,q);
+ GetPixelInfoPixel(image,p,&pixel);
p+=GetPixelChannels(image);
q+=GetPixelChannels(image);
}
CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
&background,(MagickRealType) background.alpha,area,&destination);
- SetPixelPixelInfo(image,&destination,q);
+ SetPixelInfoPixel(image,&destination,q);
q+=GetPixelChannels(image);
for (i=0; i < (step-1); i++)
{
- SetPixelPixelInfo(image,&background,q);
+ SetPixelInfoPixel(image,&background,q);
q+=GetPixelChannels(image);
}
break;
q-=GetPixelChannels(image);
if ((size_t) (x_offset+width+step-i) >= image->columns)
continue;
- SetPixelInfo(image,p,&source);
+ GetPixelInfoPixel(image,p,&source);
CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
&source,(MagickRealType) GetPixelAlpha(image,p),area,&destination);
- SetPixelPixelInfo(image,&destination,q);
- SetPixelInfo(image,p,&pixel);
+ SetPixelInfoPixel(image,&destination,q);
+ GetPixelInfoPixel(image,p,&pixel);
}
CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
&background,(MagickRealType) background.alpha,area,&destination);
q-=GetPixelChannels(image);
- SetPixelPixelInfo(image,&destination,q);
+ SetPixelInfoPixel(image,&destination,q);
for (i=0; i < (step-1); i++)
{
q-=GetPixelChannels(image);
- SetPixelPixelInfo(image,&background,q);
+ SetPixelInfoPixel(image,&background,q);
}
break;
}
status=MagickTrue;
progress=0;
background=image->background_color;
- image_view=AcquireCacheView(image);
+ image_view=AcquireAuthenticCacheView(image,exception);
#if defined(MAGICKCORE_OPENMP_SUPPORT)
- #pragma omp parallel for schedule(dynamic,4) shared(progress, status)
+ #pragma omp parallel for schedule(static,4) shared(progress,status)
#endif
for (x=0; x < (ssize_t) width; x++)
{
if ((y_offset+i) < step)
{
p+=GetPixelChannels(image);
- SetPixelInfo(image,p,&pixel);
+ GetPixelInfoPixel(image,p,&pixel);
q+=GetPixelChannels(image);
continue;
}
- SetPixelInfo(image,p,&source);
+ GetPixelInfoPixel(image,p,&source);
CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
&source,(MagickRealType) GetPixelAlpha(image,p),area,
&destination);
- SetPixelPixelInfo(image,&destination,q);
- SetPixelInfo(image,p,&pixel);
+ SetPixelInfoPixel(image,&destination,q);
+ GetPixelInfoPixel(image,p,&pixel);
p+=GetPixelChannels(image);
q+=GetPixelChannels(image);
}
CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
&background,(MagickRealType) background.alpha,area,&destination);
- SetPixelPixelInfo(image,&destination,q);
+ SetPixelInfoPixel(image,&destination,q);
q+=GetPixelChannels(image);
for (i=0; i < (step-1); i++)
{
- SetPixelPixelInfo(image,&background,q);
+ SetPixelInfoPixel(image,&background,q);
q+=GetPixelChannels(image);
}
break;
q-=GetPixelChannels(image);
if ((size_t) (y_offset+height+step-i) >= image->rows)
continue;
- SetPixelInfo(image,p,&source);
+ GetPixelInfoPixel(image,p,&source);
CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
&source,(MagickRealType) GetPixelAlpha(image,p),area,
&destination);
- SetPixelPixelInfo(image,&destination,q);
- SetPixelInfo(image,p,&pixel);
+ SetPixelInfoPixel(image,&destination,q);
+ GetPixelInfoPixel(image,p,&pixel);
}
CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
&background,(MagickRealType) background.alpha,area,&destination);
q-=GetPixelChannels(image);
- SetPixelPixelInfo(image,&destination,q);
+ SetPixelInfoPixel(image,&destination,q);
for (i=0; i < (step-1); i++)
{
q-=GetPixelChannels(image);
- SetPixelPixelInfo(image,&background,q);
+ SetPixelInfoPixel(image,&background,q);
}
break;
}
% %
% %
% %
-% R o t a t e I m a g e %
-% %
-% %
-% %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%
-% RotateImage() creates a new image that is a rotated copy of an existing
-% one. Positive angles rotate counter-clockwise (right-hand rule), while
-% negative angles rotate clockwise. Rotated images are usually larger than
-% the originals and have 'empty' triangular corners. X axis. Empty
-% triangles left over from shearing the image are filled with the background
-% color defined by member 'background_color' of the image. RotateImage
-% allocates the memory necessary for the new Image structure and returns a
-% pointer to the new image.
-%
-% The format of the RotateImage method is:
-%
-% Image *RotateImage(const Image *image,const double degrees,
-% ExceptionInfo *exception)
-%
-% A description of each parameter follows.
-%
-% o image: the image.
-%
-% o degrees: Specifies the number of degrees to rotate the image.
-%
-% o exception: return any errors or warnings in this structure.
-%
-*/
-MagickExport Image *RotateImage(const Image *image,const double degrees,
- ExceptionInfo *exception)
-{
- Image
- *rotate_image;
-
- MagickRealType
- angle;
-
- PointInfo
- shear;
-
- size_t
- rotations;
-
- /*
- Adjust rotation angle.
- */
- assert(image != (Image *) NULL);
- assert(image->signature == MagickSignature);
- if (image->debug != MagickFalse)
- (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
- assert(exception != (ExceptionInfo *) NULL);
- assert(exception->signature == MagickSignature);
- angle=degrees;
- while (angle < -45.0)
- angle+=360.0;
- for (rotations=0; angle > 45.0; rotations++)
- angle-=90.0;
- rotations%=4;
- shear.x=(-tan((double) DegreesToRadians(angle)/2.0));
- shear.y=sin((double) DegreesToRadians(angle));
- if ((fabs(shear.x) < MagickEpsilon) && (fabs(shear.y) < MagickEpsilon))
- return(IntegralRotateImage(image,rotations,exception));
- rotate_image=DistortImage(image,ScaleRotateTranslateDistortion,1,°rees,
- MagickTrue,exception);
- return(rotate_image);
-}
-\f
-/*
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-% %
-% %
-% %
% S h e a r I m a g e %
% %
% %
shear_image->page.height=0;
return(shear_image);
}
+\f
+/*
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% %
+% %
+% %
+% S h e a r R o t a t e I m a g e %
+% %
+% %
+% %
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%
+% ShearRotateImage() creates a new image that is a rotated copy of an existing
+% one. Positive angles rotate counter-clockwise (right-hand rule), while
+% negative angles rotate clockwise. Rotated images are usually larger than
+% the originals and have 'empty' triangular corners. X axis. Empty
+% triangles left over from shearing the image are filled with the background
+% color defined by member 'background_color' of the image. ShearRotateImage
+% allocates the memory necessary for the new Image structure and returns a
+% pointer to the new image.
+%
+% ShearRotateImage() is based on the paper "A Fast Algorithm for General
+% Raster Rotatation" by Alan W. Paeth. ShearRotateImage is adapted from a
+% similar method based on the Paeth paper written by Michael Halle of the
+% Spatial Imaging Group, MIT Media Lab.
+%
+% The format of the ShearRotateImage method is:
+%
+% Image *ShearRotateImage(const Image *image,const double degrees,
+% ExceptionInfo *exception)
+%
+% A description of each parameter follows.
+%
+% o image: the image.
+%
+% o degrees: Specifies the number of degrees to rotate the image.
+%
+% o exception: return any errors or warnings in this structure.
+%
+*/
+MagickExport Image *ShearRotateImage(const Image *image,const double degrees,
+ ExceptionInfo *exception)
+{
+ Image
+ *integral_image,
+ *rotate_image;
+
+ MagickBooleanType
+ status;
+
+ MagickRealType
+ angle;
+
+ PointInfo
+ shear;
+
+ RectangleInfo
+ border_info;
+
+ size_t
+ height,
+ rotations,
+ width,
+ y_width;
+
+ ssize_t
+ x_offset,
+ y_offset;
+
+ /*
+ Adjust rotation angle.
+ */
+ assert(image != (Image *) NULL);
+ assert(image->signature == MagickSignature);
+ if (image->debug != MagickFalse)
+ (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
+ assert(exception != (ExceptionInfo *) NULL);
+ assert(exception->signature == MagickSignature);
+ angle=degrees;
+ while (angle < -45.0)
+ angle+=360.0;
+ for (rotations=0; angle > 45.0; rotations++)
+ angle-=90.0;
+ rotations%=4;
+ /*
+ Calculate shear equations.
+ */
+ integral_image=IntegralRotateImage(image,rotations,exception);
+ if (integral_image == (Image *) NULL)
+ ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
+ shear.x=(-tan((double) DegreesToRadians(angle)/2.0));
+ shear.y=sin((double) DegreesToRadians(angle));
+ if ((shear.x == 0.0) && (shear.y == 0.0))
+ return(integral_image);
+ if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
+ {
+ integral_image=DestroyImage(integral_image);
+ return(integral_image);
+ }
+ if (integral_image->matte == MagickFalse)
+ (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
+ /*
+ Compute image size.
+ */
+ width=image->columns;
+ height=image->rows;
+ if ((rotations == 1) || (rotations == 3))
+ {
+ width=image->rows;
+ height=image->columns;
+ }
+ y_width=width+(ssize_t) floor(fabs(shear.x)*height+0.5);
+ x_offset=(ssize_t) ceil((double) width+((fabs(shear.y)*height)-width)/2.0-
+ 0.5);
+ y_offset=(ssize_t) ceil((double) height+((fabs(shear.y)*y_width)-height)/2.0-
+ 0.5);
+ /*
+ Surround image with a border.
+ */
+ integral_image->border_color=integral_image->background_color;
+ integral_image->compose=CopyCompositeOp;
+ border_info.width=(size_t) x_offset;
+ border_info.height=(size_t) y_offset;
+ rotate_image=BorderImage(integral_image,&border_info,image->compose,
+ exception);
+ integral_image=DestroyImage(integral_image);
+ if (rotate_image == (Image *) NULL)
+ ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
+ /*
+ Rotate the image.
+ */
+ status=XShearImage(rotate_image,shear.x,width,height,x_offset,(ssize_t)
+ (rotate_image->rows-height)/2,exception);
+ if (status == MagickFalse)
+ {
+ rotate_image=DestroyImage(rotate_image);
+ return((Image *) NULL);
+ }
+ status=YShearImage(rotate_image,shear.y,y_width,height,(ssize_t)
+ (rotate_image->columns-y_width)/2,y_offset,exception);
+ if (status == MagickFalse)
+ {
+ rotate_image=DestroyImage(rotate_image);
+ return((Image *) NULL);
+ }
+ status=XShearImage(rotate_image,shear.x,y_width,rotate_image->rows,(ssize_t)
+ (rotate_image->columns-y_width)/2,0,exception);
+ if (status == MagickFalse)
+ {
+ rotate_image=DestroyImage(rotate_image);
+ return((Image *) NULL);
+ }
+ status=CropToFitImage(&rotate_image,shear.x,shear.y,(MagickRealType) width,
+ (MagickRealType) height,MagickTrue,exception);
+ if (status == MagickFalse)
+ {
+ rotate_image=DestroyImage(rotate_image);
+ return((Image *) NULL);
+ }
+ rotate_image->compose=image->compose;
+ rotate_image->page.width=0;
+ rotate_image->page.height=0;
+ return(rotate_image);
+}