blob: 547f8cfbe874ba6339411a8c6692ce95c470dc3b [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% PPPP AAA IIIII N N TTTTT %
7% P P A A I NN N T %
8% PPPP AAAAA I N N N T %
9% P A A I N NN T %
10% P A A IIIII N N T %
11% %
12% %
13% Methods to Paint on an Image %
14% %
15% Software Design %
16% John Cristy %
17% July 1998 %
18% %
19% %
cristy1454be72011-12-19 01:52:48 +000020% Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37*/
38
39/*
40 Include declarations.
41*/
cristy4c08aed2011-07-01 19:47:50 +000042#include "MagickCore/studio.h"
43#include "MagickCore/color.h"
44#include "MagickCore/color-private.h"
45#include "MagickCore/colorspace-private.h"
46#include "MagickCore/composite.h"
47#include "MagickCore/composite-private.h"
48#include "MagickCore/draw.h"
49#include "MagickCore/draw-private.h"
50#include "MagickCore/exception.h"
51#include "MagickCore/exception-private.h"
52#include "MagickCore/gem.h"
cristy8ea81222011-09-04 10:33:32 +000053#include "MagickCore/gem-private.h"
cristy4c08aed2011-07-01 19:47:50 +000054#include "MagickCore/monitor.h"
55#include "MagickCore/monitor-private.h"
56#include "MagickCore/paint.h"
57#include "MagickCore/pixel-accessor.h"
cristyac245f82012-05-05 17:13:57 +000058#include "MagickCore/resource_.h"
cristy95f562a2012-01-01 20:49:11 +000059#include "MagickCore/statistic.h"
cristy4c08aed2011-07-01 19:47:50 +000060#include "MagickCore/string_.h"
61#include "MagickCore/thread-private.h"
cristy3ed852e2009-09-05 21:47:34 +000062
cristy3ed852e2009-09-05 21:47:34 +000063/*
64%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
65% %
66% %
67% %
68% F l o o d f i l l P a i n t I m a g e %
69% %
70% %
71% %
72%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73%
74% FloodfillPaintImage() changes the color value of any pixel that matches
75% target and is an immediate neighbor. If the method FillToBorderMethod is
76% specified, the color value is changed for any neighbor pixel that does not
77% match the bordercolor member of image.
78%
cristy908a0002011-08-28 00:13:39 +000079% By default target must match a particular pixel color exactly. However,
80% in many cases two colors may differ by a small amount. The fuzz member of
81% image defines how much tolerance is acceptable to consider two colors as
82% the same. For example, set fuzz to 10 and the color red at intensities of
83% 100 and 102 respectively are now interpreted as the same color for the
84% purposes of the floodfill.
cristy3ed852e2009-09-05 21:47:34 +000085%
86% The format of the FloodfillPaintImage method is:
87%
88% MagickBooleanType FloodfillPaintImage(Image *image,
cristyd42d9952011-07-08 14:21:50 +000089% const DrawInfo *draw_info,const PixelInfo target,
90% const ssize_t x_offset,const ssize_t y_offset,
cristy189e84c2011-08-27 18:08:53 +000091% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +000092%
93% A description of each parameter follows:
94%
95% o image: the image.
96%
cristy3ed852e2009-09-05 21:47:34 +000097% o draw_info: the draw info.
98%
99% o target: the RGB value of the target color.
100%
101% o x_offset,y_offset: the starting location of the operation.
102%
103% o invert: paint any pixel that does not match the target color.
104%
cristy189e84c2011-08-27 18:08:53 +0000105% o exception: return any errors or warnings in this structure.
106%
cristy3ed852e2009-09-05 21:47:34 +0000107*/
108MagickExport MagickBooleanType FloodfillPaintImage(Image *image,
cristyd42d9952011-07-08 14:21:50 +0000109 const DrawInfo *draw_info,const PixelInfo *target,const ssize_t x_offset,
cristy189e84c2011-08-27 18:08:53 +0000110 const ssize_t y_offset,const MagickBooleanType invert,
111 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000112{
113#define MaxStacksize (1UL << 15)
114#define PushSegmentStack(up,left,right,delta) \
115{ \
116 if (s >= (segment_stack+MaxStacksize)) \
117 ThrowBinaryException(DrawError,"SegmentStackOverflow",image->filename) \
118 else \
119 { \
cristybb503372010-05-27 20:51:26 +0000120 if ((((up)+(delta)) >= 0) && (((up)+(delta)) < (ssize_t) image->rows)) \
cristy3ed852e2009-09-05 21:47:34 +0000121 { \
122 s->x1=(double) (left); \
123 s->y1=(double) (up); \
124 s->x2=(double) (right); \
125 s->y2=(double) (delta); \
126 s++; \
127 } \
128 } \
129}
130
cristyb0d3bb92010-09-22 14:37:58 +0000131 CacheView
132 *floodplane_view,
133 *image_view;
134
cristy3ed852e2009-09-05 21:47:34 +0000135 Image
136 *floodplane_image;
137
cristy3ed852e2009-09-05 21:47:34 +0000138 MagickBooleanType
cristy14973ba2011-08-27 23:48:07 +0000139 skip,
140 status;
cristy3ed852e2009-09-05 21:47:34 +0000141
cristy4c08aed2011-07-01 19:47:50 +0000142 PixelInfo
cristyfdcc0182011-12-26 19:33:56 +0000143 fill_color,
cristy3ed852e2009-09-05 21:47:34 +0000144 pixel;
145
cristy3ed852e2009-09-05 21:47:34 +0000146 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);
cristy768165d2012-04-09 15:01:35 +0000175 if (IsGrayColorspace(image->colorspace) != MagickFalse)
176 (void) TransformImageColorspace(image,sRGBColorspace,exception);
cristy3ee7de52012-04-14 23:40:47 +0000177 if ((image->matte == MagickFalse) && (draw_info->fill.matte != MagickFalse))
cristy5b67d4e2012-02-07 19:43:53 +0000178 (void) SetImageAlpha(image,OpaqueAlpha,exception);
cristy3ed852e2009-09-05 21:47:34 +0000179 /*
180 Set floodfill state.
181 */
cristy95f562a2012-01-01 20:49:11 +0000182 floodplane_image=CloneImage(image,image->columns,image->rows,MagickTrue,
183 exception);
cristy3ed852e2009-09-05 21:47:34 +0000184 if (floodplane_image == (Image *) NULL)
185 return(MagickFalse);
cristy20990102012-05-16 18:10:49 +0000186 floodplane_image->matte=MagickFalse;
187 floodplane_image->colorspace=GRAYColorspace;
188 (void) QueryColorCompliance("#000",AllCompliance,
189 &floodplane_image->background_color,exception);
190 (void) SetImageBackgroundColor(floodplane_image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000191 segment_stack=(SegmentInfo *) AcquireQuantumMemory(MaxStacksize,
192 sizeof(*segment_stack));
193 if (segment_stack == (SegmentInfo *) NULL)
194 {
195 floodplane_image=DestroyImage(floodplane_image);
196 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
197 image->filename);
198 }
199 /*
200 Push initial segment on stack.
201 */
cristy14973ba2011-08-27 23:48:07 +0000202 status=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +0000203 x=x_offset;
204 y=y_offset;
205 start=0;
206 s=segment_stack;
207 PushSegmentStack(y,x,x,1);
208 PushSegmentStack(y+1,x,x,-1);
cristy4c08aed2011-07-01 19:47:50 +0000209 GetPixelInfo(image,&pixel);
cristydb070952012-04-20 14:33:00 +0000210 image_view=AcquireVirtualCacheView(image,exception);
211 floodplane_view=AcquireAuthenticCacheView(floodplane_image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000212 while (s > segment_stack)
213 {
cristy4c08aed2011-07-01 19:47:50 +0000214 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000215 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000216
cristy4c08aed2011-07-01 19:47:50 +0000217 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000218 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000219
cristy14973ba2011-08-27 23:48:07 +0000220 register ssize_t
221 x;
222
cristy3ed852e2009-09-05 21:47:34 +0000223 /*
224 Pop segment off stack.
225 */
226 s--;
cristybb503372010-05-27 20:51:26 +0000227 x1=(ssize_t) s->x1;
228 x2=(ssize_t) s->x2;
229 offset=(ssize_t) s->y2;
230 y=(ssize_t) s->y1+offset;
cristy3ed852e2009-09-05 21:47:34 +0000231 /*
232 Recolor neighboring pixels.
233 */
cristyb0d3bb92010-09-22 14:37:58 +0000234 p=GetCacheViewVirtualPixels(image_view,0,y,(size_t) (x1+1),1,exception);
235 q=GetCacheViewAuthenticPixels(floodplane_view,0,y,(size_t) (x1+1),1,
cristy3ed852e2009-09-05 21:47:34 +0000236 exception);
cristy4c08aed2011-07-01 19:47:50 +0000237 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000238 break;
cristyed231572011-07-14 02:18:59 +0000239 p+=x1*GetPixelChannels(image);
240 q+=x1*GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000241 for (x=x1; x >= 0; x--)
242 {
cristy95f562a2012-01-01 20:49:11 +0000243 if (GetPixelGray(floodplane_image,q) != 0)
cristy3ed852e2009-09-05 21:47:34 +0000244 break;
cristy803640d2011-11-17 02:11:32 +0000245 GetPixelInfoPixel(image,p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000246 if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert)
cristy3ed852e2009-09-05 21:47:34 +0000247 break;
cristy95f562a2012-01-01 20:49:11 +0000248 SetPixelGray(floodplane_image,QuantumRange,q);
cristyed231572011-07-14 02:18:59 +0000249 p-=GetPixelChannels(image);
250 q-=GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000251 }
cristyb0d3bb92010-09-22 14:37:58 +0000252 if (SyncCacheViewAuthenticPixels(floodplane_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000253 break;
254 skip=x >= x1 ? MagickTrue : MagickFalse;
255 if (skip == MagickFalse)
256 {
257 start=x+1;
258 if (start < x1)
259 PushSegmentStack(y,start,x1-1,-offset);
260 x=x1+1;
261 }
262 do
263 {
264 if (skip == MagickFalse)
265 {
cristybb503372010-05-27 20:51:26 +0000266 if (x < (ssize_t) image->columns)
cristy3ed852e2009-09-05 21:47:34 +0000267 {
cristyb0d3bb92010-09-22 14:37:58 +0000268 p=GetCacheViewVirtualPixels(image_view,x,y,image->columns-x,1,
cristy3ed852e2009-09-05 21:47:34 +0000269 exception);
cristyfdcc0182011-12-26 19:33:56 +0000270 q=GetCacheViewAuthenticPixels(floodplane_view,x,y,image->columns-
271 x,1,exception);
cristy636dcb52011-08-26 13:23:49 +0000272 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000273 break;
cristybb503372010-05-27 20:51:26 +0000274 for ( ; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000275 {
cristy95f562a2012-01-01 20:49:11 +0000276 if (GetPixelGray(floodplane_image,q) != 0)
cristy3ed852e2009-09-05 21:47:34 +0000277 break;
cristy803640d2011-11-17 02:11:32 +0000278 GetPixelInfoPixel(image,p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000279 if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert)
cristy3ed852e2009-09-05 21:47:34 +0000280 break;
cristy95f562a2012-01-01 20:49:11 +0000281 SetPixelGray(floodplane_image,QuantumRange,q);
cristyed231572011-07-14 02:18:59 +0000282 p+=GetPixelChannels(image);
283 q+=GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000284 }
cristy14973ba2011-08-27 23:48:07 +0000285 status=SyncCacheViewAuthenticPixels(floodplane_view,exception);
286 if (status == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000287 break;
288 }
289 PushSegmentStack(y,start,x-1,offset);
290 if (x > (x2+1))
291 PushSegmentStack(y,x2+1,x-1,-offset);
292 }
293 skip=MagickFalse;
294 x++;
295 if (x <= x2)
296 {
cristyb0d3bb92010-09-22 14:37:58 +0000297 p=GetCacheViewVirtualPixels(image_view,x,y,(size_t) (x2-x+1),1,
298 exception);
299 q=GetCacheViewAuthenticPixels(floodplane_view,x,y,(size_t) (x2-x+1),1,
cristy3ed852e2009-09-05 21:47:34 +0000300 exception);
cristy636dcb52011-08-26 13:23:49 +0000301 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000302 break;
cristy3ed852e2009-09-05 21:47:34 +0000303 for ( ; x <= x2; x++)
304 {
cristy95f562a2012-01-01 20:49:11 +0000305 if (GetPixelGray(floodplane_image,q) != 0)
cristy3ed852e2009-09-05 21:47:34 +0000306 break;
cristy803640d2011-11-17 02:11:32 +0000307 GetPixelInfoPixel(image,p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000308 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
cristy3ed852e2009-09-05 21:47:34 +0000309 break;
cristyed231572011-07-14 02:18:59 +0000310 p+=GetPixelChannels(image);
311 q+=GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000312 }
313 }
314 start=x;
315 } while (x <= x2);
316 }
cristybb503372010-05-27 20:51:26 +0000317 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000318 {
cristy4c08aed2011-07-01 19:47:50 +0000319 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000320 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000321
cristy4c08aed2011-07-01 19:47:50 +0000322 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000323 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000324
cristy14973ba2011-08-27 23:48:07 +0000325 register ssize_t
326 x;
327
cristy3ed852e2009-09-05 21:47:34 +0000328 /*
329 Tile fill color onto floodplane.
330 */
cristy95f562a2012-01-01 20:49:11 +0000331 p=GetCacheViewVirtualPixels(floodplane_view,0,y,image->columns,1,exception);
cristyb0d3bb92010-09-22 14:37:58 +0000332 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000333 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000334 break;
cristybb503372010-05-27 20:51:26 +0000335 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000336 {
cristy95f562a2012-01-01 20:49:11 +0000337 if (GetPixelGray(floodplane_image,p) != 0)
cristy3ed852e2009-09-05 21:47:34 +0000338 {
cristy2ed42f62011-10-02 19:49:57 +0000339 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
cristy8a20fa02011-12-27 15:54:31 +0000340 SetPixelInfoPixel(image,&fill_color,q);
cristy3ed852e2009-09-05 21:47:34 +0000341 }
cristyed231572011-07-14 02:18:59 +0000342 p+=GetPixelChannels(floodplane_image);
343 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000344 }
cristyb0d3bb92010-09-22 14:37:58 +0000345 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000346 break;
347 }
cristyb0d3bb92010-09-22 14:37:58 +0000348 floodplane_view=DestroyCacheView(floodplane_view);
349 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +0000350 segment_stack=(SegmentInfo *) RelinquishMagickMemory(segment_stack);
351 floodplane_image=DestroyImage(floodplane_image);
cristybb503372010-05-27 20:51:26 +0000352 return(y == (ssize_t) image->rows ? MagickTrue : MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +0000353}
354
355/*
356%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
357% %
358% %
359% %
360+ G r a d i e n t I m a g e %
361% %
362% %
363% %
364%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
365%
cristycee97112010-05-28 00:44:52 +0000366% GradientImage() applies a continuously smooth color transitions along a
cristy3ed852e2009-09-05 21:47:34 +0000367% vector from one color to another.
368%
369% Note, the interface of this method will change in the future to support
370% more than one transistion.
371%
372% The format of the GradientImage method is:
373%
374% MagickBooleanType GradientImage(Image *image,const GradientType type,
cristy101ab702011-10-13 13:06:32 +0000375% const SpreadMethod method,const PixelInfo *start_color,
376% const PixelInfo *stop_color,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000377%
378% A description of each parameter follows:
379%
380% o image: the image.
381%
382% o type: the gradient type: linear or radial.
383%
384% o spread: the gradient spread meathod: pad, reflect, or repeat.
385%
386% o start_color: the start color.
387%
388% o stop_color: the stop color.
389%
cristy189e84c2011-08-27 18:08:53 +0000390% o exception: return any errors or warnings in this structure.
391%
cristy3ed852e2009-09-05 21:47:34 +0000392*/
cristy117ff172010-08-15 21:35:32 +0000393
394static inline double MagickMax(const double x,const double y)
395{
396 return(x > y ? x : y);
397}
398
cristy3ed852e2009-09-05 21:47:34 +0000399MagickExport MagickBooleanType GradientImage(Image *image,
400 const GradientType type,const SpreadMethod method,
cristy101ab702011-10-13 13:06:32 +0000401 const PixelInfo *start_color,const PixelInfo *stop_color,
cristy189e84c2011-08-27 18:08:53 +0000402 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000403{
404 DrawInfo
405 *draw_info;
406
407 GradientInfo
408 *gradient;
409
410 MagickBooleanType
411 status;
412
cristybb503372010-05-27 20:51:26 +0000413 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000414 i;
415
416 /*
417 Set gradient start-stop end points.
418 */
419 assert(image != (const Image *) NULL);
420 assert(image->signature == MagickSignature);
421 if (image->debug != MagickFalse)
422 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy101ab702011-10-13 13:06:32 +0000423 assert(start_color != (const PixelInfo *) NULL);
424 assert(stop_color != (const PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000425 draw_info=AcquireDrawInfo();
426 gradient=(&draw_info->gradient);
427 gradient->type=type;
428 gradient->bounding_box.width=image->columns;
429 gradient->bounding_box.height=image->rows;
430 gradient->gradient_vector.x2=(double) image->columns-1.0;
431 gradient->gradient_vector.y2=(double) image->rows-1.0;
432 if ((type == LinearGradient) && (gradient->gradient_vector.y2 != 0.0))
433 gradient->gradient_vector.x2=0.0;
434 gradient->center.x=(double) gradient->gradient_vector.x2/2.0;
435 gradient->center.y=(double) gradient->gradient_vector.y2/2.0;
436 gradient->radius=MagickMax(gradient->center.x,gradient->center.y);
437 gradient->spread=method;
438 /*
439 Define the gradient to fill between the stops.
440 */
441 gradient->number_stops=2;
442 gradient->stops=(StopInfo *) AcquireQuantumMemory(gradient->number_stops,
443 sizeof(*gradient->stops));
444 if (gradient->stops == (StopInfo *) NULL)
445 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
446 image->filename);
447 (void) ResetMagickMemory(gradient->stops,0,gradient->number_stops*
448 sizeof(*gradient->stops));
cristybb503372010-05-27 20:51:26 +0000449 for (i=0; i < (ssize_t) gradient->number_stops; i++)
cristy4c08aed2011-07-01 19:47:50 +0000450 GetPixelInfo(image,&gradient->stops[i].color);
cristy9d8c8ce2011-10-25 16:13:52 +0000451 gradient->stops[0].color=(*start_color);
cristy3ed852e2009-09-05 21:47:34 +0000452 gradient->stops[0].offset=0.0;
cristy9d8c8ce2011-10-25 16:13:52 +0000453 gradient->stops[1].color=(*stop_color);
cristy3ed852e2009-09-05 21:47:34 +0000454 gradient->stops[1].offset=1.0;
455 /*
456 Draw a gradient on the image.
457 */
cristy947cb4c2011-10-20 18:41:46 +0000458 status=DrawGradientImage(image,draw_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000459 draw_info=DestroyDrawInfo(draw_info);
cristy09250192012-02-07 18:53:31 +0000460 if ((start_color->matte == MagickFalse) && (stop_color->matte == MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +0000461 image->matte=MagickFalse;
cristy101ab702011-10-13 13:06:32 +0000462 if ((IsPixelInfoGray(start_color) != MagickFalse) &&
463 (IsPixelInfoGray(stop_color) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +0000464 image->type=GrayscaleType;
465 return(status);
466}
467
468/*
469%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
470% %
471% %
472% %
473% O i l P a i n t I m a g e %
474% %
475% %
476% %
477%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
478%
479% OilPaintImage() applies a special effect filter that simulates an oil
480% painting. Each pixel is replaced by the most frequent color occurring
481% in a circular region defined by radius.
482%
483% The format of the OilPaintImage method is:
484%
485% Image *OilPaintImage(const Image *image,const double radius,
cristy14973ba2011-08-27 23:48:07 +0000486% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000487%
488% A description of each parameter follows:
489%
490% o image: the image.
491%
492% o radius: the radius of the circular neighborhood.
493%
cristy14973ba2011-08-27 23:48:07 +0000494% o sigma: the standard deviation of the Gaussian, in pixels.
495%
cristy3ed852e2009-09-05 21:47:34 +0000496% o exception: return any errors or warnings in this structure.
497%
498*/
499
cristybb503372010-05-27 20:51:26 +0000500static size_t **DestroyHistogramThreadSet(size_t **histogram)
cristy3ed852e2009-09-05 21:47:34 +0000501{
cristybb503372010-05-27 20:51:26 +0000502 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000503 i;
504
cristybb503372010-05-27 20:51:26 +0000505 assert(histogram != (size_t **) NULL);
cristyac245f82012-05-05 17:13:57 +0000506 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
cristybb503372010-05-27 20:51:26 +0000507 if (histogram[i] != (size_t *) NULL)
508 histogram[i]=(size_t *) RelinquishMagickMemory(histogram[i]);
cristyb41ee102010-10-04 16:46:15 +0000509 histogram=(size_t **) RelinquishMagickMemory(histogram);
cristy3ed852e2009-09-05 21:47:34 +0000510 return(histogram);
511}
512
cristybb503372010-05-27 20:51:26 +0000513static size_t **AcquireHistogramThreadSet(const size_t count)
cristy3ed852e2009-09-05 21:47:34 +0000514{
cristybb503372010-05-27 20:51:26 +0000515 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000516 i;
517
cristybb503372010-05-27 20:51:26 +0000518 size_t
cristy3ed852e2009-09-05 21:47:34 +0000519 **histogram,
520 number_threads;
521
cristyfeeb98d2012-05-09 16:32:12 +0000522 number_threads=GetOpenMPMaximumThreads();
cristy14973ba2011-08-27 23:48:07 +0000523 histogram=(size_t **) AcquireQuantumMemory(number_threads,sizeof(*histogram));
cristybb503372010-05-27 20:51:26 +0000524 if (histogram == (size_t **) NULL)
525 return((size_t **) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000526 (void) ResetMagickMemory(histogram,0,number_threads*sizeof(*histogram));
cristybb503372010-05-27 20:51:26 +0000527 for (i=0; i < (ssize_t) number_threads; i++)
cristy3ed852e2009-09-05 21:47:34 +0000528 {
cristy14973ba2011-08-27 23:48:07 +0000529 histogram[i]=(size_t *) AcquireQuantumMemory(count,sizeof(**histogram));
cristybb503372010-05-27 20:51:26 +0000530 if (histogram[i] == (size_t *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000531 return(DestroyHistogramThreadSet(histogram));
532 }
533 return(histogram);
534}
535
536MagickExport Image *OilPaintImage(const Image *image,const double radius,
cristy14973ba2011-08-27 23:48:07 +0000537 const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000538{
539#define NumberPaintBins 256
540#define OilPaintImageTag "OilPaint/Image"
541
cristyfa112112010-01-04 17:48:07 +0000542 CacheView
543 *image_view,
544 *paint_view;
545
cristy3ed852e2009-09-05 21:47:34 +0000546 Image
547 *paint_image;
548
cristy3ed852e2009-09-05 21:47:34 +0000549 MagickBooleanType
550 status;
551
cristybb503372010-05-27 20:51:26 +0000552 MagickOffsetType
553 progress;
554
555 size_t
cristy14973ba2011-08-27 23:48:07 +0000556 **histograms,
cristy3ed852e2009-09-05 21:47:34 +0000557 width;
558
cristybb503372010-05-27 20:51:26 +0000559 ssize_t
cristyf8561542012-01-24 00:26:46 +0000560 center,
cristybb503372010-05-27 20:51:26 +0000561 y;
562
cristy3ed852e2009-09-05 21:47:34 +0000563 /*
564 Initialize painted image attributes.
565 */
566 assert(image != (const Image *) NULL);
567 assert(image->signature == MagickSignature);
568 if (image->debug != MagickFalse)
569 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
570 assert(exception != (ExceptionInfo *) NULL);
571 assert(exception->signature == MagickSignature);
cristy14973ba2011-08-27 23:48:07 +0000572 width=GetOptimalKernelWidth2D(radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +0000573 paint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
574 if (paint_image == (Image *) NULL)
575 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000576 if (SetImageStorageClass(paint_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000577 {
cristy3ed852e2009-09-05 21:47:34 +0000578 paint_image=DestroyImage(paint_image);
579 return((Image *) NULL);
580 }
581 histograms=AcquireHistogramThreadSet(NumberPaintBins);
cristybb503372010-05-27 20:51:26 +0000582 if (histograms == (size_t **) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000583 {
584 paint_image=DestroyImage(paint_image);
585 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
586 }
587 /*
588 Oil paint image.
589 */
590 status=MagickTrue;
591 progress=0;
cristyd09f8802012-02-04 16:44:10 +0000592 center=(ssize_t) GetPixelChannels(image)*(image->columns+width)*(width/2L)+
593 GetPixelChannels(image)*(width/2L);
cristydb070952012-04-20 14:33:00 +0000594 image_view=AcquireVirtualCacheView(image,exception);
595 paint_view=AcquireAuthenticCacheView(paint_image,exception);
cristyb5d5f722009-11-04 03:03:49 +0000596#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000597 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +0000598 dynamic_number_threads(image,image->columns,image->rows,1)
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
cristy4c08aed2011-07-01 19:47:50 +0000605 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000606 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000607
cristybb503372010-05-27 20:51:26 +0000608 register size_t
cristy3ed852e2009-09-05 21:47:34 +0000609 *histogram;
610
cristy14973ba2011-08-27 23:48:07 +0000611 register ssize_t
612 x;
613
cristy3ed852e2009-09-05 21:47:34 +0000614 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,
cristyf8561542012-01-24 00:26:46 +0000638 n,
cristy9d314ff2011-03-09 01:30:28 +0000639 v;
640
cristy3ed852e2009-09-05 21:47:34 +0000641 /*
642 Assign most frequent color.
643 */
cristyf8561542012-01-24 00:26:46 +0000644 k=0;
cristy3ed852e2009-09-05 21:47:34 +0000645 j=0;
646 count=0;
cristyf8561542012-01-24 00:26:46 +0000647 (void) ResetMagickMemory(histogram,0,NumberPaintBins* sizeof(*histogram));
cristybb503372010-05-27 20:51:26 +0000648 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +0000649 {
cristybb503372010-05-27 20:51:26 +0000650 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +0000651 {
cristyf8561542012-01-24 00:26:46 +0000652 n=(ssize_t) ScaleQuantumToChar(GetPixelIntensity(image,p+
653 GetPixelChannels(image)*(u+k)));
654 histogram[n]++;
655 if (histogram[n] > count)
cristy3ed852e2009-09-05 21:47:34 +0000656 {
cristyf8561542012-01-24 00:26:46 +0000657 j=k+u;
658 count=histogram[n];
cristy3ed852e2009-09-05 21:47:34 +0000659 }
660 }
cristyf8561542012-01-24 00:26:46 +0000661 k+=(ssize_t) (image->columns+width);
cristy3ed852e2009-09-05 21:47:34 +0000662 }
cristy177e41c2012-04-15 15:08:25 +0000663 if (GetPixelMask(image,p) != 0)
664 {
665 p+=GetPixelChannels(image);
666 q+=GetPixelChannels(paint_image);
667 continue;
668 }
cristyf8561542012-01-24 00:26:46 +0000669 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
670 {
671 PixelChannel
672 channel;
673
674 PixelTrait
675 paint_traits,
676 traits;
677
678 channel=GetPixelChannelMapChannel(image,i);
679 traits=GetPixelChannelMapTraits(image,channel);
680 paint_traits=GetPixelChannelMapTraits(paint_image,channel);
681 if ((traits == UndefinedPixelTrait) ||
682 (paint_traits == UndefinedPixelTrait))
683 continue;
cristy177e41c2012-04-15 15:08:25 +0000684 if ((paint_traits & CopyPixelTrait) != 0)
cristyf8561542012-01-24 00:26:46 +0000685 {
686 SetPixelChannel(paint_image,channel,p[center+i],q);
687 continue;
688 }
689 SetPixelChannel(paint_image,channel,p[j*GetPixelChannels(image)+i],q);
690 }
cristyed231572011-07-14 02:18:59 +0000691 p+=GetPixelChannels(image);
692 q+=GetPixelChannels(paint_image);
cristy3ed852e2009-09-05 21:47:34 +0000693 }
694 if (SyncCacheViewAuthenticPixels(paint_view,exception) == MagickFalse)
695 status=MagickFalse;
696 if (image->progress_monitor != (MagickProgressMonitor) NULL)
697 {
698 MagickBooleanType
699 proceed;
700
cristyb5d5f722009-11-04 03:03:49 +0000701#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy95338b32012-01-22 03:34:36 +0000702 #pragma omp critical (MagickCore_OilPaintImage)
cristy3ed852e2009-09-05 21:47:34 +0000703#endif
704 proceed=SetImageProgress(image,OilPaintImageTag,progress++,image->rows);
705 if (proceed == MagickFalse)
706 status=MagickFalse;
707 }
708 }
709 paint_view=DestroyCacheView(paint_view);
710 image_view=DestroyCacheView(image_view);
711 histograms=DestroyHistogramThreadSet(histograms);
712 if (status == MagickFalse)
713 paint_image=DestroyImage(paint_image);
714 return(paint_image);
715}
716
717/*
718%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
719% %
720% %
721% %
722% O p a q u e P a i n t I m a g e %
723% %
724% %
725% %
726%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
727%
728% OpaquePaintImage() changes any pixel that matches color with the color
729% defined by fill.
730%
cristy14973ba2011-08-27 23:48:07 +0000731% By default color must match a particular pixel color exactly. However, in
732% many cases two colors may differ by a small amount. Fuzz defines how much
733% tolerance is acceptable to consider two colors as the same. For example,
734% set fuzz to 10 and the color red at intensities of 100 and 102 respectively
735% are now interpreted as the same color.
cristy3ed852e2009-09-05 21:47:34 +0000736%
737% The format of the OpaquePaintImage method is:
738%
739% MagickBooleanType OpaquePaintImage(Image *image,
cristy101ab702011-10-13 13:06:32 +0000740% const PixelInfo *target,const PixelInfo *fill,
cristy189e84c2011-08-27 18:08:53 +0000741% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000742%
743% A description of each parameter follows:
744%
745% o image: the image.
746%
cristy3ed852e2009-09-05 21:47:34 +0000747% o target: the RGB value of the target color.
748%
749% o fill: the replacement color.
750%
751% o invert: paint any pixel that does not match the target color.
752%
cristy189e84c2011-08-27 18:08:53 +0000753% o exception: return any errors or warnings in this structure.
754%
cristy3ed852e2009-09-05 21:47:34 +0000755*/
cristy3ed852e2009-09-05 21:47:34 +0000756MagickExport MagickBooleanType OpaquePaintImage(Image *image,
cristy189e84c2011-08-27 18:08:53 +0000757 const PixelInfo *target,const PixelInfo *fill,const MagickBooleanType invert,
758 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000759{
760#define OpaquePaintImageTag "Opaque/Image"
761
cristyc4c8d132010-01-07 01:58:38 +0000762 CacheView
763 *image_view;
764
cristy3ed852e2009-09-05 21:47:34 +0000765 MagickBooleanType
766 status;
767
cristybb503372010-05-27 20:51:26 +0000768 MagickOffsetType
769 progress;
770
cristy4c08aed2011-07-01 19:47:50 +0000771 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000772 zero;
773
cristybb503372010-05-27 20:51:26 +0000774 ssize_t
775 y;
776
cristy3ed852e2009-09-05 21:47:34 +0000777 assert(image != (Image *) NULL);
778 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +0000779 assert(target != (PixelInfo *) NULL);
780 assert(fill != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000781 if (image->debug != MagickFalse)
782 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy574cc262011-08-05 01:23:58 +0000783 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000784 return(MagickFalse);
cristy768165d2012-04-09 15:01:35 +0000785 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
786 (IsPixelInfoGray(fill) != MagickFalse))
787 (void) TransformImageColorspace(image,sRGBColorspace,exception);
cristy09250192012-02-07 18:53:31 +0000788 if ((fill->matte != MagickFalse) && (image->matte == MagickFalse))
789 (void) SetImageAlpha(image,OpaqueAlpha,exception);
cristy3ed852e2009-09-05 21:47:34 +0000790 /*
791 Make image color opaque.
792 */
793 status=MagickTrue;
794 progress=0;
cristy4c08aed2011-07-01 19:47:50 +0000795 GetPixelInfo(image,&zero);
cristydb070952012-04-20 14:33:00 +0000796 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +0000797#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000798 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +0000799 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +0000800#endif
cristybb503372010-05-27 20:51:26 +0000801 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000802 {
cristy4c08aed2011-07-01 19:47:50 +0000803 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000804 pixel;
805
cristy4c08aed2011-07-01 19:47:50 +0000806 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000807 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000808
cristy14973ba2011-08-27 23:48:07 +0000809 register ssize_t
810 x;
811
cristy3ed852e2009-09-05 21:47:34 +0000812 if (status == MagickFalse)
813 continue;
814 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy14973ba2011-08-27 23:48:07 +0000815 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000816 {
817 status=MagickFalse;
818 continue;
819 }
cristy3ed852e2009-09-05 21:47:34 +0000820 pixel=zero;
cristybb503372010-05-27 20:51:26 +0000821 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000822 {
cristy803640d2011-11-17 02:11:32 +0000823 GetPixelInfoPixel(image,q,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000824 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
cristy95f562a2012-01-01 20:49:11 +0000825 SetPixelInfoPixel(image,fill,q);
cristyed231572011-07-14 02:18:59 +0000826 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000827 }
828 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
829 status=MagickFalse;
830 if (image->progress_monitor != (MagickProgressMonitor) NULL)
831 {
832 MagickBooleanType
833 proceed;
834
cristyb5d5f722009-11-04 03:03:49 +0000835#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy95338b32012-01-22 03:34:36 +0000836 #pragma omp critical (MagickCore_OpaquePaintImage)
cristy3ed852e2009-09-05 21:47:34 +0000837#endif
838 proceed=SetImageProgress(image,OpaquePaintImageTag,progress++,
839 image->rows);
840 if (proceed == MagickFalse)
841 status=MagickFalse;
842 }
843 }
844 image_view=DestroyCacheView(image_view);
845 return(status);
846}
847
848/*
849%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
850% %
851% %
852% %
853% T r a n s p a r e n t P a i n t I m a g e %
854% %
855% %
856% %
857%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
858%
859% TransparentPaintImage() changes the opacity value associated with any pixel
860% that matches color to the value defined by opacity.
861%
cristy14973ba2011-08-27 23:48:07 +0000862% By default color must match a particular pixel color exactly. However, in
863% many cases two colors may differ by a small amount. Fuzz defines how much
864% tolerance is acceptable to consider two colors as the same. For example,
865% set fuzz to 10 and the color red at intensities of 100 and 102 respectively
866% are now interpreted as the same color.
cristy3ed852e2009-09-05 21:47:34 +0000867%
868% The format of the TransparentPaintImage method is:
869%
870% MagickBooleanType TransparentPaintImage(Image *image,
cristy4c08aed2011-07-01 19:47:50 +0000871% const PixelInfo *target,const Quantum opacity,
cristy189e84c2011-08-27 18:08:53 +0000872% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000873%
874% A description of each parameter follows:
875%
876% o image: the image.
877%
878% o target: the target color.
879%
880% o opacity: the replacement opacity value.
881%
882% o invert: paint any pixel that does not match the target color.
883%
cristy189e84c2011-08-27 18:08:53 +0000884% o exception: return any errors or warnings in this structure.
885%
cristy3ed852e2009-09-05 21:47:34 +0000886*/
887MagickExport MagickBooleanType TransparentPaintImage(Image *image,
cristy14973ba2011-08-27 23:48:07 +0000888 const PixelInfo *target,const Quantum opacity,const MagickBooleanType invert,
889 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000890{
891#define TransparentPaintImageTag "Transparent/Image"
892
cristyc4c8d132010-01-07 01:58:38 +0000893 CacheView
894 *image_view;
895
cristy3ed852e2009-09-05 21:47:34 +0000896 MagickBooleanType
897 status;
898
cristybb503372010-05-27 20:51:26 +0000899 MagickOffsetType
900 progress;
901
cristy4c08aed2011-07-01 19:47:50 +0000902 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000903 zero;
904
cristybb503372010-05-27 20:51:26 +0000905 ssize_t
906 y;
907
cristy3ed852e2009-09-05 21:47:34 +0000908 assert(image != (Image *) NULL);
909 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +0000910 assert(target != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000911 if (image->debug != MagickFalse)
912 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy574cc262011-08-05 01:23:58 +0000913 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000914 return(MagickFalse);
915 if (image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +0000916 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +0000917 /*
918 Make image color transparent.
919 */
920 status=MagickTrue;
921 progress=0;
cristy4c08aed2011-07-01 19:47:50 +0000922 GetPixelInfo(image,&zero);
cristydb070952012-04-20 14:33:00 +0000923 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +0000924#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000925 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +0000926 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +0000927#endif
cristybb503372010-05-27 20:51:26 +0000928 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000929 {
cristy4c08aed2011-07-01 19:47:50 +0000930 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000931 pixel;
932
cristybb503372010-05-27 20:51:26 +0000933 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000934 x;
935
cristy4c08aed2011-07-01 19:47:50 +0000936 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000937 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000938
939 if (status == MagickFalse)
940 continue;
941 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy14973ba2011-08-27 23:48:07 +0000942 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000943 {
944 status=MagickFalse;
945 continue;
946 }
cristy3ed852e2009-09-05 21:47:34 +0000947 pixel=zero;
cristybb503372010-05-27 20:51:26 +0000948 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000949 {
cristy803640d2011-11-17 02:11:32 +0000950 GetPixelInfoPixel(image,q,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000951 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
952 SetPixelAlpha(image,opacity,q);
cristyed231572011-07-14 02:18:59 +0000953 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000954 }
955 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
956 status=MagickFalse;
957 if (image->progress_monitor != (MagickProgressMonitor) NULL)
958 {
959 MagickBooleanType
960 proceed;
961
cristyb5d5f722009-11-04 03:03:49 +0000962#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy95338b32012-01-22 03:34:36 +0000963 #pragma omp critical (MagickCore_TransparentPaintImage)
cristy3ed852e2009-09-05 21:47:34 +0000964#endif
965 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
966 image->rows);
967 if (proceed == MagickFalse)
968 status=MagickFalse;
969 }
970 }
971 image_view=DestroyCacheView(image_view);
972 return(status);
973}
974
975/*
976%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
977% %
978% %
979% %
980% 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 %
981% %
982% %
983% %
984%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
985%
986% TransparentPaintImageChroma() changes the opacity value associated with any
987% pixel that matches color to the value defined by opacity.
988%
cristy14973ba2011-08-27 23:48:07 +0000989% As there is one fuzz value for the all the channels, TransparentPaintImage()
990% is not suitable for the operations like chroma, where the tolerance for
991% similarity of two color component (RGB) can be different. Thus we define
992% this method to take two target pixels (one low and one high) and all the
993% pixels of an image which are lying between these two pixels are made
994% transparent.
cristy3ed852e2009-09-05 21:47:34 +0000995%
cristy14973ba2011-08-27 23:48:07 +0000996% The format of the TransparentPaintImageChroma method is:
cristy3ed852e2009-09-05 21:47:34 +0000997%
cristy14973ba2011-08-27 23:48:07 +0000998% MagickBooleanType TransparentPaintImageChroma(Image *image,
999% const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
1000% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001001%
1002% A description of each parameter follows:
1003%
1004% o image: the image.
1005%
1006% o low: the low target color.
1007%
1008% o high: the high target color.
1009%
1010% o opacity: the replacement opacity value.
1011%
1012% o invert: paint any pixel that does not match the target color.
1013%
cristy189e84c2011-08-27 18:08:53 +00001014% o exception: return any errors or warnings in this structure.
1015%
cristy3ed852e2009-09-05 21:47:34 +00001016*/
1017MagickExport MagickBooleanType TransparentPaintImageChroma(Image *image,
cristy189e84c2011-08-27 18:08:53 +00001018 const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
1019 const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001020{
1021#define TransparentPaintImageTag "Transparent/Image"
1022
cristyc4c8d132010-01-07 01:58:38 +00001023 CacheView
1024 *image_view;
1025
cristy3ed852e2009-09-05 21:47:34 +00001026 MagickBooleanType
1027 status;
1028
cristybb503372010-05-27 20:51:26 +00001029 MagickOffsetType
1030 progress;
1031
1032 ssize_t
1033 y;
1034
cristy3ed852e2009-09-05 21:47:34 +00001035 assert(image != (Image *) NULL);
1036 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +00001037 assert(high != (PixelInfo *) NULL);
1038 assert(low != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001039 if (image->debug != MagickFalse)
1040 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy574cc262011-08-05 01:23:58 +00001041 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001042 return(MagickFalse);
1043 if (image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00001044 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00001045 /*
1046 Make image color transparent.
1047 */
1048 status=MagickTrue;
1049 progress=0;
cristydb070952012-04-20 14:33:00 +00001050 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00001051#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00001052 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00001053 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00001054#endif
cristybb503372010-05-27 20:51:26 +00001055 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001056 {
1057 MagickBooleanType
1058 match;
1059
cristy4c08aed2011-07-01 19:47:50 +00001060 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001061 pixel;
1062
cristy4c08aed2011-07-01 19:47:50 +00001063 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001064 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001065
cristy14973ba2011-08-27 23:48:07 +00001066 register ssize_t
1067 x;
1068
cristy3ed852e2009-09-05 21:47:34 +00001069 if (status == MagickFalse)
1070 continue;
1071 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy14973ba2011-08-27 23:48:07 +00001072 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001073 {
1074 status=MagickFalse;
1075 continue;
1076 }
cristy4c08aed2011-07-01 19:47:50 +00001077 GetPixelInfo(image,&pixel);
cristybb503372010-05-27 20:51:26 +00001078 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001079 {
cristy803640d2011-11-17 02:11:32 +00001080 GetPixelInfoPixel(image,q,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001081 match=((pixel.red >= low->red) && (pixel.red <= high->red) &&
1082 (pixel.green >= low->green) && (pixel.green <= high->green) &&
cristy14973ba2011-08-27 23:48:07 +00001083 (pixel.blue >= low->blue) && (pixel.blue <= high->blue)) ? MagickTrue :
1084 MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00001085 if (match != invert)
cristy4c08aed2011-07-01 19:47:50 +00001086 SetPixelAlpha(image,opacity,q);
cristyed231572011-07-14 02:18:59 +00001087 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001088 }
1089 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1090 status=MagickFalse;
1091 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1092 {
1093 MagickBooleanType
1094 proceed;
1095
cristyb5d5f722009-11-04 03:03:49 +00001096#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy95338b32012-01-22 03:34:36 +00001097 #pragma omp critical (MagickCore_TransparentPaintImageChroma)
cristy3ed852e2009-09-05 21:47:34 +00001098#endif
1099 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
1100 image->rows);
1101 if (proceed == MagickFalse)
1102 status=MagickFalse;
1103 }
1104 }
1105 image_view=DestroyCacheView(image_view);
1106 return(status);
1107}