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-2010 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 RotateImage, XShearImage, and YShearImage methods are based on the
37 % paper "A Fast Algorithm for General Raster Rotatation" by Alan W. Paeth,
38 % Graphics Interface '86 (Vancouver). RotateImage 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.
48 #include "magick/studio.h"
49 #include "magick/artifact.h"
50 #include "magick/attribute.h"
51 #include "magick/blob-private.h"
52 #include "magick/cache-private.h"
53 #include "magick/color-private.h"
54 #include "magick/colorspace-private.h"
55 #include "magick/composite.h"
56 #include "magick/composite-private.h"
57 #include "magick/decorate.h"
58 #include "magick/distort.h"
59 #include "magick/draw.h"
60 #include "magick/exception.h"
61 #include "magick/exception-private.h"
62 #include "magick/gem.h"
63 #include "magick/geometry.h"
64 #include "magick/image.h"
65 #include "magick/image-private.h"
66 #include "magick/memory_.h"
67 #include "magick/list.h"
68 #include "magick/monitor.h"
69 #include "magick/monitor-private.h"
70 #include "magick/pixel-private.h"
71 #include "magick/quantum.h"
72 #include "magick/resource_.h"
73 #include "magick/shear.h"
74 #include "magick/statistic.h"
75 #include "magick/string_.h"
76 #include "magick/string-private.h"
77 #include "magick/threshold.h"
78 #include "magick/transform.h"
81 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
85 % A f f i n e T r a n s f o r m I m a g e %
89 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
91 % AffineTransformImage() transforms an image as dictated by the affine matrix.
92 % It allocates the memory necessary for the new Image structure and returns
93 % a pointer to the new image.
95 % The format of the AffineTransformImage method is:
97 % Image *AffineTransformImage(const Image *image,
98 % AffineMatrix *affine_matrix,ExceptionInfo *exception)
100 % A description of each parameter follows:
102 % o image: the image.
104 % o affine_matrix: the affine matrix.
106 % o exception: return any errors or warnings in this structure.
109 MagickExport Image *AffineTransformImage(const Image *image,
110 const AffineMatrix *affine_matrix,ExceptionInfo *exception)
119 Affine transform image.
121 assert(image->signature == MagickSignature);
122 if (image->debug != MagickFalse)
123 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
124 assert(affine_matrix != (AffineMatrix *) NULL);
125 assert(exception != (ExceptionInfo *) NULL);
126 assert(exception->signature == MagickSignature);
127 distort[0]=affine_matrix->sx;
128 distort[1]=affine_matrix->rx;
129 distort[2]=affine_matrix->ry;
130 distort[3]=affine_matrix->sy;
131 distort[4]=affine_matrix->tx;
132 distort[5]=affine_matrix->ty;
133 deskew_image=DistortImage(image,AffineProjectionDistortion,6,distort,
134 MagickTrue,exception);
135 return(deskew_image);
139 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
143 + C r o p T o F i t I m a g e %
147 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149 % CropToFitImage() crops the sheared image as determined by the bounding box
150 % as defined by width and height and shearing angles.
152 % The format of the CropToFitImage method is:
154 % MagickBooleanType CropToFitImage(Image **image,
155 % const MagickRealType x_shear,const MagickRealType x_shear,
156 % const MagickRealType width,const MagickRealType height,
157 % const MagickBooleanType rotate,ExceptionInfo *exception)
159 % A description of each parameter follows.
161 % o image: the image.
163 % o x_shear, y_shear, width, height: Defines a region of the image to crop.
165 % o exception: return any errors or warnings in this structure.
168 static MagickBooleanType CropToFitImage(Image **image,
169 const MagickRealType x_shear,const MagickRealType y_shear,
170 const MagickRealType width,const MagickRealType height,
171 const MagickBooleanType rotate,ExceptionInfo *exception)
189 Calculate the rotated image size.
191 extent[0].x=(double) (-width/2.0);
192 extent[0].y=(double) (-height/2.0);
193 extent[1].x=(double) width/2.0;
194 extent[1].y=(double) (-height/2.0);
195 extent[2].x=(double) (-width/2.0);
196 extent[2].y=(double) height/2.0;
197 extent[3].x=(double) width/2.0;
198 extent[3].y=(double) height/2.0;
199 for (i=0; i < 4; i++)
201 extent[i].x+=x_shear*extent[i].y;
202 extent[i].y+=y_shear*extent[i].x;
203 if (rotate != MagickFalse)
204 extent[i].x+=x_shear*extent[i].y;
205 extent[i].x+=(double) (*image)->columns/2.0;
206 extent[i].y+=(double) (*image)->rows/2.0;
210 for (i=1; i < 4; i++)
212 if (min.x > extent[i].x)
214 if (min.y > extent[i].y)
216 if (max.x < extent[i].x)
218 if (max.y < extent[i].y)
221 geometry.x=(long) ceil(min.x-0.5);
222 geometry.y=(long) ceil(min.y-0.5);
223 geometry.width=(unsigned long) floor(max.x-min.x+0.5);
224 geometry.height=(unsigned long) floor(max.y-min.y+0.5);
226 (void) ParseAbsoluteGeometry("0x0+0+0",&(*image)->page);
227 crop_image=CropImage(*image,&geometry,exception);
228 if (crop_image == (Image *) NULL)
230 crop_image->page=page;
231 *image=DestroyImage(*image);
237 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
241 % D e s k e w I m a g e %
245 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
247 % DeskewImage() removes skew from the image. Skew is an artifact that
248 % occurs in scanned images because of the camera being misaligned,
249 % imperfections in the scanning or surface, or simply because the paper was
250 % not placed completely flat when scanned.
252 % The format of the DeskewImage method is:
254 % Image *DeskewImage(const Image *image,const double threshold,
255 % ExceptionInfo *exception)
257 % A description of each parameter follows:
259 % o image: the image.
261 % o threshold: separate background from foreground.
263 % o exception: return any errors or warnings in this structure.
267 typedef struct _RadonInfo
292 static RadonInfo *DestroyRadonInfo(RadonInfo *radon_info)
294 assert(radon_info != (RadonInfo *) NULL);
295 switch (radon_info->type)
299 if (radon_info->mapped == MagickFalse)
300 radon_info->cells=(unsigned short *) RelinquishMagickMemory(
303 radon_info->cells=(unsigned short *) UnmapBlob(radon_info->cells,
304 (size_t) radon_info->length);
305 RelinquishMagickResource(MemoryResource,radon_info->length);
310 radon_info->cells=(unsigned short *) UnmapBlob(radon_info->cells,(size_t)
312 RelinquishMagickResource(MapResource,radon_info->length);
316 if (radon_info->file != -1)
317 (void) close(radon_info->file);
318 (void) RelinquishUniqueFileResource(radon_info->path);
319 RelinquishMagickResource(DiskResource,radon_info->length);
325 return((RadonInfo *) RelinquishMagickMemory(radon_info));
328 static MagickBooleanType ResetRadonCells(RadonInfo *radon_info)
342 if (radon_info->type != DiskCache)
344 (void) ResetMagickMemory(radon_info->cells,0,(size_t) radon_info->length);
348 (void) MagickSeek(radon_info->file,0,SEEK_SET);
349 for (y=0; y < (long) radon_info->height; y++)
351 for (x=0; x < (long) radon_info->width; x++)
353 count=write(radon_info->file,&value,sizeof(*radon_info->cells));
354 if (count != (ssize_t) sizeof(*radon_info->cells))
357 if (x < (long) radon_info->width)
360 return(y < (long) radon_info->height ? MagickFalse : MagickTrue);
363 static RadonInfo *AcquireRadonInfo(const Image *image,const unsigned long width,
364 const unsigned long height,ExceptionInfo *exception)
372 radon_info=(RadonInfo *) AcquireAlignedMemory(1,sizeof(*radon_info));
373 if (radon_info == (RadonInfo *) NULL)
374 return((RadonInfo *) NULL);
375 (void) ResetMagickMemory(radon_info,0,sizeof(*radon_info));
376 radon_info->width=width;
377 radon_info->height=height;
378 radon_info->length=(MagickSizeType) width*height*sizeof(*radon_info->cells);
379 radon_info->type=MemoryCache;
380 status=AcquireMagickResource(AreaResource,radon_info->length);
381 if ((status != MagickFalse) &&
382 (radon_info->length == (MagickSizeType) ((size_t) radon_info->length)))
384 status=AcquireMagickResource(MemoryResource,radon_info->length);
385 if (status != MagickFalse)
387 radon_info->mapped=MagickFalse;
388 radon_info->cells=(unsigned short *) AcquireMagickMemory((size_t)
390 if (radon_info->cells == (unsigned short *) NULL)
392 radon_info->mapped=MagickTrue;
393 radon_info->cells=(unsigned short *) MapBlob(-1,IOMode,0,(size_t)
396 if (radon_info->cells == (unsigned short *) NULL)
397 RelinquishMagickResource(MemoryResource,radon_info->length);
400 radon_info->file=(-1);
401 if (radon_info->cells == (unsigned short *) NULL)
403 status=AcquireMagickResource(DiskResource,radon_info->length);
404 if (status == MagickFalse)
406 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
407 "CacheResourcesExhausted","`%s'",image->filename);
408 return(DestroyRadonInfo(radon_info));
410 radon_info->type=DiskCache;
411 (void) AcquireMagickResource(MemoryResource,radon_info->length);
412 radon_info->file=AcquireUniqueFileResource(radon_info->path);
413 if (radon_info->file == -1)
414 return(DestroyRadonInfo(radon_info));
415 status=AcquireMagickResource(MapResource,radon_info->length);
416 if (status != MagickFalse)
418 status=ResetRadonCells(radon_info);
419 if (status != MagickFalse)
421 radon_info->cells=(unsigned short *) MapBlob(radon_info->file,
422 IOMode,0,(size_t) radon_info->length);
423 if (radon_info->cells != (unsigned short *) NULL)
424 radon_info->type=MapCache;
426 RelinquishMagickResource(MapResource,radon_info->length);
433 static inline size_t MagickMin(const size_t x,const size_t y)
440 static inline ssize_t ReadRadonCell(const RadonInfo *radon_info,
441 const off_t offset,const size_t length,unsigned char *buffer)
449 #if !defined(MAGICKCORE_HAVE_PPREAD)
450 #if defined(MAGICKCORE_OPENMP_SUPPORT)
451 #pragma omp critical (MagickCore_ReadRadonCell)
455 if (MagickSeek(radon_info->file,offset,SEEK_SET) >= 0)
459 for (i=0; i < (ssize_t) length; i+=count)
461 #if !defined(MAGICKCORE_HAVE_PPREAD)
462 count=read(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
465 count=pread(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
466 SSIZE_MAX),(off_t) (offset+i));
477 #if !defined(MAGICKCORE_HAVE_PPREAD)
484 static inline ssize_t WriteRadonCell(const RadonInfo *radon_info,
485 const off_t offset,const size_t length,const unsigned char *buffer)
493 #if !defined(MAGICKCORE_HAVE_PWRITE)
494 #if defined(MAGICKCORE_OPENMP_SUPPORT)
495 #pragma omp critical (MagickCore_WriteRadonCell)
498 if (MagickSeek(radon_info->file,offset,SEEK_SET) >= 0)
502 for (i=0; i < (ssize_t) length; i+=count)
504 #if !defined(MAGICKCORE_HAVE_PWRITE)
505 count=write(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
508 count=pwrite(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
509 SSIZE_MAX),(off_t) (offset+i));
520 #if !defined(MAGICKCORE_HAVE_PWRITE)
527 static inline unsigned short GetRadonCell(const RadonInfo *radon_info,
528 const long x,const long y)
536 i=(off_t) radon_info->height*x+y;
538 ((MagickSizeType) (i*sizeof(*radon_info->cells)) >= radon_info->length))
540 if (radon_info->type != DiskCache)
541 return(radon_info->cells[i]);
543 (void) ReadRadonCell(radon_info,i*sizeof(*radon_info->cells),
544 sizeof(*radon_info->cells),(unsigned char *) &value);
548 static inline MagickBooleanType SetRadonCell(const RadonInfo *radon_info,
549 const long x,const long y,const unsigned short value)
557 i=(off_t) radon_info->height*x+y;
559 ((MagickSizeType) (i*sizeof(*radon_info->cells)) >= radon_info->length))
561 if (radon_info->type != DiskCache)
563 radon_info->cells[i]=value;
566 count=WriteRadonCell(radon_info,i*sizeof(*radon_info->cells),
567 sizeof(*radon_info->cells),(const unsigned char *) &value);
568 if (count != (ssize_t) sizeof(*radon_info->cells))
573 static void RadonProjection(RadonInfo *source_cells,
574 RadonInfo *destination_cells,const long sign,unsigned long *projection)
591 for (step=1; step < p->width; step*=2)
593 for (x=0; x < (long) p->width; x+=2*step)
604 for (i=0; i < (long) step; i++)
606 for (y=0; y < (long) (p->height-i-1); y++)
608 cell=GetRadonCell(p,x+i,y);
609 (void) SetRadonCell(q,x+2*i,y,cell+GetRadonCell(p,x+i+step,y+i));
610 (void) SetRadonCell(q,x+2*i+1,y,cell+GetRadonCell(p,x+i+step,y+i+1));
612 for ( ; y < (long) (p->height-i); y++)
614 cell=GetRadonCell(p,x+i,y);
615 (void) SetRadonCell(q,x+2*i,y,cell+GetRadonCell(p,x+i+step,y+i));
616 (void) SetRadonCell(q,x+2*i+1,y,cell);
618 for ( ; y < (long) p->height; y++)
620 cell=GetRadonCell(p,x+i,y);
621 (void) SetRadonCell(q,x+2*i,y,cell);
622 (void) SetRadonCell(q,x+2*i+1,y,cell);
630 #if defined(MAGICKCORE_OPENMP_SUPPORT)
631 #pragma omp parallel for schedule(dynamic,4)
633 for (x=0; x < (long) p->width; x++)
642 for (y=0; y < (long) (p->height-1); y++)
647 delta=GetRadonCell(p,x,y)-(long) GetRadonCell(p,x,y+1);
650 projection[p->width+sign*x-1]=sum;
654 static MagickBooleanType RadonTransform(const Image *image,
655 const double threshold,unsigned long *projection,ExceptionInfo *exception)
683 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
684 source_cells=AcquireRadonInfo(image,width,image->rows,exception);
685 destination_cells=AcquireRadonInfo(image,width,image->rows,exception);
686 if ((source_cells == (RadonInfo *) NULL) ||
687 (destination_cells == (RadonInfo *) NULL))
689 if (destination_cells != (RadonInfo *) NULL)
690 destination_cells=DestroyRadonInfo(destination_cells);
691 if (source_cells != (RadonInfo *) NULL)
692 source_cells=DestroyRadonInfo(source_cells);
695 if (ResetRadonCells(source_cells) == MagickFalse)
697 destination_cells=DestroyRadonInfo(destination_cells);
698 source_cells=DestroyRadonInfo(source_cells);
701 for (i=0; i < 256; i++)
703 byte=(unsigned char) i;
704 for (count=0; byte != 0; byte>>=1)
706 bits[i]=(unsigned short) count;
709 image_view=AcquireCacheView(image);
710 #if defined(MAGICKCORE_OPENMP_SUPPORT)
711 #pragma omp parallel for schedule(dynamic,4) shared(status)
713 for (y=0; y < (long) image->rows; y++)
715 register const PixelPacket
726 if (status == MagickFalse)
728 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
729 if (p == (const PixelPacket *) NULL)
736 i=(long) (image->columns+7)/8;
737 for (x=0; x < (long) image->columns; x++)
740 if (((MagickRealType) p->red < threshold) ||
741 ((MagickRealType) p->green < threshold) ||
742 ((MagickRealType) p->blue < threshold))
747 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
756 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
759 RadonProjection(source_cells,destination_cells,-1,projection);
760 (void) ResetRadonCells(source_cells);
761 #if defined(MAGICKCORE_OPENMP_SUPPORT)
762 #pragma omp parallel for schedule(dynamic,4) shared(status)
764 for (y=0; y < (long) image->rows; y++)
766 register const PixelPacket
777 if (status == MagickFalse)
779 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
780 if (p == (const PixelPacket *) NULL)
788 for (x=0; x < (long) image->columns; x++)
791 if (((MagickRealType) p->red < threshold) ||
792 ((MagickRealType) p->green < threshold) ||
793 ((MagickRealType) p->blue < threshold))
798 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
807 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
810 RadonProjection(source_cells,destination_cells,1,projection);
811 image_view=DestroyCacheView(image_view);
812 destination_cells=DestroyRadonInfo(destination_cells);
813 source_cells=DestroyRadonInfo(source_cells);
817 static void GetImageBackgroundColor(Image *image,const long offset,
818 ExceptionInfo *exception)
833 Compute average background color.
837 GetMagickPixelPacket(image,&background);
839 image_view=AcquireCacheView(image);
840 for (y=0; y < (long) image->rows; y++)
842 register const PixelPacket
848 if ((y >= offset) && (y < ((long) image->rows-offset)))
850 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
851 if (p == (const PixelPacket *) NULL)
853 for (x=0; x < (long) image->columns; x++)
855 if ((x >= offset) && (x < ((long) image->columns-offset)))
857 background.red+=QuantumScale*GetRedPixelComponent(p);
858 background.green+=QuantumScale*GetGreenPixelComponent(p);
859 background.blue+=QuantumScale*GetBluePixelComponent(p);
860 background.opacity+=QuantumScale*GetOpacityPixelComponent(p);
865 image_view=DestroyCacheView(image_view);
866 image->background_color.red=ClampToQuantum((MagickRealType) QuantumRange*
867 background.red/count);
868 image->background_color.green=ClampToQuantum((MagickRealType) QuantumRange*
869 background.green/count);
870 image->background_color.blue=ClampToQuantum((MagickRealType) QuantumRange*
871 background.blue/count);
872 image->background_color.opacity=ClampToQuantum((MagickRealType) QuantumRange*
873 background.opacity/count);
876 MagickExport Image *DeskewImage(const Image *image,const double threshold,
877 ExceptionInfo *exception)
912 Compute deskew angle.
914 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
915 projection=(unsigned long *) AcquireQuantumMemory((size_t) (2*width-1),
916 sizeof(*projection));
917 if (projection == (unsigned long *) NULL)
918 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
919 status=RadonTransform(image,threshold,projection,exception);
920 if (status == MagickFalse)
922 projection=(unsigned long *) RelinquishMagickMemory(projection);
923 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
927 for (i=0; i < (long) (2*width-1); i++)
929 if (projection[i] > max_projection)
931 skew=i-(long) width+1;
932 max_projection=projection[i];
935 projection=(unsigned long *) RelinquishMagickMemory(projection);
939 clone_image=CloneImage(image,0,0,MagickTrue,exception);
940 if (clone_image == (Image *) NULL)
941 return((Image *) NULL);
942 (void) SetImageVirtualPixelMethod(clone_image,BackgroundVirtualPixelMethod);
943 degrees=RadiansToDegrees(-atan((double) skew/width/8));
944 if (image->debug != MagickFalse)
945 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
946 " Deskew angle: %g",degrees);
947 affine_matrix.sx=cos(DegreesToRadians(fmod((double) degrees,360.0)));
948 affine_matrix.rx=sin(DegreesToRadians(fmod((double) degrees,360.0)));
949 affine_matrix.ry=(-sin(DegreesToRadians(fmod((double) degrees,360.0))));
950 affine_matrix.sy=cos(DegreesToRadians(fmod((double) degrees,360.0)));
951 affine_matrix.tx=0.0;
952 affine_matrix.ty=0.0;
953 artifact=GetImageArtifact(image,"deskew:auto-crop");
954 if (artifact == (const char *) NULL)
956 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
957 clone_image=DestroyImage(clone_image);
958 return(deskew_image);
963 GetImageBackgroundColor(clone_image,StringToLong(artifact),exception);
964 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
965 clone_image=DestroyImage(clone_image);
966 if (deskew_image == (Image *) NULL)
967 return((Image *) NULL);
968 median_image=MedianFilterImage(deskew_image,0.0,exception);
969 if (median_image == (Image *) NULL)
971 deskew_image=DestroyImage(deskew_image);
972 return((Image *) NULL);
974 geometry=GetImageBoundingBox(median_image,exception);
975 median_image=DestroyImage(median_image);
976 if (image->debug != MagickFalse)
977 (void) LogMagickEvent(TransformEvent,GetMagickModule()," Deskew geometry: "
978 "%lux%lu%+ld%+ld",geometry.width,geometry.height,geometry.x,geometry.y);
979 crop_image=CropImage(deskew_image,&geometry,exception);
980 deskew_image=DestroyImage(deskew_image);
985 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
989 + I n t e g r a l R o t a t e I m a g e %
993 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
995 % IntegralRotateImage() rotates the image an integral of 90 degrees. It
996 % allocates the memory necessary for the new Image structure and returns a
997 % pointer to the rotated image.
999 % The format of the IntegralRotateImage method is:
1001 % Image *IntegralRotateImage(const Image *image,unsigned long rotations,
1002 % ExceptionInfo *exception)
1004 % A description of each parameter follows.
1006 % o image: the image.
1008 % o rotations: Specifies the number of 90 degree rotations.
1011 static Image *IntegralRotateImage(const Image *image,unsigned long rotations,
1012 ExceptionInfo *exception)
1014 #define RotateImageTag "Rotate/Image"
1034 Initialize rotated image attributes.
1036 assert(image != (Image *) NULL);
1040 return(CloneImage(image,0,0,MagickTrue,exception));
1041 if ((rotations == 1) || (rotations == 3))
1042 rotate_image=CloneImage(image,image->rows,image->columns,MagickTrue,
1045 rotate_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1047 if (rotate_image == (Image *) NULL)
1048 return((Image *) NULL);
1050 Integral rotate the image.
1054 image_view=AcquireCacheView(image);
1055 rotate_view=AcquireCacheView(rotate_image);
1077 GetPixelCacheTileSize(image,&tile_width,&tile_height);
1078 #if defined(MAGICKCORE_OPENMP_SUPPORT) && defined(MAGICKCORE_FUTURE)
1079 #pragma omp parallel for schedule(dynamic,4) shared(progress, status)
1081 for (tile_y=0; tile_y < (long) image->rows; tile_y+=tile_height)
1086 if (status == MagickFalse)
1088 for (tile_x=0; tile_x < (long) image->columns; tile_x+=tile_width)
1093 register const IndexPacket
1096 register const PixelPacket
1099 register IndexPacket
1100 *restrict rotate_indexes;
1105 register PixelPacket
1113 if ((tile_x+(long) tile_width) > (long) image->columns)
1114 width=(unsigned long) (tile_width-(tile_x+tile_width-
1117 if ((tile_y+(long) tile_height) > (long) image->rows)
1118 height=(unsigned long) (tile_height-(tile_y+tile_height-
1120 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1122 if (p == (const PixelPacket *) NULL)
1127 indexes=GetCacheViewVirtualIndexQueue(image_view);
1128 for (y=0; y < (long) width; y++)
1130 register const PixelPacket
1131 *restrict tile_pixels;
1136 q=QueueCacheViewAuthenticPixels(rotate_view,(long)
1137 rotate_image->columns-(tile_y+height),y+tile_x,height,
1139 if (q == (PixelPacket *) NULL)
1144 tile_pixels=p+(height-1)*width+y;
1145 for (x=0; x < (long) height; x++)
1147 *q++=(*tile_pixels);
1150 rotate_indexes=GetCacheViewAuthenticIndexQueue(rotate_view);
1151 if ((indexes != (IndexPacket *) NULL) &&
1152 (rotate_indexes != (IndexPacket *) NULL))
1154 register const IndexPacket
1155 *restrict tile_indexes;
1157 tile_indexes=indexes+(height-1)*width+y;
1158 for (x=0; x < (long) height; x++)
1160 *rotate_indexes++=(*tile_indexes);
1161 tile_indexes-=width;
1164 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1165 if (sync == MagickFalse)
1169 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1174 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
1176 if (proceed == MagickFalse)
1180 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1181 image->rows-1,image->rows);
1182 Swap(page.width,page.height);
1183 Swap(page.x,page.y);
1184 if (page.width != 0)
1185 page.x=(long) (page.width-rotate_image->columns-page.x);
1193 #if defined(MAGICKCORE_OPENMP_SUPPORT) && defined(MAGICKCORE_FUTURE)
1194 #pragma omp parallel for schedule(dynamic,4) shared(progress, status)
1196 for (y=0; y < (long) image->rows; y++)
1201 register const IndexPacket
1204 register const PixelPacket
1207 register IndexPacket
1208 *restrict rotate_indexes;
1213 register PixelPacket
1216 if (status == MagickFalse)
1218 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,
1220 q=QueueCacheViewAuthenticPixels(rotate_view,0,(long) (image->rows-
1221 y-1),image->columns,1,exception);
1222 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
1227 indexes=GetCacheViewVirtualIndexQueue(image_view);
1228 rotate_indexes=GetCacheViewAuthenticIndexQueue(rotate_view);
1230 for (x=0; x < (long) image->columns; x++)
1232 if ((indexes != (IndexPacket *) NULL) &&
1233 (rotate_indexes != (IndexPacket *) NULL))
1234 for (x=0; x < (long) image->columns; x++)
1235 rotate_indexes[image->columns-x-1]=indexes[x];
1236 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1237 if (sync == MagickFalse)
1239 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1244 proceed=SetImageProgress(image,RotateImageTag,progress++,
1246 if (proceed == MagickFalse)
1250 if (page.width != 0)
1251 page.x=(long) (page.width-rotate_image->columns-page.x);
1252 if (page.height != 0)
1253 page.y=(long) (page.height-rotate_image->rows-page.y);
1268 GetPixelCacheTileSize(image,&tile_width,&tile_height);
1269 #if defined(MAGICKCORE_OPENMP_SUPPORT) && defined(MAGICKCORE_FUTURE)
1270 #pragma omp parallel for schedule(dynamic,4) shared(progress, status)
1272 for (tile_y=0; tile_y < (long) image->rows; tile_y+=tile_height)
1277 if (status == MagickFalse)
1279 for (tile_x=0; tile_x < (long) image->columns; tile_x+=tile_width)
1284 register const IndexPacket
1287 register const PixelPacket
1290 register IndexPacket
1291 *restrict rotate_indexes;
1296 register PixelPacket
1304 if ((tile_x+(long) tile_width) > (long) image->columns)
1305 width=(unsigned long) (tile_width-(tile_x+tile_width-
1308 if ((tile_y+(long) tile_height) > (long) image->rows)
1309 height=(unsigned long) (tile_height-(tile_y+tile_height-
1311 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,
1313 if (p == (const PixelPacket *) NULL)
1318 indexes=GetCacheViewVirtualIndexQueue(image_view);
1319 for (y=0; y < (long) width; y++)
1321 register const PixelPacket
1322 *restrict tile_pixels;
1327 q=QueueCacheViewAuthenticPixels(rotate_view,tile_y,(long)
1328 y+rotate_image->rows-(tile_x+width),height,1,exception);
1329 if (q == (PixelPacket *) NULL)
1334 tile_pixels=p+(width-1)-y;
1335 for (x=0; x < (long) height; x++)
1337 *q++=(*tile_pixels);
1340 rotate_indexes=GetCacheViewAuthenticIndexQueue(rotate_view);
1341 if ((indexes != (IndexPacket *) NULL) &&
1342 (rotate_indexes != (IndexPacket *) NULL))
1344 register const IndexPacket
1345 *restrict tile_indexes;
1347 tile_indexes=indexes+(width-1)-y;
1348 for (x=0; x < (long) height; x++)
1350 *rotate_indexes++=(*tile_indexes);
1351 tile_indexes+=width;
1354 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1355 if (sync == MagickFalse)
1359 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1364 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
1366 if (proceed == MagickFalse)
1370 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1371 image->rows-1,image->rows);
1372 Swap(page.width,page.height);
1373 Swap(page.x,page.y);
1374 if (page.height != 0)
1375 page.y=(long) (page.height-rotate_image->rows-page.y);
1379 rotate_view=DestroyCacheView(rotate_view);
1380 image_view=DestroyCacheView(image_view);
1381 rotate_image->type=image->type;
1382 rotate_image->page=page;
1383 if (status == MagickFalse)
1384 rotate_image=DestroyImage(rotate_image);
1385 return(rotate_image);
1389 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1393 + X S h e a r I m a g e %
1397 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1399 % XShearImage() shears the image in the X direction with a shear angle of
1400 % 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1401 % negative angles shear clockwise. Angles are measured relative to a vertical
1402 % Y-axis. X shears will widen an image creating 'empty' triangles on the left
1403 % and right sides of the source image.
1405 % The format of the XShearImage method is:
1407 % MagickBooleanType XShearImage(Image *image,const MagickRealType degrees,
1408 % const unsigned long width,const unsigned long height,
1409 % const long x_offset,const long y_offset,ExceptionInfo *exception)
1411 % A description of each parameter follows.
1413 % o image: the image.
1415 % o degrees: A MagickRealType representing the shearing angle along the X
1418 % o width, height, x_offset, y_offset: Defines a region of the image
1421 % o exception: return any errors or warnings in this structure.
1424 static MagickBooleanType XShearImage(Image *image,const MagickRealType degrees,
1425 const unsigned long width,const unsigned long height,const long x_offset,
1426 const long y_offset,ExceptionInfo *exception)
1428 #define XShearImageTag "XShear/Image"
1449 assert(image != (Image *) NULL);
1450 assert(image->signature == MagickSignature);
1451 if (image->debug != MagickFalse)
1452 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1453 GetMagickPixelPacket(image,&background);
1454 SetMagickPixelPacket(image,&image->background_color,(IndexPacket *) NULL,
1456 if (image->colorspace == CMYKColorspace)
1457 ConvertRGBToCMYK(&background);
1463 image_view=AcquireCacheView(image);
1464 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1465 #pragma omp parallel for schedule(dynamic,4) shared(progress, status)
1467 for (y=0; y < (long) height; y++)
1484 register IndexPacket
1486 *restrict shear_indexes;
1488 register PixelPacket
1495 if (status == MagickFalse)
1497 p=GetCacheViewAuthenticPixels(image_view,0,y_offset+y,image->columns,1,
1499 if (p == (PixelPacket *) NULL)
1504 indexes=GetCacheViewAuthenticIndexQueue(image_view);
1507 displacement=degrees*(MagickRealType) (y-height/2.0);
1508 if (displacement == 0.0)
1510 if (displacement > 0.0)
1514 displacement*=(-1.0);
1517 step=(long) floor((double) displacement);
1518 area=(MagickRealType) (displacement-step);
1521 GetMagickPixelPacket(image,&source);
1522 GetMagickPixelPacket(image,&destination);
1528 Transfer pixels left-to-right.
1530 if (step > x_offset)
1533 shear_indexes=indexes-step;
1534 for (i=0; i < (long) width; i++)
1536 if ((x_offset+i) < step)
1538 SetMagickPixelPacket(image,++p,++indexes,&pixel);
1543 SetMagickPixelPacket(image,p,indexes,&source);
1544 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1545 &source,(MagickRealType) p->opacity,area,&destination);
1546 SetPixelPacket(image,&destination,q++,shear_indexes++);
1547 SetMagickPixelPacket(image,p++,indexes++,&pixel);
1549 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1550 &background,(MagickRealType) background.opacity,area,&destination);
1551 SetPixelPacket(image,&destination,q++,shear_indexes++);
1552 for (i=0; i < (step-1); i++)
1553 SetPixelPacket(image,&background,q++,shear_indexes++);
1559 Transfer pixels right-to-left.
1564 shear_indexes=indexes+step;
1565 for (i=0; i < (long) width; i++)
1571 if ((unsigned long) (x_offset+width+step-i) >= image->columns)
1573 SetMagickPixelPacket(image,p,indexes,&source);
1574 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1575 &source,(MagickRealType) p->opacity,area,&destination);
1576 SetPixelPacket(image,&destination,q,shear_indexes);
1577 SetMagickPixelPacket(image,p,indexes,&pixel);
1579 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1580 &background,(MagickRealType) background.opacity,area,&destination);
1581 SetPixelPacket(image,&destination,--q,--shear_indexes);
1582 for (i=0; i < (step-1); i++)
1583 SetPixelPacket(image,&background,--q,--shear_indexes);
1587 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1589 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1594 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1595 #pragma omp critical (MagickCore_XShearImage)
1597 proceed=SetImageProgress(image,XShearImageTag,progress++,height);
1598 if (proceed == MagickFalse)
1602 image_view=DestroyCacheView(image_view);
1607 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1611 + Y S h e a r I m a g e %
1615 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1617 % YShearImage shears the image in the Y direction with a shear angle of
1618 % 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1619 % negative angles shear clockwise. Angles are measured relative to a
1620 % horizontal X-axis. Y shears will increase the height of an image creating
1621 % 'empty' triangles on the top and bottom of the source image.
1623 % The format of the YShearImage method is:
1625 % MagickBooleanType YShearImage(Image *image,const MagickRealType degrees,
1626 % const unsigned long width,const unsigned long height,
1627 % const long x_offset,const long y_offset,ExceptionInfo *exception)
1629 % A description of each parameter follows.
1631 % o image: the image.
1633 % o degrees: A MagickRealType representing the shearing angle along the Y
1636 % o width, height, x_offset, y_offset: Defines a region of the image
1639 % o exception: return any errors or warnings in this structure.
1642 static MagickBooleanType YShearImage(Image *image,const MagickRealType degrees,
1643 const unsigned long width,const unsigned long height,const long x_offset,
1644 const long y_offset,ExceptionInfo *exception)
1646 #define YShearImageTag "YShear/Image"
1667 assert(image != (Image *) NULL);
1668 assert(image->signature == MagickSignature);
1669 if (image->debug != MagickFalse)
1670 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1671 GetMagickPixelPacket(image,&background);
1672 SetMagickPixelPacket(image,&image->background_color,(IndexPacket *) NULL,
1674 if (image->colorspace == CMYKColorspace)
1675 ConvertRGBToCMYK(&background);
1681 image_view=AcquireCacheView(image);
1682 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1683 #pragma omp parallel for schedule(dynamic,4) shared(progress, status)
1685 for (x=0; x < (long) width; x++)
1699 register IndexPacket
1701 *restrict shear_indexes;
1706 register PixelPacket
1713 if (status == MagickFalse)
1715 p=GetCacheViewAuthenticPixels(image_view,x_offset+x,0,1,image->rows,
1717 if (p == (PixelPacket *) NULL)
1722 indexes=GetCacheViewAuthenticIndexQueue(image_view);
1725 displacement=degrees*(MagickRealType) (x-width/2.0);
1726 if (displacement == 0.0)
1728 if (displacement > 0.0)
1732 displacement*=(-1.0);
1735 step=(long) floor((double) displacement);
1736 area=(MagickRealType) (displacement-step);
1739 GetMagickPixelPacket(image,&source);
1740 GetMagickPixelPacket(image,&destination);
1746 Transfer pixels top-to-bottom.
1748 if (step > y_offset)
1751 shear_indexes=indexes-step;
1752 for (i=0; i < (long) height; i++)
1754 if ((y_offset+i) < step)
1756 SetMagickPixelPacket(image,++p,++indexes,&pixel);
1761 SetMagickPixelPacket(image,p,indexes,&source);
1762 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1763 &source,(MagickRealType) p->opacity,area,&destination);
1764 SetPixelPacket(image,&destination,q++,shear_indexes++);
1765 SetMagickPixelPacket(image,p++,indexes++,&pixel);
1767 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1768 &background,(MagickRealType) background.opacity,area,&destination);
1769 SetPixelPacket(image,&destination,q++,shear_indexes++);
1770 for (i=0; i < (step-1); i++)
1771 SetPixelPacket(image,&background,q++,shear_indexes++);
1777 Transfer pixels bottom-to-top.
1782 shear_indexes=indexes+step;
1783 for (i=0; i < (long) height; i++)
1789 if ((unsigned long) (y_offset+height+step-i) >= image->rows)
1791 SetMagickPixelPacket(image,p,indexes,&source);
1792 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1793 &source,(MagickRealType) p->opacity,area,&destination);
1794 SetPixelPacket(image,&destination,q,shear_indexes);
1795 SetMagickPixelPacket(image,p,indexes,&pixel);
1797 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1798 &background,(MagickRealType) background.opacity,area,&destination);
1799 SetPixelPacket(image,&destination,--q,--shear_indexes);
1800 for (i=0; i < (step-1); i++)
1801 SetPixelPacket(image,&background,--q,--shear_indexes);
1805 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1807 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1812 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1813 #pragma omp critical (MagickCore_YShearImage)
1815 proceed=SetImageProgress(image,YShearImageTag,progress++,image->rows);
1816 if (proceed == MagickFalse)
1820 image_view=DestroyCacheView(image_view);
1825 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1829 % R o t a t e I m a g e %
1833 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1835 % RotateImage() creates a new image that is a rotated copy of an existing
1836 % one. Positive angles rotate counter-clockwise (right-hand rule), while
1837 % negative angles rotate clockwise. Rotated images are usually larger than
1838 % the originals and have 'empty' triangular corners. X axis. Empty
1839 % triangles left over from shearing the image are filled with the background
1840 % color defined by member 'background_color' of the image. RotateImage
1841 % allocates the memory necessary for the new Image structure and returns a
1842 % pointer to the new image.
1844 % RotateImage() is based on the paper "A Fast Algorithm for General
1845 % Raster Rotatation" by Alan W. Paeth. RotateImage is adapted from a similar
1846 % method based on the Paeth paper written by Michael Halle of the Spatial
1847 % Imaging Group, MIT Media Lab.
1849 % The format of the RotateImage method is:
1851 % Image *RotateImage(const Image *image,const double degrees,
1852 % ExceptionInfo *exception)
1854 % A description of each parameter follows.
1856 % o image: the image.
1858 % o degrees: Specifies the number of degrees to rotate the image.
1860 % o exception: return any errors or warnings in this structure.
1863 MagickExport Image *RotateImage(const Image *image,const double degrees,
1864 ExceptionInfo *exception)
1893 Adjust rotation angle.
1895 assert(image != (Image *) NULL);
1896 assert(image->signature == MagickSignature);
1897 if (image->debug != MagickFalse)
1898 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1899 assert(exception != (ExceptionInfo *) NULL);
1900 assert(exception->signature == MagickSignature);
1902 while (angle < -45.0)
1904 for (rotations=0; angle > 45.0; rotations++)
1908 Calculate shear equations.
1910 integral_image=IntegralRotateImage(image,rotations,exception);
1911 if (integral_image == (Image *) NULL)
1912 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1913 shear.x=(-tan((double) DegreesToRadians(angle)/2.0));
1914 shear.y=sin((double) DegreesToRadians(angle));
1915 if ((shear.x == 0.0) && (shear.y == 0.0))
1916 return(integral_image);
1917 if (SetImageStorageClass(integral_image,DirectClass) == MagickFalse)
1919 InheritException(exception,&integral_image->exception);
1920 integral_image=DestroyImage(integral_image);
1921 return(integral_image);
1923 if (integral_image->matte == MagickFalse)
1924 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel);
1928 width=image->columns;
1930 if ((rotations == 1) || (rotations == 3))
1933 height=image->columns;
1935 y_width=width+(long) floor(fabs(shear.x)*height+0.5);
1936 x_offset=(long) ceil(width+((fabs(shear.y)*height)-width)/2.0-0.5);
1937 y_offset=(long) ceil(height+((fabs(shear.y)*y_width)-height)/2.0-0.5);
1939 Surround image with a border.
1941 integral_image->border_color=integral_image->background_color;
1942 integral_image->compose=CopyCompositeOp;
1943 border_info.width=(unsigned long) x_offset;
1944 border_info.height=(unsigned long) y_offset;
1945 rotate_image=BorderImage(integral_image,&border_info,exception);
1946 integral_image=DestroyImage(integral_image);
1947 if (rotate_image == (Image *) NULL)
1948 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1952 status=XShearImage(rotate_image,shear.x,width,height,x_offset,((long)
1953 rotate_image->rows-height)/2,exception);
1954 if (status == MagickFalse)
1956 rotate_image=DestroyImage(rotate_image);
1957 return((Image *) NULL);
1959 status=YShearImage(rotate_image,shear.y,y_width,height,((long)
1960 rotate_image->columns-y_width)/2,y_offset,exception);
1961 if (status == MagickFalse)
1963 rotate_image=DestroyImage(rotate_image);
1964 return((Image *) NULL);
1966 status=XShearImage(rotate_image,shear.x,y_width,rotate_image->rows,((long)
1967 rotate_image->columns-y_width)/2,0,exception);
1968 if (status == MagickFalse)
1970 rotate_image=DestroyImage(rotate_image);
1971 return((Image *) NULL);
1973 status=CropToFitImage(&rotate_image,shear.x,shear.y,(MagickRealType) width,
1974 (MagickRealType) height,MagickTrue,exception);
1975 if (status == MagickFalse)
1977 rotate_image=DestroyImage(rotate_image);
1978 return((Image *) NULL);
1980 rotate_image->compose=image->compose;
1981 rotate_image->page.width=0;
1982 rotate_image->page.height=0;
1983 return(rotate_image);
1987 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1991 % S h e a r I m a g e %
1995 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1997 % ShearImage() creates a new image that is a shear_image copy of an existing
1998 % one. Shearing slides one edge of an image along the X or Y axis, creating
1999 % a parallelogram. An X direction shear slides an edge along the X axis,
2000 % while a Y direction shear slides an edge along the Y axis. The amount of
2001 % the shear is controlled by a shear angle. For X direction shears, x_shear
2002 % is measured relative to the Y axis, and similarly, for Y direction shears
2003 % y_shear is measured relative to the X axis. Empty triangles left over from
2004 % shearing the image are filled with the background color defined by member
2005 % 'background_color' of the image.. ShearImage() allocates the memory
2006 % necessary for the new Image structure and returns a pointer to the new image.
2008 % ShearImage() is based on the paper "A Fast Algorithm for General Raster
2009 % Rotatation" by Alan W. Paeth.
2011 % The format of the ShearImage method is:
2013 % Image *ShearImage(const Image *image,const double x_shear,
2014 % const double y_shear,ExceptionInfo *exception)
2016 % A description of each parameter follows.
2018 % o image: the image.
2020 % o x_shear, y_shear: Specifies the number of degrees to shear the image.
2022 % o exception: return any errors or warnings in this structure.
2025 MagickExport Image *ShearImage(const Image *image,const double x_shear,
2026 const double y_shear,ExceptionInfo *exception)
2048 assert(image != (Image *) NULL);
2049 assert(image->signature == MagickSignature);
2050 if (image->debug != MagickFalse)
2051 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2052 assert(exception != (ExceptionInfo *) NULL);
2053 assert(exception->signature == MagickSignature);
2054 if ((x_shear != 0.0) && (fmod(x_shear,90.0) == 0.0))
2055 ThrowImageException(ImageError,"AngleIsDiscontinuous");
2056 if ((y_shear != 0.0) && (fmod(y_shear,90.0) == 0.0))
2057 ThrowImageException(ImageError,"AngleIsDiscontinuous");
2059 Initialize shear angle.
2061 integral_image=CloneImage(image,0,0,MagickTrue,exception);
2062 if (integral_image == (Image *) NULL)
2063 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2064 shear.x=(-tan(DegreesToRadians(fmod(x_shear,360.0))));
2065 shear.y=tan(DegreesToRadians(fmod(y_shear,360.0)));
2066 if ((shear.x == 0.0) && (shear.y == 0.0))
2067 return(integral_image);
2068 if (SetImageStorageClass(integral_image,DirectClass) == MagickFalse)
2070 InheritException(exception,&integral_image->exception);
2071 integral_image=DestroyImage(integral_image);
2072 return(integral_image);
2074 if (integral_image->matte == MagickFalse)
2075 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel);
2079 y_width=image->columns+(long) floor(fabs(shear.x)*image->rows+0.5);
2080 x_offset=(long) ceil(image->columns+((fabs(shear.x)*image->rows)-
2081 image->columns)/2.0-0.5);
2082 y_offset=(long) ceil(image->rows+((fabs(shear.y)*y_width)-image->rows)/2.0-
2085 Surround image with border.
2087 integral_image->border_color=integral_image->background_color;
2088 integral_image->compose=CopyCompositeOp;
2089 border_info.width=(unsigned long) x_offset;
2090 border_info.height=(unsigned long) y_offset;
2091 shear_image=BorderImage(integral_image,&border_info,exception);
2092 integral_image=DestroyImage(integral_image);
2093 if (shear_image == (Image *) NULL)
2094 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2098 if (shear_image->matte == MagickFalse)
2099 (void) SetImageAlphaChannel(shear_image,OpaqueAlphaChannel);
2100 status=XShearImage(shear_image,shear.x,image->columns,image->rows,x_offset,
2101 ((long) shear_image->rows-image->rows)/2,exception);
2102 if (status == MagickFalse)
2104 shear_image=DestroyImage(shear_image);
2105 return((Image *) NULL);
2107 status=YShearImage(shear_image,shear.y,y_width,image->rows,((long)
2108 shear_image->columns-y_width)/2,y_offset,exception);
2109 if (status == MagickFalse)
2111 shear_image=DestroyImage(shear_image);
2112 return((Image *) NULL);
2114 status=CropToFitImage(&shear_image,shear.x,shear.y,(MagickRealType)
2115 image->columns,(MagickRealType) image->rows,MagickFalse,exception);
2116 if (status == MagickFalse)
2118 shear_image=DestroyImage(shear_image);
2119 return((Image *) NULL);
2121 shear_image->compose=image->compose;
2122 shear_image->page.width=0;
2123 shear_image->page.height=0;
2124 return(shear_image);