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