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