cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % % |
| 6 | % SSSSS H H EEEEE AAA RRRR % |
| 7 | % SS H H E A A R R % |
| 8 | % SSS HHHHH EEE AAAAA RRRR % |
| 9 | % SS H H E A A R R % |
| 10 | % SSSSS H H EEEEE A A R R % |
| 11 | % % |
| 12 | % % |
| 13 | % MagickCore Methods to Shear or Rotate an Image by an Arbitrary Angle % |
| 14 | % % |
| 15 | % Software Design % |
| 16 | % John Cristy % |
| 17 | % July 1992 % |
| 18 | % % |
| 19 | % % |
cristy | 16af1cb | 2009-12-11 21:38:29 +0000 | [diff] [blame] | 20 | % Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 21 | % 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 | % |
| 36 | % The RotateImage, XShearImage, and YShearImage methods are based on the |
| 37 | % paper "A Fast Algorithm for General Raster Rotatation" by Alan W. Paeth, |
| 38 | % Graphics Interface '86 (Vancouver). RotateImage 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. |
| 41 | % |
| 42 | % |
| 43 | */ |
| 44 | |
| 45 | /* |
| 46 | Include declarations. |
| 47 | */ |
| 48 | #include "magick/studio.h" |
| 49 | #include "magick/artifact.h" |
cristy | 5a2ca48 | 2009-10-14 18:24:56 +0000 | [diff] [blame] | 50 | #include "magick/attribute.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 51 | #include "magick/blob-private.h" |
| 52 | #include "magick/cache-private.h" |
| 53 | #include "magick/color-private.h" |
| 54 | #include "magick/colorspace-private.h" |
| 55 | #include "magick/composite.h" |
| 56 | #include "magick/composite-private.h" |
| 57 | #include "magick/decorate.h" |
| 58 | #include "magick/distort.h" |
| 59 | #include "magick/draw.h" |
| 60 | #include "magick/exception.h" |
| 61 | #include "magick/exception-private.h" |
| 62 | #include "magick/gem.h" |
| 63 | #include "magick/geometry.h" |
| 64 | #include "magick/image.h" |
| 65 | #include "magick/image-private.h" |
| 66 | #include "magick/memory_.h" |
| 67 | #include "magick/list.h" |
| 68 | #include "magick/monitor.h" |
| 69 | #include "magick/monitor-private.h" |
| 70 | #include "magick/pixel-private.h" |
| 71 | #include "magick/quantum.h" |
| 72 | #include "magick/resource_.h" |
| 73 | #include "magick/shear.h" |
| 74 | #include "magick/statistic.h" |
cristy | f2f2727 | 2009-12-17 14:48:46 +0000 | [diff] [blame] | 75 | #include "magick/string_.h" |
| 76 | #include "magick/string-private.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 77 | #include "magick/threshold.h" |
| 78 | #include "magick/transform.h" |
| 79 | |
| 80 | /* |
| 81 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 82 | % % |
| 83 | % % |
| 84 | % % |
| 85 | % A f f i n e T r a n s f o r m I m a g e % |
| 86 | % % |
| 87 | % % |
| 88 | % % |
| 89 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 90 | % |
| 91 | % AffineTransformImage() transforms an image as dictated by the affine matrix. |
| 92 | % It allocates the memory necessary for the new Image structure and returns |
| 93 | % a pointer to the new image. |
| 94 | % |
| 95 | % The format of the AffineTransformImage method is: |
| 96 | % |
| 97 | % Image *AffineTransformImage(const Image *image, |
| 98 | % AffineMatrix *affine_matrix,ExceptionInfo *exception) |
| 99 | % |
| 100 | % A description of each parameter follows: |
| 101 | % |
| 102 | % o image: the image. |
| 103 | % |
| 104 | % o affine_matrix: the affine matrix. |
| 105 | % |
| 106 | % o exception: return any errors or warnings in this structure. |
| 107 | % |
| 108 | */ |
| 109 | MagickExport Image *AffineTransformImage(const Image *image, |
| 110 | const AffineMatrix *affine_matrix,ExceptionInfo *exception) |
| 111 | { |
| 112 | double |
| 113 | distort[6]; |
| 114 | |
| 115 | Image |
| 116 | *deskew_image; |
| 117 | |
| 118 | /* |
| 119 | Affine transform image. |
| 120 | */ |
| 121 | assert(image->signature == MagickSignature); |
| 122 | if (image->debug != MagickFalse) |
| 123 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 124 | assert(affine_matrix != (AffineMatrix *) NULL); |
| 125 | assert(exception != (ExceptionInfo *) NULL); |
| 126 | assert(exception->signature == MagickSignature); |
| 127 | distort[0]=affine_matrix->sx; |
| 128 | distort[1]=affine_matrix->rx; |
| 129 | distort[2]=affine_matrix->ry; |
| 130 | distort[3]=affine_matrix->sy; |
| 131 | distort[4]=affine_matrix->tx; |
| 132 | distort[5]=affine_matrix->ty; |
| 133 | deskew_image=DistortImage(image,AffineProjectionDistortion,6,distort, |
| 134 | MagickTrue,exception); |
| 135 | return(deskew_image); |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 140 | % % |
| 141 | % % |
| 142 | % % |
| 143 | + C r o p T o F i t I m a g e % |
| 144 | % % |
| 145 | % % |
| 146 | % % |
| 147 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 148 | % |
| 149 | % CropToFitImage() crops the sheared image as determined by the bounding box |
| 150 | % as defined by width and height and shearing angles. |
| 151 | % |
| 152 | % The format of the CropToFitImage method is: |
| 153 | % |
cristy | ecc2c14 | 2010-01-17 22:25:46 +0000 | [diff] [blame] | 154 | % MagickBooleanType CropToFitImage(Image **image, |
| 155 | % const MagickRealType x_shear,const MagickRealType x_shear, |
| 156 | % const MagickRealType width,const MagickRealType height, |
| 157 | % const MagickBooleanType rotate,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 158 | % |
| 159 | % A description of each parameter follows. |
| 160 | % |
| 161 | % o image: the image. |
| 162 | % |
| 163 | % o x_shear, y_shear, width, height: Defines a region of the image to crop. |
| 164 | % |
| 165 | % o exception: return any errors or warnings in this structure. |
| 166 | % |
| 167 | */ |
cristy | ecc2c14 | 2010-01-17 22:25:46 +0000 | [diff] [blame] | 168 | static MagickBooleanType CropToFitImage(Image **image, |
| 169 | const MagickRealType x_shear,const MagickRealType y_shear, |
| 170 | const MagickRealType width,const MagickRealType height, |
| 171 | const MagickBooleanType rotate,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 172 | { |
| 173 | Image |
| 174 | *crop_image; |
| 175 | |
| 176 | PointInfo |
| 177 | extent[4], |
| 178 | min, |
| 179 | max; |
| 180 | |
| 181 | RectangleInfo |
| 182 | geometry, |
| 183 | page; |
| 184 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 185 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 186 | i; |
| 187 | |
| 188 | /* |
| 189 | Calculate the rotated image size. |
| 190 | */ |
| 191 | extent[0].x=(double) (-width/2.0); |
| 192 | extent[0].y=(double) (-height/2.0); |
| 193 | extent[1].x=(double) width/2.0; |
| 194 | extent[1].y=(double) (-height/2.0); |
| 195 | extent[2].x=(double) (-width/2.0); |
| 196 | extent[2].y=(double) height/2.0; |
| 197 | extent[3].x=(double) width/2.0; |
| 198 | extent[3].y=(double) height/2.0; |
| 199 | for (i=0; i < 4; i++) |
| 200 | { |
| 201 | extent[i].x+=x_shear*extent[i].y; |
| 202 | extent[i].y+=y_shear*extent[i].x; |
| 203 | if (rotate != MagickFalse) |
| 204 | extent[i].x+=x_shear*extent[i].y; |
| 205 | extent[i].x+=(double) (*image)->columns/2.0; |
| 206 | extent[i].y+=(double) (*image)->rows/2.0; |
| 207 | } |
| 208 | min=extent[0]; |
| 209 | max=extent[0]; |
| 210 | for (i=1; i < 4; i++) |
| 211 | { |
| 212 | if (min.x > extent[i].x) |
| 213 | min.x=extent[i].x; |
| 214 | if (min.y > extent[i].y) |
| 215 | min.y=extent[i].y; |
| 216 | if (max.x < extent[i].x) |
| 217 | max.x=extent[i].x; |
| 218 | if (max.y < extent[i].y) |
| 219 | max.y=extent[i].y; |
| 220 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 221 | geometry.x=(ssize_t) ceil(min.x-0.5); |
| 222 | geometry.y=(ssize_t) ceil(min.y-0.5); |
| 223 | geometry.width=(size_t) floor(max.x-min.x+0.5); |
| 224 | geometry.height=(size_t) floor(max.y-min.y+0.5); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 225 | page=(*image)->page; |
| 226 | (void) ParseAbsoluteGeometry("0x0+0+0",&(*image)->page); |
| 227 | crop_image=CropImage(*image,&geometry,exception); |
cristy | ecc2c14 | 2010-01-17 22:25:46 +0000 | [diff] [blame] | 228 | if (crop_image == (Image *) NULL) |
| 229 | return(MagickFalse); |
| 230 | crop_image->page=page; |
| 231 | *image=DestroyImage(*image); |
| 232 | *image=crop_image; |
| 233 | return(MagickTrue); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | /* |
| 237 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 238 | % % |
| 239 | % % |
| 240 | % % |
| 241 | % D e s k e w I m a g e % |
| 242 | % % |
| 243 | % % |
| 244 | % % |
| 245 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 246 | % |
| 247 | % DeskewImage() removes skew from the image. Skew is an artifact that |
| 248 | % occurs in scanned images because of the camera being misaligned, |
| 249 | % imperfections in the scanning or surface, or simply because the paper was |
| 250 | % not placed completely flat when scanned. |
| 251 | % |
| 252 | % The format of the DeskewImage method is: |
| 253 | % |
| 254 | % Image *DeskewImage(const Image *image,const double threshold, |
| 255 | % ExceptionInfo *exception) |
| 256 | % |
| 257 | % A description of each parameter follows: |
| 258 | % |
| 259 | % o image: the image. |
| 260 | % |
| 261 | % o threshold: separate background from foreground. |
| 262 | % |
| 263 | % o exception: return any errors or warnings in this structure. |
| 264 | % |
| 265 | */ |
| 266 | |
| 267 | typedef struct _RadonInfo |
| 268 | { |
| 269 | CacheType |
| 270 | type; |
| 271 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 272 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 273 | width, |
| 274 | height; |
| 275 | |
| 276 | MagickSizeType |
| 277 | length; |
| 278 | |
| 279 | MagickBooleanType |
| 280 | mapped; |
| 281 | |
| 282 | char |
| 283 | path[MaxTextExtent]; |
| 284 | |
| 285 | int |
| 286 | file; |
| 287 | |
| 288 | unsigned short |
| 289 | *cells; |
| 290 | } RadonInfo; |
| 291 | |
| 292 | static RadonInfo *DestroyRadonInfo(RadonInfo *radon_info) |
| 293 | { |
| 294 | assert(radon_info != (RadonInfo *) NULL); |
| 295 | switch (radon_info->type) |
| 296 | { |
| 297 | case MemoryCache: |
| 298 | { |
| 299 | if (radon_info->mapped == MagickFalse) |
| 300 | radon_info->cells=(unsigned short *) RelinquishMagickMemory( |
| 301 | radon_info->cells); |
| 302 | else |
| 303 | radon_info->cells=(unsigned short *) UnmapBlob(radon_info->cells, |
| 304 | (size_t) radon_info->length); |
| 305 | RelinquishMagickResource(MemoryResource,radon_info->length); |
| 306 | break; |
| 307 | } |
| 308 | case MapCache: |
| 309 | { |
| 310 | radon_info->cells=(unsigned short *) UnmapBlob(radon_info->cells,(size_t) |
| 311 | radon_info->length); |
| 312 | RelinquishMagickResource(MapResource,radon_info->length); |
| 313 | } |
| 314 | case DiskCache: |
| 315 | { |
| 316 | if (radon_info->file != -1) |
| 317 | (void) close(radon_info->file); |
| 318 | (void) RelinquishUniqueFileResource(radon_info->path); |
| 319 | RelinquishMagickResource(DiskResource,radon_info->length); |
| 320 | break; |
| 321 | } |
| 322 | default: |
| 323 | break; |
| 324 | } |
| 325 | return((RadonInfo *) RelinquishMagickMemory(radon_info)); |
| 326 | } |
| 327 | |
| 328 | static MagickBooleanType ResetRadonCells(RadonInfo *radon_info) |
| 329 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 330 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 331 | y; |
| 332 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 333 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 334 | x; |
| 335 | |
| 336 | ssize_t |
| 337 | count; |
| 338 | |
| 339 | unsigned short |
| 340 | value; |
| 341 | |
| 342 | if (radon_info->type != DiskCache) |
| 343 | { |
| 344 | (void) ResetMagickMemory(radon_info->cells,0,(size_t) radon_info->length); |
| 345 | return(MagickTrue); |
| 346 | } |
| 347 | value=0; |
| 348 | (void) MagickSeek(radon_info->file,0,SEEK_SET); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 349 | for (y=0; y < (ssize_t) radon_info->height; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 350 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 351 | for (x=0; x < (ssize_t) radon_info->width; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 352 | { |
| 353 | count=write(radon_info->file,&value,sizeof(*radon_info->cells)); |
| 354 | if (count != (ssize_t) sizeof(*radon_info->cells)) |
| 355 | break; |
| 356 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 357 | if (x < (ssize_t) radon_info->width) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 358 | break; |
| 359 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 360 | return(y < (ssize_t) radon_info->height ? MagickFalse : MagickTrue); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 361 | } |
| 362 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 363 | static RadonInfo *AcquireRadonInfo(const Image *image,const size_t width, |
| 364 | const size_t height,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 365 | { |
| 366 | MagickBooleanType |
| 367 | status; |
| 368 | |
| 369 | RadonInfo |
| 370 | *radon_info; |
| 371 | |
cristy | 9082321 | 2009-12-12 20:48:33 +0000 | [diff] [blame] | 372 | radon_info=(RadonInfo *) AcquireAlignedMemory(1,sizeof(*radon_info)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 373 | if (radon_info == (RadonInfo *) NULL) |
| 374 | return((RadonInfo *) NULL); |
| 375 | (void) ResetMagickMemory(radon_info,0,sizeof(*radon_info)); |
| 376 | radon_info->width=width; |
| 377 | radon_info->height=height; |
| 378 | radon_info->length=(MagickSizeType) width*height*sizeof(*radon_info->cells); |
| 379 | radon_info->type=MemoryCache; |
| 380 | status=AcquireMagickResource(AreaResource,radon_info->length); |
| 381 | if ((status != MagickFalse) && |
| 382 | (radon_info->length == (MagickSizeType) ((size_t) radon_info->length))) |
| 383 | { |
| 384 | status=AcquireMagickResource(MemoryResource,radon_info->length); |
| 385 | if (status != MagickFalse) |
| 386 | { |
| 387 | radon_info->mapped=MagickFalse; |
| 388 | radon_info->cells=(unsigned short *) AcquireMagickMemory((size_t) |
| 389 | radon_info->length); |
| 390 | if (radon_info->cells == (unsigned short *) NULL) |
| 391 | { |
| 392 | radon_info->mapped=MagickTrue; |
| 393 | radon_info->cells=(unsigned short *) MapBlob(-1,IOMode,0,(size_t) |
| 394 | radon_info->length); |
| 395 | } |
| 396 | if (radon_info->cells == (unsigned short *) NULL) |
| 397 | RelinquishMagickResource(MemoryResource,radon_info->length); |
| 398 | } |
| 399 | } |
| 400 | radon_info->file=(-1); |
| 401 | if (radon_info->cells == (unsigned short *) NULL) |
| 402 | { |
| 403 | status=AcquireMagickResource(DiskResource,radon_info->length); |
| 404 | if (status == MagickFalse) |
| 405 | { |
| 406 | (void) ThrowMagickException(exception,GetMagickModule(),CacheError, |
| 407 | "CacheResourcesExhausted","`%s'",image->filename); |
| 408 | return(DestroyRadonInfo(radon_info)); |
| 409 | } |
| 410 | radon_info->type=DiskCache; |
| 411 | (void) AcquireMagickResource(MemoryResource,radon_info->length); |
| 412 | radon_info->file=AcquireUniqueFileResource(radon_info->path); |
| 413 | if (radon_info->file == -1) |
| 414 | return(DestroyRadonInfo(radon_info)); |
| 415 | status=AcquireMagickResource(MapResource,radon_info->length); |
| 416 | if (status != MagickFalse) |
| 417 | { |
| 418 | status=ResetRadonCells(radon_info); |
| 419 | if (status != MagickFalse) |
| 420 | { |
| 421 | radon_info->cells=(unsigned short *) MapBlob(radon_info->file, |
| 422 | IOMode,0,(size_t) radon_info->length); |
| 423 | if (radon_info->cells != (unsigned short *) NULL) |
| 424 | radon_info->type=MapCache; |
| 425 | else |
| 426 | RelinquishMagickResource(MapResource,radon_info->length); |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | return(radon_info); |
| 431 | } |
| 432 | |
| 433 | static inline size_t MagickMin(const size_t x,const size_t y) |
| 434 | { |
| 435 | if (x < y) |
| 436 | return(x); |
| 437 | return(y); |
| 438 | } |
| 439 | |
| 440 | static inline ssize_t ReadRadonCell(const RadonInfo *radon_info, |
| 441 | const off_t offset,const size_t length,unsigned char *buffer) |
| 442 | { |
| 443 | register ssize_t |
| 444 | i; |
| 445 | |
| 446 | ssize_t |
| 447 | count; |
| 448 | |
| 449 | #if !defined(MAGICKCORE_HAVE_PPREAD) |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 450 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 451 | #pragma omp critical (MagickCore_ReadRadonCell) |
| 452 | #endif |
| 453 | { |
| 454 | i=(-1); |
| 455 | if (MagickSeek(radon_info->file,offset,SEEK_SET) >= 0) |
| 456 | { |
| 457 | #endif |
| 458 | count=0; |
| 459 | for (i=0; i < (ssize_t) length; i+=count) |
| 460 | { |
| 461 | #if !defined(MAGICKCORE_HAVE_PPREAD) |
| 462 | count=read(radon_info->file,buffer+i,MagickMin(length-i,(size_t) |
| 463 | SSIZE_MAX)); |
| 464 | #else |
| 465 | count=pread(radon_info->file,buffer+i,MagickMin(length-i,(size_t) |
| 466 | SSIZE_MAX),(off_t) (offset+i)); |
| 467 | #endif |
| 468 | if (count > 0) |
| 469 | continue; |
| 470 | count=0; |
| 471 | if (errno != EINTR) |
| 472 | { |
| 473 | i=(-1); |
| 474 | break; |
| 475 | } |
| 476 | } |
| 477 | #if !defined(MAGICKCORE_HAVE_PPREAD) |
| 478 | } |
| 479 | } |
| 480 | #endif |
| 481 | return(i); |
| 482 | } |
| 483 | |
| 484 | static inline ssize_t WriteRadonCell(const RadonInfo *radon_info, |
| 485 | const off_t offset,const size_t length,const unsigned char *buffer) |
| 486 | { |
| 487 | register ssize_t |
| 488 | i; |
| 489 | |
| 490 | ssize_t |
| 491 | count; |
| 492 | |
| 493 | #if !defined(MAGICKCORE_HAVE_PWRITE) |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 494 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 495 | #pragma omp critical (MagickCore_WriteRadonCell) |
| 496 | #endif |
| 497 | { |
| 498 | if (MagickSeek(radon_info->file,offset,SEEK_SET) >= 0) |
| 499 | { |
| 500 | #endif |
| 501 | count=0; |
| 502 | for (i=0; i < (ssize_t) length; i+=count) |
| 503 | { |
| 504 | #if !defined(MAGICKCORE_HAVE_PWRITE) |
| 505 | count=write(radon_info->file,buffer+i,MagickMin(length-i,(size_t) |
| 506 | SSIZE_MAX)); |
| 507 | #else |
| 508 | count=pwrite(radon_info->file,buffer+i,MagickMin(length-i,(size_t) |
| 509 | SSIZE_MAX),(off_t) (offset+i)); |
| 510 | #endif |
| 511 | if (count > 0) |
| 512 | continue; |
| 513 | count=0; |
| 514 | if (errno != EINTR) |
| 515 | { |
| 516 | i=(-1); |
| 517 | break; |
| 518 | } |
| 519 | } |
| 520 | #if !defined(MAGICKCORE_HAVE_PWRITE) |
| 521 | } |
| 522 | } |
| 523 | #endif |
| 524 | return(i); |
| 525 | } |
| 526 | |
| 527 | static inline unsigned short GetRadonCell(const RadonInfo *radon_info, |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 528 | const ssize_t x,const ssize_t y) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 529 | { |
| 530 | off_t |
| 531 | i; |
| 532 | |
| 533 | unsigned short |
| 534 | value; |
| 535 | |
| 536 | i=(off_t) radon_info->height*x+y; |
| 537 | if ((i < 0) || |
| 538 | ((MagickSizeType) (i*sizeof(*radon_info->cells)) >= radon_info->length)) |
| 539 | return(0); |
| 540 | if (radon_info->type != DiskCache) |
| 541 | return(radon_info->cells[i]); |
| 542 | value=0; |
| 543 | (void) ReadRadonCell(radon_info,i*sizeof(*radon_info->cells), |
| 544 | sizeof(*radon_info->cells),(unsigned char *) &value); |
| 545 | return(value); |
| 546 | } |
| 547 | |
| 548 | static inline MagickBooleanType SetRadonCell(const RadonInfo *radon_info, |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 549 | const ssize_t x,const ssize_t y,const unsigned short value) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 550 | { |
| 551 | off_t |
| 552 | i; |
| 553 | |
| 554 | ssize_t |
| 555 | count; |
| 556 | |
| 557 | i=(off_t) radon_info->height*x+y; |
| 558 | if ((i < 0) || |
| 559 | ((MagickSizeType) (i*sizeof(*radon_info->cells)) >= radon_info->length)) |
| 560 | return(MagickFalse); |
| 561 | if (radon_info->type != DiskCache) |
| 562 | { |
| 563 | radon_info->cells[i]=value; |
| 564 | return(MagickTrue); |
| 565 | } |
| 566 | count=WriteRadonCell(radon_info,i*sizeof(*radon_info->cells), |
cristy | 44807bb | 2009-09-19 18:28:06 +0000 | [diff] [blame] | 567 | sizeof(*radon_info->cells),(const unsigned char *) &value); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 568 | if (count != (ssize_t) sizeof(*radon_info->cells)) |
| 569 | return(MagickFalse); |
| 570 | return(MagickTrue); |
| 571 | } |
| 572 | |
| 573 | static void RadonProjection(RadonInfo *source_cells, |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 574 | RadonInfo *destination_cells,const ssize_t sign,size_t *projection) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 575 | { |
| 576 | RadonInfo |
| 577 | *swap; |
| 578 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 579 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 580 | x; |
| 581 | |
| 582 | register RadonInfo |
| 583 | *p, |
| 584 | *q; |
| 585 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 586 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 587 | step; |
| 588 | |
| 589 | p=source_cells; |
| 590 | q=destination_cells; |
| 591 | for (step=1; step < p->width; step*=2) |
| 592 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 593 | for (x=0; x < (ssize_t) p->width; x+=2*step) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 594 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 595 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 596 | y; |
| 597 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 598 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 599 | i; |
| 600 | |
| 601 | unsigned short |
| 602 | cell; |
| 603 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 604 | for (i=0; i < (ssize_t) step; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 605 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 606 | for (y=0; y < (ssize_t) (p->height-i-1); y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 607 | { |
| 608 | cell=GetRadonCell(p,x+i,y); |
| 609 | (void) SetRadonCell(q,x+2*i,y,cell+GetRadonCell(p,x+i+step,y+i)); |
| 610 | (void) SetRadonCell(q,x+2*i+1,y,cell+GetRadonCell(p,x+i+step,y+i+1)); |
| 611 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 612 | for ( ; y < (ssize_t) (p->height-i); y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 613 | { |
| 614 | cell=GetRadonCell(p,x+i,y); |
| 615 | (void) SetRadonCell(q,x+2*i,y,cell+GetRadonCell(p,x+i+step,y+i)); |
| 616 | (void) SetRadonCell(q,x+2*i+1,y,cell); |
| 617 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 618 | for ( ; y < (ssize_t) p->height; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 619 | { |
| 620 | cell=GetRadonCell(p,x+i,y); |
| 621 | (void) SetRadonCell(q,x+2*i,y,cell); |
| 622 | (void) SetRadonCell(q,x+2*i+1,y,cell); |
| 623 | } |
| 624 | } |
| 625 | } |
| 626 | swap=p; |
| 627 | p=q; |
| 628 | q=swap; |
| 629 | } |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 630 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 631 | #pragma omp parallel for schedule(dynamic,4) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 632 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 633 | for (x=0; x < (ssize_t) p->width; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 634 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 635 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 636 | y; |
| 637 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 638 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 639 | sum; |
| 640 | |
| 641 | sum=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 642 | for (y=0; y < (ssize_t) (p->height-1); y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 643 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 644 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 645 | delta; |
| 646 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 647 | delta=GetRadonCell(p,x,y)-(ssize_t) GetRadonCell(p,x,y+1); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 648 | sum+=delta*delta; |
| 649 | } |
| 650 | projection[p->width+sign*x-1]=sum; |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | static MagickBooleanType RadonTransform(const Image *image, |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 655 | const double threshold,size_t *projection,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 656 | { |
| 657 | CacheView |
| 658 | *image_view; |
| 659 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 660 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 661 | y; |
| 662 | |
| 663 | MagickBooleanType |
| 664 | status; |
| 665 | |
| 666 | RadonInfo |
| 667 | *destination_cells, |
| 668 | *source_cells; |
| 669 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 670 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 671 | i; |
| 672 | |
| 673 | unsigned char |
| 674 | byte; |
| 675 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 676 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 677 | count, |
| 678 | width; |
| 679 | |
| 680 | unsigned short |
| 681 | bits[256]; |
| 682 | |
| 683 | for (width=1; width < ((image->columns+7)/8); width<<=1) ; |
| 684 | source_cells=AcquireRadonInfo(image,width,image->rows,exception); |
| 685 | destination_cells=AcquireRadonInfo(image,width,image->rows,exception); |
| 686 | if ((source_cells == (RadonInfo *) NULL) || |
| 687 | (destination_cells == (RadonInfo *) NULL)) |
| 688 | { |
| 689 | if (destination_cells != (RadonInfo *) NULL) |
| 690 | destination_cells=DestroyRadonInfo(destination_cells); |
| 691 | if (source_cells != (RadonInfo *) NULL) |
| 692 | source_cells=DestroyRadonInfo(source_cells); |
| 693 | return(MagickFalse); |
| 694 | } |
| 695 | if (ResetRadonCells(source_cells) == MagickFalse) |
| 696 | { |
| 697 | destination_cells=DestroyRadonInfo(destination_cells); |
| 698 | source_cells=DestroyRadonInfo(source_cells); |
| 699 | return(MagickFalse); |
| 700 | } |
| 701 | for (i=0; i < 256; i++) |
| 702 | { |
| 703 | byte=(unsigned char) i; |
| 704 | for (count=0; byte != 0; byte>>=1) |
| 705 | count+=byte & 0x01; |
| 706 | bits[i]=(unsigned short) count; |
| 707 | } |
| 708 | status=MagickTrue; |
| 709 | image_view=AcquireCacheView(image); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 710 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 711 | #pragma omp parallel for schedule(dynamic,4) shared(status) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 712 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 713 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 714 | { |
| 715 | register const PixelPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 716 | *restrict p; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 717 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 718 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 719 | i, |
| 720 | x; |
| 721 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 722 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 723 | bit, |
| 724 | byte; |
| 725 | |
| 726 | if (status == MagickFalse) |
| 727 | continue; |
| 728 | p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception); |
| 729 | if (p == (const PixelPacket *) NULL) |
| 730 | { |
| 731 | status=MagickFalse; |
| 732 | continue; |
| 733 | } |
| 734 | bit=0; |
| 735 | byte=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 736 | i=(ssize_t) (image->columns+7)/8; |
| 737 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 738 | { |
| 739 | byte<<=1; |
| 740 | if (((MagickRealType) p->red < threshold) || |
| 741 | ((MagickRealType) p->green < threshold) || |
| 742 | ((MagickRealType) p->blue < threshold)) |
| 743 | byte|=0x01; |
| 744 | bit++; |
| 745 | if (bit == 8) |
| 746 | { |
| 747 | (void) SetRadonCell(source_cells,--i,y,bits[byte]); |
| 748 | bit=0; |
| 749 | byte=0; |
| 750 | } |
| 751 | p++; |
| 752 | } |
| 753 | if (bit != 0) |
| 754 | { |
| 755 | byte<<=(8-bit); |
| 756 | (void) SetRadonCell(source_cells,--i,y,bits[byte]); |
| 757 | } |
| 758 | } |
| 759 | RadonProjection(source_cells,destination_cells,-1,projection); |
| 760 | (void) ResetRadonCells(source_cells); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 761 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 762 | #pragma omp parallel for schedule(dynamic,4) shared(status) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 763 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 764 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 765 | { |
| 766 | register const PixelPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 767 | *restrict p; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 768 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 769 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 770 | i, |
| 771 | x; |
| 772 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 773 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 774 | bit, |
| 775 | byte; |
| 776 | |
| 777 | if (status == MagickFalse) |
| 778 | continue; |
| 779 | p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception); |
| 780 | if (p == (const PixelPacket *) NULL) |
| 781 | { |
| 782 | status=MagickFalse; |
| 783 | continue; |
| 784 | } |
| 785 | bit=0; |
| 786 | byte=0; |
| 787 | i=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 788 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 789 | { |
| 790 | byte<<=1; |
| 791 | if (((MagickRealType) p->red < threshold) || |
| 792 | ((MagickRealType) p->green < threshold) || |
| 793 | ((MagickRealType) p->blue < threshold)) |
| 794 | byte|=0x01; |
| 795 | bit++; |
| 796 | if (bit == 8) |
| 797 | { |
| 798 | (void) SetRadonCell(source_cells,i++,y,bits[byte]); |
| 799 | bit=0; |
| 800 | byte=0; |
| 801 | } |
| 802 | p++; |
| 803 | } |
| 804 | if (bit != 0) |
| 805 | { |
| 806 | byte<<=(8-bit); |
| 807 | (void) SetRadonCell(source_cells,i++,y,bits[byte]); |
| 808 | } |
| 809 | } |
| 810 | RadonProjection(source_cells,destination_cells,1,projection); |
| 811 | image_view=DestroyCacheView(image_view); |
| 812 | destination_cells=DestroyRadonInfo(destination_cells); |
| 813 | source_cells=DestroyRadonInfo(source_cells); |
| 814 | return(MagickTrue); |
| 815 | } |
| 816 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 817 | static void GetImageBackgroundColor(Image *image,const ssize_t offset, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 818 | ExceptionInfo *exception) |
| 819 | { |
| 820 | CacheView |
| 821 | *image_view; |
| 822 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 823 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 824 | y; |
| 825 | |
| 826 | MagickPixelPacket |
| 827 | background; |
| 828 | |
| 829 | MagickRealType |
| 830 | count; |
| 831 | |
| 832 | /* |
| 833 | Compute average background color. |
| 834 | */ |
| 835 | if (offset <= 0) |
| 836 | return; |
| 837 | GetMagickPixelPacket(image,&background); |
| 838 | count=0.0; |
| 839 | image_view=AcquireCacheView(image); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 840 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 841 | { |
| 842 | register const PixelPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 843 | *restrict p; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 844 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 845 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 846 | x; |
| 847 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 848 | if ((y >= offset) && (y < ((ssize_t) image->rows-offset))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 849 | continue; |
| 850 | p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception); |
| 851 | if (p == (const PixelPacket *) NULL) |
| 852 | continue; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 853 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 854 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 855 | if ((x >= offset) && (x < ((ssize_t) image->columns-offset))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 856 | continue; |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 857 | background.red+=QuantumScale*GetRedPixelComponent(p); |
| 858 | background.green+=QuantumScale*GetGreenPixelComponent(p); |
| 859 | background.blue+=QuantumScale*GetBluePixelComponent(p); |
| 860 | background.opacity+=QuantumScale*GetOpacityPixelComponent(p); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 861 | count++; |
| 862 | p++; |
| 863 | } |
| 864 | } |
| 865 | image_view=DestroyCacheView(image_view); |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 866 | image->background_color.red=ClampToQuantum((MagickRealType) QuantumRange* |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 867 | background.red/count); |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 868 | image->background_color.green=ClampToQuantum((MagickRealType) QuantumRange* |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 869 | background.green/count); |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 870 | image->background_color.blue=ClampToQuantum((MagickRealType) QuantumRange* |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 871 | background.blue/count); |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 872 | image->background_color.opacity=ClampToQuantum((MagickRealType) QuantumRange* |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 873 | background.opacity/count); |
| 874 | } |
| 875 | |
| 876 | MagickExport Image *DeskewImage(const Image *image,const double threshold, |
| 877 | ExceptionInfo *exception) |
| 878 | { |
| 879 | AffineMatrix |
| 880 | affine_matrix; |
| 881 | |
| 882 | const char |
| 883 | *artifact; |
| 884 | |
| 885 | double |
| 886 | degrees; |
| 887 | |
| 888 | Image |
| 889 | *clone_image, |
| 890 | *crop_image, |
| 891 | *deskew_image, |
| 892 | *median_image; |
| 893 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 894 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 895 | skew; |
| 896 | |
| 897 | MagickBooleanType |
| 898 | status; |
| 899 | |
| 900 | RectangleInfo |
| 901 | geometry; |
| 902 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 903 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 904 | i; |
| 905 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 906 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 907 | max_projection, |
| 908 | *projection, |
| 909 | width; |
| 910 | |
| 911 | /* |
| 912 | Compute deskew angle. |
| 913 | */ |
| 914 | for (width=1; width < ((image->columns+7)/8); width<<=1) ; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 915 | projection=(size_t *) AcquireQuantumMemory((size_t) (2*width-1), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 916 | sizeof(*projection)); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 917 | if (projection == (size_t *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 918 | ThrowImageException(ResourceLimitError,"MemoryAllocationFailed"); |
| 919 | status=RadonTransform(image,threshold,projection,exception); |
| 920 | if (status == MagickFalse) |
| 921 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 922 | projection=(size_t *) RelinquishMagickMemory(projection); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 923 | ThrowImageException(ResourceLimitError,"MemoryAllocationFailed"); |
| 924 | } |
| 925 | max_projection=0; |
| 926 | skew=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 927 | for (i=0; i < (ssize_t) (2*width-1); i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 928 | { |
| 929 | if (projection[i] > max_projection) |
| 930 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 931 | skew=i-(ssize_t) width+1; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 932 | max_projection=projection[i]; |
| 933 | } |
| 934 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 935 | projection=(size_t *) RelinquishMagickMemory(projection); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 936 | /* |
| 937 | Deskew image. |
| 938 | */ |
| 939 | clone_image=CloneImage(image,0,0,MagickTrue,exception); |
| 940 | if (clone_image == (Image *) NULL) |
| 941 | return((Image *) NULL); |
| 942 | (void) SetImageVirtualPixelMethod(clone_image,BackgroundVirtualPixelMethod); |
| 943 | degrees=RadiansToDegrees(-atan((double) skew/width/8)); |
| 944 | if (image->debug != MagickFalse) |
cristy | 8cd5b31 | 2010-01-07 01:10:24 +0000 | [diff] [blame] | 945 | (void) LogMagickEvent(TransformEvent,GetMagickModule(), |
cristy | e7f5109 | 2010-01-17 00:39:37 +0000 | [diff] [blame] | 946 | " Deskew angle: %g",degrees); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 947 | affine_matrix.sx=cos(DegreesToRadians(fmod((double) degrees,360.0))); |
| 948 | affine_matrix.rx=sin(DegreesToRadians(fmod((double) degrees,360.0))); |
| 949 | affine_matrix.ry=(-sin(DegreesToRadians(fmod((double) degrees,360.0)))); |
| 950 | affine_matrix.sy=cos(DegreesToRadians(fmod((double) degrees,360.0))); |
| 951 | affine_matrix.tx=0.0; |
| 952 | affine_matrix.ty=0.0; |
| 953 | artifact=GetImageArtifact(image,"deskew:auto-crop"); |
| 954 | if (artifact == (const char *) NULL) |
| 955 | { |
| 956 | deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception); |
| 957 | clone_image=DestroyImage(clone_image); |
| 958 | return(deskew_image); |
| 959 | } |
| 960 | /* |
| 961 | Auto-crop image. |
| 962 | */ |
cristy | f2f2727 | 2009-12-17 14:48:46 +0000 | [diff] [blame] | 963 | GetImageBackgroundColor(clone_image,StringToLong(artifact),exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 964 | deskew_image=AffineTransformImage(clone_image,&affine_matrix,exception); |
| 965 | clone_image=DestroyImage(clone_image); |
| 966 | if (deskew_image == (Image *) NULL) |
| 967 | return((Image *) NULL); |
| 968 | median_image=MedianFilterImage(deskew_image,0.0,exception); |
| 969 | if (median_image == (Image *) NULL) |
| 970 | { |
| 971 | deskew_image=DestroyImage(deskew_image); |
| 972 | return((Image *) NULL); |
| 973 | } |
| 974 | geometry=GetImageBoundingBox(median_image,exception); |
| 975 | median_image=DestroyImage(median_image); |
| 976 | if (image->debug != MagickFalse) |
| 977 | (void) LogMagickEvent(TransformEvent,GetMagickModule()," Deskew geometry: " |
| 978 | "%lux%lu%+ld%+ld",geometry.width,geometry.height,geometry.x,geometry.y); |
| 979 | crop_image=CropImage(deskew_image,&geometry,exception); |
| 980 | deskew_image=DestroyImage(deskew_image); |
| 981 | return(crop_image); |
| 982 | } |
| 983 | |
| 984 | /* |
| 985 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 986 | % % |
| 987 | % % |
| 988 | % % |
| 989 | + I n t e g r a l R o t a t e I m a g e % |
| 990 | % % |
| 991 | % % |
| 992 | % % |
| 993 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 994 | % |
cristy | 3dfa16c | 2010-05-17 19:34:48 +0000 | [diff] [blame] | 995 | % IntegralRotateImage() rotates the image an integral of 90 degrees. It |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 996 | % allocates the memory necessary for the new Image structure and returns a |
| 997 | % pointer to the rotated image. |
| 998 | % |
| 999 | % The format of the IntegralRotateImage method is: |
| 1000 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1001 | % Image *IntegralRotateImage(const Image *image,size_t rotations, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1002 | % ExceptionInfo *exception) |
| 1003 | % |
| 1004 | % A description of each parameter follows. |
| 1005 | % |
| 1006 | % o image: the image. |
| 1007 | % |
| 1008 | % o rotations: Specifies the number of 90 degree rotations. |
| 1009 | % |
| 1010 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1011 | static Image *IntegralRotateImage(const Image *image,size_t rotations, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1012 | ExceptionInfo *exception) |
| 1013 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1014 | #define RotateImageTag "Rotate/Image" |
| 1015 | |
| 1016 | CacheView |
| 1017 | *image_view, |
| 1018 | *rotate_view; |
| 1019 | |
| 1020 | Image |
| 1021 | *rotate_image; |
| 1022 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1023 | MagickBooleanType |
| 1024 | status; |
| 1025 | |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 1026 | MagickOffsetType |
| 1027 | progress; |
| 1028 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1029 | RectangleInfo |
| 1030 | page; |
| 1031 | |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 1032 | ssize_t |
| 1033 | y; |
| 1034 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1035 | /* |
| 1036 | Initialize rotated image attributes. |
| 1037 | */ |
| 1038 | assert(image != (Image *) NULL); |
| 1039 | page=image->page; |
| 1040 | rotations%=4; |
cristy | b32b90a | 2009-09-07 21:45:48 +0000 | [diff] [blame] | 1041 | if (rotations == 0) |
| 1042 | return(CloneImage(image,0,0,MagickTrue,exception)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1043 | if ((rotations == 1) || (rotations == 3)) |
| 1044 | rotate_image=CloneImage(image,image->rows,image->columns,MagickTrue, |
| 1045 | exception); |
| 1046 | else |
| 1047 | rotate_image=CloneImage(image,image->columns,image->rows,MagickTrue, |
| 1048 | exception); |
| 1049 | if (rotate_image == (Image *) NULL) |
| 1050 | return((Image *) NULL); |
| 1051 | /* |
| 1052 | Integral rotate the image. |
| 1053 | */ |
| 1054 | status=MagickTrue; |
| 1055 | progress=0; |
| 1056 | image_view=AcquireCacheView(image); |
| 1057 | rotate_view=AcquireCacheView(rotate_image); |
| 1058 | switch (rotations) |
| 1059 | { |
| 1060 | case 0: |
| 1061 | { |
| 1062 | /* |
| 1063 | Rotate 0 degrees. |
| 1064 | */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1065 | break; |
| 1066 | } |
| 1067 | case 1: |
| 1068 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1069 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1070 | tile_y; |
| 1071 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1072 | size_t |
cristy | b32b90a | 2009-09-07 21:45:48 +0000 | [diff] [blame] | 1073 | tile_height, |
| 1074 | tile_width; |
| 1075 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1076 | /* |
| 1077 | Rotate 90 degrees. |
| 1078 | */ |
cristy | b32b90a | 2009-09-07 21:45:48 +0000 | [diff] [blame] | 1079 | GetPixelCacheTileSize(image,&tile_width,&tile_height); |
cristy | d3dd050 | 2010-04-26 00:57:20 +0000 | [diff] [blame] | 1080 | #if defined(MAGICKCORE_OPENMP_SUPPORT) && defined(MAGICKCORE_FUTURE) |
cristy | 8a7ea36 | 2010-03-10 20:31:43 +0000 | [diff] [blame] | 1081 | #pragma omp parallel for schedule(dynamic,4) shared(progress, status) |
cristy | daa9769 | 2009-09-13 02:10:35 +0000 | [diff] [blame] | 1082 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1083 | for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=tile_height) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1084 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1085 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1086 | tile_x; |
| 1087 | |
| 1088 | if (status == MagickFalse) |
| 1089 | continue; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1090 | for (tile_x=0; tile_x < (ssize_t) image->columns; tile_x+=tile_width) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1091 | { |
| 1092 | MagickBooleanType |
| 1093 | sync; |
| 1094 | |
| 1095 | register const IndexPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1096 | *restrict indexes; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1097 | |
| 1098 | register const PixelPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1099 | *restrict p; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1100 | |
| 1101 | register IndexPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1102 | *restrict rotate_indexes; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1103 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1104 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1105 | y; |
| 1106 | |
| 1107 | register PixelPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1108 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1109 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1110 | size_t |
cristy | b32b90a | 2009-09-07 21:45:48 +0000 | [diff] [blame] | 1111 | height, |
| 1112 | width; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1113 | |
cristy | b32b90a | 2009-09-07 21:45:48 +0000 | [diff] [blame] | 1114 | width=tile_width; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1115 | if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns) |
| 1116 | width=(size_t) (tile_width-(tile_x+tile_width- |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1117 | image->columns)); |
cristy | b32b90a | 2009-09-07 21:45:48 +0000 | [diff] [blame] | 1118 | height=tile_height; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1119 | if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows) |
| 1120 | height=(size_t) (tile_height-(tile_y+tile_height- |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1121 | image->rows)); |
cristy | daa9769 | 2009-09-13 02:10:35 +0000 | [diff] [blame] | 1122 | p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width,height, |
| 1123 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1124 | if (p == (const PixelPacket *) NULL) |
| 1125 | { |
| 1126 | status=MagickFalse; |
| 1127 | break; |
| 1128 | } |
| 1129 | indexes=GetCacheViewVirtualIndexQueue(image_view); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1130 | for (y=0; y < (ssize_t) width; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1131 | { |
| 1132 | register const PixelPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1133 | *restrict tile_pixels; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1134 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1135 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1136 | x; |
| 1137 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1138 | q=QueueCacheViewAuthenticPixels(rotate_view,(ssize_t) |
cristy | b32b90a | 2009-09-07 21:45:48 +0000 | [diff] [blame] | 1139 | rotate_image->columns-(tile_y+height),y+tile_x,height, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1140 | 1,exception); |
| 1141 | if (q == (PixelPacket *) NULL) |
| 1142 | { |
| 1143 | status=MagickFalse; |
| 1144 | break; |
| 1145 | } |
cristy | b32b90a | 2009-09-07 21:45:48 +0000 | [diff] [blame] | 1146 | tile_pixels=p+(height-1)*width+y; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1147 | for (x=0; x < (ssize_t) height; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1148 | { |
| 1149 | *q++=(*tile_pixels); |
cristy | b32b90a | 2009-09-07 21:45:48 +0000 | [diff] [blame] | 1150 | tile_pixels-=width; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1151 | } |
| 1152 | rotate_indexes=GetCacheViewAuthenticIndexQueue(rotate_view); |
| 1153 | if ((indexes != (IndexPacket *) NULL) && |
| 1154 | (rotate_indexes != (IndexPacket *) NULL)) |
| 1155 | { |
| 1156 | register const IndexPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1157 | *restrict tile_indexes; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1158 | |
cristy | b32b90a | 2009-09-07 21:45:48 +0000 | [diff] [blame] | 1159 | tile_indexes=indexes+(height-1)*width+y; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1160 | for (x=0; x < (ssize_t) height; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1161 | { |
| 1162 | *rotate_indexes++=(*tile_indexes); |
cristy | b32b90a | 2009-09-07 21:45:48 +0000 | [diff] [blame] | 1163 | tile_indexes-=width; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1164 | } |
| 1165 | } |
| 1166 | sync=SyncCacheViewAuthenticPixels(rotate_view,exception); |
| 1167 | if (sync == MagickFalse) |
| 1168 | status=MagickFalse; |
| 1169 | } |
| 1170 | } |
| 1171 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 1172 | { |
| 1173 | MagickBooleanType |
| 1174 | proceed; |
| 1175 | |
cristy | b32b90a | 2009-09-07 21:45:48 +0000 | [diff] [blame] | 1176 | proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1177 | image->rows); |
| 1178 | if (proceed == MagickFalse) |
| 1179 | status=MagickFalse; |
| 1180 | } |
| 1181 | } |
cristy | ecc2c14 | 2010-01-17 22:25:46 +0000 | [diff] [blame] | 1182 | (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType) |
| 1183 | image->rows-1,image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1184 | Swap(page.width,page.height); |
| 1185 | Swap(page.x,page.y); |
| 1186 | if (page.width != 0) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1187 | page.x=(ssize_t) (page.width-rotate_image->columns-page.x); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1188 | break; |
| 1189 | } |
| 1190 | case 2: |
| 1191 | { |
| 1192 | /* |
| 1193 | Rotate 180 degrees. |
| 1194 | */ |
cristy | d3dd050 | 2010-04-26 00:57:20 +0000 | [diff] [blame] | 1195 | #if defined(MAGICKCORE_OPENMP_SUPPORT) && defined(MAGICKCORE_FUTURE) |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 1196 | #pragma omp parallel for schedule(dynamic,4) shared(progress,status) |
cristy | daa9769 | 2009-09-13 02:10:35 +0000 | [diff] [blame] | 1197 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1198 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1199 | { |
| 1200 | MagickBooleanType |
| 1201 | sync; |
| 1202 | |
| 1203 | register const IndexPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1204 | *restrict indexes; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1205 | |
| 1206 | register const PixelPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1207 | *restrict p; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1208 | |
| 1209 | register IndexPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1210 | *restrict rotate_indexes; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1211 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1212 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1213 | x; |
| 1214 | |
| 1215 | register PixelPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1216 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1217 | |
| 1218 | if (status == MagickFalse) |
| 1219 | continue; |
| 1220 | p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1, |
| 1221 | exception); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1222 | q=QueueCacheViewAuthenticPixels(rotate_view,0,(ssize_t) (image->rows- |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1223 | y-1),image->columns,1,exception); |
| 1224 | if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL)) |
| 1225 | { |
| 1226 | status=MagickFalse; |
| 1227 | continue; |
| 1228 | } |
| 1229 | indexes=GetCacheViewVirtualIndexQueue(image_view); |
| 1230 | rotate_indexes=GetCacheViewAuthenticIndexQueue(rotate_view); |
| 1231 | q+=image->columns; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1232 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1233 | *--q=(*p++); |
| 1234 | if ((indexes != (IndexPacket *) NULL) && |
| 1235 | (rotate_indexes != (IndexPacket *) NULL)) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1236 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1237 | rotate_indexes[image->columns-x-1]=indexes[x]; |
| 1238 | sync=SyncCacheViewAuthenticPixels(rotate_view,exception); |
| 1239 | if (sync == MagickFalse) |
| 1240 | status=MagickFalse; |
| 1241 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 1242 | { |
| 1243 | MagickBooleanType |
| 1244 | proceed; |
| 1245 | |
| 1246 | proceed=SetImageProgress(image,RotateImageTag,progress++, |
| 1247 | image->rows); |
| 1248 | if (proceed == MagickFalse) |
| 1249 | status=MagickFalse; |
| 1250 | } |
| 1251 | } |
| 1252 | if (page.width != 0) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1253 | page.x=(ssize_t) (page.width-rotate_image->columns-page.x); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1254 | if (page.height != 0) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1255 | page.y=(ssize_t) (page.height-rotate_image->rows-page.y); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1256 | break; |
| 1257 | } |
| 1258 | case 3: |
| 1259 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1260 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1261 | tile_y; |
| 1262 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1263 | size_t |
cristy | b32b90a | 2009-09-07 21:45:48 +0000 | [diff] [blame] | 1264 | tile_height, |
| 1265 | tile_width; |
| 1266 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1267 | /* |
| 1268 | Rotate 270 degrees. |
| 1269 | */ |
cristy | b32b90a | 2009-09-07 21:45:48 +0000 | [diff] [blame] | 1270 | GetPixelCacheTileSize(image,&tile_width,&tile_height); |
cristy | d3dd050 | 2010-04-26 00:57:20 +0000 | [diff] [blame] | 1271 | #if defined(MAGICKCORE_OPENMP_SUPPORT) && defined(MAGICKCORE_FUTURE) |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 1272 | #pragma omp parallel for schedule(dynamic,4) shared(progress,status) |
cristy | daa9769 | 2009-09-13 02:10:35 +0000 | [diff] [blame] | 1273 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1274 | for (tile_y=0; tile_y < (ssize_t) image->rows; tile_y+=tile_height) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1275 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1276 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1277 | tile_x; |
| 1278 | |
| 1279 | if (status == MagickFalse) |
| 1280 | continue; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1281 | for (tile_x=0; tile_x < (ssize_t) image->columns; tile_x+=tile_width) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1282 | { |
| 1283 | MagickBooleanType |
| 1284 | sync; |
| 1285 | |
| 1286 | register const IndexPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1287 | *restrict indexes; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1288 | |
| 1289 | register const PixelPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1290 | *restrict p; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1291 | |
| 1292 | register IndexPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1293 | *restrict rotate_indexes; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1294 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1295 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1296 | y; |
| 1297 | |
| 1298 | register PixelPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1299 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1300 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1301 | size_t |
cristy | b32b90a | 2009-09-07 21:45:48 +0000 | [diff] [blame] | 1302 | height, |
| 1303 | width; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1304 | |
cristy | b32b90a | 2009-09-07 21:45:48 +0000 | [diff] [blame] | 1305 | width=tile_width; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1306 | if ((tile_x+(ssize_t) tile_width) > (ssize_t) image->columns) |
| 1307 | width=(size_t) (tile_width-(tile_x+tile_width- |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1308 | image->columns)); |
cristy | b32b90a | 2009-09-07 21:45:48 +0000 | [diff] [blame] | 1309 | height=tile_height; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1310 | if ((tile_y+(ssize_t) tile_height) > (ssize_t) image->rows) |
| 1311 | height=(size_t) (tile_height-(tile_y+tile_height- |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1312 | image->rows)); |
cristy | b32b90a | 2009-09-07 21:45:48 +0000 | [diff] [blame] | 1313 | p=GetCacheViewVirtualPixels(image_view,tile_x,tile_y,width, |
| 1314 | height,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1315 | if (p == (const PixelPacket *) NULL) |
| 1316 | { |
| 1317 | status=MagickFalse; |
| 1318 | break; |
| 1319 | } |
| 1320 | indexes=GetCacheViewVirtualIndexQueue(image_view); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1321 | for (y=0; y < (ssize_t) width; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1322 | { |
| 1323 | register const PixelPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1324 | *restrict tile_pixels; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1325 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1326 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1327 | x; |
| 1328 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1329 | q=QueueCacheViewAuthenticPixels(rotate_view,tile_y,(ssize_t) |
cristy | b32b90a | 2009-09-07 21:45:48 +0000 | [diff] [blame] | 1330 | y+rotate_image->rows-(tile_x+width),height,1,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1331 | if (q == (PixelPacket *) NULL) |
| 1332 | { |
| 1333 | status=MagickFalse; |
| 1334 | break; |
| 1335 | } |
cristy | b32b90a | 2009-09-07 21:45:48 +0000 | [diff] [blame] | 1336 | tile_pixels=p+(width-1)-y; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1337 | for (x=0; x < (ssize_t) height; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1338 | { |
| 1339 | *q++=(*tile_pixels); |
cristy | b32b90a | 2009-09-07 21:45:48 +0000 | [diff] [blame] | 1340 | tile_pixels+=width; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1341 | } |
| 1342 | rotate_indexes=GetCacheViewAuthenticIndexQueue(rotate_view); |
| 1343 | if ((indexes != (IndexPacket *) NULL) && |
| 1344 | (rotate_indexes != (IndexPacket *) NULL)) |
| 1345 | { |
| 1346 | register const IndexPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1347 | *restrict tile_indexes; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1348 | |
cristy | b32b90a | 2009-09-07 21:45:48 +0000 | [diff] [blame] | 1349 | tile_indexes=indexes+(width-1)-y; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1350 | for (x=0; x < (ssize_t) height; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1351 | { |
| 1352 | *rotate_indexes++=(*tile_indexes); |
cristy | b32b90a | 2009-09-07 21:45:48 +0000 | [diff] [blame] | 1353 | tile_indexes+=width; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1354 | } |
| 1355 | } |
| 1356 | sync=SyncCacheViewAuthenticPixels(rotate_view,exception); |
| 1357 | if (sync == MagickFalse) |
| 1358 | status=MagickFalse; |
| 1359 | } |
| 1360 | } |
| 1361 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 1362 | { |
| 1363 | MagickBooleanType |
| 1364 | proceed; |
| 1365 | |
cristy | b32b90a | 2009-09-07 21:45:48 +0000 | [diff] [blame] | 1366 | proceed=SetImageProgress(image,RotateImageTag,progress+=tile_height, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1367 | image->rows); |
| 1368 | if (proceed == MagickFalse) |
| 1369 | status=MagickFalse; |
| 1370 | } |
| 1371 | } |
cristy | ecc2c14 | 2010-01-17 22:25:46 +0000 | [diff] [blame] | 1372 | (void) SetImageProgress(image,RotateImageTag,(MagickOffsetType) |
| 1373 | image->rows-1,image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1374 | Swap(page.width,page.height); |
| 1375 | Swap(page.x,page.y); |
| 1376 | if (page.height != 0) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1377 | page.y=(ssize_t) (page.height-rotate_image->rows-page.y); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1378 | break; |
| 1379 | } |
| 1380 | } |
| 1381 | rotate_view=DestroyCacheView(rotate_view); |
| 1382 | image_view=DestroyCacheView(image_view); |
| 1383 | rotate_image->type=image->type; |
| 1384 | rotate_image->page=page; |
| 1385 | if (status == MagickFalse) |
| 1386 | rotate_image=DestroyImage(rotate_image); |
| 1387 | return(rotate_image); |
| 1388 | } |
| 1389 | |
| 1390 | /* |
| 1391 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1392 | % % |
| 1393 | % % |
| 1394 | % % |
| 1395 | + X S h e a r I m a g e % |
| 1396 | % % |
| 1397 | % % |
| 1398 | % % |
| 1399 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1400 | % |
| 1401 | % XShearImage() shears the image in the X direction with a shear angle of |
| 1402 | % 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and |
| 1403 | % negative angles shear clockwise. Angles are measured relative to a vertical |
| 1404 | % Y-axis. X shears will widen an image creating 'empty' triangles on the left |
| 1405 | % and right sides of the source image. |
| 1406 | % |
| 1407 | % The format of the XShearImage method is: |
| 1408 | % |
| 1409 | % MagickBooleanType XShearImage(Image *image,const MagickRealType degrees, |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1410 | % const size_t width,const size_t height, |
| 1411 | % const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1412 | % |
| 1413 | % A description of each parameter follows. |
| 1414 | % |
| 1415 | % o image: the image. |
| 1416 | % |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame^] | 1417 | % o degrees: A MagickRealType representing the shearing angle along the X |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1418 | % axis. |
| 1419 | % |
| 1420 | % o width, height, x_offset, y_offset: Defines a region of the image |
| 1421 | % to shear. |
| 1422 | % |
cristy | ecc2c14 | 2010-01-17 22:25:46 +0000 | [diff] [blame] | 1423 | % o exception: return any errors or warnings in this structure. |
| 1424 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1425 | */ |
| 1426 | static MagickBooleanType XShearImage(Image *image,const MagickRealType degrees, |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1427 | const size_t width,const size_t height,const ssize_t x_offset, |
| 1428 | const ssize_t y_offset,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1429 | { |
| 1430 | #define XShearImageTag "XShear/Image" |
| 1431 | |
| 1432 | typedef enum |
| 1433 | { |
| 1434 | LEFT, |
| 1435 | RIGHT |
| 1436 | } ShearDirection; |
| 1437 | |
| 1438 | CacheView |
| 1439 | *image_view; |
| 1440 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1441 | MagickBooleanType |
| 1442 | status; |
| 1443 | |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 1444 | MagickOffsetType |
| 1445 | progress; |
| 1446 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1447 | MagickPixelPacket |
| 1448 | background; |
| 1449 | |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 1450 | ssize_t |
| 1451 | y; |
| 1452 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1453 | assert(image != (Image *) NULL); |
| 1454 | assert(image->signature == MagickSignature); |
| 1455 | if (image->debug != MagickFalse) |
| 1456 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 1457 | GetMagickPixelPacket(image,&background); |
| 1458 | SetMagickPixelPacket(image,&image->background_color,(IndexPacket *) NULL, |
| 1459 | &background); |
| 1460 | if (image->colorspace == CMYKColorspace) |
| 1461 | ConvertRGBToCMYK(&background); |
| 1462 | /* |
| 1463 | XShear image. |
| 1464 | */ |
| 1465 | status=MagickTrue; |
| 1466 | progress=0; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1467 | image_view=AcquireCacheView(image); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 1468 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 1469 | #pragma omp parallel for schedule(dynamic,4) shared(progress, status) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1470 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1471 | for (y=0; y < (ssize_t) height; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1472 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1473 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1474 | step; |
| 1475 | |
| 1476 | MagickPixelPacket |
| 1477 | pixel, |
| 1478 | source, |
| 1479 | destination; |
| 1480 | |
| 1481 | MagickRealType |
| 1482 | area, |
| 1483 | displacement; |
| 1484 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1485 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1486 | i; |
| 1487 | |
| 1488 | register IndexPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1489 | *restrict indexes, |
| 1490 | *restrict shear_indexes; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1491 | |
| 1492 | register PixelPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1493 | *restrict p, |
| 1494 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1495 | |
| 1496 | ShearDirection |
| 1497 | direction; |
| 1498 | |
| 1499 | if (status == MagickFalse) |
| 1500 | continue; |
| 1501 | p=GetCacheViewAuthenticPixels(image_view,0,y_offset+y,image->columns,1, |
| 1502 | exception); |
| 1503 | if (p == (PixelPacket *) NULL) |
| 1504 | { |
| 1505 | status=MagickFalse; |
| 1506 | continue; |
| 1507 | } |
| 1508 | indexes=GetCacheViewAuthenticIndexQueue(image_view); |
| 1509 | p+=x_offset; |
| 1510 | indexes+=x_offset; |
| 1511 | displacement=degrees*(MagickRealType) (y-height/2.0); |
| 1512 | if (displacement == 0.0) |
| 1513 | continue; |
| 1514 | if (displacement > 0.0) |
| 1515 | direction=RIGHT; |
| 1516 | else |
| 1517 | { |
| 1518 | displacement*=(-1.0); |
| 1519 | direction=LEFT; |
| 1520 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1521 | step=(ssize_t) floor((double) displacement); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1522 | area=(MagickRealType) (displacement-step); |
| 1523 | step++; |
| 1524 | pixel=background; |
| 1525 | GetMagickPixelPacket(image,&source); |
| 1526 | GetMagickPixelPacket(image,&destination); |
| 1527 | switch (direction) |
| 1528 | { |
| 1529 | case LEFT: |
| 1530 | { |
| 1531 | /* |
| 1532 | Transfer pixels left-to-right. |
| 1533 | */ |
| 1534 | if (step > x_offset) |
| 1535 | break; |
| 1536 | q=p-step; |
| 1537 | shear_indexes=indexes-step; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1538 | for (i=0; i < (ssize_t) width; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1539 | { |
| 1540 | if ((x_offset+i) < step) |
| 1541 | { |
| 1542 | SetMagickPixelPacket(image,++p,++indexes,&pixel); |
| 1543 | q++; |
| 1544 | shear_indexes++; |
| 1545 | continue; |
| 1546 | } |
| 1547 | SetMagickPixelPacket(image,p,indexes,&source); |
| 1548 | MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity, |
| 1549 | &source,(MagickRealType) p->opacity,area,&destination); |
| 1550 | SetPixelPacket(image,&destination,q++,shear_indexes++); |
| 1551 | SetMagickPixelPacket(image,p++,indexes++,&pixel); |
| 1552 | } |
| 1553 | MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity, |
| 1554 | &background,(MagickRealType) background.opacity,area,&destination); |
| 1555 | SetPixelPacket(image,&destination,q++,shear_indexes++); |
| 1556 | for (i=0; i < (step-1); i++) |
| 1557 | SetPixelPacket(image,&background,q++,shear_indexes++); |
| 1558 | break; |
| 1559 | } |
| 1560 | case RIGHT: |
| 1561 | { |
| 1562 | /* |
| 1563 | Transfer pixels right-to-left. |
| 1564 | */ |
| 1565 | p+=width; |
| 1566 | indexes+=width; |
| 1567 | q=p+step; |
| 1568 | shear_indexes=indexes+step; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1569 | for (i=0; i < (ssize_t) width; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1570 | { |
| 1571 | p--; |
| 1572 | indexes--; |
| 1573 | q--; |
| 1574 | shear_indexes--; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1575 | if ((size_t) (x_offset+width+step-i) >= image->columns) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1576 | continue; |
| 1577 | SetMagickPixelPacket(image,p,indexes,&source); |
| 1578 | MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity, |
| 1579 | &source,(MagickRealType) p->opacity,area,&destination); |
| 1580 | SetPixelPacket(image,&destination,q,shear_indexes); |
| 1581 | SetMagickPixelPacket(image,p,indexes,&pixel); |
| 1582 | } |
| 1583 | MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity, |
| 1584 | &background,(MagickRealType) background.opacity,area,&destination); |
| 1585 | SetPixelPacket(image,&destination,--q,--shear_indexes); |
| 1586 | for (i=0; i < (step-1); i++) |
| 1587 | SetPixelPacket(image,&background,--q,--shear_indexes); |
| 1588 | break; |
| 1589 | } |
| 1590 | } |
| 1591 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
| 1592 | status=MagickFalse; |
| 1593 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 1594 | { |
| 1595 | MagickBooleanType |
| 1596 | proceed; |
| 1597 | |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 1598 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1599 | #pragma omp critical (MagickCore_XShearImage) |
| 1600 | #endif |
| 1601 | proceed=SetImageProgress(image,XShearImageTag,progress++,height); |
| 1602 | if (proceed == MagickFalse) |
| 1603 | status=MagickFalse; |
| 1604 | } |
| 1605 | } |
| 1606 | image_view=DestroyCacheView(image_view); |
| 1607 | return(status); |
| 1608 | } |
| 1609 | |
| 1610 | /* |
| 1611 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1612 | % % |
| 1613 | % % |
| 1614 | % % |
| 1615 | + Y S h e a r I m a g e % |
| 1616 | % % |
| 1617 | % % |
| 1618 | % % |
| 1619 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1620 | % |
| 1621 | % YShearImage shears the image in the Y direction with a shear angle of |
| 1622 | % 'degrees'. Positive angles shear counter-clockwise (right-hand rule), and |
| 1623 | % negative angles shear clockwise. Angles are measured relative to a |
| 1624 | % horizontal X-axis. Y shears will increase the height of an image creating |
| 1625 | % 'empty' triangles on the top and bottom of the source image. |
| 1626 | % |
| 1627 | % The format of the YShearImage method is: |
| 1628 | % |
| 1629 | % MagickBooleanType YShearImage(Image *image,const MagickRealType degrees, |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1630 | % const size_t width,const size_t height, |
| 1631 | % const ssize_t x_offset,const ssize_t y_offset,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1632 | % |
| 1633 | % A description of each parameter follows. |
| 1634 | % |
| 1635 | % o image: the image. |
| 1636 | % |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame^] | 1637 | % o degrees: A MagickRealType representing the shearing angle along the Y |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1638 | % axis. |
| 1639 | % |
| 1640 | % o width, height, x_offset, y_offset: Defines a region of the image |
| 1641 | % to shear. |
| 1642 | % |
cristy | ecc2c14 | 2010-01-17 22:25:46 +0000 | [diff] [blame] | 1643 | % o exception: return any errors or warnings in this structure. |
| 1644 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1645 | */ |
| 1646 | static MagickBooleanType YShearImage(Image *image,const MagickRealType degrees, |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1647 | const size_t width,const size_t height,const ssize_t x_offset, |
| 1648 | const ssize_t y_offset,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1649 | { |
| 1650 | #define YShearImageTag "YShear/Image" |
| 1651 | |
| 1652 | typedef enum |
| 1653 | { |
| 1654 | UP, |
| 1655 | DOWN |
| 1656 | } ShearDirection; |
| 1657 | |
| 1658 | CacheView |
| 1659 | *image_view; |
| 1660 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1661 | MagickBooleanType |
| 1662 | status; |
| 1663 | |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 1664 | MagickOffsetType |
| 1665 | progress; |
| 1666 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1667 | MagickPixelPacket |
| 1668 | background; |
| 1669 | |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 1670 | ssize_t |
| 1671 | x; |
| 1672 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1673 | assert(image != (Image *) NULL); |
| 1674 | assert(image->signature == MagickSignature); |
| 1675 | if (image->debug != MagickFalse) |
| 1676 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 1677 | GetMagickPixelPacket(image,&background); |
| 1678 | SetMagickPixelPacket(image,&image->background_color,(IndexPacket *) NULL, |
| 1679 | &background); |
| 1680 | if (image->colorspace == CMYKColorspace) |
| 1681 | ConvertRGBToCMYK(&background); |
| 1682 | /* |
| 1683 | Y Shear image. |
| 1684 | */ |
| 1685 | status=MagickTrue; |
| 1686 | progress=0; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1687 | image_view=AcquireCacheView(image); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 1688 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 1689 | #pragma omp parallel for schedule(dynamic,4) shared(progress, status) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1690 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1691 | for (x=0; x < (ssize_t) width; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1692 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1693 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1694 | step; |
| 1695 | |
| 1696 | MagickPixelPacket |
| 1697 | pixel, |
| 1698 | source, |
| 1699 | destination; |
| 1700 | |
| 1701 | MagickRealType |
| 1702 | area, |
| 1703 | displacement; |
| 1704 | |
| 1705 | register IndexPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1706 | *restrict indexes, |
| 1707 | *restrict shear_indexes; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1708 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1709 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1710 | i; |
| 1711 | |
| 1712 | register PixelPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1713 | *restrict p, |
| 1714 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1715 | |
| 1716 | ShearDirection |
| 1717 | direction; |
| 1718 | |
| 1719 | if (status == MagickFalse) |
| 1720 | continue; |
| 1721 | p=GetCacheViewAuthenticPixels(image_view,x_offset+x,0,1,image->rows, |
| 1722 | exception); |
| 1723 | if (p == (PixelPacket *) NULL) |
| 1724 | { |
| 1725 | status=MagickFalse; |
| 1726 | continue; |
| 1727 | } |
| 1728 | indexes=GetCacheViewAuthenticIndexQueue(image_view); |
| 1729 | p+=y_offset; |
| 1730 | indexes+=y_offset; |
| 1731 | displacement=degrees*(MagickRealType) (x-width/2.0); |
| 1732 | if (displacement == 0.0) |
| 1733 | continue; |
| 1734 | if (displacement > 0.0) |
| 1735 | direction=DOWN; |
| 1736 | else |
| 1737 | { |
| 1738 | displacement*=(-1.0); |
| 1739 | direction=UP; |
| 1740 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1741 | step=(ssize_t) floor((double) displacement); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1742 | area=(MagickRealType) (displacement-step); |
| 1743 | step++; |
| 1744 | pixel=background; |
| 1745 | GetMagickPixelPacket(image,&source); |
| 1746 | GetMagickPixelPacket(image,&destination); |
| 1747 | switch (direction) |
| 1748 | { |
| 1749 | case UP: |
| 1750 | { |
| 1751 | /* |
| 1752 | Transfer pixels top-to-bottom. |
| 1753 | */ |
| 1754 | if (step > y_offset) |
| 1755 | break; |
| 1756 | q=p-step; |
| 1757 | shear_indexes=indexes-step; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1758 | for (i=0; i < (ssize_t) height; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1759 | { |
| 1760 | if ((y_offset+i) < step) |
| 1761 | { |
| 1762 | SetMagickPixelPacket(image,++p,++indexes,&pixel); |
| 1763 | q++; |
| 1764 | shear_indexes++; |
| 1765 | continue; |
| 1766 | } |
| 1767 | SetMagickPixelPacket(image,p,indexes,&source); |
| 1768 | MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity, |
| 1769 | &source,(MagickRealType) p->opacity,area,&destination); |
| 1770 | SetPixelPacket(image,&destination,q++,shear_indexes++); |
| 1771 | SetMagickPixelPacket(image,p++,indexes++,&pixel); |
| 1772 | } |
| 1773 | MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity, |
| 1774 | &background,(MagickRealType) background.opacity,area,&destination); |
| 1775 | SetPixelPacket(image,&destination,q++,shear_indexes++); |
| 1776 | for (i=0; i < (step-1); i++) |
| 1777 | SetPixelPacket(image,&background,q++,shear_indexes++); |
| 1778 | break; |
| 1779 | } |
| 1780 | case DOWN: |
| 1781 | { |
| 1782 | /* |
| 1783 | Transfer pixels bottom-to-top. |
| 1784 | */ |
| 1785 | p+=height; |
| 1786 | indexes+=height; |
| 1787 | q=p+step; |
| 1788 | shear_indexes=indexes+step; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1789 | for (i=0; i < (ssize_t) height; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1790 | { |
| 1791 | p--; |
| 1792 | indexes--; |
| 1793 | q--; |
| 1794 | shear_indexes--; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1795 | if ((size_t) (y_offset+height+step-i) >= image->rows) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1796 | continue; |
| 1797 | SetMagickPixelPacket(image,p,indexes,&source); |
| 1798 | MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity, |
| 1799 | &source,(MagickRealType) p->opacity,area,&destination); |
| 1800 | SetPixelPacket(image,&destination,q,shear_indexes); |
| 1801 | SetMagickPixelPacket(image,p,indexes,&pixel); |
| 1802 | } |
| 1803 | MagickPixelCompositeAreaBlend(&pixel,(MagickRealType) pixel.opacity, |
| 1804 | &background,(MagickRealType) background.opacity,area,&destination); |
| 1805 | SetPixelPacket(image,&destination,--q,--shear_indexes); |
| 1806 | for (i=0; i < (step-1); i++) |
| 1807 | SetPixelPacket(image,&background,--q,--shear_indexes); |
| 1808 | break; |
| 1809 | } |
| 1810 | } |
| 1811 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
| 1812 | status=MagickFalse; |
| 1813 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 1814 | { |
| 1815 | MagickBooleanType |
| 1816 | proceed; |
| 1817 | |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 1818 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1819 | #pragma omp critical (MagickCore_YShearImage) |
| 1820 | #endif |
| 1821 | proceed=SetImageProgress(image,YShearImageTag,progress++,image->rows); |
| 1822 | if (proceed == MagickFalse) |
| 1823 | status=MagickFalse; |
| 1824 | } |
| 1825 | } |
| 1826 | image_view=DestroyCacheView(image_view); |
| 1827 | return(status); |
| 1828 | } |
| 1829 | |
| 1830 | /* |
| 1831 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1832 | % % |
| 1833 | % % |
| 1834 | % % |
| 1835 | % R o t a t e I m a g e % |
| 1836 | % % |
| 1837 | % % |
| 1838 | % % |
| 1839 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1840 | % |
| 1841 | % RotateImage() creates a new image that is a rotated copy of an existing |
| 1842 | % one. Positive angles rotate counter-clockwise (right-hand rule), while |
| 1843 | % negative angles rotate clockwise. Rotated images are usually larger than |
| 1844 | % the originals and have 'empty' triangular corners. X axis. Empty |
| 1845 | % triangles left over from shearing the image are filled with the background |
| 1846 | % color defined by member 'background_color' of the image. RotateImage |
| 1847 | % allocates the memory necessary for the new Image structure and returns a |
| 1848 | % pointer to the new image. |
| 1849 | % |
| 1850 | % RotateImage() is based on the paper "A Fast Algorithm for General |
| 1851 | % Raster Rotatation" by Alan W. Paeth. RotateImage is adapted from a similar |
| 1852 | % method based on the Paeth paper written by Michael Halle of the Spatial |
| 1853 | % Imaging Group, MIT Media Lab. |
| 1854 | % |
| 1855 | % The format of the RotateImage method is: |
| 1856 | % |
| 1857 | % Image *RotateImage(const Image *image,const double degrees, |
| 1858 | % ExceptionInfo *exception) |
| 1859 | % |
| 1860 | % A description of each parameter follows. |
| 1861 | % |
| 1862 | % o image: the image. |
| 1863 | % |
| 1864 | % o degrees: Specifies the number of degrees to rotate the image. |
| 1865 | % |
| 1866 | % o exception: return any errors or warnings in this structure. |
| 1867 | % |
| 1868 | */ |
| 1869 | MagickExport Image *RotateImage(const Image *image,const double degrees, |
| 1870 | ExceptionInfo *exception) |
| 1871 | { |
| 1872 | Image |
| 1873 | *integral_image, |
| 1874 | *rotate_image; |
| 1875 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1876 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1877 | x_offset, |
| 1878 | y_offset; |
| 1879 | |
cristy | ecc2c14 | 2010-01-17 22:25:46 +0000 | [diff] [blame] | 1880 | MagickBooleanType |
| 1881 | status; |
| 1882 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1883 | MagickRealType |
| 1884 | angle; |
| 1885 | |
| 1886 | PointInfo |
| 1887 | shear; |
| 1888 | |
| 1889 | RectangleInfo |
| 1890 | border_info; |
| 1891 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1892 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1893 | height, |
| 1894 | rotations, |
| 1895 | width, |
| 1896 | y_width; |
| 1897 | |
| 1898 | /* |
| 1899 | Adjust rotation angle. |
| 1900 | */ |
| 1901 | assert(image != (Image *) NULL); |
| 1902 | assert(image->signature == MagickSignature); |
| 1903 | if (image->debug != MagickFalse) |
| 1904 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 1905 | assert(exception != (ExceptionInfo *) NULL); |
| 1906 | assert(exception->signature == MagickSignature); |
| 1907 | angle=degrees; |
| 1908 | while (angle < -45.0) |
| 1909 | angle+=360.0; |
| 1910 | for (rotations=0; angle > 45.0; rotations++) |
| 1911 | angle-=90.0; |
| 1912 | rotations%=4; |
| 1913 | /* |
| 1914 | Calculate shear equations. |
| 1915 | */ |
| 1916 | integral_image=IntegralRotateImage(image,rotations,exception); |
| 1917 | if (integral_image == (Image *) NULL) |
| 1918 | ThrowImageException(ResourceLimitError,"MemoryAllocationFailed"); |
| 1919 | shear.x=(-tan((double) DegreesToRadians(angle)/2.0)); |
| 1920 | shear.y=sin((double) DegreesToRadians(angle)); |
| 1921 | if ((shear.x == 0.0) && (shear.y == 0.0)) |
| 1922 | return(integral_image); |
| 1923 | if (SetImageStorageClass(integral_image,DirectClass) == MagickFalse) |
| 1924 | { |
| 1925 | InheritException(exception,&integral_image->exception); |
| 1926 | integral_image=DestroyImage(integral_image); |
| 1927 | return(integral_image); |
| 1928 | } |
| 1929 | if (integral_image->matte == MagickFalse) |
| 1930 | (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel); |
| 1931 | /* |
| 1932 | Compute image size. |
| 1933 | */ |
| 1934 | width=image->columns; |
| 1935 | height=image->rows; |
| 1936 | if ((rotations == 1) || (rotations == 3)) |
| 1937 | { |
| 1938 | width=image->rows; |
| 1939 | height=image->columns; |
| 1940 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1941 | y_width=width+(ssize_t) floor(fabs(shear.x)*height+0.5); |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame^] | 1942 | x_offset=(ssize_t) ceil((double) width+((fabs(shear.y)*height)-width)/2.0- |
| 1943 | 0.5); |
| 1944 | y_offset=(ssize_t) ceil((double) height+((fabs(shear.y)*y_width)-height)/2.0- |
| 1945 | 0.5); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1946 | /* |
| 1947 | Surround image with a border. |
| 1948 | */ |
| 1949 | integral_image->border_color=integral_image->background_color; |
| 1950 | integral_image->compose=CopyCompositeOp; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1951 | border_info.width=(size_t) x_offset; |
| 1952 | border_info.height=(size_t) y_offset; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1953 | rotate_image=BorderImage(integral_image,&border_info,exception); |
| 1954 | integral_image=DestroyImage(integral_image); |
| 1955 | if (rotate_image == (Image *) NULL) |
| 1956 | ThrowImageException(ResourceLimitError,"MemoryAllocationFailed"); |
| 1957 | /* |
| 1958 | Rotate the image. |
| 1959 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1960 | status=XShearImage(rotate_image,shear.x,width,height,x_offset,((ssize_t) |
cristy | ecc2c14 | 2010-01-17 22:25:46 +0000 | [diff] [blame] | 1961 | rotate_image->rows-height)/2,exception); |
| 1962 | if (status == MagickFalse) |
| 1963 | { |
| 1964 | rotate_image=DestroyImage(rotate_image); |
| 1965 | return((Image *) NULL); |
| 1966 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1967 | status=YShearImage(rotate_image,shear.y,y_width,height,((ssize_t) |
cristy | ecc2c14 | 2010-01-17 22:25:46 +0000 | [diff] [blame] | 1968 | rotate_image->columns-y_width)/2,y_offset,exception); |
| 1969 | if (status == MagickFalse) |
| 1970 | { |
| 1971 | rotate_image=DestroyImage(rotate_image); |
| 1972 | return((Image *) NULL); |
| 1973 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1974 | status=XShearImage(rotate_image,shear.x,y_width,rotate_image->rows,((ssize_t) |
cristy | ecc2c14 | 2010-01-17 22:25:46 +0000 | [diff] [blame] | 1975 | rotate_image->columns-y_width)/2,0,exception); |
| 1976 | if (status == MagickFalse) |
| 1977 | { |
| 1978 | rotate_image=DestroyImage(rotate_image); |
| 1979 | return((Image *) NULL); |
| 1980 | } |
| 1981 | status=CropToFitImage(&rotate_image,shear.x,shear.y,(MagickRealType) width, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1982 | (MagickRealType) height,MagickTrue,exception); |
cristy | ecc2c14 | 2010-01-17 22:25:46 +0000 | [diff] [blame] | 1983 | if (status == MagickFalse) |
| 1984 | { |
| 1985 | rotate_image=DestroyImage(rotate_image); |
| 1986 | return((Image *) NULL); |
| 1987 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1988 | rotate_image->compose=image->compose; |
| 1989 | rotate_image->page.width=0; |
| 1990 | rotate_image->page.height=0; |
| 1991 | return(rotate_image); |
| 1992 | } |
| 1993 | |
| 1994 | /* |
| 1995 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1996 | % % |
| 1997 | % % |
| 1998 | % % |
| 1999 | % S h e a r I m a g e % |
| 2000 | % % |
| 2001 | % % |
| 2002 | % % |
| 2003 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2004 | % |
| 2005 | % ShearImage() creates a new image that is a shear_image copy of an existing |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame^] | 2006 | % one. Shearing slides one edge of an image along the X or Y axis, creating |
| 2007 | % a parallelogram. An X direction shear slides an edge along the X axis, |
| 2008 | % while a Y direction shear slides an edge along the Y axis. The amount of |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2009 | % the shear is controlled by a shear angle. For X direction shears, x_shear |
| 2010 | % is measured relative to the Y axis, and similarly, for Y direction shears |
| 2011 | % y_shear is measured relative to the X axis. Empty triangles left over from |
| 2012 | % shearing the image are filled with the background color defined by member |
| 2013 | % 'background_color' of the image.. ShearImage() allocates the memory |
| 2014 | % necessary for the new Image structure and returns a pointer to the new image. |
| 2015 | % |
| 2016 | % ShearImage() is based on the paper "A Fast Algorithm for General Raster |
| 2017 | % Rotatation" by Alan W. Paeth. |
| 2018 | % |
| 2019 | % The format of the ShearImage method is: |
| 2020 | % |
| 2021 | % Image *ShearImage(const Image *image,const double x_shear, |
| 2022 | % const double y_shear,ExceptionInfo *exception) |
| 2023 | % |
| 2024 | % A description of each parameter follows. |
| 2025 | % |
| 2026 | % o image: the image. |
| 2027 | % |
| 2028 | % o x_shear, y_shear: Specifies the number of degrees to shear the image. |
| 2029 | % |
| 2030 | % o exception: return any errors or warnings in this structure. |
| 2031 | % |
| 2032 | */ |
| 2033 | MagickExport Image *ShearImage(const Image *image,const double x_shear, |
| 2034 | const double y_shear,ExceptionInfo *exception) |
| 2035 | { |
| 2036 | Image |
| 2037 | *integral_image, |
| 2038 | *shear_image; |
| 2039 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2040 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2041 | x_offset, |
| 2042 | y_offset; |
| 2043 | |
cristy | ecc2c14 | 2010-01-17 22:25:46 +0000 | [diff] [blame] | 2044 | MagickBooleanType |
| 2045 | status; |
| 2046 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2047 | PointInfo |
| 2048 | shear; |
| 2049 | |
| 2050 | RectangleInfo |
| 2051 | border_info; |
| 2052 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2053 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2054 | y_width; |
| 2055 | |
| 2056 | assert(image != (Image *) NULL); |
| 2057 | assert(image->signature == MagickSignature); |
| 2058 | if (image->debug != MagickFalse) |
| 2059 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 2060 | assert(exception != (ExceptionInfo *) NULL); |
| 2061 | assert(exception->signature == MagickSignature); |
| 2062 | if ((x_shear != 0.0) && (fmod(x_shear,90.0) == 0.0)) |
| 2063 | ThrowImageException(ImageError,"AngleIsDiscontinuous"); |
| 2064 | if ((y_shear != 0.0) && (fmod(y_shear,90.0) == 0.0)) |
| 2065 | ThrowImageException(ImageError,"AngleIsDiscontinuous"); |
| 2066 | /* |
| 2067 | Initialize shear angle. |
| 2068 | */ |
| 2069 | integral_image=CloneImage(image,0,0,MagickTrue,exception); |
| 2070 | if (integral_image == (Image *) NULL) |
| 2071 | ThrowImageException(ResourceLimitError,"MemoryAllocationFailed"); |
| 2072 | shear.x=(-tan(DegreesToRadians(fmod(x_shear,360.0)))); |
| 2073 | shear.y=tan(DegreesToRadians(fmod(y_shear,360.0))); |
| 2074 | if ((shear.x == 0.0) && (shear.y == 0.0)) |
| 2075 | return(integral_image); |
| 2076 | if (SetImageStorageClass(integral_image,DirectClass) == MagickFalse) |
| 2077 | { |
| 2078 | InheritException(exception,&integral_image->exception); |
| 2079 | integral_image=DestroyImage(integral_image); |
| 2080 | return(integral_image); |
| 2081 | } |
| 2082 | if (integral_image->matte == MagickFalse) |
| 2083 | (void) SetImageAlphaChannel(integral_image,OpaqueAlphaChannel); |
| 2084 | /* |
| 2085 | Compute image size. |
| 2086 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2087 | y_width=image->columns+(ssize_t) floor(fabs(shear.x)*image->rows+0.5); |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame^] | 2088 | x_offset=(ssize_t) ceil((double) image->columns+((fabs(shear.x)*image->rows)- |
cristy | 06609ee | 2010-03-17 20:21:27 +0000 | [diff] [blame] | 2089 | image->columns)/2.0-0.5); |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame^] | 2090 | y_offset=(ssize_t) ceil((double) image->rows+((fabs(shear.y)*y_width)- |
| 2091 | image->rows)/2.0-0.5); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2092 | /* |
| 2093 | Surround image with border. |
| 2094 | */ |
| 2095 | integral_image->border_color=integral_image->background_color; |
| 2096 | integral_image->compose=CopyCompositeOp; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2097 | border_info.width=(size_t) x_offset; |
| 2098 | border_info.height=(size_t) y_offset; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2099 | shear_image=BorderImage(integral_image,&border_info,exception); |
cristy | ecc2c14 | 2010-01-17 22:25:46 +0000 | [diff] [blame] | 2100 | integral_image=DestroyImage(integral_image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2101 | if (shear_image == (Image *) NULL) |
| 2102 | ThrowImageException(ResourceLimitError,"MemoryAllocationFailed"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2103 | /* |
| 2104 | Shear the image. |
| 2105 | */ |
| 2106 | if (shear_image->matte == MagickFalse) |
| 2107 | (void) SetImageAlphaChannel(shear_image,OpaqueAlphaChannel); |
cristy | ecc2c14 | 2010-01-17 22:25:46 +0000 | [diff] [blame] | 2108 | status=XShearImage(shear_image,shear.x,image->columns,image->rows,x_offset, |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2109 | ((ssize_t) shear_image->rows-image->rows)/2,exception); |
cristy | ecc2c14 | 2010-01-17 22:25:46 +0000 | [diff] [blame] | 2110 | if (status == MagickFalse) |
| 2111 | { |
| 2112 | shear_image=DestroyImage(shear_image); |
| 2113 | return((Image *) NULL); |
| 2114 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2115 | status=YShearImage(shear_image,shear.y,y_width,image->rows,((ssize_t) |
cristy | ecc2c14 | 2010-01-17 22:25:46 +0000 | [diff] [blame] | 2116 | shear_image->columns-y_width)/2,y_offset,exception); |
| 2117 | if (status == MagickFalse) |
| 2118 | { |
| 2119 | shear_image=DestroyImage(shear_image); |
| 2120 | return((Image *) NULL); |
| 2121 | } |
| 2122 | status=CropToFitImage(&shear_image,shear.x,shear.y,(MagickRealType) |
| 2123 | image->columns,(MagickRealType) image->rows,MagickFalse,exception); |
| 2124 | if (status == MagickFalse) |
| 2125 | { |
| 2126 | shear_image=DestroyImage(shear_image); |
| 2127 | return((Image *) NULL); |
| 2128 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2129 | shear_image->compose=image->compose; |
| 2130 | shear_image->page.width=0; |
| 2131 | shear_image->page.height=0; |
| 2132 | return(shear_image); |
| 2133 | } |