blob: 129c5c85218944440524055781abf6e3586f9435 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% PPPP AAA IIIII N N TTTTT %
7% P P A A I NN N T %
8% PPPP AAAAA I N N N T %
9% P A A I N NN T %
10% P A A IIIII N N T %
11% %
12% %
13% Methods to Paint on an Image %
14% %
15% Software Design %
16% John Cristy %
17% July 1998 %
18% %
19% %
cristy7e41fe82010-12-04 23:12:08 +000020% Copyright 1999-2011 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 Include declarations.
41*/
cristy4c08aed2011-07-01 19:47:50 +000042#include "MagickCore/studio.h"
43#include "MagickCore/color.h"
44#include "MagickCore/color-private.h"
45#include "MagickCore/colorspace-private.h"
46#include "MagickCore/composite.h"
47#include "MagickCore/composite-private.h"
48#include "MagickCore/draw.h"
49#include "MagickCore/draw-private.h"
50#include "MagickCore/exception.h"
51#include "MagickCore/exception-private.h"
52#include "MagickCore/gem.h"
53#include "MagickCore/monitor.h"
54#include "MagickCore/monitor-private.h"
55#include "MagickCore/paint.h"
56#include "MagickCore/pixel-accessor.h"
57#include "MagickCore/string_.h"
58#include "MagickCore/thread-private.h"
cristy3ed852e2009-09-05 21:47:34 +000059
cristy3ed852e2009-09-05 21:47:34 +000060/*
61%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
62% %
63% %
64% %
65% F l o o d f i l l P a i n t I m a g e %
66% %
67% %
68% %
69%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
70%
71% FloodfillPaintImage() changes the color value of any pixel that matches
72% target and is an immediate neighbor. If the method FillToBorderMethod is
73% specified, the color value is changed for any neighbor pixel that does not
74% match the bordercolor member of image.
75%
76% By default target must match a particular pixel color exactly.
77% However, in many cases two colors may differ by a small amount. The
78% fuzz member of image defines how much tolerance is acceptable to
79% consider two colors as the same. For example, set fuzz to 10 and the
80% color red at intensities of 100 and 102 respectively are now
81% interpreted as the same color for the purposes of the floodfill.
82%
83% The format of the FloodfillPaintImage method is:
84%
85% MagickBooleanType FloodfillPaintImage(Image *image,
cristyd42d9952011-07-08 14:21:50 +000086% const DrawInfo *draw_info,const PixelInfo target,
87% const ssize_t x_offset,const ssize_t y_offset,
88% const MagickBooleanType invert)
cristy3ed852e2009-09-05 21:47:34 +000089%
90% A description of each parameter follows:
91%
92% o image: the image.
93%
cristy3ed852e2009-09-05 21:47:34 +000094% o draw_info: the draw info.
95%
96% o target: the RGB value of the target color.
97%
98% o x_offset,y_offset: the starting location of the operation.
99%
100% o invert: paint any pixel that does not match the target color.
101%
102*/
103MagickExport MagickBooleanType FloodfillPaintImage(Image *image,
cristyd42d9952011-07-08 14:21:50 +0000104 const DrawInfo *draw_info,const PixelInfo *target,const ssize_t x_offset,
105 const ssize_t y_offset,const MagickBooleanType invert)
cristy3ed852e2009-09-05 21:47:34 +0000106{
107#define MaxStacksize (1UL << 15)
108#define PushSegmentStack(up,left,right,delta) \
109{ \
110 if (s >= (segment_stack+MaxStacksize)) \
111 ThrowBinaryException(DrawError,"SegmentStackOverflow",image->filename) \
112 else \
113 { \
cristybb503372010-05-27 20:51:26 +0000114 if ((((up)+(delta)) >= 0) && (((up)+(delta)) < (ssize_t) image->rows)) \
cristy3ed852e2009-09-05 21:47:34 +0000115 { \
116 s->x1=(double) (left); \
117 s->y1=(double) (up); \
118 s->x2=(double) (right); \
119 s->y2=(double) (delta); \
120 s++; \
121 } \
122 } \
123}
124
cristyb0d3bb92010-09-22 14:37:58 +0000125 CacheView
126 *floodplane_view,
127 *image_view;
128
cristy3ed852e2009-09-05 21:47:34 +0000129 ExceptionInfo
130 *exception;
131
132 Image
133 *floodplane_image;
134
cristy3ed852e2009-09-05 21:47:34 +0000135 MagickBooleanType
136 skip;
137
cristy4c08aed2011-07-01 19:47:50 +0000138 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000139 fill,
140 pixel;
141
142 PixelPacket
143 fill_color;
144
145 register SegmentInfo
146 *s;
147
148 SegmentInfo
149 *segment_stack;
150
cristy9d314ff2011-03-09 01:30:28 +0000151 ssize_t
152 offset,
153 start,
154 x,
155 x1,
156 x2,
157 y;
158
cristy3ed852e2009-09-05 21:47:34 +0000159 /*
160 Check boundary conditions.
161 */
162 assert(image != (Image *) NULL);
163 assert(image->signature == MagickSignature);
164 if (image->debug != MagickFalse)
165 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
166 assert(draw_info != (DrawInfo *) NULL);
167 assert(draw_info->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +0000168 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
cristy3ed852e2009-09-05 21:47:34 +0000169 return(MagickFalse);
cristybb503372010-05-27 20:51:26 +0000170 if ((y_offset < 0) || (y_offset >= (ssize_t) image->rows))
cristy3ed852e2009-09-05 21:47:34 +0000171 return(MagickFalse);
cristy574cc262011-08-05 01:23:58 +0000172 exception=(&image->exception);
173 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000174 return(MagickFalse);
175 if (image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +0000176 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +0000177 /*
178 Set floodfill state.
179 */
180 floodplane_image=CloneImage(image,0,0,MagickTrue,&image->exception);
181 if (floodplane_image == (Image *) NULL)
182 return(MagickFalse);
cristy63240882011-08-05 19:05:27 +0000183 (void) SetImageAlphaChannel(floodplane_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +0000184 segment_stack=(SegmentInfo *) AcquireQuantumMemory(MaxStacksize,
185 sizeof(*segment_stack));
186 if (segment_stack == (SegmentInfo *) NULL)
187 {
188 floodplane_image=DestroyImage(floodplane_image);
189 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
190 image->filename);
191 }
192 /*
193 Push initial segment on stack.
194 */
cristy3ed852e2009-09-05 21:47:34 +0000195 x=x_offset;
196 y=y_offset;
197 start=0;
198 s=segment_stack;
199 PushSegmentStack(y,x,x,1);
200 PushSegmentStack(y+1,x,x,-1);
cristy4c08aed2011-07-01 19:47:50 +0000201 GetPixelInfo(image,&fill);
202 GetPixelInfo(image,&pixel);
cristyb0d3bb92010-09-22 14:37:58 +0000203 image_view=AcquireCacheView(image);
204 floodplane_view=AcquireCacheView(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000205 while (s > segment_stack)
206 {
cristy4c08aed2011-07-01 19:47:50 +0000207 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000208 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000209
cristybb503372010-05-27 20:51:26 +0000210 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000211 x;
212
cristy4c08aed2011-07-01 19:47:50 +0000213 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000214 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000215
216 /*
217 Pop segment off stack.
218 */
219 s--;
cristybb503372010-05-27 20:51:26 +0000220 x1=(ssize_t) s->x1;
221 x2=(ssize_t) s->x2;
222 offset=(ssize_t) s->y2;
223 y=(ssize_t) s->y1+offset;
cristy3ed852e2009-09-05 21:47:34 +0000224 /*
225 Recolor neighboring pixels.
226 */
cristyb0d3bb92010-09-22 14:37:58 +0000227 p=GetCacheViewVirtualPixels(image_view,0,y,(size_t) (x1+1),1,exception);
228 q=GetCacheViewAuthenticPixels(floodplane_view,0,y,(size_t) (x1+1),1,
cristy3ed852e2009-09-05 21:47:34 +0000229 exception);
cristy4c08aed2011-07-01 19:47:50 +0000230 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000231 break;
cristyed231572011-07-14 02:18:59 +0000232 p+=x1*GetPixelChannels(image);
233 q+=x1*GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000234 for (x=x1; x >= 0; x--)
235 {
cristy4c08aed2011-07-01 19:47:50 +0000236 if (GetPixelAlpha(image,q) == TransparentAlpha)
cristy3ed852e2009-09-05 21:47:34 +0000237 break;
cristy4c08aed2011-07-01 19:47:50 +0000238 SetPixelInfo(image,p,&pixel);
239 if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert)
cristy3ed852e2009-09-05 21:47:34 +0000240 break;
cristy4c08aed2011-07-01 19:47:50 +0000241 SetPixelAlpha(floodplane_image,TransparentAlpha,q);
cristyed231572011-07-14 02:18:59 +0000242 p-=GetPixelChannels(image);
243 q-=GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000244 }
cristyb0d3bb92010-09-22 14:37:58 +0000245 if (SyncCacheViewAuthenticPixels(floodplane_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000246 break;
247 skip=x >= x1 ? MagickTrue : MagickFalse;
248 if (skip == MagickFalse)
249 {
250 start=x+1;
251 if (start < x1)
252 PushSegmentStack(y,start,x1-1,-offset);
253 x=x1+1;
254 }
255 do
256 {
257 if (skip == MagickFalse)
258 {
cristybb503372010-05-27 20:51:26 +0000259 if (x < (ssize_t) image->columns)
cristy3ed852e2009-09-05 21:47:34 +0000260 {
cristyb0d3bb92010-09-22 14:37:58 +0000261 p=GetCacheViewVirtualPixels(image_view,x,y,image->columns-x,1,
cristy3ed852e2009-09-05 21:47:34 +0000262 exception);
cristyb0d3bb92010-09-22 14:37:58 +0000263 q=GetCacheViewAuthenticPixels(floodplane_view,x,y,
264 image->columns-x,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000265 if ((p == (const Quantum *) NULL) ||
266 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000267 break;
cristybb503372010-05-27 20:51:26 +0000268 for ( ; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000269 {
cristy4c08aed2011-07-01 19:47:50 +0000270 if (GetPixelAlpha(image,q) == TransparentAlpha)
cristy3ed852e2009-09-05 21:47:34 +0000271 break;
cristy4c08aed2011-07-01 19:47:50 +0000272 SetPixelInfo(image,p,&pixel);
273 if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert)
cristy3ed852e2009-09-05 21:47:34 +0000274 break;
cristy4c08aed2011-07-01 19:47:50 +0000275 SetPixelAlpha(floodplane_image,
276 TransparentAlpha,q);
cristyed231572011-07-14 02:18:59 +0000277 p+=GetPixelChannels(image);
278 q+=GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000279 }
cristyb0d3bb92010-09-22 14:37:58 +0000280 if (SyncCacheViewAuthenticPixels(floodplane_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000281 break;
282 }
283 PushSegmentStack(y,start,x-1,offset);
284 if (x > (x2+1))
285 PushSegmentStack(y,x2+1,x-1,-offset);
286 }
287 skip=MagickFalse;
288 x++;
289 if (x <= x2)
290 {
cristyb0d3bb92010-09-22 14:37:58 +0000291 p=GetCacheViewVirtualPixels(image_view,x,y,(size_t) (x2-x+1),1,
292 exception);
293 q=GetCacheViewAuthenticPixels(floodplane_view,x,y,(size_t) (x2-x+1),1,
cristy3ed852e2009-09-05 21:47:34 +0000294 exception);
cristy4c08aed2011-07-01 19:47:50 +0000295 if ((p == (const Quantum *) NULL) ||
296 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000297 break;
cristy3ed852e2009-09-05 21:47:34 +0000298 for ( ; x <= x2; x++)
299 {
cristy4c08aed2011-07-01 19:47:50 +0000300 if (GetPixelAlpha(image,q) == TransparentAlpha)
cristy3ed852e2009-09-05 21:47:34 +0000301 break;
cristy4c08aed2011-07-01 19:47:50 +0000302 SetPixelInfo(image,p,&pixel);
303 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
cristy3ed852e2009-09-05 21:47:34 +0000304 break;
cristyed231572011-07-14 02:18:59 +0000305 p+=GetPixelChannels(image);
306 q+=GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000307 }
308 }
309 start=x;
310 } while (x <= x2);
311 }
cristybb503372010-05-27 20:51:26 +0000312 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000313 {
cristy4c08aed2011-07-01 19:47:50 +0000314 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000315 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000316
cristybb503372010-05-27 20:51:26 +0000317 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000318 x;
319
cristy4c08aed2011-07-01 19:47:50 +0000320 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000321 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000322
323 /*
324 Tile fill color onto floodplane.
325 */
cristyb0d3bb92010-09-22 14:37:58 +0000326 p=GetCacheViewVirtualPixels(floodplane_view,0,y,image->columns,1,
327 exception);
328 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000329 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000330 break;
cristybb503372010-05-27 20:51:26 +0000331 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000332 {
cristy4c08aed2011-07-01 19:47:50 +0000333 if (GetPixelAlpha(floodplane_image,p) != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +0000334 {
335 (void) GetFillColor(draw_info,x,y,&fill_color);
cristy4c08aed2011-07-01 19:47:50 +0000336 SetPixelInfoPacket(image,&fill_color,&fill);
cristy3ed852e2009-09-05 21:47:34 +0000337 if (image->colorspace == CMYKColorspace)
338 ConvertRGBToCMYK(&fill);
cristyed231572011-07-14 02:18:59 +0000339 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000340 SetPixelRed(image,ClampToQuantum(fill.red),q);
cristyed231572011-07-14 02:18:59 +0000341 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000342 SetPixelGreen(image,ClampToQuantum(fill.green),q);
cristyed231572011-07-14 02:18:59 +0000343 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000344 SetPixelBlue(image,ClampToQuantum(fill.blue),q);
cristyed231572011-07-14 02:18:59 +0000345 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +0000346 (image->colorspace == CMYKColorspace))
cristy4c08aed2011-07-01 19:47:50 +0000347 SetPixelBlack(image,ClampToQuantum(fill.black),q);
cristyed231572011-07-14 02:18:59 +0000348 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000349 SetPixelAlpha(image,ClampToQuantum(fill.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +0000350 }
cristyed231572011-07-14 02:18:59 +0000351 p+=GetPixelChannels(floodplane_image);
352 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000353 }
cristyb0d3bb92010-09-22 14:37:58 +0000354 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000355 break;
356 }
cristyb0d3bb92010-09-22 14:37:58 +0000357 floodplane_view=DestroyCacheView(floodplane_view);
358 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +0000359 segment_stack=(SegmentInfo *) RelinquishMagickMemory(segment_stack);
360 floodplane_image=DestroyImage(floodplane_image);
cristybb503372010-05-27 20:51:26 +0000361 return(y == (ssize_t) image->rows ? MagickTrue : MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +0000362}
363
364/*
365%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
366% %
367% %
368% %
369+ G r a d i e n t I m a g e %
370% %
371% %
372% %
373%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
374%
cristycee97112010-05-28 00:44:52 +0000375% GradientImage() applies a continuously smooth color transitions along a
cristy3ed852e2009-09-05 21:47:34 +0000376% vector from one color to another.
377%
378% Note, the interface of this method will change in the future to support
379% more than one transistion.
380%
381% The format of the GradientImage method is:
382%
383% MagickBooleanType GradientImage(Image *image,const GradientType type,
384% const SpreadMethod method,const PixelPacket *start_color,
385% const PixelPacket *stop_color)
386%
387% A description of each parameter follows:
388%
389% o image: the image.
390%
391% o type: the gradient type: linear or radial.
392%
393% o spread: the gradient spread meathod: pad, reflect, or repeat.
394%
395% o start_color: the start color.
396%
397% o stop_color: the stop color.
398%
cristy3ed852e2009-09-05 21:47:34 +0000399*/
cristy117ff172010-08-15 21:35:32 +0000400
401static inline double MagickMax(const double x,const double y)
402{
403 return(x > y ? x : y);
404}
405
cristy3ed852e2009-09-05 21:47:34 +0000406MagickExport MagickBooleanType GradientImage(Image *image,
407 const GradientType type,const SpreadMethod method,
408 const PixelPacket *start_color,const PixelPacket *stop_color)
409{
410 DrawInfo
411 *draw_info;
412
413 GradientInfo
414 *gradient;
415
416 MagickBooleanType
417 status;
418
cristybb503372010-05-27 20:51:26 +0000419 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000420 i;
421
422 /*
423 Set gradient start-stop end points.
424 */
425 assert(image != (const Image *) NULL);
426 assert(image->signature == MagickSignature);
427 if (image->debug != MagickFalse)
428 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
429 assert(start_color != (const PixelPacket *) NULL);
430 assert(stop_color != (const PixelPacket *) NULL);
431 draw_info=AcquireDrawInfo();
432 gradient=(&draw_info->gradient);
433 gradient->type=type;
434 gradient->bounding_box.width=image->columns;
435 gradient->bounding_box.height=image->rows;
436 gradient->gradient_vector.x2=(double) image->columns-1.0;
437 gradient->gradient_vector.y2=(double) image->rows-1.0;
438 if ((type == LinearGradient) && (gradient->gradient_vector.y2 != 0.0))
439 gradient->gradient_vector.x2=0.0;
440 gradient->center.x=(double) gradient->gradient_vector.x2/2.0;
441 gradient->center.y=(double) gradient->gradient_vector.y2/2.0;
442 gradient->radius=MagickMax(gradient->center.x,gradient->center.y);
443 gradient->spread=method;
444 /*
445 Define the gradient to fill between the stops.
446 */
447 gradient->number_stops=2;
448 gradient->stops=(StopInfo *) AcquireQuantumMemory(gradient->number_stops,
449 sizeof(*gradient->stops));
450 if (gradient->stops == (StopInfo *) NULL)
451 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
452 image->filename);
453 (void) ResetMagickMemory(gradient->stops,0,gradient->number_stops*
454 sizeof(*gradient->stops));
cristybb503372010-05-27 20:51:26 +0000455 for (i=0; i < (ssize_t) gradient->number_stops; i++)
cristy4c08aed2011-07-01 19:47:50 +0000456 GetPixelInfo(image,&gradient->stops[i].color);
457 SetPixelInfoPacket(image,start_color,&gradient->stops[0].color);
cristy3ed852e2009-09-05 21:47:34 +0000458 gradient->stops[0].offset=0.0;
cristy4c08aed2011-07-01 19:47:50 +0000459 SetPixelInfoPacket(image,stop_color,&gradient->stops[1].color);
cristy3ed852e2009-09-05 21:47:34 +0000460 gradient->stops[1].offset=1.0;
461 /*
462 Draw a gradient on the image.
463 */
464 status=DrawGradientImage(image,draw_info);
465 draw_info=DestroyDrawInfo(draw_info);
cristy4c08aed2011-07-01 19:47:50 +0000466 if ((start_color->alpha == OpaqueAlpha) && (stop_color->alpha == OpaqueAlpha))
cristy3ed852e2009-09-05 21:47:34 +0000467 image->matte=MagickFalse;
cristy4c08aed2011-07-01 19:47:50 +0000468 if ((IsPixelPacketGray(start_color) != MagickFalse) &&
469 (IsPixelPacketGray(stop_color) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +0000470 image->type=GrayscaleType;
471 return(status);
472}
473
474/*
475%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
476% %
477% %
478% %
479% O i l P a i n t I m a g e %
480% %
481% %
482% %
483%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
484%
485% OilPaintImage() applies a special effect filter that simulates an oil
486% painting. Each pixel is replaced by the most frequent color occurring
487% in a circular region defined by radius.
488%
489% The format of the OilPaintImage method is:
490%
491% Image *OilPaintImage(const Image *image,const double radius,
492% ExceptionInfo *exception)
493%
494% A description of each parameter follows:
495%
496% o image: the image.
497%
498% o radius: the radius of the circular neighborhood.
499%
500% o exception: return any errors or warnings in this structure.
501%
502*/
503
cristybb503372010-05-27 20:51:26 +0000504static size_t **DestroyHistogramThreadSet(size_t **histogram)
cristy3ed852e2009-09-05 21:47:34 +0000505{
cristybb503372010-05-27 20:51:26 +0000506 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000507 i;
508
cristybb503372010-05-27 20:51:26 +0000509 assert(histogram != (size_t **) NULL);
510 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
511 if (histogram[i] != (size_t *) NULL)
512 histogram[i]=(size_t *) RelinquishMagickMemory(histogram[i]);
cristyb41ee102010-10-04 16:46:15 +0000513 histogram=(size_t **) RelinquishMagickMemory(histogram);
cristy3ed852e2009-09-05 21:47:34 +0000514 return(histogram);
515}
516
cristybb503372010-05-27 20:51:26 +0000517static size_t **AcquireHistogramThreadSet(const size_t count)
cristy3ed852e2009-09-05 21:47:34 +0000518{
cristybb503372010-05-27 20:51:26 +0000519 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000520 i;
521
cristybb503372010-05-27 20:51:26 +0000522 size_t
cristy3ed852e2009-09-05 21:47:34 +0000523 **histogram,
524 number_threads;
525
526 number_threads=GetOpenMPMaximumThreads();
cristyb41ee102010-10-04 16:46:15 +0000527 histogram=(size_t **) AcquireQuantumMemory(number_threads,
cristy3ed852e2009-09-05 21:47:34 +0000528 sizeof(*histogram));
cristybb503372010-05-27 20:51:26 +0000529 if (histogram == (size_t **) NULL)
530 return((size_t **) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000531 (void) ResetMagickMemory(histogram,0,number_threads*sizeof(*histogram));
cristybb503372010-05-27 20:51:26 +0000532 for (i=0; i < (ssize_t) number_threads; i++)
cristy3ed852e2009-09-05 21:47:34 +0000533 {
cristybb503372010-05-27 20:51:26 +0000534 histogram[i]=(size_t *) AcquireQuantumMemory(count,
cristy3ed852e2009-09-05 21:47:34 +0000535 sizeof(**histogram));
cristybb503372010-05-27 20:51:26 +0000536 if (histogram[i] == (size_t *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000537 return(DestroyHistogramThreadSet(histogram));
538 }
539 return(histogram);
540}
541
542MagickExport Image *OilPaintImage(const Image *image,const double radius,
543 ExceptionInfo *exception)
544{
545#define NumberPaintBins 256
546#define OilPaintImageTag "OilPaint/Image"
547
cristyfa112112010-01-04 17:48:07 +0000548 CacheView
549 *image_view,
550 *paint_view;
551
cristy3ed852e2009-09-05 21:47:34 +0000552 Image
553 *paint_image;
554
cristy3ed852e2009-09-05 21:47:34 +0000555 MagickBooleanType
556 status;
557
cristybb503372010-05-27 20:51:26 +0000558 MagickOffsetType
559 progress;
560
561 size_t
cristyfa112112010-01-04 17:48:07 +0000562 **restrict histograms,
cristy3ed852e2009-09-05 21:47:34 +0000563 width;
564
cristybb503372010-05-27 20:51:26 +0000565 ssize_t
566 y;
567
cristy3ed852e2009-09-05 21:47:34 +0000568 /*
569 Initialize painted image attributes.
570 */
571 assert(image != (const Image *) NULL);
572 assert(image->signature == MagickSignature);
573 if (image->debug != MagickFalse)
574 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
575 assert(exception != (ExceptionInfo *) NULL);
576 assert(exception->signature == MagickSignature);
577 width=GetOptimalKernelWidth2D(radius,0.5);
cristy3ed852e2009-09-05 21:47:34 +0000578 paint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
579 if (paint_image == (Image *) NULL)
580 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000581 if (SetImageStorageClass(paint_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000582 {
cristy3ed852e2009-09-05 21:47:34 +0000583 paint_image=DestroyImage(paint_image);
584 return((Image *) NULL);
585 }
586 histograms=AcquireHistogramThreadSet(NumberPaintBins);
cristybb503372010-05-27 20:51:26 +0000587 if (histograms == (size_t **) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000588 {
589 paint_image=DestroyImage(paint_image);
590 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
591 }
592 /*
593 Oil paint image.
594 */
595 status=MagickTrue;
596 progress=0;
597 image_view=AcquireCacheView(image);
598 paint_view=AcquireCacheView(paint_image);
cristyb5d5f722009-11-04 03:03:49 +0000599#if defined(MAGICKCORE_OPENMP_SUPPORT)
600 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000601#endif
cristybb503372010-05-27 20:51:26 +0000602 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000603 {
cristy4c08aed2011-07-01 19:47:50 +0000604 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000605 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000606
cristybb503372010-05-27 20:51:26 +0000607 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000608 x;
609
cristy4c08aed2011-07-01 19:47:50 +0000610 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000611 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000612
cristybb503372010-05-27 20:51:26 +0000613 register size_t
cristy3ed852e2009-09-05 21:47:34 +0000614 *histogram;
615
616 if (status == MagickFalse)
617 continue;
cristyfe4ba002011-02-28 14:54:12 +0000618 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
619 (width/2L),image->columns+width,width,exception);
cristy3ed852e2009-09-05 21:47:34 +0000620 q=QueueCacheViewAuthenticPixels(paint_view,0,y,paint_image->columns,1,
621 exception);
cristy4c08aed2011-07-01 19:47:50 +0000622 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000623 {
624 status=MagickFalse;
625 continue;
626 }
cristy3ed852e2009-09-05 21:47:34 +0000627 histogram=histograms[GetOpenMPThreadId()];
cristybb503372010-05-27 20:51:26 +0000628 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000629 {
cristybb503372010-05-27 20:51:26 +0000630 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000631 i,
632 u;
633
cristybb503372010-05-27 20:51:26 +0000634 size_t
cristy3ed852e2009-09-05 21:47:34 +0000635 count;
636
cristy9d314ff2011-03-09 01:30:28 +0000637 ssize_t
638 j,
639 k,
640 v;
641
cristy3ed852e2009-09-05 21:47:34 +0000642 /*
643 Assign most frequent color.
644 */
645 i=0;
646 j=0;
647 count=0;
648 (void) ResetMagickMemory(histogram,0,NumberPaintBins*sizeof(*histogram));
cristybb503372010-05-27 20:51:26 +0000649 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +0000650 {
cristybb503372010-05-27 20:51:26 +0000651 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +0000652 {
cristy4c08aed2011-07-01 19:47:50 +0000653 k=(ssize_t) ScaleQuantumToChar(GetPixelIntensity(image,p+u+i));
cristy3ed852e2009-09-05 21:47:34 +0000654 histogram[k]++;
655 if (histogram[k] > count)
656 {
657 j=i+u;
658 count=histogram[k];
659 }
660 }
cristyd99b0962010-05-29 23:14:26 +0000661 i+=(ssize_t) (image->columns+width);
cristy3ed852e2009-09-05 21:47:34 +0000662 }
cristy4c08aed2011-07-01 19:47:50 +0000663 SetPixelRed(paint_image,GetPixelRed(image,p+j*
cristyed231572011-07-14 02:18:59 +0000664 GetPixelChannels(image)),q);
cristy4c08aed2011-07-01 19:47:50 +0000665 SetPixelGreen(paint_image,GetPixelGreen(image,p+j*
cristyed231572011-07-14 02:18:59 +0000666 GetPixelChannels(image)),q);
cristy4c08aed2011-07-01 19:47:50 +0000667 SetPixelBlue(paint_image,GetPixelBlue(image,p+j*
cristyed231572011-07-14 02:18:59 +0000668 GetPixelChannels(image)),q);
cristy3ed852e2009-09-05 21:47:34 +0000669 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +0000670 SetPixelBlack(paint_image,GetPixelBlack(image,p+j*
cristyed231572011-07-14 02:18:59 +0000671 GetPixelChannels(image)),q);
cristy4c08aed2011-07-01 19:47:50 +0000672 if (image->matte != MagickFalse)
673 SetPixelAlpha(paint_image,GetPixelAlpha(image,p+j*
cristyed231572011-07-14 02:18:59 +0000674 GetPixelChannels(image)),q);
675 p+=GetPixelChannels(image);
676 q+=GetPixelChannels(paint_image);
cristy3ed852e2009-09-05 21:47:34 +0000677 }
678 if (SyncCacheViewAuthenticPixels(paint_view,exception) == MagickFalse)
679 status=MagickFalse;
680 if (image->progress_monitor != (MagickProgressMonitor) NULL)
681 {
682 MagickBooleanType
683 proceed;
684
cristyb5d5f722009-11-04 03:03:49 +0000685#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000686 #pragma omp critical (MagickCore_OilPaintImage)
687#endif
688 proceed=SetImageProgress(image,OilPaintImageTag,progress++,image->rows);
689 if (proceed == MagickFalse)
690 status=MagickFalse;
691 }
692 }
693 paint_view=DestroyCacheView(paint_view);
694 image_view=DestroyCacheView(image_view);
695 histograms=DestroyHistogramThreadSet(histograms);
696 if (status == MagickFalse)
697 paint_image=DestroyImage(paint_image);
698 return(paint_image);
699}
700
701/*
702%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
703% %
704% %
705% %
706% O p a q u e P a i n t I m a g e %
707% %
708% %
709% %
710%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
711%
712% OpaquePaintImage() changes any pixel that matches color with the color
713% defined by fill.
714%
715% By default color must match a particular pixel color exactly. However,
716% in many cases two colors may differ by a small amount. Fuzz defines
717% how much tolerance is acceptable to consider two colors as the same.
718% For example, set fuzz to 10 and the color red at intensities of 100 and
719% 102 respectively are now interpreted as the same color.
720%
721% The format of the OpaquePaintImage method is:
722%
723% MagickBooleanType OpaquePaintImage(Image *image,
724% const PixelPacket *target,const PixelPacket *fill,
725% const MagickBooleanType invert)
cristy3ed852e2009-09-05 21:47:34 +0000726%
727% A description of each parameter follows:
728%
729% o image: the image.
730%
cristy3ed852e2009-09-05 21:47:34 +0000731% o target: the RGB value of the target color.
732%
733% o fill: the replacement color.
734%
735% o invert: paint any pixel that does not match the target color.
736%
737*/
cristy3ed852e2009-09-05 21:47:34 +0000738MagickExport MagickBooleanType OpaquePaintImage(Image *image,
cristyd42d9952011-07-08 14:21:50 +0000739 const PixelInfo *target,const PixelInfo *fill,const MagickBooleanType invert)
cristy3ed852e2009-09-05 21:47:34 +0000740{
741#define OpaquePaintImageTag "Opaque/Image"
742
cristyc4c8d132010-01-07 01:58:38 +0000743 CacheView
744 *image_view;
745
cristy3ed852e2009-09-05 21:47:34 +0000746 ExceptionInfo
747 *exception;
748
cristy3ed852e2009-09-05 21:47:34 +0000749 MagickBooleanType
750 status;
751
cristybb503372010-05-27 20:51:26 +0000752 MagickOffsetType
753 progress;
754
cristy4c08aed2011-07-01 19:47:50 +0000755 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000756 zero;
757
cristybb503372010-05-27 20:51:26 +0000758 ssize_t
759 y;
760
cristy3ed852e2009-09-05 21:47:34 +0000761 assert(image != (Image *) NULL);
762 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +0000763 assert(target != (PixelInfo *) NULL);
764 assert(fill != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000765 if (image->debug != MagickFalse)
766 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy48598922011-08-05 17:45:56 +0000767 exception=(&image->exception);
cristy574cc262011-08-05 01:23:58 +0000768 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000769 return(MagickFalse);
770 /*
771 Make image color opaque.
772 */
773 status=MagickTrue;
774 progress=0;
cristy4c08aed2011-07-01 19:47:50 +0000775 GetPixelInfo(image,&zero);
cristy3ed852e2009-09-05 21:47:34 +0000776 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +0000777#if defined(MAGICKCORE_OPENMP_SUPPORT)
778 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000779#endif
cristybb503372010-05-27 20:51:26 +0000780 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000781 {
cristy4c08aed2011-07-01 19:47:50 +0000782 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000783 pixel;
784
cristybb503372010-05-27 20:51:26 +0000785 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000786 x;
787
cristy4c08aed2011-07-01 19:47:50 +0000788 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000789 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000790
791 if (status == MagickFalse)
792 continue;
793 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000794 if (q == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000795 {
796 status=MagickFalse;
797 continue;
798 }
cristy3ed852e2009-09-05 21:47:34 +0000799 pixel=zero;
cristybb503372010-05-27 20:51:26 +0000800 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000801 {
cristy4c08aed2011-07-01 19:47:50 +0000802 SetPixelInfo(image,q,&pixel);
803 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
cristy3ed852e2009-09-05 21:47:34 +0000804 {
cristyed231572011-07-14 02:18:59 +0000805 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000806 SetPixelRed(image,ClampToQuantum(fill->red),q);
cristyed231572011-07-14 02:18:59 +0000807 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000808 SetPixelGreen(image,ClampToQuantum(fill->green),q);
cristyed231572011-07-14 02:18:59 +0000809 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000810 SetPixelBlue(image,ClampToQuantum(fill->blue),q);
cristyed231572011-07-14 02:18:59 +0000811 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +0000812 (image->colorspace == CMYKColorspace))
cristy4c08aed2011-07-01 19:47:50 +0000813 SetPixelBlack(image,ClampToQuantum(fill->black),q);
cristyed231572011-07-14 02:18:59 +0000814 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000815 SetPixelAlpha(image,ClampToQuantum(fill->alpha),q);
cristy3ed852e2009-09-05 21:47:34 +0000816 }
cristyed231572011-07-14 02:18:59 +0000817 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000818 }
819 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
820 status=MagickFalse;
821 if (image->progress_monitor != (MagickProgressMonitor) NULL)
822 {
823 MagickBooleanType
824 proceed;
825
cristyb5d5f722009-11-04 03:03:49 +0000826#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyd42d9952011-07-08 14:21:50 +0000827 #pragma omp critical (MagickCore_OpaquePaintImage)
cristy3ed852e2009-09-05 21:47:34 +0000828#endif
829 proceed=SetImageProgress(image,OpaquePaintImageTag,progress++,
830 image->rows);
831 if (proceed == MagickFalse)
832 status=MagickFalse;
833 }
834 }
835 image_view=DestroyCacheView(image_view);
836 return(status);
837}
838
839/*
840%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
841% %
842% %
843% %
844% T r a n s p a r e n t P a i n t I m a g e %
845% %
846% %
847% %
848%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
849%
850% TransparentPaintImage() changes the opacity value associated with any pixel
851% that matches color to the value defined by opacity.
852%
853% By default color must match a particular pixel color exactly. However,
854% in many cases two colors may differ by a small amount. Fuzz defines
855% how much tolerance is acceptable to consider two colors as the same.
856% For example, set fuzz to 10 and the color red at intensities of 100 and
857% 102 respectively are now interpreted as the same color.
858%
859% The format of the TransparentPaintImage method is:
860%
861% MagickBooleanType TransparentPaintImage(Image *image,
cristy4c08aed2011-07-01 19:47:50 +0000862% const PixelInfo *target,const Quantum opacity,
cristy3ed852e2009-09-05 21:47:34 +0000863% const MagickBooleanType invert)
864%
865% A description of each parameter follows:
866%
867% o image: the image.
868%
869% o target: the target color.
870%
871% o opacity: the replacement opacity value.
872%
873% o invert: paint any pixel that does not match the target color.
874%
875*/
876MagickExport MagickBooleanType TransparentPaintImage(Image *image,
cristy4c08aed2011-07-01 19:47:50 +0000877 const PixelInfo *target,const Quantum opacity,
cristy3ed852e2009-09-05 21:47:34 +0000878 const MagickBooleanType invert)
879{
880#define TransparentPaintImageTag "Transparent/Image"
881
cristyc4c8d132010-01-07 01:58:38 +0000882 CacheView
883 *image_view;
884
cristy3ed852e2009-09-05 21:47:34 +0000885 ExceptionInfo
886 *exception;
887
cristy3ed852e2009-09-05 21:47:34 +0000888 MagickBooleanType
889 status;
890
cristybb503372010-05-27 20:51:26 +0000891 MagickOffsetType
892 progress;
893
cristy4c08aed2011-07-01 19:47:50 +0000894 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000895 zero;
896
cristybb503372010-05-27 20:51:26 +0000897 ssize_t
898 y;
899
cristy3ed852e2009-09-05 21:47:34 +0000900 assert(image != (Image *) NULL);
901 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +0000902 assert(target != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000903 if (image->debug != MagickFalse)
904 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy48598922011-08-05 17:45:56 +0000905 exception=(&image->exception);
cristy574cc262011-08-05 01:23:58 +0000906 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000907 return(MagickFalse);
908 if (image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +0000909 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +0000910 /*
911 Make image color transparent.
912 */
913 status=MagickTrue;
914 progress=0;
cristy4c08aed2011-07-01 19:47:50 +0000915 GetPixelInfo(image,&zero);
cristy3ed852e2009-09-05 21:47:34 +0000916 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +0000917#if defined(MAGICKCORE_OPENMP_SUPPORT)
918 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000919#endif
cristybb503372010-05-27 20:51:26 +0000920 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000921 {
cristy4c08aed2011-07-01 19:47:50 +0000922 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000923 pixel;
924
cristybb503372010-05-27 20:51:26 +0000925 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000926 x;
927
cristy4c08aed2011-07-01 19:47:50 +0000928 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000929 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000930
931 if (status == MagickFalse)
932 continue;
933 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000934 if (q == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000935 {
936 status=MagickFalse;
937 continue;
938 }
cristy3ed852e2009-09-05 21:47:34 +0000939 pixel=zero;
cristybb503372010-05-27 20:51:26 +0000940 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000941 {
cristy4c08aed2011-07-01 19:47:50 +0000942 SetPixelInfo(image,q,&pixel);
943 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
944 SetPixelAlpha(image,opacity,q);
cristyed231572011-07-14 02:18:59 +0000945 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000946 }
947 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
948 status=MagickFalse;
949 if (image->progress_monitor != (MagickProgressMonitor) NULL)
950 {
951 MagickBooleanType
952 proceed;
953
cristyb5d5f722009-11-04 03:03:49 +0000954#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000955 #pragma omp critical (MagickCore_TransparentPaintImage)
956#endif
957 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
958 image->rows);
959 if (proceed == MagickFalse)
960 status=MagickFalse;
961 }
962 }
963 image_view=DestroyCacheView(image_view);
964 return(status);
965}
966
967/*
968%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
969% %
970% %
971% %
972% T r a n s p a r e n t P a i n t I m a g e C h r o m a %
973% %
974% %
975% %
976%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
977%
978% TransparentPaintImageChroma() changes the opacity value associated with any
979% pixel that matches color to the value defined by opacity.
980%
981% As there is one fuzz value for the all the channels, the
982% TransparentPaintImage() API is not suitable for the operations like chroma,
983% where the tolerance for similarity of two color component (RGB) can be
984% different, Thus we define this method take two target pixels (one
985% low and one hight) and all the pixels of an image which are lying between
986% these two pixels are made transparent.
987%
988% The format of the TransparentPaintImage method is:
989%
990% MagickBooleanType TransparentPaintImage(Image *image,
cristy4c08aed2011-07-01 19:47:50 +0000991% const PixelInfo *low,const PixelInfo *hight,
cristy3ed852e2009-09-05 21:47:34 +0000992% const Quantum opacity,const MagickBooleanType invert)
993%
994% A description of each parameter follows:
995%
996% o image: the image.
997%
998% o low: the low target color.
999%
1000% o high: the high target color.
1001%
1002% o opacity: the replacement opacity value.
1003%
1004% o invert: paint any pixel that does not match the target color.
1005%
1006*/
1007MagickExport MagickBooleanType TransparentPaintImageChroma(Image *image,
cristy4c08aed2011-07-01 19:47:50 +00001008 const PixelInfo *low,const PixelInfo *high,
cristy3ed852e2009-09-05 21:47:34 +00001009 const Quantum opacity,const MagickBooleanType invert)
1010{
1011#define TransparentPaintImageTag "Transparent/Image"
1012
cristyc4c8d132010-01-07 01:58:38 +00001013 CacheView
1014 *image_view;
1015
cristy3ed852e2009-09-05 21:47:34 +00001016 ExceptionInfo
1017 *exception;
1018
cristy3ed852e2009-09-05 21:47:34 +00001019 MagickBooleanType
1020 status;
1021
cristybb503372010-05-27 20:51:26 +00001022 MagickOffsetType
1023 progress;
1024
1025 ssize_t
1026 y;
1027
cristy3ed852e2009-09-05 21:47:34 +00001028 assert(image != (Image *) NULL);
1029 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +00001030 assert(high != (PixelInfo *) NULL);
1031 assert(low != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001032 if (image->debug != MagickFalse)
1033 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy48598922011-08-05 17:45:56 +00001034 exception=(&image->exception);
cristy574cc262011-08-05 01:23:58 +00001035 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001036 return(MagickFalse);
1037 if (image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00001038 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00001039 /*
1040 Make image color transparent.
1041 */
1042 status=MagickTrue;
1043 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00001044 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00001045#if defined(MAGICKCORE_OPENMP_SUPPORT)
1046 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00001047#endif
cristybb503372010-05-27 20:51:26 +00001048 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001049 {
1050 MagickBooleanType
1051 match;
1052
cristy4c08aed2011-07-01 19:47:50 +00001053 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001054 pixel;
1055
cristybb503372010-05-27 20:51:26 +00001056 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001057 x;
1058
cristy4c08aed2011-07-01 19:47:50 +00001059 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001060 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001061
1062 if (status == MagickFalse)
1063 continue;
1064 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001065 if (q == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001066 {
1067 status=MagickFalse;
1068 continue;
1069 }
cristy4c08aed2011-07-01 19:47:50 +00001070 GetPixelInfo(image,&pixel);
cristybb503372010-05-27 20:51:26 +00001071 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001072 {
cristy4c08aed2011-07-01 19:47:50 +00001073 SetPixelInfo(image,q,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001074 match=((pixel.red >= low->red) && (pixel.red <= high->red) &&
1075 (pixel.green >= low->green) && (pixel.green <= high->green) &&
1076 (pixel.blue >= low->blue) && (pixel.blue <= high->blue)) ?
1077 MagickTrue : MagickFalse;
1078 if (match != invert)
cristy4c08aed2011-07-01 19:47:50 +00001079 SetPixelAlpha(image,opacity,q);
cristyed231572011-07-14 02:18:59 +00001080 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001081 }
1082 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1083 status=MagickFalse;
1084 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1085 {
1086 MagickBooleanType
1087 proceed;
1088
cristyb5d5f722009-11-04 03:03:49 +00001089#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00001090 #pragma omp critical (MagickCore_TransparentPaintImageChroma)
1091#endif
1092 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
1093 image->rows);
1094 if (proceed == MagickFalse)
1095 status=MagickFalse;
1096 }
1097 }
1098 image_view=DestroyCacheView(image_view);
1099 return(status);
1100}