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-2014 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 XShearImage() and YShearImage() methods are based on the paper "A Fast
37 % Algorithm for General Raster Rotatation" by Alan W. Paeth, Graphics
38 % Interface '86 (Vancouver). ShearRotateImage() 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.
47 #include "MagickCore/studio.h"
48 #include "MagickCore/artifact.h"
49 #include "MagickCore/attribute.h"
50 #include "MagickCore/blob-private.h"
51 #include "MagickCore/cache-private.h"
52 #include "MagickCore/channel.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 + C r o p T o F i t I m a g e %
91 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
93 % CropToFitImage() crops the sheared image as determined by the bounding box
94 % as defined by width and height and shearing angles.
96 % The format of the CropToFitImage method is:
98 % MagickBooleanType CropToFitImage(Image **image,
99 % const double x_shear,const double x_shear,
100 % const double width,const double height,
101 % const MagickBooleanType rotate,ExceptionInfo *exception)
103 % A description of each parameter follows.
105 % o image: the image.
107 % o x_shear, y_shear, width, height: Defines a region of the image to crop.
109 % o exception: return any errors or warnings in this structure.
112 static MagickBooleanType CropToFitImage(Image **image,
113 const double x_shear,const double y_shear,
114 const double width,const double height,
115 const MagickBooleanType rotate,ExceptionInfo *exception)
133 Calculate the rotated image size.
135 extent[0].x=(double) (-width/2.0);
136 extent[0].y=(double) (-height/2.0);
137 extent[1].x=(double) width/2.0;
138 extent[1].y=(double) (-height/2.0);
139 extent[2].x=(double) (-width/2.0);
140 extent[2].y=(double) height/2.0;
141 extent[3].x=(double) width/2.0;
142 extent[3].y=(double) height/2.0;
143 for (i=0; i < 4; i++)
145 extent[i].x+=x_shear*extent[i].y;
146 extent[i].y+=y_shear*extent[i].x;
147 if (rotate != MagickFalse)
148 extent[i].x+=x_shear*extent[i].y;
149 extent[i].x+=(double) (*image)->columns/2.0;
150 extent[i].y+=(double) (*image)->rows/2.0;
154 for (i=1; i < 4; i++)
156 if (min.x > extent[i].x)
158 if (min.y > extent[i].y)
160 if (max.x < extent[i].x)
162 if (max.y < extent[i].y)
165 geometry.x=(ssize_t) ceil(min.x-0.5);
166 geometry.y=(ssize_t) ceil(min.y-0.5);
167 geometry.width=(size_t) floor(max.x-min.x+0.5);
168 geometry.height=(size_t) floor(max.y-min.y+0.5);
170 (void) ParseAbsoluteGeometry("0x0+0+0",&(*image)->page);
171 crop_image=CropImage(*image,&geometry,exception);
172 if (crop_image == (Image *) NULL)
174 crop_image->page=page;
175 *image=DestroyImage(*image);
181 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
185 % D e s k e w I m a g e %
189 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
191 % DeskewImage() removes skew from the image. Skew is an artifact that
192 % occurs in scanned images because of the camera being misaligned,
193 % imperfections in the scanning or surface, or simply because the paper was
194 % not placed completely flat when scanned.
196 % The result will be auto-croped if the artifact "deskew:auto-crop" is
197 % defined, while the amount the image is to be deskewed, in degrees is also
198 % saved as the artifact "deskew:angle".
200 % If the artifact "deskew:auto-crop" is given the image will be automatically
201 % cropped of the excess background. The value is the border width of all
202 % pixels around the edge that will be used to determine an average border
203 % color for the automatic trim.
205 % The format of the DeskewImage method is:
207 % Image *DeskewImage(const Image *image,const double threshold,
208 % ExceptionInfo *exception)
210 % A description of each parameter follows:
212 % o image: the image.
214 % o threshold: separate background from foreground.
216 % o exception: return any errors or warnings in this structure.
220 typedef struct _RadonInfo
245 static RadonInfo *DestroyRadonInfo(RadonInfo *radon_info)
247 assert(radon_info != (RadonInfo *) NULL);
248 switch (radon_info->type)
252 if (radon_info->mapped == MagickFalse)
253 radon_info->cells=(unsigned short *) RelinquishMagickMemory(
257 (void) UnmapBlob(radon_info->cells,(size_t) radon_info->length);
258 radon_info->cells=(unsigned short *) NULL;
260 RelinquishMagickResource(MemoryResource,radon_info->length);
265 (void) UnmapBlob(radon_info->cells,(size_t) radon_info->length);
266 radon_info->cells=(unsigned short *) NULL;
267 RelinquishMagickResource(MapResource,radon_info->length);
271 if (radon_info->file != -1)
272 (void) close(radon_info->file);
273 (void) RelinquishUniqueFileResource(radon_info->path);
274 RelinquishMagickResource(DiskResource,radon_info->length);
280 return((RadonInfo *) RelinquishMagickMemory(radon_info));
283 static MagickBooleanType ResetRadonCells(RadonInfo *radon_info)
295 if (radon_info->type != DiskCache)
297 (void) ResetMagickMemory(radon_info->cells,0,(size_t) radon_info->length);
301 (void) lseek(radon_info->file,0,SEEK_SET);
302 for (y=0; y < (ssize_t) radon_info->height; y++)
304 for (x=0; x < (ssize_t) radon_info->width; x++)
306 count=write(radon_info->file,&value,sizeof(*radon_info->cells));
307 if (count != (ssize_t) sizeof(*radon_info->cells))
310 if (x < (ssize_t) radon_info->width)
313 return(y < (ssize_t) radon_info->height ? MagickFalse : MagickTrue);
316 static RadonInfo *AcquireRadonInfo(const Image *image,const size_t width,
317 const size_t height,ExceptionInfo *exception)
325 radon_info=(RadonInfo *) AcquireMagickMemory(sizeof(*radon_info));
326 if (radon_info == (RadonInfo *) NULL)
327 return((RadonInfo *) NULL);
328 (void) ResetMagickMemory(radon_info,0,sizeof(*radon_info));
329 radon_info->width=width;
330 radon_info->height=height;
331 radon_info->length=(MagickSizeType) width*height*sizeof(*radon_info->cells);
332 radon_info->type=MemoryCache;
333 status=AcquireMagickResource(AreaResource,radon_info->length);
334 if ((status != MagickFalse) &&
335 (radon_info->length == (MagickSizeType) ((size_t) radon_info->length)))
337 status=AcquireMagickResource(MemoryResource,radon_info->length);
338 if (status != MagickFalse)
340 radon_info->mapped=MagickFalse;
341 radon_info->cells=(unsigned short *) AcquireMagickMemory((size_t)
343 if (radon_info->cells == (unsigned short *) NULL)
345 radon_info->mapped=MagickTrue;
346 radon_info->cells=(unsigned short *) MapBlob(-1,IOMode,0,(size_t)
349 if (radon_info->cells == (unsigned short *) NULL)
350 RelinquishMagickResource(MemoryResource,radon_info->length);
353 radon_info->file=(-1);
354 if (radon_info->cells == (unsigned short *) NULL)
356 status=AcquireMagickResource(DiskResource,radon_info->length);
357 if (status == MagickFalse)
359 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
360 "CacheResourcesExhausted","`%s'",image->filename);
361 return(DestroyRadonInfo(radon_info));
363 radon_info->type=DiskCache;
364 (void) AcquireMagickResource(MemoryResource,radon_info->length);
365 radon_info->file=AcquireUniqueFileResource(radon_info->path);
366 if (radon_info->file == -1)
367 return(DestroyRadonInfo(radon_info));
368 status=AcquireMagickResource(MapResource,radon_info->length);
369 if (status != MagickFalse)
371 status=ResetRadonCells(radon_info);
372 if (status != MagickFalse)
374 radon_info->cells=(unsigned short *) MapBlob(radon_info->file,
375 IOMode,0,(size_t) radon_info->length);
376 if (radon_info->cells != (unsigned short *) NULL)
377 radon_info->type=MapCache;
379 RelinquishMagickResource(MapResource,radon_info->length);
386 static inline size_t MagickMin(const size_t x,const size_t y)
393 static inline ssize_t ReadRadonCell(const RadonInfo *radon_info,
394 const MagickOffsetType offset,const size_t length,unsigned char *buffer)
402 #if !defined(MAGICKCORE_HAVE_PPREAD)
403 #if defined(MAGICKCORE_OPENMP_SUPPORT)
404 #pragma omp critical (MagickCore_ReadRadonCell)
408 if (lseek(radon_info->file,offset,SEEK_SET) >= 0)
412 for (i=0; i < (ssize_t) length; i+=count)
414 #if !defined(MAGICKCORE_HAVE_PPREAD)
415 count=read(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
418 count=pread(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
419 SSIZE_MAX),offset+i);
430 #if !defined(MAGICKCORE_HAVE_PPREAD)
437 static inline ssize_t WriteRadonCell(const RadonInfo *radon_info,
438 const MagickOffsetType offset,const size_t length,const unsigned char *buffer)
447 #if !defined(MAGICKCORE_HAVE_PWRITE)
448 #if defined(MAGICKCORE_OPENMP_SUPPORT)
449 #pragma omp critical (MagickCore_WriteRadonCell)
452 if (lseek(radon_info->file,offset,SEEK_SET) >= 0)
456 for (i=0; i < (ssize_t) length; i+=count)
458 #if !defined(MAGICKCORE_HAVE_PWRITE)
459 count=write(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
462 count=pwrite(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
463 SSIZE_MAX),offset+i);
474 #if !defined(MAGICKCORE_HAVE_PWRITE)
481 static inline unsigned short GetRadonCell(const RadonInfo *radon_info,
482 const ssize_t x,const ssize_t y)
490 i=(MagickOffsetType) radon_info->height*x+y;
492 ((MagickSizeType) (i*sizeof(*radon_info->cells)) >= radon_info->length))
494 if (radon_info->type != DiskCache)
495 return(radon_info->cells[i]);
497 (void) ReadRadonCell(radon_info,i*sizeof(*radon_info->cells),
498 sizeof(*radon_info->cells),(unsigned char *) &value);
502 static inline MagickBooleanType SetRadonCell(const RadonInfo *radon_info,
503 const ssize_t x,const ssize_t y,const unsigned short value)
511 i=(MagickOffsetType) radon_info->height*x+y;
513 ((MagickSizeType) (i*sizeof(*radon_info->cells)) >= radon_info->length))
515 if (radon_info->type != DiskCache)
517 radon_info->cells[i]=value;
520 count=WriteRadonCell(radon_info,i*sizeof(*radon_info->cells),
521 sizeof(*radon_info->cells),(const unsigned char *) &value);
522 if (count != (ssize_t) sizeof(*radon_info->cells))
527 static void RadonProjection(const Image *image,RadonInfo *source_cells,
528 RadonInfo *destination_cells,const ssize_t sign,size_t *projection)
543 assert(image != (Image *) NULL);
546 for (step=1; step < p->width; step*=2)
548 for (x=0; x < (ssize_t) p->width; x+=2*(ssize_t) step)
559 for (i=0; i < (ssize_t) step; i++)
561 for (y=0; y < (ssize_t) (p->height-i-1); y++)
563 cell=GetRadonCell(p,x+i,y);
564 (void) SetRadonCell(q,x+2*i,y,cell+GetRadonCell(p,x+i+(ssize_t)
566 (void) SetRadonCell(q,x+2*i+1,y,cell+GetRadonCell(p,x+i+(ssize_t)
569 for ( ; y < (ssize_t) (p->height-i); y++)
571 cell=GetRadonCell(p,x+i,y);
572 (void) SetRadonCell(q,x+2*i,y,cell+GetRadonCell(p,x+i+(ssize_t) step,
574 (void) SetRadonCell(q,x+2*i+1,y,cell);
576 for ( ; y < (ssize_t) p->height; y++)
578 cell=GetRadonCell(p,x+i,y);
579 (void) SetRadonCell(q,x+2*i,y,cell);
580 (void) SetRadonCell(q,x+2*i+1,y,cell);
588 #if defined(MAGICKCORE_OPENMP_SUPPORT)
589 #pragma omp parallel for schedule(static,4) \
590 magick_threads(image,image,1,1)
592 for (x=0; x < (ssize_t) p->width; x++)
601 for (y=0; y < (ssize_t) (p->height-1); y++)
606 delta=GetRadonCell(p,x,y)-(ssize_t) GetRadonCell(p,x,y+1);
609 projection[p->width+sign*x-1]=sum;
613 static MagickBooleanType RadonTransform(const Image *image,
614 const double threshold,size_t *projection,ExceptionInfo *exception)
642 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
643 source_cells=AcquireRadonInfo(image,width,image->rows,exception);
644 destination_cells=AcquireRadonInfo(image,width,image->rows,exception);
645 if ((source_cells == (RadonInfo *) NULL) ||
646 (destination_cells == (RadonInfo *) NULL))
648 if (destination_cells != (RadonInfo *) NULL)
649 destination_cells=DestroyRadonInfo(destination_cells);
650 if (source_cells != (RadonInfo *) NULL)
651 source_cells=DestroyRadonInfo(source_cells);
654 if (ResetRadonCells(source_cells) == MagickFalse)
656 destination_cells=DestroyRadonInfo(destination_cells);
657 source_cells=DestroyRadonInfo(source_cells);
660 for (i=0; i < 256; i++)
662 byte=(unsigned char) i;
663 for (count=0; byte != 0; byte>>=1)
665 bits[i]=(unsigned short) count;
668 image_view=AcquireVirtualCacheView(image,exception);
669 #if defined(MAGICKCORE_OPENMP_SUPPORT)
670 #pragma omp parallel for schedule(static,4) shared(status) \
671 magick_threads(image,image,image->rows,1)
673 for (y=0; y < (ssize_t) image->rows; y++)
675 register const Quantum
686 if (status == MagickFalse)
688 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
689 if (p == (const Quantum *) NULL)
696 i=(ssize_t) (image->columns+7)/8;
697 for (x=0; x < (ssize_t) image->columns; x++)
700 if (GetPixelIntensity(image,p) < threshold)
705 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
709 p+=GetPixelChannels(image);
714 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
717 RadonProjection(image,source_cells,destination_cells,-1,projection);
718 (void) ResetRadonCells(source_cells);
719 #if defined(MAGICKCORE_OPENMP_SUPPORT)
720 #pragma omp parallel for schedule(static,4) shared(status) \
721 magick_threads(image,image,1,1)
723 for (y=0; y < (ssize_t) image->rows; y++)
725 register const Quantum
736 if (status == MagickFalse)
738 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
739 if (p == (const Quantum *) NULL)
747 for (x=0; x < (ssize_t) image->columns; x++)
750 if (GetPixelIntensity(image,p) < threshold)
755 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
759 p+=GetPixelChannels(image);
764 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
767 RadonProjection(image,source_cells,destination_cells,1,projection);
768 image_view=DestroyCacheView(image_view);
769 destination_cells=DestroyRadonInfo(destination_cells);
770 source_cells=DestroyRadonInfo(source_cells);
774 static void GetImageBackgroundColor(Image *image,const ssize_t offset,
775 ExceptionInfo *exception)
790 Compute average background color.
794 GetPixelInfo(image,&background);
796 image_view=AcquireVirtualCacheView(image,exception);
797 for (y=0; y < (ssize_t) image->rows; y++)
799 register const Quantum
805 if ((y >= offset) && (y < ((ssize_t) image->rows-offset)))
807 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
808 if (p == (const Quantum *) NULL)
810 for (x=0; x < (ssize_t) image->columns; x++)
812 if ((x >= offset) && (x < ((ssize_t) image->columns-offset)))
814 background.red+=QuantumScale*GetPixelRed(image,p);
815 background.green+=QuantumScale*GetPixelGreen(image,p);
816 background.blue+=QuantumScale*GetPixelBlue(image,p);
817 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
818 background.alpha+=QuantumScale*GetPixelAlpha(image,p);
820 p+=GetPixelChannels(image);
823 image_view=DestroyCacheView(image_view);
824 image->background_color.red=(double) ClampToQuantum(QuantumRange*
825 background.red/count);
826 image->background_color.green=(double) ClampToQuantum(QuantumRange*
827 background.green/count);
828 image->background_color.blue=(double) ClampToQuantum(QuantumRange*
829 background.blue/count);
830 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
831 image->background_color.alpha=(double) ClampToQuantum(QuantumRange*
832 background.alpha/count);
835 MagickExport Image *DeskewImage(const Image *image,const double threshold,
836 ExceptionInfo *exception)
871 Compute deskew angle.
873 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
874 projection=(size_t *) AcquireQuantumMemory((size_t) (2*width-1),
875 sizeof(*projection));
876 if (projection == (size_t *) NULL)
877 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
878 status=RadonTransform(image,threshold,projection,exception);
879 if (status == MagickFalse)
881 projection=(size_t *) RelinquishMagickMemory(projection);
882 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
886 for (i=0; i < (ssize_t) (2*width-1); i++)
888 if (projection[i] > max_projection)
890 skew=i-(ssize_t) width+1;
891 max_projection=projection[i];
894 projection=(size_t *) RelinquishMagickMemory(projection);
895 degrees=RadiansToDegrees(-atan((double) skew/width/8));
896 if (image->debug != MagickFalse)
897 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
898 " Deskew angle: %g",degrees);
902 clone_image=CloneImage(image,0,0,MagickTrue,exception);
903 if (clone_image == (Image *) NULL)
904 return((Image *) NULL);
907 angle[MaxTextExtent];
909 (void) FormatLocaleString(angle,MaxTextExtent,"%.20g",degrees);
910 (void) SetImageArtifact(clone_image,"deskew:angle",angle);
912 (void) SetImageVirtualPixelMethod(clone_image,BackgroundVirtualPixelMethod,
914 affine_matrix.sx=cos(DegreesToRadians(fmod((double) degrees,360.0)));
915 affine_matrix.rx=sin(DegreesToRadians(fmod((double) degrees,360.0)));
916 affine_matrix.ry=(-sin(DegreesToRadians(fmod((double) degrees,360.0))));
917 affine_matrix.sy=cos(DegreesToRadians(fmod((double) degrees,360.0)));
918 affine_matrix.tx=0.0;
919 affine_matrix.ty=0.0;
920 artifact=GetImageArtifact(image,"deskew:auto-crop");
921 if (artifact == (const char *) NULL)
923 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
924 clone_image=DestroyImage(clone_image);
925 return(deskew_image);
930 GetImageBackgroundColor(clone_image,(ssize_t) StringToLong(artifact),
932 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
933 clone_image=DestroyImage(clone_image);
934 if (deskew_image == (Image *) NULL)
935 return((Image *) NULL);
936 median_image=StatisticImage(deskew_image,MedianStatistic,3,3,exception);
937 if (median_image == (Image *) NULL)
939 deskew_image=DestroyImage(deskew_image);
940 return((Image *) NULL);
942 geometry=GetImageBoundingBox(median_image,exception);
943 median_image=DestroyImage(median_image);
944 if (image->debug != MagickFalse)
945 (void) LogMagickEvent(TransformEvent,GetMagickModule()," Deskew geometry: "
946 "%.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
947 geometry.height,(double) geometry.x,(double) geometry.y);
948 crop_image=CropImage(deskew_image,&geometry,exception);
949 deskew_image=DestroyImage(deskew_image);
954 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
958 % I n t e g r a l R o t a t e I m a g e %
962 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
964 % IntegralRotateImage() rotates the image an integral of 90 degrees. It
965 % allocates the memory necessary for the new Image structure and returns a
966 % pointer to the rotated image.
968 % The format of the IntegralRotateImage method is:
970 % Image *IntegralRotateImage(const Image *image,size_t rotations,
971 % ExceptionInfo *exception)
973 % A description of each parameter follows.
975 % o image: the image.
977 % o rotations: Specifies the number of 90 degree rotations.
980 MagickExport Image *IntegralRotateImage(const Image *image,size_t rotations,
981 ExceptionInfo *exception)
983 #define RotateImageTag "Rotate/Image"
1005 Initialize rotated image attributes.
1007 assert(image != (Image *) NULL);
1011 return(CloneImage(image,0,0,MagickTrue,exception));
1012 if ((rotations == 1) || (rotations == 3))
1013 rotate_image=CloneImage(image,image->rows,image->columns,MagickTrue,
1016 rotate_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1018 if (rotate_image == (Image *) NULL)
1019 return((Image *) NULL);
1021 Integral rotate the image.
1025 image_view=AcquireVirtualCacheView(image,exception);
1026 rotate_view=AcquireAuthenticCacheView(rotate_image,exception);
1048 GetPixelCacheTileSize(image,&tile_width,&tile_height);
1049 tile_width=image->columns;
1050 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1051 #pragma omp parallel for schedule(static,4) shared(status) \
1052 magick_threads(image,image,1,1)
1054 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
1059 if (status == MagickFalse)
1062 for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
1067 register const Quantum
1081 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
1082 width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
1084 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
1085 height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
1086 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1088 if (p == (const Quantum *) NULL)
1093 for (y=0; y < (ssize_t) width; y++)
1095 register const Quantum
1096 *restrict tile_pixels;
1101 if (status == MagickFalse)
1103 q=QueueCacheViewAuthenticPixels(rotate_view,(ssize_t)
1104 (rotate_image->columns-(tile_y+height)),y+tile_x,height,1,
1106 if (q == (Quantum *) NULL)
1111 tile_pixels=p+((height-1)*width+y)*GetPixelChannels(image);
1112 for (x=0; x < (ssize_t) height; x++)
1117 if (GetPixelReadMask(image,tile_pixels) == 0)
1119 tile_pixels-=width*GetPixelChannels(image);
1120 q+=GetPixelChannels(rotate_image);
1123 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1125 PixelChannel channel=GetPixelChannelChannel(image,i);
1126 PixelTrait traits=GetPixelChannelTraits(image,channel);
1127 PixelTrait rotate_traits=GetPixelChannelTraits(rotate_image,
1129 if ((traits == UndefinedPixelTrait) ||
1130 (rotate_traits == UndefinedPixelTrait))
1132 SetPixelChannel(rotate_image,channel,tile_pixels[i],q);
1134 tile_pixels-=width*GetPixelChannels(image);
1135 q+=GetPixelChannels(rotate_image);
1137 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1138 if (sync == MagickFalse)
1142 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1147 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1148 #pragma omp critical (MagickCore_IntegralRotateImage)
1150 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
1152 if (proceed == MagickFalse)
1156 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1157 image->rows-1,image->rows);
1158 Swap(page.width,page.height);
1159 Swap(page.x,page.y);
1160 if (page.width != 0)
1161 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1169 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1170 #pragma omp parallel for schedule(static,4) shared(status) \
1171 magick_threads(image,image,1,1)
1173 for (y=0; y < (ssize_t) image->rows; y++)
1178 register const Quantum
1187 if (status == MagickFalse)
1189 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1190 q=QueueCacheViewAuthenticPixels(rotate_view,0,(ssize_t) (image->rows-y-
1191 1),image->columns,1,exception);
1192 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1197 q+=GetPixelChannels(rotate_image)*image->columns;
1198 for (x=0; x < (ssize_t) image->columns; x++)
1203 q-=GetPixelChannels(rotate_image);
1204 if (GetPixelReadMask(image,p) == 0)
1206 p+=GetPixelChannels(image);
1209 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1211 PixelChannel channel=GetPixelChannelChannel(image,i);
1212 PixelTrait traits=GetPixelChannelTraits(image,channel);
1213 PixelTrait rotate_traits=GetPixelChannelTraits(rotate_image,
1215 if ((traits == UndefinedPixelTrait) ||
1216 (rotate_traits == UndefinedPixelTrait))
1218 SetPixelChannel(rotate_image,channel,p[i],q);
1220 p+=GetPixelChannels(image);
1222 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1223 if (sync == MagickFalse)
1225 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1230 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1231 #pragma omp critical (MagickCore_IntegralRotateImage)
1233 proceed=SetImageProgress(image,RotateImageTag,progress++,
1235 if (proceed == MagickFalse)
1239 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1240 image->rows-1,image->rows);
1241 Swap(page.width,page.height);
1242 Swap(page.x,page.y);
1243 if (page.width != 0)
1244 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1259 GetPixelCacheTileSize(image,&tile_width,&tile_height);
1260 tile_width=image->columns;
1261 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1262 #pragma omp parallel for schedule(static,4) shared(status) \
1263 magick_threads(image,image,1,1)
1265 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
1270 if (status == MagickFalse)
1273 for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
1278 register const Quantum
1292 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
1293 width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
1295 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
1296 height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
1297 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1299 if (p == (const Quantum *) NULL)
1304 for (y=0; y < (ssize_t) width; y++)
1306 register const Quantum
1307 *restrict tile_pixels;
1312 if (status == MagickFalse)
1314 q=QueueCacheViewAuthenticPixels(rotate_view,tile_y,(ssize_t) (y+
1315 rotate_image->rows-(tile_x+width)),height,1,exception);
1316 if (q == (Quantum *) NULL)
1321 tile_pixels=p+((width-1)-y)*GetPixelChannels(image);
1322 for (x=0; x < (ssize_t) height; x++)
1327 if (GetPixelReadMask(image,tile_pixels) == 0)
1329 tile_pixels+=width*GetPixelChannels(image);
1330 q+=GetPixelChannels(rotate_image);
1333 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1335 PixelChannel channel=GetPixelChannelChannel(image,i);
1336 PixelTrait traits=GetPixelChannelTraits(image,channel);
1337 PixelTrait rotate_traits=GetPixelChannelTraits(rotate_image,
1339 if ((traits == UndefinedPixelTrait) ||
1340 (rotate_traits == UndefinedPixelTrait))
1342 SetPixelChannel(rotate_image,channel,tile_pixels[i],q);
1344 tile_pixels+=width*GetPixelChannels(image);
1345 q+=GetPixelChannels(rotate_image);
1347 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1348 #pragma omp critical (MagickCore_IntegralRotateImage)
1350 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1351 if (sync == MagickFalse)
1355 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1360 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
1362 if (proceed == MagickFalse)
1366 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1367 image->rows-1,image->rows);
1368 Swap(page.width,page.height);
1369 Swap(page.x,page.y);
1370 if (page.width != 0)
1371 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1375 rotate_view=DestroyCacheView(rotate_view);
1376 image_view=DestroyCacheView(image_view);
1377 rotate_image->type=image->type;
1378 rotate_image->page=page;
1379 if (status == MagickFalse)
1380 rotate_image=DestroyImage(rotate_image);
1381 return(rotate_image);
1385 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1389 + X S h e a r I m a g e %
1393 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1395 % XShearImage() shears the image in the X direction with a shear angle of
1396 % 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1397 % negative angles shear clockwise. Angles are measured relative to a vertical
1398 % Y-axis. X shears will widen an image creating 'empty' triangles on the left
1399 % and right sides of the source image.
1401 % The format of the XShearImage method is:
1403 % MagickBooleanType XShearImage(Image *image,const double degrees,
1404 % const size_t width,const size_t height,
1405 % const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
1407 % A description of each parameter follows.
1409 % o image: the image.
1411 % o degrees: A double representing the shearing angle along the X
1414 % o width, height, x_offset, y_offset: Defines a region of the image
1417 % o exception: return any errors or warnings in this structure.
1420 static MagickBooleanType XShearImage(Image *image,const double degrees,
1421 const size_t width,const size_t height,const ssize_t x_offset,
1422 const ssize_t y_offset,ExceptionInfo *exception)
1424 #define XShearImageTag "XShear/Image"
1450 assert(image != (Image *) NULL);
1451 assert(image->signature == MagickSignature);
1452 if (image->debug != MagickFalse)
1453 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1455 background=image->background_color;
1457 image_view=AcquireAuthenticCacheView(image,exception);
1458 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1459 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1460 magick_threads(image,image,height,1)
1462 for (y=0; y < (ssize_t) height; y++)
1486 if (status == MagickFalse)
1488 p=GetCacheViewAuthenticPixels(image_view,0,y_offset+y,image->columns,1,
1490 if (p == (Quantum *) NULL)
1495 p+=x_offset*GetPixelChannels(image);
1496 displacement=degrees*(double) (y-height/2.0);
1497 if (displacement == 0.0)
1499 if (displacement > 0.0)
1503 displacement*=(-1.0);
1506 step=(ssize_t) floor((double) displacement);
1507 area=(double) (displacement-step);
1510 GetPixelInfo(image,&source);
1511 GetPixelInfo(image,&destination);
1517 Transfer pixels left-to-right.
1519 if (step > x_offset)
1521 q=p-step*GetPixelChannels(image);
1522 for (i=0; i < (ssize_t) width; i++)
1524 if ((x_offset+i) < step)
1526 p+=GetPixelChannels(image);
1527 GetPixelInfoPixel(image,p,&pixel);
1528 q+=GetPixelChannels(image);
1531 GetPixelInfoPixel(image,p,&source);
1532 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1533 &source,(double) GetPixelAlpha(image,p),area,&destination);
1534 SetPixelInfoPixel(image,&destination,q);
1535 GetPixelInfoPixel(image,p,&pixel);
1536 p+=GetPixelChannels(image);
1537 q+=GetPixelChannels(image);
1539 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1540 &background,(double) background.alpha,area,&destination);
1541 SetPixelInfoPixel(image,&destination,q);
1542 q+=GetPixelChannels(image);
1543 for (i=0; i < (step-1); i++)
1545 SetPixelInfoPixel(image,&background,q);
1546 q+=GetPixelChannels(image);
1553 Transfer pixels right-to-left.
1555 p+=width*GetPixelChannels(image);
1556 q=p+step*GetPixelChannels(image);
1557 for (i=0; i < (ssize_t) width; i++)
1559 p-=GetPixelChannels(image);
1560 q-=GetPixelChannels(image);
1561 if ((size_t) (x_offset+width+step-i) >= image->columns)
1563 GetPixelInfoPixel(image,p,&source);
1564 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1565 &source,(double) GetPixelAlpha(image,p),area,&destination);
1566 SetPixelInfoPixel(image,&destination,q);
1567 GetPixelInfoPixel(image,p,&pixel);
1569 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1570 &background,(double) background.alpha,area,&destination);
1571 q-=GetPixelChannels(image);
1572 SetPixelInfoPixel(image,&destination,q);
1573 for (i=0; i < (step-1); i++)
1575 q-=GetPixelChannels(image);
1576 SetPixelInfoPixel(image,&background,q);
1581 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1583 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1588 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1589 #pragma omp critical (MagickCore_XShearImage)
1591 proceed=SetImageProgress(image,XShearImageTag,progress++,height);
1592 if (proceed == MagickFalse)
1596 image_view=DestroyCacheView(image_view);
1601 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1605 + Y S h e a r I m a g e %
1609 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1611 % YShearImage shears the image in the Y direction with a shear angle of
1612 % 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1613 % negative angles shear clockwise. Angles are measured relative to a
1614 % horizontal X-axis. Y shears will increase the height of an image creating
1615 % 'empty' triangles on the top and bottom of the source image.
1617 % The format of the YShearImage method is:
1619 % MagickBooleanType YShearImage(Image *image,const double degrees,
1620 % const size_t width,const size_t height,
1621 % const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
1623 % A description of each parameter follows.
1625 % o image: the image.
1627 % o degrees: A double representing the shearing angle along the Y
1630 % o width, height, x_offset, y_offset: Defines a region of the image
1633 % o exception: return any errors or warnings in this structure.
1636 static MagickBooleanType YShearImage(Image *image,const double degrees,
1637 const size_t width,const size_t height,const ssize_t x_offset,
1638 const ssize_t y_offset,ExceptionInfo *exception)
1640 #define YShearImageTag "YShear/Image"
1666 assert(image != (Image *) NULL);
1667 assert(image->signature == MagickSignature);
1668 if (image->debug != MagickFalse)
1669 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1672 background=image->background_color;
1673 image_view=AcquireAuthenticCacheView(image,exception);
1674 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1675 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1676 magick_threads(image,image,width,1)
1678 for (x=0; x < (ssize_t) width; x++)
1702 if (status == MagickFalse)
1704 p=GetCacheViewAuthenticPixels(image_view,x_offset+x,0,1,image->rows,
1706 if (p == (Quantum *) NULL)
1711 p+=y_offset*GetPixelChannels(image);
1712 displacement=degrees*(double) (x-width/2.0);
1713 if (displacement == 0.0)
1715 if (displacement > 0.0)
1719 displacement*=(-1.0);
1722 step=(ssize_t) floor((double) displacement);
1723 area=(double) (displacement-step);
1726 GetPixelInfo(image,&source);
1727 GetPixelInfo(image,&destination);
1733 Transfer pixels top-to-bottom.
1735 if (step > y_offset)
1737 q=p-step*GetPixelChannels(image);
1738 for (i=0; i < (ssize_t) height; i++)
1740 if ((y_offset+i) < step)
1742 p+=GetPixelChannels(image);
1743 GetPixelInfoPixel(image,p,&pixel);
1744 q+=GetPixelChannels(image);
1747 GetPixelInfoPixel(image,p,&source);
1748 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1749 &source,(double) GetPixelAlpha(image,p),area,
1751 SetPixelInfoPixel(image,&destination,q);
1752 GetPixelInfoPixel(image,p,&pixel);
1753 p+=GetPixelChannels(image);
1754 q+=GetPixelChannels(image);
1756 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1757 &background,(double) background.alpha,area,&destination);
1758 SetPixelInfoPixel(image,&destination,q);
1759 q+=GetPixelChannels(image);
1760 for (i=0; i < (step-1); i++)
1762 SetPixelInfoPixel(image,&background,q);
1763 q+=GetPixelChannels(image);
1770 Transfer pixels bottom-to-top.
1772 p+=height*GetPixelChannels(image);
1773 q=p+step*GetPixelChannels(image);
1774 for (i=0; i < (ssize_t) height; i++)
1776 p-=GetPixelChannels(image);
1777 q-=GetPixelChannels(image);
1778 if ((size_t) (y_offset+height+step-i) >= image->rows)
1780 GetPixelInfoPixel(image,p,&source);
1781 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1782 &source,(double) GetPixelAlpha(image,p),area,
1784 SetPixelInfoPixel(image,&destination,q);
1785 GetPixelInfoPixel(image,p,&pixel);
1787 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1788 &background,(double) background.alpha,area,&destination);
1789 q-=GetPixelChannels(image);
1790 SetPixelInfoPixel(image,&destination,q);
1791 for (i=0; i < (step-1); i++)
1793 q-=GetPixelChannels(image);
1794 SetPixelInfoPixel(image,&background,q);
1799 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1801 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1806 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1807 #pragma omp critical (MagickCore_YShearImage)
1809 proceed=SetImageProgress(image,YShearImageTag,progress++,image->rows);
1810 if (proceed == MagickFalse)
1814 image_view=DestroyCacheView(image_view);
1819 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1823 % S h e a r I m a g e %
1827 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1829 % ShearImage() creates a new image that is a shear_image copy of an existing
1830 % one. Shearing slides one edge of an image along the X or Y axis, creating
1831 % a parallelogram. An X direction shear slides an edge along the X axis,
1832 % while a Y direction shear slides an edge along the Y axis. The amount of
1833 % the shear is controlled by a shear angle. For X direction shears, x_shear
1834 % is measured relative to the Y axis, and similarly, for Y direction shears
1835 % y_shear is measured relative to the X axis. Empty triangles left over from
1836 % shearing the image are filled with the background color defined by member
1837 % 'background_color' of the image.. ShearImage() allocates the memory
1838 % necessary for the new Image structure and returns a pointer to the new image.
1840 % ShearImage() is based on the paper "A Fast Algorithm for General Raster
1841 % Rotatation" by Alan W. Paeth.
1843 % The format of the ShearImage method is:
1845 % Image *ShearImage(const Image *image,const double x_shear,
1846 % const double y_shear,ExceptionInfo *exception)
1848 % A description of each parameter follows.
1850 % o image: the image.
1852 % o x_shear, y_shear: Specifies the number of degrees to shear the image.
1854 % o exception: return any errors or warnings in this structure.
1857 MagickExport Image *ShearImage(const Image *image,const double x_shear,
1858 const double y_shear,ExceptionInfo *exception)
1880 assert(image != (Image *) NULL);
1881 assert(image->signature == MagickSignature);
1882 if (image->debug != MagickFalse)
1883 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1884 assert(exception != (ExceptionInfo *) NULL);
1885 assert(exception->signature == MagickSignature);
1886 if ((x_shear != 0.0) && (fmod(x_shear,90.0) == 0.0))
1887 ThrowImageException(ImageError,"AngleIsDiscontinuous");
1888 if ((y_shear != 0.0) && (fmod(y_shear,90.0) == 0.0))
1889 ThrowImageException(ImageError,"AngleIsDiscontinuous");
1891 Initialize shear angle.
1893 integral_image=CloneImage(image,0,0,MagickTrue,exception);
1894 if (integral_image == (Image *) NULL)
1895 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1896 shear.x=(-tan(DegreesToRadians(fmod(x_shear,360.0))));
1897 shear.y=tan(DegreesToRadians(fmod(y_shear,360.0)));
1898 if ((shear.x == 0.0) && (shear.y == 0.0))
1899 return(integral_image);
1900 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
1902 integral_image=DestroyImage(integral_image);
1903 return(integral_image);
1905 if (integral_image->alpha_trait != BlendPixelTrait)
1906 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
1910 y_width=image->columns+(ssize_t) floor(fabs(shear.x)*image->rows+0.5);
1911 x_offset=(ssize_t) ceil((double) image->columns+((fabs(shear.x)*image->rows)-
1912 image->columns)/2.0-0.5);
1913 y_offset=(ssize_t) ceil((double) image->rows+((fabs(shear.y)*y_width)-
1914 image->rows)/2.0-0.5);
1916 Surround image with border.
1918 integral_image->border_color=integral_image->background_color;
1919 integral_image->compose=CopyCompositeOp;
1920 border_info.width=(size_t) x_offset;
1921 border_info.height=(size_t) y_offset;
1922 shear_image=BorderImage(integral_image,&border_info,image->compose,exception);
1923 integral_image=DestroyImage(integral_image);
1924 if (shear_image == (Image *) NULL)
1925 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1929 if (shear_image->alpha_trait != BlendPixelTrait)
1930 (void) SetImageAlphaChannel(shear_image,OpaqueAlphaChannel,exception);
1931 status=XShearImage(shear_image,shear.x,image->columns,image->rows,x_offset,
1932 (ssize_t) (shear_image->rows-image->rows)/2,exception);
1933 if (status == MagickFalse)
1935 shear_image=DestroyImage(shear_image);
1936 return((Image *) NULL);
1938 status=YShearImage(shear_image,shear.y,y_width,image->rows,(ssize_t)
1939 (shear_image->columns-y_width)/2,y_offset,exception);
1940 if (status == MagickFalse)
1942 shear_image=DestroyImage(shear_image);
1943 return((Image *) NULL);
1945 status=CropToFitImage(&shear_image,shear.x,shear.y,(double)
1946 image->columns,(double) image->rows,MagickFalse,exception);
1947 shear_image->compose=image->compose;
1948 shear_image->page.width=0;
1949 shear_image->page.height=0;
1950 if (status == MagickFalse)
1951 shear_image=DestroyImage(shear_image);
1952 return(shear_image);
1956 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1960 % S h e a r R o t a t e I m a g e %
1964 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1966 % ShearRotateImage() creates a new image that is a rotated copy of an existing
1967 % one. Positive angles rotate counter-clockwise (right-hand rule), while
1968 % negative angles rotate clockwise. Rotated images are usually larger than
1969 % the originals and have 'empty' triangular corners. X axis. Empty
1970 % triangles left over from shearing the image are filled with the background
1971 % color defined by member 'background_color' of the image. ShearRotateImage
1972 % allocates the memory necessary for the new Image structure and returns a
1973 % pointer to the new image.
1975 % ShearRotateImage() is based on the paper "A Fast Algorithm for General
1976 % Raster Rotatation" by Alan W. Paeth. ShearRotateImage is adapted from a
1977 % similar method based on the Paeth paper written by Michael Halle of the
1978 % Spatial Imaging Group, MIT Media Lab.
1980 % The format of the ShearRotateImage method is:
1982 % Image *ShearRotateImage(const Image *image,const double degrees,
1983 % ExceptionInfo *exception)
1985 % A description of each parameter follows.
1987 % o image: the image.
1989 % o degrees: Specifies the number of degrees to rotate the image.
1991 % o exception: return any errors or warnings in this structure.
1994 MagickExport Image *ShearRotateImage(const Image *image,const double degrees,
1995 ExceptionInfo *exception)
2024 Adjust rotation angle.
2026 assert(image != (Image *) NULL);
2027 assert(image->signature == MagickSignature);
2028 if (image->debug != MagickFalse)
2029 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2030 assert(exception != (ExceptionInfo *) NULL);
2031 assert(exception->signature == MagickSignature);
2033 while (angle < -45.0)
2035 for (rotations=0; angle > 45.0; rotations++)
2039 Calculate shear equations.
2041 integral_image=IntegralRotateImage(image,rotations,exception);
2042 if (integral_image == (Image *) NULL)
2043 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2044 shear.x=(-tan((double) DegreesToRadians(angle)/2.0));
2045 shear.y=sin((double) DegreesToRadians(angle));
2046 if ((shear.x == 0.0) && (shear.y == 0.0))
2047 return(integral_image);
2048 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
2050 integral_image=DestroyImage(integral_image);
2051 return(integral_image);
2053 if (integral_image->alpha_trait != BlendPixelTrait)
2054 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
2058 width=image->columns;
2060 if ((rotations == 1) || (rotations == 3))
2063 height=image->columns;
2065 y_width=width+(ssize_t) floor(fabs(shear.x)*height+0.5);
2066 x_offset=(ssize_t) ceil((double) width+((fabs(shear.y)*height)-width)/2.0-
2068 y_offset=(ssize_t) ceil((double) height+((fabs(shear.y)*y_width)-height)/2.0-
2071 Surround image with a border.
2073 integral_image->border_color=integral_image->background_color;
2074 integral_image->compose=CopyCompositeOp;
2075 border_info.width=(size_t) x_offset;
2076 border_info.height=(size_t) y_offset;
2077 rotate_image=BorderImage(integral_image,&border_info,image->compose,
2079 integral_image=DestroyImage(integral_image);
2080 if (rotate_image == (Image *) NULL)
2081 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2085 status=XShearImage(rotate_image,shear.x,width,height,x_offset,(ssize_t)
2086 (rotate_image->rows-height)/2,exception);
2087 if (status == MagickFalse)
2089 rotate_image=DestroyImage(rotate_image);
2090 return((Image *) NULL);
2092 status=YShearImage(rotate_image,shear.y,y_width,height,(ssize_t)
2093 (rotate_image->columns-y_width)/2,y_offset,exception);
2094 if (status == MagickFalse)
2096 rotate_image=DestroyImage(rotate_image);
2097 return((Image *) NULL);
2099 status=XShearImage(rotate_image,shear.x,y_width,rotate_image->rows,(ssize_t)
2100 (rotate_image->columns-y_width)/2,0,exception);
2101 if (status == MagickFalse)
2103 rotate_image=DestroyImage(rotate_image);
2104 return((Image *) NULL);
2106 status=CropToFitImage(&rotate_image,shear.x,shear.y,(double) width,
2107 (double) height,MagickTrue,exception);
2108 rotate_image->compose=image->compose;
2109 rotate_image->page.width=0;
2110 rotate_image->page.height=0;
2111 if (status == MagickFalse)
2112 rotate_image=DestroyImage(rotate_image);
2113 return(rotate_image);