blob: 93600b41927a28f19652b0c4a67eaab8f1810263 [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 {
cristy525cf962012-09-09 15:00:40 +0000213 double
214 channel_bias[MaxPixelChannels],
215 channel_sum[MaxPixelChannels];
216
217 PixelChannel
218 channel;
219
220 PixelTrait
221 threshold_traits,
222 traits;
223
cristy4c08aed2011-07-01 19:47:50 +0000224 register const Quantum
cristy525cf962012-09-09 15:00:40 +0000225 *restrict p,
226 *restrict pixels;
cristy3ed852e2009-09-05 21:47:34 +0000227
cristy4c08aed2011-07-01 19:47:50 +0000228 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000229 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000230
cristyde5cc632011-07-18 14:47:00 +0000231 register ssize_t
cristy525cf962012-09-09 15:00:40 +0000232 i,
cristyde5cc632011-07-18 14:47:00 +0000233 x;
234
cristyde5cc632011-07-18 14:47:00 +0000235 ssize_t
cristy525cf962012-09-09 15:00:40 +0000236 center,
237 u,
238 v;
cristyde5cc632011-07-18 14:47:00 +0000239
cristy3ed852e2009-09-05 21:47:34 +0000240 if (status == MagickFalse)
241 continue;
cristyd99b0962010-05-29 23:14:26 +0000242 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
cristyde5cc632011-07-18 14:47:00 +0000243 (height/2L),image->columns+width,height,exception);
244 q=QueueCacheViewAuthenticPixels(threshold_view,0,y,threshold_image->columns,
245 1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000246 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000247 {
248 status=MagickFalse;
249 continue;
250 }
cristy5f9f2462011-09-28 23:37:58 +0000251 center=(ssize_t) GetPixelChannels(image)*(image->columns+width)*(height/2L)+
cristya0312c92011-07-23 21:04:30 +0000252 GetPixelChannels(image)*(width/2);
cristy525cf962012-09-09 15:00:40 +0000253 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
254 {
255 channel=GetPixelChannelChannel(image,i);
256 traits=GetPixelChannelTraits(image,channel);
257 threshold_traits=GetPixelChannelTraits(threshold_image,channel);
258 if ((traits == UndefinedPixelTrait) ||
259 (threshold_traits == UndefinedPixelTrait))
260 continue;
261 if (((threshold_traits & CopyPixelTrait) != 0) ||
262 (GetPixelMask(image,p) != 0))
263 {
264 SetPixelChannel(threshold_image,channel,p[center+i],q);
265 continue;
266 }
267 pixels=p;
268 channel_bias[channel]=0.0;
269 channel_sum[channel]=0.0;
270 for (v=0; v < (ssize_t) height; v++)
271 {
272 for (u=0; u < (ssize_t) width; u++)
273 {
274 if (u == (ssize_t) (width-1))
275 channel_bias[channel]+=pixels[i];
276 channel_sum[channel]+=pixels[i];
277 pixels+=GetPixelChannels(image);
278 }
279 pixels+=image->columns*GetPixelChannels(image);
280 }
281 }
cristybb503372010-05-27 20:51:26 +0000282 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000283 {
cristya0312c92011-07-23 21:04:30 +0000284 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy3ed852e2009-09-05 21:47:34 +0000285 {
cristya19f1d72012-08-07 18:24:38 +0000286 double
cristy525cf962012-09-09 15:00:40 +0000287 mean;
cristyde5cc632011-07-18 14:47:00 +0000288
cristycf1296e2012-08-26 23:40:49 +0000289 channel=GetPixelChannelChannel(image,i);
290 traits=GetPixelChannelTraits(image,channel);
291 threshold_traits=GetPixelChannelTraits(threshold_image,channel);
cristy010d7d12011-08-31 01:02:48 +0000292 if ((traits == UndefinedPixelTrait) ||
293 (threshold_traits == UndefinedPixelTrait))
cristyde5cc632011-07-18 14:47:00 +0000294 continue;
cristy1eced092012-08-10 23:10:56 +0000295 if (((threshold_traits & CopyPixelTrait) != 0) ||
296 (GetPixelMask(image,p) != 0))
cristyde5cc632011-07-18 14:47:00 +0000297 {
cristy0beccfa2011-09-25 20:47:53 +0000298 SetPixelChannel(threshold_image,channel,p[center+i],q);
cristyde5cc632011-07-18 14:47:00 +0000299 continue;
300 }
cristy525cf962012-09-09 15:00:40 +0000301 channel_sum[channel]-=channel_bias[channel];
302 channel_bias[channel]=0.0;
cristyde5cc632011-07-18 14:47:00 +0000303 pixels=p;
cristyde5cc632011-07-18 14:47:00 +0000304 for (v=0; v < (ssize_t) height; v++)
cristy3ed852e2009-09-05 21:47:34 +0000305 {
cristy525cf962012-09-09 15:00:40 +0000306 channel_bias[channel]+=pixels[i];
307 pixels+=(width-1)*GetPixelChannels(image);
308 channel_sum[channel]+=pixels[i];
309 pixels+=(image->columns+1)*GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000310 }
cristy525cf962012-09-09 15:00:40 +0000311 mean=(double) (channel_sum[channel]/number_pixels+bias);
cristya19f1d72012-08-07 18:24:38 +0000312 SetPixelChannel(threshold_image,channel,(Quantum) ((double)
cristy5f9f2462011-09-28 23:37:58 +0000313 p[center+i] <= mean ? 0 : QuantumRange),q);
cristy3ed852e2009-09-05 21:47:34 +0000314 }
cristya0312c92011-07-23 21:04:30 +0000315 p+=GetPixelChannels(image);
316 q+=GetPixelChannels(threshold_image);
cristy3ed852e2009-09-05 21:47:34 +0000317 }
cristyde5cc632011-07-18 14:47:00 +0000318 if (SyncCacheViewAuthenticPixels(threshold_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000319 status=MagickFalse;
320 if (image->progress_monitor != (MagickProgressMonitor) NULL)
321 {
322 MagickBooleanType
323 proceed;
324
cristyb5d5f722009-11-04 03:03:49 +0000325#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000326 #pragma omp critical (MagickCore_AdaptiveThresholdImage)
cristy3ed852e2009-09-05 21:47:34 +0000327#endif
cristyde5cc632011-07-18 14:47:00 +0000328 proceed=SetImageProgress(image,AdaptiveThresholdImageTag,progress++,
cristy3ed852e2009-09-05 21:47:34 +0000329 image->rows);
330 if (proceed == MagickFalse)
331 status=MagickFalse;
332 }
333 }
cristyde5cc632011-07-18 14:47:00 +0000334 threshold_image->type=image->type;
cristy3ed852e2009-09-05 21:47:34 +0000335 threshold_view=DestroyCacheView(threshold_view);
336 image_view=DestroyCacheView(image_view);
337 if (status == MagickFalse)
338 threshold_image=DestroyImage(threshold_image);
339 return(threshold_image);
340}
341
342/*
343%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
344% %
345% %
346% %
347% B i l e v e l I m a g e %
348% %
349% %
350% %
351%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
352%
353% BilevelImage() changes the value of individual pixels based on the
354% intensity of each pixel channel. The result is a high-contrast image.
355%
356% More precisely each channel value of the image is 'thresholded' so that if
357% it is equal to or less than the given value it is set to zero, while any
358% value greater than that give is set to it maximum or QuantumRange.
359%
360% This function is what is used to implement the "-threshold" operator for
361% the command line API.
362%
363% If the default channel setting is given the image is thresholded using just
364% the gray 'intensity' of the image, rather than the individual channels.
365%
cristyf4ad9df2011-07-08 16:49:03 +0000366% The format of the BilevelImage method is:
cristy3ed852e2009-09-05 21:47:34 +0000367%
cristye941a752011-10-15 01:52:48 +0000368% MagickBooleanType BilevelImage(Image *image,const double threshold,
369% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000370%
371% A description of each parameter follows:
372%
373% o image: the image.
374%
cristy3ed852e2009-09-05 21:47:34 +0000375% o threshold: define the threshold values.
376%
cristye941a752011-10-15 01:52:48 +0000377% o exception: return any errors or warnings in this structure.
378%
cristyf89cb1d2011-07-07 01:24:37 +0000379% Aside: You can get the same results as operator using LevelImages()
cristy3ed852e2009-09-05 21:47:34 +0000380% with the 'threshold' value for both the black_point and the white_point.
381%
382*/
cristye941a752011-10-15 01:52:48 +0000383MagickExport MagickBooleanType BilevelImage(Image *image,const double threshold,
384 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000385{
386#define ThresholdImageTag "Threshold/Image"
387
cristyc4c8d132010-01-07 01:58:38 +0000388 CacheView
389 *image_view;
390
cristy3ed852e2009-09-05 21:47:34 +0000391 MagickBooleanType
392 status;
393
cristy5f959472010-05-27 22:19:46 +0000394 MagickOffsetType
395 progress;
396
397 ssize_t
398 y;
399
cristy3ed852e2009-09-05 21:47:34 +0000400 assert(image != (Image *) NULL);
401 assert(image->signature == MagickSignature);
402 if (image->debug != MagickFalse)
403 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy574cc262011-08-05 01:23:58 +0000404 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000405 return(MagickFalse);
406 /*
407 Bilevel threshold image.
408 */
409 status=MagickTrue;
410 progress=0;
cristydb070952012-04-20 14:33:00 +0000411 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +0000412#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristybd065992012-08-09 15:27:39 +0000413 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +0000414 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +0000415#endif
cristybb503372010-05-27 20:51:26 +0000416 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000417 {
cristybb503372010-05-27 20:51:26 +0000418 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000419 x;
420
cristy4c08aed2011-07-01 19:47:50 +0000421 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000422 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000423
424 if (status == MagickFalse)
425 continue;
426 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000427 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000428 {
429 status=MagickFalse;
430 continue;
431 }
cristy805b6a02011-08-09 00:59:35 +0000432 for (x=0; x < (ssize_t) image->columns; x++)
433 {
cristy171e2352012-07-10 17:43:12 +0000434 double
435 pixel;
436
cristy95111202011-08-09 19:41:42 +0000437 register ssize_t
438 i;
439
cristy10a6c612012-01-29 21:41:05 +0000440 if (GetPixelMask(image,q) != 0)
441 {
442 q+=GetPixelChannels(image);
443 continue;
444 }
cristyf13c5942012-08-08 23:50:11 +0000445 pixel=GetPixelIntensity(image,q);
cristy95111202011-08-09 19:41:42 +0000446 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
447 {
cristyabace412011-12-11 15:56:53 +0000448 PixelChannel
449 channel;
450
cristy95111202011-08-09 19:41:42 +0000451 PixelTrait
452 traits;
453
cristycf1296e2012-08-26 23:40:49 +0000454 channel=GetPixelChannelChannel(image,i);
455 traits=GetPixelChannelTraits(image,channel);
cristyd09f8802012-02-04 16:44:10 +0000456 if ((traits & UpdatePixelTrait) == 0)
457 continue;
cristya64f4b92012-07-11 23:59:00 +0000458 if (image->channel_mask != DefaultChannels)
cristyf13c5942012-08-08 23:50:11 +0000459 pixel=(double) q[i];
cristy171e2352012-07-10 17:43:12 +0000460 q[i]=(Quantum) (pixel <= threshold ? 0 : QuantumRange);
cristy95111202011-08-09 19:41:42 +0000461 }
cristy805b6a02011-08-09 00:59:35 +0000462 q+=GetPixelChannels(image);
463 }
cristy3ed852e2009-09-05 21:47:34 +0000464 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
465 status=MagickFalse;
466 if (image->progress_monitor != (MagickProgressMonitor) NULL)
467 {
468 MagickBooleanType
469 proceed;
470
cristyb5d5f722009-11-04 03:03:49 +0000471#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000472 #pragma omp critical (MagickCore_BilevelImage)
cristy3ed852e2009-09-05 21:47:34 +0000473#endif
474 proceed=SetImageProgress(image,ThresholdImageTag,progress++,
475 image->rows);
476 if (proceed == MagickFalse)
477 status=MagickFalse;
478 }
479 }
480 image_view=DestroyCacheView(image_view);
481 return(status);
482}
483
484/*
485%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
486% %
487% %
488% %
489% B l a c k T h r e s h o l d I m a g e %
490% %
491% %
492% %
493%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
494%
495% BlackThresholdImage() is like ThresholdImage() but forces all pixels below
cristy4e101302009-09-17 12:49:12 +0000496% the threshold into black while leaving all pixels at or above the threshold
cristy3ed852e2009-09-05 21:47:34 +0000497% unchanged.
498%
499% The format of the BlackThresholdImage method is:
500%
cristyf4ad9df2011-07-08 16:49:03 +0000501% MagickBooleanType BlackThresholdImage(Image *image,
502% const char *threshold,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000503%
504% A description of each parameter follows:
505%
506% o image: the image.
507%
cristy5f9f2462011-09-28 23:37:58 +0000508% o threshold: define the threshold value.
cristy3ed852e2009-09-05 21:47:34 +0000509%
510% o exception: return any errors or warnings in this structure.
511%
512*/
513MagickExport MagickBooleanType BlackThresholdImage(Image *image,
cristyf4ad9df2011-07-08 16:49:03 +0000514 const char *thresholds,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000515{
516#define ThresholdImageTag "Threshold/Image"
517
cristyc4c8d132010-01-07 01:58:38 +0000518 CacheView
519 *image_view;
520
cristy3ed852e2009-09-05 21:47:34 +0000521 GeometryInfo
522 geometry_info;
523
cristy3ed852e2009-09-05 21:47:34 +0000524 MagickBooleanType
525 status;
526
cristy5f959472010-05-27 22:19:46 +0000527 MagickOffsetType
528 progress;
529
cristyd6803382012-04-10 01:41:25 +0000530 PixelInfo
cristya12d8ba2012-04-29 16:33:41 +0000531 threshold;
cristy3ed852e2009-09-05 21:47:34 +0000532
533 MagickStatusType
534 flags;
535
cristy5f959472010-05-27 22:19:46 +0000536 ssize_t
537 y;
538
cristy3ed852e2009-09-05 21:47:34 +0000539 assert(image != (Image *) NULL);
540 assert(image->signature == MagickSignature);
541 if (image->debug != MagickFalse)
542 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
543 if (thresholds == (const char *) NULL)
544 return(MagickTrue);
cristy574cc262011-08-05 01:23:58 +0000545 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000546 return(MagickFalse);
cristy23e55c02012-04-10 01:21:56 +0000547 if (IsGrayColorspace(image->colorspace) != MagickFalse)
cristyb09db112012-07-11 12:04:31 +0000548 (void) TransformImageColorspace(image,RGBColorspace,exception);
cristya12d8ba2012-04-29 16:33:41 +0000549 GetPixelInfo(image,&threshold);
cristy3ed852e2009-09-05 21:47:34 +0000550 flags=ParseGeometry(thresholds,&geometry_info);
cristya12d8ba2012-04-29 16:33:41 +0000551 threshold.red=geometry_info.rho;
552 threshold.green=geometry_info.rho;
553 threshold.blue=geometry_info.rho;
554 threshold.black=geometry_info.rho;
555 threshold.alpha=100.0;
cristy5f9f2462011-09-28 23:37:58 +0000556 if ((flags & SigmaValue) != 0)
cristya12d8ba2012-04-29 16:33:41 +0000557 threshold.green=geometry_info.sigma;
cristy5f9f2462011-09-28 23:37:58 +0000558 if ((flags & XiValue) != 0)
cristya12d8ba2012-04-29 16:33:41 +0000559 threshold.blue=geometry_info.xi;
cristy5f9f2462011-09-28 23:37:58 +0000560 if ((flags & PsiValue) != 0)
cristya12d8ba2012-04-29 16:33:41 +0000561 threshold.alpha=geometry_info.psi;
562 if (threshold.colorspace == CMYKColorspace)
cristyd6803382012-04-10 01:41:25 +0000563 {
564 if ((flags & PsiValue) != 0)
cristya12d8ba2012-04-29 16:33:41 +0000565 threshold.black=geometry_info.psi;
cristyd6803382012-04-10 01:41:25 +0000566 if ((flags & ChiValue) != 0)
cristya12d8ba2012-04-29 16:33:41 +0000567 threshold.alpha=geometry_info.chi;
568 }
569 if ((flags & PercentValue) != 0)
570 {
cristy65d4e5e2012-10-17 12:22:24 +0000571 threshold.red*=(MagickRealType) (QuantumRange/100.0);
572 threshold.green*=(MagickRealType) (QuantumRange/100.0);
573 threshold.blue*=(MagickRealType) (QuantumRange/100.0);
574 threshold.black*=(MagickRealType) (QuantumRange/100.0);
575 threshold.alpha*=(MagickRealType) (QuantumRange/100.0);
cristyd6803382012-04-10 01:41:25 +0000576 }
cristy3ed852e2009-09-05 21:47:34 +0000577 /*
cristy5f9f2462011-09-28 23:37:58 +0000578 White threshold image.
cristy3ed852e2009-09-05 21:47:34 +0000579 */
580 status=MagickTrue;
581 progress=0;
cristydb070952012-04-20 14:33:00 +0000582 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +0000583#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristybd065992012-08-09 15:27:39 +0000584 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +0000585 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +0000586#endif
cristybb503372010-05-27 20:51:26 +0000587 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000588 {
cristybb503372010-05-27 20:51:26 +0000589 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000590 x;
591
cristy4c08aed2011-07-01 19:47:50 +0000592 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000593 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000594
595 if (status == MagickFalse)
596 continue;
597 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000598 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000599 {
600 status=MagickFalse;
601 continue;
602 }
cristybb503372010-05-27 20:51:26 +0000603 for (x=0; x < (ssize_t) image->columns; x++)
cristyb0ea1af2009-11-28 20:44:46 +0000604 {
cristy81629aa2012-07-12 20:08:52 +0000605 double
606 pixel;
607
cristyc4567182012-06-24 20:55:08 +0000608 register ssize_t
609 i;
610
cristy10a6c612012-01-29 21:41:05 +0000611 if (GetPixelMask(image,q) != 0)
612 {
613 q+=GetPixelChannels(image);
614 continue;
615 }
cristyf13c5942012-08-08 23:50:11 +0000616 pixel=GetPixelIntensity(image,q);
cristy188f29a2012-06-24 19:09:53 +0000617 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
618 {
619 PixelChannel
620 channel;
621
622 PixelTrait
623 traits;
624
cristycf1296e2012-08-26 23:40:49 +0000625 channel=GetPixelChannelChannel(image,i);
626 traits=GetPixelChannelTraits(image,channel);
cristy188f29a2012-06-24 19:09:53 +0000627 if ((traits & UpdatePixelTrait) == 0)
628 continue;
cristy81629aa2012-07-12 20:08:52 +0000629 if (image->channel_mask != DefaultChannels)
cristyf13c5942012-08-08 23:50:11 +0000630 pixel=(double) q[i];
cristy81629aa2012-07-12 20:08:52 +0000631 if (pixel <= GetPixelInfoChannel(&threshold,channel))
cristy525cf962012-09-09 15:00:40 +0000632 q[i]=(Quantum) 0;
cristy188f29a2012-06-24 19:09:53 +0000633 }
cristyed231572011-07-14 02:18:59 +0000634 q+=GetPixelChannels(image);
cristyb0ea1af2009-11-28 20:44:46 +0000635 }
cristy3ed852e2009-09-05 21:47:34 +0000636 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
637 status=MagickFalse;
638 if (image->progress_monitor != (MagickProgressMonitor) NULL)
639 {
640 MagickBooleanType
641 proceed;
642
cristyb5d5f722009-11-04 03:03:49 +0000643#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristya12d8ba2012-04-29 16:33:41 +0000644 #pragma omp critical (MagickCore_BlackThresholdImage)
cristy3ed852e2009-09-05 21:47:34 +0000645#endif
646 proceed=SetImageProgress(image,ThresholdImageTag,progress++,
647 image->rows);
648 if (proceed == MagickFalse)
649 status=MagickFalse;
650 }
651 }
652 image_view=DestroyCacheView(image_view);
653 return(status);
654}
655
656/*
657%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
658% %
659% %
660% %
cristy1eb45dd2009-09-25 16:38:06 +0000661% C l a m p I m a g e %
662% %
663% %
664% %
665%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
666%
cristyecb0c6d2009-09-25 16:50:09 +0000667% ClampImage() restricts the color range from 0 to the quantum depth.
cristy1eb45dd2009-09-25 16:38:06 +0000668%
cristyf4ad9df2011-07-08 16:49:03 +0000669% The format of the ClampImage method is:
cristy1eb45dd2009-09-25 16:38:06 +0000670%
cristy092d71c2011-10-14 18:01:29 +0000671% MagickBooleanType ClampImage(Image *image,ExceptionInfo *exception)
cristy1eb45dd2009-09-25 16:38:06 +0000672%
673% A description of each parameter follows:
674%
675% o image: the image.
676%
cristy092d71c2011-10-14 18:01:29 +0000677% o exception: return any errors or warnings in this structure.
678%
cristy1eb45dd2009-09-25 16:38:06 +0000679*/
680
cristy75ffdb72010-01-07 17:40:12 +0000681static inline Quantum ClampToUnsignedQuantum(const Quantum quantum)
cristy1eb45dd2009-09-25 16:38:06 +0000682{
cristy1eb45dd2009-09-25 16:38:06 +0000683 if (quantum <= 0)
cristy525cf962012-09-09 15:00:40 +0000684 return((Quantum) 0);
cristy1eb45dd2009-09-25 16:38:06 +0000685 if (quantum >= QuantumRange)
686 return(QuantumRange);
687 return(quantum);
cristy1eb45dd2009-09-25 16:38:06 +0000688}
689
cristy092d71c2011-10-14 18:01:29 +0000690MagickExport MagickBooleanType ClampImage(Image *image,ExceptionInfo *exception)
cristy1eb45dd2009-09-25 16:38:06 +0000691{
cristy9fd425e2012-08-16 11:55:06 +0000692#if !defined(MAGICKCORE_HDRI_SUPPORT)
693 return(MagickTrue);
694#else
cristy1eb45dd2009-09-25 16:38:06 +0000695#define ClampImageTag "Clamp/Image"
696
cristyc4c8d132010-01-07 01:58:38 +0000697 CacheView
698 *image_view;
699
cristy1eb45dd2009-09-25 16:38:06 +0000700 MagickBooleanType
701 status;
702
cristy5f959472010-05-27 22:19:46 +0000703 MagickOffsetType
704 progress;
705
706 ssize_t
707 y;
708
cristy1eb45dd2009-09-25 16:38:06 +0000709 assert(image != (Image *) NULL);
710 assert(image->signature == MagickSignature);
711 if (image->debug != MagickFalse)
712 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
713 if (image->storage_class == PseudoClass)
714 {
cristybb503372010-05-27 20:51:26 +0000715 register ssize_t
cristy1eb45dd2009-09-25 16:38:06 +0000716 i;
717
cristy101ab702011-10-13 13:06:32 +0000718 register PixelInfo
cristyc47d1f82009-11-26 01:44:43 +0000719 *restrict q;
cristy1eb45dd2009-09-25 16:38:06 +0000720
721 q=image->colormap;
cristybb503372010-05-27 20:51:26 +0000722 for (i=0; i < (ssize_t) image->colors; i++)
cristy1eb45dd2009-09-25 16:38:06 +0000723 {
cristye42f6582012-02-11 17:59:50 +0000724 q->red=(double) ClampToUnsignedQuantum(ClampToQuantum(q->red));
725 q->green=(double) ClampToUnsignedQuantum(ClampToQuantum(q->green));
726 q->blue=(double) ClampToUnsignedQuantum(ClampToQuantum(q->blue));
727 q->alpha=(double) ClampToUnsignedQuantum(ClampToQuantum(q->alpha));
cristy1eb45dd2009-09-25 16:38:06 +0000728 q++;
729 }
cristyea1a8aa2011-10-20 13:24:06 +0000730 return(SyncImage(image,exception));
cristy1eb45dd2009-09-25 16:38:06 +0000731 }
732 /*
cristy611721d2009-09-25 16:42:17 +0000733 Clamp image.
cristy1eb45dd2009-09-25 16:38:06 +0000734 */
735 status=MagickTrue;
736 progress=0;
cristydb070952012-04-20 14:33:00 +0000737 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +0000738#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristybd065992012-08-09 15:27:39 +0000739 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +0000740 dynamic_number_threads(image,image->columns,image->rows,1)
cristy1eb45dd2009-09-25 16:38:06 +0000741#endif
cristybb503372010-05-27 20:51:26 +0000742 for (y=0; y < (ssize_t) image->rows; y++)
cristy1eb45dd2009-09-25 16:38:06 +0000743 {
cristybb503372010-05-27 20:51:26 +0000744 register ssize_t
cristy1eb45dd2009-09-25 16:38:06 +0000745 x;
746
cristy4c08aed2011-07-01 19:47:50 +0000747 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000748 *restrict q;
cristy1eb45dd2009-09-25 16:38:06 +0000749
750 if (status == MagickFalse)
751 continue;
752 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000753 if (q == (Quantum *) NULL)
cristy1eb45dd2009-09-25 16:38:06 +0000754 {
755 status=MagickFalse;
756 continue;
757 }
cristybb503372010-05-27 20:51:26 +0000758 for (x=0; x < (ssize_t) image->columns; x++)
cristy1eb45dd2009-09-25 16:38:06 +0000759 {
cristy5f9f2462011-09-28 23:37:58 +0000760 register ssize_t
761 i;
762
cristy10a6c612012-01-29 21:41:05 +0000763 if (GetPixelMask(image,q) != 0)
764 {
765 q+=GetPixelChannels(image);
766 continue;
767 }
cristy5f9f2462011-09-28 23:37:58 +0000768 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
769 {
cristyabace412011-12-11 15:56:53 +0000770 PixelChannel
771 channel;
772
cristy5f9f2462011-09-28 23:37:58 +0000773 PixelTrait
774 traits;
775
cristycf1296e2012-08-26 23:40:49 +0000776 channel=GetPixelChannelChannel(image,i);
777 traits=GetPixelChannelTraits(image,channel);
cristy5f9f2462011-09-28 23:37:58 +0000778 if (traits == UndefinedPixelTrait)
779 continue;
780 q[i]=ClampToUnsignedQuantum(q[i]);
781 }
cristyed231572011-07-14 02:18:59 +0000782 q+=GetPixelChannels(image);
cristy1eb45dd2009-09-25 16:38:06 +0000783 }
784 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
785 status=MagickFalse;
786 if (image->progress_monitor != (MagickProgressMonitor) NULL)
787 {
788 MagickBooleanType
789 proceed;
790
cristyb5d5f722009-11-04 03:03:49 +0000791#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000792 #pragma omp critical (MagickCore_ClampImage)
cristy1eb45dd2009-09-25 16:38:06 +0000793#endif
cristyaa17cde2012-06-04 23:43:42 +0000794 proceed=SetImageProgress(image,ClampImageTag,progress++,image->rows);
cristy1eb45dd2009-09-25 16:38:06 +0000795 if (proceed == MagickFalse)
796 status=MagickFalse;
797 }
798 }
799 image_view=DestroyCacheView(image_view);
800 return(status);
cristy191c0b72012-08-12 16:29:52 +0000801#endif
cristy1eb45dd2009-09-25 16:38:06 +0000802}
803
804/*
805%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
806% %
807% %
808% %
cristy3ed852e2009-09-05 21:47:34 +0000809% D e s t r o y T h r e s h o l d M a p %
810% %
811% %
812% %
813%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
814%
815% DestroyThresholdMap() de-allocate the given ThresholdMap
816%
817% The format of the ListThresholdMaps method is:
818%
819% ThresholdMap *DestroyThresholdMap(Threshold *map)
820%
821% A description of each parameter follows.
822%
823% o map: Pointer to the Threshold map to destroy
824%
825*/
826MagickExport ThresholdMap *DestroyThresholdMap(ThresholdMap *map)
827{
828 assert(map != (ThresholdMap *) NULL);
829 if (map->map_id != (char *) NULL)
830 map->map_id=DestroyString(map->map_id);
831 if (map->description != (char *) NULL)
832 map->description=DestroyString(map->description);
cristybb503372010-05-27 20:51:26 +0000833 if (map->levels != (ssize_t *) NULL)
834 map->levels=(ssize_t *) RelinquishMagickMemory(map->levels);
cristy3ed852e2009-09-05 21:47:34 +0000835 map=(ThresholdMap *) RelinquishMagickMemory(map);
836 return(map);
837}
838
839/*
840%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
841% %
842% %
843% %
cristyb9eb87b2011-09-29 01:15:19 +0000844% G e t T h r e s h o l d M a p %
845% %
846% %
847% %
848%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
849%
850% GetThresholdMap() loads and searches one or more threshold map files for the
851% map matching the given name or alias.
852%
853% The format of the GetThresholdMap method is:
854%
855% ThresholdMap *GetThresholdMap(const char *map_id,
856% ExceptionInfo *exception)
857%
858% A description of each parameter follows.
859%
860% o map_id: ID of the map to look for.
861%
862% o exception: return any errors or warnings in this structure.
863%
864*/
865MagickExport ThresholdMap *GetThresholdMap(const char *map_id,
866 ExceptionInfo *exception)
867{
868 const StringInfo
869 *option;
870
871 LinkedListInfo
872 *options;
873
874 ThresholdMap
875 *map;
876
877 map=(ThresholdMap *)NULL;
878 options=GetConfigureOptions(ThresholdsFilename,exception);
879 while ((option=(const StringInfo *) GetNextValueInLinkedList(options)) !=
880 (const StringInfo *) NULL && (map == (ThresholdMap *) NULL))
881 map=GetThresholdMapFile((const char *) GetStringInfoDatum(option),
882 GetStringInfoPath(option),map_id,exception);
883 options=DestroyConfigureOptions(options);
884 return(map);
885}
886
887/*
888%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
889% %
890% %
891% %
cristy3ed852e2009-09-05 21:47:34 +0000892+ G e t T h r e s h o l d M a p F i l e %
893% %
894% %
895% %
896%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
897%
898% GetThresholdMapFile() look for a given threshold map name or alias in the
899% given XML file data, and return the allocated the map when found.
900%
901% The format of the ListThresholdMaps method is:
902%
903% ThresholdMap *GetThresholdMap(const char *xml,const char *filename,
904% const char *map_id,ExceptionInfo *exception)
905%
906% A description of each parameter follows.
907%
908% o xml: The threshold map list in XML format.
909%
910% o filename: The threshold map XML filename.
911%
912% o map_id: ID of the map to look for in XML list.
913%
914% o exception: return any errors or warnings in this structure.
915%
916*/
cristybd0ebf02011-09-29 01:19:42 +0000917static ThresholdMap *GetThresholdMapFile(const char *xml,
cristy3ed852e2009-09-05 21:47:34 +0000918 const char *filename,const char *map_id,ExceptionInfo *exception)
919{
cristyb9eb87b2011-09-29 01:15:19 +0000920 char
921 *p;
922
cristy3ed852e2009-09-05 21:47:34 +0000923 const char
cristyb9eb87b2011-09-29 01:15:19 +0000924 *attribute,
cristy3ed852e2009-09-05 21:47:34 +0000925 *content;
926
927 double
928 value;
929
cristyb9eb87b2011-09-29 01:15:19 +0000930 register ssize_t
931 i;
cristy3ed852e2009-09-05 21:47:34 +0000932
933 ThresholdMap
934 *map;
935
cristyb9eb87b2011-09-29 01:15:19 +0000936 XMLTreeInfo
937 *description,
938 *levels,
939 *threshold,
940 *thresholds;
941
942 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
943 "Loading threshold map file \"%s\" ...",filename);
944 map=(ThresholdMap *) NULL;
945 thresholds=NewXMLTree(xml,exception);
946 if (thresholds == (XMLTreeInfo *) NULL)
947 return(map);
948 for (threshold=GetXMLTreeChild(thresholds,"threshold");
949 threshold != (XMLTreeInfo *) NULL;
950 threshold=GetNextXMLTreeTag(threshold))
951 {
952 attribute=GetXMLTreeAttribute(threshold,"map");
953 if ((attribute != (char *) NULL) && (LocaleCompare(map_id,attribute) == 0))
954 break;
955 attribute=GetXMLTreeAttribute(threshold,"alias");
956 if ((attribute != (char *) NULL) && (LocaleCompare(map_id,attribute) == 0))
957 break;
958 }
959 if (threshold == (XMLTreeInfo *) NULL)
960 return(map);
961 description=GetXMLTreeChild(threshold,"description");
962 if (description == (XMLTreeInfo *) NULL)
963 {
964 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
965 "XmlMissingElement", "<description>, map \"%s\"",map_id);
966 thresholds=DestroyXMLTree(thresholds);
967 return(map);
968 }
969 levels=GetXMLTreeChild(threshold,"levels");
970 if (levels == (XMLTreeInfo *) NULL)
971 {
972 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
973 "XmlMissingElement", "<levels>, map \"%s\"", map_id);
974 thresholds=DestroyXMLTree(thresholds);
975 return(map);
976 }
977 map=(ThresholdMap *) AcquireMagickMemory(sizeof(ThresholdMap));
978 if (map == (ThresholdMap *) NULL)
979 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireThresholdMap");
980 map->map_id=(char *) NULL;
981 map->description=(char *) NULL;
982 map->levels=(ssize_t *) NULL;
983 attribute=GetXMLTreeAttribute(threshold,"map");
984 if (attribute != (char *) NULL)
985 map->map_id=ConstantString(attribute);
986 content=GetXMLTreeContent(description);
987 if (content != (char *) NULL)
988 map->description=ConstantString(content);
989 attribute=GetXMLTreeAttribute(levels,"width");
990 if (attribute == (char *) NULL)
991 {
992 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
993 "XmlMissingAttribute", "<levels width>, map \"%s\"",map_id);
994 thresholds=DestroyXMLTree(thresholds);
995 map=DestroyThresholdMap(map);
996 return(map);
997 }
998 map->width=StringToUnsignedLong(attribute);
999 if (map->width == 0)
1000 {
1001 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1002 "XmlInvalidAttribute", "<levels width>, map \"%s\"",map_id);
1003 thresholds=DestroyXMLTree(thresholds);
1004 map=DestroyThresholdMap(map);
1005 return(map);
1006 }
1007 attribute=GetXMLTreeAttribute(levels,"height");
1008 if (attribute == (char *) NULL)
1009 {
1010 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1011 "XmlMissingAttribute", "<levels height>, map \"%s\"",map_id);
1012 thresholds=DestroyXMLTree(thresholds);
1013 map=DestroyThresholdMap(map);
1014 return(map);
1015 }
1016 map->height=StringToUnsignedLong(attribute);
1017 if (map->height == 0)
1018 {
1019 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1020 "XmlInvalidAttribute", "<levels height>, map \"%s\"",map_id);
1021 thresholds=DestroyXMLTree(thresholds);
1022 map=DestroyThresholdMap(map);
1023 return(map);
1024 }
1025 attribute=GetXMLTreeAttribute(levels,"divisor");
1026 if (attribute == (char *) NULL)
1027 {
1028 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1029 "XmlMissingAttribute", "<levels divisor>, map \"%s\"",map_id);
1030 thresholds=DestroyXMLTree(thresholds);
1031 map=DestroyThresholdMap(map);
1032 return(map);
1033 }
1034 map->divisor=(ssize_t) StringToLong(attribute);
1035 if (map->divisor < 2)
1036 {
1037 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1038 "XmlInvalidAttribute", "<levels divisor>, map \"%s\"",map_id);
1039 thresholds=DestroyXMLTree(thresholds);
1040 map=DestroyThresholdMap(map);
1041 return(map);
1042 }
1043 content=GetXMLTreeContent(levels);
1044 if (content == (char *) NULL)
1045 {
1046 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1047 "XmlMissingContent", "<levels>, map \"%s\"",map_id);
1048 thresholds=DestroyXMLTree(thresholds);
1049 map=DestroyThresholdMap(map);
1050 return(map);
1051 }
1052 map->levels=(ssize_t *) AcquireQuantumMemory((size_t) map->width,map->height*
1053 sizeof(*map->levels));
1054 if (map->levels == (ssize_t *) NULL)
1055 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireThresholdMap");
1056 for (i=0; i < (ssize_t) (map->width*map->height); i++)
1057 {
1058 map->levels[i]=(ssize_t) strtol(content,&p,10);
1059 if (p == content)
1060 {
1061 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1062 "XmlInvalidContent", "<level> too few values, map \"%s\"",map_id);
1063 thresholds=DestroyXMLTree(thresholds);
1064 map=DestroyThresholdMap(map);
1065 return(map);
1066 }
1067 if ((map->levels[i] < 0) || (map->levels[i] > map->divisor))
1068 {
1069 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1070 "XmlInvalidContent", "<level> %.20g out of range, map \"%s\"",
1071 (double) map->levels[i],map_id);
1072 thresholds=DestroyXMLTree(thresholds);
1073 map=DestroyThresholdMap(map);
1074 return(map);
1075 }
1076 content=p;
1077 }
1078 value=(double) strtol(content,&p,10);
1079 (void) value;
1080 if (p != content)
1081 {
1082 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1083 "XmlInvalidContent", "<level> too many values, map \"%s\"",map_id);
1084 thresholds=DestroyXMLTree(thresholds);
1085 map=DestroyThresholdMap(map);
1086 return(map);
1087 }
1088 thresholds=DestroyXMLTree(thresholds);
cristy3ed852e2009-09-05 21:47:34 +00001089 return(map);
1090}
1091
1092/*
1093%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1094% %
1095% %
1096% %
1097+ L i s t T h r e s h o l d M a p F i l e %
1098% %
1099% %
1100% %
1101%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1102%
1103% ListThresholdMapFile() lists the threshold maps and their descriptions
1104% in the given XML file data.
1105%
1106% The format of the ListThresholdMaps method is:
1107%
1108% MagickBooleanType ListThresholdMaps(FILE *file,const char*xml,
1109% const char *filename,ExceptionInfo *exception)
1110%
1111% A description of each parameter follows.
1112%
1113% o file: An pointer to the output FILE.
1114%
1115% o xml: The threshold map list in XML format.
1116%
1117% o filename: The threshold map XML filename.
1118%
1119% o exception: return any errors or warnings in this structure.
1120%
1121*/
1122MagickBooleanType ListThresholdMapFile(FILE *file,const char *xml,
1123 const char *filename,ExceptionInfo *exception)
1124{
cristy5f9f2462011-09-28 23:37:58 +00001125 const char
1126 *alias,
1127 *content,
1128 *map;
1129
1130 XMLTreeInfo
1131 *description,
1132 *threshold,
1133 *thresholds;
cristy3ed852e2009-09-05 21:47:34 +00001134
1135 assert( xml != (char *)NULL );
1136 assert( file != (FILE *)NULL );
cristy3ed852e2009-09-05 21:47:34 +00001137 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1138 "Loading threshold map file \"%s\" ...",filename);
1139 thresholds=NewXMLTree(xml,exception);
1140 if ( thresholds == (XMLTreeInfo *)NULL )
1141 return(MagickFalse);
cristy1e604812011-05-19 18:07:50 +00001142 (void) FormatLocaleFile(file,"%-16s %-12s %s\n","Map","Alias","Description");
1143 (void) FormatLocaleFile(file,
1144 "----------------------------------------------------\n");
cristy5f9f2462011-09-28 23:37:58 +00001145 threshold=GetXMLTreeChild(thresholds,"threshold");
cristyb9eb87b2011-09-29 01:15:19 +00001146 for ( ; threshold != (XMLTreeInfo *) NULL;
1147 threshold=GetNextXMLTreeTag(threshold))
cristy3ed852e2009-09-05 21:47:34 +00001148 {
cristy5f9f2462011-09-28 23:37:58 +00001149 map=GetXMLTreeAttribute(threshold,"map");
1150 if (map == (char *) NULL)
1151 {
1152 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1153 "XmlMissingAttribute", "<map>");
1154 thresholds=DestroyXMLTree(thresholds);
1155 return(MagickFalse);
1156 }
1157 alias=GetXMLTreeAttribute(threshold,"alias");
cristy3ed852e2009-09-05 21:47:34 +00001158 description=GetXMLTreeChild(threshold,"description");
cristy5f9f2462011-09-28 23:37:58 +00001159 if (description == (XMLTreeInfo *) NULL)
1160 {
1161 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
cristyb9eb87b2011-09-29 01:15:19 +00001162 "XmlMissingElement", "<description>, map \"%s\"",map);
cristy5f9f2462011-09-28 23:37:58 +00001163 thresholds=DestroyXMLTree(thresholds);
1164 return(MagickFalse);
1165 }
cristy3ed852e2009-09-05 21:47:34 +00001166 content=GetXMLTreeContent(description);
cristy5f9f2462011-09-28 23:37:58 +00001167 if (content == (char *) NULL)
1168 {
1169 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1170 "XmlMissingContent", "<description>, map \"%s\"", map);
1171 thresholds=DestroyXMLTree(thresholds);
1172 return(MagickFalse);
1173 }
cristy1e604812011-05-19 18:07:50 +00001174 (void) FormatLocaleFile(file,"%-16s %-12s %s\n",map,alias ? alias : "",
1175 content);
cristy3ed852e2009-09-05 21:47:34 +00001176 }
1177 thresholds=DestroyXMLTree(thresholds);
1178 return(MagickTrue);
1179}
1180
1181/*
1182%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1183% %
1184% %
1185% %
1186% L i s t T h r e s h o l d M a p s %
1187% %
1188% %
1189% %
1190%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1191%
1192% ListThresholdMaps() lists the threshold maps and their descriptions
1193% as defined by "threshold.xml" to a file.
1194%
1195% The format of the ListThresholdMaps method is:
1196%
1197% MagickBooleanType ListThresholdMaps(FILE *file,ExceptionInfo *exception)
1198%
1199% A description of each parameter follows.
1200%
1201% o file: An pointer to the output FILE.
1202%
1203% o exception: return any errors or warnings in this structure.
1204%
1205*/
1206MagickExport MagickBooleanType ListThresholdMaps(FILE *file,
1207 ExceptionInfo *exception)
1208{
1209 const StringInfo
1210 *option;
1211
1212 LinkedListInfo
1213 *options;
1214
1215 MagickStatusType
1216 status;
1217
1218 status=MagickFalse;
cristy5f9f2462011-09-28 23:37:58 +00001219 if (file == (FILE *) NULL)
1220 file=stdout;
cristy3ed852e2009-09-05 21:47:34 +00001221 options=GetConfigureOptions(ThresholdsFilename,exception);
cristy1e604812011-05-19 18:07:50 +00001222 (void) FormatLocaleFile(file,
1223 "\n Threshold Maps for Ordered Dither Operations\n");
cristy5f9f2462011-09-28 23:37:58 +00001224 while ((option=(const StringInfo *) GetNextValueInLinkedList(options)) !=
1225 (const StringInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001226 {
cristyb51dff52011-05-19 16:55:47 +00001227 (void) FormatLocaleFile(file,"\nPATH: %s\n\n",GetStringInfoPath(option));
cristy3ed852e2009-09-05 21:47:34 +00001228 status|=ListThresholdMapFile(file,(const char *) GetStringInfoDatum(option),
1229 GetStringInfoPath(option),exception);
1230 }
1231 options=DestroyConfigureOptions(options);
1232 return(status != 0 ? MagickTrue : MagickFalse);
1233}
1234
1235/*
1236%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1237% %
1238% %
1239% %
cristy3ed852e2009-09-05 21:47:34 +00001240% O r d e r e d P o s t e r i z e I m a g e %
1241% %
1242% %
1243% %
1244%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1245%
1246% OrderedPosterizeImage() will perform a ordered dither based on a number
1247% of pre-defined dithering threshold maps, but over multiple intensity
1248% levels, which can be different for different channels, according to the
1249% input argument.
1250%
1251% The format of the OrderedPosterizeImage method is:
1252%
1253% MagickBooleanType OrderedPosterizeImage(Image *image,
1254% const char *threshold_map,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001255%
1256% A description of each parameter follows:
1257%
1258% o image: the image.
1259%
cristy3ed852e2009-09-05 21:47:34 +00001260% o threshold_map: A string containing the name of the threshold dither
1261% map to use, followed by zero or more numbers representing the number
1262% of color levels tho dither between.
1263%
cristyf998fb32011-04-27 23:00:47 +00001264% Any level number less than 2 will be equivalent to 2, and means only
cristy3ed852e2009-09-05 21:47:34 +00001265% binary dithering will be applied to each color channel.
1266%
1267% No numbers also means a 2 level (bitmap) dither will be applied to all
1268% channels, while a single number is the number of levels applied to each
1269% channel in sequence. More numbers will be applied in turn to each of
1270% the color channels.
1271%
1272% For example: "o3x3,6" will generate a 6 level posterization of the
1273% image with a ordered 3x3 diffused pixel dither being applied between
1274% each level. While checker,8,8,4 will produce a 332 colormaped image
1275% with only a single checkerboard hash pattern (50% grey) between each
1276% color level, to basically double the number of color levels with
1277% a bare minimim of dithering.
1278%
1279% o exception: return any errors or warnings in this structure.
1280%
1281*/
1282MagickExport MagickBooleanType OrderedPosterizeImage(Image *image,
1283 const char *threshold_map,ExceptionInfo *exception)
1284{
cristy3ed852e2009-09-05 21:47:34 +00001285#define DitherImageTag "Dither/Image"
1286
cristyc4c8d132010-01-07 01:58:38 +00001287 CacheView
1288 *image_view;
1289
cristy4e0b82a2011-09-29 12:47:44 +00001290 char
1291 token[MaxTextExtent];
1292
1293 const char
1294 *p;
cristy3ed852e2009-09-05 21:47:34 +00001295
1296 MagickBooleanType
1297 status;
1298
cristy5f959472010-05-27 22:19:46 +00001299 MagickOffsetType
1300 progress;
1301
cristya19f1d72012-08-07 18:24:38 +00001302 double
cristy5f95f4f2011-10-23 01:01:01 +00001303 levels[CompositePixelChannel];
cristy4e0b82a2011-09-29 12:47:44 +00001304
1305 register ssize_t
1306 i;
1307
cristy5f959472010-05-27 22:19:46 +00001308 ssize_t
1309 y;
1310
cristy3ed852e2009-09-05 21:47:34 +00001311 ThresholdMap
1312 *map;
1313
cristy3ed852e2009-09-05 21:47:34 +00001314 assert(image != (Image *) NULL);
1315 assert(image->signature == MagickSignature);
1316 if (image->debug != MagickFalse)
1317 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1318 assert(exception != (ExceptionInfo *) NULL);
1319 assert(exception->signature == MagickSignature);
1320 if (threshold_map == (const char *) NULL)
1321 return(MagickTrue);
cristy4e0b82a2011-09-29 12:47:44 +00001322 p=(char *) threshold_map;
1323 while (((isspace((int) ((unsigned char) *p)) != 0) || (*p == ',')) &&
1324 (*p != '\0'))
1325 p++;
1326 threshold_map=p;
1327 while (((isspace((int) ((unsigned char) *p)) == 0) && (*p != ',')) &&
1328 (*p != '\0'))
cristy3ed852e2009-09-05 21:47:34 +00001329 {
cristy4e0b82a2011-09-29 12:47:44 +00001330 if ((p-threshold_map) >= (MaxTextExtent-1))
1331 break;
1332 token[p-threshold_map]=(*p);
1333 p++;
cristy3ed852e2009-09-05 21:47:34 +00001334 }
cristy4e0b82a2011-09-29 12:47:44 +00001335 token[p-threshold_map]='\0';
1336 map=GetThresholdMap(token,exception);
1337 if (map == (ThresholdMap *) NULL)
1338 {
1339 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
1340 "InvalidArgument","%s : '%s'","ordered-dither",threshold_map);
cristy574cc262011-08-05 01:23:58 +00001341 return(MagickFalse);
cristy4e0b82a2011-09-29 12:47:44 +00001342 }
1343 for (i=0; i < MaxPixelChannels; i++)
1344 levels[i]=2.0;
1345 p=strchr((char *) threshold_map,',');
1346 if ((p != (char *) NULL) && (isdigit((int) ((unsigned char) *(++p))) != 0))
1347 for (i=0; (*p != '\0') && (i < MaxPixelChannels); i++)
1348 {
1349 GetMagickToken(p,&p,token);
1350 if (*token == ',')
1351 GetMagickToken(p,&p,token);
cristydbdd0e32011-11-04 23:29:40 +00001352 levels[i]=StringToDouble(token,(char **) NULL);
cristy4e0b82a2011-09-29 12:47:44 +00001353 }
1354 for (i=0; i < MaxPixelChannels; i++)
1355 if (fabs(levels[i]) >= 1)
1356 levels[i]-=1.0;
1357 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1358 return(MagickFalse);
1359 status=MagickTrue;
1360 progress=0;
cristydb070952012-04-20 14:33:00 +00001361 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00001362#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristybd065992012-08-09 15:27:39 +00001363 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00001364 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00001365#endif
cristy4e0b82a2011-09-29 12:47:44 +00001366 for (y=0; y < (ssize_t) image->rows; y++)
1367 {
1368 register ssize_t
1369 x;
1370
1371 register Quantum
1372 *restrict q;
1373
1374 if (status == MagickFalse)
1375 continue;
1376 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
1377 if (q == (Quantum *) NULL)
1378 {
1379 status=MagickFalse;
1380 continue;
1381 }
1382 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001383 {
cristybb503372010-05-27 20:51:26 +00001384 register ssize_t
cristy4e0b82a2011-09-29 12:47:44 +00001385 i;
cristy3ed852e2009-09-05 21:47:34 +00001386
cristy4e0b82a2011-09-29 12:47:44 +00001387 ssize_t
1388 n;
cristy3ed852e2009-09-05 21:47:34 +00001389
cristy4e0b82a2011-09-29 12:47:44 +00001390 n=0;
cristy10a6c612012-01-29 21:41:05 +00001391 if (GetPixelMask(image,q) != 0)
1392 {
1393 q+=GetPixelChannels(image);
1394 continue;
1395 }
cristy4e0b82a2011-09-29 12:47:44 +00001396 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
cristy3ed852e2009-09-05 21:47:34 +00001397 {
cristyabace412011-12-11 15:56:53 +00001398 PixelChannel
1399 channel;
1400
cristy4e0b82a2011-09-29 12:47:44 +00001401 PixelTrait
1402 traits;
1403
1404 ssize_t
cristy3f5d8152011-09-29 13:00:19 +00001405 level,
1406 threshold;
cristy3ed852e2009-09-05 21:47:34 +00001407
cristycf1296e2012-08-26 23:40:49 +00001408 channel=GetPixelChannelChannel(image,i);
1409 traits=GetPixelChannelTraits(image,channel);
cristy4e0b82a2011-09-29 12:47:44 +00001410 if ((traits & UpdatePixelTrait) == 0)
1411 continue;
cristy3f5d8152011-09-29 13:00:19 +00001412 if (fabs(levels[n++]) < MagickEpsilon)
1413 continue;
1414 threshold=(ssize_t) (QuantumScale*q[i]*(levels[n]*(map->divisor-1)+1));
1415 level=threshold/(map->divisor-1);
1416 threshold-=level*(map->divisor-1);
cristyada285b2012-07-07 19:00:46 +00001417 q[i]=ClampToQuantum((double) (level+(threshold >=
cristye42f6582012-02-11 17:59:50 +00001418 map->levels[(x % map->width)+map->width*(y % map->height)]))*
1419 QuantumRange/levels[n]);
cristy4e0b82a2011-09-29 12:47:44 +00001420 n++;
cristy3ed852e2009-09-05 21:47:34 +00001421 }
cristy4e0b82a2011-09-29 12:47:44 +00001422 q+=GetPixelChannels(image);
1423 }
1424 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1425 status=MagickFalse;
1426 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1427 {
1428 MagickBooleanType
1429 proceed;
cristy3ed852e2009-09-05 21:47:34 +00001430
cristyb5d5f722009-11-04 03:03:49 +00001431#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00001432 #pragma omp critical (MagickCore_OrderedPosterizeImage)
cristy3ed852e2009-09-05 21:47:34 +00001433#endif
cristy4e0b82a2011-09-29 12:47:44 +00001434 proceed=SetImageProgress(image,DitherImageTag,progress++,image->rows);
1435 if (proceed == MagickFalse)
1436 status=MagickFalse;
1437 }
cristy3ed852e2009-09-05 21:47:34 +00001438 }
cristy4e0b82a2011-09-29 12:47:44 +00001439 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00001440 map=DestroyThresholdMap(map);
1441 return(MagickTrue);
1442}
1443
1444/*
1445%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1446% %
1447% %
1448% %
1449% R a n d o m T h r e s h o l d I m a g e %
1450% %
1451% %
1452% %
1453%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1454%
1455% RandomThresholdImage() changes the value of individual pixels based on the
1456% intensity of each pixel compared to a random threshold. The result is a
1457% low-contrast, two color image.
1458%
1459% The format of the RandomThresholdImage method is:
1460%
cristyf4ad9df2011-07-08 16:49:03 +00001461% MagickBooleanType RandomThresholdImage(Image *image,
cristy3ed852e2009-09-05 21:47:34 +00001462% const char *thresholds,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001463%
1464% A description of each parameter follows:
1465%
1466% o image: the image.
1467%
cristy3ed852e2009-09-05 21:47:34 +00001468% o thresholds: a geometry string containing low,high thresholds. If the
1469% string contains 2x2, 3x3, or 4x4, an ordered dither of order 2, 3, or 4
1470% is performed instead.
1471%
1472% o exception: return any errors or warnings in this structure.
1473%
1474*/
cristy3ed852e2009-09-05 21:47:34 +00001475MagickExport MagickBooleanType RandomThresholdImage(Image *image,
1476 const char *thresholds,ExceptionInfo *exception)
1477{
cristy3ed852e2009-09-05 21:47:34 +00001478#define ThresholdImageTag "Threshold/Image"
1479
cristyfa112112010-01-04 17:48:07 +00001480 CacheView
1481 *image_view;
1482
cristy3ed852e2009-09-05 21:47:34 +00001483 GeometryInfo
1484 geometry_info;
1485
1486 MagickStatusType
1487 flags;
1488
cristy3ed852e2009-09-05 21:47:34 +00001489 MagickBooleanType
1490 status;
1491
cristy5f959472010-05-27 22:19:46 +00001492 MagickOffsetType
1493 progress;
1494
cristy4c08aed2011-07-01 19:47:50 +00001495 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001496 threshold;
1497
cristya19f1d72012-08-07 18:24:38 +00001498 double
cristy3ed852e2009-09-05 21:47:34 +00001499 min_threshold,
1500 max_threshold;
1501
1502 RandomInfo
cristyfa112112010-01-04 17:48:07 +00001503 **restrict random_info;
cristy3ed852e2009-09-05 21:47:34 +00001504
cristy5f959472010-05-27 22:19:46 +00001505 ssize_t
1506 y;
1507
glennrpb36143f2012-09-24 18:26:55 +00001508#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy57340e02012-05-05 00:53:23 +00001509 unsigned long
1510 key;
glennrpb36143f2012-09-24 18:26:55 +00001511#endif
cristy57340e02012-05-05 00:53:23 +00001512
cristy3ed852e2009-09-05 21:47:34 +00001513 assert(image != (Image *) NULL);
1514 assert(image->signature == MagickSignature);
1515 if (image->debug != MagickFalse)
1516 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1517 assert(exception != (ExceptionInfo *) NULL);
1518 assert(exception->signature == MagickSignature);
1519 if (thresholds == (const char *) NULL)
1520 return(MagickTrue);
cristye7452652012-04-14 01:34:21 +00001521 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1522 return(MagickFalse);
cristy4c08aed2011-07-01 19:47:50 +00001523 GetPixelInfo(image,&threshold);
cristy3ed852e2009-09-05 21:47:34 +00001524 min_threshold=0.0;
cristya19f1d72012-08-07 18:24:38 +00001525 max_threshold=(double) QuantumRange;
cristy3ed852e2009-09-05 21:47:34 +00001526 flags=ParseGeometry(thresholds,&geometry_info);
1527 min_threshold=geometry_info.rho;
1528 max_threshold=geometry_info.sigma;
1529 if ((flags & SigmaValue) == 0)
1530 max_threshold=min_threshold;
1531 if (strchr(thresholds,'%') != (char *) NULL)
1532 {
cristya19f1d72012-08-07 18:24:38 +00001533 max_threshold*=(double) (0.01*QuantumRange);
1534 min_threshold*=(double) (0.01*QuantumRange);
cristy3ed852e2009-09-05 21:47:34 +00001535 }
cristy3ed852e2009-09-05 21:47:34 +00001536 /*
1537 Random threshold image.
1538 */
1539 status=MagickTrue;
1540 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00001541 random_info=AcquireRandomInfoThreadSet();
glennrpb36143f2012-09-24 18:26:55 +00001542#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy57340e02012-05-05 00:53:23 +00001543 key=GetRandomSecretKey(random_info[0]);
glennrpb36143f2012-09-24 18:26:55 +00001544#endif
cristydb070952012-04-20 14:33:00 +00001545 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00001546#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristybd065992012-08-09 15:27:39 +00001547 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00001548 dynamic_number_threads(image,image->columns,image->rows,key == ~0UL)
cristy3ed852e2009-09-05 21:47:34 +00001549#endif
cristybb503372010-05-27 20:51:26 +00001550 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001551 {
cristy5c9e6f22010-09-17 17:31:01 +00001552 const int
1553 id = GetOpenMPThreadId();
cristy6ebe97c2010-07-03 01:17:28 +00001554
cristy4c08aed2011-07-01 19:47:50 +00001555 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001556 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001557
cristy5c9e6f22010-09-17 17:31:01 +00001558 register ssize_t
1559 x;
1560
cristy3ed852e2009-09-05 21:47:34 +00001561 if (status == MagickFalse)
1562 continue;
1563 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001564 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001565 {
1566 status=MagickFalse;
1567 continue;
1568 }
cristybb503372010-05-27 20:51:26 +00001569 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001570 {
cristy5f9f2462011-09-28 23:37:58 +00001571 register ssize_t
1572 i;
1573
cristy10a6c612012-01-29 21:41:05 +00001574 if (GetPixelMask(image,q) != 0)
1575 {
1576 q+=GetPixelChannels(image);
1577 continue;
1578 }
cristy5f9f2462011-09-28 23:37:58 +00001579 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1580 {
cristya19f1d72012-08-07 18:24:38 +00001581 double
cristy5f9f2462011-09-28 23:37:58 +00001582 threshold;
1583
cristyabace412011-12-11 15:56:53 +00001584 PixelChannel
1585 channel;
1586
cristy5f9f2462011-09-28 23:37:58 +00001587 PixelTrait
1588 traits;
1589
cristycf1296e2012-08-26 23:40:49 +00001590 channel=GetPixelChannelChannel(image,i);
1591 traits=GetPixelChannelTraits(image,channel);
cristy5f9f2462011-09-28 23:37:58 +00001592 if ((traits & UpdatePixelTrait) == 0)
1593 continue;
cristya19f1d72012-08-07 18:24:38 +00001594 if ((double) q[i] < min_threshold)
cristy5f9f2462011-09-28 23:37:58 +00001595 threshold=min_threshold;
1596 else
cristya19f1d72012-08-07 18:24:38 +00001597 if ((double) q[i] > max_threshold)
cristy5f9f2462011-09-28 23:37:58 +00001598 threshold=max_threshold;
cristy3ed852e2009-09-05 21:47:34 +00001599 else
cristya19f1d72012-08-07 18:24:38 +00001600 threshold=(double) (QuantumRange*
cristy5f9f2462011-09-28 23:37:58 +00001601 GetPseudoRandomValue(random_info[id]));
cristya19f1d72012-08-07 18:24:38 +00001602 q[i]=(double) q[i] <= threshold ? 0 : QuantumRange;
cristy5f9f2462011-09-28 23:37:58 +00001603 }
cristyed231572011-07-14 02:18:59 +00001604 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001605 }
1606 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1607 status=MagickFalse;
1608 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1609 {
1610 MagickBooleanType
1611 proceed;
1612
cristyb5d5f722009-11-04 03:03:49 +00001613#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00001614 #pragma omp critical (MagickCore_RandomThresholdImage)
cristy3ed852e2009-09-05 21:47:34 +00001615#endif
1616 proceed=SetImageProgress(image,ThresholdImageTag,progress++,
1617 image->rows);
1618 if (proceed == MagickFalse)
1619 status=MagickFalse;
1620 }
1621 }
1622 image_view=DestroyCacheView(image_view);
1623 random_info=DestroyRandomInfoThreadSet(random_info);
1624 return(status);
1625}
1626
1627/*
1628%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1629% %
1630% %
1631% %
1632% W h i t e T h r e s h o l d I m a g e %
1633% %
1634% %
1635% %
1636%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1637%
1638% WhiteThresholdImage() is like ThresholdImage() but forces all pixels above
cristy4e101302009-09-17 12:49:12 +00001639% the threshold into white while leaving all pixels at or below the threshold
cristy3ed852e2009-09-05 21:47:34 +00001640% unchanged.
1641%
1642% The format of the WhiteThresholdImage method is:
1643%
cristyf4ad9df2011-07-08 16:49:03 +00001644% MagickBooleanType WhiteThresholdImage(Image *image,
1645% const char *threshold,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001646%
1647% A description of each parameter follows:
1648%
1649% o image: the image.
1650%
cristy3ed852e2009-09-05 21:47:34 +00001651% o threshold: Define the threshold value.
1652%
1653% o exception: return any errors or warnings in this structure.
1654%
1655*/
1656MagickExport MagickBooleanType WhiteThresholdImage(Image *image,
cristyf4ad9df2011-07-08 16:49:03 +00001657 const char *thresholds,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001658{
1659#define ThresholdImageTag "Threshold/Image"
1660
cristy5f959472010-05-27 22:19:46 +00001661 CacheView
1662 *image_view;
1663
cristy3ed852e2009-09-05 21:47:34 +00001664 GeometryInfo
1665 geometry_info;
1666
cristy3ed852e2009-09-05 21:47:34 +00001667 MagickBooleanType
1668 status;
1669
cristy5f959472010-05-27 22:19:46 +00001670 MagickOffsetType
1671 progress;
1672
cristyd6803382012-04-10 01:41:25 +00001673 PixelInfo
cristya12d8ba2012-04-29 16:33:41 +00001674 threshold;
cristy5f9f2462011-09-28 23:37:58 +00001675
cristy3ed852e2009-09-05 21:47:34 +00001676 MagickStatusType
1677 flags;
1678
cristy5f959472010-05-27 22:19:46 +00001679 ssize_t
1680 y;
cristy3ed852e2009-09-05 21:47:34 +00001681
1682 assert(image != (Image *) NULL);
1683 assert(image->signature == MagickSignature);
1684 if (image->debug != MagickFalse)
1685 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1686 if (thresholds == (const char *) NULL)
1687 return(MagickTrue);
cristy574cc262011-08-05 01:23:58 +00001688 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001689 return(MagickFalse);
cristy23e55c02012-04-10 01:21:56 +00001690 if (IsGrayColorspace(image->colorspace) != MagickFalse)
cristyb09db112012-07-11 12:04:31 +00001691 (void) TransformImageColorspace(image,RGBColorspace,exception);
cristya12d8ba2012-04-29 16:33:41 +00001692 GetPixelInfo(image,&threshold);
cristy3ed852e2009-09-05 21:47:34 +00001693 flags=ParseGeometry(thresholds,&geometry_info);
cristya12d8ba2012-04-29 16:33:41 +00001694 threshold.red=geometry_info.rho;
1695 threshold.green=geometry_info.rho;
1696 threshold.blue=geometry_info.rho;
1697 threshold.black=geometry_info.rho;
1698 threshold.alpha=100.0;
cristy5f9f2462011-09-28 23:37:58 +00001699 if ((flags & SigmaValue) != 0)
cristya12d8ba2012-04-29 16:33:41 +00001700 threshold.green=geometry_info.sigma;
cristy5f9f2462011-09-28 23:37:58 +00001701 if ((flags & XiValue) != 0)
cristya12d8ba2012-04-29 16:33:41 +00001702 threshold.blue=geometry_info.xi;
cristy5f9f2462011-09-28 23:37:58 +00001703 if ((flags & PsiValue) != 0)
cristya12d8ba2012-04-29 16:33:41 +00001704 threshold.alpha=geometry_info.psi;
1705 if (threshold.colorspace == CMYKColorspace)
cristyd6803382012-04-10 01:41:25 +00001706 {
1707 if ((flags & PsiValue) != 0)
cristya12d8ba2012-04-29 16:33:41 +00001708 threshold.black=geometry_info.psi;
cristyd6803382012-04-10 01:41:25 +00001709 if ((flags & ChiValue) != 0)
cristya12d8ba2012-04-29 16:33:41 +00001710 threshold.alpha=geometry_info.chi;
1711 }
1712 if ((flags & PercentValue) != 0)
1713 {
cristy65d4e5e2012-10-17 12:22:24 +00001714 threshold.red*=(MagickRealType) (QuantumRange/100.0);
1715 threshold.green*=(MagickRealType) (QuantumRange/100.0);
1716 threshold.blue*=(MagickRealType) (QuantumRange/100.0);
1717 threshold.black*=(MagickRealType) (QuantumRange/100.0);
1718 threshold.alpha*=(MagickRealType) (QuantumRange/100.0);
cristyd6803382012-04-10 01:41:25 +00001719 }
cristy3ed852e2009-09-05 21:47:34 +00001720 /*
1721 White threshold image.
1722 */
1723 status=MagickTrue;
1724 progress=0;
cristydb070952012-04-20 14:33:00 +00001725 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00001726#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristybd065992012-08-09 15:27:39 +00001727 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00001728 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00001729#endif
cristybb503372010-05-27 20:51:26 +00001730 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001731 {
cristybb503372010-05-27 20:51:26 +00001732 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001733 x;
1734
cristy4c08aed2011-07-01 19:47:50 +00001735 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001736 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001737
1738 if (status == MagickFalse)
1739 continue;
1740 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001741 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001742 {
1743 status=MagickFalse;
1744 continue;
1745 }
cristybb503372010-05-27 20:51:26 +00001746 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001747 {
cristy81629aa2012-07-12 20:08:52 +00001748 double
1749 pixel;
1750
cristy188f29a2012-06-24 19:09:53 +00001751 register ssize_t
1752 i;
1753
cristy10a6c612012-01-29 21:41:05 +00001754 if (GetPixelMask(image,q) != 0)
1755 {
1756 q+=GetPixelChannels(image);
1757 continue;
1758 }
cristyf13c5942012-08-08 23:50:11 +00001759 pixel=GetPixelIntensity(image,q);
cristy188f29a2012-06-24 19:09:53 +00001760 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1761 {
1762 PixelChannel
1763 channel;
1764
1765 PixelTrait
1766 traits;
1767
cristycf1296e2012-08-26 23:40:49 +00001768 channel=GetPixelChannelChannel(image,i);
1769 traits=GetPixelChannelTraits(image,channel);
cristy188f29a2012-06-24 19:09:53 +00001770 if ((traits & UpdatePixelTrait) == 0)
1771 continue;
cristy81629aa2012-07-12 20:08:52 +00001772 if (image->channel_mask != DefaultChannels)
cristyf13c5942012-08-08 23:50:11 +00001773 pixel=(double) q[i];
cristy81629aa2012-07-12 20:08:52 +00001774 if (pixel > GetPixelInfoChannel(&threshold,channel))
cristy188f29a2012-06-24 19:09:53 +00001775 q[i]=QuantumRange;
1776 }
cristyed231572011-07-14 02:18:59 +00001777 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001778 }
1779 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1780 status=MagickFalse;
1781 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1782 {
1783 MagickBooleanType
1784 proceed;
1785
cristyb5d5f722009-11-04 03:03:49 +00001786#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristya12d8ba2012-04-29 16:33:41 +00001787 #pragma omp critical (MagickCore_WhiteThresholdImage)
cristy3ed852e2009-09-05 21:47:34 +00001788#endif
1789 proceed=SetImageProgress(image,ThresholdImageTag,progress++,
1790 image->rows);
1791 if (proceed == MagickFalse)
1792 status=MagickFalse;
1793 }
1794 }
1795 image_view=DestroyCacheView(image_view);
1796 return(status);
1797}