blob: d8b7881d9bcb088fe3dde510af97bda39a423a79 [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
539 p=source_cells;
540 q=destination_cells;
541 for (step=1; step < p->width; step*=2)
542 {
cristyeaedf062010-05-29 22:36:02 +0000543 for (x=0; x < (ssize_t) p->width; x+=2*(ssize_t) step)
cristy3ed852e2009-09-05 21:47:34 +0000544 {
cristybb503372010-05-27 20:51:26 +0000545 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000546 i;
547
cristyd40deb62011-03-09 00:52:27 +0000548 ssize_t
549 y;
550
cristy3ed852e2009-09-05 21:47:34 +0000551 unsigned short
552 cell;
553
cristybb503372010-05-27 20:51:26 +0000554 for (i=0; i < (ssize_t) step; i++)
cristy3ed852e2009-09-05 21:47:34 +0000555 {
cristybb503372010-05-27 20:51:26 +0000556 for (y=0; y < (ssize_t) (p->height-i-1); y++)
cristy3ed852e2009-09-05 21:47:34 +0000557 {
558 cell=GetRadonCell(p,x+i,y);
cristyeaedf062010-05-29 22:36:02 +0000559 (void) SetRadonCell(q,x+2*i,y,cell+GetRadonCell(p,x+i+(ssize_t)
560 step,y+i));
561 (void) SetRadonCell(q,x+2*i+1,y,cell+GetRadonCell(p,x+i+(ssize_t)
562 step,y+i+1));
cristy3ed852e2009-09-05 21:47:34 +0000563 }
cristybb503372010-05-27 20:51:26 +0000564 for ( ; y < (ssize_t) (p->height-i); y++)
cristy3ed852e2009-09-05 21:47:34 +0000565 {
566 cell=GetRadonCell(p,x+i,y);
cristyeaedf062010-05-29 22:36:02 +0000567 (void) SetRadonCell(q,x+2*i,y,cell+GetRadonCell(p,x+i+(ssize_t) step,
568 y+i));
cristy3ed852e2009-09-05 21:47:34 +0000569 (void) SetRadonCell(q,x+2*i+1,y,cell);
570 }
cristybb503372010-05-27 20:51:26 +0000571 for ( ; y < (ssize_t) p->height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000572 {
573 cell=GetRadonCell(p,x+i,y);
574 (void) SetRadonCell(q,x+2*i,y,cell);
575 (void) SetRadonCell(q,x+2*i+1,y,cell);
576 }
577 }
578 }
579 swap=p;
580 p=q;
581 q=swap;
582 }
cristyb5d5f722009-11-04 03:03:49 +0000583#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000584 #pragma omp parallel for schedule(static,4) \
cristycb7dfcc2013-01-06 18:34:59 +0000585 magick_threads(image,image,1,1)
cristy3ed852e2009-09-05 21:47:34 +0000586#endif
cristybb503372010-05-27 20:51:26 +0000587 for (x=0; x < (ssize_t) p->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000588 {
cristybb503372010-05-27 20:51:26 +0000589 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000590 y;
591
cristybb503372010-05-27 20:51:26 +0000592 size_t
cristy3ed852e2009-09-05 21:47:34 +0000593 sum;
594
595 sum=0;
cristybb503372010-05-27 20:51:26 +0000596 for (y=0; y < (ssize_t) (p->height-1); y++)
cristy3ed852e2009-09-05 21:47:34 +0000597 {
cristybb503372010-05-27 20:51:26 +0000598 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000599 delta;
600
cristybb503372010-05-27 20:51:26 +0000601 delta=GetRadonCell(p,x,y)-(ssize_t) GetRadonCell(p,x,y+1);
cristy3ed852e2009-09-05 21:47:34 +0000602 sum+=delta*delta;
603 }
604 projection[p->width+sign*x-1]=sum;
605 }
606}
607
608static MagickBooleanType RadonTransform(const Image *image,
cristybb503372010-05-27 20:51:26 +0000609 const double threshold,size_t *projection,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000610{
611 CacheView
612 *image_view;
613
cristy3ed852e2009-09-05 21:47:34 +0000614 MagickBooleanType
615 status;
616
617 RadonInfo
618 *destination_cells,
619 *source_cells;
620
cristybb503372010-05-27 20:51:26 +0000621 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000622 i;
623
cristybb503372010-05-27 20:51:26 +0000624 size_t
cristy3ed852e2009-09-05 21:47:34 +0000625 count,
626 width;
627
cristyd40deb62011-03-09 00:52:27 +0000628 ssize_t
629 y;
630
631 unsigned char
632 byte;
633
cristy3ed852e2009-09-05 21:47:34 +0000634 unsigned short
635 bits[256];
636
637 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
638 source_cells=AcquireRadonInfo(image,width,image->rows,exception);
639 destination_cells=AcquireRadonInfo(image,width,image->rows,exception);
640 if ((source_cells == (RadonInfo *) NULL) ||
641 (destination_cells == (RadonInfo *) NULL))
642 {
643 if (destination_cells != (RadonInfo *) NULL)
644 destination_cells=DestroyRadonInfo(destination_cells);
645 if (source_cells != (RadonInfo *) NULL)
646 source_cells=DestroyRadonInfo(source_cells);
647 return(MagickFalse);
648 }
649 if (ResetRadonCells(source_cells) == MagickFalse)
650 {
651 destination_cells=DestroyRadonInfo(destination_cells);
652 source_cells=DestroyRadonInfo(source_cells);
653 return(MagickFalse);
654 }
655 for (i=0; i < 256; i++)
656 {
657 byte=(unsigned char) i;
658 for (count=0; byte != 0; byte>>=1)
659 count+=byte & 0x01;
660 bits[i]=(unsigned short) count;
661 }
662 status=MagickTrue;
cristy46ff2672012-12-14 15:32:26 +0000663 image_view=AcquireVirtualCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +0000664#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000665 #pragma omp parallel for schedule(static,4) shared(status) \
cristy5e6b2592012-12-19 14:08:11 +0000666 magick_threads(image,image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +0000667#endif
cristybb503372010-05-27 20:51:26 +0000668 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000669 {
cristy4c08aed2011-07-01 19:47:50 +0000670 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000671 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000672
cristybb503372010-05-27 20:51:26 +0000673 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000674 i,
675 x;
676
cristybb503372010-05-27 20:51:26 +0000677 size_t
cristy3ed852e2009-09-05 21:47:34 +0000678 bit,
679 byte;
680
681 if (status == MagickFalse)
682 continue;
683 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000684 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000685 {
686 status=MagickFalse;
687 continue;
688 }
689 bit=0;
690 byte=0;
cristybb503372010-05-27 20:51:26 +0000691 i=(ssize_t) (image->columns+7)/8;
692 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000693 {
694 byte<<=1;
cristyf13c5942012-08-08 23:50:11 +0000695 if (GetPixelIntensity(image,p) < threshold)
cristy3ed852e2009-09-05 21:47:34 +0000696 byte|=0x01;
697 bit++;
698 if (bit == 8)
699 {
700 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
701 bit=0;
702 byte=0;
703 }
cristyed231572011-07-14 02:18:59 +0000704 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000705 }
706 if (bit != 0)
707 {
708 byte<<=(8-bit);
709 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
710 }
711 }
cristy4ee2b0c2012-05-15 00:30:35 +0000712 RadonProjection(image,source_cells,destination_cells,-1,projection);
cristy3ed852e2009-09-05 21:47:34 +0000713 (void) ResetRadonCells(source_cells);
cristyb5d5f722009-11-04 03:03:49 +0000714#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000715 #pragma omp parallel for schedule(static,4) shared(status) \
cristycb7dfcc2013-01-06 18:34:59 +0000716 magick_threads(image,image,1,1)
cristy3ed852e2009-09-05 21:47:34 +0000717#endif
cristybb503372010-05-27 20:51:26 +0000718 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000719 {
cristy4c08aed2011-07-01 19:47:50 +0000720 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000721 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000722
cristybb503372010-05-27 20:51:26 +0000723 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000724 i,
725 x;
726
cristybb503372010-05-27 20:51:26 +0000727 size_t
cristy3ed852e2009-09-05 21:47:34 +0000728 bit,
729 byte;
730
731 if (status == MagickFalse)
732 continue;
733 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000734 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000735 {
736 status=MagickFalse;
737 continue;
738 }
739 bit=0;
740 byte=0;
741 i=0;
cristybb503372010-05-27 20:51:26 +0000742 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000743 {
744 byte<<=1;
cristyf13c5942012-08-08 23:50:11 +0000745 if (GetPixelIntensity(image,p) < threshold)
cristy3ed852e2009-09-05 21:47:34 +0000746 byte|=0x01;
747 bit++;
748 if (bit == 8)
749 {
750 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
751 bit=0;
752 byte=0;
753 }
cristyed231572011-07-14 02:18:59 +0000754 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000755 }
756 if (bit != 0)
757 {
758 byte<<=(8-bit);
759 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
760 }
761 }
cristy4ee2b0c2012-05-15 00:30:35 +0000762 RadonProjection(image,source_cells,destination_cells,1,projection);
cristy3ed852e2009-09-05 21:47:34 +0000763 image_view=DestroyCacheView(image_view);
764 destination_cells=DestroyRadonInfo(destination_cells);
765 source_cells=DestroyRadonInfo(source_cells);
766 return(MagickTrue);
767}
768
cristybb503372010-05-27 20:51:26 +0000769static void GetImageBackgroundColor(Image *image,const ssize_t offset,
cristy3ed852e2009-09-05 21:47:34 +0000770 ExceptionInfo *exception)
771{
772 CacheView
773 *image_view;
774
cristy4c08aed2011-07-01 19:47:50 +0000775 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000776 background;
777
cristya19f1d72012-08-07 18:24:38 +0000778 double
cristy3ed852e2009-09-05 21:47:34 +0000779 count;
780
cristyd40deb62011-03-09 00:52:27 +0000781 ssize_t
782 y;
783
cristy3ed852e2009-09-05 21:47:34 +0000784 /*
785 Compute average background color.
786 */
787 if (offset <= 0)
788 return;
cristy4c08aed2011-07-01 19:47:50 +0000789 GetPixelInfo(image,&background);
cristy3ed852e2009-09-05 21:47:34 +0000790 count=0.0;
cristy46ff2672012-12-14 15:32:26 +0000791 image_view=AcquireVirtualCacheView(image,exception);
cristybb503372010-05-27 20:51:26 +0000792 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000793 {
cristy4c08aed2011-07-01 19:47:50 +0000794 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000795 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000796
cristybb503372010-05-27 20:51:26 +0000797 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000798 x;
799
cristybb503372010-05-27 20:51:26 +0000800 if ((y >= offset) && (y < ((ssize_t) image->rows-offset)))
cristy3ed852e2009-09-05 21:47:34 +0000801 continue;
802 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000803 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000804 continue;
cristybb503372010-05-27 20:51:26 +0000805 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000806 {
cristybb503372010-05-27 20:51:26 +0000807 if ((x >= offset) && (x < ((ssize_t) image->columns-offset)))
cristy3ed852e2009-09-05 21:47:34 +0000808 continue;
cristy4c08aed2011-07-01 19:47:50 +0000809 background.red+=QuantumScale*GetPixelRed(image,p);
810 background.green+=QuantumScale*GetPixelGreen(image,p);
811 background.blue+=QuantumScale*GetPixelBlue(image,p);
cristy26295322011-09-22 00:50:36 +0000812 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
813 background.alpha+=QuantumScale*GetPixelAlpha(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000814 count++;
cristyed231572011-07-14 02:18:59 +0000815 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000816 }
817 }
818 image_view=DestroyCacheView(image_view);
cristy8cd03c32012-07-07 18:57:59 +0000819 image->background_color.red=(double) ClampToQuantum(QuantumRange*
820 background.red/count);
821 image->background_color.green=(double) ClampToQuantum(QuantumRange*
822 background.green/count);
823 image->background_color.blue=(double) ClampToQuantum(QuantumRange*
824 background.blue/count);
cristy26295322011-09-22 00:50:36 +0000825 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy8cd03c32012-07-07 18:57:59 +0000826 image->background_color.alpha=(double) ClampToQuantum(QuantumRange*
827 background.alpha/count);
cristy3ed852e2009-09-05 21:47:34 +0000828}
829
830MagickExport Image *DeskewImage(const Image *image,const double threshold,
831 ExceptionInfo *exception)
832{
833 AffineMatrix
834 affine_matrix;
835
836 const char
837 *artifact;
838
839 double
840 degrees;
841
842 Image
843 *clone_image,
844 *crop_image,
845 *deskew_image,
846 *median_image;
847
cristy3ed852e2009-09-05 21:47:34 +0000848 MagickBooleanType
849 status;
850
851 RectangleInfo
852 geometry;
853
cristybb503372010-05-27 20:51:26 +0000854 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000855 i;
856
cristybb503372010-05-27 20:51:26 +0000857 size_t
cristy3ed852e2009-09-05 21:47:34 +0000858 max_projection,
859 *projection,
860 width;
861
cristyd40deb62011-03-09 00:52:27 +0000862 ssize_t
863 skew;
864
cristy3ed852e2009-09-05 21:47:34 +0000865 /*
866 Compute deskew angle.
867 */
868 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
cristybb503372010-05-27 20:51:26 +0000869 projection=(size_t *) AcquireQuantumMemory((size_t) (2*width-1),
cristy3ed852e2009-09-05 21:47:34 +0000870 sizeof(*projection));
cristybb503372010-05-27 20:51:26 +0000871 if (projection == (size_t *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000872 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
873 status=RadonTransform(image,threshold,projection,exception);
874 if (status == MagickFalse)
875 {
cristybb503372010-05-27 20:51:26 +0000876 projection=(size_t *) RelinquishMagickMemory(projection);
cristy3ed852e2009-09-05 21:47:34 +0000877 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
878 }
879 max_projection=0;
880 skew=0;
cristybb503372010-05-27 20:51:26 +0000881 for (i=0; i < (ssize_t) (2*width-1); i++)
cristy3ed852e2009-09-05 21:47:34 +0000882 {
883 if (projection[i] > max_projection)
884 {
cristybb503372010-05-27 20:51:26 +0000885 skew=i-(ssize_t) width+1;
cristy3ed852e2009-09-05 21:47:34 +0000886 max_projection=projection[i];
887 }
888 }
cristybb503372010-05-27 20:51:26 +0000889 projection=(size_t *) RelinquishMagickMemory(projection);
anthony553bfcc2013-04-10 01:27:43 +0000890 degrees=RadiansToDegrees(-atan((double) skew/width/8));
891 if (image->debug != MagickFalse)
892 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
893 " Deskew angle: %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +0000894 /*
895 Deskew image.
896 */
897 clone_image=CloneImage(image,0,0,MagickTrue,exception);
898 if (clone_image == (Image *) NULL)
899 return((Image *) NULL);
anthony8a95e802013-04-10 05:31:57 +0000900 {
901 char
902 angle[MaxTextExtent];
903
904 (void) FormatLocaleString(angle,MaxTextExtent,"%.20g",degrees);
905 (void) SetImageArtifact(clone_image,"deskew:angle",angle);
906 }
cristy387430f2012-02-07 13:09:46 +0000907 (void) SetImageVirtualPixelMethod(clone_image,BackgroundVirtualPixelMethod,
908 exception);
cristy3ed852e2009-09-05 21:47:34 +0000909 affine_matrix.sx=cos(DegreesToRadians(fmod((double) degrees,360.0)));
910 affine_matrix.rx=sin(DegreesToRadians(fmod((double) degrees,360.0)));
911 affine_matrix.ry=(-sin(DegreesToRadians(fmod((double) degrees,360.0))));
912 affine_matrix.sy=cos(DegreesToRadians(fmod((double) degrees,360.0)));
913 affine_matrix.tx=0.0;
914 affine_matrix.ty=0.0;
915 artifact=GetImageArtifact(image,"deskew:auto-crop");
916 if (artifact == (const char *) NULL)
917 {
918 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
919 clone_image=DestroyImage(clone_image);
920 return(deskew_image);
921 }
922 /*
923 Auto-crop image.
924 */
cristyba978e12010-09-12 20:26:50 +0000925 GetImageBackgroundColor(clone_image,(ssize_t) StringToLong(artifact),
926 exception);
cristy3ed852e2009-09-05 21:47:34 +0000927 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
928 clone_image=DestroyImage(clone_image);
929 if (deskew_image == (Image *) NULL)
930 return((Image *) NULL);
cristy95c38342011-03-18 22:39:51 +0000931 median_image=StatisticImage(deskew_image,MedianStatistic,3,3,exception);
cristy3ed852e2009-09-05 21:47:34 +0000932 if (median_image == (Image *) NULL)
933 {
934 deskew_image=DestroyImage(deskew_image);
935 return((Image *) NULL);
936 }
937 geometry=GetImageBoundingBox(median_image,exception);
938 median_image=DestroyImage(median_image);
939 if (image->debug != MagickFalse)
940 (void) LogMagickEvent(TransformEvent,GetMagickModule()," Deskew geometry: "
cristy6d8abba2010-06-03 01:10:47 +0000941 "%.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
cristye8c25f92010-06-03 00:53:06 +0000942 geometry.height,(double) geometry.x,(double) geometry.y);
cristy3ed852e2009-09-05 21:47:34 +0000943 crop_image=CropImage(deskew_image,&geometry,exception);
944 deskew_image=DestroyImage(deskew_image);
945 return(crop_image);
946}
947
948/*
949%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
950% %
951% %
952% %
cristy987feef2011-11-17 12:24:56 +0000953% 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 +0000954% %
955% %
956% %
957%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
958%
cristy3dfa16c2010-05-17 19:34:48 +0000959% IntegralRotateImage() rotates the image an integral of 90 degrees. It
cristy3ed852e2009-09-05 21:47:34 +0000960% allocates the memory necessary for the new Image structure and returns a
961% pointer to the rotated image.
962%
963% The format of the IntegralRotateImage method is:
964%
cristybb503372010-05-27 20:51:26 +0000965% Image *IntegralRotateImage(const Image *image,size_t rotations,
cristy3ed852e2009-09-05 21:47:34 +0000966% ExceptionInfo *exception)
967%
968% A description of each parameter follows.
969%
970% o image: the image.
971%
972% o rotations: Specifies the number of 90 degree rotations.
973%
974*/
cristy987feef2011-11-17 12:24:56 +0000975MagickExport Image *IntegralRotateImage(const Image *image,size_t rotations,
cristy3ed852e2009-09-05 21:47:34 +0000976 ExceptionInfo *exception)
977{
cristy3ed852e2009-09-05 21:47:34 +0000978#define RotateImageTag "Rotate/Image"
979
980 CacheView
981 *image_view,
982 *rotate_view;
983
984 Image
985 *rotate_image;
986
cristy3ed852e2009-09-05 21:47:34 +0000987 MagickBooleanType
988 status;
989
cristy5f959472010-05-27 22:19:46 +0000990 MagickOffsetType
991 progress;
992
cristy3ed852e2009-09-05 21:47:34 +0000993 RectangleInfo
994 page;
995
cristy5f959472010-05-27 22:19:46 +0000996 ssize_t
997 y;
998
cristy3ed852e2009-09-05 21:47:34 +0000999 /*
1000 Initialize rotated image attributes.
1001 */
1002 assert(image != (Image *) NULL);
1003 page=image->page;
1004 rotations%=4;
cristyb32b90a2009-09-07 21:45:48 +00001005 if (rotations == 0)
1006 return(CloneImage(image,0,0,MagickTrue,exception));
cristy3ed852e2009-09-05 21:47:34 +00001007 if ((rotations == 1) || (rotations == 3))
1008 rotate_image=CloneImage(image,image->rows,image->columns,MagickTrue,
1009 exception);
1010 else
1011 rotate_image=CloneImage(image,image->columns,image->rows,MagickTrue,
1012 exception);
1013 if (rotate_image == (Image *) NULL)
1014 return((Image *) NULL);
1015 /*
1016 Integral rotate the image.
1017 */
1018 status=MagickTrue;
1019 progress=0;
cristy46ff2672012-12-14 15:32:26 +00001020 image_view=AcquireVirtualCacheView(image,exception);
1021 rotate_view=AcquireAuthenticCacheView(rotate_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001022 switch (rotations)
1023 {
1024 case 0:
1025 {
1026 /*
1027 Rotate 0 degrees.
1028 */
cristy3ed852e2009-09-05 21:47:34 +00001029 break;
1030 }
1031 case 1:
1032 {
cristybb503372010-05-27 20:51:26 +00001033 size_t
cristyb32b90a2009-09-07 21:45:48 +00001034 tile_height,
1035 tile_width;
1036
cristyd40deb62011-03-09 00:52:27 +00001037 ssize_t
1038 tile_y;
1039
cristy3ed852e2009-09-05 21:47:34 +00001040 /*
1041 Rotate 90 degrees.
1042 */
cristyb32b90a2009-09-07 21:45:48 +00001043 GetPixelCacheTileSize(image,&tile_width,&tile_height);
cristy8b8148b2012-10-07 00:41:15 +00001044 tile_width=image->columns;
cristy26b64912012-12-16 18:20:09 +00001045#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy64c10cd2013-01-06 18:40:43 +00001046 #pragma omp parallel for schedule(static,4) shared(status) \
cristy3e1fa372013-01-06 18:07:11 +00001047 magick_threads(image,image,1,1)
cristy9a5a52f2012-10-09 14:40:31 +00001048#endif
cristyad11aac2011-09-25 21:29:16 +00001049 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
cristy3ed852e2009-09-05 21:47:34 +00001050 {
cristybb503372010-05-27 20:51:26 +00001051 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001052 tile_x;
1053
1054 if (status == MagickFalse)
1055 continue;
cristy26295322011-09-22 00:50:36 +00001056 tile_x=0;
1057 for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
cristy3ed852e2009-09-05 21:47:34 +00001058 {
1059 MagickBooleanType
1060 sync;
1061
cristy4c08aed2011-07-01 19:47:50 +00001062 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001063 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001064
cristy4c08aed2011-07-01 19:47:50 +00001065 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001066 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001067
cristya26211e2011-10-09 01:24:57 +00001068 register ssize_t
1069 y;
1070
cristybb503372010-05-27 20:51:26 +00001071 size_t
cristyb32b90a2009-09-07 21:45:48 +00001072 height,
1073 width;
cristy3ed852e2009-09-05 21:47:34 +00001074
cristyb32b90a2009-09-07 21:45:48 +00001075 width=tile_width;
cristybb503372010-05-27 20:51:26 +00001076 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
cristya45d2ec2011-08-24 13:17:19 +00001077 width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
cristyb32b90a2009-09-07 21:45:48 +00001078 height=tile_height;
cristybb503372010-05-27 20:51:26 +00001079 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
cristya45d2ec2011-08-24 13:17:19 +00001080 height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
cristydaa97692009-09-13 02:10:35 +00001081 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1082 exception);
cristy4c08aed2011-07-01 19:47:50 +00001083 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001084 {
1085 status=MagickFalse;
1086 break;
1087 }
cristybb503372010-05-27 20:51:26 +00001088 for (y=0; y < (ssize_t) width; y++)
cristy3ed852e2009-09-05 21:47:34 +00001089 {
cristy4c08aed2011-07-01 19:47:50 +00001090 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001091 *restrict tile_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001092
cristybb503372010-05-27 20:51:26 +00001093 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001094 x;
1095
cristy27709ef2011-09-18 02:53:53 +00001096 if (status == MagickFalse)
1097 continue;
cristybb503372010-05-27 20:51:26 +00001098 q=QueueCacheViewAuthenticPixels(rotate_view,(ssize_t)
cristy92611572011-07-07 16:02:42 +00001099 (rotate_image->columns-(tile_y+height)),y+tile_x,height,1,
1100 exception);
cristyacd2ed22011-08-30 01:44:23 +00001101 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001102 {
1103 status=MagickFalse;
cristy27709ef2011-09-18 02:53:53 +00001104 continue;
cristy3ed852e2009-09-05 21:47:34 +00001105 }
cristyed231572011-07-14 02:18:59 +00001106 tile_pixels=p+((height-1)*width+y)*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001107 for (x=0; x < (ssize_t) height; x++)
cristy3ed852e2009-09-05 21:47:34 +00001108 {
cristya45d2ec2011-08-24 13:17:19 +00001109 register ssize_t
1110 i;
1111
cristy883fde12013-04-08 00:50:13 +00001112 if (GetPixelReadMask(image,tile_pixels) == 0)
cristy10a6c612012-01-29 21:41:05 +00001113 {
1114 tile_pixels-=width*GetPixelChannels(image);
1115 q+=GetPixelChannels(rotate_image);
1116 continue;
1117 }
cristya45d2ec2011-08-24 13:17:19 +00001118 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1119 {
cristy5a23c552013-02-13 14:34:28 +00001120 PixelChannel channel=GetPixelChannelChannel(image,i);
1121 PixelTrait traits=GetPixelChannelTraits(image,channel);
1122 PixelTrait rotate_traits=GetPixelChannelTraits(rotate_image,
1123 channel);
cristy010d7d12011-08-31 01:02:48 +00001124 if ((traits == UndefinedPixelTrait) ||
1125 (rotate_traits == UndefinedPixelTrait))
cristya45d2ec2011-08-24 13:17:19 +00001126 continue;
cristy0beccfa2011-09-25 20:47:53 +00001127 SetPixelChannel(rotate_image,channel,tile_pixels[i],q);
cristya45d2ec2011-08-24 13:17:19 +00001128 }
cristyed231572011-07-14 02:18:59 +00001129 tile_pixels-=width*GetPixelChannels(image);
1130 q+=GetPixelChannels(rotate_image);
cristy3ed852e2009-09-05 21:47:34 +00001131 }
cristy3ed852e2009-09-05 21:47:34 +00001132 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1133 if (sync == MagickFalse)
1134 status=MagickFalse;
1135 }
1136 }
1137 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1138 {
1139 MagickBooleanType
1140 proceed;
1141
cristy26b64912012-12-16 18:20:09 +00001142#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy9a5a52f2012-10-09 14:40:31 +00001143 #pragma omp critical (MagickCore_IntegralRotateImage)
1144#endif
cristyb32b90a2009-09-07 21:45:48 +00001145 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
cristy3ed852e2009-09-05 21:47:34 +00001146 image->rows);
1147 if (proceed == MagickFalse)
1148 status=MagickFalse;
1149 }
1150 }
cristyecc2c142010-01-17 22:25:46 +00001151 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1152 image->rows-1,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001153 Swap(page.width,page.height);
1154 Swap(page.x,page.y);
1155 if (page.width != 0)
cristybb503372010-05-27 20:51:26 +00001156 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
cristy3ed852e2009-09-05 21:47:34 +00001157 break;
1158 }
1159 case 2:
1160 {
1161 /*
1162 Rotate 180 degrees.
1163 */
cristy5f890612012-12-16 23:34:05 +00001164#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy64c10cd2013-01-06 18:40:43 +00001165 #pragma omp parallel for schedule(static,4) shared(status) \
cristyd6432472013-01-06 16:56:13 +00001166 magick_threads(image,image,1,1)
cristy5f890612012-12-16 23:34:05 +00001167#endif
cristybb503372010-05-27 20:51:26 +00001168 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001169 {
1170 MagickBooleanType
1171 sync;
1172
cristy4c08aed2011-07-01 19:47:50 +00001173 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001174 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001175
cristy4c08aed2011-07-01 19:47:50 +00001176 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001177 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001178
cristy2be50602011-10-09 01:28:13 +00001179 register ssize_t
1180 x;
1181
cristy3ed852e2009-09-05 21:47:34 +00001182 if (status == MagickFalse)
1183 continue;
cristya45d2ec2011-08-24 13:17:19 +00001184 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1185 q=QueueCacheViewAuthenticPixels(rotate_view,0,(ssize_t) (image->rows-y-
1186 1),image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001187 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00001188 {
1189 status=MagickFalse;
1190 continue;
1191 }
cristyed231572011-07-14 02:18:59 +00001192 q+=GetPixelChannels(rotate_image)*image->columns;
cristybb503372010-05-27 20:51:26 +00001193 for (x=0; x < (ssize_t) image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00001194 {
cristya45d2ec2011-08-24 13:17:19 +00001195 register ssize_t
1196 i;
1197
cristyed231572011-07-14 02:18:59 +00001198 q-=GetPixelChannels(rotate_image);
cristy883fde12013-04-08 00:50:13 +00001199 if (GetPixelReadMask(image,p) == 0)
cristy10a6c612012-01-29 21:41:05 +00001200 {
1201 p+=GetPixelChannels(image);
1202 continue;
1203 }
cristya45d2ec2011-08-24 13:17:19 +00001204 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1205 {
cristy5a23c552013-02-13 14:34:28 +00001206 PixelChannel channel=GetPixelChannelChannel(image,i);
1207 PixelTrait traits=GetPixelChannelTraits(image,channel);
1208 PixelTrait rotate_traits=GetPixelChannelTraits(rotate_image,
1209 channel);
cristy010d7d12011-08-31 01:02:48 +00001210 if ((traits == UndefinedPixelTrait) ||
1211 (rotate_traits == UndefinedPixelTrait))
cristya45d2ec2011-08-24 13:17:19 +00001212 continue;
cristy0beccfa2011-09-25 20:47:53 +00001213 SetPixelChannel(rotate_image,channel,p[i],q);
cristya45d2ec2011-08-24 13:17:19 +00001214 }
cristyed231572011-07-14 02:18:59 +00001215 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001216 }
cristy3ed852e2009-09-05 21:47:34 +00001217 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1218 if (sync == MagickFalse)
1219 status=MagickFalse;
1220 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1221 {
1222 MagickBooleanType
1223 proceed;
1224
cristy5f890612012-12-16 23:34:05 +00001225#if defined(MAGICKCORE_OPENMP_SUPPORT)
1226 #pragma omp critical (MagickCore_IntegralRotateImage)
1227#endif
cristy0729beb2011-09-25 23:29:32 +00001228 proceed=SetImageProgress(image,RotateImageTag,progress++,
cristy21e03412011-09-25 22:39:35 +00001229 image->rows);
1230 if (proceed == MagickFalse)
1231 status=MagickFalse;
1232 }
1233 }
1234 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1235 image->rows-1,image->rows);
1236 Swap(page.width,page.height);
1237 Swap(page.x,page.y);
1238 if (page.width != 0)
1239 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1240 break;
1241 }
cristy3ed852e2009-09-05 21:47:34 +00001242 case 3:
1243 {
cristybb503372010-05-27 20:51:26 +00001244 size_t
cristyb32b90a2009-09-07 21:45:48 +00001245 tile_height,
1246 tile_width;
1247
cristyd40deb62011-03-09 00:52:27 +00001248 ssize_t
1249 tile_y;
1250
cristy3ed852e2009-09-05 21:47:34 +00001251 /*
1252 Rotate 270 degrees.
1253 */
cristyb32b90a2009-09-07 21:45:48 +00001254 GetPixelCacheTileSize(image,&tile_width,&tile_height);
cristy8b8148b2012-10-07 00:41:15 +00001255 tile_width=image->columns;
cristy26b64912012-12-16 18:20:09 +00001256#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy64c10cd2013-01-06 18:40:43 +00001257 #pragma omp parallel for schedule(static,4) shared(status) \
cristycb7dfcc2013-01-06 18:34:59 +00001258 magick_threads(image,image,1,1)
cristy9a5a52f2012-10-09 14:40:31 +00001259#endif
cristyad11aac2011-09-25 21:29:16 +00001260 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
cristy3ed852e2009-09-05 21:47:34 +00001261 {
cristybb503372010-05-27 20:51:26 +00001262 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001263 tile_x;
1264
1265 if (status == MagickFalse)
1266 continue;
cristy26295322011-09-22 00:50:36 +00001267 tile_x=0;
1268 for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
cristy3ed852e2009-09-05 21:47:34 +00001269 {
1270 MagickBooleanType
1271 sync;
1272
cristy4c08aed2011-07-01 19:47:50 +00001273 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001274 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001275
cristy4c08aed2011-07-01 19:47:50 +00001276 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001277 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001278
cristy2be50602011-10-09 01:28:13 +00001279 register ssize_t
1280 y;
1281
cristybb503372010-05-27 20:51:26 +00001282 size_t
cristyb32b90a2009-09-07 21:45:48 +00001283 height,
1284 width;
cristy3ed852e2009-09-05 21:47:34 +00001285
cristyb32b90a2009-09-07 21:45:48 +00001286 width=tile_width;
cristybb503372010-05-27 20:51:26 +00001287 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
cristya45d2ec2011-08-24 13:17:19 +00001288 width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
cristyb32b90a2009-09-07 21:45:48 +00001289 height=tile_height;
cristybb503372010-05-27 20:51:26 +00001290 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
cristya45d2ec2011-08-24 13:17:19 +00001291 height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
1292 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1293 exception);
cristy4c08aed2011-07-01 19:47:50 +00001294 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001295 {
1296 status=MagickFalse;
1297 break;
1298 }
cristybb503372010-05-27 20:51:26 +00001299 for (y=0; y < (ssize_t) width; y++)
cristy3ed852e2009-09-05 21:47:34 +00001300 {
cristy4c08aed2011-07-01 19:47:50 +00001301 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001302 *restrict tile_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001303
cristybb503372010-05-27 20:51:26 +00001304 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001305 x;
1306
cristy27709ef2011-09-18 02:53:53 +00001307 if (status == MagickFalse)
1308 continue;
cristya45d2ec2011-08-24 13:17:19 +00001309 q=QueueCacheViewAuthenticPixels(rotate_view,tile_y,(ssize_t) (y+
1310 rotate_image->rows-(tile_x+width)),height,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001311 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001312 {
1313 status=MagickFalse;
cristy27709ef2011-09-18 02:53:53 +00001314 continue;
cristy3ed852e2009-09-05 21:47:34 +00001315 }
cristyed231572011-07-14 02:18:59 +00001316 tile_pixels=p+((width-1)-y)*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001317 for (x=0; x < (ssize_t) height; x++)
cristy3ed852e2009-09-05 21:47:34 +00001318 {
cristya45d2ec2011-08-24 13:17:19 +00001319 register ssize_t
1320 i;
1321
cristy883fde12013-04-08 00:50:13 +00001322 if (GetPixelReadMask(image,tile_pixels) == 0)
cristy10a6c612012-01-29 21:41:05 +00001323 {
1324 tile_pixels+=width*GetPixelChannels(image);
1325 q+=GetPixelChannels(rotate_image);
1326 continue;
1327 }
cristya45d2ec2011-08-24 13:17:19 +00001328 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1329 {
cristy5a23c552013-02-13 14:34:28 +00001330 PixelChannel channel=GetPixelChannelChannel(image,i);
1331 PixelTrait traits=GetPixelChannelTraits(image,channel);
1332 PixelTrait rotate_traits=GetPixelChannelTraits(rotate_image,
1333 channel);
cristy010d7d12011-08-31 01:02:48 +00001334 if ((traits == UndefinedPixelTrait) ||
1335 (rotate_traits == UndefinedPixelTrait))
cristya45d2ec2011-08-24 13:17:19 +00001336 continue;
cristy0beccfa2011-09-25 20:47:53 +00001337 SetPixelChannel(rotate_image,channel,tile_pixels[i],q);
cristya45d2ec2011-08-24 13:17:19 +00001338 }
cristyed231572011-07-14 02:18:59 +00001339 tile_pixels+=width*GetPixelChannels(image);
1340 q+=GetPixelChannels(rotate_image);
cristy3ed852e2009-09-05 21:47:34 +00001341 }
cristy26b64912012-12-16 18:20:09 +00001342#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy9a5a52f2012-10-09 14:40:31 +00001343 #pragma omp critical (MagickCore_IntegralRotateImage)
1344#endif
cristy3ed852e2009-09-05 21:47:34 +00001345 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1346 if (sync == MagickFalse)
1347 status=MagickFalse;
1348 }
1349 }
1350 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1351 {
1352 MagickBooleanType
1353 proceed;
1354
cristy21e03412011-09-25 22:39:35 +00001355 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
1356 image->rows);
1357 if (proceed == MagickFalse)
1358 status=MagickFalse;
1359 }
1360 }
1361 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1362 image->rows-1,image->rows);
1363 Swap(page.width,page.height);
1364 Swap(page.x,page.y);
1365 if (page.width != 0)
1366 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1367 break;
1368 }
cristy3ed852e2009-09-05 21:47:34 +00001369 }
1370 rotate_view=DestroyCacheView(rotate_view);
1371 image_view=DestroyCacheView(image_view);
1372 rotate_image->type=image->type;
1373 rotate_image->page=page;
1374 if (status == MagickFalse)
1375 rotate_image=DestroyImage(rotate_image);
1376 return(rotate_image);
1377}
1378
1379/*
1380%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1381% %
1382% %
1383% %
1384+ X S h e a r I m a g e %
1385% %
1386% %
1387% %
1388%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1389%
1390% XShearImage() shears the image in the X direction with a shear angle of
1391% 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1392% negative angles shear clockwise. Angles are measured relative to a vertical
1393% Y-axis. X shears will widen an image creating 'empty' triangles on the left
1394% and right sides of the source image.
1395%
1396% The format of the XShearImage method is:
1397%
cristya19f1d72012-08-07 18:24:38 +00001398% MagickBooleanType XShearImage(Image *image,const double degrees,
cristybb503372010-05-27 20:51:26 +00001399% const size_t width,const size_t height,
1400% const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001401%
1402% A description of each parameter follows.
1403%
1404% o image: the image.
1405%
cristya19f1d72012-08-07 18:24:38 +00001406% o degrees: A double representing the shearing angle along the X
cristy3ed852e2009-09-05 21:47:34 +00001407% axis.
1408%
1409% o width, height, x_offset, y_offset: Defines a region of the image
1410% to shear.
1411%
cristyecc2c142010-01-17 22:25:46 +00001412% o exception: return any errors or warnings in this structure.
1413%
cristy3ed852e2009-09-05 21:47:34 +00001414*/
cristya19f1d72012-08-07 18:24:38 +00001415static MagickBooleanType XShearImage(Image *image,const double degrees,
cristybb503372010-05-27 20:51:26 +00001416 const size_t width,const size_t height,const ssize_t x_offset,
1417 const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001418{
1419#define XShearImageTag "XShear/Image"
1420
1421 typedef enum
1422 {
1423 LEFT,
1424 RIGHT
1425 } ShearDirection;
1426
1427 CacheView
1428 *image_view;
1429
cristy3ed852e2009-09-05 21:47:34 +00001430 MagickBooleanType
1431 status;
1432
cristy5f959472010-05-27 22:19:46 +00001433 MagickOffsetType
1434 progress;
1435
cristy4c08aed2011-07-01 19:47:50 +00001436 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001437 background;
1438
cristy5f959472010-05-27 22:19:46 +00001439 ssize_t
1440 y;
1441
cristy9d8c8ce2011-10-25 16:13:52 +00001442 /*
1443 X shear image.
1444 */
cristy3ed852e2009-09-05 21:47:34 +00001445 assert(image != (Image *) NULL);
1446 assert(image->signature == MagickSignature);
1447 if (image->debug != MagickFalse)
1448 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3ed852e2009-09-05 21:47:34 +00001449 status=MagickTrue;
cristy9d8c8ce2011-10-25 16:13:52 +00001450 background=image->background_color;
cristy3ed852e2009-09-05 21:47:34 +00001451 progress=0;
cristy46ff2672012-12-14 15:32:26 +00001452 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00001453#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00001454 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00001455 magick_threads(image,image,height,1)
cristy3ed852e2009-09-05 21:47:34 +00001456#endif
cristybb503372010-05-27 20:51:26 +00001457 for (y=0; y < (ssize_t) height; y++)
cristy3ed852e2009-09-05 21:47:34 +00001458 {
cristy4c08aed2011-07-01 19:47:50 +00001459 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001460 pixel,
1461 source,
1462 destination;
1463
cristya19f1d72012-08-07 18:24:38 +00001464 double
cristy3ed852e2009-09-05 21:47:34 +00001465 area,
1466 displacement;
1467
cristy4c08aed2011-07-01 19:47:50 +00001468 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001469 *restrict p,
1470 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001471
cristyd40deb62011-03-09 00:52:27 +00001472 register ssize_t
1473 i;
1474
cristy3ed852e2009-09-05 21:47:34 +00001475 ShearDirection
1476 direction;
1477
cristyd40deb62011-03-09 00:52:27 +00001478 ssize_t
1479 step;
1480
cristy3ed852e2009-09-05 21:47:34 +00001481 if (status == MagickFalse)
1482 continue;
1483 p=GetCacheViewAuthenticPixels(image_view,0,y_offset+y,image->columns,1,
1484 exception);
cristy4c08aed2011-07-01 19:47:50 +00001485 if (p == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001486 {
1487 status=MagickFalse;
1488 continue;
1489 }
cristyed231572011-07-14 02:18:59 +00001490 p+=x_offset*GetPixelChannels(image);
cristya19f1d72012-08-07 18:24:38 +00001491 displacement=degrees*(double) (y-height/2.0);
cristy3ed852e2009-09-05 21:47:34 +00001492 if (displacement == 0.0)
1493 continue;
1494 if (displacement > 0.0)
1495 direction=RIGHT;
1496 else
1497 {
1498 displacement*=(-1.0);
1499 direction=LEFT;
1500 }
cristybb503372010-05-27 20:51:26 +00001501 step=(ssize_t) floor((double) displacement);
cristya19f1d72012-08-07 18:24:38 +00001502 area=(double) (displacement-step);
cristy3ed852e2009-09-05 21:47:34 +00001503 step++;
1504 pixel=background;
cristy4c08aed2011-07-01 19:47:50 +00001505 GetPixelInfo(image,&source);
1506 GetPixelInfo(image,&destination);
cristy3ed852e2009-09-05 21:47:34 +00001507 switch (direction)
1508 {
1509 case LEFT:
1510 {
1511 /*
1512 Transfer pixels left-to-right.
1513 */
1514 if (step > x_offset)
1515 break;
cristyed231572011-07-14 02:18:59 +00001516 q=p-step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001517 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001518 {
1519 if ((x_offset+i) < step)
1520 {
cristyed231572011-07-14 02:18:59 +00001521 p+=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001522 GetPixelInfoPixel(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001523 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001524 continue;
1525 }
cristy803640d2011-11-17 02:11:32 +00001526 GetPixelInfoPixel(image,p,&source);
cristya19f1d72012-08-07 18:24:38 +00001527 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1528 &source,(double) GetPixelAlpha(image,p),area,&destination);
cristy803640d2011-11-17 02:11:32 +00001529 SetPixelInfoPixel(image,&destination,q);
1530 GetPixelInfoPixel(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001531 p+=GetPixelChannels(image);
1532 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001533 }
cristya19f1d72012-08-07 18:24:38 +00001534 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1535 &background,(double) background.alpha,area,&destination);
cristy803640d2011-11-17 02:11:32 +00001536 SetPixelInfoPixel(image,&destination,q);
cristyed231572011-07-14 02:18:59 +00001537 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001538 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001539 {
cristy803640d2011-11-17 02:11:32 +00001540 SetPixelInfoPixel(image,&background,q);
cristyed231572011-07-14 02:18:59 +00001541 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001542 }
cristy3ed852e2009-09-05 21:47:34 +00001543 break;
1544 }
1545 case RIGHT:
1546 {
1547 /*
1548 Transfer pixels right-to-left.
1549 */
cristyed231572011-07-14 02:18:59 +00001550 p+=width*GetPixelChannels(image);
1551 q=p+step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001552 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001553 {
cristyed231572011-07-14 02:18:59 +00001554 p-=GetPixelChannels(image);
1555 q-=GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001556 if ((size_t) (x_offset+width+step-i) >= image->columns)
cristy3ed852e2009-09-05 21:47:34 +00001557 continue;
cristy803640d2011-11-17 02:11:32 +00001558 GetPixelInfoPixel(image,p,&source);
cristya19f1d72012-08-07 18:24:38 +00001559 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1560 &source,(double) GetPixelAlpha(image,p),area,&destination);
cristy803640d2011-11-17 02:11:32 +00001561 SetPixelInfoPixel(image,&destination,q);
1562 GetPixelInfoPixel(image,p,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001563 }
cristya19f1d72012-08-07 18:24:38 +00001564 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1565 &background,(double) background.alpha,area,&destination);
cristyed231572011-07-14 02:18:59 +00001566 q-=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001567 SetPixelInfoPixel(image,&destination,q);
cristy3ed852e2009-09-05 21:47:34 +00001568 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001569 {
cristyed231572011-07-14 02:18:59 +00001570 q-=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001571 SetPixelInfoPixel(image,&background,q);
cristy4c08aed2011-07-01 19:47:50 +00001572 }
cristy3ed852e2009-09-05 21:47:34 +00001573 break;
1574 }
1575 }
1576 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1577 status=MagickFalse;
1578 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1579 {
1580 MagickBooleanType
1581 proceed;
1582
cristyb5d5f722009-11-04 03:03:49 +00001583#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy7b650b52011-10-09 01:13:39 +00001584 #pragma omp critical (MagickCore_XShearImage)
cristy3ed852e2009-09-05 21:47:34 +00001585#endif
1586 proceed=SetImageProgress(image,XShearImageTag,progress++,height);
1587 if (proceed == MagickFalse)
1588 status=MagickFalse;
1589 }
1590 }
1591 image_view=DestroyCacheView(image_view);
1592 return(status);
1593}
1594
1595/*
1596%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1597% %
1598% %
1599% %
1600+ Y S h e a r I m a g e %
1601% %
1602% %
1603% %
1604%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1605%
1606% YShearImage shears the image in the Y direction with a shear angle of
1607% 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1608% negative angles shear clockwise. Angles are measured relative to a
1609% horizontal X-axis. Y shears will increase the height of an image creating
1610% 'empty' triangles on the top and bottom of the source image.
1611%
1612% The format of the YShearImage method is:
1613%
cristya19f1d72012-08-07 18:24:38 +00001614% MagickBooleanType YShearImage(Image *image,const double degrees,
cristybb503372010-05-27 20:51:26 +00001615% const size_t width,const size_t height,
1616% const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001617%
1618% A description of each parameter follows.
1619%
1620% o image: the image.
1621%
cristya19f1d72012-08-07 18:24:38 +00001622% o degrees: A double representing the shearing angle along the Y
cristy3ed852e2009-09-05 21:47:34 +00001623% axis.
1624%
1625% o width, height, x_offset, y_offset: Defines a region of the image
1626% to shear.
1627%
cristyecc2c142010-01-17 22:25:46 +00001628% o exception: return any errors or warnings in this structure.
1629%
cristy3ed852e2009-09-05 21:47:34 +00001630*/
cristya19f1d72012-08-07 18:24:38 +00001631static MagickBooleanType YShearImage(Image *image,const double degrees,
cristybb503372010-05-27 20:51:26 +00001632 const size_t width,const size_t height,const ssize_t x_offset,
1633 const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001634{
1635#define YShearImageTag "YShear/Image"
1636
1637 typedef enum
1638 {
1639 UP,
1640 DOWN
1641 } ShearDirection;
1642
1643 CacheView
1644 *image_view;
1645
cristy3ed852e2009-09-05 21:47:34 +00001646 MagickBooleanType
1647 status;
1648
cristy5f959472010-05-27 22:19:46 +00001649 MagickOffsetType
1650 progress;
1651
cristy4c08aed2011-07-01 19:47:50 +00001652 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001653 background;
1654
cristy5f959472010-05-27 22:19:46 +00001655 ssize_t
1656 x;
1657
cristy9d8c8ce2011-10-25 16:13:52 +00001658 /*
1659 Y Shear image.
1660 */
cristy3ed852e2009-09-05 21:47:34 +00001661 assert(image != (Image *) NULL);
1662 assert(image->signature == MagickSignature);
1663 if (image->debug != MagickFalse)
1664 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3ed852e2009-09-05 21:47:34 +00001665 status=MagickTrue;
1666 progress=0;
cristy9d8c8ce2011-10-25 16:13:52 +00001667 background=image->background_color;
cristy46ff2672012-12-14 15:32:26 +00001668 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00001669#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00001670 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00001671 magick_threads(image,image,width,1)
cristy3ed852e2009-09-05 21:47:34 +00001672#endif
cristybb503372010-05-27 20:51:26 +00001673 for (x=0; x < (ssize_t) width; x++)
cristy3ed852e2009-09-05 21:47:34 +00001674 {
cristybb503372010-05-27 20:51:26 +00001675 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001676 step;
1677
cristya19f1d72012-08-07 18:24:38 +00001678 double
cristy3ed852e2009-09-05 21:47:34 +00001679 area,
1680 displacement;
1681
cristy4c08aed2011-07-01 19:47:50 +00001682 PixelInfo
1683 pixel,
1684 source,
1685 destination;
1686
1687 register Quantum
1688 *restrict p,
1689 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001690
cristybb503372010-05-27 20:51:26 +00001691 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001692 i;
1693
cristy3ed852e2009-09-05 21:47:34 +00001694 ShearDirection
1695 direction;
1696
1697 if (status == MagickFalse)
1698 continue;
1699 p=GetCacheViewAuthenticPixels(image_view,x_offset+x,0,1,image->rows,
1700 exception);
cristy4c08aed2011-07-01 19:47:50 +00001701 if (p == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001702 {
1703 status=MagickFalse;
1704 continue;
1705 }
cristyed231572011-07-14 02:18:59 +00001706 p+=y_offset*GetPixelChannels(image);
cristya19f1d72012-08-07 18:24:38 +00001707 displacement=degrees*(double) (x-width/2.0);
cristy3ed852e2009-09-05 21:47:34 +00001708 if (displacement == 0.0)
1709 continue;
1710 if (displacement > 0.0)
1711 direction=DOWN;
1712 else
1713 {
1714 displacement*=(-1.0);
1715 direction=UP;
1716 }
cristybb503372010-05-27 20:51:26 +00001717 step=(ssize_t) floor((double) displacement);
cristya19f1d72012-08-07 18:24:38 +00001718 area=(double) (displacement-step);
cristy3ed852e2009-09-05 21:47:34 +00001719 step++;
1720 pixel=background;
cristy4c08aed2011-07-01 19:47:50 +00001721 GetPixelInfo(image,&source);
1722 GetPixelInfo(image,&destination);
cristy3ed852e2009-09-05 21:47:34 +00001723 switch (direction)
1724 {
1725 case UP:
1726 {
1727 /*
1728 Transfer pixels top-to-bottom.
1729 */
1730 if (step > y_offset)
1731 break;
cristyed231572011-07-14 02:18:59 +00001732 q=p-step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001733 for (i=0; i < (ssize_t) height; i++)
cristy3ed852e2009-09-05 21:47:34 +00001734 {
1735 if ((y_offset+i) < step)
1736 {
cristyed231572011-07-14 02:18:59 +00001737 p+=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001738 GetPixelInfoPixel(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001739 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001740 continue;
1741 }
cristy803640d2011-11-17 02:11:32 +00001742 GetPixelInfoPixel(image,p,&source);
cristya19f1d72012-08-07 18:24:38 +00001743 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1744 &source,(double) GetPixelAlpha(image,p),area,
cristy4c08aed2011-07-01 19:47:50 +00001745 &destination);
cristy803640d2011-11-17 02:11:32 +00001746 SetPixelInfoPixel(image,&destination,q);
1747 GetPixelInfoPixel(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001748 p+=GetPixelChannels(image);
1749 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001750 }
cristya19f1d72012-08-07 18:24:38 +00001751 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1752 &background,(double) background.alpha,area,&destination);
cristy803640d2011-11-17 02:11:32 +00001753 SetPixelInfoPixel(image,&destination,q);
cristyed231572011-07-14 02:18:59 +00001754 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001755 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001756 {
cristy803640d2011-11-17 02:11:32 +00001757 SetPixelInfoPixel(image,&background,q);
cristyed231572011-07-14 02:18:59 +00001758 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001759 }
cristy3ed852e2009-09-05 21:47:34 +00001760 break;
1761 }
1762 case DOWN:
1763 {
1764 /*
1765 Transfer pixels bottom-to-top.
1766 */
cristyed231572011-07-14 02:18:59 +00001767 p+=height*GetPixelChannels(image);
1768 q=p+step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001769 for (i=0; i < (ssize_t) height; i++)
cristy3ed852e2009-09-05 21:47:34 +00001770 {
cristyed231572011-07-14 02:18:59 +00001771 p-=GetPixelChannels(image);
1772 q-=GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001773 if ((size_t) (y_offset+height+step-i) >= image->rows)
cristy3ed852e2009-09-05 21:47:34 +00001774 continue;
cristy803640d2011-11-17 02:11:32 +00001775 GetPixelInfoPixel(image,p,&source);
cristya19f1d72012-08-07 18:24:38 +00001776 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1777 &source,(double) GetPixelAlpha(image,p),area,
cristy4c08aed2011-07-01 19:47:50 +00001778 &destination);
cristy803640d2011-11-17 02:11:32 +00001779 SetPixelInfoPixel(image,&destination,q);
1780 GetPixelInfoPixel(image,p,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001781 }
cristya19f1d72012-08-07 18:24:38 +00001782 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1783 &background,(double) background.alpha,area,&destination);
cristyed231572011-07-14 02:18:59 +00001784 q-=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001785 SetPixelInfoPixel(image,&destination,q);
cristy3ed852e2009-09-05 21:47:34 +00001786 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001787 {
cristyed231572011-07-14 02:18:59 +00001788 q-=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001789 SetPixelInfoPixel(image,&background,q);
cristy4c08aed2011-07-01 19:47:50 +00001790 }
cristy3ed852e2009-09-05 21:47:34 +00001791 break;
1792 }
1793 }
1794 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1795 status=MagickFalse;
1796 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1797 {
1798 MagickBooleanType
1799 proceed;
1800
cristyb5d5f722009-11-04 03:03:49 +00001801#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy7b650b52011-10-09 01:13:39 +00001802 #pragma omp critical (MagickCore_YShearImage)
cristy3ed852e2009-09-05 21:47:34 +00001803#endif
1804 proceed=SetImageProgress(image,YShearImageTag,progress++,image->rows);
1805 if (proceed == MagickFalse)
1806 status=MagickFalse;
1807 }
1808 }
1809 image_view=DestroyCacheView(image_view);
1810 return(status);
1811}
1812
1813/*
1814%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1815% %
1816% %
1817% %
cristy3ed852e2009-09-05 21:47:34 +00001818% S h e a r I m a g e %
1819% %
1820% %
1821% %
1822%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1823%
1824% ShearImage() creates a new image that is a shear_image copy of an existing
cristycee97112010-05-28 00:44:52 +00001825% one. Shearing slides one edge of an image along the X or Y axis, creating
1826% a parallelogram. An X direction shear slides an edge along the X axis,
1827% while a Y direction shear slides an edge along the Y axis. The amount of
cristy3ed852e2009-09-05 21:47:34 +00001828% the shear is controlled by a shear angle. For X direction shears, x_shear
1829% is measured relative to the Y axis, and similarly, for Y direction shears
1830% y_shear is measured relative to the X axis. Empty triangles left over from
1831% shearing the image are filled with the background color defined by member
1832% 'background_color' of the image.. ShearImage() allocates the memory
1833% necessary for the new Image structure and returns a pointer to the new image.
1834%
1835% ShearImage() is based on the paper "A Fast Algorithm for General Raster
1836% Rotatation" by Alan W. Paeth.
1837%
1838% The format of the ShearImage method is:
1839%
1840% Image *ShearImage(const Image *image,const double x_shear,
1841% const double y_shear,ExceptionInfo *exception)
1842%
1843% A description of each parameter follows.
1844%
1845% o image: the image.
1846%
1847% o x_shear, y_shear: Specifies the number of degrees to shear the image.
1848%
1849% o exception: return any errors or warnings in this structure.
1850%
1851*/
1852MagickExport Image *ShearImage(const Image *image,const double x_shear,
1853 const double y_shear,ExceptionInfo *exception)
1854{
1855 Image
1856 *integral_image,
1857 *shear_image;
1858
cristybb503372010-05-27 20:51:26 +00001859 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001860 x_offset,
1861 y_offset;
1862
cristyecc2c142010-01-17 22:25:46 +00001863 MagickBooleanType
1864 status;
1865
cristy3ed852e2009-09-05 21:47:34 +00001866 PointInfo
1867 shear;
1868
1869 RectangleInfo
1870 border_info;
1871
cristybb503372010-05-27 20:51:26 +00001872 size_t
cristy3ed852e2009-09-05 21:47:34 +00001873 y_width;
1874
1875 assert(image != (Image *) NULL);
1876 assert(image->signature == MagickSignature);
1877 if (image->debug != MagickFalse)
1878 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1879 assert(exception != (ExceptionInfo *) NULL);
1880 assert(exception->signature == MagickSignature);
1881 if ((x_shear != 0.0) && (fmod(x_shear,90.0) == 0.0))
1882 ThrowImageException(ImageError,"AngleIsDiscontinuous");
1883 if ((y_shear != 0.0) && (fmod(y_shear,90.0) == 0.0))
1884 ThrowImageException(ImageError,"AngleIsDiscontinuous");
1885 /*
1886 Initialize shear angle.
1887 */
1888 integral_image=CloneImage(image,0,0,MagickTrue,exception);
1889 if (integral_image == (Image *) NULL)
1890 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1891 shear.x=(-tan(DegreesToRadians(fmod(x_shear,360.0))));
1892 shear.y=tan(DegreesToRadians(fmod(y_shear,360.0)));
1893 if ((shear.x == 0.0) && (shear.y == 0.0))
1894 return(integral_image);
cristy574cc262011-08-05 01:23:58 +00001895 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001896 {
cristy3ed852e2009-09-05 21:47:34 +00001897 integral_image=DestroyImage(integral_image);
1898 return(integral_image);
1899 }
cristy8a46d822012-08-28 23:32:39 +00001900 if (integral_image->alpha_trait != BlendPixelTrait)
cristy63240882011-08-05 19:05:27 +00001901 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00001902 /*
1903 Compute image size.
1904 */
cristybb503372010-05-27 20:51:26 +00001905 y_width=image->columns+(ssize_t) floor(fabs(shear.x)*image->rows+0.5);
cristycee97112010-05-28 00:44:52 +00001906 x_offset=(ssize_t) ceil((double) image->columns+((fabs(shear.x)*image->rows)-
cristy06609ee2010-03-17 20:21:27 +00001907 image->columns)/2.0-0.5);
cristycee97112010-05-28 00:44:52 +00001908 y_offset=(ssize_t) ceil((double) image->rows+((fabs(shear.y)*y_width)-
1909 image->rows)/2.0-0.5);
cristy3ed852e2009-09-05 21:47:34 +00001910 /*
1911 Surround image with border.
1912 */
1913 integral_image->border_color=integral_image->background_color;
1914 integral_image->compose=CopyCompositeOp;
cristybb503372010-05-27 20:51:26 +00001915 border_info.width=(size_t) x_offset;
1916 border_info.height=(size_t) y_offset;
cristy633f0c62011-09-15 13:27:36 +00001917 shear_image=BorderImage(integral_image,&border_info,image->compose,exception);
cristyecc2c142010-01-17 22:25:46 +00001918 integral_image=DestroyImage(integral_image);
cristy3ed852e2009-09-05 21:47:34 +00001919 if (shear_image == (Image *) NULL)
1920 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +00001921 /*
1922 Shear the image.
1923 */
cristy8a46d822012-08-28 23:32:39 +00001924 if (shear_image->alpha_trait != BlendPixelTrait)
cristy63240882011-08-05 19:05:27 +00001925 (void) SetImageAlphaChannel(shear_image,OpaqueAlphaChannel,exception);
cristyecc2c142010-01-17 22:25:46 +00001926 status=XShearImage(shear_image,shear.x,image->columns,image->rows,x_offset,
cristyeaedf062010-05-29 22:36:02 +00001927 (ssize_t) (shear_image->rows-image->rows)/2,exception);
cristyecc2c142010-01-17 22:25:46 +00001928 if (status == MagickFalse)
1929 {
1930 shear_image=DestroyImage(shear_image);
1931 return((Image *) NULL);
1932 }
cristyeaedf062010-05-29 22:36:02 +00001933 status=YShearImage(shear_image,shear.y,y_width,image->rows,(ssize_t)
1934 (shear_image->columns-y_width)/2,y_offset,exception);
cristyecc2c142010-01-17 22:25:46 +00001935 if (status == MagickFalse)
1936 {
1937 shear_image=DestroyImage(shear_image);
1938 return((Image *) NULL);
1939 }
cristya19f1d72012-08-07 18:24:38 +00001940 status=CropToFitImage(&shear_image,shear.x,shear.y,(double)
1941 image->columns,(double) image->rows,MagickFalse,exception);
cristy3ed852e2009-09-05 21:47:34 +00001942 shear_image->compose=image->compose;
1943 shear_image->page.width=0;
1944 shear_image->page.height=0;
cristy1c2f48d2012-12-14 01:20:55 +00001945 if (status == MagickFalse)
1946 shear_image=DestroyImage(shear_image);
cristy3ed852e2009-09-05 21:47:34 +00001947 return(shear_image);
1948}
cristyc7c81142011-11-07 12:14:23 +00001949
1950/*
1951%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1952% %
1953% %
1954% %
1955% S h e a r R o t a t e I m a g e %
1956% %
1957% %
1958% %
1959%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1960%
1961% ShearRotateImage() creates a new image that is a rotated copy of an existing
1962% one. Positive angles rotate counter-clockwise (right-hand rule), while
1963% negative angles rotate clockwise. Rotated images are usually larger than
1964% the originals and have 'empty' triangular corners. X axis. Empty
1965% triangles left over from shearing the image are filled with the background
1966% color defined by member 'background_color' of the image. ShearRotateImage
1967% allocates the memory necessary for the new Image structure and returns a
1968% pointer to the new image.
1969%
1970% ShearRotateImage() is based on the paper "A Fast Algorithm for General
1971% Raster Rotatation" by Alan W. Paeth. ShearRotateImage is adapted from a
1972% similar method based on the Paeth paper written by Michael Halle of the
1973% Spatial Imaging Group, MIT Media Lab.
1974%
1975% The format of the ShearRotateImage method is:
1976%
1977% Image *ShearRotateImage(const Image *image,const double degrees,
1978% ExceptionInfo *exception)
1979%
1980% A description of each parameter follows.
1981%
1982% o image: the image.
1983%
1984% o degrees: Specifies the number of degrees to rotate the image.
1985%
1986% o exception: return any errors or warnings in this structure.
1987%
1988*/
1989MagickExport Image *ShearRotateImage(const Image *image,const double degrees,
1990 ExceptionInfo *exception)
1991{
1992 Image
1993 *integral_image,
1994 *rotate_image;
1995
1996 MagickBooleanType
1997 status;
1998
cristya19f1d72012-08-07 18:24:38 +00001999 double
cristyc7c81142011-11-07 12:14:23 +00002000 angle;
2001
2002 PointInfo
2003 shear;
2004
2005 RectangleInfo
2006 border_info;
2007
2008 size_t
2009 height,
2010 rotations,
2011 width,
2012 y_width;
2013
2014 ssize_t
2015 x_offset,
2016 y_offset;
2017
2018 /*
2019 Adjust rotation angle.
2020 */
2021 assert(image != (Image *) NULL);
2022 assert(image->signature == MagickSignature);
2023 if (image->debug != MagickFalse)
2024 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2025 assert(exception != (ExceptionInfo *) NULL);
2026 assert(exception->signature == MagickSignature);
2027 angle=degrees;
2028 while (angle < -45.0)
2029 angle+=360.0;
2030 for (rotations=0; angle > 45.0; rotations++)
2031 angle-=90.0;
2032 rotations%=4;
2033 /*
2034 Calculate shear equations.
2035 */
2036 integral_image=IntegralRotateImage(image,rotations,exception);
2037 if (integral_image == (Image *) NULL)
2038 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2039 shear.x=(-tan((double) DegreesToRadians(angle)/2.0));
2040 shear.y=sin((double) DegreesToRadians(angle));
2041 if ((shear.x == 0.0) && (shear.y == 0.0))
2042 return(integral_image);
2043 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
2044 {
2045 integral_image=DestroyImage(integral_image);
2046 return(integral_image);
2047 }
cristy8a46d822012-08-28 23:32:39 +00002048 if (integral_image->alpha_trait != BlendPixelTrait)
cristyc7c81142011-11-07 12:14:23 +00002049 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
2050 /*
2051 Compute image size.
2052 */
2053 width=image->columns;
2054 height=image->rows;
2055 if ((rotations == 1) || (rotations == 3))
2056 {
2057 width=image->rows;
2058 height=image->columns;
2059 }
2060 y_width=width+(ssize_t) floor(fabs(shear.x)*height+0.5);
2061 x_offset=(ssize_t) ceil((double) width+((fabs(shear.y)*height)-width)/2.0-
2062 0.5);
2063 y_offset=(ssize_t) ceil((double) height+((fabs(shear.y)*y_width)-height)/2.0-
2064 0.5);
2065 /*
2066 Surround image with a border.
2067 */
2068 integral_image->border_color=integral_image->background_color;
2069 integral_image->compose=CopyCompositeOp;
2070 border_info.width=(size_t) x_offset;
2071 border_info.height=(size_t) y_offset;
2072 rotate_image=BorderImage(integral_image,&border_info,image->compose,
2073 exception);
2074 integral_image=DestroyImage(integral_image);
2075 if (rotate_image == (Image *) NULL)
2076 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2077 /*
2078 Rotate the image.
2079 */
2080 status=XShearImage(rotate_image,shear.x,width,height,x_offset,(ssize_t)
2081 (rotate_image->rows-height)/2,exception);
2082 if (status == MagickFalse)
2083 {
2084 rotate_image=DestroyImage(rotate_image);
2085 return((Image *) NULL);
2086 }
2087 status=YShearImage(rotate_image,shear.y,y_width,height,(ssize_t)
2088 (rotate_image->columns-y_width)/2,y_offset,exception);
2089 if (status == MagickFalse)
2090 {
2091 rotate_image=DestroyImage(rotate_image);
2092 return((Image *) NULL);
2093 }
2094 status=XShearImage(rotate_image,shear.x,y_width,rotate_image->rows,(ssize_t)
2095 (rotate_image->columns-y_width)/2,0,exception);
2096 if (status == MagickFalse)
2097 {
2098 rotate_image=DestroyImage(rotate_image);
2099 return((Image *) NULL);
2100 }
cristya19f1d72012-08-07 18:24:38 +00002101 status=CropToFitImage(&rotate_image,shear.x,shear.y,(double) width,
2102 (double) height,MagickTrue,exception);
cristyc7c81142011-11-07 12:14:23 +00002103 rotate_image->compose=image->compose;
2104 rotate_image->page.width=0;
2105 rotate_image->page.height=0;
cristy1c2f48d2012-12-14 01:20:55 +00002106 if (status == MagickFalse)
2107 rotate_image=DestroyImage(rotate_image);
cristyc7c81142011-11-07 12:14:23 +00002108 return(rotate_image);
2109}