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 "MagickCore/studio.h"
49 #include "MagickCore/artifact.h"
50 #include "MagickCore/attribute.h"
51 #include "MagickCore/blob-private.h"
52 #include "MagickCore/cache-private.h"
53 #include "MagickCore/color-private.h"
54 #include "MagickCore/colorspace-private.h"
55 #include "MagickCore/composite.h"
56 #include "MagickCore/composite-private.h"
57 #include "MagickCore/decorate.h"
58 #include "MagickCore/distort.h"
59 #include "MagickCore/draw.h"
60 #include "MagickCore/exception.h"
61 #include "MagickCore/exception-private.h"
62 #include "MagickCore/gem.h"
63 #include "MagickCore/geometry.h"
64 #include "MagickCore/image.h"
65 #include "MagickCore/image-private.h"
66 #include "MagickCore/memory_.h"
67 #include "MagickCore/list.h"
68 #include "MagickCore/monitor.h"
69 #include "MagickCore/monitor-private.h"
70 #include "MagickCore/pixel-accessor.h"
71 #include "MagickCore/quantum.h"
72 #include "MagickCore/resource_.h"
73 #include "MagickCore/shear.h"
74 #include "MagickCore/statistic.h"
75 #include "MagickCore/string_.h"
76 #include "MagickCore/string-private.h"
77 #include "MagickCore/thread-private.h"
78 #include "MagickCore/threshold.h"
79 #include "MagickCore/transform.h"
82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
86 % 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)
341 if (radon_info->type != DiskCache)
343 (void) ResetMagickMemory(radon_info->cells,0,(size_t) radon_info->length);
347 (void) lseek(radon_info->file,0,SEEK_SET);
348 for (y=0; y < (ssize_t) radon_info->height; y++)
350 for (x=0; x < (ssize_t) radon_info->width; x++)
352 count=write(radon_info->file,&value,sizeof(*radon_info->cells));
353 if (count != (ssize_t) sizeof(*radon_info->cells))
356 if (x < (ssize_t) radon_info->width)
359 return(y < (ssize_t) radon_info->height ? MagickFalse : MagickTrue);
362 static RadonInfo *AcquireRadonInfo(const Image *image,const size_t width,
363 const size_t height,ExceptionInfo *exception)
371 radon_info=(RadonInfo *) AcquireMagickMemory(sizeof(*radon_info));
372 if (radon_info == (RadonInfo *) NULL)
373 return((RadonInfo *) NULL);
374 (void) ResetMagickMemory(radon_info,0,sizeof(*radon_info));
375 radon_info->width=width;
376 radon_info->height=height;
377 radon_info->length=(MagickSizeType) width*height*sizeof(*radon_info->cells);
378 radon_info->type=MemoryCache;
379 status=AcquireMagickResource(AreaResource,radon_info->length);
380 if ((status != MagickFalse) &&
381 (radon_info->length == (MagickSizeType) ((size_t) radon_info->length)))
383 status=AcquireMagickResource(MemoryResource,radon_info->length);
384 if (status != MagickFalse)
386 radon_info->mapped=MagickFalse;
387 radon_info->cells=(unsigned short *) AcquireMagickMemory((size_t)
389 if (radon_info->cells == (unsigned short *) NULL)
391 radon_info->mapped=MagickTrue;
392 radon_info->cells=(unsigned short *) MapBlob(-1,IOMode,0,(size_t)
395 if (radon_info->cells == (unsigned short *) NULL)
396 RelinquishMagickResource(MemoryResource,radon_info->length);
399 radon_info->file=(-1);
400 if (radon_info->cells == (unsigned short *) NULL)
402 status=AcquireMagickResource(DiskResource,radon_info->length);
403 if (status == MagickFalse)
405 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
406 "CacheResourcesExhausted","`%s'",image->filename);
407 return(DestroyRadonInfo(radon_info));
409 radon_info->type=DiskCache;
410 (void) AcquireMagickResource(MemoryResource,radon_info->length);
411 radon_info->file=AcquireUniqueFileResource(radon_info->path);
412 if (radon_info->file == -1)
413 return(DestroyRadonInfo(radon_info));
414 status=AcquireMagickResource(MapResource,radon_info->length);
415 if (status != MagickFalse)
417 status=ResetRadonCells(radon_info);
418 if (status != MagickFalse)
420 radon_info->cells=(unsigned short *) MapBlob(radon_info->file,
421 IOMode,0,(size_t) radon_info->length);
422 if (radon_info->cells != (unsigned short *) NULL)
423 radon_info->type=MapCache;
425 RelinquishMagickResource(MapResource,radon_info->length);
432 static inline size_t MagickMin(const size_t x,const size_t y)
439 static inline ssize_t ReadRadonCell(const RadonInfo *radon_info,
440 const MagickOffsetType offset,const size_t length,unsigned char *buffer)
448 #if !defined(MAGICKCORE_HAVE_PPREAD)
449 #if defined(MAGICKCORE_OPENMP_SUPPORT)
450 #pragma omp critical (MagickCore_ReadRadonCell)
454 if (lseek(radon_info->file,offset,SEEK_SET) >= 0)
458 for (i=0; i < (ssize_t) length; i+=count)
460 #if !defined(MAGICKCORE_HAVE_PPREAD)
461 count=read(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
464 count=pread(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
465 SSIZE_MAX),offset+i);
476 #if !defined(MAGICKCORE_HAVE_PPREAD)
483 static inline ssize_t WriteRadonCell(const RadonInfo *radon_info,
484 const MagickOffsetType offset,const size_t length,const unsigned char *buffer)
492 #if !defined(MAGICKCORE_HAVE_PWRITE)
493 #if defined(MAGICKCORE_OPENMP_SUPPORT)
494 #pragma omp critical (MagickCore_WriteRadonCell)
497 if (lseek(radon_info->file,offset,SEEK_SET) >= 0)
501 for (i=0; i < (ssize_t) length; i+=count)
503 #if !defined(MAGICKCORE_HAVE_PWRITE)
504 count=write(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
507 count=pwrite(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
508 SSIZE_MAX),offset+i);
519 #if !defined(MAGICKCORE_HAVE_PWRITE)
526 static inline unsigned short GetRadonCell(const RadonInfo *radon_info,
527 const ssize_t x,const ssize_t y)
535 i=(MagickOffsetType) radon_info->height*x+y;
537 ((MagickSizeType) (i*sizeof(*radon_info->cells)) >= radon_info->length))
539 if (radon_info->type != DiskCache)
540 return(radon_info->cells[i]);
542 (void) ReadRadonCell(radon_info,i*sizeof(*radon_info->cells),
543 sizeof(*radon_info->cells),(unsigned char *) &value);
547 static inline MagickBooleanType SetRadonCell(const RadonInfo *radon_info,
548 const ssize_t x,const ssize_t y,const unsigned short value)
556 i=(MagickOffsetType) radon_info->height*x+y;
558 ((MagickSizeType) (i*sizeof(*radon_info->cells)) >= radon_info->length))
560 if (radon_info->type != DiskCache)
562 radon_info->cells[i]=value;
565 count=WriteRadonCell(radon_info,i*sizeof(*radon_info->cells),
566 sizeof(*radon_info->cells),(const unsigned char *) &value);
567 if (count != (ssize_t) sizeof(*radon_info->cells))
572 static void RadonProjection(RadonInfo *source_cells,
573 RadonInfo *destination_cells,const ssize_t sign,size_t *projection)
590 for (step=1; step < p->width; step*=2)
592 for (x=0; x < (ssize_t) p->width; x+=2*(ssize_t) step)
603 for (i=0; i < (ssize_t) step; i++)
605 for (y=0; y < (ssize_t) (p->height-i-1); y++)
607 cell=GetRadonCell(p,x+i,y);
608 (void) SetRadonCell(q,x+2*i,y,cell+GetRadonCell(p,x+i+(ssize_t)
610 (void) SetRadonCell(q,x+2*i+1,y,cell+GetRadonCell(p,x+i+(ssize_t)
613 for ( ; y < (ssize_t) (p->height-i); y++)
615 cell=GetRadonCell(p,x+i,y);
616 (void) SetRadonCell(q,x+2*i,y,cell+GetRadonCell(p,x+i+(ssize_t) step,
618 (void) SetRadonCell(q,x+2*i+1,y,cell);
620 for ( ; y < (ssize_t) p->height; y++)
622 cell=GetRadonCell(p,x+i,y);
623 (void) SetRadonCell(q,x+2*i,y,cell);
624 (void) SetRadonCell(q,x+2*i+1,y,cell);
632 #if defined(MAGICKCORE_OPENMP_SUPPORT)
633 #pragma omp parallel for schedule(dynamic,4)
635 for (x=0; x < (ssize_t) p->width; x++)
644 for (y=0; y < (ssize_t) (p->height-1); y++)
649 delta=GetRadonCell(p,x,y)-(ssize_t) GetRadonCell(p,x,y+1);
652 projection[p->width+sign*x-1]=sum;
656 static MagickBooleanType RadonTransform(const Image *image,
657 const double threshold,size_t *projection,ExceptionInfo *exception)
685 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
686 source_cells=AcquireRadonInfo(image,width,image->rows,exception);
687 destination_cells=AcquireRadonInfo(image,width,image->rows,exception);
688 if ((source_cells == (RadonInfo *) NULL) ||
689 (destination_cells == (RadonInfo *) NULL))
691 if (destination_cells != (RadonInfo *) NULL)
692 destination_cells=DestroyRadonInfo(destination_cells);
693 if (source_cells != (RadonInfo *) NULL)
694 source_cells=DestroyRadonInfo(source_cells);
697 if (ResetRadonCells(source_cells) == MagickFalse)
699 destination_cells=DestroyRadonInfo(destination_cells);
700 source_cells=DestroyRadonInfo(source_cells);
703 for (i=0; i < 256; i++)
705 byte=(unsigned char) i;
706 for (count=0; byte != 0; byte>>=1)
708 bits[i]=(unsigned short) count;
711 image_view=AcquireCacheView(image);
712 #if defined(MAGICKCORE_OPENMP_SUPPORT)
713 #pragma omp parallel for schedule(dynamic,4) shared(status)
715 for (y=0; y < (ssize_t) image->rows; y++)
717 register const Quantum
728 if (status == MagickFalse)
730 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
731 if (p == (const Quantum *) NULL)
738 i=(ssize_t) (image->columns+7)/8;
739 for (x=0; x < (ssize_t) image->columns; x++)
742 if (((MagickRealType) GetPixelRed(image,p) < threshold) ||
743 ((MagickRealType) GetPixelGreen(image,p) < threshold) ||
744 ((MagickRealType) GetPixelBlue(image,p) < threshold))
749 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
753 p+=GetPixelChannels(image);
758 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
761 RadonProjection(source_cells,destination_cells,-1,projection);
762 (void) ResetRadonCells(source_cells);
763 #if defined(MAGICKCORE_OPENMP_SUPPORT)
764 #pragma omp parallel for schedule(dynamic,4) shared(status)
766 for (y=0; y < (ssize_t) image->rows; y++)
768 register const Quantum
779 if (status == MagickFalse)
781 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
782 if (p == (const Quantum *) NULL)
790 for (x=0; x < (ssize_t) image->columns; x++)
793 if (((MagickRealType) GetPixelRed(image,p) < threshold) ||
794 ((MagickRealType) GetPixelGreen(image,p) < threshold) ||
795 ((MagickRealType) GetPixelBlue(image,p) < threshold))
800 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
804 p+=GetPixelChannels(image);
809 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
812 RadonProjection(source_cells,destination_cells,1,projection);
813 image_view=DestroyCacheView(image_view);
814 destination_cells=DestroyRadonInfo(destination_cells);
815 source_cells=DestroyRadonInfo(source_cells);
819 static void GetImageBackgroundColor(Image *image,const ssize_t offset,
820 ExceptionInfo *exception)
835 Compute average background color.
839 GetPixelInfo(image,&background);
841 image_view=AcquireCacheView(image);
842 for (y=0; y < (ssize_t) image->rows; y++)
844 register const Quantum
850 if ((y >= offset) && (y < ((ssize_t) image->rows-offset)))
852 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
853 if (p == (const Quantum *) NULL)
855 for (x=0; x < (ssize_t) image->columns; x++)
857 if ((x >= offset) && (x < ((ssize_t) image->columns-offset)))
859 background.red+=QuantumScale*GetPixelRed(image,p);
860 background.green+=QuantumScale*GetPixelGreen(image,p);
861 background.blue+=QuantumScale*GetPixelBlue(image,p);
862 background.alpha+=QuantumScale*GetPixelAlpha(image,p);
864 p+=GetPixelChannels(image);
867 image_view=DestroyCacheView(image_view);
868 image->background_color.red=ClampToQuantum((MagickRealType) QuantumRange*
869 background.red/count);
870 image->background_color.green=ClampToQuantum((MagickRealType) QuantumRange*
871 background.green/count);
872 image->background_color.blue=ClampToQuantum((MagickRealType) QuantumRange*
873 background.blue/count);
874 image->background_color.alpha=ClampToQuantum((MagickRealType) QuantumRange*
875 background.alpha/count);
878 MagickExport Image *DeskewImage(const Image *image,const double threshold,
879 ExceptionInfo *exception)
914 Compute deskew angle.
916 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
917 projection=(size_t *) AcquireQuantumMemory((size_t) (2*width-1),
918 sizeof(*projection));
919 if (projection == (size_t *) NULL)
920 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
921 status=RadonTransform(image,threshold,projection,exception);
922 if (status == MagickFalse)
924 projection=(size_t *) RelinquishMagickMemory(projection);
925 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
929 for (i=0; i < (ssize_t) (2*width-1); i++)
931 if (projection[i] > max_projection)
933 skew=i-(ssize_t) width+1;
934 max_projection=projection[i];
937 projection=(size_t *) RelinquishMagickMemory(projection);
941 clone_image=CloneImage(image,0,0,MagickTrue,exception);
942 if (clone_image == (Image *) NULL)
943 return((Image *) NULL);
944 (void) SetImageVirtualPixelMethod(clone_image,BackgroundVirtualPixelMethod);
945 degrees=RadiansToDegrees(-atan((double) skew/width/8));
946 if (image->debug != MagickFalse)
947 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
948 " Deskew angle: %g",degrees);
949 affine_matrix.sx=cos(DegreesToRadians(fmod((double) degrees,360.0)));
950 affine_matrix.rx=sin(DegreesToRadians(fmod((double) degrees,360.0)));
951 affine_matrix.ry=(-sin(DegreesToRadians(fmod((double) degrees,360.0))));
952 affine_matrix.sy=cos(DegreesToRadians(fmod((double) degrees,360.0)));
953 affine_matrix.tx=0.0;
954 affine_matrix.ty=0.0;
955 artifact=GetImageArtifact(image,"deskew:auto-crop");
956 if (artifact == (const char *) NULL)
958 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
959 clone_image=DestroyImage(clone_image);
960 return(deskew_image);
965 GetImageBackgroundColor(clone_image,(ssize_t) StringToLong(artifact),
967 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
968 clone_image=DestroyImage(clone_image);
969 if (deskew_image == (Image *) NULL)
970 return((Image *) NULL);
971 median_image=StatisticImage(deskew_image,MedianStatistic,3,3,exception);
972 if (median_image == (Image *) NULL)
974 deskew_image=DestroyImage(deskew_image);
975 return((Image *) NULL);
977 geometry=GetImageBoundingBox(median_image,exception);
978 median_image=DestroyImage(median_image);
979 if (image->debug != MagickFalse)
980 (void) LogMagickEvent(TransformEvent,GetMagickModule()," Deskew geometry: "
981 "%.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
982 geometry.height,(double) geometry.x,(double) geometry.y);
983 crop_image=CropImage(deskew_image,&geometry,exception);
984 deskew_image=DestroyImage(deskew_image);
989 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
993 + I n t e g r a l R o t a t e I m a g e %
997 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
999 % IntegralRotateImage() rotates the image an integral of 90 degrees. It
1000 % allocates the memory necessary for the new Image structure and returns a
1001 % pointer to the rotated image.
1003 % The format of the IntegralRotateImage method is:
1005 % Image *IntegralRotateImage(const Image *image,size_t rotations,
1006 % ExceptionInfo *exception)
1008 % A description of each parameter follows.
1010 % o image: the image.
1012 % o rotations: Specifies the number of 90 degree rotations.
1015 static Image *IntegralRotateImage(const Image *image,size_t rotations,
1016 ExceptionInfo *exception)
1018 #define RotateImageTag "Rotate/Image"
1040 Initialize rotated image attributes.
1042 assert(image != (Image *) NULL);
1046 return(CloneImage(image,0,0,MagickTrue,exception));
1047 if ((rotations == 1) || (rotations == 3))
1048 rotate_image=CloneImage(image,image->rows,image->columns,MagickTrue,
1051 rotate_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1053 if (rotate_image == (Image *) NULL)
1054 return((Image *) NULL);
1056 Integral rotate the image.
1060 image_view=AcquireCacheView(image);
1061 rotate_view=AcquireCacheView(rotate_image);
1083 GetPixelCacheTileSize(image,&tile_width,&tile_height);
1084 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1085 #pragma omp parallel for schedule(static,1) shared(progress, status) omp_throttle(1)
1087 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
1092 if (status == MagickFalse)
1094 for (tile_x=0; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
1099 register const Quantum
1113 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
1114 width=(size_t) (tile_width-(tile_x+tile_width-
1117 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
1118 height=(size_t) (tile_height-(tile_y+tile_height-
1120 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1122 if (p == (const Quantum *) NULL)
1127 for (y=0; y < (ssize_t) width; y++)
1129 register const Quantum
1130 *restrict tile_pixels;
1135 q=QueueCacheViewAuthenticPixels(rotate_view,(ssize_t)
1136 (rotate_image->columns-(tile_y+height)),y+tile_x,height,
1138 if (q == (const Quantum *) NULL)
1143 tile_pixels=p+((height-1)*width+y)*GetPixelChannels(image);
1144 for (x=0; x < (ssize_t) height; x++)
1146 SetPixelRed(rotate_image,GetPixelRed(image,tile_pixels),q);
1147 SetPixelGreen(rotate_image,GetPixelGreen(image,tile_pixels),q);
1148 SetPixelBlue(rotate_image,GetPixelBlue(image,tile_pixels),q);
1149 SetPixelAlpha(rotate_image,GetPixelAlpha(image,tile_pixels),q);
1150 tile_pixels-=width*GetPixelChannels(image);
1151 q+=GetPixelChannels(rotate_image);
1153 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1154 if (sync == MagickFalse)
1158 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1163 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
1165 if (proceed == MagickFalse)
1169 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1170 image->rows-1,image->rows);
1171 Swap(page.width,page.height);
1172 Swap(page.x,page.y);
1173 if (page.width != 0)
1174 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1182 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1183 #pragma omp parallel for schedule(static,8) shared(progress,status) omp_throttle(1)
1185 for (y=0; y < (ssize_t) image->rows; y++)
1190 register const Quantum
1199 if (status == MagickFalse)
1201 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,
1203 q=QueueCacheViewAuthenticPixels(rotate_view,0,(ssize_t) (image->rows-
1204 y-1),image->columns,1,exception);
1205 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1210 q+=GetPixelChannels(rotate_image)*image->columns;
1211 for (x=0; x < (ssize_t) image->columns; x++)
1213 q-=GetPixelChannels(rotate_image);
1214 SetPixelRed(rotate_image,GetPixelRed(image,p),q);
1215 SetPixelGreen(rotate_image,GetPixelGreen(image,p),q);
1216 SetPixelBlue(rotate_image,GetPixelBlue(image,p),q);
1217 SetPixelAlpha(rotate_image,GetPixelAlpha(image,p),q);
1218 p+=GetPixelChannels(image);
1220 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1221 if (sync == MagickFalse)
1223 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1228 proceed=SetImageProgress(image,RotateImageTag,progress++,
1230 if (proceed == MagickFalse)
1234 if (page.width != 0)
1235 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1236 if (page.height != 0)
1237 page.y=(ssize_t) (page.height-rotate_image->rows-page.y);
1252 GetPixelCacheTileSize(image,&tile_width,&tile_height);
1253 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1254 #pragma omp parallel for schedule(static,1) shared(progress,status) omp_throttle(1)
1256 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
1261 if (status == MagickFalse)
1263 for (tile_x=0; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
1268 register const Quantum
1282 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
1283 width=(size_t) (tile_width-(tile_x+tile_width-
1286 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
1287 height=(size_t) (tile_height-(tile_y+tile_height-
1289 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,
1291 if (p == (const Quantum *) NULL)
1296 for (y=0; y < (ssize_t) width; y++)
1298 register const Quantum
1299 *restrict tile_pixels;
1304 q=QueueCacheViewAuthenticPixels(rotate_view,tile_y,(ssize_t)
1305 (y+rotate_image->rows-(tile_x+width)),height,1,exception);
1306 if (q == (const Quantum *) NULL)
1311 tile_pixels=p+((width-1)-y)*GetPixelChannels(image);
1312 for (x=0; x < (ssize_t) height; x++)
1314 SetPixelRed(rotate_image,GetPixelRed(image,tile_pixels),q);
1315 SetPixelGreen(rotate_image,GetPixelGreen(image,tile_pixels),q);
1316 SetPixelBlue(rotate_image,GetPixelBlue(image,tile_pixels),q);
1317 SetPixelAlpha(rotate_image,GetPixelAlpha(image,tile_pixels),q);
1318 tile_pixels+=width*GetPixelChannels(image);
1319 q+=GetPixelChannels(rotate_image);
1321 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1322 if (sync == MagickFalse)
1326 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1331 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
1333 if (proceed == MagickFalse)
1337 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1338 image->rows-1,image->rows);
1339 Swap(page.width,page.height);
1340 Swap(page.x,page.y);
1341 if (page.height != 0)
1342 page.y=(ssize_t) (page.height-rotate_image->rows-page.y);
1346 rotate_view=DestroyCacheView(rotate_view);
1347 image_view=DestroyCacheView(image_view);
1348 rotate_image->type=image->type;
1349 rotate_image->page=page;
1350 if (status == MagickFalse)
1351 rotate_image=DestroyImage(rotate_image);
1352 return(rotate_image);
1356 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1360 + X S h e a r I m a g e %
1364 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1366 % XShearImage() shears the image in the X direction with a shear angle of
1367 % 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1368 % negative angles shear clockwise. Angles are measured relative to a vertical
1369 % Y-axis. X shears will widen an image creating 'empty' triangles on the left
1370 % and right sides of the source image.
1372 % The format of the XShearImage method is:
1374 % MagickBooleanType XShearImage(Image *image,const MagickRealType degrees,
1375 % const size_t width,const size_t height,
1376 % const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
1378 % A description of each parameter follows.
1380 % o image: the image.
1382 % o degrees: A MagickRealType representing the shearing angle along the X
1385 % o width, height, x_offset, y_offset: Defines a region of the image
1388 % o exception: return any errors or warnings in this structure.
1391 static MagickBooleanType XShearImage(Image *image,const MagickRealType degrees,
1392 const size_t width,const size_t height,const ssize_t x_offset,
1393 const ssize_t y_offset,ExceptionInfo *exception)
1395 #define XShearImageTag "XShear/Image"
1418 assert(image != (Image *) NULL);
1419 assert(image->signature == MagickSignature);
1420 if (image->debug != MagickFalse)
1421 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1422 GetPixelInfo(image,&background);
1423 SetPixelInfoPacket(image,&image->background_color,&background);
1424 if (image->colorspace == CMYKColorspace)
1425 ConvertRGBToCMYK(&background);
1431 image_view=AcquireCacheView(image);
1432 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1433 #pragma omp parallel for schedule(dynamic,4) shared(progress, status)
1435 for (y=0; y < (ssize_t) height; y++)
1459 if (status == MagickFalse)
1461 p=GetCacheViewAuthenticPixels(image_view,0,y_offset+y,image->columns,1,
1463 if (p == (Quantum *) NULL)
1468 p+=x_offset*GetPixelChannels(image);
1469 displacement=degrees*(MagickRealType) (y-height/2.0);
1470 if (displacement == 0.0)
1472 if (displacement > 0.0)
1476 displacement*=(-1.0);
1479 step=(ssize_t) floor((double) displacement);
1480 area=(MagickRealType) (displacement-step);
1483 GetPixelInfo(image,&source);
1484 GetPixelInfo(image,&destination);
1490 Transfer pixels left-to-right.
1492 if (step > x_offset)
1494 q=p-step*GetPixelChannels(image);
1495 for (i=0; i < (ssize_t) width; i++)
1497 if ((x_offset+i) < step)
1499 p+=GetPixelChannels(image);
1500 SetPixelInfo(image,p,&pixel);
1501 q+=GetPixelChannels(image);
1504 SetPixelInfo(image,p,&source);
1505 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1506 &source,(MagickRealType) GetPixelAlpha(image,p),area,
1508 SetPixelPixelInfo(image,&destination,q);
1509 SetPixelInfo(image,p,&pixel);
1510 p+=GetPixelChannels(image);
1511 q+=GetPixelChannels(image);
1513 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1514 &background,(MagickRealType) background.alpha,area,&destination);
1515 SetPixelPixelInfo(image,&destination,q);
1516 q+=GetPixelChannels(image);
1517 for (i=0; i < (step-1); i++)
1519 SetPixelPixelInfo(image,&background,q);
1520 q+=GetPixelChannels(image);
1527 Transfer pixels right-to-left.
1529 p+=width*GetPixelChannels(image);
1530 q=p+step*GetPixelChannels(image);
1531 for (i=0; i < (ssize_t) width; i++)
1533 p-=GetPixelChannels(image);
1534 q-=GetPixelChannels(image);
1535 if ((size_t) (x_offset+width+step-i) >= image->columns)
1537 SetPixelInfo(image,p,&source);
1538 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1539 &source,(MagickRealType) GetPixelAlpha(image,p),area,
1541 SetPixelPixelInfo(image,&destination,q);
1542 SetPixelInfo(image,p,&pixel);
1544 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1545 &background,(MagickRealType) background.alpha,area,&destination);
1546 q-=GetPixelChannels(image);
1547 SetPixelPixelInfo(image,&destination,q);
1548 for (i=0; i < (step-1); i++)
1550 q-=GetPixelChannels(image);
1551 SetPixelPixelInfo(image,&background,q);
1556 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1558 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1563 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1564 #pragma omp critical (MagickCore_XShearImage)
1566 proceed=SetImageProgress(image,XShearImageTag,progress++,height);
1567 if (proceed == MagickFalse)
1571 image_view=DestroyCacheView(image_view);
1576 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1580 + Y S h e a r I m a g e %
1584 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1586 % YShearImage shears the image in the Y direction with a shear angle of
1587 % 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1588 % negative angles shear clockwise. Angles are measured relative to a
1589 % horizontal X-axis. Y shears will increase the height of an image creating
1590 % 'empty' triangles on the top and bottom of the source image.
1592 % The format of the YShearImage method is:
1594 % MagickBooleanType YShearImage(Image *image,const MagickRealType degrees,
1595 % const size_t width,const size_t height,
1596 % const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
1598 % A description of each parameter follows.
1600 % o image: the image.
1602 % o degrees: A MagickRealType representing the shearing angle along the Y
1605 % o width, height, x_offset, y_offset: Defines a region of the image
1608 % o exception: return any errors or warnings in this structure.
1611 static MagickBooleanType YShearImage(Image *image,const MagickRealType degrees,
1612 const size_t width,const size_t height,const ssize_t x_offset,
1613 const ssize_t y_offset,ExceptionInfo *exception)
1615 #define YShearImageTag "YShear/Image"
1638 assert(image != (Image *) NULL);
1639 assert(image->signature == MagickSignature);
1640 if (image->debug != MagickFalse)
1641 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1642 GetPixelInfo(image,&background);
1643 SetPixelInfoPacket(image,&image->background_color,&background);
1644 if (image->colorspace == CMYKColorspace)
1645 ConvertRGBToCMYK(&background);
1651 image_view=AcquireCacheView(image);
1652 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1653 #pragma omp parallel for schedule(dynamic,4) shared(progress, status)
1655 for (x=0; x < (ssize_t) width; x++)
1679 if (status == MagickFalse)
1681 p=GetCacheViewAuthenticPixels(image_view,x_offset+x,0,1,image->rows,
1683 if (p == (Quantum *) NULL)
1688 p+=y_offset*GetPixelChannels(image);
1689 displacement=degrees*(MagickRealType) (x-width/2.0);
1690 if (displacement == 0.0)
1692 if (displacement > 0.0)
1696 displacement*=(-1.0);
1699 step=(ssize_t) floor((double) displacement);
1700 area=(MagickRealType) (displacement-step);
1703 GetPixelInfo(image,&source);
1704 GetPixelInfo(image,&destination);
1710 Transfer pixels top-to-bottom.
1712 if (step > y_offset)
1714 q=p-step*GetPixelChannels(image);
1715 for (i=0; i < (ssize_t) height; i++)
1717 if ((y_offset+i) < step)
1719 p+=GetPixelChannels(image);
1720 SetPixelInfo(image,p,&pixel);
1721 q+=GetPixelChannels(image);
1724 SetPixelInfo(image,p,&source);
1725 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1726 &source,(MagickRealType) GetPixelAlpha(image,p),area,
1728 SetPixelPixelInfo(image,&destination,q);
1729 SetPixelInfo(image,p,&pixel);
1730 p+=GetPixelChannels(image);
1731 q+=GetPixelChannels(image);
1733 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1734 &background,(MagickRealType) background.alpha,area,&destination);
1735 SetPixelPixelInfo(image,&destination,q);
1736 q+=GetPixelChannels(image);
1737 for (i=0; i < (step-1); i++)
1739 SetPixelPixelInfo(image,&background,q);
1740 q+=GetPixelChannels(image);
1747 Transfer pixels bottom-to-top.
1749 p+=height*GetPixelChannels(image);
1750 q=p+step*GetPixelChannels(image);
1751 for (i=0; i < (ssize_t) height; i++)
1753 p-=GetPixelChannels(image);
1754 q-=GetPixelChannels(image);
1755 if ((size_t) (y_offset+height+step-i) >= image->rows)
1757 SetPixelInfo(image,p,&source);
1758 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1759 &source,(MagickRealType) GetPixelAlpha(image,p),area,
1761 SetPixelPixelInfo(image,&destination,q);
1762 SetPixelInfo(image,p,&pixel);
1764 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1765 &background,(MagickRealType) background.alpha,area,&destination);
1766 q-=GetPixelChannels(image);
1767 SetPixelPixelInfo(image,&destination,q);
1768 for (i=0; i < (step-1); i++)
1770 q-=GetPixelChannels(image);
1771 SetPixelPixelInfo(image,&background,q);
1776 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1778 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1783 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1784 #pragma omp critical (MagickCore_YShearImage)
1786 proceed=SetImageProgress(image,YShearImageTag,progress++,image->rows);
1787 if (proceed == MagickFalse)
1791 image_view=DestroyCacheView(image_view);
1796 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1800 % R o t a t e I m a g e %
1804 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1806 % RotateImage() creates a new image that is a rotated copy of an existing
1807 % one. Positive angles rotate counter-clockwise (right-hand rule), while
1808 % negative angles rotate clockwise. Rotated images are usually larger than
1809 % the originals and have 'empty' triangular corners. X axis. Empty
1810 % triangles left over from shearing the image are filled with the background
1811 % color defined by member 'background_color' of the image. RotateImage
1812 % allocates the memory necessary for the new Image structure and returns a
1813 % pointer to the new image.
1815 % RotateImage() is based on the paper "A Fast Algorithm for General
1816 % Raster Rotatation" by Alan W. Paeth. RotateImage is adapted from a similar
1817 % method based on the Paeth paper written by Michael Halle of the Spatial
1818 % Imaging Group, MIT Media Lab.
1820 % The format of the RotateImage method is:
1822 % Image *RotateImage(const Image *image,const double degrees,
1823 % ExceptionInfo *exception)
1825 % A description of each parameter follows.
1827 % o image: the image.
1829 % o degrees: Specifies the number of degrees to rotate the image.
1831 % o exception: return any errors or warnings in this structure.
1834 MagickExport Image *RotateImage(const Image *image,const double degrees,
1835 ExceptionInfo *exception)
1864 Adjust rotation angle.
1866 assert(image != (Image *) NULL);
1867 assert(image->signature == MagickSignature);
1868 if (image->debug != MagickFalse)
1869 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1870 assert(exception != (ExceptionInfo *) NULL);
1871 assert(exception->signature == MagickSignature);
1873 while (angle < -45.0)
1875 for (rotations=0; angle > 45.0; rotations++)
1879 Calculate shear equations.
1881 integral_image=IntegralRotateImage(image,rotations,exception);
1882 if (integral_image == (Image *) NULL)
1883 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1884 shear.x=(-tan((double) DegreesToRadians(angle)/2.0));
1885 shear.y=sin((double) DegreesToRadians(angle));
1886 if ((shear.x == 0.0) && (shear.y == 0.0))
1887 return(integral_image);
1888 if (SetImageStorageClass(integral_image,DirectClass) == MagickFalse)
1890 InheritException(exception,&integral_image->exception);
1891 integral_image=DestroyImage(integral_image);
1892 return(integral_image);
1894 if (integral_image->matte == MagickFalse)
1895 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel);
1899 width=image->columns;
1901 if ((rotations == 1) || (rotations == 3))
1904 height=image->columns;
1906 y_width=width+(ssize_t) floor(fabs(shear.x)*height+0.5);
1907 x_offset=(ssize_t) ceil((double) width+((fabs(shear.y)*height)-width)/2.0-
1909 y_offset=(ssize_t) ceil((double) height+((fabs(shear.y)*y_width)-height)/2.0-
1912 Surround image with a border.
1914 integral_image->border_color=integral_image->background_color;
1915 integral_image->compose=CopyCompositeOp;
1916 border_info.width=(size_t) x_offset;
1917 border_info.height=(size_t) y_offset;
1918 rotate_image=BorderImage(integral_image,&border_info,exception);
1919 integral_image=DestroyImage(integral_image);
1920 if (rotate_image == (Image *) NULL)
1921 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1925 status=XShearImage(rotate_image,shear.x,width,height,x_offset,(ssize_t)
1926 (rotate_image->rows-height)/2,exception);
1927 if (status == MagickFalse)
1929 rotate_image=DestroyImage(rotate_image);
1930 return((Image *) NULL);
1932 status=YShearImage(rotate_image,shear.y,y_width,height,(ssize_t)
1933 (rotate_image->columns-y_width)/2,y_offset,exception);
1934 if (status == MagickFalse)
1936 rotate_image=DestroyImage(rotate_image);
1937 return((Image *) NULL);
1939 status=XShearImage(rotate_image,shear.x,y_width,rotate_image->rows,(ssize_t)
1940 (rotate_image->columns-y_width)/2,0,exception);
1941 if (status == MagickFalse)
1943 rotate_image=DestroyImage(rotate_image);
1944 return((Image *) NULL);
1946 status=CropToFitImage(&rotate_image,shear.x,shear.y,(MagickRealType) width,
1947 (MagickRealType) height,MagickTrue,exception);
1948 if (status == MagickFalse)
1950 rotate_image=DestroyImage(rotate_image);
1951 return((Image *) NULL);
1953 rotate_image->compose=image->compose;
1954 rotate_image->page.width=0;
1955 rotate_image->page.height=0;
1956 return(rotate_image);
1960 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1964 % S h e a r I m a g e %
1968 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1970 % ShearImage() creates a new image that is a shear_image copy of an existing
1971 % one. Shearing slides one edge of an image along the X or Y axis, creating
1972 % a parallelogram. An X direction shear slides an edge along the X axis,
1973 % while a Y direction shear slides an edge along the Y axis. The amount of
1974 % the shear is controlled by a shear angle. For X direction shears, x_shear
1975 % is measured relative to the Y axis, and similarly, for Y direction shears
1976 % y_shear is measured relative to the X axis. Empty triangles left over from
1977 % shearing the image are filled with the background color defined by member
1978 % 'background_color' of the image.. ShearImage() allocates the memory
1979 % necessary for the new Image structure and returns a pointer to the new image.
1981 % ShearImage() is based on the paper "A Fast Algorithm for General Raster
1982 % Rotatation" by Alan W. Paeth.
1984 % The format of the ShearImage method is:
1986 % Image *ShearImage(const Image *image,const double x_shear,
1987 % const double y_shear,ExceptionInfo *exception)
1989 % A description of each parameter follows.
1991 % o image: the image.
1993 % o x_shear, y_shear: Specifies the number of degrees to shear the image.
1995 % o exception: return any errors or warnings in this structure.
1998 MagickExport Image *ShearImage(const Image *image,const double x_shear,
1999 const double y_shear,ExceptionInfo *exception)
2021 assert(image != (Image *) NULL);
2022 assert(image->signature == MagickSignature);
2023 if (image->debug != MagickFalse)
2024 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2025 assert(exception != (ExceptionInfo *) NULL);
2026 assert(exception->signature == MagickSignature);
2027 if ((x_shear != 0.0) && (fmod(x_shear,90.0) == 0.0))
2028 ThrowImageException(ImageError,"AngleIsDiscontinuous");
2029 if ((y_shear != 0.0) && (fmod(y_shear,90.0) == 0.0))
2030 ThrowImageException(ImageError,"AngleIsDiscontinuous");
2032 Initialize shear angle.
2034 integral_image=CloneImage(image,0,0,MagickTrue,exception);
2035 if (integral_image == (Image *) NULL)
2036 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2037 shear.x=(-tan(DegreesToRadians(fmod(x_shear,360.0))));
2038 shear.y=tan(DegreesToRadians(fmod(y_shear,360.0)));
2039 if ((shear.x == 0.0) && (shear.y == 0.0))
2040 return(integral_image);
2041 if (SetImageStorageClass(integral_image,DirectClass) == MagickFalse)
2043 InheritException(exception,&integral_image->exception);
2044 integral_image=DestroyImage(integral_image);
2045 return(integral_image);
2047 if (integral_image->matte == MagickFalse)
2048 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel);
2052 y_width=image->columns+(ssize_t) floor(fabs(shear.x)*image->rows+0.5);
2053 x_offset=(ssize_t) ceil((double) image->columns+((fabs(shear.x)*image->rows)-
2054 image->columns)/2.0-0.5);
2055 y_offset=(ssize_t) ceil((double) image->rows+((fabs(shear.y)*y_width)-
2056 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=(size_t) x_offset;
2063 border_info.height=(size_t) y_offset;
2064 shear_image=BorderImage(integral_image,&border_info,exception);
2065 integral_image=DestroyImage(integral_image);
2066 if (shear_image == (Image *) NULL)
2067 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2071 if (shear_image->matte == MagickFalse)
2072 (void) SetImageAlphaChannel(shear_image,OpaqueAlphaChannel);
2073 status=XShearImage(shear_image,shear.x,image->columns,image->rows,x_offset,
2074 (ssize_t) (shear_image->rows-image->rows)/2,exception);
2075 if (status == MagickFalse)
2077 shear_image=DestroyImage(shear_image);
2078 return((Image *) NULL);
2080 status=YShearImage(shear_image,shear.y,y_width,image->rows,(ssize_t)
2081 (shear_image->columns-y_width)/2,y_offset,exception);
2082 if (status == MagickFalse)
2084 shear_image=DestroyImage(shear_image);
2085 return((Image *) NULL);
2087 status=CropToFitImage(&shear_image,shear.x,shear.y,(MagickRealType)
2088 image->columns,(MagickRealType) image->rows,MagickFalse,exception);
2089 if (status == MagickFalse)
2091 shear_image=DestroyImage(shear_image);
2092 return((Image *) NULL);
2094 shear_image->compose=image->compose;
2095 shear_image->page.width=0;
2096 shear_image->page.height=0;
2097 return(shear_image);