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