blob: 09fdcae8cf4d2b44ad399246b01203c11b3f58c7 [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);
174 if (image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +0000175 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +0000176 /*
177 Set floodfill state.
178 */
cristy95f562a2012-01-01 20:49:11 +0000179 floodplane_image=CloneImage(image,image->columns,image->rows,MagickTrue,
180 exception);
cristy3ed852e2009-09-05 21:47:34 +0000181 if (floodplane_image == (Image *) NULL)
182 return(MagickFalse);
cristy95f562a2012-01-01 20:49:11 +0000183 floodplane_image->colorspace=GRAYColorspace;
184 (void) EvaluateImage(floodplane_image,SetEvaluateOperator,0.0,exception);
cristy3ed852e2009-09-05 21:47:34 +0000185 segment_stack=(SegmentInfo *) AcquireQuantumMemory(MaxStacksize,
186 sizeof(*segment_stack));
187 if (segment_stack == (SegmentInfo *) NULL)
188 {
189 floodplane_image=DestroyImage(floodplane_image);
190 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
191 image->filename);
192 }
193 /*
194 Push initial segment on stack.
195 */
cristy14973ba2011-08-27 23:48:07 +0000196 status=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +0000197 x=x_offset;
198 y=y_offset;
199 start=0;
200 s=segment_stack;
201 PushSegmentStack(y,x,x,1);
202 PushSegmentStack(y+1,x,x,-1);
cristy4c08aed2011-07-01 19:47:50 +0000203 GetPixelInfo(image,&pixel);
cristyb0d3bb92010-09-22 14:37:58 +0000204 image_view=AcquireCacheView(image);
205 floodplane_view=AcquireCacheView(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000206 while (s > segment_stack)
207 {
cristy4c08aed2011-07-01 19:47:50 +0000208 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000209 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000210
cristy4c08aed2011-07-01 19:47:50 +0000211 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000212 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000213
cristy14973ba2011-08-27 23:48:07 +0000214 register ssize_t
215 x;
216
cristy3ed852e2009-09-05 21:47:34 +0000217 /*
218 Pop segment off stack.
219 */
220 s--;
cristybb503372010-05-27 20:51:26 +0000221 x1=(ssize_t) s->x1;
222 x2=(ssize_t) s->x2;
223 offset=(ssize_t) s->y2;
224 y=(ssize_t) s->y1+offset;
cristy3ed852e2009-09-05 21:47:34 +0000225 /*
226 Recolor neighboring pixels.
227 */
cristyb0d3bb92010-09-22 14:37:58 +0000228 p=GetCacheViewVirtualPixels(image_view,0,y,(size_t) (x1+1),1,exception);
229 q=GetCacheViewAuthenticPixels(floodplane_view,0,y,(size_t) (x1+1),1,
cristy3ed852e2009-09-05 21:47:34 +0000230 exception);
cristy4c08aed2011-07-01 19:47:50 +0000231 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000232 break;
cristyed231572011-07-14 02:18:59 +0000233 p+=x1*GetPixelChannels(image);
234 q+=x1*GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000235 for (x=x1; x >= 0; x--)
236 {
cristy95f562a2012-01-01 20:49:11 +0000237 if (GetPixelGray(floodplane_image,q) != 0)
cristy3ed852e2009-09-05 21:47:34 +0000238 break;
cristy803640d2011-11-17 02:11:32 +0000239 GetPixelInfoPixel(image,p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000240 if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert)
cristy3ed852e2009-09-05 21:47:34 +0000241 break;
cristy95f562a2012-01-01 20:49:11 +0000242 SetPixelGray(floodplane_image,QuantumRange,q);
cristyed231572011-07-14 02:18:59 +0000243 p-=GetPixelChannels(image);
244 q-=GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000245 }
cristyb0d3bb92010-09-22 14:37:58 +0000246 if (SyncCacheViewAuthenticPixels(floodplane_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000247 break;
248 skip=x >= x1 ? MagickTrue : MagickFalse;
249 if (skip == MagickFalse)
250 {
251 start=x+1;
252 if (start < x1)
253 PushSegmentStack(y,start,x1-1,-offset);
254 x=x1+1;
255 }
256 do
257 {
258 if (skip == MagickFalse)
259 {
cristybb503372010-05-27 20:51:26 +0000260 if (x < (ssize_t) image->columns)
cristy3ed852e2009-09-05 21:47:34 +0000261 {
cristyb0d3bb92010-09-22 14:37:58 +0000262 p=GetCacheViewVirtualPixels(image_view,x,y,image->columns-x,1,
cristy3ed852e2009-09-05 21:47:34 +0000263 exception);
cristyfdcc0182011-12-26 19:33:56 +0000264 q=GetCacheViewAuthenticPixels(floodplane_view,x,y,image->columns-
265 x,1,exception);
cristy636dcb52011-08-26 13:23:49 +0000266 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000267 break;
cristybb503372010-05-27 20:51:26 +0000268 for ( ; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000269 {
cristy95f562a2012-01-01 20:49:11 +0000270 if (GetPixelGray(floodplane_image,q) != 0)
cristy3ed852e2009-09-05 21:47:34 +0000271 break;
cristy803640d2011-11-17 02:11:32 +0000272 GetPixelInfoPixel(image,p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000273 if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert)
cristy3ed852e2009-09-05 21:47:34 +0000274 break;
cristy95f562a2012-01-01 20:49:11 +0000275 SetPixelGray(floodplane_image,QuantumRange,q);
cristyed231572011-07-14 02:18:59 +0000276 p+=GetPixelChannels(image);
277 q+=GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000278 }
cristy14973ba2011-08-27 23:48:07 +0000279 status=SyncCacheViewAuthenticPixels(floodplane_view,exception);
280 if (status == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000281 break;
282 }
283 PushSegmentStack(y,start,x-1,offset);
284 if (x > (x2+1))
285 PushSegmentStack(y,x2+1,x-1,-offset);
286 }
287 skip=MagickFalse;
288 x++;
289 if (x <= x2)
290 {
cristyb0d3bb92010-09-22 14:37:58 +0000291 p=GetCacheViewVirtualPixels(image_view,x,y,(size_t) (x2-x+1),1,
292 exception);
293 q=GetCacheViewAuthenticPixels(floodplane_view,x,y,(size_t) (x2-x+1),1,
cristy3ed852e2009-09-05 21:47:34 +0000294 exception);
cristy636dcb52011-08-26 13:23:49 +0000295 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000296 break;
cristy3ed852e2009-09-05 21:47:34 +0000297 for ( ; x <= x2; x++)
298 {
cristy95f562a2012-01-01 20:49:11 +0000299 if (GetPixelGray(floodplane_image,q) != 0)
cristy3ed852e2009-09-05 21:47:34 +0000300 break;
cristy803640d2011-11-17 02:11:32 +0000301 GetPixelInfoPixel(image,p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000302 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
cristy3ed852e2009-09-05 21:47:34 +0000303 break;
cristyed231572011-07-14 02:18:59 +0000304 p+=GetPixelChannels(image);
305 q+=GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000306 }
307 }
308 start=x;
309 } while (x <= x2);
310 }
cristybb503372010-05-27 20:51:26 +0000311 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000312 {
cristy4c08aed2011-07-01 19:47:50 +0000313 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000314 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000315
cristy4c08aed2011-07-01 19:47:50 +0000316 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000317 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000318
cristy14973ba2011-08-27 23:48:07 +0000319 register ssize_t
320 x;
321
cristy3ed852e2009-09-05 21:47:34 +0000322 /*
323 Tile fill color onto floodplane.
324 */
cristy95f562a2012-01-01 20:49:11 +0000325 p=GetCacheViewVirtualPixels(floodplane_view,0,y,image->columns,1,exception);
cristyb0d3bb92010-09-22 14:37:58 +0000326 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 {
cristy95f562a2012-01-01 20:49:11 +0000331 if (GetPixelGray(floodplane_image,p) != 0)
cristy3ed852e2009-09-05 21:47:34 +0000332 {
cristy2ed42f62011-10-02 19:49:57 +0000333 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
cristy8a20fa02011-12-27 15:54:31 +0000334 SetPixelInfoPixel(image,&fill_color,q);
cristy3ed852e2009-09-05 21:47:34 +0000335 }
cristyed231572011-07-14 02:18:59 +0000336 p+=GetPixelChannels(floodplane_image);
337 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000338 }
cristyb0d3bb92010-09-22 14:37:58 +0000339 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000340 break;
341 }
cristyb0d3bb92010-09-22 14:37:58 +0000342 floodplane_view=DestroyCacheView(floodplane_view);
343 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +0000344 segment_stack=(SegmentInfo *) RelinquishMagickMemory(segment_stack);
345 floodplane_image=DestroyImage(floodplane_image);
cristybb503372010-05-27 20:51:26 +0000346 return(y == (ssize_t) image->rows ? MagickTrue : MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +0000347}
348
349/*
350%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
351% %
352% %
353% %
354+ G r a d i e n t I m a g e %
355% %
356% %
357% %
358%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
359%
cristycee97112010-05-28 00:44:52 +0000360% GradientImage() applies a continuously smooth color transitions along a
cristy3ed852e2009-09-05 21:47:34 +0000361% vector from one color to another.
362%
363% Note, the interface of this method will change in the future to support
364% more than one transistion.
365%
366% The format of the GradientImage method is:
367%
368% MagickBooleanType GradientImage(Image *image,const GradientType type,
cristy101ab702011-10-13 13:06:32 +0000369% const SpreadMethod method,const PixelInfo *start_color,
370% const PixelInfo *stop_color,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000371%
372% A description of each parameter follows:
373%
374% o image: the image.
375%
376% o type: the gradient type: linear or radial.
377%
378% o spread: the gradient spread meathod: pad, reflect, or repeat.
379%
380% o start_color: the start color.
381%
382% o stop_color: the stop color.
383%
cristy189e84c2011-08-27 18:08:53 +0000384% o exception: return any errors or warnings in this structure.
385%
cristy3ed852e2009-09-05 21:47:34 +0000386*/
cristy117ff172010-08-15 21:35:32 +0000387
388static inline double MagickMax(const double x,const double y)
389{
390 return(x > y ? x : y);
391}
392
cristy3ed852e2009-09-05 21:47:34 +0000393MagickExport MagickBooleanType GradientImage(Image *image,
394 const GradientType type,const SpreadMethod method,
cristy101ab702011-10-13 13:06:32 +0000395 const PixelInfo *start_color,const PixelInfo *stop_color,
cristy189e84c2011-08-27 18:08:53 +0000396 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000397{
398 DrawInfo
399 *draw_info;
400
401 GradientInfo
402 *gradient;
403
404 MagickBooleanType
405 status;
406
cristybb503372010-05-27 20:51:26 +0000407 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000408 i;
409
410 /*
411 Set gradient start-stop end points.
412 */
413 assert(image != (const Image *) NULL);
414 assert(image->signature == MagickSignature);
415 if (image->debug != MagickFalse)
416 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy101ab702011-10-13 13:06:32 +0000417 assert(start_color != (const PixelInfo *) NULL);
418 assert(stop_color != (const PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000419 draw_info=AcquireDrawInfo();
420 gradient=(&draw_info->gradient);
421 gradient->type=type;
422 gradient->bounding_box.width=image->columns;
423 gradient->bounding_box.height=image->rows;
424 gradient->gradient_vector.x2=(double) image->columns-1.0;
425 gradient->gradient_vector.y2=(double) image->rows-1.0;
426 if ((type == LinearGradient) && (gradient->gradient_vector.y2 != 0.0))
427 gradient->gradient_vector.x2=0.0;
428 gradient->center.x=(double) gradient->gradient_vector.x2/2.0;
429 gradient->center.y=(double) gradient->gradient_vector.y2/2.0;
430 gradient->radius=MagickMax(gradient->center.x,gradient->center.y);
431 gradient->spread=method;
432 /*
433 Define the gradient to fill between the stops.
434 */
435 gradient->number_stops=2;
436 gradient->stops=(StopInfo *) AcquireQuantumMemory(gradient->number_stops,
437 sizeof(*gradient->stops));
438 if (gradient->stops == (StopInfo *) NULL)
439 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
440 image->filename);
441 (void) ResetMagickMemory(gradient->stops,0,gradient->number_stops*
442 sizeof(*gradient->stops));
cristybb503372010-05-27 20:51:26 +0000443 for (i=0; i < (ssize_t) gradient->number_stops; i++)
cristy4c08aed2011-07-01 19:47:50 +0000444 GetPixelInfo(image,&gradient->stops[i].color);
cristy9d8c8ce2011-10-25 16:13:52 +0000445 gradient->stops[0].color=(*start_color);
cristy3ed852e2009-09-05 21:47:34 +0000446 gradient->stops[0].offset=0.0;
cristy9d8c8ce2011-10-25 16:13:52 +0000447 gradient->stops[1].color=(*stop_color);
cristy3ed852e2009-09-05 21:47:34 +0000448 gradient->stops[1].offset=1.0;
449 /*
450 Draw a gradient on the image.
451 */
cristy947cb4c2011-10-20 18:41:46 +0000452 status=DrawGradientImage(image,draw_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000453 draw_info=DestroyDrawInfo(draw_info);
cristy09250192012-02-07 18:53:31 +0000454 if ((start_color->matte == MagickFalse) && (stop_color->matte == MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +0000455 image->matte=MagickFalse;
cristy101ab702011-10-13 13:06:32 +0000456 if ((IsPixelInfoGray(start_color) != MagickFalse) &&
457 (IsPixelInfoGray(stop_color) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +0000458 image->type=GrayscaleType;
459 return(status);
460}
461
462/*
463%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
464% %
465% %
466% %
467% O i l P a i n t I m a g e %
468% %
469% %
470% %
471%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
472%
473% OilPaintImage() applies a special effect filter that simulates an oil
474% painting. Each pixel is replaced by the most frequent color occurring
475% in a circular region defined by radius.
476%
477% The format of the OilPaintImage method is:
478%
479% Image *OilPaintImage(const Image *image,const double radius,
cristy14973ba2011-08-27 23:48:07 +0000480% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000481%
482% A description of each parameter follows:
483%
484% o image: the image.
485%
486% o radius: the radius of the circular neighborhood.
487%
cristy14973ba2011-08-27 23:48:07 +0000488% o sigma: the standard deviation of the Gaussian, in pixels.
489%
cristy3ed852e2009-09-05 21:47:34 +0000490% o exception: return any errors or warnings in this structure.
491%
492*/
493
cristybb503372010-05-27 20:51:26 +0000494static size_t **DestroyHistogramThreadSet(size_t **histogram)
cristy3ed852e2009-09-05 21:47:34 +0000495{
cristybb503372010-05-27 20:51:26 +0000496 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000497 i;
498
cristybb503372010-05-27 20:51:26 +0000499 assert(histogram != (size_t **) NULL);
500 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
501 if (histogram[i] != (size_t *) NULL)
502 histogram[i]=(size_t *) RelinquishMagickMemory(histogram[i]);
cristyb41ee102010-10-04 16:46:15 +0000503 histogram=(size_t **) RelinquishMagickMemory(histogram);
cristy3ed852e2009-09-05 21:47:34 +0000504 return(histogram);
505}
506
cristybb503372010-05-27 20:51:26 +0000507static size_t **AcquireHistogramThreadSet(const size_t count)
cristy3ed852e2009-09-05 21:47:34 +0000508{
cristybb503372010-05-27 20:51:26 +0000509 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000510 i;
511
cristybb503372010-05-27 20:51:26 +0000512 size_t
cristy3ed852e2009-09-05 21:47:34 +0000513 **histogram,
514 number_threads;
515
516 number_threads=GetOpenMPMaximumThreads();
cristy14973ba2011-08-27 23:48:07 +0000517 histogram=(size_t **) AcquireQuantumMemory(number_threads,sizeof(*histogram));
cristybb503372010-05-27 20:51:26 +0000518 if (histogram == (size_t **) NULL)
519 return((size_t **) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000520 (void) ResetMagickMemory(histogram,0,number_threads*sizeof(*histogram));
cristybb503372010-05-27 20:51:26 +0000521 for (i=0; i < (ssize_t) number_threads; i++)
cristy3ed852e2009-09-05 21:47:34 +0000522 {
cristy14973ba2011-08-27 23:48:07 +0000523 histogram[i]=(size_t *) AcquireQuantumMemory(count,sizeof(**histogram));
cristybb503372010-05-27 20:51:26 +0000524 if (histogram[i] == (size_t *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000525 return(DestroyHistogramThreadSet(histogram));
526 }
527 return(histogram);
528}
529
530MagickExport Image *OilPaintImage(const Image *image,const double radius,
cristy14973ba2011-08-27 23:48:07 +0000531 const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000532{
533#define NumberPaintBins 256
534#define OilPaintImageTag "OilPaint/Image"
535
cristyfa112112010-01-04 17:48:07 +0000536 CacheView
537 *image_view,
538 *paint_view;
539
cristy3ed852e2009-09-05 21:47:34 +0000540 Image
541 *paint_image;
542
cristy3ed852e2009-09-05 21:47:34 +0000543 MagickBooleanType
544 status;
545
cristybb503372010-05-27 20:51:26 +0000546 MagickOffsetType
547 progress;
548
549 size_t
cristy14973ba2011-08-27 23:48:07 +0000550 **histograms,
cristy3ed852e2009-09-05 21:47:34 +0000551 width;
552
cristybb503372010-05-27 20:51:26 +0000553 ssize_t
cristyf8561542012-01-24 00:26:46 +0000554 center,
cristybb503372010-05-27 20:51:26 +0000555 y;
556
cristy3ed852e2009-09-05 21:47:34 +0000557 /*
558 Initialize painted image attributes.
559 */
560 assert(image != (const Image *) NULL);
561 assert(image->signature == MagickSignature);
562 if (image->debug != MagickFalse)
563 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
564 assert(exception != (ExceptionInfo *) NULL);
565 assert(exception->signature == MagickSignature);
cristy14973ba2011-08-27 23:48:07 +0000566 width=GetOptimalKernelWidth2D(radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +0000567 paint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
568 if (paint_image == (Image *) NULL)
569 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000570 if (SetImageStorageClass(paint_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000571 {
cristy3ed852e2009-09-05 21:47:34 +0000572 paint_image=DestroyImage(paint_image);
573 return((Image *) NULL);
574 }
575 histograms=AcquireHistogramThreadSet(NumberPaintBins);
cristybb503372010-05-27 20:51:26 +0000576 if (histograms == (size_t **) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000577 {
578 paint_image=DestroyImage(paint_image);
579 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
580 }
581 /*
582 Oil paint image.
583 */
584 status=MagickTrue;
585 progress=0;
cristyd09f8802012-02-04 16:44:10 +0000586 center=(ssize_t) GetPixelChannels(image)*(image->columns+width)*(width/2L)+
587 GetPixelChannels(image)*(width/2L);
cristy3ed852e2009-09-05 21:47:34 +0000588 image_view=AcquireCacheView(image);
589 paint_view=AcquireCacheView(paint_image);
cristyb5d5f722009-11-04 03:03:49 +0000590#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000591 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000592#endif
cristybb503372010-05-27 20:51:26 +0000593 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000594 {
cristy4c08aed2011-07-01 19:47:50 +0000595 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000596 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000597
cristy4c08aed2011-07-01 19:47:50 +0000598 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000599 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000600
cristybb503372010-05-27 20:51:26 +0000601 register size_t
cristy3ed852e2009-09-05 21:47:34 +0000602 *histogram;
603
cristy14973ba2011-08-27 23:48:07 +0000604 register ssize_t
605 x;
606
cristy3ed852e2009-09-05 21:47:34 +0000607 if (status == MagickFalse)
608 continue;
cristyfe4ba002011-02-28 14:54:12 +0000609 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
610 (width/2L),image->columns+width,width,exception);
cristy3ed852e2009-09-05 21:47:34 +0000611 q=QueueCacheViewAuthenticPixels(paint_view,0,y,paint_image->columns,1,
612 exception);
cristy4c08aed2011-07-01 19:47:50 +0000613 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000614 {
615 status=MagickFalse;
616 continue;
617 }
cristy3ed852e2009-09-05 21:47:34 +0000618 histogram=histograms[GetOpenMPThreadId()];
cristybb503372010-05-27 20:51:26 +0000619 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000620 {
cristybb503372010-05-27 20:51:26 +0000621 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000622 i,
623 u;
624
cristybb503372010-05-27 20:51:26 +0000625 size_t
cristy3ed852e2009-09-05 21:47:34 +0000626 count;
627
cristy9d314ff2011-03-09 01:30:28 +0000628 ssize_t
629 j,
630 k,
cristyf8561542012-01-24 00:26:46 +0000631 n,
cristy9d314ff2011-03-09 01:30:28 +0000632 v;
633
cristy3ed852e2009-09-05 21:47:34 +0000634 /*
635 Assign most frequent color.
636 */
cristyf8561542012-01-24 00:26:46 +0000637 k=0;
cristy3ed852e2009-09-05 21:47:34 +0000638 j=0;
639 count=0;
cristyf8561542012-01-24 00:26:46 +0000640 (void) ResetMagickMemory(histogram,0,NumberPaintBins* sizeof(*histogram));
cristybb503372010-05-27 20:51:26 +0000641 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +0000642 {
cristybb503372010-05-27 20:51:26 +0000643 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +0000644 {
cristyf8561542012-01-24 00:26:46 +0000645 n=(ssize_t) ScaleQuantumToChar(GetPixelIntensity(image,p+
646 GetPixelChannels(image)*(u+k)));
647 histogram[n]++;
648 if (histogram[n] > count)
cristy3ed852e2009-09-05 21:47:34 +0000649 {
cristyf8561542012-01-24 00:26:46 +0000650 j=k+u;
651 count=histogram[n];
cristy3ed852e2009-09-05 21:47:34 +0000652 }
653 }
cristyf8561542012-01-24 00:26:46 +0000654 k+=(ssize_t) (image->columns+width);
cristy3ed852e2009-09-05 21:47:34 +0000655 }
cristyf8561542012-01-24 00:26:46 +0000656 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
657 {
658 PixelChannel
659 channel;
660
661 PixelTrait
662 paint_traits,
663 traits;
664
665 channel=GetPixelChannelMapChannel(image,i);
666 traits=GetPixelChannelMapTraits(image,channel);
667 paint_traits=GetPixelChannelMapTraits(paint_image,channel);
668 if ((traits == UndefinedPixelTrait) ||
669 (paint_traits == UndefinedPixelTrait))
670 continue;
cristyd09f8802012-02-04 16:44:10 +0000671 if (((paint_traits & CopyPixelTrait) != 0) ||
672 (GetPixelMask(image,p) != 0))
cristyf8561542012-01-24 00:26:46 +0000673 {
674 SetPixelChannel(paint_image,channel,p[center+i],q);
675 continue;
676 }
677 SetPixelChannel(paint_image,channel,p[j*GetPixelChannels(image)+i],q);
678 }
cristyed231572011-07-14 02:18:59 +0000679 p+=GetPixelChannels(image);
680 q+=GetPixelChannels(paint_image);
cristy3ed852e2009-09-05 21:47:34 +0000681 }
682 if (SyncCacheViewAuthenticPixels(paint_view,exception) == MagickFalse)
683 status=MagickFalse;
684 if (image->progress_monitor != (MagickProgressMonitor) NULL)
685 {
686 MagickBooleanType
687 proceed;
688
cristyb5d5f722009-11-04 03:03:49 +0000689#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy95338b32012-01-22 03:34:36 +0000690 #pragma omp critical (MagickCore_OilPaintImage)
cristy3ed852e2009-09-05 21:47:34 +0000691#endif
692 proceed=SetImageProgress(image,OilPaintImageTag,progress++,image->rows);
693 if (proceed == MagickFalse)
694 status=MagickFalse;
695 }
696 }
697 paint_view=DestroyCacheView(paint_view);
698 image_view=DestroyCacheView(image_view);
699 histograms=DestroyHistogramThreadSet(histograms);
700 if (status == MagickFalse)
701 paint_image=DestroyImage(paint_image);
702 return(paint_image);
703}
704
705/*
706%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
707% %
708% %
709% %
710% O p a q u e P a i n t I m a g e %
711% %
712% %
713% %
714%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
715%
716% OpaquePaintImage() changes any pixel that matches color with the color
717% defined by fill.
718%
cristy14973ba2011-08-27 23:48:07 +0000719% By default color must match a particular pixel color exactly. However, in
720% many cases two colors may differ by a small amount. Fuzz defines how much
721% tolerance is acceptable to consider two colors as the same. For example,
722% set fuzz to 10 and the color red at intensities of 100 and 102 respectively
723% are now interpreted as the same color.
cristy3ed852e2009-09-05 21:47:34 +0000724%
725% The format of the OpaquePaintImage method is:
726%
727% MagickBooleanType OpaquePaintImage(Image *image,
cristy101ab702011-10-13 13:06:32 +0000728% const PixelInfo *target,const PixelInfo *fill,
cristy189e84c2011-08-27 18:08:53 +0000729% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000730%
731% A description of each parameter follows:
732%
733% o image: the image.
734%
cristy3ed852e2009-09-05 21:47:34 +0000735% o target: the RGB value of the target color.
736%
737% o fill: the replacement color.
738%
739% o invert: paint any pixel that does not match the target color.
740%
cristy189e84c2011-08-27 18:08:53 +0000741% o exception: return any errors or warnings in this structure.
742%
cristy3ed852e2009-09-05 21:47:34 +0000743*/
cristy3ed852e2009-09-05 21:47:34 +0000744MagickExport MagickBooleanType OpaquePaintImage(Image *image,
cristy189e84c2011-08-27 18:08:53 +0000745 const PixelInfo *target,const PixelInfo *fill,const MagickBooleanType invert,
746 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000747{
748#define OpaquePaintImageTag "Opaque/Image"
749
cristyc4c8d132010-01-07 01:58:38 +0000750 CacheView
751 *image_view;
752
cristy3ed852e2009-09-05 21:47:34 +0000753 MagickBooleanType
754 status;
755
cristybb503372010-05-27 20:51:26 +0000756 MagickOffsetType
757 progress;
758
cristy4c08aed2011-07-01 19:47:50 +0000759 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000760 zero;
761
cristybb503372010-05-27 20:51:26 +0000762 ssize_t
763 y;
764
cristy3ed852e2009-09-05 21:47:34 +0000765 assert(image != (Image *) NULL);
766 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +0000767 assert(target != (PixelInfo *) NULL);
768 assert(fill != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000769 if (image->debug != MagickFalse)
770 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy574cc262011-08-05 01:23:58 +0000771 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000772 return(MagickFalse);
cristy09250192012-02-07 18:53:31 +0000773 if ((fill->matte != MagickFalse) && (image->matte == MagickFalse))
774 (void) SetImageAlpha(image,OpaqueAlpha,exception);
cristy3ed852e2009-09-05 21:47:34 +0000775 /*
776 Make image color opaque.
777 */
778 status=MagickTrue;
779 progress=0;
cristy4c08aed2011-07-01 19:47:50 +0000780 GetPixelInfo(image,&zero);
cristy3ed852e2009-09-05 21:47:34 +0000781 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +0000782#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000783 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000784#endif
cristybb503372010-05-27 20:51:26 +0000785 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000786 {
cristy4c08aed2011-07-01 19:47:50 +0000787 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000788 pixel;
789
cristy4c08aed2011-07-01 19:47:50 +0000790 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000791 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000792
cristy14973ba2011-08-27 23:48:07 +0000793 register ssize_t
794 x;
795
cristy3ed852e2009-09-05 21:47:34 +0000796 if (status == MagickFalse)
797 continue;
798 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy14973ba2011-08-27 23:48:07 +0000799 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000800 {
801 status=MagickFalse;
802 continue;
803 }
cristy3ed852e2009-09-05 21:47:34 +0000804 pixel=zero;
cristybb503372010-05-27 20:51:26 +0000805 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000806 {
cristy803640d2011-11-17 02:11:32 +0000807 GetPixelInfoPixel(image,q,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000808 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
cristy95f562a2012-01-01 20:49:11 +0000809 SetPixelInfoPixel(image,fill,q);
cristyed231572011-07-14 02:18:59 +0000810 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000811 }
812 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
813 status=MagickFalse;
814 if (image->progress_monitor != (MagickProgressMonitor) NULL)
815 {
816 MagickBooleanType
817 proceed;
818
cristyb5d5f722009-11-04 03:03:49 +0000819#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy95338b32012-01-22 03:34:36 +0000820 #pragma omp critical (MagickCore_OpaquePaintImage)
cristy3ed852e2009-09-05 21:47:34 +0000821#endif
822 proceed=SetImageProgress(image,OpaquePaintImageTag,progress++,
823 image->rows);
824 if (proceed == MagickFalse)
825 status=MagickFalse;
826 }
827 }
828 image_view=DestroyCacheView(image_view);
829 return(status);
830}
831
832/*
833%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
834% %
835% %
836% %
837% T r a n s p a r e n t P a i n t I m a g e %
838% %
839% %
840% %
841%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
842%
843% TransparentPaintImage() changes the opacity value associated with any pixel
844% that matches color to the value defined by opacity.
845%
cristy14973ba2011-08-27 23:48:07 +0000846% By default color must match a particular pixel color exactly. However, in
847% many cases two colors may differ by a small amount. Fuzz defines how much
848% tolerance is acceptable to consider two colors as the same. For example,
849% set fuzz to 10 and the color red at intensities of 100 and 102 respectively
850% are now interpreted as the same color.
cristy3ed852e2009-09-05 21:47:34 +0000851%
852% The format of the TransparentPaintImage method is:
853%
854% MagickBooleanType TransparentPaintImage(Image *image,
cristy4c08aed2011-07-01 19:47:50 +0000855% const PixelInfo *target,const Quantum opacity,
cristy189e84c2011-08-27 18:08:53 +0000856% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000857%
858% A description of each parameter follows:
859%
860% o image: the image.
861%
862% o target: the target color.
863%
864% o opacity: the replacement opacity value.
865%
866% o invert: paint any pixel that does not match the target color.
867%
cristy189e84c2011-08-27 18:08:53 +0000868% o exception: return any errors or warnings in this structure.
869%
cristy3ed852e2009-09-05 21:47:34 +0000870*/
871MagickExport MagickBooleanType TransparentPaintImage(Image *image,
cristy14973ba2011-08-27 23:48:07 +0000872 const PixelInfo *target,const Quantum opacity,const MagickBooleanType invert,
873 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000874{
875#define TransparentPaintImageTag "Transparent/Image"
876
cristyc4c8d132010-01-07 01:58:38 +0000877 CacheView
878 *image_view;
879
cristy3ed852e2009-09-05 21:47:34 +0000880 MagickBooleanType
881 status;
882
cristybb503372010-05-27 20:51:26 +0000883 MagickOffsetType
884 progress;
885
cristy4c08aed2011-07-01 19:47:50 +0000886 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000887 zero;
888
cristybb503372010-05-27 20:51:26 +0000889 ssize_t
890 y;
891
cristy3ed852e2009-09-05 21:47:34 +0000892 assert(image != (Image *) NULL);
893 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +0000894 assert(target != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000895 if (image->debug != MagickFalse)
896 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy574cc262011-08-05 01:23:58 +0000897 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000898 return(MagickFalse);
899 if (image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +0000900 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +0000901 /*
902 Make image color transparent.
903 */
904 status=MagickTrue;
905 progress=0;
cristy4c08aed2011-07-01 19:47:50 +0000906 GetPixelInfo(image,&zero);
cristy3ed852e2009-09-05 21:47:34 +0000907 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +0000908#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000909 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000910#endif
cristybb503372010-05-27 20:51:26 +0000911 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000912 {
cristy4c08aed2011-07-01 19:47:50 +0000913 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000914 pixel;
915
cristybb503372010-05-27 20:51:26 +0000916 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000917 x;
918
cristy4c08aed2011-07-01 19:47:50 +0000919 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000920 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000921
922 if (status == MagickFalse)
923 continue;
924 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy14973ba2011-08-27 23:48:07 +0000925 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000926 {
927 status=MagickFalse;
928 continue;
929 }
cristy3ed852e2009-09-05 21:47:34 +0000930 pixel=zero;
cristybb503372010-05-27 20:51:26 +0000931 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000932 {
cristy803640d2011-11-17 02:11:32 +0000933 GetPixelInfoPixel(image,q,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000934 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
935 SetPixelAlpha(image,opacity,q);
cristyed231572011-07-14 02:18:59 +0000936 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000937 }
938 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
939 status=MagickFalse;
940 if (image->progress_monitor != (MagickProgressMonitor) NULL)
941 {
942 MagickBooleanType
943 proceed;
944
cristyb5d5f722009-11-04 03:03:49 +0000945#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy95338b32012-01-22 03:34:36 +0000946 #pragma omp critical (MagickCore_TransparentPaintImage)
cristy3ed852e2009-09-05 21:47:34 +0000947#endif
948 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
949 image->rows);
950 if (proceed == MagickFalse)
951 status=MagickFalse;
952 }
953 }
954 image_view=DestroyCacheView(image_view);
955 return(status);
956}
957
958/*
959%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
960% %
961% %
962% %
963% 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 %
964% %
965% %
966% %
967%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
968%
969% TransparentPaintImageChroma() changes the opacity value associated with any
970% pixel that matches color to the value defined by opacity.
971%
cristy14973ba2011-08-27 23:48:07 +0000972% As there is one fuzz value for the all the channels, TransparentPaintImage()
973% is not suitable for the operations like chroma, where the tolerance for
974% similarity of two color component (RGB) can be different. Thus we define
975% this method to take two target pixels (one low and one high) and all the
976% pixels of an image which are lying between these two pixels are made
977% transparent.
cristy3ed852e2009-09-05 21:47:34 +0000978%
cristy14973ba2011-08-27 23:48:07 +0000979% The format of the TransparentPaintImageChroma method is:
cristy3ed852e2009-09-05 21:47:34 +0000980%
cristy14973ba2011-08-27 23:48:07 +0000981% MagickBooleanType TransparentPaintImageChroma(Image *image,
982% const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
983% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000984%
985% A description of each parameter follows:
986%
987% o image: the image.
988%
989% o low: the low target color.
990%
991% o high: the high target color.
992%
993% o opacity: the replacement opacity value.
994%
995% o invert: paint any pixel that does not match the target color.
996%
cristy189e84c2011-08-27 18:08:53 +0000997% o exception: return any errors or warnings in this structure.
998%
cristy3ed852e2009-09-05 21:47:34 +0000999*/
1000MagickExport MagickBooleanType TransparentPaintImageChroma(Image *image,
cristy189e84c2011-08-27 18:08:53 +00001001 const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
1002 const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001003{
1004#define TransparentPaintImageTag "Transparent/Image"
1005
cristyc4c8d132010-01-07 01:58:38 +00001006 CacheView
1007 *image_view;
1008
cristy3ed852e2009-09-05 21:47:34 +00001009 MagickBooleanType
1010 status;
1011
cristybb503372010-05-27 20:51:26 +00001012 MagickOffsetType
1013 progress;
1014
1015 ssize_t
1016 y;
1017
cristy3ed852e2009-09-05 21:47:34 +00001018 assert(image != (Image *) NULL);
1019 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +00001020 assert(high != (PixelInfo *) NULL);
1021 assert(low != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001022 if (image->debug != MagickFalse)
1023 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy574cc262011-08-05 01:23:58 +00001024 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001025 return(MagickFalse);
1026 if (image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00001027 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00001028 /*
1029 Make image color transparent.
1030 */
1031 status=MagickTrue;
1032 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00001033 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00001034#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00001035 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00001036#endif
cristybb503372010-05-27 20:51:26 +00001037 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001038 {
1039 MagickBooleanType
1040 match;
1041
cristy4c08aed2011-07-01 19:47:50 +00001042 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001043 pixel;
1044
cristy4c08aed2011-07-01 19:47:50 +00001045 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001046 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001047
cristy14973ba2011-08-27 23:48:07 +00001048 register ssize_t
1049 x;
1050
cristy3ed852e2009-09-05 21:47:34 +00001051 if (status == MagickFalse)
1052 continue;
1053 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy14973ba2011-08-27 23:48:07 +00001054 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001055 {
1056 status=MagickFalse;
1057 continue;
1058 }
cristy4c08aed2011-07-01 19:47:50 +00001059 GetPixelInfo(image,&pixel);
cristybb503372010-05-27 20:51:26 +00001060 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001061 {
cristy803640d2011-11-17 02:11:32 +00001062 GetPixelInfoPixel(image,q,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001063 match=((pixel.red >= low->red) && (pixel.red <= high->red) &&
1064 (pixel.green >= low->green) && (pixel.green <= high->green) &&
cristy14973ba2011-08-27 23:48:07 +00001065 (pixel.blue >= low->blue) && (pixel.blue <= high->blue)) ? MagickTrue :
1066 MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00001067 if (match != invert)
cristy4c08aed2011-07-01 19:47:50 +00001068 SetPixelAlpha(image,opacity,q);
cristyed231572011-07-14 02:18:59 +00001069 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001070 }
1071 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1072 status=MagickFalse;
1073 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1074 {
1075 MagickBooleanType
1076 proceed;
1077
cristyb5d5f722009-11-04 03:03:49 +00001078#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy95338b32012-01-22 03:34:36 +00001079 #pragma omp critical (MagickCore_TransparentPaintImageChroma)
cristy3ed852e2009-09-05 21:47:34 +00001080#endif
1081 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
1082 image->rows);
1083 if (proceed == MagickFalse)
1084 status=MagickFalse;
1085 }
1086 }
1087 image_view=DestroyCacheView(image_view);
1088 return(status);
1089}