blob: a36e0805df65b69b0bf5f084fc518e35e33414d5 [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% %
cristy7e41fe82010-12-04 23:12:08 +000020% Copyright 1999-2011 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*/
cristy4c08aed2011-07-01 19:47:50 +000048#include "MagickCore/studio.h"
49#include "MagickCore/artifact.h"
50#include "MagickCore/attribute.h"
51#include "MagickCore/blob-private.h"
52#include "MagickCore/cache-private.h"
53#include "MagickCore/color-private.h"
54#include "MagickCore/colorspace-private.h"
55#include "MagickCore/composite.h"
56#include "MagickCore/composite-private.h"
57#include "MagickCore/decorate.h"
58#include "MagickCore/distort.h"
59#include "MagickCore/draw.h"
60#include "MagickCore/exception.h"
61#include "MagickCore/exception-private.h"
62#include "MagickCore/gem.h"
63#include "MagickCore/geometry.h"
64#include "MagickCore/image.h"
65#include "MagickCore/image-private.h"
66#include "MagickCore/memory_.h"
67#include "MagickCore/list.h"
68#include "MagickCore/monitor.h"
69#include "MagickCore/monitor-private.h"
70#include "MagickCore/pixel-accessor.h"
71#include "MagickCore/quantum.h"
72#include "MagickCore/resource_.h"
73#include "MagickCore/shear.h"
74#include "MagickCore/statistic.h"
75#include "MagickCore/string_.h"
76#include "MagickCore/string-private.h"
77#include "MagickCore/thread-private.h"
78#include "MagickCore/threshold.h"
79#include "MagickCore/transform.h"
cristy3ed852e2009-09-05 21:47:34 +000080
81/*
82%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
83% %
84% %
85% %
86% A f f i n e T r a n s f o r m I m a g e %
87% %
88% %
89% %
90%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
91%
92% AffineTransformImage() transforms an image as dictated by the affine matrix.
93% It allocates the memory necessary for the new Image structure and returns
94% a pointer to the new image.
95%
96% The format of the AffineTransformImage method is:
97%
98% Image *AffineTransformImage(const Image *image,
99% AffineMatrix *affine_matrix,ExceptionInfo *exception)
100%
101% A description of each parameter follows:
102%
103% o image: the image.
104%
105% o affine_matrix: the affine matrix.
106%
107% o exception: return any errors or warnings in this structure.
108%
109*/
110MagickExport Image *AffineTransformImage(const Image *image,
111 const AffineMatrix *affine_matrix,ExceptionInfo *exception)
112{
113 double
114 distort[6];
115
116 Image
117 *deskew_image;
118
119 /*
120 Affine transform image.
121 */
122 assert(image->signature == MagickSignature);
123 if (image->debug != MagickFalse)
124 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
125 assert(affine_matrix != (AffineMatrix *) NULL);
126 assert(exception != (ExceptionInfo *) NULL);
127 assert(exception->signature == MagickSignature);
128 distort[0]=affine_matrix->sx;
129 distort[1]=affine_matrix->rx;
130 distort[2]=affine_matrix->ry;
131 distort[3]=affine_matrix->sy;
132 distort[4]=affine_matrix->tx;
133 distort[5]=affine_matrix->ty;
134 deskew_image=DistortImage(image,AffineProjectionDistortion,6,distort,
135 MagickTrue,exception);
136 return(deskew_image);
137}
138
139/*
140%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
141% %
142% %
143% %
144+ C r o p T o F i t I m a g e %
145% %
146% %
147% %
148%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149%
150% CropToFitImage() crops the sheared image as determined by the bounding box
151% as defined by width and height and shearing angles.
152%
153% The format of the CropToFitImage method is:
154%
cristyecc2c142010-01-17 22:25:46 +0000155% MagickBooleanType CropToFitImage(Image **image,
156% const MagickRealType x_shear,const MagickRealType x_shear,
157% const MagickRealType width,const MagickRealType height,
158% const MagickBooleanType rotate,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000159%
160% A description of each parameter follows.
161%
162% o image: the image.
163%
164% o x_shear, y_shear, width, height: Defines a region of the image to crop.
165%
166% o exception: return any errors or warnings in this structure.
167%
168*/
cristyecc2c142010-01-17 22:25:46 +0000169static MagickBooleanType CropToFitImage(Image **image,
170 const MagickRealType x_shear,const MagickRealType y_shear,
171 const MagickRealType width,const MagickRealType height,
172 const MagickBooleanType rotate,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000173{
174 Image
175 *crop_image;
176
177 PointInfo
178 extent[4],
179 min,
180 max;
181
182 RectangleInfo
183 geometry,
184 page;
185
cristybb503372010-05-27 20:51:26 +0000186 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000187 i;
188
189 /*
190 Calculate the rotated image size.
191 */
192 extent[0].x=(double) (-width/2.0);
193 extent[0].y=(double) (-height/2.0);
194 extent[1].x=(double) width/2.0;
195 extent[1].y=(double) (-height/2.0);
196 extent[2].x=(double) (-width/2.0);
197 extent[2].y=(double) height/2.0;
198 extent[3].x=(double) width/2.0;
199 extent[3].y=(double) height/2.0;
200 for (i=0; i < 4; i++)
201 {
202 extent[i].x+=x_shear*extent[i].y;
203 extent[i].y+=y_shear*extent[i].x;
204 if (rotate != MagickFalse)
205 extent[i].x+=x_shear*extent[i].y;
206 extent[i].x+=(double) (*image)->columns/2.0;
207 extent[i].y+=(double) (*image)->rows/2.0;
208 }
209 min=extent[0];
210 max=extent[0];
211 for (i=1; i < 4; i++)
212 {
213 if (min.x > extent[i].x)
214 min.x=extent[i].x;
215 if (min.y > extent[i].y)
216 min.y=extent[i].y;
217 if (max.x < extent[i].x)
218 max.x=extent[i].x;
219 if (max.y < extent[i].y)
220 max.y=extent[i].y;
221 }
cristybb503372010-05-27 20:51:26 +0000222 geometry.x=(ssize_t) ceil(min.x-0.5);
223 geometry.y=(ssize_t) ceil(min.y-0.5);
224 geometry.width=(size_t) floor(max.x-min.x+0.5);
225 geometry.height=(size_t) floor(max.y-min.y+0.5);
cristy3ed852e2009-09-05 21:47:34 +0000226 page=(*image)->page;
227 (void) ParseAbsoluteGeometry("0x0+0+0",&(*image)->page);
228 crop_image=CropImage(*image,&geometry,exception);
cristyecc2c142010-01-17 22:25:46 +0000229 if (crop_image == (Image *) NULL)
230 return(MagickFalse);
231 crop_image->page=page;
232 *image=DestroyImage(*image);
233 *image=crop_image;
234 return(MagickTrue);
cristy3ed852e2009-09-05 21:47:34 +0000235}
236
237/*
238%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
239% %
240% %
241% %
242% D e s k e w I m a g e %
243% %
244% %
245% %
246%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
247%
248% DeskewImage() removes skew from the image. Skew is an artifact that
249% occurs in scanned images because of the camera being misaligned,
250% imperfections in the scanning or surface, or simply because the paper was
251% not placed completely flat when scanned.
252%
253% The format of the DeskewImage method is:
254%
255% Image *DeskewImage(const Image *image,const double threshold,
256% ExceptionInfo *exception)
257%
258% A description of each parameter follows:
259%
260% o image: the image.
261%
262% o threshold: separate background from foreground.
263%
264% o exception: return any errors or warnings in this structure.
265%
266*/
267
268typedef struct _RadonInfo
269{
270 CacheType
271 type;
272
cristybb503372010-05-27 20:51:26 +0000273 size_t
cristy3ed852e2009-09-05 21:47:34 +0000274 width,
275 height;
276
277 MagickSizeType
278 length;
279
280 MagickBooleanType
281 mapped;
282
283 char
284 path[MaxTextExtent];
285
286 int
287 file;
288
289 unsigned short
290 *cells;
291} RadonInfo;
292
293static RadonInfo *DestroyRadonInfo(RadonInfo *radon_info)
294{
295 assert(radon_info != (RadonInfo *) NULL);
296 switch (radon_info->type)
297 {
298 case MemoryCache:
299 {
300 if (radon_info->mapped == MagickFalse)
301 radon_info->cells=(unsigned short *) RelinquishMagickMemory(
302 radon_info->cells);
303 else
304 radon_info->cells=(unsigned short *) UnmapBlob(radon_info->cells,
305 (size_t) radon_info->length);
306 RelinquishMagickResource(MemoryResource,radon_info->length);
307 break;
308 }
309 case MapCache:
310 {
311 radon_info->cells=(unsigned short *) UnmapBlob(radon_info->cells,(size_t)
312 radon_info->length);
313 RelinquishMagickResource(MapResource,radon_info->length);
314 }
315 case DiskCache:
316 {
317 if (radon_info->file != -1)
318 (void) close(radon_info->file);
319 (void) RelinquishUniqueFileResource(radon_info->path);
320 RelinquishMagickResource(DiskResource,radon_info->length);
321 break;
322 }
323 default:
324 break;
325 }
326 return((RadonInfo *) RelinquishMagickMemory(radon_info));
327}
328
329static MagickBooleanType ResetRadonCells(RadonInfo *radon_info)
330{
cristybb503372010-05-27 20:51:26 +0000331 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000332 x;
333
334 ssize_t
cristyd40deb62011-03-09 00:52:27 +0000335 count,
336 y;
cristy3ed852e2009-09-05 21:47:34 +0000337
338 unsigned short
339 value;
340
341 if (radon_info->type != DiskCache)
342 {
343 (void) ResetMagickMemory(radon_info->cells,0,(size_t) radon_info->length);
344 return(MagickTrue);
345 }
346 value=0;
cristy7f317702011-02-18 20:40:28 +0000347 (void) lseek(radon_info->file,0,SEEK_SET);
cristybb503372010-05-27 20:51:26 +0000348 for (y=0; y < (ssize_t) radon_info->height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000349 {
cristybb503372010-05-27 20:51:26 +0000350 for (x=0; x < (ssize_t) radon_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000351 {
352 count=write(radon_info->file,&value,sizeof(*radon_info->cells));
353 if (count != (ssize_t) sizeof(*radon_info->cells))
354 break;
355 }
cristybb503372010-05-27 20:51:26 +0000356 if (x < (ssize_t) radon_info->width)
cristy3ed852e2009-09-05 21:47:34 +0000357 break;
358 }
cristybb503372010-05-27 20:51:26 +0000359 return(y < (ssize_t) radon_info->height ? MagickFalse : MagickTrue);
cristy3ed852e2009-09-05 21:47:34 +0000360}
361
cristybb503372010-05-27 20:51:26 +0000362static RadonInfo *AcquireRadonInfo(const Image *image,const size_t width,
363 const size_t height,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000364{
365 MagickBooleanType
366 status;
367
368 RadonInfo
369 *radon_info;
370
cristy73bd4a52010-10-05 11:24:23 +0000371 radon_info=(RadonInfo *) AcquireMagickMemory(sizeof(*radon_info));
cristy3ed852e2009-09-05 21:47:34 +0000372 if (radon_info == (RadonInfo *) NULL)
373 return((RadonInfo *) NULL);
374 (void) ResetMagickMemory(radon_info,0,sizeof(*radon_info));
375 radon_info->width=width;
376 radon_info->height=height;
377 radon_info->length=(MagickSizeType) width*height*sizeof(*radon_info->cells);
378 radon_info->type=MemoryCache;
379 status=AcquireMagickResource(AreaResource,radon_info->length);
380 if ((status != MagickFalse) &&
381 (radon_info->length == (MagickSizeType) ((size_t) radon_info->length)))
382 {
383 status=AcquireMagickResource(MemoryResource,radon_info->length);
384 if (status != MagickFalse)
385 {
386 radon_info->mapped=MagickFalse;
387 radon_info->cells=(unsigned short *) AcquireMagickMemory((size_t)
388 radon_info->length);
389 if (radon_info->cells == (unsigned short *) NULL)
390 {
391 radon_info->mapped=MagickTrue;
392 radon_info->cells=(unsigned short *) MapBlob(-1,IOMode,0,(size_t)
393 radon_info->length);
394 }
395 if (radon_info->cells == (unsigned short *) NULL)
396 RelinquishMagickResource(MemoryResource,radon_info->length);
397 }
398 }
399 radon_info->file=(-1);
400 if (radon_info->cells == (unsigned short *) NULL)
401 {
402 status=AcquireMagickResource(DiskResource,radon_info->length);
403 if (status == MagickFalse)
404 {
405 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
406 "CacheResourcesExhausted","`%s'",image->filename);
407 return(DestroyRadonInfo(radon_info));
408 }
409 radon_info->type=DiskCache;
410 (void) AcquireMagickResource(MemoryResource,radon_info->length);
411 radon_info->file=AcquireUniqueFileResource(radon_info->path);
412 if (radon_info->file == -1)
413 return(DestroyRadonInfo(radon_info));
414 status=AcquireMagickResource(MapResource,radon_info->length);
415 if (status != MagickFalse)
416 {
417 status=ResetRadonCells(radon_info);
418 if (status != MagickFalse)
419 {
420 radon_info->cells=(unsigned short *) MapBlob(radon_info->file,
421 IOMode,0,(size_t) radon_info->length);
422 if (radon_info->cells != (unsigned short *) NULL)
423 radon_info->type=MapCache;
424 else
425 RelinquishMagickResource(MapResource,radon_info->length);
426 }
427 }
428 }
429 return(radon_info);
430}
431
432static inline size_t MagickMin(const size_t x,const size_t y)
433{
434 if (x < y)
435 return(x);
436 return(y);
437}
438
439static inline ssize_t ReadRadonCell(const RadonInfo *radon_info,
cristy2c38b272011-02-18 23:25:33 +0000440 const MagickOffsetType offset,const size_t length,unsigned char *buffer)
cristy3ed852e2009-09-05 21:47:34 +0000441{
442 register ssize_t
443 i;
444
445 ssize_t
446 count;
447
448#if !defined(MAGICKCORE_HAVE_PPREAD)
cristyb5d5f722009-11-04 03:03:49 +0000449#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000450 #pragma omp critical (MagickCore_ReadRadonCell)
451#endif
452 {
453 i=(-1);
cristy7f317702011-02-18 20:40:28 +0000454 if (lseek(radon_info->file,offset,SEEK_SET) >= 0)
cristy3ed852e2009-09-05 21:47:34 +0000455 {
456#endif
457 count=0;
458 for (i=0; i < (ssize_t) length; i+=count)
459 {
460#if !defined(MAGICKCORE_HAVE_PPREAD)
461 count=read(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
462 SSIZE_MAX));
463#else
464 count=pread(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
cristy2c38b272011-02-18 23:25:33 +0000465 SSIZE_MAX),offset+i);
cristy3ed852e2009-09-05 21:47:34 +0000466#endif
467 if (count > 0)
468 continue;
469 count=0;
470 if (errno != EINTR)
471 {
472 i=(-1);
473 break;
474 }
475 }
476#if !defined(MAGICKCORE_HAVE_PPREAD)
477 }
478 }
479#endif
480 return(i);
481}
482
483static inline ssize_t WriteRadonCell(const RadonInfo *radon_info,
cristy2c38b272011-02-18 23:25:33 +0000484 const MagickOffsetType offset,const size_t length,const unsigned char *buffer)
cristy3ed852e2009-09-05 21:47:34 +0000485{
486 register ssize_t
487 i;
488
489 ssize_t
490 count;
491
492#if !defined(MAGICKCORE_HAVE_PWRITE)
cristyb5d5f722009-11-04 03:03:49 +0000493#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000494 #pragma omp critical (MagickCore_WriteRadonCell)
495#endif
496 {
cristy7f317702011-02-18 20:40:28 +0000497 if (lseek(radon_info->file,offset,SEEK_SET) >= 0)
cristy3ed852e2009-09-05 21:47:34 +0000498 {
499#endif
500 count=0;
501 for (i=0; i < (ssize_t) length; i+=count)
502 {
503#if !defined(MAGICKCORE_HAVE_PWRITE)
504 count=write(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
505 SSIZE_MAX));
506#else
507 count=pwrite(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
cristy2c38b272011-02-18 23:25:33 +0000508 SSIZE_MAX),offset+i);
cristy3ed852e2009-09-05 21:47:34 +0000509#endif
510 if (count > 0)
511 continue;
512 count=0;
513 if (errno != EINTR)
514 {
515 i=(-1);
516 break;
517 }
518 }
519#if !defined(MAGICKCORE_HAVE_PWRITE)
520 }
521 }
522#endif
523 return(i);
524}
525
526static inline unsigned short GetRadonCell(const RadonInfo *radon_info,
cristybb503372010-05-27 20:51:26 +0000527 const ssize_t x,const ssize_t y)
cristy3ed852e2009-09-05 21:47:34 +0000528{
cristy2c38b272011-02-18 23:25:33 +0000529 MagickOffsetType
cristy3ed852e2009-09-05 21:47:34 +0000530 i;
531
532 unsigned short
533 value;
534
cristy2c38b272011-02-18 23:25:33 +0000535 i=(MagickOffsetType) radon_info->height*x+y;
cristy3ed852e2009-09-05 21:47:34 +0000536 if ((i < 0) ||
537 ((MagickSizeType) (i*sizeof(*radon_info->cells)) >= radon_info->length))
538 return(0);
539 if (radon_info->type != DiskCache)
540 return(radon_info->cells[i]);
541 value=0;
542 (void) ReadRadonCell(radon_info,i*sizeof(*radon_info->cells),
543 sizeof(*radon_info->cells),(unsigned char *) &value);
544 return(value);
545}
546
547static inline MagickBooleanType SetRadonCell(const RadonInfo *radon_info,
cristybb503372010-05-27 20:51:26 +0000548 const ssize_t x,const ssize_t y,const unsigned short value)
cristy3ed852e2009-09-05 21:47:34 +0000549{
cristy2c38b272011-02-18 23:25:33 +0000550 MagickOffsetType
cristy3ed852e2009-09-05 21:47:34 +0000551 i;
552
553 ssize_t
554 count;
555
cristy2c38b272011-02-18 23:25:33 +0000556 i=(MagickOffsetType) radon_info->height*x+y;
cristy3ed852e2009-09-05 21:47:34 +0000557 if ((i < 0) ||
558 ((MagickSizeType) (i*sizeof(*radon_info->cells)) >= radon_info->length))
559 return(MagickFalse);
560 if (radon_info->type != DiskCache)
561 {
562 radon_info->cells[i]=value;
563 return(MagickTrue);
564 }
565 count=WriteRadonCell(radon_info,i*sizeof(*radon_info->cells),
cristy44807bb2009-09-19 18:28:06 +0000566 sizeof(*radon_info->cells),(const unsigned char *) &value);
cristy3ed852e2009-09-05 21:47:34 +0000567 if (count != (ssize_t) sizeof(*radon_info->cells))
568 return(MagickFalse);
569 return(MagickTrue);
570}
571
572static void RadonProjection(RadonInfo *source_cells,
cristybb503372010-05-27 20:51:26 +0000573 RadonInfo *destination_cells,const ssize_t sign,size_t *projection)
cristy3ed852e2009-09-05 21:47:34 +0000574{
575 RadonInfo
576 *swap;
577
cristybb503372010-05-27 20:51:26 +0000578 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000579 x;
580
581 register RadonInfo
582 *p,
583 *q;
584
cristybb503372010-05-27 20:51:26 +0000585 size_t
cristy3ed852e2009-09-05 21:47:34 +0000586 step;
587
588 p=source_cells;
589 q=destination_cells;
590 for (step=1; step < p->width; step*=2)
591 {
cristyeaedf062010-05-29 22:36:02 +0000592 for (x=0; x < (ssize_t) p->width; x+=2*(ssize_t) step)
cristy3ed852e2009-09-05 21:47:34 +0000593 {
cristybb503372010-05-27 20:51:26 +0000594 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000595 i;
596
cristyd40deb62011-03-09 00:52:27 +0000597 ssize_t
598 y;
599
cristy3ed852e2009-09-05 21:47:34 +0000600 unsigned short
601 cell;
602
cristybb503372010-05-27 20:51:26 +0000603 for (i=0; i < (ssize_t) step; i++)
cristy3ed852e2009-09-05 21:47:34 +0000604 {
cristybb503372010-05-27 20:51:26 +0000605 for (y=0; y < (ssize_t) (p->height-i-1); y++)
cristy3ed852e2009-09-05 21:47:34 +0000606 {
607 cell=GetRadonCell(p,x+i,y);
cristyeaedf062010-05-29 22:36:02 +0000608 (void) SetRadonCell(q,x+2*i,y,cell+GetRadonCell(p,x+i+(ssize_t)
609 step,y+i));
610 (void) SetRadonCell(q,x+2*i+1,y,cell+GetRadonCell(p,x+i+(ssize_t)
611 step,y+i+1));
cristy3ed852e2009-09-05 21:47:34 +0000612 }
cristybb503372010-05-27 20:51:26 +0000613 for ( ; y < (ssize_t) (p->height-i); y++)
cristy3ed852e2009-09-05 21:47:34 +0000614 {
615 cell=GetRadonCell(p,x+i,y);
cristyeaedf062010-05-29 22:36:02 +0000616 (void) SetRadonCell(q,x+2*i,y,cell+GetRadonCell(p,x+i+(ssize_t) step,
617 y+i));
cristy3ed852e2009-09-05 21:47:34 +0000618 (void) SetRadonCell(q,x+2*i+1,y,cell);
619 }
cristybb503372010-05-27 20:51:26 +0000620 for ( ; y < (ssize_t) p->height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000621 {
622 cell=GetRadonCell(p,x+i,y);
623 (void) SetRadonCell(q,x+2*i,y,cell);
624 (void) SetRadonCell(q,x+2*i+1,y,cell);
625 }
626 }
627 }
628 swap=p;
629 p=q;
630 q=swap;
631 }
cristyb5d5f722009-11-04 03:03:49 +0000632#if defined(MAGICKCORE_OPENMP_SUPPORT)
633 #pragma omp parallel for schedule(dynamic,4)
cristy3ed852e2009-09-05 21:47:34 +0000634#endif
cristybb503372010-05-27 20:51:26 +0000635 for (x=0; x < (ssize_t) p->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000636 {
cristybb503372010-05-27 20:51:26 +0000637 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000638 y;
639
cristybb503372010-05-27 20:51:26 +0000640 size_t
cristy3ed852e2009-09-05 21:47:34 +0000641 sum;
642
643 sum=0;
cristybb503372010-05-27 20:51:26 +0000644 for (y=0; y < (ssize_t) (p->height-1); y++)
cristy3ed852e2009-09-05 21:47:34 +0000645 {
cristybb503372010-05-27 20:51:26 +0000646 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000647 delta;
648
cristybb503372010-05-27 20:51:26 +0000649 delta=GetRadonCell(p,x,y)-(ssize_t) GetRadonCell(p,x,y+1);
cristy3ed852e2009-09-05 21:47:34 +0000650 sum+=delta*delta;
651 }
652 projection[p->width+sign*x-1]=sum;
653 }
654}
655
656static MagickBooleanType RadonTransform(const Image *image,
cristybb503372010-05-27 20:51:26 +0000657 const double threshold,size_t *projection,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000658{
659 CacheView
660 *image_view;
661
cristy3ed852e2009-09-05 21:47:34 +0000662 MagickBooleanType
663 status;
664
665 RadonInfo
666 *destination_cells,
667 *source_cells;
668
cristybb503372010-05-27 20:51:26 +0000669 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000670 i;
671
cristybb503372010-05-27 20:51:26 +0000672 size_t
cristy3ed852e2009-09-05 21:47:34 +0000673 count,
674 width;
675
cristyd40deb62011-03-09 00:52:27 +0000676 ssize_t
677 y;
678
679 unsigned char
680 byte;
681
cristy3ed852e2009-09-05 21:47:34 +0000682 unsigned short
683 bits[256];
684
685 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
686 source_cells=AcquireRadonInfo(image,width,image->rows,exception);
687 destination_cells=AcquireRadonInfo(image,width,image->rows,exception);
688 if ((source_cells == (RadonInfo *) NULL) ||
689 (destination_cells == (RadonInfo *) NULL))
690 {
691 if (destination_cells != (RadonInfo *) NULL)
692 destination_cells=DestroyRadonInfo(destination_cells);
693 if (source_cells != (RadonInfo *) NULL)
694 source_cells=DestroyRadonInfo(source_cells);
695 return(MagickFalse);
696 }
697 if (ResetRadonCells(source_cells) == MagickFalse)
698 {
699 destination_cells=DestroyRadonInfo(destination_cells);
700 source_cells=DestroyRadonInfo(source_cells);
701 return(MagickFalse);
702 }
703 for (i=0; i < 256; i++)
704 {
705 byte=(unsigned char) i;
706 for (count=0; byte != 0; byte>>=1)
707 count+=byte & 0x01;
708 bits[i]=(unsigned short) count;
709 }
710 status=MagickTrue;
711 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +0000712#if defined(MAGICKCORE_OPENMP_SUPPORT)
713 #pragma omp parallel for schedule(dynamic,4) shared(status)
cristy3ed852e2009-09-05 21:47:34 +0000714#endif
cristybb503372010-05-27 20:51:26 +0000715 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000716 {
cristy4c08aed2011-07-01 19:47:50 +0000717 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000718 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000719
cristybb503372010-05-27 20:51:26 +0000720 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000721 i,
722 x;
723
cristybb503372010-05-27 20:51:26 +0000724 size_t
cristy3ed852e2009-09-05 21:47:34 +0000725 bit,
726 byte;
727
728 if (status == MagickFalse)
729 continue;
730 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000731 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000732 {
733 status=MagickFalse;
734 continue;
735 }
736 bit=0;
737 byte=0;
cristybb503372010-05-27 20:51:26 +0000738 i=(ssize_t) (image->columns+7)/8;
739 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000740 {
741 byte<<=1;
cristy4c08aed2011-07-01 19:47:50 +0000742 if (((MagickRealType) GetPixelRed(image,p) < threshold) ||
743 ((MagickRealType) GetPixelGreen(image,p) < threshold) ||
744 ((MagickRealType) GetPixelBlue(image,p) < threshold))
cristy3ed852e2009-09-05 21:47:34 +0000745 byte|=0x01;
746 bit++;
747 if (bit == 8)
748 {
749 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
750 bit=0;
751 byte=0;
752 }
cristyed231572011-07-14 02:18:59 +0000753 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000754 }
755 if (bit != 0)
756 {
757 byte<<=(8-bit);
758 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
759 }
760 }
761 RadonProjection(source_cells,destination_cells,-1,projection);
762 (void) ResetRadonCells(source_cells);
cristyb5d5f722009-11-04 03:03:49 +0000763#if defined(MAGICKCORE_OPENMP_SUPPORT)
764 #pragma omp parallel for schedule(dynamic,4) shared(status)
cristy3ed852e2009-09-05 21:47:34 +0000765#endif
cristybb503372010-05-27 20:51:26 +0000766 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000767 {
cristy4c08aed2011-07-01 19:47:50 +0000768 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000769 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000770
cristybb503372010-05-27 20:51:26 +0000771 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000772 i,
773 x;
774
cristybb503372010-05-27 20:51:26 +0000775 size_t
cristy3ed852e2009-09-05 21:47:34 +0000776 bit,
777 byte;
778
779 if (status == MagickFalse)
780 continue;
781 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000782 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000783 {
784 status=MagickFalse;
785 continue;
786 }
787 bit=0;
788 byte=0;
789 i=0;
cristybb503372010-05-27 20:51:26 +0000790 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000791 {
792 byte<<=1;
cristy4c08aed2011-07-01 19:47:50 +0000793 if (((MagickRealType) GetPixelRed(image,p) < threshold) ||
794 ((MagickRealType) GetPixelGreen(image,p) < threshold) ||
795 ((MagickRealType) GetPixelBlue(image,p) < threshold))
cristy3ed852e2009-09-05 21:47:34 +0000796 byte|=0x01;
797 bit++;
798 if (bit == 8)
799 {
800 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
801 bit=0;
802 byte=0;
803 }
cristyed231572011-07-14 02:18:59 +0000804 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000805 }
806 if (bit != 0)
807 {
808 byte<<=(8-bit);
809 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
810 }
811 }
812 RadonProjection(source_cells,destination_cells,1,projection);
813 image_view=DestroyCacheView(image_view);
814 destination_cells=DestroyRadonInfo(destination_cells);
815 source_cells=DestroyRadonInfo(source_cells);
816 return(MagickTrue);
817}
818
cristybb503372010-05-27 20:51:26 +0000819static void GetImageBackgroundColor(Image *image,const ssize_t offset,
cristy3ed852e2009-09-05 21:47:34 +0000820 ExceptionInfo *exception)
821{
822 CacheView
823 *image_view;
824
cristy4c08aed2011-07-01 19:47:50 +0000825 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000826 background;
827
828 MagickRealType
829 count;
830
cristyd40deb62011-03-09 00:52:27 +0000831 ssize_t
832 y;
833
cristy3ed852e2009-09-05 21:47:34 +0000834 /*
835 Compute average background color.
836 */
837 if (offset <= 0)
838 return;
cristy4c08aed2011-07-01 19:47:50 +0000839 GetPixelInfo(image,&background);
cristy3ed852e2009-09-05 21:47:34 +0000840 count=0.0;
841 image_view=AcquireCacheView(image);
cristybb503372010-05-27 20:51:26 +0000842 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000843 {
cristy4c08aed2011-07-01 19:47:50 +0000844 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000845 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000846
cristybb503372010-05-27 20:51:26 +0000847 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000848 x;
849
cristybb503372010-05-27 20:51:26 +0000850 if ((y >= offset) && (y < ((ssize_t) image->rows-offset)))
cristy3ed852e2009-09-05 21:47:34 +0000851 continue;
852 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000853 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000854 continue;
cristybb503372010-05-27 20:51:26 +0000855 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000856 {
cristybb503372010-05-27 20:51:26 +0000857 if ((x >= offset) && (x < ((ssize_t) image->columns-offset)))
cristy3ed852e2009-09-05 21:47:34 +0000858 continue;
cristy4c08aed2011-07-01 19:47:50 +0000859 background.red+=QuantumScale*GetPixelRed(image,p);
860 background.green+=QuantumScale*GetPixelGreen(image,p);
861 background.blue+=QuantumScale*GetPixelBlue(image,p);
862 background.alpha+=QuantumScale*GetPixelAlpha(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000863 count++;
cristyed231572011-07-14 02:18:59 +0000864 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000865 }
866 }
867 image_view=DestroyCacheView(image_view);
cristyce70c172010-01-07 17:15:30 +0000868 image->background_color.red=ClampToQuantum((MagickRealType) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +0000869 background.red/count);
cristyce70c172010-01-07 17:15:30 +0000870 image->background_color.green=ClampToQuantum((MagickRealType) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +0000871 background.green/count);
cristyce70c172010-01-07 17:15:30 +0000872 image->background_color.blue=ClampToQuantum((MagickRealType) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +0000873 background.blue/count);
cristy4c08aed2011-07-01 19:47:50 +0000874 image->background_color.alpha=ClampToQuantum((MagickRealType) QuantumRange*
875 background.alpha/count);
cristy3ed852e2009-09-05 21:47:34 +0000876}
877
878MagickExport Image *DeskewImage(const Image *image,const double threshold,
879 ExceptionInfo *exception)
880{
881 AffineMatrix
882 affine_matrix;
883
884 const char
885 *artifact;
886
887 double
888 degrees;
889
890 Image
891 *clone_image,
892 *crop_image,
893 *deskew_image,
894 *median_image;
895
cristy3ed852e2009-09-05 21:47:34 +0000896 MagickBooleanType
897 status;
898
899 RectangleInfo
900 geometry;
901
cristybb503372010-05-27 20:51:26 +0000902 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000903 i;
904
cristybb503372010-05-27 20:51:26 +0000905 size_t
cristy3ed852e2009-09-05 21:47:34 +0000906 max_projection,
907 *projection,
908 width;
909
cristyd40deb62011-03-09 00:52:27 +0000910 ssize_t
911 skew;
912
cristy3ed852e2009-09-05 21:47:34 +0000913 /*
914 Compute deskew angle.
915 */
916 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
cristybb503372010-05-27 20:51:26 +0000917 projection=(size_t *) AcquireQuantumMemory((size_t) (2*width-1),
cristy3ed852e2009-09-05 21:47:34 +0000918 sizeof(*projection));
cristybb503372010-05-27 20:51:26 +0000919 if (projection == (size_t *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000920 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
921 status=RadonTransform(image,threshold,projection,exception);
922 if (status == MagickFalse)
923 {
cristybb503372010-05-27 20:51:26 +0000924 projection=(size_t *) RelinquishMagickMemory(projection);
cristy3ed852e2009-09-05 21:47:34 +0000925 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
926 }
927 max_projection=0;
928 skew=0;
cristybb503372010-05-27 20:51:26 +0000929 for (i=0; i < (ssize_t) (2*width-1); i++)
cristy3ed852e2009-09-05 21:47:34 +0000930 {
931 if (projection[i] > max_projection)
932 {
cristybb503372010-05-27 20:51:26 +0000933 skew=i-(ssize_t) width+1;
cristy3ed852e2009-09-05 21:47:34 +0000934 max_projection=projection[i];
935 }
936 }
cristybb503372010-05-27 20:51:26 +0000937 projection=(size_t *) RelinquishMagickMemory(projection);
cristy3ed852e2009-09-05 21:47:34 +0000938 /*
939 Deskew image.
940 */
941 clone_image=CloneImage(image,0,0,MagickTrue,exception);
942 if (clone_image == (Image *) NULL)
943 return((Image *) NULL);
944 (void) SetImageVirtualPixelMethod(clone_image,BackgroundVirtualPixelMethod);
945 degrees=RadiansToDegrees(-atan((double) skew/width/8));
946 if (image->debug != MagickFalse)
cristy8cd5b312010-01-07 01:10:24 +0000947 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye7f51092010-01-17 00:39:37 +0000948 " Deskew angle: %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +0000949 affine_matrix.sx=cos(DegreesToRadians(fmod((double) degrees,360.0)));
950 affine_matrix.rx=sin(DegreesToRadians(fmod((double) degrees,360.0)));
951 affine_matrix.ry=(-sin(DegreesToRadians(fmod((double) degrees,360.0))));
952 affine_matrix.sy=cos(DegreesToRadians(fmod((double) degrees,360.0)));
953 affine_matrix.tx=0.0;
954 affine_matrix.ty=0.0;
955 artifact=GetImageArtifact(image,"deskew:auto-crop");
956 if (artifact == (const char *) NULL)
957 {
958 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
959 clone_image=DestroyImage(clone_image);
960 return(deskew_image);
961 }
962 /*
963 Auto-crop image.
964 */
cristyba978e12010-09-12 20:26:50 +0000965 GetImageBackgroundColor(clone_image,(ssize_t) StringToLong(artifact),
966 exception);
cristy3ed852e2009-09-05 21:47:34 +0000967 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
968 clone_image=DestroyImage(clone_image);
969 if (deskew_image == (Image *) NULL)
970 return((Image *) NULL);
cristy95c38342011-03-18 22:39:51 +0000971 median_image=StatisticImage(deskew_image,MedianStatistic,3,3,exception);
cristy3ed852e2009-09-05 21:47:34 +0000972 if (median_image == (Image *) NULL)
973 {
974 deskew_image=DestroyImage(deskew_image);
975 return((Image *) NULL);
976 }
977 geometry=GetImageBoundingBox(median_image,exception);
978 median_image=DestroyImage(median_image);
979 if (image->debug != MagickFalse)
980 (void) LogMagickEvent(TransformEvent,GetMagickModule()," Deskew geometry: "
cristy6d8abba2010-06-03 01:10:47 +0000981 "%.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
cristye8c25f92010-06-03 00:53:06 +0000982 geometry.height,(double) geometry.x,(double) geometry.y);
cristy3ed852e2009-09-05 21:47:34 +0000983 crop_image=CropImage(deskew_image,&geometry,exception);
984 deskew_image=DestroyImage(deskew_image);
985 return(crop_image);
986}
987
988/*
989%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
990% %
991% %
992% %
993+ I n t e g r a l R o t a t e I m a g e %
994% %
995% %
996% %
997%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
998%
cristy3dfa16c2010-05-17 19:34:48 +0000999% IntegralRotateImage() rotates the image an integral of 90 degrees. It
cristy3ed852e2009-09-05 21:47:34 +00001000% allocates the memory necessary for the new Image structure and returns a
1001% pointer to the rotated image.
1002%
1003% The format of the IntegralRotateImage method is:
1004%
cristybb503372010-05-27 20:51:26 +00001005% Image *IntegralRotateImage(const Image *image,size_t rotations,
cristy3ed852e2009-09-05 21:47:34 +00001006% ExceptionInfo *exception)
1007%
1008% A description of each parameter follows.
1009%
1010% o image: the image.
1011%
1012% o rotations: Specifies the number of 90 degree rotations.
1013%
1014*/
cristybb503372010-05-27 20:51:26 +00001015static Image *IntegralRotateImage(const Image *image,size_t rotations,
cristy3ed852e2009-09-05 21:47:34 +00001016 ExceptionInfo *exception)
1017{
cristy3ed852e2009-09-05 21:47:34 +00001018#define RotateImageTag "Rotate/Image"
1019
1020 CacheView
1021 *image_view,
1022 *rotate_view;
1023
1024 Image
1025 *rotate_image;
1026
cristy3ed852e2009-09-05 21:47:34 +00001027 MagickBooleanType
1028 status;
1029
cristy5f959472010-05-27 22:19:46 +00001030 MagickOffsetType
1031 progress;
1032
cristy3ed852e2009-09-05 21:47:34 +00001033 RectangleInfo
1034 page;
1035
cristy5f959472010-05-27 22:19:46 +00001036 ssize_t
1037 y;
1038
cristy3ed852e2009-09-05 21:47:34 +00001039 /*
1040 Initialize rotated image attributes.
1041 */
1042 assert(image != (Image *) NULL);
1043 page=image->page;
1044 rotations%=4;
cristyb32b90a2009-09-07 21:45:48 +00001045 if (rotations == 0)
1046 return(CloneImage(image,0,0,MagickTrue,exception));
cristy3ed852e2009-09-05 21:47:34 +00001047 if ((rotations == 1) || (rotations == 3))
1048 rotate_image=CloneImage(image,image->rows,image->columns,MagickTrue,
1049 exception);
1050 else
1051 rotate_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1052 exception);
1053 if (rotate_image == (Image *) NULL)
1054 return((Image *) NULL);
1055 /*
1056 Integral rotate the image.
1057 */
1058 status=MagickTrue;
1059 progress=0;
1060 image_view=AcquireCacheView(image);
1061 rotate_view=AcquireCacheView(rotate_image);
1062 switch (rotations)
1063 {
1064 case 0:
1065 {
1066 /*
1067 Rotate 0 degrees.
1068 */
cristy3ed852e2009-09-05 21:47:34 +00001069 break;
1070 }
1071 case 1:
1072 {
cristybb503372010-05-27 20:51:26 +00001073 size_t
cristyb32b90a2009-09-07 21:45:48 +00001074 tile_height,
1075 tile_width;
1076
cristyd40deb62011-03-09 00:52:27 +00001077 ssize_t
1078 tile_y;
1079
cristy3ed852e2009-09-05 21:47:34 +00001080 /*
1081 Rotate 90 degrees.
1082 */
cristyb32b90a2009-09-07 21:45:48 +00001083 GetPixelCacheTileSize(image,&tile_width,&tile_height);
cristy59e0d3b2010-06-07 13:12:38 +00001084#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy48012352010-10-21 16:24:59 +00001085 #pragma omp parallel for schedule(static,1) shared(progress, status) omp_throttle(1)
cristydaa97692009-09-13 02:10:35 +00001086#endif
cristyeaedf062010-05-29 22:36:02 +00001087 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
cristy3ed852e2009-09-05 21:47:34 +00001088 {
cristybb503372010-05-27 20:51:26 +00001089 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001090 tile_x;
1091
1092 if (status == MagickFalse)
1093 continue;
cristyeaedf062010-05-29 22:36:02 +00001094 for (tile_x=0; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
cristy3ed852e2009-09-05 21:47:34 +00001095 {
1096 MagickBooleanType
1097 sync;
1098
cristy4c08aed2011-07-01 19:47:50 +00001099 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001100 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001101
cristybb503372010-05-27 20:51:26 +00001102 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001103 y;
1104
cristy4c08aed2011-07-01 19:47:50 +00001105 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001106 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001107
cristybb503372010-05-27 20:51:26 +00001108 size_t
cristyb32b90a2009-09-07 21:45:48 +00001109 height,
1110 width;
cristy3ed852e2009-09-05 21:47:34 +00001111
cristyb32b90a2009-09-07 21:45:48 +00001112 width=tile_width;
cristybb503372010-05-27 20:51:26 +00001113 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
cristya45d2ec2011-08-24 13:17:19 +00001114 width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
cristyb32b90a2009-09-07 21:45:48 +00001115 height=tile_height;
cristybb503372010-05-27 20:51:26 +00001116 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
cristya45d2ec2011-08-24 13:17:19 +00001117 height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
cristydaa97692009-09-13 02:10:35 +00001118 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1119 exception);
cristy4c08aed2011-07-01 19:47:50 +00001120 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001121 {
1122 status=MagickFalse;
1123 break;
1124 }
cristybb503372010-05-27 20:51:26 +00001125 for (y=0; y < (ssize_t) width; y++)
cristy3ed852e2009-09-05 21:47:34 +00001126 {
cristy4c08aed2011-07-01 19:47:50 +00001127 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001128 *restrict tile_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001129
cristybb503372010-05-27 20:51:26 +00001130 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001131 x;
1132
cristybb503372010-05-27 20:51:26 +00001133 q=QueueCacheViewAuthenticPixels(rotate_view,(ssize_t)
cristy92611572011-07-07 16:02:42 +00001134 (rotate_image->columns-(tile_y+height)),y+tile_x,height,1,
1135 exception);
cristyacd2ed22011-08-30 01:44:23 +00001136 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001137 {
1138 status=MagickFalse;
1139 break;
1140 }
cristyed231572011-07-14 02:18:59 +00001141 tile_pixels=p+((height-1)*width+y)*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001142 for (x=0; x < (ssize_t) height; x++)
cristy3ed852e2009-09-05 21:47:34 +00001143 {
cristya45d2ec2011-08-24 13:17:19 +00001144 register ssize_t
1145 i;
1146
1147 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1148 {
1149 PixelChannel
1150 channel;
1151
1152 PixelTrait
1153 rotate_traits,
1154 traits;
1155
1156 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
cristya45d2ec2011-08-24 13:17:19 +00001157 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
1158 rotate_traits=GetPixelChannelMapTraits(rotate_image,channel);
cristy010d7d12011-08-31 01:02:48 +00001159 if ((traits == UndefinedPixelTrait) ||
1160 (rotate_traits == UndefinedPixelTrait))
cristya45d2ec2011-08-24 13:17:19 +00001161 continue;
1162 if ((rotate_traits & CopyPixelTrait) != 0)
1163 {
1164 q[channel]=tile_pixels[i];
1165 continue;
1166 }
1167 q[channel]=tile_pixels[i];
1168 }
cristyed231572011-07-14 02:18:59 +00001169 tile_pixels-=width*GetPixelChannels(image);
1170 q+=GetPixelChannels(rotate_image);
cristy3ed852e2009-09-05 21:47:34 +00001171 }
cristy3ed852e2009-09-05 21:47:34 +00001172 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1173 if (sync == MagickFalse)
1174 status=MagickFalse;
1175 }
1176 }
1177 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1178 {
1179 MagickBooleanType
1180 proceed;
1181
cristyb32b90a2009-09-07 21:45:48 +00001182 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
cristy3ed852e2009-09-05 21:47:34 +00001183 image->rows);
1184 if (proceed == MagickFalse)
1185 status=MagickFalse;
1186 }
1187 }
cristyecc2c142010-01-17 22:25:46 +00001188 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1189 image->rows-1,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001190 Swap(page.width,page.height);
1191 Swap(page.x,page.y);
1192 if (page.width != 0)
cristybb503372010-05-27 20:51:26 +00001193 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
cristy3ed852e2009-09-05 21:47:34 +00001194 break;
1195 }
1196 case 2:
1197 {
1198 /*
1199 Rotate 180 degrees.
1200 */
cristy59e0d3b2010-06-07 13:12:38 +00001201#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy09d81172010-10-21 16:15:05 +00001202 #pragma omp parallel for schedule(static,8) shared(progress,status) omp_throttle(1)
cristydaa97692009-09-13 02:10:35 +00001203#endif
cristybb503372010-05-27 20:51:26 +00001204 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001205 {
1206 MagickBooleanType
1207 sync;
1208
cristy4c08aed2011-07-01 19:47:50 +00001209 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001210 *restrict p;
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
cristy4c08aed2011-07-01 19:47:50 +00001215 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001216 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001217
1218 if (status == MagickFalse)
1219 continue;
cristya45d2ec2011-08-24 13:17:19 +00001220 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1221 q=QueueCacheViewAuthenticPixels(rotate_view,0,(ssize_t) (image->rows-y-
1222 1),image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001223 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00001224 {
1225 status=MagickFalse;
1226 continue;
1227 }
cristyed231572011-07-14 02:18:59 +00001228 q+=GetPixelChannels(rotate_image)*image->columns;
cristybb503372010-05-27 20:51:26 +00001229 for (x=0; x < (ssize_t) image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00001230 {
cristya45d2ec2011-08-24 13:17:19 +00001231 register ssize_t
1232 i;
1233
cristyed231572011-07-14 02:18:59 +00001234 q-=GetPixelChannels(rotate_image);
cristya45d2ec2011-08-24 13:17:19 +00001235 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1236 {
1237 PixelChannel
1238 channel;
1239
1240 PixelTrait
1241 rotate_traits,
1242 traits;
1243
1244 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
cristya45d2ec2011-08-24 13:17:19 +00001245 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
1246 rotate_traits=GetPixelChannelMapTraits(rotate_image,channel);
cristy010d7d12011-08-31 01:02:48 +00001247 if ((traits == UndefinedPixelTrait) ||
1248 (rotate_traits == UndefinedPixelTrait))
cristya45d2ec2011-08-24 13:17:19 +00001249 continue;
1250 if ((rotate_traits & CopyPixelTrait) != 0)
1251 {
1252 q[channel]=p[i];
1253 continue;
1254 }
1255 q[channel]=p[i];
1256 }
cristyed231572011-07-14 02:18:59 +00001257 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001258 }
cristy3ed852e2009-09-05 21:47:34 +00001259 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1260 if (sync == MagickFalse)
1261 status=MagickFalse;
1262 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1263 {
1264 MagickBooleanType
1265 proceed;
1266
1267 proceed=SetImageProgress(image,RotateImageTag,progress++,
1268 image->rows);
1269 if (proceed == MagickFalse)
1270 status=MagickFalse;
1271 }
1272 }
1273 if (page.width != 0)
cristybb503372010-05-27 20:51:26 +00001274 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
cristy3ed852e2009-09-05 21:47:34 +00001275 if (page.height != 0)
cristybb503372010-05-27 20:51:26 +00001276 page.y=(ssize_t) (page.height-rotate_image->rows-page.y);
cristy3ed852e2009-09-05 21:47:34 +00001277 break;
1278 }
1279 case 3:
1280 {
cristybb503372010-05-27 20:51:26 +00001281 size_t
cristyb32b90a2009-09-07 21:45:48 +00001282 tile_height,
1283 tile_width;
1284
cristyd40deb62011-03-09 00:52:27 +00001285 ssize_t
1286 tile_y;
1287
cristy3ed852e2009-09-05 21:47:34 +00001288 /*
1289 Rotate 270 degrees.
1290 */
cristyb32b90a2009-09-07 21:45:48 +00001291 GetPixelCacheTileSize(image,&tile_width,&tile_height);
cristy59e0d3b2010-06-07 13:12:38 +00001292#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy09d81172010-10-21 16:15:05 +00001293 #pragma omp parallel for schedule(static,1) shared(progress,status) omp_throttle(1)
cristydaa97692009-09-13 02:10:35 +00001294#endif
cristyeaedf062010-05-29 22:36:02 +00001295 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
cristy3ed852e2009-09-05 21:47:34 +00001296 {
cristybb503372010-05-27 20:51:26 +00001297 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001298 tile_x;
1299
1300 if (status == MagickFalse)
1301 continue;
cristyeaedf062010-05-29 22:36:02 +00001302 for (tile_x=0; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
cristy3ed852e2009-09-05 21:47:34 +00001303 {
1304 MagickBooleanType
1305 sync;
1306
cristy4c08aed2011-07-01 19:47:50 +00001307 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001308 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001309
cristybb503372010-05-27 20:51:26 +00001310 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001311 y;
1312
cristy4c08aed2011-07-01 19:47:50 +00001313 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001314 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001315
cristybb503372010-05-27 20:51:26 +00001316 size_t
cristyb32b90a2009-09-07 21:45:48 +00001317 height,
1318 width;
cristy3ed852e2009-09-05 21:47:34 +00001319
cristyb32b90a2009-09-07 21:45:48 +00001320 width=tile_width;
cristybb503372010-05-27 20:51:26 +00001321 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
cristya45d2ec2011-08-24 13:17:19 +00001322 width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
cristyb32b90a2009-09-07 21:45:48 +00001323 height=tile_height;
cristybb503372010-05-27 20:51:26 +00001324 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
cristya45d2ec2011-08-24 13:17:19 +00001325 height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
1326 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1327 exception);
cristy4c08aed2011-07-01 19:47:50 +00001328 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001329 {
1330 status=MagickFalse;
1331 break;
1332 }
cristybb503372010-05-27 20:51:26 +00001333 for (y=0; y < (ssize_t) width; y++)
cristy3ed852e2009-09-05 21:47:34 +00001334 {
cristy4c08aed2011-07-01 19:47:50 +00001335 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001336 *restrict tile_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001337
cristybb503372010-05-27 20:51:26 +00001338 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001339 x;
1340
cristya45d2ec2011-08-24 13:17:19 +00001341 q=QueueCacheViewAuthenticPixels(rotate_view,tile_y,(ssize_t) (y+
1342 rotate_image->rows-(tile_x+width)),height,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001343 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001344 {
1345 status=MagickFalse;
1346 break;
1347 }
cristyed231572011-07-14 02:18:59 +00001348 tile_pixels=p+((width-1)-y)*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001349 for (x=0; x < (ssize_t) height; x++)
cristy3ed852e2009-09-05 21:47:34 +00001350 {
cristya45d2ec2011-08-24 13:17:19 +00001351 register ssize_t
1352 i;
1353
1354 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1355 {
1356 PixelChannel
1357 channel;
1358
1359 PixelTrait
1360 rotate_traits,
1361 traits;
1362
1363 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
cristya45d2ec2011-08-24 13:17:19 +00001364 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
1365 rotate_traits=GetPixelChannelMapTraits(rotate_image,channel);
cristy010d7d12011-08-31 01:02:48 +00001366 if ((traits == UndefinedPixelTrait) ||
1367 (rotate_traits == UndefinedPixelTrait))
cristya45d2ec2011-08-24 13:17:19 +00001368 continue;
1369 if ((rotate_traits & CopyPixelTrait) != 0)
1370 {
1371 q[channel]=tile_pixels[i];
1372 continue;
1373 }
1374 q[channel]=tile_pixels[i];
1375 }
cristyed231572011-07-14 02:18:59 +00001376 tile_pixels+=width*GetPixelChannels(image);
1377 q+=GetPixelChannels(rotate_image);
cristy3ed852e2009-09-05 21:47:34 +00001378 }
cristy3ed852e2009-09-05 21:47:34 +00001379 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1380 if (sync == MagickFalse)
1381 status=MagickFalse;
1382 }
1383 }
1384 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1385 {
1386 MagickBooleanType
1387 proceed;
1388
cristyb32b90a2009-09-07 21:45:48 +00001389 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
cristy3ed852e2009-09-05 21:47:34 +00001390 image->rows);
1391 if (proceed == MagickFalse)
1392 status=MagickFalse;
1393 }
1394 }
cristyecc2c142010-01-17 22:25:46 +00001395 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1396 image->rows-1,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001397 Swap(page.width,page.height);
1398 Swap(page.x,page.y);
1399 if (page.height != 0)
cristybb503372010-05-27 20:51:26 +00001400 page.y=(ssize_t) (page.height-rotate_image->rows-page.y);
cristy3ed852e2009-09-05 21:47:34 +00001401 break;
1402 }
1403 }
1404 rotate_view=DestroyCacheView(rotate_view);
1405 image_view=DestroyCacheView(image_view);
1406 rotate_image->type=image->type;
1407 rotate_image->page=page;
1408 if (status == MagickFalse)
1409 rotate_image=DestroyImage(rotate_image);
1410 return(rotate_image);
1411}
1412
1413/*
1414%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1415% %
1416% %
1417% %
1418+ X S h e a r I m a g e %
1419% %
1420% %
1421% %
1422%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1423%
1424% XShearImage() shears the image in the X direction with a shear angle of
1425% 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1426% negative angles shear clockwise. Angles are measured relative to a vertical
1427% Y-axis. X shears will widen an image creating 'empty' triangles on the left
1428% and right sides of the source image.
1429%
1430% The format of the XShearImage method is:
1431%
1432% MagickBooleanType XShearImage(Image *image,const MagickRealType degrees,
cristybb503372010-05-27 20:51:26 +00001433% const size_t width,const size_t height,
1434% const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001435%
1436% A description of each parameter follows.
1437%
1438% o image: the image.
1439%
cristycee97112010-05-28 00:44:52 +00001440% o degrees: A MagickRealType representing the shearing angle along the X
cristy3ed852e2009-09-05 21:47:34 +00001441% axis.
1442%
1443% o width, height, x_offset, y_offset: Defines a region of the image
1444% to shear.
1445%
cristyecc2c142010-01-17 22:25:46 +00001446% o exception: return any errors or warnings in this structure.
1447%
cristy3ed852e2009-09-05 21:47:34 +00001448*/
1449static MagickBooleanType XShearImage(Image *image,const MagickRealType degrees,
cristybb503372010-05-27 20:51:26 +00001450 const size_t width,const size_t height,const ssize_t x_offset,
1451 const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001452{
1453#define XShearImageTag "XShear/Image"
1454
1455 typedef enum
1456 {
1457 LEFT,
1458 RIGHT
1459 } ShearDirection;
1460
1461 CacheView
1462 *image_view;
1463
cristy3ed852e2009-09-05 21:47:34 +00001464 MagickBooleanType
1465 status;
1466
cristy5f959472010-05-27 22:19:46 +00001467 MagickOffsetType
1468 progress;
1469
cristy4c08aed2011-07-01 19:47:50 +00001470 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001471 background;
1472
cristy5f959472010-05-27 22:19:46 +00001473 ssize_t
1474 y;
1475
cristy3ed852e2009-09-05 21:47:34 +00001476 assert(image != (Image *) NULL);
1477 assert(image->signature == MagickSignature);
1478 if (image->debug != MagickFalse)
1479 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy4c08aed2011-07-01 19:47:50 +00001480 GetPixelInfo(image,&background);
1481 SetPixelInfoPacket(image,&image->background_color,&background);
cristy3ed852e2009-09-05 21:47:34 +00001482 if (image->colorspace == CMYKColorspace)
1483 ConvertRGBToCMYK(&background);
1484 /*
cristy26a3acd2011-03-09 00:50:29 +00001485 X shear image.
cristy3ed852e2009-09-05 21:47:34 +00001486 */
1487 status=MagickTrue;
1488 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00001489 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00001490#if defined(MAGICKCORE_OPENMP_SUPPORT)
1491 #pragma omp parallel for schedule(dynamic,4) shared(progress, status)
cristy3ed852e2009-09-05 21:47:34 +00001492#endif
cristybb503372010-05-27 20:51:26 +00001493 for (y=0; y < (ssize_t) height; y++)
cristy3ed852e2009-09-05 21:47:34 +00001494 {
cristy4c08aed2011-07-01 19:47:50 +00001495 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001496 pixel,
1497 source,
1498 destination;
1499
1500 MagickRealType
1501 area,
1502 displacement;
1503
cristy4c08aed2011-07-01 19:47:50 +00001504 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001505 *restrict p,
1506 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001507
cristyd40deb62011-03-09 00:52:27 +00001508 register ssize_t
1509 i;
1510
cristy3ed852e2009-09-05 21:47:34 +00001511 ShearDirection
1512 direction;
1513
cristyd40deb62011-03-09 00:52:27 +00001514 ssize_t
1515 step;
1516
cristy3ed852e2009-09-05 21:47:34 +00001517 if (status == MagickFalse)
1518 continue;
1519 p=GetCacheViewAuthenticPixels(image_view,0,y_offset+y,image->columns,1,
1520 exception);
cristy4c08aed2011-07-01 19:47:50 +00001521 if (p == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001522 {
1523 status=MagickFalse;
1524 continue;
1525 }
cristyed231572011-07-14 02:18:59 +00001526 p+=x_offset*GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001527 displacement=degrees*(MagickRealType) (y-height/2.0);
1528 if (displacement == 0.0)
1529 continue;
1530 if (displacement > 0.0)
1531 direction=RIGHT;
1532 else
1533 {
1534 displacement*=(-1.0);
1535 direction=LEFT;
1536 }
cristybb503372010-05-27 20:51:26 +00001537 step=(ssize_t) floor((double) displacement);
cristy3ed852e2009-09-05 21:47:34 +00001538 area=(MagickRealType) (displacement-step);
1539 step++;
1540 pixel=background;
cristy4c08aed2011-07-01 19:47:50 +00001541 GetPixelInfo(image,&source);
1542 GetPixelInfo(image,&destination);
cristy3ed852e2009-09-05 21:47:34 +00001543 switch (direction)
1544 {
1545 case LEFT:
1546 {
1547 /*
1548 Transfer pixels left-to-right.
1549 */
1550 if (step > x_offset)
1551 break;
cristyed231572011-07-14 02:18:59 +00001552 q=p-step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001553 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001554 {
1555 if ((x_offset+i) < step)
1556 {
cristyed231572011-07-14 02:18:59 +00001557 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001558 SetPixelInfo(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001559 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001560 continue;
1561 }
cristy4c08aed2011-07-01 19:47:50 +00001562 SetPixelInfo(image,p,&source);
1563 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1564 &source,(MagickRealType) GetPixelAlpha(image,p),area,
1565 &destination);
1566 SetPixelPixelInfo(image,&destination,q);
1567 SetPixelInfo(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001568 p+=GetPixelChannels(image);
1569 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001570 }
cristy4c08aed2011-07-01 19:47:50 +00001571 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1572 &background,(MagickRealType) background.alpha,area,&destination);
1573 SetPixelPixelInfo(image,&destination,q);
cristyed231572011-07-14 02:18:59 +00001574 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001575 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001576 {
1577 SetPixelPixelInfo(image,&background,q);
cristyed231572011-07-14 02:18:59 +00001578 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001579 }
cristy3ed852e2009-09-05 21:47:34 +00001580 break;
1581 }
1582 case RIGHT:
1583 {
1584 /*
1585 Transfer pixels right-to-left.
1586 */
cristyed231572011-07-14 02:18:59 +00001587 p+=width*GetPixelChannels(image);
1588 q=p+step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001589 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001590 {
cristyed231572011-07-14 02:18:59 +00001591 p-=GetPixelChannels(image);
1592 q-=GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001593 if ((size_t) (x_offset+width+step-i) >= image->columns)
cristy3ed852e2009-09-05 21:47:34 +00001594 continue;
cristy4c08aed2011-07-01 19:47:50 +00001595 SetPixelInfo(image,p,&source);
1596 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1597 &source,(MagickRealType) GetPixelAlpha(image,p),area,
1598 &destination);
1599 SetPixelPixelInfo(image,&destination,q);
1600 SetPixelInfo(image,p,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001601 }
cristy4c08aed2011-07-01 19:47:50 +00001602 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1603 &background,(MagickRealType) background.alpha,area,&destination);
cristyed231572011-07-14 02:18:59 +00001604 q-=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001605 SetPixelPixelInfo(image,&destination,q);
cristy3ed852e2009-09-05 21:47:34 +00001606 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001607 {
cristyed231572011-07-14 02:18:59 +00001608 q-=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001609 SetPixelPixelInfo(image,&background,q);
1610 }
cristy3ed852e2009-09-05 21:47:34 +00001611 break;
1612 }
1613 }
1614 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1615 status=MagickFalse;
1616 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1617 {
1618 MagickBooleanType
1619 proceed;
1620
cristyb5d5f722009-11-04 03:03:49 +00001621#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00001622 #pragma omp critical (MagickCore_XShearImage)
1623#endif
1624 proceed=SetImageProgress(image,XShearImageTag,progress++,height);
1625 if (proceed == MagickFalse)
1626 status=MagickFalse;
1627 }
1628 }
1629 image_view=DestroyCacheView(image_view);
1630 return(status);
1631}
1632
1633/*
1634%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1635% %
1636% %
1637% %
1638+ Y S h e a r I m a g e %
1639% %
1640% %
1641% %
1642%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1643%
1644% YShearImage shears the image in the Y direction with a shear angle of
1645% 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1646% negative angles shear clockwise. Angles are measured relative to a
1647% horizontal X-axis. Y shears will increase the height of an image creating
1648% 'empty' triangles on the top and bottom of the source image.
1649%
1650% The format of the YShearImage method is:
1651%
1652% MagickBooleanType YShearImage(Image *image,const MagickRealType degrees,
cristybb503372010-05-27 20:51:26 +00001653% const size_t width,const size_t height,
1654% const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001655%
1656% A description of each parameter follows.
1657%
1658% o image: the image.
1659%
cristycee97112010-05-28 00:44:52 +00001660% o degrees: A MagickRealType representing the shearing angle along the Y
cristy3ed852e2009-09-05 21:47:34 +00001661% axis.
1662%
1663% o width, height, x_offset, y_offset: Defines a region of the image
1664% to shear.
1665%
cristyecc2c142010-01-17 22:25:46 +00001666% o exception: return any errors or warnings in this structure.
1667%
cristy3ed852e2009-09-05 21:47:34 +00001668*/
1669static MagickBooleanType YShearImage(Image *image,const MagickRealType degrees,
cristybb503372010-05-27 20:51:26 +00001670 const size_t width,const size_t height,const ssize_t x_offset,
1671 const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001672{
1673#define YShearImageTag "YShear/Image"
1674
1675 typedef enum
1676 {
1677 UP,
1678 DOWN
1679 } ShearDirection;
1680
1681 CacheView
1682 *image_view;
1683
cristy3ed852e2009-09-05 21:47:34 +00001684 MagickBooleanType
1685 status;
1686
cristy5f959472010-05-27 22:19:46 +00001687 MagickOffsetType
1688 progress;
1689
cristy4c08aed2011-07-01 19:47:50 +00001690 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001691 background;
1692
cristy5f959472010-05-27 22:19:46 +00001693 ssize_t
1694 x;
1695
cristy3ed852e2009-09-05 21:47:34 +00001696 assert(image != (Image *) NULL);
1697 assert(image->signature == MagickSignature);
1698 if (image->debug != MagickFalse)
1699 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy4c08aed2011-07-01 19:47:50 +00001700 GetPixelInfo(image,&background);
1701 SetPixelInfoPacket(image,&image->background_color,&background);
cristy3ed852e2009-09-05 21:47:34 +00001702 if (image->colorspace == CMYKColorspace)
1703 ConvertRGBToCMYK(&background);
1704 /*
1705 Y Shear image.
1706 */
1707 status=MagickTrue;
1708 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00001709 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00001710#if defined(MAGICKCORE_OPENMP_SUPPORT)
1711 #pragma omp parallel for schedule(dynamic,4) shared(progress, status)
cristy3ed852e2009-09-05 21:47:34 +00001712#endif
cristybb503372010-05-27 20:51:26 +00001713 for (x=0; x < (ssize_t) width; x++)
cristy3ed852e2009-09-05 21:47:34 +00001714 {
cristybb503372010-05-27 20:51:26 +00001715 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001716 step;
1717
cristy3ed852e2009-09-05 21:47:34 +00001718 MagickRealType
1719 area,
1720 displacement;
1721
cristy4c08aed2011-07-01 19:47:50 +00001722 PixelInfo
1723 pixel,
1724 source,
1725 destination;
1726
1727 register Quantum
1728 *restrict p,
1729 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001730
cristybb503372010-05-27 20:51:26 +00001731 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001732 i;
1733
cristy3ed852e2009-09-05 21:47:34 +00001734 ShearDirection
1735 direction;
1736
1737 if (status == MagickFalse)
1738 continue;
1739 p=GetCacheViewAuthenticPixels(image_view,x_offset+x,0,1,image->rows,
1740 exception);
cristy4c08aed2011-07-01 19:47:50 +00001741 if (p == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001742 {
1743 status=MagickFalse;
1744 continue;
1745 }
cristyed231572011-07-14 02:18:59 +00001746 p+=y_offset*GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001747 displacement=degrees*(MagickRealType) (x-width/2.0);
1748 if (displacement == 0.0)
1749 continue;
1750 if (displacement > 0.0)
1751 direction=DOWN;
1752 else
1753 {
1754 displacement*=(-1.0);
1755 direction=UP;
1756 }
cristybb503372010-05-27 20:51:26 +00001757 step=(ssize_t) floor((double) displacement);
cristy3ed852e2009-09-05 21:47:34 +00001758 area=(MagickRealType) (displacement-step);
1759 step++;
1760 pixel=background;
cristy4c08aed2011-07-01 19:47:50 +00001761 GetPixelInfo(image,&source);
1762 GetPixelInfo(image,&destination);
cristy3ed852e2009-09-05 21:47:34 +00001763 switch (direction)
1764 {
1765 case UP:
1766 {
1767 /*
1768 Transfer pixels top-to-bottom.
1769 */
1770 if (step > y_offset)
1771 break;
cristyed231572011-07-14 02:18:59 +00001772 q=p-step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001773 for (i=0; i < (ssize_t) height; i++)
cristy3ed852e2009-09-05 21:47:34 +00001774 {
1775 if ((y_offset+i) < step)
1776 {
cristyed231572011-07-14 02:18:59 +00001777 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001778 SetPixelInfo(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001779 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001780 continue;
1781 }
cristy4c08aed2011-07-01 19:47:50 +00001782 SetPixelInfo(image,p,&source);
1783 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1784 &source,(MagickRealType) GetPixelAlpha(image,p),area,
1785 &destination);
1786 SetPixelPixelInfo(image,&destination,q);
1787 SetPixelInfo(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001788 p+=GetPixelChannels(image);
1789 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001790 }
cristy4c08aed2011-07-01 19:47:50 +00001791 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1792 &background,(MagickRealType) background.alpha,area,&destination);
1793 SetPixelPixelInfo(image,&destination,q);
cristyed231572011-07-14 02:18:59 +00001794 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001795 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001796 {
1797 SetPixelPixelInfo(image,&background,q);
cristyed231572011-07-14 02:18:59 +00001798 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001799 }
cristy3ed852e2009-09-05 21:47:34 +00001800 break;
1801 }
1802 case DOWN:
1803 {
1804 /*
1805 Transfer pixels bottom-to-top.
1806 */
cristyed231572011-07-14 02:18:59 +00001807 p+=height*GetPixelChannels(image);
1808 q=p+step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001809 for (i=0; i < (ssize_t) height; i++)
cristy3ed852e2009-09-05 21:47:34 +00001810 {
cristyed231572011-07-14 02:18:59 +00001811 p-=GetPixelChannels(image);
1812 q-=GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001813 if ((size_t) (y_offset+height+step-i) >= image->rows)
cristy3ed852e2009-09-05 21:47:34 +00001814 continue;
cristy4c08aed2011-07-01 19:47:50 +00001815 SetPixelInfo(image,p,&source);
1816 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1817 &source,(MagickRealType) GetPixelAlpha(image,p),area,
1818 &destination);
1819 SetPixelPixelInfo(image,&destination,q);
1820 SetPixelInfo(image,p,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001821 }
cristy4c08aed2011-07-01 19:47:50 +00001822 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1823 &background,(MagickRealType) background.alpha,area,&destination);
cristyed231572011-07-14 02:18:59 +00001824 q-=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001825 SetPixelPixelInfo(image,&destination,q);
cristy3ed852e2009-09-05 21:47:34 +00001826 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001827 {
cristyed231572011-07-14 02:18:59 +00001828 q-=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001829 SetPixelPixelInfo(image,&background,q);
1830 }
cristy3ed852e2009-09-05 21:47:34 +00001831 break;
1832 }
1833 }
1834 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1835 status=MagickFalse;
1836 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1837 {
1838 MagickBooleanType
1839 proceed;
1840
cristyb5d5f722009-11-04 03:03:49 +00001841#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00001842 #pragma omp critical (MagickCore_YShearImage)
1843#endif
1844 proceed=SetImageProgress(image,YShearImageTag,progress++,image->rows);
1845 if (proceed == MagickFalse)
1846 status=MagickFalse;
1847 }
1848 }
1849 image_view=DestroyCacheView(image_view);
1850 return(status);
1851}
1852
1853/*
1854%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1855% %
1856% %
1857% %
1858% R o t a t e I m a g e %
1859% %
1860% %
1861% %
1862%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1863%
1864% RotateImage() creates a new image that is a rotated copy of an existing
1865% one. Positive angles rotate counter-clockwise (right-hand rule), while
1866% negative angles rotate clockwise. Rotated images are usually larger than
1867% the originals and have 'empty' triangular corners. X axis. Empty
1868% triangles left over from shearing the image are filled with the background
1869% color defined by member 'background_color' of the image. RotateImage
1870% allocates the memory necessary for the new Image structure and returns a
1871% pointer to the new image.
1872%
1873% RotateImage() is based on the paper "A Fast Algorithm for General
1874% Raster Rotatation" by Alan W. Paeth. RotateImage is adapted from a similar
1875% method based on the Paeth paper written by Michael Halle of the Spatial
1876% Imaging Group, MIT Media Lab.
1877%
1878% The format of the RotateImage method is:
1879%
1880% Image *RotateImage(const Image *image,const double degrees,
1881% ExceptionInfo *exception)
1882%
1883% A description of each parameter follows.
1884%
1885% o image: the image.
1886%
1887% o degrees: Specifies the number of degrees to rotate the image.
1888%
1889% o exception: return any errors or warnings in this structure.
1890%
1891*/
1892MagickExport Image *RotateImage(const Image *image,const double degrees,
1893 ExceptionInfo *exception)
1894{
1895 Image
1896 *integral_image,
1897 *rotate_image;
1898
cristyecc2c142010-01-17 22:25:46 +00001899 MagickBooleanType
1900 status;
1901
cristy3ed852e2009-09-05 21:47:34 +00001902 MagickRealType
1903 angle;
1904
1905 PointInfo
1906 shear;
1907
1908 RectangleInfo
1909 border_info;
1910
cristybb503372010-05-27 20:51:26 +00001911 size_t
cristy3ed852e2009-09-05 21:47:34 +00001912 height,
1913 rotations,
1914 width,
1915 y_width;
1916
cristy4c08aed2011-07-01 19:47:50 +00001917 ssize_t
1918 x_offset,
1919 y_offset;
1920
cristy3ed852e2009-09-05 21:47:34 +00001921 /*
1922 Adjust rotation angle.
1923 */
1924 assert(image != (Image *) NULL);
1925 assert(image->signature == MagickSignature);
1926 if (image->debug != MagickFalse)
1927 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1928 assert(exception != (ExceptionInfo *) NULL);
1929 assert(exception->signature == MagickSignature);
1930 angle=degrees;
1931 while (angle < -45.0)
1932 angle+=360.0;
1933 for (rotations=0; angle > 45.0; rotations++)
1934 angle-=90.0;
1935 rotations%=4;
1936 /*
1937 Calculate shear equations.
1938 */
1939 integral_image=IntegralRotateImage(image,rotations,exception);
1940 if (integral_image == (Image *) NULL)
1941 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1942 shear.x=(-tan((double) DegreesToRadians(angle)/2.0));
1943 shear.y=sin((double) DegreesToRadians(angle));
1944 if ((shear.x == 0.0) && (shear.y == 0.0))
1945 return(integral_image);
cristy574cc262011-08-05 01:23:58 +00001946 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001947 {
cristy3ed852e2009-09-05 21:47:34 +00001948 integral_image=DestroyImage(integral_image);
1949 return(integral_image);
1950 }
1951 if (integral_image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00001952 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00001953 /*
1954 Compute image size.
1955 */
1956 width=image->columns;
1957 height=image->rows;
1958 if ((rotations == 1) || (rotations == 3))
1959 {
1960 width=image->rows;
1961 height=image->columns;
1962 }
cristybb503372010-05-27 20:51:26 +00001963 y_width=width+(ssize_t) floor(fabs(shear.x)*height+0.5);
cristycee97112010-05-28 00:44:52 +00001964 x_offset=(ssize_t) ceil((double) width+((fabs(shear.y)*height)-width)/2.0-
1965 0.5);
1966 y_offset=(ssize_t) ceil((double) height+((fabs(shear.y)*y_width)-height)/2.0-
1967 0.5);
cristy3ed852e2009-09-05 21:47:34 +00001968 /*
1969 Surround image with a border.
1970 */
1971 integral_image->border_color=integral_image->background_color;
1972 integral_image->compose=CopyCompositeOp;
cristybb503372010-05-27 20:51:26 +00001973 border_info.width=(size_t) x_offset;
1974 border_info.height=(size_t) y_offset;
cristy3ed852e2009-09-05 21:47:34 +00001975 rotate_image=BorderImage(integral_image,&border_info,exception);
1976 integral_image=DestroyImage(integral_image);
1977 if (rotate_image == (Image *) NULL)
1978 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1979 /*
1980 Rotate the image.
1981 */
cristyeaedf062010-05-29 22:36:02 +00001982 status=XShearImage(rotate_image,shear.x,width,height,x_offset,(ssize_t)
1983 (rotate_image->rows-height)/2,exception);
cristyecc2c142010-01-17 22:25:46 +00001984 if (status == MagickFalse)
1985 {
1986 rotate_image=DestroyImage(rotate_image);
1987 return((Image *) NULL);
1988 }
cristyeaedf062010-05-29 22:36:02 +00001989 status=YShearImage(rotate_image,shear.y,y_width,height,(ssize_t)
1990 (rotate_image->columns-y_width)/2,y_offset,exception);
cristyecc2c142010-01-17 22:25:46 +00001991 if (status == MagickFalse)
1992 {
1993 rotate_image=DestroyImage(rotate_image);
1994 return((Image *) NULL);
1995 }
cristyeaedf062010-05-29 22:36:02 +00001996 status=XShearImage(rotate_image,shear.x,y_width,rotate_image->rows,(ssize_t)
1997 (rotate_image->columns-y_width)/2,0,exception);
cristyecc2c142010-01-17 22:25:46 +00001998 if (status == MagickFalse)
1999 {
2000 rotate_image=DestroyImage(rotate_image);
2001 return((Image *) NULL);
2002 }
2003 status=CropToFitImage(&rotate_image,shear.x,shear.y,(MagickRealType) width,
cristy3ed852e2009-09-05 21:47:34 +00002004 (MagickRealType) height,MagickTrue,exception);
cristyecc2c142010-01-17 22:25:46 +00002005 if (status == MagickFalse)
2006 {
2007 rotate_image=DestroyImage(rotate_image);
2008 return((Image *) NULL);
2009 }
cristy3ed852e2009-09-05 21:47:34 +00002010 rotate_image->compose=image->compose;
2011 rotate_image->page.width=0;
2012 rotate_image->page.height=0;
2013 return(rotate_image);
2014}
2015
2016/*
2017%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2018% %
2019% %
2020% %
2021% S h e a r I m a g e %
2022% %
2023% %
2024% %
2025%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2026%
2027% ShearImage() creates a new image that is a shear_image copy of an existing
cristycee97112010-05-28 00:44:52 +00002028% one. Shearing slides one edge of an image along the X or Y axis, creating
2029% a parallelogram. An X direction shear slides an edge along the X axis,
2030% while a Y direction shear slides an edge along the Y axis. The amount of
cristy3ed852e2009-09-05 21:47:34 +00002031% the shear is controlled by a shear angle. For X direction shears, x_shear
2032% is measured relative to the Y axis, and similarly, for Y direction shears
2033% y_shear is measured relative to the X axis. Empty triangles left over from
2034% shearing the image are filled with the background color defined by member
2035% 'background_color' of the image.. ShearImage() allocates the memory
2036% necessary for the new Image structure and returns a pointer to the new image.
2037%
2038% ShearImage() is based on the paper "A Fast Algorithm for General Raster
2039% Rotatation" by Alan W. Paeth.
2040%
2041% The format of the ShearImage method is:
2042%
2043% Image *ShearImage(const Image *image,const double x_shear,
2044% const double y_shear,ExceptionInfo *exception)
2045%
2046% A description of each parameter follows.
2047%
2048% o image: the image.
2049%
2050% o x_shear, y_shear: Specifies the number of degrees to shear the image.
2051%
2052% o exception: return any errors or warnings in this structure.
2053%
2054*/
2055MagickExport Image *ShearImage(const Image *image,const double x_shear,
2056 const double y_shear,ExceptionInfo *exception)
2057{
2058 Image
2059 *integral_image,
2060 *shear_image;
2061
cristybb503372010-05-27 20:51:26 +00002062 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002063 x_offset,
2064 y_offset;
2065
cristyecc2c142010-01-17 22:25:46 +00002066 MagickBooleanType
2067 status;
2068
cristy3ed852e2009-09-05 21:47:34 +00002069 PointInfo
2070 shear;
2071
2072 RectangleInfo
2073 border_info;
2074
cristybb503372010-05-27 20:51:26 +00002075 size_t
cristy3ed852e2009-09-05 21:47:34 +00002076 y_width;
2077
2078 assert(image != (Image *) NULL);
2079 assert(image->signature == MagickSignature);
2080 if (image->debug != MagickFalse)
2081 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2082 assert(exception != (ExceptionInfo *) NULL);
2083 assert(exception->signature == MagickSignature);
2084 if ((x_shear != 0.0) && (fmod(x_shear,90.0) == 0.0))
2085 ThrowImageException(ImageError,"AngleIsDiscontinuous");
2086 if ((y_shear != 0.0) && (fmod(y_shear,90.0) == 0.0))
2087 ThrowImageException(ImageError,"AngleIsDiscontinuous");
2088 /*
2089 Initialize shear angle.
2090 */
2091 integral_image=CloneImage(image,0,0,MagickTrue,exception);
2092 if (integral_image == (Image *) NULL)
2093 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2094 shear.x=(-tan(DegreesToRadians(fmod(x_shear,360.0))));
2095 shear.y=tan(DegreesToRadians(fmod(y_shear,360.0)));
2096 if ((shear.x == 0.0) && (shear.y == 0.0))
2097 return(integral_image);
cristy574cc262011-08-05 01:23:58 +00002098 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00002099 {
cristy3ed852e2009-09-05 21:47:34 +00002100 integral_image=DestroyImage(integral_image);
2101 return(integral_image);
2102 }
2103 if (integral_image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00002104 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00002105 /*
2106 Compute image size.
2107 */
cristybb503372010-05-27 20:51:26 +00002108 y_width=image->columns+(ssize_t) floor(fabs(shear.x)*image->rows+0.5);
cristycee97112010-05-28 00:44:52 +00002109 x_offset=(ssize_t) ceil((double) image->columns+((fabs(shear.x)*image->rows)-
cristy06609ee2010-03-17 20:21:27 +00002110 image->columns)/2.0-0.5);
cristycee97112010-05-28 00:44:52 +00002111 y_offset=(ssize_t) ceil((double) image->rows+((fabs(shear.y)*y_width)-
2112 image->rows)/2.0-0.5);
cristy3ed852e2009-09-05 21:47:34 +00002113 /*
2114 Surround image with border.
2115 */
2116 integral_image->border_color=integral_image->background_color;
2117 integral_image->compose=CopyCompositeOp;
cristybb503372010-05-27 20:51:26 +00002118 border_info.width=(size_t) x_offset;
2119 border_info.height=(size_t) y_offset;
cristy3ed852e2009-09-05 21:47:34 +00002120 shear_image=BorderImage(integral_image,&border_info,exception);
cristyecc2c142010-01-17 22:25:46 +00002121 integral_image=DestroyImage(integral_image);
cristy3ed852e2009-09-05 21:47:34 +00002122 if (shear_image == (Image *) NULL)
2123 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +00002124 /*
2125 Shear the image.
2126 */
2127 if (shear_image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00002128 (void) SetImageAlphaChannel(shear_image,OpaqueAlphaChannel,exception);
cristyecc2c142010-01-17 22:25:46 +00002129 status=XShearImage(shear_image,shear.x,image->columns,image->rows,x_offset,
cristyeaedf062010-05-29 22:36:02 +00002130 (ssize_t) (shear_image->rows-image->rows)/2,exception);
cristyecc2c142010-01-17 22:25:46 +00002131 if (status == MagickFalse)
2132 {
2133 shear_image=DestroyImage(shear_image);
2134 return((Image *) NULL);
2135 }
cristyeaedf062010-05-29 22:36:02 +00002136 status=YShearImage(shear_image,shear.y,y_width,image->rows,(ssize_t)
2137 (shear_image->columns-y_width)/2,y_offset,exception);
cristyecc2c142010-01-17 22:25:46 +00002138 if (status == MagickFalse)
2139 {
2140 shear_image=DestroyImage(shear_image);
2141 return((Image *) NULL);
2142 }
2143 status=CropToFitImage(&shear_image,shear.x,shear.y,(MagickRealType)
2144 image->columns,(MagickRealType) image->rows,MagickFalse,exception);
2145 if (status == MagickFalse)
2146 {
2147 shear_image=DestroyImage(shear_image);
2148 return((Image *) NULL);
2149 }
cristy3ed852e2009-09-05 21:47:34 +00002150 shear_image->compose=image->compose;
2151 shear_image->page.width=0;
2152 shear_image->page.height=0;
2153 return(shear_image);
2154}