blob: 143e002eb8f4b6dad27cce9b1fb56c66620cc4b8 [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%
anthonye92872a2013-04-30 01:49:12 +0000199% If the artifact "deskew:auto-crop" is given the image will be automatically
200% cropped of the excess background. The value is the border width of all
201% pixels around the edge that will be used to determine an average border
202% color for the automatic trim.
anthony8a95e802013-04-10 05:31:57 +0000203%
cristy3ed852e2009-09-05 21:47:34 +0000204% The format of the DeskewImage method is:
205%
206% Image *DeskewImage(const Image *image,const double threshold,
207% ExceptionInfo *exception)
208%
209% A description of each parameter follows:
210%
211% o image: the image.
212%
213% o threshold: separate background from foreground.
214%
215% o exception: return any errors or warnings in this structure.
216%
217*/
218
219typedef struct _RadonInfo
220{
221 CacheType
222 type;
223
cristybb503372010-05-27 20:51:26 +0000224 size_t
cristy3ed852e2009-09-05 21:47:34 +0000225 width,
226 height;
227
228 MagickSizeType
229 length;
230
231 MagickBooleanType
232 mapped;
233
234 char
235 path[MaxTextExtent];
236
237 int
238 file;
239
240 unsigned short
241 *cells;
242} RadonInfo;
243
244static RadonInfo *DestroyRadonInfo(RadonInfo *radon_info)
245{
246 assert(radon_info != (RadonInfo *) NULL);
247 switch (radon_info->type)
248 {
249 case MemoryCache:
250 {
251 if (radon_info->mapped == MagickFalse)
252 radon_info->cells=(unsigned short *) RelinquishMagickMemory(
253 radon_info->cells);
254 else
255 radon_info->cells=(unsigned short *) UnmapBlob(radon_info->cells,
256 (size_t) radon_info->length);
257 RelinquishMagickResource(MemoryResource,radon_info->length);
258 break;
259 }
260 case MapCache:
261 {
262 radon_info->cells=(unsigned short *) UnmapBlob(radon_info->cells,(size_t)
263 radon_info->length);
264 RelinquishMagickResource(MapResource,radon_info->length);
265 }
266 case DiskCache:
267 {
268 if (radon_info->file != -1)
269 (void) close(radon_info->file);
270 (void) RelinquishUniqueFileResource(radon_info->path);
271 RelinquishMagickResource(DiskResource,radon_info->length);
272 break;
273 }
274 default:
275 break;
276 }
277 return((RadonInfo *) RelinquishMagickMemory(radon_info));
278}
279
280static MagickBooleanType ResetRadonCells(RadonInfo *radon_info)
281{
cristybb503372010-05-27 20:51:26 +0000282 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000283 x;
284
285 ssize_t
cristyd40deb62011-03-09 00:52:27 +0000286 count,
287 y;
cristy3ed852e2009-09-05 21:47:34 +0000288
289 unsigned short
290 value;
291
292 if (radon_info->type != DiskCache)
293 {
294 (void) ResetMagickMemory(radon_info->cells,0,(size_t) radon_info->length);
295 return(MagickTrue);
296 }
297 value=0;
cristy7f317702011-02-18 20:40:28 +0000298 (void) lseek(radon_info->file,0,SEEK_SET);
cristybb503372010-05-27 20:51:26 +0000299 for (y=0; y < (ssize_t) radon_info->height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000300 {
cristybb503372010-05-27 20:51:26 +0000301 for (x=0; x < (ssize_t) radon_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000302 {
303 count=write(radon_info->file,&value,sizeof(*radon_info->cells));
304 if (count != (ssize_t) sizeof(*radon_info->cells))
305 break;
306 }
cristybb503372010-05-27 20:51:26 +0000307 if (x < (ssize_t) radon_info->width)
cristy3ed852e2009-09-05 21:47:34 +0000308 break;
309 }
cristybb503372010-05-27 20:51:26 +0000310 return(y < (ssize_t) radon_info->height ? MagickFalse : MagickTrue);
cristy3ed852e2009-09-05 21:47:34 +0000311}
312
cristybb503372010-05-27 20:51:26 +0000313static RadonInfo *AcquireRadonInfo(const Image *image,const size_t width,
314 const size_t height,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000315{
316 MagickBooleanType
317 status;
318
319 RadonInfo
320 *radon_info;
321
cristy73bd4a52010-10-05 11:24:23 +0000322 radon_info=(RadonInfo *) AcquireMagickMemory(sizeof(*radon_info));
cristy3ed852e2009-09-05 21:47:34 +0000323 if (radon_info == (RadonInfo *) NULL)
324 return((RadonInfo *) NULL);
325 (void) ResetMagickMemory(radon_info,0,sizeof(*radon_info));
326 radon_info->width=width;
327 radon_info->height=height;
328 radon_info->length=(MagickSizeType) width*height*sizeof(*radon_info->cells);
329 radon_info->type=MemoryCache;
330 status=AcquireMagickResource(AreaResource,radon_info->length);
331 if ((status != MagickFalse) &&
332 (radon_info->length == (MagickSizeType) ((size_t) radon_info->length)))
333 {
334 status=AcquireMagickResource(MemoryResource,radon_info->length);
335 if (status != MagickFalse)
336 {
337 radon_info->mapped=MagickFalse;
338 radon_info->cells=(unsigned short *) AcquireMagickMemory((size_t)
339 radon_info->length);
340 if (radon_info->cells == (unsigned short *) NULL)
341 {
342 radon_info->mapped=MagickTrue;
343 radon_info->cells=(unsigned short *) MapBlob(-1,IOMode,0,(size_t)
344 radon_info->length);
345 }
346 if (radon_info->cells == (unsigned short *) NULL)
347 RelinquishMagickResource(MemoryResource,radon_info->length);
348 }
349 }
350 radon_info->file=(-1);
351 if (radon_info->cells == (unsigned short *) NULL)
352 {
353 status=AcquireMagickResource(DiskResource,radon_info->length);
354 if (status == MagickFalse)
355 {
356 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
cristyefe601c2013-01-05 17:51:12 +0000357 "CacheResourcesExhausted","`%s'",image->filename);
cristy3ed852e2009-09-05 21:47:34 +0000358 return(DestroyRadonInfo(radon_info));
359 }
360 radon_info->type=DiskCache;
361 (void) AcquireMagickResource(MemoryResource,radon_info->length);
362 radon_info->file=AcquireUniqueFileResource(radon_info->path);
363 if (radon_info->file == -1)
364 return(DestroyRadonInfo(radon_info));
365 status=AcquireMagickResource(MapResource,radon_info->length);
366 if (status != MagickFalse)
367 {
368 status=ResetRadonCells(radon_info);
369 if (status != MagickFalse)
370 {
371 radon_info->cells=(unsigned short *) MapBlob(radon_info->file,
372 IOMode,0,(size_t) radon_info->length);
373 if (radon_info->cells != (unsigned short *) NULL)
374 radon_info->type=MapCache;
375 else
376 RelinquishMagickResource(MapResource,radon_info->length);
377 }
378 }
379 }
380 return(radon_info);
381}
382
383static inline size_t MagickMin(const size_t x,const size_t y)
384{
385 if (x < y)
386 return(x);
387 return(y);
388}
389
390static inline ssize_t ReadRadonCell(const RadonInfo *radon_info,
cristy2c38b272011-02-18 23:25:33 +0000391 const MagickOffsetType offset,const size_t length,unsigned char *buffer)
cristy3ed852e2009-09-05 21:47:34 +0000392{
393 register ssize_t
394 i;
395
396 ssize_t
397 count;
398
399#if !defined(MAGICKCORE_HAVE_PPREAD)
cristyb5d5f722009-11-04 03:03:49 +0000400#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000401 #pragma omp critical (MagickCore_ReadRadonCell)
402#endif
403 {
404 i=(-1);
cristy7f317702011-02-18 20:40:28 +0000405 if (lseek(radon_info->file,offset,SEEK_SET) >= 0)
cristy3ed852e2009-09-05 21:47:34 +0000406 {
407#endif
408 count=0;
409 for (i=0; i < (ssize_t) length; i+=count)
410 {
411#if !defined(MAGICKCORE_HAVE_PPREAD)
412 count=read(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
413 SSIZE_MAX));
414#else
415 count=pread(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
cristy2c38b272011-02-18 23:25:33 +0000416 SSIZE_MAX),offset+i);
cristy3ed852e2009-09-05 21:47:34 +0000417#endif
418 if (count > 0)
419 continue;
420 count=0;
421 if (errno != EINTR)
422 {
423 i=(-1);
424 break;
425 }
426 }
427#if !defined(MAGICKCORE_HAVE_PPREAD)
428 }
429 }
430#endif
431 return(i);
432}
433
434static inline ssize_t WriteRadonCell(const RadonInfo *radon_info,
cristy2c38b272011-02-18 23:25:33 +0000435 const MagickOffsetType offset,const size_t length,const unsigned char *buffer)
cristy3ed852e2009-09-05 21:47:34 +0000436{
437 register ssize_t
438 i;
439
440 ssize_t
441 count;
442
443#if !defined(MAGICKCORE_HAVE_PWRITE)
cristyb5d5f722009-11-04 03:03:49 +0000444#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000445 #pragma omp critical (MagickCore_WriteRadonCell)
446#endif
447 {
cristy7f317702011-02-18 20:40:28 +0000448 if (lseek(radon_info->file,offset,SEEK_SET) >= 0)
cristy3ed852e2009-09-05 21:47:34 +0000449 {
450#endif
451 count=0;
452 for (i=0; i < (ssize_t) length; i+=count)
453 {
454#if !defined(MAGICKCORE_HAVE_PWRITE)
455 count=write(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
456 SSIZE_MAX));
457#else
458 count=pwrite(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
cristy2c38b272011-02-18 23:25:33 +0000459 SSIZE_MAX),offset+i);
cristy3ed852e2009-09-05 21:47:34 +0000460#endif
461 if (count > 0)
462 continue;
463 count=0;
464 if (errno != EINTR)
465 {
466 i=(-1);
467 break;
468 }
469 }
470#if !defined(MAGICKCORE_HAVE_PWRITE)
471 }
472 }
473#endif
474 return(i);
475}
476
477static inline unsigned short GetRadonCell(const RadonInfo *radon_info,
cristybb503372010-05-27 20:51:26 +0000478 const ssize_t x,const ssize_t y)
cristy3ed852e2009-09-05 21:47:34 +0000479{
cristy2c38b272011-02-18 23:25:33 +0000480 MagickOffsetType
cristy3ed852e2009-09-05 21:47:34 +0000481 i;
482
483 unsigned short
484 value;
485
cristy2c38b272011-02-18 23:25:33 +0000486 i=(MagickOffsetType) radon_info->height*x+y;
cristy3ed852e2009-09-05 21:47:34 +0000487 if ((i < 0) ||
488 ((MagickSizeType) (i*sizeof(*radon_info->cells)) >= radon_info->length))
489 return(0);
490 if (radon_info->type != DiskCache)
491 return(radon_info->cells[i]);
492 value=0;
493 (void) ReadRadonCell(radon_info,i*sizeof(*radon_info->cells),
494 sizeof(*radon_info->cells),(unsigned char *) &value);
495 return(value);
496}
497
498static inline MagickBooleanType SetRadonCell(const RadonInfo *radon_info,
cristybb503372010-05-27 20:51:26 +0000499 const ssize_t x,const ssize_t y,const unsigned short value)
cristy3ed852e2009-09-05 21:47:34 +0000500{
cristy2c38b272011-02-18 23:25:33 +0000501 MagickOffsetType
cristy3ed852e2009-09-05 21:47:34 +0000502 i;
503
504 ssize_t
505 count;
506
cristy2c38b272011-02-18 23:25:33 +0000507 i=(MagickOffsetType) radon_info->height*x+y;
cristy3ed852e2009-09-05 21:47:34 +0000508 if ((i < 0) ||
509 ((MagickSizeType) (i*sizeof(*radon_info->cells)) >= radon_info->length))
510 return(MagickFalse);
511 if (radon_info->type != DiskCache)
512 {
513 radon_info->cells[i]=value;
514 return(MagickTrue);
515 }
516 count=WriteRadonCell(radon_info,i*sizeof(*radon_info->cells),
cristy44807bb2009-09-19 18:28:06 +0000517 sizeof(*radon_info->cells),(const unsigned char *) &value);
cristy3ed852e2009-09-05 21:47:34 +0000518 if (count != (ssize_t) sizeof(*radon_info->cells))
519 return(MagickFalse);
520 return(MagickTrue);
521}
522
cristy4ee2b0c2012-05-15 00:30:35 +0000523static void RadonProjection(const Image *image,RadonInfo *source_cells,
cristybb503372010-05-27 20:51:26 +0000524 RadonInfo *destination_cells,const ssize_t sign,size_t *projection)
cristy3ed852e2009-09-05 21:47:34 +0000525{
526 RadonInfo
527 *swap;
528
cristybb503372010-05-27 20:51:26 +0000529 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000530 x;
531
532 register RadonInfo
533 *p,
534 *q;
535
cristybb503372010-05-27 20:51:26 +0000536 size_t
cristy3ed852e2009-09-05 21:47:34 +0000537 step;
538
cristyb0de93f2013-05-03 13:39:25 +0000539 assert(image != (Image *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000540 p=source_cells;
541 q=destination_cells;
542 for (step=1; step < p->width; step*=2)
543 {
cristyeaedf062010-05-29 22:36:02 +0000544 for (x=0; x < (ssize_t) p->width; x+=2*(ssize_t) step)
cristy3ed852e2009-09-05 21:47:34 +0000545 {
cristybb503372010-05-27 20:51:26 +0000546 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000547 i;
548
cristyd40deb62011-03-09 00:52:27 +0000549 ssize_t
550 y;
551
cristy3ed852e2009-09-05 21:47:34 +0000552 unsigned short
553 cell;
554
cristybb503372010-05-27 20:51:26 +0000555 for (i=0; i < (ssize_t) step; i++)
cristy3ed852e2009-09-05 21:47:34 +0000556 {
cristybb503372010-05-27 20:51:26 +0000557 for (y=0; y < (ssize_t) (p->height-i-1); y++)
cristy3ed852e2009-09-05 21:47:34 +0000558 {
559 cell=GetRadonCell(p,x+i,y);
cristyeaedf062010-05-29 22:36:02 +0000560 (void) SetRadonCell(q,x+2*i,y,cell+GetRadonCell(p,x+i+(ssize_t)
561 step,y+i));
562 (void) SetRadonCell(q,x+2*i+1,y,cell+GetRadonCell(p,x+i+(ssize_t)
563 step,y+i+1));
cristy3ed852e2009-09-05 21:47:34 +0000564 }
cristybb503372010-05-27 20:51:26 +0000565 for ( ; y < (ssize_t) (p->height-i); y++)
cristy3ed852e2009-09-05 21:47:34 +0000566 {
567 cell=GetRadonCell(p,x+i,y);
cristyeaedf062010-05-29 22:36:02 +0000568 (void) SetRadonCell(q,x+2*i,y,cell+GetRadonCell(p,x+i+(ssize_t) step,
569 y+i));
cristy3ed852e2009-09-05 21:47:34 +0000570 (void) SetRadonCell(q,x+2*i+1,y,cell);
571 }
cristybb503372010-05-27 20:51:26 +0000572 for ( ; y < (ssize_t) p->height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000573 {
574 cell=GetRadonCell(p,x+i,y);
575 (void) SetRadonCell(q,x+2*i,y,cell);
576 (void) SetRadonCell(q,x+2*i+1,y,cell);
577 }
578 }
579 }
580 swap=p;
581 p=q;
582 q=swap;
583 }
cristyb5d5f722009-11-04 03:03:49 +0000584#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000585 #pragma omp parallel for schedule(static,4) \
cristycb7dfcc2013-01-06 18:34:59 +0000586 magick_threads(image,image,1,1)
cristy3ed852e2009-09-05 21:47:34 +0000587#endif
cristybb503372010-05-27 20:51:26 +0000588 for (x=0; x < (ssize_t) p->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000589 {
cristybb503372010-05-27 20:51:26 +0000590 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000591 y;
592
cristybb503372010-05-27 20:51:26 +0000593 size_t
cristy3ed852e2009-09-05 21:47:34 +0000594 sum;
595
596 sum=0;
cristybb503372010-05-27 20:51:26 +0000597 for (y=0; y < (ssize_t) (p->height-1); y++)
cristy3ed852e2009-09-05 21:47:34 +0000598 {
cristybb503372010-05-27 20:51:26 +0000599 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000600 delta;
601
cristybb503372010-05-27 20:51:26 +0000602 delta=GetRadonCell(p,x,y)-(ssize_t) GetRadonCell(p,x,y+1);
cristy3ed852e2009-09-05 21:47:34 +0000603 sum+=delta*delta;
604 }
605 projection[p->width+sign*x-1]=sum;
606 }
607}
608
609static MagickBooleanType RadonTransform(const Image *image,
cristybb503372010-05-27 20:51:26 +0000610 const double threshold,size_t *projection,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000611{
612 CacheView
613 *image_view;
614
cristy3ed852e2009-09-05 21:47:34 +0000615 MagickBooleanType
616 status;
617
618 RadonInfo
619 *destination_cells,
620 *source_cells;
621
cristybb503372010-05-27 20:51:26 +0000622 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000623 i;
624
cristybb503372010-05-27 20:51:26 +0000625 size_t
cristy3ed852e2009-09-05 21:47:34 +0000626 count,
627 width;
628
cristyd40deb62011-03-09 00:52:27 +0000629 ssize_t
630 y;
631
632 unsigned char
633 byte;
634
cristy3ed852e2009-09-05 21:47:34 +0000635 unsigned short
636 bits[256];
637
638 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
639 source_cells=AcquireRadonInfo(image,width,image->rows,exception);
640 destination_cells=AcquireRadonInfo(image,width,image->rows,exception);
641 if ((source_cells == (RadonInfo *) NULL) ||
642 (destination_cells == (RadonInfo *) NULL))
643 {
644 if (destination_cells != (RadonInfo *) NULL)
645 destination_cells=DestroyRadonInfo(destination_cells);
646 if (source_cells != (RadonInfo *) NULL)
647 source_cells=DestroyRadonInfo(source_cells);
648 return(MagickFalse);
649 }
650 if (ResetRadonCells(source_cells) == MagickFalse)
651 {
652 destination_cells=DestroyRadonInfo(destination_cells);
653 source_cells=DestroyRadonInfo(source_cells);
654 return(MagickFalse);
655 }
656 for (i=0; i < 256; i++)
657 {
658 byte=(unsigned char) i;
659 for (count=0; byte != 0; byte>>=1)
660 count+=byte & 0x01;
661 bits[i]=(unsigned short) count;
662 }
663 status=MagickTrue;
cristy46ff2672012-12-14 15:32:26 +0000664 image_view=AcquireVirtualCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +0000665#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000666 #pragma omp parallel for schedule(static,4) shared(status) \
cristy5e6b2592012-12-19 14:08:11 +0000667 magick_threads(image,image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +0000668#endif
cristybb503372010-05-27 20:51:26 +0000669 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000670 {
cristy4c08aed2011-07-01 19:47:50 +0000671 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000672 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000673
cristybb503372010-05-27 20:51:26 +0000674 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000675 i,
676 x;
677
cristybb503372010-05-27 20:51:26 +0000678 size_t
cristy3ed852e2009-09-05 21:47:34 +0000679 bit,
680 byte;
681
682 if (status == MagickFalse)
683 continue;
684 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000685 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000686 {
687 status=MagickFalse;
688 continue;
689 }
690 bit=0;
691 byte=0;
cristybb503372010-05-27 20:51:26 +0000692 i=(ssize_t) (image->columns+7)/8;
693 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000694 {
695 byte<<=1;
cristyf13c5942012-08-08 23:50:11 +0000696 if (GetPixelIntensity(image,p) < threshold)
cristy3ed852e2009-09-05 21:47:34 +0000697 byte|=0x01;
698 bit++;
699 if (bit == 8)
700 {
701 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
702 bit=0;
703 byte=0;
704 }
cristyed231572011-07-14 02:18:59 +0000705 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000706 }
707 if (bit != 0)
708 {
709 byte<<=(8-bit);
710 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
711 }
712 }
cristy4ee2b0c2012-05-15 00:30:35 +0000713 RadonProjection(image,source_cells,destination_cells,-1,projection);
cristy3ed852e2009-09-05 21:47:34 +0000714 (void) ResetRadonCells(source_cells);
cristyb5d5f722009-11-04 03:03:49 +0000715#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000716 #pragma omp parallel for schedule(static,4) shared(status) \
cristycb7dfcc2013-01-06 18:34:59 +0000717 magick_threads(image,image,1,1)
cristy3ed852e2009-09-05 21:47:34 +0000718#endif
cristybb503372010-05-27 20:51:26 +0000719 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000720 {
cristy4c08aed2011-07-01 19:47:50 +0000721 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000722 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000723
cristybb503372010-05-27 20:51:26 +0000724 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000725 i,
726 x;
727
cristybb503372010-05-27 20:51:26 +0000728 size_t
cristy3ed852e2009-09-05 21:47:34 +0000729 bit,
730 byte;
731
732 if (status == MagickFalse)
733 continue;
734 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000735 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000736 {
737 status=MagickFalse;
738 continue;
739 }
740 bit=0;
741 byte=0;
742 i=0;
cristybb503372010-05-27 20:51:26 +0000743 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000744 {
745 byte<<=1;
cristyf13c5942012-08-08 23:50:11 +0000746 if (GetPixelIntensity(image,p) < threshold)
cristy3ed852e2009-09-05 21:47:34 +0000747 byte|=0x01;
748 bit++;
749 if (bit == 8)
750 {
751 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
752 bit=0;
753 byte=0;
754 }
cristyed231572011-07-14 02:18:59 +0000755 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000756 }
757 if (bit != 0)
758 {
759 byte<<=(8-bit);
760 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
761 }
762 }
cristy4ee2b0c2012-05-15 00:30:35 +0000763 RadonProjection(image,source_cells,destination_cells,1,projection);
cristy3ed852e2009-09-05 21:47:34 +0000764 image_view=DestroyCacheView(image_view);
765 destination_cells=DestroyRadonInfo(destination_cells);
766 source_cells=DestroyRadonInfo(source_cells);
767 return(MagickTrue);
768}
769
cristybb503372010-05-27 20:51:26 +0000770static void GetImageBackgroundColor(Image *image,const ssize_t offset,
cristy3ed852e2009-09-05 21:47:34 +0000771 ExceptionInfo *exception)
772{
773 CacheView
774 *image_view;
775
cristy4c08aed2011-07-01 19:47:50 +0000776 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000777 background;
778
cristya19f1d72012-08-07 18:24:38 +0000779 double
cristy3ed852e2009-09-05 21:47:34 +0000780 count;
781
cristyd40deb62011-03-09 00:52:27 +0000782 ssize_t
783 y;
784
cristy3ed852e2009-09-05 21:47:34 +0000785 /*
786 Compute average background color.
787 */
788 if (offset <= 0)
789 return;
cristy4c08aed2011-07-01 19:47:50 +0000790 GetPixelInfo(image,&background);
cristy3ed852e2009-09-05 21:47:34 +0000791 count=0.0;
cristy46ff2672012-12-14 15:32:26 +0000792 image_view=AcquireVirtualCacheView(image,exception);
cristybb503372010-05-27 20:51:26 +0000793 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000794 {
cristy4c08aed2011-07-01 19:47:50 +0000795 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000796 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000797
cristybb503372010-05-27 20:51:26 +0000798 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000799 x;
800
cristybb503372010-05-27 20:51:26 +0000801 if ((y >= offset) && (y < ((ssize_t) image->rows-offset)))
cristy3ed852e2009-09-05 21:47:34 +0000802 continue;
803 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000804 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000805 continue;
cristybb503372010-05-27 20:51:26 +0000806 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000807 {
cristybb503372010-05-27 20:51:26 +0000808 if ((x >= offset) && (x < ((ssize_t) image->columns-offset)))
cristy3ed852e2009-09-05 21:47:34 +0000809 continue;
cristy4c08aed2011-07-01 19:47:50 +0000810 background.red+=QuantumScale*GetPixelRed(image,p);
811 background.green+=QuantumScale*GetPixelGreen(image,p);
812 background.blue+=QuantumScale*GetPixelBlue(image,p);
cristy26295322011-09-22 00:50:36 +0000813 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
814 background.alpha+=QuantumScale*GetPixelAlpha(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000815 count++;
cristyed231572011-07-14 02:18:59 +0000816 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000817 }
818 }
819 image_view=DestroyCacheView(image_view);
cristy8cd03c32012-07-07 18:57:59 +0000820 image->background_color.red=(double) ClampToQuantum(QuantumRange*
821 background.red/count);
822 image->background_color.green=(double) ClampToQuantum(QuantumRange*
823 background.green/count);
824 image->background_color.blue=(double) ClampToQuantum(QuantumRange*
825 background.blue/count);
cristy26295322011-09-22 00:50:36 +0000826 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy8cd03c32012-07-07 18:57:59 +0000827 image->background_color.alpha=(double) ClampToQuantum(QuantumRange*
828 background.alpha/count);
cristy3ed852e2009-09-05 21:47:34 +0000829}
830
831MagickExport Image *DeskewImage(const Image *image,const double threshold,
832 ExceptionInfo *exception)
833{
834 AffineMatrix
835 affine_matrix;
836
837 const char
838 *artifact;
839
840 double
841 degrees;
842
843 Image
844 *clone_image,
845 *crop_image,
846 *deskew_image,
847 *median_image;
848
cristy3ed852e2009-09-05 21:47:34 +0000849 MagickBooleanType
850 status;
851
852 RectangleInfo
853 geometry;
854
cristybb503372010-05-27 20:51:26 +0000855 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000856 i;
857
cristybb503372010-05-27 20:51:26 +0000858 size_t
cristy3ed852e2009-09-05 21:47:34 +0000859 max_projection,
860 *projection,
861 width;
862
cristyd40deb62011-03-09 00:52:27 +0000863 ssize_t
864 skew;
865
cristy3ed852e2009-09-05 21:47:34 +0000866 /*
867 Compute deskew angle.
868 */
869 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
cristybb503372010-05-27 20:51:26 +0000870 projection=(size_t *) AcquireQuantumMemory((size_t) (2*width-1),
cristy3ed852e2009-09-05 21:47:34 +0000871 sizeof(*projection));
cristybb503372010-05-27 20:51:26 +0000872 if (projection == (size_t *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000873 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
874 status=RadonTransform(image,threshold,projection,exception);
875 if (status == MagickFalse)
876 {
cristybb503372010-05-27 20:51:26 +0000877 projection=(size_t *) RelinquishMagickMemory(projection);
cristy3ed852e2009-09-05 21:47:34 +0000878 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
879 }
880 max_projection=0;
881 skew=0;
cristybb503372010-05-27 20:51:26 +0000882 for (i=0; i < (ssize_t) (2*width-1); i++)
cristy3ed852e2009-09-05 21:47:34 +0000883 {
884 if (projection[i] > max_projection)
885 {
cristybb503372010-05-27 20:51:26 +0000886 skew=i-(ssize_t) width+1;
cristy3ed852e2009-09-05 21:47:34 +0000887 max_projection=projection[i];
888 }
889 }
cristybb503372010-05-27 20:51:26 +0000890 projection=(size_t *) RelinquishMagickMemory(projection);
anthony553bfcc2013-04-10 01:27:43 +0000891 degrees=RadiansToDegrees(-atan((double) skew/width/8));
892 if (image->debug != MagickFalse)
893 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
894 " Deskew angle: %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +0000895 /*
896 Deskew image.
897 */
898 clone_image=CloneImage(image,0,0,MagickTrue,exception);
899 if (clone_image == (Image *) NULL)
900 return((Image *) NULL);
anthony8a95e802013-04-10 05:31:57 +0000901 {
902 char
903 angle[MaxTextExtent];
904
905 (void) FormatLocaleString(angle,MaxTextExtent,"%.20g",degrees);
906 (void) SetImageArtifact(clone_image,"deskew:angle",angle);
907 }
cristy387430f2012-02-07 13:09:46 +0000908 (void) SetImageVirtualPixelMethod(clone_image,BackgroundVirtualPixelMethod,
909 exception);
cristy3ed852e2009-09-05 21:47:34 +0000910 affine_matrix.sx=cos(DegreesToRadians(fmod((double) degrees,360.0)));
911 affine_matrix.rx=sin(DegreesToRadians(fmod((double) degrees,360.0)));
912 affine_matrix.ry=(-sin(DegreesToRadians(fmod((double) degrees,360.0))));
913 affine_matrix.sy=cos(DegreesToRadians(fmod((double) degrees,360.0)));
914 affine_matrix.tx=0.0;
915 affine_matrix.ty=0.0;
916 artifact=GetImageArtifact(image,"deskew:auto-crop");
917 if (artifact == (const char *) NULL)
918 {
919 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
920 clone_image=DestroyImage(clone_image);
921 return(deskew_image);
922 }
923 /*
924 Auto-crop image.
925 */
cristyba978e12010-09-12 20:26:50 +0000926 GetImageBackgroundColor(clone_image,(ssize_t) StringToLong(artifact),
927 exception);
cristy3ed852e2009-09-05 21:47:34 +0000928 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
929 clone_image=DestroyImage(clone_image);
930 if (deskew_image == (Image *) NULL)
931 return((Image *) NULL);
cristy95c38342011-03-18 22:39:51 +0000932 median_image=StatisticImage(deskew_image,MedianStatistic,3,3,exception);
cristy3ed852e2009-09-05 21:47:34 +0000933 if (median_image == (Image *) NULL)
934 {
935 deskew_image=DestroyImage(deskew_image);
936 return((Image *) NULL);
937 }
938 geometry=GetImageBoundingBox(median_image,exception);
939 median_image=DestroyImage(median_image);
940 if (image->debug != MagickFalse)
941 (void) LogMagickEvent(TransformEvent,GetMagickModule()," Deskew geometry: "
cristy6d8abba2010-06-03 01:10:47 +0000942 "%.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
cristye8c25f92010-06-03 00:53:06 +0000943 geometry.height,(double) geometry.x,(double) geometry.y);
cristy3ed852e2009-09-05 21:47:34 +0000944 crop_image=CropImage(deskew_image,&geometry,exception);
945 deskew_image=DestroyImage(deskew_image);
946 return(crop_image);
947}
948
949/*
950%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
951% %
952% %
953% %
cristy987feef2011-11-17 12:24:56 +0000954% 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 +0000955% %
956% %
957% %
958%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
959%
cristy3dfa16c2010-05-17 19:34:48 +0000960% IntegralRotateImage() rotates the image an integral of 90 degrees. It
cristy3ed852e2009-09-05 21:47:34 +0000961% allocates the memory necessary for the new Image structure and returns a
962% pointer to the rotated image.
963%
964% The format of the IntegralRotateImage method is:
965%
cristybb503372010-05-27 20:51:26 +0000966% Image *IntegralRotateImage(const Image *image,size_t rotations,
cristy3ed852e2009-09-05 21:47:34 +0000967% ExceptionInfo *exception)
968%
969% A description of each parameter follows.
970%
971% o image: the image.
972%
973% o rotations: Specifies the number of 90 degree rotations.
974%
975*/
cristy987feef2011-11-17 12:24:56 +0000976MagickExport Image *IntegralRotateImage(const Image *image,size_t rotations,
cristy3ed852e2009-09-05 21:47:34 +0000977 ExceptionInfo *exception)
978{
cristy3ed852e2009-09-05 21:47:34 +0000979#define RotateImageTag "Rotate/Image"
980
981 CacheView
982 *image_view,
983 *rotate_view;
984
985 Image
986 *rotate_image;
987
cristy3ed852e2009-09-05 21:47:34 +0000988 MagickBooleanType
989 status;
990
cristy5f959472010-05-27 22:19:46 +0000991 MagickOffsetType
992 progress;
993
cristy3ed852e2009-09-05 21:47:34 +0000994 RectangleInfo
995 page;
996
cristy5f959472010-05-27 22:19:46 +0000997 ssize_t
998 y;
999
cristy3ed852e2009-09-05 21:47:34 +00001000 /*
1001 Initialize rotated image attributes.
1002 */
1003 assert(image != (Image *) NULL);
1004 page=image->page;
1005 rotations%=4;
cristyb32b90a2009-09-07 21:45:48 +00001006 if (rotations == 0)
1007 return(CloneImage(image,0,0,MagickTrue,exception));
cristy3ed852e2009-09-05 21:47:34 +00001008 if ((rotations == 1) || (rotations == 3))
1009 rotate_image=CloneImage(image,image->rows,image->columns,MagickTrue,
1010 exception);
1011 else
1012 rotate_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1013 exception);
1014 if (rotate_image == (Image *) NULL)
1015 return((Image *) NULL);
1016 /*
1017 Integral rotate the image.
1018 */
1019 status=MagickTrue;
1020 progress=0;
cristy46ff2672012-12-14 15:32:26 +00001021 image_view=AcquireVirtualCacheView(image,exception);
1022 rotate_view=AcquireAuthenticCacheView(rotate_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001023 switch (rotations)
1024 {
1025 case 0:
1026 {
1027 /*
1028 Rotate 0 degrees.
1029 */
cristy3ed852e2009-09-05 21:47:34 +00001030 break;
1031 }
1032 case 1:
1033 {
cristybb503372010-05-27 20:51:26 +00001034 size_t
cristyb32b90a2009-09-07 21:45:48 +00001035 tile_height,
1036 tile_width;
1037
cristyd40deb62011-03-09 00:52:27 +00001038 ssize_t
1039 tile_y;
1040
cristy3ed852e2009-09-05 21:47:34 +00001041 /*
1042 Rotate 90 degrees.
1043 */
cristyb32b90a2009-09-07 21:45:48 +00001044 GetPixelCacheTileSize(image,&tile_width,&tile_height);
cristy8b8148b2012-10-07 00:41:15 +00001045 tile_width=image->columns;
cristy26b64912012-12-16 18:20:09 +00001046#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy64c10cd2013-01-06 18:40:43 +00001047 #pragma omp parallel for schedule(static,4) shared(status) \
cristy3e1fa372013-01-06 18:07:11 +00001048 magick_threads(image,image,1,1)
cristy9a5a52f2012-10-09 14:40:31 +00001049#endif
cristyad11aac2011-09-25 21:29:16 +00001050 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
cristy3ed852e2009-09-05 21:47:34 +00001051 {
cristybb503372010-05-27 20:51:26 +00001052 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001053 tile_x;
1054
1055 if (status == MagickFalse)
1056 continue;
cristy26295322011-09-22 00:50:36 +00001057 tile_x=0;
1058 for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
cristy3ed852e2009-09-05 21:47:34 +00001059 {
1060 MagickBooleanType
1061 sync;
1062
cristy4c08aed2011-07-01 19:47:50 +00001063 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001064 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001065
cristy4c08aed2011-07-01 19:47:50 +00001066 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001067 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001068
cristya26211e2011-10-09 01:24:57 +00001069 register ssize_t
1070 y;
1071
cristybb503372010-05-27 20:51:26 +00001072 size_t
cristyb32b90a2009-09-07 21:45:48 +00001073 height,
1074 width;
cristy3ed852e2009-09-05 21:47:34 +00001075
cristyb32b90a2009-09-07 21:45:48 +00001076 width=tile_width;
cristybb503372010-05-27 20:51:26 +00001077 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
cristya45d2ec2011-08-24 13:17:19 +00001078 width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
cristyb32b90a2009-09-07 21:45:48 +00001079 height=tile_height;
cristybb503372010-05-27 20:51:26 +00001080 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
cristya45d2ec2011-08-24 13:17:19 +00001081 height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
cristydaa97692009-09-13 02:10:35 +00001082 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1083 exception);
cristy4c08aed2011-07-01 19:47:50 +00001084 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001085 {
1086 status=MagickFalse;
1087 break;
1088 }
cristybb503372010-05-27 20:51:26 +00001089 for (y=0; y < (ssize_t) width; y++)
cristy3ed852e2009-09-05 21:47:34 +00001090 {
cristy4c08aed2011-07-01 19:47:50 +00001091 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001092 *restrict tile_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001093
cristybb503372010-05-27 20:51:26 +00001094 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001095 x;
1096
cristy27709ef2011-09-18 02:53:53 +00001097 if (status == MagickFalse)
1098 continue;
cristybb503372010-05-27 20:51:26 +00001099 q=QueueCacheViewAuthenticPixels(rotate_view,(ssize_t)
cristy92611572011-07-07 16:02:42 +00001100 (rotate_image->columns-(tile_y+height)),y+tile_x,height,1,
1101 exception);
cristyacd2ed22011-08-30 01:44:23 +00001102 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001103 {
1104 status=MagickFalse;
cristy27709ef2011-09-18 02:53:53 +00001105 continue;
cristy3ed852e2009-09-05 21:47:34 +00001106 }
cristyed231572011-07-14 02:18:59 +00001107 tile_pixels=p+((height-1)*width+y)*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001108 for (x=0; x < (ssize_t) height; x++)
cristy3ed852e2009-09-05 21:47:34 +00001109 {
cristya45d2ec2011-08-24 13:17:19 +00001110 register ssize_t
1111 i;
1112
cristy883fde12013-04-08 00:50:13 +00001113 if (GetPixelReadMask(image,tile_pixels) == 0)
cristy10a6c612012-01-29 21:41:05 +00001114 {
1115 tile_pixels-=width*GetPixelChannels(image);
1116 q+=GetPixelChannels(rotate_image);
1117 continue;
1118 }
cristya45d2ec2011-08-24 13:17:19 +00001119 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1120 {
cristy5a23c552013-02-13 14:34:28 +00001121 PixelChannel channel=GetPixelChannelChannel(image,i);
1122 PixelTrait traits=GetPixelChannelTraits(image,channel);
1123 PixelTrait rotate_traits=GetPixelChannelTraits(rotate_image,
1124 channel);
cristy010d7d12011-08-31 01:02:48 +00001125 if ((traits == UndefinedPixelTrait) ||
1126 (rotate_traits == UndefinedPixelTrait))
cristya45d2ec2011-08-24 13:17:19 +00001127 continue;
cristy0beccfa2011-09-25 20:47:53 +00001128 SetPixelChannel(rotate_image,channel,tile_pixels[i],q);
cristya45d2ec2011-08-24 13:17:19 +00001129 }
cristyed231572011-07-14 02:18:59 +00001130 tile_pixels-=width*GetPixelChannels(image);
1131 q+=GetPixelChannels(rotate_image);
cristy3ed852e2009-09-05 21:47:34 +00001132 }
cristy3ed852e2009-09-05 21:47:34 +00001133 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1134 if (sync == MagickFalse)
1135 status=MagickFalse;
1136 }
1137 }
1138 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1139 {
1140 MagickBooleanType
1141 proceed;
1142
cristy26b64912012-12-16 18:20:09 +00001143#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy9a5a52f2012-10-09 14:40:31 +00001144 #pragma omp critical (MagickCore_IntegralRotateImage)
1145#endif
cristyb32b90a2009-09-07 21:45:48 +00001146 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
cristy3ed852e2009-09-05 21:47:34 +00001147 image->rows);
1148 if (proceed == MagickFalse)
1149 status=MagickFalse;
1150 }
1151 }
cristyecc2c142010-01-17 22:25:46 +00001152 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1153 image->rows-1,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001154 Swap(page.width,page.height);
1155 Swap(page.x,page.y);
1156 if (page.width != 0)
cristybb503372010-05-27 20:51:26 +00001157 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
cristy3ed852e2009-09-05 21:47:34 +00001158 break;
1159 }
1160 case 2:
1161 {
1162 /*
1163 Rotate 180 degrees.
1164 */
cristy5f890612012-12-16 23:34:05 +00001165#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy64c10cd2013-01-06 18:40:43 +00001166 #pragma omp parallel for schedule(static,4) shared(status) \
cristyd6432472013-01-06 16:56:13 +00001167 magick_threads(image,image,1,1)
cristy5f890612012-12-16 23:34:05 +00001168#endif
cristybb503372010-05-27 20:51:26 +00001169 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001170 {
1171 MagickBooleanType
1172 sync;
1173
cristy4c08aed2011-07-01 19:47:50 +00001174 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001175 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001176
cristy4c08aed2011-07-01 19:47:50 +00001177 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001178 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001179
cristy2be50602011-10-09 01:28:13 +00001180 register ssize_t
1181 x;
1182
cristy3ed852e2009-09-05 21:47:34 +00001183 if (status == MagickFalse)
1184 continue;
cristya45d2ec2011-08-24 13:17:19 +00001185 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1186 q=QueueCacheViewAuthenticPixels(rotate_view,0,(ssize_t) (image->rows-y-
1187 1),image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001188 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00001189 {
1190 status=MagickFalse;
1191 continue;
1192 }
cristyed231572011-07-14 02:18:59 +00001193 q+=GetPixelChannels(rotate_image)*image->columns;
cristybb503372010-05-27 20:51:26 +00001194 for (x=0; x < (ssize_t) image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00001195 {
cristya45d2ec2011-08-24 13:17:19 +00001196 register ssize_t
1197 i;
1198
cristyed231572011-07-14 02:18:59 +00001199 q-=GetPixelChannels(rotate_image);
cristy883fde12013-04-08 00:50:13 +00001200 if (GetPixelReadMask(image,p) == 0)
cristy10a6c612012-01-29 21:41:05 +00001201 {
1202 p+=GetPixelChannels(image);
1203 continue;
1204 }
cristya45d2ec2011-08-24 13:17:19 +00001205 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1206 {
cristy5a23c552013-02-13 14:34:28 +00001207 PixelChannel channel=GetPixelChannelChannel(image,i);
1208 PixelTrait traits=GetPixelChannelTraits(image,channel);
1209 PixelTrait rotate_traits=GetPixelChannelTraits(rotate_image,
1210 channel);
cristy010d7d12011-08-31 01:02:48 +00001211 if ((traits == UndefinedPixelTrait) ||
1212 (rotate_traits == UndefinedPixelTrait))
cristya45d2ec2011-08-24 13:17:19 +00001213 continue;
cristy0beccfa2011-09-25 20:47:53 +00001214 SetPixelChannel(rotate_image,channel,p[i],q);
cristya45d2ec2011-08-24 13:17:19 +00001215 }
cristyed231572011-07-14 02:18:59 +00001216 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001217 }
cristy3ed852e2009-09-05 21:47:34 +00001218 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1219 if (sync == MagickFalse)
1220 status=MagickFalse;
1221 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1222 {
1223 MagickBooleanType
1224 proceed;
1225
cristy5f890612012-12-16 23:34:05 +00001226#if defined(MAGICKCORE_OPENMP_SUPPORT)
1227 #pragma omp critical (MagickCore_IntegralRotateImage)
1228#endif
cristy0729beb2011-09-25 23:29:32 +00001229 proceed=SetImageProgress(image,RotateImageTag,progress++,
cristy21e03412011-09-25 22:39:35 +00001230 image->rows);
1231 if (proceed == MagickFalse)
1232 status=MagickFalse;
1233 }
1234 }
1235 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1236 image->rows-1,image->rows);
1237 Swap(page.width,page.height);
1238 Swap(page.x,page.y);
1239 if (page.width != 0)
1240 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1241 break;
1242 }
cristy3ed852e2009-09-05 21:47:34 +00001243 case 3:
1244 {
cristybb503372010-05-27 20:51:26 +00001245 size_t
cristyb32b90a2009-09-07 21:45:48 +00001246 tile_height,
1247 tile_width;
1248
cristyd40deb62011-03-09 00:52:27 +00001249 ssize_t
1250 tile_y;
1251
cristy3ed852e2009-09-05 21:47:34 +00001252 /*
1253 Rotate 270 degrees.
1254 */
cristyb32b90a2009-09-07 21:45:48 +00001255 GetPixelCacheTileSize(image,&tile_width,&tile_height);
cristy8b8148b2012-10-07 00:41:15 +00001256 tile_width=image->columns;
cristy26b64912012-12-16 18:20:09 +00001257#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy64c10cd2013-01-06 18:40:43 +00001258 #pragma omp parallel for schedule(static,4) shared(status) \
cristycb7dfcc2013-01-06 18:34:59 +00001259 magick_threads(image,image,1,1)
cristy9a5a52f2012-10-09 14:40:31 +00001260#endif
cristyad11aac2011-09-25 21:29:16 +00001261 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
cristy3ed852e2009-09-05 21:47:34 +00001262 {
cristybb503372010-05-27 20:51:26 +00001263 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001264 tile_x;
1265
1266 if (status == MagickFalse)
1267 continue;
cristy26295322011-09-22 00:50:36 +00001268 tile_x=0;
1269 for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
cristy3ed852e2009-09-05 21:47:34 +00001270 {
1271 MagickBooleanType
1272 sync;
1273
cristy4c08aed2011-07-01 19:47:50 +00001274 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001275 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001276
cristy4c08aed2011-07-01 19:47:50 +00001277 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001278 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001279
cristy2be50602011-10-09 01:28:13 +00001280 register ssize_t
1281 y;
1282
cristybb503372010-05-27 20:51:26 +00001283 size_t
cristyb32b90a2009-09-07 21:45:48 +00001284 height,
1285 width;
cristy3ed852e2009-09-05 21:47:34 +00001286
cristyb32b90a2009-09-07 21:45:48 +00001287 width=tile_width;
cristybb503372010-05-27 20:51:26 +00001288 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
cristya45d2ec2011-08-24 13:17:19 +00001289 width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
cristyb32b90a2009-09-07 21:45:48 +00001290 height=tile_height;
cristybb503372010-05-27 20:51:26 +00001291 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
cristya45d2ec2011-08-24 13:17:19 +00001292 height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
1293 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1294 exception);
cristy4c08aed2011-07-01 19:47:50 +00001295 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001296 {
1297 status=MagickFalse;
1298 break;
1299 }
cristybb503372010-05-27 20:51:26 +00001300 for (y=0; y < (ssize_t) width; y++)
cristy3ed852e2009-09-05 21:47:34 +00001301 {
cristy4c08aed2011-07-01 19:47:50 +00001302 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001303 *restrict tile_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001304
cristybb503372010-05-27 20:51:26 +00001305 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001306 x;
1307
cristy27709ef2011-09-18 02:53:53 +00001308 if (status == MagickFalse)
1309 continue;
cristya45d2ec2011-08-24 13:17:19 +00001310 q=QueueCacheViewAuthenticPixels(rotate_view,tile_y,(ssize_t) (y+
1311 rotate_image->rows-(tile_x+width)),height,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001312 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001313 {
1314 status=MagickFalse;
cristy27709ef2011-09-18 02:53:53 +00001315 continue;
cristy3ed852e2009-09-05 21:47:34 +00001316 }
cristyed231572011-07-14 02:18:59 +00001317 tile_pixels=p+((width-1)-y)*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001318 for (x=0; x < (ssize_t) height; x++)
cristy3ed852e2009-09-05 21:47:34 +00001319 {
cristya45d2ec2011-08-24 13:17:19 +00001320 register ssize_t
1321 i;
1322
cristy883fde12013-04-08 00:50:13 +00001323 if (GetPixelReadMask(image,tile_pixels) == 0)
cristy10a6c612012-01-29 21:41:05 +00001324 {
1325 tile_pixels+=width*GetPixelChannels(image);
1326 q+=GetPixelChannels(rotate_image);
1327 continue;
1328 }
cristya45d2ec2011-08-24 13:17:19 +00001329 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1330 {
cristy5a23c552013-02-13 14:34:28 +00001331 PixelChannel channel=GetPixelChannelChannel(image,i);
1332 PixelTrait traits=GetPixelChannelTraits(image,channel);
1333 PixelTrait rotate_traits=GetPixelChannelTraits(rotate_image,
1334 channel);
cristy010d7d12011-08-31 01:02:48 +00001335 if ((traits == UndefinedPixelTrait) ||
1336 (rotate_traits == UndefinedPixelTrait))
cristya45d2ec2011-08-24 13:17:19 +00001337 continue;
cristy0beccfa2011-09-25 20:47:53 +00001338 SetPixelChannel(rotate_image,channel,tile_pixels[i],q);
cristya45d2ec2011-08-24 13:17:19 +00001339 }
cristyed231572011-07-14 02:18:59 +00001340 tile_pixels+=width*GetPixelChannels(image);
1341 q+=GetPixelChannels(rotate_image);
cristy3ed852e2009-09-05 21:47:34 +00001342 }
cristy26b64912012-12-16 18:20:09 +00001343#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy9a5a52f2012-10-09 14:40:31 +00001344 #pragma omp critical (MagickCore_IntegralRotateImage)
1345#endif
cristy3ed852e2009-09-05 21:47:34 +00001346 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1347 if (sync == MagickFalse)
1348 status=MagickFalse;
1349 }
1350 }
1351 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1352 {
1353 MagickBooleanType
1354 proceed;
1355
cristy21e03412011-09-25 22:39:35 +00001356 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
1357 image->rows);
1358 if (proceed == MagickFalse)
1359 status=MagickFalse;
1360 }
1361 }
1362 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1363 image->rows-1,image->rows);
1364 Swap(page.width,page.height);
1365 Swap(page.x,page.y);
1366 if (page.width != 0)
1367 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1368 break;
1369 }
cristy3ed852e2009-09-05 21:47:34 +00001370 }
1371 rotate_view=DestroyCacheView(rotate_view);
1372 image_view=DestroyCacheView(image_view);
1373 rotate_image->type=image->type;
1374 rotate_image->page=page;
1375 if (status == MagickFalse)
1376 rotate_image=DestroyImage(rotate_image);
1377 return(rotate_image);
1378}
1379
1380/*
1381%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1382% %
1383% %
1384% %
1385+ X S h e a r I m a g e %
1386% %
1387% %
1388% %
1389%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1390%
1391% XShearImage() shears the image in the X direction with a shear angle of
1392% 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1393% negative angles shear clockwise. Angles are measured relative to a vertical
1394% Y-axis. X shears will widen an image creating 'empty' triangles on the left
1395% and right sides of the source image.
1396%
1397% The format of the XShearImage method is:
1398%
cristya19f1d72012-08-07 18:24:38 +00001399% MagickBooleanType XShearImage(Image *image,const double degrees,
cristybb503372010-05-27 20:51:26 +00001400% const size_t width,const size_t height,
1401% const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001402%
1403% A description of each parameter follows.
1404%
1405% o image: the image.
1406%
cristya19f1d72012-08-07 18:24:38 +00001407% o degrees: A double representing the shearing angle along the X
cristy3ed852e2009-09-05 21:47:34 +00001408% axis.
1409%
1410% o width, height, x_offset, y_offset: Defines a region of the image
1411% to shear.
1412%
cristyecc2c142010-01-17 22:25:46 +00001413% o exception: return any errors or warnings in this structure.
1414%
cristy3ed852e2009-09-05 21:47:34 +00001415*/
cristya19f1d72012-08-07 18:24:38 +00001416static MagickBooleanType XShearImage(Image *image,const double degrees,
cristybb503372010-05-27 20:51:26 +00001417 const size_t width,const size_t height,const ssize_t x_offset,
1418 const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001419{
1420#define XShearImageTag "XShear/Image"
1421
1422 typedef enum
1423 {
1424 LEFT,
1425 RIGHT
1426 } ShearDirection;
1427
1428 CacheView
1429 *image_view;
1430
cristy3ed852e2009-09-05 21:47:34 +00001431 MagickBooleanType
1432 status;
1433
cristy5f959472010-05-27 22:19:46 +00001434 MagickOffsetType
1435 progress;
1436
cristy4c08aed2011-07-01 19:47:50 +00001437 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001438 background;
1439
cristy5f959472010-05-27 22:19:46 +00001440 ssize_t
1441 y;
1442
cristy9d8c8ce2011-10-25 16:13:52 +00001443 /*
1444 X shear image.
1445 */
cristy3ed852e2009-09-05 21:47:34 +00001446 assert(image != (Image *) NULL);
1447 assert(image->signature == MagickSignature);
1448 if (image->debug != MagickFalse)
1449 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3ed852e2009-09-05 21:47:34 +00001450 status=MagickTrue;
cristy9d8c8ce2011-10-25 16:13:52 +00001451 background=image->background_color;
cristy3ed852e2009-09-05 21:47:34 +00001452 progress=0;
cristy46ff2672012-12-14 15:32:26 +00001453 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00001454#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00001455 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00001456 magick_threads(image,image,height,1)
cristy3ed852e2009-09-05 21:47:34 +00001457#endif
cristybb503372010-05-27 20:51:26 +00001458 for (y=0; y < (ssize_t) height; y++)
cristy3ed852e2009-09-05 21:47:34 +00001459 {
cristy4c08aed2011-07-01 19:47:50 +00001460 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001461 pixel,
1462 source,
1463 destination;
1464
cristya19f1d72012-08-07 18:24:38 +00001465 double
cristy3ed852e2009-09-05 21:47:34 +00001466 area,
1467 displacement;
1468
cristy4c08aed2011-07-01 19:47:50 +00001469 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001470 *restrict p,
1471 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001472
cristyd40deb62011-03-09 00:52:27 +00001473 register ssize_t
1474 i;
1475
cristy3ed852e2009-09-05 21:47:34 +00001476 ShearDirection
1477 direction;
1478
cristyd40deb62011-03-09 00:52:27 +00001479 ssize_t
1480 step;
1481
cristy3ed852e2009-09-05 21:47:34 +00001482 if (status == MagickFalse)
1483 continue;
1484 p=GetCacheViewAuthenticPixels(image_view,0,y_offset+y,image->columns,1,
1485 exception);
cristy4c08aed2011-07-01 19:47:50 +00001486 if (p == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001487 {
1488 status=MagickFalse;
1489 continue;
1490 }
cristyed231572011-07-14 02:18:59 +00001491 p+=x_offset*GetPixelChannels(image);
cristya19f1d72012-08-07 18:24:38 +00001492 displacement=degrees*(double) (y-height/2.0);
cristy3ed852e2009-09-05 21:47:34 +00001493 if (displacement == 0.0)
1494 continue;
1495 if (displacement > 0.0)
1496 direction=RIGHT;
1497 else
1498 {
1499 displacement*=(-1.0);
1500 direction=LEFT;
1501 }
cristybb503372010-05-27 20:51:26 +00001502 step=(ssize_t) floor((double) displacement);
cristya19f1d72012-08-07 18:24:38 +00001503 area=(double) (displacement-step);
cristy3ed852e2009-09-05 21:47:34 +00001504 step++;
1505 pixel=background;
cristy4c08aed2011-07-01 19:47:50 +00001506 GetPixelInfo(image,&source);
1507 GetPixelInfo(image,&destination);
cristy3ed852e2009-09-05 21:47:34 +00001508 switch (direction)
1509 {
1510 case LEFT:
1511 {
1512 /*
1513 Transfer pixels left-to-right.
1514 */
1515 if (step > x_offset)
1516 break;
cristyed231572011-07-14 02:18:59 +00001517 q=p-step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001518 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001519 {
1520 if ((x_offset+i) < step)
1521 {
cristyed231572011-07-14 02:18:59 +00001522 p+=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001523 GetPixelInfoPixel(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001524 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001525 continue;
1526 }
cristy803640d2011-11-17 02:11:32 +00001527 GetPixelInfoPixel(image,p,&source);
cristya19f1d72012-08-07 18:24:38 +00001528 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1529 &source,(double) GetPixelAlpha(image,p),area,&destination);
cristy803640d2011-11-17 02:11:32 +00001530 SetPixelInfoPixel(image,&destination,q);
1531 GetPixelInfoPixel(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001532 p+=GetPixelChannels(image);
1533 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001534 }
cristya19f1d72012-08-07 18:24:38 +00001535 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1536 &background,(double) background.alpha,area,&destination);
cristy803640d2011-11-17 02:11:32 +00001537 SetPixelInfoPixel(image,&destination,q);
cristyed231572011-07-14 02:18:59 +00001538 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001539 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001540 {
cristy803640d2011-11-17 02:11:32 +00001541 SetPixelInfoPixel(image,&background,q);
cristyed231572011-07-14 02:18:59 +00001542 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001543 }
cristy3ed852e2009-09-05 21:47:34 +00001544 break;
1545 }
1546 case RIGHT:
1547 {
1548 /*
1549 Transfer pixels right-to-left.
1550 */
cristyed231572011-07-14 02:18:59 +00001551 p+=width*GetPixelChannels(image);
1552 q=p+step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001553 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001554 {
cristyed231572011-07-14 02:18:59 +00001555 p-=GetPixelChannels(image);
1556 q-=GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001557 if ((size_t) (x_offset+width+step-i) >= image->columns)
cristy3ed852e2009-09-05 21:47:34 +00001558 continue;
cristy803640d2011-11-17 02:11:32 +00001559 GetPixelInfoPixel(image,p,&source);
cristya19f1d72012-08-07 18:24:38 +00001560 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1561 &source,(double) GetPixelAlpha(image,p),area,&destination);
cristy803640d2011-11-17 02:11:32 +00001562 SetPixelInfoPixel(image,&destination,q);
1563 GetPixelInfoPixel(image,p,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001564 }
cristya19f1d72012-08-07 18:24:38 +00001565 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1566 &background,(double) background.alpha,area,&destination);
cristyed231572011-07-14 02:18:59 +00001567 q-=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001568 SetPixelInfoPixel(image,&destination,q);
cristy3ed852e2009-09-05 21:47:34 +00001569 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001570 {
cristyed231572011-07-14 02:18:59 +00001571 q-=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001572 SetPixelInfoPixel(image,&background,q);
cristy4c08aed2011-07-01 19:47:50 +00001573 }
cristy3ed852e2009-09-05 21:47:34 +00001574 break;
1575 }
1576 }
1577 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1578 status=MagickFalse;
1579 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1580 {
1581 MagickBooleanType
1582 proceed;
1583
cristyb5d5f722009-11-04 03:03:49 +00001584#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy7b650b52011-10-09 01:13:39 +00001585 #pragma omp critical (MagickCore_XShearImage)
cristy3ed852e2009-09-05 21:47:34 +00001586#endif
1587 proceed=SetImageProgress(image,XShearImageTag,progress++,height);
1588 if (proceed == MagickFalse)
1589 status=MagickFalse;
1590 }
1591 }
1592 image_view=DestroyCacheView(image_view);
1593 return(status);
1594}
1595
1596/*
1597%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1598% %
1599% %
1600% %
1601+ Y S h e a r I m a g e %
1602% %
1603% %
1604% %
1605%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1606%
1607% YShearImage shears the image in the Y direction with a shear angle of
1608% 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1609% negative angles shear clockwise. Angles are measured relative to a
1610% horizontal X-axis. Y shears will increase the height of an image creating
1611% 'empty' triangles on the top and bottom of the source image.
1612%
1613% The format of the YShearImage method is:
1614%
cristya19f1d72012-08-07 18:24:38 +00001615% MagickBooleanType YShearImage(Image *image,const double degrees,
cristybb503372010-05-27 20:51:26 +00001616% const size_t width,const size_t height,
1617% const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001618%
1619% A description of each parameter follows.
1620%
1621% o image: the image.
1622%
cristya19f1d72012-08-07 18:24:38 +00001623% o degrees: A double representing the shearing angle along the Y
cristy3ed852e2009-09-05 21:47:34 +00001624% axis.
1625%
1626% o width, height, x_offset, y_offset: Defines a region of the image
1627% to shear.
1628%
cristyecc2c142010-01-17 22:25:46 +00001629% o exception: return any errors or warnings in this structure.
1630%
cristy3ed852e2009-09-05 21:47:34 +00001631*/
cristya19f1d72012-08-07 18:24:38 +00001632static MagickBooleanType YShearImage(Image *image,const double degrees,
cristybb503372010-05-27 20:51:26 +00001633 const size_t width,const size_t height,const ssize_t x_offset,
1634 const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001635{
1636#define YShearImageTag "YShear/Image"
1637
1638 typedef enum
1639 {
1640 UP,
1641 DOWN
1642 } ShearDirection;
1643
1644 CacheView
1645 *image_view;
1646
cristy3ed852e2009-09-05 21:47:34 +00001647 MagickBooleanType
1648 status;
1649
cristy5f959472010-05-27 22:19:46 +00001650 MagickOffsetType
1651 progress;
1652
cristy4c08aed2011-07-01 19:47:50 +00001653 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001654 background;
1655
cristy5f959472010-05-27 22:19:46 +00001656 ssize_t
1657 x;
1658
cristy9d8c8ce2011-10-25 16:13:52 +00001659 /*
1660 Y Shear image.
1661 */
cristy3ed852e2009-09-05 21:47:34 +00001662 assert(image != (Image *) NULL);
1663 assert(image->signature == MagickSignature);
1664 if (image->debug != MagickFalse)
1665 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3ed852e2009-09-05 21:47:34 +00001666 status=MagickTrue;
1667 progress=0;
cristy9d8c8ce2011-10-25 16:13:52 +00001668 background=image->background_color;
cristy46ff2672012-12-14 15:32:26 +00001669 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00001670#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00001671 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00001672 magick_threads(image,image,width,1)
cristy3ed852e2009-09-05 21:47:34 +00001673#endif
cristybb503372010-05-27 20:51:26 +00001674 for (x=0; x < (ssize_t) width; x++)
cristy3ed852e2009-09-05 21:47:34 +00001675 {
cristybb503372010-05-27 20:51:26 +00001676 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001677 step;
1678
cristya19f1d72012-08-07 18:24:38 +00001679 double
cristy3ed852e2009-09-05 21:47:34 +00001680 area,
1681 displacement;
1682
cristy4c08aed2011-07-01 19:47:50 +00001683 PixelInfo
1684 pixel,
1685 source,
1686 destination;
1687
1688 register Quantum
1689 *restrict p,
1690 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001691
cristybb503372010-05-27 20:51:26 +00001692 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001693 i;
1694
cristy3ed852e2009-09-05 21:47:34 +00001695 ShearDirection
1696 direction;
1697
1698 if (status == MagickFalse)
1699 continue;
1700 p=GetCacheViewAuthenticPixels(image_view,x_offset+x,0,1,image->rows,
1701 exception);
cristy4c08aed2011-07-01 19:47:50 +00001702 if (p == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001703 {
1704 status=MagickFalse;
1705 continue;
1706 }
cristyed231572011-07-14 02:18:59 +00001707 p+=y_offset*GetPixelChannels(image);
cristya19f1d72012-08-07 18:24:38 +00001708 displacement=degrees*(double) (x-width/2.0);
cristy3ed852e2009-09-05 21:47:34 +00001709 if (displacement == 0.0)
1710 continue;
1711 if (displacement > 0.0)
1712 direction=DOWN;
1713 else
1714 {
1715 displacement*=(-1.0);
1716 direction=UP;
1717 }
cristybb503372010-05-27 20:51:26 +00001718 step=(ssize_t) floor((double) displacement);
cristya19f1d72012-08-07 18:24:38 +00001719 area=(double) (displacement-step);
cristy3ed852e2009-09-05 21:47:34 +00001720 step++;
1721 pixel=background;
cristy4c08aed2011-07-01 19:47:50 +00001722 GetPixelInfo(image,&source);
1723 GetPixelInfo(image,&destination);
cristy3ed852e2009-09-05 21:47:34 +00001724 switch (direction)
1725 {
1726 case UP:
1727 {
1728 /*
1729 Transfer pixels top-to-bottom.
1730 */
1731 if (step > y_offset)
1732 break;
cristyed231572011-07-14 02:18:59 +00001733 q=p-step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001734 for (i=0; i < (ssize_t) height; i++)
cristy3ed852e2009-09-05 21:47:34 +00001735 {
1736 if ((y_offset+i) < step)
1737 {
cristyed231572011-07-14 02:18:59 +00001738 p+=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001739 GetPixelInfoPixel(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001740 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001741 continue;
1742 }
cristy803640d2011-11-17 02:11:32 +00001743 GetPixelInfoPixel(image,p,&source);
cristya19f1d72012-08-07 18:24:38 +00001744 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1745 &source,(double) GetPixelAlpha(image,p),area,
cristy4c08aed2011-07-01 19:47:50 +00001746 &destination);
cristy803640d2011-11-17 02:11:32 +00001747 SetPixelInfoPixel(image,&destination,q);
1748 GetPixelInfoPixel(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001749 p+=GetPixelChannels(image);
1750 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001751 }
cristya19f1d72012-08-07 18:24:38 +00001752 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1753 &background,(double) background.alpha,area,&destination);
cristy803640d2011-11-17 02:11:32 +00001754 SetPixelInfoPixel(image,&destination,q);
cristyed231572011-07-14 02:18:59 +00001755 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001756 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001757 {
cristy803640d2011-11-17 02:11:32 +00001758 SetPixelInfoPixel(image,&background,q);
cristyed231572011-07-14 02:18:59 +00001759 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001760 }
cristy3ed852e2009-09-05 21:47:34 +00001761 break;
1762 }
1763 case DOWN:
1764 {
1765 /*
1766 Transfer pixels bottom-to-top.
1767 */
cristyed231572011-07-14 02:18:59 +00001768 p+=height*GetPixelChannels(image);
1769 q=p+step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001770 for (i=0; i < (ssize_t) height; i++)
cristy3ed852e2009-09-05 21:47:34 +00001771 {
cristyed231572011-07-14 02:18:59 +00001772 p-=GetPixelChannels(image);
1773 q-=GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001774 if ((size_t) (y_offset+height+step-i) >= image->rows)
cristy3ed852e2009-09-05 21:47:34 +00001775 continue;
cristy803640d2011-11-17 02:11:32 +00001776 GetPixelInfoPixel(image,p,&source);
cristya19f1d72012-08-07 18:24:38 +00001777 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1778 &source,(double) GetPixelAlpha(image,p),area,
cristy4c08aed2011-07-01 19:47:50 +00001779 &destination);
cristy803640d2011-11-17 02:11:32 +00001780 SetPixelInfoPixel(image,&destination,q);
1781 GetPixelInfoPixel(image,p,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001782 }
cristya19f1d72012-08-07 18:24:38 +00001783 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1784 &background,(double) background.alpha,area,&destination);
cristyed231572011-07-14 02:18:59 +00001785 q-=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001786 SetPixelInfoPixel(image,&destination,q);
cristy3ed852e2009-09-05 21:47:34 +00001787 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001788 {
cristyed231572011-07-14 02:18:59 +00001789 q-=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001790 SetPixelInfoPixel(image,&background,q);
cristy4c08aed2011-07-01 19:47:50 +00001791 }
cristy3ed852e2009-09-05 21:47:34 +00001792 break;
1793 }
1794 }
1795 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1796 status=MagickFalse;
1797 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1798 {
1799 MagickBooleanType
1800 proceed;
1801
cristyb5d5f722009-11-04 03:03:49 +00001802#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy7b650b52011-10-09 01:13:39 +00001803 #pragma omp critical (MagickCore_YShearImage)
cristy3ed852e2009-09-05 21:47:34 +00001804#endif
1805 proceed=SetImageProgress(image,YShearImageTag,progress++,image->rows);
1806 if (proceed == MagickFalse)
1807 status=MagickFalse;
1808 }
1809 }
1810 image_view=DestroyCacheView(image_view);
1811 return(status);
1812}
1813
1814/*
1815%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1816% %
1817% %
1818% %
cristy3ed852e2009-09-05 21:47:34 +00001819% S h e a r I m a g e %
1820% %
1821% %
1822% %
1823%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1824%
1825% ShearImage() creates a new image that is a shear_image copy of an existing
cristycee97112010-05-28 00:44:52 +00001826% one. Shearing slides one edge of an image along the X or Y axis, creating
1827% a parallelogram. An X direction shear slides an edge along the X axis,
1828% while a Y direction shear slides an edge along the Y axis. The amount of
cristy3ed852e2009-09-05 21:47:34 +00001829% the shear is controlled by a shear angle. For X direction shears, x_shear
1830% is measured relative to the Y axis, and similarly, for Y direction shears
1831% y_shear is measured relative to the X axis. Empty triangles left over from
1832% shearing the image are filled with the background color defined by member
1833% 'background_color' of the image.. ShearImage() allocates the memory
1834% necessary for the new Image structure and returns a pointer to the new image.
1835%
1836% ShearImage() is based on the paper "A Fast Algorithm for General Raster
1837% Rotatation" by Alan W. Paeth.
1838%
1839% The format of the ShearImage method is:
1840%
1841% Image *ShearImage(const Image *image,const double x_shear,
1842% const double y_shear,ExceptionInfo *exception)
1843%
1844% A description of each parameter follows.
1845%
1846% o image: the image.
1847%
1848% o x_shear, y_shear: Specifies the number of degrees to shear the image.
1849%
1850% o exception: return any errors or warnings in this structure.
1851%
1852*/
1853MagickExport Image *ShearImage(const Image *image,const double x_shear,
1854 const double y_shear,ExceptionInfo *exception)
1855{
1856 Image
1857 *integral_image,
1858 *shear_image;
1859
cristybb503372010-05-27 20:51:26 +00001860 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001861 x_offset,
1862 y_offset;
1863
cristyecc2c142010-01-17 22:25:46 +00001864 MagickBooleanType
1865 status;
1866
cristy3ed852e2009-09-05 21:47:34 +00001867 PointInfo
1868 shear;
1869
1870 RectangleInfo
1871 border_info;
1872
cristybb503372010-05-27 20:51:26 +00001873 size_t
cristy3ed852e2009-09-05 21:47:34 +00001874 y_width;
1875
1876 assert(image != (Image *) NULL);
1877 assert(image->signature == MagickSignature);
1878 if (image->debug != MagickFalse)
1879 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1880 assert(exception != (ExceptionInfo *) NULL);
1881 assert(exception->signature == MagickSignature);
1882 if ((x_shear != 0.0) && (fmod(x_shear,90.0) == 0.0))
1883 ThrowImageException(ImageError,"AngleIsDiscontinuous");
1884 if ((y_shear != 0.0) && (fmod(y_shear,90.0) == 0.0))
1885 ThrowImageException(ImageError,"AngleIsDiscontinuous");
1886 /*
1887 Initialize shear angle.
1888 */
1889 integral_image=CloneImage(image,0,0,MagickTrue,exception);
1890 if (integral_image == (Image *) NULL)
1891 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1892 shear.x=(-tan(DegreesToRadians(fmod(x_shear,360.0))));
1893 shear.y=tan(DegreesToRadians(fmod(y_shear,360.0)));
1894 if ((shear.x == 0.0) && (shear.y == 0.0))
1895 return(integral_image);
cristy574cc262011-08-05 01:23:58 +00001896 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001897 {
cristy3ed852e2009-09-05 21:47:34 +00001898 integral_image=DestroyImage(integral_image);
1899 return(integral_image);
1900 }
cristy8a46d822012-08-28 23:32:39 +00001901 if (integral_image->alpha_trait != BlendPixelTrait)
cristy63240882011-08-05 19:05:27 +00001902 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00001903 /*
1904 Compute image size.
1905 */
cristybb503372010-05-27 20:51:26 +00001906 y_width=image->columns+(ssize_t) floor(fabs(shear.x)*image->rows+0.5);
cristycee97112010-05-28 00:44:52 +00001907 x_offset=(ssize_t) ceil((double) image->columns+((fabs(shear.x)*image->rows)-
cristy06609ee2010-03-17 20:21:27 +00001908 image->columns)/2.0-0.5);
cristycee97112010-05-28 00:44:52 +00001909 y_offset=(ssize_t) ceil((double) image->rows+((fabs(shear.y)*y_width)-
1910 image->rows)/2.0-0.5);
cristy3ed852e2009-09-05 21:47:34 +00001911 /*
1912 Surround image with border.
1913 */
1914 integral_image->border_color=integral_image->background_color;
1915 integral_image->compose=CopyCompositeOp;
cristybb503372010-05-27 20:51:26 +00001916 border_info.width=(size_t) x_offset;
1917 border_info.height=(size_t) y_offset;
cristy633f0c62011-09-15 13:27:36 +00001918 shear_image=BorderImage(integral_image,&border_info,image->compose,exception);
cristyecc2c142010-01-17 22:25:46 +00001919 integral_image=DestroyImage(integral_image);
cristy3ed852e2009-09-05 21:47:34 +00001920 if (shear_image == (Image *) NULL)
1921 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +00001922 /*
1923 Shear the image.
1924 */
cristy8a46d822012-08-28 23:32:39 +00001925 if (shear_image->alpha_trait != BlendPixelTrait)
cristy63240882011-08-05 19:05:27 +00001926 (void) SetImageAlphaChannel(shear_image,OpaqueAlphaChannel,exception);
cristyecc2c142010-01-17 22:25:46 +00001927 status=XShearImage(shear_image,shear.x,image->columns,image->rows,x_offset,
cristyeaedf062010-05-29 22:36:02 +00001928 (ssize_t) (shear_image->rows-image->rows)/2,exception);
cristyecc2c142010-01-17 22:25:46 +00001929 if (status == MagickFalse)
1930 {
1931 shear_image=DestroyImage(shear_image);
1932 return((Image *) NULL);
1933 }
cristyeaedf062010-05-29 22:36:02 +00001934 status=YShearImage(shear_image,shear.y,y_width,image->rows,(ssize_t)
1935 (shear_image->columns-y_width)/2,y_offset,exception);
cristyecc2c142010-01-17 22:25:46 +00001936 if (status == MagickFalse)
1937 {
1938 shear_image=DestroyImage(shear_image);
1939 return((Image *) NULL);
1940 }
cristya19f1d72012-08-07 18:24:38 +00001941 status=CropToFitImage(&shear_image,shear.x,shear.y,(double)
1942 image->columns,(double) image->rows,MagickFalse,exception);
cristy3ed852e2009-09-05 21:47:34 +00001943 shear_image->compose=image->compose;
1944 shear_image->page.width=0;
1945 shear_image->page.height=0;
cristy1c2f48d2012-12-14 01:20:55 +00001946 if (status == MagickFalse)
1947 shear_image=DestroyImage(shear_image);
cristy3ed852e2009-09-05 21:47:34 +00001948 return(shear_image);
1949}
cristyc7c81142011-11-07 12:14:23 +00001950
1951/*
1952%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1953% %
1954% %
1955% %
1956% S h e a r R o t a t e I m a g e %
1957% %
1958% %
1959% %
1960%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1961%
1962% ShearRotateImage() creates a new image that is a rotated copy of an existing
1963% one. Positive angles rotate counter-clockwise (right-hand rule), while
1964% negative angles rotate clockwise. Rotated images are usually larger than
1965% the originals and have 'empty' triangular corners. X axis. Empty
1966% triangles left over from shearing the image are filled with the background
1967% color defined by member 'background_color' of the image. ShearRotateImage
1968% allocates the memory necessary for the new Image structure and returns a
1969% pointer to the new image.
1970%
1971% ShearRotateImage() is based on the paper "A Fast Algorithm for General
1972% Raster Rotatation" by Alan W. Paeth. ShearRotateImage is adapted from a
1973% similar method based on the Paeth paper written by Michael Halle of the
1974% Spatial Imaging Group, MIT Media Lab.
1975%
1976% The format of the ShearRotateImage method is:
1977%
1978% Image *ShearRotateImage(const Image *image,const double degrees,
1979% ExceptionInfo *exception)
1980%
1981% A description of each parameter follows.
1982%
1983% o image: the image.
1984%
1985% o degrees: Specifies the number of degrees to rotate the image.
1986%
1987% o exception: return any errors or warnings in this structure.
1988%
1989*/
1990MagickExport Image *ShearRotateImage(const Image *image,const double degrees,
1991 ExceptionInfo *exception)
1992{
1993 Image
1994 *integral_image,
1995 *rotate_image;
1996
1997 MagickBooleanType
1998 status;
1999
cristya19f1d72012-08-07 18:24:38 +00002000 double
cristyc7c81142011-11-07 12:14:23 +00002001 angle;
2002
2003 PointInfo
2004 shear;
2005
2006 RectangleInfo
2007 border_info;
2008
2009 size_t
2010 height,
2011 rotations,
2012 width,
2013 y_width;
2014
2015 ssize_t
2016 x_offset,
2017 y_offset;
2018
2019 /*
2020 Adjust rotation angle.
2021 */
2022 assert(image != (Image *) NULL);
2023 assert(image->signature == MagickSignature);
2024 if (image->debug != MagickFalse)
2025 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2026 assert(exception != (ExceptionInfo *) NULL);
2027 assert(exception->signature == MagickSignature);
2028 angle=degrees;
2029 while (angle < -45.0)
2030 angle+=360.0;
2031 for (rotations=0; angle > 45.0; rotations++)
2032 angle-=90.0;
2033 rotations%=4;
2034 /*
2035 Calculate shear equations.
2036 */
2037 integral_image=IntegralRotateImage(image,rotations,exception);
2038 if (integral_image == (Image *) NULL)
2039 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2040 shear.x=(-tan((double) DegreesToRadians(angle)/2.0));
2041 shear.y=sin((double) DegreesToRadians(angle));
2042 if ((shear.x == 0.0) && (shear.y == 0.0))
2043 return(integral_image);
2044 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
2045 {
2046 integral_image=DestroyImage(integral_image);
2047 return(integral_image);
2048 }
cristy8a46d822012-08-28 23:32:39 +00002049 if (integral_image->alpha_trait != BlendPixelTrait)
cristyc7c81142011-11-07 12:14:23 +00002050 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
2051 /*
2052 Compute image size.
2053 */
2054 width=image->columns;
2055 height=image->rows;
2056 if ((rotations == 1) || (rotations == 3))
2057 {
2058 width=image->rows;
2059 height=image->columns;
2060 }
2061 y_width=width+(ssize_t) floor(fabs(shear.x)*height+0.5);
2062 x_offset=(ssize_t) ceil((double) width+((fabs(shear.y)*height)-width)/2.0-
2063 0.5);
2064 y_offset=(ssize_t) ceil((double) height+((fabs(shear.y)*y_width)-height)/2.0-
2065 0.5);
2066 /*
2067 Surround image with a border.
2068 */
2069 integral_image->border_color=integral_image->background_color;
2070 integral_image->compose=CopyCompositeOp;
2071 border_info.width=(size_t) x_offset;
2072 border_info.height=(size_t) y_offset;
2073 rotate_image=BorderImage(integral_image,&border_info,image->compose,
2074 exception);
2075 integral_image=DestroyImage(integral_image);
2076 if (rotate_image == (Image *) NULL)
2077 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2078 /*
2079 Rotate the image.
2080 */
2081 status=XShearImage(rotate_image,shear.x,width,height,x_offset,(ssize_t)
2082 (rotate_image->rows-height)/2,exception);
2083 if (status == MagickFalse)
2084 {
2085 rotate_image=DestroyImage(rotate_image);
2086 return((Image *) NULL);
2087 }
2088 status=YShearImage(rotate_image,shear.y,y_width,height,(ssize_t)
2089 (rotate_image->columns-y_width)/2,y_offset,exception);
2090 if (status == MagickFalse)
2091 {
2092 rotate_image=DestroyImage(rotate_image);
2093 return((Image *) NULL);
2094 }
2095 status=XShearImage(rotate_image,shear.x,y_width,rotate_image->rows,(ssize_t)
2096 (rotate_image->columns-y_width)/2,0,exception);
2097 if (status == MagickFalse)
2098 {
2099 rotate_image=DestroyImage(rotate_image);
2100 return((Image *) NULL);
2101 }
cristya19f1d72012-08-07 18:24:38 +00002102 status=CropToFitImage(&rotate_image,shear.x,shear.y,(double) width,
2103 (double) height,MagickTrue,exception);
cristyc7c81142011-11-07 12:14:23 +00002104 rotate_image->compose=image->compose;
2105 rotate_image->page.width=0;
2106 rotate_image->page.height=0;
cristy1c2f48d2012-12-14 01:20:55 +00002107 if (status == MagickFalse)
2108 rotate_image=DestroyImage(rotate_image);
cristyc7c81142011-11-07 12:14:23 +00002109 return(rotate_image);
2110}