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