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/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(
256 radon_info->cells=(unsigned short *) UnmapBlob(radon_info->cells,
257 (size_t) radon_info->length);
258 RelinquishMagickResource(MemoryResource,radon_info->length);
263 radon_info->cells=(unsigned short *) UnmapBlob(radon_info->cells,(size_t)
265 RelinquishMagickResource(MapResource,radon_info->length);
269 if (radon_info->file != -1)
270 (void) close(radon_info->file);
271 (void) RelinquishUniqueFileResource(radon_info->path);
272 RelinquishMagickResource(DiskResource,radon_info->length);
278 return((RadonInfo *) RelinquishMagickMemory(radon_info));
281 static MagickBooleanType ResetRadonCells(RadonInfo *radon_info)
293 if (radon_info->type != DiskCache)
295 (void) ResetMagickMemory(radon_info->cells,0,(size_t) radon_info->length);
299 (void) lseek(radon_info->file,0,SEEK_SET);
300 for (y=0; y < (ssize_t) radon_info->height; y++)
302 for (x=0; x < (ssize_t) radon_info->width; x++)
304 count=write(radon_info->file,&value,sizeof(*radon_info->cells));
305 if (count != (ssize_t) sizeof(*radon_info->cells))
308 if (x < (ssize_t) radon_info->width)
311 return(y < (ssize_t) radon_info->height ? MagickFalse : MagickTrue);
314 static RadonInfo *AcquireRadonInfo(const Image *image,const size_t width,
315 const size_t height,ExceptionInfo *exception)
323 radon_info=(RadonInfo *) AcquireMagickMemory(sizeof(*radon_info));
324 if (radon_info == (RadonInfo *) NULL)
325 return((RadonInfo *) NULL);
326 (void) ResetMagickMemory(radon_info,0,sizeof(*radon_info));
327 radon_info->width=width;
328 radon_info->height=height;
329 radon_info->length=(MagickSizeType) width*height*sizeof(*radon_info->cells);
330 radon_info->type=MemoryCache;
331 status=AcquireMagickResource(AreaResource,radon_info->length);
332 if ((status != MagickFalse) &&
333 (radon_info->length == (MagickSizeType) ((size_t) radon_info->length)))
335 status=AcquireMagickResource(MemoryResource,radon_info->length);
336 if (status != MagickFalse)
338 radon_info->mapped=MagickFalse;
339 radon_info->cells=(unsigned short *) AcquireMagickMemory((size_t)
341 if (radon_info->cells == (unsigned short *) NULL)
343 radon_info->mapped=MagickTrue;
344 radon_info->cells=(unsigned short *) MapBlob(-1,IOMode,0,(size_t)
347 if (radon_info->cells == (unsigned short *) NULL)
348 RelinquishMagickResource(MemoryResource,radon_info->length);
351 radon_info->file=(-1);
352 if (radon_info->cells == (unsigned short *) NULL)
354 status=AcquireMagickResource(DiskResource,radon_info->length);
355 if (status == MagickFalse)
357 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
358 "CacheResourcesExhausted","`%s'",image->filename);
359 return(DestroyRadonInfo(radon_info));
361 radon_info->type=DiskCache;
362 (void) AcquireMagickResource(MemoryResource,radon_info->length);
363 radon_info->file=AcquireUniqueFileResource(radon_info->path);
364 if (radon_info->file == -1)
365 return(DestroyRadonInfo(radon_info));
366 status=AcquireMagickResource(MapResource,radon_info->length);
367 if (status != MagickFalse)
369 status=ResetRadonCells(radon_info);
370 if (status != MagickFalse)
372 radon_info->cells=(unsigned short *) MapBlob(radon_info->file,
373 IOMode,0,(size_t) radon_info->length);
374 if (radon_info->cells != (unsigned short *) NULL)
375 radon_info->type=MapCache;
377 RelinquishMagickResource(MapResource,radon_info->length);
384 static inline size_t MagickMin(const size_t x,const size_t y)
391 static inline ssize_t ReadRadonCell(const RadonInfo *radon_info,
392 const MagickOffsetType offset,const size_t length,unsigned char *buffer)
400 #if !defined(MAGICKCORE_HAVE_PPREAD)
401 #if defined(MAGICKCORE_OPENMP_SUPPORT)
402 #pragma omp critical (MagickCore_ReadRadonCell)
406 if (lseek(radon_info->file,offset,SEEK_SET) >= 0)
410 for (i=0; i < (ssize_t) length; i+=count)
412 #if !defined(MAGICKCORE_HAVE_PPREAD)
413 count=read(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
416 count=pread(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
417 SSIZE_MAX),offset+i);
428 #if !defined(MAGICKCORE_HAVE_PPREAD)
435 static inline ssize_t WriteRadonCell(const RadonInfo *radon_info,
436 const MagickOffsetType offset,const size_t length,const unsigned char *buffer)
444 #if !defined(MAGICKCORE_HAVE_PWRITE)
445 #if defined(MAGICKCORE_OPENMP_SUPPORT)
446 #pragma omp critical (MagickCore_WriteRadonCell)
449 if (lseek(radon_info->file,offset,SEEK_SET) >= 0)
453 for (i=0; i < (ssize_t) length; i+=count)
455 #if !defined(MAGICKCORE_HAVE_PWRITE)
456 count=write(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
459 count=pwrite(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
460 SSIZE_MAX),offset+i);
471 #if !defined(MAGICKCORE_HAVE_PWRITE)
478 static inline unsigned short GetRadonCell(const RadonInfo *radon_info,
479 const ssize_t x,const ssize_t y)
487 i=(MagickOffsetType) radon_info->height*x+y;
489 ((MagickSizeType) (i*sizeof(*radon_info->cells)) >= radon_info->length))
491 if (radon_info->type != DiskCache)
492 return(radon_info->cells[i]);
494 (void) ReadRadonCell(radon_info,i*sizeof(*radon_info->cells),
495 sizeof(*radon_info->cells),(unsigned char *) &value);
499 static inline MagickBooleanType SetRadonCell(const RadonInfo *radon_info,
500 const ssize_t x,const ssize_t y,const unsigned short value)
508 i=(MagickOffsetType) radon_info->height*x+y;
510 ((MagickSizeType) (i*sizeof(*radon_info->cells)) >= radon_info->length))
512 if (radon_info->type != DiskCache)
514 radon_info->cells[i]=value;
517 count=WriteRadonCell(radon_info,i*sizeof(*radon_info->cells),
518 sizeof(*radon_info->cells),(const unsigned char *) &value);
519 if (count != (ssize_t) sizeof(*radon_info->cells))
524 static void RadonProjection(const Image *image,RadonInfo *source_cells,
525 RadonInfo *destination_cells,const ssize_t sign,size_t *projection)
540 assert(image != (Image *) NULL);
543 for (step=1; step < p->width; step*=2)
545 for (x=0; x < (ssize_t) p->width; x+=2*(ssize_t) step)
556 for (i=0; i < (ssize_t) step; i++)
558 for (y=0; y < (ssize_t) (p->height-i-1); y++)
560 cell=GetRadonCell(p,x+i,y);
561 (void) SetRadonCell(q,x+2*i,y,cell+GetRadonCell(p,x+i+(ssize_t)
563 (void) SetRadonCell(q,x+2*i+1,y,cell+GetRadonCell(p,x+i+(ssize_t)
566 for ( ; y < (ssize_t) (p->height-i); y++)
568 cell=GetRadonCell(p,x+i,y);
569 (void) SetRadonCell(q,x+2*i,y,cell+GetRadonCell(p,x+i+(ssize_t) step,
571 (void) SetRadonCell(q,x+2*i+1,y,cell);
573 for ( ; y < (ssize_t) p->height; y++)
575 cell=GetRadonCell(p,x+i,y);
576 (void) SetRadonCell(q,x+2*i,y,cell);
577 (void) SetRadonCell(q,x+2*i+1,y,cell);
585 #if defined(MAGICKCORE_OPENMP_SUPPORT)
586 #pragma omp parallel for schedule(static,4) \
587 magick_threads(image,image,1,1)
589 for (x=0; x < (ssize_t) p->width; x++)
598 for (y=0; y < (ssize_t) (p->height-1); y++)
603 delta=GetRadonCell(p,x,y)-(ssize_t) GetRadonCell(p,x,y+1);
606 projection[p->width+sign*x-1]=sum;
610 static MagickBooleanType RadonTransform(const Image *image,
611 const double threshold,size_t *projection,ExceptionInfo *exception)
639 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
640 source_cells=AcquireRadonInfo(image,width,image->rows,exception);
641 destination_cells=AcquireRadonInfo(image,width,image->rows,exception);
642 if ((source_cells == (RadonInfo *) NULL) ||
643 (destination_cells == (RadonInfo *) NULL))
645 if (destination_cells != (RadonInfo *) NULL)
646 destination_cells=DestroyRadonInfo(destination_cells);
647 if (source_cells != (RadonInfo *) NULL)
648 source_cells=DestroyRadonInfo(source_cells);
651 if (ResetRadonCells(source_cells) == MagickFalse)
653 destination_cells=DestroyRadonInfo(destination_cells);
654 source_cells=DestroyRadonInfo(source_cells);
657 for (i=0; i < 256; i++)
659 byte=(unsigned char) i;
660 for (count=0; byte != 0; byte>>=1)
662 bits[i]=(unsigned short) count;
665 image_view=AcquireVirtualCacheView(image,exception);
666 #if defined(MAGICKCORE_OPENMP_SUPPORT)
667 #pragma omp parallel for schedule(static,4) shared(status) \
668 magick_threads(image,image,image->rows,1)
670 for (y=0; y < (ssize_t) image->rows; y++)
672 register const Quantum
683 if (status == MagickFalse)
685 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
686 if (p == (const Quantum *) NULL)
693 i=(ssize_t) (image->columns+7)/8;
694 for (x=0; x < (ssize_t) image->columns; x++)
697 if (GetPixelIntensity(image,p) < threshold)
702 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
706 p+=GetPixelChannels(image);
711 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
714 RadonProjection(image,source_cells,destination_cells,-1,projection);
715 (void) ResetRadonCells(source_cells);
716 #if defined(MAGICKCORE_OPENMP_SUPPORT)
717 #pragma omp parallel for schedule(static,4) shared(status) \
718 magick_threads(image,image,1,1)
720 for (y=0; y < (ssize_t) image->rows; y++)
722 register const Quantum
733 if (status == MagickFalse)
735 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
736 if (p == (const Quantum *) NULL)
744 for (x=0; x < (ssize_t) image->columns; x++)
747 if (GetPixelIntensity(image,p) < threshold)
752 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
756 p+=GetPixelChannels(image);
761 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
764 RadonProjection(image,source_cells,destination_cells,1,projection);
765 image_view=DestroyCacheView(image_view);
766 destination_cells=DestroyRadonInfo(destination_cells);
767 source_cells=DestroyRadonInfo(source_cells);
771 static void GetImageBackgroundColor(Image *image,const ssize_t offset,
772 ExceptionInfo *exception)
787 Compute average background color.
791 GetPixelInfo(image,&background);
793 image_view=AcquireVirtualCacheView(image,exception);
794 for (y=0; y < (ssize_t) image->rows; y++)
796 register const Quantum
802 if ((y >= offset) && (y < ((ssize_t) image->rows-offset)))
804 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
805 if (p == (const Quantum *) NULL)
807 for (x=0; x < (ssize_t) image->columns; x++)
809 if ((x >= offset) && (x < ((ssize_t) image->columns-offset)))
811 background.red+=QuantumScale*GetPixelRed(image,p);
812 background.green+=QuantumScale*GetPixelGreen(image,p);
813 background.blue+=QuantumScale*GetPixelBlue(image,p);
814 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
815 background.alpha+=QuantumScale*GetPixelAlpha(image,p);
817 p+=GetPixelChannels(image);
820 image_view=DestroyCacheView(image_view);
821 image->background_color.red=(double) ClampToQuantum(QuantumRange*
822 background.red/count);
823 image->background_color.green=(double) ClampToQuantum(QuantumRange*
824 background.green/count);
825 image->background_color.blue=(double) ClampToQuantum(QuantumRange*
826 background.blue/count);
827 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
828 image->background_color.alpha=(double) ClampToQuantum(QuantumRange*
829 background.alpha/count);
832 MagickExport Image *DeskewImage(const Image *image,const double threshold,
833 ExceptionInfo *exception)
868 Compute deskew angle.
870 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
871 projection=(size_t *) AcquireQuantumMemory((size_t) (2*width-1),
872 sizeof(*projection));
873 if (projection == (size_t *) NULL)
874 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
875 status=RadonTransform(image,threshold,projection,exception);
876 if (status == MagickFalse)
878 projection=(size_t *) RelinquishMagickMemory(projection);
879 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
883 for (i=0; i < (ssize_t) (2*width-1); i++)
885 if (projection[i] > max_projection)
887 skew=i-(ssize_t) width+1;
888 max_projection=projection[i];
891 projection=(size_t *) RelinquishMagickMemory(projection);
892 degrees=RadiansToDegrees(-atan((double) skew/width/8));
893 if (image->debug != MagickFalse)
894 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
895 " Deskew angle: %g",degrees);
899 clone_image=CloneImage(image,0,0,MagickTrue,exception);
900 if (clone_image == (Image *) NULL)
901 return((Image *) NULL);
904 angle[MaxTextExtent];
906 (void) FormatLocaleString(angle,MaxTextExtent,"%.20g",degrees);
907 (void) SetImageArtifact(clone_image,"deskew:angle",angle);
909 (void) SetImageVirtualPixelMethod(clone_image,BackgroundVirtualPixelMethod,
911 affine_matrix.sx=cos(DegreesToRadians(fmod((double) degrees,360.0)));
912 affine_matrix.rx=sin(DegreesToRadians(fmod((double) degrees,360.0)));
913 affine_matrix.ry=(-sin(DegreesToRadians(fmod((double) degrees,360.0))));
914 affine_matrix.sy=cos(DegreesToRadians(fmod((double) degrees,360.0)));
915 affine_matrix.tx=0.0;
916 affine_matrix.ty=0.0;
917 artifact=GetImageArtifact(image,"deskew:auto-crop");
918 if (artifact == (const char *) NULL)
920 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
921 clone_image=DestroyImage(clone_image);
922 return(deskew_image);
927 GetImageBackgroundColor(clone_image,(ssize_t) StringToLong(artifact),
929 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
930 clone_image=DestroyImage(clone_image);
931 if (deskew_image == (Image *) NULL)
932 return((Image *) NULL);
933 median_image=StatisticImage(deskew_image,MedianStatistic,3,3,exception);
934 if (median_image == (Image *) NULL)
936 deskew_image=DestroyImage(deskew_image);
937 return((Image *) NULL);
939 geometry=GetImageBoundingBox(median_image,exception);
940 median_image=DestroyImage(median_image);
941 if (image->debug != MagickFalse)
942 (void) LogMagickEvent(TransformEvent,GetMagickModule()," Deskew geometry: "
943 "%.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
944 geometry.height,(double) geometry.x,(double) geometry.y);
945 crop_image=CropImage(deskew_image,&geometry,exception);
946 deskew_image=DestroyImage(deskew_image);
951 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
955 % I n t e g r a l R o t a t e I m a g e %
959 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
961 % IntegralRotateImage() rotates the image an integral of 90 degrees. It
962 % allocates the memory necessary for the new Image structure and returns a
963 % pointer to the rotated image.
965 % The format of the IntegralRotateImage method is:
967 % Image *IntegralRotateImage(const Image *image,size_t rotations,
968 % ExceptionInfo *exception)
970 % A description of each parameter follows.
972 % o image: the image.
974 % o rotations: Specifies the number of 90 degree rotations.
977 MagickExport Image *IntegralRotateImage(const Image *image,size_t rotations,
978 ExceptionInfo *exception)
980 #define RotateImageTag "Rotate/Image"
1002 Initialize rotated image attributes.
1004 assert(image != (Image *) NULL);
1008 return(CloneImage(image,0,0,MagickTrue,exception));
1009 if ((rotations == 1) || (rotations == 3))
1010 rotate_image=CloneImage(image,image->rows,image->columns,MagickTrue,
1013 rotate_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1015 if (rotate_image == (Image *) NULL)
1016 return((Image *) NULL);
1018 Integral rotate the image.
1022 image_view=AcquireVirtualCacheView(image,exception);
1023 rotate_view=AcquireAuthenticCacheView(rotate_image,exception);
1045 GetPixelCacheTileSize(image,&tile_width,&tile_height);
1046 tile_width=image->columns;
1047 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1048 #pragma omp parallel for schedule(static,4) shared(status) \
1049 magick_threads(image,image,1,1)
1051 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
1056 if (status == MagickFalse)
1059 for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
1064 register const Quantum
1078 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
1079 width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
1081 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
1082 height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
1083 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1085 if (p == (const Quantum *) NULL)
1090 for (y=0; y < (ssize_t) width; y++)
1092 register const Quantum
1093 *restrict tile_pixels;
1098 if (status == MagickFalse)
1100 q=QueueCacheViewAuthenticPixels(rotate_view,(ssize_t)
1101 (rotate_image->columns-(tile_y+height)),y+tile_x,height,1,
1103 if (q == (Quantum *) NULL)
1108 tile_pixels=p+((height-1)*width+y)*GetPixelChannels(image);
1109 for (x=0; x < (ssize_t) height; x++)
1114 if (GetPixelReadMask(image,tile_pixels) == 0)
1116 tile_pixels-=width*GetPixelChannels(image);
1117 q+=GetPixelChannels(rotate_image);
1120 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1122 PixelChannel channel=GetPixelChannelChannel(image,i);
1123 PixelTrait traits=GetPixelChannelTraits(image,channel);
1124 PixelTrait rotate_traits=GetPixelChannelTraits(rotate_image,
1126 if ((traits == UndefinedPixelTrait) ||
1127 (rotate_traits == UndefinedPixelTrait))
1129 SetPixelChannel(rotate_image,channel,tile_pixels[i],q);
1131 tile_pixels-=width*GetPixelChannels(image);
1132 q+=GetPixelChannels(rotate_image);
1134 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1135 if (sync == MagickFalse)
1139 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1144 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1145 #pragma omp critical (MagickCore_IntegralRotateImage)
1147 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
1149 if (proceed == MagickFalse)
1153 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1154 image->rows-1,image->rows);
1155 Swap(page.width,page.height);
1156 Swap(page.x,page.y);
1157 if (page.width != 0)
1158 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1166 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1167 #pragma omp parallel for schedule(static,4) shared(status) \
1168 magick_threads(image,image,1,1)
1170 for (y=0; y < (ssize_t) image->rows; y++)
1175 register const Quantum
1184 if (status == MagickFalse)
1186 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1187 q=QueueCacheViewAuthenticPixels(rotate_view,0,(ssize_t) (image->rows-y-
1188 1),image->columns,1,exception);
1189 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1194 q+=GetPixelChannels(rotate_image)*image->columns;
1195 for (x=0; x < (ssize_t) image->columns; x++)
1200 q-=GetPixelChannels(rotate_image);
1201 if (GetPixelReadMask(image,p) == 0)
1203 p+=GetPixelChannels(image);
1206 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1208 PixelChannel channel=GetPixelChannelChannel(image,i);
1209 PixelTrait traits=GetPixelChannelTraits(image,channel);
1210 PixelTrait rotate_traits=GetPixelChannelTraits(rotate_image,
1212 if ((traits == UndefinedPixelTrait) ||
1213 (rotate_traits == UndefinedPixelTrait))
1215 SetPixelChannel(rotate_image,channel,p[i],q);
1217 p+=GetPixelChannels(image);
1219 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1220 if (sync == MagickFalse)
1222 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1227 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1228 #pragma omp critical (MagickCore_IntegralRotateImage)
1230 proceed=SetImageProgress(image,RotateImageTag,progress++,
1232 if (proceed == MagickFalse)
1236 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1237 image->rows-1,image->rows);
1238 Swap(page.width,page.height);
1239 Swap(page.x,page.y);
1240 if (page.width != 0)
1241 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1256 GetPixelCacheTileSize(image,&tile_width,&tile_height);
1257 tile_width=image->columns;
1258 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1259 #pragma omp parallel for schedule(static,4) shared(status) \
1260 magick_threads(image,image,1,1)
1262 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
1267 if (status == MagickFalse)
1270 for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
1275 register const Quantum
1289 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
1290 width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
1292 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
1293 height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
1294 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1296 if (p == (const Quantum *) NULL)
1301 for (y=0; y < (ssize_t) width; y++)
1303 register const Quantum
1304 *restrict tile_pixels;
1309 if (status == MagickFalse)
1311 q=QueueCacheViewAuthenticPixels(rotate_view,tile_y,(ssize_t) (y+
1312 rotate_image->rows-(tile_x+width)),height,1,exception);
1313 if (q == (Quantum *) NULL)
1318 tile_pixels=p+((width-1)-y)*GetPixelChannels(image);
1319 for (x=0; x < (ssize_t) height; x++)
1324 if (GetPixelReadMask(image,tile_pixels) == 0)
1326 tile_pixels+=width*GetPixelChannels(image);
1327 q+=GetPixelChannels(rotate_image);
1330 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1332 PixelChannel channel=GetPixelChannelChannel(image,i);
1333 PixelTrait traits=GetPixelChannelTraits(image,channel);
1334 PixelTrait rotate_traits=GetPixelChannelTraits(rotate_image,
1336 if ((traits == UndefinedPixelTrait) ||
1337 (rotate_traits == UndefinedPixelTrait))
1339 SetPixelChannel(rotate_image,channel,tile_pixels[i],q);
1341 tile_pixels+=width*GetPixelChannels(image);
1342 q+=GetPixelChannels(rotate_image);
1344 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1345 #pragma omp critical (MagickCore_IntegralRotateImage)
1347 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1348 if (sync == MagickFalse)
1352 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1357 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
1359 if (proceed == MagickFalse)
1363 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1364 image->rows-1,image->rows);
1365 Swap(page.width,page.height);
1366 Swap(page.x,page.y);
1367 if (page.width != 0)
1368 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1372 rotate_view=DestroyCacheView(rotate_view);
1373 image_view=DestroyCacheView(image_view);
1374 rotate_image->type=image->type;
1375 rotate_image->page=page;
1376 if (status == MagickFalse)
1377 rotate_image=DestroyImage(rotate_image);
1378 return(rotate_image);
1382 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1386 + X S h e a r I m a g e %
1390 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1392 % XShearImage() shears the image in the X direction with a shear angle of
1393 % 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1394 % negative angles shear clockwise. Angles are measured relative to a vertical
1395 % Y-axis. X shears will widen an image creating 'empty' triangles on the left
1396 % and right sides of the source image.
1398 % The format of the XShearImage method is:
1400 % MagickBooleanType XShearImage(Image *image,const double degrees,
1401 % const size_t width,const size_t height,
1402 % const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
1404 % A description of each parameter follows.
1406 % o image: the image.
1408 % o degrees: A double representing the shearing angle along the X
1411 % o width, height, x_offset, y_offset: Defines a region of the image
1414 % o exception: return any errors or warnings in this structure.
1417 static MagickBooleanType XShearImage(Image *image,const double degrees,
1418 const size_t width,const size_t height,const ssize_t x_offset,
1419 const ssize_t y_offset,ExceptionInfo *exception)
1421 #define XShearImageTag "XShear/Image"
1447 assert(image != (Image *) NULL);
1448 assert(image->signature == MagickSignature);
1449 if (image->debug != MagickFalse)
1450 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1452 background=image->background_color;
1454 image_view=AcquireAuthenticCacheView(image,exception);
1455 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1456 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1457 magick_threads(image,image,height,1)
1459 for (y=0; y < (ssize_t) height; y++)
1483 if (status == MagickFalse)
1485 p=GetCacheViewAuthenticPixels(image_view,0,y_offset+y,image->columns,1,
1487 if (p == (Quantum *) NULL)
1492 p+=x_offset*GetPixelChannels(image);
1493 displacement=degrees*(double) (y-height/2.0);
1494 if (displacement == 0.0)
1496 if (displacement > 0.0)
1500 displacement*=(-1.0);
1503 step=(ssize_t) floor((double) displacement);
1504 area=(double) (displacement-step);
1507 GetPixelInfo(image,&source);
1508 GetPixelInfo(image,&destination);
1514 Transfer pixels left-to-right.
1516 if (step > x_offset)
1518 q=p-step*GetPixelChannels(image);
1519 for (i=0; i < (ssize_t) width; i++)
1521 if ((x_offset+i) < step)
1523 p+=GetPixelChannels(image);
1524 GetPixelInfoPixel(image,p,&pixel);
1525 q+=GetPixelChannels(image);
1528 GetPixelInfoPixel(image,p,&source);
1529 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1530 &source,(double) GetPixelAlpha(image,p),area,&destination);
1531 SetPixelInfoPixel(image,&destination,q);
1532 GetPixelInfoPixel(image,p,&pixel);
1533 p+=GetPixelChannels(image);
1534 q+=GetPixelChannels(image);
1536 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1537 &background,(double) background.alpha,area,&destination);
1538 SetPixelInfoPixel(image,&destination,q);
1539 q+=GetPixelChannels(image);
1540 for (i=0; i < (step-1); i++)
1542 SetPixelInfoPixel(image,&background,q);
1543 q+=GetPixelChannels(image);
1550 Transfer pixels right-to-left.
1552 p+=width*GetPixelChannels(image);
1553 q=p+step*GetPixelChannels(image);
1554 for (i=0; i < (ssize_t) width; i++)
1556 p-=GetPixelChannels(image);
1557 q-=GetPixelChannels(image);
1558 if ((size_t) (x_offset+width+step-i) >= image->columns)
1560 GetPixelInfoPixel(image,p,&source);
1561 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1562 &source,(double) GetPixelAlpha(image,p),area,&destination);
1563 SetPixelInfoPixel(image,&destination,q);
1564 GetPixelInfoPixel(image,p,&pixel);
1566 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1567 &background,(double) background.alpha,area,&destination);
1568 q-=GetPixelChannels(image);
1569 SetPixelInfoPixel(image,&destination,q);
1570 for (i=0; i < (step-1); i++)
1572 q-=GetPixelChannels(image);
1573 SetPixelInfoPixel(image,&background,q);
1578 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1580 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1585 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1586 #pragma omp critical (MagickCore_XShearImage)
1588 proceed=SetImageProgress(image,XShearImageTag,progress++,height);
1589 if (proceed == MagickFalse)
1593 image_view=DestroyCacheView(image_view);
1598 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1602 + Y S h e a r I m a g e %
1606 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1608 % YShearImage shears the image in the Y direction with a shear angle of
1609 % 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1610 % negative angles shear clockwise. Angles are measured relative to a
1611 % horizontal X-axis. Y shears will increase the height of an image creating
1612 % 'empty' triangles on the top and bottom of the source image.
1614 % The format of the YShearImage method is:
1616 % MagickBooleanType YShearImage(Image *image,const double degrees,
1617 % const size_t width,const size_t height,
1618 % const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
1620 % A description of each parameter follows.
1622 % o image: the image.
1624 % o degrees: A double representing the shearing angle along the Y
1627 % o width, height, x_offset, y_offset: Defines a region of the image
1630 % o exception: return any errors or warnings in this structure.
1633 static MagickBooleanType YShearImage(Image *image,const double degrees,
1634 const size_t width,const size_t height,const ssize_t x_offset,
1635 const ssize_t y_offset,ExceptionInfo *exception)
1637 #define YShearImageTag "YShear/Image"
1663 assert(image != (Image *) NULL);
1664 assert(image->signature == MagickSignature);
1665 if (image->debug != MagickFalse)
1666 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1669 background=image->background_color;
1670 image_view=AcquireAuthenticCacheView(image,exception);
1671 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1672 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1673 magick_threads(image,image,width,1)
1675 for (x=0; x < (ssize_t) width; x++)
1699 if (status == MagickFalse)
1701 p=GetCacheViewAuthenticPixels(image_view,x_offset+x,0,1,image->rows,
1703 if (p == (Quantum *) NULL)
1708 p+=y_offset*GetPixelChannels(image);
1709 displacement=degrees*(double) (x-width/2.0);
1710 if (displacement == 0.0)
1712 if (displacement > 0.0)
1716 displacement*=(-1.0);
1719 step=(ssize_t) floor((double) displacement);
1720 area=(double) (displacement-step);
1723 GetPixelInfo(image,&source);
1724 GetPixelInfo(image,&destination);
1730 Transfer pixels top-to-bottom.
1732 if (step > y_offset)
1734 q=p-step*GetPixelChannels(image);
1735 for (i=0; i < (ssize_t) height; i++)
1737 if ((y_offset+i) < step)
1739 p+=GetPixelChannels(image);
1740 GetPixelInfoPixel(image,p,&pixel);
1741 q+=GetPixelChannels(image);
1744 GetPixelInfoPixel(image,p,&source);
1745 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1746 &source,(double) GetPixelAlpha(image,p),area,
1748 SetPixelInfoPixel(image,&destination,q);
1749 GetPixelInfoPixel(image,p,&pixel);
1750 p+=GetPixelChannels(image);
1751 q+=GetPixelChannels(image);
1753 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1754 &background,(double) background.alpha,area,&destination);
1755 SetPixelInfoPixel(image,&destination,q);
1756 q+=GetPixelChannels(image);
1757 for (i=0; i < (step-1); i++)
1759 SetPixelInfoPixel(image,&background,q);
1760 q+=GetPixelChannels(image);
1767 Transfer pixels bottom-to-top.
1769 p+=height*GetPixelChannels(image);
1770 q=p+step*GetPixelChannels(image);
1771 for (i=0; i < (ssize_t) height; i++)
1773 p-=GetPixelChannels(image);
1774 q-=GetPixelChannels(image);
1775 if ((size_t) (y_offset+height+step-i) >= image->rows)
1777 GetPixelInfoPixel(image,p,&source);
1778 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1779 &source,(double) GetPixelAlpha(image,p),area,
1781 SetPixelInfoPixel(image,&destination,q);
1782 GetPixelInfoPixel(image,p,&pixel);
1784 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1785 &background,(double) background.alpha,area,&destination);
1786 q-=GetPixelChannels(image);
1787 SetPixelInfoPixel(image,&destination,q);
1788 for (i=0; i < (step-1); i++)
1790 q-=GetPixelChannels(image);
1791 SetPixelInfoPixel(image,&background,q);
1796 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1798 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1803 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1804 #pragma omp critical (MagickCore_YShearImage)
1806 proceed=SetImageProgress(image,YShearImageTag,progress++,image->rows);
1807 if (proceed == MagickFalse)
1811 image_view=DestroyCacheView(image_view);
1816 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1820 % S h e a r I m a g e %
1824 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1826 % ShearImage() creates a new image that is a shear_image copy of an existing
1827 % one. Shearing slides one edge of an image along the X or Y axis, creating
1828 % a parallelogram. An X direction shear slides an edge along the X axis,
1829 % while a Y direction shear slides an edge along the Y axis. The amount of
1830 % the shear is controlled by a shear angle. For X direction shears, x_shear
1831 % is measured relative to the Y axis, and similarly, for Y direction shears
1832 % y_shear is measured relative to the X axis. Empty triangles left over from
1833 % shearing the image are filled with the background color defined by member
1834 % 'background_color' of the image.. ShearImage() allocates the memory
1835 % necessary for the new Image structure and returns a pointer to the new image.
1837 % ShearImage() is based on the paper "A Fast Algorithm for General Raster
1838 % Rotatation" by Alan W. Paeth.
1840 % The format of the ShearImage method is:
1842 % Image *ShearImage(const Image *image,const double x_shear,
1843 % const double y_shear,ExceptionInfo *exception)
1845 % A description of each parameter follows.
1847 % o image: the image.
1849 % o x_shear, y_shear: Specifies the number of degrees to shear the image.
1851 % o exception: return any errors or warnings in this structure.
1854 MagickExport Image *ShearImage(const Image *image,const double x_shear,
1855 const double y_shear,ExceptionInfo *exception)
1877 assert(image != (Image *) NULL);
1878 assert(image->signature == MagickSignature);
1879 if (image->debug != MagickFalse)
1880 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1881 assert(exception != (ExceptionInfo *) NULL);
1882 assert(exception->signature == MagickSignature);
1883 if ((x_shear != 0.0) && (fmod(x_shear,90.0) == 0.0))
1884 ThrowImageException(ImageError,"AngleIsDiscontinuous");
1885 if ((y_shear != 0.0) && (fmod(y_shear,90.0) == 0.0))
1886 ThrowImageException(ImageError,"AngleIsDiscontinuous");
1888 Initialize shear angle.
1890 integral_image=CloneImage(image,0,0,MagickTrue,exception);
1891 if (integral_image == (Image *) NULL)
1892 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1893 shear.x=(-tan(DegreesToRadians(fmod(x_shear,360.0))));
1894 shear.y=tan(DegreesToRadians(fmod(y_shear,360.0)));
1895 if ((shear.x == 0.0) && (shear.y == 0.0))
1896 return(integral_image);
1897 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
1899 integral_image=DestroyImage(integral_image);
1900 return(integral_image);
1902 if (integral_image->alpha_trait != BlendPixelTrait)
1903 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
1907 y_width=image->columns+(ssize_t) floor(fabs(shear.x)*image->rows+0.5);
1908 x_offset=(ssize_t) ceil((double) image->columns+((fabs(shear.x)*image->rows)-
1909 image->columns)/2.0-0.5);
1910 y_offset=(ssize_t) ceil((double) image->rows+((fabs(shear.y)*y_width)-
1911 image->rows)/2.0-0.5);
1913 Surround image with border.
1915 integral_image->border_color=integral_image->background_color;
1916 integral_image->compose=CopyCompositeOp;
1917 border_info.width=(size_t) x_offset;
1918 border_info.height=(size_t) y_offset;
1919 shear_image=BorderImage(integral_image,&border_info,image->compose,exception);
1920 integral_image=DestroyImage(integral_image);
1921 if (shear_image == (Image *) NULL)
1922 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1926 if (shear_image->alpha_trait != BlendPixelTrait)
1927 (void) SetImageAlphaChannel(shear_image,OpaqueAlphaChannel,exception);
1928 status=XShearImage(shear_image,shear.x,image->columns,image->rows,x_offset,
1929 (ssize_t) (shear_image->rows-image->rows)/2,exception);
1930 if (status == MagickFalse)
1932 shear_image=DestroyImage(shear_image);
1933 return((Image *) NULL);
1935 status=YShearImage(shear_image,shear.y,y_width,image->rows,(ssize_t)
1936 (shear_image->columns-y_width)/2,y_offset,exception);
1937 if (status == MagickFalse)
1939 shear_image=DestroyImage(shear_image);
1940 return((Image *) NULL);
1942 status=CropToFitImage(&shear_image,shear.x,shear.y,(double)
1943 image->columns,(double) image->rows,MagickFalse,exception);
1944 shear_image->compose=image->compose;
1945 shear_image->page.width=0;
1946 shear_image->page.height=0;
1947 if (status == MagickFalse)
1948 shear_image=DestroyImage(shear_image);
1949 return(shear_image);
1953 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1957 % S h e a r R o t a t e I m a g e %
1961 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1963 % ShearRotateImage() creates a new image that is a rotated copy of an existing
1964 % one. Positive angles rotate counter-clockwise (right-hand rule), while
1965 % negative angles rotate clockwise. Rotated images are usually larger than
1966 % the originals and have 'empty' triangular corners. X axis. Empty
1967 % triangles left over from shearing the image are filled with the background
1968 % color defined by member 'background_color' of the image. ShearRotateImage
1969 % allocates the memory necessary for the new Image structure and returns a
1970 % pointer to the new image.
1972 % ShearRotateImage() is based on the paper "A Fast Algorithm for General
1973 % Raster Rotatation" by Alan W. Paeth. ShearRotateImage is adapted from a
1974 % similar method based on the Paeth paper written by Michael Halle of the
1975 % Spatial Imaging Group, MIT Media Lab.
1977 % The format of the ShearRotateImage method is:
1979 % Image *ShearRotateImage(const Image *image,const double degrees,
1980 % ExceptionInfo *exception)
1982 % A description of each parameter follows.
1984 % o image: the image.
1986 % o degrees: Specifies the number of degrees to rotate the image.
1988 % o exception: return any errors or warnings in this structure.
1991 MagickExport Image *ShearRotateImage(const Image *image,const double degrees,
1992 ExceptionInfo *exception)
2021 Adjust rotation angle.
2023 assert(image != (Image *) NULL);
2024 assert(image->signature == MagickSignature);
2025 if (image->debug != MagickFalse)
2026 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2027 assert(exception != (ExceptionInfo *) NULL);
2028 assert(exception->signature == MagickSignature);
2030 while (angle < -45.0)
2032 for (rotations=0; angle > 45.0; rotations++)
2036 Calculate shear equations.
2038 integral_image=IntegralRotateImage(image,rotations,exception);
2039 if (integral_image == (Image *) NULL)
2040 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2041 shear.x=(-tan((double) DegreesToRadians(angle)/2.0));
2042 shear.y=sin((double) DegreesToRadians(angle));
2043 if ((shear.x == 0.0) && (shear.y == 0.0))
2044 return(integral_image);
2045 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
2047 integral_image=DestroyImage(integral_image);
2048 return(integral_image);
2050 if (integral_image->alpha_trait != BlendPixelTrait)
2051 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
2055 width=image->columns;
2057 if ((rotations == 1) || (rotations == 3))
2060 height=image->columns;
2062 y_width=width+(ssize_t) floor(fabs(shear.x)*height+0.5);
2063 x_offset=(ssize_t) ceil((double) width+((fabs(shear.y)*height)-width)/2.0-
2065 y_offset=(ssize_t) ceil((double) height+((fabs(shear.y)*y_width)-height)/2.0-
2068 Surround image with a border.
2070 integral_image->border_color=integral_image->background_color;
2071 integral_image->compose=CopyCompositeOp;
2072 border_info.width=(size_t) x_offset;
2073 border_info.height=(size_t) y_offset;
2074 rotate_image=BorderImage(integral_image,&border_info,image->compose,
2076 integral_image=DestroyImage(integral_image);
2077 if (rotate_image == (Image *) NULL)
2078 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2082 status=XShearImage(rotate_image,shear.x,width,height,x_offset,(ssize_t)
2083 (rotate_image->rows-height)/2,exception);
2084 if (status == MagickFalse)
2086 rotate_image=DestroyImage(rotate_image);
2087 return((Image *) NULL);
2089 status=YShearImage(rotate_image,shear.y,y_width,height,(ssize_t)
2090 (rotate_image->columns-y_width)/2,y_offset,exception);
2091 if (status == MagickFalse)
2093 rotate_image=DestroyImage(rotate_image);
2094 return((Image *) NULL);
2096 status=XShearImage(rotate_image,shear.x,y_width,rotate_image->rows,(ssize_t)
2097 (rotate_image->columns-y_width)/2,0,exception);
2098 if (status == MagickFalse)
2100 rotate_image=DestroyImage(rotate_image);
2101 return((Image *) NULL);
2103 status=CropToFitImage(&rotate_image,shear.x,shear.y,(double) width,
2104 (double) height,MagickTrue,exception);
2105 rotate_image->compose=image->compose;
2106 rotate_image->page.width=0;
2107 rotate_image->page.height=0;
2108 if (status == MagickFalse)
2109 rotate_image=DestroyImage(rotate_image);
2110 return(rotate_image);