blob: 50a4bd30a48fb8a41b66e8b3e6647869382a7242 [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;
cristy26295322011-09-22 00:50:36 +0000742 if (GetPixelIntensity(image,p) < threshold)
cristy3ed852e2009-09-05 21:47:34 +0000743 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 }
cristyed231572011-07-14 02:18:59 +0000751 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000752 }
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 {
cristy4c08aed2011-07-01 19:47:50 +0000766 register const Quantum
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);
cristy4c08aed2011-07-01 19:47:50 +0000780 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000781 {
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;
cristy26295322011-09-22 00:50:36 +0000791 if (GetPixelIntensity(image,p) < threshold)
cristy3ed852e2009-09-05 21:47:34 +0000792 byte|=0x01;
793 bit++;
794 if (bit == 8)
795 {
796 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
797 bit=0;
798 byte=0;
799 }
cristyed231572011-07-14 02:18:59 +0000800 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000801 }
802 if (bit != 0)
803 {
804 byte<<=(8-bit);
805 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
806 }
807 }
808 RadonProjection(source_cells,destination_cells,1,projection);
809 image_view=DestroyCacheView(image_view);
810 destination_cells=DestroyRadonInfo(destination_cells);
811 source_cells=DestroyRadonInfo(source_cells);
812 return(MagickTrue);
813}
814
cristybb503372010-05-27 20:51:26 +0000815static void GetImageBackgroundColor(Image *image,const ssize_t offset,
cristy3ed852e2009-09-05 21:47:34 +0000816 ExceptionInfo *exception)
817{
818 CacheView
819 *image_view;
820
cristy4c08aed2011-07-01 19:47:50 +0000821 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000822 background;
823
824 MagickRealType
825 count;
826
cristyd40deb62011-03-09 00:52:27 +0000827 ssize_t
828 y;
829
cristy3ed852e2009-09-05 21:47:34 +0000830 /*
831 Compute average background color.
832 */
833 if (offset <= 0)
834 return;
cristy4c08aed2011-07-01 19:47:50 +0000835 GetPixelInfo(image,&background);
cristy3ed852e2009-09-05 21:47:34 +0000836 count=0.0;
837 image_view=AcquireCacheView(image);
cristybb503372010-05-27 20:51:26 +0000838 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000839 {
cristy4c08aed2011-07-01 19:47:50 +0000840 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000841 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000842
cristybb503372010-05-27 20:51:26 +0000843 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000844 x;
845
cristybb503372010-05-27 20:51:26 +0000846 if ((y >= offset) && (y < ((ssize_t) image->rows-offset)))
cristy3ed852e2009-09-05 21:47:34 +0000847 continue;
848 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000849 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000850 continue;
cristybb503372010-05-27 20:51:26 +0000851 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000852 {
cristybb503372010-05-27 20:51:26 +0000853 if ((x >= offset) && (x < ((ssize_t) image->columns-offset)))
cristy3ed852e2009-09-05 21:47:34 +0000854 continue;
cristy4c08aed2011-07-01 19:47:50 +0000855 background.red+=QuantumScale*GetPixelRed(image,p);
856 background.green+=QuantumScale*GetPixelGreen(image,p);
857 background.blue+=QuantumScale*GetPixelBlue(image,p);
cristy26295322011-09-22 00:50:36 +0000858 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
859 background.alpha+=QuantumScale*GetPixelAlpha(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000860 count++;
cristyed231572011-07-14 02:18:59 +0000861 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000862 }
863 }
864 image_view=DestroyCacheView(image_view);
cristyce70c172010-01-07 17:15:30 +0000865 image->background_color.red=ClampToQuantum((MagickRealType) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +0000866 background.red/count);
cristyce70c172010-01-07 17:15:30 +0000867 image->background_color.green=ClampToQuantum((MagickRealType) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +0000868 background.green/count);
cristyce70c172010-01-07 17:15:30 +0000869 image->background_color.blue=ClampToQuantum((MagickRealType) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +0000870 background.blue/count);
cristy26295322011-09-22 00:50:36 +0000871 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
872 image->background_color.alpha=ClampToQuantum((MagickRealType) QuantumRange*
873 background.alpha/count);
cristy3ed852e2009-09-05 21:47:34 +0000874}
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
cristy3ed852e2009-09-05 21:47:34 +0000894 MagickBooleanType
895 status;
896
897 RectangleInfo
898 geometry;
899
cristybb503372010-05-27 20:51:26 +0000900 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000901 i;
902
cristybb503372010-05-27 20:51:26 +0000903 size_t
cristy3ed852e2009-09-05 21:47:34 +0000904 max_projection,
905 *projection,
906 width;
907
cristyd40deb62011-03-09 00:52:27 +0000908 ssize_t
909 skew;
910
cristy3ed852e2009-09-05 21:47:34 +0000911 /*
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 */
cristyba978e12010-09-12 20:26:50 +0000963 GetImageBackgroundColor(clone_image,(ssize_t) StringToLong(artifact),
964 exception);
cristy3ed852e2009-09-05 21:47:34 +0000965 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
966 clone_image=DestroyImage(clone_image);
967 if (deskew_image == (Image *) NULL)
968 return((Image *) NULL);
cristy95c38342011-03-18 22:39:51 +0000969 median_image=StatisticImage(deskew_image,MedianStatistic,3,3,exception);
cristy3ed852e2009-09-05 21:47:34 +0000970 if (median_image == (Image *) NULL)
971 {
972 deskew_image=DestroyImage(deskew_image);
973 return((Image *) NULL);
974 }
975 geometry=GetImageBoundingBox(median_image,exception);
976 median_image=DestroyImage(median_image);
977 if (image->debug != MagickFalse)
978 (void) LogMagickEvent(TransformEvent,GetMagickModule()," Deskew geometry: "
cristy6d8abba2010-06-03 01:10:47 +0000979 "%.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
cristye8c25f92010-06-03 00:53:06 +0000980 geometry.height,(double) geometry.x,(double) geometry.y);
cristy3ed852e2009-09-05 21:47:34 +0000981 crop_image=CropImage(deskew_image,&geometry,exception);
982 deskew_image=DestroyImage(deskew_image);
983 return(crop_image);
984}
985
986/*
987%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
988% %
989% %
990% %
991+ I n t e g r a l R o t a t e I m a g e %
992% %
993% %
994% %
995%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
996%
cristy3dfa16c2010-05-17 19:34:48 +0000997% IntegralRotateImage() rotates the image an integral of 90 degrees. It
cristy3ed852e2009-09-05 21:47:34 +0000998% allocates the memory necessary for the new Image structure and returns a
999% pointer to the rotated image.
1000%
1001% The format of the IntegralRotateImage method is:
1002%
cristybb503372010-05-27 20:51:26 +00001003% Image *IntegralRotateImage(const Image *image,size_t rotations,
cristy3ed852e2009-09-05 21:47:34 +00001004% ExceptionInfo *exception)
1005%
1006% A description of each parameter follows.
1007%
1008% o image: the image.
1009%
1010% o rotations: Specifies the number of 90 degree rotations.
1011%
1012*/
cristybb503372010-05-27 20:51:26 +00001013static Image *IntegralRotateImage(const Image *image,size_t rotations,
cristy3ed852e2009-09-05 21:47:34 +00001014 ExceptionInfo *exception)
1015{
cristy3ed852e2009-09-05 21:47:34 +00001016#define RotateImageTag "Rotate/Image"
1017
1018 CacheView
1019 *image_view,
1020 *rotate_view;
1021
1022 Image
1023 *rotate_image;
1024
cristy3ed852e2009-09-05 21:47:34 +00001025 MagickBooleanType
1026 status;
1027
cristy5f959472010-05-27 22:19:46 +00001028 MagickOffsetType
1029 progress;
1030
cristy3ed852e2009-09-05 21:47:34 +00001031 RectangleInfo
1032 page;
1033
cristy5f959472010-05-27 22:19:46 +00001034 ssize_t
1035 y;
1036
cristy3ed852e2009-09-05 21:47:34 +00001037 /*
1038 Initialize rotated image attributes.
1039 */
1040 assert(image != (Image *) NULL);
1041 page=image->page;
1042 rotations%=4;
cristyb32b90a2009-09-07 21:45:48 +00001043 if (rotations == 0)
1044 return(CloneImage(image,0,0,MagickTrue,exception));
cristy3ed852e2009-09-05 21:47:34 +00001045 if ((rotations == 1) || (rotations == 3))
1046 rotate_image=CloneImage(image,image->rows,image->columns,MagickTrue,
1047 exception);
1048 else
1049 rotate_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1050 exception);
1051 if (rotate_image == (Image *) NULL)
1052 return((Image *) NULL);
1053 /*
1054 Integral rotate the image.
1055 */
1056 status=MagickTrue;
1057 progress=0;
1058 image_view=AcquireCacheView(image);
1059 rotate_view=AcquireCacheView(rotate_image);
1060 switch (rotations)
1061 {
1062 case 0:
1063 {
1064 /*
1065 Rotate 0 degrees.
1066 */
cristy3ed852e2009-09-05 21:47:34 +00001067 break;
1068 }
1069 case 1:
1070 {
cristybb503372010-05-27 20:51:26 +00001071 size_t
cristyb32b90a2009-09-07 21:45:48 +00001072 tile_height,
1073 tile_width;
1074
cristyd40deb62011-03-09 00:52:27 +00001075 ssize_t
1076 tile_y;
1077
cristy3ed852e2009-09-05 21:47:34 +00001078 /*
1079 Rotate 90 degrees.
1080 */
cristyb32b90a2009-09-07 21:45:48 +00001081 GetPixelCacheTileSize(image,&tile_width,&tile_height);
cristyad11aac2011-09-25 21:29:16 +00001082#if defined(MAGICKCORE_OPENMP_SUPPORT)
1083 #pragma omp parallel for schedule(static,1) shared(progress, status)
1084#endif
1085 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
cristy3ed852e2009-09-05 21:47:34 +00001086 {
cristybb503372010-05-27 20:51:26 +00001087 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001088 tile_x;
1089
1090 if (status == MagickFalse)
1091 continue;
cristy26295322011-09-22 00:50:36 +00001092 tile_x=0;
1093 for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
cristy3ed852e2009-09-05 21:47:34 +00001094 {
1095 MagickBooleanType
1096 sync;
1097
cristy4c08aed2011-07-01 19:47:50 +00001098 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001099 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001100
cristybb503372010-05-27 20:51:26 +00001101 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001102 y;
1103
cristy4c08aed2011-07-01 19:47:50 +00001104 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001105 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001106
cristybb503372010-05-27 20:51:26 +00001107 size_t
cristyb32b90a2009-09-07 21:45:48 +00001108 height,
1109 width;
cristy3ed852e2009-09-05 21:47:34 +00001110
cristyb32b90a2009-09-07 21:45:48 +00001111 width=tile_width;
cristybb503372010-05-27 20:51:26 +00001112 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
cristya45d2ec2011-08-24 13:17:19 +00001113 width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
cristyb32b90a2009-09-07 21:45:48 +00001114 height=tile_height;
cristybb503372010-05-27 20:51:26 +00001115 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
cristya45d2ec2011-08-24 13:17:19 +00001116 height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
cristydaa97692009-09-13 02:10:35 +00001117 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1118 exception);
cristy4c08aed2011-07-01 19:47:50 +00001119 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001120 {
1121 status=MagickFalse;
1122 break;
1123 }
cristybb503372010-05-27 20:51:26 +00001124 for (y=0; y < (ssize_t) width; y++)
cristy3ed852e2009-09-05 21:47:34 +00001125 {
cristy4c08aed2011-07-01 19:47:50 +00001126 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001127 *restrict tile_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001128
cristybb503372010-05-27 20:51:26 +00001129 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001130 x;
1131
cristy27709ef2011-09-18 02:53:53 +00001132 if (status == MagickFalse)
1133 continue;
cristybb503372010-05-27 20:51:26 +00001134 q=QueueCacheViewAuthenticPixels(rotate_view,(ssize_t)
cristy92611572011-07-07 16:02:42 +00001135 (rotate_image->columns-(tile_y+height)),y+tile_x,height,1,
1136 exception);
cristyacd2ed22011-08-30 01:44:23 +00001137 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001138 {
1139 status=MagickFalse;
cristy27709ef2011-09-18 02:53:53 +00001140 continue;
cristy3ed852e2009-09-05 21:47:34 +00001141 }
cristyed231572011-07-14 02:18:59 +00001142 tile_pixels=p+((height-1)*width+y)*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001143 for (x=0; x < (ssize_t) height; x++)
cristy3ed852e2009-09-05 21:47:34 +00001144 {
cristya45d2ec2011-08-24 13:17:19 +00001145 register ssize_t
1146 i;
1147
1148 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1149 {
1150 PixelChannel
1151 channel;
1152
1153 PixelTrait
1154 rotate_traits,
1155 traits;
1156
1157 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
cristya45d2ec2011-08-24 13:17:19 +00001158 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
1159 rotate_traits=GetPixelChannelMapTraits(rotate_image,channel);
cristy010d7d12011-08-31 01:02:48 +00001160 if ((traits == UndefinedPixelTrait) ||
1161 (rotate_traits == UndefinedPixelTrait))
cristya45d2ec2011-08-24 13:17:19 +00001162 continue;
cristy0beccfa2011-09-25 20:47:53 +00001163 SetPixelChannel(rotate_image,channel,tile_pixels[i],q);
cristya45d2ec2011-08-24 13:17:19 +00001164 }
cristyed231572011-07-14 02:18:59 +00001165 tile_pixels-=width*GetPixelChannels(image);
1166 q+=GetPixelChannels(rotate_image);
cristy3ed852e2009-09-05 21:47:34 +00001167 }
cristy3ed852e2009-09-05 21:47:34 +00001168 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1169 if (sync == MagickFalse)
1170 status=MagickFalse;
1171 }
1172 }
1173 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1174 {
1175 MagickBooleanType
1176 proceed;
1177
cristyb32b90a2009-09-07 21:45:48 +00001178 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
cristy3ed852e2009-09-05 21:47:34 +00001179 image->rows);
1180 if (proceed == MagickFalse)
1181 status=MagickFalse;
1182 }
1183 }
cristyecc2c142010-01-17 22:25:46 +00001184 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1185 image->rows-1,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001186 Swap(page.width,page.height);
1187 Swap(page.x,page.y);
1188 if (page.width != 0)
cristybb503372010-05-27 20:51:26 +00001189 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
cristy3ed852e2009-09-05 21:47:34 +00001190 break;
1191 }
1192 case 2:
1193 {
1194 /*
1195 Rotate 180 degrees.
1196 */
cristy59e0d3b2010-06-07 13:12:38 +00001197#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy27709ef2011-09-18 02:53:53 +00001198 #pragma omp parallel for schedule(static,8) shared(progress,status)
cristydaa97692009-09-13 02:10:35 +00001199#endif
cristybb503372010-05-27 20:51:26 +00001200 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001201 {
1202 MagickBooleanType
1203 sync;
1204
cristy4c08aed2011-07-01 19:47:50 +00001205 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001206 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001207
cristybb503372010-05-27 20:51:26 +00001208 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001209 x;
1210
cristy4c08aed2011-07-01 19:47:50 +00001211 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001212 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001213
1214 if (status == MagickFalse)
1215 continue;
cristya45d2ec2011-08-24 13:17:19 +00001216 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1217 q=QueueCacheViewAuthenticPixels(rotate_view,0,(ssize_t) (image->rows-y-
1218 1),image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001219 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00001220 {
1221 status=MagickFalse;
1222 continue;
1223 }
cristyed231572011-07-14 02:18:59 +00001224 q+=GetPixelChannels(rotate_image)*image->columns;
cristybb503372010-05-27 20:51:26 +00001225 for (x=0; x < (ssize_t) image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00001226 {
cristya45d2ec2011-08-24 13:17:19 +00001227 register ssize_t
1228 i;
1229
cristyed231572011-07-14 02:18:59 +00001230 q-=GetPixelChannels(rotate_image);
cristya45d2ec2011-08-24 13:17:19 +00001231 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1232 {
1233 PixelChannel
1234 channel;
1235
1236 PixelTrait
1237 rotate_traits,
1238 traits;
1239
1240 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
cristya45d2ec2011-08-24 13:17:19 +00001241 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
1242 rotate_traits=GetPixelChannelMapTraits(rotate_image,channel);
cristy010d7d12011-08-31 01:02:48 +00001243 if ((traits == UndefinedPixelTrait) ||
1244 (rotate_traits == UndefinedPixelTrait))
cristya45d2ec2011-08-24 13:17:19 +00001245 continue;
cristy0beccfa2011-09-25 20:47:53 +00001246 SetPixelChannel(rotate_image,channel,p[i],q);
cristya45d2ec2011-08-24 13:17:19 +00001247 }
cristyed231572011-07-14 02:18:59 +00001248 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001249 }
cristy3ed852e2009-09-05 21:47:34 +00001250 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1251 if (sync == MagickFalse)
1252 status=MagickFalse;
1253 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1254 {
1255 MagickBooleanType
1256 proceed;
1257
1258 proceed=SetImageProgress(image,RotateImageTag,progress++,
1259 image->rows);
1260 if (proceed == MagickFalse)
1261 status=MagickFalse;
1262 }
1263 }
1264 if (page.width != 0)
cristybb503372010-05-27 20:51:26 +00001265 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
cristy3ed852e2009-09-05 21:47:34 +00001266 if (page.height != 0)
cristybb503372010-05-27 20:51:26 +00001267 page.y=(ssize_t) (page.height-rotate_image->rows-page.y);
cristy3ed852e2009-09-05 21:47:34 +00001268 break;
1269 }
1270 case 3:
1271 {
cristybb503372010-05-27 20:51:26 +00001272 size_t
cristyb32b90a2009-09-07 21:45:48 +00001273 tile_height,
1274 tile_width;
1275
cristyd40deb62011-03-09 00:52:27 +00001276 ssize_t
1277 tile_y;
1278
cristy3ed852e2009-09-05 21:47:34 +00001279 /*
1280 Rotate 270 degrees.
1281 */
cristyb32b90a2009-09-07 21:45:48 +00001282 GetPixelCacheTileSize(image,&tile_width,&tile_height);
cristyad11aac2011-09-25 21:29:16 +00001283#if defined(MAGICKCORE_OPENMP_SUPPORT)
1284 #pragma omp parallel for schedule(static,1) shared(progress,status)
1285#endif
1286 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
cristy3ed852e2009-09-05 21:47:34 +00001287 {
cristybb503372010-05-27 20:51:26 +00001288 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001289 tile_x;
1290
1291 if (status == MagickFalse)
1292 continue;
cristy26295322011-09-22 00:50:36 +00001293 tile_x=0;
1294 for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
cristy3ed852e2009-09-05 21:47:34 +00001295 {
1296 MagickBooleanType
1297 sync;
1298
cristy4c08aed2011-07-01 19:47:50 +00001299 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001300 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001301
cristybb503372010-05-27 20:51:26 +00001302 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001303 y;
1304
cristy4c08aed2011-07-01 19:47:50 +00001305 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001306 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001307
cristybb503372010-05-27 20:51:26 +00001308 size_t
cristyb32b90a2009-09-07 21:45:48 +00001309 height,
1310 width;
cristy3ed852e2009-09-05 21:47:34 +00001311
cristyb32b90a2009-09-07 21:45:48 +00001312 width=tile_width;
cristybb503372010-05-27 20:51:26 +00001313 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
cristya45d2ec2011-08-24 13:17:19 +00001314 width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
cristyb32b90a2009-09-07 21:45:48 +00001315 height=tile_height;
cristybb503372010-05-27 20:51:26 +00001316 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
cristya45d2ec2011-08-24 13:17:19 +00001317 height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
1318 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1319 exception);
cristy4c08aed2011-07-01 19:47:50 +00001320 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001321 {
1322 status=MagickFalse;
1323 break;
1324 }
cristybb503372010-05-27 20:51:26 +00001325 for (y=0; y < (ssize_t) width; y++)
cristy3ed852e2009-09-05 21:47:34 +00001326 {
cristy4c08aed2011-07-01 19:47:50 +00001327 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001328 *restrict tile_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001329
cristybb503372010-05-27 20:51:26 +00001330 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001331 x;
1332
cristy27709ef2011-09-18 02:53:53 +00001333 if (status == MagickFalse)
1334 continue;
cristya45d2ec2011-08-24 13:17:19 +00001335 q=QueueCacheViewAuthenticPixels(rotate_view,tile_y,(ssize_t) (y+
1336 rotate_image->rows-(tile_x+width)),height,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001337 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001338 {
1339 status=MagickFalse;
cristy27709ef2011-09-18 02:53:53 +00001340 continue;
cristy3ed852e2009-09-05 21:47:34 +00001341 }
cristyed231572011-07-14 02:18:59 +00001342 tile_pixels=p+((width-1)-y)*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001343 for (x=0; x < (ssize_t) height; x++)
cristy3ed852e2009-09-05 21:47:34 +00001344 {
cristya45d2ec2011-08-24 13:17:19 +00001345 register ssize_t
1346 i;
1347
1348 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1349 {
1350 PixelChannel
1351 channel;
1352
1353 PixelTrait
1354 rotate_traits,
1355 traits;
1356
1357 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
cristya45d2ec2011-08-24 13:17:19 +00001358 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
1359 rotate_traits=GetPixelChannelMapTraits(rotate_image,channel);
cristy010d7d12011-08-31 01:02:48 +00001360 if ((traits == UndefinedPixelTrait) ||
1361 (rotate_traits == UndefinedPixelTrait))
cristya45d2ec2011-08-24 13:17:19 +00001362 continue;
cristy0beccfa2011-09-25 20:47:53 +00001363 SetPixelChannel(rotate_image,channel,tile_pixels[i],q);
cristya45d2ec2011-08-24 13:17:19 +00001364 }
cristyed231572011-07-14 02:18:59 +00001365 tile_pixels+=width*GetPixelChannels(image);
1366 q+=GetPixelChannels(rotate_image);
cristy3ed852e2009-09-05 21:47:34 +00001367 }
cristy3ed852e2009-09-05 21:47:34 +00001368 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1369 if (sync == MagickFalse)
1370 status=MagickFalse;
1371 }
1372 }
1373 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1374 {
1375 MagickBooleanType
1376 proceed;
1377
cristyb32b90a2009-09-07 21:45:48 +00001378 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
cristy3ed852e2009-09-05 21:47:34 +00001379 image->rows);
1380 if (proceed == MagickFalse)
1381 status=MagickFalse;
1382 }
1383 }
cristyecc2c142010-01-17 22:25:46 +00001384 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1385 image->rows-1,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001386 Swap(page.width,page.height);
1387 Swap(page.x,page.y);
1388 if (page.height != 0)
cristybb503372010-05-27 20:51:26 +00001389 page.y=(ssize_t) (page.height-rotate_image->rows-page.y);
cristy3ed852e2009-09-05 21:47:34 +00001390 break;
1391 }
1392 }
1393 rotate_view=DestroyCacheView(rotate_view);
1394 image_view=DestroyCacheView(image_view);
1395 rotate_image->type=image->type;
1396 rotate_image->page=page;
1397 if (status == MagickFalse)
1398 rotate_image=DestroyImage(rotate_image);
1399 return(rotate_image);
1400}
1401
1402/*
1403%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1404% %
1405% %
1406% %
1407+ X S h e a r I m a g e %
1408% %
1409% %
1410% %
1411%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1412%
1413% XShearImage() shears the image in the X direction with a shear angle of
1414% 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1415% negative angles shear clockwise. Angles are measured relative to a vertical
1416% Y-axis. X shears will widen an image creating 'empty' triangles on the left
1417% and right sides of the source image.
1418%
1419% The format of the XShearImage method is:
1420%
1421% MagickBooleanType XShearImage(Image *image,const MagickRealType degrees,
cristybb503372010-05-27 20:51:26 +00001422% const size_t width,const size_t height,
1423% const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001424%
1425% A description of each parameter follows.
1426%
1427% o image: the image.
1428%
cristycee97112010-05-28 00:44:52 +00001429% o degrees: A MagickRealType representing the shearing angle along the X
cristy3ed852e2009-09-05 21:47:34 +00001430% axis.
1431%
1432% o width, height, x_offset, y_offset: Defines a region of the image
1433% to shear.
1434%
cristyecc2c142010-01-17 22:25:46 +00001435% o exception: return any errors or warnings in this structure.
1436%
cristy3ed852e2009-09-05 21:47:34 +00001437*/
1438static MagickBooleanType XShearImage(Image *image,const MagickRealType degrees,
cristybb503372010-05-27 20:51:26 +00001439 const size_t width,const size_t height,const ssize_t x_offset,
1440 const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001441{
1442#define XShearImageTag "XShear/Image"
1443
1444 typedef enum
1445 {
1446 LEFT,
1447 RIGHT
1448 } ShearDirection;
1449
1450 CacheView
1451 *image_view;
1452
cristy3ed852e2009-09-05 21:47:34 +00001453 MagickBooleanType
1454 status;
1455
cristy5f959472010-05-27 22:19:46 +00001456 MagickOffsetType
1457 progress;
1458
cristy4c08aed2011-07-01 19:47:50 +00001459 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001460 background;
1461
cristy5f959472010-05-27 22:19:46 +00001462 ssize_t
1463 y;
1464
cristy3ed852e2009-09-05 21:47:34 +00001465 assert(image != (Image *) NULL);
1466 assert(image->signature == MagickSignature);
1467 if (image->debug != MagickFalse)
1468 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy4c08aed2011-07-01 19:47:50 +00001469 GetPixelInfo(image,&background);
1470 SetPixelInfoPacket(image,&image->background_color,&background);
cristy3ed852e2009-09-05 21:47:34 +00001471 if (image->colorspace == CMYKColorspace)
1472 ConvertRGBToCMYK(&background);
1473 /*
cristy26a3acd2011-03-09 00:50:29 +00001474 X shear image.
cristy3ed852e2009-09-05 21:47:34 +00001475 */
1476 status=MagickTrue;
1477 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00001478 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00001479#if defined(MAGICKCORE_OPENMP_SUPPORT)
1480 #pragma omp parallel for schedule(dynamic,4) shared(progress, status)
cristy3ed852e2009-09-05 21:47:34 +00001481#endif
cristybb503372010-05-27 20:51:26 +00001482 for (y=0; y < (ssize_t) height; y++)
cristy3ed852e2009-09-05 21:47:34 +00001483 {
cristy4c08aed2011-07-01 19:47:50 +00001484 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001485 pixel,
1486 source,
1487 destination;
1488
1489 MagickRealType
1490 area,
1491 displacement;
1492
cristy4c08aed2011-07-01 19:47:50 +00001493 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001494 *restrict p,
1495 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001496
cristyd40deb62011-03-09 00:52:27 +00001497 register ssize_t
1498 i;
1499
cristy3ed852e2009-09-05 21:47:34 +00001500 ShearDirection
1501 direction;
1502
cristyd40deb62011-03-09 00:52:27 +00001503 ssize_t
1504 step;
1505
cristy3ed852e2009-09-05 21:47:34 +00001506 if (status == MagickFalse)
1507 continue;
1508 p=GetCacheViewAuthenticPixels(image_view,0,y_offset+y,image->columns,1,
1509 exception);
cristy4c08aed2011-07-01 19:47:50 +00001510 if (p == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001511 {
1512 status=MagickFalse;
1513 continue;
1514 }
cristyed231572011-07-14 02:18:59 +00001515 p+=x_offset*GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001516 displacement=degrees*(MagickRealType) (y-height/2.0);
1517 if (displacement == 0.0)
1518 continue;
1519 if (displacement > 0.0)
1520 direction=RIGHT;
1521 else
1522 {
1523 displacement*=(-1.0);
1524 direction=LEFT;
1525 }
cristybb503372010-05-27 20:51:26 +00001526 step=(ssize_t) floor((double) displacement);
cristy3ed852e2009-09-05 21:47:34 +00001527 area=(MagickRealType) (displacement-step);
1528 step++;
1529 pixel=background;
cristy4c08aed2011-07-01 19:47:50 +00001530 GetPixelInfo(image,&source);
1531 GetPixelInfo(image,&destination);
cristy3ed852e2009-09-05 21:47:34 +00001532 switch (direction)
1533 {
1534 case LEFT:
1535 {
1536 /*
1537 Transfer pixels left-to-right.
1538 */
1539 if (step > x_offset)
1540 break;
cristyed231572011-07-14 02:18:59 +00001541 q=p-step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001542 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001543 {
1544 if ((x_offset+i) < step)
1545 {
cristyed231572011-07-14 02:18:59 +00001546 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001547 SetPixelInfo(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001548 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001549 continue;
1550 }
cristy4c08aed2011-07-01 19:47:50 +00001551 SetPixelInfo(image,p,&source);
1552 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1553 &source,(MagickRealType) GetPixelAlpha(image,p),area,
1554 &destination);
1555 SetPixelPixelInfo(image,&destination,q);
1556 SetPixelInfo(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001557 p+=GetPixelChannels(image);
1558 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001559 }
cristy4c08aed2011-07-01 19:47:50 +00001560 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1561 &background,(MagickRealType) background.alpha,area,&destination);
1562 SetPixelPixelInfo(image,&destination,q);
cristyed231572011-07-14 02:18:59 +00001563 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001564 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001565 {
1566 SetPixelPixelInfo(image,&background,q);
cristyed231572011-07-14 02:18:59 +00001567 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001568 }
cristy3ed852e2009-09-05 21:47:34 +00001569 break;
1570 }
1571 case RIGHT:
1572 {
1573 /*
1574 Transfer pixels right-to-left.
1575 */
cristyed231572011-07-14 02:18:59 +00001576 p+=width*GetPixelChannels(image);
1577 q=p+step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001578 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001579 {
cristyed231572011-07-14 02:18:59 +00001580 p-=GetPixelChannels(image);
1581 q-=GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001582 if ((size_t) (x_offset+width+step-i) >= image->columns)
cristy3ed852e2009-09-05 21:47:34 +00001583 continue;
cristy4c08aed2011-07-01 19:47:50 +00001584 SetPixelInfo(image,p,&source);
1585 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1586 &source,(MagickRealType) GetPixelAlpha(image,p),area,
1587 &destination);
1588 SetPixelPixelInfo(image,&destination,q);
1589 SetPixelInfo(image,p,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001590 }
cristy4c08aed2011-07-01 19:47:50 +00001591 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1592 &background,(MagickRealType) background.alpha,area,&destination);
cristyed231572011-07-14 02:18:59 +00001593 q-=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001594 SetPixelPixelInfo(image,&destination,q);
cristy3ed852e2009-09-05 21:47:34 +00001595 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001596 {
cristyed231572011-07-14 02:18:59 +00001597 q-=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001598 SetPixelPixelInfo(image,&background,q);
1599 }
cristy3ed852e2009-09-05 21:47:34 +00001600 break;
1601 }
1602 }
1603 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1604 status=MagickFalse;
1605 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1606 {
1607 MagickBooleanType
1608 proceed;
1609
cristyb5d5f722009-11-04 03:03:49 +00001610#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00001611 #pragma omp critical (MagickCore_XShearImage)
1612#endif
1613 proceed=SetImageProgress(image,XShearImageTag,progress++,height);
1614 if (proceed == MagickFalse)
1615 status=MagickFalse;
1616 }
1617 }
1618 image_view=DestroyCacheView(image_view);
1619 return(status);
1620}
1621
1622/*
1623%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1624% %
1625% %
1626% %
1627+ Y S h e a r I m a g e %
1628% %
1629% %
1630% %
1631%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1632%
1633% YShearImage shears the image in the Y direction with a shear angle of
1634% 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1635% negative angles shear clockwise. Angles are measured relative to a
1636% horizontal X-axis. Y shears will increase the height of an image creating
1637% 'empty' triangles on the top and bottom of the source image.
1638%
1639% The format of the YShearImage method is:
1640%
1641% MagickBooleanType YShearImage(Image *image,const MagickRealType degrees,
cristybb503372010-05-27 20:51:26 +00001642% const size_t width,const size_t height,
1643% const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001644%
1645% A description of each parameter follows.
1646%
1647% o image: the image.
1648%
cristycee97112010-05-28 00:44:52 +00001649% o degrees: A MagickRealType representing the shearing angle along the Y
cristy3ed852e2009-09-05 21:47:34 +00001650% axis.
1651%
1652% o width, height, x_offset, y_offset: Defines a region of the image
1653% to shear.
1654%
cristyecc2c142010-01-17 22:25:46 +00001655% o exception: return any errors or warnings in this structure.
1656%
cristy3ed852e2009-09-05 21:47:34 +00001657*/
1658static MagickBooleanType YShearImage(Image *image,const MagickRealType degrees,
cristybb503372010-05-27 20:51:26 +00001659 const size_t width,const size_t height,const ssize_t x_offset,
1660 const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001661{
1662#define YShearImageTag "YShear/Image"
1663
1664 typedef enum
1665 {
1666 UP,
1667 DOWN
1668 } ShearDirection;
1669
1670 CacheView
1671 *image_view;
1672
cristy3ed852e2009-09-05 21:47:34 +00001673 MagickBooleanType
1674 status;
1675
cristy5f959472010-05-27 22:19:46 +00001676 MagickOffsetType
1677 progress;
1678
cristy4c08aed2011-07-01 19:47:50 +00001679 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001680 background;
1681
cristy5f959472010-05-27 22:19:46 +00001682 ssize_t
1683 x;
1684
cristy3ed852e2009-09-05 21:47:34 +00001685 assert(image != (Image *) NULL);
1686 assert(image->signature == MagickSignature);
1687 if (image->debug != MagickFalse)
1688 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy4c08aed2011-07-01 19:47:50 +00001689 GetPixelInfo(image,&background);
1690 SetPixelInfoPacket(image,&image->background_color,&background);
cristy3ed852e2009-09-05 21:47:34 +00001691 if (image->colorspace == CMYKColorspace)
1692 ConvertRGBToCMYK(&background);
1693 /*
1694 Y Shear image.
1695 */
1696 status=MagickTrue;
1697 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00001698 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00001699#if defined(MAGICKCORE_OPENMP_SUPPORT)
1700 #pragma omp parallel for schedule(dynamic,4) shared(progress, status)
cristy3ed852e2009-09-05 21:47:34 +00001701#endif
cristybb503372010-05-27 20:51:26 +00001702 for (x=0; x < (ssize_t) width; x++)
cristy3ed852e2009-09-05 21:47:34 +00001703 {
cristybb503372010-05-27 20:51:26 +00001704 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001705 step;
1706
cristy3ed852e2009-09-05 21:47:34 +00001707 MagickRealType
1708 area,
1709 displacement;
1710
cristy4c08aed2011-07-01 19:47:50 +00001711 PixelInfo
1712 pixel,
1713 source,
1714 destination;
1715
1716 register Quantum
1717 *restrict p,
1718 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001719
cristybb503372010-05-27 20:51:26 +00001720 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001721 i;
1722
cristy3ed852e2009-09-05 21:47:34 +00001723 ShearDirection
1724 direction;
1725
1726 if (status == MagickFalse)
1727 continue;
1728 p=GetCacheViewAuthenticPixels(image_view,x_offset+x,0,1,image->rows,
1729 exception);
cristy4c08aed2011-07-01 19:47:50 +00001730 if (p == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001731 {
1732 status=MagickFalse;
1733 continue;
1734 }
cristyed231572011-07-14 02:18:59 +00001735 p+=y_offset*GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001736 displacement=degrees*(MagickRealType) (x-width/2.0);
1737 if (displacement == 0.0)
1738 continue;
1739 if (displacement > 0.0)
1740 direction=DOWN;
1741 else
1742 {
1743 displacement*=(-1.0);
1744 direction=UP;
1745 }
cristybb503372010-05-27 20:51:26 +00001746 step=(ssize_t) floor((double) displacement);
cristy3ed852e2009-09-05 21:47:34 +00001747 area=(MagickRealType) (displacement-step);
1748 step++;
1749 pixel=background;
cristy4c08aed2011-07-01 19:47:50 +00001750 GetPixelInfo(image,&source);
1751 GetPixelInfo(image,&destination);
cristy3ed852e2009-09-05 21:47:34 +00001752 switch (direction)
1753 {
1754 case UP:
1755 {
1756 /*
1757 Transfer pixels top-to-bottom.
1758 */
1759 if (step > y_offset)
1760 break;
cristyed231572011-07-14 02:18:59 +00001761 q=p-step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001762 for (i=0; i < (ssize_t) height; i++)
cristy3ed852e2009-09-05 21:47:34 +00001763 {
1764 if ((y_offset+i) < step)
1765 {
cristyed231572011-07-14 02:18:59 +00001766 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001767 SetPixelInfo(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001768 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001769 continue;
1770 }
cristy4c08aed2011-07-01 19:47:50 +00001771 SetPixelInfo(image,p,&source);
1772 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1773 &source,(MagickRealType) GetPixelAlpha(image,p),area,
1774 &destination);
1775 SetPixelPixelInfo(image,&destination,q);
1776 SetPixelInfo(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001777 p+=GetPixelChannels(image);
1778 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001779 }
cristy4c08aed2011-07-01 19:47:50 +00001780 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1781 &background,(MagickRealType) background.alpha,area,&destination);
1782 SetPixelPixelInfo(image,&destination,q);
cristyed231572011-07-14 02:18:59 +00001783 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001784 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001785 {
1786 SetPixelPixelInfo(image,&background,q);
cristyed231572011-07-14 02:18:59 +00001787 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001788 }
cristy3ed852e2009-09-05 21:47:34 +00001789 break;
1790 }
1791 case DOWN:
1792 {
1793 /*
1794 Transfer pixels bottom-to-top.
1795 */
cristyed231572011-07-14 02:18:59 +00001796 p+=height*GetPixelChannels(image);
1797 q=p+step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001798 for (i=0; i < (ssize_t) height; i++)
cristy3ed852e2009-09-05 21:47:34 +00001799 {
cristyed231572011-07-14 02:18:59 +00001800 p-=GetPixelChannels(image);
1801 q-=GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001802 if ((size_t) (y_offset+height+step-i) >= image->rows)
cristy3ed852e2009-09-05 21:47:34 +00001803 continue;
cristy4c08aed2011-07-01 19:47:50 +00001804 SetPixelInfo(image,p,&source);
1805 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1806 &source,(MagickRealType) GetPixelAlpha(image,p),area,
1807 &destination);
1808 SetPixelPixelInfo(image,&destination,q);
1809 SetPixelInfo(image,p,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001810 }
cristy4c08aed2011-07-01 19:47:50 +00001811 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1812 &background,(MagickRealType) background.alpha,area,&destination);
cristyed231572011-07-14 02:18:59 +00001813 q-=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001814 SetPixelPixelInfo(image,&destination,q);
cristy3ed852e2009-09-05 21:47:34 +00001815 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001816 {
cristyed231572011-07-14 02:18:59 +00001817 q-=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001818 SetPixelPixelInfo(image,&background,q);
1819 }
cristy3ed852e2009-09-05 21:47:34 +00001820 break;
1821 }
1822 }
1823 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1824 status=MagickFalse;
1825 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1826 {
1827 MagickBooleanType
1828 proceed;
1829
cristyb5d5f722009-11-04 03:03:49 +00001830#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00001831 #pragma omp critical (MagickCore_YShearImage)
1832#endif
1833 proceed=SetImageProgress(image,YShearImageTag,progress++,image->rows);
1834 if (proceed == MagickFalse)
1835 status=MagickFalse;
1836 }
1837 }
1838 image_view=DestroyCacheView(image_view);
1839 return(status);
1840}
1841
1842/*
1843%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1844% %
1845% %
1846% %
1847% R o t a t e I m a g e %
1848% %
1849% %
1850% %
1851%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1852%
1853% RotateImage() creates a new image that is a rotated copy of an existing
1854% one. Positive angles rotate counter-clockwise (right-hand rule), while
1855% negative angles rotate clockwise. Rotated images are usually larger than
1856% the originals and have 'empty' triangular corners. X axis. Empty
1857% triangles left over from shearing the image are filled with the background
1858% color defined by member 'background_color' of the image. RotateImage
1859% allocates the memory necessary for the new Image structure and returns a
1860% pointer to the new image.
1861%
1862% RotateImage() is based on the paper "A Fast Algorithm for General
1863% Raster Rotatation" by Alan W. Paeth. RotateImage is adapted from a similar
1864% method based on the Paeth paper written by Michael Halle of the Spatial
1865% Imaging Group, MIT Media Lab.
1866%
1867% The format of the RotateImage method is:
1868%
1869% Image *RotateImage(const Image *image,const double degrees,
1870% ExceptionInfo *exception)
1871%
1872% A description of each parameter follows.
1873%
1874% o image: the image.
1875%
1876% o degrees: Specifies the number of degrees to rotate the image.
1877%
1878% o exception: return any errors or warnings in this structure.
1879%
1880*/
1881MagickExport Image *RotateImage(const Image *image,const double degrees,
1882 ExceptionInfo *exception)
1883{
1884 Image
1885 *integral_image,
1886 *rotate_image;
1887
cristyecc2c142010-01-17 22:25:46 +00001888 MagickBooleanType
1889 status;
1890
cristy3ed852e2009-09-05 21:47:34 +00001891 MagickRealType
1892 angle;
1893
1894 PointInfo
1895 shear;
1896
1897 RectangleInfo
1898 border_info;
1899
cristybb503372010-05-27 20:51:26 +00001900 size_t
cristy3ed852e2009-09-05 21:47:34 +00001901 height,
1902 rotations,
1903 width,
1904 y_width;
1905
cristy4c08aed2011-07-01 19:47:50 +00001906 ssize_t
1907 x_offset,
1908 y_offset;
1909
cristy3ed852e2009-09-05 21:47:34 +00001910 /*
1911 Adjust rotation angle.
1912 */
1913 assert(image != (Image *) NULL);
1914 assert(image->signature == MagickSignature);
1915 if (image->debug != MagickFalse)
1916 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1917 assert(exception != (ExceptionInfo *) NULL);
1918 assert(exception->signature == MagickSignature);
1919 angle=degrees;
1920 while (angle < -45.0)
1921 angle+=360.0;
1922 for (rotations=0; angle > 45.0; rotations++)
1923 angle-=90.0;
1924 rotations%=4;
1925 /*
1926 Calculate shear equations.
1927 */
1928 integral_image=IntegralRotateImage(image,rotations,exception);
1929 if (integral_image == (Image *) NULL)
1930 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1931 shear.x=(-tan((double) DegreesToRadians(angle)/2.0));
1932 shear.y=sin((double) DegreesToRadians(angle));
1933 if ((shear.x == 0.0) && (shear.y == 0.0))
1934 return(integral_image);
cristy574cc262011-08-05 01:23:58 +00001935 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001936 {
cristy3ed852e2009-09-05 21:47:34 +00001937 integral_image=DestroyImage(integral_image);
1938 return(integral_image);
1939 }
1940 if (integral_image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00001941 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00001942 /*
1943 Compute image size.
1944 */
1945 width=image->columns;
1946 height=image->rows;
1947 if ((rotations == 1) || (rotations == 3))
1948 {
1949 width=image->rows;
1950 height=image->columns;
1951 }
cristybb503372010-05-27 20:51:26 +00001952 y_width=width+(ssize_t) floor(fabs(shear.x)*height+0.5);
cristycee97112010-05-28 00:44:52 +00001953 x_offset=(ssize_t) ceil((double) width+((fabs(shear.y)*height)-width)/2.0-
1954 0.5);
1955 y_offset=(ssize_t) ceil((double) height+((fabs(shear.y)*y_width)-height)/2.0-
1956 0.5);
cristy3ed852e2009-09-05 21:47:34 +00001957 /*
1958 Surround image with a border.
1959 */
1960 integral_image->border_color=integral_image->background_color;
1961 integral_image->compose=CopyCompositeOp;
cristybb503372010-05-27 20:51:26 +00001962 border_info.width=(size_t) x_offset;
1963 border_info.height=(size_t) y_offset;
cristy633f0c62011-09-15 13:27:36 +00001964 rotate_image=BorderImage(integral_image,&border_info,image->compose,
1965 exception);
cristy3ed852e2009-09-05 21:47:34 +00001966 integral_image=DestroyImage(integral_image);
1967 if (rotate_image == (Image *) NULL)
1968 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1969 /*
1970 Rotate the image.
1971 */
cristyeaedf062010-05-29 22:36:02 +00001972 status=XShearImage(rotate_image,shear.x,width,height,x_offset,(ssize_t)
1973 (rotate_image->rows-height)/2,exception);
cristyecc2c142010-01-17 22:25:46 +00001974 if (status == MagickFalse)
1975 {
1976 rotate_image=DestroyImage(rotate_image);
1977 return((Image *) NULL);
1978 }
cristyeaedf062010-05-29 22:36:02 +00001979 status=YShearImage(rotate_image,shear.y,y_width,height,(ssize_t)
1980 (rotate_image->columns-y_width)/2,y_offset,exception);
cristyecc2c142010-01-17 22:25:46 +00001981 if (status == MagickFalse)
1982 {
1983 rotate_image=DestroyImage(rotate_image);
1984 return((Image *) NULL);
1985 }
cristyeaedf062010-05-29 22:36:02 +00001986 status=XShearImage(rotate_image,shear.x,y_width,rotate_image->rows,(ssize_t)
1987 (rotate_image->columns-y_width)/2,0,exception);
cristyecc2c142010-01-17 22:25:46 +00001988 if (status == MagickFalse)
1989 {
1990 rotate_image=DestroyImage(rotate_image);
1991 return((Image *) NULL);
1992 }
1993 status=CropToFitImage(&rotate_image,shear.x,shear.y,(MagickRealType) width,
cristy3ed852e2009-09-05 21:47:34 +00001994 (MagickRealType) height,MagickTrue,exception);
cristyecc2c142010-01-17 22:25:46 +00001995 if (status == MagickFalse)
1996 {
1997 rotate_image=DestroyImage(rotate_image);
1998 return((Image *) NULL);
1999 }
cristy3ed852e2009-09-05 21:47:34 +00002000 rotate_image->compose=image->compose;
2001 rotate_image->page.width=0;
2002 rotate_image->page.height=0;
2003 return(rotate_image);
2004}
2005
2006/*
2007%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2008% %
2009% %
2010% %
2011% S h e a r I m a g e %
2012% %
2013% %
2014% %
2015%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2016%
2017% ShearImage() creates a new image that is a shear_image copy of an existing
cristycee97112010-05-28 00:44:52 +00002018% one. Shearing slides one edge of an image along the X or Y axis, creating
2019% a parallelogram. An X direction shear slides an edge along the X axis,
2020% while a Y direction shear slides an edge along the Y axis. The amount of
cristy3ed852e2009-09-05 21:47:34 +00002021% the shear is controlled by a shear angle. For X direction shears, x_shear
2022% is measured relative to the Y axis, and similarly, for Y direction shears
2023% y_shear is measured relative to the X axis. Empty triangles left over from
2024% shearing the image are filled with the background color defined by member
2025% 'background_color' of the image.. ShearImage() allocates the memory
2026% necessary for the new Image structure and returns a pointer to the new image.
2027%
2028% ShearImage() is based on the paper "A Fast Algorithm for General Raster
2029% Rotatation" by Alan W. Paeth.
2030%
2031% The format of the ShearImage method is:
2032%
2033% Image *ShearImage(const Image *image,const double x_shear,
2034% const double y_shear,ExceptionInfo *exception)
2035%
2036% A description of each parameter follows.
2037%
2038% o image: the image.
2039%
2040% o x_shear, y_shear: Specifies the number of degrees to shear the image.
2041%
2042% o exception: return any errors or warnings in this structure.
2043%
2044*/
2045MagickExport Image *ShearImage(const Image *image,const double x_shear,
2046 const double y_shear,ExceptionInfo *exception)
2047{
2048 Image
2049 *integral_image,
2050 *shear_image;
2051
cristybb503372010-05-27 20:51:26 +00002052 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002053 x_offset,
2054 y_offset;
2055
cristyecc2c142010-01-17 22:25:46 +00002056 MagickBooleanType
2057 status;
2058
cristy3ed852e2009-09-05 21:47:34 +00002059 PointInfo
2060 shear;
2061
2062 RectangleInfo
2063 border_info;
2064
cristybb503372010-05-27 20:51:26 +00002065 size_t
cristy3ed852e2009-09-05 21:47:34 +00002066 y_width;
2067
2068 assert(image != (Image *) NULL);
2069 assert(image->signature == MagickSignature);
2070 if (image->debug != MagickFalse)
2071 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2072 assert(exception != (ExceptionInfo *) NULL);
2073 assert(exception->signature == MagickSignature);
2074 if ((x_shear != 0.0) && (fmod(x_shear,90.0) == 0.0))
2075 ThrowImageException(ImageError,"AngleIsDiscontinuous");
2076 if ((y_shear != 0.0) && (fmod(y_shear,90.0) == 0.0))
2077 ThrowImageException(ImageError,"AngleIsDiscontinuous");
2078 /*
2079 Initialize shear angle.
2080 */
2081 integral_image=CloneImage(image,0,0,MagickTrue,exception);
2082 if (integral_image == (Image *) NULL)
2083 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2084 shear.x=(-tan(DegreesToRadians(fmod(x_shear,360.0))));
2085 shear.y=tan(DegreesToRadians(fmod(y_shear,360.0)));
2086 if ((shear.x == 0.0) && (shear.y == 0.0))
2087 return(integral_image);
cristy574cc262011-08-05 01:23:58 +00002088 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00002089 {
cristy3ed852e2009-09-05 21:47:34 +00002090 integral_image=DestroyImage(integral_image);
2091 return(integral_image);
2092 }
2093 if (integral_image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00002094 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00002095 /*
2096 Compute image size.
2097 */
cristybb503372010-05-27 20:51:26 +00002098 y_width=image->columns+(ssize_t) floor(fabs(shear.x)*image->rows+0.5);
cristycee97112010-05-28 00:44:52 +00002099 x_offset=(ssize_t) ceil((double) image->columns+((fabs(shear.x)*image->rows)-
cristy06609ee2010-03-17 20:21:27 +00002100 image->columns)/2.0-0.5);
cristycee97112010-05-28 00:44:52 +00002101 y_offset=(ssize_t) ceil((double) image->rows+((fabs(shear.y)*y_width)-
2102 image->rows)/2.0-0.5);
cristy3ed852e2009-09-05 21:47:34 +00002103 /*
2104 Surround image with border.
2105 */
2106 integral_image->border_color=integral_image->background_color;
2107 integral_image->compose=CopyCompositeOp;
cristybb503372010-05-27 20:51:26 +00002108 border_info.width=(size_t) x_offset;
2109 border_info.height=(size_t) y_offset;
cristy633f0c62011-09-15 13:27:36 +00002110 shear_image=BorderImage(integral_image,&border_info,image->compose,exception);
cristyecc2c142010-01-17 22:25:46 +00002111 integral_image=DestroyImage(integral_image);
cristy3ed852e2009-09-05 21:47:34 +00002112 if (shear_image == (Image *) NULL)
2113 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +00002114 /*
2115 Shear the image.
2116 */
2117 if (shear_image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00002118 (void) SetImageAlphaChannel(shear_image,OpaqueAlphaChannel,exception);
cristyecc2c142010-01-17 22:25:46 +00002119 status=XShearImage(shear_image,shear.x,image->columns,image->rows,x_offset,
cristyeaedf062010-05-29 22:36:02 +00002120 (ssize_t) (shear_image->rows-image->rows)/2,exception);
cristyecc2c142010-01-17 22:25:46 +00002121 if (status == MagickFalse)
2122 {
2123 shear_image=DestroyImage(shear_image);
2124 return((Image *) NULL);
2125 }
cristyeaedf062010-05-29 22:36:02 +00002126 status=YShearImage(shear_image,shear.y,y_width,image->rows,(ssize_t)
2127 (shear_image->columns-y_width)/2,y_offset,exception);
cristyecc2c142010-01-17 22:25:46 +00002128 if (status == MagickFalse)
2129 {
2130 shear_image=DestroyImage(shear_image);
2131 return((Image *) NULL);
2132 }
2133 status=CropToFitImage(&shear_image,shear.x,shear.y,(MagickRealType)
2134 image->columns,(MagickRealType) image->rows,MagickFalse,exception);
2135 if (status == MagickFalse)
2136 {
2137 shear_image=DestroyImage(shear_image);
2138 return((Image *) NULL);
2139 }
cristy3ed852e2009-09-05 21:47:34 +00002140 shear_image->compose=image->compose;
2141 shear_image->page.width=0;
2142 shear_image->page.height=0;
2143 return(shear_image);
2144}