blob: 56e9ceaf9dd62e0f1f1ee1e1e871310ee56861a7 [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 }
cristyc1712b22014-03-09 14:54:05 +0000373 if (NullMatrix(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);
cristyc1712b22014-03-09 14:54:05 +0000444 (void) NullMatrix(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 {
cristy3ed852e2009-09-05 21:47:34 +0000762 case 1:
763 {
cristybb503372010-05-27 20:51:26 +0000764 size_t
cristyb32b90a2009-09-07 21:45:48 +0000765 tile_height,
766 tile_width;
767
cristyd40deb62011-03-09 00:52:27 +0000768 ssize_t
769 tile_y;
770
cristy3ed852e2009-09-05 21:47:34 +0000771 /*
772 Rotate 90 degrees.
773 */
cristyb32b90a2009-09-07 21:45:48 +0000774 GetPixelCacheTileSize(image,&tile_width,&tile_height);
cristy8b8148b2012-10-07 00:41:15 +0000775 tile_width=image->columns;
cristy26b64912012-12-16 18:20:09 +0000776#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy64c10cd2013-01-06 18:40:43 +0000777 #pragma omp parallel for schedule(static,4) shared(status) \
cristy3e1fa372013-01-06 18:07:11 +0000778 magick_threads(image,image,1,1)
cristy9a5a52f2012-10-09 14:40:31 +0000779#endif
cristyad11aac2011-09-25 21:29:16 +0000780 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
cristy3ed852e2009-09-05 21:47:34 +0000781 {
cristybb503372010-05-27 20:51:26 +0000782 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000783 tile_x;
784
785 if (status == MagickFalse)
786 continue;
cristy26295322011-09-22 00:50:36 +0000787 tile_x=0;
788 for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
cristy3ed852e2009-09-05 21:47:34 +0000789 {
790 MagickBooleanType
791 sync;
792
cristy4c08aed2011-07-01 19:47:50 +0000793 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000794 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000795
cristy4c08aed2011-07-01 19:47:50 +0000796 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000797 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000798
cristya26211e2011-10-09 01:24:57 +0000799 register ssize_t
800 y;
801
cristybb503372010-05-27 20:51:26 +0000802 size_t
cristyb32b90a2009-09-07 21:45:48 +0000803 height,
804 width;
cristy3ed852e2009-09-05 21:47:34 +0000805
cristyb32b90a2009-09-07 21:45:48 +0000806 width=tile_width;
cristybb503372010-05-27 20:51:26 +0000807 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
cristya45d2ec2011-08-24 13:17:19 +0000808 width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
cristyb32b90a2009-09-07 21:45:48 +0000809 height=tile_height;
cristybb503372010-05-27 20:51:26 +0000810 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
cristya45d2ec2011-08-24 13:17:19 +0000811 height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
cristydaa97692009-09-13 02:10:35 +0000812 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
813 exception);
cristy4c08aed2011-07-01 19:47:50 +0000814 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000815 {
816 status=MagickFalse;
817 break;
818 }
cristybb503372010-05-27 20:51:26 +0000819 for (y=0; y < (ssize_t) width; y++)
cristy3ed852e2009-09-05 21:47:34 +0000820 {
cristy4c08aed2011-07-01 19:47:50 +0000821 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000822 *restrict tile_pixels;
cristy3ed852e2009-09-05 21:47:34 +0000823
cristybb503372010-05-27 20:51:26 +0000824 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000825 x;
826
cristy27709ef2011-09-18 02:53:53 +0000827 if (status == MagickFalse)
828 continue;
cristybb503372010-05-27 20:51:26 +0000829 q=QueueCacheViewAuthenticPixels(rotate_view,(ssize_t)
cristy92611572011-07-07 16:02:42 +0000830 (rotate_image->columns-(tile_y+height)),y+tile_x,height,1,
831 exception);
cristyacd2ed22011-08-30 01:44:23 +0000832 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000833 {
834 status=MagickFalse;
cristy27709ef2011-09-18 02:53:53 +0000835 continue;
cristy3ed852e2009-09-05 21:47:34 +0000836 }
cristyed231572011-07-14 02:18:59 +0000837 tile_pixels=p+((height-1)*width+y)*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +0000838 for (x=0; x < (ssize_t) height; x++)
cristy3ed852e2009-09-05 21:47:34 +0000839 {
cristya45d2ec2011-08-24 13:17:19 +0000840 register ssize_t
841 i;
842
cristy883fde12013-04-08 00:50:13 +0000843 if (GetPixelReadMask(image,tile_pixels) == 0)
cristy10a6c612012-01-29 21:41:05 +0000844 {
845 tile_pixels-=width*GetPixelChannels(image);
846 q+=GetPixelChannels(rotate_image);
847 continue;
848 }
cristya45d2ec2011-08-24 13:17:19 +0000849 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
850 {
cristy5a23c552013-02-13 14:34:28 +0000851 PixelChannel channel=GetPixelChannelChannel(image,i);
852 PixelTrait traits=GetPixelChannelTraits(image,channel);
853 PixelTrait rotate_traits=GetPixelChannelTraits(rotate_image,
854 channel);
cristy010d7d12011-08-31 01:02:48 +0000855 if ((traits == UndefinedPixelTrait) ||
856 (rotate_traits == UndefinedPixelTrait))
cristya45d2ec2011-08-24 13:17:19 +0000857 continue;
cristy0beccfa2011-09-25 20:47:53 +0000858 SetPixelChannel(rotate_image,channel,tile_pixels[i],q);
cristya45d2ec2011-08-24 13:17:19 +0000859 }
cristyed231572011-07-14 02:18:59 +0000860 tile_pixels-=width*GetPixelChannels(image);
861 q+=GetPixelChannels(rotate_image);
cristy3ed852e2009-09-05 21:47:34 +0000862 }
cristy3ed852e2009-09-05 21:47:34 +0000863 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
864 if (sync == MagickFalse)
865 status=MagickFalse;
866 }
867 }
868 if (image->progress_monitor != (MagickProgressMonitor) NULL)
869 {
870 MagickBooleanType
871 proceed;
872
cristy26b64912012-12-16 18:20:09 +0000873#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy9a5a52f2012-10-09 14:40:31 +0000874 #pragma omp critical (MagickCore_IntegralRotateImage)
875#endif
cristyb32b90a2009-09-07 21:45:48 +0000876 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
cristy3ed852e2009-09-05 21:47:34 +0000877 image->rows);
878 if (proceed == MagickFalse)
879 status=MagickFalse;
880 }
881 }
cristyecc2c142010-01-17 22:25:46 +0000882 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
883 image->rows-1,image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000884 Swap(page.width,page.height);
885 Swap(page.x,page.y);
886 if (page.width != 0)
cristybb503372010-05-27 20:51:26 +0000887 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
cristy3ed852e2009-09-05 21:47:34 +0000888 break;
889 }
890 case 2:
891 {
892 /*
893 Rotate 180 degrees.
894 */
cristy5f890612012-12-16 23:34:05 +0000895#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy64c10cd2013-01-06 18:40:43 +0000896 #pragma omp parallel for schedule(static,4) shared(status) \
cristyd6432472013-01-06 16:56:13 +0000897 magick_threads(image,image,1,1)
cristy5f890612012-12-16 23:34:05 +0000898#endif
cristybb503372010-05-27 20:51:26 +0000899 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000900 {
901 MagickBooleanType
902 sync;
903
cristy4c08aed2011-07-01 19:47:50 +0000904 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000905 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000906
cristy4c08aed2011-07-01 19:47:50 +0000907 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000908 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000909
cristy2be50602011-10-09 01:28:13 +0000910 register ssize_t
911 x;
912
cristy3ed852e2009-09-05 21:47:34 +0000913 if (status == MagickFalse)
914 continue;
cristya45d2ec2011-08-24 13:17:19 +0000915 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
916 q=QueueCacheViewAuthenticPixels(rotate_view,0,(ssize_t) (image->rows-y-
917 1),image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000918 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000919 {
920 status=MagickFalse;
921 continue;
922 }
cristyed231572011-07-14 02:18:59 +0000923 q+=GetPixelChannels(rotate_image)*image->columns;
cristybb503372010-05-27 20:51:26 +0000924 for (x=0; x < (ssize_t) image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +0000925 {
cristya45d2ec2011-08-24 13:17:19 +0000926 register ssize_t
927 i;
928
cristyed231572011-07-14 02:18:59 +0000929 q-=GetPixelChannels(rotate_image);
cristy883fde12013-04-08 00:50:13 +0000930 if (GetPixelReadMask(image,p) == 0)
cristy10a6c612012-01-29 21:41:05 +0000931 {
932 p+=GetPixelChannels(image);
933 continue;
934 }
cristya45d2ec2011-08-24 13:17:19 +0000935 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
936 {
cristy5a23c552013-02-13 14:34:28 +0000937 PixelChannel channel=GetPixelChannelChannel(image,i);
938 PixelTrait traits=GetPixelChannelTraits(image,channel);
939 PixelTrait rotate_traits=GetPixelChannelTraits(rotate_image,
940 channel);
cristy010d7d12011-08-31 01:02:48 +0000941 if ((traits == UndefinedPixelTrait) ||
942 (rotate_traits == UndefinedPixelTrait))
cristya45d2ec2011-08-24 13:17:19 +0000943 continue;
cristy0beccfa2011-09-25 20:47:53 +0000944 SetPixelChannel(rotate_image,channel,p[i],q);
cristya45d2ec2011-08-24 13:17:19 +0000945 }
cristyed231572011-07-14 02:18:59 +0000946 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +0000947 }
cristy3ed852e2009-09-05 21:47:34 +0000948 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
949 if (sync == MagickFalse)
950 status=MagickFalse;
951 if (image->progress_monitor != (MagickProgressMonitor) NULL)
952 {
953 MagickBooleanType
954 proceed;
955
cristy5f890612012-12-16 23:34:05 +0000956#if defined(MAGICKCORE_OPENMP_SUPPORT)
957 #pragma omp critical (MagickCore_IntegralRotateImage)
958#endif
cristy0729beb2011-09-25 23:29:32 +0000959 proceed=SetImageProgress(image,RotateImageTag,progress++,
cristy21e03412011-09-25 22:39:35 +0000960 image->rows);
961 if (proceed == MagickFalse)
962 status=MagickFalse;
963 }
964 }
965 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
966 image->rows-1,image->rows);
967 Swap(page.width,page.height);
968 Swap(page.x,page.y);
969 if (page.width != 0)
970 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
971 break;
972 }
cristy3ed852e2009-09-05 21:47:34 +0000973 case 3:
974 {
cristybb503372010-05-27 20:51:26 +0000975 size_t
cristyb32b90a2009-09-07 21:45:48 +0000976 tile_height,
977 tile_width;
978
cristyd40deb62011-03-09 00:52:27 +0000979 ssize_t
980 tile_y;
981
cristy3ed852e2009-09-05 21:47:34 +0000982 /*
983 Rotate 270 degrees.
984 */
cristyb32b90a2009-09-07 21:45:48 +0000985 GetPixelCacheTileSize(image,&tile_width,&tile_height);
cristy8b8148b2012-10-07 00:41:15 +0000986 tile_width=image->columns;
cristy26b64912012-12-16 18:20:09 +0000987#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy64c10cd2013-01-06 18:40:43 +0000988 #pragma omp parallel for schedule(static,4) shared(status) \
cristycb7dfcc2013-01-06 18:34:59 +0000989 magick_threads(image,image,1,1)
cristy9a5a52f2012-10-09 14:40:31 +0000990#endif
cristyad11aac2011-09-25 21:29:16 +0000991 for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=(ssize_t) tile_height)
cristy3ed852e2009-09-05 21:47:34 +0000992 {
cristybb503372010-05-27 20:51:26 +0000993 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000994 tile_x;
995
996 if (status == MagickFalse)
997 continue;
cristy26295322011-09-22 00:50:36 +0000998 tile_x=0;
999 for ( ; tile_x < (ssize_t) image->columns; tile_x+=(ssize_t) tile_width)
cristy3ed852e2009-09-05 21:47:34 +00001000 {
1001 MagickBooleanType
1002 sync;
1003
cristy4c08aed2011-07-01 19:47:50 +00001004 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001005 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001006
cristy4c08aed2011-07-01 19:47:50 +00001007 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001008 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001009
cristy2be50602011-10-09 01:28:13 +00001010 register ssize_t
1011 y;
1012
cristybb503372010-05-27 20:51:26 +00001013 size_t
cristyb32b90a2009-09-07 21:45:48 +00001014 height,
1015 width;
cristy3ed852e2009-09-05 21:47:34 +00001016
cristyb32b90a2009-09-07 21:45:48 +00001017 width=tile_width;
cristybb503372010-05-27 20:51:26 +00001018 if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns)
cristya45d2ec2011-08-24 13:17:19 +00001019 width=(size_t) (tile_width-(tile_x+tile_width-image->columns));
cristyb32b90a2009-09-07 21:45:48 +00001020 height=tile_height;
cristybb503372010-05-27 20:51:26 +00001021 if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows)
cristya45d2ec2011-08-24 13:17:19 +00001022 height=(size_t) (tile_height-(tile_y+tile_height-image->rows));
1023 p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height,
1024 exception);
cristy4c08aed2011-07-01 19:47:50 +00001025 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001026 {
1027 status=MagickFalse;
1028 break;
1029 }
cristybb503372010-05-27 20:51:26 +00001030 for (y=0; y < (ssize_t) width; y++)
cristy3ed852e2009-09-05 21:47:34 +00001031 {
cristy4c08aed2011-07-01 19:47:50 +00001032 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00001033 *restrict tile_pixels;
cristy3ed852e2009-09-05 21:47:34 +00001034
cristybb503372010-05-27 20:51:26 +00001035 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001036 x;
1037
cristy27709ef2011-09-18 02:53:53 +00001038 if (status == MagickFalse)
1039 continue;
cristya45d2ec2011-08-24 13:17:19 +00001040 q=QueueCacheViewAuthenticPixels(rotate_view,tile_y,(ssize_t) (y+
1041 rotate_image->rows-(tile_x+width)),height,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001042 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001043 {
1044 status=MagickFalse;
cristy27709ef2011-09-18 02:53:53 +00001045 continue;
cristy3ed852e2009-09-05 21:47:34 +00001046 }
cristyed231572011-07-14 02:18:59 +00001047 tile_pixels=p+((width-1)-y)*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001048 for (x=0; x < (ssize_t) height; x++)
cristy3ed852e2009-09-05 21:47:34 +00001049 {
cristya45d2ec2011-08-24 13:17:19 +00001050 register ssize_t
1051 i;
1052
cristy883fde12013-04-08 00:50:13 +00001053 if (GetPixelReadMask(image,tile_pixels) == 0)
cristy10a6c612012-01-29 21:41:05 +00001054 {
1055 tile_pixels+=width*GetPixelChannels(image);
1056 q+=GetPixelChannels(rotate_image);
1057 continue;
1058 }
cristya45d2ec2011-08-24 13:17:19 +00001059 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1060 {
cristy5a23c552013-02-13 14:34:28 +00001061 PixelChannel channel=GetPixelChannelChannel(image,i);
1062 PixelTrait traits=GetPixelChannelTraits(image,channel);
1063 PixelTrait rotate_traits=GetPixelChannelTraits(rotate_image,
1064 channel);
cristy010d7d12011-08-31 01:02:48 +00001065 if ((traits == UndefinedPixelTrait) ||
1066 (rotate_traits == UndefinedPixelTrait))
cristya45d2ec2011-08-24 13:17:19 +00001067 continue;
cristy0beccfa2011-09-25 20:47:53 +00001068 SetPixelChannel(rotate_image,channel,tile_pixels[i],q);
cristya45d2ec2011-08-24 13:17:19 +00001069 }
cristyed231572011-07-14 02:18:59 +00001070 tile_pixels+=width*GetPixelChannels(image);
1071 q+=GetPixelChannels(rotate_image);
cristy3ed852e2009-09-05 21:47:34 +00001072 }
cristy26b64912012-12-16 18:20:09 +00001073#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy9a5a52f2012-10-09 14:40:31 +00001074 #pragma omp critical (MagickCore_IntegralRotateImage)
1075#endif
cristy3ed852e2009-09-05 21:47:34 +00001076 sync=SyncCacheViewAuthenticPixels(rotate_view,exception);
1077 if (sync == MagickFalse)
1078 status=MagickFalse;
1079 }
1080 }
1081 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1082 {
1083 MagickBooleanType
1084 proceed;
1085
cristy21e03412011-09-25 22:39:35 +00001086 proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height,
1087 image->rows);
1088 if (proceed == MagickFalse)
1089 status=MagickFalse;
1090 }
1091 }
1092 (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType)
1093 image->rows-1,image->rows);
1094 Swap(page.width,page.height);
1095 Swap(page.x,page.y);
1096 if (page.width != 0)
1097 page.x=(ssize_t) (page.width-rotate_image->columns-page.x);
1098 break;
1099 }
cristy4d0ca342014-05-01 00:42:09 +00001100 default:
1101 break;
cristy3ed852e2009-09-05 21:47:34 +00001102 }
1103 rotate_view=DestroyCacheView(rotate_view);
1104 image_view=DestroyCacheView(image_view);
1105 rotate_image->type=image->type;
1106 rotate_image->page=page;
1107 if (status == MagickFalse)
1108 rotate_image=DestroyImage(rotate_image);
1109 return(rotate_image);
1110}
1111
1112/*
1113%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1114% %
1115% %
1116% %
1117+ X S h e a r I m a g e %
1118% %
1119% %
1120% %
1121%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1122%
1123% XShearImage() shears the image in the X direction with a shear angle of
1124% 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1125% negative angles shear clockwise. Angles are measured relative to a vertical
1126% Y-axis. X shears will widen an image creating 'empty' triangles on the left
1127% and right sides of the source image.
1128%
1129% The format of the XShearImage method is:
1130%
cristya19f1d72012-08-07 18:24:38 +00001131% MagickBooleanType XShearImage(Image *image,const double degrees,
cristybb503372010-05-27 20:51:26 +00001132% const size_t width,const size_t height,
1133% const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001134%
1135% A description of each parameter follows.
1136%
1137% o image: the image.
1138%
cristya19f1d72012-08-07 18:24:38 +00001139% o degrees: A double representing the shearing angle along the X
cristy3ed852e2009-09-05 21:47:34 +00001140% axis.
1141%
1142% o width, height, x_offset, y_offset: Defines a region of the image
1143% to shear.
1144%
cristyecc2c142010-01-17 22:25:46 +00001145% o exception: return any errors or warnings in this structure.
1146%
cristy3ed852e2009-09-05 21:47:34 +00001147*/
cristya19f1d72012-08-07 18:24:38 +00001148static MagickBooleanType XShearImage(Image *image,const double degrees,
cristybb503372010-05-27 20:51:26 +00001149 const size_t width,const size_t height,const ssize_t x_offset,
1150 const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001151{
1152#define XShearImageTag "XShear/Image"
1153
1154 typedef enum
1155 {
1156 LEFT,
1157 RIGHT
1158 } ShearDirection;
1159
1160 CacheView
1161 *image_view;
1162
cristy3ed852e2009-09-05 21:47:34 +00001163 MagickBooleanType
1164 status;
1165
cristy5f959472010-05-27 22:19:46 +00001166 MagickOffsetType
1167 progress;
1168
cristy4c08aed2011-07-01 19:47:50 +00001169 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001170 background;
1171
cristy5f959472010-05-27 22:19:46 +00001172 ssize_t
1173 y;
1174
cristy9d8c8ce2011-10-25 16:13:52 +00001175 /*
1176 X shear image.
1177 */
cristy3ed852e2009-09-05 21:47:34 +00001178 assert(image != (Image *) NULL);
1179 assert(image->signature == MagickSignature);
1180 if (image->debug != MagickFalse)
1181 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3ed852e2009-09-05 21:47:34 +00001182 status=MagickTrue;
cristy9d8c8ce2011-10-25 16:13:52 +00001183 background=image->background_color;
cristy3ed852e2009-09-05 21:47:34 +00001184 progress=0;
cristy46ff2672012-12-14 15:32:26 +00001185 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00001186#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00001187 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00001188 magick_threads(image,image,height,1)
cristy3ed852e2009-09-05 21:47:34 +00001189#endif
cristybb503372010-05-27 20:51:26 +00001190 for (y=0; y < (ssize_t) height; y++)
cristy3ed852e2009-09-05 21:47:34 +00001191 {
cristy4c08aed2011-07-01 19:47:50 +00001192 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001193 pixel,
1194 source,
1195 destination;
1196
cristya19f1d72012-08-07 18:24:38 +00001197 double
cristy3ed852e2009-09-05 21:47:34 +00001198 area,
1199 displacement;
1200
cristy4c08aed2011-07-01 19:47:50 +00001201 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001202 *restrict p,
1203 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001204
cristyd40deb62011-03-09 00:52:27 +00001205 register ssize_t
1206 i;
1207
cristy3ed852e2009-09-05 21:47:34 +00001208 ShearDirection
1209 direction;
1210
cristyd40deb62011-03-09 00:52:27 +00001211 ssize_t
1212 step;
1213
cristy3ed852e2009-09-05 21:47:34 +00001214 if (status == MagickFalse)
1215 continue;
1216 p=GetCacheViewAuthenticPixels(image_view,0,y_offset+y,image->columns,1,
1217 exception);
cristy4c08aed2011-07-01 19:47:50 +00001218 if (p == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001219 {
1220 status=MagickFalse;
1221 continue;
1222 }
cristyed231572011-07-14 02:18:59 +00001223 p+=x_offset*GetPixelChannels(image);
cristya19f1d72012-08-07 18:24:38 +00001224 displacement=degrees*(double) (y-height/2.0);
cristy3ed852e2009-09-05 21:47:34 +00001225 if (displacement == 0.0)
1226 continue;
1227 if (displacement > 0.0)
1228 direction=RIGHT;
1229 else
1230 {
1231 displacement*=(-1.0);
1232 direction=LEFT;
1233 }
cristybb503372010-05-27 20:51:26 +00001234 step=(ssize_t) floor((double) displacement);
cristya19f1d72012-08-07 18:24:38 +00001235 area=(double) (displacement-step);
cristy3ed852e2009-09-05 21:47:34 +00001236 step++;
1237 pixel=background;
cristy4c08aed2011-07-01 19:47:50 +00001238 GetPixelInfo(image,&source);
1239 GetPixelInfo(image,&destination);
cristy3ed852e2009-09-05 21:47:34 +00001240 switch (direction)
1241 {
1242 case LEFT:
1243 {
1244 /*
1245 Transfer pixels left-to-right.
1246 */
1247 if (step > x_offset)
1248 break;
cristyed231572011-07-14 02:18:59 +00001249 q=p-step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001250 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001251 {
1252 if ((x_offset+i) < step)
1253 {
cristyed231572011-07-14 02:18:59 +00001254 p+=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001255 GetPixelInfoPixel(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001256 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001257 continue;
1258 }
cristy803640d2011-11-17 02:11:32 +00001259 GetPixelInfoPixel(image,p,&source);
cristya19f1d72012-08-07 18:24:38 +00001260 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1261 &source,(double) GetPixelAlpha(image,p),area,&destination);
cristy803640d2011-11-17 02:11:32 +00001262 SetPixelInfoPixel(image,&destination,q);
1263 GetPixelInfoPixel(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001264 p+=GetPixelChannels(image);
1265 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001266 }
cristya19f1d72012-08-07 18:24:38 +00001267 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1268 &background,(double) background.alpha,area,&destination);
cristy803640d2011-11-17 02:11:32 +00001269 SetPixelInfoPixel(image,&destination,q);
cristyed231572011-07-14 02:18:59 +00001270 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001271 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001272 {
cristy803640d2011-11-17 02:11:32 +00001273 SetPixelInfoPixel(image,&background,q);
cristyed231572011-07-14 02:18:59 +00001274 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001275 }
cristy3ed852e2009-09-05 21:47:34 +00001276 break;
1277 }
1278 case RIGHT:
1279 {
1280 /*
1281 Transfer pixels right-to-left.
1282 */
cristyed231572011-07-14 02:18:59 +00001283 p+=width*GetPixelChannels(image);
1284 q=p+step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001285 for (i=0; i < (ssize_t) width; i++)
cristy3ed852e2009-09-05 21:47:34 +00001286 {
cristyed231572011-07-14 02:18:59 +00001287 p-=GetPixelChannels(image);
1288 q-=GetPixelChannels(image);
cristy5285ef62014-02-26 13:55:31 +00001289 if ((size_t) (x_offset+width+step-i) > image->columns)
cristy3ed852e2009-09-05 21:47:34 +00001290 continue;
cristy803640d2011-11-17 02:11:32 +00001291 GetPixelInfoPixel(image,p,&source);
cristya19f1d72012-08-07 18:24:38 +00001292 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1293 &source,(double) GetPixelAlpha(image,p),area,&destination);
cristy803640d2011-11-17 02:11:32 +00001294 SetPixelInfoPixel(image,&destination,q);
1295 GetPixelInfoPixel(image,p,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001296 }
cristya19f1d72012-08-07 18:24:38 +00001297 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1298 &background,(double) background.alpha,area,&destination);
cristyed231572011-07-14 02:18:59 +00001299 q-=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001300 SetPixelInfoPixel(image,&destination,q);
cristy3ed852e2009-09-05 21:47:34 +00001301 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001302 {
cristyed231572011-07-14 02:18:59 +00001303 q-=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001304 SetPixelInfoPixel(image,&background,q);
cristy4c08aed2011-07-01 19:47:50 +00001305 }
cristy3ed852e2009-09-05 21:47:34 +00001306 break;
1307 }
1308 }
1309 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1310 status=MagickFalse;
1311 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1312 {
1313 MagickBooleanType
1314 proceed;
1315
cristyb5d5f722009-11-04 03:03:49 +00001316#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy7b650b52011-10-09 01:13:39 +00001317 #pragma omp critical (MagickCore_XShearImage)
cristy3ed852e2009-09-05 21:47:34 +00001318#endif
1319 proceed=SetImageProgress(image,XShearImageTag,progress++,height);
1320 if (proceed == MagickFalse)
1321 status=MagickFalse;
1322 }
1323 }
1324 image_view=DestroyCacheView(image_view);
1325 return(status);
1326}
1327
1328/*
1329%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1330% %
1331% %
1332% %
1333+ Y S h e a r I m a g e %
1334% %
1335% %
1336% %
1337%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1338%
1339% YShearImage shears the image in the Y direction with a shear angle of
1340% 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and
1341% negative angles shear clockwise. Angles are measured relative to a
1342% horizontal X-axis. Y shears will increase the height of an image creating
1343% 'empty' triangles on the top and bottom of the source image.
1344%
1345% The format of the YShearImage method is:
1346%
cristya19f1d72012-08-07 18:24:38 +00001347% MagickBooleanType YShearImage(Image *image,const double degrees,
cristybb503372010-05-27 20:51:26 +00001348% const size_t width,const size_t height,
1349% const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001350%
1351% A description of each parameter follows.
1352%
1353% o image: the image.
1354%
cristya19f1d72012-08-07 18:24:38 +00001355% o degrees: A double representing the shearing angle along the Y
cristy3ed852e2009-09-05 21:47:34 +00001356% axis.
1357%
1358% o width, height, x_offset, y_offset: Defines a region of the image
1359% to shear.
1360%
cristyecc2c142010-01-17 22:25:46 +00001361% o exception: return any errors or warnings in this structure.
1362%
cristy3ed852e2009-09-05 21:47:34 +00001363*/
cristya19f1d72012-08-07 18:24:38 +00001364static MagickBooleanType YShearImage(Image *image,const double degrees,
cristybb503372010-05-27 20:51:26 +00001365 const size_t width,const size_t height,const ssize_t x_offset,
1366 const ssize_t y_offset,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001367{
1368#define YShearImageTag "YShear/Image"
1369
1370 typedef enum
1371 {
1372 UP,
1373 DOWN
1374 } ShearDirection;
1375
1376 CacheView
1377 *image_view;
1378
cristy3ed852e2009-09-05 21:47:34 +00001379 MagickBooleanType
1380 status;
1381
cristy5f959472010-05-27 22:19:46 +00001382 MagickOffsetType
1383 progress;
1384
cristy4c08aed2011-07-01 19:47:50 +00001385 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001386 background;
1387
cristy5f959472010-05-27 22:19:46 +00001388 ssize_t
1389 x;
1390
cristy9d8c8ce2011-10-25 16:13:52 +00001391 /*
1392 Y Shear image.
1393 */
cristy3ed852e2009-09-05 21:47:34 +00001394 assert(image != (Image *) NULL);
1395 assert(image->signature == MagickSignature);
1396 if (image->debug != MagickFalse)
1397 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3ed852e2009-09-05 21:47:34 +00001398 status=MagickTrue;
1399 progress=0;
cristy9d8c8ce2011-10-25 16:13:52 +00001400 background=image->background_color;
cristy46ff2672012-12-14 15:32:26 +00001401 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00001402#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00001403 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00001404 magick_threads(image,image,width,1)
cristy3ed852e2009-09-05 21:47:34 +00001405#endif
cristybb503372010-05-27 20:51:26 +00001406 for (x=0; x < (ssize_t) width; x++)
cristy3ed852e2009-09-05 21:47:34 +00001407 {
cristybb503372010-05-27 20:51:26 +00001408 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001409 step;
1410
cristya19f1d72012-08-07 18:24:38 +00001411 double
cristy3ed852e2009-09-05 21:47:34 +00001412 area,
1413 displacement;
1414
cristy4c08aed2011-07-01 19:47:50 +00001415 PixelInfo
1416 pixel,
1417 source,
1418 destination;
1419
1420 register Quantum
1421 *restrict p,
1422 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001423
cristybb503372010-05-27 20:51:26 +00001424 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001425 i;
1426
cristy3ed852e2009-09-05 21:47:34 +00001427 ShearDirection
1428 direction;
1429
1430 if (status == MagickFalse)
1431 continue;
1432 p=GetCacheViewAuthenticPixels(image_view,x_offset+x,0,1,image->rows,
1433 exception);
cristy4c08aed2011-07-01 19:47:50 +00001434 if (p == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001435 {
1436 status=MagickFalse;
1437 continue;
1438 }
cristyed231572011-07-14 02:18:59 +00001439 p+=y_offset*GetPixelChannels(image);
cristya19f1d72012-08-07 18:24:38 +00001440 displacement=degrees*(double) (x-width/2.0);
cristy3ed852e2009-09-05 21:47:34 +00001441 if (displacement == 0.0)
1442 continue;
1443 if (displacement > 0.0)
1444 direction=DOWN;
1445 else
1446 {
1447 displacement*=(-1.0);
1448 direction=UP;
1449 }
cristybb503372010-05-27 20:51:26 +00001450 step=(ssize_t) floor((double) displacement);
cristya19f1d72012-08-07 18:24:38 +00001451 area=(double) (displacement-step);
cristy3ed852e2009-09-05 21:47:34 +00001452 step++;
1453 pixel=background;
cristy4c08aed2011-07-01 19:47:50 +00001454 GetPixelInfo(image,&source);
1455 GetPixelInfo(image,&destination);
cristy3ed852e2009-09-05 21:47:34 +00001456 switch (direction)
1457 {
1458 case UP:
1459 {
1460 /*
1461 Transfer pixels top-to-bottom.
1462 */
1463 if (step > y_offset)
1464 break;
cristyed231572011-07-14 02:18:59 +00001465 q=p-step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001466 for (i=0; i < (ssize_t) height; i++)
cristy3ed852e2009-09-05 21:47:34 +00001467 {
1468 if ((y_offset+i) < step)
1469 {
cristyed231572011-07-14 02:18:59 +00001470 p+=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001471 GetPixelInfoPixel(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001472 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001473 continue;
1474 }
cristy803640d2011-11-17 02:11:32 +00001475 GetPixelInfoPixel(image,p,&source);
cristya19f1d72012-08-07 18:24:38 +00001476 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1477 &source,(double) GetPixelAlpha(image,p),area,
cristy4c08aed2011-07-01 19:47:50 +00001478 &destination);
cristy803640d2011-11-17 02:11:32 +00001479 SetPixelInfoPixel(image,&destination,q);
1480 GetPixelInfoPixel(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00001481 p+=GetPixelChannels(image);
1482 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001483 }
cristya19f1d72012-08-07 18:24:38 +00001484 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1485 &background,(double) background.alpha,area,&destination);
cristy803640d2011-11-17 02:11:32 +00001486 SetPixelInfoPixel(image,&destination,q);
cristyed231572011-07-14 02:18:59 +00001487 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001488 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001489 {
cristy803640d2011-11-17 02:11:32 +00001490 SetPixelInfoPixel(image,&background,q);
cristyed231572011-07-14 02:18:59 +00001491 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001492 }
cristy3ed852e2009-09-05 21:47:34 +00001493 break;
1494 }
1495 case DOWN:
1496 {
1497 /*
1498 Transfer pixels bottom-to-top.
1499 */
cristyed231572011-07-14 02:18:59 +00001500 p+=height*GetPixelChannels(image);
1501 q=p+step*GetPixelChannels(image);
cristybb503372010-05-27 20:51:26 +00001502 for (i=0; i < (ssize_t) height; i++)
cristy3ed852e2009-09-05 21:47:34 +00001503 {
cristyed231572011-07-14 02:18:59 +00001504 p-=GetPixelChannels(image);
1505 q-=GetPixelChannels(image);
cristy5285ef62014-02-26 13:55:31 +00001506 if ((size_t) (y_offset+height+step-i) > image->rows)
cristy3ed852e2009-09-05 21:47:34 +00001507 continue;
cristy803640d2011-11-17 02:11:32 +00001508 GetPixelInfoPixel(image,p,&source);
cristya19f1d72012-08-07 18:24:38 +00001509 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1510 &source,(double) GetPixelAlpha(image,p),area,
cristy4c08aed2011-07-01 19:47:50 +00001511 &destination);
cristy803640d2011-11-17 02:11:32 +00001512 SetPixelInfoPixel(image,&destination,q);
1513 GetPixelInfoPixel(image,p,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001514 }
cristya19f1d72012-08-07 18:24:38 +00001515 CompositePixelInfoAreaBlend(&pixel,(double) pixel.alpha,
1516 &background,(double) background.alpha,area,&destination);
cristyed231572011-07-14 02:18:59 +00001517 q-=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001518 SetPixelInfoPixel(image,&destination,q);
cristy3ed852e2009-09-05 21:47:34 +00001519 for (i=0; i < (step-1); i++)
cristy4c08aed2011-07-01 19:47:50 +00001520 {
cristyed231572011-07-14 02:18:59 +00001521 q-=GetPixelChannels(image);
cristy803640d2011-11-17 02:11:32 +00001522 SetPixelInfoPixel(image,&background,q);
cristy4c08aed2011-07-01 19:47:50 +00001523 }
cristy3ed852e2009-09-05 21:47:34 +00001524 break;
1525 }
1526 }
1527 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1528 status=MagickFalse;
1529 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1530 {
1531 MagickBooleanType
1532 proceed;
1533
cristyb5d5f722009-11-04 03:03:49 +00001534#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy7b650b52011-10-09 01:13:39 +00001535 #pragma omp critical (MagickCore_YShearImage)
cristy3ed852e2009-09-05 21:47:34 +00001536#endif
1537 proceed=SetImageProgress(image,YShearImageTag,progress++,image->rows);
1538 if (proceed == MagickFalse)
1539 status=MagickFalse;
1540 }
1541 }
1542 image_view=DestroyCacheView(image_view);
1543 return(status);
1544}
1545
1546/*
1547%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1548% %
1549% %
1550% %
cristy3ed852e2009-09-05 21:47:34 +00001551% S h e a r I m a g e %
1552% %
1553% %
1554% %
1555%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1556%
1557% ShearImage() creates a new image that is a shear_image copy of an existing
cristycee97112010-05-28 00:44:52 +00001558% one. Shearing slides one edge of an image along the X or Y axis, creating
1559% a parallelogram. An X direction shear slides an edge along the X axis,
1560% while a Y direction shear slides an edge along the Y axis. The amount of
cristy3ed852e2009-09-05 21:47:34 +00001561% the shear is controlled by a shear angle. For X direction shears, x_shear
1562% is measured relative to the Y axis, and similarly, for Y direction shears
1563% y_shear is measured relative to the X axis. Empty triangles left over from
1564% shearing the image are filled with the background color defined by member
1565% 'background_color' of the image.. ShearImage() allocates the memory
1566% necessary for the new Image structure and returns a pointer to the new image.
1567%
1568% ShearImage() is based on the paper "A Fast Algorithm for General Raster
1569% Rotatation" by Alan W. Paeth.
1570%
1571% The format of the ShearImage method is:
1572%
1573% Image *ShearImage(const Image *image,const double x_shear,
1574% const double y_shear,ExceptionInfo *exception)
1575%
1576% A description of each parameter follows.
1577%
1578% o image: the image.
1579%
1580% o x_shear, y_shear: Specifies the number of degrees to shear the image.
1581%
1582% o exception: return any errors or warnings in this structure.
1583%
1584*/
1585MagickExport Image *ShearImage(const Image *image,const double x_shear,
1586 const double y_shear,ExceptionInfo *exception)
1587{
1588 Image
1589 *integral_image,
1590 *shear_image;
1591
cristyecc2c142010-01-17 22:25:46 +00001592 MagickBooleanType
1593 status;
1594
cristy3ed852e2009-09-05 21:47:34 +00001595 PointInfo
1596 shear;
1597
1598 RectangleInfo
cristy5285ef62014-02-26 13:55:31 +00001599 border_info,
1600 bounds;
cristy3ed852e2009-09-05 21:47:34 +00001601
1602 assert(image != (Image *) NULL);
1603 assert(image->signature == MagickSignature);
1604 if (image->debug != MagickFalse)
1605 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1606 assert(exception != (ExceptionInfo *) NULL);
1607 assert(exception->signature == MagickSignature);
1608 if ((x_shear != 0.0) && (fmod(x_shear,90.0) == 0.0))
1609 ThrowImageException(ImageError,"AngleIsDiscontinuous");
1610 if ((y_shear != 0.0) && (fmod(y_shear,90.0) == 0.0))
1611 ThrowImageException(ImageError,"AngleIsDiscontinuous");
1612 /*
1613 Initialize shear angle.
1614 */
1615 integral_image=CloneImage(image,0,0,MagickTrue,exception);
1616 if (integral_image == (Image *) NULL)
1617 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1618 shear.x=(-tan(DegreesToRadians(fmod(x_shear,360.0))));
1619 shear.y=tan(DegreesToRadians(fmod(y_shear,360.0)));
1620 if ((shear.x == 0.0) && (shear.y == 0.0))
1621 return(integral_image);
cristy574cc262011-08-05 01:23:58 +00001622 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001623 {
cristy3ed852e2009-09-05 21:47:34 +00001624 integral_image=DestroyImage(integral_image);
1625 return(integral_image);
1626 }
cristy8a46d822012-08-28 23:32:39 +00001627 if (integral_image->alpha_trait != BlendPixelTrait)
cristy63240882011-08-05 19:05:27 +00001628 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00001629 /*
1630 Compute image size.
1631 */
cristy5285ef62014-02-26 13:55:31 +00001632 bounds.width=image->columns+(ssize_t) floor(fabs(shear.x)*image->rows+0.5);
1633 bounds.x=(ssize_t) ceil((double) image->columns+((fabs(shear.x)*image->rows)-
cristy06609ee2010-03-17 20:21:27 +00001634 image->columns)/2.0-0.5);
cristy5285ef62014-02-26 13:55:31 +00001635 bounds.y=(ssize_t) ceil((double) image->rows+((fabs(shear.y)*bounds.width)-
cristycee97112010-05-28 00:44:52 +00001636 image->rows)/2.0-0.5);
cristy3ed852e2009-09-05 21:47:34 +00001637 /*
1638 Surround image with border.
1639 */
1640 integral_image->border_color=integral_image->background_color;
1641 integral_image->compose=CopyCompositeOp;
cristy5285ef62014-02-26 13:55:31 +00001642 border_info.width=(size_t) bounds.x;
1643 border_info.height=(size_t) bounds.y;
cristy633f0c62011-09-15 13:27:36 +00001644 shear_image=BorderImage(integral_image,&border_info,image->compose,exception);
cristyecc2c142010-01-17 22:25:46 +00001645 integral_image=DestroyImage(integral_image);
cristy3ed852e2009-09-05 21:47:34 +00001646 if (shear_image == (Image *) NULL)
1647 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +00001648 /*
1649 Shear the image.
1650 */
cristy8a46d822012-08-28 23:32:39 +00001651 if (shear_image->alpha_trait != BlendPixelTrait)
cristy63240882011-08-05 19:05:27 +00001652 (void) SetImageAlphaChannel(shear_image,OpaqueAlphaChannel,exception);
cristy5285ef62014-02-26 13:55:31 +00001653 status=XShearImage(shear_image,shear.x,image->columns,image->rows,bounds.x,
cristyeaedf062010-05-29 22:36:02 +00001654 (ssize_t) (shear_image->rows-image->rows)/2,exception);
cristyecc2c142010-01-17 22:25:46 +00001655 if (status == MagickFalse)
1656 {
1657 shear_image=DestroyImage(shear_image);
1658 return((Image *) NULL);
1659 }
cristy5285ef62014-02-26 13:55:31 +00001660 status=YShearImage(shear_image,shear.y,bounds.width,image->rows,(ssize_t)
1661 (shear_image->columns-bounds.width)/2,bounds.y,exception);
cristyecc2c142010-01-17 22:25:46 +00001662 if (status == MagickFalse)
1663 {
1664 shear_image=DestroyImage(shear_image);
1665 return((Image *) NULL);
1666 }
cristy5285ef62014-02-26 13:55:31 +00001667 status=CropToFitImage(&shear_image,shear.x,shear.y,(MagickRealType)
1668 image->columns,(MagickRealType) image->rows,MagickFalse,exception);
cristy3ed852e2009-09-05 21:47:34 +00001669 shear_image->compose=image->compose;
1670 shear_image->page.width=0;
1671 shear_image->page.height=0;
cristy1c2f48d2012-12-14 01:20:55 +00001672 if (status == MagickFalse)
1673 shear_image=DestroyImage(shear_image);
cristy3ed852e2009-09-05 21:47:34 +00001674 return(shear_image);
1675}
cristyc7c81142011-11-07 12:14:23 +00001676
1677/*
1678%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1679% %
1680% %
1681% %
1682% S h e a r R o t a t e I m a g e %
1683% %
1684% %
1685% %
1686%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1687%
1688% ShearRotateImage() creates a new image that is a rotated copy of an existing
1689% one. Positive angles rotate counter-clockwise (right-hand rule), while
1690% negative angles rotate clockwise. Rotated images are usually larger than
1691% the originals and have 'empty' triangular corners. X axis. Empty
1692% triangles left over from shearing the image are filled with the background
1693% color defined by member 'background_color' of the image. ShearRotateImage
1694% allocates the memory necessary for the new Image structure and returns a
1695% pointer to the new image.
1696%
1697% ShearRotateImage() is based on the paper "A Fast Algorithm for General
1698% Raster Rotatation" by Alan W. Paeth. ShearRotateImage is adapted from a
1699% similar method based on the Paeth paper written by Michael Halle of the
1700% Spatial Imaging Group, MIT Media Lab.
1701%
1702% The format of the ShearRotateImage method is:
1703%
1704% Image *ShearRotateImage(const Image *image,const double degrees,
1705% ExceptionInfo *exception)
1706%
1707% A description of each parameter follows.
1708%
1709% o image: the image.
1710%
1711% o degrees: Specifies the number of degrees to rotate the image.
1712%
1713% o exception: return any errors or warnings in this structure.
1714%
1715*/
1716MagickExport Image *ShearRotateImage(const Image *image,const double degrees,
1717 ExceptionInfo *exception)
1718{
1719 Image
1720 *integral_image,
1721 *rotate_image;
1722
1723 MagickBooleanType
1724 status;
1725
cristy5285ef62014-02-26 13:55:31 +00001726 MagickRealType
cristyc7c81142011-11-07 12:14:23 +00001727 angle;
1728
1729 PointInfo
1730 shear;
1731
1732 RectangleInfo
cristy5285ef62014-02-26 13:55:31 +00001733 border_info,
1734 bounds;
cristyc7c81142011-11-07 12:14:23 +00001735
1736 size_t
1737 height,
1738 rotations,
cristy5285ef62014-02-26 13:55:31 +00001739 shear_width,
1740 width;
cristyc7c81142011-11-07 12:14:23 +00001741
1742 /*
1743 Adjust rotation angle.
1744 */
1745 assert(image != (Image *) NULL);
1746 assert(image->signature == MagickSignature);
1747 if (image->debug != MagickFalse)
1748 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1749 assert(exception != (ExceptionInfo *) NULL);
1750 assert(exception->signature == MagickSignature);
1751 angle=degrees;
1752 while (angle < -45.0)
1753 angle+=360.0;
1754 for (rotations=0; angle > 45.0; rotations++)
1755 angle-=90.0;
1756 rotations%=4;
1757 /*
1758 Calculate shear equations.
1759 */
1760 integral_image=IntegralRotateImage(image,rotations,exception);
1761 if (integral_image == (Image *) NULL)
1762 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1763 shear.x=(-tan((double) DegreesToRadians(angle)/2.0));
1764 shear.y=sin((double) DegreesToRadians(angle));
1765 if ((shear.x == 0.0) && (shear.y == 0.0))
1766 return(integral_image);
1767 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
1768 {
1769 integral_image=DestroyImage(integral_image);
1770 return(integral_image);
1771 }
cristy8a46d822012-08-28 23:32:39 +00001772 if (integral_image->alpha_trait != BlendPixelTrait)
cristyc7c81142011-11-07 12:14:23 +00001773 (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel,exception);
1774 /*
cristy5285ef62014-02-26 13:55:31 +00001775 Compute maximum bounds for 3 shear operations.
cristyc7c81142011-11-07 12:14:23 +00001776 */
cristy5285ef62014-02-26 13:55:31 +00001777 width=integral_image->columns;
1778 height=integral_image->rows;
1779 bounds.width=(size_t) floor(fabs((double) height*shear.x)+width+0.5);
1780 bounds.height=(size_t) floor(fabs((double) bounds.width*shear.y)+height+0.5);
1781 shear_width=(size_t) floor(fabs((double) bounds.height*shear.x)+
1782 bounds.width+0.5);
1783 bounds.x=(ssize_t) floor((double) ((shear_width > bounds.width) ? width :
1784 bounds.width-shear_width+2)/2.0+0.5);
1785 bounds.y=(ssize_t) floor(((double) bounds.height-height+2)/2.0+0.5);
cristyc7c81142011-11-07 12:14:23 +00001786 /*
1787 Surround image with a border.
1788 */
1789 integral_image->border_color=integral_image->background_color;
1790 integral_image->compose=CopyCompositeOp;
cristy5285ef62014-02-26 13:55:31 +00001791 border_info.width=(size_t) bounds.x;
1792 border_info.height=(size_t) bounds.y;
cristyc7c81142011-11-07 12:14:23 +00001793 rotate_image=BorderImage(integral_image,&border_info,image->compose,
1794 exception);
1795 integral_image=DestroyImage(integral_image);
1796 if (rotate_image == (Image *) NULL)
1797 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1798 /*
1799 Rotate the image.
1800 */
cristy5285ef62014-02-26 13:55:31 +00001801 status=XShearImage(rotate_image,shear.x,width,height,bounds.x,(ssize_t)
cristyc7c81142011-11-07 12:14:23 +00001802 (rotate_image->rows-height)/2,exception);
1803 if (status == MagickFalse)
1804 {
1805 rotate_image=DestroyImage(rotate_image);
1806 return((Image *) NULL);
1807 }
cristy5285ef62014-02-26 13:55:31 +00001808 status=YShearImage(rotate_image,shear.y,bounds.width,height,(ssize_t)
1809 (rotate_image->columns-bounds.width)/2,bounds.y,exception);
cristyc7c81142011-11-07 12:14:23 +00001810 if (status == MagickFalse)
1811 {
1812 rotate_image=DestroyImage(rotate_image);
1813 return((Image *) NULL);
1814 }
cristy5285ef62014-02-26 13:55:31 +00001815 status=XShearImage(rotate_image,shear.x,bounds.width,bounds.height,(ssize_t)
1816 (rotate_image->columns-bounds.width)/2,(ssize_t) (rotate_image->rows-
1817 bounds.height)/2,exception);
cristyc7c81142011-11-07 12:14:23 +00001818 if (status == MagickFalse)
1819 {
1820 rotate_image=DestroyImage(rotate_image);
1821 return((Image *) NULL);
1822 }
cristy5285ef62014-02-26 13:55:31 +00001823 status=CropToFitImage(&rotate_image,shear.x,shear.y,(MagickRealType) width,
1824 (MagickRealType) height,MagickTrue,exception);
cristyc7c81142011-11-07 12:14:23 +00001825 rotate_image->compose=image->compose;
1826 rotate_image->page.width=0;
1827 rotate_image->page.height=0;
cristy1c2f48d2012-12-14 01:20:55 +00001828 if (status == MagickFalse)
1829 rotate_image=DestroyImage(rotate_image);
cristyc7c81142011-11-07 12:14:23 +00001830 return(rotate_image);
1831}