blob: fe95a1d1a24a9e478b21eb375c54c2e70af2513c [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
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% %
cristy1454be72011-12-19 01:52:48 +000020% Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% 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*/
cristy4c08aed2011-07-01 19:47:50 +000043#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"
cristy23e55c02012-04-10 01:21:56 +000051#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000052#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"
cristy4e0b82a2011-09-29 12:47:44 +000085#include "MagickCore/token.h"
cristy4c08aed2011-07-01 19:47:50 +000086#include "MagickCore/transform.h"
87#include "MagickCore/xml-tree.h"
cristy433d1182011-09-04 13:38:52 +000088#include "MagickCore/xml-tree-private.h"
cristy3ed852e2009-09-05 21:47:34 +000089
90/*
91 Define declarations.
92*/
93#define ThresholdsFilename "thresholds.xml"
94
95/*
96 Typedef declarations.
97*/
98struct _ThresholdMap
99{
100 char
101 *map_id,
102 *description;
103
cristybb503372010-05-27 20:51:26 +0000104 size_t
cristy3ed852e2009-09-05 21:47:34 +0000105 width,
106 height;
107
cristybb503372010-05-27 20:51:26 +0000108 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000109 divisor,
110 *levels;
111};
112
113/*
cristybd0ebf02011-09-29 01:19:42 +0000114 Forward declarations.
115*/
116static ThresholdMap
117 *GetThresholdMapFile(const char *,const char *,const char *,ExceptionInfo *);
118
119/*
cristy3ed852e2009-09-05 21:47:34 +0000120%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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%
cristyde5cc632011-07-18 14:47:00 +0000137% Image *AdaptiveThresholdImage(const Image *image,const size_t width,
138% const size_t height,const double bias,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000139%
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%
cristyde5cc632011-07-18 14:47:00 +0000148% o bias: the mean bias.
cristy3ed852e2009-09-05 21:47:34 +0000149%
150% o exception: return any errors or warnings in this structure.
151%
152*/
153MagickExport Image *AdaptiveThresholdImage(const Image *image,
cristyde5cc632011-07-18 14:47:00 +0000154 const size_t width,const size_t height,const double bias,
cristy3ed852e2009-09-05 21:47:34 +0000155 ExceptionInfo *exception)
156{
cristyde5cc632011-07-18 14:47:00 +0000157#define AdaptiveThresholdImageTag "AdaptiveThreshold/Image"
cristy3ed852e2009-09-05 21:47:34 +0000158
cristyc4c8d132010-01-07 01:58:38 +0000159 CacheView
160 *image_view,
161 *threshold_view;
162
cristy3ed852e2009-09-05 21:47:34 +0000163 Image
164 *threshold_image;
165
cristy3ed852e2009-09-05 21:47:34 +0000166 MagickBooleanType
167 status;
168
cristy5f959472010-05-27 22:19:46 +0000169 MagickOffsetType
170 progress;
171
cristyde5cc632011-07-18 14:47:00 +0000172 MagickSizeType
cristy3ed852e2009-09-05 21:47:34 +0000173 number_pixels;
174
cristy5f959472010-05-27 22:19:46 +0000175 ssize_t
176 y;
177
cristyde5cc632011-07-18 14:47:00 +0000178 /*
179 Initialize threshold image attributes.
180 */
181 assert(image != (Image *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000182 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);
cristyde5cc632011-07-18 14:47:00 +0000187 if ((width % 2) == 0)
188 ThrowImageException(OptionError,"KernelWidthMustBeAnOddNumber");
189 threshold_image=CloneImage(image,image->columns,image->rows,MagickTrue,
190 exception);
cristy3ed852e2009-09-05 21:47:34 +0000191 if (threshold_image == (Image *) NULL)
192 return((Image *) NULL);
cristyb9eb87b2011-09-29 01:15:19 +0000193 status=SetImageStorageClass(threshold_image,DirectClass,exception);
194 if (status == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000195 {
cristy3ed852e2009-09-05 21:47:34 +0000196 threshold_image=DestroyImage(threshold_image);
197 return((Image *) NULL);
198 }
199 /*
cristyde5cc632011-07-18 14:47:00 +0000200 Threshold image.
cristy3ed852e2009-09-05 21:47:34 +0000201 */
202 status=MagickTrue;
203 progress=0;
cristyde5cc632011-07-18 14:47:00 +0000204 number_pixels=(MagickSizeType) width*height;
cristydb070952012-04-20 14:33:00 +0000205 image_view=AcquireVirtualCacheView(image,exception);
206 threshold_view=AcquireAuthenticCacheView(threshold_image,exception);
cristyb5d5f722009-11-04 03:03:49 +0000207#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000208 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +0000209 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +0000210#endif
cristybb503372010-05-27 20:51:26 +0000211 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000212 {
cristy4c08aed2011-07-01 19:47:50 +0000213 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000214 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000215
cristy4c08aed2011-07-01 19:47:50 +0000216 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000217 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000218
cristyde5cc632011-07-18 14:47:00 +0000219 register ssize_t
220 x;
221
cristyde5cc632011-07-18 14:47:00 +0000222 ssize_t
223 center;
224
cristy3ed852e2009-09-05 21:47:34 +0000225 if (status == MagickFalse)
226 continue;
cristyd99b0962010-05-29 23:14:26 +0000227 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
cristyde5cc632011-07-18 14:47:00 +0000228 (height/2L),image->columns+width,height,exception);
229 q=QueueCacheViewAuthenticPixels(threshold_view,0,y,threshold_image->columns,
230 1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000231 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000232 {
233 status=MagickFalse;
234 continue;
235 }
cristy5f9f2462011-09-28 23:37:58 +0000236 center=(ssize_t) GetPixelChannels(image)*(image->columns+width)*(height/2L)+
cristya0312c92011-07-23 21:04:30 +0000237 GetPixelChannels(image)*(width/2);
cristybb503372010-05-27 20:51:26 +0000238 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000239 {
cristybb503372010-05-27 20:51:26 +0000240 register ssize_t
cristyde5cc632011-07-18 14:47:00 +0000241 i;
cristy3ed852e2009-09-05 21:47:34 +0000242
cristya0312c92011-07-23 21:04:30 +0000243 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy3ed852e2009-09-05 21:47:34 +0000244 {
cristya19f1d72012-08-07 18:24:38 +0000245 double
cristyde5cc632011-07-18 14:47:00 +0000246 mean,
247 pixel;
248
249 PixelChannel
250 channel;
251
252 PixelTrait
253 threshold_traits,
254 traits;
255
256 register const Quantum
257 *restrict pixels;
258
259 register ssize_t
260 u;
261
262 ssize_t
263 v;
264
cristye2a912b2011-12-05 20:02:07 +0000265 channel=GetPixelChannelMapChannel(image,i);
cristyabace412011-12-11 15:56:53 +0000266 traits=GetPixelChannelMapTraits(image,channel);
cristyde5cc632011-07-18 14:47:00 +0000267 threshold_traits=GetPixelChannelMapTraits(threshold_image,channel);
cristy010d7d12011-08-31 01:02:48 +0000268 if ((traits == UndefinedPixelTrait) ||
269 (threshold_traits == UndefinedPixelTrait))
cristyde5cc632011-07-18 14:47:00 +0000270 continue;
cristy1eced092012-08-10 23:10:56 +0000271 if (((threshold_traits & CopyPixelTrait) != 0) ||
272 (GetPixelMask(image,p) != 0))
cristyde5cc632011-07-18 14:47:00 +0000273 {
cristy0beccfa2011-09-25 20:47:53 +0000274 SetPixelChannel(threshold_image,channel,p[center+i],q);
cristyde5cc632011-07-18 14:47:00 +0000275 continue;
276 }
277 pixels=p;
278 pixel=0.0;
279 for (v=0; v < (ssize_t) height; v++)
cristy3ed852e2009-09-05 21:47:34 +0000280 {
cristyde5cc632011-07-18 14:47:00 +0000281 for (u=0; u < (ssize_t) width; u++)
282 {
283 pixel+=pixels[i];
cristya0312c92011-07-23 21:04:30 +0000284 pixels+=GetPixelChannels(image);
cristyde5cc632011-07-18 14:47:00 +0000285 }
cristya0312c92011-07-23 21:04:30 +0000286 pixels+=image->columns*GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000287 }
cristya19f1d72012-08-07 18:24:38 +0000288 mean=(double) (pixel/number_pixels+bias);
289 SetPixelChannel(threshold_image,channel,(Quantum) ((double)
cristy5f9f2462011-09-28 23:37:58 +0000290 p[center+i] <= mean ? 0 : QuantumRange),q);
cristy3ed852e2009-09-05 21:47:34 +0000291 }
cristya0312c92011-07-23 21:04:30 +0000292 p+=GetPixelChannels(image);
293 q+=GetPixelChannels(threshold_image);
cristy3ed852e2009-09-05 21:47:34 +0000294 }
cristyde5cc632011-07-18 14:47:00 +0000295 if (SyncCacheViewAuthenticPixels(threshold_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000296 status=MagickFalse;
297 if (image->progress_monitor != (MagickProgressMonitor) NULL)
298 {
299 MagickBooleanType
300 proceed;
301
cristyb5d5f722009-11-04 03:03:49 +0000302#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000303 #pragma omp critical (MagickCore_AdaptiveThresholdImage)
cristy3ed852e2009-09-05 21:47:34 +0000304#endif
cristyde5cc632011-07-18 14:47:00 +0000305 proceed=SetImageProgress(image,AdaptiveThresholdImageTag,progress++,
cristy3ed852e2009-09-05 21:47:34 +0000306 image->rows);
307 if (proceed == MagickFalse)
308 status=MagickFalse;
309 }
310 }
cristyde5cc632011-07-18 14:47:00 +0000311 threshold_image->type=image->type;
cristy3ed852e2009-09-05 21:47:34 +0000312 threshold_view=DestroyCacheView(threshold_view);
313 image_view=DestroyCacheView(image_view);
314 if (status == MagickFalse)
315 threshold_image=DestroyImage(threshold_image);
316 return(threshold_image);
317}
318
319/*
320%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
321% %
322% %
323% %
324% B i l e v e l I m a g e %
325% %
326% %
327% %
328%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
329%
330% BilevelImage() changes the value of individual pixels based on the
331% intensity of each pixel channel. The result is a high-contrast image.
332%
333% More precisely each channel value of the image is 'thresholded' so that if
334% it is equal to or less than the given value it is set to zero, while any
335% value greater than that give is set to it maximum or QuantumRange.
336%
337% This function is what is used to implement the "-threshold" operator for
338% the command line API.
339%
340% If the default channel setting is given the image is thresholded using just
341% the gray 'intensity' of the image, rather than the individual channels.
342%
cristyf4ad9df2011-07-08 16:49:03 +0000343% The format of the BilevelImage method is:
cristy3ed852e2009-09-05 21:47:34 +0000344%
cristye941a752011-10-15 01:52:48 +0000345% MagickBooleanType BilevelImage(Image *image,const double threshold,
346% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000347%
348% A description of each parameter follows:
349%
350% o image: the image.
351%
cristy3ed852e2009-09-05 21:47:34 +0000352% o threshold: define the threshold values.
353%
cristye941a752011-10-15 01:52:48 +0000354% o exception: return any errors or warnings in this structure.
355%
cristyf89cb1d2011-07-07 01:24:37 +0000356% Aside: You can get the same results as operator using LevelImages()
cristy3ed852e2009-09-05 21:47:34 +0000357% with the 'threshold' value for both the black_point and the white_point.
358%
359*/
cristye941a752011-10-15 01:52:48 +0000360MagickExport MagickBooleanType BilevelImage(Image *image,const double threshold,
361 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000362{
363#define ThresholdImageTag "Threshold/Image"
364
cristyc4c8d132010-01-07 01:58:38 +0000365 CacheView
366 *image_view;
367
cristy3ed852e2009-09-05 21:47:34 +0000368 MagickBooleanType
369 status;
370
cristy5f959472010-05-27 22:19:46 +0000371 MagickOffsetType
372 progress;
373
374 ssize_t
375 y;
376
cristy3ed852e2009-09-05 21:47:34 +0000377 assert(image != (Image *) NULL);
378 assert(image->signature == MagickSignature);
379 if (image->debug != MagickFalse)
380 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy574cc262011-08-05 01:23:58 +0000381 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000382 return(MagickFalse);
383 /*
384 Bilevel threshold image.
385 */
386 status=MagickTrue;
387 progress=0;
cristydb070952012-04-20 14:33:00 +0000388 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +0000389#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristybd065992012-08-09 15:27:39 +0000390 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +0000391 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +0000392#endif
cristybb503372010-05-27 20:51:26 +0000393 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000394 {
cristybb503372010-05-27 20:51:26 +0000395 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000396 x;
397
cristy4c08aed2011-07-01 19:47:50 +0000398 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000399 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000400
401 if (status == MagickFalse)
402 continue;
403 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000404 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000405 {
406 status=MagickFalse;
407 continue;
408 }
cristy805b6a02011-08-09 00:59:35 +0000409 for (x=0; x < (ssize_t) image->columns; x++)
410 {
cristy171e2352012-07-10 17:43:12 +0000411 double
412 pixel;
413
cristy95111202011-08-09 19:41:42 +0000414 register ssize_t
415 i;
416
cristy10a6c612012-01-29 21:41:05 +0000417 if (GetPixelMask(image,q) != 0)
418 {
419 q+=GetPixelChannels(image);
420 continue;
421 }
cristyf13c5942012-08-08 23:50:11 +0000422 pixel=GetPixelIntensity(image,q);
cristy95111202011-08-09 19:41:42 +0000423 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
424 {
cristyabace412011-12-11 15:56:53 +0000425 PixelChannel
426 channel;
427
cristy95111202011-08-09 19:41:42 +0000428 PixelTrait
429 traits;
430
cristyabace412011-12-11 15:56:53 +0000431 channel=GetPixelChannelMapChannel(image,i);
432 traits=GetPixelChannelMapTraits(image,channel);
cristyd09f8802012-02-04 16:44:10 +0000433 if ((traits & UpdatePixelTrait) == 0)
434 continue;
cristya64f4b92012-07-11 23:59:00 +0000435 if (image->channel_mask != DefaultChannels)
cristyf13c5942012-08-08 23:50:11 +0000436 pixel=(double) q[i];
cristy171e2352012-07-10 17:43:12 +0000437 q[i]=(Quantum) (pixel <= threshold ? 0 : QuantumRange);
cristy95111202011-08-09 19:41:42 +0000438 }
cristy805b6a02011-08-09 00:59:35 +0000439 q+=GetPixelChannels(image);
440 }
cristy3ed852e2009-09-05 21:47:34 +0000441 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
442 status=MagickFalse;
443 if (image->progress_monitor != (MagickProgressMonitor) NULL)
444 {
445 MagickBooleanType
446 proceed;
447
cristyb5d5f722009-11-04 03:03:49 +0000448#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000449 #pragma omp critical (MagickCore_BilevelImage)
cristy3ed852e2009-09-05 21:47:34 +0000450#endif
451 proceed=SetImageProgress(image,ThresholdImageTag,progress++,
452 image->rows);
453 if (proceed == MagickFalse)
454 status=MagickFalse;
455 }
456 }
457 image_view=DestroyCacheView(image_view);
458 return(status);
459}
460
461/*
462%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
463% %
464% %
465% %
466% B l a c k T h r e s h o l d I m a g e %
467% %
468% %
469% %
470%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
471%
472% BlackThresholdImage() is like ThresholdImage() but forces all pixels below
cristy4e101302009-09-17 12:49:12 +0000473% the threshold into black while leaving all pixels at or above the threshold
cristy3ed852e2009-09-05 21:47:34 +0000474% unchanged.
475%
476% The format of the BlackThresholdImage method is:
477%
cristyf4ad9df2011-07-08 16:49:03 +0000478% MagickBooleanType BlackThresholdImage(Image *image,
479% const char *threshold,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000480%
481% A description of each parameter follows:
482%
483% o image: the image.
484%
cristy5f9f2462011-09-28 23:37:58 +0000485% o threshold: define the threshold value.
cristy3ed852e2009-09-05 21:47:34 +0000486%
487% o exception: return any errors or warnings in this structure.
488%
489*/
490MagickExport MagickBooleanType BlackThresholdImage(Image *image,
cristyf4ad9df2011-07-08 16:49:03 +0000491 const char *thresholds,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000492{
493#define ThresholdImageTag "Threshold/Image"
494
cristyc4c8d132010-01-07 01:58:38 +0000495 CacheView
496 *image_view;
497
cristy3ed852e2009-09-05 21:47:34 +0000498 GeometryInfo
499 geometry_info;
500
cristy3ed852e2009-09-05 21:47:34 +0000501 MagickBooleanType
502 status;
503
cristy5f959472010-05-27 22:19:46 +0000504 MagickOffsetType
505 progress;
506
cristyd6803382012-04-10 01:41:25 +0000507 PixelInfo
cristya12d8ba2012-04-29 16:33:41 +0000508 threshold;
cristy3ed852e2009-09-05 21:47:34 +0000509
510 MagickStatusType
511 flags;
512
cristy5f959472010-05-27 22:19:46 +0000513 ssize_t
514 y;
515
cristy3ed852e2009-09-05 21:47:34 +0000516 assert(image != (Image *) NULL);
517 assert(image->signature == MagickSignature);
518 if (image->debug != MagickFalse)
519 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
520 if (thresholds == (const char *) NULL)
521 return(MagickTrue);
cristy574cc262011-08-05 01:23:58 +0000522 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000523 return(MagickFalse);
cristy23e55c02012-04-10 01:21:56 +0000524 if (IsGrayColorspace(image->colorspace) != MagickFalse)
cristyb09db112012-07-11 12:04:31 +0000525 (void) TransformImageColorspace(image,RGBColorspace,exception);
cristya12d8ba2012-04-29 16:33:41 +0000526 GetPixelInfo(image,&threshold);
cristy3ed852e2009-09-05 21:47:34 +0000527 flags=ParseGeometry(thresholds,&geometry_info);
cristya12d8ba2012-04-29 16:33:41 +0000528 threshold.red=geometry_info.rho;
529 threshold.green=geometry_info.rho;
530 threshold.blue=geometry_info.rho;
531 threshold.black=geometry_info.rho;
532 threshold.alpha=100.0;
cristy5f9f2462011-09-28 23:37:58 +0000533 if ((flags & SigmaValue) != 0)
cristya12d8ba2012-04-29 16:33:41 +0000534 threshold.green=geometry_info.sigma;
cristy5f9f2462011-09-28 23:37:58 +0000535 if ((flags & XiValue) != 0)
cristya12d8ba2012-04-29 16:33:41 +0000536 threshold.blue=geometry_info.xi;
cristy5f9f2462011-09-28 23:37:58 +0000537 if ((flags & PsiValue) != 0)
cristya12d8ba2012-04-29 16:33:41 +0000538 threshold.alpha=geometry_info.psi;
539 if (threshold.colorspace == CMYKColorspace)
cristyd6803382012-04-10 01:41:25 +0000540 {
541 if ((flags & PsiValue) != 0)
cristya12d8ba2012-04-29 16:33:41 +0000542 threshold.black=geometry_info.psi;
cristyd6803382012-04-10 01:41:25 +0000543 if ((flags & ChiValue) != 0)
cristya12d8ba2012-04-29 16:33:41 +0000544 threshold.alpha=geometry_info.chi;
545 }
546 if ((flags & PercentValue) != 0)
547 {
548 threshold.red*=(QuantumRange/100.0);
549 threshold.green*=(QuantumRange/100.0);
550 threshold.blue*=(QuantumRange/100.0);
551 threshold.black*=(QuantumRange/100.0);
552 threshold.alpha*=(QuantumRange/100.0);
cristyd6803382012-04-10 01:41:25 +0000553 }
cristy3ed852e2009-09-05 21:47:34 +0000554 /*
cristy5f9f2462011-09-28 23:37:58 +0000555 White threshold image.
cristy3ed852e2009-09-05 21:47:34 +0000556 */
557 status=MagickTrue;
558 progress=0;
cristydb070952012-04-20 14:33:00 +0000559 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +0000560#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristybd065992012-08-09 15:27:39 +0000561 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +0000562 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +0000563#endif
cristybb503372010-05-27 20:51:26 +0000564 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000565 {
cristybb503372010-05-27 20:51:26 +0000566 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000567 x;
568
cristy4c08aed2011-07-01 19:47:50 +0000569 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000570 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000571
572 if (status == MagickFalse)
573 continue;
574 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000575 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000576 {
577 status=MagickFalse;
578 continue;
579 }
cristybb503372010-05-27 20:51:26 +0000580 for (x=0; x < (ssize_t) image->columns; x++)
cristyb0ea1af2009-11-28 20:44:46 +0000581 {
cristy81629aa2012-07-12 20:08:52 +0000582 double
583 pixel;
584
cristyc4567182012-06-24 20:55:08 +0000585 register ssize_t
586 i;
587
cristy10a6c612012-01-29 21:41:05 +0000588 if (GetPixelMask(image,q) != 0)
589 {
590 q+=GetPixelChannels(image);
591 continue;
592 }
cristyf13c5942012-08-08 23:50:11 +0000593 pixel=GetPixelIntensity(image,q);
cristy188f29a2012-06-24 19:09:53 +0000594 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
595 {
596 PixelChannel
597 channel;
598
599 PixelTrait
600 traits;
601
602 channel=GetPixelChannelMapChannel(image,i);
603 traits=GetPixelChannelMapTraits(image,channel);
604 if ((traits & UpdatePixelTrait) == 0)
605 continue;
cristy81629aa2012-07-12 20:08:52 +0000606 if (image->channel_mask != DefaultChannels)
cristyf13c5942012-08-08 23:50:11 +0000607 pixel=(double) q[i];
cristy81629aa2012-07-12 20:08:52 +0000608 if (pixel <= GetPixelInfoChannel(&threshold,channel))
cristy188f29a2012-06-24 19:09:53 +0000609 q[i]=0;
610 }
cristyed231572011-07-14 02:18:59 +0000611 q+=GetPixelChannels(image);
cristyb0ea1af2009-11-28 20:44:46 +0000612 }
cristy3ed852e2009-09-05 21:47:34 +0000613 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
614 status=MagickFalse;
615 if (image->progress_monitor != (MagickProgressMonitor) NULL)
616 {
617 MagickBooleanType
618 proceed;
619
cristyb5d5f722009-11-04 03:03:49 +0000620#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristya12d8ba2012-04-29 16:33:41 +0000621 #pragma omp critical (MagickCore_BlackThresholdImage)
cristy3ed852e2009-09-05 21:47:34 +0000622#endif
623 proceed=SetImageProgress(image,ThresholdImageTag,progress++,
624 image->rows);
625 if (proceed == MagickFalse)
626 status=MagickFalse;
627 }
628 }
629 image_view=DestroyCacheView(image_view);
630 return(status);
631}
632
633/*
634%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
635% %
636% %
637% %
cristy1eb45dd2009-09-25 16:38:06 +0000638% C l a m p I m a g e %
639% %
640% %
641% %
642%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
643%
cristyecb0c6d2009-09-25 16:50:09 +0000644% ClampImage() restricts the color range from 0 to the quantum depth.
cristy1eb45dd2009-09-25 16:38:06 +0000645%
cristyf4ad9df2011-07-08 16:49:03 +0000646% The format of the ClampImage method is:
cristy1eb45dd2009-09-25 16:38:06 +0000647%
cristy092d71c2011-10-14 18:01:29 +0000648% MagickBooleanType ClampImage(Image *image,ExceptionInfo *exception)
cristy1eb45dd2009-09-25 16:38:06 +0000649%
650% A description of each parameter follows:
651%
652% o image: the image.
653%
cristy092d71c2011-10-14 18:01:29 +0000654% o exception: return any errors or warnings in this structure.
655%
cristy1eb45dd2009-09-25 16:38:06 +0000656*/
657
cristy75ffdb72010-01-07 17:40:12 +0000658static inline Quantum ClampToUnsignedQuantum(const Quantum quantum)
cristy1eb45dd2009-09-25 16:38:06 +0000659{
cristy1eb45dd2009-09-25 16:38:06 +0000660 if (quantum <= 0)
661 return(0);
662 if (quantum >= QuantumRange)
663 return(QuantumRange);
664 return(quantum);
cristy1eb45dd2009-09-25 16:38:06 +0000665}
666
cristy092d71c2011-10-14 18:01:29 +0000667MagickExport MagickBooleanType ClampImage(Image *image,ExceptionInfo *exception)
cristy1eb45dd2009-09-25 16:38:06 +0000668{
cristy1eb45dd2009-09-25 16:38:06 +0000669#define ClampImageTag "Clamp/Image"
670
cristyc4c8d132010-01-07 01:58:38 +0000671 CacheView
672 *image_view;
673
cristy1eb45dd2009-09-25 16:38:06 +0000674 MagickBooleanType
675 status;
676
cristy5f959472010-05-27 22:19:46 +0000677 MagickOffsetType
678 progress;
679
680 ssize_t
681 y;
682
cristy191c0b72012-08-12 16:29:52 +0000683#if !defined(MAGICKCORE_HDRI_SUPPORT)
684 return(MagickTrue);
685#else
cristy1eb45dd2009-09-25 16:38:06 +0000686 assert(image != (Image *) NULL);
687 assert(image->signature == MagickSignature);
688 if (image->debug != MagickFalse)
689 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
690 if (image->storage_class == PseudoClass)
691 {
cristybb503372010-05-27 20:51:26 +0000692 register ssize_t
cristy1eb45dd2009-09-25 16:38:06 +0000693 i;
694
cristy101ab702011-10-13 13:06:32 +0000695 register PixelInfo
cristyc47d1f82009-11-26 01:44:43 +0000696 *restrict q;
cristy1eb45dd2009-09-25 16:38:06 +0000697
698 q=image->colormap;
cristybb503372010-05-27 20:51:26 +0000699 for (i=0; i < (ssize_t) image->colors; i++)
cristy1eb45dd2009-09-25 16:38:06 +0000700 {
cristye42f6582012-02-11 17:59:50 +0000701 q->red=(double) ClampToUnsignedQuantum(ClampToQuantum(q->red));
702 q->green=(double) ClampToUnsignedQuantum(ClampToQuantum(q->green));
703 q->blue=(double) ClampToUnsignedQuantum(ClampToQuantum(q->blue));
704 q->alpha=(double) ClampToUnsignedQuantum(ClampToQuantum(q->alpha));
cristy1eb45dd2009-09-25 16:38:06 +0000705 q++;
706 }
cristyea1a8aa2011-10-20 13:24:06 +0000707 return(SyncImage(image,exception));
cristy1eb45dd2009-09-25 16:38:06 +0000708 }
709 /*
cristy611721d2009-09-25 16:42:17 +0000710 Clamp image.
cristy1eb45dd2009-09-25 16:38:06 +0000711 */
712 status=MagickTrue;
713 progress=0;
cristydb070952012-04-20 14:33:00 +0000714 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +0000715#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristybd065992012-08-09 15:27:39 +0000716 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +0000717 dynamic_number_threads(image,image->columns,image->rows,1)
cristy1eb45dd2009-09-25 16:38:06 +0000718#endif
cristybb503372010-05-27 20:51:26 +0000719 for (y=0; y < (ssize_t) image->rows; y++)
cristy1eb45dd2009-09-25 16:38:06 +0000720 {
cristybb503372010-05-27 20:51:26 +0000721 register ssize_t
cristy1eb45dd2009-09-25 16:38:06 +0000722 x;
723
cristy4c08aed2011-07-01 19:47:50 +0000724 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000725 *restrict q;
cristy1eb45dd2009-09-25 16:38:06 +0000726
727 if (status == MagickFalse)
728 continue;
729 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000730 if (q == (Quantum *) NULL)
cristy1eb45dd2009-09-25 16:38:06 +0000731 {
732 status=MagickFalse;
733 continue;
734 }
cristybb503372010-05-27 20:51:26 +0000735 for (x=0; x < (ssize_t) image->columns; x++)
cristy1eb45dd2009-09-25 16:38:06 +0000736 {
cristy5f9f2462011-09-28 23:37:58 +0000737 register ssize_t
738 i;
739
cristy10a6c612012-01-29 21:41:05 +0000740 if (GetPixelMask(image,q) != 0)
741 {
742 q+=GetPixelChannels(image);
743 continue;
744 }
cristy5f9f2462011-09-28 23:37:58 +0000745 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
746 {
cristyabace412011-12-11 15:56:53 +0000747 PixelChannel
748 channel;
749
cristy5f9f2462011-09-28 23:37:58 +0000750 PixelTrait
751 traits;
752
cristyabace412011-12-11 15:56:53 +0000753 channel=GetPixelChannelMapChannel(image,i);
754 traits=GetPixelChannelMapTraits(image,channel);
cristy5f9f2462011-09-28 23:37:58 +0000755 if (traits == UndefinedPixelTrait)
756 continue;
757 q[i]=ClampToUnsignedQuantum(q[i]);
758 }
cristyed231572011-07-14 02:18:59 +0000759 q+=GetPixelChannels(image);
cristy1eb45dd2009-09-25 16:38:06 +0000760 }
761 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
762 status=MagickFalse;
763 if (image->progress_monitor != (MagickProgressMonitor) NULL)
764 {
765 MagickBooleanType
766 proceed;
767
cristyb5d5f722009-11-04 03:03:49 +0000768#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000769 #pragma omp critical (MagickCore_ClampImage)
cristy1eb45dd2009-09-25 16:38:06 +0000770#endif
cristyaa17cde2012-06-04 23:43:42 +0000771 proceed=SetImageProgress(image,ClampImageTag,progress++,image->rows);
cristy1eb45dd2009-09-25 16:38:06 +0000772 if (proceed == MagickFalse)
773 status=MagickFalse;
774 }
775 }
776 image_view=DestroyCacheView(image_view);
777 return(status);
cristy191c0b72012-08-12 16:29:52 +0000778#endif
cristy1eb45dd2009-09-25 16:38:06 +0000779}
780
781/*
782%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
783% %
784% %
785% %
cristy3ed852e2009-09-05 21:47:34 +0000786% D e s t r o y T h r e s h o l d M a p %
787% %
788% %
789% %
790%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
791%
792% DestroyThresholdMap() de-allocate the given ThresholdMap
793%
794% The format of the ListThresholdMaps method is:
795%
796% ThresholdMap *DestroyThresholdMap(Threshold *map)
797%
798% A description of each parameter follows.
799%
800% o map: Pointer to the Threshold map to destroy
801%
802*/
803MagickExport ThresholdMap *DestroyThresholdMap(ThresholdMap *map)
804{
805 assert(map != (ThresholdMap *) NULL);
806 if (map->map_id != (char *) NULL)
807 map->map_id=DestroyString(map->map_id);
808 if (map->description != (char *) NULL)
809 map->description=DestroyString(map->description);
cristybb503372010-05-27 20:51:26 +0000810 if (map->levels != (ssize_t *) NULL)
811 map->levels=(ssize_t *) RelinquishMagickMemory(map->levels);
cristy3ed852e2009-09-05 21:47:34 +0000812 map=(ThresholdMap *) RelinquishMagickMemory(map);
813 return(map);
814}
815
816/*
817%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
818% %
819% %
820% %
cristyb9eb87b2011-09-29 01:15:19 +0000821% G e t T h r e s h o l d M a p %
822% %
823% %
824% %
825%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
826%
827% GetThresholdMap() loads and searches one or more threshold map files for the
828% map matching the given name or alias.
829%
830% The format of the GetThresholdMap method is:
831%
832% ThresholdMap *GetThresholdMap(const char *map_id,
833% ExceptionInfo *exception)
834%
835% A description of each parameter follows.
836%
837% o map_id: ID of the map to look for.
838%
839% o exception: return any errors or warnings in this structure.
840%
841*/
842MagickExport ThresholdMap *GetThresholdMap(const char *map_id,
843 ExceptionInfo *exception)
844{
845 const StringInfo
846 *option;
847
848 LinkedListInfo
849 *options;
850
851 ThresholdMap
852 *map;
853
854 map=(ThresholdMap *)NULL;
855 options=GetConfigureOptions(ThresholdsFilename,exception);
856 while ((option=(const StringInfo *) GetNextValueInLinkedList(options)) !=
857 (const StringInfo *) NULL && (map == (ThresholdMap *) NULL))
858 map=GetThresholdMapFile((const char *) GetStringInfoDatum(option),
859 GetStringInfoPath(option),map_id,exception);
860 options=DestroyConfigureOptions(options);
861 return(map);
862}
863
864/*
865%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
866% %
867% %
868% %
cristy3ed852e2009-09-05 21:47:34 +0000869+ G e t T h r e s h o l d M a p F i l e %
870% %
871% %
872% %
873%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
874%
875% GetThresholdMapFile() look for a given threshold map name or alias in the
876% given XML file data, and return the allocated the map when found.
877%
878% The format of the ListThresholdMaps method is:
879%
880% ThresholdMap *GetThresholdMap(const char *xml,const char *filename,
881% const char *map_id,ExceptionInfo *exception)
882%
883% A description of each parameter follows.
884%
885% o xml: The threshold map list in XML format.
886%
887% o filename: The threshold map XML filename.
888%
889% o map_id: ID of the map to look for in XML list.
890%
891% o exception: return any errors or warnings in this structure.
892%
893*/
cristybd0ebf02011-09-29 01:19:42 +0000894static ThresholdMap *GetThresholdMapFile(const char *xml,
cristy3ed852e2009-09-05 21:47:34 +0000895 const char *filename,const char *map_id,ExceptionInfo *exception)
896{
cristyb9eb87b2011-09-29 01:15:19 +0000897 char
898 *p;
899
cristy3ed852e2009-09-05 21:47:34 +0000900 const char
cristyb9eb87b2011-09-29 01:15:19 +0000901 *attribute,
cristy3ed852e2009-09-05 21:47:34 +0000902 *content;
903
904 double
905 value;
906
cristyb9eb87b2011-09-29 01:15:19 +0000907 register ssize_t
908 i;
cristy3ed852e2009-09-05 21:47:34 +0000909
910 ThresholdMap
911 *map;
912
cristyb9eb87b2011-09-29 01:15:19 +0000913 XMLTreeInfo
914 *description,
915 *levels,
916 *threshold,
917 *thresholds;
918
919 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
920 "Loading threshold map file \"%s\" ...",filename);
921 map=(ThresholdMap *) NULL;
922 thresholds=NewXMLTree(xml,exception);
923 if (thresholds == (XMLTreeInfo *) NULL)
924 return(map);
925 for (threshold=GetXMLTreeChild(thresholds,"threshold");
926 threshold != (XMLTreeInfo *) NULL;
927 threshold=GetNextXMLTreeTag(threshold))
928 {
929 attribute=GetXMLTreeAttribute(threshold,"map");
930 if ((attribute != (char *) NULL) && (LocaleCompare(map_id,attribute) == 0))
931 break;
932 attribute=GetXMLTreeAttribute(threshold,"alias");
933 if ((attribute != (char *) NULL) && (LocaleCompare(map_id,attribute) == 0))
934 break;
935 }
936 if (threshold == (XMLTreeInfo *) NULL)
937 return(map);
938 description=GetXMLTreeChild(threshold,"description");
939 if (description == (XMLTreeInfo *) NULL)
940 {
941 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
942 "XmlMissingElement", "<description>, map \"%s\"",map_id);
943 thresholds=DestroyXMLTree(thresholds);
944 return(map);
945 }
946 levels=GetXMLTreeChild(threshold,"levels");
947 if (levels == (XMLTreeInfo *) NULL)
948 {
949 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
950 "XmlMissingElement", "<levels>, map \"%s\"", map_id);
951 thresholds=DestroyXMLTree(thresholds);
952 return(map);
953 }
954 map=(ThresholdMap *) AcquireMagickMemory(sizeof(ThresholdMap));
955 if (map == (ThresholdMap *) NULL)
956 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireThresholdMap");
957 map->map_id=(char *) NULL;
958 map->description=(char *) NULL;
959 map->levels=(ssize_t *) NULL;
960 attribute=GetXMLTreeAttribute(threshold,"map");
961 if (attribute != (char *) NULL)
962 map->map_id=ConstantString(attribute);
963 content=GetXMLTreeContent(description);
964 if (content != (char *) NULL)
965 map->description=ConstantString(content);
966 attribute=GetXMLTreeAttribute(levels,"width");
967 if (attribute == (char *) NULL)
968 {
969 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
970 "XmlMissingAttribute", "<levels width>, map \"%s\"",map_id);
971 thresholds=DestroyXMLTree(thresholds);
972 map=DestroyThresholdMap(map);
973 return(map);
974 }
975 map->width=StringToUnsignedLong(attribute);
976 if (map->width == 0)
977 {
978 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
979 "XmlInvalidAttribute", "<levels width>, map \"%s\"",map_id);
980 thresholds=DestroyXMLTree(thresholds);
981 map=DestroyThresholdMap(map);
982 return(map);
983 }
984 attribute=GetXMLTreeAttribute(levels,"height");
985 if (attribute == (char *) NULL)
986 {
987 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
988 "XmlMissingAttribute", "<levels height>, map \"%s\"",map_id);
989 thresholds=DestroyXMLTree(thresholds);
990 map=DestroyThresholdMap(map);
991 return(map);
992 }
993 map->height=StringToUnsignedLong(attribute);
994 if (map->height == 0)
995 {
996 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
997 "XmlInvalidAttribute", "<levels height>, map \"%s\"",map_id);
998 thresholds=DestroyXMLTree(thresholds);
999 map=DestroyThresholdMap(map);
1000 return(map);
1001 }
1002 attribute=GetXMLTreeAttribute(levels,"divisor");
1003 if (attribute == (char *) NULL)
1004 {
1005 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1006 "XmlMissingAttribute", "<levels divisor>, map \"%s\"",map_id);
1007 thresholds=DestroyXMLTree(thresholds);
1008 map=DestroyThresholdMap(map);
1009 return(map);
1010 }
1011 map->divisor=(ssize_t) StringToLong(attribute);
1012 if (map->divisor < 2)
1013 {
1014 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1015 "XmlInvalidAttribute", "<levels divisor>, map \"%s\"",map_id);
1016 thresholds=DestroyXMLTree(thresholds);
1017 map=DestroyThresholdMap(map);
1018 return(map);
1019 }
1020 content=GetXMLTreeContent(levels);
1021 if (content == (char *) NULL)
1022 {
1023 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1024 "XmlMissingContent", "<levels>, map \"%s\"",map_id);
1025 thresholds=DestroyXMLTree(thresholds);
1026 map=DestroyThresholdMap(map);
1027 return(map);
1028 }
1029 map->levels=(ssize_t *) AcquireQuantumMemory((size_t) map->width,map->height*
1030 sizeof(*map->levels));
1031 if (map->levels == (ssize_t *) NULL)
1032 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireThresholdMap");
1033 for (i=0; i < (ssize_t) (map->width*map->height); i++)
1034 {
1035 map->levels[i]=(ssize_t) strtol(content,&p,10);
1036 if (p == content)
1037 {
1038 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1039 "XmlInvalidContent", "<level> too few values, map \"%s\"",map_id);
1040 thresholds=DestroyXMLTree(thresholds);
1041 map=DestroyThresholdMap(map);
1042 return(map);
1043 }
1044 if ((map->levels[i] < 0) || (map->levels[i] > map->divisor))
1045 {
1046 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1047 "XmlInvalidContent", "<level> %.20g out of range, map \"%s\"",
1048 (double) map->levels[i],map_id);
1049 thresholds=DestroyXMLTree(thresholds);
1050 map=DestroyThresholdMap(map);
1051 return(map);
1052 }
1053 content=p;
1054 }
1055 value=(double) strtol(content,&p,10);
1056 (void) value;
1057 if (p != content)
1058 {
1059 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1060 "XmlInvalidContent", "<level> too many values, map \"%s\"",map_id);
1061 thresholds=DestroyXMLTree(thresholds);
1062 map=DestroyThresholdMap(map);
1063 return(map);
1064 }
1065 thresholds=DestroyXMLTree(thresholds);
cristy3ed852e2009-09-05 21:47:34 +00001066 return(map);
1067}
1068
1069/*
1070%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1071% %
1072% %
1073% %
1074+ L i s t T h r e s h o l d M a p F i l e %
1075% %
1076% %
1077% %
1078%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1079%
1080% ListThresholdMapFile() lists the threshold maps and their descriptions
1081% in the given XML file data.
1082%
1083% The format of the ListThresholdMaps method is:
1084%
1085% MagickBooleanType ListThresholdMaps(FILE *file,const char*xml,
1086% const char *filename,ExceptionInfo *exception)
1087%
1088% A description of each parameter follows.
1089%
1090% o file: An pointer to the output FILE.
1091%
1092% o xml: The threshold map list in XML format.
1093%
1094% o filename: The threshold map XML filename.
1095%
1096% o exception: return any errors or warnings in this structure.
1097%
1098*/
1099MagickBooleanType ListThresholdMapFile(FILE *file,const char *xml,
1100 const char *filename,ExceptionInfo *exception)
1101{
cristy5f9f2462011-09-28 23:37:58 +00001102 const char
1103 *alias,
1104 *content,
1105 *map;
1106
1107 XMLTreeInfo
1108 *description,
1109 *threshold,
1110 *thresholds;
cristy3ed852e2009-09-05 21:47:34 +00001111
1112 assert( xml != (char *)NULL );
1113 assert( file != (FILE *)NULL );
cristy3ed852e2009-09-05 21:47:34 +00001114 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1115 "Loading threshold map file \"%s\" ...",filename);
1116 thresholds=NewXMLTree(xml,exception);
1117 if ( thresholds == (XMLTreeInfo *)NULL )
1118 return(MagickFalse);
cristy1e604812011-05-19 18:07:50 +00001119 (void) FormatLocaleFile(file,"%-16s %-12s %s\n","Map","Alias","Description");
1120 (void) FormatLocaleFile(file,
1121 "----------------------------------------------------\n");
cristy5f9f2462011-09-28 23:37:58 +00001122 threshold=GetXMLTreeChild(thresholds,"threshold");
cristyb9eb87b2011-09-29 01:15:19 +00001123 for ( ; threshold != (XMLTreeInfo *) NULL;
1124 threshold=GetNextXMLTreeTag(threshold))
cristy3ed852e2009-09-05 21:47:34 +00001125 {
cristy5f9f2462011-09-28 23:37:58 +00001126 map=GetXMLTreeAttribute(threshold,"map");
1127 if (map == (char *) NULL)
1128 {
1129 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1130 "XmlMissingAttribute", "<map>");
1131 thresholds=DestroyXMLTree(thresholds);
1132 return(MagickFalse);
1133 }
1134 alias=GetXMLTreeAttribute(threshold,"alias");
cristy3ed852e2009-09-05 21:47:34 +00001135 description=GetXMLTreeChild(threshold,"description");
cristy5f9f2462011-09-28 23:37:58 +00001136 if (description == (XMLTreeInfo *) NULL)
1137 {
1138 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
cristyb9eb87b2011-09-29 01:15:19 +00001139 "XmlMissingElement", "<description>, map \"%s\"",map);
cristy5f9f2462011-09-28 23:37:58 +00001140 thresholds=DestroyXMLTree(thresholds);
1141 return(MagickFalse);
1142 }
cristy3ed852e2009-09-05 21:47:34 +00001143 content=GetXMLTreeContent(description);
cristy5f9f2462011-09-28 23:37:58 +00001144 if (content == (char *) NULL)
1145 {
1146 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1147 "XmlMissingContent", "<description>, map \"%s\"", map);
1148 thresholds=DestroyXMLTree(thresholds);
1149 return(MagickFalse);
1150 }
cristy1e604812011-05-19 18:07:50 +00001151 (void) FormatLocaleFile(file,"%-16s %-12s %s\n",map,alias ? alias : "",
1152 content);
cristy3ed852e2009-09-05 21:47:34 +00001153 }
1154 thresholds=DestroyXMLTree(thresholds);
1155 return(MagickTrue);
1156}
1157
1158/*
1159%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1160% %
1161% %
1162% %
1163% L i s t T h r e s h o l d M a p s %
1164% %
1165% %
1166% %
1167%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1168%
1169% ListThresholdMaps() lists the threshold maps and their descriptions
1170% as defined by "threshold.xml" to a file.
1171%
1172% The format of the ListThresholdMaps method is:
1173%
1174% MagickBooleanType ListThresholdMaps(FILE *file,ExceptionInfo *exception)
1175%
1176% A description of each parameter follows.
1177%
1178% o file: An pointer to the output FILE.
1179%
1180% o exception: return any errors or warnings in this structure.
1181%
1182*/
1183MagickExport MagickBooleanType ListThresholdMaps(FILE *file,
1184 ExceptionInfo *exception)
1185{
1186 const StringInfo
1187 *option;
1188
1189 LinkedListInfo
1190 *options;
1191
1192 MagickStatusType
1193 status;
1194
1195 status=MagickFalse;
cristy5f9f2462011-09-28 23:37:58 +00001196 if (file == (FILE *) NULL)
1197 file=stdout;
cristy3ed852e2009-09-05 21:47:34 +00001198 options=GetConfigureOptions(ThresholdsFilename,exception);
cristy1e604812011-05-19 18:07:50 +00001199 (void) FormatLocaleFile(file,
1200 "\n Threshold Maps for Ordered Dither Operations\n");
cristy5f9f2462011-09-28 23:37:58 +00001201 while ((option=(const StringInfo *) GetNextValueInLinkedList(options)) !=
1202 (const StringInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001203 {
cristyb51dff52011-05-19 16:55:47 +00001204 (void) FormatLocaleFile(file,"\nPATH: %s\n\n",GetStringInfoPath(option));
cristy3ed852e2009-09-05 21:47:34 +00001205 status|=ListThresholdMapFile(file,(const char *) GetStringInfoDatum(option),
1206 GetStringInfoPath(option),exception);
1207 }
1208 options=DestroyConfigureOptions(options);
1209 return(status != 0 ? MagickTrue : MagickFalse);
1210}
1211
1212/*
1213%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1214% %
1215% %
1216% %
cristy3ed852e2009-09-05 21:47:34 +00001217% O r d e r e d P o s t e r i z e I m a g e %
1218% %
1219% %
1220% %
1221%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1222%
1223% OrderedPosterizeImage() will perform a ordered dither based on a number
1224% of pre-defined dithering threshold maps, but over multiple intensity
1225% levels, which can be different for different channels, according to the
1226% input argument.
1227%
1228% The format of the OrderedPosterizeImage method is:
1229%
1230% MagickBooleanType OrderedPosterizeImage(Image *image,
1231% const char *threshold_map,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001232%
1233% A description of each parameter follows:
1234%
1235% o image: the image.
1236%
cristy3ed852e2009-09-05 21:47:34 +00001237% o threshold_map: A string containing the name of the threshold dither
1238% map to use, followed by zero or more numbers representing the number
1239% of color levels tho dither between.
1240%
cristyf998fb32011-04-27 23:00:47 +00001241% Any level number less than 2 will be equivalent to 2, and means only
cristy3ed852e2009-09-05 21:47:34 +00001242% binary dithering will be applied to each color channel.
1243%
1244% No numbers also means a 2 level (bitmap) dither will be applied to all
1245% channels, while a single number is the number of levels applied to each
1246% channel in sequence. More numbers will be applied in turn to each of
1247% the color channels.
1248%
1249% For example: "o3x3,6" will generate a 6 level posterization of the
1250% image with a ordered 3x3 diffused pixel dither being applied between
1251% each level. While checker,8,8,4 will produce a 332 colormaped image
1252% with only a single checkerboard hash pattern (50% grey) between each
1253% color level, to basically double the number of color levels with
1254% a bare minimim of dithering.
1255%
1256% o exception: return any errors or warnings in this structure.
1257%
1258*/
1259MagickExport MagickBooleanType OrderedPosterizeImage(Image *image,
1260 const char *threshold_map,ExceptionInfo *exception)
1261{
cristy3ed852e2009-09-05 21:47:34 +00001262#define DitherImageTag "Dither/Image"
1263
cristyc4c8d132010-01-07 01:58:38 +00001264 CacheView
1265 *image_view;
1266
cristy4e0b82a2011-09-29 12:47:44 +00001267 char
1268 token[MaxTextExtent];
1269
1270 const char
1271 *p;
cristy3ed852e2009-09-05 21:47:34 +00001272
1273 MagickBooleanType
1274 status;
1275
cristy5f959472010-05-27 22:19:46 +00001276 MagickOffsetType
1277 progress;
1278
cristya19f1d72012-08-07 18:24:38 +00001279 double
cristy5f95f4f2011-10-23 01:01:01 +00001280 levels[CompositePixelChannel];
cristy4e0b82a2011-09-29 12:47:44 +00001281
1282 register ssize_t
1283 i;
1284
cristy5f959472010-05-27 22:19:46 +00001285 ssize_t
1286 y;
1287
cristy3ed852e2009-09-05 21:47:34 +00001288 ThresholdMap
1289 *map;
1290
cristy3ed852e2009-09-05 21:47:34 +00001291 assert(image != (Image *) NULL);
1292 assert(image->signature == MagickSignature);
1293 if (image->debug != MagickFalse)
1294 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1295 assert(exception != (ExceptionInfo *) NULL);
1296 assert(exception->signature == MagickSignature);
1297 if (threshold_map == (const char *) NULL)
1298 return(MagickTrue);
cristy4e0b82a2011-09-29 12:47:44 +00001299 p=(char *) threshold_map;
1300 while (((isspace((int) ((unsigned char) *p)) != 0) || (*p == ',')) &&
1301 (*p != '\0'))
1302 p++;
1303 threshold_map=p;
1304 while (((isspace((int) ((unsigned char) *p)) == 0) && (*p != ',')) &&
1305 (*p != '\0'))
cristy3ed852e2009-09-05 21:47:34 +00001306 {
cristy4e0b82a2011-09-29 12:47:44 +00001307 if ((p-threshold_map) >= (MaxTextExtent-1))
1308 break;
1309 token[p-threshold_map]=(*p);
1310 p++;
cristy3ed852e2009-09-05 21:47:34 +00001311 }
cristy4e0b82a2011-09-29 12:47:44 +00001312 token[p-threshold_map]='\0';
1313 map=GetThresholdMap(token,exception);
1314 if (map == (ThresholdMap *) NULL)
1315 {
1316 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1317 "InvalidArgument","%s : '%s'","ordered-dither",threshold_map);
cristy574cc262011-08-05 01:23:58 +00001318 return(MagickFalse);
cristy4e0b82a2011-09-29 12:47:44 +00001319 }
1320 for (i=0; i < MaxPixelChannels; i++)
1321 levels[i]=2.0;
1322 p=strchr((char *) threshold_map,',');
1323 if ((p != (char *) NULL) && (isdigit((int) ((unsigned char) *(++p))) != 0))
1324 for (i=0; (*p != '\0') && (i < MaxPixelChannels); i++)
1325 {
1326 GetMagickToken(p,&p,token);
1327 if (*token == ',')
1328 GetMagickToken(p,&p,token);
cristydbdd0e32011-11-04 23:29:40 +00001329 levels[i]=StringToDouble(token,(char **) NULL);
cristy4e0b82a2011-09-29 12:47:44 +00001330 }
1331 for (i=0; i < MaxPixelChannels; i++)
1332 if (fabs(levels[i]) >= 1)
1333 levels[i]-=1.0;
1334 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1335 return(MagickFalse);
1336 status=MagickTrue;
1337 progress=0;
cristydb070952012-04-20 14:33:00 +00001338 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00001339#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristybd065992012-08-09 15:27:39 +00001340 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00001341 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00001342#endif
cristy4e0b82a2011-09-29 12:47:44 +00001343 for (y=0; y < (ssize_t) image->rows; y++)
1344 {
1345 register ssize_t
1346 x;
1347
1348 register Quantum
1349 *restrict q;
1350
1351 if (status == MagickFalse)
1352 continue;
1353 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1354 if (q == (Quantum *) NULL)
1355 {
1356 status=MagickFalse;
1357 continue;
1358 }
1359 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001360 {
cristybb503372010-05-27 20:51:26 +00001361 register ssize_t
cristy4e0b82a2011-09-29 12:47:44 +00001362 i;
cristy3ed852e2009-09-05 21:47:34 +00001363
cristy4e0b82a2011-09-29 12:47:44 +00001364 ssize_t
1365 n;
cristy3ed852e2009-09-05 21:47:34 +00001366
cristy4e0b82a2011-09-29 12:47:44 +00001367 n=0;
cristy10a6c612012-01-29 21:41:05 +00001368 if (GetPixelMask(image,q) != 0)
1369 {
1370 q+=GetPixelChannels(image);
1371 continue;
1372 }
cristy4e0b82a2011-09-29 12:47:44 +00001373 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy3ed852e2009-09-05 21:47:34 +00001374 {
cristyabace412011-12-11 15:56:53 +00001375 PixelChannel
1376 channel;
1377
cristy4e0b82a2011-09-29 12:47:44 +00001378 PixelTrait
1379 traits;
1380
1381 ssize_t
cristy3f5d8152011-09-29 13:00:19 +00001382 level,
1383 threshold;
cristy3ed852e2009-09-05 21:47:34 +00001384
cristyabace412011-12-11 15:56:53 +00001385 channel=GetPixelChannelMapChannel(image,i);
1386 traits=GetPixelChannelMapTraits(image,channel);
cristy4e0b82a2011-09-29 12:47:44 +00001387 if ((traits & UpdatePixelTrait) == 0)
1388 continue;
cristy3f5d8152011-09-29 13:00:19 +00001389 if (fabs(levels[n++]) < MagickEpsilon)
1390 continue;
1391 threshold=(ssize_t) (QuantumScale*q[i]*(levels[n]*(map->divisor-1)+1));
1392 level=threshold/(map->divisor-1);
1393 threshold-=level*(map->divisor-1);
cristyada285b2012-07-07 19:00:46 +00001394 q[i]=ClampToQuantum((double) (level+(threshold >=
cristye42f6582012-02-11 17:59:50 +00001395 map->levels[(x % map->width)+map->width*(y % map->height)]))*
1396 QuantumRange/levels[n]);
cristy4e0b82a2011-09-29 12:47:44 +00001397 n++;
cristy3ed852e2009-09-05 21:47:34 +00001398 }
cristy4e0b82a2011-09-29 12:47:44 +00001399 q+=GetPixelChannels(image);
1400 }
1401 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1402 status=MagickFalse;
1403 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1404 {
1405 MagickBooleanType
1406 proceed;
cristy3ed852e2009-09-05 21:47:34 +00001407
cristyb5d5f722009-11-04 03:03:49 +00001408#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00001409 #pragma omp critical (MagickCore_OrderedPosterizeImage)
cristy3ed852e2009-09-05 21:47:34 +00001410#endif
cristy4e0b82a2011-09-29 12:47:44 +00001411 proceed=SetImageProgress(image,DitherImageTag,progress++,image->rows);
1412 if (proceed == MagickFalse)
1413 status=MagickFalse;
1414 }
cristy3ed852e2009-09-05 21:47:34 +00001415 }
cristy4e0b82a2011-09-29 12:47:44 +00001416 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00001417 map=DestroyThresholdMap(map);
1418 return(MagickTrue);
1419}
1420
1421/*
1422%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1423% %
1424% %
1425% %
1426% R a n d o m T h r e s h o l d I m a g e %
1427% %
1428% %
1429% %
1430%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1431%
1432% RandomThresholdImage() changes the value of individual pixels based on the
1433% intensity of each pixel compared to a random threshold. The result is a
1434% low-contrast, two color image.
1435%
1436% The format of the RandomThresholdImage method is:
1437%
cristyf4ad9df2011-07-08 16:49:03 +00001438% MagickBooleanType RandomThresholdImage(Image *image,
cristy3ed852e2009-09-05 21:47:34 +00001439% const char *thresholds,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001440%
1441% A description of each parameter follows:
1442%
1443% o image: the image.
1444%
cristy3ed852e2009-09-05 21:47:34 +00001445% o thresholds: a geometry string containing low,high thresholds. If the
1446% string contains 2x2, 3x3, or 4x4, an ordered dither of order 2, 3, or 4
1447% is performed instead.
1448%
1449% o exception: return any errors or warnings in this structure.
1450%
1451*/
cristy3ed852e2009-09-05 21:47:34 +00001452MagickExport MagickBooleanType RandomThresholdImage(Image *image,
1453 const char *thresholds,ExceptionInfo *exception)
1454{
cristy3ed852e2009-09-05 21:47:34 +00001455#define ThresholdImageTag "Threshold/Image"
1456
cristyfa112112010-01-04 17:48:07 +00001457 CacheView
1458 *image_view;
1459
cristy3ed852e2009-09-05 21:47:34 +00001460 GeometryInfo
1461 geometry_info;
1462
1463 MagickStatusType
1464 flags;
1465
cristy3ed852e2009-09-05 21:47:34 +00001466 MagickBooleanType
1467 status;
1468
cristy5f959472010-05-27 22:19:46 +00001469 MagickOffsetType
1470 progress;
1471
cristy4c08aed2011-07-01 19:47:50 +00001472 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001473 threshold;
1474
cristya19f1d72012-08-07 18:24:38 +00001475 double
cristy3ed852e2009-09-05 21:47:34 +00001476 min_threshold,
1477 max_threshold;
1478
1479 RandomInfo
cristyfa112112010-01-04 17:48:07 +00001480 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +00001481
cristy5f959472010-05-27 22:19:46 +00001482 ssize_t
1483 y;
1484
cristy57340e02012-05-05 00:53:23 +00001485 unsigned long
1486 key;
1487
cristy3ed852e2009-09-05 21:47:34 +00001488 assert(image != (Image *) NULL);
1489 assert(image->signature == MagickSignature);
1490 if (image->debug != MagickFalse)
1491 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1492 assert(exception != (ExceptionInfo *) NULL);
1493 assert(exception->signature == MagickSignature);
1494 if (thresholds == (const char *) NULL)
1495 return(MagickTrue);
cristye7452652012-04-14 01:34:21 +00001496 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1497 return(MagickFalse);
cristy4c08aed2011-07-01 19:47:50 +00001498 GetPixelInfo(image,&threshold);
cristy3ed852e2009-09-05 21:47:34 +00001499 min_threshold=0.0;
cristya19f1d72012-08-07 18:24:38 +00001500 max_threshold=(double) QuantumRange;
cristy3ed852e2009-09-05 21:47:34 +00001501 flags=ParseGeometry(thresholds,&geometry_info);
1502 min_threshold=geometry_info.rho;
1503 max_threshold=geometry_info.sigma;
1504 if ((flags & SigmaValue) == 0)
1505 max_threshold=min_threshold;
1506 if (strchr(thresholds,'%') != (char *) NULL)
1507 {
cristya19f1d72012-08-07 18:24:38 +00001508 max_threshold*=(double) (0.01*QuantumRange);
1509 min_threshold*=(double) (0.01*QuantumRange);
cristy3ed852e2009-09-05 21:47:34 +00001510 }
cristy3ed852e2009-09-05 21:47:34 +00001511 /*
1512 Random threshold image.
1513 */
1514 status=MagickTrue;
1515 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00001516 random_info=AcquireRandomInfoThreadSet();
cristy57340e02012-05-05 00:53:23 +00001517 key=GetRandomSecretKey(random_info[0]);
cristydb070952012-04-20 14:33:00 +00001518 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00001519#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristybd065992012-08-09 15:27:39 +00001520 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00001521 dynamic_number_threads(image,image->columns,image->rows,key == ~0UL)
cristy3ed852e2009-09-05 21:47:34 +00001522#endif
cristybb503372010-05-27 20:51:26 +00001523 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001524 {
cristy5c9e6f22010-09-17 17:31:01 +00001525 const int
1526 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00001527
cristy4c08aed2011-07-01 19:47:50 +00001528 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001529 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001530
cristy5c9e6f22010-09-17 17:31:01 +00001531 register ssize_t
1532 x;
1533
cristy3ed852e2009-09-05 21:47:34 +00001534 if (status == MagickFalse)
1535 continue;
1536 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001537 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001538 {
1539 status=MagickFalse;
1540 continue;
1541 }
cristybb503372010-05-27 20:51:26 +00001542 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001543 {
cristy5f9f2462011-09-28 23:37:58 +00001544 register ssize_t
1545 i;
1546
cristy10a6c612012-01-29 21:41:05 +00001547 if (GetPixelMask(image,q) != 0)
1548 {
1549 q+=GetPixelChannels(image);
1550 continue;
1551 }
cristy5f9f2462011-09-28 23:37:58 +00001552 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1553 {
cristya19f1d72012-08-07 18:24:38 +00001554 double
cristy5f9f2462011-09-28 23:37:58 +00001555 threshold;
1556
cristyabace412011-12-11 15:56:53 +00001557 PixelChannel
1558 channel;
1559
cristy5f9f2462011-09-28 23:37:58 +00001560 PixelTrait
1561 traits;
1562
cristyabace412011-12-11 15:56:53 +00001563 channel=GetPixelChannelMapChannel(image,i);
1564 traits=GetPixelChannelMapTraits(image,channel);
cristy5f9f2462011-09-28 23:37:58 +00001565 if ((traits & UpdatePixelTrait) == 0)
1566 continue;
cristya19f1d72012-08-07 18:24:38 +00001567 if ((double) q[i] < min_threshold)
cristy5f9f2462011-09-28 23:37:58 +00001568 threshold=min_threshold;
1569 else
cristya19f1d72012-08-07 18:24:38 +00001570 if ((double) q[i] > max_threshold)
cristy5f9f2462011-09-28 23:37:58 +00001571 threshold=max_threshold;
cristy3ed852e2009-09-05 21:47:34 +00001572 else
cristya19f1d72012-08-07 18:24:38 +00001573 threshold=(double) (QuantumRange*
cristy5f9f2462011-09-28 23:37:58 +00001574 GetPseudoRandomValue(random_info[id]));
cristya19f1d72012-08-07 18:24:38 +00001575 q[i]=(double) q[i] <= threshold ? 0 : QuantumRange;
cristy5f9f2462011-09-28 23:37:58 +00001576 }
cristyed231572011-07-14 02:18:59 +00001577 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001578 }
1579 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1580 status=MagickFalse;
1581 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1582 {
1583 MagickBooleanType
1584 proceed;
1585
cristyb5d5f722009-11-04 03:03:49 +00001586#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00001587 #pragma omp critical (MagickCore_RandomThresholdImage)
cristy3ed852e2009-09-05 21:47:34 +00001588#endif
1589 proceed=SetImageProgress(image,ThresholdImageTag,progress++,
1590 image->rows);
1591 if (proceed == MagickFalse)
1592 status=MagickFalse;
1593 }
1594 }
1595 image_view=DestroyCacheView(image_view);
1596 random_info=DestroyRandomInfoThreadSet(random_info);
1597 return(status);
1598}
1599
1600/*
1601%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1602% %
1603% %
1604% %
1605% W h i t e T h r e s h o l d I m a g e %
1606% %
1607% %
1608% %
1609%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1610%
1611% WhiteThresholdImage() is like ThresholdImage() but forces all pixels above
cristy4e101302009-09-17 12:49:12 +00001612% the threshold into white while leaving all pixels at or below the threshold
cristy3ed852e2009-09-05 21:47:34 +00001613% unchanged.
1614%
1615% The format of the WhiteThresholdImage method is:
1616%
cristyf4ad9df2011-07-08 16:49:03 +00001617% MagickBooleanType WhiteThresholdImage(Image *image,
1618% const char *threshold,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001619%
1620% A description of each parameter follows:
1621%
1622% o image: the image.
1623%
cristy3ed852e2009-09-05 21:47:34 +00001624% o threshold: Define the threshold value.
1625%
1626% o exception: return any errors or warnings in this structure.
1627%
1628*/
1629MagickExport MagickBooleanType WhiteThresholdImage(Image *image,
cristyf4ad9df2011-07-08 16:49:03 +00001630 const char *thresholds,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001631{
1632#define ThresholdImageTag "Threshold/Image"
1633
cristy5f959472010-05-27 22:19:46 +00001634 CacheView
1635 *image_view;
1636
cristy3ed852e2009-09-05 21:47:34 +00001637 GeometryInfo
1638 geometry_info;
1639
cristy3ed852e2009-09-05 21:47:34 +00001640 MagickBooleanType
1641 status;
1642
cristy5f959472010-05-27 22:19:46 +00001643 MagickOffsetType
1644 progress;
1645
cristyd6803382012-04-10 01:41:25 +00001646 PixelInfo
cristya12d8ba2012-04-29 16:33:41 +00001647 threshold;
cristy5f9f2462011-09-28 23:37:58 +00001648
cristy3ed852e2009-09-05 21:47:34 +00001649 MagickStatusType
1650 flags;
1651
cristy5f959472010-05-27 22:19:46 +00001652 ssize_t
1653 y;
cristy3ed852e2009-09-05 21:47:34 +00001654
1655 assert(image != (Image *) NULL);
1656 assert(image->signature == MagickSignature);
1657 if (image->debug != MagickFalse)
1658 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1659 if (thresholds == (const char *) NULL)
1660 return(MagickTrue);
cristy574cc262011-08-05 01:23:58 +00001661 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001662 return(MagickFalse);
cristy23e55c02012-04-10 01:21:56 +00001663 if (IsGrayColorspace(image->colorspace) != MagickFalse)
cristyb09db112012-07-11 12:04:31 +00001664 (void) TransformImageColorspace(image,RGBColorspace,exception);
cristya12d8ba2012-04-29 16:33:41 +00001665 GetPixelInfo(image,&threshold);
cristy3ed852e2009-09-05 21:47:34 +00001666 flags=ParseGeometry(thresholds,&geometry_info);
cristya12d8ba2012-04-29 16:33:41 +00001667 threshold.red=geometry_info.rho;
1668 threshold.green=geometry_info.rho;
1669 threshold.blue=geometry_info.rho;
1670 threshold.black=geometry_info.rho;
1671 threshold.alpha=100.0;
cristy5f9f2462011-09-28 23:37:58 +00001672 if ((flags & SigmaValue) != 0)
cristya12d8ba2012-04-29 16:33:41 +00001673 threshold.green=geometry_info.sigma;
cristy5f9f2462011-09-28 23:37:58 +00001674 if ((flags & XiValue) != 0)
cristya12d8ba2012-04-29 16:33:41 +00001675 threshold.blue=geometry_info.xi;
cristy5f9f2462011-09-28 23:37:58 +00001676 if ((flags & PsiValue) != 0)
cristya12d8ba2012-04-29 16:33:41 +00001677 threshold.alpha=geometry_info.psi;
1678 if (threshold.colorspace == CMYKColorspace)
cristyd6803382012-04-10 01:41:25 +00001679 {
1680 if ((flags & PsiValue) != 0)
cristya12d8ba2012-04-29 16:33:41 +00001681 threshold.black=geometry_info.psi;
cristyd6803382012-04-10 01:41:25 +00001682 if ((flags & ChiValue) != 0)
cristya12d8ba2012-04-29 16:33:41 +00001683 threshold.alpha=geometry_info.chi;
1684 }
1685 if ((flags & PercentValue) != 0)
1686 {
1687 threshold.red*=(QuantumRange/100.0);
1688 threshold.green*=(QuantumRange/100.0);
1689 threshold.blue*=(QuantumRange/100.0);
1690 threshold.black*=(QuantumRange/100.0);
1691 threshold.alpha*=(QuantumRange/100.0);
cristyd6803382012-04-10 01:41:25 +00001692 }
cristy3ed852e2009-09-05 21:47:34 +00001693 /*
1694 White threshold image.
1695 */
1696 status=MagickTrue;
1697 progress=0;
cristydb070952012-04-20 14:33:00 +00001698 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00001699#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristybd065992012-08-09 15:27:39 +00001700 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00001701 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00001702#endif
cristybb503372010-05-27 20:51:26 +00001703 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001704 {
cristybb503372010-05-27 20:51:26 +00001705 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001706 x;
1707
cristy4c08aed2011-07-01 19:47:50 +00001708 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001709 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001710
1711 if (status == MagickFalse)
1712 continue;
1713 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001714 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001715 {
1716 status=MagickFalse;
1717 continue;
1718 }
cristybb503372010-05-27 20:51:26 +00001719 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001720 {
cristy81629aa2012-07-12 20:08:52 +00001721 double
1722 pixel;
1723
cristy188f29a2012-06-24 19:09:53 +00001724 register ssize_t
1725 i;
1726
cristy10a6c612012-01-29 21:41:05 +00001727 if (GetPixelMask(image,q) != 0)
1728 {
1729 q+=GetPixelChannels(image);
1730 continue;
1731 }
cristyf13c5942012-08-08 23:50:11 +00001732 pixel=GetPixelIntensity(image,q);
cristy188f29a2012-06-24 19:09:53 +00001733 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1734 {
1735 PixelChannel
1736 channel;
1737
1738 PixelTrait
1739 traits;
1740
1741 channel=GetPixelChannelMapChannel(image,i);
1742 traits=GetPixelChannelMapTraits(image,channel);
1743 if ((traits & UpdatePixelTrait) == 0)
1744 continue;
cristy81629aa2012-07-12 20:08:52 +00001745 if (image->channel_mask != DefaultChannels)
cristyf13c5942012-08-08 23:50:11 +00001746 pixel=(double) q[i];
cristy81629aa2012-07-12 20:08:52 +00001747 if (pixel > GetPixelInfoChannel(&threshold,channel))
cristy188f29a2012-06-24 19:09:53 +00001748 q[i]=QuantumRange;
1749 }
cristyed231572011-07-14 02:18:59 +00001750 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001751 }
1752 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1753 status=MagickFalse;
1754 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1755 {
1756 MagickBooleanType
1757 proceed;
1758
cristyb5d5f722009-11-04 03:03:49 +00001759#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristya12d8ba2012-04-29 16:33:41 +00001760 #pragma omp critical (MagickCore_WhiteThresholdImage)
cristy3ed852e2009-09-05 21:47:34 +00001761#endif
1762 proceed=SetImageProgress(image,ThresholdImageTag,progress++,
1763 image->rows);
1764 if (proceed == MagickFalse)
1765 status=MagickFalse;
1766 }
1767 }
1768 image_view=DestroyCacheView(image_view);
1769 return(status);
1770}