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-2012 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 format of the DeskewImage method is:
197 % Image *DeskewImage(const Image *image,const double threshold,
198 % ExceptionInfo *exception)
200 % A description of each parameter follows:
202 % o image: the image.
204 % o threshold: separate background from foreground.
206 % o exception: return any errors or warnings in this structure.
210 typedef struct _RadonInfo
235 static RadonInfo *DestroyRadonInfo(RadonInfo *radon_info)
237 assert(radon_info != (RadonInfo *) NULL);
238 switch (radon_info->type)
242 if (radon_info->mapped == MagickFalse)
243 radon_info->cells=(unsigned short *) RelinquishMagickMemory(
246 radon_info->cells=(unsigned short *) UnmapBlob(radon_info->cells,
247 (size_t) radon_info->length);
248 RelinquishMagickResource(MemoryResource,radon_info->length);
253 radon_info->cells=(unsigned short *) UnmapBlob(radon_info->cells,(size_t)
255 RelinquishMagickResource(MapResource,radon_info->length);
259 if (radon_info->file != -1)
260 (void) close(radon_info->file);
261 (void) RelinquishUniqueFileResource(radon_info->path);
262 RelinquishMagickResource(DiskResource,radon_info->length);
268 return((RadonInfo *) RelinquishMagickMemory(radon_info));
271 static MagickBooleanType ResetRadonCells(RadonInfo *radon_info)
283 if (radon_info->type != DiskCache)
285 (void) ResetMagickMemory(radon_info->cells,0,(size_t) radon_info->length);
289 (void) lseek(radon_info->file,0,SEEK_SET);
290 for (y=0; y < (ssize_t) radon_info->height; y++)
292 for (x=0; x < (ssize_t) radon_info->width; x++)
294 count=write(radon_info->file,&value,sizeof(*radon_info->cells));
295 if (count != (ssize_t) sizeof(*radon_info->cells))
298 if (x < (ssize_t) radon_info->width)
301 return(y < (ssize_t) radon_info->height ? MagickFalse : MagickTrue);
304 static RadonInfo *AcquireRadonInfo(const Image *image,const size_t width,
305 const size_t height,ExceptionInfo *exception)
313 radon_info=(RadonInfo *) AcquireMagickMemory(sizeof(*radon_info));
314 if (radon_info == (RadonInfo *) NULL)
315 return((RadonInfo *) NULL);
316 (void) ResetMagickMemory(radon_info,0,sizeof(*radon_info));
317 radon_info->width=width;
318 radon_info->height=height;
319 radon_info->length=(MagickSizeType) width*height*sizeof(*radon_info->cells);
320 radon_info->type=MemoryCache;
321 status=AcquireMagickResource(AreaResource,radon_info->length);
322 if ((status != MagickFalse) &&
323 (radon_info->length == (MagickSizeType) ((size_t) radon_info->length)))
325 status=AcquireMagickResource(MemoryResource,radon_info->length);
326 if (status != MagickFalse)
328 radon_info->mapped=MagickFalse;
329 radon_info->cells=(unsigned short *) AcquireMagickMemory((size_t)
331 if (radon_info->cells == (unsigned short *) NULL)
333 radon_info->mapped=MagickTrue;
334 radon_info->cells=(unsigned short *) MapBlob(-1,IOMode,0,(size_t)
337 if (radon_info->cells == (unsigned short *) NULL)
338 RelinquishMagickResource(MemoryResource,radon_info->length);
341 radon_info->file=(-1);
342 if (radon_info->cells == (unsigned short *) NULL)
344 status=AcquireMagickResource(DiskResource,radon_info->length);
345 if (status == MagickFalse)
347 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
348 "CacheResourcesExhausted","'%s'",image->filename);
349 return(DestroyRadonInfo(radon_info));
351 radon_info->type=DiskCache;
352 (void) AcquireMagickResource(MemoryResource,radon_info->length);
353 radon_info->file=AcquireUniqueFileResource(radon_info->path);
354 if (radon_info->file == -1)
355 return(DestroyRadonInfo(radon_info));
356 status=AcquireMagickResource(MapResource,radon_info->length);
357 if (status != MagickFalse)
359 status=ResetRadonCells(radon_info);
360 if (status != MagickFalse)
362 radon_info->cells=(unsigned short *) MapBlob(radon_info->file,
363 IOMode,0,(size_t) radon_info->length);
364 if (radon_info->cells != (unsigned short *) NULL)
365 radon_info->type=MapCache;
367 RelinquishMagickResource(MapResource,radon_info->length);
374 static inline size_t MagickMin(const size_t x,const size_t y)
381 static inline ssize_t ReadRadonCell(const RadonInfo *radon_info,
382 const MagickOffsetType offset,const size_t length,unsigned char *buffer)
390 #if !defined(MAGICKCORE_HAVE_PPREAD)
391 #if defined(MAGICKCORE_OPENMP_SUPPORT)
392 #pragma omp critical (MagickCore_ReadRadonCell)
396 if (lseek(radon_info->file,offset,SEEK_SET) >= 0)
400 for (i=0; i < (ssize_t) length; i+=count)
402 #if !defined(MAGICKCORE_HAVE_PPREAD)
403 count=read(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
406 count=pread(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
407 SSIZE_MAX),offset+i);
418 #if !defined(MAGICKCORE_HAVE_PPREAD)
425 static inline ssize_t WriteRadonCell(const RadonInfo *radon_info,
426 const MagickOffsetType offset,const size_t length,const unsigned char *buffer)
434 #if !defined(MAGICKCORE_HAVE_PWRITE)
435 #if defined(MAGICKCORE_OPENMP_SUPPORT)
436 #pragma omp critical (MagickCore_WriteRadonCell)
439 if (lseek(radon_info->file,offset,SEEK_SET) >= 0)
443 for (i=0; i < (ssize_t) length; i+=count)
445 #if !defined(MAGICKCORE_HAVE_PWRITE)
446 count=write(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
449 count=pwrite(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
450 SSIZE_MAX),offset+i);
461 #if !defined(MAGICKCORE_HAVE_PWRITE)
468 static inline unsigned short GetRadonCell(const RadonInfo *radon_info,
469 const ssize_t x,const ssize_t y)
477 i=(MagickOffsetType) radon_info->height*x+y;
479 ((MagickSizeType) (i*sizeof(*radon_info->cells)) >= radon_info->length))
481 if (radon_info->type != DiskCache)
482 return(radon_info->cells[i]);
484 (void) ReadRadonCell(radon_info,i*sizeof(*radon_info->cells),
485 sizeof(*radon_info->cells),(unsigned char *) &value);
489 static inline MagickBooleanType SetRadonCell(const RadonInfo *radon_info,
490 const ssize_t x,const ssize_t y,const unsigned short value)
498 i=(MagickOffsetType) radon_info->height*x+y;
500 ((MagickSizeType) (i*sizeof(*radon_info->cells)) >= radon_info->length))
502 if (radon_info->type != DiskCache)
504 radon_info->cells[i]=value;
507 count=WriteRadonCell(radon_info,i*sizeof(*radon_info->cells),
508 sizeof(*radon_info->cells),(const unsigned char *) &value);
509 if (count != (ssize_t) sizeof(*radon_info->cells))
514 static void RadonProjection(const Image *image,RadonInfo *source_cells,
515 RadonInfo *destination_cells,const ssize_t sign,size_t *projection)
532 for (step=1; step < p->width; step*=2)
534 for (x=0; x < (ssize_t) p->width; x+=2*(ssize_t) step)
545 for (i=0; i < (ssize_t) step; i++)
547 for (y=0; y < (ssize_t) (p->height-i-1); y++)
549 cell=GetRadonCell(p,x+i,y);
550 (void) SetRadonCell(q,x+2*i,y,cell+GetRadonCell(p,x+i+(ssize_t)
552 (void) SetRadonCell(q,x+2*i+1,y,cell+GetRadonCell(p,x+i+(ssize_t)
555 for ( ; y < (ssize_t) (p->height-i); y++)
557 cell=GetRadonCell(p,x+i,y);
558 (void) SetRadonCell(q,x+2*i,y,cell+GetRadonCell(p,x+i+(ssize_t) step,
560 (void) SetRadonCell(q,x+2*i+1,y,cell);
562 for ( ; y < (ssize_t) p->height; y++)
564 cell=GetRadonCell(p,x+i,y);
565 (void) SetRadonCell(q,x+2*i,y,cell);
566 (void) SetRadonCell(q,x+2*i+1,y,cell);
574 #if defined(MAGICKCORE_OPENMP_SUPPORT)
575 #pragma omp parallel for schedule(static,4) \
576 dynamic_number_threads(image,p->width,p->height,1)
578 for (x=0; x < (ssize_t) p->width; x++)
587 for (y=0; y < (ssize_t) (p->height-1); y++)
592 delta=GetRadonCell(p,x,y)-(ssize_t) GetRadonCell(p,x,y+1);
595 projection[p->width+sign*x-1]=sum;
599 static MagickBooleanType RadonTransform(const Image *image,
600 const double threshold,size_t *projection,ExceptionInfo *exception)
628 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
629 source_cells=AcquireRadonInfo(image,width,image->rows,exception);
630 destination_cells=AcquireRadonInfo(image,width,image->rows,exception);
631 if ((source_cells == (RadonInfo *) NULL) ||
632 (destination_cells == (RadonInfo *) NULL))
634 if (destination_cells != (RadonInfo *) NULL)
635 destination_cells=DestroyRadonInfo(destination_cells);
636 if (source_cells != (RadonInfo *) NULL)
637 source_cells=DestroyRadonInfo(source_cells);
640 if (ResetRadonCells(source_cells) == MagickFalse)
642 destination_cells=DestroyRadonInfo(destination_cells);
643 source_cells=DestroyRadonInfo(source_cells);
646 for (i=0; i < 256; i++)
648 byte=(unsigned char) i;
649 for (count=0; byte != 0; byte>>=1)
651 bits[i]=(unsigned short) count;
654 image_view=AcquireVirtualCacheView(image,exception);
655 #if defined(MAGICKCORE_OPENMP_SUPPORT)
656 #pragma omp parallel for schedule(static,4) shared(status) \
657 dynamic_number_threads(image,image->columns,image->rows,1)
659 for (y=0; y < (ssize_t) image->rows; y++)
661 register const Quantum
672 if (status == MagickFalse)
674 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
675 if (p == (const Quantum *) NULL)
682 i=(ssize_t) (image->columns+7)/8;
683 for (x=0; x < (ssize_t) image->columns; x++)
686 if (GetPixelIntensity(image,p) < threshold)
691 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
695 p+=GetPixelChannels(image);
700 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
703 RadonProjection(image,source_cells,destination_cells,-1,projection);
704 (void) ResetRadonCells(source_cells);
705 #if defined(MAGICKCORE_OPENMP_SUPPORT)
706 #pragma omp parallel for schedule(static,4) shared(status) \
707 dynamic_number_threads(image,image->columns,image->rows,1)
709 for (y=0; y < (ssize_t) image->rows; y++)
711 register const Quantum
722 if (status == MagickFalse)
724 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
725 if (p == (const Quantum *) NULL)
733 for (x=0; x < (ssize_t) image->columns; x++)
736 if (GetPixelIntensity(image,p) < threshold)
741 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
745 p+=GetPixelChannels(image);
750 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
753 RadonProjection(image,source_cells,destination_cells,1,projection);
754 image_view=DestroyCacheView(image_view);
755 destination_cells=DestroyRadonInfo(destination_cells);
756 source_cells=DestroyRadonInfo(source_cells);
760 static void GetImageBackgroundColor(Image *image,const ssize_t offset,
761 ExceptionInfo *exception)
776 Compute average background color.
780 GetPixelInfo(image,&background);
782 image_view=AcquireVirtualCacheView(image,exception);
783 for (y=0; y < (ssize_t) image->rows; y++)
785 register const Quantum
791 if ((y >= offset) && (y < ((ssize_t) image->rows-offset)))
793 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
794 if (p == (const Quantum *) NULL)
796 for (x=0; x < (ssize_t) image->columns; x++)
798 if ((x >= offset) && (x < ((ssize_t) image->columns-offset)))
800 background.red+=QuantumScale*GetPixelRed(image,p);
801 background.green+=QuantumScale*GetPixelGreen(image,p);
802 background.blue+=QuantumScale*GetPixelBlue(image,p);
803 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
804 background.alpha+=QuantumScale*GetPixelAlpha(image,p);
806 p+=GetPixelChannels(image);
809 image_view=DestroyCacheView(image_view);
810 image->background_color.red=(double) ClampToQuantum(QuantumRange*
811 background.red/count);
812 image->background_color.green=(double) ClampToQuantum(QuantumRange*
813 background.green/count);
814 image->background_color.blue=(double) ClampToQuantum(QuantumRange*
815 background.blue/count);
816 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
817 image->background_color.alpha=(double) ClampToQuantum(QuantumRange*
818 background.alpha/count);
821 MagickExport Image *DeskewImage(const Image *image,const double threshold,
822 ExceptionInfo *exception)
857 Compute deskew angle.
859 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
860 projection=(size_t *) AcquireQuantumMemory((size_t) (2*width-1),
861 sizeof(*projection));
862 if (projection == (size_t *) NULL)
863 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
864 status=RadonTransform(image,threshold,projection,exception);
865 if (status == MagickFalse)
867 projection=(size_t *) RelinquishMagickMemory(projection);
868 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
872 for (i=0; i < (ssize_t) (2*width-1); i++)
874 if (projection[i] > max_projection)
876 skew=i-(ssize_t) width+1;
877 max_projection=projection[i];
880 projection=(size_t *) RelinquishMagickMemory(projection);
884 clone_image=CloneImage(image,0,0,MagickTrue,exception);
885 if (clone_image == (Image *) NULL)
886 return((Image *) NULL);
887 (void) SetImageVirtualPixelMethod(clone_image,BackgroundVirtualPixelMethod,
889 degrees=RadiansToDegrees(-atan((double) skew/width/8));
890 if (image->debug != MagickFalse)
891 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
892 " Deskew angle: %g",degrees);
893 affine_matrix.sx=cos(DegreesToRadians(fmod((double) degrees,360.0)));
894 affine_matrix.rx=sin(DegreesToRadians(fmod((double) degrees,360.0)));
895 affine_matrix.ry=(-sin(DegreesToRadians(fmod((double) degrees,360.0))));
896 affine_matrix.sy=cos(DegreesToRadians(fmod((double) degrees,360.0)));
897 affine_matrix.tx=0.0;
898 affine_matrix.ty=0.0;
899 artifact=GetImageArtifact(image,"deskew:auto-crop");
900 if (artifact == (const char *) NULL)
902 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
903 clone_image=DestroyImage(clone_image);
904 return(deskew_image);
909 GetImageBackgroundColor(clone_image,(ssize_t) StringToLong(artifact),
911 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
912 clone_image=DestroyImage(clone_image);
913 if (deskew_image == (Image *) NULL)
914 return((Image *) NULL);
915 median_image=StatisticImage(deskew_image,MedianStatistic,3,3,exception);
916 if (median_image == (Image *) NULL)
918 deskew_image=DestroyImage(deskew_image);
919 return((Image *) NULL);
921 geometry=GetImageBoundingBox(median_image,exception);
922 median_image=DestroyImage(median_image);
923 if (image->debug != MagickFalse)
924 (void) LogMagickEvent(TransformEvent,GetMagickModule()," Deskew geometry: "
925 "%.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
926 geometry.height,(double) geometry.x,(double) geometry.y);
927 crop_image=CropImage(deskew_image,&geometry,exception);
928 deskew_image=DestroyImage(deskew_image);
933 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
937 % I n t e g r a l R o t a t e I m a g e %
941 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
943 % IntegralRotateImage() rotates the image an integral of 90 degrees. It
944 % allocates the memory necessary for the new Image structure and returns a
945 % pointer to the rotated image.
947 % The format of the IntegralRotateImage method is:
949 % Image *IntegralRotateImage(const Image *image,size_t rotations,
950 % ExceptionInfo *exception)
952 % A description of each parameter follows.
954 % o image: the image.
956 % o rotations: Specifies the number of 90 degree rotations.
959 MagickExport Image *IntegralRotateImage(const Image *image,size_t rotations,
960 ExceptionInfo *exception)
962 #define RotateImageTag "Rotate/Image"
984 Initialize rotated image attributes.
986 assert(image != (Image *) NULL);
990 return(CloneImage(image,0,0,MagickTrue,exception));
991 if ((rotations == 1) || (rotations == 3))
992 rotate_image=CloneImage(image,image->rows,image->columns,MagickTrue,
995 rotate_image=CloneImage(image,image->columns,image->rows,MagickTrue,
997 if (rotate_image == (Image *) NULL)
998 return((Image *) NULL);
1000 Integral rotate the image.
1004 image_view=AcquireVirtualCacheView(image,exception);
1005 rotate_view=AcquireAuthenticCacheView(rotate_image,exception);
1027 GetPixelCacheTileSize(image,&tile_width,&tile_height);
1028 tile_width=(ssize_t) image->columns;
1029 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
1034 if (status == MagickFalse)
1037 for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
1042 register const Quantum
1056 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
1057 width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
1059 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
1060 height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
1061 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1063 if (p == (const Quantum *) NULL)
1068 for (y=0; y < (ssize_t) width; y++)
1070 register const Quantum
1071 *restrict tile_pixels;
1076 if (status == MagickFalse)
1078 q=QueueCacheViewAuthenticPixels(rotate_view,(ssize_t)
1079 (rotate_image->columns-(tile_y+height)),y+tile_x,height,1,
1081 if (q == (Quantum *) NULL)
1086 tile_pixels=p+((height-1)*width+y)*GetPixelChannels(image);
1087 for (x=0; x < (ssize_t) height; x++)
1092 if (GetPixelMask(image,tile_pixels) != 0)
1094 tile_pixels-=width*GetPixelChannels(image);
1095 q+=GetPixelChannels(rotate_image);
1098 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1107 channel=GetPixelChannelChannel(image,i);
1108 traits=GetPixelChannelTraits(image,channel);
1109 rotate_traits=GetPixelChannelTraits(rotate_image,channel);
1110 if ((traits == UndefinedPixelTrait) ||
1111 (rotate_traits == UndefinedPixelTrait))
1113 SetPixelChannel(rotate_image,channel,tile_pixels[i],q);
1115 tile_pixels-=width*GetPixelChannels(image);
1116 q+=GetPixelChannels(rotate_image);
1118 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1119 if (sync == MagickFalse)
1123 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1128 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
1130 if (proceed == MagickFalse)
1134 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1135 image->rows-1,image->rows);
1136 Swap(page.width,page.height);
1137 Swap(page.x,page.y);
1138 if (page.width != 0)
1139 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1147 for (y=0; y < (ssize_t) image->rows; y++)
1152 register const Quantum
1161 if (status == MagickFalse)
1163 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1164 q=QueueCacheViewAuthenticPixels(rotate_view,0,(ssize_t) (image->rows-y-
1165 1),image->columns,1,exception);
1166 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1171 q+=GetPixelChannels(rotate_image)*image->columns;
1172 for (x=0; x < (ssize_t) image->columns; x++)
1177 q-=GetPixelChannels(rotate_image);
1178 if (GetPixelMask(image,p) != 0)
1180 p+=GetPixelChannels(image);
1183 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1192 channel=GetPixelChannelChannel(image,i);
1193 traits=GetPixelChannelTraits(image,channel);
1194 rotate_traits=GetPixelChannelTraits(rotate_image,channel);
1195 if ((traits == UndefinedPixelTrait) ||
1196 (rotate_traits == UndefinedPixelTrait))
1198 SetPixelChannel(rotate_image,channel,p[i],q);
1200 p+=GetPixelChannels(image);
1202 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1203 if (sync == MagickFalse)
1205 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1210 proceed=SetImageProgress(image,RotateImageTag,progress++,
1212 if (proceed == MagickFalse)
1216 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1217 image->rows-1,image->rows);
1218 Swap(page.width,page.height);
1219 Swap(page.x,page.y);
1220 if (page.width != 0)
1221 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1236 GetPixelCacheTileSize(image,&tile_width,&tile_height);
1237 tile_width=(ssize_t) image->columns;
1238 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
1243 if (status == MagickFalse)
1246 for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
1251 register const Quantum
1265 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
1266 width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
1268 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
1269 height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
1270 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1272 if (p == (const Quantum *) NULL)
1277 for (y=0; y < (ssize_t) width; y++)
1279 register const Quantum
1280 *restrict tile_pixels;
1285 if (status == MagickFalse)
1287 q=QueueCacheViewAuthenticPixels(rotate_view,tile_y,(ssize_t) (y+
1288 rotate_image->rows-(tile_x+width)),height,1,exception);
1289 if (q == (Quantum *) NULL)
1294 tile_pixels=p+((width-1)-y)*GetPixelChannels(image);
1295 for (x=0; x < (ssize_t) height; x++)
1300 if (GetPixelMask(image,tile_pixels) != 0)
1302 tile_pixels+=width*GetPixelChannels(image);
1303 q+=GetPixelChannels(rotate_image);
1306 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1315 channel=GetPixelChannelChannel(image,i);
1316 traits=GetPixelChannelTraits(image,channel);
1317 rotate_traits=GetPixelChannelTraits(rotate_image,channel);
1318 if ((traits == UndefinedPixelTrait) ||
1319 (rotate_traits == UndefinedPixelTrait))
1321 SetPixelChannel(rotate_image,channel,tile_pixels[i],q);
1323 tile_pixels+=width*GetPixelChannels(image);
1324 q+=GetPixelChannels(rotate_image);
1326 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1327 if (sync == MagickFalse)
1331 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1336 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
1338 if (proceed == MagickFalse)
1342 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1343 image->rows-1,image->rows);
1344 Swap(page.width,page.height);
1345 Swap(page.x,page.y);
1346 if (page.width != 0)
1347 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1351 rotate_view=DestroyCacheView(rotate_view);
1352 image_view=DestroyCacheView(image_view);
1353 rotate_image->type=image->type;
1354 rotate_image->page=page;
1355 if (status == MagickFalse)
1356 rotate_image=DestroyImage(rotate_image);
1357 return(rotate_image);
1361 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1365 + X S h e a r I m a g e %
1369 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1371 % XShearImage() shears the image in the X direction with a shear angle of
1372 % 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1373 % negative angles shear clockwise. Angles are measured relative to a vertical
1374 % Y-axis. X shears will widen an image creating 'empty' triangles on the left
1375 % and right sides of the source image.
1377 % The format of the XShearImage method is:
1379 % MagickBooleanType XShearImage(Image *image,const double degrees,
1380 % const size_t width,const size_t height,
1381 % const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
1383 % A description of each parameter follows.
1385 % o image: the image.
1387 % o degrees: A double representing the shearing angle along the X
1390 % o width, height, x_offset, y_offset: Defines a region of the image
1393 % o exception: return any errors or warnings in this structure.
1396 static MagickBooleanType XShearImage(Image *image,const double degrees,
1397 const size_t width,const size_t height,const ssize_t x_offset,
1398 const ssize_t y_offset,ExceptionInfo *exception)
1400 #define XShearImageTag "XShear/Image"
1426 assert(image != (Image *) NULL);
1427 assert(image->signature == MagickSignature);
1428 if (image->debug != MagickFalse)
1429 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1431 background=image->background_color;
1433 image_view=AcquireAuthenticCacheView(image,exception);
1434 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1435 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1436 dynamic_number_threads(image,width,height,1)
1438 for (y=0; y < (ssize_t) height; y++)
1462 if (status == MagickFalse)
1464 p=GetCacheViewAuthenticPixels(image_view,0,y_offset+y,image->columns,1,
1466 if (p == (Quantum *) NULL)
1471 p+=x_offset*GetPixelChannels(image);
1472 displacement=degrees*(double) (y-height/2.0);
1473 if (displacement == 0.0)
1475 if (displacement > 0.0)
1479 displacement*=(-1.0);
1482 step=(ssize_t) floor((double) displacement);
1483 area=(double) (displacement-step);
1486 GetPixelInfo(image,&source);
1487 GetPixelInfo(image,&destination);
1493 Transfer pixels left-to-right.
1495 if (step > x_offset)
1497 q=p-step*GetPixelChannels(image);
1498 for (i=0; i < (ssize_t) width; i++)
1500 if ((x_offset+i) < step)
1502 p+=GetPixelChannels(image);
1503 GetPixelInfoPixel(image,p,&pixel);
1504 q+=GetPixelChannels(image);
1507 GetPixelInfoPixel(image,p,&source);
1508 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1509 &source,(double) GetPixelAlpha(image,p),area,&destination);
1510 SetPixelInfoPixel(image,&destination,q);
1511 GetPixelInfoPixel(image,p,&pixel);
1512 p+=GetPixelChannels(image);
1513 q+=GetPixelChannels(image);
1515 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1516 &background,(double) background.alpha,area,&destination);
1517 SetPixelInfoPixel(image,&destination,q);
1518 q+=GetPixelChannels(image);
1519 for (i=0; i < (step-1); i++)
1521 SetPixelInfoPixel(image,&background,q);
1522 q+=GetPixelChannels(image);
1529 Transfer pixels right-to-left.
1531 p+=width*GetPixelChannels(image);
1532 q=p+step*GetPixelChannels(image);
1533 for (i=0; i < (ssize_t) width; i++)
1535 p-=GetPixelChannels(image);
1536 q-=GetPixelChannels(image);
1537 if ((size_t) (x_offset+width+step-i) >= image->columns)
1539 GetPixelInfoPixel(image,p,&source);
1540 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1541 &source,(double) GetPixelAlpha(image,p),area,&destination);
1542 SetPixelInfoPixel(image,&destination,q);
1543 GetPixelInfoPixel(image,p,&pixel);
1545 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1546 &background,(double) background.alpha,area,&destination);
1547 q-=GetPixelChannels(image);
1548 SetPixelInfoPixel(image,&destination,q);
1549 for (i=0; i < (step-1); i++)
1551 q-=GetPixelChannels(image);
1552 SetPixelInfoPixel(image,&background,q);
1557 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1559 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1564 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1565 #pragma omp critical (MagickCore_XShearImage)
1567 proceed=SetImageProgress(image,XShearImageTag,progress++,height);
1568 if (proceed == MagickFalse)
1572 image_view=DestroyCacheView(image_view);
1577 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1581 + Y S h e a r I m a g e %
1585 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1587 % YShearImage shears the image in the Y direction with a shear angle of
1588 % 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1589 % negative angles shear clockwise. Angles are measured relative to a
1590 % horizontal X-axis. Y shears will increase the height of an image creating
1591 % 'empty' triangles on the top and bottom of the source image.
1593 % The format of the YShearImage method is:
1595 % MagickBooleanType YShearImage(Image *image,const double degrees,
1596 % const size_t width,const size_t height,
1597 % const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
1599 % A description of each parameter follows.
1601 % o image: the image.
1603 % o degrees: A double representing the shearing angle along the Y
1606 % o width, height, x_offset, y_offset: Defines a region of the image
1609 % o exception: return any errors or warnings in this structure.
1612 static MagickBooleanType YShearImage(Image *image,const double degrees,
1613 const size_t width,const size_t height,const ssize_t x_offset,
1614 const ssize_t y_offset,ExceptionInfo *exception)
1616 #define YShearImageTag "YShear/Image"
1642 assert(image != (Image *) NULL);
1643 assert(image->signature == MagickSignature);
1644 if (image->debug != MagickFalse)
1645 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1648 background=image->background_color;
1649 image_view=AcquireAuthenticCacheView(image,exception);
1650 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1651 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1652 dynamic_number_threads(image,width,height,1)
1654 for (x=0; x < (ssize_t) width; x++)
1678 if (status == MagickFalse)
1680 p=GetCacheViewAuthenticPixels(image_view,x_offset+x,0,1,image->rows,
1682 if (p == (Quantum *) NULL)
1687 p+=y_offset*GetPixelChannels(image);
1688 displacement=degrees*(double) (x-width/2.0);
1689 if (displacement == 0.0)
1691 if (displacement > 0.0)
1695 displacement*=(-1.0);
1698 step=(ssize_t) floor((double) displacement);
1699 area=(double) (displacement-step);
1702 GetPixelInfo(image,&source);
1703 GetPixelInfo(image,&destination);
1709 Transfer pixels top-to-bottom.
1711 if (step > y_offset)
1713 q=p-step*GetPixelChannels(image);
1714 for (i=0; i < (ssize_t) height; i++)
1716 if ((y_offset+i) < step)
1718 p+=GetPixelChannels(image);
1719 GetPixelInfoPixel(image,p,&pixel);
1720 q+=GetPixelChannels(image);
1723 GetPixelInfoPixel(image,p,&source);
1724 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1725 &source,(double) GetPixelAlpha(image,p),area,
1727 SetPixelInfoPixel(image,&destination,q);
1728 GetPixelInfoPixel(image,p,&pixel);
1729 p+=GetPixelChannels(image);
1730 q+=GetPixelChannels(image);
1732 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1733 &background,(double) background.alpha,area,&destination);
1734 SetPixelInfoPixel(image,&destination,q);
1735 q+=GetPixelChannels(image);
1736 for (i=0; i < (step-1); i++)
1738 SetPixelInfoPixel(image,&background,q);
1739 q+=GetPixelChannels(image);
1746 Transfer pixels bottom-to-top.
1748 p+=height*GetPixelChannels(image);
1749 q=p+step*GetPixelChannels(image);
1750 for (i=0; i < (ssize_t) height; i++)
1752 p-=GetPixelChannels(image);
1753 q-=GetPixelChannels(image);
1754 if ((size_t) (y_offset+height+step-i) >= image->rows)
1756 GetPixelInfoPixel(image,p,&source);
1757 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1758 &source,(double) GetPixelAlpha(image,p),area,
1760 SetPixelInfoPixel(image,&destination,q);
1761 GetPixelInfoPixel(image,p,&pixel);
1763 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1764 &background,(double) background.alpha,area,&destination);
1765 q-=GetPixelChannels(image);
1766 SetPixelInfoPixel(image,&destination,q);
1767 for (i=0; i < (step-1); i++)
1769 q-=GetPixelChannels(image);
1770 SetPixelInfoPixel(image,&background,q);
1775 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1777 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1782 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1783 #pragma omp critical (MagickCore_YShearImage)
1785 proceed=SetImageProgress(image,YShearImageTag,progress++,image->rows);
1786 if (proceed == MagickFalse)
1790 image_view=DestroyCacheView(image_view);
1795 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1799 % S h e a r I m a g e %
1803 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1805 % ShearImage() creates a new image that is a shear_image copy of an existing
1806 % one. Shearing slides one edge of an image along the X or Y axis, creating
1807 % a parallelogram. An X direction shear slides an edge along the X axis,
1808 % while a Y direction shear slides an edge along the Y axis. The amount of
1809 % the shear is controlled by a shear angle. For X direction shears, x_shear
1810 % is measured relative to the Y axis, and similarly, for Y direction shears
1811 % y_shear is measured relative to the X axis. Empty triangles left over from
1812 % shearing the image are filled with the background color defined by member
1813 % 'background_color' of the image.. ShearImage() allocates the memory
1814 % necessary for the new Image structure and returns a pointer to the new image.
1816 % ShearImage() is based on the paper "A Fast Algorithm for General Raster
1817 % Rotatation" by Alan W. Paeth.
1819 % The format of the ShearImage method is:
1821 % Image *ShearImage(const Image *image,const double x_shear,
1822 % const double y_shear,ExceptionInfo *exception)
1824 % A description of each parameter follows.
1826 % o image: the image.
1828 % o x_shear, y_shear: Specifies the number of degrees to shear the image.
1830 % o exception: return any errors or warnings in this structure.
1833 MagickExport Image *ShearImage(const Image *image,const double x_shear,
1834 const double y_shear,ExceptionInfo *exception)
1856 assert(image != (Image *) NULL);
1857 assert(image->signature == MagickSignature);
1858 if (image->debug != MagickFalse)
1859 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1860 assert(exception != (ExceptionInfo *) NULL);
1861 assert(exception->signature == MagickSignature);
1862 if ((x_shear != 0.0) && (fmod(x_shear,90.0) == 0.0))
1863 ThrowImageException(ImageError,"AngleIsDiscontinuous");
1864 if ((y_shear != 0.0) && (fmod(y_shear,90.0) == 0.0))
1865 ThrowImageException(ImageError,"AngleIsDiscontinuous");
1867 Initialize shear angle.
1869 integral_image=CloneImage(image,0,0,MagickTrue,exception);
1870 if (integral_image == (Image *) NULL)
1871 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1872 shear.x=(-tan(DegreesToRadians(fmod(x_shear,360.0))));
1873 shear.y=tan(DegreesToRadians(fmod(y_shear,360.0)));
1874 if ((shear.x == 0.0) && (shear.y == 0.0))
1875 return(integral_image);
1876 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
1878 integral_image=DestroyImage(integral_image);
1879 return(integral_image);
1881 if (integral_image->alpha_trait != BlendPixelTrait)
1882 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
1886 y_width=image->columns+(ssize_t) floor(fabs(shear.x)*image->rows+0.5);
1887 x_offset=(ssize_t) ceil((double) image->columns+((fabs(shear.x)*image->rows)-
1888 image->columns)/2.0-0.5);
1889 y_offset=(ssize_t) ceil((double) image->rows+((fabs(shear.y)*y_width)-
1890 image->rows)/2.0-0.5);
1892 Surround image with border.
1894 integral_image->border_color=integral_image->background_color;
1895 integral_image->compose=CopyCompositeOp;
1896 border_info.width=(size_t) x_offset;
1897 border_info.height=(size_t) y_offset;
1898 shear_image=BorderImage(integral_image,&border_info,image->compose,exception);
1899 integral_image=DestroyImage(integral_image);
1900 if (shear_image == (Image *) NULL)
1901 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1905 if (shear_image->alpha_trait != BlendPixelTrait)
1906 (void) SetImageAlphaChannel(shear_image,OpaqueAlphaChannel,exception);
1907 status=XShearImage(shear_image,shear.x,image->columns,image->rows,x_offset,
1908 (ssize_t) (shear_image->rows-image->rows)/2,exception);
1909 if (status == MagickFalse)
1911 shear_image=DestroyImage(shear_image);
1912 return((Image *) NULL);
1914 status=YShearImage(shear_image,shear.y,y_width,image->rows,(ssize_t)
1915 (shear_image->columns-y_width)/2,y_offset,exception);
1916 if (status == MagickFalse)
1918 shear_image=DestroyImage(shear_image);
1919 return((Image *) NULL);
1921 status=CropToFitImage(&shear_image,shear.x,shear.y,(double)
1922 image->columns,(double) image->rows,MagickFalse,exception);
1923 if (status == MagickFalse)
1925 shear_image=DestroyImage(shear_image);
1926 return((Image *) NULL);
1928 shear_image->compose=image->compose;
1929 shear_image->page.width=0;
1930 shear_image->page.height=0;
1931 return(shear_image);
1935 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1939 % S h e a r R o t a t e I m a g e %
1943 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1945 % ShearRotateImage() creates a new image that is a rotated copy of an existing
1946 % one. Positive angles rotate counter-clockwise (right-hand rule), while
1947 % negative angles rotate clockwise. Rotated images are usually larger than
1948 % the originals and have 'empty' triangular corners. X axis. Empty
1949 % triangles left over from shearing the image are filled with the background
1950 % color defined by member 'background_color' of the image. ShearRotateImage
1951 % allocates the memory necessary for the new Image structure and returns a
1952 % pointer to the new image.
1954 % ShearRotateImage() is based on the paper "A Fast Algorithm for General
1955 % Raster Rotatation" by Alan W. Paeth. ShearRotateImage is adapted from a
1956 % similar method based on the Paeth paper written by Michael Halle of the
1957 % Spatial Imaging Group, MIT Media Lab.
1959 % The format of the ShearRotateImage method is:
1961 % Image *ShearRotateImage(const Image *image,const double degrees,
1962 % ExceptionInfo *exception)
1964 % A description of each parameter follows.
1966 % o image: the image.
1968 % o degrees: Specifies the number of degrees to rotate the image.
1970 % o exception: return any errors or warnings in this structure.
1973 MagickExport Image *ShearRotateImage(const Image *image,const double degrees,
1974 ExceptionInfo *exception)
2003 Adjust rotation angle.
2005 assert(image != (Image *) NULL);
2006 assert(image->signature == MagickSignature);
2007 if (image->debug != MagickFalse)
2008 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2009 assert(exception != (ExceptionInfo *) NULL);
2010 assert(exception->signature == MagickSignature);
2012 while (angle < -45.0)
2014 for (rotations=0; angle > 45.0; rotations++)
2018 Calculate shear equations.
2020 integral_image=IntegralRotateImage(image,rotations,exception);
2021 if (integral_image == (Image *) NULL)
2022 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2023 shear.x=(-tan((double) DegreesToRadians(angle)/2.0));
2024 shear.y=sin((double) DegreesToRadians(angle));
2025 if ((shear.x == 0.0) && (shear.y == 0.0))
2026 return(integral_image);
2027 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
2029 integral_image=DestroyImage(integral_image);
2030 return(integral_image);
2032 if (integral_image->alpha_trait != BlendPixelTrait)
2033 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
2037 width=image->columns;
2039 if ((rotations == 1) || (rotations == 3))
2042 height=image->columns;
2044 y_width=width+(ssize_t) floor(fabs(shear.x)*height+0.5);
2045 x_offset=(ssize_t) ceil((double) width+((fabs(shear.y)*height)-width)/2.0-
2047 y_offset=(ssize_t) ceil((double) height+((fabs(shear.y)*y_width)-height)/2.0-
2050 Surround image with a border.
2052 integral_image->border_color=integral_image->background_color;
2053 integral_image->compose=CopyCompositeOp;
2054 border_info.width=(size_t) x_offset;
2055 border_info.height=(size_t) y_offset;
2056 rotate_image=BorderImage(integral_image,&border_info,image->compose,
2058 integral_image=DestroyImage(integral_image);
2059 if (rotate_image == (Image *) NULL)
2060 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2064 status=XShearImage(rotate_image,shear.x,width,height,x_offset,(ssize_t)
2065 (rotate_image->rows-height)/2,exception);
2066 if (status == MagickFalse)
2068 rotate_image=DestroyImage(rotate_image);
2069 return((Image *) NULL);
2071 status=YShearImage(rotate_image,shear.y,y_width,height,(ssize_t)
2072 (rotate_image->columns-y_width)/2,y_offset,exception);
2073 if (status == MagickFalse)
2075 rotate_image=DestroyImage(rotate_image);
2076 return((Image *) NULL);
2078 status=XShearImage(rotate_image,shear.x,y_width,rotate_image->rows,(ssize_t)
2079 (rotate_image->columns-y_width)/2,0,exception);
2080 if (status == MagickFalse)
2082 rotate_image=DestroyImage(rotate_image);
2083 return((Image *) NULL);
2085 status=CropToFitImage(&rotate_image,shear.x,shear.y,(double) width,
2086 (double) height,MagickTrue,exception);
2087 if (status == MagickFalse)
2089 rotate_image=DestroyImage(rotate_image);
2090 return((Image *) NULL);
2092 rotate_image->compose=image->compose;
2093 rotate_image->page.width=0;
2094 rotate_image->page.height=0;
2095 return(rotate_image);