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-2009 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/blob-private.h"
51 #include "magick/cache-private.h"
52 #include "magick/color-private.h"
53 #include "magick/colorspace-private.h"
54 #include "magick/composite.h"
55 #include "magick/composite-private.h"
56 #include "magick/decorate.h"
57 #include "magick/distort.h"
58 #include "magick/draw.h"
59 #include "magick/exception.h"
60 #include "magick/exception-private.h"
61 #include "magick/gem.h"
62 #include "magick/geometry.h"
63 #include "magick/image.h"
64 #include "magick/image-private.h"
65 #include "magick/memory_.h"
66 #include "magick/list.h"
67 #include "magick/monitor.h"
68 #include "magick/monitor-private.h"
69 #include "magick/pixel-private.h"
70 #include "magick/quantum.h"
71 #include "magick/resource_.h"
72 #include "magick/shear.h"
73 #include "magick/statistic.h"
74 #include "magick/threshold.h"
75 #include "magick/transform.h"
78 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82 % A f f i n e T r a n s f o r m I m a g e %
86 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
88 % AffineTransformImage() transforms an image as dictated by the affine matrix.
89 % It allocates the memory necessary for the new Image structure and returns
90 % a pointer to the new image.
92 % The format of the AffineTransformImage method is:
94 % Image *AffineTransformImage(const Image *image,
95 % AffineMatrix *affine_matrix,ExceptionInfo *exception)
97 % A description of each parameter follows:
101 % o affine_matrix: the affine matrix.
103 % o exception: return any errors or warnings in this structure.
106 MagickExport Image *AffineTransformImage(const Image *image,
107 const AffineMatrix *affine_matrix,ExceptionInfo *exception)
116 Affine transform image.
118 assert(image->signature == MagickSignature);
119 if (image->debug != MagickFalse)
120 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
121 assert(affine_matrix != (AffineMatrix *) NULL);
122 assert(exception != (ExceptionInfo *) NULL);
123 assert(exception->signature == MagickSignature);
124 distort[0]=affine_matrix->sx;
125 distort[1]=affine_matrix->rx;
126 distort[2]=affine_matrix->ry;
127 distort[3]=affine_matrix->sy;
128 distort[4]=affine_matrix->tx;
129 distort[5]=affine_matrix->ty;
130 deskew_image=DistortImage(image,AffineProjectionDistortion,6,distort,
131 MagickTrue,exception);
132 return(deskew_image);
136 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
140 + C r o p T o F i t I m a g e %
144 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
146 % CropToFitImage() crops the sheared image as determined by the bounding box
147 % as defined by width and height and shearing angles.
149 % The format of the CropToFitImage method is:
151 % Image *CropToFitImage(Image **image,const MagickRealType x_shear,
152 % const MagickRealType x_shear,const MagickRealType width,
153 % const MagickRealType height,const MagickBooleanType rotate,
154 % ExceptionInfo *exception)
156 % A description of each parameter follows.
158 % o image: the image.
160 % o x_shear, y_shear, width, height: Defines a region of the image to crop.
162 % o exception: return any errors or warnings in this structure.
165 static inline void CropToFitImage(Image **image,const MagickRealType x_shear,
166 const MagickRealType y_shear,const MagickRealType width,
167 const MagickRealType height,const MagickBooleanType rotate,
168 ExceptionInfo *exception)
186 Calculate the rotated image size.
188 extent[0].x=(double) (-width/2.0);
189 extent[0].y=(double) (-height/2.0);
190 extent[1].x=(double) width/2.0;
191 extent[1].y=(double) (-height/2.0);
192 extent[2].x=(double) (-width/2.0);
193 extent[2].y=(double) height/2.0;
194 extent[3].x=(double) width/2.0;
195 extent[3].y=(double) height/2.0;
196 for (i=0; i < 4; i++)
198 extent[i].x+=x_shear*extent[i].y;
199 extent[i].y+=y_shear*extent[i].x;
200 if (rotate != MagickFalse)
201 extent[i].x+=x_shear*extent[i].y;
202 extent[i].x+=(double) (*image)->columns/2.0;
203 extent[i].y+=(double) (*image)->rows/2.0;
207 for (i=1; i < 4; i++)
209 if (min.x > extent[i].x)
211 if (min.y > extent[i].y)
213 if (max.x < extent[i].x)
215 if (max.y < extent[i].y)
218 geometry.x=(long) (min.x+0.5);
219 geometry.y=(long) (min.y+0.5);
220 geometry.width=(unsigned long) ((long) (max.x+0.5)-(long) (min.x+0.5));
221 geometry.height=(unsigned long) ((long) (max.y+0.5)-(long) (min.y+0.5));
223 (void) ParseAbsoluteGeometry("0x0+0+0",&(*image)->page);
224 crop_image=CropImage(*image,&geometry,exception);
226 if (crop_image != (Image *) NULL)
228 crop_image->page=page;
229 *image=DestroyImage(*image);
235 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
239 % D e s k e w I m a g e %
243 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
245 % DeskewImage() removes skew from the image. Skew is an artifact that
246 % occurs in scanned images because of the camera being misaligned,
247 % imperfections in the scanning or surface, or simply because the paper was
248 % not placed completely flat when scanned.
250 % The format of the DeskewImage method is:
252 % Image *DeskewImage(const Image *image,const double threshold,
253 % ExceptionInfo *exception)
255 % A description of each parameter follows:
257 % o image: the image.
259 % o threshold: separate background from foreground.
261 % o exception: return any errors or warnings in this structure.
265 typedef struct _RadonInfo
290 static RadonInfo *DestroyRadonInfo(RadonInfo *radon_info)
292 assert(radon_info != (RadonInfo *) NULL);
293 switch (radon_info->type)
297 if (radon_info->mapped == MagickFalse)
298 radon_info->cells=(unsigned short *) RelinquishMagickMemory(
301 radon_info->cells=(unsigned short *) UnmapBlob(radon_info->cells,
302 (size_t) radon_info->length);
303 RelinquishMagickResource(MemoryResource,radon_info->length);
308 radon_info->cells=(unsigned short *) UnmapBlob(radon_info->cells,(size_t)
310 RelinquishMagickResource(MapResource,radon_info->length);
314 if (radon_info->file != -1)
315 (void) close(radon_info->file);
316 (void) RelinquishUniqueFileResource(radon_info->path);
317 RelinquishMagickResource(DiskResource,radon_info->length);
323 return((RadonInfo *) RelinquishMagickMemory(radon_info));
326 static MagickBooleanType ResetRadonCells(RadonInfo *radon_info)
340 if (radon_info->type != DiskCache)
342 (void) ResetMagickMemory(radon_info->cells,0,(size_t) radon_info->length);
346 (void) MagickSeek(radon_info->file,0,SEEK_SET);
347 for (y=0; y < (long) radon_info->height; y++)
349 for (x=0; x < (long) radon_info->width; x++)
351 count=write(radon_info->file,&value,sizeof(*radon_info->cells));
352 if (count != (ssize_t) sizeof(*radon_info->cells))
355 if (x < (long) radon_info->width)
358 return(y < (long) radon_info->height ? MagickFalse : MagickTrue);
361 static RadonInfo *AcquireRadonInfo(const Image *image,const unsigned long width,
362 const unsigned long height,ExceptionInfo *exception)
370 radon_info=(RadonInfo *) AcquireMagickMemory(sizeof(*radon_info));
371 if (radon_info == (RadonInfo *) NULL)
372 return((RadonInfo *) NULL);
373 (void) ResetMagickMemory(radon_info,0,sizeof(*radon_info));
374 radon_info->width=width;
375 radon_info->height=height;
376 radon_info->length=(MagickSizeType) width*height*sizeof(*radon_info->cells);
377 radon_info->type=MemoryCache;
378 status=AcquireMagickResource(AreaResource,radon_info->length);
379 if ((status != MagickFalse) &&
380 (radon_info->length == (MagickSizeType) ((size_t) radon_info->length)))
382 status=AcquireMagickResource(MemoryResource,radon_info->length);
383 if (status != MagickFalse)
385 radon_info->mapped=MagickFalse;
386 radon_info->cells=(unsigned short *) AcquireMagickMemory((size_t)
388 if (radon_info->cells == (unsigned short *) NULL)
390 radon_info->mapped=MagickTrue;
391 radon_info->cells=(unsigned short *) MapBlob(-1,IOMode,0,(size_t)
394 if (radon_info->cells == (unsigned short *) NULL)
395 RelinquishMagickResource(MemoryResource,radon_info->length);
398 radon_info->file=(-1);
399 if (radon_info->cells == (unsigned short *) NULL)
401 status=AcquireMagickResource(DiskResource,radon_info->length);
402 if (status == MagickFalse)
404 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
405 "CacheResourcesExhausted","`%s'",image->filename);
406 return(DestroyRadonInfo(radon_info));
408 radon_info->type=DiskCache;
409 (void) AcquireMagickResource(MemoryResource,radon_info->length);
410 radon_info->file=AcquireUniqueFileResource(radon_info->path);
411 if (radon_info->file == -1)
412 return(DestroyRadonInfo(radon_info));
413 status=AcquireMagickResource(MapResource,radon_info->length);
414 if (status != MagickFalse)
416 status=ResetRadonCells(radon_info);
417 if (status != MagickFalse)
419 radon_info->cells=(unsigned short *) MapBlob(radon_info->file,
420 IOMode,0,(size_t) radon_info->length);
421 if (radon_info->cells != (unsigned short *) NULL)
422 radon_info->type=MapCache;
424 RelinquishMagickResource(MapResource,radon_info->length);
431 static inline size_t MagickMin(const size_t x,const size_t y)
438 static inline ssize_t ReadRadonCell(const RadonInfo *radon_info,
439 const off_t offset,const size_t length,unsigned char *buffer)
447 #if !defined(MAGICKCORE_HAVE_PPREAD)
448 #if defined(MAGICKCORE_OPENMP_SUPPORT)
449 #pragma omp critical (MagickCore_ReadRadonCell)
453 if (MagickSeek(radon_info->file,offset,SEEK_SET) >= 0)
457 for (i=0; i < (ssize_t) length; i+=count)
459 #if !defined(MAGICKCORE_HAVE_PPREAD)
460 count=read(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
463 count=pread(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
464 SSIZE_MAX),(off_t) (offset+i));
475 #if !defined(MAGICKCORE_HAVE_PPREAD)
482 static inline ssize_t WriteRadonCell(const RadonInfo *radon_info,
483 const off_t offset,const size_t length,const unsigned char *buffer)
491 #if !defined(MAGICKCORE_HAVE_PWRITE)
492 #if defined(MAGICKCORE_OPENMP_SUPPORT)
493 #pragma omp critical (MagickCore_WriteRadonCell)
496 if (MagickSeek(radon_info->file,offset,SEEK_SET) >= 0)
500 for (i=0; i < (ssize_t) length; i+=count)
502 #if !defined(MAGICKCORE_HAVE_PWRITE)
503 count=write(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
506 count=pwrite(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
507 SSIZE_MAX),(off_t) (offset+i));
518 #if !defined(MAGICKCORE_HAVE_PWRITE)
525 static inline unsigned short GetRadonCell(const RadonInfo *radon_info,
526 const long x,const long y)
534 i=(off_t) radon_info->height*x+y;
536 ((MagickSizeType) (i*sizeof(*radon_info->cells)) >= radon_info->length))
538 if (radon_info->type != DiskCache)
539 return(radon_info->cells[i]);
541 (void) ReadRadonCell(radon_info,i*sizeof(*radon_info->cells),
542 sizeof(*radon_info->cells),(unsigned char *) &value);
546 static inline MagickBooleanType SetRadonCell(const RadonInfo *radon_info,
547 const long x,const long y,const unsigned short value)
555 i=(off_t) radon_info->height*x+y;
557 ((MagickSizeType) (i*sizeof(*radon_info->cells)) >= radon_info->length))
559 if (radon_info->type != DiskCache)
561 radon_info->cells[i]=value;
564 count=WriteRadonCell(radon_info,i*sizeof(*radon_info->cells),
565 sizeof(*radon_info->cells),(unsigned char *) &value);
566 if (count != (ssize_t) sizeof(*radon_info->cells))
571 static void RadonProjection(RadonInfo *source_cells,
572 RadonInfo *destination_cells,const long sign,unsigned long *projection)
589 for (step=1; step < p->width; step*=2)
591 for (x=0; x < (long) p->width; x+=2*step)
602 for (i=0; i < (long) step; i++)
604 for (y=0; y < (long) (p->height-i-1); y++)
606 cell=GetRadonCell(p,x+i,y);
607 (void) SetRadonCell(q,x+2*i,y,cell+GetRadonCell(p,x+i+step,y+i));
608 (void) SetRadonCell(q,x+2*i+1,y,cell+GetRadonCell(p,x+i+step,y+i+1));
610 for ( ; y < (long) (p->height-i); y++)
612 cell=GetRadonCell(p,x+i,y);
613 (void) SetRadonCell(q,x+2*i,y,cell+GetRadonCell(p,x+i+step,y+i));
614 (void) SetRadonCell(q,x+2*i+1,y,cell);
616 for ( ; y < (long) p->height; y++)
618 cell=GetRadonCell(p,x+i,y);
619 (void) SetRadonCell(q,x+2*i,y,cell);
620 (void) SetRadonCell(q,x+2*i+1,y,cell);
628 #if defined(MAGICKCORE_OPENMP_SUPPORT)
629 #pragma omp parallel for
631 for (x=0; x < (long) p->width; x++)
640 for (y=0; y < (long) (p->height-1); y++)
645 delta=GetRadonCell(p,x,y)-(long) GetRadonCell(p,x,y+1);
648 projection[p->width+sign*x-1]=sum;
652 static MagickBooleanType RadonTransform(const Image *image,
653 const double threshold,unsigned long *projection,ExceptionInfo *exception)
681 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
682 source_cells=AcquireRadonInfo(image,width,image->rows,exception);
683 destination_cells=AcquireRadonInfo(image,width,image->rows,exception);
684 if ((source_cells == (RadonInfo *) NULL) ||
685 (destination_cells == (RadonInfo *) NULL))
687 if (destination_cells != (RadonInfo *) NULL)
688 destination_cells=DestroyRadonInfo(destination_cells);
689 if (source_cells != (RadonInfo *) NULL)
690 source_cells=DestroyRadonInfo(source_cells);
693 if (ResetRadonCells(source_cells) == MagickFalse)
695 destination_cells=DestroyRadonInfo(destination_cells);
696 source_cells=DestroyRadonInfo(source_cells);
699 for (i=0; i < 256; i++)
701 byte=(unsigned char) i;
702 for (count=0; byte != 0; byte>>=1)
704 bits[i]=(unsigned short) count;
707 image_view=AcquireCacheView(image);
708 #if defined(MAGICKCORE_OPENMP_SUPPORT)
709 #pragma omp parallel for schedule(static,1) shared(status)
711 for (y=0; y < (long) image->rows; y++)
713 register const PixelPacket
724 if (status == MagickFalse)
726 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
727 if (p == (const PixelPacket *) NULL)
734 i=(long) (image->columns+7)/8;
735 for (x=0; x < (long) image->columns; x++)
738 if (((MagickRealType) p->red < threshold) ||
739 ((MagickRealType) p->green < threshold) ||
740 ((MagickRealType) p->blue < threshold))
745 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
754 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
757 RadonProjection(source_cells,destination_cells,-1,projection);
758 (void) ResetRadonCells(source_cells);
759 #if defined(MAGICKCORE_OPENMP_SUPPORT)
760 #pragma omp parallel for schedule(static,1) shared(status)
762 for (y=0; y < (long) image->rows; y++)
764 register const PixelPacket
775 if (status == MagickFalse)
777 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
778 if (p == (const PixelPacket *) NULL)
786 for (x=0; x < (long) image->columns; x++)
789 if (((MagickRealType) p->red < threshold) ||
790 ((MagickRealType) p->green < threshold) ||
791 ((MagickRealType) p->blue < threshold))
796 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
805 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
808 RadonProjection(source_cells,destination_cells,1,projection);
809 image_view=DestroyCacheView(image_view);
810 destination_cells=DestroyRadonInfo(destination_cells);
811 source_cells=DestroyRadonInfo(source_cells);
815 static void GetImageBackgroundColor(Image *image,const long offset,
816 ExceptionInfo *exception)
831 Compute average background color.
835 GetMagickPixelPacket(image,&background);
837 image_view=AcquireCacheView(image);
838 for (y=0; y < (long) image->rows; y++)
840 register const PixelPacket
846 if ((y >= offset) && (y < ((long) image->rows-offset)))
848 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
849 if (p == (const PixelPacket *) NULL)
851 for (x=0; x < (long) image->columns; x++)
853 if ((x >= offset) && (x < ((long) image->columns-offset)))
855 background.red+=QuantumScale*p->red;
856 background.green+=QuantumScale*p->green;
857 background.blue+=QuantumScale*p->blue;
858 background.opacity+=QuantumScale*p->opacity;
863 image_view=DestroyCacheView(image_view);
864 image->background_color.red=RoundToQuantum((MagickRealType) QuantumRange*
865 background.red/count);
866 image->background_color.green=RoundToQuantum((MagickRealType) QuantumRange*
867 background.green/count);
868 image->background_color.blue=RoundToQuantum((MagickRealType) QuantumRange*
869 background.blue/count);
870 image->background_color.opacity=RoundToQuantum((MagickRealType) QuantumRange*
871 background.opacity/count);
874 MagickExport Image *DeskewImage(const Image *image,const double threshold,
875 ExceptionInfo *exception)
910 Compute deskew angle.
912 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
913 projection=(unsigned long *) AcquireQuantumMemory((size_t) (2*width-1),
914 sizeof(*projection));
915 if (projection == (unsigned long *) NULL)
916 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
917 status=RadonTransform(image,threshold,projection,exception);
918 if (status == MagickFalse)
920 projection=(unsigned long *) RelinquishMagickMemory(projection);
921 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
925 for (i=0; i < (long) (2*width-1); i++)
927 if (projection[i] > max_projection)
929 skew=i-(long) width+1;
930 max_projection=projection[i];
933 projection=(unsigned long *) RelinquishMagickMemory(projection);
937 clone_image=CloneImage(image,0,0,MagickTrue,exception);
938 if (clone_image == (Image *) NULL)
939 return((Image *) NULL);
940 (void) SetImageVirtualPixelMethod(clone_image,BackgroundVirtualPixelMethod);
941 degrees=RadiansToDegrees(-atan((double) skew/width/8));
942 if (image->debug != MagickFalse)
943 (void) LogMagickEvent(TransformEvent,GetMagickModule()," Deskew angle: %g",
945 affine_matrix.sx=cos(DegreesToRadians(fmod((double) degrees,360.0)));
946 affine_matrix.rx=sin(DegreesToRadians(fmod((double) degrees,360.0)));
947 affine_matrix.ry=(-sin(DegreesToRadians(fmod((double) degrees,360.0))));
948 affine_matrix.sy=cos(DegreesToRadians(fmod((double) degrees,360.0)));
949 affine_matrix.tx=0.0;
950 affine_matrix.ty=0.0;
951 artifact=GetImageArtifact(image,"deskew:auto-crop");
952 if (artifact == (const char *) NULL)
954 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
955 clone_image=DestroyImage(clone_image);
956 return(deskew_image);
961 GetImageBackgroundColor(clone_image,atol(artifact),exception);
962 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
963 clone_image=DestroyImage(clone_image);
964 if (deskew_image == (Image *) NULL)
965 return((Image *) NULL);
966 median_image=MedianFilterImage(deskew_image,0.0,exception);
967 if (median_image == (Image *) NULL)
969 deskew_image=DestroyImage(deskew_image);
970 return((Image *) NULL);
972 geometry=GetImageBoundingBox(median_image,exception);
973 median_image=DestroyImage(median_image);
974 if (image->debug != MagickFalse)
975 (void) LogMagickEvent(TransformEvent,GetMagickModule()," Deskew geometry: "
976 "%lux%lu%+ld%+ld",geometry.width,geometry.height,geometry.x,geometry.y);
977 crop_image=CropImage(deskew_image,&geometry,exception);
978 deskew_image=DestroyImage(deskew_image);
983 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
987 + I n t e g r a l R o t a t e I m a g e %
991 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
993 % IntegralRotateImage() rotates the image an integral of 90 degrees. It
994 % allocates the memory necessary for the new Image structure and returns a
995 % pointer to the rotated image.
997 % The format of the IntegralRotateImage method is:
999 % Image *IntegralRotateImage(const Image *image,unsigned long rotations,
1000 % ExceptionInfo *exception)
1002 % A description of each parameter follows.
1004 % o image: the image.
1006 % o rotations: Specifies the number of 90 degree rotations.
1009 static Image *IntegralRotateImage(const Image *image,unsigned long rotations,
1010 ExceptionInfo *exception)
1012 #define RotateImageTag "Rotate/Image"
1032 Initialize rotated image attributes.
1034 assert(image != (Image *) NULL);
1038 return(CloneImage(image,0,0,MagickTrue,exception));
1039 if ((rotations == 1) || (rotations == 3))
1040 rotate_image=CloneImage(image,image->rows,image->columns,MagickTrue,
1043 rotate_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1045 if (rotate_image == (Image *) NULL)
1046 return((Image *) NULL);
1048 Integral rotate the image.
1052 image_view=AcquireCacheView(image);
1053 rotate_view=AcquireCacheView(rotate_image);
1075 GetPixelCacheTileSize(image,&tile_width,&tile_height);
1076 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1077 #pragma omp parallel for schedule(static,1) shared(progress, status)
1079 for (tile_y=0; tile_y < (long) image->rows; tile_y+=tile_height)
1084 if (status == MagickFalse)
1086 for (tile_x=0; tile_x < (long) image->columns; tile_x+=tile_width)
1091 register const IndexPacket
1092 *__restrict indexes;
1094 register const PixelPacket
1097 register IndexPacket
1098 *__restrict rotate_indexes;
1103 register PixelPacket
1111 if ((tile_x+(long) tile_width) > (long) image->columns)
1112 width=(unsigned long) (tile_width-(tile_x+tile_width-
1115 if ((tile_y+(long) tile_height) > (long) image->rows)
1116 height=(unsigned long) (tile_height-(tile_y+tile_height-
1118 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1120 if (p == (const PixelPacket *) NULL)
1125 indexes=GetCacheViewVirtualIndexQueue(image_view);
1126 for (y=0; y < (long) width; y++)
1128 register const PixelPacket
1129 *__restrict tile_pixels;
1134 q=QueueCacheViewAuthenticPixels(rotate_view,(long)
1135 rotate_image->columns-(tile_y+height),y+tile_x,height,
1137 if (q == (PixelPacket *) NULL)
1142 tile_pixels=p+(height-1)*width+y;
1143 for (x=0; x < (long) height; x++)
1145 *q++=(*tile_pixels);
1148 rotate_indexes=GetCacheViewAuthenticIndexQueue(rotate_view);
1149 if ((indexes != (IndexPacket *) NULL) &&
1150 (rotate_indexes != (IndexPacket *) NULL))
1152 register const IndexPacket
1153 *__restrict tile_indexes;
1155 tile_indexes=indexes+(height-1)*width+y;
1156 for (x=0; x < (long) height; x++)
1158 *rotate_indexes++=(*tile_indexes);
1159 tile_indexes-=width;
1162 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1163 if (sync == MagickFalse)
1167 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1172 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
1174 if (proceed == MagickFalse)
1178 (void) SetImageProgress(image,RotateImageTag,image->rows-1,image->rows);
1179 Swap(page.width,page.height);
1180 Swap(page.x,page.y);
1181 if (page.width != 0)
1182 page.x=(long) (page.width-rotate_image->columns-page.x);
1190 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1191 #pragma omp parallel for schedule(static,1) shared(progress, status)
1193 for (y=0; y < (long) image->rows; y++)
1198 register const IndexPacket
1199 *__restrict indexes;
1201 register const PixelPacket
1204 register IndexPacket
1205 *__restrict rotate_indexes;
1210 register PixelPacket
1213 if (status == MagickFalse)
1215 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,
1217 q=QueueCacheViewAuthenticPixels(rotate_view,0,(long) (image->rows-
1218 y-1),image->columns,1,exception);
1219 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
1224 indexes=GetCacheViewVirtualIndexQueue(image_view);
1225 rotate_indexes=GetCacheViewAuthenticIndexQueue(rotate_view);
1227 for (x=0; x < (long) image->columns; x++)
1229 if ((indexes != (IndexPacket *) NULL) &&
1230 (rotate_indexes != (IndexPacket *) NULL))
1231 for (x=0; x < (long) image->columns; x++)
1232 rotate_indexes[image->columns-x-1]=indexes[x];
1233 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1234 if (sync == MagickFalse)
1236 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1241 proceed=SetImageProgress(image,RotateImageTag,progress++,
1243 if (proceed == MagickFalse)
1247 if (page.width != 0)
1248 page.x=(long) (page.width-rotate_image->columns-page.x);
1249 if (page.height != 0)
1250 page.y=(long) (page.height-rotate_image->rows-page.y);
1265 GetPixelCacheTileSize(image,&tile_width,&tile_height);
1266 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1267 #pragma omp parallel for schedule(static,1) shared(progress, status)
1269 for (tile_y=0; tile_y < (long) image->rows; tile_y+=tile_height)
1274 if (status == MagickFalse)
1276 for (tile_x=0; tile_x < (long) image->columns; tile_x+=tile_width)
1281 register const IndexPacket
1282 *__restrict indexes;
1284 register const PixelPacket
1287 register IndexPacket
1288 *__restrict rotate_indexes;
1293 register PixelPacket
1301 if ((tile_x+(long) tile_width) > (long) image->columns)
1302 width=(unsigned long) (tile_width-(tile_x+tile_width-
1305 if ((tile_y+(long) tile_height) > (long) image->rows)
1306 height=(unsigned long) (tile_height-(tile_y+tile_height-
1308 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,
1310 if (p == (const PixelPacket *) NULL)
1315 indexes=GetCacheViewVirtualIndexQueue(image_view);
1316 for (y=0; y < (long) width; y++)
1318 register const PixelPacket
1319 *__restrict tile_pixels;
1324 q=QueueCacheViewAuthenticPixels(rotate_view,tile_y,(long)
1325 y+rotate_image->rows-(tile_x+width),height,1,exception);
1326 if (q == (PixelPacket *) NULL)
1331 tile_pixels=p+(width-1)-y;
1332 for (x=0; x < (long) height; x++)
1334 *q++=(*tile_pixels);
1337 rotate_indexes=GetCacheViewAuthenticIndexQueue(rotate_view);
1338 if ((indexes != (IndexPacket *) NULL) &&
1339 (rotate_indexes != (IndexPacket *) NULL))
1341 register const IndexPacket
1342 *__restrict tile_indexes;
1344 tile_indexes=indexes+(width-1)-y;
1345 for (x=0; x < (long) height; x++)
1347 *rotate_indexes++=(*tile_indexes);
1348 tile_indexes+=width;
1351 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1352 if (sync == MagickFalse)
1356 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1361 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
1363 if (proceed == MagickFalse)
1367 (void) SetImageProgress(image,RotateImageTag,image->rows-1,image->rows);
1368 Swap(page.width,page.height);
1369 Swap(page.x,page.y);
1370 if (page.height != 0)
1371 page.y=(long) (page.height-rotate_image->rows-page.y);
1375 rotate_view=DestroyCacheView(rotate_view);
1376 image_view=DestroyCacheView(image_view);
1377 rotate_image->type=image->type;
1378 rotate_image->page=page;
1379 if (status == MagickFalse)
1380 rotate_image=DestroyImage(rotate_image);
1381 return(rotate_image);
1385 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1389 + X S h e a r I m a g e %
1393 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1395 % XShearImage() shears the image in the X direction with a shear angle of
1396 % 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1397 % negative angles shear clockwise. Angles are measured relative to a vertical
1398 % Y-axis. X shears will widen an image creating 'empty' triangles on the left
1399 % and right sides of the source image.
1401 % The format of the XShearImage method is:
1403 % MagickBooleanType XShearImage(Image *image,const MagickRealType degrees,
1404 % const unsigned long width,const unsigned long height,
1405 % const long x_offset,const long y_offset)
1407 % A description of each parameter follows.
1409 % o image: the image.
1411 % o degrees: A MagickRealType representing the shearing angle along the X
1414 % o width, height, x_offset, y_offset: Defines a region of the image
1418 static MagickBooleanType XShearImage(Image *image,const MagickRealType degrees,
1419 const unsigned long width,const unsigned long height,const long x_offset,
1420 const long y_offset)
1422 #define XShearImageTag "XShear/Image"
1446 assert(image != (Image *) NULL);
1447 assert(image->signature == MagickSignature);
1448 if (image->debug != MagickFalse)
1449 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1450 GetMagickPixelPacket(image,&background);
1451 SetMagickPixelPacket(image,&image->background_color,(IndexPacket *) NULL,
1453 if (image->colorspace == CMYKColorspace)
1454 ConvertRGBToCMYK(&background);
1460 exception=(&image->exception);
1461 image_view=AcquireCacheView(image);
1462 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1463 #pragma omp parallel for schedule(static,1) shared(progress, status)
1465 for (y=0; y < (long) height; y++)
1482 register IndexPacket
1483 *__restrict indexes,
1484 *__restrict shear_indexes;
1486 register PixelPacket
1493 if (status == MagickFalse)
1495 p=GetCacheViewAuthenticPixels(image_view,0,y_offset+y,image->columns,1,
1497 if (p == (PixelPacket *) NULL)
1502 indexes=GetCacheViewAuthenticIndexQueue(image_view);
1505 displacement=degrees*(MagickRealType) (y-height/2.0);
1506 if (displacement == 0.0)
1508 if (displacement > 0.0)
1512 displacement*=(-1.0);
1515 step=(long) floor((double) displacement);
1516 area=(MagickRealType) (displacement-step);
1519 GetMagickPixelPacket(image,&source);
1520 GetMagickPixelPacket(image,&destination);
1526 Transfer pixels left-to-right.
1528 if (step > x_offset)
1531 shear_indexes=indexes-step;
1532 for (i=0; i < (long) width; i++)
1534 if ((x_offset+i) < step)
1536 SetMagickPixelPacket(image,++p,++indexes,&pixel);
1541 SetMagickPixelPacket(image,p,indexes,&source);
1542 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1543 &source,(MagickRealType) p->opacity,area,&destination);
1544 SetPixelPacket(image,&destination,q++,shear_indexes++);
1545 SetMagickPixelPacket(image,p++,indexes++,&pixel);
1547 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1548 &background,(MagickRealType) background.opacity,area,&destination);
1549 SetPixelPacket(image,&destination,q++,shear_indexes++);
1550 for (i=0; i < (step-1); i++)
1551 SetPixelPacket(image,&background,q++,shear_indexes++);
1557 Transfer pixels right-to-left.
1562 shear_indexes=indexes+step;
1563 for (i=0; i < (long) width; i++)
1569 if ((unsigned long) (x_offset+width+step-i) >= image->columns)
1571 SetMagickPixelPacket(image,p,indexes,&source);
1572 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1573 &source,(MagickRealType) p->opacity,area,&destination);
1574 SetPixelPacket(image,&destination,q,shear_indexes);
1575 SetMagickPixelPacket(image,p,indexes,&pixel);
1577 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1578 &background,(MagickRealType) background.opacity,area,&destination);
1579 SetPixelPacket(image,&destination,--q,--shear_indexes);
1580 for (i=0; i < (step-1); i++)
1581 SetPixelPacket(image,&background,--q,--shear_indexes);
1585 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1587 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1592 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1593 #pragma omp critical (MagickCore_XShearImage)
1595 proceed=SetImageProgress(image,XShearImageTag,progress++,height);
1596 if (proceed == MagickFalse)
1600 image_view=DestroyCacheView(image_view);
1605 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1609 + Y S h e a r I m a g e %
1613 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1615 % YShearImage shears the image in the Y direction with a shear angle of
1616 % 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1617 % negative angles shear clockwise. Angles are measured relative to a
1618 % horizontal X-axis. Y shears will increase the height of an image creating
1619 % 'empty' triangles on the top and bottom of the source image.
1621 % The format of the YShearImage method is:
1623 % MagickBooleanType YShearImage(Image *image,const MagickRealType degrees,
1624 % const unsigned long width,const unsigned long height,
1625 % const long x_offset,const long y_offset)
1627 % A description of each parameter follows.
1629 % o image: the image.
1631 % o degrees: A MagickRealType representing the shearing angle along the Y
1634 % o width, height, x_offset, y_offset: Defines a region of the image
1638 static MagickBooleanType YShearImage(Image *image,const MagickRealType degrees,
1639 const unsigned long width,const unsigned long height,const long x_offset,
1640 const long y_offset)
1642 #define YShearImageTag "YShear/Image"
1666 assert(image != (Image *) NULL);
1667 assert(image->signature == MagickSignature);
1668 if (image->debug != MagickFalse)
1669 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1670 GetMagickPixelPacket(image,&background);
1671 SetMagickPixelPacket(image,&image->background_color,(IndexPacket *) NULL,
1673 if (image->colorspace == CMYKColorspace)
1674 ConvertRGBToCMYK(&background);
1680 exception=(&image->exception);
1681 image_view=AcquireCacheView(image);
1682 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1683 #pragma omp parallel for schedule(static,1) shared(progress, status)
1685 for (x=0; x < (long) width; x++)
1699 register IndexPacket
1700 *__restrict indexes,
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)
1890 Adjust rotation angle.
1892 assert(image != (Image *) NULL);
1893 assert(image->signature == MagickSignature);
1894 if (image->debug != MagickFalse)
1895 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1896 assert(exception != (ExceptionInfo *) NULL);
1897 assert(exception->signature == MagickSignature);
1899 while (angle < -45.0)
1901 for (rotations=0; angle > 45.0; rotations++)
1905 Calculate shear equations.
1907 integral_image=IntegralRotateImage(image,rotations,exception);
1908 if (integral_image == (Image *) NULL)
1909 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1910 shear.x=(-tan((double) DegreesToRadians(angle)/2.0));
1911 shear.y=sin((double) DegreesToRadians(angle));
1912 if ((shear.x == 0.0) && (shear.y == 0.0))
1913 return(integral_image);
1914 if (SetImageStorageClass(integral_image,DirectClass) == MagickFalse)
1916 InheritException(exception,&integral_image->exception);
1917 integral_image=DestroyImage(integral_image);
1918 return(integral_image);
1920 if (integral_image->matte == MagickFalse)
1921 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel);
1925 width=image->columns;
1927 if ((rotations == 1) || (rotations == 3))
1930 height=image->columns;
1932 y_width=width+(long) (fabs(shear.x)*height+0.5);
1933 x_offset=(long) (width+((fabs(shear.y)*height)-width)/2.0+0.5);
1934 y_offset=(long) (height+((fabs(shear.y)*y_width)-height)/2.0+0.5);
1936 Surround image with a border.
1938 integral_image->border_color=integral_image->background_color;
1939 integral_image->compose=CopyCompositeOp;
1940 border_info.width=(unsigned long) x_offset;
1941 border_info.height=(unsigned long) y_offset;
1942 rotate_image=BorderImage(integral_image,&border_info,exception);
1943 integral_image=DestroyImage(integral_image);
1944 if (rotate_image == (Image *) NULL)
1945 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1949 (void) XShearImage(rotate_image,shear.x,width,height,x_offset,
1950 ((long) rotate_image->rows-height)/2);
1951 (void) YShearImage(rotate_image,shear.y,y_width,height,
1952 ((long) rotate_image->columns-y_width)/2,y_offset);
1953 (void) XShearImage(rotate_image,shear.x,y_width,rotate_image->rows,
1954 ((long) rotate_image->columns-y_width)/2,0);
1955 CropToFitImage(&rotate_image,shear.x,shear.y,(MagickRealType) width,
1956 (MagickRealType) height,MagickTrue,exception);
1957 rotate_image->compose=image->compose;
1958 rotate_image->page.width=0;
1959 rotate_image->page.height=0;
1960 return(rotate_image);
1964 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1968 % S h e a r I m a g e %
1972 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1974 % ShearImage() creates a new image that is a shear_image copy of an existing
1975 % one. Shearing slides one edge of an image along the X or Y axis, creating
1976 % a parallelogram. An X direction shear slides an edge along the X axis,
1977 % while a Y direction shear slides an edge along the Y axis. The amount of
1978 % the shear is controlled by a shear angle. For X direction shears, x_shear
1979 % is measured relative to the Y axis, and similarly, for Y direction shears
1980 % y_shear is measured relative to the X axis. Empty triangles left over from
1981 % shearing the image are filled with the background color defined by member
1982 % 'background_color' of the image.. ShearImage() allocates the memory
1983 % necessary for the new Image structure and returns a pointer to the new image.
1985 % ShearImage() is based on the paper "A Fast Algorithm for General Raster
1986 % Rotatation" by Alan W. Paeth.
1988 % The format of the ShearImage method is:
1990 % Image *ShearImage(const Image *image,const double x_shear,
1991 % const double y_shear,ExceptionInfo *exception)
1993 % A description of each parameter follows.
1995 % o image: the image.
1997 % o x_shear, y_shear: Specifies the number of degrees to shear the image.
1999 % o exception: return any errors or warnings in this structure.
2002 MagickExport Image *ShearImage(const Image *image,const double x_shear,
2003 const double y_shear,ExceptionInfo *exception)
2022 assert(image != (Image *) NULL);
2023 assert(image->signature == MagickSignature);
2024 if (image->debug != MagickFalse)
2025 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2026 assert(exception != (ExceptionInfo *) NULL);
2027 assert(exception->signature == MagickSignature);
2028 if ((x_shear != 0.0) && (fmod(x_shear,90.0) == 0.0))
2029 ThrowImageException(ImageError,"AngleIsDiscontinuous");
2030 if ((y_shear != 0.0) && (fmod(y_shear,90.0) == 0.0))
2031 ThrowImageException(ImageError,"AngleIsDiscontinuous");
2033 Initialize shear angle.
2035 integral_image=CloneImage(image,0,0,MagickTrue,exception);
2036 if (integral_image == (Image *) NULL)
2037 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2038 shear.x=(-tan(DegreesToRadians(fmod(x_shear,360.0))));
2039 shear.y=tan(DegreesToRadians(fmod(y_shear,360.0)));
2040 if ((shear.x == 0.0) && (shear.y == 0.0))
2041 return(integral_image);
2042 if (SetImageStorageClass(integral_image,DirectClass) == MagickFalse)
2044 InheritException(exception,&integral_image->exception);
2045 integral_image=DestroyImage(integral_image);
2046 return(integral_image);
2048 if (integral_image->matte == MagickFalse)
2049 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel);
2053 y_width=image->columns+(long) (fabs(shear.x)*image->rows+0.5);
2054 x_offset=(long) (image->columns+((fabs(shear.x)*image->rows)-image->columns)/
2056 y_offset=(long) (image->rows+((fabs(shear.y)*y_width)-image->rows)/2.0+0.5);
2058 Surround image with border.
2060 integral_image->border_color=integral_image->background_color;
2061 integral_image->compose=CopyCompositeOp;
2062 border_info.width=(unsigned long) x_offset;
2063 border_info.height=(unsigned long) y_offset;
2064 shear_image=BorderImage(integral_image,&border_info,exception);
2065 if (shear_image == (Image *) NULL)
2066 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2067 integral_image=DestroyImage(integral_image);
2071 if (shear_image->matte == MagickFalse)
2072 (void) SetImageAlphaChannel(shear_image,OpaqueAlphaChannel);
2073 (void) XShearImage(shear_image,shear.x,image->columns,image->rows,x_offset,
2074 ((long) shear_image->rows-image->rows)/2);
2075 (void) YShearImage(shear_image,shear.y,y_width,image->rows,
2076 ((long) shear_image->columns-y_width)/2,y_offset);
2077 CropToFitImage(&shear_image,shear.x,shear.y,(MagickRealType) image->columns,
2078 (MagickRealType) image->rows,MagickFalse,exception);
2079 shear_image->compose=image->compose;
2080 shear_image->page.width=0;
2081 shear_image->page.height=0;
2082 return(shear_image);