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