blob: 469d53472f86fb577a0868a4a6dfd4000efebf8b [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,
cristy189e84c2011-08-27 18:08:53 +000088% const MagickBooleanType invert,ExceptionInfo *exception)
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%
cristy189e84c2011-08-27 18:08:53 +0000102% o exception: return any errors or warnings in this structure.
103%
cristy3ed852e2009-09-05 21:47:34 +0000104*/
105MagickExport MagickBooleanType FloodfillPaintImage(Image *image,
cristyd42d9952011-07-08 14:21:50 +0000106 const DrawInfo *draw_info,const PixelInfo *target,const ssize_t x_offset,
cristy189e84c2011-08-27 18:08:53 +0000107 const ssize_t y_offset,const MagickBooleanType invert,
108 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000109{
110#define MaxStacksize (1UL << 15)
111#define PushSegmentStack(up,left,right,delta) \
112{ \
113 if (s >= (segment_stack+MaxStacksize)) \
114 ThrowBinaryException(DrawError,"SegmentStackOverflow",image->filename) \
115 else \
116 { \
cristybb503372010-05-27 20:51:26 +0000117 if ((((up)+(delta)) >= 0) && (((up)+(delta)) < (ssize_t) image->rows)) \
cristy3ed852e2009-09-05 21:47:34 +0000118 { \
119 s->x1=(double) (left); \
120 s->y1=(double) (up); \
121 s->x2=(double) (right); \
122 s->y2=(double) (delta); \
123 s++; \
124 } \
125 } \
126}
127
cristyb0d3bb92010-09-22 14:37:58 +0000128 CacheView
129 *floodplane_view,
130 *image_view;
131
cristy3ed852e2009-09-05 21:47:34 +0000132 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 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000173 return(MagickFalse);
174 if (image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +0000175 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +0000176 /*
177 Set floodfill state.
178 */
179 floodplane_image=CloneImage(image,0,0,MagickTrue,&image->exception);
180 if (floodplane_image == (Image *) NULL)
181 return(MagickFalse);
cristy63240882011-08-05 19:05:27 +0000182 (void) SetImageAlphaChannel(floodplane_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +0000183 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 */
cristy3ed852e2009-09-05 21:47:34 +0000194 x=x_offset;
195 y=y_offset;
196 start=0;
197 s=segment_stack;
198 PushSegmentStack(y,x,x,1);
199 PushSegmentStack(y+1,x,x,-1);
cristy4c08aed2011-07-01 19:47:50 +0000200 GetPixelInfo(image,&fill);
201 GetPixelInfo(image,&pixel);
cristyb0d3bb92010-09-22 14:37:58 +0000202 image_view=AcquireCacheView(image);
203 floodplane_view=AcquireCacheView(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000204 while (s > segment_stack)
205 {
cristy4c08aed2011-07-01 19:47:50 +0000206 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000207 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000208
cristybb503372010-05-27 20:51:26 +0000209 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000210 x;
211
cristy4c08aed2011-07-01 19:47:50 +0000212 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000213 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000214
215 /*
216 Pop segment off stack.
217 */
218 s--;
cristybb503372010-05-27 20:51:26 +0000219 x1=(ssize_t) s->x1;
220 x2=(ssize_t) s->x2;
221 offset=(ssize_t) s->y2;
222 y=(ssize_t) s->y1+offset;
cristy3ed852e2009-09-05 21:47:34 +0000223 /*
224 Recolor neighboring pixels.
225 */
cristyb0d3bb92010-09-22 14:37:58 +0000226 p=GetCacheViewVirtualPixels(image_view,0,y,(size_t) (x1+1),1,exception);
227 q=GetCacheViewAuthenticPixels(floodplane_view,0,y,(size_t) (x1+1),1,
cristy3ed852e2009-09-05 21:47:34 +0000228 exception);
cristy4c08aed2011-07-01 19:47:50 +0000229 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000230 break;
cristyed231572011-07-14 02:18:59 +0000231 p+=x1*GetPixelChannels(image);
232 q+=x1*GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000233 for (x=x1; x >= 0; x--)
234 {
cristy4c08aed2011-07-01 19:47:50 +0000235 if (GetPixelAlpha(image,q) == TransparentAlpha)
cristy3ed852e2009-09-05 21:47:34 +0000236 break;
cristy4c08aed2011-07-01 19:47:50 +0000237 SetPixelInfo(image,p,&pixel);
238 if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert)
cristy3ed852e2009-09-05 21:47:34 +0000239 break;
cristy4c08aed2011-07-01 19:47:50 +0000240 SetPixelAlpha(floodplane_image,TransparentAlpha,q);
cristyed231572011-07-14 02:18:59 +0000241 p-=GetPixelChannels(image);
242 q-=GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000243 }
cristyb0d3bb92010-09-22 14:37:58 +0000244 if (SyncCacheViewAuthenticPixels(floodplane_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000245 break;
246 skip=x >= x1 ? MagickTrue : MagickFalse;
247 if (skip == MagickFalse)
248 {
249 start=x+1;
250 if (start < x1)
251 PushSegmentStack(y,start,x1-1,-offset);
252 x=x1+1;
253 }
254 do
255 {
256 if (skip == MagickFalse)
257 {
cristybb503372010-05-27 20:51:26 +0000258 if (x < (ssize_t) image->columns)
cristy3ed852e2009-09-05 21:47:34 +0000259 {
cristyb0d3bb92010-09-22 14:37:58 +0000260 p=GetCacheViewVirtualPixels(image_view,x,y,image->columns-x,1,
cristy3ed852e2009-09-05 21:47:34 +0000261 exception);
cristyb0d3bb92010-09-22 14:37:58 +0000262 q=GetCacheViewAuthenticPixels(floodplane_view,x,y,
263 image->columns-x,1,exception);
cristy636dcb52011-08-26 13:23:49 +0000264 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000265 break;
cristybb503372010-05-27 20:51:26 +0000266 for ( ; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000267 {
cristy4c08aed2011-07-01 19:47:50 +0000268 if (GetPixelAlpha(image,q) == TransparentAlpha)
cristy3ed852e2009-09-05 21:47:34 +0000269 break;
cristy4c08aed2011-07-01 19:47:50 +0000270 SetPixelInfo(image,p,&pixel);
271 if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert)
cristy3ed852e2009-09-05 21:47:34 +0000272 break;
cristy4c08aed2011-07-01 19:47:50 +0000273 SetPixelAlpha(floodplane_image,
274 TransparentAlpha,q);
cristyed231572011-07-14 02:18:59 +0000275 p+=GetPixelChannels(image);
276 q+=GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000277 }
cristyb0d3bb92010-09-22 14:37:58 +0000278 if (SyncCacheViewAuthenticPixels(floodplane_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000279 break;
280 }
281 PushSegmentStack(y,start,x-1,offset);
282 if (x > (x2+1))
283 PushSegmentStack(y,x2+1,x-1,-offset);
284 }
285 skip=MagickFalse;
286 x++;
287 if (x <= x2)
288 {
cristyb0d3bb92010-09-22 14:37:58 +0000289 p=GetCacheViewVirtualPixels(image_view,x,y,(size_t) (x2-x+1),1,
290 exception);
291 q=GetCacheViewAuthenticPixels(floodplane_view,x,y,(size_t) (x2-x+1),1,
cristy3ed852e2009-09-05 21:47:34 +0000292 exception);
cristy636dcb52011-08-26 13:23:49 +0000293 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000294 break;
cristy3ed852e2009-09-05 21:47:34 +0000295 for ( ; x <= x2; x++)
296 {
cristy4c08aed2011-07-01 19:47:50 +0000297 if (GetPixelAlpha(image,q) == TransparentAlpha)
cristy3ed852e2009-09-05 21:47:34 +0000298 break;
cristy4c08aed2011-07-01 19:47:50 +0000299 SetPixelInfo(image,p,&pixel);
300 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
cristy3ed852e2009-09-05 21:47:34 +0000301 break;
cristyed231572011-07-14 02:18:59 +0000302 p+=GetPixelChannels(image);
303 q+=GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000304 }
305 }
306 start=x;
307 } while (x <= x2);
308 }
cristybb503372010-05-27 20:51:26 +0000309 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000310 {
cristy4c08aed2011-07-01 19:47:50 +0000311 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000312 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000313
cristybb503372010-05-27 20:51:26 +0000314 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000315 x;
316
cristy4c08aed2011-07-01 19:47:50 +0000317 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000318 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000319
320 /*
321 Tile fill color onto floodplane.
322 */
cristyb0d3bb92010-09-22 14:37:58 +0000323 p=GetCacheViewVirtualPixels(floodplane_view,0,y,image->columns,1,
324 exception);
325 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000326 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000327 break;
cristybb503372010-05-27 20:51:26 +0000328 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000329 {
cristy4c08aed2011-07-01 19:47:50 +0000330 if (GetPixelAlpha(floodplane_image,p) != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +0000331 {
332 (void) GetFillColor(draw_info,x,y,&fill_color);
cristy4c08aed2011-07-01 19:47:50 +0000333 SetPixelInfoPacket(image,&fill_color,&fill);
cristy3ed852e2009-09-05 21:47:34 +0000334 if (image->colorspace == CMYKColorspace)
335 ConvertRGBToCMYK(&fill);
cristyed231572011-07-14 02:18:59 +0000336 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000337 SetPixelRed(image,ClampToQuantum(fill.red),q);
cristyed231572011-07-14 02:18:59 +0000338 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000339 SetPixelGreen(image,ClampToQuantum(fill.green),q);
cristyed231572011-07-14 02:18:59 +0000340 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000341 SetPixelBlue(image,ClampToQuantum(fill.blue),q);
cristyed231572011-07-14 02:18:59 +0000342 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +0000343 (image->colorspace == CMYKColorspace))
cristy4c08aed2011-07-01 19:47:50 +0000344 SetPixelBlack(image,ClampToQuantum(fill.black),q);
cristyed231572011-07-14 02:18:59 +0000345 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000346 SetPixelAlpha(image,ClampToQuantum(fill.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +0000347 }
cristyed231572011-07-14 02:18:59 +0000348 p+=GetPixelChannels(floodplane_image);
349 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000350 }
cristyb0d3bb92010-09-22 14:37:58 +0000351 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000352 break;
353 }
cristyb0d3bb92010-09-22 14:37:58 +0000354 floodplane_view=DestroyCacheView(floodplane_view);
355 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +0000356 segment_stack=(SegmentInfo *) RelinquishMagickMemory(segment_stack);
357 floodplane_image=DestroyImage(floodplane_image);
cristybb503372010-05-27 20:51:26 +0000358 return(y == (ssize_t) image->rows ? MagickTrue : MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +0000359}
360
361/*
362%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
363% %
364% %
365% %
366+ G r a d i e n t I m a g e %
367% %
368% %
369% %
370%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
371%
cristycee97112010-05-28 00:44:52 +0000372% GradientImage() applies a continuously smooth color transitions along a
cristy3ed852e2009-09-05 21:47:34 +0000373% vector from one color to another.
374%
375% Note, the interface of this method will change in the future to support
376% more than one transistion.
377%
378% The format of the GradientImage method is:
379%
380% MagickBooleanType GradientImage(Image *image,const GradientType type,
381% const SpreadMethod method,const PixelPacket *start_color,
cristy189e84c2011-08-27 18:08:53 +0000382% const PixelPacket *stop_color,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000383%
384% A description of each parameter follows:
385%
386% o image: the image.
387%
388% o type: the gradient type: linear or radial.
389%
390% o spread: the gradient spread meathod: pad, reflect, or repeat.
391%
392% o start_color: the start color.
393%
394% o stop_color: the stop color.
395%
cristy189e84c2011-08-27 18:08:53 +0000396% o exception: return any errors or warnings in this structure.
397%
cristy3ed852e2009-09-05 21:47:34 +0000398*/
cristy117ff172010-08-15 21:35:32 +0000399
400static inline double MagickMax(const double x,const double y)
401{
402 return(x > y ? x : y);
403}
404
cristy3ed852e2009-09-05 21:47:34 +0000405MagickExport MagickBooleanType GradientImage(Image *image,
406 const GradientType type,const SpreadMethod method,
cristy189e84c2011-08-27 18:08:53 +0000407 const PixelPacket *start_color,const PixelPacket *stop_color,
408 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000409{
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,
cristy189e84c2011-08-27 18:08:53 +0000725% const MagickBooleanType invert,ExceptionInfo *exception)
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%
cristy189e84c2011-08-27 18:08:53 +0000737% o exception: return any errors or warnings in this structure.
738%
cristy3ed852e2009-09-05 21:47:34 +0000739*/
cristy3ed852e2009-09-05 21:47:34 +0000740MagickExport MagickBooleanType OpaquePaintImage(Image *image,
cristy189e84c2011-08-27 18:08:53 +0000741 const PixelInfo *target,const PixelInfo *fill,const MagickBooleanType invert,
742 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000743{
744#define OpaquePaintImageTag "Opaque/Image"
745
cristyc4c8d132010-01-07 01:58:38 +0000746 CacheView
747 *image_view;
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);
cristy574cc262011-08-05 01:23:58 +0000767 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000768 return(MagickFalse);
769 /*
770 Make image color opaque.
771 */
772 status=MagickTrue;
773 progress=0;
cristy4c08aed2011-07-01 19:47:50 +0000774 GetPixelInfo(image,&zero);
cristy3ed852e2009-09-05 21:47:34 +0000775 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +0000776#if defined(MAGICKCORE_OPENMP_SUPPORT)
777 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000778#endif
cristybb503372010-05-27 20:51:26 +0000779 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000780 {
cristy4c08aed2011-07-01 19:47:50 +0000781 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000782 pixel;
783
cristybb503372010-05-27 20:51:26 +0000784 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000785 x;
786
cristy4c08aed2011-07-01 19:47:50 +0000787 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000788 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000789
790 if (status == MagickFalse)
791 continue;
792 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000793 if (q == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000794 {
795 status=MagickFalse;
796 continue;
797 }
cristy3ed852e2009-09-05 21:47:34 +0000798 pixel=zero;
cristybb503372010-05-27 20:51:26 +0000799 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000800 {
cristy4c08aed2011-07-01 19:47:50 +0000801 SetPixelInfo(image,q,&pixel);
802 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
cristy3ed852e2009-09-05 21:47:34 +0000803 {
cristyed231572011-07-14 02:18:59 +0000804 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000805 SetPixelRed(image,ClampToQuantum(fill->red),q);
cristyed231572011-07-14 02:18:59 +0000806 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000807 SetPixelGreen(image,ClampToQuantum(fill->green),q);
cristyed231572011-07-14 02:18:59 +0000808 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000809 SetPixelBlue(image,ClampToQuantum(fill->blue),q);
cristyed231572011-07-14 02:18:59 +0000810 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +0000811 (image->colorspace == CMYKColorspace))
cristy4c08aed2011-07-01 19:47:50 +0000812 SetPixelBlack(image,ClampToQuantum(fill->black),q);
cristyed231572011-07-14 02:18:59 +0000813 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000814 SetPixelAlpha(image,ClampToQuantum(fill->alpha),q);
cristy3ed852e2009-09-05 21:47:34 +0000815 }
cristyed231572011-07-14 02:18:59 +0000816 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000817 }
818 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
819 status=MagickFalse;
820 if (image->progress_monitor != (MagickProgressMonitor) NULL)
821 {
822 MagickBooleanType
823 proceed;
824
cristyb5d5f722009-11-04 03:03:49 +0000825#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyd42d9952011-07-08 14:21:50 +0000826 #pragma omp critical (MagickCore_OpaquePaintImage)
cristy3ed852e2009-09-05 21:47:34 +0000827#endif
828 proceed=SetImageProgress(image,OpaquePaintImageTag,progress++,
829 image->rows);
830 if (proceed == MagickFalse)
831 status=MagickFalse;
832 }
833 }
834 image_view=DestroyCacheView(image_view);
835 return(status);
836}
837
838/*
839%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
840% %
841% %
842% %
843% T r a n s p a r e n t P a i n t I m a g e %
844% %
845% %
846% %
847%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
848%
849% TransparentPaintImage() changes the opacity value associated with any pixel
850% that matches color to the value defined by opacity.
851%
852% By default color must match a particular pixel color exactly. However,
853% in many cases two colors may differ by a small amount. Fuzz defines
854% how much tolerance is acceptable to consider two colors as the same.
855% For example, set fuzz to 10 and the color red at intensities of 100 and
856% 102 respectively are now interpreted as the same color.
857%
858% The format of the TransparentPaintImage method is:
859%
860% MagickBooleanType TransparentPaintImage(Image *image,
cristy4c08aed2011-07-01 19:47:50 +0000861% const PixelInfo *target,const Quantum opacity,
cristy189e84c2011-08-27 18:08:53 +0000862% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000863%
864% A description of each parameter follows:
865%
866% o image: the image.
867%
868% o target: the target color.
869%
870% o opacity: the replacement opacity value.
871%
872% o invert: paint any pixel that does not match the target color.
873%
cristy189e84c2011-08-27 18:08:53 +0000874% o exception: return any errors or warnings in this structure.
875%
cristy3ed852e2009-09-05 21:47:34 +0000876*/
877MagickExport MagickBooleanType TransparentPaintImage(Image *image,
cristy4c08aed2011-07-01 19:47:50 +0000878 const PixelInfo *target,const Quantum opacity,
cristy189e84c2011-08-27 18:08:53 +0000879 const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000880{
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 MagickBooleanType
887 status;
888
cristybb503372010-05-27 20:51:26 +0000889 MagickOffsetType
890 progress;
891
cristy4c08aed2011-07-01 19:47:50 +0000892 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000893 zero;
894
cristybb503372010-05-27 20:51:26 +0000895 ssize_t
896 y;
897
cristy3ed852e2009-09-05 21:47:34 +0000898 assert(image != (Image *) NULL);
899 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +0000900 assert(target != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000901 if (image->debug != MagickFalse)
902 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy574cc262011-08-05 01:23:58 +0000903 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000904 return(MagickFalse);
905 if (image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +0000906 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +0000907 /*
908 Make image color transparent.
909 */
910 status=MagickTrue;
911 progress=0;
cristy4c08aed2011-07-01 19:47:50 +0000912 GetPixelInfo(image,&zero);
cristy3ed852e2009-09-05 21:47:34 +0000913 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +0000914#if defined(MAGICKCORE_OPENMP_SUPPORT)
915 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000916#endif
cristybb503372010-05-27 20:51:26 +0000917 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000918 {
cristy4c08aed2011-07-01 19:47:50 +0000919 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000920 pixel;
921
cristybb503372010-05-27 20:51:26 +0000922 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000923 x;
924
cristy4c08aed2011-07-01 19:47:50 +0000925 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000926 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000927
928 if (status == MagickFalse)
929 continue;
930 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000931 if (q == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000932 {
933 status=MagickFalse;
934 continue;
935 }
cristy3ed852e2009-09-05 21:47:34 +0000936 pixel=zero;
cristybb503372010-05-27 20:51:26 +0000937 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000938 {
cristy4c08aed2011-07-01 19:47:50 +0000939 SetPixelInfo(image,q,&pixel);
940 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
941 SetPixelAlpha(image,opacity,q);
cristyed231572011-07-14 02:18:59 +0000942 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000943 }
944 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
945 status=MagickFalse;
946 if (image->progress_monitor != (MagickProgressMonitor) NULL)
947 {
948 MagickBooleanType
949 proceed;
950
cristyb5d5f722009-11-04 03:03:49 +0000951#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000952 #pragma omp critical (MagickCore_TransparentPaintImage)
953#endif
954 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
955 image->rows);
956 if (proceed == MagickFalse)
957 status=MagickFalse;
958 }
959 }
960 image_view=DestroyCacheView(image_view);
961 return(status);
962}
963
964/*
965%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
966% %
967% %
968% %
969% 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 %
970% %
971% %
972% %
973%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
974%
975% TransparentPaintImageChroma() changes the opacity value associated with any
976% pixel that matches color to the value defined by opacity.
977%
978% As there is one fuzz value for the all the channels, the
979% TransparentPaintImage() API is not suitable for the operations like chroma,
980% where the tolerance for similarity of two color component (RGB) can be
981% different, Thus we define this method take two target pixels (one
982% low and one hight) and all the pixels of an image which are lying between
983% these two pixels are made transparent.
984%
985% The format of the TransparentPaintImage method is:
986%
987% MagickBooleanType TransparentPaintImage(Image *image,
cristy4c08aed2011-07-01 19:47:50 +0000988% const PixelInfo *low,const PixelInfo *hight,
cristy189e84c2011-08-27 18:08:53 +0000989% const Quantum opacity,const MagickBooleanType invert,
990% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000991%
992% A description of each parameter follows:
993%
994% o image: the image.
995%
996% o low: the low target color.
997%
998% o high: the high target color.
999%
1000% o opacity: the replacement opacity value.
1001%
1002% o invert: paint any pixel that does not match the target color.
1003%
cristy189e84c2011-08-27 18:08:53 +00001004% o exception: return any errors or warnings in this structure.
1005%
cristy3ed852e2009-09-05 21:47:34 +00001006*/
1007MagickExport MagickBooleanType TransparentPaintImageChroma(Image *image,
cristy189e84c2011-08-27 18:08:53 +00001008 const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
1009 const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001010{
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 MagickBooleanType
1017 status;
1018
cristybb503372010-05-27 20:51:26 +00001019 MagickOffsetType
1020 progress;
1021
1022 ssize_t
1023 y;
1024
cristy3ed852e2009-09-05 21:47:34 +00001025 assert(image != (Image *) NULL);
1026 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +00001027 assert(high != (PixelInfo *) NULL);
1028 assert(low != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001029 if (image->debug != MagickFalse)
1030 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy574cc262011-08-05 01:23:58 +00001031 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001032 return(MagickFalse);
1033 if (image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00001034 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00001035 /*
1036 Make image color transparent.
1037 */
1038 status=MagickTrue;
1039 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00001040 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00001041#if defined(MAGICKCORE_OPENMP_SUPPORT)
1042 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00001043#endif
cristybb503372010-05-27 20:51:26 +00001044 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001045 {
1046 MagickBooleanType
1047 match;
1048
cristy4c08aed2011-07-01 19:47:50 +00001049 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001050 pixel;
1051
cristybb503372010-05-27 20:51:26 +00001052 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001053 x;
1054
cristy4c08aed2011-07-01 19:47:50 +00001055 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001056 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001057
1058 if (status == MagickFalse)
1059 continue;
1060 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001061 if (q == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001062 {
1063 status=MagickFalse;
1064 continue;
1065 }
cristy4c08aed2011-07-01 19:47:50 +00001066 GetPixelInfo(image,&pixel);
cristybb503372010-05-27 20:51:26 +00001067 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001068 {
cristy4c08aed2011-07-01 19:47:50 +00001069 SetPixelInfo(image,q,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001070 match=((pixel.red >= low->red) && (pixel.red <= high->red) &&
1071 (pixel.green >= low->green) && (pixel.green <= high->green) &&
1072 (pixel.blue >= low->blue) && (pixel.blue <= high->blue)) ?
1073 MagickTrue : MagickFalse;
1074 if (match != invert)
cristy4c08aed2011-07-01 19:47:50 +00001075 SetPixelAlpha(image,opacity,q);
cristyed231572011-07-14 02:18:59 +00001076 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001077 }
1078 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1079 status=MagickFalse;
1080 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1081 {
1082 MagickBooleanType
1083 proceed;
1084
cristyb5d5f722009-11-04 03:03:49 +00001085#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00001086 #pragma omp critical (MagickCore_TransparentPaintImageChroma)
1087#endif
1088 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
1089 image->rows);
1090 if (proceed == MagickFalse)
1091 status=MagickFalse;
1092 }
1093 }
1094 image_view=DestroyCacheView(image_view);
1095 return(status);
1096}