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-2013 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/color-private.h"
53 #include "MagickCore/colorspace-private.h"
54 #include "MagickCore/composite.h"
55 #include "MagickCore/composite-private.h"
56 #include "MagickCore/decorate.h"
57 #include "MagickCore/distort.h"
58 #include "MagickCore/draw.h"
59 #include "MagickCore/exception.h"
60 #include "MagickCore/exception-private.h"
61 #include "MagickCore/gem.h"
62 #include "MagickCore/geometry.h"
63 #include "MagickCore/image.h"
64 #include "MagickCore/image-private.h"
65 #include "MagickCore/memory_.h"
66 #include "MagickCore/list.h"
67 #include "MagickCore/monitor.h"
68 #include "MagickCore/monitor-private.h"
69 #include "MagickCore/nt-base-private.h"
70 #include "MagickCore/pixel-accessor.h"
71 #include "MagickCore/quantum.h"
72 #include "MagickCore/resource_.h"
73 #include "MagickCore/shear.h"
74 #include "MagickCore/statistic.h"
75 #include "MagickCore/string_.h"
76 #include "MagickCore/string-private.h"
77 #include "MagickCore/thread-private.h"
78 #include "MagickCore/threshold.h"
79 #include "MagickCore/transform.h"
82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
86 + C r o p T o F i t I m a g e %
90 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
92 % CropToFitImage() crops the sheared image as determined by the bounding box
93 % as defined by width and height and shearing angles.
95 % The format of the CropToFitImage method is:
97 % MagickBooleanType CropToFitImage(Image **image,
98 % const double x_shear,const double x_shear,
99 % const double width,const double height,
100 % const MagickBooleanType rotate,ExceptionInfo *exception)
102 % A description of each parameter follows.
104 % o image: the image.
106 % o x_shear, y_shear, width, height: Defines a region of the image to crop.
108 % o exception: return any errors or warnings in this structure.
111 static MagickBooleanType CropToFitImage(Image **image,
112 const double x_shear,const double y_shear,
113 const double width,const double height,
114 const MagickBooleanType rotate,ExceptionInfo *exception)
132 Calculate the rotated image size.
134 extent[0].x=(double) (-width/2.0);
135 extent[0].y=(double) (-height/2.0);
136 extent[1].x=(double) width/2.0;
137 extent[1].y=(double) (-height/2.0);
138 extent[2].x=(double) (-width/2.0);
139 extent[2].y=(double) height/2.0;
140 extent[3].x=(double) width/2.0;
141 extent[3].y=(double) height/2.0;
142 for (i=0; i < 4; i++)
144 extent[i].x+=x_shear*extent[i].y;
145 extent[i].y+=y_shear*extent[i].x;
146 if (rotate != MagickFalse)
147 extent[i].x+=x_shear*extent[i].y;
148 extent[i].x+=(double) (*image)->columns/2.0;
149 extent[i].y+=(double) (*image)->rows/2.0;
153 for (i=1; i < 4; i++)
155 if (min.x > extent[i].x)
157 if (min.y > extent[i].y)
159 if (max.x < extent[i].x)
161 if (max.y < extent[i].y)
164 geometry.x=(ssize_t) ceil(min.x-0.5);
165 geometry.y=(ssize_t) ceil(min.y-0.5);
166 geometry.width=(size_t) floor(max.x-min.x+0.5);
167 geometry.height=(size_t) floor(max.y-min.y+0.5);
169 (void) ParseAbsoluteGeometry("0x0+0+0",&(*image)->page);
170 crop_image=CropImage(*image,&geometry,exception);
171 if (crop_image == (Image *) NULL)
173 crop_image->page=page;
174 *image=DestroyImage(*image);
180 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
184 % D e s k e w I m a g e %
188 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
190 % DeskewImage() removes skew from the image. Skew is an artifact that
191 % occurs in scanned images because of the camera being misaligned,
192 % imperfections in the scanning or surface, or simply because the paper was
193 % not placed completely flat when scanned.
195 % The result will be auto-croped if the artifact "deskew:auto-crop" is
196 % defined, while the amount the image is to be deskewed, in degrees is also
197 % saved as the artifact "deskew:angle".
199 % If the artifact "deskew:auto-crop" is given the image will be automatically
200 % cropped of the excess background. The value is the border width of all
201 % pixels around the edge that will be used to determine an average border
202 % color for the automatic trim.
204 % The format of the DeskewImage method is:
206 % Image *DeskewImage(const Image *image,const double threshold,
207 % ExceptionInfo *exception)
209 % A description of each parameter follows:
211 % o image: the image.
213 % o threshold: separate background from foreground.
215 % o exception: return any errors or warnings in this structure.
219 typedef struct _RadonInfo
244 static RadonInfo *DestroyRadonInfo(RadonInfo *radon_info)
246 assert(radon_info != (RadonInfo *) NULL);
247 switch (radon_info->type)
251 if (radon_info->mapped == MagickFalse)
252 radon_info->cells=(unsigned short *) RelinquishMagickMemory(
255 radon_info->cells=(unsigned short *) UnmapBlob(radon_info->cells,
256 (size_t) radon_info->length);
257 RelinquishMagickResource(MemoryResource,radon_info->length);
262 radon_info->cells=(unsigned short *) UnmapBlob(radon_info->cells,(size_t)
264 RelinquishMagickResource(MapResource,radon_info->length);
268 if (radon_info->file != -1)
269 (void) close(radon_info->file);
270 (void) RelinquishUniqueFileResource(radon_info->path);
271 RelinquishMagickResource(DiskResource,radon_info->length);
277 return((RadonInfo *) RelinquishMagickMemory(radon_info));
280 static MagickBooleanType ResetRadonCells(RadonInfo *radon_info)
292 if (radon_info->type != DiskCache)
294 (void) ResetMagickMemory(radon_info->cells,0,(size_t) radon_info->length);
298 (void) lseek(radon_info->file,0,SEEK_SET);
299 for (y=0; y < (ssize_t) radon_info->height; y++)
301 for (x=0; x < (ssize_t) radon_info->width; x++)
303 count=write(radon_info->file,&value,sizeof(*radon_info->cells));
304 if (count != (ssize_t) sizeof(*radon_info->cells))
307 if (x < (ssize_t) radon_info->width)
310 return(y < (ssize_t) radon_info->height ? MagickFalse : MagickTrue);
313 static RadonInfo *AcquireRadonInfo(const Image *image,const size_t width,
314 const size_t height,ExceptionInfo *exception)
322 radon_info=(RadonInfo *) AcquireMagickMemory(sizeof(*radon_info));
323 if (radon_info == (RadonInfo *) NULL)
324 return((RadonInfo *) NULL);
325 (void) ResetMagickMemory(radon_info,0,sizeof(*radon_info));
326 radon_info->width=width;
327 radon_info->height=height;
328 radon_info->length=(MagickSizeType) width*height*sizeof(*radon_info->cells);
329 radon_info->type=MemoryCache;
330 status=AcquireMagickResource(AreaResource,radon_info->length);
331 if ((status != MagickFalse) &&
332 (radon_info->length == (MagickSizeType) ((size_t) radon_info->length)))
334 status=AcquireMagickResource(MemoryResource,radon_info->length);
335 if (status != MagickFalse)
337 radon_info->mapped=MagickFalse;
338 radon_info->cells=(unsigned short *) AcquireMagickMemory((size_t)
340 if (radon_info->cells == (unsigned short *) NULL)
342 radon_info->mapped=MagickTrue;
343 radon_info->cells=(unsigned short *) MapBlob(-1,IOMode,0,(size_t)
346 if (radon_info->cells == (unsigned short *) NULL)
347 RelinquishMagickResource(MemoryResource,radon_info->length);
350 radon_info->file=(-1);
351 if (radon_info->cells == (unsigned short *) NULL)
353 status=AcquireMagickResource(DiskResource,radon_info->length);
354 if (status == MagickFalse)
356 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
357 "CacheResourcesExhausted","`%s'",image->filename);
358 return(DestroyRadonInfo(radon_info));
360 radon_info->type=DiskCache;
361 (void) AcquireMagickResource(MemoryResource,radon_info->length);
362 radon_info->file=AcquireUniqueFileResource(radon_info->path);
363 if (radon_info->file == -1)
364 return(DestroyRadonInfo(radon_info));
365 status=AcquireMagickResource(MapResource,radon_info->length);
366 if (status != MagickFalse)
368 status=ResetRadonCells(radon_info);
369 if (status != MagickFalse)
371 radon_info->cells=(unsigned short *) MapBlob(radon_info->file,
372 IOMode,0,(size_t) radon_info->length);
373 if (radon_info->cells != (unsigned short *) NULL)
374 radon_info->type=MapCache;
376 RelinquishMagickResource(MapResource,radon_info->length);
383 static inline size_t MagickMin(const size_t x,const size_t y)
390 static inline ssize_t ReadRadonCell(const RadonInfo *radon_info,
391 const MagickOffsetType offset,const size_t length,unsigned char *buffer)
399 #if !defined(MAGICKCORE_HAVE_PPREAD)
400 #if defined(MAGICKCORE_OPENMP_SUPPORT)
401 #pragma omp critical (MagickCore_ReadRadonCell)
405 if (lseek(radon_info->file,offset,SEEK_SET) >= 0)
409 for (i=0; i < (ssize_t) length; i+=count)
411 #if !defined(MAGICKCORE_HAVE_PPREAD)
412 count=read(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
415 count=pread(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
416 SSIZE_MAX),offset+i);
427 #if !defined(MAGICKCORE_HAVE_PPREAD)
434 static inline ssize_t WriteRadonCell(const RadonInfo *radon_info,
435 const MagickOffsetType offset,const size_t length,const unsigned char *buffer)
443 #if !defined(MAGICKCORE_HAVE_PWRITE)
444 #if defined(MAGICKCORE_OPENMP_SUPPORT)
445 #pragma omp critical (MagickCore_WriteRadonCell)
448 if (lseek(radon_info->file,offset,SEEK_SET) >= 0)
452 for (i=0; i < (ssize_t) length; i+=count)
454 #if !defined(MAGICKCORE_HAVE_PWRITE)
455 count=write(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
458 count=pwrite(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
459 SSIZE_MAX),offset+i);
470 #if !defined(MAGICKCORE_HAVE_PWRITE)
477 static inline unsigned short GetRadonCell(const RadonInfo *radon_info,
478 const ssize_t x,const ssize_t y)
486 i=(MagickOffsetType) radon_info->height*x+y;
488 ((MagickSizeType) (i*sizeof(*radon_info->cells)) >= radon_info->length))
490 if (radon_info->type != DiskCache)
491 return(radon_info->cells[i]);
493 (void) ReadRadonCell(radon_info,i*sizeof(*radon_info->cells),
494 sizeof(*radon_info->cells),(unsigned char *) &value);
498 static inline MagickBooleanType SetRadonCell(const RadonInfo *radon_info,
499 const ssize_t x,const ssize_t y,const unsigned short value)
507 i=(MagickOffsetType) radon_info->height*x+y;
509 ((MagickSizeType) (i*sizeof(*radon_info->cells)) >= radon_info->length))
511 if (radon_info->type != DiskCache)
513 radon_info->cells[i]=value;
516 count=WriteRadonCell(radon_info,i*sizeof(*radon_info->cells),
517 sizeof(*radon_info->cells),(const unsigned char *) &value);
518 if (count != (ssize_t) sizeof(*radon_info->cells))
523 static void RadonProjection(const Image *image,RadonInfo *source_cells,
524 RadonInfo *destination_cells,const ssize_t sign,size_t *projection)
541 for (step=1; step < p->width; step*=2)
543 for (x=0; x < (ssize_t) p->width; x+=2*(ssize_t) step)
554 for (i=0; i < (ssize_t) step; i++)
556 for (y=0; y < (ssize_t) (p->height-i-1); y++)
558 cell=GetRadonCell(p,x+i,y);
559 (void) SetRadonCell(q,x+2*i,y,cell+GetRadonCell(p,x+i+(ssize_t)
561 (void) SetRadonCell(q,x+2*i+1,y,cell+GetRadonCell(p,x+i+(ssize_t)
564 for ( ; y < (ssize_t) (p->height-i); y++)
566 cell=GetRadonCell(p,x+i,y);
567 (void) SetRadonCell(q,x+2*i,y,cell+GetRadonCell(p,x+i+(ssize_t) step,
569 (void) SetRadonCell(q,x+2*i+1,y,cell);
571 for ( ; y < (ssize_t) p->height; y++)
573 cell=GetRadonCell(p,x+i,y);
574 (void) SetRadonCell(q,x+2*i,y,cell);
575 (void) SetRadonCell(q,x+2*i+1,y,cell);
583 #if defined(MAGICKCORE_OPENMP_SUPPORT)
584 #pragma omp parallel for schedule(static,4) \
585 magick_threads(image,image,1,1)
587 for (x=0; x < (ssize_t) p->width; x++)
596 for (y=0; y < (ssize_t) (p->height-1); y++)
601 delta=GetRadonCell(p,x,y)-(ssize_t) GetRadonCell(p,x,y+1);
604 projection[p->width+sign*x-1]=sum;
608 static MagickBooleanType RadonTransform(const Image *image,
609 const double threshold,size_t *projection,ExceptionInfo *exception)
637 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
638 source_cells=AcquireRadonInfo(image,width,image->rows,exception);
639 destination_cells=AcquireRadonInfo(image,width,image->rows,exception);
640 if ((source_cells == (RadonInfo *) NULL) ||
641 (destination_cells == (RadonInfo *) NULL))
643 if (destination_cells != (RadonInfo *) NULL)
644 destination_cells=DestroyRadonInfo(destination_cells);
645 if (source_cells != (RadonInfo *) NULL)
646 source_cells=DestroyRadonInfo(source_cells);
649 if (ResetRadonCells(source_cells) == MagickFalse)
651 destination_cells=DestroyRadonInfo(destination_cells);
652 source_cells=DestroyRadonInfo(source_cells);
655 for (i=0; i < 256; i++)
657 byte=(unsigned char) i;
658 for (count=0; byte != 0; byte>>=1)
660 bits[i]=(unsigned short) count;
663 image_view=AcquireVirtualCacheView(image,exception);
664 #if defined(MAGICKCORE_OPENMP_SUPPORT)
665 #pragma omp parallel for schedule(static,4) shared(status) \
666 magick_threads(image,image,image->rows,1)
668 for (y=0; y < (ssize_t) image->rows; y++)
670 register const Quantum
681 if (status == MagickFalse)
683 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
684 if (p == (const Quantum *) NULL)
691 i=(ssize_t) (image->columns+7)/8;
692 for (x=0; x < (ssize_t) image->columns; x++)
695 if (GetPixelIntensity(image,p) < threshold)
700 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
704 p+=GetPixelChannels(image);
709 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
712 RadonProjection(image,source_cells,destination_cells,-1,projection);
713 (void) ResetRadonCells(source_cells);
714 #if defined(MAGICKCORE_OPENMP_SUPPORT)
715 #pragma omp parallel for schedule(static,4) shared(status) \
716 magick_threads(image,image,1,1)
718 for (y=0; y < (ssize_t) image->rows; y++)
720 register const Quantum
731 if (status == MagickFalse)
733 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
734 if (p == (const Quantum *) NULL)
742 for (x=0; x < (ssize_t) image->columns; x++)
745 if (GetPixelIntensity(image,p) < threshold)
750 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
754 p+=GetPixelChannels(image);
759 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
762 RadonProjection(image,source_cells,destination_cells,1,projection);
763 image_view=DestroyCacheView(image_view);
764 destination_cells=DestroyRadonInfo(destination_cells);
765 source_cells=DestroyRadonInfo(source_cells);
769 static void GetImageBackgroundColor(Image *image,const ssize_t offset,
770 ExceptionInfo *exception)
785 Compute average background color.
789 GetPixelInfo(image,&background);
791 image_view=AcquireVirtualCacheView(image,exception);
792 for (y=0; y < (ssize_t) image->rows; y++)
794 register const Quantum
800 if ((y >= offset) && (y < ((ssize_t) image->rows-offset)))
802 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
803 if (p == (const Quantum *) NULL)
805 for (x=0; x < (ssize_t) image->columns; x++)
807 if ((x >= offset) && (x < ((ssize_t) image->columns-offset)))
809 background.red+=QuantumScale*GetPixelRed(image,p);
810 background.green+=QuantumScale*GetPixelGreen(image,p);
811 background.blue+=QuantumScale*GetPixelBlue(image,p);
812 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
813 background.alpha+=QuantumScale*GetPixelAlpha(image,p);
815 p+=GetPixelChannels(image);
818 image_view=DestroyCacheView(image_view);
819 image->background_color.red=(double) ClampToQuantum(QuantumRange*
820 background.red/count);
821 image->background_color.green=(double) ClampToQuantum(QuantumRange*
822 background.green/count);
823 image->background_color.blue=(double) ClampToQuantum(QuantumRange*
824 background.blue/count);
825 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
826 image->background_color.alpha=(double) ClampToQuantum(QuantumRange*
827 background.alpha/count);
830 MagickExport Image *DeskewImage(const Image *image,const double threshold,
831 ExceptionInfo *exception)
866 Compute deskew angle.
868 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
869 projection=(size_t *) AcquireQuantumMemory((size_t) (2*width-1),
870 sizeof(*projection));
871 if (projection == (size_t *) NULL)
872 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
873 status=RadonTransform(image,threshold,projection,exception);
874 if (status == MagickFalse)
876 projection=(size_t *) RelinquishMagickMemory(projection);
877 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
881 for (i=0; i < (ssize_t) (2*width-1); i++)
883 if (projection[i] > max_projection)
885 skew=i-(ssize_t) width+1;
886 max_projection=projection[i];
889 projection=(size_t *) RelinquishMagickMemory(projection);
890 degrees=RadiansToDegrees(-atan((double) skew/width/8));
891 if (image->debug != MagickFalse)
892 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
893 " Deskew angle: %g",degrees);
897 clone_image=CloneImage(image,0,0,MagickTrue,exception);
898 if (clone_image == (Image *) NULL)
899 return((Image *) NULL);
902 angle[MaxTextExtent];
904 (void) FormatLocaleString(angle,MaxTextExtent,"%.20g",degrees);
905 (void) SetImageArtifact(clone_image,"deskew:angle",angle);
907 (void) SetImageVirtualPixelMethod(clone_image,BackgroundVirtualPixelMethod,
909 affine_matrix.sx=cos(DegreesToRadians(fmod((double) degrees,360.0)));
910 affine_matrix.rx=sin(DegreesToRadians(fmod((double) degrees,360.0)));
911 affine_matrix.ry=(-sin(DegreesToRadians(fmod((double) degrees,360.0))));
912 affine_matrix.sy=cos(DegreesToRadians(fmod((double) degrees,360.0)));
913 affine_matrix.tx=0.0;
914 affine_matrix.ty=0.0;
915 artifact=GetImageArtifact(image,"deskew:auto-crop");
916 if (artifact == (const char *) NULL)
918 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
919 clone_image=DestroyImage(clone_image);
920 return(deskew_image);
925 GetImageBackgroundColor(clone_image,(ssize_t) StringToLong(artifact),
927 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
928 clone_image=DestroyImage(clone_image);
929 if (deskew_image == (Image *) NULL)
930 return((Image *) NULL);
931 median_image=StatisticImage(deskew_image,MedianStatistic,3,3,exception);
932 if (median_image == (Image *) NULL)
934 deskew_image=DestroyImage(deskew_image);
935 return((Image *) NULL);
937 geometry=GetImageBoundingBox(median_image,exception);
938 median_image=DestroyImage(median_image);
939 if (image->debug != MagickFalse)
940 (void) LogMagickEvent(TransformEvent,GetMagickModule()," Deskew geometry: "
941 "%.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
942 geometry.height,(double) geometry.x,(double) geometry.y);
943 crop_image=CropImage(deskew_image,&geometry,exception);
944 deskew_image=DestroyImage(deskew_image);
949 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
953 % I n t e g r a l R o t a t e I m a g e %
957 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
959 % IntegralRotateImage() rotates the image an integral of 90 degrees. It
960 % allocates the memory necessary for the new Image structure and returns a
961 % pointer to the rotated image.
963 % The format of the IntegralRotateImage method is:
965 % Image *IntegralRotateImage(const Image *image,size_t rotations,
966 % ExceptionInfo *exception)
968 % A description of each parameter follows.
970 % o image: the image.
972 % o rotations: Specifies the number of 90 degree rotations.
975 MagickExport Image *IntegralRotateImage(const Image *image,size_t rotations,
976 ExceptionInfo *exception)
978 #define RotateImageTag "Rotate/Image"
1000 Initialize rotated image attributes.
1002 assert(image != (Image *) NULL);
1006 return(CloneImage(image,0,0,MagickTrue,exception));
1007 if ((rotations == 1) || (rotations == 3))
1008 rotate_image=CloneImage(image,image->rows,image->columns,MagickTrue,
1011 rotate_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1013 if (rotate_image == (Image *) NULL)
1014 return((Image *) NULL);
1016 Integral rotate the image.
1020 image_view=AcquireVirtualCacheView(image,exception);
1021 rotate_view=AcquireAuthenticCacheView(rotate_image,exception);
1043 GetPixelCacheTileSize(image,&tile_width,&tile_height);
1044 tile_width=image->columns;
1045 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1046 #pragma omp parallel for schedule(static,4) shared(status) \
1047 magick_threads(image,image,1,1)
1049 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
1054 if (status == MagickFalse)
1057 for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
1062 register const Quantum
1076 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
1077 width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
1079 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
1080 height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
1081 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1083 if (p == (const Quantum *) NULL)
1088 for (y=0; y < (ssize_t) width; y++)
1090 register const Quantum
1091 *restrict tile_pixels;
1096 if (status == MagickFalse)
1098 q=QueueCacheViewAuthenticPixels(rotate_view,(ssize_t)
1099 (rotate_image->columns-(tile_y+height)),y+tile_x,height,1,
1101 if (q == (Quantum *) NULL)
1106 tile_pixels=p+((height-1)*width+y)*GetPixelChannels(image);
1107 for (x=0; x < (ssize_t) height; x++)
1112 if (GetPixelReadMask(image,tile_pixels) == 0)
1114 tile_pixels-=width*GetPixelChannels(image);
1115 q+=GetPixelChannels(rotate_image);
1118 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1120 PixelChannel channel=GetPixelChannelChannel(image,i);
1121 PixelTrait traits=GetPixelChannelTraits(image,channel);
1122 PixelTrait rotate_traits=GetPixelChannelTraits(rotate_image,
1124 if ((traits == UndefinedPixelTrait) ||
1125 (rotate_traits == UndefinedPixelTrait))
1127 SetPixelChannel(rotate_image,channel,tile_pixels[i],q);
1129 tile_pixels-=width*GetPixelChannels(image);
1130 q+=GetPixelChannels(rotate_image);
1132 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1133 if (sync == MagickFalse)
1137 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1142 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1143 #pragma omp critical (MagickCore_IntegralRotateImage)
1145 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
1147 if (proceed == MagickFalse)
1151 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1152 image->rows-1,image->rows);
1153 Swap(page.width,page.height);
1154 Swap(page.x,page.y);
1155 if (page.width != 0)
1156 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1164 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1165 #pragma omp parallel for schedule(static,4) shared(status) \
1166 magick_threads(image,image,1,1)
1168 for (y=0; y < (ssize_t) image->rows; y++)
1173 register const Quantum
1182 if (status == MagickFalse)
1184 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1185 q=QueueCacheViewAuthenticPixels(rotate_view,0,(ssize_t) (image->rows-y-
1186 1),image->columns,1,exception);
1187 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1192 q+=GetPixelChannels(rotate_image)*image->columns;
1193 for (x=0; x < (ssize_t) image->columns; x++)
1198 q-=GetPixelChannels(rotate_image);
1199 if (GetPixelReadMask(image,p) == 0)
1201 p+=GetPixelChannels(image);
1204 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1206 PixelChannel channel=GetPixelChannelChannel(image,i);
1207 PixelTrait traits=GetPixelChannelTraits(image,channel);
1208 PixelTrait rotate_traits=GetPixelChannelTraits(rotate_image,
1210 if ((traits == UndefinedPixelTrait) ||
1211 (rotate_traits == UndefinedPixelTrait))
1213 SetPixelChannel(rotate_image,channel,p[i],q);
1215 p+=GetPixelChannels(image);
1217 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1218 if (sync == MagickFalse)
1220 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1225 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1226 #pragma omp critical (MagickCore_IntegralRotateImage)
1228 proceed=SetImageProgress(image,RotateImageTag,progress++,
1230 if (proceed == MagickFalse)
1234 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1235 image->rows-1,image->rows);
1236 Swap(page.width,page.height);
1237 Swap(page.x,page.y);
1238 if (page.width != 0)
1239 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1254 GetPixelCacheTileSize(image,&tile_width,&tile_height);
1255 tile_width=image->columns;
1256 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1257 #pragma omp parallel for schedule(static,4) shared(status) \
1258 magick_threads(image,image,1,1)
1260 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
1265 if (status == MagickFalse)
1268 for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
1273 register const Quantum
1287 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
1288 width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
1290 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
1291 height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
1292 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1294 if (p == (const Quantum *) NULL)
1299 for (y=0; y < (ssize_t) width; y++)
1301 register const Quantum
1302 *restrict tile_pixels;
1307 if (status == MagickFalse)
1309 q=QueueCacheViewAuthenticPixels(rotate_view,tile_y,(ssize_t) (y+
1310 rotate_image->rows-(tile_x+width)),height,1,exception);
1311 if (q == (Quantum *) NULL)
1316 tile_pixels=p+((width-1)-y)*GetPixelChannels(image);
1317 for (x=0; x < (ssize_t) height; x++)
1322 if (GetPixelReadMask(image,tile_pixels) == 0)
1324 tile_pixels+=width*GetPixelChannels(image);
1325 q+=GetPixelChannels(rotate_image);
1328 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1330 PixelChannel channel=GetPixelChannelChannel(image,i);
1331 PixelTrait traits=GetPixelChannelTraits(image,channel);
1332 PixelTrait rotate_traits=GetPixelChannelTraits(rotate_image,
1334 if ((traits == UndefinedPixelTrait) ||
1335 (rotate_traits == UndefinedPixelTrait))
1337 SetPixelChannel(rotate_image,channel,tile_pixels[i],q);
1339 tile_pixels+=width*GetPixelChannels(image);
1340 q+=GetPixelChannels(rotate_image);
1342 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1343 #pragma omp critical (MagickCore_IntegralRotateImage)
1345 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1346 if (sync == MagickFalse)
1350 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1355 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
1357 if (proceed == MagickFalse)
1361 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1362 image->rows-1,image->rows);
1363 Swap(page.width,page.height);
1364 Swap(page.x,page.y);
1365 if (page.width != 0)
1366 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1370 rotate_view=DestroyCacheView(rotate_view);
1371 image_view=DestroyCacheView(image_view);
1372 rotate_image->type=image->type;
1373 rotate_image->page=page;
1374 if (status == MagickFalse)
1375 rotate_image=DestroyImage(rotate_image);
1376 return(rotate_image);
1380 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1384 + X S h e a r I m a g e %
1388 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1390 % XShearImage() shears the image in the X direction with a shear angle of
1391 % 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1392 % negative angles shear clockwise. Angles are measured relative to a vertical
1393 % Y-axis. X shears will widen an image creating 'empty' triangles on the left
1394 % and right sides of the source image.
1396 % The format of the XShearImage method is:
1398 % MagickBooleanType XShearImage(Image *image,const double degrees,
1399 % const size_t width,const size_t height,
1400 % const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
1402 % A description of each parameter follows.
1404 % o image: the image.
1406 % o degrees: A double representing the shearing angle along the X
1409 % o width, height, x_offset, y_offset: Defines a region of the image
1412 % o exception: return any errors or warnings in this structure.
1415 static MagickBooleanType XShearImage(Image *image,const double degrees,
1416 const size_t width,const size_t height,const ssize_t x_offset,
1417 const ssize_t y_offset,ExceptionInfo *exception)
1419 #define XShearImageTag "XShear/Image"
1445 assert(image != (Image *) NULL);
1446 assert(image->signature == MagickSignature);
1447 if (image->debug != MagickFalse)
1448 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1450 background=image->background_color;
1452 image_view=AcquireAuthenticCacheView(image,exception);
1453 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1454 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1455 magick_threads(image,image,height,1)
1457 for (y=0; y < (ssize_t) height; y++)
1481 if (status == MagickFalse)
1483 p=GetCacheViewAuthenticPixels(image_view,0,y_offset+y,image->columns,1,
1485 if (p == (Quantum *) NULL)
1490 p+=x_offset*GetPixelChannels(image);
1491 displacement=degrees*(double) (y-height/2.0);
1492 if (displacement == 0.0)
1494 if (displacement > 0.0)
1498 displacement*=(-1.0);
1501 step=(ssize_t) floor((double) displacement);
1502 area=(double) (displacement-step);
1505 GetPixelInfo(image,&source);
1506 GetPixelInfo(image,&destination);
1512 Transfer pixels left-to-right.
1514 if (step > x_offset)
1516 q=p-step*GetPixelChannels(image);
1517 for (i=0; i < (ssize_t) width; i++)
1519 if ((x_offset+i) < step)
1521 p+=GetPixelChannels(image);
1522 GetPixelInfoPixel(image,p,&pixel);
1523 q+=GetPixelChannels(image);
1526 GetPixelInfoPixel(image,p,&source);
1527 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1528 &source,(double) GetPixelAlpha(image,p),area,&destination);
1529 SetPixelInfoPixel(image,&destination,q);
1530 GetPixelInfoPixel(image,p,&pixel);
1531 p+=GetPixelChannels(image);
1532 q+=GetPixelChannels(image);
1534 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1535 &background,(double) background.alpha,area,&destination);
1536 SetPixelInfoPixel(image,&destination,q);
1537 q+=GetPixelChannels(image);
1538 for (i=0; i < (step-1); i++)
1540 SetPixelInfoPixel(image,&background,q);
1541 q+=GetPixelChannels(image);
1548 Transfer pixels right-to-left.
1550 p+=width*GetPixelChannels(image);
1551 q=p+step*GetPixelChannels(image);
1552 for (i=0; i < (ssize_t) width; i++)
1554 p-=GetPixelChannels(image);
1555 q-=GetPixelChannels(image);
1556 if ((size_t) (x_offset+width+step-i) >= image->columns)
1558 GetPixelInfoPixel(image,p,&source);
1559 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1560 &source,(double) GetPixelAlpha(image,p),area,&destination);
1561 SetPixelInfoPixel(image,&destination,q);
1562 GetPixelInfoPixel(image,p,&pixel);
1564 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1565 &background,(double) background.alpha,area,&destination);
1566 q-=GetPixelChannels(image);
1567 SetPixelInfoPixel(image,&destination,q);
1568 for (i=0; i < (step-1); i++)
1570 q-=GetPixelChannels(image);
1571 SetPixelInfoPixel(image,&background,q);
1576 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1578 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1583 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1584 #pragma omp critical (MagickCore_XShearImage)
1586 proceed=SetImageProgress(image,XShearImageTag,progress++,height);
1587 if (proceed == MagickFalse)
1591 image_view=DestroyCacheView(image_view);
1596 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1600 + Y S h e a r I m a g e %
1604 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1606 % YShearImage shears the image in the Y direction with a shear angle of
1607 % 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1608 % negative angles shear clockwise. Angles are measured relative to a
1609 % horizontal X-axis. Y shears will increase the height of an image creating
1610 % 'empty' triangles on the top and bottom of the source image.
1612 % The format of the YShearImage method is:
1614 % MagickBooleanType YShearImage(Image *image,const double degrees,
1615 % const size_t width,const size_t height,
1616 % const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
1618 % A description of each parameter follows.
1620 % o image: the image.
1622 % o degrees: A double representing the shearing angle along the Y
1625 % o width, height, x_offset, y_offset: Defines a region of the image
1628 % o exception: return any errors or warnings in this structure.
1631 static MagickBooleanType YShearImage(Image *image,const double degrees,
1632 const size_t width,const size_t height,const ssize_t x_offset,
1633 const ssize_t y_offset,ExceptionInfo *exception)
1635 #define YShearImageTag "YShear/Image"
1661 assert(image != (Image *) NULL);
1662 assert(image->signature == MagickSignature);
1663 if (image->debug != MagickFalse)
1664 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1667 background=image->background_color;
1668 image_view=AcquireAuthenticCacheView(image,exception);
1669 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1670 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1671 magick_threads(image,image,width,1)
1673 for (x=0; x < (ssize_t) width; x++)
1697 if (status == MagickFalse)
1699 p=GetCacheViewAuthenticPixels(image_view,x_offset+x,0,1,image->rows,
1701 if (p == (Quantum *) NULL)
1706 p+=y_offset*GetPixelChannels(image);
1707 displacement=degrees*(double) (x-width/2.0);
1708 if (displacement == 0.0)
1710 if (displacement > 0.0)
1714 displacement*=(-1.0);
1717 step=(ssize_t) floor((double) displacement);
1718 area=(double) (displacement-step);
1721 GetPixelInfo(image,&source);
1722 GetPixelInfo(image,&destination);
1728 Transfer pixels top-to-bottom.
1730 if (step > y_offset)
1732 q=p-step*GetPixelChannels(image);
1733 for (i=0; i < (ssize_t) height; i++)
1735 if ((y_offset+i) < step)
1737 p+=GetPixelChannels(image);
1738 GetPixelInfoPixel(image,p,&pixel);
1739 q+=GetPixelChannels(image);
1742 GetPixelInfoPixel(image,p,&source);
1743 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1744 &source,(double) GetPixelAlpha(image,p),area,
1746 SetPixelInfoPixel(image,&destination,q);
1747 GetPixelInfoPixel(image,p,&pixel);
1748 p+=GetPixelChannels(image);
1749 q+=GetPixelChannels(image);
1751 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1752 &background,(double) background.alpha,area,&destination);
1753 SetPixelInfoPixel(image,&destination,q);
1754 q+=GetPixelChannels(image);
1755 for (i=0; i < (step-1); i++)
1757 SetPixelInfoPixel(image,&background,q);
1758 q+=GetPixelChannels(image);
1765 Transfer pixels bottom-to-top.
1767 p+=height*GetPixelChannels(image);
1768 q=p+step*GetPixelChannels(image);
1769 for (i=0; i < (ssize_t) height; i++)
1771 p-=GetPixelChannels(image);
1772 q-=GetPixelChannels(image);
1773 if ((size_t) (y_offset+height+step-i) >= image->rows)
1775 GetPixelInfoPixel(image,p,&source);
1776 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1777 &source,(double) GetPixelAlpha(image,p),area,
1779 SetPixelInfoPixel(image,&destination,q);
1780 GetPixelInfoPixel(image,p,&pixel);
1782 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1783 &background,(double) background.alpha,area,&destination);
1784 q-=GetPixelChannels(image);
1785 SetPixelInfoPixel(image,&destination,q);
1786 for (i=0; i < (step-1); i++)
1788 q-=GetPixelChannels(image);
1789 SetPixelInfoPixel(image,&background,q);
1794 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1796 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1801 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1802 #pragma omp critical (MagickCore_YShearImage)
1804 proceed=SetImageProgress(image,YShearImageTag,progress++,image->rows);
1805 if (proceed == MagickFalse)
1809 image_view=DestroyCacheView(image_view);
1814 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1818 % S h e a r I m a g e %
1822 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1824 % ShearImage() creates a new image that is a shear_image copy of an existing
1825 % one. Shearing slides one edge of an image along the X or Y axis, creating
1826 % a parallelogram. An X direction shear slides an edge along the X axis,
1827 % while a Y direction shear slides an edge along the Y axis. The amount of
1828 % the shear is controlled by a shear angle. For X direction shears, x_shear
1829 % is measured relative to the Y axis, and similarly, for Y direction shears
1830 % y_shear is measured relative to the X axis. Empty triangles left over from
1831 % shearing the image are filled with the background color defined by member
1832 % 'background_color' of the image.. ShearImage() allocates the memory
1833 % necessary for the new Image structure and returns a pointer to the new image.
1835 % ShearImage() is based on the paper "A Fast Algorithm for General Raster
1836 % Rotatation" by Alan W. Paeth.
1838 % The format of the ShearImage method is:
1840 % Image *ShearImage(const Image *image,const double x_shear,
1841 % const double y_shear,ExceptionInfo *exception)
1843 % A description of each parameter follows.
1845 % o image: the image.
1847 % o x_shear, y_shear: Specifies the number of degrees to shear the image.
1849 % o exception: return any errors or warnings in this structure.
1852 MagickExport Image *ShearImage(const Image *image,const double x_shear,
1853 const double y_shear,ExceptionInfo *exception)
1875 assert(image != (Image *) NULL);
1876 assert(image->signature == MagickSignature);
1877 if (image->debug != MagickFalse)
1878 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1879 assert(exception != (ExceptionInfo *) NULL);
1880 assert(exception->signature == MagickSignature);
1881 if ((x_shear != 0.0) && (fmod(x_shear,90.0) == 0.0))
1882 ThrowImageException(ImageError,"AngleIsDiscontinuous");
1883 if ((y_shear != 0.0) && (fmod(y_shear,90.0) == 0.0))
1884 ThrowImageException(ImageError,"AngleIsDiscontinuous");
1886 Initialize shear angle.
1888 integral_image=CloneImage(image,0,0,MagickTrue,exception);
1889 if (integral_image == (Image *) NULL)
1890 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1891 shear.x=(-tan(DegreesToRadians(fmod(x_shear,360.0))));
1892 shear.y=tan(DegreesToRadians(fmod(y_shear,360.0)));
1893 if ((shear.x == 0.0) && (shear.y == 0.0))
1894 return(integral_image);
1895 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
1897 integral_image=DestroyImage(integral_image);
1898 return(integral_image);
1900 if (integral_image->alpha_trait != BlendPixelTrait)
1901 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
1905 y_width=image->columns+(ssize_t) floor(fabs(shear.x)*image->rows+0.5);
1906 x_offset=(ssize_t) ceil((double) image->columns+((fabs(shear.x)*image->rows)-
1907 image->columns)/2.0-0.5);
1908 y_offset=(ssize_t) ceil((double) image->rows+((fabs(shear.y)*y_width)-
1909 image->rows)/2.0-0.5);
1911 Surround image with border.
1913 integral_image->border_color=integral_image->background_color;
1914 integral_image->compose=CopyCompositeOp;
1915 border_info.width=(size_t) x_offset;
1916 border_info.height=(size_t) y_offset;
1917 shear_image=BorderImage(integral_image,&border_info,image->compose,exception);
1918 integral_image=DestroyImage(integral_image);
1919 if (shear_image == (Image *) NULL)
1920 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1924 if (shear_image->alpha_trait != BlendPixelTrait)
1925 (void) SetImageAlphaChannel(shear_image,OpaqueAlphaChannel,exception);
1926 status=XShearImage(shear_image,shear.x,image->columns,image->rows,x_offset,
1927 (ssize_t) (shear_image->rows-image->rows)/2,exception);
1928 if (status == MagickFalse)
1930 shear_image=DestroyImage(shear_image);
1931 return((Image *) NULL);
1933 status=YShearImage(shear_image,shear.y,y_width,image->rows,(ssize_t)
1934 (shear_image->columns-y_width)/2,y_offset,exception);
1935 if (status == MagickFalse)
1937 shear_image=DestroyImage(shear_image);
1938 return((Image *) NULL);
1940 status=CropToFitImage(&shear_image,shear.x,shear.y,(double)
1941 image->columns,(double) image->rows,MagickFalse,exception);
1942 shear_image->compose=image->compose;
1943 shear_image->page.width=0;
1944 shear_image->page.height=0;
1945 if (status == MagickFalse)
1946 shear_image=DestroyImage(shear_image);
1947 return(shear_image);
1951 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1955 % S h e a r R o t a t e I m a g e %
1959 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1961 % ShearRotateImage() creates a new image that is a rotated copy of an existing
1962 % one. Positive angles rotate counter-clockwise (right-hand rule), while
1963 % negative angles rotate clockwise. Rotated images are usually larger than
1964 % the originals and have 'empty' triangular corners. X axis. Empty
1965 % triangles left over from shearing the image are filled with the background
1966 % color defined by member 'background_color' of the image. ShearRotateImage
1967 % allocates the memory necessary for the new Image structure and returns a
1968 % pointer to the new image.
1970 % ShearRotateImage() is based on the paper "A Fast Algorithm for General
1971 % Raster Rotatation" by Alan W. Paeth. ShearRotateImage is adapted from a
1972 % similar method based on the Paeth paper written by Michael Halle of the
1973 % Spatial Imaging Group, MIT Media Lab.
1975 % The format of the ShearRotateImage method is:
1977 % Image *ShearRotateImage(const Image *image,const double degrees,
1978 % ExceptionInfo *exception)
1980 % A description of each parameter follows.
1982 % o image: the image.
1984 % o degrees: Specifies the number of degrees to rotate the image.
1986 % o exception: return any errors or warnings in this structure.
1989 MagickExport Image *ShearRotateImage(const Image *image,const double degrees,
1990 ExceptionInfo *exception)
2019 Adjust rotation angle.
2021 assert(image != (Image *) NULL);
2022 assert(image->signature == MagickSignature);
2023 if (image->debug != MagickFalse)
2024 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2025 assert(exception != (ExceptionInfo *) NULL);
2026 assert(exception->signature == MagickSignature);
2028 while (angle < -45.0)
2030 for (rotations=0; angle > 45.0; rotations++)
2034 Calculate shear equations.
2036 integral_image=IntegralRotateImage(image,rotations,exception);
2037 if (integral_image == (Image *) NULL)
2038 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2039 shear.x=(-tan((double) DegreesToRadians(angle)/2.0));
2040 shear.y=sin((double) DegreesToRadians(angle));
2041 if ((shear.x == 0.0) && (shear.y == 0.0))
2042 return(integral_image);
2043 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
2045 integral_image=DestroyImage(integral_image);
2046 return(integral_image);
2048 if (integral_image->alpha_trait != BlendPixelTrait)
2049 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
2053 width=image->columns;
2055 if ((rotations == 1) || (rotations == 3))
2058 height=image->columns;
2060 y_width=width+(ssize_t) floor(fabs(shear.x)*height+0.5);
2061 x_offset=(ssize_t) ceil((double) width+((fabs(shear.y)*height)-width)/2.0-
2063 y_offset=(ssize_t) ceil((double) height+((fabs(shear.y)*y_width)-height)/2.0-
2066 Surround image with a border.
2068 integral_image->border_color=integral_image->background_color;
2069 integral_image->compose=CopyCompositeOp;
2070 border_info.width=(size_t) x_offset;
2071 border_info.height=(size_t) y_offset;
2072 rotate_image=BorderImage(integral_image,&border_info,image->compose,
2074 integral_image=DestroyImage(integral_image);
2075 if (rotate_image == (Image *) NULL)
2076 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2080 status=XShearImage(rotate_image,shear.x,width,height,x_offset,(ssize_t)
2081 (rotate_image->rows-height)/2,exception);
2082 if (status == MagickFalse)
2084 rotate_image=DestroyImage(rotate_image);
2085 return((Image *) NULL);
2087 status=YShearImage(rotate_image,shear.y,y_width,height,(ssize_t)
2088 (rotate_image->columns-y_width)/2,y_offset,exception);
2089 if (status == MagickFalse)
2091 rotate_image=DestroyImage(rotate_image);
2092 return((Image *) NULL);
2094 status=XShearImage(rotate_image,shear.x,y_width,rotate_image->rows,(ssize_t)
2095 (rotate_image->columns-y_width)/2,0,exception);
2096 if (status == MagickFalse)
2098 rotate_image=DestroyImage(rotate_image);
2099 return((Image *) NULL);
2101 status=CropToFitImage(&rotate_image,shear.x,shear.y,(double) width,
2102 (double) height,MagickTrue,exception);
2103 rotate_image->compose=image->compose;
2104 rotate_image->page.width=0;
2105 rotate_image->page.height=0;
2106 if (status == MagickFalse)
2107 rotate_image=DestroyImage(rotate_image);
2108 return(rotate_image);