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