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-2011 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/thread-private.h"
78 #include "magick/threshold.h"
79 #include "magick/transform.h"
82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
86 % A f f i n e T r a n s f o r m I m a g e %
90 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
92 % AffineTransformImage() transforms an image as dictated by the affine matrix.
93 % It allocates the memory necessary for the new Image structure and returns
94 % a pointer to the new image.
96 % The format of the AffineTransformImage method is:
98 % Image *AffineTransformImage(const Image *image,
99 % AffineMatrix *affine_matrix,ExceptionInfo *exception)
101 % A description of each parameter follows:
103 % o image: the image.
105 % o affine_matrix: the affine matrix.
107 % o exception: return any errors or warnings in this structure.
110 MagickExport Image *AffineTransformImage(const Image *image,
111 const AffineMatrix *affine_matrix,ExceptionInfo *exception)
120 Affine transform image.
122 assert(image->signature == MagickSignature);
123 if (image->debug != MagickFalse)
124 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
125 assert(affine_matrix != (AffineMatrix *) NULL);
126 assert(exception != (ExceptionInfo *) NULL);
127 assert(exception->signature == MagickSignature);
128 distort[0]=affine_matrix->sx;
129 distort[1]=affine_matrix->rx;
130 distort[2]=affine_matrix->ry;
131 distort[3]=affine_matrix->sy;
132 distort[4]=affine_matrix->tx;
133 distort[5]=affine_matrix->ty;
134 deskew_image=DistortImage(image,AffineProjectionDistortion,6,distort,
135 MagickTrue,exception);
136 return(deskew_image);
140 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
144 + C r o p T o F i t I m a g e %
148 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150 % CropToFitImage() crops the sheared image as determined by the bounding box
151 % as defined by width and height and shearing angles.
153 % The format of the CropToFitImage method is:
155 % MagickBooleanType CropToFitImage(Image **image,
156 % const MagickRealType x_shear,const MagickRealType x_shear,
157 % const MagickRealType width,const MagickRealType height,
158 % const MagickBooleanType rotate,ExceptionInfo *exception)
160 % A description of each parameter follows.
162 % o image: the image.
164 % o x_shear, y_shear, width, height: Defines a region of the image to crop.
166 % o exception: return any errors or warnings in this structure.
169 static MagickBooleanType CropToFitImage(Image **image,
170 const MagickRealType x_shear,const MagickRealType y_shear,
171 const MagickRealType width,const MagickRealType height,
172 const MagickBooleanType rotate,ExceptionInfo *exception)
190 Calculate the rotated image size.
192 extent[0].x=(double) (-width/2.0);
193 extent[0].y=(double) (-height/2.0);
194 extent[1].x=(double) width/2.0;
195 extent[1].y=(double) (-height/2.0);
196 extent[2].x=(double) (-width/2.0);
197 extent[2].y=(double) height/2.0;
198 extent[3].x=(double) width/2.0;
199 extent[3].y=(double) height/2.0;
200 for (i=0; i < 4; i++)
202 extent[i].x+=x_shear*extent[i].y;
203 extent[i].y+=y_shear*extent[i].x;
204 if (rotate != MagickFalse)
205 extent[i].x+=x_shear*extent[i].y;
206 extent[i].x+=(double) (*image)->columns/2.0;
207 extent[i].y+=(double) (*image)->rows/2.0;
211 for (i=1; i < 4; i++)
213 if (min.x > extent[i].x)
215 if (min.y > extent[i].y)
217 if (max.x < extent[i].x)
219 if (max.y < extent[i].y)
222 geometry.x=(ssize_t) ceil(min.x-0.5);
223 geometry.y=(ssize_t) ceil(min.y-0.5);
224 geometry.width=(size_t) floor(max.x-min.x+0.5);
225 geometry.height=(size_t) floor(max.y-min.y+0.5);
227 (void) ParseAbsoluteGeometry("0x0+0+0",&(*image)->page);
228 crop_image=CropImage(*image,&geometry,exception);
229 if (crop_image == (Image *) NULL)
231 crop_image->page=page;
232 *image=DestroyImage(*image);
238 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
242 % D e s k e w I m a g e %
246 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
248 % DeskewImage() removes skew from the image. Skew is an artifact that
249 % occurs in scanned images because of the camera being misaligned,
250 % imperfections in the scanning or surface, or simply because the paper was
251 % not placed completely flat when scanned.
253 % The format of the DeskewImage method is:
255 % Image *DeskewImage(const Image *image,const double threshold,
256 % ExceptionInfo *exception)
258 % A description of each parameter follows:
260 % o image: the image.
262 % o threshold: separate background from foreground.
264 % o exception: return any errors or warnings in this structure.
268 typedef struct _RadonInfo
293 static RadonInfo *DestroyRadonInfo(RadonInfo *radon_info)
295 assert(radon_info != (RadonInfo *) NULL);
296 switch (radon_info->type)
300 if (radon_info->mapped == MagickFalse)
301 radon_info->cells=(unsigned short *) RelinquishMagickMemory(
304 radon_info->cells=(unsigned short *) UnmapBlob(radon_info->cells,
305 (size_t) radon_info->length);
306 RelinquishMagickResource(MemoryResource,radon_info->length);
311 radon_info->cells=(unsigned short *) UnmapBlob(radon_info->cells,(size_t)
313 RelinquishMagickResource(MapResource,radon_info->length);
317 if (radon_info->file != -1)
318 (void) close(radon_info->file);
319 (void) RelinquishUniqueFileResource(radon_info->path);
320 RelinquishMagickResource(DiskResource,radon_info->length);
326 return((RadonInfo *) RelinquishMagickMemory(radon_info));
329 static MagickBooleanType ResetRadonCells(RadonInfo *radon_info)
343 if (radon_info->type != DiskCache)
345 (void) ResetMagickMemory(radon_info->cells,0,(size_t) radon_info->length);
349 (void) MagickSeek(radon_info->file,0,SEEK_SET);
350 for (y=0; y < (ssize_t) radon_info->height; y++)
352 for (x=0; x < (ssize_t) radon_info->width; x++)
354 count=write(radon_info->file,&value,sizeof(*radon_info->cells));
355 if (count != (ssize_t) sizeof(*radon_info->cells))
358 if (x < (ssize_t) radon_info->width)
361 return(y < (ssize_t) radon_info->height ? MagickFalse : MagickTrue);
364 static RadonInfo *AcquireRadonInfo(const Image *image,const size_t width,
365 const size_t height,ExceptionInfo *exception)
373 radon_info=(RadonInfo *) AcquireMagickMemory(sizeof(*radon_info));
374 if (radon_info == (RadonInfo *) NULL)
375 return((RadonInfo *) NULL);
376 (void) ResetMagickMemory(radon_info,0,sizeof(*radon_info));
377 radon_info->width=width;
378 radon_info->height=height;
379 radon_info->length=(MagickSizeType) width*height*sizeof(*radon_info->cells);
380 radon_info->type=MemoryCache;
381 status=AcquireMagickResource(AreaResource,radon_info->length);
382 if ((status != MagickFalse) &&
383 (radon_info->length == (MagickSizeType) ((size_t) radon_info->length)))
385 status=AcquireMagickResource(MemoryResource,radon_info->length);
386 if (status != MagickFalse)
388 radon_info->mapped=MagickFalse;
389 radon_info->cells=(unsigned short *) AcquireMagickMemory((size_t)
391 if (radon_info->cells == (unsigned short *) NULL)
393 radon_info->mapped=MagickTrue;
394 radon_info->cells=(unsigned short *) MapBlob(-1,IOMode,0,(size_t)
397 if (radon_info->cells == (unsigned short *) NULL)
398 RelinquishMagickResource(MemoryResource,radon_info->length);
401 radon_info->file=(-1);
402 if (radon_info->cells == (unsigned short *) NULL)
404 status=AcquireMagickResource(DiskResource,radon_info->length);
405 if (status == MagickFalse)
407 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
408 "CacheResourcesExhausted","`%s'",image->filename);
409 return(DestroyRadonInfo(radon_info));
411 radon_info->type=DiskCache;
412 (void) AcquireMagickResource(MemoryResource,radon_info->length);
413 radon_info->file=AcquireUniqueFileResource(radon_info->path);
414 if (radon_info->file == -1)
415 return(DestroyRadonInfo(radon_info));
416 status=AcquireMagickResource(MapResource,radon_info->length);
417 if (status != MagickFalse)
419 status=ResetRadonCells(radon_info);
420 if (status != MagickFalse)
422 radon_info->cells=(unsigned short *) MapBlob(radon_info->file,
423 IOMode,0,(size_t) radon_info->length);
424 if (radon_info->cells != (unsigned short *) NULL)
425 radon_info->type=MapCache;
427 RelinquishMagickResource(MapResource,radon_info->length);
434 static inline size_t MagickMin(const size_t x,const size_t y)
441 static inline ssize_t ReadRadonCell(const RadonInfo *radon_info,
442 const off_t offset,const size_t length,unsigned char *buffer)
450 #if !defined(MAGICKCORE_HAVE_PPREAD)
451 #if defined(MAGICKCORE_OPENMP_SUPPORT)
452 #pragma omp critical (MagickCore_ReadRadonCell)
456 if (MagickSeek(radon_info->file,offset,SEEK_SET) >= 0)
460 for (i=0; i < (ssize_t) length; i+=count)
462 #if !defined(MAGICKCORE_HAVE_PPREAD)
463 count=read(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
466 count=pread(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
467 SSIZE_MAX),(off_t) (offset+i));
478 #if !defined(MAGICKCORE_HAVE_PPREAD)
485 static inline ssize_t WriteRadonCell(const RadonInfo *radon_info,
486 const off_t offset,const size_t length,const unsigned char *buffer)
494 #if !defined(MAGICKCORE_HAVE_PWRITE)
495 #if defined(MAGICKCORE_OPENMP_SUPPORT)
496 #pragma omp critical (MagickCore_WriteRadonCell)
499 if (MagickSeek(radon_info->file,offset,SEEK_SET) >= 0)
503 for (i=0; i < (ssize_t) length; i+=count)
505 #if !defined(MAGICKCORE_HAVE_PWRITE)
506 count=write(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
509 count=pwrite(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
510 SSIZE_MAX),(off_t) (offset+i));
521 #if !defined(MAGICKCORE_HAVE_PWRITE)
528 static inline unsigned short GetRadonCell(const RadonInfo *radon_info,
529 const ssize_t x,const ssize_t y)
537 i=(off_t) radon_info->height*x+y;
539 ((MagickSizeType) (i*sizeof(*radon_info->cells)) >= radon_info->length))
541 if (radon_info->type != DiskCache)
542 return(radon_info->cells[i]);
544 (void) ReadRadonCell(radon_info,i*sizeof(*radon_info->cells),
545 sizeof(*radon_info->cells),(unsigned char *) &value);
549 static inline MagickBooleanType SetRadonCell(const RadonInfo *radon_info,
550 const ssize_t x,const ssize_t y,const unsigned short value)
558 i=(off_t) radon_info->height*x+y;
560 ((MagickSizeType) (i*sizeof(*radon_info->cells)) >= radon_info->length))
562 if (radon_info->type != DiskCache)
564 radon_info->cells[i]=value;
567 count=WriteRadonCell(radon_info,i*sizeof(*radon_info->cells),
568 sizeof(*radon_info->cells),(const unsigned char *) &value);
569 if (count != (ssize_t) sizeof(*radon_info->cells))
574 static void RadonProjection(RadonInfo *source_cells,
575 RadonInfo *destination_cells,const ssize_t sign,size_t *projection)
592 for (step=1; step < p->width; step*=2)
594 for (x=0; x < (ssize_t) p->width; x+=2*(ssize_t) step)
605 for (i=0; i < (ssize_t) step; i++)
607 for (y=0; y < (ssize_t) (p->height-i-1); y++)
609 cell=GetRadonCell(p,x+i,y);
610 (void) SetRadonCell(q,x+2*i,y,cell+GetRadonCell(p,x+i+(ssize_t)
612 (void) SetRadonCell(q,x+2*i+1,y,cell+GetRadonCell(p,x+i+(ssize_t)
615 for ( ; y < (ssize_t) (p->height-i); y++)
617 cell=GetRadonCell(p,x+i,y);
618 (void) SetRadonCell(q,x+2*i,y,cell+GetRadonCell(p,x+i+(ssize_t) step,
620 (void) SetRadonCell(q,x+2*i+1,y,cell);
622 for ( ; y < (ssize_t) p->height; y++)
624 cell=GetRadonCell(p,x+i,y);
625 (void) SetRadonCell(q,x+2*i,y,cell);
626 (void) SetRadonCell(q,x+2*i+1,y,cell);
634 #if defined(MAGICKCORE_OPENMP_SUPPORT)
635 #pragma omp parallel for schedule(dynamic,4)
637 for (x=0; x < (ssize_t) p->width; x++)
646 for (y=0; y < (ssize_t) (p->height-1); y++)
651 delta=GetRadonCell(p,x,y)-(ssize_t) GetRadonCell(p,x,y+1);
654 projection[p->width+sign*x-1]=sum;
658 static MagickBooleanType RadonTransform(const Image *image,
659 const double threshold,size_t *projection,ExceptionInfo *exception)
687 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
688 source_cells=AcquireRadonInfo(image,width,image->rows,exception);
689 destination_cells=AcquireRadonInfo(image,width,image->rows,exception);
690 if ((source_cells == (RadonInfo *) NULL) ||
691 (destination_cells == (RadonInfo *) NULL))
693 if (destination_cells != (RadonInfo *) NULL)
694 destination_cells=DestroyRadonInfo(destination_cells);
695 if (source_cells != (RadonInfo *) NULL)
696 source_cells=DestroyRadonInfo(source_cells);
699 if (ResetRadonCells(source_cells) == MagickFalse)
701 destination_cells=DestroyRadonInfo(destination_cells);
702 source_cells=DestroyRadonInfo(source_cells);
705 for (i=0; i < 256; i++)
707 byte=(unsigned char) i;
708 for (count=0; byte != 0; byte>>=1)
710 bits[i]=(unsigned short) count;
713 image_view=AcquireCacheView(image);
714 #if defined(MAGICKCORE_OPENMP_SUPPORT)
715 #pragma omp parallel for schedule(dynamic,4) shared(status)
717 for (y=0; y < (ssize_t) image->rows; y++)
719 register const PixelPacket
730 if (status == MagickFalse)
732 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
733 if (p == (const PixelPacket *) NULL)
740 i=(ssize_t) (image->columns+7)/8;
741 for (x=0; x < (ssize_t) image->columns; x++)
744 if (((MagickRealType) p->red < threshold) ||
745 ((MagickRealType) p->green < threshold) ||
746 ((MagickRealType) p->blue < threshold))
751 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
760 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
763 RadonProjection(source_cells,destination_cells,-1,projection);
764 (void) ResetRadonCells(source_cells);
765 #if defined(MAGICKCORE_OPENMP_SUPPORT)
766 #pragma omp parallel for schedule(dynamic,4) shared(status)
768 for (y=0; y < (ssize_t) image->rows; y++)
770 register const PixelPacket
781 if (status == MagickFalse)
783 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
784 if (p == (const PixelPacket *) NULL)
792 for (x=0; x < (ssize_t) image->columns; x++)
795 if (((MagickRealType) p->red < threshold) ||
796 ((MagickRealType) p->green < threshold) ||
797 ((MagickRealType) p->blue < threshold))
802 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
811 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
814 RadonProjection(source_cells,destination_cells,1,projection);
815 image_view=DestroyCacheView(image_view);
816 destination_cells=DestroyRadonInfo(destination_cells);
817 source_cells=DestroyRadonInfo(source_cells);
821 static void GetImageBackgroundColor(Image *image,const ssize_t offset,
822 ExceptionInfo *exception)
837 Compute average background color.
841 GetMagickPixelPacket(image,&background);
843 image_view=AcquireCacheView(image);
844 for (y=0; y < (ssize_t) image->rows; y++)
846 register const PixelPacket
852 if ((y >= offset) && (y < ((ssize_t) image->rows-offset)))
854 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
855 if (p == (const PixelPacket *) NULL)
857 for (x=0; x < (ssize_t) image->columns; x++)
859 if ((x >= offset) && (x < ((ssize_t) image->columns-offset)))
861 background.red+=QuantumScale*GetRedPixelComponent(p);
862 background.green+=QuantumScale*GetGreenPixelComponent(p);
863 background.blue+=QuantumScale*GetBluePixelComponent(p);
864 background.opacity+=QuantumScale*GetOpacityPixelComponent(p);
869 image_view=DestroyCacheView(image_view);
870 image->background_color.red=ClampToQuantum((MagickRealType) QuantumRange*
871 background.red/count);
872 image->background_color.green=ClampToQuantum((MagickRealType) QuantumRange*
873 background.green/count);
874 image->background_color.blue=ClampToQuantum((MagickRealType) QuantumRange*
875 background.blue/count);
876 image->background_color.opacity=ClampToQuantum((MagickRealType) QuantumRange*
877 background.opacity/count);
880 MagickExport Image *DeskewImage(const Image *image,const double threshold,
881 ExceptionInfo *exception)
916 Compute deskew angle.
918 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
919 projection=(size_t *) AcquireQuantumMemory((size_t) (2*width-1),
920 sizeof(*projection));
921 if (projection == (size_t *) NULL)
922 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
923 status=RadonTransform(image,threshold,projection,exception);
924 if (status == MagickFalse)
926 projection=(size_t *) RelinquishMagickMemory(projection);
927 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
931 for (i=0; i < (ssize_t) (2*width-1); i++)
933 if (projection[i] > max_projection)
935 skew=i-(ssize_t) width+1;
936 max_projection=projection[i];
939 projection=(size_t *) RelinquishMagickMemory(projection);
943 clone_image=CloneImage(image,0,0,MagickTrue,exception);
944 if (clone_image == (Image *) NULL)
945 return((Image *) NULL);
946 (void) SetImageVirtualPixelMethod(clone_image,BackgroundVirtualPixelMethod);
947 degrees=RadiansToDegrees(-atan((double) skew/width/8));
948 if (image->debug != MagickFalse)
949 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
950 " Deskew angle: %g",degrees);
951 affine_matrix.sx=cos(DegreesToRadians(fmod((double) degrees,360.0)));
952 affine_matrix.rx=sin(DegreesToRadians(fmod((double) degrees,360.0)));
953 affine_matrix.ry=(-sin(DegreesToRadians(fmod((double) degrees,360.0))));
954 affine_matrix.sy=cos(DegreesToRadians(fmod((double) degrees,360.0)));
955 affine_matrix.tx=0.0;
956 affine_matrix.ty=0.0;
957 artifact=GetImageArtifact(image,"deskew:auto-crop");
958 if (artifact == (const char *) NULL)
960 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
961 clone_image=DestroyImage(clone_image);
962 return(deskew_image);
967 GetImageBackgroundColor(clone_image,(ssize_t) StringToLong(artifact),
969 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
970 clone_image=DestroyImage(clone_image);
971 if (deskew_image == (Image *) NULL)
972 return((Image *) NULL);
973 median_image=MedianFilterImage(deskew_image,0.0,exception);
974 if (median_image == (Image *) NULL)
976 deskew_image=DestroyImage(deskew_image);
977 return((Image *) NULL);
979 geometry=GetImageBoundingBox(median_image,exception);
980 median_image=DestroyImage(median_image);
981 if (image->debug != MagickFalse)
982 (void) LogMagickEvent(TransformEvent,GetMagickModule()," Deskew geometry: "
983 "%.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
984 geometry.height,(double) geometry.x,(double) geometry.y);
985 crop_image=CropImage(deskew_image,&geometry,exception);
986 deskew_image=DestroyImage(deskew_image);
991 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
995 + I n t e g r a l R o t a t e I m a g e %
999 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1001 % IntegralRotateImage() rotates the image an integral of 90 degrees. It
1002 % allocates the memory necessary for the new Image structure and returns a
1003 % pointer to the rotated image.
1005 % The format of the IntegralRotateImage method is:
1007 % Image *IntegralRotateImage(const Image *image,size_t rotations,
1008 % ExceptionInfo *exception)
1010 % A description of each parameter follows.
1012 % o image: the image.
1014 % o rotations: Specifies the number of 90 degree rotations.
1017 static Image *IntegralRotateImage(const Image *image,size_t rotations,
1018 ExceptionInfo *exception)
1020 #define RotateImageTag "Rotate/Image"
1042 Initialize rotated image attributes.
1044 assert(image != (Image *) NULL);
1048 return(CloneImage(image,0,0,MagickTrue,exception));
1049 if ((rotations == 1) || (rotations == 3))
1050 rotate_image=CloneImage(image,image->rows,image->columns,MagickTrue,
1053 rotate_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1055 if (rotate_image == (Image *) NULL)
1056 return((Image *) NULL);
1058 Integral rotate the image.
1062 image_view=AcquireCacheView(image);
1063 rotate_view=AcquireCacheView(rotate_image);
1085 GetPixelCacheTileSize(image,&tile_width,&tile_height);
1086 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1087 #pragma omp parallel for schedule(static,1) shared(progress, status) omp_throttle(1)
1089 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
1094 if (status == MagickFalse)
1096 for (tile_x=0; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
1101 register const IndexPacket
1104 register const PixelPacket
1107 register IndexPacket
1108 *restrict rotate_indexes;
1113 register PixelPacket
1121 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
1122 width=(size_t) (tile_width-(tile_x+tile_width-
1125 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
1126 height=(size_t) (tile_height-(tile_y+tile_height-
1128 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1130 if (p == (const PixelPacket *) NULL)
1135 indexes=GetCacheViewVirtualIndexQueue(image_view);
1136 for (y=0; y < (ssize_t) width; y++)
1138 register const PixelPacket
1139 *restrict tile_pixels;
1144 q=QueueCacheViewAuthenticPixels(rotate_view,(ssize_t)
1145 (rotate_image->columns-(tile_y+height)),y+tile_x,height,
1147 if (q == (PixelPacket *) NULL)
1152 tile_pixels=p+(height-1)*width+y;
1153 for (x=0; x < (ssize_t) height; x++)
1155 *q++=(*tile_pixels);
1158 rotate_indexes=GetCacheViewAuthenticIndexQueue(rotate_view);
1159 if ((indexes != (IndexPacket *) NULL) &&
1160 (rotate_indexes != (IndexPacket *) NULL))
1162 register const IndexPacket
1163 *restrict tile_indexes;
1165 tile_indexes=indexes+(height-1)*width+y;
1166 for (x=0; x < (ssize_t) height; x++)
1168 *rotate_indexes++=(*tile_indexes);
1169 tile_indexes-=width;
1172 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1173 if (sync == MagickFalse)
1177 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1182 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
1184 if (proceed == MagickFalse)
1188 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1189 image->rows-1,image->rows);
1190 Swap(page.width,page.height);
1191 Swap(page.x,page.y);
1192 if (page.width != 0)
1193 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1201 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1202 #pragma omp parallel for schedule(static,8) shared(progress,status) omp_throttle(1)
1204 for (y=0; y < (ssize_t) image->rows; y++)
1209 register const IndexPacket
1212 register const PixelPacket
1215 register IndexPacket
1216 *restrict rotate_indexes;
1221 register PixelPacket
1224 if (status == MagickFalse)
1226 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,
1228 q=QueueCacheViewAuthenticPixels(rotate_view,0,(ssize_t) (image->rows-
1229 y-1),image->columns,1,exception);
1230 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
1235 indexes=GetCacheViewVirtualIndexQueue(image_view);
1236 rotate_indexes=GetCacheViewAuthenticIndexQueue(rotate_view);
1238 for (x=0; x < (ssize_t) image->columns; x++)
1240 if ((indexes != (IndexPacket *) NULL) &&
1241 (rotate_indexes != (IndexPacket *) NULL))
1242 for (x=0; x < (ssize_t) image->columns; x++)
1243 rotate_indexes[image->columns-x-1]=indexes[x];
1244 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1245 if (sync == MagickFalse)
1247 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1252 proceed=SetImageProgress(image,RotateImageTag,progress++,
1254 if (proceed == MagickFalse)
1258 if (page.width != 0)
1259 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1260 if (page.height != 0)
1261 page.y=(ssize_t) (page.height-rotate_image->rows-page.y);
1276 GetPixelCacheTileSize(image,&tile_width,&tile_height);
1277 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1278 #pragma omp parallel for schedule(static,1) shared(progress,status) omp_throttle(1)
1280 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
1285 if (status == MagickFalse)
1287 for (tile_x=0; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
1292 register const IndexPacket
1295 register const PixelPacket
1298 register IndexPacket
1299 *restrict rotate_indexes;
1304 register PixelPacket
1312 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
1313 width=(size_t) (tile_width-(tile_x+tile_width-
1316 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
1317 height=(size_t) (tile_height-(tile_y+tile_height-
1319 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,
1321 if (p == (const PixelPacket *) NULL)
1326 indexes=GetCacheViewVirtualIndexQueue(image_view);
1327 for (y=0; y < (ssize_t) width; y++)
1329 register const PixelPacket
1330 *restrict tile_pixels;
1335 q=QueueCacheViewAuthenticPixels(rotate_view,tile_y,(ssize_t)
1336 (y+rotate_image->rows-(tile_x+width)),height,1,exception);
1337 if (q == (PixelPacket *) NULL)
1342 tile_pixels=p+(width-1)-y;
1343 for (x=0; x < (ssize_t) height; x++)
1345 *q++=(*tile_pixels);
1348 rotate_indexes=GetCacheViewAuthenticIndexQueue(rotate_view);
1349 if ((indexes != (IndexPacket *) NULL) &&
1350 (rotate_indexes != (IndexPacket *) NULL))
1352 register const IndexPacket
1353 *restrict tile_indexes;
1355 tile_indexes=indexes+(width-1)-y;
1356 for (x=0; x < (ssize_t) height; x++)
1358 *rotate_indexes++=(*tile_indexes);
1359 tile_indexes+=width;
1362 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1363 if (sync == MagickFalse)
1367 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1372 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
1374 if (proceed == MagickFalse)
1378 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1379 image->rows-1,image->rows);
1380 Swap(page.width,page.height);
1381 Swap(page.x,page.y);
1382 if (page.height != 0)
1383 page.y=(ssize_t) (page.height-rotate_image->rows-page.y);
1387 rotate_view=DestroyCacheView(rotate_view);
1388 image_view=DestroyCacheView(image_view);
1389 rotate_image->type=image->type;
1390 rotate_image->page=page;
1391 if (status == MagickFalse)
1392 rotate_image=DestroyImage(rotate_image);
1393 return(rotate_image);
1397 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1401 + X S h e a r I m a g e %
1405 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1407 % XShearImage() shears the image in the X direction with a shear angle of
1408 % 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1409 % negative angles shear clockwise. Angles are measured relative to a vertical
1410 % Y-axis. X shears will widen an image creating 'empty' triangles on the left
1411 % and right sides of the source image.
1413 % The format of the XShearImage method is:
1415 % MagickBooleanType XShearImage(Image *image,const MagickRealType degrees,
1416 % const size_t width,const size_t height,
1417 % const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
1419 % A description of each parameter follows.
1421 % o image: the image.
1423 % o degrees: A MagickRealType representing the shearing angle along the X
1426 % o width, height, x_offset, y_offset: Defines a region of the image
1429 % o exception: return any errors or warnings in this structure.
1432 static MagickBooleanType XShearImage(Image *image,const MagickRealType degrees,
1433 const size_t width,const size_t height,const ssize_t x_offset,
1434 const ssize_t y_offset,ExceptionInfo *exception)
1436 #define XShearImageTag "XShear/Image"
1459 assert(image != (Image *) NULL);
1460 assert(image->signature == MagickSignature);
1461 if (image->debug != MagickFalse)
1462 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1463 GetMagickPixelPacket(image,&background);
1464 SetMagickPixelPacket(image,&image->background_color,(IndexPacket *) NULL,
1466 if (image->colorspace == CMYKColorspace)
1467 ConvertRGBToCMYK(&background);
1473 image_view=AcquireCacheView(image);
1474 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1475 #pragma omp parallel for schedule(dynamic,4) shared(progress, status)
1477 for (y=0; y < (ssize_t) height; y++)
1494 register IndexPacket
1496 *restrict shear_indexes;
1498 register PixelPacket
1505 if (status == MagickFalse)
1507 p=GetCacheViewAuthenticPixels(image_view,0,y_offset+y,image->columns,1,
1509 if (p == (PixelPacket *) NULL)
1514 indexes=GetCacheViewAuthenticIndexQueue(image_view);
1517 displacement=degrees*(MagickRealType) (y-height/2.0);
1518 if (displacement == 0.0)
1520 if (displacement > 0.0)
1524 displacement*=(-1.0);
1527 step=(ssize_t) floor((double) displacement);
1528 area=(MagickRealType) (displacement-step);
1531 GetMagickPixelPacket(image,&source);
1532 GetMagickPixelPacket(image,&destination);
1538 Transfer pixels left-to-right.
1540 if (step > x_offset)
1543 shear_indexes=indexes-step;
1544 for (i=0; i < (ssize_t) width; i++)
1546 if ((x_offset+i) < step)
1548 SetMagickPixelPacket(image,++p,++indexes,&pixel);
1553 SetMagickPixelPacket(image,p,indexes,&source);
1554 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1555 &source,(MagickRealType) p->opacity,area,&destination);
1556 SetPixelPacket(image,&destination,q++,shear_indexes++);
1557 SetMagickPixelPacket(image,p++,indexes++,&pixel);
1559 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1560 &background,(MagickRealType) background.opacity,area,&destination);
1561 SetPixelPacket(image,&destination,q++,shear_indexes++);
1562 for (i=0; i < (step-1); i++)
1563 SetPixelPacket(image,&background,q++,shear_indexes++);
1569 Transfer pixels right-to-left.
1574 shear_indexes=indexes+step;
1575 for (i=0; i < (ssize_t) width; i++)
1581 if ((size_t) (x_offset+width+step-i) >= image->columns)
1583 SetMagickPixelPacket(image,p,indexes,&source);
1584 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1585 &source,(MagickRealType) p->opacity,area,&destination);
1586 SetPixelPacket(image,&destination,q,shear_indexes);
1587 SetMagickPixelPacket(image,p,indexes,&pixel);
1589 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1590 &background,(MagickRealType) background.opacity,area,&destination);
1591 SetPixelPacket(image,&destination,--q,--shear_indexes);
1592 for (i=0; i < (step-1); i++)
1593 SetPixelPacket(image,&background,--q,--shear_indexes);
1597 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1599 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1604 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1605 #pragma omp critical (MagickCore_XShearImage)
1607 proceed=SetImageProgress(image,XShearImageTag,progress++,height);
1608 if (proceed == MagickFalse)
1612 image_view=DestroyCacheView(image_view);
1617 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1621 + Y S h e a r I m a g e %
1625 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1627 % YShearImage shears the image in the Y direction with a shear angle of
1628 % 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1629 % negative angles shear clockwise. Angles are measured relative to a
1630 % horizontal X-axis. Y shears will increase the height of an image creating
1631 % 'empty' triangles on the top and bottom of the source image.
1633 % The format of the YShearImage method is:
1635 % MagickBooleanType YShearImage(Image *image,const MagickRealType degrees,
1636 % const size_t width,const size_t height,
1637 % const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
1639 % A description of each parameter follows.
1641 % o image: the image.
1643 % o degrees: A MagickRealType representing the shearing angle along the Y
1646 % o width, height, x_offset, y_offset: Defines a region of the image
1649 % o exception: return any errors or warnings in this structure.
1652 static MagickBooleanType YShearImage(Image *image,const MagickRealType degrees,
1653 const size_t width,const size_t height,const ssize_t x_offset,
1654 const ssize_t y_offset,ExceptionInfo *exception)
1656 #define YShearImageTag "YShear/Image"
1679 assert(image != (Image *) NULL);
1680 assert(image->signature == MagickSignature);
1681 if (image->debug != MagickFalse)
1682 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1683 GetMagickPixelPacket(image,&background);
1684 SetMagickPixelPacket(image,&image->background_color,(IndexPacket *) NULL,
1686 if (image->colorspace == CMYKColorspace)
1687 ConvertRGBToCMYK(&background);
1693 image_view=AcquireCacheView(image);
1694 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1695 #pragma omp parallel for schedule(dynamic,4) shared(progress, status)
1697 for (x=0; x < (ssize_t) width; x++)
1711 register IndexPacket
1713 *restrict shear_indexes;
1718 register PixelPacket
1725 if (status == MagickFalse)
1727 p=GetCacheViewAuthenticPixels(image_view,x_offset+x,0,1,image->rows,
1729 if (p == (PixelPacket *) NULL)
1734 indexes=GetCacheViewAuthenticIndexQueue(image_view);
1737 displacement=degrees*(MagickRealType) (x-width/2.0);
1738 if (displacement == 0.0)
1740 if (displacement > 0.0)
1744 displacement*=(-1.0);
1747 step=(ssize_t) floor((double) displacement);
1748 area=(MagickRealType) (displacement-step);
1751 GetMagickPixelPacket(image,&source);
1752 GetMagickPixelPacket(image,&destination);
1758 Transfer pixels top-to-bottom.
1760 if (step > y_offset)
1763 shear_indexes=indexes-step;
1764 for (i=0; i < (ssize_t) height; i++)
1766 if ((y_offset+i) < step)
1768 SetMagickPixelPacket(image,++p,++indexes,&pixel);
1773 SetMagickPixelPacket(image,p,indexes,&source);
1774 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1775 &source,(MagickRealType) p->opacity,area,&destination);
1776 SetPixelPacket(image,&destination,q++,shear_indexes++);
1777 SetMagickPixelPacket(image,p++,indexes++,&pixel);
1779 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1780 &background,(MagickRealType) background.opacity,area,&destination);
1781 SetPixelPacket(image,&destination,q++,shear_indexes++);
1782 for (i=0; i < (step-1); i++)
1783 SetPixelPacket(image,&background,q++,shear_indexes++);
1789 Transfer pixels bottom-to-top.
1794 shear_indexes=indexes+step;
1795 for (i=0; i < (ssize_t) height; i++)
1801 if ((size_t) (y_offset+height+step-i) >= image->rows)
1803 SetMagickPixelPacket(image,p,indexes,&source);
1804 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1805 &source,(MagickRealType) p->opacity,area,&destination);
1806 SetPixelPacket(image,&destination,q,shear_indexes);
1807 SetMagickPixelPacket(image,p,indexes,&pixel);
1809 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1810 &background,(MagickRealType) background.opacity,area,&destination);
1811 SetPixelPacket(image,&destination,--q,--shear_indexes);
1812 for (i=0; i < (step-1); i++)
1813 SetPixelPacket(image,&background,--q,--shear_indexes);
1817 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1819 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1824 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1825 #pragma omp critical (MagickCore_YShearImage)
1827 proceed=SetImageProgress(image,YShearImageTag,progress++,image->rows);
1828 if (proceed == MagickFalse)
1832 image_view=DestroyCacheView(image_view);
1837 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1841 % R o t a t e I m a g e %
1845 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1847 % RotateImage() creates a new image that is a rotated copy of an existing
1848 % one. Positive angles rotate counter-clockwise (right-hand rule), while
1849 % negative angles rotate clockwise. Rotated images are usually larger than
1850 % the originals and have 'empty' triangular corners. X axis. Empty
1851 % triangles left over from shearing the image are filled with the background
1852 % color defined by member 'background_color' of the image. RotateImage
1853 % allocates the memory necessary for the new Image structure and returns a
1854 % pointer to the new image.
1856 % RotateImage() is based on the paper "A Fast Algorithm for General
1857 % Raster Rotatation" by Alan W. Paeth. RotateImage is adapted from a similar
1858 % method based on the Paeth paper written by Michael Halle of the Spatial
1859 % Imaging Group, MIT Media Lab.
1861 % The format of the RotateImage method is:
1863 % Image *RotateImage(const Image *image,const double degrees,
1864 % ExceptionInfo *exception)
1866 % A description of each parameter follows.
1868 % o image: the image.
1870 % o degrees: Specifies the number of degrees to rotate the image.
1872 % o exception: return any errors or warnings in this structure.
1875 MagickExport Image *RotateImage(const Image *image,const double degrees,
1876 ExceptionInfo *exception)
1905 Adjust rotation angle.
1907 assert(image != (Image *) NULL);
1908 assert(image->signature == MagickSignature);
1909 if (image->debug != MagickFalse)
1910 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1911 assert(exception != (ExceptionInfo *) NULL);
1912 assert(exception->signature == MagickSignature);
1914 while (angle < -45.0)
1916 for (rotations=0; angle > 45.0; rotations++)
1920 Calculate shear equations.
1922 integral_image=IntegralRotateImage(image,rotations,exception);
1923 if (integral_image == (Image *) NULL)
1924 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1925 shear.x=(-tan((double) DegreesToRadians(angle)/2.0));
1926 shear.y=sin((double) DegreesToRadians(angle));
1927 if ((shear.x == 0.0) && (shear.y == 0.0))
1928 return(integral_image);
1929 if (SetImageStorageClass(integral_image,DirectClass) == MagickFalse)
1931 InheritException(exception,&integral_image->exception);
1932 integral_image=DestroyImage(integral_image);
1933 return(integral_image);
1935 if (integral_image->matte == MagickFalse)
1936 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel);
1940 width=image->columns;
1942 if ((rotations == 1) || (rotations == 3))
1945 height=image->columns;
1947 y_width=width+(ssize_t) floor(fabs(shear.x)*height+0.5);
1948 x_offset=(ssize_t) ceil((double) width+((fabs(shear.y)*height)-width)/2.0-
1950 y_offset=(ssize_t) ceil((double) height+((fabs(shear.y)*y_width)-height)/2.0-
1953 Surround image with a border.
1955 integral_image->border_color=integral_image->background_color;
1956 integral_image->compose=CopyCompositeOp;
1957 border_info.width=(size_t) x_offset;
1958 border_info.height=(size_t) y_offset;
1959 rotate_image=BorderImage(integral_image,&border_info,exception);
1960 integral_image=DestroyImage(integral_image);
1961 if (rotate_image == (Image *) NULL)
1962 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1966 status=XShearImage(rotate_image,shear.x,width,height,x_offset,(ssize_t)
1967 (rotate_image->rows-height)/2,exception);
1968 if (status == MagickFalse)
1970 rotate_image=DestroyImage(rotate_image);
1971 return((Image *) NULL);
1973 status=YShearImage(rotate_image,shear.y,y_width,height,(ssize_t)
1974 (rotate_image->columns-y_width)/2,y_offset,exception);
1975 if (status == MagickFalse)
1977 rotate_image=DestroyImage(rotate_image);
1978 return((Image *) NULL);
1980 status=XShearImage(rotate_image,shear.x,y_width,rotate_image->rows,(ssize_t)
1981 (rotate_image->columns-y_width)/2,0,exception);
1982 if (status == MagickFalse)
1984 rotate_image=DestroyImage(rotate_image);
1985 return((Image *) NULL);
1987 status=CropToFitImage(&rotate_image,shear.x,shear.y,(MagickRealType) width,
1988 (MagickRealType) height,MagickTrue,exception);
1989 if (status == MagickFalse)
1991 rotate_image=DestroyImage(rotate_image);
1992 return((Image *) NULL);
1994 rotate_image->compose=image->compose;
1995 rotate_image->page.width=0;
1996 rotate_image->page.height=0;
1997 return(rotate_image);
2001 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2005 % S h e a r I m a g e %
2009 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2011 % ShearImage() creates a new image that is a shear_image copy of an existing
2012 % one. Shearing slides one edge of an image along the X or Y axis, creating
2013 % a parallelogram. An X direction shear slides an edge along the X axis,
2014 % while a Y direction shear slides an edge along the Y axis. The amount of
2015 % the shear is controlled by a shear angle. For X direction shears, x_shear
2016 % is measured relative to the Y axis, and similarly, for Y direction shears
2017 % y_shear is measured relative to the X axis. Empty triangles left over from
2018 % shearing the image are filled with the background color defined by member
2019 % 'background_color' of the image.. ShearImage() allocates the memory
2020 % necessary for the new Image structure and returns a pointer to the new image.
2022 % ShearImage() is based on the paper "A Fast Algorithm for General Raster
2023 % Rotatation" by Alan W. Paeth.
2025 % The format of the ShearImage method is:
2027 % Image *ShearImage(const Image *image,const double x_shear,
2028 % const double y_shear,ExceptionInfo *exception)
2030 % A description of each parameter follows.
2032 % o image: the image.
2034 % o x_shear, y_shear: Specifies the number of degrees to shear the image.
2036 % o exception: return any errors or warnings in this structure.
2039 MagickExport Image *ShearImage(const Image *image,const double x_shear,
2040 const double y_shear,ExceptionInfo *exception)
2062 assert(image != (Image *) NULL);
2063 assert(image->signature == MagickSignature);
2064 if (image->debug != MagickFalse)
2065 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2066 assert(exception != (ExceptionInfo *) NULL);
2067 assert(exception->signature == MagickSignature);
2068 if ((x_shear != 0.0) && (fmod(x_shear,90.0) == 0.0))
2069 ThrowImageException(ImageError,"AngleIsDiscontinuous");
2070 if ((y_shear != 0.0) && (fmod(y_shear,90.0) == 0.0))
2071 ThrowImageException(ImageError,"AngleIsDiscontinuous");
2073 Initialize shear angle.
2075 integral_image=CloneImage(image,0,0,MagickTrue,exception);
2076 if (integral_image == (Image *) NULL)
2077 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2078 shear.x=(-tan(DegreesToRadians(fmod(x_shear,360.0))));
2079 shear.y=tan(DegreesToRadians(fmod(y_shear,360.0)));
2080 if ((shear.x == 0.0) && (shear.y == 0.0))
2081 return(integral_image);
2082 if (SetImageStorageClass(integral_image,DirectClass) == MagickFalse)
2084 InheritException(exception,&integral_image->exception);
2085 integral_image=DestroyImage(integral_image);
2086 return(integral_image);
2088 if (integral_image->matte == MagickFalse)
2089 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel);
2093 y_width=image->columns+(ssize_t) floor(fabs(shear.x)*image->rows+0.5);
2094 x_offset=(ssize_t) ceil((double) image->columns+((fabs(shear.x)*image->rows)-
2095 image->columns)/2.0-0.5);
2096 y_offset=(ssize_t) ceil((double) image->rows+((fabs(shear.y)*y_width)-
2097 image->rows)/2.0-0.5);
2099 Surround image with border.
2101 integral_image->border_color=integral_image->background_color;
2102 integral_image->compose=CopyCompositeOp;
2103 border_info.width=(size_t) x_offset;
2104 border_info.height=(size_t) y_offset;
2105 shear_image=BorderImage(integral_image,&border_info,exception);
2106 integral_image=DestroyImage(integral_image);
2107 if (shear_image == (Image *) NULL)
2108 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2112 if (shear_image->matte == MagickFalse)
2113 (void) SetImageAlphaChannel(shear_image,OpaqueAlphaChannel);
2114 status=XShearImage(shear_image,shear.x,image->columns,image->rows,x_offset,
2115 (ssize_t) (shear_image->rows-image->rows)/2,exception);
2116 if (status == MagickFalse)
2118 shear_image=DestroyImage(shear_image);
2119 return((Image *) NULL);
2121 status=YShearImage(shear_image,shear.y,y_width,image->rows,(ssize_t)
2122 (shear_image->columns-y_width)/2,y_offset,exception);
2123 if (status == MagickFalse)
2125 shear_image=DestroyImage(shear_image);
2126 return((Image *) NULL);
2128 status=CropToFitImage(&shear_image,shear.x,shear.y,(MagickRealType)
2129 image->columns,(MagickRealType) image->rows,MagickFalse,exception);
2130 if (status == MagickFalse)
2132 shear_image=DestroyImage(shear_image);
2133 return((Image *) NULL);
2135 shear_image->compose=image->compose;
2136 shear_image->page.width=0;
2137 shear_image->page.height=0;
2138 return(shear_image);