blob: c22e8aaa910fbbd8327c547065059e38ea308395 [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% %
cristy1454be72011-12-19 01:52:48 +000020% Copyright 1999-2012 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"
cristy0740a982011-10-13 15:01:01 +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,
98% const MagickRealType x_shear,const MagickRealType x_shear,
99% const MagickRealType width,const MagickRealType height,
100% 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,
112 const MagickRealType x_shear,const MagickRealType y_shear,
113 const MagickRealType width,const MagickRealType height,
114 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,
348 "CacheResourcesExhausted","`%s'",image->filename);
349 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
514static void RadonProjection(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)
cristye6178502011-12-23 17:02:29 +0000575 #pragma omp parallel for schedule(static,4)
cristy3ed852e2009-09-05 21:47:34 +0000576#endif
cristybb503372010-05-27 20:51:26 +0000577 for (x=0; x < (ssize_t) p->width; x++)
cristy3ed852e2009-09-05 21:47:34 +0000578 {
cristybb503372010-05-27 20:51:26 +0000579 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000580 y;
581
cristybb503372010-05-27 20:51:26 +0000582 size_t
cristy3ed852e2009-09-05 21:47:34 +0000583 sum;
584
585 sum=0;
cristybb503372010-05-27 20:51:26 +0000586 for (y=0; y < (ssize_t) (p->height-1); y++)
cristy3ed852e2009-09-05 21:47:34 +0000587 {
cristybb503372010-05-27 20:51:26 +0000588 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000589 delta;
590
cristybb503372010-05-27 20:51:26 +0000591 delta=GetRadonCell(p,x,y)-(ssize_t) GetRadonCell(p,x,y+1);
cristy3ed852e2009-09-05 21:47:34 +0000592 sum+=delta*delta;
593 }
594 projection[p->width+sign*x-1]=sum;
595 }
596}
597
598static MagickBooleanType RadonTransform(const Image *image,
cristybb503372010-05-27 20:51:26 +0000599 const double threshold,size_t *projection,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000600{
601 CacheView
602 *image_view;
603
cristy3ed852e2009-09-05 21:47:34 +0000604 MagickBooleanType
605 status;
606
607 RadonInfo
608 *destination_cells,
609 *source_cells;
610
cristybb503372010-05-27 20:51:26 +0000611 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000612 i;
613
cristybb503372010-05-27 20:51:26 +0000614 size_t
cristy3ed852e2009-09-05 21:47:34 +0000615 count,
616 width;
617
cristyd40deb62011-03-09 00:52:27 +0000618 ssize_t
619 y;
620
621 unsigned char
622 byte;
623
cristy3ed852e2009-09-05 21:47:34 +0000624 unsigned short
625 bits[256];
626
627 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
628 source_cells=AcquireRadonInfo(image,width,image->rows,exception);
629 destination_cells=AcquireRadonInfo(image,width,image->rows,exception);
630 if ((source_cells == (RadonInfo *) NULL) ||
631 (destination_cells == (RadonInfo *) NULL))
632 {
633 if (destination_cells != (RadonInfo *) NULL)
634 destination_cells=DestroyRadonInfo(destination_cells);
635 if (source_cells != (RadonInfo *) NULL)
636 source_cells=DestroyRadonInfo(source_cells);
637 return(MagickFalse);
638 }
639 if (ResetRadonCells(source_cells) == MagickFalse)
640 {
641 destination_cells=DestroyRadonInfo(destination_cells);
642 source_cells=DestroyRadonInfo(source_cells);
643 return(MagickFalse);
644 }
645 for (i=0; i < 256; i++)
646 {
647 byte=(unsigned char) i;
648 for (count=0; byte != 0; byte>>=1)
649 count+=byte & 0x01;
650 bits[i]=(unsigned short) count;
651 }
652 status=MagickTrue;
653 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +0000654#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000655 #pragma omp parallel for schedule(static,4) shared(status)
cristy3ed852e2009-09-05 21:47:34 +0000656#endif
cristybb503372010-05-27 20:51:26 +0000657 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000658 {
cristy4c08aed2011-07-01 19:47:50 +0000659 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000660 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000661
cristybb503372010-05-27 20:51:26 +0000662 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000663 i,
664 x;
665
cristybb503372010-05-27 20:51:26 +0000666 size_t
cristy3ed852e2009-09-05 21:47:34 +0000667 bit,
668 byte;
669
670 if (status == MagickFalse)
671 continue;
672 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000673 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000674 {
675 status=MagickFalse;
676 continue;
677 }
678 bit=0;
679 byte=0;
cristybb503372010-05-27 20:51:26 +0000680 i=(ssize_t) (image->columns+7)/8;
681 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000682 {
683 byte<<=1;
cristy5b8dd492011-10-09 13:56:36 +0000684 if ((double) GetPixelIntensity(image,p) < threshold)
cristy3ed852e2009-09-05 21:47:34 +0000685 byte|=0x01;
686 bit++;
687 if (bit == 8)
688 {
689 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
690 bit=0;
691 byte=0;
692 }
cristyed231572011-07-14 02:18:59 +0000693 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000694 }
695 if (bit != 0)
696 {
697 byte<<=(8-bit);
698 (void) SetRadonCell(source_cells,--i,y,bits[byte]);
699 }
700 }
701 RadonProjection(source_cells,destination_cells,-1,projection);
702 (void) ResetRadonCells(source_cells);
cristyb5d5f722009-11-04 03:03:49 +0000703#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000704 #pragma omp parallel for schedule(static,4) shared(status)
cristy3ed852e2009-09-05 21:47:34 +0000705#endif
cristybb503372010-05-27 20:51:26 +0000706 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000707 {
cristy4c08aed2011-07-01 19:47:50 +0000708 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000709 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000710
cristybb503372010-05-27 20:51:26 +0000711 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000712 i,
713 x;
714
cristybb503372010-05-27 20:51:26 +0000715 size_t
cristy3ed852e2009-09-05 21:47:34 +0000716 bit,
717 byte;
718
719 if (status == MagickFalse)
720 continue;
721 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000722 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000723 {
724 status=MagickFalse;
725 continue;
726 }
727 bit=0;
728 byte=0;
729 i=0;
cristybb503372010-05-27 20:51:26 +0000730 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000731 {
732 byte<<=1;
cristy5b8dd492011-10-09 13:56:36 +0000733 if ((double) GetPixelIntensity(image,p) < threshold)
cristy3ed852e2009-09-05 21:47:34 +0000734 byte|=0x01;
735 bit++;
736 if (bit == 8)
737 {
738 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
739 bit=0;
740 byte=0;
741 }
cristyed231572011-07-14 02:18:59 +0000742 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000743 }
744 if (bit != 0)
745 {
746 byte<<=(8-bit);
747 (void) SetRadonCell(source_cells,i++,y,bits[byte]);
748 }
749 }
750 RadonProjection(source_cells,destination_cells,1,projection);
751 image_view=DestroyCacheView(image_view);
752 destination_cells=DestroyRadonInfo(destination_cells);
753 source_cells=DestroyRadonInfo(source_cells);
754 return(MagickTrue);
755}
756
cristybb503372010-05-27 20:51:26 +0000757static void GetImageBackgroundColor(Image *image,const ssize_t offset,
cristy3ed852e2009-09-05 21:47:34 +0000758 ExceptionInfo *exception)
759{
760 CacheView
761 *image_view;
762
cristy4c08aed2011-07-01 19:47:50 +0000763 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000764 background;
765
766 MagickRealType
767 count;
768
cristyd40deb62011-03-09 00:52:27 +0000769 ssize_t
770 y;
771
cristy3ed852e2009-09-05 21:47:34 +0000772 /*
773 Compute average background color.
774 */
775 if (offset <= 0)
776 return;
cristy4c08aed2011-07-01 19:47:50 +0000777 GetPixelInfo(image,&background);
cristy3ed852e2009-09-05 21:47:34 +0000778 count=0.0;
779 image_view=AcquireCacheView(image);
cristybb503372010-05-27 20:51:26 +0000780 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000781 {
cristy4c08aed2011-07-01 19:47:50 +0000782 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000783 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000784
cristybb503372010-05-27 20:51:26 +0000785 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000786 x;
787
cristybb503372010-05-27 20:51:26 +0000788 if ((y >= offset) && (y < ((ssize_t) image->rows-offset)))
cristy3ed852e2009-09-05 21:47:34 +0000789 continue;
790 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000791 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000792 continue;
cristybb503372010-05-27 20:51:26 +0000793 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000794 {
cristybb503372010-05-27 20:51:26 +0000795 if ((x >= offset) && (x < ((ssize_t) image->columns-offset)))
cristy3ed852e2009-09-05 21:47:34 +0000796 continue;
cristy4c08aed2011-07-01 19:47:50 +0000797 background.red+=QuantumScale*GetPixelRed(image,p);
798 background.green+=QuantumScale*GetPixelGreen(image,p);
799 background.blue+=QuantumScale*GetPixelBlue(image,p);
cristy26295322011-09-22 00:50:36 +0000800 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
801 background.alpha+=QuantumScale*GetPixelAlpha(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000802 count++;
cristyed231572011-07-14 02:18:59 +0000803 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000804 }
805 }
806 image_view=DestroyCacheView(image_view);
cristy5b8dd492011-10-09 13:56:36 +0000807 image->background_color.red=(double) ClampToQuantum((MagickRealType)
808 QuantumRange*background.red/count);
809 image->background_color.green=(double) ClampToQuantum((MagickRealType)
810 QuantumRange*background.green/count);
811 image->background_color.blue=(double) ClampToQuantum((MagickRealType)
812 QuantumRange*background.blue/count);
cristy26295322011-09-22 00:50:36 +0000813 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy5b8dd492011-10-09 13:56:36 +0000814 image->background_color.alpha=(double) ClampToQuantum((MagickRealType)
815 QuantumRange*background.alpha/count);
cristy3ed852e2009-09-05 21:47:34 +0000816}
817
818MagickExport Image *DeskewImage(const Image *image,const double threshold,
819 ExceptionInfo *exception)
820{
821 AffineMatrix
822 affine_matrix;
823
824 const char
825 *artifact;
826
827 double
828 degrees;
829
830 Image
831 *clone_image,
832 *crop_image,
833 *deskew_image,
834 *median_image;
835
cristy3ed852e2009-09-05 21:47:34 +0000836 MagickBooleanType
837 status;
838
839 RectangleInfo
840 geometry;
841
cristybb503372010-05-27 20:51:26 +0000842 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000843 i;
844
cristybb503372010-05-27 20:51:26 +0000845 size_t
cristy3ed852e2009-09-05 21:47:34 +0000846 max_projection,
847 *projection,
848 width;
849
cristyd40deb62011-03-09 00:52:27 +0000850 ssize_t
851 skew;
852
cristy3ed852e2009-09-05 21:47:34 +0000853 /*
854 Compute deskew angle.
855 */
856 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
cristybb503372010-05-27 20:51:26 +0000857 projection=(size_t *) AcquireQuantumMemory((size_t) (2*width-1),
cristy3ed852e2009-09-05 21:47:34 +0000858 sizeof(*projection));
cristybb503372010-05-27 20:51:26 +0000859 if (projection == (size_t *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000860 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
861 status=RadonTransform(image,threshold,projection,exception);
862 if (status == MagickFalse)
863 {
cristybb503372010-05-27 20:51:26 +0000864 projection=(size_t *) RelinquishMagickMemory(projection);
cristy3ed852e2009-09-05 21:47:34 +0000865 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
866 }
867 max_projection=0;
868 skew=0;
cristybb503372010-05-27 20:51:26 +0000869 for (i=0; i < (ssize_t) (2*width-1); i++)
cristy3ed852e2009-09-05 21:47:34 +0000870 {
871 if (projection[i] > max_projection)
872 {
cristybb503372010-05-27 20:51:26 +0000873 skew=i-(ssize_t) width+1;
cristy3ed852e2009-09-05 21:47:34 +0000874 max_projection=projection[i];
875 }
876 }
cristybb503372010-05-27 20:51:26 +0000877 projection=(size_t *) RelinquishMagickMemory(projection);
cristy3ed852e2009-09-05 21:47:34 +0000878 /*
879 Deskew image.
880 */
881 clone_image=CloneImage(image,0,0,MagickTrue,exception);
882 if (clone_image == (Image *) NULL)
883 return((Image *) NULL);
884 (void) SetImageVirtualPixelMethod(clone_image,BackgroundVirtualPixelMethod);
885 degrees=RadiansToDegrees(-atan((double) skew/width/8));
886 if (image->debug != MagickFalse)
cristy8cd5b312010-01-07 01:10:24 +0000887 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
cristye7f51092010-01-17 00:39:37 +0000888 " Deskew angle: %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +0000889 affine_matrix.sx=cos(DegreesToRadians(fmod((double) degrees,360.0)));
890 affine_matrix.rx=sin(DegreesToRadians(fmod((double) degrees,360.0)));
891 affine_matrix.ry=(-sin(DegreesToRadians(fmod((double) degrees,360.0))));
892 affine_matrix.sy=cos(DegreesToRadians(fmod((double) degrees,360.0)));
893 affine_matrix.tx=0.0;
894 affine_matrix.ty=0.0;
895 artifact=GetImageArtifact(image,"deskew:auto-crop");
896 if (artifact == (const char *) NULL)
897 {
898 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
899 clone_image=DestroyImage(clone_image);
900 return(deskew_image);
901 }
902 /*
903 Auto-crop image.
904 */
cristyba978e12010-09-12 20:26:50 +0000905 GetImageBackgroundColor(clone_image,(ssize_t) StringToLong(artifact),
906 exception);
cristy3ed852e2009-09-05 21:47:34 +0000907 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
908 clone_image=DestroyImage(clone_image);
909 if (deskew_image == (Image *) NULL)
910 return((Image *) NULL);
cristy95c38342011-03-18 22:39:51 +0000911 median_image=StatisticImage(deskew_image,MedianStatistic,3,3,exception);
cristy3ed852e2009-09-05 21:47:34 +0000912 if (median_image == (Image *) NULL)
913 {
914 deskew_image=DestroyImage(deskew_image);
915 return((Image *) NULL);
916 }
917 geometry=GetImageBoundingBox(median_image,exception);
918 median_image=DestroyImage(median_image);
919 if (image->debug != MagickFalse)
920 (void) LogMagickEvent(TransformEvent,GetMagickModule()," Deskew geometry: "
cristy6d8abba2010-06-03 01:10:47 +0000921 "%.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
cristye8c25f92010-06-03 00:53:06 +0000922 geometry.height,(double) geometry.x,(double) geometry.y);
cristy3ed852e2009-09-05 21:47:34 +0000923 crop_image=CropImage(deskew_image,&geometry,exception);
924 deskew_image=DestroyImage(deskew_image);
925 return(crop_image);
926}
927
928/*
929%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
930% %
931% %
932% %
cristy987feef2011-11-17 12:24:56 +0000933% 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 +0000934% %
935% %
936% %
937%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
938%
cristy3dfa16c2010-05-17 19:34:48 +0000939% IntegralRotateImage() rotates the image an integral of 90 degrees. It
cristy3ed852e2009-09-05 21:47:34 +0000940% allocates the memory necessary for the new Image structure and returns a
941% pointer to the rotated image.
942%
943% The format of the IntegralRotateImage method is:
944%
cristybb503372010-05-27 20:51:26 +0000945% Image *IntegralRotateImage(const Image *image,size_t rotations,
cristy3ed852e2009-09-05 21:47:34 +0000946% ExceptionInfo *exception)
947%
948% A description of each parameter follows.
949%
950% o image: the image.
951%
952% o rotations: Specifies the number of 90 degree rotations.
953%
954*/
cristy987feef2011-11-17 12:24:56 +0000955MagickExport Image *IntegralRotateImage(const Image *image,size_t rotations,
cristy3ed852e2009-09-05 21:47:34 +0000956 ExceptionInfo *exception)
957{
cristy3ed852e2009-09-05 21:47:34 +0000958#define RotateImageTag "Rotate/Image"
959
960 CacheView
961 *image_view,
962 *rotate_view;
963
964 Image
965 *rotate_image;
966
cristy3ed852e2009-09-05 21:47:34 +0000967 MagickBooleanType
968 status;
969
cristy5f959472010-05-27 22:19:46 +0000970 MagickOffsetType
971 progress;
972
cristy3ed852e2009-09-05 21:47:34 +0000973 RectangleInfo
974 page;
975
cristy5f959472010-05-27 22:19:46 +0000976 ssize_t
977 y;
978
cristy3ed852e2009-09-05 21:47:34 +0000979 /*
980 Initialize rotated image attributes.
981 */
982 assert(image != (Image *) NULL);
983 page=image->page;
984 rotations%=4;
cristyb32b90a2009-09-07 21:45:48 +0000985 if (rotations == 0)
986 return(CloneImage(image,0,0,MagickTrue,exception));
cristy3ed852e2009-09-05 21:47:34 +0000987 if ((rotations == 1) || (rotations == 3))
988 rotate_image=CloneImage(image,image->rows,image->columns,MagickTrue,
989 exception);
990 else
991 rotate_image=CloneImage(image,image->columns,image->rows,MagickTrue,
992 exception);
993 if (rotate_image == (Image *) NULL)
994 return((Image *) NULL);
995 /*
996 Integral rotate the image.
997 */
998 status=MagickTrue;
999 progress=0;
1000 image_view=AcquireCacheView(image);
1001 rotate_view=AcquireCacheView(rotate_image);
1002 switch (rotations)
1003 {
1004 case 0:
1005 {
1006 /*
1007 Rotate 0 degrees.
1008 */
cristy3ed852e2009-09-05 21:47:34 +00001009 break;
1010 }
1011 case 1:
1012 {
cristybb503372010-05-27 20:51:26 +00001013 size_t
cristyb32b90a2009-09-07 21:45:48 +00001014 tile_height,
1015 tile_width;
1016
cristyd40deb62011-03-09 00:52:27 +00001017 ssize_t
1018 tile_y;
1019
cristy3ed852e2009-09-05 21:47:34 +00001020 /*
1021 Rotate 90 degrees.
1022 */
cristyb32b90a2009-09-07 21:45:48 +00001023 GetPixelCacheTileSize(image,&tile_width,&tile_height);
cristye31fbf02011-10-10 11:58:31 +00001024#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy19593872012-01-22 02:00:33 +00001025 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristyad11aac2011-09-25 21:29:16 +00001026#endif
1027 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
cristy3ed852e2009-09-05 21:47:34 +00001028 {
cristybb503372010-05-27 20:51:26 +00001029 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001030 tile_x;
1031
1032 if (status == MagickFalse)
1033 continue;
cristy26295322011-09-22 00:50:36 +00001034 tile_x=0;
1035 for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
cristy3ed852e2009-09-05 21:47:34 +00001036 {
1037 MagickBooleanType
1038 sync;
1039
cristy4c08aed2011-07-01 19:47:50 +00001040 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001041 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001042
cristy4c08aed2011-07-01 19:47:50 +00001043 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001044 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001045
cristya26211e2011-10-09 01:24:57 +00001046 register ssize_t
1047 y;
1048
cristybb503372010-05-27 20:51:26 +00001049 size_t
cristyb32b90a2009-09-07 21:45:48 +00001050 height,
1051 width;
cristy3ed852e2009-09-05 21:47:34 +00001052
cristyb32b90a2009-09-07 21:45:48 +00001053 width=tile_width;
cristybb503372010-05-27 20:51:26 +00001054 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
cristya45d2ec2011-08-24 13:17:19 +00001055 width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
cristyb32b90a2009-09-07 21:45:48 +00001056 height=tile_height;
cristybb503372010-05-27 20:51:26 +00001057 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
cristya45d2ec2011-08-24 13:17:19 +00001058 height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
cristydaa97692009-09-13 02:10:35 +00001059 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1060 exception);
cristy4c08aed2011-07-01 19:47:50 +00001061 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001062 {
1063 status=MagickFalse;
1064 break;
1065 }
cristybb503372010-05-27 20:51:26 +00001066 for (y=0; y < (ssize_t) width; y++)
cristy3ed852e2009-09-05 21:47:34 +00001067 {
cristy4c08aed2011-07-01 19:47:50 +00001068 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001069 *restrict tile_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001070
cristybb503372010-05-27 20:51:26 +00001071 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001072 x;
1073
cristy27709ef2011-09-18 02:53:53 +00001074 if (status == MagickFalse)
1075 continue;
cristybb503372010-05-27 20:51:26 +00001076 q=QueueCacheViewAuthenticPixels(rotate_view,(ssize_t)
cristy92611572011-07-07 16:02:42 +00001077 (rotate_image->columns-(tile_y+height)),y+tile_x,height,1,
1078 exception);
cristyacd2ed22011-08-30 01:44:23 +00001079 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001080 {
1081 status=MagickFalse;
cristy27709ef2011-09-18 02:53:53 +00001082 continue;
cristy3ed852e2009-09-05 21:47:34 +00001083 }
cristyed231572011-07-14 02:18:59 +00001084 tile_pixels=p+((height-1)*width+y)*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001085 for (x=0; x < (ssize_t) height; x++)
cristy3ed852e2009-09-05 21:47:34 +00001086 {
cristya45d2ec2011-08-24 13:17:19 +00001087 register ssize_t
1088 i;
1089
cristy10a6c612012-01-29 21:41:05 +00001090 if (GetPixelMask(image,tile_pixels) != 0)
1091 {
1092 tile_pixels-=width*GetPixelChannels(image);
1093 q+=GetPixelChannels(rotate_image);
1094 continue;
1095 }
cristya45d2ec2011-08-24 13:17:19 +00001096 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1097 {
1098 PixelChannel
1099 channel;
1100
1101 PixelTrait
1102 rotate_traits,
1103 traits;
1104
cristye2a912b2011-12-05 20:02:07 +00001105 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00001106 traits=GetPixelChannelMapTraits(image,channel);
cristya45d2ec2011-08-24 13:17:19 +00001107 rotate_traits=GetPixelChannelMapTraits(rotate_image,channel);
cristy010d7d12011-08-31 01:02:48 +00001108 if ((traits == UndefinedPixelTrait) ||
1109 (rotate_traits == UndefinedPixelTrait))
cristya45d2ec2011-08-24 13:17:19 +00001110 continue;
cristy0beccfa2011-09-25 20:47:53 +00001111 SetPixelChannel(rotate_image,channel,tile_pixels[i],q);
cristya45d2ec2011-08-24 13:17:19 +00001112 }
cristyed231572011-07-14 02:18:59 +00001113 tile_pixels-=width*GetPixelChannels(image);
1114 q+=GetPixelChannels(rotate_image);
cristy3ed852e2009-09-05 21:47:34 +00001115 }
cristy3ed852e2009-09-05 21:47:34 +00001116 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1117 if (sync == MagickFalse)
1118 status=MagickFalse;
1119 }
1120 }
1121 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1122 {
1123 MagickBooleanType
1124 proceed;
1125
cristy21e03412011-09-25 22:39:35 +00001126#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy7b650b52011-10-09 01:13:39 +00001127 #pragma omp critical (MagickCore_IntegralRotateImage)
cristy21e03412011-09-25 22:39:35 +00001128#endif
cristyb32b90a2009-09-07 21:45:48 +00001129 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
cristy3ed852e2009-09-05 21:47:34 +00001130 image->rows);
1131 if (proceed == MagickFalse)
1132 status=MagickFalse;
1133 }
1134 }
cristyecc2c142010-01-17 22:25:46 +00001135 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1136 image->rows-1,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001137 Swap(page.width,page.height);
1138 Swap(page.x,page.y);
1139 if (page.width != 0)
cristybb503372010-05-27 20:51:26 +00001140 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
cristy3ed852e2009-09-05 21:47:34 +00001141 break;
1142 }
1143 case 2:
1144 {
1145 /*
1146 Rotate 180 degrees.
1147 */
cristye31fbf02011-10-10 11:58:31 +00001148#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy19593872012-01-22 02:00:33 +00001149 #pragma omp parallel for schedule(static) shared(progress,status)
cristydaa97692009-09-13 02:10:35 +00001150#endif
cristybb503372010-05-27 20:51:26 +00001151 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001152 {
1153 MagickBooleanType
1154 sync;
1155
cristy4c08aed2011-07-01 19:47:50 +00001156 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001157 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001158
cristy4c08aed2011-07-01 19:47:50 +00001159 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001160 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001161
cristy2be50602011-10-09 01:28:13 +00001162 register ssize_t
1163 x;
1164
cristy3ed852e2009-09-05 21:47:34 +00001165 if (status == MagickFalse)
1166 continue;
cristya45d2ec2011-08-24 13:17:19 +00001167 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1168 q=QueueCacheViewAuthenticPixels(rotate_view,0,(ssize_t) (image->rows-y-
1169 1),image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001170 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00001171 {
1172 status=MagickFalse;
1173 continue;
1174 }
cristyed231572011-07-14 02:18:59 +00001175 q+=GetPixelChannels(rotate_image)*image->columns;
cristybb503372010-05-27 20:51:26 +00001176 for (x=0; x < (ssize_t) image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00001177 {
cristya45d2ec2011-08-24 13:17:19 +00001178 register ssize_t
1179 i;
1180
cristyed231572011-07-14 02:18:59 +00001181 q-=GetPixelChannels(rotate_image);
cristy10a6c612012-01-29 21:41:05 +00001182 if (GetPixelMask(image,p) != 0)
1183 {
1184 p+=GetPixelChannels(image);
1185 continue;
1186 }
cristya45d2ec2011-08-24 13:17:19 +00001187 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1188 {
1189 PixelChannel
1190 channel;
1191
1192 PixelTrait
1193 rotate_traits,
1194 traits;
1195
cristye2a912b2011-12-05 20:02:07 +00001196 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00001197 traits=GetPixelChannelMapTraits(image,channel);
cristya45d2ec2011-08-24 13:17:19 +00001198 rotate_traits=GetPixelChannelMapTraits(rotate_image,channel);
cristy010d7d12011-08-31 01:02:48 +00001199 if ((traits == UndefinedPixelTrait) ||
1200 (rotate_traits == UndefinedPixelTrait))
cristya45d2ec2011-08-24 13:17:19 +00001201 continue;
cristy0beccfa2011-09-25 20:47:53 +00001202 SetPixelChannel(rotate_image,channel,p[i],q);
cristya45d2ec2011-08-24 13:17:19 +00001203 }
cristyed231572011-07-14 02:18:59 +00001204 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001205 }
cristy3ed852e2009-09-05 21:47:34 +00001206 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1207 if (sync == MagickFalse)
1208 status=MagickFalse;
1209 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1210 {
1211 MagickBooleanType
1212 proceed;
1213
cristy21e03412011-09-25 22:39:35 +00001214#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy7b650b52011-10-09 01:13:39 +00001215 #pragma omp critical (MagickCore_IntegralRotateImage)
cristy21e03412011-09-25 22:39:35 +00001216#endif
cristy0729beb2011-09-25 23:29:32 +00001217 proceed=SetImageProgress(image,RotateImageTag,progress++,
cristy21e03412011-09-25 22:39:35 +00001218 image->rows);
1219 if (proceed == MagickFalse)
1220 status=MagickFalse;
1221 }
1222 }
1223 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1224 image->rows-1,image->rows);
1225 Swap(page.width,page.height);
1226 Swap(page.x,page.y);
1227 if (page.width != 0)
1228 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1229 break;
1230 }
cristy3ed852e2009-09-05 21:47:34 +00001231 case 3:
1232 {
cristybb503372010-05-27 20:51:26 +00001233 size_t
cristyb32b90a2009-09-07 21:45:48 +00001234 tile_height,
1235 tile_width;
1236
cristyd40deb62011-03-09 00:52:27 +00001237 ssize_t
1238 tile_y;
1239
cristy3ed852e2009-09-05 21:47:34 +00001240 /*
1241 Rotate 270 degrees.
1242 */
cristyb32b90a2009-09-07 21:45:48 +00001243 GetPixelCacheTileSize(image,&tile_width,&tile_height);
cristye31fbf02011-10-10 11:58:31 +00001244#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy19593872012-01-22 02:00:33 +00001245 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristyad11aac2011-09-25 21:29:16 +00001246#endif
1247 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
cristy3ed852e2009-09-05 21:47:34 +00001248 {
cristybb503372010-05-27 20:51:26 +00001249 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001250 tile_x;
1251
1252 if (status == MagickFalse)
1253 continue;
cristy26295322011-09-22 00:50:36 +00001254 tile_x=0;
1255 for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
cristy3ed852e2009-09-05 21:47:34 +00001256 {
1257 MagickBooleanType
1258 sync;
1259
cristy4c08aed2011-07-01 19:47:50 +00001260 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001261 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001262
cristy4c08aed2011-07-01 19:47:50 +00001263 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001264 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001265
cristy2be50602011-10-09 01:28:13 +00001266 register ssize_t
1267 y;
1268
cristybb503372010-05-27 20:51:26 +00001269 size_t
cristyb32b90a2009-09-07 21:45:48 +00001270 height,
1271 width;
cristy3ed852e2009-09-05 21:47:34 +00001272
cristyb32b90a2009-09-07 21:45:48 +00001273 width=tile_width;
cristybb503372010-05-27 20:51:26 +00001274 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
cristya45d2ec2011-08-24 13:17:19 +00001275 width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
cristyb32b90a2009-09-07 21:45:48 +00001276 height=tile_height;
cristybb503372010-05-27 20:51:26 +00001277 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
cristya45d2ec2011-08-24 13:17:19 +00001278 height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
1279 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1280 exception);
cristy4c08aed2011-07-01 19:47:50 +00001281 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001282 {
1283 status=MagickFalse;
1284 break;
1285 }
cristybb503372010-05-27 20:51:26 +00001286 for (y=0; y < (ssize_t) width; y++)
cristy3ed852e2009-09-05 21:47:34 +00001287 {
cristy4c08aed2011-07-01 19:47:50 +00001288 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001289 *restrict tile_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001290
cristybb503372010-05-27 20:51:26 +00001291 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001292 x;
1293
cristy27709ef2011-09-18 02:53:53 +00001294 if (status == MagickFalse)
1295 continue;
cristya45d2ec2011-08-24 13:17:19 +00001296 q=QueueCacheViewAuthenticPixels(rotate_view,tile_y,(ssize_t) (y+
1297 rotate_image->rows-(tile_x+width)),height,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001298 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001299 {
1300 status=MagickFalse;
cristy27709ef2011-09-18 02:53:53 +00001301 continue;
cristy3ed852e2009-09-05 21:47:34 +00001302 }
cristyed231572011-07-14 02:18:59 +00001303 tile_pixels=p+((width-1)-y)*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001304 for (x=0; x < (ssize_t) height; x++)
cristy3ed852e2009-09-05 21:47:34 +00001305 {
cristya45d2ec2011-08-24 13:17:19 +00001306 register ssize_t
1307 i;
1308
cristy10a6c612012-01-29 21:41:05 +00001309 if (GetPixelMask(image,tile_pixels) != 0)
1310 {
1311 tile_pixels+=width*GetPixelChannels(image);
1312 q+=GetPixelChannels(rotate_image);
1313 continue;
1314 }
cristya45d2ec2011-08-24 13:17:19 +00001315 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1316 {
1317 PixelChannel
1318 channel;
1319
1320 PixelTrait
1321 rotate_traits,
1322 traits;
1323
cristye2a912b2011-12-05 20:02:07 +00001324 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +00001325 traits=GetPixelChannelMapTraits(image,channel);
cristya45d2ec2011-08-24 13:17:19 +00001326 rotate_traits=GetPixelChannelMapTraits(rotate_image,channel);
cristy010d7d12011-08-31 01:02:48 +00001327 if ((traits == UndefinedPixelTrait) ||
1328 (rotate_traits == UndefinedPixelTrait))
cristya45d2ec2011-08-24 13:17:19 +00001329 continue;
cristy0beccfa2011-09-25 20:47:53 +00001330 SetPixelChannel(rotate_image,channel,tile_pixels[i],q);
cristya45d2ec2011-08-24 13:17:19 +00001331 }
cristyed231572011-07-14 02:18:59 +00001332 tile_pixels+=width*GetPixelChannels(image);
1333 q+=GetPixelChannels(rotate_image);
cristy3ed852e2009-09-05 21:47:34 +00001334 }
cristy3ed852e2009-09-05 21:47:34 +00001335 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1336 if (sync == MagickFalse)
1337 status=MagickFalse;
1338 }
1339 }
1340 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1341 {
1342 MagickBooleanType
1343 proceed;
1344
cristy21e03412011-09-25 22:39:35 +00001345#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy7b650b52011-10-09 01:13:39 +00001346 #pragma omp critical (MagickCore_IntegralRotateImage)
cristy21e03412011-09-25 22:39:35 +00001347#endif
1348 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
1349 image->rows);
1350 if (proceed == MagickFalse)
1351 status=MagickFalse;
1352 }
1353 }
1354 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1355 image->rows-1,image->rows);
1356 Swap(page.width,page.height);
1357 Swap(page.x,page.y);
1358 if (page.width != 0)
1359 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1360 break;
1361 }
cristy3ed852e2009-09-05 21:47:34 +00001362 }
1363 rotate_view=DestroyCacheView(rotate_view);
1364 image_view=DestroyCacheView(image_view);
1365 rotate_image->type=image->type;
1366 rotate_image->page=page;
1367 if (status == MagickFalse)
1368 rotate_image=DestroyImage(rotate_image);
1369 return(rotate_image);
1370}
1371
1372/*
1373%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1374% %
1375% %
1376% %
1377+ X S h e a r I m a g e %
1378% %
1379% %
1380% %
1381%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1382%
1383% XShearImage() shears the image in the X direction with a shear angle of
1384% 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1385% negative angles shear clockwise. Angles are measured relative to a vertical
1386% Y-axis. X shears will widen an image creating 'empty' triangles on the left
1387% and right sides of the source image.
1388%
1389% The format of the XShearImage method is:
1390%
1391% MagickBooleanType XShearImage(Image *image,const MagickRealType degrees,
cristybb503372010-05-27 20:51:26 +00001392% const size_t width,const size_t height,
1393% const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001394%
1395% A description of each parameter follows.
1396%
1397% o image: the image.
1398%
cristycee97112010-05-28 00:44:52 +00001399% o degrees: A MagickRealType representing the shearing angle along the X
cristy3ed852e2009-09-05 21:47:34 +00001400% axis.
1401%
1402% o width, height, x_offset, y_offset: Defines a region of the image
1403% to shear.
1404%
cristyecc2c142010-01-17 22:25:46 +00001405% o exception: return any errors or warnings in this structure.
1406%
cristy3ed852e2009-09-05 21:47:34 +00001407*/
1408static MagickBooleanType XShearImage(Image *image,const MagickRealType degrees,
cristybb503372010-05-27 20:51:26 +00001409 const size_t width,const size_t height,const ssize_t x_offset,
1410 const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001411{
1412#define XShearImageTag "XShear/Image"
1413
1414 typedef enum
1415 {
1416 LEFT,
1417 RIGHT
1418 } ShearDirection;
1419
1420 CacheView
1421 *image_view;
1422
cristy3ed852e2009-09-05 21:47:34 +00001423 MagickBooleanType
1424 status;
1425
cristy5f959472010-05-27 22:19:46 +00001426 MagickOffsetType
1427 progress;
1428
cristy4c08aed2011-07-01 19:47:50 +00001429 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001430 background;
1431
cristy5f959472010-05-27 22:19:46 +00001432 ssize_t
1433 y;
1434
cristy9d8c8ce2011-10-25 16:13:52 +00001435 /*
1436 X shear image.
1437 */
cristy3ed852e2009-09-05 21:47:34 +00001438 assert(image != (Image *) NULL);
1439 assert(image->signature == MagickSignature);
1440 if (image->debug != MagickFalse)
1441 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3ed852e2009-09-05 21:47:34 +00001442 status=MagickTrue;
cristy9d8c8ce2011-10-25 16:13:52 +00001443 background=image->background_color;
cristy3ed852e2009-09-05 21:47:34 +00001444 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00001445 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00001446#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyd36a25e2012-01-18 14:30:53 +00001447 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00001448#endif
cristybb503372010-05-27 20:51:26 +00001449 for (y=0; y < (ssize_t) height; y++)
cristy3ed852e2009-09-05 21:47:34 +00001450 {
cristy4c08aed2011-07-01 19:47:50 +00001451 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001452 pixel,
1453 source,
1454 destination;
1455
1456 MagickRealType
1457 area,
1458 displacement;
1459
cristy4c08aed2011-07-01 19:47:50 +00001460 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001461 *restrict p,
1462 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001463
cristyd40deb62011-03-09 00:52:27 +00001464 register ssize_t
1465 i;
1466
cristy3ed852e2009-09-05 21:47:34 +00001467 ShearDirection
1468 direction;
1469
cristyd40deb62011-03-09 00:52:27 +00001470 ssize_t
1471 step;
1472
cristy3ed852e2009-09-05 21:47:34 +00001473 if (status == MagickFalse)
1474 continue;
1475 p=GetCacheViewAuthenticPixels(image_view,0,y_offset+y,image->columns,1,
1476 exception);
cristy4c08aed2011-07-01 19:47:50 +00001477 if (p == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001478 {
1479 status=MagickFalse;
1480 continue;
1481 }
cristyed231572011-07-14 02:18:59 +00001482 p+=x_offset*GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001483 displacement=degrees*(MagickRealType) (y-height/2.0);
1484 if (displacement == 0.0)
1485 continue;
1486 if (displacement > 0.0)
1487 direction=RIGHT;
1488 else
1489 {
1490 displacement*=(-1.0);
1491 direction=LEFT;
1492 }
cristybb503372010-05-27 20:51:26 +00001493 step=(ssize_t) floor((double) displacement);
cristy3ed852e2009-09-05 21:47:34 +00001494 area=(MagickRealType) (displacement-step);
1495 step++;
1496 pixel=background;
cristy4c08aed2011-07-01 19:47:50 +00001497 GetPixelInfo(image,&source);
1498 GetPixelInfo(image,&destination);
cristy3ed852e2009-09-05 21:47:34 +00001499 switch (direction)
1500 {
1501 case LEFT:
1502 {
1503 /*
1504 Transfer pixels left-to-right.
1505 */
1506 if (step > x_offset)
1507 break;
cristyed231572011-07-14 02:18:59 +00001508 q=p-step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001509 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001510 {
1511 if ((x_offset+i) < step)
1512 {
cristyed231572011-07-14 02:18:59 +00001513 p+=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001514 GetPixelInfoPixel(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001515 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001516 continue;
1517 }
cristy803640d2011-11-17 02:11:32 +00001518 GetPixelInfoPixel(image,p,&source);
cristy4c08aed2011-07-01 19:47:50 +00001519 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
cristyf53a3bb2011-10-09 02:13:12 +00001520 &source,(MagickRealType) GetPixelAlpha(image,p),area,&destination);
cristy803640d2011-11-17 02:11:32 +00001521 SetPixelInfoPixel(image,&destination,q);
1522 GetPixelInfoPixel(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001523 p+=GetPixelChannels(image);
1524 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001525 }
cristy4c08aed2011-07-01 19:47:50 +00001526 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1527 &background,(MagickRealType) background.alpha,area,&destination);
cristy803640d2011-11-17 02:11:32 +00001528 SetPixelInfoPixel(image,&destination,q);
cristyed231572011-07-14 02:18:59 +00001529 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001530 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001531 {
cristy803640d2011-11-17 02:11:32 +00001532 SetPixelInfoPixel(image,&background,q);
cristyed231572011-07-14 02:18:59 +00001533 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001534 }
cristy3ed852e2009-09-05 21:47:34 +00001535 break;
1536 }
1537 case RIGHT:
1538 {
1539 /*
1540 Transfer pixels right-to-left.
1541 */
cristyed231572011-07-14 02:18:59 +00001542 p+=width*GetPixelChannels(image);
1543 q=p+step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001544 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001545 {
cristyed231572011-07-14 02:18:59 +00001546 p-=GetPixelChannels(image);
1547 q-=GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001548 if ((size_t) (x_offset+width+step-i) >= image->columns)
cristy3ed852e2009-09-05 21:47:34 +00001549 continue;
cristy803640d2011-11-17 02:11:32 +00001550 GetPixelInfoPixel(image,p,&source);
cristy4c08aed2011-07-01 19:47:50 +00001551 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
cristyf53a3bb2011-10-09 02:13:12 +00001552 &source,(MagickRealType) GetPixelAlpha(image,p),area,&destination);
cristy803640d2011-11-17 02:11:32 +00001553 SetPixelInfoPixel(image,&destination,q);
1554 GetPixelInfoPixel(image,p,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001555 }
cristy4c08aed2011-07-01 19:47:50 +00001556 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1557 &background,(MagickRealType) background.alpha,area,&destination);
cristyed231572011-07-14 02:18:59 +00001558 q-=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001559 SetPixelInfoPixel(image,&destination,q);
cristy3ed852e2009-09-05 21:47:34 +00001560 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001561 {
cristyed231572011-07-14 02:18:59 +00001562 q-=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001563 SetPixelInfoPixel(image,&background,q);
cristy4c08aed2011-07-01 19:47:50 +00001564 }
cristy3ed852e2009-09-05 21:47:34 +00001565 break;
1566 }
1567 }
1568 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1569 status=MagickFalse;
1570 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1571 {
1572 MagickBooleanType
1573 proceed;
1574
cristyb5d5f722009-11-04 03:03:49 +00001575#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy7b650b52011-10-09 01:13:39 +00001576 #pragma omp critical (MagickCore_XShearImage)
cristy3ed852e2009-09-05 21:47:34 +00001577#endif
1578 proceed=SetImageProgress(image,XShearImageTag,progress++,height);
1579 if (proceed == MagickFalse)
1580 status=MagickFalse;
1581 }
1582 }
1583 image_view=DestroyCacheView(image_view);
1584 return(status);
1585}
1586
1587/*
1588%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1589% %
1590% %
1591% %
1592+ Y S h e a r I m a g e %
1593% %
1594% %
1595% %
1596%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1597%
1598% YShearImage shears the image in the Y direction with a shear angle of
1599% 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1600% negative angles shear clockwise. Angles are measured relative to a
1601% horizontal X-axis. Y shears will increase the height of an image creating
1602% 'empty' triangles on the top and bottom of the source image.
1603%
1604% The format of the YShearImage method is:
1605%
1606% MagickBooleanType YShearImage(Image *image,const MagickRealType degrees,
cristybb503372010-05-27 20:51:26 +00001607% const size_t width,const size_t height,
1608% const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001609%
1610% A description of each parameter follows.
1611%
1612% o image: the image.
1613%
cristycee97112010-05-28 00:44:52 +00001614% o degrees: A MagickRealType representing the shearing angle along the Y
cristy3ed852e2009-09-05 21:47:34 +00001615% axis.
1616%
1617% o width, height, x_offset, y_offset: Defines a region of the image
1618% to shear.
1619%
cristyecc2c142010-01-17 22:25:46 +00001620% o exception: return any errors or warnings in this structure.
1621%
cristy3ed852e2009-09-05 21:47:34 +00001622*/
1623static MagickBooleanType YShearImage(Image *image,const MagickRealType degrees,
cristybb503372010-05-27 20:51:26 +00001624 const size_t width,const size_t height,const ssize_t x_offset,
1625 const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001626{
1627#define YShearImageTag "YShear/Image"
1628
1629 typedef enum
1630 {
1631 UP,
1632 DOWN
1633 } ShearDirection;
1634
1635 CacheView
1636 *image_view;
1637
cristy3ed852e2009-09-05 21:47:34 +00001638 MagickBooleanType
1639 status;
1640
cristy5f959472010-05-27 22:19:46 +00001641 MagickOffsetType
1642 progress;
1643
cristy4c08aed2011-07-01 19:47:50 +00001644 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001645 background;
1646
cristy5f959472010-05-27 22:19:46 +00001647 ssize_t
1648 x;
1649
cristy9d8c8ce2011-10-25 16:13:52 +00001650 /*
1651 Y Shear image.
1652 */
cristy3ed852e2009-09-05 21:47:34 +00001653 assert(image != (Image *) NULL);
1654 assert(image->signature == MagickSignature);
1655 if (image->debug != MagickFalse)
1656 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3ed852e2009-09-05 21:47:34 +00001657 status=MagickTrue;
1658 progress=0;
cristy9d8c8ce2011-10-25 16:13:52 +00001659 background=image->background_color;
cristy3ed852e2009-09-05 21:47:34 +00001660 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00001661#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyd36a25e2012-01-18 14:30:53 +00001662 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00001663#endif
cristybb503372010-05-27 20:51:26 +00001664 for (x=0; x < (ssize_t) width; x++)
cristy3ed852e2009-09-05 21:47:34 +00001665 {
cristybb503372010-05-27 20:51:26 +00001666 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001667 step;
1668
cristy3ed852e2009-09-05 21:47:34 +00001669 MagickRealType
1670 area,
1671 displacement;
1672
cristy4c08aed2011-07-01 19:47:50 +00001673 PixelInfo
1674 pixel,
1675 source,
1676 destination;
1677
1678 register Quantum
1679 *restrict p,
1680 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001681
cristybb503372010-05-27 20:51:26 +00001682 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001683 i;
1684
cristy3ed852e2009-09-05 21:47:34 +00001685 ShearDirection
1686 direction;
1687
1688 if (status == MagickFalse)
1689 continue;
1690 p=GetCacheViewAuthenticPixels(image_view,x_offset+x,0,1,image->rows,
1691 exception);
cristy4c08aed2011-07-01 19:47:50 +00001692 if (p == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001693 {
1694 status=MagickFalse;
1695 continue;
1696 }
cristyed231572011-07-14 02:18:59 +00001697 p+=y_offset*GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001698 displacement=degrees*(MagickRealType) (x-width/2.0);
1699 if (displacement == 0.0)
1700 continue;
1701 if (displacement > 0.0)
1702 direction=DOWN;
1703 else
1704 {
1705 displacement*=(-1.0);
1706 direction=UP;
1707 }
cristybb503372010-05-27 20:51:26 +00001708 step=(ssize_t) floor((double) displacement);
cristy3ed852e2009-09-05 21:47:34 +00001709 area=(MagickRealType) (displacement-step);
1710 step++;
1711 pixel=background;
cristy4c08aed2011-07-01 19:47:50 +00001712 GetPixelInfo(image,&source);
1713 GetPixelInfo(image,&destination);
cristy3ed852e2009-09-05 21:47:34 +00001714 switch (direction)
1715 {
1716 case UP:
1717 {
1718 /*
1719 Transfer pixels top-to-bottom.
1720 */
1721 if (step > y_offset)
1722 break;
cristyed231572011-07-14 02:18:59 +00001723 q=p-step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001724 for (i=0; i < (ssize_t) height; i++)
cristy3ed852e2009-09-05 21:47:34 +00001725 {
1726 if ((y_offset+i) < step)
1727 {
cristyed231572011-07-14 02:18:59 +00001728 p+=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001729 GetPixelInfoPixel(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001730 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001731 continue;
1732 }
cristy803640d2011-11-17 02:11:32 +00001733 GetPixelInfoPixel(image,p,&source);
cristy4c08aed2011-07-01 19:47:50 +00001734 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1735 &source,(MagickRealType) GetPixelAlpha(image,p),area,
1736 &destination);
cristy803640d2011-11-17 02:11:32 +00001737 SetPixelInfoPixel(image,&destination,q);
1738 GetPixelInfoPixel(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001739 p+=GetPixelChannels(image);
1740 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001741 }
cristy4c08aed2011-07-01 19:47:50 +00001742 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1743 &background,(MagickRealType) background.alpha,area,&destination);
cristy803640d2011-11-17 02:11:32 +00001744 SetPixelInfoPixel(image,&destination,q);
cristyed231572011-07-14 02:18:59 +00001745 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001746 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001747 {
cristy803640d2011-11-17 02:11:32 +00001748 SetPixelInfoPixel(image,&background,q);
cristyed231572011-07-14 02:18:59 +00001749 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001750 }
cristy3ed852e2009-09-05 21:47:34 +00001751 break;
1752 }
1753 case DOWN:
1754 {
1755 /*
1756 Transfer pixels bottom-to-top.
1757 */
cristyed231572011-07-14 02:18:59 +00001758 p+=height*GetPixelChannels(image);
1759 q=p+step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001760 for (i=0; i < (ssize_t) height; i++)
cristy3ed852e2009-09-05 21:47:34 +00001761 {
cristyed231572011-07-14 02:18:59 +00001762 p-=GetPixelChannels(image);
1763 q-=GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001764 if ((size_t) (y_offset+height+step-i) >= image->rows)
cristy3ed852e2009-09-05 21:47:34 +00001765 continue;
cristy803640d2011-11-17 02:11:32 +00001766 GetPixelInfoPixel(image,p,&source);
cristy4c08aed2011-07-01 19:47:50 +00001767 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1768 &source,(MagickRealType) GetPixelAlpha(image,p),area,
1769 &destination);
cristy803640d2011-11-17 02:11:32 +00001770 SetPixelInfoPixel(image,&destination,q);
1771 GetPixelInfoPixel(image,p,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001772 }
cristy4c08aed2011-07-01 19:47:50 +00001773 CompositePixelInfoAreaBlend(&pixel,(MagickRealType) pixel.alpha,
1774 &background,(MagickRealType) background.alpha,area,&destination);
cristyed231572011-07-14 02:18:59 +00001775 q-=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001776 SetPixelInfoPixel(image,&destination,q);
cristy3ed852e2009-09-05 21:47:34 +00001777 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001778 {
cristyed231572011-07-14 02:18:59 +00001779 q-=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001780 SetPixelInfoPixel(image,&background,q);
cristy4c08aed2011-07-01 19:47:50 +00001781 }
cristy3ed852e2009-09-05 21:47:34 +00001782 break;
1783 }
1784 }
1785 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1786 status=MagickFalse;
1787 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1788 {
1789 MagickBooleanType
1790 proceed;
1791
cristyb5d5f722009-11-04 03:03:49 +00001792#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy7b650b52011-10-09 01:13:39 +00001793 #pragma omp critical (MagickCore_YShearImage)
cristy3ed852e2009-09-05 21:47:34 +00001794#endif
1795 proceed=SetImageProgress(image,YShearImageTag,progress++,image->rows);
1796 if (proceed == MagickFalse)
1797 status=MagickFalse;
1798 }
1799 }
1800 image_view=DestroyCacheView(image_view);
1801 return(status);
1802}
1803
1804/*
1805%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1806% %
1807% %
1808% %
cristy3ed852e2009-09-05 21:47:34 +00001809% S h e a r I m a g e %
1810% %
1811% %
1812% %
1813%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1814%
1815% ShearImage() creates a new image that is a shear_image copy of an existing
cristycee97112010-05-28 00:44:52 +00001816% one. Shearing slides one edge of an image along the X or Y axis, creating
1817% a parallelogram. An X direction shear slides an edge along the X axis,
1818% while a Y direction shear slides an edge along the Y axis. The amount of
cristy3ed852e2009-09-05 21:47:34 +00001819% the shear is controlled by a shear angle. For X direction shears, x_shear
1820% is measured relative to the Y axis, and similarly, for Y direction shears
1821% y_shear is measured relative to the X axis. Empty triangles left over from
1822% shearing the image are filled with the background color defined by member
1823% 'background_color' of the image.. ShearImage() allocates the memory
1824% necessary for the new Image structure and returns a pointer to the new image.
1825%
1826% ShearImage() is based on the paper "A Fast Algorithm for General Raster
1827% Rotatation" by Alan W. Paeth.
1828%
1829% The format of the ShearImage method is:
1830%
1831% Image *ShearImage(const Image *image,const double x_shear,
1832% const double y_shear,ExceptionInfo *exception)
1833%
1834% A description of each parameter follows.
1835%
1836% o image: the image.
1837%
1838% o x_shear, y_shear: Specifies the number of degrees to shear the image.
1839%
1840% o exception: return any errors or warnings in this structure.
1841%
1842*/
1843MagickExport Image *ShearImage(const Image *image,const double x_shear,
1844 const double y_shear,ExceptionInfo *exception)
1845{
1846 Image
1847 *integral_image,
1848 *shear_image;
1849
cristybb503372010-05-27 20:51:26 +00001850 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001851 x_offset,
1852 y_offset;
1853
cristyecc2c142010-01-17 22:25:46 +00001854 MagickBooleanType
1855 status;
1856
cristy3ed852e2009-09-05 21:47:34 +00001857 PointInfo
1858 shear;
1859
1860 RectangleInfo
1861 border_info;
1862
cristybb503372010-05-27 20:51:26 +00001863 size_t
cristy3ed852e2009-09-05 21:47:34 +00001864 y_width;
1865
1866 assert(image != (Image *) NULL);
1867 assert(image->signature == MagickSignature);
1868 if (image->debug != MagickFalse)
1869 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1870 assert(exception != (ExceptionInfo *) NULL);
1871 assert(exception->signature == MagickSignature);
1872 if ((x_shear != 0.0) && (fmod(x_shear,90.0) == 0.0))
1873 ThrowImageException(ImageError,"AngleIsDiscontinuous");
1874 if ((y_shear != 0.0) && (fmod(y_shear,90.0) == 0.0))
1875 ThrowImageException(ImageError,"AngleIsDiscontinuous");
1876 /*
1877 Initialize shear angle.
1878 */
1879 integral_image=CloneImage(image,0,0,MagickTrue,exception);
1880 if (integral_image == (Image *) NULL)
1881 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1882 shear.x=(-tan(DegreesToRadians(fmod(x_shear,360.0))));
1883 shear.y=tan(DegreesToRadians(fmod(y_shear,360.0)));
1884 if ((shear.x == 0.0) && (shear.y == 0.0))
1885 return(integral_image);
cristy574cc262011-08-05 01:23:58 +00001886 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001887 {
cristy3ed852e2009-09-05 21:47:34 +00001888 integral_image=DestroyImage(integral_image);
1889 return(integral_image);
1890 }
1891 if (integral_image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00001892 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00001893 /*
1894 Compute image size.
1895 */
cristybb503372010-05-27 20:51:26 +00001896 y_width=image->columns+(ssize_t) floor(fabs(shear.x)*image->rows+0.5);
cristycee97112010-05-28 00:44:52 +00001897 x_offset=(ssize_t) ceil((double) image->columns+((fabs(shear.x)*image->rows)-
cristy06609ee2010-03-17 20:21:27 +00001898 image->columns)/2.0-0.5);
cristycee97112010-05-28 00:44:52 +00001899 y_offset=(ssize_t) ceil((double) image->rows+((fabs(shear.y)*y_width)-
1900 image->rows)/2.0-0.5);
cristy3ed852e2009-09-05 21:47:34 +00001901 /*
1902 Surround image with border.
1903 */
1904 integral_image->border_color=integral_image->background_color;
1905 integral_image->compose=CopyCompositeOp;
cristybb503372010-05-27 20:51:26 +00001906 border_info.width=(size_t) x_offset;
1907 border_info.height=(size_t) y_offset;
cristy633f0c62011-09-15 13:27:36 +00001908 shear_image=BorderImage(integral_image,&border_info,image->compose,exception);
cristyecc2c142010-01-17 22:25:46 +00001909 integral_image=DestroyImage(integral_image);
cristy3ed852e2009-09-05 21:47:34 +00001910 if (shear_image == (Image *) NULL)
1911 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +00001912 /*
1913 Shear the image.
1914 */
1915 if (shear_image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00001916 (void) SetImageAlphaChannel(shear_image,OpaqueAlphaChannel,exception);
cristyecc2c142010-01-17 22:25:46 +00001917 status=XShearImage(shear_image,shear.x,image->columns,image->rows,x_offset,
cristyeaedf062010-05-29 22:36:02 +00001918 (ssize_t) (shear_image->rows-image->rows)/2,exception);
cristyecc2c142010-01-17 22:25:46 +00001919 if (status == MagickFalse)
1920 {
1921 shear_image=DestroyImage(shear_image);
1922 return((Image *) NULL);
1923 }
cristyeaedf062010-05-29 22:36:02 +00001924 status=YShearImage(shear_image,shear.y,y_width,image->rows,(ssize_t)
1925 (shear_image->columns-y_width)/2,y_offset,exception);
cristyecc2c142010-01-17 22:25:46 +00001926 if (status == MagickFalse)
1927 {
1928 shear_image=DestroyImage(shear_image);
1929 return((Image *) NULL);
1930 }
1931 status=CropToFitImage(&shear_image,shear.x,shear.y,(MagickRealType)
1932 image->columns,(MagickRealType) image->rows,MagickFalse,exception);
1933 if (status == MagickFalse)
1934 {
1935 shear_image=DestroyImage(shear_image);
1936 return((Image *) NULL);
1937 }
cristy3ed852e2009-09-05 21:47:34 +00001938 shear_image->compose=image->compose;
1939 shear_image->page.width=0;
1940 shear_image->page.height=0;
1941 return(shear_image);
1942}
cristyc7c81142011-11-07 12:14:23 +00001943
1944/*
1945%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1946% %
1947% %
1948% %
1949% S h e a r R o t a t e I m a g e %
1950% %
1951% %
1952% %
1953%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1954%
1955% ShearRotateImage() creates a new image that is a rotated copy of an existing
1956% one. Positive angles rotate counter-clockwise (right-hand rule), while
1957% negative angles rotate clockwise. Rotated images are usually larger than
1958% the originals and have 'empty' triangular corners. X axis. Empty
1959% triangles left over from shearing the image are filled with the background
1960% color defined by member 'background_color' of the image. ShearRotateImage
1961% allocates the memory necessary for the new Image structure and returns a
1962% pointer to the new image.
1963%
1964% ShearRotateImage() is based on the paper "A Fast Algorithm for General
1965% Raster Rotatation" by Alan W. Paeth. ShearRotateImage is adapted from a
1966% similar method based on the Paeth paper written by Michael Halle of the
1967% Spatial Imaging Group, MIT Media Lab.
1968%
1969% The format of the ShearRotateImage method is:
1970%
1971% Image *ShearRotateImage(const Image *image,const double degrees,
1972% ExceptionInfo *exception)
1973%
1974% A description of each parameter follows.
1975%
1976% o image: the image.
1977%
1978% o degrees: Specifies the number of degrees to rotate the image.
1979%
1980% o exception: return any errors or warnings in this structure.
1981%
1982*/
1983MagickExport Image *ShearRotateImage(const Image *image,const double degrees,
1984 ExceptionInfo *exception)
1985{
1986 Image
1987 *integral_image,
1988 *rotate_image;
1989
1990 MagickBooleanType
1991 status;
1992
1993 MagickRealType
1994 angle;
1995
1996 PointInfo
1997 shear;
1998
1999 RectangleInfo
2000 border_info;
2001
2002 size_t
2003 height,
2004 rotations,
2005 width,
2006 y_width;
2007
2008 ssize_t
2009 x_offset,
2010 y_offset;
2011
2012 /*
2013 Adjust rotation angle.
2014 */
2015 assert(image != (Image *) NULL);
2016 assert(image->signature == MagickSignature);
2017 if (image->debug != MagickFalse)
2018 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2019 assert(exception != (ExceptionInfo *) NULL);
2020 assert(exception->signature == MagickSignature);
2021 angle=degrees;
2022 while (angle < -45.0)
2023 angle+=360.0;
2024 for (rotations=0; angle > 45.0; rotations++)
2025 angle-=90.0;
2026 rotations%=4;
2027 /*
2028 Calculate shear equations.
2029 */
2030 integral_image=IntegralRotateImage(image,rotations,exception);
2031 if (integral_image == (Image *) NULL)
2032 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2033 shear.x=(-tan((double) DegreesToRadians(angle)/2.0));
2034 shear.y=sin((double) DegreesToRadians(angle));
2035 if ((shear.x == 0.0) && (shear.y == 0.0))
2036 return(integral_image);
2037 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
2038 {
2039 integral_image=DestroyImage(integral_image);
2040 return(integral_image);
2041 }
2042 if (integral_image->matte == MagickFalse)
2043 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
2044 /*
2045 Compute image size.
2046 */
2047 width=image->columns;
2048 height=image->rows;
2049 if ((rotations == 1) || (rotations == 3))
2050 {
2051 width=image->rows;
2052 height=image->columns;
2053 }
2054 y_width=width+(ssize_t) floor(fabs(shear.x)*height+0.5);
2055 x_offset=(ssize_t) ceil((double) width+((fabs(shear.y)*height)-width)/2.0-
2056 0.5);
2057 y_offset=(ssize_t) ceil((double) height+((fabs(shear.y)*y_width)-height)/2.0-
2058 0.5);
2059 /*
2060 Surround image with a border.
2061 */
2062 integral_image->border_color=integral_image->background_color;
2063 integral_image->compose=CopyCompositeOp;
2064 border_info.width=(size_t) x_offset;
2065 border_info.height=(size_t) y_offset;
2066 rotate_image=BorderImage(integral_image,&border_info,image->compose,
2067 exception);
2068 integral_image=DestroyImage(integral_image);
2069 if (rotate_image == (Image *) NULL)
2070 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
2071 /*
2072 Rotate the image.
2073 */
2074 status=XShearImage(rotate_image,shear.x,width,height,x_offset,(ssize_t)
2075 (rotate_image->rows-height)/2,exception);
2076 if (status == MagickFalse)
2077 {
2078 rotate_image=DestroyImage(rotate_image);
2079 return((Image *) NULL);
2080 }
2081 status=YShearImage(rotate_image,shear.y,y_width,height,(ssize_t)
2082 (rotate_image->columns-y_width)/2,y_offset,exception);
2083 if (status == MagickFalse)
2084 {
2085 rotate_image=DestroyImage(rotate_image);
2086 return((Image *) NULL);
2087 }
2088 status=XShearImage(rotate_image,shear.x,y_width,rotate_image->rows,(ssize_t)
2089 (rotate_image->columns-y_width)/2,0,exception);
2090 if (status == MagickFalse)
2091 {
2092 rotate_image=DestroyImage(rotate_image);
2093 return((Image *) NULL);
2094 }
2095 status=CropToFitImage(&rotate_image,shear.x,shear.y,(MagickRealType) width,
2096 (MagickRealType) height,MagickTrue,exception);
2097 if (status == MagickFalse)
2098 {
2099 rotate_image=DestroyImage(rotate_image);
2100 return((Image *) NULL);
2101 }
2102 rotate_image->compose=image->compose;
2103 rotate_image->page.width=0;
2104 rotate_image->page.height=0;
2105 return(rotate_image);
2106}