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