blob: a6ca982ed03c81539c6a02f8f41f4e7c79801cc6 [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 pixel;
142
cristy101ab702011-10-13 13:06:32 +0000143 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000144 fill_color;
145
146 register SegmentInfo
147 *s;
148
149 SegmentInfo
150 *segment_stack;
151
cristy9d314ff2011-03-09 01:30:28 +0000152 ssize_t
153 offset,
154 start,
155 x,
156 x1,
157 x2,
158 y;
159
cristy3ed852e2009-09-05 21:47:34 +0000160 /*
161 Check boundary conditions.
162 */
163 assert(image != (Image *) NULL);
164 assert(image->signature == MagickSignature);
165 if (image->debug != MagickFalse)
166 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
167 assert(draw_info != (DrawInfo *) NULL);
168 assert(draw_info->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +0000169 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
cristy3ed852e2009-09-05 21:47:34 +0000170 return(MagickFalse);
cristybb503372010-05-27 20:51:26 +0000171 if ((y_offset < 0) || (y_offset >= (ssize_t) image->rows))
cristy3ed852e2009-09-05 21:47:34 +0000172 return(MagickFalse);
cristy574cc262011-08-05 01:23:58 +0000173 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000174 return(MagickFalse);
175 if (image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +0000176 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +0000177 /*
178 Set floodfill state.
179 */
cristyc82a27b2011-10-21 01:07:16 +0000180 floodplane_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000181 if (floodplane_image == (Image *) NULL)
182 return(MagickFalse);
cristy63240882011-08-05 19:05:27 +0000183 (void) SetImageAlphaChannel(floodplane_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +0000184 segment_stack=(SegmentInfo *) AcquireQuantumMemory(MaxStacksize,
185 sizeof(*segment_stack));
186 if (segment_stack == (SegmentInfo *) NULL)
187 {
188 floodplane_image=DestroyImage(floodplane_image);
189 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
190 image->filename);
191 }
192 /*
193 Push initial segment on stack.
194 */
cristy14973ba2011-08-27 23:48:07 +0000195 status=MagickTrue;
cristy2ed42f62011-10-02 19:49:57 +0000196 fill_color.black=0.0;
197 fill_color.index=0.0;
cristy3ed852e2009-09-05 21:47:34 +0000198 x=x_offset;
199 y=y_offset;
200 start=0;
201 s=segment_stack;
202 PushSegmentStack(y,x,x,1);
203 PushSegmentStack(y+1,x,x,-1);
cristy4c08aed2011-07-01 19:47:50 +0000204 GetPixelInfo(image,&pixel);
cristyb0d3bb92010-09-22 14:37:58 +0000205 image_view=AcquireCacheView(image);
206 floodplane_view=AcquireCacheView(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000207 while (s > segment_stack)
208 {
cristy4c08aed2011-07-01 19:47:50 +0000209 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000210 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000211
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
cristy14973ba2011-08-27 23:48:07 +0000215 register ssize_t
216 x;
217
cristy3ed852e2009-09-05 21:47:34 +0000218 /*
219 Pop segment off stack.
220 */
221 s--;
cristybb503372010-05-27 20:51:26 +0000222 x1=(ssize_t) s->x1;
223 x2=(ssize_t) s->x2;
224 offset=(ssize_t) s->y2;
225 y=(ssize_t) s->y1+offset;
cristy3ed852e2009-09-05 21:47:34 +0000226 /*
227 Recolor neighboring pixels.
228 */
cristyb0d3bb92010-09-22 14:37:58 +0000229 p=GetCacheViewVirtualPixels(image_view,0,y,(size_t) (x1+1),1,exception);
230 q=GetCacheViewAuthenticPixels(floodplane_view,0,y,(size_t) (x1+1),1,
cristy3ed852e2009-09-05 21:47:34 +0000231 exception);
cristy4c08aed2011-07-01 19:47:50 +0000232 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000233 break;
cristyed231572011-07-14 02:18:59 +0000234 p+=x1*GetPixelChannels(image);
235 q+=x1*GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000236 for (x=x1; x >= 0; x--)
237 {
cristy4c08aed2011-07-01 19:47:50 +0000238 if (GetPixelAlpha(image,q) == TransparentAlpha)
cristy3ed852e2009-09-05 21:47:34 +0000239 break;
cristy803640d2011-11-17 02:11:32 +0000240 GetPixelInfoPixel(image,p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000241 if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert)
cristy3ed852e2009-09-05 21:47:34 +0000242 break;
cristy4c08aed2011-07-01 19:47:50 +0000243 SetPixelAlpha(floodplane_image,TransparentAlpha,q);
cristyed231572011-07-14 02:18:59 +0000244 p-=GetPixelChannels(image);
245 q-=GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000246 }
cristyb0d3bb92010-09-22 14:37:58 +0000247 if (SyncCacheViewAuthenticPixels(floodplane_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000248 break;
249 skip=x >= x1 ? MagickTrue : MagickFalse;
250 if (skip == MagickFalse)
251 {
252 start=x+1;
253 if (start < x1)
254 PushSegmentStack(y,start,x1-1,-offset);
255 x=x1+1;
256 }
257 do
258 {
259 if (skip == MagickFalse)
260 {
cristybb503372010-05-27 20:51:26 +0000261 if (x < (ssize_t) image->columns)
cristy3ed852e2009-09-05 21:47:34 +0000262 {
cristyb0d3bb92010-09-22 14:37:58 +0000263 p=GetCacheViewVirtualPixels(image_view,x,y,image->columns-x,1,
cristy3ed852e2009-09-05 21:47:34 +0000264 exception);
cristyb0d3bb92010-09-22 14:37:58 +0000265 q=GetCacheViewAuthenticPixels(floodplane_view,x,y,
266 image->columns-x,1,exception);
cristy636dcb52011-08-26 13:23:49 +0000267 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000268 break;
cristybb503372010-05-27 20:51:26 +0000269 for ( ; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000270 {
cristy4c08aed2011-07-01 19:47:50 +0000271 if (GetPixelAlpha(image,q) == TransparentAlpha)
cristy3ed852e2009-09-05 21:47:34 +0000272 break;
cristy803640d2011-11-17 02:11:32 +0000273 GetPixelInfoPixel(image,p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000274 if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert)
cristy3ed852e2009-09-05 21:47:34 +0000275 break;
cristy14973ba2011-08-27 23:48:07 +0000276 SetPixelAlpha(floodplane_image,TransparentAlpha,q);
cristyed231572011-07-14 02:18:59 +0000277 p+=GetPixelChannels(image);
278 q+=GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000279 }
cristy14973ba2011-08-27 23:48:07 +0000280 status=SyncCacheViewAuthenticPixels(floodplane_view,exception);
281 if (status == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000282 break;
283 }
284 PushSegmentStack(y,start,x-1,offset);
285 if (x > (x2+1))
286 PushSegmentStack(y,x2+1,x-1,-offset);
287 }
288 skip=MagickFalse;
289 x++;
290 if (x <= x2)
291 {
cristyb0d3bb92010-09-22 14:37:58 +0000292 p=GetCacheViewVirtualPixels(image_view,x,y,(size_t) (x2-x+1),1,
293 exception);
294 q=GetCacheViewAuthenticPixels(floodplane_view,x,y,(size_t) (x2-x+1),1,
cristy3ed852e2009-09-05 21:47:34 +0000295 exception);
cristy636dcb52011-08-26 13:23:49 +0000296 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000297 break;
cristy3ed852e2009-09-05 21:47:34 +0000298 for ( ; x <= x2; x++)
299 {
cristy4c08aed2011-07-01 19:47:50 +0000300 if (GetPixelAlpha(image,q) == TransparentAlpha)
cristy3ed852e2009-09-05 21:47:34 +0000301 break;
cristy803640d2011-11-17 02:11:32 +0000302 GetPixelInfoPixel(image,p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000303 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
cristy3ed852e2009-09-05 21:47:34 +0000304 break;
cristyed231572011-07-14 02:18:59 +0000305 p+=GetPixelChannels(image);
306 q+=GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000307 }
308 }
309 start=x;
310 } while (x <= x2);
311 }
cristybb503372010-05-27 20:51:26 +0000312 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000313 {
cristy4c08aed2011-07-01 19:47:50 +0000314 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000315 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000316
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
cristy14973ba2011-08-27 23:48:07 +0000320 register ssize_t
321 x;
322
cristy3ed852e2009-09-05 21:47:34 +0000323 /*
324 Tile fill color onto floodplane.
325 */
cristyb0d3bb92010-09-22 14:37:58 +0000326 p=GetCacheViewVirtualPixels(floodplane_view,0,y,image->columns,1,
327 exception);
328 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000329 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000330 break;
cristybb503372010-05-27 20:51:26 +0000331 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000332 {
cristy4c08aed2011-07-01 19:47:50 +0000333 if (GetPixelAlpha(floodplane_image,p) != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +0000334 {
cristy2ed42f62011-10-02 19:49:57 +0000335 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
cristyed231572011-07-14 02:18:59 +0000336 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy9d8c8ce2011-10-25 16:13:52 +0000337 SetPixelRed(image,ClampToQuantum(fill_color.red),q);
cristyed231572011-07-14 02:18:59 +0000338 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy9d8c8ce2011-10-25 16:13:52 +0000339 SetPixelGreen(image,ClampToQuantum(fill_color.green),q);
cristyed231572011-07-14 02:18:59 +0000340 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy9d8c8ce2011-10-25 16:13:52 +0000341 SetPixelBlue(image,ClampToQuantum(fill_color.blue),q);
cristy14973ba2011-08-27 23:48:07 +0000342 if ((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0)
cristy9d8c8ce2011-10-25 16:13:52 +0000343 SetPixelBlack(image,ClampToQuantum(fill_color.black),q);
cristyed231572011-07-14 02:18:59 +0000344 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy9d8c8ce2011-10-25 16:13:52 +0000345 SetPixelAlpha(image,ClampToQuantum(fill_color.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +0000346 }
cristyed231572011-07-14 02:18:59 +0000347 p+=GetPixelChannels(floodplane_image);
348 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000349 }
cristyb0d3bb92010-09-22 14:37:58 +0000350 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000351 break;
352 }
cristyb0d3bb92010-09-22 14:37:58 +0000353 floodplane_view=DestroyCacheView(floodplane_view);
354 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +0000355 segment_stack=(SegmentInfo *) RelinquishMagickMemory(segment_stack);
356 floodplane_image=DestroyImage(floodplane_image);
cristybb503372010-05-27 20:51:26 +0000357 return(y == (ssize_t) image->rows ? MagickTrue : MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +0000358}
359
360/*
361%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
362% %
363% %
364% %
365+ G r a d i e n t I m a g e %
366% %
367% %
368% %
369%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
370%
cristycee97112010-05-28 00:44:52 +0000371% GradientImage() applies a continuously smooth color transitions along a
cristy3ed852e2009-09-05 21:47:34 +0000372% vector from one color to another.
373%
374% Note, the interface of this method will change in the future to support
375% more than one transistion.
376%
377% The format of the GradientImage method is:
378%
379% MagickBooleanType GradientImage(Image *image,const GradientType type,
cristy101ab702011-10-13 13:06:32 +0000380% const SpreadMethod method,const PixelInfo *start_color,
381% const PixelInfo *stop_color,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000382%
383% A description of each parameter follows:
384%
385% o image: the image.
386%
387% o type: the gradient type: linear or radial.
388%
389% o spread: the gradient spread meathod: pad, reflect, or repeat.
390%
391% o start_color: the start color.
392%
393% o stop_color: the stop color.
394%
cristy189e84c2011-08-27 18:08:53 +0000395% o exception: return any errors or warnings in this structure.
396%
cristy3ed852e2009-09-05 21:47:34 +0000397*/
cristy117ff172010-08-15 21:35:32 +0000398
399static inline double MagickMax(const double x,const double y)
400{
401 return(x > y ? x : y);
402}
403
cristy3ed852e2009-09-05 21:47:34 +0000404MagickExport MagickBooleanType GradientImage(Image *image,
405 const GradientType type,const SpreadMethod method,
cristy101ab702011-10-13 13:06:32 +0000406 const PixelInfo *start_color,const PixelInfo *stop_color,
cristy189e84c2011-08-27 18:08:53 +0000407 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000408{
409 DrawInfo
410 *draw_info;
411
412 GradientInfo
413 *gradient;
414
415 MagickBooleanType
416 status;
417
cristybb503372010-05-27 20:51:26 +0000418 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000419 i;
420
421 /*
422 Set gradient start-stop end points.
423 */
424 assert(image != (const Image *) NULL);
425 assert(image->signature == MagickSignature);
426 if (image->debug != MagickFalse)
427 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy101ab702011-10-13 13:06:32 +0000428 assert(start_color != (const PixelInfo *) NULL);
429 assert(stop_color != (const PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000430 draw_info=AcquireDrawInfo();
431 gradient=(&draw_info->gradient);
432 gradient->type=type;
433 gradient->bounding_box.width=image->columns;
434 gradient->bounding_box.height=image->rows;
435 gradient->gradient_vector.x2=(double) image->columns-1.0;
436 gradient->gradient_vector.y2=(double) image->rows-1.0;
437 if ((type == LinearGradient) && (gradient->gradient_vector.y2 != 0.0))
438 gradient->gradient_vector.x2=0.0;
439 gradient->center.x=(double) gradient->gradient_vector.x2/2.0;
440 gradient->center.y=(double) gradient->gradient_vector.y2/2.0;
441 gradient->radius=MagickMax(gradient->center.x,gradient->center.y);
442 gradient->spread=method;
443 /*
444 Define the gradient to fill between the stops.
445 */
446 gradient->number_stops=2;
447 gradient->stops=(StopInfo *) AcquireQuantumMemory(gradient->number_stops,
448 sizeof(*gradient->stops));
449 if (gradient->stops == (StopInfo *) NULL)
450 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
451 image->filename);
452 (void) ResetMagickMemory(gradient->stops,0,gradient->number_stops*
453 sizeof(*gradient->stops));
cristybb503372010-05-27 20:51:26 +0000454 for (i=0; i < (ssize_t) gradient->number_stops; i++)
cristy4c08aed2011-07-01 19:47:50 +0000455 GetPixelInfo(image,&gradient->stops[i].color);
cristy9d8c8ce2011-10-25 16:13:52 +0000456 gradient->stops[0].color=(*start_color);
cristy3ed852e2009-09-05 21:47:34 +0000457 gradient->stops[0].offset=0.0;
cristy9d8c8ce2011-10-25 16:13:52 +0000458 gradient->stops[1].color=(*stop_color);
cristy3ed852e2009-09-05 21:47:34 +0000459 gradient->stops[1].offset=1.0;
460 /*
461 Draw a gradient on the image.
462 */
cristy947cb4c2011-10-20 18:41:46 +0000463 status=DrawGradientImage(image,draw_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000464 draw_info=DestroyDrawInfo(draw_info);
cristy4c08aed2011-07-01 19:47:50 +0000465 if ((start_color->alpha == OpaqueAlpha) && (stop_color->alpha == OpaqueAlpha))
cristy3ed852e2009-09-05 21:47:34 +0000466 image->matte=MagickFalse;
cristy101ab702011-10-13 13:06:32 +0000467 if ((IsPixelInfoGray(start_color) != MagickFalse) &&
468 (IsPixelInfoGray(stop_color) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +0000469 image->type=GrayscaleType;
470 return(status);
471}
472
473/*
474%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
475% %
476% %
477% %
478% O i l P a i n t I m a g e %
479% %
480% %
481% %
482%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
483%
484% OilPaintImage() applies a special effect filter that simulates an oil
485% painting. Each pixel is replaced by the most frequent color occurring
486% in a circular region defined by radius.
487%
488% The format of the OilPaintImage method is:
489%
490% Image *OilPaintImage(const Image *image,const double radius,
cristy14973ba2011-08-27 23:48:07 +0000491% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000492%
493% A description of each parameter follows:
494%
495% o image: the image.
496%
497% o radius: the radius of the circular neighborhood.
498%
cristy14973ba2011-08-27 23:48:07 +0000499% o sigma: the standard deviation of the Gaussian, in pixels.
500%
cristy3ed852e2009-09-05 21:47:34 +0000501% o exception: return any errors or warnings in this structure.
502%
503*/
504
cristybb503372010-05-27 20:51:26 +0000505static size_t **DestroyHistogramThreadSet(size_t **histogram)
cristy3ed852e2009-09-05 21:47:34 +0000506{
cristybb503372010-05-27 20:51:26 +0000507 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000508 i;
509
cristybb503372010-05-27 20:51:26 +0000510 assert(histogram != (size_t **) NULL);
511 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
512 if (histogram[i] != (size_t *) NULL)
513 histogram[i]=(size_t *) RelinquishMagickMemory(histogram[i]);
cristyb41ee102010-10-04 16:46:15 +0000514 histogram=(size_t **) RelinquishMagickMemory(histogram);
cristy3ed852e2009-09-05 21:47:34 +0000515 return(histogram);
516}
517
cristybb503372010-05-27 20:51:26 +0000518static size_t **AcquireHistogramThreadSet(const size_t count)
cristy3ed852e2009-09-05 21:47:34 +0000519{
cristybb503372010-05-27 20:51:26 +0000520 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000521 i;
522
cristybb503372010-05-27 20:51:26 +0000523 size_t
cristy3ed852e2009-09-05 21:47:34 +0000524 **histogram,
525 number_threads;
526
527 number_threads=GetOpenMPMaximumThreads();
cristy14973ba2011-08-27 23:48:07 +0000528 histogram=(size_t **) AcquireQuantumMemory(number_threads,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 {
cristy14973ba2011-08-27 23:48:07 +0000534 histogram[i]=(size_t *) AcquireQuantumMemory(count,sizeof(**histogram));
cristybb503372010-05-27 20:51:26 +0000535 if (histogram[i] == (size_t *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000536 return(DestroyHistogramThreadSet(histogram));
537 }
538 return(histogram);
539}
540
541MagickExport Image *OilPaintImage(const Image *image,const double radius,
cristy14973ba2011-08-27 23:48:07 +0000542 const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000543{
544#define NumberPaintBins 256
545#define OilPaintImageTag "OilPaint/Image"
546
cristyfa112112010-01-04 17:48:07 +0000547 CacheView
548 *image_view,
549 *paint_view;
550
cristy3ed852e2009-09-05 21:47:34 +0000551 Image
552 *paint_image;
553
cristy3ed852e2009-09-05 21:47:34 +0000554 MagickBooleanType
555 status;
556
cristybb503372010-05-27 20:51:26 +0000557 MagickOffsetType
558 progress;
559
560 size_t
cristy14973ba2011-08-27 23:48:07 +0000561 **histograms,
cristy3ed852e2009-09-05 21:47:34 +0000562 width;
563
cristybb503372010-05-27 20:51:26 +0000564 ssize_t
565 y;
566
cristy3ed852e2009-09-05 21:47:34 +0000567 /*
568 Initialize painted image attributes.
569 */
570 assert(image != (const Image *) NULL);
571 assert(image->signature == MagickSignature);
572 if (image->debug != MagickFalse)
573 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
574 assert(exception != (ExceptionInfo *) NULL);
575 assert(exception->signature == MagickSignature);
cristy14973ba2011-08-27 23:48:07 +0000576 width=GetOptimalKernelWidth2D(radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +0000577 paint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
578 if (paint_image == (Image *) NULL)
579 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000580 if (SetImageStorageClass(paint_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000581 {
cristy3ed852e2009-09-05 21:47:34 +0000582 paint_image=DestroyImage(paint_image);
583 return((Image *) NULL);
584 }
585 histograms=AcquireHistogramThreadSet(NumberPaintBins);
cristybb503372010-05-27 20:51:26 +0000586 if (histograms == (size_t **) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000587 {
588 paint_image=DestroyImage(paint_image);
589 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
590 }
591 /*
592 Oil paint image.
593 */
594 status=MagickTrue;
595 progress=0;
596 image_view=AcquireCacheView(image);
597 paint_view=AcquireCacheView(paint_image);
cristyb5d5f722009-11-04 03:03:49 +0000598#if defined(MAGICKCORE_OPENMP_SUPPORT)
599 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000600#endif
cristybb503372010-05-27 20:51:26 +0000601 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000602 {
cristy4c08aed2011-07-01 19:47:50 +0000603 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000604 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000605
cristy4c08aed2011-07-01 19:47:50 +0000606 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000607 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000608
cristybb503372010-05-27 20:51:26 +0000609 register size_t
cristy3ed852e2009-09-05 21:47:34 +0000610 *histogram;
611
cristy14973ba2011-08-27 23:48:07 +0000612 register ssize_t
613 x;
614
cristy3ed852e2009-09-05 21:47:34 +0000615 if (status == MagickFalse)
616 continue;
cristyfe4ba002011-02-28 14:54:12 +0000617 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
618 (width/2L),image->columns+width,width,exception);
cristy3ed852e2009-09-05 21:47:34 +0000619 q=QueueCacheViewAuthenticPixels(paint_view,0,y,paint_image->columns,1,
620 exception);
cristy4c08aed2011-07-01 19:47:50 +0000621 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000622 {
623 status=MagickFalse;
624 continue;
625 }
cristy3ed852e2009-09-05 21:47:34 +0000626 histogram=histograms[GetOpenMPThreadId()];
cristybb503372010-05-27 20:51:26 +0000627 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000628 {
cristybb503372010-05-27 20:51:26 +0000629 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000630 i,
631 u;
632
cristybb503372010-05-27 20:51:26 +0000633 size_t
cristy3ed852e2009-09-05 21:47:34 +0000634 count;
635
cristy9d314ff2011-03-09 01:30:28 +0000636 ssize_t
637 j,
638 k,
639 v;
640
cristy3ed852e2009-09-05 21:47:34 +0000641 /*
642 Assign most frequent color.
643 */
644 i=0;
645 j=0;
646 count=0;
647 (void) ResetMagickMemory(histogram,0,NumberPaintBins*sizeof(*histogram));
cristybb503372010-05-27 20:51:26 +0000648 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +0000649 {
cristybb503372010-05-27 20:51:26 +0000650 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +0000651 {
cristy14973ba2011-08-27 23:48:07 +0000652 k=(ssize_t) ScaleQuantumToChar(GetPixelIntensity(image,p+
653 GetPixelChannels(image)*(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 }
cristy14973ba2011-08-27 23:48:07 +0000663 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
664 SetPixelRed(paint_image,GetPixelRed(image,p+j*
665 GetPixelChannels(image)),q);
666 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
667 SetPixelGreen(paint_image,GetPixelGreen(image,p+j*
668 GetPixelChannels(image)),q);
669 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
670 SetPixelBlue(paint_image,GetPixelBlue(image,p+j*
671 GetPixelChannels(image)),q);
672 if ((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000673 SetPixelBlack(paint_image,GetPixelBlack(image,p+j*
cristyed231572011-07-14 02:18:59 +0000674 GetPixelChannels(image)),q);
cristy14973ba2011-08-27 23:48:07 +0000675 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000676 SetPixelAlpha(paint_image,GetPixelAlpha(image,p+j*
cristyed231572011-07-14 02:18:59 +0000677 GetPixelChannels(image)),q);
678 p+=GetPixelChannels(image);
679 q+=GetPixelChannels(paint_image);
cristy3ed852e2009-09-05 21:47:34 +0000680 }
681 if (SyncCacheViewAuthenticPixels(paint_view,exception) == MagickFalse)
682 status=MagickFalse;
683 if (image->progress_monitor != (MagickProgressMonitor) NULL)
684 {
685 MagickBooleanType
686 proceed;
687
cristyb5d5f722009-11-04 03:03:49 +0000688#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000689 #pragma omp critical (MagickCore_OilPaintImage)
690#endif
691 proceed=SetImageProgress(image,OilPaintImageTag,progress++,image->rows);
692 if (proceed == MagickFalse)
693 status=MagickFalse;
694 }
695 }
696 paint_view=DestroyCacheView(paint_view);
697 image_view=DestroyCacheView(image_view);
698 histograms=DestroyHistogramThreadSet(histograms);
699 if (status == MagickFalse)
700 paint_image=DestroyImage(paint_image);
701 return(paint_image);
702}
703
704/*
705%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
706% %
707% %
708% %
709% O p a q u e P a i n t I m a g e %
710% %
711% %
712% %
713%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
714%
715% OpaquePaintImage() changes any pixel that matches color with the color
716% defined by fill.
717%
cristy14973ba2011-08-27 23:48:07 +0000718% By default color must match a particular pixel color exactly. However, in
719% many cases two colors may differ by a small amount. Fuzz defines how much
720% tolerance is acceptable to consider two colors as the same. For example,
721% set fuzz to 10 and the color red at intensities of 100 and 102 respectively
722% are now interpreted as the same color.
cristy3ed852e2009-09-05 21:47:34 +0000723%
724% The format of the OpaquePaintImage method is:
725%
726% MagickBooleanType OpaquePaintImage(Image *image,
cristy101ab702011-10-13 13:06:32 +0000727% const PixelInfo *target,const PixelInfo *fill,
cristy189e84c2011-08-27 18:08:53 +0000728% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000729%
730% A description of each parameter follows:
731%
732% o image: the image.
733%
cristy3ed852e2009-09-05 21:47:34 +0000734% o target: the RGB value of the target color.
735%
736% o fill: the replacement color.
737%
738% o invert: paint any pixel that does not match the target color.
739%
cristy189e84c2011-08-27 18:08:53 +0000740% o exception: return any errors or warnings in this structure.
741%
cristy3ed852e2009-09-05 21:47:34 +0000742*/
cristy3ed852e2009-09-05 21:47:34 +0000743MagickExport MagickBooleanType OpaquePaintImage(Image *image,
cristy189e84c2011-08-27 18:08:53 +0000744 const PixelInfo *target,const PixelInfo *fill,const MagickBooleanType invert,
745 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000746{
747#define OpaquePaintImageTag "Opaque/Image"
748
cristyc4c8d132010-01-07 01:58:38 +0000749 CacheView
750 *image_view;
751
cristy3ed852e2009-09-05 21:47:34 +0000752 MagickBooleanType
753 status;
754
cristybb503372010-05-27 20:51:26 +0000755 MagickOffsetType
756 progress;
757
cristy4c08aed2011-07-01 19:47:50 +0000758 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000759 zero;
760
cristybb503372010-05-27 20:51:26 +0000761 ssize_t
762 y;
763
cristy3ed852e2009-09-05 21:47:34 +0000764 assert(image != (Image *) NULL);
765 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +0000766 assert(target != (PixelInfo *) NULL);
767 assert(fill != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000768 if (image->debug != MagickFalse)
769 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy574cc262011-08-05 01:23:58 +0000770 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000771 return(MagickFalse);
772 /*
773 Make image color opaque.
774 */
775 status=MagickTrue;
776 progress=0;
cristy4c08aed2011-07-01 19:47:50 +0000777 GetPixelInfo(image,&zero);
cristy3ed852e2009-09-05 21:47:34 +0000778 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +0000779#if defined(MAGICKCORE_OPENMP_SUPPORT)
780 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000781#endif
cristybb503372010-05-27 20:51:26 +0000782 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000783 {
cristy4c08aed2011-07-01 19:47:50 +0000784 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000785 pixel;
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
cristy14973ba2011-08-27 23:48:07 +0000790 register ssize_t
791 x;
792
cristy3ed852e2009-09-05 21:47:34 +0000793 if (status == MagickFalse)
794 continue;
795 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy14973ba2011-08-27 23:48:07 +0000796 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000797 {
798 status=MagickFalse;
799 continue;
800 }
cristy3ed852e2009-09-05 21:47:34 +0000801 pixel=zero;
cristybb503372010-05-27 20:51:26 +0000802 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000803 {
cristy803640d2011-11-17 02:11:32 +0000804 GetPixelInfoPixel(image,q,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000805 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
cristy3ed852e2009-09-05 21:47:34 +0000806 {
cristyed231572011-07-14 02:18:59 +0000807 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000808 SetPixelRed(image,ClampToQuantum(fill->red),q);
cristyed231572011-07-14 02:18:59 +0000809 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000810 SetPixelGreen(image,ClampToQuantum(fill->green),q);
cristyed231572011-07-14 02:18:59 +0000811 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000812 SetPixelBlue(image,ClampToQuantum(fill->blue),q);
cristy14973ba2011-08-27 23:48:07 +0000813 if ((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000814 SetPixelBlack(image,ClampToQuantum(fill->black),q);
cristyed231572011-07-14 02:18:59 +0000815 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000816 SetPixelAlpha(image,ClampToQuantum(fill->alpha),q);
cristy3ed852e2009-09-05 21:47:34 +0000817 }
cristyed231572011-07-14 02:18:59 +0000818 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000819 }
820 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
821 status=MagickFalse;
822 if (image->progress_monitor != (MagickProgressMonitor) NULL)
823 {
824 MagickBooleanType
825 proceed;
826
cristyb5d5f722009-11-04 03:03:49 +0000827#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyd42d9952011-07-08 14:21:50 +0000828 #pragma omp critical (MagickCore_OpaquePaintImage)
cristy3ed852e2009-09-05 21:47:34 +0000829#endif
830 proceed=SetImageProgress(image,OpaquePaintImageTag,progress++,
831 image->rows);
832 if (proceed == MagickFalse)
833 status=MagickFalse;
834 }
835 }
836 image_view=DestroyCacheView(image_view);
837 return(status);
838}
839
840/*
841%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
842% %
843% %
844% %
845% T r a n s p a r e n t P a i n t I m a g e %
846% %
847% %
848% %
849%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
850%
851% TransparentPaintImage() changes the opacity value associated with any pixel
852% that matches color to the value defined by opacity.
853%
cristy14973ba2011-08-27 23:48:07 +0000854% By default color must match a particular pixel color exactly. However, in
855% many cases two colors may differ by a small amount. Fuzz defines how much
856% tolerance is acceptable to consider two colors as the same. For example,
857% set fuzz to 10 and the color red at intensities of 100 and 102 respectively
858% are now interpreted as the same color.
cristy3ed852e2009-09-05 21:47:34 +0000859%
860% The format of the TransparentPaintImage method is:
861%
862% MagickBooleanType TransparentPaintImage(Image *image,
cristy4c08aed2011-07-01 19:47:50 +0000863% const PixelInfo *target,const Quantum opacity,
cristy189e84c2011-08-27 18:08:53 +0000864% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000865%
866% A description of each parameter follows:
867%
868% o image: the image.
869%
870% o target: the target color.
871%
872% o opacity: the replacement opacity value.
873%
874% o invert: paint any pixel that does not match the target color.
875%
cristy189e84c2011-08-27 18:08:53 +0000876% o exception: return any errors or warnings in this structure.
877%
cristy3ed852e2009-09-05 21:47:34 +0000878*/
879MagickExport MagickBooleanType TransparentPaintImage(Image *image,
cristy14973ba2011-08-27 23:48:07 +0000880 const PixelInfo *target,const Quantum opacity,const MagickBooleanType invert,
881 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000882{
883#define TransparentPaintImageTag "Transparent/Image"
884
cristyc4c8d132010-01-07 01:58:38 +0000885 CacheView
886 *image_view;
887
cristy3ed852e2009-09-05 21:47:34 +0000888 MagickBooleanType
889 status;
890
cristybb503372010-05-27 20:51:26 +0000891 MagickOffsetType
892 progress;
893
cristy4c08aed2011-07-01 19:47:50 +0000894 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000895 zero;
896
cristybb503372010-05-27 20:51:26 +0000897 ssize_t
898 y;
899
cristy3ed852e2009-09-05 21:47:34 +0000900 assert(image != (Image *) NULL);
901 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +0000902 assert(target != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000903 if (image->debug != MagickFalse)
904 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy574cc262011-08-05 01:23:58 +0000905 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000906 return(MagickFalse);
907 if (image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +0000908 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +0000909 /*
910 Make image color transparent.
911 */
912 status=MagickTrue;
913 progress=0;
cristy4c08aed2011-07-01 19:47:50 +0000914 GetPixelInfo(image,&zero);
cristy3ed852e2009-09-05 21:47:34 +0000915 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +0000916#if defined(MAGICKCORE_OPENMP_SUPPORT)
917 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000918#endif
cristybb503372010-05-27 20:51:26 +0000919 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000920 {
cristy4c08aed2011-07-01 19:47:50 +0000921 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000922 pixel;
923
cristybb503372010-05-27 20:51:26 +0000924 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000925 x;
926
cristy4c08aed2011-07-01 19:47:50 +0000927 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000928 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000929
930 if (status == MagickFalse)
931 continue;
932 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy14973ba2011-08-27 23:48:07 +0000933 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000934 {
935 status=MagickFalse;
936 continue;
937 }
cristy3ed852e2009-09-05 21:47:34 +0000938 pixel=zero;
cristybb503372010-05-27 20:51:26 +0000939 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000940 {
cristy803640d2011-11-17 02:11:32 +0000941 GetPixelInfoPixel(image,q,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000942 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
943 SetPixelAlpha(image,opacity,q);
cristyed231572011-07-14 02:18:59 +0000944 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000945 }
946 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
947 status=MagickFalse;
948 if (image->progress_monitor != (MagickProgressMonitor) NULL)
949 {
950 MagickBooleanType
951 proceed;
952
cristyb5d5f722009-11-04 03:03:49 +0000953#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000954 #pragma omp critical (MagickCore_TransparentPaintImage)
955#endif
956 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
957 image->rows);
958 if (proceed == MagickFalse)
959 status=MagickFalse;
960 }
961 }
962 image_view=DestroyCacheView(image_view);
963 return(status);
964}
965
966/*
967%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
968% %
969% %
970% %
971% 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 %
972% %
973% %
974% %
975%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
976%
977% TransparentPaintImageChroma() changes the opacity value associated with any
978% pixel that matches color to the value defined by opacity.
979%
cristy14973ba2011-08-27 23:48:07 +0000980% As there is one fuzz value for the all the channels, TransparentPaintImage()
981% is not suitable for the operations like chroma, where the tolerance for
982% similarity of two color component (RGB) can be different. Thus we define
983% this method to take two target pixels (one low and one high) and all the
984% pixels of an image which are lying between these two pixels are made
985% transparent.
cristy3ed852e2009-09-05 21:47:34 +0000986%
cristy14973ba2011-08-27 23:48:07 +0000987% The format of the TransparentPaintImageChroma method is:
cristy3ed852e2009-09-05 21:47:34 +0000988%
cristy14973ba2011-08-27 23:48:07 +0000989% MagickBooleanType TransparentPaintImageChroma(Image *image,
990% const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
991% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000992%
993% A description of each parameter follows:
994%
995% o image: the image.
996%
997% o low: the low target color.
998%
999% o high: the high target color.
1000%
1001% o opacity: the replacement opacity value.
1002%
1003% o invert: paint any pixel that does not match the target color.
1004%
cristy189e84c2011-08-27 18:08:53 +00001005% o exception: return any errors or warnings in this structure.
1006%
cristy3ed852e2009-09-05 21:47:34 +00001007*/
1008MagickExport MagickBooleanType TransparentPaintImageChroma(Image *image,
cristy189e84c2011-08-27 18:08:53 +00001009 const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
1010 const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001011{
1012#define TransparentPaintImageTag "Transparent/Image"
1013
cristyc4c8d132010-01-07 01:58:38 +00001014 CacheView
1015 *image_view;
1016
cristy3ed852e2009-09-05 21:47:34 +00001017 MagickBooleanType
1018 status;
1019
cristybb503372010-05-27 20:51:26 +00001020 MagickOffsetType
1021 progress;
1022
1023 ssize_t
1024 y;
1025
cristy3ed852e2009-09-05 21:47:34 +00001026 assert(image != (Image *) NULL);
1027 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +00001028 assert(high != (PixelInfo *) NULL);
1029 assert(low != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001030 if (image->debug != MagickFalse)
1031 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy574cc262011-08-05 01:23:58 +00001032 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001033 return(MagickFalse);
1034 if (image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00001035 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00001036 /*
1037 Make image color transparent.
1038 */
1039 status=MagickTrue;
1040 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00001041 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00001042#if defined(MAGICKCORE_OPENMP_SUPPORT)
1043 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00001044#endif
cristybb503372010-05-27 20:51:26 +00001045 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001046 {
1047 MagickBooleanType
1048 match;
1049
cristy4c08aed2011-07-01 19:47:50 +00001050 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001051 pixel;
1052
cristy4c08aed2011-07-01 19:47:50 +00001053 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001054 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001055
cristy14973ba2011-08-27 23:48:07 +00001056 register ssize_t
1057 x;
1058
cristy3ed852e2009-09-05 21:47:34 +00001059 if (status == MagickFalse)
1060 continue;
1061 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy14973ba2011-08-27 23:48:07 +00001062 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001063 {
1064 status=MagickFalse;
1065 continue;
1066 }
cristy4c08aed2011-07-01 19:47:50 +00001067 GetPixelInfo(image,&pixel);
cristybb503372010-05-27 20:51:26 +00001068 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001069 {
cristy803640d2011-11-17 02:11:32 +00001070 GetPixelInfoPixel(image,q,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001071 match=((pixel.red >= low->red) && (pixel.red <= high->red) &&
1072 (pixel.green >= low->green) && (pixel.green <= high->green) &&
cristy14973ba2011-08-27 23:48:07 +00001073 (pixel.blue >= low->blue) && (pixel.blue <= high->blue)) ? MagickTrue :
1074 MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00001075 if (match != invert)
cristy4c08aed2011-07-01 19:47:50 +00001076 SetPixelAlpha(image,opacity,q);
cristyed231572011-07-14 02:18:59 +00001077 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001078 }
1079 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1080 status=MagickFalse;
1081 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1082 {
1083 MagickBooleanType
1084 proceed;
1085
cristyb5d5f722009-11-04 03:03:49 +00001086#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00001087 #pragma omp critical (MagickCore_TransparentPaintImageChroma)
1088#endif
1089 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
1090 image->rows);
1091 if (proceed == MagickFalse)
1092 status=MagickFalse;
1093 }
1094 }
1095 image_view=DestroyCacheView(image_view);
1096 return(status);
1097}