blob: 271f1f9170697bd8967ea9fd924d3ed0192dc33d [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%
195% The format of the DeskewImage method is:
196%
197% Image *DeskewImage(const Image *image,const double threshold,
198% ExceptionInfo *exception)
199%
200% A description of each parameter follows:
201%
202% o image: the image.
203%
204% o threshold: separate background from foreground.
205%
206% o exception: return any errors or warnings in this structure.
207%
208*/
209
210typedef struct _RadonInfo
211{
212 CacheType
213 type;
214
cristybb503372010-05-27 20:51:26 +0000215 size_t
cristy3ed852e2009-09-05 21:47:34 +0000216 width,
217 height;
218
219 MagickSizeType
220 length;
221
222 MagickBooleanType
223 mapped;
224
225 char
226 path[MaxTextExtent];
227
228 int
229 file;
230
231 unsigned short
232 *cells;
233} RadonInfo;
234
235static RadonInfo *DestroyRadonInfo(RadonInfo *radon_info)
236{
237 assert(radon_info != (RadonInfo *) NULL);
238 switch (radon_info->type)
239 {
240 case MemoryCache:
241 {
242 if (radon_info->mapped == MagickFalse)
243 radon_info->cells=(unsigned short *) RelinquishMagickMemory(
244 radon_info->cells);
245 else
246 radon_info->cells=(unsigned short *) UnmapBlob(radon_info->cells,
247 (size_t) radon_info->length);
248 RelinquishMagickResource(MemoryResource,radon_info->length);
249 break;
250 }
251 case MapCache:
252 {
253 radon_info->cells=(unsigned short *) UnmapBlob(radon_info->cells,(size_t)
254 radon_info->length);
255 RelinquishMagickResource(MapResource,radon_info->length);
256 }
257 case DiskCache:
258 {
259 if (radon_info->file != -1)
260 (void) close(radon_info->file);
261 (void) RelinquishUniqueFileResource(radon_info->path);
262 RelinquishMagickResource(DiskResource,radon_info->length);
263 break;
264 }
265 default:
266 break;
267 }
268 return((RadonInfo *) RelinquishMagickMemory(radon_info));
269}
270
271static MagickBooleanType ResetRadonCells(RadonInfo *radon_info)
272{
cristybb503372010-05-27 20:51:26 +0000273 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000274 x;
275
276 ssize_t
cristyd40deb62011-03-09 00:52:27 +0000277 count,
278 y;
cristy3ed852e2009-09-05 21:47:34 +0000279
280 unsigned short
281 value;
282
283 if (radon_info->type != DiskCache)
284 {
285 (void) ResetMagickMemory(radon_info->cells,0,(size_t) radon_info->length);
286 return(MagickTrue);
287 }
288 value=0;
cristy7f317702011-02-18 20:40:28 +0000289 (void) lseek(radon_info->file,0,SEEK_SET);
cristybb503372010-05-27 20:51:26 +0000290 for (y=0; y < (ssize_t) radon_info->height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000291 {
cristybb503372010-05-27 20:51:26 +0000292 for (x=0; x < (ssize_t) radon_info->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000293 {
294 count=write(radon_info->file,&value,sizeof(*radon_info->cells));
295 if (count != (ssize_t) sizeof(*radon_info->cells))
296 break;
297 }
cristybb503372010-05-27 20:51:26 +0000298 if (x < (ssize_t) radon_info->width)
cristy3ed852e2009-09-05 21:47:34 +0000299 break;
300 }
cristybb503372010-05-27 20:51:26 +0000301 return(y < (ssize_t) radon_info->height ? MagickFalse : MagickTrue);
cristy3ed852e2009-09-05 21:47:34 +0000302}
303
cristybb503372010-05-27 20:51:26 +0000304static RadonInfo *AcquireRadonInfo(const Image *image,const size_t width,
305 const size_t height,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000306{
307 MagickBooleanType
308 status;
309
310 RadonInfo
311 *radon_info;
312
cristy73bd4a52010-10-05 11:24:23 +0000313 radon_info=(RadonInfo *) AcquireMagickMemory(sizeof(*radon_info));
cristy3ed852e2009-09-05 21:47:34 +0000314 if (radon_info == (RadonInfo *) NULL)
315 return((RadonInfo *) NULL);
316 (void) ResetMagickMemory(radon_info,0,sizeof(*radon_info));
317 radon_info->width=width;
318 radon_info->height=height;
319 radon_info->length=(MagickSizeType) width*height*sizeof(*radon_info->cells);
320 radon_info->type=MemoryCache;
321 status=AcquireMagickResource(AreaResource,radon_info->length);
322 if ((status != MagickFalse) &&
323 (radon_info->length == (MagickSizeType) ((size_t) radon_info->length)))
324 {
325 status=AcquireMagickResource(MemoryResource,radon_info->length);
326 if (status != MagickFalse)
327 {
328 radon_info->mapped=MagickFalse;
329 radon_info->cells=(unsigned short *) AcquireMagickMemory((size_t)
330 radon_info->length);
331 if (radon_info->cells == (unsigned short *) NULL)
332 {
333 radon_info->mapped=MagickTrue;
334 radon_info->cells=(unsigned short *) MapBlob(-1,IOMode,0,(size_t)
335 radon_info->length);
336 }
337 if (radon_info->cells == (unsigned short *) NULL)
338 RelinquishMagickResource(MemoryResource,radon_info->length);
339 }
340 }
341 radon_info->file=(-1);
342 if (radon_info->cells == (unsigned short *) NULL)
343 {
344 status=AcquireMagickResource(DiskResource,radon_info->length);
345 if (status == MagickFalse)
346 {
347 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
cristyefe601c2013-01-05 17:51:12 +0000348 "CacheResourcesExhausted","`%s'",image->filename);
cristy3ed852e2009-09-05 21:47:34 +0000349 return(DestroyRadonInfo(radon_info));
350 }
351 radon_info->type=DiskCache;
352 (void) AcquireMagickResource(MemoryResource,radon_info->length);
353 radon_info->file=AcquireUniqueFileResource(radon_info->path);
354 if (radon_info->file == -1)
355 return(DestroyRadonInfo(radon_info));
356 status=AcquireMagickResource(MapResource,radon_info->length);
357 if (status != MagickFalse)
358 {
359 status=ResetRadonCells(radon_info);
360 if (status != MagickFalse)
361 {
362 radon_info->cells=(unsigned short *) MapBlob(radon_info->file,
363 IOMode,0,(size_t) radon_info->length);
364 if (radon_info->cells != (unsigned short *) NULL)
365 radon_info->type=MapCache;
366 else
367 RelinquishMagickResource(MapResource,radon_info->length);
368 }
369 }
370 }
371 return(radon_info);
372}
373
374static inline size_t MagickMin(const size_t x,const size_t y)
375{
376 if (x < y)
377 return(x);
378 return(y);
379}
380
381static inline ssize_t ReadRadonCell(const RadonInfo *radon_info,
cristy2c38b272011-02-18 23:25:33 +0000382 const MagickOffsetType offset,const size_t length,unsigned char *buffer)
cristy3ed852e2009-09-05 21:47:34 +0000383{
384 register ssize_t
385 i;
386
387 ssize_t
388 count;
389
390#if !defined(MAGICKCORE_HAVE_PPREAD)
cristyb5d5f722009-11-04 03:03:49 +0000391#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000392 #pragma omp critical (MagickCore_ReadRadonCell)
393#endif
394 {
395 i=(-1);
cristy7f317702011-02-18 20:40:28 +0000396 if (lseek(radon_info->file,offset,SEEK_SET) >= 0)
cristy3ed852e2009-09-05 21:47:34 +0000397 {
398#endif
399 count=0;
400 for (i=0; i < (ssize_t) length; i+=count)
401 {
402#if !defined(MAGICKCORE_HAVE_PPREAD)
403 count=read(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
404 SSIZE_MAX));
405#else
406 count=pread(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
cristy2c38b272011-02-18 23:25:33 +0000407 SSIZE_MAX),offset+i);
cristy3ed852e2009-09-05 21:47:34 +0000408#endif
409 if (count > 0)
410 continue;
411 count=0;
412 if (errno != EINTR)
413 {
414 i=(-1);
415 break;
416 }
417 }
418#if !defined(MAGICKCORE_HAVE_PPREAD)
419 }
420 }
421#endif
422 return(i);
423}
424
425static inline ssize_t WriteRadonCell(const RadonInfo *radon_info,
cristy2c38b272011-02-18 23:25:33 +0000426 const MagickOffsetType offset,const size_t length,const unsigned char *buffer)
cristy3ed852e2009-09-05 21:47:34 +0000427{
428 register ssize_t
429 i;
430
431 ssize_t
432 count;
433
434#if !defined(MAGICKCORE_HAVE_PWRITE)
cristyb5d5f722009-11-04 03:03:49 +0000435#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000436 #pragma omp critical (MagickCore_WriteRadonCell)
437#endif
438 {
cristy7f317702011-02-18 20:40:28 +0000439 if (lseek(radon_info->file,offset,SEEK_SET) >= 0)
cristy3ed852e2009-09-05 21:47:34 +0000440 {
441#endif
442 count=0;
443 for (i=0; i < (ssize_t) length; i+=count)
444 {
445#if !defined(MAGICKCORE_HAVE_PWRITE)
446 count=write(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
447 SSIZE_MAX));
448#else
449 count=pwrite(radon_info->file,buffer+i,MagickMin(length-i,(size_t)
cristy2c38b272011-02-18 23:25:33 +0000450 SSIZE_MAX),offset+i);
cristy3ed852e2009-09-05 21:47:34 +0000451#endif
452 if (count > 0)
453 continue;
454 count=0;
455 if (errno != EINTR)
456 {
457 i=(-1);
458 break;
459 }
460 }
461#if !defined(MAGICKCORE_HAVE_PWRITE)
462 }
463 }
464#endif
465 return(i);
466}
467
468static inline unsigned short GetRadonCell(const RadonInfo *radon_info,
cristybb503372010-05-27 20:51:26 +0000469 const ssize_t x,const ssize_t y)
cristy3ed852e2009-09-05 21:47:34 +0000470{
cristy2c38b272011-02-18 23:25:33 +0000471 MagickOffsetType
cristy3ed852e2009-09-05 21:47:34 +0000472 i;
473
474 unsigned short
475 value;
476
cristy2c38b272011-02-18 23:25:33 +0000477 i=(MagickOffsetType) radon_info->height*x+y;
cristy3ed852e2009-09-05 21:47:34 +0000478 if ((i < 0) ||
479 ((MagickSizeType) (i*sizeof(*radon_info->cells)) >= radon_info->length))
480 return(0);
481 if (radon_info->type != DiskCache)
482 return(radon_info->cells[i]);
483 value=0;
484 (void) ReadRadonCell(radon_info,i*sizeof(*radon_info->cells),
485 sizeof(*radon_info->cells),(unsigned char *) &value);
486 return(value);
487}
488
489static inline MagickBooleanType SetRadonCell(const RadonInfo *radon_info,
cristybb503372010-05-27 20:51:26 +0000490 const ssize_t x,const ssize_t y,const unsigned short value)
cristy3ed852e2009-09-05 21:47:34 +0000491{
cristy2c38b272011-02-18 23:25:33 +0000492 MagickOffsetType
cristy3ed852e2009-09-05 21:47:34 +0000493 i;
494
495 ssize_t
496 count;
497
cristy2c38b272011-02-18 23:25:33 +0000498 i=(MagickOffsetType) radon_info->height*x+y;
cristy3ed852e2009-09-05 21:47:34 +0000499 if ((i < 0) ||
500 ((MagickSizeType) (i*sizeof(*radon_info->cells)) >= radon_info->length))
501 return(MagickFalse);
502 if (radon_info->type != DiskCache)
503 {
504 radon_info->cells[i]=value;
505 return(MagickTrue);
506 }
507 count=WriteRadonCell(radon_info,i*sizeof(*radon_info->cells),
cristy44807bb2009-09-19 18:28:06 +0000508 sizeof(*radon_info->cells),(const unsigned char *) &value);
cristy3ed852e2009-09-05 21:47:34 +0000509 if (count != (ssize_t) sizeof(*radon_info->cells))
510 return(MagickFalse);
511 return(MagickTrue);
512}
513
cristy4ee2b0c2012-05-15 00:30:35 +0000514static void RadonProjection(const Image *image,RadonInfo *source_cells,
cristybb503372010-05-27 20:51:26 +0000515 RadonInfo *destination_cells,const ssize_t sign,size_t *projection)
cristy3ed852e2009-09-05 21:47:34 +0000516{
517 RadonInfo
518 *swap;
519
cristybb503372010-05-27 20:51:26 +0000520 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000521 x;
522
523 register RadonInfo
524 *p,
525 *q;
526
cristybb503372010-05-27 20:51:26 +0000527 size_t
cristy3ed852e2009-09-05 21:47:34 +0000528 step;
529
530 p=source_cells;
531 q=destination_cells;
532 for (step=1; step < p->width; step*=2)
533 {
cristyeaedf062010-05-29 22:36:02 +0000534 for (x=0; x < (ssize_t) p->width; x+=2*(ssize_t) step)
cristy3ed852e2009-09-05 21:47:34 +0000535 {
cristybb503372010-05-27 20:51:26 +0000536 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000537 i;
538
cristyd40deb62011-03-09 00:52:27 +0000539 ssize_t
540 y;
541
cristy3ed852e2009-09-05 21:47:34 +0000542 unsigned short
543 cell;
544
cristybb503372010-05-27 20:51:26 +0000545 for (i=0; i < (ssize_t) step; i++)
cristy3ed852e2009-09-05 21:47:34 +0000546 {
cristybb503372010-05-27 20:51:26 +0000547 for (y=0; y < (ssize_t) (p->height-i-1); y++)
cristy3ed852e2009-09-05 21:47:34 +0000548 {
549 cell=GetRadonCell(p,x+i,y);
cristyeaedf062010-05-29 22:36:02 +0000550 (void) SetRadonCell(q,x+2*i,y,cell+GetRadonCell(p,x+i+(ssize_t)
551 step,y+i));
552 (void) SetRadonCell(q,x+2*i+1,y,cell+GetRadonCell(p,x+i+(ssize_t)
553 step,y+i+1));
cristy3ed852e2009-09-05 21:47:34 +0000554 }
cristybb503372010-05-27 20:51:26 +0000555 for ( ; y < (ssize_t) (p->height-i); y++)
cristy3ed852e2009-09-05 21:47:34 +0000556 {
557 cell=GetRadonCell(p,x+i,y);
cristyeaedf062010-05-29 22:36:02 +0000558 (void) SetRadonCell(q,x+2*i,y,cell+GetRadonCell(p,x+i+(ssize_t) step,
559 y+i));
cristy3ed852e2009-09-05 21:47:34 +0000560 (void) SetRadonCell(q,x+2*i+1,y,cell);
561 }
cristybb503372010-05-27 20:51:26 +0000562 for ( ; y < (ssize_t) p->height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000563 {
564 cell=GetRadonCell(p,x+i,y);
565 (void) SetRadonCell(q,x+2*i,y,cell);
566 (void) SetRadonCell(q,x+2*i+1,y,cell);
567 }
568 }
569 }
570 swap=p;
571 p=q;
572 q=swap;
573 }
cristyb5d5f722009-11-04 03:03:49 +0000574#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000575 #pragma omp parallel for schedule(static,4) \
cristy5e6b2592012-12-19 14:08:11 +0000576 magick_threads(image,image,p->width,1)
cristy3ed852e2009-09-05 21:47:34 +0000577#endif
cristybb503372010-05-27 20:51:26 +0000578 for (x=0; x < (ssize_t) p->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000579 {
cristybb503372010-05-27 20:51:26 +0000580 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000581 y;
582
cristybb503372010-05-27 20:51:26 +0000583 size_t
cristy3ed852e2009-09-05 21:47:34 +0000584 sum;
585
586 sum=0;
cristybb503372010-05-27 20:51:26 +0000587 for (y=0; y < (ssize_t) (p->height-1); y++)
cristy3ed852e2009-09-05 21:47:34 +0000588 {
cristybb503372010-05-27 20:51:26 +0000589 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000590 delta;
591
cristybb503372010-05-27 20:51:26 +0000592 delta=GetRadonCell(p,x,y)-(ssize_t) GetRadonCell(p,x,y+1);
cristy3ed852e2009-09-05 21:47:34 +0000593 sum+=delta*delta;
594 }
595 projection[p->width+sign*x-1]=sum;
596 }
597}
598
599static MagickBooleanType RadonTransform(const Image *image,
cristybb503372010-05-27 20:51:26 +0000600 const double threshold,size_t *projection,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000601{
602 CacheView
603 *image_view;
604
cristy3ed852e2009-09-05 21:47:34 +0000605 MagickBooleanType
606 status;
607
608 RadonInfo
609 *destination_cells,
610 *source_cells;
611
cristybb503372010-05-27 20:51:26 +0000612 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000613 i;
614
cristybb503372010-05-27 20:51:26 +0000615 size_t
cristy3ed852e2009-09-05 21:47:34 +0000616 count,
617 width;
618
cristyd40deb62011-03-09 00:52:27 +0000619 ssize_t
620 y;
621
622 unsigned char
623 byte;
624
cristy3ed852e2009-09-05 21:47:34 +0000625 unsigned short
626 bits[256];
627
628 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
629 source_cells=AcquireRadonInfo(image,width,image->rows,exception);
630 destination_cells=AcquireRadonInfo(image,width,image->rows,exception);
631 if ((source_cells == (RadonInfo *) NULL) ||
632 (destination_cells == (RadonInfo *) NULL))
633 {
634 if (destination_cells != (RadonInfo *) NULL)
635 destination_cells=DestroyRadonInfo(destination_cells);
636 if (source_cells != (RadonInfo *) NULL)
637 source_cells=DestroyRadonInfo(source_cells);
638 return(MagickFalse);
639 }
640 if (ResetRadonCells(source_cells) == MagickFalse)
641 {
642 destination_cells=DestroyRadonInfo(destination_cells);
643 source_cells=DestroyRadonInfo(source_cells);
644 return(MagickFalse);
645 }
646 for (i=0; i < 256; i++)
647 {
648 byte=(unsigned char) i;
649 for (count=0; byte != 0; byte>>=1)
650 count+=byte & 0x01;
651 bits[i]=(unsigned short) count;
652 }
653 status=MagickTrue;
cristy46ff2672012-12-14 15:32:26 +0000654 image_view=AcquireVirtualCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +0000655#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000656 #pragma omp parallel for schedule(static,4) shared(status) \
cristy5e6b2592012-12-19 14:08:11 +0000657 magick_threads(image,image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +0000658#endif
cristybb503372010-05-27 20:51:26 +0000659 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000660 {
cristy4c08aed2011-07-01 19:47:50 +0000661 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000662 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000663
cristybb503372010-05-27 20:51:26 +0000664 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000665 i,
666 x;
667
cristybb503372010-05-27 20:51:26 +0000668 size_t
cristy3ed852e2009-09-05 21:47:34 +0000669 bit,
670 byte;
671
672 if (status == MagickFalse)
673 continue;
674 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000675 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000676 {
677 status=MagickFalse;
678 continue;
679 }
680 bit=0;
681 byte=0;
cristybb503372010-05-27 20:51:26 +0000682 i=(ssize_t) (image->columns+7)/8;
683 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000684 {
685 byte<<=1;
cristyf13c5942012-08-08 23:50:11 +0000686 if (GetPixelIntensity(image,p) < threshold)
cristy3ed852e2009-09-05 21:47:34 +0000687 byte|=0x01;
688 bit++;
689 if (bit == 8)
690 {
691 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
692 bit=0;
693 byte=0;
694 }
cristyed231572011-07-14 02:18:59 +0000695 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000696 }
697 if (bit != 0)
698 {
699 byte<<=(8-bit);
700 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
701 }
702 }
cristy4ee2b0c2012-05-15 00:30:35 +0000703 RadonProjection(image,source_cells,destination_cells,-1,projection);
cristy3ed852e2009-09-05 21:47:34 +0000704 (void) ResetRadonCells(source_cells);
cristyb5d5f722009-11-04 03:03:49 +0000705#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000706 #pragma omp parallel for schedule(static,4) shared(status) \
cristy5e6b2592012-12-19 14:08:11 +0000707 magick_threads(image,image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +0000708#endif
cristybb503372010-05-27 20:51:26 +0000709 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000710 {
cristy4c08aed2011-07-01 19:47:50 +0000711 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000712 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000713
cristybb503372010-05-27 20:51:26 +0000714 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000715 i,
716 x;
717
cristybb503372010-05-27 20:51:26 +0000718 size_t
cristy3ed852e2009-09-05 21:47:34 +0000719 bit,
720 byte;
721
722 if (status == MagickFalse)
723 continue;
724 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000725 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000726 {
727 status=MagickFalse;
728 continue;
729 }
730 bit=0;
731 byte=0;
732 i=0;
cristybb503372010-05-27 20:51:26 +0000733 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000734 {
735 byte<<=1;
cristyf13c5942012-08-08 23:50:11 +0000736 if (GetPixelIntensity(image,p) < threshold)
cristy3ed852e2009-09-05 21:47:34 +0000737 byte|=0x01;
738 bit++;
739 if (bit == 8)
740 {
741 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
742 bit=0;
743 byte=0;
744 }
cristyed231572011-07-14 02:18:59 +0000745 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000746 }
747 if (bit != 0)
748 {
749 byte<<=(8-bit);
750 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
751 }
752 }
cristy4ee2b0c2012-05-15 00:30:35 +0000753 RadonProjection(image,source_cells,destination_cells,1,projection);
cristy3ed852e2009-09-05 21:47:34 +0000754 image_view=DestroyCacheView(image_view);
755 destination_cells=DestroyRadonInfo(destination_cells);
756 source_cells=DestroyRadonInfo(source_cells);
757 return(MagickTrue);
758}
759
cristybb503372010-05-27 20:51:26 +0000760static void GetImageBackgroundColor(Image *image,const ssize_t offset,
cristy3ed852e2009-09-05 21:47:34 +0000761 ExceptionInfo *exception)
762{
763 CacheView
764 *image_view;
765
cristy4c08aed2011-07-01 19:47:50 +0000766 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000767 background;
768
cristya19f1d72012-08-07 18:24:38 +0000769 double
cristy3ed852e2009-09-05 21:47:34 +0000770 count;
771
cristyd40deb62011-03-09 00:52:27 +0000772 ssize_t
773 y;
774
cristy3ed852e2009-09-05 21:47:34 +0000775 /*
776 Compute average background color.
777 */
778 if (offset <= 0)
779 return;
cristy4c08aed2011-07-01 19:47:50 +0000780 GetPixelInfo(image,&background);
cristy3ed852e2009-09-05 21:47:34 +0000781 count=0.0;
cristy46ff2672012-12-14 15:32:26 +0000782 image_view=AcquireVirtualCacheView(image,exception);
cristybb503372010-05-27 20:51:26 +0000783 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000784 {
cristy4c08aed2011-07-01 19:47:50 +0000785 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000786 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000787
cristybb503372010-05-27 20:51:26 +0000788 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000789 x;
790
cristybb503372010-05-27 20:51:26 +0000791 if ((y >= offset) && (y < ((ssize_t) image->rows-offset)))
cristy3ed852e2009-09-05 21:47:34 +0000792 continue;
793 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000794 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000795 continue;
cristybb503372010-05-27 20:51:26 +0000796 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000797 {
cristybb503372010-05-27 20:51:26 +0000798 if ((x >= offset) && (x < ((ssize_t) image->columns-offset)))
cristy3ed852e2009-09-05 21:47:34 +0000799 continue;
cristy4c08aed2011-07-01 19:47:50 +0000800 background.red+=QuantumScale*GetPixelRed(image,p);
801 background.green+=QuantumScale*GetPixelGreen(image,p);
802 background.blue+=QuantumScale*GetPixelBlue(image,p);
cristy26295322011-09-22 00:50:36 +0000803 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
804 background.alpha+=QuantumScale*GetPixelAlpha(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000805 count++;
cristyed231572011-07-14 02:18:59 +0000806 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000807 }
808 }
809 image_view=DestroyCacheView(image_view);
cristy8cd03c32012-07-07 18:57:59 +0000810 image->background_color.red=(double) ClampToQuantum(QuantumRange*
811 background.red/count);
812 image->background_color.green=(double) ClampToQuantum(QuantumRange*
813 background.green/count);
814 image->background_color.blue=(double) ClampToQuantum(QuantumRange*
815 background.blue/count);
cristy26295322011-09-22 00:50:36 +0000816 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy8cd03c32012-07-07 18:57:59 +0000817 image->background_color.alpha=(double) ClampToQuantum(QuantumRange*
818 background.alpha/count);
cristy3ed852e2009-09-05 21:47:34 +0000819}
820
821MagickExport Image *DeskewImage(const Image *image,const double threshold,
822 ExceptionInfo *exception)
823{
824 AffineMatrix
825 affine_matrix;
826
827 const char
828 *artifact;
829
830 double
831 degrees;
832
833 Image
834 *clone_image,
835 *crop_image,
836 *deskew_image,
837 *median_image;
838
cristy3ed852e2009-09-05 21:47:34 +0000839 MagickBooleanType
840 status;
841
842 RectangleInfo
843 geometry;
844
cristybb503372010-05-27 20:51:26 +0000845 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000846 i;
847
cristybb503372010-05-27 20:51:26 +0000848 size_t
cristy3ed852e2009-09-05 21:47:34 +0000849 max_projection,
850 *projection,
851 width;
852
cristyd40deb62011-03-09 00:52:27 +0000853 ssize_t
854 skew;
855
cristy3ed852e2009-09-05 21:47:34 +0000856 /*
857 Compute deskew angle.
858 */
859 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
cristybb503372010-05-27 20:51:26 +0000860 projection=(size_t *) AcquireQuantumMemory((size_t) (2*width-1),
cristy3ed852e2009-09-05 21:47:34 +0000861 sizeof(*projection));
cristybb503372010-05-27 20:51:26 +0000862 if (projection == (size_t *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000863 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
864 status=RadonTransform(image,threshold,projection,exception);
865 if (status == MagickFalse)
866 {
cristybb503372010-05-27 20:51:26 +0000867 projection=(size_t *) RelinquishMagickMemory(projection);
cristy3ed852e2009-09-05 21:47:34 +0000868 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
869 }
870 max_projection=0;
871 skew=0;
cristybb503372010-05-27 20:51:26 +0000872 for (i=0; i < (ssize_t) (2*width-1); i++)
cristy3ed852e2009-09-05 21:47:34 +0000873 {
874 if (projection[i] > max_projection)
875 {
cristybb503372010-05-27 20:51:26 +0000876 skew=i-(ssize_t) width+1;
cristy3ed852e2009-09-05 21:47:34 +0000877 max_projection=projection[i];
878 }
879 }
cristybb503372010-05-27 20:51:26 +0000880 projection=(size_t *) RelinquishMagickMemory(projection);
cristy3ed852e2009-09-05 21:47:34 +0000881 /*
882 Deskew image.
883 */
884 clone_image=CloneImage(image,0,0,MagickTrue,exception);
885 if (clone_image == (Image *) NULL)
886 return((Image *) NULL);
cristy387430f2012-02-07 13:09:46 +0000887 (void) SetImageVirtualPixelMethod(clone_image,BackgroundVirtualPixelMethod,
888 exception);
cristy3ed852e2009-09-05 21:47:34 +0000889 degrees=RadiansToDegrees(-atan((double) skew/width/8));
890 if (image->debug != MagickFalse)
cristy8cd5b312010-01-07 01:10:24 +0000891 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye7f51092010-01-17 00:39:37 +0000892 " Deskew angle: %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +0000893 affine_matrix.sx=cos(DegreesToRadians(fmod((double) degrees,360.0)));
894 affine_matrix.rx=sin(DegreesToRadians(fmod((double) degrees,360.0)));
895 affine_matrix.ry=(-sin(DegreesToRadians(fmod((double) degrees,360.0))));
896 affine_matrix.sy=cos(DegreesToRadians(fmod((double) degrees,360.0)));
897 affine_matrix.tx=0.0;
898 affine_matrix.ty=0.0;
899 artifact=GetImageArtifact(image,"deskew:auto-crop");
900 if (artifact == (const char *) NULL)
901 {
902 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
903 clone_image=DestroyImage(clone_image);
904 return(deskew_image);
905 }
906 /*
907 Auto-crop image.
908 */
cristyba978e12010-09-12 20:26:50 +0000909 GetImageBackgroundColor(clone_image,(ssize_t) StringToLong(artifact),
910 exception);
cristy3ed852e2009-09-05 21:47:34 +0000911 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
912 clone_image=DestroyImage(clone_image);
913 if (deskew_image == (Image *) NULL)
914 return((Image *) NULL);
cristy95c38342011-03-18 22:39:51 +0000915 median_image=StatisticImage(deskew_image,MedianStatistic,3,3,exception);
cristy3ed852e2009-09-05 21:47:34 +0000916 if (median_image == (Image *) NULL)
917 {
918 deskew_image=DestroyImage(deskew_image);
919 return((Image *) NULL);
920 }
921 geometry=GetImageBoundingBox(median_image,exception);
922 median_image=DestroyImage(median_image);
923 if (image->debug != MagickFalse)
924 (void) LogMagickEvent(TransformEvent,GetMagickModule()," Deskew geometry: "
cristy6d8abba2010-06-03 01:10:47 +0000925 "%.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
cristye8c25f92010-06-03 00:53:06 +0000926 geometry.height,(double) geometry.x,(double) geometry.y);
cristy3ed852e2009-09-05 21:47:34 +0000927 crop_image=CropImage(deskew_image,&geometry,exception);
928 deskew_image=DestroyImage(deskew_image);
929 return(crop_image);
930}
931
932/*
933%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
934% %
935% %
936% %
cristy987feef2011-11-17 12:24:56 +0000937% 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 +0000938% %
939% %
940% %
941%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
942%
cristy3dfa16c2010-05-17 19:34:48 +0000943% IntegralRotateImage() rotates the image an integral of 90 degrees. It
cristy3ed852e2009-09-05 21:47:34 +0000944% allocates the memory necessary for the new Image structure and returns a
945% pointer to the rotated image.
946%
947% The format of the IntegralRotateImage method is:
948%
cristybb503372010-05-27 20:51:26 +0000949% Image *IntegralRotateImage(const Image *image,size_t rotations,
cristy3ed852e2009-09-05 21:47:34 +0000950% ExceptionInfo *exception)
951%
952% A description of each parameter follows.
953%
954% o image: the image.
955%
956% o rotations: Specifies the number of 90 degree rotations.
957%
958*/
cristy987feef2011-11-17 12:24:56 +0000959MagickExport Image *IntegralRotateImage(const Image *image,size_t rotations,
cristy3ed852e2009-09-05 21:47:34 +0000960 ExceptionInfo *exception)
961{
cristy3ed852e2009-09-05 21:47:34 +0000962#define RotateImageTag "Rotate/Image"
963
964 CacheView
965 *image_view,
966 *rotate_view;
967
968 Image
969 *rotate_image;
970
cristy3ed852e2009-09-05 21:47:34 +0000971 MagickBooleanType
972 status;
973
cristy5f959472010-05-27 22:19:46 +0000974 MagickOffsetType
975 progress;
976
cristy3ed852e2009-09-05 21:47:34 +0000977 RectangleInfo
978 page;
979
cristy5f959472010-05-27 22:19:46 +0000980 ssize_t
981 y;
982
cristy3ed852e2009-09-05 21:47:34 +0000983 /*
984 Initialize rotated image attributes.
985 */
986 assert(image != (Image *) NULL);
987 page=image->page;
988 rotations%=4;
cristyb32b90a2009-09-07 21:45:48 +0000989 if (rotations == 0)
990 return(CloneImage(image,0,0,MagickTrue,exception));
cristy3ed852e2009-09-05 21:47:34 +0000991 if ((rotations == 1) || (rotations == 3))
992 rotate_image=CloneImage(image,image->rows,image->columns,MagickTrue,
993 exception);
994 else
995 rotate_image=CloneImage(image,image->columns,image->rows,MagickTrue,
996 exception);
997 if (rotate_image == (Image *) NULL)
998 return((Image *) NULL);
999 /*
1000 Integral rotate the image.
1001 */
1002 status=MagickTrue;
1003 progress=0;
cristy46ff2672012-12-14 15:32:26 +00001004 image_view=AcquireVirtualCacheView(image,exception);
1005 rotate_view=AcquireAuthenticCacheView(rotate_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001006 switch (rotations)
1007 {
1008 case 0:
1009 {
1010 /*
1011 Rotate 0 degrees.
1012 */
cristy3ed852e2009-09-05 21:47:34 +00001013 break;
1014 }
1015 case 1:
1016 {
cristybb503372010-05-27 20:51:26 +00001017 size_t
cristyb32b90a2009-09-07 21:45:48 +00001018 tile_height,
1019 tile_width;
1020
cristyd40deb62011-03-09 00:52:27 +00001021 ssize_t
1022 tile_y;
1023
cristy3ed852e2009-09-05 21:47:34 +00001024 /*
1025 Rotate 90 degrees.
1026 */
cristyb32b90a2009-09-07 21:45:48 +00001027 GetPixelCacheTileSize(image,&tile_width,&tile_height);
cristy8b8148b2012-10-07 00:41:15 +00001028 tile_width=image->columns;
cristy26b64912012-12-16 18:20:09 +00001029#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyd6432472013-01-06 16:56:13 +00001030 #pragma omp parallel for schedule(static,4) \
cristy3e1fa372013-01-06 18:07:11 +00001031 magick_threads(image,image,1,1)
cristy9a5a52f2012-10-09 14:40:31 +00001032#endif
cristyad11aac2011-09-25 21:29:16 +00001033 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
cristy3ed852e2009-09-05 21:47:34 +00001034 {
cristybb503372010-05-27 20:51:26 +00001035 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001036 tile_x;
1037
1038 if (status == MagickFalse)
1039 continue;
cristy26295322011-09-22 00:50:36 +00001040 tile_x=0;
1041 for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
cristy3ed852e2009-09-05 21:47:34 +00001042 {
1043 MagickBooleanType
1044 sync;
1045
cristy4c08aed2011-07-01 19:47:50 +00001046 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001047 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001048
cristy4c08aed2011-07-01 19:47:50 +00001049 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001050 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001051
cristya26211e2011-10-09 01:24:57 +00001052 register ssize_t
1053 y;
1054
cristybb503372010-05-27 20:51:26 +00001055 size_t
cristyb32b90a2009-09-07 21:45:48 +00001056 height,
1057 width;
cristy3ed852e2009-09-05 21:47:34 +00001058
cristyb32b90a2009-09-07 21:45:48 +00001059 width=tile_width;
cristybb503372010-05-27 20:51:26 +00001060 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
cristya45d2ec2011-08-24 13:17:19 +00001061 width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
cristyb32b90a2009-09-07 21:45:48 +00001062 height=tile_height;
cristybb503372010-05-27 20:51:26 +00001063 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
cristya45d2ec2011-08-24 13:17:19 +00001064 height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
cristydaa97692009-09-13 02:10:35 +00001065 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1066 exception);
cristy4c08aed2011-07-01 19:47:50 +00001067 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001068 {
1069 status=MagickFalse;
1070 break;
1071 }
cristybb503372010-05-27 20:51:26 +00001072 for (y=0; y < (ssize_t) width; y++)
cristy3ed852e2009-09-05 21:47:34 +00001073 {
cristy4c08aed2011-07-01 19:47:50 +00001074 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001075 *restrict tile_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001076
cristybb503372010-05-27 20:51:26 +00001077 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001078 x;
1079
cristy27709ef2011-09-18 02:53:53 +00001080 if (status == MagickFalse)
1081 continue;
cristybb503372010-05-27 20:51:26 +00001082 q=QueueCacheViewAuthenticPixels(rotate_view,(ssize_t)
cristy92611572011-07-07 16:02:42 +00001083 (rotate_image->columns-(tile_y+height)),y+tile_x,height,1,
1084 exception);
cristyacd2ed22011-08-30 01:44:23 +00001085 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001086 {
1087 status=MagickFalse;
cristy27709ef2011-09-18 02:53:53 +00001088 continue;
cristy3ed852e2009-09-05 21:47:34 +00001089 }
cristyed231572011-07-14 02:18:59 +00001090 tile_pixels=p+((height-1)*width+y)*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001091 for (x=0; x < (ssize_t) height; x++)
cristy3ed852e2009-09-05 21:47:34 +00001092 {
cristya45d2ec2011-08-24 13:17:19 +00001093 register ssize_t
1094 i;
1095
cristy10a6c612012-01-29 21:41:05 +00001096 if (GetPixelMask(image,tile_pixels) != 0)
1097 {
1098 tile_pixels-=width*GetPixelChannels(image);
1099 q+=GetPixelChannels(rotate_image);
1100 continue;
1101 }
cristya45d2ec2011-08-24 13:17:19 +00001102 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1103 {
1104 PixelChannel
1105 channel;
1106
1107 PixelTrait
1108 rotate_traits,
1109 traits;
1110
cristycf1296e2012-08-26 23:40:49 +00001111 channel=GetPixelChannelChannel(image,i);
1112 traits=GetPixelChannelTraits(image,channel);
1113 rotate_traits=GetPixelChannelTraits(rotate_image,channel);
cristy010d7d12011-08-31 01:02:48 +00001114 if ((traits == UndefinedPixelTrait) ||
1115 (rotate_traits == UndefinedPixelTrait))
cristya45d2ec2011-08-24 13:17:19 +00001116 continue;
cristy0beccfa2011-09-25 20:47:53 +00001117 SetPixelChannel(rotate_image,channel,tile_pixels[i],q);
cristya45d2ec2011-08-24 13:17:19 +00001118 }
cristyed231572011-07-14 02:18:59 +00001119 tile_pixels-=width*GetPixelChannels(image);
1120 q+=GetPixelChannels(rotate_image);
cristy3ed852e2009-09-05 21:47:34 +00001121 }
cristy3ed852e2009-09-05 21:47:34 +00001122 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1123 if (sync == MagickFalse)
1124 status=MagickFalse;
1125 }
1126 }
1127 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1128 {
1129 MagickBooleanType
1130 proceed;
1131
cristy26b64912012-12-16 18:20:09 +00001132#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy9a5a52f2012-10-09 14:40:31 +00001133 #pragma omp critical (MagickCore_IntegralRotateImage)
1134#endif
cristyb32b90a2009-09-07 21:45:48 +00001135 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
cristy3ed852e2009-09-05 21:47:34 +00001136 image->rows);
1137 if (proceed == MagickFalse)
1138 status=MagickFalse;
1139 }
1140 }
cristyecc2c142010-01-17 22:25:46 +00001141 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1142 image->rows-1,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001143 Swap(page.width,page.height);
1144 Swap(page.x,page.y);
1145 if (page.width != 0)
cristybb503372010-05-27 20:51:26 +00001146 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
cristy3ed852e2009-09-05 21:47:34 +00001147 break;
1148 }
1149 case 2:
1150 {
1151 /*
1152 Rotate 180 degrees.
1153 */
cristy5f890612012-12-16 23:34:05 +00001154#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyd6432472013-01-06 16:56:13 +00001155 #pragma omp parallel for schedule(static,4) \
1156 magick_threads(image,image,1,1)
cristy5f890612012-12-16 23:34:05 +00001157#endif
cristy3e1fa372013-01-06 18:07:11 +00001158 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
1159 {
1160 register ssize_t
1161 tile_x;
1162
1163 if (status == MagickFalse)
1164 continue;
1165 tile_x=0;
1166 for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
1167 {
1168 MagickBooleanType
1169 sync;
1170
1171 register const Quantum
1172 *restrict p;
1173
1174 register Quantum
1175 *restrict q;
1176
1177#endif
cristybb503372010-05-27 20:51:26 +00001178 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001179 {
1180 MagickBooleanType
1181 sync;
1182
cristy4c08aed2011-07-01 19:47:50 +00001183 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001184 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001185
cristy4c08aed2011-07-01 19:47:50 +00001186 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001187 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001188
cristy2be50602011-10-09 01:28:13 +00001189 register ssize_t
1190 x;
1191
cristy3ed852e2009-09-05 21:47:34 +00001192 if (status == MagickFalse)
1193 continue;
cristya45d2ec2011-08-24 13:17:19 +00001194 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1195 q=QueueCacheViewAuthenticPixels(rotate_view,0,(ssize_t) (image->rows-y-
1196 1),image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001197 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00001198 {
1199 status=MagickFalse;
1200 continue;
1201 }
cristyed231572011-07-14 02:18:59 +00001202 q+=GetPixelChannels(rotate_image)*image->columns;
cristybb503372010-05-27 20:51:26 +00001203 for (x=0; x < (ssize_t) image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00001204 {
cristya45d2ec2011-08-24 13:17:19 +00001205 register ssize_t
1206 i;
1207
cristyed231572011-07-14 02:18:59 +00001208 q-=GetPixelChannels(rotate_image);
cristy10a6c612012-01-29 21:41:05 +00001209 if (GetPixelMask(image,p) != 0)
1210 {
1211 p+=GetPixelChannels(image);
1212 continue;
1213 }
cristya45d2ec2011-08-24 13:17:19 +00001214 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1215 {
1216 PixelChannel
1217 channel;
1218
1219 PixelTrait
1220 rotate_traits,
1221 traits;
1222
cristycf1296e2012-08-26 23:40:49 +00001223 channel=GetPixelChannelChannel(image,i);
1224 traits=GetPixelChannelTraits(image,channel);
1225 rotate_traits=GetPixelChannelTraits(rotate_image,channel);
cristy010d7d12011-08-31 01:02:48 +00001226 if ((traits == UndefinedPixelTrait) ||
1227 (rotate_traits == UndefinedPixelTrait))
cristya45d2ec2011-08-24 13:17:19 +00001228 continue;
cristy0beccfa2011-09-25 20:47:53 +00001229 SetPixelChannel(rotate_image,channel,p[i],q);
cristya45d2ec2011-08-24 13:17:19 +00001230 }
cristyed231572011-07-14 02:18:59 +00001231 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001232 }
cristy3ed852e2009-09-05 21:47:34 +00001233 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1234 if (sync == MagickFalse)
1235 status=MagickFalse;
1236 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1237 {
1238 MagickBooleanType
1239 proceed;
1240
cristy5f890612012-12-16 23:34:05 +00001241#if defined(MAGICKCORE_OPENMP_SUPPORT)
1242 #pragma omp critical (MagickCore_IntegralRotateImage)
1243#endif
cristy0729beb2011-09-25 23:29:32 +00001244 proceed=SetImageProgress(image,RotateImageTag,progress++,
cristy21e03412011-09-25 22:39:35 +00001245 image->rows);
1246 if (proceed == MagickFalse)
1247 status=MagickFalse;
1248 }
1249 }
1250 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1251 image->rows-1,image->rows);
1252 Swap(page.width,page.height);
1253 Swap(page.x,page.y);
1254 if (page.width != 0)
1255 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1256 break;
1257 }
cristy3ed852e2009-09-05 21:47:34 +00001258 case 3:
1259 {
cristybb503372010-05-27 20:51:26 +00001260 size_t
cristyb32b90a2009-09-07 21:45:48 +00001261 tile_height,
1262 tile_width;
1263
cristyd40deb62011-03-09 00:52:27 +00001264 ssize_t
1265 tile_y;
1266
cristy3ed852e2009-09-05 21:47:34 +00001267 /*
1268 Rotate 270 degrees.
1269 */
cristyb32b90a2009-09-07 21:45:48 +00001270 GetPixelCacheTileSize(image,&tile_width,&tile_height);
cristy8b8148b2012-10-07 00:41:15 +00001271 tile_width=image->columns;
cristy26b64912012-12-16 18:20:09 +00001272#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyd6432472013-01-06 16:56:13 +00001273 #pragma omp parallel for schedule(static,4) \
1274 magick_threads(image,image,image->rows/tile_height,1)
cristy9a5a52f2012-10-09 14:40:31 +00001275#endif
cristyad11aac2011-09-25 21:29:16 +00001276 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
cristy3ed852e2009-09-05 21:47:34 +00001277 {
cristybb503372010-05-27 20:51:26 +00001278 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001279 tile_x;
1280
1281 if (status == MagickFalse)
1282 continue;
cristy26295322011-09-22 00:50:36 +00001283 tile_x=0;
1284 for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
cristy3ed852e2009-09-05 21:47:34 +00001285 {
1286 MagickBooleanType
1287 sync;
1288
cristy4c08aed2011-07-01 19:47:50 +00001289 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001290 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001291
cristy4c08aed2011-07-01 19:47:50 +00001292 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001293 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001294
cristy2be50602011-10-09 01:28:13 +00001295 register ssize_t
1296 y;
1297
cristybb503372010-05-27 20:51:26 +00001298 size_t
cristyb32b90a2009-09-07 21:45:48 +00001299 height,
1300 width;
cristy3ed852e2009-09-05 21:47:34 +00001301
cristyb32b90a2009-09-07 21:45:48 +00001302 width=tile_width;
cristybb503372010-05-27 20:51:26 +00001303 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
cristya45d2ec2011-08-24 13:17:19 +00001304 width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
cristyb32b90a2009-09-07 21:45:48 +00001305 height=tile_height;
cristybb503372010-05-27 20:51:26 +00001306 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
cristya45d2ec2011-08-24 13:17:19 +00001307 height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
1308 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1309 exception);
cristy4c08aed2011-07-01 19:47:50 +00001310 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001311 {
1312 status=MagickFalse;
1313 break;
1314 }
cristybb503372010-05-27 20:51:26 +00001315 for (y=0; y < (ssize_t) width; y++)
cristy3ed852e2009-09-05 21:47:34 +00001316 {
cristy4c08aed2011-07-01 19:47:50 +00001317 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001318 *restrict tile_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001319
cristybb503372010-05-27 20:51:26 +00001320 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001321 x;
1322
cristy27709ef2011-09-18 02:53:53 +00001323 if (status == MagickFalse)
1324 continue;
cristya45d2ec2011-08-24 13:17:19 +00001325 q=QueueCacheViewAuthenticPixels(rotate_view,tile_y,(ssize_t) (y+
1326 rotate_image->rows-(tile_x+width)),height,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001327 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001328 {
1329 status=MagickFalse;
cristy27709ef2011-09-18 02:53:53 +00001330 continue;
cristy3ed852e2009-09-05 21:47:34 +00001331 }
cristyed231572011-07-14 02:18:59 +00001332 tile_pixels=p+((width-1)-y)*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001333 for (x=0; x < (ssize_t) height; x++)
cristy3ed852e2009-09-05 21:47:34 +00001334 {
cristya45d2ec2011-08-24 13:17:19 +00001335 register ssize_t
1336 i;
1337
cristy10a6c612012-01-29 21:41:05 +00001338 if (GetPixelMask(image,tile_pixels) != 0)
1339 {
1340 tile_pixels+=width*GetPixelChannels(image);
1341 q+=GetPixelChannels(rotate_image);
1342 continue;
1343 }
cristya45d2ec2011-08-24 13:17:19 +00001344 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1345 {
1346 PixelChannel
1347 channel;
1348
1349 PixelTrait
1350 rotate_traits,
1351 traits;
1352
cristycf1296e2012-08-26 23:40:49 +00001353 channel=GetPixelChannelChannel(image,i);
1354 traits=GetPixelChannelTraits(image,channel);
1355 rotate_traits=GetPixelChannelTraits(rotate_image,channel);
cristy010d7d12011-08-31 01:02:48 +00001356 if ((traits == UndefinedPixelTrait) ||
1357 (rotate_traits == UndefinedPixelTrait))
cristya45d2ec2011-08-24 13:17:19 +00001358 continue;
cristy0beccfa2011-09-25 20:47:53 +00001359 SetPixelChannel(rotate_image,channel,tile_pixels[i],q);
cristya45d2ec2011-08-24 13:17:19 +00001360 }
cristyed231572011-07-14 02:18:59 +00001361 tile_pixels+=width*GetPixelChannels(image);
1362 q+=GetPixelChannels(rotate_image);
cristy3ed852e2009-09-05 21:47:34 +00001363 }
cristy26b64912012-12-16 18:20:09 +00001364#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy9a5a52f2012-10-09 14:40:31 +00001365 #pragma omp critical (MagickCore_IntegralRotateImage)
1366#endif
cristy3ed852e2009-09-05 21:47:34 +00001367 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1368 if (sync == MagickFalse)
1369 status=MagickFalse;
1370 }
1371 }
1372 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1373 {
1374 MagickBooleanType
1375 proceed;
1376
cristy21e03412011-09-25 22:39:35 +00001377 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
1378 image->rows);
1379 if (proceed == MagickFalse)
1380 status=MagickFalse;
1381 }
1382 }
1383 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1384 image->rows-1,image->rows);
1385 Swap(page.width,page.height);
1386 Swap(page.x,page.y);
1387 if (page.width != 0)
1388 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1389 break;
1390 }
cristy3ed852e2009-09-05 21:47:34 +00001391 }
1392 rotate_view=DestroyCacheView(rotate_view);
1393 image_view=DestroyCacheView(image_view);
1394 rotate_image->type=image->type;
1395 rotate_image->page=page;
1396 if (status == MagickFalse)
1397 rotate_image=DestroyImage(rotate_image);
1398 return(rotate_image);
1399}
1400
1401/*
1402%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1403% %
1404% %
1405% %
1406+ X S h e a r I m a g e %
1407% %
1408% %
1409% %
1410%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1411%
1412% XShearImage() shears the image in the X direction with a shear angle of
1413% 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1414% negative angles shear clockwise. Angles are measured relative to a vertical
1415% Y-axis. X shears will widen an image creating 'empty' triangles on the left
1416% and right sides of the source image.
1417%
1418% The format of the XShearImage method is:
1419%
cristya19f1d72012-08-07 18:24:38 +00001420% MagickBooleanType XShearImage(Image *image,const double degrees,
cristybb503372010-05-27 20:51:26 +00001421% const size_t width,const size_t height,
1422% const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001423%
1424% A description of each parameter follows.
1425%
1426% o image: the image.
1427%
cristya19f1d72012-08-07 18:24:38 +00001428% o degrees: A double representing the shearing angle along the X
cristy3ed852e2009-09-05 21:47:34 +00001429% axis.
1430%
1431% o width, height, x_offset, y_offset: Defines a region of the image
1432% to shear.
1433%
cristyecc2c142010-01-17 22:25:46 +00001434% o exception: return any errors or warnings in this structure.
1435%
cristy3ed852e2009-09-05 21:47:34 +00001436*/
cristya19f1d72012-08-07 18:24:38 +00001437static MagickBooleanType XShearImage(Image *image,const double degrees,
cristybb503372010-05-27 20:51:26 +00001438 const size_t width,const size_t height,const ssize_t x_offset,
1439 const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001440{
1441#define XShearImageTag "XShear/Image"
1442
1443 typedef enum
1444 {
1445 LEFT,
1446 RIGHT
1447 } ShearDirection;
1448
1449 CacheView
1450 *image_view;
1451
cristy3ed852e2009-09-05 21:47:34 +00001452 MagickBooleanType
1453 status;
1454
cristy5f959472010-05-27 22:19:46 +00001455 MagickOffsetType
1456 progress;
1457
cristy4c08aed2011-07-01 19:47:50 +00001458 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001459 background;
1460
cristy5f959472010-05-27 22:19:46 +00001461 ssize_t
1462 y;
1463
cristy9d8c8ce2011-10-25 16:13:52 +00001464 /*
1465 X shear image.
1466 */
cristy3ed852e2009-09-05 21:47:34 +00001467 assert(image != (Image *) NULL);
1468 assert(image->signature == MagickSignature);
1469 if (image->debug != MagickFalse)
1470 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3ed852e2009-09-05 21:47:34 +00001471 status=MagickTrue;
cristy9d8c8ce2011-10-25 16:13:52 +00001472 background=image->background_color;
cristy3ed852e2009-09-05 21:47:34 +00001473 progress=0;
cristy46ff2672012-12-14 15:32:26 +00001474 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00001475#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00001476 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00001477 magick_threads(image,image,height,1)
cristy3ed852e2009-09-05 21:47:34 +00001478#endif
cristybb503372010-05-27 20:51:26 +00001479 for (y=0; y < (ssize_t) height; y++)
cristy3ed852e2009-09-05 21:47:34 +00001480 {
cristy4c08aed2011-07-01 19:47:50 +00001481 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001482 pixel,
1483 source,
1484 destination;
1485
cristya19f1d72012-08-07 18:24:38 +00001486 double
cristy3ed852e2009-09-05 21:47:34 +00001487 area,
1488 displacement;
1489
cristy4c08aed2011-07-01 19:47:50 +00001490 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001491 *restrict p,
1492 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001493
cristyd40deb62011-03-09 00:52:27 +00001494 register ssize_t
1495 i;
1496
cristy3ed852e2009-09-05 21:47:34 +00001497 ShearDirection
1498 direction;
1499
cristyd40deb62011-03-09 00:52:27 +00001500 ssize_t
1501 step;
1502
cristy3ed852e2009-09-05 21:47:34 +00001503 if (status == MagickFalse)
1504 continue;
1505 p=GetCacheViewAuthenticPixels(image_view,0,y_offset+y,image->columns,1,
1506 exception);
cristy4c08aed2011-07-01 19:47:50 +00001507 if (p == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001508 {
1509 status=MagickFalse;
1510 continue;
1511 }
cristyed231572011-07-14 02:18:59 +00001512 p+=x_offset*GetPixelChannels(image);
cristya19f1d72012-08-07 18:24:38 +00001513 displacement=degrees*(double) (y-height/2.0);
cristy3ed852e2009-09-05 21:47:34 +00001514 if (displacement == 0.0)
1515 continue;
1516 if (displacement > 0.0)
1517 direction=RIGHT;
1518 else
1519 {
1520 displacement*=(-1.0);
1521 direction=LEFT;
1522 }
cristybb503372010-05-27 20:51:26 +00001523 step=(ssize_t) floor((double) displacement);
cristya19f1d72012-08-07 18:24:38 +00001524 area=(double) (displacement-step);
cristy3ed852e2009-09-05 21:47:34 +00001525 step++;
1526 pixel=background;
cristy4c08aed2011-07-01 19:47:50 +00001527 GetPixelInfo(image,&source);
1528 GetPixelInfo(image,&destination);
cristy3ed852e2009-09-05 21:47:34 +00001529 switch (direction)
1530 {
1531 case LEFT:
1532 {
1533 /*
1534 Transfer pixels left-to-right.
1535 */
1536 if (step > x_offset)
1537 break;
cristyed231572011-07-14 02:18:59 +00001538 q=p-step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001539 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001540 {
1541 if ((x_offset+i) < step)
1542 {
cristyed231572011-07-14 02:18:59 +00001543 p+=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001544 GetPixelInfoPixel(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001545 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001546 continue;
1547 }
cristy803640d2011-11-17 02:11:32 +00001548 GetPixelInfoPixel(image,p,&source);
cristya19f1d72012-08-07 18:24:38 +00001549 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1550 &source,(double) GetPixelAlpha(image,p),area,&destination);
cristy803640d2011-11-17 02:11:32 +00001551 SetPixelInfoPixel(image,&destination,q);
1552 GetPixelInfoPixel(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001553 p+=GetPixelChannels(image);
1554 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001555 }
cristya19f1d72012-08-07 18:24:38 +00001556 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1557 &background,(double) background.alpha,area,&destination);
cristy803640d2011-11-17 02:11:32 +00001558 SetPixelInfoPixel(image,&destination,q);
cristyed231572011-07-14 02:18:59 +00001559 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001560 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001561 {
cristy803640d2011-11-17 02:11:32 +00001562 SetPixelInfoPixel(image,&background,q);
cristyed231572011-07-14 02:18:59 +00001563 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001564 }
cristy3ed852e2009-09-05 21:47:34 +00001565 break;
1566 }
1567 case RIGHT:
1568 {
1569 /*
1570 Transfer pixels right-to-left.
1571 */
cristyed231572011-07-14 02:18:59 +00001572 p+=width*GetPixelChannels(image);
1573 q=p+step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001574 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001575 {
cristyed231572011-07-14 02:18:59 +00001576 p-=GetPixelChannels(image);
1577 q-=GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001578 if ((size_t) (x_offset+width+step-i) >= image->columns)
cristy3ed852e2009-09-05 21:47:34 +00001579 continue;
cristy803640d2011-11-17 02:11:32 +00001580 GetPixelInfoPixel(image,p,&source);
cristya19f1d72012-08-07 18:24:38 +00001581 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1582 &source,(double) GetPixelAlpha(image,p),area,&destination);
cristy803640d2011-11-17 02:11:32 +00001583 SetPixelInfoPixel(image,&destination,q);
1584 GetPixelInfoPixel(image,p,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001585 }
cristya19f1d72012-08-07 18:24:38 +00001586 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1587 &background,(double) background.alpha,area,&destination);
cristyed231572011-07-14 02:18:59 +00001588 q-=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001589 SetPixelInfoPixel(image,&destination,q);
cristy3ed852e2009-09-05 21:47:34 +00001590 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001591 {
cristyed231572011-07-14 02:18:59 +00001592 q-=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001593 SetPixelInfoPixel(image,&background,q);
cristy4c08aed2011-07-01 19:47:50 +00001594 }
cristy3ed852e2009-09-05 21:47:34 +00001595 break;
1596 }
1597 }
1598 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1599 status=MagickFalse;
1600 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1601 {
1602 MagickBooleanType
1603 proceed;
1604
cristyb5d5f722009-11-04 03:03:49 +00001605#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy7b650b52011-10-09 01:13:39 +00001606 #pragma omp critical (MagickCore_XShearImage)
cristy3ed852e2009-09-05 21:47:34 +00001607#endif
1608 proceed=SetImageProgress(image,XShearImageTag,progress++,height);
1609 if (proceed == MagickFalse)
1610 status=MagickFalse;
1611 }
1612 }
1613 image_view=DestroyCacheView(image_view);
1614 return(status);
1615}
1616
1617/*
1618%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1619% %
1620% %
1621% %
1622+ Y S h e a r I m a g e %
1623% %
1624% %
1625% %
1626%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1627%
1628% YShearImage shears the image in the Y direction with a shear angle of
1629% 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1630% negative angles shear clockwise. Angles are measured relative to a
1631% horizontal X-axis. Y shears will increase the height of an image creating
1632% 'empty' triangles on the top and bottom of the source image.
1633%
1634% The format of the YShearImage method is:
1635%
cristya19f1d72012-08-07 18:24:38 +00001636% MagickBooleanType YShearImage(Image *image,const double degrees,
cristybb503372010-05-27 20:51:26 +00001637% const size_t width,const size_t height,
1638% const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001639%
1640% A description of each parameter follows.
1641%
1642% o image: the image.
1643%
cristya19f1d72012-08-07 18:24:38 +00001644% o degrees: A double representing the shearing angle along the Y
cristy3ed852e2009-09-05 21:47:34 +00001645% axis.
1646%
1647% o width, height, x_offset, y_offset: Defines a region of the image
1648% to shear.
1649%
cristyecc2c142010-01-17 22:25:46 +00001650% o exception: return any errors or warnings in this structure.
1651%
cristy3ed852e2009-09-05 21:47:34 +00001652*/
cristya19f1d72012-08-07 18:24:38 +00001653static MagickBooleanType YShearImage(Image *image,const double degrees,
cristybb503372010-05-27 20:51:26 +00001654 const size_t width,const size_t height,const ssize_t x_offset,
1655 const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001656{
1657#define YShearImageTag "YShear/Image"
1658
1659 typedef enum
1660 {
1661 UP,
1662 DOWN
1663 } ShearDirection;
1664
1665 CacheView
1666 *image_view;
1667
cristy3ed852e2009-09-05 21:47:34 +00001668 MagickBooleanType
1669 status;
1670
cristy5f959472010-05-27 22:19:46 +00001671 MagickOffsetType
1672 progress;
1673
cristy4c08aed2011-07-01 19:47:50 +00001674 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001675 background;
1676
cristy5f959472010-05-27 22:19:46 +00001677 ssize_t
1678 x;
1679
cristy9d8c8ce2011-10-25 16:13:52 +00001680 /*
1681 Y Shear image.
1682 */
cristy3ed852e2009-09-05 21:47:34 +00001683 assert(image != (Image *) NULL);
1684 assert(image->signature == MagickSignature);
1685 if (image->debug != MagickFalse)
1686 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3ed852e2009-09-05 21:47:34 +00001687 status=MagickTrue;
1688 progress=0;
cristy9d8c8ce2011-10-25 16:13:52 +00001689 background=image->background_color;
cristy46ff2672012-12-14 15:32:26 +00001690 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00001691#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00001692 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00001693 magick_threads(image,image,width,1)
cristy3ed852e2009-09-05 21:47:34 +00001694#endif
cristybb503372010-05-27 20:51:26 +00001695 for (x=0; x < (ssize_t) width; x++)
cristy3ed852e2009-09-05 21:47:34 +00001696 {
cristybb503372010-05-27 20:51:26 +00001697 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001698 step;
1699
cristya19f1d72012-08-07 18:24:38 +00001700 double
cristy3ed852e2009-09-05 21:47:34 +00001701 area,
1702 displacement;
1703
cristy4c08aed2011-07-01 19:47:50 +00001704 PixelInfo
1705 pixel,
1706 source,
1707 destination;
1708
1709 register Quantum
1710 *restrict p,
1711 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001712
cristybb503372010-05-27 20:51:26 +00001713 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001714 i;
1715
cristy3ed852e2009-09-05 21:47:34 +00001716 ShearDirection
1717 direction;
1718
1719 if (status == MagickFalse)
1720 continue;
1721 p=GetCacheViewAuthenticPixels(image_view,x_offset+x,0,1,image->rows,
1722 exception);
cristy4c08aed2011-07-01 19:47:50 +00001723 if (p == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001724 {
1725 status=MagickFalse;
1726 continue;
1727 }
cristyed231572011-07-14 02:18:59 +00001728 p+=y_offset*GetPixelChannels(image);
cristya19f1d72012-08-07 18:24:38 +00001729 displacement=degrees*(double) (x-width/2.0);
cristy3ed852e2009-09-05 21:47:34 +00001730 if (displacement == 0.0)
1731 continue;
1732 if (displacement > 0.0)
1733 direction=DOWN;
1734 else
1735 {
1736 displacement*=(-1.0);
1737 direction=UP;
1738 }
cristybb503372010-05-27 20:51:26 +00001739 step=(ssize_t) floor((double) displacement);
cristya19f1d72012-08-07 18:24:38 +00001740 area=(double) (displacement-step);
cristy3ed852e2009-09-05 21:47:34 +00001741 step++;
1742 pixel=background;
cristy4c08aed2011-07-01 19:47:50 +00001743 GetPixelInfo(image,&source);
1744 GetPixelInfo(image,&destination);
cristy3ed852e2009-09-05 21:47:34 +00001745 switch (direction)
1746 {
1747 case UP:
1748 {
1749 /*
1750 Transfer pixels top-to-bottom.
1751 */
1752 if (step > y_offset)
1753 break;
cristyed231572011-07-14 02:18:59 +00001754 q=p-step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001755 for (i=0; i < (ssize_t) height; i++)
cristy3ed852e2009-09-05 21:47:34 +00001756 {
1757 if ((y_offset+i) < step)
1758 {
cristyed231572011-07-14 02:18:59 +00001759 p+=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001760 GetPixelInfoPixel(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001761 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001762 continue;
1763 }
cristy803640d2011-11-17 02:11:32 +00001764 GetPixelInfoPixel(image,p,&source);
cristya19f1d72012-08-07 18:24:38 +00001765 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1766 &source,(double) GetPixelAlpha(image,p),area,
cristy4c08aed2011-07-01 19:47:50 +00001767 &destination);
cristy803640d2011-11-17 02:11:32 +00001768 SetPixelInfoPixel(image,&destination,q);
1769 GetPixelInfoPixel(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001770 p+=GetPixelChannels(image);
1771 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001772 }
cristya19f1d72012-08-07 18:24:38 +00001773 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1774 &background,(double) background.alpha,area,&destination);
cristy803640d2011-11-17 02:11:32 +00001775 SetPixelInfoPixel(image,&destination,q);
cristyed231572011-07-14 02:18:59 +00001776 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001777 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001778 {
cristy803640d2011-11-17 02:11:32 +00001779 SetPixelInfoPixel(image,&background,q);
cristyed231572011-07-14 02:18:59 +00001780 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001781 }
cristy3ed852e2009-09-05 21:47:34 +00001782 break;
1783 }
1784 case DOWN:
1785 {
1786 /*
1787 Transfer pixels bottom-to-top.
1788 */
cristyed231572011-07-14 02:18:59 +00001789 p+=height*GetPixelChannels(image);
1790 q=p+step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001791 for (i=0; i < (ssize_t) height; i++)
cristy3ed852e2009-09-05 21:47:34 +00001792 {
cristyed231572011-07-14 02:18:59 +00001793 p-=GetPixelChannels(image);
1794 q-=GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001795 if ((size_t) (y_offset+height+step-i) >= image->rows)
cristy3ed852e2009-09-05 21:47:34 +00001796 continue;
cristy803640d2011-11-17 02:11:32 +00001797 GetPixelInfoPixel(image,p,&source);
cristya19f1d72012-08-07 18:24:38 +00001798 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1799 &source,(double) GetPixelAlpha(image,p),area,
cristy4c08aed2011-07-01 19:47:50 +00001800 &destination);
cristy803640d2011-11-17 02:11:32 +00001801 SetPixelInfoPixel(image,&destination,q);
1802 GetPixelInfoPixel(image,p,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001803 }
cristya19f1d72012-08-07 18:24:38 +00001804 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1805 &background,(double) background.alpha,area,&destination);
cristyed231572011-07-14 02:18:59 +00001806 q-=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001807 SetPixelInfoPixel(image,&destination,q);
cristy3ed852e2009-09-05 21:47:34 +00001808 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001809 {
cristyed231572011-07-14 02:18:59 +00001810 q-=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001811 SetPixelInfoPixel(image,&background,q);
cristy4c08aed2011-07-01 19:47:50 +00001812 }
cristy3ed852e2009-09-05 21:47:34 +00001813 break;
1814 }
1815 }
1816 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1817 status=MagickFalse;
1818 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1819 {
1820 MagickBooleanType
1821 proceed;
1822
cristyb5d5f722009-11-04 03:03:49 +00001823#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy7b650b52011-10-09 01:13:39 +00001824 #pragma omp critical (MagickCore_YShearImage)
cristy3ed852e2009-09-05 21:47:34 +00001825#endif
1826 proceed=SetImageProgress(image,YShearImageTag,progress++,image->rows);
1827 if (proceed == MagickFalse)
1828 status=MagickFalse;
1829 }
1830 }
1831 image_view=DestroyCacheView(image_view);
1832 return(status);
1833}
1834
1835/*
1836%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1837% %
1838% %
1839% %
cristy3ed852e2009-09-05 21:47:34 +00001840% S h e a r I m a g e %
1841% %
1842% %
1843% %
1844%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1845%
1846% ShearImage() creates a new image that is a shear_image copy of an existing
cristycee97112010-05-28 00:44:52 +00001847% one. Shearing slides one edge of an image along the X or Y axis, creating
1848% a parallelogram. An X direction shear slides an edge along the X axis,
1849% while a Y direction shear slides an edge along the Y axis. The amount of
cristy3ed852e2009-09-05 21:47:34 +00001850% the shear is controlled by a shear angle. For X direction shears, x_shear
1851% is measured relative to the Y axis, and similarly, for Y direction shears
1852% y_shear is measured relative to the X axis. Empty triangles left over from
1853% shearing the image are filled with the background color defined by member
1854% 'background_color' of the image.. ShearImage() allocates the memory
1855% necessary for the new Image structure and returns a pointer to the new image.
1856%
1857% ShearImage() is based on the paper "A Fast Algorithm for General Raster
1858% Rotatation" by Alan W. Paeth.
1859%
1860% The format of the ShearImage method is:
1861%
1862% Image *ShearImage(const Image *image,const double x_shear,
1863% const double y_shear,ExceptionInfo *exception)
1864%
1865% A description of each parameter follows.
1866%
1867% o image: the image.
1868%
1869% o x_shear, y_shear: Specifies the number of degrees to shear the image.
1870%
1871% o exception: return any errors or warnings in this structure.
1872%
1873*/
1874MagickExport Image *ShearImage(const Image *image,const double x_shear,
1875 const double y_shear,ExceptionInfo *exception)
1876{
1877 Image
1878 *integral_image,
1879 *shear_image;
1880
cristybb503372010-05-27 20:51:26 +00001881 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001882 x_offset,
1883 y_offset;
1884
cristyecc2c142010-01-17 22:25:46 +00001885 MagickBooleanType
1886 status;
1887
cristy3ed852e2009-09-05 21:47:34 +00001888 PointInfo
1889 shear;
1890
1891 RectangleInfo
1892 border_info;
1893
cristybb503372010-05-27 20:51:26 +00001894 size_t
cristy3ed852e2009-09-05 21:47:34 +00001895 y_width;
1896
1897 assert(image != (Image *) NULL);
1898 assert(image->signature == MagickSignature);
1899 if (image->debug != MagickFalse)
1900 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1901 assert(exception != (ExceptionInfo *) NULL);
1902 assert(exception->signature == MagickSignature);
1903 if ((x_shear != 0.0) && (fmod(x_shear,90.0) == 0.0))
1904 ThrowImageException(ImageError,"AngleIsDiscontinuous");
1905 if ((y_shear != 0.0) && (fmod(y_shear,90.0) == 0.0))
1906 ThrowImageException(ImageError,"AngleIsDiscontinuous");
1907 /*
1908 Initialize shear angle.
1909 */
1910 integral_image=CloneImage(image,0,0,MagickTrue,exception);
1911 if (integral_image == (Image *) NULL)
1912 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1913 shear.x=(-tan(DegreesToRadians(fmod(x_shear,360.0))));
1914 shear.y=tan(DegreesToRadians(fmod(y_shear,360.0)));
1915 if ((shear.x == 0.0) && (shear.y == 0.0))
1916 return(integral_image);
cristy574cc262011-08-05 01:23:58 +00001917 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001918 {
cristy3ed852e2009-09-05 21:47:34 +00001919 integral_image=DestroyImage(integral_image);
1920 return(integral_image);
1921 }
cristy8a46d822012-08-28 23:32:39 +00001922 if (integral_image->alpha_trait != BlendPixelTrait)
cristy63240882011-08-05 19:05:27 +00001923 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00001924 /*
1925 Compute image size.
1926 */
cristybb503372010-05-27 20:51:26 +00001927 y_width=image->columns+(ssize_t) floor(fabs(shear.x)*image->rows+0.5);
cristycee97112010-05-28 00:44:52 +00001928 x_offset=(ssize_t) ceil((double) image->columns+((fabs(shear.x)*image->rows)-
cristy06609ee2010-03-17 20:21:27 +00001929 image->columns)/2.0-0.5);
cristycee97112010-05-28 00:44:52 +00001930 y_offset=(ssize_t) ceil((double) image->rows+((fabs(shear.y)*y_width)-
1931 image->rows)/2.0-0.5);
cristy3ed852e2009-09-05 21:47:34 +00001932 /*
1933 Surround image with border.
1934 */
1935 integral_image->border_color=integral_image->background_color;
1936 integral_image->compose=CopyCompositeOp;
cristybb503372010-05-27 20:51:26 +00001937 border_info.width=(size_t) x_offset;
1938 border_info.height=(size_t) y_offset;
cristy633f0c62011-09-15 13:27:36 +00001939 shear_image=BorderImage(integral_image,&border_info,image->compose,exception);
cristyecc2c142010-01-17 22:25:46 +00001940 integral_image=DestroyImage(integral_image);
cristy3ed852e2009-09-05 21:47:34 +00001941 if (shear_image == (Image *) NULL)
1942 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +00001943 /*
1944 Shear the image.
1945 */
cristy8a46d822012-08-28 23:32:39 +00001946 if (shear_image->alpha_trait != BlendPixelTrait)
cristy63240882011-08-05 19:05:27 +00001947 (void) SetImageAlphaChannel(shear_image,OpaqueAlphaChannel,exception);
cristyecc2c142010-01-17 22:25:46 +00001948 status=XShearImage(shear_image,shear.x,image->columns,image->rows,x_offset,
cristyeaedf062010-05-29 22:36:02 +00001949 (ssize_t) (shear_image->rows-image->rows)/2,exception);
cristyecc2c142010-01-17 22:25:46 +00001950 if (status == MagickFalse)
1951 {
1952 shear_image=DestroyImage(shear_image);
1953 return((Image *) NULL);
1954 }
cristyeaedf062010-05-29 22:36:02 +00001955 status=YShearImage(shear_image,shear.y,y_width,image->rows,(ssize_t)
1956 (shear_image->columns-y_width)/2,y_offset,exception);
cristyecc2c142010-01-17 22:25:46 +00001957 if (status == MagickFalse)
1958 {
1959 shear_image=DestroyImage(shear_image);
1960 return((Image *) NULL);
1961 }
cristya19f1d72012-08-07 18:24:38 +00001962 status=CropToFitImage(&shear_image,shear.x,shear.y,(double)
1963 image->columns,(double) image->rows,MagickFalse,exception);
cristy3ed852e2009-09-05 21:47:34 +00001964 shear_image->compose=image->compose;
1965 shear_image->page.width=0;
1966 shear_image->page.height=0;
cristy1c2f48d2012-12-14 01:20:55 +00001967 if (status == MagickFalse)
1968 shear_image=DestroyImage(shear_image);
cristy3ed852e2009-09-05 21:47:34 +00001969 return(shear_image);
1970}
cristyc7c81142011-11-07 12:14:23 +00001971
1972/*
1973%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1974% %
1975% %
1976% %
1977% S h e a r R o t a t e I m a g e %
1978% %
1979% %
1980% %
1981%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1982%
1983% ShearRotateImage() creates a new image that is a rotated copy of an existing
1984% one. Positive angles rotate counter-clockwise (right-hand rule), while
1985% negative angles rotate clockwise. Rotated images are usually larger than
1986% the originals and have 'empty' triangular corners. X axis. Empty
1987% triangles left over from shearing the image are filled with the background
1988% color defined by member 'background_color' of the image. ShearRotateImage
1989% allocates the memory necessary for the new Image structure and returns a
1990% pointer to the new image.
1991%
1992% ShearRotateImage() is based on the paper "A Fast Algorithm for General
1993% Raster Rotatation" by Alan W. Paeth. ShearRotateImage is adapted from a
1994% similar method based on the Paeth paper written by Michael Halle of the
1995% Spatial Imaging Group, MIT Media Lab.
1996%
1997% The format of the ShearRotateImage method is:
1998%
1999% Image *ShearRotateImage(const Image *image,const double degrees,
2000% ExceptionInfo *exception)
2001%
2002% A description of each parameter follows.
2003%
2004% o image: the image.
2005%
2006% o degrees: Specifies the number of degrees to rotate the image.
2007%
2008% o exception: return any errors or warnings in this structure.
2009%
2010*/
2011MagickExport Image *ShearRotateImage(const Image *image,const double degrees,
2012 ExceptionInfo *exception)
2013{
2014 Image
2015 *integral_image,
2016 *rotate_image;
2017
2018 MagickBooleanType
2019 status;
2020
cristya19f1d72012-08-07 18:24:38 +00002021 double
cristyc7c81142011-11-07 12:14:23 +00002022 angle;
2023
2024 PointInfo
2025 shear;
2026
2027 RectangleInfo
2028 border_info;
2029
2030 size_t
2031 height,
2032 rotations,
2033 width,
2034 y_width;
2035
2036 ssize_t
2037 x_offset,
2038 y_offset;
2039
2040 /*
2041 Adjust rotation angle.
2042 */
2043 assert(image != (Image *) NULL);
2044 assert(image->signature == MagickSignature);
2045 if (image->debug != MagickFalse)
2046 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2047 assert(exception != (ExceptionInfo *) NULL);
2048 assert(exception->signature == MagickSignature);
2049 angle=degrees;
2050 while (angle < -45.0)
2051 angle+=360.0;
2052 for (rotations=0; angle > 45.0; rotations++)
2053 angle-=90.0;
2054 rotations%=4;
2055 /*
2056 Calculate shear equations.
2057 */
2058 integral_image=IntegralRotateImage(image,rotations,exception);
2059 if (integral_image == (Image *) NULL)
2060 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2061 shear.x=(-tan((double) DegreesToRadians(angle)/2.0));
2062 shear.y=sin((double) DegreesToRadians(angle));
2063 if ((shear.x == 0.0) && (shear.y == 0.0))
2064 return(integral_image);
2065 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
2066 {
2067 integral_image=DestroyImage(integral_image);
2068 return(integral_image);
2069 }
cristy8a46d822012-08-28 23:32:39 +00002070 if (integral_image->alpha_trait != BlendPixelTrait)
cristyc7c81142011-11-07 12:14:23 +00002071 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
2072 /*
2073 Compute image size.
2074 */
2075 width=image->columns;
2076 height=image->rows;
2077 if ((rotations == 1) || (rotations == 3))
2078 {
2079 width=image->rows;
2080 height=image->columns;
2081 }
2082 y_width=width+(ssize_t) floor(fabs(shear.x)*height+0.5);
2083 x_offset=(ssize_t) ceil((double) width+((fabs(shear.y)*height)-width)/2.0-
2084 0.5);
2085 y_offset=(ssize_t) ceil((double) height+((fabs(shear.y)*y_width)-height)/2.0-
2086 0.5);
2087 /*
2088 Surround image with a border.
2089 */
2090 integral_image->border_color=integral_image->background_color;
2091 integral_image->compose=CopyCompositeOp;
2092 border_info.width=(size_t) x_offset;
2093 border_info.height=(size_t) y_offset;
2094 rotate_image=BorderImage(integral_image,&border_info,image->compose,
2095 exception);
2096 integral_image=DestroyImage(integral_image);
2097 if (rotate_image == (Image *) NULL)
2098 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2099 /*
2100 Rotate the image.
2101 */
2102 status=XShearImage(rotate_image,shear.x,width,height,x_offset,(ssize_t)
2103 (rotate_image->rows-height)/2,exception);
2104 if (status == MagickFalse)
2105 {
2106 rotate_image=DestroyImage(rotate_image);
2107 return((Image *) NULL);
2108 }
2109 status=YShearImage(rotate_image,shear.y,y_width,height,(ssize_t)
2110 (rotate_image->columns-y_width)/2,y_offset,exception);
2111 if (status == MagickFalse)
2112 {
2113 rotate_image=DestroyImage(rotate_image);
2114 return((Image *) NULL);
2115 }
2116 status=XShearImage(rotate_image,shear.x,y_width,rotate_image->rows,(ssize_t)
2117 (rotate_image->columns-y_width)/2,0,exception);
2118 if (status == MagickFalse)
2119 {
2120 rotate_image=DestroyImage(rotate_image);
2121 return((Image *) NULL);
2122 }
cristya19f1d72012-08-07 18:24:38 +00002123 status=CropToFitImage(&rotate_image,shear.x,shear.y,(double) width,
2124 (double) height,MagickTrue,exception);
cristyc7c81142011-11-07 12:14:23 +00002125 rotate_image->compose=image->compose;
2126 rotate_image->page.width=0;
2127 rotate_image->page.height=0;
cristy1c2f48d2012-12-14 01:20:55 +00002128 if (status == MagickFalse)
2129 rotate_image=DestroyImage(rotate_image);
cristyc7c81142011-11-07 12:14:23 +00002130 return(rotate_image);
2131}