blob: 447cf3276f84b7ba9440aaa125a5a0d87f21ffe2 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% SSSSS H H EEEEE AAA RRRR %
7% SS H H E A A R R %
8% SSS HHHHH EEE AAAAA RRRR %
9% SS H H E A A R R %
10% SSSSS H H EEEEE A A R R %
11% %
12% %
13% MagickCore Methods to Shear or Rotate an Image by an Arbitrary Angle %
14% %
15% Software Design %
16% John Cristy %
17% July 1992 %
18% %
19% %
cristy16af1cb2009-12-11 21:38:29 +000020% Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36% The RotateImage, XShearImage, and YShearImage methods are based on the
37% paper "A Fast Algorithm for General Raster Rotatation" by Alan W. Paeth,
38% Graphics Interface '86 (Vancouver). RotateImage is adapted from a similar
39% method based on the Paeth paper written by Michael Halle of the Spatial
40% Imaging Group, MIT Media Lab.
41%
42%
43*/
44
45/*
46 Include declarations.
47*/
48#include "magick/studio.h"
49#include "magick/artifact.h"
cristy5a2ca482009-10-14 18:24:56 +000050#include "magick/attribute.h"
cristy3ed852e2009-09-05 21:47:34 +000051#include "magick/blob-private.h"
52#include "magick/cache-private.h"
53#include "magick/color-private.h"
54#include "magick/colorspace-private.h"
55#include "magick/composite.h"
56#include "magick/composite-private.h"
57#include "magick/decorate.h"
58#include "magick/distort.h"
59#include "magick/draw.h"
60#include "magick/exception.h"
61#include "magick/exception-private.h"
62#include "magick/gem.h"
63#include "magick/geometry.h"
64#include "magick/image.h"
65#include "magick/image-private.h"
66#include "magick/memory_.h"
67#include "magick/list.h"
68#include "magick/monitor.h"
69#include "magick/monitor-private.h"
70#include "magick/pixel-private.h"
71#include "magick/quantum.h"
72#include "magick/resource_.h"
73#include "magick/shear.h"
74#include "magick/statistic.h"
cristyf2f27272009-12-17 14:48:46 +000075#include "magick/string_.h"
76#include "magick/string-private.h"
cristy3ed852e2009-09-05 21:47:34 +000077#include "magick/threshold.h"
78#include "magick/transform.h"
79
80/*
81%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82% %
83% %
84% %
85% A f f i n e T r a n s f o r m I m a g e %
86% %
87% %
88% %
89%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
90%
91% AffineTransformImage() transforms an image as dictated by the affine matrix.
92% It allocates the memory necessary for the new Image structure and returns
93% a pointer to the new image.
94%
95% The format of the AffineTransformImage method is:
96%
97% Image *AffineTransformImage(const Image *image,
98% AffineMatrix *affine_matrix,ExceptionInfo *exception)
99%
100% A description of each parameter follows:
101%
102% o image: the image.
103%
104% o affine_matrix: the affine matrix.
105%
106% o exception: return any errors or warnings in this structure.
107%
108*/
109MagickExport Image *AffineTransformImage(const Image *image,
110 const AffineMatrix *affine_matrix,ExceptionInfo *exception)
111{
112 double
113 distort[6];
114
115 Image
116 *deskew_image;
117
118 /*
119 Affine transform image.
120 */
121 assert(image->signature == MagickSignature);
122 if (image->debug != MagickFalse)
123 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
124 assert(affine_matrix != (AffineMatrix *) NULL);
125 assert(exception != (ExceptionInfo *) NULL);
126 assert(exception->signature == MagickSignature);
127 distort[0]=affine_matrix->sx;
128 distort[1]=affine_matrix->rx;
129 distort[2]=affine_matrix->ry;
130 distort[3]=affine_matrix->sy;
131 distort[4]=affine_matrix->tx;
132 distort[5]=affine_matrix->ty;
133 deskew_image=DistortImage(image,AffineProjectionDistortion,6,distort,
134 MagickTrue,exception);
135 return(deskew_image);
136}
137
138/*
139%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
140% %
141% %
142% %
143+ C r o p T o F i t I m a g e %
144% %
145% %
146% %
147%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
148%
149% CropToFitImage() crops the sheared image as determined by the bounding box
150% as defined by width and height and shearing angles.
151%
152% The format of the CropToFitImage method is:
153%
cristyecc2c142010-01-17 22:25:46 +0000154% MagickBooleanType CropToFitImage(Image **image,
155% const MagickRealType x_shear,const MagickRealType x_shear,
156% const MagickRealType width,const MagickRealType height,
157% const MagickBooleanType rotate,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000158%
159% A description of each parameter follows.
160%
161% o image: the image.
162%
163% o x_shear, y_shear, width, height: Defines a region of the image to crop.
164%
165% o exception: return any errors or warnings in this structure.
166%
167*/
cristyecc2c142010-01-17 22:25:46 +0000168static MagickBooleanType CropToFitImage(Image **image,
169 const MagickRealType x_shear,const MagickRealType y_shear,
170 const MagickRealType width,const MagickRealType height,
171 const MagickBooleanType rotate,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000172{
173 Image
174 *crop_image;
175
176 PointInfo
177 extent[4],
178 min,
179 max;
180
181 RectangleInfo
182 geometry,
183 page;
184
cristybb503372010-05-27 20:51:26 +0000185 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000186 i;
187
188 /*
189 Calculate the rotated image size.
190 */
191 extent[0].x=(double) (-width/2.0);
192 extent[0].y=(double) (-height/2.0);
193 extent[1].x=(double) width/2.0;
194 extent[1].y=(double) (-height/2.0);
195 extent[2].x=(double) (-width/2.0);
196 extent[2].y=(double) height/2.0;
197 extent[3].x=(double) width/2.0;
198 extent[3].y=(double) height/2.0;
199 for (i=0; i < 4; i++)
200 {
201 extent[i].x+=x_shear*extent[i].y;
202 extent[i].y+=y_shear*extent[i].x;
203 if (rotate != MagickFalse)
204 extent[i].x+=x_shear*extent[i].y;
205 extent[i].x+=(double) (*image)->columns/2.0;
206 extent[i].y+=(double) (*image)->rows/2.0;
207 }
208 min=extent[0];
209 max=extent[0];
210 for (i=1; i < 4; i++)
211 {
212 if (min.x > extent[i].x)
213 min.x=extent[i].x;
214 if (min.y > extent[i].y)
215 min.y=extent[i].y;
216 if (max.x < extent[i].x)
217 max.x=extent[i].x;
218 if (max.y < extent[i].y)
219 max.y=extent[i].y;
220 }
cristybb503372010-05-27 20:51:26 +0000221 geometry.x=(ssize_t) ceil(min.x-0.5);
222 geometry.y=(ssize_t) ceil(min.y-0.5);
223 geometry.width=(size_t) floor(max.x-min.x+0.5);
224 geometry.height=(size_t) floor(max.y-min.y+0.5);
cristy3ed852e2009-09-05 21:47:34 +0000225 page=(*image)->page;
226 (void) ParseAbsoluteGeometry("0x0+0+0",&(*image)->page);
227 crop_image=CropImage(*image,&geometry,exception);
cristyecc2c142010-01-17 22:25:46 +0000228 if (crop_image == (Image *) NULL)
229 return(MagickFalse);
230 crop_image->page=page;
231 *image=DestroyImage(*image);
232 *image=crop_image;
233 return(MagickTrue);
cristy3ed852e2009-09-05 21:47:34 +0000234}
235
236/*
237%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
238% %
239% %
240% %
241% D e s k e w I m a g e %
242% %
243% %
244% %
245%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
246%
247% DeskewImage() removes skew from the image. Skew is an artifact that
248% occurs in scanned images because of the camera being misaligned,
249% imperfections in the scanning or surface, or simply because the paper was
250% not placed completely flat when scanned.
251%
252% The format of the DeskewImage method is:
253%
254% Image *DeskewImage(const Image *image,const double threshold,
255% ExceptionInfo *exception)
256%
257% A description of each parameter follows:
258%
259% o image: the image.
260%
261% o threshold: separate background from foreground.
262%
263% o exception: return any errors or warnings in this structure.
264%
265*/
266
267typedef struct _RadonInfo
268{
269 CacheType
270 type;
271
cristybb503372010-05-27 20:51:26 +0000272 size_t
cristy3ed852e2009-09-05 21:47:34 +0000273 width,
274 height;
275
276 MagickSizeType
277 length;
278
279 MagickBooleanType
280 mapped;
281
282 char
283 path[MaxTextExtent];
284
285 int
286 file;
287
288 unsigned short
289 *cells;
290} RadonInfo;
291
292static RadonInfo *DestroyRadonInfo(RadonInfo *radon_info)
293{
294 assert(radon_info != (RadonInfo *) NULL);
295 switch (radon_info->type)
296 {
297 case MemoryCache:
298 {
299 if (radon_info->mapped == MagickFalse)
300 radon_info->cells=(unsigned short *) RelinquishMagickMemory(
301 radon_info->cells);
302 else
303 radon_info->cells=(unsigned short *) UnmapBlob(radon_info->cells,
304 (size_t) radon_info->length);
305 RelinquishMagickResource(MemoryResource,radon_info->length);
306 break;
307 }
308 case MapCache:
309 {
310 radon_info->cells=(unsigned short *) UnmapBlob(radon_info->cells,(size_t)
311 radon_info->length);
312 RelinquishMagickResource(MapResource,radon_info->length);
313 }
314 case DiskCache:
315 {
316 if (radon_info->file != -1)
317 (void) close(radon_info->file);
318 (void) RelinquishUniqueFileResource(radon_info->path);
319 RelinquishMagickResource(DiskResource,radon_info->length);
320 break;
321 }
322 default:
323 break;
324 }
325 return((RadonInfo *) RelinquishMagickMemory(radon_info));
326}
327
328static MagickBooleanType ResetRadonCells(RadonInfo *radon_info)
329{
cristybb503372010-05-27 20:51:26 +0000330 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000331 y;
332
cristybb503372010-05-27 20:51:26 +0000333 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000334 x;
335
336 ssize_t
337 count;
338
339 unsigned short
340 value;
341
342 if (radon_info->type != DiskCache)
343 {
344 (void) ResetMagickMemory(radon_info->cells,0,(size_t) radon_info->length);
345 return(MagickTrue);
346 }
347 value=0;
348 (void) MagickSeek(radon_info->file,0,SEEK_SET);
cristybb503372010-05-27 20:51:26 +0000349 for (y=0; y < (ssize_t) radon_info->height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000350 {
cristybb503372010-05-27 20:51:26 +0000351 for (x=0; x < (ssize_t) radon_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000352 {
353 count=write(radon_info->file,&value,sizeof(*radon_info->cells));
354 if (count != (ssize_t) sizeof(*radon_info->cells))
355 break;
356 }
cristybb503372010-05-27 20:51:26 +0000357 if (x < (ssize_t) radon_info->width)
cristy3ed852e2009-09-05 21:47:34 +0000358 break;
359 }
cristybb503372010-05-27 20:51:26 +0000360 return(y < (ssize_t) radon_info->height ? MagickFalse : MagickTrue);
cristy3ed852e2009-09-05 21:47:34 +0000361}
362
cristybb503372010-05-27 20:51:26 +0000363static RadonInfo *AcquireRadonInfo(const Image *image,const size_t width,
364 const size_t height,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000365{
366 MagickBooleanType
367 status;
368
369 RadonInfo
370 *radon_info;
371
cristy90823212009-12-12 20:48:33 +0000372 radon_info=(RadonInfo *) AcquireAlignedMemory(1,sizeof(*radon_info));
cristy3ed852e2009-09-05 21:47:34 +0000373 if (radon_info == (RadonInfo *) NULL)
374 return((RadonInfo *) NULL);
375 (void) ResetMagickMemory(radon_info,0,sizeof(*radon_info));
376 radon_info->width=width;
377 radon_info->height=height;
378 radon_info->length=(MagickSizeType) width*height*sizeof(*radon_info->cells);
379 radon_info->type=MemoryCache;
380 status=AcquireMagickResource(AreaResource,radon_info->length);
381 if ((status != MagickFalse) &&
382 (radon_info->length == (MagickSizeType) ((size_t) radon_info->length)))
383 {
384 status=AcquireMagickResource(MemoryResource,radon_info->length);
385 if (status != MagickFalse)
386 {
387 radon_info->mapped=MagickFalse;
388 radon_info->cells=(unsigned short *) AcquireMagickMemory((size_t)
389 radon_info->length);
390 if (radon_info->cells == (unsigned short *) NULL)
391 {
392 radon_info->mapped=MagickTrue;
393 radon_info->cells=(unsigned short *) MapBlob(-1,IOMode,0,(size_t)
394 radon_info->length);
395 }
396 if (radon_info->cells == (unsigned short *) NULL)
397 RelinquishMagickResource(MemoryResource,radon_info->length);
398 }
399 }
400 radon_info->file=(-1);
401 if (radon_info->cells == (unsigned short *) NULL)
402 {
403 status=AcquireMagickResource(DiskResource,radon_info->length);
404 if (status == MagickFalse)
405 {
406 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
407 "CacheResourcesExhausted","`%s'",image->filename);
408 return(DestroyRadonInfo(radon_info));
409 }
410 radon_info->type=DiskCache;
411 (void) AcquireMagickResource(MemoryResource,radon_info->length);
412 radon_info->file=AcquireUniqueFileResource(radon_info->path);
413 if (radon_info->file == -1)
414 return(DestroyRadonInfo(radon_info));
415 status=AcquireMagickResource(MapResource,radon_info->length);
416 if (status != MagickFalse)
417 {
418 status=ResetRadonCells(radon_info);
419 if (status != MagickFalse)
420 {
421 radon_info->cells=(unsigned short *) MapBlob(radon_info->file,
422 IOMode,0,(size_t) radon_info->length);
423 if (radon_info->cells != (unsigned short *) NULL)
424 radon_info->type=MapCache;
425 else
426 RelinquishMagickResource(MapResource,radon_info->length);
427 }
428 }
429 }
430 return(radon_info);
431}
432
433static inline size_t MagickMin(const size_t x,const size_t y)
434{
435 if (x < y)
436 return(x);
437 return(y);
438}
439
440static inline ssize_t ReadRadonCell(const RadonInfo *radon_info,
441 const off_t offset,const size_t length,unsigned char *buffer)
442{
443 register ssize_t
444 i;
445
446 ssize_t
447 count;
448
449#if !defined(MAGICKCORE_HAVE_PPREAD)
cristyb5d5f722009-11-04 03:03:49 +0000450#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000451 #pragma omp critical (MagickCore_ReadRadonCell)
452#endif
453 {
454 i=(-1);
455 if (MagickSeek(radon_info->file,offset,SEEK_SET) >= 0)
456 {
457#endif
458 count=0;
459 for (i=0; i < (ssize_t) length; i+=count)
460 {
461#if !defined(MAGICKCORE_HAVE_PPREAD)
462 count=read(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
463 SSIZE_MAX));
464#else
465 count=pread(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
466 SSIZE_MAX),(off_t) (offset+i));
467#endif
468 if (count > 0)
469 continue;
470 count=0;
471 if (errno != EINTR)
472 {
473 i=(-1);
474 break;
475 }
476 }
477#if !defined(MAGICKCORE_HAVE_PPREAD)
478 }
479 }
480#endif
481 return(i);
482}
483
484static inline ssize_t WriteRadonCell(const RadonInfo *radon_info,
485 const off_t offset,const size_t length,const unsigned char *buffer)
486{
487 register ssize_t
488 i;
489
490 ssize_t
491 count;
492
493#if !defined(MAGICKCORE_HAVE_PWRITE)
cristyb5d5f722009-11-04 03:03:49 +0000494#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000495 #pragma omp critical (MagickCore_WriteRadonCell)
496#endif
497 {
498 if (MagickSeek(radon_info->file,offset,SEEK_SET) >= 0)
499 {
500#endif
501 count=0;
502 for (i=0; i < (ssize_t) length; i+=count)
503 {
504#if !defined(MAGICKCORE_HAVE_PWRITE)
505 count=write(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
506 SSIZE_MAX));
507#else
508 count=pwrite(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
509 SSIZE_MAX),(off_t) (offset+i));
510#endif
511 if (count > 0)
512 continue;
513 count=0;
514 if (errno != EINTR)
515 {
516 i=(-1);
517 break;
518 }
519 }
520#if !defined(MAGICKCORE_HAVE_PWRITE)
521 }
522 }
523#endif
524 return(i);
525}
526
527static inline unsigned short GetRadonCell(const RadonInfo *radon_info,
cristybb503372010-05-27 20:51:26 +0000528 const ssize_t x,const ssize_t y)
cristy3ed852e2009-09-05 21:47:34 +0000529{
530 off_t
531 i;
532
533 unsigned short
534 value;
535
536 i=(off_t) radon_info->height*x+y;
537 if ((i < 0) ||
538 ((MagickSizeType) (i*sizeof(*radon_info->cells)) >= radon_info->length))
539 return(0);
540 if (radon_info->type != DiskCache)
541 return(radon_info->cells[i]);
542 value=0;
543 (void) ReadRadonCell(radon_info,i*sizeof(*radon_info->cells),
544 sizeof(*radon_info->cells),(unsigned char *) &value);
545 return(value);
546}
547
548static inline MagickBooleanType SetRadonCell(const RadonInfo *radon_info,
cristybb503372010-05-27 20:51:26 +0000549 const ssize_t x,const ssize_t y,const unsigned short value)
cristy3ed852e2009-09-05 21:47:34 +0000550{
551 off_t
552 i;
553
554 ssize_t
555 count;
556
557 i=(off_t) radon_info->height*x+y;
558 if ((i < 0) ||
559 ((MagickSizeType) (i*sizeof(*radon_info->cells)) >= radon_info->length))
560 return(MagickFalse);
561 if (radon_info->type != DiskCache)
562 {
563 radon_info->cells[i]=value;
564 return(MagickTrue);
565 }
566 count=WriteRadonCell(radon_info,i*sizeof(*radon_info->cells),
cristy44807bb2009-09-19 18:28:06 +0000567 sizeof(*radon_info->cells),(const unsigned char *) &value);
cristy3ed852e2009-09-05 21:47:34 +0000568 if (count != (ssize_t) sizeof(*radon_info->cells))
569 return(MagickFalse);
570 return(MagickTrue);
571}
572
573static void RadonProjection(RadonInfo *source_cells,
cristybb503372010-05-27 20:51:26 +0000574 RadonInfo *destination_cells,const ssize_t sign,size_t *projection)
cristy3ed852e2009-09-05 21:47:34 +0000575{
576 RadonInfo
577 *swap;
578
cristybb503372010-05-27 20:51:26 +0000579 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000580 x;
581
582 register RadonInfo
583 *p,
584 *q;
585
cristybb503372010-05-27 20:51:26 +0000586 size_t
cristy3ed852e2009-09-05 21:47:34 +0000587 step;
588
589 p=source_cells;
590 q=destination_cells;
591 for (step=1; step < p->width; step*=2)
592 {
cristybb503372010-05-27 20:51:26 +0000593 for (x=0; x < (ssize_t) p->width; x+=2*step)
cristy3ed852e2009-09-05 21:47:34 +0000594 {
cristybb503372010-05-27 20:51:26 +0000595 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000596 y;
597
cristybb503372010-05-27 20:51:26 +0000598 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000599 i;
600
601 unsigned short
602 cell;
603
cristybb503372010-05-27 20:51:26 +0000604 for (i=0; i < (ssize_t) step; i++)
cristy3ed852e2009-09-05 21:47:34 +0000605 {
cristybb503372010-05-27 20:51:26 +0000606 for (y=0; y < (ssize_t) (p->height-i-1); y++)
cristy3ed852e2009-09-05 21:47:34 +0000607 {
608 cell=GetRadonCell(p,x+i,y);
609 (void) SetRadonCell(q,x+2*i,y,cell+GetRadonCell(p,x+i+step,y+i));
610 (void) SetRadonCell(q,x+2*i+1,y,cell+GetRadonCell(p,x+i+step,y+i+1));
611 }
cristybb503372010-05-27 20:51:26 +0000612 for ( ; y < (ssize_t) (p->height-i); y++)
cristy3ed852e2009-09-05 21:47:34 +0000613 {
614 cell=GetRadonCell(p,x+i,y);
615 (void) SetRadonCell(q,x+2*i,y,cell+GetRadonCell(p,x+i+step,y+i));
616 (void) SetRadonCell(q,x+2*i+1,y,cell);
617 }
cristybb503372010-05-27 20:51:26 +0000618 for ( ; y < (ssize_t) p->height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000619 {
620 cell=GetRadonCell(p,x+i,y);
621 (void) SetRadonCell(q,x+2*i,y,cell);
622 (void) SetRadonCell(q,x+2*i+1,y,cell);
623 }
624 }
625 }
626 swap=p;
627 p=q;
628 q=swap;
629 }
cristyb5d5f722009-11-04 03:03:49 +0000630#if defined(MAGICKCORE_OPENMP_SUPPORT)
631 #pragma omp parallel for schedule(dynamic,4)
cristy3ed852e2009-09-05 21:47:34 +0000632#endif
cristybb503372010-05-27 20:51:26 +0000633 for (x=0; x < (ssize_t) p->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000634 {
cristybb503372010-05-27 20:51:26 +0000635 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000636 y;
637
cristybb503372010-05-27 20:51:26 +0000638 size_t
cristy3ed852e2009-09-05 21:47:34 +0000639 sum;
640
641 sum=0;
cristybb503372010-05-27 20:51:26 +0000642 for (y=0; y < (ssize_t) (p->height-1); y++)
cristy3ed852e2009-09-05 21:47:34 +0000643 {
cristybb503372010-05-27 20:51:26 +0000644 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000645 delta;
646
cristybb503372010-05-27 20:51:26 +0000647 delta=GetRadonCell(p,x,y)-(ssize_t) GetRadonCell(p,x,y+1);
cristy3ed852e2009-09-05 21:47:34 +0000648 sum+=delta*delta;
649 }
650 projection[p->width+sign*x-1]=sum;
651 }
652}
653
654static MagickBooleanType RadonTransform(const Image *image,
cristybb503372010-05-27 20:51:26 +0000655 const double threshold,size_t *projection,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000656{
657 CacheView
658 *image_view;
659
cristybb503372010-05-27 20:51:26 +0000660 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000661 y;
662
663 MagickBooleanType
664 status;
665
666 RadonInfo
667 *destination_cells,
668 *source_cells;
669
cristybb503372010-05-27 20:51:26 +0000670 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000671 i;
672
673 unsigned char
674 byte;
675
cristybb503372010-05-27 20:51:26 +0000676 size_t
cristy3ed852e2009-09-05 21:47:34 +0000677 count,
678 width;
679
680 unsigned short
681 bits[256];
682
683 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
684 source_cells=AcquireRadonInfo(image,width,image->rows,exception);
685 destination_cells=AcquireRadonInfo(image,width,image->rows,exception);
686 if ((source_cells == (RadonInfo *) NULL) ||
687 (destination_cells == (RadonInfo *) NULL))
688 {
689 if (destination_cells != (RadonInfo *) NULL)
690 destination_cells=DestroyRadonInfo(destination_cells);
691 if (source_cells != (RadonInfo *) NULL)
692 source_cells=DestroyRadonInfo(source_cells);
693 return(MagickFalse);
694 }
695 if (ResetRadonCells(source_cells) == MagickFalse)
696 {
697 destination_cells=DestroyRadonInfo(destination_cells);
698 source_cells=DestroyRadonInfo(source_cells);
699 return(MagickFalse);
700 }
701 for (i=0; i < 256; i++)
702 {
703 byte=(unsigned char) i;
704 for (count=0; byte != 0; byte>>=1)
705 count+=byte & 0x01;
706 bits[i]=(unsigned short) count;
707 }
708 status=MagickTrue;
709 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +0000710#if defined(MAGICKCORE_OPENMP_SUPPORT)
711 #pragma omp parallel for schedule(dynamic,4) shared(status)
cristy3ed852e2009-09-05 21:47:34 +0000712#endif
cristybb503372010-05-27 20:51:26 +0000713 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000714 {
715 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +0000716 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000717
cristybb503372010-05-27 20:51:26 +0000718 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000719 i,
720 x;
721
cristybb503372010-05-27 20:51:26 +0000722 size_t
cristy3ed852e2009-09-05 21:47:34 +0000723 bit,
724 byte;
725
726 if (status == MagickFalse)
727 continue;
728 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
729 if (p == (const PixelPacket *) NULL)
730 {
731 status=MagickFalse;
732 continue;
733 }
734 bit=0;
735 byte=0;
cristybb503372010-05-27 20:51:26 +0000736 i=(ssize_t) (image->columns+7)/8;
737 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000738 {
739 byte<<=1;
740 if (((MagickRealType) p->red < threshold) ||
741 ((MagickRealType) p->green < threshold) ||
742 ((MagickRealType) p->blue < threshold))
743 byte|=0x01;
744 bit++;
745 if (bit == 8)
746 {
747 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
748 bit=0;
749 byte=0;
750 }
751 p++;
752 }
753 if (bit != 0)
754 {
755 byte<<=(8-bit);
756 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
757 }
758 }
759 RadonProjection(source_cells,destination_cells,-1,projection);
760 (void) ResetRadonCells(source_cells);
cristyb5d5f722009-11-04 03:03:49 +0000761#if defined(MAGICKCORE_OPENMP_SUPPORT)
762 #pragma omp parallel for schedule(dynamic,4) shared(status)
cristy3ed852e2009-09-05 21:47:34 +0000763#endif
cristybb503372010-05-27 20:51:26 +0000764 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000765 {
766 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +0000767 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000768
cristybb503372010-05-27 20:51:26 +0000769 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000770 i,
771 x;
772
cristybb503372010-05-27 20:51:26 +0000773 size_t
cristy3ed852e2009-09-05 21:47:34 +0000774 bit,
775 byte;
776
777 if (status == MagickFalse)
778 continue;
779 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
780 if (p == (const PixelPacket *) NULL)
781 {
782 status=MagickFalse;
783 continue;
784 }
785 bit=0;
786 byte=0;
787 i=0;
cristybb503372010-05-27 20:51:26 +0000788 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000789 {
790 byte<<=1;
791 if (((MagickRealType) p->red < threshold) ||
792 ((MagickRealType) p->green < threshold) ||
793 ((MagickRealType) p->blue < threshold))
794 byte|=0x01;
795 bit++;
796 if (bit == 8)
797 {
798 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
799 bit=0;
800 byte=0;
801 }
802 p++;
803 }
804 if (bit != 0)
805 {
806 byte<<=(8-bit);
807 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
808 }
809 }
810 RadonProjection(source_cells,destination_cells,1,projection);
811 image_view=DestroyCacheView(image_view);
812 destination_cells=DestroyRadonInfo(destination_cells);
813 source_cells=DestroyRadonInfo(source_cells);
814 return(MagickTrue);
815}
816
cristybb503372010-05-27 20:51:26 +0000817static void GetImageBackgroundColor(Image *image,const ssize_t offset,
cristy3ed852e2009-09-05 21:47:34 +0000818 ExceptionInfo *exception)
819{
820 CacheView
821 *image_view;
822
cristybb503372010-05-27 20:51:26 +0000823 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000824 y;
825
826 MagickPixelPacket
827 background;
828
829 MagickRealType
830 count;
831
832 /*
833 Compute average background color.
834 */
835 if (offset <= 0)
836 return;
837 GetMagickPixelPacket(image,&background);
838 count=0.0;
839 image_view=AcquireCacheView(image);
cristybb503372010-05-27 20:51:26 +0000840 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000841 {
842 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +0000843 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000844
cristybb503372010-05-27 20:51:26 +0000845 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000846 x;
847
cristybb503372010-05-27 20:51:26 +0000848 if ((y >= offset) && (y < ((ssize_t) image->rows-offset)))
cristy3ed852e2009-09-05 21:47:34 +0000849 continue;
850 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
851 if (p == (const PixelPacket *) NULL)
852 continue;
cristybb503372010-05-27 20:51:26 +0000853 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000854 {
cristybb503372010-05-27 20:51:26 +0000855 if ((x >= offset) && (x < ((ssize_t) image->columns-offset)))
cristy3ed852e2009-09-05 21:47:34 +0000856 continue;
cristyce70c172010-01-07 17:15:30 +0000857 background.red+=QuantumScale*GetRedPixelComponent(p);
858 background.green+=QuantumScale*GetGreenPixelComponent(p);
859 background.blue+=QuantumScale*GetBluePixelComponent(p);
860 background.opacity+=QuantumScale*GetOpacityPixelComponent(p);
cristy3ed852e2009-09-05 21:47:34 +0000861 count++;
862 p++;
863 }
864 }
865 image_view=DestroyCacheView(image_view);
cristyce70c172010-01-07 17:15:30 +0000866 image->background_color.red=ClampToQuantum((MagickRealType) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +0000867 background.red/count);
cristyce70c172010-01-07 17:15:30 +0000868 image->background_color.green=ClampToQuantum((MagickRealType) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +0000869 background.green/count);
cristyce70c172010-01-07 17:15:30 +0000870 image->background_color.blue=ClampToQuantum((MagickRealType) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +0000871 background.blue/count);
cristyce70c172010-01-07 17:15:30 +0000872 image->background_color.opacity=ClampToQuantum((MagickRealType) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +0000873 background.opacity/count);
874}
875
876MagickExport Image *DeskewImage(const Image *image,const double threshold,
877 ExceptionInfo *exception)
878{
879 AffineMatrix
880 affine_matrix;
881
882 const char
883 *artifact;
884
885 double
886 degrees;
887
888 Image
889 *clone_image,
890 *crop_image,
891 *deskew_image,
892 *median_image;
893
cristybb503372010-05-27 20:51:26 +0000894 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000895 skew;
896
897 MagickBooleanType
898 status;
899
900 RectangleInfo
901 geometry;
902
cristybb503372010-05-27 20:51:26 +0000903 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000904 i;
905
cristybb503372010-05-27 20:51:26 +0000906 size_t
cristy3ed852e2009-09-05 21:47:34 +0000907 max_projection,
908 *projection,
909 width;
910
911 /*
912 Compute deskew angle.
913 */
914 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
cristybb503372010-05-27 20:51:26 +0000915 projection=(size_t *) AcquireQuantumMemory((size_t) (2*width-1),
cristy3ed852e2009-09-05 21:47:34 +0000916 sizeof(*projection));
cristybb503372010-05-27 20:51:26 +0000917 if (projection == (size_t *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000918 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
919 status=RadonTransform(image,threshold,projection,exception);
920 if (status == MagickFalse)
921 {
cristybb503372010-05-27 20:51:26 +0000922 projection=(size_t *) RelinquishMagickMemory(projection);
cristy3ed852e2009-09-05 21:47:34 +0000923 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
924 }
925 max_projection=0;
926 skew=0;
cristybb503372010-05-27 20:51:26 +0000927 for (i=0; i < (ssize_t) (2*width-1); i++)
cristy3ed852e2009-09-05 21:47:34 +0000928 {
929 if (projection[i] > max_projection)
930 {
cristybb503372010-05-27 20:51:26 +0000931 skew=i-(ssize_t) width+1;
cristy3ed852e2009-09-05 21:47:34 +0000932 max_projection=projection[i];
933 }
934 }
cristybb503372010-05-27 20:51:26 +0000935 projection=(size_t *) RelinquishMagickMemory(projection);
cristy3ed852e2009-09-05 21:47:34 +0000936 /*
937 Deskew image.
938 */
939 clone_image=CloneImage(image,0,0,MagickTrue,exception);
940 if (clone_image == (Image *) NULL)
941 return((Image *) NULL);
942 (void) SetImageVirtualPixelMethod(clone_image,BackgroundVirtualPixelMethod);
943 degrees=RadiansToDegrees(-atan((double) skew/width/8));
944 if (image->debug != MagickFalse)
cristy8cd5b312010-01-07 01:10:24 +0000945 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye7f51092010-01-17 00:39:37 +0000946 " Deskew angle: %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +0000947 affine_matrix.sx=cos(DegreesToRadians(fmod((double) degrees,360.0)));
948 affine_matrix.rx=sin(DegreesToRadians(fmod((double) degrees,360.0)));
949 affine_matrix.ry=(-sin(DegreesToRadians(fmod((double) degrees,360.0))));
950 affine_matrix.sy=cos(DegreesToRadians(fmod((double) degrees,360.0)));
951 affine_matrix.tx=0.0;
952 affine_matrix.ty=0.0;
953 artifact=GetImageArtifact(image,"deskew:auto-crop");
954 if (artifact == (const char *) NULL)
955 {
956 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
957 clone_image=DestroyImage(clone_image);
958 return(deskew_image);
959 }
960 /*
961 Auto-crop image.
962 */
cristyf2f27272009-12-17 14:48:46 +0000963 GetImageBackgroundColor(clone_image,StringToLong(artifact),exception);
cristy3ed852e2009-09-05 21:47:34 +0000964 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
965 clone_image=DestroyImage(clone_image);
966 if (deskew_image == (Image *) NULL)
967 return((Image *) NULL);
968 median_image=MedianFilterImage(deskew_image,0.0,exception);
969 if (median_image == (Image *) NULL)
970 {
971 deskew_image=DestroyImage(deskew_image);
972 return((Image *) NULL);
973 }
974 geometry=GetImageBoundingBox(median_image,exception);
975 median_image=DestroyImage(median_image);
976 if (image->debug != MagickFalse)
977 (void) LogMagickEvent(TransformEvent,GetMagickModule()," Deskew geometry: "
cristyf1d91242010-05-28 02:23:19 +0000978 "%lux%lu%+ld%+ld",(unsigned long) geometry.width,(unsigned long)
979 geometry.height,(long) geometry.x,(long) geometry.y);
cristy3ed852e2009-09-05 21:47:34 +0000980 crop_image=CropImage(deskew_image,&geometry,exception);
981 deskew_image=DestroyImage(deskew_image);
982 return(crop_image);
983}
984
985/*
986%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
987% %
988% %
989% %
990+ I n t e g r a l R o t a t e I m a g e %
991% %
992% %
993% %
994%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
995%
cristy3dfa16c2010-05-17 19:34:48 +0000996% IntegralRotateImage() rotates the image an integral of 90 degrees. It
cristy3ed852e2009-09-05 21:47:34 +0000997% allocates the memory necessary for the new Image structure and returns a
998% pointer to the rotated image.
999%
1000% The format of the IntegralRotateImage method is:
1001%
cristybb503372010-05-27 20:51:26 +00001002% Image *IntegralRotateImage(const Image *image,size_t rotations,
cristy3ed852e2009-09-05 21:47:34 +00001003% ExceptionInfo *exception)
1004%
1005% A description of each parameter follows.
1006%
1007% o image: the image.
1008%
1009% o rotations: Specifies the number of 90 degree rotations.
1010%
1011*/
cristybb503372010-05-27 20:51:26 +00001012static Image *IntegralRotateImage(const Image *image,size_t rotations,
cristy3ed852e2009-09-05 21:47:34 +00001013 ExceptionInfo *exception)
1014{
cristy3ed852e2009-09-05 21:47:34 +00001015#define RotateImageTag "Rotate/Image"
1016
1017 CacheView
1018 *image_view,
1019 *rotate_view;
1020
1021 Image
1022 *rotate_image;
1023
cristy3ed852e2009-09-05 21:47:34 +00001024 MagickBooleanType
1025 status;
1026
cristy5f959472010-05-27 22:19:46 +00001027 MagickOffsetType
1028 progress;
1029
cristy3ed852e2009-09-05 21:47:34 +00001030 RectangleInfo
1031 page;
1032
cristy5f959472010-05-27 22:19:46 +00001033 ssize_t
1034 y;
1035
cristy3ed852e2009-09-05 21:47:34 +00001036 /*
1037 Initialize rotated image attributes.
1038 */
1039 assert(image != (Image *) NULL);
1040 page=image->page;
1041 rotations%=4;
cristyb32b90a2009-09-07 21:45:48 +00001042 if (rotations == 0)
1043 return(CloneImage(image,0,0,MagickTrue,exception));
cristy3ed852e2009-09-05 21:47:34 +00001044 if ((rotations == 1) || (rotations == 3))
1045 rotate_image=CloneImage(image,image->rows,image->columns,MagickTrue,
1046 exception);
1047 else
1048 rotate_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1049 exception);
1050 if (rotate_image == (Image *) NULL)
1051 return((Image *) NULL);
1052 /*
1053 Integral rotate the image.
1054 */
1055 status=MagickTrue;
1056 progress=0;
1057 image_view=AcquireCacheView(image);
1058 rotate_view=AcquireCacheView(rotate_image);
1059 switch (rotations)
1060 {
1061 case 0:
1062 {
1063 /*
1064 Rotate 0 degrees.
1065 */
cristy3ed852e2009-09-05 21:47:34 +00001066 break;
1067 }
1068 case 1:
1069 {
cristybb503372010-05-27 20:51:26 +00001070 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001071 tile_y;
1072
cristybb503372010-05-27 20:51:26 +00001073 size_t
cristyb32b90a2009-09-07 21:45:48 +00001074 tile_height,
1075 tile_width;
1076
cristy3ed852e2009-09-05 21:47:34 +00001077 /*
1078 Rotate 90 degrees.
1079 */
cristyb32b90a2009-09-07 21:45:48 +00001080 GetPixelCacheTileSize(image,&tile_width,&tile_height);
cristyd3dd0502010-04-26 00:57:20 +00001081#if defined(MAGICKCORE_OPENMP_SUPPORT) && defined(MAGICKCORE_FUTURE)
cristy8a7ea362010-03-10 20:31:43 +00001082 #pragma omp parallel for schedule(dynamic,4) shared(progress, status)
cristydaa97692009-09-13 02:10:35 +00001083#endif
cristybb503372010-05-27 20:51:26 +00001084 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=tile_height)
cristy3ed852e2009-09-05 21:47:34 +00001085 {
cristybb503372010-05-27 20:51:26 +00001086 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001087 tile_x;
1088
1089 if (status == MagickFalse)
1090 continue;
cristybb503372010-05-27 20:51:26 +00001091 for (tile_x=0; tile_x < (ssize_t) image->columns; tile_x+=tile_width)
cristy3ed852e2009-09-05 21:47:34 +00001092 {
1093 MagickBooleanType
1094 sync;
1095
1096 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001097 *restrict indexes;
cristy3ed852e2009-09-05 21:47:34 +00001098
1099 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001100 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001101
1102 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001103 *restrict rotate_indexes;
cristy3ed852e2009-09-05 21:47:34 +00001104
cristybb503372010-05-27 20:51:26 +00001105 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001106 y;
1107
1108 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001109 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001110
cristybb503372010-05-27 20:51:26 +00001111 size_t
cristyb32b90a2009-09-07 21:45:48 +00001112 height,
1113 width;
cristy3ed852e2009-09-05 21:47:34 +00001114
cristyb32b90a2009-09-07 21:45:48 +00001115 width=tile_width;
cristybb503372010-05-27 20:51:26 +00001116 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
1117 width=(size_t) (tile_width-(tile_x+tile_width-
cristy3ed852e2009-09-05 21:47:34 +00001118 image->columns));
cristyb32b90a2009-09-07 21:45:48 +00001119 height=tile_height;
cristybb503372010-05-27 20:51:26 +00001120 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
1121 height=(size_t) (tile_height-(tile_y+tile_height-
cristy3ed852e2009-09-05 21:47:34 +00001122 image->rows));
cristydaa97692009-09-13 02:10:35 +00001123 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1124 exception);
cristy3ed852e2009-09-05 21:47:34 +00001125 if (p == (const PixelPacket *) NULL)
1126 {
1127 status=MagickFalse;
1128 break;
1129 }
1130 indexes=GetCacheViewVirtualIndexQueue(image_view);
cristybb503372010-05-27 20:51:26 +00001131 for (y=0; y < (ssize_t) width; y++)
cristy3ed852e2009-09-05 21:47:34 +00001132 {
1133 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001134 *restrict tile_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001135
cristybb503372010-05-27 20:51:26 +00001136 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001137 x;
1138
cristybb503372010-05-27 20:51:26 +00001139 q=QueueCacheViewAuthenticPixels(rotate_view,(ssize_t)
cristyb32b90a2009-09-07 21:45:48 +00001140 rotate_image->columns-(tile_y+height),y+tile_x,height,
cristy3ed852e2009-09-05 21:47:34 +00001141 1,exception);
1142 if (q == (PixelPacket *) NULL)
1143 {
1144 status=MagickFalse;
1145 break;
1146 }
cristyb32b90a2009-09-07 21:45:48 +00001147 tile_pixels=p+(height-1)*width+y;
cristybb503372010-05-27 20:51:26 +00001148 for (x=0; x < (ssize_t) height; x++)
cristy3ed852e2009-09-05 21:47:34 +00001149 {
1150 *q++=(*tile_pixels);
cristyb32b90a2009-09-07 21:45:48 +00001151 tile_pixels-=width;
cristy3ed852e2009-09-05 21:47:34 +00001152 }
1153 rotate_indexes=GetCacheViewAuthenticIndexQueue(rotate_view);
1154 if ((indexes != (IndexPacket *) NULL) &&
1155 (rotate_indexes != (IndexPacket *) NULL))
1156 {
1157 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001158 *restrict tile_indexes;
cristy3ed852e2009-09-05 21:47:34 +00001159
cristyb32b90a2009-09-07 21:45:48 +00001160 tile_indexes=indexes+(height-1)*width+y;
cristybb503372010-05-27 20:51:26 +00001161 for (x=0; x < (ssize_t) height; x++)
cristy3ed852e2009-09-05 21:47:34 +00001162 {
1163 *rotate_indexes++=(*tile_indexes);
cristyb32b90a2009-09-07 21:45:48 +00001164 tile_indexes-=width;
cristy3ed852e2009-09-05 21:47:34 +00001165 }
1166 }
1167 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1168 if (sync == MagickFalse)
1169 status=MagickFalse;
1170 }
1171 }
1172 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1173 {
1174 MagickBooleanType
1175 proceed;
1176
cristyb32b90a2009-09-07 21:45:48 +00001177 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
cristy3ed852e2009-09-05 21:47:34 +00001178 image->rows);
1179 if (proceed == MagickFalse)
1180 status=MagickFalse;
1181 }
1182 }
cristyecc2c142010-01-17 22:25:46 +00001183 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1184 image->rows-1,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001185 Swap(page.width,page.height);
1186 Swap(page.x,page.y);
1187 if (page.width != 0)
cristybb503372010-05-27 20:51:26 +00001188 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
cristy3ed852e2009-09-05 21:47:34 +00001189 break;
1190 }
1191 case 2:
1192 {
1193 /*
1194 Rotate 180 degrees.
1195 */
cristyd3dd0502010-04-26 00:57:20 +00001196#if defined(MAGICKCORE_OPENMP_SUPPORT) && defined(MAGICKCORE_FUTURE)
cristy5f959472010-05-27 22:19:46 +00001197 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristydaa97692009-09-13 02:10:35 +00001198#endif
cristybb503372010-05-27 20:51:26 +00001199 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001200 {
1201 MagickBooleanType
1202 sync;
1203
1204 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001205 *restrict indexes;
cristy3ed852e2009-09-05 21:47:34 +00001206
1207 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001208 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001209
1210 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001211 *restrict rotate_indexes;
cristy3ed852e2009-09-05 21:47:34 +00001212
cristybb503372010-05-27 20:51:26 +00001213 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001214 x;
1215
1216 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001217 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001218
1219 if (status == MagickFalse)
1220 continue;
1221 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,
1222 exception);
cristybb503372010-05-27 20:51:26 +00001223 q=QueueCacheViewAuthenticPixels(rotate_view,0,(ssize_t) (image->rows-
cristy3ed852e2009-09-05 21:47:34 +00001224 y-1),image->columns,1,exception);
1225 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
1226 {
1227 status=MagickFalse;
1228 continue;
1229 }
1230 indexes=GetCacheViewVirtualIndexQueue(image_view);
1231 rotate_indexes=GetCacheViewAuthenticIndexQueue(rotate_view);
1232 q+=image->columns;
cristybb503372010-05-27 20:51:26 +00001233 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001234 *--q=(*p++);
1235 if ((indexes != (IndexPacket *) NULL) &&
1236 (rotate_indexes != (IndexPacket *) NULL))
cristybb503372010-05-27 20:51:26 +00001237 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001238 rotate_indexes[image->columns-x-1]=indexes[x];
1239 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1240 if (sync == MagickFalse)
1241 status=MagickFalse;
1242 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1243 {
1244 MagickBooleanType
1245 proceed;
1246
1247 proceed=SetImageProgress(image,RotateImageTag,progress++,
1248 image->rows);
1249 if (proceed == MagickFalse)
1250 status=MagickFalse;
1251 }
1252 }
1253 if (page.width != 0)
cristybb503372010-05-27 20:51:26 +00001254 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
cristy3ed852e2009-09-05 21:47:34 +00001255 if (page.height != 0)
cristybb503372010-05-27 20:51:26 +00001256 page.y=(ssize_t) (page.height-rotate_image->rows-page.y);
cristy3ed852e2009-09-05 21:47:34 +00001257 break;
1258 }
1259 case 3:
1260 {
cristybb503372010-05-27 20:51:26 +00001261 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001262 tile_y;
1263
cristybb503372010-05-27 20:51:26 +00001264 size_t
cristyb32b90a2009-09-07 21:45:48 +00001265 tile_height,
1266 tile_width;
1267
cristy3ed852e2009-09-05 21:47:34 +00001268 /*
1269 Rotate 270 degrees.
1270 */
cristyb32b90a2009-09-07 21:45:48 +00001271 GetPixelCacheTileSize(image,&tile_width,&tile_height);
cristyd3dd0502010-04-26 00:57:20 +00001272#if defined(MAGICKCORE_OPENMP_SUPPORT) && defined(MAGICKCORE_FUTURE)
cristy5f959472010-05-27 22:19:46 +00001273 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristydaa97692009-09-13 02:10:35 +00001274#endif
cristybb503372010-05-27 20:51:26 +00001275 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=tile_height)
cristy3ed852e2009-09-05 21:47:34 +00001276 {
cristybb503372010-05-27 20:51:26 +00001277 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001278 tile_x;
1279
1280 if (status == MagickFalse)
1281 continue;
cristybb503372010-05-27 20:51:26 +00001282 for (tile_x=0; tile_x < (ssize_t) image->columns; tile_x+=tile_width)
cristy3ed852e2009-09-05 21:47:34 +00001283 {
1284 MagickBooleanType
1285 sync;
1286
1287 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001288 *restrict indexes;
cristy3ed852e2009-09-05 21:47:34 +00001289
1290 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001291 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001292
1293 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001294 *restrict rotate_indexes;
cristy3ed852e2009-09-05 21:47:34 +00001295
cristybb503372010-05-27 20:51:26 +00001296 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001297 y;
1298
1299 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001300 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001301
cristybb503372010-05-27 20:51:26 +00001302 size_t
cristyb32b90a2009-09-07 21:45:48 +00001303 height,
1304 width;
cristy3ed852e2009-09-05 21:47:34 +00001305
cristyb32b90a2009-09-07 21:45:48 +00001306 width=tile_width;
cristybb503372010-05-27 20:51:26 +00001307 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
1308 width=(size_t) (tile_width-(tile_x+tile_width-
cristy3ed852e2009-09-05 21:47:34 +00001309 image->columns));
cristyb32b90a2009-09-07 21:45:48 +00001310 height=tile_height;
cristybb503372010-05-27 20:51:26 +00001311 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
1312 height=(size_t) (tile_height-(tile_y+tile_height-
cristy3ed852e2009-09-05 21:47:34 +00001313 image->rows));
cristyb32b90a2009-09-07 21:45:48 +00001314 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,
1315 height,exception);
cristy3ed852e2009-09-05 21:47:34 +00001316 if (p == (const PixelPacket *) NULL)
1317 {
1318 status=MagickFalse;
1319 break;
1320 }
1321 indexes=GetCacheViewVirtualIndexQueue(image_view);
cristybb503372010-05-27 20:51:26 +00001322 for (y=0; y < (ssize_t) width; y++)
cristy3ed852e2009-09-05 21:47:34 +00001323 {
1324 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001325 *restrict tile_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001326
cristybb503372010-05-27 20:51:26 +00001327 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001328 x;
1329
cristybb503372010-05-27 20:51:26 +00001330 q=QueueCacheViewAuthenticPixels(rotate_view,tile_y,(ssize_t)
cristyb32b90a2009-09-07 21:45:48 +00001331 y+rotate_image->rows-(tile_x+width),height,1,exception);
cristy3ed852e2009-09-05 21:47:34 +00001332 if (q == (PixelPacket *) NULL)
1333 {
1334 status=MagickFalse;
1335 break;
1336 }
cristyb32b90a2009-09-07 21:45:48 +00001337 tile_pixels=p+(width-1)-y;
cristybb503372010-05-27 20:51:26 +00001338 for (x=0; x < (ssize_t) height; x++)
cristy3ed852e2009-09-05 21:47:34 +00001339 {
1340 *q++=(*tile_pixels);
cristyb32b90a2009-09-07 21:45:48 +00001341 tile_pixels+=width;
cristy3ed852e2009-09-05 21:47:34 +00001342 }
1343 rotate_indexes=GetCacheViewAuthenticIndexQueue(rotate_view);
1344 if ((indexes != (IndexPacket *) NULL) &&
1345 (rotate_indexes != (IndexPacket *) NULL))
1346 {
1347 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001348 *restrict tile_indexes;
cristy3ed852e2009-09-05 21:47:34 +00001349
cristyb32b90a2009-09-07 21:45:48 +00001350 tile_indexes=indexes+(width-1)-y;
cristybb503372010-05-27 20:51:26 +00001351 for (x=0; x < (ssize_t) height; x++)
cristy3ed852e2009-09-05 21:47:34 +00001352 {
1353 *rotate_indexes++=(*tile_indexes);
cristyb32b90a2009-09-07 21:45:48 +00001354 tile_indexes+=width;
cristy3ed852e2009-09-05 21:47:34 +00001355 }
1356 }
1357 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1358 if (sync == MagickFalse)
1359 status=MagickFalse;
1360 }
1361 }
1362 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1363 {
1364 MagickBooleanType
1365 proceed;
1366
cristyb32b90a2009-09-07 21:45:48 +00001367 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
cristy3ed852e2009-09-05 21:47:34 +00001368 image->rows);
1369 if (proceed == MagickFalse)
1370 status=MagickFalse;
1371 }
1372 }
cristyecc2c142010-01-17 22:25:46 +00001373 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1374 image->rows-1,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001375 Swap(page.width,page.height);
1376 Swap(page.x,page.y);
1377 if (page.height != 0)
cristybb503372010-05-27 20:51:26 +00001378 page.y=(ssize_t) (page.height-rotate_image->rows-page.y);
cristy3ed852e2009-09-05 21:47:34 +00001379 break;
1380 }
1381 }
1382 rotate_view=DestroyCacheView(rotate_view);
1383 image_view=DestroyCacheView(image_view);
1384 rotate_image->type=image->type;
1385 rotate_image->page=page;
1386 if (status == MagickFalse)
1387 rotate_image=DestroyImage(rotate_image);
1388 return(rotate_image);
1389}
1390
1391/*
1392%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1393% %
1394% %
1395% %
1396+ X S h e a r I m a g e %
1397% %
1398% %
1399% %
1400%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1401%
1402% XShearImage() shears the image in the X direction with a shear angle of
1403% 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1404% negative angles shear clockwise. Angles are measured relative to a vertical
1405% Y-axis. X shears will widen an image creating 'empty' triangles on the left
1406% and right sides of the source image.
1407%
1408% The format of the XShearImage method is:
1409%
1410% MagickBooleanType XShearImage(Image *image,const MagickRealType degrees,
cristybb503372010-05-27 20:51:26 +00001411% const size_t width,const size_t height,
1412% const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001413%
1414% A description of each parameter follows.
1415%
1416% o image: the image.
1417%
cristycee97112010-05-28 00:44:52 +00001418% o degrees: A MagickRealType representing the shearing angle along the X
cristy3ed852e2009-09-05 21:47:34 +00001419% axis.
1420%
1421% o width, height, x_offset, y_offset: Defines a region of the image
1422% to shear.
1423%
cristyecc2c142010-01-17 22:25:46 +00001424% o exception: return any errors or warnings in this structure.
1425%
cristy3ed852e2009-09-05 21:47:34 +00001426*/
1427static MagickBooleanType XShearImage(Image *image,const MagickRealType degrees,
cristybb503372010-05-27 20:51:26 +00001428 const size_t width,const size_t height,const ssize_t x_offset,
1429 const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001430{
1431#define XShearImageTag "XShear/Image"
1432
1433 typedef enum
1434 {
1435 LEFT,
1436 RIGHT
1437 } ShearDirection;
1438
1439 CacheView
1440 *image_view;
1441
cristy3ed852e2009-09-05 21:47:34 +00001442 MagickBooleanType
1443 status;
1444
cristy5f959472010-05-27 22:19:46 +00001445 MagickOffsetType
1446 progress;
1447
cristy3ed852e2009-09-05 21:47:34 +00001448 MagickPixelPacket
1449 background;
1450
cristy5f959472010-05-27 22:19:46 +00001451 ssize_t
1452 y;
1453
cristy3ed852e2009-09-05 21:47:34 +00001454 assert(image != (Image *) NULL);
1455 assert(image->signature == MagickSignature);
1456 if (image->debug != MagickFalse)
1457 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1458 GetMagickPixelPacket(image,&background);
1459 SetMagickPixelPacket(image,&image->background_color,(IndexPacket *) NULL,
1460 &background);
1461 if (image->colorspace == CMYKColorspace)
1462 ConvertRGBToCMYK(&background);
1463 /*
1464 XShear image.
1465 */
1466 status=MagickTrue;
1467 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00001468 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00001469#if defined(MAGICKCORE_OPENMP_SUPPORT)
1470 #pragma omp parallel for schedule(dynamic,4) shared(progress, status)
cristy3ed852e2009-09-05 21:47:34 +00001471#endif
cristybb503372010-05-27 20:51:26 +00001472 for (y=0; y < (ssize_t) height; y++)
cristy3ed852e2009-09-05 21:47:34 +00001473 {
cristybb503372010-05-27 20:51:26 +00001474 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001475 step;
1476
1477 MagickPixelPacket
1478 pixel,
1479 source,
1480 destination;
1481
1482 MagickRealType
1483 area,
1484 displacement;
1485
cristybb503372010-05-27 20:51:26 +00001486 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001487 i;
1488
1489 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001490 *restrict indexes,
1491 *restrict shear_indexes;
cristy3ed852e2009-09-05 21:47:34 +00001492
1493 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001494 *restrict p,
1495 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001496
1497 ShearDirection
1498 direction;
1499
1500 if (status == MagickFalse)
1501 continue;
1502 p=GetCacheViewAuthenticPixels(image_view,0,y_offset+y,image->columns,1,
1503 exception);
1504 if (p == (PixelPacket *) NULL)
1505 {
1506 status=MagickFalse;
1507 continue;
1508 }
1509 indexes=GetCacheViewAuthenticIndexQueue(image_view);
1510 p+=x_offset;
1511 indexes+=x_offset;
1512 displacement=degrees*(MagickRealType) (y-height/2.0);
1513 if (displacement == 0.0)
1514 continue;
1515 if (displacement > 0.0)
1516 direction=RIGHT;
1517 else
1518 {
1519 displacement*=(-1.0);
1520 direction=LEFT;
1521 }
cristybb503372010-05-27 20:51:26 +00001522 step=(ssize_t) floor((double) displacement);
cristy3ed852e2009-09-05 21:47:34 +00001523 area=(MagickRealType) (displacement-step);
1524 step++;
1525 pixel=background;
1526 GetMagickPixelPacket(image,&source);
1527 GetMagickPixelPacket(image,&destination);
1528 switch (direction)
1529 {
1530 case LEFT:
1531 {
1532 /*
1533 Transfer pixels left-to-right.
1534 */
1535 if (step > x_offset)
1536 break;
1537 q=p-step;
1538 shear_indexes=indexes-step;
cristybb503372010-05-27 20:51:26 +00001539 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001540 {
1541 if ((x_offset+i) < step)
1542 {
1543 SetMagickPixelPacket(image,++p,++indexes,&pixel);
1544 q++;
1545 shear_indexes++;
1546 continue;
1547 }
1548 SetMagickPixelPacket(image,p,indexes,&source);
1549 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1550 &source,(MagickRealType) p->opacity,area,&destination);
1551 SetPixelPacket(image,&destination,q++,shear_indexes++);
1552 SetMagickPixelPacket(image,p++,indexes++,&pixel);
1553 }
1554 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1555 &background,(MagickRealType) background.opacity,area,&destination);
1556 SetPixelPacket(image,&destination,q++,shear_indexes++);
1557 for (i=0; i < (step-1); i++)
1558 SetPixelPacket(image,&background,q++,shear_indexes++);
1559 break;
1560 }
1561 case RIGHT:
1562 {
1563 /*
1564 Transfer pixels right-to-left.
1565 */
1566 p+=width;
1567 indexes+=width;
1568 q=p+step;
1569 shear_indexes=indexes+step;
cristybb503372010-05-27 20:51:26 +00001570 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001571 {
1572 p--;
1573 indexes--;
1574 q--;
1575 shear_indexes--;
cristybb503372010-05-27 20:51:26 +00001576 if ((size_t) (x_offset+width+step-i) >= image->columns)
cristy3ed852e2009-09-05 21:47:34 +00001577 continue;
1578 SetMagickPixelPacket(image,p,indexes,&source);
1579 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1580 &source,(MagickRealType) p->opacity,area,&destination);
1581 SetPixelPacket(image,&destination,q,shear_indexes);
1582 SetMagickPixelPacket(image,p,indexes,&pixel);
1583 }
1584 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1585 &background,(MagickRealType) background.opacity,area,&destination);
1586 SetPixelPacket(image,&destination,--q,--shear_indexes);
1587 for (i=0; i < (step-1); i++)
1588 SetPixelPacket(image,&background,--q,--shear_indexes);
1589 break;
1590 }
1591 }
1592 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1593 status=MagickFalse;
1594 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1595 {
1596 MagickBooleanType
1597 proceed;
1598
cristyb5d5f722009-11-04 03:03:49 +00001599#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00001600 #pragma omp critical (MagickCore_XShearImage)
1601#endif
1602 proceed=SetImageProgress(image,XShearImageTag,progress++,height);
1603 if (proceed == MagickFalse)
1604 status=MagickFalse;
1605 }
1606 }
1607 image_view=DestroyCacheView(image_view);
1608 return(status);
1609}
1610
1611/*
1612%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1613% %
1614% %
1615% %
1616+ Y S h e a r I m a g e %
1617% %
1618% %
1619% %
1620%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1621%
1622% YShearImage shears the image in the Y direction with a shear angle of
1623% 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1624% negative angles shear clockwise. Angles are measured relative to a
1625% horizontal X-axis. Y shears will increase the height of an image creating
1626% 'empty' triangles on the top and bottom of the source image.
1627%
1628% The format of the YShearImage method is:
1629%
1630% MagickBooleanType YShearImage(Image *image,const MagickRealType degrees,
cristybb503372010-05-27 20:51:26 +00001631% const size_t width,const size_t height,
1632% const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001633%
1634% A description of each parameter follows.
1635%
1636% o image: the image.
1637%
cristycee97112010-05-28 00:44:52 +00001638% o degrees: A MagickRealType representing the shearing angle along the Y
cristy3ed852e2009-09-05 21:47:34 +00001639% axis.
1640%
1641% o width, height, x_offset, y_offset: Defines a region of the image
1642% to shear.
1643%
cristyecc2c142010-01-17 22:25:46 +00001644% o exception: return any errors or warnings in this structure.
1645%
cristy3ed852e2009-09-05 21:47:34 +00001646*/
1647static MagickBooleanType YShearImage(Image *image,const MagickRealType degrees,
cristybb503372010-05-27 20:51:26 +00001648 const size_t width,const size_t height,const ssize_t x_offset,
1649 const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001650{
1651#define YShearImageTag "YShear/Image"
1652
1653 typedef enum
1654 {
1655 UP,
1656 DOWN
1657 } ShearDirection;
1658
1659 CacheView
1660 *image_view;
1661
cristy3ed852e2009-09-05 21:47:34 +00001662 MagickBooleanType
1663 status;
1664
cristy5f959472010-05-27 22:19:46 +00001665 MagickOffsetType
1666 progress;
1667
cristy3ed852e2009-09-05 21:47:34 +00001668 MagickPixelPacket
1669 background;
1670
cristy5f959472010-05-27 22:19:46 +00001671 ssize_t
1672 x;
1673
cristy3ed852e2009-09-05 21:47:34 +00001674 assert(image != (Image *) NULL);
1675 assert(image->signature == MagickSignature);
1676 if (image->debug != MagickFalse)
1677 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1678 GetMagickPixelPacket(image,&background);
1679 SetMagickPixelPacket(image,&image->background_color,(IndexPacket *) NULL,
1680 &background);
1681 if (image->colorspace == CMYKColorspace)
1682 ConvertRGBToCMYK(&background);
1683 /*
1684 Y Shear image.
1685 */
1686 status=MagickTrue;
1687 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00001688 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00001689#if defined(MAGICKCORE_OPENMP_SUPPORT)
1690 #pragma omp parallel for schedule(dynamic,4) shared(progress, status)
cristy3ed852e2009-09-05 21:47:34 +00001691#endif
cristybb503372010-05-27 20:51:26 +00001692 for (x=0; x < (ssize_t) width; x++)
cristy3ed852e2009-09-05 21:47:34 +00001693 {
cristybb503372010-05-27 20:51:26 +00001694 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001695 step;
1696
1697 MagickPixelPacket
1698 pixel,
1699 source,
1700 destination;
1701
1702 MagickRealType
1703 area,
1704 displacement;
1705
1706 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001707 *restrict indexes,
1708 *restrict shear_indexes;
cristy3ed852e2009-09-05 21:47:34 +00001709
cristybb503372010-05-27 20:51:26 +00001710 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001711 i;
1712
1713 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001714 *restrict p,
1715 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001716
1717 ShearDirection
1718 direction;
1719
1720 if (status == MagickFalse)
1721 continue;
1722 p=GetCacheViewAuthenticPixels(image_view,x_offset+x,0,1,image->rows,
1723 exception);
1724 if (p == (PixelPacket *) NULL)
1725 {
1726 status=MagickFalse;
1727 continue;
1728 }
1729 indexes=GetCacheViewAuthenticIndexQueue(image_view);
1730 p+=y_offset;
1731 indexes+=y_offset;
1732 displacement=degrees*(MagickRealType) (x-width/2.0);
1733 if (displacement == 0.0)
1734 continue;
1735 if (displacement > 0.0)
1736 direction=DOWN;
1737 else
1738 {
1739 displacement*=(-1.0);
1740 direction=UP;
1741 }
cristybb503372010-05-27 20:51:26 +00001742 step=(ssize_t) floor((double) displacement);
cristy3ed852e2009-09-05 21:47:34 +00001743 area=(MagickRealType) (displacement-step);
1744 step++;
1745 pixel=background;
1746 GetMagickPixelPacket(image,&source);
1747 GetMagickPixelPacket(image,&destination);
1748 switch (direction)
1749 {
1750 case UP:
1751 {
1752 /*
1753 Transfer pixels top-to-bottom.
1754 */
1755 if (step > y_offset)
1756 break;
1757 q=p-step;
1758 shear_indexes=indexes-step;
cristybb503372010-05-27 20:51:26 +00001759 for (i=0; i < (ssize_t) height; i++)
cristy3ed852e2009-09-05 21:47:34 +00001760 {
1761 if ((y_offset+i) < step)
1762 {
1763 SetMagickPixelPacket(image,++p,++indexes,&pixel);
1764 q++;
1765 shear_indexes++;
1766 continue;
1767 }
1768 SetMagickPixelPacket(image,p,indexes,&source);
1769 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1770 &source,(MagickRealType) p->opacity,area,&destination);
1771 SetPixelPacket(image,&destination,q++,shear_indexes++);
1772 SetMagickPixelPacket(image,p++,indexes++,&pixel);
1773 }
1774 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1775 &background,(MagickRealType) background.opacity,area,&destination);
1776 SetPixelPacket(image,&destination,q++,shear_indexes++);
1777 for (i=0; i < (step-1); i++)
1778 SetPixelPacket(image,&background,q++,shear_indexes++);
1779 break;
1780 }
1781 case DOWN:
1782 {
1783 /*
1784 Transfer pixels bottom-to-top.
1785 */
1786 p+=height;
1787 indexes+=height;
1788 q=p+step;
1789 shear_indexes=indexes+step;
cristybb503372010-05-27 20:51:26 +00001790 for (i=0; i < (ssize_t) height; i++)
cristy3ed852e2009-09-05 21:47:34 +00001791 {
1792 p--;
1793 indexes--;
1794 q--;
1795 shear_indexes--;
cristybb503372010-05-27 20:51:26 +00001796 if ((size_t) (y_offset+height+step-i) >= image->rows)
cristy3ed852e2009-09-05 21:47:34 +00001797 continue;
1798 SetMagickPixelPacket(image,p,indexes,&source);
1799 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1800 &source,(MagickRealType) p->opacity,area,&destination);
1801 SetPixelPacket(image,&destination,q,shear_indexes);
1802 SetMagickPixelPacket(image,p,indexes,&pixel);
1803 }
1804 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1805 &background,(MagickRealType) background.opacity,area,&destination);
1806 SetPixelPacket(image,&destination,--q,--shear_indexes);
1807 for (i=0; i < (step-1); i++)
1808 SetPixelPacket(image,&background,--q,--shear_indexes);
1809 break;
1810 }
1811 }
1812 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1813 status=MagickFalse;
1814 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1815 {
1816 MagickBooleanType
1817 proceed;
1818
cristyb5d5f722009-11-04 03:03:49 +00001819#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00001820 #pragma omp critical (MagickCore_YShearImage)
1821#endif
1822 proceed=SetImageProgress(image,YShearImageTag,progress++,image->rows);
1823 if (proceed == MagickFalse)
1824 status=MagickFalse;
1825 }
1826 }
1827 image_view=DestroyCacheView(image_view);
1828 return(status);
1829}
1830
1831/*
1832%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1833% %
1834% %
1835% %
1836% R o t a t e I m a g e %
1837% %
1838% %
1839% %
1840%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1841%
1842% RotateImage() creates a new image that is a rotated copy of an existing
1843% one. Positive angles rotate counter-clockwise (right-hand rule), while
1844% negative angles rotate clockwise. Rotated images are usually larger than
1845% the originals and have 'empty' triangular corners. X axis. Empty
1846% triangles left over from shearing the image are filled with the background
1847% color defined by member 'background_color' of the image. RotateImage
1848% allocates the memory necessary for the new Image structure and returns a
1849% pointer to the new image.
1850%
1851% RotateImage() is based on the paper "A Fast Algorithm for General
1852% Raster Rotatation" by Alan W. Paeth. RotateImage is adapted from a similar
1853% method based on the Paeth paper written by Michael Halle of the Spatial
1854% Imaging Group, MIT Media Lab.
1855%
1856% The format of the RotateImage method is:
1857%
1858% Image *RotateImage(const Image *image,const double degrees,
1859% ExceptionInfo *exception)
1860%
1861% A description of each parameter follows.
1862%
1863% o image: the image.
1864%
1865% o degrees: Specifies the number of degrees to rotate the image.
1866%
1867% o exception: return any errors or warnings in this structure.
1868%
1869*/
1870MagickExport Image *RotateImage(const Image *image,const double degrees,
1871 ExceptionInfo *exception)
1872{
1873 Image
1874 *integral_image,
1875 *rotate_image;
1876
cristybb503372010-05-27 20:51:26 +00001877 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001878 x_offset,
1879 y_offset;
1880
cristyecc2c142010-01-17 22:25:46 +00001881 MagickBooleanType
1882 status;
1883
cristy3ed852e2009-09-05 21:47:34 +00001884 MagickRealType
1885 angle;
1886
1887 PointInfo
1888 shear;
1889
1890 RectangleInfo
1891 border_info;
1892
cristybb503372010-05-27 20:51:26 +00001893 size_t
cristy3ed852e2009-09-05 21:47:34 +00001894 height,
1895 rotations,
1896 width,
1897 y_width;
1898
1899 /*
1900 Adjust rotation angle.
1901 */
1902 assert(image != (Image *) NULL);
1903 assert(image->signature == MagickSignature);
1904 if (image->debug != MagickFalse)
1905 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1906 assert(exception != (ExceptionInfo *) NULL);
1907 assert(exception->signature == MagickSignature);
1908 angle=degrees;
1909 while (angle < -45.0)
1910 angle+=360.0;
1911 for (rotations=0; angle > 45.0; rotations++)
1912 angle-=90.0;
1913 rotations%=4;
1914 /*
1915 Calculate shear equations.
1916 */
1917 integral_image=IntegralRotateImage(image,rotations,exception);
1918 if (integral_image == (Image *) NULL)
1919 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1920 shear.x=(-tan((double) DegreesToRadians(angle)/2.0));
1921 shear.y=sin((double) DegreesToRadians(angle));
1922 if ((shear.x == 0.0) && (shear.y == 0.0))
1923 return(integral_image);
1924 if (SetImageStorageClass(integral_image,DirectClass) == MagickFalse)
1925 {
1926 InheritException(exception,&integral_image->exception);
1927 integral_image=DestroyImage(integral_image);
1928 return(integral_image);
1929 }
1930 if (integral_image->matte == MagickFalse)
1931 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel);
1932 /*
1933 Compute image size.
1934 */
1935 width=image->columns;
1936 height=image->rows;
1937 if ((rotations == 1) || (rotations == 3))
1938 {
1939 width=image->rows;
1940 height=image->columns;
1941 }
cristybb503372010-05-27 20:51:26 +00001942 y_width=width+(ssize_t) floor(fabs(shear.x)*height+0.5);
cristycee97112010-05-28 00:44:52 +00001943 x_offset=(ssize_t) ceil((double) width+((fabs(shear.y)*height)-width)/2.0-
1944 0.5);
1945 y_offset=(ssize_t) ceil((double) height+((fabs(shear.y)*y_width)-height)/2.0-
1946 0.5);
cristy3ed852e2009-09-05 21:47:34 +00001947 /*
1948 Surround image with a border.
1949 */
1950 integral_image->border_color=integral_image->background_color;
1951 integral_image->compose=CopyCompositeOp;
cristybb503372010-05-27 20:51:26 +00001952 border_info.width=(size_t) x_offset;
1953 border_info.height=(size_t) y_offset;
cristy3ed852e2009-09-05 21:47:34 +00001954 rotate_image=BorderImage(integral_image,&border_info,exception);
1955 integral_image=DestroyImage(integral_image);
1956 if (rotate_image == (Image *) NULL)
1957 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1958 /*
1959 Rotate the image.
1960 */
cristybb503372010-05-27 20:51:26 +00001961 status=XShearImage(rotate_image,shear.x,width,height,x_offset,((ssize_t)
cristyecc2c142010-01-17 22:25:46 +00001962 rotate_image->rows-height)/2,exception);
1963 if (status == MagickFalse)
1964 {
1965 rotate_image=DestroyImage(rotate_image);
1966 return((Image *) NULL);
1967 }
cristybb503372010-05-27 20:51:26 +00001968 status=YShearImage(rotate_image,shear.y,y_width,height,((ssize_t)
cristyecc2c142010-01-17 22:25:46 +00001969 rotate_image->columns-y_width)/2,y_offset,exception);
1970 if (status == MagickFalse)
1971 {
1972 rotate_image=DestroyImage(rotate_image);
1973 return((Image *) NULL);
1974 }
cristybb503372010-05-27 20:51:26 +00001975 status=XShearImage(rotate_image,shear.x,y_width,rotate_image->rows,((ssize_t)
cristyecc2c142010-01-17 22:25:46 +00001976 rotate_image->columns-y_width)/2,0,exception);
1977 if (status == MagickFalse)
1978 {
1979 rotate_image=DestroyImage(rotate_image);
1980 return((Image *) NULL);
1981 }
1982 status=CropToFitImage(&rotate_image,shear.x,shear.y,(MagickRealType) width,
cristy3ed852e2009-09-05 21:47:34 +00001983 (MagickRealType) height,MagickTrue,exception);
cristyecc2c142010-01-17 22:25:46 +00001984 if (status == MagickFalse)
1985 {
1986 rotate_image=DestroyImage(rotate_image);
1987 return((Image *) NULL);
1988 }
cristy3ed852e2009-09-05 21:47:34 +00001989 rotate_image->compose=image->compose;
1990 rotate_image->page.width=0;
1991 rotate_image->page.height=0;
1992 return(rotate_image);
1993}
1994
1995/*
1996%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1997% %
1998% %
1999% %
2000% S h e a r I m a g e %
2001% %
2002% %
2003% %
2004%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2005%
2006% ShearImage() creates a new image that is a shear_image copy of an existing
cristycee97112010-05-28 00:44:52 +00002007% one. Shearing slides one edge of an image along the X or Y axis, creating
2008% a parallelogram. An X direction shear slides an edge along the X axis,
2009% while a Y direction shear slides an edge along the Y axis. The amount of
cristy3ed852e2009-09-05 21:47:34 +00002010% the shear is controlled by a shear angle. For X direction shears, x_shear
2011% is measured relative to the Y axis, and similarly, for Y direction shears
2012% y_shear is measured relative to the X axis. Empty triangles left over from
2013% shearing the image are filled with the background color defined by member
2014% 'background_color' of the image.. ShearImage() allocates the memory
2015% necessary for the new Image structure and returns a pointer to the new image.
2016%
2017% ShearImage() is based on the paper "A Fast Algorithm for General Raster
2018% Rotatation" by Alan W. Paeth.
2019%
2020% The format of the ShearImage method is:
2021%
2022% Image *ShearImage(const Image *image,const double x_shear,
2023% const double y_shear,ExceptionInfo *exception)
2024%
2025% A description of each parameter follows.
2026%
2027% o image: the image.
2028%
2029% o x_shear, y_shear: Specifies the number of degrees to shear the image.
2030%
2031% o exception: return any errors or warnings in this structure.
2032%
2033*/
2034MagickExport Image *ShearImage(const Image *image,const double x_shear,
2035 const double y_shear,ExceptionInfo *exception)
2036{
2037 Image
2038 *integral_image,
2039 *shear_image;
2040
cristybb503372010-05-27 20:51:26 +00002041 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002042 x_offset,
2043 y_offset;
2044
cristyecc2c142010-01-17 22:25:46 +00002045 MagickBooleanType
2046 status;
2047
cristy3ed852e2009-09-05 21:47:34 +00002048 PointInfo
2049 shear;
2050
2051 RectangleInfo
2052 border_info;
2053
cristybb503372010-05-27 20:51:26 +00002054 size_t
cristy3ed852e2009-09-05 21:47:34 +00002055 y_width;
2056
2057 assert(image != (Image *) NULL);
2058 assert(image->signature == MagickSignature);
2059 if (image->debug != MagickFalse)
2060 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2061 assert(exception != (ExceptionInfo *) NULL);
2062 assert(exception->signature == MagickSignature);
2063 if ((x_shear != 0.0) && (fmod(x_shear,90.0) == 0.0))
2064 ThrowImageException(ImageError,"AngleIsDiscontinuous");
2065 if ((y_shear != 0.0) && (fmod(y_shear,90.0) == 0.0))
2066 ThrowImageException(ImageError,"AngleIsDiscontinuous");
2067 /*
2068 Initialize shear angle.
2069 */
2070 integral_image=CloneImage(image,0,0,MagickTrue,exception);
2071 if (integral_image == (Image *) NULL)
2072 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2073 shear.x=(-tan(DegreesToRadians(fmod(x_shear,360.0))));
2074 shear.y=tan(DegreesToRadians(fmod(y_shear,360.0)));
2075 if ((shear.x == 0.0) && (shear.y == 0.0))
2076 return(integral_image);
2077 if (SetImageStorageClass(integral_image,DirectClass) == MagickFalse)
2078 {
2079 InheritException(exception,&integral_image->exception);
2080 integral_image=DestroyImage(integral_image);
2081 return(integral_image);
2082 }
2083 if (integral_image->matte == MagickFalse)
2084 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel);
2085 /*
2086 Compute image size.
2087 */
cristybb503372010-05-27 20:51:26 +00002088 y_width=image->columns+(ssize_t) floor(fabs(shear.x)*image->rows+0.5);
cristycee97112010-05-28 00:44:52 +00002089 x_offset=(ssize_t) ceil((double) image->columns+((fabs(shear.x)*image->rows)-
cristy06609ee2010-03-17 20:21:27 +00002090 image->columns)/2.0-0.5);
cristycee97112010-05-28 00:44:52 +00002091 y_offset=(ssize_t) ceil((double) image->rows+((fabs(shear.y)*y_width)-
2092 image->rows)/2.0-0.5);
cristy3ed852e2009-09-05 21:47:34 +00002093 /*
2094 Surround image with border.
2095 */
2096 integral_image->border_color=integral_image->background_color;
2097 integral_image->compose=CopyCompositeOp;
cristybb503372010-05-27 20:51:26 +00002098 border_info.width=(size_t) x_offset;
2099 border_info.height=(size_t) y_offset;
cristy3ed852e2009-09-05 21:47:34 +00002100 shear_image=BorderImage(integral_image,&border_info,exception);
cristyecc2c142010-01-17 22:25:46 +00002101 integral_image=DestroyImage(integral_image);
cristy3ed852e2009-09-05 21:47:34 +00002102 if (shear_image == (Image *) NULL)
2103 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +00002104 /*
2105 Shear the image.
2106 */
2107 if (shear_image->matte == MagickFalse)
2108 (void) SetImageAlphaChannel(shear_image,OpaqueAlphaChannel);
cristyecc2c142010-01-17 22:25:46 +00002109 status=XShearImage(shear_image,shear.x,image->columns,image->rows,x_offset,
cristybb503372010-05-27 20:51:26 +00002110 ((ssize_t) shear_image->rows-image->rows)/2,exception);
cristyecc2c142010-01-17 22:25:46 +00002111 if (status == MagickFalse)
2112 {
2113 shear_image=DestroyImage(shear_image);
2114 return((Image *) NULL);
2115 }
cristybb503372010-05-27 20:51:26 +00002116 status=YShearImage(shear_image,shear.y,y_width,image->rows,((ssize_t)
cristyecc2c142010-01-17 22:25:46 +00002117 shear_image->columns-y_width)/2,y_offset,exception);
2118 if (status == MagickFalse)
2119 {
2120 shear_image=DestroyImage(shear_image);
2121 return((Image *) NULL);
2122 }
2123 status=CropToFitImage(&shear_image,shear.x,shear.y,(MagickRealType)
2124 image->columns,(MagickRealType) image->rows,MagickFalse,exception);
2125 if (status == MagickFalse)
2126 {
2127 shear_image=DestroyImage(shear_image);
2128 return((Image *) NULL);
2129 }
cristy3ed852e2009-09-05 21:47:34 +00002130 shear_image->compose=image->compose;
2131 shear_image->page.width=0;
2132 shear_image->page.height=0;
2133 return(shear_image);
2134}