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-2013 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 XShearImage() and YShearImage() methods are based on the paper "A Fast
37 % Algorithm for General Raster Rotatation" by Alan W. Paeth, Graphics
38 % Interface '86 (Vancouver). ShearRotateImage() 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.
47 #include "MagickCore/studio.h"
48 #include "MagickCore/artifact.h"
49 #include "MagickCore/attribute.h"
50 #include "MagickCore/blob-private.h"
51 #include "MagickCore/cache-private.h"
52 #include "MagickCore/color-private.h"
53 #include "MagickCore/colorspace-private.h"
54 #include "MagickCore/composite.h"
55 #include "MagickCore/composite-private.h"
56 #include "MagickCore/decorate.h"
57 #include "MagickCore/distort.h"
58 #include "MagickCore/draw.h"
59 #include "MagickCore/exception.h"
60 #include "MagickCore/exception-private.h"
61 #include "MagickCore/gem.h"
62 #include "MagickCore/geometry.h"
63 #include "MagickCore/image.h"
64 #include "MagickCore/image-private.h"
65 #include "MagickCore/memory_.h"
66 #include "MagickCore/list.h"
67 #include "MagickCore/monitor.h"
68 #include "MagickCore/monitor-private.h"
69 #include "MagickCore/nt-base-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 + C r o p T o F i t I m a g e %
90 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
92 % CropToFitImage() crops the sheared image as determined by the bounding box
93 % as defined by width and height and shearing angles.
95 % The format of the CropToFitImage method is:
97 % MagickBooleanType CropToFitImage(Image **image,
98 % const double x_shear,const double x_shear,
99 % const double width,const double height,
100 % const MagickBooleanType rotate,ExceptionInfo *exception)
102 % A description of each parameter follows.
104 % o image: the image.
106 % o x_shear, y_shear, width, height: Defines a region of the image to crop.
108 % o exception: return any errors or warnings in this structure.
111 static MagickBooleanType CropToFitImage(Image **image,
112 const double x_shear,const double y_shear,
113 const double width,const double height,
114 const MagickBooleanType rotate,ExceptionInfo *exception)
132 Calculate the rotated image size.
134 extent[0].x=(double) (-width/2.0);
135 extent[0].y=(double) (-height/2.0);
136 extent[1].x=(double) width/2.0;
137 extent[1].y=(double) (-height/2.0);
138 extent[2].x=(double) (-width/2.0);
139 extent[2].y=(double) height/2.0;
140 extent[3].x=(double) width/2.0;
141 extent[3].y=(double) height/2.0;
142 for (i=0; i < 4; i++)
144 extent[i].x+=x_shear*extent[i].y;
145 extent[i].y+=y_shear*extent[i].x;
146 if (rotate != MagickFalse)
147 extent[i].x+=x_shear*extent[i].y;
148 extent[i].x+=(double) (*image)->columns/2.0;
149 extent[i].y+=(double) (*image)->rows/2.0;
153 for (i=1; i < 4; i++)
155 if (min.x > extent[i].x)
157 if (min.y > extent[i].y)
159 if (max.x < extent[i].x)
161 if (max.y < extent[i].y)
164 geometry.x=(ssize_t) ceil(min.x-0.5);
165 geometry.y=(ssize_t) ceil(min.y-0.5);
166 geometry.width=(size_t) floor(max.x-min.x+0.5);
167 geometry.height=(size_t) floor(max.y-min.y+0.5);
169 (void) ParseAbsoluteGeometry("0x0+0+0",&(*image)->page);
170 crop_image=CropImage(*image,&geometry,exception);
171 if (crop_image == (Image *) NULL)
173 crop_image->page=page;
174 *image=DestroyImage(*image);
180 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
184 % D e s k e w I m a g e %
188 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
190 % DeskewImage() removes skew from the image. Skew is an artifact that
191 % occurs in scanned images because of the camera being misaligned,
192 % imperfections in the scanning or surface, or simply because the paper was
193 % not placed completely flat when scanned.
195 % The result will be auto-croped if the artifact "deskew:auto-crop" is
196 % defined, while the amount the image is to be deskewed, in degrees is also
197 % saved as the artifact "deskew:angle".
199 % If the artifact "deskew:auto-crop" is given the image will be automatically
200 % cropped of the excess background. The value is the border width of all
201 % pixels around the edge that will be used to determine an average border
202 % color for the automatic trim.
204 % The format of the DeskewImage method is:
206 % Image *DeskewImage(const Image *image,const double threshold,
207 % ExceptionInfo *exception)
209 % A description of each parameter follows:
211 % o image: the image.
213 % o threshold: separate background from foreground.
215 % o exception: return any errors or warnings in this structure.
219 typedef struct _RadonInfo
244 static RadonInfo *DestroyRadonInfo(RadonInfo *radon_info)
246 assert(radon_info != (RadonInfo *) NULL);
247 switch (radon_info->type)
251 if (radon_info->mapped == MagickFalse)
252 radon_info->cells=(unsigned short *) RelinquishMagickMemory(
255 radon_info->cells=(unsigned short *) UnmapBlob(radon_info->cells,
256 (size_t) radon_info->length);
257 RelinquishMagickResource(MemoryResource,radon_info->length);
262 radon_info->cells=(unsigned short *) UnmapBlob(radon_info->cells,(size_t)
264 RelinquishMagickResource(MapResource,radon_info->length);
268 if (radon_info->file != -1)
269 (void) close(radon_info->file);
270 (void) RelinquishUniqueFileResource(radon_info->path);
271 RelinquishMagickResource(DiskResource,radon_info->length);
277 return((RadonInfo *) RelinquishMagickMemory(radon_info));
280 static MagickBooleanType ResetRadonCells(RadonInfo *radon_info)
292 if (radon_info->type != DiskCache)
294 (void) ResetMagickMemory(radon_info->cells,0,(size_t) radon_info->length);
298 (void) lseek(radon_info->file,0,SEEK_SET);
299 for (y=0; y < (ssize_t) radon_info->height; y++)
301 for (x=0; x < (ssize_t) radon_info->width; x++)
303 count=write(radon_info->file,&value,sizeof(*radon_info->cells));
304 if (count != (ssize_t) sizeof(*radon_info->cells))
307 if (x < (ssize_t) radon_info->width)
310 return(y < (ssize_t) radon_info->height ? MagickFalse : MagickTrue);
313 static RadonInfo *AcquireRadonInfo(const Image *image,const size_t width,
314 const size_t height,ExceptionInfo *exception)
322 radon_info=(RadonInfo *) AcquireMagickMemory(sizeof(*radon_info));
323 if (radon_info == (RadonInfo *) NULL)
324 return((RadonInfo *) NULL);
325 (void) ResetMagickMemory(radon_info,0,sizeof(*radon_info));
326 radon_info->width=width;
327 radon_info->height=height;
328 radon_info->length=(MagickSizeType) width*height*sizeof(*radon_info->cells);
329 radon_info->type=MemoryCache;
330 status=AcquireMagickResource(AreaResource,radon_info->length);
331 if ((status != MagickFalse) &&
332 (radon_info->length == (MagickSizeType) ((size_t) radon_info->length)))
334 status=AcquireMagickResource(MemoryResource,radon_info->length);
335 if (status != MagickFalse)
337 radon_info->mapped=MagickFalse;
338 radon_info->cells=(unsigned short *) AcquireMagickMemory((size_t)
340 if (radon_info->cells == (unsigned short *) NULL)
342 radon_info->mapped=MagickTrue;
343 radon_info->cells=(unsigned short *) MapBlob(-1,IOMode,0,(size_t)
346 if (radon_info->cells == (unsigned short *) NULL)
347 RelinquishMagickResource(MemoryResource,radon_info->length);
350 radon_info->file=(-1);
351 if (radon_info->cells == (unsigned short *) NULL)
353 status=AcquireMagickResource(DiskResource,radon_info->length);
354 if (status == MagickFalse)
356 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
357 "CacheResourcesExhausted","`%s'",image->filename);
358 return(DestroyRadonInfo(radon_info));
360 radon_info->type=DiskCache;
361 (void) AcquireMagickResource(MemoryResource,radon_info->length);
362 radon_info->file=AcquireUniqueFileResource(radon_info->path);
363 if (radon_info->file == -1)
364 return(DestroyRadonInfo(radon_info));
365 status=AcquireMagickResource(MapResource,radon_info->length);
366 if (status != MagickFalse)
368 status=ResetRadonCells(radon_info);
369 if (status != MagickFalse)
371 radon_info->cells=(unsigned short *) MapBlob(radon_info->file,
372 IOMode,0,(size_t) radon_info->length);
373 if (radon_info->cells != (unsigned short *) NULL)
374 radon_info->type=MapCache;
376 RelinquishMagickResource(MapResource,radon_info->length);
383 static inline size_t MagickMin(const size_t x,const size_t y)
390 static inline ssize_t ReadRadonCell(const RadonInfo *radon_info,
391 const MagickOffsetType offset,const size_t length,unsigned char *buffer)
399 #if !defined(MAGICKCORE_HAVE_PPREAD)
400 #if defined(MAGICKCORE_OPENMP_SUPPORT)
401 #pragma omp critical (MagickCore_ReadRadonCell)
405 if (lseek(radon_info->file,offset,SEEK_SET) >= 0)
409 for (i=0; i < (ssize_t) length; i+=count)
411 #if !defined(MAGICKCORE_HAVE_PPREAD)
412 count=read(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
415 count=pread(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
416 SSIZE_MAX),offset+i);
427 #if !defined(MAGICKCORE_HAVE_PPREAD)
434 static inline ssize_t WriteRadonCell(const RadonInfo *radon_info,
435 const MagickOffsetType offset,const size_t length,const unsigned char *buffer)
443 #if !defined(MAGICKCORE_HAVE_PWRITE)
444 #if defined(MAGICKCORE_OPENMP_SUPPORT)
445 #pragma omp critical (MagickCore_WriteRadonCell)
448 if (lseek(radon_info->file,offset,SEEK_SET) >= 0)
452 for (i=0; i < (ssize_t) length; i+=count)
454 #if !defined(MAGICKCORE_HAVE_PWRITE)
455 count=write(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
458 count=pwrite(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
459 SSIZE_MAX),offset+i);
470 #if !defined(MAGICKCORE_HAVE_PWRITE)
477 static inline unsigned short GetRadonCell(const RadonInfo *radon_info,
478 const ssize_t x,const ssize_t y)
486 i=(MagickOffsetType) radon_info->height*x+y;
488 ((MagickSizeType) (i*sizeof(*radon_info->cells)) >= radon_info->length))
490 if (radon_info->type != DiskCache)
491 return(radon_info->cells[i]);
493 (void) ReadRadonCell(radon_info,i*sizeof(*radon_info->cells),
494 sizeof(*radon_info->cells),(unsigned char *) &value);
498 static inline MagickBooleanType SetRadonCell(const RadonInfo *radon_info,
499 const ssize_t x,const ssize_t y,const unsigned short value)
507 i=(MagickOffsetType) radon_info->height*x+y;
509 ((MagickSizeType) (i*sizeof(*radon_info->cells)) >= radon_info->length))
511 if (radon_info->type != DiskCache)
513 radon_info->cells[i]=value;
516 count=WriteRadonCell(radon_info,i*sizeof(*radon_info->cells),
517 sizeof(*radon_info->cells),(const unsigned char *) &value);
518 if (count != (ssize_t) sizeof(*radon_info->cells))
523 static void RadonProjection(const Image *image,RadonInfo *source_cells,
524 RadonInfo *destination_cells,const ssize_t sign,size_t *projection)
539 assert(image != (Image *) NULL);
542 for (step=1; step < p->width; step*=2)
544 for (x=0; x < (ssize_t) p->width; x+=2*(ssize_t) step)
555 for (i=0; i < (ssize_t) step; i++)
557 for (y=0; y < (ssize_t) (p->height-i-1); y++)
559 cell=GetRadonCell(p,x+i,y);
560 (void) SetRadonCell(q,x+2*i,y,cell+GetRadonCell(p,x+i+(ssize_t)
562 (void) SetRadonCell(q,x+2*i+1,y,cell+GetRadonCell(p,x+i+(ssize_t)
565 for ( ; y < (ssize_t) (p->height-i); y++)
567 cell=GetRadonCell(p,x+i,y);
568 (void) SetRadonCell(q,x+2*i,y,cell+GetRadonCell(p,x+i+(ssize_t) step,
570 (void) SetRadonCell(q,x+2*i+1,y,cell);
572 for ( ; y < (ssize_t) p->height; y++)
574 cell=GetRadonCell(p,x+i,y);
575 (void) SetRadonCell(q,x+2*i,y,cell);
576 (void) SetRadonCell(q,x+2*i+1,y,cell);
584 #if defined(MAGICKCORE_OPENMP_SUPPORT)
585 #pragma omp parallel for schedule(static,4) \
586 magick_threads(image,image,1,1)
588 for (x=0; x < (ssize_t) p->width; x++)
597 for (y=0; y < (ssize_t) (p->height-1); y++)
602 delta=GetRadonCell(p,x,y)-(ssize_t) GetRadonCell(p,x,y+1);
605 projection[p->width+sign*x-1]=sum;
609 static MagickBooleanType RadonTransform(const Image *image,
610 const double threshold,size_t *projection,ExceptionInfo *exception)
638 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
639 source_cells=AcquireRadonInfo(image,width,image->rows,exception);
640 destination_cells=AcquireRadonInfo(image,width,image->rows,exception);
641 if ((source_cells == (RadonInfo *) NULL) ||
642 (destination_cells == (RadonInfo *) NULL))
644 if (destination_cells != (RadonInfo *) NULL)
645 destination_cells=DestroyRadonInfo(destination_cells);
646 if (source_cells != (RadonInfo *) NULL)
647 source_cells=DestroyRadonInfo(source_cells);
650 if (ResetRadonCells(source_cells) == MagickFalse)
652 destination_cells=DestroyRadonInfo(destination_cells);
653 source_cells=DestroyRadonInfo(source_cells);
656 for (i=0; i < 256; i++)
658 byte=(unsigned char) i;
659 for (count=0; byte != 0; byte>>=1)
661 bits[i]=(unsigned short) count;
664 image_view=AcquireVirtualCacheView(image,exception);
665 #if defined(MAGICKCORE_OPENMP_SUPPORT)
666 #pragma omp parallel for schedule(static,4) shared(status) \
667 magick_threads(image,image,image->rows,1)
669 for (y=0; y < (ssize_t) image->rows; y++)
671 register const Quantum
682 if (status == MagickFalse)
684 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
685 if (p == (const Quantum *) NULL)
692 i=(ssize_t) (image->columns+7)/8;
693 for (x=0; x < (ssize_t) image->columns; x++)
696 if (GetPixelIntensity(image,p) < threshold)
701 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
705 p+=GetPixelChannels(image);
710 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
713 RadonProjection(image,source_cells,destination_cells,-1,projection);
714 (void) ResetRadonCells(source_cells);
715 #if defined(MAGICKCORE_OPENMP_SUPPORT)
716 #pragma omp parallel for schedule(static,4) shared(status) \
717 magick_threads(image,image,1,1)
719 for (y=0; y < (ssize_t) image->rows; y++)
721 register const Quantum
732 if (status == MagickFalse)
734 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
735 if (p == (const Quantum *) NULL)
743 for (x=0; x < (ssize_t) image->columns; x++)
746 if (GetPixelIntensity(image,p) < threshold)
751 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
755 p+=GetPixelChannels(image);
760 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
763 RadonProjection(image,source_cells,destination_cells,1,projection);
764 image_view=DestroyCacheView(image_view);
765 destination_cells=DestroyRadonInfo(destination_cells);
766 source_cells=DestroyRadonInfo(source_cells);
770 static void GetImageBackgroundColor(Image *image,const ssize_t offset,
771 ExceptionInfo *exception)
786 Compute average background color.
790 GetPixelInfo(image,&background);
792 image_view=AcquireVirtualCacheView(image,exception);
793 for (y=0; y < (ssize_t) image->rows; y++)
795 register const Quantum
801 if ((y >= offset) && (y < ((ssize_t) image->rows-offset)))
803 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
804 if (p == (const Quantum *) NULL)
806 for (x=0; x < (ssize_t) image->columns; x++)
808 if ((x >= offset) && (x < ((ssize_t) image->columns-offset)))
810 background.red+=QuantumScale*GetPixelRed(image,p);
811 background.green+=QuantumScale*GetPixelGreen(image,p);
812 background.blue+=QuantumScale*GetPixelBlue(image,p);
813 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
814 background.alpha+=QuantumScale*GetPixelAlpha(image,p);
816 p+=GetPixelChannels(image);
819 image_view=DestroyCacheView(image_view);
820 image->background_color.red=(double) ClampToQuantum(QuantumRange*
821 background.red/count);
822 image->background_color.green=(double) ClampToQuantum(QuantumRange*
823 background.green/count);
824 image->background_color.blue=(double) ClampToQuantum(QuantumRange*
825 background.blue/count);
826 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
827 image->background_color.alpha=(double) ClampToQuantum(QuantumRange*
828 background.alpha/count);
831 MagickExport Image *DeskewImage(const Image *image,const double threshold,
832 ExceptionInfo *exception)
867 Compute deskew angle.
869 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
870 projection=(size_t *) AcquireQuantumMemory((size_t) (2*width-1),
871 sizeof(*projection));
872 if (projection == (size_t *) NULL)
873 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
874 status=RadonTransform(image,threshold,projection,exception);
875 if (status == MagickFalse)
877 projection=(size_t *) RelinquishMagickMemory(projection);
878 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
882 for (i=0; i < (ssize_t) (2*width-1); i++)
884 if (projection[i] > max_projection)
886 skew=i-(ssize_t) width+1;
887 max_projection=projection[i];
890 projection=(size_t *) RelinquishMagickMemory(projection);
891 degrees=RadiansToDegrees(-atan((double) skew/width/8));
892 if (image->debug != MagickFalse)
893 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
894 " Deskew angle: %g",degrees);
898 clone_image=CloneImage(image,0,0,MagickTrue,exception);
899 if (clone_image == (Image *) NULL)
900 return((Image *) NULL);
903 angle[MaxTextExtent];
905 (void) FormatLocaleString(angle,MaxTextExtent,"%.20g",degrees);
906 (void) SetImageArtifact(clone_image,"deskew:angle",angle);
908 (void) SetImageVirtualPixelMethod(clone_image,BackgroundVirtualPixelMethod,
910 affine_matrix.sx=cos(DegreesToRadians(fmod((double) degrees,360.0)));
911 affine_matrix.rx=sin(DegreesToRadians(fmod((double) degrees,360.0)));
912 affine_matrix.ry=(-sin(DegreesToRadians(fmod((double) degrees,360.0))));
913 affine_matrix.sy=cos(DegreesToRadians(fmod((double) degrees,360.0)));
914 affine_matrix.tx=0.0;
915 affine_matrix.ty=0.0;
916 artifact=GetImageArtifact(image,"deskew:auto-crop");
917 if (artifact == (const char *) NULL)
919 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
920 clone_image=DestroyImage(clone_image);
921 return(deskew_image);
926 GetImageBackgroundColor(clone_image,(ssize_t) StringToLong(artifact),
928 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
929 clone_image=DestroyImage(clone_image);
930 if (deskew_image == (Image *) NULL)
931 return((Image *) NULL);
932 median_image=StatisticImage(deskew_image,MedianStatistic,3,3,exception);
933 if (median_image == (Image *) NULL)
935 deskew_image=DestroyImage(deskew_image);
936 return((Image *) NULL);
938 geometry=GetImageBoundingBox(median_image,exception);
939 median_image=DestroyImage(median_image);
940 if (image->debug != MagickFalse)
941 (void) LogMagickEvent(TransformEvent,GetMagickModule()," Deskew geometry: "
942 "%.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
943 geometry.height,(double) geometry.x,(double) geometry.y);
944 crop_image=CropImage(deskew_image,&geometry,exception);
945 deskew_image=DestroyImage(deskew_image);
950 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
954 % I n t e g r a l R o t a t e I m a g e %
958 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
960 % IntegralRotateImage() rotates the image an integral of 90 degrees. It
961 % allocates the memory necessary for the new Image structure and returns a
962 % pointer to the rotated image.
964 % The format of the IntegralRotateImage method is:
966 % Image *IntegralRotateImage(const Image *image,size_t rotations,
967 % ExceptionInfo *exception)
969 % A description of each parameter follows.
971 % o image: the image.
973 % o rotations: Specifies the number of 90 degree rotations.
976 MagickExport Image *IntegralRotateImage(const Image *image,size_t rotations,
977 ExceptionInfo *exception)
979 #define RotateImageTag "Rotate/Image"
1001 Initialize rotated image attributes.
1003 assert(image != (Image *) NULL);
1007 return(CloneImage(image,0,0,MagickTrue,exception));
1008 if ((rotations == 1) || (rotations == 3))
1009 rotate_image=CloneImage(image,image->rows,image->columns,MagickTrue,
1012 rotate_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1014 if (rotate_image == (Image *) NULL)
1015 return((Image *) NULL);
1017 Integral rotate the image.
1021 image_view=AcquireVirtualCacheView(image,exception);
1022 rotate_view=AcquireAuthenticCacheView(rotate_image,exception);
1044 GetPixelCacheTileSize(image,&tile_width,&tile_height);
1045 tile_width=image->columns;
1046 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1047 #pragma omp parallel for schedule(static,4) shared(status) \
1048 magick_threads(image,image,1,1)
1050 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
1055 if (status == MagickFalse)
1058 for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
1063 register const Quantum
1077 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
1078 width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
1080 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
1081 height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
1082 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1084 if (p == (const Quantum *) NULL)
1089 for (y=0; y < (ssize_t) width; y++)
1091 register const Quantum
1092 *restrict tile_pixels;
1097 if (status == MagickFalse)
1099 q=QueueCacheViewAuthenticPixels(rotate_view,(ssize_t)
1100 (rotate_image->columns-(tile_y+height)),y+tile_x,height,1,
1102 if (q == (Quantum *) NULL)
1107 tile_pixels=p+((height-1)*width+y)*GetPixelChannels(image);
1108 for (x=0; x < (ssize_t) height; x++)
1113 if (GetPixelReadMask(image,tile_pixels) == 0)
1115 tile_pixels-=width*GetPixelChannels(image);
1116 q+=GetPixelChannels(rotate_image);
1119 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1121 PixelChannel channel=GetPixelChannelChannel(image,i);
1122 PixelTrait traits=GetPixelChannelTraits(image,channel);
1123 PixelTrait rotate_traits=GetPixelChannelTraits(rotate_image,
1125 if ((traits == UndefinedPixelTrait) ||
1126 (rotate_traits == UndefinedPixelTrait))
1128 SetPixelChannel(rotate_image,channel,tile_pixels[i],q);
1130 tile_pixels-=width*GetPixelChannels(image);
1131 q+=GetPixelChannels(rotate_image);
1133 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1134 if (sync == MagickFalse)
1138 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1143 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1144 #pragma omp critical (MagickCore_IntegralRotateImage)
1146 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
1148 if (proceed == MagickFalse)
1152 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1153 image->rows-1,image->rows);
1154 Swap(page.width,page.height);
1155 Swap(page.x,page.y);
1156 if (page.width != 0)
1157 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1165 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1166 #pragma omp parallel for schedule(static,4) shared(status) \
1167 magick_threads(image,image,1,1)
1169 for (y=0; y < (ssize_t) image->rows; y++)
1174 register const Quantum
1183 if (status == MagickFalse)
1185 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1186 q=QueueCacheViewAuthenticPixels(rotate_view,0,(ssize_t) (image->rows-y-
1187 1),image->columns,1,exception);
1188 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1193 q+=GetPixelChannels(rotate_image)*image->columns;
1194 for (x=0; x < (ssize_t) image->columns; x++)
1199 q-=GetPixelChannels(rotate_image);
1200 if (GetPixelReadMask(image,p) == 0)
1202 p+=GetPixelChannels(image);
1205 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1207 PixelChannel channel=GetPixelChannelChannel(image,i);
1208 PixelTrait traits=GetPixelChannelTraits(image,channel);
1209 PixelTrait rotate_traits=GetPixelChannelTraits(rotate_image,
1211 if ((traits == UndefinedPixelTrait) ||
1212 (rotate_traits == UndefinedPixelTrait))
1214 SetPixelChannel(rotate_image,channel,p[i],q);
1216 p+=GetPixelChannels(image);
1218 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1219 if (sync == MagickFalse)
1221 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1226 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1227 #pragma omp critical (MagickCore_IntegralRotateImage)
1229 proceed=SetImageProgress(image,RotateImageTag,progress++,
1231 if (proceed == MagickFalse)
1235 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1236 image->rows-1,image->rows);
1237 Swap(page.width,page.height);
1238 Swap(page.x,page.y);
1239 if (page.width != 0)
1240 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1255 GetPixelCacheTileSize(image,&tile_width,&tile_height);
1256 tile_width=image->columns;
1257 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1258 #pragma omp parallel for schedule(static,4) shared(status) \
1259 magick_threads(image,image,1,1)
1261 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
1266 if (status == MagickFalse)
1269 for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
1274 register const Quantum
1288 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
1289 width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
1291 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
1292 height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
1293 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1295 if (p == (const Quantum *) NULL)
1300 for (y=0; y < (ssize_t) width; y++)
1302 register const Quantum
1303 *restrict tile_pixels;
1308 if (status == MagickFalse)
1310 q=QueueCacheViewAuthenticPixels(rotate_view,tile_y,(ssize_t) (y+
1311 rotate_image->rows-(tile_x+width)),height,1,exception);
1312 if (q == (Quantum *) NULL)
1317 tile_pixels=p+((width-1)-y)*GetPixelChannels(image);
1318 for (x=0; x < (ssize_t) height; x++)
1323 if (GetPixelReadMask(image,tile_pixels) == 0)
1325 tile_pixels+=width*GetPixelChannels(image);
1326 q+=GetPixelChannels(rotate_image);
1329 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1331 PixelChannel channel=GetPixelChannelChannel(image,i);
1332 PixelTrait traits=GetPixelChannelTraits(image,channel);
1333 PixelTrait rotate_traits=GetPixelChannelTraits(rotate_image,
1335 if ((traits == UndefinedPixelTrait) ||
1336 (rotate_traits == UndefinedPixelTrait))
1338 SetPixelChannel(rotate_image,channel,tile_pixels[i],q);
1340 tile_pixels+=width*GetPixelChannels(image);
1341 q+=GetPixelChannels(rotate_image);
1343 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1344 #pragma omp critical (MagickCore_IntegralRotateImage)
1346 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1347 if (sync == MagickFalse)
1351 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1356 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
1358 if (proceed == MagickFalse)
1362 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1363 image->rows-1,image->rows);
1364 Swap(page.width,page.height);
1365 Swap(page.x,page.y);
1366 if (page.width != 0)
1367 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1371 rotate_view=DestroyCacheView(rotate_view);
1372 image_view=DestroyCacheView(image_view);
1373 rotate_image->type=image->type;
1374 rotate_image->page=page;
1375 if (status == MagickFalse)
1376 rotate_image=DestroyImage(rotate_image);
1377 return(rotate_image);
1381 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1385 + X S h e a r I m a g e %
1389 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1391 % XShearImage() shears the image in the X direction with a shear angle of
1392 % 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1393 % negative angles shear clockwise. Angles are measured relative to a vertical
1394 % Y-axis. X shears will widen an image creating 'empty' triangles on the left
1395 % and right sides of the source image.
1397 % The format of the XShearImage method is:
1399 % MagickBooleanType XShearImage(Image *image,const double degrees,
1400 % const size_t width,const size_t height,
1401 % const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
1403 % A description of each parameter follows.
1405 % o image: the image.
1407 % o degrees: A double representing the shearing angle along the X
1410 % o width, height, x_offset, y_offset: Defines a region of the image
1413 % o exception: return any errors or warnings in this structure.
1416 static MagickBooleanType XShearImage(Image *image,const double degrees,
1417 const size_t width,const size_t height,const ssize_t x_offset,
1418 const ssize_t y_offset,ExceptionInfo *exception)
1420 #define XShearImageTag "XShear/Image"
1446 assert(image != (Image *) NULL);
1447 assert(image->signature == MagickSignature);
1448 if (image->debug != MagickFalse)
1449 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1451 background=image->background_color;
1453 image_view=AcquireAuthenticCacheView(image,exception);
1454 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1455 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1456 magick_threads(image,image,height,1)
1458 for (y=0; y < (ssize_t) height; y++)
1482 if (status == MagickFalse)
1484 p=GetCacheViewAuthenticPixels(image_view,0,y_offset+y,image->columns,1,
1486 if (p == (Quantum *) NULL)
1491 p+=x_offset*GetPixelChannels(image);
1492 displacement=degrees*(double) (y-height/2.0);
1493 if (displacement == 0.0)
1495 if (displacement > 0.0)
1499 displacement*=(-1.0);
1502 step=(ssize_t) floor((double) displacement);
1503 area=(double) (displacement-step);
1506 GetPixelInfo(image,&source);
1507 GetPixelInfo(image,&destination);
1513 Transfer pixels left-to-right.
1515 if (step > x_offset)
1517 q=p-step*GetPixelChannels(image);
1518 for (i=0; i < (ssize_t) width; i++)
1520 if ((x_offset+i) < step)
1522 p+=GetPixelChannels(image);
1523 GetPixelInfoPixel(image,p,&pixel);
1524 q+=GetPixelChannels(image);
1527 GetPixelInfoPixel(image,p,&source);
1528 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1529 &source,(double) GetPixelAlpha(image,p),area,&destination);
1530 SetPixelInfoPixel(image,&destination,q);
1531 GetPixelInfoPixel(image,p,&pixel);
1532 p+=GetPixelChannels(image);
1533 q+=GetPixelChannels(image);
1535 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1536 &background,(double) background.alpha,area,&destination);
1537 SetPixelInfoPixel(image,&destination,q);
1538 q+=GetPixelChannels(image);
1539 for (i=0; i < (step-1); i++)
1541 SetPixelInfoPixel(image,&background,q);
1542 q+=GetPixelChannels(image);
1549 Transfer pixels right-to-left.
1551 p+=width*GetPixelChannels(image);
1552 q=p+step*GetPixelChannels(image);
1553 for (i=0; i < (ssize_t) width; i++)
1555 p-=GetPixelChannels(image);
1556 q-=GetPixelChannels(image);
1557 if ((size_t) (x_offset+width+step-i) >= image->columns)
1559 GetPixelInfoPixel(image,p,&source);
1560 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1561 &source,(double) GetPixelAlpha(image,p),area,&destination);
1562 SetPixelInfoPixel(image,&destination,q);
1563 GetPixelInfoPixel(image,p,&pixel);
1565 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1566 &background,(double) background.alpha,area,&destination);
1567 q-=GetPixelChannels(image);
1568 SetPixelInfoPixel(image,&destination,q);
1569 for (i=0; i < (step-1); i++)
1571 q-=GetPixelChannels(image);
1572 SetPixelInfoPixel(image,&background,q);
1577 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1579 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1584 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1585 #pragma omp critical (MagickCore_XShearImage)
1587 proceed=SetImageProgress(image,XShearImageTag,progress++,height);
1588 if (proceed == MagickFalse)
1592 image_view=DestroyCacheView(image_view);
1597 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1601 + Y S h e a r I m a g e %
1605 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1607 % YShearImage shears the image in the Y direction with a shear angle of
1608 % 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1609 % negative angles shear clockwise. Angles are measured relative to a
1610 % horizontal X-axis. Y shears will increase the height of an image creating
1611 % 'empty' triangles on the top and bottom of the source image.
1613 % The format of the YShearImage method is:
1615 % MagickBooleanType YShearImage(Image *image,const double degrees,
1616 % const size_t width,const size_t height,
1617 % const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
1619 % A description of each parameter follows.
1621 % o image: the image.
1623 % o degrees: A double representing the shearing angle along the Y
1626 % o width, height, x_offset, y_offset: Defines a region of the image
1629 % o exception: return any errors or warnings in this structure.
1632 static MagickBooleanType YShearImage(Image *image,const double degrees,
1633 const size_t width,const size_t height,const ssize_t x_offset,
1634 const ssize_t y_offset,ExceptionInfo *exception)
1636 #define YShearImageTag "YShear/Image"
1662 assert(image != (Image *) NULL);
1663 assert(image->signature == MagickSignature);
1664 if (image->debug != MagickFalse)
1665 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1668 background=image->background_color;
1669 image_view=AcquireAuthenticCacheView(image,exception);
1670 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1671 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1672 magick_threads(image,image,width,1)
1674 for (x=0; x < (ssize_t) width; x++)
1698 if (status == MagickFalse)
1700 p=GetCacheViewAuthenticPixels(image_view,x_offset+x,0,1,image->rows,
1702 if (p == (Quantum *) NULL)
1707 p+=y_offset*GetPixelChannels(image);
1708 displacement=degrees*(double) (x-width/2.0);
1709 if (displacement == 0.0)
1711 if (displacement > 0.0)
1715 displacement*=(-1.0);
1718 step=(ssize_t) floor((double) displacement);
1719 area=(double) (displacement-step);
1722 GetPixelInfo(image,&source);
1723 GetPixelInfo(image,&destination);
1729 Transfer pixels top-to-bottom.
1731 if (step > y_offset)
1733 q=p-step*GetPixelChannels(image);
1734 for (i=0; i < (ssize_t) height; i++)
1736 if ((y_offset+i) < step)
1738 p+=GetPixelChannels(image);
1739 GetPixelInfoPixel(image,p,&pixel);
1740 q+=GetPixelChannels(image);
1743 GetPixelInfoPixel(image,p,&source);
1744 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1745 &source,(double) GetPixelAlpha(image,p),area,
1747 SetPixelInfoPixel(image,&destination,q);
1748 GetPixelInfoPixel(image,p,&pixel);
1749 p+=GetPixelChannels(image);
1750 q+=GetPixelChannels(image);
1752 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1753 &background,(double) background.alpha,area,&destination);
1754 SetPixelInfoPixel(image,&destination,q);
1755 q+=GetPixelChannels(image);
1756 for (i=0; i < (step-1); i++)
1758 SetPixelInfoPixel(image,&background,q);
1759 q+=GetPixelChannels(image);
1766 Transfer pixels bottom-to-top.
1768 p+=height*GetPixelChannels(image);
1769 q=p+step*GetPixelChannels(image);
1770 for (i=0; i < (ssize_t) height; i++)
1772 p-=GetPixelChannels(image);
1773 q-=GetPixelChannels(image);
1774 if ((size_t) (y_offset+height+step-i) >= image->rows)
1776 GetPixelInfoPixel(image,p,&source);
1777 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1778 &source,(double) GetPixelAlpha(image,p),area,
1780 SetPixelInfoPixel(image,&destination,q);
1781 GetPixelInfoPixel(image,p,&pixel);
1783 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1784 &background,(double) background.alpha,area,&destination);
1785 q-=GetPixelChannels(image);
1786 SetPixelInfoPixel(image,&destination,q);
1787 for (i=0; i < (step-1); i++)
1789 q-=GetPixelChannels(image);
1790 SetPixelInfoPixel(image,&background,q);
1795 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1797 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1802 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1803 #pragma omp critical (MagickCore_YShearImage)
1805 proceed=SetImageProgress(image,YShearImageTag,progress++,image->rows);
1806 if (proceed == MagickFalse)
1810 image_view=DestroyCacheView(image_view);
1815 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1819 % S h e a r I m a g e %
1823 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1825 % ShearImage() creates a new image that is a shear_image copy of an existing
1826 % one. Shearing slides one edge of an image along the X or Y axis, creating
1827 % a parallelogram. An X direction shear slides an edge along the X axis,
1828 % while a Y direction shear slides an edge along the Y axis. The amount of
1829 % the shear is controlled by a shear angle. For X direction shears, x_shear
1830 % is measured relative to the Y axis, and similarly, for Y direction shears
1831 % y_shear is measured relative to the X axis. Empty triangles left over from
1832 % shearing the image are filled with the background color defined by member
1833 % 'background_color' of the image.. ShearImage() allocates the memory
1834 % necessary for the new Image structure and returns a pointer to the new image.
1836 % ShearImage() is based on the paper "A Fast Algorithm for General Raster
1837 % Rotatation" by Alan W. Paeth.
1839 % The format of the ShearImage method is:
1841 % Image *ShearImage(const Image *image,const double x_shear,
1842 % const double y_shear,ExceptionInfo *exception)
1844 % A description of each parameter follows.
1846 % o image: the image.
1848 % o x_shear, y_shear: Specifies the number of degrees to shear the image.
1850 % o exception: return any errors or warnings in this structure.
1853 MagickExport Image *ShearImage(const Image *image,const double x_shear,
1854 const double y_shear,ExceptionInfo *exception)
1876 assert(image != (Image *) NULL);
1877 assert(image->signature == MagickSignature);
1878 if (image->debug != MagickFalse)
1879 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1880 assert(exception != (ExceptionInfo *) NULL);
1881 assert(exception->signature == MagickSignature);
1882 if ((x_shear != 0.0) && (fmod(x_shear,90.0) == 0.0))
1883 ThrowImageException(ImageError,"AngleIsDiscontinuous");
1884 if ((y_shear != 0.0) && (fmod(y_shear,90.0) == 0.0))
1885 ThrowImageException(ImageError,"AngleIsDiscontinuous");
1887 Initialize shear angle.
1889 integral_image=CloneImage(image,0,0,MagickTrue,exception);
1890 if (integral_image == (Image *) NULL)
1891 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1892 shear.x=(-tan(DegreesToRadians(fmod(x_shear,360.0))));
1893 shear.y=tan(DegreesToRadians(fmod(y_shear,360.0)));
1894 if ((shear.x == 0.0) && (shear.y == 0.0))
1895 return(integral_image);
1896 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
1898 integral_image=DestroyImage(integral_image);
1899 return(integral_image);
1901 if (integral_image->alpha_trait != BlendPixelTrait)
1902 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
1906 y_width=image->columns+(ssize_t) floor(fabs(shear.x)*image->rows+0.5);
1907 x_offset=(ssize_t) ceil((double) image->columns+((fabs(shear.x)*image->rows)-
1908 image->columns)/2.0-0.5);
1909 y_offset=(ssize_t) ceil((double) image->rows+((fabs(shear.y)*y_width)-
1910 image->rows)/2.0-0.5);
1912 Surround image with border.
1914 integral_image->border_color=integral_image->background_color;
1915 integral_image->compose=CopyCompositeOp;
1916 border_info.width=(size_t) x_offset;
1917 border_info.height=(size_t) y_offset;
1918 shear_image=BorderImage(integral_image,&border_info,image->compose,exception);
1919 integral_image=DestroyImage(integral_image);
1920 if (shear_image == (Image *) NULL)
1921 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1925 if (shear_image->alpha_trait != BlendPixelTrait)
1926 (void) SetImageAlphaChannel(shear_image,OpaqueAlphaChannel,exception);
1927 status=XShearImage(shear_image,shear.x,image->columns,image->rows,x_offset,
1928 (ssize_t) (shear_image->rows-image->rows)/2,exception);
1929 if (status == MagickFalse)
1931 shear_image=DestroyImage(shear_image);
1932 return((Image *) NULL);
1934 status=YShearImage(shear_image,shear.y,y_width,image->rows,(ssize_t)
1935 (shear_image->columns-y_width)/2,y_offset,exception);
1936 if (status == MagickFalse)
1938 shear_image=DestroyImage(shear_image);
1939 return((Image *) NULL);
1941 status=CropToFitImage(&shear_image,shear.x,shear.y,(double)
1942 image->columns,(double) image->rows,MagickFalse,exception);
1943 shear_image->compose=image->compose;
1944 shear_image->page.width=0;
1945 shear_image->page.height=0;
1946 if (status == MagickFalse)
1947 shear_image=DestroyImage(shear_image);
1948 return(shear_image);
1952 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1956 % S h e a r R o t a t e I m a g e %
1960 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1962 % ShearRotateImage() creates a new image that is a rotated copy of an existing
1963 % one. Positive angles rotate counter-clockwise (right-hand rule), while
1964 % negative angles rotate clockwise. Rotated images are usually larger than
1965 % the originals and have 'empty' triangular corners. X axis. Empty
1966 % triangles left over from shearing the image are filled with the background
1967 % color defined by member 'background_color' of the image. ShearRotateImage
1968 % allocates the memory necessary for the new Image structure and returns a
1969 % pointer to the new image.
1971 % ShearRotateImage() is based on the paper "A Fast Algorithm for General
1972 % Raster Rotatation" by Alan W. Paeth. ShearRotateImage is adapted from a
1973 % similar method based on the Paeth paper written by Michael Halle of the
1974 % Spatial Imaging Group, MIT Media Lab.
1976 % The format of the ShearRotateImage method is:
1978 % Image *ShearRotateImage(const Image *image,const double degrees,
1979 % ExceptionInfo *exception)
1981 % A description of each parameter follows.
1983 % o image: the image.
1985 % o degrees: Specifies the number of degrees to rotate the image.
1987 % o exception: return any errors or warnings in this structure.
1990 MagickExport Image *ShearRotateImage(const Image *image,const double degrees,
1991 ExceptionInfo *exception)
2020 Adjust rotation angle.
2022 assert(image != (Image *) NULL);
2023 assert(image->signature == MagickSignature);
2024 if (image->debug != MagickFalse)
2025 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2026 assert(exception != (ExceptionInfo *) NULL);
2027 assert(exception->signature == MagickSignature);
2029 while (angle < -45.0)
2031 for (rotations=0; angle > 45.0; rotations++)
2035 Calculate shear equations.
2037 integral_image=IntegralRotateImage(image,rotations,exception);
2038 if (integral_image == (Image *) NULL)
2039 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2040 shear.x=(-tan((double) DegreesToRadians(angle)/2.0));
2041 shear.y=sin((double) DegreesToRadians(angle));
2042 if ((shear.x == 0.0) && (shear.y == 0.0))
2043 return(integral_image);
2044 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
2046 integral_image=DestroyImage(integral_image);
2047 return(integral_image);
2049 if (integral_image->alpha_trait != BlendPixelTrait)
2050 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
2054 width=image->columns;
2056 if ((rotations == 1) || (rotations == 3))
2059 height=image->columns;
2061 y_width=width+(ssize_t) floor(fabs(shear.x)*height+0.5);
2062 x_offset=(ssize_t) ceil((double) width+((fabs(shear.y)*height)-width)/2.0-
2064 y_offset=(ssize_t) ceil((double) height+((fabs(shear.y)*y_width)-height)/2.0-
2067 Surround image with a border.
2069 integral_image->border_color=integral_image->background_color;
2070 integral_image->compose=CopyCompositeOp;
2071 border_info.width=(size_t) x_offset;
2072 border_info.height=(size_t) y_offset;
2073 rotate_image=BorderImage(integral_image,&border_info,image->compose,
2075 integral_image=DestroyImage(integral_image);
2076 if (rotate_image == (Image *) NULL)
2077 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2081 status=XShearImage(rotate_image,shear.x,width,height,x_offset,(ssize_t)
2082 (rotate_image->rows-height)/2,exception);
2083 if (status == MagickFalse)
2085 rotate_image=DestroyImage(rotate_image);
2086 return((Image *) NULL);
2088 status=YShearImage(rotate_image,shear.y,y_width,height,(ssize_t)
2089 (rotate_image->columns-y_width)/2,y_offset,exception);
2090 if (status == MagickFalse)
2092 rotate_image=DestroyImage(rotate_image);
2093 return((Image *) NULL);
2095 status=XShearImage(rotate_image,shear.x,y_width,rotate_image->rows,(ssize_t)
2096 (rotate_image->columns-y_width)/2,0,exception);
2097 if (status == MagickFalse)
2099 rotate_image=DestroyImage(rotate_image);
2100 return((Image *) NULL);
2102 status=CropToFitImage(&rotate_image,shear.x,shear.y,(double) width,
2103 (double) height,MagickTrue,exception);
2104 rotate_image->compose=image->compose;
2105 rotate_image->page.width=0;
2106 rotate_image->page.height=0;
2107 if (status == MagickFalse)
2108 rotate_image=DestroyImage(rotate_image);
2109 return(rotate_image);