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