2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % SSSSS H H EEEEE AAA RRRR %
8 % SSS HHHHH EEE AAAAA RRRR %
10 % SSSSS H H EEEEE A A R R %
13 % MagickCore Methods to Shear or Rotate an Image by an Arbitrary Angle %
20 % Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 % The RotateImage, XShearImage, and YShearImage methods are based on the
37 % paper "A Fast Algorithm for General Raster Rotatation" by Alan W. Paeth,
38 % Graphics Interface '86 (Vancouver). RotateImage is adapted from a similar
39 % method based on the Paeth paper written by Michael Halle of the Spatial
40 % Imaging Group, MIT Media Lab.
48 #include "magick/studio.h"
49 #include "magick/artifact.h"
50 #include "magick/attribute.h"
51 #include "magick/blob-private.h"
52 #include "magick/cache-private.h"
53 #include "magick/color-private.h"
54 #include "magick/colorspace-private.h"
55 #include "magick/composite.h"
56 #include "magick/composite-private.h"
57 #include "magick/decorate.h"
58 #include "magick/distort.h"
59 #include "magick/draw.h"
60 #include "magick/exception.h"
61 #include "magick/exception-private.h"
62 #include "magick/gem.h"
63 #include "magick/geometry.h"
64 #include "magick/image.h"
65 #include "magick/image-private.h"
66 #include "magick/memory_.h"
67 #include "magick/list.h"
68 #include "magick/monitor.h"
69 #include "magick/monitor-private.h"
70 #include "magick/pixel-private.h"
71 #include "magick/quantum.h"
72 #include "magick/resource_.h"
73 #include "magick/shear.h"
74 #include "magick/statistic.h"
75 #include "magick/threshold.h"
76 #include "magick/transform.h"
79 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
83 % A f f i n e T r a n s f o r m I m a g e %
87 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
89 % AffineTransformImage() transforms an image as dictated by the affine matrix.
90 % It allocates the memory necessary for the new Image structure and returns
91 % a pointer to the new image.
93 % The format of the AffineTransformImage method is:
95 % Image *AffineTransformImage(const Image *image,
96 % AffineMatrix *affine_matrix,ExceptionInfo *exception)
98 % A description of each parameter follows:
100 % o image: the image.
102 % o affine_matrix: the affine matrix.
104 % o exception: return any errors or warnings in this structure.
107 MagickExport Image *AffineTransformImage(const Image *image,
108 const AffineMatrix *affine_matrix,ExceptionInfo *exception)
117 Affine transform image.
119 assert(image->signature == MagickSignature);
120 if (image->debug != MagickFalse)
121 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
122 assert(affine_matrix != (AffineMatrix *) NULL);
123 assert(exception != (ExceptionInfo *) NULL);
124 assert(exception->signature == MagickSignature);
125 distort[0]=affine_matrix->sx;
126 distort[1]=affine_matrix->rx;
127 distort[2]=affine_matrix->ry;
128 distort[3]=affine_matrix->sy;
129 distort[4]=affine_matrix->tx;
130 distort[5]=affine_matrix->ty;
131 deskew_image=DistortImage(image,AffineProjectionDistortion,6,distort,
132 MagickTrue,exception);
133 return(deskew_image);
137 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
141 + C r o p T o F i t I m a g e %
145 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
147 % CropToFitImage() crops the sheared image as determined by the bounding box
148 % as defined by width and height and shearing angles.
150 % The format of the CropToFitImage method is:
152 % Image *CropToFitImage(Image **image,const MagickRealType x_shear,
153 % const MagickRealType x_shear,const MagickRealType width,
154 % const MagickRealType height,const MagickBooleanType rotate,
155 % ExceptionInfo *exception)
157 % A description of each parameter follows.
159 % o image: the image.
161 % o x_shear, y_shear, width, height: Defines a region of the image to crop.
163 % o exception: return any errors or warnings in this structure.
166 static inline void CropToFitImage(Image **image,const MagickRealType x_shear,
167 const MagickRealType y_shear,const MagickRealType width,
168 const MagickRealType height,const MagickBooleanType rotate,
169 ExceptionInfo *exception)
187 Calculate the rotated image size.
189 extent[0].x=(double) (-width/2.0);
190 extent[0].y=(double) (-height/2.0);
191 extent[1].x=(double) width/2.0;
192 extent[1].y=(double) (-height/2.0);
193 extent[2].x=(double) (-width/2.0);
194 extent[2].y=(double) height/2.0;
195 extent[3].x=(double) width/2.0;
196 extent[3].y=(double) height/2.0;
197 for (i=0; i < 4; i++)
199 extent[i].x+=x_shear*extent[i].y;
200 extent[i].y+=y_shear*extent[i].x;
201 if (rotate != MagickFalse)
202 extent[i].x+=x_shear*extent[i].y;
203 extent[i].x+=(double) (*image)->columns/2.0;
204 extent[i].y+=(double) (*image)->rows/2.0;
208 for (i=1; i < 4; i++)
210 if (min.x > extent[i].x)
212 if (min.y > extent[i].y)
214 if (max.x < extent[i].x)
216 if (max.y < extent[i].y)
219 geometry.x=(long) (min.x+0.5);
220 geometry.y=(long) (min.y+0.5);
221 geometry.width=(unsigned long) ((long) (max.x+0.5)-(long) (min.x+0.5));
222 geometry.height=(unsigned long) ((long) (max.y+0.5)-(long) (min.y+0.5));
224 (void) ParseAbsoluteGeometry("0x0+0+0",&(*image)->page);
225 crop_image=CropImage(*image,&geometry,exception);
227 if (crop_image != (Image *) NULL)
229 crop_image->page=page;
230 *image=DestroyImage(*image);
236 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
240 % D e s k e w I m a g e %
244 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
246 % DeskewImage() removes skew from the image. Skew is an artifact that
247 % occurs in scanned images because of the camera being misaligned,
248 % imperfections in the scanning or surface, or simply because the paper was
249 % not placed completely flat when scanned.
251 % The format of the DeskewImage method is:
253 % Image *DeskewImage(const Image *image,const double threshold,
254 % ExceptionInfo *exception)
256 % A description of each parameter follows:
258 % o image: the image.
260 % o threshold: separate background from foreground.
262 % o exception: return any errors or warnings in this structure.
266 typedef struct _RadonInfo
291 static RadonInfo *DestroyRadonInfo(RadonInfo *radon_info)
293 assert(radon_info != (RadonInfo *) NULL);
294 switch (radon_info->type)
298 if (radon_info->mapped == MagickFalse)
299 radon_info->cells=(unsigned short *) RelinquishMagickMemory(
302 radon_info->cells=(unsigned short *) UnmapBlob(radon_info->cells,
303 (size_t) radon_info->length);
304 RelinquishMagickResource(MemoryResource,radon_info->length);
309 radon_info->cells=(unsigned short *) UnmapBlob(radon_info->cells,(size_t)
311 RelinquishMagickResource(MapResource,radon_info->length);
315 if (radon_info->file != -1)
316 (void) close(radon_info->file);
317 (void) RelinquishUniqueFileResource(radon_info->path);
318 RelinquishMagickResource(DiskResource,radon_info->length);
324 return((RadonInfo *) RelinquishMagickMemory(radon_info));
327 static MagickBooleanType ResetRadonCells(RadonInfo *radon_info)
341 if (radon_info->type != DiskCache)
343 (void) ResetMagickMemory(radon_info->cells,0,(size_t) radon_info->length);
347 (void) MagickSeek(radon_info->file,0,SEEK_SET);
348 for (y=0; y < (long) radon_info->height; y++)
350 for (x=0; x < (long) radon_info->width; x++)
352 count=write(radon_info->file,&value,sizeof(*radon_info->cells));
353 if (count != (ssize_t) sizeof(*radon_info->cells))
356 if (x < (long) radon_info->width)
359 return(y < (long) radon_info->height ? MagickFalse : MagickTrue);
362 static RadonInfo *AcquireRadonInfo(const Image *image,const unsigned long width,
363 const unsigned long height,ExceptionInfo *exception)
371 radon_info=(RadonInfo *) AcquireMagickMemory(sizeof(*radon_info));
372 if (radon_info == (RadonInfo *) NULL)
373 return((RadonInfo *) NULL);
374 (void) ResetMagickMemory(radon_info,0,sizeof(*radon_info));
375 radon_info->width=width;
376 radon_info->height=height;
377 radon_info->length=(MagickSizeType) width*height*sizeof(*radon_info->cells);
378 radon_info->type=MemoryCache;
379 status=AcquireMagickResource(AreaResource,radon_info->length);
380 if ((status != MagickFalse) &&
381 (radon_info->length == (MagickSizeType) ((size_t) radon_info->length)))
383 status=AcquireMagickResource(MemoryResource,radon_info->length);
384 if (status != MagickFalse)
386 radon_info->mapped=MagickFalse;
387 radon_info->cells=(unsigned short *) AcquireMagickMemory((size_t)
389 if (radon_info->cells == (unsigned short *) NULL)
391 radon_info->mapped=MagickTrue;
392 radon_info->cells=(unsigned short *) MapBlob(-1,IOMode,0,(size_t)
395 if (radon_info->cells == (unsigned short *) NULL)
396 RelinquishMagickResource(MemoryResource,radon_info->length);
399 radon_info->file=(-1);
400 if (radon_info->cells == (unsigned short *) NULL)
402 status=AcquireMagickResource(DiskResource,radon_info->length);
403 if (status == MagickFalse)
405 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
406 "CacheResourcesExhausted","`%s'",image->filename);
407 return(DestroyRadonInfo(radon_info));
409 radon_info->type=DiskCache;
410 (void) AcquireMagickResource(MemoryResource,radon_info->length);
411 radon_info->file=AcquireUniqueFileResource(radon_info->path);
412 if (radon_info->file == -1)
413 return(DestroyRadonInfo(radon_info));
414 status=AcquireMagickResource(MapResource,radon_info->length);
415 if (status != MagickFalse)
417 status=ResetRadonCells(radon_info);
418 if (status != MagickFalse)
420 radon_info->cells=(unsigned short *) MapBlob(radon_info->file,
421 IOMode,0,(size_t) radon_info->length);
422 if (radon_info->cells != (unsigned short *) NULL)
423 radon_info->type=MapCache;
425 RelinquishMagickResource(MapResource,radon_info->length);
432 static inline size_t MagickMin(const size_t x,const size_t y)
439 static inline ssize_t ReadRadonCell(const RadonInfo *radon_info,
440 const off_t offset,const size_t length,unsigned char *buffer)
448 #if !defined(MAGICKCORE_HAVE_PPREAD)
449 #if defined(MAGICKCORE_OPENMP_SUPPORT)
450 #pragma omp critical (MagickCore_ReadRadonCell)
454 if (MagickSeek(radon_info->file,offset,SEEK_SET) >= 0)
458 for (i=0; i < (ssize_t) length; i+=count)
460 #if !defined(MAGICKCORE_HAVE_PPREAD)
461 count=read(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
464 count=pread(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
465 SSIZE_MAX),(off_t) (offset+i));
476 #if !defined(MAGICKCORE_HAVE_PPREAD)
483 static inline ssize_t WriteRadonCell(const RadonInfo *radon_info,
484 const off_t offset,const size_t length,const unsigned char *buffer)
492 #if !defined(MAGICKCORE_HAVE_PWRITE)
493 #if defined(MAGICKCORE_OPENMP_SUPPORT)
494 #pragma omp critical (MagickCore_WriteRadonCell)
497 if (MagickSeek(radon_info->file,offset,SEEK_SET) >= 0)
501 for (i=0; i < (ssize_t) length; i+=count)
503 #if !defined(MAGICKCORE_HAVE_PWRITE)
504 count=write(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
507 count=pwrite(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
508 SSIZE_MAX),(off_t) (offset+i));
519 #if !defined(MAGICKCORE_HAVE_PWRITE)
526 static inline unsigned short GetRadonCell(const RadonInfo *radon_info,
527 const long x,const long y)
535 i=(off_t) radon_info->height*x+y;
537 ((MagickSizeType) (i*sizeof(*radon_info->cells)) >= radon_info->length))
539 if (radon_info->type != DiskCache)
540 return(radon_info->cells[i]);
542 (void) ReadRadonCell(radon_info,i*sizeof(*radon_info->cells),
543 sizeof(*radon_info->cells),(unsigned char *) &value);
547 static inline MagickBooleanType SetRadonCell(const RadonInfo *radon_info,
548 const long x,const long y,const unsigned short value)
556 i=(off_t) radon_info->height*x+y;
558 ((MagickSizeType) (i*sizeof(*radon_info->cells)) >= radon_info->length))
560 if (radon_info->type != DiskCache)
562 radon_info->cells[i]=value;
565 count=WriteRadonCell(radon_info,i*sizeof(*radon_info->cells),
566 sizeof(*radon_info->cells),(const unsigned char *) &value);
567 if (count != (ssize_t) sizeof(*radon_info->cells))
572 static void RadonProjection(RadonInfo *source_cells,
573 RadonInfo *destination_cells,const long sign,unsigned long *projection)
590 for (step=1; step < p->width; step*=2)
592 for (x=0; x < (long) p->width; x+=2*step)
603 for (i=0; i < (long) step; i++)
605 for (y=0; y < (long) (p->height-i-1); y++)
607 cell=GetRadonCell(p,x+i,y);
608 (void) SetRadonCell(q,x+2*i,y,cell+GetRadonCell(p,x+i+step,y+i));
609 (void) SetRadonCell(q,x+2*i+1,y,cell+GetRadonCell(p,x+i+step,y+i+1));
611 for ( ; y < (long) (p->height-i); y++)
613 cell=GetRadonCell(p,x+i,y);
614 (void) SetRadonCell(q,x+2*i,y,cell+GetRadonCell(p,x+i+step,y+i));
615 (void) SetRadonCell(q,x+2*i+1,y,cell);
617 for ( ; y < (long) p->height; y++)
619 cell=GetRadonCell(p,x+i,y);
620 (void) SetRadonCell(q,x+2*i,y,cell);
621 (void) SetRadonCell(q,x+2*i+1,y,cell);
629 #if defined(MAGICKCORE_OPENMP_SUPPORT)
630 #pragma omp parallel for schedule(dynamic,4)
632 for (x=0; x < (long) p->width; x++)
641 for (y=0; y < (long) (p->height-1); y++)
646 delta=GetRadonCell(p,x,y)-(long) GetRadonCell(p,x,y+1);
649 projection[p->width+sign*x-1]=sum;
653 static MagickBooleanType RadonTransform(const Image *image,
654 const double threshold,unsigned long *projection,ExceptionInfo *exception)
682 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
683 source_cells=AcquireRadonInfo(image,width,image->rows,exception);
684 destination_cells=AcquireRadonInfo(image,width,image->rows,exception);
685 if ((source_cells == (RadonInfo *) NULL) ||
686 (destination_cells == (RadonInfo *) NULL))
688 if (destination_cells != (RadonInfo *) NULL)
689 destination_cells=DestroyRadonInfo(destination_cells);
690 if (source_cells != (RadonInfo *) NULL)
691 source_cells=DestroyRadonInfo(source_cells);
694 if (ResetRadonCells(source_cells) == MagickFalse)
696 destination_cells=DestroyRadonInfo(destination_cells);
697 source_cells=DestroyRadonInfo(source_cells);
700 for (i=0; i < 256; i++)
702 byte=(unsigned char) i;
703 for (count=0; byte != 0; byte>>=1)
705 bits[i]=(unsigned short) count;
708 image_view=AcquireCacheView(image);
709 #if defined(MAGICKCORE_OPENMP_SUPPORT)
710 #pragma omp parallel for schedule(dynamic,4) shared(status)
712 for (y=0; y < (long) image->rows; y++)
714 register const PixelPacket
725 if (status == MagickFalse)
727 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
728 if (p == (const PixelPacket *) NULL)
735 i=(long) (image->columns+7)/8;
736 for (x=0; x < (long) image->columns; x++)
739 if (((MagickRealType) p->red < threshold) ||
740 ((MagickRealType) p->green < threshold) ||
741 ((MagickRealType) p->blue < threshold))
746 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
755 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
758 RadonProjection(source_cells,destination_cells,-1,projection);
759 (void) ResetRadonCells(source_cells);
760 #if defined(MAGICKCORE_OPENMP_SUPPORT)
761 #pragma omp parallel for schedule(dynamic,4) shared(status)
763 for (y=0; y < (long) image->rows; y++)
765 register const PixelPacket
776 if (status == MagickFalse)
778 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
779 if (p == (const PixelPacket *) NULL)
787 for (x=0; x < (long) image->columns; x++)
790 if (((MagickRealType) p->red < threshold) ||
791 ((MagickRealType) p->green < threshold) ||
792 ((MagickRealType) p->blue < threshold))
797 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
806 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
809 RadonProjection(source_cells,destination_cells,1,projection);
810 image_view=DestroyCacheView(image_view);
811 destination_cells=DestroyRadonInfo(destination_cells);
812 source_cells=DestroyRadonInfo(source_cells);
816 static void GetImageBackgroundColor(Image *image,const long offset,
817 ExceptionInfo *exception)
832 Compute average background color.
836 GetMagickPixelPacket(image,&background);
838 image_view=AcquireCacheView(image);
839 for (y=0; y < (long) image->rows; y++)
841 register const PixelPacket
847 if ((y >= offset) && (y < ((long) image->rows-offset)))
849 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
850 if (p == (const PixelPacket *) NULL)
852 for (x=0; x < (long) image->columns; x++)
854 if ((x >= offset) && (x < ((long) image->columns-offset)))
856 background.red+=QuantumScale*p->red;
857 background.green+=QuantumScale*p->green;
858 background.blue+=QuantumScale*p->blue;
859 background.opacity+=QuantumScale*p->opacity;
864 image_view=DestroyCacheView(image_view);
865 image->background_color.red=RoundToQuantum((MagickRealType) QuantumRange*
866 background.red/count);
867 image->background_color.green=RoundToQuantum((MagickRealType) QuantumRange*
868 background.green/count);
869 image->background_color.blue=RoundToQuantum((MagickRealType) QuantumRange*
870 background.blue/count);
871 image->background_color.opacity=RoundToQuantum((MagickRealType) QuantumRange*
872 background.opacity/count);
875 MagickExport Image *DeskewImage(const Image *image,const double threshold,
876 ExceptionInfo *exception)
911 Compute deskew angle.
913 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
914 projection=(unsigned long *) AcquireQuantumMemory((size_t) (2*width-1),
915 sizeof(*projection));
916 if (projection == (unsigned long *) NULL)
917 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
918 status=RadonTransform(image,threshold,projection,exception);
919 if (status == MagickFalse)
921 projection=(unsigned long *) RelinquishMagickMemory(projection);
922 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
926 for (i=0; i < (long) (2*width-1); i++)
928 if (projection[i] > max_projection)
930 skew=i-(long) width+1;
931 max_projection=projection[i];
934 projection=(unsigned long *) RelinquishMagickMemory(projection);
938 clone_image=CloneImage(image,0,0,MagickTrue,exception);
939 if (clone_image == (Image *) NULL)
940 return((Image *) NULL);
941 (void) SetImageVirtualPixelMethod(clone_image,BackgroundVirtualPixelMethod);
942 degrees=RadiansToDegrees(-atan((double) skew/width/8));
943 if (image->debug != MagickFalse)
944 (void) LogMagickEvent(TransformEvent,GetMagickModule()," Deskew angle: %g",
946 affine_matrix.sx=cos(DegreesToRadians(fmod((double) degrees,360.0)));
947 affine_matrix.rx=sin(DegreesToRadians(fmod((double) degrees,360.0)));
948 affine_matrix.ry=(-sin(DegreesToRadians(fmod((double) degrees,360.0))));
949 affine_matrix.sy=cos(DegreesToRadians(fmod((double) degrees,360.0)));
950 affine_matrix.tx=0.0;
951 affine_matrix.ty=0.0;
952 artifact=GetImageArtifact(image,"deskew:auto-crop");
953 if (artifact == (const char *) NULL)
955 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
956 clone_image=DestroyImage(clone_image);
957 return(deskew_image);
962 GetImageBackgroundColor(clone_image,atol(artifact),exception);
963 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
964 clone_image=DestroyImage(clone_image);
965 if (deskew_image == (Image *) NULL)
966 return((Image *) NULL);
967 median_image=MedianFilterImage(deskew_image,0.0,exception);
968 if (median_image == (Image *) NULL)
970 deskew_image=DestroyImage(deskew_image);
971 return((Image *) NULL);
973 geometry=GetImageBoundingBox(median_image,exception);
974 median_image=DestroyImage(median_image);
975 if (image->debug != MagickFalse)
976 (void) LogMagickEvent(TransformEvent,GetMagickModule()," Deskew geometry: "
977 "%lux%lu%+ld%+ld",geometry.width,geometry.height,geometry.x,geometry.y);
978 crop_image=CropImage(deskew_image,&geometry,exception);
979 deskew_image=DestroyImage(deskew_image);
984 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
988 + I n t e g r a l R o t a t e I m a g e %
992 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
994 % IntegralRotateImage() rotates the image an integral of 90 degrees. It
995 % allocates the memory necessary for the new Image structure and returns a
996 % pointer to the rotated image.
998 % The format of the IntegralRotateImage method is:
1000 % Image *IntegralRotateImage(const Image *image,unsigned long rotations,
1001 % ExceptionInfo *exception)
1003 % A description of each parameter follows.
1005 % o image: the image.
1007 % o rotations: Specifies the number of 90 degree rotations.
1010 static Image *IntegralRotateImage(const Image *image,unsigned long rotations,
1011 ExceptionInfo *exception)
1013 #define RotateImageTag "Rotate/Image"
1033 Initialize rotated image attributes.
1035 assert(image != (Image *) NULL);
1039 return(CloneImage(image,0,0,MagickTrue,exception));
1040 if ((rotations == 1) || (rotations == 3))
1041 rotate_image=CloneImage(image,image->rows,image->columns,MagickTrue,
1044 rotate_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1046 if (rotate_image == (Image *) NULL)
1047 return((Image *) NULL);
1049 Integral rotate the image.
1053 image_view=AcquireCacheView(image);
1054 rotate_view=AcquireCacheView(rotate_image);
1076 GetPixelCacheTileSize(image,&tile_width,&tile_height);
1077 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1078 #pragma omp parallel for schedule(dynamic,4) shared(progress, status)
1080 for (tile_y=0; tile_y < (long) image->rows; tile_y+=tile_height)
1085 if (status == MagickFalse)
1087 for (tile_x=0; tile_x < (long) image->columns; tile_x+=tile_width)
1092 register const IndexPacket
1095 register const PixelPacket
1098 register IndexPacket
1099 *restrict rotate_indexes;
1104 register PixelPacket
1112 if ((tile_x+(long) tile_width) > (long) image->columns)
1113 width=(unsigned long) (tile_width-(tile_x+tile_width-
1116 if ((tile_y+(long) tile_height) > (long) image->rows)
1117 height=(unsigned long) (tile_height-(tile_y+tile_height-
1119 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1121 if (p == (const PixelPacket *) NULL)
1126 indexes=GetCacheViewVirtualIndexQueue(image_view);
1127 for (y=0; y < (long) width; y++)
1129 register const PixelPacket
1130 *restrict tile_pixels;
1135 q=QueueCacheViewAuthenticPixels(rotate_view,(long)
1136 rotate_image->columns-(tile_y+height),y+tile_x,height,
1138 if (q == (PixelPacket *) NULL)
1143 tile_pixels=p+(height-1)*width+y;
1144 for (x=0; x < (long) height; x++)
1146 *q++=(*tile_pixels);
1149 rotate_indexes=GetCacheViewAuthenticIndexQueue(rotate_view);
1150 if ((indexes != (IndexPacket *) NULL) &&
1151 (rotate_indexes != (IndexPacket *) NULL))
1153 register const IndexPacket
1154 *restrict tile_indexes;
1156 tile_indexes=indexes+(height-1)*width+y;
1157 for (x=0; x < (long) height; x++)
1159 *rotate_indexes++=(*tile_indexes);
1160 tile_indexes-=width;
1163 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1164 if (sync == MagickFalse)
1168 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1173 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
1175 if (proceed == MagickFalse)
1179 (void) SetImageProgress(image,RotateImageTag,image->rows-1,image->rows);
1180 Swap(page.width,page.height);
1181 Swap(page.x,page.y);
1182 if (page.width != 0)
1183 page.x=(long) (page.width-rotate_image->columns-page.x);
1191 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1192 #pragma omp parallel for schedule(dynamic,4) shared(progress, status)
1194 for (y=0; y < (long) image->rows; y++)
1199 register const IndexPacket
1202 register const PixelPacket
1205 register IndexPacket
1206 *restrict rotate_indexes;
1211 register PixelPacket
1214 if (status == MagickFalse)
1216 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,
1218 q=QueueCacheViewAuthenticPixels(rotate_view,0,(long) (image->rows-
1219 y-1),image->columns,1,exception);
1220 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
1225 indexes=GetCacheViewVirtualIndexQueue(image_view);
1226 rotate_indexes=GetCacheViewAuthenticIndexQueue(rotate_view);
1228 for (x=0; x < (long) image->columns; x++)
1230 if ((indexes != (IndexPacket *) NULL) &&
1231 (rotate_indexes != (IndexPacket *) NULL))
1232 for (x=0; x < (long) image->columns; x++)
1233 rotate_indexes[image->columns-x-1]=indexes[x];
1234 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1235 if (sync == MagickFalse)
1237 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1242 proceed=SetImageProgress(image,RotateImageTag,progress++,
1244 if (proceed == MagickFalse)
1248 if (page.width != 0)
1249 page.x=(long) (page.width-rotate_image->columns-page.x);
1250 if (page.height != 0)
1251 page.y=(long) (page.height-rotate_image->rows-page.y);
1266 GetPixelCacheTileSize(image,&tile_width,&tile_height);
1267 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1268 #pragma omp parallel for schedule(dynamic,4) shared(progress, status)
1270 for (tile_y=0; tile_y < (long) image->rows; tile_y+=tile_height)
1275 if (status == MagickFalse)
1277 for (tile_x=0; tile_x < (long) image->columns; tile_x+=tile_width)
1282 register const IndexPacket
1285 register const PixelPacket
1288 register IndexPacket
1289 *restrict rotate_indexes;
1294 register PixelPacket
1302 if ((tile_x+(long) tile_width) > (long) image->columns)
1303 width=(unsigned long) (tile_width-(tile_x+tile_width-
1306 if ((tile_y+(long) tile_height) > (long) image->rows)
1307 height=(unsigned long) (tile_height-(tile_y+tile_height-
1309 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,
1311 if (p == (const PixelPacket *) NULL)
1316 indexes=GetCacheViewVirtualIndexQueue(image_view);
1317 for (y=0; y < (long) width; y++)
1319 register const PixelPacket
1320 *restrict tile_pixels;
1325 q=QueueCacheViewAuthenticPixels(rotate_view,tile_y,(long)
1326 y+rotate_image->rows-(tile_x+width),height,1,exception);
1327 if (q == (PixelPacket *) NULL)
1332 tile_pixels=p+(width-1)-y;
1333 for (x=0; x < (long) height; x++)
1335 *q++=(*tile_pixels);
1338 rotate_indexes=GetCacheViewAuthenticIndexQueue(rotate_view);
1339 if ((indexes != (IndexPacket *) NULL) &&
1340 (rotate_indexes != (IndexPacket *) NULL))
1342 register const IndexPacket
1343 *restrict tile_indexes;
1345 tile_indexes=indexes+(width-1)-y;
1346 for (x=0; x < (long) height; x++)
1348 *rotate_indexes++=(*tile_indexes);
1349 tile_indexes+=width;
1352 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1353 if (sync == MagickFalse)
1357 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1362 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
1364 if (proceed == MagickFalse)
1368 (void) SetImageProgress(image,RotateImageTag,image->rows-1,image->rows);
1369 Swap(page.width,page.height);
1370 Swap(page.x,page.y);
1371 if (page.height != 0)
1372 page.y=(long) (page.height-rotate_image->rows-page.y);
1376 rotate_view=DestroyCacheView(rotate_view);
1377 image_view=DestroyCacheView(image_view);
1378 rotate_image->type=image->type;
1379 rotate_image->page=page;
1380 if (status == MagickFalse)
1381 rotate_image=DestroyImage(rotate_image);
1382 return(rotate_image);
1386 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1390 + X S h e a r I m a g e %
1394 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1396 % XShearImage() shears the image in the X direction with a shear angle of
1397 % 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1398 % negative angles shear clockwise. Angles are measured relative to a vertical
1399 % Y-axis. X shears will widen an image creating 'empty' triangles on the left
1400 % and right sides of the source image.
1402 % The format of the XShearImage method is:
1404 % MagickBooleanType XShearImage(Image *image,const MagickRealType degrees,
1405 % const unsigned long width,const unsigned long height,
1406 % const long x_offset,const long y_offset)
1408 % A description of each parameter follows.
1410 % o image: the image.
1412 % o degrees: A MagickRealType representing the shearing angle along the X
1415 % o width, height, x_offset, y_offset: Defines a region of the image
1419 static MagickBooleanType XShearImage(Image *image,const MagickRealType degrees,
1420 const unsigned long width,const unsigned long height,const long x_offset,
1421 const long y_offset)
1423 #define XShearImageTag "XShear/Image"
1447 assert(image != (Image *) NULL);
1448 assert(image->signature == MagickSignature);
1449 if (image->debug != MagickFalse)
1450 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1451 GetMagickPixelPacket(image,&background);
1452 SetMagickPixelPacket(image,&image->background_color,(IndexPacket *) NULL,
1454 if (image->colorspace == CMYKColorspace)
1455 ConvertRGBToCMYK(&background);
1461 exception=(&image->exception);
1462 image_view=AcquireCacheView(image);
1463 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1464 #pragma omp parallel for schedule(dynamic,4) shared(progress, status)
1466 for (y=0; y < (long) height; y++)
1483 register IndexPacket
1485 *restrict shear_indexes;
1487 register PixelPacket
1494 if (status == MagickFalse)
1496 p=GetCacheViewAuthenticPixels(image_view,0,y_offset+y,image->columns,1,
1498 if (p == (PixelPacket *) NULL)
1503 indexes=GetCacheViewAuthenticIndexQueue(image_view);
1506 displacement=degrees*(MagickRealType) (y-height/2.0);
1507 if (displacement == 0.0)
1509 if (displacement > 0.0)
1513 displacement*=(-1.0);
1516 step=(long) floor((double) displacement);
1517 area=(MagickRealType) (displacement-step);
1520 GetMagickPixelPacket(image,&source);
1521 GetMagickPixelPacket(image,&destination);
1527 Transfer pixels left-to-right.
1529 if (step > x_offset)
1532 shear_indexes=indexes-step;
1533 for (i=0; i < (long) width; i++)
1535 if ((x_offset+i) < step)
1537 SetMagickPixelPacket(image,++p,++indexes,&pixel);
1542 SetMagickPixelPacket(image,p,indexes,&source);
1543 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1544 &source,(MagickRealType) p->opacity,area,&destination);
1545 SetPixelPacket(image,&destination,q++,shear_indexes++);
1546 SetMagickPixelPacket(image,p++,indexes++,&pixel);
1548 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1549 &background,(MagickRealType) background.opacity,area,&destination);
1550 SetPixelPacket(image,&destination,q++,shear_indexes++);
1551 for (i=0; i < (step-1); i++)
1552 SetPixelPacket(image,&background,q++,shear_indexes++);
1558 Transfer pixels right-to-left.
1563 shear_indexes=indexes+step;
1564 for (i=0; i < (long) width; i++)
1570 if ((unsigned long) (x_offset+width+step-i) >= image->columns)
1572 SetMagickPixelPacket(image,p,indexes,&source);
1573 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1574 &source,(MagickRealType) p->opacity,area,&destination);
1575 SetPixelPacket(image,&destination,q,shear_indexes);
1576 SetMagickPixelPacket(image,p,indexes,&pixel);
1578 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1579 &background,(MagickRealType) background.opacity,area,&destination);
1580 SetPixelPacket(image,&destination,--q,--shear_indexes);
1581 for (i=0; i < (step-1); i++)
1582 SetPixelPacket(image,&background,--q,--shear_indexes);
1586 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1588 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1593 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1594 #pragma omp critical (MagickCore_XShearImage)
1596 proceed=SetImageProgress(image,XShearImageTag,progress++,height);
1597 if (proceed == MagickFalse)
1601 image_view=DestroyCacheView(image_view);
1606 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1610 + Y S h e a r I m a g e %
1614 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1616 % YShearImage shears the image in the Y direction with a shear angle of
1617 % 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1618 % negative angles shear clockwise. Angles are measured relative to a
1619 % horizontal X-axis. Y shears will increase the height of an image creating
1620 % 'empty' triangles on the top and bottom of the source image.
1622 % The format of the YShearImage method is:
1624 % MagickBooleanType YShearImage(Image *image,const MagickRealType degrees,
1625 % const unsigned long width,const unsigned long height,
1626 % const long x_offset,const long y_offset)
1628 % A description of each parameter follows.
1630 % o image: the image.
1632 % o degrees: A MagickRealType representing the shearing angle along the Y
1635 % o width, height, x_offset, y_offset: Defines a region of the image
1639 static MagickBooleanType YShearImage(Image *image,const MagickRealType degrees,
1640 const unsigned long width,const unsigned long height,const long x_offset,
1641 const long y_offset)
1643 #define YShearImageTag "YShear/Image"
1667 assert(image != (Image *) NULL);
1668 assert(image->signature == MagickSignature);
1669 if (image->debug != MagickFalse)
1670 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1671 GetMagickPixelPacket(image,&background);
1672 SetMagickPixelPacket(image,&image->background_color,(IndexPacket *) NULL,
1674 if (image->colorspace == CMYKColorspace)
1675 ConvertRGBToCMYK(&background);
1681 exception=(&image->exception);
1682 image_view=AcquireCacheView(image);
1683 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1684 #pragma omp parallel for schedule(dynamic,4) shared(progress, status)
1686 for (x=0; x < (long) width; x++)
1700 register IndexPacket
1702 *restrict shear_indexes;
1707 register PixelPacket
1714 if (status == MagickFalse)
1716 p=GetCacheViewAuthenticPixels(image_view,x_offset+x,0,1,image->rows,
1718 if (p == (PixelPacket *) NULL)
1723 indexes=GetCacheViewAuthenticIndexQueue(image_view);
1726 displacement=degrees*(MagickRealType) (x-width/2.0);
1727 if (displacement == 0.0)
1729 if (displacement > 0.0)
1733 displacement*=(-1.0);
1736 step=(long) floor((double) displacement);
1737 area=(MagickRealType) (displacement-step);
1740 GetMagickPixelPacket(image,&source);
1741 GetMagickPixelPacket(image,&destination);
1747 Transfer pixels top-to-bottom.
1749 if (step > y_offset)
1752 shear_indexes=indexes-step;
1753 for (i=0; i < (long) height; i++)
1755 if ((y_offset+i) < step)
1757 SetMagickPixelPacket(image,++p,++indexes,&pixel);
1762 SetMagickPixelPacket(image,p,indexes,&source);
1763 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1764 &source,(MagickRealType) p->opacity,area,&destination);
1765 SetPixelPacket(image,&destination,q++,shear_indexes++);
1766 SetMagickPixelPacket(image,p++,indexes++,&pixel);
1768 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1769 &background,(MagickRealType) background.opacity,area,&destination);
1770 SetPixelPacket(image,&destination,q++,shear_indexes++);
1771 for (i=0; i < (step-1); i++)
1772 SetPixelPacket(image,&background,q++,shear_indexes++);
1778 Transfer pixels bottom-to-top.
1783 shear_indexes=indexes+step;
1784 for (i=0; i < (long) height; i++)
1790 if ((unsigned long) (y_offset+height+step-i) >= image->rows)
1792 SetMagickPixelPacket(image,p,indexes,&source);
1793 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1794 &source,(MagickRealType) p->opacity,area,&destination);
1795 SetPixelPacket(image,&destination,q,shear_indexes);
1796 SetMagickPixelPacket(image,p,indexes,&pixel);
1798 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1799 &background,(MagickRealType) background.opacity,area,&destination);
1800 SetPixelPacket(image,&destination,--q,--shear_indexes);
1801 for (i=0; i < (step-1); i++)
1802 SetPixelPacket(image,&background,--q,--shear_indexes);
1806 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1808 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1813 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1814 #pragma omp critical (MagickCore_YShearImage)
1816 proceed=SetImageProgress(image,YShearImageTag,progress++,image->rows);
1817 if (proceed == MagickFalse)
1821 image_view=DestroyCacheView(image_view);
1826 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1830 % R o t a t e I m a g e %
1834 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1836 % RotateImage() creates a new image that is a rotated copy of an existing
1837 % one. Positive angles rotate counter-clockwise (right-hand rule), while
1838 % negative angles rotate clockwise. Rotated images are usually larger than
1839 % the originals and have 'empty' triangular corners. X axis. Empty
1840 % triangles left over from shearing the image are filled with the background
1841 % color defined by member 'background_color' of the image. RotateImage
1842 % allocates the memory necessary for the new Image structure and returns a
1843 % pointer to the new image.
1845 % RotateImage() is based on the paper "A Fast Algorithm for General
1846 % Raster Rotatation" by Alan W. Paeth. RotateImage is adapted from a similar
1847 % method based on the Paeth paper written by Michael Halle of the Spatial
1848 % Imaging Group, MIT Media Lab.
1850 % The format of the RotateImage method is:
1852 % Image *RotateImage(const Image *image,const double degrees,
1853 % ExceptionInfo *exception)
1855 % A description of each parameter follows.
1857 % o image: the image.
1859 % o degrees: Specifies the number of degrees to rotate the image.
1861 % o exception: return any errors or warnings in this structure.
1864 MagickExport Image *RotateImage(const Image *image,const double degrees,
1865 ExceptionInfo *exception)
1891 Adjust rotation angle.
1893 assert(image != (Image *) NULL);
1894 assert(image->signature == MagickSignature);
1895 if (image->debug != MagickFalse)
1896 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1897 assert(exception != (ExceptionInfo *) NULL);
1898 assert(exception->signature == MagickSignature);
1900 while (angle < -45.0)
1902 for (rotations=0; angle > 45.0; rotations++)
1906 Calculate shear equations.
1908 integral_image=IntegralRotateImage(image,rotations,exception);
1909 if (integral_image == (Image *) NULL)
1910 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1911 shear.x=(-tan((double) DegreesToRadians(angle)/2.0));
1912 shear.y=sin((double) DegreesToRadians(angle));
1913 if ((shear.x == 0.0) && (shear.y == 0.0))
1914 return(integral_image);
1915 if (SetImageStorageClass(integral_image,DirectClass) == MagickFalse)
1917 InheritException(exception,&integral_image->exception);
1918 integral_image=DestroyImage(integral_image);
1919 return(integral_image);
1921 if (integral_image->matte == MagickFalse)
1922 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel);
1926 width=image->columns;
1928 if ((rotations == 1) || (rotations == 3))
1931 height=image->columns;
1933 y_width=width+(long) (fabs(shear.x)*height+0.5);
1934 x_offset=(long) (width+((fabs(shear.y)*height)-width)/2.0+0.5);
1935 y_offset=(long) (height+((fabs(shear.y)*y_width)-height)/2.0+0.5);
1937 Surround image with a border.
1939 integral_image->border_color=integral_image->background_color;
1940 integral_image->compose=CopyCompositeOp;
1941 border_info.width=(unsigned long) x_offset;
1942 border_info.height=(unsigned long) y_offset;
1943 rotate_image=BorderImage(integral_image,&border_info,exception);
1944 integral_image=DestroyImage(integral_image);
1945 if (rotate_image == (Image *) NULL)
1946 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1950 (void) XShearImage(rotate_image,shear.x,width,height,x_offset,
1951 ((long) rotate_image->rows-height)/2);
1952 (void) YShearImage(rotate_image,shear.y,y_width,height,
1953 ((long) rotate_image->columns-y_width)/2,y_offset);
1954 (void) XShearImage(rotate_image,shear.x,y_width,rotate_image->rows,
1955 ((long) rotate_image->columns-y_width)/2,0);
1956 CropToFitImage(&rotate_image,shear.x,shear.y,(MagickRealType) width,
1957 (MagickRealType) height,MagickTrue,exception);
1958 rotate_image->compose=image->compose;
1959 rotate_image->page.width=0;
1960 rotate_image->page.height=0;
1961 return(rotate_image);
1965 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1969 % S h e a r I m a g e %
1973 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1975 % ShearImage() creates a new image that is a shear_image copy of an existing
1976 % one. Shearing slides one edge of an image along the X or Y axis, creating
1977 % a parallelogram. An X direction shear slides an edge along the X axis,
1978 % while a Y direction shear slides an edge along the Y axis. The amount of
1979 % the shear is controlled by a shear angle. For X direction shears, x_shear
1980 % is measured relative to the Y axis, and similarly, for Y direction shears
1981 % y_shear is measured relative to the X axis. Empty triangles left over from
1982 % shearing the image are filled with the background color defined by member
1983 % 'background_color' of the image.. ShearImage() allocates the memory
1984 % necessary for the new Image structure and returns a pointer to the new image.
1986 % ShearImage() is based on the paper "A Fast Algorithm for General Raster
1987 % Rotatation" by Alan W. Paeth.
1989 % The format of the ShearImage method is:
1991 % Image *ShearImage(const Image *image,const double x_shear,
1992 % const double y_shear,ExceptionInfo *exception)
1994 % A description of each parameter follows.
1996 % o image: the image.
1998 % o x_shear, y_shear: Specifies the number of degrees to shear the image.
2000 % o exception: return any errors or warnings in this structure.
2003 MagickExport Image *ShearImage(const Image *image,const double x_shear,
2004 const double y_shear,ExceptionInfo *exception)
2023 assert(image != (Image *) NULL);
2024 assert(image->signature == MagickSignature);
2025 if (image->debug != MagickFalse)
2026 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2027 assert(exception != (ExceptionInfo *) NULL);
2028 assert(exception->signature == MagickSignature);
2029 if ((x_shear != 0.0) && (fmod(x_shear,90.0) == 0.0))
2030 ThrowImageException(ImageError,"AngleIsDiscontinuous");
2031 if ((y_shear != 0.0) && (fmod(y_shear,90.0) == 0.0))
2032 ThrowImageException(ImageError,"AngleIsDiscontinuous");
2034 Initialize shear angle.
2036 integral_image=CloneImage(image,0,0,MagickTrue,exception);
2037 if (integral_image == (Image *) NULL)
2038 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2039 shear.x=(-tan(DegreesToRadians(fmod(x_shear,360.0))));
2040 shear.y=tan(DegreesToRadians(fmod(y_shear,360.0)));
2041 if ((shear.x == 0.0) && (shear.y == 0.0))
2042 return(integral_image);
2043 if (SetImageStorageClass(integral_image,DirectClass) == MagickFalse)
2045 InheritException(exception,&integral_image->exception);
2046 integral_image=DestroyImage(integral_image);
2047 return(integral_image);
2049 if (integral_image->matte == MagickFalse)
2050 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel);
2054 y_width=image->columns+(long) (fabs(shear.x)*image->rows+0.5);
2055 x_offset=(long) (image->columns+((fabs(shear.x)*image->rows)-image->columns)/
2057 y_offset=(long) (image->rows+((fabs(shear.y)*y_width)-image->rows)/2.0+0.5);
2059 Surround image with border.
2061 integral_image->border_color=integral_image->background_color;
2062 integral_image->compose=CopyCompositeOp;
2063 border_info.width=(unsigned long) x_offset;
2064 border_info.height=(unsigned long) y_offset;
2065 shear_image=BorderImage(integral_image,&border_info,exception);
2066 if (shear_image == (Image *) NULL)
2067 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2068 integral_image=DestroyImage(integral_image);
2072 if (shear_image->matte == MagickFalse)
2073 (void) SetImageAlphaChannel(shear_image,OpaqueAlphaChannel);
2074 (void) XShearImage(shear_image,shear.x,image->columns,image->rows,x_offset,
2075 ((long) shear_image->rows-image->rows)/2);
2076 (void) YShearImage(shear_image,shear.y,y_width,image->rows,
2077 ((long) shear_image->columns-y_width)/2,y_offset);
2078 CropToFitImage(&shear_image,shear.x,shear.y,(MagickRealType) image->columns,
2079 (MagickRealType) image->rows,MagickFalse,exception);
2080 shear_image->compose=image->compose;
2081 shear_image->page.width=0;
2082 shear_image->page.height=0;
2083 return(shear_image);