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