blob: e25342ee484454a414b61ce804c4f8c3fc858543 [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% %
cristy45ef08f2012-12-07 13:13:34 +000020% Copyright 1999-2013 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{
cristyf9e2a082013-01-26 22:16:32 +0000113#define MaxStacksize 131072UL
cristy3ed852e2009-09-05 21:47:34 +0000114#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);
cristya6400b12013-03-15 12:20:18 +0000175 if (IsGrayColorspace(image->colorspace) != MagickFalse)
176 (void) TransformImageColorspace(image,sRGBColorspace,exception);
cristy400275a2013-03-13 00:34:24 +0000177 if ((image->alpha_trait != BlendPixelTrait) &&
178 (draw_info->fill.alpha_trait == BlendPixelTrait))
cristy5b67d4e2012-02-07 19:43:53 +0000179 (void) SetImageAlpha(image,OpaqueAlpha,exception);
cristy3ed852e2009-09-05 21:47:34 +0000180 /*
181 Set floodfill state.
182 */
cristy95f562a2012-01-01 20:49:11 +0000183 floodplane_image=CloneImage(image,image->columns,image->rows,MagickTrue,
184 exception);
cristy3ed852e2009-09-05 21:47:34 +0000185 if (floodplane_image == (Image *) NULL)
186 return(MagickFalse);
cristy8a46d822012-08-28 23:32:39 +0000187 floodplane_image->alpha_trait=UndefinedPixelTrait;
cristy20990102012-05-16 18:10:49 +0000188 floodplane_image->colorspace=GRAYColorspace;
189 (void) QueryColorCompliance("#000",AllCompliance,
190 &floodplane_image->background_color,exception);
191 (void) SetImageBackgroundColor(floodplane_image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000192 segment_stack=(SegmentInfo *) AcquireQuantumMemory(MaxStacksize,
193 sizeof(*segment_stack));
194 if (segment_stack == (SegmentInfo *) NULL)
195 {
196 floodplane_image=DestroyImage(floodplane_image);
197 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
198 image->filename);
199 }
200 /*
201 Push initial segment on stack.
202 */
cristy14973ba2011-08-27 23:48:07 +0000203 status=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +0000204 x=x_offset;
205 y=y_offset;
206 start=0;
207 s=segment_stack;
208 PushSegmentStack(y,x,x,1);
209 PushSegmentStack(y+1,x,x,-1);
cristy4c08aed2011-07-01 19:47:50 +0000210 GetPixelInfo(image,&pixel);
cristy46ff2672012-12-14 15:32:26 +0000211 image_view=AcquireVirtualCacheView(image,exception);
212 floodplane_view=AcquireAuthenticCacheView(floodplane_image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000213 while (s > segment_stack)
214 {
cristy4c08aed2011-07-01 19:47:50 +0000215 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000216 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000217
cristy4c08aed2011-07-01 19:47:50 +0000218 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000219 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000220
cristy14973ba2011-08-27 23:48:07 +0000221 register ssize_t
222 x;
223
cristy3ed852e2009-09-05 21:47:34 +0000224 /*
225 Pop segment off stack.
226 */
227 s--;
cristybb503372010-05-27 20:51:26 +0000228 x1=(ssize_t) s->x1;
229 x2=(ssize_t) s->x2;
230 offset=(ssize_t) s->y2;
231 y=(ssize_t) s->y1+offset;
cristy3ed852e2009-09-05 21:47:34 +0000232 /*
233 Recolor neighboring pixels.
234 */
cristyb0d3bb92010-09-22 14:37:58 +0000235 p=GetCacheViewVirtualPixels(image_view,0,y,(size_t) (x1+1),1,exception);
236 q=GetCacheViewAuthenticPixels(floodplane_view,0,y,(size_t) (x1+1),1,
cristy3ed852e2009-09-05 21:47:34 +0000237 exception);
cristy4c08aed2011-07-01 19:47:50 +0000238 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000239 break;
cristyed231572011-07-14 02:18:59 +0000240 p+=x1*GetPixelChannels(image);
241 q+=x1*GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000242 for (x=x1; x >= 0; x--)
243 {
cristy95f562a2012-01-01 20:49:11 +0000244 if (GetPixelGray(floodplane_image,q) != 0)
cristy3ed852e2009-09-05 21:47:34 +0000245 break;
cristy803640d2011-11-17 02:11:32 +0000246 GetPixelInfoPixel(image,p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000247 if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert)
cristy3ed852e2009-09-05 21:47:34 +0000248 break;
cristy95f562a2012-01-01 20:49:11 +0000249 SetPixelGray(floodplane_image,QuantumRange,q);
cristyed231572011-07-14 02:18:59 +0000250 p-=GetPixelChannels(image);
251 q-=GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000252 }
cristyb0d3bb92010-09-22 14:37:58 +0000253 if (SyncCacheViewAuthenticPixels(floodplane_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000254 break;
255 skip=x >= x1 ? MagickTrue : MagickFalse;
256 if (skip == MagickFalse)
257 {
258 start=x+1;
259 if (start < x1)
260 PushSegmentStack(y,start,x1-1,-offset);
261 x=x1+1;
262 }
263 do
264 {
265 if (skip == MagickFalse)
266 {
cristybb503372010-05-27 20:51:26 +0000267 if (x < (ssize_t) image->columns)
cristy3ed852e2009-09-05 21:47:34 +0000268 {
cristyb0d3bb92010-09-22 14:37:58 +0000269 p=GetCacheViewVirtualPixels(image_view,x,y,image->columns-x,1,
cristy3ed852e2009-09-05 21:47:34 +0000270 exception);
cristyfdcc0182011-12-26 19:33:56 +0000271 q=GetCacheViewAuthenticPixels(floodplane_view,x,y,image->columns-
272 x,1,exception);
cristy636dcb52011-08-26 13:23:49 +0000273 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000274 break;
cristybb503372010-05-27 20:51:26 +0000275 for ( ; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000276 {
cristy95f562a2012-01-01 20:49:11 +0000277 if (GetPixelGray(floodplane_image,q) != 0)
cristy3ed852e2009-09-05 21:47:34 +0000278 break;
cristy803640d2011-11-17 02:11:32 +0000279 GetPixelInfoPixel(image,p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000280 if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert)
cristy3ed852e2009-09-05 21:47:34 +0000281 break;
cristy95f562a2012-01-01 20:49:11 +0000282 SetPixelGray(floodplane_image,QuantumRange,q);
cristyed231572011-07-14 02:18:59 +0000283 p+=GetPixelChannels(image);
284 q+=GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000285 }
cristy14973ba2011-08-27 23:48:07 +0000286 status=SyncCacheViewAuthenticPixels(floodplane_view,exception);
287 if (status == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000288 break;
289 }
290 PushSegmentStack(y,start,x-1,offset);
291 if (x > (x2+1))
292 PushSegmentStack(y,x2+1,x-1,-offset);
293 }
294 skip=MagickFalse;
295 x++;
296 if (x <= x2)
297 {
cristyb0d3bb92010-09-22 14:37:58 +0000298 p=GetCacheViewVirtualPixels(image_view,x,y,(size_t) (x2-x+1),1,
299 exception);
300 q=GetCacheViewAuthenticPixels(floodplane_view,x,y,(size_t) (x2-x+1),1,
cristy3ed852e2009-09-05 21:47:34 +0000301 exception);
cristy636dcb52011-08-26 13:23:49 +0000302 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000303 break;
cristy3ed852e2009-09-05 21:47:34 +0000304 for ( ; x <= x2; x++)
305 {
cristy95f562a2012-01-01 20:49:11 +0000306 if (GetPixelGray(floodplane_image,q) != 0)
cristy3ed852e2009-09-05 21:47:34 +0000307 break;
cristy803640d2011-11-17 02:11:32 +0000308 GetPixelInfoPixel(image,p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000309 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
cristy3ed852e2009-09-05 21:47:34 +0000310 break;
cristyed231572011-07-14 02:18:59 +0000311 p+=GetPixelChannels(image);
312 q+=GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000313 }
314 }
315 start=x;
316 } while (x <= x2);
317 }
cristybb503372010-05-27 20:51:26 +0000318 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000319 {
cristy4c08aed2011-07-01 19:47:50 +0000320 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000321 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000322
cristy4c08aed2011-07-01 19:47:50 +0000323 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000324 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000325
cristy14973ba2011-08-27 23:48:07 +0000326 register ssize_t
327 x;
328
cristy3ed852e2009-09-05 21:47:34 +0000329 /*
330 Tile fill color onto floodplane.
331 */
cristy95f562a2012-01-01 20:49:11 +0000332 p=GetCacheViewVirtualPixels(floodplane_view,0,y,image->columns,1,exception);
cristyb0d3bb92010-09-22 14:37:58 +0000333 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000334 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000335 break;
cristybb503372010-05-27 20:51:26 +0000336 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000337 {
cristy95f562a2012-01-01 20:49:11 +0000338 if (GetPixelGray(floodplane_image,p) != 0)
cristy3ed852e2009-09-05 21:47:34 +0000339 {
cristy2ed42f62011-10-02 19:49:57 +0000340 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
cristy8a20fa02011-12-27 15:54:31 +0000341 SetPixelInfoPixel(image,&fill_color,q);
cristy3ed852e2009-09-05 21:47:34 +0000342 }
cristyed231572011-07-14 02:18:59 +0000343 p+=GetPixelChannels(floodplane_image);
344 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000345 }
cristyb0d3bb92010-09-22 14:37:58 +0000346 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000347 break;
348 }
cristyb0d3bb92010-09-22 14:37:58 +0000349 floodplane_view=DestroyCacheView(floodplane_view);
350 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +0000351 segment_stack=(SegmentInfo *) RelinquishMagickMemory(segment_stack);
352 floodplane_image=DestroyImage(floodplane_image);
cristybb503372010-05-27 20:51:26 +0000353 return(y == (ssize_t) image->rows ? MagickTrue : MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +0000354}
355
356/*
357%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
358% %
359% %
360% %
361+ G r a d i e n t I m a g e %
362% %
363% %
364% %
365%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
366%
cristycee97112010-05-28 00:44:52 +0000367% GradientImage() applies a continuously smooth color transitions along a
cristy3ed852e2009-09-05 21:47:34 +0000368% vector from one color to another.
369%
370% Note, the interface of this method will change in the future to support
371% more than one transistion.
372%
373% The format of the GradientImage method is:
374%
375% MagickBooleanType GradientImage(Image *image,const GradientType type,
cristy101ab702011-10-13 13:06:32 +0000376% const SpreadMethod method,const PixelInfo *start_color,
377% const PixelInfo *stop_color,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000378%
379% A description of each parameter follows:
380%
381% o image: the image.
382%
383% o type: the gradient type: linear or radial.
384%
385% o spread: the gradient spread meathod: pad, reflect, or repeat.
386%
387% o start_color: the start color.
388%
389% o stop_color: the stop color.
390%
cristy189e84c2011-08-27 18:08:53 +0000391% o exception: return any errors or warnings in this structure.
392%
cristy3ed852e2009-09-05 21:47:34 +0000393*/
cristy117ff172010-08-15 21:35:32 +0000394
395static inline double MagickMax(const double x,const double y)
396{
397 return(x > y ? x : y);
398}
399
cristy3ed852e2009-09-05 21:47:34 +0000400MagickExport MagickBooleanType GradientImage(Image *image,
401 const GradientType type,const SpreadMethod method,
cristy101ab702011-10-13 13:06:32 +0000402 const PixelInfo *start_color,const PixelInfo *stop_color,
cristy189e84c2011-08-27 18:08:53 +0000403 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000404{
405 DrawInfo
406 *draw_info;
407
408 GradientInfo
409 *gradient;
410
411 MagickBooleanType
412 status;
413
cristybb503372010-05-27 20:51:26 +0000414 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000415 i;
416
417 /*
418 Set gradient start-stop end points.
419 */
420 assert(image != (const Image *) NULL);
421 assert(image->signature == MagickSignature);
422 if (image->debug != MagickFalse)
423 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy101ab702011-10-13 13:06:32 +0000424 assert(start_color != (const PixelInfo *) NULL);
425 assert(stop_color != (const PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000426 draw_info=AcquireDrawInfo();
427 gradient=(&draw_info->gradient);
428 gradient->type=type;
429 gradient->bounding_box.width=image->columns;
430 gradient->bounding_box.height=image->rows;
431 gradient->gradient_vector.x2=(double) image->columns-1.0;
432 gradient->gradient_vector.y2=(double) image->rows-1.0;
433 if ((type == LinearGradient) && (gradient->gradient_vector.y2 != 0.0))
434 gradient->gradient_vector.x2=0.0;
435 gradient->center.x=(double) gradient->gradient_vector.x2/2.0;
436 gradient->center.y=(double) gradient->gradient_vector.y2/2.0;
437 gradient->radius=MagickMax(gradient->center.x,gradient->center.y);
438 gradient->spread=method;
439 /*
440 Define the gradient to fill between the stops.
441 */
442 gradient->number_stops=2;
443 gradient->stops=(StopInfo *) AcquireQuantumMemory(gradient->number_stops,
444 sizeof(*gradient->stops));
445 if (gradient->stops == (StopInfo *) NULL)
446 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
447 image->filename);
448 (void) ResetMagickMemory(gradient->stops,0,gradient->number_stops*
449 sizeof(*gradient->stops));
cristybb503372010-05-27 20:51:26 +0000450 for (i=0; i < (ssize_t) gradient->number_stops; i++)
cristy4c08aed2011-07-01 19:47:50 +0000451 GetPixelInfo(image,&gradient->stops[i].color);
cristy9d8c8ce2011-10-25 16:13:52 +0000452 gradient->stops[0].color=(*start_color);
cristy3ed852e2009-09-05 21:47:34 +0000453 gradient->stops[0].offset=0.0;
cristy9d8c8ce2011-10-25 16:13:52 +0000454 gradient->stops[1].color=(*stop_color);
cristy3ed852e2009-09-05 21:47:34 +0000455 gradient->stops[1].offset=1.0;
456 /*
457 Draw a gradient on the image.
458 */
cristybafdbba2012-06-20 11:50:57 +0000459 (void) SetImageColorspace(image,start_color->colorspace,exception);
cristy947cb4c2011-10-20 18:41:46 +0000460 status=DrawGradientImage(image,draw_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000461 draw_info=DestroyDrawInfo(draw_info);
cristy3ed852e2009-09-05 21:47:34 +0000462 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
cristy9357bdd2012-07-30 12:28:34 +0000519 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
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
cristy404d0da2012-12-20 13:04:30 +0000544 *linear_image,
cristy3ed852e2009-09-05 21:47:34 +0000545 *paint_image;
546
cristy3ed852e2009-09-05 21:47:34 +0000547 MagickBooleanType
548 status;
549
cristybb503372010-05-27 20:51:26 +0000550 MagickOffsetType
551 progress;
552
553 size_t
cristy14973ba2011-08-27 23:48:07 +0000554 **histograms,
cristy3ed852e2009-09-05 21:47:34 +0000555 width;
556
cristybb503372010-05-27 20:51:26 +0000557 ssize_t
cristyf8561542012-01-24 00:26:46 +0000558 center,
cristybb503372010-05-27 20:51:26 +0000559 y;
560
cristy3ed852e2009-09-05 21:47:34 +0000561 /*
562 Initialize painted image attributes.
563 */
564 assert(image != (const Image *) NULL);
565 assert(image->signature == MagickSignature);
566 if (image->debug != MagickFalse)
567 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
568 assert(exception != (ExceptionInfo *) NULL);
569 assert(exception->signature == MagickSignature);
cristy14973ba2011-08-27 23:48:07 +0000570 width=GetOptimalKernelWidth2D(radius,sigma);
cristy404d0da2012-12-20 13:04:30 +0000571 linear_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000572 paint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy404d0da2012-12-20 13:04:30 +0000573 if ((linear_image == (Image *) NULL) || (paint_image == (Image *) NULL))
574 {
575 if (linear_image != (Image *) NULL)
576 linear_image=DestroyImage(linear_image);
577 if (paint_image != (Image *) NULL)
578 linear_image=DestroyImage(paint_image);
579 return((Image *) NULL);
580 }
cristy574cc262011-08-05 01:23:58 +0000581 if (SetImageStorageClass(paint_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000582 {
cristy404d0da2012-12-20 13:04:30 +0000583 linear_image=DestroyImage(linear_image);
cristy3ed852e2009-09-05 21:47:34 +0000584 paint_image=DestroyImage(paint_image);
585 return((Image *) NULL);
586 }
587 histograms=AcquireHistogramThreadSet(NumberPaintBins);
cristybb503372010-05-27 20:51:26 +0000588 if (histograms == (size_t **) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000589 {
cristy404d0da2012-12-20 13:04:30 +0000590 linear_image=DestroyImage(linear_image);
cristy3ed852e2009-09-05 21:47:34 +0000591 paint_image=DestroyImage(paint_image);
592 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
593 }
594 /*
595 Oil paint image.
596 */
597 status=MagickTrue;
598 progress=0;
cristy404d0da2012-12-20 13:04:30 +0000599 center=(ssize_t) GetPixelChannels(linear_image)*(linear_image->columns+width)*
600 (width/2L)+GetPixelChannels(linear_image)*(width/2L);
601 image_view=AcquireVirtualCacheView(linear_image,exception);
cristy46ff2672012-12-14 15:32:26 +0000602 paint_view=AcquireAuthenticCacheView(paint_image,exception);
cristyb5d5f722009-11-04 03:03:49 +0000603#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000604 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy404d0da2012-12-20 13:04:30 +0000605 magick_threads(linear_image,paint_image,linear_image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +0000606#endif
cristy404d0da2012-12-20 13:04:30 +0000607 for (y=0; y < (ssize_t) linear_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000608 {
cristy4c08aed2011-07-01 19:47:50 +0000609 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000610 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000611
cristy4c08aed2011-07-01 19:47:50 +0000612 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000613 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000614
cristybb503372010-05-27 20:51:26 +0000615 register size_t
cristy3ed852e2009-09-05 21:47:34 +0000616 *histogram;
617
cristy14973ba2011-08-27 23:48:07 +0000618 register ssize_t
619 x;
620
cristy3ed852e2009-09-05 21:47:34 +0000621 if (status == MagickFalse)
622 continue;
cristyfe4ba002011-02-28 14:54:12 +0000623 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
cristy404d0da2012-12-20 13:04:30 +0000624 (width/2L),linear_image->columns+width,width,exception);
cristy3ed852e2009-09-05 21:47:34 +0000625 q=QueueCacheViewAuthenticPixels(paint_view,0,y,paint_image->columns,1,
626 exception);
cristy4c08aed2011-07-01 19:47:50 +0000627 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000628 {
629 status=MagickFalse;
630 continue;
631 }
cristy3ed852e2009-09-05 21:47:34 +0000632 histogram=histograms[GetOpenMPThreadId()];
cristy404d0da2012-12-20 13:04:30 +0000633 for (x=0; x < (ssize_t) linear_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000634 {
cristybb503372010-05-27 20:51:26 +0000635 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000636 i,
637 u;
638
cristybb503372010-05-27 20:51:26 +0000639 size_t
cristy3ed852e2009-09-05 21:47:34 +0000640 count;
641
cristy9d314ff2011-03-09 01:30:28 +0000642 ssize_t
643 j,
644 k,
cristyf8561542012-01-24 00:26:46 +0000645 n,
cristy9d314ff2011-03-09 01:30:28 +0000646 v;
647
cristy3ed852e2009-09-05 21:47:34 +0000648 /*
649 Assign most frequent color.
650 */
cristyf8561542012-01-24 00:26:46 +0000651 k=0;
cristy3ed852e2009-09-05 21:47:34 +0000652 j=0;
653 count=0;
cristyf8561542012-01-24 00:26:46 +0000654 (void) ResetMagickMemory(histogram,0,NumberPaintBins* sizeof(*histogram));
cristybb503372010-05-27 20:51:26 +0000655 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +0000656 {
cristybb503372010-05-27 20:51:26 +0000657 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +0000658 {
cristy404d0da2012-12-20 13:04:30 +0000659 n=(ssize_t) ScaleQuantumToChar(ClampToQuantum(GetPixelIntensity(
660 linear_image,p+GetPixelChannels(linear_image)*(u+k))));
cristyf8561542012-01-24 00:26:46 +0000661 histogram[n]++;
662 if (histogram[n] > count)
cristy3ed852e2009-09-05 21:47:34 +0000663 {
cristyf8561542012-01-24 00:26:46 +0000664 j=k+u;
665 count=histogram[n];
cristy3ed852e2009-09-05 21:47:34 +0000666 }
667 }
cristy034ade92013-03-07 12:18:19 +0000668 k+=(ssize_t) (linear_image->columns+width);
cristy3ed852e2009-09-05 21:47:34 +0000669 }
cristy404d0da2012-12-20 13:04:30 +0000670 for (i=0; i < (ssize_t) GetPixelChannels(linear_image); i++)
cristyf8561542012-01-24 00:26:46 +0000671 {
cristy5a23c552013-02-13 14:34:28 +0000672 PixelChannel channel=GetPixelChannelChannel(linear_image,i);
673 PixelTrait traits=GetPixelChannelTraits(linear_image,channel);
674 PixelTrait paint_traits=GetPixelChannelTraits(paint_image,channel);
cristyf8561542012-01-24 00:26:46 +0000675 if ((traits == UndefinedPixelTrait) ||
676 (paint_traits == UndefinedPixelTrait))
677 continue;
cristy1eced092012-08-10 23:10:56 +0000678 if (((paint_traits & CopyPixelTrait) != 0) ||
cristy404d0da2012-12-20 13:04:30 +0000679 (GetPixelMask(linear_image,p) != 0))
cristyf8561542012-01-24 00:26:46 +0000680 {
681 SetPixelChannel(paint_image,channel,p[center+i],q);
682 continue;
683 }
cristy404d0da2012-12-20 13:04:30 +0000684 SetPixelChannel(paint_image,channel,p[j*GetPixelChannels(linear_image)+
685 i],q);
cristyf8561542012-01-24 00:26:46 +0000686 }
cristy404d0da2012-12-20 13:04:30 +0000687 p+=GetPixelChannels(linear_image);
cristyed231572011-07-14 02:18:59 +0000688 q+=GetPixelChannels(paint_image);
cristy3ed852e2009-09-05 21:47:34 +0000689 }
690 if (SyncCacheViewAuthenticPixels(paint_view,exception) == MagickFalse)
691 status=MagickFalse;
cristy404d0da2012-12-20 13:04:30 +0000692 if (linear_image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000693 {
694 MagickBooleanType
695 proceed;
696
cristyb5d5f722009-11-04 03:03:49 +0000697#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy95338b32012-01-22 03:34:36 +0000698 #pragma omp critical (MagickCore_OilPaintImage)
cristy3ed852e2009-09-05 21:47:34 +0000699#endif
cristy404d0da2012-12-20 13:04:30 +0000700 proceed=SetImageProgress(linear_image,OilPaintImageTag,progress++,
701 linear_image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000702 if (proceed == MagickFalse)
703 status=MagickFalse;
704 }
705 }
706 paint_view=DestroyCacheView(paint_view);
707 image_view=DestroyCacheView(image_view);
708 histograms=DestroyHistogramThreadSet(histograms);
cristy404d0da2012-12-20 13:04:30 +0000709 linear_image=DestroyImage(linear_image);
cristy3ed852e2009-09-05 21:47:34 +0000710 if (status == MagickFalse)
711 paint_image=DestroyImage(paint_image);
712 return(paint_image);
713}
714
715/*
716%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
717% %
718% %
719% %
720% O p a q u e P a i n t I m a g e %
721% %
722% %
723% %
724%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
725%
726% OpaquePaintImage() changes any pixel that matches color with the color
727% defined by fill.
728%
cristy14973ba2011-08-27 23:48:07 +0000729% By default color must match a particular pixel color exactly. However, in
730% many cases two colors may differ by a small amount. Fuzz defines how much
731% tolerance is acceptable to consider two colors as the same. For example,
732% set fuzz to 10 and the color red at intensities of 100 and 102 respectively
733% are now interpreted as the same color.
cristy3ed852e2009-09-05 21:47:34 +0000734%
735% The format of the OpaquePaintImage method is:
736%
737% MagickBooleanType OpaquePaintImage(Image *image,
cristy101ab702011-10-13 13:06:32 +0000738% const PixelInfo *target,const PixelInfo *fill,
cristy189e84c2011-08-27 18:08:53 +0000739% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000740%
741% A description of each parameter follows:
742%
743% o image: the image.
744%
cristy3ed852e2009-09-05 21:47:34 +0000745% o target: the RGB value of the target color.
746%
747% o fill: the replacement color.
748%
749% o invert: paint any pixel that does not match the target color.
750%
cristy189e84c2011-08-27 18:08:53 +0000751% o exception: return any errors or warnings in this structure.
752%
cristy3ed852e2009-09-05 21:47:34 +0000753*/
cristy3ed852e2009-09-05 21:47:34 +0000754MagickExport MagickBooleanType OpaquePaintImage(Image *image,
cristy189e84c2011-08-27 18:08:53 +0000755 const PixelInfo *target,const PixelInfo *fill,const MagickBooleanType invert,
756 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000757{
758#define OpaquePaintImageTag "Opaque/Image"
759
cristyc4c8d132010-01-07 01:58:38 +0000760 CacheView
761 *image_view;
762
cristy3ed852e2009-09-05 21:47:34 +0000763 MagickBooleanType
764 status;
765
cristybb503372010-05-27 20:51:26 +0000766 MagickOffsetType
767 progress;
768
cristy4c08aed2011-07-01 19:47:50 +0000769 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000770 zero;
771
cristybb503372010-05-27 20:51:26 +0000772 ssize_t
773 y;
774
cristy3ed852e2009-09-05 21:47:34 +0000775 assert(image != (Image *) NULL);
776 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +0000777 assert(target != (PixelInfo *) NULL);
778 assert(fill != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000779 if (image->debug != MagickFalse)
780 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy574cc262011-08-05 01:23:58 +0000781 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000782 return(MagickFalse);
cristya6400b12013-03-15 12:20:18 +0000783 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
784 (IsPixelInfoGray(fill) == MagickFalse))
785 (void) TransformImageColorspace(image,sRGBColorspace,exception);
cristy400275a2013-03-13 00:34:24 +0000786 if ((fill->alpha_trait == BlendPixelTrait) &&
787 (image->alpha_trait != BlendPixelTrait))
cristy09250192012-02-07 18:53:31 +0000788 (void) SetImageAlpha(image,OpaqueAlpha,exception);
cristy3ed852e2009-09-05 21:47:34 +0000789 /*
790 Make image color opaque.
791 */
792 status=MagickTrue;
793 progress=0;
cristy4c08aed2011-07-01 19:47:50 +0000794 GetPixelInfo(image,&zero);
cristy46ff2672012-12-14 15:32:26 +0000795 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +0000796#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000797 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +0000798 magick_threads(image,image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +0000799#endif
cristybb503372010-05-27 20:51:26 +0000800 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000801 {
cristy4c08aed2011-07-01 19:47:50 +0000802 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000803 pixel;
804
cristy4c08aed2011-07-01 19:47:50 +0000805 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000806 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000807
cristy14973ba2011-08-27 23:48:07 +0000808 register ssize_t
809 x;
810
cristy3ed852e2009-09-05 21:47:34 +0000811 if (status == MagickFalse)
812 continue;
813 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy14973ba2011-08-27 23:48:07 +0000814 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000815 {
816 status=MagickFalse;
817 continue;
818 }
cristy3ed852e2009-09-05 21:47:34 +0000819 pixel=zero;
cristybb503372010-05-27 20:51:26 +0000820 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000821 {
cristy803640d2011-11-17 02:11:32 +0000822 GetPixelInfoPixel(image,q,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000823 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
cristy95f562a2012-01-01 20:49:11 +0000824 SetPixelInfoPixel(image,fill,q);
cristyed231572011-07-14 02:18:59 +0000825 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000826 }
827 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
828 status=MagickFalse;
829 if (image->progress_monitor != (MagickProgressMonitor) NULL)
830 {
831 MagickBooleanType
832 proceed;
833
cristyb5d5f722009-11-04 03:03:49 +0000834#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy95338b32012-01-22 03:34:36 +0000835 #pragma omp critical (MagickCore_OpaquePaintImage)
cristy3ed852e2009-09-05 21:47:34 +0000836#endif
837 proceed=SetImageProgress(image,OpaquePaintImageTag,progress++,
838 image->rows);
839 if (proceed == MagickFalse)
840 status=MagickFalse;
841 }
842 }
843 image_view=DestroyCacheView(image_view);
844 return(status);
845}
846
847/*
848%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
849% %
850% %
851% %
852% T r a n s p a r e n t P a i n t I m a g e %
853% %
854% %
855% %
856%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
857%
858% TransparentPaintImage() changes the opacity value associated with any pixel
859% that matches color to the value defined by opacity.
860%
cristy14973ba2011-08-27 23:48:07 +0000861% By default color must match a particular pixel color exactly. However, in
862% many cases two colors may differ by a small amount. Fuzz defines how much
863% tolerance is acceptable to consider two colors as the same. For example,
864% set fuzz to 10 and the color red at intensities of 100 and 102 respectively
865% are now interpreted as the same color.
cristy3ed852e2009-09-05 21:47:34 +0000866%
867% The format of the TransparentPaintImage method is:
868%
869% MagickBooleanType TransparentPaintImage(Image *image,
cristy4c08aed2011-07-01 19:47:50 +0000870% const PixelInfo *target,const Quantum opacity,
cristy189e84c2011-08-27 18:08:53 +0000871% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000872%
873% A description of each parameter follows:
874%
875% o image: the image.
876%
877% o target: the target color.
878%
879% o opacity: the replacement opacity value.
880%
881% o invert: paint any pixel that does not match the target color.
882%
cristy189e84c2011-08-27 18:08:53 +0000883% o exception: return any errors or warnings in this structure.
884%
cristy3ed852e2009-09-05 21:47:34 +0000885*/
886MagickExport MagickBooleanType TransparentPaintImage(Image *image,
cristy14973ba2011-08-27 23:48:07 +0000887 const PixelInfo *target,const Quantum opacity,const MagickBooleanType invert,
888 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000889{
890#define TransparentPaintImageTag "Transparent/Image"
891
cristyc4c8d132010-01-07 01:58:38 +0000892 CacheView
893 *image_view;
894
cristy3ed852e2009-09-05 21:47:34 +0000895 MagickBooleanType
896 status;
897
cristybb503372010-05-27 20:51:26 +0000898 MagickOffsetType
899 progress;
900
cristy4c08aed2011-07-01 19:47:50 +0000901 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000902 zero;
903
cristybb503372010-05-27 20:51:26 +0000904 ssize_t
905 y;
906
cristy3ed852e2009-09-05 21:47:34 +0000907 assert(image != (Image *) NULL);
908 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +0000909 assert(target != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000910 if (image->debug != MagickFalse)
911 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy574cc262011-08-05 01:23:58 +0000912 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000913 return(MagickFalse);
cristy8a46d822012-08-28 23:32:39 +0000914 if (image->alpha_trait != BlendPixelTrait)
cristy63240882011-08-05 19:05:27 +0000915 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +0000916 /*
917 Make image color transparent.
918 */
919 status=MagickTrue;
920 progress=0;
cristy4c08aed2011-07-01 19:47:50 +0000921 GetPixelInfo(image,&zero);
cristy46ff2672012-12-14 15:32:26 +0000922 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +0000923#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000924 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +0000925 magick_threads(image,image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +0000926#endif
cristybb503372010-05-27 20:51:26 +0000927 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000928 {
cristy4c08aed2011-07-01 19:47:50 +0000929 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000930 pixel;
931
cristybb503372010-05-27 20:51:26 +0000932 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000933 x;
934
cristy4c08aed2011-07-01 19:47:50 +0000935 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000936 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000937
938 if (status == MagickFalse)
939 continue;
940 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy14973ba2011-08-27 23:48:07 +0000941 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000942 {
943 status=MagickFalse;
944 continue;
945 }
cristy3ed852e2009-09-05 21:47:34 +0000946 pixel=zero;
cristybb503372010-05-27 20:51:26 +0000947 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000948 {
cristy803640d2011-11-17 02:11:32 +0000949 GetPixelInfoPixel(image,q,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000950 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
951 SetPixelAlpha(image,opacity,q);
cristyed231572011-07-14 02:18:59 +0000952 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000953 }
954 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
955 status=MagickFalse;
956 if (image->progress_monitor != (MagickProgressMonitor) NULL)
957 {
958 MagickBooleanType
959 proceed;
960
cristyb5d5f722009-11-04 03:03:49 +0000961#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy95338b32012-01-22 03:34:36 +0000962 #pragma omp critical (MagickCore_TransparentPaintImage)
cristy3ed852e2009-09-05 21:47:34 +0000963#endif
964 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
965 image->rows);
966 if (proceed == MagickFalse)
967 status=MagickFalse;
968 }
969 }
970 image_view=DestroyCacheView(image_view);
971 return(status);
972}
973
974/*
975%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
976% %
977% %
978% %
979% 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 %
980% %
981% %
982% %
983%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
984%
985% TransparentPaintImageChroma() changes the opacity value associated with any
986% pixel that matches color to the value defined by opacity.
987%
cristy14973ba2011-08-27 23:48:07 +0000988% As there is one fuzz value for the all the channels, TransparentPaintImage()
989% is not suitable for the operations like chroma, where the tolerance for
990% similarity of two color component (RGB) can be different. Thus we define
991% this method to take two target pixels (one low and one high) and all the
992% pixels of an image which are lying between these two pixels are made
993% transparent.
cristy3ed852e2009-09-05 21:47:34 +0000994%
cristy14973ba2011-08-27 23:48:07 +0000995% The format of the TransparentPaintImageChroma method is:
cristy3ed852e2009-09-05 21:47:34 +0000996%
cristy14973ba2011-08-27 23:48:07 +0000997% MagickBooleanType TransparentPaintImageChroma(Image *image,
998% const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
999% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001000%
1001% A description of each parameter follows:
1002%
1003% o image: the image.
1004%
1005% o low: the low target color.
1006%
1007% o high: the high target color.
1008%
1009% o opacity: the replacement opacity value.
1010%
1011% o invert: paint any pixel that does not match the target color.
1012%
cristy189e84c2011-08-27 18:08:53 +00001013% o exception: return any errors or warnings in this structure.
1014%
cristy3ed852e2009-09-05 21:47:34 +00001015*/
1016MagickExport MagickBooleanType TransparentPaintImageChroma(Image *image,
cristy189e84c2011-08-27 18:08:53 +00001017 const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
1018 const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001019{
1020#define TransparentPaintImageTag "Transparent/Image"
1021
cristyc4c8d132010-01-07 01:58:38 +00001022 CacheView
1023 *image_view;
1024
cristy3ed852e2009-09-05 21:47:34 +00001025 MagickBooleanType
1026 status;
1027
cristybb503372010-05-27 20:51:26 +00001028 MagickOffsetType
1029 progress;
1030
1031 ssize_t
1032 y;
1033
cristy3ed852e2009-09-05 21:47:34 +00001034 assert(image != (Image *) NULL);
1035 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +00001036 assert(high != (PixelInfo *) NULL);
1037 assert(low != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001038 if (image->debug != MagickFalse)
1039 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy574cc262011-08-05 01:23:58 +00001040 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001041 return(MagickFalse);
cristy8a46d822012-08-28 23:32:39 +00001042 if (image->alpha_trait != BlendPixelTrait)
cristy63240882011-08-05 19:05:27 +00001043 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00001044 /*
1045 Make image color transparent.
1046 */
1047 status=MagickTrue;
1048 progress=0;
cristy46ff2672012-12-14 15:32:26 +00001049 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00001050#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00001051 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00001052 magick_threads(image,image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00001053#endif
cristybb503372010-05-27 20:51:26 +00001054 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001055 {
1056 MagickBooleanType
1057 match;
1058
cristy4c08aed2011-07-01 19:47:50 +00001059 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001060 pixel;
1061
cristy4c08aed2011-07-01 19:47:50 +00001062 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001063 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001064
cristy14973ba2011-08-27 23:48:07 +00001065 register ssize_t
1066 x;
1067
cristy3ed852e2009-09-05 21:47:34 +00001068 if (status == MagickFalse)
1069 continue;
1070 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy14973ba2011-08-27 23:48:07 +00001071 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001072 {
1073 status=MagickFalse;
1074 continue;
1075 }
cristy4c08aed2011-07-01 19:47:50 +00001076 GetPixelInfo(image,&pixel);
cristybb503372010-05-27 20:51:26 +00001077 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001078 {
cristy803640d2011-11-17 02:11:32 +00001079 GetPixelInfoPixel(image,q,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001080 match=((pixel.red >= low->red) && (pixel.red <= high->red) &&
1081 (pixel.green >= low->green) && (pixel.green <= high->green) &&
cristy14973ba2011-08-27 23:48:07 +00001082 (pixel.blue >= low->blue) && (pixel.blue <= high->blue)) ? MagickTrue :
1083 MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00001084 if (match != invert)
cristy4c08aed2011-07-01 19:47:50 +00001085 SetPixelAlpha(image,opacity,q);
cristyed231572011-07-14 02:18:59 +00001086 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001087 }
1088 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1089 status=MagickFalse;
1090 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1091 {
1092 MagickBooleanType
1093 proceed;
1094
cristyb5d5f722009-11-04 03:03:49 +00001095#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy95338b32012-01-22 03:34:36 +00001096 #pragma omp critical (MagickCore_TransparentPaintImageChroma)
cristy3ed852e2009-09-05 21:47:34 +00001097#endif
1098 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
1099 image->rows);
1100 if (proceed == MagickFalse)
1101 status=MagickFalse;
1102 }
1103 }
1104 image_view=DestroyCacheView(image_view);
1105 return(status);
1106}