cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % % |
| 6 | % SSSSS TTTTT AAA TTTTT IIIII SSSSS TTTTT IIIII CCCC % |
| 7 | % SS T A A T I SS T I C % |
| 8 | % SSS T AAAAA T I SSS T I C % |
| 9 | % SS T A A T I SS T I C % |
| 10 | % SSSSS T A A T IIIII SSSSS T IIIII CCCC % |
| 11 | % % |
| 12 | % % |
cristy | 3e2860c | 2010-01-24 01:36:30 +0000 | [diff] [blame] | 13 | % MagickCore Image Statistical Methods % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 14 | % % |
| 15 | % Software Design % |
| 16 | % John Cristy % |
| 17 | % July 1992 % |
| 18 | % % |
| 19 | % % |
cristy | 1454be7 | 2011-12-19 01:52:48 +0000 | [diff] [blame] | 20 | % Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 21 | % dedicated to making software imaging solutions freely available. % |
| 22 | % % |
| 23 | % You may not use this file except in compliance with the License. You may % |
| 24 | % obtain a copy of the License at % |
| 25 | % % |
| 26 | % http://www.imagemagick.org/script/license.php % |
| 27 | % % |
| 28 | % Unless required by applicable law or agreed to in writing, software % |
| 29 | % distributed under the License is distributed on an "AS IS" BASIS, % |
| 30 | % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. % |
| 31 | % See the License for the specific language governing permissions and % |
| 32 | % limitations under the License. % |
| 33 | % % |
| 34 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 35 | % |
| 36 | % |
| 37 | % |
| 38 | */ |
| 39 | |
| 40 | /* |
| 41 | Include declarations. |
| 42 | */ |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 43 | #include "MagickCore/studio.h" |
| 44 | #include "MagickCore/property.h" |
| 45 | #include "MagickCore/animate.h" |
| 46 | #include "MagickCore/blob.h" |
| 47 | #include "MagickCore/blob-private.h" |
| 48 | #include "MagickCore/cache.h" |
| 49 | #include "MagickCore/cache-private.h" |
| 50 | #include "MagickCore/cache-view.h" |
| 51 | #include "MagickCore/client.h" |
| 52 | #include "MagickCore/color.h" |
| 53 | #include "MagickCore/color-private.h" |
| 54 | #include "MagickCore/colorspace.h" |
| 55 | #include "MagickCore/colorspace-private.h" |
| 56 | #include "MagickCore/composite.h" |
| 57 | #include "MagickCore/composite-private.h" |
| 58 | #include "MagickCore/compress.h" |
| 59 | #include "MagickCore/constitute.h" |
| 60 | #include "MagickCore/display.h" |
| 61 | #include "MagickCore/draw.h" |
| 62 | #include "MagickCore/enhance.h" |
| 63 | #include "MagickCore/exception.h" |
| 64 | #include "MagickCore/exception-private.h" |
| 65 | #include "MagickCore/gem.h" |
cristy | 8ea8122 | 2011-09-04 10:33:32 +0000 | [diff] [blame] | 66 | #include "MagickCore/gem-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 67 | #include "MagickCore/geometry.h" |
| 68 | #include "MagickCore/list.h" |
| 69 | #include "MagickCore/image-private.h" |
| 70 | #include "MagickCore/magic.h" |
| 71 | #include "MagickCore/magick.h" |
| 72 | #include "MagickCore/memory_.h" |
| 73 | #include "MagickCore/module.h" |
| 74 | #include "MagickCore/monitor.h" |
| 75 | #include "MagickCore/monitor-private.h" |
| 76 | #include "MagickCore/option.h" |
| 77 | #include "MagickCore/paint.h" |
| 78 | #include "MagickCore/pixel-accessor.h" |
| 79 | #include "MagickCore/profile.h" |
| 80 | #include "MagickCore/quantize.h" |
| 81 | #include "MagickCore/quantum-private.h" |
| 82 | #include "MagickCore/random_.h" |
| 83 | #include "MagickCore/random-private.h" |
cristy | 57340e0 | 2012-05-05 00:53:23 +0000 | [diff] [blame] | 84 | #include "MagickCore/resource_.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 85 | #include "MagickCore/segment.h" |
| 86 | #include "MagickCore/semaphore.h" |
| 87 | #include "MagickCore/signature-private.h" |
| 88 | #include "MagickCore/statistic.h" |
| 89 | #include "MagickCore/string_.h" |
| 90 | #include "MagickCore/thread-private.h" |
| 91 | #include "MagickCore/timer.h" |
| 92 | #include "MagickCore/utility.h" |
| 93 | #include "MagickCore/version.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 94 | |
| 95 | /* |
| 96 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 97 | % % |
| 98 | % % |
| 99 | % % |
cristy | d18ae7c | 2010-03-07 17:39:52 +0000 | [diff] [blame] | 100 | % E v a l u a t e I m a g e % |
cristy | 316d517 | 2009-09-17 19:31:25 +0000 | [diff] [blame] | 101 | % % |
| 102 | % % |
| 103 | % % |
| 104 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 105 | % |
cristy | d18ae7c | 2010-03-07 17:39:52 +0000 | [diff] [blame] | 106 | % EvaluateImage() applies a value to the image with an arithmetic, relational, |
| 107 | % or logical operator to an image. Use these operations to lighten or darken |
| 108 | % an image, to increase or decrease contrast in an image, or to produce the |
| 109 | % "negative" of an image. |
cristy | 316d517 | 2009-09-17 19:31:25 +0000 | [diff] [blame] | 110 | % |
cristy | d42d995 | 2011-07-08 14:21:50 +0000 | [diff] [blame] | 111 | % The format of the EvaluateImage method is: |
cristy | 316d517 | 2009-09-17 19:31:25 +0000 | [diff] [blame] | 112 | % |
cristy | d18ae7c | 2010-03-07 17:39:52 +0000 | [diff] [blame] | 113 | % MagickBooleanType EvaluateImage(Image *image, |
| 114 | % const MagickEvaluateOperator op,const double value, |
| 115 | % ExceptionInfo *exception) |
| 116 | % MagickBooleanType EvaluateImages(Image *images, |
| 117 | % const MagickEvaluateOperator op,const double value, |
| 118 | % ExceptionInfo *exception) |
cristy | 316d517 | 2009-09-17 19:31:25 +0000 | [diff] [blame] | 119 | % |
| 120 | % A description of each parameter follows: |
| 121 | % |
cristy | d18ae7c | 2010-03-07 17:39:52 +0000 | [diff] [blame] | 122 | % o image: the image. |
| 123 | % |
cristy | d18ae7c | 2010-03-07 17:39:52 +0000 | [diff] [blame] | 124 | % o op: A channel op. |
| 125 | % |
| 126 | % o value: A value value. |
cristy | 316d517 | 2009-09-17 19:31:25 +0000 | [diff] [blame] | 127 | % |
| 128 | % o exception: return any errors or warnings in this structure. |
| 129 | % |
| 130 | */ |
| 131 | |
cristy | 95a072b | 2011-10-13 18:03:11 +0000 | [diff] [blame] | 132 | typedef struct _PixelChannels |
cristy | ba18a7a | 2011-09-25 15:43:43 +0000 | [diff] [blame] | 133 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 134 | double |
cristy | 5f95f4f | 2011-10-23 01:01:01 +0000 | [diff] [blame] | 135 | channel[CompositePixelChannel]; |
cristy | 95a072b | 2011-10-13 18:03:11 +0000 | [diff] [blame] | 136 | } PixelChannels; |
cristy | ba18a7a | 2011-09-25 15:43:43 +0000 | [diff] [blame] | 137 | |
cristy | 95a072b | 2011-10-13 18:03:11 +0000 | [diff] [blame] | 138 | static PixelChannels **DestroyPixelThreadSet(PixelChannels **pixels) |
cristy | 316d517 | 2009-09-17 19:31:25 +0000 | [diff] [blame] | 139 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 140 | register ssize_t |
cristy | 316d517 | 2009-09-17 19:31:25 +0000 | [diff] [blame] | 141 | i; |
| 142 | |
cristy | 95a072b | 2011-10-13 18:03:11 +0000 | [diff] [blame] | 143 | assert(pixels != (PixelChannels **) NULL); |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 144 | for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++) |
cristy | 95a072b | 2011-10-13 18:03:11 +0000 | [diff] [blame] | 145 | if (pixels[i] != (PixelChannels *) NULL) |
| 146 | pixels[i]=(PixelChannels *) RelinquishMagickMemory(pixels[i]); |
| 147 | pixels=(PixelChannels **) RelinquishMagickMemory(pixels); |
cristy | 316d517 | 2009-09-17 19:31:25 +0000 | [diff] [blame] | 148 | return(pixels); |
| 149 | } |
| 150 | |
cristy | 95a072b | 2011-10-13 18:03:11 +0000 | [diff] [blame] | 151 | static PixelChannels **AcquirePixelThreadSet(const Image *image, |
cristy | 08a3d70 | 2010-11-28 01:57:36 +0000 | [diff] [blame] | 152 | const size_t number_images) |
cristy | 316d517 | 2009-09-17 19:31:25 +0000 | [diff] [blame] | 153 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 154 | register ssize_t |
cristy | ba18a7a | 2011-09-25 15:43:43 +0000 | [diff] [blame] | 155 | i; |
cristy | 316d517 | 2009-09-17 19:31:25 +0000 | [diff] [blame] | 156 | |
cristy | 95a072b | 2011-10-13 18:03:11 +0000 | [diff] [blame] | 157 | PixelChannels |
cristy | 316d517 | 2009-09-17 19:31:25 +0000 | [diff] [blame] | 158 | **pixels; |
| 159 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 160 | size_t |
cristy | 08a3d70 | 2010-11-28 01:57:36 +0000 | [diff] [blame] | 161 | length, |
cristy | 316d517 | 2009-09-17 19:31:25 +0000 | [diff] [blame] | 162 | number_threads; |
| 163 | |
cristy | 9357bdd | 2012-07-30 12:28:34 +0000 | [diff] [blame] | 164 | number_threads=(size_t) GetMagickResourceLimit(ThreadResource); |
cristy | e2a912b | 2011-12-05 20:02:07 +0000 | [diff] [blame] | 165 | pixels=(PixelChannels **) AcquireQuantumMemory(number_threads, |
| 166 | sizeof(*pixels)); |
cristy | 95a072b | 2011-10-13 18:03:11 +0000 | [diff] [blame] | 167 | if (pixels == (PixelChannels **) NULL) |
| 168 | return((PixelChannels **) NULL); |
cristy | 316d517 | 2009-09-17 19:31:25 +0000 | [diff] [blame] | 169 | (void) ResetMagickMemory(pixels,0,number_threads*sizeof(*pixels)); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 170 | for (i=0; i < (ssize_t) number_threads; i++) |
cristy | 316d517 | 2009-09-17 19:31:25 +0000 | [diff] [blame] | 171 | { |
cristy | ba18a7a | 2011-09-25 15:43:43 +0000 | [diff] [blame] | 172 | register ssize_t |
| 173 | j; |
| 174 | |
cristy | 08a3d70 | 2010-11-28 01:57:36 +0000 | [diff] [blame] | 175 | length=image->columns; |
| 176 | if (length < number_images) |
| 177 | length=number_images; |
cristy | 95a072b | 2011-10-13 18:03:11 +0000 | [diff] [blame] | 178 | pixels[i]=(PixelChannels *) AcquireQuantumMemory(length,sizeof(**pixels)); |
| 179 | if (pixels[i] == (PixelChannels *) NULL) |
cristy | 316d517 | 2009-09-17 19:31:25 +0000 | [diff] [blame] | 180 | return(DestroyPixelThreadSet(pixels)); |
cristy | 08a3d70 | 2010-11-28 01:57:36 +0000 | [diff] [blame] | 181 | for (j=0; j < (ssize_t) length; j++) |
cristy | ba18a7a | 2011-09-25 15:43:43 +0000 | [diff] [blame] | 182 | { |
| 183 | register ssize_t |
| 184 | k; |
| 185 | |
| 186 | for (k=0; k < MaxPixelChannels; k++) |
| 187 | pixels[i][j].channel[k]=0.0; |
| 188 | } |
cristy | 316d517 | 2009-09-17 19:31:25 +0000 | [diff] [blame] | 189 | } |
| 190 | return(pixels); |
| 191 | } |
| 192 | |
cristy | 99bd523 | 2011-12-07 14:38:20 +0000 | [diff] [blame] | 193 | static inline double EvaluateMax(const double x,const double y) |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 194 | { |
| 195 | if (x > y) |
| 196 | return(x); |
| 197 | return(y); |
| 198 | } |
| 199 | |
cristy | 08a3d70 | 2010-11-28 01:57:36 +0000 | [diff] [blame] | 200 | #if defined(__cplusplus) || defined(c_plusplus) |
| 201 | extern "C" { |
| 202 | #endif |
| 203 | |
| 204 | static int IntensityCompare(const void *x,const void *y) |
| 205 | { |
cristy | 95a072b | 2011-10-13 18:03:11 +0000 | [diff] [blame] | 206 | const PixelChannels |
cristy | 08a3d70 | 2010-11-28 01:57:36 +0000 | [diff] [blame] | 207 | *color_1, |
| 208 | *color_2; |
| 209 | |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 210 | double |
cristy | ba18a7a | 2011-09-25 15:43:43 +0000 | [diff] [blame] | 211 | distance; |
cristy | 08a3d70 | 2010-11-28 01:57:36 +0000 | [diff] [blame] | 212 | |
cristy | ba18a7a | 2011-09-25 15:43:43 +0000 | [diff] [blame] | 213 | register ssize_t |
| 214 | i; |
| 215 | |
cristy | 95a072b | 2011-10-13 18:03:11 +0000 | [diff] [blame] | 216 | color_1=(const PixelChannels *) x; |
| 217 | color_2=(const PixelChannels *) y; |
cristy | ba18a7a | 2011-09-25 15:43:43 +0000 | [diff] [blame] | 218 | distance=0.0; |
| 219 | for (i=0; i < MaxPixelChannels; i++) |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 220 | distance+=color_1->channel[i]-(double) color_2->channel[i]; |
cristy | ba18a7a | 2011-09-25 15:43:43 +0000 | [diff] [blame] | 221 | return(distance < 0 ? -1 : distance > 0 ? 1 : 0); |
cristy | 08a3d70 | 2010-11-28 01:57:36 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | #if defined(__cplusplus) || defined(c_plusplus) |
| 225 | } |
| 226 | #endif |
| 227 | |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 228 | static inline double MagickMin(const double x,const double y) |
| 229 | { |
| 230 | if (x < y) |
| 231 | return(x); |
| 232 | return(y); |
| 233 | } |
| 234 | |
cristy | 84ff57f | 2012-11-06 02:01:48 +0000 | [diff] [blame] | 235 | static double ApplyEvaluateOperator(RandomInfo *random_info,const Quantum pixel, |
| 236 | const MagickEvaluateOperator op,const double value) |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 237 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 238 | double |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 239 | result; |
| 240 | |
| 241 | result=0.0; |
| 242 | switch (op) |
| 243 | { |
| 244 | case UndefinedEvaluateOperator: |
| 245 | break; |
cristy | 33aaed2 | 2010-08-11 18:10:50 +0000 | [diff] [blame] | 246 | case AbsEvaluateOperator: |
| 247 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 248 | result=(double) fabs((double) (pixel+value)); |
cristy | 33aaed2 | 2010-08-11 18:10:50 +0000 | [diff] [blame] | 249 | break; |
| 250 | } |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 251 | case AddEvaluateOperator: |
| 252 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 253 | result=(double) (pixel+value); |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 254 | break; |
| 255 | } |
| 256 | case AddModulusEvaluateOperator: |
| 257 | { |
| 258 | /* |
cristy | e2a912b | 2011-12-05 20:02:07 +0000 | [diff] [blame] | 259 | This returns a 'floored modulus' of the addition which is a positive |
| 260 | result. It differs from % or fmod() that returns a 'truncated modulus' |
| 261 | result, where floor() is replaced by trunc() and could return a |
| 262 | negative result (which is clipped). |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 263 | */ |
| 264 | result=pixel+value; |
cristy | 364a8e2 | 2010-05-10 17:06:33 +0000 | [diff] [blame] | 265 | result-=(QuantumRange+1.0)*floor((double) result/(QuantumRange+1.0)); |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 266 | break; |
| 267 | } |
| 268 | case AndEvaluateOperator: |
| 269 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 270 | result=(double) ((size_t) pixel & (size_t) (value+0.5)); |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 271 | break; |
| 272 | } |
| 273 | case CosineEvaluateOperator: |
| 274 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 275 | result=(double) (QuantumRange*(0.5*cos((double) (2.0*MagickPI* |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 276 | QuantumScale*pixel*value))+0.5)); |
| 277 | break; |
| 278 | } |
| 279 | case DivideEvaluateOperator: |
| 280 | { |
| 281 | result=pixel/(value == 0.0 ? 1.0 : value); |
| 282 | break; |
| 283 | } |
cristy | 9fe8cd7 | 2010-10-19 01:24:07 +0000 | [diff] [blame] | 284 | case ExponentialEvaluateOperator: |
| 285 | { |
cristy | 84ff57f | 2012-11-06 02:01:48 +0000 | [diff] [blame] | 286 | result=(double) (QuantumRange*exp((double) (value*QuantumScale*pixel))); |
cristy | 9fe8cd7 | 2010-10-19 01:24:07 +0000 | [diff] [blame] | 287 | break; |
| 288 | } |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 289 | case GaussianNoiseEvaluateOperator: |
| 290 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 291 | result=(double) GenerateDifferentialNoise(random_info,pixel, |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 292 | GaussianNoise,value); |
| 293 | break; |
| 294 | } |
| 295 | case ImpulseNoiseEvaluateOperator: |
| 296 | { |
cristy | 84ff57f | 2012-11-06 02:01:48 +0000 | [diff] [blame] | 297 | result=(double) GenerateDifferentialNoise(random_info,pixel,ImpulseNoise, |
| 298 | value); |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 299 | break; |
| 300 | } |
| 301 | case LaplacianNoiseEvaluateOperator: |
| 302 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 303 | result=(double) GenerateDifferentialNoise(random_info,pixel, |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 304 | LaplacianNoise,value); |
| 305 | break; |
| 306 | } |
| 307 | case LeftShiftEvaluateOperator: |
| 308 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 309 | result=(double) ((size_t) pixel << (size_t) (value+0.5)); |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 310 | break; |
| 311 | } |
| 312 | case LogEvaluateOperator: |
| 313 | { |
cristy | a9b9d2e | 2012-06-05 12:03:43 +0000 | [diff] [blame] | 314 | if ((QuantumScale*pixel) >= MagickEpsilon) |
cristy | 84ff57f | 2012-11-06 02:01:48 +0000 | [diff] [blame] | 315 | result=(double) (QuantumRange*log((double) (QuantumScale*value*pixel+ |
| 316 | 1.0))/log((double) (value+1.0))); |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 317 | break; |
| 318 | } |
| 319 | case MaxEvaluateOperator: |
| 320 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 321 | result=(double) EvaluateMax((double) pixel,value); |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 322 | break; |
| 323 | } |
| 324 | case MeanEvaluateOperator: |
| 325 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 326 | result=(double) (pixel+value); |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 327 | break; |
| 328 | } |
cristy | 08a3d70 | 2010-11-28 01:57:36 +0000 | [diff] [blame] | 329 | case MedianEvaluateOperator: |
| 330 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 331 | result=(double) (pixel+value); |
cristy | 08a3d70 | 2010-11-28 01:57:36 +0000 | [diff] [blame] | 332 | break; |
| 333 | } |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 334 | case MinEvaluateOperator: |
| 335 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 336 | result=(double) MagickMin((double) pixel,value); |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 337 | break; |
| 338 | } |
| 339 | case MultiplicativeNoiseEvaluateOperator: |
| 340 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 341 | result=(double) GenerateDifferentialNoise(random_info,pixel, |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 342 | MultiplicativeGaussianNoise,value); |
| 343 | break; |
| 344 | } |
| 345 | case MultiplyEvaluateOperator: |
| 346 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 347 | result=(double) (value*pixel); |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 348 | break; |
| 349 | } |
| 350 | case OrEvaluateOperator: |
| 351 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 352 | result=(double) ((size_t) pixel | (size_t) (value+0.5)); |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 353 | break; |
| 354 | } |
| 355 | case PoissonNoiseEvaluateOperator: |
| 356 | { |
cristy | 84ff57f | 2012-11-06 02:01:48 +0000 | [diff] [blame] | 357 | result=(double) GenerateDifferentialNoise(random_info,pixel,PoissonNoise, |
| 358 | value); |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 359 | break; |
| 360 | } |
| 361 | case PowEvaluateOperator: |
| 362 | { |
cristy | 84ff57f | 2012-11-06 02:01:48 +0000 | [diff] [blame] | 363 | result=(double) (QuantumRange*pow((double) (QuantumScale*pixel),(double) |
| 364 | value)); |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 365 | break; |
| 366 | } |
| 367 | case RightShiftEvaluateOperator: |
| 368 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 369 | result=(double) ((size_t) pixel >> (size_t) (value+0.5)); |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 370 | break; |
| 371 | } |
| 372 | case SetEvaluateOperator: |
| 373 | { |
| 374 | result=value; |
| 375 | break; |
| 376 | } |
| 377 | case SineEvaluateOperator: |
| 378 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 379 | result=(double) (QuantumRange*(0.5*sin((double) (2.0*MagickPI* |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 380 | QuantumScale*pixel*value))+0.5)); |
| 381 | break; |
| 382 | } |
| 383 | case SubtractEvaluateOperator: |
| 384 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 385 | result=(double) (pixel-value); |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 386 | break; |
| 387 | } |
cristy | 12a3f8e | 2012-01-31 01:53:19 +0000 | [diff] [blame] | 388 | case SumEvaluateOperator: |
| 389 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 390 | result=(double) (pixel+value); |
cristy | 12a3f8e | 2012-01-31 01:53:19 +0000 | [diff] [blame] | 391 | break; |
| 392 | } |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 393 | case ThresholdEvaluateOperator: |
| 394 | { |
cristy | 84ff57f | 2012-11-06 02:01:48 +0000 | [diff] [blame] | 395 | result=(double) (((double) pixel <= value) ? 0 : QuantumRange); |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 396 | break; |
| 397 | } |
| 398 | case ThresholdBlackEvaluateOperator: |
| 399 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 400 | result=(double) (((double) pixel <= value) ? 0 : pixel); |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 401 | break; |
| 402 | } |
| 403 | case ThresholdWhiteEvaluateOperator: |
| 404 | { |
cristy | 84ff57f | 2012-11-06 02:01:48 +0000 | [diff] [blame] | 405 | result=(double) (((double) pixel > value) ? QuantumRange : pixel); |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 406 | break; |
| 407 | } |
| 408 | case UniformNoiseEvaluateOperator: |
| 409 | { |
cristy | 84ff57f | 2012-11-06 02:01:48 +0000 | [diff] [blame] | 410 | result=(double) GenerateDifferentialNoise(random_info,pixel,UniformNoise, |
| 411 | value); |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 412 | break; |
| 413 | } |
| 414 | case XorEvaluateOperator: |
| 415 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 416 | result=(double) ((size_t) pixel ^ (size_t) (value+0.5)); |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 417 | break; |
| 418 | } |
| 419 | } |
cristy | d18ae7c | 2010-03-07 17:39:52 +0000 | [diff] [blame] | 420 | return(result); |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 421 | } |
| 422 | |
cristy | d18ae7c | 2010-03-07 17:39:52 +0000 | [diff] [blame] | 423 | MagickExport Image *EvaluateImages(const Image *images, |
| 424 | const MagickEvaluateOperator op,ExceptionInfo *exception) |
| 425 | { |
| 426 | #define EvaluateImageTag "Evaluate/Image" |
| 427 | |
| 428 | CacheView |
| 429 | *evaluate_view; |
| 430 | |
| 431 | const Image |
| 432 | *next; |
| 433 | |
| 434 | Image |
cristy | 4ee2b0c | 2012-05-15 00:30:35 +0000 | [diff] [blame] | 435 | *image; |
cristy | d18ae7c | 2010-03-07 17:39:52 +0000 | [diff] [blame] | 436 | |
cristy | d18ae7c | 2010-03-07 17:39:52 +0000 | [diff] [blame] | 437 | MagickBooleanType |
| 438 | status; |
| 439 | |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 440 | MagickOffsetType |
| 441 | progress; |
| 442 | |
cristy | 95a072b | 2011-10-13 18:03:11 +0000 | [diff] [blame] | 443 | PixelChannels |
cristy | ba18a7a | 2011-09-25 15:43:43 +0000 | [diff] [blame] | 444 | **restrict evaluate_pixels; |
cristy | d18ae7c | 2010-03-07 17:39:52 +0000 | [diff] [blame] | 445 | |
| 446 | RandomInfo |
| 447 | **restrict random_info; |
| 448 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 449 | size_t |
cristy | d18ae7c | 2010-03-07 17:39:52 +0000 | [diff] [blame] | 450 | number_images; |
| 451 | |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 452 | ssize_t |
| 453 | y; |
| 454 | |
glennrp | b36143f | 2012-09-24 18:26:55 +0000 | [diff] [blame] | 455 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 57340e0 | 2012-05-05 00:53:23 +0000 | [diff] [blame] | 456 | unsigned long |
| 457 | key; |
glennrp | b36143f | 2012-09-24 18:26:55 +0000 | [diff] [blame] | 458 | #endif |
cristy | 57340e0 | 2012-05-05 00:53:23 +0000 | [diff] [blame] | 459 | |
cristy | d18ae7c | 2010-03-07 17:39:52 +0000 | [diff] [blame] | 460 | /* |
| 461 | Ensure the image are the same size. |
| 462 | */ |
| 463 | assert(images != (Image *) NULL); |
| 464 | assert(images->signature == MagickSignature); |
| 465 | if (images->debug != MagickFalse) |
| 466 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename); |
| 467 | assert(exception != (ExceptionInfo *) NULL); |
| 468 | assert(exception->signature == MagickSignature); |
| 469 | for (next=images; next != (Image *) NULL; next=GetNextImageInList(next)) |
| 470 | if ((next->columns != images->columns) || (next->rows != images->rows)) |
| 471 | { |
| 472 | (void) ThrowMagickException(exception,GetMagickModule(),OptionError, |
anthony | e5b3965 | 2012-04-21 05:37:29 +0000 | [diff] [blame] | 473 | "ImageWidthsOrHeightsDiffer","'%s'",images->filename); |
cristy | d18ae7c | 2010-03-07 17:39:52 +0000 | [diff] [blame] | 474 | return((Image *) NULL); |
| 475 | } |
| 476 | /* |
| 477 | Initialize evaluate next attributes. |
| 478 | */ |
cristy | 4ee2b0c | 2012-05-15 00:30:35 +0000 | [diff] [blame] | 479 | image=CloneImage(images,images->columns,images->rows,MagickTrue, |
cristy | d18ae7c | 2010-03-07 17:39:52 +0000 | [diff] [blame] | 480 | exception); |
cristy | 4ee2b0c | 2012-05-15 00:30:35 +0000 | [diff] [blame] | 481 | if (image == (Image *) NULL) |
cristy | d18ae7c | 2010-03-07 17:39:52 +0000 | [diff] [blame] | 482 | return((Image *) NULL); |
cristy | 4ee2b0c | 2012-05-15 00:30:35 +0000 | [diff] [blame] | 483 | if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse) |
cristy | d18ae7c | 2010-03-07 17:39:52 +0000 | [diff] [blame] | 484 | { |
cristy | 4ee2b0c | 2012-05-15 00:30:35 +0000 | [diff] [blame] | 485 | image=DestroyImage(image); |
cristy | d18ae7c | 2010-03-07 17:39:52 +0000 | [diff] [blame] | 486 | return((Image *) NULL); |
| 487 | } |
cristy | 08a3d70 | 2010-11-28 01:57:36 +0000 | [diff] [blame] | 488 | number_images=GetImageListLength(images); |
| 489 | evaluate_pixels=AcquirePixelThreadSet(images,number_images); |
cristy | 95a072b | 2011-10-13 18:03:11 +0000 | [diff] [blame] | 490 | if (evaluate_pixels == (PixelChannels **) NULL) |
cristy | d18ae7c | 2010-03-07 17:39:52 +0000 | [diff] [blame] | 491 | { |
cristy | 4ee2b0c | 2012-05-15 00:30:35 +0000 | [diff] [blame] | 492 | image=DestroyImage(image); |
cristy | d18ae7c | 2010-03-07 17:39:52 +0000 | [diff] [blame] | 493 | (void) ThrowMagickException(exception,GetMagickModule(), |
anthony | e5b3965 | 2012-04-21 05:37:29 +0000 | [diff] [blame] | 494 | ResourceLimitError,"MemoryAllocationFailed","'%s'",images->filename); |
cristy | d18ae7c | 2010-03-07 17:39:52 +0000 | [diff] [blame] | 495 | return((Image *) NULL); |
| 496 | } |
| 497 | /* |
| 498 | Evaluate image pixels. |
| 499 | */ |
| 500 | status=MagickTrue; |
| 501 | progress=0; |
cristy | d18ae7c | 2010-03-07 17:39:52 +0000 | [diff] [blame] | 502 | random_info=AcquireRandomInfoThreadSet(); |
glennrp | b36143f | 2012-09-24 18:26:55 +0000 | [diff] [blame] | 503 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 57340e0 | 2012-05-05 00:53:23 +0000 | [diff] [blame] | 504 | key=GetRandomSecretKey(random_info[0]); |
glennrp | b36143f | 2012-09-24 18:26:55 +0000 | [diff] [blame] | 505 | #endif |
cristy | 4ee2b0c | 2012-05-15 00:30:35 +0000 | [diff] [blame] | 506 | evaluate_view=AcquireAuthenticCacheView(image,exception); |
cristy | 08a3d70 | 2010-11-28 01:57:36 +0000 | [diff] [blame] | 507 | if (op == MedianEvaluateOperator) |
cristy | d18ae7c | 2010-03-07 17:39:52 +0000 | [diff] [blame] | 508 | { |
cristy | e2a912b | 2011-12-05 20:02:07 +0000 | [diff] [blame] | 509 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 9a5a52f | 2012-10-09 14:40:31 +0000 | [diff] [blame] | 510 | #pragma omp parallel for schedule(static,4) shared(progress,status) \ |
cristy | 4ee2b0c | 2012-05-15 00:30:35 +0000 | [diff] [blame] | 511 | dynamic_number_threads(image,image->columns,image->rows,key == ~0UL) |
cristy | a30fec6 | 2011-11-21 17:42:27 +0000 | [diff] [blame] | 512 | #endif |
cristy | 4ee2b0c | 2012-05-15 00:30:35 +0000 | [diff] [blame] | 513 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 125a5a3 | 2010-05-07 13:30:52 +0000 | [diff] [blame] | 514 | { |
cristy | a30fec6 | 2011-11-21 17:42:27 +0000 | [diff] [blame] | 515 | CacheView |
| 516 | *image_view; |
cristy | 08a3d70 | 2010-11-28 01:57:36 +0000 | [diff] [blame] | 517 | |
cristy | a30fec6 | 2011-11-21 17:42:27 +0000 | [diff] [blame] | 518 | const Image |
| 519 | *next; |
| 520 | |
| 521 | const int |
| 522 | id = GetOpenMPThreadId(); |
| 523 | |
| 524 | register PixelChannels |
| 525 | *evaluate_pixel; |
| 526 | |
| 527 | register Quantum |
| 528 | *restrict q; |
| 529 | |
| 530 | register ssize_t |
| 531 | x; |
| 532 | |
| 533 | if (status == MagickFalse) |
| 534 | continue; |
cristy | 14a436e | 2012-11-04 23:05:48 +0000 | [diff] [blame] | 535 | q=QueueCacheViewAuthenticPixels(evaluate_view,0,y,image->columns,1, |
| 536 | exception); |
cristy | a30fec6 | 2011-11-21 17:42:27 +0000 | [diff] [blame] | 537 | if (q == (Quantum *) NULL) |
| 538 | { |
| 539 | status=MagickFalse; |
| 540 | continue; |
| 541 | } |
| 542 | evaluate_pixel=evaluate_pixels[id]; |
cristy | 4ee2b0c | 2012-05-15 00:30:35 +0000 | [diff] [blame] | 543 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | a30fec6 | 2011-11-21 17:42:27 +0000 | [diff] [blame] | 544 | { |
| 545 | register ssize_t |
| 546 | j, |
| 547 | k; |
| 548 | |
| 549 | for (j=0; j < (ssize_t) number_images; j++) |
| 550 | for (k=0; k < MaxPixelChannels; k++) |
| 551 | evaluate_pixel[j].channel[k]=0.0; |
| 552 | next=images; |
| 553 | for (j=0; j < (ssize_t) number_images; j++) |
| 554 | { |
| 555 | register const Quantum |
| 556 | *p; |
| 557 | |
| 558 | register ssize_t |
| 559 | i; |
| 560 | |
cristy | db07095 | 2012-04-20 14:33:00 +0000 | [diff] [blame] | 561 | image_view=AcquireVirtualCacheView(next,exception); |
cristy | a30fec6 | 2011-11-21 17:42:27 +0000 | [diff] [blame] | 562 | p=GetCacheViewVirtualPixels(image_view,x,y,1,1,exception); |
| 563 | if (p == (const Quantum *) NULL) |
| 564 | { |
| 565 | image_view=DestroyCacheView(image_view); |
| 566 | break; |
| 567 | } |
cristy | 4ee2b0c | 2012-05-15 00:30:35 +0000 | [diff] [blame] | 568 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
cristy | a30fec6 | 2011-11-21 17:42:27 +0000 | [diff] [blame] | 569 | { |
| 570 | PixelChannel |
| 571 | channel; |
| 572 | |
| 573 | PixelTrait |
| 574 | evaluate_traits, |
| 575 | traits; |
| 576 | |
cristy | cf1296e | 2012-08-26 23:40:49 +0000 | [diff] [blame] | 577 | channel=GetPixelChannelChannel(image,i); |
| 578 | evaluate_traits=GetPixelChannelTraits(image,channel); |
| 579 | traits=GetPixelChannelTraits(next,channel); |
cristy | a30fec6 | 2011-11-21 17:42:27 +0000 | [diff] [blame] | 580 | if ((traits == UndefinedPixelTrait) || |
| 581 | (evaluate_traits == UndefinedPixelTrait)) |
| 582 | continue; |
| 583 | if ((evaluate_traits & UpdatePixelTrait) == 0) |
| 584 | continue; |
| 585 | evaluate_pixel[j].channel[i]=ApplyEvaluateOperator( |
cristy | 4ee2b0c | 2012-05-15 00:30:35 +0000 | [diff] [blame] | 586 | random_info[id],GetPixelChannel(image,channel,p),op, |
cristy | a30fec6 | 2011-11-21 17:42:27 +0000 | [diff] [blame] | 587 | evaluate_pixel[j].channel[i]); |
| 588 | } |
| 589 | image_view=DestroyCacheView(image_view); |
| 590 | next=GetNextImageInList(next); |
| 591 | } |
| 592 | qsort((void *) evaluate_pixel,number_images,sizeof(*evaluate_pixel), |
| 593 | IntensityCompare); |
cristy | 4ee2b0c | 2012-05-15 00:30:35 +0000 | [diff] [blame] | 594 | for (k=0; k < (ssize_t) GetPixelChannels(image); k++) |
cristy | a30fec6 | 2011-11-21 17:42:27 +0000 | [diff] [blame] | 595 | q[k]=ClampToQuantum(evaluate_pixel[j/2].channel[k]); |
cristy | 4ee2b0c | 2012-05-15 00:30:35 +0000 | [diff] [blame] | 596 | q+=GetPixelChannels(image); |
cristy | a30fec6 | 2011-11-21 17:42:27 +0000 | [diff] [blame] | 597 | } |
| 598 | if (SyncCacheViewAuthenticPixels(evaluate_view,exception) == MagickFalse) |
| 599 | status=MagickFalse; |
| 600 | if (images->progress_monitor != (MagickProgressMonitor) NULL) |
| 601 | { |
| 602 | MagickBooleanType |
| 603 | proceed; |
| 604 | |
| 605 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 606 | #pragma omp critical (MagickCore_EvaluateImages) |
| 607 | #endif |
| 608 | proceed=SetImageProgress(images,EvaluateImageTag,progress++, |
cristy | 4ee2b0c | 2012-05-15 00:30:35 +0000 | [diff] [blame] | 609 | image->rows); |
cristy | a30fec6 | 2011-11-21 17:42:27 +0000 | [diff] [blame] | 610 | if (proceed == MagickFalse) |
| 611 | status=MagickFalse; |
| 612 | } |
| 613 | } |
| 614 | } |
| 615 | else |
| 616 | { |
| 617 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 9a5a52f | 2012-10-09 14:40:31 +0000 | [diff] [blame] | 618 | #pragma omp parallel for schedule(static,4) shared(progress,status) \ |
cristy | 4ee2b0c | 2012-05-15 00:30:35 +0000 | [diff] [blame] | 619 | dynamic_number_threads(image,image->columns,image->rows,key == ~0UL) |
cristy | a30fec6 | 2011-11-21 17:42:27 +0000 | [diff] [blame] | 620 | #endif |
cristy | 4ee2b0c | 2012-05-15 00:30:35 +0000 | [diff] [blame] | 621 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | a30fec6 | 2011-11-21 17:42:27 +0000 | [diff] [blame] | 622 | { |
| 623 | CacheView |
| 624 | *image_view; |
| 625 | |
| 626 | const Image |
| 627 | *next; |
| 628 | |
| 629 | const int |
| 630 | id = GetOpenMPThreadId(); |
| 631 | |
| 632 | register ssize_t |
| 633 | i, |
| 634 | x; |
| 635 | |
| 636 | register PixelChannels |
| 637 | *evaluate_pixel; |
| 638 | |
| 639 | register Quantum |
| 640 | *restrict q; |
| 641 | |
| 642 | ssize_t |
| 643 | j; |
| 644 | |
| 645 | if (status == MagickFalse) |
| 646 | continue; |
| 647 | q=QueueCacheViewAuthenticPixels(evaluate_view,0,y, |
cristy | 4ee2b0c | 2012-05-15 00:30:35 +0000 | [diff] [blame] | 648 | image->columns,1,exception); |
cristy | a30fec6 | 2011-11-21 17:42:27 +0000 | [diff] [blame] | 649 | if (q == (Quantum *) NULL) |
| 650 | { |
| 651 | status=MagickFalse; |
| 652 | continue; |
| 653 | } |
| 654 | evaluate_pixel=evaluate_pixels[id]; |
cristy | 4ee2b0c | 2012-05-15 00:30:35 +0000 | [diff] [blame] | 655 | for (j=0; j < (ssize_t) image->columns; j++) |
cristy | a30fec6 | 2011-11-21 17:42:27 +0000 | [diff] [blame] | 656 | for (i=0; i < MaxPixelChannels; i++) |
| 657 | evaluate_pixel[j].channel[i]=0.0; |
cristy | 08a3d70 | 2010-11-28 01:57:36 +0000 | [diff] [blame] | 658 | next=images; |
cristy | ba18a7a | 2011-09-25 15:43:43 +0000 | [diff] [blame] | 659 | for (j=0; j < (ssize_t) number_images; j++) |
cristy | 08a3d70 | 2010-11-28 01:57:36 +0000 | [diff] [blame] | 660 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 661 | register const Quantum |
cristy | 08a3d70 | 2010-11-28 01:57:36 +0000 | [diff] [blame] | 662 | *p; |
| 663 | |
cristy | db07095 | 2012-04-20 14:33:00 +0000 | [diff] [blame] | 664 | image_view=AcquireVirtualCacheView(next,exception); |
cristy | a30fec6 | 2011-11-21 17:42:27 +0000 | [diff] [blame] | 665 | p=GetCacheViewVirtualPixels(image_view,0,y,next->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 666 | if (p == (const Quantum *) NULL) |
cristy | 08a3d70 | 2010-11-28 01:57:36 +0000 | [diff] [blame] | 667 | { |
| 668 | image_view=DestroyCacheView(image_view); |
| 669 | break; |
| 670 | } |
cristy | a30fec6 | 2011-11-21 17:42:27 +0000 | [diff] [blame] | 671 | for (x=0; x < (ssize_t) next->columns; x++) |
cristy | ba18a7a | 2011-09-25 15:43:43 +0000 | [diff] [blame] | 672 | { |
cristy | a30fec6 | 2011-11-21 17:42:27 +0000 | [diff] [blame] | 673 | register ssize_t |
| 674 | i; |
cristy | ba18a7a | 2011-09-25 15:43:43 +0000 | [diff] [blame] | 675 | |
cristy | c94ba6f | 2012-01-29 23:19:58 +0000 | [diff] [blame] | 676 | if (GetPixelMask(next,p) != 0) |
cristy | 10a6c61 | 2012-01-29 21:41:05 +0000 | [diff] [blame] | 677 | { |
cristy | c94ba6f | 2012-01-29 23:19:58 +0000 | [diff] [blame] | 678 | p+=GetPixelChannels(next); |
cristy | 10a6c61 | 2012-01-29 21:41:05 +0000 | [diff] [blame] | 679 | continue; |
| 680 | } |
| 681 | for (i=0; i < (ssize_t) GetPixelChannels(next); i++) |
cristy | a30fec6 | 2011-11-21 17:42:27 +0000 | [diff] [blame] | 682 | { |
| 683 | PixelChannel |
| 684 | channel; |
cristy | ba18a7a | 2011-09-25 15:43:43 +0000 | [diff] [blame] | 685 | |
cristy | a30fec6 | 2011-11-21 17:42:27 +0000 | [diff] [blame] | 686 | PixelTrait |
| 687 | evaluate_traits, |
| 688 | traits; |
| 689 | |
cristy | cf1296e | 2012-08-26 23:40:49 +0000 | [diff] [blame] | 690 | channel=GetPixelChannelChannel(image,i); |
| 691 | traits=GetPixelChannelTraits(next,channel); |
| 692 | evaluate_traits=GetPixelChannelTraits(image,channel); |
cristy | a30fec6 | 2011-11-21 17:42:27 +0000 | [diff] [blame] | 693 | if ((traits == UndefinedPixelTrait) || |
| 694 | (evaluate_traits == UndefinedPixelTrait)) |
| 695 | continue; |
| 696 | if ((traits & UpdatePixelTrait) == 0) |
| 697 | continue; |
| 698 | evaluate_pixel[x].channel[i]=ApplyEvaluateOperator( |
cristy | 4ee2b0c | 2012-05-15 00:30:35 +0000 | [diff] [blame] | 699 | random_info[id],GetPixelChannel(image,channel,p),j == |
cristy | e2a912b | 2011-12-05 20:02:07 +0000 | [diff] [blame] | 700 | 0 ? AddEvaluateOperator : op,evaluate_pixel[x].channel[i]); |
cristy | a30fec6 | 2011-11-21 17:42:27 +0000 | [diff] [blame] | 701 | } |
| 702 | p+=GetPixelChannels(next); |
cristy | ba18a7a | 2011-09-25 15:43:43 +0000 | [diff] [blame] | 703 | } |
cristy | 08a3d70 | 2010-11-28 01:57:36 +0000 | [diff] [blame] | 704 | image_view=DestroyCacheView(image_view); |
| 705 | next=GetNextImageInList(next); |
| 706 | } |
cristy | 4ee2b0c | 2012-05-15 00:30:35 +0000 | [diff] [blame] | 707 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 08a3d70 | 2010-11-28 01:57:36 +0000 | [diff] [blame] | 708 | { |
cristy | a30fec6 | 2011-11-21 17:42:27 +0000 | [diff] [blame] | 709 | register ssize_t |
| 710 | i; |
cristy | d18ae7c | 2010-03-07 17:39:52 +0000 | [diff] [blame] | 711 | |
cristy | a30fec6 | 2011-11-21 17:42:27 +0000 | [diff] [blame] | 712 | switch (op) |
cristy | 08a3d70 | 2010-11-28 01:57:36 +0000 | [diff] [blame] | 713 | { |
cristy | a30fec6 | 2011-11-21 17:42:27 +0000 | [diff] [blame] | 714 | case MeanEvaluateOperator: |
| 715 | { |
cristy | 4ee2b0c | 2012-05-15 00:30:35 +0000 | [diff] [blame] | 716 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 717 | evaluate_pixel[x].channel[i]/=(double) number_images; |
cristy | a30fec6 | 2011-11-21 17:42:27 +0000 | [diff] [blame] | 718 | break; |
| 719 | } |
| 720 | case MultiplyEvaluateOperator: |
| 721 | { |
cristy | 4ee2b0c | 2012-05-15 00:30:35 +0000 | [diff] [blame] | 722 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
cristy | a30fec6 | 2011-11-21 17:42:27 +0000 | [diff] [blame] | 723 | { |
| 724 | register ssize_t |
| 725 | j; |
| 726 | |
| 727 | for (j=0; j < (ssize_t) (number_images-1); j++) |
| 728 | evaluate_pixel[x].channel[i]*=QuantumScale; |
| 729 | } |
| 730 | break; |
| 731 | } |
| 732 | default: |
| 733 | break; |
cristy | 08a3d70 | 2010-11-28 01:57:36 +0000 | [diff] [blame] | 734 | } |
cristy | a30fec6 | 2011-11-21 17:42:27 +0000 | [diff] [blame] | 735 | } |
cristy | 4ee2b0c | 2012-05-15 00:30:35 +0000 | [diff] [blame] | 736 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 08a3d70 | 2010-11-28 01:57:36 +0000 | [diff] [blame] | 737 | { |
cristy | ba18a7a | 2011-09-25 15:43:43 +0000 | [diff] [blame] | 738 | register ssize_t |
| 739 | i; |
| 740 | |
cristy | 4ee2b0c | 2012-05-15 00:30:35 +0000 | [diff] [blame] | 741 | if (GetPixelMask(image,q) != 0) |
cristy | 10a6c61 | 2012-01-29 21:41:05 +0000 | [diff] [blame] | 742 | { |
cristy | 4ee2b0c | 2012-05-15 00:30:35 +0000 | [diff] [blame] | 743 | q+=GetPixelChannels(image); |
cristy | 10a6c61 | 2012-01-29 21:41:05 +0000 | [diff] [blame] | 744 | continue; |
| 745 | } |
cristy | 4ee2b0c | 2012-05-15 00:30:35 +0000 | [diff] [blame] | 746 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
cristy | ba18a7a | 2011-09-25 15:43:43 +0000 | [diff] [blame] | 747 | { |
cristy | abace41 | 2011-12-11 15:56:53 +0000 | [diff] [blame] | 748 | PixelChannel |
| 749 | channel; |
| 750 | |
cristy | ba18a7a | 2011-09-25 15:43:43 +0000 | [diff] [blame] | 751 | PixelTrait |
cristy | ba18a7a | 2011-09-25 15:43:43 +0000 | [diff] [blame] | 752 | traits; |
| 753 | |
cristy | cf1296e | 2012-08-26 23:40:49 +0000 | [diff] [blame] | 754 | channel=GetPixelChannelChannel(image,i); |
| 755 | traits=GetPixelChannelTraits(image,channel); |
cristy | a30fec6 | 2011-11-21 17:42:27 +0000 | [diff] [blame] | 756 | if (traits == UndefinedPixelTrait) |
cristy | ba18a7a | 2011-09-25 15:43:43 +0000 | [diff] [blame] | 757 | continue; |
| 758 | if ((traits & UpdatePixelTrait) == 0) |
| 759 | continue; |
cristy | a30fec6 | 2011-11-21 17:42:27 +0000 | [diff] [blame] | 760 | q[i]=ClampToQuantum(evaluate_pixel[x].channel[i]); |
cristy | ba18a7a | 2011-09-25 15:43:43 +0000 | [diff] [blame] | 761 | } |
cristy | 4ee2b0c | 2012-05-15 00:30:35 +0000 | [diff] [blame] | 762 | q+=GetPixelChannels(image); |
cristy | 08a3d70 | 2010-11-28 01:57:36 +0000 | [diff] [blame] | 763 | } |
cristy | a30fec6 | 2011-11-21 17:42:27 +0000 | [diff] [blame] | 764 | if (SyncCacheViewAuthenticPixels(evaluate_view,exception) == MagickFalse) |
| 765 | status=MagickFalse; |
| 766 | if (images->progress_monitor != (MagickProgressMonitor) NULL) |
cristy | 9ee911c | 2011-09-28 22:33:09 +0000 | [diff] [blame] | 767 | { |
cristy | a30fec6 | 2011-11-21 17:42:27 +0000 | [diff] [blame] | 768 | MagickBooleanType |
| 769 | proceed; |
cristy | 9ee911c | 2011-09-28 22:33:09 +0000 | [diff] [blame] | 770 | |
cristy | a30fec6 | 2011-11-21 17:42:27 +0000 | [diff] [blame] | 771 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 772 | #pragma omp critical (MagickCore_EvaluateImages) |
cristy | 08a3d70 | 2010-11-28 01:57:36 +0000 | [diff] [blame] | 773 | #endif |
cristy | a30fec6 | 2011-11-21 17:42:27 +0000 | [diff] [blame] | 774 | proceed=SetImageProgress(images,EvaluateImageTag,progress++, |
cristy | 4ee2b0c | 2012-05-15 00:30:35 +0000 | [diff] [blame] | 775 | image->rows); |
cristy | a30fec6 | 2011-11-21 17:42:27 +0000 | [diff] [blame] | 776 | if (proceed == MagickFalse) |
| 777 | status=MagickFalse; |
| 778 | } |
| 779 | } |
cristy | 08a3d70 | 2010-11-28 01:57:36 +0000 | [diff] [blame] | 780 | } |
cristy | d18ae7c | 2010-03-07 17:39:52 +0000 | [diff] [blame] | 781 | evaluate_view=DestroyCacheView(evaluate_view); |
| 782 | evaluate_pixels=DestroyPixelThreadSet(evaluate_pixels); |
| 783 | random_info=DestroyRandomInfoThreadSet(random_info); |
| 784 | if (status == MagickFalse) |
cristy | 4ee2b0c | 2012-05-15 00:30:35 +0000 | [diff] [blame] | 785 | image=DestroyImage(image); |
| 786 | return(image); |
cristy | d18ae7c | 2010-03-07 17:39:52 +0000 | [diff] [blame] | 787 | } |
| 788 | |
cristy | d42d995 | 2011-07-08 14:21:50 +0000 | [diff] [blame] | 789 | MagickExport MagickBooleanType EvaluateImage(Image *image, |
| 790 | const MagickEvaluateOperator op,const double value,ExceptionInfo *exception) |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 791 | { |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 792 | CacheView |
| 793 | *image_view; |
| 794 | |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 795 | MagickBooleanType |
| 796 | status; |
| 797 | |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 798 | MagickOffsetType |
| 799 | progress; |
| 800 | |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 801 | RandomInfo |
| 802 | **restrict random_info; |
| 803 | |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 804 | ssize_t |
| 805 | y; |
| 806 | |
glennrp | b36143f | 2012-09-24 18:26:55 +0000 | [diff] [blame] | 807 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 57340e0 | 2012-05-05 00:53:23 +0000 | [diff] [blame] | 808 | unsigned long |
| 809 | key; |
glennrp | b36143f | 2012-09-24 18:26:55 +0000 | [diff] [blame] | 810 | #endif |
cristy | 57340e0 | 2012-05-05 00:53:23 +0000 | [diff] [blame] | 811 | |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 812 | assert(image != (Image *) NULL); |
| 813 | assert(image->signature == MagickSignature); |
| 814 | if (image->debug != MagickFalse) |
| 815 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 816 | assert(exception != (ExceptionInfo *) NULL); |
| 817 | assert(exception->signature == MagickSignature); |
cristy | 574cc26 | 2011-08-05 01:23:58 +0000 | [diff] [blame] | 818 | if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse) |
| 819 | return(MagickFalse); |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 820 | status=MagickTrue; |
| 821 | progress=0; |
| 822 | random_info=AcquireRandomInfoThreadSet(); |
glennrp | b36143f | 2012-09-24 18:26:55 +0000 | [diff] [blame] | 823 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 57340e0 | 2012-05-05 00:53:23 +0000 | [diff] [blame] | 824 | key=GetRandomSecretKey(random_info[0]); |
glennrp | b36143f | 2012-09-24 18:26:55 +0000 | [diff] [blame] | 825 | #endif |
cristy | db07095 | 2012-04-20 14:33:00 +0000 | [diff] [blame] | 826 | image_view=AcquireAuthenticCacheView(image,exception); |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 827 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 57340e0 | 2012-05-05 00:53:23 +0000 | [diff] [blame] | 828 | #pragma omp parallel for schedule(static,4) shared(progress,status) \ |
cristy | 4ee2b0c | 2012-05-15 00:30:35 +0000 | [diff] [blame] | 829 | dynamic_number_threads(image,image->columns,image->rows,key == ~0UL) |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 830 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 831 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 832 | { |
cristy | 5c9e6f2 | 2010-09-17 17:31:01 +0000 | [diff] [blame] | 833 | const int |
| 834 | id = GetOpenMPThreadId(); |
cristy | 6ebe97c | 2010-07-03 01:17:28 +0000 | [diff] [blame] | 835 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 836 | register Quantum |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 837 | *restrict q; |
| 838 | |
cristy | 5c9e6f2 | 2010-09-17 17:31:01 +0000 | [diff] [blame] | 839 | register ssize_t |
| 840 | x; |
| 841 | |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 842 | if (status == MagickFalse) |
| 843 | continue; |
| 844 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 845 | if (q == (Quantum *) NULL) |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 846 | { |
| 847 | status=MagickFalse; |
| 848 | continue; |
| 849 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 850 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 851 | { |
cristy | 49dd6a0 | 2011-09-24 23:08:01 +0000 | [diff] [blame] | 852 | register ssize_t |
| 853 | i; |
| 854 | |
| 855 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
| 856 | { |
cristy | abace41 | 2011-12-11 15:56:53 +0000 | [diff] [blame] | 857 | PixelChannel |
| 858 | channel; |
| 859 | |
cristy | 49dd6a0 | 2011-09-24 23:08:01 +0000 | [diff] [blame] | 860 | PixelTrait |
| 861 | traits; |
| 862 | |
cristy | cf1296e | 2012-08-26 23:40:49 +0000 | [diff] [blame] | 863 | channel=GetPixelChannelChannel(image,i); |
| 864 | traits=GetPixelChannelTraits(image,channel); |
cristy | 49dd6a0 | 2011-09-24 23:08:01 +0000 | [diff] [blame] | 865 | if (traits == UndefinedPixelTrait) |
| 866 | continue; |
cristy | 1eced09 | 2012-08-10 23:10:56 +0000 | [diff] [blame] | 867 | if (((traits & CopyPixelTrait) != 0) || |
| 868 | (GetPixelMask(image,q) != 0)) |
cristy | 177e41c | 2012-04-15 15:08:25 +0000 | [diff] [blame] | 869 | continue; |
cristy | 49dd6a0 | 2011-09-24 23:08:01 +0000 | [diff] [blame] | 870 | q[i]=ClampToQuantum(ApplyEvaluateOperator(random_info[id],q[i],op, |
| 871 | value)); |
| 872 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 873 | q+=GetPixelChannels(image); |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 874 | } |
| 875 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
| 876 | status=MagickFalse; |
| 877 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 878 | { |
| 879 | MagickBooleanType |
| 880 | proceed; |
| 881 | |
| 882 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 883 | #pragma omp critical (MagickCore_EvaluateImage) |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 884 | #endif |
| 885 | proceed=SetImageProgress(image,EvaluateImageTag,progress++,image->rows); |
| 886 | if (proceed == MagickFalse) |
| 887 | status=MagickFalse; |
| 888 | } |
| 889 | } |
| 890 | image_view=DestroyCacheView(image_view); |
| 891 | random_info=DestroyRandomInfoThreadSet(random_info); |
| 892 | return(status); |
| 893 | } |
| 894 | |
| 895 | /* |
| 896 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 897 | % % |
| 898 | % % |
| 899 | % % |
| 900 | % F u n c t i o n I m a g e % |
| 901 | % % |
| 902 | % % |
| 903 | % % |
| 904 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 905 | % |
| 906 | % FunctionImage() applies a value to the image with an arithmetic, relational, |
| 907 | % or logical operator to an image. Use these operations to lighten or darken |
| 908 | % an image, to increase or decrease contrast in an image, or to produce the |
| 909 | % "negative" of an image. |
| 910 | % |
cristy | d42d995 | 2011-07-08 14:21:50 +0000 | [diff] [blame] | 911 | % The format of the FunctionImage method is: |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 912 | % |
| 913 | % MagickBooleanType FunctionImage(Image *image, |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 914 | % const MagickFunction function,const ssize_t number_parameters, |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 915 | % const double *parameters,ExceptionInfo *exception) |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 916 | % |
| 917 | % A description of each parameter follows: |
| 918 | % |
| 919 | % o image: the image. |
| 920 | % |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 921 | % o function: A channel function. |
| 922 | % |
| 923 | % o parameters: one or more parameters. |
| 924 | % |
| 925 | % o exception: return any errors or warnings in this structure. |
| 926 | % |
| 927 | */ |
| 928 | |
| 929 | static Quantum ApplyFunction(Quantum pixel,const MagickFunction function, |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 930 | const size_t number_parameters,const double *parameters, |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 931 | ExceptionInfo *exception) |
| 932 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 933 | double |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 934 | result; |
| 935 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 936 | register ssize_t |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 937 | i; |
| 938 | |
| 939 | (void) exception; |
| 940 | result=0.0; |
| 941 | switch (function) |
| 942 | { |
| 943 | case PolynomialFunction: |
| 944 | { |
| 945 | /* |
cristy | e2a912b | 2011-12-05 20:02:07 +0000 | [diff] [blame] | 946 | Polynomial: polynomial constants, highest to lowest order (e.g. c0*x^3+ |
| 947 | c1*x^2+c2*x+c3). |
cristy | 94a7578 | 2011-09-25 02:33:56 +0000 | [diff] [blame] | 948 | */ |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 949 | result=0.0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 950 | for (i=0; i < (ssize_t) number_parameters; i++) |
cristy | 94a7578 | 2011-09-25 02:33:56 +0000 | [diff] [blame] | 951 | result=result*QuantumScale*pixel+parameters[i]; |
| 952 | result*=QuantumRange; |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 953 | break; |
| 954 | } |
| 955 | case SinusoidFunction: |
| 956 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 957 | double |
cristy | 94a7578 | 2011-09-25 02:33:56 +0000 | [diff] [blame] | 958 | amplitude, |
| 959 | bias, |
| 960 | frequency, |
| 961 | phase; |
| 962 | |
| 963 | /* |
| 964 | Sinusoid: frequency, phase, amplitude, bias. |
| 965 | */ |
| 966 | frequency=(number_parameters >= 1) ? parameters[0] : 1.0; |
| 967 | phase=(number_parameters >= 2) ? parameters[1] : 0.0; |
| 968 | amplitude=(number_parameters >= 3) ? parameters[2] : 0.5; |
| 969 | bias=(number_parameters >= 4) ? parameters[3] : 0.5; |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 970 | result=(double) (QuantumRange*(amplitude*sin((double) (2.0* |
cristy | 94a7578 | 2011-09-25 02:33:56 +0000 | [diff] [blame] | 971 | MagickPI*(frequency*QuantumScale*pixel+phase/360.0)))+bias)); |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 972 | break; |
| 973 | } |
| 974 | case ArcsinFunction: |
| 975 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 976 | double |
cristy | 94a7578 | 2011-09-25 02:33:56 +0000 | [diff] [blame] | 977 | bias, |
| 978 | center, |
| 979 | range, |
| 980 | width; |
| 981 | |
| 982 | /* |
cristy | e2a912b | 2011-12-05 20:02:07 +0000 | [diff] [blame] | 983 | Arcsin (peged at range limits for invalid results): width, center, |
| 984 | range, and bias. |
cristy | 94a7578 | 2011-09-25 02:33:56 +0000 | [diff] [blame] | 985 | */ |
| 986 | width=(number_parameters >= 1) ? parameters[0] : 1.0; |
| 987 | center=(number_parameters >= 2) ? parameters[1] : 0.5; |
| 988 | range=(number_parameters >= 3) ? parameters[2] : 1.0; |
| 989 | bias=(number_parameters >= 4) ? parameters[3] : 0.5; |
| 990 | result=2.0/width*(QuantumScale*pixel-center); |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 991 | if ( result <= -1.0 ) |
cristy | 94a7578 | 2011-09-25 02:33:56 +0000 | [diff] [blame] | 992 | result=bias-range/2.0; |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 993 | else |
cristy | 94a7578 | 2011-09-25 02:33:56 +0000 | [diff] [blame] | 994 | if (result >= 1.0) |
| 995 | result=bias+range/2.0; |
| 996 | else |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 997 | result=(double) (range/MagickPI*asin((double) result)+bias); |
cristy | 94a7578 | 2011-09-25 02:33:56 +0000 | [diff] [blame] | 998 | result*=QuantumRange; |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 999 | break; |
| 1000 | } |
| 1001 | case ArctanFunction: |
| 1002 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1003 | double |
cristy | 94a7578 | 2011-09-25 02:33:56 +0000 | [diff] [blame] | 1004 | center, |
| 1005 | bias, |
| 1006 | range, |
| 1007 | slope; |
| 1008 | |
| 1009 | /* |
| 1010 | Arctan: slope, center, range, and bias. |
| 1011 | */ |
| 1012 | slope=(number_parameters >= 1) ? parameters[0] : 1.0; |
| 1013 | center=(number_parameters >= 2) ? parameters[1] : 0.5; |
| 1014 | range=(number_parameters >= 3) ? parameters[2] : 1.0; |
| 1015 | bias=(number_parameters >= 4) ? parameters[3] : 0.5; |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1016 | result=(double) (MagickPI*slope*(QuantumScale*pixel-center)); |
| 1017 | result=(double) (QuantumRange*(range/MagickPI*atan((double) |
cristy | 94a7578 | 2011-09-25 02:33:56 +0000 | [diff] [blame] | 1018 | result)+bias)); |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 1019 | break; |
| 1020 | } |
| 1021 | case UndefinedFunction: |
| 1022 | break; |
| 1023 | } |
| 1024 | return(ClampToQuantum(result)); |
| 1025 | } |
| 1026 | |
| 1027 | MagickExport MagickBooleanType FunctionImage(Image *image, |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1028 | const MagickFunction function,const size_t number_parameters, |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 1029 | const double *parameters,ExceptionInfo *exception) |
| 1030 | { |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 1031 | #define FunctionImageTag "Function/Image " |
| 1032 | |
| 1033 | CacheView |
| 1034 | *image_view; |
| 1035 | |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 1036 | MagickBooleanType |
| 1037 | status; |
| 1038 | |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 1039 | MagickOffsetType |
| 1040 | progress; |
| 1041 | |
| 1042 | ssize_t |
| 1043 | y; |
| 1044 | |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 1045 | assert(image != (Image *) NULL); |
| 1046 | assert(image->signature == MagickSignature); |
| 1047 | if (image->debug != MagickFalse) |
| 1048 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 1049 | assert(exception != (ExceptionInfo *) NULL); |
| 1050 | assert(exception->signature == MagickSignature); |
cristy | 574cc26 | 2011-08-05 01:23:58 +0000 | [diff] [blame] | 1051 | if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse) |
| 1052 | return(MagickFalse); |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 1053 | status=MagickTrue; |
| 1054 | progress=0; |
cristy | db07095 | 2012-04-20 14:33:00 +0000 | [diff] [blame] | 1055 | image_view=AcquireAuthenticCacheView(image,exception); |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 1056 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 1057 | #pragma omp parallel for schedule(static,4) shared(progress,status) \ |
cristy | 4ee2b0c | 2012-05-15 00:30:35 +0000 | [diff] [blame] | 1058 | dynamic_number_threads(image,image->columns,image->rows,1) |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 1059 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1060 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 1061 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1062 | register Quantum |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 1063 | *restrict q; |
| 1064 | |
cristy | 94a7578 | 2011-09-25 02:33:56 +0000 | [diff] [blame] | 1065 | register ssize_t |
| 1066 | x; |
| 1067 | |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 1068 | if (status == MagickFalse) |
| 1069 | continue; |
| 1070 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 1071 | if (q == (Quantum *) NULL) |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 1072 | { |
| 1073 | status=MagickFalse; |
| 1074 | continue; |
| 1075 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1076 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 1077 | { |
cristy | 94a7578 | 2011-09-25 02:33:56 +0000 | [diff] [blame] | 1078 | register ssize_t |
| 1079 | i; |
| 1080 | |
cristy | 10a6c61 | 2012-01-29 21:41:05 +0000 | [diff] [blame] | 1081 | if (GetPixelMask(image,q) != 0) |
| 1082 | { |
| 1083 | q+=GetPixelChannels(image); |
| 1084 | continue; |
| 1085 | } |
cristy | 94a7578 | 2011-09-25 02:33:56 +0000 | [diff] [blame] | 1086 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
| 1087 | { |
cristy | abace41 | 2011-12-11 15:56:53 +0000 | [diff] [blame] | 1088 | PixelChannel |
| 1089 | channel; |
| 1090 | |
cristy | 94a7578 | 2011-09-25 02:33:56 +0000 | [diff] [blame] | 1091 | PixelTrait |
| 1092 | traits; |
| 1093 | |
cristy | cf1296e | 2012-08-26 23:40:49 +0000 | [diff] [blame] | 1094 | channel=GetPixelChannelChannel(image,i); |
| 1095 | traits=GetPixelChannelTraits(image,channel); |
cristy | 94a7578 | 2011-09-25 02:33:56 +0000 | [diff] [blame] | 1096 | if (traits == UndefinedPixelTrait) |
| 1097 | continue; |
| 1098 | if ((traits & UpdatePixelTrait) == 0) |
| 1099 | continue; |
| 1100 | q[i]=ApplyFunction(q[i],function,number_parameters,parameters, |
| 1101 | exception); |
| 1102 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1103 | q+=GetPixelChannels(image); |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 1104 | } |
| 1105 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
| 1106 | status=MagickFalse; |
| 1107 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 1108 | { |
| 1109 | MagickBooleanType |
| 1110 | proceed; |
| 1111 | |
| 1112 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 1113 | #pragma omp critical (MagickCore_FunctionImage) |
cristy | 351842f | 2010-03-07 15:27:38 +0000 | [diff] [blame] | 1114 | #endif |
| 1115 | proceed=SetImageProgress(image,FunctionImageTag,progress++,image->rows); |
| 1116 | if (proceed == MagickFalse) |
| 1117 | status=MagickFalse; |
| 1118 | } |
| 1119 | } |
| 1120 | image_view=DestroyCacheView(image_view); |
| 1121 | return(status); |
| 1122 | } |
| 1123 | |
| 1124 | /* |
| 1125 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1126 | % % |
| 1127 | % % |
| 1128 | % % |
cristy | d42d995 | 2011-07-08 14:21:50 +0000 | [diff] [blame] | 1129 | % G e t I m a g e E x t r e m a % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1130 | % % |
| 1131 | % % |
| 1132 | % % |
| 1133 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1134 | % |
cristy | d42d995 | 2011-07-08 14:21:50 +0000 | [diff] [blame] | 1135 | % GetImageExtrema() returns the extrema of one or more image channels. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1136 | % |
cristy | d42d995 | 2011-07-08 14:21:50 +0000 | [diff] [blame] | 1137 | % The format of the GetImageExtrema method is: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1138 | % |
cristy | d42d995 | 2011-07-08 14:21:50 +0000 | [diff] [blame] | 1139 | % MagickBooleanType GetImageExtrema(const Image *image,size_t *minima, |
| 1140 | % size_t *maxima,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1141 | % |
| 1142 | % A description of each parameter follows: |
| 1143 | % |
| 1144 | % o image: the image. |
| 1145 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1146 | % o minima: the minimum value in the channel. |
| 1147 | % |
| 1148 | % o maxima: the maximum value in the channel. |
| 1149 | % |
| 1150 | % o exception: return any errors or warnings in this structure. |
| 1151 | % |
| 1152 | */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1153 | MagickExport MagickBooleanType GetImageExtrema(const Image *image, |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1154 | size_t *minima,size_t *maxima,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1155 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1156 | double |
| 1157 | max, |
| 1158 | min; |
| 1159 | |
| 1160 | MagickBooleanType |
| 1161 | status; |
| 1162 | |
| 1163 | assert(image != (Image *) NULL); |
| 1164 | assert(image->signature == MagickSignature); |
| 1165 | if (image->debug != MagickFalse) |
| 1166 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | d42d995 | 2011-07-08 14:21:50 +0000 | [diff] [blame] | 1167 | status=GetImageRange(image,&min,&max,exception); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1168 | *minima=(size_t) ceil(min-0.5); |
| 1169 | *maxima=(size_t) floor(max+0.5); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1170 | return(status); |
| 1171 | } |
| 1172 | |
| 1173 | /* |
| 1174 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1175 | % % |
| 1176 | % % |
| 1177 | % % |
cristy | d42d995 | 2011-07-08 14:21:50 +0000 | [diff] [blame] | 1178 | % G e t I m a g e M e a n % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1179 | % % |
| 1180 | % % |
| 1181 | % % |
| 1182 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1183 | % |
cristy | 5048d30 | 2012-08-07 01:05:16 +0000 | [diff] [blame] | 1184 | % GetImageMean() returns the mean and standard deviation of one or more image |
| 1185 | % channels. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1186 | % |
cristy | d42d995 | 2011-07-08 14:21:50 +0000 | [diff] [blame] | 1187 | % The format of the GetImageMean method is: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1188 | % |
cristy | d42d995 | 2011-07-08 14:21:50 +0000 | [diff] [blame] | 1189 | % MagickBooleanType GetImageMean(const Image *image,double *mean, |
| 1190 | % double *standard_deviation,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1191 | % |
| 1192 | % A description of each parameter follows: |
| 1193 | % |
| 1194 | % o image: the image. |
| 1195 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1196 | % o mean: the average value in the channel. |
| 1197 | % |
| 1198 | % o standard_deviation: the standard deviation of the channel. |
| 1199 | % |
| 1200 | % o exception: return any errors or warnings in this structure. |
| 1201 | % |
| 1202 | */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1203 | MagickExport MagickBooleanType GetImageMean(const Image *image,double *mean, |
| 1204 | double *standard_deviation,ExceptionInfo *exception) |
| 1205 | { |
cristy | 5048d30 | 2012-08-07 01:05:16 +0000 | [diff] [blame] | 1206 | double |
| 1207 | area; |
| 1208 | |
cristy | fd9dcd4 | 2010-08-08 18:07:02 +0000 | [diff] [blame] | 1209 | ChannelStatistics |
| 1210 | *channel_statistics; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1211 | |
cristy | 94a7578 | 2011-09-25 02:33:56 +0000 | [diff] [blame] | 1212 | register ssize_t |
| 1213 | i; |
| 1214 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1215 | assert(image != (Image *) NULL); |
| 1216 | assert(image->signature == MagickSignature); |
| 1217 | if (image->debug != MagickFalse) |
| 1218 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | d42d995 | 2011-07-08 14:21:50 +0000 | [diff] [blame] | 1219 | channel_statistics=GetImageStatistics(image,exception); |
cristy | fd9dcd4 | 2010-08-08 18:07:02 +0000 | [diff] [blame] | 1220 | if (channel_statistics == (ChannelStatistics *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1221 | return(MagickFalse); |
cristy | 5048d30 | 2012-08-07 01:05:16 +0000 | [diff] [blame] | 1222 | area=0.0; |
cristy | 5f95f4f | 2011-10-23 01:01:01 +0000 | [diff] [blame] | 1223 | channel_statistics[CompositePixelChannel].mean=0.0; |
| 1224 | channel_statistics[CompositePixelChannel].standard_deviation=0.0; |
cristy | 94a7578 | 2011-09-25 02:33:56 +0000 | [diff] [blame] | 1225 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
| 1226 | { |
cristy | abace41 | 2011-12-11 15:56:53 +0000 | [diff] [blame] | 1227 | PixelChannel |
| 1228 | channel; |
| 1229 | |
cristy | 94a7578 | 2011-09-25 02:33:56 +0000 | [diff] [blame] | 1230 | PixelTrait |
| 1231 | traits; |
| 1232 | |
cristy | cf1296e | 2012-08-26 23:40:49 +0000 | [diff] [blame] | 1233 | channel=GetPixelChannelChannel(image,i); |
| 1234 | traits=GetPixelChannelTraits(image,channel); |
cristy | 94a7578 | 2011-09-25 02:33:56 +0000 | [diff] [blame] | 1235 | if (traits == UndefinedPixelTrait) |
| 1236 | continue; |
| 1237 | if ((traits & UpdatePixelTrait) == 0) |
| 1238 | continue; |
cristy | 5f95f4f | 2011-10-23 01:01:01 +0000 | [diff] [blame] | 1239 | channel_statistics[CompositePixelChannel].mean+=channel_statistics[i].mean; |
| 1240 | channel_statistics[CompositePixelChannel].standard_deviation+= |
cristy | 94a7578 | 2011-09-25 02:33:56 +0000 | [diff] [blame] | 1241 | channel_statistics[i].variance-channel_statistics[i].mean* |
| 1242 | channel_statistics[i].mean; |
| 1243 | area++; |
| 1244 | } |
cristy | 5f95f4f | 2011-10-23 01:01:01 +0000 | [diff] [blame] | 1245 | channel_statistics[CompositePixelChannel].mean/=area; |
| 1246 | channel_statistics[CompositePixelChannel].standard_deviation= |
| 1247 | sqrt(channel_statistics[CompositePixelChannel].standard_deviation/area); |
| 1248 | *mean=channel_statistics[CompositePixelChannel].mean; |
cristy | e2a912b | 2011-12-05 20:02:07 +0000 | [diff] [blame] | 1249 | *standard_deviation= |
| 1250 | channel_statistics[CompositePixelChannel].standard_deviation; |
cristy | fd9dcd4 | 2010-08-08 18:07:02 +0000 | [diff] [blame] | 1251 | channel_statistics=(ChannelStatistics *) RelinquishMagickMemory( |
| 1252 | channel_statistics); |
| 1253 | return(MagickTrue); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1254 | } |
| 1255 | |
| 1256 | /* |
| 1257 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1258 | % % |
| 1259 | % % |
| 1260 | % % |
cristy | d42d995 | 2011-07-08 14:21:50 +0000 | [diff] [blame] | 1261 | % G e t I m a g e K u r t o s i s % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1262 | % % |
| 1263 | % % |
| 1264 | % % |
| 1265 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1266 | % |
cristy | d42d995 | 2011-07-08 14:21:50 +0000 | [diff] [blame] | 1267 | % GetImageKurtosis() returns the kurtosis and skewness of one or more |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1268 | % image channels. |
| 1269 | % |
cristy | d42d995 | 2011-07-08 14:21:50 +0000 | [diff] [blame] | 1270 | % The format of the GetImageKurtosis method is: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1271 | % |
cristy | d42d995 | 2011-07-08 14:21:50 +0000 | [diff] [blame] | 1272 | % MagickBooleanType GetImageKurtosis(const Image *image,double *kurtosis, |
| 1273 | % double *skewness,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1274 | % |
| 1275 | % A description of each parameter follows: |
| 1276 | % |
| 1277 | % o image: the image. |
| 1278 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1279 | % o kurtosis: the kurtosis of the channel. |
| 1280 | % |
| 1281 | % o skewness: the skewness of the channel. |
| 1282 | % |
| 1283 | % o exception: return any errors or warnings in this structure. |
| 1284 | % |
| 1285 | */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1286 | MagickExport MagickBooleanType GetImageKurtosis(const Image *image, |
| 1287 | double *kurtosis,double *skewness,ExceptionInfo *exception) |
| 1288 | { |
cristy | 94a7578 | 2011-09-25 02:33:56 +0000 | [diff] [blame] | 1289 | CacheView |
| 1290 | *image_view; |
| 1291 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1292 | double |
| 1293 | area, |
| 1294 | mean, |
| 1295 | standard_deviation, |
| 1296 | sum_squares, |
| 1297 | sum_cubes, |
| 1298 | sum_fourth_power; |
| 1299 | |
cristy | 94a7578 | 2011-09-25 02:33:56 +0000 | [diff] [blame] | 1300 | MagickBooleanType |
| 1301 | status; |
| 1302 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1303 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1304 | y; |
| 1305 | |
| 1306 | assert(image != (Image *) NULL); |
| 1307 | assert(image->signature == MagickSignature); |
| 1308 | if (image->debug != MagickFalse) |
| 1309 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | 94a7578 | 2011-09-25 02:33:56 +0000 | [diff] [blame] | 1310 | status=MagickTrue; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1311 | *kurtosis=0.0; |
| 1312 | *skewness=0.0; |
| 1313 | area=0.0; |
| 1314 | mean=0.0; |
| 1315 | standard_deviation=0.0; |
| 1316 | sum_squares=0.0; |
| 1317 | sum_cubes=0.0; |
| 1318 | sum_fourth_power=0.0; |
cristy | db07095 | 2012-04-20 14:33:00 +0000 | [diff] [blame] | 1319 | image_view=AcquireVirtualCacheView(image,exception); |
cristy | 94a7578 | 2011-09-25 02:33:56 +0000 | [diff] [blame] | 1320 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 9a5a52f | 2012-10-09 14:40:31 +0000 | [diff] [blame] | 1321 | #pragma omp parallel for schedule(static,4) shared(status) \ |
cristy | 4ee2b0c | 2012-05-15 00:30:35 +0000 | [diff] [blame] | 1322 | dynamic_number_threads(image,image->columns,image->rows,1) |
cristy | 94a7578 | 2011-09-25 02:33:56 +0000 | [diff] [blame] | 1323 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1324 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1325 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1326 | register const Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1327 | *restrict p; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1328 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1329 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1330 | x; |
| 1331 | |
cristy | 94a7578 | 2011-09-25 02:33:56 +0000 | [diff] [blame] | 1332 | if (status == MagickFalse) |
| 1333 | continue; |
| 1334 | p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1335 | if (p == (const Quantum *) NULL) |
cristy | 94a7578 | 2011-09-25 02:33:56 +0000 | [diff] [blame] | 1336 | { |
| 1337 | status=MagickFalse; |
| 1338 | continue; |
| 1339 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1340 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1341 | { |
cristy | 94a7578 | 2011-09-25 02:33:56 +0000 | [diff] [blame] | 1342 | register ssize_t |
| 1343 | i; |
| 1344 | |
| 1345 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
| 1346 | { |
cristy | abace41 | 2011-12-11 15:56:53 +0000 | [diff] [blame] | 1347 | PixelChannel |
| 1348 | channel; |
| 1349 | |
cristy | 94a7578 | 2011-09-25 02:33:56 +0000 | [diff] [blame] | 1350 | PixelTrait |
| 1351 | traits; |
| 1352 | |
cristy | cf1296e | 2012-08-26 23:40:49 +0000 | [diff] [blame] | 1353 | channel=GetPixelChannelChannel(image,i); |
| 1354 | traits=GetPixelChannelTraits(image,channel); |
cristy | 94a7578 | 2011-09-25 02:33:56 +0000 | [diff] [blame] | 1355 | if (traits == UndefinedPixelTrait) |
| 1356 | continue; |
cristy | 1eced09 | 2012-08-10 23:10:56 +0000 | [diff] [blame] | 1357 | if (((traits & UpdatePixelTrait) == 0) || |
| 1358 | (GetPixelMask(image,p) != 0)) |
cristy | 94a7578 | 2011-09-25 02:33:56 +0000 | [diff] [blame] | 1359 | continue; |
| 1360 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 1361 | #pragma omp critical (MagickCore_GetImageKurtosis) |
| 1362 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1363 | { |
cristy | 94a7578 | 2011-09-25 02:33:56 +0000 | [diff] [blame] | 1364 | mean+=p[i]; |
| 1365 | sum_squares+=(double) p[i]*p[i]; |
| 1366 | sum_cubes+=(double) p[i]*p[i]*p[i]; |
| 1367 | sum_fourth_power+=(double) p[i]*p[i]*p[i]*p[i]; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1368 | area++; |
| 1369 | } |
cristy | 94a7578 | 2011-09-25 02:33:56 +0000 | [diff] [blame] | 1370 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1371 | p+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1372 | } |
| 1373 | } |
cristy | 94a7578 | 2011-09-25 02:33:56 +0000 | [diff] [blame] | 1374 | image_view=DestroyCacheView(image_view); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1375 | if (area != 0.0) |
| 1376 | { |
| 1377 | mean/=area; |
| 1378 | sum_squares/=area; |
| 1379 | sum_cubes/=area; |
| 1380 | sum_fourth_power/=area; |
| 1381 | } |
| 1382 | standard_deviation=sqrt(sum_squares-(mean*mean)); |
| 1383 | if (standard_deviation != 0.0) |
| 1384 | { |
| 1385 | *kurtosis=sum_fourth_power-4.0*mean*sum_cubes+6.0*mean*mean*sum_squares- |
| 1386 | 3.0*mean*mean*mean*mean; |
| 1387 | *kurtosis/=standard_deviation*standard_deviation*standard_deviation* |
| 1388 | standard_deviation; |
| 1389 | *kurtosis-=3.0; |
| 1390 | *skewness=sum_cubes-3.0*mean*sum_squares+2.0*mean*mean*mean; |
| 1391 | *skewness/=standard_deviation*standard_deviation*standard_deviation; |
| 1392 | } |
cristy | 94a7578 | 2011-09-25 02:33:56 +0000 | [diff] [blame] | 1393 | return(status); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1394 | } |
cristy | 46f0820 | 2010-01-10 04:04:21 +0000 | [diff] [blame] | 1395 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1396 | /* |
| 1397 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1398 | % % |
| 1399 | % % |
| 1400 | % % |
cristy | d42d995 | 2011-07-08 14:21:50 +0000 | [diff] [blame] | 1401 | % G e t I m a g e R a n g e % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1402 | % % |
| 1403 | % % |
| 1404 | % % |
| 1405 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1406 | % |
cristy | d42d995 | 2011-07-08 14:21:50 +0000 | [diff] [blame] | 1407 | % GetImageRange() returns the range of one or more image channels. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1408 | % |
cristy | d42d995 | 2011-07-08 14:21:50 +0000 | [diff] [blame] | 1409 | % The format of the GetImageRange method is: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1410 | % |
cristy | d42d995 | 2011-07-08 14:21:50 +0000 | [diff] [blame] | 1411 | % MagickBooleanType GetImageRange(const Image *image,double *minima, |
| 1412 | % double *maxima,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1413 | % |
| 1414 | % A description of each parameter follows: |
| 1415 | % |
| 1416 | % o image: the image. |
| 1417 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1418 | % o minima: the minimum value in the channel. |
| 1419 | % |
| 1420 | % o maxima: the maximum value in the channel. |
| 1421 | % |
| 1422 | % o exception: return any errors or warnings in this structure. |
| 1423 | % |
| 1424 | */ |
cristy | d42d995 | 2011-07-08 14:21:50 +0000 | [diff] [blame] | 1425 | MagickExport MagickBooleanType GetImageRange(const Image *image,double *minima, |
| 1426 | double *maxima,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1427 | { |
cristy | 4a9be68 | 2011-09-25 02:02:32 +0000 | [diff] [blame] | 1428 | CacheView |
| 1429 | *image_view; |
| 1430 | |
| 1431 | MagickBooleanType |
cristy | 2729a44 | 2012-06-07 11:09:49 +0000 | [diff] [blame] | 1432 | initialize, |
cristy | 4a9be68 | 2011-09-25 02:02:32 +0000 | [diff] [blame] | 1433 | status; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1434 | |
cristy | 9d314ff | 2011-03-09 01:30:28 +0000 | [diff] [blame] | 1435 | ssize_t |
| 1436 | y; |
| 1437 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1438 | assert(image != (Image *) NULL); |
| 1439 | assert(image->signature == MagickSignature); |
| 1440 | if (image->debug != MagickFalse) |
| 1441 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | 4a9be68 | 2011-09-25 02:02:32 +0000 | [diff] [blame] | 1442 | status=MagickTrue; |
cristy | 2729a44 | 2012-06-07 11:09:49 +0000 | [diff] [blame] | 1443 | initialize=MagickTrue; |
| 1444 | *maxima=0.0; |
| 1445 | *minima=0.0; |
cristy | db07095 | 2012-04-20 14:33:00 +0000 | [diff] [blame] | 1446 | image_view=AcquireVirtualCacheView(image,exception); |
cristy | 4a9be68 | 2011-09-25 02:02:32 +0000 | [diff] [blame] | 1447 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 9a5a52f | 2012-10-09 14:40:31 +0000 | [diff] [blame] | 1448 | #pragma omp parallel for schedule(static,4) shared(status,initialize) \ |
cristy | 4ee2b0c | 2012-05-15 00:30:35 +0000 | [diff] [blame] | 1449 | dynamic_number_threads(image,image->columns,image->rows,1) |
cristy | 4a9be68 | 2011-09-25 02:02:32 +0000 | [diff] [blame] | 1450 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1451 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1452 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1453 | register const Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1454 | *restrict p; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1455 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1456 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1457 | x; |
| 1458 | |
cristy | 4a9be68 | 2011-09-25 02:02:32 +0000 | [diff] [blame] | 1459 | if (status == MagickFalse) |
| 1460 | continue; |
| 1461 | p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1462 | if (p == (const Quantum *) NULL) |
cristy | 4a9be68 | 2011-09-25 02:02:32 +0000 | [diff] [blame] | 1463 | { |
| 1464 | status=MagickFalse; |
| 1465 | continue; |
| 1466 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1467 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1468 | { |
cristy | 4a9be68 | 2011-09-25 02:02:32 +0000 | [diff] [blame] | 1469 | register ssize_t |
| 1470 | i; |
| 1471 | |
cristy | 10a6c61 | 2012-01-29 21:41:05 +0000 | [diff] [blame] | 1472 | if (GetPixelMask(image,p) != 0) |
| 1473 | { |
| 1474 | p+=GetPixelChannels(image); |
| 1475 | continue; |
| 1476 | } |
cristy | 4a9be68 | 2011-09-25 02:02:32 +0000 | [diff] [blame] | 1477 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
| 1478 | { |
cristy | abace41 | 2011-12-11 15:56:53 +0000 | [diff] [blame] | 1479 | PixelChannel |
| 1480 | channel; |
| 1481 | |
cristy | 4a9be68 | 2011-09-25 02:02:32 +0000 | [diff] [blame] | 1482 | PixelTrait |
| 1483 | traits; |
| 1484 | |
cristy | cf1296e | 2012-08-26 23:40:49 +0000 | [diff] [blame] | 1485 | channel=GetPixelChannelChannel(image,i); |
| 1486 | traits=GetPixelChannelTraits(image,channel); |
cristy | 4a9be68 | 2011-09-25 02:02:32 +0000 | [diff] [blame] | 1487 | if (traits == UndefinedPixelTrait) |
| 1488 | continue; |
| 1489 | if ((traits & UpdatePixelTrait) == 0) |
| 1490 | continue; |
| 1491 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 1492 | #pragma omp critical (MagickCore_GetImageRange) |
| 1493 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1494 | { |
cristy | 2729a44 | 2012-06-07 11:09:49 +0000 | [diff] [blame] | 1495 | if (initialize != MagickFalse) |
| 1496 | { |
| 1497 | *minima=(double) p[i]; |
| 1498 | *maxima=(double) p[i]; |
| 1499 | initialize=MagickFalse; |
| 1500 | } |
| 1501 | else |
| 1502 | { |
| 1503 | if ((double) p[i] < *minima) |
| 1504 | *minima=(double) p[i]; |
| 1505 | if ((double) p[i] > *maxima) |
| 1506 | *maxima=(double) p[i]; |
| 1507 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1508 | } |
cristy | 4a9be68 | 2011-09-25 02:02:32 +0000 | [diff] [blame] | 1509 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1510 | p+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1511 | } |
| 1512 | } |
cristy | 4a9be68 | 2011-09-25 02:02:32 +0000 | [diff] [blame] | 1513 | image_view=DestroyCacheView(image_view); |
| 1514 | return(status); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1515 | } |
| 1516 | |
| 1517 | /* |
| 1518 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1519 | % % |
| 1520 | % % |
| 1521 | % % |
cristy | d42d995 | 2011-07-08 14:21:50 +0000 | [diff] [blame] | 1522 | % G e t I m a g e S t a t i s t i c s % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1523 | % % |
| 1524 | % % |
| 1525 | % % |
| 1526 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1527 | % |
cristy | 5048d30 | 2012-08-07 01:05:16 +0000 | [diff] [blame] | 1528 | % GetImageStatistics() returns statistics for each channel in the image. The |
| 1529 | % statistics include the channel depth, its minima, maxima, mean, standard |
| 1530 | % deviation, kurtosis and skewness. You can access the red channel mean, for |
| 1531 | % example, like this: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1532 | % |
cristy | d42d995 | 2011-07-08 14:21:50 +0000 | [diff] [blame] | 1533 | % channel_statistics=GetImageStatistics(image,exception); |
cristy | 49dd6a0 | 2011-09-24 23:08:01 +0000 | [diff] [blame] | 1534 | % red_mean=channel_statistics[RedPixelChannel].mean; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1535 | % |
| 1536 | % Use MagickRelinquishMemory() to free the statistics buffer. |
| 1537 | % |
cristy | d42d995 | 2011-07-08 14:21:50 +0000 | [diff] [blame] | 1538 | % The format of the GetImageStatistics method is: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1539 | % |
cristy | d42d995 | 2011-07-08 14:21:50 +0000 | [diff] [blame] | 1540 | % ChannelStatistics *GetImageStatistics(const Image *image, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1541 | % ExceptionInfo *exception) |
| 1542 | % |
| 1543 | % A description of each parameter follows: |
| 1544 | % |
| 1545 | % o image: the image. |
| 1546 | % |
| 1547 | % o exception: return any errors or warnings in this structure. |
| 1548 | % |
| 1549 | */ |
cristy | 49dd6a0 | 2011-09-24 23:08:01 +0000 | [diff] [blame] | 1550 | |
| 1551 | static size_t GetImageChannels(const Image *image) |
| 1552 | { |
| 1553 | register ssize_t |
| 1554 | i; |
| 1555 | |
| 1556 | size_t |
| 1557 | channels; |
| 1558 | |
| 1559 | channels=0; |
| 1560 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
| 1561 | { |
cristy | abace41 | 2011-12-11 15:56:53 +0000 | [diff] [blame] | 1562 | PixelChannel |
| 1563 | channel; |
| 1564 | |
cristy | 49dd6a0 | 2011-09-24 23:08:01 +0000 | [diff] [blame] | 1565 | PixelTrait |
| 1566 | traits; |
| 1567 | |
cristy | cf1296e | 2012-08-26 23:40:49 +0000 | [diff] [blame] | 1568 | channel=GetPixelChannelChannel(image,i); |
| 1569 | traits=GetPixelChannelTraits(image,channel); |
cristy | 49dd6a0 | 2011-09-24 23:08:01 +0000 | [diff] [blame] | 1570 | if ((traits & UpdatePixelTrait) != 0) |
| 1571 | channels++; |
| 1572 | } |
| 1573 | return(channels); |
| 1574 | } |
| 1575 | |
cristy | d42d995 | 2011-07-08 14:21:50 +0000 | [diff] [blame] | 1576 | MagickExport ChannelStatistics *GetImageStatistics(const Image *image, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1577 | ExceptionInfo *exception) |
| 1578 | { |
| 1579 | ChannelStatistics |
| 1580 | *channel_statistics; |
| 1581 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1582 | MagickStatusType |
| 1583 | status; |
| 1584 | |
| 1585 | QuantumAny |
| 1586 | range; |
| 1587 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1588 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1589 | i; |
| 1590 | |
| 1591 | size_t |
cristy | 9d314ff | 2011-03-09 01:30:28 +0000 | [diff] [blame] | 1592 | channels, |
cristy | 49dd6a0 | 2011-09-24 23:08:01 +0000 | [diff] [blame] | 1593 | depth; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1594 | |
cristy | 9d314ff | 2011-03-09 01:30:28 +0000 | [diff] [blame] | 1595 | ssize_t |
| 1596 | y; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1597 | |
| 1598 | assert(image != (Image *) NULL); |
| 1599 | assert(image->signature == MagickSignature); |
| 1600 | if (image->debug != MagickFalse) |
| 1601 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | 49dd6a0 | 2011-09-24 23:08:01 +0000 | [diff] [blame] | 1602 | channel_statistics=(ChannelStatistics *) AcquireQuantumMemory( |
| 1603 | MaxPixelChannels+1,sizeof(*channel_statistics)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1604 | if (channel_statistics == (ChannelStatistics *) NULL) |
| 1605 | ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed"); |
cristy | 49dd6a0 | 2011-09-24 23:08:01 +0000 | [diff] [blame] | 1606 | (void) ResetMagickMemory(channel_statistics,0,(MaxPixelChannels+1)* |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1607 | sizeof(*channel_statistics)); |
cristy | 25a5f2f | 2011-09-24 14:09:43 +0000 | [diff] [blame] | 1608 | for (i=0; i <= (ssize_t) MaxPixelChannels; i++) |
cristy | 3e37d9e | 2012-08-16 01:27:51 +0000 | [diff] [blame] | 1609 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1610 | channel_statistics[i].depth=1; |
cristy | 3e37d9e | 2012-08-16 01:27:51 +0000 | [diff] [blame] | 1611 | channel_statistics[i].maxima=(-MagickHuge); |
| 1612 | channel_statistics[i].minima=MagickHuge; |
| 1613 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1614 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1615 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1616 | register const Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1617 | *restrict p; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1618 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1619 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1620 | x; |
| 1621 | |
| 1622 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1623 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1624 | break; |
cristy | 49dd6a0 | 2011-09-24 23:08:01 +0000 | [diff] [blame] | 1625 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1626 | { |
cristy | 49dd6a0 | 2011-09-24 23:08:01 +0000 | [diff] [blame] | 1627 | register ssize_t |
| 1628 | i; |
| 1629 | |
cristy | 10a6c61 | 2012-01-29 21:41:05 +0000 | [diff] [blame] | 1630 | if (GetPixelMask(image,p) != 0) |
| 1631 | { |
| 1632 | p+=GetPixelChannels(image); |
| 1633 | continue; |
| 1634 | } |
cristy | 49dd6a0 | 2011-09-24 23:08:01 +0000 | [diff] [blame] | 1635 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
| 1636 | { |
cristy | abace41 | 2011-12-11 15:56:53 +0000 | [diff] [blame] | 1637 | PixelChannel |
| 1638 | channel; |
| 1639 | |
cristy | 49dd6a0 | 2011-09-24 23:08:01 +0000 | [diff] [blame] | 1640 | PixelTrait |
| 1641 | traits; |
| 1642 | |
cristy | cf1296e | 2012-08-26 23:40:49 +0000 | [diff] [blame] | 1643 | channel=GetPixelChannelChannel(image,i); |
| 1644 | traits=GetPixelChannelTraits(image,channel); |
cristy | 49dd6a0 | 2011-09-24 23:08:01 +0000 | [diff] [blame] | 1645 | if (traits == UndefinedPixelTrait) |
| 1646 | continue; |
cristy | 3cad9cd | 2012-01-02 18:27:40 +0000 | [diff] [blame] | 1647 | if (channel_statistics[channel].depth != MAGICKCORE_QUANTUM_DEPTH) |
cristy | 49dd6a0 | 2011-09-24 23:08:01 +0000 | [diff] [blame] | 1648 | { |
cristy | 3cad9cd | 2012-01-02 18:27:40 +0000 | [diff] [blame] | 1649 | depth=channel_statistics[channel].depth; |
cristy | 49dd6a0 | 2011-09-24 23:08:01 +0000 | [diff] [blame] | 1650 | range=GetQuantumRange(depth); |
| 1651 | status=p[i] != ScaleAnyToQuantum(ScaleQuantumToAny(p[i],range), |
| 1652 | range) ? MagickTrue : MagickFalse; |
| 1653 | if (status != MagickFalse) |
| 1654 | { |
cristy | cd8ce3e | 2011-12-14 12:33:23 +0000 | [diff] [blame] | 1655 | channel_statistics[channel].depth++; |
cristy | 6f7d3a9 | 2012-05-27 00:31:01 +0000 | [diff] [blame] | 1656 | i--; |
cristy | 49dd6a0 | 2011-09-24 23:08:01 +0000 | [diff] [blame] | 1657 | continue; |
| 1658 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1659 | } |
cristy | 3e37d9e | 2012-08-16 01:27:51 +0000 | [diff] [blame] | 1660 | if ((double) p[i] < channel_statistics[channel].minima) |
| 1661 | channel_statistics[channel].minima=(double) p[i]; |
| 1662 | if ((double) p[i] > channel_statistics[channel].maxima) |
| 1663 | channel_statistics[channel].maxima=(double) p[i]; |
cristy | cd8ce3e | 2011-12-14 12:33:23 +0000 | [diff] [blame] | 1664 | channel_statistics[channel].sum+=p[i]; |
| 1665 | channel_statistics[channel].sum_squared+=(double) p[i]*p[i]; |
| 1666 | channel_statistics[channel].sum_cubed+=(double) p[i]*p[i]*p[i]; |
| 1667 | channel_statistics[channel].sum_fourth_power+=(double) p[i]*p[i]*p[i]* |
| 1668 | p[i]; |
cristy | 5048d30 | 2012-08-07 01:05:16 +0000 | [diff] [blame] | 1669 | channel_statistics[channel].area++; |
cristy | 49dd6a0 | 2011-09-24 23:08:01 +0000 | [diff] [blame] | 1670 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1671 | p+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1672 | } |
| 1673 | } |
cristy | 25a5f2f | 2011-09-24 14:09:43 +0000 | [diff] [blame] | 1674 | for (i=0; i < (ssize_t) MaxPixelChannels; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1675 | { |
cristy | a38fb2c | 2012-08-16 11:37:15 +0000 | [diff] [blame] | 1676 | double |
| 1677 | area; |
| 1678 | |
cristy | 3e3ec3a | 2012-11-03 23:11:06 +0000 | [diff] [blame] | 1679 | area=PerceptibleReciprocal(channel_statistics[i].area); |
cristy | a38fb2c | 2012-08-16 11:37:15 +0000 | [diff] [blame] | 1680 | channel_statistics[i].sum*=area; |
| 1681 | channel_statistics[i].sum_squared*=area; |
| 1682 | channel_statistics[i].sum_cubed*=area; |
| 1683 | channel_statistics[i].sum_fourth_power*=area; |
cristy | fd9dcd4 | 2010-08-08 18:07:02 +0000 | [diff] [blame] | 1684 | channel_statistics[i].mean=channel_statistics[i].sum; |
| 1685 | channel_statistics[i].variance=channel_statistics[i].sum_squared; |
| 1686 | channel_statistics[i].standard_deviation=sqrt( |
| 1687 | channel_statistics[i].variance-(channel_statistics[i].mean* |
| 1688 | channel_statistics[i].mean)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1689 | } |
cristy | 25a5f2f | 2011-09-24 14:09:43 +0000 | [diff] [blame] | 1690 | for (i=0; i < (ssize_t) MaxPixelChannels; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1691 | { |
cristy | 99bd523 | 2011-12-07 14:38:20 +0000 | [diff] [blame] | 1692 | channel_statistics[CompositePixelChannel].depth=(size_t) EvaluateMax( |
| 1693 | (double) channel_statistics[CompositePixelChannel].depth,(double) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1694 | channel_statistics[i].depth); |
cristy | 5f95f4f | 2011-10-23 01:01:01 +0000 | [diff] [blame] | 1695 | channel_statistics[CompositePixelChannel].minima=MagickMin( |
| 1696 | channel_statistics[CompositePixelChannel].minima, |
cristy | 32daba4 | 2011-05-01 02:34:39 +0000 | [diff] [blame] | 1697 | channel_statistics[i].minima); |
cristy | 99bd523 | 2011-12-07 14:38:20 +0000 | [diff] [blame] | 1698 | channel_statistics[CompositePixelChannel].maxima=EvaluateMax( |
cristy | 5f95f4f | 2011-10-23 01:01:01 +0000 | [diff] [blame] | 1699 | channel_statistics[CompositePixelChannel].maxima, |
cristy | 32daba4 | 2011-05-01 02:34:39 +0000 | [diff] [blame] | 1700 | channel_statistics[i].maxima); |
cristy | 5f95f4f | 2011-10-23 01:01:01 +0000 | [diff] [blame] | 1701 | channel_statistics[CompositePixelChannel].sum+=channel_statistics[i].sum; |
| 1702 | channel_statistics[CompositePixelChannel].sum_squared+= |
cristy | fd9dcd4 | 2010-08-08 18:07:02 +0000 | [diff] [blame] | 1703 | channel_statistics[i].sum_squared; |
cristy | 5f95f4f | 2011-10-23 01:01:01 +0000 | [diff] [blame] | 1704 | channel_statistics[CompositePixelChannel].sum_cubed+= |
cristy | 32daba4 | 2011-05-01 02:34:39 +0000 | [diff] [blame] | 1705 | channel_statistics[i].sum_cubed; |
cristy | 5f95f4f | 2011-10-23 01:01:01 +0000 | [diff] [blame] | 1706 | channel_statistics[CompositePixelChannel].sum_fourth_power+= |
cristy | a8178ed | 2010-08-10 17:31:59 +0000 | [diff] [blame] | 1707 | channel_statistics[i].sum_fourth_power; |
cristy | 5f95f4f | 2011-10-23 01:01:01 +0000 | [diff] [blame] | 1708 | channel_statistics[CompositePixelChannel].mean+=channel_statistics[i].mean; |
| 1709 | channel_statistics[CompositePixelChannel].variance+= |
cristy | 32daba4 | 2011-05-01 02:34:39 +0000 | [diff] [blame] | 1710 | channel_statistics[i].variance-channel_statistics[i].mean* |
| 1711 | channel_statistics[i].mean; |
cristy | 5f95f4f | 2011-10-23 01:01:01 +0000 | [diff] [blame] | 1712 | channel_statistics[CompositePixelChannel].standard_deviation+= |
cristy | fd9dcd4 | 2010-08-08 18:07:02 +0000 | [diff] [blame] | 1713 | channel_statistics[i].variance-channel_statistics[i].mean* |
| 1714 | channel_statistics[i].mean; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1715 | } |
cristy | 49dd6a0 | 2011-09-24 23:08:01 +0000 | [diff] [blame] | 1716 | channels=GetImageChannels(image); |
cristy | 5f95f4f | 2011-10-23 01:01:01 +0000 | [diff] [blame] | 1717 | channel_statistics[CompositePixelChannel].sum/=channels; |
| 1718 | channel_statistics[CompositePixelChannel].sum_squared/=channels; |
| 1719 | channel_statistics[CompositePixelChannel].sum_cubed/=channels; |
| 1720 | channel_statistics[CompositePixelChannel].sum_fourth_power/=channels; |
| 1721 | channel_statistics[CompositePixelChannel].mean/=channels; |
| 1722 | channel_statistics[CompositePixelChannel].variance/=channels; |
| 1723 | channel_statistics[CompositePixelChannel].standard_deviation= |
| 1724 | sqrt(channel_statistics[CompositePixelChannel].standard_deviation/channels); |
| 1725 | channel_statistics[CompositePixelChannel].kurtosis/=channels; |
| 1726 | channel_statistics[CompositePixelChannel].skewness/=channels; |
cristy | 25a5f2f | 2011-09-24 14:09:43 +0000 | [diff] [blame] | 1727 | for (i=0; i <= (ssize_t) MaxPixelChannels; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1728 | { |
cristy | a38fb2c | 2012-08-16 11:37:15 +0000 | [diff] [blame] | 1729 | double |
| 1730 | standard_deviation; |
| 1731 | |
cristy | 3e3ec3a | 2012-11-03 23:11:06 +0000 | [diff] [blame] | 1732 | standard_deviation=PerceptibleReciprocal( |
cristy | a38fb2c | 2012-08-16 11:37:15 +0000 | [diff] [blame] | 1733 | channel_statistics[i].standard_deviation); |
cristy | e2a912b | 2011-12-05 20:02:07 +0000 | [diff] [blame] | 1734 | channel_statistics[i].skewness=(channel_statistics[i].sum_cubed-3.0* |
| 1735 | channel_statistics[i].mean*channel_statistics[i].sum_squared+2.0* |
| 1736 | channel_statistics[i].mean*channel_statistics[i].mean* |
cristy | a38fb2c | 2012-08-16 11:37:15 +0000 | [diff] [blame] | 1737 | channel_statistics[i].mean)*(standard_deviation*standard_deviation* |
| 1738 | standard_deviation); |
cristy | e2a912b | 2011-12-05 20:02:07 +0000 | [diff] [blame] | 1739 | channel_statistics[i].kurtosis=(channel_statistics[i].sum_fourth_power-4.0* |
| 1740 | channel_statistics[i].mean*channel_statistics[i].sum_cubed+6.0* |
| 1741 | channel_statistics[i].mean*channel_statistics[i].mean* |
cristy | a8178ed | 2010-08-10 17:31:59 +0000 | [diff] [blame] | 1742 | channel_statistics[i].sum_squared-3.0*channel_statistics[i].mean* |
| 1743 | channel_statistics[i].mean*1.0*channel_statistics[i].mean* |
cristy | a38fb2c | 2012-08-16 11:37:15 +0000 | [diff] [blame] | 1744 | channel_statistics[i].mean)*(standard_deviation*standard_deviation* |
| 1745 | standard_deviation*standard_deviation)-3.0; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1746 | } |
| 1747 | return(channel_statistics); |
| 1748 | } |
cristy | 99bd523 | 2011-12-07 14:38:20 +0000 | [diff] [blame] | 1749 | |
| 1750 | /* |
| 1751 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1752 | % % |
| 1753 | % % |
| 1754 | % % |
| 1755 | % S t a t i s t i c I m a g e % |
| 1756 | % % |
| 1757 | % % |
| 1758 | % % |
| 1759 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1760 | % |
| 1761 | % StatisticImage() makes each pixel the min / max / median / mode / etc. of |
| 1762 | % the neighborhood of the specified width and height. |
| 1763 | % |
| 1764 | % The format of the StatisticImage method is: |
| 1765 | % |
| 1766 | % Image *StatisticImage(const Image *image,const StatisticType type, |
| 1767 | % const size_t width,const size_t height,ExceptionInfo *exception) |
| 1768 | % |
| 1769 | % A description of each parameter follows: |
| 1770 | % |
| 1771 | % o image: the image. |
| 1772 | % |
| 1773 | % o type: the statistic type (median, mode, etc.). |
| 1774 | % |
| 1775 | % o width: the width of the pixel neighborhood. |
| 1776 | % |
| 1777 | % o height: the height of the pixel neighborhood. |
| 1778 | % |
| 1779 | % o exception: return any errors or warnings in this structure. |
| 1780 | % |
| 1781 | */ |
| 1782 | |
| 1783 | typedef struct _SkipNode |
| 1784 | { |
| 1785 | size_t |
| 1786 | next[9], |
| 1787 | count, |
| 1788 | signature; |
| 1789 | } SkipNode; |
| 1790 | |
| 1791 | typedef struct _SkipList |
| 1792 | { |
| 1793 | ssize_t |
| 1794 | level; |
| 1795 | |
| 1796 | SkipNode |
| 1797 | *nodes; |
| 1798 | } SkipList; |
| 1799 | |
| 1800 | typedef struct _PixelList |
| 1801 | { |
| 1802 | size_t |
| 1803 | length, |
| 1804 | seed; |
| 1805 | |
| 1806 | SkipList |
| 1807 | skip_list; |
| 1808 | |
| 1809 | size_t |
| 1810 | signature; |
| 1811 | } PixelList; |
| 1812 | |
| 1813 | static PixelList *DestroyPixelList(PixelList *pixel_list) |
| 1814 | { |
| 1815 | if (pixel_list == (PixelList *) NULL) |
| 1816 | return((PixelList *) NULL); |
| 1817 | if (pixel_list->skip_list.nodes != (SkipNode *) NULL) |
| 1818 | pixel_list->skip_list.nodes=(SkipNode *) RelinquishMagickMemory( |
| 1819 | pixel_list->skip_list.nodes); |
| 1820 | pixel_list=(PixelList *) RelinquishMagickMemory(pixel_list); |
| 1821 | return(pixel_list); |
| 1822 | } |
| 1823 | |
| 1824 | static PixelList **DestroyPixelListThreadSet(PixelList **pixel_list) |
| 1825 | { |
| 1826 | register ssize_t |
| 1827 | i; |
| 1828 | |
| 1829 | assert(pixel_list != (PixelList **) NULL); |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 1830 | for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++) |
cristy | 99bd523 | 2011-12-07 14:38:20 +0000 | [diff] [blame] | 1831 | if (pixel_list[i] != (PixelList *) NULL) |
| 1832 | pixel_list[i]=DestroyPixelList(pixel_list[i]); |
| 1833 | pixel_list=(PixelList **) RelinquishMagickMemory(pixel_list); |
| 1834 | return(pixel_list); |
| 1835 | } |
| 1836 | |
| 1837 | static PixelList *AcquirePixelList(const size_t width,const size_t height) |
| 1838 | { |
| 1839 | PixelList |
| 1840 | *pixel_list; |
| 1841 | |
| 1842 | pixel_list=(PixelList *) AcquireMagickMemory(sizeof(*pixel_list)); |
| 1843 | if (pixel_list == (PixelList *) NULL) |
| 1844 | return(pixel_list); |
| 1845 | (void) ResetMagickMemory((void *) pixel_list,0,sizeof(*pixel_list)); |
| 1846 | pixel_list->length=width*height; |
| 1847 | pixel_list->skip_list.nodes=(SkipNode *) AcquireQuantumMemory(65537UL, |
| 1848 | sizeof(*pixel_list->skip_list.nodes)); |
| 1849 | if (pixel_list->skip_list.nodes == (SkipNode *) NULL) |
| 1850 | return(DestroyPixelList(pixel_list)); |
| 1851 | (void) ResetMagickMemory(pixel_list->skip_list.nodes,0,65537UL* |
| 1852 | sizeof(*pixel_list->skip_list.nodes)); |
| 1853 | pixel_list->signature=MagickSignature; |
| 1854 | return(pixel_list); |
| 1855 | } |
| 1856 | |
| 1857 | static PixelList **AcquirePixelListThreadSet(const size_t width, |
| 1858 | const size_t height) |
| 1859 | { |
| 1860 | PixelList |
| 1861 | **pixel_list; |
| 1862 | |
| 1863 | register ssize_t |
| 1864 | i; |
| 1865 | |
| 1866 | size_t |
| 1867 | number_threads; |
| 1868 | |
cristy | 9357bdd | 2012-07-30 12:28:34 +0000 | [diff] [blame] | 1869 | number_threads=(size_t) GetMagickResourceLimit(ThreadResource); |
cristy | 99bd523 | 2011-12-07 14:38:20 +0000 | [diff] [blame] | 1870 | pixel_list=(PixelList **) AcquireQuantumMemory(number_threads, |
| 1871 | sizeof(*pixel_list)); |
| 1872 | if (pixel_list == (PixelList **) NULL) |
| 1873 | return((PixelList **) NULL); |
| 1874 | (void) ResetMagickMemory(pixel_list,0,number_threads*sizeof(*pixel_list)); |
| 1875 | for (i=0; i < (ssize_t) number_threads; i++) |
| 1876 | { |
| 1877 | pixel_list[i]=AcquirePixelList(width,height); |
| 1878 | if (pixel_list[i] == (PixelList *) NULL) |
| 1879 | return(DestroyPixelListThreadSet(pixel_list)); |
| 1880 | } |
| 1881 | return(pixel_list); |
| 1882 | } |
| 1883 | |
| 1884 | static void AddNodePixelList(PixelList *pixel_list,const size_t color) |
| 1885 | { |
| 1886 | register SkipList |
| 1887 | *p; |
| 1888 | |
| 1889 | register ssize_t |
| 1890 | level; |
| 1891 | |
| 1892 | size_t |
| 1893 | search, |
| 1894 | update[9]; |
| 1895 | |
| 1896 | /* |
| 1897 | Initialize the node. |
| 1898 | */ |
| 1899 | p=(&pixel_list->skip_list); |
| 1900 | p->nodes[color].signature=pixel_list->signature; |
| 1901 | p->nodes[color].count=1; |
| 1902 | /* |
| 1903 | Determine where it belongs in the list. |
| 1904 | */ |
| 1905 | search=65536UL; |
| 1906 | for (level=p->level; level >= 0; level--) |
| 1907 | { |
| 1908 | while (p->nodes[search].next[level] < color) |
| 1909 | search=p->nodes[search].next[level]; |
| 1910 | update[level]=search; |
| 1911 | } |
| 1912 | /* |
| 1913 | Generate a pseudo-random level for this node. |
| 1914 | */ |
| 1915 | for (level=0; ; level++) |
| 1916 | { |
| 1917 | pixel_list->seed=(pixel_list->seed*42893621L)+1L; |
| 1918 | if ((pixel_list->seed & 0x300) != 0x300) |
| 1919 | break; |
| 1920 | } |
| 1921 | if (level > 8) |
| 1922 | level=8; |
| 1923 | if (level > (p->level+2)) |
| 1924 | level=p->level+2; |
| 1925 | /* |
| 1926 | If we're raising the list's level, link back to the root node. |
| 1927 | */ |
| 1928 | while (level > p->level) |
| 1929 | { |
| 1930 | p->level++; |
| 1931 | update[p->level]=65536UL; |
| 1932 | } |
| 1933 | /* |
| 1934 | Link the node into the skip-list. |
| 1935 | */ |
| 1936 | do |
| 1937 | { |
| 1938 | p->nodes[color].next[level]=p->nodes[update[level]].next[level]; |
| 1939 | p->nodes[update[level]].next[level]=color; |
| 1940 | } while (level-- > 0); |
| 1941 | } |
| 1942 | |
| 1943 | static inline void GetMaximumPixelList(PixelList *pixel_list,Quantum *pixel) |
| 1944 | { |
| 1945 | register SkipList |
| 1946 | *p; |
| 1947 | |
| 1948 | size_t |
| 1949 | color, |
| 1950 | maximum; |
| 1951 | |
| 1952 | ssize_t |
| 1953 | count; |
| 1954 | |
| 1955 | /* |
| 1956 | Find the maximum value for each of the color. |
| 1957 | */ |
| 1958 | p=(&pixel_list->skip_list); |
| 1959 | color=65536L; |
| 1960 | count=0; |
| 1961 | maximum=p->nodes[color].next[0]; |
| 1962 | do |
| 1963 | { |
| 1964 | color=p->nodes[color].next[0]; |
| 1965 | if (color > maximum) |
| 1966 | maximum=color; |
| 1967 | count+=p->nodes[color].count; |
| 1968 | } while (count < (ssize_t) pixel_list->length); |
| 1969 | *pixel=ScaleShortToQuantum((unsigned short) maximum); |
| 1970 | } |
| 1971 | |
| 1972 | static inline void GetMeanPixelList(PixelList *pixel_list,Quantum *pixel) |
| 1973 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1974 | double |
cristy | 99bd523 | 2011-12-07 14:38:20 +0000 | [diff] [blame] | 1975 | sum; |
| 1976 | |
| 1977 | register SkipList |
| 1978 | *p; |
| 1979 | |
| 1980 | size_t |
| 1981 | color; |
| 1982 | |
| 1983 | ssize_t |
| 1984 | count; |
| 1985 | |
| 1986 | /* |
| 1987 | Find the mean value for each of the color. |
| 1988 | */ |
| 1989 | p=(&pixel_list->skip_list); |
| 1990 | color=65536L; |
| 1991 | count=0; |
| 1992 | sum=0.0; |
| 1993 | do |
| 1994 | { |
| 1995 | color=p->nodes[color].next[0]; |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1996 | sum+=(double) p->nodes[color].count*color; |
cristy | 99bd523 | 2011-12-07 14:38:20 +0000 | [diff] [blame] | 1997 | count+=p->nodes[color].count; |
| 1998 | } while (count < (ssize_t) pixel_list->length); |
| 1999 | sum/=pixel_list->length; |
| 2000 | *pixel=ScaleShortToQuantum((unsigned short) sum); |
| 2001 | } |
| 2002 | |
| 2003 | static inline void GetMedianPixelList(PixelList *pixel_list,Quantum *pixel) |
| 2004 | { |
| 2005 | register SkipList |
| 2006 | *p; |
| 2007 | |
| 2008 | size_t |
| 2009 | color; |
| 2010 | |
| 2011 | ssize_t |
| 2012 | count; |
| 2013 | |
| 2014 | /* |
| 2015 | Find the median value for each of the color. |
| 2016 | */ |
| 2017 | p=(&pixel_list->skip_list); |
| 2018 | color=65536L; |
| 2019 | count=0; |
| 2020 | do |
| 2021 | { |
| 2022 | color=p->nodes[color].next[0]; |
| 2023 | count+=p->nodes[color].count; |
| 2024 | } while (count <= (ssize_t) (pixel_list->length >> 1)); |
| 2025 | *pixel=ScaleShortToQuantum((unsigned short) color); |
| 2026 | } |
| 2027 | |
| 2028 | static inline void GetMinimumPixelList(PixelList *pixel_list,Quantum *pixel) |
| 2029 | { |
| 2030 | register SkipList |
| 2031 | *p; |
| 2032 | |
| 2033 | size_t |
| 2034 | color, |
| 2035 | minimum; |
| 2036 | |
| 2037 | ssize_t |
| 2038 | count; |
| 2039 | |
| 2040 | /* |
| 2041 | Find the minimum value for each of the color. |
| 2042 | */ |
| 2043 | p=(&pixel_list->skip_list); |
| 2044 | count=0; |
| 2045 | color=65536UL; |
| 2046 | minimum=p->nodes[color].next[0]; |
| 2047 | do |
| 2048 | { |
| 2049 | color=p->nodes[color].next[0]; |
| 2050 | if (color < minimum) |
| 2051 | minimum=color; |
| 2052 | count+=p->nodes[color].count; |
| 2053 | } while (count < (ssize_t) pixel_list->length); |
| 2054 | *pixel=ScaleShortToQuantum((unsigned short) minimum); |
| 2055 | } |
| 2056 | |
| 2057 | static inline void GetModePixelList(PixelList *pixel_list,Quantum *pixel) |
| 2058 | { |
| 2059 | register SkipList |
| 2060 | *p; |
| 2061 | |
| 2062 | size_t |
| 2063 | color, |
| 2064 | max_count, |
| 2065 | mode; |
| 2066 | |
| 2067 | ssize_t |
| 2068 | count; |
| 2069 | |
| 2070 | /* |
| 2071 | Make each pixel the 'predominant color' of the specified neighborhood. |
| 2072 | */ |
| 2073 | p=(&pixel_list->skip_list); |
| 2074 | color=65536L; |
| 2075 | mode=color; |
| 2076 | max_count=p->nodes[mode].count; |
| 2077 | count=0; |
| 2078 | do |
| 2079 | { |
| 2080 | color=p->nodes[color].next[0]; |
| 2081 | if (p->nodes[color].count > max_count) |
| 2082 | { |
| 2083 | mode=color; |
| 2084 | max_count=p->nodes[mode].count; |
| 2085 | } |
| 2086 | count+=p->nodes[color].count; |
| 2087 | } while (count < (ssize_t) pixel_list->length); |
| 2088 | *pixel=ScaleShortToQuantum((unsigned short) mode); |
| 2089 | } |
| 2090 | |
| 2091 | static inline void GetNonpeakPixelList(PixelList *pixel_list,Quantum *pixel) |
| 2092 | { |
| 2093 | register SkipList |
| 2094 | *p; |
| 2095 | |
| 2096 | size_t |
| 2097 | color, |
| 2098 | next, |
| 2099 | previous; |
| 2100 | |
| 2101 | ssize_t |
| 2102 | count; |
| 2103 | |
| 2104 | /* |
| 2105 | Finds the non peak value for each of the colors. |
| 2106 | */ |
| 2107 | p=(&pixel_list->skip_list); |
| 2108 | color=65536L; |
| 2109 | next=p->nodes[color].next[0]; |
| 2110 | count=0; |
| 2111 | do |
| 2112 | { |
| 2113 | previous=color; |
| 2114 | color=next; |
| 2115 | next=p->nodes[color].next[0]; |
| 2116 | count+=p->nodes[color].count; |
| 2117 | } while (count <= (ssize_t) (pixel_list->length >> 1)); |
| 2118 | if ((previous == 65536UL) && (next != 65536UL)) |
| 2119 | color=next; |
| 2120 | else |
| 2121 | if ((previous != 65536UL) && (next == 65536UL)) |
| 2122 | color=previous; |
| 2123 | *pixel=ScaleShortToQuantum((unsigned short) color); |
| 2124 | } |
| 2125 | |
| 2126 | static inline void GetStandardDeviationPixelList(PixelList *pixel_list, |
| 2127 | Quantum *pixel) |
| 2128 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 2129 | double |
cristy | 99bd523 | 2011-12-07 14:38:20 +0000 | [diff] [blame] | 2130 | sum, |
| 2131 | sum_squared; |
| 2132 | |
| 2133 | register SkipList |
| 2134 | *p; |
| 2135 | |
| 2136 | size_t |
| 2137 | color; |
| 2138 | |
| 2139 | ssize_t |
| 2140 | count; |
| 2141 | |
| 2142 | /* |
| 2143 | Find the standard-deviation value for each of the color. |
| 2144 | */ |
| 2145 | p=(&pixel_list->skip_list); |
| 2146 | color=65536L; |
| 2147 | count=0; |
| 2148 | sum=0.0; |
| 2149 | sum_squared=0.0; |
| 2150 | do |
| 2151 | { |
| 2152 | register ssize_t |
| 2153 | i; |
| 2154 | |
| 2155 | color=p->nodes[color].next[0]; |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 2156 | sum+=(double) p->nodes[color].count*color; |
cristy | 99bd523 | 2011-12-07 14:38:20 +0000 | [diff] [blame] | 2157 | for (i=0; i < (ssize_t) p->nodes[color].count; i++) |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 2158 | sum_squared+=((double) color)*((double) color); |
cristy | 99bd523 | 2011-12-07 14:38:20 +0000 | [diff] [blame] | 2159 | count+=p->nodes[color].count; |
| 2160 | } while (count < (ssize_t) pixel_list->length); |
| 2161 | sum/=pixel_list->length; |
| 2162 | sum_squared/=pixel_list->length; |
| 2163 | *pixel=ScaleShortToQuantum((unsigned short) sqrt(sum_squared-(sum*sum))); |
| 2164 | } |
| 2165 | |
| 2166 | static inline void InsertPixelList(const Image *image,const Quantum pixel, |
| 2167 | PixelList *pixel_list) |
| 2168 | { |
| 2169 | size_t |
| 2170 | signature; |
| 2171 | |
| 2172 | unsigned short |
| 2173 | index; |
| 2174 | |
| 2175 | index=ScaleQuantumToShort(pixel); |
| 2176 | signature=pixel_list->skip_list.nodes[index].signature; |
| 2177 | if (signature == pixel_list->signature) |
| 2178 | { |
| 2179 | pixel_list->skip_list.nodes[index].count++; |
| 2180 | return; |
| 2181 | } |
| 2182 | AddNodePixelList(pixel_list,index); |
| 2183 | } |
| 2184 | |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 2185 | static inline double MagickAbsoluteValue(const double x) |
cristy | 99bd523 | 2011-12-07 14:38:20 +0000 | [diff] [blame] | 2186 | { |
| 2187 | if (x < 0) |
| 2188 | return(-x); |
| 2189 | return(x); |
| 2190 | } |
| 2191 | |
| 2192 | static inline size_t MagickMax(const size_t x,const size_t y) |
| 2193 | { |
| 2194 | if (x > y) |
| 2195 | return(x); |
| 2196 | return(y); |
| 2197 | } |
| 2198 | |
| 2199 | static void ResetPixelList(PixelList *pixel_list) |
| 2200 | { |
| 2201 | int |
| 2202 | level; |
| 2203 | |
| 2204 | register SkipNode |
| 2205 | *root; |
| 2206 | |
| 2207 | register SkipList |
| 2208 | *p; |
| 2209 | |
| 2210 | /* |
| 2211 | Reset the skip-list. |
| 2212 | */ |
| 2213 | p=(&pixel_list->skip_list); |
| 2214 | root=p->nodes+65536UL; |
| 2215 | p->level=0; |
| 2216 | for (level=0; level < 9; level++) |
| 2217 | root->next[level]=65536UL; |
| 2218 | pixel_list->seed=pixel_list->signature++; |
| 2219 | } |
| 2220 | |
| 2221 | MagickExport Image *StatisticImage(const Image *image,const StatisticType type, |
| 2222 | const size_t width,const size_t height,ExceptionInfo *exception) |
| 2223 | { |
| 2224 | #define StatisticImageTag "Statistic/Image" |
| 2225 | |
| 2226 | CacheView |
| 2227 | *image_view, |
| 2228 | *statistic_view; |
| 2229 | |
| 2230 | Image |
| 2231 | *statistic_image; |
| 2232 | |
| 2233 | MagickBooleanType |
| 2234 | status; |
| 2235 | |
| 2236 | MagickOffsetType |
| 2237 | progress; |
| 2238 | |
| 2239 | PixelList |
| 2240 | **restrict pixel_list; |
| 2241 | |
| 2242 | ssize_t |
| 2243 | center, |
| 2244 | y; |
| 2245 | |
| 2246 | /* |
| 2247 | Initialize statistics image attributes. |
| 2248 | */ |
| 2249 | assert(image != (Image *) NULL); |
| 2250 | assert(image->signature == MagickSignature); |
| 2251 | if (image->debug != MagickFalse) |
| 2252 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 2253 | assert(exception != (ExceptionInfo *) NULL); |
| 2254 | assert(exception->signature == MagickSignature); |
| 2255 | statistic_image=CloneImage(image,image->columns,image->rows,MagickTrue, |
| 2256 | exception); |
| 2257 | if (statistic_image == (Image *) NULL) |
| 2258 | return((Image *) NULL); |
| 2259 | status=SetImageStorageClass(statistic_image,DirectClass,exception); |
| 2260 | if (status == MagickFalse) |
| 2261 | { |
| 2262 | statistic_image=DestroyImage(statistic_image); |
| 2263 | return((Image *) NULL); |
| 2264 | } |
| 2265 | pixel_list=AcquirePixelListThreadSet(MagickMax(width,1),MagickMax(height,1)); |
| 2266 | if (pixel_list == (PixelList **) NULL) |
| 2267 | { |
| 2268 | statistic_image=DestroyImage(statistic_image); |
| 2269 | ThrowImageException(ResourceLimitError,"MemoryAllocationFailed"); |
| 2270 | } |
| 2271 | /* |
| 2272 | Make each pixel the min / max / median / mode / etc. of the neighborhood. |
| 2273 | */ |
| 2274 | center=(ssize_t) GetPixelChannels(image)*(image->columns+MagickMax(width,1))* |
| 2275 | (MagickMax(height,1)/2L)+GetPixelChannels(image)*(MagickMax(width,1)/2L); |
| 2276 | status=MagickTrue; |
| 2277 | progress=0; |
cristy | db07095 | 2012-04-20 14:33:00 +0000 | [diff] [blame] | 2278 | image_view=AcquireVirtualCacheView(image,exception); |
| 2279 | statistic_view=AcquireAuthenticCacheView(statistic_image,exception); |
cristy | 99bd523 | 2011-12-07 14:38:20 +0000 | [diff] [blame] | 2280 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 2281 | #pragma omp parallel for schedule(static,4) shared(progress,status) \ |
cristy | 4ee2b0c | 2012-05-15 00:30:35 +0000 | [diff] [blame] | 2282 | dynamic_number_threads(image,image->columns,image->rows,1) |
cristy | 99bd523 | 2011-12-07 14:38:20 +0000 | [diff] [blame] | 2283 | #endif |
| 2284 | for (y=0; y < (ssize_t) statistic_image->rows; y++) |
| 2285 | { |
| 2286 | const int |
| 2287 | id = GetOpenMPThreadId(); |
| 2288 | |
| 2289 | register const Quantum |
| 2290 | *restrict p; |
| 2291 | |
| 2292 | register Quantum |
| 2293 | *restrict q; |
| 2294 | |
| 2295 | register ssize_t |
| 2296 | x; |
| 2297 | |
| 2298 | if (status == MagickFalse) |
| 2299 | continue; |
| 2300 | p=GetCacheViewVirtualPixels(image_view,-((ssize_t) MagickMax(width,1)/2L),y- |
| 2301 | (ssize_t) (MagickMax(height,1)/2L),image->columns+MagickMax(width,1), |
| 2302 | MagickMax(height,1),exception); |
| 2303 | q=QueueCacheViewAuthenticPixels(statistic_view,0,y,statistic_image->columns, 1,exception); |
| 2304 | if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL)) |
| 2305 | { |
| 2306 | status=MagickFalse; |
| 2307 | continue; |
| 2308 | } |
| 2309 | for (x=0; x < (ssize_t) statistic_image->columns; x++) |
| 2310 | { |
| 2311 | register ssize_t |
| 2312 | i; |
| 2313 | |
| 2314 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
| 2315 | { |
| 2316 | PixelChannel |
| 2317 | channel; |
| 2318 | |
| 2319 | PixelTrait |
| 2320 | statistic_traits, |
| 2321 | traits; |
| 2322 | |
| 2323 | Quantum |
| 2324 | pixel; |
| 2325 | |
| 2326 | register const Quantum |
| 2327 | *restrict pixels; |
| 2328 | |
| 2329 | register ssize_t |
| 2330 | u; |
| 2331 | |
| 2332 | ssize_t |
| 2333 | v; |
| 2334 | |
cristy | cf1296e | 2012-08-26 23:40:49 +0000 | [diff] [blame] | 2335 | channel=GetPixelChannelChannel(image,i); |
| 2336 | traits=GetPixelChannelTraits(image,channel); |
| 2337 | statistic_traits=GetPixelChannelTraits(statistic_image,channel); |
cristy | 99bd523 | 2011-12-07 14:38:20 +0000 | [diff] [blame] | 2338 | if ((traits == UndefinedPixelTrait) || |
| 2339 | (statistic_traits == UndefinedPixelTrait)) |
| 2340 | continue; |
cristy | 1eced09 | 2012-08-10 23:10:56 +0000 | [diff] [blame] | 2341 | if (((statistic_traits & CopyPixelTrait) != 0) || |
| 2342 | (GetPixelMask(image,p) != 0)) |
cristy | 99bd523 | 2011-12-07 14:38:20 +0000 | [diff] [blame] | 2343 | { |
| 2344 | SetPixelChannel(statistic_image,channel,p[center+i],q); |
| 2345 | continue; |
| 2346 | } |
| 2347 | pixels=p; |
| 2348 | ResetPixelList(pixel_list[id]); |
| 2349 | for (v=0; v < (ssize_t) MagickMax(height,1); v++) |
| 2350 | { |
| 2351 | for (u=0; u < (ssize_t) MagickMax(width,1); u++) |
| 2352 | { |
| 2353 | InsertPixelList(image,pixels[i],pixel_list[id]); |
| 2354 | pixels+=GetPixelChannels(image); |
| 2355 | } |
| 2356 | pixels+=image->columns*GetPixelChannels(image); |
| 2357 | } |
| 2358 | switch (type) |
| 2359 | { |
| 2360 | case GradientStatistic: |
| 2361 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 2362 | double |
cristy | 99bd523 | 2011-12-07 14:38:20 +0000 | [diff] [blame] | 2363 | maximum, |
| 2364 | minimum; |
| 2365 | |
| 2366 | GetMinimumPixelList(pixel_list[id],&pixel); |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 2367 | minimum=(double) pixel; |
cristy | 99bd523 | 2011-12-07 14:38:20 +0000 | [diff] [blame] | 2368 | GetMaximumPixelList(pixel_list[id],&pixel); |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 2369 | maximum=(double) pixel; |
cristy | 99bd523 | 2011-12-07 14:38:20 +0000 | [diff] [blame] | 2370 | pixel=ClampToQuantum(MagickAbsoluteValue(maximum-minimum)); |
| 2371 | break; |
| 2372 | } |
| 2373 | case MaximumStatistic: |
| 2374 | { |
| 2375 | GetMaximumPixelList(pixel_list[id],&pixel); |
| 2376 | break; |
| 2377 | } |
| 2378 | case MeanStatistic: |
| 2379 | { |
| 2380 | GetMeanPixelList(pixel_list[id],&pixel); |
| 2381 | break; |
| 2382 | } |
| 2383 | case MedianStatistic: |
| 2384 | default: |
| 2385 | { |
| 2386 | GetMedianPixelList(pixel_list[id],&pixel); |
| 2387 | break; |
| 2388 | } |
| 2389 | case MinimumStatistic: |
| 2390 | { |
| 2391 | GetMinimumPixelList(pixel_list[id],&pixel); |
| 2392 | break; |
| 2393 | } |
| 2394 | case ModeStatistic: |
| 2395 | { |
| 2396 | GetModePixelList(pixel_list[id],&pixel); |
| 2397 | break; |
| 2398 | } |
| 2399 | case NonpeakStatistic: |
| 2400 | { |
| 2401 | GetNonpeakPixelList(pixel_list[id],&pixel); |
| 2402 | break; |
| 2403 | } |
| 2404 | case StandardDeviationStatistic: |
| 2405 | { |
| 2406 | GetStandardDeviationPixelList(pixel_list[id],&pixel); |
| 2407 | break; |
| 2408 | } |
| 2409 | } |
| 2410 | SetPixelChannel(statistic_image,channel,pixel,q); |
| 2411 | } |
| 2412 | p+=GetPixelChannels(image); |
| 2413 | q+=GetPixelChannels(statistic_image); |
| 2414 | } |
| 2415 | if (SyncCacheViewAuthenticPixels(statistic_view,exception) == MagickFalse) |
| 2416 | status=MagickFalse; |
| 2417 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 2418 | { |
| 2419 | MagickBooleanType |
| 2420 | proceed; |
| 2421 | |
| 2422 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 2423 | #pragma omp critical (MagickCore_StatisticImage) |
cristy | 99bd523 | 2011-12-07 14:38:20 +0000 | [diff] [blame] | 2424 | #endif |
| 2425 | proceed=SetImageProgress(image,StatisticImageTag,progress++, |
| 2426 | image->rows); |
| 2427 | if (proceed == MagickFalse) |
| 2428 | status=MagickFalse; |
| 2429 | } |
| 2430 | } |
| 2431 | statistic_view=DestroyCacheView(statistic_view); |
| 2432 | image_view=DestroyCacheView(image_view); |
| 2433 | pixel_list=DestroyPixelListThreadSet(pixel_list); |
| 2434 | return(statistic_image); |
| 2435 | } |