blob: ac33f9277e575db3da537fd30683f8ec8302589c [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);
cristy3ee7de52012-04-14 23:40:47 +0000176 if ((image->matte == MagickFalse) && (draw_info->fill.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 }
cristy177e41c2012-04-15 15:08:25 +0000658 if (GetPixelMask(image,p) != 0)
659 {
660 p+=GetPixelChannels(image);
661 q+=GetPixelChannels(paint_image);
662 continue;
663 }
cristyf8561542012-01-24 00:26:46 +0000664 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
665 {
666 PixelChannel
667 channel;
668
669 PixelTrait
670 paint_traits,
671 traits;
672
673 channel=GetPixelChannelMapChannel(image,i);
674 traits=GetPixelChannelMapTraits(image,channel);
675 paint_traits=GetPixelChannelMapTraits(paint_image,channel);
676 if ((traits == UndefinedPixelTrait) ||
677 (paint_traits == UndefinedPixelTrait))
678 continue;
cristy177e41c2012-04-15 15:08:25 +0000679 if ((paint_traits & CopyPixelTrait) != 0)
cristyf8561542012-01-24 00:26:46 +0000680 {
681 SetPixelChannel(paint_image,channel,p[center+i],q);
682 continue;
683 }
684 SetPixelChannel(paint_image,channel,p[j*GetPixelChannels(image)+i],q);
685 }
cristyed231572011-07-14 02:18:59 +0000686 p+=GetPixelChannels(image);
687 q+=GetPixelChannels(paint_image);
cristy3ed852e2009-09-05 21:47:34 +0000688 }
689 if (SyncCacheViewAuthenticPixels(paint_view,exception) == MagickFalse)
690 status=MagickFalse;
691 if (image->progress_monitor != (MagickProgressMonitor) NULL)
692 {
693 MagickBooleanType
694 proceed;
695
cristyb5d5f722009-11-04 03:03:49 +0000696#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy95338b32012-01-22 03:34:36 +0000697 #pragma omp critical (MagickCore_OilPaintImage)
cristy3ed852e2009-09-05 21:47:34 +0000698#endif
699 proceed=SetImageProgress(image,OilPaintImageTag,progress++,image->rows);
700 if (proceed == MagickFalse)
701 status=MagickFalse;
702 }
703 }
704 paint_view=DestroyCacheView(paint_view);
705 image_view=DestroyCacheView(image_view);
706 histograms=DestroyHistogramThreadSet(histograms);
707 if (status == MagickFalse)
708 paint_image=DestroyImage(paint_image);
709 return(paint_image);
710}
711
712/*
713%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
714% %
715% %
716% %
717% O p a q u e P a i n t I m a g e %
718% %
719% %
720% %
721%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
722%
723% OpaquePaintImage() changes any pixel that matches color with the color
724% defined by fill.
725%
cristy14973ba2011-08-27 23:48:07 +0000726% By default color must match a particular pixel color exactly. However, in
727% many cases two colors may differ by a small amount. Fuzz defines how much
728% tolerance is acceptable to consider two colors as the same. For example,
729% set fuzz to 10 and the color red at intensities of 100 and 102 respectively
730% are now interpreted as the same color.
cristy3ed852e2009-09-05 21:47:34 +0000731%
732% The format of the OpaquePaintImage method is:
733%
734% MagickBooleanType OpaquePaintImage(Image *image,
cristy101ab702011-10-13 13:06:32 +0000735% const PixelInfo *target,const PixelInfo *fill,
cristy189e84c2011-08-27 18:08:53 +0000736% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000737%
738% A description of each parameter follows:
739%
740% o image: the image.
741%
cristy3ed852e2009-09-05 21:47:34 +0000742% o target: the RGB value of the target color.
743%
744% o fill: the replacement color.
745%
746% o invert: paint any pixel that does not match the target color.
747%
cristy189e84c2011-08-27 18:08:53 +0000748% o exception: return any errors or warnings in this structure.
749%
cristy3ed852e2009-09-05 21:47:34 +0000750*/
cristy3ed852e2009-09-05 21:47:34 +0000751MagickExport MagickBooleanType OpaquePaintImage(Image *image,
cristy189e84c2011-08-27 18:08:53 +0000752 const PixelInfo *target,const PixelInfo *fill,const MagickBooleanType invert,
753 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000754{
755#define OpaquePaintImageTag "Opaque/Image"
756
cristyc4c8d132010-01-07 01:58:38 +0000757 CacheView
758 *image_view;
759
cristy3ed852e2009-09-05 21:47:34 +0000760 MagickBooleanType
761 status;
762
cristybb503372010-05-27 20:51:26 +0000763 MagickOffsetType
764 progress;
765
cristy4c08aed2011-07-01 19:47:50 +0000766 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000767 zero;
768
cristybb503372010-05-27 20:51:26 +0000769 ssize_t
770 y;
771
cristy3ed852e2009-09-05 21:47:34 +0000772 assert(image != (Image *) NULL);
773 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +0000774 assert(target != (PixelInfo *) NULL);
775 assert(fill != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000776 if (image->debug != MagickFalse)
777 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy574cc262011-08-05 01:23:58 +0000778 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000779 return(MagickFalse);
cristy768165d2012-04-09 15:01:35 +0000780 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
781 (IsPixelInfoGray(fill) != MagickFalse))
782 (void) TransformImageColorspace(image,sRGBColorspace,exception);
cristy09250192012-02-07 18:53:31 +0000783 if ((fill->matte != MagickFalse) && (image->matte == MagickFalse))
784 (void) SetImageAlpha(image,OpaqueAlpha,exception);
cristy3ed852e2009-09-05 21:47:34 +0000785 /*
786 Make image color opaque.
787 */
788 status=MagickTrue;
789 progress=0;
cristy4c08aed2011-07-01 19:47:50 +0000790 GetPixelInfo(image,&zero);
cristy3ed852e2009-09-05 21:47:34 +0000791 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +0000792#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000793 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000794#endif
cristybb503372010-05-27 20:51:26 +0000795 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000796 {
cristy4c08aed2011-07-01 19:47:50 +0000797 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000798 pixel;
799
cristy4c08aed2011-07-01 19:47:50 +0000800 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000801 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000802
cristy14973ba2011-08-27 23:48:07 +0000803 register ssize_t
804 x;
805
cristy3ed852e2009-09-05 21:47:34 +0000806 if (status == MagickFalse)
807 continue;
808 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy14973ba2011-08-27 23:48:07 +0000809 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000810 {
811 status=MagickFalse;
812 continue;
813 }
cristy3ed852e2009-09-05 21:47:34 +0000814 pixel=zero;
cristybb503372010-05-27 20:51:26 +0000815 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000816 {
cristy803640d2011-11-17 02:11:32 +0000817 GetPixelInfoPixel(image,q,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000818 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
cristy95f562a2012-01-01 20:49:11 +0000819 SetPixelInfoPixel(image,fill,q);
cristyed231572011-07-14 02:18:59 +0000820 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000821 }
822 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
823 status=MagickFalse;
824 if (image->progress_monitor != (MagickProgressMonitor) NULL)
825 {
826 MagickBooleanType
827 proceed;
828
cristyb5d5f722009-11-04 03:03:49 +0000829#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy95338b32012-01-22 03:34:36 +0000830 #pragma omp critical (MagickCore_OpaquePaintImage)
cristy3ed852e2009-09-05 21:47:34 +0000831#endif
832 proceed=SetImageProgress(image,OpaquePaintImageTag,progress++,
833 image->rows);
834 if (proceed == MagickFalse)
835 status=MagickFalse;
836 }
837 }
838 image_view=DestroyCacheView(image_view);
839 return(status);
840}
841
842/*
843%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
844% %
845% %
846% %
847% T r a n s p a r e n t P a i n t I m a g e %
848% %
849% %
850% %
851%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
852%
853% TransparentPaintImage() changes the opacity value associated with any pixel
854% that matches color to the value defined by opacity.
855%
cristy14973ba2011-08-27 23:48:07 +0000856% By default color must match a particular pixel color exactly. However, in
857% many cases two colors may differ by a small amount. Fuzz defines how much
858% tolerance is acceptable to consider two colors as the same. For example,
859% set fuzz to 10 and the color red at intensities of 100 and 102 respectively
860% are now interpreted as the same color.
cristy3ed852e2009-09-05 21:47:34 +0000861%
862% The format of the TransparentPaintImage method is:
863%
864% MagickBooleanType TransparentPaintImage(Image *image,
cristy4c08aed2011-07-01 19:47:50 +0000865% const PixelInfo *target,const Quantum opacity,
cristy189e84c2011-08-27 18:08:53 +0000866% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000867%
868% A description of each parameter follows:
869%
870% o image: the image.
871%
872% o target: the target color.
873%
874% o opacity: the replacement opacity value.
875%
876% o invert: paint any pixel that does not match the target color.
877%
cristy189e84c2011-08-27 18:08:53 +0000878% o exception: return any errors or warnings in this structure.
879%
cristy3ed852e2009-09-05 21:47:34 +0000880*/
881MagickExport MagickBooleanType TransparentPaintImage(Image *image,
cristy14973ba2011-08-27 23:48:07 +0000882 const PixelInfo *target,const Quantum opacity,const MagickBooleanType invert,
883 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000884{
885#define TransparentPaintImageTag "Transparent/Image"
886
cristyc4c8d132010-01-07 01:58:38 +0000887 CacheView
888 *image_view;
889
cristy3ed852e2009-09-05 21:47:34 +0000890 MagickBooleanType
891 status;
892
cristybb503372010-05-27 20:51:26 +0000893 MagickOffsetType
894 progress;
895
cristy4c08aed2011-07-01 19:47:50 +0000896 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000897 zero;
898
cristybb503372010-05-27 20:51:26 +0000899 ssize_t
900 y;
901
cristy3ed852e2009-09-05 21:47:34 +0000902 assert(image != (Image *) NULL);
903 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +0000904 assert(target != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000905 if (image->debug != MagickFalse)
906 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy574cc262011-08-05 01:23:58 +0000907 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000908 return(MagickFalse);
909 if (image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +0000910 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +0000911 /*
912 Make image color transparent.
913 */
914 status=MagickTrue;
915 progress=0;
cristy4c08aed2011-07-01 19:47:50 +0000916 GetPixelInfo(image,&zero);
cristy3ed852e2009-09-05 21:47:34 +0000917 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +0000918#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000919 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +0000920#endif
cristybb503372010-05-27 20:51:26 +0000921 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000922 {
cristy4c08aed2011-07-01 19:47:50 +0000923 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000924 pixel;
925
cristybb503372010-05-27 20:51:26 +0000926 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000927 x;
928
cristy4c08aed2011-07-01 19:47:50 +0000929 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000930 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000931
932 if (status == MagickFalse)
933 continue;
934 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy14973ba2011-08-27 23:48:07 +0000935 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000936 {
937 status=MagickFalse;
938 continue;
939 }
cristy3ed852e2009-09-05 21:47:34 +0000940 pixel=zero;
cristybb503372010-05-27 20:51:26 +0000941 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000942 {
cristy803640d2011-11-17 02:11:32 +0000943 GetPixelInfoPixel(image,q,&pixel);
cristy4c08aed2011-07-01 19:47:50 +0000944 if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert)
945 SetPixelAlpha(image,opacity,q);
cristyed231572011-07-14 02:18:59 +0000946 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000947 }
948 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
949 status=MagickFalse;
950 if (image->progress_monitor != (MagickProgressMonitor) NULL)
951 {
952 MagickBooleanType
953 proceed;
954
cristyb5d5f722009-11-04 03:03:49 +0000955#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy95338b32012-01-22 03:34:36 +0000956 #pragma omp critical (MagickCore_TransparentPaintImage)
cristy3ed852e2009-09-05 21:47:34 +0000957#endif
958 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
959 image->rows);
960 if (proceed == MagickFalse)
961 status=MagickFalse;
962 }
963 }
964 image_view=DestroyCacheView(image_view);
965 return(status);
966}
967
968/*
969%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
970% %
971% %
972% %
973% T r a n s p a r e n t P a i n t I m a g e C h r o m a %
974% %
975% %
976% %
977%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
978%
979% TransparentPaintImageChroma() changes the opacity value associated with any
980% pixel that matches color to the value defined by opacity.
981%
cristy14973ba2011-08-27 23:48:07 +0000982% As there is one fuzz value for the all the channels, TransparentPaintImage()
983% is not suitable for the operations like chroma, where the tolerance for
984% similarity of two color component (RGB) can be different. Thus we define
985% this method to take two target pixels (one low and one high) and all the
986% pixels of an image which are lying between these two pixels are made
987% transparent.
cristy3ed852e2009-09-05 21:47:34 +0000988%
cristy14973ba2011-08-27 23:48:07 +0000989% The format of the TransparentPaintImageChroma method is:
cristy3ed852e2009-09-05 21:47:34 +0000990%
cristy14973ba2011-08-27 23:48:07 +0000991% MagickBooleanType TransparentPaintImageChroma(Image *image,
992% const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
993% const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000994%
995% A description of each parameter follows:
996%
997% o image: the image.
998%
999% o low: the low target color.
1000%
1001% o high: the high target color.
1002%
1003% o opacity: the replacement opacity value.
1004%
1005% o invert: paint any pixel that does not match the target color.
1006%
cristy189e84c2011-08-27 18:08:53 +00001007% o exception: return any errors or warnings in this structure.
1008%
cristy3ed852e2009-09-05 21:47:34 +00001009*/
1010MagickExport MagickBooleanType TransparentPaintImageChroma(Image *image,
cristy189e84c2011-08-27 18:08:53 +00001011 const PixelInfo *low,const PixelInfo *high,const Quantum opacity,
1012 const MagickBooleanType invert,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001013{
1014#define TransparentPaintImageTag "Transparent/Image"
1015
cristyc4c8d132010-01-07 01:58:38 +00001016 CacheView
1017 *image_view;
1018
cristy3ed852e2009-09-05 21:47:34 +00001019 MagickBooleanType
1020 status;
1021
cristybb503372010-05-27 20:51:26 +00001022 MagickOffsetType
1023 progress;
1024
1025 ssize_t
1026 y;
1027
cristy3ed852e2009-09-05 21:47:34 +00001028 assert(image != (Image *) NULL);
1029 assert(image->signature == MagickSignature);
cristy4c08aed2011-07-01 19:47:50 +00001030 assert(high != (PixelInfo *) NULL);
1031 assert(low != (PixelInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001032 if (image->debug != MagickFalse)
1033 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy574cc262011-08-05 01:23:58 +00001034 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001035 return(MagickFalse);
1036 if (image->matte == MagickFalse)
cristy63240882011-08-05 19:05:27 +00001037 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00001038 /*
1039 Make image color transparent.
1040 */
1041 status=MagickTrue;
1042 progress=0;
cristy3ed852e2009-09-05 21:47:34 +00001043 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00001044#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00001045 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristy3ed852e2009-09-05 21:47:34 +00001046#endif
cristybb503372010-05-27 20:51:26 +00001047 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001048 {
1049 MagickBooleanType
1050 match;
1051
cristy4c08aed2011-07-01 19:47:50 +00001052 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001053 pixel;
1054
cristy4c08aed2011-07-01 19:47:50 +00001055 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001056 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001057
cristy14973ba2011-08-27 23:48:07 +00001058 register ssize_t
1059 x;
1060
cristy3ed852e2009-09-05 21:47:34 +00001061 if (status == MagickFalse)
1062 continue;
1063 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristy14973ba2011-08-27 23:48:07 +00001064 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001065 {
1066 status=MagickFalse;
1067 continue;
1068 }
cristy4c08aed2011-07-01 19:47:50 +00001069 GetPixelInfo(image,&pixel);
cristybb503372010-05-27 20:51:26 +00001070 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001071 {
cristy803640d2011-11-17 02:11:32 +00001072 GetPixelInfoPixel(image,q,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001073 match=((pixel.red >= low->red) && (pixel.red <= high->red) &&
1074 (pixel.green >= low->green) && (pixel.green <= high->green) &&
cristy14973ba2011-08-27 23:48:07 +00001075 (pixel.blue >= low->blue) && (pixel.blue <= high->blue)) ? MagickTrue :
1076 MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00001077 if (match != invert)
cristy4c08aed2011-07-01 19:47:50 +00001078 SetPixelAlpha(image,opacity,q);
cristyed231572011-07-14 02:18:59 +00001079 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001080 }
1081 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1082 status=MagickFalse;
1083 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1084 {
1085 MagickBooleanType
1086 proceed;
1087
cristyb5d5f722009-11-04 03:03:49 +00001088#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy95338b32012-01-22 03:34:36 +00001089 #pragma omp critical (MagickCore_TransparentPaintImageChroma)
cristy3ed852e2009-09-05 21:47:34 +00001090#endif
1091 proceed=SetImageProgress(image,TransparentPaintImageTag,progress++,
1092 image->rows);
1093 if (proceed == MagickFalse)
1094 status=MagickFalse;
1095 }
1096 }
1097 image_view=DestroyCacheView(image_view);
1098 return(status);
1099}