cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % % |
| 6 | % EEEEE N N H H AAA N N CCCC EEEEE % |
| 7 | % E NN N H H A A NN N C E % |
| 8 | % EEE N N N HHHHH AAAAA N N N C EEE % |
| 9 | % E N NN H H A A N NN C E % |
| 10 | % EEEEE N N H H A A N N CCCC EEEEE % |
| 11 | % % |
| 12 | % % |
| 13 | % MagickCore Image Enhancement Methods % |
| 14 | % % |
| 15 | % Software Design % |
| 16 | % John Cristy % |
| 17 | % July 1992 % |
| 18 | % % |
| 19 | % % |
cristy | 45ef08f | 2012-12-07 13:13:34 +0000 | [diff] [blame] | 20 | % Copyright 1999-2013 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/artifact.h" |
cristy | 5836c74 | 2013-03-27 21:10:56 +0000 | [diff] [blame] | 45 | #include "MagickCore/attribute.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 46 | #include "MagickCore/cache.h" |
| 47 | #include "MagickCore/cache-view.h" |
cristy | 6a2180c | 2013-05-27 10:28:36 +0000 | [diff] [blame] | 48 | #include "MagickCore/channel.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 49 | #include "MagickCore/color.h" |
| 50 | #include "MagickCore/color-private.h" |
| 51 | #include "MagickCore/colorspace.h" |
cristy | 0898eba | 2012-04-09 16:38:29 +0000 | [diff] [blame] | 52 | #include "MagickCore/colorspace-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 53 | #include "MagickCore/composite-private.h" |
| 54 | #include "MagickCore/enhance.h" |
| 55 | #include "MagickCore/exception.h" |
| 56 | #include "MagickCore/exception-private.h" |
| 57 | #include "MagickCore/fx.h" |
| 58 | #include "MagickCore/gem.h" |
cristy | d1dd6e4 | 2011-09-04 01:46:08 +0000 | [diff] [blame] | 59 | #include "MagickCore/gem-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 60 | #include "MagickCore/geometry.h" |
| 61 | #include "MagickCore/histogram.h" |
| 62 | #include "MagickCore/image.h" |
| 63 | #include "MagickCore/image-private.h" |
| 64 | #include "MagickCore/memory_.h" |
| 65 | #include "MagickCore/monitor.h" |
| 66 | #include "MagickCore/monitor-private.h" |
| 67 | #include "MagickCore/option.h" |
cristy | 1e0fe42 | 2012-04-11 18:43:52 +0000 | [diff] [blame] | 68 | #include "MagickCore/pixel.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 69 | #include "MagickCore/pixel-accessor.h" |
| 70 | #include "MagickCore/quantum.h" |
| 71 | #include "MagickCore/quantum-private.h" |
| 72 | #include "MagickCore/resample.h" |
| 73 | #include "MagickCore/resample-private.h" |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 74 | #include "MagickCore/resource_.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 75 | #include "MagickCore/statistic.h" |
| 76 | #include "MagickCore/string_.h" |
| 77 | #include "MagickCore/string-private.h" |
| 78 | #include "MagickCore/thread-private.h" |
cristy | d1e1c22 | 2012-08-13 01:13:28 +0000 | [diff] [blame] | 79 | #include "MagickCore/threshold.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 80 | #include "MagickCore/token.h" |
| 81 | #include "MagickCore/xml-tree.h" |
cristy | 433d118 | 2011-09-04 13:38:52 +0000 | [diff] [blame] | 82 | #include "MagickCore/xml-tree-private.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 83 | |
| 84 | /* |
| 85 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 86 | % % |
| 87 | % % |
| 88 | % % |
| 89 | % A u t o G a m m a I m a g e % |
| 90 | % % |
| 91 | % % |
| 92 | % % |
| 93 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 94 | % |
| 95 | % AutoGammaImage() extract the 'mean' from the image and adjust the image |
| 96 | % to try make set its gamma appropriatally. |
| 97 | % |
cristy | 308b4e6 | 2009-09-21 14:40:44 +0000 | [diff] [blame] | 98 | % The format of the AutoGammaImage method is: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 99 | % |
cristy | 9511120 | 2011-08-09 19:41:42 +0000 | [diff] [blame] | 100 | % MagickBooleanType AutoGammaImage(Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 101 | % |
| 102 | % A description of each parameter follows: |
| 103 | % |
| 104 | % o image: The image to auto-level |
| 105 | % |
cristy | 9511120 | 2011-08-09 19:41:42 +0000 | [diff] [blame] | 106 | % o exception: return any errors or warnings in this structure. |
| 107 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 108 | */ |
cristy | 9511120 | 2011-08-09 19:41:42 +0000 | [diff] [blame] | 109 | MagickExport MagickBooleanType AutoGammaImage(Image *image, |
| 110 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 111 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 112 | double |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 113 | gamma, |
| 114 | log_mean, |
| 115 | mean, |
| 116 | sans; |
anthony | 4efe597 | 2009-09-11 06:46:40 +0000 | [diff] [blame] | 117 | |
cristy | 9511120 | 2011-08-09 19:41:42 +0000 | [diff] [blame] | 118 | MagickStatusType |
| 119 | status; |
| 120 | |
cristy | 01e9afd | 2011-08-10 17:38:41 +0000 | [diff] [blame] | 121 | register ssize_t |
| 122 | i; |
| 123 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 124 | log_mean=log(0.5); |
cristy | 5f95f4f | 2011-10-23 01:01:01 +0000 | [diff] [blame] | 125 | if (image->channel_mask == DefaultChannels) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 126 | { |
| 127 | /* |
cristy | 01e9afd | 2011-08-10 17:38:41 +0000 | [diff] [blame] | 128 | Apply gamma correction equally across all given channels. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 129 | */ |
cristy | 9511120 | 2011-08-09 19:41:42 +0000 | [diff] [blame] | 130 | (void) GetImageMean(image,&mean,&sans,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 131 | gamma=log(mean*QuantumScale)/log_mean; |
cristy | 01e9afd | 2011-08-10 17:38:41 +0000 | [diff] [blame] | 132 | return(LevelImage(image,0.0,(double) QuantumRange,gamma,exception)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 133 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 134 | /* |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 135 | Auto-gamma each channel separately. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 136 | */ |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 137 | status=MagickTrue; |
cristy | 01e9afd | 2011-08-10 17:38:41 +0000 | [diff] [blame] | 138 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
| 139 | { |
cristy | bd5a96c | 2011-08-21 00:04:26 +0000 | [diff] [blame] | 140 | ChannelType |
| 141 | channel_mask; |
| 142 | |
cristy | 5a23c55 | 2013-02-13 14:34:28 +0000 | [diff] [blame] | 143 | PixelChannel channel=GetPixelChannelChannel(image,i); |
| 144 | PixelTrait traits=GetPixelChannelTraits(image,channel); |
cristy | 01e9afd | 2011-08-10 17:38:41 +0000 | [diff] [blame] | 145 | if ((traits & UpdatePixelTrait) == 0) |
| 146 | continue; |
cristy | cf1296e | 2012-08-26 23:40:49 +0000 | [diff] [blame] | 147 | channel_mask=SetImageChannelMask(image,(ChannelType) (1 << i)); |
cristy | 01e9afd | 2011-08-10 17:38:41 +0000 | [diff] [blame] | 148 | status=GetImageMean(image,&mean,&sans,exception); |
| 149 | gamma=log(mean*QuantumScale)/log_mean; |
| 150 | status&=LevelImage(image,0.0,(double) QuantumRange,gamma,exception); |
cristy | cf1296e | 2012-08-26 23:40:49 +0000 | [diff] [blame] | 151 | (void) SetImageChannelMask(image,channel_mask); |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 152 | if( IfMagickFalse(status) ) |
cristy | 01e9afd | 2011-08-10 17:38:41 +0000 | [diff] [blame] | 153 | break; |
| 154 | } |
cristy | ee1bdaa | 2013-07-16 16:45:03 +0000 | [diff] [blame] | 155 | return(status != 0 ? MagickTrue : MagickFalse); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /* |
| 159 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 160 | % % |
| 161 | % % |
| 162 | % % |
| 163 | % A u t o L e v e l I m a g e % |
| 164 | % % |
| 165 | % % |
| 166 | % % |
| 167 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 168 | % |
| 169 | % AutoLevelImage() adjusts the levels of a particular image channel by |
| 170 | % scaling the minimum and maximum values to the full quantum range. |
| 171 | % |
| 172 | % The format of the LevelImage method is: |
| 173 | % |
cristy | 9511120 | 2011-08-09 19:41:42 +0000 | [diff] [blame] | 174 | % MagickBooleanType AutoLevelImage(Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 175 | % |
| 176 | % A description of each parameter follows: |
| 177 | % |
| 178 | % o image: The image to auto-level |
| 179 | % |
cristy | 9511120 | 2011-08-09 19:41:42 +0000 | [diff] [blame] | 180 | % o exception: return any errors or warnings in this structure. |
| 181 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 182 | */ |
cristy | 9511120 | 2011-08-09 19:41:42 +0000 | [diff] [blame] | 183 | MagickExport MagickBooleanType AutoLevelImage(Image *image, |
| 184 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 185 | { |
cristy | b303c3d | 2011-09-09 11:24:40 +0000 | [diff] [blame] | 186 | return(MinMaxStretchImage(image,0.0,0.0,1.0,exception)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /* |
| 190 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 191 | % % |
| 192 | % % |
| 193 | % % |
cristy | a28d6b8 | 2010-01-11 20:03:47 +0000 | [diff] [blame] | 194 | % B r i g h t n e s s C o n t r a s t I m a g e % |
| 195 | % % |
| 196 | % % |
| 197 | % % |
| 198 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 199 | % |
cristy | f4356f9 | 2011-08-01 15:33:48 +0000 | [diff] [blame] | 200 | % BrightnessContrastImage() changes the brightness and/or contrast of an |
| 201 | % image. It converts the brightness and contrast parameters into slope and |
| 202 | % intercept and calls a polynomical function to apply to the image. |
cristy | a28d6b8 | 2010-01-11 20:03:47 +0000 | [diff] [blame] | 203 | % |
| 204 | % The format of the BrightnessContrastImage method is: |
| 205 | % |
| 206 | % MagickBooleanType BrightnessContrastImage(Image *image, |
cristy | 444eda6 | 2011-08-10 02:07:46 +0000 | [diff] [blame] | 207 | % const double brightness,const double contrast,ExceptionInfo *exception) |
cristy | a28d6b8 | 2010-01-11 20:03:47 +0000 | [diff] [blame] | 208 | % |
| 209 | % A description of each parameter follows: |
| 210 | % |
| 211 | % o image: the image. |
| 212 | % |
cristy | a28d6b8 | 2010-01-11 20:03:47 +0000 | [diff] [blame] | 213 | % o brightness: the brightness percent (-100 .. 100). |
| 214 | % |
| 215 | % o contrast: the contrast percent (-100 .. 100). |
| 216 | % |
cristy | 444eda6 | 2011-08-10 02:07:46 +0000 | [diff] [blame] | 217 | % o exception: return any errors or warnings in this structure. |
| 218 | % |
cristy | a28d6b8 | 2010-01-11 20:03:47 +0000 | [diff] [blame] | 219 | */ |
cristy | a28d6b8 | 2010-01-11 20:03:47 +0000 | [diff] [blame] | 220 | MagickExport MagickBooleanType BrightnessContrastImage(Image *image, |
cristy | 444eda6 | 2011-08-10 02:07:46 +0000 | [diff] [blame] | 221 | const double brightness,const double contrast,ExceptionInfo *exception) |
cristy | a28d6b8 | 2010-01-11 20:03:47 +0000 | [diff] [blame] | 222 | { |
cristy | a28d6b8 | 2010-01-11 20:03:47 +0000 | [diff] [blame] | 223 | #define BrightnessContastImageTag "BrightnessContast/Image" |
| 224 | |
| 225 | double |
| 226 | alpha, |
cristy | a28d6b8 | 2010-01-11 20:03:47 +0000 | [diff] [blame] | 227 | coefficients[2], |
cristy | e23ec9d | 2011-08-16 18:15:40 +0000 | [diff] [blame] | 228 | intercept, |
cristy | a28d6b8 | 2010-01-11 20:03:47 +0000 | [diff] [blame] | 229 | slope; |
| 230 | |
| 231 | MagickBooleanType |
| 232 | status; |
| 233 | |
| 234 | /* |
| 235 | Compute slope and intercept. |
| 236 | */ |
| 237 | assert(image != (Image *) NULL); |
| 238 | assert(image->signature == MagickSignature); |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 239 | if( IfMagickTrue(image->debug) ) |
cristy | a28d6b8 | 2010-01-11 20:03:47 +0000 | [diff] [blame] | 240 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 241 | alpha=contrast; |
cristy | 4205a3c | 2010-09-12 20:19:59 +0000 | [diff] [blame] | 242 | slope=tan((double) (MagickPI*(alpha/100.0+1.0)/4.0)); |
cristy | a28d6b8 | 2010-01-11 20:03:47 +0000 | [diff] [blame] | 243 | if (slope < 0.0) |
| 244 | slope=0.0; |
| 245 | intercept=brightness/100.0+((100-brightness)/200.0)*(1.0-slope); |
| 246 | coefficients[0]=slope; |
| 247 | coefficients[1]=intercept; |
cristy | 444eda6 | 2011-08-10 02:07:46 +0000 | [diff] [blame] | 248 | status=FunctionImage(image,PolynomialFunction,2,coefficients,exception); |
| 249 | return(status); |
| 250 | } |
| 251 | |
| 252 | /* |
| 253 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 254 | % % |
| 255 | % % |
| 256 | % % |
| 257 | % C l u t I m a g e % |
| 258 | % % |
| 259 | % % |
| 260 | % % |
| 261 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 262 | % |
| 263 | % ClutImage() replaces each color value in the given image, by using it as an |
| 264 | % index to lookup a replacement color value in a Color Look UP Table in the |
| 265 | % form of an image. The values are extracted along a diagonal of the CLUT |
| 266 | % image so either a horizontal or vertial gradient image can be used. |
| 267 | % |
| 268 | % Typically this is used to either re-color a gray-scale image according to a |
| 269 | % color gradient in the CLUT image, or to perform a freeform histogram |
| 270 | % (level) adjustment according to the (typically gray-scale) gradient in the |
| 271 | % CLUT image. |
| 272 | % |
| 273 | % When the 'channel' mask includes the matte/alpha transparency channel but |
| 274 | % one image has no such channel it is assumed that that image is a simple |
| 275 | % gray-scale image that will effect the alpha channel values, either for |
| 276 | % gray-scale coloring (with transparent or semi-transparent colors), or |
| 277 | % a histogram adjustment of existing alpha channel values. If both images |
| 278 | % have matte channels, direct and normal indexing is applied, which is rarely |
| 279 | % used. |
| 280 | % |
| 281 | % The format of the ClutImage method is: |
| 282 | % |
| 283 | % MagickBooleanType ClutImage(Image *image,Image *clut_image, |
cristy | 5c4e258 | 2011-09-11 19:21:03 +0000 | [diff] [blame] | 284 | % const PixelInterpolateMethod method,ExceptionInfo *exception) |
cristy | 444eda6 | 2011-08-10 02:07:46 +0000 | [diff] [blame] | 285 | % |
| 286 | % A description of each parameter follows: |
| 287 | % |
| 288 | % o image: the image, which is replaced by indexed CLUT values |
| 289 | % |
| 290 | % o clut_image: the color lookup table image for replacement color values. |
| 291 | % |
cristy | 5c4e258 | 2011-09-11 19:21:03 +0000 | [diff] [blame] | 292 | % o method: the pixel interpolation method. |
cristy | 444eda6 | 2011-08-10 02:07:46 +0000 | [diff] [blame] | 293 | % |
| 294 | % o exception: return any errors or warnings in this structure. |
| 295 | % |
| 296 | */ |
| 297 | MagickExport MagickBooleanType ClutImage(Image *image,const Image *clut_image, |
cristy | 5c4e258 | 2011-09-11 19:21:03 +0000 | [diff] [blame] | 298 | const PixelInterpolateMethod method,ExceptionInfo *exception) |
cristy | 444eda6 | 2011-08-10 02:07:46 +0000 | [diff] [blame] | 299 | { |
cristy | 444eda6 | 2011-08-10 02:07:46 +0000 | [diff] [blame] | 300 | #define ClutImageTag "Clut/Image" |
| 301 | |
| 302 | CacheView |
| 303 | *clut_view, |
| 304 | *image_view; |
| 305 | |
cristy | 444eda6 | 2011-08-10 02:07:46 +0000 | [diff] [blame] | 306 | MagickBooleanType |
| 307 | status; |
| 308 | |
| 309 | MagickOffsetType |
| 310 | progress; |
| 311 | |
cristy | 1e0fe42 | 2012-04-11 18:43:52 +0000 | [diff] [blame] | 312 | PixelInfo |
| 313 | *clut_map; |
cristy | 444eda6 | 2011-08-10 02:07:46 +0000 | [diff] [blame] | 314 | |
cristy | 1e0fe42 | 2012-04-11 18:43:52 +0000 | [diff] [blame] | 315 | register ssize_t |
| 316 | i; |
| 317 | |
| 318 | ssize_t adjust, |
cristy | 444eda6 | 2011-08-10 02:07:46 +0000 | [diff] [blame] | 319 | y; |
| 320 | |
| 321 | assert(image != (Image *) NULL); |
| 322 | assert(image->signature == MagickSignature); |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 323 | if( IfMagickTrue(image->debug) ) |
cristy | 444eda6 | 2011-08-10 02:07:46 +0000 | [diff] [blame] | 324 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 325 | assert(clut_image != (Image *) NULL); |
| 326 | assert(clut_image->signature == MagickSignature); |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 327 | if( IfMagickFalse(SetImageStorageClass(image,DirectClass,exception)) ) |
cristy | 444eda6 | 2011-08-10 02:07:46 +0000 | [diff] [blame] | 328 | return(MagickFalse); |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 329 | if( IfMagickTrue(IsGrayColorspace(image->colorspace)) && |
| 330 | IfMagickFalse(IsGrayColorspace(clut_image->colorspace))) |
cristy | 0c81d06 | 2013-04-21 15:22:02 +0000 | [diff] [blame] | 331 | (void) SetImageColorspace(image,sRGBColorspace,exception); |
cristy | 1e0fe42 | 2012-04-11 18:43:52 +0000 | [diff] [blame] | 332 | clut_map=(PixelInfo *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*clut_map)); |
| 333 | if (clut_map == (PixelInfo *) NULL) |
cristy | 444eda6 | 2011-08-10 02:07:46 +0000 | [diff] [blame] | 334 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
| 335 | image->filename); |
| 336 | /* |
| 337 | Clut image. |
| 338 | */ |
| 339 | status=MagickTrue; |
| 340 | progress=0; |
| 341 | adjust=(ssize_t) (clut_image->interpolate == IntegerInterpolatePixel ? 0 : 1); |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 342 | clut_view=AcquireVirtualCacheView(clut_image,exception); |
cristy | 1e0fe42 | 2012-04-11 18:43:52 +0000 | [diff] [blame] | 343 | for (i=0; i <= (ssize_t) MaxMap; i++) |
cristy | 444eda6 | 2011-08-10 02:07:46 +0000 | [diff] [blame] | 344 | { |
cristy | 1e0fe42 | 2012-04-11 18:43:52 +0000 | [diff] [blame] | 345 | GetPixelInfo(clut_image,clut_map+i); |
anthony | 6feb780 | 2012-04-12 06:24:29 +0000 | [diff] [blame] | 346 | (void) InterpolatePixelInfo(clut_image,clut_view,method, |
cristy | 1e0fe42 | 2012-04-11 18:43:52 +0000 | [diff] [blame] | 347 | QuantumScale*i*(clut_image->columns-adjust),QuantumScale*i* |
| 348 | (clut_image->rows-adjust),clut_map+i,exception); |
cristy | 444eda6 | 2011-08-10 02:07:46 +0000 | [diff] [blame] | 349 | } |
| 350 | clut_view=DestroyCacheView(clut_view); |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 351 | image_view=AcquireAuthenticCacheView(image,exception); |
cristy | 444eda6 | 2011-08-10 02:07:46 +0000 | [diff] [blame] | 352 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 353 | #pragma omp parallel for schedule(static,4) shared(progress,status) \ |
cristy | 5e6b259 | 2012-12-19 14:08:11 +0000 | [diff] [blame] | 354 | magick_threads(image,image,image->rows,1) |
cristy | 444eda6 | 2011-08-10 02:07:46 +0000 | [diff] [blame] | 355 | #endif |
| 356 | for (y=0; y < (ssize_t) image->rows; y++) |
| 357 | { |
cristy | 1e0fe42 | 2012-04-11 18:43:52 +0000 | [diff] [blame] | 358 | PixelInfo |
| 359 | pixel; |
| 360 | |
cristy | 444eda6 | 2011-08-10 02:07:46 +0000 | [diff] [blame] | 361 | register Quantum |
| 362 | *restrict q; |
| 363 | |
| 364 | register ssize_t |
| 365 | x; |
| 366 | |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 367 | if( IfMagickFalse(status) ) |
cristy | 444eda6 | 2011-08-10 02:07:46 +0000 | [diff] [blame] | 368 | continue; |
| 369 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
| 370 | if (q == (Quantum *) NULL) |
| 371 | { |
| 372 | status=MagickFalse; |
| 373 | continue; |
| 374 | } |
cristy | 1e0fe42 | 2012-04-11 18:43:52 +0000 | [diff] [blame] | 375 | GetPixelInfo(image,&pixel); |
cristy | 444eda6 | 2011-08-10 02:07:46 +0000 | [diff] [blame] | 376 | for (x=0; x < (ssize_t) image->columns; x++) |
| 377 | { |
cristy | 883fde1 | 2013-04-08 00:50:13 +0000 | [diff] [blame] | 378 | if (GetPixelReadMask(image,q) == 0) |
cristy | 10a6c61 | 2012-01-29 21:41:05 +0000 | [diff] [blame] | 379 | { |
| 380 | q+=GetPixelChannels(image); |
| 381 | continue; |
| 382 | } |
cristy | 1e0fe42 | 2012-04-11 18:43:52 +0000 | [diff] [blame] | 383 | GetPixelInfoPixel(image,q,&pixel); |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 384 | pixel.red=clut_map[ScaleQuantumToMap( |
| 385 | ClampToQuantum(pixel.red))].red; |
| 386 | pixel.green=clut_map[ScaleQuantumToMap( |
| 387 | ClampToQuantum(pixel.green))].green; |
| 388 | pixel.blue=clut_map[ScaleQuantumToMap( |
| 389 | ClampToQuantum(pixel.blue))].blue; |
| 390 | pixel.black=clut_map[ScaleQuantumToMap( |
| 391 | ClampToQuantum(pixel.black))].black; |
| 392 | pixel.alpha=clut_map[ScaleQuantumToMap( |
| 393 | ClampToQuantum(pixel.alpha))].alpha; |
cristy | 1e0fe42 | 2012-04-11 18:43:52 +0000 | [diff] [blame] | 394 | SetPixelInfoPixel(image,&pixel,q); |
cristy | 444eda6 | 2011-08-10 02:07:46 +0000 | [diff] [blame] | 395 | q+=GetPixelChannels(image); |
| 396 | } |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 397 | if( IfMagickFalse(SyncCacheViewAuthenticPixels(image_view,exception)) ) |
cristy | 444eda6 | 2011-08-10 02:07:46 +0000 | [diff] [blame] | 398 | status=MagickFalse; |
| 399 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 400 | { |
| 401 | MagickBooleanType |
| 402 | proceed; |
| 403 | |
| 404 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 564a569 | 2012-01-20 23:56:26 +0000 | [diff] [blame] | 405 | #pragma omp critical (MagickCore_ClutImage) |
cristy | 444eda6 | 2011-08-10 02:07:46 +0000 | [diff] [blame] | 406 | #endif |
| 407 | proceed=SetImageProgress(image,ClutImageTag,progress++,image->rows); |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 408 | if( IfMagickFalse(proceed) ) |
cristy | 444eda6 | 2011-08-10 02:07:46 +0000 | [diff] [blame] | 409 | status=MagickFalse; |
| 410 | } |
| 411 | } |
| 412 | image_view=DestroyCacheView(image_view); |
cristy | 1e0fe42 | 2012-04-11 18:43:52 +0000 | [diff] [blame] | 413 | clut_map=(PixelInfo *) RelinquishMagickMemory(clut_map); |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 414 | if ((clut_image->alpha_trait == BlendPixelTrait) && |
cristy | 444eda6 | 2011-08-10 02:07:46 +0000 | [diff] [blame] | 415 | ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)) |
| 416 | (void) SetImageAlphaChannel(image,ActivateAlphaChannel,exception); |
cristy | a28d6b8 | 2010-01-11 20:03:47 +0000 | [diff] [blame] | 417 | return(status); |
| 418 | } |
| 419 | |
| 420 | /* |
| 421 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 422 | % % |
| 423 | % % |
| 424 | % % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 425 | % C o l o r D e c i s i o n L i s t I m a g e % |
| 426 | % % |
| 427 | % % |
| 428 | % % |
| 429 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 430 | % |
| 431 | % ColorDecisionListImage() accepts a lightweight Color Correction Collection |
| 432 | % (CCC) file which solely contains one or more color corrections and applies |
| 433 | % the correction to the image. Here is a sample CCC file: |
| 434 | % |
| 435 | % <ColorCorrectionCollection xmlns="urn:ASC:CDL:v1.2"> |
| 436 | % <ColorCorrection id="cc03345"> |
| 437 | % <SOPNode> |
| 438 | % <Slope> 0.9 1.2 0.5 </Slope> |
| 439 | % <Offset> 0.4 -0.5 0.6 </Offset> |
| 440 | % <Power> 1.0 0.8 1.5 </Power> |
| 441 | % </SOPNode> |
| 442 | % <SATNode> |
| 443 | % <Saturation> 0.85 </Saturation> |
| 444 | % </SATNode> |
| 445 | % </ColorCorrection> |
| 446 | % </ColorCorrectionCollection> |
| 447 | % |
| 448 | % which includes the slop, offset, and power for each of the RGB channels |
| 449 | % as well as the saturation. |
| 450 | % |
| 451 | % The format of the ColorDecisionListImage method is: |
| 452 | % |
| 453 | % MagickBooleanType ColorDecisionListImage(Image *image, |
cristy | 1bfa9f0 | 2011-08-11 02:35:43 +0000 | [diff] [blame] | 454 | % const char *color_correction_collection,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 455 | % |
| 456 | % A description of each parameter follows: |
| 457 | % |
| 458 | % o image: the image. |
| 459 | % |
| 460 | % o color_correction_collection: the color correction collection in XML. |
| 461 | % |
cristy | 1bfa9f0 | 2011-08-11 02:35:43 +0000 | [diff] [blame] | 462 | % o exception: return any errors or warnings in this structure. |
| 463 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 464 | */ |
| 465 | MagickExport MagickBooleanType ColorDecisionListImage(Image *image, |
cristy | 1bfa9f0 | 2011-08-11 02:35:43 +0000 | [diff] [blame] | 466 | const char *color_correction_collection,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 467 | { |
| 468 | #define ColorDecisionListCorrectImageTag "ColorDecisionList/Image" |
| 469 | |
| 470 | typedef struct _Correction |
| 471 | { |
| 472 | double |
| 473 | slope, |
| 474 | offset, |
| 475 | power; |
| 476 | } Correction; |
| 477 | |
| 478 | typedef struct _ColorCorrection |
| 479 | { |
| 480 | Correction |
| 481 | red, |
| 482 | green, |
| 483 | blue; |
| 484 | |
| 485 | double |
| 486 | saturation; |
| 487 | } ColorCorrection; |
| 488 | |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 489 | CacheView |
| 490 | *image_view; |
| 491 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 492 | char |
| 493 | token[MaxTextExtent]; |
| 494 | |
| 495 | ColorCorrection |
| 496 | color_correction; |
| 497 | |
| 498 | const char |
| 499 | *content, |
| 500 | *p; |
| 501 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 502 | MagickBooleanType |
| 503 | status; |
| 504 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 505 | MagickOffsetType |
| 506 | progress; |
| 507 | |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 508 | PixelInfo |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 509 | *cdl_map; |
| 510 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 511 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 512 | i; |
| 513 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 514 | ssize_t |
| 515 | y; |
| 516 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 517 | XMLTreeInfo |
| 518 | *cc, |
| 519 | *ccc, |
| 520 | *sat, |
| 521 | *sop; |
| 522 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 523 | /* |
| 524 | Allocate and initialize cdl maps. |
| 525 | */ |
| 526 | assert(image != (Image *) NULL); |
| 527 | assert(image->signature == MagickSignature); |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 528 | if( IfMagickTrue(image->debug) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 529 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 530 | if (color_correction_collection == (const char *) NULL) |
| 531 | return(MagickFalse); |
cristy | 1bfa9f0 | 2011-08-11 02:35:43 +0000 | [diff] [blame] | 532 | ccc=NewXMLTree((const char *) color_correction_collection,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 533 | if (ccc == (XMLTreeInfo *) NULL) |
| 534 | return(MagickFalse); |
| 535 | cc=GetXMLTreeChild(ccc,"ColorCorrection"); |
| 536 | if (cc == (XMLTreeInfo *) NULL) |
| 537 | { |
| 538 | ccc=DestroyXMLTree(ccc); |
| 539 | return(MagickFalse); |
| 540 | } |
| 541 | color_correction.red.slope=1.0; |
| 542 | color_correction.red.offset=0.0; |
| 543 | color_correction.red.power=1.0; |
| 544 | color_correction.green.slope=1.0; |
| 545 | color_correction.green.offset=0.0; |
| 546 | color_correction.green.power=1.0; |
| 547 | color_correction.blue.slope=1.0; |
| 548 | color_correction.blue.offset=0.0; |
| 549 | color_correction.blue.power=1.0; |
| 550 | color_correction.saturation=0.0; |
| 551 | sop=GetXMLTreeChild(cc,"SOPNode"); |
| 552 | if (sop != (XMLTreeInfo *) NULL) |
| 553 | { |
| 554 | XMLTreeInfo |
| 555 | *offset, |
| 556 | *power, |
| 557 | *slope; |
| 558 | |
| 559 | slope=GetXMLTreeChild(sop,"Slope"); |
| 560 | if (slope != (XMLTreeInfo *) NULL) |
| 561 | { |
| 562 | content=GetXMLTreeContent(slope); |
| 563 | p=(const char *) content; |
| 564 | for (i=0; (*p != '\0') && (i < 3); i++) |
| 565 | { |
| 566 | GetMagickToken(p,&p,token); |
| 567 | if (*token == ',') |
| 568 | GetMagickToken(p,&p,token); |
| 569 | switch (i) |
| 570 | { |
cristy | c1acd84 | 2011-05-19 23:05:47 +0000 | [diff] [blame] | 571 | case 0: |
| 572 | { |
cristy | 9b34e30 | 2011-11-05 02:15:45 +0000 | [diff] [blame] | 573 | color_correction.red.slope=StringToDouble(token,(char **) NULL); |
cristy | c1acd84 | 2011-05-19 23:05:47 +0000 | [diff] [blame] | 574 | break; |
| 575 | } |
| 576 | case 1: |
| 577 | { |
cristy | dbdd0e3 | 2011-11-04 23:29:40 +0000 | [diff] [blame] | 578 | color_correction.green.slope=StringToDouble(token, |
cristy | c1acd84 | 2011-05-19 23:05:47 +0000 | [diff] [blame] | 579 | (char **) NULL); |
| 580 | break; |
| 581 | } |
| 582 | case 2: |
| 583 | { |
cristy | dbdd0e3 | 2011-11-04 23:29:40 +0000 | [diff] [blame] | 584 | color_correction.blue.slope=StringToDouble(token, |
cristy | c1acd84 | 2011-05-19 23:05:47 +0000 | [diff] [blame] | 585 | (char **) NULL); |
| 586 | break; |
| 587 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 588 | } |
| 589 | } |
| 590 | } |
| 591 | offset=GetXMLTreeChild(sop,"Offset"); |
| 592 | if (offset != (XMLTreeInfo *) NULL) |
| 593 | { |
| 594 | content=GetXMLTreeContent(offset); |
| 595 | p=(const char *) content; |
| 596 | for (i=0; (*p != '\0') && (i < 3); i++) |
| 597 | { |
| 598 | GetMagickToken(p,&p,token); |
| 599 | if (*token == ',') |
| 600 | GetMagickToken(p,&p,token); |
| 601 | switch (i) |
| 602 | { |
cristy | c1acd84 | 2011-05-19 23:05:47 +0000 | [diff] [blame] | 603 | case 0: |
| 604 | { |
cristy | dbdd0e3 | 2011-11-04 23:29:40 +0000 | [diff] [blame] | 605 | color_correction.red.offset=StringToDouble(token, |
cristy | c1acd84 | 2011-05-19 23:05:47 +0000 | [diff] [blame] | 606 | (char **) NULL); |
| 607 | break; |
| 608 | } |
| 609 | case 1: |
| 610 | { |
cristy | dbdd0e3 | 2011-11-04 23:29:40 +0000 | [diff] [blame] | 611 | color_correction.green.offset=StringToDouble(token, |
cristy | c1acd84 | 2011-05-19 23:05:47 +0000 | [diff] [blame] | 612 | (char **) NULL); |
| 613 | break; |
| 614 | } |
| 615 | case 2: |
| 616 | { |
cristy | dbdd0e3 | 2011-11-04 23:29:40 +0000 | [diff] [blame] | 617 | color_correction.blue.offset=StringToDouble(token, |
cristy | c1acd84 | 2011-05-19 23:05:47 +0000 | [diff] [blame] | 618 | (char **) NULL); |
| 619 | break; |
| 620 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 621 | } |
| 622 | } |
| 623 | } |
| 624 | power=GetXMLTreeChild(sop,"Power"); |
| 625 | if (power != (XMLTreeInfo *) NULL) |
| 626 | { |
| 627 | content=GetXMLTreeContent(power); |
| 628 | p=(const char *) content; |
| 629 | for (i=0; (*p != '\0') && (i < 3); i++) |
| 630 | { |
| 631 | GetMagickToken(p,&p,token); |
| 632 | if (*token == ',') |
| 633 | GetMagickToken(p,&p,token); |
| 634 | switch (i) |
| 635 | { |
cristy | c1acd84 | 2011-05-19 23:05:47 +0000 | [diff] [blame] | 636 | case 0: |
| 637 | { |
cristy | 9b34e30 | 2011-11-05 02:15:45 +0000 | [diff] [blame] | 638 | color_correction.red.power=StringToDouble(token,(char **) NULL); |
cristy | c1acd84 | 2011-05-19 23:05:47 +0000 | [diff] [blame] | 639 | break; |
| 640 | } |
| 641 | case 1: |
| 642 | { |
cristy | dbdd0e3 | 2011-11-04 23:29:40 +0000 | [diff] [blame] | 643 | color_correction.green.power=StringToDouble(token, |
cristy | c1acd84 | 2011-05-19 23:05:47 +0000 | [diff] [blame] | 644 | (char **) NULL); |
| 645 | break; |
| 646 | } |
| 647 | case 2: |
| 648 | { |
cristy | dbdd0e3 | 2011-11-04 23:29:40 +0000 | [diff] [blame] | 649 | color_correction.blue.power=StringToDouble(token, |
cristy | c1acd84 | 2011-05-19 23:05:47 +0000 | [diff] [blame] | 650 | (char **) NULL); |
| 651 | break; |
| 652 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 653 | } |
| 654 | } |
| 655 | } |
| 656 | } |
| 657 | sat=GetXMLTreeChild(cc,"SATNode"); |
| 658 | if (sat != (XMLTreeInfo *) NULL) |
| 659 | { |
| 660 | XMLTreeInfo |
| 661 | *saturation; |
| 662 | |
| 663 | saturation=GetXMLTreeChild(sat,"Saturation"); |
| 664 | if (saturation != (XMLTreeInfo *) NULL) |
| 665 | { |
| 666 | content=GetXMLTreeContent(saturation); |
| 667 | p=(const char *) content; |
| 668 | GetMagickToken(p,&p,token); |
cristy | ca826b5 | 2012-01-18 18:50:46 +0000 | [diff] [blame] | 669 | color_correction.saturation=StringToDouble(token,(char **) NULL); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 670 | } |
| 671 | } |
| 672 | ccc=DestroyXMLTree(ccc); |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 673 | if( IfMagickTrue(image->debug) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 674 | { |
| 675 | (void) LogMagickEvent(TransformEvent,GetMagickModule(), |
| 676 | " Color Correction Collection:"); |
| 677 | (void) LogMagickEvent(TransformEvent,GetMagickModule(), |
cristy | e7f5109 | 2010-01-17 00:39:37 +0000 | [diff] [blame] | 678 | " color_correction.red.slope: %g",color_correction.red.slope); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 679 | (void) LogMagickEvent(TransformEvent,GetMagickModule(), |
cristy | e7f5109 | 2010-01-17 00:39:37 +0000 | [diff] [blame] | 680 | " color_correction.red.offset: %g",color_correction.red.offset); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 681 | (void) LogMagickEvent(TransformEvent,GetMagickModule(), |
cristy | e7f5109 | 2010-01-17 00:39:37 +0000 | [diff] [blame] | 682 | " color_correction.red.power: %g",color_correction.red.power); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 683 | (void) LogMagickEvent(TransformEvent,GetMagickModule(), |
cristy | e7f5109 | 2010-01-17 00:39:37 +0000 | [diff] [blame] | 684 | " color_correction.green.slope: %g",color_correction.green.slope); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 685 | (void) LogMagickEvent(TransformEvent,GetMagickModule(), |
cristy | e7f5109 | 2010-01-17 00:39:37 +0000 | [diff] [blame] | 686 | " color_correction.green.offset: %g",color_correction.green.offset); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 687 | (void) LogMagickEvent(TransformEvent,GetMagickModule(), |
cristy | e7f5109 | 2010-01-17 00:39:37 +0000 | [diff] [blame] | 688 | " color_correction.green.power: %g",color_correction.green.power); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 689 | (void) LogMagickEvent(TransformEvent,GetMagickModule(), |
cristy | e7f5109 | 2010-01-17 00:39:37 +0000 | [diff] [blame] | 690 | " color_correction.blue.slope: %g",color_correction.blue.slope); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 691 | (void) LogMagickEvent(TransformEvent,GetMagickModule(), |
cristy | e7f5109 | 2010-01-17 00:39:37 +0000 | [diff] [blame] | 692 | " color_correction.blue.offset: %g",color_correction.blue.offset); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 693 | (void) LogMagickEvent(TransformEvent,GetMagickModule(), |
cristy | e7f5109 | 2010-01-17 00:39:37 +0000 | [diff] [blame] | 694 | " color_correction.blue.power: %g",color_correction.blue.power); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 695 | (void) LogMagickEvent(TransformEvent,GetMagickModule(), |
cristy | e7f5109 | 2010-01-17 00:39:37 +0000 | [diff] [blame] | 696 | " color_correction.saturation: %g",color_correction.saturation); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 697 | } |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 698 | cdl_map=(PixelInfo *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*cdl_map)); |
| 699 | if (cdl_map == (PixelInfo *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 700 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
| 701 | image->filename); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 702 | for (i=0; i <= (ssize_t) MaxMap; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 703 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 704 | cdl_map[i].red=(double) ScaleMapToQuantum((double) |
cristy | da1f9c1 | 2011-10-02 21:39:49 +0000 | [diff] [blame] | 705 | (MaxMap*(pow(color_correction.red.slope*i/MaxMap+ |
| 706 | color_correction.red.offset,color_correction.red.power)))); |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 707 | cdl_map[i].green=(double) ScaleMapToQuantum((double) |
cristy | da1f9c1 | 2011-10-02 21:39:49 +0000 | [diff] [blame] | 708 | (MaxMap*(pow(color_correction.green.slope*i/MaxMap+ |
| 709 | color_correction.green.offset,color_correction.green.power)))); |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 710 | cdl_map[i].blue=(double) ScaleMapToQuantum((double) |
cristy | da1f9c1 | 2011-10-02 21:39:49 +0000 | [diff] [blame] | 711 | (MaxMap*(pow(color_correction.blue.slope*i/MaxMap+ |
| 712 | color_correction.blue.offset,color_correction.blue.power)))); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 713 | } |
| 714 | if (image->storage_class == PseudoClass) |
nicolas | e0aa3c3 | 2012-08-08 14:57:02 +0000 | [diff] [blame] | 715 | for (i=0; i < (ssize_t) image->colors; i++) |
nicolas | fd74ae3 | 2012-08-08 16:00:11 +0000 | [diff] [blame] | 716 | { |
| 717 | /* |
cristy | aeded78 | 2012-09-11 23:39:36 +0000 | [diff] [blame] | 718 | Apply transfer function to colormap. |
nicolas | fd74ae3 | 2012-08-08 16:00:11 +0000 | [diff] [blame] | 719 | */ |
| 720 | double |
cristy | aeded78 | 2012-09-11 23:39:36 +0000 | [diff] [blame] | 721 | luma; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 722 | |
cristy | a86a5cb | 2012-10-14 13:40:33 +0000 | [diff] [blame] | 723 | luma=0.21267f*image->colormap[i].red+0.71526*image->colormap[i].green+ |
| 724 | 0.07217f*image->colormap[i].blue; |
nicolas | fd74ae3 | 2012-08-08 16:00:11 +0000 | [diff] [blame] | 725 | image->colormap[i].red=luma+color_correction.saturation*cdl_map[ |
cristy | aeded78 | 2012-09-11 23:39:36 +0000 | [diff] [blame] | 726 | ScaleQuantumToMap(ClampToQuantum(image->colormap[i].red))].red-luma; |
nicolas | fd74ae3 | 2012-08-08 16:00:11 +0000 | [diff] [blame] | 727 | image->colormap[i].green=luma+color_correction.saturation*cdl_map[ |
| 728 | ScaleQuantumToMap(ClampToQuantum(image->colormap[i].green))].green-luma; |
| 729 | image->colormap[i].blue=luma+color_correction.saturation*cdl_map[ |
| 730 | ScaleQuantumToMap(ClampToQuantum(image->colormap[i].blue))].blue-luma; |
| 731 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 732 | /* |
| 733 | Apply transfer function to image. |
| 734 | */ |
| 735 | status=MagickTrue; |
| 736 | progress=0; |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 737 | image_view=AcquireAuthenticCacheView(image,exception); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 738 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 739 | #pragma omp parallel for schedule(static,4) shared(progress,status) \ |
cristy | 5e6b259 | 2012-12-19 14:08:11 +0000 | [diff] [blame] | 740 | magick_threads(image,image,image->rows,1) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 741 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 742 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 743 | { |
| 744 | double |
| 745 | luma; |
| 746 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 747 | register Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 748 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 749 | |
cristy | 8d4629b | 2010-08-30 17:59:46 +0000 | [diff] [blame] | 750 | register ssize_t |
| 751 | x; |
| 752 | |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 753 | if( IfMagickFalse(status) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 754 | continue; |
| 755 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 756 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 757 | { |
| 758 | status=MagickFalse; |
| 759 | continue; |
| 760 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 761 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 762 | { |
cristy | b711323 | 2013-02-15 00:35:19 +0000 | [diff] [blame] | 763 | luma=0.21267f*GetPixelRed(image,q)+0.71526*GetPixelGreen(image,q)+ |
| 764 | 0.07217f*GetPixelBlue(image,q); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 765 | SetPixelRed(image,ClampToQuantum(luma+color_correction.saturation* |
| 766 | (cdl_map[ScaleQuantumToMap(GetPixelRed(image,q))].red-luma)),q); |
| 767 | SetPixelGreen(image,ClampToQuantum(luma+color_correction.saturation* |
| 768 | (cdl_map[ScaleQuantumToMap(GetPixelGreen(image,q))].green-luma)),q); |
| 769 | SetPixelBlue(image,ClampToQuantum(luma+color_correction.saturation* |
| 770 | (cdl_map[ScaleQuantumToMap(GetPixelBlue(image,q))].blue-luma)),q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 771 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 772 | } |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 773 | if( IfMagickFalse(SyncCacheViewAuthenticPixels(image_view,exception)) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 774 | status=MagickFalse; |
| 775 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 776 | { |
| 777 | MagickBooleanType |
| 778 | proceed; |
| 779 | |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 780 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 564a569 | 2012-01-20 23:56:26 +0000 | [diff] [blame] | 781 | #pragma omp critical (MagickCore_ColorDecisionListImageChannel) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 782 | #endif |
| 783 | proceed=SetImageProgress(image,ColorDecisionListCorrectImageTag, |
| 784 | progress++,image->rows); |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 785 | if( IfMagickFalse(proceed) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 786 | status=MagickFalse; |
| 787 | } |
| 788 | } |
| 789 | image_view=DestroyCacheView(image_view); |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 790 | cdl_map=(PixelInfo *) RelinquishMagickMemory(cdl_map); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 791 | return(status); |
| 792 | } |
| 793 | |
| 794 | /* |
| 795 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 796 | % % |
| 797 | % % |
| 798 | % % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 799 | % C o n t r a s t I m a g e % |
| 800 | % % |
| 801 | % % |
| 802 | % % |
| 803 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 804 | % |
| 805 | % ContrastImage() enhances the intensity differences between the lighter and |
| 806 | % darker elements of the image. Set sharpen to a MagickTrue to increase the |
| 807 | % image contrast otherwise the contrast is reduced. |
| 808 | % |
| 809 | % The format of the ContrastImage method is: |
| 810 | % |
| 811 | % MagickBooleanType ContrastImage(Image *image, |
cristy | e23ec9d | 2011-08-16 18:15:40 +0000 | [diff] [blame] | 812 | % const MagickBooleanType sharpen,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 813 | % |
| 814 | % A description of each parameter follows: |
| 815 | % |
| 816 | % o image: the image. |
| 817 | % |
| 818 | % o sharpen: Increase or decrease image contrast. |
| 819 | % |
cristy | e23ec9d | 2011-08-16 18:15:40 +0000 | [diff] [blame] | 820 | % o exception: return any errors or warnings in this structure. |
| 821 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 822 | */ |
| 823 | |
cristy | 3094b7f | 2011-10-01 23:18:02 +0000 | [diff] [blame] | 824 | static void Contrast(const int sign,double *red,double *green,double *blue) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 825 | { |
| 826 | double |
| 827 | brightness, |
| 828 | hue, |
| 829 | saturation; |
| 830 | |
| 831 | /* |
| 832 | Enhance contrast: dark color become darker, light color become lighter. |
| 833 | */ |
cristy | 3094b7f | 2011-10-01 23:18:02 +0000 | [diff] [blame] | 834 | assert(red != (double *) NULL); |
| 835 | assert(green != (double *) NULL); |
| 836 | assert(blue != (double *) NULL); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 837 | hue=0.0; |
| 838 | saturation=0.0; |
| 839 | brightness=0.0; |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 840 | ConvertRGBToHSB(*red,*green,*blue,&hue,&saturation,&brightness); |
cristy | 31ac0f0 | 2011-03-12 02:04:47 +0000 | [diff] [blame] | 841 | brightness+=0.5*sign*(0.5*(sin((double) (MagickPI*(brightness-0.5)))+1.0)- |
cristy | 4205a3c | 2010-09-12 20:19:59 +0000 | [diff] [blame] | 842 | brightness); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 843 | if (brightness > 1.0) |
| 844 | brightness=1.0; |
| 845 | else |
| 846 | if (brightness < 0.0) |
| 847 | brightness=0.0; |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 848 | ConvertHSBToRGB(hue,saturation,brightness,red,green,blue); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | MagickExport MagickBooleanType ContrastImage(Image *image, |
cristy | e23ec9d | 2011-08-16 18:15:40 +0000 | [diff] [blame] | 852 | const MagickBooleanType sharpen,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 853 | { |
| 854 | #define ContrastImageTag "Contrast/Image" |
| 855 | |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 856 | CacheView |
| 857 | *image_view; |
| 858 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 859 | int |
| 860 | sign; |
| 861 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 862 | MagickBooleanType |
| 863 | status; |
| 864 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 865 | MagickOffsetType |
| 866 | progress; |
| 867 | |
| 868 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 869 | i; |
| 870 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 871 | ssize_t |
| 872 | y; |
| 873 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 874 | assert(image != (Image *) NULL); |
| 875 | assert(image->signature == MagickSignature); |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 876 | if( IfMagickTrue(image->debug) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 877 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 878 | sign=IfMagickTrue(sharpen) ? 1 : -1; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 879 | if (image->storage_class == PseudoClass) |
| 880 | { |
| 881 | /* |
| 882 | Contrast enhance colormap. |
| 883 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 884 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | bdf42e5 | 2012-10-10 23:58:20 +0000 | [diff] [blame] | 885 | { |
| 886 | double |
| 887 | blue, |
| 888 | green, |
| 889 | red; |
| 890 | |
| 891 | Contrast(sign,&red,&green,&blue); |
| 892 | image->colormap[i].red=(MagickRealType) red; |
| 893 | image->colormap[i].red=(MagickRealType) red; |
| 894 | image->colormap[i].red=(MagickRealType) red; |
| 895 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 896 | } |
| 897 | /* |
| 898 | Contrast enhance image. |
| 899 | */ |
| 900 | status=MagickTrue; |
| 901 | progress=0; |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 902 | image_view=AcquireAuthenticCacheView(image,exception); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 903 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 904 | #pragma omp parallel for schedule(static,4) shared(progress,status) \ |
cristy | 5e6b259 | 2012-12-19 14:08:11 +0000 | [diff] [blame] | 905 | magick_threads(image,image,image->rows,1) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 906 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 907 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 908 | { |
cristy | 3094b7f | 2011-10-01 23:18:02 +0000 | [diff] [blame] | 909 | double |
cristy | 5afeab8 | 2011-04-30 01:30:09 +0000 | [diff] [blame] | 910 | blue, |
| 911 | green, |
| 912 | red; |
| 913 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 914 | register Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 915 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 916 | |
cristy | 8d4629b | 2010-08-30 17:59:46 +0000 | [diff] [blame] | 917 | register ssize_t |
| 918 | x; |
| 919 | |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 920 | if( IfMagickFalse(status) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 921 | continue; |
| 922 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 923 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 924 | { |
| 925 | status=MagickFalse; |
| 926 | continue; |
| 927 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 928 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 929 | { |
cristy | da1f9c1 | 2011-10-02 21:39:49 +0000 | [diff] [blame] | 930 | red=(double) GetPixelRed(image,q); |
| 931 | green=(double) GetPixelGreen(image,q); |
| 932 | blue=(double) GetPixelBlue(image,q); |
cristy | 5afeab8 | 2011-04-30 01:30:09 +0000 | [diff] [blame] | 933 | Contrast(sign,&red,&green,&blue); |
cristy | da1f9c1 | 2011-10-02 21:39:49 +0000 | [diff] [blame] | 934 | SetPixelRed(image,ClampToQuantum(red),q); |
| 935 | SetPixelGreen(image,ClampToQuantum(green),q); |
| 936 | SetPixelBlue(image,ClampToQuantum(blue),q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 937 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 938 | } |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 939 | if( IfMagickFalse(SyncCacheViewAuthenticPixels(image_view,exception)) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 940 | status=MagickFalse; |
| 941 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 942 | { |
| 943 | MagickBooleanType |
| 944 | proceed; |
| 945 | |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 946 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 564a569 | 2012-01-20 23:56:26 +0000 | [diff] [blame] | 947 | #pragma omp critical (MagickCore_ContrastImage) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 948 | #endif |
| 949 | proceed=SetImageProgress(image,ContrastImageTag,progress++,image->rows); |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 950 | if( IfMagickFalse(proceed) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 951 | status=MagickFalse; |
| 952 | } |
| 953 | } |
| 954 | image_view=DestroyCacheView(image_view); |
| 955 | return(status); |
| 956 | } |
| 957 | |
| 958 | /* |
| 959 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 960 | % % |
| 961 | % % |
| 962 | % % |
| 963 | % C o n t r a s t S t r e t c h I m a g e % |
| 964 | % % |
| 965 | % % |
| 966 | % % |
| 967 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 968 | % |
cristy | f161178 | 2011-08-01 15:39:13 +0000 | [diff] [blame] | 969 | % ContrastStretchImage() is a simple image enhancement technique that attempts |
anthony | e5b3965 | 2012-04-21 05:37:29 +0000 | [diff] [blame] | 970 | % to improve the contrast in an image by 'stretching' the range of intensity |
cristy | f161178 | 2011-08-01 15:39:13 +0000 | [diff] [blame] | 971 | % values it contains to span a desired range of values. It differs from the |
| 972 | % more sophisticated histogram equalization in that it can only apply a |
| 973 | % linear scaling function to the image pixel values. As a result the |
anthony | e5b3965 | 2012-04-21 05:37:29 +0000 | [diff] [blame] | 974 | % 'enhancement' is less harsh. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 975 | % |
| 976 | % The format of the ContrastStretchImage method is: |
| 977 | % |
| 978 | % MagickBooleanType ContrastStretchImage(Image *image, |
cristy | e23ec9d | 2011-08-16 18:15:40 +0000 | [diff] [blame] | 979 | % const char *levels,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 980 | % |
| 981 | % A description of each parameter follows: |
| 982 | % |
| 983 | % o image: the image. |
| 984 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 985 | % o black_point: the black point. |
| 986 | % |
| 987 | % o white_point: the white point. |
| 988 | % |
| 989 | % o levels: Specify the levels where the black and white points have the |
| 990 | % range of 0 to number-of-pixels (e.g. 1%, 10x90%, etc.). |
| 991 | % |
cristy | e23ec9d | 2011-08-16 18:15:40 +0000 | [diff] [blame] | 992 | % o exception: return any errors or warnings in this structure. |
| 993 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 994 | */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 995 | MagickExport MagickBooleanType ContrastStretchImage(Image *image, |
cristy | e23ec9d | 2011-08-16 18:15:40 +0000 | [diff] [blame] | 996 | const double black_point,const double white_point,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 997 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 998 | #define MaxRange(color) ((double) ScaleQuantumToMap((Quantum) (color))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 999 | #define ContrastStretchImageTag "ContrastStretch/Image" |
| 1000 | |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 1001 | CacheView |
| 1002 | *image_view; |
| 1003 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1004 | MagickBooleanType |
| 1005 | status; |
| 1006 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1007 | MagickOffsetType |
| 1008 | progress; |
| 1009 | |
cristy | f45fec7 | 2011-08-23 16:02:32 +0000 | [diff] [blame] | 1010 | double |
| 1011 | *black, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1012 | *histogram, |
| 1013 | *stretch_map, |
cristy | f45fec7 | 2011-08-23 16:02:32 +0000 | [diff] [blame] | 1014 | *white; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1015 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1016 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1017 | i; |
| 1018 | |
cristy | 564a569 | 2012-01-20 23:56:26 +0000 | [diff] [blame] | 1019 | size_t |
| 1020 | number_channels; |
| 1021 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1022 | ssize_t |
| 1023 | y; |
| 1024 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1025 | /* |
| 1026 | Allocate histogram and stretch map. |
| 1027 | */ |
| 1028 | assert(image != (Image *) NULL); |
| 1029 | assert(image->signature == MagickSignature); |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 1030 | if( IfMagickTrue(image->debug) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1031 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | f45fec7 | 2011-08-23 16:02:32 +0000 | [diff] [blame] | 1032 | black=(double *) AcquireQuantumMemory(GetPixelChannels(image),sizeof(*black)); |
| 1033 | white=(double *) AcquireQuantumMemory(GetPixelChannels(image),sizeof(*white)); |
cristy | 564a569 | 2012-01-20 23:56:26 +0000 | [diff] [blame] | 1034 | histogram=(double *) AcquireQuantumMemory(MaxMap+1UL,GetPixelChannels(image)* |
| 1035 | sizeof(*histogram)); |
cristy | f45fec7 | 2011-08-23 16:02:32 +0000 | [diff] [blame] | 1036 | stretch_map=(double *) AcquireQuantumMemory(MaxMap+1UL, |
cristy | 465571b | 2011-08-21 20:43:15 +0000 | [diff] [blame] | 1037 | GetPixelChannels(image)*sizeof(*stretch_map)); |
cristy | f45fec7 | 2011-08-23 16:02:32 +0000 | [diff] [blame] | 1038 | if ((black == (double *) NULL) || (white == (double *) NULL) || |
| 1039 | (histogram == (double *) NULL) || (stretch_map == (double *) NULL)) |
cristy | 465571b | 2011-08-21 20:43:15 +0000 | [diff] [blame] | 1040 | { |
cristy | f45fec7 | 2011-08-23 16:02:32 +0000 | [diff] [blame] | 1041 | if (stretch_map != (double *) NULL) |
| 1042 | stretch_map=(double *) RelinquishMagickMemory(stretch_map); |
| 1043 | if (histogram != (double *) NULL) |
| 1044 | histogram=(double *) RelinquishMagickMemory(histogram); |
| 1045 | if (white != (double *) NULL) |
| 1046 | white=(double *) RelinquishMagickMemory(white); |
| 1047 | if (black != (double *) NULL) |
| 1048 | black=(double *) RelinquishMagickMemory(black); |
cristy | 465571b | 2011-08-21 20:43:15 +0000 | [diff] [blame] | 1049 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
| 1050 | image->filename); |
| 1051 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1052 | /* |
| 1053 | Form histogram. |
| 1054 | */ |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 1055 | if( IfMagickTrue(IsImageGray(image,exception)) ) |
cristy | 5836c74 | 2013-03-27 21:10:56 +0000 | [diff] [blame] | 1056 | (void) SetImageColorspace(image,GRAYColorspace,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1057 | status=MagickTrue; |
cristy | 465571b | 2011-08-21 20:43:15 +0000 | [diff] [blame] | 1058 | (void) ResetMagickMemory(histogram,0,(MaxMap+1)*GetPixelChannels(image)* |
| 1059 | sizeof(*histogram)); |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 1060 | image_view=AcquireVirtualCacheView(image,exception); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1061 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1062 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1063 | register const Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1064 | *restrict p; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1065 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1066 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1067 | x; |
| 1068 | |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 1069 | if( IfMagickFalse(status) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1070 | continue; |
| 1071 | p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1072 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1073 | { |
| 1074 | status=MagickFalse; |
| 1075 | continue; |
| 1076 | } |
cristy | 43547a5 | 2011-08-09 13:07:05 +0000 | [diff] [blame] | 1077 | for (x=0; x < (ssize_t) image->columns; x++) |
| 1078 | { |
cristy | a64f4b9 | 2012-07-11 23:59:00 +0000 | [diff] [blame] | 1079 | double |
| 1080 | pixel; |
| 1081 | |
cristy | 465571b | 2011-08-21 20:43:15 +0000 | [diff] [blame] | 1082 | register ssize_t |
| 1083 | i; |
| 1084 | |
cristy | f13c594 | 2012-08-08 23:50:11 +0000 | [diff] [blame] | 1085 | pixel=GetPixelIntensity(image,p); |
cristy | 465571b | 2011-08-21 20:43:15 +0000 | [diff] [blame] | 1086 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
cristy | a64f4b9 | 2012-07-11 23:59:00 +0000 | [diff] [blame] | 1087 | { |
| 1088 | if (image->channel_mask != DefaultChannels) |
cristy | 1fc77e8 | 2012-08-08 15:43:42 +0000 | [diff] [blame] | 1089 | pixel=(double) p[i]; |
| 1090 | histogram[GetPixelChannels(image)*ScaleQuantumToMap( |
| 1091 | ClampToQuantum(pixel))+i]++; |
cristy | a64f4b9 | 2012-07-11 23:59:00 +0000 | [diff] [blame] | 1092 | } |
cristy | 43547a5 | 2011-08-09 13:07:05 +0000 | [diff] [blame] | 1093 | p+=GetPixelChannels(image); |
| 1094 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1095 | } |
cristy | db07095 | 2012-04-20 14:33:00 +0000 | [diff] [blame] | 1096 | image_view=DestroyCacheView(image_view); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1097 | /* |
| 1098 | Find the histogram boundaries by locating the black/white levels. |
| 1099 | */ |
cristy | 564a569 | 2012-01-20 23:56:26 +0000 | [diff] [blame] | 1100 | number_channels=GetPixelChannels(image); |
cristy | c94ba6f | 2012-01-29 23:19:58 +0000 | [diff] [blame] | 1101 | for (i=0; i < (ssize_t) number_channels; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1102 | { |
cristy | 465571b | 2011-08-21 20:43:15 +0000 | [diff] [blame] | 1103 | double |
| 1104 | intensity; |
| 1105 | |
| 1106 | register ssize_t |
| 1107 | j; |
| 1108 | |
| 1109 | black[i]=0.0; |
| 1110 | white[i]=MaxRange(QuantumRange); |
| 1111 | intensity=0.0; |
| 1112 | for (j=0; j <= (ssize_t) MaxMap; j++) |
| 1113 | { |
| 1114 | intensity+=histogram[GetPixelChannels(image)*j+i]; |
| 1115 | if (intensity > black_point) |
| 1116 | break; |
| 1117 | } |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1118 | black[i]=(double) j; |
cristy | 465571b | 2011-08-21 20:43:15 +0000 | [diff] [blame] | 1119 | intensity=0.0; |
| 1120 | for (j=(ssize_t) MaxMap; j != 0; j--) |
| 1121 | { |
| 1122 | intensity+=histogram[GetPixelChannels(image)*j+i]; |
| 1123 | if (intensity > ((double) image->columns*image->rows-white_point)) |
| 1124 | break; |
| 1125 | } |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1126 | white[i]=(double) j; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1127 | } |
cristy | 465571b | 2011-08-21 20:43:15 +0000 | [diff] [blame] | 1128 | histogram=(double *) RelinquishMagickMemory(histogram); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1129 | /* |
cristy | 465571b | 2011-08-21 20:43:15 +0000 | [diff] [blame] | 1130 | Stretch the histogram to create the stretched image mapping. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1131 | */ |
cristy | 465571b | 2011-08-21 20:43:15 +0000 | [diff] [blame] | 1132 | (void) ResetMagickMemory(stretch_map,0,(MaxMap+1)*GetPixelChannels(image)* |
| 1133 | sizeof(*stretch_map)); |
cristy | 564a569 | 2012-01-20 23:56:26 +0000 | [diff] [blame] | 1134 | number_channels=GetPixelChannels(image); |
cristy | 564a569 | 2012-01-20 23:56:26 +0000 | [diff] [blame] | 1135 | for (i=0; i < (ssize_t) number_channels; i++) |
cristy | 465571b | 2011-08-21 20:43:15 +0000 | [diff] [blame] | 1136 | { |
| 1137 | register ssize_t |
| 1138 | j; |
| 1139 | |
| 1140 | for (j=0; j <= (ssize_t) MaxMap; j++) |
| 1141 | { |
| 1142 | if (j < (ssize_t) black[i]) |
| 1143 | stretch_map[GetPixelChannels(image)*j+i]=0.0; |
| 1144 | else |
| 1145 | if (j > (ssize_t) white[i]) |
cristy | b711323 | 2013-02-15 00:35:19 +0000 | [diff] [blame] | 1146 | stretch_map[GetPixelChannels(image)*j+i]=(double) QuantumRange; |
cristy | 465571b | 2011-08-21 20:43:15 +0000 | [diff] [blame] | 1147 | else |
| 1148 | if (black[i] != white[i]) |
cristy | b711323 | 2013-02-15 00:35:19 +0000 | [diff] [blame] | 1149 | stretch_map[GetPixelChannels(image)*j+i]=(double) ScaleMapToQuantum( |
| 1150 | (double) (MaxMap*(j-black[i])/(white[i]-black[i]))); |
cristy | 465571b | 2011-08-21 20:43:15 +0000 | [diff] [blame] | 1151 | } |
| 1152 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1153 | if (image->storage_class == PseudoClass) |
| 1154 | { |
cristy | 465571b | 2011-08-21 20:43:15 +0000 | [diff] [blame] | 1155 | register ssize_t |
| 1156 | j; |
| 1157 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1158 | /* |
cristy | 465571b | 2011-08-21 20:43:15 +0000 | [diff] [blame] | 1159 | Stretch-contrast colormap. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1160 | */ |
cristy | 465571b | 2011-08-21 20:43:15 +0000 | [diff] [blame] | 1161 | for (j=0; j < (ssize_t) image->colors; j++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1162 | { |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1163 | if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1164 | { |
cristy | cf1296e | 2012-08-26 23:40:49 +0000 | [diff] [blame] | 1165 | i=GetPixelChannelChannel(image,RedPixelChannel); |
cristy | 465571b | 2011-08-21 20:43:15 +0000 | [diff] [blame] | 1166 | if (black[i] != white[i]) |
cristy | da1f9c1 | 2011-10-02 21:39:49 +0000 | [diff] [blame] | 1167 | image->colormap[j].red=stretch_map[GetPixelChannels(image)* |
| 1168 | ScaleQuantumToMap(ClampToQuantum(image->colormap[j].red))]+i; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1169 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1170 | if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1171 | { |
cristy | cf1296e | 2012-08-26 23:40:49 +0000 | [diff] [blame] | 1172 | i=GetPixelChannelChannel(image,GreenPixelChannel); |
cristy | 465571b | 2011-08-21 20:43:15 +0000 | [diff] [blame] | 1173 | if (black[i] != white[i]) |
cristy | da1f9c1 | 2011-10-02 21:39:49 +0000 | [diff] [blame] | 1174 | image->colormap[j].green=stretch_map[GetPixelChannels(image)* |
| 1175 | ScaleQuantumToMap(ClampToQuantum(image->colormap[j].green))]+i; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1176 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1177 | if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1178 | { |
cristy | cf1296e | 2012-08-26 23:40:49 +0000 | [diff] [blame] | 1179 | i=GetPixelChannelChannel(image,BluePixelChannel); |
cristy | 465571b | 2011-08-21 20:43:15 +0000 | [diff] [blame] | 1180 | if (black[i] != white[i]) |
cristy | da1f9c1 | 2011-10-02 21:39:49 +0000 | [diff] [blame] | 1181 | image->colormap[j].blue=stretch_map[GetPixelChannels(image)* |
| 1182 | ScaleQuantumToMap(ClampToQuantum(image->colormap[j].blue))]+i; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1183 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1184 | if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1185 | { |
cristy | cf1296e | 2012-08-26 23:40:49 +0000 | [diff] [blame] | 1186 | i=GetPixelChannelChannel(image,AlphaPixelChannel); |
cristy | 465571b | 2011-08-21 20:43:15 +0000 | [diff] [blame] | 1187 | if (black[i] != white[i]) |
cristy | da1f9c1 | 2011-10-02 21:39:49 +0000 | [diff] [blame] | 1188 | image->colormap[j].alpha=stretch_map[GetPixelChannels(image)* |
| 1189 | ScaleQuantumToMap(ClampToQuantum(image->colormap[j].alpha))]+i; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1190 | } |
| 1191 | } |
| 1192 | } |
| 1193 | /* |
cristy | 465571b | 2011-08-21 20:43:15 +0000 | [diff] [blame] | 1194 | Stretch-contrast image. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1195 | */ |
| 1196 | status=MagickTrue; |
| 1197 | progress=0; |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 1198 | image_view=AcquireAuthenticCacheView(image,exception); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 1199 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 1200 | #pragma omp parallel for schedule(static,4) shared(progress,status) \ |
cristy | 5e6b259 | 2012-12-19 14:08:11 +0000 | [diff] [blame] | 1201 | magick_threads(image,image,image->rows,1) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1202 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1203 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1204 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1205 | register Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1206 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1207 | |
cristy | 8d4629b | 2010-08-30 17:59:46 +0000 | [diff] [blame] | 1208 | register ssize_t |
| 1209 | x; |
| 1210 | |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 1211 | if( IfMagickFalse(status) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1212 | continue; |
| 1213 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 1214 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1215 | { |
| 1216 | status=MagickFalse; |
| 1217 | continue; |
| 1218 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1219 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1220 | { |
cristy | 465571b | 2011-08-21 20:43:15 +0000 | [diff] [blame] | 1221 | register ssize_t |
| 1222 | i; |
| 1223 | |
cristy | 883fde1 | 2013-04-08 00:50:13 +0000 | [diff] [blame] | 1224 | if (GetPixelReadMask(image,q) == 0) |
cristy | 10a6c61 | 2012-01-29 21:41:05 +0000 | [diff] [blame] | 1225 | { |
| 1226 | q+=GetPixelChannels(image); |
| 1227 | continue; |
| 1228 | } |
cristy | 465571b | 2011-08-21 20:43:15 +0000 | [diff] [blame] | 1229 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
| 1230 | { |
cristy | 5a23c55 | 2013-02-13 14:34:28 +0000 | [diff] [blame] | 1231 | PixelChannel channel=GetPixelChannelChannel(image,i); |
| 1232 | PixelTrait traits=GetPixelChannelTraits(image,channel); |
cristy | d09f880 | 2012-02-04 16:44:10 +0000 | [diff] [blame] | 1233 | if (((traits & UpdatePixelTrait) == 0) || (black[i] == white[i])) |
| 1234 | continue; |
| 1235 | q[i]=ClampToQuantum(stretch_map[GetPixelChannels(image)* |
| 1236 | ScaleQuantumToMap(q[i])+i]); |
cristy | 465571b | 2011-08-21 20:43:15 +0000 | [diff] [blame] | 1237 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1238 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1239 | } |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 1240 | if( IfMagickFalse(SyncCacheViewAuthenticPixels(image_view,exception)) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1241 | status=MagickFalse; |
| 1242 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 1243 | { |
| 1244 | MagickBooleanType |
| 1245 | proceed; |
| 1246 | |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 1247 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 564a569 | 2012-01-20 23:56:26 +0000 | [diff] [blame] | 1248 | #pragma omp critical (MagickCore_ContrastStretchImage) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1249 | #endif |
| 1250 | proceed=SetImageProgress(image,ContrastStretchImageTag,progress++, |
| 1251 | image->rows); |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 1252 | if( IfMagickFalse(proceed) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1253 | status=MagickFalse; |
| 1254 | } |
| 1255 | } |
| 1256 | image_view=DestroyCacheView(image_view); |
cristy | f45fec7 | 2011-08-23 16:02:32 +0000 | [diff] [blame] | 1257 | stretch_map=(double *) RelinquishMagickMemory(stretch_map); |
| 1258 | white=(double *) RelinquishMagickMemory(white); |
| 1259 | black=(double *) RelinquishMagickMemory(black); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1260 | return(status); |
| 1261 | } |
| 1262 | |
| 1263 | /* |
| 1264 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1265 | % % |
| 1266 | % % |
| 1267 | % % |
| 1268 | % E n h a n c e I m a g e % |
| 1269 | % % |
| 1270 | % % |
| 1271 | % % |
| 1272 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1273 | % |
| 1274 | % EnhanceImage() applies a digital filter that improves the quality of a |
| 1275 | % noisy image. |
| 1276 | % |
| 1277 | % The format of the EnhanceImage method is: |
| 1278 | % |
| 1279 | % Image *EnhanceImage(const Image *image,ExceptionInfo *exception) |
| 1280 | % |
| 1281 | % A description of each parameter follows: |
| 1282 | % |
| 1283 | % o image: the image. |
| 1284 | % |
| 1285 | % o exception: return any errors or warnings in this structure. |
| 1286 | % |
| 1287 | */ |
| 1288 | MagickExport Image *EnhanceImage(const Image *image,ExceptionInfo *exception) |
| 1289 | { |
cristy | 6d8c3d7 | 2011-08-22 01:20:01 +0000 | [diff] [blame] | 1290 | #define EnhancePixel(weight) \ |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1291 | mean=((double) r[i]+GetPixelChannel(enhance_image,channel,q))/2.0; \ |
cristy | 76c670b | 2012-10-06 21:30:50 +0000 | [diff] [blame] | 1292 | distance=(double) r[i]-(double) GetPixelChannel(enhance_image,channel,q); \ |
| 1293 | distance_squared=QuantumScale*(2.0*((double) QuantumRange+1.0)+mean)* \ |
| 1294 | distance*distance; \ |
| 1295 | if (distance_squared < ((double) QuantumRange*(double) QuantumRange/25.0f)) \ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1296 | { \ |
cristy | 6d8c3d7 | 2011-08-22 01:20:01 +0000 | [diff] [blame] | 1297 | aggregate+=(weight)*r[i]; \ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1298 | total_weight+=(weight); \ |
| 1299 | } \ |
cristy | 6d8c3d7 | 2011-08-22 01:20:01 +0000 | [diff] [blame] | 1300 | r+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1301 | #define EnhanceImageTag "Enhance/Image" |
| 1302 | |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 1303 | CacheView |
| 1304 | *enhance_view, |
| 1305 | *image_view; |
| 1306 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1307 | Image |
| 1308 | *enhance_image; |
| 1309 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1310 | MagickBooleanType |
| 1311 | status; |
| 1312 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1313 | MagickOffsetType |
| 1314 | progress; |
| 1315 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1316 | ssize_t |
| 1317 | y; |
| 1318 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1319 | /* |
| 1320 | Initialize enhanced image attributes. |
| 1321 | */ |
| 1322 | assert(image != (const Image *) NULL); |
| 1323 | assert(image->signature == MagickSignature); |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 1324 | if( IfMagickTrue(image->debug) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1325 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 1326 | assert(exception != (ExceptionInfo *) NULL); |
| 1327 | assert(exception->signature == MagickSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1328 | enhance_image=CloneImage(image,image->columns,image->rows,MagickTrue, |
| 1329 | exception); |
| 1330 | if (enhance_image == (Image *) NULL) |
| 1331 | return((Image *) NULL); |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 1332 | if( IfMagickFalse(SetImageStorageClass(enhance_image,DirectClass,exception)) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1333 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1334 | enhance_image=DestroyImage(enhance_image); |
| 1335 | return((Image *) NULL); |
| 1336 | } |
| 1337 | /* |
| 1338 | Enhance image. |
| 1339 | */ |
| 1340 | status=MagickTrue; |
| 1341 | progress=0; |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 1342 | image_view=AcquireVirtualCacheView(image,exception); |
| 1343 | enhance_view=AcquireAuthenticCacheView(enhance_image,exception); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 1344 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 1345 | #pragma omp parallel for schedule(static,4) shared(progress,status) \ |
cristy | 5e6b259 | 2012-12-19 14:08:11 +0000 | [diff] [blame] | 1346 | magick_threads(image,enhance_image,image->rows,1) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1347 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1348 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1349 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1350 | register const Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1351 | *restrict p; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1352 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1353 | register Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1354 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1355 | |
cristy | 8d4629b | 2010-08-30 17:59:46 +0000 | [diff] [blame] | 1356 | register ssize_t |
| 1357 | x; |
| 1358 | |
cristy | 6d8c3d7 | 2011-08-22 01:20:01 +0000 | [diff] [blame] | 1359 | ssize_t |
| 1360 | center; |
| 1361 | |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 1362 | if( IfMagickFalse(status) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1363 | continue; |
| 1364 | p=GetCacheViewVirtualPixels(image_view,-2,y-2,image->columns+4,5,exception); |
| 1365 | q=QueueCacheViewAuthenticPixels(enhance_view,0,y,enhance_image->columns,1, |
| 1366 | exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1367 | if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1368 | { |
| 1369 | status=MagickFalse; |
| 1370 | continue; |
| 1371 | } |
cristy | f45fec7 | 2011-08-23 16:02:32 +0000 | [diff] [blame] | 1372 | center=(ssize_t) GetPixelChannels(image)*(2*(image->columns+4)+2); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1373 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1374 | { |
cristy | 6d8c3d7 | 2011-08-22 01:20:01 +0000 | [diff] [blame] | 1375 | register ssize_t |
| 1376 | i; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1377 | |
cristy | 883fde1 | 2013-04-08 00:50:13 +0000 | [diff] [blame] | 1378 | if (GetPixelReadMask(image,p) == 0) |
cristy | 10a6c61 | 2012-01-29 21:41:05 +0000 | [diff] [blame] | 1379 | { |
| 1380 | p+=GetPixelChannels(image); |
| 1381 | q+=GetPixelChannels(enhance_image); |
| 1382 | continue; |
| 1383 | } |
cristy | 6d8c3d7 | 2011-08-22 01:20:01 +0000 | [diff] [blame] | 1384 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
| 1385 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1386 | double |
cristy | 6d8c3d7 | 2011-08-22 01:20:01 +0000 | [diff] [blame] | 1387 | aggregate, |
| 1388 | distance, |
| 1389 | distance_squared, |
| 1390 | mean, |
| 1391 | total_weight; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1392 | |
cristy | 6d8c3d7 | 2011-08-22 01:20:01 +0000 | [diff] [blame] | 1393 | register const Quantum |
| 1394 | *restrict r; |
| 1395 | |
cristy | 5a23c55 | 2013-02-13 14:34:28 +0000 | [diff] [blame] | 1396 | PixelChannel channel=GetPixelChannelChannel(image,i); |
| 1397 | PixelTrait traits=GetPixelChannelTraits(image,channel); |
| 1398 | PixelTrait enhance_traits=GetPixelChannelTraits(enhance_image,channel); |
cristy | 010d7d1 | 2011-08-31 01:02:48 +0000 | [diff] [blame] | 1399 | if ((traits == UndefinedPixelTrait) || |
| 1400 | (enhance_traits == UndefinedPixelTrait)) |
cristy | 6d8c3d7 | 2011-08-22 01:20:01 +0000 | [diff] [blame] | 1401 | continue; |
cristy | 0beccfa | 2011-09-25 20:47:53 +0000 | [diff] [blame] | 1402 | SetPixelChannel(enhance_image,channel,p[center+i],q); |
cristy | 6d8c3d7 | 2011-08-22 01:20:01 +0000 | [diff] [blame] | 1403 | if ((enhance_traits & CopyPixelTrait) != 0) |
| 1404 | continue; |
| 1405 | /* |
| 1406 | Compute weighted average of target pixel color components. |
| 1407 | */ |
| 1408 | aggregate=0.0; |
| 1409 | total_weight=0.0; |
cristy | f45fec7 | 2011-08-23 16:02:32 +0000 | [diff] [blame] | 1410 | r=p; |
cristy | 6d8c3d7 | 2011-08-22 01:20:01 +0000 | [diff] [blame] | 1411 | EnhancePixel(5.0); EnhancePixel(8.0); EnhancePixel(10.0); |
| 1412 | EnhancePixel(8.0); EnhancePixel(5.0); |
| 1413 | r=p+1*GetPixelChannels(image)*(image->columns+4); |
| 1414 | EnhancePixel(8.0); EnhancePixel(20.0); EnhancePixel(40.0); |
| 1415 | EnhancePixel(20.0); EnhancePixel(8.0); |
| 1416 | r=p+2*GetPixelChannels(image)*(image->columns+4); |
| 1417 | EnhancePixel(10.0); EnhancePixel(40.0); EnhancePixel(80.0); |
| 1418 | EnhancePixel(40.0); EnhancePixel(10.0); |
| 1419 | r=p+3*GetPixelChannels(image)*(image->columns+4); |
| 1420 | EnhancePixel(8.0); EnhancePixel(20.0); EnhancePixel(40.0); |
| 1421 | EnhancePixel(20.0); EnhancePixel(8.0); |
| 1422 | r=p+4*GetPixelChannels(image)*(image->columns+4); |
| 1423 | EnhancePixel(5.0); EnhancePixel(8.0); EnhancePixel(10.0); |
| 1424 | EnhancePixel(8.0); EnhancePixel(5.0); |
cristy | 0beccfa | 2011-09-25 20:47:53 +0000 | [diff] [blame] | 1425 | SetPixelChannel(enhance_image,channel,ClampToQuantum(aggregate/ |
| 1426 | total_weight),q); |
cristy | 6d8c3d7 | 2011-08-22 01:20:01 +0000 | [diff] [blame] | 1427 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1428 | p+=GetPixelChannels(image); |
| 1429 | q+=GetPixelChannels(enhance_image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1430 | } |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 1431 | if( IfMagickFalse(SyncCacheViewAuthenticPixels(enhance_view,exception)) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1432 | status=MagickFalse; |
| 1433 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 1434 | { |
| 1435 | MagickBooleanType |
| 1436 | proceed; |
| 1437 | |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 1438 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 564a569 | 2012-01-20 23:56:26 +0000 | [diff] [blame] | 1439 | #pragma omp critical (MagickCore_EnhanceImage) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1440 | #endif |
| 1441 | proceed=SetImageProgress(image,EnhanceImageTag,progress++,image->rows); |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 1442 | if( IfMagickFalse(proceed) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1443 | status=MagickFalse; |
| 1444 | } |
| 1445 | } |
| 1446 | enhance_view=DestroyCacheView(enhance_view); |
| 1447 | image_view=DestroyCacheView(image_view); |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 1448 | if( IfMagickFalse(status) ) |
cristy | 1c2f48d | 2012-12-14 01:20:55 +0000 | [diff] [blame] | 1449 | enhance_image=DestroyImage(enhance_image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1450 | return(enhance_image); |
| 1451 | } |
| 1452 | |
| 1453 | /* |
| 1454 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1455 | % % |
| 1456 | % % |
| 1457 | % % |
| 1458 | % E q u a l i z e I m a g e % |
| 1459 | % % |
| 1460 | % % |
| 1461 | % % |
| 1462 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1463 | % |
| 1464 | % EqualizeImage() applies a histogram equalization to the image. |
| 1465 | % |
| 1466 | % The format of the EqualizeImage method is: |
| 1467 | % |
cristy | 6d8c3d7 | 2011-08-22 01:20:01 +0000 | [diff] [blame] | 1468 | % MagickBooleanType EqualizeImage(Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1469 | % |
| 1470 | % A description of each parameter follows: |
| 1471 | % |
| 1472 | % o image: the image. |
| 1473 | % |
cristy | 6d8c3d7 | 2011-08-22 01:20:01 +0000 | [diff] [blame] | 1474 | % o exception: return any errors or warnings in this structure. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1475 | % |
| 1476 | */ |
cristy | 6d8c3d7 | 2011-08-22 01:20:01 +0000 | [diff] [blame] | 1477 | MagickExport MagickBooleanType EqualizeImage(Image *image, |
| 1478 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1479 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1480 | #define EqualizeImageTag "Equalize/Image" |
| 1481 | |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 1482 | CacheView |
| 1483 | *image_view; |
| 1484 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1485 | MagickBooleanType |
| 1486 | status; |
| 1487 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1488 | MagickOffsetType |
| 1489 | progress; |
| 1490 | |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1491 | double |
cristy | 5f95f4f | 2011-10-23 01:01:01 +0000 | [diff] [blame] | 1492 | black[CompositePixelChannel], |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1493 | *equalize_map, |
| 1494 | *histogram, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1495 | *map, |
cristy | 5f95f4f | 2011-10-23 01:01:01 +0000 | [diff] [blame] | 1496 | white[CompositePixelChannel]; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1497 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1498 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1499 | i; |
| 1500 | |
cristy | 564a569 | 2012-01-20 23:56:26 +0000 | [diff] [blame] | 1501 | size_t |
| 1502 | number_channels; |
| 1503 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1504 | ssize_t |
| 1505 | y; |
| 1506 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1507 | /* |
| 1508 | Allocate and initialize histogram arrays. |
| 1509 | */ |
| 1510 | assert(image != (Image *) NULL); |
| 1511 | assert(image->signature == MagickSignature); |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 1512 | if( IfMagickTrue(image->debug) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1513 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1514 | equalize_map=(double *) AcquireQuantumMemory(MaxMap+1UL, |
cristy | f45fec7 | 2011-08-23 16:02:32 +0000 | [diff] [blame] | 1515 | GetPixelChannels(image)*sizeof(*equalize_map)); |
cristy | 7d4aa38 | 2013-06-30 01:59:39 +0000 | [diff] [blame] | 1516 | histogram=(double *) AcquireQuantumMemory(MaxMap+1UL,GetPixelChannels(image)* |
| 1517 | sizeof(*histogram)); |
| 1518 | map=(double *) AcquireQuantumMemory(MaxMap+1UL,GetPixelChannels(image)* |
| 1519 | sizeof(*map)); |
cristy | 955c177 | 2012-12-21 17:25:55 +0000 | [diff] [blame] | 1520 | if ((equalize_map == (double *) NULL) || (histogram == (double *) NULL) || |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1521 | (map == (double *) NULL)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1522 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1523 | if (map != (double *) NULL) |
| 1524 | map=(double *) RelinquishMagickMemory(map); |
| 1525 | if (histogram != (double *) NULL) |
| 1526 | histogram=(double *) RelinquishMagickMemory(histogram); |
| 1527 | if (equalize_map != (double *) NULL) |
| 1528 | equalize_map=(double *) RelinquishMagickMemory(equalize_map); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1529 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
| 1530 | image->filename); |
| 1531 | } |
| 1532 | /* |
| 1533 | Form histogram. |
| 1534 | */ |
cristy | f45fec7 | 2011-08-23 16:02:32 +0000 | [diff] [blame] | 1535 | status=MagickTrue; |
| 1536 | (void) ResetMagickMemory(histogram,0,(MaxMap+1)*GetPixelChannels(image)* |
| 1537 | sizeof(*histogram)); |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 1538 | image_view=AcquireVirtualCacheView(image,exception); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1539 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1540 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1541 | register const Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1542 | *restrict p; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1543 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1544 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1545 | x; |
| 1546 | |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 1547 | if( IfMagickFalse(status) ) |
cristy | f45fec7 | 2011-08-23 16:02:32 +0000 | [diff] [blame] | 1548 | continue; |
| 1549 | p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1550 | if (p == (const Quantum *) NULL) |
cristy | f45fec7 | 2011-08-23 16:02:32 +0000 | [diff] [blame] | 1551 | { |
| 1552 | status=MagickFalse; |
| 1553 | continue; |
| 1554 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1555 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1556 | { |
cristy | f45fec7 | 2011-08-23 16:02:32 +0000 | [diff] [blame] | 1557 | register ssize_t |
| 1558 | i; |
| 1559 | |
| 1560 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
| 1561 | histogram[GetPixelChannels(image)*ScaleQuantumToMap(p[i])+i]++; |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1562 | p+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1563 | } |
| 1564 | } |
cristy | db07095 | 2012-04-20 14:33:00 +0000 | [diff] [blame] | 1565 | image_view=DestroyCacheView(image_view); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1566 | /* |
| 1567 | Integrate the histogram to get the equalization map. |
| 1568 | */ |
cristy | 564a569 | 2012-01-20 23:56:26 +0000 | [diff] [blame] | 1569 | number_channels=GetPixelChannels(image); |
cristy | c94ba6f | 2012-01-29 23:19:58 +0000 | [diff] [blame] | 1570 | for (i=0; i < (ssize_t) number_channels; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1571 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1572 | double |
cristy | f45fec7 | 2011-08-23 16:02:32 +0000 | [diff] [blame] | 1573 | intensity; |
| 1574 | |
| 1575 | register ssize_t |
| 1576 | j; |
| 1577 | |
| 1578 | intensity=0.0; |
| 1579 | for (j=0; j <= (ssize_t) MaxMap; j++) |
| 1580 | { |
| 1581 | intensity+=histogram[GetPixelChannels(image)*j+i]; |
| 1582 | map[GetPixelChannels(image)*j+i]=intensity; |
| 1583 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1584 | } |
cristy | f45fec7 | 2011-08-23 16:02:32 +0000 | [diff] [blame] | 1585 | (void) ResetMagickMemory(equalize_map,0,(MaxMap+1)*GetPixelChannels(image)* |
| 1586 | sizeof(*equalize_map)); |
cristy | 564a569 | 2012-01-20 23:56:26 +0000 | [diff] [blame] | 1587 | number_channels=GetPixelChannels(image); |
cristy | 564a569 | 2012-01-20 23:56:26 +0000 | [diff] [blame] | 1588 | for (i=0; i < (ssize_t) number_channels; i++) |
cristy | f45fec7 | 2011-08-23 16:02:32 +0000 | [diff] [blame] | 1589 | { |
| 1590 | register ssize_t |
| 1591 | j; |
| 1592 | |
| 1593 | black[i]=map[i]; |
| 1594 | white[i]=map[GetPixelChannels(image)*MaxMap+i]; |
| 1595 | if (black[i] != white[i]) |
| 1596 | for (j=0; j <= (ssize_t) MaxMap; j++) |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1597 | equalize_map[GetPixelChannels(image)*j+i]=(double) |
| 1598 | ScaleMapToQuantum((double) ((MaxMap*(map[ |
cristy | f45fec7 | 2011-08-23 16:02:32 +0000 | [diff] [blame] | 1599 | GetPixelChannels(image)*j+i]-black[i]))/(white[i]-black[i]))); |
| 1600 | } |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1601 | histogram=(double *) RelinquishMagickMemory(histogram); |
| 1602 | map=(double *) RelinquishMagickMemory(map); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1603 | if (image->storage_class == PseudoClass) |
| 1604 | { |
cristy | f45fec7 | 2011-08-23 16:02:32 +0000 | [diff] [blame] | 1605 | register ssize_t |
| 1606 | j; |
| 1607 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1608 | /* |
| 1609 | Equalize colormap. |
| 1610 | */ |
cristy | f45fec7 | 2011-08-23 16:02:32 +0000 | [diff] [blame] | 1611 | for (j=0; j < (ssize_t) image->colors; j++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1612 | { |
cristy | f54798b | 2011-11-21 18:38:23 +0000 | [diff] [blame] | 1613 | if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0) |
cristy | f45fec7 | 2011-08-23 16:02:32 +0000 | [diff] [blame] | 1614 | { |
cristy | 5a23c55 | 2013-02-13 14:34:28 +0000 | [diff] [blame] | 1615 | PixelChannel channel=GetPixelChannelChannel(image,RedPixelChannel); |
cristy | f54798b | 2011-11-21 18:38:23 +0000 | [diff] [blame] | 1616 | if (black[channel] != white[channel]) |
cristy | da1f9c1 | 2011-10-02 21:39:49 +0000 | [diff] [blame] | 1617 | image->colormap[j].red=equalize_map[GetPixelChannels(image)* |
cristy | f54798b | 2011-11-21 18:38:23 +0000 | [diff] [blame] | 1618 | ScaleQuantumToMap(ClampToQuantum(image->colormap[j].red))]+ |
| 1619 | channel; |
cristy | f45fec7 | 2011-08-23 16:02:32 +0000 | [diff] [blame] | 1620 | } |
cristy | f54798b | 2011-11-21 18:38:23 +0000 | [diff] [blame] | 1621 | if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0) |
cristy | f45fec7 | 2011-08-23 16:02:32 +0000 | [diff] [blame] | 1622 | { |
cristy | 5a23c55 | 2013-02-13 14:34:28 +0000 | [diff] [blame] | 1623 | PixelChannel channel=GetPixelChannelChannel(image, |
| 1624 | GreenPixelChannel); |
cristy | f54798b | 2011-11-21 18:38:23 +0000 | [diff] [blame] | 1625 | if (black[channel] != white[channel]) |
cristy | da1f9c1 | 2011-10-02 21:39:49 +0000 | [diff] [blame] | 1626 | image->colormap[j].green=equalize_map[GetPixelChannels(image)* |
cristy | f54798b | 2011-11-21 18:38:23 +0000 | [diff] [blame] | 1627 | ScaleQuantumToMap(ClampToQuantum(image->colormap[j].green))]+ |
| 1628 | channel; |
cristy | f45fec7 | 2011-08-23 16:02:32 +0000 | [diff] [blame] | 1629 | } |
cristy | f54798b | 2011-11-21 18:38:23 +0000 | [diff] [blame] | 1630 | if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0) |
cristy | f45fec7 | 2011-08-23 16:02:32 +0000 | [diff] [blame] | 1631 | { |
cristy | 5a23c55 | 2013-02-13 14:34:28 +0000 | [diff] [blame] | 1632 | PixelChannel channel=GetPixelChannelChannel(image,BluePixelChannel); |
cristy | f54798b | 2011-11-21 18:38:23 +0000 | [diff] [blame] | 1633 | if (black[channel] != white[channel]) |
cristy | da1f9c1 | 2011-10-02 21:39:49 +0000 | [diff] [blame] | 1634 | image->colormap[j].blue=equalize_map[GetPixelChannels(image)* |
cristy | f54798b | 2011-11-21 18:38:23 +0000 | [diff] [blame] | 1635 | ScaleQuantumToMap(ClampToQuantum(image->colormap[j].blue))]+ |
| 1636 | channel; |
cristy | f45fec7 | 2011-08-23 16:02:32 +0000 | [diff] [blame] | 1637 | } |
cristy | f54798b | 2011-11-21 18:38:23 +0000 | [diff] [blame] | 1638 | if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) |
cristy | f45fec7 | 2011-08-23 16:02:32 +0000 | [diff] [blame] | 1639 | { |
cristy | 5a23c55 | 2013-02-13 14:34:28 +0000 | [diff] [blame] | 1640 | PixelChannel channel=GetPixelChannelChannel(image, |
| 1641 | AlphaPixelChannel); |
cristy | f54798b | 2011-11-21 18:38:23 +0000 | [diff] [blame] | 1642 | if (black[channel] != white[channel]) |
cristy | da1f9c1 | 2011-10-02 21:39:49 +0000 | [diff] [blame] | 1643 | image->colormap[j].alpha=equalize_map[GetPixelChannels(image)* |
cristy | f54798b | 2011-11-21 18:38:23 +0000 | [diff] [blame] | 1644 | ScaleQuantumToMap(ClampToQuantum(image->colormap[j].alpha))]+ |
| 1645 | channel; |
cristy | f45fec7 | 2011-08-23 16:02:32 +0000 | [diff] [blame] | 1646 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1647 | } |
| 1648 | } |
| 1649 | /* |
| 1650 | Equalize image. |
| 1651 | */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1652 | progress=0; |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 1653 | image_view=AcquireAuthenticCacheView(image,exception); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 1654 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 1655 | #pragma omp parallel for schedule(static,4) shared(progress,status) \ |
cristy | 5e6b259 | 2012-12-19 14:08:11 +0000 | [diff] [blame] | 1656 | magick_threads(image,image,image->rows,1) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1657 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1658 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1659 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1660 | register Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1661 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1662 | |
cristy | 8d4629b | 2010-08-30 17:59:46 +0000 | [diff] [blame] | 1663 | register ssize_t |
| 1664 | x; |
| 1665 | |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 1666 | if( IfMagickFalse(status) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1667 | continue; |
| 1668 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 1669 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1670 | { |
| 1671 | status=MagickFalse; |
| 1672 | continue; |
| 1673 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1674 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1675 | { |
cristy | f45fec7 | 2011-08-23 16:02:32 +0000 | [diff] [blame] | 1676 | register ssize_t |
| 1677 | i; |
| 1678 | |
cristy | 883fde1 | 2013-04-08 00:50:13 +0000 | [diff] [blame] | 1679 | if (GetPixelReadMask(image,q) == 0) |
cristy | 10a6c61 | 2012-01-29 21:41:05 +0000 | [diff] [blame] | 1680 | { |
| 1681 | q+=GetPixelChannels(image); |
| 1682 | continue; |
| 1683 | } |
cristy | f45fec7 | 2011-08-23 16:02:32 +0000 | [diff] [blame] | 1684 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
| 1685 | { |
cristy | 5a23c55 | 2013-02-13 14:34:28 +0000 | [diff] [blame] | 1686 | PixelChannel channel=GetPixelChannelChannel(image,i); |
| 1687 | PixelTrait traits=GetPixelChannelTraits(image,channel); |
cristy | d09f880 | 2012-02-04 16:44:10 +0000 | [diff] [blame] | 1688 | if (((traits & UpdatePixelTrait) == 0) || (black[i] == white[i])) |
| 1689 | continue; |
| 1690 | q[i]=ClampToQuantum(equalize_map[GetPixelChannels(image)* |
| 1691 | ScaleQuantumToMap(q[i])+i]); |
cristy | f45fec7 | 2011-08-23 16:02:32 +0000 | [diff] [blame] | 1692 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1693 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1694 | } |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 1695 | if( IfMagickFalse(SyncCacheViewAuthenticPixels(image_view,exception)) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1696 | status=MagickFalse; |
| 1697 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 1698 | { |
| 1699 | MagickBooleanType |
| 1700 | proceed; |
| 1701 | |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 1702 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 564a569 | 2012-01-20 23:56:26 +0000 | [diff] [blame] | 1703 | #pragma omp critical (MagickCore_EqualizeImage) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1704 | #endif |
| 1705 | proceed=SetImageProgress(image,EqualizeImageTag,progress++,image->rows); |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 1706 | if( IfMagickFalse(proceed) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1707 | status=MagickFalse; |
| 1708 | } |
| 1709 | } |
| 1710 | image_view=DestroyCacheView(image_view); |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1711 | equalize_map=(double *) RelinquishMagickMemory(equalize_map); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1712 | return(status); |
| 1713 | } |
| 1714 | |
| 1715 | /* |
| 1716 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1717 | % % |
| 1718 | % % |
| 1719 | % % |
| 1720 | % G a m m a I m a g e % |
| 1721 | % % |
| 1722 | % % |
| 1723 | % % |
| 1724 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1725 | % |
| 1726 | % GammaImage() gamma-corrects a particular image channel. The same |
| 1727 | % image viewed on different devices will have perceptual differences in the |
| 1728 | % way the image's intensities are represented on the screen. Specify |
| 1729 | % individual gamma levels for the red, green, and blue channels, or adjust |
| 1730 | % all three with the gamma parameter. Values typically range from 0.8 to 2.3. |
| 1731 | % |
| 1732 | % You can also reduce the influence of a particular channel with a gamma |
| 1733 | % value of 0. |
| 1734 | % |
| 1735 | % The format of the GammaImage method is: |
| 1736 | % |
cristy | b3e7c6c | 2011-07-24 01:43:55 +0000 | [diff] [blame] | 1737 | % MagickBooleanType GammaImage(Image *image,const double gamma, |
| 1738 | % ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1739 | % |
| 1740 | % A description of each parameter follows: |
| 1741 | % |
| 1742 | % o image: the image. |
| 1743 | % |
cristy | a636014 | 2011-03-23 23:08:04 +0000 | [diff] [blame] | 1744 | % o level: the image gamma as a string (e.g. 1.6,1.2,1.0). |
| 1745 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1746 | % o gamma: the image gamma. |
| 1747 | % |
| 1748 | */ |
cristy | 4618c6b | 2013-04-25 12:42:33 +0000 | [diff] [blame] | 1749 | |
| 1750 | static inline double gamma_pow(const double value,const double gamma) |
| 1751 | { |
cristy | 38e9738 | 2013-05-05 19:04:17 +0000 | [diff] [blame] | 1752 | return(value < 0.0 ? value : pow(value,gamma)); |
cristy | 4618c6b | 2013-04-25 12:42:33 +0000 | [diff] [blame] | 1753 | } |
| 1754 | |
cristy | b3e7c6c | 2011-07-24 01:43:55 +0000 | [diff] [blame] | 1755 | MagickExport MagickBooleanType GammaImage(Image *image,const double gamma, |
| 1756 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1757 | { |
| 1758 | #define GammaCorrectImageTag "GammaCorrect/Image" |
| 1759 | |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 1760 | CacheView |
| 1761 | *image_view; |
| 1762 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1763 | MagickBooleanType |
| 1764 | status; |
| 1765 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1766 | MagickOffsetType |
| 1767 | progress; |
| 1768 | |
cristy | 1959387 | 2012-01-22 02:00:33 +0000 | [diff] [blame] | 1769 | Quantum |
| 1770 | *gamma_map; |
| 1771 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1772 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1773 | i; |
| 1774 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1775 | ssize_t |
| 1776 | y; |
| 1777 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1778 | /* |
| 1779 | Allocate and initialize gamma maps. |
| 1780 | */ |
| 1781 | assert(image != (Image *) NULL); |
| 1782 | assert(image->signature == MagickSignature); |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 1783 | if( IfMagickTrue(image->debug) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1784 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 1785 | if (gamma == 1.0) |
| 1786 | return(MagickTrue); |
cristy | 1959387 | 2012-01-22 02:00:33 +0000 | [diff] [blame] | 1787 | gamma_map=(Quantum *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*gamma_map)); |
| 1788 | if (gamma_map == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1789 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
| 1790 | image->filename); |
| 1791 | (void) ResetMagickMemory(gamma_map,0,(MaxMap+1)*sizeof(*gamma_map)); |
| 1792 | if (gamma != 0.0) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1793 | for (i=0; i <= (ssize_t) MaxMap; i++) |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1794 | gamma_map[i]=ScaleMapToQuantum((double) (MaxMap*pow((double) i/ |
cristy | 1959387 | 2012-01-22 02:00:33 +0000 | [diff] [blame] | 1795 | MaxMap,1.0/gamma))); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1796 | if (image->storage_class == PseudoClass) |
nicolas | e0aa3c3 | 2012-08-08 14:57:02 +0000 | [diff] [blame] | 1797 | for (i=0; i < (ssize_t) image->colors; i++) |
nicolas | fd74ae3 | 2012-08-08 16:00:11 +0000 | [diff] [blame] | 1798 | { |
| 1799 | /* |
cristy | aeded78 | 2012-09-11 23:39:36 +0000 | [diff] [blame] | 1800 | Gamma-correct colormap. |
nicolas | fd74ae3 | 2012-08-08 16:00:11 +0000 | [diff] [blame] | 1801 | */ |
cristy | 4618c6b | 2013-04-25 12:42:33 +0000 | [diff] [blame] | 1802 | #if !defined(MAGICKCORE_HDRI_SUPPORT) |
nicolas | fd74ae3 | 2012-08-08 16:00:11 +0000 | [diff] [blame] | 1803 | if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0) |
cristy | faa663d | 2013-04-29 00:07:58 +0000 | [diff] [blame] | 1804 | image->colormap[i].red=(double) gamma_map[ScaleQuantumToMap( |
| 1805 | ClampToQuantum(image->colormap[i].red))]; |
nicolas | fd74ae3 | 2012-08-08 16:00:11 +0000 | [diff] [blame] | 1806 | if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0) |
cristy | faa663d | 2013-04-29 00:07:58 +0000 | [diff] [blame] | 1807 | image->colormap[i].green=(double) gamma_map[ScaleQuantumToMap( |
| 1808 | ClampToQuantum(image->colormap[i].green))]; |
nicolas | fd74ae3 | 2012-08-08 16:00:11 +0000 | [diff] [blame] | 1809 | if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0) |
cristy | faa663d | 2013-04-29 00:07:58 +0000 | [diff] [blame] | 1810 | image->colormap[i].blue=(double) gamma_map[ScaleQuantumToMap( |
| 1811 | ClampToQuantum(image->colormap[i].blue))]; |
nicolas | fd74ae3 | 2012-08-08 16:00:11 +0000 | [diff] [blame] | 1812 | if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) |
cristy | faa663d | 2013-04-29 00:07:58 +0000 | [diff] [blame] | 1813 | image->colormap[i].alpha=(double) gamma_map[ScaleQuantumToMap( |
| 1814 | ClampToQuantum(image->colormap[i].alpha))]; |
cristy | 4618c6b | 2013-04-25 12:42:33 +0000 | [diff] [blame] | 1815 | #else |
| 1816 | if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0) |
cristy | 38e9738 | 2013-05-05 19:04:17 +0000 | [diff] [blame] | 1817 | image->colormap[i].red=QuantumRange*gamma_pow(QuantumScale* |
| 1818 | image->colormap[i].red,1.0/gamma); |
cristy | 4618c6b | 2013-04-25 12:42:33 +0000 | [diff] [blame] | 1819 | if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0) |
cristy | 38e9738 | 2013-05-05 19:04:17 +0000 | [diff] [blame] | 1820 | image->colormap[i].green=QuantumRange*gamma_pow(QuantumScale* |
| 1821 | image->colormap[i].green,1.0/gamma); |
cristy | 4618c6b | 2013-04-25 12:42:33 +0000 | [diff] [blame] | 1822 | if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0) |
cristy | 38e9738 | 2013-05-05 19:04:17 +0000 | [diff] [blame] | 1823 | image->colormap[i].blue=QuantumRange*gamma_pow(QuantumScale* |
| 1824 | image->colormap[i].blue,1.0/gamma); |
cristy | 4618c6b | 2013-04-25 12:42:33 +0000 | [diff] [blame] | 1825 | if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) |
cristy | 38e9738 | 2013-05-05 19:04:17 +0000 | [diff] [blame] | 1826 | image->colormap[i].alpha=QuantumRange*gamma_pow(QuantumScale* |
| 1827 | image->colormap[i].alpha,1.0/gamma); |
cristy | 4618c6b | 2013-04-25 12:42:33 +0000 | [diff] [blame] | 1828 | #endif |
nicolas | fd74ae3 | 2012-08-08 16:00:11 +0000 | [diff] [blame] | 1829 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1830 | /* |
| 1831 | Gamma-correct image. |
| 1832 | */ |
| 1833 | status=MagickTrue; |
| 1834 | progress=0; |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 1835 | image_view=AcquireAuthenticCacheView(image,exception); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 1836 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 1837 | #pragma omp parallel for schedule(static,4) shared(progress,status) \ |
cristy | 5e6b259 | 2012-12-19 14:08:11 +0000 | [diff] [blame] | 1838 | magick_threads(image,image,image->rows,1) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1839 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1840 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1841 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1842 | register Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1843 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1844 | |
cristy | 8d4629b | 2010-08-30 17:59:46 +0000 | [diff] [blame] | 1845 | register ssize_t |
| 1846 | x; |
| 1847 | |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 1848 | if( IfMagickFalse(status) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1849 | continue; |
| 1850 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 1851 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1852 | { |
| 1853 | status=MagickFalse; |
| 1854 | continue; |
| 1855 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1856 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1857 | { |
cristy | d476a8e | 2011-07-23 16:13:22 +0000 | [diff] [blame] | 1858 | register ssize_t |
| 1859 | i; |
| 1860 | |
cristy | 883fde1 | 2013-04-08 00:50:13 +0000 | [diff] [blame] | 1861 | if (GetPixelReadMask(image,q) == 0) |
cristy | 10a6c61 | 2012-01-29 21:41:05 +0000 | [diff] [blame] | 1862 | { |
| 1863 | q+=GetPixelChannels(image); |
| 1864 | continue; |
| 1865 | } |
cristy | a30d9ba | 2011-07-23 21:00:48 +0000 | [diff] [blame] | 1866 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
cristy | d476a8e | 2011-07-23 16:13:22 +0000 | [diff] [blame] | 1867 | { |
cristy | 5a23c55 | 2013-02-13 14:34:28 +0000 | [diff] [blame] | 1868 | PixelChannel channel=GetPixelChannelChannel(image,i); |
| 1869 | PixelTrait traits=GetPixelChannelTraits(image,channel); |
cristy | d09f880 | 2012-02-04 16:44:10 +0000 | [diff] [blame] | 1870 | if ((traits & UpdatePixelTrait) == 0) |
| 1871 | continue; |
cristy | 4618c6b | 2013-04-25 12:42:33 +0000 | [diff] [blame] | 1872 | #if !defined(MAGICKCORE_HDRI_SUPPORT) |
cristy | d09f880 | 2012-02-04 16:44:10 +0000 | [diff] [blame] | 1873 | q[i]=gamma_map[ScaleQuantumToMap(q[i])]; |
cristy | 4618c6b | 2013-04-25 12:42:33 +0000 | [diff] [blame] | 1874 | #else |
cristy | 38e9738 | 2013-05-05 19:04:17 +0000 | [diff] [blame] | 1875 | q[i]=QuantumRange*gamma_pow(QuantumScale*q[i],1.0/gamma); |
cristy | 4618c6b | 2013-04-25 12:42:33 +0000 | [diff] [blame] | 1876 | #endif |
cristy | d476a8e | 2011-07-23 16:13:22 +0000 | [diff] [blame] | 1877 | } |
cristy | a30d9ba | 2011-07-23 21:00:48 +0000 | [diff] [blame] | 1878 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1879 | } |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 1880 | if( IfMagickFalse(SyncCacheViewAuthenticPixels(image_view,exception)) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1881 | status=MagickFalse; |
| 1882 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 1883 | { |
| 1884 | MagickBooleanType |
| 1885 | proceed; |
| 1886 | |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 1887 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 564a569 | 2012-01-20 23:56:26 +0000 | [diff] [blame] | 1888 | #pragma omp critical (MagickCore_GammaImage) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1889 | #endif |
| 1890 | proceed=SetImageProgress(image,GammaCorrectImageTag,progress++, |
| 1891 | image->rows); |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 1892 | if( IfMagickFalse(proceed) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1893 | status=MagickFalse; |
| 1894 | } |
| 1895 | } |
| 1896 | image_view=DestroyCacheView(image_view); |
cristy | 1959387 | 2012-01-22 02:00:33 +0000 | [diff] [blame] | 1897 | gamma_map=(Quantum *) RelinquishMagickMemory(gamma_map); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1898 | if (image->gamma != 0.0) |
| 1899 | image->gamma*=gamma; |
| 1900 | return(status); |
| 1901 | } |
| 1902 | |
| 1903 | /* |
| 1904 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1905 | % % |
| 1906 | % % |
| 1907 | % % |
cristy | ab94359 | 2013-03-29 16:47:23 +0000 | [diff] [blame] | 1908 | % G r a y s c a l e I m a g e % |
| 1909 | % % |
| 1910 | % % |
| 1911 | % % |
| 1912 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1913 | % |
| 1914 | % GrayscaleImage() converts the image to grayscale. |
| 1915 | % |
| 1916 | % The format of the GrayscaleImage method is: |
| 1917 | % |
| 1918 | % MagickBooleanType GrayscaleImage(Image *image, |
| 1919 | % const PixelIntensityMethod method ,ExceptionInfo *exception) |
| 1920 | % |
| 1921 | % A description of each parameter follows: |
| 1922 | % |
| 1923 | % o image: the image. |
| 1924 | % |
| 1925 | % o method: the pixel intensity method. |
| 1926 | % |
| 1927 | % o exception: return any errors or warnings in this structure. |
| 1928 | % |
| 1929 | */ |
| 1930 | |
| 1931 | static inline MagickRealType MagickMax(const MagickRealType x, |
| 1932 | const MagickRealType y) |
| 1933 | { |
| 1934 | if (x > y) |
| 1935 | return(x); |
| 1936 | return(y); |
| 1937 | } |
| 1938 | |
| 1939 | static inline MagickRealType MagickMin(const MagickRealType x, |
| 1940 | const MagickRealType y) |
| 1941 | { |
| 1942 | if (x < y) |
| 1943 | return(x); |
| 1944 | return(y); |
| 1945 | } |
| 1946 | |
| 1947 | MagickExport MagickBooleanType GrayscaleImage(Image *image, |
cristy | 95bbc4f | 2013-04-25 19:02:42 +0000 | [diff] [blame] | 1948 | const PixelIntensityMethod method,ExceptionInfo *exception) |
cristy | ab94359 | 2013-03-29 16:47:23 +0000 | [diff] [blame] | 1949 | { |
| 1950 | #define GrayscaleImageTag "Grayscale/Image" |
| 1951 | |
| 1952 | CacheView |
| 1953 | *image_view; |
| 1954 | |
| 1955 | MagickBooleanType |
| 1956 | status; |
| 1957 | |
| 1958 | MagickOffsetType |
| 1959 | progress; |
| 1960 | |
| 1961 | ssize_t |
| 1962 | y; |
| 1963 | |
| 1964 | assert(image != (Image *) NULL); |
| 1965 | assert(image->signature == MagickSignature); |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 1966 | if( IfMagickTrue(image->debug) ) |
cristy | ab94359 | 2013-03-29 16:47:23 +0000 | [diff] [blame] | 1967 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 1968 | if (image->storage_class == PseudoClass) |
| 1969 | { |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 1970 | if( IfMagickFalse(SyncImage(image,exception)) ) |
cristy | ab94359 | 2013-03-29 16:47:23 +0000 | [diff] [blame] | 1971 | return(MagickFalse); |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 1972 | if( IfMagickFalse(SetImageStorageClass(image,DirectClass,exception)) ) |
cristy | ab94359 | 2013-03-29 16:47:23 +0000 | [diff] [blame] | 1973 | return(MagickFalse); |
| 1974 | } |
cristy | f96cb21 | 2013-04-10 22:28:48 +0000 | [diff] [blame] | 1975 | switch (image->intensity) |
| 1976 | { |
| 1977 | case Rec601LuminancePixelIntensityMethod: |
| 1978 | case Rec709LuminancePixelIntensityMethod: |
| 1979 | { |
cristy | 0c81d06 | 2013-04-21 15:22:02 +0000 | [diff] [blame] | 1980 | (void) SetImageColorspace(image,RGBColorspace,exception); |
cristy | f96cb21 | 2013-04-10 22:28:48 +0000 | [diff] [blame] | 1981 | break; |
| 1982 | } |
| 1983 | case Rec601LumaPixelIntensityMethod: |
| 1984 | case Rec709LumaPixelIntensityMethod: |
| 1985 | case UndefinedPixelIntensityMethod: |
| 1986 | { |
cristy | 0c81d06 | 2013-04-21 15:22:02 +0000 | [diff] [blame] | 1987 | (void) SetImageColorspace(image,sRGBColorspace,exception); |
cristy | f96cb21 | 2013-04-10 22:28:48 +0000 | [diff] [blame] | 1988 | break; |
| 1989 | } |
| 1990 | default: |
| 1991 | break; |
| 1992 | } |
cristy | ab94359 | 2013-03-29 16:47:23 +0000 | [diff] [blame] | 1993 | /* |
| 1994 | Grayscale image. |
| 1995 | */ |
| 1996 | status=MagickTrue; |
| 1997 | progress=0; |
| 1998 | image_view=AcquireAuthenticCacheView(image,exception); |
| 1999 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 2000 | #pragma omp parallel for schedule(static,4) shared(progress,status) \ |
| 2001 | magick_threads(image,image,image->rows,1) |
| 2002 | #endif |
| 2003 | for (y=0; y < (ssize_t) image->rows; y++) |
| 2004 | { |
| 2005 | register Quantum |
| 2006 | *restrict q; |
| 2007 | |
| 2008 | register ssize_t |
| 2009 | x; |
| 2010 | |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 2011 | if( IfMagickFalse(status) ) |
cristy | ab94359 | 2013-03-29 16:47:23 +0000 | [diff] [blame] | 2012 | continue; |
| 2013 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
| 2014 | if (q == (Quantum *) NULL) |
| 2015 | { |
| 2016 | status=MagickFalse; |
| 2017 | continue; |
| 2018 | } |
| 2019 | for (x=0; x < (ssize_t) image->columns; x++) |
| 2020 | { |
| 2021 | MagickRealType |
| 2022 | blue, |
| 2023 | green, |
| 2024 | red, |
| 2025 | intensity; |
| 2026 | |
cristy | 883fde1 | 2013-04-08 00:50:13 +0000 | [diff] [blame] | 2027 | if (GetPixelReadMask(image,q) == 0) |
cristy | ab94359 | 2013-03-29 16:47:23 +0000 | [diff] [blame] | 2028 | { |
| 2029 | q+=GetPixelChannels(image); |
| 2030 | continue; |
| 2031 | } |
| 2032 | red=(MagickRealType) GetPixelRed(image,q); |
| 2033 | green=(MagickRealType) GetPixelGreen(image,q); |
| 2034 | blue=(MagickRealType) GetPixelBlue(image,q); |
cristy | f96cb21 | 2013-04-10 22:28:48 +0000 | [diff] [blame] | 2035 | intensity=0.0; |
cristy | 95bbc4f | 2013-04-25 19:02:42 +0000 | [diff] [blame] | 2036 | switch (method) |
cristy | ab94359 | 2013-03-29 16:47:23 +0000 | [diff] [blame] | 2037 | { |
| 2038 | case AveragePixelIntensityMethod: |
| 2039 | { |
| 2040 | intensity=(red+green+blue)/3.0; |
| 2041 | break; |
| 2042 | } |
| 2043 | case BrightnessPixelIntensityMethod: |
| 2044 | { |
| 2045 | intensity=MagickMax(MagickMax(red,green),blue); |
| 2046 | break; |
| 2047 | } |
| 2048 | case LightnessPixelIntensityMethod: |
| 2049 | { |
cristy | 06d9c90 | 2013-06-02 17:55:06 +0000 | [diff] [blame] | 2050 | intensity=(MagickMin(MagickMin(red,green),blue)+ |
| 2051 | MagickMax(MagickMax(red,green),blue))/2.0; |
cristy | ab94359 | 2013-03-29 16:47:23 +0000 | [diff] [blame] | 2052 | break; |
| 2053 | } |
cristy | b04db12 | 2013-04-11 10:26:01 +0000 | [diff] [blame] | 2054 | case MSPixelIntensityMethod: |
| 2055 | { |
| 2056 | intensity=(MagickRealType) (((double) red*red+green*green+ |
| 2057 | blue*blue)/3.0); |
| 2058 | break; |
| 2059 | } |
cristy | ab94359 | 2013-03-29 16:47:23 +0000 | [diff] [blame] | 2060 | case Rec601LumaPixelIntensityMethod: |
| 2061 | { |
cristy | 06d9c90 | 2013-06-02 17:55:06 +0000 | [diff] [blame] | 2062 | if (image->colorspace == RGBColorspace) |
| 2063 | { |
| 2064 | red=EncodePixelGamma(red); |
| 2065 | green=EncodePixelGamma(green); |
| 2066 | blue=EncodePixelGamma(blue); |
| 2067 | } |
cristy | 9e2436a | 2013-05-02 20:35:59 +0000 | [diff] [blame] | 2068 | intensity=0.298839*red+0.586811*green+0.114350*blue; |
cristy | ab94359 | 2013-03-29 16:47:23 +0000 | [diff] [blame] | 2069 | break; |
| 2070 | } |
| 2071 | case Rec601LuminancePixelIntensityMethod: |
| 2072 | { |
cristy | 06d9c90 | 2013-06-02 17:55:06 +0000 | [diff] [blame] | 2073 | if (image->colorspace == sRGBColorspace) |
| 2074 | { |
| 2075 | red=DecodePixelGamma(red); |
| 2076 | green=DecodePixelGamma(green); |
| 2077 | blue=DecodePixelGamma(blue); |
| 2078 | } |
cristy | 9e2436a | 2013-05-02 20:35:59 +0000 | [diff] [blame] | 2079 | intensity=0.298839*red+0.586811*green+0.114350*blue; |
cristy | ab94359 | 2013-03-29 16:47:23 +0000 | [diff] [blame] | 2080 | break; |
| 2081 | } |
| 2082 | case Rec709LumaPixelIntensityMethod: |
cristy | 06d9c90 | 2013-06-02 17:55:06 +0000 | [diff] [blame] | 2083 | default: |
cristy | ab94359 | 2013-03-29 16:47:23 +0000 | [diff] [blame] | 2084 | { |
cristy | 06d9c90 | 2013-06-02 17:55:06 +0000 | [diff] [blame] | 2085 | if (image->colorspace == RGBColorspace) |
| 2086 | { |
| 2087 | red=EncodePixelGamma(red); |
| 2088 | green=EncodePixelGamma(green); |
| 2089 | blue=EncodePixelGamma(blue); |
| 2090 | } |
cristy | 1352acf | 2013-06-10 17:16:17 +0000 | [diff] [blame] | 2091 | intensity=0.212656*red+0.715158*green+0.072186*blue; |
cristy | ab94359 | 2013-03-29 16:47:23 +0000 | [diff] [blame] | 2092 | break; |
| 2093 | } |
| 2094 | case Rec709LuminancePixelIntensityMethod: |
| 2095 | { |
cristy | 06d9c90 | 2013-06-02 17:55:06 +0000 | [diff] [blame] | 2096 | if (image->colorspace == sRGBColorspace) |
| 2097 | { |
| 2098 | red=DecodePixelGamma(red); |
| 2099 | green=DecodePixelGamma(green); |
| 2100 | blue=DecodePixelGamma(blue); |
| 2101 | } |
cristy | 1352acf | 2013-06-10 17:16:17 +0000 | [diff] [blame] | 2102 | intensity=0.212656*red+0.715158*green+0.072186*blue; |
cristy | ab94359 | 2013-03-29 16:47:23 +0000 | [diff] [blame] | 2103 | break; |
| 2104 | } |
| 2105 | case RMSPixelIntensityMethod: |
| 2106 | { |
cristy | 5afee3a | 2013-04-11 10:18:42 +0000 | [diff] [blame] | 2107 | intensity=(MagickRealType) (sqrt((double) red*red+green*green+ |
cristy | 84d50c6 | 2013-04-11 10:26:35 +0000 | [diff] [blame] | 2108 | blue*blue)/sqrt(3.0)); |
cristy | ab94359 | 2013-03-29 16:47:23 +0000 | [diff] [blame] | 2109 | break; |
| 2110 | } |
| 2111 | } |
| 2112 | SetPixelGray(image,ClampToQuantum(intensity),q); |
| 2113 | q+=GetPixelChannels(image); |
| 2114 | } |
cristy | 06d9c90 | 2013-06-02 17:55:06 +0000 | [diff] [blame] | 2115 | if (IfMagickFalse(SyncCacheViewAuthenticPixels(image_view,exception))) |
cristy | ab94359 | 2013-03-29 16:47:23 +0000 | [diff] [blame] | 2116 | status=MagickFalse; |
| 2117 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 2118 | { |
| 2119 | MagickBooleanType |
| 2120 | proceed; |
| 2121 | |
| 2122 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 2123 | #pragma omp critical (MagickCore_GrayscaleImage) |
| 2124 | #endif |
| 2125 | proceed=SetImageProgress(image,GrayscaleImageTag,progress++, |
| 2126 | image->rows); |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 2127 | if( IfMagickFalse(proceed) ) |
cristy | ab94359 | 2013-03-29 16:47:23 +0000 | [diff] [blame] | 2128 | status=MagickFalse; |
| 2129 | } |
| 2130 | } |
| 2131 | image_view=DestroyCacheView(image_view); |
cristy | 95bbc4f | 2013-04-25 19:02:42 +0000 | [diff] [blame] | 2132 | image->intensity=method; |
cristy | f96cb21 | 2013-04-10 22:28:48 +0000 | [diff] [blame] | 2133 | image->type=GrayscaleType; |
cristy | 95bbc4f | 2013-04-25 19:02:42 +0000 | [diff] [blame] | 2134 | return(SetImageColorspace(image,GRAYColorspace,exception)); |
cristy | ab94359 | 2013-03-29 16:47:23 +0000 | [diff] [blame] | 2135 | } |
| 2136 | |
| 2137 | /* |
| 2138 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2139 | % % |
| 2140 | % % |
| 2141 | % % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2142 | % H a l d C l u t I m a g e % |
| 2143 | % % |
| 2144 | % % |
| 2145 | % % |
| 2146 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2147 | % |
| 2148 | % HaldClutImage() applies a Hald color lookup table to the image. A Hald |
| 2149 | % color lookup table is a 3-dimensional color cube mapped to 2 dimensions. |
| 2150 | % Create it with the HALD coder. You can apply any color transformation to |
| 2151 | % the Hald image and then use this method to apply the transform to the |
| 2152 | % image. |
| 2153 | % |
| 2154 | % The format of the HaldClutImage method is: |
| 2155 | % |
cristy | 7c0a0a4 | 2011-08-23 17:57:25 +0000 | [diff] [blame] | 2156 | % MagickBooleanType HaldClutImage(Image *image,Image *hald_image, |
| 2157 | % ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2158 | % |
| 2159 | % A description of each parameter follows: |
| 2160 | % |
| 2161 | % o image: the image, which is replaced by indexed CLUT values |
| 2162 | % |
| 2163 | % o hald_image: the color lookup table image for replacement color values. |
| 2164 | % |
cristy | 7c0a0a4 | 2011-08-23 17:57:25 +0000 | [diff] [blame] | 2165 | % o exception: return any errors or warnings in this structure. |
| 2166 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2167 | */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2168 | MagickExport MagickBooleanType HaldClutImage(Image *image, |
cristy | 7c0a0a4 | 2011-08-23 17:57:25 +0000 | [diff] [blame] | 2169 | const Image *hald_image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2170 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2171 | #define HaldClutImageTag "Clut/Image" |
| 2172 | |
| 2173 | typedef struct _HaldInfo |
| 2174 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 2175 | double |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2176 | x, |
| 2177 | y, |
| 2178 | z; |
| 2179 | } HaldInfo; |
| 2180 | |
cristy | fa11211 | 2010-01-04 17:48:07 +0000 | [diff] [blame] | 2181 | CacheView |
cristy | d551fbc | 2011-03-31 18:07:46 +0000 | [diff] [blame] | 2182 | *hald_view, |
cristy | fa11211 | 2010-01-04 17:48:07 +0000 | [diff] [blame] | 2183 | *image_view; |
| 2184 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2185 | double |
| 2186 | width; |
| 2187 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2188 | MagickBooleanType |
| 2189 | status; |
| 2190 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2191 | MagickOffsetType |
| 2192 | progress; |
| 2193 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2194 | PixelInfo |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2195 | zero; |
| 2196 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2197 | size_t |
| 2198 | cube_size, |
| 2199 | length, |
| 2200 | level; |
| 2201 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2202 | ssize_t |
| 2203 | y; |
| 2204 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2205 | assert(image != (Image *) NULL); |
| 2206 | assert(image->signature == MagickSignature); |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 2207 | if( IfMagickTrue(image->debug) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2208 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 2209 | assert(hald_image != (Image *) NULL); |
| 2210 | assert(hald_image->signature == MagickSignature); |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 2211 | if( IfMagickFalse(SetImageStorageClass(image,DirectClass,exception)) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2212 | return(MagickFalse); |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 2213 | if (image->alpha_trait != BlendPixelTrait) |
cristy | 6324088 | 2011-08-05 19:05:27 +0000 | [diff] [blame] | 2214 | (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2215 | /* |
| 2216 | Hald clut image. |
| 2217 | */ |
| 2218 | status=MagickTrue; |
| 2219 | progress=0; |
cristy | ab94359 | 2013-03-29 16:47:23 +0000 | [diff] [blame] | 2220 | length=(size_t) MagickMin((MagickRealType) hald_image->columns, |
| 2221 | (MagickRealType) hald_image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2222 | for (level=2; (level*level*level) < length; level++) ; |
| 2223 | level*=level; |
| 2224 | cube_size=level*level; |
| 2225 | width=(double) hald_image->columns; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2226 | GetPixelInfo(hald_image,&zero); |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 2227 | hald_view=AcquireVirtualCacheView(hald_image,exception); |
| 2228 | image_view=AcquireAuthenticCacheView(image,exception); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 2229 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 2230 | #pragma omp parallel for schedule(static,4) shared(progress,status) \ |
cristy | 5e6b259 | 2012-12-19 14:08:11 +0000 | [diff] [blame] | 2231 | magick_threads(image,image,image->rows,1) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2232 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2233 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2234 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2235 | register Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 2236 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2237 | |
cristy | 8d4629b | 2010-08-30 17:59:46 +0000 | [diff] [blame] | 2238 | register ssize_t |
| 2239 | x; |
| 2240 | |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 2241 | if( IfMagickFalse(status) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2242 | continue; |
| 2243 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 2244 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2245 | { |
| 2246 | status=MagickFalse; |
| 2247 | continue; |
| 2248 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2249 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2250 | { |
cristy | 7c0a0a4 | 2011-08-23 17:57:25 +0000 | [diff] [blame] | 2251 | double |
| 2252 | offset; |
| 2253 | |
| 2254 | HaldInfo |
| 2255 | point; |
| 2256 | |
| 2257 | PixelInfo |
| 2258 | pixel, |
| 2259 | pixel1, |
| 2260 | pixel2, |
| 2261 | pixel3, |
| 2262 | pixel4; |
| 2263 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2264 | point.x=QuantumScale*(level-1.0)*GetPixelRed(image,q); |
| 2265 | point.y=QuantumScale*(level-1.0)*GetPixelGreen(image,q); |
| 2266 | point.z=QuantumScale*(level-1.0)*GetPixelBlue(image,q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2267 | offset=point.x+level*floor(point.y)+cube_size*floor(point.z); |
| 2268 | point.x-=floor(point.x); |
| 2269 | point.y-=floor(point.y); |
| 2270 | point.z-=floor(point.z); |
cristy | 7c0a0a4 | 2011-08-23 17:57:25 +0000 | [diff] [blame] | 2271 | pixel1=zero; |
| 2272 | (void) InterpolatePixelInfo(image,hald_view,image->interpolate, |
| 2273 | fmod(offset,width),floor(offset/width),&pixel1,exception); |
| 2274 | pixel2=zero; |
| 2275 | (void) InterpolatePixelInfo(image,hald_view,image->interpolate, |
| 2276 | fmod(offset+level,width),floor((offset+level)/width),&pixel2,exception); |
| 2277 | pixel3=zero; |
| 2278 | CompositePixelInfoAreaBlend(&pixel1,pixel1.alpha,&pixel2,pixel2.alpha, |
| 2279 | point.y,&pixel3); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2280 | offset+=cube_size; |
cristy | 7c0a0a4 | 2011-08-23 17:57:25 +0000 | [diff] [blame] | 2281 | (void) InterpolatePixelInfo(image,hald_view,image->interpolate, |
| 2282 | fmod(offset,width),floor(offset/width),&pixel1,exception); |
| 2283 | (void) InterpolatePixelInfo(image,hald_view,image->interpolate, |
| 2284 | fmod(offset+level,width),floor((offset+level)/width),&pixel2,exception); |
| 2285 | pixel4=zero; |
| 2286 | CompositePixelInfoAreaBlend(&pixel1,pixel1.alpha,&pixel2,pixel2.alpha, |
| 2287 | point.y,&pixel4); |
| 2288 | pixel=zero; |
| 2289 | CompositePixelInfoAreaBlend(&pixel3,pixel3.alpha,&pixel4,pixel4.alpha, |
| 2290 | point.z,&pixel); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2291 | if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0) |
cristy | f45fec7 | 2011-08-23 16:02:32 +0000 | [diff] [blame] | 2292 | SetPixelRed(image,ClampToQuantum(pixel.red),q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2293 | if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0) |
cristy | f45fec7 | 2011-08-23 16:02:32 +0000 | [diff] [blame] | 2294 | SetPixelGreen(image,ClampToQuantum(pixel.green),q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2295 | if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0) |
cristy | f45fec7 | 2011-08-23 16:02:32 +0000 | [diff] [blame] | 2296 | SetPixelBlue(image,ClampToQuantum(pixel.blue),q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2297 | if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) && |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2298 | (image->colorspace == CMYKColorspace)) |
cristy | f45fec7 | 2011-08-23 16:02:32 +0000 | [diff] [blame] | 2299 | SetPixelBlack(image,ClampToQuantum(pixel.black),q); |
| 2300 | if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) && |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 2301 | (image->alpha_trait == BlendPixelTrait)) |
cristy | f45fec7 | 2011-08-23 16:02:32 +0000 | [diff] [blame] | 2302 | SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2303 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2304 | } |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 2305 | if( IfMagickFalse(SyncCacheViewAuthenticPixels(image_view,exception)) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2306 | status=MagickFalse; |
| 2307 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 2308 | { |
| 2309 | MagickBooleanType |
| 2310 | proceed; |
| 2311 | |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 2312 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 564a569 | 2012-01-20 23:56:26 +0000 | [diff] [blame] | 2313 | #pragma omp critical (MagickCore_HaldClutImage) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2314 | #endif |
| 2315 | proceed=SetImageProgress(image,HaldClutImageTag,progress++,image->rows); |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 2316 | if( IfMagickFalse(proceed) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2317 | status=MagickFalse; |
| 2318 | } |
| 2319 | } |
cristy | d551fbc | 2011-03-31 18:07:46 +0000 | [diff] [blame] | 2320 | hald_view=DestroyCacheView(hald_view); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2321 | image_view=DestroyCacheView(image_view); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2322 | return(status); |
| 2323 | } |
| 2324 | |
| 2325 | /* |
| 2326 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2327 | % % |
| 2328 | % % |
| 2329 | % % |
| 2330 | % L e v e l I m a g e % |
| 2331 | % % |
| 2332 | % % |
| 2333 | % % |
| 2334 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2335 | % |
| 2336 | % LevelImage() adjusts the levels of a particular image channel by |
| 2337 | % scaling the colors falling between specified white and black points to |
| 2338 | % the full available quantum range. |
| 2339 | % |
| 2340 | % The parameters provided represent the black, and white points. The black |
| 2341 | % point specifies the darkest color in the image. Colors darker than the |
| 2342 | % black point are set to zero. White point specifies the lightest color in |
| 2343 | % the image. Colors brighter than the white point are set to the maximum |
| 2344 | % quantum value. |
| 2345 | % |
| 2346 | % If a '!' flag is given, map black and white colors to the given levels |
| 2347 | % rather than mapping those levels to black and white. See |
cristy | 50fbc38 | 2011-07-07 02:19:17 +0000 | [diff] [blame] | 2348 | % LevelizeImage() below. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2349 | % |
| 2350 | % Gamma specifies a gamma correction to apply to the image. |
| 2351 | % |
| 2352 | % The format of the LevelImage method is: |
| 2353 | % |
cristy | 01e9afd | 2011-08-10 17:38:41 +0000 | [diff] [blame] | 2354 | % MagickBooleanType LevelImage(Image *image,const double black_point, |
| 2355 | % const double white_point,const double gamma,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2356 | % |
| 2357 | % A description of each parameter follows: |
| 2358 | % |
| 2359 | % o image: the image. |
| 2360 | % |
cristy | 01e9afd | 2011-08-10 17:38:41 +0000 | [diff] [blame] | 2361 | % o black_point: The level to map zero (black) to. |
| 2362 | % |
| 2363 | % o white_point: The level to map QuantumRange (white) to. |
| 2364 | % |
| 2365 | % o exception: return any errors or warnings in this structure. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2366 | % |
| 2367 | */ |
cristy | 780e9ef | 2011-12-18 23:33:50 +0000 | [diff] [blame] | 2368 | |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 2369 | static inline double LevelPixel(const double black_point, |
| 2370 | const double white_point,const double gamma,const double pixel) |
cristy | 780e9ef | 2011-12-18 23:33:50 +0000 | [diff] [blame] | 2371 | { |
| 2372 | double |
| 2373 | level_pixel, |
| 2374 | scale; |
| 2375 | |
cristy | 780e9ef | 2011-12-18 23:33:50 +0000 | [diff] [blame] | 2376 | scale=(white_point != black_point) ? 1.0/(white_point-black_point) : 1.0; |
cristy | e128ac5 | 2013-05-25 13:19:06 +0000 | [diff] [blame] | 2377 | level_pixel=QuantumRange*gamma_pow(scale*((double) pixel-black_point), |
| 2378 | 1.0/gamma); |
cristy | 00d4fee | 2013-04-23 18:35:34 +0000 | [diff] [blame] | 2379 | return(level_pixel); |
cristy | 780e9ef | 2011-12-18 23:33:50 +0000 | [diff] [blame] | 2380 | } |
| 2381 | |
cristy | 7c0a0a4 | 2011-08-23 17:57:25 +0000 | [diff] [blame] | 2382 | MagickExport MagickBooleanType LevelImage(Image *image,const double black_point, |
| 2383 | const double white_point,const double gamma,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2384 | { |
| 2385 | #define LevelImageTag "Level/Image" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2386 | |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 2387 | CacheView |
| 2388 | *image_view; |
| 2389 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2390 | MagickBooleanType |
| 2391 | status; |
| 2392 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2393 | MagickOffsetType |
| 2394 | progress; |
| 2395 | |
cristy | 8d4629b | 2010-08-30 17:59:46 +0000 | [diff] [blame] | 2396 | register ssize_t |
| 2397 | i; |
| 2398 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2399 | ssize_t |
| 2400 | y; |
| 2401 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2402 | /* |
| 2403 | Allocate and initialize levels map. |
| 2404 | */ |
| 2405 | assert(image != (Image *) NULL); |
| 2406 | assert(image->signature == MagickSignature); |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 2407 | if( IfMagickTrue(image->debug) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2408 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 2409 | if (image->storage_class == PseudoClass) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2410 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2411 | { |
| 2412 | /* |
| 2413 | Level colormap. |
| 2414 | */ |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2415 | if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0) |
cristy | 780e9ef | 2011-12-18 23:33:50 +0000 | [diff] [blame] | 2416 | image->colormap[i].red=(double) ClampToQuantum(LevelPixel(black_point, |
| 2417 | white_point,gamma,image->colormap[i].red)); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2418 | if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0) |
cristy | 780e9ef | 2011-12-18 23:33:50 +0000 | [diff] [blame] | 2419 | image->colormap[i].green=(double) ClampToQuantum(LevelPixel(black_point, |
| 2420 | white_point,gamma,image->colormap[i].green)); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2421 | if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0) |
cristy | 780e9ef | 2011-12-18 23:33:50 +0000 | [diff] [blame] | 2422 | image->colormap[i].blue=(double) ClampToQuantum(LevelPixel(black_point, |
| 2423 | white_point,gamma,image->colormap[i].blue)); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2424 | if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) |
cristy | 780e9ef | 2011-12-18 23:33:50 +0000 | [diff] [blame] | 2425 | image->colormap[i].alpha=(double) ClampToQuantum(LevelPixel(black_point, |
| 2426 | white_point,gamma,image->colormap[i].alpha)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2427 | } |
| 2428 | /* |
| 2429 | Level image. |
| 2430 | */ |
| 2431 | status=MagickTrue; |
| 2432 | progress=0; |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 2433 | image_view=AcquireAuthenticCacheView(image,exception); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 2434 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 2435 | #pragma omp parallel for schedule(static,4) shared(progress,status) \ |
cristy | 5e6b259 | 2012-12-19 14:08:11 +0000 | [diff] [blame] | 2436 | magick_threads(image,image,image->rows,1) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2437 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2438 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2439 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2440 | register Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 2441 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2442 | |
cristy | 8d4629b | 2010-08-30 17:59:46 +0000 | [diff] [blame] | 2443 | register ssize_t |
| 2444 | x; |
| 2445 | |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 2446 | if( IfMagickFalse(status) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2447 | continue; |
| 2448 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 2449 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2450 | { |
| 2451 | status=MagickFalse; |
| 2452 | continue; |
| 2453 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2454 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2455 | { |
cristy | 7c0a0a4 | 2011-08-23 17:57:25 +0000 | [diff] [blame] | 2456 | register ssize_t |
| 2457 | i; |
| 2458 | |
cristy | 883fde1 | 2013-04-08 00:50:13 +0000 | [diff] [blame] | 2459 | if (GetPixelReadMask(image,q) == 0) |
cristy | 10a6c61 | 2012-01-29 21:41:05 +0000 | [diff] [blame] | 2460 | { |
| 2461 | q+=GetPixelChannels(image); |
| 2462 | continue; |
| 2463 | } |
cristy | 7c0a0a4 | 2011-08-23 17:57:25 +0000 | [diff] [blame] | 2464 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
| 2465 | { |
cristy | 5a23c55 | 2013-02-13 14:34:28 +0000 | [diff] [blame] | 2466 | PixelChannel channel=GetPixelChannelChannel(image,i); |
| 2467 | PixelTrait traits=GetPixelChannelTraits(image,channel); |
cristy | d09f880 | 2012-02-04 16:44:10 +0000 | [diff] [blame] | 2468 | if ((traits & UpdatePixelTrait) == 0) |
cristy | 7c0a0a4 | 2011-08-23 17:57:25 +0000 | [diff] [blame] | 2469 | continue; |
cristy | 780e9ef | 2011-12-18 23:33:50 +0000 | [diff] [blame] | 2470 | q[i]=ClampToQuantum(LevelPixel(black_point,white_point,gamma, |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 2471 | (double) q[i])); |
cristy | 7c0a0a4 | 2011-08-23 17:57:25 +0000 | [diff] [blame] | 2472 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2473 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2474 | } |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 2475 | if( IfMagickFalse(SyncCacheViewAuthenticPixels(image_view,exception)) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2476 | status=MagickFalse; |
| 2477 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 2478 | { |
| 2479 | MagickBooleanType |
| 2480 | proceed; |
| 2481 | |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 2482 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 564a569 | 2012-01-20 23:56:26 +0000 | [diff] [blame] | 2483 | #pragma omp critical (MagickCore_LevelImage) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2484 | #endif |
| 2485 | proceed=SetImageProgress(image,LevelImageTag,progress++,image->rows); |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 2486 | if( IfMagickFalse(proceed) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2487 | status=MagickFalse; |
| 2488 | } |
| 2489 | } |
| 2490 | image_view=DestroyCacheView(image_view); |
cristy | fb43840 | 2013-07-28 19:59:59 +0000 | [diff] [blame] | 2491 | (void) ClampImage(image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2492 | return(status); |
| 2493 | } |
| 2494 | |
| 2495 | /* |
| 2496 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2497 | % % |
| 2498 | % % |
| 2499 | % % |
cristy | 33bd515 | 2011-08-24 01:42:24 +0000 | [diff] [blame] | 2500 | % L e v e l i z e I m a g e % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2501 | % % |
| 2502 | % % |
| 2503 | % % |
| 2504 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2505 | % |
cristy | 50fbc38 | 2011-07-07 02:19:17 +0000 | [diff] [blame] | 2506 | % LevelizeImage() applies the reversed LevelImage() operation to just |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2507 | % the specific channels specified. It compresses the full range of color |
| 2508 | % values, so that they lie between the given black and white points. Gamma is |
| 2509 | % applied before the values are mapped. |
| 2510 | % |
cristy | 50fbc38 | 2011-07-07 02:19:17 +0000 | [diff] [blame] | 2511 | % LevelizeImage() can be called with by using a +level command line |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2512 | % API option, or using a '!' on a -level or LevelImage() geometry string. |
| 2513 | % |
anthony | 31f1bf7 | 2012-01-30 12:37:22 +0000 | [diff] [blame] | 2514 | % It can be used to de-contrast a greyscale image to the exact levels |
| 2515 | % specified. Or by using specific levels for each channel of an image you |
| 2516 | % can convert a gray-scale image to any linear color gradient, according to |
| 2517 | % those levels. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2518 | % |
cristy | 50fbc38 | 2011-07-07 02:19:17 +0000 | [diff] [blame] | 2519 | % The format of the LevelizeImage method is: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2520 | % |
cristy | 50fbc38 | 2011-07-07 02:19:17 +0000 | [diff] [blame] | 2521 | % MagickBooleanType LevelizeImage(Image *image,const double black_point, |
cristy | 7c0a0a4 | 2011-08-23 17:57:25 +0000 | [diff] [blame] | 2522 | % const double white_point,const double gamma,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2523 | % |
| 2524 | % A description of each parameter follows: |
| 2525 | % |
| 2526 | % o image: the image. |
| 2527 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2528 | % o black_point: The level to map zero (black) to. |
| 2529 | % |
cristy | 01e9afd | 2011-08-10 17:38:41 +0000 | [diff] [blame] | 2530 | % o white_point: The level to map QuantumRange (white) to. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2531 | % |
| 2532 | % o gamma: adjust gamma by this factor before mapping values. |
| 2533 | % |
cristy | 7c0a0a4 | 2011-08-23 17:57:25 +0000 | [diff] [blame] | 2534 | % o exception: return any errors or warnings in this structure. |
| 2535 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2536 | */ |
cristy | d1a2c0f | 2011-02-09 14:14:50 +0000 | [diff] [blame] | 2537 | MagickExport MagickBooleanType LevelizeImage(Image *image, |
cristy | 7c0a0a4 | 2011-08-23 17:57:25 +0000 | [diff] [blame] | 2538 | const double black_point,const double white_point,const double gamma, |
| 2539 | ExceptionInfo *exception) |
cristy | d1a2c0f | 2011-02-09 14:14:50 +0000 | [diff] [blame] | 2540 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2541 | #define LevelizeImageTag "Levelize/Image" |
cristy | 00d4fee | 2013-04-23 18:35:34 +0000 | [diff] [blame] | 2542 | #define LevelizeValue(x) ClampToQuantum(((MagickRealType) gamma_pow((double) \ |
| 2543 | (QuantumScale*(x)),gamma))*(white_point-black_point)+black_point) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2544 | |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 2545 | CacheView |
| 2546 | *image_view; |
| 2547 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2548 | MagickBooleanType |
| 2549 | status; |
| 2550 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2551 | MagickOffsetType |
| 2552 | progress; |
| 2553 | |
| 2554 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2555 | i; |
| 2556 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2557 | ssize_t |
| 2558 | y; |
| 2559 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2560 | /* |
| 2561 | Allocate and initialize levels map. |
| 2562 | */ |
| 2563 | assert(image != (Image *) NULL); |
| 2564 | assert(image->signature == MagickSignature); |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 2565 | if( IfMagickTrue(image->debug) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2566 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 2567 | if (image->storage_class == PseudoClass) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2568 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2569 | { |
| 2570 | /* |
| 2571 | Level colormap. |
| 2572 | */ |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2573 | if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0) |
cristy | a6400b1 | 2013-03-15 12:20:18 +0000 | [diff] [blame] | 2574 | image->colormap[i].red=(double) LevelizeValue(image->colormap[i].red); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2575 | if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0) |
cristy | da1f9c1 | 2011-10-02 21:39:49 +0000 | [diff] [blame] | 2576 | image->colormap[i].green=(double) LevelizeValue( |
| 2577 | image->colormap[i].green); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2578 | if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0) |
cristy | a6400b1 | 2013-03-15 12:20:18 +0000 | [diff] [blame] | 2579 | image->colormap[i].blue=(double) LevelizeValue(image->colormap[i].blue); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2580 | if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) |
cristy | da1f9c1 | 2011-10-02 21:39:49 +0000 | [diff] [blame] | 2581 | image->colormap[i].alpha=(double) LevelizeValue( |
| 2582 | image->colormap[i].alpha); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2583 | } |
| 2584 | /* |
| 2585 | Level image. |
| 2586 | */ |
| 2587 | status=MagickTrue; |
| 2588 | progress=0; |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 2589 | image_view=AcquireAuthenticCacheView(image,exception); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 2590 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 2591 | #pragma omp parallel for schedule(static,4) shared(progress,status) \ |
cristy | 5e6b259 | 2012-12-19 14:08:11 +0000 | [diff] [blame] | 2592 | magick_threads(image,image,image->rows,1) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2593 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2594 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2595 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2596 | register Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 2597 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2598 | |
cristy | 8d4629b | 2010-08-30 17:59:46 +0000 | [diff] [blame] | 2599 | register ssize_t |
| 2600 | x; |
| 2601 | |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 2602 | if( IfMagickFalse(status) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2603 | continue; |
| 2604 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 2605 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2606 | { |
| 2607 | status=MagickFalse; |
| 2608 | continue; |
| 2609 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2610 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2611 | { |
cristy | 7c0a0a4 | 2011-08-23 17:57:25 +0000 | [diff] [blame] | 2612 | register ssize_t |
| 2613 | i; |
| 2614 | |
cristy | 883fde1 | 2013-04-08 00:50:13 +0000 | [diff] [blame] | 2615 | if (GetPixelReadMask(image,q) == 0) |
cristy | 10a6c61 | 2012-01-29 21:41:05 +0000 | [diff] [blame] | 2616 | { |
| 2617 | q+=GetPixelChannels(image); |
| 2618 | continue; |
| 2619 | } |
cristy | 7c0a0a4 | 2011-08-23 17:57:25 +0000 | [diff] [blame] | 2620 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
| 2621 | { |
cristy | 5a23c55 | 2013-02-13 14:34:28 +0000 | [diff] [blame] | 2622 | PixelChannel channel=GetPixelChannelChannel(image,i); |
| 2623 | PixelTrait traits=GetPixelChannelTraits(image,channel); |
cristy | d09f880 | 2012-02-04 16:44:10 +0000 | [diff] [blame] | 2624 | if ((traits & UpdatePixelTrait) == 0) |
cristy | b8b0d16 | 2011-12-18 23:41:28 +0000 | [diff] [blame] | 2625 | continue; |
cristy | 780e9ef | 2011-12-18 23:33:50 +0000 | [diff] [blame] | 2626 | q[i]=LevelizeValue(q[i]); |
cristy | 7c0a0a4 | 2011-08-23 17:57:25 +0000 | [diff] [blame] | 2627 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2628 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2629 | } |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 2630 | if( IfMagickFalse(SyncCacheViewAuthenticPixels(image_view,exception)) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2631 | status=MagickFalse; |
| 2632 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 2633 | { |
| 2634 | MagickBooleanType |
| 2635 | proceed; |
| 2636 | |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 2637 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 564a569 | 2012-01-20 23:56:26 +0000 | [diff] [blame] | 2638 | #pragma omp critical (MagickCore_LevelizeImage) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2639 | #endif |
| 2640 | proceed=SetImageProgress(image,LevelizeImageTag,progress++,image->rows); |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 2641 | if( IfMagickFalse(proceed) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2642 | status=MagickFalse; |
| 2643 | } |
| 2644 | } |
cristy | 8d4629b | 2010-08-30 17:59:46 +0000 | [diff] [blame] | 2645 | image_view=DestroyCacheView(image_view); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2646 | return(status); |
| 2647 | } |
| 2648 | |
| 2649 | /* |
| 2650 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2651 | % % |
| 2652 | % % |
| 2653 | % % |
| 2654 | % L e v e l I m a g e C o l o r s % |
| 2655 | % % |
| 2656 | % % |
| 2657 | % % |
| 2658 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2659 | % |
cristy | 490408a | 2011-07-07 14:42:05 +0000 | [diff] [blame] | 2660 | % LevelImageColors() maps the given color to "black" and "white" values, |
cristy | ee0f8d7 | 2009-09-19 00:58:29 +0000 | [diff] [blame] | 2661 | % linearly spreading out the colors, and level values on a channel by channel |
| 2662 | % bases, as per LevelImage(). The given colors allows you to specify |
glennrp | 1e7f7bc | 2011-03-02 19:25:28 +0000 | [diff] [blame] | 2663 | % different level ranges for each of the color channels separately. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2664 | % |
| 2665 | % If the boolean 'invert' is set true the image values will modifyed in the |
| 2666 | % reverse direction. That is any existing "black" and "white" colors in the |
| 2667 | % image will become the color values given, with all other values compressed |
| 2668 | % appropriatally. This effectivally maps a greyscale gradient into the given |
| 2669 | % color gradient. |
| 2670 | % |
cristy | 490408a | 2011-07-07 14:42:05 +0000 | [diff] [blame] | 2671 | % The format of the LevelImageColors method is: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2672 | % |
cristy | 490408a | 2011-07-07 14:42:05 +0000 | [diff] [blame] | 2673 | % MagickBooleanType LevelImageColors(Image *image, |
| 2674 | % const PixelInfo *black_color,const PixelInfo *white_color, |
cristy | 7c0a0a4 | 2011-08-23 17:57:25 +0000 | [diff] [blame] | 2675 | % const MagickBooleanType invert,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2676 | % |
| 2677 | % A description of each parameter follows: |
| 2678 | % |
| 2679 | % o image: the image. |
| 2680 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2681 | % o black_color: The color to map black to/from |
| 2682 | % |
| 2683 | % o white_point: The color to map white to/from |
| 2684 | % |
| 2685 | % o invert: if true map the colors (levelize), rather than from (level) |
| 2686 | % |
cristy | 7c0a0a4 | 2011-08-23 17:57:25 +0000 | [diff] [blame] | 2687 | % o exception: return any errors or warnings in this structure. |
| 2688 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2689 | */ |
cristy | 490408a | 2011-07-07 14:42:05 +0000 | [diff] [blame] | 2690 | MagickExport MagickBooleanType LevelImageColors(Image *image, |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2691 | const PixelInfo *black_color,const PixelInfo *white_color, |
cristy | 7c0a0a4 | 2011-08-23 17:57:25 +0000 | [diff] [blame] | 2692 | const MagickBooleanType invert,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2693 | { |
cristy | bd5a96c | 2011-08-21 00:04:26 +0000 | [diff] [blame] | 2694 | ChannelType |
| 2695 | channel_mask; |
| 2696 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2697 | MagickStatusType |
| 2698 | status; |
| 2699 | |
| 2700 | /* |
| 2701 | Allocate and initialize levels map. |
| 2702 | */ |
| 2703 | assert(image != (Image *) NULL); |
| 2704 | assert(image->signature == MagickSignature); |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 2705 | if( IfMagickTrue(image->debug) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2706 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 2707 | if( IfMagickTrue(IsGrayColorspace(image->colorspace)) && |
| 2708 | (IfMagickFalse(IsGrayColorspace(black_color->colorspace)) || |
| 2709 | IfMagickFalse(IsGrayColorspace(white_color->colorspace)))) |
cristy | 0c81d06 | 2013-04-21 15:22:02 +0000 | [diff] [blame] | 2710 | (void) SetImageColorspace(image,sRGBColorspace,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2711 | status=MagickFalse; |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 2712 | if( IfMagickFalse(invert) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2713 | { |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2714 | if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0) |
cristy | f89cb1d | 2011-07-07 01:24:37 +0000 | [diff] [blame] | 2715 | { |
cristy | cf1296e | 2012-08-26 23:40:49 +0000 | [diff] [blame] | 2716 | channel_mask=SetImageChannelMask(image,RedChannel); |
cristy | 01e9afd | 2011-08-10 17:38:41 +0000 | [diff] [blame] | 2717 | status|=LevelImage(image,black_color->red,white_color->red,1.0, |
cristy | 7c0a0a4 | 2011-08-23 17:57:25 +0000 | [diff] [blame] | 2718 | exception); |
cristy | cf1296e | 2012-08-26 23:40:49 +0000 | [diff] [blame] | 2719 | (void) SetImageChannelMask(image,channel_mask); |
cristy | f89cb1d | 2011-07-07 01:24:37 +0000 | [diff] [blame] | 2720 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2721 | if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0) |
cristy | f89cb1d | 2011-07-07 01:24:37 +0000 | [diff] [blame] | 2722 | { |
cristy | cf1296e | 2012-08-26 23:40:49 +0000 | [diff] [blame] | 2723 | channel_mask=SetImageChannelMask(image,GreenChannel); |
cristy | 01e9afd | 2011-08-10 17:38:41 +0000 | [diff] [blame] | 2724 | status|=LevelImage(image,black_color->green,white_color->green,1.0, |
cristy | 7c0a0a4 | 2011-08-23 17:57:25 +0000 | [diff] [blame] | 2725 | exception); |
cristy | cf1296e | 2012-08-26 23:40:49 +0000 | [diff] [blame] | 2726 | (void) SetImageChannelMask(image,channel_mask); |
cristy | f89cb1d | 2011-07-07 01:24:37 +0000 | [diff] [blame] | 2727 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2728 | if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0) |
cristy | f89cb1d | 2011-07-07 01:24:37 +0000 | [diff] [blame] | 2729 | { |
cristy | cf1296e | 2012-08-26 23:40:49 +0000 | [diff] [blame] | 2730 | channel_mask=SetImageChannelMask(image,BlueChannel); |
cristy | 01e9afd | 2011-08-10 17:38:41 +0000 | [diff] [blame] | 2731 | status|=LevelImage(image,black_color->blue,white_color->blue,1.0, |
cristy | 7c0a0a4 | 2011-08-23 17:57:25 +0000 | [diff] [blame] | 2732 | exception); |
cristy | cf1296e | 2012-08-26 23:40:49 +0000 | [diff] [blame] | 2733 | (void) SetImageChannelMask(image,channel_mask); |
cristy | f89cb1d | 2011-07-07 01:24:37 +0000 | [diff] [blame] | 2734 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2735 | if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) && |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2736 | (image->colorspace == CMYKColorspace)) |
cristy | f89cb1d | 2011-07-07 01:24:37 +0000 | [diff] [blame] | 2737 | { |
cristy | cf1296e | 2012-08-26 23:40:49 +0000 | [diff] [blame] | 2738 | channel_mask=SetImageChannelMask(image,BlackChannel); |
cristy | 01e9afd | 2011-08-10 17:38:41 +0000 | [diff] [blame] | 2739 | status|=LevelImage(image,black_color->black,white_color->black,1.0, |
cristy | 7c0a0a4 | 2011-08-23 17:57:25 +0000 | [diff] [blame] | 2740 | exception); |
cristy | cf1296e | 2012-08-26 23:40:49 +0000 | [diff] [blame] | 2741 | (void) SetImageChannelMask(image,channel_mask); |
cristy | f89cb1d | 2011-07-07 01:24:37 +0000 | [diff] [blame] | 2742 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2743 | if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) && |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 2744 | (image->alpha_trait == BlendPixelTrait)) |
cristy | f89cb1d | 2011-07-07 01:24:37 +0000 | [diff] [blame] | 2745 | { |
cristy | cf1296e | 2012-08-26 23:40:49 +0000 | [diff] [blame] | 2746 | channel_mask=SetImageChannelMask(image,AlphaChannel); |
cristy | 01e9afd | 2011-08-10 17:38:41 +0000 | [diff] [blame] | 2747 | status|=LevelImage(image,black_color->alpha,white_color->alpha,1.0, |
cristy | 7c0a0a4 | 2011-08-23 17:57:25 +0000 | [diff] [blame] | 2748 | exception); |
cristy | cf1296e | 2012-08-26 23:40:49 +0000 | [diff] [blame] | 2749 | (void) SetImageChannelMask(image,channel_mask); |
cristy | f89cb1d | 2011-07-07 01:24:37 +0000 | [diff] [blame] | 2750 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2751 | } |
| 2752 | else |
| 2753 | { |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2754 | if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0) |
cristy | f89cb1d | 2011-07-07 01:24:37 +0000 | [diff] [blame] | 2755 | { |
cristy | cf1296e | 2012-08-26 23:40:49 +0000 | [diff] [blame] | 2756 | channel_mask=SetImageChannelMask(image,RedChannel); |
cristy | 7c0a0a4 | 2011-08-23 17:57:25 +0000 | [diff] [blame] | 2757 | status|=LevelizeImage(image,black_color->red,white_color->red,1.0, |
| 2758 | exception); |
cristy | cf1296e | 2012-08-26 23:40:49 +0000 | [diff] [blame] | 2759 | (void) SetImageChannelMask(image,channel_mask); |
cristy | f89cb1d | 2011-07-07 01:24:37 +0000 | [diff] [blame] | 2760 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2761 | if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0) |
cristy | f89cb1d | 2011-07-07 01:24:37 +0000 | [diff] [blame] | 2762 | { |
cristy | cf1296e | 2012-08-26 23:40:49 +0000 | [diff] [blame] | 2763 | channel_mask=SetImageChannelMask(image,GreenChannel); |
cristy | 7c0a0a4 | 2011-08-23 17:57:25 +0000 | [diff] [blame] | 2764 | status|=LevelizeImage(image,black_color->green,white_color->green,1.0, |
| 2765 | exception); |
cristy | cf1296e | 2012-08-26 23:40:49 +0000 | [diff] [blame] | 2766 | (void) SetImageChannelMask(image,channel_mask); |
cristy | f89cb1d | 2011-07-07 01:24:37 +0000 | [diff] [blame] | 2767 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2768 | if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0) |
cristy | f89cb1d | 2011-07-07 01:24:37 +0000 | [diff] [blame] | 2769 | { |
cristy | cf1296e | 2012-08-26 23:40:49 +0000 | [diff] [blame] | 2770 | channel_mask=SetImageChannelMask(image,BlueChannel); |
cristy | 7c0a0a4 | 2011-08-23 17:57:25 +0000 | [diff] [blame] | 2771 | status|=LevelizeImage(image,black_color->blue,white_color->blue,1.0, |
| 2772 | exception); |
cristy | cf1296e | 2012-08-26 23:40:49 +0000 | [diff] [blame] | 2773 | (void) SetImageChannelMask(image,channel_mask); |
cristy | f89cb1d | 2011-07-07 01:24:37 +0000 | [diff] [blame] | 2774 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2775 | if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) && |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2776 | (image->colorspace == CMYKColorspace)) |
cristy | f89cb1d | 2011-07-07 01:24:37 +0000 | [diff] [blame] | 2777 | { |
cristy | cf1296e | 2012-08-26 23:40:49 +0000 | [diff] [blame] | 2778 | channel_mask=SetImageChannelMask(image,BlackChannel); |
cristy | 7c0a0a4 | 2011-08-23 17:57:25 +0000 | [diff] [blame] | 2779 | status|=LevelizeImage(image,black_color->black,white_color->black,1.0, |
| 2780 | exception); |
cristy | cf1296e | 2012-08-26 23:40:49 +0000 | [diff] [blame] | 2781 | (void) SetImageChannelMask(image,channel_mask); |
cristy | f89cb1d | 2011-07-07 01:24:37 +0000 | [diff] [blame] | 2782 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2783 | if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) && |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 2784 | (image->alpha_trait == BlendPixelTrait)) |
cristy | f89cb1d | 2011-07-07 01:24:37 +0000 | [diff] [blame] | 2785 | { |
cristy | cf1296e | 2012-08-26 23:40:49 +0000 | [diff] [blame] | 2786 | channel_mask=SetImageChannelMask(image,AlphaChannel); |
cristy | 7c0a0a4 | 2011-08-23 17:57:25 +0000 | [diff] [blame] | 2787 | status|=LevelizeImage(image,black_color->alpha,white_color->alpha,1.0, |
| 2788 | exception); |
cristy | cf1296e | 2012-08-26 23:40:49 +0000 | [diff] [blame] | 2789 | (void) SetImageChannelMask(image,channel_mask); |
cristy | f89cb1d | 2011-07-07 01:24:37 +0000 | [diff] [blame] | 2790 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2791 | } |
cristy | ee1bdaa | 2013-07-16 16:45:03 +0000 | [diff] [blame] | 2792 | return(status != 0 ? MagickTrue : MagickFalse); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2793 | } |
| 2794 | |
| 2795 | /* |
| 2796 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2797 | % % |
| 2798 | % % |
| 2799 | % % |
| 2800 | % L i n e a r S t r e t c h I m a g e % |
| 2801 | % % |
| 2802 | % % |
| 2803 | % % |
| 2804 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2805 | % |
cristy | f161178 | 2011-08-01 15:39:13 +0000 | [diff] [blame] | 2806 | % LinearStretchImage() discards any pixels below the black point and above |
| 2807 | % the white point and levels the remaining pixels. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2808 | % |
| 2809 | % The format of the LinearStretchImage method is: |
| 2810 | % |
| 2811 | % MagickBooleanType LinearStretchImage(Image *image, |
cristy | 33bd515 | 2011-08-24 01:42:24 +0000 | [diff] [blame] | 2812 | % const double black_point,const double white_point, |
| 2813 | % ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2814 | % |
| 2815 | % A description of each parameter follows: |
| 2816 | % |
| 2817 | % o image: the image. |
| 2818 | % |
| 2819 | % o black_point: the black point. |
| 2820 | % |
| 2821 | % o white_point: the white point. |
| 2822 | % |
cristy | 33bd515 | 2011-08-24 01:42:24 +0000 | [diff] [blame] | 2823 | % o exception: return any errors or warnings in this structure. |
| 2824 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2825 | */ |
| 2826 | MagickExport MagickBooleanType LinearStretchImage(Image *image, |
cristy | 33bd515 | 2011-08-24 01:42:24 +0000 | [diff] [blame] | 2827 | const double black_point,const double white_point,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2828 | { |
| 2829 | #define LinearStretchImageTag "LinearStretch/Image" |
| 2830 | |
cristy | 33bd515 | 2011-08-24 01:42:24 +0000 | [diff] [blame] | 2831 | CacheView |
| 2832 | *image_view; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2833 | |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 2834 | double |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2835 | *histogram, |
| 2836 | intensity; |
| 2837 | |
cristy | 1c2f48d | 2012-12-14 01:20:55 +0000 | [diff] [blame] | 2838 | MagickBooleanType |
| 2839 | status; |
| 2840 | |
cristy | 8d4629b | 2010-08-30 17:59:46 +0000 | [diff] [blame] | 2841 | ssize_t |
| 2842 | black, |
| 2843 | white, |
| 2844 | y; |
| 2845 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2846 | /* |
| 2847 | Allocate histogram and linear map. |
| 2848 | */ |
| 2849 | assert(image != (Image *) NULL); |
| 2850 | assert(image->signature == MagickSignature); |
cristy | 1c2f48d | 2012-12-14 01:20:55 +0000 | [diff] [blame] | 2851 | histogram=(double *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*histogram)); |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 2852 | if (histogram == (double *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2853 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
| 2854 | image->filename); |
| 2855 | /* |
| 2856 | Form histogram. |
| 2857 | */ |
| 2858 | (void) ResetMagickMemory(histogram,0,(MaxMap+1)*sizeof(*histogram)); |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 2859 | image_view=AcquireVirtualCacheView(image,exception); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2860 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2861 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2862 | register const Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 2863 | *restrict p; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2864 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2865 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2866 | x; |
| 2867 | |
cristy | 33bd515 | 2011-08-24 01:42:24 +0000 | [diff] [blame] | 2868 | p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2869 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2870 | break; |
cristy | 33bd515 | 2011-08-24 01:42:24 +0000 | [diff] [blame] | 2871 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2872 | { |
cristy | f13c594 | 2012-08-08 23:50:11 +0000 | [diff] [blame] | 2873 | double |
| 2874 | intensity; |
| 2875 | |
| 2876 | intensity=GetPixelIntensity(image,p); |
| 2877 | histogram[ScaleQuantumToMap(ClampToQuantum(intensity))]++; |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2878 | p+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2879 | } |
| 2880 | } |
cristy | 33bd515 | 2011-08-24 01:42:24 +0000 | [diff] [blame] | 2881 | image_view=DestroyCacheView(image_view); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2882 | /* |
| 2883 | Find the histogram boundaries by locating the black and white point levels. |
| 2884 | */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2885 | intensity=0.0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2886 | for (black=0; black < (ssize_t) MaxMap; black++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2887 | { |
| 2888 | intensity+=histogram[black]; |
| 2889 | if (intensity >= black_point) |
| 2890 | break; |
| 2891 | } |
| 2892 | intensity=0.0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2893 | for (white=(ssize_t) MaxMap; white != 0; white--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2894 | { |
| 2895 | intensity+=histogram[white]; |
| 2896 | if (intensity >= white_point) |
| 2897 | break; |
| 2898 | } |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 2899 | histogram=(double *) RelinquishMagickMemory(histogram); |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 2900 | status=LevelImage(image,(double) black,(double) white,1.0,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2901 | return(status); |
| 2902 | } |
| 2903 | |
| 2904 | /* |
| 2905 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2906 | % % |
| 2907 | % % |
| 2908 | % % |
| 2909 | % M o d u l a t e I m a g e % |
| 2910 | % % |
| 2911 | % % |
| 2912 | % % |
| 2913 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2914 | % |
| 2915 | % ModulateImage() lets you control the brightness, saturation, and hue |
| 2916 | % of an image. Modulate represents the brightness, saturation, and hue |
| 2917 | % as one parameter (e.g. 90,150,100). If the image colorspace is HSL, the |
cristy | e83f340 | 2012-08-17 13:12:15 +0000 | [diff] [blame] | 2918 | % modulation is lightness, saturation, and hue. For HWB, use blackness, |
| 2919 | % whiteness, and hue. And for HCL, use chrome, luma, and hue. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2920 | % |
| 2921 | % The format of the ModulateImage method is: |
| 2922 | % |
cristy | 33bd515 | 2011-08-24 01:42:24 +0000 | [diff] [blame] | 2923 | % MagickBooleanType ModulateImage(Image *image,const char *modulate, |
| 2924 | % ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2925 | % |
| 2926 | % A description of each parameter follows: |
| 2927 | % |
| 2928 | % o image: the image. |
| 2929 | % |
cristy | 33bd515 | 2011-08-24 01:42:24 +0000 | [diff] [blame] | 2930 | % o modulate: Define the percent change in brightness, saturation, and hue. |
| 2931 | % |
| 2932 | % o exception: return any errors or warnings in this structure. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2933 | % |
| 2934 | */ |
| 2935 | |
cristy | e83f340 | 2012-08-17 13:12:15 +0000 | [diff] [blame] | 2936 | static inline void ModulateHCL(const double percent_hue, |
| 2937 | const double percent_chroma,const double percent_luma,double *red, |
| 2938 | double *green,double *blue) |
| 2939 | { |
| 2940 | double |
| 2941 | hue, |
| 2942 | luma, |
| 2943 | chroma; |
| 2944 | |
| 2945 | /* |
| 2946 | Increase or decrease color luma, chroma, or hue. |
| 2947 | */ |
| 2948 | ConvertRGBToHCL(*red,*green,*blue,&hue,&chroma,&luma); |
| 2949 | hue+=0.5*(0.01*percent_hue-1.0); |
| 2950 | while (hue < 0.0) |
| 2951 | hue+=1.0; |
| 2952 | while (hue > 1.0) |
| 2953 | hue-=1.0; |
| 2954 | chroma*=0.01*percent_chroma; |
| 2955 | luma*=0.01*percent_luma; |
| 2956 | ConvertHCLToRGB(hue,chroma,luma,red,green,blue); |
| 2957 | } |
| 2958 | |
cristy | 9e2436a | 2013-05-02 20:35:59 +0000 | [diff] [blame] | 2959 | static inline void ModulateHCLp(const double percent_hue, |
| 2960 | const double percent_chroma,const double percent_luma,double *red, |
| 2961 | double *green,double *blue) |
| 2962 | { |
| 2963 | double |
| 2964 | hue, |
| 2965 | luma, |
| 2966 | chroma; |
| 2967 | |
| 2968 | /* |
| 2969 | Increase or decrease color luma, chroma, or hue. |
| 2970 | */ |
| 2971 | ConvertRGBToHCLp(*red,*green,*blue,&hue,&chroma,&luma); |
| 2972 | hue+=0.5*(0.01*percent_hue-1.0); |
| 2973 | while (hue < 0.0) |
| 2974 | hue+=1.0; |
| 2975 | while (hue > 1.0) |
| 2976 | hue-=1.0; |
| 2977 | chroma*=0.01*percent_chroma; |
| 2978 | luma*=0.01*percent_luma; |
| 2979 | ConvertHCLpToRGB(hue,chroma,luma,red,green,blue); |
| 2980 | } |
| 2981 | |
cristy | e83f340 | 2012-08-17 13:12:15 +0000 | [diff] [blame] | 2982 | static inline void ModulateHSB(const double percent_hue, |
cristy | 3094b7f | 2011-10-01 23:18:02 +0000 | [diff] [blame] | 2983 | const double percent_saturation,const double percent_brightness,double *red, |
| 2984 | double *green,double *blue) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2985 | { |
| 2986 | double |
| 2987 | brightness, |
| 2988 | hue, |
| 2989 | saturation; |
| 2990 | |
| 2991 | /* |
| 2992 | Increase or decrease color brightness, saturation, or hue. |
| 2993 | */ |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 2994 | ConvertRGBToHSB(*red,*green,*blue,&hue,&saturation,&brightness); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2995 | hue+=0.5*(0.01*percent_hue-1.0); |
| 2996 | while (hue < 0.0) |
| 2997 | hue+=1.0; |
| 2998 | while (hue > 1.0) |
| 2999 | hue-=1.0; |
| 3000 | saturation*=0.01*percent_saturation; |
| 3001 | brightness*=0.01*percent_brightness; |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 3002 | ConvertHSBToRGB(hue,saturation,brightness,red,green,blue); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3003 | } |
| 3004 | |
cristy | af64eb2 | 2013-05-02 14:07:10 +0000 | [diff] [blame] | 3005 | static inline void ModulateHSI(const double percent_hue, |
| 3006 | const double percent_saturation,const double percent_intensity,double *red, |
| 3007 | double *green,double *blue) |
| 3008 | { |
| 3009 | double |
| 3010 | intensity, |
| 3011 | hue, |
| 3012 | saturation; |
| 3013 | |
| 3014 | /* |
| 3015 | Increase or decrease color intensity, saturation, or hue. |
| 3016 | */ |
| 3017 | ConvertRGBToHSI(*red,*green,*blue,&hue,&saturation,&intensity); |
| 3018 | hue+=0.5*(0.01*percent_hue-1.0); |
| 3019 | while (hue < 0.0) |
| 3020 | hue+=1.0; |
| 3021 | while (hue > 1.0) |
| 3022 | hue-=1.0; |
| 3023 | saturation*=0.01*percent_saturation; |
| 3024 | intensity*=0.01*percent_intensity; |
| 3025 | ConvertHSIToRGB(hue,saturation,intensity,red,green,blue); |
| 3026 | } |
| 3027 | |
cristy | e83f340 | 2012-08-17 13:12:15 +0000 | [diff] [blame] | 3028 | static inline void ModulateHSL(const double percent_hue, |
cristy | 3094b7f | 2011-10-01 23:18:02 +0000 | [diff] [blame] | 3029 | const double percent_saturation,const double percent_lightness,double *red, |
| 3030 | double *green,double *blue) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3031 | { |
| 3032 | double |
| 3033 | hue, |
| 3034 | lightness, |
| 3035 | saturation; |
| 3036 | |
| 3037 | /* |
| 3038 | Increase or decrease color lightness, saturation, or hue. |
| 3039 | */ |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 3040 | ConvertRGBToHSL(*red,*green,*blue,&hue,&saturation,&lightness); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3041 | hue+=0.5*(0.01*percent_hue-1.0); |
| 3042 | while (hue < 0.0) |
| 3043 | hue+=1.0; |
cristy | e8f91f7 | 2013-04-05 00:50:58 +0000 | [diff] [blame] | 3044 | while (hue >= 1.0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3045 | hue-=1.0; |
| 3046 | saturation*=0.01*percent_saturation; |
| 3047 | lightness*=0.01*percent_lightness; |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 3048 | ConvertHSLToRGB(hue,saturation,lightness,red,green,blue); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3049 | } |
| 3050 | |
cristy | 246c313 | 2013-05-02 16:35:53 +0000 | [diff] [blame] | 3051 | static inline void ModulateHSV(const double percent_hue, |
| 3052 | const double percent_saturation,const double percent_value,double *red, |
| 3053 | double *green,double *blue) |
| 3054 | { |
| 3055 | double |
| 3056 | hue, |
| 3057 | saturation, |
| 3058 | value; |
| 3059 | |
| 3060 | /* |
| 3061 | Increase or decrease color value, saturation, or hue. |
| 3062 | */ |
| 3063 | ConvertRGBToHSV(*red,*green,*blue,&hue,&saturation,&value); |
| 3064 | hue+=0.5*(0.01*percent_hue-1.0); |
| 3065 | while (hue < 0.0) |
| 3066 | hue+=1.0; |
| 3067 | while (hue >= 1.0) |
| 3068 | hue-=1.0; |
| 3069 | saturation*=0.01*percent_saturation; |
| 3070 | value*=0.01*percent_value; |
| 3071 | ConvertHSVToRGB(hue,saturation,value,red,green,blue); |
| 3072 | } |
| 3073 | |
cristy | e83f340 | 2012-08-17 13:12:15 +0000 | [diff] [blame] | 3074 | static inline void ModulateHWB(const double percent_hue, |
| 3075 | const double percent_whiteness,const double percent_blackness,double *red, |
| 3076 | double *green,double *blue) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3077 | { |
| 3078 | double |
| 3079 | blackness, |
| 3080 | hue, |
| 3081 | whiteness; |
| 3082 | |
| 3083 | /* |
| 3084 | Increase or decrease color blackness, whiteness, or hue. |
| 3085 | */ |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 3086 | ConvertRGBToHWB(*red,*green,*blue,&hue,&whiteness,&blackness); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3087 | hue+=0.5*(0.01*percent_hue-1.0); |
| 3088 | while (hue < 0.0) |
| 3089 | hue+=1.0; |
cristy | e8f91f7 | 2013-04-05 00:50:58 +0000 | [diff] [blame] | 3090 | while (hue >= 1.0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3091 | hue-=1.0; |
| 3092 | blackness*=0.01*percent_blackness; |
| 3093 | whiteness*=0.01*percent_whiteness; |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 3094 | ConvertHWBToRGB(hue,whiteness,blackness,red,green,blue); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3095 | } |
| 3096 | |
cristy | 28e21a7 | 2013-04-06 00:24:19 +0000 | [diff] [blame] | 3097 | static inline void ModulateLCHab(const double percent_luma, |
| 3098 | const double percent_chroma,const double percent_hue,double *red, |
| 3099 | double *green,double *blue) |
| 3100 | { |
| 3101 | double |
| 3102 | hue, |
| 3103 | luma, |
| 3104 | chroma; |
| 3105 | |
| 3106 | /* |
| 3107 | Increase or decrease color luma, chroma, or hue. |
| 3108 | */ |
| 3109 | ConvertRGBToLCHab(*red,*green,*blue,&luma,&chroma,&hue); |
| 3110 | luma*=0.01*percent_luma; |
| 3111 | chroma*=0.01*percent_chroma; |
| 3112 | hue+=0.5*(0.01*percent_hue-1.0); |
| 3113 | while (hue < 0.0) |
| 3114 | hue+=1.0; |
| 3115 | while (hue >= 1.0) |
| 3116 | hue-=1.0; |
| 3117 | ConvertLCHabToRGB(luma,chroma,hue,red,green,blue); |
| 3118 | } |
| 3119 | |
| 3120 | static inline void ModulateLCHuv(const double percent_luma, |
cristy | b285095 | 2013-04-04 19:00:52 +0000 | [diff] [blame] | 3121 | const double percent_chroma,const double percent_hue,double *red, |
| 3122 | double *green,double *blue) |
| 3123 | { |
| 3124 | double |
| 3125 | hue, |
| 3126 | luma, |
| 3127 | chroma; |
| 3128 | |
| 3129 | /* |
| 3130 | Increase or decrease color luma, chroma, or hue. |
| 3131 | */ |
cristy | 30ad51e | 2013-04-05 16:39:20 +0000 | [diff] [blame] | 3132 | ConvertRGBToLCHuv(*red,*green,*blue,&luma,&chroma,&hue); |
cristy | b285095 | 2013-04-04 19:00:52 +0000 | [diff] [blame] | 3133 | luma*=0.01*percent_luma; |
| 3134 | chroma*=0.01*percent_chroma; |
| 3135 | hue+=0.5*(0.01*percent_hue-1.0); |
| 3136 | while (hue < 0.0) |
| 3137 | hue+=1.0; |
cristy | e8f91f7 | 2013-04-05 00:50:58 +0000 | [diff] [blame] | 3138 | while (hue >= 1.0) |
cristy | b285095 | 2013-04-04 19:00:52 +0000 | [diff] [blame] | 3139 | hue-=1.0; |
cristy | 30ad51e | 2013-04-05 16:39:20 +0000 | [diff] [blame] | 3140 | ConvertLCHuvToRGB(luma,chroma,hue,red,green,blue); |
cristy | b285095 | 2013-04-04 19:00:52 +0000 | [diff] [blame] | 3141 | } |
| 3142 | |
cristy | 33bd515 | 2011-08-24 01:42:24 +0000 | [diff] [blame] | 3143 | MagickExport MagickBooleanType ModulateImage(Image *image,const char *modulate, |
| 3144 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3145 | { |
| 3146 | #define ModulateImageTag "Modulate/Image" |
| 3147 | |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 3148 | CacheView |
| 3149 | *image_view; |
| 3150 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3151 | ColorspaceType |
| 3152 | colorspace; |
| 3153 | |
| 3154 | const char |
| 3155 | *artifact; |
| 3156 | |
| 3157 | double |
| 3158 | percent_brightness, |
| 3159 | percent_hue, |
cristy | 4d41221 | 2012-06-27 23:14:48 +0000 | [diff] [blame] | 3160 | percent_saturation; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3161 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3162 | GeometryInfo |
| 3163 | geometry_info; |
| 3164 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3165 | MagickBooleanType |
| 3166 | status; |
| 3167 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3168 | MagickOffsetType |
| 3169 | progress; |
| 3170 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3171 | MagickStatusType |
| 3172 | flags; |
| 3173 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3174 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3175 | i; |
| 3176 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3177 | ssize_t |
| 3178 | y; |
| 3179 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3180 | /* |
cristy | 2b726bd | 2010-01-11 01:05:39 +0000 | [diff] [blame] | 3181 | Initialize modulate table. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3182 | */ |
| 3183 | assert(image != (Image *) NULL); |
| 3184 | assert(image->signature == MagickSignature); |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 3185 | if( IfMagickTrue(image->debug) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3186 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 3187 | if (modulate == (char *) NULL) |
| 3188 | return(MagickFalse); |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 3189 | if( IfMagickFalse(IssRGBCompatibleColorspace(image->colorspace)) ) |
cristy | 0c81d06 | 2013-04-21 15:22:02 +0000 | [diff] [blame] | 3190 | (void) SetImageColorspace(image,sRGBColorspace,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3191 | flags=ParseGeometry(modulate,&geometry_info); |
| 3192 | percent_brightness=geometry_info.rho; |
| 3193 | percent_saturation=geometry_info.sigma; |
| 3194 | if ((flags & SigmaValue) == 0) |
| 3195 | percent_saturation=100.0; |
| 3196 | percent_hue=geometry_info.xi; |
| 3197 | if ((flags & XiValue) == 0) |
| 3198 | percent_hue=100.0; |
| 3199 | colorspace=UndefinedColorspace; |
| 3200 | artifact=GetImageArtifact(image,"modulate:colorspace"); |
| 3201 | if (artifact != (const char *) NULL) |
cristy | 042ee78 | 2011-04-22 18:48:30 +0000 | [diff] [blame] | 3202 | colorspace=(ColorspaceType) ParseCommandOption(MagickColorspaceOptions, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3203 | MagickFalse,artifact); |
| 3204 | if (image->storage_class == PseudoClass) |
nicolas | e0aa3c3 | 2012-08-08 14:57:02 +0000 | [diff] [blame] | 3205 | for (i=0; i < (ssize_t) image->colors; i++) |
nicolas | fd74ae3 | 2012-08-08 16:00:11 +0000 | [diff] [blame] | 3206 | { |
| 3207 | double |
| 3208 | blue, |
| 3209 | green, |
| 3210 | red; |
cristy | 4d41221 | 2012-06-27 23:14:48 +0000 | [diff] [blame] | 3211 | |
nicolas | fd74ae3 | 2012-08-08 16:00:11 +0000 | [diff] [blame] | 3212 | /* |
cristy | 9013d23 | 2013-04-04 00:48:36 +0000 | [diff] [blame] | 3213 | Modulate image colormap. |
nicolas | fd74ae3 | 2012-08-08 16:00:11 +0000 | [diff] [blame] | 3214 | */ |
cristy | cb99efe | 2013-04-04 10:20:48 +0000 | [diff] [blame] | 3215 | red=(double) image->colormap[i].red; |
| 3216 | green=(double) image->colormap[i].green; |
| 3217 | blue=(double) image->colormap[i].blue; |
nicolas | fd74ae3 | 2012-08-08 16:00:11 +0000 | [diff] [blame] | 3218 | switch (colorspace) |
| 3219 | { |
cristy | e83f340 | 2012-08-17 13:12:15 +0000 | [diff] [blame] | 3220 | case HCLColorspace: |
| 3221 | { |
| 3222 | ModulateHCL(percent_hue,percent_saturation,percent_brightness, |
| 3223 | &red,&green,&blue); |
| 3224 | break; |
| 3225 | } |
cristy | 9e2436a | 2013-05-02 20:35:59 +0000 | [diff] [blame] | 3226 | case HCLpColorspace: |
| 3227 | { |
| 3228 | ModulateHCLp(percent_hue,percent_saturation,percent_brightness, |
| 3229 | &red,&green,&blue); |
| 3230 | break; |
| 3231 | } |
nicolas | fd74ae3 | 2012-08-08 16:00:11 +0000 | [diff] [blame] | 3232 | case HSBColorspace: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3233 | { |
nicolas | fd74ae3 | 2012-08-08 16:00:11 +0000 | [diff] [blame] | 3234 | ModulateHSB(percent_hue,percent_saturation,percent_brightness, |
| 3235 | &red,&green,&blue); |
| 3236 | break; |
| 3237 | } |
cristy | af64eb2 | 2013-05-02 14:07:10 +0000 | [diff] [blame] | 3238 | case HSIColorspace: |
| 3239 | { |
| 3240 | ModulateHSI(percent_hue,percent_saturation,percent_brightness, |
| 3241 | &red,&green,&blue); |
| 3242 | break; |
| 3243 | } |
nicolas | fd74ae3 | 2012-08-08 16:00:11 +0000 | [diff] [blame] | 3244 | case HSLColorspace: |
| 3245 | default: |
| 3246 | { |
| 3247 | ModulateHSL(percent_hue,percent_saturation,percent_brightness, |
| 3248 | &red,&green,&blue); |
| 3249 | break; |
| 3250 | } |
cristy | 246c313 | 2013-05-02 16:35:53 +0000 | [diff] [blame] | 3251 | case HSVColorspace: |
| 3252 | { |
| 3253 | ModulateHSV(percent_hue,percent_saturation,percent_brightness, |
| 3254 | &red,&green,&blue); |
| 3255 | break; |
| 3256 | } |
nicolas | fd74ae3 | 2012-08-08 16:00:11 +0000 | [diff] [blame] | 3257 | case HWBColorspace: |
| 3258 | { |
| 3259 | ModulateHWB(percent_hue,percent_saturation,percent_brightness, |
| 3260 | &red,&green,&blue); |
| 3261 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3262 | } |
cristy | 8369dae | 2013-05-06 20:26:38 +0000 | [diff] [blame] | 3263 | case LCHColorspace: |
cristy | 28e21a7 | 2013-04-06 00:24:19 +0000 | [diff] [blame] | 3264 | case LCHabColorspace: |
| 3265 | { |
| 3266 | ModulateLCHab(percent_brightness,percent_saturation,percent_hue, |
| 3267 | &red,&green,&blue); |
| 3268 | break; |
| 3269 | } |
cristy | 30ad51e | 2013-04-05 16:39:20 +0000 | [diff] [blame] | 3270 | case LCHuvColorspace: |
cristy | b285095 | 2013-04-04 19:00:52 +0000 | [diff] [blame] | 3271 | { |
cristy | 28e21a7 | 2013-04-06 00:24:19 +0000 | [diff] [blame] | 3272 | ModulateLCHuv(percent_brightness,percent_saturation,percent_hue, |
cristy | b285095 | 2013-04-04 19:00:52 +0000 | [diff] [blame] | 3273 | &red,&green,&blue); |
| 3274 | break; |
| 3275 | } |
cristy | 0a39a5c | 2012-06-27 12:51:45 +0000 | [diff] [blame] | 3276 | } |
cristy | cb99efe | 2013-04-04 10:20:48 +0000 | [diff] [blame] | 3277 | image->colormap[i].red=red; |
| 3278 | image->colormap[i].green=green; |
| 3279 | image->colormap[i].blue=blue; |
nicolas | fd74ae3 | 2012-08-08 16:00:11 +0000 | [diff] [blame] | 3280 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3281 | /* |
| 3282 | Modulate image. |
| 3283 | */ |
| 3284 | status=MagickTrue; |
| 3285 | progress=0; |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 3286 | image_view=AcquireAuthenticCacheView(image,exception); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 3287 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 3288 | #pragma omp parallel for schedule(static,4) shared(progress,status) \ |
cristy | 5e6b259 | 2012-12-19 14:08:11 +0000 | [diff] [blame] | 3289 | magick_threads(image,image,image->rows,1) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3290 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3291 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3292 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 3293 | register Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 3294 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3295 | |
cristy | 8d4629b | 2010-08-30 17:59:46 +0000 | [diff] [blame] | 3296 | register ssize_t |
| 3297 | x; |
| 3298 | |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 3299 | if( IfMagickFalse(status) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3300 | continue; |
| 3301 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 3302 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3303 | { |
| 3304 | status=MagickFalse; |
| 3305 | continue; |
| 3306 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3307 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3308 | { |
cristy | 4d41221 | 2012-06-27 23:14:48 +0000 | [diff] [blame] | 3309 | double |
| 3310 | blue, |
| 3311 | green, |
| 3312 | red; |
| 3313 | |
cristy | cb99efe | 2013-04-04 10:20:48 +0000 | [diff] [blame] | 3314 | red=(double) GetPixelRed(image,q); |
| 3315 | green=(double) GetPixelGreen(image,q); |
| 3316 | blue=(double) GetPixelBlue(image,q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3317 | switch (colorspace) |
| 3318 | { |
cristy | ab3c327 | 2012-08-17 17:21:33 +0000 | [diff] [blame] | 3319 | case HCLColorspace: |
| 3320 | { |
| 3321 | ModulateHCL(percent_hue,percent_saturation,percent_brightness, |
| 3322 | &red,&green,&blue); |
| 3323 | break; |
| 3324 | } |
cristy | 690a0eb | 2013-07-24 16:43:22 +0000 | [diff] [blame] | 3325 | case HCLpColorspace: |
| 3326 | { |
| 3327 | ModulateHCLp(percent_hue,percent_saturation,percent_brightness, |
| 3328 | &red,&green,&blue); |
| 3329 | break; |
| 3330 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3331 | case HSBColorspace: |
| 3332 | { |
| 3333 | ModulateHSB(percent_hue,percent_saturation,percent_brightness, |
cristy | 5afeab8 | 2011-04-30 01:30:09 +0000 | [diff] [blame] | 3334 | &red,&green,&blue); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3335 | break; |
| 3336 | } |
| 3337 | case HSLColorspace: |
| 3338 | default: |
| 3339 | { |
| 3340 | ModulateHSL(percent_hue,percent_saturation,percent_brightness, |
cristy | 5afeab8 | 2011-04-30 01:30:09 +0000 | [diff] [blame] | 3341 | &red,&green,&blue); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3342 | break; |
| 3343 | } |
cristy | 5bf65a4 | 2013-06-24 14:49:51 +0000 | [diff] [blame] | 3344 | case HSVColorspace: |
| 3345 | { |
| 3346 | ModulateHSV(percent_hue,percent_saturation,percent_brightness, |
| 3347 | &red,&green,&blue); |
| 3348 | break; |
| 3349 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3350 | case HWBColorspace: |
| 3351 | { |
| 3352 | ModulateHWB(percent_hue,percent_saturation,percent_brightness, |
cristy | 5afeab8 | 2011-04-30 01:30:09 +0000 | [diff] [blame] | 3353 | &red,&green,&blue); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3354 | break; |
| 3355 | } |
cristy | 28e21a7 | 2013-04-06 00:24:19 +0000 | [diff] [blame] | 3356 | case LCHabColorspace: |
| 3357 | { |
| 3358 | ModulateLCHab(percent_brightness,percent_saturation,percent_hue, |
| 3359 | &red,&green,&blue); |
| 3360 | break; |
| 3361 | } |
cristy | b285095 | 2013-04-04 19:00:52 +0000 | [diff] [blame] | 3362 | case LCHColorspace: |
cristy | 30ad51e | 2013-04-05 16:39:20 +0000 | [diff] [blame] | 3363 | case LCHuvColorspace: |
cristy | b285095 | 2013-04-04 19:00:52 +0000 | [diff] [blame] | 3364 | { |
cristy | 28e21a7 | 2013-04-06 00:24:19 +0000 | [diff] [blame] | 3365 | ModulateLCHuv(percent_brightness,percent_saturation,percent_hue, |
cristy | b285095 | 2013-04-04 19:00:52 +0000 | [diff] [blame] | 3366 | &red,&green,&blue); |
| 3367 | break; |
| 3368 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3369 | } |
cristy | cb99efe | 2013-04-04 10:20:48 +0000 | [diff] [blame] | 3370 | SetPixelRed(image,ClampToQuantum(red),q); |
| 3371 | SetPixelGreen(image,ClampToQuantum(green),q); |
| 3372 | SetPixelBlue(image,ClampToQuantum(blue),q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 3373 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3374 | } |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 3375 | if( IfMagickFalse(SyncCacheViewAuthenticPixels(image_view,exception)) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3376 | status=MagickFalse; |
| 3377 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 3378 | { |
| 3379 | MagickBooleanType |
| 3380 | proceed; |
| 3381 | |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 3382 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 564a569 | 2012-01-20 23:56:26 +0000 | [diff] [blame] | 3383 | #pragma omp critical (MagickCore_ModulateImage) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3384 | #endif |
| 3385 | proceed=SetImageProgress(image,ModulateImageTag,progress++,image->rows); |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 3386 | if( IfMagickFalse(proceed) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3387 | status=MagickFalse; |
| 3388 | } |
| 3389 | } |
| 3390 | image_view=DestroyCacheView(image_view); |
| 3391 | return(status); |
| 3392 | } |
| 3393 | |
| 3394 | /* |
| 3395 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3396 | % % |
| 3397 | % % |
| 3398 | % % |
| 3399 | % N e g a t e I m a g e % |
| 3400 | % % |
| 3401 | % % |
| 3402 | % % |
| 3403 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3404 | % |
| 3405 | % NegateImage() negates the colors in the reference image. The grayscale |
| 3406 | % option means that only grayscale values within the image are negated. |
| 3407 | % |
cristy | 50fbc38 | 2011-07-07 02:19:17 +0000 | [diff] [blame] | 3408 | % The format of the NegateImage method is: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3409 | % |
| 3410 | % MagickBooleanType NegateImage(Image *image, |
cristy | b3e7c6c | 2011-07-24 01:43:55 +0000 | [diff] [blame] | 3411 | % const MagickBooleanType grayscale,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3412 | % |
| 3413 | % A description of each parameter follows: |
| 3414 | % |
| 3415 | % o image: the image. |
| 3416 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3417 | % o grayscale: If MagickTrue, only negate grayscale pixels within the image. |
| 3418 | % |
cristy | b3e7c6c | 2011-07-24 01:43:55 +0000 | [diff] [blame] | 3419 | % o exception: return any errors or warnings in this structure. |
| 3420 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3421 | */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3422 | MagickExport MagickBooleanType NegateImage(Image *image, |
cristy | b3e7c6c | 2011-07-24 01:43:55 +0000 | [diff] [blame] | 3423 | const MagickBooleanType grayscale,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3424 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3425 | #define NegateImageTag "Negate/Image" |
| 3426 | |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 3427 | CacheView |
| 3428 | *image_view; |
| 3429 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3430 | MagickBooleanType |
| 3431 | status; |
| 3432 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3433 | MagickOffsetType |
| 3434 | progress; |
| 3435 | |
| 3436 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3437 | i; |
| 3438 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3439 | ssize_t |
| 3440 | y; |
| 3441 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3442 | assert(image != (Image *) NULL); |
| 3443 | assert(image->signature == MagickSignature); |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 3444 | if( IfMagickTrue(image->debug) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3445 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 3446 | if (image->storage_class == PseudoClass) |
nicolas | e0aa3c3 | 2012-08-08 14:57:02 +0000 | [diff] [blame] | 3447 | for (i=0; i < (ssize_t) image->colors; i++) |
nicolas | fd74ae3 | 2012-08-08 16:00:11 +0000 | [diff] [blame] | 3448 | { |
| 3449 | /* |
cristy | aeded78 | 2012-09-11 23:39:36 +0000 | [diff] [blame] | 3450 | Negate colormap. |
nicolas | fd74ae3 | 2012-08-08 16:00:11 +0000 | [diff] [blame] | 3451 | */ |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 3452 | if( IfMagickTrue(grayscale) ) |
cristy | aeded78 | 2012-09-11 23:39:36 +0000 | [diff] [blame] | 3453 | if ((image->colormap[i].red != image->colormap[i].green) || |
| 3454 | (image->colormap[i].green != image->colormap[i].blue)) |
| 3455 | continue; |
nicolas | fd74ae3 | 2012-08-08 16:00:11 +0000 | [diff] [blame] | 3456 | if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0) |
cristy | aeded78 | 2012-09-11 23:39:36 +0000 | [diff] [blame] | 3457 | image->colormap[i].red=QuantumRange-image->colormap[i].red; |
nicolas | fd74ae3 | 2012-08-08 16:00:11 +0000 | [diff] [blame] | 3458 | if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0) |
cristy | aeded78 | 2012-09-11 23:39:36 +0000 | [diff] [blame] | 3459 | image->colormap[i].green=QuantumRange-image->colormap[i].green; |
nicolas | fd74ae3 | 2012-08-08 16:00:11 +0000 | [diff] [blame] | 3460 | if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0) |
cristy | aeded78 | 2012-09-11 23:39:36 +0000 | [diff] [blame] | 3461 | image->colormap[i].blue=QuantumRange-image->colormap[i].blue; |
nicolas | fd74ae3 | 2012-08-08 16:00:11 +0000 | [diff] [blame] | 3462 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3463 | /* |
| 3464 | Negate image. |
| 3465 | */ |
| 3466 | status=MagickTrue; |
| 3467 | progress=0; |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 3468 | image_view=AcquireAuthenticCacheView(image,exception); |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 3469 | if( IfMagickTrue(grayscale) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3470 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3471 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3472 | { |
| 3473 | MagickBooleanType |
| 3474 | sync; |
| 3475 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 3476 | register Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 3477 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3478 | |
cristy | 8d4629b | 2010-08-30 17:59:46 +0000 | [diff] [blame] | 3479 | register ssize_t |
| 3480 | x; |
| 3481 | |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 3482 | if( IfMagickFalse(status) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3483 | continue; |
| 3484 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1, |
| 3485 | exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 3486 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3487 | { |
| 3488 | status=MagickFalse; |
| 3489 | continue; |
| 3490 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3491 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3492 | { |
cristy | 9aa95be | 2011-07-20 21:56:45 +0000 | [diff] [blame] | 3493 | register ssize_t |
| 3494 | i; |
| 3495 | |
cristy | 883fde1 | 2013-04-08 00:50:13 +0000 | [diff] [blame] | 3496 | if ((GetPixelReadMask(image,q) == 0) || |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 3497 | IfMagickTrue(IsPixelGray(image,q))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3498 | { |
cristy | a30d9ba | 2011-07-23 21:00:48 +0000 | [diff] [blame] | 3499 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3500 | continue; |
| 3501 | } |
cristy | a30d9ba | 2011-07-23 21:00:48 +0000 | [diff] [blame] | 3502 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
cristy | 9aa95be | 2011-07-20 21:56:45 +0000 | [diff] [blame] | 3503 | { |
cristy | 5a23c55 | 2013-02-13 14:34:28 +0000 | [diff] [blame] | 3504 | PixelChannel channel=GetPixelChannelChannel(image,i); |
| 3505 | PixelTrait traits=GetPixelChannelTraits(image,channel); |
cristy | d09f880 | 2012-02-04 16:44:10 +0000 | [diff] [blame] | 3506 | if ((traits & UpdatePixelTrait) == 0) |
| 3507 | continue; |
| 3508 | q[i]=QuantumRange-q[i]; |
cristy | 9aa95be | 2011-07-20 21:56:45 +0000 | [diff] [blame] | 3509 | } |
cristy | a30d9ba | 2011-07-23 21:00:48 +0000 | [diff] [blame] | 3510 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3511 | } |
| 3512 | sync=SyncCacheViewAuthenticPixels(image_view,exception); |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 3513 | if( IfMagickFalse(sync) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3514 | status=MagickFalse; |
| 3515 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 3516 | { |
| 3517 | MagickBooleanType |
| 3518 | proceed; |
| 3519 | |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 3520 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 564a569 | 2012-01-20 23:56:26 +0000 | [diff] [blame] | 3521 | #pragma omp critical (MagickCore_NegateImage) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3522 | #endif |
| 3523 | proceed=SetImageProgress(image,NegateImageTag,progress++, |
| 3524 | image->rows); |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 3525 | if( IfMagickFalse(proceed) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3526 | status=MagickFalse; |
| 3527 | } |
| 3528 | } |
| 3529 | image_view=DestroyCacheView(image_view); |
| 3530 | return(MagickTrue); |
| 3531 | } |
| 3532 | /* |
| 3533 | Negate image. |
| 3534 | */ |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 3535 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 9a5a52f | 2012-10-09 14:40:31 +0000 | [diff] [blame] | 3536 | #pragma omp parallel for schedule(static,4) shared(progress,status) \ |
cristy | 5e6b259 | 2012-12-19 14:08:11 +0000 | [diff] [blame] | 3537 | magick_threads(image,image,image->rows,1) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3538 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3539 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3540 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 3541 | register Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 3542 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3543 | |
cristy | 8d4629b | 2010-08-30 17:59:46 +0000 | [diff] [blame] | 3544 | register ssize_t |
| 3545 | x; |
| 3546 | |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 3547 | if( IfMagickFalse(status) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3548 | continue; |
| 3549 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 3550 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3551 | { |
| 3552 | status=MagickFalse; |
| 3553 | continue; |
| 3554 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3555 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3556 | { |
cristy | f7dc44c | 2011-07-20 14:41:15 +0000 | [diff] [blame] | 3557 | register ssize_t |
| 3558 | i; |
| 3559 | |
cristy | 883fde1 | 2013-04-08 00:50:13 +0000 | [diff] [blame] | 3560 | if (GetPixelReadMask(image,q) == 0) |
cristy | d09f880 | 2012-02-04 16:44:10 +0000 | [diff] [blame] | 3561 | { |
| 3562 | q+=GetPixelChannels(image); |
| 3563 | continue; |
| 3564 | } |
cristy | a30d9ba | 2011-07-23 21:00:48 +0000 | [diff] [blame] | 3565 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
cristy | f7dc44c | 2011-07-20 14:41:15 +0000 | [diff] [blame] | 3566 | { |
cristy | 5a23c55 | 2013-02-13 14:34:28 +0000 | [diff] [blame] | 3567 | PixelChannel channel=GetPixelChannelChannel(image,i); |
| 3568 | PixelTrait traits=GetPixelChannelTraits(image,channel); |
cristy | d09f880 | 2012-02-04 16:44:10 +0000 | [diff] [blame] | 3569 | if ((traits & UpdatePixelTrait) == 0) |
cristy | ec9e3a6 | 2012-02-01 02:09:32 +0000 | [diff] [blame] | 3570 | continue; |
| 3571 | q[i]=QuantumRange-q[i]; |
cristy | f7dc44c | 2011-07-20 14:41:15 +0000 | [diff] [blame] | 3572 | } |
cristy | a30d9ba | 2011-07-23 21:00:48 +0000 | [diff] [blame] | 3573 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3574 | } |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 3575 | if( IfMagickFalse(SyncCacheViewAuthenticPixels(image_view,exception)) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3576 | status=MagickFalse; |
| 3577 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 3578 | { |
| 3579 | MagickBooleanType |
| 3580 | proceed; |
| 3581 | |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 3582 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 564a569 | 2012-01-20 23:56:26 +0000 | [diff] [blame] | 3583 | #pragma omp critical (MagickCore_NegateImage) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3584 | #endif |
| 3585 | proceed=SetImageProgress(image,NegateImageTag,progress++,image->rows); |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 3586 | if( IfMagickFalse(proceed) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3587 | status=MagickFalse; |
| 3588 | } |
| 3589 | } |
| 3590 | image_view=DestroyCacheView(image_view); |
| 3591 | return(status); |
| 3592 | } |
| 3593 | |
| 3594 | /* |
| 3595 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3596 | % % |
| 3597 | % % |
| 3598 | % % |
| 3599 | % N o r m a l i z e I m a g e % |
| 3600 | % % |
| 3601 | % % |
| 3602 | % % |
| 3603 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3604 | % |
anthony | 207ce47 | 2012-04-04 06:21:26 +0000 | [diff] [blame] | 3605 | % The NormalizeImage() method enhances the contrast of a color image by |
| 3606 | % mapping the darkest 2 percent of all pixel to black and the brightest |
| 3607 | % 1 percent to white. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3608 | % |
| 3609 | % The format of the NormalizeImage method is: |
| 3610 | % |
cristy | e23ec9d | 2011-08-16 18:15:40 +0000 | [diff] [blame] | 3611 | % MagickBooleanType NormalizeImage(Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3612 | % |
| 3613 | % A description of each parameter follows: |
| 3614 | % |
| 3615 | % o image: the image. |
| 3616 | % |
cristy | e23ec9d | 2011-08-16 18:15:40 +0000 | [diff] [blame] | 3617 | % o exception: return any errors or warnings in this structure. |
| 3618 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3619 | */ |
cristy | e23ec9d | 2011-08-16 18:15:40 +0000 | [diff] [blame] | 3620 | MagickExport MagickBooleanType NormalizeImage(Image *image, |
| 3621 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3622 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3623 | double |
| 3624 | black_point, |
| 3625 | white_point; |
| 3626 | |
cristy | 530239c | 2010-07-25 17:34:26 +0000 | [diff] [blame] | 3627 | black_point=(double) image->columns*image->rows*0.0015; |
| 3628 | white_point=(double) image->columns*image->rows*0.9995; |
cristy | e23ec9d | 2011-08-16 18:15:40 +0000 | [diff] [blame] | 3629 | return(ContrastStretchImage(image,black_point,white_point,exception)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3630 | } |
| 3631 | |
| 3632 | /* |
| 3633 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3634 | % % |
| 3635 | % % |
| 3636 | % % |
| 3637 | % S i g m o i d a l C o n t r a s t I m a g e % |
| 3638 | % % |
| 3639 | % % |
| 3640 | % % |
| 3641 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3642 | % |
| 3643 | % SigmoidalContrastImage() adjusts the contrast of an image with a non-linear |
| 3644 | % sigmoidal contrast algorithm. Increase the contrast of the image using a |
| 3645 | % sigmoidal transfer function without saturating highlights or shadows. |
| 3646 | % Contrast indicates how much to increase the contrast (0 is none; 3 is |
anthony | 207ce47 | 2012-04-04 06:21:26 +0000 | [diff] [blame] | 3647 | % typical; 20 is pushing it); mid-point indicates where midtones fall in the |
| 3648 | % resultant image (0 is white; 50% is middle-gray; 100% is black). Set |
| 3649 | % sharpen to MagickTrue to increase the image contrast otherwise the contrast |
| 3650 | % is reduced. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3651 | % |
| 3652 | % The format of the SigmoidalContrastImage method is: |
| 3653 | % |
| 3654 | % MagickBooleanType SigmoidalContrastImage(Image *image, |
cristy | 33bd515 | 2011-08-24 01:42:24 +0000 | [diff] [blame] | 3655 | % const MagickBooleanType sharpen,const char *levels, |
| 3656 | % ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3657 | % |
| 3658 | % A description of each parameter follows: |
| 3659 | % |
| 3660 | % o image: the image. |
| 3661 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3662 | % o sharpen: Increase or decrease image contrast. |
| 3663 | % |
nicolas | 07299f1 | 2012-09-10 17:30:07 +0000 | [diff] [blame] | 3664 | % o contrast: strength of the contrast, the larger the number the more |
cristy | fa76958 | 2010-09-30 23:30:03 +0000 | [diff] [blame] | 3665 | % 'threshold-like' it becomes. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3666 | % |
nicolas | 1de98ba | 2012-09-10 17:34:35 +0000 | [diff] [blame] | 3667 | % o midpoint: midpoint of the function as a color value 0 to QuantumRange. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3668 | % |
cristy | 33bd515 | 2011-08-24 01:42:24 +0000 | [diff] [blame] | 3669 | % o exception: return any errors or warnings in this structure. |
| 3670 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3671 | */ |
nicolas | 678607b | 2012-09-10 20:05:40 +0000 | [diff] [blame] | 3672 | |
nicolas | 6457d9f | 2012-09-12 14:40:01 +0000 | [diff] [blame] | 3673 | /* |
nicolas | ef20d0e | 2012-09-18 16:05:53 +0000 | [diff] [blame] | 3674 | ImageMagick 6 has a version of this function which uses LUTs. |
| 3675 | */ |
| 3676 | |
| 3677 | /* |
nicolas | 4d6c534 | 2012-09-12 14:59:55 +0000 | [diff] [blame] | 3678 | Sigmoidal function Sigmoidal with inflexion point moved to b and "slope |
| 3679 | constant" set to a. |
| 3680 | |
nicolas | 6457d9f | 2012-09-12 14:40:01 +0000 | [diff] [blame] | 3681 | The first version, based on the hyperbolic tangent tanh, when combined with |
| 3682 | the scaling step, is an exact arithmetic clone of the the sigmoid function |
| 3683 | based on the logistic curve. The equivalence is based on the identity |
| 3684 | |
nicolas | 4d6c534 | 2012-09-12 14:59:55 +0000 | [diff] [blame] | 3685 | 1/(1+exp(-t)) = (1+tanh(t/2))/2 |
nicolas | 6457d9f | 2012-09-12 14:40:01 +0000 | [diff] [blame] | 3686 | |
nicolas | 4d6c534 | 2012-09-12 14:59:55 +0000 | [diff] [blame] | 3687 | (http://de.wikipedia.org/wiki/Sigmoidfunktion) and the fact that the |
| 3688 | scaled sigmoidal derivation is invariant under affine transformations of |
| 3689 | the ordinate. |
| 3690 | |
| 3691 | The tanh version is almost certainly more accurate and cheaper. The 0.5 |
| 3692 | factor in the argument is to clone the legacy ImageMagick behavior. The |
| 3693 | reason for making the define depend on atanh even though it only uses tanh |
| 3694 | has to do with the construction of the inverse of the scaled sigmoidal. |
nicolas | 6457d9f | 2012-09-12 14:40:01 +0000 | [diff] [blame] | 3695 | */ |
nicolas | e346602 | 2012-09-04 13:51:46 +0000 | [diff] [blame] | 3696 | #if defined(MAGICKCORE_HAVE_ATANH) |
nicolas | 07299f1 | 2012-09-10 17:30:07 +0000 | [diff] [blame] | 3697 | #define Sigmoidal(a,b,x) ( tanh((0.5*(a))*((x)-(b))) ) |
nicolas | e346602 | 2012-09-04 13:51:46 +0000 | [diff] [blame] | 3698 | #else |
nicolas | 07299f1 | 2012-09-10 17:30:07 +0000 | [diff] [blame] | 3699 | #define Sigmoidal(a,b,x) ( 1.0/(1.0+exp((a)*((b)-(x)))) ) |
nicolas | e346602 | 2012-09-04 13:51:46 +0000 | [diff] [blame] | 3700 | #endif |
cristy | 5eab535 | 2012-09-12 13:12:43 +0000 | [diff] [blame] | 3701 | /* |
nicolas | 6457d9f | 2012-09-12 14:40:01 +0000 | [diff] [blame] | 3702 | Scaled sigmoidal function: |
cristy | 5eab535 | 2012-09-12 13:12:43 +0000 | [diff] [blame] | 3703 | |
nicolas | 07299f1 | 2012-09-10 17:30:07 +0000 | [diff] [blame] | 3704 | ( Sigmoidal(a,b,x) - Sigmoidal(a,b,0) ) / |
| 3705 | ( Sigmoidal(a,b,1) - Sigmoidal(a,b,0) ) |
cristy | 5eab535 | 2012-09-12 13:12:43 +0000 | [diff] [blame] | 3706 | |
| 3707 | See http://osdir.com/ml/video.image-magick.devel/2005-04/msg00006.html and |
| 3708 | http://www.cs.dartmouth.edu/farid/downloads/tutorials/fip.pdf. The limit |
| 3709 | of ScaledSigmoidal as a->0 is the identity, but a=0 gives a division by |
nicolas | 1f1584e | 2012-09-12 15:20:56 +0000 | [diff] [blame] | 3710 | zero. This is fixed below by exiting immediately when contrast is small, |
cristy | 5eab535 | 2012-09-12 13:12:43 +0000 | [diff] [blame] | 3711 | leaving the image (or colormap) unmodified. This appears to be safe because |
| 3712 | the series expansion of the logistic sigmoidal function around x=b is |
nicolas | 6457d9f | 2012-09-12 14:40:01 +0000 | [diff] [blame] | 3713 | |
| 3714 | 1/2-a*(b-x)/4+... |
| 3715 | |
| 3716 | so that the key denominator s(1)-s(0) is about a/4 (a/2 with tanh). |
cristy | 5eab535 | 2012-09-12 13:12:43 +0000 | [diff] [blame] | 3717 | */ |
nicolas | 07299f1 | 2012-09-10 17:30:07 +0000 | [diff] [blame] | 3718 | #define ScaledSigmoidal(a,b,x) ( \ |
nicolas | f0158cf | 2012-09-10 20:11:02 +0000 | [diff] [blame] | 3719 | (Sigmoidal((a),(b),(x))-Sigmoidal((a),(b),0.0)) / \ |
nicolas | 07299f1 | 2012-09-10 17:30:07 +0000 | [diff] [blame] | 3720 | (Sigmoidal((a),(b),1.0)-Sigmoidal((a),(b),0.0)) ) |
cristy | 5eab535 | 2012-09-12 13:12:43 +0000 | [diff] [blame] | 3721 | /* |
nicolas | 6457d9f | 2012-09-12 14:40:01 +0000 | [diff] [blame] | 3722 | Inverse of ScaledSigmoidal, used for +sigmoidal-contrast. Because b |
| 3723 | may be 0 or 1, the argument of the hyperbolic tangent (resp. logistic |
| 3724 | sigmoidal) may be outside of the interval (-1,1) (resp. (0,1)), even |
| 3725 | when creating a LUT from in gamut values, hence the branching. In |
| 3726 | addition, HDRI may have out of gamut values. |
nicolas | 7922b46 | 2012-09-12 14:50:49 +0000 | [diff] [blame] | 3727 | InverseScaledSigmoidal is not a two-sided inverse of ScaledSigmoidal: |
cristy | 5eab535 | 2012-09-12 13:12:43 +0000 | [diff] [blame] | 3728 | It is only a right inverse. This is unavoidable. |
| 3729 | */ |
cristy | 5eab535 | 2012-09-12 13:12:43 +0000 | [diff] [blame] | 3730 | static inline double InverseScaledSigmoidal(const double a,const double b, |
| 3731 | const double x) |
| 3732 | { |
nicolas | 6457d9f | 2012-09-12 14:40:01 +0000 | [diff] [blame] | 3733 | const double sig0=Sigmoidal(a,b,0.0); |
nicolas | b45b69a | 2012-09-15 20:00:31 +0000 | [diff] [blame] | 3734 | const double sig1=Sigmoidal(a,b,1.0); |
| 3735 | const double argument=(sig1-sig0)*x+sig0; |
nicolas | 6457d9f | 2012-09-12 14:40:01 +0000 | [diff] [blame] | 3736 | const double clamped= |
| 3737 | ( |
nicolas | 196d63c | 2012-09-12 14:46:01 +0000 | [diff] [blame] | 3738 | #if defined(MAGICKCORE_HAVE_ATANH) |
nicolas | 6457d9f | 2012-09-12 14:40:01 +0000 | [diff] [blame] | 3739 | argument < -1+MagickEpsilon |
| 3740 | ? |
| 3741 | -1+MagickEpsilon |
| 3742 | : |
| 3743 | ( argument > 1-MagickEpsilon ? 1-MagickEpsilon : argument ) |
| 3744 | ); |
| 3745 | return(b+(2.0/a)*atanh(clamped)); |
nicolas | e346602 | 2012-09-04 13:51:46 +0000 | [diff] [blame] | 3746 | #else |
nicolas | 6457d9f | 2012-09-12 14:40:01 +0000 | [diff] [blame] | 3747 | argument < MagickEpsilon |
| 3748 | ? |
| 3749 | MagickEpsilon |
| 3750 | : |
| 3751 | ( argument > 1-MagickEpsilon ? 1-MagickEpsilon : argument ) |
| 3752 | ); |
nicolas | 02c9a68 | 2012-09-15 20:01:56 +0000 | [diff] [blame] | 3753 | return(b-log(1.0/clamped-1.0)/a); |
nicolas | e346602 | 2012-09-04 13:51:46 +0000 | [diff] [blame] | 3754 | #endif |
nicolas | 196d63c | 2012-09-12 14:46:01 +0000 | [diff] [blame] | 3755 | } |
cristy | 5eab535 | 2012-09-12 13:12:43 +0000 | [diff] [blame] | 3756 | |
| 3757 | MagickExport MagickBooleanType SigmoidalContrastImage(Image *image, |
| 3758 | const MagickBooleanType sharpen,const double contrast,const double midpoint, |
| 3759 | ExceptionInfo *exception) |
| 3760 | { |
| 3761 | #define SigmoidalContrastImageTag "SigmoidalContrast/Image" |
nicolas | f94a4f4 | 2012-09-11 09:18:55 +0000 | [diff] [blame] | 3762 | #define ScaledSig(x) ( ClampToQuantum(QuantumRange* \ |
cristy | aeded78 | 2012-09-11 23:39:36 +0000 | [diff] [blame] | 3763 | ScaledSigmoidal(contrast,QuantumScale*midpoint,QuantumScale*(x))) ) |
nicolas | f94a4f4 | 2012-09-11 09:18:55 +0000 | [diff] [blame] | 3764 | #define InverseScaledSig(x) ( ClampToQuantum(QuantumRange* \ |
cristy | aeded78 | 2012-09-11 23:39:36 +0000 | [diff] [blame] | 3765 | InverseScaledSigmoidal(contrast,QuantumScale*midpoint,QuantumScale*(x))) ) |
nicolas | 07299f1 | 2012-09-10 17:30:07 +0000 | [diff] [blame] | 3766 | |
cristy | 5eab535 | 2012-09-12 13:12:43 +0000 | [diff] [blame] | 3767 | CacheView |
| 3768 | *image_view; |
| 3769 | |
| 3770 | MagickBooleanType |
| 3771 | status; |
| 3772 | |
| 3773 | MagickOffsetType |
| 3774 | progress; |
| 3775 | |
| 3776 | ssize_t |
| 3777 | y; |
| 3778 | |
| 3779 | /* |
| 3780 | Convenience macros. |
| 3781 | */ |
nicolas | 33c7671 | 2012-08-08 17:15:05 +0000 | [diff] [blame] | 3782 | assert(image != (Image *) NULL); |
| 3783 | assert(image->signature == MagickSignature); |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 3784 | if( IfMagickTrue(image->debug) ) |
nicolas | 33c7671 | 2012-08-08 17:15:05 +0000 | [diff] [blame] | 3785 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
nicolas | 07299f1 | 2012-09-10 17:30:07 +0000 | [diff] [blame] | 3786 | /* |
nicolas | 6457d9f | 2012-09-12 14:40:01 +0000 | [diff] [blame] | 3787 | Side effect: may clamp values unless contrast<MagickEpsilon, in which |
cristy | 5eab535 | 2012-09-12 13:12:43 +0000 | [diff] [blame] | 3788 | case nothing is done. |
| 3789 | */ |
| 3790 | if (contrast < MagickEpsilon) |
| 3791 | return(MagickTrue); |
| 3792 | /* |
nicolas | 4ff353c | 2012-09-10 20:15:40 +0000 | [diff] [blame] | 3793 | Sigmoidal-contrast enhance colormap. |
nicolas | 07299f1 | 2012-09-10 17:30:07 +0000 | [diff] [blame] | 3794 | */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3795 | if (image->storage_class == PseudoClass) |
nicolas | fd74ae3 | 2012-08-08 16:00:11 +0000 | [diff] [blame] | 3796 | { |
nicolas | b06434e | 2012-09-10 18:06:15 +0000 | [diff] [blame] | 3797 | register ssize_t |
cristy | aeded78 | 2012-09-11 23:39:36 +0000 | [diff] [blame] | 3798 | i; |
nicolas | 07299f1 | 2012-09-10 17:30:07 +0000 | [diff] [blame] | 3799 | |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 3800 | if( IfMagickTrue(sharpen) ) |
nicolas | 80ad4a6 | 2012-09-11 15:17:16 +0000 | [diff] [blame] | 3801 | for (i=0; i < (ssize_t) image->colors; i++) |
| 3802 | { |
| 3803 | if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0) |
cristy | 6bbed7e | 2013-04-03 23:59:10 +0000 | [diff] [blame] | 3804 | image->colormap[i].red=(MagickRealType) ScaledSig( |
| 3805 | image->colormap[i].red); |
cristy | aeded78 | 2012-09-11 23:39:36 +0000 | [diff] [blame] | 3806 | if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0) |
cristy | 6bbed7e | 2013-04-03 23:59:10 +0000 | [diff] [blame] | 3807 | image->colormap[i].green=(MagickRealType) ScaledSig( |
| 3808 | image->colormap[i].green); |
cristy | aeded78 | 2012-09-11 23:39:36 +0000 | [diff] [blame] | 3809 | if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0) |
cristy | 6bbed7e | 2013-04-03 23:59:10 +0000 | [diff] [blame] | 3810 | image->colormap[i].blue=(MagickRealType) ScaledSig( |
| 3811 | image->colormap[i].blue); |
cristy | aeded78 | 2012-09-11 23:39:36 +0000 | [diff] [blame] | 3812 | if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) |
cristy | 6bbed7e | 2013-04-03 23:59:10 +0000 | [diff] [blame] | 3813 | image->colormap[i].alpha=(MagickRealType) ScaledSig( |
| 3814 | image->colormap[i].alpha); |
cristy | aeded78 | 2012-09-11 23:39:36 +0000 | [diff] [blame] | 3815 | } |
nicolas | b06434e | 2012-09-10 18:06:15 +0000 | [diff] [blame] | 3816 | else |
nicolas | 80ad4a6 | 2012-09-11 15:17:16 +0000 | [diff] [blame] | 3817 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | aeded78 | 2012-09-11 23:39:36 +0000 | [diff] [blame] | 3818 | { |
| 3819 | if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0) |
cristy | 6bbed7e | 2013-04-03 23:59:10 +0000 | [diff] [blame] | 3820 | image->colormap[i].red=(MagickRealType) InverseScaledSig( |
| 3821 | image->colormap[i].red); |
cristy | aeded78 | 2012-09-11 23:39:36 +0000 | [diff] [blame] | 3822 | if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0) |
cristy | 6bbed7e | 2013-04-03 23:59:10 +0000 | [diff] [blame] | 3823 | image->colormap[i].green=(MagickRealType) InverseScaledSig( |
| 3824 | image->colormap[i].green); |
cristy | aeded78 | 2012-09-11 23:39:36 +0000 | [diff] [blame] | 3825 | if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0) |
cristy | 6bbed7e | 2013-04-03 23:59:10 +0000 | [diff] [blame] | 3826 | image->colormap[i].blue=(MagickRealType) InverseScaledSig( |
| 3827 | image->colormap[i].blue); |
nicolas | 80ad4a6 | 2012-09-11 15:17:16 +0000 | [diff] [blame] | 3828 | if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) |
cristy | 6bbed7e | 2013-04-03 23:59:10 +0000 | [diff] [blame] | 3829 | image->colormap[i].alpha=(MagickRealType) InverseScaledSig( |
| 3830 | image->colormap[i].alpha); |
cristy | aeded78 | 2012-09-11 23:39:36 +0000 | [diff] [blame] | 3831 | } |
nicolas | fd74ae3 | 2012-08-08 16:00:11 +0000 | [diff] [blame] | 3832 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3833 | /* |
nicolas | 4ff353c | 2012-09-10 20:15:40 +0000 | [diff] [blame] | 3834 | Sigmoidal-contrast enhance image. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3835 | */ |
| 3836 | status=MagickTrue; |
| 3837 | progress=0; |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 3838 | image_view=AcquireAuthenticCacheView(image,exception); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 3839 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 3840 | #pragma omp parallel for schedule(static,4) shared(progress,status) \ |
cristy | 5e6b259 | 2012-12-19 14:08:11 +0000 | [diff] [blame] | 3841 | magick_threads(image,image,image->rows,1) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3842 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3843 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3844 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 3845 | register Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 3846 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3847 | |
cristy | 8d4629b | 2010-08-30 17:59:46 +0000 | [diff] [blame] | 3848 | register ssize_t |
| 3849 | x; |
| 3850 | |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 3851 | if( IfMagickFalse(status) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3852 | continue; |
| 3853 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 3854 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3855 | { |
| 3856 | status=MagickFalse; |
| 3857 | continue; |
| 3858 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3859 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3860 | { |
cristy | 33bd515 | 2011-08-24 01:42:24 +0000 | [diff] [blame] | 3861 | register ssize_t |
cristy | aeded78 | 2012-09-11 23:39:36 +0000 | [diff] [blame] | 3862 | i; |
cristy | 33bd515 | 2011-08-24 01:42:24 +0000 | [diff] [blame] | 3863 | |
cristy | 883fde1 | 2013-04-08 00:50:13 +0000 | [diff] [blame] | 3864 | if (GetPixelReadMask(image,q) == 0) |
cristy | 10a6c61 | 2012-01-29 21:41:05 +0000 | [diff] [blame] | 3865 | { |
| 3866 | q+=GetPixelChannels(image); |
| 3867 | continue; |
| 3868 | } |
cristy | 33bd515 | 2011-08-24 01:42:24 +0000 | [diff] [blame] | 3869 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
| 3870 | { |
cristy | 5a23c55 | 2013-02-13 14:34:28 +0000 | [diff] [blame] | 3871 | PixelChannel channel=GetPixelChannelChannel(image,i); |
| 3872 | PixelTrait traits=GetPixelChannelTraits(image,channel); |
cristy | d09f880 | 2012-02-04 16:44:10 +0000 | [diff] [blame] | 3873 | if ((traits & UpdatePixelTrait) == 0) |
| 3874 | continue; |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 3875 | if( IfMagickTrue(sharpen) ) |
cristy | aeded78 | 2012-09-11 23:39:36 +0000 | [diff] [blame] | 3876 | q[i]=ScaledSig(q[i]); |
| 3877 | else |
| 3878 | q[i]=InverseScaledSig(q[i]); |
cristy | 33bd515 | 2011-08-24 01:42:24 +0000 | [diff] [blame] | 3879 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 3880 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3881 | } |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 3882 | if( IfMagickFalse(SyncCacheViewAuthenticPixels(image_view,exception)) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3883 | status=MagickFalse; |
| 3884 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 3885 | { |
| 3886 | MagickBooleanType |
| 3887 | proceed; |
| 3888 | |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 3889 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 564a569 | 2012-01-20 23:56:26 +0000 | [diff] [blame] | 3890 | #pragma omp critical (MagickCore_SigmoidalContrastImage) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3891 | #endif |
| 3892 | proceed=SetImageProgress(image,SigmoidalContrastImageTag,progress++, |
| 3893 | image->rows); |
anthony | a322a83 | 2013-04-27 06:28:03 +0000 | [diff] [blame] | 3894 | if( IfMagickFalse(proceed) ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3895 | status=MagickFalse; |
| 3896 | } |
| 3897 | } |
| 3898 | image_view=DestroyCacheView(image_view); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3899 | return(status); |
| 3900 | } |