blob: cf0299dcaa61a25e372a47207310334a773fc1ae [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% %
cristy1454be72011-12-19 01:52:48 +000020% Copyright 1999-2012 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"
cristyac245f82012-05-05 17:13:57 +000058#include "MagickCore/resource_.h"
cristy95f562a2012-01-01 20:49:11 +000059#include "MagickCore/statistic.h"
cristy4c08aed2011-07-01 19:47:50 +000060#include "MagickCore/string_.h"
61#include "MagickCore/thread-private.h"
cristy3ed852e2009-09-05 21:47:34 +000062
cristy3ed852e2009-09-05 21:47:34 +000063/*
64%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
65% %
66% %
67% %
68% F l o o d f i l l P a i n t I m a g e %
69% %
70% %
71% %
72%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73%
74% FloodfillPaintImage() changes the color value of any pixel that matches
75% target and is an immediate neighbor. If the method FillToBorderMethod is
76% specified, the color value is changed for any neighbor pixel that does not
77% match the bordercolor member of image.
78%
cristy908a0002011-08-28 00:13:39 +000079% By default target must match a particular pixel color exactly. However,
80% in many cases two colors may differ by a small amount. The fuzz member of
81% image defines how much tolerance is acceptable to consider two colors as
82% the same. For example, set fuzz to 10 and the color red at intensities of
83% 100 and 102 respectively are now interpreted as the same color for the
84% purposes of the floodfill.
cristy3ed852e2009-09-05 21:47:34 +000085%
86% The format of the FloodfillPaintImage method is:
87%
88% MagickBooleanType FloodfillPaintImage(Image *image,
cristyd42d9952011-07-08 14:21:50 +000089% const DrawInfo *draw_info,const PixelInfo target,
90% const ssize_t x_offset,const ssize_t y_offset,
cristy189e84c2011-08-27 18:08:53 +000091% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +000092%
93% A description of each parameter follows:
94%
95% o image: the image.
96%
cristy3ed852e2009-09-05 21:47:34 +000097% o draw_info: the draw info.
98%
99% o target: the RGB value of the target color.
100%
101% o x_offset,y_offset: the starting location of the operation.
102%
103% o invert: paint any pixel that does not match the target color.
104%
cristy189e84c2011-08-27 18:08:53 +0000105% o exception: return any errors or warnings in this structure.
106%
cristy3ed852e2009-09-05 21:47:34 +0000107*/
108MagickExport MagickBooleanType FloodfillPaintImage(Image *image,
cristyd42d9952011-07-08 14:21:50 +0000109 const DrawInfo *draw_info,const PixelInfo *target,const ssize_t x_offset,
cristy189e84c2011-08-27 18:08:53 +0000110 const ssize_t y_offset,const MagickBooleanType invert,
111 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000112{
113#define MaxStacksize (1UL << 15)
114#define PushSegmentStack(up,left,right,delta) \
115{ \
116 if (s >= (segment_stack+MaxStacksize)) \
117 ThrowBinaryException(DrawError,"SegmentStackOverflow",image->filename) \
118 else \
119 { \
cristybb503372010-05-27 20:51:26 +0000120 if ((((up)+(delta)) >= 0) && (((up)+(delta)) < (ssize_t) image->rows)) \
cristy3ed852e2009-09-05 21:47:34 +0000121 { \
122 s->x1=(double) (left); \
123 s->y1=(double) (up); \
124 s->x2=(double) (right); \
125 s->y2=(double) (delta); \
126 s++; \
127 } \
128 } \
129}
130
cristyb0d3bb92010-09-22 14:37:58 +0000131 CacheView
132 *floodplane_view,
133 *image_view;
134
cristy3ed852e2009-09-05 21:47:34 +0000135 Image
136 *floodplane_image;
137
cristy3ed852e2009-09-05 21:47:34 +0000138 MagickBooleanType
cristy14973ba2011-08-27 23:48:07 +0000139 skip,
140 status;
cristy3ed852e2009-09-05 21:47:34 +0000141
cristy4c08aed2011-07-01 19:47:50 +0000142 PixelInfo
cristyfdcc0182011-12-26 19:33:56 +0000143 fill_color,
cristy3ed852e2009-09-05 21:47:34 +0000144 pixel;
145
cristy3ed852e2009-09-05 21:47:34 +0000146 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);
cristy768165d2012-04-09 15:01:35 +0000175 if (IsGrayColorspace(image->colorspace) != MagickFalse)
176 (void) TransformImageColorspace(image,sRGBColorspace,exception);
cristy3ee7de52012-04-14 23:40:47 +0000177 if ((image->matte == MagickFalse) && (draw_info->fill.matte != MagickFalse))
cristy5b67d4e2012-02-07 19:43:53 +0000178 (void) SetImageAlpha(image,OpaqueAlpha,exception);
cristy3ed852e2009-09-05 21:47:34 +0000179 /*
180 Set floodfill state.
181 */
cristy95f562a2012-01-01 20:49:11 +0000182 floodplane_image=CloneImage(image,image->columns,image->rows,MagickTrue,
183 exception);
cristy3ed852e2009-09-05 21:47:34 +0000184 if (floodplane_image == (Image *) NULL)
185 return(MagickFalse);
cristy95f562a2012-01-01 20:49:11 +0000186 floodplane_image->colorspace=GRAYColorspace;
187 (void) EvaluateImage(floodplane_image,SetEvaluateOperator,0.0,exception);
cristy3ed852e2009-09-05 21:47:34 +0000188 segment_stack=(SegmentInfo *) AcquireQuantumMemory(MaxStacksize,
189 sizeof(*segment_stack));
190 if (segment_stack == (SegmentInfo *) NULL)
191 {
192 floodplane_image=DestroyImage(floodplane_image);
193 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
194 image->filename);
195 }
196 /*
197 Push initial segment on stack.
198 */
cristy14973ba2011-08-27 23:48:07 +0000199 status=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +0000200 x=x_offset;
201 y=y_offset;
202 start=0;
203 s=segment_stack;
204 PushSegmentStack(y,x,x,1);
205 PushSegmentStack(y+1,x,x,-1);
cristy4c08aed2011-07-01 19:47:50 +0000206 GetPixelInfo(image,&pixel);
cristydb070952012-04-20 14:33:00 +0000207 image_view=AcquireVirtualCacheView(image,exception);
208 floodplane_view=AcquireAuthenticCacheView(floodplane_image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000209 while (s > segment_stack)
210 {
cristy4c08aed2011-07-01 19:47:50 +0000211 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000212 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000213
cristy4c08aed2011-07-01 19:47:50 +0000214 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000215 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000216
cristy14973ba2011-08-27 23:48:07 +0000217 register ssize_t
218 x;
219
cristy3ed852e2009-09-05 21:47:34 +0000220 /*
221 Pop segment off stack.
222 */
223 s--;
cristybb503372010-05-27 20:51:26 +0000224 x1=(ssize_t) s->x1;
225 x2=(ssize_t) s->x2;
226 offset=(ssize_t) s->y2;
227 y=(ssize_t) s->y1+offset;
cristy3ed852e2009-09-05 21:47:34 +0000228 /*
229 Recolor neighboring pixels.
230 */
cristyb0d3bb92010-09-22 14:37:58 +0000231 p=GetCacheViewVirtualPixels(image_view,0,y,(size_t) (x1+1),1,exception);
232 q=GetCacheViewAuthenticPixels(floodplane_view,0,y,(size_t) (x1+1),1,
cristy3ed852e2009-09-05 21:47:34 +0000233 exception);
cristy4c08aed2011-07-01 19:47:50 +0000234 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000235 break;
cristyed231572011-07-14 02:18:59 +0000236 p+=x1*GetPixelChannels(image);
237 q+=x1*GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000238 for (x=x1; x >= 0; x--)
239 {
cristy95f562a2012-01-01 20:49:11 +0000240 if (GetPixelGray(floodplane_image,q) != 0)
cristy3ed852e2009-09-05 21:47:34 +0000241 break;
cristy803640d2011-11-17 02:11:32 +0000242 GetPixelInfoPixel(image,p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000243 if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert)
cristy3ed852e2009-09-05 21:47:34 +0000244 break;
cristy95f562a2012-01-01 20:49:11 +0000245 SetPixelGray(floodplane_image,QuantumRange,q);
cristyed231572011-07-14 02:18:59 +0000246 p-=GetPixelChannels(image);
247 q-=GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000248 }
cristyb0d3bb92010-09-22 14:37:58 +0000249 if (SyncCacheViewAuthenticPixels(floodplane_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000250 break;
251 skip=x >= x1 ? MagickTrue : MagickFalse;
252 if (skip == MagickFalse)
253 {
254 start=x+1;
255 if (start < x1)
256 PushSegmentStack(y,start,x1-1,-offset);
257 x=x1+1;
258 }
259 do
260 {
261 if (skip == MagickFalse)
262 {
cristybb503372010-05-27 20:51:26 +0000263 if (x < (ssize_t) image->columns)
cristy3ed852e2009-09-05 21:47:34 +0000264 {
cristyb0d3bb92010-09-22 14:37:58 +0000265 p=GetCacheViewVirtualPixels(image_view,x,y,image->columns-x,1,
cristy3ed852e2009-09-05 21:47:34 +0000266 exception);
cristyfdcc0182011-12-26 19:33:56 +0000267 q=GetCacheViewAuthenticPixels(floodplane_view,x,y,image->columns-
268 x,1,exception);
cristy636dcb52011-08-26 13:23:49 +0000269 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000270 break;
cristybb503372010-05-27 20:51:26 +0000271 for ( ; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000272 {
cristy95f562a2012-01-01 20:49:11 +0000273 if (GetPixelGray(floodplane_image,q) != 0)
cristy3ed852e2009-09-05 21:47:34 +0000274 break;
cristy803640d2011-11-17 02:11:32 +0000275 GetPixelInfoPixel(image,p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000276 if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert)
cristy3ed852e2009-09-05 21:47:34 +0000277 break;
cristy95f562a2012-01-01 20:49:11 +0000278 SetPixelGray(floodplane_image,QuantumRange,q);
cristyed231572011-07-14 02:18:59 +0000279 p+=GetPixelChannels(image);
280 q+=GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000281 }
cristy14973ba2011-08-27 23:48:07 +0000282 status=SyncCacheViewAuthenticPixels(floodplane_view,exception);
283 if (status == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000284 break;
285 }
286 PushSegmentStack(y,start,x-1,offset);
287 if (x > (x2+1))
288 PushSegmentStack(y,x2+1,x-1,-offset);
289 }
290 skip=MagickFalse;
291 x++;
292 if (x <= x2)
293 {
cristyb0d3bb92010-09-22 14:37:58 +0000294 p=GetCacheViewVirtualPixels(image_view,x,y,(size_t) (x2-x+1),1,
295 exception);
296 q=GetCacheViewAuthenticPixels(floodplane_view,x,y,(size_t) (x2-x+1),1,
cristy3ed852e2009-09-05 21:47:34 +0000297 exception);
cristy636dcb52011-08-26 13:23:49 +0000298 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000299 break;
cristy3ed852e2009-09-05 21:47:34 +0000300 for ( ; x <= x2; x++)
301 {
cristy95f562a2012-01-01 20:49:11 +0000302 if (GetPixelGray(floodplane_image,q) != 0)
cristy3ed852e2009-09-05 21:47:34 +0000303 break;
cristy803640d2011-11-17 02:11:32 +0000304 GetPixelInfoPixel(image,p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000305 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
cristy3ed852e2009-09-05 21:47:34 +0000306 break;
cristyed231572011-07-14 02:18:59 +0000307 p+=GetPixelChannels(image);
308 q+=GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000309 }
310 }
311 start=x;
312 } while (x <= x2);
313 }
cristybb503372010-05-27 20:51:26 +0000314 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000315 {
cristy4c08aed2011-07-01 19:47:50 +0000316 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000317 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000318
cristy4c08aed2011-07-01 19:47:50 +0000319 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000320 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000321
cristy14973ba2011-08-27 23:48:07 +0000322 register ssize_t
323 x;
324
cristy3ed852e2009-09-05 21:47:34 +0000325 /*
326 Tile fill color onto floodplane.
327 */
cristy95f562a2012-01-01 20:49:11 +0000328 p=GetCacheViewVirtualPixels(floodplane_view,0,y,image->columns,1,exception);
cristyb0d3bb92010-09-22 14:37:58 +0000329 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000330 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000331 break;
cristybb503372010-05-27 20:51:26 +0000332 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000333 {
cristy95f562a2012-01-01 20:49:11 +0000334 if (GetPixelGray(floodplane_image,p) != 0)
cristy3ed852e2009-09-05 21:47:34 +0000335 {
cristy2ed42f62011-10-02 19:49:57 +0000336 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
cristy8a20fa02011-12-27 15:54:31 +0000337 SetPixelInfoPixel(image,&fill_color,q);
cristy3ed852e2009-09-05 21:47:34 +0000338 }
cristyed231572011-07-14 02:18:59 +0000339 p+=GetPixelChannels(floodplane_image);
340 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000341 }
cristyb0d3bb92010-09-22 14:37:58 +0000342 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000343 break;
344 }
cristyb0d3bb92010-09-22 14:37:58 +0000345 floodplane_view=DestroyCacheView(floodplane_view);
346 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +0000347 segment_stack=(SegmentInfo *) RelinquishMagickMemory(segment_stack);
348 floodplane_image=DestroyImage(floodplane_image);
cristybb503372010-05-27 20:51:26 +0000349 return(y == (ssize_t) image->rows ? MagickTrue : MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +0000350}
351
352/*
353%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
354% %
355% %
356% %
357+ G r a d i e n t I m a g e %
358% %
359% %
360% %
361%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
362%
cristycee97112010-05-28 00:44:52 +0000363% GradientImage() applies a continuously smooth color transitions along a
cristy3ed852e2009-09-05 21:47:34 +0000364% vector from one color to another.
365%
366% Note, the interface of this method will change in the future to support
367% more than one transistion.
368%
369% The format of the GradientImage method is:
370%
371% MagickBooleanType GradientImage(Image *image,const GradientType type,
cristy101ab702011-10-13 13:06:32 +0000372% const SpreadMethod method,const PixelInfo *start_color,
373% const PixelInfo *stop_color,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000374%
375% A description of each parameter follows:
376%
377% o image: the image.
378%
379% o type: the gradient type: linear or radial.
380%
381% o spread: the gradient spread meathod: pad, reflect, or repeat.
382%
383% o start_color: the start color.
384%
385% o stop_color: the stop color.
386%
cristy189e84c2011-08-27 18:08:53 +0000387% o exception: return any errors or warnings in this structure.
388%
cristy3ed852e2009-09-05 21:47:34 +0000389*/
cristy117ff172010-08-15 21:35:32 +0000390
391static inline double MagickMax(const double x,const double y)
392{
393 return(x > y ? x : y);
394}
395
cristy3ed852e2009-09-05 21:47:34 +0000396MagickExport MagickBooleanType GradientImage(Image *image,
397 const GradientType type,const SpreadMethod method,
cristy101ab702011-10-13 13:06:32 +0000398 const PixelInfo *start_color,const PixelInfo *stop_color,
cristy189e84c2011-08-27 18:08:53 +0000399 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000400{
401 DrawInfo
402 *draw_info;
403
404 GradientInfo
405 *gradient;
406
407 MagickBooleanType
408 status;
409
cristybb503372010-05-27 20:51:26 +0000410 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000411 i;
412
413 /*
414 Set gradient start-stop end points.
415 */
416 assert(image != (const Image *) NULL);
417 assert(image->signature == MagickSignature);
418 if (image->debug != MagickFalse)
419 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy101ab702011-10-13 13:06:32 +0000420 assert(start_color != (const PixelInfo *) NULL);
421 assert(stop_color != (const PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000422 draw_info=AcquireDrawInfo();
423 gradient=(&draw_info->gradient);
424 gradient->type=type;
425 gradient->bounding_box.width=image->columns;
426 gradient->bounding_box.height=image->rows;
427 gradient->gradient_vector.x2=(double) image->columns-1.0;
428 gradient->gradient_vector.y2=(double) image->rows-1.0;
429 if ((type == LinearGradient) && (gradient->gradient_vector.y2 != 0.0))
430 gradient->gradient_vector.x2=0.0;
431 gradient->center.x=(double) gradient->gradient_vector.x2/2.0;
432 gradient->center.y=(double) gradient->gradient_vector.y2/2.0;
433 gradient->radius=MagickMax(gradient->center.x,gradient->center.y);
434 gradient->spread=method;
435 /*
436 Define the gradient to fill between the stops.
437 */
438 gradient->number_stops=2;
439 gradient->stops=(StopInfo *) AcquireQuantumMemory(gradient->number_stops,
440 sizeof(*gradient->stops));
441 if (gradient->stops == (StopInfo *) NULL)
442 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
443 image->filename);
444 (void) ResetMagickMemory(gradient->stops,0,gradient->number_stops*
445 sizeof(*gradient->stops));
cristybb503372010-05-27 20:51:26 +0000446 for (i=0; i < (ssize_t) gradient->number_stops; i++)
cristy4c08aed2011-07-01 19:47:50 +0000447 GetPixelInfo(image,&gradient->stops[i].color);
cristy9d8c8ce2011-10-25 16:13:52 +0000448 gradient->stops[0].color=(*start_color);
cristy3ed852e2009-09-05 21:47:34 +0000449 gradient->stops[0].offset=0.0;
cristy9d8c8ce2011-10-25 16:13:52 +0000450 gradient->stops[1].color=(*stop_color);
cristy3ed852e2009-09-05 21:47:34 +0000451 gradient->stops[1].offset=1.0;
452 /*
453 Draw a gradient on the image.
454 */
cristy947cb4c2011-10-20 18:41:46 +0000455 status=DrawGradientImage(image,draw_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000456 draw_info=DestroyDrawInfo(draw_info);
cristy09250192012-02-07 18:53:31 +0000457 if ((start_color->matte == MagickFalse) && (stop_color->matte == MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +0000458 image->matte=MagickFalse;
cristy101ab702011-10-13 13:06:32 +0000459 if ((IsPixelInfoGray(start_color) != MagickFalse) &&
460 (IsPixelInfoGray(stop_color) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +0000461 image->type=GrayscaleType;
462 return(status);
463}
464
465/*
466%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
467% %
468% %
469% %
470% O i l P a i n t I m a g e %
471% %
472% %
473% %
474%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
475%
476% OilPaintImage() applies a special effect filter that simulates an oil
477% painting. Each pixel is replaced by the most frequent color occurring
478% in a circular region defined by radius.
479%
480% The format of the OilPaintImage method is:
481%
482% Image *OilPaintImage(const Image *image,const double radius,
cristy14973ba2011-08-27 23:48:07 +0000483% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000484%
485% A description of each parameter follows:
486%
487% o image: the image.
488%
489% o radius: the radius of the circular neighborhood.
490%
cristy14973ba2011-08-27 23:48:07 +0000491% o sigma: the standard deviation of the Gaussian, in pixels.
492%
cristy3ed852e2009-09-05 21:47:34 +0000493% o exception: return any errors or warnings in this structure.
494%
495*/
496
cristybb503372010-05-27 20:51:26 +0000497static size_t **DestroyHistogramThreadSet(size_t **histogram)
cristy3ed852e2009-09-05 21:47:34 +0000498{
cristybb503372010-05-27 20:51:26 +0000499 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000500 i;
501
cristybb503372010-05-27 20:51:26 +0000502 assert(histogram != (size_t **) NULL);
cristyac245f82012-05-05 17:13:57 +0000503 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
cristybb503372010-05-27 20:51:26 +0000504 if (histogram[i] != (size_t *) NULL)
505 histogram[i]=(size_t *) RelinquishMagickMemory(histogram[i]);
cristyb41ee102010-10-04 16:46:15 +0000506 histogram=(size_t **) RelinquishMagickMemory(histogram);
cristy3ed852e2009-09-05 21:47:34 +0000507 return(histogram);
508}
509
cristybb503372010-05-27 20:51:26 +0000510static size_t **AcquireHistogramThreadSet(const size_t count)
cristy3ed852e2009-09-05 21:47:34 +0000511{
cristybb503372010-05-27 20:51:26 +0000512 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000513 i;
514
cristybb503372010-05-27 20:51:26 +0000515 size_t
cristy3ed852e2009-09-05 21:47:34 +0000516 **histogram,
517 number_threads;
518
cristyfeeb98d2012-05-09 16:32:12 +0000519 number_threads=GetOpenMPMaximumThreads();
cristy14973ba2011-08-27 23:48:07 +0000520 histogram=(size_t **) AcquireQuantumMemory(number_threads,sizeof(*histogram));
cristybb503372010-05-27 20:51:26 +0000521 if (histogram == (size_t **) NULL)
522 return((size_t **) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000523 (void) ResetMagickMemory(histogram,0,number_threads*sizeof(*histogram));
cristybb503372010-05-27 20:51:26 +0000524 for (i=0; i < (ssize_t) number_threads; i++)
cristy3ed852e2009-09-05 21:47:34 +0000525 {
cristy14973ba2011-08-27 23:48:07 +0000526 histogram[i]=(size_t *) AcquireQuantumMemory(count,sizeof(**histogram));
cristybb503372010-05-27 20:51:26 +0000527 if (histogram[i] == (size_t *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000528 return(DestroyHistogramThreadSet(histogram));
529 }
530 return(histogram);
531}
532
533MagickExport Image *OilPaintImage(const Image *image,const double radius,
cristy14973ba2011-08-27 23:48:07 +0000534 const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000535{
536#define NumberPaintBins 256
537#define OilPaintImageTag "OilPaint/Image"
538
cristyfa112112010-01-04 17:48:07 +0000539 CacheView
540 *image_view,
541 *paint_view;
542
cristy3ed852e2009-09-05 21:47:34 +0000543 Image
544 *paint_image;
545
cristy3ed852e2009-09-05 21:47:34 +0000546 MagickBooleanType
547 status;
548
cristybb503372010-05-27 20:51:26 +0000549 MagickOffsetType
550 progress;
551
552 size_t
cristy14973ba2011-08-27 23:48:07 +0000553 **histograms,
cristy3ed852e2009-09-05 21:47:34 +0000554 width;
555
cristybb503372010-05-27 20:51:26 +0000556 ssize_t
cristyf8561542012-01-24 00:26:46 +0000557 center,
cristybb503372010-05-27 20:51:26 +0000558 y;
559
cristy3ed852e2009-09-05 21:47:34 +0000560 /*
561 Initialize painted image attributes.
562 */
563 assert(image != (const Image *) NULL);
564 assert(image->signature == MagickSignature);
565 if (image->debug != MagickFalse)
566 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
567 assert(exception != (ExceptionInfo *) NULL);
568 assert(exception->signature == MagickSignature);
cristy14973ba2011-08-27 23:48:07 +0000569 width=GetOptimalKernelWidth2D(radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +0000570 paint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
571 if (paint_image == (Image *) NULL)
572 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000573 if (SetImageStorageClass(paint_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000574 {
cristy3ed852e2009-09-05 21:47:34 +0000575 paint_image=DestroyImage(paint_image);
576 return((Image *) NULL);
577 }
578 histograms=AcquireHistogramThreadSet(NumberPaintBins);
cristybb503372010-05-27 20:51:26 +0000579 if (histograms == (size_t **) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000580 {
581 paint_image=DestroyImage(paint_image);
582 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
583 }
584 /*
585 Oil paint image.
586 */
587 status=MagickTrue;
588 progress=0;
cristyd09f8802012-02-04 16:44:10 +0000589 center=(ssize_t) GetPixelChannels(image)*(image->columns+width)*(width/2L)+
590 GetPixelChannels(image)*(width/2L);
cristydb070952012-04-20 14:33:00 +0000591 image_view=AcquireVirtualCacheView(image,exception);
592 paint_view=AcquireAuthenticCacheView(paint_image,exception);
cristyb5d5f722009-11-04 03:03:49 +0000593#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000594 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristyddacdd12012-05-07 23:08:14 +0000595 dynamic_number_threads(image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +0000596#endif
cristybb503372010-05-27 20:51:26 +0000597 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000598 {
cristy4c08aed2011-07-01 19:47:50 +0000599 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000600 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000601
cristy4c08aed2011-07-01 19:47:50 +0000602 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000603 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000604
cristybb503372010-05-27 20:51:26 +0000605 register size_t
cristy3ed852e2009-09-05 21:47:34 +0000606 *histogram;
607
cristy14973ba2011-08-27 23:48:07 +0000608 register ssize_t
609 x;
610
cristy3ed852e2009-09-05 21:47:34 +0000611 if (status == MagickFalse)
612 continue;
cristyfe4ba002011-02-28 14:54:12 +0000613 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
614 (width/2L),image->columns+width,width,exception);
cristy3ed852e2009-09-05 21:47:34 +0000615 q=QueueCacheViewAuthenticPixels(paint_view,0,y,paint_image->columns,1,
616 exception);
cristy4c08aed2011-07-01 19:47:50 +0000617 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000618 {
619 status=MagickFalse;
620 continue;
621 }
cristy3ed852e2009-09-05 21:47:34 +0000622 histogram=histograms[GetOpenMPThreadId()];
cristybb503372010-05-27 20:51:26 +0000623 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000624 {
cristybb503372010-05-27 20:51:26 +0000625 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000626 i,
627 u;
628
cristybb503372010-05-27 20:51:26 +0000629 size_t
cristy3ed852e2009-09-05 21:47:34 +0000630 count;
631
cristy9d314ff2011-03-09 01:30:28 +0000632 ssize_t
633 j,
634 k,
cristyf8561542012-01-24 00:26:46 +0000635 n,
cristy9d314ff2011-03-09 01:30:28 +0000636 v;
637
cristy3ed852e2009-09-05 21:47:34 +0000638 /*
639 Assign most frequent color.
640 */
cristyf8561542012-01-24 00:26:46 +0000641 k=0;
cristy3ed852e2009-09-05 21:47:34 +0000642 j=0;
643 count=0;
cristyf8561542012-01-24 00:26:46 +0000644 (void) ResetMagickMemory(histogram,0,NumberPaintBins* sizeof(*histogram));
cristybb503372010-05-27 20:51:26 +0000645 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +0000646 {
cristybb503372010-05-27 20:51:26 +0000647 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +0000648 {
cristyf8561542012-01-24 00:26:46 +0000649 n=(ssize_t) ScaleQuantumToChar(GetPixelIntensity(image,p+
650 GetPixelChannels(image)*(u+k)));
651 histogram[n]++;
652 if (histogram[n] > count)
cristy3ed852e2009-09-05 21:47:34 +0000653 {
cristyf8561542012-01-24 00:26:46 +0000654 j=k+u;
655 count=histogram[n];
cristy3ed852e2009-09-05 21:47:34 +0000656 }
657 }
cristyf8561542012-01-24 00:26:46 +0000658 k+=(ssize_t) (image->columns+width);
cristy3ed852e2009-09-05 21:47:34 +0000659 }
cristy177e41c2012-04-15 15:08:25 +0000660 if (GetPixelMask(image,p) != 0)
661 {
662 p+=GetPixelChannels(image);
663 q+=GetPixelChannels(paint_image);
664 continue;
665 }
cristyf8561542012-01-24 00:26:46 +0000666 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
667 {
668 PixelChannel
669 channel;
670
671 PixelTrait
672 paint_traits,
673 traits;
674
675 channel=GetPixelChannelMapChannel(image,i);
676 traits=GetPixelChannelMapTraits(image,channel);
677 paint_traits=GetPixelChannelMapTraits(paint_image,channel);
678 if ((traits == UndefinedPixelTrait) ||
679 (paint_traits == UndefinedPixelTrait))
680 continue;
cristy177e41c2012-04-15 15:08:25 +0000681 if ((paint_traits & CopyPixelTrait) != 0)
cristyf8561542012-01-24 00:26:46 +0000682 {
683 SetPixelChannel(paint_image,channel,p[center+i],q);
684 continue;
685 }
686 SetPixelChannel(paint_image,channel,p[j*GetPixelChannels(image)+i],q);
687 }
cristyed231572011-07-14 02:18:59 +0000688 p+=GetPixelChannels(image);
689 q+=GetPixelChannels(paint_image);
cristy3ed852e2009-09-05 21:47:34 +0000690 }
691 if (SyncCacheViewAuthenticPixels(paint_view,exception) == MagickFalse)
692 status=MagickFalse;
693 if (image->progress_monitor != (MagickProgressMonitor) NULL)
694 {
695 MagickBooleanType
696 proceed;
697
cristyb5d5f722009-11-04 03:03:49 +0000698#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy95338b32012-01-22 03:34:36 +0000699 #pragma omp critical (MagickCore_OilPaintImage)
cristy3ed852e2009-09-05 21:47:34 +0000700#endif
701 proceed=SetImageProgress(image,OilPaintImageTag,progress++,image->rows);
702 if (proceed == MagickFalse)
703 status=MagickFalse;
704 }
705 }
706 paint_view=DestroyCacheView(paint_view);
707 image_view=DestroyCacheView(image_view);
708 histograms=DestroyHistogramThreadSet(histograms);
709 if (status == MagickFalse)
710 paint_image=DestroyImage(paint_image);
711 return(paint_image);
712}
713
714/*
715%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
716% %
717% %
718% %
719% O p a q u e P a i n t I m a g e %
720% %
721% %
722% %
723%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
724%
725% OpaquePaintImage() changes any pixel that matches color with the color
726% defined by fill.
727%
cristy14973ba2011-08-27 23:48:07 +0000728% By default color must match a particular pixel color exactly. However, in
729% many cases two colors may differ by a small amount. Fuzz defines how much
730% tolerance is acceptable to consider two colors as the same. For example,
731% set fuzz to 10 and the color red at intensities of 100 and 102 respectively
732% are now interpreted as the same color.
cristy3ed852e2009-09-05 21:47:34 +0000733%
734% The format of the OpaquePaintImage method is:
735%
736% MagickBooleanType OpaquePaintImage(Image *image,
cristy101ab702011-10-13 13:06:32 +0000737% const PixelInfo *target,const PixelInfo *fill,
cristy189e84c2011-08-27 18:08:53 +0000738% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000739%
740% A description of each parameter follows:
741%
742% o image: the image.
743%
cristy3ed852e2009-09-05 21:47:34 +0000744% o target: the RGB value of the target color.
745%
746% o fill: the replacement color.
747%
748% o invert: paint any pixel that does not match the target color.
749%
cristy189e84c2011-08-27 18:08:53 +0000750% o exception: return any errors or warnings in this structure.
751%
cristy3ed852e2009-09-05 21:47:34 +0000752*/
cristy3ed852e2009-09-05 21:47:34 +0000753MagickExport MagickBooleanType OpaquePaintImage(Image *image,
cristy189e84c2011-08-27 18:08:53 +0000754 const PixelInfo *target,const PixelInfo *fill,const MagickBooleanType invert,
755 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000756{
757#define OpaquePaintImageTag "Opaque/Image"
758
cristyc4c8d132010-01-07 01:58:38 +0000759 CacheView
760 *image_view;
761
cristy3ed852e2009-09-05 21:47:34 +0000762 MagickBooleanType
763 status;
764
cristybb503372010-05-27 20:51:26 +0000765 MagickOffsetType
766 progress;
767
cristy4c08aed2011-07-01 19:47:50 +0000768 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000769 zero;
770
cristybb503372010-05-27 20:51:26 +0000771 ssize_t
772 y;
773
cristy3ed852e2009-09-05 21:47:34 +0000774 assert(image != (Image *) NULL);
775 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +0000776 assert(target != (PixelInfo *) NULL);
777 assert(fill != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000778 if (image->debug != MagickFalse)
779 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy574cc262011-08-05 01:23:58 +0000780 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000781 return(MagickFalse);
cristy768165d2012-04-09 15:01:35 +0000782 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
783 (IsPixelInfoGray(fill) != MagickFalse))
784 (void) TransformImageColorspace(image,sRGBColorspace,exception);
cristy09250192012-02-07 18:53:31 +0000785 if ((fill->matte != MagickFalse) && (image->matte == MagickFalse))
786 (void) SetImageAlpha(image,OpaqueAlpha,exception);
cristy3ed852e2009-09-05 21:47:34 +0000787 /*
788 Make image color opaque.
789 */
790 status=MagickTrue;
791 progress=0;
cristy4c08aed2011-07-01 19:47:50 +0000792 GetPixelInfo(image,&zero);
cristydb070952012-04-20 14:33:00 +0000793 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +0000794#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000795 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristyddacdd12012-05-07 23:08:14 +0000796 dynamic_number_threads(image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +0000797#endif
cristybb503372010-05-27 20:51:26 +0000798 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000799 {
cristy4c08aed2011-07-01 19:47:50 +0000800 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000801 pixel;
802
cristy4c08aed2011-07-01 19:47:50 +0000803 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000804 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000805
cristy14973ba2011-08-27 23:48:07 +0000806 register ssize_t
807 x;
808
cristy3ed852e2009-09-05 21:47:34 +0000809 if (status == MagickFalse)
810 continue;
811 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy14973ba2011-08-27 23:48:07 +0000812 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000813 {
814 status=MagickFalse;
815 continue;
816 }
cristy3ed852e2009-09-05 21:47:34 +0000817 pixel=zero;
cristybb503372010-05-27 20:51:26 +0000818 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000819 {
cristy803640d2011-11-17 02:11:32 +0000820 GetPixelInfoPixel(image,q,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000821 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
cristy95f562a2012-01-01 20:49:11 +0000822 SetPixelInfoPixel(image,fill,q);
cristyed231572011-07-14 02:18:59 +0000823 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000824 }
825 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
826 status=MagickFalse;
827 if (image->progress_monitor != (MagickProgressMonitor) NULL)
828 {
829 MagickBooleanType
830 proceed;
831
cristyb5d5f722009-11-04 03:03:49 +0000832#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy95338b32012-01-22 03:34:36 +0000833 #pragma omp critical (MagickCore_OpaquePaintImage)
cristy3ed852e2009-09-05 21:47:34 +0000834#endif
835 proceed=SetImageProgress(image,OpaquePaintImageTag,progress++,
836 image->rows);
837 if (proceed == MagickFalse)
838 status=MagickFalse;
839 }
840 }
841 image_view=DestroyCacheView(image_view);
842 return(status);
843}
844
845/*
846%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
847% %
848% %
849% %
850% T r a n s p a r e n t P a i n t I m a g e %
851% %
852% %
853% %
854%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
855%
856% TransparentPaintImage() changes the opacity value associated with any pixel
857% that matches color to the value defined by opacity.
858%
cristy14973ba2011-08-27 23:48:07 +0000859% By default color must match a particular pixel color exactly. However, in
860% many cases two colors may differ by a small amount. Fuzz defines how much
861% tolerance is acceptable to consider two colors as the same. For example,
862% set fuzz to 10 and the color red at intensities of 100 and 102 respectively
863% are now interpreted as the same color.
cristy3ed852e2009-09-05 21:47:34 +0000864%
865% The format of the TransparentPaintImage method is:
866%
867% MagickBooleanType TransparentPaintImage(Image *image,
cristy4c08aed2011-07-01 19:47:50 +0000868% const PixelInfo *target,const Quantum opacity,
cristy189e84c2011-08-27 18:08:53 +0000869% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000870%
871% A description of each parameter follows:
872%
873% o image: the image.
874%
875% o target: the target color.
876%
877% o opacity: the replacement opacity value.
878%
879% o invert: paint any pixel that does not match the target color.
880%
cristy189e84c2011-08-27 18:08:53 +0000881% o exception: return any errors or warnings in this structure.
882%
cristy3ed852e2009-09-05 21:47:34 +0000883*/
884MagickExport MagickBooleanType TransparentPaintImage(Image *image,
cristy14973ba2011-08-27 23:48:07 +0000885 const PixelInfo *target,const Quantum opacity,const MagickBooleanType invert,
886 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000887{
888#define TransparentPaintImageTag "Transparent/Image"
889
cristyc4c8d132010-01-07 01:58:38 +0000890 CacheView
891 *image_view;
892
cristy3ed852e2009-09-05 21:47:34 +0000893 MagickBooleanType
894 status;
895
cristybb503372010-05-27 20:51:26 +0000896 MagickOffsetType
897 progress;
898
cristy4c08aed2011-07-01 19:47:50 +0000899 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000900 zero;
901
cristybb503372010-05-27 20:51:26 +0000902 ssize_t
903 y;
904
cristy3ed852e2009-09-05 21:47:34 +0000905 assert(image != (Image *) NULL);
906 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +0000907 assert(target != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000908 if (image->debug != MagickFalse)
909 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy574cc262011-08-05 01:23:58 +0000910 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000911 return(MagickFalse);
912 if (image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +0000913 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +0000914 /*
915 Make image color transparent.
916 */
917 status=MagickTrue;
918 progress=0;
cristy4c08aed2011-07-01 19:47:50 +0000919 GetPixelInfo(image,&zero);
cristydb070952012-04-20 14:33:00 +0000920 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +0000921#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000922 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristyddacdd12012-05-07 23:08:14 +0000923 dynamic_number_threads(image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +0000924#endif
cristybb503372010-05-27 20:51:26 +0000925 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000926 {
cristy4c08aed2011-07-01 19:47:50 +0000927 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000928 pixel;
929
cristybb503372010-05-27 20:51:26 +0000930 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000931 x;
932
cristy4c08aed2011-07-01 19:47:50 +0000933 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000934 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000935
936 if (status == MagickFalse)
937 continue;
938 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy14973ba2011-08-27 23:48:07 +0000939 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000940 {
941 status=MagickFalse;
942 continue;
943 }
cristy3ed852e2009-09-05 21:47:34 +0000944 pixel=zero;
cristybb503372010-05-27 20:51:26 +0000945 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000946 {
cristy803640d2011-11-17 02:11:32 +0000947 GetPixelInfoPixel(image,q,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000948 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
949 SetPixelAlpha(image,opacity,q);
cristyed231572011-07-14 02:18:59 +0000950 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000951 }
952 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
953 status=MagickFalse;
954 if (image->progress_monitor != (MagickProgressMonitor) NULL)
955 {
956 MagickBooleanType
957 proceed;
958
cristyb5d5f722009-11-04 03:03:49 +0000959#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy95338b32012-01-22 03:34:36 +0000960 #pragma omp critical (MagickCore_TransparentPaintImage)
cristy3ed852e2009-09-05 21:47:34 +0000961#endif
962 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
963 image->rows);
964 if (proceed == MagickFalse)
965 status=MagickFalse;
966 }
967 }
968 image_view=DestroyCacheView(image_view);
969 return(status);
970}
971
972/*
973%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
974% %
975% %
976% %
977% 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 %
978% %
979% %
980% %
981%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
982%
983% TransparentPaintImageChroma() changes the opacity value associated with any
984% pixel that matches color to the value defined by opacity.
985%
cristy14973ba2011-08-27 23:48:07 +0000986% As there is one fuzz value for the all the channels, TransparentPaintImage()
987% is not suitable for the operations like chroma, where the tolerance for
988% similarity of two color component (RGB) can be different. Thus we define
989% this method to take two target pixels (one low and one high) and all the
990% pixels of an image which are lying between these two pixels are made
991% transparent.
cristy3ed852e2009-09-05 21:47:34 +0000992%
cristy14973ba2011-08-27 23:48:07 +0000993% The format of the TransparentPaintImageChroma method is:
cristy3ed852e2009-09-05 21:47:34 +0000994%
cristy14973ba2011-08-27 23:48:07 +0000995% MagickBooleanType TransparentPaintImageChroma(Image *image,
996% const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
997% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000998%
999% A description of each parameter follows:
1000%
1001% o image: the image.
1002%
1003% o low: the low target color.
1004%
1005% o high: the high target color.
1006%
1007% o opacity: the replacement opacity value.
1008%
1009% o invert: paint any pixel that does not match the target color.
1010%
cristy189e84c2011-08-27 18:08:53 +00001011% o exception: return any errors or warnings in this structure.
1012%
cristy3ed852e2009-09-05 21:47:34 +00001013*/
1014MagickExport MagickBooleanType TransparentPaintImageChroma(Image *image,
cristy189e84c2011-08-27 18:08:53 +00001015 const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
1016 const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001017{
1018#define TransparentPaintImageTag "Transparent/Image"
1019
cristyc4c8d132010-01-07 01:58:38 +00001020 CacheView
1021 *image_view;
1022
cristy3ed852e2009-09-05 21:47:34 +00001023 MagickBooleanType
1024 status;
1025
cristybb503372010-05-27 20:51:26 +00001026 MagickOffsetType
1027 progress;
1028
1029 ssize_t
1030 y;
1031
cristy3ed852e2009-09-05 21:47:34 +00001032 assert(image != (Image *) NULL);
1033 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +00001034 assert(high != (PixelInfo *) NULL);
1035 assert(low != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001036 if (image->debug != MagickFalse)
1037 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy574cc262011-08-05 01:23:58 +00001038 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001039 return(MagickFalse);
1040 if (image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00001041 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00001042 /*
1043 Make image color transparent.
1044 */
1045 status=MagickTrue;
1046 progress=0;
cristydb070952012-04-20 14:33:00 +00001047 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00001048#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00001049 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristyddacdd12012-05-07 23:08:14 +00001050 dynamic_number_threads(image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00001051#endif
cristybb503372010-05-27 20:51:26 +00001052 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001053 {
1054 MagickBooleanType
1055 match;
1056
cristy4c08aed2011-07-01 19:47:50 +00001057 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001058 pixel;
1059
cristy4c08aed2011-07-01 19:47:50 +00001060 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001061 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001062
cristy14973ba2011-08-27 23:48:07 +00001063 register ssize_t
1064 x;
1065
cristy3ed852e2009-09-05 21:47:34 +00001066 if (status == MagickFalse)
1067 continue;
1068 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy14973ba2011-08-27 23:48:07 +00001069 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001070 {
1071 status=MagickFalse;
1072 continue;
1073 }
cristy4c08aed2011-07-01 19:47:50 +00001074 GetPixelInfo(image,&pixel);
cristybb503372010-05-27 20:51:26 +00001075 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001076 {
cristy803640d2011-11-17 02:11:32 +00001077 GetPixelInfoPixel(image,q,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001078 match=((pixel.red >= low->red) && (pixel.red <= high->red) &&
1079 (pixel.green >= low->green) && (pixel.green <= high->green) &&
cristy14973ba2011-08-27 23:48:07 +00001080 (pixel.blue >= low->blue) && (pixel.blue <= high->blue)) ? MagickTrue :
1081 MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00001082 if (match != invert)
cristy4c08aed2011-07-01 19:47:50 +00001083 SetPixelAlpha(image,opacity,q);
cristyed231572011-07-14 02:18:59 +00001084 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001085 }
1086 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1087 status=MagickFalse;
1088 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1089 {
1090 MagickBooleanType
1091 proceed;
1092
cristyb5d5f722009-11-04 03:03:49 +00001093#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy95338b32012-01-22 03:34:36 +00001094 #pragma omp critical (MagickCore_TransparentPaintImageChroma)
cristy3ed852e2009-09-05 21:47:34 +00001095#endif
1096 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
1097 image->rows);
1098 if (proceed == MagickFalse)
1099 status=MagickFalse;
1100 }
1101 }
1102 image_view=DestroyCacheView(image_view);
1103 return(status);
1104}