cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
| 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 | % % |
cristy | 7e41fe8 | 2010-12-04 23:12:08 +0000 | [diff] [blame] | 20 | % Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 21 | % 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 | */ |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 42 | #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" |
cristy | 8ea8122 | 2011-09-04 10:33:32 +0000 | [diff] [blame] | 53 | #include "MagickCore/gem-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 54 | #include "MagickCore/monitor.h" |
| 55 | #include "MagickCore/monitor-private.h" |
| 56 | #include "MagickCore/paint.h" |
| 57 | #include "MagickCore/pixel-accessor.h" |
| 58 | #include "MagickCore/string_.h" |
| 59 | #include "MagickCore/thread-private.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 60 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 61 | /* |
| 62 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 63 | % % |
| 64 | % % |
| 65 | % % |
| 66 | % F l o o d f i l l P a i n t I m a g e % |
| 67 | % % |
| 68 | % % |
| 69 | % % |
| 70 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 71 | % |
| 72 | % FloodfillPaintImage() changes the color value of any pixel that matches |
| 73 | % target and is an immediate neighbor. If the method FillToBorderMethod is |
| 74 | % specified, the color value is changed for any neighbor pixel that does not |
| 75 | % match the bordercolor member of image. |
| 76 | % |
cristy | 908a000 | 2011-08-28 00:13:39 +0000 | [diff] [blame] | 77 | % By default target must match a particular pixel color exactly. However, |
| 78 | % in many cases two colors may differ by a small amount. The fuzz member of |
| 79 | % image defines how much tolerance is acceptable to consider two colors as |
| 80 | % the same. For example, set fuzz to 10 and the color red at intensities of |
| 81 | % 100 and 102 respectively are now interpreted as the same color for the |
| 82 | % purposes of the floodfill. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 83 | % |
| 84 | % The format of the FloodfillPaintImage method is: |
| 85 | % |
| 86 | % MagickBooleanType FloodfillPaintImage(Image *image, |
cristy | d42d995 | 2011-07-08 14:21:50 +0000 | [diff] [blame] | 87 | % const DrawInfo *draw_info,const PixelInfo target, |
| 88 | % const ssize_t x_offset,const ssize_t y_offset, |
cristy | 189e84c | 2011-08-27 18:08:53 +0000 | [diff] [blame] | 89 | % const MagickBooleanType invert,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 90 | % |
| 91 | % A description of each parameter follows: |
| 92 | % |
| 93 | % o image: the image. |
| 94 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 95 | % o draw_info: the draw info. |
| 96 | % |
| 97 | % o target: the RGB value of the target color. |
| 98 | % |
| 99 | % o x_offset,y_offset: the starting location of the operation. |
| 100 | % |
| 101 | % o invert: paint any pixel that does not match the target color. |
| 102 | % |
cristy | 189e84c | 2011-08-27 18:08:53 +0000 | [diff] [blame] | 103 | % o exception: return any errors or warnings in this structure. |
| 104 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 105 | */ |
| 106 | MagickExport MagickBooleanType FloodfillPaintImage(Image *image, |
cristy | d42d995 | 2011-07-08 14:21:50 +0000 | [diff] [blame] | 107 | const DrawInfo *draw_info,const PixelInfo *target,const ssize_t x_offset, |
cristy | 189e84c | 2011-08-27 18:08:53 +0000 | [diff] [blame] | 108 | const ssize_t y_offset,const MagickBooleanType invert, |
| 109 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 110 | { |
| 111 | #define MaxStacksize (1UL << 15) |
| 112 | #define PushSegmentStack(up,left,right,delta) \ |
| 113 | { \ |
| 114 | if (s >= (segment_stack+MaxStacksize)) \ |
| 115 | ThrowBinaryException(DrawError,"SegmentStackOverflow",image->filename) \ |
| 116 | else \ |
| 117 | { \ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 118 | if ((((up)+(delta)) >= 0) && (((up)+(delta)) < (ssize_t) image->rows)) \ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 119 | { \ |
| 120 | s->x1=(double) (left); \ |
| 121 | s->y1=(double) (up); \ |
| 122 | s->x2=(double) (right); \ |
| 123 | s->y2=(double) (delta); \ |
| 124 | s++; \ |
| 125 | } \ |
| 126 | } \ |
| 127 | } |
| 128 | |
cristy | b0d3bb9 | 2010-09-22 14:37:58 +0000 | [diff] [blame] | 129 | CacheView |
| 130 | *floodplane_view, |
| 131 | *image_view; |
| 132 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 133 | Image |
| 134 | *floodplane_image; |
| 135 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 136 | MagickBooleanType |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 137 | skip, |
| 138 | status; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 139 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 140 | PixelInfo |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 141 | fill, |
| 142 | pixel; |
| 143 | |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame^] | 144 | PixelInfo |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 145 | fill_color; |
| 146 | |
| 147 | register SegmentInfo |
| 148 | *s; |
| 149 | |
| 150 | SegmentInfo |
| 151 | *segment_stack; |
| 152 | |
cristy | 9d314ff | 2011-03-09 01:30:28 +0000 | [diff] [blame] | 153 | ssize_t |
| 154 | offset, |
| 155 | start, |
| 156 | x, |
| 157 | x1, |
| 158 | x2, |
| 159 | y; |
| 160 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 161 | /* |
| 162 | Check boundary conditions. |
| 163 | */ |
| 164 | assert(image != (Image *) NULL); |
| 165 | assert(image->signature == MagickSignature); |
| 166 | if (image->debug != MagickFalse) |
| 167 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 168 | assert(draw_info != (DrawInfo *) NULL); |
| 169 | assert(draw_info->signature == MagickSignature); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 170 | if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 171 | return(MagickFalse); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 172 | if ((y_offset < 0) || (y_offset >= (ssize_t) image->rows)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 173 | return(MagickFalse); |
cristy | 574cc26 | 2011-08-05 01:23:58 +0000 | [diff] [blame] | 174 | if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 175 | return(MagickFalse); |
| 176 | if (image->matte == MagickFalse) |
cristy | 6324088 | 2011-08-05 19:05:27 +0000 | [diff] [blame] | 177 | (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 178 | /* |
| 179 | Set floodfill state. |
| 180 | */ |
| 181 | floodplane_image=CloneImage(image,0,0,MagickTrue,&image->exception); |
| 182 | if (floodplane_image == (Image *) NULL) |
| 183 | return(MagickFalse); |
cristy | 6324088 | 2011-08-05 19:05:27 +0000 | [diff] [blame] | 184 | (void) SetImageAlphaChannel(floodplane_image,OpaqueAlphaChannel,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 185 | segment_stack=(SegmentInfo *) AcquireQuantumMemory(MaxStacksize, |
| 186 | sizeof(*segment_stack)); |
| 187 | if (segment_stack == (SegmentInfo *) NULL) |
| 188 | { |
| 189 | floodplane_image=DestroyImage(floodplane_image); |
| 190 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
| 191 | image->filename); |
| 192 | } |
| 193 | /* |
| 194 | Push initial segment on stack. |
| 195 | */ |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 196 | status=MagickTrue; |
cristy | 2ed42f6 | 2011-10-02 19:49:57 +0000 | [diff] [blame] | 197 | fill_color.black=0.0; |
| 198 | fill_color.index=0.0; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 199 | 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); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 205 | GetPixelInfo(image,&fill); |
| 206 | GetPixelInfo(image,&pixel); |
cristy | b0d3bb9 | 2010-09-22 14:37:58 +0000 | [diff] [blame] | 207 | image_view=AcquireCacheView(image); |
| 208 | floodplane_view=AcquireCacheView(floodplane_image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 209 | while (s > segment_stack) |
| 210 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 211 | register const Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 212 | *restrict p; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 213 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 214 | register Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 215 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 216 | |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 217 | register ssize_t |
| 218 | x; |
| 219 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 220 | /* |
| 221 | Pop segment off stack. |
| 222 | */ |
| 223 | s--; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 224 | x1=(ssize_t) s->x1; |
| 225 | x2=(ssize_t) s->x2; |
| 226 | offset=(ssize_t) s->y2; |
| 227 | y=(ssize_t) s->y1+offset; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 228 | /* |
| 229 | Recolor neighboring pixels. |
| 230 | */ |
cristy | b0d3bb9 | 2010-09-22 14:37:58 +0000 | [diff] [blame] | 231 | p=GetCacheViewVirtualPixels(image_view,0,y,(size_t) (x1+1),1,exception); |
| 232 | q=GetCacheViewAuthenticPixels(floodplane_view,0,y,(size_t) (x1+1),1, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 233 | exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 234 | if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 235 | break; |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 236 | p+=x1*GetPixelChannels(image); |
| 237 | q+=x1*GetPixelChannels(floodplane_image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 238 | for (x=x1; x >= 0; x--) |
| 239 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 240 | if (GetPixelAlpha(image,q) == TransparentAlpha) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 241 | break; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 242 | SetPixelInfo(image,p,&pixel); |
| 243 | if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 244 | break; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 245 | SetPixelAlpha(floodplane_image,TransparentAlpha,q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 246 | p-=GetPixelChannels(image); |
| 247 | q-=GetPixelChannels(floodplane_image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 248 | } |
cristy | b0d3bb9 | 2010-09-22 14:37:58 +0000 | [diff] [blame] | 249 | if (SyncCacheViewAuthenticPixels(floodplane_view,exception) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 250 | break; |
| 251 | skip=x >= x1 ? MagickTrue : MagickFalse; |
| 252 | if (skip == MagickFalse) |
| 253 | { |
| 254 | start=x+1; |
| 255 | if (start < x1) |
| 256 | PushSegmentStack(y,start,x1-1,-offset); |
| 257 | x=x1+1; |
| 258 | } |
| 259 | do |
| 260 | { |
| 261 | if (skip == MagickFalse) |
| 262 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 263 | if (x < (ssize_t) image->columns) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 264 | { |
cristy | b0d3bb9 | 2010-09-22 14:37:58 +0000 | [diff] [blame] | 265 | p=GetCacheViewVirtualPixels(image_view,x,y,image->columns-x,1, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 266 | exception); |
cristy | b0d3bb9 | 2010-09-22 14:37:58 +0000 | [diff] [blame] | 267 | q=GetCacheViewAuthenticPixels(floodplane_view,x,y, |
| 268 | image->columns-x,1,exception); |
cristy | 636dcb5 | 2011-08-26 13:23:49 +0000 | [diff] [blame] | 269 | if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 270 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 271 | for ( ; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 272 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 273 | if (GetPixelAlpha(image,q) == TransparentAlpha) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 274 | break; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 275 | SetPixelInfo(image,p,&pixel); |
| 276 | if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 277 | break; |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 278 | SetPixelAlpha(floodplane_image,TransparentAlpha,q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 279 | p+=GetPixelChannels(image); |
| 280 | q+=GetPixelChannels(floodplane_image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 281 | } |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 282 | status=SyncCacheViewAuthenticPixels(floodplane_view,exception); |
| 283 | if (status == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 284 | break; |
| 285 | } |
| 286 | PushSegmentStack(y,start,x-1,offset); |
| 287 | if (x > (x2+1)) |
| 288 | PushSegmentStack(y,x2+1,x-1,-offset); |
| 289 | } |
| 290 | skip=MagickFalse; |
| 291 | x++; |
| 292 | if (x <= x2) |
| 293 | { |
cristy | b0d3bb9 | 2010-09-22 14:37:58 +0000 | [diff] [blame] | 294 | p=GetCacheViewVirtualPixels(image_view,x,y,(size_t) (x2-x+1),1, |
| 295 | exception); |
| 296 | q=GetCacheViewAuthenticPixels(floodplane_view,x,y,(size_t) (x2-x+1),1, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 297 | exception); |
cristy | 636dcb5 | 2011-08-26 13:23:49 +0000 | [diff] [blame] | 298 | if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 299 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 300 | for ( ; x <= x2; x++) |
| 301 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 302 | if (GetPixelAlpha(image,q) == TransparentAlpha) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 303 | break; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 304 | SetPixelInfo(image,p,&pixel); |
| 305 | if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 306 | break; |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 307 | p+=GetPixelChannels(image); |
| 308 | q+=GetPixelChannels(floodplane_image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | start=x; |
| 312 | } while (x <= x2); |
| 313 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 314 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 315 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 316 | register const Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 317 | *restrict p; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 318 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 319 | register Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 320 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 321 | |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 322 | register ssize_t |
| 323 | x; |
| 324 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 325 | /* |
| 326 | Tile fill color onto floodplane. |
| 327 | */ |
cristy | b0d3bb9 | 2010-09-22 14:37:58 +0000 | [diff] [blame] | 328 | p=GetCacheViewVirtualPixels(floodplane_view,0,y,image->columns,1, |
| 329 | exception); |
| 330 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 331 | if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 332 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 333 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 334 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 335 | if (GetPixelAlpha(floodplane_image,p) != OpaqueAlpha) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 336 | { |
cristy | 2ed42f6 | 2011-10-02 19:49:57 +0000 | [diff] [blame] | 337 | (void) GetFillColor(draw_info,x,y,&fill_color,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 338 | SetPixelInfoPacket(image,&fill_color,&fill); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 339 | if (image->colorspace == CMYKColorspace) |
| 340 | ConvertRGBToCMYK(&fill); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 341 | if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 342 | SetPixelRed(image,ClampToQuantum(fill.red),q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 343 | if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 344 | SetPixelGreen(image,ClampToQuantum(fill.green),q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 345 | if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 346 | SetPixelBlue(image,ClampToQuantum(fill.blue),q); |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 347 | if ((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 348 | SetPixelBlack(image,ClampToQuantum(fill.black),q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 349 | if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 350 | SetPixelAlpha(image,ClampToQuantum(fill.alpha),q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 351 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 352 | p+=GetPixelChannels(floodplane_image); |
| 353 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 354 | } |
cristy | b0d3bb9 | 2010-09-22 14:37:58 +0000 | [diff] [blame] | 355 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 356 | break; |
| 357 | } |
cristy | b0d3bb9 | 2010-09-22 14:37:58 +0000 | [diff] [blame] | 358 | floodplane_view=DestroyCacheView(floodplane_view); |
| 359 | image_view=DestroyCacheView(image_view); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 360 | segment_stack=(SegmentInfo *) RelinquishMagickMemory(segment_stack); |
| 361 | floodplane_image=DestroyImage(floodplane_image); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 362 | return(y == (ssize_t) image->rows ? MagickTrue : MagickFalse); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | /* |
| 366 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 367 | % % |
| 368 | % % |
| 369 | % % |
| 370 | + G r a d i e n t I m a g e % |
| 371 | % % |
| 372 | % % |
| 373 | % % |
| 374 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 375 | % |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 376 | % GradientImage() applies a continuously smooth color transitions along a |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 377 | % vector from one color to another. |
| 378 | % |
| 379 | % Note, the interface of this method will change in the future to support |
| 380 | % more than one transistion. |
| 381 | % |
| 382 | % The format of the GradientImage method is: |
| 383 | % |
| 384 | % MagickBooleanType GradientImage(Image *image,const GradientType type, |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame^] | 385 | % const SpreadMethod method,const PixelInfo *start_color, |
| 386 | % const PixelInfo *stop_color,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 387 | % |
| 388 | % A description of each parameter follows: |
| 389 | % |
| 390 | % o image: the image. |
| 391 | % |
| 392 | % o type: the gradient type: linear or radial. |
| 393 | % |
| 394 | % o spread: the gradient spread meathod: pad, reflect, or repeat. |
| 395 | % |
| 396 | % o start_color: the start color. |
| 397 | % |
| 398 | % o stop_color: the stop color. |
| 399 | % |
cristy | 189e84c | 2011-08-27 18:08:53 +0000 | [diff] [blame] | 400 | % o exception: return any errors or warnings in this structure. |
| 401 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 402 | */ |
cristy | 117ff17 | 2010-08-15 21:35:32 +0000 | [diff] [blame] | 403 | |
| 404 | static inline double MagickMax(const double x,const double y) |
| 405 | { |
| 406 | return(x > y ? x : y); |
| 407 | } |
| 408 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 409 | MagickExport MagickBooleanType GradientImage(Image *image, |
| 410 | const GradientType type,const SpreadMethod method, |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame^] | 411 | const PixelInfo *start_color,const PixelInfo *stop_color, |
cristy | 189e84c | 2011-08-27 18:08:53 +0000 | [diff] [blame] | 412 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 413 | { |
| 414 | DrawInfo |
| 415 | *draw_info; |
| 416 | |
| 417 | GradientInfo |
| 418 | *gradient; |
| 419 | |
| 420 | MagickBooleanType |
| 421 | status; |
| 422 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 423 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 424 | i; |
| 425 | |
| 426 | /* |
| 427 | Set gradient start-stop end points. |
| 428 | */ |
| 429 | assert(image != (const Image *) NULL); |
| 430 | assert(image->signature == MagickSignature); |
| 431 | if (image->debug != MagickFalse) |
| 432 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame^] | 433 | assert(start_color != (const PixelInfo *) NULL); |
| 434 | assert(stop_color != (const PixelInfo *) NULL); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 435 | draw_info=AcquireDrawInfo(); |
| 436 | gradient=(&draw_info->gradient); |
| 437 | gradient->type=type; |
| 438 | gradient->bounding_box.width=image->columns; |
| 439 | gradient->bounding_box.height=image->rows; |
| 440 | gradient->gradient_vector.x2=(double) image->columns-1.0; |
| 441 | gradient->gradient_vector.y2=(double) image->rows-1.0; |
| 442 | if ((type == LinearGradient) && (gradient->gradient_vector.y2 != 0.0)) |
| 443 | gradient->gradient_vector.x2=0.0; |
| 444 | gradient->center.x=(double) gradient->gradient_vector.x2/2.0; |
| 445 | gradient->center.y=(double) gradient->gradient_vector.y2/2.0; |
| 446 | gradient->radius=MagickMax(gradient->center.x,gradient->center.y); |
| 447 | gradient->spread=method; |
| 448 | /* |
| 449 | Define the gradient to fill between the stops. |
| 450 | */ |
| 451 | gradient->number_stops=2; |
| 452 | gradient->stops=(StopInfo *) AcquireQuantumMemory(gradient->number_stops, |
| 453 | sizeof(*gradient->stops)); |
| 454 | if (gradient->stops == (StopInfo *) NULL) |
| 455 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
| 456 | image->filename); |
| 457 | (void) ResetMagickMemory(gradient->stops,0,gradient->number_stops* |
| 458 | sizeof(*gradient->stops)); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 459 | for (i=0; i < (ssize_t) gradient->number_stops; i++) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 460 | GetPixelInfo(image,&gradient->stops[i].color); |
| 461 | SetPixelInfoPacket(image,start_color,&gradient->stops[0].color); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 462 | gradient->stops[0].offset=0.0; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 463 | SetPixelInfoPacket(image,stop_color,&gradient->stops[1].color); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 464 | gradient->stops[1].offset=1.0; |
| 465 | /* |
| 466 | Draw a gradient on the image. |
| 467 | */ |
| 468 | status=DrawGradientImage(image,draw_info); |
| 469 | draw_info=DestroyDrawInfo(draw_info); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 470 | if ((start_color->alpha == OpaqueAlpha) && (stop_color->alpha == OpaqueAlpha)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 471 | image->matte=MagickFalse; |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame^] | 472 | if ((IsPixelInfoGray(start_color) != MagickFalse) && |
| 473 | (IsPixelInfoGray(stop_color) != MagickFalse)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 474 | image->type=GrayscaleType; |
| 475 | return(status); |
| 476 | } |
| 477 | |
| 478 | /* |
| 479 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 480 | % % |
| 481 | % % |
| 482 | % % |
| 483 | % O i l P a i n t I m a g e % |
| 484 | % % |
| 485 | % % |
| 486 | % % |
| 487 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 488 | % |
| 489 | % OilPaintImage() applies a special effect filter that simulates an oil |
| 490 | % painting. Each pixel is replaced by the most frequent color occurring |
| 491 | % in a circular region defined by radius. |
| 492 | % |
| 493 | % The format of the OilPaintImage method is: |
| 494 | % |
| 495 | % Image *OilPaintImage(const Image *image,const double radius, |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 496 | % const double sigma,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 497 | % |
| 498 | % A description of each parameter follows: |
| 499 | % |
| 500 | % o image: the image. |
| 501 | % |
| 502 | % o radius: the radius of the circular neighborhood. |
| 503 | % |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 504 | % o sigma: the standard deviation of the Gaussian, in pixels. |
| 505 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 506 | % o exception: return any errors or warnings in this structure. |
| 507 | % |
| 508 | */ |
| 509 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 510 | static size_t **DestroyHistogramThreadSet(size_t **histogram) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 511 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 512 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 513 | i; |
| 514 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 515 | assert(histogram != (size_t **) NULL); |
| 516 | for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++) |
| 517 | if (histogram[i] != (size_t *) NULL) |
| 518 | histogram[i]=(size_t *) RelinquishMagickMemory(histogram[i]); |
cristy | b41ee10 | 2010-10-04 16:46:15 +0000 | [diff] [blame] | 519 | histogram=(size_t **) RelinquishMagickMemory(histogram); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 520 | return(histogram); |
| 521 | } |
| 522 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 523 | static size_t **AcquireHistogramThreadSet(const size_t count) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 524 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 525 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 526 | i; |
| 527 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 528 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 529 | **histogram, |
| 530 | number_threads; |
| 531 | |
| 532 | number_threads=GetOpenMPMaximumThreads(); |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 533 | histogram=(size_t **) AcquireQuantumMemory(number_threads,sizeof(*histogram)); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 534 | if (histogram == (size_t **) NULL) |
| 535 | return((size_t **) NULL); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 536 | (void) ResetMagickMemory(histogram,0,number_threads*sizeof(*histogram)); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 537 | for (i=0; i < (ssize_t) number_threads; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 538 | { |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 539 | histogram[i]=(size_t *) AcquireQuantumMemory(count,sizeof(**histogram)); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 540 | if (histogram[i] == (size_t *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 541 | return(DestroyHistogramThreadSet(histogram)); |
| 542 | } |
| 543 | return(histogram); |
| 544 | } |
| 545 | |
| 546 | MagickExport Image *OilPaintImage(const Image *image,const double radius, |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 547 | const double sigma,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 548 | { |
| 549 | #define NumberPaintBins 256 |
| 550 | #define OilPaintImageTag "OilPaint/Image" |
| 551 | |
cristy | fa11211 | 2010-01-04 17:48:07 +0000 | [diff] [blame] | 552 | CacheView |
| 553 | *image_view, |
| 554 | *paint_view; |
| 555 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 556 | Image |
| 557 | *paint_image; |
| 558 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 559 | MagickBooleanType |
| 560 | status; |
| 561 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 562 | MagickOffsetType |
| 563 | progress; |
| 564 | |
| 565 | size_t |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 566 | **histograms, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 567 | width; |
| 568 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 569 | ssize_t |
| 570 | y; |
| 571 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 572 | /* |
| 573 | Initialize painted image attributes. |
| 574 | */ |
| 575 | assert(image != (const Image *) NULL); |
| 576 | assert(image->signature == MagickSignature); |
| 577 | if (image->debug != MagickFalse) |
| 578 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 579 | assert(exception != (ExceptionInfo *) NULL); |
| 580 | assert(exception->signature == MagickSignature); |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 581 | width=GetOptimalKernelWidth2D(radius,sigma); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 582 | paint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception); |
| 583 | if (paint_image == (Image *) NULL) |
| 584 | return((Image *) NULL); |
cristy | 574cc26 | 2011-08-05 01:23:58 +0000 | [diff] [blame] | 585 | if (SetImageStorageClass(paint_image,DirectClass,exception) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 586 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 587 | paint_image=DestroyImage(paint_image); |
| 588 | return((Image *) NULL); |
| 589 | } |
| 590 | histograms=AcquireHistogramThreadSet(NumberPaintBins); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 591 | if (histograms == (size_t **) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 592 | { |
| 593 | paint_image=DestroyImage(paint_image); |
| 594 | ThrowImageException(ResourceLimitError,"MemoryAllocationFailed"); |
| 595 | } |
| 596 | /* |
| 597 | Oil paint image. |
| 598 | */ |
| 599 | status=MagickTrue; |
| 600 | progress=0; |
| 601 | image_view=AcquireCacheView(image); |
| 602 | paint_view=AcquireCacheView(paint_image); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 603 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 604 | #pragma omp parallel for schedule(dynamic,4) shared(progress,status) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 605 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 606 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 607 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 608 | register const Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 609 | *restrict p; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 610 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 611 | register Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 612 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 613 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 614 | register size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 615 | *histogram; |
| 616 | |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 617 | register ssize_t |
| 618 | x; |
| 619 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 620 | if (status == MagickFalse) |
| 621 | continue; |
cristy | fe4ba00 | 2011-02-28 14:54:12 +0000 | [diff] [blame] | 622 | p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t) |
| 623 | (width/2L),image->columns+width,width,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 624 | q=QueueCacheViewAuthenticPixels(paint_view,0,y,paint_image->columns,1, |
| 625 | exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 626 | if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 627 | { |
| 628 | status=MagickFalse; |
| 629 | continue; |
| 630 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 631 | histogram=histograms[GetOpenMPThreadId()]; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 632 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 633 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 634 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 635 | i, |
| 636 | u; |
| 637 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 638 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 639 | count; |
| 640 | |
cristy | 9d314ff | 2011-03-09 01:30:28 +0000 | [diff] [blame] | 641 | ssize_t |
| 642 | j, |
| 643 | k, |
| 644 | v; |
| 645 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 646 | /* |
| 647 | Assign most frequent color. |
| 648 | */ |
| 649 | i=0; |
| 650 | j=0; |
| 651 | count=0; |
| 652 | (void) ResetMagickMemory(histogram,0,NumberPaintBins*sizeof(*histogram)); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 653 | for (v=0; v < (ssize_t) width; v++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 654 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 655 | for (u=0; u < (ssize_t) width; u++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 656 | { |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 657 | k=(ssize_t) ScaleQuantumToChar(GetPixelIntensity(image,p+ |
| 658 | GetPixelChannels(image)*(u+i))); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 659 | histogram[k]++; |
| 660 | if (histogram[k] > count) |
| 661 | { |
| 662 | j=i+u; |
| 663 | count=histogram[k]; |
| 664 | } |
| 665 | } |
cristy | d99b096 | 2010-05-29 23:14:26 +0000 | [diff] [blame] | 666 | i+=(ssize_t) (image->columns+width); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 667 | } |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 668 | if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0) |
| 669 | SetPixelRed(paint_image,GetPixelRed(image,p+j* |
| 670 | GetPixelChannels(image)),q); |
| 671 | if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0) |
| 672 | SetPixelGreen(paint_image,GetPixelGreen(image,p+j* |
| 673 | GetPixelChannels(image)),q); |
| 674 | if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0) |
| 675 | SetPixelBlue(paint_image,GetPixelBlue(image,p+j* |
| 676 | GetPixelChannels(image)),q); |
| 677 | if ((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 678 | SetPixelBlack(paint_image,GetPixelBlack(image,p+j* |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 679 | GetPixelChannels(image)),q); |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 680 | if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 681 | SetPixelAlpha(paint_image,GetPixelAlpha(image,p+j* |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 682 | GetPixelChannels(image)),q); |
| 683 | p+=GetPixelChannels(image); |
| 684 | q+=GetPixelChannels(paint_image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 685 | } |
| 686 | if (SyncCacheViewAuthenticPixels(paint_view,exception) == MagickFalse) |
| 687 | status=MagickFalse; |
| 688 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 689 | { |
| 690 | MagickBooleanType |
| 691 | proceed; |
| 692 | |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 693 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 694 | #pragma omp critical (MagickCore_OilPaintImage) |
| 695 | #endif |
| 696 | proceed=SetImageProgress(image,OilPaintImageTag,progress++,image->rows); |
| 697 | if (proceed == MagickFalse) |
| 698 | status=MagickFalse; |
| 699 | } |
| 700 | } |
| 701 | paint_view=DestroyCacheView(paint_view); |
| 702 | image_view=DestroyCacheView(image_view); |
| 703 | histograms=DestroyHistogramThreadSet(histograms); |
| 704 | if (status == MagickFalse) |
| 705 | paint_image=DestroyImage(paint_image); |
| 706 | return(paint_image); |
| 707 | } |
| 708 | |
| 709 | /* |
| 710 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 711 | % % |
| 712 | % % |
| 713 | % % |
| 714 | % O p a q u e P a i n t I m a g e % |
| 715 | % % |
| 716 | % % |
| 717 | % % |
| 718 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 719 | % |
| 720 | % OpaquePaintImage() changes any pixel that matches color with the color |
| 721 | % defined by fill. |
| 722 | % |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 723 | % By default color must match a particular pixel color exactly. However, in |
| 724 | % many cases two colors may differ by a small amount. Fuzz defines how much |
| 725 | % tolerance is acceptable to consider two colors as the same. For example, |
| 726 | % set fuzz to 10 and the color red at intensities of 100 and 102 respectively |
| 727 | % are now interpreted as the same color. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 728 | % |
| 729 | % The format of the OpaquePaintImage method is: |
| 730 | % |
| 731 | % MagickBooleanType OpaquePaintImage(Image *image, |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame^] | 732 | % const PixelInfo *target,const PixelInfo *fill, |
cristy | 189e84c | 2011-08-27 18:08:53 +0000 | [diff] [blame] | 733 | % const MagickBooleanType invert,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 734 | % |
| 735 | % A description of each parameter follows: |
| 736 | % |
| 737 | % o image: the image. |
| 738 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 739 | % o target: the RGB value of the target color. |
| 740 | % |
| 741 | % o fill: the replacement color. |
| 742 | % |
| 743 | % o invert: paint any pixel that does not match the target color. |
| 744 | % |
cristy | 189e84c | 2011-08-27 18:08:53 +0000 | [diff] [blame] | 745 | % o exception: return any errors or warnings in this structure. |
| 746 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 747 | */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 748 | MagickExport MagickBooleanType OpaquePaintImage(Image *image, |
cristy | 189e84c | 2011-08-27 18:08:53 +0000 | [diff] [blame] | 749 | const PixelInfo *target,const PixelInfo *fill,const MagickBooleanType invert, |
| 750 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 751 | { |
| 752 | #define OpaquePaintImageTag "Opaque/Image" |
| 753 | |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 754 | CacheView |
| 755 | *image_view; |
| 756 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 757 | MagickBooleanType |
| 758 | status; |
| 759 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 760 | MagickOffsetType |
| 761 | progress; |
| 762 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 763 | PixelInfo |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 764 | zero; |
| 765 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 766 | ssize_t |
| 767 | y; |
| 768 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 769 | assert(image != (Image *) NULL); |
| 770 | assert(image->signature == MagickSignature); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 771 | assert(target != (PixelInfo *) NULL); |
| 772 | assert(fill != (PixelInfo *) NULL); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 773 | if (image->debug != MagickFalse) |
| 774 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | 574cc26 | 2011-08-05 01:23:58 +0000 | [diff] [blame] | 775 | if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 776 | return(MagickFalse); |
| 777 | /* |
| 778 | Make image color opaque. |
| 779 | */ |
| 780 | status=MagickTrue; |
| 781 | progress=0; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 782 | GetPixelInfo(image,&zero); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 783 | image_view=AcquireCacheView(image); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 784 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 785 | #pragma omp parallel for schedule(dynamic,4) shared(progress,status) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 786 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 787 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 788 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 789 | PixelInfo |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 790 | pixel; |
| 791 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 792 | register Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 793 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 794 | |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 795 | register ssize_t |
| 796 | x; |
| 797 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 798 | if (status == MagickFalse) |
| 799 | continue; |
| 800 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 801 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 802 | { |
| 803 | status=MagickFalse; |
| 804 | continue; |
| 805 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 806 | pixel=zero; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 807 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 808 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 809 | SetPixelInfo(image,q,&pixel); |
| 810 | if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 811 | { |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 812 | if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 813 | SetPixelRed(image,ClampToQuantum(fill->red),q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 814 | if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 815 | SetPixelGreen(image,ClampToQuantum(fill->green),q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 816 | if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 817 | SetPixelBlue(image,ClampToQuantum(fill->blue),q); |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 818 | if ((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 819 | SetPixelBlack(image,ClampToQuantum(fill->black),q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 820 | if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 821 | SetPixelAlpha(image,ClampToQuantum(fill->alpha),q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 822 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 823 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 824 | } |
| 825 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
| 826 | status=MagickFalse; |
| 827 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 828 | { |
| 829 | MagickBooleanType |
| 830 | proceed; |
| 831 | |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 832 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | d42d995 | 2011-07-08 14:21:50 +0000 | [diff] [blame] | 833 | #pragma omp critical (MagickCore_OpaquePaintImage) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 834 | #endif |
| 835 | proceed=SetImageProgress(image,OpaquePaintImageTag,progress++, |
| 836 | image->rows); |
| 837 | if (proceed == MagickFalse) |
| 838 | status=MagickFalse; |
| 839 | } |
| 840 | } |
| 841 | image_view=DestroyCacheView(image_view); |
| 842 | return(status); |
| 843 | } |
| 844 | |
| 845 | /* |
| 846 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 847 | % % |
| 848 | % % |
| 849 | % % |
| 850 | % T r a n s p a r e n t P a i n t I m a g e % |
| 851 | % % |
| 852 | % % |
| 853 | % % |
| 854 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 855 | % |
| 856 | % TransparentPaintImage() changes the opacity value associated with any pixel |
| 857 | % that matches color to the value defined by opacity. |
| 858 | % |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 859 | % By default color must match a particular pixel color exactly. However, in |
| 860 | % many cases two colors may differ by a small amount. Fuzz defines how much |
| 861 | % tolerance is acceptable to consider two colors as the same. For example, |
| 862 | % set fuzz to 10 and the color red at intensities of 100 and 102 respectively |
| 863 | % are now interpreted as the same color. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 864 | % |
| 865 | % The format of the TransparentPaintImage method is: |
| 866 | % |
| 867 | % MagickBooleanType TransparentPaintImage(Image *image, |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 868 | % const PixelInfo *target,const Quantum opacity, |
cristy | 189e84c | 2011-08-27 18:08:53 +0000 | [diff] [blame] | 869 | % const MagickBooleanType invert,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 870 | % |
| 871 | % A description of each parameter follows: |
| 872 | % |
| 873 | % o image: the image. |
| 874 | % |
| 875 | % o target: the target color. |
| 876 | % |
| 877 | % o opacity: the replacement opacity value. |
| 878 | % |
| 879 | % o invert: paint any pixel that does not match the target color. |
| 880 | % |
cristy | 189e84c | 2011-08-27 18:08:53 +0000 | [diff] [blame] | 881 | % o exception: return any errors or warnings in this structure. |
| 882 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 883 | */ |
| 884 | MagickExport MagickBooleanType TransparentPaintImage(Image *image, |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 885 | const PixelInfo *target,const Quantum opacity,const MagickBooleanType invert, |
| 886 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 887 | { |
| 888 | #define TransparentPaintImageTag "Transparent/Image" |
| 889 | |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 890 | CacheView |
| 891 | *image_view; |
| 892 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 893 | MagickBooleanType |
| 894 | status; |
| 895 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 896 | MagickOffsetType |
| 897 | progress; |
| 898 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 899 | PixelInfo |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 900 | zero; |
| 901 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 902 | ssize_t |
| 903 | y; |
| 904 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 905 | assert(image != (Image *) NULL); |
| 906 | assert(image->signature == MagickSignature); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 907 | assert(target != (PixelInfo *) NULL); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 908 | if (image->debug != MagickFalse) |
| 909 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | 574cc26 | 2011-08-05 01:23:58 +0000 | [diff] [blame] | 910 | if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 911 | return(MagickFalse); |
| 912 | if (image->matte == MagickFalse) |
cristy | 6324088 | 2011-08-05 19:05:27 +0000 | [diff] [blame] | 913 | (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 914 | /* |
| 915 | Make image color transparent. |
| 916 | */ |
| 917 | status=MagickTrue; |
| 918 | progress=0; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 919 | GetPixelInfo(image,&zero); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 920 | image_view=AcquireCacheView(image); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 921 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 922 | #pragma omp parallel for schedule(dynamic,4) shared(progress,status) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 923 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 924 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 925 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 926 | PixelInfo |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 927 | pixel; |
| 928 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 929 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 930 | x; |
| 931 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 932 | register Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 933 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 934 | |
| 935 | if (status == MagickFalse) |
| 936 | continue; |
| 937 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 938 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 939 | { |
| 940 | status=MagickFalse; |
| 941 | continue; |
| 942 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 943 | pixel=zero; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 944 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 945 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 946 | SetPixelInfo(image,q,&pixel); |
| 947 | if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert) |
| 948 | SetPixelAlpha(image,opacity,q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 949 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 950 | } |
| 951 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
| 952 | status=MagickFalse; |
| 953 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 954 | { |
| 955 | MagickBooleanType |
| 956 | proceed; |
| 957 | |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 958 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 959 | #pragma omp critical (MagickCore_TransparentPaintImage) |
| 960 | #endif |
| 961 | proceed=SetImageProgress(image,TransparentPaintImageTag,progress++, |
| 962 | image->rows); |
| 963 | if (proceed == MagickFalse) |
| 964 | status=MagickFalse; |
| 965 | } |
| 966 | } |
| 967 | image_view=DestroyCacheView(image_view); |
| 968 | return(status); |
| 969 | } |
| 970 | |
| 971 | /* |
| 972 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 973 | % % |
| 974 | % % |
| 975 | % % |
| 976 | % 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 % |
| 977 | % % |
| 978 | % % |
| 979 | % % |
| 980 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 981 | % |
| 982 | % TransparentPaintImageChroma() changes the opacity value associated with any |
| 983 | % pixel that matches color to the value defined by opacity. |
| 984 | % |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 985 | % As there is one fuzz value for the all the channels, TransparentPaintImage() |
| 986 | % is not suitable for the operations like chroma, where the tolerance for |
| 987 | % similarity of two color component (RGB) can be different. Thus we define |
| 988 | % this method to take two target pixels (one low and one high) and all the |
| 989 | % pixels of an image which are lying between these two pixels are made |
| 990 | % transparent. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 991 | % |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 992 | % The format of the TransparentPaintImageChroma method is: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 993 | % |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 994 | % MagickBooleanType TransparentPaintImageChroma(Image *image, |
| 995 | % const PixelInfo *low,const PixelInfo *high,const Quantum opacity, |
| 996 | % const MagickBooleanType invert,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 997 | % |
| 998 | % A description of each parameter follows: |
| 999 | % |
| 1000 | % o image: the image. |
| 1001 | % |
| 1002 | % o low: the low target color. |
| 1003 | % |
| 1004 | % o high: the high target color. |
| 1005 | % |
| 1006 | % o opacity: the replacement opacity value. |
| 1007 | % |
| 1008 | % o invert: paint any pixel that does not match the target color. |
| 1009 | % |
cristy | 189e84c | 2011-08-27 18:08:53 +0000 | [diff] [blame] | 1010 | % o exception: return any errors or warnings in this structure. |
| 1011 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1012 | */ |
| 1013 | MagickExport MagickBooleanType TransparentPaintImageChroma(Image *image, |
cristy | 189e84c | 2011-08-27 18:08:53 +0000 | [diff] [blame] | 1014 | const PixelInfo *low,const PixelInfo *high,const Quantum opacity, |
| 1015 | const MagickBooleanType invert,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1016 | { |
| 1017 | #define TransparentPaintImageTag "Transparent/Image" |
| 1018 | |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 1019 | CacheView |
| 1020 | *image_view; |
| 1021 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1022 | MagickBooleanType |
| 1023 | status; |
| 1024 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1025 | MagickOffsetType |
| 1026 | progress; |
| 1027 | |
| 1028 | ssize_t |
| 1029 | y; |
| 1030 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1031 | assert(image != (Image *) NULL); |
| 1032 | assert(image->signature == MagickSignature); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1033 | assert(high != (PixelInfo *) NULL); |
| 1034 | assert(low != (PixelInfo *) NULL); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1035 | if (image->debug != MagickFalse) |
| 1036 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | 574cc26 | 2011-08-05 01:23:58 +0000 | [diff] [blame] | 1037 | if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1038 | return(MagickFalse); |
| 1039 | if (image->matte == MagickFalse) |
cristy | 6324088 | 2011-08-05 19:05:27 +0000 | [diff] [blame] | 1040 | (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1041 | /* |
| 1042 | Make image color transparent. |
| 1043 | */ |
| 1044 | status=MagickTrue; |
| 1045 | progress=0; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1046 | image_view=AcquireCacheView(image); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 1047 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 1048 | #pragma omp parallel for schedule(dynamic,4) shared(progress,status) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1049 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1050 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1051 | { |
| 1052 | MagickBooleanType |
| 1053 | match; |
| 1054 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1055 | PixelInfo |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1056 | pixel; |
| 1057 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1058 | register Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1059 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1060 | |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 1061 | register ssize_t |
| 1062 | x; |
| 1063 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1064 | if (status == MagickFalse) |
| 1065 | continue; |
| 1066 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 1067 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1068 | { |
| 1069 | status=MagickFalse; |
| 1070 | continue; |
| 1071 | } |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1072 | GetPixelInfo(image,&pixel); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1073 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1074 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1075 | SetPixelInfo(image,q,&pixel); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1076 | match=((pixel.red >= low->red) && (pixel.red <= high->red) && |
| 1077 | (pixel.green >= low->green) && (pixel.green <= high->green) && |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 1078 | (pixel.blue >= low->blue) && (pixel.blue <= high->blue)) ? MagickTrue : |
| 1079 | MagickFalse; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1080 | if (match != invert) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1081 | SetPixelAlpha(image,opacity,q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1082 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1083 | } |
| 1084 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
| 1085 | status=MagickFalse; |
| 1086 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 1087 | { |
| 1088 | MagickBooleanType |
| 1089 | proceed; |
| 1090 | |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 1091 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1092 | #pragma omp critical (MagickCore_TransparentPaintImageChroma) |
| 1093 | #endif |
| 1094 | proceed=SetImageProgress(image,TransparentPaintImageTag,progress++, |
| 1095 | image->rows); |
| 1096 | if (proceed == MagickFalse) |
| 1097 | status=MagickFalse; |
| 1098 | } |
| 1099 | } |
| 1100 | image_view=DestroyCacheView(image_view); |
| 1101 | return(status); |
| 1102 | } |