2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % SSSSS H H EEEEE AAA RRRR %
8 % SSS HHHHH EEE AAAAA RRRR %
10 % SSSSS H H EEEEE A A R R %
13 % MagickCore Methods to Shear or Rotate an Image by an Arbitrary Angle %
20 % Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 % The XShearImage() and YShearImage() methods are based on the paper "A Fast
37 % Algorithm for General Raster Rotatation" by Alan W. Paeth, Graphics
38 % Interface '86 (Vancouver). ShearRotateImage() is adapted from a similar
39 % method based on the Paeth paper written by Michael Halle of the Spatial
40 % Imaging Group, MIT Media Lab.
47 #include "MagickCore/studio.h"
48 #include "MagickCore/artifact.h"
49 #include "MagickCore/attribute.h"
50 #include "MagickCore/blob-private.h"
51 #include "MagickCore/cache-private.h"
52 #include "MagickCore/color-private.h"
53 #include "MagickCore/colorspace-private.h"
54 #include "MagickCore/composite.h"
55 #include "MagickCore/composite-private.h"
56 #include "MagickCore/decorate.h"
57 #include "MagickCore/distort.h"
58 #include "MagickCore/draw.h"
59 #include "MagickCore/exception.h"
60 #include "MagickCore/exception-private.h"
61 #include "MagickCore/gem.h"
62 #include "MagickCore/geometry.h"
63 #include "MagickCore/image.h"
64 #include "MagickCore/image-private.h"
65 #include "MagickCore/memory_.h"
66 #include "MagickCore/list.h"
67 #include "MagickCore/monitor.h"
68 #include "MagickCore/monitor-private.h"
69 #include "MagickCore/nt-base-private.h"
70 #include "MagickCore/pixel-accessor.h"
71 #include "MagickCore/quantum.h"
72 #include "MagickCore/resource_.h"
73 #include "MagickCore/shear.h"
74 #include "MagickCore/statistic.h"
75 #include "MagickCore/string_.h"
76 #include "MagickCore/string-private.h"
77 #include "MagickCore/thread-private.h"
78 #include "MagickCore/threshold.h"
79 #include "MagickCore/transform.h"
82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
86 + C r o p T o F i t I m a g e %
90 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
92 % CropToFitImage() crops the sheared image as determined by the bounding box
93 % as defined by width and height and shearing angles.
95 % The format of the CropToFitImage method is:
97 % MagickBooleanType CropToFitImage(Image **image,
98 % const MagickRealType x_shear,const MagickRealType x_shear,
99 % const MagickRealType width,const MagickRealType 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 MagickRealType x_shear,const MagickRealType y_shear,
113 const MagickRealType width,const MagickRealType 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(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 if ((p->width*p->height) > 8192) \
577 num_threads(GetMagickResourceLimit(ThreadResource))
579 for (x=0; x < (ssize_t) p->width; x++)
588 for (y=0; y < (ssize_t) (p->height-1); y++)
593 delta=GetRadonCell(p,x,y)-(ssize_t) GetRadonCell(p,x,y+1);
596 projection[p->width+sign*x-1]=sum;
600 static MagickBooleanType RadonTransform(const Image *image,
601 const double threshold,size_t *projection,ExceptionInfo *exception)
629 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
630 source_cells=AcquireRadonInfo(image,width,image->rows,exception);
631 destination_cells=AcquireRadonInfo(image,width,image->rows,exception);
632 if ((source_cells == (RadonInfo *) NULL) ||
633 (destination_cells == (RadonInfo *) NULL))
635 if (destination_cells != (RadonInfo *) NULL)
636 destination_cells=DestroyRadonInfo(destination_cells);
637 if (source_cells != (RadonInfo *) NULL)
638 source_cells=DestroyRadonInfo(source_cells);
641 if (ResetRadonCells(source_cells) == MagickFalse)
643 destination_cells=DestroyRadonInfo(destination_cells);
644 source_cells=DestroyRadonInfo(source_cells);
647 for (i=0; i < 256; i++)
649 byte=(unsigned char) i;
650 for (count=0; byte != 0; byte>>=1)
652 bits[i]=(unsigned short) count;
655 image_view=AcquireVirtualCacheView(image,exception);
656 #if defined(MAGICKCORE_OPENMP_SUPPORT)
657 #pragma omp parallel for schedule(static,4) shared(status) \
658 if ((image->rows*image->columns) > 8192) \
659 num_threads(GetMagickResourceLimit(ThreadResource))
661 for (y=0; y < (ssize_t) image->rows; y++)
663 register const Quantum
674 if (status == MagickFalse)
676 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
677 if (p == (const Quantum *) NULL)
684 i=(ssize_t) (image->columns+7)/8;
685 for (x=0; x < (ssize_t) image->columns; x++)
688 if ((double) GetPixelIntensity(image,p) < threshold)
693 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
697 p+=GetPixelChannels(image);
702 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
705 RadonProjection(source_cells,destination_cells,-1,projection);
706 (void) ResetRadonCells(source_cells);
707 #if defined(MAGICKCORE_OPENMP_SUPPORT)
708 #pragma omp parallel for schedule(static,4) shared(status) \
709 if ((image->rows*image->columns) > 8192) \
710 num_threads(GetMagickResourceLimit(ThreadResource))
712 for (y=0; y < (ssize_t) image->rows; y++)
714 register const Quantum
725 if (status == MagickFalse)
727 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
728 if (p == (const Quantum *) NULL)
736 for (x=0; x < (ssize_t) image->columns; x++)
739 if ((double) GetPixelIntensity(image,p) < threshold)
744 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
748 p+=GetPixelChannels(image);
753 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
756 RadonProjection(source_cells,destination_cells,1,projection);
757 image_view=DestroyCacheView(image_view);
758 destination_cells=DestroyRadonInfo(destination_cells);
759 source_cells=DestroyRadonInfo(source_cells);
763 static void GetImageBackgroundColor(Image *image,const ssize_t offset,
764 ExceptionInfo *exception)
779 Compute average background color.
783 GetPixelInfo(image,&background);
785 image_view=AcquireVirtualCacheView(image,exception);
786 for (y=0; y < (ssize_t) image->rows; y++)
788 register const Quantum
794 if ((y >= offset) && (y < ((ssize_t) image->rows-offset)))
796 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
797 if (p == (const Quantum *) NULL)
799 for (x=0; x < (ssize_t) image->columns; x++)
801 if ((x >= offset) && (x < ((ssize_t) image->columns-offset)))
803 background.red+=QuantumScale*GetPixelRed(image,p);
804 background.green+=QuantumScale*GetPixelGreen(image,p);
805 background.blue+=QuantumScale*GetPixelBlue(image,p);
806 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
807 background.alpha+=QuantumScale*GetPixelAlpha(image,p);
809 p+=GetPixelChannels(image);
812 image_view=DestroyCacheView(image_view);
813 image->background_color.red=(double) ClampToQuantum((MagickRealType)
814 QuantumRange*background.red/count);
815 image->background_color.green=(double) ClampToQuantum((MagickRealType)
816 QuantumRange*background.green/count);
817 image->background_color.blue=(double) ClampToQuantum((MagickRealType)
818 QuantumRange*background.blue/count);
819 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
820 image->background_color.alpha=(double) ClampToQuantum((MagickRealType)
821 QuantumRange*background.alpha/count);
824 MagickExport Image *DeskewImage(const Image *image,const double threshold,
825 ExceptionInfo *exception)
860 Compute deskew angle.
862 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
863 projection=(size_t *) AcquireQuantumMemory((size_t) (2*width-1),
864 sizeof(*projection));
865 if (projection == (size_t *) NULL)
866 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
867 status=RadonTransform(image,threshold,projection,exception);
868 if (status == MagickFalse)
870 projection=(size_t *) RelinquishMagickMemory(projection);
871 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
875 for (i=0; i < (ssize_t) (2*width-1); i++)
877 if (projection[i] > max_projection)
879 skew=i-(ssize_t) width+1;
880 max_projection=projection[i];
883 projection=(size_t *) RelinquishMagickMemory(projection);
887 clone_image=CloneImage(image,0,0,MagickTrue,exception);
888 if (clone_image == (Image *) NULL)
889 return((Image *) NULL);
890 (void) SetImageVirtualPixelMethod(clone_image,BackgroundVirtualPixelMethod,
892 degrees=RadiansToDegrees(-atan((double) skew/width/8));
893 if (image->debug != MagickFalse)
894 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
895 " Deskew angle: %g",degrees);
896 affine_matrix.sx=cos(DegreesToRadians(fmod((double) degrees,360.0)));
897 affine_matrix.rx=sin(DegreesToRadians(fmod((double) degrees,360.0)));
898 affine_matrix.ry=(-sin(DegreesToRadians(fmod((double) degrees,360.0))));
899 affine_matrix.sy=cos(DegreesToRadians(fmod((double) degrees,360.0)));
900 affine_matrix.tx=0.0;
901 affine_matrix.ty=0.0;
902 artifact=GetImageArtifact(image,"deskew:auto-crop");
903 if (artifact == (const char *) NULL)
905 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
906 clone_image=DestroyImage(clone_image);
907 return(deskew_image);
912 GetImageBackgroundColor(clone_image,(ssize_t) StringToLong(artifact),
914 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
915 clone_image=DestroyImage(clone_image);
916 if (deskew_image == (Image *) NULL)
917 return((Image *) NULL);
918 median_image=StatisticImage(deskew_image,MedianStatistic,3,3,exception);
919 if (median_image == (Image *) NULL)
921 deskew_image=DestroyImage(deskew_image);
922 return((Image *) NULL);
924 geometry=GetImageBoundingBox(median_image,exception);
925 median_image=DestroyImage(median_image);
926 if (image->debug != MagickFalse)
927 (void) LogMagickEvent(TransformEvent,GetMagickModule()," Deskew geometry: "
928 "%.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
929 geometry.height,(double) geometry.x,(double) geometry.y);
930 crop_image=CropImage(deskew_image,&geometry,exception);
931 deskew_image=DestroyImage(deskew_image);
936 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
940 % I n t e g r a l R o t a t e I m a g e %
944 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
946 % IntegralRotateImage() rotates the image an integral of 90 degrees. It
947 % allocates the memory necessary for the new Image structure and returns a
948 % pointer to the rotated image.
950 % The format of the IntegralRotateImage method is:
952 % Image *IntegralRotateImage(const Image *image,size_t rotations,
953 % ExceptionInfo *exception)
955 % A description of each parameter follows.
957 % o image: the image.
959 % o rotations: Specifies the number of 90 degree rotations.
962 MagickExport Image *IntegralRotateImage(const Image *image,size_t rotations,
963 ExceptionInfo *exception)
965 #define RotateImageTag "Rotate/Image"
987 Initialize rotated image attributes.
989 assert(image != (Image *) NULL);
993 return(CloneImage(image,0,0,MagickTrue,exception));
994 if ((rotations == 1) || (rotations == 3))
995 rotate_image=CloneImage(image,image->rows,image->columns,MagickTrue,
998 rotate_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1000 if (rotate_image == (Image *) NULL)
1001 return((Image *) NULL);
1003 Integral rotate the image.
1007 image_view=AcquireVirtualCacheView(image,exception);
1008 rotate_view=AcquireAuthenticCacheView(rotate_image,exception);
1030 GetPixelCacheTileSize(image,&tile_width,&tile_height);
1031 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1032 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1033 if ((image->rows*image->columns) > 8192) \
1034 num_threads(GetMagickResourceLimit(ThreadResource))
1036 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
1041 if (status == MagickFalse)
1044 for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
1049 register const Quantum
1063 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
1064 width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
1066 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
1067 height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
1068 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1070 if (p == (const Quantum *) NULL)
1075 for (y=0; y < (ssize_t) width; y++)
1077 register const Quantum
1078 *restrict tile_pixels;
1083 if (status == MagickFalse)
1085 q=QueueCacheViewAuthenticPixels(rotate_view,(ssize_t)
1086 (rotate_image->columns-(tile_y+height)),y+tile_x,height,1,
1088 if (q == (Quantum *) NULL)
1093 tile_pixels=p+((height-1)*width+y)*GetPixelChannels(image);
1094 for (x=0; x < (ssize_t) height; x++)
1099 if (GetPixelMask(image,tile_pixels) != 0)
1101 tile_pixels-=width*GetPixelChannels(image);
1102 q+=GetPixelChannels(rotate_image);
1105 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1114 channel=GetPixelChannelMapChannel(image,i);
1115 traits=GetPixelChannelMapTraits(image,channel);
1116 rotate_traits=GetPixelChannelMapTraits(rotate_image,channel);
1117 if ((traits == UndefinedPixelTrait) ||
1118 (rotate_traits == UndefinedPixelTrait))
1120 SetPixelChannel(rotate_image,channel,tile_pixels[i],q);
1122 tile_pixels-=width*GetPixelChannels(image);
1123 q+=GetPixelChannels(rotate_image);
1125 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1126 if (sync == MagickFalse)
1130 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1135 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1136 #pragma omp critical (MagickCore_IntegralRotateImage)
1138 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
1140 if (proceed == MagickFalse)
1144 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1145 image->rows-1,image->rows);
1146 Swap(page.width,page.height);
1147 Swap(page.x,page.y);
1148 if (page.width != 0)
1149 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1157 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1158 #pragma omp parallel for schedule(static) shared(progress,status) \
1159 if ((image->rows*image->columns) > 8192) \
1160 num_threads(GetMagickResourceLimit(ThreadResource))
1162 for (y=0; y < (ssize_t) image->rows; y++)
1167 register const Quantum
1176 if (status == MagickFalse)
1178 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1179 q=QueueCacheViewAuthenticPixels(rotate_view,0,(ssize_t) (image->rows-y-
1180 1),image->columns,1,exception);
1181 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1186 q+=GetPixelChannels(rotate_image)*image->columns;
1187 for (x=0; x < (ssize_t) image->columns; x++)
1192 q-=GetPixelChannels(rotate_image);
1193 if (GetPixelMask(image,p) != 0)
1195 p+=GetPixelChannels(image);
1198 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1207 channel=GetPixelChannelMapChannel(image,i);
1208 traits=GetPixelChannelMapTraits(image,channel);
1209 rotate_traits=GetPixelChannelMapTraits(rotate_image,channel);
1210 if ((traits == UndefinedPixelTrait) ||
1211 (rotate_traits == UndefinedPixelTrait))
1213 SetPixelChannel(rotate_image,channel,p[i],q);
1215 p+=GetPixelChannels(image);
1217 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1218 if (sync == MagickFalse)
1220 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1225 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1226 #pragma omp critical (MagickCore_IntegralRotateImage)
1228 proceed=SetImageProgress(image,RotateImageTag,progress++,
1230 if (proceed == MagickFalse)
1234 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1235 image->rows-1,image->rows);
1236 Swap(page.width,page.height);
1237 Swap(page.x,page.y);
1238 if (page.width != 0)
1239 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1254 GetPixelCacheTileSize(image,&tile_width,&tile_height);
1255 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1256 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1257 if ((image->rows*image->columns) > 8192) \
1258 num_threads(GetMagickResourceLimit(ThreadResource))
1260 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
1265 if (status == MagickFalse)
1268 for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
1273 register const Quantum
1287 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
1288 width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
1290 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
1291 height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
1292 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1294 if (p == (const Quantum *) NULL)
1299 for (y=0; y < (ssize_t) width; y++)
1301 register const Quantum
1302 *restrict tile_pixels;
1307 if (status == MagickFalse)
1309 q=QueueCacheViewAuthenticPixels(rotate_view,tile_y,(ssize_t) (y+
1310 rotate_image->rows-(tile_x+width)),height,1,exception);
1311 if (q == (Quantum *) NULL)
1316 tile_pixels=p+((width-1)-y)*GetPixelChannels(image);
1317 for (x=0; x < (ssize_t) height; x++)
1322 if (GetPixelMask(image,tile_pixels) != 0)
1324 tile_pixels+=width*GetPixelChannels(image);
1325 q+=GetPixelChannels(rotate_image);
1328 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1337 channel=GetPixelChannelMapChannel(image,i);
1338 traits=GetPixelChannelMapTraits(image,channel);
1339 rotate_traits=GetPixelChannelMapTraits(rotate_image,channel);
1340 if ((traits == UndefinedPixelTrait) ||
1341 (rotate_traits == UndefinedPixelTrait))
1343 SetPixelChannel(rotate_image,channel,tile_pixels[i],q);
1345 tile_pixels+=width*GetPixelChannels(image);
1346 q+=GetPixelChannels(rotate_image);
1348 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1349 if (sync == MagickFalse)
1353 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1358 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1359 #pragma omp critical (MagickCore_IntegralRotateImage)
1361 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
1363 if (proceed == MagickFalse)
1367 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1368 image->rows-1,image->rows);
1369 Swap(page.width,page.height);
1370 Swap(page.x,page.y);
1371 if (page.width != 0)
1372 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1376 rotate_view=DestroyCacheView(rotate_view);
1377 image_view=DestroyCacheView(image_view);
1378 rotate_image->type=image->type;
1379 rotate_image->page=page;
1380 if (status == MagickFalse)
1381 rotate_image=DestroyImage(rotate_image);
1382 return(rotate_image);
1386 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1390 + X S h e a r I m a g e %
1394 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1396 % XShearImage() shears the image in the X direction with a shear angle of
1397 % 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1398 % negative angles shear clockwise. Angles are measured relative to a vertical
1399 % Y-axis. X shears will widen an image creating 'empty' triangles on the left
1400 % and right sides of the source image.
1402 % The format of the XShearImage method is:
1404 % MagickBooleanType XShearImage(Image *image,const MagickRealType degrees,
1405 % const size_t width,const size_t height,
1406 % const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
1408 % A description of each parameter follows.
1410 % o image: the image.
1412 % o degrees: A MagickRealType representing the shearing angle along the X
1415 % o width, height, x_offset, y_offset: Defines a region of the image
1418 % o exception: return any errors or warnings in this structure.
1421 static MagickBooleanType XShearImage(Image *image,const MagickRealType degrees,
1422 const size_t width,const size_t height,const ssize_t x_offset,
1423 const ssize_t y_offset,ExceptionInfo *exception)
1425 #define XShearImageTag "XShear/Image"
1451 assert(image != (Image *) NULL);
1452 assert(image->signature == MagickSignature);
1453 if (image->debug != MagickFalse)
1454 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1456 background=image->background_color;
1458 image_view=AcquireAuthenticCacheView(image,exception);
1459 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1460 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1461 if ((height*width) > 8192) \
1462 num_threads(GetMagickResourceLimit(ThreadResource))
1464 for (y=0; y < (ssize_t) height; y++)
1488 if (status == MagickFalse)
1490 p=GetCacheViewAuthenticPixels(image_view,0,y_offset+y,image->columns,1,
1492 if (p == (Quantum *) NULL)
1497 p+=x_offset*GetPixelChannels(image);
1498 displacement=degrees*(MagickRealType) (y-height/2.0);
1499 if (displacement == 0.0)
1501 if (displacement > 0.0)
1505 displacement*=(-1.0);
1508 step=(ssize_t) floor((double) displacement);
1509 area=(MagickRealType) (displacement-step);
1512 GetPixelInfo(image,&source);
1513 GetPixelInfo(image,&destination);
1519 Transfer pixels left-to-right.
1521 if (step > x_offset)
1523 q=p-step*GetPixelChannels(image);
1524 for (i=0; i < (ssize_t) width; i++)
1526 if ((x_offset+i) < step)
1528 p+=GetPixelChannels(image);
1529 GetPixelInfoPixel(image,p,&pixel);
1530 q+=GetPixelChannels(image);
1533 GetPixelInfoPixel(image,p,&source);
1534 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1535 &source,(MagickRealType) GetPixelAlpha(image,p),area,&destination);
1536 SetPixelInfoPixel(image,&destination,q);
1537 GetPixelInfoPixel(image,p,&pixel);
1538 p+=GetPixelChannels(image);
1539 q+=GetPixelChannels(image);
1541 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1542 &background,(MagickRealType) background.alpha,area,&destination);
1543 SetPixelInfoPixel(image,&destination,q);
1544 q+=GetPixelChannels(image);
1545 for (i=0; i < (step-1); i++)
1547 SetPixelInfoPixel(image,&background,q);
1548 q+=GetPixelChannels(image);
1555 Transfer pixels right-to-left.
1557 p+=width*GetPixelChannels(image);
1558 q=p+step*GetPixelChannels(image);
1559 for (i=0; i < (ssize_t) width; i++)
1561 p-=GetPixelChannels(image);
1562 q-=GetPixelChannels(image);
1563 if ((size_t) (x_offset+width+step-i) >= image->columns)
1565 GetPixelInfoPixel(image,p,&source);
1566 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1567 &source,(MagickRealType) GetPixelAlpha(image,p),area,&destination);
1568 SetPixelInfoPixel(image,&destination,q);
1569 GetPixelInfoPixel(image,p,&pixel);
1571 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1572 &background,(MagickRealType) background.alpha,area,&destination);
1573 q-=GetPixelChannels(image);
1574 SetPixelInfoPixel(image,&destination,q);
1575 for (i=0; i < (step-1); i++)
1577 q-=GetPixelChannels(image);
1578 SetPixelInfoPixel(image,&background,q);
1583 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1585 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1590 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1591 #pragma omp critical (MagickCore_XShearImage)
1593 proceed=SetImageProgress(image,XShearImageTag,progress++,height);
1594 if (proceed == MagickFalse)
1598 image_view=DestroyCacheView(image_view);
1603 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1607 + Y S h e a r I m a g e %
1611 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1613 % YShearImage shears the image in the Y direction with a shear angle of
1614 % 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1615 % negative angles shear clockwise. Angles are measured relative to a
1616 % horizontal X-axis. Y shears will increase the height of an image creating
1617 % 'empty' triangles on the top and bottom of the source image.
1619 % The format of the YShearImage method is:
1621 % MagickBooleanType YShearImage(Image *image,const MagickRealType degrees,
1622 % const size_t width,const size_t height,
1623 % const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
1625 % A description of each parameter follows.
1627 % o image: the image.
1629 % o degrees: A MagickRealType representing the shearing angle along the Y
1632 % o width, height, x_offset, y_offset: Defines a region of the image
1635 % o exception: return any errors or warnings in this structure.
1638 static MagickBooleanType YShearImage(Image *image,const MagickRealType degrees,
1639 const size_t width,const size_t height,const ssize_t x_offset,
1640 const ssize_t y_offset,ExceptionInfo *exception)
1642 #define YShearImageTag "YShear/Image"
1668 assert(image != (Image *) NULL);
1669 assert(image->signature == MagickSignature);
1670 if (image->debug != MagickFalse)
1671 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1674 background=image->background_color;
1675 image_view=AcquireAuthenticCacheView(image,exception);
1676 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1677 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1678 if ((height*width) > 8192) \
1679 num_threads(GetMagickResourceLimit(ThreadResource))
1681 for (x=0; x < (ssize_t) width; x++)
1705 if (status == MagickFalse)
1707 p=GetCacheViewAuthenticPixels(image_view,x_offset+x,0,1,image->rows,
1709 if (p == (Quantum *) NULL)
1714 p+=y_offset*GetPixelChannels(image);
1715 displacement=degrees*(MagickRealType) (x-width/2.0);
1716 if (displacement == 0.0)
1718 if (displacement > 0.0)
1722 displacement*=(-1.0);
1725 step=(ssize_t) floor((double) displacement);
1726 area=(MagickRealType) (displacement-step);
1729 GetPixelInfo(image,&source);
1730 GetPixelInfo(image,&destination);
1736 Transfer pixels top-to-bottom.
1738 if (step > y_offset)
1740 q=p-step*GetPixelChannels(image);
1741 for (i=0; i < (ssize_t) height; i++)
1743 if ((y_offset+i) < step)
1745 p+=GetPixelChannels(image);
1746 GetPixelInfoPixel(image,p,&pixel);
1747 q+=GetPixelChannels(image);
1750 GetPixelInfoPixel(image,p,&source);
1751 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1752 &source,(MagickRealType) GetPixelAlpha(image,p),area,
1754 SetPixelInfoPixel(image,&destination,q);
1755 GetPixelInfoPixel(image,p,&pixel);
1756 p+=GetPixelChannels(image);
1757 q+=GetPixelChannels(image);
1759 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1760 &background,(MagickRealType) background.alpha,area,&destination);
1761 SetPixelInfoPixel(image,&destination,q);
1762 q+=GetPixelChannels(image);
1763 for (i=0; i < (step-1); i++)
1765 SetPixelInfoPixel(image,&background,q);
1766 q+=GetPixelChannels(image);
1773 Transfer pixels bottom-to-top.
1775 p+=height*GetPixelChannels(image);
1776 q=p+step*GetPixelChannels(image);
1777 for (i=0; i < (ssize_t) height; i++)
1779 p-=GetPixelChannels(image);
1780 q-=GetPixelChannels(image);
1781 if ((size_t) (y_offset+height+step-i) >= image->rows)
1783 GetPixelInfoPixel(image,p,&source);
1784 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1785 &source,(MagickRealType) GetPixelAlpha(image,p),area,
1787 SetPixelInfoPixel(image,&destination,q);
1788 GetPixelInfoPixel(image,p,&pixel);
1790 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1791 &background,(MagickRealType) background.alpha,area,&destination);
1792 q-=GetPixelChannels(image);
1793 SetPixelInfoPixel(image,&destination,q);
1794 for (i=0; i < (step-1); i++)
1796 q-=GetPixelChannels(image);
1797 SetPixelInfoPixel(image,&background,q);
1802 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1804 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1809 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1810 #pragma omp critical (MagickCore_YShearImage)
1812 proceed=SetImageProgress(image,YShearImageTag,progress++,image->rows);
1813 if (proceed == MagickFalse)
1817 image_view=DestroyCacheView(image_view);
1822 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1826 % S h e a r I m a g e %
1830 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1832 % ShearImage() creates a new image that is a shear_image copy of an existing
1833 % one. Shearing slides one edge of an image along the X or Y axis, creating
1834 % a parallelogram. An X direction shear slides an edge along the X axis,
1835 % while a Y direction shear slides an edge along the Y axis. The amount of
1836 % the shear is controlled by a shear angle. For X direction shears, x_shear
1837 % is measured relative to the Y axis, and similarly, for Y direction shears
1838 % y_shear is measured relative to the X axis. Empty triangles left over from
1839 % shearing the image are filled with the background color defined by member
1840 % 'background_color' of the image.. ShearImage() allocates the memory
1841 % necessary for the new Image structure and returns a pointer to the new image.
1843 % ShearImage() is based on the paper "A Fast Algorithm for General Raster
1844 % Rotatation" by Alan W. Paeth.
1846 % The format of the ShearImage method is:
1848 % Image *ShearImage(const Image *image,const double x_shear,
1849 % const double y_shear,ExceptionInfo *exception)
1851 % A description of each parameter follows.
1853 % o image: the image.
1855 % o x_shear, y_shear: Specifies the number of degrees to shear the image.
1857 % o exception: return any errors or warnings in this structure.
1860 MagickExport Image *ShearImage(const Image *image,const double x_shear,
1861 const double y_shear,ExceptionInfo *exception)
1883 assert(image != (Image *) NULL);
1884 assert(image->signature == MagickSignature);
1885 if (image->debug != MagickFalse)
1886 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1887 assert(exception != (ExceptionInfo *) NULL);
1888 assert(exception->signature == MagickSignature);
1889 if ((x_shear != 0.0) && (fmod(x_shear,90.0) == 0.0))
1890 ThrowImageException(ImageError,"AngleIsDiscontinuous");
1891 if ((y_shear != 0.0) && (fmod(y_shear,90.0) == 0.0))
1892 ThrowImageException(ImageError,"AngleIsDiscontinuous");
1894 Initialize shear angle.
1896 integral_image=CloneImage(image,0,0,MagickTrue,exception);
1897 if (integral_image == (Image *) NULL)
1898 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1899 shear.x=(-tan(DegreesToRadians(fmod(x_shear,360.0))));
1900 shear.y=tan(DegreesToRadians(fmod(y_shear,360.0)));
1901 if ((shear.x == 0.0) && (shear.y == 0.0))
1902 return(integral_image);
1903 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
1905 integral_image=DestroyImage(integral_image);
1906 return(integral_image);
1908 if (integral_image->matte == MagickFalse)
1909 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
1913 y_width=image->columns+(ssize_t) floor(fabs(shear.x)*image->rows+0.5);
1914 x_offset=(ssize_t) ceil((double) image->columns+((fabs(shear.x)*image->rows)-
1915 image->columns)/2.0-0.5);
1916 y_offset=(ssize_t) ceil((double) image->rows+((fabs(shear.y)*y_width)-
1917 image->rows)/2.0-0.5);
1919 Surround image with border.
1921 integral_image->border_color=integral_image->background_color;
1922 integral_image->compose=CopyCompositeOp;
1923 border_info.width=(size_t) x_offset;
1924 border_info.height=(size_t) y_offset;
1925 shear_image=BorderImage(integral_image,&border_info,image->compose,exception);
1926 integral_image=DestroyImage(integral_image);
1927 if (shear_image == (Image *) NULL)
1928 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1932 if (shear_image->matte == MagickFalse)
1933 (void) SetImageAlphaChannel(shear_image,OpaqueAlphaChannel,exception);
1934 status=XShearImage(shear_image,shear.x,image->columns,image->rows,x_offset,
1935 (ssize_t) (shear_image->rows-image->rows)/2,exception);
1936 if (status == MagickFalse)
1938 shear_image=DestroyImage(shear_image);
1939 return((Image *) NULL);
1941 status=YShearImage(shear_image,shear.y,y_width,image->rows,(ssize_t)
1942 (shear_image->columns-y_width)/2,y_offset,exception);
1943 if (status == MagickFalse)
1945 shear_image=DestroyImage(shear_image);
1946 return((Image *) NULL);
1948 status=CropToFitImage(&shear_image,shear.x,shear.y,(MagickRealType)
1949 image->columns,(MagickRealType) image->rows,MagickFalse,exception);
1950 if (status == MagickFalse)
1952 shear_image=DestroyImage(shear_image);
1953 return((Image *) NULL);
1955 shear_image->compose=image->compose;
1956 shear_image->page.width=0;
1957 shear_image->page.height=0;
1958 return(shear_image);
1962 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1966 % S h e a r R o t a t e I m a g e %
1970 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1972 % ShearRotateImage() creates a new image that is a rotated copy of an existing
1973 % one. Positive angles rotate counter-clockwise (right-hand rule), while
1974 % negative angles rotate clockwise. Rotated images are usually larger than
1975 % the originals and have 'empty' triangular corners. X axis. Empty
1976 % triangles left over from shearing the image are filled with the background
1977 % color defined by member 'background_color' of the image. ShearRotateImage
1978 % allocates the memory necessary for the new Image structure and returns a
1979 % pointer to the new image.
1981 % ShearRotateImage() is based on the paper "A Fast Algorithm for General
1982 % Raster Rotatation" by Alan W. Paeth. ShearRotateImage is adapted from a
1983 % similar method based on the Paeth paper written by Michael Halle of the
1984 % Spatial Imaging Group, MIT Media Lab.
1986 % The format of the ShearRotateImage method is:
1988 % Image *ShearRotateImage(const Image *image,const double degrees,
1989 % ExceptionInfo *exception)
1991 % A description of each parameter follows.
1993 % o image: the image.
1995 % o degrees: Specifies the number of degrees to rotate the image.
1997 % o exception: return any errors or warnings in this structure.
2000 MagickExport Image *ShearRotateImage(const Image *image,const double degrees,
2001 ExceptionInfo *exception)
2030 Adjust rotation angle.
2032 assert(image != (Image *) NULL);
2033 assert(image->signature == MagickSignature);
2034 if (image->debug != MagickFalse)
2035 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2036 assert(exception != (ExceptionInfo *) NULL);
2037 assert(exception->signature == MagickSignature);
2039 while (angle < -45.0)
2041 for (rotations=0; angle > 45.0; rotations++)
2045 Calculate shear equations.
2047 integral_image=IntegralRotateImage(image,rotations,exception);
2048 if (integral_image == (Image *) NULL)
2049 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2050 shear.x=(-tan((double) DegreesToRadians(angle)/2.0));
2051 shear.y=sin((double) DegreesToRadians(angle));
2052 if ((shear.x == 0.0) && (shear.y == 0.0))
2053 return(integral_image);
2054 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
2056 integral_image=DestroyImage(integral_image);
2057 return(integral_image);
2059 if (integral_image->matte == MagickFalse)
2060 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
2064 width=image->columns;
2066 if ((rotations == 1) || (rotations == 3))
2069 height=image->columns;
2071 y_width=width+(ssize_t) floor(fabs(shear.x)*height+0.5);
2072 x_offset=(ssize_t) ceil((double) width+((fabs(shear.y)*height)-width)/2.0-
2074 y_offset=(ssize_t) ceil((double) height+((fabs(shear.y)*y_width)-height)/2.0-
2077 Surround image with a border.
2079 integral_image->border_color=integral_image->background_color;
2080 integral_image->compose=CopyCompositeOp;
2081 border_info.width=(size_t) x_offset;
2082 border_info.height=(size_t) y_offset;
2083 rotate_image=BorderImage(integral_image,&border_info,image->compose,
2085 integral_image=DestroyImage(integral_image);
2086 if (rotate_image == (Image *) NULL)
2087 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2091 status=XShearImage(rotate_image,shear.x,width,height,x_offset,(ssize_t)
2092 (rotate_image->rows-height)/2,exception);
2093 if (status == MagickFalse)
2095 rotate_image=DestroyImage(rotate_image);
2096 return((Image *) NULL);
2098 status=YShearImage(rotate_image,shear.y,y_width,height,(ssize_t)
2099 (rotate_image->columns-y_width)/2,y_offset,exception);
2100 if (status == MagickFalse)
2102 rotate_image=DestroyImage(rotate_image);
2103 return((Image *) NULL);
2105 status=XShearImage(rotate_image,shear.x,y_width,rotate_image->rows,(ssize_t)
2106 (rotate_image->columns-y_width)/2,0,exception);
2107 if (status == MagickFalse)
2109 rotate_image=DestroyImage(rotate_image);
2110 return((Image *) NULL);
2112 status=CropToFitImage(&rotate_image,shear.x,shear.y,(MagickRealType) width,
2113 (MagickRealType) height,MagickTrue,exception);
2114 if (status == MagickFalse)
2116 rotate_image=DestroyImage(rotate_image);
2117 return((Image *) NULL);
2119 rotate_image->compose=image->compose;
2120 rotate_image->page.width=0;
2121 rotate_image->page.height=0;
2122 return(rotate_image);