blob: 65072d05720f73682bc43c41c3dfa633aa4e8a27 [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
144 PixelPacket
145 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;
cristy3ed852e2009-09-05 21:47:34 +0000197 x=x_offset;
198 y=y_offset;
199 start=0;
200 s=segment_stack;
201 PushSegmentStack(y,x,x,1);
202 PushSegmentStack(y+1,x,x,-1);
cristy4c08aed2011-07-01 19:47:50 +0000203 GetPixelInfo(image,&fill);
204 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;
cristy4c08aed2011-07-01 19:47:50 +0000240 SetPixelInfo(image,p,&pixel);
241 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;
cristy4c08aed2011-07-01 19:47:50 +0000273 SetPixelInfo(image,p,&pixel);
274 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;
cristy4c08aed2011-07-01 19:47:50 +0000302 SetPixelInfo(image,p,&pixel);
303 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
cristy3ed852e2009-09-05 21:47:34 +0000304 break;
cristyed231572011-07-14 02:18:59 +0000305 p+=GetPixelChannels(image);
306 q+=GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000307 }
308 }
309 start=x;
310 } while (x <= x2);
311 }
cristybb503372010-05-27 20:51:26 +0000312 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000313 {
cristy4c08aed2011-07-01 19:47:50 +0000314 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000315 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000316
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 {
335 (void) GetFillColor(draw_info,x,y,&fill_color);
cristy4c08aed2011-07-01 19:47:50 +0000336 SetPixelInfoPacket(image,&fill_color,&fill);
cristy3ed852e2009-09-05 21:47:34 +0000337 if (image->colorspace == CMYKColorspace)
338 ConvertRGBToCMYK(&fill);
cristyed231572011-07-14 02:18:59 +0000339 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000340 SetPixelRed(image,ClampToQuantum(fill.red),q);
cristyed231572011-07-14 02:18:59 +0000341 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000342 SetPixelGreen(image,ClampToQuantum(fill.green),q);
cristyed231572011-07-14 02:18:59 +0000343 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000344 SetPixelBlue(image,ClampToQuantum(fill.blue),q);
cristy14973ba2011-08-27 23:48:07 +0000345 if ((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000346 SetPixelBlack(image,ClampToQuantum(fill.black),q);
cristyed231572011-07-14 02:18:59 +0000347 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000348 SetPixelAlpha(image,ClampToQuantum(fill.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +0000349 }
cristyed231572011-07-14 02:18:59 +0000350 p+=GetPixelChannels(floodplane_image);
351 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000352 }
cristyb0d3bb92010-09-22 14:37:58 +0000353 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000354 break;
355 }
cristyb0d3bb92010-09-22 14:37:58 +0000356 floodplane_view=DestroyCacheView(floodplane_view);
357 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +0000358 segment_stack=(SegmentInfo *) RelinquishMagickMemory(segment_stack);
359 floodplane_image=DestroyImage(floodplane_image);
cristybb503372010-05-27 20:51:26 +0000360 return(y == (ssize_t) image->rows ? MagickTrue : MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +0000361}
362
363/*
364%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
365% %
366% %
367% %
368+ G r a d i e n t I m a g e %
369% %
370% %
371% %
372%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
373%
cristycee97112010-05-28 00:44:52 +0000374% GradientImage() applies a continuously smooth color transitions along a
cristy3ed852e2009-09-05 21:47:34 +0000375% vector from one color to another.
376%
377% Note, the interface of this method will change in the future to support
378% more than one transistion.
379%
380% The format of the GradientImage method is:
381%
382% MagickBooleanType GradientImage(Image *image,const GradientType type,
383% const SpreadMethod method,const PixelPacket *start_color,
cristy189e84c2011-08-27 18:08:53 +0000384% const PixelPacket *stop_color,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000385%
386% A description of each parameter follows:
387%
388% o image: the image.
389%
390% o type: the gradient type: linear or radial.
391%
392% o spread: the gradient spread meathod: pad, reflect, or repeat.
393%
394% o start_color: the start color.
395%
396% o stop_color: the stop color.
397%
cristy189e84c2011-08-27 18:08:53 +0000398% o exception: return any errors or warnings in this structure.
399%
cristy3ed852e2009-09-05 21:47:34 +0000400*/
cristy117ff172010-08-15 21:35:32 +0000401
402static inline double MagickMax(const double x,const double y)
403{
404 return(x > y ? x : y);
405}
406
cristy3ed852e2009-09-05 21:47:34 +0000407MagickExport MagickBooleanType GradientImage(Image *image,
408 const GradientType type,const SpreadMethod method,
cristy189e84c2011-08-27 18:08:53 +0000409 const PixelPacket *start_color,const PixelPacket *stop_color,
410 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000411{
412 DrawInfo
413 *draw_info;
414
415 GradientInfo
416 *gradient;
417
418 MagickBooleanType
419 status;
420
cristybb503372010-05-27 20:51:26 +0000421 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000422 i;
423
424 /*
425 Set gradient start-stop end points.
426 */
427 assert(image != (const Image *) NULL);
428 assert(image->signature == MagickSignature);
429 if (image->debug != MagickFalse)
430 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
431 assert(start_color != (const PixelPacket *) NULL);
432 assert(stop_color != (const PixelPacket *) NULL);
433 draw_info=AcquireDrawInfo();
434 gradient=(&draw_info->gradient);
435 gradient->type=type;
436 gradient->bounding_box.width=image->columns;
437 gradient->bounding_box.height=image->rows;
438 gradient->gradient_vector.x2=(double) image->columns-1.0;
439 gradient->gradient_vector.y2=(double) image->rows-1.0;
440 if ((type == LinearGradient) && (gradient->gradient_vector.y2 != 0.0))
441 gradient->gradient_vector.x2=0.0;
442 gradient->center.x=(double) gradient->gradient_vector.x2/2.0;
443 gradient->center.y=(double) gradient->gradient_vector.y2/2.0;
444 gradient->radius=MagickMax(gradient->center.x,gradient->center.y);
445 gradient->spread=method;
446 /*
447 Define the gradient to fill between the stops.
448 */
449 gradient->number_stops=2;
450 gradient->stops=(StopInfo *) AcquireQuantumMemory(gradient->number_stops,
451 sizeof(*gradient->stops));
452 if (gradient->stops == (StopInfo *) NULL)
453 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
454 image->filename);
455 (void) ResetMagickMemory(gradient->stops,0,gradient->number_stops*
456 sizeof(*gradient->stops));
cristybb503372010-05-27 20:51:26 +0000457 for (i=0; i < (ssize_t) gradient->number_stops; i++)
cristy4c08aed2011-07-01 19:47:50 +0000458 GetPixelInfo(image,&gradient->stops[i].color);
459 SetPixelInfoPacket(image,start_color,&gradient->stops[0].color);
cristy3ed852e2009-09-05 21:47:34 +0000460 gradient->stops[0].offset=0.0;
cristy4c08aed2011-07-01 19:47:50 +0000461 SetPixelInfoPacket(image,stop_color,&gradient->stops[1].color);
cristy3ed852e2009-09-05 21:47:34 +0000462 gradient->stops[1].offset=1.0;
463 /*
464 Draw a gradient on the image.
465 */
466 status=DrawGradientImage(image,draw_info);
467 draw_info=DestroyDrawInfo(draw_info);
cristy4c08aed2011-07-01 19:47:50 +0000468 if ((start_color->alpha == OpaqueAlpha) && (stop_color->alpha == OpaqueAlpha))
cristy3ed852e2009-09-05 21:47:34 +0000469 image->matte=MagickFalse;
cristy4c08aed2011-07-01 19:47:50 +0000470 if ((IsPixelPacketGray(start_color) != MagickFalse) &&
471 (IsPixelPacketGray(stop_color) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +0000472 image->type=GrayscaleType;
473 return(status);
474}
475
476/*
477%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
478% %
479% %
480% %
481% O i l P a i n t I m a g e %
482% %
483% %
484% %
485%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
486%
487% OilPaintImage() applies a special effect filter that simulates an oil
488% painting. Each pixel is replaced by the most frequent color occurring
489% in a circular region defined by radius.
490%
491% The format of the OilPaintImage method is:
492%
493% Image *OilPaintImage(const Image *image,const double radius,
cristy14973ba2011-08-27 23:48:07 +0000494% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000495%
496% A description of each parameter follows:
497%
498% o image: the image.
499%
500% o radius: the radius of the circular neighborhood.
501%
cristy14973ba2011-08-27 23:48:07 +0000502% o sigma: the standard deviation of the Gaussian, in pixels.
503%
cristy3ed852e2009-09-05 21:47:34 +0000504% o exception: return any errors or warnings in this structure.
505%
506*/
507
cristybb503372010-05-27 20:51:26 +0000508static size_t **DestroyHistogramThreadSet(size_t **histogram)
cristy3ed852e2009-09-05 21:47:34 +0000509{
cristybb503372010-05-27 20:51:26 +0000510 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000511 i;
512
cristybb503372010-05-27 20:51:26 +0000513 assert(histogram != (size_t **) NULL);
514 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
515 if (histogram[i] != (size_t *) NULL)
516 histogram[i]=(size_t *) RelinquishMagickMemory(histogram[i]);
cristyb41ee102010-10-04 16:46:15 +0000517 histogram=(size_t **) RelinquishMagickMemory(histogram);
cristy3ed852e2009-09-05 21:47:34 +0000518 return(histogram);
519}
520
cristybb503372010-05-27 20:51:26 +0000521static size_t **AcquireHistogramThreadSet(const size_t count)
cristy3ed852e2009-09-05 21:47:34 +0000522{
cristybb503372010-05-27 20:51:26 +0000523 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000524 i;
525
cristybb503372010-05-27 20:51:26 +0000526 size_t
cristy3ed852e2009-09-05 21:47:34 +0000527 **histogram,
528 number_threads;
529
530 number_threads=GetOpenMPMaximumThreads();
cristy14973ba2011-08-27 23:48:07 +0000531 histogram=(size_t **) AcquireQuantumMemory(number_threads,sizeof(*histogram));
cristybb503372010-05-27 20:51:26 +0000532 if (histogram == (size_t **) NULL)
533 return((size_t **) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000534 (void) ResetMagickMemory(histogram,0,number_threads*sizeof(*histogram));
cristybb503372010-05-27 20:51:26 +0000535 for (i=0; i < (ssize_t) number_threads; i++)
cristy3ed852e2009-09-05 21:47:34 +0000536 {
cristy14973ba2011-08-27 23:48:07 +0000537 histogram[i]=(size_t *) AcquireQuantumMemory(count,sizeof(**histogram));
cristybb503372010-05-27 20:51:26 +0000538 if (histogram[i] == (size_t *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000539 return(DestroyHistogramThreadSet(histogram));
540 }
541 return(histogram);
542}
543
544MagickExport Image *OilPaintImage(const Image *image,const double radius,
cristy14973ba2011-08-27 23:48:07 +0000545 const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000546{
547#define NumberPaintBins 256
548#define OilPaintImageTag "OilPaint/Image"
549
cristyfa112112010-01-04 17:48:07 +0000550 CacheView
551 *image_view,
552 *paint_view;
553
cristy3ed852e2009-09-05 21:47:34 +0000554 Image
555 *paint_image;
556
cristy3ed852e2009-09-05 21:47:34 +0000557 MagickBooleanType
558 status;
559
cristybb503372010-05-27 20:51:26 +0000560 MagickOffsetType
561 progress;
562
563 size_t
cristy14973ba2011-08-27 23:48:07 +0000564 **histograms,
cristy3ed852e2009-09-05 21:47:34 +0000565 width;
566
cristybb503372010-05-27 20:51:26 +0000567 ssize_t
568 y;
569
cristy3ed852e2009-09-05 21:47:34 +0000570 /*
571 Initialize painted image attributes.
572 */
573 assert(image != (const Image *) NULL);
574 assert(image->signature == MagickSignature);
575 if (image->debug != MagickFalse)
576 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
577 assert(exception != (ExceptionInfo *) NULL);
578 assert(exception->signature == MagickSignature);
cristy14973ba2011-08-27 23:48:07 +0000579 width=GetOptimalKernelWidth2D(radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +0000580 paint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
581 if (paint_image == (Image *) NULL)
582 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000583 if (SetImageStorageClass(paint_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000584 {
cristy3ed852e2009-09-05 21:47:34 +0000585 paint_image=DestroyImage(paint_image);
586 return((Image *) NULL);
587 }
588 histograms=AcquireHistogramThreadSet(NumberPaintBins);
cristybb503372010-05-27 20:51:26 +0000589 if (histograms == (size_t **) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000590 {
591 paint_image=DestroyImage(paint_image);
592 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
593 }
594 /*
595 Oil paint image.
596 */
597 status=MagickTrue;
598 progress=0;
599 image_view=AcquireCacheView(image);
600 paint_view=AcquireCacheView(paint_image);
cristyb5d5f722009-11-04 03:03:49 +0000601#if defined(MAGICKCORE_OPENMP_SUPPORT)
602 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000603#endif
cristybb503372010-05-27 20:51:26 +0000604 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000605 {
cristy4c08aed2011-07-01 19:47:50 +0000606 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000607 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000608
cristy4c08aed2011-07-01 19:47:50 +0000609 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000610 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000611
cristybb503372010-05-27 20:51:26 +0000612 register size_t
cristy3ed852e2009-09-05 21:47:34 +0000613 *histogram;
614
cristy14973ba2011-08-27 23:48:07 +0000615 register ssize_t
616 x;
617
cristy3ed852e2009-09-05 21:47:34 +0000618 if (status == MagickFalse)
619 continue;
cristyfe4ba002011-02-28 14:54:12 +0000620 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
621 (width/2L),image->columns+width,width,exception);
cristy3ed852e2009-09-05 21:47:34 +0000622 q=QueueCacheViewAuthenticPixels(paint_view,0,y,paint_image->columns,1,
623 exception);
cristy4c08aed2011-07-01 19:47:50 +0000624 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000625 {
626 status=MagickFalse;
627 continue;
628 }
cristy3ed852e2009-09-05 21:47:34 +0000629 histogram=histograms[GetOpenMPThreadId()];
cristybb503372010-05-27 20:51:26 +0000630 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000631 {
cristybb503372010-05-27 20:51:26 +0000632 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000633 i,
634 u;
635
cristybb503372010-05-27 20:51:26 +0000636 size_t
cristy3ed852e2009-09-05 21:47:34 +0000637 count;
638
cristy9d314ff2011-03-09 01:30:28 +0000639 ssize_t
640 j,
641 k,
642 v;
643
cristy3ed852e2009-09-05 21:47:34 +0000644 /*
645 Assign most frequent color.
646 */
647 i=0;
648 j=0;
649 count=0;
650 (void) ResetMagickMemory(histogram,0,NumberPaintBins*sizeof(*histogram));
cristybb503372010-05-27 20:51:26 +0000651 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +0000652 {
cristybb503372010-05-27 20:51:26 +0000653 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +0000654 {
cristy14973ba2011-08-27 23:48:07 +0000655 k=(ssize_t) ScaleQuantumToChar(GetPixelIntensity(image,p+
656 GetPixelChannels(image)*(u+i)));
cristy3ed852e2009-09-05 21:47:34 +0000657 histogram[k]++;
658 if (histogram[k] > count)
659 {
660 j=i+u;
661 count=histogram[k];
662 }
663 }
cristyd99b0962010-05-29 23:14:26 +0000664 i+=(ssize_t) (image->columns+width);
cristy3ed852e2009-09-05 21:47:34 +0000665 }
cristy14973ba2011-08-27 23:48:07 +0000666 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
667 SetPixelRed(paint_image,GetPixelRed(image,p+j*
668 GetPixelChannels(image)),q);
669 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
670 SetPixelGreen(paint_image,GetPixelGreen(image,p+j*
671 GetPixelChannels(image)),q);
672 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
673 SetPixelBlue(paint_image,GetPixelBlue(image,p+j*
674 GetPixelChannels(image)),q);
675 if ((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000676 SetPixelBlack(paint_image,GetPixelBlack(image,p+j*
cristyed231572011-07-14 02:18:59 +0000677 GetPixelChannels(image)),q);
cristy14973ba2011-08-27 23:48:07 +0000678 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000679 SetPixelAlpha(paint_image,GetPixelAlpha(image,p+j*
cristyed231572011-07-14 02:18:59 +0000680 GetPixelChannels(image)),q);
681 p+=GetPixelChannels(image);
682 q+=GetPixelChannels(paint_image);
cristy3ed852e2009-09-05 21:47:34 +0000683 }
684 if (SyncCacheViewAuthenticPixels(paint_view,exception) == MagickFalse)
685 status=MagickFalse;
686 if (image->progress_monitor != (MagickProgressMonitor) NULL)
687 {
688 MagickBooleanType
689 proceed;
690
cristyb5d5f722009-11-04 03:03:49 +0000691#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000692 #pragma omp critical (MagickCore_OilPaintImage)
693#endif
694 proceed=SetImageProgress(image,OilPaintImageTag,progress++,image->rows);
695 if (proceed == MagickFalse)
696 status=MagickFalse;
697 }
698 }
699 paint_view=DestroyCacheView(paint_view);
700 image_view=DestroyCacheView(image_view);
701 histograms=DestroyHistogramThreadSet(histograms);
702 if (status == MagickFalse)
703 paint_image=DestroyImage(paint_image);
704 return(paint_image);
705}
706
707/*
708%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
709% %
710% %
711% %
712% O p a q u e P a i n t I m a g e %
713% %
714% %
715% %
716%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
717%
718% OpaquePaintImage() changes any pixel that matches color with the color
719% defined by fill.
720%
cristy14973ba2011-08-27 23:48:07 +0000721% By default color must match a particular pixel color exactly. However, in
722% many cases two colors may differ by a small amount. Fuzz defines how much
723% tolerance is acceptable to consider two colors as the same. For example,
724% set fuzz to 10 and the color red at intensities of 100 and 102 respectively
725% are now interpreted as the same color.
cristy3ed852e2009-09-05 21:47:34 +0000726%
727% The format of the OpaquePaintImage method is:
728%
729% MagickBooleanType OpaquePaintImage(Image *image,
730% const PixelPacket *target,const PixelPacket *fill,
cristy189e84c2011-08-27 18:08:53 +0000731% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000732%
733% A description of each parameter follows:
734%
735% o image: the image.
736%
cristy3ed852e2009-09-05 21:47:34 +0000737% o target: the RGB value of the target color.
738%
739% o fill: the replacement color.
740%
741% o invert: paint any pixel that does not match the target color.
742%
cristy189e84c2011-08-27 18:08:53 +0000743% o exception: return any errors or warnings in this structure.
744%
cristy3ed852e2009-09-05 21:47:34 +0000745*/
cristy3ed852e2009-09-05 21:47:34 +0000746MagickExport MagickBooleanType OpaquePaintImage(Image *image,
cristy189e84c2011-08-27 18:08:53 +0000747 const PixelInfo *target,const PixelInfo *fill,const MagickBooleanType invert,
748 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000749{
750#define OpaquePaintImageTag "Opaque/Image"
751
cristyc4c8d132010-01-07 01:58:38 +0000752 CacheView
753 *image_view;
754
cristy3ed852e2009-09-05 21:47:34 +0000755 MagickBooleanType
756 status;
757
cristybb503372010-05-27 20:51:26 +0000758 MagickOffsetType
759 progress;
760
cristy4c08aed2011-07-01 19:47:50 +0000761 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000762 zero;
763
cristybb503372010-05-27 20:51:26 +0000764 ssize_t
765 y;
766
cristy3ed852e2009-09-05 21:47:34 +0000767 assert(image != (Image *) NULL);
768 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +0000769 assert(target != (PixelInfo *) NULL);
770 assert(fill != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000771 if (image->debug != MagickFalse)
772 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy574cc262011-08-05 01:23:58 +0000773 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000774 return(MagickFalse);
775 /*
776 Make image color opaque.
777 */
778 status=MagickTrue;
779 progress=0;
cristy4c08aed2011-07-01 19:47:50 +0000780 GetPixelInfo(image,&zero);
cristy3ed852e2009-09-05 21:47:34 +0000781 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +0000782#if defined(MAGICKCORE_OPENMP_SUPPORT)
783 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000784#endif
cristybb503372010-05-27 20:51:26 +0000785 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000786 {
cristy4c08aed2011-07-01 19:47:50 +0000787 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000788 pixel;
789
cristy4c08aed2011-07-01 19:47:50 +0000790 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000791 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000792
cristy14973ba2011-08-27 23:48:07 +0000793 register ssize_t
794 x;
795
cristy3ed852e2009-09-05 21:47:34 +0000796 if (status == MagickFalse)
797 continue;
798 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy14973ba2011-08-27 23:48:07 +0000799 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000800 {
801 status=MagickFalse;
802 continue;
803 }
cristy3ed852e2009-09-05 21:47:34 +0000804 pixel=zero;
cristybb503372010-05-27 20:51:26 +0000805 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000806 {
cristy4c08aed2011-07-01 19:47:50 +0000807 SetPixelInfo(image,q,&pixel);
808 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
cristy3ed852e2009-09-05 21:47:34 +0000809 {
cristyed231572011-07-14 02:18:59 +0000810 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000811 SetPixelRed(image,ClampToQuantum(fill->red),q);
cristyed231572011-07-14 02:18:59 +0000812 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000813 SetPixelGreen(image,ClampToQuantum(fill->green),q);
cristyed231572011-07-14 02:18:59 +0000814 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000815 SetPixelBlue(image,ClampToQuantum(fill->blue),q);
cristy14973ba2011-08-27 23:48:07 +0000816 if ((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000817 SetPixelBlack(image,ClampToQuantum(fill->black),q);
cristyed231572011-07-14 02:18:59 +0000818 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000819 SetPixelAlpha(image,ClampToQuantum(fill->alpha),q);
cristy3ed852e2009-09-05 21:47:34 +0000820 }
cristyed231572011-07-14 02:18:59 +0000821 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000822 }
823 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
824 status=MagickFalse;
825 if (image->progress_monitor != (MagickProgressMonitor) NULL)
826 {
827 MagickBooleanType
828 proceed;
829
cristyb5d5f722009-11-04 03:03:49 +0000830#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyd42d9952011-07-08 14:21:50 +0000831 #pragma omp critical (MagickCore_OpaquePaintImage)
cristy3ed852e2009-09-05 21:47:34 +0000832#endif
833 proceed=SetImageProgress(image,OpaquePaintImageTag,progress++,
834 image->rows);
835 if (proceed == MagickFalse)
836 status=MagickFalse;
837 }
838 }
839 image_view=DestroyCacheView(image_view);
840 return(status);
841}
842
843/*
844%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
845% %
846% %
847% %
848% T r a n s p a r e n t P a i n t I m a g e %
849% %
850% %
851% %
852%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
853%
854% TransparentPaintImage() changes the opacity value associated with any pixel
855% that matches color to the value defined by opacity.
856%
cristy14973ba2011-08-27 23:48:07 +0000857% By default color must match a particular pixel color exactly. However, in
858% many cases two colors may differ by a small amount. Fuzz defines how much
859% tolerance is acceptable to consider two colors as the same. For example,
860% set fuzz to 10 and the color red at intensities of 100 and 102 respectively
861% are now interpreted as the same color.
cristy3ed852e2009-09-05 21:47:34 +0000862%
863% The format of the TransparentPaintImage method is:
864%
865% MagickBooleanType TransparentPaintImage(Image *image,
cristy4c08aed2011-07-01 19:47:50 +0000866% const PixelInfo *target,const Quantum opacity,
cristy189e84c2011-08-27 18:08:53 +0000867% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000868%
869% A description of each parameter follows:
870%
871% o image: the image.
872%
873% o target: the target color.
874%
875% o opacity: the replacement opacity value.
876%
877% o invert: paint any pixel that does not match the target color.
878%
cristy189e84c2011-08-27 18:08:53 +0000879% o exception: return any errors or warnings in this structure.
880%
cristy3ed852e2009-09-05 21:47:34 +0000881*/
882MagickExport MagickBooleanType TransparentPaintImage(Image *image,
cristy14973ba2011-08-27 23:48:07 +0000883 const PixelInfo *target,const Quantum opacity,const MagickBooleanType invert,
884 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000885{
886#define TransparentPaintImageTag "Transparent/Image"
887
cristyc4c8d132010-01-07 01:58:38 +0000888 CacheView
889 *image_view;
890
cristy3ed852e2009-09-05 21:47:34 +0000891 MagickBooleanType
892 status;
893
cristybb503372010-05-27 20:51:26 +0000894 MagickOffsetType
895 progress;
896
cristy4c08aed2011-07-01 19:47:50 +0000897 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000898 zero;
899
cristybb503372010-05-27 20:51:26 +0000900 ssize_t
901 y;
902
cristy3ed852e2009-09-05 21:47:34 +0000903 assert(image != (Image *) NULL);
904 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +0000905 assert(target != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000906 if (image->debug != MagickFalse)
907 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy574cc262011-08-05 01:23:58 +0000908 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000909 return(MagickFalse);
910 if (image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +0000911 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +0000912 /*
913 Make image color transparent.
914 */
915 status=MagickTrue;
916 progress=0;
cristy4c08aed2011-07-01 19:47:50 +0000917 GetPixelInfo(image,&zero);
cristy3ed852e2009-09-05 21:47:34 +0000918 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +0000919#if defined(MAGICKCORE_OPENMP_SUPPORT)
920 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000921#endif
cristybb503372010-05-27 20:51:26 +0000922 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000923 {
cristy4c08aed2011-07-01 19:47:50 +0000924 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000925 pixel;
926
cristybb503372010-05-27 20:51:26 +0000927 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000928 x;
929
cristy4c08aed2011-07-01 19:47:50 +0000930 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000931 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000932
933 if (status == MagickFalse)
934 continue;
935 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy14973ba2011-08-27 23:48:07 +0000936 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000937 {
938 status=MagickFalse;
939 continue;
940 }
cristy3ed852e2009-09-05 21:47:34 +0000941 pixel=zero;
cristybb503372010-05-27 20:51:26 +0000942 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000943 {
cristy4c08aed2011-07-01 19:47:50 +0000944 SetPixelInfo(image,q,&pixel);
945 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
946 SetPixelAlpha(image,opacity,q);
cristyed231572011-07-14 02:18:59 +0000947 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000948 }
949 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
950 status=MagickFalse;
951 if (image->progress_monitor != (MagickProgressMonitor) NULL)
952 {
953 MagickBooleanType
954 proceed;
955
cristyb5d5f722009-11-04 03:03:49 +0000956#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000957 #pragma omp critical (MagickCore_TransparentPaintImage)
958#endif
959 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
960 image->rows);
961 if (proceed == MagickFalse)
962 status=MagickFalse;
963 }
964 }
965 image_view=DestroyCacheView(image_view);
966 return(status);
967}
968
969/*
970%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
971% %
972% %
973% %
974% 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 %
975% %
976% %
977% %
978%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
979%
980% TransparentPaintImageChroma() changes the opacity value associated with any
981% pixel that matches color to the value defined by opacity.
982%
cristy14973ba2011-08-27 23:48:07 +0000983% As there is one fuzz value for the all the channels, TransparentPaintImage()
984% is not suitable for the operations like chroma, where the tolerance for
985% similarity of two color component (RGB) can be different. Thus we define
986% this method to take two target pixels (one low and one high) and all the
987% pixels of an image which are lying between these two pixels are made
988% transparent.
cristy3ed852e2009-09-05 21:47:34 +0000989%
cristy14973ba2011-08-27 23:48:07 +0000990% The format of the TransparentPaintImageChroma method is:
cristy3ed852e2009-09-05 21:47:34 +0000991%
cristy14973ba2011-08-27 23:48:07 +0000992% MagickBooleanType TransparentPaintImageChroma(Image *image,
993% const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
994% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000995%
996% A description of each parameter follows:
997%
998% o image: the image.
999%
1000% o low: the low target color.
1001%
1002% o high: the high target color.
1003%
1004% o opacity: the replacement opacity value.
1005%
1006% o invert: paint any pixel that does not match the target color.
1007%
cristy189e84c2011-08-27 18:08:53 +00001008% o exception: return any errors or warnings in this structure.
1009%
cristy3ed852e2009-09-05 21:47:34 +00001010*/
1011MagickExport MagickBooleanType TransparentPaintImageChroma(Image *image,
cristy189e84c2011-08-27 18:08:53 +00001012 const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
1013 const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001014{
1015#define TransparentPaintImageTag "Transparent/Image"
1016
cristyc4c8d132010-01-07 01:58:38 +00001017 CacheView
1018 *image_view;
1019
cristy3ed852e2009-09-05 21:47:34 +00001020 MagickBooleanType
1021 status;
1022
cristybb503372010-05-27 20:51:26 +00001023 MagickOffsetType
1024 progress;
1025
1026 ssize_t
1027 y;
1028
cristy3ed852e2009-09-05 21:47:34 +00001029 assert(image != (Image *) NULL);
1030 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +00001031 assert(high != (PixelInfo *) NULL);
1032 assert(low != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001033 if (image->debug != MagickFalse)
1034 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy574cc262011-08-05 01:23:58 +00001035 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001036 return(MagickFalse);
1037 if (image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00001038 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00001039 /*
1040 Make image color transparent.
1041 */
1042 status=MagickTrue;
1043 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00001044 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00001045#if defined(MAGICKCORE_OPENMP_SUPPORT)
1046 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00001047#endif
cristybb503372010-05-27 20:51:26 +00001048 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001049 {
1050 MagickBooleanType
1051 match;
1052
cristy4c08aed2011-07-01 19:47:50 +00001053 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001054 pixel;
1055
cristy4c08aed2011-07-01 19:47:50 +00001056 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001057 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001058
cristy14973ba2011-08-27 23:48:07 +00001059 register ssize_t
1060 x;
1061
cristy3ed852e2009-09-05 21:47:34 +00001062 if (status == MagickFalse)
1063 continue;
1064 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy14973ba2011-08-27 23:48:07 +00001065 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001066 {
1067 status=MagickFalse;
1068 continue;
1069 }
cristy4c08aed2011-07-01 19:47:50 +00001070 GetPixelInfo(image,&pixel);
cristybb503372010-05-27 20:51:26 +00001071 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001072 {
cristy4c08aed2011-07-01 19:47:50 +00001073 SetPixelInfo(image,q,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001074 match=((pixel.red >= low->red) && (pixel.red <= high->red) &&
1075 (pixel.green >= low->green) && (pixel.green <= high->green) &&
cristy14973ba2011-08-27 23:48:07 +00001076 (pixel.blue >= low->blue) && (pixel.blue <= high->blue)) ? MagickTrue :
1077 MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00001078 if (match != invert)
cristy4c08aed2011-07-01 19:47:50 +00001079 SetPixelAlpha(image,opacity,q);
cristyed231572011-07-14 02:18:59 +00001080 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001081 }
1082 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1083 status=MagickFalse;
1084 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1085 {
1086 MagickBooleanType
1087 proceed;
1088
cristyb5d5f722009-11-04 03:03:49 +00001089#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00001090 #pragma omp critical (MagickCore_TransparentPaintImageChroma)
1091#endif
1092 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
1093 image->rows);
1094 if (proceed == MagickFalse)
1095 status=MagickFalse;
1096 }
1097 }
1098 image_view=DestroyCacheView(image_view);
1099 return(status);
1100}