blob: 1d76dd109d3080cc7cefb9d99989ec02d1a03a6b [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% %
cristy45ef08f2012-12-07 13:13:34 +000020% Copyright 1999-2013 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%
cristyc7c81142011-11-07 12:14:23 +000036% The XShearImage() and YShearImage() methods are based on the paper "A Fast
cristy7f81c002011-11-07 01:08:58 +000037% Algorithm for General Raster Rotatation" by Alan W. Paeth, Graphics
cristyc7c81142011-11-07 12:14:23 +000038% Interface '86 (Vancouver). ShearRotateImage() 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.
cristy3ed852e2009-09-05 21:47:34 +000041%
42*/
43
44/*
45 Include declarations.
46*/
cristy4c08aed2011-07-01 19:47:50 +000047#include "MagickCore/studio.h"
48#include "MagickCore/artifact.h"
49#include "MagickCore/attribute.h"
50#include "MagickCore/blob-private.h"
51#include "MagickCore/cache-private.h"
52#include "MagickCore/color-private.h"
53#include "MagickCore/colorspace-private.h"
54#include "MagickCore/composite.h"
55#include "MagickCore/composite-private.h"
56#include "MagickCore/decorate.h"
57#include "MagickCore/distort.h"
58#include "MagickCore/draw.h"
59#include "MagickCore/exception.h"
60#include "MagickCore/exception-private.h"
61#include "MagickCore/gem.h"
62#include "MagickCore/geometry.h"
63#include "MagickCore/image.h"
64#include "MagickCore/image-private.h"
65#include "MagickCore/memory_.h"
66#include "MagickCore/list.h"
67#include "MagickCore/monitor.h"
68#include "MagickCore/monitor-private.h"
cristy2c5fc272012-02-22 01:27:46 +000069#include "MagickCore/nt-base-private.h"
cristy4c08aed2011-07-01 19:47:50 +000070#include "MagickCore/pixel-accessor.h"
71#include "MagickCore/quantum.h"
72#include "MagickCore/resource_.h"
73#include "MagickCore/shear.h"
74#include "MagickCore/statistic.h"
75#include "MagickCore/string_.h"
76#include "MagickCore/string-private.h"
77#include "MagickCore/thread-private.h"
78#include "MagickCore/threshold.h"
79#include "MagickCore/transform.h"
cristy3ed852e2009-09-05 21:47:34 +000080
81/*
82%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
83% %
84% %
85% %
cristy3ed852e2009-09-05 21:47:34 +000086+ C r o p T o F i t I m a g e %
87% %
88% %
89% %
90%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
91%
92% CropToFitImage() crops the sheared image as determined by the bounding box
93% as defined by width and height and shearing angles.
94%
95% The format of the CropToFitImage method is:
96%
cristyecc2c142010-01-17 22:25:46 +000097% MagickBooleanType CropToFitImage(Image **image,
cristya19f1d72012-08-07 18:24:38 +000098% const double x_shear,const double x_shear,
99% const double width,const double height,
cristyecc2c142010-01-17 22:25:46 +0000100% const MagickBooleanType rotate,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000101%
102% A description of each parameter follows.
103%
104% o image: the image.
105%
106% o x_shear, y_shear, width, height: Defines a region of the image to crop.
107%
108% o exception: return any errors or warnings in this structure.
109%
110*/
cristyecc2c142010-01-17 22:25:46 +0000111static MagickBooleanType CropToFitImage(Image **image,
cristya19f1d72012-08-07 18:24:38 +0000112 const double x_shear,const double y_shear,
113 const double width,const double height,
cristyecc2c142010-01-17 22:25:46 +0000114 const MagickBooleanType rotate,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000115{
116 Image
117 *crop_image;
118
119 PointInfo
120 extent[4],
121 min,
122 max;
123
124 RectangleInfo
125 geometry,
126 page;
127
cristybb503372010-05-27 20:51:26 +0000128 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000129 i;
130
131 /*
132 Calculate the rotated image size.
133 */
134 extent[0].x=(double) (-width/2.0);
135 extent[0].y=(double) (-height/2.0);
136 extent[1].x=(double) width/2.0;
137 extent[1].y=(double) (-height/2.0);
138 extent[2].x=(double) (-width/2.0);
139 extent[2].y=(double) height/2.0;
140 extent[3].x=(double) width/2.0;
141 extent[3].y=(double) height/2.0;
142 for (i=0; i < 4; i++)
143 {
144 extent[i].x+=x_shear*extent[i].y;
145 extent[i].y+=y_shear*extent[i].x;
146 if (rotate != MagickFalse)
147 extent[i].x+=x_shear*extent[i].y;
148 extent[i].x+=(double) (*image)->columns/2.0;
149 extent[i].y+=(double) (*image)->rows/2.0;
150 }
151 min=extent[0];
152 max=extent[0];
153 for (i=1; i < 4; i++)
154 {
155 if (min.x > extent[i].x)
156 min.x=extent[i].x;
157 if (min.y > extent[i].y)
158 min.y=extent[i].y;
159 if (max.x < extent[i].x)
160 max.x=extent[i].x;
161 if (max.y < extent[i].y)
162 max.y=extent[i].y;
163 }
cristybb503372010-05-27 20:51:26 +0000164 geometry.x=(ssize_t) ceil(min.x-0.5);
165 geometry.y=(ssize_t) ceil(min.y-0.5);
166 geometry.width=(size_t) floor(max.x-min.x+0.5);
167 geometry.height=(size_t) floor(max.y-min.y+0.5);
cristy3ed852e2009-09-05 21:47:34 +0000168 page=(*image)->page;
169 (void) ParseAbsoluteGeometry("0x0+0+0",&(*image)->page);
170 crop_image=CropImage(*image,&geometry,exception);
cristyecc2c142010-01-17 22:25:46 +0000171 if (crop_image == (Image *) NULL)
172 return(MagickFalse);
173 crop_image->page=page;
174 *image=DestroyImage(*image);
175 *image=crop_image;
176 return(MagickTrue);
cristy3ed852e2009-09-05 21:47:34 +0000177}
178
179/*
180%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
181% %
182% %
183% %
184% D e s k e w I m a g e %
185% %
186% %
187% %
188%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
189%
190% DeskewImage() removes skew from the image. Skew is an artifact that
191% occurs in scanned images because of the camera being misaligned,
192% imperfections in the scanning or surface, or simply because the paper was
193% not placed completely flat when scanned.
194%
anthony8a95e802013-04-10 05:31:57 +0000195% The result will be auto-croped if the artifact "deskew:auto-crop" is
196% defined, while the amount the image is to be deskewed, in degrees is also
197% saved as the artifact "deskew:angle".
198%
anthony76c9dbf2013-04-10 05:56:31 +0000199% If the artifact "deskew:auto-crop" is defined with 'offset limits' used to
200% determine the images background color, the image will be automatically
201% cropped of the excess background.
anthony8a95e802013-04-10 05:31:57 +0000202%
cristy3ed852e2009-09-05 21:47:34 +0000203% The format of the DeskewImage method is:
204%
205% Image *DeskewImage(const Image *image,const double threshold,
206% ExceptionInfo *exception)
207%
208% A description of each parameter follows:
209%
210% o image: the image.
211%
212% o threshold: separate background from foreground.
213%
214% o exception: return any errors or warnings in this structure.
215%
216*/
217
218typedef struct _RadonInfo
219{
220 CacheType
221 type;
222
cristybb503372010-05-27 20:51:26 +0000223 size_t
cristy3ed852e2009-09-05 21:47:34 +0000224 width,
225 height;
226
227 MagickSizeType
228 length;
229
230 MagickBooleanType
231 mapped;
232
233 char
234 path[MaxTextExtent];
235
236 int
237 file;
238
239 unsigned short
240 *cells;
241} RadonInfo;
242
243static RadonInfo *DestroyRadonInfo(RadonInfo *radon_info)
244{
245 assert(radon_info != (RadonInfo *) NULL);
246 switch (radon_info->type)
247 {
248 case MemoryCache:
249 {
250 if (radon_info->mapped == MagickFalse)
251 radon_info->cells=(unsigned short *) RelinquishMagickMemory(
252 radon_info->cells);
253 else
254 radon_info->cells=(unsigned short *) UnmapBlob(radon_info->cells,
255 (size_t) radon_info->length);
256 RelinquishMagickResource(MemoryResource,radon_info->length);
257 break;
258 }
259 case MapCache:
260 {
261 radon_info->cells=(unsigned short *) UnmapBlob(radon_info->cells,(size_t)
262 radon_info->length);
263 RelinquishMagickResource(MapResource,radon_info->length);
264 }
265 case DiskCache:
266 {
267 if (radon_info->file != -1)
268 (void) close(radon_info->file);
269 (void) RelinquishUniqueFileResource(radon_info->path);
270 RelinquishMagickResource(DiskResource,radon_info->length);
271 break;
272 }
273 default:
274 break;
275 }
276 return((RadonInfo *) RelinquishMagickMemory(radon_info));
277}
278
279static MagickBooleanType ResetRadonCells(RadonInfo *radon_info)
280{
cristybb503372010-05-27 20:51:26 +0000281 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000282 x;
283
284 ssize_t
cristyd40deb62011-03-09 00:52:27 +0000285 count,
286 y;
cristy3ed852e2009-09-05 21:47:34 +0000287
288 unsigned short
289 value;
290
291 if (radon_info->type != DiskCache)
292 {
293 (void) ResetMagickMemory(radon_info->cells,0,(size_t) radon_info->length);
294 return(MagickTrue);
295 }
296 value=0;
cristy7f317702011-02-18 20:40:28 +0000297 (void) lseek(radon_info->file,0,SEEK_SET);
cristybb503372010-05-27 20:51:26 +0000298 for (y=0; y < (ssize_t) radon_info->height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000299 {
cristybb503372010-05-27 20:51:26 +0000300 for (x=0; x < (ssize_t) radon_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000301 {
302 count=write(radon_info->file,&value,sizeof(*radon_info->cells));
303 if (count != (ssize_t) sizeof(*radon_info->cells))
304 break;
305 }
cristybb503372010-05-27 20:51:26 +0000306 if (x < (ssize_t) radon_info->width)
cristy3ed852e2009-09-05 21:47:34 +0000307 break;
308 }
cristybb503372010-05-27 20:51:26 +0000309 return(y < (ssize_t) radon_info->height ? MagickFalse : MagickTrue);
cristy3ed852e2009-09-05 21:47:34 +0000310}
311
cristybb503372010-05-27 20:51:26 +0000312static RadonInfo *AcquireRadonInfo(const Image *image,const size_t width,
313 const size_t height,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000314{
315 MagickBooleanType
316 status;
317
318 RadonInfo
319 *radon_info;
320
cristy73bd4a52010-10-05 11:24:23 +0000321 radon_info=(RadonInfo *) AcquireMagickMemory(sizeof(*radon_info));
cristy3ed852e2009-09-05 21:47:34 +0000322 if (radon_info == (RadonInfo *) NULL)
323 return((RadonInfo *) NULL);
324 (void) ResetMagickMemory(radon_info,0,sizeof(*radon_info));
325 radon_info->width=width;
326 radon_info->height=height;
327 radon_info->length=(MagickSizeType) width*height*sizeof(*radon_info->cells);
328 radon_info->type=MemoryCache;
329 status=AcquireMagickResource(AreaResource,radon_info->length);
330 if ((status != MagickFalse) &&
331 (radon_info->length == (MagickSizeType) ((size_t) radon_info->length)))
332 {
333 status=AcquireMagickResource(MemoryResource,radon_info->length);
334 if (status != MagickFalse)
335 {
336 radon_info->mapped=MagickFalse;
337 radon_info->cells=(unsigned short *) AcquireMagickMemory((size_t)
338 radon_info->length);
339 if (radon_info->cells == (unsigned short *) NULL)
340 {
341 radon_info->mapped=MagickTrue;
342 radon_info->cells=(unsigned short *) MapBlob(-1,IOMode,0,(size_t)
343 radon_info->length);
344 }
345 if (radon_info->cells == (unsigned short *) NULL)
346 RelinquishMagickResource(MemoryResource,radon_info->length);
347 }
348 }
349 radon_info->file=(-1);
350 if (radon_info->cells == (unsigned short *) NULL)
351 {
352 status=AcquireMagickResource(DiskResource,radon_info->length);
353 if (status == MagickFalse)
354 {
355 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
cristyefe601c2013-01-05 17:51:12 +0000356 "CacheResourcesExhausted","`%s'",image->filename);
cristy3ed852e2009-09-05 21:47:34 +0000357 return(DestroyRadonInfo(radon_info));
358 }
359 radon_info->type=DiskCache;
360 (void) AcquireMagickResource(MemoryResource,radon_info->length);
361 radon_info->file=AcquireUniqueFileResource(radon_info->path);
362 if (radon_info->file == -1)
363 return(DestroyRadonInfo(radon_info));
364 status=AcquireMagickResource(MapResource,radon_info->length);
365 if (status != MagickFalse)
366 {
367 status=ResetRadonCells(radon_info);
368 if (status != MagickFalse)
369 {
370 radon_info->cells=(unsigned short *) MapBlob(radon_info->file,
371 IOMode,0,(size_t) radon_info->length);
372 if (radon_info->cells != (unsigned short *) NULL)
373 radon_info->type=MapCache;
374 else
375 RelinquishMagickResource(MapResource,radon_info->length);
376 }
377 }
378 }
379 return(radon_info);
380}
381
382static inline size_t MagickMin(const size_t x,const size_t y)
383{
384 if (x < y)
385 return(x);
386 return(y);
387}
388
389static inline ssize_t ReadRadonCell(const RadonInfo *radon_info,
cristy2c38b272011-02-18 23:25:33 +0000390 const MagickOffsetType offset,const size_t length,unsigned char *buffer)
cristy3ed852e2009-09-05 21:47:34 +0000391{
392 register ssize_t
393 i;
394
395 ssize_t
396 count;
397
398#if !defined(MAGICKCORE_HAVE_PPREAD)
cristyb5d5f722009-11-04 03:03:49 +0000399#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000400 #pragma omp critical (MagickCore_ReadRadonCell)
401#endif
402 {
403 i=(-1);
cristy7f317702011-02-18 20:40:28 +0000404 if (lseek(radon_info->file,offset,SEEK_SET) >= 0)
cristy3ed852e2009-09-05 21:47:34 +0000405 {
406#endif
407 count=0;
408 for (i=0; i < (ssize_t) length; i+=count)
409 {
410#if !defined(MAGICKCORE_HAVE_PPREAD)
411 count=read(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
412 SSIZE_MAX));
413#else
414 count=pread(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
cristy2c38b272011-02-18 23:25:33 +0000415 SSIZE_MAX),offset+i);
cristy3ed852e2009-09-05 21:47:34 +0000416#endif
417 if (count > 0)
418 continue;
419 count=0;
420 if (errno != EINTR)
421 {
422 i=(-1);
423 break;
424 }
425 }
426#if !defined(MAGICKCORE_HAVE_PPREAD)
427 }
428 }
429#endif
430 return(i);
431}
432
433static inline ssize_t WriteRadonCell(const RadonInfo *radon_info,
cristy2c38b272011-02-18 23:25:33 +0000434 const MagickOffsetType offset,const size_t length,const unsigned char *buffer)
cristy3ed852e2009-09-05 21:47:34 +0000435{
436 register ssize_t
437 i;
438
439 ssize_t
440 count;
441
442#if !defined(MAGICKCORE_HAVE_PWRITE)
cristyb5d5f722009-11-04 03:03:49 +0000443#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000444 #pragma omp critical (MagickCore_WriteRadonCell)
445#endif
446 {
cristy7f317702011-02-18 20:40:28 +0000447 if (lseek(radon_info->file,offset,SEEK_SET) >= 0)
cristy3ed852e2009-09-05 21:47:34 +0000448 {
449#endif
450 count=0;
451 for (i=0; i < (ssize_t) length; i+=count)
452 {
453#if !defined(MAGICKCORE_HAVE_PWRITE)
454 count=write(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
455 SSIZE_MAX));
456#else
457 count=pwrite(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
cristy2c38b272011-02-18 23:25:33 +0000458 SSIZE_MAX),offset+i);
cristy3ed852e2009-09-05 21:47:34 +0000459#endif
460 if (count > 0)
461 continue;
462 count=0;
463 if (errno != EINTR)
464 {
465 i=(-1);
466 break;
467 }
468 }
469#if !defined(MAGICKCORE_HAVE_PWRITE)
470 }
471 }
472#endif
473 return(i);
474}
475
476static inline unsigned short GetRadonCell(const RadonInfo *radon_info,
cristybb503372010-05-27 20:51:26 +0000477 const ssize_t x,const ssize_t y)
cristy3ed852e2009-09-05 21:47:34 +0000478{
cristy2c38b272011-02-18 23:25:33 +0000479 MagickOffsetType
cristy3ed852e2009-09-05 21:47:34 +0000480 i;
481
482 unsigned short
483 value;
484
cristy2c38b272011-02-18 23:25:33 +0000485 i=(MagickOffsetType) radon_info->height*x+y;
cristy3ed852e2009-09-05 21:47:34 +0000486 if ((i < 0) ||
487 ((MagickSizeType) (i*sizeof(*radon_info->cells)) >= radon_info->length))
488 return(0);
489 if (radon_info->type != DiskCache)
490 return(radon_info->cells[i]);
491 value=0;
492 (void) ReadRadonCell(radon_info,i*sizeof(*radon_info->cells),
493 sizeof(*radon_info->cells),(unsigned char *) &value);
494 return(value);
495}
496
497static inline MagickBooleanType SetRadonCell(const RadonInfo *radon_info,
cristybb503372010-05-27 20:51:26 +0000498 const ssize_t x,const ssize_t y,const unsigned short value)
cristy3ed852e2009-09-05 21:47:34 +0000499{
cristy2c38b272011-02-18 23:25:33 +0000500 MagickOffsetType
cristy3ed852e2009-09-05 21:47:34 +0000501 i;
502
503 ssize_t
504 count;
505
cristy2c38b272011-02-18 23:25:33 +0000506 i=(MagickOffsetType) radon_info->height*x+y;
cristy3ed852e2009-09-05 21:47:34 +0000507 if ((i < 0) ||
508 ((MagickSizeType) (i*sizeof(*radon_info->cells)) >= radon_info->length))
509 return(MagickFalse);
510 if (radon_info->type != DiskCache)
511 {
512 radon_info->cells[i]=value;
513 return(MagickTrue);
514 }
515 count=WriteRadonCell(radon_info,i*sizeof(*radon_info->cells),
cristy44807bb2009-09-19 18:28:06 +0000516 sizeof(*radon_info->cells),(const unsigned char *) &value);
cristy3ed852e2009-09-05 21:47:34 +0000517 if (count != (ssize_t) sizeof(*radon_info->cells))
518 return(MagickFalse);
519 return(MagickTrue);
520}
521
cristy4ee2b0c2012-05-15 00:30:35 +0000522static void RadonProjection(const Image *image,RadonInfo *source_cells,
cristybb503372010-05-27 20:51:26 +0000523 RadonInfo *destination_cells,const ssize_t sign,size_t *projection)
cristy3ed852e2009-09-05 21:47:34 +0000524{
525 RadonInfo
526 *swap;
527
cristybb503372010-05-27 20:51:26 +0000528 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000529 x;
530
531 register RadonInfo
532 *p,
533 *q;
534
cristybb503372010-05-27 20:51:26 +0000535 size_t
cristy3ed852e2009-09-05 21:47:34 +0000536 step;
537
538 p=source_cells;
539 q=destination_cells;
540 for (step=1; step < p->width; step*=2)
541 {
cristyeaedf062010-05-29 22:36:02 +0000542 for (x=0; x < (ssize_t) p->width; x+=2*(ssize_t) step)
cristy3ed852e2009-09-05 21:47:34 +0000543 {
cristybb503372010-05-27 20:51:26 +0000544 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000545 i;
546
cristyd40deb62011-03-09 00:52:27 +0000547 ssize_t
548 y;
549
cristy3ed852e2009-09-05 21:47:34 +0000550 unsigned short
551 cell;
552
cristybb503372010-05-27 20:51:26 +0000553 for (i=0; i < (ssize_t) step; i++)
cristy3ed852e2009-09-05 21:47:34 +0000554 {
cristybb503372010-05-27 20:51:26 +0000555 for (y=0; y < (ssize_t) (p->height-i-1); y++)
cristy3ed852e2009-09-05 21:47:34 +0000556 {
557 cell=GetRadonCell(p,x+i,y);
cristyeaedf062010-05-29 22:36:02 +0000558 (void) SetRadonCell(q,x+2*i,y,cell+GetRadonCell(p,x+i+(ssize_t)
559 step,y+i));
560 (void) SetRadonCell(q,x+2*i+1,y,cell+GetRadonCell(p,x+i+(ssize_t)
561 step,y+i+1));
cristy3ed852e2009-09-05 21:47:34 +0000562 }
cristybb503372010-05-27 20:51:26 +0000563 for ( ; y < (ssize_t) (p->height-i); y++)
cristy3ed852e2009-09-05 21:47:34 +0000564 {
565 cell=GetRadonCell(p,x+i,y);
cristyeaedf062010-05-29 22:36:02 +0000566 (void) SetRadonCell(q,x+2*i,y,cell+GetRadonCell(p,x+i+(ssize_t) step,
567 y+i));
cristy3ed852e2009-09-05 21:47:34 +0000568 (void) SetRadonCell(q,x+2*i+1,y,cell);
569 }
cristybb503372010-05-27 20:51:26 +0000570 for ( ; y < (ssize_t) p->height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000571 {
572 cell=GetRadonCell(p,x+i,y);
573 (void) SetRadonCell(q,x+2*i,y,cell);
574 (void) SetRadonCell(q,x+2*i+1,y,cell);
575 }
576 }
577 }
578 swap=p;
579 p=q;
580 q=swap;
581 }
cristyb5d5f722009-11-04 03:03:49 +0000582#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000583 #pragma omp parallel for schedule(static,4) \
cristycb7dfcc2013-01-06 18:34:59 +0000584 magick_threads(image,image,1,1)
cristy3ed852e2009-09-05 21:47:34 +0000585#endif
cristybb503372010-05-27 20:51:26 +0000586 for (x=0; x < (ssize_t) p->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000587 {
cristybb503372010-05-27 20:51:26 +0000588 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000589 y;
590
cristybb503372010-05-27 20:51:26 +0000591 size_t
cristy3ed852e2009-09-05 21:47:34 +0000592 sum;
593
594 sum=0;
cristybb503372010-05-27 20:51:26 +0000595 for (y=0; y < (ssize_t) (p->height-1); y++)
cristy3ed852e2009-09-05 21:47:34 +0000596 {
cristybb503372010-05-27 20:51:26 +0000597 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000598 delta;
599
cristybb503372010-05-27 20:51:26 +0000600 delta=GetRadonCell(p,x,y)-(ssize_t) GetRadonCell(p,x,y+1);
cristy3ed852e2009-09-05 21:47:34 +0000601 sum+=delta*delta;
602 }
603 projection[p->width+sign*x-1]=sum;
604 }
605}
606
607static MagickBooleanType RadonTransform(const Image *image,
cristybb503372010-05-27 20:51:26 +0000608 const double threshold,size_t *projection,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000609{
610 CacheView
611 *image_view;
612
cristy3ed852e2009-09-05 21:47:34 +0000613 MagickBooleanType
614 status;
615
616 RadonInfo
617 *destination_cells,
618 *source_cells;
619
cristybb503372010-05-27 20:51:26 +0000620 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000621 i;
622
cristybb503372010-05-27 20:51:26 +0000623 size_t
cristy3ed852e2009-09-05 21:47:34 +0000624 count,
625 width;
626
cristyd40deb62011-03-09 00:52:27 +0000627 ssize_t
628 y;
629
630 unsigned char
631 byte;
632
cristy3ed852e2009-09-05 21:47:34 +0000633 unsigned short
634 bits[256];
635
636 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
637 source_cells=AcquireRadonInfo(image,width,image->rows,exception);
638 destination_cells=AcquireRadonInfo(image,width,image->rows,exception);
639 if ((source_cells == (RadonInfo *) NULL) ||
640 (destination_cells == (RadonInfo *) NULL))
641 {
642 if (destination_cells != (RadonInfo *) NULL)
643 destination_cells=DestroyRadonInfo(destination_cells);
644 if (source_cells != (RadonInfo *) NULL)
645 source_cells=DestroyRadonInfo(source_cells);
646 return(MagickFalse);
647 }
648 if (ResetRadonCells(source_cells) == MagickFalse)
649 {
650 destination_cells=DestroyRadonInfo(destination_cells);
651 source_cells=DestroyRadonInfo(source_cells);
652 return(MagickFalse);
653 }
654 for (i=0; i < 256; i++)
655 {
656 byte=(unsigned char) i;
657 for (count=0; byte != 0; byte>>=1)
658 count+=byte & 0x01;
659 bits[i]=(unsigned short) count;
660 }
661 status=MagickTrue;
cristy46ff2672012-12-14 15:32:26 +0000662 image_view=AcquireVirtualCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +0000663#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000664 #pragma omp parallel for schedule(static,4) shared(status) \
cristy5e6b2592012-12-19 14:08:11 +0000665 magick_threads(image,image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +0000666#endif
cristybb503372010-05-27 20:51:26 +0000667 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000668 {
cristy4c08aed2011-07-01 19:47:50 +0000669 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000670 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000671
cristybb503372010-05-27 20:51:26 +0000672 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000673 i,
674 x;
675
cristybb503372010-05-27 20:51:26 +0000676 size_t
cristy3ed852e2009-09-05 21:47:34 +0000677 bit,
678 byte;
679
680 if (status == MagickFalse)
681 continue;
682 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000683 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000684 {
685 status=MagickFalse;
686 continue;
687 }
688 bit=0;
689 byte=0;
cristybb503372010-05-27 20:51:26 +0000690 i=(ssize_t) (image->columns+7)/8;
691 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000692 {
693 byte<<=1;
cristyf13c5942012-08-08 23:50:11 +0000694 if (GetPixelIntensity(image,p) < threshold)
cristy3ed852e2009-09-05 21:47:34 +0000695 byte|=0x01;
696 bit++;
697 if (bit == 8)
698 {
699 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
700 bit=0;
701 byte=0;
702 }
cristyed231572011-07-14 02:18:59 +0000703 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000704 }
705 if (bit != 0)
706 {
707 byte<<=(8-bit);
708 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
709 }
710 }
cristy4ee2b0c2012-05-15 00:30:35 +0000711 RadonProjection(image,source_cells,destination_cells,-1,projection);
cristy3ed852e2009-09-05 21:47:34 +0000712 (void) ResetRadonCells(source_cells);
cristyb5d5f722009-11-04 03:03:49 +0000713#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000714 #pragma omp parallel for schedule(static,4) shared(status) \
cristycb7dfcc2013-01-06 18:34:59 +0000715 magick_threads(image,image,1,1)
cristy3ed852e2009-09-05 21:47:34 +0000716#endif
cristybb503372010-05-27 20:51:26 +0000717 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000718 {
cristy4c08aed2011-07-01 19:47:50 +0000719 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000720 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000721
cristybb503372010-05-27 20:51:26 +0000722 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000723 i,
724 x;
725
cristybb503372010-05-27 20:51:26 +0000726 size_t
cristy3ed852e2009-09-05 21:47:34 +0000727 bit,
728 byte;
729
730 if (status == MagickFalse)
731 continue;
732 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000733 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000734 {
735 status=MagickFalse;
736 continue;
737 }
738 bit=0;
739 byte=0;
740 i=0;
cristybb503372010-05-27 20:51:26 +0000741 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000742 {
743 byte<<=1;
cristyf13c5942012-08-08 23:50:11 +0000744 if (GetPixelIntensity(image,p) < threshold)
cristy3ed852e2009-09-05 21:47:34 +0000745 byte|=0x01;
746 bit++;
747 if (bit == 8)
748 {
749 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
750 bit=0;
751 byte=0;
752 }
cristyed231572011-07-14 02:18:59 +0000753 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000754 }
755 if (bit != 0)
756 {
757 byte<<=(8-bit);
758 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
759 }
760 }
cristy4ee2b0c2012-05-15 00:30:35 +0000761 RadonProjection(image,source_cells,destination_cells,1,projection);
cristy3ed852e2009-09-05 21:47:34 +0000762 image_view=DestroyCacheView(image_view);
763 destination_cells=DestroyRadonInfo(destination_cells);
764 source_cells=DestroyRadonInfo(source_cells);
765 return(MagickTrue);
766}
767
cristybb503372010-05-27 20:51:26 +0000768static void GetImageBackgroundColor(Image *image,const ssize_t offset,
cristy3ed852e2009-09-05 21:47:34 +0000769 ExceptionInfo *exception)
770{
771 CacheView
772 *image_view;
773
cristy4c08aed2011-07-01 19:47:50 +0000774 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000775 background;
776
cristya19f1d72012-08-07 18:24:38 +0000777 double
cristy3ed852e2009-09-05 21:47:34 +0000778 count;
779
cristyd40deb62011-03-09 00:52:27 +0000780 ssize_t
781 y;
782
cristy3ed852e2009-09-05 21:47:34 +0000783 /*
784 Compute average background color.
785 */
786 if (offset <= 0)
787 return;
cristy4c08aed2011-07-01 19:47:50 +0000788 GetPixelInfo(image,&background);
cristy3ed852e2009-09-05 21:47:34 +0000789 count=0.0;
cristy46ff2672012-12-14 15:32:26 +0000790 image_view=AcquireVirtualCacheView(image,exception);
cristybb503372010-05-27 20:51:26 +0000791 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000792 {
cristy4c08aed2011-07-01 19:47:50 +0000793 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000794 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000795
cristybb503372010-05-27 20:51:26 +0000796 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000797 x;
798
cristybb503372010-05-27 20:51:26 +0000799 if ((y >= offset) && (y < ((ssize_t) image->rows-offset)))
cristy3ed852e2009-09-05 21:47:34 +0000800 continue;
801 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000802 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000803 continue;
cristybb503372010-05-27 20:51:26 +0000804 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000805 {
cristybb503372010-05-27 20:51:26 +0000806 if ((x >= offset) && (x < ((ssize_t) image->columns-offset)))
cristy3ed852e2009-09-05 21:47:34 +0000807 continue;
cristy4c08aed2011-07-01 19:47:50 +0000808 background.red+=QuantumScale*GetPixelRed(image,p);
809 background.green+=QuantumScale*GetPixelGreen(image,p);
810 background.blue+=QuantumScale*GetPixelBlue(image,p);
cristy26295322011-09-22 00:50:36 +0000811 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
812 background.alpha+=QuantumScale*GetPixelAlpha(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000813 count++;
cristyed231572011-07-14 02:18:59 +0000814 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000815 }
816 }
817 image_view=DestroyCacheView(image_view);
cristy8cd03c32012-07-07 18:57:59 +0000818 image->background_color.red=(double) ClampToQuantum(QuantumRange*
819 background.red/count);
820 image->background_color.green=(double) ClampToQuantum(QuantumRange*
821 background.green/count);
822 image->background_color.blue=(double) ClampToQuantum(QuantumRange*
823 background.blue/count);
cristy26295322011-09-22 00:50:36 +0000824 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy8cd03c32012-07-07 18:57:59 +0000825 image->background_color.alpha=(double) ClampToQuantum(QuantumRange*
826 background.alpha/count);
cristy3ed852e2009-09-05 21:47:34 +0000827}
828
829MagickExport Image *DeskewImage(const Image *image,const double threshold,
830 ExceptionInfo *exception)
831{
832 AffineMatrix
833 affine_matrix;
834
835 const char
836 *artifact;
837
838 double
839 degrees;
840
841 Image
842 *clone_image,
843 *crop_image,
844 *deskew_image,
845 *median_image;
846
cristy3ed852e2009-09-05 21:47:34 +0000847 MagickBooleanType
848 status;
849
850 RectangleInfo
851 geometry;
852
cristybb503372010-05-27 20:51:26 +0000853 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000854 i;
855
cristybb503372010-05-27 20:51:26 +0000856 size_t
cristy3ed852e2009-09-05 21:47:34 +0000857 max_projection,
858 *projection,
859 width;
860
cristyd40deb62011-03-09 00:52:27 +0000861 ssize_t
862 skew;
863
cristy3ed852e2009-09-05 21:47:34 +0000864 /*
865 Compute deskew angle.
866 */
867 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
cristybb503372010-05-27 20:51:26 +0000868 projection=(size_t *) AcquireQuantumMemory((size_t) (2*width-1),
cristy3ed852e2009-09-05 21:47:34 +0000869 sizeof(*projection));
cristybb503372010-05-27 20:51:26 +0000870 if (projection == (size_t *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000871 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
872 status=RadonTransform(image,threshold,projection,exception);
873 if (status == MagickFalse)
874 {
cristybb503372010-05-27 20:51:26 +0000875 projection=(size_t *) RelinquishMagickMemory(projection);
cristy3ed852e2009-09-05 21:47:34 +0000876 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
877 }
878 max_projection=0;
879 skew=0;
cristybb503372010-05-27 20:51:26 +0000880 for (i=0; i < (ssize_t) (2*width-1); i++)
cristy3ed852e2009-09-05 21:47:34 +0000881 {
882 if (projection[i] > max_projection)
883 {
cristybb503372010-05-27 20:51:26 +0000884 skew=i-(ssize_t) width+1;
cristy3ed852e2009-09-05 21:47:34 +0000885 max_projection=projection[i];
886 }
887 }
cristybb503372010-05-27 20:51:26 +0000888 projection=(size_t *) RelinquishMagickMemory(projection);
anthony553bfcc2013-04-10 01:27:43 +0000889 degrees=RadiansToDegrees(-atan((double) skew/width/8));
890 if (image->debug != MagickFalse)
891 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
892 " Deskew angle: %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +0000893 /*
894 Deskew image.
895 */
896 clone_image=CloneImage(image,0,0,MagickTrue,exception);
897 if (clone_image == (Image *) NULL)
898 return((Image *) NULL);
anthony8a95e802013-04-10 05:31:57 +0000899 {
900 char
901 angle[MaxTextExtent];
902
903 (void) FormatLocaleString(angle,MaxTextExtent,"%.20g",degrees);
904 (void) SetImageArtifact(clone_image,"deskew:angle",angle);
905 }
cristy387430f2012-02-07 13:09:46 +0000906 (void) SetImageVirtualPixelMethod(clone_image,BackgroundVirtualPixelMethod,
907 exception);
cristy3ed852e2009-09-05 21:47:34 +0000908 affine_matrix.sx=cos(DegreesToRadians(fmod((double) degrees,360.0)));
909 affine_matrix.rx=sin(DegreesToRadians(fmod((double) degrees,360.0)));
910 affine_matrix.ry=(-sin(DegreesToRadians(fmod((double) degrees,360.0))));
911 affine_matrix.sy=cos(DegreesToRadians(fmod((double) degrees,360.0)));
912 affine_matrix.tx=0.0;
913 affine_matrix.ty=0.0;
914 artifact=GetImageArtifact(image,"deskew:auto-crop");
915 if (artifact == (const char *) NULL)
916 {
917 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
918 clone_image=DestroyImage(clone_image);
919 return(deskew_image);
920 }
921 /*
922 Auto-crop image.
923 */
cristyba978e12010-09-12 20:26:50 +0000924 GetImageBackgroundColor(clone_image,(ssize_t) StringToLong(artifact),
925 exception);
cristy3ed852e2009-09-05 21:47:34 +0000926 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
927 clone_image=DestroyImage(clone_image);
928 if (deskew_image == (Image *) NULL)
929 return((Image *) NULL);
cristy95c38342011-03-18 22:39:51 +0000930 median_image=StatisticImage(deskew_image,MedianStatistic,3,3,exception);
cristy3ed852e2009-09-05 21:47:34 +0000931 if (median_image == (Image *) NULL)
932 {
933 deskew_image=DestroyImage(deskew_image);
934 return((Image *) NULL);
935 }
936 geometry=GetImageBoundingBox(median_image,exception);
937 median_image=DestroyImage(median_image);
938 if (image->debug != MagickFalse)
939 (void) LogMagickEvent(TransformEvent,GetMagickModule()," Deskew geometry: "
cristy6d8abba2010-06-03 01:10:47 +0000940 "%.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
cristye8c25f92010-06-03 00:53:06 +0000941 geometry.height,(double) geometry.x,(double) geometry.y);
cristy3ed852e2009-09-05 21:47:34 +0000942 crop_image=CropImage(deskew_image,&geometry,exception);
943 deskew_image=DestroyImage(deskew_image);
944 return(crop_image);
945}
946
947/*
948%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
949% %
950% %
951% %
cristy987feef2011-11-17 12:24:56 +0000952% I n t e g r a l R o t a t e I m a g e %
cristy3ed852e2009-09-05 21:47:34 +0000953% %
954% %
955% %
956%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
957%
cristy3dfa16c2010-05-17 19:34:48 +0000958% IntegralRotateImage() rotates the image an integral of 90 degrees. It
cristy3ed852e2009-09-05 21:47:34 +0000959% allocates the memory necessary for the new Image structure and returns a
960% pointer to the rotated image.
961%
962% The format of the IntegralRotateImage method is:
963%
cristybb503372010-05-27 20:51:26 +0000964% Image *IntegralRotateImage(const Image *image,size_t rotations,
cristy3ed852e2009-09-05 21:47:34 +0000965% ExceptionInfo *exception)
966%
967% A description of each parameter follows.
968%
969% o image: the image.
970%
971% o rotations: Specifies the number of 90 degree rotations.
972%
973*/
cristy987feef2011-11-17 12:24:56 +0000974MagickExport Image *IntegralRotateImage(const Image *image,size_t rotations,
cristy3ed852e2009-09-05 21:47:34 +0000975 ExceptionInfo *exception)
976{
cristy3ed852e2009-09-05 21:47:34 +0000977#define RotateImageTag "Rotate/Image"
978
979 CacheView
980 *image_view,
981 *rotate_view;
982
983 Image
984 *rotate_image;
985
cristy3ed852e2009-09-05 21:47:34 +0000986 MagickBooleanType
987 status;
988
cristy5f959472010-05-27 22:19:46 +0000989 MagickOffsetType
990 progress;
991
cristy3ed852e2009-09-05 21:47:34 +0000992 RectangleInfo
993 page;
994
cristy5f959472010-05-27 22:19:46 +0000995 ssize_t
996 y;
997
cristy3ed852e2009-09-05 21:47:34 +0000998 /*
999 Initialize rotated image attributes.
1000 */
1001 assert(image != (Image *) NULL);
1002 page=image->page;
1003 rotations%=4;
cristyb32b90a2009-09-07 21:45:48 +00001004 if (rotations == 0)
1005 return(CloneImage(image,0,0,MagickTrue,exception));
cristy3ed852e2009-09-05 21:47:34 +00001006 if ((rotations == 1) || (rotations == 3))
1007 rotate_image=CloneImage(image,image->rows,image->columns,MagickTrue,
1008 exception);
1009 else
1010 rotate_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1011 exception);
1012 if (rotate_image == (Image *) NULL)
1013 return((Image *) NULL);
1014 /*
1015 Integral rotate the image.
1016 */
1017 status=MagickTrue;
1018 progress=0;
cristy46ff2672012-12-14 15:32:26 +00001019 image_view=AcquireVirtualCacheView(image,exception);
1020 rotate_view=AcquireAuthenticCacheView(rotate_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001021 switch (rotations)
1022 {
1023 case 0:
1024 {
1025 /*
1026 Rotate 0 degrees.
1027 */
cristy3ed852e2009-09-05 21:47:34 +00001028 break;
1029 }
1030 case 1:
1031 {
cristybb503372010-05-27 20:51:26 +00001032 size_t
cristyb32b90a2009-09-07 21:45:48 +00001033 tile_height,
1034 tile_width;
1035
cristyd40deb62011-03-09 00:52:27 +00001036 ssize_t
1037 tile_y;
1038
cristy3ed852e2009-09-05 21:47:34 +00001039 /*
1040 Rotate 90 degrees.
1041 */
cristyb32b90a2009-09-07 21:45:48 +00001042 GetPixelCacheTileSize(image,&tile_width,&tile_height);
cristy8b8148b2012-10-07 00:41:15 +00001043 tile_width=image->columns;
cristy26b64912012-12-16 18:20:09 +00001044#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy64c10cd2013-01-06 18:40:43 +00001045 #pragma omp parallel for schedule(static,4) shared(status) \
cristy3e1fa372013-01-06 18:07:11 +00001046 magick_threads(image,image,1,1)
cristy9a5a52f2012-10-09 14:40:31 +00001047#endif
cristyad11aac2011-09-25 21:29:16 +00001048 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
cristy3ed852e2009-09-05 21:47:34 +00001049 {
cristybb503372010-05-27 20:51:26 +00001050 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001051 tile_x;
1052
1053 if (status == MagickFalse)
1054 continue;
cristy26295322011-09-22 00:50:36 +00001055 tile_x=0;
1056 for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
cristy3ed852e2009-09-05 21:47:34 +00001057 {
1058 MagickBooleanType
1059 sync;
1060
cristy4c08aed2011-07-01 19:47:50 +00001061 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001062 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001063
cristy4c08aed2011-07-01 19:47:50 +00001064 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001065 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001066
cristya26211e2011-10-09 01:24:57 +00001067 register ssize_t
1068 y;
1069
cristybb503372010-05-27 20:51:26 +00001070 size_t
cristyb32b90a2009-09-07 21:45:48 +00001071 height,
1072 width;
cristy3ed852e2009-09-05 21:47:34 +00001073
cristyb32b90a2009-09-07 21:45:48 +00001074 width=tile_width;
cristybb503372010-05-27 20:51:26 +00001075 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
cristya45d2ec2011-08-24 13:17:19 +00001076 width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
cristyb32b90a2009-09-07 21:45:48 +00001077 height=tile_height;
cristybb503372010-05-27 20:51:26 +00001078 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
cristya45d2ec2011-08-24 13:17:19 +00001079 height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
cristydaa97692009-09-13 02:10:35 +00001080 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1081 exception);
cristy4c08aed2011-07-01 19:47:50 +00001082 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001083 {
1084 status=MagickFalse;
1085 break;
1086 }
cristybb503372010-05-27 20:51:26 +00001087 for (y=0; y < (ssize_t) width; y++)
cristy3ed852e2009-09-05 21:47:34 +00001088 {
cristy4c08aed2011-07-01 19:47:50 +00001089 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001090 *restrict tile_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001091
cristybb503372010-05-27 20:51:26 +00001092 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001093 x;
1094
cristy27709ef2011-09-18 02:53:53 +00001095 if (status == MagickFalse)
1096 continue;
cristybb503372010-05-27 20:51:26 +00001097 q=QueueCacheViewAuthenticPixels(rotate_view,(ssize_t)
cristy92611572011-07-07 16:02:42 +00001098 (rotate_image->columns-(tile_y+height)),y+tile_x,height,1,
1099 exception);
cristyacd2ed22011-08-30 01:44:23 +00001100 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001101 {
1102 status=MagickFalse;
cristy27709ef2011-09-18 02:53:53 +00001103 continue;
cristy3ed852e2009-09-05 21:47:34 +00001104 }
cristyed231572011-07-14 02:18:59 +00001105 tile_pixels=p+((height-1)*width+y)*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001106 for (x=0; x < (ssize_t) height; x++)
cristy3ed852e2009-09-05 21:47:34 +00001107 {
cristya45d2ec2011-08-24 13:17:19 +00001108 register ssize_t
1109 i;
1110
cristy883fde12013-04-08 00:50:13 +00001111 if (GetPixelReadMask(image,tile_pixels) == 0)
cristy10a6c612012-01-29 21:41:05 +00001112 {
1113 tile_pixels-=width*GetPixelChannels(image);
1114 q+=GetPixelChannels(rotate_image);
1115 continue;
1116 }
cristya45d2ec2011-08-24 13:17:19 +00001117 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1118 {
cristy5a23c552013-02-13 14:34:28 +00001119 PixelChannel channel=GetPixelChannelChannel(image,i);
1120 PixelTrait traits=GetPixelChannelTraits(image,channel);
1121 PixelTrait rotate_traits=GetPixelChannelTraits(rotate_image,
1122 channel);
cristy010d7d12011-08-31 01:02:48 +00001123 if ((traits == UndefinedPixelTrait) ||
1124 (rotate_traits == UndefinedPixelTrait))
cristya45d2ec2011-08-24 13:17:19 +00001125 continue;
cristy0beccfa2011-09-25 20:47:53 +00001126 SetPixelChannel(rotate_image,channel,tile_pixels[i],q);
cristya45d2ec2011-08-24 13:17:19 +00001127 }
cristyed231572011-07-14 02:18:59 +00001128 tile_pixels-=width*GetPixelChannels(image);
1129 q+=GetPixelChannels(rotate_image);
cristy3ed852e2009-09-05 21:47:34 +00001130 }
cristy3ed852e2009-09-05 21:47:34 +00001131 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1132 if (sync == MagickFalse)
1133 status=MagickFalse;
1134 }
1135 }
1136 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1137 {
1138 MagickBooleanType
1139 proceed;
1140
cristy26b64912012-12-16 18:20:09 +00001141#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy9a5a52f2012-10-09 14:40:31 +00001142 #pragma omp critical (MagickCore_IntegralRotateImage)
1143#endif
cristyb32b90a2009-09-07 21:45:48 +00001144 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
cristy3ed852e2009-09-05 21:47:34 +00001145 image->rows);
1146 if (proceed == MagickFalse)
1147 status=MagickFalse;
1148 }
1149 }
cristyecc2c142010-01-17 22:25:46 +00001150 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1151 image->rows-1,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001152 Swap(page.width,page.height);
1153 Swap(page.x,page.y);
1154 if (page.width != 0)
cristybb503372010-05-27 20:51:26 +00001155 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
cristy3ed852e2009-09-05 21:47:34 +00001156 break;
1157 }
1158 case 2:
1159 {
1160 /*
1161 Rotate 180 degrees.
1162 */
cristy5f890612012-12-16 23:34:05 +00001163#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy64c10cd2013-01-06 18:40:43 +00001164 #pragma omp parallel for schedule(static,4) shared(status) \
cristyd6432472013-01-06 16:56:13 +00001165 magick_threads(image,image,1,1)
cristy5f890612012-12-16 23:34:05 +00001166#endif
cristybb503372010-05-27 20:51:26 +00001167 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001168 {
1169 MagickBooleanType
1170 sync;
1171
cristy4c08aed2011-07-01 19:47:50 +00001172 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001173 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001174
cristy4c08aed2011-07-01 19:47:50 +00001175 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001176 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001177
cristy2be50602011-10-09 01:28:13 +00001178 register ssize_t
1179 x;
1180
cristy3ed852e2009-09-05 21:47:34 +00001181 if (status == MagickFalse)
1182 continue;
cristya45d2ec2011-08-24 13:17:19 +00001183 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1184 q=QueueCacheViewAuthenticPixels(rotate_view,0,(ssize_t) (image->rows-y-
1185 1),image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001186 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00001187 {
1188 status=MagickFalse;
1189 continue;
1190 }
cristyed231572011-07-14 02:18:59 +00001191 q+=GetPixelChannels(rotate_image)*image->columns;
cristybb503372010-05-27 20:51:26 +00001192 for (x=0; x < (ssize_t) image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00001193 {
cristya45d2ec2011-08-24 13:17:19 +00001194 register ssize_t
1195 i;
1196
cristyed231572011-07-14 02:18:59 +00001197 q-=GetPixelChannels(rotate_image);
cristy883fde12013-04-08 00:50:13 +00001198 if (GetPixelReadMask(image,p) == 0)
cristy10a6c612012-01-29 21:41:05 +00001199 {
1200 p+=GetPixelChannels(image);
1201 continue;
1202 }
cristya45d2ec2011-08-24 13:17:19 +00001203 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1204 {
cristy5a23c552013-02-13 14:34:28 +00001205 PixelChannel channel=GetPixelChannelChannel(image,i);
1206 PixelTrait traits=GetPixelChannelTraits(image,channel);
1207 PixelTrait rotate_traits=GetPixelChannelTraits(rotate_image,
1208 channel);
cristy010d7d12011-08-31 01:02:48 +00001209 if ((traits == UndefinedPixelTrait) ||
1210 (rotate_traits == UndefinedPixelTrait))
cristya45d2ec2011-08-24 13:17:19 +00001211 continue;
cristy0beccfa2011-09-25 20:47:53 +00001212 SetPixelChannel(rotate_image,channel,p[i],q);
cristya45d2ec2011-08-24 13:17:19 +00001213 }
cristyed231572011-07-14 02:18:59 +00001214 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001215 }
cristy3ed852e2009-09-05 21:47:34 +00001216 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1217 if (sync == MagickFalse)
1218 status=MagickFalse;
1219 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1220 {
1221 MagickBooleanType
1222 proceed;
1223
cristy5f890612012-12-16 23:34:05 +00001224#if defined(MAGICKCORE_OPENMP_SUPPORT)
1225 #pragma omp critical (MagickCore_IntegralRotateImage)
1226#endif
cristy0729beb2011-09-25 23:29:32 +00001227 proceed=SetImageProgress(image,RotateImageTag,progress++,
cristy21e03412011-09-25 22:39:35 +00001228 image->rows);
1229 if (proceed == MagickFalse)
1230 status=MagickFalse;
1231 }
1232 }
1233 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1234 image->rows-1,image->rows);
1235 Swap(page.width,page.height);
1236 Swap(page.x,page.y);
1237 if (page.width != 0)
1238 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1239 break;
1240 }
cristy3ed852e2009-09-05 21:47:34 +00001241 case 3:
1242 {
cristybb503372010-05-27 20:51:26 +00001243 size_t
cristyb32b90a2009-09-07 21:45:48 +00001244 tile_height,
1245 tile_width;
1246
cristyd40deb62011-03-09 00:52:27 +00001247 ssize_t
1248 tile_y;
1249
cristy3ed852e2009-09-05 21:47:34 +00001250 /*
1251 Rotate 270 degrees.
1252 */
cristyb32b90a2009-09-07 21:45:48 +00001253 GetPixelCacheTileSize(image,&tile_width,&tile_height);
cristy8b8148b2012-10-07 00:41:15 +00001254 tile_width=image->columns;
cristy26b64912012-12-16 18:20:09 +00001255#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy64c10cd2013-01-06 18:40:43 +00001256 #pragma omp parallel for schedule(static,4) shared(status) \
cristycb7dfcc2013-01-06 18:34:59 +00001257 magick_threads(image,image,1,1)
cristy9a5a52f2012-10-09 14:40:31 +00001258#endif
cristyad11aac2011-09-25 21:29:16 +00001259 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
cristy3ed852e2009-09-05 21:47:34 +00001260 {
cristybb503372010-05-27 20:51:26 +00001261 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001262 tile_x;
1263
1264 if (status == MagickFalse)
1265 continue;
cristy26295322011-09-22 00:50:36 +00001266 tile_x=0;
1267 for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
cristy3ed852e2009-09-05 21:47:34 +00001268 {
1269 MagickBooleanType
1270 sync;
1271
cristy4c08aed2011-07-01 19:47:50 +00001272 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001273 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001274
cristy4c08aed2011-07-01 19:47:50 +00001275 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001276 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001277
cristy2be50602011-10-09 01:28:13 +00001278 register ssize_t
1279 y;
1280
cristybb503372010-05-27 20:51:26 +00001281 size_t
cristyb32b90a2009-09-07 21:45:48 +00001282 height,
1283 width;
cristy3ed852e2009-09-05 21:47:34 +00001284
cristyb32b90a2009-09-07 21:45:48 +00001285 width=tile_width;
cristybb503372010-05-27 20:51:26 +00001286 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
cristya45d2ec2011-08-24 13:17:19 +00001287 width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
cristyb32b90a2009-09-07 21:45:48 +00001288 height=tile_height;
cristybb503372010-05-27 20:51:26 +00001289 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
cristya45d2ec2011-08-24 13:17:19 +00001290 height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
1291 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1292 exception);
cristy4c08aed2011-07-01 19:47:50 +00001293 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001294 {
1295 status=MagickFalse;
1296 break;
1297 }
cristybb503372010-05-27 20:51:26 +00001298 for (y=0; y < (ssize_t) width; y++)
cristy3ed852e2009-09-05 21:47:34 +00001299 {
cristy4c08aed2011-07-01 19:47:50 +00001300 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001301 *restrict tile_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001302
cristybb503372010-05-27 20:51:26 +00001303 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001304 x;
1305
cristy27709ef2011-09-18 02:53:53 +00001306 if (status == MagickFalse)
1307 continue;
cristya45d2ec2011-08-24 13:17:19 +00001308 q=QueueCacheViewAuthenticPixels(rotate_view,tile_y,(ssize_t) (y+
1309 rotate_image->rows-(tile_x+width)),height,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001310 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001311 {
1312 status=MagickFalse;
cristy27709ef2011-09-18 02:53:53 +00001313 continue;
cristy3ed852e2009-09-05 21:47:34 +00001314 }
cristyed231572011-07-14 02:18:59 +00001315 tile_pixels=p+((width-1)-y)*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001316 for (x=0; x < (ssize_t) height; x++)
cristy3ed852e2009-09-05 21:47:34 +00001317 {
cristya45d2ec2011-08-24 13:17:19 +00001318 register ssize_t
1319 i;
1320
cristy883fde12013-04-08 00:50:13 +00001321 if (GetPixelReadMask(image,tile_pixels) == 0)
cristy10a6c612012-01-29 21:41:05 +00001322 {
1323 tile_pixels+=width*GetPixelChannels(image);
1324 q+=GetPixelChannels(rotate_image);
1325 continue;
1326 }
cristya45d2ec2011-08-24 13:17:19 +00001327 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1328 {
cristy5a23c552013-02-13 14:34:28 +00001329 PixelChannel channel=GetPixelChannelChannel(image,i);
1330 PixelTrait traits=GetPixelChannelTraits(image,channel);
1331 PixelTrait rotate_traits=GetPixelChannelTraits(rotate_image,
1332 channel);
cristy010d7d12011-08-31 01:02:48 +00001333 if ((traits == UndefinedPixelTrait) ||
1334 (rotate_traits == UndefinedPixelTrait))
cristya45d2ec2011-08-24 13:17:19 +00001335 continue;
cristy0beccfa2011-09-25 20:47:53 +00001336 SetPixelChannel(rotate_image,channel,tile_pixels[i],q);
cristya45d2ec2011-08-24 13:17:19 +00001337 }
cristyed231572011-07-14 02:18:59 +00001338 tile_pixels+=width*GetPixelChannels(image);
1339 q+=GetPixelChannels(rotate_image);
cristy3ed852e2009-09-05 21:47:34 +00001340 }
cristy26b64912012-12-16 18:20:09 +00001341#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy9a5a52f2012-10-09 14:40:31 +00001342 #pragma omp critical (MagickCore_IntegralRotateImage)
1343#endif
cristy3ed852e2009-09-05 21:47:34 +00001344 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1345 if (sync == MagickFalse)
1346 status=MagickFalse;
1347 }
1348 }
1349 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1350 {
1351 MagickBooleanType
1352 proceed;
1353
cristy21e03412011-09-25 22:39:35 +00001354 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
1355 image->rows);
1356 if (proceed == MagickFalse)
1357 status=MagickFalse;
1358 }
1359 }
1360 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1361 image->rows-1,image->rows);
1362 Swap(page.width,page.height);
1363 Swap(page.x,page.y);
1364 if (page.width != 0)
1365 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1366 break;
1367 }
cristy3ed852e2009-09-05 21:47:34 +00001368 }
1369 rotate_view=DestroyCacheView(rotate_view);
1370 image_view=DestroyCacheView(image_view);
1371 rotate_image->type=image->type;
1372 rotate_image->page=page;
1373 if (status == MagickFalse)
1374 rotate_image=DestroyImage(rotate_image);
1375 return(rotate_image);
1376}
1377
1378/*
1379%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1380% %
1381% %
1382% %
1383+ X S h e a r I m a g e %
1384% %
1385% %
1386% %
1387%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1388%
1389% XShearImage() shears the image in the X direction with a shear angle of
1390% 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1391% negative angles shear clockwise. Angles are measured relative to a vertical
1392% Y-axis. X shears will widen an image creating 'empty' triangles on the left
1393% and right sides of the source image.
1394%
1395% The format of the XShearImage method is:
1396%
cristya19f1d72012-08-07 18:24:38 +00001397% MagickBooleanType XShearImage(Image *image,const double degrees,
cristybb503372010-05-27 20:51:26 +00001398% const size_t width,const size_t height,
1399% const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001400%
1401% A description of each parameter follows.
1402%
1403% o image: the image.
1404%
cristya19f1d72012-08-07 18:24:38 +00001405% o degrees: A double representing the shearing angle along the X
cristy3ed852e2009-09-05 21:47:34 +00001406% axis.
1407%
1408% o width, height, x_offset, y_offset: Defines a region of the image
1409% to shear.
1410%
cristyecc2c142010-01-17 22:25:46 +00001411% o exception: return any errors or warnings in this structure.
1412%
cristy3ed852e2009-09-05 21:47:34 +00001413*/
cristya19f1d72012-08-07 18:24:38 +00001414static MagickBooleanType XShearImage(Image *image,const double degrees,
cristybb503372010-05-27 20:51:26 +00001415 const size_t width,const size_t height,const ssize_t x_offset,
1416 const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001417{
1418#define XShearImageTag "XShear/Image"
1419
1420 typedef enum
1421 {
1422 LEFT,
1423 RIGHT
1424 } ShearDirection;
1425
1426 CacheView
1427 *image_view;
1428
cristy3ed852e2009-09-05 21:47:34 +00001429 MagickBooleanType
1430 status;
1431
cristy5f959472010-05-27 22:19:46 +00001432 MagickOffsetType
1433 progress;
1434
cristy4c08aed2011-07-01 19:47:50 +00001435 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001436 background;
1437
cristy5f959472010-05-27 22:19:46 +00001438 ssize_t
1439 y;
1440
cristy9d8c8ce2011-10-25 16:13:52 +00001441 /*
1442 X shear image.
1443 */
cristy3ed852e2009-09-05 21:47:34 +00001444 assert(image != (Image *) NULL);
1445 assert(image->signature == MagickSignature);
1446 if (image->debug != MagickFalse)
1447 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3ed852e2009-09-05 21:47:34 +00001448 status=MagickTrue;
cristy9d8c8ce2011-10-25 16:13:52 +00001449 background=image->background_color;
cristy3ed852e2009-09-05 21:47:34 +00001450 progress=0;
cristy46ff2672012-12-14 15:32:26 +00001451 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00001452#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00001453 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00001454 magick_threads(image,image,height,1)
cristy3ed852e2009-09-05 21:47:34 +00001455#endif
cristybb503372010-05-27 20:51:26 +00001456 for (y=0; y < (ssize_t) height; y++)
cristy3ed852e2009-09-05 21:47:34 +00001457 {
cristy4c08aed2011-07-01 19:47:50 +00001458 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001459 pixel,
1460 source,
1461 destination;
1462
cristya19f1d72012-08-07 18:24:38 +00001463 double
cristy3ed852e2009-09-05 21:47:34 +00001464 area,
1465 displacement;
1466
cristy4c08aed2011-07-01 19:47:50 +00001467 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001468 *restrict p,
1469 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001470
cristyd40deb62011-03-09 00:52:27 +00001471 register ssize_t
1472 i;
1473
cristy3ed852e2009-09-05 21:47:34 +00001474 ShearDirection
1475 direction;
1476
cristyd40deb62011-03-09 00:52:27 +00001477 ssize_t
1478 step;
1479
cristy3ed852e2009-09-05 21:47:34 +00001480 if (status == MagickFalse)
1481 continue;
1482 p=GetCacheViewAuthenticPixels(image_view,0,y_offset+y,image->columns,1,
1483 exception);
cristy4c08aed2011-07-01 19:47:50 +00001484 if (p == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001485 {
1486 status=MagickFalse;
1487 continue;
1488 }
cristyed231572011-07-14 02:18:59 +00001489 p+=x_offset*GetPixelChannels(image);
cristya19f1d72012-08-07 18:24:38 +00001490 displacement=degrees*(double) (y-height/2.0);
cristy3ed852e2009-09-05 21:47:34 +00001491 if (displacement == 0.0)
1492 continue;
1493 if (displacement > 0.0)
1494 direction=RIGHT;
1495 else
1496 {
1497 displacement*=(-1.0);
1498 direction=LEFT;
1499 }
cristybb503372010-05-27 20:51:26 +00001500 step=(ssize_t) floor((double) displacement);
cristya19f1d72012-08-07 18:24:38 +00001501 area=(double) (displacement-step);
cristy3ed852e2009-09-05 21:47:34 +00001502 step++;
1503 pixel=background;
cristy4c08aed2011-07-01 19:47:50 +00001504 GetPixelInfo(image,&source);
1505 GetPixelInfo(image,&destination);
cristy3ed852e2009-09-05 21:47:34 +00001506 switch (direction)
1507 {
1508 case LEFT:
1509 {
1510 /*
1511 Transfer pixels left-to-right.
1512 */
1513 if (step > x_offset)
1514 break;
cristyed231572011-07-14 02:18:59 +00001515 q=p-step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001516 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001517 {
1518 if ((x_offset+i) < step)
1519 {
cristyed231572011-07-14 02:18:59 +00001520 p+=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001521 GetPixelInfoPixel(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001522 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001523 continue;
1524 }
cristy803640d2011-11-17 02:11:32 +00001525 GetPixelInfoPixel(image,p,&source);
cristya19f1d72012-08-07 18:24:38 +00001526 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1527 &source,(double) GetPixelAlpha(image,p),area,&destination);
cristy803640d2011-11-17 02:11:32 +00001528 SetPixelInfoPixel(image,&destination,q);
1529 GetPixelInfoPixel(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001530 p+=GetPixelChannels(image);
1531 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001532 }
cristya19f1d72012-08-07 18:24:38 +00001533 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1534 &background,(double) background.alpha,area,&destination);
cristy803640d2011-11-17 02:11:32 +00001535 SetPixelInfoPixel(image,&destination,q);
cristyed231572011-07-14 02:18:59 +00001536 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001537 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001538 {
cristy803640d2011-11-17 02:11:32 +00001539 SetPixelInfoPixel(image,&background,q);
cristyed231572011-07-14 02:18:59 +00001540 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001541 }
cristy3ed852e2009-09-05 21:47:34 +00001542 break;
1543 }
1544 case RIGHT:
1545 {
1546 /*
1547 Transfer pixels right-to-left.
1548 */
cristyed231572011-07-14 02:18:59 +00001549 p+=width*GetPixelChannels(image);
1550 q=p+step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001551 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001552 {
cristyed231572011-07-14 02:18:59 +00001553 p-=GetPixelChannels(image);
1554 q-=GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001555 if ((size_t) (x_offset+width+step-i) >= image->columns)
cristy3ed852e2009-09-05 21:47:34 +00001556 continue;
cristy803640d2011-11-17 02:11:32 +00001557 GetPixelInfoPixel(image,p,&source);
cristya19f1d72012-08-07 18:24:38 +00001558 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1559 &source,(double) GetPixelAlpha(image,p),area,&destination);
cristy803640d2011-11-17 02:11:32 +00001560 SetPixelInfoPixel(image,&destination,q);
1561 GetPixelInfoPixel(image,p,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001562 }
cristya19f1d72012-08-07 18:24:38 +00001563 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1564 &background,(double) background.alpha,area,&destination);
cristyed231572011-07-14 02:18:59 +00001565 q-=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001566 SetPixelInfoPixel(image,&destination,q);
cristy3ed852e2009-09-05 21:47:34 +00001567 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001568 {
cristyed231572011-07-14 02:18:59 +00001569 q-=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001570 SetPixelInfoPixel(image,&background,q);
cristy4c08aed2011-07-01 19:47:50 +00001571 }
cristy3ed852e2009-09-05 21:47:34 +00001572 break;
1573 }
1574 }
1575 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1576 status=MagickFalse;
1577 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1578 {
1579 MagickBooleanType
1580 proceed;
1581
cristyb5d5f722009-11-04 03:03:49 +00001582#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy7b650b52011-10-09 01:13:39 +00001583 #pragma omp critical (MagickCore_XShearImage)
cristy3ed852e2009-09-05 21:47:34 +00001584#endif
1585 proceed=SetImageProgress(image,XShearImageTag,progress++,height);
1586 if (proceed == MagickFalse)
1587 status=MagickFalse;
1588 }
1589 }
1590 image_view=DestroyCacheView(image_view);
1591 return(status);
1592}
1593
1594/*
1595%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1596% %
1597% %
1598% %
1599+ Y S h e a r I m a g e %
1600% %
1601% %
1602% %
1603%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1604%
1605% YShearImage shears the image in the Y direction with a shear angle of
1606% 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1607% negative angles shear clockwise. Angles are measured relative to a
1608% horizontal X-axis. Y shears will increase the height of an image creating
1609% 'empty' triangles on the top and bottom of the source image.
1610%
1611% The format of the YShearImage method is:
1612%
cristya19f1d72012-08-07 18:24:38 +00001613% MagickBooleanType YShearImage(Image *image,const double degrees,
cristybb503372010-05-27 20:51:26 +00001614% const size_t width,const size_t height,
1615% const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001616%
1617% A description of each parameter follows.
1618%
1619% o image: the image.
1620%
cristya19f1d72012-08-07 18:24:38 +00001621% o degrees: A double representing the shearing angle along the Y
cristy3ed852e2009-09-05 21:47:34 +00001622% axis.
1623%
1624% o width, height, x_offset, y_offset: Defines a region of the image
1625% to shear.
1626%
cristyecc2c142010-01-17 22:25:46 +00001627% o exception: return any errors or warnings in this structure.
1628%
cristy3ed852e2009-09-05 21:47:34 +00001629*/
cristya19f1d72012-08-07 18:24:38 +00001630static MagickBooleanType YShearImage(Image *image,const double degrees,
cristybb503372010-05-27 20:51:26 +00001631 const size_t width,const size_t height,const ssize_t x_offset,
1632 const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001633{
1634#define YShearImageTag "YShear/Image"
1635
1636 typedef enum
1637 {
1638 UP,
1639 DOWN
1640 } ShearDirection;
1641
1642 CacheView
1643 *image_view;
1644
cristy3ed852e2009-09-05 21:47:34 +00001645 MagickBooleanType
1646 status;
1647
cristy5f959472010-05-27 22:19:46 +00001648 MagickOffsetType
1649 progress;
1650
cristy4c08aed2011-07-01 19:47:50 +00001651 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001652 background;
1653
cristy5f959472010-05-27 22:19:46 +00001654 ssize_t
1655 x;
1656
cristy9d8c8ce2011-10-25 16:13:52 +00001657 /*
1658 Y Shear image.
1659 */
cristy3ed852e2009-09-05 21:47:34 +00001660 assert(image != (Image *) NULL);
1661 assert(image->signature == MagickSignature);
1662 if (image->debug != MagickFalse)
1663 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3ed852e2009-09-05 21:47:34 +00001664 status=MagickTrue;
1665 progress=0;
cristy9d8c8ce2011-10-25 16:13:52 +00001666 background=image->background_color;
cristy46ff2672012-12-14 15:32:26 +00001667 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00001668#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00001669 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00001670 magick_threads(image,image,width,1)
cristy3ed852e2009-09-05 21:47:34 +00001671#endif
cristybb503372010-05-27 20:51:26 +00001672 for (x=0; x < (ssize_t) width; x++)
cristy3ed852e2009-09-05 21:47:34 +00001673 {
cristybb503372010-05-27 20:51:26 +00001674 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001675 step;
1676
cristya19f1d72012-08-07 18:24:38 +00001677 double
cristy3ed852e2009-09-05 21:47:34 +00001678 area,
1679 displacement;
1680
cristy4c08aed2011-07-01 19:47:50 +00001681 PixelInfo
1682 pixel,
1683 source,
1684 destination;
1685
1686 register Quantum
1687 *restrict p,
1688 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001689
cristybb503372010-05-27 20:51:26 +00001690 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001691 i;
1692
cristy3ed852e2009-09-05 21:47:34 +00001693 ShearDirection
1694 direction;
1695
1696 if (status == MagickFalse)
1697 continue;
1698 p=GetCacheViewAuthenticPixels(image_view,x_offset+x,0,1,image->rows,
1699 exception);
cristy4c08aed2011-07-01 19:47:50 +00001700 if (p == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001701 {
1702 status=MagickFalse;
1703 continue;
1704 }
cristyed231572011-07-14 02:18:59 +00001705 p+=y_offset*GetPixelChannels(image);
cristya19f1d72012-08-07 18:24:38 +00001706 displacement=degrees*(double) (x-width/2.0);
cristy3ed852e2009-09-05 21:47:34 +00001707 if (displacement == 0.0)
1708 continue;
1709 if (displacement > 0.0)
1710 direction=DOWN;
1711 else
1712 {
1713 displacement*=(-1.0);
1714 direction=UP;
1715 }
cristybb503372010-05-27 20:51:26 +00001716 step=(ssize_t) floor((double) displacement);
cristya19f1d72012-08-07 18:24:38 +00001717 area=(double) (displacement-step);
cristy3ed852e2009-09-05 21:47:34 +00001718 step++;
1719 pixel=background;
cristy4c08aed2011-07-01 19:47:50 +00001720 GetPixelInfo(image,&source);
1721 GetPixelInfo(image,&destination);
cristy3ed852e2009-09-05 21:47:34 +00001722 switch (direction)
1723 {
1724 case UP:
1725 {
1726 /*
1727 Transfer pixels top-to-bottom.
1728 */
1729 if (step > y_offset)
1730 break;
cristyed231572011-07-14 02:18:59 +00001731 q=p-step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001732 for (i=0; i < (ssize_t) height; i++)
cristy3ed852e2009-09-05 21:47:34 +00001733 {
1734 if ((y_offset+i) < step)
1735 {
cristyed231572011-07-14 02:18:59 +00001736 p+=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001737 GetPixelInfoPixel(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001738 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001739 continue;
1740 }
cristy803640d2011-11-17 02:11:32 +00001741 GetPixelInfoPixel(image,p,&source);
cristya19f1d72012-08-07 18:24:38 +00001742 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1743 &source,(double) GetPixelAlpha(image,p),area,
cristy4c08aed2011-07-01 19:47:50 +00001744 &destination);
cristy803640d2011-11-17 02:11:32 +00001745 SetPixelInfoPixel(image,&destination,q);
1746 GetPixelInfoPixel(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001747 p+=GetPixelChannels(image);
1748 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001749 }
cristya19f1d72012-08-07 18:24:38 +00001750 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1751 &background,(double) background.alpha,area,&destination);
cristy803640d2011-11-17 02:11:32 +00001752 SetPixelInfoPixel(image,&destination,q);
cristyed231572011-07-14 02:18:59 +00001753 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001754 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001755 {
cristy803640d2011-11-17 02:11:32 +00001756 SetPixelInfoPixel(image,&background,q);
cristyed231572011-07-14 02:18:59 +00001757 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001758 }
cristy3ed852e2009-09-05 21:47:34 +00001759 break;
1760 }
1761 case DOWN:
1762 {
1763 /*
1764 Transfer pixels bottom-to-top.
1765 */
cristyed231572011-07-14 02:18:59 +00001766 p+=height*GetPixelChannels(image);
1767 q=p+step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001768 for (i=0; i < (ssize_t) height; i++)
cristy3ed852e2009-09-05 21:47:34 +00001769 {
cristyed231572011-07-14 02:18:59 +00001770 p-=GetPixelChannels(image);
1771 q-=GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001772 if ((size_t) (y_offset+height+step-i) >= image->rows)
cristy3ed852e2009-09-05 21:47:34 +00001773 continue;
cristy803640d2011-11-17 02:11:32 +00001774 GetPixelInfoPixel(image,p,&source);
cristya19f1d72012-08-07 18:24:38 +00001775 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1776 &source,(double) GetPixelAlpha(image,p),area,
cristy4c08aed2011-07-01 19:47:50 +00001777 &destination);
cristy803640d2011-11-17 02:11:32 +00001778 SetPixelInfoPixel(image,&destination,q);
1779 GetPixelInfoPixel(image,p,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001780 }
cristya19f1d72012-08-07 18:24:38 +00001781 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1782 &background,(double) background.alpha,area,&destination);
cristyed231572011-07-14 02:18:59 +00001783 q-=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001784 SetPixelInfoPixel(image,&destination,q);
cristy3ed852e2009-09-05 21:47:34 +00001785 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001786 {
cristyed231572011-07-14 02:18:59 +00001787 q-=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001788 SetPixelInfoPixel(image,&background,q);
cristy4c08aed2011-07-01 19:47:50 +00001789 }
cristy3ed852e2009-09-05 21:47:34 +00001790 break;
1791 }
1792 }
1793 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1794 status=MagickFalse;
1795 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1796 {
1797 MagickBooleanType
1798 proceed;
1799
cristyb5d5f722009-11-04 03:03:49 +00001800#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy7b650b52011-10-09 01:13:39 +00001801 #pragma omp critical (MagickCore_YShearImage)
cristy3ed852e2009-09-05 21:47:34 +00001802#endif
1803 proceed=SetImageProgress(image,YShearImageTag,progress++,image->rows);
1804 if (proceed == MagickFalse)
1805 status=MagickFalse;
1806 }
1807 }
1808 image_view=DestroyCacheView(image_view);
1809 return(status);
1810}
1811
1812/*
1813%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1814% %
1815% %
1816% %
cristy3ed852e2009-09-05 21:47:34 +00001817% S h e a r I m a g e %
1818% %
1819% %
1820% %
1821%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1822%
1823% ShearImage() creates a new image that is a shear_image copy of an existing
cristycee97112010-05-28 00:44:52 +00001824% one. Shearing slides one edge of an image along the X or Y axis, creating
1825% a parallelogram. An X direction shear slides an edge along the X axis,
1826% while a Y direction shear slides an edge along the Y axis. The amount of
cristy3ed852e2009-09-05 21:47:34 +00001827% the shear is controlled by a shear angle. For X direction shears, x_shear
1828% is measured relative to the Y axis, and similarly, for Y direction shears
1829% y_shear is measured relative to the X axis. Empty triangles left over from
1830% shearing the image are filled with the background color defined by member
1831% 'background_color' of the image.. ShearImage() allocates the memory
1832% necessary for the new Image structure and returns a pointer to the new image.
1833%
1834% ShearImage() is based on the paper "A Fast Algorithm for General Raster
1835% Rotatation" by Alan W. Paeth.
1836%
1837% The format of the ShearImage method is:
1838%
1839% Image *ShearImage(const Image *image,const double x_shear,
1840% const double y_shear,ExceptionInfo *exception)
1841%
1842% A description of each parameter follows.
1843%
1844% o image: the image.
1845%
1846% o x_shear, y_shear: Specifies the number of degrees to shear the image.
1847%
1848% o exception: return any errors or warnings in this structure.
1849%
1850*/
1851MagickExport Image *ShearImage(const Image *image,const double x_shear,
1852 const double y_shear,ExceptionInfo *exception)
1853{
1854 Image
1855 *integral_image,
1856 *shear_image;
1857
cristybb503372010-05-27 20:51:26 +00001858 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001859 x_offset,
1860 y_offset;
1861
cristyecc2c142010-01-17 22:25:46 +00001862 MagickBooleanType
1863 status;
1864
cristy3ed852e2009-09-05 21:47:34 +00001865 PointInfo
1866 shear;
1867
1868 RectangleInfo
1869 border_info;
1870
cristybb503372010-05-27 20:51:26 +00001871 size_t
cristy3ed852e2009-09-05 21:47:34 +00001872 y_width;
1873
1874 assert(image != (Image *) NULL);
1875 assert(image->signature == MagickSignature);
1876 if (image->debug != MagickFalse)
1877 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1878 assert(exception != (ExceptionInfo *) NULL);
1879 assert(exception->signature == MagickSignature);
1880 if ((x_shear != 0.0) && (fmod(x_shear,90.0) == 0.0))
1881 ThrowImageException(ImageError,"AngleIsDiscontinuous");
1882 if ((y_shear != 0.0) && (fmod(y_shear,90.0) == 0.0))
1883 ThrowImageException(ImageError,"AngleIsDiscontinuous");
1884 /*
1885 Initialize shear angle.
1886 */
1887 integral_image=CloneImage(image,0,0,MagickTrue,exception);
1888 if (integral_image == (Image *) NULL)
1889 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1890 shear.x=(-tan(DegreesToRadians(fmod(x_shear,360.0))));
1891 shear.y=tan(DegreesToRadians(fmod(y_shear,360.0)));
1892 if ((shear.x == 0.0) && (shear.y == 0.0))
1893 return(integral_image);
cristy574cc262011-08-05 01:23:58 +00001894 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001895 {
cristy3ed852e2009-09-05 21:47:34 +00001896 integral_image=DestroyImage(integral_image);
1897 return(integral_image);
1898 }
cristy8a46d822012-08-28 23:32:39 +00001899 if (integral_image->alpha_trait != BlendPixelTrait)
cristy63240882011-08-05 19:05:27 +00001900 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00001901 /*
1902 Compute image size.
1903 */
cristybb503372010-05-27 20:51:26 +00001904 y_width=image->columns+(ssize_t) floor(fabs(shear.x)*image->rows+0.5);
cristycee97112010-05-28 00:44:52 +00001905 x_offset=(ssize_t) ceil((double) image->columns+((fabs(shear.x)*image->rows)-
cristy06609ee2010-03-17 20:21:27 +00001906 image->columns)/2.0-0.5);
cristycee97112010-05-28 00:44:52 +00001907 y_offset=(ssize_t) ceil((double) image->rows+((fabs(shear.y)*y_width)-
1908 image->rows)/2.0-0.5);
cristy3ed852e2009-09-05 21:47:34 +00001909 /*
1910 Surround image with border.
1911 */
1912 integral_image->border_color=integral_image->background_color;
1913 integral_image->compose=CopyCompositeOp;
cristybb503372010-05-27 20:51:26 +00001914 border_info.width=(size_t) x_offset;
1915 border_info.height=(size_t) y_offset;
cristy633f0c62011-09-15 13:27:36 +00001916 shear_image=BorderImage(integral_image,&border_info,image->compose,exception);
cristyecc2c142010-01-17 22:25:46 +00001917 integral_image=DestroyImage(integral_image);
cristy3ed852e2009-09-05 21:47:34 +00001918 if (shear_image == (Image *) NULL)
1919 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +00001920 /*
1921 Shear the image.
1922 */
cristy8a46d822012-08-28 23:32:39 +00001923 if (shear_image->alpha_trait != BlendPixelTrait)
cristy63240882011-08-05 19:05:27 +00001924 (void) SetImageAlphaChannel(shear_image,OpaqueAlphaChannel,exception);
cristyecc2c142010-01-17 22:25:46 +00001925 status=XShearImage(shear_image,shear.x,image->columns,image->rows,x_offset,
cristyeaedf062010-05-29 22:36:02 +00001926 (ssize_t) (shear_image->rows-image->rows)/2,exception);
cristyecc2c142010-01-17 22:25:46 +00001927 if (status == MagickFalse)
1928 {
1929 shear_image=DestroyImage(shear_image);
1930 return((Image *) NULL);
1931 }
cristyeaedf062010-05-29 22:36:02 +00001932 status=YShearImage(shear_image,shear.y,y_width,image->rows,(ssize_t)
1933 (shear_image->columns-y_width)/2,y_offset,exception);
cristyecc2c142010-01-17 22:25:46 +00001934 if (status == MagickFalse)
1935 {
1936 shear_image=DestroyImage(shear_image);
1937 return((Image *) NULL);
1938 }
cristya19f1d72012-08-07 18:24:38 +00001939 status=CropToFitImage(&shear_image,shear.x,shear.y,(double)
1940 image->columns,(double) image->rows,MagickFalse,exception);
cristy3ed852e2009-09-05 21:47:34 +00001941 shear_image->compose=image->compose;
1942 shear_image->page.width=0;
1943 shear_image->page.height=0;
cristy1c2f48d2012-12-14 01:20:55 +00001944 if (status == MagickFalse)
1945 shear_image=DestroyImage(shear_image);
cristy3ed852e2009-09-05 21:47:34 +00001946 return(shear_image);
1947}
cristyc7c81142011-11-07 12:14:23 +00001948
1949/*
1950%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1951% %
1952% %
1953% %
1954% S h e a r R o t a t e I m a g e %
1955% %
1956% %
1957% %
1958%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1959%
1960% ShearRotateImage() creates a new image that is a rotated copy of an existing
1961% one. Positive angles rotate counter-clockwise (right-hand rule), while
1962% negative angles rotate clockwise. Rotated images are usually larger than
1963% the originals and have 'empty' triangular corners. X axis. Empty
1964% triangles left over from shearing the image are filled with the background
1965% color defined by member 'background_color' of the image. ShearRotateImage
1966% allocates the memory necessary for the new Image structure and returns a
1967% pointer to the new image.
1968%
1969% ShearRotateImage() is based on the paper "A Fast Algorithm for General
1970% Raster Rotatation" by Alan W. Paeth. ShearRotateImage is adapted from a
1971% similar method based on the Paeth paper written by Michael Halle of the
1972% Spatial Imaging Group, MIT Media Lab.
1973%
1974% The format of the ShearRotateImage method is:
1975%
1976% Image *ShearRotateImage(const Image *image,const double degrees,
1977% ExceptionInfo *exception)
1978%
1979% A description of each parameter follows.
1980%
1981% o image: the image.
1982%
1983% o degrees: Specifies the number of degrees to rotate the image.
1984%
1985% o exception: return any errors or warnings in this structure.
1986%
1987*/
1988MagickExport Image *ShearRotateImage(const Image *image,const double degrees,
1989 ExceptionInfo *exception)
1990{
1991 Image
1992 *integral_image,
1993 *rotate_image;
1994
1995 MagickBooleanType
1996 status;
1997
cristya19f1d72012-08-07 18:24:38 +00001998 double
cristyc7c81142011-11-07 12:14:23 +00001999 angle;
2000
2001 PointInfo
2002 shear;
2003
2004 RectangleInfo
2005 border_info;
2006
2007 size_t
2008 height,
2009 rotations,
2010 width,
2011 y_width;
2012
2013 ssize_t
2014 x_offset,
2015 y_offset;
2016
2017 /*
2018 Adjust rotation angle.
2019 */
2020 assert(image != (Image *) NULL);
2021 assert(image->signature == MagickSignature);
2022 if (image->debug != MagickFalse)
2023 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2024 assert(exception != (ExceptionInfo *) NULL);
2025 assert(exception->signature == MagickSignature);
2026 angle=degrees;
2027 while (angle < -45.0)
2028 angle+=360.0;
2029 for (rotations=0; angle > 45.0; rotations++)
2030 angle-=90.0;
2031 rotations%=4;
2032 /*
2033 Calculate shear equations.
2034 */
2035 integral_image=IntegralRotateImage(image,rotations,exception);
2036 if (integral_image == (Image *) NULL)
2037 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2038 shear.x=(-tan((double) DegreesToRadians(angle)/2.0));
2039 shear.y=sin((double) DegreesToRadians(angle));
2040 if ((shear.x == 0.0) && (shear.y == 0.0))
2041 return(integral_image);
2042 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
2043 {
2044 integral_image=DestroyImage(integral_image);
2045 return(integral_image);
2046 }
cristy8a46d822012-08-28 23:32:39 +00002047 if (integral_image->alpha_trait != BlendPixelTrait)
cristyc7c81142011-11-07 12:14:23 +00002048 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
2049 /*
2050 Compute image size.
2051 */
2052 width=image->columns;
2053 height=image->rows;
2054 if ((rotations == 1) || (rotations == 3))
2055 {
2056 width=image->rows;
2057 height=image->columns;
2058 }
2059 y_width=width+(ssize_t) floor(fabs(shear.x)*height+0.5);
2060 x_offset=(ssize_t) ceil((double) width+((fabs(shear.y)*height)-width)/2.0-
2061 0.5);
2062 y_offset=(ssize_t) ceil((double) height+((fabs(shear.y)*y_width)-height)/2.0-
2063 0.5);
2064 /*
2065 Surround image with a border.
2066 */
2067 integral_image->border_color=integral_image->background_color;
2068 integral_image->compose=CopyCompositeOp;
2069 border_info.width=(size_t) x_offset;
2070 border_info.height=(size_t) y_offset;
2071 rotate_image=BorderImage(integral_image,&border_info,image->compose,
2072 exception);
2073 integral_image=DestroyImage(integral_image);
2074 if (rotate_image == (Image *) NULL)
2075 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2076 /*
2077 Rotate the image.
2078 */
2079 status=XShearImage(rotate_image,shear.x,width,height,x_offset,(ssize_t)
2080 (rotate_image->rows-height)/2,exception);
2081 if (status == MagickFalse)
2082 {
2083 rotate_image=DestroyImage(rotate_image);
2084 return((Image *) NULL);
2085 }
2086 status=YShearImage(rotate_image,shear.y,y_width,height,(ssize_t)
2087 (rotate_image->columns-y_width)/2,y_offset,exception);
2088 if (status == MagickFalse)
2089 {
2090 rotate_image=DestroyImage(rotate_image);
2091 return((Image *) NULL);
2092 }
2093 status=XShearImage(rotate_image,shear.x,y_width,rotate_image->rows,(ssize_t)
2094 (rotate_image->columns-y_width)/2,0,exception);
2095 if (status == MagickFalse)
2096 {
2097 rotate_image=DestroyImage(rotate_image);
2098 return((Image *) NULL);
2099 }
cristya19f1d72012-08-07 18:24:38 +00002100 status=CropToFitImage(&rotate_image,shear.x,shear.y,(double) width,
2101 (double) height,MagickTrue,exception);
cristyc7c81142011-11-07 12:14:23 +00002102 rotate_image->compose=image->compose;
2103 rotate_image->page.width=0;
2104 rotate_image->page.height=0;
cristy1c2f48d2012-12-14 01:20:55 +00002105 if (status == MagickFalse)
2106 rotate_image=DestroyImage(rotate_image);
cristyc7c81142011-11-07 12:14:23 +00002107 return(rotate_image);
2108}