• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                      SSSSS  H   H  EEEEE   AAA    RRRR                      %
7 %                      SS     H   H  E      A   A   R   R                     %
8 %                       SSS   HHHHH  EEE    AAAAA   RRRR                      %
9 %                         SS  H   H  E      A   A   R R                       %
10 %                      SSSSS  H   H  EEEEE  A   A   R  R                      %
11 %                                                                             %
12 %                                                                             %
13 %    MagickCore Methods to Shear or Rotate an Image by an Arbitrary Angle     %
14 %                                                                             %
15 %                               Software Design                               %
16 %                                    Cristy                                   %
17 %                                  July 1992                                  %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2019 ImageMagick Studio LLC, a non-profit organization      %
21 %  dedicated to making software imaging solutions freely available.           %
22 %                                                                             %
23 %  You may not use this file except in compliance with the License.  You may  %
24 %  obtain a copy of the License at                                            %
25 %                                                                             %
26 %    https://imagemagick.org/script/license.php                               %
27 %                                                                             %
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.                                             %
33 %                                                                             %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 %
36 %  The XShearImage() and YShearImage() methods are based on the paper "A Fast
37 %  Algorithm for General Raster Rotation" 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.
41 %
42 */
43 
44 /*
45   Include declarations.
46 */
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/channel.h"
53 #include "MagickCore/color-private.h"
54 #include "MagickCore/colorspace-private.h"
55 #include "MagickCore/composite.h"
56 #include "MagickCore/composite-private.h"
57 #include "MagickCore/decorate.h"
58 #include "MagickCore/distort.h"
59 #include "MagickCore/draw.h"
60 #include "MagickCore/exception.h"
61 #include "MagickCore/exception-private.h"
62 #include "MagickCore/gem.h"
63 #include "MagickCore/geometry.h"
64 #include "MagickCore/image.h"
65 #include "MagickCore/image-private.h"
66 #include "MagickCore/matrix.h"
67 #include "MagickCore/memory_.h"
68 #include "MagickCore/list.h"
69 #include "MagickCore/monitor.h"
70 #include "MagickCore/monitor-private.h"
71 #include "MagickCore/nt-base-private.h"
72 #include "MagickCore/pixel-accessor.h"
73 #include "MagickCore/quantum.h"
74 #include "MagickCore/resource_.h"
75 #include "MagickCore/shear.h"
76 #include "MagickCore/statistic.h"
77 #include "MagickCore/string_.h"
78 #include "MagickCore/string-private.h"
79 #include "MagickCore/thread-private.h"
80 #include "MagickCore/threshold.h"
81 #include "MagickCore/transform.h"
82 
83 /*
84 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
85 %                                                                             %
86 %                                                                             %
87 %                                                                             %
88 +   C r o p T o F i t I m a g e                                               %
89 %                                                                             %
90 %                                                                             %
91 %                                                                             %
92 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
93 %
94 %  CropToFitImage() crops the sheared image as determined by the bounding box
95 %  as defined by width and height and shearing angles.
96 %
97 %  The format of the CropToFitImage method is:
98 %
99 %      MagickBooleanType CropToFitImage(Image **image,
100 %        const double x_shear,const double x_shear,
101 %        const double width,const double height,
102 %        const MagickBooleanType rotate,ExceptionInfo *exception)
103 %
104 %  A description of each parameter follows.
105 %
106 %    o image: the image.
107 %
108 %    o x_shear, y_shear, width, height: Defines a region of the image to crop.
109 %
110 %    o exception: return any errors or warnings in this structure.
111 %
112 */
CropToFitImage(Image ** image,const double x_shear,const double y_shear,const double width,const double height,const MagickBooleanType rotate,ExceptionInfo * exception)113 static MagickBooleanType CropToFitImage(Image **image,
114   const double x_shear,const double y_shear,
115   const double width,const double height,
116   const MagickBooleanType rotate,ExceptionInfo *exception)
117 {
118   Image
119     *crop_image;
120 
121   PointInfo
122     extent[4],
123     min,
124     max;
125 
126   RectangleInfo
127     geometry,
128     page;
129 
130   register ssize_t
131     i;
132 
133   /*
134     Calculate the rotated image size.
135   */
136   extent[0].x=(double) (-width/2.0);
137   extent[0].y=(double) (-height/2.0);
138   extent[1].x=(double) width/2.0;
139   extent[1].y=(double) (-height/2.0);
140   extent[2].x=(double) (-width/2.0);
141   extent[2].y=(double) height/2.0;
142   extent[3].x=(double) width/2.0;
143   extent[3].y=(double) height/2.0;
144   for (i=0; i < 4; i++)
145   {
146     extent[i].x+=x_shear*extent[i].y;
147     extent[i].y+=y_shear*extent[i].x;
148     if (rotate != MagickFalse)
149       extent[i].x+=x_shear*extent[i].y;
150     extent[i].x+=(double) (*image)->columns/2.0;
151     extent[i].y+=(double) (*image)->rows/2.0;
152   }
153   min=extent[0];
154   max=extent[0];
155   for (i=1; i < 4; i++)
156   {
157     if (min.x > extent[i].x)
158       min.x=extent[i].x;
159     if (min.y > extent[i].y)
160       min.y=extent[i].y;
161     if (max.x < extent[i].x)
162       max.x=extent[i].x;
163     if (max.y < extent[i].y)
164       max.y=extent[i].y;
165   }
166   geometry.x=(ssize_t) ceil(min.x-0.5);
167   geometry.y=(ssize_t) ceil(min.y-0.5);
168   geometry.width=(size_t) floor(max.x-min.x+0.5);
169   geometry.height=(size_t) floor(max.y-min.y+0.5);
170   page=(*image)->page;
171   (void) ParseAbsoluteGeometry("0x0+0+0",&(*image)->page);
172   crop_image=CropImage(*image,&geometry,exception);
173   if (crop_image == (Image *) NULL)
174     return(MagickFalse);
175   crop_image->page=page;
176   *image=DestroyImage(*image);
177   *image=crop_image;
178   return(MagickTrue);
179 }
180 
181 /*
182 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
183 %                                                                             %
184 %                                                                             %
185 %                                                                             %
186 %     D e s k e w I m a g e                                                   %
187 %                                                                             %
188 %                                                                             %
189 %                                                                             %
190 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
191 %
192 %  DeskewImage() removes skew from the image.  Skew is an artifact that
193 %  occurs in scanned images because of the camera being misaligned,
194 %  imperfections in the scanning or surface, or simply because the paper was
195 %  not placed completely flat when scanned.
196 %
197 %  The result will be auto-croped if the artifact "deskew:auto-crop" is
198 %  defined, while the amount the image is to be deskewed, in degrees is also
199 %  saved as the artifact "deskew:angle".
200 %
201 %  The format of the DeskewImage method is:
202 %
203 %      Image *DeskewImage(const Image *image,const double threshold,
204 %        ExceptionInfo *exception)
205 %
206 %  A description of each parameter follows:
207 %
208 %    o image: the image.
209 %
210 %    o threshold: separate background from foreground.
211 %
212 %    o exception: return any errors or warnings in this structure.
213 %
214 */
215 
RadonProjection(const Image * image,MatrixInfo * source_matrixs,MatrixInfo * destination_matrixs,const ssize_t sign,size_t * projection)216 static void RadonProjection(const Image *image,MatrixInfo *source_matrixs,
217   MatrixInfo *destination_matrixs,const ssize_t sign,size_t *projection)
218 {
219   MatrixInfo
220     *swap;
221 
222   register MatrixInfo
223     *p,
224     *q;
225 
226   register ssize_t
227     x;
228 
229   size_t
230     step;
231 
232   p=source_matrixs;
233   q=destination_matrixs;
234   for (step=1; step < GetMatrixColumns(p); step*=2)
235   {
236     for (x=0; x < (ssize_t) GetMatrixColumns(p); x+=2*(ssize_t) step)
237     {
238       register ssize_t
239         i;
240 
241       ssize_t
242         y;
243 
244       unsigned short
245         element,
246         neighbor;
247 
248       for (i=0; i < (ssize_t) step; i++)
249       {
250         for (y=0; y < (ssize_t) (GetMatrixRows(p)-i-1); y++)
251         {
252           if (GetMatrixElement(p,x+i,y,&element) == MagickFalse)
253             continue;
254           if (GetMatrixElement(p,x+i+step,y+i,&neighbor) == MagickFalse)
255             continue;
256           neighbor+=element;
257           if (SetMatrixElement(q,x+2*i,y,&neighbor) == MagickFalse)
258             continue;
259           if (GetMatrixElement(p,x+i+step,y+i+1,&neighbor) == MagickFalse)
260             continue;
261           neighbor+=element;
262           if (SetMatrixElement(q,x+2*i+1,y,&neighbor) == MagickFalse)
263             continue;
264         }
265         for ( ; y < (ssize_t) (GetMatrixRows(p)-i); y++)
266         {
267           if (GetMatrixElement(p,x+i,y,&element) == MagickFalse)
268             continue;
269           if (GetMatrixElement(p,x+i+step,y+i,&neighbor) == MagickFalse)
270             continue;
271           neighbor+=element;
272           if (SetMatrixElement(q,x+2*i,y,&neighbor) == MagickFalse)
273             continue;
274           if (SetMatrixElement(q,x+2*i+1,y,&element) == MagickFalse)
275             continue;
276         }
277         for ( ; y < (ssize_t) GetMatrixRows(p); y++)
278         {
279           if (GetMatrixElement(p,x+i,y,&element) == MagickFalse)
280             continue;
281           if (SetMatrixElement(q,x+2*i,y,&element) == MagickFalse)
282             continue;
283           if (SetMatrixElement(q,x+2*i+1,y,&element) == MagickFalse)
284             continue;
285         }
286       }
287     }
288     swap=p;
289     p=q;
290     q=swap;
291   }
292 #if defined(MAGICKCORE_OPENMP_SUPPORT)
293   #pragma omp parallel for schedule(static) \
294     magick_number_threads(image,image,GetMatrixColumns(p),1)
295 #endif
296   for (x=0; x < (ssize_t) GetMatrixColumns(p); x++)
297   {
298     register ssize_t
299       y;
300 
301     size_t
302       sum;
303 
304     sum=0;
305     for (y=0; y < (ssize_t) (GetMatrixRows(p)-1); y++)
306     {
307       ssize_t
308         delta;
309 
310       unsigned short
311         element,
312         neighbor;
313 
314       if (GetMatrixElement(p,x,y,&element) == MagickFalse)
315         continue;
316       if (GetMatrixElement(p,x,y+1,&neighbor) == MagickFalse)
317         continue;
318       delta=(ssize_t) element-(ssize_t) neighbor;
319       sum+=delta*delta;
320     }
321     projection[GetMatrixColumns(p)+sign*x-1]=sum;
322   }
323 }
324 
RadonTransform(const Image * image,const double threshold,size_t * projection,ExceptionInfo * exception)325 static MagickBooleanType RadonTransform(const Image *image,
326   const double threshold,size_t *projection,ExceptionInfo *exception)
327 {
328   CacheView
329     *image_view;
330 
331   MatrixInfo
332     *destination_matrixs,
333     *source_matrixs;
334 
335   MagickBooleanType
336     status;
337 
338   size_t
339     count,
340     width;
341 
342   ssize_t
343     j,
344     y;
345 
346   unsigned char
347     c;
348 
349   unsigned short
350     bits[256];
351 
352   for (width=1; width < ((image->columns+7)/8); width<<=1) ;
353   source_matrixs=AcquireMatrixInfo(width,image->rows,sizeof(unsigned short),
354     exception);
355   destination_matrixs=AcquireMatrixInfo(width,image->rows,
356     sizeof(unsigned short),exception);
357   if ((source_matrixs == (MatrixInfo *) NULL) ||
358       (destination_matrixs == (MatrixInfo *) NULL))
359     {
360       if (destination_matrixs != (MatrixInfo *) NULL)
361         destination_matrixs=DestroyMatrixInfo(destination_matrixs);
362       if (source_matrixs != (MatrixInfo *) NULL)
363         source_matrixs=DestroyMatrixInfo(source_matrixs);
364       return(MagickFalse);
365     }
366   if (NullMatrix(source_matrixs) == MagickFalse)
367     {
368       destination_matrixs=DestroyMatrixInfo(destination_matrixs);
369       source_matrixs=DestroyMatrixInfo(source_matrixs);
370       return(MagickFalse);
371     }
372   for (j=0; j < 256; j++)
373   {
374     c=(unsigned char) j;
375     for (count=0; c != 0; c>>=1)
376       count+=c & 0x01;
377     bits[j]=(unsigned short) count;
378   }
379   status=MagickTrue;
380   image_view=AcquireVirtualCacheView(image,exception);
381 #if defined(MAGICKCORE_OPENMP_SUPPORT)
382   #pragma omp parallel for schedule(static) shared(status) \
383     magick_number_threads(image,image,image->rows,1)
384 #endif
385   for (y=0; y < (ssize_t) image->rows; y++)
386   {
387     register const Quantum
388       *magick_restrict p;
389 
390     register ssize_t
391       i,
392       x;
393 
394     size_t
395       bit,
396       byte;
397 
398     unsigned short
399       value;
400 
401     if (status == MagickFalse)
402       continue;
403     p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
404     if (p == (const Quantum *) NULL)
405       {
406         status=MagickFalse;
407         continue;
408       }
409     bit=0;
410     byte=0;
411     i=(ssize_t) (image->columns+7)/8;
412     for (x=0; x < (ssize_t) image->columns; x++)
413     {
414       byte<<=1;
415       if (((MagickRealType) GetPixelRed(image,p) < threshold) ||
416           ((MagickRealType) GetPixelGreen(image,p) < threshold) ||
417           ((MagickRealType) GetPixelBlue(image,p) < threshold))
418         byte|=0x01;
419       bit++;
420       if (bit == 8)
421         {
422           value=bits[byte];
423           (void) SetMatrixElement(source_matrixs,--i,y,&value);
424           bit=0;
425           byte=0;
426         }
427       p+=GetPixelChannels(image);
428     }
429     if (bit != 0)
430       {
431         byte<<=(8-bit);
432         value=bits[byte];
433         (void) SetMatrixElement(source_matrixs,--i,y,&value);
434       }
435   }
436   RadonProjection(image,source_matrixs,destination_matrixs,-1,projection);
437   (void) NullMatrix(source_matrixs);
438 #if defined(MAGICKCORE_OPENMP_SUPPORT)
439   #pragma omp parallel for schedule(static) shared(status) \
440     magick_number_threads(image,image,image->rows,1)
441 #endif
442   for (y=0; y < (ssize_t) image->rows; y++)
443   {
444     register const Quantum
445       *magick_restrict p;
446 
447     register ssize_t
448       i,
449       x;
450 
451     size_t
452       bit,
453       byte;
454 
455     unsigned short
456      value;
457 
458     if (status == MagickFalse)
459       continue;
460     p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
461     if (p == (const Quantum *) NULL)
462       {
463         status=MagickFalse;
464         continue;
465       }
466     bit=0;
467     byte=0;
468     i=0;
469     for (x=0; x < (ssize_t) image->columns; x++)
470     {
471       byte<<=1;
472       if (((MagickRealType) GetPixelRed(image,p) < threshold) ||
473           ((MagickRealType) GetPixelGreen(image,p) < threshold) ||
474           ((MagickRealType) GetPixelBlue(image,p) < threshold))
475         byte|=0x01;
476       bit++;
477       if (bit == 8)
478         {
479           value=bits[byte];
480           (void) SetMatrixElement(source_matrixs,i++,y,&value);
481           bit=0;
482           byte=0;
483         }
484       p+=GetPixelChannels(image);
485     }
486     if (bit != 0)
487       {
488         byte<<=(8-bit);
489         value=bits[byte];
490         (void) SetMatrixElement(source_matrixs,i++,y,&value);
491       }
492   }
493   RadonProjection(image,source_matrixs,destination_matrixs,1,projection);
494   image_view=DestroyCacheView(image_view);
495   destination_matrixs=DestroyMatrixInfo(destination_matrixs);
496   source_matrixs=DestroyMatrixInfo(source_matrixs);
497   return(MagickTrue);
498 }
499 
GetImageBackgroundColor(Image * image,const ssize_t offset,ExceptionInfo * exception)500 static void GetImageBackgroundColor(Image *image,const ssize_t offset,
501   ExceptionInfo *exception)
502 {
503   CacheView
504     *image_view;
505 
506   PixelInfo
507     background;
508 
509   double
510     count;
511 
512   ssize_t
513     y;
514 
515   /*
516     Compute average background color.
517   */
518   if (offset <= 0)
519     return;
520   GetPixelInfo(image,&background);
521   count=0.0;
522   image_view=AcquireVirtualCacheView(image,exception);
523   for (y=0; y < (ssize_t) image->rows; y++)
524   {
525     register const Quantum
526       *magick_restrict p;
527 
528     register ssize_t
529       x;
530 
531     if ((y >= offset) && (y < ((ssize_t) image->rows-offset)))
532       continue;
533     p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
534     if (p == (const Quantum *) NULL)
535       continue;
536     for (x=0; x < (ssize_t) image->columns; x++)
537     {
538       if ((x >= offset) && (x < ((ssize_t) image->columns-offset)))
539         continue;
540       background.red+=QuantumScale*GetPixelRed(image,p);
541       background.green+=QuantumScale*GetPixelGreen(image,p);
542       background.blue+=QuantumScale*GetPixelBlue(image,p);
543       if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
544         background.alpha+=QuantumScale*GetPixelAlpha(image,p);
545       count++;
546       p+=GetPixelChannels(image);
547     }
548   }
549   image_view=DestroyCacheView(image_view);
550   image->background_color.red=(double) ClampToQuantum(QuantumRange*
551     background.red/count);
552   image->background_color.green=(double) ClampToQuantum(QuantumRange*
553     background.green/count);
554   image->background_color.blue=(double) ClampToQuantum(QuantumRange*
555     background.blue/count);
556   if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
557     image->background_color.alpha=(double) ClampToQuantum(QuantumRange*
558       background.alpha/count);
559 }
560 
DeskewImage(const Image * image,const double threshold,ExceptionInfo * exception)561 MagickExport Image *DeskewImage(const Image *image,const double threshold,
562   ExceptionInfo *exception)
563 {
564   AffineMatrix
565     affine_matrix;
566 
567   const char
568     *artifact;
569 
570   double
571     degrees;
572 
573   Image
574     *clone_image,
575     *crop_image,
576     *deskew_image,
577     *median_image;
578 
579   MagickBooleanType
580     status;
581 
582   RectangleInfo
583     geometry;
584 
585   register ssize_t
586     i;
587 
588   size_t
589     max_projection,
590     *projection,
591     width;
592 
593   ssize_t
594     skew;
595 
596   /*
597     Compute deskew angle.
598   */
599   for (width=1; width < ((image->columns+7)/8); width<<=1) ;
600   projection=(size_t *) AcquireQuantumMemory((size_t) (2*width-1),
601     sizeof(*projection));
602   if (projection == (size_t *) NULL)
603     ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
604   status=RadonTransform(image,threshold,projection,exception);
605   if (status == MagickFalse)
606     {
607       projection=(size_t *) RelinquishMagickMemory(projection);
608       ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
609     }
610   max_projection=0;
611   skew=0;
612   for (i=0; i < (ssize_t) (2*width-1); i++)
613   {
614     if (projection[i] > max_projection)
615       {
616         skew=i-(ssize_t) width+1;
617         max_projection=projection[i];
618       }
619   }
620   projection=(size_t *) RelinquishMagickMemory(projection);
621   degrees=RadiansToDegrees(-atan((double) skew/width/8));
622   if (image->debug != MagickFalse)
623     (void) LogMagickEvent(TransformEvent,GetMagickModule(),
624       "  Deskew angle: %g",degrees);
625   /*
626     Deskew image.
627   */
628   clone_image=CloneImage(image,0,0,MagickTrue,exception);
629   if (clone_image == (Image *) NULL)
630     return((Image *) NULL);
631   {
632     char
633       angle[MagickPathExtent];
634 
635     (void) FormatLocaleString(angle,MagickPathExtent,"%.20g",degrees);
636     (void) SetImageArtifact(clone_image,"deskew:angle",angle);
637   }
638   (void) SetImageVirtualPixelMethod(clone_image,BackgroundVirtualPixelMethod,
639     exception);
640   affine_matrix.sx=cos(DegreesToRadians(fmod((double) degrees,360.0)));
641   affine_matrix.rx=sin(DegreesToRadians(fmod((double) degrees,360.0)));
642   affine_matrix.ry=(-sin(DegreesToRadians(fmod((double) degrees,360.0))));
643   affine_matrix.sy=cos(DegreesToRadians(fmod((double) degrees,360.0)));
644   affine_matrix.tx=0.0;
645   affine_matrix.ty=0.0;
646   artifact=GetImageArtifact(image,"deskew:auto-crop");
647   if (IsStringTrue(artifact) == MagickFalse)
648     {
649       deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
650       clone_image=DestroyImage(clone_image);
651       return(deskew_image);
652     }
653   /*
654     Auto-crop image.
655   */
656   GetImageBackgroundColor(clone_image,(ssize_t) StringToLong(artifact),
657     exception);
658   deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
659   clone_image=DestroyImage(clone_image);
660   if (deskew_image == (Image *) NULL)
661     return((Image *) NULL);
662   median_image=StatisticImage(deskew_image,MedianStatistic,3,3,exception);
663   if (median_image == (Image *) NULL)
664     {
665       deskew_image=DestroyImage(deskew_image);
666       return((Image *) NULL);
667     }
668   geometry=GetImageBoundingBox(median_image,exception);
669   median_image=DestroyImage(median_image);
670   if (image->debug != MagickFalse)
671     (void) LogMagickEvent(TransformEvent,GetMagickModule(),"  Deskew geometry: "
672       "%.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
673       geometry.height,(double) geometry.x,(double) geometry.y);
674   crop_image=CropImage(deskew_image,&geometry,exception);
675   deskew_image=DestroyImage(deskew_image);
676   return(crop_image);
677 }
678 
679 /*
680 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
681 %                                                                             %
682 %                                                                             %
683 %                                                                             %
684 %   I n t e g r a l R o t a t e I m a g e                                     %
685 %                                                                             %
686 %                                                                             %
687 %                                                                             %
688 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
689 %
690 %  IntegralRotateImage() rotates the image an integral of 90 degrees.  It
691 %  allocates the memory necessary for the new Image structure and returns a
692 %  pointer to the rotated image.
693 %
694 %  The format of the IntegralRotateImage method is:
695 %
696 %      Image *IntegralRotateImage(const Image *image,size_t rotations,
697 %        ExceptionInfo *exception)
698 %
699 %  A description of each parameter follows.
700 %
701 %    o image: the image.
702 %
703 %    o rotations: Specifies the number of 90 degree rotations.
704 %
705 */
IntegralRotateImage(const Image * image,size_t rotations,ExceptionInfo * exception)706 MagickExport Image *IntegralRotateImage(const Image *image,size_t rotations,
707   ExceptionInfo *exception)
708 {
709 #define RotateImageTag  "Rotate/Image"
710 
711   CacheView
712     *image_view,
713     *rotate_view;
714 
715   Image
716     *rotate_image;
717 
718   MagickBooleanType
719     status;
720 
721   MagickOffsetType
722     progress;
723 
724   RectangleInfo
725     page;
726 
727   /*
728     Initialize rotated image attributes.
729   */
730   assert(image != (Image *) NULL);
731   page=image->page;
732   rotations%=4;
733   if (rotations == 0)
734     return(CloneImage(image,0,0,MagickTrue,exception));
735   if ((rotations == 1) || (rotations == 3))
736     rotate_image=CloneImage(image,image->rows,image->columns,MagickTrue,
737       exception);
738   else
739     rotate_image=CloneImage(image,0,0,MagickTrue,
740       exception);
741   if (rotate_image == (Image *) NULL)
742     return((Image *) NULL);
743   /*
744     Integral rotate the image.
745   */
746   status=MagickTrue;
747   progress=0;
748   image_view=AcquireVirtualCacheView(image,exception);
749   rotate_view=AcquireAuthenticCacheView(rotate_image,exception);
750   switch (rotations)
751   {
752     case 1:
753     {
754       size_t
755         tile_height,
756         tile_width;
757 
758       ssize_t
759         tile_y;
760 
761       /*
762         Rotate 90 degrees.
763       */
764       GetPixelCacheTileSize(image,&tile_width,&tile_height);
765       tile_width=image->columns;
766 #if defined(MAGICKCORE_OPENMP_SUPPORT)
767       #pragma omp parallel for schedule(static) shared(status) \
768         magick_number_threads(image,image,image->rows/tile_height,1)
769 #endif
770       for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
771       {
772         register ssize_t
773           tile_x;
774 
775         if (status == MagickFalse)
776           continue;
777         tile_x=0;
778         for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
779         {
780           MagickBooleanType
781             sync;
782 
783           register const Quantum
784             *magick_restrict p;
785 
786           register Quantum
787             *magick_restrict q;
788 
789           register ssize_t
790             y;
791 
792           size_t
793             height,
794             width;
795 
796           width=tile_width;
797           if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
798             width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
799           height=tile_height;
800           if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
801             height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
802           p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
803             exception);
804           if (p == (const Quantum *) NULL)
805             {
806               status=MagickFalse;
807               break;
808             }
809           for (y=0; y < (ssize_t) width; y++)
810           {
811             register const Quantum
812               *magick_restrict tile_pixels;
813 
814             register ssize_t
815               x;
816 
817             if (status == MagickFalse)
818               continue;
819             q=QueueCacheViewAuthenticPixels(rotate_view,(ssize_t)
820               (rotate_image->columns-(tile_y+height)),y+tile_x,height,1,
821               exception);
822             if (q == (Quantum *) NULL)
823               {
824                 status=MagickFalse;
825                 continue;
826               }
827             tile_pixels=p+((height-1)*width+y)*GetPixelChannels(image);
828             for (x=0; x < (ssize_t) height; x++)
829             {
830               register ssize_t
831                 i;
832 
833               for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
834               {
835                 PixelChannel channel = GetPixelChannelChannel(image,i);
836                 PixelTrait traits = GetPixelChannelTraits(image,channel);
837                 PixelTrait rotate_traits=GetPixelChannelTraits(rotate_image,
838                   channel);
839                 if ((traits == UndefinedPixelTrait) ||
840                     (rotate_traits == UndefinedPixelTrait))
841                   continue;
842                 SetPixelChannel(rotate_image,channel,tile_pixels[i],q);
843               }
844               tile_pixels-=width*GetPixelChannels(image);
845               q+=GetPixelChannels(rotate_image);
846             }
847             sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
848             if (sync == MagickFalse)
849               status=MagickFalse;
850           }
851         }
852         if (image->progress_monitor != (MagickProgressMonitor) NULL)
853           {
854             MagickBooleanType
855               proceed;
856 
857             proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
858               image->rows);
859             if (proceed == MagickFalse)
860               status=MagickFalse;
861           }
862       }
863       (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
864         image->rows-1,image->rows);
865       Swap(page.width,page.height);
866       Swap(page.x,page.y);
867       if (page.width != 0)
868         page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
869       break;
870     }
871     case 2:
872     {
873       register ssize_t
874         y;
875 
876       /*
877         Rotate 180 degrees.
878       */
879 #if defined(MAGICKCORE_OPENMP_SUPPORT)
880       #pragma omp parallel for schedule(static) shared(status) \
881         magick_number_threads(image,image,image->rows,1)
882 #endif
883       for (y=0; y < (ssize_t) image->rows; y++)
884       {
885         MagickBooleanType
886           sync;
887 
888         register const Quantum
889           *magick_restrict p;
890 
891         register Quantum
892           *magick_restrict q;
893 
894         register ssize_t
895           x;
896 
897         if (status == MagickFalse)
898           continue;
899         p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
900         q=QueueCacheViewAuthenticPixels(rotate_view,0,(ssize_t) (image->rows-y-
901           1),image->columns,1,exception);
902         if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
903           {
904             status=MagickFalse;
905             continue;
906           }
907         q+=GetPixelChannels(rotate_image)*image->columns;
908         for (x=0; x < (ssize_t) image->columns; x++)
909         {
910           register ssize_t
911             i;
912 
913           q-=GetPixelChannels(rotate_image);
914           for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
915           {
916             PixelChannel channel = GetPixelChannelChannel(image,i);
917             PixelTrait traits = GetPixelChannelTraits(image,channel);
918             PixelTrait rotate_traits=GetPixelChannelTraits(rotate_image,
919               channel);
920             if ((traits == UndefinedPixelTrait) ||
921                 (rotate_traits == UndefinedPixelTrait))
922               continue;
923             SetPixelChannel(rotate_image,channel,p[i],q);
924           }
925           p+=GetPixelChannels(image);
926         }
927         sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
928         if (sync == MagickFalse)
929           status=MagickFalse;
930         if (image->progress_monitor != (MagickProgressMonitor) NULL)
931           {
932             MagickBooleanType
933               proceed;
934 
935             proceed=SetImageProgress(image,RotateImageTag,progress++,
936               image->rows);
937             if (proceed == MagickFalse)
938               status=MagickFalse;
939           }
940       }
941       (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
942         image->rows-1,image->rows);
943       if (page.width != 0)
944         page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
945       if (page.height != 0)
946         page.y=(ssize_t) (page.height-rotate_image->rows-page.y);
947       break;
948     }
949     case 3:
950     {
951       size_t
952         tile_height,
953         tile_width;
954 
955       ssize_t
956         tile_y;
957 
958       /*
959         Rotate 270 degrees.
960       */
961       GetPixelCacheTileSize(image,&tile_width,&tile_height);
962       tile_width=image->columns;
963 #if defined(MAGICKCORE_OPENMP_SUPPORT)
964       #pragma omp parallel for schedule(static) shared(status) \
965         magick_number_threads(image,image,image->rows/tile_height,1)
966 #endif
967       for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
968       {
969         register ssize_t
970           tile_x;
971 
972         if (status == MagickFalse)
973           continue;
974         tile_x=0;
975         for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
976         {
977           MagickBooleanType
978             sync;
979 
980           register const Quantum
981             *magick_restrict p;
982 
983           register Quantum
984             *magick_restrict q;
985 
986           register ssize_t
987             y;
988 
989           size_t
990             height,
991             width;
992 
993           width=tile_width;
994           if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
995             width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
996           height=tile_height;
997           if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
998             height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
999           p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1000             exception);
1001           if (p == (const Quantum *) NULL)
1002             {
1003               status=MagickFalse;
1004               break;
1005             }
1006           for (y=0; y < (ssize_t) width; y++)
1007           {
1008             register const Quantum
1009               *magick_restrict tile_pixels;
1010 
1011             register ssize_t
1012               x;
1013 
1014             if (status == MagickFalse)
1015               continue;
1016             q=QueueCacheViewAuthenticPixels(rotate_view,tile_y,(ssize_t) (y+
1017               rotate_image->rows-(tile_x+width)),height,1,exception);
1018             if (q == (Quantum *) NULL)
1019               {
1020                 status=MagickFalse;
1021                 continue;
1022               }
1023             tile_pixels=p+((width-1)-y)*GetPixelChannels(image);
1024             for (x=0; x < (ssize_t) height; x++)
1025             {
1026               register ssize_t
1027                 i;
1028 
1029               for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1030               {
1031                 PixelChannel channel = GetPixelChannelChannel(image,i);
1032                 PixelTrait traits = GetPixelChannelTraits(image,channel);
1033                 PixelTrait rotate_traits=GetPixelChannelTraits(rotate_image,
1034                   channel);
1035                 if ((traits == UndefinedPixelTrait) ||
1036                     (rotate_traits == UndefinedPixelTrait))
1037                   continue;
1038                 SetPixelChannel(rotate_image,channel,tile_pixels[i],q);
1039               }
1040               tile_pixels+=width*GetPixelChannels(image);
1041               q+=GetPixelChannels(rotate_image);
1042             }
1043 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1044             #pragma omp critical (MagickCore_IntegralRotateImage)
1045 #endif
1046             sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1047             if (sync == MagickFalse)
1048               status=MagickFalse;
1049           }
1050         }
1051         if (image->progress_monitor != (MagickProgressMonitor) NULL)
1052           {
1053             MagickBooleanType
1054               proceed;
1055 
1056             proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
1057               image->rows);
1058             if (proceed == MagickFalse)
1059               status=MagickFalse;
1060           }
1061       }
1062       (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1063         image->rows-1,image->rows);
1064       Swap(page.width,page.height);
1065       Swap(page.x,page.y);
1066       if (page.height != 0)
1067         page.y=(ssize_t) (page.height-rotate_image->rows-page.y);
1068       break;
1069     }
1070     default:
1071       break;
1072   }
1073   rotate_view=DestroyCacheView(rotate_view);
1074   image_view=DestroyCacheView(image_view);
1075   rotate_image->type=image->type;
1076   rotate_image->page=page;
1077   if (status == MagickFalse)
1078     rotate_image=DestroyImage(rotate_image);
1079   return(rotate_image);
1080 }
1081 
1082 /*
1083 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1084 %                                                                             %
1085 %                                                                             %
1086 %                                                                             %
1087 +   X S h e a r I m a g e                                                     %
1088 %                                                                             %
1089 %                                                                             %
1090 %                                                                             %
1091 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1092 %
1093 %  XShearImage() shears the image in the X direction with a shear angle of
1094 %  'degrees'.  Positive angles shear counter-clockwise (right-hand rule), and
1095 %  negative angles shear clockwise.  Angles are measured relative to a vertical
1096 %  Y-axis.  X shears will widen an image creating 'empty' triangles on the left
1097 %  and right sides of the source image.
1098 %
1099 %  The format of the XShearImage method is:
1100 %
1101 %      MagickBooleanType XShearImage(Image *image,const double degrees,
1102 %        const size_t width,const size_t height,
1103 %        const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
1104 %
1105 %  A description of each parameter follows.
1106 %
1107 %    o image: the image.
1108 %
1109 %    o degrees: A double representing the shearing angle along the X
1110 %      axis.
1111 %
1112 %    o width, height, x_offset, y_offset: Defines a region of the image
1113 %      to shear.
1114 %
1115 %    o exception: return any errors or warnings in this structure.
1116 %
1117 */
XShearImage(Image * image,const double degrees,const size_t width,const size_t height,const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo * exception)1118 static MagickBooleanType XShearImage(Image *image,const double degrees,
1119   const size_t width,const size_t height,const ssize_t x_offset,
1120   const ssize_t y_offset,ExceptionInfo *exception)
1121 {
1122 #define XShearImageTag  "XShear/Image"
1123 
1124   typedef enum
1125   {
1126     LEFT,
1127     RIGHT
1128   } ShearDirection;
1129 
1130   CacheView
1131     *image_view;
1132 
1133   MagickBooleanType
1134     status;
1135 
1136   MagickOffsetType
1137     progress;
1138 
1139   PixelInfo
1140     background;
1141 
1142   ssize_t
1143     y;
1144 
1145   /*
1146     X shear image.
1147   */
1148   assert(image != (Image *) NULL);
1149   assert(image->signature == MagickCoreSignature);
1150   if (image->debug != MagickFalse)
1151     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1152   status=MagickTrue;
1153   background=image->background_color;
1154   progress=0;
1155   image_view=AcquireAuthenticCacheView(image,exception);
1156 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1157   #pragma omp parallel for schedule(static) shared(progress,status) \
1158     magick_number_threads(image,image,height,1)
1159 #endif
1160   for (y=0; y < (ssize_t) height; y++)
1161   {
1162     PixelInfo
1163       pixel,
1164       source,
1165       destination;
1166 
1167     double
1168       area,
1169       displacement;
1170 
1171     register Quantum
1172       *magick_restrict p,
1173       *magick_restrict q;
1174 
1175     register ssize_t
1176       i;
1177 
1178     ShearDirection
1179       direction;
1180 
1181     ssize_t
1182       step;
1183 
1184     if (status == MagickFalse)
1185       continue;
1186     p=GetCacheViewAuthenticPixels(image_view,0,y_offset+y,image->columns,1,
1187       exception);
1188     if (p == (Quantum *) NULL)
1189       {
1190         status=MagickFalse;
1191         continue;
1192       }
1193     p+=x_offset*GetPixelChannels(image);
1194     displacement=degrees*(double) (y-height/2.0);
1195     if (displacement == 0.0)
1196       continue;
1197     if (displacement > 0.0)
1198       direction=RIGHT;
1199     else
1200       {
1201         displacement*=(-1.0);
1202         direction=LEFT;
1203       }
1204     step=(ssize_t) floor((double) displacement);
1205     area=(double) (displacement-step);
1206     step++;
1207     pixel=background;
1208     GetPixelInfo(image,&source);
1209     GetPixelInfo(image,&destination);
1210     switch (direction)
1211     {
1212       case LEFT:
1213       {
1214         /*
1215           Transfer pixels left-to-right.
1216         */
1217         if (step > x_offset)
1218           break;
1219         q=p-step*GetPixelChannels(image);
1220         for (i=0; i < (ssize_t) width; i++)
1221         {
1222           if ((x_offset+i) < step)
1223             {
1224               p+=GetPixelChannels(image);
1225               GetPixelInfoPixel(image,p,&pixel);
1226               q+=GetPixelChannels(image);
1227               continue;
1228             }
1229           GetPixelInfoPixel(image,p,&source);
1230           CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1231             &source,(double) GetPixelAlpha(image,p),area,&destination);
1232           SetPixelViaPixelInfo(image,&destination,q);
1233           GetPixelInfoPixel(image,p,&pixel);
1234           p+=GetPixelChannels(image);
1235           q+=GetPixelChannels(image);
1236         }
1237         CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1238           &background,(double) background.alpha,area,&destination);
1239         SetPixelViaPixelInfo(image,&destination,q);
1240         q+=GetPixelChannels(image);
1241         for (i=0; i < (step-1); i++)
1242         {
1243           SetPixelViaPixelInfo(image,&background,q);
1244           q+=GetPixelChannels(image);
1245         }
1246         break;
1247       }
1248       case RIGHT:
1249       {
1250         /*
1251           Transfer pixels right-to-left.
1252         */
1253         p+=width*GetPixelChannels(image);
1254         q=p+step*GetPixelChannels(image);
1255         for (i=0; i < (ssize_t) width; i++)
1256         {
1257           p-=GetPixelChannels(image);
1258           q-=GetPixelChannels(image);
1259           if ((size_t) (x_offset+width+step-i) > image->columns)
1260             continue;
1261           GetPixelInfoPixel(image,p,&source);
1262           CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1263             &source,(double) GetPixelAlpha(image,p),area,&destination);
1264           SetPixelViaPixelInfo(image,&destination,q);
1265           GetPixelInfoPixel(image,p,&pixel);
1266         }
1267         CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1268           &background,(double) background.alpha,area,&destination);
1269         q-=GetPixelChannels(image);
1270         SetPixelViaPixelInfo(image,&destination,q);
1271         for (i=0; i < (step-1); i++)
1272         {
1273           q-=GetPixelChannels(image);
1274           SetPixelViaPixelInfo(image,&background,q);
1275         }
1276         break;
1277       }
1278     }
1279     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1280       status=MagickFalse;
1281     if (image->progress_monitor != (MagickProgressMonitor) NULL)
1282       {
1283         MagickBooleanType
1284           proceed;
1285 
1286 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1287         #pragma omp atomic
1288 #endif
1289         progress++;
1290         proceed=SetImageProgress(image,XShearImageTag,progress,height);
1291         if (proceed == MagickFalse)
1292           status=MagickFalse;
1293       }
1294   }
1295   image_view=DestroyCacheView(image_view);
1296   return(status);
1297 }
1298 
1299 /*
1300 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1301 %                                                                             %
1302 %                                                                             %
1303 %                                                                             %
1304 +   Y S h e a r I m a g e                                                     %
1305 %                                                                             %
1306 %                                                                             %
1307 %                                                                             %
1308 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1309 %
1310 %  YShearImage shears the image in the Y direction with a shear angle of
1311 %  'degrees'.  Positive angles shear counter-clockwise (right-hand rule), and
1312 %  negative angles shear clockwise.  Angles are measured relative to a
1313 %  horizontal X-axis.  Y shears will increase the height of an image creating
1314 %  'empty' triangles on the top and bottom of the source image.
1315 %
1316 %  The format of the YShearImage method is:
1317 %
1318 %      MagickBooleanType YShearImage(Image *image,const double degrees,
1319 %        const size_t width,const size_t height,
1320 %        const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
1321 %
1322 %  A description of each parameter follows.
1323 %
1324 %    o image: the image.
1325 %
1326 %    o degrees: A double representing the shearing angle along the Y
1327 %      axis.
1328 %
1329 %    o width, height, x_offset, y_offset: Defines a region of the image
1330 %      to shear.
1331 %
1332 %    o exception: return any errors or warnings in this structure.
1333 %
1334 */
YShearImage(Image * image,const double degrees,const size_t width,const size_t height,const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo * exception)1335 static MagickBooleanType YShearImage(Image *image,const double degrees,
1336   const size_t width,const size_t height,const ssize_t x_offset,
1337   const ssize_t y_offset,ExceptionInfo *exception)
1338 {
1339 #define YShearImageTag  "YShear/Image"
1340 
1341   typedef enum
1342   {
1343     UP,
1344     DOWN
1345   } ShearDirection;
1346 
1347   CacheView
1348     *image_view;
1349 
1350   MagickBooleanType
1351     status;
1352 
1353   MagickOffsetType
1354     progress;
1355 
1356   PixelInfo
1357     background;
1358 
1359   ssize_t
1360     x;
1361 
1362   /*
1363     Y Shear image.
1364   */
1365   assert(image != (Image *) NULL);
1366   assert(image->signature == MagickCoreSignature);
1367   if (image->debug != MagickFalse)
1368     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1369   status=MagickTrue;
1370   progress=0;
1371   background=image->background_color;
1372   image_view=AcquireAuthenticCacheView(image,exception);
1373 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1374   #pragma omp parallel for schedule(static) shared(progress,status) \
1375     magick_number_threads(image,image,width,1)
1376 #endif
1377   for (x=0; x < (ssize_t) width; x++)
1378   {
1379     ssize_t
1380       step;
1381 
1382     double
1383       area,
1384       displacement;
1385 
1386     PixelInfo
1387       pixel,
1388       source,
1389       destination;
1390 
1391     register Quantum
1392       *magick_restrict p,
1393       *magick_restrict q;
1394 
1395     register ssize_t
1396       i;
1397 
1398     ShearDirection
1399       direction;
1400 
1401     if (status == MagickFalse)
1402       continue;
1403     p=GetCacheViewAuthenticPixels(image_view,x_offset+x,0,1,image->rows,
1404       exception);
1405     if (p == (Quantum *) NULL)
1406       {
1407         status=MagickFalse;
1408         continue;
1409       }
1410     p+=y_offset*GetPixelChannels(image);
1411     displacement=degrees*(double) (x-width/2.0);
1412     if (displacement == 0.0)
1413       continue;
1414     if (displacement > 0.0)
1415       direction=DOWN;
1416     else
1417       {
1418         displacement*=(-1.0);
1419         direction=UP;
1420       }
1421     step=(ssize_t) floor((double) displacement);
1422     area=(double) (displacement-step);
1423     step++;
1424     pixel=background;
1425     GetPixelInfo(image,&source);
1426     GetPixelInfo(image,&destination);
1427     switch (direction)
1428     {
1429       case UP:
1430       {
1431         /*
1432           Transfer pixels top-to-bottom.
1433         */
1434         if (step > y_offset)
1435           break;
1436         q=p-step*GetPixelChannels(image);
1437         for (i=0; i < (ssize_t) height; i++)
1438         {
1439           if ((y_offset+i) < step)
1440             {
1441               p+=GetPixelChannels(image);
1442               GetPixelInfoPixel(image,p,&pixel);
1443               q+=GetPixelChannels(image);
1444               continue;
1445             }
1446           GetPixelInfoPixel(image,p,&source);
1447           CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1448             &source,(double) GetPixelAlpha(image,p),area,
1449             &destination);
1450           SetPixelViaPixelInfo(image,&destination,q);
1451           GetPixelInfoPixel(image,p,&pixel);
1452           p+=GetPixelChannels(image);
1453           q+=GetPixelChannels(image);
1454         }
1455         CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1456           &background,(double) background.alpha,area,&destination);
1457         SetPixelViaPixelInfo(image,&destination,q);
1458         q+=GetPixelChannels(image);
1459         for (i=0; i < (step-1); i++)
1460         {
1461           SetPixelViaPixelInfo(image,&background,q);
1462           q+=GetPixelChannels(image);
1463         }
1464         break;
1465       }
1466       case DOWN:
1467       {
1468         /*
1469           Transfer pixels bottom-to-top.
1470         */
1471         p+=height*GetPixelChannels(image);
1472         q=p+step*GetPixelChannels(image);
1473         for (i=0; i < (ssize_t) height; i++)
1474         {
1475           p-=GetPixelChannels(image);
1476           q-=GetPixelChannels(image);
1477           if ((size_t) (y_offset+height+step-i) > image->rows)
1478             continue;
1479           GetPixelInfoPixel(image,p,&source);
1480           CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1481             &source,(double) GetPixelAlpha(image,p),area,
1482             &destination);
1483           SetPixelViaPixelInfo(image,&destination,q);
1484           GetPixelInfoPixel(image,p,&pixel);
1485         }
1486         CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1487           &background,(double) background.alpha,area,&destination);
1488         q-=GetPixelChannels(image);
1489         SetPixelViaPixelInfo(image,&destination,q);
1490         for (i=0; i < (step-1); i++)
1491         {
1492           q-=GetPixelChannels(image);
1493           SetPixelViaPixelInfo(image,&background,q);
1494         }
1495         break;
1496       }
1497     }
1498     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1499       status=MagickFalse;
1500     if (image->progress_monitor != (MagickProgressMonitor) NULL)
1501       {
1502         MagickBooleanType
1503           proceed;
1504 
1505 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1506         #pragma omp atomic
1507 #endif
1508         progress++;
1509         proceed=SetImageProgress(image,YShearImageTag,progress,image->rows);
1510         if (proceed == MagickFalse)
1511           status=MagickFalse;
1512       }
1513   }
1514   image_view=DestroyCacheView(image_view);
1515   return(status);
1516 }
1517 
1518 /*
1519 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1520 %                                                                             %
1521 %                                                                             %
1522 %                                                                             %
1523 %   S h e a r I m a g e                                                       %
1524 %                                                                             %
1525 %                                                                             %
1526 %                                                                             %
1527 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1528 %
1529 %  ShearImage() creates a new image that is a shear_image copy of an existing
1530 %  one.  Shearing slides one edge of an image along the X or Y axis, creating
1531 %  a parallelogram.  An X direction shear slides an edge along the X axis,
1532 %  while a Y direction shear slides an edge along the Y axis.  The amount of
1533 %  the shear is controlled by a shear angle.  For X direction shears, x_shear
1534 %  is measured relative to the Y axis, and similarly, for Y direction shears
1535 %  y_shear is measured relative to the X axis.  Empty triangles left over from
1536 %  shearing the image are filled with the background color defined by member
1537 %  'background_color' of the image..  ShearImage() allocates the memory
1538 %  necessary for the new Image structure and returns a pointer to the new image.
1539 %
1540 %  ShearImage() is based on the paper "A Fast Algorithm for General Raster
1541 %  Rotatation" by Alan W. Paeth.
1542 %
1543 %  The format of the ShearImage method is:
1544 %
1545 %      Image *ShearImage(const Image *image,const double x_shear,
1546 %        const double y_shear,ExceptionInfo *exception)
1547 %
1548 %  A description of each parameter follows.
1549 %
1550 %    o image: the image.
1551 %
1552 %    o x_shear, y_shear: Specifies the number of degrees to shear the image.
1553 %
1554 %    o exception: return any errors or warnings in this structure.
1555 %
1556 */
ShearImage(const Image * image,const double x_shear,const double y_shear,ExceptionInfo * exception)1557 MagickExport Image *ShearImage(const Image *image,const double x_shear,
1558   const double y_shear,ExceptionInfo *exception)
1559 {
1560   Image
1561     *integral_image,
1562     *shear_image;
1563 
1564   MagickBooleanType
1565     status;
1566 
1567   PointInfo
1568     shear;
1569 
1570   RectangleInfo
1571     border_info,
1572     bounds;
1573 
1574   assert(image != (Image *) NULL);
1575   assert(image->signature == MagickCoreSignature);
1576   if (image->debug != MagickFalse)
1577     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1578   assert(exception != (ExceptionInfo *) NULL);
1579   assert(exception->signature == MagickCoreSignature);
1580   if ((x_shear != 0.0) && (fmod(x_shear,90.0) == 0.0))
1581     ThrowImageException(ImageError,"AngleIsDiscontinuous");
1582   if ((y_shear != 0.0) && (fmod(y_shear,90.0) == 0.0))
1583     ThrowImageException(ImageError,"AngleIsDiscontinuous");
1584   /*
1585     Initialize shear angle.
1586   */
1587   integral_image=CloneImage(image,0,0,MagickTrue,exception);
1588   if (integral_image == (Image *) NULL)
1589     ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1590   shear.x=(-tan(DegreesToRadians(fmod(x_shear,360.0))));
1591   shear.y=tan(DegreesToRadians(fmod(y_shear,360.0)));
1592   if ((shear.x == 0.0) && (shear.y == 0.0))
1593     return(integral_image);
1594   if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
1595     {
1596       integral_image=DestroyImage(integral_image);
1597       return(integral_image);
1598     }
1599   if (integral_image->alpha_trait == UndefinedPixelTrait)
1600     (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
1601   /*
1602     Compute image size.
1603   */
1604   bounds.width=image->columns+(ssize_t) floor(fabs(shear.x)*image->rows+0.5);
1605   bounds.x=(ssize_t) ceil((double) image->columns+((fabs(shear.x)*image->rows)-
1606     image->columns)/2.0-0.5);
1607   bounds.y=(ssize_t) ceil((double) image->rows+((fabs(shear.y)*bounds.width)-
1608     image->rows)/2.0-0.5);
1609   /*
1610     Surround image with border.
1611   */
1612   integral_image->border_color=integral_image->background_color;
1613   integral_image->compose=CopyCompositeOp;
1614   border_info.width=(size_t) bounds.x;
1615   border_info.height=(size_t) bounds.y;
1616   shear_image=BorderImage(integral_image,&border_info,image->compose,exception);
1617   integral_image=DestroyImage(integral_image);
1618   if (shear_image == (Image *) NULL)
1619     ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1620   /*
1621     Shear the image.
1622   */
1623   if (shear_image->alpha_trait == UndefinedPixelTrait)
1624     (void) SetImageAlphaChannel(shear_image,OpaqueAlphaChannel,exception);
1625   status=XShearImage(shear_image,shear.x,image->columns,image->rows,bounds.x,
1626     (ssize_t) (shear_image->rows-image->rows)/2,exception);
1627   if (status == MagickFalse)
1628     {
1629       shear_image=DestroyImage(shear_image);
1630       return((Image *) NULL);
1631     }
1632   status=YShearImage(shear_image,shear.y,bounds.width,image->rows,(ssize_t)
1633     (shear_image->columns-bounds.width)/2,bounds.y,exception);
1634   if (status == MagickFalse)
1635     {
1636       shear_image=DestroyImage(shear_image);
1637       return((Image *) NULL);
1638     }
1639   status=CropToFitImage(&shear_image,shear.x,shear.y,(MagickRealType)
1640     image->columns,(MagickRealType) image->rows,MagickFalse,exception);
1641   shear_image->alpha_trait=image->alpha_trait;
1642   shear_image->compose=image->compose;
1643   shear_image->page.width=0;
1644   shear_image->page.height=0;
1645   if (status == MagickFalse)
1646     shear_image=DestroyImage(shear_image);
1647   return(shear_image);
1648 }
1649 
1650 /*
1651 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1652 %                                                                             %
1653 %                                                                             %
1654 %                                                                             %
1655 %   S h e a r R o t a t e I m a g e                                           %
1656 %                                                                             %
1657 %                                                                             %
1658 %                                                                             %
1659 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1660 %
1661 %  ShearRotateImage() creates a new image that is a rotated copy of an existing
1662 %  one.  Positive angles rotate counter-clockwise (right-hand rule), while
1663 %  negative angles rotate clockwise.  Rotated images are usually larger than
1664 %  the originals and have 'empty' triangular corners.  X axis.  Empty
1665 %  triangles left over from shearing the image are filled with the background
1666 %  color defined by member 'background_color' of the image.  ShearRotateImage
1667 %  allocates the memory necessary for the new Image structure and returns a
1668 %  pointer to the new image.
1669 %
1670 %  ShearRotateImage() is based on the paper "A Fast Algorithm for General
1671 %  Raster Rotatation" by Alan W. Paeth.  ShearRotateImage is adapted from a
1672 %  similar method based on the Paeth paper written by Michael Halle of the
1673 %  Spatial Imaging Group, MIT Media Lab.
1674 %
1675 %  The format of the ShearRotateImage method is:
1676 %
1677 %      Image *ShearRotateImage(const Image *image,const double degrees,
1678 %        ExceptionInfo *exception)
1679 %
1680 %  A description of each parameter follows.
1681 %
1682 %    o image: the image.
1683 %
1684 %    o degrees: Specifies the number of degrees to rotate the image.
1685 %
1686 %    o exception: return any errors or warnings in this structure.
1687 %
1688 */
ShearRotateImage(const Image * image,const double degrees,ExceptionInfo * exception)1689 MagickExport Image *ShearRotateImage(const Image *image,const double degrees,
1690   ExceptionInfo *exception)
1691 {
1692   Image
1693     *integral_image,
1694     *rotate_image;
1695 
1696   MagickBooleanType
1697     status;
1698 
1699   MagickRealType
1700     angle;
1701 
1702   PointInfo
1703     shear;
1704 
1705   RectangleInfo
1706     border_info,
1707     bounds;
1708 
1709   size_t
1710     height,
1711     rotations,
1712     shear_width,
1713     width;
1714 
1715   /*
1716     Adjust rotation angle.
1717   */
1718   assert(image != (Image *) NULL);
1719   assert(image->signature == MagickCoreSignature);
1720   if (image->debug != MagickFalse)
1721     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1722   assert(exception != (ExceptionInfo *) NULL);
1723   assert(exception->signature == MagickCoreSignature);
1724   angle=fmod(degrees,360.0);
1725   if (angle < -45.0)
1726     angle+=360.0;
1727   for (rotations=0; angle > 45.0; rotations++)
1728     angle-=90.0;
1729   rotations%=4;
1730   /*
1731     Calculate shear equations.
1732   */
1733   integral_image=IntegralRotateImage(image,rotations,exception);
1734   if (integral_image == (Image *) NULL)
1735     ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1736   shear.x=(-tan((double) DegreesToRadians(angle)/2.0));
1737   shear.y=sin((double) DegreesToRadians(angle));
1738   if ((shear.x == 0.0) && (shear.y == 0.0))
1739     return(integral_image);
1740   if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
1741     {
1742       integral_image=DestroyImage(integral_image);
1743       return(integral_image);
1744     }
1745   if (integral_image->alpha_trait == UndefinedPixelTrait)
1746     (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
1747   /*
1748     Compute maximum bounds for 3 shear operations.
1749   */
1750   width=integral_image->columns;
1751   height=integral_image->rows;
1752   bounds.width=(size_t) floor(fabs((double) height*shear.x)+width+0.5);
1753   bounds.height=(size_t) floor(fabs((double) bounds.width*shear.y)+height+0.5);
1754   shear_width=(size_t) floor(fabs((double) bounds.height*shear.x)+
1755     bounds.width+0.5);
1756   bounds.x=(ssize_t) floor((double) ((shear_width > bounds.width) ? width :
1757     bounds.width-shear_width+2)/2.0+0.5);
1758   bounds.y=(ssize_t) floor(((double) bounds.height-height+2)/2.0+0.5);
1759   /*
1760     Surround image with a border.
1761   */
1762   integral_image->border_color=integral_image->background_color;
1763   integral_image->compose=CopyCompositeOp;
1764   border_info.width=(size_t) bounds.x;
1765   border_info.height=(size_t) bounds.y;
1766   rotate_image=BorderImage(integral_image,&border_info,image->compose,
1767     exception);
1768   integral_image=DestroyImage(integral_image);
1769   if (rotate_image == (Image *) NULL)
1770     ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1771   /*
1772     Rotate the image.
1773   */
1774   status=XShearImage(rotate_image,shear.x,width,height,bounds.x,(ssize_t)
1775     (rotate_image->rows-height)/2,exception);
1776   if (status == MagickFalse)
1777     {
1778       rotate_image=DestroyImage(rotate_image);
1779       return((Image *) NULL);
1780     }
1781   status=YShearImage(rotate_image,shear.y,bounds.width,height,(ssize_t)
1782     (rotate_image->columns-bounds.width)/2,bounds.y,exception);
1783   if (status == MagickFalse)
1784     {
1785       rotate_image=DestroyImage(rotate_image);
1786       return((Image *) NULL);
1787     }
1788   status=XShearImage(rotate_image,shear.x,bounds.width,bounds.height,(ssize_t)
1789     (rotate_image->columns-bounds.width)/2,(ssize_t) (rotate_image->rows-
1790     bounds.height)/2,exception);
1791   if (status == MagickFalse)
1792     {
1793       rotate_image=DestroyImage(rotate_image);
1794       return((Image *) NULL);
1795     }
1796   status=CropToFitImage(&rotate_image,shear.x,shear.y,(MagickRealType) width,
1797     (MagickRealType) height,MagickTrue,exception);
1798   rotate_image->alpha_trait=image->alpha_trait;
1799   rotate_image->compose=image->compose;
1800   rotate_image->page.width=0;
1801   rotate_image->page.height=0;
1802   if (status == MagickFalse)
1803     rotate_image=DestroyImage(rotate_image);
1804   return(rotate_image);
1805 }
1806