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".
200 % The format of the DeskewImage method is:
202 % Image *DeskewImage(const Image *image,const double threshold,
203 % ExceptionInfo *exception)
205 % A description of each parameter follows:
207 % o image: the image.
209 % o threshold: separate background from foreground.
211 % o exception: return any errors or warnings in this structure.
215 typedef struct _RadonInfo
240 static RadonInfo *DestroyRadonInfo(RadonInfo *radon_info)
242 assert(radon_info != (RadonInfo *) NULL);
243 switch (radon_info->type)
247 if (radon_info->mapped == MagickFalse)
248 radon_info->cells=(unsigned short *) RelinquishMagickMemory(
251 radon_info->cells=(unsigned short *) UnmapBlob(radon_info->cells,
252 (size_t) radon_info->length);
253 RelinquishMagickResource(MemoryResource,radon_info->length);
258 radon_info->cells=(unsigned short *) UnmapBlob(radon_info->cells,(size_t)
260 RelinquishMagickResource(MapResource,radon_info->length);
264 if (radon_info->file != -1)
265 (void) close(radon_info->file);
266 (void) RelinquishUniqueFileResource(radon_info->path);
267 RelinquishMagickResource(DiskResource,radon_info->length);
273 return((RadonInfo *) RelinquishMagickMemory(radon_info));
276 static MagickBooleanType ResetRadonCells(RadonInfo *radon_info)
288 if (radon_info->type != DiskCache)
290 (void) ResetMagickMemory(radon_info->cells,0,(size_t) radon_info->length);
294 (void) lseek(radon_info->file,0,SEEK_SET);
295 for (y=0; y < (ssize_t) radon_info->height; y++)
297 for (x=0; x < (ssize_t) radon_info->width; x++)
299 count=write(radon_info->file,&value,sizeof(*radon_info->cells));
300 if (count != (ssize_t) sizeof(*radon_info->cells))
303 if (x < (ssize_t) radon_info->width)
306 return(y < (ssize_t) radon_info->height ? MagickFalse : MagickTrue);
309 static RadonInfo *AcquireRadonInfo(const Image *image,const size_t width,
310 const size_t height,ExceptionInfo *exception)
318 radon_info=(RadonInfo *) AcquireMagickMemory(sizeof(*radon_info));
319 if (radon_info == (RadonInfo *) NULL)
320 return((RadonInfo *) NULL);
321 (void) ResetMagickMemory(radon_info,0,sizeof(*radon_info));
322 radon_info->width=width;
323 radon_info->height=height;
324 radon_info->length=(MagickSizeType) width*height*sizeof(*radon_info->cells);
325 radon_info->type=MemoryCache;
326 status=AcquireMagickResource(AreaResource,radon_info->length);
327 if ((status != MagickFalse) &&
328 (radon_info->length == (MagickSizeType) ((size_t) radon_info->length)))
330 status=AcquireMagickResource(MemoryResource,radon_info->length);
331 if (status != MagickFalse)
333 radon_info->mapped=MagickFalse;
334 radon_info->cells=(unsigned short *) AcquireMagickMemory((size_t)
336 if (radon_info->cells == (unsigned short *) NULL)
338 radon_info->mapped=MagickTrue;
339 radon_info->cells=(unsigned short *) MapBlob(-1,IOMode,0,(size_t)
342 if (radon_info->cells == (unsigned short *) NULL)
343 RelinquishMagickResource(MemoryResource,radon_info->length);
346 radon_info->file=(-1);
347 if (radon_info->cells == (unsigned short *) NULL)
349 status=AcquireMagickResource(DiskResource,radon_info->length);
350 if (status == MagickFalse)
352 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
353 "CacheResourcesExhausted","`%s'",image->filename);
354 return(DestroyRadonInfo(radon_info));
356 radon_info->type=DiskCache;
357 (void) AcquireMagickResource(MemoryResource,radon_info->length);
358 radon_info->file=AcquireUniqueFileResource(radon_info->path);
359 if (radon_info->file == -1)
360 return(DestroyRadonInfo(radon_info));
361 status=AcquireMagickResource(MapResource,radon_info->length);
362 if (status != MagickFalse)
364 status=ResetRadonCells(radon_info);
365 if (status != MagickFalse)
367 radon_info->cells=(unsigned short *) MapBlob(radon_info->file,
368 IOMode,0,(size_t) radon_info->length);
369 if (radon_info->cells != (unsigned short *) NULL)
370 radon_info->type=MapCache;
372 RelinquishMagickResource(MapResource,radon_info->length);
379 static inline size_t MagickMin(const size_t x,const size_t y)
386 static inline ssize_t ReadRadonCell(const RadonInfo *radon_info,
387 const MagickOffsetType offset,const size_t length,unsigned char *buffer)
395 #if !defined(MAGICKCORE_HAVE_PPREAD)
396 #if defined(MAGICKCORE_OPENMP_SUPPORT)
397 #pragma omp critical (MagickCore_ReadRadonCell)
401 if (lseek(radon_info->file,offset,SEEK_SET) >= 0)
405 for (i=0; i < (ssize_t) length; i+=count)
407 #if !defined(MAGICKCORE_HAVE_PPREAD)
408 count=read(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
411 count=pread(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
412 SSIZE_MAX),offset+i);
423 #if !defined(MAGICKCORE_HAVE_PPREAD)
430 static inline ssize_t WriteRadonCell(const RadonInfo *radon_info,
431 const MagickOffsetType offset,const size_t length,const unsigned char *buffer)
439 #if !defined(MAGICKCORE_HAVE_PWRITE)
440 #if defined(MAGICKCORE_OPENMP_SUPPORT)
441 #pragma omp critical (MagickCore_WriteRadonCell)
444 if (lseek(radon_info->file,offset,SEEK_SET) >= 0)
448 for (i=0; i < (ssize_t) length; i+=count)
450 #if !defined(MAGICKCORE_HAVE_PWRITE)
451 count=write(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
454 count=pwrite(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
455 SSIZE_MAX),offset+i);
466 #if !defined(MAGICKCORE_HAVE_PWRITE)
473 static inline unsigned short GetRadonCell(const RadonInfo *radon_info,
474 const ssize_t x,const ssize_t y)
482 i=(MagickOffsetType) radon_info->height*x+y;
484 ((MagickSizeType) (i*sizeof(*radon_info->cells)) >= radon_info->length))
486 if (radon_info->type != DiskCache)
487 return(radon_info->cells[i]);
489 (void) ReadRadonCell(radon_info,i*sizeof(*radon_info->cells),
490 sizeof(*radon_info->cells),(unsigned char *) &value);
494 static inline MagickBooleanType SetRadonCell(const RadonInfo *radon_info,
495 const ssize_t x,const ssize_t y,const unsigned short value)
503 i=(MagickOffsetType) radon_info->height*x+y;
505 ((MagickSizeType) (i*sizeof(*radon_info->cells)) >= radon_info->length))
507 if (radon_info->type != DiskCache)
509 radon_info->cells[i]=value;
512 count=WriteRadonCell(radon_info,i*sizeof(*radon_info->cells),
513 sizeof(*radon_info->cells),(const unsigned char *) &value);
514 if (count != (ssize_t) sizeof(*radon_info->cells))
519 static void RadonProjection(const Image *image,RadonInfo *source_cells,
520 RadonInfo *destination_cells,const ssize_t sign,size_t *projection)
537 for (step=1; step < p->width; step*=2)
539 for (x=0; x < (ssize_t) p->width; x+=2*(ssize_t) step)
550 for (i=0; i < (ssize_t) step; i++)
552 for (y=0; y < (ssize_t) (p->height-i-1); y++)
554 cell=GetRadonCell(p,x+i,y);
555 (void) SetRadonCell(q,x+2*i,y,cell+GetRadonCell(p,x+i+(ssize_t)
557 (void) SetRadonCell(q,x+2*i+1,y,cell+GetRadonCell(p,x+i+(ssize_t)
560 for ( ; y < (ssize_t) (p->height-i); y++)
562 cell=GetRadonCell(p,x+i,y);
563 (void) SetRadonCell(q,x+2*i,y,cell+GetRadonCell(p,x+i+(ssize_t) step,
565 (void) SetRadonCell(q,x+2*i+1,y,cell);
567 for ( ; y < (ssize_t) p->height; y++)
569 cell=GetRadonCell(p,x+i,y);
570 (void) SetRadonCell(q,x+2*i,y,cell);
571 (void) SetRadonCell(q,x+2*i+1,y,cell);
579 #if defined(MAGICKCORE_OPENMP_SUPPORT)
580 #pragma omp parallel for schedule(static,4) \
581 magick_threads(image,image,1,1)
583 for (x=0; x < (ssize_t) p->width; x++)
592 for (y=0; y < (ssize_t) (p->height-1); y++)
597 delta=GetRadonCell(p,x,y)-(ssize_t) GetRadonCell(p,x,y+1);
600 projection[p->width+sign*x-1]=sum;
604 static MagickBooleanType RadonTransform(const Image *image,
605 const double threshold,size_t *projection,ExceptionInfo *exception)
633 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
634 source_cells=AcquireRadonInfo(image,width,image->rows,exception);
635 destination_cells=AcquireRadonInfo(image,width,image->rows,exception);
636 if ((source_cells == (RadonInfo *) NULL) ||
637 (destination_cells == (RadonInfo *) NULL))
639 if (destination_cells != (RadonInfo *) NULL)
640 destination_cells=DestroyRadonInfo(destination_cells);
641 if (source_cells != (RadonInfo *) NULL)
642 source_cells=DestroyRadonInfo(source_cells);
645 if (ResetRadonCells(source_cells) == MagickFalse)
647 destination_cells=DestroyRadonInfo(destination_cells);
648 source_cells=DestroyRadonInfo(source_cells);
651 for (i=0; i < 256; i++)
653 byte=(unsigned char) i;
654 for (count=0; byte != 0; byte>>=1)
656 bits[i]=(unsigned short) count;
659 image_view=AcquireVirtualCacheView(image,exception);
660 #if defined(MAGICKCORE_OPENMP_SUPPORT)
661 #pragma omp parallel for schedule(static,4) shared(status) \
662 magick_threads(image,image,image->rows,1)
664 for (y=0; y < (ssize_t) image->rows; y++)
666 register const Quantum
677 if (status == MagickFalse)
679 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
680 if (p == (const Quantum *) NULL)
687 i=(ssize_t) (image->columns+7)/8;
688 for (x=0; x < (ssize_t) image->columns; x++)
691 if (GetPixelIntensity(image,p) < threshold)
696 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
700 p+=GetPixelChannels(image);
705 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
708 RadonProjection(image,source_cells,destination_cells,-1,projection);
709 (void) ResetRadonCells(source_cells);
710 #if defined(MAGICKCORE_OPENMP_SUPPORT)
711 #pragma omp parallel for schedule(static,4) shared(status) \
712 magick_threads(image,image,1,1)
714 for (y=0; y < (ssize_t) image->rows; y++)
716 register const Quantum
727 if (status == MagickFalse)
729 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
730 if (p == (const Quantum *) NULL)
738 for (x=0; x < (ssize_t) image->columns; x++)
741 if (GetPixelIntensity(image,p) < threshold)
746 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
750 p+=GetPixelChannels(image);
755 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
758 RadonProjection(image,source_cells,destination_cells,1,projection);
759 image_view=DestroyCacheView(image_view);
760 destination_cells=DestroyRadonInfo(destination_cells);
761 source_cells=DestroyRadonInfo(source_cells);
765 static void GetImageBackgroundColor(Image *image,const ssize_t offset,
766 ExceptionInfo *exception)
781 Compute average background color.
785 GetPixelInfo(image,&background);
787 image_view=AcquireVirtualCacheView(image,exception);
788 for (y=0; y < (ssize_t) image->rows; y++)
790 register const Quantum
796 if ((y >= offset) && (y < ((ssize_t) image->rows-offset)))
798 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
799 if (p == (const Quantum *) NULL)
801 for (x=0; x < (ssize_t) image->columns; x++)
803 if ((x >= offset) && (x < ((ssize_t) image->columns-offset)))
805 background.red+=QuantumScale*GetPixelRed(image,p);
806 background.green+=QuantumScale*GetPixelGreen(image,p);
807 background.blue+=QuantumScale*GetPixelBlue(image,p);
808 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
809 background.alpha+=QuantumScale*GetPixelAlpha(image,p);
811 p+=GetPixelChannels(image);
814 image_view=DestroyCacheView(image_view);
815 image->background_color.red=(double) ClampToQuantum(QuantumRange*
816 background.red/count);
817 image->background_color.green=(double) ClampToQuantum(QuantumRange*
818 background.green/count);
819 image->background_color.blue=(double) ClampToQuantum(QuantumRange*
820 background.blue/count);
821 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
822 image->background_color.alpha=(double) ClampToQuantum(QuantumRange*
823 background.alpha/count);
826 MagickExport Image *DeskewImage(const Image *image,const double threshold,
827 ExceptionInfo *exception)
862 Compute deskew angle.
864 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
865 projection=(size_t *) AcquireQuantumMemory((size_t) (2*width-1),
866 sizeof(*projection));
867 if (projection == (size_t *) NULL)
868 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
869 status=RadonTransform(image,threshold,projection,exception);
870 if (status == MagickFalse)
872 projection=(size_t *) RelinquishMagickMemory(projection);
873 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
877 for (i=0; i < (ssize_t) (2*width-1); i++)
879 if (projection[i] > max_projection)
881 skew=i-(ssize_t) width+1;
882 max_projection=projection[i];
885 projection=(size_t *) RelinquishMagickMemory(projection);
886 degrees=RadiansToDegrees(-atan((double) skew/width/8));
887 if (image->debug != MagickFalse)
888 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
889 " Deskew angle: %g",degrees);
893 clone_image=CloneImage(image,0,0,MagickTrue,exception);
894 if (clone_image == (Image *) NULL)
895 return((Image *) NULL);
898 angle[MaxTextExtent];
900 (void) FormatLocaleString(angle,MaxTextExtent,"%.20g",degrees);
901 (void) SetImageArtifact(clone_image,"deskew:angle",angle);
903 (void) SetImageVirtualPixelMethod(clone_image,BackgroundVirtualPixelMethod,
905 affine_matrix.sx=cos(DegreesToRadians(fmod((double) degrees,360.0)));
906 affine_matrix.rx=sin(DegreesToRadians(fmod((double) degrees,360.0)));
907 affine_matrix.ry=(-sin(DegreesToRadians(fmod((double) degrees,360.0))));
908 affine_matrix.sy=cos(DegreesToRadians(fmod((double) degrees,360.0)));
909 affine_matrix.tx=0.0;
910 affine_matrix.ty=0.0;
911 artifact=GetImageArtifact(image,"deskew:auto-crop");
912 if (artifact == (const char *) NULL)
914 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
915 clone_image=DestroyImage(clone_image);
916 return(deskew_image);
921 GetImageBackgroundColor(clone_image,(ssize_t) StringToLong(artifact),
923 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
924 clone_image=DestroyImage(clone_image);
925 if (deskew_image == (Image *) NULL)
926 return((Image *) NULL);
927 median_image=StatisticImage(deskew_image,MedianStatistic,3,3,exception);
928 if (median_image == (Image *) NULL)
930 deskew_image=DestroyImage(deskew_image);
931 return((Image *) NULL);
933 geometry=GetImageBoundingBox(median_image,exception);
934 median_image=DestroyImage(median_image);
935 if (image->debug != MagickFalse)
936 (void) LogMagickEvent(TransformEvent,GetMagickModule()," Deskew geometry: "
937 "%.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
938 geometry.height,(double) geometry.x,(double) geometry.y);
939 crop_image=CropImage(deskew_image,&geometry,exception);
940 deskew_image=DestroyImage(deskew_image);
945 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
949 % I n t e g r a l R o t a t e I m a g e %
953 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
955 % IntegralRotateImage() rotates the image an integral of 90 degrees. It
956 % allocates the memory necessary for the new Image structure and returns a
957 % pointer to the rotated image.
959 % The format of the IntegralRotateImage method is:
961 % Image *IntegralRotateImage(const Image *image,size_t rotations,
962 % ExceptionInfo *exception)
964 % A description of each parameter follows.
966 % o image: the image.
968 % o rotations: Specifies the number of 90 degree rotations.
971 MagickExport Image *IntegralRotateImage(const Image *image,size_t rotations,
972 ExceptionInfo *exception)
974 #define RotateImageTag "Rotate/Image"
996 Initialize rotated image attributes.
998 assert(image != (Image *) NULL);
1002 return(CloneImage(image,0,0,MagickTrue,exception));
1003 if ((rotations == 1) || (rotations == 3))
1004 rotate_image=CloneImage(image,image->rows,image->columns,MagickTrue,
1007 rotate_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1009 if (rotate_image == (Image *) NULL)
1010 return((Image *) NULL);
1012 Integral rotate the image.
1016 image_view=AcquireVirtualCacheView(image,exception);
1017 rotate_view=AcquireAuthenticCacheView(rotate_image,exception);
1039 GetPixelCacheTileSize(image,&tile_width,&tile_height);
1040 tile_width=image->columns;
1041 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1042 #pragma omp parallel for schedule(static,4) shared(status) \
1043 magick_threads(image,image,1,1)
1045 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
1050 if (status == MagickFalse)
1053 for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
1058 register const Quantum
1072 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
1073 width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
1075 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
1076 height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
1077 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1079 if (p == (const Quantum *) NULL)
1084 for (y=0; y < (ssize_t) width; y++)
1086 register const Quantum
1087 *restrict tile_pixels;
1092 if (status == MagickFalse)
1094 q=QueueCacheViewAuthenticPixels(rotate_view,(ssize_t)
1095 (rotate_image->columns-(tile_y+height)),y+tile_x,height,1,
1097 if (q == (Quantum *) NULL)
1102 tile_pixels=p+((height-1)*width+y)*GetPixelChannels(image);
1103 for (x=0; x < (ssize_t) height; x++)
1108 if (GetPixelReadMask(image,tile_pixels) == 0)
1110 tile_pixels-=width*GetPixelChannels(image);
1111 q+=GetPixelChannels(rotate_image);
1114 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1116 PixelChannel channel=GetPixelChannelChannel(image,i);
1117 PixelTrait traits=GetPixelChannelTraits(image,channel);
1118 PixelTrait rotate_traits=GetPixelChannelTraits(rotate_image,
1120 if ((traits == UndefinedPixelTrait) ||
1121 (rotate_traits == UndefinedPixelTrait))
1123 SetPixelChannel(rotate_image,channel,tile_pixels[i],q);
1125 tile_pixels-=width*GetPixelChannels(image);
1126 q+=GetPixelChannels(rotate_image);
1128 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1129 if (sync == MagickFalse)
1133 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1138 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1139 #pragma omp critical (MagickCore_IntegralRotateImage)
1141 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
1143 if (proceed == MagickFalse)
1147 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1148 image->rows-1,image->rows);
1149 Swap(page.width,page.height);
1150 Swap(page.x,page.y);
1151 if (page.width != 0)
1152 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1160 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1161 #pragma omp parallel for schedule(static,4) shared(status) \
1162 magick_threads(image,image,1,1)
1164 for (y=0; y < (ssize_t) image->rows; y++)
1169 register const Quantum
1178 if (status == MagickFalse)
1180 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1181 q=QueueCacheViewAuthenticPixels(rotate_view,0,(ssize_t) (image->rows-y-
1182 1),image->columns,1,exception);
1183 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
1188 q+=GetPixelChannels(rotate_image)*image->columns;
1189 for (x=0; x < (ssize_t) image->columns; x++)
1194 q-=GetPixelChannels(rotate_image);
1195 if (GetPixelReadMask(image,p) == 0)
1197 p+=GetPixelChannels(image);
1200 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1202 PixelChannel channel=GetPixelChannelChannel(image,i);
1203 PixelTrait traits=GetPixelChannelTraits(image,channel);
1204 PixelTrait rotate_traits=GetPixelChannelTraits(rotate_image,
1206 if ((traits == UndefinedPixelTrait) ||
1207 (rotate_traits == UndefinedPixelTrait))
1209 SetPixelChannel(rotate_image,channel,p[i],q);
1211 p+=GetPixelChannels(image);
1213 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1214 if (sync == MagickFalse)
1216 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1221 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1222 #pragma omp critical (MagickCore_IntegralRotateImage)
1224 proceed=SetImageProgress(image,RotateImageTag,progress++,
1226 if (proceed == MagickFalse)
1230 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1231 image->rows-1,image->rows);
1232 Swap(page.width,page.height);
1233 Swap(page.x,page.y);
1234 if (page.width != 0)
1235 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1250 GetPixelCacheTileSize(image,&tile_width,&tile_height);
1251 tile_width=image->columns;
1252 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1253 #pragma omp parallel for schedule(static,4) shared(status) \
1254 magick_threads(image,image,1,1)
1256 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
1261 if (status == MagickFalse)
1264 for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
1269 register const Quantum
1283 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
1284 width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
1286 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
1287 height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
1288 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1290 if (p == (const Quantum *) NULL)
1295 for (y=0; y < (ssize_t) width; y++)
1297 register const Quantum
1298 *restrict tile_pixels;
1303 if (status == MagickFalse)
1305 q=QueueCacheViewAuthenticPixels(rotate_view,tile_y,(ssize_t) (y+
1306 rotate_image->rows-(tile_x+width)),height,1,exception);
1307 if (q == (Quantum *) NULL)
1312 tile_pixels=p+((width-1)-y)*GetPixelChannels(image);
1313 for (x=0; x < (ssize_t) height; x++)
1318 if (GetPixelReadMask(image,tile_pixels) == 0)
1320 tile_pixels+=width*GetPixelChannels(image);
1321 q+=GetPixelChannels(rotate_image);
1324 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1326 PixelChannel channel=GetPixelChannelChannel(image,i);
1327 PixelTrait traits=GetPixelChannelTraits(image,channel);
1328 PixelTrait rotate_traits=GetPixelChannelTraits(rotate_image,
1330 if ((traits == UndefinedPixelTrait) ||
1331 (rotate_traits == UndefinedPixelTrait))
1333 SetPixelChannel(rotate_image,channel,tile_pixels[i],q);
1335 tile_pixels+=width*GetPixelChannels(image);
1336 q+=GetPixelChannels(rotate_image);
1338 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1339 #pragma omp critical (MagickCore_IntegralRotateImage)
1341 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1342 if (sync == MagickFalse)
1346 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1351 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
1353 if (proceed == MagickFalse)
1357 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1358 image->rows-1,image->rows);
1359 Swap(page.width,page.height);
1360 Swap(page.x,page.y);
1361 if (page.width != 0)
1362 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1366 rotate_view=DestroyCacheView(rotate_view);
1367 image_view=DestroyCacheView(image_view);
1368 rotate_image->type=image->type;
1369 rotate_image->page=page;
1370 if (status == MagickFalse)
1371 rotate_image=DestroyImage(rotate_image);
1372 return(rotate_image);
1376 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1380 + X S h e a r I m a g e %
1384 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1386 % XShearImage() shears the image in the X direction with a shear angle of
1387 % 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1388 % negative angles shear clockwise. Angles are measured relative to a vertical
1389 % Y-axis. X shears will widen an image creating 'empty' triangles on the left
1390 % and right sides of the source image.
1392 % The format of the XShearImage method is:
1394 % MagickBooleanType XShearImage(Image *image,const double degrees,
1395 % const size_t width,const size_t height,
1396 % const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
1398 % A description of each parameter follows.
1400 % o image: the image.
1402 % o degrees: A double representing the shearing angle along the X
1405 % o width, height, x_offset, y_offset: Defines a region of the image
1408 % o exception: return any errors or warnings in this structure.
1411 static MagickBooleanType XShearImage(Image *image,const double degrees,
1412 const size_t width,const size_t height,const ssize_t x_offset,
1413 const ssize_t y_offset,ExceptionInfo *exception)
1415 #define XShearImageTag "XShear/Image"
1441 assert(image != (Image *) NULL);
1442 assert(image->signature == MagickSignature);
1443 if (image->debug != MagickFalse)
1444 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1446 background=image->background_color;
1448 image_view=AcquireAuthenticCacheView(image,exception);
1449 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1450 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1451 magick_threads(image,image,height,1)
1453 for (y=0; y < (ssize_t) height; y++)
1477 if (status == MagickFalse)
1479 p=GetCacheViewAuthenticPixels(image_view,0,y_offset+y,image->columns,1,
1481 if (p == (Quantum *) NULL)
1486 p+=x_offset*GetPixelChannels(image);
1487 displacement=degrees*(double) (y-height/2.0);
1488 if (displacement == 0.0)
1490 if (displacement > 0.0)
1494 displacement*=(-1.0);
1497 step=(ssize_t) floor((double) displacement);
1498 area=(double) (displacement-step);
1501 GetPixelInfo(image,&source);
1502 GetPixelInfo(image,&destination);
1508 Transfer pixels left-to-right.
1510 if (step > x_offset)
1512 q=p-step*GetPixelChannels(image);
1513 for (i=0; i < (ssize_t) width; i++)
1515 if ((x_offset+i) < step)
1517 p+=GetPixelChannels(image);
1518 GetPixelInfoPixel(image,p,&pixel);
1519 q+=GetPixelChannels(image);
1522 GetPixelInfoPixel(image,p,&source);
1523 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1524 &source,(double) GetPixelAlpha(image,p),area,&destination);
1525 SetPixelInfoPixel(image,&destination,q);
1526 GetPixelInfoPixel(image,p,&pixel);
1527 p+=GetPixelChannels(image);
1528 q+=GetPixelChannels(image);
1530 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1531 &background,(double) background.alpha,area,&destination);
1532 SetPixelInfoPixel(image,&destination,q);
1533 q+=GetPixelChannels(image);
1534 for (i=0; i < (step-1); i++)
1536 SetPixelInfoPixel(image,&background,q);
1537 q+=GetPixelChannels(image);
1544 Transfer pixels right-to-left.
1546 p+=width*GetPixelChannels(image);
1547 q=p+step*GetPixelChannels(image);
1548 for (i=0; i < (ssize_t) width; i++)
1550 p-=GetPixelChannels(image);
1551 q-=GetPixelChannels(image);
1552 if ((size_t) (x_offset+width+step-i) >= image->columns)
1554 GetPixelInfoPixel(image,p,&source);
1555 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1556 &source,(double) GetPixelAlpha(image,p),area,&destination);
1557 SetPixelInfoPixel(image,&destination,q);
1558 GetPixelInfoPixel(image,p,&pixel);
1560 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1561 &background,(double) background.alpha,area,&destination);
1562 q-=GetPixelChannels(image);
1563 SetPixelInfoPixel(image,&destination,q);
1564 for (i=0; i < (step-1); i++)
1566 q-=GetPixelChannels(image);
1567 SetPixelInfoPixel(image,&background,q);
1572 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1574 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1579 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1580 #pragma omp critical (MagickCore_XShearImage)
1582 proceed=SetImageProgress(image,XShearImageTag,progress++,height);
1583 if (proceed == MagickFalse)
1587 image_view=DestroyCacheView(image_view);
1592 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1596 + Y S h e a r I m a g e %
1600 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1602 % YShearImage shears the image in the Y direction with a shear angle of
1603 % 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1604 % negative angles shear clockwise. Angles are measured relative to a
1605 % horizontal X-axis. Y shears will increase the height of an image creating
1606 % 'empty' triangles on the top and bottom of the source image.
1608 % The format of the YShearImage method is:
1610 % MagickBooleanType YShearImage(Image *image,const double degrees,
1611 % const size_t width,const size_t height,
1612 % const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
1614 % A description of each parameter follows.
1616 % o image: the image.
1618 % o degrees: A double representing the shearing angle along the Y
1621 % o width, height, x_offset, y_offset: Defines a region of the image
1624 % o exception: return any errors or warnings in this structure.
1627 static MagickBooleanType YShearImage(Image *image,const double degrees,
1628 const size_t width,const size_t height,const ssize_t x_offset,
1629 const ssize_t y_offset,ExceptionInfo *exception)
1631 #define YShearImageTag "YShear/Image"
1657 assert(image != (Image *) NULL);
1658 assert(image->signature == MagickSignature);
1659 if (image->debug != MagickFalse)
1660 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1663 background=image->background_color;
1664 image_view=AcquireAuthenticCacheView(image,exception);
1665 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1666 #pragma omp parallel for schedule(static,4) shared(progress,status) \
1667 magick_threads(image,image,width,1)
1669 for (x=0; x < (ssize_t) width; x++)
1693 if (status == MagickFalse)
1695 p=GetCacheViewAuthenticPixels(image_view,x_offset+x,0,1,image->rows,
1697 if (p == (Quantum *) NULL)
1702 p+=y_offset*GetPixelChannels(image);
1703 displacement=degrees*(double) (x-width/2.0);
1704 if (displacement == 0.0)
1706 if (displacement > 0.0)
1710 displacement*=(-1.0);
1713 step=(ssize_t) floor((double) displacement);
1714 area=(double) (displacement-step);
1717 GetPixelInfo(image,&source);
1718 GetPixelInfo(image,&destination);
1724 Transfer pixels top-to-bottom.
1726 if (step > y_offset)
1728 q=p-step*GetPixelChannels(image);
1729 for (i=0; i < (ssize_t) height; i++)
1731 if ((y_offset+i) < step)
1733 p+=GetPixelChannels(image);
1734 GetPixelInfoPixel(image,p,&pixel);
1735 q+=GetPixelChannels(image);
1738 GetPixelInfoPixel(image,p,&source);
1739 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1740 &source,(double) GetPixelAlpha(image,p),area,
1742 SetPixelInfoPixel(image,&destination,q);
1743 GetPixelInfoPixel(image,p,&pixel);
1744 p+=GetPixelChannels(image);
1745 q+=GetPixelChannels(image);
1747 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1748 &background,(double) background.alpha,area,&destination);
1749 SetPixelInfoPixel(image,&destination,q);
1750 q+=GetPixelChannels(image);
1751 for (i=0; i < (step-1); i++)
1753 SetPixelInfoPixel(image,&background,q);
1754 q+=GetPixelChannels(image);
1761 Transfer pixels bottom-to-top.
1763 p+=height*GetPixelChannels(image);
1764 q=p+step*GetPixelChannels(image);
1765 for (i=0; i < (ssize_t) height; i++)
1767 p-=GetPixelChannels(image);
1768 q-=GetPixelChannels(image);
1769 if ((size_t) (y_offset+height+step-i) >= image->rows)
1771 GetPixelInfoPixel(image,p,&source);
1772 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1773 &source,(double) GetPixelAlpha(image,p),area,
1775 SetPixelInfoPixel(image,&destination,q);
1776 GetPixelInfoPixel(image,p,&pixel);
1778 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1779 &background,(double) background.alpha,area,&destination);
1780 q-=GetPixelChannels(image);
1781 SetPixelInfoPixel(image,&destination,q);
1782 for (i=0; i < (step-1); i++)
1784 q-=GetPixelChannels(image);
1785 SetPixelInfoPixel(image,&background,q);
1790 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1792 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1797 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1798 #pragma omp critical (MagickCore_YShearImage)
1800 proceed=SetImageProgress(image,YShearImageTag,progress++,image->rows);
1801 if (proceed == MagickFalse)
1805 image_view=DestroyCacheView(image_view);
1810 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1814 % S h e a r I m a g e %
1818 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1820 % ShearImage() creates a new image that is a shear_image copy of an existing
1821 % one. Shearing slides one edge of an image along the X or Y axis, creating
1822 % a parallelogram. An X direction shear slides an edge along the X axis,
1823 % while a Y direction shear slides an edge along the Y axis. The amount of
1824 % the shear is controlled by a shear angle. For X direction shears, x_shear
1825 % is measured relative to the Y axis, and similarly, for Y direction shears
1826 % y_shear is measured relative to the X axis. Empty triangles left over from
1827 % shearing the image are filled with the background color defined by member
1828 % 'background_color' of the image.. ShearImage() allocates the memory
1829 % necessary for the new Image structure and returns a pointer to the new image.
1831 % ShearImage() is based on the paper "A Fast Algorithm for General Raster
1832 % Rotatation" by Alan W. Paeth.
1834 % The format of the ShearImage method is:
1836 % Image *ShearImage(const Image *image,const double x_shear,
1837 % const double y_shear,ExceptionInfo *exception)
1839 % A description of each parameter follows.
1841 % o image: the image.
1843 % o x_shear, y_shear: Specifies the number of degrees to shear the image.
1845 % o exception: return any errors or warnings in this structure.
1848 MagickExport Image *ShearImage(const Image *image,const double x_shear,
1849 const double y_shear,ExceptionInfo *exception)
1871 assert(image != (Image *) NULL);
1872 assert(image->signature == MagickSignature);
1873 if (image->debug != MagickFalse)
1874 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1875 assert(exception != (ExceptionInfo *) NULL);
1876 assert(exception->signature == MagickSignature);
1877 if ((x_shear != 0.0) && (fmod(x_shear,90.0) == 0.0))
1878 ThrowImageException(ImageError,"AngleIsDiscontinuous");
1879 if ((y_shear != 0.0) && (fmod(y_shear,90.0) == 0.0))
1880 ThrowImageException(ImageError,"AngleIsDiscontinuous");
1882 Initialize shear angle.
1884 integral_image=CloneImage(image,0,0,MagickTrue,exception);
1885 if (integral_image == (Image *) NULL)
1886 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1887 shear.x=(-tan(DegreesToRadians(fmod(x_shear,360.0))));
1888 shear.y=tan(DegreesToRadians(fmod(y_shear,360.0)));
1889 if ((shear.x == 0.0) && (shear.y == 0.0))
1890 return(integral_image);
1891 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
1893 integral_image=DestroyImage(integral_image);
1894 return(integral_image);
1896 if (integral_image->alpha_trait != BlendPixelTrait)
1897 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
1901 y_width=image->columns+(ssize_t) floor(fabs(shear.x)*image->rows+0.5);
1902 x_offset=(ssize_t) ceil((double) image->columns+((fabs(shear.x)*image->rows)-
1903 image->columns)/2.0-0.5);
1904 y_offset=(ssize_t) ceil((double) image->rows+((fabs(shear.y)*y_width)-
1905 image->rows)/2.0-0.5);
1907 Surround image with border.
1909 integral_image->border_color=integral_image->background_color;
1910 integral_image->compose=CopyCompositeOp;
1911 border_info.width=(size_t) x_offset;
1912 border_info.height=(size_t) y_offset;
1913 shear_image=BorderImage(integral_image,&border_info,image->compose,exception);
1914 integral_image=DestroyImage(integral_image);
1915 if (shear_image == (Image *) NULL)
1916 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1920 if (shear_image->alpha_trait != BlendPixelTrait)
1921 (void) SetImageAlphaChannel(shear_image,OpaqueAlphaChannel,exception);
1922 status=XShearImage(shear_image,shear.x,image->columns,image->rows,x_offset,
1923 (ssize_t) (shear_image->rows-image->rows)/2,exception);
1924 if (status == MagickFalse)
1926 shear_image=DestroyImage(shear_image);
1927 return((Image *) NULL);
1929 status=YShearImage(shear_image,shear.y,y_width,image->rows,(ssize_t)
1930 (shear_image->columns-y_width)/2,y_offset,exception);
1931 if (status == MagickFalse)
1933 shear_image=DestroyImage(shear_image);
1934 return((Image *) NULL);
1936 status=CropToFitImage(&shear_image,shear.x,shear.y,(double)
1937 image->columns,(double) image->rows,MagickFalse,exception);
1938 shear_image->compose=image->compose;
1939 shear_image->page.width=0;
1940 shear_image->page.height=0;
1941 if (status == MagickFalse)
1942 shear_image=DestroyImage(shear_image);
1943 return(shear_image);
1947 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1951 % S h e a r R o t a t e I m a g e %
1955 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1957 % ShearRotateImage() creates a new image that is a rotated copy of an existing
1958 % one. Positive angles rotate counter-clockwise (right-hand rule), while
1959 % negative angles rotate clockwise. Rotated images are usually larger than
1960 % the originals and have 'empty' triangular corners. X axis. Empty
1961 % triangles left over from shearing the image are filled with the background
1962 % color defined by member 'background_color' of the image. ShearRotateImage
1963 % allocates the memory necessary for the new Image structure and returns a
1964 % pointer to the new image.
1966 % ShearRotateImage() is based on the paper "A Fast Algorithm for General
1967 % Raster Rotatation" by Alan W. Paeth. ShearRotateImage is adapted from a
1968 % similar method based on the Paeth paper written by Michael Halle of the
1969 % Spatial Imaging Group, MIT Media Lab.
1971 % The format of the ShearRotateImage method is:
1973 % Image *ShearRotateImage(const Image *image,const double degrees,
1974 % ExceptionInfo *exception)
1976 % A description of each parameter follows.
1978 % o image: the image.
1980 % o degrees: Specifies the number of degrees to rotate the image.
1982 % o exception: return any errors or warnings in this structure.
1985 MagickExport Image *ShearRotateImage(const Image *image,const double degrees,
1986 ExceptionInfo *exception)
2015 Adjust rotation angle.
2017 assert(image != (Image *) NULL);
2018 assert(image->signature == MagickSignature);
2019 if (image->debug != MagickFalse)
2020 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2021 assert(exception != (ExceptionInfo *) NULL);
2022 assert(exception->signature == MagickSignature);
2024 while (angle < -45.0)
2026 for (rotations=0; angle > 45.0; rotations++)
2030 Calculate shear equations.
2032 integral_image=IntegralRotateImage(image,rotations,exception);
2033 if (integral_image == (Image *) NULL)
2034 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2035 shear.x=(-tan((double) DegreesToRadians(angle)/2.0));
2036 shear.y=sin((double) DegreesToRadians(angle));
2037 if ((shear.x == 0.0) && (shear.y == 0.0))
2038 return(integral_image);
2039 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
2041 integral_image=DestroyImage(integral_image);
2042 return(integral_image);
2044 if (integral_image->alpha_trait != BlendPixelTrait)
2045 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
2049 width=image->columns;
2051 if ((rotations == 1) || (rotations == 3))
2054 height=image->columns;
2056 y_width=width+(ssize_t) floor(fabs(shear.x)*height+0.5);
2057 x_offset=(ssize_t) ceil((double) width+((fabs(shear.y)*height)-width)/2.0-
2059 y_offset=(ssize_t) ceil((double) height+((fabs(shear.y)*y_width)-height)/2.0-
2062 Surround image with a border.
2064 integral_image->border_color=integral_image->background_color;
2065 integral_image->compose=CopyCompositeOp;
2066 border_info.width=(size_t) x_offset;
2067 border_info.height=(size_t) y_offset;
2068 rotate_image=BorderImage(integral_image,&border_info,image->compose,
2070 integral_image=DestroyImage(integral_image);
2071 if (rotate_image == (Image *) NULL)
2072 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2076 status=XShearImage(rotate_image,shear.x,width,height,x_offset,(ssize_t)
2077 (rotate_image->rows-height)/2,exception);
2078 if (status == MagickFalse)
2080 rotate_image=DestroyImage(rotate_image);
2081 return((Image *) NULL);
2083 status=YShearImage(rotate_image,shear.y,y_width,height,(ssize_t)
2084 (rotate_image->columns-y_width)/2,y_offset,exception);
2085 if (status == MagickFalse)
2087 rotate_image=DestroyImage(rotate_image);
2088 return((Image *) NULL);
2090 status=XShearImage(rotate_image,shear.x,y_width,rotate_image->rows,(ssize_t)
2091 (rotate_image->columns-y_width)/2,0,exception);
2092 if (status == MagickFalse)
2094 rotate_image=DestroyImage(rotate_image);
2095 return((Image *) NULL);
2097 status=CropToFitImage(&rotate_image,shear.x,shear.y,(double) width,
2098 (double) height,MagickTrue,exception);
2099 rotate_image->compose=image->compose;
2100 rotate_image->page.width=0;
2101 rotate_image->page.height=0;
2102 if (status == MagickFalse)
2103 rotate_image=DestroyImage(rotate_image);
2104 return(rotate_image);