blob: d22a7402e14b31ff95efc31a72061dc8a4dd3655 [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);
cristy400275a2013-03-13 00:34:24 +0000175 if ((image->alpha_trait != BlendPixelTrait) &&
176 (draw_info->fill.alpha_trait == BlendPixelTrait))
cristy5b67d4e2012-02-07 19:43:53 +0000177 (void) SetImageAlpha(image,OpaqueAlpha,exception);
cristy3ed852e2009-09-05 21:47:34 +0000178 /*
179 Set floodfill state.
180 */
cristy95f562a2012-01-01 20:49:11 +0000181 floodplane_image=CloneImage(image,image->columns,image->rows,MagickTrue,
182 exception);
cristy3ed852e2009-09-05 21:47:34 +0000183 if (floodplane_image == (Image *) NULL)
184 return(MagickFalse);
cristy8a46d822012-08-28 23:32:39 +0000185 floodplane_image->alpha_trait=UndefinedPixelTrait;
cristy20990102012-05-16 18:10:49 +0000186 floodplane_image->colorspace=GRAYColorspace;
187 (void) QueryColorCompliance("#000",AllCompliance,
188 &floodplane_image->background_color,exception);
189 (void) SetImageBackgroundColor(floodplane_image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000190 segment_stack=(SegmentInfo *) AcquireQuantumMemory(MaxStacksize,
191 sizeof(*segment_stack));
192 if (segment_stack == (SegmentInfo *) NULL)
193 {
194 floodplane_image=DestroyImage(floodplane_image);
195 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
196 image->filename);
197 }
198 /*
199 Push initial segment on stack.
200 */
cristy14973ba2011-08-27 23:48:07 +0000201 status=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +0000202 x=x_offset;
203 y=y_offset;
204 start=0;
205 s=segment_stack;
206 PushSegmentStack(y,x,x,1);
207 PushSegmentStack(y+1,x,x,-1);
cristy4c08aed2011-07-01 19:47:50 +0000208 GetPixelInfo(image,&pixel);
cristy46ff2672012-12-14 15:32:26 +0000209 image_view=AcquireVirtualCacheView(image,exception);
210 floodplane_view=AcquireAuthenticCacheView(floodplane_image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000211 while (s > segment_stack)
212 {
cristy4c08aed2011-07-01 19:47:50 +0000213 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000214 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000215
cristy4c08aed2011-07-01 19:47:50 +0000216 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000217 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000218
cristy14973ba2011-08-27 23:48:07 +0000219 register ssize_t
220 x;
221
cristy3ed852e2009-09-05 21:47:34 +0000222 /*
223 Pop segment off stack.
224 */
225 s--;
cristybb503372010-05-27 20:51:26 +0000226 x1=(ssize_t) s->x1;
227 x2=(ssize_t) s->x2;
228 offset=(ssize_t) s->y2;
229 y=(ssize_t) s->y1+offset;
cristy3ed852e2009-09-05 21:47:34 +0000230 /*
231 Recolor neighboring pixels.
232 */
cristyb0d3bb92010-09-22 14:37:58 +0000233 p=GetCacheViewVirtualPixels(image_view,0,y,(size_t) (x1+1),1,exception);
234 q=GetCacheViewAuthenticPixels(floodplane_view,0,y,(size_t) (x1+1),1,
cristy3ed852e2009-09-05 21:47:34 +0000235 exception);
cristy4c08aed2011-07-01 19:47:50 +0000236 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000237 break;
cristyed231572011-07-14 02:18:59 +0000238 p+=x1*GetPixelChannels(image);
239 q+=x1*GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000240 for (x=x1; x >= 0; x--)
241 {
cristy95f562a2012-01-01 20:49:11 +0000242 if (GetPixelGray(floodplane_image,q) != 0)
cristy3ed852e2009-09-05 21:47:34 +0000243 break;
cristy803640d2011-11-17 02:11:32 +0000244 GetPixelInfoPixel(image,p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000245 if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert)
cristy3ed852e2009-09-05 21:47:34 +0000246 break;
cristy95f562a2012-01-01 20:49:11 +0000247 SetPixelGray(floodplane_image,QuantumRange,q);
cristyed231572011-07-14 02:18:59 +0000248 p-=GetPixelChannels(image);
249 q-=GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000250 }
cristyb0d3bb92010-09-22 14:37:58 +0000251 if (SyncCacheViewAuthenticPixels(floodplane_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000252 break;
253 skip=x >= x1 ? MagickTrue : MagickFalse;
254 if (skip == MagickFalse)
255 {
256 start=x+1;
257 if (start < x1)
258 PushSegmentStack(y,start,x1-1,-offset);
259 x=x1+1;
260 }
261 do
262 {
263 if (skip == MagickFalse)
264 {
cristybb503372010-05-27 20:51:26 +0000265 if (x < (ssize_t) image->columns)
cristy3ed852e2009-09-05 21:47:34 +0000266 {
cristyb0d3bb92010-09-22 14:37:58 +0000267 p=GetCacheViewVirtualPixels(image_view,x,y,image->columns-x,1,
cristy3ed852e2009-09-05 21:47:34 +0000268 exception);
cristyfdcc0182011-12-26 19:33:56 +0000269 q=GetCacheViewAuthenticPixels(floodplane_view,x,y,image->columns-
270 x,1,exception);
cristy636dcb52011-08-26 13:23:49 +0000271 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000272 break;
cristybb503372010-05-27 20:51:26 +0000273 for ( ; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000274 {
cristy95f562a2012-01-01 20:49:11 +0000275 if (GetPixelGray(floodplane_image,q) != 0)
cristy3ed852e2009-09-05 21:47:34 +0000276 break;
cristy803640d2011-11-17 02:11:32 +0000277 GetPixelInfoPixel(image,p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000278 if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert)
cristy3ed852e2009-09-05 21:47:34 +0000279 break;
cristy95f562a2012-01-01 20:49:11 +0000280 SetPixelGray(floodplane_image,QuantumRange,q);
cristyed231572011-07-14 02:18:59 +0000281 p+=GetPixelChannels(image);
282 q+=GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000283 }
cristy14973ba2011-08-27 23:48:07 +0000284 status=SyncCacheViewAuthenticPixels(floodplane_view,exception);
285 if (status == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000286 break;
287 }
288 PushSegmentStack(y,start,x-1,offset);
289 if (x > (x2+1))
290 PushSegmentStack(y,x2+1,x-1,-offset);
291 }
292 skip=MagickFalse;
293 x++;
294 if (x <= x2)
295 {
cristyb0d3bb92010-09-22 14:37:58 +0000296 p=GetCacheViewVirtualPixels(image_view,x,y,(size_t) (x2-x+1),1,
297 exception);
298 q=GetCacheViewAuthenticPixels(floodplane_view,x,y,(size_t) (x2-x+1),1,
cristy3ed852e2009-09-05 21:47:34 +0000299 exception);
cristy636dcb52011-08-26 13:23:49 +0000300 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000301 break;
cristy3ed852e2009-09-05 21:47:34 +0000302 for ( ; x <= x2; x++)
303 {
cristy95f562a2012-01-01 20:49:11 +0000304 if (GetPixelGray(floodplane_image,q) != 0)
cristy3ed852e2009-09-05 21:47:34 +0000305 break;
cristy803640d2011-11-17 02:11:32 +0000306 GetPixelInfoPixel(image,p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000307 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
cristy3ed852e2009-09-05 21:47:34 +0000308 break;
cristyed231572011-07-14 02:18:59 +0000309 p+=GetPixelChannels(image);
310 q+=GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000311 }
312 }
313 start=x;
314 } while (x <= x2);
315 }
cristybb503372010-05-27 20:51:26 +0000316 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000317 {
cristy4c08aed2011-07-01 19:47:50 +0000318 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000319 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000320
cristy4c08aed2011-07-01 19:47:50 +0000321 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000322 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000323
cristy14973ba2011-08-27 23:48:07 +0000324 register ssize_t
325 x;
326
cristy3ed852e2009-09-05 21:47:34 +0000327 /*
328 Tile fill color onto floodplane.
329 */
cristy95f562a2012-01-01 20:49:11 +0000330 p=GetCacheViewVirtualPixels(floodplane_view,0,y,image->columns,1,exception);
cristyb0d3bb92010-09-22 14:37:58 +0000331 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000332 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000333 break;
cristybb503372010-05-27 20:51:26 +0000334 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000335 {
cristy95f562a2012-01-01 20:49:11 +0000336 if (GetPixelGray(floodplane_image,p) != 0)
cristy3ed852e2009-09-05 21:47:34 +0000337 {
cristy2ed42f62011-10-02 19:49:57 +0000338 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
cristy8a20fa02011-12-27 15:54:31 +0000339 SetPixelInfoPixel(image,&fill_color,q);
cristy3ed852e2009-09-05 21:47:34 +0000340 }
cristyed231572011-07-14 02:18:59 +0000341 p+=GetPixelChannels(floodplane_image);
342 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000343 }
cristyb0d3bb92010-09-22 14:37:58 +0000344 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000345 break;
346 }
cristyb0d3bb92010-09-22 14:37:58 +0000347 floodplane_view=DestroyCacheView(floodplane_view);
348 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +0000349 segment_stack=(SegmentInfo *) RelinquishMagickMemory(segment_stack);
350 floodplane_image=DestroyImage(floodplane_image);
cristybb503372010-05-27 20:51:26 +0000351 return(y == (ssize_t) image->rows ? MagickTrue : MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +0000352}
353
354/*
355%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
356% %
357% %
358% %
359+ G r a d i e n t I m a g e %
360% %
361% %
362% %
363%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
364%
cristycee97112010-05-28 00:44:52 +0000365% GradientImage() applies a continuously smooth color transitions along a
cristy3ed852e2009-09-05 21:47:34 +0000366% vector from one color to another.
367%
368% Note, the interface of this method will change in the future to support
369% more than one transistion.
370%
371% The format of the GradientImage method is:
372%
373% MagickBooleanType GradientImage(Image *image,const GradientType type,
cristy101ab702011-10-13 13:06:32 +0000374% const SpreadMethod method,const PixelInfo *start_color,
375% const PixelInfo *stop_color,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000376%
377% A description of each parameter follows:
378%
379% o image: the image.
380%
381% o type: the gradient type: linear or radial.
382%
383% o spread: the gradient spread meathod: pad, reflect, or repeat.
384%
385% o start_color: the start color.
386%
387% o stop_color: the stop color.
388%
cristy189e84c2011-08-27 18:08:53 +0000389% o exception: return any errors or warnings in this structure.
390%
cristy3ed852e2009-09-05 21:47:34 +0000391*/
cristy117ff172010-08-15 21:35:32 +0000392
393static inline double MagickMax(const double x,const double y)
394{
395 return(x > y ? x : y);
396}
397
cristy3ed852e2009-09-05 21:47:34 +0000398MagickExport MagickBooleanType GradientImage(Image *image,
399 const GradientType type,const SpreadMethod method,
cristy101ab702011-10-13 13:06:32 +0000400 const PixelInfo *start_color,const PixelInfo *stop_color,
cristy189e84c2011-08-27 18:08:53 +0000401 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000402{
403 DrawInfo
404 *draw_info;
405
406 GradientInfo
407 *gradient;
408
409 MagickBooleanType
410 status;
411
cristybb503372010-05-27 20:51:26 +0000412 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000413 i;
414
415 /*
416 Set gradient start-stop end points.
417 */
418 assert(image != (const Image *) NULL);
419 assert(image->signature == MagickSignature);
420 if (image->debug != MagickFalse)
421 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy101ab702011-10-13 13:06:32 +0000422 assert(start_color != (const PixelInfo *) NULL);
423 assert(stop_color != (const PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000424 draw_info=AcquireDrawInfo();
425 gradient=(&draw_info->gradient);
426 gradient->type=type;
427 gradient->bounding_box.width=image->columns;
428 gradient->bounding_box.height=image->rows;
429 gradient->gradient_vector.x2=(double) image->columns-1.0;
430 gradient->gradient_vector.y2=(double) image->rows-1.0;
431 if ((type == LinearGradient) && (gradient->gradient_vector.y2 != 0.0))
432 gradient->gradient_vector.x2=0.0;
433 gradient->center.x=(double) gradient->gradient_vector.x2/2.0;
434 gradient->center.y=(double) gradient->gradient_vector.y2/2.0;
435 gradient->radius=MagickMax(gradient->center.x,gradient->center.y);
436 gradient->spread=method;
437 /*
438 Define the gradient to fill between the stops.
439 */
440 gradient->number_stops=2;
441 gradient->stops=(StopInfo *) AcquireQuantumMemory(gradient->number_stops,
442 sizeof(*gradient->stops));
443 if (gradient->stops == (StopInfo *) NULL)
444 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
445 image->filename);
446 (void) ResetMagickMemory(gradient->stops,0,gradient->number_stops*
447 sizeof(*gradient->stops));
cristybb503372010-05-27 20:51:26 +0000448 for (i=0; i < (ssize_t) gradient->number_stops; i++)
cristy4c08aed2011-07-01 19:47:50 +0000449 GetPixelInfo(image,&gradient->stops[i].color);
cristy9d8c8ce2011-10-25 16:13:52 +0000450 gradient->stops[0].color=(*start_color);
cristy3ed852e2009-09-05 21:47:34 +0000451 gradient->stops[0].offset=0.0;
cristy9d8c8ce2011-10-25 16:13:52 +0000452 gradient->stops[1].color=(*stop_color);
cristy3ed852e2009-09-05 21:47:34 +0000453 gradient->stops[1].offset=1.0;
454 /*
455 Draw a gradient on the image.
456 */
cristybafdbba2012-06-20 11:50:57 +0000457 (void) SetImageColorspace(image,start_color->colorspace,exception);
cristy947cb4c2011-10-20 18:41:46 +0000458 status=DrawGradientImage(image,draw_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000459 draw_info=DestroyDrawInfo(draw_info);
cristy3ed852e2009-09-05 21:47:34 +0000460 return(status);
461}
462
463/*
464%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
465% %
466% %
467% %
468% O i l P a i n t I m a g e %
469% %
470% %
471% %
472%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
473%
474% OilPaintImage() applies a special effect filter that simulates an oil
475% painting. Each pixel is replaced by the most frequent color occurring
476% in a circular region defined by radius.
477%
478% The format of the OilPaintImage method is:
479%
480% Image *OilPaintImage(const Image *image,const double radius,
cristy14973ba2011-08-27 23:48:07 +0000481% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000482%
483% A description of each parameter follows:
484%
485% o image: the image.
486%
487% o radius: the radius of the circular neighborhood.
488%
cristy14973ba2011-08-27 23:48:07 +0000489% o sigma: the standard deviation of the Gaussian, in pixels.
490%
cristy3ed852e2009-09-05 21:47:34 +0000491% o exception: return any errors or warnings in this structure.
492%
493*/
494
cristybb503372010-05-27 20:51:26 +0000495static size_t **DestroyHistogramThreadSet(size_t **histogram)
cristy3ed852e2009-09-05 21:47:34 +0000496{
cristybb503372010-05-27 20:51:26 +0000497 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000498 i;
499
cristybb503372010-05-27 20:51:26 +0000500 assert(histogram != (size_t **) NULL);
cristyac245f82012-05-05 17:13:57 +0000501 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
cristybb503372010-05-27 20:51:26 +0000502 if (histogram[i] != (size_t *) NULL)
503 histogram[i]=(size_t *) RelinquishMagickMemory(histogram[i]);
cristyb41ee102010-10-04 16:46:15 +0000504 histogram=(size_t **) RelinquishMagickMemory(histogram);
cristy3ed852e2009-09-05 21:47:34 +0000505 return(histogram);
506}
507
cristybb503372010-05-27 20:51:26 +0000508static size_t **AcquireHistogramThreadSet(const size_t count)
cristy3ed852e2009-09-05 21:47:34 +0000509{
cristybb503372010-05-27 20:51:26 +0000510 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000511 i;
512
cristybb503372010-05-27 20:51:26 +0000513 size_t
cristy3ed852e2009-09-05 21:47:34 +0000514 **histogram,
515 number_threads;
516
cristy9357bdd2012-07-30 12:28:34 +0000517 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
cristy14973ba2011-08-27 23:48:07 +0000518 histogram=(size_t **) AcquireQuantumMemory(number_threads,sizeof(*histogram));
cristybb503372010-05-27 20:51:26 +0000519 if (histogram == (size_t **) NULL)
520 return((size_t **) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000521 (void) ResetMagickMemory(histogram,0,number_threads*sizeof(*histogram));
cristybb503372010-05-27 20:51:26 +0000522 for (i=0; i < (ssize_t) number_threads; i++)
cristy3ed852e2009-09-05 21:47:34 +0000523 {
cristy14973ba2011-08-27 23:48:07 +0000524 histogram[i]=(size_t *) AcquireQuantumMemory(count,sizeof(**histogram));
cristybb503372010-05-27 20:51:26 +0000525 if (histogram[i] == (size_t *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000526 return(DestroyHistogramThreadSet(histogram));
527 }
528 return(histogram);
529}
530
531MagickExport Image *OilPaintImage(const Image *image,const double radius,
cristy14973ba2011-08-27 23:48:07 +0000532 const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000533{
534#define NumberPaintBins 256
535#define OilPaintImageTag "OilPaint/Image"
536
cristyfa112112010-01-04 17:48:07 +0000537 CacheView
538 *image_view,
539 *paint_view;
540
cristy3ed852e2009-09-05 21:47:34 +0000541 Image
cristy404d0da2012-12-20 13:04:30 +0000542 *linear_image,
cristy3ed852e2009-09-05 21:47:34 +0000543 *paint_image;
544
cristy3ed852e2009-09-05 21:47:34 +0000545 MagickBooleanType
546 status;
547
cristybb503372010-05-27 20:51:26 +0000548 MagickOffsetType
549 progress;
550
551 size_t
cristy14973ba2011-08-27 23:48:07 +0000552 **histograms,
cristy3ed852e2009-09-05 21:47:34 +0000553 width;
554
cristybb503372010-05-27 20:51:26 +0000555 ssize_t
cristyf8561542012-01-24 00:26:46 +0000556 center,
cristybb503372010-05-27 20:51:26 +0000557 y;
558
cristy3ed852e2009-09-05 21:47:34 +0000559 /*
560 Initialize painted image attributes.
561 */
562 assert(image != (const Image *) NULL);
563 assert(image->signature == MagickSignature);
564 if (image->debug != MagickFalse)
565 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
566 assert(exception != (ExceptionInfo *) NULL);
567 assert(exception->signature == MagickSignature);
cristy14973ba2011-08-27 23:48:07 +0000568 width=GetOptimalKernelWidth2D(radius,sigma);
cristy404d0da2012-12-20 13:04:30 +0000569 linear_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000570 paint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
cristy404d0da2012-12-20 13:04:30 +0000571 if ((linear_image == (Image *) NULL) || (paint_image == (Image *) NULL))
572 {
573 if (linear_image != (Image *) NULL)
574 linear_image=DestroyImage(linear_image);
575 if (paint_image != (Image *) NULL)
576 linear_image=DestroyImage(paint_image);
577 return((Image *) NULL);
578 }
cristy574cc262011-08-05 01:23:58 +0000579 if (SetImageStorageClass(paint_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000580 {
cristy404d0da2012-12-20 13:04:30 +0000581 linear_image=DestroyImage(linear_image);
cristy3ed852e2009-09-05 21:47:34 +0000582 paint_image=DestroyImage(paint_image);
583 return((Image *) NULL);
584 }
585 histograms=AcquireHistogramThreadSet(NumberPaintBins);
cristybb503372010-05-27 20:51:26 +0000586 if (histograms == (size_t **) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000587 {
cristy404d0da2012-12-20 13:04:30 +0000588 linear_image=DestroyImage(linear_image);
cristy3ed852e2009-09-05 21:47:34 +0000589 paint_image=DestroyImage(paint_image);
590 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
591 }
592 /*
593 Oil paint image.
594 */
595 status=MagickTrue;
596 progress=0;
cristy404d0da2012-12-20 13:04:30 +0000597 center=(ssize_t) GetPixelChannels(linear_image)*(linear_image->columns+width)*
598 (width/2L)+GetPixelChannels(linear_image)*(width/2L);
599 image_view=AcquireVirtualCacheView(linear_image,exception);
cristy46ff2672012-12-14 15:32:26 +0000600 paint_view=AcquireAuthenticCacheView(paint_image,exception);
cristyb5d5f722009-11-04 03:03:49 +0000601#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000602 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy404d0da2012-12-20 13:04:30 +0000603 magick_threads(linear_image,paint_image,linear_image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +0000604#endif
cristy404d0da2012-12-20 13:04:30 +0000605 for (y=0; y < (ssize_t) linear_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000606 {
cristy4c08aed2011-07-01 19:47:50 +0000607 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000608 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000609
cristy4c08aed2011-07-01 19:47:50 +0000610 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000611 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000612
cristybb503372010-05-27 20:51:26 +0000613 register size_t
cristy3ed852e2009-09-05 21:47:34 +0000614 *histogram;
615
cristy14973ba2011-08-27 23:48:07 +0000616 register ssize_t
617 x;
618
cristy3ed852e2009-09-05 21:47:34 +0000619 if (status == MagickFalse)
620 continue;
cristyfe4ba002011-02-28 14:54:12 +0000621 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
cristy404d0da2012-12-20 13:04:30 +0000622 (width/2L),linear_image->columns+width,width,exception);
cristy3ed852e2009-09-05 21:47:34 +0000623 q=QueueCacheViewAuthenticPixels(paint_view,0,y,paint_image->columns,1,
624 exception);
cristy4c08aed2011-07-01 19:47:50 +0000625 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000626 {
627 status=MagickFalse;
628 continue;
629 }
cristy3ed852e2009-09-05 21:47:34 +0000630 histogram=histograms[GetOpenMPThreadId()];
cristy404d0da2012-12-20 13:04:30 +0000631 for (x=0; x < (ssize_t) linear_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000632 {
cristybb503372010-05-27 20:51:26 +0000633 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000634 i,
635 u;
636
cristybb503372010-05-27 20:51:26 +0000637 size_t
cristy3ed852e2009-09-05 21:47:34 +0000638 count;
639
cristy9d314ff2011-03-09 01:30:28 +0000640 ssize_t
641 j,
642 k,
cristyf8561542012-01-24 00:26:46 +0000643 n,
cristy9d314ff2011-03-09 01:30:28 +0000644 v;
645
cristy3ed852e2009-09-05 21:47:34 +0000646 /*
647 Assign most frequent color.
648 */
cristyf8561542012-01-24 00:26:46 +0000649 k=0;
cristy3ed852e2009-09-05 21:47:34 +0000650 j=0;
651 count=0;
cristyf8561542012-01-24 00:26:46 +0000652 (void) ResetMagickMemory(histogram,0,NumberPaintBins* sizeof(*histogram));
cristybb503372010-05-27 20:51:26 +0000653 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +0000654 {
cristybb503372010-05-27 20:51:26 +0000655 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +0000656 {
cristy404d0da2012-12-20 13:04:30 +0000657 n=(ssize_t) ScaleQuantumToChar(ClampToQuantum(GetPixelIntensity(
658 linear_image,p+GetPixelChannels(linear_image)*(u+k))));
cristyf8561542012-01-24 00:26:46 +0000659 histogram[n]++;
660 if (histogram[n] > count)
cristy3ed852e2009-09-05 21:47:34 +0000661 {
cristyf8561542012-01-24 00:26:46 +0000662 j=k+u;
663 count=histogram[n];
cristy3ed852e2009-09-05 21:47:34 +0000664 }
665 }
cristy034ade92013-03-07 12:18:19 +0000666 k+=(ssize_t) (linear_image->columns+width);
cristy3ed852e2009-09-05 21:47:34 +0000667 }
cristy404d0da2012-12-20 13:04:30 +0000668 for (i=0; i < (ssize_t) GetPixelChannels(linear_image); i++)
cristyf8561542012-01-24 00:26:46 +0000669 {
cristy5a23c552013-02-13 14:34:28 +0000670 PixelChannel channel=GetPixelChannelChannel(linear_image,i);
671 PixelTrait traits=GetPixelChannelTraits(linear_image,channel);
672 PixelTrait paint_traits=GetPixelChannelTraits(paint_image,channel);
cristyf8561542012-01-24 00:26:46 +0000673 if ((traits == UndefinedPixelTrait) ||
674 (paint_traits == UndefinedPixelTrait))
675 continue;
cristy1eced092012-08-10 23:10:56 +0000676 if (((paint_traits & CopyPixelTrait) != 0) ||
cristy404d0da2012-12-20 13:04:30 +0000677 (GetPixelMask(linear_image,p) != 0))
cristyf8561542012-01-24 00:26:46 +0000678 {
679 SetPixelChannel(paint_image,channel,p[center+i],q);
680 continue;
681 }
cristy404d0da2012-12-20 13:04:30 +0000682 SetPixelChannel(paint_image,channel,p[j*GetPixelChannels(linear_image)+
683 i],q);
cristyf8561542012-01-24 00:26:46 +0000684 }
cristy404d0da2012-12-20 13:04:30 +0000685 p+=GetPixelChannels(linear_image);
cristyed231572011-07-14 02:18:59 +0000686 q+=GetPixelChannels(paint_image);
cristy3ed852e2009-09-05 21:47:34 +0000687 }
688 if (SyncCacheViewAuthenticPixels(paint_view,exception) == MagickFalse)
689 status=MagickFalse;
cristy404d0da2012-12-20 13:04:30 +0000690 if (linear_image->progress_monitor != (MagickProgressMonitor) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000691 {
692 MagickBooleanType
693 proceed;
694
cristyb5d5f722009-11-04 03:03:49 +0000695#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy95338b32012-01-22 03:34:36 +0000696 #pragma omp critical (MagickCore_OilPaintImage)
cristy3ed852e2009-09-05 21:47:34 +0000697#endif
cristy404d0da2012-12-20 13:04:30 +0000698 proceed=SetImageProgress(linear_image,OilPaintImageTag,progress++,
699 linear_image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000700 if (proceed == MagickFalse)
701 status=MagickFalse;
702 }
703 }
704 paint_view=DestroyCacheView(paint_view);
705 image_view=DestroyCacheView(image_view);
706 histograms=DestroyHistogramThreadSet(histograms);
cristy404d0da2012-12-20 13:04:30 +0000707 linear_image=DestroyImage(linear_image);
cristy3ed852e2009-09-05 21:47:34 +0000708 if (status == MagickFalse)
709 paint_image=DestroyImage(paint_image);
710 return(paint_image);
711}
712
713/*
714%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
715% %
716% %
717% %
718% O p a q u e P a i n t I m a g e %
719% %
720% %
721% %
722%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
723%
724% OpaquePaintImage() changes any pixel that matches color with the color
725% defined by fill.
726%
cristy14973ba2011-08-27 23:48:07 +0000727% By default color must match a particular pixel color exactly. However, in
728% many cases two colors may differ by a small amount. Fuzz defines how much
729% tolerance is acceptable to consider two colors as the same. For example,
730% set fuzz to 10 and the color red at intensities of 100 and 102 respectively
731% are now interpreted as the same color.
cristy3ed852e2009-09-05 21:47:34 +0000732%
733% The format of the OpaquePaintImage method is:
734%
735% MagickBooleanType OpaquePaintImage(Image *image,
cristy101ab702011-10-13 13:06:32 +0000736% const PixelInfo *target,const PixelInfo *fill,
cristy189e84c2011-08-27 18:08:53 +0000737% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000738%
739% A description of each parameter follows:
740%
741% o image: the image.
742%
cristy3ed852e2009-09-05 21:47:34 +0000743% o target: the RGB value of the target color.
744%
745% o fill: the replacement color.
746%
747% o invert: paint any pixel that does not match the target color.
748%
cristy189e84c2011-08-27 18:08:53 +0000749% o exception: return any errors or warnings in this structure.
750%
cristy3ed852e2009-09-05 21:47:34 +0000751*/
cristy3ed852e2009-09-05 21:47:34 +0000752MagickExport MagickBooleanType OpaquePaintImage(Image *image,
cristy189e84c2011-08-27 18:08:53 +0000753 const PixelInfo *target,const PixelInfo *fill,const MagickBooleanType invert,
754 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000755{
756#define OpaquePaintImageTag "Opaque/Image"
757
cristyc4c8d132010-01-07 01:58:38 +0000758 CacheView
759 *image_view;
760
cristy3ed852e2009-09-05 21:47:34 +0000761 MagickBooleanType
762 status;
763
cristybb503372010-05-27 20:51:26 +0000764 MagickOffsetType
765 progress;
766
cristy4c08aed2011-07-01 19:47:50 +0000767 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000768 zero;
769
cristybb503372010-05-27 20:51:26 +0000770 ssize_t
771 y;
772
cristy3ed852e2009-09-05 21:47:34 +0000773 assert(image != (Image *) NULL);
774 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +0000775 assert(target != (PixelInfo *) NULL);
776 assert(fill != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000777 if (image->debug != MagickFalse)
778 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy574cc262011-08-05 01:23:58 +0000779 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000780 return(MagickFalse);
cristy400275a2013-03-13 00:34:24 +0000781 if ((fill->alpha_trait == BlendPixelTrait) &&
782 (image->alpha_trait != BlendPixelTrait))
cristy09250192012-02-07 18:53:31 +0000783 (void) SetImageAlpha(image,OpaqueAlpha,exception);
cristy3ed852e2009-09-05 21:47:34 +0000784 /*
785 Make image color opaque.
786 */
787 status=MagickTrue;
788 progress=0;
cristy4c08aed2011-07-01 19:47:50 +0000789 GetPixelInfo(image,&zero);
cristy46ff2672012-12-14 15:32:26 +0000790 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +0000791#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000792 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +0000793 magick_threads(image,image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +0000794#endif
cristybb503372010-05-27 20:51:26 +0000795 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000796 {
cristy4c08aed2011-07-01 19:47:50 +0000797 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000798 pixel;
799
cristy4c08aed2011-07-01 19:47:50 +0000800 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000801 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000802
cristy14973ba2011-08-27 23:48:07 +0000803 register ssize_t
804 x;
805
cristy3ed852e2009-09-05 21:47:34 +0000806 if (status == MagickFalse)
807 continue;
808 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy14973ba2011-08-27 23:48:07 +0000809 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000810 {
811 status=MagickFalse;
812 continue;
813 }
cristy3ed852e2009-09-05 21:47:34 +0000814 pixel=zero;
cristybb503372010-05-27 20:51:26 +0000815 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000816 {
cristy803640d2011-11-17 02:11:32 +0000817 GetPixelInfoPixel(image,q,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000818 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
cristy95f562a2012-01-01 20:49:11 +0000819 SetPixelInfoPixel(image,fill,q);
cristyed231572011-07-14 02:18:59 +0000820 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000821 }
822 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
823 status=MagickFalse;
824 if (image->progress_monitor != (MagickProgressMonitor) NULL)
825 {
826 MagickBooleanType
827 proceed;
828
cristyb5d5f722009-11-04 03:03:49 +0000829#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy95338b32012-01-22 03:34:36 +0000830 #pragma omp critical (MagickCore_OpaquePaintImage)
cristy3ed852e2009-09-05 21:47:34 +0000831#endif
832 proceed=SetImageProgress(image,OpaquePaintImageTag,progress++,
833 image->rows);
834 if (proceed == MagickFalse)
835 status=MagickFalse;
836 }
837 }
838 image_view=DestroyCacheView(image_view);
839 return(status);
840}
841
842/*
843%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
844% %
845% %
846% %
847% T r a n s p a r e n t P a i n t I m a g e %
848% %
849% %
850% %
851%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
852%
853% TransparentPaintImage() changes the opacity value associated with any pixel
854% that matches color to the value defined by opacity.
855%
cristy14973ba2011-08-27 23:48:07 +0000856% By default color must match a particular pixel color exactly. However, in
857% many cases two colors may differ by a small amount. Fuzz defines how much
858% tolerance is acceptable to consider two colors as the same. For example,
859% set fuzz to 10 and the color red at intensities of 100 and 102 respectively
860% are now interpreted as the same color.
cristy3ed852e2009-09-05 21:47:34 +0000861%
862% The format of the TransparentPaintImage method is:
863%
864% MagickBooleanType TransparentPaintImage(Image *image,
cristy4c08aed2011-07-01 19:47:50 +0000865% const PixelInfo *target,const Quantum opacity,
cristy189e84c2011-08-27 18:08:53 +0000866% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000867%
868% A description of each parameter follows:
869%
870% o image: the image.
871%
872% o target: the target color.
873%
874% o opacity: the replacement opacity value.
875%
876% o invert: paint any pixel that does not match the target color.
877%
cristy189e84c2011-08-27 18:08:53 +0000878% o exception: return any errors or warnings in this structure.
879%
cristy3ed852e2009-09-05 21:47:34 +0000880*/
881MagickExport MagickBooleanType TransparentPaintImage(Image *image,
cristy14973ba2011-08-27 23:48:07 +0000882 const PixelInfo *target,const Quantum opacity,const MagickBooleanType invert,
883 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000884{
885#define TransparentPaintImageTag "Transparent/Image"
886
cristyc4c8d132010-01-07 01:58:38 +0000887 CacheView
888 *image_view;
889
cristy3ed852e2009-09-05 21:47:34 +0000890 MagickBooleanType
891 status;
892
cristybb503372010-05-27 20:51:26 +0000893 MagickOffsetType
894 progress;
895
cristy4c08aed2011-07-01 19:47:50 +0000896 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000897 zero;
898
cristybb503372010-05-27 20:51:26 +0000899 ssize_t
900 y;
901
cristy3ed852e2009-09-05 21:47:34 +0000902 assert(image != (Image *) NULL);
903 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +0000904 assert(target != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000905 if (image->debug != MagickFalse)
906 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy574cc262011-08-05 01:23:58 +0000907 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000908 return(MagickFalse);
cristy8a46d822012-08-28 23:32:39 +0000909 if (image->alpha_trait != BlendPixelTrait)
cristy63240882011-08-05 19:05:27 +0000910 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +0000911 /*
912 Make image color transparent.
913 */
914 status=MagickTrue;
915 progress=0;
cristy4c08aed2011-07-01 19:47:50 +0000916 GetPixelInfo(image,&zero);
cristy46ff2672012-12-14 15:32:26 +0000917 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +0000918#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000919 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +0000920 magick_threads(image,image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +0000921#endif
cristybb503372010-05-27 20:51:26 +0000922 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000923 {
cristy4c08aed2011-07-01 19:47:50 +0000924 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000925 pixel;
926
cristybb503372010-05-27 20:51:26 +0000927 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000928 x;
929
cristy4c08aed2011-07-01 19:47:50 +0000930 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000931 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000932
933 if (status == MagickFalse)
934 continue;
935 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy14973ba2011-08-27 23:48:07 +0000936 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000937 {
938 status=MagickFalse;
939 continue;
940 }
cristy3ed852e2009-09-05 21:47:34 +0000941 pixel=zero;
cristybb503372010-05-27 20:51:26 +0000942 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000943 {
cristy803640d2011-11-17 02:11:32 +0000944 GetPixelInfoPixel(image,q,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000945 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
946 SetPixelAlpha(image,opacity,q);
cristyed231572011-07-14 02:18:59 +0000947 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000948 }
949 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
950 status=MagickFalse;
951 if (image->progress_monitor != (MagickProgressMonitor) NULL)
952 {
953 MagickBooleanType
954 proceed;
955
cristyb5d5f722009-11-04 03:03:49 +0000956#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy95338b32012-01-22 03:34:36 +0000957 #pragma omp critical (MagickCore_TransparentPaintImage)
cristy3ed852e2009-09-05 21:47:34 +0000958#endif
959 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
960 image->rows);
961 if (proceed == MagickFalse)
962 status=MagickFalse;
963 }
964 }
965 image_view=DestroyCacheView(image_view);
966 return(status);
967}
968
969/*
970%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
971% %
972% %
973% %
974% T r a n s p a r e n t P a i n t I m a g e C h r o m a %
975% %
976% %
977% %
978%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
979%
980% TransparentPaintImageChroma() changes the opacity value associated with any
981% pixel that matches color to the value defined by opacity.
982%
cristy14973ba2011-08-27 23:48:07 +0000983% As there is one fuzz value for the all the channels, TransparentPaintImage()
984% is not suitable for the operations like chroma, where the tolerance for
985% similarity of two color component (RGB) can be different. Thus we define
986% this method to take two target pixels (one low and one high) and all the
987% pixels of an image which are lying between these two pixels are made
988% transparent.
cristy3ed852e2009-09-05 21:47:34 +0000989%
cristy14973ba2011-08-27 23:48:07 +0000990% The format of the TransparentPaintImageChroma method is:
cristy3ed852e2009-09-05 21:47:34 +0000991%
cristy14973ba2011-08-27 23:48:07 +0000992% MagickBooleanType TransparentPaintImageChroma(Image *image,
993% const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
994% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000995%
996% A description of each parameter follows:
997%
998% o image: the image.
999%
1000% o low: the low target color.
1001%
1002% o high: the high target color.
1003%
1004% o opacity: the replacement opacity value.
1005%
1006% o invert: paint any pixel that does not match the target color.
1007%
cristy189e84c2011-08-27 18:08:53 +00001008% o exception: return any errors or warnings in this structure.
1009%
cristy3ed852e2009-09-05 21:47:34 +00001010*/
1011MagickExport MagickBooleanType TransparentPaintImageChroma(Image *image,
cristy189e84c2011-08-27 18:08:53 +00001012 const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
1013 const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001014{
1015#define TransparentPaintImageTag "Transparent/Image"
1016
cristyc4c8d132010-01-07 01:58:38 +00001017 CacheView
1018 *image_view;
1019
cristy3ed852e2009-09-05 21:47:34 +00001020 MagickBooleanType
1021 status;
1022
cristybb503372010-05-27 20:51:26 +00001023 MagickOffsetType
1024 progress;
1025
1026 ssize_t
1027 y;
1028
cristy3ed852e2009-09-05 21:47:34 +00001029 assert(image != (Image *) NULL);
1030 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +00001031 assert(high != (PixelInfo *) NULL);
1032 assert(low != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001033 if (image->debug != MagickFalse)
1034 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy574cc262011-08-05 01:23:58 +00001035 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001036 return(MagickFalse);
cristy8a46d822012-08-28 23:32:39 +00001037 if (image->alpha_trait != BlendPixelTrait)
cristy63240882011-08-05 19:05:27 +00001038 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00001039 /*
1040 Make image color transparent.
1041 */
1042 status=MagickTrue;
1043 progress=0;
cristy46ff2672012-12-14 15:32:26 +00001044 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00001045#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00001046 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy5e6b2592012-12-19 14:08:11 +00001047 magick_threads(image,image,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00001048#endif
cristybb503372010-05-27 20:51:26 +00001049 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001050 {
1051 MagickBooleanType
1052 match;
1053
cristy4c08aed2011-07-01 19:47:50 +00001054 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001055 pixel;
1056
cristy4c08aed2011-07-01 19:47:50 +00001057 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001058 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001059
cristy14973ba2011-08-27 23:48:07 +00001060 register ssize_t
1061 x;
1062
cristy3ed852e2009-09-05 21:47:34 +00001063 if (status == MagickFalse)
1064 continue;
1065 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy14973ba2011-08-27 23:48:07 +00001066 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001067 {
1068 status=MagickFalse;
1069 continue;
1070 }
cristy4c08aed2011-07-01 19:47:50 +00001071 GetPixelInfo(image,&pixel);
cristybb503372010-05-27 20:51:26 +00001072 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001073 {
cristy803640d2011-11-17 02:11:32 +00001074 GetPixelInfoPixel(image,q,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001075 match=((pixel.red >= low->red) && (pixel.red <= high->red) &&
1076 (pixel.green >= low->green) && (pixel.green <= high->green) &&
cristy14973ba2011-08-27 23:48:07 +00001077 (pixel.blue >= low->blue) && (pixel.blue <= high->blue)) ? MagickTrue :
1078 MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00001079 if (match != invert)
cristy4c08aed2011-07-01 19:47:50 +00001080 SetPixelAlpha(image,opacity,q);
cristyed231572011-07-14 02:18:59 +00001081 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001082 }
1083 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1084 status=MagickFalse;
1085 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1086 {
1087 MagickBooleanType
1088 proceed;
1089
cristyb5d5f722009-11-04 03:03:49 +00001090#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy95338b32012-01-22 03:34:36 +00001091 #pragma omp critical (MagickCore_TransparentPaintImageChroma)
cristy3ed852e2009-09-05 21:47:34 +00001092#endif
1093 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
1094 image->rows);
1095 if (proceed == MagickFalse)
1096 status=MagickFalse;
1097 }
1098 }
1099 image_view=DestroyCacheView(image_view);
1100 return(status);
1101}