MagickCore  7.0.9
shear.c
Go to the documentation of this file.
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-2020 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"
52 #include "MagickCore/channel.h"
55 #include "MagickCore/composite.h"
57 #include "MagickCore/decorate.h"
58 #include "MagickCore/distort.h"
59 #include "MagickCore/draw.h"
60 #include "MagickCore/exception.h"
62 #include "MagickCore/gem.h"
63 #include "MagickCore/geometry.h"
64 #include "MagickCore/image.h"
66 #include "MagickCore/matrix.h"
67 #include "MagickCore/memory_.h"
68 #include "MagickCore/list.h"
69 #include "MagickCore/monitor.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"
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 */
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 
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 
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 
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 
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 
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);
551  background.red/count);
553  background.green/count);
555  background.blue/count);
556  if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
558  background.alpha/count);
559 }
560 
561 MagickExport Image *DeskewImage(const Image *image,const double threshold,
562  ExceptionInfo *exception)
563 {
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 
580  status;
581 
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)
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  }
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 */
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 
719  status;
720 
722  progress;
723 
725  page;
726 
727  /*
728  Initialize rotated image attributes.
729  */
730  assert(image != (Image *) NULL);
731  page=image->page;
732  rotations%=4;
733  switch (rotations)
734  {
735  case 0:
736  {
737  rotate_image=CloneImage(image,0,0,MagickTrue,exception);
738  break;
739  }
740  case 2:
741  {
742  rotate_image=CloneImage(image,image->columns,image->rows,MagickTrue,
743  exception);
744  break;
745  }
746  case 1:
747  case 3:
748  {
749  rotate_image=CloneImage(image,image->rows,image->columns,MagickTrue,
750  exception);
751  break;
752  }
753  }
754  if (rotate_image == (Image *) NULL)
755  return((Image *) NULL);
756  /*
757  Integral rotate the image.
758  */
759  status=MagickTrue;
760  progress=0;
761  if (rotations != 0)
762  {
763  image_view=AcquireVirtualCacheView(image,exception);
764  rotate_view=AcquireAuthenticCacheView(rotate_image,exception);
765  }
766  switch (rotations)
767  {
768  case 1:
769  {
770  size_t
771  tile_height,
772  tile_width;
773 
774  ssize_t
775  tile_y;
776 
777  /*
778  Rotate 90 degrees.
779  */
780  GetPixelCacheTileSize(image,&tile_width,&tile_height);
781  tile_width=image->columns;
782 #if defined(MAGICKCORE_OPENMP_SUPPORT)
783  #pragma omp parallel for schedule(static) shared(status) \
784  magick_number_threads(image,rotate_image,image->rows/tile_height,1)
785 #endif
786  for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
787  {
788  register ssize_t
789  tile_x;
790 
791  if (status == MagickFalse)
792  continue;
793  tile_x=0;
794  for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
795  {
797  sync;
798 
799  register const Quantum
800  *magick_restrict p;
801 
802  register Quantum
803  *magick_restrict q;
804 
805  register ssize_t
806  y;
807 
808  size_t
809  height,
810  width;
811 
812  width=tile_width;
813  if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
814  width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
815  height=tile_height;
816  if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
817  height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
818  p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
819  exception);
820  if (p == (const Quantum *) NULL)
821  {
822  status=MagickFalse;
823  break;
824  }
825  for (y=0; y < (ssize_t) width; y++)
826  {
827  register const Quantum
828  *magick_restrict tile_pixels;
829 
830  register ssize_t
831  x;
832 
833  if (status == MagickFalse)
834  continue;
835  q=QueueCacheViewAuthenticPixels(rotate_view,(ssize_t)
836  (rotate_image->columns-(tile_y+height)),y+tile_x,height,1,
837  exception);
838  if (q == (Quantum *) NULL)
839  {
840  status=MagickFalse;
841  continue;
842  }
843  tile_pixels=p+((height-1)*width+y)*GetPixelChannels(image);
844  for (x=0; x < (ssize_t) height; x++)
845  {
846  register ssize_t
847  i;
848 
849  for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
850  {
851  PixelChannel channel = GetPixelChannelChannel(image,i);
852  PixelTrait traits = GetPixelChannelTraits(image,channel);
853  PixelTrait rotate_traits = GetPixelChannelTraits(rotate_image,
854  channel);
855  if ((traits == UndefinedPixelTrait) ||
856  (rotate_traits == UndefinedPixelTrait))
857  continue;
858  SetPixelChannel(rotate_image,channel,tile_pixels[i],q);
859  }
860  tile_pixels-=width*GetPixelChannels(image);
861  q+=GetPixelChannels(rotate_image);
862  }
863  sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
864  if (sync == MagickFalse)
865  status=MagickFalse;
866  }
867  }
868  if (image->progress_monitor != (MagickProgressMonitor) NULL)
869  {
871  proceed;
872 
873  proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
874  image->rows);
875  if (proceed == MagickFalse)
876  status=MagickFalse;
877  }
878  }
880  image->rows-1,image->rows);
881  Swap(page.width,page.height);
882  Swap(page.x,page.y);
883  if (page.width != 0)
884  page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
885  break;
886  }
887  case 2:
888  {
889  register ssize_t
890  y;
891 
892  /*
893  Rotate 180 degrees.
894  */
895 #if defined(MAGICKCORE_OPENMP_SUPPORT)
896  #pragma omp parallel for schedule(static) shared(status) \
897  magick_number_threads(image,rotate_image,image->rows,1)
898 #endif
899  for (y=0; y < (ssize_t) image->rows; y++)
900  {
902  sync;
903 
904  register const Quantum
905  *magick_restrict p;
906 
907  register Quantum
908  *magick_restrict q;
909 
910  register ssize_t
911  x;
912 
913  if (status == MagickFalse)
914  continue;
915  p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
916  q=QueueCacheViewAuthenticPixels(rotate_view,0,(ssize_t) (image->rows-y-
917  1),image->columns,1,exception);
918  if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
919  {
920  status=MagickFalse;
921  continue;
922  }
923  q+=GetPixelChannels(rotate_image)*image->columns;
924  for (x=0; x < (ssize_t) image->columns; x++)
925  {
926  register ssize_t
927  i;
928 
929  q-=GetPixelChannels(rotate_image);
930  for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
931  {
932  PixelChannel channel = GetPixelChannelChannel(image,i);
933  PixelTrait traits = GetPixelChannelTraits(image,channel);
934  PixelTrait rotate_traits = GetPixelChannelTraits(rotate_image,
935  channel);
936  if ((traits == UndefinedPixelTrait) ||
937  (rotate_traits == UndefinedPixelTrait))
938  continue;
939  SetPixelChannel(rotate_image,channel,p[i],q);
940  }
941  p+=GetPixelChannels(image);
942  }
943  sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
944  if (sync == MagickFalse)
945  status=MagickFalse;
946  if (image->progress_monitor != (MagickProgressMonitor) NULL)
947  {
949  proceed;
950 
951  proceed=SetImageProgress(image,RotateImageTag,progress++,
952  image->rows);
953  if (proceed == MagickFalse)
954  status=MagickFalse;
955  }
956  }
958  image->rows-1,image->rows);
959  if (page.width != 0)
960  page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
961  if (page.height != 0)
962  page.y=(ssize_t) (page.height-rotate_image->rows-page.y);
963  break;
964  }
965  case 3:
966  {
967  size_t
968  tile_height,
969  tile_width;
970 
971  ssize_t
972  tile_y;
973 
974  /*
975  Rotate 270 degrees.
976  */
977  GetPixelCacheTileSize(image,&tile_width,&tile_height);
978  tile_width=image->columns;
979 #if defined(MAGICKCORE_OPENMP_SUPPORT)
980  #pragma omp parallel for schedule(static) shared(status) \
981  magick_number_threads(image,rotate_image,image->rows/tile_height,1)
982 #endif
983  for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
984  {
985  register ssize_t
986  tile_x;
987 
988  if (status == MagickFalse)
989  continue;
990  tile_x=0;
991  for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
992  {
994  sync;
995 
996  register const Quantum
997  *magick_restrict p;
998 
999  register Quantum
1000  *magick_restrict q;
1001 
1002  register ssize_t
1003  y;
1004 
1005  size_t
1006  height,
1007  width;
1008 
1009  width=tile_width;
1010  if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
1011  width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
1012  height=tile_height;
1013  if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
1014  height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
1015  p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1016  exception);
1017  if (p == (const Quantum *) NULL)
1018  {
1019  status=MagickFalse;
1020  break;
1021  }
1022  for (y=0; y < (ssize_t) width; y++)
1023  {
1024  register const Quantum
1025  *magick_restrict tile_pixels;
1026 
1027  register ssize_t
1028  x;
1029 
1030  if (status == MagickFalse)
1031  continue;
1032  q=QueueCacheViewAuthenticPixels(rotate_view,tile_y,(ssize_t) (y+
1033  rotate_image->rows-(tile_x+width)),height,1,exception);
1034  if (q == (Quantum *) NULL)
1035  {
1036  status=MagickFalse;
1037  continue;
1038  }
1039  tile_pixels=p+((width-1)-y)*GetPixelChannels(image);
1040  for (x=0; x < (ssize_t) height; x++)
1041  {
1042  register ssize_t
1043  i;
1044 
1045  for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1046  {
1047  PixelChannel channel = GetPixelChannelChannel(image,i);
1048  PixelTrait traits = GetPixelChannelTraits(image,channel);
1049  PixelTrait rotate_traits = GetPixelChannelTraits(rotate_image,
1050  channel);
1051  if ((traits == UndefinedPixelTrait) ||
1052  (rotate_traits == UndefinedPixelTrait))
1053  continue;
1054  SetPixelChannel(rotate_image,channel,tile_pixels[i],q);
1055  }
1056  tile_pixels+=width*GetPixelChannels(image);
1057  q+=GetPixelChannels(rotate_image);
1058  }
1059 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1060  #pragma omp critical (MagickCore_IntegralRotateImage)
1061 #endif
1062  sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1063  if (sync == MagickFalse)
1064  status=MagickFalse;
1065  }
1066  }
1067  if (image->progress_monitor != (MagickProgressMonitor) NULL)
1068  {
1070  proceed;
1071 
1072  proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
1073  image->rows);
1074  if (proceed == MagickFalse)
1075  status=MagickFalse;
1076  }
1077  }
1079  image->rows-1,image->rows);
1080  Swap(page.width,page.height);
1081  Swap(page.x,page.y);
1082  if (page.height != 0)
1083  page.y=(ssize_t) (page.height-rotate_image->rows-page.y);
1084  break;
1085  }
1086  default:
1087  break;
1088  }
1089  if (rotations != 0)
1090  {
1091  rotate_view=DestroyCacheView(rotate_view);
1092  image_view=DestroyCacheView(image_view);
1093  }
1094  rotate_image->type=image->type;
1095  rotate_image->page=page;
1096  if (status == MagickFalse)
1097  rotate_image=DestroyImage(rotate_image);
1098  return(rotate_image);
1099 }
1100 
1101 /*
1102 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1103 % %
1104 % %
1105 % %
1106 + X S h e a r I m a g e %
1107 % %
1108 % %
1109 % %
1110 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1111 %
1112 % XShearImage() shears the image in the X direction with a shear angle of
1113 % 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1114 % negative angles shear clockwise. Angles are measured relative to a vertical
1115 % Y-axis. X shears will widen an image creating 'empty' triangles on the left
1116 % and right sides of the source image.
1117 %
1118 % The format of the XShearImage method is:
1119 %
1120 % MagickBooleanType XShearImage(Image *image,const double degrees,
1121 % const size_t width,const size_t height,
1122 % const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
1123 %
1124 % A description of each parameter follows.
1125 %
1126 % o image: the image.
1127 %
1128 % o degrees: A double representing the shearing angle along the X
1129 % axis.
1130 %
1131 % o width, height, x_offset, y_offset: Defines a region of the image
1132 % to shear.
1133 %
1134 % o exception: return any errors or warnings in this structure.
1135 %
1136 */
1137 static MagickBooleanType XShearImage(Image *image,const double degrees,
1138  const size_t width,const size_t height,const ssize_t x_offset,
1139  const ssize_t y_offset,ExceptionInfo *exception)
1140 {
1141 #define XShearImageTag "XShear/Image"
1142 
1143  typedef enum
1144  {
1145  LEFT,
1146  RIGHT
1147  } ShearDirection;
1148 
1149  CacheView
1150  *image_view;
1151 
1153  status;
1154 
1156  progress;
1157 
1158  PixelInfo
1159  background;
1160 
1161  ssize_t
1162  y;
1163 
1164  /*
1165  X shear image.
1166  */
1167  assert(image != (Image *) NULL);
1168  assert(image->signature == MagickCoreSignature);
1169  if (image->debug != MagickFalse)
1170  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1171  status=MagickTrue;
1172  background=image->background_color;
1173  progress=0;
1174  image_view=AcquireAuthenticCacheView(image,exception);
1175 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1176  #pragma omp parallel for schedule(static) shared(progress,status) \
1177  magick_number_threads(image,image,height,1)
1178 #endif
1179  for (y=0; y < (ssize_t) height; y++)
1180  {
1181  PixelInfo
1182  pixel,
1183  source,
1184  destination;
1185 
1186  double
1187  area,
1188  displacement;
1189 
1190  register Quantum
1191  *magick_restrict p,
1192  *magick_restrict q;
1193 
1194  register ssize_t
1195  i;
1196 
1197  ShearDirection
1198  direction;
1199 
1200  ssize_t
1201  step;
1202 
1203  if (status == MagickFalse)
1204  continue;
1205  p=GetCacheViewAuthenticPixels(image_view,0,y_offset+y,image->columns,1,
1206  exception);
1207  if (p == (Quantum *) NULL)
1208  {
1209  status=MagickFalse;
1210  continue;
1211  }
1212  p+=x_offset*GetPixelChannels(image);
1213  displacement=degrees*(double) (y-height/2.0);
1214  if (displacement == 0.0)
1215  continue;
1216  if (displacement > 0.0)
1217  direction=RIGHT;
1218  else
1219  {
1220  displacement*=(-1.0);
1221  direction=LEFT;
1222  }
1223  step=(ssize_t) floor((double) displacement);
1224  area=(double) (displacement-step);
1225  step++;
1226  pixel=background;
1227  GetPixelInfo(image,&source);
1228  GetPixelInfo(image,&destination);
1229  switch (direction)
1230  {
1231  case LEFT:
1232  {
1233  /*
1234  Transfer pixels left-to-right.
1235  */
1236  if (step > x_offset)
1237  break;
1238  q=p-step*GetPixelChannels(image);
1239  for (i=0; i < (ssize_t) width; i++)
1240  {
1241  if ((x_offset+i) < step)
1242  {
1243  p+=GetPixelChannels(image);
1244  GetPixelInfoPixel(image,p,&pixel);
1245  q+=GetPixelChannels(image);
1246  continue;
1247  }
1248  GetPixelInfoPixel(image,p,&source);
1249  CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1250  &source,(double) GetPixelAlpha(image,p),area,&destination);
1251  SetPixelViaPixelInfo(image,&destination,q);
1252  GetPixelInfoPixel(image,p,&pixel);
1253  p+=GetPixelChannels(image);
1254  q+=GetPixelChannels(image);
1255  }
1256  CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1257  &background,(double) background.alpha,area,&destination);
1258  SetPixelViaPixelInfo(image,&destination,q);
1259  q+=GetPixelChannels(image);
1260  for (i=0; i < (step-1); i++)
1261  {
1262  SetPixelViaPixelInfo(image,&background,q);
1263  q+=GetPixelChannels(image);
1264  }
1265  break;
1266  }
1267  case RIGHT:
1268  {
1269  /*
1270  Transfer pixels right-to-left.
1271  */
1272  p+=width*GetPixelChannels(image);
1273  q=p+step*GetPixelChannels(image);
1274  for (i=0; i < (ssize_t) width; i++)
1275  {
1276  p-=GetPixelChannels(image);
1277  q-=GetPixelChannels(image);
1278  if ((size_t) (x_offset+width+step-i) > image->columns)
1279  continue;
1280  GetPixelInfoPixel(image,p,&source);
1281  CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1282  &source,(double) GetPixelAlpha(image,p),area,&destination);
1283  SetPixelViaPixelInfo(image,&destination,q);
1284  GetPixelInfoPixel(image,p,&pixel);
1285  }
1286  CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1287  &background,(double) background.alpha,area,&destination);
1288  q-=GetPixelChannels(image);
1289  SetPixelViaPixelInfo(image,&destination,q);
1290  for (i=0; i < (step-1); i++)
1291  {
1292  q-=GetPixelChannels(image);
1293  SetPixelViaPixelInfo(image,&background,q);
1294  }
1295  break;
1296  }
1297  }
1298  if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1299  status=MagickFalse;
1300  if (image->progress_monitor != (MagickProgressMonitor) NULL)
1301  {
1303  proceed;
1304 
1305 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1306  #pragma omp atomic
1307 #endif
1308  progress++;
1309  proceed=SetImageProgress(image,XShearImageTag,progress,height);
1310  if (proceed == MagickFalse)
1311  status=MagickFalse;
1312  }
1313  }
1314  image_view=DestroyCacheView(image_view);
1315  return(status);
1316 }
1317 
1318 /*
1319 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1320 % %
1321 % %
1322 % %
1323 + Y S h e a r I m a g e %
1324 % %
1325 % %
1326 % %
1327 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1328 %
1329 % YShearImage shears the image in the Y direction with a shear angle of
1330 % 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1331 % negative angles shear clockwise. Angles are measured relative to a
1332 % horizontal X-axis. Y shears will increase the height of an image creating
1333 % 'empty' triangles on the top and bottom of the source image.
1334 %
1335 % The format of the YShearImage method is:
1336 %
1337 % MagickBooleanType YShearImage(Image *image,const double degrees,
1338 % const size_t width,const size_t height,
1339 % const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
1340 %
1341 % A description of each parameter follows.
1342 %
1343 % o image: the image.
1344 %
1345 % o degrees: A double representing the shearing angle along the Y
1346 % axis.
1347 %
1348 % o width, height, x_offset, y_offset: Defines a region of the image
1349 % to shear.
1350 %
1351 % o exception: return any errors or warnings in this structure.
1352 %
1353 */
1354 static MagickBooleanType YShearImage(Image *image,const double degrees,
1355  const size_t width,const size_t height,const ssize_t x_offset,
1356  const ssize_t y_offset,ExceptionInfo *exception)
1357 {
1358 #define YShearImageTag "YShear/Image"
1359 
1360  typedef enum
1361  {
1362  UP,
1363  DOWN
1364  } ShearDirection;
1365 
1366  CacheView
1367  *image_view;
1368 
1370  status;
1371 
1373  progress;
1374 
1375  PixelInfo
1376  background;
1377 
1378  ssize_t
1379  x;
1380 
1381  /*
1382  Y Shear image.
1383  */
1384  assert(image != (Image *) NULL);
1385  assert(image->signature == MagickCoreSignature);
1386  if (image->debug != MagickFalse)
1387  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1388  status=MagickTrue;
1389  progress=0;
1390  background=image->background_color;
1391  image_view=AcquireAuthenticCacheView(image,exception);
1392 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1393  #pragma omp parallel for schedule(static) shared(progress,status) \
1394  magick_number_threads(image,image,width,1)
1395 #endif
1396  for (x=0; x < (ssize_t) width; x++)
1397  {
1398  double
1399  area,
1400  displacement;
1401 
1402  PixelInfo
1403  pixel,
1404  source,
1405  destination;
1406 
1407  register Quantum
1408  *magick_restrict p,
1409  *magick_restrict q;
1410 
1411  register ssize_t
1412  i;
1413 
1414  ShearDirection
1415  direction;
1416 
1417  ssize_t
1418  step;
1419 
1420  if (status == MagickFalse)
1421  continue;
1422  p=GetCacheViewAuthenticPixels(image_view,x_offset+x,0,1,image->rows,
1423  exception);
1424  if (p == (Quantum *) NULL)
1425  {
1426  status=MagickFalse;
1427  continue;
1428  }
1429  p+=y_offset*GetPixelChannels(image);
1430  displacement=degrees*(double) (x-width/2.0);
1431  if (displacement == 0.0)
1432  continue;
1433  if (displacement > 0.0)
1434  direction=DOWN;
1435  else
1436  {
1437  displacement*=(-1.0);
1438  direction=UP;
1439  }
1440  step=(ssize_t) floor((double) displacement);
1441  area=(double) (displacement-step);
1442  step++;
1443  pixel=background;
1444  GetPixelInfo(image,&source);
1445  GetPixelInfo(image,&destination);
1446  switch (direction)
1447  {
1448  case UP:
1449  {
1450  /*
1451  Transfer pixels top-to-bottom.
1452  */
1453  if (step > y_offset)
1454  break;
1455  q=p-step*GetPixelChannels(image);
1456  for (i=0; i < (ssize_t) height; i++)
1457  {
1458  if ((y_offset+i) < step)
1459  {
1460  p+=GetPixelChannels(image);
1461  GetPixelInfoPixel(image,p,&pixel);
1462  q+=GetPixelChannels(image);
1463  continue;
1464  }
1465  GetPixelInfoPixel(image,p,&source);
1466  CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1467  &source,(double) GetPixelAlpha(image,p),area,
1468  &destination);
1469  SetPixelViaPixelInfo(image,&destination,q);
1470  GetPixelInfoPixel(image,p,&pixel);
1471  p+=GetPixelChannels(image);
1472  q+=GetPixelChannels(image);
1473  }
1474  CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1475  &background,(double) background.alpha,area,&destination);
1476  SetPixelViaPixelInfo(image,&destination,q);
1477  q+=GetPixelChannels(image);
1478  for (i=0; i < (step-1); i++)
1479  {
1480  SetPixelViaPixelInfo(image,&background,q);
1481  q+=GetPixelChannels(image);
1482  }
1483  break;
1484  }
1485  case DOWN:
1486  {
1487  /*
1488  Transfer pixels bottom-to-top.
1489  */
1490  p+=height*GetPixelChannels(image);
1491  q=p+step*GetPixelChannels(image);
1492  for (i=0; i < (ssize_t) height; i++)
1493  {
1494  p-=GetPixelChannels(image);
1495  q-=GetPixelChannels(image);
1496  if ((size_t) (y_offset+height+step-i) > image->rows)
1497  continue;
1498  GetPixelInfoPixel(image,p,&source);
1499  CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1500  &source,(double) GetPixelAlpha(image,p),area,
1501  &destination);
1502  SetPixelViaPixelInfo(image,&destination,q);
1503  GetPixelInfoPixel(image,p,&pixel);
1504  }
1505  CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1506  &background,(double) background.alpha,area,&destination);
1507  q-=GetPixelChannels(image);
1508  SetPixelViaPixelInfo(image,&destination,q);
1509  for (i=0; i < (step-1); i++)
1510  {
1511  q-=GetPixelChannels(image);
1512  SetPixelViaPixelInfo(image,&background,q);
1513  }
1514  break;
1515  }
1516  }
1517  if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1518  status=MagickFalse;
1519  if (image->progress_monitor != (MagickProgressMonitor) NULL)
1520  {
1522  proceed;
1523 
1524 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1525  #pragma omp atomic
1526 #endif
1527  progress++;
1528  proceed=SetImageProgress(image,YShearImageTag,progress,image->rows);
1529  if (proceed == MagickFalse)
1530  status=MagickFalse;
1531  }
1532  }
1533  image_view=DestroyCacheView(image_view);
1534  return(status);
1535 }
1536 
1537 /*
1538 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1539 % %
1540 % %
1541 % %
1542 % S h e a r I m a g e %
1543 % %
1544 % %
1545 % %
1546 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1547 %
1548 % ShearImage() creates a new image that is a shear_image copy of an existing
1549 % one. Shearing slides one edge of an image along the X or Y axis, creating
1550 % a parallelogram. An X direction shear slides an edge along the X axis,
1551 % while a Y direction shear slides an edge along the Y axis. The amount of
1552 % the shear is controlled by a shear angle. For X direction shears, x_shear
1553 % is measured relative to the Y axis, and similarly, for Y direction shears
1554 % y_shear is measured relative to the X axis. Empty triangles left over from
1555 % shearing the image are filled with the background color defined by member
1556 % 'background_color' of the image.. ShearImage() allocates the memory
1557 % necessary for the new Image structure and returns a pointer to the new image.
1558 %
1559 % ShearImage() is based on the paper "A Fast Algorithm for General Raster
1560 % Rotatation" by Alan W. Paeth.
1561 %
1562 % The format of the ShearImage method is:
1563 %
1564 % Image *ShearImage(const Image *image,const double x_shear,
1565 % const double y_shear,ExceptionInfo *exception)
1566 %
1567 % A description of each parameter follows.
1568 %
1569 % o image: the image.
1570 %
1571 % o x_shear, y_shear: Specifies the number of degrees to shear the image.
1572 %
1573 % o exception: return any errors or warnings in this structure.
1574 %
1575 */
1576 MagickExport Image *ShearImage(const Image *image,const double x_shear,
1577  const double y_shear,ExceptionInfo *exception)
1578 {
1579  Image
1580  *integral_image,
1581  *shear_image;
1582 
1584  status;
1585 
1586  PointInfo
1587  shear;
1588 
1590  border_info,
1591  bounds;
1592 
1593  assert(image != (Image *) NULL);
1594  assert(image->signature == MagickCoreSignature);
1595  if (image->debug != MagickFalse)
1596  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1597  assert(exception != (ExceptionInfo *) NULL);
1598  assert(exception->signature == MagickCoreSignature);
1599  if ((x_shear != 0.0) && (fmod(x_shear,90.0) == 0.0))
1600  ThrowImageException(ImageError,"AngleIsDiscontinuous");
1601  if ((y_shear != 0.0) && (fmod(y_shear,90.0) == 0.0))
1602  ThrowImageException(ImageError,"AngleIsDiscontinuous");
1603  /*
1604  Initialize shear angle.
1605  */
1606  integral_image=CloneImage(image,0,0,MagickTrue,exception);
1607  if (integral_image == (Image *) NULL)
1608  ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1609  shear.x=(-tan(DegreesToRadians(fmod(x_shear,360.0))));
1610  shear.y=tan(DegreesToRadians(fmod(y_shear,360.0)));
1611  if ((shear.x == 0.0) && (shear.y == 0.0))
1612  return(integral_image);
1613  if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
1614  {
1615  integral_image=DestroyImage(integral_image);
1616  return(integral_image);
1617  }
1618  if (integral_image->alpha_trait == UndefinedPixelTrait)
1619  (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
1620  /*
1621  Compute image size.
1622  */
1623  bounds.width=image->columns+(ssize_t) floor(fabs(shear.x)*image->rows+0.5);
1624  bounds.x=(ssize_t) ceil((double) image->columns+((fabs(shear.x)*image->rows)-
1625  image->columns)/2.0-0.5);
1626  bounds.y=(ssize_t) ceil((double) image->rows+((fabs(shear.y)*bounds.width)-
1627  image->rows)/2.0-0.5);
1628  /*
1629  Surround image with border.
1630  */
1631  integral_image->border_color=integral_image->background_color;
1632  integral_image->compose=CopyCompositeOp;
1633  border_info.width=(size_t) bounds.x;
1634  border_info.height=(size_t) bounds.y;
1635  shear_image=BorderImage(integral_image,&border_info,image->compose,exception);
1636  integral_image=DestroyImage(integral_image);
1637  if (shear_image == (Image *) NULL)
1638  ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1639  /*
1640  Shear the image.
1641  */
1642  if (shear_image->alpha_trait == UndefinedPixelTrait)
1643  (void) SetImageAlphaChannel(shear_image,OpaqueAlphaChannel,exception);
1644  status=XShearImage(shear_image,shear.x,image->columns,image->rows,bounds.x,
1645  (ssize_t) (shear_image->rows-image->rows)/2,exception);
1646  if (status == MagickFalse)
1647  {
1648  shear_image=DestroyImage(shear_image);
1649  return((Image *) NULL);
1650  }
1651  status=YShearImage(shear_image,shear.y,bounds.width,image->rows,(ssize_t)
1652  (shear_image->columns-bounds.width)/2,bounds.y,exception);
1653  if (status == MagickFalse)
1654  {
1655  shear_image=DestroyImage(shear_image);
1656  return((Image *) NULL);
1657  }
1658  status=CropToFitImage(&shear_image,shear.x,shear.y,(MagickRealType)
1659  image->columns,(MagickRealType) image->rows,MagickFalse,exception);
1660  shear_image->alpha_trait=image->alpha_trait;
1661  shear_image->compose=image->compose;
1662  shear_image->page.width=0;
1663  shear_image->page.height=0;
1664  if (status == MagickFalse)
1665  shear_image=DestroyImage(shear_image);
1666  return(shear_image);
1667 }
1668 
1669 /*
1670 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1671 % %
1672 % %
1673 % %
1674 % S h e a r R o t a t e I m a g e %
1675 % %
1676 % %
1677 % %
1678 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1679 %
1680 % ShearRotateImage() creates a new image that is a rotated copy of an existing
1681 % one. Positive angles rotate counter-clockwise (right-hand rule), while
1682 % negative angles rotate clockwise. Rotated images are usually larger than
1683 % the originals and have 'empty' triangular corners. X axis. Empty
1684 % triangles left over from shearing the image are filled with the background
1685 % color defined by member 'background_color' of the image. ShearRotateImage
1686 % allocates the memory necessary for the new Image structure and returns a
1687 % pointer to the new image.
1688 %
1689 % ShearRotateImage() is based on the paper "A Fast Algorithm for General
1690 % Raster Rotatation" by Alan W. Paeth. ShearRotateImage is adapted from a
1691 % similar method based on the Paeth paper written by Michael Halle of the
1692 % Spatial Imaging Group, MIT Media Lab.
1693 %
1694 % The format of the ShearRotateImage method is:
1695 %
1696 % Image *ShearRotateImage(const Image *image,const double degrees,
1697 % ExceptionInfo *exception)
1698 %
1699 % A description of each parameter follows.
1700 %
1701 % o image: the image.
1702 %
1703 % o degrees: Specifies the number of degrees to rotate the image.
1704 %
1705 % o exception: return any errors or warnings in this structure.
1706 %
1707 */
1708 MagickExport Image *ShearRotateImage(const Image *image,const double degrees,
1709  ExceptionInfo *exception)
1710 {
1711  Image
1712  *integral_image,
1713  *rotate_image;
1714 
1716  status;
1717 
1719  angle;
1720 
1721  PointInfo
1722  shear;
1723 
1725  border_info,
1726  bounds;
1727 
1728  size_t
1729  height,
1730  rotations,
1731  shear_width,
1732  width;
1733 
1734  /*
1735  Adjust rotation angle.
1736  */
1737  assert(image != (Image *) NULL);
1738  assert(image->signature == MagickCoreSignature);
1739  if (image->debug != MagickFalse)
1740  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1741  assert(exception != (ExceptionInfo *) NULL);
1742  assert(exception->signature == MagickCoreSignature);
1743  angle=fmod(degrees,360.0);
1744  if (angle < -45.0)
1745  angle+=360.0;
1746  for (rotations=0; angle > 45.0; rotations++)
1747  angle-=90.0;
1748  rotations%=4;
1749  /*
1750  Calculate shear equations.
1751  */
1752  integral_image=IntegralRotateImage(image,rotations,exception);
1753  if (integral_image == (Image *) NULL)
1754  ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1755  shear.x=(-tan((double) DegreesToRadians(angle)/2.0));
1756  shear.y=sin((double) DegreesToRadians(angle));
1757  if ((shear.x == 0.0) && (shear.y == 0.0))
1758  return(integral_image);
1759  if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
1760  {
1761  integral_image=DestroyImage(integral_image);
1762  return(integral_image);
1763  }
1764  if (integral_image->alpha_trait == UndefinedPixelTrait)
1765  (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
1766  /*
1767  Compute maximum bounds for 3 shear operations.
1768  */
1769  width=integral_image->columns;
1770  height=integral_image->rows;
1771  bounds.width=(size_t) floor(fabs((double) height*shear.x)+width+0.5);
1772  bounds.height=(size_t) floor(fabs((double) bounds.width*shear.y)+height+0.5);
1773  shear_width=(size_t) floor(fabs((double) bounds.height*shear.x)+
1774  bounds.width+0.5);
1775  bounds.x=(ssize_t) floor((double) ((shear_width > bounds.width) ? width :
1776  bounds.width-shear_width+2)/2.0+0.5);
1777  bounds.y=(ssize_t) floor(((double) bounds.height-height+2)/2.0+0.5);
1778  /*
1779  Surround image with a border.
1780  */
1781  integral_image->border_color=integral_image->background_color;
1782  integral_image->compose=CopyCompositeOp;
1783  border_info.width=(size_t) bounds.x;
1784  border_info.height=(size_t) bounds.y;
1785  rotate_image=BorderImage(integral_image,&border_info,image->compose,
1786  exception);
1787  integral_image=DestroyImage(integral_image);
1788  if (rotate_image == (Image *) NULL)
1789  ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1790  /*
1791  Rotate the image.
1792  */
1793  status=XShearImage(rotate_image,shear.x,width,height,bounds.x,(ssize_t)
1794  (rotate_image->rows-height)/2,exception);
1795  if (status == MagickFalse)
1796  {
1797  rotate_image=DestroyImage(rotate_image);
1798  return((Image *) NULL);
1799  }
1800  status=YShearImage(rotate_image,shear.y,bounds.width,height,(ssize_t)
1801  (rotate_image->columns-bounds.width)/2,bounds.y,exception);
1802  if (status == MagickFalse)
1803  {
1804  rotate_image=DestroyImage(rotate_image);
1805  return((Image *) NULL);
1806  }
1807  status=XShearImage(rotate_image,shear.x,bounds.width,bounds.height,(ssize_t)
1808  (rotate_image->columns-bounds.width)/2,(ssize_t) (rotate_image->rows-
1809  bounds.height)/2,exception);
1810  if (status == MagickFalse)
1811  {
1812  rotate_image=DestroyImage(rotate_image);
1813  return((Image *) NULL);
1814  }
1815  status=CropToFitImage(&rotate_image,shear.x,shear.y,(MagickRealType) width,
1816  (MagickRealType) height,MagickTrue,exception);
1817  rotate_image->alpha_trait=image->alpha_trait;
1818  rotate_image->compose=image->compose;
1819  rotate_image->page.width=0;
1820  rotate_image->page.height=0;
1821  if (status == MagickFalse)
1822  rotate_image=DestroyImage(rotate_image);
1823  return(rotate_image);
1824 }
size_t rows
Definition: image.h:172
#define magick_restrict
Definition: MagickCore.h:41
MagickDoubleType MagickRealType
Definition: magick-type.h:120
MagickExport CacheView * DestroyCacheView(CacheView *cache_view)
Definition: cache-view.c:252
double rx
Definition: geometry.h:95
MagickExport Image * DeskewImage(const Image *image, const double threshold, ExceptionInfo *exception)
Definition: shear.c:561
MagickProgressMonitor progress_monitor
Definition: image.h:303
static void GetImageBackgroundColor(Image *image, const ssize_t offset, ExceptionInfo *exception)
Definition: shear.c:500
ImageType type
Definition: image.h:264
MagickExport Image * ShearRotateImage(const Image *image, const double degrees, ExceptionInfo *exception)
Definition: shear.c:1708
static MagickBooleanType RadonTransform(const Image *image, const double threshold, size_t *projection, ExceptionInfo *exception)
Definition: shear.c:325
static Quantum GetPixelAlpha(const Image *magick_restrict image, const Quantum *magick_restrict pixel)
static PixelTrait GetPixelAlphaTraits(const Image *magick_restrict image)
static Quantum GetPixelRed(const Image *magick_restrict image, const Quantum *magick_restrict pixel)
PixelInfo border_color
Definition: image.h:179
MagickExport MagickBooleanType NullMatrix(MatrixInfo *matrix_info)
Definition: matrix.c:1003
double ty
Definition: geometry.h:95
size_t signature
Definition: exception.h:123
MagickExport MagickStatusType ParseAbsoluteGeometry(const char *geometry, RectangleInfo *region_info)
Definition: geometry.c:703
static void RadonProjection(const Image *image, MatrixInfo *source_matrixs, MatrixInfo *destination_matrixs, const ssize_t sign, size_t *projection)
Definition: shear.c:216
MagickPrivate void GetPixelCacheTileSize(const Image *, size_t *, size_t *)
MagickExport MagickBooleanType SetImageArtifact(Image *image, const char *artifact, const char *value)
Definition: artifact.c:445
MagickExport const char * GetImageArtifact(const Image *image, const char *artifact)
Definition: artifact.c:273
MagickRealType red
Definition: pixel.h:190
#define XShearImageTag
static PixelTrait GetPixelChannelTraits(const Image *magick_restrict image, const PixelChannel channel)
static MagickBooleanType 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)
Definition: shear.c:1137
MagickExport ssize_t FormatLocaleString(char *magick_restrict string, const size_t length, const char *magick_restrict format,...)
Definition: locale.c:499
static void SetPixelViaPixelInfo(const Image *magick_restrict image, const PixelInfo *magick_restrict pixel_info, Quantum *magick_restrict pixel)
MagickExport const Quantum * GetCacheViewVirtualPixels(const CacheView *cache_view, const ssize_t x, const ssize_t y, const size_t columns, const size_t rows, ExceptionInfo *exception)
Definition: cache-view.c:651
static long StringToLong(const char *magick_restrict value)
MagickRealType alpha
Definition: pixel.h:190
MagickExport size_t GetMatrixColumns(const MatrixInfo *matrix_info)
Definition: matrix.c:609
MagickExport MagickBooleanType GetMatrixElement(const MatrixInfo *matrix_info, const ssize_t x, const ssize_t y, void *value)
Definition: matrix.c:705
size_t width
Definition: geometry.h:130
Definition: log.h:52
ssize_t MagickOffsetType
Definition: magick-type.h:129
static Quantum ClampToQuantum(const MagickRealType quantum)
Definition: quantum.h:85
MagickExport void GetPixelInfo(const Image *image, PixelInfo *pixel)
Definition: pixel.c:2170
Definition: image.h:151
double tx
Definition: geometry.h:95
MagickExport MagickBooleanType SetMatrixElement(const MatrixInfo *matrix_info, const ssize_t x, const ssize_t y, const void *value)
Definition: matrix.c:1110
MagickExport Image * AffineTransformImage(const Image *image, const AffineMatrix *affine_matrix, ExceptionInfo *exception)
Definition: distort.c:284
MagickExport Image * CropImage(const Image *image, const RectangleInfo *geometry, ExceptionInfo *exception)
Definition: transform.c:535
double x
Definition: geometry.h:123
#define MagickCoreSignature
MagickExport Quantum * GetCacheViewAuthenticPixels(CacheView *cache_view, const ssize_t x, const ssize_t y, const size_t columns, const size_t rows, ExceptionInfo *exception)
Definition: cache-view.c:299
MagickExport MagickBooleanType SetImageAlphaChannel(Image *image, const AlphaChannelOption alpha_type, ExceptionInfo *exception)
Definition: channel.c:974
MagickBooleanType
Definition: magick-type.h:158
MagickExport Image * IntegralRotateImage(const Image *image, size_t rotations, ExceptionInfo *exception)
Definition: shear.c:706
#define YShearImageTag
MagickExport void * AcquireQuantumMemory(const size_t count, const size_t quantum)
Definition: memory.c:553
static double DegreesToRadians(const double degrees)
Definition: image-private.h:56
double y
Definition: geometry.h:123
RectangleInfo page
Definition: image.h:212
#define MagickPathExtent
double ry
Definition: geometry.h:95
static Quantum GetPixelGreen(const Image *magick_restrict image, const Quantum *magick_restrict pixel)
MagickExport MagickBooleanType IsStringTrue(const char *value)
Definition: string.c:1425
static void CompositePixelInfoAreaBlend(const PixelInfo *p, const double alpha, const PixelInfo *q, const double beta, const double area, PixelInfo *composite)
static void GetPixelInfoPixel(const Image *magick_restrict image, const Quantum *magick_restrict pixel, PixelInfo *magick_restrict pixel_info)
PixelTrait alpha_trait
Definition: image.h:280
static MagickBooleanType 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)
Definition: shear.c:1354
MagickRealType blue
Definition: pixel.h:190
MagickExport Quantum * QueueCacheViewAuthenticPixels(CacheView *cache_view, const ssize_t x, const ssize_t y, const size_t columns, const size_t rows, ExceptionInfo *exception)
Definition: cache-view.c:977
MagickExport VirtualPixelMethod SetImageVirtualPixelMethod(Image *image, const VirtualPixelMethod virtual_pixel_method, ExceptionInfo *exception)
Definition: image.c:3497
double sx
Definition: geometry.h:95
MagickExport MatrixInfo * AcquireMatrixInfo(const size_t columns, const size_t rows, const size_t stride, ExceptionInfo *exception)
Definition: matrix.c:200
MagickExport MagickBooleanType LogMagickEvent(const LogEventType type, const char *module, const char *function, const size_t line, const char *format,...)
Definition: log.c:1413
size_t signature
Definition: image.h:354
#define QuantumScale
Definition: magick-type.h:115
size_t columns
Definition: image.h:172
ssize_t x
Definition: geometry.h:134
size_t height
Definition: geometry.h:130
MagickExport MagickBooleanType SetImageStorageClass(Image *image, const ClassType storage_class, ExceptionInfo *exception)
Definition: image.c:2615
PixelChannel
Definition: pixel.h:67
static size_t GetPixelChannels(const Image *magick_restrict image)
char filename[MagickPathExtent]
Definition: image.h:319
#define GetMagickModule()
Definition: log.h:28
double sy
Definition: geometry.h:95
#define ThrowImageException(severity, tag)
static PixelChannel GetPixelChannelChannel(const Image *magick_restrict image, const ssize_t offset)
MagickExport CacheView * AcquireVirtualCacheView(const Image *image, ExceptionInfo *exception)
Definition: cache-view.c:149
MagickExport Image * ShearImage(const Image *image, const double x_shear, const double y_shear, ExceptionInfo *exception)
Definition: shear.c:1576
static double RadiansToDegrees(const double radians)
Definition: image-private.h:61
unsigned short Quantum
Definition: magick-type.h:82
MagickExport RectangleInfo GetImageBoundingBox(const Image *image, ExceptionInfo *exception)
Definition: attribute.c:386
MagickExport Image * BorderImage(const Image *image, const RectangleInfo *border_info, const CompositeOperator compose, ExceptionInfo *exception)
Definition: decorate.c:103
#define RotateImageTag
static void SetPixelChannel(const Image *magick_restrict image, const PixelChannel channel, const Quantum quantum, Quantum *magick_restrict pixel)
MagickExport void * RelinquishMagickMemory(void *memory)
Definition: memory.c:1084
MagickRealType green
Definition: pixel.h:190
MagickBooleanType(* MagickProgressMonitor)(const char *, const MagickOffsetType, const MagickSizeType, void *)
Definition: monitor.h:26
#define Swap(x, y)
Definition: studio.h:345
static MagickBooleanType CropToFitImage(Image **image, const double x_shear, const double y_shear, const double width, const double height, const MagickBooleanType rotate, ExceptionInfo *exception)
Definition: shear.c:113
CompositeOperator compose
Definition: image.h:234
#define MagickExport
MagickExport MagickBooleanType SyncCacheViewAuthenticPixels(CacheView *magick_restrict cache_view, ExceptionInfo *exception)
Definition: cache-view.c:1100
ssize_t y
Definition: geometry.h:134
MagickExport CacheView * AcquireAuthenticCacheView(const Image *image, ExceptionInfo *exception)
Definition: cache-view.c:112
static Quantum GetPixelBlue(const Image *magick_restrict image, const Quantum *magick_restrict pixel)
PixelTrait
Definition: pixel.h:134
PixelInfo background_color
Definition: image.h:179
MagickExport Image * DestroyImage(Image *image)
Definition: image.c:1181
MagickExport Image * CloneImage(const Image *image, const size_t columns, const size_t rows, const MagickBooleanType detach, ExceptionInfo *exception)
Definition: image.c:796
MagickExport Image * StatisticImage(const Image *image, const StatisticType type, const size_t width, const size_t height, ExceptionInfo *exception)
Definition: statistic.c:2863
#define QuantumRange
Definition: magick-type.h:83
MagickExport MagickBooleanType SetImageProgress(const Image *image, const char *tag, const MagickOffsetType offset, const MagickSizeType extent)
Definition: monitor.c:136
MagickBooleanType debug
Definition: image.h:334
MagickExport size_t GetMatrixRows(const MatrixInfo *matrix_info)
Definition: matrix.c:751
MagickExport MatrixInfo * DestroyMatrixInfo(MatrixInfo *matrix_info)
Definition: matrix.c:369