cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % % |
| 6 | % TTTTT H H RRRR EEEEE SSSSS H H OOO L DDDD % |
| 7 | % T H H R R E SS H H O O L D D % |
| 8 | % T HHHHH RRRR EEE SSS HHHHH O O L D D % |
| 9 | % T H H R R E SS H H O O L D D % |
| 10 | % T H H R R EEEEE SSSSS H H OOO LLLLL DDDD % |
| 11 | % % |
| 12 | % % |
| 13 | % MagickCore Image Threshold Methods % |
| 14 | % % |
| 15 | % Software Design % |
cristy | de984cd | 2013-12-01 14:49:27 +0000 | [diff] [blame] | 16 | % Cristy % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 17 | % October 1996 % |
| 18 | % % |
| 19 | % % |
cristy | b56bb24 | 2014-11-25 17:12:48 +0000 | [diff] [blame] | 20 | % Copyright 1999-2015 ImageMagick Studio LLC, a non-profit organization % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 21 | % dedicated to making software imaging solutions freely available. % |
| 22 | % % |
| 23 | % You may not use this file except in compliance with the License. You may % |
| 24 | % obtain a copy of the License at % |
| 25 | % % |
| 26 | % http://www.imagemagick.org/script/license.php % |
| 27 | % % |
| 28 | % Unless required by applicable law or agreed to in writing, software % |
| 29 | % distributed under the License is distributed on an "AS IS" BASIS, % |
| 30 | % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. % |
| 31 | % See the License for the specific language governing permissions and % |
| 32 | % limitations under the License. % |
| 33 | % % |
| 34 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 35 | % |
| 36 | % |
| 37 | % |
| 38 | */ |
| 39 | |
| 40 | /* |
| 41 | Include declarations. |
| 42 | */ |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 43 | #include "MagickCore/studio.h" |
| 44 | #include "MagickCore/property.h" |
| 45 | #include "MagickCore/blob.h" |
| 46 | #include "MagickCore/cache-view.h" |
| 47 | #include "MagickCore/color.h" |
| 48 | #include "MagickCore/color-private.h" |
| 49 | #include "MagickCore/colormap.h" |
| 50 | #include "MagickCore/colorspace.h" |
cristy | 23e55c0 | 2012-04-10 01:21:56 +0000 | [diff] [blame] | 51 | #include "MagickCore/colorspace-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 52 | #include "MagickCore/configure.h" |
| 53 | #include "MagickCore/constitute.h" |
| 54 | #include "MagickCore/decorate.h" |
| 55 | #include "MagickCore/draw.h" |
| 56 | #include "MagickCore/enhance.h" |
| 57 | #include "MagickCore/exception.h" |
| 58 | #include "MagickCore/exception-private.h" |
| 59 | #include "MagickCore/effect.h" |
| 60 | #include "MagickCore/fx.h" |
| 61 | #include "MagickCore/gem.h" |
| 62 | #include "MagickCore/geometry.h" |
| 63 | #include "MagickCore/image-private.h" |
| 64 | #include "MagickCore/list.h" |
| 65 | #include "MagickCore/log.h" |
| 66 | #include "MagickCore/memory_.h" |
| 67 | #include "MagickCore/monitor.h" |
| 68 | #include "MagickCore/monitor-private.h" |
| 69 | #include "MagickCore/montage.h" |
| 70 | #include "MagickCore/option.h" |
| 71 | #include "MagickCore/pixel-accessor.h" |
cristy | 741129f | 2015-01-09 20:08:46 +0000 | [diff] [blame^] | 72 | #include "MagickCore/pixel-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 73 | #include "MagickCore/quantize.h" |
| 74 | #include "MagickCore/quantum.h" |
| 75 | #include "MagickCore/random_.h" |
| 76 | #include "MagickCore/random-private.h" |
| 77 | #include "MagickCore/resize.h" |
| 78 | #include "MagickCore/resource_.h" |
| 79 | #include "MagickCore/segment.h" |
| 80 | #include "MagickCore/shear.h" |
| 81 | #include "MagickCore/signature-private.h" |
| 82 | #include "MagickCore/string_.h" |
| 83 | #include "MagickCore/string-private.h" |
| 84 | #include "MagickCore/thread-private.h" |
| 85 | #include "MagickCore/threshold.h" |
cristy | 4e0b82a | 2011-09-29 12:47:44 +0000 | [diff] [blame] | 86 | #include "MagickCore/token.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 87 | #include "MagickCore/transform.h" |
| 88 | #include "MagickCore/xml-tree.h" |
cristy | 433d118 | 2011-09-04 13:38:52 +0000 | [diff] [blame] | 89 | #include "MagickCore/xml-tree-private.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 90 | |
| 91 | /* |
| 92 | Define declarations. |
| 93 | */ |
| 94 | #define ThresholdsFilename "thresholds.xml" |
| 95 | |
| 96 | /* |
| 97 | Typedef declarations. |
| 98 | */ |
| 99 | struct _ThresholdMap |
| 100 | { |
| 101 | char |
| 102 | *map_id, |
| 103 | *description; |
| 104 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 105 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 106 | width, |
| 107 | height; |
| 108 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 109 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 110 | divisor, |
| 111 | *levels; |
| 112 | }; |
| 113 | |
| 114 | /* |
cristy | da9d0e4 | 2013-02-26 00:50:38 +0000 | [diff] [blame] | 115 | Static declarations. |
| 116 | */ |
| 117 | static const char |
| 118 | *MinimalThresholdMap = |
| 119 | "<?xml version=\"1.0\"?>" |
| 120 | "<thresholds>" |
| 121 | " <threshold map=\"threshold\" alias=\"1x1\">" |
| 122 | " <description>Threshold 1x1 (non-dither)</description>" |
| 123 | " <levels width=\"1\" height=\"1\" divisor=\"2\">" |
| 124 | " 1" |
| 125 | " </levels>" |
| 126 | " </threshold>" |
| 127 | " <threshold map=\"checks\" alias=\"2x1\">" |
| 128 | " <description>Checkerboard 2x1 (dither)</description>" |
| 129 | " <levels width=\"2\" height=\"2\" divisor=\"3\">" |
| 130 | " 1 2" |
| 131 | " 2 1" |
| 132 | " </levels>" |
| 133 | " </threshold>" |
| 134 | "</thresholds>"; |
| 135 | |
| 136 | /* |
cristy | bd0ebf0 | 2011-09-29 01:19:42 +0000 | [diff] [blame] | 137 | Forward declarations. |
| 138 | */ |
| 139 | static ThresholdMap |
| 140 | *GetThresholdMapFile(const char *,const char *,const char *,ExceptionInfo *); |
| 141 | |
| 142 | /* |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 143 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 144 | % % |
| 145 | % % |
| 146 | % % |
| 147 | % A d a p t i v e T h r e s h o l d I m a g e % |
| 148 | % % |
| 149 | % % |
| 150 | % % |
| 151 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 152 | % |
| 153 | % AdaptiveThresholdImage() selects an individual threshold for each pixel |
| 154 | % based on the range of intensity values in its local neighborhood. This |
| 155 | % allows for thresholding of an image whose global intensity histogram |
| 156 | % doesn't contain distinctive peaks. |
| 157 | % |
| 158 | % The format of the AdaptiveThresholdImage method is: |
| 159 | % |
cristy | de5cc63 | 2011-07-18 14:47:00 +0000 | [diff] [blame] | 160 | % Image *AdaptiveThresholdImage(const Image *image,const size_t width, |
| 161 | % const size_t height,const double bias,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 162 | % |
| 163 | % A description of each parameter follows: |
| 164 | % |
| 165 | % o image: the image. |
| 166 | % |
| 167 | % o width: the width of the local neighborhood. |
| 168 | % |
| 169 | % o height: the height of the local neighborhood. |
| 170 | % |
cristy | de5cc63 | 2011-07-18 14:47:00 +0000 | [diff] [blame] | 171 | % o bias: the mean bias. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 172 | % |
| 173 | % o exception: return any errors or warnings in this structure. |
| 174 | % |
| 175 | */ |
| 176 | MagickExport Image *AdaptiveThresholdImage(const Image *image, |
cristy | de5cc63 | 2011-07-18 14:47:00 +0000 | [diff] [blame] | 177 | const size_t width,const size_t height,const double bias, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 178 | ExceptionInfo *exception) |
| 179 | { |
cristy | de5cc63 | 2011-07-18 14:47:00 +0000 | [diff] [blame] | 180 | #define AdaptiveThresholdImageTag "AdaptiveThreshold/Image" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 181 | |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 182 | CacheView |
| 183 | *image_view, |
| 184 | *threshold_view; |
| 185 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 186 | Image |
| 187 | *threshold_image; |
| 188 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 189 | MagickBooleanType |
| 190 | status; |
| 191 | |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 192 | MagickOffsetType |
| 193 | progress; |
| 194 | |
cristy | de5cc63 | 2011-07-18 14:47:00 +0000 | [diff] [blame] | 195 | MagickSizeType |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 196 | number_pixels; |
| 197 | |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 198 | ssize_t |
| 199 | y; |
| 200 | |
cristy | de5cc63 | 2011-07-18 14:47:00 +0000 | [diff] [blame] | 201 | /* |
| 202 | Initialize threshold image attributes. |
| 203 | */ |
| 204 | assert(image != (Image *) NULL); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 205 | assert(image->signature == MagickSignature); |
| 206 | if (image->debug != MagickFalse) |
| 207 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 208 | assert(exception != (ExceptionInfo *) NULL); |
| 209 | assert(exception->signature == MagickSignature); |
cristy | de5cc63 | 2011-07-18 14:47:00 +0000 | [diff] [blame] | 210 | threshold_image=CloneImage(image,image->columns,image->rows,MagickTrue, |
| 211 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 212 | if (threshold_image == (Image *) NULL) |
| 213 | return((Image *) NULL); |
cristy | b9eb87b | 2011-09-29 01:15:19 +0000 | [diff] [blame] | 214 | status=SetImageStorageClass(threshold_image,DirectClass,exception); |
| 215 | if (status == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 216 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 217 | threshold_image=DestroyImage(threshold_image); |
| 218 | return((Image *) NULL); |
| 219 | } |
| 220 | /* |
cristy | de5cc63 | 2011-07-18 14:47:00 +0000 | [diff] [blame] | 221 | Threshold image. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 222 | */ |
| 223 | status=MagickTrue; |
| 224 | progress=0; |
cristy | de5cc63 | 2011-07-18 14:47:00 +0000 | [diff] [blame] | 225 | number_pixels=(MagickSizeType) width*height; |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 226 | image_view=AcquireVirtualCacheView(image,exception); |
| 227 | threshold_view=AcquireAuthenticCacheView(threshold_image,exception); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 228 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 229 | #pragma omp parallel for schedule(static,4) shared(progress,status) \ |
cristy | 5e6b259 | 2012-12-19 14:08:11 +0000 | [diff] [blame] | 230 | magick_threads(image,threshold_image,image->rows,1) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 231 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 232 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 233 | { |
cristy | 525cf96 | 2012-09-09 15:00:40 +0000 | [diff] [blame] | 234 | double |
| 235 | channel_bias[MaxPixelChannels], |
| 236 | channel_sum[MaxPixelChannels]; |
| 237 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 238 | register const Quantum |
cristy | 525cf96 | 2012-09-09 15:00:40 +0000 | [diff] [blame] | 239 | *restrict p, |
| 240 | *restrict pixels; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 241 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 242 | register Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 243 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 244 | |
cristy | de5cc63 | 2011-07-18 14:47:00 +0000 | [diff] [blame] | 245 | register ssize_t |
cristy | 525cf96 | 2012-09-09 15:00:40 +0000 | [diff] [blame] | 246 | i, |
cristy | de5cc63 | 2011-07-18 14:47:00 +0000 | [diff] [blame] | 247 | x; |
| 248 | |
cristy | de5cc63 | 2011-07-18 14:47:00 +0000 | [diff] [blame] | 249 | ssize_t |
cristy | 525cf96 | 2012-09-09 15:00:40 +0000 | [diff] [blame] | 250 | center, |
| 251 | u, |
| 252 | v; |
cristy | de5cc63 | 2011-07-18 14:47:00 +0000 | [diff] [blame] | 253 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 254 | if (status == MagickFalse) |
| 255 | continue; |
cristy | d99b096 | 2010-05-29 23:14:26 +0000 | [diff] [blame] | 256 | p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t) |
cristy | de5cc63 | 2011-07-18 14:47:00 +0000 | [diff] [blame] | 257 | (height/2L),image->columns+width,height,exception); |
| 258 | q=QueueCacheViewAuthenticPixels(threshold_view,0,y,threshold_image->columns, |
| 259 | 1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 260 | if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 261 | { |
| 262 | status=MagickFalse; |
| 263 | continue; |
| 264 | } |
cristy | 5f9f246 | 2011-09-28 23:37:58 +0000 | [diff] [blame] | 265 | center=(ssize_t) GetPixelChannels(image)*(image->columns+width)*(height/2L)+ |
cristy | a0312c9 | 2011-07-23 21:04:30 +0000 | [diff] [blame] | 266 | GetPixelChannels(image)*(width/2); |
cristy | 525cf96 | 2012-09-09 15:00:40 +0000 | [diff] [blame] | 267 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
| 268 | { |
cristy | 5a23c55 | 2013-02-13 14:34:28 +0000 | [diff] [blame] | 269 | PixelChannel channel=GetPixelChannelChannel(image,i); |
| 270 | PixelTrait traits=GetPixelChannelTraits(image,channel); |
| 271 | PixelTrait threshold_traits=GetPixelChannelTraits(threshold_image, |
| 272 | channel); |
cristy | 525cf96 | 2012-09-09 15:00:40 +0000 | [diff] [blame] | 273 | if ((traits == UndefinedPixelTrait) || |
| 274 | (threshold_traits == UndefinedPixelTrait)) |
| 275 | continue; |
| 276 | if (((threshold_traits & CopyPixelTrait) != 0) || |
cristy | 883fde1 | 2013-04-08 00:50:13 +0000 | [diff] [blame] | 277 | (GetPixelReadMask(image,p) == 0)) |
cristy | 525cf96 | 2012-09-09 15:00:40 +0000 | [diff] [blame] | 278 | { |
| 279 | SetPixelChannel(threshold_image,channel,p[center+i],q); |
| 280 | continue; |
| 281 | } |
| 282 | pixels=p; |
| 283 | channel_bias[channel]=0.0; |
| 284 | channel_sum[channel]=0.0; |
| 285 | for (v=0; v < (ssize_t) height; v++) |
| 286 | { |
| 287 | for (u=0; u < (ssize_t) width; u++) |
| 288 | { |
| 289 | if (u == (ssize_t) (width-1)) |
| 290 | channel_bias[channel]+=pixels[i]; |
| 291 | channel_sum[channel]+=pixels[i]; |
| 292 | pixels+=GetPixelChannels(image); |
| 293 | } |
cristy | 0c7bac1 | 2014-12-07 15:49:24 +0000 | [diff] [blame] | 294 | pixels+=GetPixelChannels(image)*image->columns; |
cristy | 525cf96 | 2012-09-09 15:00:40 +0000 | [diff] [blame] | 295 | } |
| 296 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 297 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 298 | { |
cristy | a0312c9 | 2011-07-23 21:04:30 +0000 | [diff] [blame] | 299 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 300 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 301 | double |
cristy | 525cf96 | 2012-09-09 15:00:40 +0000 | [diff] [blame] | 302 | mean; |
cristy | de5cc63 | 2011-07-18 14:47:00 +0000 | [diff] [blame] | 303 | |
cristy | 5a23c55 | 2013-02-13 14:34:28 +0000 | [diff] [blame] | 304 | PixelChannel channel=GetPixelChannelChannel(image,i); |
| 305 | PixelTrait traits=GetPixelChannelTraits(image,channel); |
| 306 | PixelTrait threshold_traits=GetPixelChannelTraits(threshold_image, |
| 307 | channel); |
cristy | 010d7d1 | 2011-08-31 01:02:48 +0000 | [diff] [blame] | 308 | if ((traits == UndefinedPixelTrait) || |
| 309 | (threshold_traits == UndefinedPixelTrait)) |
cristy | de5cc63 | 2011-07-18 14:47:00 +0000 | [diff] [blame] | 310 | continue; |
cristy | 1eced09 | 2012-08-10 23:10:56 +0000 | [diff] [blame] | 311 | if (((threshold_traits & CopyPixelTrait) != 0) || |
cristy | 883fde1 | 2013-04-08 00:50:13 +0000 | [diff] [blame] | 312 | (GetPixelReadMask(image,p) == 0)) |
cristy | de5cc63 | 2011-07-18 14:47:00 +0000 | [diff] [blame] | 313 | { |
cristy | 0beccfa | 2011-09-25 20:47:53 +0000 | [diff] [blame] | 314 | SetPixelChannel(threshold_image,channel,p[center+i],q); |
cristy | de5cc63 | 2011-07-18 14:47:00 +0000 | [diff] [blame] | 315 | continue; |
| 316 | } |
cristy | 525cf96 | 2012-09-09 15:00:40 +0000 | [diff] [blame] | 317 | channel_sum[channel]-=channel_bias[channel]; |
| 318 | channel_bias[channel]=0.0; |
cristy | de5cc63 | 2011-07-18 14:47:00 +0000 | [diff] [blame] | 319 | pixels=p; |
cristy | de5cc63 | 2011-07-18 14:47:00 +0000 | [diff] [blame] | 320 | for (v=0; v < (ssize_t) height; v++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 321 | { |
cristy | 525cf96 | 2012-09-09 15:00:40 +0000 | [diff] [blame] | 322 | channel_bias[channel]+=pixels[i]; |
| 323 | pixels+=(width-1)*GetPixelChannels(image); |
| 324 | channel_sum[channel]+=pixels[i]; |
dirk | a608e0a | 2015-01-06 22:24:27 +0000 | [diff] [blame] | 325 | pixels+=GetPixelChannels(image)*(image->columns+1); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 326 | } |
cristy | 525cf96 | 2012-09-09 15:00:40 +0000 | [diff] [blame] | 327 | mean=(double) (channel_sum[channel]/number_pixels+bias); |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 328 | SetPixelChannel(threshold_image,channel,(Quantum) ((double) |
cristy | 5f9f246 | 2011-09-28 23:37:58 +0000 | [diff] [blame] | 329 | p[center+i] <= mean ? 0 : QuantumRange),q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 330 | } |
cristy | a0312c9 | 2011-07-23 21:04:30 +0000 | [diff] [blame] | 331 | p+=GetPixelChannels(image); |
| 332 | q+=GetPixelChannels(threshold_image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 333 | } |
cristy | de5cc63 | 2011-07-18 14:47:00 +0000 | [diff] [blame] | 334 | if (SyncCacheViewAuthenticPixels(threshold_view,exception) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 335 | status=MagickFalse; |
| 336 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 337 | { |
| 338 | MagickBooleanType |
| 339 | proceed; |
| 340 | |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 341 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 342 | #pragma omp critical (MagickCore_AdaptiveThresholdImage) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 343 | #endif |
cristy | de5cc63 | 2011-07-18 14:47:00 +0000 | [diff] [blame] | 344 | proceed=SetImageProgress(image,AdaptiveThresholdImageTag,progress++, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 345 | image->rows); |
| 346 | if (proceed == MagickFalse) |
| 347 | status=MagickFalse; |
| 348 | } |
| 349 | } |
cristy | de5cc63 | 2011-07-18 14:47:00 +0000 | [diff] [blame] | 350 | threshold_image->type=image->type; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 351 | threshold_view=DestroyCacheView(threshold_view); |
| 352 | image_view=DestroyCacheView(image_view); |
| 353 | if (status == MagickFalse) |
| 354 | threshold_image=DestroyImage(threshold_image); |
| 355 | return(threshold_image); |
| 356 | } |
| 357 | |
| 358 | /* |
| 359 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 360 | % % |
| 361 | % % |
| 362 | % % |
| 363 | % B i l e v e l I m a g e % |
| 364 | % % |
| 365 | % % |
| 366 | % % |
| 367 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 368 | % |
| 369 | % BilevelImage() changes the value of individual pixels based on the |
| 370 | % intensity of each pixel channel. The result is a high-contrast image. |
| 371 | % |
| 372 | % More precisely each channel value of the image is 'thresholded' so that if |
| 373 | % it is equal to or less than the given value it is set to zero, while any |
| 374 | % value greater than that give is set to it maximum or QuantumRange. |
| 375 | % |
| 376 | % This function is what is used to implement the "-threshold" operator for |
| 377 | % the command line API. |
| 378 | % |
| 379 | % If the default channel setting is given the image is thresholded using just |
| 380 | % the gray 'intensity' of the image, rather than the individual channels. |
| 381 | % |
cristy | f4ad9df | 2011-07-08 16:49:03 +0000 | [diff] [blame] | 382 | % The format of the BilevelImage method is: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 383 | % |
cristy | e941a75 | 2011-10-15 01:52:48 +0000 | [diff] [blame] | 384 | % MagickBooleanType BilevelImage(Image *image,const double threshold, |
| 385 | % ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 386 | % |
| 387 | % A description of each parameter follows: |
| 388 | % |
| 389 | % o image: the image. |
| 390 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 391 | % o threshold: define the threshold values. |
| 392 | % |
cristy | e941a75 | 2011-10-15 01:52:48 +0000 | [diff] [blame] | 393 | % o exception: return any errors or warnings in this structure. |
| 394 | % |
cristy | f89cb1d | 2011-07-07 01:24:37 +0000 | [diff] [blame] | 395 | % Aside: You can get the same results as operator using LevelImages() |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 396 | % with the 'threshold' value for both the black_point and the white_point. |
| 397 | % |
| 398 | */ |
cristy | e941a75 | 2011-10-15 01:52:48 +0000 | [diff] [blame] | 399 | MagickExport MagickBooleanType BilevelImage(Image *image,const double threshold, |
| 400 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 401 | { |
| 402 | #define ThresholdImageTag "Threshold/Image" |
| 403 | |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 404 | CacheView |
| 405 | *image_view; |
| 406 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 407 | MagickBooleanType |
| 408 | status; |
| 409 | |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 410 | MagickOffsetType |
| 411 | progress; |
| 412 | |
| 413 | ssize_t |
| 414 | y; |
| 415 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 416 | assert(image != (Image *) NULL); |
| 417 | assert(image->signature == MagickSignature); |
| 418 | if (image->debug != MagickFalse) |
| 419 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | 574cc26 | 2011-08-05 01:23:58 +0000 | [diff] [blame] | 420 | if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 421 | return(MagickFalse); |
cristy | 3a3b996 | 2013-02-08 18:41:18 +0000 | [diff] [blame] | 422 | if (IsGrayColorspace(image->colorspace) != MagickFalse) |
cristy | 0c81d06 | 2013-04-21 15:22:02 +0000 | [diff] [blame] | 423 | (void) SetImageColorspace(image,sRGBColorspace,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 424 | /* |
| 425 | Bilevel threshold image. |
| 426 | */ |
| 427 | status=MagickTrue; |
| 428 | progress=0; |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 429 | image_view=AcquireAuthenticCacheView(image,exception); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 430 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | bd06599 | 2012-08-09 15:27:39 +0000 | [diff] [blame] | 431 | #pragma omp parallel for schedule(static,4) shared(progress,status) \ |
cristy | 5e6b259 | 2012-12-19 14:08:11 +0000 | [diff] [blame] | 432 | magick_threads(image,image,image->rows,1) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 433 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 434 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 435 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 436 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 437 | x; |
| 438 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 439 | register Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 440 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 441 | |
| 442 | if (status == MagickFalse) |
| 443 | continue; |
| 444 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 445 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 446 | { |
| 447 | status=MagickFalse; |
| 448 | continue; |
| 449 | } |
cristy | 805b6a0 | 2011-08-09 00:59:35 +0000 | [diff] [blame] | 450 | for (x=0; x < (ssize_t) image->columns; x++) |
| 451 | { |
cristy | 171e235 | 2012-07-10 17:43:12 +0000 | [diff] [blame] | 452 | double |
| 453 | pixel; |
| 454 | |
cristy | 9511120 | 2011-08-09 19:41:42 +0000 | [diff] [blame] | 455 | register ssize_t |
| 456 | i; |
| 457 | |
cristy | 883fde1 | 2013-04-08 00:50:13 +0000 | [diff] [blame] | 458 | if (GetPixelReadMask(image,q) == 0) |
cristy | 10a6c61 | 2012-01-29 21:41:05 +0000 | [diff] [blame] | 459 | { |
| 460 | q+=GetPixelChannels(image); |
| 461 | continue; |
| 462 | } |
cristy | f13c594 | 2012-08-08 23:50:11 +0000 | [diff] [blame] | 463 | pixel=GetPixelIntensity(image,q); |
cristy | 9511120 | 2011-08-09 19:41:42 +0000 | [diff] [blame] | 464 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
| 465 | { |
cristy | 5a23c55 | 2013-02-13 14:34:28 +0000 | [diff] [blame] | 466 | PixelChannel channel=GetPixelChannelChannel(image,i); |
| 467 | PixelTrait traits=GetPixelChannelTraits(image,channel); |
cristy | d09f880 | 2012-02-04 16:44:10 +0000 | [diff] [blame] | 468 | if ((traits & UpdatePixelTrait) == 0) |
| 469 | continue; |
cristy | a64f4b9 | 2012-07-11 23:59:00 +0000 | [diff] [blame] | 470 | if (image->channel_mask != DefaultChannels) |
cristy | f13c594 | 2012-08-08 23:50:11 +0000 | [diff] [blame] | 471 | pixel=(double) q[i]; |
cristy | 171e235 | 2012-07-10 17:43:12 +0000 | [diff] [blame] | 472 | q[i]=(Quantum) (pixel <= threshold ? 0 : QuantumRange); |
cristy | 9511120 | 2011-08-09 19:41:42 +0000 | [diff] [blame] | 473 | } |
cristy | 805b6a0 | 2011-08-09 00:59:35 +0000 | [diff] [blame] | 474 | q+=GetPixelChannels(image); |
| 475 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 476 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
| 477 | status=MagickFalse; |
| 478 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 479 | { |
| 480 | MagickBooleanType |
| 481 | proceed; |
| 482 | |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 483 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 484 | #pragma omp critical (MagickCore_BilevelImage) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 485 | #endif |
| 486 | proceed=SetImageProgress(image,ThresholdImageTag,progress++, |
| 487 | image->rows); |
| 488 | if (proceed == MagickFalse) |
| 489 | status=MagickFalse; |
| 490 | } |
| 491 | } |
| 492 | image_view=DestroyCacheView(image_view); |
| 493 | return(status); |
| 494 | } |
| 495 | |
| 496 | /* |
| 497 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 498 | % % |
| 499 | % % |
| 500 | % % |
| 501 | % B l a c k T h r e s h o l d I m a g e % |
| 502 | % % |
| 503 | % % |
| 504 | % % |
| 505 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 506 | % |
| 507 | % BlackThresholdImage() is like ThresholdImage() but forces all pixels below |
cristy | 4e10130 | 2009-09-17 12:49:12 +0000 | [diff] [blame] | 508 | % the threshold into black while leaving all pixels at or above the threshold |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 509 | % unchanged. |
| 510 | % |
| 511 | % The format of the BlackThresholdImage method is: |
| 512 | % |
cristy | f4ad9df | 2011-07-08 16:49:03 +0000 | [diff] [blame] | 513 | % MagickBooleanType BlackThresholdImage(Image *image, |
| 514 | % const char *threshold,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 515 | % |
| 516 | % A description of each parameter follows: |
| 517 | % |
| 518 | % o image: the image. |
| 519 | % |
cristy | 5f9f246 | 2011-09-28 23:37:58 +0000 | [diff] [blame] | 520 | % o threshold: define the threshold value. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 521 | % |
| 522 | % o exception: return any errors or warnings in this structure. |
| 523 | % |
| 524 | */ |
| 525 | MagickExport MagickBooleanType BlackThresholdImage(Image *image, |
cristy | f4ad9df | 2011-07-08 16:49:03 +0000 | [diff] [blame] | 526 | const char *thresholds,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 527 | { |
| 528 | #define ThresholdImageTag "Threshold/Image" |
| 529 | |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 530 | CacheView |
| 531 | *image_view; |
| 532 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 533 | GeometryInfo |
| 534 | geometry_info; |
| 535 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 536 | MagickBooleanType |
| 537 | status; |
| 538 | |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 539 | MagickOffsetType |
| 540 | progress; |
| 541 | |
cristy | d680338 | 2012-04-10 01:41:25 +0000 | [diff] [blame] | 542 | PixelInfo |
cristy | a12d8ba | 2012-04-29 16:33:41 +0000 | [diff] [blame] | 543 | threshold; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 544 | |
| 545 | MagickStatusType |
| 546 | flags; |
| 547 | |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 548 | ssize_t |
| 549 | y; |
| 550 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 551 | assert(image != (Image *) NULL); |
| 552 | assert(image->signature == MagickSignature); |
| 553 | if (image->debug != MagickFalse) |
| 554 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 555 | if (thresholds == (const char *) NULL) |
| 556 | return(MagickTrue); |
cristy | 574cc26 | 2011-08-05 01:23:58 +0000 | [diff] [blame] | 557 | if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 558 | return(MagickFalse); |
cristy | 23e55c0 | 2012-04-10 01:21:56 +0000 | [diff] [blame] | 559 | if (IsGrayColorspace(image->colorspace) != MagickFalse) |
cristy | 0c81d06 | 2013-04-21 15:22:02 +0000 | [diff] [blame] | 560 | (void) SetImageColorspace(image,sRGBColorspace,exception); |
cristy | a12d8ba | 2012-04-29 16:33:41 +0000 | [diff] [blame] | 561 | GetPixelInfo(image,&threshold); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 562 | flags=ParseGeometry(thresholds,&geometry_info); |
cristy | a12d8ba | 2012-04-29 16:33:41 +0000 | [diff] [blame] | 563 | threshold.red=geometry_info.rho; |
| 564 | threshold.green=geometry_info.rho; |
| 565 | threshold.blue=geometry_info.rho; |
| 566 | threshold.black=geometry_info.rho; |
| 567 | threshold.alpha=100.0; |
cristy | 5f9f246 | 2011-09-28 23:37:58 +0000 | [diff] [blame] | 568 | if ((flags & SigmaValue) != 0) |
cristy | a12d8ba | 2012-04-29 16:33:41 +0000 | [diff] [blame] | 569 | threshold.green=geometry_info.sigma; |
cristy | 5f9f246 | 2011-09-28 23:37:58 +0000 | [diff] [blame] | 570 | if ((flags & XiValue) != 0) |
cristy | a12d8ba | 2012-04-29 16:33:41 +0000 | [diff] [blame] | 571 | threshold.blue=geometry_info.xi; |
cristy | 5f9f246 | 2011-09-28 23:37:58 +0000 | [diff] [blame] | 572 | if ((flags & PsiValue) != 0) |
cristy | a12d8ba | 2012-04-29 16:33:41 +0000 | [diff] [blame] | 573 | threshold.alpha=geometry_info.psi; |
| 574 | if (threshold.colorspace == CMYKColorspace) |
cristy | d680338 | 2012-04-10 01:41:25 +0000 | [diff] [blame] | 575 | { |
| 576 | if ((flags & PsiValue) != 0) |
cristy | a12d8ba | 2012-04-29 16:33:41 +0000 | [diff] [blame] | 577 | threshold.black=geometry_info.psi; |
cristy | d680338 | 2012-04-10 01:41:25 +0000 | [diff] [blame] | 578 | if ((flags & ChiValue) != 0) |
cristy | a12d8ba | 2012-04-29 16:33:41 +0000 | [diff] [blame] | 579 | threshold.alpha=geometry_info.chi; |
| 580 | } |
| 581 | if ((flags & PercentValue) != 0) |
| 582 | { |
cristy | 65d4e5e | 2012-10-17 12:22:24 +0000 | [diff] [blame] | 583 | threshold.red*=(MagickRealType) (QuantumRange/100.0); |
| 584 | threshold.green*=(MagickRealType) (QuantumRange/100.0); |
| 585 | threshold.blue*=(MagickRealType) (QuantumRange/100.0); |
| 586 | threshold.black*=(MagickRealType) (QuantumRange/100.0); |
| 587 | threshold.alpha*=(MagickRealType) (QuantumRange/100.0); |
cristy | d680338 | 2012-04-10 01:41:25 +0000 | [diff] [blame] | 588 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 589 | /* |
cristy | 5f9f246 | 2011-09-28 23:37:58 +0000 | [diff] [blame] | 590 | White threshold image. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 591 | */ |
| 592 | status=MagickTrue; |
| 593 | progress=0; |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 594 | image_view=AcquireAuthenticCacheView(image,exception); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 595 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | bd06599 | 2012-08-09 15:27:39 +0000 | [diff] [blame] | 596 | #pragma omp parallel for schedule(static,4) shared(progress,status) \ |
cristy | 5e6b259 | 2012-12-19 14:08:11 +0000 | [diff] [blame] | 597 | magick_threads(image,image,image->rows,1) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 598 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 599 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 600 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 601 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 602 | x; |
| 603 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 604 | register Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 605 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 606 | |
| 607 | if (status == MagickFalse) |
| 608 | continue; |
| 609 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 610 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 611 | { |
| 612 | status=MagickFalse; |
| 613 | continue; |
| 614 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 615 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | b0ea1af | 2009-11-28 20:44:46 +0000 | [diff] [blame] | 616 | { |
cristy | 81629aa | 2012-07-12 20:08:52 +0000 | [diff] [blame] | 617 | double |
| 618 | pixel; |
| 619 | |
cristy | c456718 | 2012-06-24 20:55:08 +0000 | [diff] [blame] | 620 | register ssize_t |
| 621 | i; |
| 622 | |
cristy | 883fde1 | 2013-04-08 00:50:13 +0000 | [diff] [blame] | 623 | if (GetPixelReadMask(image,q) == 0) |
cristy | 10a6c61 | 2012-01-29 21:41:05 +0000 | [diff] [blame] | 624 | { |
| 625 | q+=GetPixelChannels(image); |
| 626 | continue; |
| 627 | } |
cristy | f13c594 | 2012-08-08 23:50:11 +0000 | [diff] [blame] | 628 | pixel=GetPixelIntensity(image,q); |
cristy | 188f29a | 2012-06-24 19:09:53 +0000 | [diff] [blame] | 629 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
| 630 | { |
cristy | 5a23c55 | 2013-02-13 14:34:28 +0000 | [diff] [blame] | 631 | PixelChannel channel=GetPixelChannelChannel(image,i); |
| 632 | PixelTrait traits=GetPixelChannelTraits(image,channel); |
cristy | 188f29a | 2012-06-24 19:09:53 +0000 | [diff] [blame] | 633 | if ((traits & UpdatePixelTrait) == 0) |
| 634 | continue; |
cristy | 81629aa | 2012-07-12 20:08:52 +0000 | [diff] [blame] | 635 | if (image->channel_mask != DefaultChannels) |
cristy | f13c594 | 2012-08-08 23:50:11 +0000 | [diff] [blame] | 636 | pixel=(double) q[i]; |
cristy | 81629aa | 2012-07-12 20:08:52 +0000 | [diff] [blame] | 637 | if (pixel <= GetPixelInfoChannel(&threshold,channel)) |
cristy | 525cf96 | 2012-09-09 15:00:40 +0000 | [diff] [blame] | 638 | q[i]=(Quantum) 0; |
cristy | 188f29a | 2012-06-24 19:09:53 +0000 | [diff] [blame] | 639 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 640 | q+=GetPixelChannels(image); |
cristy | b0ea1af | 2009-11-28 20:44:46 +0000 | [diff] [blame] | 641 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 642 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
| 643 | status=MagickFalse; |
| 644 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 645 | { |
| 646 | MagickBooleanType |
| 647 | proceed; |
| 648 | |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 649 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | a12d8ba | 2012-04-29 16:33:41 +0000 | [diff] [blame] | 650 | #pragma omp critical (MagickCore_BlackThresholdImage) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 651 | #endif |
| 652 | proceed=SetImageProgress(image,ThresholdImageTag,progress++, |
| 653 | image->rows); |
| 654 | if (proceed == MagickFalse) |
| 655 | status=MagickFalse; |
| 656 | } |
| 657 | } |
| 658 | image_view=DestroyCacheView(image_view); |
| 659 | return(status); |
| 660 | } |
| 661 | |
| 662 | /* |
| 663 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 664 | % % |
| 665 | % % |
| 666 | % % |
cristy | 1eb45dd | 2009-09-25 16:38:06 +0000 | [diff] [blame] | 667 | % C l a m p I m a g e % |
| 668 | % % |
| 669 | % % |
| 670 | % % |
| 671 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 672 | % |
cristy | 7884a93 | 2012-11-04 14:33:51 +0000 | [diff] [blame] | 673 | % ClampImage() set each pixel whose value is below zero to zero and any the |
| 674 | % pixel whose value is above the quantum range to the quantum range (e.g. |
| 675 | % 65535) otherwise the pixel value remains unchanged. |
cristy | 1eb45dd | 2009-09-25 16:38:06 +0000 | [diff] [blame] | 676 | % |
cristy | f4ad9df | 2011-07-08 16:49:03 +0000 | [diff] [blame] | 677 | % The format of the ClampImage method is: |
cristy | 1eb45dd | 2009-09-25 16:38:06 +0000 | [diff] [blame] | 678 | % |
cristy | 092d71c | 2011-10-14 18:01:29 +0000 | [diff] [blame] | 679 | % MagickBooleanType ClampImage(Image *image,ExceptionInfo *exception) |
cristy | 1eb45dd | 2009-09-25 16:38:06 +0000 | [diff] [blame] | 680 | % |
| 681 | % A description of each parameter follows: |
| 682 | % |
| 683 | % o image: the image. |
| 684 | % |
cristy | 092d71c | 2011-10-14 18:01:29 +0000 | [diff] [blame] | 685 | % o exception: return any errors or warnings in this structure. |
| 686 | % |
cristy | 1eb45dd | 2009-09-25 16:38:06 +0000 | [diff] [blame] | 687 | */ |
| 688 | |
cristy | 092d71c | 2011-10-14 18:01:29 +0000 | [diff] [blame] | 689 | MagickExport MagickBooleanType ClampImage(Image *image,ExceptionInfo *exception) |
cristy | 1eb45dd | 2009-09-25 16:38:06 +0000 | [diff] [blame] | 690 | { |
cristy | 1eb45dd | 2009-09-25 16:38:06 +0000 | [diff] [blame] | 691 | #define ClampImageTag "Clamp/Image" |
| 692 | |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 693 | CacheView |
| 694 | *image_view; |
| 695 | |
cristy | 1eb45dd | 2009-09-25 16:38:06 +0000 | [diff] [blame] | 696 | MagickBooleanType |
| 697 | status; |
| 698 | |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 699 | MagickOffsetType |
| 700 | progress; |
| 701 | |
| 702 | ssize_t |
| 703 | y; |
| 704 | |
cristy | 1eb45dd | 2009-09-25 16:38:06 +0000 | [diff] [blame] | 705 | assert(image != (Image *) NULL); |
| 706 | assert(image->signature == MagickSignature); |
| 707 | if (image->debug != MagickFalse) |
| 708 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 709 | if (image->storage_class == PseudoClass) |
| 710 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 711 | register ssize_t |
cristy | 1eb45dd | 2009-09-25 16:38:06 +0000 | [diff] [blame] | 712 | i; |
| 713 | |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 714 | register PixelInfo |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 715 | *restrict q; |
cristy | 1eb45dd | 2009-09-25 16:38:06 +0000 | [diff] [blame] | 716 | |
| 717 | q=image->colormap; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 718 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 1eb45dd | 2009-09-25 16:38:06 +0000 | [diff] [blame] | 719 | { |
cristy | ddcbde6 | 2012-11-06 00:17:10 +0000 | [diff] [blame] | 720 | q->red=(double) ClampPixel(q->red); |
| 721 | q->green=(double) ClampPixel(q->green); |
| 722 | q->blue=(double) ClampPixel(q->blue); |
| 723 | q->alpha=(double) ClampPixel(q->alpha); |
cristy | 1eb45dd | 2009-09-25 16:38:06 +0000 | [diff] [blame] | 724 | q++; |
| 725 | } |
cristy | ea1a8aa | 2011-10-20 13:24:06 +0000 | [diff] [blame] | 726 | return(SyncImage(image,exception)); |
cristy | 1eb45dd | 2009-09-25 16:38:06 +0000 | [diff] [blame] | 727 | } |
| 728 | /* |
cristy | 611721d | 2009-09-25 16:42:17 +0000 | [diff] [blame] | 729 | Clamp image. |
cristy | 1eb45dd | 2009-09-25 16:38:06 +0000 | [diff] [blame] | 730 | */ |
| 731 | status=MagickTrue; |
| 732 | progress=0; |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 733 | image_view=AcquireAuthenticCacheView(image,exception); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 734 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | bd06599 | 2012-08-09 15:27:39 +0000 | [diff] [blame] | 735 | #pragma omp parallel for schedule(static,4) shared(progress,status) \ |
cristy | 5e6b259 | 2012-12-19 14:08:11 +0000 | [diff] [blame] | 736 | magick_threads(image,image,image->rows,1) |
cristy | 1eb45dd | 2009-09-25 16:38:06 +0000 | [diff] [blame] | 737 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 738 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 1eb45dd | 2009-09-25 16:38:06 +0000 | [diff] [blame] | 739 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 740 | register ssize_t |
cristy | 1eb45dd | 2009-09-25 16:38:06 +0000 | [diff] [blame] | 741 | x; |
| 742 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 743 | register Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 744 | *restrict q; |
cristy | 1eb45dd | 2009-09-25 16:38:06 +0000 | [diff] [blame] | 745 | |
| 746 | if (status == MagickFalse) |
| 747 | continue; |
| 748 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 749 | if (q == (Quantum *) NULL) |
cristy | 1eb45dd | 2009-09-25 16:38:06 +0000 | [diff] [blame] | 750 | { |
| 751 | status=MagickFalse; |
| 752 | continue; |
| 753 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 754 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 1eb45dd | 2009-09-25 16:38:06 +0000 | [diff] [blame] | 755 | { |
cristy | 5f9f246 | 2011-09-28 23:37:58 +0000 | [diff] [blame] | 756 | register ssize_t |
| 757 | i; |
| 758 | |
cristy | 883fde1 | 2013-04-08 00:50:13 +0000 | [diff] [blame] | 759 | if (GetPixelReadMask(image,q) == 0) |
cristy | 10a6c61 | 2012-01-29 21:41:05 +0000 | [diff] [blame] | 760 | { |
| 761 | q+=GetPixelChannels(image); |
| 762 | continue; |
| 763 | } |
cristy | 5f9f246 | 2011-09-28 23:37:58 +0000 | [diff] [blame] | 764 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
| 765 | { |
cristy | 5a23c55 | 2013-02-13 14:34:28 +0000 | [diff] [blame] | 766 | PixelChannel channel=GetPixelChannelChannel(image,i); |
| 767 | PixelTrait traits=GetPixelChannelTraits(image,channel); |
cristy | 454bb02 | 2013-04-23 13:03:38 +0000 | [diff] [blame] | 768 | if ((traits & UpdatePixelTrait) == 0) |
cristy | 5f9f246 | 2011-09-28 23:37:58 +0000 | [diff] [blame] | 769 | continue; |
cristy | ddcbde6 | 2012-11-06 00:17:10 +0000 | [diff] [blame] | 770 | q[i]=ClampPixel(q[i]); |
cristy | 5f9f246 | 2011-09-28 23:37:58 +0000 | [diff] [blame] | 771 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 772 | q+=GetPixelChannels(image); |
cristy | 1eb45dd | 2009-09-25 16:38:06 +0000 | [diff] [blame] | 773 | } |
| 774 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
| 775 | status=MagickFalse; |
| 776 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 777 | { |
| 778 | MagickBooleanType |
| 779 | proceed; |
| 780 | |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 781 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 782 | #pragma omp critical (MagickCore_ClampImage) |
cristy | 1eb45dd | 2009-09-25 16:38:06 +0000 | [diff] [blame] | 783 | #endif |
cristy | aa17cde | 2012-06-04 23:43:42 +0000 | [diff] [blame] | 784 | proceed=SetImageProgress(image,ClampImageTag,progress++,image->rows); |
cristy | 1eb45dd | 2009-09-25 16:38:06 +0000 | [diff] [blame] | 785 | if (proceed == MagickFalse) |
| 786 | status=MagickFalse; |
| 787 | } |
| 788 | } |
| 789 | image_view=DestroyCacheView(image_view); |
| 790 | return(status); |
| 791 | } |
| 792 | |
| 793 | /* |
| 794 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 795 | % % |
| 796 | % % |
| 797 | % % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 798 | % D e s t r o y T h r e s h o l d M a p % |
| 799 | % % |
| 800 | % % |
| 801 | % % |
| 802 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 803 | % |
| 804 | % DestroyThresholdMap() de-allocate the given ThresholdMap |
| 805 | % |
| 806 | % The format of the ListThresholdMaps method is: |
| 807 | % |
| 808 | % ThresholdMap *DestroyThresholdMap(Threshold *map) |
| 809 | % |
| 810 | % A description of each parameter follows. |
| 811 | % |
| 812 | % o map: Pointer to the Threshold map to destroy |
| 813 | % |
| 814 | */ |
| 815 | MagickExport ThresholdMap *DestroyThresholdMap(ThresholdMap *map) |
| 816 | { |
| 817 | assert(map != (ThresholdMap *) NULL); |
| 818 | if (map->map_id != (char *) NULL) |
| 819 | map->map_id=DestroyString(map->map_id); |
| 820 | if (map->description != (char *) NULL) |
| 821 | map->description=DestroyString(map->description); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 822 | if (map->levels != (ssize_t *) NULL) |
| 823 | map->levels=(ssize_t *) RelinquishMagickMemory(map->levels); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 824 | map=(ThresholdMap *) RelinquishMagickMemory(map); |
| 825 | return(map); |
| 826 | } |
| 827 | |
| 828 | /* |
| 829 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 830 | % % |
| 831 | % % |
| 832 | % % |
cristy | b9eb87b | 2011-09-29 01:15:19 +0000 | [diff] [blame] | 833 | % G e t T h r e s h o l d M a p % |
| 834 | % % |
| 835 | % % |
| 836 | % % |
| 837 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 838 | % |
| 839 | % GetThresholdMap() loads and searches one or more threshold map files for the |
| 840 | % map matching the given name or alias. |
| 841 | % |
| 842 | % The format of the GetThresholdMap method is: |
| 843 | % |
| 844 | % ThresholdMap *GetThresholdMap(const char *map_id, |
| 845 | % ExceptionInfo *exception) |
| 846 | % |
| 847 | % A description of each parameter follows. |
| 848 | % |
| 849 | % o map_id: ID of the map to look for. |
| 850 | % |
| 851 | % o exception: return any errors or warnings in this structure. |
| 852 | % |
| 853 | */ |
| 854 | MagickExport ThresholdMap *GetThresholdMap(const char *map_id, |
| 855 | ExceptionInfo *exception) |
| 856 | { |
| 857 | const StringInfo |
| 858 | *option; |
| 859 | |
| 860 | LinkedListInfo |
| 861 | *options; |
| 862 | |
| 863 | ThresholdMap |
| 864 | *map; |
| 865 | |
cristy | da9d0e4 | 2013-02-26 00:50:38 +0000 | [diff] [blame] | 866 | map=GetThresholdMapFile(MinimalThresholdMap,"built-in",map_id,exception); |
| 867 | if (map != (ThresholdMap *) NULL) |
| 868 | return(map); |
cristy | b9eb87b | 2011-09-29 01:15:19 +0000 | [diff] [blame] | 869 | options=GetConfigureOptions(ThresholdsFilename,exception); |
cristy | da9d0e4 | 2013-02-26 00:50:38 +0000 | [diff] [blame] | 870 | option=(const StringInfo *) GetNextValueInLinkedList(options); |
| 871 | while (option != (const StringInfo *) NULL) |
| 872 | { |
cristy | b9eb87b | 2011-09-29 01:15:19 +0000 | [diff] [blame] | 873 | map=GetThresholdMapFile((const char *) GetStringInfoDatum(option), |
| 874 | GetStringInfoPath(option),map_id,exception); |
cristy | da9d0e4 | 2013-02-26 00:50:38 +0000 | [diff] [blame] | 875 | if (map != (ThresholdMap *) NULL) |
cristy | 44efc98 | 2013-12-29 15:20:50 +0000 | [diff] [blame] | 876 | break; |
cristy | da9d0e4 | 2013-02-26 00:50:38 +0000 | [diff] [blame] | 877 | option=(const StringInfo *) GetNextValueInLinkedList(options); |
| 878 | } |
cristy | b9eb87b | 2011-09-29 01:15:19 +0000 | [diff] [blame] | 879 | options=DestroyConfigureOptions(options); |
cristy | 44efc98 | 2013-12-29 15:20:50 +0000 | [diff] [blame] | 880 | return(map); |
cristy | b9eb87b | 2011-09-29 01:15:19 +0000 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | /* |
| 884 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 885 | % % |
| 886 | % % |
| 887 | % % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 888 | + G e t T h r e s h o l d M a p F i l e % |
| 889 | % % |
| 890 | % % |
| 891 | % % |
| 892 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 893 | % |
| 894 | % GetThresholdMapFile() look for a given threshold map name or alias in the |
| 895 | % given XML file data, and return the allocated the map when found. |
| 896 | % |
| 897 | % The format of the ListThresholdMaps method is: |
| 898 | % |
| 899 | % ThresholdMap *GetThresholdMap(const char *xml,const char *filename, |
| 900 | % const char *map_id,ExceptionInfo *exception) |
| 901 | % |
| 902 | % A description of each parameter follows. |
| 903 | % |
| 904 | % o xml: The threshold map list in XML format. |
| 905 | % |
| 906 | % o filename: The threshold map XML filename. |
| 907 | % |
| 908 | % o map_id: ID of the map to look for in XML list. |
| 909 | % |
| 910 | % o exception: return any errors or warnings in this structure. |
| 911 | % |
| 912 | */ |
cristy | 311eb74 | 2013-02-25 19:55:37 +0000 | [diff] [blame] | 913 | static ThresholdMap *GetThresholdMapFile(const char *xml,const char *filename, |
| 914 | const char *map_id,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 915 | { |
cristy | b9eb87b | 2011-09-29 01:15:19 +0000 | [diff] [blame] | 916 | char |
| 917 | *p; |
| 918 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 919 | const char |
cristy | b9eb87b | 2011-09-29 01:15:19 +0000 | [diff] [blame] | 920 | *attribute, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 921 | *content; |
| 922 | |
| 923 | double |
| 924 | value; |
| 925 | |
cristy | b9eb87b | 2011-09-29 01:15:19 +0000 | [diff] [blame] | 926 | register ssize_t |
| 927 | i; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 928 | |
| 929 | ThresholdMap |
| 930 | *map; |
| 931 | |
cristy | b9eb87b | 2011-09-29 01:15:19 +0000 | [diff] [blame] | 932 | XMLTreeInfo |
| 933 | *description, |
| 934 | *levels, |
| 935 | *threshold, |
| 936 | *thresholds; |
| 937 | |
| 938 | (void) LogMagickEvent(ConfigureEvent,GetMagickModule(), |
| 939 | "Loading threshold map file \"%s\" ...",filename); |
| 940 | map=(ThresholdMap *) NULL; |
| 941 | thresholds=NewXMLTree(xml,exception); |
| 942 | if (thresholds == (XMLTreeInfo *) NULL) |
| 943 | return(map); |
| 944 | for (threshold=GetXMLTreeChild(thresholds,"threshold"); |
| 945 | threshold != (XMLTreeInfo *) NULL; |
| 946 | threshold=GetNextXMLTreeTag(threshold)) |
| 947 | { |
| 948 | attribute=GetXMLTreeAttribute(threshold,"map"); |
| 949 | if ((attribute != (char *) NULL) && (LocaleCompare(map_id,attribute) == 0)) |
| 950 | break; |
| 951 | attribute=GetXMLTreeAttribute(threshold,"alias"); |
| 952 | if ((attribute != (char *) NULL) && (LocaleCompare(map_id,attribute) == 0)) |
| 953 | break; |
| 954 | } |
| 955 | if (threshold == (XMLTreeInfo *) NULL) |
cristy | ed5aadc | 2013-12-29 13:30:36 +0000 | [diff] [blame] | 956 | { |
| 957 | thresholds=DestroyXMLTree(thresholds); |
| 958 | return(map); |
| 959 | } |
cristy | b9eb87b | 2011-09-29 01:15:19 +0000 | [diff] [blame] | 960 | description=GetXMLTreeChild(threshold,"description"); |
| 961 | if (description == (XMLTreeInfo *) NULL) |
| 962 | { |
| 963 | (void) ThrowMagickException(exception,GetMagickModule(),OptionError, |
| 964 | "XmlMissingElement", "<description>, map \"%s\"",map_id); |
| 965 | thresholds=DestroyXMLTree(thresholds); |
| 966 | return(map); |
| 967 | } |
| 968 | levels=GetXMLTreeChild(threshold,"levels"); |
| 969 | if (levels == (XMLTreeInfo *) NULL) |
| 970 | { |
| 971 | (void) ThrowMagickException(exception,GetMagickModule(),OptionError, |
| 972 | "XmlMissingElement", "<levels>, map \"%s\"", map_id); |
| 973 | thresholds=DestroyXMLTree(thresholds); |
| 974 | return(map); |
| 975 | } |
| 976 | map=(ThresholdMap *) AcquireMagickMemory(sizeof(ThresholdMap)); |
| 977 | if (map == (ThresholdMap *) NULL) |
| 978 | ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireThresholdMap"); |
| 979 | map->map_id=(char *) NULL; |
| 980 | map->description=(char *) NULL; |
| 981 | map->levels=(ssize_t *) NULL; |
| 982 | attribute=GetXMLTreeAttribute(threshold,"map"); |
| 983 | if (attribute != (char *) NULL) |
| 984 | map->map_id=ConstantString(attribute); |
| 985 | content=GetXMLTreeContent(description); |
| 986 | if (content != (char *) NULL) |
| 987 | map->description=ConstantString(content); |
| 988 | attribute=GetXMLTreeAttribute(levels,"width"); |
| 989 | if (attribute == (char *) NULL) |
| 990 | { |
| 991 | (void) ThrowMagickException(exception,GetMagickModule(),OptionError, |
| 992 | "XmlMissingAttribute", "<levels width>, map \"%s\"",map_id); |
| 993 | thresholds=DestroyXMLTree(thresholds); |
| 994 | map=DestroyThresholdMap(map); |
| 995 | return(map); |
| 996 | } |
| 997 | map->width=StringToUnsignedLong(attribute); |
| 998 | if (map->width == 0) |
| 999 | { |
| 1000 | (void) ThrowMagickException(exception,GetMagickModule(),OptionError, |
| 1001 | "XmlInvalidAttribute", "<levels width>, map \"%s\"",map_id); |
| 1002 | thresholds=DestroyXMLTree(thresholds); |
| 1003 | map=DestroyThresholdMap(map); |
| 1004 | return(map); |
| 1005 | } |
| 1006 | attribute=GetXMLTreeAttribute(levels,"height"); |
| 1007 | if (attribute == (char *) NULL) |
| 1008 | { |
| 1009 | (void) ThrowMagickException(exception,GetMagickModule(),OptionError, |
| 1010 | "XmlMissingAttribute", "<levels height>, map \"%s\"",map_id); |
| 1011 | thresholds=DestroyXMLTree(thresholds); |
| 1012 | map=DestroyThresholdMap(map); |
| 1013 | return(map); |
| 1014 | } |
| 1015 | map->height=StringToUnsignedLong(attribute); |
| 1016 | if (map->height == 0) |
| 1017 | { |
| 1018 | (void) ThrowMagickException(exception,GetMagickModule(),OptionError, |
| 1019 | "XmlInvalidAttribute", "<levels height>, map \"%s\"",map_id); |
| 1020 | thresholds=DestroyXMLTree(thresholds); |
| 1021 | map=DestroyThresholdMap(map); |
| 1022 | return(map); |
| 1023 | } |
| 1024 | attribute=GetXMLTreeAttribute(levels,"divisor"); |
| 1025 | if (attribute == (char *) NULL) |
| 1026 | { |
| 1027 | (void) ThrowMagickException(exception,GetMagickModule(),OptionError, |
| 1028 | "XmlMissingAttribute", "<levels divisor>, map \"%s\"",map_id); |
| 1029 | thresholds=DestroyXMLTree(thresholds); |
| 1030 | map=DestroyThresholdMap(map); |
| 1031 | return(map); |
| 1032 | } |
| 1033 | map->divisor=(ssize_t) StringToLong(attribute); |
| 1034 | if (map->divisor < 2) |
| 1035 | { |
| 1036 | (void) ThrowMagickException(exception,GetMagickModule(),OptionError, |
| 1037 | "XmlInvalidAttribute", "<levels divisor>, map \"%s\"",map_id); |
| 1038 | thresholds=DestroyXMLTree(thresholds); |
| 1039 | map=DestroyThresholdMap(map); |
| 1040 | return(map); |
| 1041 | } |
| 1042 | content=GetXMLTreeContent(levels); |
| 1043 | if (content == (char *) NULL) |
| 1044 | { |
| 1045 | (void) ThrowMagickException(exception,GetMagickModule(),OptionError, |
| 1046 | "XmlMissingContent", "<levels>, map \"%s\"",map_id); |
| 1047 | thresholds=DestroyXMLTree(thresholds); |
| 1048 | map=DestroyThresholdMap(map); |
| 1049 | return(map); |
| 1050 | } |
| 1051 | map->levels=(ssize_t *) AcquireQuantumMemory((size_t) map->width,map->height* |
| 1052 | sizeof(*map->levels)); |
| 1053 | if (map->levels == (ssize_t *) NULL) |
| 1054 | ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireThresholdMap"); |
| 1055 | for (i=0; i < (ssize_t) (map->width*map->height); i++) |
| 1056 | { |
| 1057 | map->levels[i]=(ssize_t) strtol(content,&p,10); |
| 1058 | if (p == content) |
| 1059 | { |
| 1060 | (void) ThrowMagickException(exception,GetMagickModule(),OptionError, |
| 1061 | "XmlInvalidContent", "<level> too few values, map \"%s\"",map_id); |
| 1062 | thresholds=DestroyXMLTree(thresholds); |
| 1063 | map=DestroyThresholdMap(map); |
| 1064 | return(map); |
| 1065 | } |
| 1066 | if ((map->levels[i] < 0) || (map->levels[i] > map->divisor)) |
| 1067 | { |
| 1068 | (void) ThrowMagickException(exception,GetMagickModule(),OptionError, |
| 1069 | "XmlInvalidContent", "<level> %.20g out of range, map \"%s\"", |
| 1070 | (double) map->levels[i],map_id); |
| 1071 | thresholds=DestroyXMLTree(thresholds); |
| 1072 | map=DestroyThresholdMap(map); |
| 1073 | return(map); |
| 1074 | } |
| 1075 | content=p; |
| 1076 | } |
| 1077 | value=(double) strtol(content,&p,10); |
| 1078 | (void) value; |
| 1079 | if (p != content) |
| 1080 | { |
| 1081 | (void) ThrowMagickException(exception,GetMagickModule(),OptionError, |
| 1082 | "XmlInvalidContent", "<level> too many values, map \"%s\"",map_id); |
| 1083 | thresholds=DestroyXMLTree(thresholds); |
| 1084 | map=DestroyThresholdMap(map); |
| 1085 | return(map); |
| 1086 | } |
| 1087 | thresholds=DestroyXMLTree(thresholds); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1088 | return(map); |
| 1089 | } |
| 1090 | |
| 1091 | /* |
| 1092 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1093 | % % |
| 1094 | % % |
| 1095 | % % |
| 1096 | + L i s t T h r e s h o l d M a p F i l e % |
| 1097 | % % |
| 1098 | % % |
| 1099 | % % |
| 1100 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1101 | % |
| 1102 | % ListThresholdMapFile() lists the threshold maps and their descriptions |
| 1103 | % in the given XML file data. |
| 1104 | % |
| 1105 | % The format of the ListThresholdMaps method is: |
| 1106 | % |
| 1107 | % MagickBooleanType ListThresholdMaps(FILE *file,const char*xml, |
| 1108 | % const char *filename,ExceptionInfo *exception) |
| 1109 | % |
| 1110 | % A description of each parameter follows. |
| 1111 | % |
| 1112 | % o file: An pointer to the output FILE. |
| 1113 | % |
| 1114 | % o xml: The threshold map list in XML format. |
| 1115 | % |
| 1116 | % o filename: The threshold map XML filename. |
| 1117 | % |
| 1118 | % o exception: return any errors or warnings in this structure. |
| 1119 | % |
| 1120 | */ |
| 1121 | MagickBooleanType ListThresholdMapFile(FILE *file,const char *xml, |
| 1122 | const char *filename,ExceptionInfo *exception) |
| 1123 | { |
cristy | 5f9f246 | 2011-09-28 23:37:58 +0000 | [diff] [blame] | 1124 | const char |
| 1125 | *alias, |
| 1126 | *content, |
| 1127 | *map; |
| 1128 | |
| 1129 | XMLTreeInfo |
| 1130 | *description, |
| 1131 | *threshold, |
| 1132 | *thresholds; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1133 | |
cristy | f432c63 | 2014-12-07 15:11:28 +0000 | [diff] [blame] | 1134 | assert( xml != (char *) NULL ); |
| 1135 | assert( file != (FILE *) NULL ); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1136 | (void) LogMagickEvent(ConfigureEvent,GetMagickModule(), |
| 1137 | "Loading threshold map file \"%s\" ...",filename); |
| 1138 | thresholds=NewXMLTree(xml,exception); |
cristy | f432c63 | 2014-12-07 15:11:28 +0000 | [diff] [blame] | 1139 | if ( thresholds == (XMLTreeInfo *) NULL ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1140 | return(MagickFalse); |
cristy | 1e60481 | 2011-05-19 18:07:50 +0000 | [diff] [blame] | 1141 | (void) FormatLocaleFile(file,"%-16s %-12s %s\n","Map","Alias","Description"); |
| 1142 | (void) FormatLocaleFile(file, |
| 1143 | "----------------------------------------------------\n"); |
cristy | 5f9f246 | 2011-09-28 23:37:58 +0000 | [diff] [blame] | 1144 | threshold=GetXMLTreeChild(thresholds,"threshold"); |
cristy | b9eb87b | 2011-09-29 01:15:19 +0000 | [diff] [blame] | 1145 | for ( ; threshold != (XMLTreeInfo *) NULL; |
| 1146 | threshold=GetNextXMLTreeTag(threshold)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1147 | { |
cristy | 5f9f246 | 2011-09-28 23:37:58 +0000 | [diff] [blame] | 1148 | map=GetXMLTreeAttribute(threshold,"map"); |
| 1149 | if (map == (char *) NULL) |
| 1150 | { |
| 1151 | (void) ThrowMagickException(exception,GetMagickModule(),OptionError, |
| 1152 | "XmlMissingAttribute", "<map>"); |
| 1153 | thresholds=DestroyXMLTree(thresholds); |
| 1154 | return(MagickFalse); |
| 1155 | } |
| 1156 | alias=GetXMLTreeAttribute(threshold,"alias"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1157 | description=GetXMLTreeChild(threshold,"description"); |
cristy | 5f9f246 | 2011-09-28 23:37:58 +0000 | [diff] [blame] | 1158 | if (description == (XMLTreeInfo *) NULL) |
| 1159 | { |
| 1160 | (void) ThrowMagickException(exception,GetMagickModule(),OptionError, |
cristy | b9eb87b | 2011-09-29 01:15:19 +0000 | [diff] [blame] | 1161 | "XmlMissingElement", "<description>, map \"%s\"",map); |
cristy | 5f9f246 | 2011-09-28 23:37:58 +0000 | [diff] [blame] | 1162 | thresholds=DestroyXMLTree(thresholds); |
| 1163 | return(MagickFalse); |
| 1164 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1165 | content=GetXMLTreeContent(description); |
cristy | 5f9f246 | 2011-09-28 23:37:58 +0000 | [diff] [blame] | 1166 | if (content == (char *) NULL) |
| 1167 | { |
| 1168 | (void) ThrowMagickException(exception,GetMagickModule(),OptionError, |
| 1169 | "XmlMissingContent", "<description>, map \"%s\"", map); |
| 1170 | thresholds=DestroyXMLTree(thresholds); |
| 1171 | return(MagickFalse); |
| 1172 | } |
cristy | 1e60481 | 2011-05-19 18:07:50 +0000 | [diff] [blame] | 1173 | (void) FormatLocaleFile(file,"%-16s %-12s %s\n",map,alias ? alias : "", |
| 1174 | content); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1175 | } |
| 1176 | thresholds=DestroyXMLTree(thresholds); |
| 1177 | return(MagickTrue); |
| 1178 | } |
| 1179 | |
| 1180 | /* |
| 1181 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1182 | % % |
| 1183 | % % |
| 1184 | % % |
| 1185 | % L i s t T h r e s h o l d M a p s % |
| 1186 | % % |
| 1187 | % % |
| 1188 | % % |
| 1189 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1190 | % |
| 1191 | % ListThresholdMaps() lists the threshold maps and their descriptions |
| 1192 | % as defined by "threshold.xml" to a file. |
| 1193 | % |
| 1194 | % The format of the ListThresholdMaps method is: |
| 1195 | % |
| 1196 | % MagickBooleanType ListThresholdMaps(FILE *file,ExceptionInfo *exception) |
| 1197 | % |
| 1198 | % A description of each parameter follows. |
| 1199 | % |
| 1200 | % o file: An pointer to the output FILE. |
| 1201 | % |
| 1202 | % o exception: return any errors or warnings in this structure. |
| 1203 | % |
| 1204 | */ |
| 1205 | MagickExport MagickBooleanType ListThresholdMaps(FILE *file, |
| 1206 | ExceptionInfo *exception) |
| 1207 | { |
| 1208 | const StringInfo |
| 1209 | *option; |
| 1210 | |
| 1211 | LinkedListInfo |
| 1212 | *options; |
| 1213 | |
| 1214 | MagickStatusType |
| 1215 | status; |
| 1216 | |
cristy | 3589219 | 2014-05-26 12:04:36 +0000 | [diff] [blame] | 1217 | status=MagickTrue; |
cristy | 5f9f246 | 2011-09-28 23:37:58 +0000 | [diff] [blame] | 1218 | if (file == (FILE *) NULL) |
| 1219 | file=stdout; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1220 | options=GetConfigureOptions(ThresholdsFilename,exception); |
cristy | 1e60481 | 2011-05-19 18:07:50 +0000 | [diff] [blame] | 1221 | (void) FormatLocaleFile(file, |
| 1222 | "\n Threshold Maps for Ordered Dither Operations\n"); |
cristy | 1d755c4 | 2013-02-26 12:38:44 +0000 | [diff] [blame] | 1223 | option=(const StringInfo *) GetNextValueInLinkedList(options); |
| 1224 | while (option != (const StringInfo *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1225 | { |
cristy | 1d755c4 | 2013-02-26 12:38:44 +0000 | [diff] [blame] | 1226 | (void) FormatLocaleFile(file,"\nPath: %s\n\n",GetStringInfoPath(option)); |
cristy | aae13f6 | 2013-08-15 14:41:32 +0000 | [diff] [blame] | 1227 | status&=ListThresholdMapFile(file,(const char *) GetStringInfoDatum(option), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1228 | GetStringInfoPath(option),exception); |
cristy | 1d755c4 | 2013-02-26 12:38:44 +0000 | [diff] [blame] | 1229 | option=(const StringInfo *) GetNextValueInLinkedList(options); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1230 | } |
| 1231 | options=DestroyConfigureOptions(options); |
| 1232 | return(status != 0 ? MagickTrue : MagickFalse); |
| 1233 | } |
| 1234 | |
| 1235 | /* |
| 1236 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1237 | % % |
| 1238 | % % |
| 1239 | % % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1240 | % O r d e r e d P o s t e r i z e I m a g e % |
| 1241 | % % |
| 1242 | % % |
| 1243 | % % |
| 1244 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1245 | % |
| 1246 | % OrderedPosterizeImage() will perform a ordered dither based on a number |
| 1247 | % of pre-defined dithering threshold maps, but over multiple intensity |
| 1248 | % levels, which can be different for different channels, according to the |
| 1249 | % input argument. |
| 1250 | % |
| 1251 | % The format of the OrderedPosterizeImage method is: |
| 1252 | % |
| 1253 | % MagickBooleanType OrderedPosterizeImage(Image *image, |
| 1254 | % const char *threshold_map,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1255 | % |
| 1256 | % A description of each parameter follows: |
| 1257 | % |
| 1258 | % o image: the image. |
| 1259 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1260 | % o threshold_map: A string containing the name of the threshold dither |
| 1261 | % map to use, followed by zero or more numbers representing the number |
| 1262 | % of color levels tho dither between. |
| 1263 | % |
cristy | f998fb3 | 2011-04-27 23:00:47 +0000 | [diff] [blame] | 1264 | % Any level number less than 2 will be equivalent to 2, and means only |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1265 | % binary dithering will be applied to each color channel. |
| 1266 | % |
| 1267 | % No numbers also means a 2 level (bitmap) dither will be applied to all |
| 1268 | % channels, while a single number is the number of levels applied to each |
| 1269 | % channel in sequence. More numbers will be applied in turn to each of |
| 1270 | % the color channels. |
| 1271 | % |
| 1272 | % For example: "o3x3,6" will generate a 6 level posterization of the |
| 1273 | % image with a ordered 3x3 diffused pixel dither being applied between |
| 1274 | % each level. While checker,8,8,4 will produce a 332 colormaped image |
| 1275 | % with only a single checkerboard hash pattern (50% grey) between each |
| 1276 | % color level, to basically double the number of color levels with |
| 1277 | % a bare minimim of dithering. |
| 1278 | % |
| 1279 | % o exception: return any errors or warnings in this structure. |
| 1280 | % |
| 1281 | */ |
| 1282 | MagickExport MagickBooleanType OrderedPosterizeImage(Image *image, |
| 1283 | const char *threshold_map,ExceptionInfo *exception) |
| 1284 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1285 | #define DitherImageTag "Dither/Image" |
| 1286 | |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 1287 | CacheView |
| 1288 | *image_view; |
| 1289 | |
cristy | 4e0b82a | 2011-09-29 12:47:44 +0000 | [diff] [blame] | 1290 | char |
| 1291 | token[MaxTextExtent]; |
| 1292 | |
| 1293 | const char |
| 1294 | *p; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1295 | |
cristy | 311eb74 | 2013-02-25 19:55:37 +0000 | [diff] [blame] | 1296 | double |
| 1297 | levels[CompositePixelChannel]; |
| 1298 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1299 | MagickBooleanType |
| 1300 | status; |
| 1301 | |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 1302 | MagickOffsetType |
| 1303 | progress; |
| 1304 | |
cristy | 4e0b82a | 2011-09-29 12:47:44 +0000 | [diff] [blame] | 1305 | register ssize_t |
| 1306 | i; |
| 1307 | |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 1308 | ssize_t |
| 1309 | y; |
| 1310 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1311 | ThresholdMap |
| 1312 | *map; |
| 1313 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1314 | assert(image != (Image *) NULL); |
| 1315 | assert(image->signature == MagickSignature); |
| 1316 | if (image->debug != MagickFalse) |
| 1317 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 1318 | assert(exception != (ExceptionInfo *) NULL); |
| 1319 | assert(exception->signature == MagickSignature); |
| 1320 | if (threshold_map == (const char *) NULL) |
| 1321 | return(MagickTrue); |
cristy | 4e0b82a | 2011-09-29 12:47:44 +0000 | [diff] [blame] | 1322 | p=(char *) threshold_map; |
| 1323 | while (((isspace((int) ((unsigned char) *p)) != 0) || (*p == ',')) && |
| 1324 | (*p != '\0')) |
| 1325 | p++; |
| 1326 | threshold_map=p; |
| 1327 | while (((isspace((int) ((unsigned char) *p)) == 0) && (*p != ',')) && |
| 1328 | (*p != '\0')) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1329 | { |
cristy | 4e0b82a | 2011-09-29 12:47:44 +0000 | [diff] [blame] | 1330 | if ((p-threshold_map) >= (MaxTextExtent-1)) |
| 1331 | break; |
| 1332 | token[p-threshold_map]=(*p); |
| 1333 | p++; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1334 | } |
cristy | 4e0b82a | 2011-09-29 12:47:44 +0000 | [diff] [blame] | 1335 | token[p-threshold_map]='\0'; |
| 1336 | map=GetThresholdMap(token,exception); |
| 1337 | if (map == (ThresholdMap *) NULL) |
| 1338 | { |
| 1339 | (void) ThrowMagickException(exception,GetMagickModule(),OptionError, |
| 1340 | "InvalidArgument","%s : '%s'","ordered-dither",threshold_map); |
cristy | 574cc26 | 2011-08-05 01:23:58 +0000 | [diff] [blame] | 1341 | return(MagickFalse); |
cristy | 4e0b82a | 2011-09-29 12:47:44 +0000 | [diff] [blame] | 1342 | } |
| 1343 | for (i=0; i < MaxPixelChannels; i++) |
| 1344 | levels[i]=2.0; |
| 1345 | p=strchr((char *) threshold_map,','); |
| 1346 | if ((p != (char *) NULL) && (isdigit((int) ((unsigned char) *(++p))) != 0)) |
| 1347 | for (i=0; (*p != '\0') && (i < MaxPixelChannels); i++) |
| 1348 | { |
| 1349 | GetMagickToken(p,&p,token); |
| 1350 | if (*token == ',') |
| 1351 | GetMagickToken(p,&p,token); |
cristy | dbdd0e3 | 2011-11-04 23:29:40 +0000 | [diff] [blame] | 1352 | levels[i]=StringToDouble(token,(char **) NULL); |
cristy | 4e0b82a | 2011-09-29 12:47:44 +0000 | [diff] [blame] | 1353 | } |
| 1354 | for (i=0; i < MaxPixelChannels; i++) |
| 1355 | if (fabs(levels[i]) >= 1) |
| 1356 | levels[i]-=1.0; |
| 1357 | if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse) |
| 1358 | return(MagickFalse); |
| 1359 | status=MagickTrue; |
| 1360 | progress=0; |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 1361 | image_view=AcquireAuthenticCacheView(image,exception); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 1362 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | bd06599 | 2012-08-09 15:27:39 +0000 | [diff] [blame] | 1363 | #pragma omp parallel for schedule(static,4) shared(progress,status) \ |
cristy | 5e6b259 | 2012-12-19 14:08:11 +0000 | [diff] [blame] | 1364 | magick_threads(image,image,image->rows,1) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1365 | #endif |
cristy | 4e0b82a | 2011-09-29 12:47:44 +0000 | [diff] [blame] | 1366 | for (y=0; y < (ssize_t) image->rows; y++) |
| 1367 | { |
| 1368 | register ssize_t |
| 1369 | x; |
| 1370 | |
| 1371 | register Quantum |
| 1372 | *restrict q; |
| 1373 | |
| 1374 | if (status == MagickFalse) |
| 1375 | continue; |
| 1376 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
| 1377 | if (q == (Quantum *) NULL) |
| 1378 | { |
| 1379 | status=MagickFalse; |
| 1380 | continue; |
| 1381 | } |
| 1382 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1383 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1384 | register ssize_t |
cristy | 4e0b82a | 2011-09-29 12:47:44 +0000 | [diff] [blame] | 1385 | i; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1386 | |
cristy | 4e0b82a | 2011-09-29 12:47:44 +0000 | [diff] [blame] | 1387 | ssize_t |
| 1388 | n; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1389 | |
cristy | 4e0b82a | 2011-09-29 12:47:44 +0000 | [diff] [blame] | 1390 | n=0; |
cristy | 883fde1 | 2013-04-08 00:50:13 +0000 | [diff] [blame] | 1391 | if (GetPixelReadMask(image,q) == 0) |
cristy | 10a6c61 | 2012-01-29 21:41:05 +0000 | [diff] [blame] | 1392 | { |
| 1393 | q+=GetPixelChannels(image); |
| 1394 | continue; |
| 1395 | } |
cristy | 4e0b82a | 2011-09-29 12:47:44 +0000 | [diff] [blame] | 1396 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1397 | { |
cristy | 4e0b82a | 2011-09-29 12:47:44 +0000 | [diff] [blame] | 1398 | ssize_t |
cristy | 3f5d815 | 2011-09-29 13:00:19 +0000 | [diff] [blame] | 1399 | level, |
| 1400 | threshold; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1401 | |
cristy | 5a23c55 | 2013-02-13 14:34:28 +0000 | [diff] [blame] | 1402 | PixelChannel channel=GetPixelChannelChannel(image,i); |
| 1403 | PixelTrait traits=GetPixelChannelTraits(image,channel); |
cristy | 4e0b82a | 2011-09-29 12:47:44 +0000 | [diff] [blame] | 1404 | if ((traits & UpdatePixelTrait) == 0) |
| 1405 | continue; |
cristy | 3f5d815 | 2011-09-29 13:00:19 +0000 | [diff] [blame] | 1406 | if (fabs(levels[n++]) < MagickEpsilon) |
| 1407 | continue; |
| 1408 | threshold=(ssize_t) (QuantumScale*q[i]*(levels[n]*(map->divisor-1)+1)); |
| 1409 | level=threshold/(map->divisor-1); |
| 1410 | threshold-=level*(map->divisor-1); |
cristy | ada285b | 2012-07-07 19:00:46 +0000 | [diff] [blame] | 1411 | q[i]=ClampToQuantum((double) (level+(threshold >= |
cristy | e42f658 | 2012-02-11 17:59:50 +0000 | [diff] [blame] | 1412 | map->levels[(x % map->width)+map->width*(y % map->height)]))* |
| 1413 | QuantumRange/levels[n]); |
cristy | 4e0b82a | 2011-09-29 12:47:44 +0000 | [diff] [blame] | 1414 | n++; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1415 | } |
cristy | 4e0b82a | 2011-09-29 12:47:44 +0000 | [diff] [blame] | 1416 | q+=GetPixelChannels(image); |
| 1417 | } |
| 1418 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
| 1419 | status=MagickFalse; |
| 1420 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 1421 | { |
| 1422 | MagickBooleanType |
| 1423 | proceed; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1424 | |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 1425 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 1426 | #pragma omp critical (MagickCore_OrderedPosterizeImage) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1427 | #endif |
cristy | 4e0b82a | 2011-09-29 12:47:44 +0000 | [diff] [blame] | 1428 | proceed=SetImageProgress(image,DitherImageTag,progress++,image->rows); |
| 1429 | if (proceed == MagickFalse) |
| 1430 | status=MagickFalse; |
| 1431 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1432 | } |
cristy | 4e0b82a | 2011-09-29 12:47:44 +0000 | [diff] [blame] | 1433 | image_view=DestroyCacheView(image_view); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1434 | map=DestroyThresholdMap(map); |
| 1435 | return(MagickTrue); |
| 1436 | } |
| 1437 | |
| 1438 | /* |
| 1439 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1440 | % % |
| 1441 | % % |
| 1442 | % % |
cristy | 7884a93 | 2012-11-04 14:33:51 +0000 | [diff] [blame] | 1443 | % P e r c e p t i b l e I m a g e % |
| 1444 | % % |
| 1445 | % % |
| 1446 | % % |
| 1447 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1448 | % |
cristy | 1d755c4 | 2013-02-26 12:38:44 +0000 | [diff] [blame] | 1449 | % PerceptibleImage() set each pixel whose value is less than |epsilon| to |
| 1450 | % epsilon or -epsilon (whichever is closer) otherwise the pixel value remains |
cristy | 7884a93 | 2012-11-04 14:33:51 +0000 | [diff] [blame] | 1451 | % unchanged. |
| 1452 | % |
| 1453 | % The format of the PerceptibleImage method is: |
| 1454 | % |
| 1455 | % MagickBooleanType PerceptibleImage(Image *image,const double epsilon, |
| 1456 | % ExceptionInfo *exception) |
| 1457 | % |
| 1458 | % A description of each parameter follows: |
| 1459 | % |
| 1460 | % o image: the image. |
| 1461 | % |
| 1462 | % o epsilon: the epsilon threshold (e.g. 1.0e-9). |
| 1463 | % |
| 1464 | % o exception: return any errors or warnings in this structure. |
| 1465 | % |
| 1466 | */ |
| 1467 | |
| 1468 | static inline Quantum PerceptibleThreshold(const Quantum quantum, |
| 1469 | const double epsilon) |
| 1470 | { |
| 1471 | double |
| 1472 | sign; |
| 1473 | |
| 1474 | sign=(double) quantum < 0.0 ? -1.0 : 1.0; |
| 1475 | if ((sign*quantum) >= epsilon) |
| 1476 | return(quantum); |
| 1477 | return((Quantum) (sign*epsilon)); |
| 1478 | } |
| 1479 | |
| 1480 | MagickExport MagickBooleanType PerceptibleImage(Image *image, |
| 1481 | const double epsilon,ExceptionInfo *exception) |
| 1482 | { |
| 1483 | #define PerceptibleImageTag "Perceptible/Image" |
| 1484 | |
| 1485 | CacheView |
| 1486 | *image_view; |
| 1487 | |
| 1488 | MagickBooleanType |
| 1489 | status; |
| 1490 | |
| 1491 | MagickOffsetType |
| 1492 | progress; |
| 1493 | |
| 1494 | ssize_t |
| 1495 | y; |
| 1496 | |
| 1497 | assert(image != (Image *) NULL); |
| 1498 | assert(image->signature == MagickSignature); |
| 1499 | if (image->debug != MagickFalse) |
| 1500 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 1501 | if (image->storage_class == PseudoClass) |
| 1502 | { |
| 1503 | register ssize_t |
| 1504 | i; |
| 1505 | |
| 1506 | register PixelInfo |
| 1507 | *restrict q; |
| 1508 | |
| 1509 | q=image->colormap; |
| 1510 | for (i=0; i < (ssize_t) image->colors; i++) |
| 1511 | { |
| 1512 | q->red=(double) PerceptibleThreshold(ClampToQuantum(q->red), |
| 1513 | epsilon); |
| 1514 | q->green=(double) PerceptibleThreshold(ClampToQuantum(q->green), |
| 1515 | epsilon); |
| 1516 | q->blue=(double) PerceptibleThreshold(ClampToQuantum(q->blue), |
| 1517 | epsilon); |
| 1518 | q->alpha=(double) PerceptibleThreshold(ClampToQuantum(q->alpha), |
| 1519 | epsilon); |
| 1520 | q++; |
| 1521 | } |
| 1522 | return(SyncImage(image,exception)); |
| 1523 | } |
| 1524 | /* |
| 1525 | Perceptible image. |
| 1526 | */ |
| 1527 | status=MagickTrue; |
| 1528 | progress=0; |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 1529 | image_view=AcquireAuthenticCacheView(image,exception); |
cristy | 7884a93 | 2012-11-04 14:33:51 +0000 | [diff] [blame] | 1530 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 1531 | #pragma omp parallel for schedule(static,4) shared(progress,status) \ |
cristy | 5e6b259 | 2012-12-19 14:08:11 +0000 | [diff] [blame] | 1532 | magick_threads(image,image,image->rows,1) |
cristy | 7884a93 | 2012-11-04 14:33:51 +0000 | [diff] [blame] | 1533 | #endif |
| 1534 | for (y=0; y < (ssize_t) image->rows; y++) |
| 1535 | { |
| 1536 | register ssize_t |
| 1537 | x; |
| 1538 | |
| 1539 | register Quantum |
| 1540 | *restrict q; |
| 1541 | |
| 1542 | if (status == MagickFalse) |
| 1543 | continue; |
| 1544 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
| 1545 | if (q == (Quantum *) NULL) |
| 1546 | { |
| 1547 | status=MagickFalse; |
| 1548 | continue; |
| 1549 | } |
| 1550 | for (x=0; x < (ssize_t) image->columns; x++) |
| 1551 | { |
| 1552 | register ssize_t |
| 1553 | i; |
| 1554 | |
cristy | 883fde1 | 2013-04-08 00:50:13 +0000 | [diff] [blame] | 1555 | if (GetPixelReadMask(image,q) == 0) |
cristy | 7884a93 | 2012-11-04 14:33:51 +0000 | [diff] [blame] | 1556 | { |
| 1557 | q+=GetPixelChannels(image); |
| 1558 | continue; |
| 1559 | } |
| 1560 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
| 1561 | { |
cristy | 5a23c55 | 2013-02-13 14:34:28 +0000 | [diff] [blame] | 1562 | PixelChannel channel=GetPixelChannelChannel(image,i); |
| 1563 | PixelTrait traits=GetPixelChannelTraits(image,channel); |
cristy | 7884a93 | 2012-11-04 14:33:51 +0000 | [diff] [blame] | 1564 | if (traits == UndefinedPixelTrait) |
| 1565 | continue; |
| 1566 | q[i]=PerceptibleThreshold(q[i],epsilon); |
| 1567 | } |
| 1568 | q+=GetPixelChannels(image); |
| 1569 | } |
| 1570 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
| 1571 | status=MagickFalse; |
| 1572 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 1573 | { |
| 1574 | MagickBooleanType |
| 1575 | proceed; |
| 1576 | |
| 1577 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 1578 | #pragma omp critical (MagickCore_PerceptibleImage) |
| 1579 | #endif |
| 1580 | proceed=SetImageProgress(image,PerceptibleImageTag,progress++,image->rows); |
| 1581 | if (proceed == MagickFalse) |
| 1582 | status=MagickFalse; |
| 1583 | } |
| 1584 | } |
| 1585 | image_view=DestroyCacheView(image_view); |
| 1586 | return(status); |
| 1587 | } |
| 1588 | |
| 1589 | /* |
| 1590 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1591 | % % |
| 1592 | % % |
| 1593 | % % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1594 | % R a n d o m T h r e s h o l d I m a g e % |
| 1595 | % % |
| 1596 | % % |
| 1597 | % % |
| 1598 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1599 | % |
| 1600 | % RandomThresholdImage() changes the value of individual pixels based on the |
| 1601 | % intensity of each pixel compared to a random threshold. The result is a |
| 1602 | % low-contrast, two color image. |
| 1603 | % |
| 1604 | % The format of the RandomThresholdImage method is: |
| 1605 | % |
cristy | f4ad9df | 2011-07-08 16:49:03 +0000 | [diff] [blame] | 1606 | % MagickBooleanType RandomThresholdImage(Image *image, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1607 | % const char *thresholds,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1608 | % |
| 1609 | % A description of each parameter follows: |
| 1610 | % |
| 1611 | % o image: the image. |
| 1612 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1613 | % o thresholds: a geometry string containing low,high thresholds. If the |
| 1614 | % string contains 2x2, 3x3, or 4x4, an ordered dither of order 2, 3, or 4 |
| 1615 | % is performed instead. |
| 1616 | % |
| 1617 | % o exception: return any errors or warnings in this structure. |
| 1618 | % |
| 1619 | */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1620 | MagickExport MagickBooleanType RandomThresholdImage(Image *image, |
| 1621 | const char *thresholds,ExceptionInfo *exception) |
| 1622 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1623 | #define ThresholdImageTag "Threshold/Image" |
| 1624 | |
cristy | fa11211 | 2010-01-04 17:48:07 +0000 | [diff] [blame] | 1625 | CacheView |
| 1626 | *image_view; |
| 1627 | |
cristy | 311eb74 | 2013-02-25 19:55:37 +0000 | [diff] [blame] | 1628 | double |
| 1629 | min_threshold, |
| 1630 | max_threshold; |
| 1631 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1632 | GeometryInfo |
| 1633 | geometry_info; |
| 1634 | |
| 1635 | MagickStatusType |
| 1636 | flags; |
| 1637 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1638 | MagickBooleanType |
| 1639 | status; |
| 1640 | |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 1641 | MagickOffsetType |
| 1642 | progress; |
| 1643 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1644 | PixelInfo |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1645 | threshold; |
| 1646 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1647 | RandomInfo |
cristy | fa11211 | 2010-01-04 17:48:07 +0000 | [diff] [blame] | 1648 | **restrict random_info; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1649 | |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 1650 | ssize_t |
| 1651 | y; |
| 1652 | |
glennrp | b36143f | 2012-09-24 18:26:55 +0000 | [diff] [blame] | 1653 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 57340e0 | 2012-05-05 00:53:23 +0000 | [diff] [blame] | 1654 | unsigned long |
| 1655 | key; |
glennrp | b36143f | 2012-09-24 18:26:55 +0000 | [diff] [blame] | 1656 | #endif |
cristy | 57340e0 | 2012-05-05 00:53:23 +0000 | [diff] [blame] | 1657 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1658 | assert(image != (Image *) NULL); |
| 1659 | assert(image->signature == MagickSignature); |
| 1660 | if (image->debug != MagickFalse) |
| 1661 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 1662 | assert(exception != (ExceptionInfo *) NULL); |
| 1663 | assert(exception->signature == MagickSignature); |
| 1664 | if (thresholds == (const char *) NULL) |
| 1665 | return(MagickTrue); |
cristy | e745265 | 2012-04-14 01:34:21 +0000 | [diff] [blame] | 1666 | if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse) |
| 1667 | return(MagickFalse); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1668 | GetPixelInfo(image,&threshold); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1669 | min_threshold=0.0; |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1670 | max_threshold=(double) QuantumRange; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1671 | flags=ParseGeometry(thresholds,&geometry_info); |
| 1672 | min_threshold=geometry_info.rho; |
| 1673 | max_threshold=geometry_info.sigma; |
| 1674 | if ((flags & SigmaValue) == 0) |
| 1675 | max_threshold=min_threshold; |
| 1676 | if (strchr(thresholds,'%') != (char *) NULL) |
| 1677 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1678 | max_threshold*=(double) (0.01*QuantumRange); |
| 1679 | min_threshold*=(double) (0.01*QuantumRange); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1680 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1681 | /* |
| 1682 | Random threshold image. |
| 1683 | */ |
| 1684 | status=MagickTrue; |
| 1685 | progress=0; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1686 | random_info=AcquireRandomInfoThreadSet(); |
glennrp | b36143f | 2012-09-24 18:26:55 +0000 | [diff] [blame] | 1687 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 57340e0 | 2012-05-05 00:53:23 +0000 | [diff] [blame] | 1688 | key=GetRandomSecretKey(random_info[0]); |
glennrp | b36143f | 2012-09-24 18:26:55 +0000 | [diff] [blame] | 1689 | #endif |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 1690 | image_view=AcquireAuthenticCacheView(image,exception); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 1691 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | bd06599 | 2012-08-09 15:27:39 +0000 | [diff] [blame] | 1692 | #pragma omp parallel for schedule(static,4) shared(progress,status) \ |
cristy | 5e6b259 | 2012-12-19 14:08:11 +0000 | [diff] [blame] | 1693 | magick_threads(image,image,image->rows,key == ~0UL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1694 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1695 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1696 | { |
cristy | 5c9e6f2 | 2010-09-17 17:31:01 +0000 | [diff] [blame] | 1697 | const int |
| 1698 | id = GetOpenMPThreadId(); |
cristy | 6ebe97c | 2010-07-03 01:17:28 +0000 | [diff] [blame] | 1699 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1700 | register Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1701 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1702 | |
cristy | 5c9e6f2 | 2010-09-17 17:31:01 +0000 | [diff] [blame] | 1703 | register ssize_t |
| 1704 | x; |
| 1705 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1706 | if (status == MagickFalse) |
| 1707 | continue; |
| 1708 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 1709 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1710 | { |
| 1711 | status=MagickFalse; |
| 1712 | continue; |
| 1713 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1714 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1715 | { |
cristy | 5f9f246 | 2011-09-28 23:37:58 +0000 | [diff] [blame] | 1716 | register ssize_t |
| 1717 | i; |
| 1718 | |
cristy | 883fde1 | 2013-04-08 00:50:13 +0000 | [diff] [blame] | 1719 | if (GetPixelReadMask(image,q) == 0) |
cristy | 10a6c61 | 2012-01-29 21:41:05 +0000 | [diff] [blame] | 1720 | { |
| 1721 | q+=GetPixelChannels(image); |
| 1722 | continue; |
| 1723 | } |
cristy | 5f9f246 | 2011-09-28 23:37:58 +0000 | [diff] [blame] | 1724 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
| 1725 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1726 | double |
cristy | 5f9f246 | 2011-09-28 23:37:58 +0000 | [diff] [blame] | 1727 | threshold; |
| 1728 | |
cristy | 5a23c55 | 2013-02-13 14:34:28 +0000 | [diff] [blame] | 1729 | PixelChannel channel=GetPixelChannelChannel(image,i); |
| 1730 | PixelTrait traits=GetPixelChannelTraits(image,channel); |
cristy | 5f9f246 | 2011-09-28 23:37:58 +0000 | [diff] [blame] | 1731 | if ((traits & UpdatePixelTrait) == 0) |
| 1732 | continue; |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1733 | if ((double) q[i] < min_threshold) |
cristy | 5f9f246 | 2011-09-28 23:37:58 +0000 | [diff] [blame] | 1734 | threshold=min_threshold; |
| 1735 | else |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1736 | if ((double) q[i] > max_threshold) |
cristy | 5f9f246 | 2011-09-28 23:37:58 +0000 | [diff] [blame] | 1737 | threshold=max_threshold; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1738 | else |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1739 | threshold=(double) (QuantumRange* |
cristy | 5f9f246 | 2011-09-28 23:37:58 +0000 | [diff] [blame] | 1740 | GetPseudoRandomValue(random_info[id])); |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1741 | q[i]=(double) q[i] <= threshold ? 0 : QuantumRange; |
cristy | 5f9f246 | 2011-09-28 23:37:58 +0000 | [diff] [blame] | 1742 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1743 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1744 | } |
| 1745 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
| 1746 | status=MagickFalse; |
| 1747 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 1748 | { |
| 1749 | MagickBooleanType |
| 1750 | proceed; |
| 1751 | |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 1752 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 1753 | #pragma omp critical (MagickCore_RandomThresholdImage) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1754 | #endif |
| 1755 | proceed=SetImageProgress(image,ThresholdImageTag,progress++, |
| 1756 | image->rows); |
| 1757 | if (proceed == MagickFalse) |
| 1758 | status=MagickFalse; |
| 1759 | } |
| 1760 | } |
| 1761 | image_view=DestroyCacheView(image_view); |
| 1762 | random_info=DestroyRandomInfoThreadSet(random_info); |
| 1763 | return(status); |
| 1764 | } |
| 1765 | |
| 1766 | /* |
| 1767 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1768 | % % |
| 1769 | % % |
| 1770 | % % |
| 1771 | % W h i t e T h r e s h o l d I m a g e % |
| 1772 | % % |
| 1773 | % % |
| 1774 | % % |
| 1775 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1776 | % |
| 1777 | % WhiteThresholdImage() is like ThresholdImage() but forces all pixels above |
cristy | 4e10130 | 2009-09-17 12:49:12 +0000 | [diff] [blame] | 1778 | % the threshold into white while leaving all pixels at or below the threshold |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1779 | % unchanged. |
| 1780 | % |
| 1781 | % The format of the WhiteThresholdImage method is: |
| 1782 | % |
cristy | f4ad9df | 2011-07-08 16:49:03 +0000 | [diff] [blame] | 1783 | % MagickBooleanType WhiteThresholdImage(Image *image, |
| 1784 | % const char *threshold,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1785 | % |
| 1786 | % A description of each parameter follows: |
| 1787 | % |
| 1788 | % o image: the image. |
| 1789 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1790 | % o threshold: Define the threshold value. |
| 1791 | % |
| 1792 | % o exception: return any errors or warnings in this structure. |
| 1793 | % |
| 1794 | */ |
| 1795 | MagickExport MagickBooleanType WhiteThresholdImage(Image *image, |
cristy | f4ad9df | 2011-07-08 16:49:03 +0000 | [diff] [blame] | 1796 | const char *thresholds,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1797 | { |
| 1798 | #define ThresholdImageTag "Threshold/Image" |
| 1799 | |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 1800 | CacheView |
| 1801 | *image_view; |
| 1802 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1803 | GeometryInfo |
| 1804 | geometry_info; |
| 1805 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1806 | MagickBooleanType |
| 1807 | status; |
| 1808 | |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 1809 | MagickOffsetType |
| 1810 | progress; |
| 1811 | |
cristy | d680338 | 2012-04-10 01:41:25 +0000 | [diff] [blame] | 1812 | PixelInfo |
cristy | a12d8ba | 2012-04-29 16:33:41 +0000 | [diff] [blame] | 1813 | threshold; |
cristy | 5f9f246 | 2011-09-28 23:37:58 +0000 | [diff] [blame] | 1814 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1815 | MagickStatusType |
| 1816 | flags; |
| 1817 | |
cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 1818 | ssize_t |
| 1819 | y; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1820 | |
| 1821 | assert(image != (Image *) NULL); |
| 1822 | assert(image->signature == MagickSignature); |
| 1823 | if (image->debug != MagickFalse) |
| 1824 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 1825 | if (thresholds == (const char *) NULL) |
| 1826 | return(MagickTrue); |
cristy | 574cc26 | 2011-08-05 01:23:58 +0000 | [diff] [blame] | 1827 | if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1828 | return(MagickFalse); |
cristy | 23e55c0 | 2012-04-10 01:21:56 +0000 | [diff] [blame] | 1829 | if (IsGrayColorspace(image->colorspace) != MagickFalse) |
cristy | 6c312f0 | 2013-05-19 22:12:34 +0000 | [diff] [blame] | 1830 | (void) TransformImageColorspace(image,sRGBColorspace,exception); |
cristy | a12d8ba | 2012-04-29 16:33:41 +0000 | [diff] [blame] | 1831 | GetPixelInfo(image,&threshold); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1832 | flags=ParseGeometry(thresholds,&geometry_info); |
cristy | a12d8ba | 2012-04-29 16:33:41 +0000 | [diff] [blame] | 1833 | threshold.red=geometry_info.rho; |
| 1834 | threshold.green=geometry_info.rho; |
| 1835 | threshold.blue=geometry_info.rho; |
| 1836 | threshold.black=geometry_info.rho; |
| 1837 | threshold.alpha=100.0; |
cristy | 5f9f246 | 2011-09-28 23:37:58 +0000 | [diff] [blame] | 1838 | if ((flags & SigmaValue) != 0) |
cristy | a12d8ba | 2012-04-29 16:33:41 +0000 | [diff] [blame] | 1839 | threshold.green=geometry_info.sigma; |
cristy | 5f9f246 | 2011-09-28 23:37:58 +0000 | [diff] [blame] | 1840 | if ((flags & XiValue) != 0) |
cristy | a12d8ba | 2012-04-29 16:33:41 +0000 | [diff] [blame] | 1841 | threshold.blue=geometry_info.xi; |
cristy | 5f9f246 | 2011-09-28 23:37:58 +0000 | [diff] [blame] | 1842 | if ((flags & PsiValue) != 0) |
cristy | a12d8ba | 2012-04-29 16:33:41 +0000 | [diff] [blame] | 1843 | threshold.alpha=geometry_info.psi; |
| 1844 | if (threshold.colorspace == CMYKColorspace) |
cristy | d680338 | 2012-04-10 01:41:25 +0000 | [diff] [blame] | 1845 | { |
| 1846 | if ((flags & PsiValue) != 0) |
cristy | a12d8ba | 2012-04-29 16:33:41 +0000 | [diff] [blame] | 1847 | threshold.black=geometry_info.psi; |
cristy | d680338 | 2012-04-10 01:41:25 +0000 | [diff] [blame] | 1848 | if ((flags & ChiValue) != 0) |
cristy | a12d8ba | 2012-04-29 16:33:41 +0000 | [diff] [blame] | 1849 | threshold.alpha=geometry_info.chi; |
| 1850 | } |
| 1851 | if ((flags & PercentValue) != 0) |
| 1852 | { |
cristy | 65d4e5e | 2012-10-17 12:22:24 +0000 | [diff] [blame] | 1853 | threshold.red*=(MagickRealType) (QuantumRange/100.0); |
| 1854 | threshold.green*=(MagickRealType) (QuantumRange/100.0); |
| 1855 | threshold.blue*=(MagickRealType) (QuantumRange/100.0); |
| 1856 | threshold.black*=(MagickRealType) (QuantumRange/100.0); |
| 1857 | threshold.alpha*=(MagickRealType) (QuantumRange/100.0); |
cristy | d680338 | 2012-04-10 01:41:25 +0000 | [diff] [blame] | 1858 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1859 | /* |
| 1860 | White threshold image. |
| 1861 | */ |
| 1862 | status=MagickTrue; |
| 1863 | progress=0; |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 1864 | image_view=AcquireAuthenticCacheView(image,exception); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 1865 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | bd06599 | 2012-08-09 15:27:39 +0000 | [diff] [blame] | 1866 | #pragma omp parallel for schedule(static,4) shared(progress,status) \ |
cristy | 5e6b259 | 2012-12-19 14:08:11 +0000 | [diff] [blame] | 1867 | magick_threads(image,image,image->rows,1) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1868 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1869 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1870 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1871 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1872 | x; |
| 1873 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1874 | register Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1875 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1876 | |
| 1877 | if (status == MagickFalse) |
| 1878 | continue; |
| 1879 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 1880 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1881 | { |
| 1882 | status=MagickFalse; |
| 1883 | continue; |
| 1884 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1885 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1886 | { |
cristy | 81629aa | 2012-07-12 20:08:52 +0000 | [diff] [blame] | 1887 | double |
| 1888 | pixel; |
| 1889 | |
cristy | 188f29a | 2012-06-24 19:09:53 +0000 | [diff] [blame] | 1890 | register ssize_t |
| 1891 | i; |
| 1892 | |
cristy | 883fde1 | 2013-04-08 00:50:13 +0000 | [diff] [blame] | 1893 | if (GetPixelReadMask(image,q) == 0) |
cristy | 10a6c61 | 2012-01-29 21:41:05 +0000 | [diff] [blame] | 1894 | { |
| 1895 | q+=GetPixelChannels(image); |
| 1896 | continue; |
| 1897 | } |
cristy | f13c594 | 2012-08-08 23:50:11 +0000 | [diff] [blame] | 1898 | pixel=GetPixelIntensity(image,q); |
cristy | 188f29a | 2012-06-24 19:09:53 +0000 | [diff] [blame] | 1899 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
| 1900 | { |
cristy | 5a23c55 | 2013-02-13 14:34:28 +0000 | [diff] [blame] | 1901 | PixelChannel channel=GetPixelChannelChannel(image,i); |
| 1902 | PixelTrait traits=GetPixelChannelTraits(image,channel); |
cristy | 188f29a | 2012-06-24 19:09:53 +0000 | [diff] [blame] | 1903 | if ((traits & UpdatePixelTrait) == 0) |
| 1904 | continue; |
cristy | 81629aa | 2012-07-12 20:08:52 +0000 | [diff] [blame] | 1905 | if (image->channel_mask != DefaultChannels) |
cristy | f13c594 | 2012-08-08 23:50:11 +0000 | [diff] [blame] | 1906 | pixel=(double) q[i]; |
cristy | 81629aa | 2012-07-12 20:08:52 +0000 | [diff] [blame] | 1907 | if (pixel > GetPixelInfoChannel(&threshold,channel)) |
cristy | 188f29a | 2012-06-24 19:09:53 +0000 | [diff] [blame] | 1908 | q[i]=QuantumRange; |
| 1909 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1910 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1911 | } |
| 1912 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
| 1913 | status=MagickFalse; |
| 1914 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 1915 | { |
| 1916 | MagickBooleanType |
| 1917 | proceed; |
| 1918 | |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 1919 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | a12d8ba | 2012-04-29 16:33:41 +0000 | [diff] [blame] | 1920 | #pragma omp critical (MagickCore_WhiteThresholdImage) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1921 | #endif |
| 1922 | proceed=SetImageProgress(image,ThresholdImageTag,progress++, |
| 1923 | image->rows); |
| 1924 | if (proceed == MagickFalse) |
| 1925 | status=MagickFalse; |
| 1926 | } |
| 1927 | } |
| 1928 | image_view=DestroyCacheView(image_view); |
| 1929 | return(status); |
| 1930 | } |