blob: 61eb9aea46271cff89cbf28219694e90027bb778 [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"
cristy0740a982011-10-13 15:01:01 +000070#include "MagickCore/nt-base-private.h"
cristy4c08aed2011-07-01 19:47:50 +000071#include "MagickCore/pixel-accessor.h"
72#include "MagickCore/quantum.h"
73#include "MagickCore/resource_.h"
74#include "MagickCore/shear.h"
75#include "MagickCore/statistic.h"
76#include "MagickCore/string_.h"
77#include "MagickCore/string-private.h"
78#include "MagickCore/thread-private.h"
79#include "MagickCore/threshold.h"
80#include "MagickCore/transform.h"
cristy3ed852e2009-09-05 21:47:34 +000081
82/*
83%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84% %
85% %
86% %
87% A f f i n e T r a n s f o r m I m a g e %
88% %
89% %
90% %
91%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
92%
93% AffineTransformImage() transforms an image as dictated by the affine matrix.
94% It allocates the memory necessary for the new Image structure and returns
95% a pointer to the new image.
96%
97% The format of the AffineTransformImage method is:
98%
99% Image *AffineTransformImage(const Image *image,
100% AffineMatrix *affine_matrix,ExceptionInfo *exception)
101%
102% A description of each parameter follows:
103%
104% o image: the image.
105%
106% o affine_matrix: the affine matrix.
107%
108% o exception: return any errors or warnings in this structure.
109%
110*/
111MagickExport Image *AffineTransformImage(const Image *image,
112 const AffineMatrix *affine_matrix,ExceptionInfo *exception)
113{
114 double
115 distort[6];
116
117 Image
118 *deskew_image;
119
120 /*
121 Affine transform image.
122 */
123 assert(image->signature == MagickSignature);
124 if (image->debug != MagickFalse)
125 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
126 assert(affine_matrix != (AffineMatrix *) NULL);
127 assert(exception != (ExceptionInfo *) NULL);
128 assert(exception->signature == MagickSignature);
129 distort[0]=affine_matrix->sx;
130 distort[1]=affine_matrix->rx;
131 distort[2]=affine_matrix->ry;
132 distort[3]=affine_matrix->sy;
133 distort[4]=affine_matrix->tx;
134 distort[5]=affine_matrix->ty;
135 deskew_image=DistortImage(image,AffineProjectionDistortion,6,distort,
136 MagickTrue,exception);
137 return(deskew_image);
138}
139
140/*
141%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
142% %
143% %
144% %
145+ C r o p T o F i t I m a g e %
146% %
147% %
148% %
149%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150%
151% CropToFitImage() crops the sheared image as determined by the bounding box
152% as defined by width and height and shearing angles.
153%
154% The format of the CropToFitImage method is:
155%
cristyecc2c142010-01-17 22:25:46 +0000156% MagickBooleanType CropToFitImage(Image **image,
157% const MagickRealType x_shear,const MagickRealType x_shear,
158% const MagickRealType width,const MagickRealType height,
159% const MagickBooleanType rotate,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000160%
161% A description of each parameter follows.
162%
163% o image: the image.
164%
165% o x_shear, y_shear, width, height: Defines a region of the image to crop.
166%
167% o exception: return any errors or warnings in this structure.
168%
169*/
cristyecc2c142010-01-17 22:25:46 +0000170static MagickBooleanType CropToFitImage(Image **image,
171 const MagickRealType x_shear,const MagickRealType y_shear,
172 const MagickRealType width,const MagickRealType height,
173 const MagickBooleanType rotate,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000174{
175 Image
176 *crop_image;
177
178 PointInfo
179 extent[4],
180 min,
181 max;
182
183 RectangleInfo
184 geometry,
185 page;
186
cristybb503372010-05-27 20:51:26 +0000187 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000188 i;
189
190 /*
191 Calculate the rotated image size.
192 */
193 extent[0].x=(double) (-width/2.0);
194 extent[0].y=(double) (-height/2.0);
195 extent[1].x=(double) width/2.0;
196 extent[1].y=(double) (-height/2.0);
197 extent[2].x=(double) (-width/2.0);
198 extent[2].y=(double) height/2.0;
199 extent[3].x=(double) width/2.0;
200 extent[3].y=(double) height/2.0;
201 for (i=0; i < 4; i++)
202 {
203 extent[i].x+=x_shear*extent[i].y;
204 extent[i].y+=y_shear*extent[i].x;
205 if (rotate != MagickFalse)
206 extent[i].x+=x_shear*extent[i].y;
207 extent[i].x+=(double) (*image)->columns/2.0;
208 extent[i].y+=(double) (*image)->rows/2.0;
209 }
210 min=extent[0];
211 max=extent[0];
212 for (i=1; i < 4; i++)
213 {
214 if (min.x > extent[i].x)
215 min.x=extent[i].x;
216 if (min.y > extent[i].y)
217 min.y=extent[i].y;
218 if (max.x < extent[i].x)
219 max.x=extent[i].x;
220 if (max.y < extent[i].y)
221 max.y=extent[i].y;
222 }
cristybb503372010-05-27 20:51:26 +0000223 geometry.x=(ssize_t) ceil(min.x-0.5);
224 geometry.y=(ssize_t) ceil(min.y-0.5);
225 geometry.width=(size_t) floor(max.x-min.x+0.5);
226 geometry.height=(size_t) floor(max.y-min.y+0.5);
cristy3ed852e2009-09-05 21:47:34 +0000227 page=(*image)->page;
228 (void) ParseAbsoluteGeometry("0x0+0+0",&(*image)->page);
229 crop_image=CropImage(*image,&geometry,exception);
cristyecc2c142010-01-17 22:25:46 +0000230 if (crop_image == (Image *) NULL)
231 return(MagickFalse);
232 crop_image->page=page;
233 *image=DestroyImage(*image);
234 *image=crop_image;
235 return(MagickTrue);
cristy3ed852e2009-09-05 21:47:34 +0000236}
237
238/*
239%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
240% %
241% %
242% %
243% D e s k e w I m a g e %
244% %
245% %
246% %
247%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
248%
249% DeskewImage() removes skew from the image. Skew is an artifact that
250% occurs in scanned images because of the camera being misaligned,
251% imperfections in the scanning or surface, or simply because the paper was
252% not placed completely flat when scanned.
253%
254% The format of the DeskewImage method is:
255%
256% Image *DeskewImage(const Image *image,const double threshold,
257% ExceptionInfo *exception)
258%
259% A description of each parameter follows:
260%
261% o image: the image.
262%
263% o threshold: separate background from foreground.
264%
265% o exception: return any errors or warnings in this structure.
266%
267*/
268
269typedef struct _RadonInfo
270{
271 CacheType
272 type;
273
cristybb503372010-05-27 20:51:26 +0000274 size_t
cristy3ed852e2009-09-05 21:47:34 +0000275 width,
276 height;
277
278 MagickSizeType
279 length;
280
281 MagickBooleanType
282 mapped;
283
284 char
285 path[MaxTextExtent];
286
287 int
288 file;
289
290 unsigned short
291 *cells;
292} RadonInfo;
293
294static RadonInfo *DestroyRadonInfo(RadonInfo *radon_info)
295{
296 assert(radon_info != (RadonInfo *) NULL);
297 switch (radon_info->type)
298 {
299 case MemoryCache:
300 {
301 if (radon_info->mapped == MagickFalse)
302 radon_info->cells=(unsigned short *) RelinquishMagickMemory(
303 radon_info->cells);
304 else
305 radon_info->cells=(unsigned short *) UnmapBlob(radon_info->cells,
306 (size_t) radon_info->length);
307 RelinquishMagickResource(MemoryResource,radon_info->length);
308 break;
309 }
310 case MapCache:
311 {
312 radon_info->cells=(unsigned short *) UnmapBlob(radon_info->cells,(size_t)
313 radon_info->length);
314 RelinquishMagickResource(MapResource,radon_info->length);
315 }
316 case DiskCache:
317 {
318 if (radon_info->file != -1)
319 (void) close(radon_info->file);
320 (void) RelinquishUniqueFileResource(radon_info->path);
321 RelinquishMagickResource(DiskResource,radon_info->length);
322 break;
323 }
324 default:
325 break;
326 }
327 return((RadonInfo *) RelinquishMagickMemory(radon_info));
328}
329
330static MagickBooleanType ResetRadonCells(RadonInfo *radon_info)
331{
cristybb503372010-05-27 20:51:26 +0000332 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000333 x;
334
335 ssize_t
cristyd40deb62011-03-09 00:52:27 +0000336 count,
337 y;
cristy3ed852e2009-09-05 21:47:34 +0000338
339 unsigned short
340 value;
341
342 if (radon_info->type != DiskCache)
343 {
344 (void) ResetMagickMemory(radon_info->cells,0,(size_t) radon_info->length);
345 return(MagickTrue);
346 }
347 value=0;
cristy7f317702011-02-18 20:40:28 +0000348 (void) lseek(radon_info->file,0,SEEK_SET);
cristybb503372010-05-27 20:51:26 +0000349 for (y=0; y < (ssize_t) radon_info->height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000350 {
cristybb503372010-05-27 20:51:26 +0000351 for (x=0; x < (ssize_t) radon_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000352 {
353 count=write(radon_info->file,&value,sizeof(*radon_info->cells));
354 if (count != (ssize_t) sizeof(*radon_info->cells))
355 break;
356 }
cristybb503372010-05-27 20:51:26 +0000357 if (x < (ssize_t) radon_info->width)
cristy3ed852e2009-09-05 21:47:34 +0000358 break;
359 }
cristybb503372010-05-27 20:51:26 +0000360 return(y < (ssize_t) radon_info->height ? MagickFalse : MagickTrue);
cristy3ed852e2009-09-05 21:47:34 +0000361}
362
cristybb503372010-05-27 20:51:26 +0000363static RadonInfo *AcquireRadonInfo(const Image *image,const size_t width,
364 const size_t height,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000365{
366 MagickBooleanType
367 status;
368
369 RadonInfo
370 *radon_info;
371
cristy73bd4a52010-10-05 11:24:23 +0000372 radon_info=(RadonInfo *) AcquireMagickMemory(sizeof(*radon_info));
cristy3ed852e2009-09-05 21:47:34 +0000373 if (radon_info == (RadonInfo *) NULL)
374 return((RadonInfo *) NULL);
375 (void) ResetMagickMemory(radon_info,0,sizeof(*radon_info));
376 radon_info->width=width;
377 radon_info->height=height;
378 radon_info->length=(MagickSizeType) width*height*sizeof(*radon_info->cells);
379 radon_info->type=MemoryCache;
380 status=AcquireMagickResource(AreaResource,radon_info->length);
381 if ((status != MagickFalse) &&
382 (radon_info->length == (MagickSizeType) ((size_t) radon_info->length)))
383 {
384 status=AcquireMagickResource(MemoryResource,radon_info->length);
385 if (status != MagickFalse)
386 {
387 radon_info->mapped=MagickFalse;
388 radon_info->cells=(unsigned short *) AcquireMagickMemory((size_t)
389 radon_info->length);
390 if (radon_info->cells == (unsigned short *) NULL)
391 {
392 radon_info->mapped=MagickTrue;
393 radon_info->cells=(unsigned short *) MapBlob(-1,IOMode,0,(size_t)
394 radon_info->length);
395 }
396 if (radon_info->cells == (unsigned short *) NULL)
397 RelinquishMagickResource(MemoryResource,radon_info->length);
398 }
399 }
400 radon_info->file=(-1);
401 if (radon_info->cells == (unsigned short *) NULL)
402 {
403 status=AcquireMagickResource(DiskResource,radon_info->length);
404 if (status == MagickFalse)
405 {
406 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
407 "CacheResourcesExhausted","`%s'",image->filename);
408 return(DestroyRadonInfo(radon_info));
409 }
410 radon_info->type=DiskCache;
411 (void) AcquireMagickResource(MemoryResource,radon_info->length);
412 radon_info->file=AcquireUniqueFileResource(radon_info->path);
413 if (radon_info->file == -1)
414 return(DestroyRadonInfo(radon_info));
415 status=AcquireMagickResource(MapResource,radon_info->length);
416 if (status != MagickFalse)
417 {
418 status=ResetRadonCells(radon_info);
419 if (status != MagickFalse)
420 {
421 radon_info->cells=(unsigned short *) MapBlob(radon_info->file,
422 IOMode,0,(size_t) radon_info->length);
423 if (radon_info->cells != (unsigned short *) NULL)
424 radon_info->type=MapCache;
425 else
426 RelinquishMagickResource(MapResource,radon_info->length);
427 }
428 }
429 }
430 return(radon_info);
431}
432
433static inline size_t MagickMin(const size_t x,const size_t y)
434{
435 if (x < y)
436 return(x);
437 return(y);
438}
439
440static inline ssize_t ReadRadonCell(const RadonInfo *radon_info,
cristy2c38b272011-02-18 23:25:33 +0000441 const MagickOffsetType offset,const size_t length,unsigned char *buffer)
cristy3ed852e2009-09-05 21:47:34 +0000442{
443 register ssize_t
444 i;
445
446 ssize_t
447 count;
448
449#if !defined(MAGICKCORE_HAVE_PPREAD)
cristyb5d5f722009-11-04 03:03:49 +0000450#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000451 #pragma omp critical (MagickCore_ReadRadonCell)
452#endif
453 {
454 i=(-1);
cristy7f317702011-02-18 20:40:28 +0000455 if (lseek(radon_info->file,offset,SEEK_SET) >= 0)
cristy3ed852e2009-09-05 21:47:34 +0000456 {
457#endif
458 count=0;
459 for (i=0; i < (ssize_t) length; i+=count)
460 {
461#if !defined(MAGICKCORE_HAVE_PPREAD)
462 count=read(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
463 SSIZE_MAX));
464#else
465 count=pread(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
cristy2c38b272011-02-18 23:25:33 +0000466 SSIZE_MAX),offset+i);
cristy3ed852e2009-09-05 21:47:34 +0000467#endif
468 if (count > 0)
469 continue;
470 count=0;
471 if (errno != EINTR)
472 {
473 i=(-1);
474 break;
475 }
476 }
477#if !defined(MAGICKCORE_HAVE_PPREAD)
478 }
479 }
480#endif
481 return(i);
482}
483
484static inline ssize_t WriteRadonCell(const RadonInfo *radon_info,
cristy2c38b272011-02-18 23:25:33 +0000485 const MagickOffsetType offset,const size_t length,const unsigned char *buffer)
cristy3ed852e2009-09-05 21:47:34 +0000486{
487 register ssize_t
488 i;
489
490 ssize_t
491 count;
492
493#if !defined(MAGICKCORE_HAVE_PWRITE)
cristyb5d5f722009-11-04 03:03:49 +0000494#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000495 #pragma omp critical (MagickCore_WriteRadonCell)
496#endif
497 {
cristy7f317702011-02-18 20:40:28 +0000498 if (lseek(radon_info->file,offset,SEEK_SET) >= 0)
cristy3ed852e2009-09-05 21:47:34 +0000499 {
500#endif
501 count=0;
502 for (i=0; i < (ssize_t) length; i+=count)
503 {
504#if !defined(MAGICKCORE_HAVE_PWRITE)
505 count=write(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
506 SSIZE_MAX));
507#else
508 count=pwrite(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
cristy2c38b272011-02-18 23:25:33 +0000509 SSIZE_MAX),offset+i);
cristy3ed852e2009-09-05 21:47:34 +0000510#endif
511 if (count > 0)
512 continue;
513 count=0;
514 if (errno != EINTR)
515 {
516 i=(-1);
517 break;
518 }
519 }
520#if !defined(MAGICKCORE_HAVE_PWRITE)
521 }
522 }
523#endif
524 return(i);
525}
526
527static inline unsigned short GetRadonCell(const RadonInfo *radon_info,
cristybb503372010-05-27 20:51:26 +0000528 const ssize_t x,const ssize_t y)
cristy3ed852e2009-09-05 21:47:34 +0000529{
cristy2c38b272011-02-18 23:25:33 +0000530 MagickOffsetType
cristy3ed852e2009-09-05 21:47:34 +0000531 i;
532
533 unsigned short
534 value;
535
cristy2c38b272011-02-18 23:25:33 +0000536 i=(MagickOffsetType) radon_info->height*x+y;
cristy3ed852e2009-09-05 21:47:34 +0000537 if ((i < 0) ||
538 ((MagickSizeType) (i*sizeof(*radon_info->cells)) >= radon_info->length))
539 return(0);
540 if (radon_info->type != DiskCache)
541 return(radon_info->cells[i]);
542 value=0;
543 (void) ReadRadonCell(radon_info,i*sizeof(*radon_info->cells),
544 sizeof(*radon_info->cells),(unsigned char *) &value);
545 return(value);
546}
547
548static inline MagickBooleanType SetRadonCell(const RadonInfo *radon_info,
cristybb503372010-05-27 20:51:26 +0000549 const ssize_t x,const ssize_t y,const unsigned short value)
cristy3ed852e2009-09-05 21:47:34 +0000550{
cristy2c38b272011-02-18 23:25:33 +0000551 MagickOffsetType
cristy3ed852e2009-09-05 21:47:34 +0000552 i;
553
554 ssize_t
555 count;
556
cristy2c38b272011-02-18 23:25:33 +0000557 i=(MagickOffsetType) radon_info->height*x+y;
cristy3ed852e2009-09-05 21:47:34 +0000558 if ((i < 0) ||
559 ((MagickSizeType) (i*sizeof(*radon_info->cells)) >= radon_info->length))
560 return(MagickFalse);
561 if (radon_info->type != DiskCache)
562 {
563 radon_info->cells[i]=value;
564 return(MagickTrue);
565 }
566 count=WriteRadonCell(radon_info,i*sizeof(*radon_info->cells),
cristy44807bb2009-09-19 18:28:06 +0000567 sizeof(*radon_info->cells),(const unsigned char *) &value);
cristy3ed852e2009-09-05 21:47:34 +0000568 if (count != (ssize_t) sizeof(*radon_info->cells))
569 return(MagickFalse);
570 return(MagickTrue);
571}
572
573static void RadonProjection(RadonInfo *source_cells,
cristybb503372010-05-27 20:51:26 +0000574 RadonInfo *destination_cells,const ssize_t sign,size_t *projection)
cristy3ed852e2009-09-05 21:47:34 +0000575{
576 RadonInfo
577 *swap;
578
cristybb503372010-05-27 20:51:26 +0000579 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000580 x;
581
582 register RadonInfo
583 *p,
584 *q;
585
cristybb503372010-05-27 20:51:26 +0000586 size_t
cristy3ed852e2009-09-05 21:47:34 +0000587 step;
588
589 p=source_cells;
590 q=destination_cells;
591 for (step=1; step < p->width; step*=2)
592 {
cristyeaedf062010-05-29 22:36:02 +0000593 for (x=0; x < (ssize_t) p->width; x+=2*(ssize_t) step)
cristy3ed852e2009-09-05 21:47:34 +0000594 {
cristybb503372010-05-27 20:51:26 +0000595 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000596 i;
597
cristyd40deb62011-03-09 00:52:27 +0000598 ssize_t
599 y;
600
cristy3ed852e2009-09-05 21:47:34 +0000601 unsigned short
602 cell;
603
cristybb503372010-05-27 20:51:26 +0000604 for (i=0; i < (ssize_t) step; i++)
cristy3ed852e2009-09-05 21:47:34 +0000605 {
cristybb503372010-05-27 20:51:26 +0000606 for (y=0; y < (ssize_t) (p->height-i-1); y++)
cristy3ed852e2009-09-05 21:47:34 +0000607 {
608 cell=GetRadonCell(p,x+i,y);
cristyeaedf062010-05-29 22:36:02 +0000609 (void) SetRadonCell(q,x+2*i,y,cell+GetRadonCell(p,x+i+(ssize_t)
610 step,y+i));
611 (void) SetRadonCell(q,x+2*i+1,y,cell+GetRadonCell(p,x+i+(ssize_t)
612 step,y+i+1));
cristy3ed852e2009-09-05 21:47:34 +0000613 }
cristybb503372010-05-27 20:51:26 +0000614 for ( ; y < (ssize_t) (p->height-i); y++)
cristy3ed852e2009-09-05 21:47:34 +0000615 {
616 cell=GetRadonCell(p,x+i,y);
cristyeaedf062010-05-29 22:36:02 +0000617 (void) SetRadonCell(q,x+2*i,y,cell+GetRadonCell(p,x+i+(ssize_t) step,
618 y+i));
cristy3ed852e2009-09-05 21:47:34 +0000619 (void) SetRadonCell(q,x+2*i+1,y,cell);
620 }
cristybb503372010-05-27 20:51:26 +0000621 for ( ; y < (ssize_t) p->height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000622 {
623 cell=GetRadonCell(p,x+i,y);
624 (void) SetRadonCell(q,x+2*i,y,cell);
625 (void) SetRadonCell(q,x+2*i+1,y,cell);
626 }
627 }
628 }
629 swap=p;
630 p=q;
631 q=swap;
632 }
cristyb5d5f722009-11-04 03:03:49 +0000633#if defined(MAGICKCORE_OPENMP_SUPPORT)
634 #pragma omp parallel for schedule(dynamic,4)
cristy3ed852e2009-09-05 21:47:34 +0000635#endif
cristybb503372010-05-27 20:51:26 +0000636 for (x=0; x < (ssize_t) p->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000637 {
cristybb503372010-05-27 20:51:26 +0000638 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000639 y;
640
cristybb503372010-05-27 20:51:26 +0000641 size_t
cristy3ed852e2009-09-05 21:47:34 +0000642 sum;
643
644 sum=0;
cristybb503372010-05-27 20:51:26 +0000645 for (y=0; y < (ssize_t) (p->height-1); y++)
cristy3ed852e2009-09-05 21:47:34 +0000646 {
cristybb503372010-05-27 20:51:26 +0000647 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000648 delta;
649
cristybb503372010-05-27 20:51:26 +0000650 delta=GetRadonCell(p,x,y)-(ssize_t) GetRadonCell(p,x,y+1);
cristy3ed852e2009-09-05 21:47:34 +0000651 sum+=delta*delta;
652 }
653 projection[p->width+sign*x-1]=sum;
654 }
655}
656
657static MagickBooleanType RadonTransform(const Image *image,
cristybb503372010-05-27 20:51:26 +0000658 const double threshold,size_t *projection,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000659{
660 CacheView
661 *image_view;
662
cristy3ed852e2009-09-05 21:47:34 +0000663 MagickBooleanType
664 status;
665
666 RadonInfo
667 *destination_cells,
668 *source_cells;
669
cristybb503372010-05-27 20:51:26 +0000670 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000671 i;
672
cristybb503372010-05-27 20:51:26 +0000673 size_t
cristy3ed852e2009-09-05 21:47:34 +0000674 count,
675 width;
676
cristyd40deb62011-03-09 00:52:27 +0000677 ssize_t
678 y;
679
680 unsigned char
681 byte;
682
cristy3ed852e2009-09-05 21:47:34 +0000683 unsigned short
684 bits[256];
685
686 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
687 source_cells=AcquireRadonInfo(image,width,image->rows,exception);
688 destination_cells=AcquireRadonInfo(image,width,image->rows,exception);
689 if ((source_cells == (RadonInfo *) NULL) ||
690 (destination_cells == (RadonInfo *) NULL))
691 {
692 if (destination_cells != (RadonInfo *) NULL)
693 destination_cells=DestroyRadonInfo(destination_cells);
694 if (source_cells != (RadonInfo *) NULL)
695 source_cells=DestroyRadonInfo(source_cells);
696 return(MagickFalse);
697 }
698 if (ResetRadonCells(source_cells) == MagickFalse)
699 {
700 destination_cells=DestroyRadonInfo(destination_cells);
701 source_cells=DestroyRadonInfo(source_cells);
702 return(MagickFalse);
703 }
704 for (i=0; i < 256; i++)
705 {
706 byte=(unsigned char) i;
707 for (count=0; byte != 0; byte>>=1)
708 count+=byte & 0x01;
709 bits[i]=(unsigned short) count;
710 }
711 status=MagickTrue;
712 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +0000713#if defined(MAGICKCORE_OPENMP_SUPPORT)
714 #pragma omp parallel for schedule(dynamic,4) shared(status)
cristy3ed852e2009-09-05 21:47:34 +0000715#endif
cristybb503372010-05-27 20:51:26 +0000716 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000717 {
cristy4c08aed2011-07-01 19:47:50 +0000718 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000719 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000720
cristybb503372010-05-27 20:51:26 +0000721 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000722 i,
723 x;
724
cristybb503372010-05-27 20:51:26 +0000725 size_t
cristy3ed852e2009-09-05 21:47:34 +0000726 bit,
727 byte;
728
729 if (status == MagickFalse)
730 continue;
731 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000732 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000733 {
734 status=MagickFalse;
735 continue;
736 }
737 bit=0;
738 byte=0;
cristybb503372010-05-27 20:51:26 +0000739 i=(ssize_t) (image->columns+7)/8;
740 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000741 {
742 byte<<=1;
cristy5b8dd492011-10-09 13:56:36 +0000743 if ((double) GetPixelIntensity(image,p) < threshold)
cristy3ed852e2009-09-05 21:47:34 +0000744 byte|=0x01;
745 bit++;
746 if (bit == 8)
747 {
748 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
749 bit=0;
750 byte=0;
751 }
cristyed231572011-07-14 02:18:59 +0000752 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000753 }
754 if (bit != 0)
755 {
756 byte<<=(8-bit);
757 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
758 }
759 }
760 RadonProjection(source_cells,destination_cells,-1,projection);
761 (void) ResetRadonCells(source_cells);
cristyb5d5f722009-11-04 03:03:49 +0000762#if defined(MAGICKCORE_OPENMP_SUPPORT)
763 #pragma omp parallel for schedule(dynamic,4) shared(status)
cristy3ed852e2009-09-05 21:47:34 +0000764#endif
cristybb503372010-05-27 20:51:26 +0000765 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000766 {
cristy4c08aed2011-07-01 19:47:50 +0000767 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000768 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000769
cristybb503372010-05-27 20:51:26 +0000770 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000771 i,
772 x;
773
cristybb503372010-05-27 20:51:26 +0000774 size_t
cristy3ed852e2009-09-05 21:47:34 +0000775 bit,
776 byte;
777
778 if (status == MagickFalse)
779 continue;
780 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000781 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000782 {
783 status=MagickFalse;
784 continue;
785 }
786 bit=0;
787 byte=0;
788 i=0;
cristybb503372010-05-27 20:51:26 +0000789 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000790 {
791 byte<<=1;
cristy5b8dd492011-10-09 13:56:36 +0000792 if ((double) GetPixelIntensity(image,p) < threshold)
cristy3ed852e2009-09-05 21:47:34 +0000793 byte|=0x01;
794 bit++;
795 if (bit == 8)
796 {
797 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
798 bit=0;
799 byte=0;
800 }
cristyed231572011-07-14 02:18:59 +0000801 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000802 }
803 if (bit != 0)
804 {
805 byte<<=(8-bit);
806 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
807 }
808 }
809 RadonProjection(source_cells,destination_cells,1,projection);
810 image_view=DestroyCacheView(image_view);
811 destination_cells=DestroyRadonInfo(destination_cells);
812 source_cells=DestroyRadonInfo(source_cells);
813 return(MagickTrue);
814}
815
cristybb503372010-05-27 20:51:26 +0000816static void GetImageBackgroundColor(Image *image,const ssize_t offset,
cristy3ed852e2009-09-05 21:47:34 +0000817 ExceptionInfo *exception)
818{
819 CacheView
820 *image_view;
821
cristy4c08aed2011-07-01 19:47:50 +0000822 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000823 background;
824
825 MagickRealType
826 count;
827
cristyd40deb62011-03-09 00:52:27 +0000828 ssize_t
829 y;
830
cristy3ed852e2009-09-05 21:47:34 +0000831 /*
832 Compute average background color.
833 */
834 if (offset <= 0)
835 return;
cristy4c08aed2011-07-01 19:47:50 +0000836 GetPixelInfo(image,&background);
cristy3ed852e2009-09-05 21:47:34 +0000837 count=0.0;
838 image_view=AcquireCacheView(image);
cristybb503372010-05-27 20:51:26 +0000839 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000840 {
cristy4c08aed2011-07-01 19:47:50 +0000841 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000842 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000843
cristybb503372010-05-27 20:51:26 +0000844 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000845 x;
846
cristybb503372010-05-27 20:51:26 +0000847 if ((y >= offset) && (y < ((ssize_t) image->rows-offset)))
cristy3ed852e2009-09-05 21:47:34 +0000848 continue;
849 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000850 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000851 continue;
cristybb503372010-05-27 20:51:26 +0000852 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000853 {
cristybb503372010-05-27 20:51:26 +0000854 if ((x >= offset) && (x < ((ssize_t) image->columns-offset)))
cristy3ed852e2009-09-05 21:47:34 +0000855 continue;
cristy4c08aed2011-07-01 19:47:50 +0000856 background.red+=QuantumScale*GetPixelRed(image,p);
857 background.green+=QuantumScale*GetPixelGreen(image,p);
858 background.blue+=QuantumScale*GetPixelBlue(image,p);
cristy26295322011-09-22 00:50:36 +0000859 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
860 background.alpha+=QuantumScale*GetPixelAlpha(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000861 count++;
cristyed231572011-07-14 02:18:59 +0000862 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000863 }
864 }
865 image_view=DestroyCacheView(image_view);
cristy5b8dd492011-10-09 13:56:36 +0000866 image->background_color.red=(double) ClampToQuantum((MagickRealType)
867 QuantumRange*background.red/count);
868 image->background_color.green=(double) ClampToQuantum((MagickRealType)
869 QuantumRange*background.green/count);
870 image->background_color.blue=(double) ClampToQuantum((MagickRealType)
871 QuantumRange*background.blue/count);
cristy26295322011-09-22 00:50:36 +0000872 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy5b8dd492011-10-09 13:56:36 +0000873 image->background_color.alpha=(double) ClampToQuantum((MagickRealType)
874 QuantumRange*background.alpha/count);
cristy3ed852e2009-09-05 21:47:34 +0000875}
876
877MagickExport Image *DeskewImage(const Image *image,const double threshold,
878 ExceptionInfo *exception)
879{
880 AffineMatrix
881 affine_matrix;
882
883 const char
884 *artifact;
885
886 double
887 degrees;
888
889 Image
890 *clone_image,
891 *crop_image,
892 *deskew_image,
893 *median_image;
894
cristy3ed852e2009-09-05 21:47:34 +0000895 MagickBooleanType
896 status;
897
898 RectangleInfo
899 geometry;
900
cristybb503372010-05-27 20:51:26 +0000901 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000902 i;
903
cristybb503372010-05-27 20:51:26 +0000904 size_t
cristy3ed852e2009-09-05 21:47:34 +0000905 max_projection,
906 *projection,
907 width;
908
cristyd40deb62011-03-09 00:52:27 +0000909 ssize_t
910 skew;
911
cristy3ed852e2009-09-05 21:47:34 +0000912 /*
913 Compute deskew angle.
914 */
915 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
cristybb503372010-05-27 20:51:26 +0000916 projection=(size_t *) AcquireQuantumMemory((size_t) (2*width-1),
cristy3ed852e2009-09-05 21:47:34 +0000917 sizeof(*projection));
cristybb503372010-05-27 20:51:26 +0000918 if (projection == (size_t *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000919 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
920 status=RadonTransform(image,threshold,projection,exception);
921 if (status == MagickFalse)
922 {
cristybb503372010-05-27 20:51:26 +0000923 projection=(size_t *) RelinquishMagickMemory(projection);
cristy3ed852e2009-09-05 21:47:34 +0000924 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
925 }
926 max_projection=0;
927 skew=0;
cristybb503372010-05-27 20:51:26 +0000928 for (i=0; i < (ssize_t) (2*width-1); i++)
cristy3ed852e2009-09-05 21:47:34 +0000929 {
930 if (projection[i] > max_projection)
931 {
cristybb503372010-05-27 20:51:26 +0000932 skew=i-(ssize_t) width+1;
cristy3ed852e2009-09-05 21:47:34 +0000933 max_projection=projection[i];
934 }
935 }
cristybb503372010-05-27 20:51:26 +0000936 projection=(size_t *) RelinquishMagickMemory(projection);
cristy3ed852e2009-09-05 21:47:34 +0000937 /*
938 Deskew image.
939 */
940 clone_image=CloneImage(image,0,0,MagickTrue,exception);
941 if (clone_image == (Image *) NULL)
942 return((Image *) NULL);
943 (void) SetImageVirtualPixelMethod(clone_image,BackgroundVirtualPixelMethod);
944 degrees=RadiansToDegrees(-atan((double) skew/width/8));
945 if (image->debug != MagickFalse)
cristy8cd5b312010-01-07 01:10:24 +0000946 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye7f51092010-01-17 00:39:37 +0000947 " Deskew angle: %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +0000948 affine_matrix.sx=cos(DegreesToRadians(fmod((double) degrees,360.0)));
949 affine_matrix.rx=sin(DegreesToRadians(fmod((double) degrees,360.0)));
950 affine_matrix.ry=(-sin(DegreesToRadians(fmod((double) degrees,360.0))));
951 affine_matrix.sy=cos(DegreesToRadians(fmod((double) degrees,360.0)));
952 affine_matrix.tx=0.0;
953 affine_matrix.ty=0.0;
954 artifact=GetImageArtifact(image,"deskew:auto-crop");
955 if (artifact == (const char *) NULL)
956 {
957 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
958 clone_image=DestroyImage(clone_image);
959 return(deskew_image);
960 }
961 /*
962 Auto-crop image.
963 */
cristyba978e12010-09-12 20:26:50 +0000964 GetImageBackgroundColor(clone_image,(ssize_t) StringToLong(artifact),
965 exception);
cristy3ed852e2009-09-05 21:47:34 +0000966 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
967 clone_image=DestroyImage(clone_image);
968 if (deskew_image == (Image *) NULL)
969 return((Image *) NULL);
cristy95c38342011-03-18 22:39:51 +0000970 median_image=StatisticImage(deskew_image,MedianStatistic,3,3,exception);
cristy3ed852e2009-09-05 21:47:34 +0000971 if (median_image == (Image *) NULL)
972 {
973 deskew_image=DestroyImage(deskew_image);
974 return((Image *) NULL);
975 }
976 geometry=GetImageBoundingBox(median_image,exception);
977 median_image=DestroyImage(median_image);
978 if (image->debug != MagickFalse)
979 (void) LogMagickEvent(TransformEvent,GetMagickModule()," Deskew geometry: "
cristy6d8abba2010-06-03 01:10:47 +0000980 "%.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
cristye8c25f92010-06-03 00:53:06 +0000981 geometry.height,(double) geometry.x,(double) geometry.y);
cristy3ed852e2009-09-05 21:47:34 +0000982 crop_image=CropImage(deskew_image,&geometry,exception);
983 deskew_image=DestroyImage(deskew_image);
984 return(crop_image);
985}
986
987/*
988%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
989% %
990% %
991% %
992+ I n t e g r a l R o t a t e I m a g e %
993% %
994% %
995% %
996%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
997%
cristy3dfa16c2010-05-17 19:34:48 +0000998% IntegralRotateImage() rotates the image an integral of 90 degrees. It
cristy3ed852e2009-09-05 21:47:34 +0000999% allocates the memory necessary for the new Image structure and returns a
1000% pointer to the rotated image.
1001%
1002% The format of the IntegralRotateImage method is:
1003%
cristybb503372010-05-27 20:51:26 +00001004% Image *IntegralRotateImage(const Image *image,size_t rotations,
cristy3ed852e2009-09-05 21:47:34 +00001005% ExceptionInfo *exception)
1006%
1007% A description of each parameter follows.
1008%
1009% o image: the image.
1010%
1011% o rotations: Specifies the number of 90 degree rotations.
1012%
1013*/
cristybb503372010-05-27 20:51:26 +00001014static Image *IntegralRotateImage(const Image *image,size_t rotations,
cristy3ed852e2009-09-05 21:47:34 +00001015 ExceptionInfo *exception)
1016{
cristy3ed852e2009-09-05 21:47:34 +00001017#define RotateImageTag "Rotate/Image"
1018
1019 CacheView
1020 *image_view,
1021 *rotate_view;
1022
1023 Image
1024 *rotate_image;
1025
cristy3ed852e2009-09-05 21:47:34 +00001026 MagickBooleanType
1027 status;
1028
cristy5f959472010-05-27 22:19:46 +00001029 MagickOffsetType
1030 progress;
1031
cristy3ed852e2009-09-05 21:47:34 +00001032 RectangleInfo
1033 page;
1034
cristy5f959472010-05-27 22:19:46 +00001035 ssize_t
1036 y;
1037
cristy3ed852e2009-09-05 21:47:34 +00001038 /*
1039 Initialize rotated image attributes.
1040 */
1041 assert(image != (Image *) NULL);
1042 page=image->page;
1043 rotations%=4;
cristyb32b90a2009-09-07 21:45:48 +00001044 if (rotations == 0)
1045 return(CloneImage(image,0,0,MagickTrue,exception));
cristy3ed852e2009-09-05 21:47:34 +00001046 if ((rotations == 1) || (rotations == 3))
1047 rotate_image=CloneImage(image,image->rows,image->columns,MagickTrue,
1048 exception);
1049 else
1050 rotate_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1051 exception);
1052 if (rotate_image == (Image *) NULL)
1053 return((Image *) NULL);
1054 /*
1055 Integral rotate the image.
1056 */
1057 status=MagickTrue;
1058 progress=0;
1059 image_view=AcquireCacheView(image);
1060 rotate_view=AcquireCacheView(rotate_image);
1061 switch (rotations)
1062 {
1063 case 0:
1064 {
1065 /*
1066 Rotate 0 degrees.
1067 */
cristy3ed852e2009-09-05 21:47:34 +00001068 break;
1069 }
1070 case 1:
1071 {
cristybb503372010-05-27 20:51:26 +00001072 size_t
cristyb32b90a2009-09-07 21:45:48 +00001073 tile_height,
1074 tile_width;
1075
cristyd40deb62011-03-09 00:52:27 +00001076 ssize_t
1077 tile_y;
1078
cristy3ed852e2009-09-05 21:47:34 +00001079 /*
1080 Rotate 90 degrees.
1081 */
cristyb32b90a2009-09-07 21:45:48 +00001082 GetPixelCacheTileSize(image,&tile_width,&tile_height);
cristy11b69e32011-09-29 16:32:44 +00001083 tile_width=image->columns;
cristye31fbf02011-10-10 11:58:31 +00001084#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyc8388eb2011-10-10 11:56:42 +00001085 #pragma omp parallel for schedule(dynamic,4) shared(progress, status) omp_throttle(1)
cristyad11aac2011-09-25 21:29:16 +00001086#endif
1087 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;
cristy26295322011-09-22 00:50:36 +00001094 tile_x=0;
1095 for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
cristy3ed852e2009-09-05 21:47:34 +00001096 {
1097 MagickBooleanType
1098 sync;
1099
cristy4c08aed2011-07-01 19:47:50 +00001100 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001101 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001102
cristy4c08aed2011-07-01 19:47:50 +00001103 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001104 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001105
cristya26211e2011-10-09 01:24:57 +00001106 register ssize_t
1107 y;
1108
cristybb503372010-05-27 20:51:26 +00001109 size_t
cristyb32b90a2009-09-07 21:45:48 +00001110 height,
1111 width;
cristy3ed852e2009-09-05 21:47:34 +00001112
cristyb32b90a2009-09-07 21:45:48 +00001113 width=tile_width;
cristybb503372010-05-27 20:51:26 +00001114 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
cristya45d2ec2011-08-24 13:17:19 +00001115 width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
cristyb32b90a2009-09-07 21:45:48 +00001116 height=tile_height;
cristybb503372010-05-27 20:51:26 +00001117 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
cristya45d2ec2011-08-24 13:17:19 +00001118 height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
cristydaa97692009-09-13 02:10:35 +00001119 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1120 exception);
cristy4c08aed2011-07-01 19:47:50 +00001121 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001122 {
1123 status=MagickFalse;
1124 break;
1125 }
cristybb503372010-05-27 20:51:26 +00001126 for (y=0; y < (ssize_t) width; y++)
cristy3ed852e2009-09-05 21:47:34 +00001127 {
cristy4c08aed2011-07-01 19:47:50 +00001128 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001129 *restrict tile_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001130
cristybb503372010-05-27 20:51:26 +00001131 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001132 x;
1133
cristy27709ef2011-09-18 02:53:53 +00001134 if (status == MagickFalse)
1135 continue;
cristybb503372010-05-27 20:51:26 +00001136 q=QueueCacheViewAuthenticPixels(rotate_view,(ssize_t)
cristy92611572011-07-07 16:02:42 +00001137 (rotate_image->columns-(tile_y+height)),y+tile_x,height,1,
1138 exception);
cristyacd2ed22011-08-30 01:44:23 +00001139 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001140 {
1141 status=MagickFalse;
cristy27709ef2011-09-18 02:53:53 +00001142 continue;
cristy3ed852e2009-09-05 21:47:34 +00001143 }
cristyed231572011-07-14 02:18:59 +00001144 tile_pixels=p+((height-1)*width+y)*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001145 for (x=0; x < (ssize_t) height; x++)
cristy3ed852e2009-09-05 21:47:34 +00001146 {
cristya45d2ec2011-08-24 13:17:19 +00001147 register ssize_t
1148 i;
1149
1150 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1151 {
1152 PixelChannel
1153 channel;
1154
1155 PixelTrait
1156 rotate_traits,
1157 traits;
1158
1159 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
cristya45d2ec2011-08-24 13:17:19 +00001160 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
1161 rotate_traits=GetPixelChannelMapTraits(rotate_image,channel);
cristy010d7d12011-08-31 01:02:48 +00001162 if ((traits == UndefinedPixelTrait) ||
1163 (rotate_traits == UndefinedPixelTrait))
cristya45d2ec2011-08-24 13:17:19 +00001164 continue;
cristy0beccfa2011-09-25 20:47:53 +00001165 SetPixelChannel(rotate_image,channel,tile_pixels[i],q);
cristya45d2ec2011-08-24 13:17:19 +00001166 }
cristyed231572011-07-14 02:18:59 +00001167 tile_pixels-=width*GetPixelChannels(image);
1168 q+=GetPixelChannels(rotate_image);
cristy3ed852e2009-09-05 21:47:34 +00001169 }
cristy3ed852e2009-09-05 21:47:34 +00001170 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1171 if (sync == MagickFalse)
1172 status=MagickFalse;
1173 }
1174 }
1175 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1176 {
1177 MagickBooleanType
1178 proceed;
1179
cristy21e03412011-09-25 22:39:35 +00001180#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy7b650b52011-10-09 01:13:39 +00001181 #pragma omp critical (MagickCore_IntegralRotateImage)
cristy21e03412011-09-25 22:39:35 +00001182#endif
cristyb32b90a2009-09-07 21:45:48 +00001183 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
cristy3ed852e2009-09-05 21:47:34 +00001184 image->rows);
1185 if (proceed == MagickFalse)
1186 status=MagickFalse;
1187 }
1188 }
cristyecc2c142010-01-17 22:25:46 +00001189 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1190 image->rows-1,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001191 Swap(page.width,page.height);
1192 Swap(page.x,page.y);
1193 if (page.width != 0)
cristybb503372010-05-27 20:51:26 +00001194 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
cristy3ed852e2009-09-05 21:47:34 +00001195 break;
1196 }
1197 case 2:
1198 {
1199 /*
1200 Rotate 180 degrees.
1201 */
cristye31fbf02011-10-10 11:58:31 +00001202#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy7b650b52011-10-09 01:13:39 +00001203 #pragma omp parallel for schedule(dynamic,4) shared(progress,status) omp_throttle(1)
cristydaa97692009-09-13 02:10:35 +00001204#endif
cristybb503372010-05-27 20:51:26 +00001205 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001206 {
1207 MagickBooleanType
1208 sync;
1209
cristy4c08aed2011-07-01 19:47:50 +00001210 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001211 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001212
cristy4c08aed2011-07-01 19:47:50 +00001213 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001214 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001215
cristy2be50602011-10-09 01:28:13 +00001216 register ssize_t
1217 x;
1218
cristy3ed852e2009-09-05 21:47:34 +00001219 if (status == MagickFalse)
1220 continue;
cristya45d2ec2011-08-24 13:17:19 +00001221 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1222 q=QueueCacheViewAuthenticPixels(rotate_view,0,(ssize_t) (image->rows-y-
1223 1),image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001224 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00001225 {
1226 status=MagickFalse;
1227 continue;
1228 }
cristyed231572011-07-14 02:18:59 +00001229 q+=GetPixelChannels(rotate_image)*image->columns;
cristybb503372010-05-27 20:51:26 +00001230 for (x=0; x < (ssize_t) image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00001231 {
cristya45d2ec2011-08-24 13:17:19 +00001232 register ssize_t
1233 i;
1234
cristyed231572011-07-14 02:18:59 +00001235 q-=GetPixelChannels(rotate_image);
cristya45d2ec2011-08-24 13:17:19 +00001236 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1237 {
1238 PixelChannel
1239 channel;
1240
1241 PixelTrait
1242 rotate_traits,
1243 traits;
1244
1245 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
cristya45d2ec2011-08-24 13:17:19 +00001246 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
1247 rotate_traits=GetPixelChannelMapTraits(rotate_image,channel);
cristy010d7d12011-08-31 01:02:48 +00001248 if ((traits == UndefinedPixelTrait) ||
1249 (rotate_traits == UndefinedPixelTrait))
cristya45d2ec2011-08-24 13:17:19 +00001250 continue;
cristy0beccfa2011-09-25 20:47:53 +00001251 SetPixelChannel(rotate_image,channel,p[i],q);
cristya45d2ec2011-08-24 13:17:19 +00001252 }
cristyed231572011-07-14 02:18:59 +00001253 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001254 }
cristy3ed852e2009-09-05 21:47:34 +00001255 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1256 if (sync == MagickFalse)
1257 status=MagickFalse;
1258 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1259 {
1260 MagickBooleanType
1261 proceed;
1262
cristy21e03412011-09-25 22:39:35 +00001263#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy7b650b52011-10-09 01:13:39 +00001264 #pragma omp critical (MagickCore_IntegralRotateImage)
cristy21e03412011-09-25 22:39:35 +00001265#endif
cristy0729beb2011-09-25 23:29:32 +00001266 proceed=SetImageProgress(image,RotateImageTag,progress++,
cristy21e03412011-09-25 22:39:35 +00001267 image->rows);
1268 if (proceed == MagickFalse)
1269 status=MagickFalse;
1270 }
1271 }
1272 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1273 image->rows-1,image->rows);
1274 Swap(page.width,page.height);
1275 Swap(page.x,page.y);
1276 if (page.width != 0)
1277 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1278 break;
1279 }
cristy3ed852e2009-09-05 21:47:34 +00001280 case 3:
1281 {
cristybb503372010-05-27 20:51:26 +00001282 size_t
cristyb32b90a2009-09-07 21:45:48 +00001283 tile_height,
1284 tile_width;
1285
cristyd40deb62011-03-09 00:52:27 +00001286 ssize_t
1287 tile_y;
1288
cristy3ed852e2009-09-05 21:47:34 +00001289 /*
1290 Rotate 270 degrees.
1291 */
cristyb32b90a2009-09-07 21:45:48 +00001292 GetPixelCacheTileSize(image,&tile_width,&tile_height);
cristy11b69e32011-09-29 16:32:44 +00001293 tile_width=image->columns;
cristye31fbf02011-10-10 11:58:31 +00001294#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyc8388eb2011-10-10 11:56:42 +00001295 #pragma omp parallel for schedule(dynamic,4) shared(progress,status) omp_throttle(1)
cristyad11aac2011-09-25 21:29:16 +00001296#endif
1297 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
cristy3ed852e2009-09-05 21:47:34 +00001298 {
cristybb503372010-05-27 20:51:26 +00001299 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001300 tile_x;
1301
1302 if (status == MagickFalse)
1303 continue;
cristy26295322011-09-22 00:50:36 +00001304 tile_x=0;
1305 for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
cristy3ed852e2009-09-05 21:47:34 +00001306 {
1307 MagickBooleanType
1308 sync;
1309
cristy4c08aed2011-07-01 19:47:50 +00001310 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001311 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001312
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
cristy2be50602011-10-09 01:28:13 +00001316 register ssize_t
1317 y;
1318
cristybb503372010-05-27 20:51:26 +00001319 size_t
cristyb32b90a2009-09-07 21:45:48 +00001320 height,
1321 width;
cristy3ed852e2009-09-05 21:47:34 +00001322
cristyb32b90a2009-09-07 21:45:48 +00001323 width=tile_width;
cristybb503372010-05-27 20:51:26 +00001324 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
cristya45d2ec2011-08-24 13:17:19 +00001325 width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
cristyb32b90a2009-09-07 21:45:48 +00001326 height=tile_height;
cristybb503372010-05-27 20:51:26 +00001327 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
cristya45d2ec2011-08-24 13:17:19 +00001328 height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
1329 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1330 exception);
cristy4c08aed2011-07-01 19:47:50 +00001331 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001332 {
1333 status=MagickFalse;
1334 break;
1335 }
cristybb503372010-05-27 20:51:26 +00001336 for (y=0; y < (ssize_t) width; y++)
cristy3ed852e2009-09-05 21:47:34 +00001337 {
cristy4c08aed2011-07-01 19:47:50 +00001338 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001339 *restrict tile_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001340
cristybb503372010-05-27 20:51:26 +00001341 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001342 x;
1343
cristy27709ef2011-09-18 02:53:53 +00001344 if (status == MagickFalse)
1345 continue;
cristya45d2ec2011-08-24 13:17:19 +00001346 q=QueueCacheViewAuthenticPixels(rotate_view,tile_y,(ssize_t) (y+
1347 rotate_image->rows-(tile_x+width)),height,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001348 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001349 {
1350 status=MagickFalse;
cristy27709ef2011-09-18 02:53:53 +00001351 continue;
cristy3ed852e2009-09-05 21:47:34 +00001352 }
cristyed231572011-07-14 02:18:59 +00001353 tile_pixels=p+((width-1)-y)*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001354 for (x=0; x < (ssize_t) height; x++)
cristy3ed852e2009-09-05 21:47:34 +00001355 {
cristya45d2ec2011-08-24 13:17:19 +00001356 register ssize_t
1357 i;
1358
1359 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1360 {
1361 PixelChannel
1362 channel;
1363
1364 PixelTrait
1365 rotate_traits,
1366 traits;
1367
1368 traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
cristya45d2ec2011-08-24 13:17:19 +00001369 channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
1370 rotate_traits=GetPixelChannelMapTraits(rotate_image,channel);
cristy010d7d12011-08-31 01:02:48 +00001371 if ((traits == UndefinedPixelTrait) ||
1372 (rotate_traits == UndefinedPixelTrait))
cristya45d2ec2011-08-24 13:17:19 +00001373 continue;
cristy0beccfa2011-09-25 20:47:53 +00001374 SetPixelChannel(rotate_image,channel,tile_pixels[i],q);
cristya45d2ec2011-08-24 13:17:19 +00001375 }
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
cristy21e03412011-09-25 22:39:35 +00001389#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy7b650b52011-10-09 01:13:39 +00001390 #pragma omp critical (MagickCore_IntegralRotateImage)
cristy21e03412011-09-25 22:39:35 +00001391#endif
1392 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
1393 image->rows);
1394 if (proceed == MagickFalse)
1395 status=MagickFalse;
1396 }
1397 }
1398 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1399 image->rows-1,image->rows);
1400 Swap(page.width,page.height);
1401 Swap(page.x,page.y);
1402 if (page.width != 0)
1403 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1404 break;
1405 }
cristy3ed852e2009-09-05 21:47:34 +00001406 }
1407 rotate_view=DestroyCacheView(rotate_view);
1408 image_view=DestroyCacheView(image_view);
1409 rotate_image->type=image->type;
1410 rotate_image->page=page;
1411 if (status == MagickFalse)
1412 rotate_image=DestroyImage(rotate_image);
1413 return(rotate_image);
1414}
1415
1416/*
1417%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1418% %
1419% %
1420% %
1421+ X S h e a r I m a g e %
1422% %
1423% %
1424% %
1425%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1426%
1427% XShearImage() shears the image in the X direction with a shear angle of
1428% 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1429% negative angles shear clockwise. Angles are measured relative to a vertical
1430% Y-axis. X shears will widen an image creating 'empty' triangles on the left
1431% and right sides of the source image.
1432%
1433% The format of the XShearImage method is:
1434%
1435% MagickBooleanType XShearImage(Image *image,const MagickRealType degrees,
cristybb503372010-05-27 20:51:26 +00001436% const size_t width,const size_t height,
1437% const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001438%
1439% A description of each parameter follows.
1440%
1441% o image: the image.
1442%
cristycee97112010-05-28 00:44:52 +00001443% o degrees: A MagickRealType representing the shearing angle along the X
cristy3ed852e2009-09-05 21:47:34 +00001444% axis.
1445%
1446% o width, height, x_offset, y_offset: Defines a region of the image
1447% to shear.
1448%
cristyecc2c142010-01-17 22:25:46 +00001449% o exception: return any errors or warnings in this structure.
1450%
cristy3ed852e2009-09-05 21:47:34 +00001451*/
1452static MagickBooleanType XShearImage(Image *image,const MagickRealType degrees,
cristybb503372010-05-27 20:51:26 +00001453 const size_t width,const size_t height,const ssize_t x_offset,
1454 const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001455{
1456#define XShearImageTag "XShear/Image"
1457
1458 typedef enum
1459 {
1460 LEFT,
1461 RIGHT
1462 } ShearDirection;
1463
1464 CacheView
1465 *image_view;
1466
cristy3ed852e2009-09-05 21:47:34 +00001467 MagickBooleanType
1468 status;
1469
cristy5f959472010-05-27 22:19:46 +00001470 MagickOffsetType
1471 progress;
1472
cristy4c08aed2011-07-01 19:47:50 +00001473 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001474 background;
1475
cristy5f959472010-05-27 22:19:46 +00001476 ssize_t
1477 y;
1478
cristy3ed852e2009-09-05 21:47:34 +00001479 assert(image != (Image *) NULL);
1480 assert(image->signature == MagickSignature);
1481 if (image->debug != MagickFalse)
1482 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy4c08aed2011-07-01 19:47:50 +00001483 GetPixelInfo(image,&background);
1484 SetPixelInfoPacket(image,&image->background_color,&background);
cristy3ed852e2009-09-05 21:47:34 +00001485 if (image->colorspace == CMYKColorspace)
1486 ConvertRGBToCMYK(&background);
1487 /*
cristy26a3acd2011-03-09 00:50:29 +00001488 X shear image.
cristy3ed852e2009-09-05 21:47:34 +00001489 */
1490 status=MagickTrue;
1491 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00001492 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00001493#if defined(MAGICKCORE_OPENMP_SUPPORT)
1494 #pragma omp parallel for schedule(dynamic,4) shared(progress, status)
cristy3ed852e2009-09-05 21:47:34 +00001495#endif
cristybb503372010-05-27 20:51:26 +00001496 for (y=0; y < (ssize_t) height; y++)
cristy3ed852e2009-09-05 21:47:34 +00001497 {
cristy4c08aed2011-07-01 19:47:50 +00001498 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001499 pixel,
1500 source,
1501 destination;
1502
1503 MagickRealType
1504 area,
1505 displacement;
1506
cristy4c08aed2011-07-01 19:47:50 +00001507 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001508 *restrict p,
1509 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001510
cristyd40deb62011-03-09 00:52:27 +00001511 register ssize_t
1512 i;
1513
cristy3ed852e2009-09-05 21:47:34 +00001514 ShearDirection
1515 direction;
1516
cristyd40deb62011-03-09 00:52:27 +00001517 ssize_t
1518 step;
1519
cristy3ed852e2009-09-05 21:47:34 +00001520 if (status == MagickFalse)
1521 continue;
1522 p=GetCacheViewAuthenticPixels(image_view,0,y_offset+y,image->columns,1,
1523 exception);
cristy4c08aed2011-07-01 19:47:50 +00001524 if (p == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001525 {
1526 status=MagickFalse;
1527 continue;
1528 }
cristyed231572011-07-14 02:18:59 +00001529 p+=x_offset*GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001530 displacement=degrees*(MagickRealType) (y-height/2.0);
1531 if (displacement == 0.0)
1532 continue;
1533 if (displacement > 0.0)
1534 direction=RIGHT;
1535 else
1536 {
1537 displacement*=(-1.0);
1538 direction=LEFT;
1539 }
cristybb503372010-05-27 20:51:26 +00001540 step=(ssize_t) floor((double) displacement);
cristy3ed852e2009-09-05 21:47:34 +00001541 area=(MagickRealType) (displacement-step);
1542 step++;
1543 pixel=background;
cristy4c08aed2011-07-01 19:47:50 +00001544 GetPixelInfo(image,&source);
1545 GetPixelInfo(image,&destination);
cristy3ed852e2009-09-05 21:47:34 +00001546 switch (direction)
1547 {
1548 case LEFT:
1549 {
1550 /*
1551 Transfer pixels left-to-right.
1552 */
1553 if (step > x_offset)
1554 break;
cristyed231572011-07-14 02:18:59 +00001555 q=p-step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001556 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001557 {
1558 if ((x_offset+i) < step)
1559 {
cristyed231572011-07-14 02:18:59 +00001560 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001561 SetPixelInfo(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001562 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001563 continue;
1564 }
cristy4c08aed2011-07-01 19:47:50 +00001565 SetPixelInfo(image,p,&source);
1566 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
cristyf53a3bb2011-10-09 02:13:12 +00001567 &source,(MagickRealType) GetPixelAlpha(image,p),area,&destination);
cristy4c08aed2011-07-01 19:47:50 +00001568 SetPixelPixelInfo(image,&destination,q);
1569 SetPixelInfo(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001570 p+=GetPixelChannels(image);
1571 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001572 }
cristy4c08aed2011-07-01 19:47:50 +00001573 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1574 &background,(MagickRealType) background.alpha,area,&destination);
1575 SetPixelPixelInfo(image,&destination,q);
cristyed231572011-07-14 02:18:59 +00001576 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001577 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001578 {
1579 SetPixelPixelInfo(image,&background,q);
cristyed231572011-07-14 02:18:59 +00001580 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001581 }
cristy3ed852e2009-09-05 21:47:34 +00001582 break;
1583 }
1584 case RIGHT:
1585 {
1586 /*
1587 Transfer pixels right-to-left.
1588 */
cristyed231572011-07-14 02:18:59 +00001589 p+=width*GetPixelChannels(image);
1590 q=p+step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001591 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001592 {
cristyed231572011-07-14 02:18:59 +00001593 p-=GetPixelChannels(image);
1594 q-=GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001595 if ((size_t) (x_offset+width+step-i) >= image->columns)
cristy3ed852e2009-09-05 21:47:34 +00001596 continue;
cristy4c08aed2011-07-01 19:47:50 +00001597 SetPixelInfo(image,p,&source);
1598 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
cristyf53a3bb2011-10-09 02:13:12 +00001599 &source,(MagickRealType) GetPixelAlpha(image,p),area,&destination);
cristy4c08aed2011-07-01 19:47:50 +00001600 SetPixelPixelInfo(image,&destination,q);
1601 SetPixelInfo(image,p,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001602 }
cristy4c08aed2011-07-01 19:47:50 +00001603 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1604 &background,(MagickRealType) background.alpha,area,&destination);
cristyed231572011-07-14 02:18:59 +00001605 q-=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001606 SetPixelPixelInfo(image,&destination,q);
cristy3ed852e2009-09-05 21:47:34 +00001607 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001608 {
cristyed231572011-07-14 02:18:59 +00001609 q-=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001610 SetPixelPixelInfo(image,&background,q);
1611 }
cristy3ed852e2009-09-05 21:47:34 +00001612 break;
1613 }
1614 }
1615 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1616 status=MagickFalse;
1617 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1618 {
1619 MagickBooleanType
1620 proceed;
1621
cristyb5d5f722009-11-04 03:03:49 +00001622#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy7b650b52011-10-09 01:13:39 +00001623 #pragma omp critical (MagickCore_XShearImage)
cristy3ed852e2009-09-05 21:47:34 +00001624#endif
1625 proceed=SetImageProgress(image,XShearImageTag,progress++,height);
1626 if (proceed == MagickFalse)
1627 status=MagickFalse;
1628 }
1629 }
1630 image_view=DestroyCacheView(image_view);
1631 return(status);
1632}
1633
1634/*
1635%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1636% %
1637% %
1638% %
1639+ Y S h e a r I m a g e %
1640% %
1641% %
1642% %
1643%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1644%
1645% YShearImage shears the image in the Y direction with a shear angle of
1646% 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1647% negative angles shear clockwise. Angles are measured relative to a
1648% horizontal X-axis. Y shears will increase the height of an image creating
1649% 'empty' triangles on the top and bottom of the source image.
1650%
1651% The format of the YShearImage method is:
1652%
1653% MagickBooleanType YShearImage(Image *image,const MagickRealType degrees,
cristybb503372010-05-27 20:51:26 +00001654% const size_t width,const size_t height,
1655% const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001656%
1657% A description of each parameter follows.
1658%
1659% o image: the image.
1660%
cristycee97112010-05-28 00:44:52 +00001661% o degrees: A MagickRealType representing the shearing angle along the Y
cristy3ed852e2009-09-05 21:47:34 +00001662% axis.
1663%
1664% o width, height, x_offset, y_offset: Defines a region of the image
1665% to shear.
1666%
cristyecc2c142010-01-17 22:25:46 +00001667% o exception: return any errors or warnings in this structure.
1668%
cristy3ed852e2009-09-05 21:47:34 +00001669*/
1670static MagickBooleanType YShearImage(Image *image,const MagickRealType degrees,
cristybb503372010-05-27 20:51:26 +00001671 const size_t width,const size_t height,const ssize_t x_offset,
1672 const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001673{
1674#define YShearImageTag "YShear/Image"
1675
1676 typedef enum
1677 {
1678 UP,
1679 DOWN
1680 } ShearDirection;
1681
1682 CacheView
1683 *image_view;
1684
cristy3ed852e2009-09-05 21:47:34 +00001685 MagickBooleanType
1686 status;
1687
cristy5f959472010-05-27 22:19:46 +00001688 MagickOffsetType
1689 progress;
1690
cristy4c08aed2011-07-01 19:47:50 +00001691 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001692 background;
1693
cristy5f959472010-05-27 22:19:46 +00001694 ssize_t
1695 x;
1696
cristy3ed852e2009-09-05 21:47:34 +00001697 assert(image != (Image *) NULL);
1698 assert(image->signature == MagickSignature);
1699 if (image->debug != MagickFalse)
1700 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy4c08aed2011-07-01 19:47:50 +00001701 GetPixelInfo(image,&background);
1702 SetPixelInfoPacket(image,&image->background_color,&background);
cristy3ed852e2009-09-05 21:47:34 +00001703 if (image->colorspace == CMYKColorspace)
1704 ConvertRGBToCMYK(&background);
1705 /*
1706 Y Shear image.
1707 */
1708 status=MagickTrue;
1709 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00001710 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00001711#if defined(MAGICKCORE_OPENMP_SUPPORT)
1712 #pragma omp parallel for schedule(dynamic,4) shared(progress, status)
cristy3ed852e2009-09-05 21:47:34 +00001713#endif
cristybb503372010-05-27 20:51:26 +00001714 for (x=0; x < (ssize_t) width; x++)
cristy3ed852e2009-09-05 21:47:34 +00001715 {
cristybb503372010-05-27 20:51:26 +00001716 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001717 step;
1718
cristy3ed852e2009-09-05 21:47:34 +00001719 MagickRealType
1720 area,
1721 displacement;
1722
cristy4c08aed2011-07-01 19:47:50 +00001723 PixelInfo
1724 pixel,
1725 source,
1726 destination;
1727
1728 register Quantum
1729 *restrict p,
1730 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001731
cristybb503372010-05-27 20:51:26 +00001732 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001733 i;
1734
cristy3ed852e2009-09-05 21:47:34 +00001735 ShearDirection
1736 direction;
1737
1738 if (status == MagickFalse)
1739 continue;
1740 p=GetCacheViewAuthenticPixels(image_view,x_offset+x,0,1,image->rows,
1741 exception);
cristy4c08aed2011-07-01 19:47:50 +00001742 if (p == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001743 {
1744 status=MagickFalse;
1745 continue;
1746 }
cristyed231572011-07-14 02:18:59 +00001747 p+=y_offset*GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001748 displacement=degrees*(MagickRealType) (x-width/2.0);
1749 if (displacement == 0.0)
1750 continue;
1751 if (displacement > 0.0)
1752 direction=DOWN;
1753 else
1754 {
1755 displacement*=(-1.0);
1756 direction=UP;
1757 }
cristybb503372010-05-27 20:51:26 +00001758 step=(ssize_t) floor((double) displacement);
cristy3ed852e2009-09-05 21:47:34 +00001759 area=(MagickRealType) (displacement-step);
1760 step++;
1761 pixel=background;
cristy4c08aed2011-07-01 19:47:50 +00001762 GetPixelInfo(image,&source);
1763 GetPixelInfo(image,&destination);
cristy3ed852e2009-09-05 21:47:34 +00001764 switch (direction)
1765 {
1766 case UP:
1767 {
1768 /*
1769 Transfer pixels top-to-bottom.
1770 */
1771 if (step > y_offset)
1772 break;
cristyed231572011-07-14 02:18:59 +00001773 q=p-step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001774 for (i=0; i < (ssize_t) height; i++)
cristy3ed852e2009-09-05 21:47:34 +00001775 {
1776 if ((y_offset+i) < step)
1777 {
cristyed231572011-07-14 02:18:59 +00001778 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001779 SetPixelInfo(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001780 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001781 continue;
1782 }
cristy4c08aed2011-07-01 19:47:50 +00001783 SetPixelInfo(image,p,&source);
1784 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1785 &source,(MagickRealType) GetPixelAlpha(image,p),area,
1786 &destination);
1787 SetPixelPixelInfo(image,&destination,q);
1788 SetPixelInfo(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001789 p+=GetPixelChannels(image);
1790 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001791 }
cristy4c08aed2011-07-01 19:47:50 +00001792 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1793 &background,(MagickRealType) background.alpha,area,&destination);
1794 SetPixelPixelInfo(image,&destination,q);
cristyed231572011-07-14 02:18:59 +00001795 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001796 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001797 {
1798 SetPixelPixelInfo(image,&background,q);
cristyed231572011-07-14 02:18:59 +00001799 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001800 }
cristy3ed852e2009-09-05 21:47:34 +00001801 break;
1802 }
1803 case DOWN:
1804 {
1805 /*
1806 Transfer pixels bottom-to-top.
1807 */
cristyed231572011-07-14 02:18:59 +00001808 p+=height*GetPixelChannels(image);
1809 q=p+step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001810 for (i=0; i < (ssize_t) height; i++)
cristy3ed852e2009-09-05 21:47:34 +00001811 {
cristyed231572011-07-14 02:18:59 +00001812 p-=GetPixelChannels(image);
1813 q-=GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001814 if ((size_t) (y_offset+height+step-i) >= image->rows)
cristy3ed852e2009-09-05 21:47:34 +00001815 continue;
cristy4c08aed2011-07-01 19:47:50 +00001816 SetPixelInfo(image,p,&source);
1817 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1818 &source,(MagickRealType) GetPixelAlpha(image,p),area,
1819 &destination);
1820 SetPixelPixelInfo(image,&destination,q);
1821 SetPixelInfo(image,p,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001822 }
cristy4c08aed2011-07-01 19:47:50 +00001823 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1824 &background,(MagickRealType) background.alpha,area,&destination);
cristyed231572011-07-14 02:18:59 +00001825 q-=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001826 SetPixelPixelInfo(image,&destination,q);
cristy3ed852e2009-09-05 21:47:34 +00001827 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001828 {
cristyed231572011-07-14 02:18:59 +00001829 q-=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001830 SetPixelPixelInfo(image,&background,q);
1831 }
cristy3ed852e2009-09-05 21:47:34 +00001832 break;
1833 }
1834 }
1835 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1836 status=MagickFalse;
1837 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1838 {
1839 MagickBooleanType
1840 proceed;
1841
cristyb5d5f722009-11-04 03:03:49 +00001842#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy7b650b52011-10-09 01:13:39 +00001843 #pragma omp critical (MagickCore_YShearImage)
cristy3ed852e2009-09-05 21:47:34 +00001844#endif
1845 proceed=SetImageProgress(image,YShearImageTag,progress++,image->rows);
1846 if (proceed == MagickFalse)
1847 status=MagickFalse;
1848 }
1849 }
1850 image_view=DestroyCacheView(image_view);
1851 return(status);
1852}
1853
1854/*
1855%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1856% %
1857% %
1858% %
1859% R o t a t e I m a g e %
1860% %
1861% %
1862% %
1863%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1864%
1865% RotateImage() creates a new image that is a rotated copy of an existing
1866% one. Positive angles rotate counter-clockwise (right-hand rule), while
1867% negative angles rotate clockwise. Rotated images are usually larger than
1868% the originals and have 'empty' triangular corners. X axis. Empty
1869% triangles left over from shearing the image are filled with the background
1870% color defined by member 'background_color' of the image. RotateImage
1871% allocates the memory necessary for the new Image structure and returns a
1872% pointer to the new image.
1873%
1874% RotateImage() is based on the paper "A Fast Algorithm for General
1875% Raster Rotatation" by Alan W. Paeth. RotateImage is adapted from a similar
1876% method based on the Paeth paper written by Michael Halle of the Spatial
1877% Imaging Group, MIT Media Lab.
1878%
1879% The format of the RotateImage method is:
1880%
1881% Image *RotateImage(const Image *image,const double degrees,
1882% ExceptionInfo *exception)
1883%
1884% A description of each parameter follows.
1885%
1886% o image: the image.
1887%
1888% o degrees: Specifies the number of degrees to rotate the image.
1889%
1890% o exception: return any errors or warnings in this structure.
1891%
1892*/
1893MagickExport Image *RotateImage(const Image *image,const double degrees,
1894 ExceptionInfo *exception)
1895{
1896 Image
1897 *integral_image,
1898 *rotate_image;
1899
cristyecc2c142010-01-17 22:25:46 +00001900 MagickBooleanType
1901 status;
1902
cristy3ed852e2009-09-05 21:47:34 +00001903 MagickRealType
1904 angle;
1905
1906 PointInfo
1907 shear;
1908
1909 RectangleInfo
1910 border_info;
1911
cristybb503372010-05-27 20:51:26 +00001912 size_t
cristy3ed852e2009-09-05 21:47:34 +00001913 height,
1914 rotations,
1915 width,
1916 y_width;
1917
cristy4c08aed2011-07-01 19:47:50 +00001918 ssize_t
1919 x_offset,
1920 y_offset;
1921
cristy3ed852e2009-09-05 21:47:34 +00001922 /*
1923 Adjust rotation angle.
1924 */
1925 assert(image != (Image *) NULL);
1926 assert(image->signature == MagickSignature);
1927 if (image->debug != MagickFalse)
1928 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1929 assert(exception != (ExceptionInfo *) NULL);
1930 assert(exception->signature == MagickSignature);
1931 angle=degrees;
1932 while (angle < -45.0)
1933 angle+=360.0;
1934 for (rotations=0; angle > 45.0; rotations++)
1935 angle-=90.0;
1936 rotations%=4;
1937 /*
1938 Calculate shear equations.
1939 */
1940 integral_image=IntegralRotateImage(image,rotations,exception);
1941 if (integral_image == (Image *) NULL)
1942 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1943 shear.x=(-tan((double) DegreesToRadians(angle)/2.0));
1944 shear.y=sin((double) DegreesToRadians(angle));
1945 if ((shear.x == 0.0) && (shear.y == 0.0))
1946 return(integral_image);
cristy574cc262011-08-05 01:23:58 +00001947 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001948 {
cristy3ed852e2009-09-05 21:47:34 +00001949 integral_image=DestroyImage(integral_image);
1950 return(integral_image);
1951 }
1952 if (integral_image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00001953 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00001954 /*
1955 Compute image size.
1956 */
1957 width=image->columns;
1958 height=image->rows;
1959 if ((rotations == 1) || (rotations == 3))
1960 {
1961 width=image->rows;
1962 height=image->columns;
1963 }
cristybb503372010-05-27 20:51:26 +00001964 y_width=width+(ssize_t) floor(fabs(shear.x)*height+0.5);
cristycee97112010-05-28 00:44:52 +00001965 x_offset=(ssize_t) ceil((double) width+((fabs(shear.y)*height)-width)/2.0-
1966 0.5);
1967 y_offset=(ssize_t) ceil((double) height+((fabs(shear.y)*y_width)-height)/2.0-
1968 0.5);
cristy3ed852e2009-09-05 21:47:34 +00001969 /*
1970 Surround image with a border.
1971 */
1972 integral_image->border_color=integral_image->background_color;
1973 integral_image->compose=CopyCompositeOp;
cristybb503372010-05-27 20:51:26 +00001974 border_info.width=(size_t) x_offset;
1975 border_info.height=(size_t) y_offset;
cristy633f0c62011-09-15 13:27:36 +00001976 rotate_image=BorderImage(integral_image,&border_info,image->compose,
1977 exception);
cristy3ed852e2009-09-05 21:47:34 +00001978 integral_image=DestroyImage(integral_image);
1979 if (rotate_image == (Image *) NULL)
1980 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1981 /*
1982 Rotate the image.
1983 */
cristyeaedf062010-05-29 22:36:02 +00001984 status=XShearImage(rotate_image,shear.x,width,height,x_offset,(ssize_t)
1985 (rotate_image->rows-height)/2,exception);
cristyecc2c142010-01-17 22:25:46 +00001986 if (status == MagickFalse)
1987 {
1988 rotate_image=DestroyImage(rotate_image);
1989 return((Image *) NULL);
1990 }
cristyeaedf062010-05-29 22:36:02 +00001991 status=YShearImage(rotate_image,shear.y,y_width,height,(ssize_t)
1992 (rotate_image->columns-y_width)/2,y_offset,exception);
cristyecc2c142010-01-17 22:25:46 +00001993 if (status == MagickFalse)
1994 {
1995 rotate_image=DestroyImage(rotate_image);
1996 return((Image *) NULL);
1997 }
cristyeaedf062010-05-29 22:36:02 +00001998 status=XShearImage(rotate_image,shear.x,y_width,rotate_image->rows,(ssize_t)
1999 (rotate_image->columns-y_width)/2,0,exception);
cristyecc2c142010-01-17 22:25:46 +00002000 if (status == MagickFalse)
2001 {
2002 rotate_image=DestroyImage(rotate_image);
2003 return((Image *) NULL);
2004 }
2005 status=CropToFitImage(&rotate_image,shear.x,shear.y,(MagickRealType) width,
cristy3ed852e2009-09-05 21:47:34 +00002006 (MagickRealType) height,MagickTrue,exception);
cristyecc2c142010-01-17 22:25:46 +00002007 if (status == MagickFalse)
2008 {
2009 rotate_image=DestroyImage(rotate_image);
2010 return((Image *) NULL);
2011 }
cristy3ed852e2009-09-05 21:47:34 +00002012 rotate_image->compose=image->compose;
2013 rotate_image->page.width=0;
2014 rotate_image->page.height=0;
2015 return(rotate_image);
2016}
2017
2018/*
2019%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2020% %
2021% %
2022% %
2023% S h e a r I m a g e %
2024% %
2025% %
2026% %
2027%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2028%
2029% ShearImage() creates a new image that is a shear_image copy of an existing
cristycee97112010-05-28 00:44:52 +00002030% one. Shearing slides one edge of an image along the X or Y axis, creating
2031% a parallelogram. An X direction shear slides an edge along the X axis,
2032% while a Y direction shear slides an edge along the Y axis. The amount of
cristy3ed852e2009-09-05 21:47:34 +00002033% the shear is controlled by a shear angle. For X direction shears, x_shear
2034% is measured relative to the Y axis, and similarly, for Y direction shears
2035% y_shear is measured relative to the X axis. Empty triangles left over from
2036% shearing the image are filled with the background color defined by member
2037% 'background_color' of the image.. ShearImage() allocates the memory
2038% necessary for the new Image structure and returns a pointer to the new image.
2039%
2040% ShearImage() is based on the paper "A Fast Algorithm for General Raster
2041% Rotatation" by Alan W. Paeth.
2042%
2043% The format of the ShearImage method is:
2044%
2045% Image *ShearImage(const Image *image,const double x_shear,
2046% const double y_shear,ExceptionInfo *exception)
2047%
2048% A description of each parameter follows.
2049%
2050% o image: the image.
2051%
2052% o x_shear, y_shear: Specifies the number of degrees to shear the image.
2053%
2054% o exception: return any errors or warnings in this structure.
2055%
2056*/
2057MagickExport Image *ShearImage(const Image *image,const double x_shear,
2058 const double y_shear,ExceptionInfo *exception)
2059{
2060 Image
2061 *integral_image,
2062 *shear_image;
2063
cristybb503372010-05-27 20:51:26 +00002064 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002065 x_offset,
2066 y_offset;
2067
cristyecc2c142010-01-17 22:25:46 +00002068 MagickBooleanType
2069 status;
2070
cristy3ed852e2009-09-05 21:47:34 +00002071 PointInfo
2072 shear;
2073
2074 RectangleInfo
2075 border_info;
2076
cristybb503372010-05-27 20:51:26 +00002077 size_t
cristy3ed852e2009-09-05 21:47:34 +00002078 y_width;
2079
2080 assert(image != (Image *) NULL);
2081 assert(image->signature == MagickSignature);
2082 if (image->debug != MagickFalse)
2083 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2084 assert(exception != (ExceptionInfo *) NULL);
2085 assert(exception->signature == MagickSignature);
2086 if ((x_shear != 0.0) && (fmod(x_shear,90.0) == 0.0))
2087 ThrowImageException(ImageError,"AngleIsDiscontinuous");
2088 if ((y_shear != 0.0) && (fmod(y_shear,90.0) == 0.0))
2089 ThrowImageException(ImageError,"AngleIsDiscontinuous");
2090 /*
2091 Initialize shear angle.
2092 */
2093 integral_image=CloneImage(image,0,0,MagickTrue,exception);
2094 if (integral_image == (Image *) NULL)
2095 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2096 shear.x=(-tan(DegreesToRadians(fmod(x_shear,360.0))));
2097 shear.y=tan(DegreesToRadians(fmod(y_shear,360.0)));
2098 if ((shear.x == 0.0) && (shear.y == 0.0))
2099 return(integral_image);
cristy574cc262011-08-05 01:23:58 +00002100 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00002101 {
cristy3ed852e2009-09-05 21:47:34 +00002102 integral_image=DestroyImage(integral_image);
2103 return(integral_image);
2104 }
2105 if (integral_image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00002106 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00002107 /*
2108 Compute image size.
2109 */
cristybb503372010-05-27 20:51:26 +00002110 y_width=image->columns+(ssize_t) floor(fabs(shear.x)*image->rows+0.5);
cristycee97112010-05-28 00:44:52 +00002111 x_offset=(ssize_t) ceil((double) image->columns+((fabs(shear.x)*image->rows)-
cristy06609ee2010-03-17 20:21:27 +00002112 image->columns)/2.0-0.5);
cristycee97112010-05-28 00:44:52 +00002113 y_offset=(ssize_t) ceil((double) image->rows+((fabs(shear.y)*y_width)-
2114 image->rows)/2.0-0.5);
cristy3ed852e2009-09-05 21:47:34 +00002115 /*
2116 Surround image with border.
2117 */
2118 integral_image->border_color=integral_image->background_color;
2119 integral_image->compose=CopyCompositeOp;
cristybb503372010-05-27 20:51:26 +00002120 border_info.width=(size_t) x_offset;
2121 border_info.height=(size_t) y_offset;
cristy633f0c62011-09-15 13:27:36 +00002122 shear_image=BorderImage(integral_image,&border_info,image->compose,exception);
cristyecc2c142010-01-17 22:25:46 +00002123 integral_image=DestroyImage(integral_image);
cristy3ed852e2009-09-05 21:47:34 +00002124 if (shear_image == (Image *) NULL)
2125 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +00002126 /*
2127 Shear the image.
2128 */
2129 if (shear_image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00002130 (void) SetImageAlphaChannel(shear_image,OpaqueAlphaChannel,exception);
cristyecc2c142010-01-17 22:25:46 +00002131 status=XShearImage(shear_image,shear.x,image->columns,image->rows,x_offset,
cristyeaedf062010-05-29 22:36:02 +00002132 (ssize_t) (shear_image->rows-image->rows)/2,exception);
cristyecc2c142010-01-17 22:25:46 +00002133 if (status == MagickFalse)
2134 {
2135 shear_image=DestroyImage(shear_image);
2136 return((Image *) NULL);
2137 }
cristyeaedf062010-05-29 22:36:02 +00002138 status=YShearImage(shear_image,shear.y,y_width,image->rows,(ssize_t)
2139 (shear_image->columns-y_width)/2,y_offset,exception);
cristyecc2c142010-01-17 22:25:46 +00002140 if (status == MagickFalse)
2141 {
2142 shear_image=DestroyImage(shear_image);
2143 return((Image *) NULL);
2144 }
2145 status=CropToFitImage(&shear_image,shear.x,shear.y,(MagickRealType)
2146 image->columns,(MagickRealType) image->rows,MagickFalse,exception);
2147 if (status == MagickFalse)
2148 {
2149 shear_image=DestroyImage(shear_image);
2150 return((Image *) NULL);
2151 }
cristy3ed852e2009-09-05 21:47:34 +00002152 shear_image->compose=image->compose;
2153 shear_image->page.width=0;
2154 shear_image->page.height=0;
2155 return(shear_image);
2156}