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 (GetPixelIntensity(image,p) < threshold)
747 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
751 p+=GetPixelChannels(image);
756 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
759 RadonProjection(source_cells,destination_cells,-1,projection);
760 (void) ResetRadonCells(source_cells);
761 #if defined(MAGICKCORE_OPENMP_SUPPORT)
762 #pragma omp parallel for schedule(dynamic,4) shared(status)
764 for (y=0; y < (ssize_t) image->rows; y++)
766 register const Quantum
777 if (status == MagickFalse)
779 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
780 if (p == (const Quantum *) NULL)
788 for (x=0; x < (ssize_t) image->columns; x++)
791 if (GetPixelIntensity(image,p) < threshold)
796 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
800 p+=GetPixelChannels(image);
805 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
808 RadonProjection(source_cells,destination_cells,1,projection);
809 image_view=DestroyCacheView(image_view);
810 destination_cells=DestroyRadonInfo(destination_cells);
811 source_cells=DestroyRadonInfo(source_cells);
815 static void GetImageBackgroundColor(Image *image,const ssize_t offset,
816 ExceptionInfo *exception)
831 Compute average background color.
835 GetPixelInfo(image,&background);
837 image_view=AcquireCacheView(image);
838 for (y=0; y < (ssize_t) image->rows; y++)
840 register const Quantum
846 if ((y >= offset) && (y < ((ssize_t) image->rows-offset)))
848 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
849 if (p == (const Quantum *) NULL)
851 for (x=0; x < (ssize_t) image->columns; x++)
853 if ((x >= offset) && (x < ((ssize_t) image->columns-offset)))
855 background.red+=QuantumScale*GetPixelRed(image,p);
856 background.green+=QuantumScale*GetPixelGreen(image,p);
857 background.blue+=QuantumScale*GetPixelBlue(image,p);
858 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
859 background.alpha+=QuantumScale*GetPixelAlpha(image,p);
861 p+=GetPixelChannels(image);
864 image_view=DestroyCacheView(image_view);
865 image->background_color.red=ClampToQuantum((MagickRealType) QuantumRange*
866 background.red/count);
867 image->background_color.green=ClampToQuantum((MagickRealType) QuantumRange*
868 background.green/count);
869 image->background_color.blue=ClampToQuantum((MagickRealType) QuantumRange*
870 background.blue/count);
871 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
872 image->background_color.alpha=ClampToQuantum((MagickRealType) QuantumRange*
873 background.alpha/count);
876 MagickExport Image *DeskewImage(const Image *image,const double threshold,
877 ExceptionInfo *exception)
912 Compute deskew angle.
914 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
915 projection=(size_t *) AcquireQuantumMemory((size_t) (2*width-1),
916 sizeof(*projection));
917 if (projection == (size_t *) NULL)
918 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
919 status=RadonTransform(image,threshold,projection,exception);
920 if (status == MagickFalse)
922 projection=(size_t *) RelinquishMagickMemory(projection);
923 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
927 for (i=0; i < (ssize_t) (2*width-1); i++)
929 if (projection[i] > max_projection)
931 skew=i-(ssize_t) width+1;
932 max_projection=projection[i];
935 projection=(size_t *) RelinquishMagickMemory(projection);
939 clone_image=CloneImage(image,0,0,MagickTrue,exception);
940 if (clone_image == (Image *) NULL)
941 return((Image *) NULL);
942 (void) SetImageVirtualPixelMethod(clone_image,BackgroundVirtualPixelMethod);
943 degrees=RadiansToDegrees(-atan((double) skew/width/8));
944 if (image->debug != MagickFalse)
945 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
946 " Deskew angle: %g",degrees);
947 affine_matrix.sx=cos(DegreesToRadians(fmod((double) degrees,360.0)));
948 affine_matrix.rx=sin(DegreesToRadians(fmod((double) degrees,360.0)));
949 affine_matrix.ry=(-sin(DegreesToRadians(fmod((double) degrees,360.0))));
950 affine_matrix.sy=cos(DegreesToRadians(fmod((double) degrees,360.0)));
951 affine_matrix.tx=0.0;
952 affine_matrix.ty=0.0;
953 artifact=GetImageArtifact(image,"deskew:auto-crop");
954 if (artifact == (const char *) NULL)
956 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
957 clone_image=DestroyImage(clone_image);
958 return(deskew_image);
963 GetImageBackgroundColor(clone_image,(ssize_t) StringToLong(artifact),
965 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
966 clone_image=DestroyImage(clone_image);
967 if (deskew_image == (Image *) NULL)
968 return((Image *) NULL);
969 median_image=StatisticImage(deskew_image,MedianStatistic,3,3,exception);
970 if (median_image == (Image *) NULL)
972 deskew_image=DestroyImage(deskew_image);
973 return((Image *) NULL);
975 geometry=GetImageBoundingBox(median_image,exception);
976 median_image=DestroyImage(median_image);
977 if (image->debug != MagickFalse)
978 (void) LogMagickEvent(TransformEvent,GetMagickModule()," Deskew geometry: "
979 "%.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
980 geometry.height,(double) geometry.x,(double) geometry.y);
981 crop_image=CropImage(deskew_image,&geometry,exception);
982 deskew_image=DestroyImage(deskew_image);
987 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
991 + I n t e g r a l R o t a t e I m a g e %
995 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
997 % IntegralRotateImage() rotates the image an integral of 90 degrees. It
998 % allocates the memory necessary for the new Image structure and returns a
999 % pointer to the rotated image.
1001 % The format of the IntegralRotateImage method is:
1003 % Image *IntegralRotateImage(const Image *image,size_t rotations,
1004 % ExceptionInfo *exception)
1006 % A description of each parameter follows.
1008 % o image: the image.
1010 % o rotations: Specifies the number of 90 degree rotations.
1013 static Image *IntegralRotateImage(const Image *image,size_t rotations,
1014 ExceptionInfo *exception)
1016 #define RotateImageTag "Rotate/Image"
1038 Initialize rotated image attributes.
1040 assert(image != (Image *) NULL);
1044 return(CloneImage(image,0,0,MagickTrue,exception));
1045 if ((rotations == 1) || (rotations == 3))
1046 rotate_image=CloneImage(image,image->rows,image->columns,MagickTrue,
1049 rotate_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1051 if (rotate_image == (Image *) NULL)
1052 return((Image *) NULL);
1054 Integral rotate the image.
1058 image_view=AcquireCacheView(image);
1059 rotate_view=AcquireCacheView(rotate_image);
1081 GetPixelCacheTileSize(image,&tile_width,&tile_height);
1082 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1083 #pragma omp parallel for schedule(static,1) shared(progress, status)
1085 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
1090 if (status == MagickFalse)
1093 for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
1098 register const Quantum
1112 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
1113 width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
1115 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
1116 height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
1117 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1119 if (p == (const Quantum *) NULL)
1124 for (y=0; y < (ssize_t) width; y++)
1126 register const Quantum
1127 *restrict tile_pixels;
1132 if (status == MagickFalse)
1134 q=QueueCacheViewAuthenticPixels(rotate_view,(ssize_t)
1135 (rotate_image->columns-(tile_y+height)),y+tile_x,height,1,
1137 if (q == (Quantum *) NULL)
1142 tile_pixels=p+((height-1)*width+y)*GetPixelChannels(image);
1143 for (x=0; x < (ssize_t) height; x++)
1148 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1157 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
1158 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
1159 rotate_traits=GetPixelChannelMapTraits(rotate_image,channel);
1160 if ((traits == UndefinedPixelTrait) ||
1161 (rotate_traits == UndefinedPixelTrait))
1163 SetPixelChannel(rotate_image,channel,tile_pixels[i],q);
1165 tile_pixels-=width*GetPixelChannels(image);
1166 q+=GetPixelChannels(rotate_image);
1168 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1169 if (sync == MagickFalse)
1173 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1178 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1179 #pragma omp critical (MagickCore_IntegralRotateImage)
1181 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
1183 if (proceed == MagickFalse)
1187 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1188 image->rows-1,image->rows);
1189 Swap(page.width,page.height);
1190 Swap(page.x,page.y);
1191 if (page.width != 0)
1192 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1200 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1201 #pragma omp parallel for schedule(static,8) shared(progress,status)
1203 for (y=0; y < (ssize_t) image->rows; y++)
1208 register const Quantum
1217 if (status == MagickFalse)
1219 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1220 q=QueueCacheViewAuthenticPixels(rotate_view,0,(ssize_t) (image->rows-y-
1221 1),image->columns,1,exception);
1222 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1227 q+=GetPixelChannels(rotate_image)*image->columns;
1228 for (x=0; x < (ssize_t) image->columns; x++)
1233 q-=GetPixelChannels(rotate_image);
1234 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1243 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
1244 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
1245 rotate_traits=GetPixelChannelMapTraits(rotate_image,channel);
1246 if ((traits == UndefinedPixelTrait) ||
1247 (rotate_traits == UndefinedPixelTrait))
1249 SetPixelChannel(rotate_image,channel,p[i],q);
1251 p+=GetPixelChannels(image);
1253 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1254 if (sync == MagickFalse)
1256 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1261 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1262 #pragma omp critical (MagickCore_IntegralRotateImage)
1264 proceed=SetImageProgress(image,RotateImageTag,progress++,
1266 if (proceed == MagickFalse)
1270 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1271 image->rows-1,image->rows);
1272 Swap(page.width,page.height);
1273 Swap(page.x,page.y);
1274 if (page.width != 0)
1275 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1290 GetPixelCacheTileSize(image,&tile_width,&tile_height);
1291 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1292 #pragma omp parallel for schedule(static,1) shared(progress,status)
1294 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
1299 if (status == MagickFalse)
1302 for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
1307 register const Quantum
1321 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
1322 width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
1324 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
1325 height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
1326 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1328 if (p == (const Quantum *) NULL)
1333 for (y=0; y < (ssize_t) width; y++)
1335 register const Quantum
1336 *restrict tile_pixels;
1341 if (status == MagickFalse)
1343 q=QueueCacheViewAuthenticPixels(rotate_view,tile_y,(ssize_t) (y+
1344 rotate_image->rows-(tile_x+width)),height,1,exception);
1345 if (q == (Quantum *) NULL)
1350 tile_pixels=p+((width-1)-y)*GetPixelChannels(image);
1351 for (x=0; x < (ssize_t) height; x++)
1356 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1365 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
1366 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
1367 rotate_traits=GetPixelChannelMapTraits(rotate_image,channel);
1368 if ((traits == UndefinedPixelTrait) ||
1369 (rotate_traits == UndefinedPixelTrait))
1371 SetPixelChannel(rotate_image,channel,tile_pixels[i],q);
1373 tile_pixels+=width*GetPixelChannels(image);
1374 q+=GetPixelChannels(rotate_image);
1376 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1377 if (sync == MagickFalse)
1381 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1386 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1387 #pragma omp critical (MagickCore_IntegralRotateImage)
1389 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
1391 if (proceed == MagickFalse)
1395 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1396 image->rows-1,image->rows);
1397 Swap(page.width,page.height);
1398 Swap(page.x,page.y);
1399 if (page.width != 0)
1400 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1404 rotate_view=DestroyCacheView(rotate_view);
1405 image_view=DestroyCacheView(image_view);
1406 rotate_image->type=image->type;
1407 rotate_image->page=page;
1408 if (status == MagickFalse)
1409 rotate_image=DestroyImage(rotate_image);
1410 return(rotate_image);
1414 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1418 + X S h e a r I m a g e %
1422 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1424 % XShearImage() shears the image in the X direction with a shear angle of
1425 % 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1426 % negative angles shear clockwise. Angles are measured relative to a vertical
1427 % Y-axis. X shears will widen an image creating 'empty' triangles on the left
1428 % and right sides of the source image.
1430 % The format of the XShearImage method is:
1432 % MagickBooleanType XShearImage(Image *image,const MagickRealType degrees,
1433 % const size_t width,const size_t height,
1434 % const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
1436 % A description of each parameter follows.
1438 % o image: the image.
1440 % o degrees: A MagickRealType representing the shearing angle along the X
1443 % o width, height, x_offset, y_offset: Defines a region of the image
1446 % o exception: return any errors or warnings in this structure.
1449 static MagickBooleanType XShearImage(Image *image,const MagickRealType degrees,
1450 const size_t width,const size_t height,const ssize_t x_offset,
1451 const ssize_t y_offset,ExceptionInfo *exception)
1453 #define XShearImageTag "XShear/Image"
1476 assert(image != (Image *) NULL);
1477 assert(image->signature == MagickSignature);
1478 if (image->debug != MagickFalse)
1479 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1480 GetPixelInfo(image,&background);
1481 SetPixelInfoPacket(image,&image->background_color,&background);
1482 if (image->colorspace == CMYKColorspace)
1483 ConvertRGBToCMYK(&background);
1489 image_view=AcquireCacheView(image);
1490 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1491 #pragma omp parallel for schedule(dynamic,4) shared(progress, status)
1493 for (y=0; y < (ssize_t) height; y++)
1517 if (status == MagickFalse)
1519 p=GetCacheViewAuthenticPixels(image_view,0,y_offset+y,image->columns,1,
1521 if (p == (Quantum *) NULL)
1526 p+=x_offset*GetPixelChannels(image);
1527 displacement=degrees*(MagickRealType) (y-height/2.0);
1528 if (displacement == 0.0)
1530 if (displacement > 0.0)
1534 displacement*=(-1.0);
1537 step=(ssize_t) floor((double) displacement);
1538 area=(MagickRealType) (displacement-step);
1541 GetPixelInfo(image,&source);
1542 GetPixelInfo(image,&destination);
1548 Transfer pixels left-to-right.
1550 if (step > x_offset)
1552 q=p-step*GetPixelChannels(image);
1553 for (i=0; i < (ssize_t) width; i++)
1555 if ((x_offset+i) < step)
1557 p+=GetPixelChannels(image);
1558 SetPixelInfo(image,p,&pixel);
1559 q+=GetPixelChannels(image);
1562 SetPixelInfo(image,p,&source);
1563 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1564 &source,(MagickRealType) GetPixelAlpha(image,p),area,
1566 SetPixelPixelInfo(image,&destination,q);
1567 SetPixelInfo(image,p,&pixel);
1568 p+=GetPixelChannels(image);
1569 q+=GetPixelChannels(image);
1571 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1572 &background,(MagickRealType) background.alpha,area,&destination);
1573 SetPixelPixelInfo(image,&destination,q);
1574 q+=GetPixelChannels(image);
1575 for (i=0; i < (step-1); i++)
1577 SetPixelPixelInfo(image,&background,q);
1578 q+=GetPixelChannels(image);
1585 Transfer pixels right-to-left.
1587 p+=width*GetPixelChannels(image);
1588 q=p+step*GetPixelChannels(image);
1589 for (i=0; i < (ssize_t) width; i++)
1591 p-=GetPixelChannels(image);
1592 q-=GetPixelChannels(image);
1593 if ((size_t) (x_offset+width+step-i) >= image->columns)
1595 SetPixelInfo(image,p,&source);
1596 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1597 &source,(MagickRealType) GetPixelAlpha(image,p),area,
1599 SetPixelPixelInfo(image,&destination,q);
1600 SetPixelInfo(image,p,&pixel);
1602 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1603 &background,(MagickRealType) background.alpha,area,&destination);
1604 q-=GetPixelChannels(image);
1605 SetPixelPixelInfo(image,&destination,q);
1606 for (i=0; i < (step-1); i++)
1608 q-=GetPixelChannels(image);
1609 SetPixelPixelInfo(image,&background,q);
1614 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1616 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1621 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1622 #pragma omp critical (MagickCore_XShearImage)
1624 proceed=SetImageProgress(image,XShearImageTag,progress++,height);
1625 if (proceed == MagickFalse)
1629 image_view=DestroyCacheView(image_view);
1634 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1638 + Y S h e a r I m a g e %
1642 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1644 % YShearImage shears the image in the Y direction with a shear angle of
1645 % 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1646 % negative angles shear clockwise. Angles are measured relative to a
1647 % horizontal X-axis. Y shears will increase the height of an image creating
1648 % 'empty' triangles on the top and bottom of the source image.
1650 % The format of the YShearImage method is:
1652 % MagickBooleanType YShearImage(Image *image,const MagickRealType degrees,
1653 % const size_t width,const size_t height,
1654 % const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
1656 % A description of each parameter follows.
1658 % o image: the image.
1660 % o degrees: A MagickRealType representing the shearing angle along the Y
1663 % o width, height, x_offset, y_offset: Defines a region of the image
1666 % o exception: return any errors or warnings in this structure.
1669 static MagickBooleanType YShearImage(Image *image,const MagickRealType degrees,
1670 const size_t width,const size_t height,const ssize_t x_offset,
1671 const ssize_t y_offset,ExceptionInfo *exception)
1673 #define YShearImageTag "YShear/Image"
1696 assert(image != (Image *) NULL);
1697 assert(image->signature == MagickSignature);
1698 if (image->debug != MagickFalse)
1699 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1700 GetPixelInfo(image,&background);
1701 SetPixelInfoPacket(image,&image->background_color,&background);
1702 if (image->colorspace == CMYKColorspace)
1703 ConvertRGBToCMYK(&background);
1709 image_view=AcquireCacheView(image);
1710 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1711 #pragma omp parallel for schedule(dynamic,4) shared(progress, status)
1713 for (x=0; x < (ssize_t) width; x++)
1737 if (status == MagickFalse)
1739 p=GetCacheViewAuthenticPixels(image_view,x_offset+x,0,1,image->rows,
1741 if (p == (Quantum *) NULL)
1746 p+=y_offset*GetPixelChannels(image);
1747 displacement=degrees*(MagickRealType) (x-width/2.0);
1748 if (displacement == 0.0)
1750 if (displacement > 0.0)
1754 displacement*=(-1.0);
1757 step=(ssize_t) floor((double) displacement);
1758 area=(MagickRealType) (displacement-step);
1761 GetPixelInfo(image,&source);
1762 GetPixelInfo(image,&destination);
1768 Transfer pixels top-to-bottom.
1770 if (step > y_offset)
1772 q=p-step*GetPixelChannels(image);
1773 for (i=0; i < (ssize_t) height; i++)
1775 if ((y_offset+i) < step)
1777 p+=GetPixelChannels(image);
1778 SetPixelInfo(image,p,&pixel);
1779 q+=GetPixelChannels(image);
1782 SetPixelInfo(image,p,&source);
1783 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1784 &source,(MagickRealType) GetPixelAlpha(image,p),area,
1786 SetPixelPixelInfo(image,&destination,q);
1787 SetPixelInfo(image,p,&pixel);
1788 p+=GetPixelChannels(image);
1789 q+=GetPixelChannels(image);
1791 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1792 &background,(MagickRealType) background.alpha,area,&destination);
1793 SetPixelPixelInfo(image,&destination,q);
1794 q+=GetPixelChannels(image);
1795 for (i=0; i < (step-1); i++)
1797 SetPixelPixelInfo(image,&background,q);
1798 q+=GetPixelChannels(image);
1805 Transfer pixels bottom-to-top.
1807 p+=height*GetPixelChannels(image);
1808 q=p+step*GetPixelChannels(image);
1809 for (i=0; i < (ssize_t) height; i++)
1811 p-=GetPixelChannels(image);
1812 q-=GetPixelChannels(image);
1813 if ((size_t) (y_offset+height+step-i) >= image->rows)
1815 SetPixelInfo(image,p,&source);
1816 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1817 &source,(MagickRealType) GetPixelAlpha(image,p),area,
1819 SetPixelPixelInfo(image,&destination,q);
1820 SetPixelInfo(image,p,&pixel);
1822 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1823 &background,(MagickRealType) background.alpha,area,&destination);
1824 q-=GetPixelChannels(image);
1825 SetPixelPixelInfo(image,&destination,q);
1826 for (i=0; i < (step-1); i++)
1828 q-=GetPixelChannels(image);
1829 SetPixelPixelInfo(image,&background,q);
1834 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1836 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1841 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1842 #pragma omp critical (MagickCore_YShearImage)
1844 proceed=SetImageProgress(image,YShearImageTag,progress++,image->rows);
1845 if (proceed == MagickFalse)
1849 image_view=DestroyCacheView(image_view);
1854 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1858 % R o t a t e I m a g e %
1862 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1864 % RotateImage() creates a new image that is a rotated copy of an existing
1865 % one. Positive angles rotate counter-clockwise (right-hand rule), while
1866 % negative angles rotate clockwise. Rotated images are usually larger than
1867 % the originals and have 'empty' triangular corners. X axis. Empty
1868 % triangles left over from shearing the image are filled with the background
1869 % color defined by member 'background_color' of the image. RotateImage
1870 % allocates the memory necessary for the new Image structure and returns a
1871 % pointer to the new image.
1873 % RotateImage() is based on the paper "A Fast Algorithm for General
1874 % Raster Rotatation" by Alan W. Paeth. RotateImage is adapted from a similar
1875 % method based on the Paeth paper written by Michael Halle of the Spatial
1876 % Imaging Group, MIT Media Lab.
1878 % The format of the RotateImage method is:
1880 % Image *RotateImage(const Image *image,const double degrees,
1881 % ExceptionInfo *exception)
1883 % A description of each parameter follows.
1885 % o image: the image.
1887 % o degrees: Specifies the number of degrees to rotate the image.
1889 % o exception: return any errors or warnings in this structure.
1892 MagickExport Image *RotateImage(const Image *image,const double degrees,
1893 ExceptionInfo *exception)
1922 Adjust rotation angle.
1924 assert(image != (Image *) NULL);
1925 assert(image->signature == MagickSignature);
1926 if (image->debug != MagickFalse)
1927 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1928 assert(exception != (ExceptionInfo *) NULL);
1929 assert(exception->signature == MagickSignature);
1931 while (angle < -45.0)
1933 for (rotations=0; angle > 45.0; rotations++)
1937 Calculate shear equations.
1939 integral_image=IntegralRotateImage(image,rotations,exception);
1940 if (integral_image == (Image *) NULL)
1941 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1942 shear.x=(-tan((double) DegreesToRadians(angle)/2.0));
1943 shear.y=sin((double) DegreesToRadians(angle));
1944 if ((shear.x == 0.0) && (shear.y == 0.0))
1945 return(integral_image);
1946 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
1948 integral_image=DestroyImage(integral_image);
1949 return(integral_image);
1951 if (integral_image->matte == MagickFalse)
1952 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
1956 width=image->columns;
1958 if ((rotations == 1) || (rotations == 3))
1961 height=image->columns;
1963 y_width=width+(ssize_t) floor(fabs(shear.x)*height+0.5);
1964 x_offset=(ssize_t) ceil((double) width+((fabs(shear.y)*height)-width)/2.0-
1966 y_offset=(ssize_t) ceil((double) height+((fabs(shear.y)*y_width)-height)/2.0-
1969 Surround image with a border.
1971 integral_image->border_color=integral_image->background_color;
1972 integral_image->compose=CopyCompositeOp;
1973 border_info.width=(size_t) x_offset;
1974 border_info.height=(size_t) y_offset;
1975 rotate_image=BorderImage(integral_image,&border_info,image->compose,
1977 integral_image=DestroyImage(integral_image);
1978 if (rotate_image == (Image *) NULL)
1979 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1983 status=XShearImage(rotate_image,shear.x,width,height,x_offset,(ssize_t)
1984 (rotate_image->rows-height)/2,exception);
1985 if (status == MagickFalse)
1987 rotate_image=DestroyImage(rotate_image);
1988 return((Image *) NULL);
1990 status=YShearImage(rotate_image,shear.y,y_width,height,(ssize_t)
1991 (rotate_image->columns-y_width)/2,y_offset,exception);
1992 if (status == MagickFalse)
1994 rotate_image=DestroyImage(rotate_image);
1995 return((Image *) NULL);
1997 status=XShearImage(rotate_image,shear.x,y_width,rotate_image->rows,(ssize_t)
1998 (rotate_image->columns-y_width)/2,0,exception);
1999 if (status == MagickFalse)
2001 rotate_image=DestroyImage(rotate_image);
2002 return((Image *) NULL);
2004 status=CropToFitImage(&rotate_image,shear.x,shear.y,(MagickRealType) width,
2005 (MagickRealType) height,MagickTrue,exception);
2006 if (status == MagickFalse)
2008 rotate_image=DestroyImage(rotate_image);
2009 return((Image *) NULL);
2011 rotate_image->compose=image->compose;
2012 rotate_image->page.width=0;
2013 rotate_image->page.height=0;
2014 return(rotate_image);
2018 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2022 % S h e a r I m a g e %
2026 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2028 % ShearImage() creates a new image that is a shear_image copy of an existing
2029 % one. Shearing slides one edge of an image along the X or Y axis, creating
2030 % a parallelogram. An X direction shear slides an edge along the X axis,
2031 % while a Y direction shear slides an edge along the Y axis. The amount of
2032 % the shear is controlled by a shear angle. For X direction shears, x_shear
2033 % is measured relative to the Y axis, and similarly, for Y direction shears
2034 % y_shear is measured relative to the X axis. Empty triangles left over from
2035 % shearing the image are filled with the background color defined by member
2036 % 'background_color' of the image.. ShearImage() allocates the memory
2037 % necessary for the new Image structure and returns a pointer to the new image.
2039 % ShearImage() is based on the paper "A Fast Algorithm for General Raster
2040 % Rotatation" by Alan W. Paeth.
2042 % The format of the ShearImage method is:
2044 % Image *ShearImage(const Image *image,const double x_shear,
2045 % const double y_shear,ExceptionInfo *exception)
2047 % A description of each parameter follows.
2049 % o image: the image.
2051 % o x_shear, y_shear: Specifies the number of degrees to shear the image.
2053 % o exception: return any errors or warnings in this structure.
2056 MagickExport Image *ShearImage(const Image *image,const double x_shear,
2057 const double y_shear,ExceptionInfo *exception)
2079 assert(image != (Image *) NULL);
2080 assert(image->signature == MagickSignature);
2081 if (image->debug != MagickFalse)
2082 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2083 assert(exception != (ExceptionInfo *) NULL);
2084 assert(exception->signature == MagickSignature);
2085 if ((x_shear != 0.0) && (fmod(x_shear,90.0) == 0.0))
2086 ThrowImageException(ImageError,"AngleIsDiscontinuous");
2087 if ((y_shear != 0.0) && (fmod(y_shear,90.0) == 0.0))
2088 ThrowImageException(ImageError,"AngleIsDiscontinuous");
2090 Initialize shear angle.
2092 integral_image=CloneImage(image,0,0,MagickTrue,exception);
2093 if (integral_image == (Image *) NULL)
2094 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2095 shear.x=(-tan(DegreesToRadians(fmod(x_shear,360.0))));
2096 shear.y=tan(DegreesToRadians(fmod(y_shear,360.0)));
2097 if ((shear.x == 0.0) && (shear.y == 0.0))
2098 return(integral_image);
2099 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
2101 integral_image=DestroyImage(integral_image);
2102 return(integral_image);
2104 if (integral_image->matte == MagickFalse)
2105 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
2109 y_width=image->columns+(ssize_t) floor(fabs(shear.x)*image->rows+0.5);
2110 x_offset=(ssize_t) ceil((double) image->columns+((fabs(shear.x)*image->rows)-
2111 image->columns)/2.0-0.5);
2112 y_offset=(ssize_t) ceil((double) image->rows+((fabs(shear.y)*y_width)-
2113 image->rows)/2.0-0.5);
2115 Surround image with border.
2117 integral_image->border_color=integral_image->background_color;
2118 integral_image->compose=CopyCompositeOp;
2119 border_info.width=(size_t) x_offset;
2120 border_info.height=(size_t) y_offset;
2121 shear_image=BorderImage(integral_image,&border_info,image->compose,exception);
2122 integral_image=DestroyImage(integral_image);
2123 if (shear_image == (Image *) NULL)
2124 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2128 if (shear_image->matte == MagickFalse)
2129 (void) SetImageAlphaChannel(shear_image,OpaqueAlphaChannel,exception);
2130 status=XShearImage(shear_image,shear.x,image->columns,image->rows,x_offset,
2131 (ssize_t) (shear_image->rows-image->rows)/2,exception);
2132 if (status == MagickFalse)
2134 shear_image=DestroyImage(shear_image);
2135 return((Image *) NULL);
2137 status=YShearImage(shear_image,shear.y,y_width,image->rows,(ssize_t)
2138 (shear_image->columns-y_width)/2,y_offset,exception);
2139 if (status == MagickFalse)
2141 shear_image=DestroyImage(shear_image);
2142 return((Image *) NULL);
2144 status=CropToFitImage(&shear_image,shear.x,shear.y,(MagickRealType)
2145 image->columns,(MagickRealType) image->rows,MagickFalse,exception);
2146 if (status == MagickFalse)
2148 shear_image=DestroyImage(shear_image);
2149 return((Image *) NULL);
2151 shear_image->compose=image->compose;
2152 shear_image->page.width=0;
2153 shear_image->page.height=0;
2154 return(shear_image);