blob: 3b70e53ecc03d6617d5ae4b11ea8bbb987d970f4 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% PPPP AAA IIIII N N TTTTT %
7% P P A A I NN N T %
8% PPPP AAAAA I N N N T %
9% P A A I N NN T %
10% P A A IIIII N N T %
11% %
12% %
13% Methods to Paint on an Image %
14% %
15% Software Design %
16% John Cristy %
17% July 1998 %
18% %
19% %
cristy1454be72011-12-19 01:52:48 +000020% Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37*/
38
39/*
40 Include declarations.
41*/
cristy4c08aed2011-07-01 19:47:50 +000042#include "MagickCore/studio.h"
43#include "MagickCore/color.h"
44#include "MagickCore/color-private.h"
45#include "MagickCore/colorspace-private.h"
46#include "MagickCore/composite.h"
47#include "MagickCore/composite-private.h"
48#include "MagickCore/draw.h"
49#include "MagickCore/draw-private.h"
50#include "MagickCore/exception.h"
51#include "MagickCore/exception-private.h"
52#include "MagickCore/gem.h"
cristy8ea81222011-09-04 10:33:32 +000053#include "MagickCore/gem-private.h"
cristy4c08aed2011-07-01 19:47:50 +000054#include "MagickCore/monitor.h"
55#include "MagickCore/monitor-private.h"
56#include "MagickCore/paint.h"
57#include "MagickCore/pixel-accessor.h"
cristy95f562a2012-01-01 20:49:11 +000058#include "MagickCore/statistic.h"
cristy4c08aed2011-07-01 19:47:50 +000059#include "MagickCore/string_.h"
60#include "MagickCore/thread-private.h"
cristy3ed852e2009-09-05 21:47:34 +000061
cristy3ed852e2009-09-05 21:47:34 +000062/*
63%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
64% %
65% %
66% %
67% F l o o d f i l l P a i n t I m a g e %
68% %
69% %
70% %
71%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72%
73% FloodfillPaintImage() changes the color value of any pixel that matches
74% target and is an immediate neighbor. If the method FillToBorderMethod is
75% specified, the color value is changed for any neighbor pixel that does not
76% match the bordercolor member of image.
77%
cristy908a0002011-08-28 00:13:39 +000078% By default target must match a particular pixel color exactly. However,
79% in many cases two colors may differ by a small amount. The fuzz member of
80% image defines how much tolerance is acceptable to consider two colors as
81% the same. For example, set fuzz to 10 and the color red at intensities of
82% 100 and 102 respectively are now interpreted as the same color for the
83% purposes of the floodfill.
cristy3ed852e2009-09-05 21:47:34 +000084%
85% The format of the FloodfillPaintImage method is:
86%
87% MagickBooleanType FloodfillPaintImage(Image *image,
cristyd42d9952011-07-08 14:21:50 +000088% const DrawInfo *draw_info,const PixelInfo target,
89% const ssize_t x_offset,const ssize_t y_offset,
cristy189e84c2011-08-27 18:08:53 +000090% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +000091%
92% A description of each parameter follows:
93%
94% o image: the image.
95%
cristy3ed852e2009-09-05 21:47:34 +000096% o draw_info: the draw info.
97%
98% o target: the RGB value of the target color.
99%
100% o x_offset,y_offset: the starting location of the operation.
101%
102% o invert: paint any pixel that does not match the target color.
103%
cristy189e84c2011-08-27 18:08:53 +0000104% o exception: return any errors or warnings in this structure.
105%
cristy3ed852e2009-09-05 21:47:34 +0000106*/
107MagickExport MagickBooleanType FloodfillPaintImage(Image *image,
cristyd42d9952011-07-08 14:21:50 +0000108 const DrawInfo *draw_info,const PixelInfo *target,const ssize_t x_offset,
cristy189e84c2011-08-27 18:08:53 +0000109 const ssize_t y_offset,const MagickBooleanType invert,
110 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000111{
112#define MaxStacksize (1UL << 15)
113#define PushSegmentStack(up,left,right,delta) \
114{ \
115 if (s >= (segment_stack+MaxStacksize)) \
116 ThrowBinaryException(DrawError,"SegmentStackOverflow",image->filename) \
117 else \
118 { \
cristybb503372010-05-27 20:51:26 +0000119 if ((((up)+(delta)) >= 0) && (((up)+(delta)) < (ssize_t) image->rows)) \
cristy3ed852e2009-09-05 21:47:34 +0000120 { \
121 s->x1=(double) (left); \
122 s->y1=(double) (up); \
123 s->x2=(double) (right); \
124 s->y2=(double) (delta); \
125 s++; \
126 } \
127 } \
128}
129
cristyb0d3bb92010-09-22 14:37:58 +0000130 CacheView
131 *floodplane_view,
132 *image_view;
133
cristy3ed852e2009-09-05 21:47:34 +0000134 Image
135 *floodplane_image;
136
cristy3ed852e2009-09-05 21:47:34 +0000137 MagickBooleanType
cristy14973ba2011-08-27 23:48:07 +0000138 skip,
139 status;
cristy3ed852e2009-09-05 21:47:34 +0000140
cristy4c08aed2011-07-01 19:47:50 +0000141 PixelInfo
cristyfdcc0182011-12-26 19:33:56 +0000142 fill_color,
cristy3ed852e2009-09-05 21:47:34 +0000143 pixel;
144
cristy3ed852e2009-09-05 21:47:34 +0000145 register SegmentInfo
146 *s;
147
148 SegmentInfo
149 *segment_stack;
150
cristy9d314ff2011-03-09 01:30:28 +0000151 ssize_t
152 offset,
153 start,
154 x,
155 x1,
156 x2,
157 y;
158
cristy3ed852e2009-09-05 21:47:34 +0000159 /*
160 Check boundary conditions.
161 */
162 assert(image != (Image *) NULL);
163 assert(image->signature == MagickSignature);
164 if (image->debug != MagickFalse)
165 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
166 assert(draw_info != (DrawInfo *) NULL);
167 assert(draw_info->signature == MagickSignature);
cristybb503372010-05-27 20:51:26 +0000168 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
cristy3ed852e2009-09-05 21:47:34 +0000169 return(MagickFalse);
cristybb503372010-05-27 20:51:26 +0000170 if ((y_offset < 0) || (y_offset >= (ssize_t) image->rows))
cristy3ed852e2009-09-05 21:47:34 +0000171 return(MagickFalse);
cristy574cc262011-08-05 01:23:58 +0000172 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000173 return(MagickFalse);
cristy768165d2012-04-09 15:01:35 +0000174 if (IsGrayColorspace(image->colorspace) != MagickFalse)
175 (void) TransformImageColorspace(image,sRGBColorspace,exception);
cristy707f0632012-02-15 23:40:22 +0000176 if ((target->matte != MagickFalse) && (image->matte == MagickFalse))
cristy5b67d4e2012-02-07 19:43:53 +0000177 (void) SetImageAlpha(image,OpaqueAlpha,exception);
cristy3ed852e2009-09-05 21:47:34 +0000178 /*
179 Set floodfill state.
180 */
cristy95f562a2012-01-01 20:49:11 +0000181 floodplane_image=CloneImage(image,image->columns,image->rows,MagickTrue,
182 exception);
cristy3ed852e2009-09-05 21:47:34 +0000183 if (floodplane_image == (Image *) NULL)
184 return(MagickFalse);
cristy95f562a2012-01-01 20:49:11 +0000185 floodplane_image->colorspace=GRAYColorspace;
186 (void) EvaluateImage(floodplane_image,SetEvaluateOperator,0.0,exception);
cristy3ed852e2009-09-05 21:47:34 +0000187 segment_stack=(SegmentInfo *) AcquireQuantumMemory(MaxStacksize,
188 sizeof(*segment_stack));
189 if (segment_stack == (SegmentInfo *) NULL)
190 {
191 floodplane_image=DestroyImage(floodplane_image);
192 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
193 image->filename);
194 }
195 /*
196 Push initial segment on stack.
197 */
cristy14973ba2011-08-27 23:48:07 +0000198 status=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +0000199 x=x_offset;
200 y=y_offset;
201 start=0;
202 s=segment_stack;
203 PushSegmentStack(y,x,x,1);
204 PushSegmentStack(y+1,x,x,-1);
cristy4c08aed2011-07-01 19:47:50 +0000205 GetPixelInfo(image,&pixel);
cristyb0d3bb92010-09-22 14:37:58 +0000206 image_view=AcquireCacheView(image);
207 floodplane_view=AcquireCacheView(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000208 while (s > segment_stack)
209 {
cristy4c08aed2011-07-01 19:47:50 +0000210 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000211 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000212
cristy4c08aed2011-07-01 19:47:50 +0000213 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000214 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000215
cristy14973ba2011-08-27 23:48:07 +0000216 register ssize_t
217 x;
218
cristy3ed852e2009-09-05 21:47:34 +0000219 /*
220 Pop segment off stack.
221 */
222 s--;
cristybb503372010-05-27 20:51:26 +0000223 x1=(ssize_t) s->x1;
224 x2=(ssize_t) s->x2;
225 offset=(ssize_t) s->y2;
226 y=(ssize_t) s->y1+offset;
cristy3ed852e2009-09-05 21:47:34 +0000227 /*
228 Recolor neighboring pixels.
229 */
cristyb0d3bb92010-09-22 14:37:58 +0000230 p=GetCacheViewVirtualPixels(image_view,0,y,(size_t) (x1+1),1,exception);
231 q=GetCacheViewAuthenticPixels(floodplane_view,0,y,(size_t) (x1+1),1,
cristy3ed852e2009-09-05 21:47:34 +0000232 exception);
cristy4c08aed2011-07-01 19:47:50 +0000233 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000234 break;
cristyed231572011-07-14 02:18:59 +0000235 p+=x1*GetPixelChannels(image);
236 q+=x1*GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000237 for (x=x1; x >= 0; x--)
238 {
cristy95f562a2012-01-01 20:49:11 +0000239 if (GetPixelGray(floodplane_image,q) != 0)
cristy3ed852e2009-09-05 21:47:34 +0000240 break;
cristy803640d2011-11-17 02:11:32 +0000241 GetPixelInfoPixel(image,p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000242 if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert)
cristy3ed852e2009-09-05 21:47:34 +0000243 break;
cristy95f562a2012-01-01 20:49:11 +0000244 SetPixelGray(floodplane_image,QuantumRange,q);
cristyed231572011-07-14 02:18:59 +0000245 p-=GetPixelChannels(image);
246 q-=GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000247 }
cristyb0d3bb92010-09-22 14:37:58 +0000248 if (SyncCacheViewAuthenticPixels(floodplane_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000249 break;
250 skip=x >= x1 ? MagickTrue : MagickFalse;
251 if (skip == MagickFalse)
252 {
253 start=x+1;
254 if (start < x1)
255 PushSegmentStack(y,start,x1-1,-offset);
256 x=x1+1;
257 }
258 do
259 {
260 if (skip == MagickFalse)
261 {
cristybb503372010-05-27 20:51:26 +0000262 if (x < (ssize_t) image->columns)
cristy3ed852e2009-09-05 21:47:34 +0000263 {
cristyb0d3bb92010-09-22 14:37:58 +0000264 p=GetCacheViewVirtualPixels(image_view,x,y,image->columns-x,1,
cristy3ed852e2009-09-05 21:47:34 +0000265 exception);
cristyfdcc0182011-12-26 19:33:56 +0000266 q=GetCacheViewAuthenticPixels(floodplane_view,x,y,image->columns-
267 x,1,exception);
cristy636dcb52011-08-26 13:23:49 +0000268 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000269 break;
cristybb503372010-05-27 20:51:26 +0000270 for ( ; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000271 {
cristy95f562a2012-01-01 20:49:11 +0000272 if (GetPixelGray(floodplane_image,q) != 0)
cristy3ed852e2009-09-05 21:47:34 +0000273 break;
cristy803640d2011-11-17 02:11:32 +0000274 GetPixelInfoPixel(image,p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000275 if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert)
cristy3ed852e2009-09-05 21:47:34 +0000276 break;
cristy95f562a2012-01-01 20:49:11 +0000277 SetPixelGray(floodplane_image,QuantumRange,q);
cristyed231572011-07-14 02:18:59 +0000278 p+=GetPixelChannels(image);
279 q+=GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000280 }
cristy14973ba2011-08-27 23:48:07 +0000281 status=SyncCacheViewAuthenticPixels(floodplane_view,exception);
282 if (status == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000283 break;
284 }
285 PushSegmentStack(y,start,x-1,offset);
286 if (x > (x2+1))
287 PushSegmentStack(y,x2+1,x-1,-offset);
288 }
289 skip=MagickFalse;
290 x++;
291 if (x <= x2)
292 {
cristyb0d3bb92010-09-22 14:37:58 +0000293 p=GetCacheViewVirtualPixels(image_view,x,y,(size_t) (x2-x+1),1,
294 exception);
295 q=GetCacheViewAuthenticPixels(floodplane_view,x,y,(size_t) (x2-x+1),1,
cristy3ed852e2009-09-05 21:47:34 +0000296 exception);
cristy636dcb52011-08-26 13:23:49 +0000297 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000298 break;
cristy3ed852e2009-09-05 21:47:34 +0000299 for ( ; x <= x2; x++)
300 {
cristy95f562a2012-01-01 20:49:11 +0000301 if (GetPixelGray(floodplane_image,q) != 0)
cristy3ed852e2009-09-05 21:47:34 +0000302 break;
cristy803640d2011-11-17 02:11:32 +0000303 GetPixelInfoPixel(image,p,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000304 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
cristy3ed852e2009-09-05 21:47:34 +0000305 break;
cristyed231572011-07-14 02:18:59 +0000306 p+=GetPixelChannels(image);
307 q+=GetPixelChannels(floodplane_image);
cristy3ed852e2009-09-05 21:47:34 +0000308 }
309 }
310 start=x;
311 } while (x <= x2);
312 }
cristybb503372010-05-27 20:51:26 +0000313 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000314 {
cristy4c08aed2011-07-01 19:47:50 +0000315 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000316 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000317
cristy4c08aed2011-07-01 19:47:50 +0000318 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000319 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000320
cristy14973ba2011-08-27 23:48:07 +0000321 register ssize_t
322 x;
323
cristy3ed852e2009-09-05 21:47:34 +0000324 /*
325 Tile fill color onto floodplane.
326 */
cristy95f562a2012-01-01 20:49:11 +0000327 p=GetCacheViewVirtualPixels(floodplane_view,0,y,image->columns,1,exception);
cristyb0d3bb92010-09-22 14:37:58 +0000328 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000329 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000330 break;
cristybb503372010-05-27 20:51:26 +0000331 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000332 {
cristy95f562a2012-01-01 20:49:11 +0000333 if (GetPixelGray(floodplane_image,p) != 0)
cristy3ed852e2009-09-05 21:47:34 +0000334 {
cristy2ed42f62011-10-02 19:49:57 +0000335 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
cristy8a20fa02011-12-27 15:54:31 +0000336 SetPixelInfoPixel(image,&fill_color,q);
cristy3ed852e2009-09-05 21:47:34 +0000337 }
cristyed231572011-07-14 02:18:59 +0000338 p+=GetPixelChannels(floodplane_image);
339 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000340 }
cristyb0d3bb92010-09-22 14:37:58 +0000341 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000342 break;
343 }
cristyb0d3bb92010-09-22 14:37:58 +0000344 floodplane_view=DestroyCacheView(floodplane_view);
345 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +0000346 segment_stack=(SegmentInfo *) RelinquishMagickMemory(segment_stack);
347 floodplane_image=DestroyImage(floodplane_image);
cristybb503372010-05-27 20:51:26 +0000348 return(y == (ssize_t) image->rows ? MagickTrue : MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +0000349}
350
351/*
352%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
353% %
354% %
355% %
356+ G r a d i e n t I m a g e %
357% %
358% %
359% %
360%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
361%
cristycee97112010-05-28 00:44:52 +0000362% GradientImage() applies a continuously smooth color transitions along a
cristy3ed852e2009-09-05 21:47:34 +0000363% vector from one color to another.
364%
365% Note, the interface of this method will change in the future to support
366% more than one transistion.
367%
368% The format of the GradientImage method is:
369%
370% MagickBooleanType GradientImage(Image *image,const GradientType type,
cristy101ab702011-10-13 13:06:32 +0000371% const SpreadMethod method,const PixelInfo *start_color,
372% const PixelInfo *stop_color,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000373%
374% A description of each parameter follows:
375%
376% o image: the image.
377%
378% o type: the gradient type: linear or radial.
379%
380% o spread: the gradient spread meathod: pad, reflect, or repeat.
381%
382% o start_color: the start color.
383%
384% o stop_color: the stop color.
385%
cristy189e84c2011-08-27 18:08:53 +0000386% o exception: return any errors or warnings in this structure.
387%
cristy3ed852e2009-09-05 21:47:34 +0000388*/
cristy117ff172010-08-15 21:35:32 +0000389
390static inline double MagickMax(const double x,const double y)
391{
392 return(x > y ? x : y);
393}
394
cristy3ed852e2009-09-05 21:47:34 +0000395MagickExport MagickBooleanType GradientImage(Image *image,
396 const GradientType type,const SpreadMethod method,
cristy101ab702011-10-13 13:06:32 +0000397 const PixelInfo *start_color,const PixelInfo *stop_color,
cristy189e84c2011-08-27 18:08:53 +0000398 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000399{
400 DrawInfo
401 *draw_info;
402
403 GradientInfo
404 *gradient;
405
406 MagickBooleanType
407 status;
408
cristybb503372010-05-27 20:51:26 +0000409 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000410 i;
411
412 /*
413 Set gradient start-stop end points.
414 */
415 assert(image != (const Image *) NULL);
416 assert(image->signature == MagickSignature);
417 if (image->debug != MagickFalse)
418 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy101ab702011-10-13 13:06:32 +0000419 assert(start_color != (const PixelInfo *) NULL);
420 assert(stop_color != (const PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000421 draw_info=AcquireDrawInfo();
422 gradient=(&draw_info->gradient);
423 gradient->type=type;
424 gradient->bounding_box.width=image->columns;
425 gradient->bounding_box.height=image->rows;
426 gradient->gradient_vector.x2=(double) image->columns-1.0;
427 gradient->gradient_vector.y2=(double) image->rows-1.0;
428 if ((type == LinearGradient) && (gradient->gradient_vector.y2 != 0.0))
429 gradient->gradient_vector.x2=0.0;
430 gradient->center.x=(double) gradient->gradient_vector.x2/2.0;
431 gradient->center.y=(double) gradient->gradient_vector.y2/2.0;
432 gradient->radius=MagickMax(gradient->center.x,gradient->center.y);
433 gradient->spread=method;
434 /*
435 Define the gradient to fill between the stops.
436 */
437 gradient->number_stops=2;
438 gradient->stops=(StopInfo *) AcquireQuantumMemory(gradient->number_stops,
439 sizeof(*gradient->stops));
440 if (gradient->stops == (StopInfo *) NULL)
441 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
442 image->filename);
443 (void) ResetMagickMemory(gradient->stops,0,gradient->number_stops*
444 sizeof(*gradient->stops));
cristybb503372010-05-27 20:51:26 +0000445 for (i=0; i < (ssize_t) gradient->number_stops; i++)
cristy4c08aed2011-07-01 19:47:50 +0000446 GetPixelInfo(image,&gradient->stops[i].color);
cristy9d8c8ce2011-10-25 16:13:52 +0000447 gradient->stops[0].color=(*start_color);
cristy3ed852e2009-09-05 21:47:34 +0000448 gradient->stops[0].offset=0.0;
cristy9d8c8ce2011-10-25 16:13:52 +0000449 gradient->stops[1].color=(*stop_color);
cristy3ed852e2009-09-05 21:47:34 +0000450 gradient->stops[1].offset=1.0;
451 /*
452 Draw a gradient on the image.
453 */
cristy947cb4c2011-10-20 18:41:46 +0000454 status=DrawGradientImage(image,draw_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000455 draw_info=DestroyDrawInfo(draw_info);
cristy09250192012-02-07 18:53:31 +0000456 if ((start_color->matte == MagickFalse) && (stop_color->matte == MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +0000457 image->matte=MagickFalse;
cristy101ab702011-10-13 13:06:32 +0000458 if ((IsPixelInfoGray(start_color) != MagickFalse) &&
459 (IsPixelInfoGray(stop_color) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +0000460 image->type=GrayscaleType;
461 return(status);
462}
463
464/*
465%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
466% %
467% %
468% %
469% O i l P a i n t I m a g e %
470% %
471% %
472% %
473%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
474%
475% OilPaintImage() applies a special effect filter that simulates an oil
476% painting. Each pixel is replaced by the most frequent color occurring
477% in a circular region defined by radius.
478%
479% The format of the OilPaintImage method is:
480%
481% Image *OilPaintImage(const Image *image,const double radius,
cristy14973ba2011-08-27 23:48:07 +0000482% const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000483%
484% A description of each parameter follows:
485%
486% o image: the image.
487%
488% o radius: the radius of the circular neighborhood.
489%
cristy14973ba2011-08-27 23:48:07 +0000490% o sigma: the standard deviation of the Gaussian, in pixels.
491%
cristy3ed852e2009-09-05 21:47:34 +0000492% o exception: return any errors or warnings in this structure.
493%
494*/
495
cristybb503372010-05-27 20:51:26 +0000496static size_t **DestroyHistogramThreadSet(size_t **histogram)
cristy3ed852e2009-09-05 21:47:34 +0000497{
cristybb503372010-05-27 20:51:26 +0000498 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000499 i;
500
cristybb503372010-05-27 20:51:26 +0000501 assert(histogram != (size_t **) NULL);
502 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
503 if (histogram[i] != (size_t *) NULL)
504 histogram[i]=(size_t *) RelinquishMagickMemory(histogram[i]);
cristyb41ee102010-10-04 16:46:15 +0000505 histogram=(size_t **) RelinquishMagickMemory(histogram);
cristy3ed852e2009-09-05 21:47:34 +0000506 return(histogram);
507}
508
cristybb503372010-05-27 20:51:26 +0000509static size_t **AcquireHistogramThreadSet(const size_t count)
cristy3ed852e2009-09-05 21:47:34 +0000510{
cristybb503372010-05-27 20:51:26 +0000511 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000512 i;
513
cristybb503372010-05-27 20:51:26 +0000514 size_t
cristy3ed852e2009-09-05 21:47:34 +0000515 **histogram,
516 number_threads;
517
518 number_threads=GetOpenMPMaximumThreads();
cristy14973ba2011-08-27 23:48:07 +0000519 histogram=(size_t **) AcquireQuantumMemory(number_threads,sizeof(*histogram));
cristybb503372010-05-27 20:51:26 +0000520 if (histogram == (size_t **) NULL)
521 return((size_t **) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000522 (void) ResetMagickMemory(histogram,0,number_threads*sizeof(*histogram));
cristybb503372010-05-27 20:51:26 +0000523 for (i=0; i < (ssize_t) number_threads; i++)
cristy3ed852e2009-09-05 21:47:34 +0000524 {
cristy14973ba2011-08-27 23:48:07 +0000525 histogram[i]=(size_t *) AcquireQuantumMemory(count,sizeof(**histogram));
cristybb503372010-05-27 20:51:26 +0000526 if (histogram[i] == (size_t *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000527 return(DestroyHistogramThreadSet(histogram));
528 }
529 return(histogram);
530}
531
532MagickExport Image *OilPaintImage(const Image *image,const double radius,
cristy14973ba2011-08-27 23:48:07 +0000533 const double sigma,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000534{
535#define NumberPaintBins 256
536#define OilPaintImageTag "OilPaint/Image"
537
cristyfa112112010-01-04 17:48:07 +0000538 CacheView
539 *image_view,
540 *paint_view;
541
cristy3ed852e2009-09-05 21:47:34 +0000542 Image
543 *paint_image;
544
cristy3ed852e2009-09-05 21:47:34 +0000545 MagickBooleanType
546 status;
547
cristybb503372010-05-27 20:51:26 +0000548 MagickOffsetType
549 progress;
550
551 size_t
cristy14973ba2011-08-27 23:48:07 +0000552 **histograms,
cristy3ed852e2009-09-05 21:47:34 +0000553 width;
554
cristybb503372010-05-27 20:51:26 +0000555 ssize_t
cristyf8561542012-01-24 00:26:46 +0000556 center,
cristybb503372010-05-27 20:51:26 +0000557 y;
558
cristy3ed852e2009-09-05 21:47:34 +0000559 /*
560 Initialize painted image attributes.
561 */
562 assert(image != (const Image *) NULL);
563 assert(image->signature == MagickSignature);
564 if (image->debug != MagickFalse)
565 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
566 assert(exception != (ExceptionInfo *) NULL);
567 assert(exception->signature == MagickSignature);
cristy14973ba2011-08-27 23:48:07 +0000568 width=GetOptimalKernelWidth2D(radius,sigma);
cristy3ed852e2009-09-05 21:47:34 +0000569 paint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
570 if (paint_image == (Image *) NULL)
571 return((Image *) NULL);
cristy574cc262011-08-05 01:23:58 +0000572 if (SetImageStorageClass(paint_image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000573 {
cristy3ed852e2009-09-05 21:47:34 +0000574 paint_image=DestroyImage(paint_image);
575 return((Image *) NULL);
576 }
577 histograms=AcquireHistogramThreadSet(NumberPaintBins);
cristybb503372010-05-27 20:51:26 +0000578 if (histograms == (size_t **) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000579 {
580 paint_image=DestroyImage(paint_image);
581 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
582 }
583 /*
584 Oil paint image.
585 */
586 status=MagickTrue;
587 progress=0;
cristyd09f8802012-02-04 16:44:10 +0000588 center=(ssize_t) GetPixelChannels(image)*(image->columns+width)*(width/2L)+
589 GetPixelChannels(image)*(width/2L);
cristy3ed852e2009-09-05 21:47:34 +0000590 image_view=AcquireCacheView(image);
591 paint_view=AcquireCacheView(paint_image);
cristyb5d5f722009-11-04 03:03:49 +0000592#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000593 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000594#endif
cristybb503372010-05-27 20:51:26 +0000595 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000596 {
cristy4c08aed2011-07-01 19:47:50 +0000597 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000598 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000599
cristy4c08aed2011-07-01 19:47:50 +0000600 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000601 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000602
cristybb503372010-05-27 20:51:26 +0000603 register size_t
cristy3ed852e2009-09-05 21:47:34 +0000604 *histogram;
605
cristy14973ba2011-08-27 23:48:07 +0000606 register ssize_t
607 x;
608
cristy3ed852e2009-09-05 21:47:34 +0000609 if (status == MagickFalse)
610 continue;
cristyfe4ba002011-02-28 14:54:12 +0000611 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t)
612 (width/2L),image->columns+width,width,exception);
cristy3ed852e2009-09-05 21:47:34 +0000613 q=QueueCacheViewAuthenticPixels(paint_view,0,y,paint_image->columns,1,
614 exception);
cristy4c08aed2011-07-01 19:47:50 +0000615 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000616 {
617 status=MagickFalse;
618 continue;
619 }
cristy3ed852e2009-09-05 21:47:34 +0000620 histogram=histograms[GetOpenMPThreadId()];
cristybb503372010-05-27 20:51:26 +0000621 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000622 {
cristybb503372010-05-27 20:51:26 +0000623 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000624 i,
625 u;
626
cristybb503372010-05-27 20:51:26 +0000627 size_t
cristy3ed852e2009-09-05 21:47:34 +0000628 count;
629
cristy9d314ff2011-03-09 01:30:28 +0000630 ssize_t
631 j,
632 k,
cristyf8561542012-01-24 00:26:46 +0000633 n,
cristy9d314ff2011-03-09 01:30:28 +0000634 v;
635
cristy3ed852e2009-09-05 21:47:34 +0000636 /*
637 Assign most frequent color.
638 */
cristyf8561542012-01-24 00:26:46 +0000639 k=0;
cristy3ed852e2009-09-05 21:47:34 +0000640 j=0;
641 count=0;
cristyf8561542012-01-24 00:26:46 +0000642 (void) ResetMagickMemory(histogram,0,NumberPaintBins* sizeof(*histogram));
cristybb503372010-05-27 20:51:26 +0000643 for (v=0; v < (ssize_t) width; v++)
cristy3ed852e2009-09-05 21:47:34 +0000644 {
cristybb503372010-05-27 20:51:26 +0000645 for (u=0; u < (ssize_t) width; u++)
cristy3ed852e2009-09-05 21:47:34 +0000646 {
cristyf8561542012-01-24 00:26:46 +0000647 n=(ssize_t) ScaleQuantumToChar(GetPixelIntensity(image,p+
648 GetPixelChannels(image)*(u+k)));
649 histogram[n]++;
650 if (histogram[n] > count)
cristy3ed852e2009-09-05 21:47:34 +0000651 {
cristyf8561542012-01-24 00:26:46 +0000652 j=k+u;
653 count=histogram[n];
cristy3ed852e2009-09-05 21:47:34 +0000654 }
655 }
cristyf8561542012-01-24 00:26:46 +0000656 k+=(ssize_t) (image->columns+width);
cristy3ed852e2009-09-05 21:47:34 +0000657 }
cristyf8561542012-01-24 00:26:46 +0000658 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
659 {
660 PixelChannel
661 channel;
662
663 PixelTrait
664 paint_traits,
665 traits;
666
667 channel=GetPixelChannelMapChannel(image,i);
668 traits=GetPixelChannelMapTraits(image,channel);
669 paint_traits=GetPixelChannelMapTraits(paint_image,channel);
670 if ((traits == UndefinedPixelTrait) ||
671 (paint_traits == UndefinedPixelTrait))
672 continue;
cristyd09f8802012-02-04 16:44:10 +0000673 if (((paint_traits & CopyPixelTrait) != 0) ||
674 (GetPixelMask(image,p) != 0))
cristyf8561542012-01-24 00:26:46 +0000675 {
676 SetPixelChannel(paint_image,channel,p[center+i],q);
677 continue;
678 }
679 SetPixelChannel(paint_image,channel,p[j*GetPixelChannels(image)+i],q);
680 }
cristyed231572011-07-14 02:18:59 +0000681 p+=GetPixelChannels(image);
682 q+=GetPixelChannels(paint_image);
cristy3ed852e2009-09-05 21:47:34 +0000683 }
684 if (SyncCacheViewAuthenticPixels(paint_view,exception) == MagickFalse)
685 status=MagickFalse;
686 if (image->progress_monitor != (MagickProgressMonitor) NULL)
687 {
688 MagickBooleanType
689 proceed;
690
cristyb5d5f722009-11-04 03:03:49 +0000691#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy95338b32012-01-22 03:34:36 +0000692 #pragma omp critical (MagickCore_OilPaintImage)
cristy3ed852e2009-09-05 21:47:34 +0000693#endif
694 proceed=SetImageProgress(image,OilPaintImageTag,progress++,image->rows);
695 if (proceed == MagickFalse)
696 status=MagickFalse;
697 }
698 }
699 paint_view=DestroyCacheView(paint_view);
700 image_view=DestroyCacheView(image_view);
701 histograms=DestroyHistogramThreadSet(histograms);
702 if (status == MagickFalse)
703 paint_image=DestroyImage(paint_image);
704 return(paint_image);
705}
706
707/*
708%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
709% %
710% %
711% %
712% O p a q u e P a i n t I m a g e %
713% %
714% %
715% %
716%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
717%
718% OpaquePaintImage() changes any pixel that matches color with the color
719% defined by fill.
720%
cristy14973ba2011-08-27 23:48:07 +0000721% By default color must match a particular pixel color exactly. However, in
722% many cases two colors may differ by a small amount. Fuzz defines how much
723% tolerance is acceptable to consider two colors as the same. For example,
724% set fuzz to 10 and the color red at intensities of 100 and 102 respectively
725% are now interpreted as the same color.
cristy3ed852e2009-09-05 21:47:34 +0000726%
727% The format of the OpaquePaintImage method is:
728%
729% MagickBooleanType OpaquePaintImage(Image *image,
cristy101ab702011-10-13 13:06:32 +0000730% const PixelInfo *target,const PixelInfo *fill,
cristy189e84c2011-08-27 18:08:53 +0000731% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000732%
733% A description of each parameter follows:
734%
735% o image: the image.
736%
cristy3ed852e2009-09-05 21:47:34 +0000737% o target: the RGB value of the target color.
738%
739% o fill: the replacement color.
740%
741% o invert: paint any pixel that does not match the target color.
742%
cristy189e84c2011-08-27 18:08:53 +0000743% o exception: return any errors or warnings in this structure.
744%
cristy3ed852e2009-09-05 21:47:34 +0000745*/
cristy3ed852e2009-09-05 21:47:34 +0000746MagickExport MagickBooleanType OpaquePaintImage(Image *image,
cristy189e84c2011-08-27 18:08:53 +0000747 const PixelInfo *target,const PixelInfo *fill,const MagickBooleanType invert,
748 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000749{
750#define OpaquePaintImageTag "Opaque/Image"
751
cristyc4c8d132010-01-07 01:58:38 +0000752 CacheView
753 *image_view;
754
cristy3ed852e2009-09-05 21:47:34 +0000755 MagickBooleanType
756 status;
757
cristybb503372010-05-27 20:51:26 +0000758 MagickOffsetType
759 progress;
760
cristy4c08aed2011-07-01 19:47:50 +0000761 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000762 zero;
763
cristybb503372010-05-27 20:51:26 +0000764 ssize_t
765 y;
766
cristy3ed852e2009-09-05 21:47:34 +0000767 assert(image != (Image *) NULL);
768 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +0000769 assert(target != (PixelInfo *) NULL);
770 assert(fill != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000771 if (image->debug != MagickFalse)
772 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy574cc262011-08-05 01:23:58 +0000773 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000774 return(MagickFalse);
cristy768165d2012-04-09 15:01:35 +0000775 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
776 (IsPixelInfoGray(fill) != MagickFalse))
777 (void) TransformImageColorspace(image,sRGBColorspace,exception);
cristy09250192012-02-07 18:53:31 +0000778 if ((fill->matte != MagickFalse) && (image->matte == MagickFalse))
779 (void) SetImageAlpha(image,OpaqueAlpha,exception);
cristy3ed852e2009-09-05 21:47:34 +0000780 /*
781 Make image color opaque.
782 */
783 status=MagickTrue;
784 progress=0;
cristy4c08aed2011-07-01 19:47:50 +0000785 GetPixelInfo(image,&zero);
cristy3ed852e2009-09-05 21:47:34 +0000786 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +0000787#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000788 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000789#endif
cristybb503372010-05-27 20:51:26 +0000790 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000791 {
cristy4c08aed2011-07-01 19:47:50 +0000792 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000793 pixel;
794
cristy4c08aed2011-07-01 19:47:50 +0000795 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000796 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000797
cristy14973ba2011-08-27 23:48:07 +0000798 register ssize_t
799 x;
800
cristy3ed852e2009-09-05 21:47:34 +0000801 if (status == MagickFalse)
802 continue;
803 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy14973ba2011-08-27 23:48:07 +0000804 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000805 {
806 status=MagickFalse;
807 continue;
808 }
cristy3ed852e2009-09-05 21:47:34 +0000809 pixel=zero;
cristybb503372010-05-27 20:51:26 +0000810 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000811 {
cristy803640d2011-11-17 02:11:32 +0000812 GetPixelInfoPixel(image,q,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000813 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
cristy95f562a2012-01-01 20:49:11 +0000814 SetPixelInfoPixel(image,fill,q);
cristyed231572011-07-14 02:18:59 +0000815 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000816 }
817 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
818 status=MagickFalse;
819 if (image->progress_monitor != (MagickProgressMonitor) NULL)
820 {
821 MagickBooleanType
822 proceed;
823
cristyb5d5f722009-11-04 03:03:49 +0000824#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy95338b32012-01-22 03:34:36 +0000825 #pragma omp critical (MagickCore_OpaquePaintImage)
cristy3ed852e2009-09-05 21:47:34 +0000826#endif
827 proceed=SetImageProgress(image,OpaquePaintImageTag,progress++,
828 image->rows);
829 if (proceed == MagickFalse)
830 status=MagickFalse;
831 }
832 }
833 image_view=DestroyCacheView(image_view);
834 return(status);
835}
836
837/*
838%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
839% %
840% %
841% %
842% T r a n s p a r e n t P a i n t I m a g e %
843% %
844% %
845% %
846%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
847%
848% TransparentPaintImage() changes the opacity value associated with any pixel
849% that matches color to the value defined by opacity.
850%
cristy14973ba2011-08-27 23:48:07 +0000851% By default color must match a particular pixel color exactly. However, in
852% many cases two colors may differ by a small amount. Fuzz defines how much
853% tolerance is acceptable to consider two colors as the same. For example,
854% set fuzz to 10 and the color red at intensities of 100 and 102 respectively
855% are now interpreted as the same color.
cristy3ed852e2009-09-05 21:47:34 +0000856%
857% The format of the TransparentPaintImage method is:
858%
859% MagickBooleanType TransparentPaintImage(Image *image,
cristy4c08aed2011-07-01 19:47:50 +0000860% const PixelInfo *target,const Quantum opacity,
cristy189e84c2011-08-27 18:08:53 +0000861% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000862%
863% A description of each parameter follows:
864%
865% o image: the image.
866%
867% o target: the target color.
868%
869% o opacity: the replacement opacity value.
870%
871% o invert: paint any pixel that does not match the target color.
872%
cristy189e84c2011-08-27 18:08:53 +0000873% o exception: return any errors or warnings in this structure.
874%
cristy3ed852e2009-09-05 21:47:34 +0000875*/
876MagickExport MagickBooleanType TransparentPaintImage(Image *image,
cristy14973ba2011-08-27 23:48:07 +0000877 const PixelInfo *target,const Quantum opacity,const MagickBooleanType invert,
878 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000879{
880#define TransparentPaintImageTag "Transparent/Image"
881
cristyc4c8d132010-01-07 01:58:38 +0000882 CacheView
883 *image_view;
884
cristy3ed852e2009-09-05 21:47:34 +0000885 MagickBooleanType
886 status;
887
cristybb503372010-05-27 20:51:26 +0000888 MagickOffsetType
889 progress;
890
cristy4c08aed2011-07-01 19:47:50 +0000891 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000892 zero;
893
cristybb503372010-05-27 20:51:26 +0000894 ssize_t
895 y;
896
cristy3ed852e2009-09-05 21:47:34 +0000897 assert(image != (Image *) NULL);
898 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +0000899 assert(target != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000900 if (image->debug != MagickFalse)
901 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy574cc262011-08-05 01:23:58 +0000902 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000903 return(MagickFalse);
904 if (image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +0000905 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +0000906 /*
907 Make image color transparent.
908 */
909 status=MagickTrue;
910 progress=0;
cristy4c08aed2011-07-01 19:47:50 +0000911 GetPixelInfo(image,&zero);
cristy3ed852e2009-09-05 21:47:34 +0000912 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +0000913#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000914 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000915#endif
cristybb503372010-05-27 20:51:26 +0000916 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000917 {
cristy4c08aed2011-07-01 19:47:50 +0000918 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000919 pixel;
920
cristybb503372010-05-27 20:51:26 +0000921 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000922 x;
923
cristy4c08aed2011-07-01 19:47:50 +0000924 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000925 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000926
927 if (status == MagickFalse)
928 continue;
929 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy14973ba2011-08-27 23:48:07 +0000930 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000931 {
932 status=MagickFalse;
933 continue;
934 }
cristy3ed852e2009-09-05 21:47:34 +0000935 pixel=zero;
cristybb503372010-05-27 20:51:26 +0000936 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000937 {
cristy803640d2011-11-17 02:11:32 +0000938 GetPixelInfoPixel(image,q,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000939 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
940 SetPixelAlpha(image,opacity,q);
cristyed231572011-07-14 02:18:59 +0000941 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000942 }
943 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
944 status=MagickFalse;
945 if (image->progress_monitor != (MagickProgressMonitor) NULL)
946 {
947 MagickBooleanType
948 proceed;
949
cristyb5d5f722009-11-04 03:03:49 +0000950#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy95338b32012-01-22 03:34:36 +0000951 #pragma omp critical (MagickCore_TransparentPaintImage)
cristy3ed852e2009-09-05 21:47:34 +0000952#endif
953 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
954 image->rows);
955 if (proceed == MagickFalse)
956 status=MagickFalse;
957 }
958 }
959 image_view=DestroyCacheView(image_view);
960 return(status);
961}
962
963/*
964%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
965% %
966% %
967% %
968% 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 %
969% %
970% %
971% %
972%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
973%
974% TransparentPaintImageChroma() changes the opacity value associated with any
975% pixel that matches color to the value defined by opacity.
976%
cristy14973ba2011-08-27 23:48:07 +0000977% As there is one fuzz value for the all the channels, TransparentPaintImage()
978% is not suitable for the operations like chroma, where the tolerance for
979% similarity of two color component (RGB) can be different. Thus we define
980% this method to take two target pixels (one low and one high) and all the
981% pixels of an image which are lying between these two pixels are made
982% transparent.
cristy3ed852e2009-09-05 21:47:34 +0000983%
cristy14973ba2011-08-27 23:48:07 +0000984% The format of the TransparentPaintImageChroma method is:
cristy3ed852e2009-09-05 21:47:34 +0000985%
cristy14973ba2011-08-27 23:48:07 +0000986% MagickBooleanType TransparentPaintImageChroma(Image *image,
987% const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
988% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000989%
990% A description of each parameter follows:
991%
992% o image: the image.
993%
994% o low: the low target color.
995%
996% o high: the high target color.
997%
998% o opacity: the replacement opacity value.
999%
1000% o invert: paint any pixel that does not match the target color.
1001%
cristy189e84c2011-08-27 18:08:53 +00001002% o exception: return any errors or warnings in this structure.
1003%
cristy3ed852e2009-09-05 21:47:34 +00001004*/
1005MagickExport MagickBooleanType TransparentPaintImageChroma(Image *image,
cristy189e84c2011-08-27 18:08:53 +00001006 const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
1007 const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001008{
1009#define TransparentPaintImageTag "Transparent/Image"
1010
cristyc4c8d132010-01-07 01:58:38 +00001011 CacheView
1012 *image_view;
1013
cristy3ed852e2009-09-05 21:47:34 +00001014 MagickBooleanType
1015 status;
1016
cristybb503372010-05-27 20:51:26 +00001017 MagickOffsetType
1018 progress;
1019
1020 ssize_t
1021 y;
1022
cristy3ed852e2009-09-05 21:47:34 +00001023 assert(image != (Image *) NULL);
1024 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +00001025 assert(high != (PixelInfo *) NULL);
1026 assert(low != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001027 if (image->debug != MagickFalse)
1028 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy574cc262011-08-05 01:23:58 +00001029 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001030 return(MagickFalse);
1031 if (image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00001032 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00001033 /*
1034 Make image color transparent.
1035 */
1036 status=MagickTrue;
1037 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00001038 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00001039#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00001040 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00001041#endif
cristybb503372010-05-27 20:51:26 +00001042 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001043 {
1044 MagickBooleanType
1045 match;
1046
cristy4c08aed2011-07-01 19:47:50 +00001047 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001048 pixel;
1049
cristy4c08aed2011-07-01 19:47:50 +00001050 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001051 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001052
cristy14973ba2011-08-27 23:48:07 +00001053 register ssize_t
1054 x;
1055
cristy3ed852e2009-09-05 21:47:34 +00001056 if (status == MagickFalse)
1057 continue;
1058 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy14973ba2011-08-27 23:48:07 +00001059 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001060 {
1061 status=MagickFalse;
1062 continue;
1063 }
cristy4c08aed2011-07-01 19:47:50 +00001064 GetPixelInfo(image,&pixel);
cristybb503372010-05-27 20:51:26 +00001065 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001066 {
cristy803640d2011-11-17 02:11:32 +00001067 GetPixelInfoPixel(image,q,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001068 match=((pixel.red >= low->red) && (pixel.red <= high->red) &&
1069 (pixel.green >= low->green) && (pixel.green <= high->green) &&
cristy14973ba2011-08-27 23:48:07 +00001070 (pixel.blue >= low->blue) && (pixel.blue <= high->blue)) ? MagickTrue :
1071 MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00001072 if (match != invert)
cristy4c08aed2011-07-01 19:47:50 +00001073 SetPixelAlpha(image,opacity,q);
cristyed231572011-07-14 02:18:59 +00001074 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001075 }
1076 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1077 status=MagickFalse;
1078 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1079 {
1080 MagickBooleanType
1081 proceed;
1082
cristyb5d5f722009-11-04 03:03:49 +00001083#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy95338b32012-01-22 03:34:36 +00001084 #pragma omp critical (MagickCore_TransparentPaintImageChroma)
cristy3ed852e2009-09-05 21:47:34 +00001085#endif
1086 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
1087 image->rows);
1088 if (proceed == MagickFalse)
1089 status=MagickFalse;
1090 }
1091 }
1092 image_view=DestroyCacheView(image_view);
1093 return(status);
1094}