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