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 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 magick_threads(image,image,1,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 magick_threads(image,image,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 magick_threads(image,image,1,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=image->columns;
1029 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1030 #pragma omp parallel for schedule(static,4) shared(status) \
1031 magick_threads(image,image,1,1)
1033 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
1038 if (status == MagickFalse)
1041 for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
1046 register const Quantum
1060 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
1061 width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
1063 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
1064 height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
1065 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1067 if (p == (const Quantum *) NULL)
1072 for (y=0; y < (ssize_t) width; y++)
1074 register const Quantum
1075 *restrict tile_pixels;
1080 if (status == MagickFalse)
1082 q=QueueCacheViewAuthenticPixels(rotate_view,(ssize_t)
1083 (rotate_image->columns-(tile_y+height)),y+tile_x,height,1,
1085 if (q == (Quantum *) NULL)
1090 tile_pixels=p+((height-1)*width+y)*GetPixelChannels(image);
1091 for (x=0; x < (ssize_t) height; x++)
1096 if (GetPixelMask(image,tile_pixels) != 0)
1098 tile_pixels-=width*GetPixelChannels(image);
1099 q+=GetPixelChannels(rotate_image);
1102 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1104 PixelChannel channel=GetPixelChannelChannel(image,i);
1105 PixelTrait traits=GetPixelChannelTraits(image,channel);
1106 PixelTrait rotate_traits=GetPixelChannelTraits(rotate_image,
1108 if ((traits == UndefinedPixelTrait) ||
1109 (rotate_traits == UndefinedPixelTrait))
1111 SetPixelChannel(rotate_image,channel,tile_pixels[i],q);
1113 tile_pixels-=width*GetPixelChannels(image);
1114 q+=GetPixelChannels(rotate_image);
1116 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1117 if (sync == MagickFalse)
1121 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1126 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1127 #pragma omp critical (MagickCore_IntegralRotateImage)
1129 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
1131 if (proceed == MagickFalse)
1135 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1136 image->rows-1,image->rows);
1137 Swap(page.width,page.height);
1138 Swap(page.x,page.y);
1139 if (page.width != 0)
1140 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1148 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1149 #pragma omp parallel for schedule(static,4) shared(status) \
1150 magick_threads(image,image,1,1)
1152 for (y=0; y < (ssize_t) image->rows; y++)
1157 register const Quantum
1166 if (status == MagickFalse)
1168 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1169 q=QueueCacheViewAuthenticPixels(rotate_view,0,(ssize_t) (image->rows-y-
1170 1),image->columns,1,exception);
1171 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1176 q+=GetPixelChannels(rotate_image)*image->columns;
1177 for (x=0; x < (ssize_t) image->columns; x++)
1182 q-=GetPixelChannels(rotate_image);
1183 if (GetPixelMask(image,p) != 0)
1185 p+=GetPixelChannels(image);
1188 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1190 PixelChannel channel=GetPixelChannelChannel(image,i);
1191 PixelTrait traits=GetPixelChannelTraits(image,channel);
1192 PixelTrait rotate_traits=GetPixelChannelTraits(rotate_image,
1194 if ((traits == UndefinedPixelTrait) ||
1195 (rotate_traits == UndefinedPixelTrait))
1197 SetPixelChannel(rotate_image,channel,p[i],q);
1199 p+=GetPixelChannels(image);
1201 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1202 if (sync == MagickFalse)
1204 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1209 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1210 #pragma omp critical (MagickCore_IntegralRotateImage)
1212 proceed=SetImageProgress(image,RotateImageTag,progress++,
1214 if (proceed == MagickFalse)
1218 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1219 image->rows-1,image->rows);
1220 Swap(page.width,page.height);
1221 Swap(page.x,page.y);
1222 if (page.width != 0)
1223 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1238 GetPixelCacheTileSize(image,&tile_width,&tile_height);
1239 tile_width=image->columns;
1240 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1241 #pragma omp parallel for schedule(static,4) shared(status) \
1242 magick_threads(image,image,1,1)
1244 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
1249 if (status == MagickFalse)
1252 for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
1257 register const Quantum
1271 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
1272 width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
1274 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
1275 height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
1276 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1278 if (p == (const Quantum *) NULL)
1283 for (y=0; y < (ssize_t) width; y++)
1285 register const Quantum
1286 *restrict tile_pixels;
1291 if (status == MagickFalse)
1293 q=QueueCacheViewAuthenticPixels(rotate_view,tile_y,(ssize_t) (y+
1294 rotate_image->rows-(tile_x+width)),height,1,exception);
1295 if (q == (Quantum *) NULL)
1300 tile_pixels=p+((width-1)-y)*GetPixelChannels(image);
1301 for (x=0; x < (ssize_t) height; x++)
1306 if (GetPixelMask(image,tile_pixels) != 0)
1308 tile_pixels+=width*GetPixelChannels(image);
1309 q+=GetPixelChannels(rotate_image);
1312 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1314 PixelChannel channel=GetPixelChannelChannel(image,i);
1315 PixelTrait traits=GetPixelChannelTraits(image,channel);
1316 PixelTrait rotate_traits=GetPixelChannelTraits(rotate_image,
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 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1327 #pragma omp critical (MagickCore_IntegralRotateImage)
1329 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1330 if (sync == MagickFalse)
1334 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1339 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
1341 if (proceed == MagickFalse)
1345 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1346 image->rows-1,image->rows);
1347 Swap(page.width,page.height);
1348 Swap(page.x,page.y);
1349 if (page.width != 0)
1350 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1354 rotate_view=DestroyCacheView(rotate_view);
1355 image_view=DestroyCacheView(image_view);
1356 rotate_image->type=image->type;
1357 rotate_image->page=page;
1358 if (status == MagickFalse)
1359 rotate_image=DestroyImage(rotate_image);
1360 return(rotate_image);
1364 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1368 + X S h e a r I m a g e %
1372 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1374 % XShearImage() shears the image in the X direction with a shear angle of
1375 % 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1376 % negative angles shear clockwise. Angles are measured relative to a vertical
1377 % Y-axis. X shears will widen an image creating 'empty' triangles on the left
1378 % and right sides of the source image.
1380 % The format of the XShearImage method is:
1382 % MagickBooleanType XShearImage(Image *image,const double degrees,
1383 % const size_t width,const size_t height,
1384 % const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
1386 % A description of each parameter follows.
1388 % o image: the image.
1390 % o degrees: A double representing the shearing angle along the X
1393 % o width, height, x_offset, y_offset: Defines a region of the image
1396 % o exception: return any errors or warnings in this structure.
1399 static MagickBooleanType XShearImage(Image *image,const double degrees,
1400 const size_t width,const size_t height,const ssize_t x_offset,
1401 const ssize_t y_offset,ExceptionInfo *exception)
1403 #define XShearImageTag "XShear/Image"
1429 assert(image != (Image *) NULL);
1430 assert(image->signature == MagickSignature);
1431 if (image->debug != MagickFalse)
1432 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1434 background=image->background_color;
1436 image_view=AcquireAuthenticCacheView(image,exception);
1437 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1438 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1439 magick_threads(image,image,height,1)
1441 for (y=0; y < (ssize_t) height; y++)
1465 if (status == MagickFalse)
1467 p=GetCacheViewAuthenticPixels(image_view,0,y_offset+y,image->columns,1,
1469 if (p == (Quantum *) NULL)
1474 p+=x_offset*GetPixelChannels(image);
1475 displacement=degrees*(double) (y-height/2.0);
1476 if (displacement == 0.0)
1478 if (displacement > 0.0)
1482 displacement*=(-1.0);
1485 step=(ssize_t) floor((double) displacement);
1486 area=(double) (displacement-step);
1489 GetPixelInfo(image,&source);
1490 GetPixelInfo(image,&destination);
1496 Transfer pixels left-to-right.
1498 if (step > x_offset)
1500 q=p-step*GetPixelChannels(image);
1501 for (i=0; i < (ssize_t) width; i++)
1503 if ((x_offset+i) < step)
1505 p+=GetPixelChannels(image);
1506 GetPixelInfoPixel(image,p,&pixel);
1507 q+=GetPixelChannels(image);
1510 GetPixelInfoPixel(image,p,&source);
1511 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1512 &source,(double) GetPixelAlpha(image,p),area,&destination);
1513 SetPixelInfoPixel(image,&destination,q);
1514 GetPixelInfoPixel(image,p,&pixel);
1515 p+=GetPixelChannels(image);
1516 q+=GetPixelChannels(image);
1518 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1519 &background,(double) background.alpha,area,&destination);
1520 SetPixelInfoPixel(image,&destination,q);
1521 q+=GetPixelChannels(image);
1522 for (i=0; i < (step-1); i++)
1524 SetPixelInfoPixel(image,&background,q);
1525 q+=GetPixelChannels(image);
1532 Transfer pixels right-to-left.
1534 p+=width*GetPixelChannels(image);
1535 q=p+step*GetPixelChannels(image);
1536 for (i=0; i < (ssize_t) width; i++)
1538 p-=GetPixelChannels(image);
1539 q-=GetPixelChannels(image);
1540 if ((size_t) (x_offset+width+step-i) >= image->columns)
1542 GetPixelInfoPixel(image,p,&source);
1543 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1544 &source,(double) GetPixelAlpha(image,p),area,&destination);
1545 SetPixelInfoPixel(image,&destination,q);
1546 GetPixelInfoPixel(image,p,&pixel);
1548 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1549 &background,(double) background.alpha,area,&destination);
1550 q-=GetPixelChannels(image);
1551 SetPixelInfoPixel(image,&destination,q);
1552 for (i=0; i < (step-1); i++)
1554 q-=GetPixelChannels(image);
1555 SetPixelInfoPixel(image,&background,q);
1560 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1562 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1567 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1568 #pragma omp critical (MagickCore_XShearImage)
1570 proceed=SetImageProgress(image,XShearImageTag,progress++,height);
1571 if (proceed == MagickFalse)
1575 image_view=DestroyCacheView(image_view);
1580 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1584 + Y S h e a r I m a g e %
1588 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1590 % YShearImage shears the image in the Y direction with a shear angle of
1591 % 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1592 % negative angles shear clockwise. Angles are measured relative to a
1593 % horizontal X-axis. Y shears will increase the height of an image creating
1594 % 'empty' triangles on the top and bottom of the source image.
1596 % The format of the YShearImage method is:
1598 % MagickBooleanType YShearImage(Image *image,const double degrees,
1599 % const size_t width,const size_t height,
1600 % const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
1602 % A description of each parameter follows.
1604 % o image: the image.
1606 % o degrees: A double representing the shearing angle along the Y
1609 % o width, height, x_offset, y_offset: Defines a region of the image
1612 % o exception: return any errors or warnings in this structure.
1615 static MagickBooleanType YShearImage(Image *image,const double degrees,
1616 const size_t width,const size_t height,const ssize_t x_offset,
1617 const ssize_t y_offset,ExceptionInfo *exception)
1619 #define YShearImageTag "YShear/Image"
1645 assert(image != (Image *) NULL);
1646 assert(image->signature == MagickSignature);
1647 if (image->debug != MagickFalse)
1648 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1651 background=image->background_color;
1652 image_view=AcquireAuthenticCacheView(image,exception);
1653 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1654 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1655 magick_threads(image,image,width,1)
1657 for (x=0; x < (ssize_t) width; x++)
1681 if (status == MagickFalse)
1683 p=GetCacheViewAuthenticPixels(image_view,x_offset+x,0,1,image->rows,
1685 if (p == (Quantum *) NULL)
1690 p+=y_offset*GetPixelChannels(image);
1691 displacement=degrees*(double) (x-width/2.0);
1692 if (displacement == 0.0)
1694 if (displacement > 0.0)
1698 displacement*=(-1.0);
1701 step=(ssize_t) floor((double) displacement);
1702 area=(double) (displacement-step);
1705 GetPixelInfo(image,&source);
1706 GetPixelInfo(image,&destination);
1712 Transfer pixels top-to-bottom.
1714 if (step > y_offset)
1716 q=p-step*GetPixelChannels(image);
1717 for (i=0; i < (ssize_t) height; i++)
1719 if ((y_offset+i) < step)
1721 p+=GetPixelChannels(image);
1722 GetPixelInfoPixel(image,p,&pixel);
1723 q+=GetPixelChannels(image);
1726 GetPixelInfoPixel(image,p,&source);
1727 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1728 &source,(double) GetPixelAlpha(image,p),area,
1730 SetPixelInfoPixel(image,&destination,q);
1731 GetPixelInfoPixel(image,p,&pixel);
1732 p+=GetPixelChannels(image);
1733 q+=GetPixelChannels(image);
1735 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1736 &background,(double) background.alpha,area,&destination);
1737 SetPixelInfoPixel(image,&destination,q);
1738 q+=GetPixelChannels(image);
1739 for (i=0; i < (step-1); i++)
1741 SetPixelInfoPixel(image,&background,q);
1742 q+=GetPixelChannels(image);
1749 Transfer pixels bottom-to-top.
1751 p+=height*GetPixelChannels(image);
1752 q=p+step*GetPixelChannels(image);
1753 for (i=0; i < (ssize_t) height; i++)
1755 p-=GetPixelChannels(image);
1756 q-=GetPixelChannels(image);
1757 if ((size_t) (y_offset+height+step-i) >= image->rows)
1759 GetPixelInfoPixel(image,p,&source);
1760 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1761 &source,(double) GetPixelAlpha(image,p),area,
1763 SetPixelInfoPixel(image,&destination,q);
1764 GetPixelInfoPixel(image,p,&pixel);
1766 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1767 &background,(double) background.alpha,area,&destination);
1768 q-=GetPixelChannels(image);
1769 SetPixelInfoPixel(image,&destination,q);
1770 for (i=0; i < (step-1); i++)
1772 q-=GetPixelChannels(image);
1773 SetPixelInfoPixel(image,&background,q);
1778 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1780 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1785 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1786 #pragma omp critical (MagickCore_YShearImage)
1788 proceed=SetImageProgress(image,YShearImageTag,progress++,image->rows);
1789 if (proceed == MagickFalse)
1793 image_view=DestroyCacheView(image_view);
1798 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1802 % S h e a r I m a g e %
1806 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1808 % ShearImage() creates a new image that is a shear_image copy of an existing
1809 % one. Shearing slides one edge of an image along the X or Y axis, creating
1810 % a parallelogram. An X direction shear slides an edge along the X axis,
1811 % while a Y direction shear slides an edge along the Y axis. The amount of
1812 % the shear is controlled by a shear angle. For X direction shears, x_shear
1813 % is measured relative to the Y axis, and similarly, for Y direction shears
1814 % y_shear is measured relative to the X axis. Empty triangles left over from
1815 % shearing the image are filled with the background color defined by member
1816 % 'background_color' of the image.. ShearImage() allocates the memory
1817 % necessary for the new Image structure and returns a pointer to the new image.
1819 % ShearImage() is based on the paper "A Fast Algorithm for General Raster
1820 % Rotatation" by Alan W. Paeth.
1822 % The format of the ShearImage method is:
1824 % Image *ShearImage(const Image *image,const double x_shear,
1825 % const double y_shear,ExceptionInfo *exception)
1827 % A description of each parameter follows.
1829 % o image: the image.
1831 % o x_shear, y_shear: Specifies the number of degrees to shear the image.
1833 % o exception: return any errors or warnings in this structure.
1836 MagickExport Image *ShearImage(const Image *image,const double x_shear,
1837 const double y_shear,ExceptionInfo *exception)
1859 assert(image != (Image *) NULL);
1860 assert(image->signature == MagickSignature);
1861 if (image->debug != MagickFalse)
1862 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1863 assert(exception != (ExceptionInfo *) NULL);
1864 assert(exception->signature == MagickSignature);
1865 if ((x_shear != 0.0) && (fmod(x_shear,90.0) == 0.0))
1866 ThrowImageException(ImageError,"AngleIsDiscontinuous");
1867 if ((y_shear != 0.0) && (fmod(y_shear,90.0) == 0.0))
1868 ThrowImageException(ImageError,"AngleIsDiscontinuous");
1870 Initialize shear angle.
1872 integral_image=CloneImage(image,0,0,MagickTrue,exception);
1873 if (integral_image == (Image *) NULL)
1874 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1875 shear.x=(-tan(DegreesToRadians(fmod(x_shear,360.0))));
1876 shear.y=tan(DegreesToRadians(fmod(y_shear,360.0)));
1877 if ((shear.x == 0.0) && (shear.y == 0.0))
1878 return(integral_image);
1879 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
1881 integral_image=DestroyImage(integral_image);
1882 return(integral_image);
1884 if (integral_image->alpha_trait != BlendPixelTrait)
1885 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
1889 y_width=image->columns+(ssize_t) floor(fabs(shear.x)*image->rows+0.5);
1890 x_offset=(ssize_t) ceil((double) image->columns+((fabs(shear.x)*image->rows)-
1891 image->columns)/2.0-0.5);
1892 y_offset=(ssize_t) ceil((double) image->rows+((fabs(shear.y)*y_width)-
1893 image->rows)/2.0-0.5);
1895 Surround image with border.
1897 integral_image->border_color=integral_image->background_color;
1898 integral_image->compose=CopyCompositeOp;
1899 border_info.width=(size_t) x_offset;
1900 border_info.height=(size_t) y_offset;
1901 shear_image=BorderImage(integral_image,&border_info,image->compose,exception);
1902 integral_image=DestroyImage(integral_image);
1903 if (shear_image == (Image *) NULL)
1904 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1908 if (shear_image->alpha_trait != BlendPixelTrait)
1909 (void) SetImageAlphaChannel(shear_image,OpaqueAlphaChannel,exception);
1910 status=XShearImage(shear_image,shear.x,image->columns,image->rows,x_offset,
1911 (ssize_t) (shear_image->rows-image->rows)/2,exception);
1912 if (status == MagickFalse)
1914 shear_image=DestroyImage(shear_image);
1915 return((Image *) NULL);
1917 status=YShearImage(shear_image,shear.y,y_width,image->rows,(ssize_t)
1918 (shear_image->columns-y_width)/2,y_offset,exception);
1919 if (status == MagickFalse)
1921 shear_image=DestroyImage(shear_image);
1922 return((Image *) NULL);
1924 status=CropToFitImage(&shear_image,shear.x,shear.y,(double)
1925 image->columns,(double) image->rows,MagickFalse,exception);
1926 shear_image->compose=image->compose;
1927 shear_image->page.width=0;
1928 shear_image->page.height=0;
1929 if (status == MagickFalse)
1930 shear_image=DestroyImage(shear_image);
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 rotate_image->compose=image->compose;
2088 rotate_image->page.width=0;
2089 rotate_image->page.height=0;
2090 if (status == MagickFalse)
2091 rotate_image=DestroyImage(rotate_image);
2092 return(rotate_image);