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/nt-base-private.h"
71 #include "MagickCore/pixel-accessor.h"
72 #include "MagickCore/quantum.h"
73 #include "MagickCore/resource_.h"
74 #include "MagickCore/shear.h"
75 #include "MagickCore/statistic.h"
76 #include "MagickCore/string_.h"
77 #include "MagickCore/string-private.h"
78 #include "MagickCore/thread-private.h"
79 #include "MagickCore/threshold.h"
80 #include "MagickCore/transform.h"
83 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
87 % A f f i n e T r a n s f o r m I m a g e %
91 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
93 % AffineTransformImage() transforms an image as dictated by the affine matrix.
94 % It allocates the memory necessary for the new Image structure and returns
95 % a pointer to the new image.
97 % The format of the AffineTransformImage method is:
99 % Image *AffineTransformImage(const Image *image,
100 % AffineMatrix *affine_matrix,ExceptionInfo *exception)
102 % A description of each parameter follows:
104 % o image: the image.
106 % o affine_matrix: the affine matrix.
108 % o exception: return any errors or warnings in this structure.
111 MagickExport Image *AffineTransformImage(const Image *image,
112 const AffineMatrix *affine_matrix,ExceptionInfo *exception)
121 Affine transform image.
123 assert(image->signature == MagickSignature);
124 if (image->debug != MagickFalse)
125 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
126 assert(affine_matrix != (AffineMatrix *) NULL);
127 assert(exception != (ExceptionInfo *) NULL);
128 assert(exception->signature == MagickSignature);
129 distort[0]=affine_matrix->sx;
130 distort[1]=affine_matrix->rx;
131 distort[2]=affine_matrix->ry;
132 distort[3]=affine_matrix->sy;
133 distort[4]=affine_matrix->tx;
134 distort[5]=affine_matrix->ty;
135 deskew_image=DistortImage(image,AffineProjectionDistortion,6,distort,
136 MagickTrue,exception);
137 return(deskew_image);
141 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
145 + C r o p T o F i t I m a g e %
149 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
151 % CropToFitImage() crops the sheared image as determined by the bounding box
152 % as defined by width and height and shearing angles.
154 % The format of the CropToFitImage method is:
156 % MagickBooleanType CropToFitImage(Image **image,
157 % const MagickRealType x_shear,const MagickRealType x_shear,
158 % const MagickRealType width,const MagickRealType height,
159 % const MagickBooleanType rotate,ExceptionInfo *exception)
161 % A description of each parameter follows.
163 % o image: the image.
165 % o x_shear, y_shear, width, height: Defines a region of the image to crop.
167 % o exception: return any errors or warnings in this structure.
170 static MagickBooleanType CropToFitImage(Image **image,
171 const MagickRealType x_shear,const MagickRealType y_shear,
172 const MagickRealType width,const MagickRealType height,
173 const MagickBooleanType rotate,ExceptionInfo *exception)
191 Calculate the rotated image size.
193 extent[0].x=(double) (-width/2.0);
194 extent[0].y=(double) (-height/2.0);
195 extent[1].x=(double) width/2.0;
196 extent[1].y=(double) (-height/2.0);
197 extent[2].x=(double) (-width/2.0);
198 extent[2].y=(double) height/2.0;
199 extent[3].x=(double) width/2.0;
200 extent[3].y=(double) height/2.0;
201 for (i=0; i < 4; i++)
203 extent[i].x+=x_shear*extent[i].y;
204 extent[i].y+=y_shear*extent[i].x;
205 if (rotate != MagickFalse)
206 extent[i].x+=x_shear*extent[i].y;
207 extent[i].x+=(double) (*image)->columns/2.0;
208 extent[i].y+=(double) (*image)->rows/2.0;
212 for (i=1; i < 4; i++)
214 if (min.x > extent[i].x)
216 if (min.y > extent[i].y)
218 if (max.x < extent[i].x)
220 if (max.y < extent[i].y)
223 geometry.x=(ssize_t) ceil(min.x-0.5);
224 geometry.y=(ssize_t) ceil(min.y-0.5);
225 geometry.width=(size_t) floor(max.x-min.x+0.5);
226 geometry.height=(size_t) floor(max.y-min.y+0.5);
228 (void) ParseAbsoluteGeometry("0x0+0+0",&(*image)->page);
229 crop_image=CropImage(*image,&geometry,exception);
230 if (crop_image == (Image *) NULL)
232 crop_image->page=page;
233 *image=DestroyImage(*image);
239 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
243 % D e s k e w I m a g e %
247 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
249 % DeskewImage() removes skew from the image. Skew is an artifact that
250 % occurs in scanned images because of the camera being misaligned,
251 % imperfections in the scanning or surface, or simply because the paper was
252 % not placed completely flat when scanned.
254 % The format of the DeskewImage method is:
256 % Image *DeskewImage(const Image *image,const double threshold,
257 % ExceptionInfo *exception)
259 % A description of each parameter follows:
261 % o image: the image.
263 % o threshold: separate background from foreground.
265 % o exception: return any errors or warnings in this structure.
269 typedef struct _RadonInfo
294 static RadonInfo *DestroyRadonInfo(RadonInfo *radon_info)
296 assert(radon_info != (RadonInfo *) NULL);
297 switch (radon_info->type)
301 if (radon_info->mapped == MagickFalse)
302 radon_info->cells=(unsigned short *) RelinquishMagickMemory(
305 radon_info->cells=(unsigned short *) UnmapBlob(radon_info->cells,
306 (size_t) radon_info->length);
307 RelinquishMagickResource(MemoryResource,radon_info->length);
312 radon_info->cells=(unsigned short *) UnmapBlob(radon_info->cells,(size_t)
314 RelinquishMagickResource(MapResource,radon_info->length);
318 if (radon_info->file != -1)
319 (void) close(radon_info->file);
320 (void) RelinquishUniqueFileResource(radon_info->path);
321 RelinquishMagickResource(DiskResource,radon_info->length);
327 return((RadonInfo *) RelinquishMagickMemory(radon_info));
330 static MagickBooleanType ResetRadonCells(RadonInfo *radon_info)
342 if (radon_info->type != DiskCache)
344 (void) ResetMagickMemory(radon_info->cells,0,(size_t) radon_info->length);
348 (void) lseek(radon_info->file,0,SEEK_SET);
349 for (y=0; y < (ssize_t) radon_info->height; y++)
351 for (x=0; x < (ssize_t) radon_info->width; x++)
353 count=write(radon_info->file,&value,sizeof(*radon_info->cells));
354 if (count != (ssize_t) sizeof(*radon_info->cells))
357 if (x < (ssize_t) radon_info->width)
360 return(y < (ssize_t) radon_info->height ? MagickFalse : MagickTrue);
363 static RadonInfo *AcquireRadonInfo(const Image *image,const size_t width,
364 const size_t height,ExceptionInfo *exception)
372 radon_info=(RadonInfo *) AcquireMagickMemory(sizeof(*radon_info));
373 if (radon_info == (RadonInfo *) NULL)
374 return((RadonInfo *) NULL);
375 (void) ResetMagickMemory(radon_info,0,sizeof(*radon_info));
376 radon_info->width=width;
377 radon_info->height=height;
378 radon_info->length=(MagickSizeType) width*height*sizeof(*radon_info->cells);
379 radon_info->type=MemoryCache;
380 status=AcquireMagickResource(AreaResource,radon_info->length);
381 if ((status != MagickFalse) &&
382 (radon_info->length == (MagickSizeType) ((size_t) radon_info->length)))
384 status=AcquireMagickResource(MemoryResource,radon_info->length);
385 if (status != MagickFalse)
387 radon_info->mapped=MagickFalse;
388 radon_info->cells=(unsigned short *) AcquireMagickMemory((size_t)
390 if (radon_info->cells == (unsigned short *) NULL)
392 radon_info->mapped=MagickTrue;
393 radon_info->cells=(unsigned short *) MapBlob(-1,IOMode,0,(size_t)
396 if (radon_info->cells == (unsigned short *) NULL)
397 RelinquishMagickResource(MemoryResource,radon_info->length);
400 radon_info->file=(-1);
401 if (radon_info->cells == (unsigned short *) NULL)
403 status=AcquireMagickResource(DiskResource,radon_info->length);
404 if (status == MagickFalse)
406 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
407 "CacheResourcesExhausted","`%s'",image->filename);
408 return(DestroyRadonInfo(radon_info));
410 radon_info->type=DiskCache;
411 (void) AcquireMagickResource(MemoryResource,radon_info->length);
412 radon_info->file=AcquireUniqueFileResource(radon_info->path);
413 if (radon_info->file == -1)
414 return(DestroyRadonInfo(radon_info));
415 status=AcquireMagickResource(MapResource,radon_info->length);
416 if (status != MagickFalse)
418 status=ResetRadonCells(radon_info);
419 if (status != MagickFalse)
421 radon_info->cells=(unsigned short *) MapBlob(radon_info->file,
422 IOMode,0,(size_t) radon_info->length);
423 if (radon_info->cells != (unsigned short *) NULL)
424 radon_info->type=MapCache;
426 RelinquishMagickResource(MapResource,radon_info->length);
433 static inline size_t MagickMin(const size_t x,const size_t y)
440 static inline ssize_t ReadRadonCell(const RadonInfo *radon_info,
441 const MagickOffsetType offset,const size_t length,unsigned char *buffer)
449 #if !defined(MAGICKCORE_HAVE_PPREAD)
450 #if defined(MAGICKCORE_OPENMP_SUPPORT)
451 #pragma omp critical (MagickCore_ReadRadonCell)
455 if (lseek(radon_info->file,offset,SEEK_SET) >= 0)
459 for (i=0; i < (ssize_t) length; i+=count)
461 #if !defined(MAGICKCORE_HAVE_PPREAD)
462 count=read(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
465 count=pread(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
466 SSIZE_MAX),offset+i);
477 #if !defined(MAGICKCORE_HAVE_PPREAD)
484 static inline ssize_t WriteRadonCell(const RadonInfo *radon_info,
485 const MagickOffsetType offset,const size_t length,const unsigned char *buffer)
493 #if !defined(MAGICKCORE_HAVE_PWRITE)
494 #if defined(MAGICKCORE_OPENMP_SUPPORT)
495 #pragma omp critical (MagickCore_WriteRadonCell)
498 if (lseek(radon_info->file,offset,SEEK_SET) >= 0)
502 for (i=0; i < (ssize_t) length; i+=count)
504 #if !defined(MAGICKCORE_HAVE_PWRITE)
505 count=write(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
508 count=pwrite(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
509 SSIZE_MAX),offset+i);
520 #if !defined(MAGICKCORE_HAVE_PWRITE)
527 static inline unsigned short GetRadonCell(const RadonInfo *radon_info,
528 const ssize_t x,const ssize_t y)
536 i=(MagickOffsetType) radon_info->height*x+y;
538 ((MagickSizeType) (i*sizeof(*radon_info->cells)) >= radon_info->length))
540 if (radon_info->type != DiskCache)
541 return(radon_info->cells[i]);
543 (void) ReadRadonCell(radon_info,i*sizeof(*radon_info->cells),
544 sizeof(*radon_info->cells),(unsigned char *) &value);
548 static inline MagickBooleanType SetRadonCell(const RadonInfo *radon_info,
549 const ssize_t x,const ssize_t y,const unsigned short value)
557 i=(MagickOffsetType) radon_info->height*x+y;
559 ((MagickSizeType) (i*sizeof(*radon_info->cells)) >= radon_info->length))
561 if (radon_info->type != DiskCache)
563 radon_info->cells[i]=value;
566 count=WriteRadonCell(radon_info,i*sizeof(*radon_info->cells),
567 sizeof(*radon_info->cells),(const unsigned char *) &value);
568 if (count != (ssize_t) sizeof(*radon_info->cells))
573 static void RadonProjection(RadonInfo *source_cells,
574 RadonInfo *destination_cells,const ssize_t sign,size_t *projection)
591 for (step=1; step < p->width; step*=2)
593 for (x=0; x < (ssize_t) p->width; x+=2*(ssize_t) step)
604 for (i=0; i < (ssize_t) step; i++)
606 for (y=0; y < (ssize_t) (p->height-i-1); y++)
608 cell=GetRadonCell(p,x+i,y);
609 (void) SetRadonCell(q,x+2*i,y,cell+GetRadonCell(p,x+i+(ssize_t)
611 (void) SetRadonCell(q,x+2*i+1,y,cell+GetRadonCell(p,x+i+(ssize_t)
614 for ( ; y < (ssize_t) (p->height-i); y++)
616 cell=GetRadonCell(p,x+i,y);
617 (void) SetRadonCell(q,x+2*i,y,cell+GetRadonCell(p,x+i+(ssize_t) step,
619 (void) SetRadonCell(q,x+2*i+1,y,cell);
621 for ( ; y < (ssize_t) p->height; y++)
623 cell=GetRadonCell(p,x+i,y);
624 (void) SetRadonCell(q,x+2*i,y,cell);
625 (void) SetRadonCell(q,x+2*i+1,y,cell);
633 #if defined(MAGICKCORE_OPENMP_SUPPORT)
634 #pragma omp parallel for schedule(dynamic,4)
636 for (x=0; x < (ssize_t) p->width; x++)
645 for (y=0; y < (ssize_t) (p->height-1); y++)
650 delta=GetRadonCell(p,x,y)-(ssize_t) GetRadonCell(p,x,y+1);
653 projection[p->width+sign*x-1]=sum;
657 static MagickBooleanType RadonTransform(const Image *image,
658 const double threshold,size_t *projection,ExceptionInfo *exception)
686 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
687 source_cells=AcquireRadonInfo(image,width,image->rows,exception);
688 destination_cells=AcquireRadonInfo(image,width,image->rows,exception);
689 if ((source_cells == (RadonInfo *) NULL) ||
690 (destination_cells == (RadonInfo *) NULL))
692 if (destination_cells != (RadonInfo *) NULL)
693 destination_cells=DestroyRadonInfo(destination_cells);
694 if (source_cells != (RadonInfo *) NULL)
695 source_cells=DestroyRadonInfo(source_cells);
698 if (ResetRadonCells(source_cells) == MagickFalse)
700 destination_cells=DestroyRadonInfo(destination_cells);
701 source_cells=DestroyRadonInfo(source_cells);
704 for (i=0; i < 256; i++)
706 byte=(unsigned char) i;
707 for (count=0; byte != 0; byte>>=1)
709 bits[i]=(unsigned short) count;
712 image_view=AcquireCacheView(image);
713 #if defined(MAGICKCORE_OPENMP_SUPPORT)
714 #pragma omp parallel for schedule(dynamic,4) shared(status)
716 for (y=0; y < (ssize_t) image->rows; y++)
718 register const Quantum
729 if (status == MagickFalse)
731 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
732 if (p == (const Quantum *) NULL)
739 i=(ssize_t) (image->columns+7)/8;
740 for (x=0; x < (ssize_t) image->columns; x++)
743 if ((double) GetPixelIntensity(image,p) < threshold)
748 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
752 p+=GetPixelChannels(image);
757 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
760 RadonProjection(source_cells,destination_cells,-1,projection);
761 (void) ResetRadonCells(source_cells);
762 #if defined(MAGICKCORE_OPENMP_SUPPORT)
763 #pragma omp parallel for schedule(dynamic,4) shared(status)
765 for (y=0; y < (ssize_t) image->rows; y++)
767 register const Quantum
778 if (status == MagickFalse)
780 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
781 if (p == (const Quantum *) NULL)
789 for (x=0; x < (ssize_t) image->columns; x++)
792 if ((double) GetPixelIntensity(image,p) < threshold)
797 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
801 p+=GetPixelChannels(image);
806 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
809 RadonProjection(source_cells,destination_cells,1,projection);
810 image_view=DestroyCacheView(image_view);
811 destination_cells=DestroyRadonInfo(destination_cells);
812 source_cells=DestroyRadonInfo(source_cells);
816 static void GetImageBackgroundColor(Image *image,const ssize_t offset,
817 ExceptionInfo *exception)
832 Compute average background color.
836 GetPixelInfo(image,&background);
838 image_view=AcquireCacheView(image);
839 for (y=0; y < (ssize_t) image->rows; y++)
841 register const Quantum
847 if ((y >= offset) && (y < ((ssize_t) image->rows-offset)))
849 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
850 if (p == (const Quantum *) NULL)
852 for (x=0; x < (ssize_t) image->columns; x++)
854 if ((x >= offset) && (x < ((ssize_t) image->columns-offset)))
856 background.red+=QuantumScale*GetPixelRed(image,p);
857 background.green+=QuantumScale*GetPixelGreen(image,p);
858 background.blue+=QuantumScale*GetPixelBlue(image,p);
859 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
860 background.alpha+=QuantumScale*GetPixelAlpha(image,p);
862 p+=GetPixelChannels(image);
865 image_view=DestroyCacheView(image_view);
866 image->background_color.red=(double) ClampToQuantum((MagickRealType)
867 QuantumRange*background.red/count);
868 image->background_color.green=(double) ClampToQuantum((MagickRealType)
869 QuantumRange*background.green/count);
870 image->background_color.blue=(double) ClampToQuantum((MagickRealType)
871 QuantumRange*background.blue/count);
872 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
873 image->background_color.alpha=(double) ClampToQuantum((MagickRealType)
874 QuantumRange*background.alpha/count);
877 MagickExport Image *DeskewImage(const Image *image,const double threshold,
878 ExceptionInfo *exception)
913 Compute deskew angle.
915 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
916 projection=(size_t *) AcquireQuantumMemory((size_t) (2*width-1),
917 sizeof(*projection));
918 if (projection == (size_t *) NULL)
919 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
920 status=RadonTransform(image,threshold,projection,exception);
921 if (status == MagickFalse)
923 projection=(size_t *) RelinquishMagickMemory(projection);
924 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
928 for (i=0; i < (ssize_t) (2*width-1); i++)
930 if (projection[i] > max_projection)
932 skew=i-(ssize_t) width+1;
933 max_projection=projection[i];
936 projection=(size_t *) RelinquishMagickMemory(projection);
940 clone_image=CloneImage(image,0,0,MagickTrue,exception);
941 if (clone_image == (Image *) NULL)
942 return((Image *) NULL);
943 (void) SetImageVirtualPixelMethod(clone_image,BackgroundVirtualPixelMethod);
944 degrees=RadiansToDegrees(-atan((double) skew/width/8));
945 if (image->debug != MagickFalse)
946 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
947 " Deskew angle: %g",degrees);
948 affine_matrix.sx=cos(DegreesToRadians(fmod((double) degrees,360.0)));
949 affine_matrix.rx=sin(DegreesToRadians(fmod((double) degrees,360.0)));
950 affine_matrix.ry=(-sin(DegreesToRadians(fmod((double) degrees,360.0))));
951 affine_matrix.sy=cos(DegreesToRadians(fmod((double) degrees,360.0)));
952 affine_matrix.tx=0.0;
953 affine_matrix.ty=0.0;
954 artifact=GetImageArtifact(image,"deskew:auto-crop");
955 if (artifact == (const char *) NULL)
957 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
958 clone_image=DestroyImage(clone_image);
959 return(deskew_image);
964 GetImageBackgroundColor(clone_image,(ssize_t) StringToLong(artifact),
966 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
967 clone_image=DestroyImage(clone_image);
968 if (deskew_image == (Image *) NULL)
969 return((Image *) NULL);
970 median_image=StatisticImage(deskew_image,MedianStatistic,3,3,exception);
971 if (median_image == (Image *) NULL)
973 deskew_image=DestroyImage(deskew_image);
974 return((Image *) NULL);
976 geometry=GetImageBoundingBox(median_image,exception);
977 median_image=DestroyImage(median_image);
978 if (image->debug != MagickFalse)
979 (void) LogMagickEvent(TransformEvent,GetMagickModule()," Deskew geometry: "
980 "%.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
981 geometry.height,(double) geometry.x,(double) geometry.y);
982 crop_image=CropImage(deskew_image,&geometry,exception);
983 deskew_image=DestroyImage(deskew_image);
988 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
992 + I n t e g r a l R o t a t e I m a g e %
996 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
998 % IntegralRotateImage() rotates the image an integral of 90 degrees. It
999 % allocates the memory necessary for the new Image structure and returns a
1000 % pointer to the rotated image.
1002 % The format of the IntegralRotateImage method is:
1004 % Image *IntegralRotateImage(const Image *image,size_t rotations,
1005 % ExceptionInfo *exception)
1007 % A description of each parameter follows.
1009 % o image: the image.
1011 % o rotations: Specifies the number of 90 degree rotations.
1014 static Image *IntegralRotateImage(const Image *image,size_t rotations,
1015 ExceptionInfo *exception)
1017 #define RotateImageTag "Rotate/Image"
1039 Initialize rotated image attributes.
1041 assert(image != (Image *) NULL);
1045 return(CloneImage(image,0,0,MagickTrue,exception));
1046 if ((rotations == 1) || (rotations == 3))
1047 rotate_image=CloneImage(image,image->rows,image->columns,MagickTrue,
1050 rotate_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1052 if (rotate_image == (Image *) NULL)
1053 return((Image *) NULL);
1055 Integral rotate the image.
1059 image_view=AcquireCacheView(image);
1060 rotate_view=AcquireCacheView(rotate_image);
1082 GetPixelCacheTileSize(image,&tile_width,&tile_height);
1083 tile_width=image->columns;
1084 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1085 #pragma omp parallel for schedule(dynamic,4) 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)
1095 for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
1100 register const Quantum
1114 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
1115 width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
1117 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
1118 height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
1119 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1121 if (p == (const Quantum *) NULL)
1126 for (y=0; y < (ssize_t) width; y++)
1128 register const Quantum
1129 *restrict tile_pixels;
1134 if (status == MagickFalse)
1136 q=QueueCacheViewAuthenticPixels(rotate_view,(ssize_t)
1137 (rotate_image->columns-(tile_y+height)),y+tile_x,height,1,
1139 if (q == (Quantum *) NULL)
1144 tile_pixels=p+((height-1)*width+y)*GetPixelChannels(image);
1145 for (x=0; x < (ssize_t) height; x++)
1150 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1159 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
1160 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
1161 rotate_traits=GetPixelChannelMapTraits(rotate_image,channel);
1162 if ((traits == UndefinedPixelTrait) ||
1163 (rotate_traits == UndefinedPixelTrait))
1165 SetPixelChannel(rotate_image,channel,tile_pixels[i],q);
1167 tile_pixels-=width*GetPixelChannels(image);
1168 q+=GetPixelChannels(rotate_image);
1170 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1171 if (sync == MagickFalse)
1175 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1180 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1181 #pragma omp critical (MagickCore_IntegralRotateImage)
1183 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
1185 if (proceed == MagickFalse)
1189 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1190 image->rows-1,image->rows);
1191 Swap(page.width,page.height);
1192 Swap(page.x,page.y);
1193 if (page.width != 0)
1194 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1202 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1203 #pragma omp parallel for schedule(dynamic,4) shared(progress,status) omp_throttle(1)
1205 for (y=0; y < (ssize_t) image->rows; y++)
1210 register const Quantum
1219 if (status == MagickFalse)
1221 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1222 q=QueueCacheViewAuthenticPixels(rotate_view,0,(ssize_t) (image->rows-y-
1223 1),image->columns,1,exception);
1224 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1229 q+=GetPixelChannels(rotate_image)*image->columns;
1230 for (x=0; x < (ssize_t) image->columns; x++)
1235 q-=GetPixelChannels(rotate_image);
1236 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1245 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
1246 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
1247 rotate_traits=GetPixelChannelMapTraits(rotate_image,channel);
1248 if ((traits == UndefinedPixelTrait) ||
1249 (rotate_traits == UndefinedPixelTrait))
1251 SetPixelChannel(rotate_image,channel,p[i],q);
1253 p+=GetPixelChannels(image);
1255 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1256 if (sync == MagickFalse)
1258 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1263 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1264 #pragma omp critical (MagickCore_IntegralRotateImage)
1266 proceed=SetImageProgress(image,RotateImageTag,progress++,
1268 if (proceed == MagickFalse)
1272 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1273 image->rows-1,image->rows);
1274 Swap(page.width,page.height);
1275 Swap(page.x,page.y);
1276 if (page.width != 0)
1277 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1292 GetPixelCacheTileSize(image,&tile_width,&tile_height);
1293 tile_width=image->columns;
1294 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1295 #pragma omp parallel for schedule(dynamic,4) shared(progress,status) omp_throttle(1)
1297 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
1302 if (status == MagickFalse)
1305 for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
1310 register const Quantum
1324 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
1325 width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
1327 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
1328 height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
1329 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1331 if (p == (const Quantum *) NULL)
1336 for (y=0; y < (ssize_t) width; y++)
1338 register const Quantum
1339 *restrict tile_pixels;
1344 if (status == MagickFalse)
1346 q=QueueCacheViewAuthenticPixels(rotate_view,tile_y,(ssize_t) (y+
1347 rotate_image->rows-(tile_x+width)),height,1,exception);
1348 if (q == (Quantum *) NULL)
1353 tile_pixels=p+((width-1)-y)*GetPixelChannels(image);
1354 for (x=0; x < (ssize_t) height; x++)
1359 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1368 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
1369 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
1370 rotate_traits=GetPixelChannelMapTraits(rotate_image,channel);
1371 if ((traits == UndefinedPixelTrait) ||
1372 (rotate_traits == UndefinedPixelTrait))
1374 SetPixelChannel(rotate_image,channel,tile_pixels[i],q);
1376 tile_pixels+=width*GetPixelChannels(image);
1377 q+=GetPixelChannels(rotate_image);
1379 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1380 if (sync == MagickFalse)
1384 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1389 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1390 #pragma omp critical (MagickCore_IntegralRotateImage)
1392 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
1394 if (proceed == MagickFalse)
1398 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1399 image->rows-1,image->rows);
1400 Swap(page.width,page.height);
1401 Swap(page.x,page.y);
1402 if (page.width != 0)
1403 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1407 rotate_view=DestroyCacheView(rotate_view);
1408 image_view=DestroyCacheView(image_view);
1409 rotate_image->type=image->type;
1410 rotate_image->page=page;
1411 if (status == MagickFalse)
1412 rotate_image=DestroyImage(rotate_image);
1413 return(rotate_image);
1417 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1421 + X S h e a r I m a g e %
1425 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1427 % XShearImage() shears the image in the X direction with a shear angle of
1428 % 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1429 % negative angles shear clockwise. Angles are measured relative to a vertical
1430 % Y-axis. X shears will widen an image creating 'empty' triangles on the left
1431 % and right sides of the source image.
1433 % The format of the XShearImage method is:
1435 % MagickBooleanType XShearImage(Image *image,const MagickRealType degrees,
1436 % const size_t width,const size_t height,
1437 % const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
1439 % A description of each parameter follows.
1441 % o image: the image.
1443 % o degrees: A MagickRealType representing the shearing angle along the X
1446 % o width, height, x_offset, y_offset: Defines a region of the image
1449 % o exception: return any errors or warnings in this structure.
1452 static MagickBooleanType XShearImage(Image *image,const MagickRealType degrees,
1453 const size_t width,const size_t height,const ssize_t x_offset,
1454 const ssize_t y_offset,ExceptionInfo *exception)
1456 #define XShearImageTag "XShear/Image"
1479 assert(image != (Image *) NULL);
1480 assert(image->signature == MagickSignature);
1481 if (image->debug != MagickFalse)
1482 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1483 background=image->background_color;
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,&destination);
1565 SetPixelPixelInfo(image,&destination,q);
1566 SetPixelInfo(image,p,&pixel);
1567 p+=GetPixelChannels(image);
1568 q+=GetPixelChannels(image);
1570 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1571 &background,(MagickRealType) background.alpha,area,&destination);
1572 SetPixelPixelInfo(image,&destination,q);
1573 q+=GetPixelChannels(image);
1574 for (i=0; i < (step-1); i++)
1576 SetPixelPixelInfo(image,&background,q);
1577 q+=GetPixelChannels(image);
1584 Transfer pixels right-to-left.
1586 p+=width*GetPixelChannels(image);
1587 q=p+step*GetPixelChannels(image);
1588 for (i=0; i < (ssize_t) width; i++)
1590 p-=GetPixelChannels(image);
1591 q-=GetPixelChannels(image);
1592 if ((size_t) (x_offset+width+step-i) >= image->columns)
1594 SetPixelInfo(image,p,&source);
1595 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1596 &source,(MagickRealType) GetPixelAlpha(image,p),area,&destination);
1597 SetPixelPixelInfo(image,&destination,q);
1598 SetPixelInfo(image,p,&pixel);
1600 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1601 &background,(MagickRealType) background.alpha,area,&destination);
1602 q-=GetPixelChannels(image);
1603 SetPixelPixelInfo(image,&destination,q);
1604 for (i=0; i < (step-1); i++)
1606 q-=GetPixelChannels(image);
1607 SetPixelPixelInfo(image,&background,q);
1612 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1614 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1619 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1620 #pragma omp critical (MagickCore_XShearImage)
1622 proceed=SetImageProgress(image,XShearImageTag,progress++,height);
1623 if (proceed == MagickFalse)
1627 image_view=DestroyCacheView(image_view);
1632 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1636 + Y S h e a r I m a g e %
1640 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1642 % YShearImage shears the image in the Y direction with a shear angle of
1643 % 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1644 % negative angles shear clockwise. Angles are measured relative to a
1645 % horizontal X-axis. Y shears will increase the height of an image creating
1646 % 'empty' triangles on the top and bottom of the source image.
1648 % The format of the YShearImage method is:
1650 % MagickBooleanType YShearImage(Image *image,const MagickRealType degrees,
1651 % const size_t width,const size_t height,
1652 % const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
1654 % A description of each parameter follows.
1656 % o image: the image.
1658 % o degrees: A MagickRealType representing the shearing angle along the Y
1661 % o width, height, x_offset, y_offset: Defines a region of the image
1664 % o exception: return any errors or warnings in this structure.
1667 static MagickBooleanType YShearImage(Image *image,const MagickRealType degrees,
1668 const size_t width,const size_t height,const ssize_t x_offset,
1669 const ssize_t y_offset,ExceptionInfo *exception)
1671 #define YShearImageTag "YShear/Image"
1694 assert(image != (Image *) NULL);
1695 assert(image->signature == MagickSignature);
1696 if (image->debug != MagickFalse)
1697 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1698 background=image->background_color;
1704 image_view=AcquireCacheView(image);
1705 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1706 #pragma omp parallel for schedule(dynamic,4) shared(progress, status)
1708 for (x=0; x < (ssize_t) width; x++)
1732 if (status == MagickFalse)
1734 p=GetCacheViewAuthenticPixels(image_view,x_offset+x,0,1,image->rows,
1736 if (p == (Quantum *) NULL)
1741 p+=y_offset*GetPixelChannels(image);
1742 displacement=degrees*(MagickRealType) (x-width/2.0);
1743 if (displacement == 0.0)
1745 if (displacement > 0.0)
1749 displacement*=(-1.0);
1752 step=(ssize_t) floor((double) displacement);
1753 area=(MagickRealType) (displacement-step);
1756 GetPixelInfo(image,&source);
1757 GetPixelInfo(image,&destination);
1763 Transfer pixels top-to-bottom.
1765 if (step > y_offset)
1767 q=p-step*GetPixelChannels(image);
1768 for (i=0; i < (ssize_t) height; i++)
1770 if ((y_offset+i) < step)
1772 p+=GetPixelChannels(image);
1773 SetPixelInfo(image,p,&pixel);
1774 q+=GetPixelChannels(image);
1777 SetPixelInfo(image,p,&source);
1778 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1779 &source,(MagickRealType) GetPixelAlpha(image,p),area,
1781 SetPixelPixelInfo(image,&destination,q);
1782 SetPixelInfo(image,p,&pixel);
1783 p+=GetPixelChannels(image);
1784 q+=GetPixelChannels(image);
1786 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1787 &background,(MagickRealType) background.alpha,area,&destination);
1788 SetPixelPixelInfo(image,&destination,q);
1789 q+=GetPixelChannels(image);
1790 for (i=0; i < (step-1); i++)
1792 SetPixelPixelInfo(image,&background,q);
1793 q+=GetPixelChannels(image);
1800 Transfer pixels bottom-to-top.
1802 p+=height*GetPixelChannels(image);
1803 q=p+step*GetPixelChannels(image);
1804 for (i=0; i < (ssize_t) height; i++)
1806 p-=GetPixelChannels(image);
1807 q-=GetPixelChannels(image);
1808 if ((size_t) (y_offset+height+step-i) >= image->rows)
1810 SetPixelInfo(image,p,&source);
1811 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1812 &source,(MagickRealType) GetPixelAlpha(image,p),area,
1814 SetPixelPixelInfo(image,&destination,q);
1815 SetPixelInfo(image,p,&pixel);
1817 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1818 &background,(MagickRealType) background.alpha,area,&destination);
1819 q-=GetPixelChannels(image);
1820 SetPixelPixelInfo(image,&destination,q);
1821 for (i=0; i < (step-1); i++)
1823 q-=GetPixelChannels(image);
1824 SetPixelPixelInfo(image,&background,q);
1829 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1831 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1836 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1837 #pragma omp critical (MagickCore_YShearImage)
1839 proceed=SetImageProgress(image,YShearImageTag,progress++,image->rows);
1840 if (proceed == MagickFalse)
1844 image_view=DestroyCacheView(image_view);
1849 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1853 % R o t a t e I m a g e %
1857 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1859 % RotateImage() creates a new image that is a rotated copy of an existing
1860 % one. Positive angles rotate counter-clockwise (right-hand rule), while
1861 % negative angles rotate clockwise. Rotated images are usually larger than
1862 % the originals and have 'empty' triangular corners. X axis. Empty
1863 % triangles left over from shearing the image are filled with the background
1864 % color defined by member 'background_color' of the image. RotateImage
1865 % allocates the memory necessary for the new Image structure and returns a
1866 % pointer to the new image.
1868 % RotateImage() is based on the paper "A Fast Algorithm for General
1869 % Raster Rotatation" by Alan W. Paeth. RotateImage is adapted from a similar
1870 % method based on the Paeth paper written by Michael Halle of the Spatial
1871 % Imaging Group, MIT Media Lab.
1873 % The format of the RotateImage method is:
1875 % Image *RotateImage(const Image *image,const double degrees,
1876 % ExceptionInfo *exception)
1878 % A description of each parameter follows.
1880 % o image: the image.
1882 % o degrees: Specifies the number of degrees to rotate the image.
1884 % o exception: return any errors or warnings in this structure.
1887 MagickExport Image *RotateImage(const Image *image,const double degrees,
1888 ExceptionInfo *exception)
1917 Adjust rotation angle.
1919 assert(image != (Image *) NULL);
1920 assert(image->signature == MagickSignature);
1921 if (image->debug != MagickFalse)
1922 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1923 assert(exception != (ExceptionInfo *) NULL);
1924 assert(exception->signature == MagickSignature);
1926 while (angle < -45.0)
1928 for (rotations=0; angle > 45.0; rotations++)
1932 Calculate shear equations.
1934 integral_image=IntegralRotateImage(image,rotations,exception);
1935 if (integral_image == (Image *) NULL)
1936 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1937 shear.x=(-tan((double) DegreesToRadians(angle)/2.0));
1938 shear.y=sin((double) DegreesToRadians(angle));
1939 if ((shear.x == 0.0) && (shear.y == 0.0))
1940 return(integral_image);
1941 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
1943 integral_image=DestroyImage(integral_image);
1944 return(integral_image);
1946 if (integral_image->matte == MagickFalse)
1947 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
1951 width=image->columns;
1953 if ((rotations == 1) || (rotations == 3))
1956 height=image->columns;
1958 y_width=width+(ssize_t) floor(fabs(shear.x)*height+0.5);
1959 x_offset=(ssize_t) ceil((double) width+((fabs(shear.y)*height)-width)/2.0-
1961 y_offset=(ssize_t) ceil((double) height+((fabs(shear.y)*y_width)-height)/2.0-
1964 Surround image with a border.
1966 integral_image->border_color=integral_image->background_color;
1967 integral_image->compose=CopyCompositeOp;
1968 border_info.width=(size_t) x_offset;
1969 border_info.height=(size_t) y_offset;
1970 rotate_image=BorderImage(integral_image,&border_info,image->compose,
1972 integral_image=DestroyImage(integral_image);
1973 if (rotate_image == (Image *) NULL)
1974 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1978 status=XShearImage(rotate_image,shear.x,width,height,x_offset,(ssize_t)
1979 (rotate_image->rows-height)/2,exception);
1980 if (status == MagickFalse)
1982 rotate_image=DestroyImage(rotate_image);
1983 return((Image *) NULL);
1985 status=YShearImage(rotate_image,shear.y,y_width,height,(ssize_t)
1986 (rotate_image->columns-y_width)/2,y_offset,exception);
1987 if (status == MagickFalse)
1989 rotate_image=DestroyImage(rotate_image);
1990 return((Image *) NULL);
1992 status=XShearImage(rotate_image,shear.x,y_width,rotate_image->rows,(ssize_t)
1993 (rotate_image->columns-y_width)/2,0,exception);
1994 if (status == MagickFalse)
1996 rotate_image=DestroyImage(rotate_image);
1997 return((Image *) NULL);
1999 status=CropToFitImage(&rotate_image,shear.x,shear.y,(MagickRealType) width,
2000 (MagickRealType) height,MagickTrue,exception);
2001 if (status == MagickFalse)
2003 rotate_image=DestroyImage(rotate_image);
2004 return((Image *) NULL);
2006 rotate_image->compose=image->compose;
2007 rotate_image->page.width=0;
2008 rotate_image->page.height=0;
2009 return(rotate_image);
2013 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2017 % S h e a r I m a g e %
2021 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2023 % ShearImage() creates a new image that is a shear_image copy of an existing
2024 % one. Shearing slides one edge of an image along the X or Y axis, creating
2025 % a parallelogram. An X direction shear slides an edge along the X axis,
2026 % while a Y direction shear slides an edge along the Y axis. The amount of
2027 % the shear is controlled by a shear angle. For X direction shears, x_shear
2028 % is measured relative to the Y axis, and similarly, for Y direction shears
2029 % y_shear is measured relative to the X axis. Empty triangles left over from
2030 % shearing the image are filled with the background color defined by member
2031 % 'background_color' of the image.. ShearImage() allocates the memory
2032 % necessary for the new Image structure and returns a pointer to the new image.
2034 % ShearImage() is based on the paper "A Fast Algorithm for General Raster
2035 % Rotatation" by Alan W. Paeth.
2037 % The format of the ShearImage method is:
2039 % Image *ShearImage(const Image *image,const double x_shear,
2040 % const double y_shear,ExceptionInfo *exception)
2042 % A description of each parameter follows.
2044 % o image: the image.
2046 % o x_shear, y_shear: Specifies the number of degrees to shear the image.
2048 % o exception: return any errors or warnings in this structure.
2051 MagickExport Image *ShearImage(const Image *image,const double x_shear,
2052 const double y_shear,ExceptionInfo *exception)
2074 assert(image != (Image *) NULL);
2075 assert(image->signature == MagickSignature);
2076 if (image->debug != MagickFalse)
2077 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2078 assert(exception != (ExceptionInfo *) NULL);
2079 assert(exception->signature == MagickSignature);
2080 if ((x_shear != 0.0) && (fmod(x_shear,90.0) == 0.0))
2081 ThrowImageException(ImageError,"AngleIsDiscontinuous");
2082 if ((y_shear != 0.0) && (fmod(y_shear,90.0) == 0.0))
2083 ThrowImageException(ImageError,"AngleIsDiscontinuous");
2085 Initialize shear angle.
2087 integral_image=CloneImage(image,0,0,MagickTrue,exception);
2088 if (integral_image == (Image *) NULL)
2089 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2090 shear.x=(-tan(DegreesToRadians(fmod(x_shear,360.0))));
2091 shear.y=tan(DegreesToRadians(fmod(y_shear,360.0)));
2092 if ((shear.x == 0.0) && (shear.y == 0.0))
2093 return(integral_image);
2094 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
2096 integral_image=DestroyImage(integral_image);
2097 return(integral_image);
2099 if (integral_image->matte == MagickFalse)
2100 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
2104 y_width=image->columns+(ssize_t) floor(fabs(shear.x)*image->rows+0.5);
2105 x_offset=(ssize_t) ceil((double) image->columns+((fabs(shear.x)*image->rows)-
2106 image->columns)/2.0-0.5);
2107 y_offset=(ssize_t) ceil((double) image->rows+((fabs(shear.y)*y_width)-
2108 image->rows)/2.0-0.5);
2110 Surround image with border.
2112 integral_image->border_color=integral_image->background_color;
2113 integral_image->compose=CopyCompositeOp;
2114 border_info.width=(size_t) x_offset;
2115 border_info.height=(size_t) y_offset;
2116 shear_image=BorderImage(integral_image,&border_info,image->compose,exception);
2117 integral_image=DestroyImage(integral_image);
2118 if (shear_image == (Image *) NULL)
2119 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2123 if (shear_image->matte == MagickFalse)
2124 (void) SetImageAlphaChannel(shear_image,OpaqueAlphaChannel,exception);
2125 status=XShearImage(shear_image,shear.x,image->columns,image->rows,x_offset,
2126 (ssize_t) (shear_image->rows-image->rows)/2,exception);
2127 if (status == MagickFalse)
2129 shear_image=DestroyImage(shear_image);
2130 return((Image *) NULL);
2132 status=YShearImage(shear_image,shear.y,y_width,image->rows,(ssize_t)
2133 (shear_image->columns-y_width)/2,y_offset,exception);
2134 if (status == MagickFalse)
2136 shear_image=DestroyImage(shear_image);
2137 return((Image *) NULL);
2139 status=CropToFitImage(&shear_image,shear.x,shear.y,(MagickRealType)
2140 image->columns,(MagickRealType) image->rows,MagickFalse,exception);
2141 if (status == MagickFalse)
2143 shear_image=DestroyImage(shear_image);
2144 return((Image *) NULL);
2146 shear_image->compose=image->compose;
2147 shear_image->page.width=0;
2148 shear_image->page.height=0;
2149 return(shear_image);