blob: 23355c7c61772fade131b8847bdeeddf28f75137 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
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% %
cristy16af1cb2009-12-11 21:38:29 +000020% Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% 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
45/*
46 Include declarations.
47*/
48#include "magick/studio.h"
49#include "magick/artifact.h"
cristy5a2ca482009-10-14 18:24:56 +000050#include "magick/attribute.h"
cristy3ed852e2009-09-05 21:47:34 +000051#include "magick/blob-private.h"
52#include "magick/cache-private.h"
53#include "magick/color-private.h"
54#include "magick/colorspace-private.h"
55#include "magick/composite.h"
56#include "magick/composite-private.h"
57#include "magick/decorate.h"
58#include "magick/distort.h"
59#include "magick/draw.h"
60#include "magick/exception.h"
61#include "magick/exception-private.h"
62#include "magick/gem.h"
63#include "magick/geometry.h"
64#include "magick/image.h"
65#include "magick/image-private.h"
66#include "magick/memory_.h"
67#include "magick/list.h"
68#include "magick/monitor.h"
69#include "magick/monitor-private.h"
70#include "magick/pixel-private.h"
71#include "magick/quantum.h"
72#include "magick/resource_.h"
73#include "magick/shear.h"
74#include "magick/statistic.h"
cristyf2f27272009-12-17 14:48:46 +000075#include "magick/string_.h"
76#include "magick/string-private.h"
cristy3ed852e2009-09-05 21:47:34 +000077#include "magick/threshold.h"
78#include "magick/transform.h"
79
80/*
81%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82% %
83% %
84% %
85% A f f i n e T r a n s f o r m I m a g e %
86% %
87% %
88% %
89%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
90%
91% AffineTransformImage() transforms an image as dictated by the affine matrix.
92% It allocates the memory necessary for the new Image structure and returns
93% a pointer to the new image.
94%
95% The format of the AffineTransformImage method is:
96%
97% Image *AffineTransformImage(const Image *image,
98% AffineMatrix *affine_matrix,ExceptionInfo *exception)
99%
100% A description of each parameter follows:
101%
102% o image: the image.
103%
104% o affine_matrix: the affine matrix.
105%
106% o exception: return any errors or warnings in this structure.
107%
108*/
109MagickExport Image *AffineTransformImage(const Image *image,
110 const AffineMatrix *affine_matrix,ExceptionInfo *exception)
111{
112 double
113 distort[6];
114
115 Image
116 *deskew_image;
117
118 /*
119 Affine transform image.
120 */
121 assert(image->signature == MagickSignature);
122 if (image->debug != MagickFalse)
123 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
124 assert(affine_matrix != (AffineMatrix *) NULL);
125 assert(exception != (ExceptionInfo *) NULL);
126 assert(exception->signature == MagickSignature);
127 distort[0]=affine_matrix->sx;
128 distort[1]=affine_matrix->rx;
129 distort[2]=affine_matrix->ry;
130 distort[3]=affine_matrix->sy;
131 distort[4]=affine_matrix->tx;
132 distort[5]=affine_matrix->ty;
133 deskew_image=DistortImage(image,AffineProjectionDistortion,6,distort,
134 MagickTrue,exception);
135 return(deskew_image);
136}
137
138/*
139%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
140% %
141% %
142% %
143+ C r o p T o F i t I m a g e %
144% %
145% %
146% %
147%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
148%
149% CropToFitImage() crops the sheared image as determined by the bounding box
150% as defined by width and height and shearing angles.
151%
152% The format of the CropToFitImage method is:
153%
154% Image *CropToFitImage(Image **image,const MagickRealType x_shear,
155% const MagickRealType x_shear,const MagickRealType width,
156% const MagickRealType height,const MagickBooleanType rotate,
157% ExceptionInfo *exception)
158%
159% A description of each parameter follows.
160%
161% o image: the image.
162%
163% o x_shear, y_shear, width, height: Defines a region of the image to crop.
164%
165% o exception: return any errors or warnings in this structure.
166%
167*/
168static inline void CropToFitImage(Image **image,const MagickRealType x_shear,
169 const MagickRealType y_shear,const MagickRealType width,
170 const MagickRealType height,const MagickBooleanType rotate,
171 ExceptionInfo *exception)
172{
173 Image
174 *crop_image;
175
176 PointInfo
177 extent[4],
178 min,
179 max;
180
181 RectangleInfo
182 geometry,
183 page;
184
185 register long
186 i;
187
188 /*
189 Calculate the rotated image size.
190 */
191 extent[0].x=(double) (-width/2.0);
192 extent[0].y=(double) (-height/2.0);
193 extent[1].x=(double) width/2.0;
194 extent[1].y=(double) (-height/2.0);
195 extent[2].x=(double) (-width/2.0);
196 extent[2].y=(double) height/2.0;
197 extent[3].x=(double) width/2.0;
198 extent[3].y=(double) height/2.0;
199 for (i=0; i < 4; i++)
200 {
201 extent[i].x+=x_shear*extent[i].y;
202 extent[i].y+=y_shear*extent[i].x;
203 if (rotate != MagickFalse)
204 extent[i].x+=x_shear*extent[i].y;
205 extent[i].x+=(double) (*image)->columns/2.0;
206 extent[i].y+=(double) (*image)->rows/2.0;
207 }
208 min=extent[0];
209 max=extent[0];
210 for (i=1; i < 4; i++)
211 {
212 if (min.x > extent[i].x)
213 min.x=extent[i].x;
214 if (min.y > extent[i].y)
215 min.y=extent[i].y;
216 if (max.x < extent[i].x)
217 max.x=extent[i].x;
218 if (max.y < extent[i].y)
219 max.y=extent[i].y;
220 }
221 geometry.x=(long) (min.x+0.5);
222 geometry.y=(long) (min.y+0.5);
223 geometry.width=(unsigned long) ((long) (max.x+0.5)-(long) (min.x+0.5));
224 geometry.height=(unsigned long) ((long) (max.y+0.5)-(long) (min.y+0.5));
225 page=(*image)->page;
226 (void) ParseAbsoluteGeometry("0x0+0+0",&(*image)->page);
227 crop_image=CropImage(*image,&geometry,exception);
228 (*image)->page=page;
229 if (crop_image != (Image *) NULL)
230 {
231 crop_image->page=page;
232 *image=DestroyImage(*image);
233 *image=crop_image;
234 }
235}
236
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
268typedef struct _RadonInfo
269{
270 CacheType
271 type;
272
273 unsigned long
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
293static 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
329static MagickBooleanType ResetRadonCells(RadonInfo *radon_info)
330{
331 long
332 y;
333
334 register long
335 x;
336
337 ssize_t
338 count;
339
340 unsigned short
341 value;
342
343 if (radon_info->type != DiskCache)
344 {
345 (void) ResetMagickMemory(radon_info->cells,0,(size_t) radon_info->length);
346 return(MagickTrue);
347 }
348 value=0;
349 (void) MagickSeek(radon_info->file,0,SEEK_SET);
350 for (y=0; y < (long) radon_info->height; y++)
351 {
352 for (x=0; x < (long) radon_info->width; x++)
353 {
354 count=write(radon_info->file,&value,sizeof(*radon_info->cells));
355 if (count != (ssize_t) sizeof(*radon_info->cells))
356 break;
357 }
358 if (x < (long) radon_info->width)
359 break;
360 }
361 return(y < (long) radon_info->height ? MagickFalse : MagickTrue);
362}
363
364static RadonInfo *AcquireRadonInfo(const Image *image,const unsigned long width,
365 const unsigned long height,ExceptionInfo *exception)
366{
367 MagickBooleanType
368 status;
369
370 RadonInfo
371 *radon_info;
372
cristy90823212009-12-12 20:48:33 +0000373 radon_info=(RadonInfo *) AcquireAlignedMemory(1,sizeof(*radon_info));
cristy3ed852e2009-09-05 21:47:34 +0000374 if (radon_info == (RadonInfo *) NULL)
375 return((RadonInfo *) NULL);
376 (void) ResetMagickMemory(radon_info,0,sizeof(*radon_info));
377 radon_info->width=width;
378 radon_info->height=height;
379 radon_info->length=(MagickSizeType) width*height*sizeof(*radon_info->cells);
380 radon_info->type=MemoryCache;
381 status=AcquireMagickResource(AreaResource,radon_info->length);
382 if ((status != MagickFalse) &&
383 (radon_info->length == (MagickSizeType) ((size_t) radon_info->length)))
384 {
385 status=AcquireMagickResource(MemoryResource,radon_info->length);
386 if (status != MagickFalse)
387 {
388 radon_info->mapped=MagickFalse;
389 radon_info->cells=(unsigned short *) AcquireMagickMemory((size_t)
390 radon_info->length);
391 if (radon_info->cells == (unsigned short *) NULL)
392 {
393 radon_info->mapped=MagickTrue;
394 radon_info->cells=(unsigned short *) MapBlob(-1,IOMode,0,(size_t)
395 radon_info->length);
396 }
397 if (radon_info->cells == (unsigned short *) NULL)
398 RelinquishMagickResource(MemoryResource,radon_info->length);
399 }
400 }
401 radon_info->file=(-1);
402 if (radon_info->cells == (unsigned short *) NULL)
403 {
404 status=AcquireMagickResource(DiskResource,radon_info->length);
405 if (status == MagickFalse)
406 {
407 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
408 "CacheResourcesExhausted","`%s'",image->filename);
409 return(DestroyRadonInfo(radon_info));
410 }
411 radon_info->type=DiskCache;
412 (void) AcquireMagickResource(MemoryResource,radon_info->length);
413 radon_info->file=AcquireUniqueFileResource(radon_info->path);
414 if (radon_info->file == -1)
415 return(DestroyRadonInfo(radon_info));
416 status=AcquireMagickResource(MapResource,radon_info->length);
417 if (status != MagickFalse)
418 {
419 status=ResetRadonCells(radon_info);
420 if (status != MagickFalse)
421 {
422 radon_info->cells=(unsigned short *) MapBlob(radon_info->file,
423 IOMode,0,(size_t) radon_info->length);
424 if (radon_info->cells != (unsigned short *) NULL)
425 radon_info->type=MapCache;
426 else
427 RelinquishMagickResource(MapResource,radon_info->length);
428 }
429 }
430 }
431 return(radon_info);
432}
433
434static inline size_t MagickMin(const size_t x,const size_t y)
435{
436 if (x < y)
437 return(x);
438 return(y);
439}
440
441static inline ssize_t ReadRadonCell(const RadonInfo *radon_info,
442 const off_t offset,const size_t length,unsigned char *buffer)
443{
444 register ssize_t
445 i;
446
447 ssize_t
448 count;
449
450#if !defined(MAGICKCORE_HAVE_PPREAD)
cristyb5d5f722009-11-04 03:03:49 +0000451#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000452 #pragma omp critical (MagickCore_ReadRadonCell)
453#endif
454 {
455 i=(-1);
456 if (MagickSeek(radon_info->file,offset,SEEK_SET) >= 0)
457 {
458#endif
459 count=0;
460 for (i=0; i < (ssize_t) length; i+=count)
461 {
462#if !defined(MAGICKCORE_HAVE_PPREAD)
463 count=read(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
464 SSIZE_MAX));
465#else
466 count=pread(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
467 SSIZE_MAX),(off_t) (offset+i));
468#endif
469 if (count > 0)
470 continue;
471 count=0;
472 if (errno != EINTR)
473 {
474 i=(-1);
475 break;
476 }
477 }
478#if !defined(MAGICKCORE_HAVE_PPREAD)
479 }
480 }
481#endif
482 return(i);
483}
484
485static inline ssize_t WriteRadonCell(const RadonInfo *radon_info,
486 const off_t offset,const size_t length,const unsigned char *buffer)
487{
488 register ssize_t
489 i;
490
491 ssize_t
492 count;
493
494#if !defined(MAGICKCORE_HAVE_PWRITE)
cristyb5d5f722009-11-04 03:03:49 +0000495#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000496 #pragma omp critical (MagickCore_WriteRadonCell)
497#endif
498 {
499 if (MagickSeek(radon_info->file,offset,SEEK_SET) >= 0)
500 {
501#endif
502 count=0;
503 for (i=0; i < (ssize_t) length; i+=count)
504 {
505#if !defined(MAGICKCORE_HAVE_PWRITE)
506 count=write(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
507 SSIZE_MAX));
508#else
509 count=pwrite(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
510 SSIZE_MAX),(off_t) (offset+i));
511#endif
512 if (count > 0)
513 continue;
514 count=0;
515 if (errno != EINTR)
516 {
517 i=(-1);
518 break;
519 }
520 }
521#if !defined(MAGICKCORE_HAVE_PWRITE)
522 }
523 }
524#endif
525 return(i);
526}
527
528static inline unsigned short GetRadonCell(const RadonInfo *radon_info,
529 const long x,const long y)
530{
531 off_t
532 i;
533
534 unsigned short
535 value;
536
537 i=(off_t) radon_info->height*x+y;
538 if ((i < 0) ||
539 ((MagickSizeType) (i*sizeof(*radon_info->cells)) >= radon_info->length))
540 return(0);
541 if (radon_info->type != DiskCache)
542 return(radon_info->cells[i]);
543 value=0;
544 (void) ReadRadonCell(radon_info,i*sizeof(*radon_info->cells),
545 sizeof(*radon_info->cells),(unsigned char *) &value);
546 return(value);
547}
548
549static inline MagickBooleanType SetRadonCell(const RadonInfo *radon_info,
550 const long x,const long y,const unsigned short value)
551{
552 off_t
553 i;
554
555 ssize_t
556 count;
557
558 i=(off_t) radon_info->height*x+y;
559 if ((i < 0) ||
560 ((MagickSizeType) (i*sizeof(*radon_info->cells)) >= radon_info->length))
561 return(MagickFalse);
562 if (radon_info->type != DiskCache)
563 {
564 radon_info->cells[i]=value;
565 return(MagickTrue);
566 }
567 count=WriteRadonCell(radon_info,i*sizeof(*radon_info->cells),
cristy44807bb2009-09-19 18:28:06 +0000568 sizeof(*radon_info->cells),(const unsigned char *) &value);
cristy3ed852e2009-09-05 21:47:34 +0000569 if (count != (ssize_t) sizeof(*radon_info->cells))
570 return(MagickFalse);
571 return(MagickTrue);
572}
573
574static void RadonProjection(RadonInfo *source_cells,
575 RadonInfo *destination_cells,const long sign,unsigned long *projection)
576{
577 RadonInfo
578 *swap;
579
580 register long
581 x;
582
583 register RadonInfo
584 *p,
585 *q;
586
587 unsigned long
588 step;
589
590 p=source_cells;
591 q=destination_cells;
592 for (step=1; step < p->width; step*=2)
593 {
594 for (x=0; x < (long) p->width; x+=2*step)
595 {
596 long
597 y;
598
599 register long
600 i;
601
602 unsigned short
603 cell;
604
605 for (i=0; i < (long) step; i++)
606 {
607 for (y=0; y < (long) (p->height-i-1); y++)
608 {
609 cell=GetRadonCell(p,x+i,y);
610 (void) SetRadonCell(q,x+2*i,y,cell+GetRadonCell(p,x+i+step,y+i));
611 (void) SetRadonCell(q,x+2*i+1,y,cell+GetRadonCell(p,x+i+step,y+i+1));
612 }
613 for ( ; y < (long) (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+step,y+i));
617 (void) SetRadonCell(q,x+2*i+1,y,cell);
618 }
619 for ( ; y < (long) p->height; y++)
620 {
621 cell=GetRadonCell(p,x+i,y);
622 (void) SetRadonCell(q,x+2*i,y,cell);
623 (void) SetRadonCell(q,x+2*i+1,y,cell);
624 }
625 }
626 }
627 swap=p;
628 p=q;
629 q=swap;
630 }
cristyb5d5f722009-11-04 03:03:49 +0000631#if defined(MAGICKCORE_OPENMP_SUPPORT)
632 #pragma omp parallel for schedule(dynamic,4)
cristy3ed852e2009-09-05 21:47:34 +0000633#endif
634 for (x=0; x < (long) p->width; x++)
635 {
636 register long
637 y;
638
639 unsigned long
640 sum;
641
642 sum=0;
643 for (y=0; y < (long) (p->height-1); y++)
644 {
645 long
646 delta;
647
648 delta=GetRadonCell(p,x,y)-(long) GetRadonCell(p,x,y+1);
649 sum+=delta*delta;
650 }
651 projection[p->width+sign*x-1]=sum;
652 }
653}
654
655static MagickBooleanType RadonTransform(const Image *image,
656 const double threshold,unsigned long *projection,ExceptionInfo *exception)
657{
658 CacheView
659 *image_view;
660
661 long
662 y;
663
664 MagickBooleanType
665 status;
666
667 RadonInfo
668 *destination_cells,
669 *source_cells;
670
671 register long
672 i;
673
674 unsigned char
675 byte;
676
677 unsigned long
678 count,
679 width;
680
681 unsigned short
682 bits[256];
683
684 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
685 source_cells=AcquireRadonInfo(image,width,image->rows,exception);
686 destination_cells=AcquireRadonInfo(image,width,image->rows,exception);
687 if ((source_cells == (RadonInfo *) NULL) ||
688 (destination_cells == (RadonInfo *) NULL))
689 {
690 if (destination_cells != (RadonInfo *) NULL)
691 destination_cells=DestroyRadonInfo(destination_cells);
692 if (source_cells != (RadonInfo *) NULL)
693 source_cells=DestroyRadonInfo(source_cells);
694 return(MagickFalse);
695 }
696 if (ResetRadonCells(source_cells) == MagickFalse)
697 {
698 destination_cells=DestroyRadonInfo(destination_cells);
699 source_cells=DestroyRadonInfo(source_cells);
700 return(MagickFalse);
701 }
702 for (i=0; i < 256; i++)
703 {
704 byte=(unsigned char) i;
705 for (count=0; byte != 0; byte>>=1)
706 count+=byte & 0x01;
707 bits[i]=(unsigned short) count;
708 }
709 status=MagickTrue;
710 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +0000711#if defined(MAGICKCORE_OPENMP_SUPPORT)
712 #pragma omp parallel for schedule(dynamic,4) shared(status)
cristy3ed852e2009-09-05 21:47:34 +0000713#endif
714 for (y=0; y < (long) image->rows; y++)
715 {
716 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +0000717 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000718
719 register long
720 i,
721 x;
722
723 unsigned long
724 bit,
725 byte;
726
727 if (status == MagickFalse)
728 continue;
729 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
730 if (p == (const PixelPacket *) NULL)
731 {
732 status=MagickFalse;
733 continue;
734 }
735 bit=0;
736 byte=0;
737 i=(long) (image->columns+7)/8;
738 for (x=0; x < (long) image->columns; x++)
739 {
740 byte<<=1;
741 if (((MagickRealType) p->red < threshold) ||
742 ((MagickRealType) p->green < threshold) ||
743 ((MagickRealType) p->blue < threshold))
744 byte|=0x01;
745 bit++;
746 if (bit == 8)
747 {
748 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
749 bit=0;
750 byte=0;
751 }
752 p++;
753 }
754 if (bit != 0)
755 {
756 byte<<=(8-bit);
757 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
758 }
759 }
760 RadonProjection(source_cells,destination_cells,-1,projection);
761 (void) ResetRadonCells(source_cells);
cristyb5d5f722009-11-04 03:03:49 +0000762#if defined(MAGICKCORE_OPENMP_SUPPORT)
763 #pragma omp parallel for schedule(dynamic,4) shared(status)
cristy3ed852e2009-09-05 21:47:34 +0000764#endif
765 for (y=0; y < (long) image->rows; y++)
766 {
767 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +0000768 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000769
770 register long
771 i,
772 x;
773
774 unsigned long
775 bit,
776 byte;
777
778 if (status == MagickFalse)
779 continue;
780 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
781 if (p == (const PixelPacket *) NULL)
782 {
783 status=MagickFalse;
784 continue;
785 }
786 bit=0;
787 byte=0;
788 i=0;
789 for (x=0; x < (long) image->columns; x++)
790 {
791 byte<<=1;
792 if (((MagickRealType) p->red < threshold) ||
793 ((MagickRealType) p->green < threshold) ||
794 ((MagickRealType) p->blue < threshold))
795 byte|=0x01;
796 bit++;
797 if (bit == 8)
798 {
799 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
800 bit=0;
801 byte=0;
802 }
803 p++;
804 }
805 if (bit != 0)
806 {
807 byte<<=(8-bit);
808 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
809 }
810 }
811 RadonProjection(source_cells,destination_cells,1,projection);
812 image_view=DestroyCacheView(image_view);
813 destination_cells=DestroyRadonInfo(destination_cells);
814 source_cells=DestroyRadonInfo(source_cells);
815 return(MagickTrue);
816}
817
818static void GetImageBackgroundColor(Image *image,const long offset,
819 ExceptionInfo *exception)
820{
821 CacheView
822 *image_view;
823
824 long
825 y;
826
827 MagickPixelPacket
828 background;
829
830 MagickRealType
831 count;
832
833 /*
834 Compute average background color.
835 */
836 if (offset <= 0)
837 return;
838 GetMagickPixelPacket(image,&background);
839 count=0.0;
840 image_view=AcquireCacheView(image);
841 for (y=0; y < (long) image->rows; y++)
842 {
843 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +0000844 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000845
846 register long
847 x;
848
849 if ((y >= offset) && (y < ((long) image->rows-offset)))
850 continue;
851 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
852 if (p == (const PixelPacket *) NULL)
853 continue;
854 for (x=0; x < (long) image->columns; x++)
855 {
856 if ((x >= offset) && (x < ((long) image->columns-offset)))
857 continue;
858 background.red+=QuantumScale*p->red;
859 background.green+=QuantumScale*p->green;
860 background.blue+=QuantumScale*p->blue;
861 background.opacity+=QuantumScale*p->opacity;
862 count++;
863 p++;
864 }
865 }
866 image_view=DestroyCacheView(image_view);
867 image->background_color.red=RoundToQuantum((MagickRealType) QuantumRange*
868 background.red/count);
869 image->background_color.green=RoundToQuantum((MagickRealType) QuantumRange*
870 background.green/count);
871 image->background_color.blue=RoundToQuantum((MagickRealType) QuantumRange*
872 background.blue/count);
873 image->background_color.opacity=RoundToQuantum((MagickRealType) QuantumRange*
874 background.opacity/count);
875}
876
877MagickExport Image *DeskewImage(const Image *image,const double threshold,
878 ExceptionInfo *exception)
879{
880 AffineMatrix
881 affine_matrix;
882
883 const char
884 *artifact;
885
886 double
887 degrees;
888
889 Image
890 *clone_image,
891 *crop_image,
892 *deskew_image,
893 *median_image;
894
895 long
896 skew;
897
898 MagickBooleanType
899 status;
900
901 RectangleInfo
902 geometry;
903
904 register long
905 i;
906
907 unsigned long
908 max_projection,
909 *projection,
910 width;
911
912 /*
913 Compute deskew angle.
914 */
915 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
916 projection=(unsigned long *) AcquireQuantumMemory((size_t) (2*width-1),
917 sizeof(*projection));
918 if (projection == (unsigned long *) NULL)
919 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
920 status=RadonTransform(image,threshold,projection,exception);
921 if (status == MagickFalse)
922 {
923 projection=(unsigned long *) RelinquishMagickMemory(projection);
924 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
925 }
926 max_projection=0;
927 skew=0;
928 for (i=0; i < (long) (2*width-1); i++)
929 {
930 if (projection[i] > max_projection)
931 {
932 skew=i-(long) width+1;
933 max_projection=projection[i];
934 }
935 }
936 projection=(unsigned long *) RelinquishMagickMemory(projection);
937 /*
938 Deskew image.
939 */
940 clone_image=CloneImage(image,0,0,MagickTrue,exception);
941 if (clone_image == (Image *) NULL)
942 return((Image *) NULL);
943 (void) SetImageVirtualPixelMethod(clone_image,BackgroundVirtualPixelMethod);
944 degrees=RadiansToDegrees(-atan((double) skew/width/8));
945 if (image->debug != MagickFalse)
946 (void) LogMagickEvent(TransformEvent,GetMagickModule()," Deskew angle: %g",
947 degrees);
948 affine_matrix.sx=cos(DegreesToRadians(fmod((double) degrees,360.0)));
949 affine_matrix.rx=sin(DegreesToRadians(fmod((double) degrees,360.0)));
950 affine_matrix.ry=(-sin(DegreesToRadians(fmod((double) degrees,360.0))));
951 affine_matrix.sy=cos(DegreesToRadians(fmod((double) degrees,360.0)));
952 affine_matrix.tx=0.0;
953 affine_matrix.ty=0.0;
954 artifact=GetImageArtifact(image,"deskew:auto-crop");
955 if (artifact == (const char *) NULL)
956 {
957 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
958 clone_image=DestroyImage(clone_image);
959 return(deskew_image);
960 }
961 /*
962 Auto-crop image.
963 */
cristyf2f27272009-12-17 14:48:46 +0000964 GetImageBackgroundColor(clone_image,StringToLong(artifact),exception);
cristy3ed852e2009-09-05 21:47:34 +0000965 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
966 clone_image=DestroyImage(clone_image);
967 if (deskew_image == (Image *) NULL)
968 return((Image *) NULL);
969 median_image=MedianFilterImage(deskew_image,0.0,exception);
970 if (median_image == (Image *) NULL)
971 {
972 deskew_image=DestroyImage(deskew_image);
973 return((Image *) NULL);
974 }
975 geometry=GetImageBoundingBox(median_image,exception);
976 median_image=DestroyImage(median_image);
977 if (image->debug != MagickFalse)
978 (void) LogMagickEvent(TransformEvent,GetMagickModule()," Deskew geometry: "
979 "%lux%lu%+ld%+ld",geometry.width,geometry.height,geometry.x,geometry.y);
980 crop_image=CropImage(deskew_image,&geometry,exception);
981 deskew_image=DestroyImage(deskew_image);
982 return(crop_image);
983}
984
985/*
986%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
987% %
988% %
989% %
990+ I n t e g r a l R o t a t e I m a g e %
991% %
992% %
993% %
994%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
995%
996% IntegralRotateImage() rotates the image an integral of 90 degrees. It
997% allocates the memory necessary for the new Image structure and returns a
998% pointer to the rotated image.
999%
1000% The format of the IntegralRotateImage method is:
1001%
1002% Image *IntegralRotateImage(const Image *image,unsigned long rotations,
1003% ExceptionInfo *exception)
1004%
1005% A description of each parameter follows.
1006%
1007% o image: the image.
1008%
1009% o rotations: Specifies the number of 90 degree rotations.
1010%
1011*/
1012static Image *IntegralRotateImage(const Image *image,unsigned long rotations,
1013 ExceptionInfo *exception)
1014{
cristy3ed852e2009-09-05 21:47:34 +00001015#define RotateImageTag "Rotate/Image"
1016
1017 CacheView
1018 *image_view,
1019 *rotate_view;
1020
1021 Image
1022 *rotate_image;
1023
1024 long
1025 progress,
1026 y;
1027
1028 MagickBooleanType
1029 status;
1030
1031 RectangleInfo
1032 page;
1033
1034 /*
1035 Initialize rotated image attributes.
1036 */
1037 assert(image != (Image *) NULL);
1038 page=image->page;
1039 rotations%=4;
cristyb32b90a2009-09-07 21:45:48 +00001040 if (rotations == 0)
1041 return(CloneImage(image,0,0,MagickTrue,exception));
cristy3ed852e2009-09-05 21:47:34 +00001042 if ((rotations == 1) || (rotations == 3))
1043 rotate_image=CloneImage(image,image->rows,image->columns,MagickTrue,
1044 exception);
1045 else
1046 rotate_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1047 exception);
1048 if (rotate_image == (Image *) NULL)
1049 return((Image *) NULL);
1050 /*
1051 Integral rotate the image.
1052 */
1053 status=MagickTrue;
1054 progress=0;
1055 image_view=AcquireCacheView(image);
1056 rotate_view=AcquireCacheView(rotate_image);
1057 switch (rotations)
1058 {
1059 case 0:
1060 {
1061 /*
1062 Rotate 0 degrees.
1063 */
cristy3ed852e2009-09-05 21:47:34 +00001064 break;
1065 }
1066 case 1:
1067 {
1068 long
1069 tile_y;
1070
cristyb32b90a2009-09-07 21:45:48 +00001071 unsigned long
1072 tile_height,
1073 tile_width;
1074
cristy3ed852e2009-09-05 21:47:34 +00001075 /*
1076 Rotate 90 degrees.
1077 */
cristyb32b90a2009-09-07 21:45:48 +00001078 GetPixelCacheTileSize(image,&tile_width,&tile_height);
cristyb5d5f722009-11-04 03:03:49 +00001079#if defined(MAGICKCORE_OPENMP_SUPPORT)
1080 #pragma omp parallel for schedule(dynamic,4) shared(progress, status)
cristydaa97692009-09-13 02:10:35 +00001081#endif
cristyb32b90a2009-09-07 21:45:48 +00001082 for (tile_y=0; tile_y < (long) image->rows; tile_y+=tile_height)
cristy3ed852e2009-09-05 21:47:34 +00001083 {
1084 register long
1085 tile_x;
1086
1087 if (status == MagickFalse)
1088 continue;
cristyb32b90a2009-09-07 21:45:48 +00001089 for (tile_x=0; tile_x < (long) image->columns; tile_x+=tile_width)
cristy3ed852e2009-09-05 21:47:34 +00001090 {
1091 MagickBooleanType
1092 sync;
1093
1094 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001095 *restrict indexes;
cristy3ed852e2009-09-05 21:47:34 +00001096
1097 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001098 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001099
1100 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001101 *restrict rotate_indexes;
cristy3ed852e2009-09-05 21:47:34 +00001102
1103 register long
1104 y;
1105
1106 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001107 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001108
1109 unsigned long
cristyb32b90a2009-09-07 21:45:48 +00001110 height,
1111 width;
cristy3ed852e2009-09-05 21:47:34 +00001112
cristyb32b90a2009-09-07 21:45:48 +00001113 width=tile_width;
1114 if ((tile_x+(long) tile_width) > (long) image->columns)
1115 width=(unsigned long) (tile_width-(tile_x+tile_width-
cristy3ed852e2009-09-05 21:47:34 +00001116 image->columns));
cristyb32b90a2009-09-07 21:45:48 +00001117 height=tile_height;
1118 if ((tile_y+(long) tile_height) > (long) image->rows)
1119 height=(unsigned long) (tile_height-(tile_y+tile_height-
cristy3ed852e2009-09-05 21:47:34 +00001120 image->rows));
cristydaa97692009-09-13 02:10:35 +00001121 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1122 exception);
cristy3ed852e2009-09-05 21:47:34 +00001123 if (p == (const PixelPacket *) NULL)
1124 {
1125 status=MagickFalse;
1126 break;
1127 }
1128 indexes=GetCacheViewVirtualIndexQueue(image_view);
cristyb32b90a2009-09-07 21:45:48 +00001129 for (y=0; y < (long) width; y++)
cristy3ed852e2009-09-05 21:47:34 +00001130 {
1131 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001132 *restrict tile_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001133
1134 register long
1135 x;
1136
1137 q=QueueCacheViewAuthenticPixels(rotate_view,(long)
cristyb32b90a2009-09-07 21:45:48 +00001138 rotate_image->columns-(tile_y+height),y+tile_x,height,
cristy3ed852e2009-09-05 21:47:34 +00001139 1,exception);
1140 if (q == (PixelPacket *) NULL)
1141 {
1142 status=MagickFalse;
1143 break;
1144 }
cristyb32b90a2009-09-07 21:45:48 +00001145 tile_pixels=p+(height-1)*width+y;
1146 for (x=0; x < (long) height; x++)
cristy3ed852e2009-09-05 21:47:34 +00001147 {
1148 *q++=(*tile_pixels);
cristyb32b90a2009-09-07 21:45:48 +00001149 tile_pixels-=width;
cristy3ed852e2009-09-05 21:47:34 +00001150 }
1151 rotate_indexes=GetCacheViewAuthenticIndexQueue(rotate_view);
1152 if ((indexes != (IndexPacket *) NULL) &&
1153 (rotate_indexes != (IndexPacket *) NULL))
1154 {
1155 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001156 *restrict tile_indexes;
cristy3ed852e2009-09-05 21:47:34 +00001157
cristyb32b90a2009-09-07 21:45:48 +00001158 tile_indexes=indexes+(height-1)*width+y;
1159 for (x=0; x < (long) height; x++)
cristy3ed852e2009-09-05 21:47:34 +00001160 {
1161 *rotate_indexes++=(*tile_indexes);
cristyb32b90a2009-09-07 21:45:48 +00001162 tile_indexes-=width;
cristy3ed852e2009-09-05 21:47:34 +00001163 }
1164 }
1165 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1166 if (sync == MagickFalse)
1167 status=MagickFalse;
1168 }
1169 }
1170 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1171 {
1172 MagickBooleanType
1173 proceed;
1174
cristyb32b90a2009-09-07 21:45:48 +00001175 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
cristy3ed852e2009-09-05 21:47:34 +00001176 image->rows);
1177 if (proceed == MagickFalse)
1178 status=MagickFalse;
1179 }
1180 }
cristyb32b90a2009-09-07 21:45:48 +00001181 (void) SetImageProgress(image,RotateImageTag,image->rows-1,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001182 Swap(page.width,page.height);
1183 Swap(page.x,page.y);
1184 if (page.width != 0)
1185 page.x=(long) (page.width-rotate_image->columns-page.x);
1186 break;
1187 }
1188 case 2:
1189 {
1190 /*
1191 Rotate 180 degrees.
1192 */
cristyb5d5f722009-11-04 03:03:49 +00001193#if defined(MAGICKCORE_OPENMP_SUPPORT)
1194 #pragma omp parallel for schedule(dynamic,4) shared(progress, status)
cristydaa97692009-09-13 02:10:35 +00001195#endif
cristy3ed852e2009-09-05 21:47:34 +00001196 for (y=0; y < (long) image->rows; y++)
1197 {
1198 MagickBooleanType
1199 sync;
1200
1201 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001202 *restrict indexes;
cristy3ed852e2009-09-05 21:47:34 +00001203
1204 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001205 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001206
1207 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001208 *restrict rotate_indexes;
cristy3ed852e2009-09-05 21:47:34 +00001209
1210 register long
1211 x;
1212
1213 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001214 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001215
1216 if (status == MagickFalse)
1217 continue;
1218 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,
1219 exception);
1220 q=QueueCacheViewAuthenticPixels(rotate_view,0,(long) (image->rows-
1221 y-1),image->columns,1,exception);
1222 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
1223 {
1224 status=MagickFalse;
1225 continue;
1226 }
1227 indexes=GetCacheViewVirtualIndexQueue(image_view);
1228 rotate_indexes=GetCacheViewAuthenticIndexQueue(rotate_view);
1229 q+=image->columns;
1230 for (x=0; x < (long) image->columns; x++)
1231 *--q=(*p++);
1232 if ((indexes != (IndexPacket *) NULL) &&
1233 (rotate_indexes != (IndexPacket *) NULL))
1234 for (x=0; x < (long) image->columns; x++)
1235 rotate_indexes[image->columns-x-1]=indexes[x];
1236 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1237 if (sync == MagickFalse)
1238 status=MagickFalse;
1239 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1240 {
1241 MagickBooleanType
1242 proceed;
1243
1244 proceed=SetImageProgress(image,RotateImageTag,progress++,
1245 image->rows);
1246 if (proceed == MagickFalse)
1247 status=MagickFalse;
1248 }
1249 }
1250 if (page.width != 0)
1251 page.x=(long) (page.width-rotate_image->columns-page.x);
1252 if (page.height != 0)
1253 page.y=(long) (page.height-rotate_image->rows-page.y);
1254 break;
1255 }
1256 case 3:
1257 {
1258 long
1259 tile_y;
1260
cristyb32b90a2009-09-07 21:45:48 +00001261 unsigned long
1262 tile_height,
1263 tile_width;
1264
cristy3ed852e2009-09-05 21:47:34 +00001265 /*
1266 Rotate 270 degrees.
1267 */
cristyb32b90a2009-09-07 21:45:48 +00001268 GetPixelCacheTileSize(image,&tile_width,&tile_height);
cristyb5d5f722009-11-04 03:03:49 +00001269#if defined(MAGICKCORE_OPENMP_SUPPORT)
1270 #pragma omp parallel for schedule(dynamic,4) shared(progress, status)
cristydaa97692009-09-13 02:10:35 +00001271#endif
cristyb32b90a2009-09-07 21:45:48 +00001272 for (tile_y=0; tile_y < (long) image->rows; tile_y+=tile_height)
cristy3ed852e2009-09-05 21:47:34 +00001273 {
1274 register long
1275 tile_x;
1276
1277 if (status == MagickFalse)
1278 continue;
cristyb32b90a2009-09-07 21:45:48 +00001279 for (tile_x=0; tile_x < (long) image->columns; tile_x+=tile_width)
cristy3ed852e2009-09-05 21:47:34 +00001280 {
1281 MagickBooleanType
1282 sync;
1283
1284 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001285 *restrict indexes;
cristy3ed852e2009-09-05 21:47:34 +00001286
1287 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001288 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001289
1290 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001291 *restrict rotate_indexes;
cristy3ed852e2009-09-05 21:47:34 +00001292
1293 register long
1294 y;
1295
1296 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001297 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001298
1299 unsigned long
cristyb32b90a2009-09-07 21:45:48 +00001300 height,
1301 width;
cristy3ed852e2009-09-05 21:47:34 +00001302
cristyb32b90a2009-09-07 21:45:48 +00001303 width=tile_width;
1304 if ((tile_x+(long) tile_width) > (long) image->columns)
1305 width=(unsigned long) (tile_width-(tile_x+tile_width-
cristy3ed852e2009-09-05 21:47:34 +00001306 image->columns));
cristyb32b90a2009-09-07 21:45:48 +00001307 height=tile_height;
1308 if ((tile_y+(long) tile_height) > (long) image->rows)
1309 height=(unsigned long) (tile_height-(tile_y+tile_height-
cristy3ed852e2009-09-05 21:47:34 +00001310 image->rows));
cristyb32b90a2009-09-07 21:45:48 +00001311 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,
1312 height,exception);
cristy3ed852e2009-09-05 21:47:34 +00001313 if (p == (const PixelPacket *) NULL)
1314 {
1315 status=MagickFalse;
1316 break;
1317 }
1318 indexes=GetCacheViewVirtualIndexQueue(image_view);
cristyb32b90a2009-09-07 21:45:48 +00001319 for (y=0; y < (long) width; y++)
cristy3ed852e2009-09-05 21:47:34 +00001320 {
1321 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001322 *restrict tile_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001323
1324 register long
1325 x;
1326
1327 q=QueueCacheViewAuthenticPixels(rotate_view,tile_y,(long)
cristyb32b90a2009-09-07 21:45:48 +00001328 y+rotate_image->rows-(tile_x+width),height,1,exception);
cristy3ed852e2009-09-05 21:47:34 +00001329 if (q == (PixelPacket *) NULL)
1330 {
1331 status=MagickFalse;
1332 break;
1333 }
cristyb32b90a2009-09-07 21:45:48 +00001334 tile_pixels=p+(width-1)-y;
1335 for (x=0; x < (long) height; x++)
cristy3ed852e2009-09-05 21:47:34 +00001336 {
1337 *q++=(*tile_pixels);
cristyb32b90a2009-09-07 21:45:48 +00001338 tile_pixels+=width;
cristy3ed852e2009-09-05 21:47:34 +00001339 }
1340 rotate_indexes=GetCacheViewAuthenticIndexQueue(rotate_view);
1341 if ((indexes != (IndexPacket *) NULL) &&
1342 (rotate_indexes != (IndexPacket *) NULL))
1343 {
1344 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001345 *restrict tile_indexes;
cristy3ed852e2009-09-05 21:47:34 +00001346
cristyb32b90a2009-09-07 21:45:48 +00001347 tile_indexes=indexes+(width-1)-y;
1348 for (x=0; x < (long) height; x++)
cristy3ed852e2009-09-05 21:47:34 +00001349 {
1350 *rotate_indexes++=(*tile_indexes);
cristyb32b90a2009-09-07 21:45:48 +00001351 tile_indexes+=width;
cristy3ed852e2009-09-05 21:47:34 +00001352 }
1353 }
1354 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1355 if (sync == MagickFalse)
1356 status=MagickFalse;
1357 }
1358 }
1359 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1360 {
1361 MagickBooleanType
1362 proceed;
1363
cristyb32b90a2009-09-07 21:45:48 +00001364 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
cristy3ed852e2009-09-05 21:47:34 +00001365 image->rows);
1366 if (proceed == MagickFalse)
1367 status=MagickFalse;
1368 }
1369 }
cristyb32b90a2009-09-07 21:45:48 +00001370 (void) SetImageProgress(image,RotateImageTag,image->rows-1,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001371 Swap(page.width,page.height);
1372 Swap(page.x,page.y);
1373 if (page.height != 0)
1374 page.y=(long) (page.height-rotate_image->rows-page.y);
1375 break;
1376 }
1377 }
1378 rotate_view=DestroyCacheView(rotate_view);
1379 image_view=DestroyCacheView(image_view);
1380 rotate_image->type=image->type;
1381 rotate_image->page=page;
1382 if (status == MagickFalse)
1383 rotate_image=DestroyImage(rotate_image);
1384 return(rotate_image);
1385}
1386
1387/*
1388%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1389% %
1390% %
1391% %
1392+ X S h e a r I m a g e %
1393% %
1394% %
1395% %
1396%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1397%
1398% XShearImage() shears the image in the X direction with a shear angle of
1399% 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1400% negative angles shear clockwise. Angles are measured relative to a vertical
1401% Y-axis. X shears will widen an image creating 'empty' triangles on the left
1402% and right sides of the source image.
1403%
1404% The format of the XShearImage method is:
1405%
1406% MagickBooleanType XShearImage(Image *image,const MagickRealType degrees,
1407% const unsigned long width,const unsigned long height,
1408% const long x_offset,const long y_offset)
1409%
1410% A description of each parameter follows.
1411%
1412% o image: the image.
1413%
1414% o degrees: A MagickRealType representing the shearing angle along the X
1415% axis.
1416%
1417% o width, height, x_offset, y_offset: Defines a region of the image
1418% to shear.
1419%
1420*/
1421static MagickBooleanType XShearImage(Image *image,const MagickRealType degrees,
1422 const unsigned long width,const unsigned long height,const long x_offset,
1423 const long y_offset)
1424{
1425#define XShearImageTag "XShear/Image"
1426
1427 typedef enum
1428 {
1429 LEFT,
1430 RIGHT
1431 } ShearDirection;
1432
1433 CacheView
1434 *image_view;
1435
1436 ExceptionInfo
1437 *exception;
1438
1439 long
1440 progress,
1441 y;
1442
1443 MagickBooleanType
1444 status;
1445
1446 MagickPixelPacket
1447 background;
1448
1449 assert(image != (Image *) NULL);
1450 assert(image->signature == MagickSignature);
1451 if (image->debug != MagickFalse)
1452 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1453 GetMagickPixelPacket(image,&background);
1454 SetMagickPixelPacket(image,&image->background_color,(IndexPacket *) NULL,
1455 &background);
1456 if (image->colorspace == CMYKColorspace)
1457 ConvertRGBToCMYK(&background);
1458 /*
1459 XShear image.
1460 */
1461 status=MagickTrue;
1462 progress=0;
1463 exception=(&image->exception);
1464 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00001465#if defined(MAGICKCORE_OPENMP_SUPPORT)
1466 #pragma omp parallel for schedule(dynamic,4) shared(progress, status)
cristy3ed852e2009-09-05 21:47:34 +00001467#endif
1468 for (y=0; y < (long) height; y++)
1469 {
1470 long
1471 step;
1472
1473 MagickPixelPacket
1474 pixel,
1475 source,
1476 destination;
1477
1478 MagickRealType
1479 area,
1480 displacement;
1481
1482 register long
1483 i;
1484
1485 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001486 *restrict indexes,
1487 *restrict shear_indexes;
cristy3ed852e2009-09-05 21:47:34 +00001488
1489 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001490 *restrict p,
1491 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001492
1493 ShearDirection
1494 direction;
1495
1496 if (status == MagickFalse)
1497 continue;
1498 p=GetCacheViewAuthenticPixels(image_view,0,y_offset+y,image->columns,1,
1499 exception);
1500 if (p == (PixelPacket *) NULL)
1501 {
1502 status=MagickFalse;
1503 continue;
1504 }
1505 indexes=GetCacheViewAuthenticIndexQueue(image_view);
1506 p+=x_offset;
1507 indexes+=x_offset;
1508 displacement=degrees*(MagickRealType) (y-height/2.0);
1509 if (displacement == 0.0)
1510 continue;
1511 if (displacement > 0.0)
1512 direction=RIGHT;
1513 else
1514 {
1515 displacement*=(-1.0);
1516 direction=LEFT;
1517 }
1518 step=(long) floor((double) displacement);
1519 area=(MagickRealType) (displacement-step);
1520 step++;
1521 pixel=background;
1522 GetMagickPixelPacket(image,&source);
1523 GetMagickPixelPacket(image,&destination);
1524 switch (direction)
1525 {
1526 case LEFT:
1527 {
1528 /*
1529 Transfer pixels left-to-right.
1530 */
1531 if (step > x_offset)
1532 break;
1533 q=p-step;
1534 shear_indexes=indexes-step;
1535 for (i=0; i < (long) width; i++)
1536 {
1537 if ((x_offset+i) < step)
1538 {
1539 SetMagickPixelPacket(image,++p,++indexes,&pixel);
1540 q++;
1541 shear_indexes++;
1542 continue;
1543 }
1544 SetMagickPixelPacket(image,p,indexes,&source);
1545 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1546 &source,(MagickRealType) p->opacity,area,&destination);
1547 SetPixelPacket(image,&destination,q++,shear_indexes++);
1548 SetMagickPixelPacket(image,p++,indexes++,&pixel);
1549 }
1550 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1551 &background,(MagickRealType) background.opacity,area,&destination);
1552 SetPixelPacket(image,&destination,q++,shear_indexes++);
1553 for (i=0; i < (step-1); i++)
1554 SetPixelPacket(image,&background,q++,shear_indexes++);
1555 break;
1556 }
1557 case RIGHT:
1558 {
1559 /*
1560 Transfer pixels right-to-left.
1561 */
1562 p+=width;
1563 indexes+=width;
1564 q=p+step;
1565 shear_indexes=indexes+step;
1566 for (i=0; i < (long) width; i++)
1567 {
1568 p--;
1569 indexes--;
1570 q--;
1571 shear_indexes--;
1572 if ((unsigned long) (x_offset+width+step-i) >= image->columns)
1573 continue;
1574 SetMagickPixelPacket(image,p,indexes,&source);
1575 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1576 &source,(MagickRealType) p->opacity,area,&destination);
1577 SetPixelPacket(image,&destination,q,shear_indexes);
1578 SetMagickPixelPacket(image,p,indexes,&pixel);
1579 }
1580 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1581 &background,(MagickRealType) background.opacity,area,&destination);
1582 SetPixelPacket(image,&destination,--q,--shear_indexes);
1583 for (i=0; i < (step-1); i++)
1584 SetPixelPacket(image,&background,--q,--shear_indexes);
1585 break;
1586 }
1587 }
1588 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1589 status=MagickFalse;
1590 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1591 {
1592 MagickBooleanType
1593 proceed;
1594
cristyb5d5f722009-11-04 03:03:49 +00001595#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00001596 #pragma omp critical (MagickCore_XShearImage)
1597#endif
1598 proceed=SetImageProgress(image,XShearImageTag,progress++,height);
1599 if (proceed == MagickFalse)
1600 status=MagickFalse;
1601 }
1602 }
1603 image_view=DestroyCacheView(image_view);
1604 return(status);
1605}
1606
1607/*
1608%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1609% %
1610% %
1611% %
1612+ Y S h e a r I m a g e %
1613% %
1614% %
1615% %
1616%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1617%
1618% YShearImage shears the image in the Y direction with a shear angle of
1619% 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1620% negative angles shear clockwise. Angles are measured relative to a
1621% horizontal X-axis. Y shears will increase the height of an image creating
1622% 'empty' triangles on the top and bottom of the source image.
1623%
1624% The format of the YShearImage method is:
1625%
1626% MagickBooleanType YShearImage(Image *image,const MagickRealType degrees,
1627% const unsigned long width,const unsigned long height,
1628% const long x_offset,const long y_offset)
1629%
1630% A description of each parameter follows.
1631%
1632% o image: the image.
1633%
1634% o degrees: A MagickRealType representing the shearing angle along the Y
1635% axis.
1636%
1637% o width, height, x_offset, y_offset: Defines a region of the image
1638% to shear.
1639%
1640*/
1641static MagickBooleanType YShearImage(Image *image,const MagickRealType degrees,
1642 const unsigned long width,const unsigned long height,const long x_offset,
1643 const long y_offset)
1644{
1645#define YShearImageTag "YShear/Image"
1646
1647 typedef enum
1648 {
1649 UP,
1650 DOWN
1651 } ShearDirection;
1652
1653 CacheView
1654 *image_view;
1655
1656 ExceptionInfo
1657 *exception;
1658
1659 long
1660 progress,
1661 x;
1662
1663 MagickBooleanType
1664 status;
1665
1666 MagickPixelPacket
1667 background;
1668
1669 assert(image != (Image *) NULL);
1670 assert(image->signature == MagickSignature);
1671 if (image->debug != MagickFalse)
1672 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1673 GetMagickPixelPacket(image,&background);
1674 SetMagickPixelPacket(image,&image->background_color,(IndexPacket *) NULL,
1675 &background);
1676 if (image->colorspace == CMYKColorspace)
1677 ConvertRGBToCMYK(&background);
1678 /*
1679 Y Shear image.
1680 */
1681 status=MagickTrue;
1682 progress=0;
1683 exception=(&image->exception);
1684 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00001685#if defined(MAGICKCORE_OPENMP_SUPPORT)
1686 #pragma omp parallel for schedule(dynamic,4) shared(progress, status)
cristy3ed852e2009-09-05 21:47:34 +00001687#endif
1688 for (x=0; x < (long) width; x++)
1689 {
1690 long
1691 step;
1692
1693 MagickPixelPacket
1694 pixel,
1695 source,
1696 destination;
1697
1698 MagickRealType
1699 area,
1700 displacement;
1701
1702 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001703 *restrict indexes,
1704 *restrict shear_indexes;
cristy3ed852e2009-09-05 21:47:34 +00001705
1706 register long
1707 i;
1708
1709 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001710 *restrict p,
1711 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001712
1713 ShearDirection
1714 direction;
1715
1716 if (status == MagickFalse)
1717 continue;
1718 p=GetCacheViewAuthenticPixels(image_view,x_offset+x,0,1,image->rows,
1719 exception);
1720 if (p == (PixelPacket *) NULL)
1721 {
1722 status=MagickFalse;
1723 continue;
1724 }
1725 indexes=GetCacheViewAuthenticIndexQueue(image_view);
1726 p+=y_offset;
1727 indexes+=y_offset;
1728 displacement=degrees*(MagickRealType) (x-width/2.0);
1729 if (displacement == 0.0)
1730 continue;
1731 if (displacement > 0.0)
1732 direction=DOWN;
1733 else
1734 {
1735 displacement*=(-1.0);
1736 direction=UP;
1737 }
1738 step=(long) floor((double) displacement);
1739 area=(MagickRealType) (displacement-step);
1740 step++;
1741 pixel=background;
1742 GetMagickPixelPacket(image,&source);
1743 GetMagickPixelPacket(image,&destination);
1744 switch (direction)
1745 {
1746 case UP:
1747 {
1748 /*
1749 Transfer pixels top-to-bottom.
1750 */
1751 if (step > y_offset)
1752 break;
1753 q=p-step;
1754 shear_indexes=indexes-step;
1755 for (i=0; i < (long) height; i++)
1756 {
1757 if ((y_offset+i) < step)
1758 {
1759 SetMagickPixelPacket(image,++p,++indexes,&pixel);
1760 q++;
1761 shear_indexes++;
1762 continue;
1763 }
1764 SetMagickPixelPacket(image,p,indexes,&source);
1765 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1766 &source,(MagickRealType) p->opacity,area,&destination);
1767 SetPixelPacket(image,&destination,q++,shear_indexes++);
1768 SetMagickPixelPacket(image,p++,indexes++,&pixel);
1769 }
1770 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1771 &background,(MagickRealType) background.opacity,area,&destination);
1772 SetPixelPacket(image,&destination,q++,shear_indexes++);
1773 for (i=0; i < (step-1); i++)
1774 SetPixelPacket(image,&background,q++,shear_indexes++);
1775 break;
1776 }
1777 case DOWN:
1778 {
1779 /*
1780 Transfer pixels bottom-to-top.
1781 */
1782 p+=height;
1783 indexes+=height;
1784 q=p+step;
1785 shear_indexes=indexes+step;
1786 for (i=0; i < (long) height; i++)
1787 {
1788 p--;
1789 indexes--;
1790 q--;
1791 shear_indexes--;
1792 if ((unsigned long) (y_offset+height+step-i) >= image->rows)
1793 continue;
1794 SetMagickPixelPacket(image,p,indexes,&source);
1795 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1796 &source,(MagickRealType) p->opacity,area,&destination);
1797 SetPixelPacket(image,&destination,q,shear_indexes);
1798 SetMagickPixelPacket(image,p,indexes,&pixel);
1799 }
1800 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1801 &background,(MagickRealType) background.opacity,area,&destination);
1802 SetPixelPacket(image,&destination,--q,--shear_indexes);
1803 for (i=0; i < (step-1); i++)
1804 SetPixelPacket(image,&background,--q,--shear_indexes);
1805 break;
1806 }
1807 }
1808 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1809 status=MagickFalse;
1810 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1811 {
1812 MagickBooleanType
1813 proceed;
1814
cristyb5d5f722009-11-04 03:03:49 +00001815#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00001816 #pragma omp critical (MagickCore_YShearImage)
1817#endif
1818 proceed=SetImageProgress(image,YShearImageTag,progress++,image->rows);
1819 if (proceed == MagickFalse)
1820 status=MagickFalse;
1821 }
1822 }
1823 image_view=DestroyCacheView(image_view);
1824 return(status);
1825}
1826
1827/*
1828%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1829% %
1830% %
1831% %
1832% R o t a t e I m a g e %
1833% %
1834% %
1835% %
1836%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1837%
1838% RotateImage() creates a new image that is a rotated copy of an existing
1839% one. Positive angles rotate counter-clockwise (right-hand rule), while
1840% negative angles rotate clockwise. Rotated images are usually larger than
1841% the originals and have 'empty' triangular corners. X axis. Empty
1842% triangles left over from shearing the image are filled with the background
1843% color defined by member 'background_color' of the image. RotateImage
1844% allocates the memory necessary for the new Image structure and returns a
1845% pointer to the new image.
1846%
1847% RotateImage() is based on the paper "A Fast Algorithm for General
1848% Raster Rotatation" by Alan W. Paeth. RotateImage is adapted from a similar
1849% method based on the Paeth paper written by Michael Halle of the Spatial
1850% Imaging Group, MIT Media Lab.
1851%
1852% The format of the RotateImage method is:
1853%
1854% Image *RotateImage(const Image *image,const double degrees,
1855% ExceptionInfo *exception)
1856%
1857% A description of each parameter follows.
1858%
1859% o image: the image.
1860%
1861% o degrees: Specifies the number of degrees to rotate the image.
1862%
1863% o exception: return any errors or warnings in this structure.
1864%
1865*/
1866MagickExport Image *RotateImage(const Image *image,const double degrees,
1867 ExceptionInfo *exception)
1868{
1869 Image
1870 *integral_image,
1871 *rotate_image;
1872
1873 long
1874 x_offset,
1875 y_offset;
1876
1877 MagickRealType
1878 angle;
1879
1880 PointInfo
1881 shear;
1882
1883 RectangleInfo
1884 border_info;
1885
1886 unsigned long
1887 height,
1888 rotations,
1889 width,
1890 y_width;
1891
1892 /*
1893 Adjust rotation angle.
1894 */
1895 assert(image != (Image *) NULL);
1896 assert(image->signature == MagickSignature);
1897 if (image->debug != MagickFalse)
1898 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1899 assert(exception != (ExceptionInfo *) NULL);
1900 assert(exception->signature == MagickSignature);
1901 angle=degrees;
1902 while (angle < -45.0)
1903 angle+=360.0;
1904 for (rotations=0; angle > 45.0; rotations++)
1905 angle-=90.0;
1906 rotations%=4;
1907 /*
1908 Calculate shear equations.
1909 */
1910 integral_image=IntegralRotateImage(image,rotations,exception);
1911 if (integral_image == (Image *) NULL)
1912 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1913 shear.x=(-tan((double) DegreesToRadians(angle)/2.0));
1914 shear.y=sin((double) DegreesToRadians(angle));
1915 if ((shear.x == 0.0) && (shear.y == 0.0))
1916 return(integral_image);
1917 if (SetImageStorageClass(integral_image,DirectClass) == MagickFalse)
1918 {
1919 InheritException(exception,&integral_image->exception);
1920 integral_image=DestroyImage(integral_image);
1921 return(integral_image);
1922 }
1923 if (integral_image->matte == MagickFalse)
1924 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel);
1925 /*
1926 Compute image size.
1927 */
1928 width=image->columns;
1929 height=image->rows;
1930 if ((rotations == 1) || (rotations == 3))
1931 {
1932 width=image->rows;
1933 height=image->columns;
1934 }
1935 y_width=width+(long) (fabs(shear.x)*height+0.5);
1936 x_offset=(long) (width+((fabs(shear.y)*height)-width)/2.0+0.5);
1937 y_offset=(long) (height+((fabs(shear.y)*y_width)-height)/2.0+0.5);
1938 /*
1939 Surround image with a border.
1940 */
1941 integral_image->border_color=integral_image->background_color;
1942 integral_image->compose=CopyCompositeOp;
1943 border_info.width=(unsigned long) x_offset;
1944 border_info.height=(unsigned long) y_offset;
1945 rotate_image=BorderImage(integral_image,&border_info,exception);
1946 integral_image=DestroyImage(integral_image);
1947 if (rotate_image == (Image *) NULL)
1948 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1949 /*
1950 Rotate the image.
1951 */
1952 (void) XShearImage(rotate_image,shear.x,width,height,x_offset,
1953 ((long) rotate_image->rows-height)/2);
1954 (void) YShearImage(rotate_image,shear.y,y_width,height,
1955 ((long) rotate_image->columns-y_width)/2,y_offset);
1956 (void) XShearImage(rotate_image,shear.x,y_width,rotate_image->rows,
1957 ((long) rotate_image->columns-y_width)/2,0);
1958 CropToFitImage(&rotate_image,shear.x,shear.y,(MagickRealType) width,
1959 (MagickRealType) height,MagickTrue,exception);
1960 rotate_image->compose=image->compose;
1961 rotate_image->page.width=0;
1962 rotate_image->page.height=0;
1963 return(rotate_image);
1964}
1965
1966/*
1967%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1968% %
1969% %
1970% %
1971% S h e a r I m a g e %
1972% %
1973% %
1974% %
1975%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1976%
1977% ShearImage() creates a new image that is a shear_image copy of an existing
1978% one. Shearing slides one edge of an image along the X or Y axis, creating
1979% a parallelogram. An X direction shear slides an edge along the X axis,
1980% while a Y direction shear slides an edge along the Y axis. The amount of
1981% the shear is controlled by a shear angle. For X direction shears, x_shear
1982% is measured relative to the Y axis, and similarly, for Y direction shears
1983% y_shear is measured relative to the X axis. Empty triangles left over from
1984% shearing the image are filled with the background color defined by member
1985% 'background_color' of the image.. ShearImage() allocates the memory
1986% necessary for the new Image structure and returns a pointer to the new image.
1987%
1988% ShearImage() is based on the paper "A Fast Algorithm for General Raster
1989% Rotatation" by Alan W. Paeth.
1990%
1991% The format of the ShearImage method is:
1992%
1993% Image *ShearImage(const Image *image,const double x_shear,
1994% const double y_shear,ExceptionInfo *exception)
1995%
1996% A description of each parameter follows.
1997%
1998% o image: the image.
1999%
2000% o x_shear, y_shear: Specifies the number of degrees to shear the image.
2001%
2002% o exception: return any errors or warnings in this structure.
2003%
2004*/
2005MagickExport Image *ShearImage(const Image *image,const double x_shear,
2006 const double y_shear,ExceptionInfo *exception)
2007{
2008 Image
2009 *integral_image,
2010 *shear_image;
2011
2012 long
2013 x_offset,
2014 y_offset;
2015
2016 PointInfo
2017 shear;
2018
2019 RectangleInfo
2020 border_info;
2021
2022 unsigned long
2023 y_width;
2024
2025 assert(image != (Image *) NULL);
2026 assert(image->signature == MagickSignature);
2027 if (image->debug != MagickFalse)
2028 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2029 assert(exception != (ExceptionInfo *) NULL);
2030 assert(exception->signature == MagickSignature);
2031 if ((x_shear != 0.0) && (fmod(x_shear,90.0) == 0.0))
2032 ThrowImageException(ImageError,"AngleIsDiscontinuous");
2033 if ((y_shear != 0.0) && (fmod(y_shear,90.0) == 0.0))
2034 ThrowImageException(ImageError,"AngleIsDiscontinuous");
2035 /*
2036 Initialize shear angle.
2037 */
2038 integral_image=CloneImage(image,0,0,MagickTrue,exception);
2039 if (integral_image == (Image *) NULL)
2040 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2041 shear.x=(-tan(DegreesToRadians(fmod(x_shear,360.0))));
2042 shear.y=tan(DegreesToRadians(fmod(y_shear,360.0)));
2043 if ((shear.x == 0.0) && (shear.y == 0.0))
2044 return(integral_image);
2045 if (SetImageStorageClass(integral_image,DirectClass) == MagickFalse)
2046 {
2047 InheritException(exception,&integral_image->exception);
2048 integral_image=DestroyImage(integral_image);
2049 return(integral_image);
2050 }
2051 if (integral_image->matte == MagickFalse)
2052 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel);
2053 /*
2054 Compute image size.
2055 */
2056 y_width=image->columns+(long) (fabs(shear.x)*image->rows+0.5);
2057 x_offset=(long) (image->columns+((fabs(shear.x)*image->rows)-image->columns)/
2058 2.0+0.5);
2059 y_offset=(long) (image->rows+((fabs(shear.y)*y_width)-image->rows)/2.0+0.5);
2060 /*
2061 Surround image with border.
2062 */
2063 integral_image->border_color=integral_image->background_color;
2064 integral_image->compose=CopyCompositeOp;
2065 border_info.width=(unsigned long) x_offset;
2066 border_info.height=(unsigned long) y_offset;
2067 shear_image=BorderImage(integral_image,&border_info,exception);
2068 if (shear_image == (Image *) NULL)
2069 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2070 integral_image=DestroyImage(integral_image);
2071 /*
2072 Shear the image.
2073 */
2074 if (shear_image->matte == MagickFalse)
2075 (void) SetImageAlphaChannel(shear_image,OpaqueAlphaChannel);
2076 (void) XShearImage(shear_image,shear.x,image->columns,image->rows,x_offset,
2077 ((long) shear_image->rows-image->rows)/2);
2078 (void) YShearImage(shear_image,shear.y,y_width,image->rows,
2079 ((long) shear_image->columns-y_width)/2,y_offset);
2080 CropToFitImage(&shear_image,shear.x,shear.y,(MagickRealType) image->columns,
2081 (MagickRealType) image->rows,MagickFalse,exception);
2082 shear_image->compose=image->compose;
2083 shear_image->page.width=0;
2084 shear_image->page.height=0;
2085 return(shear_image);
2086}