blob: c47efff03c426532c1c1dfbb1bee64968ddb7645 [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% %
cristy7e41fe82010-12-04 23:12:08 +000020% Copyright 1999-2011 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"
53#include "MagickCore/monitor.h"
54#include "MagickCore/monitor-private.h"
55#include "MagickCore/paint.h"
56#include "MagickCore/pixel-accessor.h"
57#include "MagickCore/string_.h"
58#include "MagickCore/thread-private.h"
cristy3ed852e2009-09-05 21:47:34 +000059
cristy3ed852e2009-09-05 21:47:34 +000060/*
61%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
62% %
63% %
64% %
65% F l o o d f i l l P a i n t I m a g e %
66% %
67% %
68% %
69%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
70%
71% FloodfillPaintImage() changes the color value of any pixel that matches
72% target and is an immediate neighbor. If the method FillToBorderMethod is
73% specified, the color value is changed for any neighbor pixel that does not
74% match the bordercolor member of image.
75%
76% By default target must match a particular pixel color exactly.
77% However, in many cases two colors may differ by a small amount. The
78% fuzz member of image defines how much tolerance is acceptable to
79% consider two colors as the same. For example, set fuzz to 10 and the
80% color red at intensities of 100 and 102 respectively are now
81% interpreted as the same color for the purposes of the floodfill.
82%
83% The format of the FloodfillPaintImage method is:
84%
85% MagickBooleanType FloodfillPaintImage(Image *image,
cristyd42d9952011-07-08 14:21:50 +000086% const DrawInfo *draw_info,const PixelInfo target,
87% const ssize_t x_offset,const ssize_t y_offset,
88% const MagickBooleanType invert)
cristy3ed852e2009-09-05 21:47:34 +000089%
90% A description of each parameter follows:
91%
92% o image: the image.
93%
cristy3ed852e2009-09-05 21:47:34 +000094% o draw_info: the draw info.
95%
96% o target: the RGB value of the target color.
97%
98% o x_offset,y_offset: the starting location of the operation.
99%
100% o invert: paint any pixel that does not match the target color.
101%
102*/
103MagickExport MagickBooleanType FloodfillPaintImage(Image *image,
cristyd42d9952011-07-08 14:21:50 +0000104 const DrawInfo *draw_info,const PixelInfo *target,const ssize_t x_offset,
105 const ssize_t y_offset,const MagickBooleanType invert)
cristy3ed852e2009-09-05 21:47:34 +0000106{
107#define MaxStacksize (1UL << 15)
108#define PushSegmentStack(up,left,right,delta) \
109{ \
110 if (s >= (segment_stack+MaxStacksize)) \
111 ThrowBinaryException(DrawError,"SegmentStackOverflow",image->filename) \
112 else \
113 { \
cristybb503372010-05-27 20:51:26 +0000114 if ((((up)+(delta)) >= 0) && (((up)+(delta)) < (ssize_t) image->rows)) \
cristy3ed852e2009-09-05 21:47:34 +0000115 { \
116 s->x1=(double) (left); \
117 s->y1=(double) (up); \
118 s->x2=(double) (right); \
119 s->y2=(double) (delta); \
120 s++; \
121 } \
122 } \
123}
124
cristyb0d3bb92010-09-22 14:37:58 +0000125 CacheView
126 *floodplane_view,
127 *image_view;
128
cristy3ed852e2009-09-05 21:47:34 +0000129 ExceptionInfo
130 *exception;
131
132 Image
133 *floodplane_image;
134
cristy3ed852e2009-09-05 21:47:34 +0000135 MagickBooleanType
136 skip;
137
cristy4c08aed2011-07-01 19:47:50 +0000138 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000139 fill,
140 pixel;
141
142 PixelPacket
143 fill_color;
144
145 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 exception=(&image->exception);
173 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000174 return(MagickFalse);
175 if (image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +0000176 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +0000177 /*
178 Set floodfill state.
179 */
180 floodplane_image=CloneImage(image,0,0,MagickTrue,&image->exception);
181 if (floodplane_image == (Image *) NULL)
182 return(MagickFalse);
cristy63240882011-08-05 19:05:27 +0000183 (void) SetImageAlphaChannel(floodplane_image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +0000184 segment_stack=(SegmentInfo *) AcquireQuantumMemory(MaxStacksize,
185 sizeof(*segment_stack));
186 if (segment_stack == (SegmentInfo *) NULL)
187 {
188 floodplane_image=DestroyImage(floodplane_image);
189 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
190 image->filename);
191 }
192 /*
193 Push initial segment on stack.
194 */
cristy3ed852e2009-09-05 21:47:34 +0000195 x=x_offset;
196 y=y_offset;
197 start=0;
198 s=segment_stack;
199 PushSegmentStack(y,x,x,1);
200 PushSegmentStack(y+1,x,x,-1);
cristy4c08aed2011-07-01 19:47:50 +0000201 GetPixelInfo(image,&fill);
202 GetPixelInfo(image,&pixel);
cristyb0d3bb92010-09-22 14:37:58 +0000203 image_view=AcquireCacheView(image);
204 floodplane_view=AcquireCacheView(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000205 while (s > segment_stack)
206 {
cristy4c08aed2011-07-01 19:47:50 +0000207 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000208 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000209
cristybb503372010-05-27 20:51:26 +0000210 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000211 x;
212
cristy4c08aed2011-07-01 19:47:50 +0000213 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000214 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000215
216 /*
217 Pop segment off stack.
218 */
219 s--;
cristybb503372010-05-27 20:51:26 +0000220 x1=(ssize_t) s->x1;
221 x2=(ssize_t) s->x2;
222 offset=(ssize_t) s->y2;
223 y=(ssize_t) s->y1+offset;
cristy3ed852e2009-09-05 21:47:34 +0000224 /*
225 Recolor neighboring pixels.
226 */
cristyb0d3bb92010-09-22 14:37:58 +0000227 p=GetCacheViewVirtualPixels(image_view,0,y,(size_t) (x1+1),1,exception);
228 q=GetCacheViewAuthenticPixels(floodplane_view,0,y,(size_t) (x1+1),1,
cristy3ed852e2009-09-05 21:47:34 +0000229 exception);
cristy4c08aed2011-07-01 19:47:50 +0000230 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000231 break;
cristyed231572011-07-14 02:18:59 +0000232 p+=x1*GetPixelChannels(image);
233 q+=x1*GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000234 for (x=x1; x >= 0; x--)
235 {
cristy4c08aed2011-07-01 19:47:50 +0000236 if (GetPixelAlpha(image,q) == TransparentAlpha)
cristy3ed852e2009-09-05 21:47:34 +0000237 break;
cristy4c08aed2011-07-01 19:47:50 +0000238 SetPixelInfo(image,p,&pixel);
239 if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert)
cristy3ed852e2009-09-05 21:47:34 +0000240 break;
cristy4c08aed2011-07-01 19:47:50 +0000241 SetPixelAlpha(floodplane_image,TransparentAlpha,q);
cristyed231572011-07-14 02:18:59 +0000242 p-=GetPixelChannels(image);
243 q-=GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000244 }
cristyb0d3bb92010-09-22 14:37:58 +0000245 if (SyncCacheViewAuthenticPixels(floodplane_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000246 break;
247 skip=x >= x1 ? MagickTrue : MagickFalse;
248 if (skip == MagickFalse)
249 {
250 start=x+1;
251 if (start < x1)
252 PushSegmentStack(y,start,x1-1,-offset);
253 x=x1+1;
254 }
255 do
256 {
257 if (skip == MagickFalse)
258 {
cristybb503372010-05-27 20:51:26 +0000259 if (x < (ssize_t) image->columns)
cristy3ed852e2009-09-05 21:47:34 +0000260 {
cristyb0d3bb92010-09-22 14:37:58 +0000261 p=GetCacheViewVirtualPixels(image_view,x,y,image->columns-x,1,
cristy3ed852e2009-09-05 21:47:34 +0000262 exception);
cristyb0d3bb92010-09-22 14:37:58 +0000263 q=GetCacheViewAuthenticPixels(floodplane_view,x,y,
264 image->columns-x,1,exception);
cristy636dcb52011-08-26 13:23:49 +0000265 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000266 break;
cristybb503372010-05-27 20:51:26 +0000267 for ( ; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000268 {
cristy4c08aed2011-07-01 19:47:50 +0000269 if (GetPixelAlpha(image,q) == TransparentAlpha)
cristy3ed852e2009-09-05 21:47:34 +0000270 break;
cristy4c08aed2011-07-01 19:47:50 +0000271 SetPixelInfo(image,p,&pixel);
272 if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert)
cristy3ed852e2009-09-05 21:47:34 +0000273 break;
cristy4c08aed2011-07-01 19:47:50 +0000274 SetPixelAlpha(floodplane_image,
275 TransparentAlpha,q);
cristyed231572011-07-14 02:18:59 +0000276 p+=GetPixelChannels(image);
277 q+=GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000278 }
cristyb0d3bb92010-09-22 14:37:58 +0000279 if (SyncCacheViewAuthenticPixels(floodplane_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000280 break;
281 }
282 PushSegmentStack(y,start,x-1,offset);
283 if (x > (x2+1))
284 PushSegmentStack(y,x2+1,x-1,-offset);
285 }
286 skip=MagickFalse;
287 x++;
288 if (x <= x2)
289 {
cristyb0d3bb92010-09-22 14:37:58 +0000290 p=GetCacheViewVirtualPixels(image_view,x,y,(size_t) (x2-x+1),1,
291 exception);
292 q=GetCacheViewAuthenticPixels(floodplane_view,x,y,(size_t) (x2-x+1),1,
cristy3ed852e2009-09-05 21:47:34 +0000293 exception);
cristy636dcb52011-08-26 13:23:49 +0000294 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000295 break;
cristy3ed852e2009-09-05 21:47:34 +0000296 for ( ; x <= x2; x++)
297 {
cristy4c08aed2011-07-01 19:47:50 +0000298 if (GetPixelAlpha(image,q) == TransparentAlpha)
cristy3ed852e2009-09-05 21:47:34 +0000299 break;
cristy4c08aed2011-07-01 19:47:50 +0000300 SetPixelInfo(image,p,&pixel);
301 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
cristy3ed852e2009-09-05 21:47:34 +0000302 break;
cristyed231572011-07-14 02:18:59 +0000303 p+=GetPixelChannels(image);
304 q+=GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000305 }
306 }
307 start=x;
308 } while (x <= x2);
309 }
cristybb503372010-05-27 20:51:26 +0000310 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000311 {
cristy4c08aed2011-07-01 19:47:50 +0000312 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000313 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000314
cristybb503372010-05-27 20:51:26 +0000315 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000316 x;
317
cristy4c08aed2011-07-01 19:47:50 +0000318 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000319 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000320
321 /*
322 Tile fill color onto floodplane.
323 */
cristyb0d3bb92010-09-22 14:37:58 +0000324 p=GetCacheViewVirtualPixels(floodplane_view,0,y,image->columns,1,
325 exception);
326 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000327 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000328 break;
cristybb503372010-05-27 20:51:26 +0000329 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000330 {
cristy4c08aed2011-07-01 19:47:50 +0000331 if (GetPixelAlpha(floodplane_image,p) != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +0000332 {
333 (void) GetFillColor(draw_info,x,y,&fill_color);
cristy4c08aed2011-07-01 19:47:50 +0000334 SetPixelInfoPacket(image,&fill_color,&fill);
cristy3ed852e2009-09-05 21:47:34 +0000335 if (image->colorspace == CMYKColorspace)
336 ConvertRGBToCMYK(&fill);
cristyed231572011-07-14 02:18:59 +0000337 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000338 SetPixelRed(image,ClampToQuantum(fill.red),q);
cristyed231572011-07-14 02:18:59 +0000339 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000340 SetPixelGreen(image,ClampToQuantum(fill.green),q);
cristyed231572011-07-14 02:18:59 +0000341 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000342 SetPixelBlue(image,ClampToQuantum(fill.blue),q);
cristyed231572011-07-14 02:18:59 +0000343 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +0000344 (image->colorspace == CMYKColorspace))
cristy4c08aed2011-07-01 19:47:50 +0000345 SetPixelBlack(image,ClampToQuantum(fill.black),q);
cristyed231572011-07-14 02:18:59 +0000346 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000347 SetPixelAlpha(image,ClampToQuantum(fill.alpha),q);
cristy3ed852e2009-09-05 21:47:34 +0000348 }
cristyed231572011-07-14 02:18:59 +0000349 p+=GetPixelChannels(floodplane_image);
350 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000351 }
cristyb0d3bb92010-09-22 14:37:58 +0000352 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000353 break;
354 }
cristyb0d3bb92010-09-22 14:37:58 +0000355 floodplane_view=DestroyCacheView(floodplane_view);
356 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +0000357 segment_stack=(SegmentInfo *) RelinquishMagickMemory(segment_stack);
358 floodplane_image=DestroyImage(floodplane_image);
cristybb503372010-05-27 20:51:26 +0000359 return(y == (ssize_t) image->rows ? MagickTrue : MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +0000360}
361
362/*
363%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
364% %
365% %
366% %
367+ G r a d i e n t I m a g e %
368% %
369% %
370% %
371%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
372%
cristycee97112010-05-28 00:44:52 +0000373% GradientImage() applies a continuously smooth color transitions along a
cristy3ed852e2009-09-05 21:47:34 +0000374% vector from one color to another.
375%
376% Note, the interface of this method will change in the future to support
377% more than one transistion.
378%
379% The format of the GradientImage method is:
380%
381% MagickBooleanType GradientImage(Image *image,const GradientType type,
382% const SpreadMethod method,const PixelPacket *start_color,
383% const PixelPacket *stop_color)
384%
385% A description of each parameter follows:
386%
387% o image: the image.
388%
389% o type: the gradient type: linear or radial.
390%
391% o spread: the gradient spread meathod: pad, reflect, or repeat.
392%
393% o start_color: the start color.
394%
395% o stop_color: the stop color.
396%
cristy3ed852e2009-09-05 21:47:34 +0000397*/
cristy117ff172010-08-15 21:35:32 +0000398
399static inline double MagickMax(const double x,const double y)
400{
401 return(x > y ? x : y);
402}
403
cristy3ed852e2009-09-05 21:47:34 +0000404MagickExport MagickBooleanType GradientImage(Image *image,
405 const GradientType type,const SpreadMethod method,
406 const PixelPacket *start_color,const PixelPacket *stop_color)
407{
408 DrawInfo
409 *draw_info;
410
411 GradientInfo
412 *gradient;
413
414 MagickBooleanType
415 status;
416
cristybb503372010-05-27 20:51:26 +0000417 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000418 i;
419
420 /*
421 Set gradient start-stop end points.
422 */
423 assert(image != (const Image *) NULL);
424 assert(image->signature == MagickSignature);
425 if (image->debug != MagickFalse)
426 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
427 assert(start_color != (const PixelPacket *) NULL);
428 assert(stop_color != (const PixelPacket *) NULL);
429 draw_info=AcquireDrawInfo();
430 gradient=(&draw_info->gradient);
431 gradient->type=type;
432 gradient->bounding_box.width=image->columns;
433 gradient->bounding_box.height=image->rows;
434 gradient->gradient_vector.x2=(double) image->columns-1.0;
435 gradient->gradient_vector.y2=(double) image->rows-1.0;
436 if ((type == LinearGradient) && (gradient->gradient_vector.y2 != 0.0))
437 gradient->gradient_vector.x2=0.0;
438 gradient->center.x=(double) gradient->gradient_vector.x2/2.0;
439 gradient->center.y=(double) gradient->gradient_vector.y2/2.0;
440 gradient->radius=MagickMax(gradient->center.x,gradient->center.y);
441 gradient->spread=method;
442 /*
443 Define the gradient to fill between the stops.
444 */
445 gradient->number_stops=2;
446 gradient->stops=(StopInfo *) AcquireQuantumMemory(gradient->number_stops,
447 sizeof(*gradient->stops));
448 if (gradient->stops == (StopInfo *) NULL)
449 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
450 image->filename);
451 (void) ResetMagickMemory(gradient->stops,0,gradient->number_stops*
452 sizeof(*gradient->stops));
cristybb503372010-05-27 20:51:26 +0000453 for (i=0; i < (ssize_t) gradient->number_stops; i++)
cristy4c08aed2011-07-01 19:47:50 +0000454 GetPixelInfo(image,&gradient->stops[i].color);
455 SetPixelInfoPacket(image,start_color,&gradient->stops[0].color);
cristy3ed852e2009-09-05 21:47:34 +0000456 gradient->stops[0].offset=0.0;
cristy4c08aed2011-07-01 19:47:50 +0000457 SetPixelInfoPacket(image,stop_color,&gradient->stops[1].color);
cristy3ed852e2009-09-05 21:47:34 +0000458 gradient->stops[1].offset=1.0;
459 /*
460 Draw a gradient on the image.
461 */
462 status=DrawGradientImage(image,draw_info);
463 draw_info=DestroyDrawInfo(draw_info);
cristy4c08aed2011-07-01 19:47:50 +0000464 if ((start_color->alpha == OpaqueAlpha) && (stop_color->alpha == OpaqueAlpha))
cristy3ed852e2009-09-05 21:47:34 +0000465 image->matte=MagickFalse;
cristy4c08aed2011-07-01 19:47:50 +0000466 if ((IsPixelPacketGray(start_color) != MagickFalse) &&
467 (IsPixelPacketGray(stop_color) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +0000468 image->type=GrayscaleType;
469 return(status);
470}
471
472/*
473%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
474% %
475% %
476% %
477% O i l P a i n t I m a g e %
478% %
479% %
480% %
481%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
482%
483% OilPaintImage() applies a special effect filter that simulates an oil
484% painting. Each pixel is replaced by the most frequent color occurring
485% in a circular region defined by radius.
486%
487% The format of the OilPaintImage method is:
488%
489% Image *OilPaintImage(const Image *image,const double radius,
490% ExceptionInfo *exception)
491%
492% A description of each parameter follows:
493%
494% o image: the image.
495%
496% o radius: the radius of the circular neighborhood.
497%
498% o exception: return any errors or warnings in this structure.
499%
500*/
501
cristybb503372010-05-27 20:51:26 +0000502static size_t **DestroyHistogramThreadSet(size_t **histogram)
cristy3ed852e2009-09-05 21:47:34 +0000503{
cristybb503372010-05-27 20:51:26 +0000504 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000505 i;
506
cristybb503372010-05-27 20:51:26 +0000507 assert(histogram != (size_t **) NULL);
508 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
509 if (histogram[i] != (size_t *) NULL)
510 histogram[i]=(size_t *) RelinquishMagickMemory(histogram[i]);
cristyb41ee102010-10-04 16:46:15 +0000511 histogram=(size_t **) RelinquishMagickMemory(histogram);
cristy3ed852e2009-09-05 21:47:34 +0000512 return(histogram);
513}
514
cristybb503372010-05-27 20:51:26 +0000515static size_t **AcquireHistogramThreadSet(const size_t count)
cristy3ed852e2009-09-05 21:47:34 +0000516{
cristybb503372010-05-27 20:51:26 +0000517 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000518 i;
519
cristybb503372010-05-27 20:51:26 +0000520 size_t
cristy3ed852e2009-09-05 21:47:34 +0000521 **histogram,
522 number_threads;
523
524 number_threads=GetOpenMPMaximumThreads();
cristyb41ee102010-10-04 16:46:15 +0000525 histogram=(size_t **) AcquireQuantumMemory(number_threads,
cristy3ed852e2009-09-05 21:47:34 +0000526 sizeof(*histogram));
cristybb503372010-05-27 20:51:26 +0000527 if (histogram == (size_t **) NULL)
528 return((size_t **) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000529 (void) ResetMagickMemory(histogram,0,number_threads*sizeof(*histogram));
cristybb503372010-05-27 20:51:26 +0000530 for (i=0; i < (ssize_t) number_threads; i++)
cristy3ed852e2009-09-05 21:47:34 +0000531 {
cristybb503372010-05-27 20:51:26 +0000532 histogram[i]=(size_t *) AcquireQuantumMemory(count,
cristy3ed852e2009-09-05 21:47:34 +0000533 sizeof(**histogram));
cristybb503372010-05-27 20:51:26 +0000534 if (histogram[i] == (size_t *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000535 return(DestroyHistogramThreadSet(histogram));
536 }
537 return(histogram);
538}
539
540MagickExport Image *OilPaintImage(const Image *image,const double radius,
541 ExceptionInfo *exception)
542{
543#define NumberPaintBins 256
544#define OilPaintImageTag "OilPaint/Image"
545
cristyfa112112010-01-04 17:48:07 +0000546 CacheView
547 *image_view,
548 *paint_view;
549
cristy3ed852e2009-09-05 21:47:34 +0000550 Image
551 *paint_image;
552
cristy3ed852e2009-09-05 21:47:34 +0000553 MagickBooleanType
554 status;
555
cristybb503372010-05-27 20:51:26 +0000556 MagickOffsetType
557 progress;
558
559 size_t
cristyfa112112010-01-04 17:48:07 +0000560 **restrict histograms,
cristy3ed852e2009-09-05 21:47:34 +0000561 width;
562
cristybb503372010-05-27 20:51:26 +0000563 ssize_t
564 y;
565
cristy3ed852e2009-09-05 21:47:34 +0000566 /*
567 Initialize painted image attributes.
568 */
569 assert(image != (const Image *) NULL);
570 assert(image->signature == MagickSignature);
571 if (image->debug != MagickFalse)
572 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
573 assert(exception != (ExceptionInfo *) NULL);
574 assert(exception->signature == MagickSignature);
575 width=GetOptimalKernelWidth2D(radius,0.5);
cristy3ed852e2009-09-05 21:47:34 +0000576 paint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
577 if (paint_image == (Image *) NULL)
578 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000579 if (SetImageStorageClass(paint_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000580 {
cristy3ed852e2009-09-05 21:47:34 +0000581 paint_image=DestroyImage(paint_image);
582 return((Image *) NULL);
583 }
584 histograms=AcquireHistogramThreadSet(NumberPaintBins);
cristybb503372010-05-27 20:51:26 +0000585 if (histograms == (size_t **) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000586 {
587 paint_image=DestroyImage(paint_image);
588 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
589 }
590 /*
591 Oil paint image.
592 */
593 status=MagickTrue;
594 progress=0;
595 image_view=AcquireCacheView(image);
596 paint_view=AcquireCacheView(paint_image);
cristyb5d5f722009-11-04 03:03:49 +0000597#if defined(MAGICKCORE_OPENMP_SUPPORT)
598 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000599#endif
cristybb503372010-05-27 20:51:26 +0000600 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000601 {
cristy4c08aed2011-07-01 19:47:50 +0000602 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000603 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000604
cristybb503372010-05-27 20:51:26 +0000605 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000606 x;
607
cristy4c08aed2011-07-01 19:47:50 +0000608 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000609 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000610
cristybb503372010-05-27 20:51:26 +0000611 register size_t
cristy3ed852e2009-09-05 21:47:34 +0000612 *histogram;
613
614 if (status == MagickFalse)
615 continue;
cristyfe4ba002011-02-28 14:54:12 +0000616 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
617 (width/2L),image->columns+width,width,exception);
cristy3ed852e2009-09-05 21:47:34 +0000618 q=QueueCacheViewAuthenticPixels(paint_view,0,y,paint_image->columns,1,
619 exception);
cristy4c08aed2011-07-01 19:47:50 +0000620 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000621 {
622 status=MagickFalse;
623 continue;
624 }
cristy3ed852e2009-09-05 21:47:34 +0000625 histogram=histograms[GetOpenMPThreadId()];
cristybb503372010-05-27 20:51:26 +0000626 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000627 {
cristybb503372010-05-27 20:51:26 +0000628 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000629 i,
630 u;
631
cristybb503372010-05-27 20:51:26 +0000632 size_t
cristy3ed852e2009-09-05 21:47:34 +0000633 count;
634
cristy9d314ff2011-03-09 01:30:28 +0000635 ssize_t
636 j,
637 k,
638 v;
639
cristy3ed852e2009-09-05 21:47:34 +0000640 /*
641 Assign most frequent color.
642 */
643 i=0;
644 j=0;
645 count=0;
646 (void) ResetMagickMemory(histogram,0,NumberPaintBins*sizeof(*histogram));
cristybb503372010-05-27 20:51:26 +0000647 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +0000648 {
cristybb503372010-05-27 20:51:26 +0000649 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +0000650 {
cristy4c08aed2011-07-01 19:47:50 +0000651 k=(ssize_t) ScaleQuantumToChar(GetPixelIntensity(image,p+u+i));
cristy3ed852e2009-09-05 21:47:34 +0000652 histogram[k]++;
653 if (histogram[k] > count)
654 {
655 j=i+u;
656 count=histogram[k];
657 }
658 }
cristyd99b0962010-05-29 23:14:26 +0000659 i+=(ssize_t) (image->columns+width);
cristy3ed852e2009-09-05 21:47:34 +0000660 }
cristy4c08aed2011-07-01 19:47:50 +0000661 SetPixelRed(paint_image,GetPixelRed(image,p+j*
cristyed231572011-07-14 02:18:59 +0000662 GetPixelChannels(image)),q);
cristy4c08aed2011-07-01 19:47:50 +0000663 SetPixelGreen(paint_image,GetPixelGreen(image,p+j*
cristyed231572011-07-14 02:18:59 +0000664 GetPixelChannels(image)),q);
cristy4c08aed2011-07-01 19:47:50 +0000665 SetPixelBlue(paint_image,GetPixelBlue(image,p+j*
cristyed231572011-07-14 02:18:59 +0000666 GetPixelChannels(image)),q);
cristy3ed852e2009-09-05 21:47:34 +0000667 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +0000668 SetPixelBlack(paint_image,GetPixelBlack(image,p+j*
cristyed231572011-07-14 02:18:59 +0000669 GetPixelChannels(image)),q);
cristy4c08aed2011-07-01 19:47:50 +0000670 if (image->matte != MagickFalse)
671 SetPixelAlpha(paint_image,GetPixelAlpha(image,p+j*
cristyed231572011-07-14 02:18:59 +0000672 GetPixelChannels(image)),q);
673 p+=GetPixelChannels(image);
674 q+=GetPixelChannels(paint_image);
cristy3ed852e2009-09-05 21:47:34 +0000675 }
676 if (SyncCacheViewAuthenticPixels(paint_view,exception) == MagickFalse)
677 status=MagickFalse;
678 if (image->progress_monitor != (MagickProgressMonitor) NULL)
679 {
680 MagickBooleanType
681 proceed;
682
cristyb5d5f722009-11-04 03:03:49 +0000683#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000684 #pragma omp critical (MagickCore_OilPaintImage)
685#endif
686 proceed=SetImageProgress(image,OilPaintImageTag,progress++,image->rows);
687 if (proceed == MagickFalse)
688 status=MagickFalse;
689 }
690 }
691 paint_view=DestroyCacheView(paint_view);
692 image_view=DestroyCacheView(image_view);
693 histograms=DestroyHistogramThreadSet(histograms);
694 if (status == MagickFalse)
695 paint_image=DestroyImage(paint_image);
696 return(paint_image);
697}
698
699/*
700%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
701% %
702% %
703% %
704% O p a q u e P a i n t I m a g e %
705% %
706% %
707% %
708%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
709%
710% OpaquePaintImage() changes any pixel that matches color with the color
711% defined by fill.
712%
713% By default color must match a particular pixel color exactly. However,
714% in many cases two colors may differ by a small amount. Fuzz defines
715% how much tolerance is acceptable to consider two colors as the same.
716% For example, set fuzz to 10 and the color red at intensities of 100 and
717% 102 respectively are now interpreted as the same color.
718%
719% The format of the OpaquePaintImage method is:
720%
721% MagickBooleanType OpaquePaintImage(Image *image,
722% const PixelPacket *target,const PixelPacket *fill,
723% const MagickBooleanType invert)
cristy3ed852e2009-09-05 21:47:34 +0000724%
725% A description of each parameter follows:
726%
727% o image: the image.
728%
cristy3ed852e2009-09-05 21:47:34 +0000729% o target: the RGB value of the target color.
730%
731% o fill: the replacement color.
732%
733% o invert: paint any pixel that does not match the target color.
734%
735*/
cristy3ed852e2009-09-05 21:47:34 +0000736MagickExport MagickBooleanType OpaquePaintImage(Image *image,
cristyd42d9952011-07-08 14:21:50 +0000737 const PixelInfo *target,const PixelInfo *fill,const MagickBooleanType invert)
cristy3ed852e2009-09-05 21:47:34 +0000738{
739#define OpaquePaintImageTag "Opaque/Image"
740
cristyc4c8d132010-01-07 01:58:38 +0000741 CacheView
742 *image_view;
743
cristy3ed852e2009-09-05 21:47:34 +0000744 ExceptionInfo
745 *exception;
746
cristy3ed852e2009-09-05 21:47:34 +0000747 MagickBooleanType
748 status;
749
cristybb503372010-05-27 20:51:26 +0000750 MagickOffsetType
751 progress;
752
cristy4c08aed2011-07-01 19:47:50 +0000753 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000754 zero;
755
cristybb503372010-05-27 20:51:26 +0000756 ssize_t
757 y;
758
cristy3ed852e2009-09-05 21:47:34 +0000759 assert(image != (Image *) NULL);
760 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +0000761 assert(target != (PixelInfo *) NULL);
762 assert(fill != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000763 if (image->debug != MagickFalse)
764 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy48598922011-08-05 17:45:56 +0000765 exception=(&image->exception);
cristy574cc262011-08-05 01:23:58 +0000766 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000767 return(MagickFalse);
768 /*
769 Make image color opaque.
770 */
771 status=MagickTrue;
772 progress=0;
cristy4c08aed2011-07-01 19:47:50 +0000773 GetPixelInfo(image,&zero);
cristy3ed852e2009-09-05 21:47:34 +0000774 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +0000775#if defined(MAGICKCORE_OPENMP_SUPPORT)
776 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000777#endif
cristybb503372010-05-27 20:51:26 +0000778 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000779 {
cristy4c08aed2011-07-01 19:47:50 +0000780 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000781 pixel;
782
cristybb503372010-05-27 20:51:26 +0000783 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000784 x;
785
cristy4c08aed2011-07-01 19:47:50 +0000786 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000787 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000788
789 if (status == MagickFalse)
790 continue;
791 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000792 if (q == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000793 {
794 status=MagickFalse;
795 continue;
796 }
cristy3ed852e2009-09-05 21:47:34 +0000797 pixel=zero;
cristybb503372010-05-27 20:51:26 +0000798 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000799 {
cristy4c08aed2011-07-01 19:47:50 +0000800 SetPixelInfo(image,q,&pixel);
801 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
cristy3ed852e2009-09-05 21:47:34 +0000802 {
cristyed231572011-07-14 02:18:59 +0000803 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000804 SetPixelRed(image,ClampToQuantum(fill->red),q);
cristyed231572011-07-14 02:18:59 +0000805 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000806 SetPixelGreen(image,ClampToQuantum(fill->green),q);
cristyed231572011-07-14 02:18:59 +0000807 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000808 SetPixelBlue(image,ClampToQuantum(fill->blue),q);
cristyed231572011-07-14 02:18:59 +0000809 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy3ed852e2009-09-05 21:47:34 +0000810 (image->colorspace == CMYKColorspace))
cristy4c08aed2011-07-01 19:47:50 +0000811 SetPixelBlack(image,ClampToQuantum(fill->black),q);
cristyed231572011-07-14 02:18:59 +0000812 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000813 SetPixelAlpha(image,ClampToQuantum(fill->alpha),q);
cristy3ed852e2009-09-05 21:47:34 +0000814 }
cristyed231572011-07-14 02:18:59 +0000815 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000816 }
817 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
818 status=MagickFalse;
819 if (image->progress_monitor != (MagickProgressMonitor) NULL)
820 {
821 MagickBooleanType
822 proceed;
823
cristyb5d5f722009-11-04 03:03:49 +0000824#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyd42d9952011-07-08 14:21:50 +0000825 #pragma omp critical (MagickCore_OpaquePaintImage)
cristy3ed852e2009-09-05 21:47:34 +0000826#endif
827 proceed=SetImageProgress(image,OpaquePaintImageTag,progress++,
828 image->rows);
829 if (proceed == MagickFalse)
830 status=MagickFalse;
831 }
832 }
833 image_view=DestroyCacheView(image_view);
834 return(status);
835}
836
837/*
838%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
839% %
840% %
841% %
842% T r a n s p a r e n t P a i n t I m a g e %
843% %
844% %
845% %
846%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
847%
848% TransparentPaintImage() changes the opacity value associated with any pixel
849% that matches color to the value defined by opacity.
850%
851% By default color must match a particular pixel color exactly. However,
852% in many cases two colors may differ by a small amount. Fuzz defines
853% how much tolerance is acceptable to consider two colors as the same.
854% For example, set fuzz to 10 and the color red at intensities of 100 and
855% 102 respectively are now interpreted as the same color.
856%
857% The format of the TransparentPaintImage method is:
858%
859% MagickBooleanType TransparentPaintImage(Image *image,
cristy4c08aed2011-07-01 19:47:50 +0000860% const PixelInfo *target,const Quantum opacity,
cristy3ed852e2009-09-05 21:47:34 +0000861% const MagickBooleanType invert)
862%
863% A description of each parameter follows:
864%
865% o image: the image.
866%
867% o target: the target color.
868%
869% o opacity: the replacement opacity value.
870%
871% o invert: paint any pixel that does not match the target color.
872%
873*/
874MagickExport MagickBooleanType TransparentPaintImage(Image *image,
cristy4c08aed2011-07-01 19:47:50 +0000875 const PixelInfo *target,const Quantum opacity,
cristy3ed852e2009-09-05 21:47:34 +0000876 const MagickBooleanType invert)
877{
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 ExceptionInfo
884 *exception;
885
cristy3ed852e2009-09-05 21:47:34 +0000886 MagickBooleanType
887 status;
888
cristybb503372010-05-27 20:51:26 +0000889 MagickOffsetType
890 progress;
891
cristy4c08aed2011-07-01 19:47:50 +0000892 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000893 zero;
894
cristybb503372010-05-27 20:51:26 +0000895 ssize_t
896 y;
897
cristy3ed852e2009-09-05 21:47:34 +0000898 assert(image != (Image *) NULL);
899 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +0000900 assert(target != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000901 if (image->debug != MagickFalse)
902 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy48598922011-08-05 17:45:56 +0000903 exception=(&image->exception);
cristy574cc262011-08-05 01:23:58 +0000904 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000905 return(MagickFalse);
906 if (image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +0000907 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +0000908 /*
909 Make image color transparent.
910 */
911 status=MagickTrue;
912 progress=0;
cristy4c08aed2011-07-01 19:47:50 +0000913 GetPixelInfo(image,&zero);
cristy3ed852e2009-09-05 21:47:34 +0000914 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +0000915#if defined(MAGICKCORE_OPENMP_SUPPORT)
916 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000917#endif
cristybb503372010-05-27 20:51:26 +0000918 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000919 {
cristy4c08aed2011-07-01 19:47:50 +0000920 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000921 pixel;
922
cristybb503372010-05-27 20:51:26 +0000923 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000924 x;
925
cristy4c08aed2011-07-01 19:47:50 +0000926 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000927 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000928
929 if (status == MagickFalse)
930 continue;
931 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000932 if (q == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000933 {
934 status=MagickFalse;
935 continue;
936 }
cristy3ed852e2009-09-05 21:47:34 +0000937 pixel=zero;
cristybb503372010-05-27 20:51:26 +0000938 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000939 {
cristy4c08aed2011-07-01 19:47:50 +0000940 SetPixelInfo(image,q,&pixel);
941 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
942 SetPixelAlpha(image,opacity,q);
cristyed231572011-07-14 02:18:59 +0000943 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000944 }
945 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
946 status=MagickFalse;
947 if (image->progress_monitor != (MagickProgressMonitor) NULL)
948 {
949 MagickBooleanType
950 proceed;
951
cristyb5d5f722009-11-04 03:03:49 +0000952#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000953 #pragma omp critical (MagickCore_TransparentPaintImage)
954#endif
955 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
956 image->rows);
957 if (proceed == MagickFalse)
958 status=MagickFalse;
959 }
960 }
961 image_view=DestroyCacheView(image_view);
962 return(status);
963}
964
965/*
966%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
967% %
968% %
969% %
970% 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 %
971% %
972% %
973% %
974%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
975%
976% TransparentPaintImageChroma() changes the opacity value associated with any
977% pixel that matches color to the value defined by opacity.
978%
979% As there is one fuzz value for the all the channels, the
980% TransparentPaintImage() API is not suitable for the operations like chroma,
981% where the tolerance for similarity of two color component (RGB) can be
982% different, Thus we define this method take two target pixels (one
983% low and one hight) and all the pixels of an image which are lying between
984% these two pixels are made transparent.
985%
986% The format of the TransparentPaintImage method is:
987%
988% MagickBooleanType TransparentPaintImage(Image *image,
cristy4c08aed2011-07-01 19:47:50 +0000989% const PixelInfo *low,const PixelInfo *hight,
cristy3ed852e2009-09-05 21:47:34 +0000990% const Quantum opacity,const MagickBooleanType invert)
991%
992% A description of each parameter follows:
993%
994% o image: the image.
995%
996% o low: the low target color.
997%
998% o high: the high target color.
999%
1000% o opacity: the replacement opacity value.
1001%
1002% o invert: paint any pixel that does not match the target color.
1003%
1004*/
1005MagickExport MagickBooleanType TransparentPaintImageChroma(Image *image,
cristy4c08aed2011-07-01 19:47:50 +00001006 const PixelInfo *low,const PixelInfo *high,
cristy3ed852e2009-09-05 21:47:34 +00001007 const Quantum opacity,const MagickBooleanType invert)
1008{
1009#define TransparentPaintImageTag "Transparent/Image"
1010
cristyc4c8d132010-01-07 01:58:38 +00001011 CacheView
1012 *image_view;
1013
cristy3ed852e2009-09-05 21:47:34 +00001014 ExceptionInfo
1015 *exception;
1016
cristy3ed852e2009-09-05 21:47:34 +00001017 MagickBooleanType
1018 status;
1019
cristybb503372010-05-27 20:51:26 +00001020 MagickOffsetType
1021 progress;
1022
1023 ssize_t
1024 y;
1025
cristy3ed852e2009-09-05 21:47:34 +00001026 assert(image != (Image *) NULL);
1027 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +00001028 assert(high != (PixelInfo *) NULL);
1029 assert(low != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001030 if (image->debug != MagickFalse)
1031 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy48598922011-08-05 17:45:56 +00001032 exception=(&image->exception);
cristy574cc262011-08-05 01:23:58 +00001033 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001034 return(MagickFalse);
1035 if (image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00001036 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00001037 /*
1038 Make image color transparent.
1039 */
1040 status=MagickTrue;
1041 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00001042 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00001043#if defined(MAGICKCORE_OPENMP_SUPPORT)
1044 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00001045#endif
cristybb503372010-05-27 20:51:26 +00001046 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001047 {
1048 MagickBooleanType
1049 match;
1050
cristy4c08aed2011-07-01 19:47:50 +00001051 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001052 pixel;
1053
cristybb503372010-05-27 20:51:26 +00001054 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001055 x;
1056
cristy4c08aed2011-07-01 19:47:50 +00001057 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001058 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001059
1060 if (status == MagickFalse)
1061 continue;
1062 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001063 if (q == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001064 {
1065 status=MagickFalse;
1066 continue;
1067 }
cristy4c08aed2011-07-01 19:47:50 +00001068 GetPixelInfo(image,&pixel);
cristybb503372010-05-27 20:51:26 +00001069 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001070 {
cristy4c08aed2011-07-01 19:47:50 +00001071 SetPixelInfo(image,q,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001072 match=((pixel.red >= low->red) && (pixel.red <= high->red) &&
1073 (pixel.green >= low->green) && (pixel.green <= high->green) &&
1074 (pixel.blue >= low->blue) && (pixel.blue <= high->blue)) ?
1075 MagickTrue : MagickFalse;
1076 if (match != invert)
cristy4c08aed2011-07-01 19:47:50 +00001077 SetPixelAlpha(image,opacity,q);
cristyed231572011-07-14 02:18:59 +00001078 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001079 }
1080 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1081 status=MagickFalse;
1082 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1083 {
1084 MagickBooleanType
1085 proceed;
1086
cristyb5d5f722009-11-04 03:03:49 +00001087#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00001088 #pragma omp critical (MagickCore_TransparentPaintImageChroma)
1089#endif
1090 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
1091 image->rows);
1092 if (proceed == MagickFalse)
1093 status=MagickFalse;
1094 }
1095 }
1096 image_view=DestroyCacheView(image_view);
1097 return(status);
1098}