blob: d8867cf7ee655c703e45ac1549b25f1fa67ef7d8 [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%
cristyecc2c142010-01-17 22:25:46 +0000154% MagickBooleanType CropToFitImage(Image **image,
155% const MagickRealType x_shear,const MagickRealType x_shear,
156% const MagickRealType width,const MagickRealType height,
157% const MagickBooleanType rotate,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000158%
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*/
cristyecc2c142010-01-17 22:25:46 +0000168static MagickBooleanType CropToFitImage(Image **image,
169 const MagickRealType x_shear,const MagickRealType y_shear,
170 const MagickRealType width,const MagickRealType height,
171 const MagickBooleanType rotate,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000172{
173 Image
174 *crop_image;
175
176 PointInfo
177 extent[4],
178 min,
179 max;
180
181 RectangleInfo
182 geometry,
183 page;
184
cristybb503372010-05-27 20:51:26 +0000185 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000186 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 }
cristybb503372010-05-27 20:51:26 +0000221 geometry.x=(ssize_t) ceil(min.x-0.5);
222 geometry.y=(ssize_t) ceil(min.y-0.5);
223 geometry.width=(size_t) floor(max.x-min.x+0.5);
224 geometry.height=(size_t) floor(max.y-min.y+0.5);
cristy3ed852e2009-09-05 21:47:34 +0000225 page=(*image)->page;
226 (void) ParseAbsoluteGeometry("0x0+0+0",&(*image)->page);
227 crop_image=CropImage(*image,&geometry,exception);
cristyecc2c142010-01-17 22:25:46 +0000228 if (crop_image == (Image *) NULL)
229 return(MagickFalse);
230 crop_image->page=page;
231 *image=DestroyImage(*image);
232 *image=crop_image;
233 return(MagickTrue);
cristy3ed852e2009-09-05 21:47:34 +0000234}
235
236/*
237%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
238% %
239% %
240% %
241% D e s k e w I m a g e %
242% %
243% %
244% %
245%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
246%
247% DeskewImage() removes skew from the image. Skew is an artifact that
248% occurs in scanned images because of the camera being misaligned,
249% imperfections in the scanning or surface, or simply because the paper was
250% not placed completely flat when scanned.
251%
252% The format of the DeskewImage method is:
253%
254% Image *DeskewImage(const Image *image,const double threshold,
255% ExceptionInfo *exception)
256%
257% A description of each parameter follows:
258%
259% o image: the image.
260%
261% o threshold: separate background from foreground.
262%
263% o exception: return any errors or warnings in this structure.
264%
265*/
266
267typedef struct _RadonInfo
268{
269 CacheType
270 type;
271
cristybb503372010-05-27 20:51:26 +0000272 size_t
cristy3ed852e2009-09-05 21:47:34 +0000273 width,
274 height;
275
276 MagickSizeType
277 length;
278
279 MagickBooleanType
280 mapped;
281
282 char
283 path[MaxTextExtent];
284
285 int
286 file;
287
288 unsigned short
289 *cells;
290} RadonInfo;
291
292static RadonInfo *DestroyRadonInfo(RadonInfo *radon_info)
293{
294 assert(radon_info != (RadonInfo *) NULL);
295 switch (radon_info->type)
296 {
297 case MemoryCache:
298 {
299 if (radon_info->mapped == MagickFalse)
300 radon_info->cells=(unsigned short *) RelinquishMagickMemory(
301 radon_info->cells);
302 else
303 radon_info->cells=(unsigned short *) UnmapBlob(radon_info->cells,
304 (size_t) radon_info->length);
305 RelinquishMagickResource(MemoryResource,radon_info->length);
306 break;
307 }
308 case MapCache:
309 {
310 radon_info->cells=(unsigned short *) UnmapBlob(radon_info->cells,(size_t)
311 radon_info->length);
312 RelinquishMagickResource(MapResource,radon_info->length);
313 }
314 case DiskCache:
315 {
316 if (radon_info->file != -1)
317 (void) close(radon_info->file);
318 (void) RelinquishUniqueFileResource(radon_info->path);
319 RelinquishMagickResource(DiskResource,radon_info->length);
320 break;
321 }
322 default:
323 break;
324 }
325 return((RadonInfo *) RelinquishMagickMemory(radon_info));
326}
327
328static MagickBooleanType ResetRadonCells(RadonInfo *radon_info)
329{
cristybb503372010-05-27 20:51:26 +0000330 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000331 y;
332
cristybb503372010-05-27 20:51:26 +0000333 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000334 x;
335
336 ssize_t
337 count;
338
339 unsigned short
340 value;
341
342 if (radon_info->type != DiskCache)
343 {
344 (void) ResetMagickMemory(radon_info->cells,0,(size_t) radon_info->length);
345 return(MagickTrue);
346 }
347 value=0;
348 (void) MagickSeek(radon_info->file,0,SEEK_SET);
cristybb503372010-05-27 20:51:26 +0000349 for (y=0; y < (ssize_t) radon_info->height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000350 {
cristybb503372010-05-27 20:51:26 +0000351 for (x=0; x < (ssize_t) radon_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000352 {
353 count=write(radon_info->file,&value,sizeof(*radon_info->cells));
354 if (count != (ssize_t) sizeof(*radon_info->cells))
355 break;
356 }
cristybb503372010-05-27 20:51:26 +0000357 if (x < (ssize_t) radon_info->width)
cristy3ed852e2009-09-05 21:47:34 +0000358 break;
359 }
cristybb503372010-05-27 20:51:26 +0000360 return(y < (ssize_t) radon_info->height ? MagickFalse : MagickTrue);
cristy3ed852e2009-09-05 21:47:34 +0000361}
362
cristybb503372010-05-27 20:51:26 +0000363static RadonInfo *AcquireRadonInfo(const Image *image,const size_t width,
364 const size_t height,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000365{
366 MagickBooleanType
367 status;
368
369 RadonInfo
370 *radon_info;
371
cristy90823212009-12-12 20:48:33 +0000372 radon_info=(RadonInfo *) AcquireAlignedMemory(1,sizeof(*radon_info));
cristy3ed852e2009-09-05 21:47:34 +0000373 if (radon_info == (RadonInfo *) NULL)
374 return((RadonInfo *) NULL);
375 (void) ResetMagickMemory(radon_info,0,sizeof(*radon_info));
376 radon_info->width=width;
377 radon_info->height=height;
378 radon_info->length=(MagickSizeType) width*height*sizeof(*radon_info->cells);
379 radon_info->type=MemoryCache;
380 status=AcquireMagickResource(AreaResource,radon_info->length);
381 if ((status != MagickFalse) &&
382 (radon_info->length == (MagickSizeType) ((size_t) radon_info->length)))
383 {
384 status=AcquireMagickResource(MemoryResource,radon_info->length);
385 if (status != MagickFalse)
386 {
387 radon_info->mapped=MagickFalse;
388 radon_info->cells=(unsigned short *) AcquireMagickMemory((size_t)
389 radon_info->length);
390 if (radon_info->cells == (unsigned short *) NULL)
391 {
392 radon_info->mapped=MagickTrue;
393 radon_info->cells=(unsigned short *) MapBlob(-1,IOMode,0,(size_t)
394 radon_info->length);
395 }
396 if (radon_info->cells == (unsigned short *) NULL)
397 RelinquishMagickResource(MemoryResource,radon_info->length);
398 }
399 }
400 radon_info->file=(-1);
401 if (radon_info->cells == (unsigned short *) NULL)
402 {
403 status=AcquireMagickResource(DiskResource,radon_info->length);
404 if (status == MagickFalse)
405 {
406 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
407 "CacheResourcesExhausted","`%s'",image->filename);
408 return(DestroyRadonInfo(radon_info));
409 }
410 radon_info->type=DiskCache;
411 (void) AcquireMagickResource(MemoryResource,radon_info->length);
412 radon_info->file=AcquireUniqueFileResource(radon_info->path);
413 if (radon_info->file == -1)
414 return(DestroyRadonInfo(radon_info));
415 status=AcquireMagickResource(MapResource,radon_info->length);
416 if (status != MagickFalse)
417 {
418 status=ResetRadonCells(radon_info);
419 if (status != MagickFalse)
420 {
421 radon_info->cells=(unsigned short *) MapBlob(radon_info->file,
422 IOMode,0,(size_t) radon_info->length);
423 if (radon_info->cells != (unsigned short *) NULL)
424 radon_info->type=MapCache;
425 else
426 RelinquishMagickResource(MapResource,radon_info->length);
427 }
428 }
429 }
430 return(radon_info);
431}
432
433static inline size_t MagickMin(const size_t x,const size_t y)
434{
435 if (x < y)
436 return(x);
437 return(y);
438}
439
440static inline ssize_t ReadRadonCell(const RadonInfo *radon_info,
441 const off_t offset,const size_t length,unsigned char *buffer)
442{
443 register ssize_t
444 i;
445
446 ssize_t
447 count;
448
449#if !defined(MAGICKCORE_HAVE_PPREAD)
cristyb5d5f722009-11-04 03:03:49 +0000450#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000451 #pragma omp critical (MagickCore_ReadRadonCell)
452#endif
453 {
454 i=(-1);
455 if (MagickSeek(radon_info->file,offset,SEEK_SET) >= 0)
456 {
457#endif
458 count=0;
459 for (i=0; i < (ssize_t) length; i+=count)
460 {
461#if !defined(MAGICKCORE_HAVE_PPREAD)
462 count=read(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
463 SSIZE_MAX));
464#else
465 count=pread(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
466 SSIZE_MAX),(off_t) (offset+i));
467#endif
468 if (count > 0)
469 continue;
470 count=0;
471 if (errno != EINTR)
472 {
473 i=(-1);
474 break;
475 }
476 }
477#if !defined(MAGICKCORE_HAVE_PPREAD)
478 }
479 }
480#endif
481 return(i);
482}
483
484static inline ssize_t WriteRadonCell(const RadonInfo *radon_info,
485 const off_t offset,const size_t length,const unsigned char *buffer)
486{
487 register ssize_t
488 i;
489
490 ssize_t
491 count;
492
493#if !defined(MAGICKCORE_HAVE_PWRITE)
cristyb5d5f722009-11-04 03:03:49 +0000494#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000495 #pragma omp critical (MagickCore_WriteRadonCell)
496#endif
497 {
498 if (MagickSeek(radon_info->file,offset,SEEK_SET) >= 0)
499 {
500#endif
501 count=0;
502 for (i=0; i < (ssize_t) length; i+=count)
503 {
504#if !defined(MAGICKCORE_HAVE_PWRITE)
505 count=write(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
506 SSIZE_MAX));
507#else
508 count=pwrite(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
509 SSIZE_MAX),(off_t) (offset+i));
510#endif
511 if (count > 0)
512 continue;
513 count=0;
514 if (errno != EINTR)
515 {
516 i=(-1);
517 break;
518 }
519 }
520#if !defined(MAGICKCORE_HAVE_PWRITE)
521 }
522 }
523#endif
524 return(i);
525}
526
527static inline unsigned short GetRadonCell(const RadonInfo *radon_info,
cristybb503372010-05-27 20:51:26 +0000528 const ssize_t x,const ssize_t y)
cristy3ed852e2009-09-05 21:47:34 +0000529{
530 off_t
531 i;
532
533 unsigned short
534 value;
535
536 i=(off_t) radon_info->height*x+y;
537 if ((i < 0) ||
538 ((MagickSizeType) (i*sizeof(*radon_info->cells)) >= radon_info->length))
539 return(0);
540 if (radon_info->type != DiskCache)
541 return(radon_info->cells[i]);
542 value=0;
543 (void) ReadRadonCell(radon_info,i*sizeof(*radon_info->cells),
544 sizeof(*radon_info->cells),(unsigned char *) &value);
545 return(value);
546}
547
548static inline MagickBooleanType SetRadonCell(const RadonInfo *radon_info,
cristybb503372010-05-27 20:51:26 +0000549 const ssize_t x,const ssize_t y,const unsigned short value)
cristy3ed852e2009-09-05 21:47:34 +0000550{
551 off_t
552 i;
553
554 ssize_t
555 count;
556
557 i=(off_t) radon_info->height*x+y;
558 if ((i < 0) ||
559 ((MagickSizeType) (i*sizeof(*radon_info->cells)) >= radon_info->length))
560 return(MagickFalse);
561 if (radon_info->type != DiskCache)
562 {
563 radon_info->cells[i]=value;
564 return(MagickTrue);
565 }
566 count=WriteRadonCell(radon_info,i*sizeof(*radon_info->cells),
cristy44807bb2009-09-19 18:28:06 +0000567 sizeof(*radon_info->cells),(const unsigned char *) &value);
cristy3ed852e2009-09-05 21:47:34 +0000568 if (count != (ssize_t) sizeof(*radon_info->cells))
569 return(MagickFalse);
570 return(MagickTrue);
571}
572
573static void RadonProjection(RadonInfo *source_cells,
cristybb503372010-05-27 20:51:26 +0000574 RadonInfo *destination_cells,const ssize_t sign,size_t *projection)
cristy3ed852e2009-09-05 21:47:34 +0000575{
576 RadonInfo
577 *swap;
578
cristybb503372010-05-27 20:51:26 +0000579 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000580 x;
581
582 register RadonInfo
583 *p,
584 *q;
585
cristybb503372010-05-27 20:51:26 +0000586 size_t
cristy3ed852e2009-09-05 21:47:34 +0000587 step;
588
589 p=source_cells;
590 q=destination_cells;
591 for (step=1; step < p->width; step*=2)
592 {
cristybb503372010-05-27 20:51:26 +0000593 for (x=0; x < (ssize_t) p->width; x+=2*step)
cristy3ed852e2009-09-05 21:47:34 +0000594 {
cristybb503372010-05-27 20:51:26 +0000595 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000596 y;
597
cristybb503372010-05-27 20:51:26 +0000598 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000599 i;
600
601 unsigned short
602 cell;
603
cristybb503372010-05-27 20:51:26 +0000604 for (i=0; i < (ssize_t) step; i++)
cristy3ed852e2009-09-05 21:47:34 +0000605 {
cristybb503372010-05-27 20:51:26 +0000606 for (y=0; y < (ssize_t) (p->height-i-1); y++)
cristy3ed852e2009-09-05 21:47:34 +0000607 {
608 cell=GetRadonCell(p,x+i,y);
609 (void) SetRadonCell(q,x+2*i,y,cell+GetRadonCell(p,x+i+step,y+i));
610 (void) SetRadonCell(q,x+2*i+1,y,cell+GetRadonCell(p,x+i+step,y+i+1));
611 }
cristybb503372010-05-27 20:51:26 +0000612 for ( ; y < (ssize_t) (p->height-i); y++)
cristy3ed852e2009-09-05 21:47:34 +0000613 {
614 cell=GetRadonCell(p,x+i,y);
615 (void) SetRadonCell(q,x+2*i,y,cell+GetRadonCell(p,x+i+step,y+i));
616 (void) SetRadonCell(q,x+2*i+1,y,cell);
617 }
cristybb503372010-05-27 20:51:26 +0000618 for ( ; y < (ssize_t) p->height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000619 {
620 cell=GetRadonCell(p,x+i,y);
621 (void) SetRadonCell(q,x+2*i,y,cell);
622 (void) SetRadonCell(q,x+2*i+1,y,cell);
623 }
624 }
625 }
626 swap=p;
627 p=q;
628 q=swap;
629 }
cristyb5d5f722009-11-04 03:03:49 +0000630#if defined(MAGICKCORE_OPENMP_SUPPORT)
631 #pragma omp parallel for schedule(dynamic,4)
cristy3ed852e2009-09-05 21:47:34 +0000632#endif
cristybb503372010-05-27 20:51:26 +0000633 for (x=0; x < (ssize_t) p->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000634 {
cristybb503372010-05-27 20:51:26 +0000635 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000636 y;
637
cristybb503372010-05-27 20:51:26 +0000638 size_t
cristy3ed852e2009-09-05 21:47:34 +0000639 sum;
640
641 sum=0;
cristybb503372010-05-27 20:51:26 +0000642 for (y=0; y < (ssize_t) (p->height-1); y++)
cristy3ed852e2009-09-05 21:47:34 +0000643 {
cristybb503372010-05-27 20:51:26 +0000644 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000645 delta;
646
cristybb503372010-05-27 20:51:26 +0000647 delta=GetRadonCell(p,x,y)-(ssize_t) GetRadonCell(p,x,y+1);
cristy3ed852e2009-09-05 21:47:34 +0000648 sum+=delta*delta;
649 }
650 projection[p->width+sign*x-1]=sum;
651 }
652}
653
654static MagickBooleanType RadonTransform(const Image *image,
cristybb503372010-05-27 20:51:26 +0000655 const double threshold,size_t *projection,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000656{
657 CacheView
658 *image_view;
659
cristybb503372010-05-27 20:51:26 +0000660 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000661 y;
662
663 MagickBooleanType
664 status;
665
666 RadonInfo
667 *destination_cells,
668 *source_cells;
669
cristybb503372010-05-27 20:51:26 +0000670 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000671 i;
672
673 unsigned char
674 byte;
675
cristybb503372010-05-27 20:51:26 +0000676 size_t
cristy3ed852e2009-09-05 21:47:34 +0000677 count,
678 width;
679
680 unsigned short
681 bits[256];
682
683 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
684 source_cells=AcquireRadonInfo(image,width,image->rows,exception);
685 destination_cells=AcquireRadonInfo(image,width,image->rows,exception);
686 if ((source_cells == (RadonInfo *) NULL) ||
687 (destination_cells == (RadonInfo *) NULL))
688 {
689 if (destination_cells != (RadonInfo *) NULL)
690 destination_cells=DestroyRadonInfo(destination_cells);
691 if (source_cells != (RadonInfo *) NULL)
692 source_cells=DestroyRadonInfo(source_cells);
693 return(MagickFalse);
694 }
695 if (ResetRadonCells(source_cells) == MagickFalse)
696 {
697 destination_cells=DestroyRadonInfo(destination_cells);
698 source_cells=DestroyRadonInfo(source_cells);
699 return(MagickFalse);
700 }
701 for (i=0; i < 256; i++)
702 {
703 byte=(unsigned char) i;
704 for (count=0; byte != 0; byte>>=1)
705 count+=byte & 0x01;
706 bits[i]=(unsigned short) count;
707 }
708 status=MagickTrue;
709 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +0000710#if defined(MAGICKCORE_OPENMP_SUPPORT)
711 #pragma omp parallel for schedule(dynamic,4) shared(status)
cristy3ed852e2009-09-05 21:47:34 +0000712#endif
cristybb503372010-05-27 20:51:26 +0000713 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000714 {
715 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +0000716 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000717
cristybb503372010-05-27 20:51:26 +0000718 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000719 i,
720 x;
721
cristybb503372010-05-27 20:51:26 +0000722 size_t
cristy3ed852e2009-09-05 21:47:34 +0000723 bit,
724 byte;
725
726 if (status == MagickFalse)
727 continue;
728 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
729 if (p == (const PixelPacket *) NULL)
730 {
731 status=MagickFalse;
732 continue;
733 }
734 bit=0;
735 byte=0;
cristybb503372010-05-27 20:51:26 +0000736 i=(ssize_t) (image->columns+7)/8;
737 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000738 {
739 byte<<=1;
740 if (((MagickRealType) p->red < threshold) ||
741 ((MagickRealType) p->green < threshold) ||
742 ((MagickRealType) p->blue < threshold))
743 byte|=0x01;
744 bit++;
745 if (bit == 8)
746 {
747 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
748 bit=0;
749 byte=0;
750 }
751 p++;
752 }
753 if (bit != 0)
754 {
755 byte<<=(8-bit);
756 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
757 }
758 }
759 RadonProjection(source_cells,destination_cells,-1,projection);
760 (void) ResetRadonCells(source_cells);
cristyb5d5f722009-11-04 03:03:49 +0000761#if defined(MAGICKCORE_OPENMP_SUPPORT)
762 #pragma omp parallel for schedule(dynamic,4) shared(status)
cristy3ed852e2009-09-05 21:47:34 +0000763#endif
cristybb503372010-05-27 20:51:26 +0000764 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000765 {
766 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +0000767 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000768
cristybb503372010-05-27 20:51:26 +0000769 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000770 i,
771 x;
772
cristybb503372010-05-27 20:51:26 +0000773 size_t
cristy3ed852e2009-09-05 21:47:34 +0000774 bit,
775 byte;
776
777 if (status == MagickFalse)
778 continue;
779 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
780 if (p == (const PixelPacket *) NULL)
781 {
782 status=MagickFalse;
783 continue;
784 }
785 bit=0;
786 byte=0;
787 i=0;
cristybb503372010-05-27 20:51:26 +0000788 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000789 {
790 byte<<=1;
791 if (((MagickRealType) p->red < threshold) ||
792 ((MagickRealType) p->green < threshold) ||
793 ((MagickRealType) p->blue < threshold))
794 byte|=0x01;
795 bit++;
796 if (bit == 8)
797 {
798 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
799 bit=0;
800 byte=0;
801 }
802 p++;
803 }
804 if (bit != 0)
805 {
806 byte<<=(8-bit);
807 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
808 }
809 }
810 RadonProjection(source_cells,destination_cells,1,projection);
811 image_view=DestroyCacheView(image_view);
812 destination_cells=DestroyRadonInfo(destination_cells);
813 source_cells=DestroyRadonInfo(source_cells);
814 return(MagickTrue);
815}
816
cristybb503372010-05-27 20:51:26 +0000817static void GetImageBackgroundColor(Image *image,const ssize_t offset,
cristy3ed852e2009-09-05 21:47:34 +0000818 ExceptionInfo *exception)
819{
820 CacheView
821 *image_view;
822
cristybb503372010-05-27 20:51:26 +0000823 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000824 y;
825
826 MagickPixelPacket
827 background;
828
829 MagickRealType
830 count;
831
832 /*
833 Compute average background color.
834 */
835 if (offset <= 0)
836 return;
837 GetMagickPixelPacket(image,&background);
838 count=0.0;
839 image_view=AcquireCacheView(image);
cristybb503372010-05-27 20:51:26 +0000840 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000841 {
842 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +0000843 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000844
cristybb503372010-05-27 20:51:26 +0000845 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000846 x;
847
cristybb503372010-05-27 20:51:26 +0000848 if ((y >= offset) && (y < ((ssize_t) image->rows-offset)))
cristy3ed852e2009-09-05 21:47:34 +0000849 continue;
850 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
851 if (p == (const PixelPacket *) NULL)
852 continue;
cristybb503372010-05-27 20:51:26 +0000853 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000854 {
cristybb503372010-05-27 20:51:26 +0000855 if ((x >= offset) && (x < ((ssize_t) image->columns-offset)))
cristy3ed852e2009-09-05 21:47:34 +0000856 continue;
cristyce70c172010-01-07 17:15:30 +0000857 background.red+=QuantumScale*GetRedPixelComponent(p);
858 background.green+=QuantumScale*GetGreenPixelComponent(p);
859 background.blue+=QuantumScale*GetBluePixelComponent(p);
860 background.opacity+=QuantumScale*GetOpacityPixelComponent(p);
cristy3ed852e2009-09-05 21:47:34 +0000861 count++;
862 p++;
863 }
864 }
865 image_view=DestroyCacheView(image_view);
cristyce70c172010-01-07 17:15:30 +0000866 image->background_color.red=ClampToQuantum((MagickRealType) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +0000867 background.red/count);
cristyce70c172010-01-07 17:15:30 +0000868 image->background_color.green=ClampToQuantum((MagickRealType) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +0000869 background.green/count);
cristyce70c172010-01-07 17:15:30 +0000870 image->background_color.blue=ClampToQuantum((MagickRealType) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +0000871 background.blue/count);
cristyce70c172010-01-07 17:15:30 +0000872 image->background_color.opacity=ClampToQuantum((MagickRealType) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +0000873 background.opacity/count);
874}
875
876MagickExport Image *DeskewImage(const Image *image,const double threshold,
877 ExceptionInfo *exception)
878{
879 AffineMatrix
880 affine_matrix;
881
882 const char
883 *artifact;
884
885 double
886 degrees;
887
888 Image
889 *clone_image,
890 *crop_image,
891 *deskew_image,
892 *median_image;
893
cristybb503372010-05-27 20:51:26 +0000894 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000895 skew;
896
897 MagickBooleanType
898 status;
899
900 RectangleInfo
901 geometry;
902
cristybb503372010-05-27 20:51:26 +0000903 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000904 i;
905
cristybb503372010-05-27 20:51:26 +0000906 size_t
cristy3ed852e2009-09-05 21:47:34 +0000907 max_projection,
908 *projection,
909 width;
910
911 /*
912 Compute deskew angle.
913 */
914 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
cristybb503372010-05-27 20:51:26 +0000915 projection=(size_t *) AcquireQuantumMemory((size_t) (2*width-1),
cristy3ed852e2009-09-05 21:47:34 +0000916 sizeof(*projection));
cristybb503372010-05-27 20:51:26 +0000917 if (projection == (size_t *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000918 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
919 status=RadonTransform(image,threshold,projection,exception);
920 if (status == MagickFalse)
921 {
cristybb503372010-05-27 20:51:26 +0000922 projection=(size_t *) RelinquishMagickMemory(projection);
cristy3ed852e2009-09-05 21:47:34 +0000923 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
924 }
925 max_projection=0;
926 skew=0;
cristybb503372010-05-27 20:51:26 +0000927 for (i=0; i < (ssize_t) (2*width-1); i++)
cristy3ed852e2009-09-05 21:47:34 +0000928 {
929 if (projection[i] > max_projection)
930 {
cristybb503372010-05-27 20:51:26 +0000931 skew=i-(ssize_t) width+1;
cristy3ed852e2009-09-05 21:47:34 +0000932 max_projection=projection[i];
933 }
934 }
cristybb503372010-05-27 20:51:26 +0000935 projection=(size_t *) RelinquishMagickMemory(projection);
cristy3ed852e2009-09-05 21:47:34 +0000936 /*
937 Deskew image.
938 */
939 clone_image=CloneImage(image,0,0,MagickTrue,exception);
940 if (clone_image == (Image *) NULL)
941 return((Image *) NULL);
942 (void) SetImageVirtualPixelMethod(clone_image,BackgroundVirtualPixelMethod);
943 degrees=RadiansToDegrees(-atan((double) skew/width/8));
944 if (image->debug != MagickFalse)
cristy8cd5b312010-01-07 01:10:24 +0000945 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye7f51092010-01-17 00:39:37 +0000946 " Deskew angle: %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +0000947 affine_matrix.sx=cos(DegreesToRadians(fmod((double) degrees,360.0)));
948 affine_matrix.rx=sin(DegreesToRadians(fmod((double) degrees,360.0)));
949 affine_matrix.ry=(-sin(DegreesToRadians(fmod((double) degrees,360.0))));
950 affine_matrix.sy=cos(DegreesToRadians(fmod((double) degrees,360.0)));
951 affine_matrix.tx=0.0;
952 affine_matrix.ty=0.0;
953 artifact=GetImageArtifact(image,"deskew:auto-crop");
954 if (artifact == (const char *) NULL)
955 {
956 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
957 clone_image=DestroyImage(clone_image);
958 return(deskew_image);
959 }
960 /*
961 Auto-crop image.
962 */
cristyf2f27272009-12-17 14:48:46 +0000963 GetImageBackgroundColor(clone_image,StringToLong(artifact),exception);
cristy3ed852e2009-09-05 21:47:34 +0000964 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
965 clone_image=DestroyImage(clone_image);
966 if (deskew_image == (Image *) NULL)
967 return((Image *) NULL);
968 median_image=MedianFilterImage(deskew_image,0.0,exception);
969 if (median_image == (Image *) NULL)
970 {
971 deskew_image=DestroyImage(deskew_image);
972 return((Image *) NULL);
973 }
974 geometry=GetImageBoundingBox(median_image,exception);
975 median_image=DestroyImage(median_image);
976 if (image->debug != MagickFalse)
977 (void) LogMagickEvent(TransformEvent,GetMagickModule()," Deskew geometry: "
978 "%lux%lu%+ld%+ld",geometry.width,geometry.height,geometry.x,geometry.y);
979 crop_image=CropImage(deskew_image,&geometry,exception);
980 deskew_image=DestroyImage(deskew_image);
981 return(crop_image);
982}
983
984/*
985%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
986% %
987% %
988% %
989+ I n t e g r a l R o t a t e I m a g e %
990% %
991% %
992% %
993%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
994%
cristy3dfa16c2010-05-17 19:34:48 +0000995% IntegralRotateImage() rotates the image an integral of 90 degrees. It
cristy3ed852e2009-09-05 21:47:34 +0000996% allocates the memory necessary for the new Image structure and returns a
997% pointer to the rotated image.
998%
999% The format of the IntegralRotateImage method is:
1000%
cristybb503372010-05-27 20:51:26 +00001001% Image *IntegralRotateImage(const Image *image,size_t rotations,
cristy3ed852e2009-09-05 21:47:34 +00001002% ExceptionInfo *exception)
1003%
1004% A description of each parameter follows.
1005%
1006% o image: the image.
1007%
1008% o rotations: Specifies the number of 90 degree rotations.
1009%
1010*/
cristybb503372010-05-27 20:51:26 +00001011static Image *IntegralRotateImage(const Image *image,size_t rotations,
cristy3ed852e2009-09-05 21:47:34 +00001012 ExceptionInfo *exception)
1013{
cristy3ed852e2009-09-05 21:47:34 +00001014#define RotateImageTag "Rotate/Image"
1015
1016 CacheView
1017 *image_view,
1018 *rotate_view;
1019
1020 Image
1021 *rotate_image;
1022
cristy3ed852e2009-09-05 21:47:34 +00001023 MagickBooleanType
1024 status;
1025
cristy5f959472010-05-27 22:19:46 +00001026 MagickOffsetType
1027 progress;
1028
cristy3ed852e2009-09-05 21:47:34 +00001029 RectangleInfo
1030 page;
1031
cristy5f959472010-05-27 22:19:46 +00001032 ssize_t
1033 y;
1034
cristy3ed852e2009-09-05 21:47:34 +00001035 /*
1036 Initialize rotated image attributes.
1037 */
1038 assert(image != (Image *) NULL);
1039 page=image->page;
1040 rotations%=4;
cristyb32b90a2009-09-07 21:45:48 +00001041 if (rotations == 0)
1042 return(CloneImage(image,0,0,MagickTrue,exception));
cristy3ed852e2009-09-05 21:47:34 +00001043 if ((rotations == 1) || (rotations == 3))
1044 rotate_image=CloneImage(image,image->rows,image->columns,MagickTrue,
1045 exception);
1046 else
1047 rotate_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1048 exception);
1049 if (rotate_image == (Image *) NULL)
1050 return((Image *) NULL);
1051 /*
1052 Integral rotate the image.
1053 */
1054 status=MagickTrue;
1055 progress=0;
1056 image_view=AcquireCacheView(image);
1057 rotate_view=AcquireCacheView(rotate_image);
1058 switch (rotations)
1059 {
1060 case 0:
1061 {
1062 /*
1063 Rotate 0 degrees.
1064 */
cristy3ed852e2009-09-05 21:47:34 +00001065 break;
1066 }
1067 case 1:
1068 {
cristybb503372010-05-27 20:51:26 +00001069 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001070 tile_y;
1071
cristybb503372010-05-27 20:51:26 +00001072 size_t
cristyb32b90a2009-09-07 21:45:48 +00001073 tile_height,
1074 tile_width;
1075
cristy3ed852e2009-09-05 21:47:34 +00001076 /*
1077 Rotate 90 degrees.
1078 */
cristyb32b90a2009-09-07 21:45:48 +00001079 GetPixelCacheTileSize(image,&tile_width,&tile_height);
cristyd3dd0502010-04-26 00:57:20 +00001080#if defined(MAGICKCORE_OPENMP_SUPPORT) && defined(MAGICKCORE_FUTURE)
cristy8a7ea362010-03-10 20:31:43 +00001081 #pragma omp parallel for schedule(dynamic,4) shared(progress, status)
cristydaa97692009-09-13 02:10:35 +00001082#endif
cristybb503372010-05-27 20:51:26 +00001083 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=tile_height)
cristy3ed852e2009-09-05 21:47:34 +00001084 {
cristybb503372010-05-27 20:51:26 +00001085 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001086 tile_x;
1087
1088 if (status == MagickFalse)
1089 continue;
cristybb503372010-05-27 20:51:26 +00001090 for (tile_x=0; tile_x < (ssize_t) image->columns; tile_x+=tile_width)
cristy3ed852e2009-09-05 21:47:34 +00001091 {
1092 MagickBooleanType
1093 sync;
1094
1095 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001096 *restrict indexes;
cristy3ed852e2009-09-05 21:47:34 +00001097
1098 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001099 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001100
1101 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001102 *restrict rotate_indexes;
cristy3ed852e2009-09-05 21:47:34 +00001103
cristybb503372010-05-27 20:51:26 +00001104 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001105 y;
1106
1107 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001108 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001109
cristybb503372010-05-27 20:51:26 +00001110 size_t
cristyb32b90a2009-09-07 21:45:48 +00001111 height,
1112 width;
cristy3ed852e2009-09-05 21:47:34 +00001113
cristyb32b90a2009-09-07 21:45:48 +00001114 width=tile_width;
cristybb503372010-05-27 20:51:26 +00001115 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
1116 width=(size_t) (tile_width-(tile_x+tile_width-
cristy3ed852e2009-09-05 21:47:34 +00001117 image->columns));
cristyb32b90a2009-09-07 21:45:48 +00001118 height=tile_height;
cristybb503372010-05-27 20:51:26 +00001119 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
1120 height=(size_t) (tile_height-(tile_y+tile_height-
cristy3ed852e2009-09-05 21:47:34 +00001121 image->rows));
cristydaa97692009-09-13 02:10:35 +00001122 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1123 exception);
cristy3ed852e2009-09-05 21:47:34 +00001124 if (p == (const PixelPacket *) NULL)
1125 {
1126 status=MagickFalse;
1127 break;
1128 }
1129 indexes=GetCacheViewVirtualIndexQueue(image_view);
cristybb503372010-05-27 20:51:26 +00001130 for (y=0; y < (ssize_t) width; y++)
cristy3ed852e2009-09-05 21:47:34 +00001131 {
1132 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001133 *restrict tile_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001134
cristybb503372010-05-27 20:51:26 +00001135 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001136 x;
1137
cristybb503372010-05-27 20:51:26 +00001138 q=QueueCacheViewAuthenticPixels(rotate_view,(ssize_t)
cristyb32b90a2009-09-07 21:45:48 +00001139 rotate_image->columns-(tile_y+height),y+tile_x,height,
cristy3ed852e2009-09-05 21:47:34 +00001140 1,exception);
1141 if (q == (PixelPacket *) NULL)
1142 {
1143 status=MagickFalse;
1144 break;
1145 }
cristyb32b90a2009-09-07 21:45:48 +00001146 tile_pixels=p+(height-1)*width+y;
cristybb503372010-05-27 20:51:26 +00001147 for (x=0; x < (ssize_t) height; x++)
cristy3ed852e2009-09-05 21:47:34 +00001148 {
1149 *q++=(*tile_pixels);
cristyb32b90a2009-09-07 21:45:48 +00001150 tile_pixels-=width;
cristy3ed852e2009-09-05 21:47:34 +00001151 }
1152 rotate_indexes=GetCacheViewAuthenticIndexQueue(rotate_view);
1153 if ((indexes != (IndexPacket *) NULL) &&
1154 (rotate_indexes != (IndexPacket *) NULL))
1155 {
1156 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001157 *restrict tile_indexes;
cristy3ed852e2009-09-05 21:47:34 +00001158
cristyb32b90a2009-09-07 21:45:48 +00001159 tile_indexes=indexes+(height-1)*width+y;
cristybb503372010-05-27 20:51:26 +00001160 for (x=0; x < (ssize_t) height; x++)
cristy3ed852e2009-09-05 21:47:34 +00001161 {
1162 *rotate_indexes++=(*tile_indexes);
cristyb32b90a2009-09-07 21:45:48 +00001163 tile_indexes-=width;
cristy3ed852e2009-09-05 21:47:34 +00001164 }
1165 }
1166 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1167 if (sync == MagickFalse)
1168 status=MagickFalse;
1169 }
1170 }
1171 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1172 {
1173 MagickBooleanType
1174 proceed;
1175
cristyb32b90a2009-09-07 21:45:48 +00001176 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
cristy3ed852e2009-09-05 21:47:34 +00001177 image->rows);
1178 if (proceed == MagickFalse)
1179 status=MagickFalse;
1180 }
1181 }
cristyecc2c142010-01-17 22:25:46 +00001182 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1183 image->rows-1,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001184 Swap(page.width,page.height);
1185 Swap(page.x,page.y);
1186 if (page.width != 0)
cristybb503372010-05-27 20:51:26 +00001187 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
cristy3ed852e2009-09-05 21:47:34 +00001188 break;
1189 }
1190 case 2:
1191 {
1192 /*
1193 Rotate 180 degrees.
1194 */
cristyd3dd0502010-04-26 00:57:20 +00001195#if defined(MAGICKCORE_OPENMP_SUPPORT) && defined(MAGICKCORE_FUTURE)
cristy5f959472010-05-27 22:19:46 +00001196 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristydaa97692009-09-13 02:10:35 +00001197#endif
cristybb503372010-05-27 20:51:26 +00001198 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001199 {
1200 MagickBooleanType
1201 sync;
1202
1203 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001204 *restrict indexes;
cristy3ed852e2009-09-05 21:47:34 +00001205
1206 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001207 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001208
1209 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001210 *restrict rotate_indexes;
cristy3ed852e2009-09-05 21:47:34 +00001211
cristybb503372010-05-27 20:51:26 +00001212 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001213 x;
1214
1215 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001216 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001217
1218 if (status == MagickFalse)
1219 continue;
1220 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,
1221 exception);
cristybb503372010-05-27 20:51:26 +00001222 q=QueueCacheViewAuthenticPixels(rotate_view,0,(ssize_t) (image->rows-
cristy3ed852e2009-09-05 21:47:34 +00001223 y-1),image->columns,1,exception);
1224 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
1225 {
1226 status=MagickFalse;
1227 continue;
1228 }
1229 indexes=GetCacheViewVirtualIndexQueue(image_view);
1230 rotate_indexes=GetCacheViewAuthenticIndexQueue(rotate_view);
1231 q+=image->columns;
cristybb503372010-05-27 20:51:26 +00001232 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001233 *--q=(*p++);
1234 if ((indexes != (IndexPacket *) NULL) &&
1235 (rotate_indexes != (IndexPacket *) NULL))
cristybb503372010-05-27 20:51:26 +00001236 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001237 rotate_indexes[image->columns-x-1]=indexes[x];
1238 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1239 if (sync == MagickFalse)
1240 status=MagickFalse;
1241 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1242 {
1243 MagickBooleanType
1244 proceed;
1245
1246 proceed=SetImageProgress(image,RotateImageTag,progress++,
1247 image->rows);
1248 if (proceed == MagickFalse)
1249 status=MagickFalse;
1250 }
1251 }
1252 if (page.width != 0)
cristybb503372010-05-27 20:51:26 +00001253 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
cristy3ed852e2009-09-05 21:47:34 +00001254 if (page.height != 0)
cristybb503372010-05-27 20:51:26 +00001255 page.y=(ssize_t) (page.height-rotate_image->rows-page.y);
cristy3ed852e2009-09-05 21:47:34 +00001256 break;
1257 }
1258 case 3:
1259 {
cristybb503372010-05-27 20:51:26 +00001260 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001261 tile_y;
1262
cristybb503372010-05-27 20:51:26 +00001263 size_t
cristyb32b90a2009-09-07 21:45:48 +00001264 tile_height,
1265 tile_width;
1266
cristy3ed852e2009-09-05 21:47:34 +00001267 /*
1268 Rotate 270 degrees.
1269 */
cristyb32b90a2009-09-07 21:45:48 +00001270 GetPixelCacheTileSize(image,&tile_width,&tile_height);
cristyd3dd0502010-04-26 00:57:20 +00001271#if defined(MAGICKCORE_OPENMP_SUPPORT) && defined(MAGICKCORE_FUTURE)
cristy5f959472010-05-27 22:19:46 +00001272 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristydaa97692009-09-13 02:10:35 +00001273#endif
cristybb503372010-05-27 20:51:26 +00001274 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=tile_height)
cristy3ed852e2009-09-05 21:47:34 +00001275 {
cristybb503372010-05-27 20:51:26 +00001276 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001277 tile_x;
1278
1279 if (status == MagickFalse)
1280 continue;
cristybb503372010-05-27 20:51:26 +00001281 for (tile_x=0; tile_x < (ssize_t) image->columns; tile_x+=tile_width)
cristy3ed852e2009-09-05 21:47:34 +00001282 {
1283 MagickBooleanType
1284 sync;
1285
1286 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001287 *restrict indexes;
cristy3ed852e2009-09-05 21:47:34 +00001288
1289 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001290 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001291
1292 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001293 *restrict rotate_indexes;
cristy3ed852e2009-09-05 21:47:34 +00001294
cristybb503372010-05-27 20:51:26 +00001295 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001296 y;
1297
1298 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001299 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001300
cristybb503372010-05-27 20:51:26 +00001301 size_t
cristyb32b90a2009-09-07 21:45:48 +00001302 height,
1303 width;
cristy3ed852e2009-09-05 21:47:34 +00001304
cristyb32b90a2009-09-07 21:45:48 +00001305 width=tile_width;
cristybb503372010-05-27 20:51:26 +00001306 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
1307 width=(size_t) (tile_width-(tile_x+tile_width-
cristy3ed852e2009-09-05 21:47:34 +00001308 image->columns));
cristyb32b90a2009-09-07 21:45:48 +00001309 height=tile_height;
cristybb503372010-05-27 20:51:26 +00001310 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
1311 height=(size_t) (tile_height-(tile_y+tile_height-
cristy3ed852e2009-09-05 21:47:34 +00001312 image->rows));
cristyb32b90a2009-09-07 21:45:48 +00001313 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,
1314 height,exception);
cristy3ed852e2009-09-05 21:47:34 +00001315 if (p == (const PixelPacket *) NULL)
1316 {
1317 status=MagickFalse;
1318 break;
1319 }
1320 indexes=GetCacheViewVirtualIndexQueue(image_view);
cristybb503372010-05-27 20:51:26 +00001321 for (y=0; y < (ssize_t) width; y++)
cristy3ed852e2009-09-05 21:47:34 +00001322 {
1323 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001324 *restrict tile_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001325
cristybb503372010-05-27 20:51:26 +00001326 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001327 x;
1328
cristybb503372010-05-27 20:51:26 +00001329 q=QueueCacheViewAuthenticPixels(rotate_view,tile_y,(ssize_t)
cristyb32b90a2009-09-07 21:45:48 +00001330 y+rotate_image->rows-(tile_x+width),height,1,exception);
cristy3ed852e2009-09-05 21:47:34 +00001331 if (q == (PixelPacket *) NULL)
1332 {
1333 status=MagickFalse;
1334 break;
1335 }
cristyb32b90a2009-09-07 21:45:48 +00001336 tile_pixels=p+(width-1)-y;
cristybb503372010-05-27 20:51:26 +00001337 for (x=0; x < (ssize_t) height; x++)
cristy3ed852e2009-09-05 21:47:34 +00001338 {
1339 *q++=(*tile_pixels);
cristyb32b90a2009-09-07 21:45:48 +00001340 tile_pixels+=width;
cristy3ed852e2009-09-05 21:47:34 +00001341 }
1342 rotate_indexes=GetCacheViewAuthenticIndexQueue(rotate_view);
1343 if ((indexes != (IndexPacket *) NULL) &&
1344 (rotate_indexes != (IndexPacket *) NULL))
1345 {
1346 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001347 *restrict tile_indexes;
cristy3ed852e2009-09-05 21:47:34 +00001348
cristyb32b90a2009-09-07 21:45:48 +00001349 tile_indexes=indexes+(width-1)-y;
cristybb503372010-05-27 20:51:26 +00001350 for (x=0; x < (ssize_t) height; x++)
cristy3ed852e2009-09-05 21:47:34 +00001351 {
1352 *rotate_indexes++=(*tile_indexes);
cristyb32b90a2009-09-07 21:45:48 +00001353 tile_indexes+=width;
cristy3ed852e2009-09-05 21:47:34 +00001354 }
1355 }
1356 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1357 if (sync == MagickFalse)
1358 status=MagickFalse;
1359 }
1360 }
1361 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1362 {
1363 MagickBooleanType
1364 proceed;
1365
cristyb32b90a2009-09-07 21:45:48 +00001366 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
cristy3ed852e2009-09-05 21:47:34 +00001367 image->rows);
1368 if (proceed == MagickFalse)
1369 status=MagickFalse;
1370 }
1371 }
cristyecc2c142010-01-17 22:25:46 +00001372 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1373 image->rows-1,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001374 Swap(page.width,page.height);
1375 Swap(page.x,page.y);
1376 if (page.height != 0)
cristybb503372010-05-27 20:51:26 +00001377 page.y=(ssize_t) (page.height-rotate_image->rows-page.y);
cristy3ed852e2009-09-05 21:47:34 +00001378 break;
1379 }
1380 }
1381 rotate_view=DestroyCacheView(rotate_view);
1382 image_view=DestroyCacheView(image_view);
1383 rotate_image->type=image->type;
1384 rotate_image->page=page;
1385 if (status == MagickFalse)
1386 rotate_image=DestroyImage(rotate_image);
1387 return(rotate_image);
1388}
1389
1390/*
1391%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1392% %
1393% %
1394% %
1395+ X S h e a r I m a g e %
1396% %
1397% %
1398% %
1399%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1400%
1401% XShearImage() shears the image in the X direction with a shear angle of
1402% 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1403% negative angles shear clockwise. Angles are measured relative to a vertical
1404% Y-axis. X shears will widen an image creating 'empty' triangles on the left
1405% and right sides of the source image.
1406%
1407% The format of the XShearImage method is:
1408%
1409% MagickBooleanType XShearImage(Image *image,const MagickRealType degrees,
cristybb503372010-05-27 20:51:26 +00001410% const size_t width,const size_t height,
1411% const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001412%
1413% A description of each parameter follows.
1414%
1415% o image: the image.
1416%
cristycee97112010-05-28 00:44:52 +00001417% o degrees: A MagickRealType representing the shearing angle along the X
cristy3ed852e2009-09-05 21:47:34 +00001418% axis.
1419%
1420% o width, height, x_offset, y_offset: Defines a region of the image
1421% to shear.
1422%
cristyecc2c142010-01-17 22:25:46 +00001423% o exception: return any errors or warnings in this structure.
1424%
cristy3ed852e2009-09-05 21:47:34 +00001425*/
1426static MagickBooleanType XShearImage(Image *image,const MagickRealType degrees,
cristybb503372010-05-27 20:51:26 +00001427 const size_t width,const size_t height,const ssize_t x_offset,
1428 const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001429{
1430#define XShearImageTag "XShear/Image"
1431
1432 typedef enum
1433 {
1434 LEFT,
1435 RIGHT
1436 } ShearDirection;
1437
1438 CacheView
1439 *image_view;
1440
cristy3ed852e2009-09-05 21:47:34 +00001441 MagickBooleanType
1442 status;
1443
cristy5f959472010-05-27 22:19:46 +00001444 MagickOffsetType
1445 progress;
1446
cristy3ed852e2009-09-05 21:47:34 +00001447 MagickPixelPacket
1448 background;
1449
cristy5f959472010-05-27 22:19:46 +00001450 ssize_t
1451 y;
1452
cristy3ed852e2009-09-05 21:47:34 +00001453 assert(image != (Image *) NULL);
1454 assert(image->signature == MagickSignature);
1455 if (image->debug != MagickFalse)
1456 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1457 GetMagickPixelPacket(image,&background);
1458 SetMagickPixelPacket(image,&image->background_color,(IndexPacket *) NULL,
1459 &background);
1460 if (image->colorspace == CMYKColorspace)
1461 ConvertRGBToCMYK(&background);
1462 /*
1463 XShear image.
1464 */
1465 status=MagickTrue;
1466 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00001467 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00001468#if defined(MAGICKCORE_OPENMP_SUPPORT)
1469 #pragma omp parallel for schedule(dynamic,4) shared(progress, status)
cristy3ed852e2009-09-05 21:47:34 +00001470#endif
cristybb503372010-05-27 20:51:26 +00001471 for (y=0; y < (ssize_t) height; y++)
cristy3ed852e2009-09-05 21:47:34 +00001472 {
cristybb503372010-05-27 20:51:26 +00001473 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001474 step;
1475
1476 MagickPixelPacket
1477 pixel,
1478 source,
1479 destination;
1480
1481 MagickRealType
1482 area,
1483 displacement;
1484
cristybb503372010-05-27 20:51:26 +00001485 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001486 i;
1487
1488 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001489 *restrict indexes,
1490 *restrict shear_indexes;
cristy3ed852e2009-09-05 21:47:34 +00001491
1492 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001493 *restrict p,
1494 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001495
1496 ShearDirection
1497 direction;
1498
1499 if (status == MagickFalse)
1500 continue;
1501 p=GetCacheViewAuthenticPixels(image_view,0,y_offset+y,image->columns,1,
1502 exception);
1503 if (p == (PixelPacket *) NULL)
1504 {
1505 status=MagickFalse;
1506 continue;
1507 }
1508 indexes=GetCacheViewAuthenticIndexQueue(image_view);
1509 p+=x_offset;
1510 indexes+=x_offset;
1511 displacement=degrees*(MagickRealType) (y-height/2.0);
1512 if (displacement == 0.0)
1513 continue;
1514 if (displacement > 0.0)
1515 direction=RIGHT;
1516 else
1517 {
1518 displacement*=(-1.0);
1519 direction=LEFT;
1520 }
cristybb503372010-05-27 20:51:26 +00001521 step=(ssize_t) floor((double) displacement);
cristy3ed852e2009-09-05 21:47:34 +00001522 area=(MagickRealType) (displacement-step);
1523 step++;
1524 pixel=background;
1525 GetMagickPixelPacket(image,&source);
1526 GetMagickPixelPacket(image,&destination);
1527 switch (direction)
1528 {
1529 case LEFT:
1530 {
1531 /*
1532 Transfer pixels left-to-right.
1533 */
1534 if (step > x_offset)
1535 break;
1536 q=p-step;
1537 shear_indexes=indexes-step;
cristybb503372010-05-27 20:51:26 +00001538 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001539 {
1540 if ((x_offset+i) < step)
1541 {
1542 SetMagickPixelPacket(image,++p,++indexes,&pixel);
1543 q++;
1544 shear_indexes++;
1545 continue;
1546 }
1547 SetMagickPixelPacket(image,p,indexes,&source);
1548 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1549 &source,(MagickRealType) p->opacity,area,&destination);
1550 SetPixelPacket(image,&destination,q++,shear_indexes++);
1551 SetMagickPixelPacket(image,p++,indexes++,&pixel);
1552 }
1553 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1554 &background,(MagickRealType) background.opacity,area,&destination);
1555 SetPixelPacket(image,&destination,q++,shear_indexes++);
1556 for (i=0; i < (step-1); i++)
1557 SetPixelPacket(image,&background,q++,shear_indexes++);
1558 break;
1559 }
1560 case RIGHT:
1561 {
1562 /*
1563 Transfer pixels right-to-left.
1564 */
1565 p+=width;
1566 indexes+=width;
1567 q=p+step;
1568 shear_indexes=indexes+step;
cristybb503372010-05-27 20:51:26 +00001569 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001570 {
1571 p--;
1572 indexes--;
1573 q--;
1574 shear_indexes--;
cristybb503372010-05-27 20:51:26 +00001575 if ((size_t) (x_offset+width+step-i) >= image->columns)
cristy3ed852e2009-09-05 21:47:34 +00001576 continue;
1577 SetMagickPixelPacket(image,p,indexes,&source);
1578 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1579 &source,(MagickRealType) p->opacity,area,&destination);
1580 SetPixelPacket(image,&destination,q,shear_indexes);
1581 SetMagickPixelPacket(image,p,indexes,&pixel);
1582 }
1583 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1584 &background,(MagickRealType) background.opacity,area,&destination);
1585 SetPixelPacket(image,&destination,--q,--shear_indexes);
1586 for (i=0; i < (step-1); i++)
1587 SetPixelPacket(image,&background,--q,--shear_indexes);
1588 break;
1589 }
1590 }
1591 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1592 status=MagickFalse;
1593 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1594 {
1595 MagickBooleanType
1596 proceed;
1597
cristyb5d5f722009-11-04 03:03:49 +00001598#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00001599 #pragma omp critical (MagickCore_XShearImage)
1600#endif
1601 proceed=SetImageProgress(image,XShearImageTag,progress++,height);
1602 if (proceed == MagickFalse)
1603 status=MagickFalse;
1604 }
1605 }
1606 image_view=DestroyCacheView(image_view);
1607 return(status);
1608}
1609
1610/*
1611%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1612% %
1613% %
1614% %
1615+ Y S h e a r I m a g e %
1616% %
1617% %
1618% %
1619%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1620%
1621% YShearImage shears the image in the Y direction with a shear angle of
1622% 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1623% negative angles shear clockwise. Angles are measured relative to a
1624% horizontal X-axis. Y shears will increase the height of an image creating
1625% 'empty' triangles on the top and bottom of the source image.
1626%
1627% The format of the YShearImage method is:
1628%
1629% MagickBooleanType YShearImage(Image *image,const MagickRealType degrees,
cristybb503372010-05-27 20:51:26 +00001630% const size_t width,const size_t height,
1631% const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001632%
1633% A description of each parameter follows.
1634%
1635% o image: the image.
1636%
cristycee97112010-05-28 00:44:52 +00001637% o degrees: A MagickRealType representing the shearing angle along the Y
cristy3ed852e2009-09-05 21:47:34 +00001638% axis.
1639%
1640% o width, height, x_offset, y_offset: Defines a region of the image
1641% to shear.
1642%
cristyecc2c142010-01-17 22:25:46 +00001643% o exception: return any errors or warnings in this structure.
1644%
cristy3ed852e2009-09-05 21:47:34 +00001645*/
1646static MagickBooleanType YShearImage(Image *image,const MagickRealType degrees,
cristybb503372010-05-27 20:51:26 +00001647 const size_t width,const size_t height,const ssize_t x_offset,
1648 const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001649{
1650#define YShearImageTag "YShear/Image"
1651
1652 typedef enum
1653 {
1654 UP,
1655 DOWN
1656 } ShearDirection;
1657
1658 CacheView
1659 *image_view;
1660
cristy3ed852e2009-09-05 21:47:34 +00001661 MagickBooleanType
1662 status;
1663
cristy5f959472010-05-27 22:19:46 +00001664 MagickOffsetType
1665 progress;
1666
cristy3ed852e2009-09-05 21:47:34 +00001667 MagickPixelPacket
1668 background;
1669
cristy5f959472010-05-27 22:19:46 +00001670 ssize_t
1671 x;
1672
cristy3ed852e2009-09-05 21:47:34 +00001673 assert(image != (Image *) NULL);
1674 assert(image->signature == MagickSignature);
1675 if (image->debug != MagickFalse)
1676 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1677 GetMagickPixelPacket(image,&background);
1678 SetMagickPixelPacket(image,&image->background_color,(IndexPacket *) NULL,
1679 &background);
1680 if (image->colorspace == CMYKColorspace)
1681 ConvertRGBToCMYK(&background);
1682 /*
1683 Y Shear image.
1684 */
1685 status=MagickTrue;
1686 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00001687 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00001688#if defined(MAGICKCORE_OPENMP_SUPPORT)
1689 #pragma omp parallel for schedule(dynamic,4) shared(progress, status)
cristy3ed852e2009-09-05 21:47:34 +00001690#endif
cristybb503372010-05-27 20:51:26 +00001691 for (x=0; x < (ssize_t) width; x++)
cristy3ed852e2009-09-05 21:47:34 +00001692 {
cristybb503372010-05-27 20:51:26 +00001693 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001694 step;
1695
1696 MagickPixelPacket
1697 pixel,
1698 source,
1699 destination;
1700
1701 MagickRealType
1702 area,
1703 displacement;
1704
1705 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001706 *restrict indexes,
1707 *restrict shear_indexes;
cristy3ed852e2009-09-05 21:47:34 +00001708
cristybb503372010-05-27 20:51:26 +00001709 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001710 i;
1711
1712 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001713 *restrict p,
1714 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001715
1716 ShearDirection
1717 direction;
1718
1719 if (status == MagickFalse)
1720 continue;
1721 p=GetCacheViewAuthenticPixels(image_view,x_offset+x,0,1,image->rows,
1722 exception);
1723 if (p == (PixelPacket *) NULL)
1724 {
1725 status=MagickFalse;
1726 continue;
1727 }
1728 indexes=GetCacheViewAuthenticIndexQueue(image_view);
1729 p+=y_offset;
1730 indexes+=y_offset;
1731 displacement=degrees*(MagickRealType) (x-width/2.0);
1732 if (displacement == 0.0)
1733 continue;
1734 if (displacement > 0.0)
1735 direction=DOWN;
1736 else
1737 {
1738 displacement*=(-1.0);
1739 direction=UP;
1740 }
cristybb503372010-05-27 20:51:26 +00001741 step=(ssize_t) floor((double) displacement);
cristy3ed852e2009-09-05 21:47:34 +00001742 area=(MagickRealType) (displacement-step);
1743 step++;
1744 pixel=background;
1745 GetMagickPixelPacket(image,&source);
1746 GetMagickPixelPacket(image,&destination);
1747 switch (direction)
1748 {
1749 case UP:
1750 {
1751 /*
1752 Transfer pixels top-to-bottom.
1753 */
1754 if (step > y_offset)
1755 break;
1756 q=p-step;
1757 shear_indexes=indexes-step;
cristybb503372010-05-27 20:51:26 +00001758 for (i=0; i < (ssize_t) height; i++)
cristy3ed852e2009-09-05 21:47:34 +00001759 {
1760 if ((y_offset+i) < step)
1761 {
1762 SetMagickPixelPacket(image,++p,++indexes,&pixel);
1763 q++;
1764 shear_indexes++;
1765 continue;
1766 }
1767 SetMagickPixelPacket(image,p,indexes,&source);
1768 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1769 &source,(MagickRealType) p->opacity,area,&destination);
1770 SetPixelPacket(image,&destination,q++,shear_indexes++);
1771 SetMagickPixelPacket(image,p++,indexes++,&pixel);
1772 }
1773 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1774 &background,(MagickRealType) background.opacity,area,&destination);
1775 SetPixelPacket(image,&destination,q++,shear_indexes++);
1776 for (i=0; i < (step-1); i++)
1777 SetPixelPacket(image,&background,q++,shear_indexes++);
1778 break;
1779 }
1780 case DOWN:
1781 {
1782 /*
1783 Transfer pixels bottom-to-top.
1784 */
1785 p+=height;
1786 indexes+=height;
1787 q=p+step;
1788 shear_indexes=indexes+step;
cristybb503372010-05-27 20:51:26 +00001789 for (i=0; i < (ssize_t) height; i++)
cristy3ed852e2009-09-05 21:47:34 +00001790 {
1791 p--;
1792 indexes--;
1793 q--;
1794 shear_indexes--;
cristybb503372010-05-27 20:51:26 +00001795 if ((size_t) (y_offset+height+step-i) >= image->rows)
cristy3ed852e2009-09-05 21:47:34 +00001796 continue;
1797 SetMagickPixelPacket(image,p,indexes,&source);
1798 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1799 &source,(MagickRealType) p->opacity,area,&destination);
1800 SetPixelPacket(image,&destination,q,shear_indexes);
1801 SetMagickPixelPacket(image,p,indexes,&pixel);
1802 }
1803 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1804 &background,(MagickRealType) background.opacity,area,&destination);
1805 SetPixelPacket(image,&destination,--q,--shear_indexes);
1806 for (i=0; i < (step-1); i++)
1807 SetPixelPacket(image,&background,--q,--shear_indexes);
1808 break;
1809 }
1810 }
1811 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1812 status=MagickFalse;
1813 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1814 {
1815 MagickBooleanType
1816 proceed;
1817
cristyb5d5f722009-11-04 03:03:49 +00001818#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00001819 #pragma omp critical (MagickCore_YShearImage)
1820#endif
1821 proceed=SetImageProgress(image,YShearImageTag,progress++,image->rows);
1822 if (proceed == MagickFalse)
1823 status=MagickFalse;
1824 }
1825 }
1826 image_view=DestroyCacheView(image_view);
1827 return(status);
1828}
1829
1830/*
1831%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1832% %
1833% %
1834% %
1835% R o t a t e I m a g e %
1836% %
1837% %
1838% %
1839%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1840%
1841% RotateImage() creates a new image that is a rotated copy of an existing
1842% one. Positive angles rotate counter-clockwise (right-hand rule), while
1843% negative angles rotate clockwise. Rotated images are usually larger than
1844% the originals and have 'empty' triangular corners. X axis. Empty
1845% triangles left over from shearing the image are filled with the background
1846% color defined by member 'background_color' of the image. RotateImage
1847% allocates the memory necessary for the new Image structure and returns a
1848% pointer to the new image.
1849%
1850% RotateImage() is based on the paper "A Fast Algorithm for General
1851% Raster Rotatation" by Alan W. Paeth. RotateImage is adapted from a similar
1852% method based on the Paeth paper written by Michael Halle of the Spatial
1853% Imaging Group, MIT Media Lab.
1854%
1855% The format of the RotateImage method is:
1856%
1857% Image *RotateImage(const Image *image,const double degrees,
1858% ExceptionInfo *exception)
1859%
1860% A description of each parameter follows.
1861%
1862% o image: the image.
1863%
1864% o degrees: Specifies the number of degrees to rotate the image.
1865%
1866% o exception: return any errors or warnings in this structure.
1867%
1868*/
1869MagickExport Image *RotateImage(const Image *image,const double degrees,
1870 ExceptionInfo *exception)
1871{
1872 Image
1873 *integral_image,
1874 *rotate_image;
1875
cristybb503372010-05-27 20:51:26 +00001876 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001877 x_offset,
1878 y_offset;
1879
cristyecc2c142010-01-17 22:25:46 +00001880 MagickBooleanType
1881 status;
1882
cristy3ed852e2009-09-05 21:47:34 +00001883 MagickRealType
1884 angle;
1885
1886 PointInfo
1887 shear;
1888
1889 RectangleInfo
1890 border_info;
1891
cristybb503372010-05-27 20:51:26 +00001892 size_t
cristy3ed852e2009-09-05 21:47:34 +00001893 height,
1894 rotations,
1895 width,
1896 y_width;
1897
1898 /*
1899 Adjust rotation angle.
1900 */
1901 assert(image != (Image *) NULL);
1902 assert(image->signature == MagickSignature);
1903 if (image->debug != MagickFalse)
1904 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1905 assert(exception != (ExceptionInfo *) NULL);
1906 assert(exception->signature == MagickSignature);
1907 angle=degrees;
1908 while (angle < -45.0)
1909 angle+=360.0;
1910 for (rotations=0; angle > 45.0; rotations++)
1911 angle-=90.0;
1912 rotations%=4;
1913 /*
1914 Calculate shear equations.
1915 */
1916 integral_image=IntegralRotateImage(image,rotations,exception);
1917 if (integral_image == (Image *) NULL)
1918 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1919 shear.x=(-tan((double) DegreesToRadians(angle)/2.0));
1920 shear.y=sin((double) DegreesToRadians(angle));
1921 if ((shear.x == 0.0) && (shear.y == 0.0))
1922 return(integral_image);
1923 if (SetImageStorageClass(integral_image,DirectClass) == MagickFalse)
1924 {
1925 InheritException(exception,&integral_image->exception);
1926 integral_image=DestroyImage(integral_image);
1927 return(integral_image);
1928 }
1929 if (integral_image->matte == MagickFalse)
1930 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel);
1931 /*
1932 Compute image size.
1933 */
1934 width=image->columns;
1935 height=image->rows;
1936 if ((rotations == 1) || (rotations == 3))
1937 {
1938 width=image->rows;
1939 height=image->columns;
1940 }
cristybb503372010-05-27 20:51:26 +00001941 y_width=width+(ssize_t) floor(fabs(shear.x)*height+0.5);
cristycee97112010-05-28 00:44:52 +00001942 x_offset=(ssize_t) ceil((double) width+((fabs(shear.y)*height)-width)/2.0-
1943 0.5);
1944 y_offset=(ssize_t) ceil((double) height+((fabs(shear.y)*y_width)-height)/2.0-
1945 0.5);
cristy3ed852e2009-09-05 21:47:34 +00001946 /*
1947 Surround image with a border.
1948 */
1949 integral_image->border_color=integral_image->background_color;
1950 integral_image->compose=CopyCompositeOp;
cristybb503372010-05-27 20:51:26 +00001951 border_info.width=(size_t) x_offset;
1952 border_info.height=(size_t) y_offset;
cristy3ed852e2009-09-05 21:47:34 +00001953 rotate_image=BorderImage(integral_image,&border_info,exception);
1954 integral_image=DestroyImage(integral_image);
1955 if (rotate_image == (Image *) NULL)
1956 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1957 /*
1958 Rotate the image.
1959 */
cristybb503372010-05-27 20:51:26 +00001960 status=XShearImage(rotate_image,shear.x,width,height,x_offset,((ssize_t)
cristyecc2c142010-01-17 22:25:46 +00001961 rotate_image->rows-height)/2,exception);
1962 if (status == MagickFalse)
1963 {
1964 rotate_image=DestroyImage(rotate_image);
1965 return((Image *) NULL);
1966 }
cristybb503372010-05-27 20:51:26 +00001967 status=YShearImage(rotate_image,shear.y,y_width,height,((ssize_t)
cristyecc2c142010-01-17 22:25:46 +00001968 rotate_image->columns-y_width)/2,y_offset,exception);
1969 if (status == MagickFalse)
1970 {
1971 rotate_image=DestroyImage(rotate_image);
1972 return((Image *) NULL);
1973 }
cristybb503372010-05-27 20:51:26 +00001974 status=XShearImage(rotate_image,shear.x,y_width,rotate_image->rows,((ssize_t)
cristyecc2c142010-01-17 22:25:46 +00001975 rotate_image->columns-y_width)/2,0,exception);
1976 if (status == MagickFalse)
1977 {
1978 rotate_image=DestroyImage(rotate_image);
1979 return((Image *) NULL);
1980 }
1981 status=CropToFitImage(&rotate_image,shear.x,shear.y,(MagickRealType) width,
cristy3ed852e2009-09-05 21:47:34 +00001982 (MagickRealType) height,MagickTrue,exception);
cristyecc2c142010-01-17 22:25:46 +00001983 if (status == MagickFalse)
1984 {
1985 rotate_image=DestroyImage(rotate_image);
1986 return((Image *) NULL);
1987 }
cristy3ed852e2009-09-05 21:47:34 +00001988 rotate_image->compose=image->compose;
1989 rotate_image->page.width=0;
1990 rotate_image->page.height=0;
1991 return(rotate_image);
1992}
1993
1994/*
1995%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1996% %
1997% %
1998% %
1999% S h e a r I m a g e %
2000% %
2001% %
2002% %
2003%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2004%
2005% ShearImage() creates a new image that is a shear_image copy of an existing
cristycee97112010-05-28 00:44:52 +00002006% one. Shearing slides one edge of an image along the X or Y axis, creating
2007% a parallelogram. An X direction shear slides an edge along the X axis,
2008% while a Y direction shear slides an edge along the Y axis. The amount of
cristy3ed852e2009-09-05 21:47:34 +00002009% the shear is controlled by a shear angle. For X direction shears, x_shear
2010% is measured relative to the Y axis, and similarly, for Y direction shears
2011% y_shear is measured relative to the X axis. Empty triangles left over from
2012% shearing the image are filled with the background color defined by member
2013% 'background_color' of the image.. ShearImage() allocates the memory
2014% necessary for the new Image structure and returns a pointer to the new image.
2015%
2016% ShearImage() is based on the paper "A Fast Algorithm for General Raster
2017% Rotatation" by Alan W. Paeth.
2018%
2019% The format of the ShearImage method is:
2020%
2021% Image *ShearImage(const Image *image,const double x_shear,
2022% const double y_shear,ExceptionInfo *exception)
2023%
2024% A description of each parameter follows.
2025%
2026% o image: the image.
2027%
2028% o x_shear, y_shear: Specifies the number of degrees to shear the image.
2029%
2030% o exception: return any errors or warnings in this structure.
2031%
2032*/
2033MagickExport Image *ShearImage(const Image *image,const double x_shear,
2034 const double y_shear,ExceptionInfo *exception)
2035{
2036 Image
2037 *integral_image,
2038 *shear_image;
2039
cristybb503372010-05-27 20:51:26 +00002040 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002041 x_offset,
2042 y_offset;
2043
cristyecc2c142010-01-17 22:25:46 +00002044 MagickBooleanType
2045 status;
2046
cristy3ed852e2009-09-05 21:47:34 +00002047 PointInfo
2048 shear;
2049
2050 RectangleInfo
2051 border_info;
2052
cristybb503372010-05-27 20:51:26 +00002053 size_t
cristy3ed852e2009-09-05 21:47:34 +00002054 y_width;
2055
2056 assert(image != (Image *) NULL);
2057 assert(image->signature == MagickSignature);
2058 if (image->debug != MagickFalse)
2059 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2060 assert(exception != (ExceptionInfo *) NULL);
2061 assert(exception->signature == MagickSignature);
2062 if ((x_shear != 0.0) && (fmod(x_shear,90.0) == 0.0))
2063 ThrowImageException(ImageError,"AngleIsDiscontinuous");
2064 if ((y_shear != 0.0) && (fmod(y_shear,90.0) == 0.0))
2065 ThrowImageException(ImageError,"AngleIsDiscontinuous");
2066 /*
2067 Initialize shear angle.
2068 */
2069 integral_image=CloneImage(image,0,0,MagickTrue,exception);
2070 if (integral_image == (Image *) NULL)
2071 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2072 shear.x=(-tan(DegreesToRadians(fmod(x_shear,360.0))));
2073 shear.y=tan(DegreesToRadians(fmod(y_shear,360.0)));
2074 if ((shear.x == 0.0) && (shear.y == 0.0))
2075 return(integral_image);
2076 if (SetImageStorageClass(integral_image,DirectClass) == MagickFalse)
2077 {
2078 InheritException(exception,&integral_image->exception);
2079 integral_image=DestroyImage(integral_image);
2080 return(integral_image);
2081 }
2082 if (integral_image->matte == MagickFalse)
2083 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel);
2084 /*
2085 Compute image size.
2086 */
cristybb503372010-05-27 20:51:26 +00002087 y_width=image->columns+(ssize_t) floor(fabs(shear.x)*image->rows+0.5);
cristycee97112010-05-28 00:44:52 +00002088 x_offset=(ssize_t) ceil((double) image->columns+((fabs(shear.x)*image->rows)-
cristy06609ee2010-03-17 20:21:27 +00002089 image->columns)/2.0-0.5);
cristycee97112010-05-28 00:44:52 +00002090 y_offset=(ssize_t) ceil((double) image->rows+((fabs(shear.y)*y_width)-
2091 image->rows)/2.0-0.5);
cristy3ed852e2009-09-05 21:47:34 +00002092 /*
2093 Surround image with border.
2094 */
2095 integral_image->border_color=integral_image->background_color;
2096 integral_image->compose=CopyCompositeOp;
cristybb503372010-05-27 20:51:26 +00002097 border_info.width=(size_t) x_offset;
2098 border_info.height=(size_t) y_offset;
cristy3ed852e2009-09-05 21:47:34 +00002099 shear_image=BorderImage(integral_image,&border_info,exception);
cristyecc2c142010-01-17 22:25:46 +00002100 integral_image=DestroyImage(integral_image);
cristy3ed852e2009-09-05 21:47:34 +00002101 if (shear_image == (Image *) NULL)
2102 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +00002103 /*
2104 Shear the image.
2105 */
2106 if (shear_image->matte == MagickFalse)
2107 (void) SetImageAlphaChannel(shear_image,OpaqueAlphaChannel);
cristyecc2c142010-01-17 22:25:46 +00002108 status=XShearImage(shear_image,shear.x,image->columns,image->rows,x_offset,
cristybb503372010-05-27 20:51:26 +00002109 ((ssize_t) shear_image->rows-image->rows)/2,exception);
cristyecc2c142010-01-17 22:25:46 +00002110 if (status == MagickFalse)
2111 {
2112 shear_image=DestroyImage(shear_image);
2113 return((Image *) NULL);
2114 }
cristybb503372010-05-27 20:51:26 +00002115 status=YShearImage(shear_image,shear.y,y_width,image->rows,((ssize_t)
cristyecc2c142010-01-17 22:25:46 +00002116 shear_image->columns-y_width)/2,y_offset,exception);
2117 if (status == MagickFalse)
2118 {
2119 shear_image=DestroyImage(shear_image);
2120 return((Image *) NULL);
2121 }
2122 status=CropToFitImage(&shear_image,shear.x,shear.y,(MagickRealType)
2123 image->columns,(MagickRealType) image->rows,MagickFalse,exception);
2124 if (status == MagickFalse)
2125 {
2126 shear_image=DestroyImage(shear_image);
2127 return((Image *) NULL);
2128 }
cristy3ed852e2009-09-05 21:47:34 +00002129 shear_image->compose=image->compose;
2130 shear_image->page.width=0;
2131 shear_image->page.height=0;
2132 return(shear_image);
2133}