blob: bdea9cdf604f3f3c803e42349fd55603e90e985e [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%
199%
cristy3ed852e2009-09-05 21:47:34 +0000200% The format of the DeskewImage method is:
201%
202% Image *DeskewImage(const Image *image,const double threshold,
203% ExceptionInfo *exception)
204%
205% A description of each parameter follows:
206%
207% o image: the image.
208%
209% o threshold: separate background from foreground.
210%
211% o exception: return any errors or warnings in this structure.
212%
213*/
214
215typedef struct _RadonInfo
216{
217 CacheType
218 type;
219
cristybb503372010-05-27 20:51:26 +0000220 size_t
cristy3ed852e2009-09-05 21:47:34 +0000221 width,
222 height;
223
224 MagickSizeType
225 length;
226
227 MagickBooleanType
228 mapped;
229
230 char
231 path[MaxTextExtent];
232
233 int
234 file;
235
236 unsigned short
237 *cells;
238} RadonInfo;
239
240static RadonInfo *DestroyRadonInfo(RadonInfo *radon_info)
241{
242 assert(radon_info != (RadonInfo *) NULL);
243 switch (radon_info->type)
244 {
245 case MemoryCache:
246 {
247 if (radon_info->mapped == MagickFalse)
248 radon_info->cells=(unsigned short *) RelinquishMagickMemory(
249 radon_info->cells);
250 else
251 radon_info->cells=(unsigned short *) UnmapBlob(radon_info->cells,
252 (size_t) radon_info->length);
253 RelinquishMagickResource(MemoryResource,radon_info->length);
254 break;
255 }
256 case MapCache:
257 {
258 radon_info->cells=(unsigned short *) UnmapBlob(radon_info->cells,(size_t)
259 radon_info->length);
260 RelinquishMagickResource(MapResource,radon_info->length);
261 }
262 case DiskCache:
263 {
264 if (radon_info->file != -1)
265 (void) close(radon_info->file);
266 (void) RelinquishUniqueFileResource(radon_info->path);
267 RelinquishMagickResource(DiskResource,radon_info->length);
268 break;
269 }
270 default:
271 break;
272 }
273 return((RadonInfo *) RelinquishMagickMemory(radon_info));
274}
275
276static MagickBooleanType ResetRadonCells(RadonInfo *radon_info)
277{
cristybb503372010-05-27 20:51:26 +0000278 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000279 x;
280
281 ssize_t
cristyd40deb62011-03-09 00:52:27 +0000282 count,
283 y;
cristy3ed852e2009-09-05 21:47:34 +0000284
285 unsigned short
286 value;
287
288 if (radon_info->type != DiskCache)
289 {
290 (void) ResetMagickMemory(radon_info->cells,0,(size_t) radon_info->length);
291 return(MagickTrue);
292 }
293 value=0;
cristy7f317702011-02-18 20:40:28 +0000294 (void) lseek(radon_info->file,0,SEEK_SET);
cristybb503372010-05-27 20:51:26 +0000295 for (y=0; y < (ssize_t) radon_info->height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000296 {
cristybb503372010-05-27 20:51:26 +0000297 for (x=0; x < (ssize_t) radon_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000298 {
299 count=write(radon_info->file,&value,sizeof(*radon_info->cells));
300 if (count != (ssize_t) sizeof(*radon_info->cells))
301 break;
302 }
cristybb503372010-05-27 20:51:26 +0000303 if (x < (ssize_t) radon_info->width)
cristy3ed852e2009-09-05 21:47:34 +0000304 break;
305 }
cristybb503372010-05-27 20:51:26 +0000306 return(y < (ssize_t) radon_info->height ? MagickFalse : MagickTrue);
cristy3ed852e2009-09-05 21:47:34 +0000307}
308
cristybb503372010-05-27 20:51:26 +0000309static RadonInfo *AcquireRadonInfo(const Image *image,const size_t width,
310 const size_t height,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000311{
312 MagickBooleanType
313 status;
314
315 RadonInfo
316 *radon_info;
317
cristy73bd4a52010-10-05 11:24:23 +0000318 radon_info=(RadonInfo *) AcquireMagickMemory(sizeof(*radon_info));
cristy3ed852e2009-09-05 21:47:34 +0000319 if (radon_info == (RadonInfo *) NULL)
320 return((RadonInfo *) NULL);
321 (void) ResetMagickMemory(radon_info,0,sizeof(*radon_info));
322 radon_info->width=width;
323 radon_info->height=height;
324 radon_info->length=(MagickSizeType) width*height*sizeof(*radon_info->cells);
325 radon_info->type=MemoryCache;
326 status=AcquireMagickResource(AreaResource,radon_info->length);
327 if ((status != MagickFalse) &&
328 (radon_info->length == (MagickSizeType) ((size_t) radon_info->length)))
329 {
330 status=AcquireMagickResource(MemoryResource,radon_info->length);
331 if (status != MagickFalse)
332 {
333 radon_info->mapped=MagickFalse;
334 radon_info->cells=(unsigned short *) AcquireMagickMemory((size_t)
335 radon_info->length);
336 if (radon_info->cells == (unsigned short *) NULL)
337 {
338 radon_info->mapped=MagickTrue;
339 radon_info->cells=(unsigned short *) MapBlob(-1,IOMode,0,(size_t)
340 radon_info->length);
341 }
342 if (radon_info->cells == (unsigned short *) NULL)
343 RelinquishMagickResource(MemoryResource,radon_info->length);
344 }
345 }
346 radon_info->file=(-1);
347 if (radon_info->cells == (unsigned short *) NULL)
348 {
349 status=AcquireMagickResource(DiskResource,radon_info->length);
350 if (status == MagickFalse)
351 {
352 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
cristyefe601c2013-01-05 17:51:12 +0000353 "CacheResourcesExhausted","`%s'",image->filename);
cristy3ed852e2009-09-05 21:47:34 +0000354 return(DestroyRadonInfo(radon_info));
355 }
356 radon_info->type=DiskCache;
357 (void) AcquireMagickResource(MemoryResource,radon_info->length);
358 radon_info->file=AcquireUniqueFileResource(radon_info->path);
359 if (radon_info->file == -1)
360 return(DestroyRadonInfo(radon_info));
361 status=AcquireMagickResource(MapResource,radon_info->length);
362 if (status != MagickFalse)
363 {
364 status=ResetRadonCells(radon_info);
365 if (status != MagickFalse)
366 {
367 radon_info->cells=(unsigned short *) MapBlob(radon_info->file,
368 IOMode,0,(size_t) radon_info->length);
369 if (radon_info->cells != (unsigned short *) NULL)
370 radon_info->type=MapCache;
371 else
372 RelinquishMagickResource(MapResource,radon_info->length);
373 }
374 }
375 }
376 return(radon_info);
377}
378
379static inline size_t MagickMin(const size_t x,const size_t y)
380{
381 if (x < y)
382 return(x);
383 return(y);
384}
385
386static inline ssize_t ReadRadonCell(const RadonInfo *radon_info,
cristy2c38b272011-02-18 23:25:33 +0000387 const MagickOffsetType offset,const size_t length,unsigned char *buffer)
cristy3ed852e2009-09-05 21:47:34 +0000388{
389 register ssize_t
390 i;
391
392 ssize_t
393 count;
394
395#if !defined(MAGICKCORE_HAVE_PPREAD)
cristyb5d5f722009-11-04 03:03:49 +0000396#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000397 #pragma omp critical (MagickCore_ReadRadonCell)
398#endif
399 {
400 i=(-1);
cristy7f317702011-02-18 20:40:28 +0000401 if (lseek(radon_info->file,offset,SEEK_SET) >= 0)
cristy3ed852e2009-09-05 21:47:34 +0000402 {
403#endif
404 count=0;
405 for (i=0; i < (ssize_t) length; i+=count)
406 {
407#if !defined(MAGICKCORE_HAVE_PPREAD)
408 count=read(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
409 SSIZE_MAX));
410#else
411 count=pread(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
cristy2c38b272011-02-18 23:25:33 +0000412 SSIZE_MAX),offset+i);
cristy3ed852e2009-09-05 21:47:34 +0000413#endif
414 if (count > 0)
415 continue;
416 count=0;
417 if (errno != EINTR)
418 {
419 i=(-1);
420 break;
421 }
422 }
423#if !defined(MAGICKCORE_HAVE_PPREAD)
424 }
425 }
426#endif
427 return(i);
428}
429
430static inline ssize_t WriteRadonCell(const RadonInfo *radon_info,
cristy2c38b272011-02-18 23:25:33 +0000431 const MagickOffsetType offset,const size_t length,const unsigned char *buffer)
cristy3ed852e2009-09-05 21:47:34 +0000432{
433 register ssize_t
434 i;
435
436 ssize_t
437 count;
438
439#if !defined(MAGICKCORE_HAVE_PWRITE)
cristyb5d5f722009-11-04 03:03:49 +0000440#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000441 #pragma omp critical (MagickCore_WriteRadonCell)
442#endif
443 {
cristy7f317702011-02-18 20:40:28 +0000444 if (lseek(radon_info->file,offset,SEEK_SET) >= 0)
cristy3ed852e2009-09-05 21:47:34 +0000445 {
446#endif
447 count=0;
448 for (i=0; i < (ssize_t) length; i+=count)
449 {
450#if !defined(MAGICKCORE_HAVE_PWRITE)
451 count=write(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
452 SSIZE_MAX));
453#else
454 count=pwrite(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
cristy2c38b272011-02-18 23:25:33 +0000455 SSIZE_MAX),offset+i);
cristy3ed852e2009-09-05 21:47:34 +0000456#endif
457 if (count > 0)
458 continue;
459 count=0;
460 if (errno != EINTR)
461 {
462 i=(-1);
463 break;
464 }
465 }
466#if !defined(MAGICKCORE_HAVE_PWRITE)
467 }
468 }
469#endif
470 return(i);
471}
472
473static inline unsigned short GetRadonCell(const RadonInfo *radon_info,
cristybb503372010-05-27 20:51:26 +0000474 const ssize_t x,const ssize_t y)
cristy3ed852e2009-09-05 21:47:34 +0000475{
cristy2c38b272011-02-18 23:25:33 +0000476 MagickOffsetType
cristy3ed852e2009-09-05 21:47:34 +0000477 i;
478
479 unsigned short
480 value;
481
cristy2c38b272011-02-18 23:25:33 +0000482 i=(MagickOffsetType) radon_info->height*x+y;
cristy3ed852e2009-09-05 21:47:34 +0000483 if ((i < 0) ||
484 ((MagickSizeType) (i*sizeof(*radon_info->cells)) >= radon_info->length))
485 return(0);
486 if (radon_info->type != DiskCache)
487 return(radon_info->cells[i]);
488 value=0;
489 (void) ReadRadonCell(radon_info,i*sizeof(*radon_info->cells),
490 sizeof(*radon_info->cells),(unsigned char *) &value);
491 return(value);
492}
493
494static inline MagickBooleanType SetRadonCell(const RadonInfo *radon_info,
cristybb503372010-05-27 20:51:26 +0000495 const ssize_t x,const ssize_t y,const unsigned short value)
cristy3ed852e2009-09-05 21:47:34 +0000496{
cristy2c38b272011-02-18 23:25:33 +0000497 MagickOffsetType
cristy3ed852e2009-09-05 21:47:34 +0000498 i;
499
500 ssize_t
501 count;
502
cristy2c38b272011-02-18 23:25:33 +0000503 i=(MagickOffsetType) radon_info->height*x+y;
cristy3ed852e2009-09-05 21:47:34 +0000504 if ((i < 0) ||
505 ((MagickSizeType) (i*sizeof(*radon_info->cells)) >= radon_info->length))
506 return(MagickFalse);
507 if (radon_info->type != DiskCache)
508 {
509 radon_info->cells[i]=value;
510 return(MagickTrue);
511 }
512 count=WriteRadonCell(radon_info,i*sizeof(*radon_info->cells),
cristy44807bb2009-09-19 18:28:06 +0000513 sizeof(*radon_info->cells),(const unsigned char *) &value);
cristy3ed852e2009-09-05 21:47:34 +0000514 if (count != (ssize_t) sizeof(*radon_info->cells))
515 return(MagickFalse);
516 return(MagickTrue);
517}
518
cristy4ee2b0c2012-05-15 00:30:35 +0000519static void RadonProjection(const Image *image,RadonInfo *source_cells,
cristybb503372010-05-27 20:51:26 +0000520 RadonInfo *destination_cells,const ssize_t sign,size_t *projection)
cristy3ed852e2009-09-05 21:47:34 +0000521{
522 RadonInfo
523 *swap;
524
cristybb503372010-05-27 20:51:26 +0000525 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000526 x;
527
528 register RadonInfo
529 *p,
530 *q;
531
cristybb503372010-05-27 20:51:26 +0000532 size_t
cristy3ed852e2009-09-05 21:47:34 +0000533 step;
534
535 p=source_cells;
536 q=destination_cells;
537 for (step=1; step < p->width; step*=2)
538 {
cristyeaedf062010-05-29 22:36:02 +0000539 for (x=0; x < (ssize_t) p->width; x+=2*(ssize_t) step)
cristy3ed852e2009-09-05 21:47:34 +0000540 {
cristybb503372010-05-27 20:51:26 +0000541 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000542 i;
543
cristyd40deb62011-03-09 00:52:27 +0000544 ssize_t
545 y;
546
cristy3ed852e2009-09-05 21:47:34 +0000547 unsigned short
548 cell;
549
cristybb503372010-05-27 20:51:26 +0000550 for (i=0; i < (ssize_t) step; i++)
cristy3ed852e2009-09-05 21:47:34 +0000551 {
cristybb503372010-05-27 20:51:26 +0000552 for (y=0; y < (ssize_t) (p->height-i-1); y++)
cristy3ed852e2009-09-05 21:47:34 +0000553 {
554 cell=GetRadonCell(p,x+i,y);
cristyeaedf062010-05-29 22:36:02 +0000555 (void) SetRadonCell(q,x+2*i,y,cell+GetRadonCell(p,x+i+(ssize_t)
556 step,y+i));
557 (void) SetRadonCell(q,x+2*i+1,y,cell+GetRadonCell(p,x+i+(ssize_t)
558 step,y+i+1));
cristy3ed852e2009-09-05 21:47:34 +0000559 }
cristybb503372010-05-27 20:51:26 +0000560 for ( ; y < (ssize_t) (p->height-i); y++)
cristy3ed852e2009-09-05 21:47:34 +0000561 {
562 cell=GetRadonCell(p,x+i,y);
cristyeaedf062010-05-29 22:36:02 +0000563 (void) SetRadonCell(q,x+2*i,y,cell+GetRadonCell(p,x+i+(ssize_t) step,
564 y+i));
cristy3ed852e2009-09-05 21:47:34 +0000565 (void) SetRadonCell(q,x+2*i+1,y,cell);
566 }
cristybb503372010-05-27 20:51:26 +0000567 for ( ; y < (ssize_t) p->height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000568 {
569 cell=GetRadonCell(p,x+i,y);
570 (void) SetRadonCell(q,x+2*i,y,cell);
571 (void) SetRadonCell(q,x+2*i+1,y,cell);
572 }
573 }
574 }
575 swap=p;
576 p=q;
577 q=swap;
578 }
cristyb5d5f722009-11-04 03:03:49 +0000579#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000580 #pragma omp parallel for schedule(static,4) \
cristycb7dfcc2013-01-06 18:34:59 +0000581 magick_threads(image,image,1,1)
cristy3ed852e2009-09-05 21:47:34 +0000582#endif
cristybb503372010-05-27 20:51:26 +0000583 for (x=0; x < (ssize_t) p->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000584 {
cristybb503372010-05-27 20:51:26 +0000585 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000586 y;
587
cristybb503372010-05-27 20:51:26 +0000588 size_t
cristy3ed852e2009-09-05 21:47:34 +0000589 sum;
590
591 sum=0;
cristybb503372010-05-27 20:51:26 +0000592 for (y=0; y < (ssize_t) (p->height-1); y++)
cristy3ed852e2009-09-05 21:47:34 +0000593 {
cristybb503372010-05-27 20:51:26 +0000594 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000595 delta;
596
cristybb503372010-05-27 20:51:26 +0000597 delta=GetRadonCell(p,x,y)-(ssize_t) GetRadonCell(p,x,y+1);
cristy3ed852e2009-09-05 21:47:34 +0000598 sum+=delta*delta;
599 }
600 projection[p->width+sign*x-1]=sum;
601 }
602}
603
604static MagickBooleanType RadonTransform(const Image *image,
cristybb503372010-05-27 20:51:26 +0000605 const double threshold,size_t *projection,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000606{
607 CacheView
608 *image_view;
609
cristy3ed852e2009-09-05 21:47:34 +0000610 MagickBooleanType
611 status;
612
613 RadonInfo
614 *destination_cells,
615 *source_cells;
616
cristybb503372010-05-27 20:51:26 +0000617 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000618 i;
619
cristybb503372010-05-27 20:51:26 +0000620 size_t
cristy3ed852e2009-09-05 21:47:34 +0000621 count,
622 width;
623
cristyd40deb62011-03-09 00:52:27 +0000624 ssize_t
625 y;
626
627 unsigned char
628 byte;
629
cristy3ed852e2009-09-05 21:47:34 +0000630 unsigned short
631 bits[256];
632
633 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
634 source_cells=AcquireRadonInfo(image,width,image->rows,exception);
635 destination_cells=AcquireRadonInfo(image,width,image->rows,exception);
636 if ((source_cells == (RadonInfo *) NULL) ||
637 (destination_cells == (RadonInfo *) NULL))
638 {
639 if (destination_cells != (RadonInfo *) NULL)
640 destination_cells=DestroyRadonInfo(destination_cells);
641 if (source_cells != (RadonInfo *) NULL)
642 source_cells=DestroyRadonInfo(source_cells);
643 return(MagickFalse);
644 }
645 if (ResetRadonCells(source_cells) == MagickFalse)
646 {
647 destination_cells=DestroyRadonInfo(destination_cells);
648 source_cells=DestroyRadonInfo(source_cells);
649 return(MagickFalse);
650 }
651 for (i=0; i < 256; i++)
652 {
653 byte=(unsigned char) i;
654 for (count=0; byte != 0; byte>>=1)
655 count+=byte & 0x01;
656 bits[i]=(unsigned short) count;
657 }
658 status=MagickTrue;
cristy46ff2672012-12-14 15:32:26 +0000659 image_view=AcquireVirtualCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +0000660#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000661 #pragma omp parallel for schedule(static,4) shared(status) \
cristy5e6b2592012-12-19 14:08:11 +0000662 magick_threads(image,image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +0000663#endif
cristybb503372010-05-27 20:51:26 +0000664 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000665 {
cristy4c08aed2011-07-01 19:47:50 +0000666 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000667 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000668
cristybb503372010-05-27 20:51:26 +0000669 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000670 i,
671 x;
672
cristybb503372010-05-27 20:51:26 +0000673 size_t
cristy3ed852e2009-09-05 21:47:34 +0000674 bit,
675 byte;
676
677 if (status == MagickFalse)
678 continue;
679 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000680 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000681 {
682 status=MagickFalse;
683 continue;
684 }
685 bit=0;
686 byte=0;
cristybb503372010-05-27 20:51:26 +0000687 i=(ssize_t) (image->columns+7)/8;
688 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000689 {
690 byte<<=1;
cristyf13c5942012-08-08 23:50:11 +0000691 if (GetPixelIntensity(image,p) < threshold)
cristy3ed852e2009-09-05 21:47:34 +0000692 byte|=0x01;
693 bit++;
694 if (bit == 8)
695 {
696 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
697 bit=0;
698 byte=0;
699 }
cristyed231572011-07-14 02:18:59 +0000700 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000701 }
702 if (bit != 0)
703 {
704 byte<<=(8-bit);
705 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
706 }
707 }
cristy4ee2b0c2012-05-15 00:30:35 +0000708 RadonProjection(image,source_cells,destination_cells,-1,projection);
cristy3ed852e2009-09-05 21:47:34 +0000709 (void) ResetRadonCells(source_cells);
cristyb5d5f722009-11-04 03:03:49 +0000710#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000711 #pragma omp parallel for schedule(static,4) shared(status) \
cristycb7dfcc2013-01-06 18:34:59 +0000712 magick_threads(image,image,1,1)
cristy3ed852e2009-09-05 21:47:34 +0000713#endif
cristybb503372010-05-27 20:51:26 +0000714 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000715 {
cristy4c08aed2011-07-01 19:47:50 +0000716 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000717 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000718
cristybb503372010-05-27 20:51:26 +0000719 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000720 i,
721 x;
722
cristybb503372010-05-27 20:51:26 +0000723 size_t
cristy3ed852e2009-09-05 21:47:34 +0000724 bit,
725 byte;
726
727 if (status == MagickFalse)
728 continue;
729 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000730 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000731 {
732 status=MagickFalse;
733 continue;
734 }
735 bit=0;
736 byte=0;
737 i=0;
cristybb503372010-05-27 20:51:26 +0000738 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000739 {
740 byte<<=1;
cristyf13c5942012-08-08 23:50:11 +0000741 if (GetPixelIntensity(image,p) < threshold)
cristy3ed852e2009-09-05 21:47:34 +0000742 byte|=0x01;
743 bit++;
744 if (bit == 8)
745 {
746 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
747 bit=0;
748 byte=0;
749 }
cristyed231572011-07-14 02:18:59 +0000750 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000751 }
752 if (bit != 0)
753 {
754 byte<<=(8-bit);
755 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
756 }
757 }
cristy4ee2b0c2012-05-15 00:30:35 +0000758 RadonProjection(image,source_cells,destination_cells,1,projection);
cristy3ed852e2009-09-05 21:47:34 +0000759 image_view=DestroyCacheView(image_view);
760 destination_cells=DestroyRadonInfo(destination_cells);
761 source_cells=DestroyRadonInfo(source_cells);
762 return(MagickTrue);
763}
764
cristybb503372010-05-27 20:51:26 +0000765static void GetImageBackgroundColor(Image *image,const ssize_t offset,
cristy3ed852e2009-09-05 21:47:34 +0000766 ExceptionInfo *exception)
767{
768 CacheView
769 *image_view;
770
cristy4c08aed2011-07-01 19:47:50 +0000771 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000772 background;
773
cristya19f1d72012-08-07 18:24:38 +0000774 double
cristy3ed852e2009-09-05 21:47:34 +0000775 count;
776
cristyd40deb62011-03-09 00:52:27 +0000777 ssize_t
778 y;
779
cristy3ed852e2009-09-05 21:47:34 +0000780 /*
781 Compute average background color.
782 */
783 if (offset <= 0)
784 return;
cristy4c08aed2011-07-01 19:47:50 +0000785 GetPixelInfo(image,&background);
cristy3ed852e2009-09-05 21:47:34 +0000786 count=0.0;
cristy46ff2672012-12-14 15:32:26 +0000787 image_view=AcquireVirtualCacheView(image,exception);
cristybb503372010-05-27 20:51:26 +0000788 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000789 {
cristy4c08aed2011-07-01 19:47:50 +0000790 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000791 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000792
cristybb503372010-05-27 20:51:26 +0000793 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000794 x;
795
cristybb503372010-05-27 20:51:26 +0000796 if ((y >= offset) && (y < ((ssize_t) image->rows-offset)))
cristy3ed852e2009-09-05 21:47:34 +0000797 continue;
798 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000799 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000800 continue;
cristybb503372010-05-27 20:51:26 +0000801 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000802 {
cristybb503372010-05-27 20:51:26 +0000803 if ((x >= offset) && (x < ((ssize_t) image->columns-offset)))
cristy3ed852e2009-09-05 21:47:34 +0000804 continue;
cristy4c08aed2011-07-01 19:47:50 +0000805 background.red+=QuantumScale*GetPixelRed(image,p);
806 background.green+=QuantumScale*GetPixelGreen(image,p);
807 background.blue+=QuantumScale*GetPixelBlue(image,p);
cristy26295322011-09-22 00:50:36 +0000808 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
809 background.alpha+=QuantumScale*GetPixelAlpha(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000810 count++;
cristyed231572011-07-14 02:18:59 +0000811 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000812 }
813 }
814 image_view=DestroyCacheView(image_view);
cristy8cd03c32012-07-07 18:57:59 +0000815 image->background_color.red=(double) ClampToQuantum(QuantumRange*
816 background.red/count);
817 image->background_color.green=(double) ClampToQuantum(QuantumRange*
818 background.green/count);
819 image->background_color.blue=(double) ClampToQuantum(QuantumRange*
820 background.blue/count);
cristy26295322011-09-22 00:50:36 +0000821 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy8cd03c32012-07-07 18:57:59 +0000822 image->background_color.alpha=(double) ClampToQuantum(QuantumRange*
823 background.alpha/count);
cristy3ed852e2009-09-05 21:47:34 +0000824}
825
826MagickExport Image *DeskewImage(const Image *image,const double threshold,
827 ExceptionInfo *exception)
828{
829 AffineMatrix
830 affine_matrix;
831
832 const char
833 *artifact;
834
835 double
836 degrees;
837
838 Image
839 *clone_image,
840 *crop_image,
841 *deskew_image,
842 *median_image;
843
cristy3ed852e2009-09-05 21:47:34 +0000844 MagickBooleanType
845 status;
846
847 RectangleInfo
848 geometry;
849
cristybb503372010-05-27 20:51:26 +0000850 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000851 i;
852
cristybb503372010-05-27 20:51:26 +0000853 size_t
cristy3ed852e2009-09-05 21:47:34 +0000854 max_projection,
855 *projection,
856 width;
857
cristyd40deb62011-03-09 00:52:27 +0000858 ssize_t
859 skew;
860
cristy3ed852e2009-09-05 21:47:34 +0000861 /*
862 Compute deskew angle.
863 */
864 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
cristybb503372010-05-27 20:51:26 +0000865 projection=(size_t *) AcquireQuantumMemory((size_t) (2*width-1),
cristy3ed852e2009-09-05 21:47:34 +0000866 sizeof(*projection));
cristybb503372010-05-27 20:51:26 +0000867 if (projection == (size_t *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000868 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
869 status=RadonTransform(image,threshold,projection,exception);
870 if (status == MagickFalse)
871 {
cristybb503372010-05-27 20:51:26 +0000872 projection=(size_t *) RelinquishMagickMemory(projection);
cristy3ed852e2009-09-05 21:47:34 +0000873 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
874 }
875 max_projection=0;
876 skew=0;
cristybb503372010-05-27 20:51:26 +0000877 for (i=0; i < (ssize_t) (2*width-1); i++)
cristy3ed852e2009-09-05 21:47:34 +0000878 {
879 if (projection[i] > max_projection)
880 {
cristybb503372010-05-27 20:51:26 +0000881 skew=i-(ssize_t) width+1;
cristy3ed852e2009-09-05 21:47:34 +0000882 max_projection=projection[i];
883 }
884 }
cristybb503372010-05-27 20:51:26 +0000885 projection=(size_t *) RelinquishMagickMemory(projection);
anthony553bfcc2013-04-10 01:27:43 +0000886 degrees=RadiansToDegrees(-atan((double) skew/width/8));
887 if (image->debug != MagickFalse)
888 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
889 " Deskew angle: %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +0000890 /*
891 Deskew image.
892 */
893 clone_image=CloneImage(image,0,0,MagickTrue,exception);
894 if (clone_image == (Image *) NULL)
895 return((Image *) NULL);
anthony8a95e802013-04-10 05:31:57 +0000896 {
897 char
898 angle[MaxTextExtent];
899
900 (void) FormatLocaleString(angle,MaxTextExtent,"%.20g",degrees);
901 (void) SetImageArtifact(clone_image,"deskew:angle",angle);
902 }
cristy387430f2012-02-07 13:09:46 +0000903 (void) SetImageVirtualPixelMethod(clone_image,BackgroundVirtualPixelMethod,
904 exception);
cristy3ed852e2009-09-05 21:47:34 +0000905 affine_matrix.sx=cos(DegreesToRadians(fmod((double) degrees,360.0)));
906 affine_matrix.rx=sin(DegreesToRadians(fmod((double) degrees,360.0)));
907 affine_matrix.ry=(-sin(DegreesToRadians(fmod((double) degrees,360.0))));
908 affine_matrix.sy=cos(DegreesToRadians(fmod((double) degrees,360.0)));
909 affine_matrix.tx=0.0;
910 affine_matrix.ty=0.0;
911 artifact=GetImageArtifact(image,"deskew:auto-crop");
912 if (artifact == (const char *) NULL)
913 {
914 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
915 clone_image=DestroyImage(clone_image);
916 return(deskew_image);
917 }
918 /*
919 Auto-crop image.
920 */
cristyba978e12010-09-12 20:26:50 +0000921 GetImageBackgroundColor(clone_image,(ssize_t) StringToLong(artifact),
922 exception);
cristy3ed852e2009-09-05 21:47:34 +0000923 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
924 clone_image=DestroyImage(clone_image);
925 if (deskew_image == (Image *) NULL)
926 return((Image *) NULL);
cristy95c38342011-03-18 22:39:51 +0000927 median_image=StatisticImage(deskew_image,MedianStatistic,3,3,exception);
cristy3ed852e2009-09-05 21:47:34 +0000928 if (median_image == (Image *) NULL)
929 {
930 deskew_image=DestroyImage(deskew_image);
931 return((Image *) NULL);
932 }
933 geometry=GetImageBoundingBox(median_image,exception);
934 median_image=DestroyImage(median_image);
935 if (image->debug != MagickFalse)
936 (void) LogMagickEvent(TransformEvent,GetMagickModule()," Deskew geometry: "
cristy6d8abba2010-06-03 01:10:47 +0000937 "%.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
cristye8c25f92010-06-03 00:53:06 +0000938 geometry.height,(double) geometry.x,(double) geometry.y);
cristy3ed852e2009-09-05 21:47:34 +0000939 crop_image=CropImage(deskew_image,&geometry,exception);
940 deskew_image=DestroyImage(deskew_image);
941 return(crop_image);
942}
943
944/*
945%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
946% %
947% %
948% %
cristy987feef2011-11-17 12:24:56 +0000949% 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 +0000950% %
951% %
952% %
953%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
954%
cristy3dfa16c2010-05-17 19:34:48 +0000955% IntegralRotateImage() rotates the image an integral of 90 degrees. It
cristy3ed852e2009-09-05 21:47:34 +0000956% allocates the memory necessary for the new Image structure and returns a
957% pointer to the rotated image.
958%
959% The format of the IntegralRotateImage method is:
960%
cristybb503372010-05-27 20:51:26 +0000961% Image *IntegralRotateImage(const Image *image,size_t rotations,
cristy3ed852e2009-09-05 21:47:34 +0000962% ExceptionInfo *exception)
963%
964% A description of each parameter follows.
965%
966% o image: the image.
967%
968% o rotations: Specifies the number of 90 degree rotations.
969%
970*/
cristy987feef2011-11-17 12:24:56 +0000971MagickExport Image *IntegralRotateImage(const Image *image,size_t rotations,
cristy3ed852e2009-09-05 21:47:34 +0000972 ExceptionInfo *exception)
973{
cristy3ed852e2009-09-05 21:47:34 +0000974#define RotateImageTag "Rotate/Image"
975
976 CacheView
977 *image_view,
978 *rotate_view;
979
980 Image
981 *rotate_image;
982
cristy3ed852e2009-09-05 21:47:34 +0000983 MagickBooleanType
984 status;
985
cristy5f959472010-05-27 22:19:46 +0000986 MagickOffsetType
987 progress;
988
cristy3ed852e2009-09-05 21:47:34 +0000989 RectangleInfo
990 page;
991
cristy5f959472010-05-27 22:19:46 +0000992 ssize_t
993 y;
994
cristy3ed852e2009-09-05 21:47:34 +0000995 /*
996 Initialize rotated image attributes.
997 */
998 assert(image != (Image *) NULL);
999 page=image->page;
1000 rotations%=4;
cristyb32b90a2009-09-07 21:45:48 +00001001 if (rotations == 0)
1002 return(CloneImage(image,0,0,MagickTrue,exception));
cristy3ed852e2009-09-05 21:47:34 +00001003 if ((rotations == 1) || (rotations == 3))
1004 rotate_image=CloneImage(image,image->rows,image->columns,MagickTrue,
1005 exception);
1006 else
1007 rotate_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1008 exception);
1009 if (rotate_image == (Image *) NULL)
1010 return((Image *) NULL);
1011 /*
1012 Integral rotate the image.
1013 */
1014 status=MagickTrue;
1015 progress=0;
cristy46ff2672012-12-14 15:32:26 +00001016 image_view=AcquireVirtualCacheView(image,exception);
1017 rotate_view=AcquireAuthenticCacheView(rotate_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001018 switch (rotations)
1019 {
1020 case 0:
1021 {
1022 /*
1023 Rotate 0 degrees.
1024 */
cristy3ed852e2009-09-05 21:47:34 +00001025 break;
1026 }
1027 case 1:
1028 {
cristybb503372010-05-27 20:51:26 +00001029 size_t
cristyb32b90a2009-09-07 21:45:48 +00001030 tile_height,
1031 tile_width;
1032
cristyd40deb62011-03-09 00:52:27 +00001033 ssize_t
1034 tile_y;
1035
cristy3ed852e2009-09-05 21:47:34 +00001036 /*
1037 Rotate 90 degrees.
1038 */
cristyb32b90a2009-09-07 21:45:48 +00001039 GetPixelCacheTileSize(image,&tile_width,&tile_height);
cristy8b8148b2012-10-07 00:41:15 +00001040 tile_width=image->columns;
cristy26b64912012-12-16 18:20:09 +00001041#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy64c10cd2013-01-06 18:40:43 +00001042 #pragma omp parallel for schedule(static,4) shared(status) \
cristy3e1fa372013-01-06 18:07:11 +00001043 magick_threads(image,image,1,1)
cristy9a5a52f2012-10-09 14:40:31 +00001044#endif
cristyad11aac2011-09-25 21:29:16 +00001045 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
cristy3ed852e2009-09-05 21:47:34 +00001046 {
cristybb503372010-05-27 20:51:26 +00001047 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001048 tile_x;
1049
1050 if (status == MagickFalse)
1051 continue;
cristy26295322011-09-22 00:50:36 +00001052 tile_x=0;
1053 for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
cristy3ed852e2009-09-05 21:47:34 +00001054 {
1055 MagickBooleanType
1056 sync;
1057
cristy4c08aed2011-07-01 19:47:50 +00001058 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001059 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001060
cristy4c08aed2011-07-01 19:47:50 +00001061 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001062 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001063
cristya26211e2011-10-09 01:24:57 +00001064 register ssize_t
1065 y;
1066
cristybb503372010-05-27 20:51:26 +00001067 size_t
cristyb32b90a2009-09-07 21:45:48 +00001068 height,
1069 width;
cristy3ed852e2009-09-05 21:47:34 +00001070
cristyb32b90a2009-09-07 21:45:48 +00001071 width=tile_width;
cristybb503372010-05-27 20:51:26 +00001072 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
cristya45d2ec2011-08-24 13:17:19 +00001073 width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
cristyb32b90a2009-09-07 21:45:48 +00001074 height=tile_height;
cristybb503372010-05-27 20:51:26 +00001075 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
cristya45d2ec2011-08-24 13:17:19 +00001076 height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
cristydaa97692009-09-13 02:10:35 +00001077 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1078 exception);
cristy4c08aed2011-07-01 19:47:50 +00001079 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001080 {
1081 status=MagickFalse;
1082 break;
1083 }
cristybb503372010-05-27 20:51:26 +00001084 for (y=0; y < (ssize_t) width; y++)
cristy3ed852e2009-09-05 21:47:34 +00001085 {
cristy4c08aed2011-07-01 19:47:50 +00001086 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001087 *restrict tile_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001088
cristybb503372010-05-27 20:51:26 +00001089 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001090 x;
1091
cristy27709ef2011-09-18 02:53:53 +00001092 if (status == MagickFalse)
1093 continue;
cristybb503372010-05-27 20:51:26 +00001094 q=QueueCacheViewAuthenticPixels(rotate_view,(ssize_t)
cristy92611572011-07-07 16:02:42 +00001095 (rotate_image->columns-(tile_y+height)),y+tile_x,height,1,
1096 exception);
cristyacd2ed22011-08-30 01:44:23 +00001097 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001098 {
1099 status=MagickFalse;
cristy27709ef2011-09-18 02:53:53 +00001100 continue;
cristy3ed852e2009-09-05 21:47:34 +00001101 }
cristyed231572011-07-14 02:18:59 +00001102 tile_pixels=p+((height-1)*width+y)*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001103 for (x=0; x < (ssize_t) height; x++)
cristy3ed852e2009-09-05 21:47:34 +00001104 {
cristya45d2ec2011-08-24 13:17:19 +00001105 register ssize_t
1106 i;
1107
cristy883fde12013-04-08 00:50:13 +00001108 if (GetPixelReadMask(image,tile_pixels) == 0)
cristy10a6c612012-01-29 21:41:05 +00001109 {
1110 tile_pixels-=width*GetPixelChannels(image);
1111 q+=GetPixelChannels(rotate_image);
1112 continue;
1113 }
cristya45d2ec2011-08-24 13:17:19 +00001114 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1115 {
cristy5a23c552013-02-13 14:34:28 +00001116 PixelChannel channel=GetPixelChannelChannel(image,i);
1117 PixelTrait traits=GetPixelChannelTraits(image,channel);
1118 PixelTrait rotate_traits=GetPixelChannelTraits(rotate_image,
1119 channel);
cristy010d7d12011-08-31 01:02:48 +00001120 if ((traits == UndefinedPixelTrait) ||
1121 (rotate_traits == UndefinedPixelTrait))
cristya45d2ec2011-08-24 13:17:19 +00001122 continue;
cristy0beccfa2011-09-25 20:47:53 +00001123 SetPixelChannel(rotate_image,channel,tile_pixels[i],q);
cristya45d2ec2011-08-24 13:17:19 +00001124 }
cristyed231572011-07-14 02:18:59 +00001125 tile_pixels-=width*GetPixelChannels(image);
1126 q+=GetPixelChannels(rotate_image);
cristy3ed852e2009-09-05 21:47:34 +00001127 }
cristy3ed852e2009-09-05 21:47:34 +00001128 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1129 if (sync == MagickFalse)
1130 status=MagickFalse;
1131 }
1132 }
1133 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1134 {
1135 MagickBooleanType
1136 proceed;
1137
cristy26b64912012-12-16 18:20:09 +00001138#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy9a5a52f2012-10-09 14:40:31 +00001139 #pragma omp critical (MagickCore_IntegralRotateImage)
1140#endif
cristyb32b90a2009-09-07 21:45:48 +00001141 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
cristy3ed852e2009-09-05 21:47:34 +00001142 image->rows);
1143 if (proceed == MagickFalse)
1144 status=MagickFalse;
1145 }
1146 }
cristyecc2c142010-01-17 22:25:46 +00001147 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1148 image->rows-1,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001149 Swap(page.width,page.height);
1150 Swap(page.x,page.y);
1151 if (page.width != 0)
cristybb503372010-05-27 20:51:26 +00001152 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
cristy3ed852e2009-09-05 21:47:34 +00001153 break;
1154 }
1155 case 2:
1156 {
1157 /*
1158 Rotate 180 degrees.
1159 */
cristy5f890612012-12-16 23:34:05 +00001160#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy64c10cd2013-01-06 18:40:43 +00001161 #pragma omp parallel for schedule(static,4) shared(status) \
cristyd6432472013-01-06 16:56:13 +00001162 magick_threads(image,image,1,1)
cristy5f890612012-12-16 23:34:05 +00001163#endif
cristybb503372010-05-27 20:51:26 +00001164 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001165 {
1166 MagickBooleanType
1167 sync;
1168
cristy4c08aed2011-07-01 19:47:50 +00001169 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001170 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001171
cristy4c08aed2011-07-01 19:47:50 +00001172 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001173 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001174
cristy2be50602011-10-09 01:28:13 +00001175 register ssize_t
1176 x;
1177
cristy3ed852e2009-09-05 21:47:34 +00001178 if (status == MagickFalse)
1179 continue;
cristya45d2ec2011-08-24 13:17:19 +00001180 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1181 q=QueueCacheViewAuthenticPixels(rotate_view,0,(ssize_t) (image->rows-y-
1182 1),image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001183 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00001184 {
1185 status=MagickFalse;
1186 continue;
1187 }
cristyed231572011-07-14 02:18:59 +00001188 q+=GetPixelChannels(rotate_image)*image->columns;
cristybb503372010-05-27 20:51:26 +00001189 for (x=0; x < (ssize_t) image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00001190 {
cristya45d2ec2011-08-24 13:17:19 +00001191 register ssize_t
1192 i;
1193
cristyed231572011-07-14 02:18:59 +00001194 q-=GetPixelChannels(rotate_image);
cristy883fde12013-04-08 00:50:13 +00001195 if (GetPixelReadMask(image,p) == 0)
cristy10a6c612012-01-29 21:41:05 +00001196 {
1197 p+=GetPixelChannels(image);
1198 continue;
1199 }
cristya45d2ec2011-08-24 13:17:19 +00001200 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1201 {
cristy5a23c552013-02-13 14:34:28 +00001202 PixelChannel channel=GetPixelChannelChannel(image,i);
1203 PixelTrait traits=GetPixelChannelTraits(image,channel);
1204 PixelTrait rotate_traits=GetPixelChannelTraits(rotate_image,
1205 channel);
cristy010d7d12011-08-31 01:02:48 +00001206 if ((traits == UndefinedPixelTrait) ||
1207 (rotate_traits == UndefinedPixelTrait))
cristya45d2ec2011-08-24 13:17:19 +00001208 continue;
cristy0beccfa2011-09-25 20:47:53 +00001209 SetPixelChannel(rotate_image,channel,p[i],q);
cristya45d2ec2011-08-24 13:17:19 +00001210 }
cristyed231572011-07-14 02:18:59 +00001211 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001212 }
cristy3ed852e2009-09-05 21:47:34 +00001213 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1214 if (sync == MagickFalse)
1215 status=MagickFalse;
1216 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1217 {
1218 MagickBooleanType
1219 proceed;
1220
cristy5f890612012-12-16 23:34:05 +00001221#if defined(MAGICKCORE_OPENMP_SUPPORT)
1222 #pragma omp critical (MagickCore_IntegralRotateImage)
1223#endif
cristy0729beb2011-09-25 23:29:32 +00001224 proceed=SetImageProgress(image,RotateImageTag,progress++,
cristy21e03412011-09-25 22:39:35 +00001225 image->rows);
1226 if (proceed == MagickFalse)
1227 status=MagickFalse;
1228 }
1229 }
1230 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1231 image->rows-1,image->rows);
1232 Swap(page.width,page.height);
1233 Swap(page.x,page.y);
1234 if (page.width != 0)
1235 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1236 break;
1237 }
cristy3ed852e2009-09-05 21:47:34 +00001238 case 3:
1239 {
cristybb503372010-05-27 20:51:26 +00001240 size_t
cristyb32b90a2009-09-07 21:45:48 +00001241 tile_height,
1242 tile_width;
1243
cristyd40deb62011-03-09 00:52:27 +00001244 ssize_t
1245 tile_y;
1246
cristy3ed852e2009-09-05 21:47:34 +00001247 /*
1248 Rotate 270 degrees.
1249 */
cristyb32b90a2009-09-07 21:45:48 +00001250 GetPixelCacheTileSize(image,&tile_width,&tile_height);
cristy8b8148b2012-10-07 00:41:15 +00001251 tile_width=image->columns;
cristy26b64912012-12-16 18:20:09 +00001252#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy64c10cd2013-01-06 18:40:43 +00001253 #pragma omp parallel for schedule(static,4) shared(status) \
cristycb7dfcc2013-01-06 18:34:59 +00001254 magick_threads(image,image,1,1)
cristy9a5a52f2012-10-09 14:40:31 +00001255#endif
cristyad11aac2011-09-25 21:29:16 +00001256 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
cristy3ed852e2009-09-05 21:47:34 +00001257 {
cristybb503372010-05-27 20:51:26 +00001258 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001259 tile_x;
1260
1261 if (status == MagickFalse)
1262 continue;
cristy26295322011-09-22 00:50:36 +00001263 tile_x=0;
1264 for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
cristy3ed852e2009-09-05 21:47:34 +00001265 {
1266 MagickBooleanType
1267 sync;
1268
cristy4c08aed2011-07-01 19:47:50 +00001269 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001270 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001271
cristy4c08aed2011-07-01 19:47:50 +00001272 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001273 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001274
cristy2be50602011-10-09 01:28:13 +00001275 register ssize_t
1276 y;
1277
cristybb503372010-05-27 20:51:26 +00001278 size_t
cristyb32b90a2009-09-07 21:45:48 +00001279 height,
1280 width;
cristy3ed852e2009-09-05 21:47:34 +00001281
cristyb32b90a2009-09-07 21:45:48 +00001282 width=tile_width;
cristybb503372010-05-27 20:51:26 +00001283 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
cristya45d2ec2011-08-24 13:17:19 +00001284 width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
cristyb32b90a2009-09-07 21:45:48 +00001285 height=tile_height;
cristybb503372010-05-27 20:51:26 +00001286 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
cristya45d2ec2011-08-24 13:17:19 +00001287 height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
1288 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1289 exception);
cristy4c08aed2011-07-01 19:47:50 +00001290 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001291 {
1292 status=MagickFalse;
1293 break;
1294 }
cristybb503372010-05-27 20:51:26 +00001295 for (y=0; y < (ssize_t) width; y++)
cristy3ed852e2009-09-05 21:47:34 +00001296 {
cristy4c08aed2011-07-01 19:47:50 +00001297 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001298 *restrict tile_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001299
cristybb503372010-05-27 20:51:26 +00001300 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001301 x;
1302
cristy27709ef2011-09-18 02:53:53 +00001303 if (status == MagickFalse)
1304 continue;
cristya45d2ec2011-08-24 13:17:19 +00001305 q=QueueCacheViewAuthenticPixels(rotate_view,tile_y,(ssize_t) (y+
1306 rotate_image->rows-(tile_x+width)),height,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001307 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001308 {
1309 status=MagickFalse;
cristy27709ef2011-09-18 02:53:53 +00001310 continue;
cristy3ed852e2009-09-05 21:47:34 +00001311 }
cristyed231572011-07-14 02:18:59 +00001312 tile_pixels=p+((width-1)-y)*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001313 for (x=0; x < (ssize_t) height; x++)
cristy3ed852e2009-09-05 21:47:34 +00001314 {
cristya45d2ec2011-08-24 13:17:19 +00001315 register ssize_t
1316 i;
1317
cristy883fde12013-04-08 00:50:13 +00001318 if (GetPixelReadMask(image,tile_pixels) == 0)
cristy10a6c612012-01-29 21:41:05 +00001319 {
1320 tile_pixels+=width*GetPixelChannels(image);
1321 q+=GetPixelChannels(rotate_image);
1322 continue;
1323 }
cristya45d2ec2011-08-24 13:17:19 +00001324 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1325 {
cristy5a23c552013-02-13 14:34:28 +00001326 PixelChannel channel=GetPixelChannelChannel(image,i);
1327 PixelTrait traits=GetPixelChannelTraits(image,channel);
1328 PixelTrait rotate_traits=GetPixelChannelTraits(rotate_image,
1329 channel);
cristy010d7d12011-08-31 01:02:48 +00001330 if ((traits == UndefinedPixelTrait) ||
1331 (rotate_traits == UndefinedPixelTrait))
cristya45d2ec2011-08-24 13:17:19 +00001332 continue;
cristy0beccfa2011-09-25 20:47:53 +00001333 SetPixelChannel(rotate_image,channel,tile_pixels[i],q);
cristya45d2ec2011-08-24 13:17:19 +00001334 }
cristyed231572011-07-14 02:18:59 +00001335 tile_pixels+=width*GetPixelChannels(image);
1336 q+=GetPixelChannels(rotate_image);
cristy3ed852e2009-09-05 21:47:34 +00001337 }
cristy26b64912012-12-16 18:20:09 +00001338#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy9a5a52f2012-10-09 14:40:31 +00001339 #pragma omp critical (MagickCore_IntegralRotateImage)
1340#endif
cristy3ed852e2009-09-05 21:47:34 +00001341 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1342 if (sync == MagickFalse)
1343 status=MagickFalse;
1344 }
1345 }
1346 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1347 {
1348 MagickBooleanType
1349 proceed;
1350
cristy21e03412011-09-25 22:39:35 +00001351 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
1352 image->rows);
1353 if (proceed == MagickFalse)
1354 status=MagickFalse;
1355 }
1356 }
1357 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1358 image->rows-1,image->rows);
1359 Swap(page.width,page.height);
1360 Swap(page.x,page.y);
1361 if (page.width != 0)
1362 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1363 break;
1364 }
cristy3ed852e2009-09-05 21:47:34 +00001365 }
1366 rotate_view=DestroyCacheView(rotate_view);
1367 image_view=DestroyCacheView(image_view);
1368 rotate_image->type=image->type;
1369 rotate_image->page=page;
1370 if (status == MagickFalse)
1371 rotate_image=DestroyImage(rotate_image);
1372 return(rotate_image);
1373}
1374
1375/*
1376%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1377% %
1378% %
1379% %
1380+ X S h e a r I m a g e %
1381% %
1382% %
1383% %
1384%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1385%
1386% XShearImage() shears the image in the X direction with a shear angle of
1387% 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1388% negative angles shear clockwise. Angles are measured relative to a vertical
1389% Y-axis. X shears will widen an image creating 'empty' triangles on the left
1390% and right sides of the source image.
1391%
1392% The format of the XShearImage method is:
1393%
cristya19f1d72012-08-07 18:24:38 +00001394% MagickBooleanType XShearImage(Image *image,const double degrees,
cristybb503372010-05-27 20:51:26 +00001395% const size_t width,const size_t height,
1396% const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001397%
1398% A description of each parameter follows.
1399%
1400% o image: the image.
1401%
cristya19f1d72012-08-07 18:24:38 +00001402% o degrees: A double representing the shearing angle along the X
cristy3ed852e2009-09-05 21:47:34 +00001403% axis.
1404%
1405% o width, height, x_offset, y_offset: Defines a region of the image
1406% to shear.
1407%
cristyecc2c142010-01-17 22:25:46 +00001408% o exception: return any errors or warnings in this structure.
1409%
cristy3ed852e2009-09-05 21:47:34 +00001410*/
cristya19f1d72012-08-07 18:24:38 +00001411static MagickBooleanType XShearImage(Image *image,const double degrees,
cristybb503372010-05-27 20:51:26 +00001412 const size_t width,const size_t height,const ssize_t x_offset,
1413 const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001414{
1415#define XShearImageTag "XShear/Image"
1416
1417 typedef enum
1418 {
1419 LEFT,
1420 RIGHT
1421 } ShearDirection;
1422
1423 CacheView
1424 *image_view;
1425
cristy3ed852e2009-09-05 21:47:34 +00001426 MagickBooleanType
1427 status;
1428
cristy5f959472010-05-27 22:19:46 +00001429 MagickOffsetType
1430 progress;
1431
cristy4c08aed2011-07-01 19:47:50 +00001432 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001433 background;
1434
cristy5f959472010-05-27 22:19:46 +00001435 ssize_t
1436 y;
1437
cristy9d8c8ce2011-10-25 16:13:52 +00001438 /*
1439 X shear image.
1440 */
cristy3ed852e2009-09-05 21:47:34 +00001441 assert(image != (Image *) NULL);
1442 assert(image->signature == MagickSignature);
1443 if (image->debug != MagickFalse)
1444 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3ed852e2009-09-05 21:47:34 +00001445 status=MagickTrue;
cristy9d8c8ce2011-10-25 16:13:52 +00001446 background=image->background_color;
cristy3ed852e2009-09-05 21:47:34 +00001447 progress=0;
cristy46ff2672012-12-14 15:32:26 +00001448 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00001449#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00001450 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00001451 magick_threads(image,image,height,1)
cristy3ed852e2009-09-05 21:47:34 +00001452#endif
cristybb503372010-05-27 20:51:26 +00001453 for (y=0; y < (ssize_t) height; y++)
cristy3ed852e2009-09-05 21:47:34 +00001454 {
cristy4c08aed2011-07-01 19:47:50 +00001455 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001456 pixel,
1457 source,
1458 destination;
1459
cristya19f1d72012-08-07 18:24:38 +00001460 double
cristy3ed852e2009-09-05 21:47:34 +00001461 area,
1462 displacement;
1463
cristy4c08aed2011-07-01 19:47:50 +00001464 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001465 *restrict p,
1466 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001467
cristyd40deb62011-03-09 00:52:27 +00001468 register ssize_t
1469 i;
1470
cristy3ed852e2009-09-05 21:47:34 +00001471 ShearDirection
1472 direction;
1473
cristyd40deb62011-03-09 00:52:27 +00001474 ssize_t
1475 step;
1476
cristy3ed852e2009-09-05 21:47:34 +00001477 if (status == MagickFalse)
1478 continue;
1479 p=GetCacheViewAuthenticPixels(image_view,0,y_offset+y,image->columns,1,
1480 exception);
cristy4c08aed2011-07-01 19:47:50 +00001481 if (p == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001482 {
1483 status=MagickFalse;
1484 continue;
1485 }
cristyed231572011-07-14 02:18:59 +00001486 p+=x_offset*GetPixelChannels(image);
cristya19f1d72012-08-07 18:24:38 +00001487 displacement=degrees*(double) (y-height/2.0);
cristy3ed852e2009-09-05 21:47:34 +00001488 if (displacement == 0.0)
1489 continue;
1490 if (displacement > 0.0)
1491 direction=RIGHT;
1492 else
1493 {
1494 displacement*=(-1.0);
1495 direction=LEFT;
1496 }
cristybb503372010-05-27 20:51:26 +00001497 step=(ssize_t) floor((double) displacement);
cristya19f1d72012-08-07 18:24:38 +00001498 area=(double) (displacement-step);
cristy3ed852e2009-09-05 21:47:34 +00001499 step++;
1500 pixel=background;
cristy4c08aed2011-07-01 19:47:50 +00001501 GetPixelInfo(image,&source);
1502 GetPixelInfo(image,&destination);
cristy3ed852e2009-09-05 21:47:34 +00001503 switch (direction)
1504 {
1505 case LEFT:
1506 {
1507 /*
1508 Transfer pixels left-to-right.
1509 */
1510 if (step > x_offset)
1511 break;
cristyed231572011-07-14 02:18:59 +00001512 q=p-step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001513 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001514 {
1515 if ((x_offset+i) < step)
1516 {
cristyed231572011-07-14 02:18:59 +00001517 p+=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001518 GetPixelInfoPixel(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001519 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001520 continue;
1521 }
cristy803640d2011-11-17 02:11:32 +00001522 GetPixelInfoPixel(image,p,&source);
cristya19f1d72012-08-07 18:24:38 +00001523 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1524 &source,(double) GetPixelAlpha(image,p),area,&destination);
cristy803640d2011-11-17 02:11:32 +00001525 SetPixelInfoPixel(image,&destination,q);
1526 GetPixelInfoPixel(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001527 p+=GetPixelChannels(image);
1528 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001529 }
cristya19f1d72012-08-07 18:24:38 +00001530 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1531 &background,(double) background.alpha,area,&destination);
cristy803640d2011-11-17 02:11:32 +00001532 SetPixelInfoPixel(image,&destination,q);
cristyed231572011-07-14 02:18:59 +00001533 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001534 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001535 {
cristy803640d2011-11-17 02:11:32 +00001536 SetPixelInfoPixel(image,&background,q);
cristyed231572011-07-14 02:18:59 +00001537 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001538 }
cristy3ed852e2009-09-05 21:47:34 +00001539 break;
1540 }
1541 case RIGHT:
1542 {
1543 /*
1544 Transfer pixels right-to-left.
1545 */
cristyed231572011-07-14 02:18:59 +00001546 p+=width*GetPixelChannels(image);
1547 q=p+step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001548 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001549 {
cristyed231572011-07-14 02:18:59 +00001550 p-=GetPixelChannels(image);
1551 q-=GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001552 if ((size_t) (x_offset+width+step-i) >= image->columns)
cristy3ed852e2009-09-05 21:47:34 +00001553 continue;
cristy803640d2011-11-17 02:11:32 +00001554 GetPixelInfoPixel(image,p,&source);
cristya19f1d72012-08-07 18:24:38 +00001555 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1556 &source,(double) GetPixelAlpha(image,p),area,&destination);
cristy803640d2011-11-17 02:11:32 +00001557 SetPixelInfoPixel(image,&destination,q);
1558 GetPixelInfoPixel(image,p,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001559 }
cristya19f1d72012-08-07 18:24:38 +00001560 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1561 &background,(double) background.alpha,area,&destination);
cristyed231572011-07-14 02:18:59 +00001562 q-=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001563 SetPixelInfoPixel(image,&destination,q);
cristy3ed852e2009-09-05 21:47:34 +00001564 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001565 {
cristyed231572011-07-14 02:18:59 +00001566 q-=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001567 SetPixelInfoPixel(image,&background,q);
cristy4c08aed2011-07-01 19:47:50 +00001568 }
cristy3ed852e2009-09-05 21:47:34 +00001569 break;
1570 }
1571 }
1572 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1573 status=MagickFalse;
1574 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1575 {
1576 MagickBooleanType
1577 proceed;
1578
cristyb5d5f722009-11-04 03:03:49 +00001579#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy7b650b52011-10-09 01:13:39 +00001580 #pragma omp critical (MagickCore_XShearImage)
cristy3ed852e2009-09-05 21:47:34 +00001581#endif
1582 proceed=SetImageProgress(image,XShearImageTag,progress++,height);
1583 if (proceed == MagickFalse)
1584 status=MagickFalse;
1585 }
1586 }
1587 image_view=DestroyCacheView(image_view);
1588 return(status);
1589}
1590
1591/*
1592%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1593% %
1594% %
1595% %
1596+ Y S h e a r I m a g e %
1597% %
1598% %
1599% %
1600%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1601%
1602% YShearImage shears the image in the Y direction with a shear angle of
1603% 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1604% negative angles shear clockwise. Angles are measured relative to a
1605% horizontal X-axis. Y shears will increase the height of an image creating
1606% 'empty' triangles on the top and bottom of the source image.
1607%
1608% The format of the YShearImage method is:
1609%
cristya19f1d72012-08-07 18:24:38 +00001610% MagickBooleanType YShearImage(Image *image,const double degrees,
cristybb503372010-05-27 20:51:26 +00001611% const size_t width,const size_t height,
1612% const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001613%
1614% A description of each parameter follows.
1615%
1616% o image: the image.
1617%
cristya19f1d72012-08-07 18:24:38 +00001618% o degrees: A double representing the shearing angle along the Y
cristy3ed852e2009-09-05 21:47:34 +00001619% axis.
1620%
1621% o width, height, x_offset, y_offset: Defines a region of the image
1622% to shear.
1623%
cristyecc2c142010-01-17 22:25:46 +00001624% o exception: return any errors or warnings in this structure.
1625%
cristy3ed852e2009-09-05 21:47:34 +00001626*/
cristya19f1d72012-08-07 18:24:38 +00001627static MagickBooleanType YShearImage(Image *image,const double degrees,
cristybb503372010-05-27 20:51:26 +00001628 const size_t width,const size_t height,const ssize_t x_offset,
1629 const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001630{
1631#define YShearImageTag "YShear/Image"
1632
1633 typedef enum
1634 {
1635 UP,
1636 DOWN
1637 } ShearDirection;
1638
1639 CacheView
1640 *image_view;
1641
cristy3ed852e2009-09-05 21:47:34 +00001642 MagickBooleanType
1643 status;
1644
cristy5f959472010-05-27 22:19:46 +00001645 MagickOffsetType
1646 progress;
1647
cristy4c08aed2011-07-01 19:47:50 +00001648 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001649 background;
1650
cristy5f959472010-05-27 22:19:46 +00001651 ssize_t
1652 x;
1653
cristy9d8c8ce2011-10-25 16:13:52 +00001654 /*
1655 Y Shear image.
1656 */
cristy3ed852e2009-09-05 21:47:34 +00001657 assert(image != (Image *) NULL);
1658 assert(image->signature == MagickSignature);
1659 if (image->debug != MagickFalse)
1660 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3ed852e2009-09-05 21:47:34 +00001661 status=MagickTrue;
1662 progress=0;
cristy9d8c8ce2011-10-25 16:13:52 +00001663 background=image->background_color;
cristy46ff2672012-12-14 15:32:26 +00001664 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00001665#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00001666 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00001667 magick_threads(image,image,width,1)
cristy3ed852e2009-09-05 21:47:34 +00001668#endif
cristybb503372010-05-27 20:51:26 +00001669 for (x=0; x < (ssize_t) width; x++)
cristy3ed852e2009-09-05 21:47:34 +00001670 {
cristybb503372010-05-27 20:51:26 +00001671 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001672 step;
1673
cristya19f1d72012-08-07 18:24:38 +00001674 double
cristy3ed852e2009-09-05 21:47:34 +00001675 area,
1676 displacement;
1677
cristy4c08aed2011-07-01 19:47:50 +00001678 PixelInfo
1679 pixel,
1680 source,
1681 destination;
1682
1683 register Quantum
1684 *restrict p,
1685 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001686
cristybb503372010-05-27 20:51:26 +00001687 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001688 i;
1689
cristy3ed852e2009-09-05 21:47:34 +00001690 ShearDirection
1691 direction;
1692
1693 if (status == MagickFalse)
1694 continue;
1695 p=GetCacheViewAuthenticPixels(image_view,x_offset+x,0,1,image->rows,
1696 exception);
cristy4c08aed2011-07-01 19:47:50 +00001697 if (p == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001698 {
1699 status=MagickFalse;
1700 continue;
1701 }
cristyed231572011-07-14 02:18:59 +00001702 p+=y_offset*GetPixelChannels(image);
cristya19f1d72012-08-07 18:24:38 +00001703 displacement=degrees*(double) (x-width/2.0);
cristy3ed852e2009-09-05 21:47:34 +00001704 if (displacement == 0.0)
1705 continue;
1706 if (displacement > 0.0)
1707 direction=DOWN;
1708 else
1709 {
1710 displacement*=(-1.0);
1711 direction=UP;
1712 }
cristybb503372010-05-27 20:51:26 +00001713 step=(ssize_t) floor((double) displacement);
cristya19f1d72012-08-07 18:24:38 +00001714 area=(double) (displacement-step);
cristy3ed852e2009-09-05 21:47:34 +00001715 step++;
1716 pixel=background;
cristy4c08aed2011-07-01 19:47:50 +00001717 GetPixelInfo(image,&source);
1718 GetPixelInfo(image,&destination);
cristy3ed852e2009-09-05 21:47:34 +00001719 switch (direction)
1720 {
1721 case UP:
1722 {
1723 /*
1724 Transfer pixels top-to-bottom.
1725 */
1726 if (step > y_offset)
1727 break;
cristyed231572011-07-14 02:18:59 +00001728 q=p-step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001729 for (i=0; i < (ssize_t) height; i++)
cristy3ed852e2009-09-05 21:47:34 +00001730 {
1731 if ((y_offset+i) < step)
1732 {
cristyed231572011-07-14 02:18:59 +00001733 p+=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001734 GetPixelInfoPixel(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001735 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001736 continue;
1737 }
cristy803640d2011-11-17 02:11:32 +00001738 GetPixelInfoPixel(image,p,&source);
cristya19f1d72012-08-07 18:24:38 +00001739 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1740 &source,(double) GetPixelAlpha(image,p),area,
cristy4c08aed2011-07-01 19:47:50 +00001741 &destination);
cristy803640d2011-11-17 02:11:32 +00001742 SetPixelInfoPixel(image,&destination,q);
1743 GetPixelInfoPixel(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001744 p+=GetPixelChannels(image);
1745 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001746 }
cristya19f1d72012-08-07 18:24:38 +00001747 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1748 &background,(double) background.alpha,area,&destination);
cristy803640d2011-11-17 02:11:32 +00001749 SetPixelInfoPixel(image,&destination,q);
cristyed231572011-07-14 02:18:59 +00001750 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001751 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001752 {
cristy803640d2011-11-17 02:11:32 +00001753 SetPixelInfoPixel(image,&background,q);
cristyed231572011-07-14 02:18:59 +00001754 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001755 }
cristy3ed852e2009-09-05 21:47:34 +00001756 break;
1757 }
1758 case DOWN:
1759 {
1760 /*
1761 Transfer pixels bottom-to-top.
1762 */
cristyed231572011-07-14 02:18:59 +00001763 p+=height*GetPixelChannels(image);
1764 q=p+step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001765 for (i=0; i < (ssize_t) height; i++)
cristy3ed852e2009-09-05 21:47:34 +00001766 {
cristyed231572011-07-14 02:18:59 +00001767 p-=GetPixelChannels(image);
1768 q-=GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001769 if ((size_t) (y_offset+height+step-i) >= image->rows)
cristy3ed852e2009-09-05 21:47:34 +00001770 continue;
cristy803640d2011-11-17 02:11:32 +00001771 GetPixelInfoPixel(image,p,&source);
cristya19f1d72012-08-07 18:24:38 +00001772 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1773 &source,(double) GetPixelAlpha(image,p),area,
cristy4c08aed2011-07-01 19:47:50 +00001774 &destination);
cristy803640d2011-11-17 02:11:32 +00001775 SetPixelInfoPixel(image,&destination,q);
1776 GetPixelInfoPixel(image,p,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001777 }
cristya19f1d72012-08-07 18:24:38 +00001778 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1779 &background,(double) background.alpha,area,&destination);
cristyed231572011-07-14 02:18:59 +00001780 q-=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001781 SetPixelInfoPixel(image,&destination,q);
cristy3ed852e2009-09-05 21:47:34 +00001782 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001783 {
cristyed231572011-07-14 02:18:59 +00001784 q-=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001785 SetPixelInfoPixel(image,&background,q);
cristy4c08aed2011-07-01 19:47:50 +00001786 }
cristy3ed852e2009-09-05 21:47:34 +00001787 break;
1788 }
1789 }
1790 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1791 status=MagickFalse;
1792 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1793 {
1794 MagickBooleanType
1795 proceed;
1796
cristyb5d5f722009-11-04 03:03:49 +00001797#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy7b650b52011-10-09 01:13:39 +00001798 #pragma omp critical (MagickCore_YShearImage)
cristy3ed852e2009-09-05 21:47:34 +00001799#endif
1800 proceed=SetImageProgress(image,YShearImageTag,progress++,image->rows);
1801 if (proceed == MagickFalse)
1802 status=MagickFalse;
1803 }
1804 }
1805 image_view=DestroyCacheView(image_view);
1806 return(status);
1807}
1808
1809/*
1810%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1811% %
1812% %
1813% %
cristy3ed852e2009-09-05 21:47:34 +00001814% S h e a r I m a g e %
1815% %
1816% %
1817% %
1818%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1819%
1820% ShearImage() creates a new image that is a shear_image copy of an existing
cristycee97112010-05-28 00:44:52 +00001821% one. Shearing slides one edge of an image along the X or Y axis, creating
1822% a parallelogram. An X direction shear slides an edge along the X axis,
1823% while a Y direction shear slides an edge along the Y axis. The amount of
cristy3ed852e2009-09-05 21:47:34 +00001824% the shear is controlled by a shear angle. For X direction shears, x_shear
1825% is measured relative to the Y axis, and similarly, for Y direction shears
1826% y_shear is measured relative to the X axis. Empty triangles left over from
1827% shearing the image are filled with the background color defined by member
1828% 'background_color' of the image.. ShearImage() allocates the memory
1829% necessary for the new Image structure and returns a pointer to the new image.
1830%
1831% ShearImage() is based on the paper "A Fast Algorithm for General Raster
1832% Rotatation" by Alan W. Paeth.
1833%
1834% The format of the ShearImage method is:
1835%
1836% Image *ShearImage(const Image *image,const double x_shear,
1837% const double y_shear,ExceptionInfo *exception)
1838%
1839% A description of each parameter follows.
1840%
1841% o image: the image.
1842%
1843% o x_shear, y_shear: Specifies the number of degrees to shear the image.
1844%
1845% o exception: return any errors or warnings in this structure.
1846%
1847*/
1848MagickExport Image *ShearImage(const Image *image,const double x_shear,
1849 const double y_shear,ExceptionInfo *exception)
1850{
1851 Image
1852 *integral_image,
1853 *shear_image;
1854
cristybb503372010-05-27 20:51:26 +00001855 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001856 x_offset,
1857 y_offset;
1858
cristyecc2c142010-01-17 22:25:46 +00001859 MagickBooleanType
1860 status;
1861
cristy3ed852e2009-09-05 21:47:34 +00001862 PointInfo
1863 shear;
1864
1865 RectangleInfo
1866 border_info;
1867
cristybb503372010-05-27 20:51:26 +00001868 size_t
cristy3ed852e2009-09-05 21:47:34 +00001869 y_width;
1870
1871 assert(image != (Image *) NULL);
1872 assert(image->signature == MagickSignature);
1873 if (image->debug != MagickFalse)
1874 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1875 assert(exception != (ExceptionInfo *) NULL);
1876 assert(exception->signature == MagickSignature);
1877 if ((x_shear != 0.0) && (fmod(x_shear,90.0) == 0.0))
1878 ThrowImageException(ImageError,"AngleIsDiscontinuous");
1879 if ((y_shear != 0.0) && (fmod(y_shear,90.0) == 0.0))
1880 ThrowImageException(ImageError,"AngleIsDiscontinuous");
1881 /*
1882 Initialize shear angle.
1883 */
1884 integral_image=CloneImage(image,0,0,MagickTrue,exception);
1885 if (integral_image == (Image *) NULL)
1886 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1887 shear.x=(-tan(DegreesToRadians(fmod(x_shear,360.0))));
1888 shear.y=tan(DegreesToRadians(fmod(y_shear,360.0)));
1889 if ((shear.x == 0.0) && (shear.y == 0.0))
1890 return(integral_image);
cristy574cc262011-08-05 01:23:58 +00001891 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001892 {
cristy3ed852e2009-09-05 21:47:34 +00001893 integral_image=DestroyImage(integral_image);
1894 return(integral_image);
1895 }
cristy8a46d822012-08-28 23:32:39 +00001896 if (integral_image->alpha_trait != BlendPixelTrait)
cristy63240882011-08-05 19:05:27 +00001897 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00001898 /*
1899 Compute image size.
1900 */
cristybb503372010-05-27 20:51:26 +00001901 y_width=image->columns+(ssize_t) floor(fabs(shear.x)*image->rows+0.5);
cristycee97112010-05-28 00:44:52 +00001902 x_offset=(ssize_t) ceil((double) image->columns+((fabs(shear.x)*image->rows)-
cristy06609ee2010-03-17 20:21:27 +00001903 image->columns)/2.0-0.5);
cristycee97112010-05-28 00:44:52 +00001904 y_offset=(ssize_t) ceil((double) image->rows+((fabs(shear.y)*y_width)-
1905 image->rows)/2.0-0.5);
cristy3ed852e2009-09-05 21:47:34 +00001906 /*
1907 Surround image with border.
1908 */
1909 integral_image->border_color=integral_image->background_color;
1910 integral_image->compose=CopyCompositeOp;
cristybb503372010-05-27 20:51:26 +00001911 border_info.width=(size_t) x_offset;
1912 border_info.height=(size_t) y_offset;
cristy633f0c62011-09-15 13:27:36 +00001913 shear_image=BorderImage(integral_image,&border_info,image->compose,exception);
cristyecc2c142010-01-17 22:25:46 +00001914 integral_image=DestroyImage(integral_image);
cristy3ed852e2009-09-05 21:47:34 +00001915 if (shear_image == (Image *) NULL)
1916 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +00001917 /*
1918 Shear the image.
1919 */
cristy8a46d822012-08-28 23:32:39 +00001920 if (shear_image->alpha_trait != BlendPixelTrait)
cristy63240882011-08-05 19:05:27 +00001921 (void) SetImageAlphaChannel(shear_image,OpaqueAlphaChannel,exception);
cristyecc2c142010-01-17 22:25:46 +00001922 status=XShearImage(shear_image,shear.x,image->columns,image->rows,x_offset,
cristyeaedf062010-05-29 22:36:02 +00001923 (ssize_t) (shear_image->rows-image->rows)/2,exception);
cristyecc2c142010-01-17 22:25:46 +00001924 if (status == MagickFalse)
1925 {
1926 shear_image=DestroyImage(shear_image);
1927 return((Image *) NULL);
1928 }
cristyeaedf062010-05-29 22:36:02 +00001929 status=YShearImage(shear_image,shear.y,y_width,image->rows,(ssize_t)
1930 (shear_image->columns-y_width)/2,y_offset,exception);
cristyecc2c142010-01-17 22:25:46 +00001931 if (status == MagickFalse)
1932 {
1933 shear_image=DestroyImage(shear_image);
1934 return((Image *) NULL);
1935 }
cristya19f1d72012-08-07 18:24:38 +00001936 status=CropToFitImage(&shear_image,shear.x,shear.y,(double)
1937 image->columns,(double) image->rows,MagickFalse,exception);
cristy3ed852e2009-09-05 21:47:34 +00001938 shear_image->compose=image->compose;
1939 shear_image->page.width=0;
1940 shear_image->page.height=0;
cristy1c2f48d2012-12-14 01:20:55 +00001941 if (status == MagickFalse)
1942 shear_image=DestroyImage(shear_image);
cristy3ed852e2009-09-05 21:47:34 +00001943 return(shear_image);
1944}
cristyc7c81142011-11-07 12:14:23 +00001945
1946/*
1947%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1948% %
1949% %
1950% %
1951% S h e a r R o t a t e I m a g e %
1952% %
1953% %
1954% %
1955%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1956%
1957% ShearRotateImage() creates a new image that is a rotated copy of an existing
1958% one. Positive angles rotate counter-clockwise (right-hand rule), while
1959% negative angles rotate clockwise. Rotated images are usually larger than
1960% the originals and have 'empty' triangular corners. X axis. Empty
1961% triangles left over from shearing the image are filled with the background
1962% color defined by member 'background_color' of the image. ShearRotateImage
1963% allocates the memory necessary for the new Image structure and returns a
1964% pointer to the new image.
1965%
1966% ShearRotateImage() is based on the paper "A Fast Algorithm for General
1967% Raster Rotatation" by Alan W. Paeth. ShearRotateImage is adapted from a
1968% similar method based on the Paeth paper written by Michael Halle of the
1969% Spatial Imaging Group, MIT Media Lab.
1970%
1971% The format of the ShearRotateImage method is:
1972%
1973% Image *ShearRotateImage(const Image *image,const double degrees,
1974% ExceptionInfo *exception)
1975%
1976% A description of each parameter follows.
1977%
1978% o image: the image.
1979%
1980% o degrees: Specifies the number of degrees to rotate the image.
1981%
1982% o exception: return any errors or warnings in this structure.
1983%
1984*/
1985MagickExport Image *ShearRotateImage(const Image *image,const double degrees,
1986 ExceptionInfo *exception)
1987{
1988 Image
1989 *integral_image,
1990 *rotate_image;
1991
1992 MagickBooleanType
1993 status;
1994
cristya19f1d72012-08-07 18:24:38 +00001995 double
cristyc7c81142011-11-07 12:14:23 +00001996 angle;
1997
1998 PointInfo
1999 shear;
2000
2001 RectangleInfo
2002 border_info;
2003
2004 size_t
2005 height,
2006 rotations,
2007 width,
2008 y_width;
2009
2010 ssize_t
2011 x_offset,
2012 y_offset;
2013
2014 /*
2015 Adjust rotation angle.
2016 */
2017 assert(image != (Image *) NULL);
2018 assert(image->signature == MagickSignature);
2019 if (image->debug != MagickFalse)
2020 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2021 assert(exception != (ExceptionInfo *) NULL);
2022 assert(exception->signature == MagickSignature);
2023 angle=degrees;
2024 while (angle < -45.0)
2025 angle+=360.0;
2026 for (rotations=0; angle > 45.0; rotations++)
2027 angle-=90.0;
2028 rotations%=4;
2029 /*
2030 Calculate shear equations.
2031 */
2032 integral_image=IntegralRotateImage(image,rotations,exception);
2033 if (integral_image == (Image *) NULL)
2034 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2035 shear.x=(-tan((double) DegreesToRadians(angle)/2.0));
2036 shear.y=sin((double) DegreesToRadians(angle));
2037 if ((shear.x == 0.0) && (shear.y == 0.0))
2038 return(integral_image);
2039 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
2040 {
2041 integral_image=DestroyImage(integral_image);
2042 return(integral_image);
2043 }
cristy8a46d822012-08-28 23:32:39 +00002044 if (integral_image->alpha_trait != BlendPixelTrait)
cristyc7c81142011-11-07 12:14:23 +00002045 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
2046 /*
2047 Compute image size.
2048 */
2049 width=image->columns;
2050 height=image->rows;
2051 if ((rotations == 1) || (rotations == 3))
2052 {
2053 width=image->rows;
2054 height=image->columns;
2055 }
2056 y_width=width+(ssize_t) floor(fabs(shear.x)*height+0.5);
2057 x_offset=(ssize_t) ceil((double) width+((fabs(shear.y)*height)-width)/2.0-
2058 0.5);
2059 y_offset=(ssize_t) ceil((double) height+((fabs(shear.y)*y_width)-height)/2.0-
2060 0.5);
2061 /*
2062 Surround image with a border.
2063 */
2064 integral_image->border_color=integral_image->background_color;
2065 integral_image->compose=CopyCompositeOp;
2066 border_info.width=(size_t) x_offset;
2067 border_info.height=(size_t) y_offset;
2068 rotate_image=BorderImage(integral_image,&border_info,image->compose,
2069 exception);
2070 integral_image=DestroyImage(integral_image);
2071 if (rotate_image == (Image *) NULL)
2072 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2073 /*
2074 Rotate the image.
2075 */
2076 status=XShearImage(rotate_image,shear.x,width,height,x_offset,(ssize_t)
2077 (rotate_image->rows-height)/2,exception);
2078 if (status == MagickFalse)
2079 {
2080 rotate_image=DestroyImage(rotate_image);
2081 return((Image *) NULL);
2082 }
2083 status=YShearImage(rotate_image,shear.y,y_width,height,(ssize_t)
2084 (rotate_image->columns-y_width)/2,y_offset,exception);
2085 if (status == MagickFalse)
2086 {
2087 rotate_image=DestroyImage(rotate_image);
2088 return((Image *) NULL);
2089 }
2090 status=XShearImage(rotate_image,shear.x,y_width,rotate_image->rows,(ssize_t)
2091 (rotate_image->columns-y_width)/2,0,exception);
2092 if (status == MagickFalse)
2093 {
2094 rotate_image=DestroyImage(rotate_image);
2095 return((Image *) NULL);
2096 }
cristya19f1d72012-08-07 18:24:38 +00002097 status=CropToFitImage(&rotate_image,shear.x,shear.y,(double) width,
2098 (double) height,MagickTrue,exception);
cristyc7c81142011-11-07 12:14:23 +00002099 rotate_image->compose=image->compose;
2100 rotate_image->page.width=0;
2101 rotate_image->page.height=0;
cristy1c2f48d2012-12-14 01:20:55 +00002102 if (status == MagickFalse)
2103 rotate_image=DestroyImage(rotate_image);
cristyc7c81142011-11-07 12:14:23 +00002104 return(rotate_image);
2105}