blob: 4d303efcbc33fb2d4c579f6d17932a22ac7ebec0 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% PPPP AAA IIIII N N TTTTT %
7% P P A A I NN N T %
8% PPPP AAAAA I N N N T %
9% P A A I N NN T %
10% P A A IIIII N N T %
11% %
12% %
13% Methods to Paint on an Image %
14% %
15% Software Design %
16% John Cristy %
17% July 1998 %
18% %
19% %
cristy1454be72011-12-19 01:52:48 +000020% Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37*/
38
39/*
40 Include declarations.
41*/
cristy4c08aed2011-07-01 19:47:50 +000042#include "MagickCore/studio.h"
43#include "MagickCore/color.h"
44#include "MagickCore/color-private.h"
45#include "MagickCore/colorspace-private.h"
46#include "MagickCore/composite.h"
47#include "MagickCore/composite-private.h"
48#include "MagickCore/draw.h"
49#include "MagickCore/draw-private.h"
50#include "MagickCore/exception.h"
51#include "MagickCore/exception-private.h"
52#include "MagickCore/gem.h"
cristy8ea81222011-09-04 10:33:32 +000053#include "MagickCore/gem-private.h"
cristy4c08aed2011-07-01 19:47:50 +000054#include "MagickCore/monitor.h"
55#include "MagickCore/monitor-private.h"
56#include "MagickCore/paint.h"
57#include "MagickCore/pixel-accessor.h"
58#include "MagickCore/string_.h"
59#include "MagickCore/thread-private.h"
cristy3ed852e2009-09-05 21:47:34 +000060
cristy3ed852e2009-09-05 21:47:34 +000061/*
62%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
63% %
64% %
65% %
66% F l o o d f i l l P a i n t I m a g e %
67% %
68% %
69% %
70%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
71%
72% FloodfillPaintImage() changes the color value of any pixel that matches
73% target and is an immediate neighbor. If the method FillToBorderMethod is
74% specified, the color value is changed for any neighbor pixel that does not
75% match the bordercolor member of image.
76%
cristy908a0002011-08-28 00:13:39 +000077% By default target must match a particular pixel color exactly. However,
78% in many cases two colors may differ by a small amount. The fuzz member of
79% image defines how much tolerance is acceptable to consider two colors as
80% the same. For example, set fuzz to 10 and the color red at intensities of
81% 100 and 102 respectively are now interpreted as the same color for the
82% purposes of the floodfill.
cristy3ed852e2009-09-05 21:47:34 +000083%
84% The format of the FloodfillPaintImage method is:
85%
86% MagickBooleanType FloodfillPaintImage(Image *image,
cristyd42d9952011-07-08 14:21:50 +000087% const DrawInfo *draw_info,const PixelInfo target,
88% const ssize_t x_offset,const ssize_t y_offset,
cristy189e84c2011-08-27 18:08:53 +000089% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +000090%
91% A description of each parameter follows:
92%
93% o image: the image.
94%
cristy3ed852e2009-09-05 21:47:34 +000095% o draw_info: the draw info.
96%
97% o target: the RGB value of the target color.
98%
99% o x_offset,y_offset: the starting location of the operation.
100%
101% o invert: paint any pixel that does not match the target color.
102%
cristy189e84c2011-08-27 18:08:53 +0000103% o exception: return any errors or warnings in this structure.
104%
cristy3ed852e2009-09-05 21:47:34 +0000105*/
106MagickExport MagickBooleanType FloodfillPaintImage(Image *image,
cristyd42d9952011-07-08 14:21:50 +0000107 const DrawInfo *draw_info,const PixelInfo *target,const ssize_t x_offset,
cristy189e84c2011-08-27 18:08:53 +0000108 const ssize_t y_offset,const MagickBooleanType invert,
109 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000110{
111#define MaxStacksize (1UL << 15)
112#define PushSegmentStack(up,left,right,delta) \
113{ \
114 if (s >= (segment_stack+MaxStacksize)) \
115 ThrowBinaryException(DrawError,"SegmentStackOverflow",image->filename) \
116 else \
117 { \
cristybb503372010-05-27 20:51:26 +0000118 if ((((up)+(delta)) >= 0) && (((up)+(delta)) < (ssize_t) image->rows)) \
cristy3ed852e2009-09-05 21:47:34 +0000119 { \
120 s->x1=(double) (left); \
121 s->y1=(double) (up); \
122 s->x2=(double) (right); \
123 s->y2=(double) (delta); \
124 s++; \
125 } \
126 } \
127}
128
cristyb0d3bb92010-09-22 14:37:58 +0000129 CacheView
130 *floodplane_view,
131 *image_view;
132
cristy3ed852e2009-09-05 21:47:34 +0000133 Image
134 *floodplane_image;
135
cristy3ed852e2009-09-05 21:47:34 +0000136 MagickBooleanType
cristy14973ba2011-08-27 23:48:07 +0000137 skip,
138 status;
cristy3ed852e2009-09-05 21:47:34 +0000139
cristy4c08aed2011-07-01 19:47:50 +0000140 PixelInfo
cristyfdcc0182011-12-26 19:33:56 +0000141 fill_color,
cristy3ed852e2009-09-05 21:47:34 +0000142 pixel;
143
cristy3ed852e2009-09-05 21:47:34 +0000144 register SegmentInfo
145 *s;
146
147 SegmentInfo
148 *segment_stack;
149
cristy9d314ff2011-03-09 01:30:28 +0000150 ssize_t
151 offset,
152 start,
153 x,
154 x1,
155 x2,
156 y;
157
cristy3ed852e2009-09-05 21:47:34 +0000158 /*
159 Check boundary conditions.
160 */
161 assert(image != (Image *) NULL);
162 assert(image->signature == MagickSignature);
163 if (image->debug != MagickFalse)
164 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
165 assert(draw_info != (DrawInfo *) NULL);
166 assert(draw_info->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +0000167 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
cristy3ed852e2009-09-05 21:47:34 +0000168 return(MagickFalse);
cristybb503372010-05-27 20:51:26 +0000169 if ((y_offset < 0) || (y_offset >= (ssize_t) image->rows))
cristy3ed852e2009-09-05 21:47:34 +0000170 return(MagickFalse);
cristy574cc262011-08-05 01:23:58 +0000171 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000172 return(MagickFalse);
173 if (image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +0000174 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +0000175 /*
176 Set floodfill state.
177 */
cristyc82a27b2011-10-21 01:07:16 +0000178 floodplane_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000179 if (floodplane_image == (Image *) NULL)
180 return(MagickFalse);
cristy63240882011-08-05 19:05:27 +0000181 (void) SetImageAlphaChannel(floodplane_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +0000182 segment_stack=(SegmentInfo *) AcquireQuantumMemory(MaxStacksize,
183 sizeof(*segment_stack));
184 if (segment_stack == (SegmentInfo *) NULL)
185 {
186 floodplane_image=DestroyImage(floodplane_image);
187 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
188 image->filename);
189 }
190 /*
191 Push initial segment on stack.
192 */
cristy14973ba2011-08-27 23:48:07 +0000193 status=MagickTrue;
cristy2ed42f62011-10-02 19:49:57 +0000194 fill_color.black=0.0;
195 fill_color.index=0.0;
cristy3ed852e2009-09-05 21:47:34 +0000196 x=x_offset;
197 y=y_offset;
198 start=0;
199 s=segment_stack;
200 PushSegmentStack(y,x,x,1);
201 PushSegmentStack(y+1,x,x,-1);
cristy4c08aed2011-07-01 19:47:50 +0000202 GetPixelInfo(image,&pixel);
cristyb0d3bb92010-09-22 14:37:58 +0000203 image_view=AcquireCacheView(image);
204 floodplane_view=AcquireCacheView(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000205 while (s > segment_stack)
206 {
cristy4c08aed2011-07-01 19:47:50 +0000207 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000208 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000209
cristy4c08aed2011-07-01 19:47:50 +0000210 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000211 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000212
cristy14973ba2011-08-27 23:48:07 +0000213 register ssize_t
214 x;
215
cristy3ed852e2009-09-05 21:47:34 +0000216 /*
217 Pop segment off stack.
218 */
219 s--;
cristybb503372010-05-27 20:51:26 +0000220 x1=(ssize_t) s->x1;
221 x2=(ssize_t) s->x2;
222 offset=(ssize_t) s->y2;
223 y=(ssize_t) s->y1+offset;
cristy3ed852e2009-09-05 21:47:34 +0000224 /*
225 Recolor neighboring pixels.
226 */
cristyb0d3bb92010-09-22 14:37:58 +0000227 p=GetCacheViewVirtualPixels(image_view,0,y,(size_t) (x1+1),1,exception);
228 q=GetCacheViewAuthenticPixels(floodplane_view,0,y,(size_t) (x1+1),1,
cristy3ed852e2009-09-05 21:47:34 +0000229 exception);
cristy4c08aed2011-07-01 19:47:50 +0000230 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000231 break;
cristyed231572011-07-14 02:18:59 +0000232 p+=x1*GetPixelChannels(image);
233 q+=x1*GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000234 for (x=x1; x >= 0; x--)
235 {
cristyfdcc0182011-12-26 19:33:56 +0000236 if (GetPixelAlpha(floodplane_image,q) == TransparentAlpha)
cristy3ed852e2009-09-05 21:47:34 +0000237 break;
cristy803640d2011-11-17 02:11:32 +0000238 GetPixelInfoPixel(image,p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000239 if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert)
cristy3ed852e2009-09-05 21:47:34 +0000240 break;
cristy4c08aed2011-07-01 19:47:50 +0000241 SetPixelAlpha(floodplane_image,TransparentAlpha,q);
cristyed231572011-07-14 02:18:59 +0000242 p-=GetPixelChannels(image);
243 q-=GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000244 }
cristyb0d3bb92010-09-22 14:37:58 +0000245 if (SyncCacheViewAuthenticPixels(floodplane_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000246 break;
247 skip=x >= x1 ? MagickTrue : MagickFalse;
248 if (skip == MagickFalse)
249 {
250 start=x+1;
251 if (start < x1)
252 PushSegmentStack(y,start,x1-1,-offset);
253 x=x1+1;
254 }
255 do
256 {
257 if (skip == MagickFalse)
258 {
cristybb503372010-05-27 20:51:26 +0000259 if (x < (ssize_t) image->columns)
cristy3ed852e2009-09-05 21:47:34 +0000260 {
cristyb0d3bb92010-09-22 14:37:58 +0000261 p=GetCacheViewVirtualPixels(image_view,x,y,image->columns-x,1,
cristy3ed852e2009-09-05 21:47:34 +0000262 exception);
cristyfdcc0182011-12-26 19:33:56 +0000263 q=GetCacheViewAuthenticPixels(floodplane_view,x,y,image->columns-
264 x,1,exception);
cristy636dcb52011-08-26 13:23:49 +0000265 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000266 break;
cristybb503372010-05-27 20:51:26 +0000267 for ( ; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000268 {
cristyfdcc0182011-12-26 19:33:56 +0000269 if (GetPixelAlpha(floodplane_image,q) == TransparentAlpha)
cristy3ed852e2009-09-05 21:47:34 +0000270 break;
cristy803640d2011-11-17 02:11:32 +0000271 GetPixelInfoPixel(image,p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000272 if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert)
cristy3ed852e2009-09-05 21:47:34 +0000273 break;
cristy14973ba2011-08-27 23:48:07 +0000274 SetPixelAlpha(floodplane_image,TransparentAlpha,q);
cristyed231572011-07-14 02:18:59 +0000275 p+=GetPixelChannels(image);
276 q+=GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000277 }
cristy14973ba2011-08-27 23:48:07 +0000278 status=SyncCacheViewAuthenticPixels(floodplane_view,exception);
279 if (status == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000280 break;
281 }
282 PushSegmentStack(y,start,x-1,offset);
283 if (x > (x2+1))
284 PushSegmentStack(y,x2+1,x-1,-offset);
285 }
286 skip=MagickFalse;
287 x++;
288 if (x <= x2)
289 {
cristyb0d3bb92010-09-22 14:37:58 +0000290 p=GetCacheViewVirtualPixels(image_view,x,y,(size_t) (x2-x+1),1,
291 exception);
292 q=GetCacheViewAuthenticPixels(floodplane_view,x,y,(size_t) (x2-x+1),1,
cristy3ed852e2009-09-05 21:47:34 +0000293 exception);
cristy636dcb52011-08-26 13:23:49 +0000294 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000295 break;
cristy3ed852e2009-09-05 21:47:34 +0000296 for ( ; x <= x2; x++)
297 {
cristyfdcc0182011-12-26 19:33:56 +0000298 if (GetPixelAlpha(floodplane_image,q) == TransparentAlpha)
cristy3ed852e2009-09-05 21:47:34 +0000299 break;
cristy803640d2011-11-17 02:11:32 +0000300 GetPixelInfoPixel(image,p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000301 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
cristy3ed852e2009-09-05 21:47:34 +0000302 break;
cristyed231572011-07-14 02:18:59 +0000303 p+=GetPixelChannels(image);
304 q+=GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000305 }
306 }
307 start=x;
308 } while (x <= x2);
309 }
cristybb503372010-05-27 20:51:26 +0000310 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000311 {
cristy4c08aed2011-07-01 19:47:50 +0000312 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000313 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000314
cristy4c08aed2011-07-01 19:47:50 +0000315 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000316 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000317
cristy14973ba2011-08-27 23:48:07 +0000318 register ssize_t
319 x;
320
cristy3ed852e2009-09-05 21:47:34 +0000321 /*
322 Tile fill color onto floodplane.
323 */
cristyb0d3bb92010-09-22 14:37:58 +0000324 p=GetCacheViewVirtualPixels(floodplane_view,0,y,image->columns,1,
325 exception);
326 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000327 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000328 break;
cristybb503372010-05-27 20:51:26 +0000329 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000330 {
cristy4c08aed2011-07-01 19:47:50 +0000331 if (GetPixelAlpha(floodplane_image,p) != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +0000332 {
cristy2ed42f62011-10-02 19:49:57 +0000333 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
cristyed231572011-07-14 02:18:59 +0000334 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy9d8c8ce2011-10-25 16:13:52 +0000335 SetPixelRed(image,ClampToQuantum(fill_color.red),q);
cristyed231572011-07-14 02:18:59 +0000336 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy9d8c8ce2011-10-25 16:13:52 +0000337 SetPixelGreen(image,ClampToQuantum(fill_color.green),q);
cristyed231572011-07-14 02:18:59 +0000338 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy9d8c8ce2011-10-25 16:13:52 +0000339 SetPixelBlue(image,ClampToQuantum(fill_color.blue),q);
cristy14973ba2011-08-27 23:48:07 +0000340 if ((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0)
cristy9d8c8ce2011-10-25 16:13:52 +0000341 SetPixelBlack(image,ClampToQuantum(fill_color.black),q);
cristyed231572011-07-14 02:18:59 +0000342 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy9d8c8ce2011-10-25 16:13:52 +0000343 SetPixelAlpha(image,ClampToQuantum(fill_color.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +0000344 }
cristyed231572011-07-14 02:18:59 +0000345 p+=GetPixelChannels(floodplane_image);
346 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000347 }
cristyb0d3bb92010-09-22 14:37:58 +0000348 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000349 break;
350 }
cristyb0d3bb92010-09-22 14:37:58 +0000351 floodplane_view=DestroyCacheView(floodplane_view);
352 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +0000353 segment_stack=(SegmentInfo *) RelinquishMagickMemory(segment_stack);
354 floodplane_image=DestroyImage(floodplane_image);
cristybb503372010-05-27 20:51:26 +0000355 return(y == (ssize_t) image->rows ? MagickTrue : MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +0000356}
357
358/*
359%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
360% %
361% %
362% %
363+ G r a d i e n t I m a g e %
364% %
365% %
366% %
367%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
368%
cristycee97112010-05-28 00:44:52 +0000369% GradientImage() applies a continuously smooth color transitions along a
cristy3ed852e2009-09-05 21:47:34 +0000370% vector from one color to another.
371%
372% Note, the interface of this method will change in the future to support
373% more than one transistion.
374%
375% The format of the GradientImage method is:
376%
377% MagickBooleanType GradientImage(Image *image,const GradientType type,
cristy101ab702011-10-13 13:06:32 +0000378% const SpreadMethod method,const PixelInfo *start_color,
379% const PixelInfo *stop_color,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000380%
381% A description of each parameter follows:
382%
383% o image: the image.
384%
385% o type: the gradient type: linear or radial.
386%
387% o spread: the gradient spread meathod: pad, reflect, or repeat.
388%
389% o start_color: the start color.
390%
391% o stop_color: the stop color.
392%
cristy189e84c2011-08-27 18:08:53 +0000393% o exception: return any errors or warnings in this structure.
394%
cristy3ed852e2009-09-05 21:47:34 +0000395*/
cristy117ff172010-08-15 21:35:32 +0000396
397static inline double MagickMax(const double x,const double y)
398{
399 return(x > y ? x : y);
400}
401
cristy3ed852e2009-09-05 21:47:34 +0000402MagickExport MagickBooleanType GradientImage(Image *image,
403 const GradientType type,const SpreadMethod method,
cristy101ab702011-10-13 13:06:32 +0000404 const PixelInfo *start_color,const PixelInfo *stop_color,
cristy189e84c2011-08-27 18:08:53 +0000405 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000406{
407 DrawInfo
408 *draw_info;
409
410 GradientInfo
411 *gradient;
412
413 MagickBooleanType
414 status;
415
cristybb503372010-05-27 20:51:26 +0000416 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000417 i;
418
419 /*
420 Set gradient start-stop end points.
421 */
422 assert(image != (const Image *) NULL);
423 assert(image->signature == MagickSignature);
424 if (image->debug != MagickFalse)
425 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy101ab702011-10-13 13:06:32 +0000426 assert(start_color != (const PixelInfo *) NULL);
427 assert(stop_color != (const PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000428 draw_info=AcquireDrawInfo();
429 gradient=(&draw_info->gradient);
430 gradient->type=type;
431 gradient->bounding_box.width=image->columns;
432 gradient->bounding_box.height=image->rows;
433 gradient->gradient_vector.x2=(double) image->columns-1.0;
434 gradient->gradient_vector.y2=(double) image->rows-1.0;
435 if ((type == LinearGradient) && (gradient->gradient_vector.y2 != 0.0))
436 gradient->gradient_vector.x2=0.0;
437 gradient->center.x=(double) gradient->gradient_vector.x2/2.0;
438 gradient->center.y=(double) gradient->gradient_vector.y2/2.0;
439 gradient->radius=MagickMax(gradient->center.x,gradient->center.y);
440 gradient->spread=method;
441 /*
442 Define the gradient to fill between the stops.
443 */
444 gradient->number_stops=2;
445 gradient->stops=(StopInfo *) AcquireQuantumMemory(gradient->number_stops,
446 sizeof(*gradient->stops));
447 if (gradient->stops == (StopInfo *) NULL)
448 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
449 image->filename);
450 (void) ResetMagickMemory(gradient->stops,0,gradient->number_stops*
451 sizeof(*gradient->stops));
cristybb503372010-05-27 20:51:26 +0000452 for (i=0; i < (ssize_t) gradient->number_stops; i++)
cristy4c08aed2011-07-01 19:47:50 +0000453 GetPixelInfo(image,&gradient->stops[i].color);
cristy9d8c8ce2011-10-25 16:13:52 +0000454 gradient->stops[0].color=(*start_color);
cristy3ed852e2009-09-05 21:47:34 +0000455 gradient->stops[0].offset=0.0;
cristy9d8c8ce2011-10-25 16:13:52 +0000456 gradient->stops[1].color=(*stop_color);
cristy3ed852e2009-09-05 21:47:34 +0000457 gradient->stops[1].offset=1.0;
458 /*
459 Draw a gradient on the image.
460 */
cristy947cb4c2011-10-20 18:41:46 +0000461 status=DrawGradientImage(image,draw_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000462 draw_info=DestroyDrawInfo(draw_info);
cristy4c08aed2011-07-01 19:47:50 +0000463 if ((start_color->alpha == OpaqueAlpha) && (stop_color->alpha == OpaqueAlpha))
cristy3ed852e2009-09-05 21:47:34 +0000464 image->matte=MagickFalse;
cristy101ab702011-10-13 13:06:32 +0000465 if ((IsPixelInfoGray(start_color) != MagickFalse) &&
466 (IsPixelInfoGray(stop_color) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +0000467 image->type=GrayscaleType;
468 return(status);
469}
470
471/*
472%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
473% %
474% %
475% %
476% O i l P a i n t I m a g e %
477% %
478% %
479% %
480%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
481%
482% OilPaintImage() applies a special effect filter that simulates an oil
483% painting. Each pixel is replaced by the most frequent color occurring
484% in a circular region defined by radius.
485%
486% The format of the OilPaintImage method is:
487%
488% Image *OilPaintImage(const Image *image,const double radius,
cristy14973ba2011-08-27 23:48:07 +0000489% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000490%
491% A description of each parameter follows:
492%
493% o image: the image.
494%
495% o radius: the radius of the circular neighborhood.
496%
cristy14973ba2011-08-27 23:48:07 +0000497% o sigma: the standard deviation of the Gaussian, in pixels.
498%
cristy3ed852e2009-09-05 21:47:34 +0000499% o exception: return any errors or warnings in this structure.
500%
501*/
502
cristybb503372010-05-27 20:51:26 +0000503static size_t **DestroyHistogramThreadSet(size_t **histogram)
cristy3ed852e2009-09-05 21:47:34 +0000504{
cristybb503372010-05-27 20:51:26 +0000505 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000506 i;
507
cristybb503372010-05-27 20:51:26 +0000508 assert(histogram != (size_t **) NULL);
509 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
510 if (histogram[i] != (size_t *) NULL)
511 histogram[i]=(size_t *) RelinquishMagickMemory(histogram[i]);
cristyb41ee102010-10-04 16:46:15 +0000512 histogram=(size_t **) RelinquishMagickMemory(histogram);
cristy3ed852e2009-09-05 21:47:34 +0000513 return(histogram);
514}
515
cristybb503372010-05-27 20:51:26 +0000516static size_t **AcquireHistogramThreadSet(const size_t count)
cristy3ed852e2009-09-05 21:47:34 +0000517{
cristybb503372010-05-27 20:51:26 +0000518 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000519 i;
520
cristybb503372010-05-27 20:51:26 +0000521 size_t
cristy3ed852e2009-09-05 21:47:34 +0000522 **histogram,
523 number_threads;
524
525 number_threads=GetOpenMPMaximumThreads();
cristy14973ba2011-08-27 23:48:07 +0000526 histogram=(size_t **) AcquireQuantumMemory(number_threads,sizeof(*histogram));
cristybb503372010-05-27 20:51:26 +0000527 if (histogram == (size_t **) NULL)
528 return((size_t **) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000529 (void) ResetMagickMemory(histogram,0,number_threads*sizeof(*histogram));
cristybb503372010-05-27 20:51:26 +0000530 for (i=0; i < (ssize_t) number_threads; i++)
cristy3ed852e2009-09-05 21:47:34 +0000531 {
cristy14973ba2011-08-27 23:48:07 +0000532 histogram[i]=(size_t *) AcquireQuantumMemory(count,sizeof(**histogram));
cristybb503372010-05-27 20:51:26 +0000533 if (histogram[i] == (size_t *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000534 return(DestroyHistogramThreadSet(histogram));
535 }
536 return(histogram);
537}
538
539MagickExport Image *OilPaintImage(const Image *image,const double radius,
cristy14973ba2011-08-27 23:48:07 +0000540 const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000541{
542#define NumberPaintBins 256
543#define OilPaintImageTag "OilPaint/Image"
544
cristyfa112112010-01-04 17:48:07 +0000545 CacheView
546 *image_view,
547 *paint_view;
548
cristy3ed852e2009-09-05 21:47:34 +0000549 Image
550 *paint_image;
551
cristy3ed852e2009-09-05 21:47:34 +0000552 MagickBooleanType
553 status;
554
cristybb503372010-05-27 20:51:26 +0000555 MagickOffsetType
556 progress;
557
558 size_t
cristy14973ba2011-08-27 23:48:07 +0000559 **histograms,
cristy3ed852e2009-09-05 21:47:34 +0000560 width;
561
cristybb503372010-05-27 20:51:26 +0000562 ssize_t
563 y;
564
cristy3ed852e2009-09-05 21:47:34 +0000565 /*
566 Initialize painted image attributes.
567 */
568 assert(image != (const Image *) NULL);
569 assert(image->signature == MagickSignature);
570 if (image->debug != MagickFalse)
571 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
572 assert(exception != (ExceptionInfo *) NULL);
573 assert(exception->signature == MagickSignature);
cristy14973ba2011-08-27 23:48:07 +0000574 width=GetOptimalKernelWidth2D(radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +0000575 paint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
576 if (paint_image == (Image *) NULL)
577 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000578 if (SetImageStorageClass(paint_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000579 {
cristy3ed852e2009-09-05 21:47:34 +0000580 paint_image=DestroyImage(paint_image);
581 return((Image *) NULL);
582 }
583 histograms=AcquireHistogramThreadSet(NumberPaintBins);
cristybb503372010-05-27 20:51:26 +0000584 if (histograms == (size_t **) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000585 {
586 paint_image=DestroyImage(paint_image);
587 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
588 }
589 /*
590 Oil paint image.
591 */
592 status=MagickTrue;
593 progress=0;
594 image_view=AcquireCacheView(image);
595 paint_view=AcquireCacheView(paint_image);
cristyb5d5f722009-11-04 03:03:49 +0000596#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000597 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000598#endif
cristybb503372010-05-27 20:51:26 +0000599 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000600 {
cristy4c08aed2011-07-01 19:47:50 +0000601 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000602 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000603
cristy4c08aed2011-07-01 19:47:50 +0000604 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000605 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000606
cristybb503372010-05-27 20:51:26 +0000607 register size_t
cristy3ed852e2009-09-05 21:47:34 +0000608 *histogram;
609
cristy14973ba2011-08-27 23:48:07 +0000610 register ssize_t
611 x;
612
cristy3ed852e2009-09-05 21:47:34 +0000613 if (status == MagickFalse)
614 continue;
cristyfe4ba002011-02-28 14:54:12 +0000615 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
616 (width/2L),image->columns+width,width,exception);
cristy3ed852e2009-09-05 21:47:34 +0000617 q=QueueCacheViewAuthenticPixels(paint_view,0,y,paint_image->columns,1,
618 exception);
cristy4c08aed2011-07-01 19:47:50 +0000619 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000620 {
621 status=MagickFalse;
622 continue;
623 }
cristy3ed852e2009-09-05 21:47:34 +0000624 histogram=histograms[GetOpenMPThreadId()];
cristybb503372010-05-27 20:51:26 +0000625 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000626 {
cristybb503372010-05-27 20:51:26 +0000627 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000628 i,
629 u;
630
cristybb503372010-05-27 20:51:26 +0000631 size_t
cristy3ed852e2009-09-05 21:47:34 +0000632 count;
633
cristy9d314ff2011-03-09 01:30:28 +0000634 ssize_t
635 j,
636 k,
637 v;
638
cristy3ed852e2009-09-05 21:47:34 +0000639 /*
640 Assign most frequent color.
641 */
642 i=0;
643 j=0;
644 count=0;
645 (void) ResetMagickMemory(histogram,0,NumberPaintBins*sizeof(*histogram));
cristybb503372010-05-27 20:51:26 +0000646 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +0000647 {
cristybb503372010-05-27 20:51:26 +0000648 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +0000649 {
cristy14973ba2011-08-27 23:48:07 +0000650 k=(ssize_t) ScaleQuantumToChar(GetPixelIntensity(image,p+
651 GetPixelChannels(image)*(u+i)));
cristy3ed852e2009-09-05 21:47:34 +0000652 histogram[k]++;
653 if (histogram[k] > count)
654 {
655 j=i+u;
656 count=histogram[k];
657 }
658 }
cristyd99b0962010-05-29 23:14:26 +0000659 i+=(ssize_t) (image->columns+width);
cristy3ed852e2009-09-05 21:47:34 +0000660 }
cristy14973ba2011-08-27 23:48:07 +0000661 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
662 SetPixelRed(paint_image,GetPixelRed(image,p+j*
663 GetPixelChannels(image)),q);
664 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
665 SetPixelGreen(paint_image,GetPixelGreen(image,p+j*
666 GetPixelChannels(image)),q);
667 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
668 SetPixelBlue(paint_image,GetPixelBlue(image,p+j*
669 GetPixelChannels(image)),q);
670 if ((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000671 SetPixelBlack(paint_image,GetPixelBlack(image,p+j*
cristyed231572011-07-14 02:18:59 +0000672 GetPixelChannels(image)),q);
cristy14973ba2011-08-27 23:48:07 +0000673 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000674 SetPixelAlpha(paint_image,GetPixelAlpha(image,p+j*
cristyed231572011-07-14 02:18:59 +0000675 GetPixelChannels(image)),q);
676 p+=GetPixelChannels(image);
677 q+=GetPixelChannels(paint_image);
cristy3ed852e2009-09-05 21:47:34 +0000678 }
679 if (SyncCacheViewAuthenticPixels(paint_view,exception) == MagickFalse)
680 status=MagickFalse;
681 if (image->progress_monitor != (MagickProgressMonitor) NULL)
682 {
683 MagickBooleanType
684 proceed;
685
cristyb5d5f722009-11-04 03:03:49 +0000686#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000687 #pragma omp critical (MagickCore_OilPaintImage)
688#endif
689 proceed=SetImageProgress(image,OilPaintImageTag,progress++,image->rows);
690 if (proceed == MagickFalse)
691 status=MagickFalse;
692 }
693 }
694 paint_view=DestroyCacheView(paint_view);
695 image_view=DestroyCacheView(image_view);
696 histograms=DestroyHistogramThreadSet(histograms);
697 if (status == MagickFalse)
698 paint_image=DestroyImage(paint_image);
699 return(paint_image);
700}
701
702/*
703%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
704% %
705% %
706% %
707% O p a q u e P a i n t I m a g e %
708% %
709% %
710% %
711%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
712%
713% OpaquePaintImage() changes any pixel that matches color with the color
714% defined by fill.
715%
cristy14973ba2011-08-27 23:48:07 +0000716% By default color must match a particular pixel color exactly. However, in
717% many cases two colors may differ by a small amount. Fuzz defines how much
718% tolerance is acceptable to consider two colors as the same. For example,
719% set fuzz to 10 and the color red at intensities of 100 and 102 respectively
720% are now interpreted as the same color.
cristy3ed852e2009-09-05 21:47:34 +0000721%
722% The format of the OpaquePaintImage method is:
723%
724% MagickBooleanType OpaquePaintImage(Image *image,
cristy101ab702011-10-13 13:06:32 +0000725% const PixelInfo *target,const PixelInfo *fill,
cristy189e84c2011-08-27 18:08:53 +0000726% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000727%
728% A description of each parameter follows:
729%
730% o image: the image.
731%
cristy3ed852e2009-09-05 21:47:34 +0000732% o target: the RGB value of the target color.
733%
734% o fill: the replacement color.
735%
736% o invert: paint any pixel that does not match the target color.
737%
cristy189e84c2011-08-27 18:08:53 +0000738% o exception: return any errors or warnings in this structure.
739%
cristy3ed852e2009-09-05 21:47:34 +0000740*/
cristy3ed852e2009-09-05 21:47:34 +0000741MagickExport MagickBooleanType OpaquePaintImage(Image *image,
cristy189e84c2011-08-27 18:08:53 +0000742 const PixelInfo *target,const PixelInfo *fill,const MagickBooleanType invert,
743 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000744{
745#define OpaquePaintImageTag "Opaque/Image"
746
cristyc4c8d132010-01-07 01:58:38 +0000747 CacheView
748 *image_view;
749
cristy3ed852e2009-09-05 21:47:34 +0000750 MagickBooleanType
751 status;
752
cristybb503372010-05-27 20:51:26 +0000753 MagickOffsetType
754 progress;
755
cristy4c08aed2011-07-01 19:47:50 +0000756 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000757 zero;
758
cristybb503372010-05-27 20:51:26 +0000759 ssize_t
760 y;
761
cristy3ed852e2009-09-05 21:47:34 +0000762 assert(image != (Image *) NULL);
763 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +0000764 assert(target != (PixelInfo *) NULL);
765 assert(fill != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000766 if (image->debug != MagickFalse)
767 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy574cc262011-08-05 01:23:58 +0000768 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000769 return(MagickFalse);
770 /*
771 Make image color opaque.
772 */
773 status=MagickTrue;
774 progress=0;
cristy4c08aed2011-07-01 19:47:50 +0000775 GetPixelInfo(image,&zero);
cristy3ed852e2009-09-05 21:47:34 +0000776 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +0000777#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000778 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000779#endif
cristybb503372010-05-27 20:51:26 +0000780 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000781 {
cristy4c08aed2011-07-01 19:47:50 +0000782 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000783 pixel;
784
cristy4c08aed2011-07-01 19:47:50 +0000785 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000786 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000787
cristy14973ba2011-08-27 23:48:07 +0000788 register ssize_t
789 x;
790
cristy3ed852e2009-09-05 21:47:34 +0000791 if (status == MagickFalse)
792 continue;
793 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy14973ba2011-08-27 23:48:07 +0000794 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000795 {
796 status=MagickFalse;
797 continue;
798 }
cristy3ed852e2009-09-05 21:47:34 +0000799 pixel=zero;
cristybb503372010-05-27 20:51:26 +0000800 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000801 {
cristy803640d2011-11-17 02:11:32 +0000802 GetPixelInfoPixel(image,q,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000803 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
cristy3ed852e2009-09-05 21:47:34 +0000804 {
cristyed231572011-07-14 02:18:59 +0000805 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000806 SetPixelRed(image,ClampToQuantum(fill->red),q);
cristyed231572011-07-14 02:18:59 +0000807 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000808 SetPixelGreen(image,ClampToQuantum(fill->green),q);
cristyed231572011-07-14 02:18:59 +0000809 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000810 SetPixelBlue(image,ClampToQuantum(fill->blue),q);
cristy14973ba2011-08-27 23:48:07 +0000811 if ((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000812 SetPixelBlack(image,ClampToQuantum(fill->black),q);
cristyed231572011-07-14 02:18:59 +0000813 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000814 SetPixelAlpha(image,ClampToQuantum(fill->alpha),q);
cristy3ed852e2009-09-05 21:47:34 +0000815 }
cristyed231572011-07-14 02:18:59 +0000816 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000817 }
818 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
819 status=MagickFalse;
820 if (image->progress_monitor != (MagickProgressMonitor) NULL)
821 {
822 MagickBooleanType
823 proceed;
824
cristyb5d5f722009-11-04 03:03:49 +0000825#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyd42d9952011-07-08 14:21:50 +0000826 #pragma omp critical (MagickCore_OpaquePaintImage)
cristy3ed852e2009-09-05 21:47:34 +0000827#endif
828 proceed=SetImageProgress(image,OpaquePaintImageTag,progress++,
829 image->rows);
830 if (proceed == MagickFalse)
831 status=MagickFalse;
832 }
833 }
834 image_view=DestroyCacheView(image_view);
835 return(status);
836}
837
838/*
839%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
840% %
841% %
842% %
843% T r a n s p a r e n t P a i n t I m a g e %
844% %
845% %
846% %
847%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
848%
849% TransparentPaintImage() changes the opacity value associated with any pixel
850% that matches color to the value defined by opacity.
851%
cristy14973ba2011-08-27 23:48:07 +0000852% By default color must match a particular pixel color exactly. However, in
853% many cases two colors may differ by a small amount. Fuzz defines how much
854% tolerance is acceptable to consider two colors as the same. For example,
855% set fuzz to 10 and the color red at intensities of 100 and 102 respectively
856% are now interpreted as the same color.
cristy3ed852e2009-09-05 21:47:34 +0000857%
858% The format of the TransparentPaintImage method is:
859%
860% MagickBooleanType TransparentPaintImage(Image *image,
cristy4c08aed2011-07-01 19:47:50 +0000861% const PixelInfo *target,const Quantum opacity,
cristy189e84c2011-08-27 18:08:53 +0000862% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000863%
864% A description of each parameter follows:
865%
866% o image: the image.
867%
868% o target: the target color.
869%
870% o opacity: the replacement opacity value.
871%
872% o invert: paint any pixel that does not match the target color.
873%
cristy189e84c2011-08-27 18:08:53 +0000874% o exception: return any errors or warnings in this structure.
875%
cristy3ed852e2009-09-05 21:47:34 +0000876*/
877MagickExport MagickBooleanType TransparentPaintImage(Image *image,
cristy14973ba2011-08-27 23:48:07 +0000878 const PixelInfo *target,const Quantum opacity,const MagickBooleanType invert,
879 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000880{
881#define TransparentPaintImageTag "Transparent/Image"
882
cristyc4c8d132010-01-07 01:58:38 +0000883 CacheView
884 *image_view;
885
cristy3ed852e2009-09-05 21:47:34 +0000886 MagickBooleanType
887 status;
888
cristybb503372010-05-27 20:51:26 +0000889 MagickOffsetType
890 progress;
891
cristy4c08aed2011-07-01 19:47:50 +0000892 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000893 zero;
894
cristybb503372010-05-27 20:51:26 +0000895 ssize_t
896 y;
897
cristy3ed852e2009-09-05 21:47:34 +0000898 assert(image != (Image *) NULL);
899 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +0000900 assert(target != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000901 if (image->debug != MagickFalse)
902 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy574cc262011-08-05 01:23:58 +0000903 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000904 return(MagickFalse);
905 if (image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +0000906 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +0000907 /*
908 Make image color transparent.
909 */
910 status=MagickTrue;
911 progress=0;
cristy4c08aed2011-07-01 19:47:50 +0000912 GetPixelInfo(image,&zero);
cristy3ed852e2009-09-05 21:47:34 +0000913 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +0000914#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000915 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000916#endif
cristybb503372010-05-27 20:51:26 +0000917 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000918 {
cristy4c08aed2011-07-01 19:47:50 +0000919 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000920 pixel;
921
cristybb503372010-05-27 20:51:26 +0000922 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000923 x;
924
cristy4c08aed2011-07-01 19:47:50 +0000925 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000926 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000927
928 if (status == MagickFalse)
929 continue;
930 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy14973ba2011-08-27 23:48:07 +0000931 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000932 {
933 status=MagickFalse;
934 continue;
935 }
cristy3ed852e2009-09-05 21:47:34 +0000936 pixel=zero;
cristybb503372010-05-27 20:51:26 +0000937 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000938 {
cristy803640d2011-11-17 02:11:32 +0000939 GetPixelInfoPixel(image,q,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000940 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
941 SetPixelAlpha(image,opacity,q);
cristyed231572011-07-14 02:18:59 +0000942 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000943 }
944 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
945 status=MagickFalse;
946 if (image->progress_monitor != (MagickProgressMonitor) NULL)
947 {
948 MagickBooleanType
949 proceed;
950
cristyb5d5f722009-11-04 03:03:49 +0000951#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000952 #pragma omp critical (MagickCore_TransparentPaintImage)
953#endif
954 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
955 image->rows);
956 if (proceed == MagickFalse)
957 status=MagickFalse;
958 }
959 }
960 image_view=DestroyCacheView(image_view);
961 return(status);
962}
963
964/*
965%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
966% %
967% %
968% %
969% 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 %
970% %
971% %
972% %
973%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
974%
975% TransparentPaintImageChroma() changes the opacity value associated with any
976% pixel that matches color to the value defined by opacity.
977%
cristy14973ba2011-08-27 23:48:07 +0000978% As there is one fuzz value for the all the channels, TransparentPaintImage()
979% is not suitable for the operations like chroma, where the tolerance for
980% similarity of two color component (RGB) can be different. Thus we define
981% this method to take two target pixels (one low and one high) and all the
982% pixels of an image which are lying between these two pixels are made
983% transparent.
cristy3ed852e2009-09-05 21:47:34 +0000984%
cristy14973ba2011-08-27 23:48:07 +0000985% The format of the TransparentPaintImageChroma method is:
cristy3ed852e2009-09-05 21:47:34 +0000986%
cristy14973ba2011-08-27 23:48:07 +0000987% MagickBooleanType TransparentPaintImageChroma(Image *image,
988% const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
989% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000990%
991% A description of each parameter follows:
992%
993% o image: the image.
994%
995% o low: the low target color.
996%
997% o high: the high target color.
998%
999% o opacity: the replacement opacity value.
1000%
1001% o invert: paint any pixel that does not match the target color.
1002%
cristy189e84c2011-08-27 18:08:53 +00001003% o exception: return any errors or warnings in this structure.
1004%
cristy3ed852e2009-09-05 21:47:34 +00001005*/
1006MagickExport MagickBooleanType TransparentPaintImageChroma(Image *image,
cristy189e84c2011-08-27 18:08:53 +00001007 const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
1008 const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001009{
1010#define TransparentPaintImageTag "Transparent/Image"
1011
cristyc4c8d132010-01-07 01:58:38 +00001012 CacheView
1013 *image_view;
1014
cristy3ed852e2009-09-05 21:47:34 +00001015 MagickBooleanType
1016 status;
1017
cristybb503372010-05-27 20:51:26 +00001018 MagickOffsetType
1019 progress;
1020
1021 ssize_t
1022 y;
1023
cristy3ed852e2009-09-05 21:47:34 +00001024 assert(image != (Image *) NULL);
1025 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +00001026 assert(high != (PixelInfo *) NULL);
1027 assert(low != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001028 if (image->debug != MagickFalse)
1029 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy574cc262011-08-05 01:23:58 +00001030 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001031 return(MagickFalse);
1032 if (image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00001033 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00001034 /*
1035 Make image color transparent.
1036 */
1037 status=MagickTrue;
1038 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00001039 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00001040#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00001041 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00001042#endif
cristybb503372010-05-27 20:51:26 +00001043 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001044 {
1045 MagickBooleanType
1046 match;
1047
cristy4c08aed2011-07-01 19:47:50 +00001048 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001049 pixel;
1050
cristy4c08aed2011-07-01 19:47:50 +00001051 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001052 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001053
cristy14973ba2011-08-27 23:48:07 +00001054 register ssize_t
1055 x;
1056
cristy3ed852e2009-09-05 21:47:34 +00001057 if (status == MagickFalse)
1058 continue;
1059 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy14973ba2011-08-27 23:48:07 +00001060 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001061 {
1062 status=MagickFalse;
1063 continue;
1064 }
cristy4c08aed2011-07-01 19:47:50 +00001065 GetPixelInfo(image,&pixel);
cristybb503372010-05-27 20:51:26 +00001066 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001067 {
cristy803640d2011-11-17 02:11:32 +00001068 GetPixelInfoPixel(image,q,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001069 match=((pixel.red >= low->red) && (pixel.red <= high->red) &&
1070 (pixel.green >= low->green) && (pixel.green <= high->green) &&
cristy14973ba2011-08-27 23:48:07 +00001071 (pixel.blue >= low->blue) && (pixel.blue <= high->blue)) ? MagickTrue :
1072 MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00001073 if (match != invert)
cristy4c08aed2011-07-01 19:47:50 +00001074 SetPixelAlpha(image,opacity,q);
cristyed231572011-07-14 02:18:59 +00001075 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001076 }
1077 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1078 status=MagickFalse;
1079 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1080 {
1081 MagickBooleanType
1082 proceed;
1083
cristyb5d5f722009-11-04 03:03:49 +00001084#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00001085 #pragma omp critical (MagickCore_TransparentPaintImageChroma)
1086#endif
1087 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
1088 image->rows);
1089 if (proceed == MagickFalse)
1090 status=MagickFalse;
1091 }
1092 }
1093 image_view=DestroyCacheView(image_view);
1094 return(status);
1095}