blob: d0b0157896e77cf91212030a7b34763740f9b0d5 [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
cristy9d8c8ce2011-10-25 16:13:52 +00001479 /*
1480 X shear image.
1481 */
cristy3ed852e2009-09-05 21:47:34 +00001482 assert(image != (Image *) NULL);
1483 assert(image->signature == MagickSignature);
1484 if (image->debug != MagickFalse)
1485 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3ed852e2009-09-05 21:47:34 +00001486 status=MagickTrue;
cristy9d8c8ce2011-10-25 16:13:52 +00001487 background=image->background_color;
cristy3ed852e2009-09-05 21:47:34 +00001488 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00001489 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00001490#if defined(MAGICKCORE_OPENMP_SUPPORT)
1491 #pragma omp parallel for schedule(dynamic,4) shared(progress, status)
cristy3ed852e2009-09-05 21:47:34 +00001492#endif
cristybb503372010-05-27 20:51:26 +00001493 for (y=0; y < (ssize_t) height; y++)
cristy3ed852e2009-09-05 21:47:34 +00001494 {
cristy4c08aed2011-07-01 19:47:50 +00001495 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001496 pixel,
1497 source,
1498 destination;
1499
1500 MagickRealType
1501 area,
1502 displacement;
1503
cristy4c08aed2011-07-01 19:47:50 +00001504 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001505 *restrict p,
1506 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001507
cristyd40deb62011-03-09 00:52:27 +00001508 register ssize_t
1509 i;
1510
cristy3ed852e2009-09-05 21:47:34 +00001511 ShearDirection
1512 direction;
1513
cristyd40deb62011-03-09 00:52:27 +00001514 ssize_t
1515 step;
1516
cristy3ed852e2009-09-05 21:47:34 +00001517 if (status == MagickFalse)
1518 continue;
1519 p=GetCacheViewAuthenticPixels(image_view,0,y_offset+y,image->columns,1,
1520 exception);
cristy4c08aed2011-07-01 19:47:50 +00001521 if (p == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001522 {
1523 status=MagickFalse;
1524 continue;
1525 }
cristyed231572011-07-14 02:18:59 +00001526 p+=x_offset*GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001527 displacement=degrees*(MagickRealType) (y-height/2.0);
1528 if (displacement == 0.0)
1529 continue;
1530 if (displacement > 0.0)
1531 direction=RIGHT;
1532 else
1533 {
1534 displacement*=(-1.0);
1535 direction=LEFT;
1536 }
cristybb503372010-05-27 20:51:26 +00001537 step=(ssize_t) floor((double) displacement);
cristy3ed852e2009-09-05 21:47:34 +00001538 area=(MagickRealType) (displacement-step);
1539 step++;
1540 pixel=background;
cristy4c08aed2011-07-01 19:47:50 +00001541 GetPixelInfo(image,&source);
1542 GetPixelInfo(image,&destination);
cristy3ed852e2009-09-05 21:47:34 +00001543 switch (direction)
1544 {
1545 case LEFT:
1546 {
1547 /*
1548 Transfer pixels left-to-right.
1549 */
1550 if (step > x_offset)
1551 break;
cristyed231572011-07-14 02:18:59 +00001552 q=p-step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001553 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001554 {
1555 if ((x_offset+i) < step)
1556 {
cristyed231572011-07-14 02:18:59 +00001557 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001558 SetPixelInfo(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001559 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001560 continue;
1561 }
cristy4c08aed2011-07-01 19:47:50 +00001562 SetPixelInfo(image,p,&source);
1563 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
cristyf53a3bb2011-10-09 02:13:12 +00001564 &source,(MagickRealType) GetPixelAlpha(image,p),area,&destination);
cristy4c08aed2011-07-01 19:47:50 +00001565 SetPixelPixelInfo(image,&destination,q);
1566 SetPixelInfo(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001567 p+=GetPixelChannels(image);
1568 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001569 }
cristy4c08aed2011-07-01 19:47:50 +00001570 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1571 &background,(MagickRealType) background.alpha,area,&destination);
1572 SetPixelPixelInfo(image,&destination,q);
cristyed231572011-07-14 02:18:59 +00001573 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001574 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001575 {
1576 SetPixelPixelInfo(image,&background,q);
cristyed231572011-07-14 02:18:59 +00001577 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001578 }
cristy3ed852e2009-09-05 21:47:34 +00001579 break;
1580 }
1581 case RIGHT:
1582 {
1583 /*
1584 Transfer pixels right-to-left.
1585 */
cristyed231572011-07-14 02:18:59 +00001586 p+=width*GetPixelChannels(image);
1587 q=p+step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001588 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001589 {
cristyed231572011-07-14 02:18:59 +00001590 p-=GetPixelChannels(image);
1591 q-=GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001592 if ((size_t) (x_offset+width+step-i) >= image->columns)
cristy3ed852e2009-09-05 21:47:34 +00001593 continue;
cristy4c08aed2011-07-01 19:47:50 +00001594 SetPixelInfo(image,p,&source);
1595 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
cristyf53a3bb2011-10-09 02:13:12 +00001596 &source,(MagickRealType) GetPixelAlpha(image,p),area,&destination);
cristy4c08aed2011-07-01 19:47:50 +00001597 SetPixelPixelInfo(image,&destination,q);
1598 SetPixelInfo(image,p,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001599 }
cristy4c08aed2011-07-01 19:47:50 +00001600 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1601 &background,(MagickRealType) background.alpha,area,&destination);
cristyed231572011-07-14 02:18:59 +00001602 q-=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001603 SetPixelPixelInfo(image,&destination,q);
cristy3ed852e2009-09-05 21:47:34 +00001604 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001605 {
cristyed231572011-07-14 02:18:59 +00001606 q-=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001607 SetPixelPixelInfo(image,&background,q);
1608 }
cristy3ed852e2009-09-05 21:47:34 +00001609 break;
1610 }
1611 }
1612 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1613 status=MagickFalse;
1614 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1615 {
1616 MagickBooleanType
1617 proceed;
1618
cristyb5d5f722009-11-04 03:03:49 +00001619#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy7b650b52011-10-09 01:13:39 +00001620 #pragma omp critical (MagickCore_XShearImage)
cristy3ed852e2009-09-05 21:47:34 +00001621#endif
1622 proceed=SetImageProgress(image,XShearImageTag,progress++,height);
1623 if (proceed == MagickFalse)
1624 status=MagickFalse;
1625 }
1626 }
1627 image_view=DestroyCacheView(image_view);
1628 return(status);
1629}
1630
1631/*
1632%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1633% %
1634% %
1635% %
1636+ Y S h e a r I m a g e %
1637% %
1638% %
1639% %
1640%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1641%
1642% YShearImage shears the image in the Y direction with a shear angle of
1643% 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1644% negative angles shear clockwise. Angles are measured relative to a
1645% horizontal X-axis. Y shears will increase the height of an image creating
1646% 'empty' triangles on the top and bottom of the source image.
1647%
1648% The format of the YShearImage method is:
1649%
1650% MagickBooleanType YShearImage(Image *image,const MagickRealType degrees,
cristybb503372010-05-27 20:51:26 +00001651% const size_t width,const size_t height,
1652% const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001653%
1654% A description of each parameter follows.
1655%
1656% o image: the image.
1657%
cristycee97112010-05-28 00:44:52 +00001658% o degrees: A MagickRealType representing the shearing angle along the Y
cristy3ed852e2009-09-05 21:47:34 +00001659% axis.
1660%
1661% o width, height, x_offset, y_offset: Defines a region of the image
1662% to shear.
1663%
cristyecc2c142010-01-17 22:25:46 +00001664% o exception: return any errors or warnings in this structure.
1665%
cristy3ed852e2009-09-05 21:47:34 +00001666*/
1667static MagickBooleanType YShearImage(Image *image,const MagickRealType degrees,
cristybb503372010-05-27 20:51:26 +00001668 const size_t width,const size_t height,const ssize_t x_offset,
1669 const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001670{
1671#define YShearImageTag "YShear/Image"
1672
1673 typedef enum
1674 {
1675 UP,
1676 DOWN
1677 } ShearDirection;
1678
1679 CacheView
1680 *image_view;
1681
cristy3ed852e2009-09-05 21:47:34 +00001682 MagickBooleanType
1683 status;
1684
cristy5f959472010-05-27 22:19:46 +00001685 MagickOffsetType
1686 progress;
1687
cristy4c08aed2011-07-01 19:47:50 +00001688 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001689 background;
1690
cristy5f959472010-05-27 22:19:46 +00001691 ssize_t
1692 x;
1693
cristy9d8c8ce2011-10-25 16:13:52 +00001694 /*
1695 Y Shear image.
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);
cristy3ed852e2009-09-05 21:47:34 +00001701 status=MagickTrue;
1702 progress=0;
cristy9d8c8ce2011-10-25 16:13:52 +00001703 background=image->background_color;
cristy3ed852e2009-09-05 21:47:34 +00001704 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00001705#if defined(MAGICKCORE_OPENMP_SUPPORT)
1706 #pragma omp parallel for schedule(dynamic,4) shared(progress, status)
cristy3ed852e2009-09-05 21:47:34 +00001707#endif
cristybb503372010-05-27 20:51:26 +00001708 for (x=0; x < (ssize_t) width; x++)
cristy3ed852e2009-09-05 21:47:34 +00001709 {
cristybb503372010-05-27 20:51:26 +00001710 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001711 step;
1712
cristy3ed852e2009-09-05 21:47:34 +00001713 MagickRealType
1714 area,
1715 displacement;
1716
cristy4c08aed2011-07-01 19:47:50 +00001717 PixelInfo
1718 pixel,
1719 source,
1720 destination;
1721
1722 register Quantum
1723 *restrict p,
1724 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001725
cristybb503372010-05-27 20:51:26 +00001726 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001727 i;
1728
cristy3ed852e2009-09-05 21:47:34 +00001729 ShearDirection
1730 direction;
1731
1732 if (status == MagickFalse)
1733 continue;
1734 p=GetCacheViewAuthenticPixels(image_view,x_offset+x,0,1,image->rows,
1735 exception);
cristy4c08aed2011-07-01 19:47:50 +00001736 if (p == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001737 {
1738 status=MagickFalse;
1739 continue;
1740 }
cristyed231572011-07-14 02:18:59 +00001741 p+=y_offset*GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001742 displacement=degrees*(MagickRealType) (x-width/2.0);
1743 if (displacement == 0.0)
1744 continue;
1745 if (displacement > 0.0)
1746 direction=DOWN;
1747 else
1748 {
1749 displacement*=(-1.0);
1750 direction=UP;
1751 }
cristybb503372010-05-27 20:51:26 +00001752 step=(ssize_t) floor((double) displacement);
cristy3ed852e2009-09-05 21:47:34 +00001753 area=(MagickRealType) (displacement-step);
1754 step++;
1755 pixel=background;
cristy4c08aed2011-07-01 19:47:50 +00001756 GetPixelInfo(image,&source);
1757 GetPixelInfo(image,&destination);
cristy3ed852e2009-09-05 21:47:34 +00001758 switch (direction)
1759 {
1760 case UP:
1761 {
1762 /*
1763 Transfer pixels top-to-bottom.
1764 */
1765 if (step > y_offset)
1766 break;
cristyed231572011-07-14 02:18:59 +00001767 q=p-step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001768 for (i=0; i < (ssize_t) height; i++)
cristy3ed852e2009-09-05 21:47:34 +00001769 {
1770 if ((y_offset+i) < step)
1771 {
cristyed231572011-07-14 02:18:59 +00001772 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001773 SetPixelInfo(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001774 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001775 continue;
1776 }
cristy4c08aed2011-07-01 19:47:50 +00001777 SetPixelInfo(image,p,&source);
1778 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1779 &source,(MagickRealType) GetPixelAlpha(image,p),area,
1780 &destination);
1781 SetPixelPixelInfo(image,&destination,q);
1782 SetPixelInfo(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001783 p+=GetPixelChannels(image);
1784 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001785 }
cristy4c08aed2011-07-01 19:47:50 +00001786 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1787 &background,(MagickRealType) background.alpha,area,&destination);
1788 SetPixelPixelInfo(image,&destination,q);
cristyed231572011-07-14 02:18:59 +00001789 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001790 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001791 {
1792 SetPixelPixelInfo(image,&background,q);
cristyed231572011-07-14 02:18:59 +00001793 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001794 }
cristy3ed852e2009-09-05 21:47:34 +00001795 break;
1796 }
1797 case DOWN:
1798 {
1799 /*
1800 Transfer pixels bottom-to-top.
1801 */
cristyed231572011-07-14 02:18:59 +00001802 p+=height*GetPixelChannels(image);
1803 q=p+step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001804 for (i=0; i < (ssize_t) height; i++)
cristy3ed852e2009-09-05 21:47:34 +00001805 {
cristyed231572011-07-14 02:18:59 +00001806 p-=GetPixelChannels(image);
1807 q-=GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001808 if ((size_t) (y_offset+height+step-i) >= image->rows)
cristy3ed852e2009-09-05 21:47:34 +00001809 continue;
cristy4c08aed2011-07-01 19:47:50 +00001810 SetPixelInfo(image,p,&source);
1811 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1812 &source,(MagickRealType) GetPixelAlpha(image,p),area,
1813 &destination);
1814 SetPixelPixelInfo(image,&destination,q);
1815 SetPixelInfo(image,p,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001816 }
cristy4c08aed2011-07-01 19:47:50 +00001817 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1818 &background,(MagickRealType) background.alpha,area,&destination);
cristyed231572011-07-14 02:18:59 +00001819 q-=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001820 SetPixelPixelInfo(image,&destination,q);
cristy3ed852e2009-09-05 21:47:34 +00001821 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001822 {
cristyed231572011-07-14 02:18:59 +00001823 q-=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001824 SetPixelPixelInfo(image,&background,q);
1825 }
cristy3ed852e2009-09-05 21:47:34 +00001826 break;
1827 }
1828 }
1829 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1830 status=MagickFalse;
1831 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1832 {
1833 MagickBooleanType
1834 proceed;
1835
cristyb5d5f722009-11-04 03:03:49 +00001836#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy7b650b52011-10-09 01:13:39 +00001837 #pragma omp critical (MagickCore_YShearImage)
cristy3ed852e2009-09-05 21:47:34 +00001838#endif
1839 proceed=SetImageProgress(image,YShearImageTag,progress++,image->rows);
1840 if (proceed == MagickFalse)
1841 status=MagickFalse;
1842 }
1843 }
1844 image_view=DestroyCacheView(image_view);
1845 return(status);
1846}
1847
1848/*
1849%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1850% %
1851% %
1852% %
1853% R o t a t e I m a g e %
1854% %
1855% %
1856% %
1857%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1858%
1859% RotateImage() creates a new image that is a rotated copy of an existing
1860% one. Positive angles rotate counter-clockwise (right-hand rule), while
1861% negative angles rotate clockwise. Rotated images are usually larger than
1862% the originals and have 'empty' triangular corners. X axis. Empty
1863% triangles left over from shearing the image are filled with the background
1864% color defined by member 'background_color' of the image. RotateImage
1865% allocates the memory necessary for the new Image structure and returns a
1866% pointer to the new image.
1867%
1868% RotateImage() is based on the paper "A Fast Algorithm for General
1869% Raster Rotatation" by Alan W. Paeth. RotateImage is adapted from a similar
1870% method based on the Paeth paper written by Michael Halle of the Spatial
1871% Imaging Group, MIT Media Lab.
1872%
1873% The format of the RotateImage method is:
1874%
1875% Image *RotateImage(const Image *image,const double degrees,
1876% ExceptionInfo *exception)
1877%
1878% A description of each parameter follows.
1879%
1880% o image: the image.
1881%
1882% o degrees: Specifies the number of degrees to rotate the image.
1883%
1884% o exception: return any errors or warnings in this structure.
1885%
1886*/
1887MagickExport Image *RotateImage(const Image *image,const double degrees,
1888 ExceptionInfo *exception)
1889{
1890 Image
1891 *integral_image,
1892 *rotate_image;
1893
cristyecc2c142010-01-17 22:25:46 +00001894 MagickBooleanType
1895 status;
1896
cristy3ed852e2009-09-05 21:47:34 +00001897 MagickRealType
1898 angle;
1899
1900 PointInfo
1901 shear;
1902
1903 RectangleInfo
1904 border_info;
1905
cristybb503372010-05-27 20:51:26 +00001906 size_t
cristy3ed852e2009-09-05 21:47:34 +00001907 height,
1908 rotations,
1909 width,
1910 y_width;
1911
cristy4c08aed2011-07-01 19:47:50 +00001912 ssize_t
1913 x_offset,
1914 y_offset;
1915
cristy3ed852e2009-09-05 21:47:34 +00001916 /*
1917 Adjust rotation angle.
1918 */
1919 assert(image != (Image *) NULL);
1920 assert(image->signature == MagickSignature);
1921 if (image->debug != MagickFalse)
1922 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1923 assert(exception != (ExceptionInfo *) NULL);
1924 assert(exception->signature == MagickSignature);
1925 angle=degrees;
1926 while (angle < -45.0)
1927 angle+=360.0;
1928 for (rotations=0; angle > 45.0; rotations++)
1929 angle-=90.0;
1930 rotations%=4;
1931 /*
1932 Calculate shear equations.
1933 */
1934 integral_image=IntegralRotateImage(image,rotations,exception);
1935 if (integral_image == (Image *) NULL)
1936 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1937 shear.x=(-tan((double) DegreesToRadians(angle)/2.0));
1938 shear.y=sin((double) DegreesToRadians(angle));
1939 if ((shear.x == 0.0) && (shear.y == 0.0))
1940 return(integral_image);
cristy574cc262011-08-05 01:23:58 +00001941 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001942 {
cristy3ed852e2009-09-05 21:47:34 +00001943 integral_image=DestroyImage(integral_image);
1944 return(integral_image);
1945 }
1946 if (integral_image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00001947 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00001948 /*
1949 Compute image size.
1950 */
1951 width=image->columns;
1952 height=image->rows;
1953 if ((rotations == 1) || (rotations == 3))
1954 {
1955 width=image->rows;
1956 height=image->columns;
1957 }
cristybb503372010-05-27 20:51:26 +00001958 y_width=width+(ssize_t) floor(fabs(shear.x)*height+0.5);
cristycee97112010-05-28 00:44:52 +00001959 x_offset=(ssize_t) ceil((double) width+((fabs(shear.y)*height)-width)/2.0-
1960 0.5);
1961 y_offset=(ssize_t) ceil((double) height+((fabs(shear.y)*y_width)-height)/2.0-
1962 0.5);
cristy3ed852e2009-09-05 21:47:34 +00001963 /*
1964 Surround image with a border.
1965 */
1966 integral_image->border_color=integral_image->background_color;
1967 integral_image->compose=CopyCompositeOp;
cristybb503372010-05-27 20:51:26 +00001968 border_info.width=(size_t) x_offset;
1969 border_info.height=(size_t) y_offset;
cristy633f0c62011-09-15 13:27:36 +00001970 rotate_image=BorderImage(integral_image,&border_info,image->compose,
1971 exception);
cristy3ed852e2009-09-05 21:47:34 +00001972 integral_image=DestroyImage(integral_image);
1973 if (rotate_image == (Image *) NULL)
1974 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1975 /*
1976 Rotate the image.
1977 */
cristyeaedf062010-05-29 22:36:02 +00001978 status=XShearImage(rotate_image,shear.x,width,height,x_offset,(ssize_t)
1979 (rotate_image->rows-height)/2,exception);
cristyecc2c142010-01-17 22:25:46 +00001980 if (status == MagickFalse)
1981 {
1982 rotate_image=DestroyImage(rotate_image);
1983 return((Image *) NULL);
1984 }
cristyeaedf062010-05-29 22:36:02 +00001985 status=YShearImage(rotate_image,shear.y,y_width,height,(ssize_t)
1986 (rotate_image->columns-y_width)/2,y_offset,exception);
cristyecc2c142010-01-17 22:25:46 +00001987 if (status == MagickFalse)
1988 {
1989 rotate_image=DestroyImage(rotate_image);
1990 return((Image *) NULL);
1991 }
cristyeaedf062010-05-29 22:36:02 +00001992 status=XShearImage(rotate_image,shear.x,y_width,rotate_image->rows,(ssize_t)
1993 (rotate_image->columns-y_width)/2,0,exception);
cristyecc2c142010-01-17 22:25:46 +00001994 if (status == MagickFalse)
1995 {
1996 rotate_image=DestroyImage(rotate_image);
1997 return((Image *) NULL);
1998 }
1999 status=CropToFitImage(&rotate_image,shear.x,shear.y,(MagickRealType) width,
cristy3ed852e2009-09-05 21:47:34 +00002000 (MagickRealType) height,MagickTrue,exception);
cristyecc2c142010-01-17 22:25:46 +00002001 if (status == MagickFalse)
2002 {
2003 rotate_image=DestroyImage(rotate_image);
2004 return((Image *) NULL);
2005 }
cristy3ed852e2009-09-05 21:47:34 +00002006 rotate_image->compose=image->compose;
2007 rotate_image->page.width=0;
2008 rotate_image->page.height=0;
2009 return(rotate_image);
2010}
2011
2012/*
2013%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2014% %
2015% %
2016% %
2017% S h e a r I m a g e %
2018% %
2019% %
2020% %
2021%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2022%
2023% ShearImage() creates a new image that is a shear_image copy of an existing
cristycee97112010-05-28 00:44:52 +00002024% one. Shearing slides one edge of an image along the X or Y axis, creating
2025% a parallelogram. An X direction shear slides an edge along the X axis,
2026% while a Y direction shear slides an edge along the Y axis. The amount of
cristy3ed852e2009-09-05 21:47:34 +00002027% the shear is controlled by a shear angle. For X direction shears, x_shear
2028% is measured relative to the Y axis, and similarly, for Y direction shears
2029% y_shear is measured relative to the X axis. Empty triangles left over from
2030% shearing the image are filled with the background color defined by member
2031% 'background_color' of the image.. ShearImage() allocates the memory
2032% necessary for the new Image structure and returns a pointer to the new image.
2033%
2034% ShearImage() is based on the paper "A Fast Algorithm for General Raster
2035% Rotatation" by Alan W. Paeth.
2036%
2037% The format of the ShearImage method is:
2038%
2039% Image *ShearImage(const Image *image,const double x_shear,
2040% const double y_shear,ExceptionInfo *exception)
2041%
2042% A description of each parameter follows.
2043%
2044% o image: the image.
2045%
2046% o x_shear, y_shear: Specifies the number of degrees to shear the image.
2047%
2048% o exception: return any errors or warnings in this structure.
2049%
2050*/
2051MagickExport Image *ShearImage(const Image *image,const double x_shear,
2052 const double y_shear,ExceptionInfo *exception)
2053{
2054 Image
2055 *integral_image,
2056 *shear_image;
2057
cristybb503372010-05-27 20:51:26 +00002058 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002059 x_offset,
2060 y_offset;
2061
cristyecc2c142010-01-17 22:25:46 +00002062 MagickBooleanType
2063 status;
2064
cristy3ed852e2009-09-05 21:47:34 +00002065 PointInfo
2066 shear;
2067
2068 RectangleInfo
2069 border_info;
2070
cristybb503372010-05-27 20:51:26 +00002071 size_t
cristy3ed852e2009-09-05 21:47:34 +00002072 y_width;
2073
2074 assert(image != (Image *) NULL);
2075 assert(image->signature == MagickSignature);
2076 if (image->debug != MagickFalse)
2077 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2078 assert(exception != (ExceptionInfo *) NULL);
2079 assert(exception->signature == MagickSignature);
2080 if ((x_shear != 0.0) && (fmod(x_shear,90.0) == 0.0))
2081 ThrowImageException(ImageError,"AngleIsDiscontinuous");
2082 if ((y_shear != 0.0) && (fmod(y_shear,90.0) == 0.0))
2083 ThrowImageException(ImageError,"AngleIsDiscontinuous");
2084 /*
2085 Initialize shear angle.
2086 */
2087 integral_image=CloneImage(image,0,0,MagickTrue,exception);
2088 if (integral_image == (Image *) NULL)
2089 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2090 shear.x=(-tan(DegreesToRadians(fmod(x_shear,360.0))));
2091 shear.y=tan(DegreesToRadians(fmod(y_shear,360.0)));
2092 if ((shear.x == 0.0) && (shear.y == 0.0))
2093 return(integral_image);
cristy574cc262011-08-05 01:23:58 +00002094 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00002095 {
cristy3ed852e2009-09-05 21:47:34 +00002096 integral_image=DestroyImage(integral_image);
2097 return(integral_image);
2098 }
2099 if (integral_image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00002100 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00002101 /*
2102 Compute image size.
2103 */
cristybb503372010-05-27 20:51:26 +00002104 y_width=image->columns+(ssize_t) floor(fabs(shear.x)*image->rows+0.5);
cristycee97112010-05-28 00:44:52 +00002105 x_offset=(ssize_t) ceil((double) image->columns+((fabs(shear.x)*image->rows)-
cristy06609ee2010-03-17 20:21:27 +00002106 image->columns)/2.0-0.5);
cristycee97112010-05-28 00:44:52 +00002107 y_offset=(ssize_t) ceil((double) image->rows+((fabs(shear.y)*y_width)-
2108 image->rows)/2.0-0.5);
cristy3ed852e2009-09-05 21:47:34 +00002109 /*
2110 Surround image with border.
2111 */
2112 integral_image->border_color=integral_image->background_color;
2113 integral_image->compose=CopyCompositeOp;
cristybb503372010-05-27 20:51:26 +00002114 border_info.width=(size_t) x_offset;
2115 border_info.height=(size_t) y_offset;
cristy633f0c62011-09-15 13:27:36 +00002116 shear_image=BorderImage(integral_image,&border_info,image->compose,exception);
cristyecc2c142010-01-17 22:25:46 +00002117 integral_image=DestroyImage(integral_image);
cristy3ed852e2009-09-05 21:47:34 +00002118 if (shear_image == (Image *) NULL)
2119 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +00002120 /*
2121 Shear the image.
2122 */
2123 if (shear_image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00002124 (void) SetImageAlphaChannel(shear_image,OpaqueAlphaChannel,exception);
cristyecc2c142010-01-17 22:25:46 +00002125 status=XShearImage(shear_image,shear.x,image->columns,image->rows,x_offset,
cristyeaedf062010-05-29 22:36:02 +00002126 (ssize_t) (shear_image->rows-image->rows)/2,exception);
cristyecc2c142010-01-17 22:25:46 +00002127 if (status == MagickFalse)
2128 {
2129 shear_image=DestroyImage(shear_image);
2130 return((Image *) NULL);
2131 }
cristyeaedf062010-05-29 22:36:02 +00002132 status=YShearImage(shear_image,shear.y,y_width,image->rows,(ssize_t)
2133 (shear_image->columns-y_width)/2,y_offset,exception);
cristyecc2c142010-01-17 22:25:46 +00002134 if (status == MagickFalse)
2135 {
2136 shear_image=DestroyImage(shear_image);
2137 return((Image *) NULL);
2138 }
2139 status=CropToFitImage(&shear_image,shear.x,shear.y,(MagickRealType)
2140 image->columns,(MagickRealType) image->rows,MagickFalse,exception);
2141 if (status == MagickFalse)
2142 {
2143 shear_image=DestroyImage(shear_image);
2144 return((Image *) NULL);
2145 }
cristy3ed852e2009-09-05 21:47:34 +00002146 shear_image->compose=image->compose;
2147 shear_image->page.width=0;
2148 shear_image->page.height=0;
2149 return(shear_image);
2150}