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