blob: 44663614c98a764d4c3f239b6e2d6afc6fb6ad1f [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);
172 if (SetImageStorageClass(image,DirectClass) == MagickFalse)
173 return(MagickFalse);
174 if (image->matte == MagickFalse)
175 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel);
176 /*
177 Set floodfill state.
178 */
179 floodplane_image=CloneImage(image,0,0,MagickTrue,&image->exception);
180 if (floodplane_image == (Image *) NULL)
181 return(MagickFalse);
182 (void) SetImageAlphaChannel(floodplane_image,OpaqueAlphaChannel);
183 segment_stack=(SegmentInfo *) AcquireQuantumMemory(MaxStacksize,
184 sizeof(*segment_stack));
185 if (segment_stack == (SegmentInfo *) NULL)
186 {
187 floodplane_image=DestroyImage(floodplane_image);
188 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
189 image->filename);
190 }
191 /*
192 Push initial segment on stack.
193 */
194 exception=(&image->exception);
195 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);
581 if (SetImageStorageClass(paint_image,DirectClass) == MagickFalse)
582 {
583 InheritException(exception,&paint_image->exception);
584 paint_image=DestroyImage(paint_image);
585 return((Image *) NULL);
586 }
587 histograms=AcquireHistogramThreadSet(NumberPaintBins);
cristybb503372010-05-27 20:51:26 +0000588 if (histograms == (size_t **) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000589 {
590 paint_image=DestroyImage(paint_image);
591 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
592 }
593 /*
594 Oil paint image.
595 */
596 status=MagickTrue;
597 progress=0;
598 image_view=AcquireCacheView(image);
599 paint_view=AcquireCacheView(paint_image);
cristyb5d5f722009-11-04 03:03:49 +0000600#if defined(MAGICKCORE_OPENMP_SUPPORT)
601 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000602#endif
cristybb503372010-05-27 20:51:26 +0000603 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000604 {
cristy4c08aed2011-07-01 19:47:50 +0000605 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000606 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000607
cristybb503372010-05-27 20:51:26 +0000608 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000609 x;
610
cristy4c08aed2011-07-01 19:47:50 +0000611 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000612 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000613
cristybb503372010-05-27 20:51:26 +0000614 register size_t
cristy3ed852e2009-09-05 21:47:34 +0000615 *histogram;
616
617 if (status == MagickFalse)
618 continue;
cristyfe4ba002011-02-28 14:54:12 +0000619 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
620 (width/2L),image->columns+width,width,exception);
cristy3ed852e2009-09-05 21:47:34 +0000621 q=QueueCacheViewAuthenticPixels(paint_view,0,y,paint_image->columns,1,
622 exception);
cristy4c08aed2011-07-01 19:47:50 +0000623 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000624 {
625 status=MagickFalse;
626 continue;
627 }
cristy3ed852e2009-09-05 21:47:34 +0000628 histogram=histograms[GetOpenMPThreadId()];
cristybb503372010-05-27 20:51:26 +0000629 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000630 {
cristybb503372010-05-27 20:51:26 +0000631 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000632 i,
633 u;
634
cristybb503372010-05-27 20:51:26 +0000635 size_t
cristy3ed852e2009-09-05 21:47:34 +0000636 count;
637
cristy9d314ff2011-03-09 01:30:28 +0000638 ssize_t
639 j,
640 k,
641 v;
642
cristy3ed852e2009-09-05 21:47:34 +0000643 /*
644 Assign most frequent color.
645 */
646 i=0;
647 j=0;
648 count=0;
649 (void) ResetMagickMemory(histogram,0,NumberPaintBins*sizeof(*histogram));
cristybb503372010-05-27 20:51:26 +0000650 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +0000651 {
cristybb503372010-05-27 20:51:26 +0000652 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +0000653 {
cristy4c08aed2011-07-01 19:47:50 +0000654 k=(ssize_t) ScaleQuantumToChar(GetPixelIntensity(image,p+u+i));
cristy3ed852e2009-09-05 21:47:34 +0000655 histogram[k]++;
656 if (histogram[k] > count)
657 {
658 j=i+u;
659 count=histogram[k];
660 }
661 }
cristyd99b0962010-05-29 23:14:26 +0000662 i+=(ssize_t) (image->columns+width);
cristy3ed852e2009-09-05 21:47:34 +0000663 }
cristy4c08aed2011-07-01 19:47:50 +0000664 SetPixelRed(paint_image,GetPixelRed(image,p+j*
cristyed231572011-07-14 02:18:59 +0000665 GetPixelChannels(image)),q);
cristy4c08aed2011-07-01 19:47:50 +0000666 SetPixelGreen(paint_image,GetPixelGreen(image,p+j*
cristyed231572011-07-14 02:18:59 +0000667 GetPixelChannels(image)),q);
cristy4c08aed2011-07-01 19:47:50 +0000668 SetPixelBlue(paint_image,GetPixelBlue(image,p+j*
cristyed231572011-07-14 02:18:59 +0000669 GetPixelChannels(image)),q);
cristy3ed852e2009-09-05 21:47:34 +0000670 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +0000671 SetPixelBlack(paint_image,GetPixelBlack(image,p+j*
cristyed231572011-07-14 02:18:59 +0000672 GetPixelChannels(image)),q);
cristy4c08aed2011-07-01 19:47:50 +0000673 if (image->matte != MagickFalse)
674 SetPixelAlpha(paint_image,GetPixelAlpha(image,p+j*
cristyed231572011-07-14 02:18:59 +0000675 GetPixelChannels(image)),q);
676 p+=GetPixelChannels(image);
677 q+=GetPixelChannels(paint_image);
cristy3ed852e2009-09-05 21:47:34 +0000678 }
679 if (SyncCacheViewAuthenticPixels(paint_view,exception) == MagickFalse)
680 status=MagickFalse;
681 if (image->progress_monitor != (MagickProgressMonitor) NULL)
682 {
683 MagickBooleanType
684 proceed;
685
cristyb5d5f722009-11-04 03:03:49 +0000686#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000687 #pragma omp critical (MagickCore_OilPaintImage)
688#endif
689 proceed=SetImageProgress(image,OilPaintImageTag,progress++,image->rows);
690 if (proceed == MagickFalse)
691 status=MagickFalse;
692 }
693 }
694 paint_view=DestroyCacheView(paint_view);
695 image_view=DestroyCacheView(image_view);
696 histograms=DestroyHistogramThreadSet(histograms);
697 if (status == MagickFalse)
698 paint_image=DestroyImage(paint_image);
699 return(paint_image);
700}
701
702/*
703%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
704% %
705% %
706% %
707% O p a q u e P a i n t I m a g e %
708% %
709% %
710% %
711%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
712%
713% OpaquePaintImage() changes any pixel that matches color with the color
714% defined by fill.
715%
716% By default color must match a particular pixel color exactly. However,
717% in many cases two colors may differ by a small amount. Fuzz defines
718% how much tolerance is acceptable to consider two colors as the same.
719% For example, set fuzz to 10 and the color red at intensities of 100 and
720% 102 respectively are now interpreted as the same color.
721%
722% The format of the OpaquePaintImage method is:
723%
724% MagickBooleanType OpaquePaintImage(Image *image,
725% const PixelPacket *target,const PixelPacket *fill,
726% const MagickBooleanType invert)
cristy3ed852e2009-09-05 21:47:34 +0000727%
728% A description of each parameter follows:
729%
730% o image: the image.
731%
cristy3ed852e2009-09-05 21:47:34 +0000732% o target: the RGB value of the target color.
733%
734% o fill: the replacement color.
735%
736% o invert: paint any pixel that does not match the target color.
737%
738*/
cristy3ed852e2009-09-05 21:47:34 +0000739MagickExport MagickBooleanType OpaquePaintImage(Image *image,
cristyd42d9952011-07-08 14:21:50 +0000740 const PixelInfo *target,const PixelInfo *fill,const MagickBooleanType invert)
cristy3ed852e2009-09-05 21:47:34 +0000741{
742#define OpaquePaintImageTag "Opaque/Image"
743
cristyc4c8d132010-01-07 01:58:38 +0000744 CacheView
745 *image_view;
746
cristy3ed852e2009-09-05 21:47:34 +0000747 ExceptionInfo
748 *exception;
749
cristy3ed852e2009-09-05 21:47:34 +0000750 MagickBooleanType
751 status;
752
cristybb503372010-05-27 20:51:26 +0000753 MagickOffsetType
754 progress;
755
cristy4c08aed2011-07-01 19:47:50 +0000756 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000757 zero;
758
cristybb503372010-05-27 20:51:26 +0000759 ssize_t
760 y;
761
cristy3ed852e2009-09-05 21:47:34 +0000762 assert(image != (Image *) NULL);
763 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +0000764 assert(target != (PixelInfo *) NULL);
765 assert(fill != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000766 if (image->debug != MagickFalse)
767 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
768 if (SetImageStorageClass(image,DirectClass) == MagickFalse)
769 return(MagickFalse);
770 /*
771 Make image color opaque.
772 */
773 status=MagickTrue;
774 progress=0;
775 exception=(&image->exception);
cristy4c08aed2011-07-01 19:47:50 +0000776 GetPixelInfo(image,&zero);
cristy3ed852e2009-09-05 21:47:34 +0000777 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +0000778#if defined(MAGICKCORE_OPENMP_SUPPORT)
779 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000780#endif
cristybb503372010-05-27 20:51:26 +0000781 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000782 {
cristy4c08aed2011-07-01 19:47:50 +0000783 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000784 pixel;
785
cristybb503372010-05-27 20:51:26 +0000786 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000787 x;
788
cristy4c08aed2011-07-01 19:47:50 +0000789 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000790 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000791
792 if (status == MagickFalse)
793 continue;
794 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000795 if (q == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000796 {
797 status=MagickFalse;
798 continue;
799 }
cristy3ed852e2009-09-05 21:47:34 +0000800 pixel=zero;
cristybb503372010-05-27 20:51:26 +0000801 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000802 {
cristy4c08aed2011-07-01 19:47:50 +0000803 SetPixelInfo(image,q,&pixel);
804 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
cristy3ed852e2009-09-05 21:47:34 +0000805 {
cristyed231572011-07-14 02:18:59 +0000806 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000807 SetPixelRed(image,ClampToQuantum(fill->red),q);
cristyed231572011-07-14 02:18:59 +0000808 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000809 SetPixelGreen(image,ClampToQuantum(fill->green),q);
cristyed231572011-07-14 02:18:59 +0000810 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000811 SetPixelBlue(image,ClampToQuantum(fill->blue),q);
cristyed231572011-07-14 02:18:59 +0000812 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +0000813 (image->colorspace == CMYKColorspace))
cristy4c08aed2011-07-01 19:47:50 +0000814 SetPixelBlack(image,ClampToQuantum(fill->black),q);
cristyed231572011-07-14 02:18:59 +0000815 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000816 SetPixelAlpha(image,ClampToQuantum(fill->alpha),q);
cristy3ed852e2009-09-05 21:47:34 +0000817 }
cristyed231572011-07-14 02:18:59 +0000818 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000819 }
820 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
821 status=MagickFalse;
822 if (image->progress_monitor != (MagickProgressMonitor) NULL)
823 {
824 MagickBooleanType
825 proceed;
826
cristyb5d5f722009-11-04 03:03:49 +0000827#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyd42d9952011-07-08 14:21:50 +0000828 #pragma omp critical (MagickCore_OpaquePaintImage)
cristy3ed852e2009-09-05 21:47:34 +0000829#endif
830 proceed=SetImageProgress(image,OpaquePaintImageTag,progress++,
831 image->rows);
832 if (proceed == MagickFalse)
833 status=MagickFalse;
834 }
835 }
836 image_view=DestroyCacheView(image_view);
837 return(status);
838}
839
840/*
841%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
842% %
843% %
844% %
845% T r a n s p a r e n t P a i n t I m a g e %
846% %
847% %
848% %
849%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
850%
851% TransparentPaintImage() changes the opacity value associated with any pixel
852% that matches color to the value defined by opacity.
853%
854% By default color must match a particular pixel color exactly. However,
855% in many cases two colors may differ by a small amount. Fuzz defines
856% how much tolerance is acceptable to consider two colors as the same.
857% For example, set fuzz to 10 and the color red at intensities of 100 and
858% 102 respectively are now interpreted as the same color.
859%
860% The format of the TransparentPaintImage method is:
861%
862% MagickBooleanType TransparentPaintImage(Image *image,
cristy4c08aed2011-07-01 19:47:50 +0000863% const PixelInfo *target,const Quantum opacity,
cristy3ed852e2009-09-05 21:47:34 +0000864% const MagickBooleanType invert)
865%
866% A description of each parameter follows:
867%
868% o image: the image.
869%
870% o target: the target color.
871%
872% o opacity: the replacement opacity value.
873%
874% o invert: paint any pixel that does not match the target color.
875%
876*/
877MagickExport MagickBooleanType TransparentPaintImage(Image *image,
cristy4c08aed2011-07-01 19:47:50 +0000878 const PixelInfo *target,const Quantum opacity,
cristy3ed852e2009-09-05 21:47:34 +0000879 const MagickBooleanType invert)
880{
881#define TransparentPaintImageTag "Transparent/Image"
882
cristyc4c8d132010-01-07 01:58:38 +0000883 CacheView
884 *image_view;
885
cristy3ed852e2009-09-05 21:47:34 +0000886 ExceptionInfo
887 *exception;
888
cristy3ed852e2009-09-05 21:47:34 +0000889 MagickBooleanType
890 status;
891
cristybb503372010-05-27 20:51:26 +0000892 MagickOffsetType
893 progress;
894
cristy4c08aed2011-07-01 19:47:50 +0000895 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000896 zero;
897
cristybb503372010-05-27 20:51:26 +0000898 ssize_t
899 y;
900
cristy3ed852e2009-09-05 21:47:34 +0000901 assert(image != (Image *) NULL);
902 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +0000903 assert(target != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000904 if (image->debug != MagickFalse)
905 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
906 if (SetImageStorageClass(image,DirectClass) == MagickFalse)
907 return(MagickFalse);
908 if (image->matte == MagickFalse)
909 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel);
910 /*
911 Make image color transparent.
912 */
913 status=MagickTrue;
914 progress=0;
915 exception=(&image->exception);
cristy4c08aed2011-07-01 19:47:50 +0000916 GetPixelInfo(image,&zero);
cristy3ed852e2009-09-05 21:47:34 +0000917 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +0000918#if defined(MAGICKCORE_OPENMP_SUPPORT)
919 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000920#endif
cristybb503372010-05-27 20:51:26 +0000921 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000922 {
cristy4c08aed2011-07-01 19:47:50 +0000923 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000924 pixel;
925
cristybb503372010-05-27 20:51:26 +0000926 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000927 x;
928
cristy4c08aed2011-07-01 19:47:50 +0000929 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000930 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000931
932 if (status == MagickFalse)
933 continue;
934 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000935 if (q == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000936 {
937 status=MagickFalse;
938 continue;
939 }
cristy3ed852e2009-09-05 21:47:34 +0000940 pixel=zero;
cristybb503372010-05-27 20:51:26 +0000941 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000942 {
cristy4c08aed2011-07-01 19:47:50 +0000943 SetPixelInfo(image,q,&pixel);
944 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
945 SetPixelAlpha(image,opacity,q);
cristyed231572011-07-14 02:18:59 +0000946 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000947 }
948 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
949 status=MagickFalse;
950 if (image->progress_monitor != (MagickProgressMonitor) NULL)
951 {
952 MagickBooleanType
953 proceed;
954
cristyb5d5f722009-11-04 03:03:49 +0000955#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000956 #pragma omp critical (MagickCore_TransparentPaintImage)
957#endif
958 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
959 image->rows);
960 if (proceed == MagickFalse)
961 status=MagickFalse;
962 }
963 }
964 image_view=DestroyCacheView(image_view);
965 return(status);
966}
967
968/*
969%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
970% %
971% %
972% %
973% 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 %
974% %
975% %
976% %
977%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
978%
979% TransparentPaintImageChroma() changes the opacity value associated with any
980% pixel that matches color to the value defined by opacity.
981%
982% As there is one fuzz value for the all the channels, the
983% TransparentPaintImage() API is not suitable for the operations like chroma,
984% where the tolerance for similarity of two color component (RGB) can be
985% different, Thus we define this method take two target pixels (one
986% low and one hight) and all the pixels of an image which are lying between
987% these two pixels are made transparent.
988%
989% The format of the TransparentPaintImage method is:
990%
991% MagickBooleanType TransparentPaintImage(Image *image,
cristy4c08aed2011-07-01 19:47:50 +0000992% const PixelInfo *low,const PixelInfo *hight,
cristy3ed852e2009-09-05 21:47:34 +0000993% const Quantum opacity,const MagickBooleanType invert)
994%
995% A description of each parameter follows:
996%
997% o image: the image.
998%
999% o low: the low target color.
1000%
1001% o high: the high target color.
1002%
1003% o opacity: the replacement opacity value.
1004%
1005% o invert: paint any pixel that does not match the target color.
1006%
1007*/
1008MagickExport MagickBooleanType TransparentPaintImageChroma(Image *image,
cristy4c08aed2011-07-01 19:47:50 +00001009 const PixelInfo *low,const PixelInfo *high,
cristy3ed852e2009-09-05 21:47:34 +00001010 const Quantum opacity,const MagickBooleanType invert)
1011{
1012#define TransparentPaintImageTag "Transparent/Image"
1013
cristyc4c8d132010-01-07 01:58:38 +00001014 CacheView
1015 *image_view;
1016
cristy3ed852e2009-09-05 21:47:34 +00001017 ExceptionInfo
1018 *exception;
1019
cristy3ed852e2009-09-05 21:47:34 +00001020 MagickBooleanType
1021 status;
1022
cristybb503372010-05-27 20:51:26 +00001023 MagickOffsetType
1024 progress;
1025
1026 ssize_t
1027 y;
1028
cristy3ed852e2009-09-05 21:47:34 +00001029 assert(image != (Image *) NULL);
1030 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +00001031 assert(high != (PixelInfo *) NULL);
1032 assert(low != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001033 if (image->debug != MagickFalse)
1034 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1035 if (SetImageStorageClass(image,DirectClass) == MagickFalse)
1036 return(MagickFalse);
1037 if (image->matte == MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001038 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel);
cristy3ed852e2009-09-05 21:47:34 +00001039 /*
1040 Make image color transparent.
1041 */
1042 status=MagickTrue;
1043 progress=0;
1044 exception=(&image->exception);
1045 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00001046#if defined(MAGICKCORE_OPENMP_SUPPORT)
1047 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00001048#endif
cristybb503372010-05-27 20:51:26 +00001049 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001050 {
1051 MagickBooleanType
1052 match;
1053
cristy4c08aed2011-07-01 19:47:50 +00001054 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001055 pixel;
1056
cristybb503372010-05-27 20:51:26 +00001057 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001058 x;
1059
cristy4c08aed2011-07-01 19:47:50 +00001060 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001061 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001062
1063 if (status == MagickFalse)
1064 continue;
1065 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001066 if (q == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001067 {
1068 status=MagickFalse;
1069 continue;
1070 }
cristy4c08aed2011-07-01 19:47:50 +00001071 GetPixelInfo(image,&pixel);
cristybb503372010-05-27 20:51:26 +00001072 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001073 {
cristy4c08aed2011-07-01 19:47:50 +00001074 SetPixelInfo(image,q,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001075 match=((pixel.red >= low->red) && (pixel.red <= high->red) &&
1076 (pixel.green >= low->green) && (pixel.green <= high->green) &&
1077 (pixel.blue >= low->blue) && (pixel.blue <= high->blue)) ?
1078 MagickTrue : MagickFalse;
1079 if (match != invert)
cristy4c08aed2011-07-01 19:47:50 +00001080 SetPixelAlpha(image,opacity,q);
cristyed231572011-07-14 02:18:59 +00001081 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001082 }
1083 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1084 status=MagickFalse;
1085 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1086 {
1087 MagickBooleanType
1088 proceed;
1089
cristyb5d5f722009-11-04 03:03:49 +00001090#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00001091 #pragma omp critical (MagickCore_TransparentPaintImageChroma)
1092#endif
1093 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
1094 image->rows);
1095 if (proceed == MagickFalse)
1096 status=MagickFalse;
1097 }
1098 }
1099 image_view=DestroyCacheView(image_view);
1100 return(status);
1101}