blob: 06fdc4ddc68b86eb39a51f6f9b5116cc04c094b3 [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 {
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 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);
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
cristybb503372010-05-27 20:51:26 +0000663 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000664 y;
665
666 MagickBooleanType
667 status;
668
669 RadonInfo
670 *destination_cells,
671 *source_cells;
672
cristybb503372010-05-27 20:51:26 +0000673 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000674 i;
675
676 unsigned char
677 byte;
678
cristybb503372010-05-27 20:51:26 +0000679 size_t
cristy3ed852e2009-09-05 21:47:34 +0000680 count,
681 width;
682
683 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 {
718 register const PixelPacket
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);
732 if (p == (const PixelPacket *) NULL)
733 {
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;
743 if (((MagickRealType) p->red < threshold) ||
744 ((MagickRealType) p->green < threshold) ||
745 ((MagickRealType) p->blue < threshold))
746 byte|=0x01;
747 bit++;
748 if (bit == 8)
749 {
750 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
751 bit=0;
752 byte=0;
753 }
754 p++;
755 }
756 if (bit != 0)
757 {
758 byte<<=(8-bit);
759 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
760 }
761 }
762 RadonProjection(source_cells,destination_cells,-1,projection);
763 (void) ResetRadonCells(source_cells);
cristyb5d5f722009-11-04 03:03:49 +0000764#if defined(MAGICKCORE_OPENMP_SUPPORT)
765 #pragma omp parallel for schedule(dynamic,4) shared(status)
cristy3ed852e2009-09-05 21:47:34 +0000766#endif
cristybb503372010-05-27 20:51:26 +0000767 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000768 {
769 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +0000770 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000771
cristybb503372010-05-27 20:51:26 +0000772 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000773 i,
774 x;
775
cristybb503372010-05-27 20:51:26 +0000776 size_t
cristy3ed852e2009-09-05 21:47:34 +0000777 bit,
778 byte;
779
780 if (status == MagickFalse)
781 continue;
782 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
783 if (p == (const PixelPacket *) NULL)
784 {
785 status=MagickFalse;
786 continue;
787 }
788 bit=0;
789 byte=0;
790 i=0;
cristybb503372010-05-27 20:51:26 +0000791 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000792 {
793 byte<<=1;
794 if (((MagickRealType) p->red < threshold) ||
795 ((MagickRealType) p->green < threshold) ||
796 ((MagickRealType) p->blue < threshold))
797 byte|=0x01;
798 bit++;
799 if (bit == 8)
800 {
801 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
802 bit=0;
803 byte=0;
804 }
805 p++;
806 }
807 if (bit != 0)
808 {
809 byte<<=(8-bit);
810 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
811 }
812 }
813 RadonProjection(source_cells,destination_cells,1,projection);
814 image_view=DestroyCacheView(image_view);
815 destination_cells=DestroyRadonInfo(destination_cells);
816 source_cells=DestroyRadonInfo(source_cells);
817 return(MagickTrue);
818}
819
cristybb503372010-05-27 20:51:26 +0000820static void GetImageBackgroundColor(Image *image,const ssize_t offset,
cristy3ed852e2009-09-05 21:47:34 +0000821 ExceptionInfo *exception)
822{
823 CacheView
824 *image_view;
825
cristybb503372010-05-27 20:51:26 +0000826 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000827 y;
828
829 MagickPixelPacket
830 background;
831
832 MagickRealType
833 count;
834
835 /*
836 Compute average background color.
837 */
838 if (offset <= 0)
839 return;
840 GetMagickPixelPacket(image,&background);
841 count=0.0;
842 image_view=AcquireCacheView(image);
cristybb503372010-05-27 20:51:26 +0000843 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000844 {
845 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +0000846 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000847
cristybb503372010-05-27 20:51:26 +0000848 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000849 x;
850
cristybb503372010-05-27 20:51:26 +0000851 if ((y >= offset) && (y < ((ssize_t) image->rows-offset)))
cristy3ed852e2009-09-05 21:47:34 +0000852 continue;
853 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
854 if (p == (const PixelPacket *) NULL)
855 continue;
cristybb503372010-05-27 20:51:26 +0000856 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000857 {
cristybb503372010-05-27 20:51:26 +0000858 if ((x >= offset) && (x < ((ssize_t) image->columns-offset)))
cristy3ed852e2009-09-05 21:47:34 +0000859 continue;
cristyce70c172010-01-07 17:15:30 +0000860 background.red+=QuantumScale*GetRedPixelComponent(p);
861 background.green+=QuantumScale*GetGreenPixelComponent(p);
862 background.blue+=QuantumScale*GetBluePixelComponent(p);
863 background.opacity+=QuantumScale*GetOpacityPixelComponent(p);
cristy3ed852e2009-09-05 21:47:34 +0000864 count++;
865 p++;
866 }
867 }
868 image_view=DestroyCacheView(image_view);
cristyce70c172010-01-07 17:15:30 +0000869 image->background_color.red=ClampToQuantum((MagickRealType) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +0000870 background.red/count);
cristyce70c172010-01-07 17:15:30 +0000871 image->background_color.green=ClampToQuantum((MagickRealType) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +0000872 background.green/count);
cristyce70c172010-01-07 17:15:30 +0000873 image->background_color.blue=ClampToQuantum((MagickRealType) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +0000874 background.blue/count);
cristyce70c172010-01-07 17:15:30 +0000875 image->background_color.opacity=ClampToQuantum((MagickRealType) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +0000876 background.opacity/count);
877}
878
879MagickExport Image *DeskewImage(const Image *image,const double threshold,
880 ExceptionInfo *exception)
881{
882 AffineMatrix
883 affine_matrix;
884
885 const char
886 *artifact;
887
888 double
889 degrees;
890
891 Image
892 *clone_image,
893 *crop_image,
894 *deskew_image,
895 *median_image;
896
cristybb503372010-05-27 20:51:26 +0000897 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000898 skew;
899
900 MagickBooleanType
901 status;
902
903 RectangleInfo
904 geometry;
905
cristybb503372010-05-27 20:51:26 +0000906 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000907 i;
908
cristybb503372010-05-27 20:51:26 +0000909 size_t
cristy3ed852e2009-09-05 21:47:34 +0000910 max_projection,
911 *projection,
912 width;
913
914 /*
915 Compute deskew angle.
916 */
917 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
cristybb503372010-05-27 20:51:26 +0000918 projection=(size_t *) AcquireQuantumMemory((size_t) (2*width-1),
cristy3ed852e2009-09-05 21:47:34 +0000919 sizeof(*projection));
cristybb503372010-05-27 20:51:26 +0000920 if (projection == (size_t *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000921 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
922 status=RadonTransform(image,threshold,projection,exception);
923 if (status == MagickFalse)
924 {
cristybb503372010-05-27 20:51:26 +0000925 projection=(size_t *) RelinquishMagickMemory(projection);
cristy3ed852e2009-09-05 21:47:34 +0000926 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
927 }
928 max_projection=0;
929 skew=0;
cristybb503372010-05-27 20:51:26 +0000930 for (i=0; i < (ssize_t) (2*width-1); i++)
cristy3ed852e2009-09-05 21:47:34 +0000931 {
932 if (projection[i] > max_projection)
933 {
cristybb503372010-05-27 20:51:26 +0000934 skew=i-(ssize_t) width+1;
cristy3ed852e2009-09-05 21:47:34 +0000935 max_projection=projection[i];
936 }
937 }
cristybb503372010-05-27 20:51:26 +0000938 projection=(size_t *) RelinquishMagickMemory(projection);
cristy3ed852e2009-09-05 21:47:34 +0000939 /*
940 Deskew image.
941 */
942 clone_image=CloneImage(image,0,0,MagickTrue,exception);
943 if (clone_image == (Image *) NULL)
944 return((Image *) NULL);
945 (void) SetImageVirtualPixelMethod(clone_image,BackgroundVirtualPixelMethod);
946 degrees=RadiansToDegrees(-atan((double) skew/width/8));
947 if (image->debug != MagickFalse)
cristy8cd5b312010-01-07 01:10:24 +0000948 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye7f51092010-01-17 00:39:37 +0000949 " Deskew angle: %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +0000950 affine_matrix.sx=cos(DegreesToRadians(fmod((double) degrees,360.0)));
951 affine_matrix.rx=sin(DegreesToRadians(fmod((double) degrees,360.0)));
952 affine_matrix.ry=(-sin(DegreesToRadians(fmod((double) degrees,360.0))));
953 affine_matrix.sy=cos(DegreesToRadians(fmod((double) degrees,360.0)));
954 affine_matrix.tx=0.0;
955 affine_matrix.ty=0.0;
956 artifact=GetImageArtifact(image,"deskew:auto-crop");
957 if (artifact == (const char *) NULL)
958 {
959 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
960 clone_image=DestroyImage(clone_image);
961 return(deskew_image);
962 }
963 /*
964 Auto-crop image.
965 */
cristyf2f27272009-12-17 14:48:46 +0000966 GetImageBackgroundColor(clone_image,StringToLong(artifact),exception);
cristy3ed852e2009-09-05 21:47:34 +0000967 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
968 clone_image=DestroyImage(clone_image);
969 if (deskew_image == (Image *) NULL)
970 return((Image *) NULL);
971 median_image=MedianFilterImage(deskew_image,0.0,exception);
972 if (median_image == (Image *) NULL)
973 {
974 deskew_image=DestroyImage(deskew_image);
975 return((Image *) NULL);
976 }
977 geometry=GetImageBoundingBox(median_image,exception);
978 median_image=DestroyImage(median_image);
979 if (image->debug != MagickFalse)
980 (void) LogMagickEvent(TransformEvent,GetMagickModule()," Deskew geometry: "
cristye8c25f92010-06-03 00:53:06 +0000981 "%.20gx%.20g%+.20gx%+.20g",(double) geometry.width,(double)
982 geometry.height,(double) geometry.x,(double) geometry.y);
cristy3ed852e2009-09-05 21:47:34 +0000983 crop_image=CropImage(deskew_image,&geometry,exception);
984 deskew_image=DestroyImage(deskew_image);
985 return(crop_image);
986}
987
988/*
989%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
990% %
991% %
992% %
993+ I n t e g r a l R o t a t e I m a g e %
994% %
995% %
996% %
997%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
998%
cristy3dfa16c2010-05-17 19:34:48 +0000999% IntegralRotateImage() rotates the image an integral of 90 degrees. It
cristy3ed852e2009-09-05 21:47:34 +00001000% allocates the memory necessary for the new Image structure and returns a
1001% pointer to the rotated image.
1002%
1003% The format of the IntegralRotateImage method is:
1004%
cristybb503372010-05-27 20:51:26 +00001005% Image *IntegralRotateImage(const Image *image,size_t rotations,
cristy3ed852e2009-09-05 21:47:34 +00001006% ExceptionInfo *exception)
1007%
1008% A description of each parameter follows.
1009%
1010% o image: the image.
1011%
1012% o rotations: Specifies the number of 90 degree rotations.
1013%
1014*/
cristybb503372010-05-27 20:51:26 +00001015static Image *IntegralRotateImage(const Image *image,size_t rotations,
cristy3ed852e2009-09-05 21:47:34 +00001016 ExceptionInfo *exception)
1017{
cristy3ed852e2009-09-05 21:47:34 +00001018#define RotateImageTag "Rotate/Image"
1019
1020 CacheView
1021 *image_view,
1022 *rotate_view;
1023
1024 Image
1025 *rotate_image;
1026
cristy3ed852e2009-09-05 21:47:34 +00001027 MagickBooleanType
1028 status;
1029
cristy5f959472010-05-27 22:19:46 +00001030 MagickOffsetType
1031 progress;
1032
cristy3ed852e2009-09-05 21:47:34 +00001033 RectangleInfo
1034 page;
1035
cristy5f959472010-05-27 22:19:46 +00001036 ssize_t
1037 y;
1038
cristy3ed852e2009-09-05 21:47:34 +00001039 /*
1040 Initialize rotated image attributes.
1041 */
1042 assert(image != (Image *) NULL);
1043 page=image->page;
1044 rotations%=4;
cristyb32b90a2009-09-07 21:45:48 +00001045 if (rotations == 0)
1046 return(CloneImage(image,0,0,MagickTrue,exception));
cristy3ed852e2009-09-05 21:47:34 +00001047 if ((rotations == 1) || (rotations == 3))
1048 rotate_image=CloneImage(image,image->rows,image->columns,MagickTrue,
1049 exception);
1050 else
1051 rotate_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1052 exception);
1053 if (rotate_image == (Image *) NULL)
1054 return((Image *) NULL);
1055 /*
1056 Integral rotate the image.
1057 */
1058 status=MagickTrue;
1059 progress=0;
1060 image_view=AcquireCacheView(image);
1061 rotate_view=AcquireCacheView(rotate_image);
1062 switch (rotations)
1063 {
1064 case 0:
1065 {
1066 /*
1067 Rotate 0 degrees.
1068 */
cristy3ed852e2009-09-05 21:47:34 +00001069 break;
1070 }
1071 case 1:
1072 {
cristybb503372010-05-27 20:51:26 +00001073 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001074 tile_y;
1075
cristybb503372010-05-27 20:51:26 +00001076 size_t
cristyb32b90a2009-09-07 21:45:48 +00001077 tile_height,
1078 tile_width;
1079
cristy3ed852e2009-09-05 21:47:34 +00001080 /*
1081 Rotate 90 degrees.
1082 */
cristyb32b90a2009-09-07 21:45:48 +00001083 GetPixelCacheTileSize(image,&tile_width,&tile_height);
cristyd3dd0502010-04-26 00:57:20 +00001084#if defined(MAGICKCORE_OPENMP_SUPPORT) && defined(MAGICKCORE_FUTURE)
cristy8a7ea362010-03-10 20:31:43 +00001085 #pragma omp parallel for schedule(dynamic,4) shared(progress, status)
cristydaa97692009-09-13 02:10:35 +00001086#endif
cristyeaedf062010-05-29 22:36:02 +00001087 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
cristy3ed852e2009-09-05 21:47:34 +00001088 {
cristybb503372010-05-27 20:51:26 +00001089 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001090 tile_x;
1091
1092 if (status == MagickFalse)
1093 continue;
cristyeaedf062010-05-29 22:36:02 +00001094 for (tile_x=0; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
cristy3ed852e2009-09-05 21:47:34 +00001095 {
1096 MagickBooleanType
1097 sync;
1098
1099 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001100 *restrict indexes;
cristy3ed852e2009-09-05 21:47:34 +00001101
1102 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001103 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001104
1105 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001106 *restrict rotate_indexes;
cristy3ed852e2009-09-05 21:47:34 +00001107
cristybb503372010-05-27 20:51:26 +00001108 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001109 y;
1110
1111 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001112 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001113
cristybb503372010-05-27 20:51:26 +00001114 size_t
cristyb32b90a2009-09-07 21:45:48 +00001115 height,
1116 width;
cristy3ed852e2009-09-05 21:47:34 +00001117
cristyb32b90a2009-09-07 21:45:48 +00001118 width=tile_width;
cristybb503372010-05-27 20:51:26 +00001119 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
1120 width=(size_t) (tile_width-(tile_x+tile_width-
cristy3ed852e2009-09-05 21:47:34 +00001121 image->columns));
cristyb32b90a2009-09-07 21:45:48 +00001122 height=tile_height;
cristybb503372010-05-27 20:51:26 +00001123 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
1124 height=(size_t) (tile_height-(tile_y+tile_height-
cristy3ed852e2009-09-05 21:47:34 +00001125 image->rows));
cristydaa97692009-09-13 02:10:35 +00001126 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1127 exception);
cristy3ed852e2009-09-05 21:47:34 +00001128 if (p == (const PixelPacket *) NULL)
1129 {
1130 status=MagickFalse;
1131 break;
1132 }
1133 indexes=GetCacheViewVirtualIndexQueue(image_view);
cristybb503372010-05-27 20:51:26 +00001134 for (y=0; y < (ssize_t) width; y++)
cristy3ed852e2009-09-05 21:47:34 +00001135 {
1136 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001137 *restrict tile_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001138
cristybb503372010-05-27 20:51:26 +00001139 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001140 x;
1141
cristybb503372010-05-27 20:51:26 +00001142 q=QueueCacheViewAuthenticPixels(rotate_view,(ssize_t)
cristyeaedf062010-05-29 22:36:02 +00001143 (rotate_image->columns-(tile_y+height)),y+tile_x,height,
cristy3ed852e2009-09-05 21:47:34 +00001144 1,exception);
1145 if (q == (PixelPacket *) NULL)
1146 {
1147 status=MagickFalse;
1148 break;
1149 }
cristyb32b90a2009-09-07 21:45:48 +00001150 tile_pixels=p+(height-1)*width+y;
cristybb503372010-05-27 20:51:26 +00001151 for (x=0; x < (ssize_t) height; x++)
cristy3ed852e2009-09-05 21:47:34 +00001152 {
1153 *q++=(*tile_pixels);
cristyb32b90a2009-09-07 21:45:48 +00001154 tile_pixels-=width;
cristy3ed852e2009-09-05 21:47:34 +00001155 }
1156 rotate_indexes=GetCacheViewAuthenticIndexQueue(rotate_view);
1157 if ((indexes != (IndexPacket *) NULL) &&
1158 (rotate_indexes != (IndexPacket *) NULL))
1159 {
1160 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001161 *restrict tile_indexes;
cristy3ed852e2009-09-05 21:47:34 +00001162
cristyb32b90a2009-09-07 21:45:48 +00001163 tile_indexes=indexes+(height-1)*width+y;
cristybb503372010-05-27 20:51:26 +00001164 for (x=0; x < (ssize_t) height; x++)
cristy3ed852e2009-09-05 21:47:34 +00001165 {
1166 *rotate_indexes++=(*tile_indexes);
cristyb32b90a2009-09-07 21:45:48 +00001167 tile_indexes-=width;
cristy3ed852e2009-09-05 21:47:34 +00001168 }
1169 }
1170 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
cristyb32b90a2009-09-07 21:45:48 +00001180 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
cristy3ed852e2009-09-05 21:47:34 +00001181 image->rows);
1182 if (proceed == MagickFalse)
1183 status=MagickFalse;
1184 }
1185 }
cristyecc2c142010-01-17 22:25:46 +00001186 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1187 image->rows-1,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001188 Swap(page.width,page.height);
1189 Swap(page.x,page.y);
1190 if (page.width != 0)
cristybb503372010-05-27 20:51:26 +00001191 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
cristy3ed852e2009-09-05 21:47:34 +00001192 break;
1193 }
1194 case 2:
1195 {
1196 /*
1197 Rotate 180 degrees.
1198 */
cristyd3dd0502010-04-26 00:57:20 +00001199#if defined(MAGICKCORE_OPENMP_SUPPORT) && defined(MAGICKCORE_FUTURE)
cristy5f959472010-05-27 22:19:46 +00001200 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristydaa97692009-09-13 02:10:35 +00001201#endif
cristybb503372010-05-27 20:51:26 +00001202 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001203 {
1204 MagickBooleanType
1205 sync;
1206
1207 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001208 *restrict indexes;
cristy3ed852e2009-09-05 21:47:34 +00001209
1210 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001211 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001212
1213 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001214 *restrict rotate_indexes;
cristy3ed852e2009-09-05 21:47:34 +00001215
cristybb503372010-05-27 20:51:26 +00001216 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001217 x;
1218
1219 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001220 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001221
1222 if (status == MagickFalse)
1223 continue;
1224 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,
1225 exception);
cristybb503372010-05-27 20:51:26 +00001226 q=QueueCacheViewAuthenticPixels(rotate_view,0,(ssize_t) (image->rows-
cristy3ed852e2009-09-05 21:47:34 +00001227 y-1),image->columns,1,exception);
1228 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
1229 {
1230 status=MagickFalse;
1231 continue;
1232 }
1233 indexes=GetCacheViewVirtualIndexQueue(image_view);
1234 rotate_indexes=GetCacheViewAuthenticIndexQueue(rotate_view);
1235 q+=image->columns;
cristybb503372010-05-27 20:51:26 +00001236 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001237 *--q=(*p++);
1238 if ((indexes != (IndexPacket *) NULL) &&
1239 (rotate_indexes != (IndexPacket *) NULL))
cristybb503372010-05-27 20:51:26 +00001240 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001241 rotate_indexes[image->columns-x-1]=indexes[x];
1242 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1243 if (sync == MagickFalse)
1244 status=MagickFalse;
1245 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1246 {
1247 MagickBooleanType
1248 proceed;
1249
1250 proceed=SetImageProgress(image,RotateImageTag,progress++,
1251 image->rows);
1252 if (proceed == MagickFalse)
1253 status=MagickFalse;
1254 }
1255 }
1256 if (page.width != 0)
cristybb503372010-05-27 20:51:26 +00001257 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
cristy3ed852e2009-09-05 21:47:34 +00001258 if (page.height != 0)
cristybb503372010-05-27 20:51:26 +00001259 page.y=(ssize_t) (page.height-rotate_image->rows-page.y);
cristy3ed852e2009-09-05 21:47:34 +00001260 break;
1261 }
1262 case 3:
1263 {
cristybb503372010-05-27 20:51:26 +00001264 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001265 tile_y;
1266
cristybb503372010-05-27 20:51:26 +00001267 size_t
cristyb32b90a2009-09-07 21:45:48 +00001268 tile_height,
1269 tile_width;
1270
cristy3ed852e2009-09-05 21:47:34 +00001271 /*
1272 Rotate 270 degrees.
1273 */
cristyb32b90a2009-09-07 21:45:48 +00001274 GetPixelCacheTileSize(image,&tile_width,&tile_height);
cristyd3dd0502010-04-26 00:57:20 +00001275#if defined(MAGICKCORE_OPENMP_SUPPORT) && defined(MAGICKCORE_FUTURE)
cristy5f959472010-05-27 22:19:46 +00001276 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristydaa97692009-09-13 02:10:35 +00001277#endif
cristyeaedf062010-05-29 22:36:02 +00001278 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
cristy3ed852e2009-09-05 21:47:34 +00001279 {
cristybb503372010-05-27 20:51:26 +00001280 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001281 tile_x;
1282
1283 if (status == MagickFalse)
1284 continue;
cristyeaedf062010-05-29 22:36:02 +00001285 for (tile_x=0; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
cristy3ed852e2009-09-05 21:47:34 +00001286 {
1287 MagickBooleanType
1288 sync;
1289
1290 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001291 *restrict indexes;
cristy3ed852e2009-09-05 21:47:34 +00001292
1293 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001294 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001295
1296 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001297 *restrict rotate_indexes;
cristy3ed852e2009-09-05 21:47:34 +00001298
cristybb503372010-05-27 20:51:26 +00001299 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001300 y;
1301
1302 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001303 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001304
cristybb503372010-05-27 20:51:26 +00001305 size_t
cristyb32b90a2009-09-07 21:45:48 +00001306 height,
1307 width;
cristy3ed852e2009-09-05 21:47:34 +00001308
cristyb32b90a2009-09-07 21:45:48 +00001309 width=tile_width;
cristybb503372010-05-27 20:51:26 +00001310 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
1311 width=(size_t) (tile_width-(tile_x+tile_width-
cristy3ed852e2009-09-05 21:47:34 +00001312 image->columns));
cristyb32b90a2009-09-07 21:45:48 +00001313 height=tile_height;
cristybb503372010-05-27 20:51:26 +00001314 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
1315 height=(size_t) (tile_height-(tile_y+tile_height-
cristy3ed852e2009-09-05 21:47:34 +00001316 image->rows));
cristyb32b90a2009-09-07 21:45:48 +00001317 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,
1318 height,exception);
cristy3ed852e2009-09-05 21:47:34 +00001319 if (p == (const PixelPacket *) NULL)
1320 {
1321 status=MagickFalse;
1322 break;
1323 }
1324 indexes=GetCacheViewVirtualIndexQueue(image_view);
cristybb503372010-05-27 20:51:26 +00001325 for (y=0; y < (ssize_t) width; y++)
cristy3ed852e2009-09-05 21:47:34 +00001326 {
1327 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001328 *restrict tile_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001329
cristybb503372010-05-27 20:51:26 +00001330 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001331 x;
1332
cristybb503372010-05-27 20:51:26 +00001333 q=QueueCacheViewAuthenticPixels(rotate_view,tile_y,(ssize_t)
cristyeaedf062010-05-29 22:36:02 +00001334 (y+rotate_image->rows-(tile_x+width)),height,1,exception);
cristy3ed852e2009-09-05 21:47:34 +00001335 if (q == (PixelPacket *) NULL)
1336 {
1337 status=MagickFalse;
1338 break;
1339 }
cristyb32b90a2009-09-07 21:45:48 +00001340 tile_pixels=p+(width-1)-y;
cristybb503372010-05-27 20:51:26 +00001341 for (x=0; x < (ssize_t) height; x++)
cristy3ed852e2009-09-05 21:47:34 +00001342 {
1343 *q++=(*tile_pixels);
cristyb32b90a2009-09-07 21:45:48 +00001344 tile_pixels+=width;
cristy3ed852e2009-09-05 21:47:34 +00001345 }
1346 rotate_indexes=GetCacheViewAuthenticIndexQueue(rotate_view);
1347 if ((indexes != (IndexPacket *) NULL) &&
1348 (rotate_indexes != (IndexPacket *) NULL))
1349 {
1350 register const IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001351 *restrict tile_indexes;
cristy3ed852e2009-09-05 21:47:34 +00001352
cristyb32b90a2009-09-07 21:45:48 +00001353 tile_indexes=indexes+(width-1)-y;
cristybb503372010-05-27 20:51:26 +00001354 for (x=0; x < (ssize_t) height; x++)
cristy3ed852e2009-09-05 21:47:34 +00001355 {
1356 *rotate_indexes++=(*tile_indexes);
cristyb32b90a2009-09-07 21:45:48 +00001357 tile_indexes+=width;
cristy3ed852e2009-09-05 21:47:34 +00001358 }
1359 }
1360 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1361 if (sync == MagickFalse)
1362 status=MagickFalse;
1363 }
1364 }
1365 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1366 {
1367 MagickBooleanType
1368 proceed;
1369
cristyb32b90a2009-09-07 21:45:48 +00001370 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
cristy3ed852e2009-09-05 21:47:34 +00001371 image->rows);
1372 if (proceed == MagickFalse)
1373 status=MagickFalse;
1374 }
1375 }
cristyecc2c142010-01-17 22:25:46 +00001376 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1377 image->rows-1,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001378 Swap(page.width,page.height);
1379 Swap(page.x,page.y);
1380 if (page.height != 0)
cristybb503372010-05-27 20:51:26 +00001381 page.y=(ssize_t) (page.height-rotate_image->rows-page.y);
cristy3ed852e2009-09-05 21:47:34 +00001382 break;
1383 }
1384 }
1385 rotate_view=DestroyCacheView(rotate_view);
1386 image_view=DestroyCacheView(image_view);
1387 rotate_image->type=image->type;
1388 rotate_image->page=page;
1389 if (status == MagickFalse)
1390 rotate_image=DestroyImage(rotate_image);
1391 return(rotate_image);
1392}
1393
1394/*
1395%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1396% %
1397% %
1398% %
1399+ X S h e a r I m a g e %
1400% %
1401% %
1402% %
1403%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1404%
1405% XShearImage() shears the image in the X direction with a shear angle of
1406% 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1407% negative angles shear clockwise. Angles are measured relative to a vertical
1408% Y-axis. X shears will widen an image creating 'empty' triangles on the left
1409% and right sides of the source image.
1410%
1411% The format of the XShearImage method is:
1412%
1413% MagickBooleanType XShearImage(Image *image,const MagickRealType degrees,
cristybb503372010-05-27 20:51:26 +00001414% const size_t width,const size_t height,
1415% const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001416%
1417% A description of each parameter follows.
1418%
1419% o image: the image.
1420%
cristycee97112010-05-28 00:44:52 +00001421% o degrees: A MagickRealType representing the shearing angle along the X
cristy3ed852e2009-09-05 21:47:34 +00001422% axis.
1423%
1424% o width, height, x_offset, y_offset: Defines a region of the image
1425% to shear.
1426%
cristyecc2c142010-01-17 22:25:46 +00001427% o exception: return any errors or warnings in this structure.
1428%
cristy3ed852e2009-09-05 21:47:34 +00001429*/
1430static MagickBooleanType XShearImage(Image *image,const MagickRealType degrees,
cristybb503372010-05-27 20:51:26 +00001431 const size_t width,const size_t height,const ssize_t x_offset,
1432 const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001433{
1434#define XShearImageTag "XShear/Image"
1435
1436 typedef enum
1437 {
1438 LEFT,
1439 RIGHT
1440 } ShearDirection;
1441
1442 CacheView
1443 *image_view;
1444
cristy3ed852e2009-09-05 21:47:34 +00001445 MagickBooleanType
1446 status;
1447
cristy5f959472010-05-27 22:19:46 +00001448 MagickOffsetType
1449 progress;
1450
cristy3ed852e2009-09-05 21:47:34 +00001451 MagickPixelPacket
1452 background;
1453
cristy5f959472010-05-27 22:19:46 +00001454 ssize_t
1455 y;
1456
cristy3ed852e2009-09-05 21:47:34 +00001457 assert(image != (Image *) NULL);
1458 assert(image->signature == MagickSignature);
1459 if (image->debug != MagickFalse)
1460 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1461 GetMagickPixelPacket(image,&background);
1462 SetMagickPixelPacket(image,&image->background_color,(IndexPacket *) NULL,
1463 &background);
1464 if (image->colorspace == CMYKColorspace)
1465 ConvertRGBToCMYK(&background);
1466 /*
1467 XShear image.
1468 */
1469 status=MagickTrue;
1470 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00001471 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00001472#if defined(MAGICKCORE_OPENMP_SUPPORT)
1473 #pragma omp parallel for schedule(dynamic,4) shared(progress, status)
cristy3ed852e2009-09-05 21:47:34 +00001474#endif
cristybb503372010-05-27 20:51:26 +00001475 for (y=0; y < (ssize_t) height; y++)
cristy3ed852e2009-09-05 21:47:34 +00001476 {
cristybb503372010-05-27 20:51:26 +00001477 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001478 step;
1479
1480 MagickPixelPacket
1481 pixel,
1482 source,
1483 destination;
1484
1485 MagickRealType
1486 area,
1487 displacement;
1488
cristybb503372010-05-27 20:51:26 +00001489 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001490 i;
1491
1492 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001493 *restrict indexes,
1494 *restrict shear_indexes;
cristy3ed852e2009-09-05 21:47:34 +00001495
1496 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001497 *restrict p,
1498 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001499
1500 ShearDirection
1501 direction;
1502
1503 if (status == MagickFalse)
1504 continue;
1505 p=GetCacheViewAuthenticPixels(image_view,0,y_offset+y,image->columns,1,
1506 exception);
1507 if (p == (PixelPacket *) NULL)
1508 {
1509 status=MagickFalse;
1510 continue;
1511 }
1512 indexes=GetCacheViewAuthenticIndexQueue(image_view);
1513 p+=x_offset;
1514 indexes+=x_offset;
1515 displacement=degrees*(MagickRealType) (y-height/2.0);
1516 if (displacement == 0.0)
1517 continue;
1518 if (displacement > 0.0)
1519 direction=RIGHT;
1520 else
1521 {
1522 displacement*=(-1.0);
1523 direction=LEFT;
1524 }
cristybb503372010-05-27 20:51:26 +00001525 step=(ssize_t) floor((double) displacement);
cristy3ed852e2009-09-05 21:47:34 +00001526 area=(MagickRealType) (displacement-step);
1527 step++;
1528 pixel=background;
1529 GetMagickPixelPacket(image,&source);
1530 GetMagickPixelPacket(image,&destination);
1531 switch (direction)
1532 {
1533 case LEFT:
1534 {
1535 /*
1536 Transfer pixels left-to-right.
1537 */
1538 if (step > x_offset)
1539 break;
1540 q=p-step;
1541 shear_indexes=indexes-step;
cristybb503372010-05-27 20:51:26 +00001542 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001543 {
1544 if ((x_offset+i) < step)
1545 {
1546 SetMagickPixelPacket(image,++p,++indexes,&pixel);
1547 q++;
1548 shear_indexes++;
1549 continue;
1550 }
1551 SetMagickPixelPacket(image,p,indexes,&source);
1552 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1553 &source,(MagickRealType) p->opacity,area,&destination);
1554 SetPixelPacket(image,&destination,q++,shear_indexes++);
1555 SetMagickPixelPacket(image,p++,indexes++,&pixel);
1556 }
1557 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1558 &background,(MagickRealType) background.opacity,area,&destination);
1559 SetPixelPacket(image,&destination,q++,shear_indexes++);
1560 for (i=0; i < (step-1); i++)
1561 SetPixelPacket(image,&background,q++,shear_indexes++);
1562 break;
1563 }
1564 case RIGHT:
1565 {
1566 /*
1567 Transfer pixels right-to-left.
1568 */
1569 p+=width;
1570 indexes+=width;
1571 q=p+step;
1572 shear_indexes=indexes+step;
cristybb503372010-05-27 20:51:26 +00001573 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001574 {
1575 p--;
1576 indexes--;
1577 q--;
1578 shear_indexes--;
cristybb503372010-05-27 20:51:26 +00001579 if ((size_t) (x_offset+width+step-i) >= image->columns)
cristy3ed852e2009-09-05 21:47:34 +00001580 continue;
1581 SetMagickPixelPacket(image,p,indexes,&source);
1582 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1583 &source,(MagickRealType) p->opacity,area,&destination);
1584 SetPixelPacket(image,&destination,q,shear_indexes);
1585 SetMagickPixelPacket(image,p,indexes,&pixel);
1586 }
1587 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1588 &background,(MagickRealType) background.opacity,area,&destination);
1589 SetPixelPacket(image,&destination,--q,--shear_indexes);
1590 for (i=0; i < (step-1); i++)
1591 SetPixelPacket(image,&background,--q,--shear_indexes);
1592 break;
1593 }
1594 }
1595 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1596 status=MagickFalse;
1597 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1598 {
1599 MagickBooleanType
1600 proceed;
1601
cristyb5d5f722009-11-04 03:03:49 +00001602#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00001603 #pragma omp critical (MagickCore_XShearImage)
1604#endif
1605 proceed=SetImageProgress(image,XShearImageTag,progress++,height);
1606 if (proceed == MagickFalse)
1607 status=MagickFalse;
1608 }
1609 }
1610 image_view=DestroyCacheView(image_view);
1611 return(status);
1612}
1613
1614/*
1615%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1616% %
1617% %
1618% %
1619+ Y S h e a r I m a g e %
1620% %
1621% %
1622% %
1623%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1624%
1625% YShearImage shears the image in the Y direction with a shear angle of
1626% 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1627% negative angles shear clockwise. Angles are measured relative to a
1628% horizontal X-axis. Y shears will increase the height of an image creating
1629% 'empty' triangles on the top and bottom of the source image.
1630%
1631% The format of the YShearImage method is:
1632%
1633% MagickBooleanType YShearImage(Image *image,const MagickRealType degrees,
cristybb503372010-05-27 20:51:26 +00001634% const size_t width,const size_t height,
1635% const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001636%
1637% A description of each parameter follows.
1638%
1639% o image: the image.
1640%
cristycee97112010-05-28 00:44:52 +00001641% o degrees: A MagickRealType representing the shearing angle along the Y
cristy3ed852e2009-09-05 21:47:34 +00001642% axis.
1643%
1644% o width, height, x_offset, y_offset: Defines a region of the image
1645% to shear.
1646%
cristyecc2c142010-01-17 22:25:46 +00001647% o exception: return any errors or warnings in this structure.
1648%
cristy3ed852e2009-09-05 21:47:34 +00001649*/
1650static MagickBooleanType YShearImage(Image *image,const MagickRealType degrees,
cristybb503372010-05-27 20:51:26 +00001651 const size_t width,const size_t height,const ssize_t x_offset,
1652 const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001653{
1654#define YShearImageTag "YShear/Image"
1655
1656 typedef enum
1657 {
1658 UP,
1659 DOWN
1660 } ShearDirection;
1661
1662 CacheView
1663 *image_view;
1664
cristy3ed852e2009-09-05 21:47:34 +00001665 MagickBooleanType
1666 status;
1667
cristy5f959472010-05-27 22:19:46 +00001668 MagickOffsetType
1669 progress;
1670
cristy3ed852e2009-09-05 21:47:34 +00001671 MagickPixelPacket
1672 background;
1673
cristy5f959472010-05-27 22:19:46 +00001674 ssize_t
1675 x;
1676
cristy3ed852e2009-09-05 21:47:34 +00001677 assert(image != (Image *) NULL);
1678 assert(image->signature == MagickSignature);
1679 if (image->debug != MagickFalse)
1680 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1681 GetMagickPixelPacket(image,&background);
1682 SetMagickPixelPacket(image,&image->background_color,(IndexPacket *) NULL,
1683 &background);
1684 if (image->colorspace == CMYKColorspace)
1685 ConvertRGBToCMYK(&background);
1686 /*
1687 Y Shear image.
1688 */
1689 status=MagickTrue;
1690 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00001691 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00001692#if defined(MAGICKCORE_OPENMP_SUPPORT)
1693 #pragma omp parallel for schedule(dynamic,4) shared(progress, status)
cristy3ed852e2009-09-05 21:47:34 +00001694#endif
cristybb503372010-05-27 20:51:26 +00001695 for (x=0; x < (ssize_t) width; x++)
cristy3ed852e2009-09-05 21:47:34 +00001696 {
cristybb503372010-05-27 20:51:26 +00001697 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001698 step;
1699
1700 MagickPixelPacket
1701 pixel,
1702 source,
1703 destination;
1704
1705 MagickRealType
1706 area,
1707 displacement;
1708
1709 register IndexPacket
cristyc47d1f82009-11-26 01:44:43 +00001710 *restrict indexes,
1711 *restrict shear_indexes;
cristy3ed852e2009-09-05 21:47:34 +00001712
cristybb503372010-05-27 20:51:26 +00001713 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001714 i;
1715
1716 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001717 *restrict p,
1718 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001719
1720 ShearDirection
1721 direction;
1722
1723 if (status == MagickFalse)
1724 continue;
1725 p=GetCacheViewAuthenticPixels(image_view,x_offset+x,0,1,image->rows,
1726 exception);
1727 if (p == (PixelPacket *) NULL)
1728 {
1729 status=MagickFalse;
1730 continue;
1731 }
1732 indexes=GetCacheViewAuthenticIndexQueue(image_view);
1733 p+=y_offset;
1734 indexes+=y_offset;
1735 displacement=degrees*(MagickRealType) (x-width/2.0);
1736 if (displacement == 0.0)
1737 continue;
1738 if (displacement > 0.0)
1739 direction=DOWN;
1740 else
1741 {
1742 displacement*=(-1.0);
1743 direction=UP;
1744 }
cristybb503372010-05-27 20:51:26 +00001745 step=(ssize_t) floor((double) displacement);
cristy3ed852e2009-09-05 21:47:34 +00001746 area=(MagickRealType) (displacement-step);
1747 step++;
1748 pixel=background;
1749 GetMagickPixelPacket(image,&source);
1750 GetMagickPixelPacket(image,&destination);
1751 switch (direction)
1752 {
1753 case UP:
1754 {
1755 /*
1756 Transfer pixels top-to-bottom.
1757 */
1758 if (step > y_offset)
1759 break;
1760 q=p-step;
1761 shear_indexes=indexes-step;
cristybb503372010-05-27 20:51:26 +00001762 for (i=0; i < (ssize_t) height; i++)
cristy3ed852e2009-09-05 21:47:34 +00001763 {
1764 if ((y_offset+i) < step)
1765 {
1766 SetMagickPixelPacket(image,++p,++indexes,&pixel);
1767 q++;
1768 shear_indexes++;
1769 continue;
1770 }
1771 SetMagickPixelPacket(image,p,indexes,&source);
1772 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1773 &source,(MagickRealType) p->opacity,area,&destination);
1774 SetPixelPacket(image,&destination,q++,shear_indexes++);
1775 SetMagickPixelPacket(image,p++,indexes++,&pixel);
1776 }
1777 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1778 &background,(MagickRealType) background.opacity,area,&destination);
1779 SetPixelPacket(image,&destination,q++,shear_indexes++);
1780 for (i=0; i < (step-1); i++)
1781 SetPixelPacket(image,&background,q++,shear_indexes++);
1782 break;
1783 }
1784 case DOWN:
1785 {
1786 /*
1787 Transfer pixels bottom-to-top.
1788 */
1789 p+=height;
1790 indexes+=height;
1791 q=p+step;
1792 shear_indexes=indexes+step;
cristybb503372010-05-27 20:51:26 +00001793 for (i=0; i < (ssize_t) height; i++)
cristy3ed852e2009-09-05 21:47:34 +00001794 {
1795 p--;
1796 indexes--;
1797 q--;
1798 shear_indexes--;
cristybb503372010-05-27 20:51:26 +00001799 if ((size_t) (y_offset+height+step-i) >= image->rows)
cristy3ed852e2009-09-05 21:47:34 +00001800 continue;
1801 SetMagickPixelPacket(image,p,indexes,&source);
1802 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1803 &source,(MagickRealType) p->opacity,area,&destination);
1804 SetPixelPacket(image,&destination,q,shear_indexes);
1805 SetMagickPixelPacket(image,p,indexes,&pixel);
1806 }
1807 MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity,
1808 &background,(MagickRealType) background.opacity,area,&destination);
1809 SetPixelPacket(image,&destination,--q,--shear_indexes);
1810 for (i=0; i < (step-1); i++)
1811 SetPixelPacket(image,&background,--q,--shear_indexes);
1812 break;
1813 }
1814 }
1815 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1816 status=MagickFalse;
1817 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1818 {
1819 MagickBooleanType
1820 proceed;
1821
cristyb5d5f722009-11-04 03:03:49 +00001822#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00001823 #pragma omp critical (MagickCore_YShearImage)
1824#endif
1825 proceed=SetImageProgress(image,YShearImageTag,progress++,image->rows);
1826 if (proceed == MagickFalse)
1827 status=MagickFalse;
1828 }
1829 }
1830 image_view=DestroyCacheView(image_view);
1831 return(status);
1832}
1833
1834/*
1835%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1836% %
1837% %
1838% %
1839% R o t a t e I m a g e %
1840% %
1841% %
1842% %
1843%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1844%
1845% RotateImage() creates a new image that is a rotated copy of an existing
1846% one. Positive angles rotate counter-clockwise (right-hand rule), while
1847% negative angles rotate clockwise. Rotated images are usually larger than
1848% the originals and have 'empty' triangular corners. X axis. Empty
1849% triangles left over from shearing the image are filled with the background
1850% color defined by member 'background_color' of the image. RotateImage
1851% allocates the memory necessary for the new Image structure and returns a
1852% pointer to the new image.
1853%
1854% RotateImage() is based on the paper "A Fast Algorithm for General
1855% Raster Rotatation" by Alan W. Paeth. RotateImage is adapted from a similar
1856% method based on the Paeth paper written by Michael Halle of the Spatial
1857% Imaging Group, MIT Media Lab.
1858%
1859% The format of the RotateImage method is:
1860%
1861% Image *RotateImage(const Image *image,const double degrees,
1862% ExceptionInfo *exception)
1863%
1864% A description of each parameter follows.
1865%
1866% o image: the image.
1867%
1868% o degrees: Specifies the number of degrees to rotate the image.
1869%
1870% o exception: return any errors or warnings in this structure.
1871%
1872*/
1873MagickExport Image *RotateImage(const Image *image,const double degrees,
1874 ExceptionInfo *exception)
1875{
1876 Image
1877 *integral_image,
1878 *rotate_image;
1879
cristybb503372010-05-27 20:51:26 +00001880 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001881 x_offset,
1882 y_offset;
1883
cristyecc2c142010-01-17 22:25:46 +00001884 MagickBooleanType
1885 status;
1886
cristy3ed852e2009-09-05 21:47:34 +00001887 MagickRealType
1888 angle;
1889
1890 PointInfo
1891 shear;
1892
1893 RectangleInfo
1894 border_info;
1895
cristybb503372010-05-27 20:51:26 +00001896 size_t
cristy3ed852e2009-09-05 21:47:34 +00001897 height,
1898 rotations,
1899 width,
1900 y_width;
1901
1902 /*
1903 Adjust rotation angle.
1904 */
1905 assert(image != (Image *) NULL);
1906 assert(image->signature == MagickSignature);
1907 if (image->debug != MagickFalse)
1908 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1909 assert(exception != (ExceptionInfo *) NULL);
1910 assert(exception->signature == MagickSignature);
1911 angle=degrees;
1912 while (angle < -45.0)
1913 angle+=360.0;
1914 for (rotations=0; angle > 45.0; rotations++)
1915 angle-=90.0;
1916 rotations%=4;
1917 /*
1918 Calculate shear equations.
1919 */
1920 integral_image=IntegralRotateImage(image,rotations,exception);
1921 if (integral_image == (Image *) NULL)
1922 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1923 shear.x=(-tan((double) DegreesToRadians(angle)/2.0));
1924 shear.y=sin((double) DegreesToRadians(angle));
1925 if ((shear.x == 0.0) && (shear.y == 0.0))
1926 return(integral_image);
1927 if (SetImageStorageClass(integral_image,DirectClass) == MagickFalse)
1928 {
1929 InheritException(exception,&integral_image->exception);
1930 integral_image=DestroyImage(integral_image);
1931 return(integral_image);
1932 }
1933 if (integral_image->matte == MagickFalse)
1934 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel);
1935 /*
1936 Compute image size.
1937 */
1938 width=image->columns;
1939 height=image->rows;
1940 if ((rotations == 1) || (rotations == 3))
1941 {
1942 width=image->rows;
1943 height=image->columns;
1944 }
cristybb503372010-05-27 20:51:26 +00001945 y_width=width+(ssize_t) floor(fabs(shear.x)*height+0.5);
cristycee97112010-05-28 00:44:52 +00001946 x_offset=(ssize_t) ceil((double) width+((fabs(shear.y)*height)-width)/2.0-
1947 0.5);
1948 y_offset=(ssize_t) ceil((double) height+((fabs(shear.y)*y_width)-height)/2.0-
1949 0.5);
cristy3ed852e2009-09-05 21:47:34 +00001950 /*
1951 Surround image with a border.
1952 */
1953 integral_image->border_color=integral_image->background_color;
1954 integral_image->compose=CopyCompositeOp;
cristybb503372010-05-27 20:51:26 +00001955 border_info.width=(size_t) x_offset;
1956 border_info.height=(size_t) y_offset;
cristy3ed852e2009-09-05 21:47:34 +00001957 rotate_image=BorderImage(integral_image,&border_info,exception);
1958 integral_image=DestroyImage(integral_image);
1959 if (rotate_image == (Image *) NULL)
1960 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1961 /*
1962 Rotate the image.
1963 */
cristyeaedf062010-05-29 22:36:02 +00001964 status=XShearImage(rotate_image,shear.x,width,height,x_offset,(ssize_t)
1965 (rotate_image->rows-height)/2,exception);
cristyecc2c142010-01-17 22:25:46 +00001966 if (status == MagickFalse)
1967 {
1968 rotate_image=DestroyImage(rotate_image);
1969 return((Image *) NULL);
1970 }
cristyeaedf062010-05-29 22:36:02 +00001971 status=YShearImage(rotate_image,shear.y,y_width,height,(ssize_t)
1972 (rotate_image->columns-y_width)/2,y_offset,exception);
cristyecc2c142010-01-17 22:25:46 +00001973 if (status == MagickFalse)
1974 {
1975 rotate_image=DestroyImage(rotate_image);
1976 return((Image *) NULL);
1977 }
cristyeaedf062010-05-29 22:36:02 +00001978 status=XShearImage(rotate_image,shear.x,y_width,rotate_image->rows,(ssize_t)
1979 (rotate_image->columns-y_width)/2,0,exception);
cristyecc2c142010-01-17 22:25:46 +00001980 if (status == MagickFalse)
1981 {
1982 rotate_image=DestroyImage(rotate_image);
1983 return((Image *) NULL);
1984 }
1985 status=CropToFitImage(&rotate_image,shear.x,shear.y,(MagickRealType) width,
cristy3ed852e2009-09-05 21:47:34 +00001986 (MagickRealType) height,MagickTrue,exception);
cristyecc2c142010-01-17 22:25:46 +00001987 if (status == MagickFalse)
1988 {
1989 rotate_image=DestroyImage(rotate_image);
1990 return((Image *) NULL);
1991 }
cristy3ed852e2009-09-05 21:47:34 +00001992 rotate_image->compose=image->compose;
1993 rotate_image->page.width=0;
1994 rotate_image->page.height=0;
1995 return(rotate_image);
1996}
1997
1998/*
1999%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2000% %
2001% %
2002% %
2003% S h e a r I m a g e %
2004% %
2005% %
2006% %
2007%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2008%
2009% ShearImage() creates a new image that is a shear_image copy of an existing
cristycee97112010-05-28 00:44:52 +00002010% one. Shearing slides one edge of an image along the X or Y axis, creating
2011% a parallelogram. An X direction shear slides an edge along the X axis,
2012% while a Y direction shear slides an edge along the Y axis. The amount of
cristy3ed852e2009-09-05 21:47:34 +00002013% the shear is controlled by a shear angle. For X direction shears, x_shear
2014% is measured relative to the Y axis, and similarly, for Y direction shears
2015% y_shear is measured relative to the X axis. Empty triangles left over from
2016% shearing the image are filled with the background color defined by member
2017% 'background_color' of the image.. ShearImage() allocates the memory
2018% necessary for the new Image structure and returns a pointer to the new image.
2019%
2020% ShearImage() is based on the paper "A Fast Algorithm for General Raster
2021% Rotatation" by Alan W. Paeth.
2022%
2023% The format of the ShearImage method is:
2024%
2025% Image *ShearImage(const Image *image,const double x_shear,
2026% const double y_shear,ExceptionInfo *exception)
2027%
2028% A description of each parameter follows.
2029%
2030% o image: the image.
2031%
2032% o x_shear, y_shear: Specifies the number of degrees to shear the image.
2033%
2034% o exception: return any errors or warnings in this structure.
2035%
2036*/
2037MagickExport Image *ShearImage(const Image *image,const double x_shear,
2038 const double y_shear,ExceptionInfo *exception)
2039{
2040 Image
2041 *integral_image,
2042 *shear_image;
2043
cristybb503372010-05-27 20:51:26 +00002044 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002045 x_offset,
2046 y_offset;
2047
cristyecc2c142010-01-17 22:25:46 +00002048 MagickBooleanType
2049 status;
2050
cristy3ed852e2009-09-05 21:47:34 +00002051 PointInfo
2052 shear;
2053
2054 RectangleInfo
2055 border_info;
2056
cristybb503372010-05-27 20:51:26 +00002057 size_t
cristy3ed852e2009-09-05 21:47:34 +00002058 y_width;
2059
2060 assert(image != (Image *) NULL);
2061 assert(image->signature == MagickSignature);
2062 if (image->debug != MagickFalse)
2063 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2064 assert(exception != (ExceptionInfo *) NULL);
2065 assert(exception->signature == MagickSignature);
2066 if ((x_shear != 0.0) && (fmod(x_shear,90.0) == 0.0))
2067 ThrowImageException(ImageError,"AngleIsDiscontinuous");
2068 if ((y_shear != 0.0) && (fmod(y_shear,90.0) == 0.0))
2069 ThrowImageException(ImageError,"AngleIsDiscontinuous");
2070 /*
2071 Initialize shear angle.
2072 */
2073 integral_image=CloneImage(image,0,0,MagickTrue,exception);
2074 if (integral_image == (Image *) NULL)
2075 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2076 shear.x=(-tan(DegreesToRadians(fmod(x_shear,360.0))));
2077 shear.y=tan(DegreesToRadians(fmod(y_shear,360.0)));
2078 if ((shear.x == 0.0) && (shear.y == 0.0))
2079 return(integral_image);
2080 if (SetImageStorageClass(integral_image,DirectClass) == MagickFalse)
2081 {
2082 InheritException(exception,&integral_image->exception);
2083 integral_image=DestroyImage(integral_image);
2084 return(integral_image);
2085 }
2086 if (integral_image->matte == MagickFalse)
2087 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel);
2088 /*
2089 Compute image size.
2090 */
cristybb503372010-05-27 20:51:26 +00002091 y_width=image->columns+(ssize_t) floor(fabs(shear.x)*image->rows+0.5);
cristycee97112010-05-28 00:44:52 +00002092 x_offset=(ssize_t) ceil((double) image->columns+((fabs(shear.x)*image->rows)-
cristy06609ee2010-03-17 20:21:27 +00002093 image->columns)/2.0-0.5);
cristycee97112010-05-28 00:44:52 +00002094 y_offset=(ssize_t) ceil((double) image->rows+((fabs(shear.y)*y_width)-
2095 image->rows)/2.0-0.5);
cristy3ed852e2009-09-05 21:47:34 +00002096 /*
2097 Surround image with border.
2098 */
2099 integral_image->border_color=integral_image->background_color;
2100 integral_image->compose=CopyCompositeOp;
cristybb503372010-05-27 20:51:26 +00002101 border_info.width=(size_t) x_offset;
2102 border_info.height=(size_t) y_offset;
cristy3ed852e2009-09-05 21:47:34 +00002103 shear_image=BorderImage(integral_image,&border_info,exception);
cristyecc2c142010-01-17 22:25:46 +00002104 integral_image=DestroyImage(integral_image);
cristy3ed852e2009-09-05 21:47:34 +00002105 if (shear_image == (Image *) NULL)
2106 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +00002107 /*
2108 Shear the image.
2109 */
2110 if (shear_image->matte == MagickFalse)
2111 (void) SetImageAlphaChannel(shear_image,OpaqueAlphaChannel);
cristyecc2c142010-01-17 22:25:46 +00002112 status=XShearImage(shear_image,shear.x,image->columns,image->rows,x_offset,
cristyeaedf062010-05-29 22:36:02 +00002113 (ssize_t) (shear_image->rows-image->rows)/2,exception);
cristyecc2c142010-01-17 22:25:46 +00002114 if (status == MagickFalse)
2115 {
2116 shear_image=DestroyImage(shear_image);
2117 return((Image *) NULL);
2118 }
cristyeaedf062010-05-29 22:36:02 +00002119 status=YShearImage(shear_image,shear.y,y_width,image->rows,(ssize_t)
2120 (shear_image->columns-y_width)/2,y_offset,exception);
cristyecc2c142010-01-17 22:25:46 +00002121 if (status == MagickFalse)
2122 {
2123 shear_image=DestroyImage(shear_image);
2124 return((Image *) NULL);
2125 }
2126 status=CropToFitImage(&shear_image,shear.x,shear.y,(MagickRealType)
2127 image->columns,(MagickRealType) image->rows,MagickFalse,exception);
2128 if (status == MagickFalse)
2129 {
2130 shear_image=DestroyImage(shear_image);
2131 return((Image *) NULL);
2132 }
cristy3ed852e2009-09-05 21:47:34 +00002133 shear_image->compose=image->compose;
2134 shear_image->page.width=0;
2135 shear_image->page.height=0;
2136 return(shear_image);
2137}