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)
577 for (x=0; x < (ssize_t) p->width; x++)
586 for (y=0; y < (ssize_t) (p->height-1); y++)
591 delta=GetRadonCell(p,x,y)-(ssize_t) GetRadonCell(p,x,y+1);
594 projection[p->width+sign*x-1]=sum;
598 static MagickBooleanType RadonTransform(const Image *image,
599 const double threshold,size_t *projection,ExceptionInfo *exception)
627 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
628 source_cells=AcquireRadonInfo(image,width,image->rows,exception);
629 destination_cells=AcquireRadonInfo(image,width,image->rows,exception);
630 if ((source_cells == (RadonInfo *) NULL) ||
631 (destination_cells == (RadonInfo *) NULL))
633 if (destination_cells != (RadonInfo *) NULL)
634 destination_cells=DestroyRadonInfo(destination_cells);
635 if (source_cells != (RadonInfo *) NULL)
636 source_cells=DestroyRadonInfo(source_cells);
639 if (ResetRadonCells(source_cells) == MagickFalse)
641 destination_cells=DestroyRadonInfo(destination_cells);
642 source_cells=DestroyRadonInfo(source_cells);
645 for (i=0; i < 256; i++)
647 byte=(unsigned char) i;
648 for (count=0; byte != 0; byte>>=1)
650 bits[i]=(unsigned short) count;
653 image_view=AcquireCacheView(image);
654 #if defined(MAGICKCORE_OPENMP_SUPPORT)
655 #pragma omp parallel for schedule(static,4) shared(status)
657 for (y=0; y < (ssize_t) image->rows; y++)
659 register const Quantum
670 if (status == MagickFalse)
672 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
673 if (p == (const Quantum *) NULL)
680 i=(ssize_t) (image->columns+7)/8;
681 for (x=0; x < (ssize_t) image->columns; x++)
684 if ((double) GetPixelIntensity(image,p) < threshold)
689 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
693 p+=GetPixelChannels(image);
698 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
701 RadonProjection(source_cells,destination_cells,-1,projection);
702 (void) ResetRadonCells(source_cells);
703 #if defined(MAGICKCORE_OPENMP_SUPPORT)
704 #pragma omp parallel for schedule(static,4) shared(status)
706 for (y=0; y < (ssize_t) image->rows; y++)
708 register const Quantum
719 if (status == MagickFalse)
721 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
722 if (p == (const Quantum *) NULL)
730 for (x=0; x < (ssize_t) image->columns; x++)
733 if ((double) GetPixelIntensity(image,p) < threshold)
738 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
742 p+=GetPixelChannels(image);
747 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
750 RadonProjection(source_cells,destination_cells,1,projection);
751 image_view=DestroyCacheView(image_view);
752 destination_cells=DestroyRadonInfo(destination_cells);
753 source_cells=DestroyRadonInfo(source_cells);
757 static void GetImageBackgroundColor(Image *image,const ssize_t offset,
758 ExceptionInfo *exception)
773 Compute average background color.
777 GetPixelInfo(image,&background);
779 image_view=AcquireCacheView(image);
780 for (y=0; y < (ssize_t) image->rows; y++)
782 register const Quantum
788 if ((y >= offset) && (y < ((ssize_t) image->rows-offset)))
790 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
791 if (p == (const Quantum *) NULL)
793 for (x=0; x < (ssize_t) image->columns; x++)
795 if ((x >= offset) && (x < ((ssize_t) image->columns-offset)))
797 background.red+=QuantumScale*GetPixelRed(image,p);
798 background.green+=QuantumScale*GetPixelGreen(image,p);
799 background.blue+=QuantumScale*GetPixelBlue(image,p);
800 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
801 background.alpha+=QuantumScale*GetPixelAlpha(image,p);
803 p+=GetPixelChannels(image);
806 image_view=DestroyCacheView(image_view);
807 image->background_color.red=(double) ClampToQuantum((MagickRealType)
808 QuantumRange*background.red/count);
809 image->background_color.green=(double) ClampToQuantum((MagickRealType)
810 QuantumRange*background.green/count);
811 image->background_color.blue=(double) ClampToQuantum((MagickRealType)
812 QuantumRange*background.blue/count);
813 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
814 image->background_color.alpha=(double) ClampToQuantum((MagickRealType)
815 QuantumRange*background.alpha/count);
818 MagickExport Image *DeskewImage(const Image *image,const double threshold,
819 ExceptionInfo *exception)
854 Compute deskew angle.
856 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
857 projection=(size_t *) AcquireQuantumMemory((size_t) (2*width-1),
858 sizeof(*projection));
859 if (projection == (size_t *) NULL)
860 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
861 status=RadonTransform(image,threshold,projection,exception);
862 if (status == MagickFalse)
864 projection=(size_t *) RelinquishMagickMemory(projection);
865 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
869 for (i=0; i < (ssize_t) (2*width-1); i++)
871 if (projection[i] > max_projection)
873 skew=i-(ssize_t) width+1;
874 max_projection=projection[i];
877 projection=(size_t *) RelinquishMagickMemory(projection);
881 clone_image=CloneImage(image,0,0,MagickTrue,exception);
882 if (clone_image == (Image *) NULL)
883 return((Image *) NULL);
884 (void) SetImageVirtualPixelMethod(clone_image,BackgroundVirtualPixelMethod);
885 degrees=RadiansToDegrees(-atan((double) skew/width/8));
886 if (image->debug != MagickFalse)
887 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
888 " Deskew angle: %g",degrees);
889 affine_matrix.sx=cos(DegreesToRadians(fmod((double) degrees,360.0)));
890 affine_matrix.rx=sin(DegreesToRadians(fmod((double) degrees,360.0)));
891 affine_matrix.ry=(-sin(DegreesToRadians(fmod((double) degrees,360.0))));
892 affine_matrix.sy=cos(DegreesToRadians(fmod((double) degrees,360.0)));
893 affine_matrix.tx=0.0;
894 affine_matrix.ty=0.0;
895 artifact=GetImageArtifact(image,"deskew:auto-crop");
896 if (artifact == (const char *) NULL)
898 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
899 clone_image=DestroyImage(clone_image);
900 return(deskew_image);
905 GetImageBackgroundColor(clone_image,(ssize_t) StringToLong(artifact),
907 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
908 clone_image=DestroyImage(clone_image);
909 if (deskew_image == (Image *) NULL)
910 return((Image *) NULL);
911 median_image=StatisticImage(deskew_image,MedianStatistic,3,3,exception);
912 if (median_image == (Image *) NULL)
914 deskew_image=DestroyImage(deskew_image);
915 return((Image *) NULL);
917 geometry=GetImageBoundingBox(median_image,exception);
918 median_image=DestroyImage(median_image);
919 if (image->debug != MagickFalse)
920 (void) LogMagickEvent(TransformEvent,GetMagickModule()," Deskew geometry: "
921 "%.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
922 geometry.height,(double) geometry.x,(double) geometry.y);
923 crop_image=CropImage(deskew_image,&geometry,exception);
924 deskew_image=DestroyImage(deskew_image);
929 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
933 % I n t e g r a l R o t a t e I m a g e %
937 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
939 % IntegralRotateImage() rotates the image an integral of 90 degrees. It
940 % allocates the memory necessary for the new Image structure and returns a
941 % pointer to the rotated image.
943 % The format of the IntegralRotateImage method is:
945 % Image *IntegralRotateImage(const Image *image,size_t rotations,
946 % ExceptionInfo *exception)
948 % A description of each parameter follows.
950 % o image: the image.
952 % o rotations: Specifies the number of 90 degree rotations.
955 MagickExport Image *IntegralRotateImage(const Image *image,size_t rotations,
956 ExceptionInfo *exception)
958 #define RotateImageTag "Rotate/Image"
980 Initialize rotated image attributes.
982 assert(image != (Image *) NULL);
986 return(CloneImage(image,0,0,MagickTrue,exception));
987 if ((rotations == 1) || (rotations == 3))
988 rotate_image=CloneImage(image,image->rows,image->columns,MagickTrue,
991 rotate_image=CloneImage(image,image->columns,image->rows,MagickTrue,
993 if (rotate_image == (Image *) NULL)
994 return((Image *) NULL);
996 Integral rotate the image.
1000 image_view=AcquireCacheView(image);
1001 rotate_view=AcquireCacheView(rotate_image);
1023 GetPixelCacheTileSize(image,&tile_width,&tile_height);
1024 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1025 #pragma omp parallel for schedule(static,4) shared(progress,status)
1027 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
1032 if (status == MagickFalse)
1035 for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
1040 register const Quantum
1054 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
1055 width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
1057 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
1058 height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
1059 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1061 if (p == (const Quantum *) NULL)
1066 for (y=0; y < (ssize_t) width; y++)
1068 register const Quantum
1069 *restrict tile_pixels;
1074 if (status == MagickFalse)
1076 q=QueueCacheViewAuthenticPixels(rotate_view,(ssize_t)
1077 (rotate_image->columns-(tile_y+height)),y+tile_x,height,1,
1079 if (q == (Quantum *) NULL)
1084 tile_pixels=p+((height-1)*width+y)*GetPixelChannels(image);
1085 for (x=0; x < (ssize_t) height; x++)
1090 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1099 channel=GetPixelChannelMapChannel(image,i);
1100 traits=GetPixelChannelMapTraits(image,channel);
1101 rotate_traits=GetPixelChannelMapTraits(rotate_image,channel);
1102 if ((traits == UndefinedPixelTrait) ||
1103 (rotate_traits == UndefinedPixelTrait))
1105 SetPixelChannel(rotate_image,channel,tile_pixels[i],q);
1107 tile_pixels-=width*GetPixelChannels(image);
1108 q+=GetPixelChannels(rotate_image);
1110 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1111 if (sync == MagickFalse)
1115 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1120 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1121 #pragma omp critical (MagickCore_IntegralRotateImage)
1123 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
1125 if (proceed == MagickFalse)
1129 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1130 image->rows-1,image->rows);
1131 Swap(page.width,page.height);
1132 Swap(page.x,page.y);
1133 if (page.width != 0)
1134 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1142 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1143 #pragma omp parallel for schedule(static) shared(progress,status)
1145 for (y=0; y < (ssize_t) image->rows; y++)
1150 register const Quantum
1159 if (status == MagickFalse)
1161 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1162 q=QueueCacheViewAuthenticPixels(rotate_view,0,(ssize_t) (image->rows-y-
1163 1),image->columns,1,exception);
1164 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1169 q+=GetPixelChannels(rotate_image)*image->columns;
1170 for (x=0; x < (ssize_t) image->columns; x++)
1175 q-=GetPixelChannels(rotate_image);
1176 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1185 channel=GetPixelChannelMapChannel(image,i);
1186 traits=GetPixelChannelMapTraits(image,channel);
1187 rotate_traits=GetPixelChannelMapTraits(rotate_image,channel);
1188 if ((traits == UndefinedPixelTrait) ||
1189 (rotate_traits == UndefinedPixelTrait))
1191 SetPixelChannel(rotate_image,channel,p[i],q);
1193 p+=GetPixelChannels(image);
1195 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1196 if (sync == MagickFalse)
1198 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1203 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1204 #pragma omp critical (MagickCore_IntegralRotateImage)
1206 proceed=SetImageProgress(image,RotateImageTag,progress++,
1208 if (proceed == MagickFalse)
1212 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1213 image->rows-1,image->rows);
1214 Swap(page.width,page.height);
1215 Swap(page.x,page.y);
1216 if (page.width != 0)
1217 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1232 GetPixelCacheTileSize(image,&tile_width,&tile_height);
1233 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1234 #pragma omp parallel for schedule(static,4) shared(progress,status)
1236 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
1241 if (status == MagickFalse)
1244 for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
1249 register const Quantum
1263 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
1264 width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
1266 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
1267 height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
1268 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1270 if (p == (const Quantum *) NULL)
1275 for (y=0; y < (ssize_t) width; y++)
1277 register const Quantum
1278 *restrict tile_pixels;
1283 if (status == MagickFalse)
1285 q=QueueCacheViewAuthenticPixels(rotate_view,tile_y,(ssize_t) (y+
1286 rotate_image->rows-(tile_x+width)),height,1,exception);
1287 if (q == (Quantum *) NULL)
1292 tile_pixels=p+((width-1)-y)*GetPixelChannels(image);
1293 for (x=0; x < (ssize_t) height; x++)
1298 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1307 channel=GetPixelChannelMapChannel(image,i);
1308 traits=GetPixelChannelMapTraits(image,channel);
1309 rotate_traits=GetPixelChannelMapTraits(rotate_image,channel);
1310 if ((traits == UndefinedPixelTrait) ||
1311 (rotate_traits == UndefinedPixelTrait))
1313 SetPixelChannel(rotate_image,channel,tile_pixels[i],q);
1315 tile_pixels+=width*GetPixelChannels(image);
1316 q+=GetPixelChannels(rotate_image);
1318 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1319 if (sync == MagickFalse)
1323 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1328 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1329 #pragma omp critical (MagickCore_IntegralRotateImage)
1331 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
1333 if (proceed == MagickFalse)
1337 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1338 image->rows-1,image->rows);
1339 Swap(page.width,page.height);
1340 Swap(page.x,page.y);
1341 if (page.width != 0)
1342 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1346 rotate_view=DestroyCacheView(rotate_view);
1347 image_view=DestroyCacheView(image_view);
1348 rotate_image->type=image->type;
1349 rotate_image->page=page;
1350 if (status == MagickFalse)
1351 rotate_image=DestroyImage(rotate_image);
1352 return(rotate_image);
1356 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1360 + X S h e a r I m a g e %
1364 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1366 % XShearImage() shears the image in the X direction with a shear angle of
1367 % 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1368 % negative angles shear clockwise. Angles are measured relative to a vertical
1369 % Y-axis. X shears will widen an image creating 'empty' triangles on the left
1370 % and right sides of the source image.
1372 % The format of the XShearImage method is:
1374 % MagickBooleanType XShearImage(Image *image,const MagickRealType degrees,
1375 % const size_t width,const size_t height,
1376 % const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
1378 % A description of each parameter follows.
1380 % o image: the image.
1382 % o degrees: A MagickRealType representing the shearing angle along the X
1385 % o width, height, x_offset, y_offset: Defines a region of the image
1388 % o exception: return any errors or warnings in this structure.
1391 static MagickBooleanType XShearImage(Image *image,const MagickRealType degrees,
1392 const size_t width,const size_t height,const ssize_t x_offset,
1393 const ssize_t y_offset,ExceptionInfo *exception)
1395 #define XShearImageTag "XShear/Image"
1421 assert(image != (Image *) NULL);
1422 assert(image->signature == MagickSignature);
1423 if (image->debug != MagickFalse)
1424 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1426 background=image->background_color;
1428 image_view=AcquireCacheView(image);
1429 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1430 #pragma omp parallel for schedule(static,4) shared(progress,status)
1432 for (y=0; y < (ssize_t) height; y++)
1456 if (status == MagickFalse)
1458 p=GetCacheViewAuthenticPixels(image_view,0,y_offset+y,image->columns,1,
1460 if (p == (Quantum *) NULL)
1465 p+=x_offset*GetPixelChannels(image);
1466 displacement=degrees*(MagickRealType) (y-height/2.0);
1467 if (displacement == 0.0)
1469 if (displacement > 0.0)
1473 displacement*=(-1.0);
1476 step=(ssize_t) floor((double) displacement);
1477 area=(MagickRealType) (displacement-step);
1480 GetPixelInfo(image,&source);
1481 GetPixelInfo(image,&destination);
1487 Transfer pixels left-to-right.
1489 if (step > x_offset)
1491 q=p-step*GetPixelChannels(image);
1492 for (i=0; i < (ssize_t) width; i++)
1494 if ((x_offset+i) < step)
1496 p+=GetPixelChannels(image);
1497 GetPixelInfoPixel(image,p,&pixel);
1498 q+=GetPixelChannels(image);
1501 GetPixelInfoPixel(image,p,&source);
1502 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1503 &source,(MagickRealType) GetPixelAlpha(image,p),area,&destination);
1504 SetPixelInfoPixel(image,&destination,q);
1505 GetPixelInfoPixel(image,p,&pixel);
1506 p+=GetPixelChannels(image);
1507 q+=GetPixelChannels(image);
1509 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1510 &background,(MagickRealType) background.alpha,area,&destination);
1511 SetPixelInfoPixel(image,&destination,q);
1512 q+=GetPixelChannels(image);
1513 for (i=0; i < (step-1); i++)
1515 SetPixelInfoPixel(image,&background,q);
1516 q+=GetPixelChannels(image);
1523 Transfer pixels right-to-left.
1525 p+=width*GetPixelChannels(image);
1526 q=p+step*GetPixelChannels(image);
1527 for (i=0; i < (ssize_t) width; i++)
1529 p-=GetPixelChannels(image);
1530 q-=GetPixelChannels(image);
1531 if ((size_t) (x_offset+width+step-i) >= image->columns)
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);
1539 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1540 &background,(MagickRealType) background.alpha,area,&destination);
1541 q-=GetPixelChannels(image);
1542 SetPixelInfoPixel(image,&destination,q);
1543 for (i=0; i < (step-1); i++)
1545 q-=GetPixelChannels(image);
1546 SetPixelInfoPixel(image,&background,q);
1551 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1553 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1558 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1559 #pragma omp critical (MagickCore_XShearImage)
1561 proceed=SetImageProgress(image,XShearImageTag,progress++,height);
1562 if (proceed == MagickFalse)
1566 image_view=DestroyCacheView(image_view);
1571 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1575 + Y S h e a r I m a g e %
1579 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1581 % YShearImage shears the image in the Y direction with a shear angle of
1582 % 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1583 % negative angles shear clockwise. Angles are measured relative to a
1584 % horizontal X-axis. Y shears will increase the height of an image creating
1585 % 'empty' triangles on the top and bottom of the source image.
1587 % The format of the YShearImage method is:
1589 % MagickBooleanType YShearImage(Image *image,const MagickRealType degrees,
1590 % const size_t width,const size_t height,
1591 % const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
1593 % A description of each parameter follows.
1595 % o image: the image.
1597 % o degrees: A MagickRealType representing the shearing angle along the Y
1600 % o width, height, x_offset, y_offset: Defines a region of the image
1603 % o exception: return any errors or warnings in this structure.
1606 static MagickBooleanType YShearImage(Image *image,const MagickRealType degrees,
1607 const size_t width,const size_t height,const ssize_t x_offset,
1608 const ssize_t y_offset,ExceptionInfo *exception)
1610 #define YShearImageTag "YShear/Image"
1636 assert(image != (Image *) NULL);
1637 assert(image->signature == MagickSignature);
1638 if (image->debug != MagickFalse)
1639 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1642 background=image->background_color;
1643 image_view=AcquireCacheView(image);
1644 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1645 #pragma omp parallel for schedule(static,4) shared(progress,status)
1647 for (x=0; x < (ssize_t) width; x++)
1671 if (status == MagickFalse)
1673 p=GetCacheViewAuthenticPixels(image_view,x_offset+x,0,1,image->rows,
1675 if (p == (Quantum *) NULL)
1680 p+=y_offset*GetPixelChannels(image);
1681 displacement=degrees*(MagickRealType) (x-width/2.0);
1682 if (displacement == 0.0)
1684 if (displacement > 0.0)
1688 displacement*=(-1.0);
1691 step=(ssize_t) floor((double) displacement);
1692 area=(MagickRealType) (displacement-step);
1695 GetPixelInfo(image,&source);
1696 GetPixelInfo(image,&destination);
1702 Transfer pixels top-to-bottom.
1704 if (step > y_offset)
1706 q=p-step*GetPixelChannels(image);
1707 for (i=0; i < (ssize_t) height; i++)
1709 if ((y_offset+i) < step)
1711 p+=GetPixelChannels(image);
1712 GetPixelInfoPixel(image,p,&pixel);
1713 q+=GetPixelChannels(image);
1716 GetPixelInfoPixel(image,p,&source);
1717 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1718 &source,(MagickRealType) GetPixelAlpha(image,p),area,
1720 SetPixelInfoPixel(image,&destination,q);
1721 GetPixelInfoPixel(image,p,&pixel);
1722 p+=GetPixelChannels(image);
1723 q+=GetPixelChannels(image);
1725 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1726 &background,(MagickRealType) background.alpha,area,&destination);
1727 SetPixelInfoPixel(image,&destination,q);
1728 q+=GetPixelChannels(image);
1729 for (i=0; i < (step-1); i++)
1731 SetPixelInfoPixel(image,&background,q);
1732 q+=GetPixelChannels(image);
1739 Transfer pixels bottom-to-top.
1741 p+=height*GetPixelChannels(image);
1742 q=p+step*GetPixelChannels(image);
1743 for (i=0; i < (ssize_t) height; i++)
1745 p-=GetPixelChannels(image);
1746 q-=GetPixelChannels(image);
1747 if ((size_t) (y_offset+height+step-i) >= image->rows)
1749 GetPixelInfoPixel(image,p,&source);
1750 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1751 &source,(MagickRealType) GetPixelAlpha(image,p),area,
1753 SetPixelInfoPixel(image,&destination,q);
1754 GetPixelInfoPixel(image,p,&pixel);
1756 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1757 &background,(MagickRealType) background.alpha,area,&destination);
1758 q-=GetPixelChannels(image);
1759 SetPixelInfoPixel(image,&destination,q);
1760 for (i=0; i < (step-1); i++)
1762 q-=GetPixelChannels(image);
1763 SetPixelInfoPixel(image,&background,q);
1768 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1770 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1775 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1776 #pragma omp critical (MagickCore_YShearImage)
1778 proceed=SetImageProgress(image,YShearImageTag,progress++,image->rows);
1779 if (proceed == MagickFalse)
1783 image_view=DestroyCacheView(image_view);
1788 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1792 % S h e a r I m a g e %
1796 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1798 % ShearImage() creates a new image that is a shear_image copy of an existing
1799 % one. Shearing slides one edge of an image along the X or Y axis, creating
1800 % a parallelogram. An X direction shear slides an edge along the X axis,
1801 % while a Y direction shear slides an edge along the Y axis. The amount of
1802 % the shear is controlled by a shear angle. For X direction shears, x_shear
1803 % is measured relative to the Y axis, and similarly, for Y direction shears
1804 % y_shear is measured relative to the X axis. Empty triangles left over from
1805 % shearing the image are filled with the background color defined by member
1806 % 'background_color' of the image.. ShearImage() allocates the memory
1807 % necessary for the new Image structure and returns a pointer to the new image.
1809 % ShearImage() is based on the paper "A Fast Algorithm for General Raster
1810 % Rotatation" by Alan W. Paeth.
1812 % The format of the ShearImage method is:
1814 % Image *ShearImage(const Image *image,const double x_shear,
1815 % const double y_shear,ExceptionInfo *exception)
1817 % A description of each parameter follows.
1819 % o image: the image.
1821 % o x_shear, y_shear: Specifies the number of degrees to shear the image.
1823 % o exception: return any errors or warnings in this structure.
1826 MagickExport Image *ShearImage(const Image *image,const double x_shear,
1827 const double y_shear,ExceptionInfo *exception)
1849 assert(image != (Image *) NULL);
1850 assert(image->signature == MagickSignature);
1851 if (image->debug != MagickFalse)
1852 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1853 assert(exception != (ExceptionInfo *) NULL);
1854 assert(exception->signature == MagickSignature);
1855 if ((x_shear != 0.0) && (fmod(x_shear,90.0) == 0.0))
1856 ThrowImageException(ImageError,"AngleIsDiscontinuous");
1857 if ((y_shear != 0.0) && (fmod(y_shear,90.0) == 0.0))
1858 ThrowImageException(ImageError,"AngleIsDiscontinuous");
1860 Initialize shear angle.
1862 integral_image=CloneImage(image,0,0,MagickTrue,exception);
1863 if (integral_image == (Image *) NULL)
1864 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1865 shear.x=(-tan(DegreesToRadians(fmod(x_shear,360.0))));
1866 shear.y=tan(DegreesToRadians(fmod(y_shear,360.0)));
1867 if ((shear.x == 0.0) && (shear.y == 0.0))
1868 return(integral_image);
1869 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
1871 integral_image=DestroyImage(integral_image);
1872 return(integral_image);
1874 if (integral_image->matte == MagickFalse)
1875 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
1879 y_width=image->columns+(ssize_t) floor(fabs(shear.x)*image->rows+0.5);
1880 x_offset=(ssize_t) ceil((double) image->columns+((fabs(shear.x)*image->rows)-
1881 image->columns)/2.0-0.5);
1882 y_offset=(ssize_t) ceil((double) image->rows+((fabs(shear.y)*y_width)-
1883 image->rows)/2.0-0.5);
1885 Surround image with border.
1887 integral_image->border_color=integral_image->background_color;
1888 integral_image->compose=CopyCompositeOp;
1889 border_info.width=(size_t) x_offset;
1890 border_info.height=(size_t) y_offset;
1891 shear_image=BorderImage(integral_image,&border_info,image->compose,exception);
1892 integral_image=DestroyImage(integral_image);
1893 if (shear_image == (Image *) NULL)
1894 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1898 if (shear_image->matte == MagickFalse)
1899 (void) SetImageAlphaChannel(shear_image,OpaqueAlphaChannel,exception);
1900 status=XShearImage(shear_image,shear.x,image->columns,image->rows,x_offset,
1901 (ssize_t) (shear_image->rows-image->rows)/2,exception);
1902 if (status == MagickFalse)
1904 shear_image=DestroyImage(shear_image);
1905 return((Image *) NULL);
1907 status=YShearImage(shear_image,shear.y,y_width,image->rows,(ssize_t)
1908 (shear_image->columns-y_width)/2,y_offset,exception);
1909 if (status == MagickFalse)
1911 shear_image=DestroyImage(shear_image);
1912 return((Image *) NULL);
1914 status=CropToFitImage(&shear_image,shear.x,shear.y,(MagickRealType)
1915 image->columns,(MagickRealType) image->rows,MagickFalse,exception);
1916 if (status == MagickFalse)
1918 shear_image=DestroyImage(shear_image);
1919 return((Image *) NULL);
1921 shear_image->compose=image->compose;
1922 shear_image->page.width=0;
1923 shear_image->page.height=0;
1924 return(shear_image);
1928 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1932 % S h e a r R o t a t e I m a g e %
1936 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1938 % ShearRotateImage() creates a new image that is a rotated copy of an existing
1939 % one. Positive angles rotate counter-clockwise (right-hand rule), while
1940 % negative angles rotate clockwise. Rotated images are usually larger than
1941 % the originals and have 'empty' triangular corners. X axis. Empty
1942 % triangles left over from shearing the image are filled with the background
1943 % color defined by member 'background_color' of the image. ShearRotateImage
1944 % allocates the memory necessary for the new Image structure and returns a
1945 % pointer to the new image.
1947 % ShearRotateImage() is based on the paper "A Fast Algorithm for General
1948 % Raster Rotatation" by Alan W. Paeth. ShearRotateImage is adapted from a
1949 % similar method based on the Paeth paper written by Michael Halle of the
1950 % Spatial Imaging Group, MIT Media Lab.
1952 % The format of the ShearRotateImage method is:
1954 % Image *ShearRotateImage(const Image *image,const double degrees,
1955 % ExceptionInfo *exception)
1957 % A description of each parameter follows.
1959 % o image: the image.
1961 % o degrees: Specifies the number of degrees to rotate the image.
1963 % o exception: return any errors or warnings in this structure.
1966 MagickExport Image *ShearRotateImage(const Image *image,const double degrees,
1967 ExceptionInfo *exception)
1996 Adjust rotation angle.
1998 assert(image != (Image *) NULL);
1999 assert(image->signature == MagickSignature);
2000 if (image->debug != MagickFalse)
2001 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2002 assert(exception != (ExceptionInfo *) NULL);
2003 assert(exception->signature == MagickSignature);
2005 while (angle < -45.0)
2007 for (rotations=0; angle > 45.0; rotations++)
2011 Calculate shear equations.
2013 integral_image=IntegralRotateImage(image,rotations,exception);
2014 if (integral_image == (Image *) NULL)
2015 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2016 shear.x=(-tan((double) DegreesToRadians(angle)/2.0));
2017 shear.y=sin((double) DegreesToRadians(angle));
2018 if ((shear.x == 0.0) && (shear.y == 0.0))
2019 return(integral_image);
2020 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
2022 integral_image=DestroyImage(integral_image);
2023 return(integral_image);
2025 if (integral_image->matte == MagickFalse)
2026 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
2030 width=image->columns;
2032 if ((rotations == 1) || (rotations == 3))
2035 height=image->columns;
2037 y_width=width+(ssize_t) floor(fabs(shear.x)*height+0.5);
2038 x_offset=(ssize_t) ceil((double) width+((fabs(shear.y)*height)-width)/2.0-
2040 y_offset=(ssize_t) ceil((double) height+((fabs(shear.y)*y_width)-height)/2.0-
2043 Surround image with a border.
2045 integral_image->border_color=integral_image->background_color;
2046 integral_image->compose=CopyCompositeOp;
2047 border_info.width=(size_t) x_offset;
2048 border_info.height=(size_t) y_offset;
2049 rotate_image=BorderImage(integral_image,&border_info,image->compose,
2051 integral_image=DestroyImage(integral_image);
2052 if (rotate_image == (Image *) NULL)
2053 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2057 status=XShearImage(rotate_image,shear.x,width,height,x_offset,(ssize_t)
2058 (rotate_image->rows-height)/2,exception);
2059 if (status == MagickFalse)
2061 rotate_image=DestroyImage(rotate_image);
2062 return((Image *) NULL);
2064 status=YShearImage(rotate_image,shear.y,y_width,height,(ssize_t)
2065 (rotate_image->columns-y_width)/2,y_offset,exception);
2066 if (status == MagickFalse)
2068 rotate_image=DestroyImage(rotate_image);
2069 return((Image *) NULL);
2071 status=XShearImage(rotate_image,shear.x,y_width,rotate_image->rows,(ssize_t)
2072 (rotate_image->columns-y_width)/2,0,exception);
2073 if (status == MagickFalse)
2075 rotate_image=DestroyImage(rotate_image);
2076 return((Image *) NULL);
2078 status=CropToFitImage(&rotate_image,shear.x,shear.y,(MagickRealType) width,
2079 (MagickRealType) height,MagickTrue,exception);
2080 if (status == MagickFalse)
2082 rotate_image=DestroyImage(rotate_image);
2083 return((Image *) NULL);
2085 rotate_image->compose=image->compose;
2086 rotate_image->page.width=0;
2087 rotate_image->page.height=0;
2088 return(rotate_image);