blob: 4d9ffeb68f285a5236e30c5d1900067b58def304 [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%
cristy908a0002011-08-28 00:13:39 +000076% By default target must match a particular pixel color exactly. However,
77% in many cases two colors may differ by a small amount. The fuzz member of
78% image defines how much tolerance is acceptable to consider two colors as
79% the same. For example, set fuzz to 10 and the color red at intensities of
80% 100 and 102 respectively are now interpreted as the same color for the
81% purposes of the floodfill.
cristy3ed852e2009-09-05 21:47:34 +000082%
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,
cristy189e84c2011-08-27 18:08:53 +000088% const MagickBooleanType invert,ExceptionInfo *exception)
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%
cristy189e84c2011-08-27 18:08:53 +0000102% o exception: return any errors or warnings in this structure.
103%
cristy3ed852e2009-09-05 21:47:34 +0000104*/
105MagickExport MagickBooleanType FloodfillPaintImage(Image *image,
cristyd42d9952011-07-08 14:21:50 +0000106 const DrawInfo *draw_info,const PixelInfo *target,const ssize_t x_offset,
cristy189e84c2011-08-27 18:08:53 +0000107 const ssize_t y_offset,const MagickBooleanType invert,
108 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000109{
110#define MaxStacksize (1UL << 15)
111#define PushSegmentStack(up,left,right,delta) \
112{ \
113 if (s >= (segment_stack+MaxStacksize)) \
114 ThrowBinaryException(DrawError,"SegmentStackOverflow",image->filename) \
115 else \
116 { \
cristybb503372010-05-27 20:51:26 +0000117 if ((((up)+(delta)) >= 0) && (((up)+(delta)) < (ssize_t) image->rows)) \
cristy3ed852e2009-09-05 21:47:34 +0000118 { \
119 s->x1=(double) (left); \
120 s->y1=(double) (up); \
121 s->x2=(double) (right); \
122 s->y2=(double) (delta); \
123 s++; \
124 } \
125 } \
126}
127
cristyb0d3bb92010-09-22 14:37:58 +0000128 CacheView
129 *floodplane_view,
130 *image_view;
131
cristy3ed852e2009-09-05 21:47:34 +0000132 Image
133 *floodplane_image;
134
cristy3ed852e2009-09-05 21:47:34 +0000135 MagickBooleanType
cristy14973ba2011-08-27 23:48:07 +0000136 skip,
137 status;
cristy3ed852e2009-09-05 21:47:34 +0000138
cristy4c08aed2011-07-01 19:47:50 +0000139 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000140 fill,
141 pixel;
142
143 PixelPacket
144 fill_color;
145
146 register SegmentInfo
147 *s;
148
149 SegmentInfo
150 *segment_stack;
151
cristy9d314ff2011-03-09 01:30:28 +0000152 ssize_t
153 offset,
154 start,
155 x,
156 x1,
157 x2,
158 y;
159
cristy3ed852e2009-09-05 21:47:34 +0000160 /*
161 Check boundary conditions.
162 */
163 assert(image != (Image *) NULL);
164 assert(image->signature == MagickSignature);
165 if (image->debug != MagickFalse)
166 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
167 assert(draw_info != (DrawInfo *) NULL);
168 assert(draw_info->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +0000169 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
cristy3ed852e2009-09-05 21:47:34 +0000170 return(MagickFalse);
cristybb503372010-05-27 20:51:26 +0000171 if ((y_offset < 0) || (y_offset >= (ssize_t) image->rows))
cristy3ed852e2009-09-05 21:47:34 +0000172 return(MagickFalse);
cristy574cc262011-08-05 01:23:58 +0000173 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000174 return(MagickFalse);
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 */
cristy14973ba2011-08-27 23:48:07 +0000195 status=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +0000196 x=x_offset;
197 y=y_offset;
198 start=0;
199 s=segment_stack;
200 PushSegmentStack(y,x,x,1);
201 PushSegmentStack(y+1,x,x,-1);
cristy4c08aed2011-07-01 19:47:50 +0000202 GetPixelInfo(image,&fill);
203 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 {
cristy4c08aed2011-07-01 19:47:50 +0000237 if (GetPixelAlpha(image,q) == TransparentAlpha)
cristy3ed852e2009-09-05 21:47:34 +0000238 break;
cristy4c08aed2011-07-01 19:47:50 +0000239 SetPixelInfo(image,p,&pixel);
240 if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert)
cristy3ed852e2009-09-05 21:47:34 +0000241 break;
cristy4c08aed2011-07-01 19:47:50 +0000242 SetPixelAlpha(floodplane_image,TransparentAlpha,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);
cristyb0d3bb92010-09-22 14:37:58 +0000264 q=GetCacheViewAuthenticPixels(floodplane_view,x,y,
265 image->columns-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 {
cristy4c08aed2011-07-01 19:47:50 +0000270 if (GetPixelAlpha(image,q) == TransparentAlpha)
cristy3ed852e2009-09-05 21:47:34 +0000271 break;
cristy4c08aed2011-07-01 19:47:50 +0000272 SetPixelInfo(image,p,&pixel);
273 if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert)
cristy3ed852e2009-09-05 21:47:34 +0000274 break;
cristy14973ba2011-08-27 23:48:07 +0000275 SetPixelAlpha(floodplane_image,TransparentAlpha,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 {
cristy4c08aed2011-07-01 19:47:50 +0000299 if (GetPixelAlpha(image,q) == TransparentAlpha)
cristy3ed852e2009-09-05 21:47:34 +0000300 break;
cristy4c08aed2011-07-01 19:47:50 +0000301 SetPixelInfo(image,p,&pixel);
302 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 */
cristyb0d3bb92010-09-22 14:37:58 +0000325 p=GetCacheViewVirtualPixels(floodplane_view,0,y,image->columns,1,
326 exception);
327 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000328 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000329 break;
cristybb503372010-05-27 20:51:26 +0000330 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000331 {
cristy4c08aed2011-07-01 19:47:50 +0000332 if (GetPixelAlpha(floodplane_image,p) != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +0000333 {
334 (void) GetFillColor(draw_info,x,y,&fill_color);
cristy4c08aed2011-07-01 19:47:50 +0000335 SetPixelInfoPacket(image,&fill_color,&fill);
cristy3ed852e2009-09-05 21:47:34 +0000336 if (image->colorspace == CMYKColorspace)
337 ConvertRGBToCMYK(&fill);
cristyed231572011-07-14 02:18:59 +0000338 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000339 SetPixelRed(image,ClampToQuantum(fill.red),q);
cristyed231572011-07-14 02:18:59 +0000340 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000341 SetPixelGreen(image,ClampToQuantum(fill.green),q);
cristyed231572011-07-14 02:18:59 +0000342 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000343 SetPixelBlue(image,ClampToQuantum(fill.blue),q);
cristy14973ba2011-08-27 23:48:07 +0000344 if ((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0)
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,
cristy189e84c2011-08-27 18:08:53 +0000383% const PixelPacket *stop_color,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000384%
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%
cristy189e84c2011-08-27 18:08:53 +0000397% o exception: return any errors or warnings in this structure.
398%
cristy3ed852e2009-09-05 21:47:34 +0000399*/
cristy117ff172010-08-15 21:35:32 +0000400
401static inline double MagickMax(const double x,const double y)
402{
403 return(x > y ? x : y);
404}
405
cristy3ed852e2009-09-05 21:47:34 +0000406MagickExport MagickBooleanType GradientImage(Image *image,
407 const GradientType type,const SpreadMethod method,
cristy189e84c2011-08-27 18:08:53 +0000408 const PixelPacket *start_color,const PixelPacket *stop_color,
409 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000410{
411 DrawInfo
412 *draw_info;
413
414 GradientInfo
415 *gradient;
416
417 MagickBooleanType
418 status;
419
cristybb503372010-05-27 20:51:26 +0000420 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000421 i;
422
423 /*
424 Set gradient start-stop end points.
425 */
426 assert(image != (const Image *) NULL);
427 assert(image->signature == MagickSignature);
428 if (image->debug != MagickFalse)
429 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
430 assert(start_color != (const PixelPacket *) NULL);
431 assert(stop_color != (const PixelPacket *) NULL);
432 draw_info=AcquireDrawInfo();
433 gradient=(&draw_info->gradient);
434 gradient->type=type;
435 gradient->bounding_box.width=image->columns;
436 gradient->bounding_box.height=image->rows;
437 gradient->gradient_vector.x2=(double) image->columns-1.0;
438 gradient->gradient_vector.y2=(double) image->rows-1.0;
439 if ((type == LinearGradient) && (gradient->gradient_vector.y2 != 0.0))
440 gradient->gradient_vector.x2=0.0;
441 gradient->center.x=(double) gradient->gradient_vector.x2/2.0;
442 gradient->center.y=(double) gradient->gradient_vector.y2/2.0;
443 gradient->radius=MagickMax(gradient->center.x,gradient->center.y);
444 gradient->spread=method;
445 /*
446 Define the gradient to fill between the stops.
447 */
448 gradient->number_stops=2;
449 gradient->stops=(StopInfo *) AcquireQuantumMemory(gradient->number_stops,
450 sizeof(*gradient->stops));
451 if (gradient->stops == (StopInfo *) NULL)
452 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
453 image->filename);
454 (void) ResetMagickMemory(gradient->stops,0,gradient->number_stops*
455 sizeof(*gradient->stops));
cristybb503372010-05-27 20:51:26 +0000456 for (i=0; i < (ssize_t) gradient->number_stops; i++)
cristy4c08aed2011-07-01 19:47:50 +0000457 GetPixelInfo(image,&gradient->stops[i].color);
458 SetPixelInfoPacket(image,start_color,&gradient->stops[0].color);
cristy3ed852e2009-09-05 21:47:34 +0000459 gradient->stops[0].offset=0.0;
cristy4c08aed2011-07-01 19:47:50 +0000460 SetPixelInfoPacket(image,stop_color,&gradient->stops[1].color);
cristy3ed852e2009-09-05 21:47:34 +0000461 gradient->stops[1].offset=1.0;
462 /*
463 Draw a gradient on the image.
464 */
465 status=DrawGradientImage(image,draw_info);
466 draw_info=DestroyDrawInfo(draw_info);
cristy4c08aed2011-07-01 19:47:50 +0000467 if ((start_color->alpha == OpaqueAlpha) && (stop_color->alpha == OpaqueAlpha))
cristy3ed852e2009-09-05 21:47:34 +0000468 image->matte=MagickFalse;
cristy4c08aed2011-07-01 19:47:50 +0000469 if ((IsPixelPacketGray(start_color) != MagickFalse) &&
470 (IsPixelPacketGray(stop_color) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +0000471 image->type=GrayscaleType;
472 return(status);
473}
474
475/*
476%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
477% %
478% %
479% %
480% O i l P a i n t I m a g e %
481% %
482% %
483% %
484%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
485%
486% OilPaintImage() applies a special effect filter that simulates an oil
487% painting. Each pixel is replaced by the most frequent color occurring
488% in a circular region defined by radius.
489%
490% The format of the OilPaintImage method is:
491%
492% Image *OilPaintImage(const Image *image,const double radius,
cristy14973ba2011-08-27 23:48:07 +0000493% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000494%
495% A description of each parameter follows:
496%
497% o image: the image.
498%
499% o radius: the radius of the circular neighborhood.
500%
cristy14973ba2011-08-27 23:48:07 +0000501% o sigma: the standard deviation of the Gaussian, in pixels.
502%
cristy3ed852e2009-09-05 21:47:34 +0000503% o exception: return any errors or warnings in this structure.
504%
505*/
506
cristybb503372010-05-27 20:51:26 +0000507static size_t **DestroyHistogramThreadSet(size_t **histogram)
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 assert(histogram != (size_t **) NULL);
513 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
514 if (histogram[i] != (size_t *) NULL)
515 histogram[i]=(size_t *) RelinquishMagickMemory(histogram[i]);
cristyb41ee102010-10-04 16:46:15 +0000516 histogram=(size_t **) RelinquishMagickMemory(histogram);
cristy3ed852e2009-09-05 21:47:34 +0000517 return(histogram);
518}
519
cristybb503372010-05-27 20:51:26 +0000520static size_t **AcquireHistogramThreadSet(const size_t count)
cristy3ed852e2009-09-05 21:47:34 +0000521{
cristybb503372010-05-27 20:51:26 +0000522 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000523 i;
524
cristybb503372010-05-27 20:51:26 +0000525 size_t
cristy3ed852e2009-09-05 21:47:34 +0000526 **histogram,
527 number_threads;
528
529 number_threads=GetOpenMPMaximumThreads();
cristy14973ba2011-08-27 23:48:07 +0000530 histogram=(size_t **) AcquireQuantumMemory(number_threads,sizeof(*histogram));
cristybb503372010-05-27 20:51:26 +0000531 if (histogram == (size_t **) NULL)
532 return((size_t **) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000533 (void) ResetMagickMemory(histogram,0,number_threads*sizeof(*histogram));
cristybb503372010-05-27 20:51:26 +0000534 for (i=0; i < (ssize_t) number_threads; i++)
cristy3ed852e2009-09-05 21:47:34 +0000535 {
cristy14973ba2011-08-27 23:48:07 +0000536 histogram[i]=(size_t *) AcquireQuantumMemory(count,sizeof(**histogram));
cristybb503372010-05-27 20:51:26 +0000537 if (histogram[i] == (size_t *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000538 return(DestroyHistogramThreadSet(histogram));
539 }
540 return(histogram);
541}
542
543MagickExport Image *OilPaintImage(const Image *image,const double radius,
cristy14973ba2011-08-27 23:48:07 +0000544 const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000545{
546#define NumberPaintBins 256
547#define OilPaintImageTag "OilPaint/Image"
548
cristyfa112112010-01-04 17:48:07 +0000549 CacheView
550 *image_view,
551 *paint_view;
552
cristy3ed852e2009-09-05 21:47:34 +0000553 Image
554 *paint_image;
555
cristy3ed852e2009-09-05 21:47:34 +0000556 MagickBooleanType
557 status;
558
cristybb503372010-05-27 20:51:26 +0000559 MagickOffsetType
560 progress;
561
562 size_t
cristy14973ba2011-08-27 23:48:07 +0000563 **histograms,
cristy3ed852e2009-09-05 21:47:34 +0000564 width;
565
cristybb503372010-05-27 20:51:26 +0000566 ssize_t
567 y;
568
cristy3ed852e2009-09-05 21:47:34 +0000569 /*
570 Initialize painted image attributes.
571 */
572 assert(image != (const Image *) NULL);
573 assert(image->signature == MagickSignature);
574 if (image->debug != MagickFalse)
575 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
576 assert(exception != (ExceptionInfo *) NULL);
577 assert(exception->signature == MagickSignature);
cristy14973ba2011-08-27 23:48:07 +0000578 width=GetOptimalKernelWidth2D(radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +0000579 paint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
580 if (paint_image == (Image *) NULL)
581 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000582 if (SetImageStorageClass(paint_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000583 {
cristy3ed852e2009-09-05 21:47:34 +0000584 paint_image=DestroyImage(paint_image);
585 return((Image *) NULL);
586 }
587 histograms=AcquireHistogramThreadSet(NumberPaintBins);
cristybb503372010-05-27 20:51:26 +0000588 if (histograms == (size_t **) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000589 {
590 paint_image=DestroyImage(paint_image);
591 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
592 }
593 /*
594 Oil paint image.
595 */
596 status=MagickTrue;
597 progress=0;
598 image_view=AcquireCacheView(image);
599 paint_view=AcquireCacheView(paint_image);
cristyb5d5f722009-11-04 03:03:49 +0000600#if defined(MAGICKCORE_OPENMP_SUPPORT)
601 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000602#endif
cristybb503372010-05-27 20:51:26 +0000603 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000604 {
cristy4c08aed2011-07-01 19:47:50 +0000605 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000606 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000607
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
cristy14973ba2011-08-27 23:48:07 +0000614 register ssize_t
615 x;
616
cristy3ed852e2009-09-05 21:47:34 +0000617 if (status == MagickFalse)
618 continue;
cristyfe4ba002011-02-28 14:54:12 +0000619 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
620 (width/2L),image->columns+width,width,exception);
cristy3ed852e2009-09-05 21:47:34 +0000621 q=QueueCacheViewAuthenticPixels(paint_view,0,y,paint_image->columns,1,
622 exception);
cristy4c08aed2011-07-01 19:47:50 +0000623 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000624 {
625 status=MagickFalse;
626 continue;
627 }
cristy3ed852e2009-09-05 21:47:34 +0000628 histogram=histograms[GetOpenMPThreadId()];
cristybb503372010-05-27 20:51:26 +0000629 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000630 {
cristybb503372010-05-27 20:51:26 +0000631 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000632 i,
633 u;
634
cristybb503372010-05-27 20:51:26 +0000635 size_t
cristy3ed852e2009-09-05 21:47:34 +0000636 count;
637
cristy9d314ff2011-03-09 01:30:28 +0000638 ssize_t
639 j,
640 k,
641 v;
642
cristy3ed852e2009-09-05 21:47:34 +0000643 /*
644 Assign most frequent color.
645 */
646 i=0;
647 j=0;
648 count=0;
649 (void) ResetMagickMemory(histogram,0,NumberPaintBins*sizeof(*histogram));
cristybb503372010-05-27 20:51:26 +0000650 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +0000651 {
cristybb503372010-05-27 20:51:26 +0000652 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +0000653 {
cristy14973ba2011-08-27 23:48:07 +0000654 k=(ssize_t) ScaleQuantumToChar(GetPixelIntensity(image,p+
655 GetPixelChannels(image)*(u+i)));
cristy3ed852e2009-09-05 21:47:34 +0000656 histogram[k]++;
657 if (histogram[k] > count)
658 {
659 j=i+u;
660 count=histogram[k];
661 }
662 }
cristyd99b0962010-05-29 23:14:26 +0000663 i+=(ssize_t) (image->columns+width);
cristy3ed852e2009-09-05 21:47:34 +0000664 }
cristy14973ba2011-08-27 23:48:07 +0000665 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
666 SetPixelRed(paint_image,GetPixelRed(image,p+j*
667 GetPixelChannels(image)),q);
668 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
669 SetPixelGreen(paint_image,GetPixelGreen(image,p+j*
670 GetPixelChannels(image)),q);
671 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
672 SetPixelBlue(paint_image,GetPixelBlue(image,p+j*
673 GetPixelChannels(image)),q);
674 if ((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000675 SetPixelBlack(paint_image,GetPixelBlack(image,p+j*
cristyed231572011-07-14 02:18:59 +0000676 GetPixelChannels(image)),q);
cristy14973ba2011-08-27 23:48:07 +0000677 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000678 SetPixelAlpha(paint_image,GetPixelAlpha(image,p+j*
cristyed231572011-07-14 02:18:59 +0000679 GetPixelChannels(image)),q);
680 p+=GetPixelChannels(image);
681 q+=GetPixelChannels(paint_image);
cristy3ed852e2009-09-05 21:47:34 +0000682 }
683 if (SyncCacheViewAuthenticPixels(paint_view,exception) == MagickFalse)
684 status=MagickFalse;
685 if (image->progress_monitor != (MagickProgressMonitor) NULL)
686 {
687 MagickBooleanType
688 proceed;
689
cristyb5d5f722009-11-04 03:03:49 +0000690#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000691 #pragma omp critical (MagickCore_OilPaintImage)
692#endif
693 proceed=SetImageProgress(image,OilPaintImageTag,progress++,image->rows);
694 if (proceed == MagickFalse)
695 status=MagickFalse;
696 }
697 }
698 paint_view=DestroyCacheView(paint_view);
699 image_view=DestroyCacheView(image_view);
700 histograms=DestroyHistogramThreadSet(histograms);
701 if (status == MagickFalse)
702 paint_image=DestroyImage(paint_image);
703 return(paint_image);
704}
705
706/*
707%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
708% %
709% %
710% %
711% O p a q u e P a i n t I m a g e %
712% %
713% %
714% %
715%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
716%
717% OpaquePaintImage() changes any pixel that matches color with the color
718% defined by fill.
719%
cristy14973ba2011-08-27 23:48:07 +0000720% By default color must match a particular pixel color exactly. However, in
721% many cases two colors may differ by a small amount. Fuzz defines how much
722% tolerance is acceptable to consider two colors as the same. For example,
723% set fuzz to 10 and the color red at intensities of 100 and 102 respectively
724% are now interpreted as the same color.
cristy3ed852e2009-09-05 21:47:34 +0000725%
726% The format of the OpaquePaintImage method is:
727%
728% MagickBooleanType OpaquePaintImage(Image *image,
729% const PixelPacket *target,const PixelPacket *fill,
cristy189e84c2011-08-27 18:08:53 +0000730% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000731%
732% A description of each parameter follows:
733%
734% o image: the image.
735%
cristy3ed852e2009-09-05 21:47:34 +0000736% o target: the RGB value of the target color.
737%
738% o fill: the replacement color.
739%
740% o invert: paint any pixel that does not match the target color.
741%
cristy189e84c2011-08-27 18:08:53 +0000742% o exception: return any errors or warnings in this structure.
743%
cristy3ed852e2009-09-05 21:47:34 +0000744*/
cristy3ed852e2009-09-05 21:47:34 +0000745MagickExport MagickBooleanType OpaquePaintImage(Image *image,
cristy189e84c2011-08-27 18:08:53 +0000746 const PixelInfo *target,const PixelInfo *fill,const MagickBooleanType invert,
747 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000748{
749#define OpaquePaintImageTag "Opaque/Image"
750
cristyc4c8d132010-01-07 01:58:38 +0000751 CacheView
752 *image_view;
753
cristy3ed852e2009-09-05 21:47:34 +0000754 MagickBooleanType
755 status;
756
cristybb503372010-05-27 20:51:26 +0000757 MagickOffsetType
758 progress;
759
cristy4c08aed2011-07-01 19:47:50 +0000760 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000761 zero;
762
cristybb503372010-05-27 20:51:26 +0000763 ssize_t
764 y;
765
cristy3ed852e2009-09-05 21:47:34 +0000766 assert(image != (Image *) NULL);
767 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +0000768 assert(target != (PixelInfo *) NULL);
769 assert(fill != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000770 if (image->debug != MagickFalse)
771 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy574cc262011-08-05 01:23:58 +0000772 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000773 return(MagickFalse);
774 /*
775 Make image color opaque.
776 */
777 status=MagickTrue;
778 progress=0;
cristy4c08aed2011-07-01 19:47:50 +0000779 GetPixelInfo(image,&zero);
cristy3ed852e2009-09-05 21:47:34 +0000780 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +0000781#if defined(MAGICKCORE_OPENMP_SUPPORT)
782 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000783#endif
cristybb503372010-05-27 20:51:26 +0000784 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000785 {
cristy4c08aed2011-07-01 19:47:50 +0000786 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000787 pixel;
788
cristy4c08aed2011-07-01 19:47:50 +0000789 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000790 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000791
cristy14973ba2011-08-27 23:48:07 +0000792 register ssize_t
793 x;
794
cristy3ed852e2009-09-05 21:47:34 +0000795 if (status == MagickFalse)
796 continue;
797 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy14973ba2011-08-27 23:48:07 +0000798 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000799 {
800 status=MagickFalse;
801 continue;
802 }
cristy3ed852e2009-09-05 21:47:34 +0000803 pixel=zero;
cristybb503372010-05-27 20:51:26 +0000804 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000805 {
cristy4c08aed2011-07-01 19:47:50 +0000806 SetPixelInfo(image,q,&pixel);
807 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
cristy3ed852e2009-09-05 21:47:34 +0000808 {
cristyed231572011-07-14 02:18:59 +0000809 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000810 SetPixelRed(image,ClampToQuantum(fill->red),q);
cristyed231572011-07-14 02:18:59 +0000811 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000812 SetPixelGreen(image,ClampToQuantum(fill->green),q);
cristyed231572011-07-14 02:18:59 +0000813 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000814 SetPixelBlue(image,ClampToQuantum(fill->blue),q);
cristy14973ba2011-08-27 23:48:07 +0000815 if ((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000816 SetPixelBlack(image,ClampToQuantum(fill->black),q);
cristyed231572011-07-14 02:18:59 +0000817 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +0000818 SetPixelAlpha(image,ClampToQuantum(fill->alpha),q);
cristy3ed852e2009-09-05 21:47:34 +0000819 }
cristyed231572011-07-14 02:18:59 +0000820 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000821 }
822 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
823 status=MagickFalse;
824 if (image->progress_monitor != (MagickProgressMonitor) NULL)
825 {
826 MagickBooleanType
827 proceed;
828
cristyb5d5f722009-11-04 03:03:49 +0000829#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyd42d9952011-07-08 14:21:50 +0000830 #pragma omp critical (MagickCore_OpaquePaintImage)
cristy3ed852e2009-09-05 21:47:34 +0000831#endif
832 proceed=SetImageProgress(image,OpaquePaintImageTag,progress++,
833 image->rows);
834 if (proceed == MagickFalse)
835 status=MagickFalse;
836 }
837 }
838 image_view=DestroyCacheView(image_view);
839 return(status);
840}
841
842/*
843%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
844% %
845% %
846% %
847% T r a n s p a r e n t P a i n t I m a g e %
848% %
849% %
850% %
851%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
852%
853% TransparentPaintImage() changes the opacity value associated with any pixel
854% that matches color to the value defined by opacity.
855%
cristy14973ba2011-08-27 23:48:07 +0000856% By default color must match a particular pixel color exactly. However, in
857% many cases two colors may differ by a small amount. Fuzz defines how much
858% tolerance is acceptable to consider two colors as the same. For example,
859% set fuzz to 10 and the color red at intensities of 100 and 102 respectively
860% are now interpreted as the same color.
cristy3ed852e2009-09-05 21:47:34 +0000861%
862% The format of the TransparentPaintImage method is:
863%
864% MagickBooleanType TransparentPaintImage(Image *image,
cristy4c08aed2011-07-01 19:47:50 +0000865% const PixelInfo *target,const Quantum opacity,
cristy189e84c2011-08-27 18:08:53 +0000866% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000867%
868% A description of each parameter follows:
869%
870% o image: the image.
871%
872% o target: the target color.
873%
874% o opacity: the replacement opacity value.
875%
876% o invert: paint any pixel that does not match the target color.
877%
cristy189e84c2011-08-27 18:08:53 +0000878% o exception: return any errors or warnings in this structure.
879%
cristy3ed852e2009-09-05 21:47:34 +0000880*/
881MagickExport MagickBooleanType TransparentPaintImage(Image *image,
cristy14973ba2011-08-27 23:48:07 +0000882 const PixelInfo *target,const Quantum opacity,const MagickBooleanType invert,
883 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000884{
885#define TransparentPaintImageTag "Transparent/Image"
886
cristyc4c8d132010-01-07 01:58:38 +0000887 CacheView
888 *image_view;
889
cristy3ed852e2009-09-05 21:47:34 +0000890 MagickBooleanType
891 status;
892
cristybb503372010-05-27 20:51:26 +0000893 MagickOffsetType
894 progress;
895
cristy4c08aed2011-07-01 19:47:50 +0000896 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000897 zero;
898
cristybb503372010-05-27 20:51:26 +0000899 ssize_t
900 y;
901
cristy3ed852e2009-09-05 21:47:34 +0000902 assert(image != (Image *) NULL);
903 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +0000904 assert(target != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000905 if (image->debug != MagickFalse)
906 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy574cc262011-08-05 01:23:58 +0000907 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000908 return(MagickFalse);
909 if (image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +0000910 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +0000911 /*
912 Make image color transparent.
913 */
914 status=MagickTrue;
915 progress=0;
cristy4c08aed2011-07-01 19:47:50 +0000916 GetPixelInfo(image,&zero);
cristy3ed852e2009-09-05 21:47:34 +0000917 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +0000918#if defined(MAGICKCORE_OPENMP_SUPPORT)
919 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000920#endif
cristybb503372010-05-27 20:51:26 +0000921 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000922 {
cristy4c08aed2011-07-01 19:47:50 +0000923 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000924 pixel;
925
cristybb503372010-05-27 20:51:26 +0000926 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000927 x;
928
cristy4c08aed2011-07-01 19:47:50 +0000929 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000930 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000931
932 if (status == MagickFalse)
933 continue;
934 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy14973ba2011-08-27 23:48:07 +0000935 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000936 {
937 status=MagickFalse;
938 continue;
939 }
cristy3ed852e2009-09-05 21:47:34 +0000940 pixel=zero;
cristybb503372010-05-27 20:51:26 +0000941 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000942 {
cristy4c08aed2011-07-01 19:47:50 +0000943 SetPixelInfo(image,q,&pixel);
944 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
945 SetPixelAlpha(image,opacity,q);
cristyed231572011-07-14 02:18:59 +0000946 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000947 }
948 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
949 status=MagickFalse;
950 if (image->progress_monitor != (MagickProgressMonitor) NULL)
951 {
952 MagickBooleanType
953 proceed;
954
cristyb5d5f722009-11-04 03:03:49 +0000955#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000956 #pragma omp critical (MagickCore_TransparentPaintImage)
957#endif
958 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
959 image->rows);
960 if (proceed == MagickFalse)
961 status=MagickFalse;
962 }
963 }
964 image_view=DestroyCacheView(image_view);
965 return(status);
966}
967
968/*
969%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
970% %
971% %
972% %
973% 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 %
974% %
975% %
976% %
977%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
978%
979% TransparentPaintImageChroma() changes the opacity value associated with any
980% pixel that matches color to the value defined by opacity.
981%
cristy14973ba2011-08-27 23:48:07 +0000982% As there is one fuzz value for the all the channels, TransparentPaintImage()
983% is not suitable for the operations like chroma, where the tolerance for
984% similarity of two color component (RGB) can be different. Thus we define
985% this method to take two target pixels (one low and one high) and all the
986% pixels of an image which are lying between these two pixels are made
987% transparent.
cristy3ed852e2009-09-05 21:47:34 +0000988%
cristy14973ba2011-08-27 23:48:07 +0000989% The format of the TransparentPaintImageChroma method is:
cristy3ed852e2009-09-05 21:47:34 +0000990%
cristy14973ba2011-08-27 23:48:07 +0000991% MagickBooleanType TransparentPaintImageChroma(Image *image,
992% const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
993% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000994%
995% A description of each parameter follows:
996%
997% o image: the image.
998%
999% o low: the low target color.
1000%
1001% o high: the high target color.
1002%
1003% o opacity: the replacement opacity value.
1004%
1005% o invert: paint any pixel that does not match the target color.
1006%
cristy189e84c2011-08-27 18:08:53 +00001007% o exception: return any errors or warnings in this structure.
1008%
cristy3ed852e2009-09-05 21:47:34 +00001009*/
1010MagickExport MagickBooleanType TransparentPaintImageChroma(Image *image,
cristy189e84c2011-08-27 18:08:53 +00001011 const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
1012 const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001013{
1014#define TransparentPaintImageTag "Transparent/Image"
1015
cristyc4c8d132010-01-07 01:58:38 +00001016 CacheView
1017 *image_view;
1018
cristy3ed852e2009-09-05 21:47:34 +00001019 MagickBooleanType
1020 status;
1021
cristybb503372010-05-27 20:51:26 +00001022 MagickOffsetType
1023 progress;
1024
1025 ssize_t
1026 y;
1027
cristy3ed852e2009-09-05 21:47:34 +00001028 assert(image != (Image *) NULL);
1029 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +00001030 assert(high != (PixelInfo *) NULL);
1031 assert(low != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001032 if (image->debug != MagickFalse)
1033 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy574cc262011-08-05 01:23:58 +00001034 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001035 return(MagickFalse);
1036 if (image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00001037 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00001038 /*
1039 Make image color transparent.
1040 */
1041 status=MagickTrue;
1042 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00001043 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00001044#if defined(MAGICKCORE_OPENMP_SUPPORT)
1045 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00001046#endif
cristybb503372010-05-27 20:51:26 +00001047 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001048 {
1049 MagickBooleanType
1050 match;
1051
cristy4c08aed2011-07-01 19:47:50 +00001052 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001053 pixel;
1054
cristy4c08aed2011-07-01 19:47:50 +00001055 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001056 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001057
cristy14973ba2011-08-27 23:48:07 +00001058 register ssize_t
1059 x;
1060
cristy3ed852e2009-09-05 21:47:34 +00001061 if (status == MagickFalse)
1062 continue;
1063 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy14973ba2011-08-27 23:48:07 +00001064 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001065 {
1066 status=MagickFalse;
1067 continue;
1068 }
cristy4c08aed2011-07-01 19:47:50 +00001069 GetPixelInfo(image,&pixel);
cristybb503372010-05-27 20:51:26 +00001070 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001071 {
cristy4c08aed2011-07-01 19:47:50 +00001072 SetPixelInfo(image,q,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001073 match=((pixel.red >= low->red) && (pixel.red <= high->red) &&
1074 (pixel.green >= low->green) && (pixel.green <= high->green) &&
cristy14973ba2011-08-27 23:48:07 +00001075 (pixel.blue >= low->blue) && (pixel.blue <= high->blue)) ? MagickTrue :
1076 MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00001077 if (match != invert)
cristy4c08aed2011-07-01 19:47:50 +00001078 SetPixelAlpha(image,opacity,q);
cristyed231572011-07-14 02:18:59 +00001079 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001080 }
1081 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1082 status=MagickFalse;
1083 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1084 {
1085 MagickBooleanType
1086 proceed;
1087
cristyb5d5f722009-11-04 03:03:49 +00001088#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00001089 #pragma omp critical (MagickCore_TransparentPaintImageChroma)
1090#endif
1091 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
1092 image->rows);
1093 if (proceed == MagickFalse)
1094 status=MagickFalse;
1095 }
1096 }
1097 image_view=DestroyCacheView(image_view);
1098 return(status);
1099}