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-2011 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 "MagickCore/studio.h"
49 #include "MagickCore/artifact.h"
50 #include "MagickCore/attribute.h"
51 #include "MagickCore/blob-private.h"
52 #include "MagickCore/cache-private.h"
53 #include "MagickCore/color-private.h"
54 #include "MagickCore/colorspace-private.h"
55 #include "MagickCore/composite.h"
56 #include "MagickCore/composite-private.h"
57 #include "MagickCore/decorate.h"
58 #include "MagickCore/distort.h"
59 #include "MagickCore/draw.h"
60 #include "MagickCore/exception.h"
61 #include "MagickCore/exception-private.h"
62 #include "MagickCore/gem.h"
63 #include "MagickCore/geometry.h"
64 #include "MagickCore/image.h"
65 #include "MagickCore/image-private.h"
66 #include "MagickCore/memory_.h"
67 #include "MagickCore/list.h"
68 #include "MagickCore/monitor.h"
69 #include "MagickCore/monitor-private.h"
70 #include "MagickCore/nt-base-private.h"
71 #include "MagickCore/pixel-accessor.h"
72 #include "MagickCore/quantum.h"
73 #include "MagickCore/resource_.h"
74 #include "MagickCore/shear.h"
75 #include "MagickCore/statistic.h"
76 #include "MagickCore/string_.h"
77 #include "MagickCore/string-private.h"
78 #include "MagickCore/thread-private.h"
79 #include "MagickCore/threshold.h"
80 #include "MagickCore/transform.h"
83 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
87 % A f f i n e T r a n s f o r m I m a g e %
91 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
93 % AffineTransformImage() transforms an image as dictated by the affine matrix.
94 % It allocates the memory necessary for the new Image structure and returns
95 % a pointer to the new image.
97 % The format of the AffineTransformImage method is:
99 % Image *AffineTransformImage(const Image *image,
100 % AffineMatrix *affine_matrix,ExceptionInfo *exception)
102 % A description of each parameter follows:
104 % o image: the image.
106 % o affine_matrix: the affine matrix.
108 % o exception: return any errors or warnings in this structure.
111 MagickExport Image *AffineTransformImage(const Image *image,
112 const AffineMatrix *affine_matrix,ExceptionInfo *exception)
121 Affine transform image.
123 assert(image->signature == MagickSignature);
124 if (image->debug != MagickFalse)
125 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
126 assert(affine_matrix != (AffineMatrix *) NULL);
127 assert(exception != (ExceptionInfo *) NULL);
128 assert(exception->signature == MagickSignature);
129 distort[0]=affine_matrix->sx;
130 distort[1]=affine_matrix->rx;
131 distort[2]=affine_matrix->ry;
132 distort[3]=affine_matrix->sy;
133 distort[4]=affine_matrix->tx;
134 distort[5]=affine_matrix->ty;
135 deskew_image=DistortImage(image,AffineProjectionDistortion,6,distort,
136 MagickTrue,exception);
137 return(deskew_image);
141 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
145 + C r o p T o F i t I m a g e %
149 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
151 % CropToFitImage() crops the sheared image as determined by the bounding box
152 % as defined by width and height and shearing angles.
154 % The format of the CropToFitImage method is:
156 % MagickBooleanType CropToFitImage(Image **image,
157 % const MagickRealType x_shear,const MagickRealType x_shear,
158 % const MagickRealType width,const MagickRealType height,
159 % const MagickBooleanType rotate,ExceptionInfo *exception)
161 % A description of each parameter follows.
163 % o image: the image.
165 % o x_shear, y_shear, width, height: Defines a region of the image to crop.
167 % o exception: return any errors or warnings in this structure.
170 static MagickBooleanType CropToFitImage(Image **image,
171 const MagickRealType x_shear,const MagickRealType y_shear,
172 const MagickRealType width,const MagickRealType height,
173 const MagickBooleanType rotate,ExceptionInfo *exception)
191 Calculate the rotated image size.
193 extent[0].x=(double) (-width/2.0);
194 extent[0].y=(double) (-height/2.0);
195 extent[1].x=(double) width/2.0;
196 extent[1].y=(double) (-height/2.0);
197 extent[2].x=(double) (-width/2.0);
198 extent[2].y=(double) height/2.0;
199 extent[3].x=(double) width/2.0;
200 extent[3].y=(double) height/2.0;
201 for (i=0; i < 4; i++)
203 extent[i].x+=x_shear*extent[i].y;
204 extent[i].y+=y_shear*extent[i].x;
205 if (rotate != MagickFalse)
206 extent[i].x+=x_shear*extent[i].y;
207 extent[i].x+=(double) (*image)->columns/2.0;
208 extent[i].y+=(double) (*image)->rows/2.0;
212 for (i=1; i < 4; i++)
214 if (min.x > extent[i].x)
216 if (min.y > extent[i].y)
218 if (max.x < extent[i].x)
220 if (max.y < extent[i].y)
223 geometry.x=(ssize_t) ceil(min.x-0.5);
224 geometry.y=(ssize_t) ceil(min.y-0.5);
225 geometry.width=(size_t) floor(max.x-min.x+0.5);
226 geometry.height=(size_t) floor(max.y-min.y+0.5);
228 (void) ParseAbsoluteGeometry("0x0+0+0",&(*image)->page);
229 crop_image=CropImage(*image,&geometry,exception);
230 if (crop_image == (Image *) NULL)
232 crop_image->page=page;
233 *image=DestroyImage(*image);
239 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
243 % D e s k e w I m a g e %
247 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
249 % DeskewImage() removes skew from the image. Skew is an artifact that
250 % occurs in scanned images because of the camera being misaligned,
251 % imperfections in the scanning or surface, or simply because the paper was
252 % not placed completely flat when scanned.
254 % The format of the DeskewImage method is:
256 % Image *DeskewImage(const Image *image,const double threshold,
257 % ExceptionInfo *exception)
259 % A description of each parameter follows:
261 % o image: the image.
263 % o threshold: separate background from foreground.
265 % o exception: return any errors or warnings in this structure.
269 typedef struct _RadonInfo
294 static RadonInfo *DestroyRadonInfo(RadonInfo *radon_info)
296 assert(radon_info != (RadonInfo *) NULL);
297 switch (radon_info->type)
301 if (radon_info->mapped == MagickFalse)
302 radon_info->cells=(unsigned short *) RelinquishMagickMemory(
305 radon_info->cells=(unsigned short *) UnmapBlob(radon_info->cells,
306 (size_t) radon_info->length);
307 RelinquishMagickResource(MemoryResource,radon_info->length);
312 radon_info->cells=(unsigned short *) UnmapBlob(radon_info->cells,(size_t)
314 RelinquishMagickResource(MapResource,radon_info->length);
318 if (radon_info->file != -1)
319 (void) close(radon_info->file);
320 (void) RelinquishUniqueFileResource(radon_info->path);
321 RelinquishMagickResource(DiskResource,radon_info->length);
327 return((RadonInfo *) RelinquishMagickMemory(radon_info));
330 static MagickBooleanType ResetRadonCells(RadonInfo *radon_info)
342 if (radon_info->type != DiskCache)
344 (void) ResetMagickMemory(radon_info->cells,0,(size_t) radon_info->length);
348 (void) lseek(radon_info->file,0,SEEK_SET);
349 for (y=0; y < (ssize_t) radon_info->height; y++)
351 for (x=0; x < (ssize_t) radon_info->width; x++)
353 count=write(radon_info->file,&value,sizeof(*radon_info->cells));
354 if (count != (ssize_t) sizeof(*radon_info->cells))
357 if (x < (ssize_t) radon_info->width)
360 return(y < (ssize_t) radon_info->height ? MagickFalse : MagickTrue);
363 static RadonInfo *AcquireRadonInfo(const Image *image,const size_t width,
364 const size_t height,ExceptionInfo *exception)
372 radon_info=(RadonInfo *) AcquireMagickMemory(sizeof(*radon_info));
373 if (radon_info == (RadonInfo *) NULL)
374 return((RadonInfo *) NULL);
375 (void) ResetMagickMemory(radon_info,0,sizeof(*radon_info));
376 radon_info->width=width;
377 radon_info->height=height;
378 radon_info->length=(MagickSizeType) width*height*sizeof(*radon_info->cells);
379 radon_info->type=MemoryCache;
380 status=AcquireMagickResource(AreaResource,radon_info->length);
381 if ((status != MagickFalse) &&
382 (radon_info->length == (MagickSizeType) ((size_t) radon_info->length)))
384 status=AcquireMagickResource(MemoryResource,radon_info->length);
385 if (status != MagickFalse)
387 radon_info->mapped=MagickFalse;
388 radon_info->cells=(unsigned short *) AcquireMagickMemory((size_t)
390 if (radon_info->cells == (unsigned short *) NULL)
392 radon_info->mapped=MagickTrue;
393 radon_info->cells=(unsigned short *) MapBlob(-1,IOMode,0,(size_t)
396 if (radon_info->cells == (unsigned short *) NULL)
397 RelinquishMagickResource(MemoryResource,radon_info->length);
400 radon_info->file=(-1);
401 if (radon_info->cells == (unsigned short *) NULL)
403 status=AcquireMagickResource(DiskResource,radon_info->length);
404 if (status == MagickFalse)
406 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
407 "CacheResourcesExhausted","`%s'",image->filename);
408 return(DestroyRadonInfo(radon_info));
410 radon_info->type=DiskCache;
411 (void) AcquireMagickResource(MemoryResource,radon_info->length);
412 radon_info->file=AcquireUniqueFileResource(radon_info->path);
413 if (radon_info->file == -1)
414 return(DestroyRadonInfo(radon_info));
415 status=AcquireMagickResource(MapResource,radon_info->length);
416 if (status != MagickFalse)
418 status=ResetRadonCells(radon_info);
419 if (status != MagickFalse)
421 radon_info->cells=(unsigned short *) MapBlob(radon_info->file,
422 IOMode,0,(size_t) radon_info->length);
423 if (radon_info->cells != (unsigned short *) NULL)
424 radon_info->type=MapCache;
426 RelinquishMagickResource(MapResource,radon_info->length);
433 static inline size_t MagickMin(const size_t x,const size_t y)
440 static inline ssize_t ReadRadonCell(const RadonInfo *radon_info,
441 const MagickOffsetType offset,const size_t length,unsigned char *buffer)
449 #if !defined(MAGICKCORE_HAVE_PPREAD)
450 #if defined(MAGICKCORE_OPENMP_SUPPORT)
451 #pragma omp critical (MagickCore_ReadRadonCell)
455 if (lseek(radon_info->file,offset,SEEK_SET) >= 0)
459 for (i=0; i < (ssize_t) length; i+=count)
461 #if !defined(MAGICKCORE_HAVE_PPREAD)
462 count=read(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
465 count=pread(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
466 SSIZE_MAX),offset+i);
477 #if !defined(MAGICKCORE_HAVE_PPREAD)
484 static inline ssize_t WriteRadonCell(const RadonInfo *radon_info,
485 const MagickOffsetType offset,const size_t length,const unsigned char *buffer)
493 #if !defined(MAGICKCORE_HAVE_PWRITE)
494 #if defined(MAGICKCORE_OPENMP_SUPPORT)
495 #pragma omp critical (MagickCore_WriteRadonCell)
498 if (lseek(radon_info->file,offset,SEEK_SET) >= 0)
502 for (i=0; i < (ssize_t) length; i+=count)
504 #if !defined(MAGICKCORE_HAVE_PWRITE)
505 count=write(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
508 count=pwrite(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
509 SSIZE_MAX),offset+i);
520 #if !defined(MAGICKCORE_HAVE_PWRITE)
527 static inline unsigned short GetRadonCell(const RadonInfo *radon_info,
528 const ssize_t x,const ssize_t y)
536 i=(MagickOffsetType) radon_info->height*x+y;
538 ((MagickSizeType) (i*sizeof(*radon_info->cells)) >= radon_info->length))
540 if (radon_info->type != DiskCache)
541 return(radon_info->cells[i]);
543 (void) ReadRadonCell(radon_info,i*sizeof(*radon_info->cells),
544 sizeof(*radon_info->cells),(unsigned char *) &value);
548 static inline MagickBooleanType SetRadonCell(const RadonInfo *radon_info,
549 const ssize_t x,const ssize_t y,const unsigned short value)
557 i=(MagickOffsetType) radon_info->height*x+y;
559 ((MagickSizeType) (i*sizeof(*radon_info->cells)) >= radon_info->length))
561 if (radon_info->type != DiskCache)
563 radon_info->cells[i]=value;
566 count=WriteRadonCell(radon_info,i*sizeof(*radon_info->cells),
567 sizeof(*radon_info->cells),(const unsigned char *) &value);
568 if (count != (ssize_t) sizeof(*radon_info->cells))
573 static void RadonProjection(RadonInfo *source_cells,
574 RadonInfo *destination_cells,const ssize_t sign,size_t *projection)
591 for (step=1; step < p->width; step*=2)
593 for (x=0; x < (ssize_t) p->width; x+=2*(ssize_t) step)
604 for (i=0; i < (ssize_t) step; i++)
606 for (y=0; y < (ssize_t) (p->height-i-1); y++)
608 cell=GetRadonCell(p,x+i,y);
609 (void) SetRadonCell(q,x+2*i,y,cell+GetRadonCell(p,x+i+(ssize_t)
611 (void) SetRadonCell(q,x+2*i+1,y,cell+GetRadonCell(p,x+i+(ssize_t)
614 for ( ; y < (ssize_t) (p->height-i); y++)
616 cell=GetRadonCell(p,x+i,y);
617 (void) SetRadonCell(q,x+2*i,y,cell+GetRadonCell(p,x+i+(ssize_t) step,
619 (void) SetRadonCell(q,x+2*i+1,y,cell);
621 for ( ; y < (ssize_t) p->height; y++)
623 cell=GetRadonCell(p,x+i,y);
624 (void) SetRadonCell(q,x+2*i,y,cell);
625 (void) SetRadonCell(q,x+2*i+1,y,cell);
633 #if defined(MAGICKCORE_OPENMP_SUPPORT)
634 #pragma omp parallel for schedule(dynamic,4)
636 for (x=0; x < (ssize_t) p->width; x++)
645 for (y=0; y < (ssize_t) (p->height-1); y++)
650 delta=GetRadonCell(p,x,y)-(ssize_t) GetRadonCell(p,x,y+1);
653 projection[p->width+sign*x-1]=sum;
657 static MagickBooleanType RadonTransform(const Image *image,
658 const double threshold,size_t *projection,ExceptionInfo *exception)
686 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
687 source_cells=AcquireRadonInfo(image,width,image->rows,exception);
688 destination_cells=AcquireRadonInfo(image,width,image->rows,exception);
689 if ((source_cells == (RadonInfo *) NULL) ||
690 (destination_cells == (RadonInfo *) NULL))
692 if (destination_cells != (RadonInfo *) NULL)
693 destination_cells=DestroyRadonInfo(destination_cells);
694 if (source_cells != (RadonInfo *) NULL)
695 source_cells=DestroyRadonInfo(source_cells);
698 if (ResetRadonCells(source_cells) == MagickFalse)
700 destination_cells=DestroyRadonInfo(destination_cells);
701 source_cells=DestroyRadonInfo(source_cells);
704 for (i=0; i < 256; i++)
706 byte=(unsigned char) i;
707 for (count=0; byte != 0; byte>>=1)
709 bits[i]=(unsigned short) count;
712 image_view=AcquireCacheView(image);
713 #if defined(MAGICKCORE_OPENMP_SUPPORT)
714 #pragma omp parallel for schedule(dynamic,4) shared(status)
716 for (y=0; y < (ssize_t) image->rows; y++)
718 register const Quantum
729 if (status == MagickFalse)
731 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
732 if (p == (const Quantum *) NULL)
739 i=(ssize_t) (image->columns+7)/8;
740 for (x=0; x < (ssize_t) image->columns; x++)
743 if ((double) GetPixelIntensity(image,p) < threshold)
748 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
752 p+=GetPixelChannels(image);
757 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
760 RadonProjection(source_cells,destination_cells,-1,projection);
761 (void) ResetRadonCells(source_cells);
762 #if defined(MAGICKCORE_OPENMP_SUPPORT)
763 #pragma omp parallel for schedule(dynamic,4) shared(status)
765 for (y=0; y < (ssize_t) image->rows; y++)
767 register const Quantum
778 if (status == MagickFalse)
780 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
781 if (p == (const Quantum *) NULL)
789 for (x=0; x < (ssize_t) image->columns; x++)
792 if ((double) GetPixelIntensity(image,p) < threshold)
797 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
801 p+=GetPixelChannels(image);
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 ssize_t offset,
817 ExceptionInfo *exception)
832 Compute average background color.
836 GetPixelInfo(image,&background);
838 image_view=AcquireCacheView(image);
839 for (y=0; y < (ssize_t) image->rows; y++)
841 register const Quantum
847 if ((y >= offset) && (y < ((ssize_t) image->rows-offset)))
849 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
850 if (p == (const Quantum *) NULL)
852 for (x=0; x < (ssize_t) image->columns; x++)
854 if ((x >= offset) && (x < ((ssize_t) image->columns-offset)))
856 background.red+=QuantumScale*GetPixelRed(image,p);
857 background.green+=QuantumScale*GetPixelGreen(image,p);
858 background.blue+=QuantumScale*GetPixelBlue(image,p);
859 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
860 background.alpha+=QuantumScale*GetPixelAlpha(image,p);
862 p+=GetPixelChannels(image);
865 image_view=DestroyCacheView(image_view);
866 image->background_color.red=(double) ClampToQuantum((MagickRealType)
867 QuantumRange*background.red/count);
868 image->background_color.green=(double) ClampToQuantum((MagickRealType)
869 QuantumRange*background.green/count);
870 image->background_color.blue=(double) ClampToQuantum((MagickRealType)
871 QuantumRange*background.blue/count);
872 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
873 image->background_color.alpha=(double) ClampToQuantum((MagickRealType)
874 QuantumRange*background.alpha/count);
877 MagickExport Image *DeskewImage(const Image *image,const double threshold,
878 ExceptionInfo *exception)
913 Compute deskew angle.
915 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
916 projection=(size_t *) AcquireQuantumMemory((size_t) (2*width-1),
917 sizeof(*projection));
918 if (projection == (size_t *) NULL)
919 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
920 status=RadonTransform(image,threshold,projection,exception);
921 if (status == MagickFalse)
923 projection=(size_t *) RelinquishMagickMemory(projection);
924 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
928 for (i=0; i < (ssize_t) (2*width-1); i++)
930 if (projection[i] > max_projection)
932 skew=i-(ssize_t) width+1;
933 max_projection=projection[i];
936 projection=(size_t *) RelinquishMagickMemory(projection);
940 clone_image=CloneImage(image,0,0,MagickTrue,exception);
941 if (clone_image == (Image *) NULL)
942 return((Image *) NULL);
943 (void) SetImageVirtualPixelMethod(clone_image,BackgroundVirtualPixelMethod);
944 degrees=RadiansToDegrees(-atan((double) skew/width/8));
945 if (image->debug != MagickFalse)
946 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
947 " Deskew angle: %g",degrees);
948 affine_matrix.sx=cos(DegreesToRadians(fmod((double) degrees,360.0)));
949 affine_matrix.rx=sin(DegreesToRadians(fmod((double) degrees,360.0)));
950 affine_matrix.ry=(-sin(DegreesToRadians(fmod((double) degrees,360.0))));
951 affine_matrix.sy=cos(DegreesToRadians(fmod((double) degrees,360.0)));
952 affine_matrix.tx=0.0;
953 affine_matrix.ty=0.0;
954 artifact=GetImageArtifact(image,"deskew:auto-crop");
955 if (artifact == (const char *) NULL)
957 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
958 clone_image=DestroyImage(clone_image);
959 return(deskew_image);
964 GetImageBackgroundColor(clone_image,(ssize_t) StringToLong(artifact),
966 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
967 clone_image=DestroyImage(clone_image);
968 if (deskew_image == (Image *) NULL)
969 return((Image *) NULL);
970 median_image=StatisticImage(deskew_image,MedianStatistic,3,3,exception);
971 if (median_image == (Image *) NULL)
973 deskew_image=DestroyImage(deskew_image);
974 return((Image *) NULL);
976 geometry=GetImageBoundingBox(median_image,exception);
977 median_image=DestroyImage(median_image);
978 if (image->debug != MagickFalse)
979 (void) LogMagickEvent(TransformEvent,GetMagickModule()," Deskew geometry: "
980 "%.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
981 geometry.height,(double) geometry.x,(double) geometry.y);
982 crop_image=CropImage(deskew_image,&geometry,exception);
983 deskew_image=DestroyImage(deskew_image);
988 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
992 + I n t e g r a l R o t a t e I m a g e %
996 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
998 % IntegralRotateImage() rotates the image an integral of 90 degrees. It
999 % allocates the memory necessary for the new Image structure and returns a
1000 % pointer to the rotated image.
1002 % The format of the IntegralRotateImage method is:
1004 % Image *IntegralRotateImage(const Image *image,size_t rotations,
1005 % ExceptionInfo *exception)
1007 % A description of each parameter follows.
1009 % o image: the image.
1011 % o rotations: Specifies the number of 90 degree rotations.
1014 static Image *IntegralRotateImage(const Image *image,size_t rotations,
1015 ExceptionInfo *exception)
1017 #define RotateImageTag "Rotate/Image"
1039 Initialize rotated image attributes.
1041 assert(image != (Image *) NULL);
1045 return(CloneImage(image,0,0,MagickTrue,exception));
1046 if ((rotations == 1) || (rotations == 3))
1047 rotate_image=CloneImage(image,image->rows,image->columns,MagickTrue,
1050 rotate_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1052 if (rotate_image == (Image *) NULL)
1053 return((Image *) NULL);
1055 Integral rotate the image.
1059 image_view=AcquireCacheView(image);
1060 rotate_view=AcquireCacheView(rotate_image);
1082 GetPixelCacheTileSize(image,&tile_width,&tile_height);
1083 tile_width=image->columns;
1084 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1085 #pragma omp parallel for schedule(dynamic,4) shared(progress, status) omp_throttle(1)
1087 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
1092 if (status == MagickFalse)
1095 for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
1100 register const Quantum
1114 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
1115 width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
1117 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
1118 height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
1119 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1121 if (p == (const Quantum *) NULL)
1126 for (y=0; y < (ssize_t) width; y++)
1128 register const Quantum
1129 *restrict tile_pixels;
1134 if (status == MagickFalse)
1136 q=QueueCacheViewAuthenticPixels(rotate_view,(ssize_t)
1137 (rotate_image->columns-(tile_y+height)),y+tile_x,height,1,
1139 if (q == (Quantum *) NULL)
1144 tile_pixels=p+((height-1)*width+y)*GetPixelChannels(image);
1145 for (x=0; x < (ssize_t) height; x++)
1150 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1159 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
1160 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
1161 rotate_traits=GetPixelChannelMapTraits(rotate_image,channel);
1162 if ((traits == UndefinedPixelTrait) ||
1163 (rotate_traits == UndefinedPixelTrait))
1165 SetPixelChannel(rotate_image,channel,tile_pixels[i],q);
1167 tile_pixels-=width*GetPixelChannels(image);
1168 q+=GetPixelChannels(rotate_image);
1170 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1171 if (sync == MagickFalse)
1175 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1180 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1181 #pragma omp critical (MagickCore_IntegralRotateImage)
1183 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
1185 if (proceed == MagickFalse)
1189 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1190 image->rows-1,image->rows);
1191 Swap(page.width,page.height);
1192 Swap(page.x,page.y);
1193 if (page.width != 0)
1194 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1202 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1203 #pragma omp parallel for schedule(dynamic,4) shared(progress,status) omp_throttle(1)
1205 for (y=0; y < (ssize_t) image->rows; y++)
1210 register const Quantum
1219 if (status == MagickFalse)
1221 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1222 q=QueueCacheViewAuthenticPixels(rotate_view,0,(ssize_t) (image->rows-y-
1223 1),image->columns,1,exception);
1224 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1229 q+=GetPixelChannels(rotate_image)*image->columns;
1230 for (x=0; x < (ssize_t) image->columns; x++)
1235 q-=GetPixelChannels(rotate_image);
1236 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1245 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
1246 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
1247 rotate_traits=GetPixelChannelMapTraits(rotate_image,channel);
1248 if ((traits == UndefinedPixelTrait) ||
1249 (rotate_traits == UndefinedPixelTrait))
1251 SetPixelChannel(rotate_image,channel,p[i],q);
1253 p+=GetPixelChannels(image);
1255 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1256 if (sync == MagickFalse)
1258 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1263 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1264 #pragma omp critical (MagickCore_IntegralRotateImage)
1266 proceed=SetImageProgress(image,RotateImageTag,progress++,
1268 if (proceed == MagickFalse)
1272 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1273 image->rows-1,image->rows);
1274 Swap(page.width,page.height);
1275 Swap(page.x,page.y);
1276 if (page.width != 0)
1277 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1292 GetPixelCacheTileSize(image,&tile_width,&tile_height);
1293 tile_width=image->columns;
1294 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1295 #pragma omp parallel for schedule(dynamic,4) shared(progress,status) omp_throttle(1)
1297 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
1302 if (status == MagickFalse)
1305 for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
1310 register const Quantum
1324 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
1325 width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
1327 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
1328 height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
1329 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1331 if (p == (const Quantum *) NULL)
1336 for (y=0; y < (ssize_t) width; y++)
1338 register const Quantum
1339 *restrict tile_pixels;
1344 if (status == MagickFalse)
1346 q=QueueCacheViewAuthenticPixels(rotate_view,tile_y,(ssize_t) (y+
1347 rotate_image->rows-(tile_x+width)),height,1,exception);
1348 if (q == (Quantum *) NULL)
1353 tile_pixels=p+((width-1)-y)*GetPixelChannels(image);
1354 for (x=0; x < (ssize_t) height; x++)
1359 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1368 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
1369 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
1370 rotate_traits=GetPixelChannelMapTraits(rotate_image,channel);
1371 if ((traits == UndefinedPixelTrait) ||
1372 (rotate_traits == UndefinedPixelTrait))
1374 SetPixelChannel(rotate_image,channel,tile_pixels[i],q);
1376 tile_pixels+=width*GetPixelChannels(image);
1377 q+=GetPixelChannels(rotate_image);
1379 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1380 if (sync == MagickFalse)
1384 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1389 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1390 #pragma omp critical (MagickCore_IntegralRotateImage)
1392 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
1394 if (proceed == MagickFalse)
1398 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1399 image->rows-1,image->rows);
1400 Swap(page.width,page.height);
1401 Swap(page.x,page.y);
1402 if (page.width != 0)
1403 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1407 rotate_view=DestroyCacheView(rotate_view);
1408 image_view=DestroyCacheView(image_view);
1409 rotate_image->type=image->type;
1410 rotate_image->page=page;
1411 if (status == MagickFalse)
1412 rotate_image=DestroyImage(rotate_image);
1413 return(rotate_image);
1417 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1421 + X S h e a r I m a g e %
1425 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1427 % XShearImage() shears the image in the X direction with a shear angle of
1428 % 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1429 % negative angles shear clockwise. Angles are measured relative to a vertical
1430 % Y-axis. X shears will widen an image creating 'empty' triangles on the left
1431 % and right sides of the source image.
1433 % The format of the XShearImage method is:
1435 % MagickBooleanType XShearImage(Image *image,const MagickRealType degrees,
1436 % const size_t width,const size_t height,
1437 % const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
1439 % A description of each parameter follows.
1441 % o image: the image.
1443 % o degrees: A MagickRealType representing the shearing angle along the X
1446 % o width, height, x_offset, y_offset: Defines a region of the image
1449 % o exception: return any errors or warnings in this structure.
1452 static MagickBooleanType XShearImage(Image *image,const MagickRealType degrees,
1453 const size_t width,const size_t height,const ssize_t x_offset,
1454 const ssize_t y_offset,ExceptionInfo *exception)
1456 #define XShearImageTag "XShear/Image"
1479 assert(image != (Image *) NULL);
1480 assert(image->signature == MagickSignature);
1481 if (image->debug != MagickFalse)
1482 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1483 GetPixelInfo(image,&background);
1484 SetPixelInfoPacket(image,&image->background_color,&background);
1485 if (image->colorspace == CMYKColorspace)
1486 ConvertRGBToCMYK(&background);
1492 image_view=AcquireCacheView(image);
1493 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1494 #pragma omp parallel for schedule(dynamic,4) shared(progress, status)
1496 for (y=0; y < (ssize_t) height; y++)
1520 if (status == MagickFalse)
1522 p=GetCacheViewAuthenticPixels(image_view,0,y_offset+y,image->columns,1,
1524 if (p == (Quantum *) NULL)
1529 p+=x_offset*GetPixelChannels(image);
1530 displacement=degrees*(MagickRealType) (y-height/2.0);
1531 if (displacement == 0.0)
1533 if (displacement > 0.0)
1537 displacement*=(-1.0);
1540 step=(ssize_t) floor((double) displacement);
1541 area=(MagickRealType) (displacement-step);
1544 GetPixelInfo(image,&source);
1545 GetPixelInfo(image,&destination);
1551 Transfer pixels left-to-right.
1553 if (step > x_offset)
1555 q=p-step*GetPixelChannels(image);
1556 for (i=0; i < (ssize_t) width; i++)
1558 if ((x_offset+i) < step)
1560 p+=GetPixelChannels(image);
1561 SetPixelInfo(image,p,&pixel);
1562 q+=GetPixelChannels(image);
1565 SetPixelInfo(image,p,&source);
1566 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1567 &source,(MagickRealType) GetPixelAlpha(image,p),area,&destination);
1568 SetPixelPixelInfo(image,&destination,q);
1569 SetPixelInfo(image,p,&pixel);
1570 p+=GetPixelChannels(image);
1571 q+=GetPixelChannels(image);
1573 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1574 &background,(MagickRealType) background.alpha,area,&destination);
1575 SetPixelPixelInfo(image,&destination,q);
1576 q+=GetPixelChannels(image);
1577 for (i=0; i < (step-1); i++)
1579 SetPixelPixelInfo(image,&background,q);
1580 q+=GetPixelChannels(image);
1587 Transfer pixels right-to-left.
1589 p+=width*GetPixelChannels(image);
1590 q=p+step*GetPixelChannels(image);
1591 for (i=0; i < (ssize_t) width; i++)
1593 p-=GetPixelChannels(image);
1594 q-=GetPixelChannels(image);
1595 if ((size_t) (x_offset+width+step-i) >= image->columns)
1597 SetPixelInfo(image,p,&source);
1598 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1599 &source,(MagickRealType) GetPixelAlpha(image,p),area,&destination);
1600 SetPixelPixelInfo(image,&destination,q);
1601 SetPixelInfo(image,p,&pixel);
1603 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1604 &background,(MagickRealType) background.alpha,area,&destination);
1605 q-=GetPixelChannels(image);
1606 SetPixelPixelInfo(image,&destination,q);
1607 for (i=0; i < (step-1); i++)
1609 q-=GetPixelChannels(image);
1610 SetPixelPixelInfo(image,&background,q);
1615 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1617 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1622 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1623 #pragma omp critical (MagickCore_XShearImage)
1625 proceed=SetImageProgress(image,XShearImageTag,progress++,height);
1626 if (proceed == MagickFalse)
1630 image_view=DestroyCacheView(image_view);
1635 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1639 + Y S h e a r I m a g e %
1643 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1645 % YShearImage shears the image in the Y direction with a shear angle of
1646 % 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1647 % negative angles shear clockwise. Angles are measured relative to a
1648 % horizontal X-axis. Y shears will increase the height of an image creating
1649 % 'empty' triangles on the top and bottom of the source image.
1651 % The format of the YShearImage method is:
1653 % MagickBooleanType YShearImage(Image *image,const MagickRealType degrees,
1654 % const size_t width,const size_t height,
1655 % const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
1657 % A description of each parameter follows.
1659 % o image: the image.
1661 % o degrees: A MagickRealType representing the shearing angle along the Y
1664 % o width, height, x_offset, y_offset: Defines a region of the image
1667 % o exception: return any errors or warnings in this structure.
1670 static MagickBooleanType YShearImage(Image *image,const MagickRealType degrees,
1671 const size_t width,const size_t height,const ssize_t x_offset,
1672 const ssize_t y_offset,ExceptionInfo *exception)
1674 #define YShearImageTag "YShear/Image"
1697 assert(image != (Image *) NULL);
1698 assert(image->signature == MagickSignature);
1699 if (image->debug != MagickFalse)
1700 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1701 GetPixelInfo(image,&background);
1702 SetPixelInfoPacket(image,&image->background_color,&background);
1703 if (image->colorspace == CMYKColorspace)
1704 ConvertRGBToCMYK(&background);
1710 image_view=AcquireCacheView(image);
1711 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1712 #pragma omp parallel for schedule(dynamic,4) shared(progress, status)
1714 for (x=0; x < (ssize_t) width; x++)
1738 if (status == MagickFalse)
1740 p=GetCacheViewAuthenticPixels(image_view,x_offset+x,0,1,image->rows,
1742 if (p == (Quantum *) NULL)
1747 p+=y_offset*GetPixelChannels(image);
1748 displacement=degrees*(MagickRealType) (x-width/2.0);
1749 if (displacement == 0.0)
1751 if (displacement > 0.0)
1755 displacement*=(-1.0);
1758 step=(ssize_t) floor((double) displacement);
1759 area=(MagickRealType) (displacement-step);
1762 GetPixelInfo(image,&source);
1763 GetPixelInfo(image,&destination);
1769 Transfer pixels top-to-bottom.
1771 if (step > y_offset)
1773 q=p-step*GetPixelChannels(image);
1774 for (i=0; i < (ssize_t) height; i++)
1776 if ((y_offset+i) < step)
1778 p+=GetPixelChannels(image);
1779 SetPixelInfo(image,p,&pixel);
1780 q+=GetPixelChannels(image);
1783 SetPixelInfo(image,p,&source);
1784 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1785 &source,(MagickRealType) GetPixelAlpha(image,p),area,
1787 SetPixelPixelInfo(image,&destination,q);
1788 SetPixelInfo(image,p,&pixel);
1789 p+=GetPixelChannels(image);
1790 q+=GetPixelChannels(image);
1792 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1793 &background,(MagickRealType) background.alpha,area,&destination);
1794 SetPixelPixelInfo(image,&destination,q);
1795 q+=GetPixelChannels(image);
1796 for (i=0; i < (step-1); i++)
1798 SetPixelPixelInfo(image,&background,q);
1799 q+=GetPixelChannels(image);
1806 Transfer pixels bottom-to-top.
1808 p+=height*GetPixelChannels(image);
1809 q=p+step*GetPixelChannels(image);
1810 for (i=0; i < (ssize_t) height; i++)
1812 p-=GetPixelChannels(image);
1813 q-=GetPixelChannels(image);
1814 if ((size_t) (y_offset+height+step-i) >= image->rows)
1816 SetPixelInfo(image,p,&source);
1817 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1818 &source,(MagickRealType) GetPixelAlpha(image,p),area,
1820 SetPixelPixelInfo(image,&destination,q);
1821 SetPixelInfo(image,p,&pixel);
1823 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1824 &background,(MagickRealType) background.alpha,area,&destination);
1825 q-=GetPixelChannels(image);
1826 SetPixelPixelInfo(image,&destination,q);
1827 for (i=0; i < (step-1); i++)
1829 q-=GetPixelChannels(image);
1830 SetPixelPixelInfo(image,&background,q);
1835 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1837 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1842 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1843 #pragma omp critical (MagickCore_YShearImage)
1845 proceed=SetImageProgress(image,YShearImageTag,progress++,image->rows);
1846 if (proceed == MagickFalse)
1850 image_view=DestroyCacheView(image_view);
1855 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1859 % R o t a t e I m a g e %
1863 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1865 % RotateImage() creates a new image that is a rotated copy of an existing
1866 % one. Positive angles rotate counter-clockwise (right-hand rule), while
1867 % negative angles rotate clockwise. Rotated images are usually larger than
1868 % the originals and have 'empty' triangular corners. X axis. Empty
1869 % triangles left over from shearing the image are filled with the background
1870 % color defined by member 'background_color' of the image. RotateImage
1871 % allocates the memory necessary for the new Image structure and returns a
1872 % pointer to the new image.
1874 % RotateImage() is based on the paper "A Fast Algorithm for General
1875 % Raster Rotatation" by Alan W. Paeth. RotateImage is adapted from a similar
1876 % method based on the Paeth paper written by Michael Halle of the Spatial
1877 % Imaging Group, MIT Media Lab.
1879 % The format of the RotateImage method is:
1881 % Image *RotateImage(const Image *image,const double degrees,
1882 % ExceptionInfo *exception)
1884 % A description of each parameter follows.
1886 % o image: the image.
1888 % o degrees: Specifies the number of degrees to rotate the image.
1890 % o exception: return any errors or warnings in this structure.
1893 MagickExport Image *RotateImage(const Image *image,const double degrees,
1894 ExceptionInfo *exception)
1923 Adjust rotation angle.
1925 assert(image != (Image *) NULL);
1926 assert(image->signature == MagickSignature);
1927 if (image->debug != MagickFalse)
1928 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1929 assert(exception != (ExceptionInfo *) NULL);
1930 assert(exception->signature == MagickSignature);
1932 while (angle < -45.0)
1934 for (rotations=0; angle > 45.0; rotations++)
1938 Calculate shear equations.
1940 integral_image=IntegralRotateImage(image,rotations,exception);
1941 if (integral_image == (Image *) NULL)
1942 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1943 shear.x=(-tan((double) DegreesToRadians(angle)/2.0));
1944 shear.y=sin((double) DegreesToRadians(angle));
1945 if ((shear.x == 0.0) && (shear.y == 0.0))
1946 return(integral_image);
1947 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
1949 integral_image=DestroyImage(integral_image);
1950 return(integral_image);
1952 if (integral_image->matte == MagickFalse)
1953 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
1957 width=image->columns;
1959 if ((rotations == 1) || (rotations == 3))
1962 height=image->columns;
1964 y_width=width+(ssize_t) floor(fabs(shear.x)*height+0.5);
1965 x_offset=(ssize_t) ceil((double) width+((fabs(shear.y)*height)-width)/2.0-
1967 y_offset=(ssize_t) ceil((double) height+((fabs(shear.y)*y_width)-height)/2.0-
1970 Surround image with a border.
1972 integral_image->border_color=integral_image->background_color;
1973 integral_image->compose=CopyCompositeOp;
1974 border_info.width=(size_t) x_offset;
1975 border_info.height=(size_t) y_offset;
1976 rotate_image=BorderImage(integral_image,&border_info,image->compose,
1978 integral_image=DestroyImage(integral_image);
1979 if (rotate_image == (Image *) NULL)
1980 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1984 status=XShearImage(rotate_image,shear.x,width,height,x_offset,(ssize_t)
1985 (rotate_image->rows-height)/2,exception);
1986 if (status == MagickFalse)
1988 rotate_image=DestroyImage(rotate_image);
1989 return((Image *) NULL);
1991 status=YShearImage(rotate_image,shear.y,y_width,height,(ssize_t)
1992 (rotate_image->columns-y_width)/2,y_offset,exception);
1993 if (status == MagickFalse)
1995 rotate_image=DestroyImage(rotate_image);
1996 return((Image *) NULL);
1998 status=XShearImage(rotate_image,shear.x,y_width,rotate_image->rows,(ssize_t)
1999 (rotate_image->columns-y_width)/2,0,exception);
2000 if (status == MagickFalse)
2002 rotate_image=DestroyImage(rotate_image);
2003 return((Image *) NULL);
2005 status=CropToFitImage(&rotate_image,shear.x,shear.y,(MagickRealType) width,
2006 (MagickRealType) height,MagickTrue,exception);
2007 if (status == MagickFalse)
2009 rotate_image=DestroyImage(rotate_image);
2010 return((Image *) NULL);
2012 rotate_image->compose=image->compose;
2013 rotate_image->page.width=0;
2014 rotate_image->page.height=0;
2015 return(rotate_image);
2019 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2023 % S h e a r I m a g e %
2027 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2029 % ShearImage() creates a new image that is a shear_image copy of an existing
2030 % one. Shearing slides one edge of an image along the X or Y axis, creating
2031 % a parallelogram. An X direction shear slides an edge along the X axis,
2032 % while a Y direction shear slides an edge along the Y axis. The amount of
2033 % the shear is controlled by a shear angle. For X direction shears, x_shear
2034 % is measured relative to the Y axis, and similarly, for Y direction shears
2035 % y_shear is measured relative to the X axis. Empty triangles left over from
2036 % shearing the image are filled with the background color defined by member
2037 % 'background_color' of the image.. ShearImage() allocates the memory
2038 % necessary for the new Image structure and returns a pointer to the new image.
2040 % ShearImage() is based on the paper "A Fast Algorithm for General Raster
2041 % Rotatation" by Alan W. Paeth.
2043 % The format of the ShearImage method is:
2045 % Image *ShearImage(const Image *image,const double x_shear,
2046 % const double y_shear,ExceptionInfo *exception)
2048 % A description of each parameter follows.
2050 % o image: the image.
2052 % o x_shear, y_shear: Specifies the number of degrees to shear the image.
2054 % o exception: return any errors or warnings in this structure.
2057 MagickExport Image *ShearImage(const Image *image,const double x_shear,
2058 const double y_shear,ExceptionInfo *exception)
2080 assert(image != (Image *) NULL);
2081 assert(image->signature == MagickSignature);
2082 if (image->debug != MagickFalse)
2083 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2084 assert(exception != (ExceptionInfo *) NULL);
2085 assert(exception->signature == MagickSignature);
2086 if ((x_shear != 0.0) && (fmod(x_shear,90.0) == 0.0))
2087 ThrowImageException(ImageError,"AngleIsDiscontinuous");
2088 if ((y_shear != 0.0) && (fmod(y_shear,90.0) == 0.0))
2089 ThrowImageException(ImageError,"AngleIsDiscontinuous");
2091 Initialize shear angle.
2093 integral_image=CloneImage(image,0,0,MagickTrue,exception);
2094 if (integral_image == (Image *) NULL)
2095 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2096 shear.x=(-tan(DegreesToRadians(fmod(x_shear,360.0))));
2097 shear.y=tan(DegreesToRadians(fmod(y_shear,360.0)));
2098 if ((shear.x == 0.0) && (shear.y == 0.0))
2099 return(integral_image);
2100 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
2102 integral_image=DestroyImage(integral_image);
2103 return(integral_image);
2105 if (integral_image->matte == MagickFalse)
2106 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
2110 y_width=image->columns+(ssize_t) floor(fabs(shear.x)*image->rows+0.5);
2111 x_offset=(ssize_t) ceil((double) image->columns+((fabs(shear.x)*image->rows)-
2112 image->columns)/2.0-0.5);
2113 y_offset=(ssize_t) ceil((double) image->rows+((fabs(shear.y)*y_width)-
2114 image->rows)/2.0-0.5);
2116 Surround image with border.
2118 integral_image->border_color=integral_image->background_color;
2119 integral_image->compose=CopyCompositeOp;
2120 border_info.width=(size_t) x_offset;
2121 border_info.height=(size_t) y_offset;
2122 shear_image=BorderImage(integral_image,&border_info,image->compose,exception);
2123 integral_image=DestroyImage(integral_image);
2124 if (shear_image == (Image *) NULL)
2125 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2129 if (shear_image->matte == MagickFalse)
2130 (void) SetImageAlphaChannel(shear_image,OpaqueAlphaChannel,exception);
2131 status=XShearImage(shear_image,shear.x,image->columns,image->rows,x_offset,
2132 (ssize_t) (shear_image->rows-image->rows)/2,exception);
2133 if (status == MagickFalse)
2135 shear_image=DestroyImage(shear_image);
2136 return((Image *) NULL);
2138 status=YShearImage(shear_image,shear.y,y_width,image->rows,(ssize_t)
2139 (shear_image->columns-y_width)/2,y_offset,exception);
2140 if (status == MagickFalse)
2142 shear_image=DestroyImage(shear_image);
2143 return((Image *) NULL);
2145 status=CropToFitImage(&shear_image,shear.x,shear.y,(MagickRealType)
2146 image->columns,(MagickRealType) image->rows,MagickFalse,exception);
2147 if (status == MagickFalse)
2149 shear_image=DestroyImage(shear_image);
2150 return((Image *) NULL);
2152 shear_image->compose=image->compose;
2153 shear_image->page.width=0;
2154 shear_image->page.height=0;
2155 return(shear_image);