blob: 771e9a8990dd9ffcc5e0eeae637e1b1c75dca330 [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"
cristy8ea81222011-09-04 10:33:32 +000053#include "MagickCore/gem-private.h"
cristy4c08aed2011-07-01 19:47:50 +000054#include "MagickCore/monitor.h"
55#include "MagickCore/monitor-private.h"
56#include "MagickCore/paint.h"
57#include "MagickCore/pixel-accessor.h"
58#include "MagickCore/string_.h"
59#include "MagickCore/thread-private.h"
cristy3ed852e2009-09-05 21:47:34 +000060
cristy3ed852e2009-09-05 21:47:34 +000061/*
62%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
63% %
64% %
65% %
66% F l o o d f i l l P a i n t I m a g e %
67% %
68% %
69% %
70%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
71%
72% FloodfillPaintImage() changes the color value of any pixel that matches
73% target and is an immediate neighbor. If the method FillToBorderMethod is
74% specified, the color value is changed for any neighbor pixel that does not
75% match the bordercolor member of image.
76%
cristy908a0002011-08-28 00:13:39 +000077% By default target must match a particular pixel color exactly. However,
78% in many cases two colors may differ by a small amount. The fuzz member of
79% image defines how much tolerance is acceptable to consider two colors as
80% the same. For example, set fuzz to 10 and the color red at intensities of
81% 100 and 102 respectively are now interpreted as the same color for the
82% purposes of the floodfill.
cristy3ed852e2009-09-05 21:47:34 +000083%
84% The format of the FloodfillPaintImage method is:
85%
86% MagickBooleanType FloodfillPaintImage(Image *image,
cristyd42d9952011-07-08 14:21:50 +000087% const DrawInfo *draw_info,const PixelInfo target,
88% const ssize_t x_offset,const ssize_t y_offset,
cristy189e84c2011-08-27 18:08:53 +000089% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +000090%
91% A description of each parameter follows:
92%
93% o image: the image.
94%
cristy3ed852e2009-09-05 21:47:34 +000095% o draw_info: the draw info.
96%
97% o target: the RGB value of the target color.
98%
99% o x_offset,y_offset: the starting location of the operation.
100%
101% o invert: paint any pixel that does not match the target color.
102%
cristy189e84c2011-08-27 18:08:53 +0000103% o exception: return any errors or warnings in this structure.
104%
cristy3ed852e2009-09-05 21:47:34 +0000105*/
106MagickExport MagickBooleanType FloodfillPaintImage(Image *image,
cristyd42d9952011-07-08 14:21:50 +0000107 const DrawInfo *draw_info,const PixelInfo *target,const ssize_t x_offset,
cristy189e84c2011-08-27 18:08:53 +0000108 const ssize_t y_offset,const MagickBooleanType invert,
109 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000110{
111#define MaxStacksize (1UL << 15)
112#define PushSegmentStack(up,left,right,delta) \
113{ \
114 if (s >= (segment_stack+MaxStacksize)) \
115 ThrowBinaryException(DrawError,"SegmentStackOverflow",image->filename) \
116 else \
117 { \
cristybb503372010-05-27 20:51:26 +0000118 if ((((up)+(delta)) >= 0) && (((up)+(delta)) < (ssize_t) image->rows)) \
cristy3ed852e2009-09-05 21:47:34 +0000119 { \
120 s->x1=(double) (left); \
121 s->y1=(double) (up); \
122 s->x2=(double) (right); \
123 s->y2=(double) (delta); \
124 s++; \
125 } \
126 } \
127}
128
cristyb0d3bb92010-09-22 14:37:58 +0000129 CacheView
130 *floodplane_view,
131 *image_view;
132
cristy3ed852e2009-09-05 21:47:34 +0000133 Image
134 *floodplane_image;
135
cristy3ed852e2009-09-05 21:47:34 +0000136 MagickBooleanType
cristy14973ba2011-08-27 23:48:07 +0000137 skip,
138 status;
cristy3ed852e2009-09-05 21:47:34 +0000139
cristy4c08aed2011-07-01 19:47:50 +0000140 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000141 fill,
142 pixel;
143
cristy101ab702011-10-13 13:06:32 +0000144 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000145 fill_color;
146
147 register SegmentInfo
148 *s;
149
150 SegmentInfo
151 *segment_stack;
152
cristy9d314ff2011-03-09 01:30:28 +0000153 ssize_t
154 offset,
155 start,
156 x,
157 x1,
158 x2,
159 y;
160
cristy3ed852e2009-09-05 21:47:34 +0000161 /*
162 Check boundary conditions.
163 */
164 assert(image != (Image *) NULL);
165 assert(image->signature == MagickSignature);
166 if (image->debug != MagickFalse)
167 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
168 assert(draw_info != (DrawInfo *) NULL);
169 assert(draw_info->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +0000170 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
cristy3ed852e2009-09-05 21:47:34 +0000171 return(MagickFalse);
cristybb503372010-05-27 20:51:26 +0000172 if ((y_offset < 0) || (y_offset >= (ssize_t) image->rows))
cristy3ed852e2009-09-05 21:47:34 +0000173 return(MagickFalse);
cristy574cc262011-08-05 01:23:58 +0000174 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000175 return(MagickFalse);
176 if (image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +0000177 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +0000178 /*
179 Set floodfill state.
180 */
181 floodplane_image=CloneImage(image,0,0,MagickTrue,&image->exception);
182 if (floodplane_image == (Image *) NULL)
183 return(MagickFalse);
cristy63240882011-08-05 19:05:27 +0000184 (void) SetImageAlphaChannel(floodplane_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +0000185 segment_stack=(SegmentInfo *) AcquireQuantumMemory(MaxStacksize,
186 sizeof(*segment_stack));
187 if (segment_stack == (SegmentInfo *) NULL)
188 {
189 floodplane_image=DestroyImage(floodplane_image);
190 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
191 image->filename);
192 }
193 /*
194 Push initial segment on stack.
195 */
cristy14973ba2011-08-27 23:48:07 +0000196 status=MagickTrue;
cristy2ed42f62011-10-02 19:49:57 +0000197 fill_color.black=0.0;
198 fill_color.index=0.0;
cristy3ed852e2009-09-05 21:47:34 +0000199 x=x_offset;
200 y=y_offset;
201 start=0;
202 s=segment_stack;
203 PushSegmentStack(y,x,x,1);
204 PushSegmentStack(y+1,x,x,-1);
cristy4c08aed2011-07-01 19:47:50 +0000205 GetPixelInfo(image,&fill);
206 GetPixelInfo(image,&pixel);
cristyb0d3bb92010-09-22 14:37:58 +0000207 image_view=AcquireCacheView(image);
208 floodplane_view=AcquireCacheView(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000209 while (s > segment_stack)
210 {
cristy4c08aed2011-07-01 19:47:50 +0000211 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000212 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000213
cristy4c08aed2011-07-01 19:47:50 +0000214 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000215 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000216
cristy14973ba2011-08-27 23:48:07 +0000217 register ssize_t
218 x;
219
cristy3ed852e2009-09-05 21:47:34 +0000220 /*
221 Pop segment off stack.
222 */
223 s--;
cristybb503372010-05-27 20:51:26 +0000224 x1=(ssize_t) s->x1;
225 x2=(ssize_t) s->x2;
226 offset=(ssize_t) s->y2;
227 y=(ssize_t) s->y1+offset;
cristy3ed852e2009-09-05 21:47:34 +0000228 /*
229 Recolor neighboring pixels.
230 */
cristyb0d3bb92010-09-22 14:37:58 +0000231 p=GetCacheViewVirtualPixels(image_view,0,y,(size_t) (x1+1),1,exception);
232 q=GetCacheViewAuthenticPixels(floodplane_view,0,y,(size_t) (x1+1),1,
cristy3ed852e2009-09-05 21:47:34 +0000233 exception);
cristy4c08aed2011-07-01 19:47:50 +0000234 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000235 break;
cristyed231572011-07-14 02:18:59 +0000236 p+=x1*GetPixelChannels(image);
237 q+=x1*GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000238 for (x=x1; x >= 0; x--)
239 {
cristy4c08aed2011-07-01 19:47:50 +0000240 if (GetPixelAlpha(image,q) == TransparentAlpha)
cristy3ed852e2009-09-05 21:47:34 +0000241 break;
cristy4c08aed2011-07-01 19:47:50 +0000242 SetPixelInfo(image,p,&pixel);
243 if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert)
cristy3ed852e2009-09-05 21:47:34 +0000244 break;
cristy4c08aed2011-07-01 19:47:50 +0000245 SetPixelAlpha(floodplane_image,TransparentAlpha,q);
cristyed231572011-07-14 02:18:59 +0000246 p-=GetPixelChannels(image);
247 q-=GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000248 }
cristyb0d3bb92010-09-22 14:37:58 +0000249 if (SyncCacheViewAuthenticPixels(floodplane_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000250 break;
251 skip=x >= x1 ? MagickTrue : MagickFalse;
252 if (skip == MagickFalse)
253 {
254 start=x+1;
255 if (start < x1)
256 PushSegmentStack(y,start,x1-1,-offset);
257 x=x1+1;
258 }
259 do
260 {
261 if (skip == MagickFalse)
262 {
cristybb503372010-05-27 20:51:26 +0000263 if (x < (ssize_t) image->columns)
cristy3ed852e2009-09-05 21:47:34 +0000264 {
cristyb0d3bb92010-09-22 14:37:58 +0000265 p=GetCacheViewVirtualPixels(image_view,x,y,image->columns-x,1,
cristy3ed852e2009-09-05 21:47:34 +0000266 exception);
cristyb0d3bb92010-09-22 14:37:58 +0000267 q=GetCacheViewAuthenticPixels(floodplane_view,x,y,
268 image->columns-x,1,exception);
cristy636dcb52011-08-26 13:23:49 +0000269 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000270 break;
cristybb503372010-05-27 20:51:26 +0000271 for ( ; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000272 {
cristy4c08aed2011-07-01 19:47:50 +0000273 if (GetPixelAlpha(image,q) == TransparentAlpha)
cristy3ed852e2009-09-05 21:47:34 +0000274 break;
cristy4c08aed2011-07-01 19:47:50 +0000275 SetPixelInfo(image,p,&pixel);
276 if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert)
cristy3ed852e2009-09-05 21:47:34 +0000277 break;
cristy14973ba2011-08-27 23:48:07 +0000278 SetPixelAlpha(floodplane_image,TransparentAlpha,q);
cristyed231572011-07-14 02:18:59 +0000279 p+=GetPixelChannels(image);
280 q+=GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000281 }
cristy14973ba2011-08-27 23:48:07 +0000282 status=SyncCacheViewAuthenticPixels(floodplane_view,exception);
283 if (status == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000284 break;
285 }
286 PushSegmentStack(y,start,x-1,offset);
287 if (x > (x2+1))
288 PushSegmentStack(y,x2+1,x-1,-offset);
289 }
290 skip=MagickFalse;
291 x++;
292 if (x <= x2)
293 {
cristyb0d3bb92010-09-22 14:37:58 +0000294 p=GetCacheViewVirtualPixels(image_view,x,y,(size_t) (x2-x+1),1,
295 exception);
296 q=GetCacheViewAuthenticPixels(floodplane_view,x,y,(size_t) (x2-x+1),1,
cristy3ed852e2009-09-05 21:47:34 +0000297 exception);
cristy636dcb52011-08-26 13:23:49 +0000298 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000299 break;
cristy3ed852e2009-09-05 21:47:34 +0000300 for ( ; x <= x2; x++)
301 {
cristy4c08aed2011-07-01 19:47:50 +0000302 if (GetPixelAlpha(image,q) == TransparentAlpha)
cristy3ed852e2009-09-05 21:47:34 +0000303 break;
cristy4c08aed2011-07-01 19:47:50 +0000304 SetPixelInfo(image,p,&pixel);
305 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
cristy3ed852e2009-09-05 21:47:34 +0000306 break;
cristyed231572011-07-14 02:18:59 +0000307 p+=GetPixelChannels(image);
308 q+=GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000309 }
310 }
311 start=x;
312 } while (x <= x2);
313 }
cristybb503372010-05-27 20:51:26 +0000314 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000315 {
cristy4c08aed2011-07-01 19:47:50 +0000316 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000317 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000318
cristy4c08aed2011-07-01 19:47:50 +0000319 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000320 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000321
cristy14973ba2011-08-27 23:48:07 +0000322 register ssize_t
323 x;
324
cristy3ed852e2009-09-05 21:47:34 +0000325 /*
326 Tile fill color onto floodplane.
327 */
cristyb0d3bb92010-09-22 14:37:58 +0000328 p=GetCacheViewVirtualPixels(floodplane_view,0,y,image->columns,1,
329 exception);
330 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000331 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000332 break;
cristybb503372010-05-27 20:51:26 +0000333 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000334 {
cristy4c08aed2011-07-01 19:47:50 +0000335 if (GetPixelAlpha(floodplane_image,p) != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +0000336 {
cristy2ed42f62011-10-02 19:49:57 +0000337 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
cristy4c08aed2011-07-01 19:47:50 +0000338 SetPixelInfoPacket(image,&fill_color,&fill);
cristy3ed852e2009-09-05 21:47:34 +0000339 if (image->colorspace == CMYKColorspace)
340 ConvertRGBToCMYK(&fill);
cristyed231572011-07-14 02:18:59 +0000341 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000342 SetPixelRed(image,ClampToQuantum(fill.red),q);
cristyed231572011-07-14 02:18:59 +0000343 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000344 SetPixelGreen(image,ClampToQuantum(fill.green),q);
cristyed231572011-07-14 02:18:59 +0000345 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000346 SetPixelBlue(image,ClampToQuantum(fill.blue),q);
cristy14973ba2011-08-27 23:48:07 +0000347 if ((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000348 SetPixelBlack(image,ClampToQuantum(fill.black),q);
cristyed231572011-07-14 02:18:59 +0000349 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000350 SetPixelAlpha(image,ClampToQuantum(fill.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +0000351 }
cristyed231572011-07-14 02:18:59 +0000352 p+=GetPixelChannels(floodplane_image);
353 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000354 }
cristyb0d3bb92010-09-22 14:37:58 +0000355 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000356 break;
357 }
cristyb0d3bb92010-09-22 14:37:58 +0000358 floodplane_view=DestroyCacheView(floodplane_view);
359 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +0000360 segment_stack=(SegmentInfo *) RelinquishMagickMemory(segment_stack);
361 floodplane_image=DestroyImage(floodplane_image);
cristybb503372010-05-27 20:51:26 +0000362 return(y == (ssize_t) image->rows ? MagickTrue : MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +0000363}
364
365/*
366%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
367% %
368% %
369% %
370+ G r a d i e n t I m a g e %
371% %
372% %
373% %
374%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
375%
cristycee97112010-05-28 00:44:52 +0000376% GradientImage() applies a continuously smooth color transitions along a
cristy3ed852e2009-09-05 21:47:34 +0000377% vector from one color to another.
378%
379% Note, the interface of this method will change in the future to support
380% more than one transistion.
381%
382% The format of the GradientImage method is:
383%
384% MagickBooleanType GradientImage(Image *image,const GradientType type,
cristy101ab702011-10-13 13:06:32 +0000385% const SpreadMethod method,const PixelInfo *start_color,
386% const PixelInfo *stop_color,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000387%
388% A description of each parameter follows:
389%
390% o image: the image.
391%
392% o type: the gradient type: linear or radial.
393%
394% o spread: the gradient spread meathod: pad, reflect, or repeat.
395%
396% o start_color: the start color.
397%
398% o stop_color: the stop color.
399%
cristy189e84c2011-08-27 18:08:53 +0000400% o exception: return any errors or warnings in this structure.
401%
cristy3ed852e2009-09-05 21:47:34 +0000402*/
cristy117ff172010-08-15 21:35:32 +0000403
404static inline double MagickMax(const double x,const double y)
405{
406 return(x > y ? x : y);
407}
408
cristy3ed852e2009-09-05 21:47:34 +0000409MagickExport MagickBooleanType GradientImage(Image *image,
410 const GradientType type,const SpreadMethod method,
cristy101ab702011-10-13 13:06:32 +0000411 const PixelInfo *start_color,const PixelInfo *stop_color,
cristy189e84c2011-08-27 18:08:53 +0000412 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000413{
414 DrawInfo
415 *draw_info;
416
417 GradientInfo
418 *gradient;
419
420 MagickBooleanType
421 status;
422
cristybb503372010-05-27 20:51:26 +0000423 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000424 i;
425
426 /*
427 Set gradient start-stop end points.
428 */
429 assert(image != (const Image *) NULL);
430 assert(image->signature == MagickSignature);
431 if (image->debug != MagickFalse)
432 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy101ab702011-10-13 13:06:32 +0000433 assert(start_color != (const PixelInfo *) NULL);
434 assert(stop_color != (const PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000435 draw_info=AcquireDrawInfo();
436 gradient=(&draw_info->gradient);
437 gradient->type=type;
438 gradient->bounding_box.width=image->columns;
439 gradient->bounding_box.height=image->rows;
440 gradient->gradient_vector.x2=(double) image->columns-1.0;
441 gradient->gradient_vector.y2=(double) image->rows-1.0;
442 if ((type == LinearGradient) && (gradient->gradient_vector.y2 != 0.0))
443 gradient->gradient_vector.x2=0.0;
444 gradient->center.x=(double) gradient->gradient_vector.x2/2.0;
445 gradient->center.y=(double) gradient->gradient_vector.y2/2.0;
446 gradient->radius=MagickMax(gradient->center.x,gradient->center.y);
447 gradient->spread=method;
448 /*
449 Define the gradient to fill between the stops.
450 */
451 gradient->number_stops=2;
452 gradient->stops=(StopInfo *) AcquireQuantumMemory(gradient->number_stops,
453 sizeof(*gradient->stops));
454 if (gradient->stops == (StopInfo *) NULL)
455 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
456 image->filename);
457 (void) ResetMagickMemory(gradient->stops,0,gradient->number_stops*
458 sizeof(*gradient->stops));
cristybb503372010-05-27 20:51:26 +0000459 for (i=0; i < (ssize_t) gradient->number_stops; i++)
cristy4c08aed2011-07-01 19:47:50 +0000460 GetPixelInfo(image,&gradient->stops[i].color);
461 SetPixelInfoPacket(image,start_color,&gradient->stops[0].color);
cristy3ed852e2009-09-05 21:47:34 +0000462 gradient->stops[0].offset=0.0;
cristy4c08aed2011-07-01 19:47:50 +0000463 SetPixelInfoPacket(image,stop_color,&gradient->stops[1].color);
cristy3ed852e2009-09-05 21:47:34 +0000464 gradient->stops[1].offset=1.0;
465 /*
466 Draw a gradient on the image.
467 */
cristy947cb4c2011-10-20 18:41:46 +0000468 status=DrawGradientImage(image,draw_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000469 draw_info=DestroyDrawInfo(draw_info);
cristy4c08aed2011-07-01 19:47:50 +0000470 if ((start_color->alpha == OpaqueAlpha) && (stop_color->alpha == OpaqueAlpha))
cristy3ed852e2009-09-05 21:47:34 +0000471 image->matte=MagickFalse;
cristy101ab702011-10-13 13:06:32 +0000472 if ((IsPixelInfoGray(start_color) != MagickFalse) &&
473 (IsPixelInfoGray(stop_color) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +0000474 image->type=GrayscaleType;
475 return(status);
476}
477
478/*
479%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
480% %
481% %
482% %
483% O i l P a i n t I m a g e %
484% %
485% %
486% %
487%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
488%
489% OilPaintImage() applies a special effect filter that simulates an oil
490% painting. Each pixel is replaced by the most frequent color occurring
491% in a circular region defined by radius.
492%
493% The format of the OilPaintImage method is:
494%
495% Image *OilPaintImage(const Image *image,const double radius,
cristy14973ba2011-08-27 23:48:07 +0000496% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000497%
498% A description of each parameter follows:
499%
500% o image: the image.
501%
502% o radius: the radius of the circular neighborhood.
503%
cristy14973ba2011-08-27 23:48:07 +0000504% o sigma: the standard deviation of the Gaussian, in pixels.
505%
cristy3ed852e2009-09-05 21:47:34 +0000506% o exception: return any errors or warnings in this structure.
507%
508*/
509
cristybb503372010-05-27 20:51:26 +0000510static size_t **DestroyHistogramThreadSet(size_t **histogram)
cristy3ed852e2009-09-05 21:47:34 +0000511{
cristybb503372010-05-27 20:51:26 +0000512 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000513 i;
514
cristybb503372010-05-27 20:51:26 +0000515 assert(histogram != (size_t **) NULL);
516 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
517 if (histogram[i] != (size_t *) NULL)
518 histogram[i]=(size_t *) RelinquishMagickMemory(histogram[i]);
cristyb41ee102010-10-04 16:46:15 +0000519 histogram=(size_t **) RelinquishMagickMemory(histogram);
cristy3ed852e2009-09-05 21:47:34 +0000520 return(histogram);
521}
522
cristybb503372010-05-27 20:51:26 +0000523static size_t **AcquireHistogramThreadSet(const size_t count)
cristy3ed852e2009-09-05 21:47:34 +0000524{
cristybb503372010-05-27 20:51:26 +0000525 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000526 i;
527
cristybb503372010-05-27 20:51:26 +0000528 size_t
cristy3ed852e2009-09-05 21:47:34 +0000529 **histogram,
530 number_threads;
531
532 number_threads=GetOpenMPMaximumThreads();
cristy14973ba2011-08-27 23:48:07 +0000533 histogram=(size_t **) AcquireQuantumMemory(number_threads,sizeof(*histogram));
cristybb503372010-05-27 20:51:26 +0000534 if (histogram == (size_t **) NULL)
535 return((size_t **) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000536 (void) ResetMagickMemory(histogram,0,number_threads*sizeof(*histogram));
cristybb503372010-05-27 20:51:26 +0000537 for (i=0; i < (ssize_t) number_threads; i++)
cristy3ed852e2009-09-05 21:47:34 +0000538 {
cristy14973ba2011-08-27 23:48:07 +0000539 histogram[i]=(size_t *) AcquireQuantumMemory(count,sizeof(**histogram));
cristybb503372010-05-27 20:51:26 +0000540 if (histogram[i] == (size_t *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000541 return(DestroyHistogramThreadSet(histogram));
542 }
543 return(histogram);
544}
545
546MagickExport Image *OilPaintImage(const Image *image,const double radius,
cristy14973ba2011-08-27 23:48:07 +0000547 const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000548{
549#define NumberPaintBins 256
550#define OilPaintImageTag "OilPaint/Image"
551
cristyfa112112010-01-04 17:48:07 +0000552 CacheView
553 *image_view,
554 *paint_view;
555
cristy3ed852e2009-09-05 21:47:34 +0000556 Image
557 *paint_image;
558
cristy3ed852e2009-09-05 21:47:34 +0000559 MagickBooleanType
560 status;
561
cristybb503372010-05-27 20:51:26 +0000562 MagickOffsetType
563 progress;
564
565 size_t
cristy14973ba2011-08-27 23:48:07 +0000566 **histograms,
cristy3ed852e2009-09-05 21:47:34 +0000567 width;
568
cristybb503372010-05-27 20:51:26 +0000569 ssize_t
570 y;
571
cristy3ed852e2009-09-05 21:47:34 +0000572 /*
573 Initialize painted image attributes.
574 */
575 assert(image != (const Image *) NULL);
576 assert(image->signature == MagickSignature);
577 if (image->debug != MagickFalse)
578 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
579 assert(exception != (ExceptionInfo *) NULL);
580 assert(exception->signature == MagickSignature);
cristy14973ba2011-08-27 23:48:07 +0000581 width=GetOptimalKernelWidth2D(radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +0000582 paint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
583 if (paint_image == (Image *) NULL)
584 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000585 if (SetImageStorageClass(paint_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000586 {
cristy3ed852e2009-09-05 21:47:34 +0000587 paint_image=DestroyImage(paint_image);
588 return((Image *) NULL);
589 }
590 histograms=AcquireHistogramThreadSet(NumberPaintBins);
cristybb503372010-05-27 20:51:26 +0000591 if (histograms == (size_t **) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000592 {
593 paint_image=DestroyImage(paint_image);
594 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
595 }
596 /*
597 Oil paint image.
598 */
599 status=MagickTrue;
600 progress=0;
601 image_view=AcquireCacheView(image);
602 paint_view=AcquireCacheView(paint_image);
cristyb5d5f722009-11-04 03:03:49 +0000603#if defined(MAGICKCORE_OPENMP_SUPPORT)
604 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000605#endif
cristybb503372010-05-27 20:51:26 +0000606 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000607 {
cristy4c08aed2011-07-01 19:47:50 +0000608 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000609 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000610
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
cristy14973ba2011-08-27 23:48:07 +0000617 register ssize_t
618 x;
619
cristy3ed852e2009-09-05 21:47:34 +0000620 if (status == MagickFalse)
621 continue;
cristyfe4ba002011-02-28 14:54:12 +0000622 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
623 (width/2L),image->columns+width,width,exception);
cristy3ed852e2009-09-05 21:47:34 +0000624 q=QueueCacheViewAuthenticPixels(paint_view,0,y,paint_image->columns,1,
625 exception);
cristy4c08aed2011-07-01 19:47:50 +0000626 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000627 {
628 status=MagickFalse;
629 continue;
630 }
cristy3ed852e2009-09-05 21:47:34 +0000631 histogram=histograms[GetOpenMPThreadId()];
cristybb503372010-05-27 20:51:26 +0000632 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000633 {
cristybb503372010-05-27 20:51:26 +0000634 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000635 i,
636 u;
637
cristybb503372010-05-27 20:51:26 +0000638 size_t
cristy3ed852e2009-09-05 21:47:34 +0000639 count;
640
cristy9d314ff2011-03-09 01:30:28 +0000641 ssize_t
642 j,
643 k,
644 v;
645
cristy3ed852e2009-09-05 21:47:34 +0000646 /*
647 Assign most frequent color.
648 */
649 i=0;
650 j=0;
651 count=0;
652 (void) ResetMagickMemory(histogram,0,NumberPaintBins*sizeof(*histogram));
cristybb503372010-05-27 20:51:26 +0000653 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +0000654 {
cristybb503372010-05-27 20:51:26 +0000655 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +0000656 {
cristy14973ba2011-08-27 23:48:07 +0000657 k=(ssize_t) ScaleQuantumToChar(GetPixelIntensity(image,p+
658 GetPixelChannels(image)*(u+i)));
cristy3ed852e2009-09-05 21:47:34 +0000659 histogram[k]++;
660 if (histogram[k] > count)
661 {
662 j=i+u;
663 count=histogram[k];
664 }
665 }
cristyd99b0962010-05-29 23:14:26 +0000666 i+=(ssize_t) (image->columns+width);
cristy3ed852e2009-09-05 21:47:34 +0000667 }
cristy14973ba2011-08-27 23:48:07 +0000668 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
669 SetPixelRed(paint_image,GetPixelRed(image,p+j*
670 GetPixelChannels(image)),q);
671 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
672 SetPixelGreen(paint_image,GetPixelGreen(image,p+j*
673 GetPixelChannels(image)),q);
674 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
675 SetPixelBlue(paint_image,GetPixelBlue(image,p+j*
676 GetPixelChannels(image)),q);
677 if ((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000678 SetPixelBlack(paint_image,GetPixelBlack(image,p+j*
cristyed231572011-07-14 02:18:59 +0000679 GetPixelChannels(image)),q);
cristy14973ba2011-08-27 23:48:07 +0000680 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000681 SetPixelAlpha(paint_image,GetPixelAlpha(image,p+j*
cristyed231572011-07-14 02:18:59 +0000682 GetPixelChannels(image)),q);
683 p+=GetPixelChannels(image);
684 q+=GetPixelChannels(paint_image);
cristy3ed852e2009-09-05 21:47:34 +0000685 }
686 if (SyncCacheViewAuthenticPixels(paint_view,exception) == MagickFalse)
687 status=MagickFalse;
688 if (image->progress_monitor != (MagickProgressMonitor) NULL)
689 {
690 MagickBooleanType
691 proceed;
692
cristyb5d5f722009-11-04 03:03:49 +0000693#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000694 #pragma omp critical (MagickCore_OilPaintImage)
695#endif
696 proceed=SetImageProgress(image,OilPaintImageTag,progress++,image->rows);
697 if (proceed == MagickFalse)
698 status=MagickFalse;
699 }
700 }
701 paint_view=DestroyCacheView(paint_view);
702 image_view=DestroyCacheView(image_view);
703 histograms=DestroyHistogramThreadSet(histograms);
704 if (status == MagickFalse)
705 paint_image=DestroyImage(paint_image);
706 return(paint_image);
707}
708
709/*
710%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
711% %
712% %
713% %
714% O p a q u e P a i n t I m a g e %
715% %
716% %
717% %
718%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
719%
720% OpaquePaintImage() changes any pixel that matches color with the color
721% defined by fill.
722%
cristy14973ba2011-08-27 23:48:07 +0000723% By default color must match a particular pixel color exactly. However, in
724% many cases two colors may differ by a small amount. Fuzz defines how much
725% tolerance is acceptable to consider two colors as the same. For example,
726% set fuzz to 10 and the color red at intensities of 100 and 102 respectively
727% are now interpreted as the same color.
cristy3ed852e2009-09-05 21:47:34 +0000728%
729% The format of the OpaquePaintImage method is:
730%
731% MagickBooleanType OpaquePaintImage(Image *image,
cristy101ab702011-10-13 13:06:32 +0000732% const PixelInfo *target,const PixelInfo *fill,
cristy189e84c2011-08-27 18:08:53 +0000733% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000734%
735% A description of each parameter follows:
736%
737% o image: the image.
738%
cristy3ed852e2009-09-05 21:47:34 +0000739% o target: the RGB value of the target color.
740%
741% o fill: the replacement color.
742%
743% o invert: paint any pixel that does not match the target color.
744%
cristy189e84c2011-08-27 18:08:53 +0000745% o exception: return any errors or warnings in this structure.
746%
cristy3ed852e2009-09-05 21:47:34 +0000747*/
cristy3ed852e2009-09-05 21:47:34 +0000748MagickExport MagickBooleanType OpaquePaintImage(Image *image,
cristy189e84c2011-08-27 18:08:53 +0000749 const PixelInfo *target,const PixelInfo *fill,const MagickBooleanType invert,
750 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000751{
752#define OpaquePaintImageTag "Opaque/Image"
753
cristyc4c8d132010-01-07 01:58:38 +0000754 CacheView
755 *image_view;
756
cristy3ed852e2009-09-05 21:47:34 +0000757 MagickBooleanType
758 status;
759
cristybb503372010-05-27 20:51:26 +0000760 MagickOffsetType
761 progress;
762
cristy4c08aed2011-07-01 19:47:50 +0000763 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000764 zero;
765
cristybb503372010-05-27 20:51:26 +0000766 ssize_t
767 y;
768
cristy3ed852e2009-09-05 21:47:34 +0000769 assert(image != (Image *) NULL);
770 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +0000771 assert(target != (PixelInfo *) NULL);
772 assert(fill != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000773 if (image->debug != MagickFalse)
774 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy574cc262011-08-05 01:23:58 +0000775 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000776 return(MagickFalse);
777 /*
778 Make image color opaque.
779 */
780 status=MagickTrue;
781 progress=0;
cristy4c08aed2011-07-01 19:47:50 +0000782 GetPixelInfo(image,&zero);
cristy3ed852e2009-09-05 21:47:34 +0000783 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +0000784#if defined(MAGICKCORE_OPENMP_SUPPORT)
785 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000786#endif
cristybb503372010-05-27 20:51:26 +0000787 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000788 {
cristy4c08aed2011-07-01 19:47:50 +0000789 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000790 pixel;
791
cristy4c08aed2011-07-01 19:47:50 +0000792 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000793 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000794
cristy14973ba2011-08-27 23:48:07 +0000795 register ssize_t
796 x;
797
cristy3ed852e2009-09-05 21:47:34 +0000798 if (status == MagickFalse)
799 continue;
800 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy14973ba2011-08-27 23:48:07 +0000801 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000802 {
803 status=MagickFalse;
804 continue;
805 }
cristy3ed852e2009-09-05 21:47:34 +0000806 pixel=zero;
cristybb503372010-05-27 20:51:26 +0000807 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000808 {
cristy4c08aed2011-07-01 19:47:50 +0000809 SetPixelInfo(image,q,&pixel);
810 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
cristy3ed852e2009-09-05 21:47:34 +0000811 {
cristyed231572011-07-14 02:18:59 +0000812 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000813 SetPixelRed(image,ClampToQuantum(fill->red),q);
cristyed231572011-07-14 02:18:59 +0000814 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000815 SetPixelGreen(image,ClampToQuantum(fill->green),q);
cristyed231572011-07-14 02:18:59 +0000816 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000817 SetPixelBlue(image,ClampToQuantum(fill->blue),q);
cristy14973ba2011-08-27 23:48:07 +0000818 if ((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000819 SetPixelBlack(image,ClampToQuantum(fill->black),q);
cristyed231572011-07-14 02:18:59 +0000820 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000821 SetPixelAlpha(image,ClampToQuantum(fill->alpha),q);
cristy3ed852e2009-09-05 21:47:34 +0000822 }
cristyed231572011-07-14 02:18:59 +0000823 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000824 }
825 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
826 status=MagickFalse;
827 if (image->progress_monitor != (MagickProgressMonitor) NULL)
828 {
829 MagickBooleanType
830 proceed;
831
cristyb5d5f722009-11-04 03:03:49 +0000832#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyd42d9952011-07-08 14:21:50 +0000833 #pragma omp critical (MagickCore_OpaquePaintImage)
cristy3ed852e2009-09-05 21:47:34 +0000834#endif
835 proceed=SetImageProgress(image,OpaquePaintImageTag,progress++,
836 image->rows);
837 if (proceed == MagickFalse)
838 status=MagickFalse;
839 }
840 }
841 image_view=DestroyCacheView(image_view);
842 return(status);
843}
844
845/*
846%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
847% %
848% %
849% %
850% T r a n s p a r e n t P a i n t I m a g e %
851% %
852% %
853% %
854%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
855%
856% TransparentPaintImage() changes the opacity value associated with any pixel
857% that matches color to the value defined by opacity.
858%
cristy14973ba2011-08-27 23:48:07 +0000859% By default color must match a particular pixel color exactly. However, in
860% many cases two colors may differ by a small amount. Fuzz defines how much
861% tolerance is acceptable to consider two colors as the same. For example,
862% set fuzz to 10 and the color red at intensities of 100 and 102 respectively
863% are now interpreted as the same color.
cristy3ed852e2009-09-05 21:47:34 +0000864%
865% The format of the TransparentPaintImage method is:
866%
867% MagickBooleanType TransparentPaintImage(Image *image,
cristy4c08aed2011-07-01 19:47:50 +0000868% const PixelInfo *target,const Quantum opacity,
cristy189e84c2011-08-27 18:08:53 +0000869% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000870%
871% A description of each parameter follows:
872%
873% o image: the image.
874%
875% o target: the target color.
876%
877% o opacity: the replacement opacity value.
878%
879% o invert: paint any pixel that does not match the target color.
880%
cristy189e84c2011-08-27 18:08:53 +0000881% o exception: return any errors or warnings in this structure.
882%
cristy3ed852e2009-09-05 21:47:34 +0000883*/
884MagickExport MagickBooleanType TransparentPaintImage(Image *image,
cristy14973ba2011-08-27 23:48:07 +0000885 const PixelInfo *target,const Quantum opacity,const MagickBooleanType invert,
886 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000887{
888#define TransparentPaintImageTag "Transparent/Image"
889
cristyc4c8d132010-01-07 01:58:38 +0000890 CacheView
891 *image_view;
892
cristy3ed852e2009-09-05 21:47:34 +0000893 MagickBooleanType
894 status;
895
cristybb503372010-05-27 20:51:26 +0000896 MagickOffsetType
897 progress;
898
cristy4c08aed2011-07-01 19:47:50 +0000899 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000900 zero;
901
cristybb503372010-05-27 20:51:26 +0000902 ssize_t
903 y;
904
cristy3ed852e2009-09-05 21:47:34 +0000905 assert(image != (Image *) NULL);
906 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +0000907 assert(target != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000908 if (image->debug != MagickFalse)
909 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy574cc262011-08-05 01:23:58 +0000910 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000911 return(MagickFalse);
912 if (image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +0000913 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +0000914 /*
915 Make image color transparent.
916 */
917 status=MagickTrue;
918 progress=0;
cristy4c08aed2011-07-01 19:47:50 +0000919 GetPixelInfo(image,&zero);
cristy3ed852e2009-09-05 21:47:34 +0000920 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +0000921#if defined(MAGICKCORE_OPENMP_SUPPORT)
922 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000923#endif
cristybb503372010-05-27 20:51:26 +0000924 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000925 {
cristy4c08aed2011-07-01 19:47:50 +0000926 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000927 pixel;
928
cristybb503372010-05-27 20:51:26 +0000929 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000930 x;
931
cristy4c08aed2011-07-01 19:47:50 +0000932 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000933 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000934
935 if (status == MagickFalse)
936 continue;
937 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy14973ba2011-08-27 23:48:07 +0000938 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000939 {
940 status=MagickFalse;
941 continue;
942 }
cristy3ed852e2009-09-05 21:47:34 +0000943 pixel=zero;
cristybb503372010-05-27 20:51:26 +0000944 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000945 {
cristy4c08aed2011-07-01 19:47:50 +0000946 SetPixelInfo(image,q,&pixel);
947 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
948 SetPixelAlpha(image,opacity,q);
cristyed231572011-07-14 02:18:59 +0000949 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000950 }
951 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
952 status=MagickFalse;
953 if (image->progress_monitor != (MagickProgressMonitor) NULL)
954 {
955 MagickBooleanType
956 proceed;
957
cristyb5d5f722009-11-04 03:03:49 +0000958#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000959 #pragma omp critical (MagickCore_TransparentPaintImage)
960#endif
961 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
962 image->rows);
963 if (proceed == MagickFalse)
964 status=MagickFalse;
965 }
966 }
967 image_view=DestroyCacheView(image_view);
968 return(status);
969}
970
971/*
972%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
973% %
974% %
975% %
976% 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 %
977% %
978% %
979% %
980%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
981%
982% TransparentPaintImageChroma() changes the opacity value associated with any
983% pixel that matches color to the value defined by opacity.
984%
cristy14973ba2011-08-27 23:48:07 +0000985% As there is one fuzz value for the all the channels, TransparentPaintImage()
986% is not suitable for the operations like chroma, where the tolerance for
987% similarity of two color component (RGB) can be different. Thus we define
988% this method to take two target pixels (one low and one high) and all the
989% pixels of an image which are lying between these two pixels are made
990% transparent.
cristy3ed852e2009-09-05 21:47:34 +0000991%
cristy14973ba2011-08-27 23:48:07 +0000992% The format of the TransparentPaintImageChroma method is:
cristy3ed852e2009-09-05 21:47:34 +0000993%
cristy14973ba2011-08-27 23:48:07 +0000994% MagickBooleanType TransparentPaintImageChroma(Image *image,
995% const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
996% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000997%
998% A description of each parameter follows:
999%
1000% o image: the image.
1001%
1002% o low: the low target color.
1003%
1004% o high: the high target color.
1005%
1006% o opacity: the replacement opacity value.
1007%
1008% o invert: paint any pixel that does not match the target color.
1009%
cristy189e84c2011-08-27 18:08:53 +00001010% o exception: return any errors or warnings in this structure.
1011%
cristy3ed852e2009-09-05 21:47:34 +00001012*/
1013MagickExport MagickBooleanType TransparentPaintImageChroma(Image *image,
cristy189e84c2011-08-27 18:08:53 +00001014 const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
1015 const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001016{
1017#define TransparentPaintImageTag "Transparent/Image"
1018
cristyc4c8d132010-01-07 01:58:38 +00001019 CacheView
1020 *image_view;
1021
cristy3ed852e2009-09-05 21:47:34 +00001022 MagickBooleanType
1023 status;
1024
cristybb503372010-05-27 20:51:26 +00001025 MagickOffsetType
1026 progress;
1027
1028 ssize_t
1029 y;
1030
cristy3ed852e2009-09-05 21:47:34 +00001031 assert(image != (Image *) NULL);
1032 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +00001033 assert(high != (PixelInfo *) NULL);
1034 assert(low != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001035 if (image->debug != MagickFalse)
1036 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy574cc262011-08-05 01:23:58 +00001037 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001038 return(MagickFalse);
1039 if (image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00001040 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00001041 /*
1042 Make image color transparent.
1043 */
1044 status=MagickTrue;
1045 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00001046 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00001047#if defined(MAGICKCORE_OPENMP_SUPPORT)
1048 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00001049#endif
cristybb503372010-05-27 20:51:26 +00001050 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001051 {
1052 MagickBooleanType
1053 match;
1054
cristy4c08aed2011-07-01 19:47:50 +00001055 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001056 pixel;
1057
cristy4c08aed2011-07-01 19:47:50 +00001058 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001059 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001060
cristy14973ba2011-08-27 23:48:07 +00001061 register ssize_t
1062 x;
1063
cristy3ed852e2009-09-05 21:47:34 +00001064 if (status == MagickFalse)
1065 continue;
1066 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy14973ba2011-08-27 23:48:07 +00001067 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001068 {
1069 status=MagickFalse;
1070 continue;
1071 }
cristy4c08aed2011-07-01 19:47:50 +00001072 GetPixelInfo(image,&pixel);
cristybb503372010-05-27 20:51:26 +00001073 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001074 {
cristy4c08aed2011-07-01 19:47:50 +00001075 SetPixelInfo(image,q,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001076 match=((pixel.red >= low->red) && (pixel.red <= high->red) &&
1077 (pixel.green >= low->green) && (pixel.green <= high->green) &&
cristy14973ba2011-08-27 23:48:07 +00001078 (pixel.blue >= low->blue) && (pixel.blue <= high->blue)) ? MagickTrue :
1079 MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00001080 if (match != invert)
cristy4c08aed2011-07-01 19:47:50 +00001081 SetPixelAlpha(image,opacity,q);
cristyed231572011-07-14 02:18:59 +00001082 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001083 }
1084 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1085 status=MagickFalse;
1086 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1087 {
1088 MagickBooleanType
1089 proceed;
1090
cristyb5d5f722009-11-04 03:03:49 +00001091#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00001092 #pragma omp critical (MagickCore_TransparentPaintImageChroma)
1093#endif
1094 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
1095 image->rows);
1096 if (proceed == MagickFalse)
1097 status=MagickFalse;
1098 }
1099 }
1100 image_view=DestroyCacheView(image_view);
1101 return(status);
1102}