blob: 531cb270d9ff6cd4521de7aac5c432ef2d95470b [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"
cristy95f562a2012-01-01 20:49:11 +000058#include "MagickCore/statistic.h"
cristy4c08aed2011-07-01 19:47:50 +000059#include "MagickCore/string_.h"
60#include "MagickCore/thread-private.h"
cristy3ed852e2009-09-05 21:47:34 +000061
cristy3ed852e2009-09-05 21:47:34 +000062/*
63%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
64% %
65% %
66% %
67% F l o o d f i l l P a i n t I m a g e %
68% %
69% %
70% %
71%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72%
73% FloodfillPaintImage() changes the color value of any pixel that matches
74% target and is an immediate neighbor. If the method FillToBorderMethod is
75% specified, the color value is changed for any neighbor pixel that does not
76% match the bordercolor member of image.
77%
cristy908a0002011-08-28 00:13:39 +000078% By default target must match a particular pixel color exactly. However,
79% in many cases two colors may differ by a small amount. The fuzz member of
80% image defines how much tolerance is acceptable to consider two colors as
81% the same. For example, set fuzz to 10 and the color red at intensities of
82% 100 and 102 respectively are now interpreted as the same color for the
83% purposes of the floodfill.
cristy3ed852e2009-09-05 21:47:34 +000084%
85% The format of the FloodfillPaintImage method is:
86%
87% MagickBooleanType FloodfillPaintImage(Image *image,
cristyd42d9952011-07-08 14:21:50 +000088% const DrawInfo *draw_info,const PixelInfo target,
89% const ssize_t x_offset,const ssize_t y_offset,
cristy189e84c2011-08-27 18:08:53 +000090% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +000091%
92% A description of each parameter follows:
93%
94% o image: the image.
95%
cristy3ed852e2009-09-05 21:47:34 +000096% o draw_info: the draw info.
97%
98% o target: the RGB value of the target color.
99%
100% o x_offset,y_offset: the starting location of the operation.
101%
102% o invert: paint any pixel that does not match the target color.
103%
cristy189e84c2011-08-27 18:08:53 +0000104% o exception: return any errors or warnings in this structure.
105%
cristy3ed852e2009-09-05 21:47:34 +0000106*/
107MagickExport MagickBooleanType FloodfillPaintImage(Image *image,
cristyd42d9952011-07-08 14:21:50 +0000108 const DrawInfo *draw_info,const PixelInfo *target,const ssize_t x_offset,
cristy189e84c2011-08-27 18:08:53 +0000109 const ssize_t y_offset,const MagickBooleanType invert,
110 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000111{
112#define MaxStacksize (1UL << 15)
113#define PushSegmentStack(up,left,right,delta) \
114{ \
115 if (s >= (segment_stack+MaxStacksize)) \
116 ThrowBinaryException(DrawError,"SegmentStackOverflow",image->filename) \
117 else \
118 { \
cristybb503372010-05-27 20:51:26 +0000119 if ((((up)+(delta)) >= 0) && (((up)+(delta)) < (ssize_t) image->rows)) \
cristy3ed852e2009-09-05 21:47:34 +0000120 { \
121 s->x1=(double) (left); \
122 s->y1=(double) (up); \
123 s->x2=(double) (right); \
124 s->y2=(double) (delta); \
125 s++; \
126 } \
127 } \
128}
129
cristyb0d3bb92010-09-22 14:37:58 +0000130 CacheView
131 *floodplane_view,
132 *image_view;
133
cristy3ed852e2009-09-05 21:47:34 +0000134 Image
135 *floodplane_image;
136
cristy3ed852e2009-09-05 21:47:34 +0000137 MagickBooleanType
cristy14973ba2011-08-27 23:48:07 +0000138 skip,
139 status;
cristy3ed852e2009-09-05 21:47:34 +0000140
cristy4c08aed2011-07-01 19:47:50 +0000141 PixelInfo
cristyfdcc0182011-12-26 19:33:56 +0000142 fill_color,
cristy3ed852e2009-09-05 21:47:34 +0000143 pixel;
144
cristy3ed852e2009-09-05 21:47:34 +0000145 register SegmentInfo
146 *s;
147
148 SegmentInfo
149 *segment_stack;
150
cristy9d314ff2011-03-09 01:30:28 +0000151 ssize_t
152 offset,
153 start,
154 x,
155 x1,
156 x2,
157 y;
158
cristy3ed852e2009-09-05 21:47:34 +0000159 /*
160 Check boundary conditions.
161 */
162 assert(image != (Image *) NULL);
163 assert(image->signature == MagickSignature);
164 if (image->debug != MagickFalse)
165 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
166 assert(draw_info != (DrawInfo *) NULL);
167 assert(draw_info->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +0000168 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
cristy3ed852e2009-09-05 21:47:34 +0000169 return(MagickFalse);
cristybb503372010-05-27 20:51:26 +0000170 if ((y_offset < 0) || (y_offset >= (ssize_t) image->rows))
cristy3ed852e2009-09-05 21:47:34 +0000171 return(MagickFalse);
cristy574cc262011-08-05 01:23:58 +0000172 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000173 return(MagickFalse);
cristy5b67d4e2012-02-07 19:43:53 +0000174 if ((target->matte != MagickFalse) &&
175 (image->matte == MagickFalse))
176 (void) SetImageAlpha(image,OpaqueAlpha,exception);
cristy3ed852e2009-09-05 21:47:34 +0000177 if (image->matte == MagickFalse)
cristy5b67d4e2012-02-07 19:43:53 +0000178 (void) SetImageAlpha(image,OpaqueAlpha,exception);
cristy3ed852e2009-09-05 21:47:34 +0000179 /*
180 Set floodfill state.
181 */
cristy95f562a2012-01-01 20:49:11 +0000182 floodplane_image=CloneImage(image,image->columns,image->rows,MagickTrue,
183 exception);
cristy3ed852e2009-09-05 21:47:34 +0000184 if (floodplane_image == (Image *) NULL)
185 return(MagickFalse);
cristy95f562a2012-01-01 20:49:11 +0000186 floodplane_image->colorspace=GRAYColorspace;
187 (void) EvaluateImage(floodplane_image,SetEvaluateOperator,0.0,exception);
cristy3ed852e2009-09-05 21:47:34 +0000188 segment_stack=(SegmentInfo *) AcquireQuantumMemory(MaxStacksize,
189 sizeof(*segment_stack));
190 if (segment_stack == (SegmentInfo *) NULL)
191 {
192 floodplane_image=DestroyImage(floodplane_image);
193 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
194 image->filename);
195 }
196 /*
197 Push initial segment on stack.
198 */
cristy14973ba2011-08-27 23:48:07 +0000199 status=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +0000200 x=x_offset;
201 y=y_offset;
202 start=0;
203 s=segment_stack;
204 PushSegmentStack(y,x,x,1);
205 PushSegmentStack(y+1,x,x,-1);
cristy4c08aed2011-07-01 19:47:50 +0000206 GetPixelInfo(image,&pixel);
cristyb0d3bb92010-09-22 14:37:58 +0000207 image_view=AcquireCacheView(image);
208 floodplane_view=AcquireCacheView(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000209 while (s > segment_stack)
210 {
cristy4c08aed2011-07-01 19:47:50 +0000211 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000212 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000213
cristy4c08aed2011-07-01 19:47:50 +0000214 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000215 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000216
cristy14973ba2011-08-27 23:48:07 +0000217 register ssize_t
218 x;
219
cristy3ed852e2009-09-05 21:47:34 +0000220 /*
221 Pop segment off stack.
222 */
223 s--;
cristybb503372010-05-27 20:51:26 +0000224 x1=(ssize_t) s->x1;
225 x2=(ssize_t) s->x2;
226 offset=(ssize_t) s->y2;
227 y=(ssize_t) s->y1+offset;
cristy3ed852e2009-09-05 21:47:34 +0000228 /*
229 Recolor neighboring pixels.
230 */
cristyb0d3bb92010-09-22 14:37:58 +0000231 p=GetCacheViewVirtualPixels(image_view,0,y,(size_t) (x1+1),1,exception);
232 q=GetCacheViewAuthenticPixels(floodplane_view,0,y,(size_t) (x1+1),1,
cristy3ed852e2009-09-05 21:47:34 +0000233 exception);
cristy4c08aed2011-07-01 19:47:50 +0000234 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000235 break;
cristyed231572011-07-14 02:18:59 +0000236 p+=x1*GetPixelChannels(image);
237 q+=x1*GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000238 for (x=x1; x >= 0; x--)
239 {
cristy95f562a2012-01-01 20:49:11 +0000240 if (GetPixelGray(floodplane_image,q) != 0)
cristy3ed852e2009-09-05 21:47:34 +0000241 break;
cristy803640d2011-11-17 02:11:32 +0000242 GetPixelInfoPixel(image,p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000243 if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert)
cristy3ed852e2009-09-05 21:47:34 +0000244 break;
cristy95f562a2012-01-01 20:49:11 +0000245 SetPixelGray(floodplane_image,QuantumRange,q);
cristyed231572011-07-14 02:18:59 +0000246 p-=GetPixelChannels(image);
247 q-=GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000248 }
cristyb0d3bb92010-09-22 14:37:58 +0000249 if (SyncCacheViewAuthenticPixels(floodplane_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000250 break;
251 skip=x >= x1 ? MagickTrue : MagickFalse;
252 if (skip == MagickFalse)
253 {
254 start=x+1;
255 if (start < x1)
256 PushSegmentStack(y,start,x1-1,-offset);
257 x=x1+1;
258 }
259 do
260 {
261 if (skip == MagickFalse)
262 {
cristybb503372010-05-27 20:51:26 +0000263 if (x < (ssize_t) image->columns)
cristy3ed852e2009-09-05 21:47:34 +0000264 {
cristyb0d3bb92010-09-22 14:37:58 +0000265 p=GetCacheViewVirtualPixels(image_view,x,y,image->columns-x,1,
cristy3ed852e2009-09-05 21:47:34 +0000266 exception);
cristyfdcc0182011-12-26 19:33:56 +0000267 q=GetCacheViewAuthenticPixels(floodplane_view,x,y,image->columns-
268 x,1,exception);
cristy636dcb52011-08-26 13:23:49 +0000269 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000270 break;
cristybb503372010-05-27 20:51:26 +0000271 for ( ; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000272 {
cristy95f562a2012-01-01 20:49:11 +0000273 if (GetPixelGray(floodplane_image,q) != 0)
cristy3ed852e2009-09-05 21:47:34 +0000274 break;
cristy803640d2011-11-17 02:11:32 +0000275 GetPixelInfoPixel(image,p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000276 if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert)
cristy3ed852e2009-09-05 21:47:34 +0000277 break;
cristy95f562a2012-01-01 20:49:11 +0000278 SetPixelGray(floodplane_image,QuantumRange,q);
cristyed231572011-07-14 02:18:59 +0000279 p+=GetPixelChannels(image);
280 q+=GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000281 }
cristy14973ba2011-08-27 23:48:07 +0000282 status=SyncCacheViewAuthenticPixels(floodplane_view,exception);
283 if (status == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000284 break;
285 }
286 PushSegmentStack(y,start,x-1,offset);
287 if (x > (x2+1))
288 PushSegmentStack(y,x2+1,x-1,-offset);
289 }
290 skip=MagickFalse;
291 x++;
292 if (x <= x2)
293 {
cristyb0d3bb92010-09-22 14:37:58 +0000294 p=GetCacheViewVirtualPixels(image_view,x,y,(size_t) (x2-x+1),1,
295 exception);
296 q=GetCacheViewAuthenticPixels(floodplane_view,x,y,(size_t) (x2-x+1),1,
cristy3ed852e2009-09-05 21:47:34 +0000297 exception);
cristy636dcb52011-08-26 13:23:49 +0000298 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000299 break;
cristy3ed852e2009-09-05 21:47:34 +0000300 for ( ; x <= x2; x++)
301 {
cristy95f562a2012-01-01 20:49:11 +0000302 if (GetPixelGray(floodplane_image,q) != 0)
cristy3ed852e2009-09-05 21:47:34 +0000303 break;
cristy803640d2011-11-17 02:11:32 +0000304 GetPixelInfoPixel(image,p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000305 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
cristy3ed852e2009-09-05 21:47:34 +0000306 break;
cristyed231572011-07-14 02:18:59 +0000307 p+=GetPixelChannels(image);
308 q+=GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000309 }
310 }
311 start=x;
312 } while (x <= x2);
313 }
cristybb503372010-05-27 20:51:26 +0000314 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000315 {
cristy4c08aed2011-07-01 19:47:50 +0000316 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000317 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000318
cristy4c08aed2011-07-01 19:47:50 +0000319 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000320 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000321
cristy14973ba2011-08-27 23:48:07 +0000322 register ssize_t
323 x;
324
cristy3ed852e2009-09-05 21:47:34 +0000325 /*
326 Tile fill color onto floodplane.
327 */
cristy95f562a2012-01-01 20:49:11 +0000328 p=GetCacheViewVirtualPixels(floodplane_view,0,y,image->columns,1,exception);
cristyb0d3bb92010-09-22 14:37:58 +0000329 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000330 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000331 break;
cristybb503372010-05-27 20:51:26 +0000332 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000333 {
cristy95f562a2012-01-01 20:49:11 +0000334 if (GetPixelGray(floodplane_image,p) != 0)
cristy3ed852e2009-09-05 21:47:34 +0000335 {
cristy2ed42f62011-10-02 19:49:57 +0000336 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
cristy8a20fa02011-12-27 15:54:31 +0000337 SetPixelInfoPixel(image,&fill_color,q);
cristy3ed852e2009-09-05 21:47:34 +0000338 }
cristyed231572011-07-14 02:18:59 +0000339 p+=GetPixelChannels(floodplane_image);
340 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000341 }
cristyb0d3bb92010-09-22 14:37:58 +0000342 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000343 break;
344 }
cristyb0d3bb92010-09-22 14:37:58 +0000345 floodplane_view=DestroyCacheView(floodplane_view);
346 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +0000347 segment_stack=(SegmentInfo *) RelinquishMagickMemory(segment_stack);
348 floodplane_image=DestroyImage(floodplane_image);
cristybb503372010-05-27 20:51:26 +0000349 return(y == (ssize_t) image->rows ? MagickTrue : MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +0000350}
351
352/*
353%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
354% %
355% %
356% %
357+ G r a d i e n t I m a g e %
358% %
359% %
360% %
361%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
362%
cristycee97112010-05-28 00:44:52 +0000363% GradientImage() applies a continuously smooth color transitions along a
cristy3ed852e2009-09-05 21:47:34 +0000364% vector from one color to another.
365%
366% Note, the interface of this method will change in the future to support
367% more than one transistion.
368%
369% The format of the GradientImage method is:
370%
371% MagickBooleanType GradientImage(Image *image,const GradientType type,
cristy101ab702011-10-13 13:06:32 +0000372% const SpreadMethod method,const PixelInfo *start_color,
373% const PixelInfo *stop_color,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000374%
375% A description of each parameter follows:
376%
377% o image: the image.
378%
379% o type: the gradient type: linear or radial.
380%
381% o spread: the gradient spread meathod: pad, reflect, or repeat.
382%
383% o start_color: the start color.
384%
385% o stop_color: the stop color.
386%
cristy189e84c2011-08-27 18:08:53 +0000387% o exception: return any errors or warnings in this structure.
388%
cristy3ed852e2009-09-05 21:47:34 +0000389*/
cristy117ff172010-08-15 21:35:32 +0000390
391static inline double MagickMax(const double x,const double y)
392{
393 return(x > y ? x : y);
394}
395
cristy3ed852e2009-09-05 21:47:34 +0000396MagickExport MagickBooleanType GradientImage(Image *image,
397 const GradientType type,const SpreadMethod method,
cristy101ab702011-10-13 13:06:32 +0000398 const PixelInfo *start_color,const PixelInfo *stop_color,
cristy189e84c2011-08-27 18:08:53 +0000399 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000400{
401 DrawInfo
402 *draw_info;
403
404 GradientInfo
405 *gradient;
406
407 MagickBooleanType
408 status;
409
cristybb503372010-05-27 20:51:26 +0000410 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000411 i;
412
413 /*
414 Set gradient start-stop end points.
415 */
416 assert(image != (const Image *) NULL);
417 assert(image->signature == MagickSignature);
418 if (image->debug != MagickFalse)
419 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy101ab702011-10-13 13:06:32 +0000420 assert(start_color != (const PixelInfo *) NULL);
421 assert(stop_color != (const PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000422 draw_info=AcquireDrawInfo();
423 gradient=(&draw_info->gradient);
424 gradient->type=type;
425 gradient->bounding_box.width=image->columns;
426 gradient->bounding_box.height=image->rows;
427 gradient->gradient_vector.x2=(double) image->columns-1.0;
428 gradient->gradient_vector.y2=(double) image->rows-1.0;
429 if ((type == LinearGradient) && (gradient->gradient_vector.y2 != 0.0))
430 gradient->gradient_vector.x2=0.0;
431 gradient->center.x=(double) gradient->gradient_vector.x2/2.0;
432 gradient->center.y=(double) gradient->gradient_vector.y2/2.0;
433 gradient->radius=MagickMax(gradient->center.x,gradient->center.y);
434 gradient->spread=method;
435 /*
436 Define the gradient to fill between the stops.
437 */
438 gradient->number_stops=2;
439 gradient->stops=(StopInfo *) AcquireQuantumMemory(gradient->number_stops,
440 sizeof(*gradient->stops));
441 if (gradient->stops == (StopInfo *) NULL)
442 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
443 image->filename);
444 (void) ResetMagickMemory(gradient->stops,0,gradient->number_stops*
445 sizeof(*gradient->stops));
cristybb503372010-05-27 20:51:26 +0000446 for (i=0; i < (ssize_t) gradient->number_stops; i++)
cristy4c08aed2011-07-01 19:47:50 +0000447 GetPixelInfo(image,&gradient->stops[i].color);
cristy9d8c8ce2011-10-25 16:13:52 +0000448 gradient->stops[0].color=(*start_color);
cristy3ed852e2009-09-05 21:47:34 +0000449 gradient->stops[0].offset=0.0;
cristy9d8c8ce2011-10-25 16:13:52 +0000450 gradient->stops[1].color=(*stop_color);
cristy3ed852e2009-09-05 21:47:34 +0000451 gradient->stops[1].offset=1.0;
452 /*
453 Draw a gradient on the image.
454 */
cristy947cb4c2011-10-20 18:41:46 +0000455 status=DrawGradientImage(image,draw_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000456 draw_info=DestroyDrawInfo(draw_info);
cristy09250192012-02-07 18:53:31 +0000457 if ((start_color->matte == MagickFalse) && (stop_color->matte == MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +0000458 image->matte=MagickFalse;
cristy101ab702011-10-13 13:06:32 +0000459 if ((IsPixelInfoGray(start_color) != MagickFalse) &&
460 (IsPixelInfoGray(stop_color) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +0000461 image->type=GrayscaleType;
462 return(status);
463}
464
465/*
466%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
467% %
468% %
469% %
470% O i l P a i n t I m a g e %
471% %
472% %
473% %
474%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
475%
476% OilPaintImage() applies a special effect filter that simulates an oil
477% painting. Each pixel is replaced by the most frequent color occurring
478% in a circular region defined by radius.
479%
480% The format of the OilPaintImage method is:
481%
482% Image *OilPaintImage(const Image *image,const double radius,
cristy14973ba2011-08-27 23:48:07 +0000483% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000484%
485% A description of each parameter follows:
486%
487% o image: the image.
488%
489% o radius: the radius of the circular neighborhood.
490%
cristy14973ba2011-08-27 23:48:07 +0000491% o sigma: the standard deviation of the Gaussian, in pixels.
492%
cristy3ed852e2009-09-05 21:47:34 +0000493% o exception: return any errors or warnings in this structure.
494%
495*/
496
cristybb503372010-05-27 20:51:26 +0000497static size_t **DestroyHistogramThreadSet(size_t **histogram)
cristy3ed852e2009-09-05 21:47:34 +0000498{
cristybb503372010-05-27 20:51:26 +0000499 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000500 i;
501
cristybb503372010-05-27 20:51:26 +0000502 assert(histogram != (size_t **) NULL);
503 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
504 if (histogram[i] != (size_t *) NULL)
505 histogram[i]=(size_t *) RelinquishMagickMemory(histogram[i]);
cristyb41ee102010-10-04 16:46:15 +0000506 histogram=(size_t **) RelinquishMagickMemory(histogram);
cristy3ed852e2009-09-05 21:47:34 +0000507 return(histogram);
508}
509
cristybb503372010-05-27 20:51:26 +0000510static size_t **AcquireHistogramThreadSet(const size_t count)
cristy3ed852e2009-09-05 21:47:34 +0000511{
cristybb503372010-05-27 20:51:26 +0000512 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000513 i;
514
cristybb503372010-05-27 20:51:26 +0000515 size_t
cristy3ed852e2009-09-05 21:47:34 +0000516 **histogram,
517 number_threads;
518
519 number_threads=GetOpenMPMaximumThreads();
cristy14973ba2011-08-27 23:48:07 +0000520 histogram=(size_t **) AcquireQuantumMemory(number_threads,sizeof(*histogram));
cristybb503372010-05-27 20:51:26 +0000521 if (histogram == (size_t **) NULL)
522 return((size_t **) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000523 (void) ResetMagickMemory(histogram,0,number_threads*sizeof(*histogram));
cristybb503372010-05-27 20:51:26 +0000524 for (i=0; i < (ssize_t) number_threads; i++)
cristy3ed852e2009-09-05 21:47:34 +0000525 {
cristy14973ba2011-08-27 23:48:07 +0000526 histogram[i]=(size_t *) AcquireQuantumMemory(count,sizeof(**histogram));
cristybb503372010-05-27 20:51:26 +0000527 if (histogram[i] == (size_t *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000528 return(DestroyHistogramThreadSet(histogram));
529 }
530 return(histogram);
531}
532
533MagickExport Image *OilPaintImage(const Image *image,const double radius,
cristy14973ba2011-08-27 23:48:07 +0000534 const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000535{
536#define NumberPaintBins 256
537#define OilPaintImageTag "OilPaint/Image"
538
cristyfa112112010-01-04 17:48:07 +0000539 CacheView
540 *image_view,
541 *paint_view;
542
cristy3ed852e2009-09-05 21:47:34 +0000543 Image
544 *paint_image;
545
cristy3ed852e2009-09-05 21:47:34 +0000546 MagickBooleanType
547 status;
548
cristybb503372010-05-27 20:51:26 +0000549 MagickOffsetType
550 progress;
551
552 size_t
cristy14973ba2011-08-27 23:48:07 +0000553 **histograms,
cristy3ed852e2009-09-05 21:47:34 +0000554 width;
555
cristybb503372010-05-27 20:51:26 +0000556 ssize_t
cristyf8561542012-01-24 00:26:46 +0000557 center,
cristybb503372010-05-27 20:51:26 +0000558 y;
559
cristy3ed852e2009-09-05 21:47:34 +0000560 /*
561 Initialize painted image attributes.
562 */
563 assert(image != (const Image *) NULL);
564 assert(image->signature == MagickSignature);
565 if (image->debug != MagickFalse)
566 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
567 assert(exception != (ExceptionInfo *) NULL);
568 assert(exception->signature == MagickSignature);
cristy14973ba2011-08-27 23:48:07 +0000569 width=GetOptimalKernelWidth2D(radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +0000570 paint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
571 if (paint_image == (Image *) NULL)
572 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000573 if (SetImageStorageClass(paint_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000574 {
cristy3ed852e2009-09-05 21:47:34 +0000575 paint_image=DestroyImage(paint_image);
576 return((Image *) NULL);
577 }
578 histograms=AcquireHistogramThreadSet(NumberPaintBins);
cristybb503372010-05-27 20:51:26 +0000579 if (histograms == (size_t **) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000580 {
581 paint_image=DestroyImage(paint_image);
582 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
583 }
584 /*
585 Oil paint image.
586 */
587 status=MagickTrue;
588 progress=0;
cristyd09f8802012-02-04 16:44:10 +0000589 center=(ssize_t) GetPixelChannels(image)*(image->columns+width)*(width/2L)+
590 GetPixelChannels(image)*(width/2L);
cristy3ed852e2009-09-05 21:47:34 +0000591 image_view=AcquireCacheView(image);
592 paint_view=AcquireCacheView(paint_image);
cristyb5d5f722009-11-04 03:03:49 +0000593#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000594 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000595#endif
cristybb503372010-05-27 20:51:26 +0000596 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000597 {
cristy4c08aed2011-07-01 19:47:50 +0000598 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000599 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000600
cristy4c08aed2011-07-01 19:47:50 +0000601 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000602 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000603
cristybb503372010-05-27 20:51:26 +0000604 register size_t
cristy3ed852e2009-09-05 21:47:34 +0000605 *histogram;
606
cristy14973ba2011-08-27 23:48:07 +0000607 register ssize_t
608 x;
609
cristy3ed852e2009-09-05 21:47:34 +0000610 if (status == MagickFalse)
611 continue;
cristyfe4ba002011-02-28 14:54:12 +0000612 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
613 (width/2L),image->columns+width,width,exception);
cristy3ed852e2009-09-05 21:47:34 +0000614 q=QueueCacheViewAuthenticPixels(paint_view,0,y,paint_image->columns,1,
615 exception);
cristy4c08aed2011-07-01 19:47:50 +0000616 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000617 {
618 status=MagickFalse;
619 continue;
620 }
cristy3ed852e2009-09-05 21:47:34 +0000621 histogram=histograms[GetOpenMPThreadId()];
cristybb503372010-05-27 20:51:26 +0000622 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000623 {
cristybb503372010-05-27 20:51:26 +0000624 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000625 i,
626 u;
627
cristybb503372010-05-27 20:51:26 +0000628 size_t
cristy3ed852e2009-09-05 21:47:34 +0000629 count;
630
cristy9d314ff2011-03-09 01:30:28 +0000631 ssize_t
632 j,
633 k,
cristyf8561542012-01-24 00:26:46 +0000634 n,
cristy9d314ff2011-03-09 01:30:28 +0000635 v;
636
cristy3ed852e2009-09-05 21:47:34 +0000637 /*
638 Assign most frequent color.
639 */
cristyf8561542012-01-24 00:26:46 +0000640 k=0;
cristy3ed852e2009-09-05 21:47:34 +0000641 j=0;
642 count=0;
cristyf8561542012-01-24 00:26:46 +0000643 (void) ResetMagickMemory(histogram,0,NumberPaintBins* sizeof(*histogram));
cristybb503372010-05-27 20:51:26 +0000644 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +0000645 {
cristybb503372010-05-27 20:51:26 +0000646 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +0000647 {
cristyf8561542012-01-24 00:26:46 +0000648 n=(ssize_t) ScaleQuantumToChar(GetPixelIntensity(image,p+
649 GetPixelChannels(image)*(u+k)));
650 histogram[n]++;
651 if (histogram[n] > count)
cristy3ed852e2009-09-05 21:47:34 +0000652 {
cristyf8561542012-01-24 00:26:46 +0000653 j=k+u;
654 count=histogram[n];
cristy3ed852e2009-09-05 21:47:34 +0000655 }
656 }
cristyf8561542012-01-24 00:26:46 +0000657 k+=(ssize_t) (image->columns+width);
cristy3ed852e2009-09-05 21:47:34 +0000658 }
cristyf8561542012-01-24 00:26:46 +0000659 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
660 {
661 PixelChannel
662 channel;
663
664 PixelTrait
665 paint_traits,
666 traits;
667
668 channel=GetPixelChannelMapChannel(image,i);
669 traits=GetPixelChannelMapTraits(image,channel);
670 paint_traits=GetPixelChannelMapTraits(paint_image,channel);
671 if ((traits == UndefinedPixelTrait) ||
672 (paint_traits == UndefinedPixelTrait))
673 continue;
cristyd09f8802012-02-04 16:44:10 +0000674 if (((paint_traits & CopyPixelTrait) != 0) ||
675 (GetPixelMask(image,p) != 0))
cristyf8561542012-01-24 00:26:46 +0000676 {
677 SetPixelChannel(paint_image,channel,p[center+i],q);
678 continue;
679 }
680 SetPixelChannel(paint_image,channel,p[j*GetPixelChannels(image)+i],q);
681 }
cristyed231572011-07-14 02:18:59 +0000682 p+=GetPixelChannels(image);
683 q+=GetPixelChannels(paint_image);
cristy3ed852e2009-09-05 21:47:34 +0000684 }
685 if (SyncCacheViewAuthenticPixels(paint_view,exception) == MagickFalse)
686 status=MagickFalse;
687 if (image->progress_monitor != (MagickProgressMonitor) NULL)
688 {
689 MagickBooleanType
690 proceed;
691
cristyb5d5f722009-11-04 03:03:49 +0000692#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy95338b32012-01-22 03:34:36 +0000693 #pragma omp critical (MagickCore_OilPaintImage)
cristy3ed852e2009-09-05 21:47:34 +0000694#endif
695 proceed=SetImageProgress(image,OilPaintImageTag,progress++,image->rows);
696 if (proceed == MagickFalse)
697 status=MagickFalse;
698 }
699 }
700 paint_view=DestroyCacheView(paint_view);
701 image_view=DestroyCacheView(image_view);
702 histograms=DestroyHistogramThreadSet(histograms);
703 if (status == MagickFalse)
704 paint_image=DestroyImage(paint_image);
705 return(paint_image);
706}
707
708/*
709%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
710% %
711% %
712% %
713% O p a q u e P a i n t I m a g e %
714% %
715% %
716% %
717%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
718%
719% OpaquePaintImage() changes any pixel that matches color with the color
720% defined by fill.
721%
cristy14973ba2011-08-27 23:48:07 +0000722% By default color must match a particular pixel color exactly. However, in
723% many cases two colors may differ by a small amount. Fuzz defines how much
724% tolerance is acceptable to consider two colors as the same. For example,
725% set fuzz to 10 and the color red at intensities of 100 and 102 respectively
726% are now interpreted as the same color.
cristy3ed852e2009-09-05 21:47:34 +0000727%
728% The format of the OpaquePaintImage method is:
729%
730% MagickBooleanType OpaquePaintImage(Image *image,
cristy101ab702011-10-13 13:06:32 +0000731% const PixelInfo *target,const PixelInfo *fill,
cristy189e84c2011-08-27 18:08:53 +0000732% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000733%
734% A description of each parameter follows:
735%
736% o image: the image.
737%
cristy3ed852e2009-09-05 21:47:34 +0000738% o target: the RGB value of the target color.
739%
740% o fill: the replacement color.
741%
742% o invert: paint any pixel that does not match the target color.
743%
cristy189e84c2011-08-27 18:08:53 +0000744% o exception: return any errors or warnings in this structure.
745%
cristy3ed852e2009-09-05 21:47:34 +0000746*/
cristy3ed852e2009-09-05 21:47:34 +0000747MagickExport MagickBooleanType OpaquePaintImage(Image *image,
cristy189e84c2011-08-27 18:08:53 +0000748 const PixelInfo *target,const PixelInfo *fill,const MagickBooleanType invert,
749 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000750{
751#define OpaquePaintImageTag "Opaque/Image"
752
cristyc4c8d132010-01-07 01:58:38 +0000753 CacheView
754 *image_view;
755
cristy3ed852e2009-09-05 21:47:34 +0000756 MagickBooleanType
757 status;
758
cristybb503372010-05-27 20:51:26 +0000759 MagickOffsetType
760 progress;
761
cristy4c08aed2011-07-01 19:47:50 +0000762 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000763 zero;
764
cristybb503372010-05-27 20:51:26 +0000765 ssize_t
766 y;
767
cristy3ed852e2009-09-05 21:47:34 +0000768 assert(image != (Image *) NULL);
769 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +0000770 assert(target != (PixelInfo *) NULL);
771 assert(fill != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000772 if (image->debug != MagickFalse)
773 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy574cc262011-08-05 01:23:58 +0000774 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000775 return(MagickFalse);
cristy09250192012-02-07 18:53:31 +0000776 if ((fill->matte != MagickFalse) && (image->matte == MagickFalse))
777 (void) SetImageAlpha(image,OpaqueAlpha,exception);
cristy3ed852e2009-09-05 21:47:34 +0000778 /*
779 Make image color opaque.
780 */
781 status=MagickTrue;
782 progress=0;
cristy4c08aed2011-07-01 19:47:50 +0000783 GetPixelInfo(image,&zero);
cristy3ed852e2009-09-05 21:47:34 +0000784 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +0000785#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000786 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000787#endif
cristybb503372010-05-27 20:51:26 +0000788 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000789 {
cristy4c08aed2011-07-01 19:47:50 +0000790 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000791 pixel;
792
cristy4c08aed2011-07-01 19:47:50 +0000793 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000794 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000795
cristy14973ba2011-08-27 23:48:07 +0000796 register ssize_t
797 x;
798
cristy3ed852e2009-09-05 21:47:34 +0000799 if (status == MagickFalse)
800 continue;
801 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy14973ba2011-08-27 23:48:07 +0000802 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000803 {
804 status=MagickFalse;
805 continue;
806 }
cristy3ed852e2009-09-05 21:47:34 +0000807 pixel=zero;
cristybb503372010-05-27 20:51:26 +0000808 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000809 {
cristy803640d2011-11-17 02:11:32 +0000810 GetPixelInfoPixel(image,q,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000811 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
cristy95f562a2012-01-01 20:49:11 +0000812 SetPixelInfoPixel(image,fill,q);
cristyed231572011-07-14 02:18:59 +0000813 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000814 }
815 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
816 status=MagickFalse;
817 if (image->progress_monitor != (MagickProgressMonitor) NULL)
818 {
819 MagickBooleanType
820 proceed;
821
cristyb5d5f722009-11-04 03:03:49 +0000822#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy95338b32012-01-22 03:34:36 +0000823 #pragma omp critical (MagickCore_OpaquePaintImage)
cristy3ed852e2009-09-05 21:47:34 +0000824#endif
825 proceed=SetImageProgress(image,OpaquePaintImageTag,progress++,
826 image->rows);
827 if (proceed == MagickFalse)
828 status=MagickFalse;
829 }
830 }
831 image_view=DestroyCacheView(image_view);
832 return(status);
833}
834
835/*
836%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
837% %
838% %
839% %
840% T r a n s p a r e n t P a i n t I m a g e %
841% %
842% %
843% %
844%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
845%
846% TransparentPaintImage() changes the opacity value associated with any pixel
847% that matches color to the value defined by opacity.
848%
cristy14973ba2011-08-27 23:48:07 +0000849% By default color must match a particular pixel color exactly. However, in
850% many cases two colors may differ by a small amount. Fuzz defines how much
851% tolerance is acceptable to consider two colors as the same. For example,
852% set fuzz to 10 and the color red at intensities of 100 and 102 respectively
853% are now interpreted as the same color.
cristy3ed852e2009-09-05 21:47:34 +0000854%
855% The format of the TransparentPaintImage method is:
856%
857% MagickBooleanType TransparentPaintImage(Image *image,
cristy4c08aed2011-07-01 19:47:50 +0000858% const PixelInfo *target,const Quantum opacity,
cristy189e84c2011-08-27 18:08:53 +0000859% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000860%
861% A description of each parameter follows:
862%
863% o image: the image.
864%
865% o target: the target color.
866%
867% o opacity: the replacement opacity value.
868%
869% o invert: paint any pixel that does not match the target color.
870%
cristy189e84c2011-08-27 18:08:53 +0000871% o exception: return any errors or warnings in this structure.
872%
cristy3ed852e2009-09-05 21:47:34 +0000873*/
874MagickExport MagickBooleanType TransparentPaintImage(Image *image,
cristy14973ba2011-08-27 23:48:07 +0000875 const PixelInfo *target,const Quantum opacity,const MagickBooleanType invert,
876 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000877{
878#define TransparentPaintImageTag "Transparent/Image"
879
cristyc4c8d132010-01-07 01:58:38 +0000880 CacheView
881 *image_view;
882
cristy3ed852e2009-09-05 21:47:34 +0000883 MagickBooleanType
884 status;
885
cristybb503372010-05-27 20:51:26 +0000886 MagickOffsetType
887 progress;
888
cristy4c08aed2011-07-01 19:47:50 +0000889 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000890 zero;
891
cristybb503372010-05-27 20:51:26 +0000892 ssize_t
893 y;
894
cristy3ed852e2009-09-05 21:47:34 +0000895 assert(image != (Image *) NULL);
896 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +0000897 assert(target != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000898 if (image->debug != MagickFalse)
899 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy574cc262011-08-05 01:23:58 +0000900 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000901 return(MagickFalse);
902 if (image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +0000903 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +0000904 /*
905 Make image color transparent.
906 */
907 status=MagickTrue;
908 progress=0;
cristy4c08aed2011-07-01 19:47:50 +0000909 GetPixelInfo(image,&zero);
cristy3ed852e2009-09-05 21:47:34 +0000910 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +0000911#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000912 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000913#endif
cristybb503372010-05-27 20:51:26 +0000914 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000915 {
cristy4c08aed2011-07-01 19:47:50 +0000916 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000917 pixel;
918
cristybb503372010-05-27 20:51:26 +0000919 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000920 x;
921
cristy4c08aed2011-07-01 19:47:50 +0000922 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000923 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000924
925 if (status == MagickFalse)
926 continue;
927 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy14973ba2011-08-27 23:48:07 +0000928 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000929 {
930 status=MagickFalse;
931 continue;
932 }
cristy3ed852e2009-09-05 21:47:34 +0000933 pixel=zero;
cristybb503372010-05-27 20:51:26 +0000934 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000935 {
cristy803640d2011-11-17 02:11:32 +0000936 GetPixelInfoPixel(image,q,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000937 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
938 SetPixelAlpha(image,opacity,q);
cristyed231572011-07-14 02:18:59 +0000939 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000940 }
941 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
942 status=MagickFalse;
943 if (image->progress_monitor != (MagickProgressMonitor) NULL)
944 {
945 MagickBooleanType
946 proceed;
947
cristyb5d5f722009-11-04 03:03:49 +0000948#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy95338b32012-01-22 03:34:36 +0000949 #pragma omp critical (MagickCore_TransparentPaintImage)
cristy3ed852e2009-09-05 21:47:34 +0000950#endif
951 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
952 image->rows);
953 if (proceed == MagickFalse)
954 status=MagickFalse;
955 }
956 }
957 image_view=DestroyCacheView(image_view);
958 return(status);
959}
960
961/*
962%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
963% %
964% %
965% %
966% 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 %
967% %
968% %
969% %
970%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
971%
972% TransparentPaintImageChroma() changes the opacity value associated with any
973% pixel that matches color to the value defined by opacity.
974%
cristy14973ba2011-08-27 23:48:07 +0000975% As there is one fuzz value for the all the channels, TransparentPaintImage()
976% is not suitable for the operations like chroma, where the tolerance for
977% similarity of two color component (RGB) can be different. Thus we define
978% this method to take two target pixels (one low and one high) and all the
979% pixels of an image which are lying between these two pixels are made
980% transparent.
cristy3ed852e2009-09-05 21:47:34 +0000981%
cristy14973ba2011-08-27 23:48:07 +0000982% The format of the TransparentPaintImageChroma method is:
cristy3ed852e2009-09-05 21:47:34 +0000983%
cristy14973ba2011-08-27 23:48:07 +0000984% MagickBooleanType TransparentPaintImageChroma(Image *image,
985% const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
986% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000987%
988% A description of each parameter follows:
989%
990% o image: the image.
991%
992% o low: the low target color.
993%
994% o high: the high target color.
995%
996% o opacity: the replacement opacity value.
997%
998% o invert: paint any pixel that does not match the target color.
999%
cristy189e84c2011-08-27 18:08:53 +00001000% o exception: return any errors or warnings in this structure.
1001%
cristy3ed852e2009-09-05 21:47:34 +00001002*/
1003MagickExport MagickBooleanType TransparentPaintImageChroma(Image *image,
cristy189e84c2011-08-27 18:08:53 +00001004 const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
1005 const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001006{
1007#define TransparentPaintImageTag "Transparent/Image"
1008
cristyc4c8d132010-01-07 01:58:38 +00001009 CacheView
1010 *image_view;
1011
cristy3ed852e2009-09-05 21:47:34 +00001012 MagickBooleanType
1013 status;
1014
cristybb503372010-05-27 20:51:26 +00001015 MagickOffsetType
1016 progress;
1017
1018 ssize_t
1019 y;
1020
cristy3ed852e2009-09-05 21:47:34 +00001021 assert(image != (Image *) NULL);
1022 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +00001023 assert(high != (PixelInfo *) NULL);
1024 assert(low != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001025 if (image->debug != MagickFalse)
1026 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy574cc262011-08-05 01:23:58 +00001027 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001028 return(MagickFalse);
1029 if (image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00001030 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00001031 /*
1032 Make image color transparent.
1033 */
1034 status=MagickTrue;
1035 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00001036 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00001037#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00001038 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00001039#endif
cristybb503372010-05-27 20:51:26 +00001040 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001041 {
1042 MagickBooleanType
1043 match;
1044
cristy4c08aed2011-07-01 19:47:50 +00001045 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001046 pixel;
1047
cristy4c08aed2011-07-01 19:47:50 +00001048 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001049 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001050
cristy14973ba2011-08-27 23:48:07 +00001051 register ssize_t
1052 x;
1053
cristy3ed852e2009-09-05 21:47:34 +00001054 if (status == MagickFalse)
1055 continue;
1056 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy14973ba2011-08-27 23:48:07 +00001057 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001058 {
1059 status=MagickFalse;
1060 continue;
1061 }
cristy4c08aed2011-07-01 19:47:50 +00001062 GetPixelInfo(image,&pixel);
cristybb503372010-05-27 20:51:26 +00001063 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001064 {
cristy803640d2011-11-17 02:11:32 +00001065 GetPixelInfoPixel(image,q,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001066 match=((pixel.red >= low->red) && (pixel.red <= high->red) &&
1067 (pixel.green >= low->green) && (pixel.green <= high->green) &&
cristy14973ba2011-08-27 23:48:07 +00001068 (pixel.blue >= low->blue) && (pixel.blue <= high->blue)) ? MagickTrue :
1069 MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00001070 if (match != invert)
cristy4c08aed2011-07-01 19:47:50 +00001071 SetPixelAlpha(image,opacity,q);
cristyed231572011-07-14 02:18:59 +00001072 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001073 }
1074 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1075 status=MagickFalse;
1076 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1077 {
1078 MagickBooleanType
1079 proceed;
1080
cristyb5d5f722009-11-04 03:03:49 +00001081#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy95338b32012-01-22 03:34:36 +00001082 #pragma omp critical (MagickCore_TransparentPaintImageChroma)
cristy3ed852e2009-09-05 21:47:34 +00001083#endif
1084 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
1085 image->rows);
1086 if (proceed == MagickFalse)
1087 status=MagickFalse;
1088 }
1089 }
1090 image_view=DestroyCacheView(image_view);
1091 return(status);
1092}