blob: 387b740ff2cef2bbcdb5483db5b1aa6d22bcfcaf [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 %
cristyde984cd2013-12-01 14:49:27 +000016% Cristy %
cristy3ed852e2009-09-05 21:47:34 +000017% July 1992 %
18% %
19% %
cristyfe676ee2013-11-18 13:03:38 +000020% Copyright 1999-2014 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"
cristy6a2180c2013-05-27 10:28:36 +000052#include "MagickCore/channel.h"
cristy4c08aed2011-07-01 19:47:50 +000053#include "MagickCore/color-private.h"
54#include "MagickCore/colorspace-private.h"
55#include "MagickCore/composite.h"
56#include "MagickCore/composite-private.h"
57#include "MagickCore/decorate.h"
58#include "MagickCore/distort.h"
59#include "MagickCore/draw.h"
60#include "MagickCore/exception.h"
61#include "MagickCore/exception-private.h"
62#include "MagickCore/gem.h"
63#include "MagickCore/geometry.h"
64#include "MagickCore/image.h"
65#include "MagickCore/image-private.h"
cristy106efa12014-03-09 01:06:05 +000066#include "MagickCore/matrix.h"
cristy4c08aed2011-07-01 19:47:50 +000067#include "MagickCore/memory_.h"
68#include "MagickCore/list.h"
69#include "MagickCore/monitor.h"
70#include "MagickCore/monitor-private.h"
cristy2c5fc272012-02-22 01:27:46 +000071#include "MagickCore/nt-base-private.h"
cristy4c08aed2011-07-01 19:47:50 +000072#include "MagickCore/pixel-accessor.h"
73#include "MagickCore/quantum.h"
74#include "MagickCore/resource_.h"
75#include "MagickCore/shear.h"
76#include "MagickCore/statistic.h"
77#include "MagickCore/string_.h"
78#include "MagickCore/string-private.h"
79#include "MagickCore/thread-private.h"
80#include "MagickCore/threshold.h"
81#include "MagickCore/transform.h"
cristy3ed852e2009-09-05 21:47:34 +000082
83/*
84%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
85% %
86% %
87% %
cristy3ed852e2009-09-05 21:47:34 +000088+ C r o p T o F i t I m a g e %
89% %
90% %
91% %
92%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
93%
94% CropToFitImage() crops the sheared image as determined by the bounding box
95% as defined by width and height and shearing angles.
96%
97% The format of the CropToFitImage method is:
98%
cristyecc2c142010-01-17 22:25:46 +000099% MagickBooleanType CropToFitImage(Image **image,
cristya19f1d72012-08-07 18:24:38 +0000100% const double x_shear,const double x_shear,
101% const double width,const double height,
cristyecc2c142010-01-17 22:25:46 +0000102% const MagickBooleanType rotate,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000103%
104% A description of each parameter follows.
105%
106% o image: the image.
107%
108% o x_shear, y_shear, width, height: Defines a region of the image to crop.
109%
110% o exception: return any errors or warnings in this structure.
111%
112*/
cristyecc2c142010-01-17 22:25:46 +0000113static MagickBooleanType CropToFitImage(Image **image,
cristya19f1d72012-08-07 18:24:38 +0000114 const double x_shear,const double y_shear,
115 const double width,const double height,
cristyecc2c142010-01-17 22:25:46 +0000116 const MagickBooleanType rotate,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000117{
118 Image
119 *crop_image;
120
121 PointInfo
122 extent[4],
123 min,
124 max;
125
126 RectangleInfo
127 geometry,
128 page;
129
cristybb503372010-05-27 20:51:26 +0000130 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000131 i;
132
133 /*
134 Calculate the rotated image size.
135 */
136 extent[0].x=(double) (-width/2.0);
137 extent[0].y=(double) (-height/2.0);
138 extent[1].x=(double) width/2.0;
139 extent[1].y=(double) (-height/2.0);
140 extent[2].x=(double) (-width/2.0);
141 extent[2].y=(double) height/2.0;
142 extent[3].x=(double) width/2.0;
143 extent[3].y=(double) height/2.0;
144 for (i=0; i < 4; i++)
145 {
146 extent[i].x+=x_shear*extent[i].y;
147 extent[i].y+=y_shear*extent[i].x;
148 if (rotate != MagickFalse)
149 extent[i].x+=x_shear*extent[i].y;
150 extent[i].x+=(double) (*image)->columns/2.0;
151 extent[i].y+=(double) (*image)->rows/2.0;
152 }
153 min=extent[0];
154 max=extent[0];
155 for (i=1; i < 4; i++)
156 {
157 if (min.x > extent[i].x)
158 min.x=extent[i].x;
159 if (min.y > extent[i].y)
160 min.y=extent[i].y;
161 if (max.x < extent[i].x)
162 max.x=extent[i].x;
163 if (max.y < extent[i].y)
164 max.y=extent[i].y;
165 }
cristybb503372010-05-27 20:51:26 +0000166 geometry.x=(ssize_t) ceil(min.x-0.5);
167 geometry.y=(ssize_t) ceil(min.y-0.5);
168 geometry.width=(size_t) floor(max.x-min.x+0.5);
169 geometry.height=(size_t) floor(max.y-min.y+0.5);
cristy3ed852e2009-09-05 21:47:34 +0000170 page=(*image)->page;
171 (void) ParseAbsoluteGeometry("0x0+0+0",&(*image)->page);
172 crop_image=CropImage(*image,&geometry,exception);
cristyecc2c142010-01-17 22:25:46 +0000173 if (crop_image == (Image *) NULL)
174 return(MagickFalse);
175 crop_image->page=page;
176 *image=DestroyImage(*image);
177 *image=crop_image;
178 return(MagickTrue);
cristy3ed852e2009-09-05 21:47:34 +0000179}
180
181/*
182%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
183% %
184% %
185% %
186% D e s k e w I m a g e %
187% %
188% %
189% %
190%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
191%
192% DeskewImage() removes skew from the image. Skew is an artifact that
193% occurs in scanned images because of the camera being misaligned,
194% imperfections in the scanning or surface, or simply because the paper was
195% not placed completely flat when scanned.
196%
anthony8a95e802013-04-10 05:31:57 +0000197% The result will be auto-croped if the artifact "deskew:auto-crop" is
198% defined, while the amount the image is to be deskewed, in degrees is also
199% saved as the artifact "deskew:angle".
200%
anthonye92872a2013-04-30 01:49:12 +0000201% If the artifact "deskew:auto-crop" is given the image will be automatically
202% cropped of the excess background. The value is the border width of all
203% pixels around the edge that will be used to determine an average border
204% color for the automatic trim.
anthony8a95e802013-04-10 05:31:57 +0000205%
cristy3ed852e2009-09-05 21:47:34 +0000206% The format of the DeskewImage method is:
207%
208% Image *DeskewImage(const Image *image,const double threshold,
209% ExceptionInfo *exception)
210%
211% A description of each parameter follows:
212%
213% o image: the image.
214%
215% o threshold: separate background from foreground.
216%
217% o exception: return any errors or warnings in this structure.
218%
219*/
220
cristy7207d422014-03-09 13:56:58 +0000221static void RadonProjection(const Image *image,MatrixInfo *source_matrixs,
222 MatrixInfo *destination_matrixs,const ssize_t sign,size_t *projection)
cristy3ed852e2009-09-05 21:47:34 +0000223{
cristy106efa12014-03-09 01:06:05 +0000224 MatrixInfo
cristy3ed852e2009-09-05 21:47:34 +0000225 *swap;
226
cristy106efa12014-03-09 01:06:05 +0000227 register MatrixInfo
cristy3ed852e2009-09-05 21:47:34 +0000228 *p,
229 *q;
230
cristy106efa12014-03-09 01:06:05 +0000231 register ssize_t
232 x;
233
cristybb503372010-05-27 20:51:26 +0000234 size_t
cristy3ed852e2009-09-05 21:47:34 +0000235 step;
236
cristy7207d422014-03-09 13:56:58 +0000237 p=source_matrixs;
238 q=destination_matrixs;
cristy106efa12014-03-09 01:06:05 +0000239 for (step=1; step < GetMatrixColumns(p); step*=2)
cristy3ed852e2009-09-05 21:47:34 +0000240 {
cristy106efa12014-03-09 01:06:05 +0000241 for (x=0; x < (ssize_t) GetMatrixColumns(p); x+=2*(ssize_t) step)
cristy3ed852e2009-09-05 21:47:34 +0000242 {
cristybb503372010-05-27 20:51:26 +0000243 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000244 i;
245
cristyd40deb62011-03-09 00:52:27 +0000246 ssize_t
247 y;
248
cristy3ed852e2009-09-05 21:47:34 +0000249 unsigned short
cristy7207d422014-03-09 13:56:58 +0000250 element,
cristy106efa12014-03-09 01:06:05 +0000251 neighbor;
cristy3ed852e2009-09-05 21:47:34 +0000252
cristybb503372010-05-27 20:51:26 +0000253 for (i=0; i < (ssize_t) step; i++)
cristy3ed852e2009-09-05 21:47:34 +0000254 {
cristy106efa12014-03-09 01:06:05 +0000255 for (y=0; y < (ssize_t) (GetMatrixRows(p)-i-1); y++)
cristy3ed852e2009-09-05 21:47:34 +0000256 {
cristy7207d422014-03-09 13:56:58 +0000257 if (GetMatrixElement(p,x+i,y,&element) == MagickFalse)
cristy106efa12014-03-09 01:06:05 +0000258 continue;
259 if (GetMatrixElement(p,x+i+step,y+i,&neighbor) == MagickFalse)
260 continue;
cristy7207d422014-03-09 13:56:58 +0000261 neighbor+=element;
cristy106efa12014-03-09 01:06:05 +0000262 if (SetMatrixElement(q,x+2*i,y,&neighbor) == MagickFalse)
263 continue;
264 if (GetMatrixElement(p,x+i+step,y+i+1,&neighbor) == MagickFalse)
265 continue;
cristy7207d422014-03-09 13:56:58 +0000266 neighbor+=element;
cristy106efa12014-03-09 01:06:05 +0000267 if (SetMatrixElement(q,x+2*i+1,y,&neighbor) == MagickFalse)
268 continue;
cristy3ed852e2009-09-05 21:47:34 +0000269 }
cristy106efa12014-03-09 01:06:05 +0000270 for ( ; y < (ssize_t) (GetMatrixRows(p)-i); y++)
cristy3ed852e2009-09-05 21:47:34 +0000271 {
cristy7207d422014-03-09 13:56:58 +0000272 if (GetMatrixElement(p,x+i,y,&element) == MagickFalse)
cristy106efa12014-03-09 01:06:05 +0000273 continue;
274 if (GetMatrixElement(p,x+i+step,y+i,&neighbor) == MagickFalse)
275 continue;
cristy7207d422014-03-09 13:56:58 +0000276 neighbor+=element;
cristy106efa12014-03-09 01:06:05 +0000277 if (SetMatrixElement(q,x+2*i,y,&neighbor) == MagickFalse)
278 continue;
cristy7207d422014-03-09 13:56:58 +0000279 if (SetMatrixElement(q,x+2*i+1,y,&element) == MagickFalse)
cristy106efa12014-03-09 01:06:05 +0000280 continue;
cristy3ed852e2009-09-05 21:47:34 +0000281 }
cristy106efa12014-03-09 01:06:05 +0000282 for ( ; y < (ssize_t) GetMatrixRows(p); y++)
cristy3ed852e2009-09-05 21:47:34 +0000283 {
cristy7207d422014-03-09 13:56:58 +0000284 if (GetMatrixElement(p,x+i,y,&element) == MagickFalse)
cristy106efa12014-03-09 01:06:05 +0000285 continue;
cristy7207d422014-03-09 13:56:58 +0000286 if (SetMatrixElement(q,x+2*i,y,&element) == MagickFalse)
cristy106efa12014-03-09 01:06:05 +0000287 continue;
cristy7207d422014-03-09 13:56:58 +0000288 if (SetMatrixElement(q,x+2*i+1,y,&element) == MagickFalse)
cristy106efa12014-03-09 01:06:05 +0000289 continue;
cristy3ed852e2009-09-05 21:47:34 +0000290 }
291 }
292 }
293 swap=p;
294 p=q;
295 q=swap;
296 }
cristyb5d5f722009-11-04 03:03:49 +0000297#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000298 #pragma omp parallel for schedule(static,4) \
cristycb7dfcc2013-01-06 18:34:59 +0000299 magick_threads(image,image,1,1)
cristy3ed852e2009-09-05 21:47:34 +0000300#endif
cristy106efa12014-03-09 01:06:05 +0000301 for (x=0; x < (ssize_t) GetMatrixColumns(p); x++)
cristy3ed852e2009-09-05 21:47:34 +0000302 {
cristybb503372010-05-27 20:51:26 +0000303 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000304 y;
305
cristybb503372010-05-27 20:51:26 +0000306 size_t
cristy3ed852e2009-09-05 21:47:34 +0000307 sum;
308
309 sum=0;
cristy106efa12014-03-09 01:06:05 +0000310 for (y=0; y < (ssize_t) (GetMatrixRows(p)-1); y++)
cristy3ed852e2009-09-05 21:47:34 +0000311 {
cristybb503372010-05-27 20:51:26 +0000312 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000313 delta;
314
cristy106efa12014-03-09 01:06:05 +0000315 unsigned short
cristy7207d422014-03-09 13:56:58 +0000316 element,
cristy106efa12014-03-09 01:06:05 +0000317 neighbor;
318
cristy7207d422014-03-09 13:56:58 +0000319 if (GetMatrixElement(p,x,y,&element) == MagickFalse)
cristy106efa12014-03-09 01:06:05 +0000320 continue;
321 if (GetMatrixElement(p,x,y+1,&neighbor) == MagickFalse)
322 continue;
cristy7207d422014-03-09 13:56:58 +0000323 delta=(ssize_t) element-(ssize_t) neighbor;
cristy3ed852e2009-09-05 21:47:34 +0000324 sum+=delta*delta;
325 }
cristy106efa12014-03-09 01:06:05 +0000326 projection[GetMatrixColumns(p)+sign*x-1]=sum;
cristy3ed852e2009-09-05 21:47:34 +0000327 }
328}
329
330static MagickBooleanType RadonTransform(const Image *image,
cristybb503372010-05-27 20:51:26 +0000331 const double threshold,size_t *projection,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000332{
333 CacheView
334 *image_view;
335
cristy106efa12014-03-09 01:06:05 +0000336 MatrixInfo
cristy7207d422014-03-09 13:56:58 +0000337 *destination_matrixs,
338 *source_matrixs;
cristy3ed852e2009-09-05 21:47:34 +0000339
cristy106efa12014-03-09 01:06:05 +0000340 MagickBooleanType
341 status;
342
cristybb503372010-05-27 20:51:26 +0000343 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000344 i;
345
cristybb503372010-05-27 20:51:26 +0000346 size_t
cristy3ed852e2009-09-05 21:47:34 +0000347 count,
348 width;
349
cristyd40deb62011-03-09 00:52:27 +0000350 ssize_t
351 y;
352
353 unsigned char
354 byte;
355
cristy3ed852e2009-09-05 21:47:34 +0000356 unsigned short
357 bits[256];
358
359 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
cristy7207d422014-03-09 13:56:58 +0000360 source_matrixs=AcquireMatrixInfo(width,image->rows,sizeof(unsigned short),
cristy106efa12014-03-09 01:06:05 +0000361 exception);
cristy7207d422014-03-09 13:56:58 +0000362 destination_matrixs=AcquireMatrixInfo(width,image->rows,sizeof(unsigned short),
cristy106efa12014-03-09 01:06:05 +0000363 exception);
cristy7207d422014-03-09 13:56:58 +0000364 if ((source_matrixs == (MatrixInfo *) NULL) ||
365 (destination_matrixs == (MatrixInfo *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000366 {
cristy7207d422014-03-09 13:56:58 +0000367 if (destination_matrixs != (MatrixInfo *) NULL)
368 destination_matrixs=DestroyMatrixInfo(destination_matrixs);
369 if (source_matrixs != (MatrixInfo *) NULL)
370 source_matrixs=DestroyMatrixInfo(source_matrixs);
cristy3ed852e2009-09-05 21:47:34 +0000371 return(MagickFalse);
372 }
cristy7207d422014-03-09 13:56:58 +0000373 if (ResetMatrixInfo(source_matrixs) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000374 {
cristy7207d422014-03-09 13:56:58 +0000375 destination_matrixs=DestroyMatrixInfo(destination_matrixs);
376 source_matrixs=DestroyMatrixInfo(source_matrixs);
cristy3ed852e2009-09-05 21:47:34 +0000377 return(MagickFalse);
378 }
379 for (i=0; i < 256; i++)
380 {
381 byte=(unsigned char) i;
382 for (count=0; byte != 0; byte>>=1)
383 count+=byte & 0x01;
384 bits[i]=(unsigned short) count;
385 }
386 status=MagickTrue;
cristy46ff2672012-12-14 15:32:26 +0000387 image_view=AcquireVirtualCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +0000388#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000389 #pragma omp parallel for schedule(static,4) shared(status) \
cristycb7dfcc2013-01-06 18:34:59 +0000390 magick_threads(image,image,1,1)
cristy3ed852e2009-09-05 21:47:34 +0000391#endif
cristybb503372010-05-27 20:51:26 +0000392 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000393 {
cristy4c08aed2011-07-01 19:47:50 +0000394 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000395 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000396
cristybb503372010-05-27 20:51:26 +0000397 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000398 i,
399 x;
400
cristybb503372010-05-27 20:51:26 +0000401 size_t
cristy3ed852e2009-09-05 21:47:34 +0000402 bit,
403 byte;
404
cristy106efa12014-03-09 01:06:05 +0000405 unsigned short
406 value;
407
408 if (status == MagickFalse)
409 continue;
410 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
411 if (p == (const Quantum *) NULL)
412 {
413 status=MagickFalse;
414 continue;
415 }
416 bit=0;
417 byte=0;
418 i=(ssize_t) (image->columns+7)/8;
419 for (x=0; x < (ssize_t) image->columns; x++)
420 {
421 byte<<=1;
422 if (((MagickRealType) GetPixelRed(image,p) < threshold) ||
423 ((MagickRealType) GetPixelGreen(image,p) < threshold) ||
424 ((MagickRealType) GetPixelBlue(image,p) < threshold))
425 byte|=0x01;
426 bit++;
427 if (bit == 8)
428 {
429 value=bits[byte];
cristy7207d422014-03-09 13:56:58 +0000430 (void) SetMatrixElement(source_matrixs,--i,y,&value);
cristy106efa12014-03-09 01:06:05 +0000431 bit=0;
432 byte=0;
433 }
434 p+=GetPixelChannels(image);
435 }
436 if (bit != 0)
437 {
438 byte<<=(8-bit);
439 value=bits[byte];
cristy7207d422014-03-09 13:56:58 +0000440 (void) SetMatrixElement(source_matrixs,--i,y,&value);
cristy106efa12014-03-09 01:06:05 +0000441 }
442 }
cristy7207d422014-03-09 13:56:58 +0000443 RadonProjection(image,source_matrixs,destination_matrixs,-1,projection);
444 (void) ResetMatrixInfo(source_matrixs);
cristy106efa12014-03-09 01:06:05 +0000445#if defined(MAGICKCORE_OPENMP_SUPPORT)
446 #pragma omp parallel for schedule(static,4) shared(status) \
447 magick_threads(image,image,image->rows,1)
448#endif
449 for (y=0; y < (ssize_t) image->rows; y++)
450 {
451 register const Quantum
452 *restrict p;
453
454 register ssize_t
455 i,
456 x;
457
458 size_t
459 bit,
460 byte;
461
462 unsigned short
463 value;
464
cristy3ed852e2009-09-05 21:47:34 +0000465 if (status == MagickFalse)
466 continue;
467 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000468 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000469 {
470 status=MagickFalse;
471 continue;
472 }
473 bit=0;
474 byte=0;
475 i=0;
cristybb503372010-05-27 20:51:26 +0000476 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000477 {
478 byte<<=1;
cristy106efa12014-03-09 01:06:05 +0000479 if (((MagickRealType) GetPixelRed(image,p) < threshold) ||
480 ((MagickRealType) GetPixelGreen(image,p) < threshold) ||
481 ((MagickRealType) GetPixelBlue(image,p) < threshold))
cristy3ed852e2009-09-05 21:47:34 +0000482 byte|=0x01;
483 bit++;
484 if (bit == 8)
485 {
cristy106efa12014-03-09 01:06:05 +0000486 value=bits[byte];
cristy7207d422014-03-09 13:56:58 +0000487 (void) SetMatrixElement(source_matrixs,i++,y,&value);
cristy3ed852e2009-09-05 21:47:34 +0000488 bit=0;
489 byte=0;
490 }
cristyed231572011-07-14 02:18:59 +0000491 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000492 }
493 if (bit != 0)
494 {
495 byte<<=(8-bit);
cristy106efa12014-03-09 01:06:05 +0000496 value=bits[byte];
cristy7207d422014-03-09 13:56:58 +0000497 (void) SetMatrixElement(source_matrixs,i++,y,&value);
cristy3ed852e2009-09-05 21:47:34 +0000498 }
499 }
cristy7207d422014-03-09 13:56:58 +0000500 RadonProjection(image,source_matrixs,destination_matrixs,1,projection);
cristy3ed852e2009-09-05 21:47:34 +0000501 image_view=DestroyCacheView(image_view);
cristy7207d422014-03-09 13:56:58 +0000502 destination_matrixs=DestroyMatrixInfo(destination_matrixs);
503 source_matrixs=DestroyMatrixInfo(source_matrixs);
cristy3ed852e2009-09-05 21:47:34 +0000504 return(MagickTrue);
505}
506
cristybb503372010-05-27 20:51:26 +0000507static void GetImageBackgroundColor(Image *image,const ssize_t offset,
cristy3ed852e2009-09-05 21:47:34 +0000508 ExceptionInfo *exception)
509{
510 CacheView
511 *image_view;
512
cristy4c08aed2011-07-01 19:47:50 +0000513 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000514 background;
515
cristya19f1d72012-08-07 18:24:38 +0000516 double
cristy3ed852e2009-09-05 21:47:34 +0000517 count;
518
cristyd40deb62011-03-09 00:52:27 +0000519 ssize_t
520 y;
521
cristy3ed852e2009-09-05 21:47:34 +0000522 /*
523 Compute average background color.
524 */
525 if (offset <= 0)
526 return;
cristy4c08aed2011-07-01 19:47:50 +0000527 GetPixelInfo(image,&background);
cristy3ed852e2009-09-05 21:47:34 +0000528 count=0.0;
cristy46ff2672012-12-14 15:32:26 +0000529 image_view=AcquireVirtualCacheView(image,exception);
cristybb503372010-05-27 20:51:26 +0000530 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000531 {
cristy4c08aed2011-07-01 19:47:50 +0000532 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000533 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000534
cristybb503372010-05-27 20:51:26 +0000535 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000536 x;
537
cristybb503372010-05-27 20:51:26 +0000538 if ((y >= offset) && (y < ((ssize_t) image->rows-offset)))
cristy3ed852e2009-09-05 21:47:34 +0000539 continue;
540 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000541 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000542 continue;
cristybb503372010-05-27 20:51:26 +0000543 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000544 {
cristybb503372010-05-27 20:51:26 +0000545 if ((x >= offset) && (x < ((ssize_t) image->columns-offset)))
cristy3ed852e2009-09-05 21:47:34 +0000546 continue;
cristy4c08aed2011-07-01 19:47:50 +0000547 background.red+=QuantumScale*GetPixelRed(image,p);
548 background.green+=QuantumScale*GetPixelGreen(image,p);
549 background.blue+=QuantumScale*GetPixelBlue(image,p);
cristy26295322011-09-22 00:50:36 +0000550 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
551 background.alpha+=QuantumScale*GetPixelAlpha(image,p);
cristy3ed852e2009-09-05 21:47:34 +0000552 count++;
cristyed231572011-07-14 02:18:59 +0000553 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000554 }
555 }
556 image_view=DestroyCacheView(image_view);
cristy8cd03c32012-07-07 18:57:59 +0000557 image->background_color.red=(double) ClampToQuantum(QuantumRange*
558 background.red/count);
559 image->background_color.green=(double) ClampToQuantum(QuantumRange*
560 background.green/count);
561 image->background_color.blue=(double) ClampToQuantum(QuantumRange*
562 background.blue/count);
cristy26295322011-09-22 00:50:36 +0000563 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy8cd03c32012-07-07 18:57:59 +0000564 image->background_color.alpha=(double) ClampToQuantum(QuantumRange*
565 background.alpha/count);
cristy3ed852e2009-09-05 21:47:34 +0000566}
567
568MagickExport Image *DeskewImage(const Image *image,const double threshold,
569 ExceptionInfo *exception)
570{
571 AffineMatrix
572 affine_matrix;
573
574 const char
575 *artifact;
576
577 double
578 degrees;
579
580 Image
581 *clone_image,
582 *crop_image,
583 *deskew_image,
584 *median_image;
585
cristy3ed852e2009-09-05 21:47:34 +0000586 MagickBooleanType
587 status;
588
589 RectangleInfo
590 geometry;
591
cristybb503372010-05-27 20:51:26 +0000592 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000593 i;
594
cristybb503372010-05-27 20:51:26 +0000595 size_t
cristy3ed852e2009-09-05 21:47:34 +0000596 max_projection,
597 *projection,
598 width;
599
cristyd40deb62011-03-09 00:52:27 +0000600 ssize_t
601 skew;
602
cristy3ed852e2009-09-05 21:47:34 +0000603 /*
604 Compute deskew angle.
605 */
606 for (width=1; width < ((image->columns+7)/8); width<<=1) ;
cristybb503372010-05-27 20:51:26 +0000607 projection=(size_t *) AcquireQuantumMemory((size_t) (2*width-1),
cristy3ed852e2009-09-05 21:47:34 +0000608 sizeof(*projection));
cristybb503372010-05-27 20:51:26 +0000609 if (projection == (size_t *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000610 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
611 status=RadonTransform(image,threshold,projection,exception);
612 if (status == MagickFalse)
613 {
cristybb503372010-05-27 20:51:26 +0000614 projection=(size_t *) RelinquishMagickMemory(projection);
cristy3ed852e2009-09-05 21:47:34 +0000615 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
616 }
617 max_projection=0;
618 skew=0;
cristybb503372010-05-27 20:51:26 +0000619 for (i=0; i < (ssize_t) (2*width-1); i++)
cristy3ed852e2009-09-05 21:47:34 +0000620 {
621 if (projection[i] > max_projection)
622 {
cristybb503372010-05-27 20:51:26 +0000623 skew=i-(ssize_t) width+1;
cristy3ed852e2009-09-05 21:47:34 +0000624 max_projection=projection[i];
625 }
626 }
cristybb503372010-05-27 20:51:26 +0000627 projection=(size_t *) RelinquishMagickMemory(projection);
anthony553bfcc2013-04-10 01:27:43 +0000628 degrees=RadiansToDegrees(-atan((double) skew/width/8));
629 if (image->debug != MagickFalse)
630 (void) LogMagickEvent(TransformEvent,GetMagickModule(),
631 " Deskew angle: %g",degrees);
cristy3ed852e2009-09-05 21:47:34 +0000632 /*
633 Deskew image.
634 */
635 clone_image=CloneImage(image,0,0,MagickTrue,exception);
636 if (clone_image == (Image *) NULL)
637 return((Image *) NULL);
anthony8a95e802013-04-10 05:31:57 +0000638 {
639 char
640 angle[MaxTextExtent];
641
642 (void) FormatLocaleString(angle,MaxTextExtent,"%.20g",degrees);
643 (void) SetImageArtifact(clone_image,"deskew:angle",angle);
644 }
cristy387430f2012-02-07 13:09:46 +0000645 (void) SetImageVirtualPixelMethod(clone_image,BackgroundVirtualPixelMethod,
646 exception);
cristy3ed852e2009-09-05 21:47:34 +0000647 affine_matrix.sx=cos(DegreesToRadians(fmod((double) degrees,360.0)));
648 affine_matrix.rx=sin(DegreesToRadians(fmod((double) degrees,360.0)));
649 affine_matrix.ry=(-sin(DegreesToRadians(fmod((double) degrees,360.0))));
650 affine_matrix.sy=cos(DegreesToRadians(fmod((double) degrees,360.0)));
651 affine_matrix.tx=0.0;
652 affine_matrix.ty=0.0;
653 artifact=GetImageArtifact(image,"deskew:auto-crop");
654 if (artifact == (const char *) NULL)
655 {
656 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
657 clone_image=DestroyImage(clone_image);
658 return(deskew_image);
659 }
660 /*
661 Auto-crop image.
662 */
cristyba978e12010-09-12 20:26:50 +0000663 GetImageBackgroundColor(clone_image,(ssize_t) StringToLong(artifact),
664 exception);
cristy3ed852e2009-09-05 21:47:34 +0000665 deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception);
666 clone_image=DestroyImage(clone_image);
667 if (deskew_image == (Image *) NULL)
668 return((Image *) NULL);
cristy95c38342011-03-18 22:39:51 +0000669 median_image=StatisticImage(deskew_image,MedianStatistic,3,3,exception);
cristy3ed852e2009-09-05 21:47:34 +0000670 if (median_image == (Image *) NULL)
671 {
672 deskew_image=DestroyImage(deskew_image);
673 return((Image *) NULL);
674 }
675 geometry=GetImageBoundingBox(median_image,exception);
676 median_image=DestroyImage(median_image);
677 if (image->debug != MagickFalse)
678 (void) LogMagickEvent(TransformEvent,GetMagickModule()," Deskew geometry: "
cristy6d8abba2010-06-03 01:10:47 +0000679 "%.20gx%.20g%+.20g%+.20g",(double) geometry.width,(double)
cristye8c25f92010-06-03 00:53:06 +0000680 geometry.height,(double) geometry.x,(double) geometry.y);
cristy3ed852e2009-09-05 21:47:34 +0000681 crop_image=CropImage(deskew_image,&geometry,exception);
682 deskew_image=DestroyImage(deskew_image);
683 return(crop_image);
684}
685
686/*
687%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
688% %
689% %
690% %
cristy987feef2011-11-17 12:24:56 +0000691% 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 +0000692% %
693% %
694% %
695%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
696%
cristy3dfa16c2010-05-17 19:34:48 +0000697% IntegralRotateImage() rotates the image an integral of 90 degrees. It
cristy3ed852e2009-09-05 21:47:34 +0000698% allocates the memory necessary for the new Image structure and returns a
699% pointer to the rotated image.
700%
701% The format of the IntegralRotateImage method is:
702%
cristybb503372010-05-27 20:51:26 +0000703% Image *IntegralRotateImage(const Image *image,size_t rotations,
cristy3ed852e2009-09-05 21:47:34 +0000704% ExceptionInfo *exception)
705%
706% A description of each parameter follows.
707%
708% o image: the image.
709%
710% o rotations: Specifies the number of 90 degree rotations.
711%
712*/
cristy987feef2011-11-17 12:24:56 +0000713MagickExport Image *IntegralRotateImage(const Image *image,size_t rotations,
cristy3ed852e2009-09-05 21:47:34 +0000714 ExceptionInfo *exception)
715{
cristy3ed852e2009-09-05 21:47:34 +0000716#define RotateImageTag "Rotate/Image"
717
718 CacheView
719 *image_view,
720 *rotate_view;
721
722 Image
723 *rotate_image;
724
cristy3ed852e2009-09-05 21:47:34 +0000725 MagickBooleanType
726 status;
727
cristy5f959472010-05-27 22:19:46 +0000728 MagickOffsetType
729 progress;
730
cristy3ed852e2009-09-05 21:47:34 +0000731 RectangleInfo
732 page;
733
cristy5f959472010-05-27 22:19:46 +0000734 ssize_t
735 y;
736
cristy3ed852e2009-09-05 21:47:34 +0000737 /*
738 Initialize rotated image attributes.
739 */
740 assert(image != (Image *) NULL);
741 page=image->page;
742 rotations%=4;
cristyb32b90a2009-09-07 21:45:48 +0000743 if (rotations == 0)
744 return(CloneImage(image,0,0,MagickTrue,exception));
cristy3ed852e2009-09-05 21:47:34 +0000745 if ((rotations == 1) || (rotations == 3))
746 rotate_image=CloneImage(image,image->rows,image->columns,MagickTrue,
747 exception);
748 else
749 rotate_image=CloneImage(image,image->columns,image->rows,MagickTrue,
750 exception);
751 if (rotate_image == (Image *) NULL)
752 return((Image *) NULL);
753 /*
754 Integral rotate the image.
755 */
756 status=MagickTrue;
757 progress=0;
cristy46ff2672012-12-14 15:32:26 +0000758 image_view=AcquireVirtualCacheView(image,exception);
759 rotate_view=AcquireAuthenticCacheView(rotate_image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000760 switch (rotations)
761 {
762 case 0:
763 {
764 /*
765 Rotate 0 degrees.
766 */
cristy3ed852e2009-09-05 21:47:34 +0000767 break;
768 }
769 case 1:
770 {
cristybb503372010-05-27 20:51:26 +0000771 size_t
cristyb32b90a2009-09-07 21:45:48 +0000772 tile_height,
773 tile_width;
774
cristyd40deb62011-03-09 00:52:27 +0000775 ssize_t
776 tile_y;
777
cristy3ed852e2009-09-05 21:47:34 +0000778 /*
779 Rotate 90 degrees.
780 */
cristyb32b90a2009-09-07 21:45:48 +0000781 GetPixelCacheTileSize(image,&tile_width,&tile_height);
cristy8b8148b2012-10-07 00:41:15 +0000782 tile_width=image->columns;
cristy26b64912012-12-16 18:20:09 +0000783#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy64c10cd2013-01-06 18:40:43 +0000784 #pragma omp parallel for schedule(static,4) shared(status) \
cristy3e1fa372013-01-06 18:07:11 +0000785 magick_threads(image,image,1,1)
cristy9a5a52f2012-10-09 14:40:31 +0000786#endif
cristyad11aac2011-09-25 21:29:16 +0000787 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
cristy3ed852e2009-09-05 21:47:34 +0000788 {
cristybb503372010-05-27 20:51:26 +0000789 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000790 tile_x;
791
792 if (status == MagickFalse)
793 continue;
cristy26295322011-09-22 00:50:36 +0000794 tile_x=0;
795 for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
cristy3ed852e2009-09-05 21:47:34 +0000796 {
797 MagickBooleanType
798 sync;
799
cristy4c08aed2011-07-01 19:47:50 +0000800 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000801 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000802
cristy4c08aed2011-07-01 19:47:50 +0000803 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000804 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000805
cristya26211e2011-10-09 01:24:57 +0000806 register ssize_t
807 y;
808
cristybb503372010-05-27 20:51:26 +0000809 size_t
cristyb32b90a2009-09-07 21:45:48 +0000810 height,
811 width;
cristy3ed852e2009-09-05 21:47:34 +0000812
cristyb32b90a2009-09-07 21:45:48 +0000813 width=tile_width;
cristybb503372010-05-27 20:51:26 +0000814 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
cristya45d2ec2011-08-24 13:17:19 +0000815 width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
cristyb32b90a2009-09-07 21:45:48 +0000816 height=tile_height;
cristybb503372010-05-27 20:51:26 +0000817 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
cristya45d2ec2011-08-24 13:17:19 +0000818 height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
cristydaa97692009-09-13 02:10:35 +0000819 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
820 exception);
cristy4c08aed2011-07-01 19:47:50 +0000821 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000822 {
823 status=MagickFalse;
824 break;
825 }
cristybb503372010-05-27 20:51:26 +0000826 for (y=0; y < (ssize_t) width; y++)
cristy3ed852e2009-09-05 21:47:34 +0000827 {
cristy4c08aed2011-07-01 19:47:50 +0000828 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000829 *restrict tile_pixels;
cristy3ed852e2009-09-05 21:47:34 +0000830
cristybb503372010-05-27 20:51:26 +0000831 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000832 x;
833
cristy27709ef2011-09-18 02:53:53 +0000834 if (status == MagickFalse)
835 continue;
cristybb503372010-05-27 20:51:26 +0000836 q=QueueCacheViewAuthenticPixels(rotate_view,(ssize_t)
cristy92611572011-07-07 16:02:42 +0000837 (rotate_image->columns-(tile_y+height)),y+tile_x,height,1,
838 exception);
cristyacd2ed22011-08-30 01:44:23 +0000839 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000840 {
841 status=MagickFalse;
cristy27709ef2011-09-18 02:53:53 +0000842 continue;
cristy3ed852e2009-09-05 21:47:34 +0000843 }
cristyed231572011-07-14 02:18:59 +0000844 tile_pixels=p+((height-1)*width+y)*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +0000845 for (x=0; x < (ssize_t) height; x++)
cristy3ed852e2009-09-05 21:47:34 +0000846 {
cristya45d2ec2011-08-24 13:17:19 +0000847 register ssize_t
848 i;
849
cristy883fde12013-04-08 00:50:13 +0000850 if (GetPixelReadMask(image,tile_pixels) == 0)
cristy10a6c612012-01-29 21:41:05 +0000851 {
852 tile_pixels-=width*GetPixelChannels(image);
853 q+=GetPixelChannels(rotate_image);
854 continue;
855 }
cristya45d2ec2011-08-24 13:17:19 +0000856 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
857 {
cristy5a23c552013-02-13 14:34:28 +0000858 PixelChannel channel=GetPixelChannelChannel(image,i);
859 PixelTrait traits=GetPixelChannelTraits(image,channel);
860 PixelTrait rotate_traits=GetPixelChannelTraits(rotate_image,
861 channel);
cristy010d7d12011-08-31 01:02:48 +0000862 if ((traits == UndefinedPixelTrait) ||
863 (rotate_traits == UndefinedPixelTrait))
cristya45d2ec2011-08-24 13:17:19 +0000864 continue;
cristy0beccfa2011-09-25 20:47:53 +0000865 SetPixelChannel(rotate_image,channel,tile_pixels[i],q);
cristya45d2ec2011-08-24 13:17:19 +0000866 }
cristyed231572011-07-14 02:18:59 +0000867 tile_pixels-=width*GetPixelChannels(image);
868 q+=GetPixelChannels(rotate_image);
cristy3ed852e2009-09-05 21:47:34 +0000869 }
cristy3ed852e2009-09-05 21:47:34 +0000870 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
871 if (sync == MagickFalse)
872 status=MagickFalse;
873 }
874 }
875 if (image->progress_monitor != (MagickProgressMonitor) NULL)
876 {
877 MagickBooleanType
878 proceed;
879
cristy26b64912012-12-16 18:20:09 +0000880#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy9a5a52f2012-10-09 14:40:31 +0000881 #pragma omp critical (MagickCore_IntegralRotateImage)
882#endif
cristyb32b90a2009-09-07 21:45:48 +0000883 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
cristy3ed852e2009-09-05 21:47:34 +0000884 image->rows);
885 if (proceed == MagickFalse)
886 status=MagickFalse;
887 }
888 }
cristyecc2c142010-01-17 22:25:46 +0000889 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
890 image->rows-1,image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000891 Swap(page.width,page.height);
892 Swap(page.x,page.y);
893 if (page.width != 0)
cristybb503372010-05-27 20:51:26 +0000894 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
cristy3ed852e2009-09-05 21:47:34 +0000895 break;
896 }
897 case 2:
898 {
899 /*
900 Rotate 180 degrees.
901 */
cristy5f890612012-12-16 23:34:05 +0000902#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy64c10cd2013-01-06 18:40:43 +0000903 #pragma omp parallel for schedule(static,4) shared(status) \
cristyd6432472013-01-06 16:56:13 +0000904 magick_threads(image,image,1,1)
cristy5f890612012-12-16 23:34:05 +0000905#endif
cristybb503372010-05-27 20:51:26 +0000906 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000907 {
908 MagickBooleanType
909 sync;
910
cristy4c08aed2011-07-01 19:47:50 +0000911 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000912 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000913
cristy4c08aed2011-07-01 19:47:50 +0000914 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000915 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000916
cristy2be50602011-10-09 01:28:13 +0000917 register ssize_t
918 x;
919
cristy3ed852e2009-09-05 21:47:34 +0000920 if (status == MagickFalse)
921 continue;
cristya45d2ec2011-08-24 13:17:19 +0000922 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
923 q=QueueCacheViewAuthenticPixels(rotate_view,0,(ssize_t) (image->rows-y-
924 1),image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000925 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000926 {
927 status=MagickFalse;
928 continue;
929 }
cristyed231572011-07-14 02:18:59 +0000930 q+=GetPixelChannels(rotate_image)*image->columns;
cristybb503372010-05-27 20:51:26 +0000931 for (x=0; x < (ssize_t) image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +0000932 {
cristya45d2ec2011-08-24 13:17:19 +0000933 register ssize_t
934 i;
935
cristyed231572011-07-14 02:18:59 +0000936 q-=GetPixelChannels(rotate_image);
cristy883fde12013-04-08 00:50:13 +0000937 if (GetPixelReadMask(image,p) == 0)
cristy10a6c612012-01-29 21:41:05 +0000938 {
939 p+=GetPixelChannels(image);
940 continue;
941 }
cristya45d2ec2011-08-24 13:17:19 +0000942 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
943 {
cristy5a23c552013-02-13 14:34:28 +0000944 PixelChannel channel=GetPixelChannelChannel(image,i);
945 PixelTrait traits=GetPixelChannelTraits(image,channel);
946 PixelTrait rotate_traits=GetPixelChannelTraits(rotate_image,
947 channel);
cristy010d7d12011-08-31 01:02:48 +0000948 if ((traits == UndefinedPixelTrait) ||
949 (rotate_traits == UndefinedPixelTrait))
cristya45d2ec2011-08-24 13:17:19 +0000950 continue;
cristy0beccfa2011-09-25 20:47:53 +0000951 SetPixelChannel(rotate_image,channel,p[i],q);
cristya45d2ec2011-08-24 13:17:19 +0000952 }
cristyed231572011-07-14 02:18:59 +0000953 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +0000954 }
cristy3ed852e2009-09-05 21:47:34 +0000955 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
956 if (sync == MagickFalse)
957 status=MagickFalse;
958 if (image->progress_monitor != (MagickProgressMonitor) NULL)
959 {
960 MagickBooleanType
961 proceed;
962
cristy5f890612012-12-16 23:34:05 +0000963#if defined(MAGICKCORE_OPENMP_SUPPORT)
964 #pragma omp critical (MagickCore_IntegralRotateImage)
965#endif
cristy0729beb2011-09-25 23:29:32 +0000966 proceed=SetImageProgress(image,RotateImageTag,progress++,
cristy21e03412011-09-25 22:39:35 +0000967 image->rows);
968 if (proceed == MagickFalse)
969 status=MagickFalse;
970 }
971 }
972 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
973 image->rows-1,image->rows);
974 Swap(page.width,page.height);
975 Swap(page.x,page.y);
976 if (page.width != 0)
977 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
978 break;
979 }
cristy3ed852e2009-09-05 21:47:34 +0000980 case 3:
981 {
cristybb503372010-05-27 20:51:26 +0000982 size_t
cristyb32b90a2009-09-07 21:45:48 +0000983 tile_height,
984 tile_width;
985
cristyd40deb62011-03-09 00:52:27 +0000986 ssize_t
987 tile_y;
988
cristy3ed852e2009-09-05 21:47:34 +0000989 /*
990 Rotate 270 degrees.
991 */
cristyb32b90a2009-09-07 21:45:48 +0000992 GetPixelCacheTileSize(image,&tile_width,&tile_height);
cristy8b8148b2012-10-07 00:41:15 +0000993 tile_width=image->columns;
cristy26b64912012-12-16 18:20:09 +0000994#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy64c10cd2013-01-06 18:40:43 +0000995 #pragma omp parallel for schedule(static,4) shared(status) \
cristycb7dfcc2013-01-06 18:34:59 +0000996 magick_threads(image,image,1,1)
cristy9a5a52f2012-10-09 14:40:31 +0000997#endif
cristyad11aac2011-09-25 21:29:16 +0000998 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
cristy3ed852e2009-09-05 21:47:34 +0000999 {
cristybb503372010-05-27 20:51:26 +00001000 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001001 tile_x;
1002
1003 if (status == MagickFalse)
1004 continue;
cristy26295322011-09-22 00:50:36 +00001005 tile_x=0;
1006 for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
cristy3ed852e2009-09-05 21:47:34 +00001007 {
1008 MagickBooleanType
1009 sync;
1010
cristy4c08aed2011-07-01 19:47:50 +00001011 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001012 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001013
cristy4c08aed2011-07-01 19:47:50 +00001014 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001015 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001016
cristy2be50602011-10-09 01:28:13 +00001017 register ssize_t
1018 y;
1019
cristybb503372010-05-27 20:51:26 +00001020 size_t
cristyb32b90a2009-09-07 21:45:48 +00001021 height,
1022 width;
cristy3ed852e2009-09-05 21:47:34 +00001023
cristyb32b90a2009-09-07 21:45:48 +00001024 width=tile_width;
cristybb503372010-05-27 20:51:26 +00001025 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
cristya45d2ec2011-08-24 13:17:19 +00001026 width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
cristyb32b90a2009-09-07 21:45:48 +00001027 height=tile_height;
cristybb503372010-05-27 20:51:26 +00001028 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
cristya45d2ec2011-08-24 13:17:19 +00001029 height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
1030 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1031 exception);
cristy4c08aed2011-07-01 19:47:50 +00001032 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001033 {
1034 status=MagickFalse;
1035 break;
1036 }
cristybb503372010-05-27 20:51:26 +00001037 for (y=0; y < (ssize_t) width; y++)
cristy3ed852e2009-09-05 21:47:34 +00001038 {
cristy4c08aed2011-07-01 19:47:50 +00001039 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001040 *restrict tile_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001041
cristybb503372010-05-27 20:51:26 +00001042 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001043 x;
1044
cristy27709ef2011-09-18 02:53:53 +00001045 if (status == MagickFalse)
1046 continue;
cristya45d2ec2011-08-24 13:17:19 +00001047 q=QueueCacheViewAuthenticPixels(rotate_view,tile_y,(ssize_t) (y+
1048 rotate_image->rows-(tile_x+width)),height,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001049 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001050 {
1051 status=MagickFalse;
cristy27709ef2011-09-18 02:53:53 +00001052 continue;
cristy3ed852e2009-09-05 21:47:34 +00001053 }
cristyed231572011-07-14 02:18:59 +00001054 tile_pixels=p+((width-1)-y)*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001055 for (x=0; x < (ssize_t) height; x++)
cristy3ed852e2009-09-05 21:47:34 +00001056 {
cristya45d2ec2011-08-24 13:17:19 +00001057 register ssize_t
1058 i;
1059
cristy883fde12013-04-08 00:50:13 +00001060 if (GetPixelReadMask(image,tile_pixels) == 0)
cristy10a6c612012-01-29 21:41:05 +00001061 {
1062 tile_pixels+=width*GetPixelChannels(image);
1063 q+=GetPixelChannels(rotate_image);
1064 continue;
1065 }
cristya45d2ec2011-08-24 13:17:19 +00001066 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1067 {
cristy5a23c552013-02-13 14:34:28 +00001068 PixelChannel channel=GetPixelChannelChannel(image,i);
1069 PixelTrait traits=GetPixelChannelTraits(image,channel);
1070 PixelTrait rotate_traits=GetPixelChannelTraits(rotate_image,
1071 channel);
cristy010d7d12011-08-31 01:02:48 +00001072 if ((traits == UndefinedPixelTrait) ||
1073 (rotate_traits == UndefinedPixelTrait))
cristya45d2ec2011-08-24 13:17:19 +00001074 continue;
cristy0beccfa2011-09-25 20:47:53 +00001075 SetPixelChannel(rotate_image,channel,tile_pixels[i],q);
cristya45d2ec2011-08-24 13:17:19 +00001076 }
cristyed231572011-07-14 02:18:59 +00001077 tile_pixels+=width*GetPixelChannels(image);
1078 q+=GetPixelChannels(rotate_image);
cristy3ed852e2009-09-05 21:47:34 +00001079 }
cristy26b64912012-12-16 18:20:09 +00001080#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy9a5a52f2012-10-09 14:40:31 +00001081 #pragma omp critical (MagickCore_IntegralRotateImage)
1082#endif
cristy3ed852e2009-09-05 21:47:34 +00001083 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1084 if (sync == MagickFalse)
1085 status=MagickFalse;
1086 }
1087 }
1088 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1089 {
1090 MagickBooleanType
1091 proceed;
1092
cristy21e03412011-09-25 22:39:35 +00001093 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
1094 image->rows);
1095 if (proceed == MagickFalse)
1096 status=MagickFalse;
1097 }
1098 }
1099 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1100 image->rows-1,image->rows);
1101 Swap(page.width,page.height);
1102 Swap(page.x,page.y);
1103 if (page.width != 0)
1104 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1105 break;
1106 }
cristy3ed852e2009-09-05 21:47:34 +00001107 }
1108 rotate_view=DestroyCacheView(rotate_view);
1109 image_view=DestroyCacheView(image_view);
1110 rotate_image->type=image->type;
1111 rotate_image->page=page;
1112 if (status == MagickFalse)
1113 rotate_image=DestroyImage(rotate_image);
1114 return(rotate_image);
1115}
1116
1117/*
1118%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1119% %
1120% %
1121% %
1122+ X S h e a r I m a g e %
1123% %
1124% %
1125% %
1126%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1127%
1128% XShearImage() shears the image in the X direction with a shear angle of
1129% 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1130% negative angles shear clockwise. Angles are measured relative to a vertical
1131% Y-axis. X shears will widen an image creating 'empty' triangles on the left
1132% and right sides of the source image.
1133%
1134% The format of the XShearImage method is:
1135%
cristya19f1d72012-08-07 18:24:38 +00001136% MagickBooleanType XShearImage(Image *image,const double degrees,
cristybb503372010-05-27 20:51:26 +00001137% const size_t width,const size_t height,
1138% const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001139%
1140% A description of each parameter follows.
1141%
1142% o image: the image.
1143%
cristya19f1d72012-08-07 18:24:38 +00001144% o degrees: A double representing the shearing angle along the X
cristy3ed852e2009-09-05 21:47:34 +00001145% axis.
1146%
1147% o width, height, x_offset, y_offset: Defines a region of the image
1148% to shear.
1149%
cristyecc2c142010-01-17 22:25:46 +00001150% o exception: return any errors or warnings in this structure.
1151%
cristy3ed852e2009-09-05 21:47:34 +00001152*/
cristya19f1d72012-08-07 18:24:38 +00001153static MagickBooleanType XShearImage(Image *image,const double degrees,
cristybb503372010-05-27 20:51:26 +00001154 const size_t width,const size_t height,const ssize_t x_offset,
1155 const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001156{
1157#define XShearImageTag "XShear/Image"
1158
1159 typedef enum
1160 {
1161 LEFT,
1162 RIGHT
1163 } ShearDirection;
1164
1165 CacheView
1166 *image_view;
1167
cristy3ed852e2009-09-05 21:47:34 +00001168 MagickBooleanType
1169 status;
1170
cristy5f959472010-05-27 22:19:46 +00001171 MagickOffsetType
1172 progress;
1173
cristy4c08aed2011-07-01 19:47:50 +00001174 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001175 background;
1176
cristy5f959472010-05-27 22:19:46 +00001177 ssize_t
1178 y;
1179
cristy9d8c8ce2011-10-25 16:13:52 +00001180 /*
1181 X shear image.
1182 */
cristy3ed852e2009-09-05 21:47:34 +00001183 assert(image != (Image *) NULL);
1184 assert(image->signature == MagickSignature);
1185 if (image->debug != MagickFalse)
1186 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3ed852e2009-09-05 21:47:34 +00001187 status=MagickTrue;
cristy9d8c8ce2011-10-25 16:13:52 +00001188 background=image->background_color;
cristy3ed852e2009-09-05 21:47:34 +00001189 progress=0;
cristy46ff2672012-12-14 15:32:26 +00001190 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00001191#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00001192 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00001193 magick_threads(image,image,height,1)
cristy3ed852e2009-09-05 21:47:34 +00001194#endif
cristybb503372010-05-27 20:51:26 +00001195 for (y=0; y < (ssize_t) height; y++)
cristy3ed852e2009-09-05 21:47:34 +00001196 {
cristy4c08aed2011-07-01 19:47:50 +00001197 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001198 pixel,
1199 source,
1200 destination;
1201
cristya19f1d72012-08-07 18:24:38 +00001202 double
cristy3ed852e2009-09-05 21:47:34 +00001203 area,
1204 displacement;
1205
cristy4c08aed2011-07-01 19:47:50 +00001206 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001207 *restrict p,
1208 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001209
cristyd40deb62011-03-09 00:52:27 +00001210 register ssize_t
1211 i;
1212
cristy3ed852e2009-09-05 21:47:34 +00001213 ShearDirection
1214 direction;
1215
cristyd40deb62011-03-09 00:52:27 +00001216 ssize_t
1217 step;
1218
cristy3ed852e2009-09-05 21:47:34 +00001219 if (status == MagickFalse)
1220 continue;
1221 p=GetCacheViewAuthenticPixels(image_view,0,y_offset+y,image->columns,1,
1222 exception);
cristy4c08aed2011-07-01 19:47:50 +00001223 if (p == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001224 {
1225 status=MagickFalse;
1226 continue;
1227 }
cristyed231572011-07-14 02:18:59 +00001228 p+=x_offset*GetPixelChannels(image);
cristya19f1d72012-08-07 18:24:38 +00001229 displacement=degrees*(double) (y-height/2.0);
cristy3ed852e2009-09-05 21:47:34 +00001230 if (displacement == 0.0)
1231 continue;
1232 if (displacement > 0.0)
1233 direction=RIGHT;
1234 else
1235 {
1236 displacement*=(-1.0);
1237 direction=LEFT;
1238 }
cristybb503372010-05-27 20:51:26 +00001239 step=(ssize_t) floor((double) displacement);
cristya19f1d72012-08-07 18:24:38 +00001240 area=(double) (displacement-step);
cristy3ed852e2009-09-05 21:47:34 +00001241 step++;
1242 pixel=background;
cristy4c08aed2011-07-01 19:47:50 +00001243 GetPixelInfo(image,&source);
1244 GetPixelInfo(image,&destination);
cristy3ed852e2009-09-05 21:47:34 +00001245 switch (direction)
1246 {
1247 case LEFT:
1248 {
1249 /*
1250 Transfer pixels left-to-right.
1251 */
1252 if (step > x_offset)
1253 break;
cristyed231572011-07-14 02:18:59 +00001254 q=p-step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001255 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001256 {
1257 if ((x_offset+i) < step)
1258 {
cristyed231572011-07-14 02:18:59 +00001259 p+=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001260 GetPixelInfoPixel(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001261 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001262 continue;
1263 }
cristy803640d2011-11-17 02:11:32 +00001264 GetPixelInfoPixel(image,p,&source);
cristya19f1d72012-08-07 18:24:38 +00001265 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1266 &source,(double) GetPixelAlpha(image,p),area,&destination);
cristy803640d2011-11-17 02:11:32 +00001267 SetPixelInfoPixel(image,&destination,q);
1268 GetPixelInfoPixel(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001269 p+=GetPixelChannels(image);
1270 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001271 }
cristya19f1d72012-08-07 18:24:38 +00001272 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1273 &background,(double) background.alpha,area,&destination);
cristy803640d2011-11-17 02:11:32 +00001274 SetPixelInfoPixel(image,&destination,q);
cristyed231572011-07-14 02:18:59 +00001275 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001276 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001277 {
cristy803640d2011-11-17 02:11:32 +00001278 SetPixelInfoPixel(image,&background,q);
cristyed231572011-07-14 02:18:59 +00001279 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001280 }
cristy3ed852e2009-09-05 21:47:34 +00001281 break;
1282 }
1283 case RIGHT:
1284 {
1285 /*
1286 Transfer pixels right-to-left.
1287 */
cristyed231572011-07-14 02:18:59 +00001288 p+=width*GetPixelChannels(image);
1289 q=p+step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001290 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001291 {
cristyed231572011-07-14 02:18:59 +00001292 p-=GetPixelChannels(image);
1293 q-=GetPixelChannels(image);
cristy5285ef62014-02-26 13:55:31 +00001294 if ((size_t) (x_offset+width+step-i) > image->columns)
cristy3ed852e2009-09-05 21:47:34 +00001295 continue;
cristy803640d2011-11-17 02:11:32 +00001296 GetPixelInfoPixel(image,p,&source);
cristya19f1d72012-08-07 18:24:38 +00001297 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1298 &source,(double) GetPixelAlpha(image,p),area,&destination);
cristy803640d2011-11-17 02:11:32 +00001299 SetPixelInfoPixel(image,&destination,q);
1300 GetPixelInfoPixel(image,p,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001301 }
cristya19f1d72012-08-07 18:24:38 +00001302 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1303 &background,(double) background.alpha,area,&destination);
cristyed231572011-07-14 02:18:59 +00001304 q-=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001305 SetPixelInfoPixel(image,&destination,q);
cristy3ed852e2009-09-05 21:47:34 +00001306 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001307 {
cristyed231572011-07-14 02:18:59 +00001308 q-=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001309 SetPixelInfoPixel(image,&background,q);
cristy4c08aed2011-07-01 19:47:50 +00001310 }
cristy3ed852e2009-09-05 21:47:34 +00001311 break;
1312 }
1313 }
1314 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1315 status=MagickFalse;
1316 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1317 {
1318 MagickBooleanType
1319 proceed;
1320
cristyb5d5f722009-11-04 03:03:49 +00001321#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy7b650b52011-10-09 01:13:39 +00001322 #pragma omp critical (MagickCore_XShearImage)
cristy3ed852e2009-09-05 21:47:34 +00001323#endif
1324 proceed=SetImageProgress(image,XShearImageTag,progress++,height);
1325 if (proceed == MagickFalse)
1326 status=MagickFalse;
1327 }
1328 }
1329 image_view=DestroyCacheView(image_view);
1330 return(status);
1331}
1332
1333/*
1334%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1335% %
1336% %
1337% %
1338+ Y S h e a r I m a g e %
1339% %
1340% %
1341% %
1342%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1343%
1344% YShearImage shears the image in the Y direction with a shear angle of
1345% 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1346% negative angles shear clockwise. Angles are measured relative to a
1347% horizontal X-axis. Y shears will increase the height of an image creating
1348% 'empty' triangles on the top and bottom of the source image.
1349%
1350% The format of the YShearImage method is:
1351%
cristya19f1d72012-08-07 18:24:38 +00001352% MagickBooleanType YShearImage(Image *image,const double degrees,
cristybb503372010-05-27 20:51:26 +00001353% const size_t width,const size_t height,
1354% const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001355%
1356% A description of each parameter follows.
1357%
1358% o image: the image.
1359%
cristya19f1d72012-08-07 18:24:38 +00001360% o degrees: A double representing the shearing angle along the Y
cristy3ed852e2009-09-05 21:47:34 +00001361% axis.
1362%
1363% o width, height, x_offset, y_offset: Defines a region of the image
1364% to shear.
1365%
cristyecc2c142010-01-17 22:25:46 +00001366% o exception: return any errors or warnings in this structure.
1367%
cristy3ed852e2009-09-05 21:47:34 +00001368*/
cristya19f1d72012-08-07 18:24:38 +00001369static MagickBooleanType YShearImage(Image *image,const double degrees,
cristybb503372010-05-27 20:51:26 +00001370 const size_t width,const size_t height,const ssize_t x_offset,
1371 const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001372{
1373#define YShearImageTag "YShear/Image"
1374
1375 typedef enum
1376 {
1377 UP,
1378 DOWN
1379 } ShearDirection;
1380
1381 CacheView
1382 *image_view;
1383
cristy3ed852e2009-09-05 21:47:34 +00001384 MagickBooleanType
1385 status;
1386
cristy5f959472010-05-27 22:19:46 +00001387 MagickOffsetType
1388 progress;
1389
cristy4c08aed2011-07-01 19:47:50 +00001390 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001391 background;
1392
cristy5f959472010-05-27 22:19:46 +00001393 ssize_t
1394 x;
1395
cristy9d8c8ce2011-10-25 16:13:52 +00001396 /*
1397 Y Shear image.
1398 */
cristy3ed852e2009-09-05 21:47:34 +00001399 assert(image != (Image *) NULL);
1400 assert(image->signature == MagickSignature);
1401 if (image->debug != MagickFalse)
1402 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3ed852e2009-09-05 21:47:34 +00001403 status=MagickTrue;
1404 progress=0;
cristy9d8c8ce2011-10-25 16:13:52 +00001405 background=image->background_color;
cristy46ff2672012-12-14 15:32:26 +00001406 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00001407#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00001408 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00001409 magick_threads(image,image,width,1)
cristy3ed852e2009-09-05 21:47:34 +00001410#endif
cristybb503372010-05-27 20:51:26 +00001411 for (x=0; x < (ssize_t) width; x++)
cristy3ed852e2009-09-05 21:47:34 +00001412 {
cristybb503372010-05-27 20:51:26 +00001413 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001414 step;
1415
cristya19f1d72012-08-07 18:24:38 +00001416 double
cristy3ed852e2009-09-05 21:47:34 +00001417 area,
1418 displacement;
1419
cristy4c08aed2011-07-01 19:47:50 +00001420 PixelInfo
1421 pixel,
1422 source,
1423 destination;
1424
1425 register Quantum
1426 *restrict p,
1427 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001428
cristybb503372010-05-27 20:51:26 +00001429 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001430 i;
1431
cristy3ed852e2009-09-05 21:47:34 +00001432 ShearDirection
1433 direction;
1434
1435 if (status == MagickFalse)
1436 continue;
1437 p=GetCacheViewAuthenticPixels(image_view,x_offset+x,0,1,image->rows,
1438 exception);
cristy4c08aed2011-07-01 19:47:50 +00001439 if (p == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001440 {
1441 status=MagickFalse;
1442 continue;
1443 }
cristyed231572011-07-14 02:18:59 +00001444 p+=y_offset*GetPixelChannels(image);
cristya19f1d72012-08-07 18:24:38 +00001445 displacement=degrees*(double) (x-width/2.0);
cristy3ed852e2009-09-05 21:47:34 +00001446 if (displacement == 0.0)
1447 continue;
1448 if (displacement > 0.0)
1449 direction=DOWN;
1450 else
1451 {
1452 displacement*=(-1.0);
1453 direction=UP;
1454 }
cristybb503372010-05-27 20:51:26 +00001455 step=(ssize_t) floor((double) displacement);
cristya19f1d72012-08-07 18:24:38 +00001456 area=(double) (displacement-step);
cristy3ed852e2009-09-05 21:47:34 +00001457 step++;
1458 pixel=background;
cristy4c08aed2011-07-01 19:47:50 +00001459 GetPixelInfo(image,&source);
1460 GetPixelInfo(image,&destination);
cristy3ed852e2009-09-05 21:47:34 +00001461 switch (direction)
1462 {
1463 case UP:
1464 {
1465 /*
1466 Transfer pixels top-to-bottom.
1467 */
1468 if (step > y_offset)
1469 break;
cristyed231572011-07-14 02:18:59 +00001470 q=p-step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001471 for (i=0; i < (ssize_t) height; i++)
cristy3ed852e2009-09-05 21:47:34 +00001472 {
1473 if ((y_offset+i) < step)
1474 {
cristyed231572011-07-14 02:18:59 +00001475 p+=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001476 GetPixelInfoPixel(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001477 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001478 continue;
1479 }
cristy803640d2011-11-17 02:11:32 +00001480 GetPixelInfoPixel(image,p,&source);
cristya19f1d72012-08-07 18:24:38 +00001481 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1482 &source,(double) GetPixelAlpha(image,p),area,
cristy4c08aed2011-07-01 19:47:50 +00001483 &destination);
cristy803640d2011-11-17 02:11:32 +00001484 SetPixelInfoPixel(image,&destination,q);
1485 GetPixelInfoPixel(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001486 p+=GetPixelChannels(image);
1487 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001488 }
cristya19f1d72012-08-07 18:24:38 +00001489 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1490 &background,(double) background.alpha,area,&destination);
cristy803640d2011-11-17 02:11:32 +00001491 SetPixelInfoPixel(image,&destination,q);
cristyed231572011-07-14 02:18:59 +00001492 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001493 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001494 {
cristy803640d2011-11-17 02:11:32 +00001495 SetPixelInfoPixel(image,&background,q);
cristyed231572011-07-14 02:18:59 +00001496 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001497 }
cristy3ed852e2009-09-05 21:47:34 +00001498 break;
1499 }
1500 case DOWN:
1501 {
1502 /*
1503 Transfer pixels bottom-to-top.
1504 */
cristyed231572011-07-14 02:18:59 +00001505 p+=height*GetPixelChannels(image);
1506 q=p+step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001507 for (i=0; i < (ssize_t) height; i++)
cristy3ed852e2009-09-05 21:47:34 +00001508 {
cristyed231572011-07-14 02:18:59 +00001509 p-=GetPixelChannels(image);
1510 q-=GetPixelChannels(image);
cristy5285ef62014-02-26 13:55:31 +00001511 if ((size_t) (y_offset+height+step-i) > image->rows)
cristy3ed852e2009-09-05 21:47:34 +00001512 continue;
cristy803640d2011-11-17 02:11:32 +00001513 GetPixelInfoPixel(image,p,&source);
cristya19f1d72012-08-07 18:24:38 +00001514 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1515 &source,(double) GetPixelAlpha(image,p),area,
cristy4c08aed2011-07-01 19:47:50 +00001516 &destination);
cristy803640d2011-11-17 02:11:32 +00001517 SetPixelInfoPixel(image,&destination,q);
1518 GetPixelInfoPixel(image,p,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001519 }
cristya19f1d72012-08-07 18:24:38 +00001520 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1521 &background,(double) background.alpha,area,&destination);
cristyed231572011-07-14 02:18:59 +00001522 q-=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001523 SetPixelInfoPixel(image,&destination,q);
cristy3ed852e2009-09-05 21:47:34 +00001524 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001525 {
cristyed231572011-07-14 02:18:59 +00001526 q-=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001527 SetPixelInfoPixel(image,&background,q);
cristy4c08aed2011-07-01 19:47:50 +00001528 }
cristy3ed852e2009-09-05 21:47:34 +00001529 break;
1530 }
1531 }
1532 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1533 status=MagickFalse;
1534 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1535 {
1536 MagickBooleanType
1537 proceed;
1538
cristyb5d5f722009-11-04 03:03:49 +00001539#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy7b650b52011-10-09 01:13:39 +00001540 #pragma omp critical (MagickCore_YShearImage)
cristy3ed852e2009-09-05 21:47:34 +00001541#endif
1542 proceed=SetImageProgress(image,YShearImageTag,progress++,image->rows);
1543 if (proceed == MagickFalse)
1544 status=MagickFalse;
1545 }
1546 }
1547 image_view=DestroyCacheView(image_view);
1548 return(status);
1549}
1550
1551/*
1552%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1553% %
1554% %
1555% %
cristy3ed852e2009-09-05 21:47:34 +00001556% S h e a r I m a g e %
1557% %
1558% %
1559% %
1560%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1561%
1562% ShearImage() creates a new image that is a shear_image copy of an existing
cristycee97112010-05-28 00:44:52 +00001563% one. Shearing slides one edge of an image along the X or Y axis, creating
1564% a parallelogram. An X direction shear slides an edge along the X axis,
1565% while a Y direction shear slides an edge along the Y axis. The amount of
cristy3ed852e2009-09-05 21:47:34 +00001566% the shear is controlled by a shear angle. For X direction shears, x_shear
1567% is measured relative to the Y axis, and similarly, for Y direction shears
1568% y_shear is measured relative to the X axis. Empty triangles left over from
1569% shearing the image are filled with the background color defined by member
1570% 'background_color' of the image.. ShearImage() allocates the memory
1571% necessary for the new Image structure and returns a pointer to the new image.
1572%
1573% ShearImage() is based on the paper "A Fast Algorithm for General Raster
1574% Rotatation" by Alan W. Paeth.
1575%
1576% The format of the ShearImage method is:
1577%
1578% Image *ShearImage(const Image *image,const double x_shear,
1579% const double y_shear,ExceptionInfo *exception)
1580%
1581% A description of each parameter follows.
1582%
1583% o image: the image.
1584%
1585% o x_shear, y_shear: Specifies the number of degrees to shear the image.
1586%
1587% o exception: return any errors or warnings in this structure.
1588%
1589*/
1590MagickExport Image *ShearImage(const Image *image,const double x_shear,
1591 const double y_shear,ExceptionInfo *exception)
1592{
1593 Image
1594 *integral_image,
1595 *shear_image;
1596
cristyecc2c142010-01-17 22:25:46 +00001597 MagickBooleanType
1598 status;
1599
cristy3ed852e2009-09-05 21:47:34 +00001600 PointInfo
1601 shear;
1602
1603 RectangleInfo
cristy5285ef62014-02-26 13:55:31 +00001604 border_info,
1605 bounds;
cristy3ed852e2009-09-05 21:47:34 +00001606
1607 assert(image != (Image *) NULL);
1608 assert(image->signature == MagickSignature);
1609 if (image->debug != MagickFalse)
1610 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1611 assert(exception != (ExceptionInfo *) NULL);
1612 assert(exception->signature == MagickSignature);
1613 if ((x_shear != 0.0) && (fmod(x_shear,90.0) == 0.0))
1614 ThrowImageException(ImageError,"AngleIsDiscontinuous");
1615 if ((y_shear != 0.0) && (fmod(y_shear,90.0) == 0.0))
1616 ThrowImageException(ImageError,"AngleIsDiscontinuous");
1617 /*
1618 Initialize shear angle.
1619 */
1620 integral_image=CloneImage(image,0,0,MagickTrue,exception);
1621 if (integral_image == (Image *) NULL)
1622 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1623 shear.x=(-tan(DegreesToRadians(fmod(x_shear,360.0))));
1624 shear.y=tan(DegreesToRadians(fmod(y_shear,360.0)));
1625 if ((shear.x == 0.0) && (shear.y == 0.0))
1626 return(integral_image);
cristy574cc262011-08-05 01:23:58 +00001627 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001628 {
cristy3ed852e2009-09-05 21:47:34 +00001629 integral_image=DestroyImage(integral_image);
1630 return(integral_image);
1631 }
cristy8a46d822012-08-28 23:32:39 +00001632 if (integral_image->alpha_trait != BlendPixelTrait)
cristy63240882011-08-05 19:05:27 +00001633 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00001634 /*
1635 Compute image size.
1636 */
cristy5285ef62014-02-26 13:55:31 +00001637 bounds.width=image->columns+(ssize_t) floor(fabs(shear.x)*image->rows+0.5);
1638 bounds.x=(ssize_t) ceil((double) image->columns+((fabs(shear.x)*image->rows)-
cristy06609ee2010-03-17 20:21:27 +00001639 image->columns)/2.0-0.5);
cristy5285ef62014-02-26 13:55:31 +00001640 bounds.y=(ssize_t) ceil((double) image->rows+((fabs(shear.y)*bounds.width)-
cristycee97112010-05-28 00:44:52 +00001641 image->rows)/2.0-0.5);
cristy3ed852e2009-09-05 21:47:34 +00001642 /*
1643 Surround image with border.
1644 */
1645 integral_image->border_color=integral_image->background_color;
1646 integral_image->compose=CopyCompositeOp;
cristy5285ef62014-02-26 13:55:31 +00001647 border_info.width=(size_t) bounds.x;
1648 border_info.height=(size_t) bounds.y;
cristy633f0c62011-09-15 13:27:36 +00001649 shear_image=BorderImage(integral_image,&border_info,image->compose,exception);
cristyecc2c142010-01-17 22:25:46 +00001650 integral_image=DestroyImage(integral_image);
cristy3ed852e2009-09-05 21:47:34 +00001651 if (shear_image == (Image *) NULL)
1652 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +00001653 /*
1654 Shear the image.
1655 */
cristy8a46d822012-08-28 23:32:39 +00001656 if (shear_image->alpha_trait != BlendPixelTrait)
cristy63240882011-08-05 19:05:27 +00001657 (void) SetImageAlphaChannel(shear_image,OpaqueAlphaChannel,exception);
cristy5285ef62014-02-26 13:55:31 +00001658 status=XShearImage(shear_image,shear.x,image->columns,image->rows,bounds.x,
cristyeaedf062010-05-29 22:36:02 +00001659 (ssize_t) (shear_image->rows-image->rows)/2,exception);
cristyecc2c142010-01-17 22:25:46 +00001660 if (status == MagickFalse)
1661 {
1662 shear_image=DestroyImage(shear_image);
1663 return((Image *) NULL);
1664 }
cristy5285ef62014-02-26 13:55:31 +00001665 status=YShearImage(shear_image,shear.y,bounds.width,image->rows,(ssize_t)
1666 (shear_image->columns-bounds.width)/2,bounds.y,exception);
cristyecc2c142010-01-17 22:25:46 +00001667 if (status == MagickFalse)
1668 {
1669 shear_image=DestroyImage(shear_image);
1670 return((Image *) NULL);
1671 }
cristy5285ef62014-02-26 13:55:31 +00001672 status=CropToFitImage(&shear_image,shear.x,shear.y,(MagickRealType)
1673 image->columns,(MagickRealType) image->rows,MagickFalse,exception);
cristy3ed852e2009-09-05 21:47:34 +00001674 shear_image->compose=image->compose;
1675 shear_image->page.width=0;
1676 shear_image->page.height=0;
cristy1c2f48d2012-12-14 01:20:55 +00001677 if (status == MagickFalse)
1678 shear_image=DestroyImage(shear_image);
cristy3ed852e2009-09-05 21:47:34 +00001679 return(shear_image);
1680}
cristyc7c81142011-11-07 12:14:23 +00001681
1682/*
1683%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1684% %
1685% %
1686% %
1687% S h e a r R o t a t e I m a g e %
1688% %
1689% %
1690% %
1691%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1692%
1693% ShearRotateImage() creates a new image that is a rotated copy of an existing
1694% one. Positive angles rotate counter-clockwise (right-hand rule), while
1695% negative angles rotate clockwise. Rotated images are usually larger than
1696% the originals and have 'empty' triangular corners. X axis. Empty
1697% triangles left over from shearing the image are filled with the background
1698% color defined by member 'background_color' of the image. ShearRotateImage
1699% allocates the memory necessary for the new Image structure and returns a
1700% pointer to the new image.
1701%
1702% ShearRotateImage() is based on the paper "A Fast Algorithm for General
1703% Raster Rotatation" by Alan W. Paeth. ShearRotateImage is adapted from a
1704% similar method based on the Paeth paper written by Michael Halle of the
1705% Spatial Imaging Group, MIT Media Lab.
1706%
1707% The format of the ShearRotateImage method is:
1708%
1709% Image *ShearRotateImage(const Image *image,const double degrees,
1710% ExceptionInfo *exception)
1711%
1712% A description of each parameter follows.
1713%
1714% o image: the image.
1715%
1716% o degrees: Specifies the number of degrees to rotate the image.
1717%
1718% o exception: return any errors or warnings in this structure.
1719%
1720*/
1721MagickExport Image *ShearRotateImage(const Image *image,const double degrees,
1722 ExceptionInfo *exception)
1723{
1724 Image
1725 *integral_image,
1726 *rotate_image;
1727
1728 MagickBooleanType
1729 status;
1730
cristy5285ef62014-02-26 13:55:31 +00001731 MagickRealType
cristyc7c81142011-11-07 12:14:23 +00001732 angle;
1733
1734 PointInfo
1735 shear;
1736
1737 RectangleInfo
cristy5285ef62014-02-26 13:55:31 +00001738 border_info,
1739 bounds;
cristyc7c81142011-11-07 12:14:23 +00001740
1741 size_t
1742 height,
1743 rotations,
cristy5285ef62014-02-26 13:55:31 +00001744 shear_width,
1745 width;
cristyc7c81142011-11-07 12:14:23 +00001746
1747 /*
1748 Adjust rotation angle.
1749 */
1750 assert(image != (Image *) NULL);
1751 assert(image->signature == MagickSignature);
1752 if (image->debug != MagickFalse)
1753 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1754 assert(exception != (ExceptionInfo *) NULL);
1755 assert(exception->signature == MagickSignature);
1756 angle=degrees;
1757 while (angle < -45.0)
1758 angle+=360.0;
1759 for (rotations=0; angle > 45.0; rotations++)
1760 angle-=90.0;
1761 rotations%=4;
1762 /*
1763 Calculate shear equations.
1764 */
1765 integral_image=IntegralRotateImage(image,rotations,exception);
1766 if (integral_image == (Image *) NULL)
1767 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1768 shear.x=(-tan((double) DegreesToRadians(angle)/2.0));
1769 shear.y=sin((double) DegreesToRadians(angle));
1770 if ((shear.x == 0.0) && (shear.y == 0.0))
1771 return(integral_image);
1772 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
1773 {
1774 integral_image=DestroyImage(integral_image);
1775 return(integral_image);
1776 }
cristy8a46d822012-08-28 23:32:39 +00001777 if (integral_image->alpha_trait != BlendPixelTrait)
cristyc7c81142011-11-07 12:14:23 +00001778 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
1779 /*
cristy5285ef62014-02-26 13:55:31 +00001780 Compute maximum bounds for 3 shear operations.
cristyc7c81142011-11-07 12:14:23 +00001781 */
cristy5285ef62014-02-26 13:55:31 +00001782 width=integral_image->columns;
1783 height=integral_image->rows;
1784 bounds.width=(size_t) floor(fabs((double) height*shear.x)+width+0.5);
1785 bounds.height=(size_t) floor(fabs((double) bounds.width*shear.y)+height+0.5);
1786 shear_width=(size_t) floor(fabs((double) bounds.height*shear.x)+
1787 bounds.width+0.5);
1788 bounds.x=(ssize_t) floor((double) ((shear_width > bounds.width) ? width :
1789 bounds.width-shear_width+2)/2.0+0.5);
1790 bounds.y=(ssize_t) floor(((double) bounds.height-height+2)/2.0+0.5);
cristyc7c81142011-11-07 12:14:23 +00001791 /*
1792 Surround image with a border.
1793 */
1794 integral_image->border_color=integral_image->background_color;
1795 integral_image->compose=CopyCompositeOp;
cristy5285ef62014-02-26 13:55:31 +00001796 border_info.width=(size_t) bounds.x;
1797 border_info.height=(size_t) bounds.y;
cristyc7c81142011-11-07 12:14:23 +00001798 rotate_image=BorderImage(integral_image,&border_info,image->compose,
1799 exception);
1800 integral_image=DestroyImage(integral_image);
1801 if (rotate_image == (Image *) NULL)
1802 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1803 /*
1804 Rotate the image.
1805 */
cristy5285ef62014-02-26 13:55:31 +00001806 status=XShearImage(rotate_image,shear.x,width,height,bounds.x,(ssize_t)
cristyc7c81142011-11-07 12:14:23 +00001807 (rotate_image->rows-height)/2,exception);
1808 if (status == MagickFalse)
1809 {
1810 rotate_image=DestroyImage(rotate_image);
1811 return((Image *) NULL);
1812 }
cristy5285ef62014-02-26 13:55:31 +00001813 status=YShearImage(rotate_image,shear.y,bounds.width,height,(ssize_t)
1814 (rotate_image->columns-bounds.width)/2,bounds.y,exception);
cristyc7c81142011-11-07 12:14:23 +00001815 if (status == MagickFalse)
1816 {
1817 rotate_image=DestroyImage(rotate_image);
1818 return((Image *) NULL);
1819 }
cristy5285ef62014-02-26 13:55:31 +00001820 status=XShearImage(rotate_image,shear.x,bounds.width,bounds.height,(ssize_t)
1821 (rotate_image->columns-bounds.width)/2,(ssize_t) (rotate_image->rows-
1822 bounds.height)/2,exception);
cristyc7c81142011-11-07 12:14:23 +00001823 if (status == MagickFalse)
1824 {
1825 rotate_image=DestroyImage(rotate_image);
1826 return((Image *) NULL);
1827 }
cristy5285ef62014-02-26 13:55:31 +00001828 status=CropToFitImage(&rotate_image,shear.x,shear.y,(MagickRealType) width,
1829 (MagickRealType) height,MagickTrue,exception);
cristyc7c81142011-11-07 12:14:23 +00001830 rotate_image->compose=image->compose;
1831 rotate_image->page.width=0;
1832 rotate_image->page.height=0;
cristy1c2f48d2012-12-14 01:20:55 +00001833 if (status == MagickFalse)
1834 rotate_image=DestroyImage(rotate_image);
cristyc7c81142011-11-07 12:14:23 +00001835 return(rotate_image);
1836}