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