cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % % |
| 6 | % PPPP AAA IIIII N N TTTTT % |
| 7 | % P P A A I NN N T % |
| 8 | % PPPP AAAAA I N N N T % |
| 9 | % P A A I N NN T % |
| 10 | % P A A IIIII N N T % |
| 11 | % % |
| 12 | % % |
| 13 | % Methods to Paint on an Image % |
| 14 | % % |
| 15 | % Software Design % |
| 16 | % John Cristy % |
| 17 | % July 1998 % |
| 18 | % % |
| 19 | % % |
cristy | 45ef08f | 2012-12-07 13:13:34 +0000 | [diff] [blame] | 20 | % Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 21 | % dedicated to making software imaging solutions freely available. % |
| 22 | % % |
| 23 | % You may not use this file except in compliance with the License. You may % |
| 24 | % obtain a copy of the License at % |
| 25 | % % |
| 26 | % http://www.imagemagick.org/script/license.php % |
| 27 | % % |
| 28 | % Unless required by applicable law or agreed to in writing, software % |
| 29 | % distributed under the License is distributed on an "AS IS" BASIS, % |
| 30 | % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. % |
| 31 | % See the License for the specific language governing permissions and % |
| 32 | % limitations under the License. % |
| 33 | % % |
| 34 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 35 | % |
| 36 | % |
| 37 | */ |
| 38 | |
| 39 | /* |
| 40 | Include declarations. |
| 41 | */ |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 42 | #include "MagickCore/studio.h" |
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 | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 143 | PixelInfo |
cristy | fdcc018 | 2011-12-26 19:33:56 +0000 | [diff] [blame] | 144 | fill_color, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 145 | pixel; |
| 146 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 147 | register SegmentInfo |
| 148 | *s; |
| 149 | |
| 150 | SegmentInfo |
| 151 | *segment_stack; |
| 152 | |
cristy | 9d314ff | 2011-03-09 01:30:28 +0000 | [diff] [blame] | 153 | ssize_t |
| 154 | offset, |
| 155 | start, |
| 156 | x, |
| 157 | x1, |
| 158 | x2, |
| 159 | y; |
| 160 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 161 | /* |
| 162 | Check boundary conditions. |
| 163 | */ |
| 164 | assert(image != (Image *) NULL); |
| 165 | assert(image->signature == MagickSignature); |
| 166 | if (image->debug != MagickFalse) |
| 167 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 168 | assert(draw_info != (DrawInfo *) NULL); |
| 169 | assert(draw_info->signature == MagickSignature); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 170 | if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 171 | return(MagickFalse); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 172 | if ((y_offset < 0) || (y_offset >= (ssize_t) image->rows)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 173 | return(MagickFalse); |
cristy | 574cc26 | 2011-08-05 01:23:58 +0000 | [diff] [blame] | 174 | if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 175 | return(MagickFalse); |
cristy | a6400b1 | 2013-03-15 12:20:18 +0000 | [diff] [blame] | 176 | if (IsGrayColorspace(image->colorspace) != MagickFalse) |
cristy | 0c81d06 | 2013-04-21 15:22:02 +0000 | [diff] [blame] | 177 | (void) SetImageColorspace(image,sRGBColorspace,exception); |
cristy | 400275a | 2013-03-13 00:34:24 +0000 | [diff] [blame] | 178 | if ((image->alpha_trait != BlendPixelTrait) && |
| 179 | (draw_info->fill.alpha_trait == BlendPixelTrait)) |
cristy | 5b67d4e | 2012-02-07 19:43:53 +0000 | [diff] [blame] | 180 | (void) SetImageAlpha(image,OpaqueAlpha,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 181 | /* |
| 182 | Set floodfill state. |
| 183 | */ |
cristy | 95f562a | 2012-01-01 20:49:11 +0000 | [diff] [blame] | 184 | floodplane_image=CloneImage(image,image->columns,image->rows,MagickTrue, |
| 185 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 186 | if (floodplane_image == (Image *) NULL) |
| 187 | return(MagickFalse); |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 188 | floodplane_image->alpha_trait=UndefinedPixelTrait; |
cristy | 2099010 | 2012-05-16 18:10:49 +0000 | [diff] [blame] | 189 | floodplane_image->colorspace=GRAYColorspace; |
| 190 | (void) QueryColorCompliance("#000",AllCompliance, |
| 191 | &floodplane_image->background_color,exception); |
| 192 | (void) SetImageBackgroundColor(floodplane_image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 193 | segment_stack=(SegmentInfo *) AcquireQuantumMemory(MaxStacksize, |
| 194 | sizeof(*segment_stack)); |
| 195 | if (segment_stack == (SegmentInfo *) NULL) |
| 196 | { |
| 197 | floodplane_image=DestroyImage(floodplane_image); |
| 198 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
| 199 | image->filename); |
| 200 | } |
| 201 | /* |
| 202 | Push initial segment on stack. |
| 203 | */ |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 204 | status=MagickTrue; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 205 | x=x_offset; |
| 206 | y=y_offset; |
| 207 | start=0; |
| 208 | s=segment_stack; |
| 209 | PushSegmentStack(y,x,x,1); |
| 210 | PushSegmentStack(y+1,x,x,-1); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 211 | GetPixelInfo(image,&pixel); |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 212 | image_view=AcquireVirtualCacheView(image,exception); |
| 213 | floodplane_view=AcquireAuthenticCacheView(floodplane_image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 214 | while (s > segment_stack) |
| 215 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 216 | register const Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 217 | *restrict p; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 218 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 219 | register Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 220 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 221 | |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 222 | register ssize_t |
| 223 | x; |
| 224 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 225 | /* |
| 226 | Pop segment off stack. |
| 227 | */ |
| 228 | s--; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 229 | x1=(ssize_t) s->x1; |
| 230 | x2=(ssize_t) s->x2; |
| 231 | offset=(ssize_t) s->y2; |
| 232 | y=(ssize_t) s->y1+offset; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 233 | /* |
| 234 | Recolor neighboring pixels. |
| 235 | */ |
cristy | b0d3bb9 | 2010-09-22 14:37:58 +0000 | [diff] [blame] | 236 | p=GetCacheViewVirtualPixels(image_view,0,y,(size_t) (x1+1),1,exception); |
| 237 | q=GetCacheViewAuthenticPixels(floodplane_view,0,y,(size_t) (x1+1),1, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 238 | exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 239 | if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 240 | break; |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 241 | p+=x1*GetPixelChannels(image); |
| 242 | q+=x1*GetPixelChannels(floodplane_image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 243 | for (x=x1; x >= 0; x--) |
| 244 | { |
cristy | 95f562a | 2012-01-01 20:49:11 +0000 | [diff] [blame] | 245 | if (GetPixelGray(floodplane_image,q) != 0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 246 | break; |
cristy | 803640d | 2011-11-17 02:11:32 +0000 | [diff] [blame] | 247 | GetPixelInfoPixel(image,p,&pixel); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 248 | if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 249 | break; |
cristy | 95f562a | 2012-01-01 20:49:11 +0000 | [diff] [blame] | 250 | SetPixelGray(floodplane_image,QuantumRange,q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 251 | p-=GetPixelChannels(image); |
| 252 | q-=GetPixelChannels(floodplane_image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 253 | } |
cristy | b0d3bb9 | 2010-09-22 14:37:58 +0000 | [diff] [blame] | 254 | if (SyncCacheViewAuthenticPixels(floodplane_view,exception) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 255 | break; |
| 256 | skip=x >= x1 ? MagickTrue : MagickFalse; |
| 257 | if (skip == MagickFalse) |
| 258 | { |
| 259 | start=x+1; |
| 260 | if (start < x1) |
| 261 | PushSegmentStack(y,start,x1-1,-offset); |
| 262 | x=x1+1; |
| 263 | } |
| 264 | do |
| 265 | { |
| 266 | if (skip == MagickFalse) |
| 267 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 268 | if (x < (ssize_t) image->columns) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 269 | { |
cristy | b0d3bb9 | 2010-09-22 14:37:58 +0000 | [diff] [blame] | 270 | p=GetCacheViewVirtualPixels(image_view,x,y,image->columns-x,1, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 271 | exception); |
cristy | fdcc018 | 2011-12-26 19:33:56 +0000 | [diff] [blame] | 272 | q=GetCacheViewAuthenticPixels(floodplane_view,x,y,image->columns- |
| 273 | x,1,exception); |
cristy | 636dcb5 | 2011-08-26 13:23:49 +0000 | [diff] [blame] | 274 | if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 275 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 276 | for ( ; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 277 | { |
cristy | 95f562a | 2012-01-01 20:49:11 +0000 | [diff] [blame] | 278 | if (GetPixelGray(floodplane_image,q) != 0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 279 | break; |
cristy | 803640d | 2011-11-17 02:11:32 +0000 | [diff] [blame] | 280 | GetPixelInfoPixel(image,p,&pixel); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 281 | if (IsFuzzyEquivalencePixelInfo(&pixel,target) == invert) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 282 | break; |
cristy | 95f562a | 2012-01-01 20:49:11 +0000 | [diff] [blame] | 283 | SetPixelGray(floodplane_image,QuantumRange,q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 284 | p+=GetPixelChannels(image); |
| 285 | q+=GetPixelChannels(floodplane_image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 286 | } |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 287 | status=SyncCacheViewAuthenticPixels(floodplane_view,exception); |
| 288 | if (status == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 289 | break; |
| 290 | } |
| 291 | PushSegmentStack(y,start,x-1,offset); |
| 292 | if (x > (x2+1)) |
| 293 | PushSegmentStack(y,x2+1,x-1,-offset); |
| 294 | } |
| 295 | skip=MagickFalse; |
| 296 | x++; |
| 297 | if (x <= x2) |
| 298 | { |
cristy | b0d3bb9 | 2010-09-22 14:37:58 +0000 | [diff] [blame] | 299 | p=GetCacheViewVirtualPixels(image_view,x,y,(size_t) (x2-x+1),1, |
| 300 | exception); |
| 301 | q=GetCacheViewAuthenticPixels(floodplane_view,x,y,(size_t) (x2-x+1),1, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 302 | exception); |
cristy | 636dcb5 | 2011-08-26 13:23:49 +0000 | [diff] [blame] | 303 | if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 304 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 305 | for ( ; x <= x2; x++) |
| 306 | { |
cristy | 95f562a | 2012-01-01 20:49:11 +0000 | [diff] [blame] | 307 | if (GetPixelGray(floodplane_image,q) != 0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 308 | break; |
cristy | 803640d | 2011-11-17 02:11:32 +0000 | [diff] [blame] | 309 | GetPixelInfoPixel(image,p,&pixel); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 310 | if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 311 | break; |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 312 | p+=GetPixelChannels(image); |
| 313 | q+=GetPixelChannels(floodplane_image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | start=x; |
| 317 | } while (x <= x2); |
| 318 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 319 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 320 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 321 | register const Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 322 | *restrict p; |
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 Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 325 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 326 | |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 327 | register ssize_t |
| 328 | x; |
| 329 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 330 | /* |
| 331 | Tile fill color onto floodplane. |
| 332 | */ |
cristy | 95f562a | 2012-01-01 20:49:11 +0000 | [diff] [blame] | 333 | p=GetCacheViewVirtualPixels(floodplane_view,0,y,image->columns,1,exception); |
cristy | b0d3bb9 | 2010-09-22 14:37:58 +0000 | [diff] [blame] | 334 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 335 | if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 336 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 337 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 338 | { |
cristy | 95f562a | 2012-01-01 20:49:11 +0000 | [diff] [blame] | 339 | if (GetPixelGray(floodplane_image,p) != 0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 340 | { |
cristy | 2ed42f6 | 2011-10-02 19:49:57 +0000 | [diff] [blame] | 341 | (void) GetFillColor(draw_info,x,y,&fill_color,exception); |
cristy | 8a20fa0 | 2011-12-27 15:54:31 +0000 | [diff] [blame] | 342 | SetPixelInfoPixel(image,&fill_color,q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 343 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 344 | p+=GetPixelChannels(floodplane_image); |
| 345 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 346 | } |
cristy | b0d3bb9 | 2010-09-22 14:37:58 +0000 | [diff] [blame] | 347 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 348 | break; |
| 349 | } |
cristy | b0d3bb9 | 2010-09-22 14:37:58 +0000 | [diff] [blame] | 350 | floodplane_view=DestroyCacheView(floodplane_view); |
| 351 | image_view=DestroyCacheView(image_view); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 352 | segment_stack=(SegmentInfo *) RelinquishMagickMemory(segment_stack); |
| 353 | floodplane_image=DestroyImage(floodplane_image); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 354 | return(y == (ssize_t) image->rows ? MagickTrue : MagickFalse); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | /* |
| 358 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 359 | % % |
| 360 | % % |
| 361 | % % |
| 362 | + G r a d i e n t I m a g e % |
| 363 | % % |
| 364 | % % |
| 365 | % % |
| 366 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 367 | % |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 368 | % GradientImage() applies a continuously smooth color transitions along a |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 369 | % vector from one color to another. |
| 370 | % |
| 371 | % Note, the interface of this method will change in the future to support |
| 372 | % more than one transistion. |
| 373 | % |
| 374 | % The format of the GradientImage method is: |
| 375 | % |
| 376 | % MagickBooleanType GradientImage(Image *image,const GradientType type, |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 377 | % const SpreadMethod method,const PixelInfo *start_color, |
| 378 | % const PixelInfo *stop_color,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 379 | % |
| 380 | % A description of each parameter follows: |
| 381 | % |
| 382 | % o image: the image. |
| 383 | % |
| 384 | % o type: the gradient type: linear or radial. |
| 385 | % |
| 386 | % o spread: the gradient spread meathod: pad, reflect, or repeat. |
| 387 | % |
| 388 | % o start_color: the start color. |
| 389 | % |
| 390 | % o stop_color: the stop color. |
| 391 | % |
cristy | 189e84c | 2011-08-27 18:08:53 +0000 | [diff] [blame] | 392 | % o exception: return any errors or warnings in this structure. |
| 393 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 394 | */ |
cristy | 117ff17 | 2010-08-15 21:35:32 +0000 | [diff] [blame] | 395 | |
| 396 | static inline double MagickMax(const double x,const double y) |
| 397 | { |
| 398 | return(x > y ? x : y); |
| 399 | } |
| 400 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 401 | MagickExport MagickBooleanType GradientImage(Image *image, |
| 402 | const GradientType type,const SpreadMethod method, |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 403 | const PixelInfo *start_color,const PixelInfo *stop_color, |
cristy | 189e84c | 2011-08-27 18:08:53 +0000 | [diff] [blame] | 404 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 405 | { |
| 406 | DrawInfo |
| 407 | *draw_info; |
| 408 | |
| 409 | GradientInfo |
| 410 | *gradient; |
| 411 | |
| 412 | MagickBooleanType |
| 413 | status; |
| 414 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 415 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 416 | i; |
| 417 | |
| 418 | /* |
| 419 | Set gradient start-stop end points. |
| 420 | */ |
| 421 | assert(image != (const Image *) NULL); |
| 422 | assert(image->signature == MagickSignature); |
| 423 | if (image->debug != MagickFalse) |
| 424 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 425 | assert(start_color != (const PixelInfo *) NULL); |
| 426 | assert(stop_color != (const PixelInfo *) NULL); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 427 | draw_info=AcquireDrawInfo(); |
| 428 | gradient=(&draw_info->gradient); |
| 429 | gradient->type=type; |
| 430 | gradient->bounding_box.width=image->columns; |
| 431 | gradient->bounding_box.height=image->rows; |
| 432 | gradient->gradient_vector.x2=(double) image->columns-1.0; |
| 433 | gradient->gradient_vector.y2=(double) image->rows-1.0; |
| 434 | if ((type == LinearGradient) && (gradient->gradient_vector.y2 != 0.0)) |
| 435 | gradient->gradient_vector.x2=0.0; |
| 436 | gradient->center.x=(double) gradient->gradient_vector.x2/2.0; |
| 437 | gradient->center.y=(double) gradient->gradient_vector.y2/2.0; |
| 438 | gradient->radius=MagickMax(gradient->center.x,gradient->center.y); |
| 439 | gradient->spread=method; |
| 440 | /* |
| 441 | Define the gradient to fill between the stops. |
| 442 | */ |
| 443 | gradient->number_stops=2; |
| 444 | gradient->stops=(StopInfo *) AcquireQuantumMemory(gradient->number_stops, |
| 445 | sizeof(*gradient->stops)); |
| 446 | if (gradient->stops == (StopInfo *) NULL) |
| 447 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
| 448 | image->filename); |
| 449 | (void) ResetMagickMemory(gradient->stops,0,gradient->number_stops* |
| 450 | sizeof(*gradient->stops)); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 451 | for (i=0; i < (ssize_t) gradient->number_stops; i++) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 452 | GetPixelInfo(image,&gradient->stops[i].color); |
cristy | 9d8c8ce | 2011-10-25 16:13:52 +0000 | [diff] [blame] | 453 | gradient->stops[0].color=(*start_color); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 454 | gradient->stops[0].offset=0.0; |
cristy | 9d8c8ce | 2011-10-25 16:13:52 +0000 | [diff] [blame] | 455 | gradient->stops[1].color=(*stop_color); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 456 | gradient->stops[1].offset=1.0; |
| 457 | /* |
| 458 | Draw a gradient on the image. |
| 459 | */ |
cristy | bafdbba | 2012-06-20 11:50:57 +0000 | [diff] [blame] | 460 | (void) SetImageColorspace(image,start_color->colorspace,exception); |
cristy | 947cb4c | 2011-10-20 18:41:46 +0000 | [diff] [blame] | 461 | status=DrawGradientImage(image,draw_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 462 | draw_info=DestroyDrawInfo(draw_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 463 | return(status); |
| 464 | } |
| 465 | |
| 466 | /* |
| 467 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 468 | % % |
| 469 | % % |
| 470 | % % |
| 471 | % O i l P a i n t I m a g e % |
| 472 | % % |
| 473 | % % |
| 474 | % % |
| 475 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 476 | % |
| 477 | % OilPaintImage() applies a special effect filter that simulates an oil |
| 478 | % painting. Each pixel is replaced by the most frequent color occurring |
| 479 | % in a circular region defined by radius. |
| 480 | % |
| 481 | % The format of the OilPaintImage method is: |
| 482 | % |
| 483 | % Image *OilPaintImage(const Image *image,const double radius, |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 484 | % const double sigma,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 485 | % |
| 486 | % A description of each parameter follows: |
| 487 | % |
| 488 | % o image: the image. |
| 489 | % |
| 490 | % o radius: the radius of the circular neighborhood. |
| 491 | % |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 492 | % o sigma: the standard deviation of the Gaussian, in pixels. |
| 493 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 494 | % o exception: return any errors or warnings in this structure. |
| 495 | % |
| 496 | */ |
| 497 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 498 | static size_t **DestroyHistogramThreadSet(size_t **histogram) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 499 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 500 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 501 | i; |
| 502 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 503 | assert(histogram != (size_t **) NULL); |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 504 | for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 505 | if (histogram[i] != (size_t *) NULL) |
| 506 | histogram[i]=(size_t *) RelinquishMagickMemory(histogram[i]); |
cristy | b41ee10 | 2010-10-04 16:46:15 +0000 | [diff] [blame] | 507 | histogram=(size_t **) RelinquishMagickMemory(histogram); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 508 | return(histogram); |
| 509 | } |
| 510 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 511 | static size_t **AcquireHistogramThreadSet(const size_t count) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 512 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 513 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 514 | i; |
| 515 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 516 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 517 | **histogram, |
| 518 | number_threads; |
| 519 | |
cristy | 9357bdd | 2012-07-30 12:28:34 +0000 | [diff] [blame] | 520 | number_threads=(size_t) GetMagickResourceLimit(ThreadResource); |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 521 | histogram=(size_t **) AcquireQuantumMemory(number_threads,sizeof(*histogram)); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 522 | if (histogram == (size_t **) NULL) |
| 523 | return((size_t **) NULL); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 524 | (void) ResetMagickMemory(histogram,0,number_threads*sizeof(*histogram)); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 525 | for (i=0; i < (ssize_t) number_threads; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 526 | { |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 527 | histogram[i]=(size_t *) AcquireQuantumMemory(count,sizeof(**histogram)); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 528 | if (histogram[i] == (size_t *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 529 | return(DestroyHistogramThreadSet(histogram)); |
| 530 | } |
| 531 | return(histogram); |
| 532 | } |
| 533 | |
| 534 | MagickExport Image *OilPaintImage(const Image *image,const double radius, |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 535 | const double sigma,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 536 | { |
| 537 | #define NumberPaintBins 256 |
| 538 | #define OilPaintImageTag "OilPaint/Image" |
| 539 | |
cristy | fa11211 | 2010-01-04 17:48:07 +0000 | [diff] [blame] | 540 | CacheView |
| 541 | *image_view, |
| 542 | *paint_view; |
| 543 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 544 | Image |
cristy | 404d0da | 2012-12-20 13:04:30 +0000 | [diff] [blame] | 545 | *linear_image, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 546 | *paint_image; |
| 547 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 548 | MagickBooleanType |
| 549 | status; |
| 550 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 551 | MagickOffsetType |
| 552 | progress; |
| 553 | |
| 554 | size_t |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 555 | **histograms, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 556 | width; |
| 557 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 558 | ssize_t |
cristy | f856154 | 2012-01-24 00:26:46 +0000 | [diff] [blame] | 559 | center, |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 560 | y; |
| 561 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 562 | /* |
| 563 | Initialize painted image attributes. |
| 564 | */ |
| 565 | assert(image != (const Image *) NULL); |
| 566 | assert(image->signature == MagickSignature); |
| 567 | if (image->debug != MagickFalse) |
| 568 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 569 | assert(exception != (ExceptionInfo *) NULL); |
| 570 | assert(exception->signature == MagickSignature); |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 571 | width=GetOptimalKernelWidth2D(radius,sigma); |
cristy | 404d0da | 2012-12-20 13:04:30 +0000 | [diff] [blame] | 572 | linear_image=CloneImage(image,0,0,MagickTrue,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 573 | paint_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception); |
cristy | 404d0da | 2012-12-20 13:04:30 +0000 | [diff] [blame] | 574 | if ((linear_image == (Image *) NULL) || (paint_image == (Image *) NULL)) |
| 575 | { |
| 576 | if (linear_image != (Image *) NULL) |
| 577 | linear_image=DestroyImage(linear_image); |
| 578 | if (paint_image != (Image *) NULL) |
| 579 | linear_image=DestroyImage(paint_image); |
| 580 | return((Image *) NULL); |
| 581 | } |
cristy | 574cc26 | 2011-08-05 01:23:58 +0000 | [diff] [blame] | 582 | if (SetImageStorageClass(paint_image,DirectClass,exception) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 583 | { |
cristy | 404d0da | 2012-12-20 13:04:30 +0000 | [diff] [blame] | 584 | linear_image=DestroyImage(linear_image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 585 | paint_image=DestroyImage(paint_image); |
| 586 | return((Image *) NULL); |
| 587 | } |
| 588 | histograms=AcquireHistogramThreadSet(NumberPaintBins); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 589 | if (histograms == (size_t **) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 590 | { |
cristy | 404d0da | 2012-12-20 13:04:30 +0000 | [diff] [blame] | 591 | linear_image=DestroyImage(linear_image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 592 | paint_image=DestroyImage(paint_image); |
| 593 | ThrowImageException(ResourceLimitError,"MemoryAllocationFailed"); |
| 594 | } |
| 595 | /* |
| 596 | Oil paint image. |
| 597 | */ |
| 598 | status=MagickTrue; |
| 599 | progress=0; |
cristy | 404d0da | 2012-12-20 13:04:30 +0000 | [diff] [blame] | 600 | center=(ssize_t) GetPixelChannels(linear_image)*(linear_image->columns+width)* |
| 601 | (width/2L)+GetPixelChannels(linear_image)*(width/2L); |
| 602 | image_view=AcquireVirtualCacheView(linear_image,exception); |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 603 | paint_view=AcquireAuthenticCacheView(paint_image,exception); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 604 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 605 | #pragma omp parallel for schedule(static,4) shared(progress,status) \ |
cristy | 404d0da | 2012-12-20 13:04:30 +0000 | [diff] [blame] | 606 | magick_threads(linear_image,paint_image,linear_image->rows,1) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 607 | #endif |
cristy | 404d0da | 2012-12-20 13:04:30 +0000 | [diff] [blame] | 608 | for (y=0; y < (ssize_t) linear_image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 609 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 610 | register const Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 611 | *restrict p; |
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 Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 614 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 615 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 616 | register size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 617 | *histogram; |
| 618 | |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 619 | register ssize_t |
| 620 | x; |
| 621 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 622 | if (status == MagickFalse) |
| 623 | continue; |
cristy | fe4ba00 | 2011-02-28 14:54:12 +0000 | [diff] [blame] | 624 | p=GetCacheViewVirtualPixels(image_view,-((ssize_t) width/2L),y-(ssize_t) |
cristy | 404d0da | 2012-12-20 13:04:30 +0000 | [diff] [blame] | 625 | (width/2L),linear_image->columns+width,width,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 626 | q=QueueCacheViewAuthenticPixels(paint_view,0,y,paint_image->columns,1, |
| 627 | exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 628 | if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 629 | { |
| 630 | status=MagickFalse; |
| 631 | continue; |
| 632 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 633 | histogram=histograms[GetOpenMPThreadId()]; |
cristy | 404d0da | 2012-12-20 13:04:30 +0000 | [diff] [blame] | 634 | for (x=0; x < (ssize_t) linear_image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 635 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 636 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 637 | i, |
| 638 | u; |
| 639 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 640 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 641 | count; |
| 642 | |
cristy | 9d314ff | 2011-03-09 01:30:28 +0000 | [diff] [blame] | 643 | ssize_t |
| 644 | j, |
| 645 | k, |
cristy | f856154 | 2012-01-24 00:26:46 +0000 | [diff] [blame] | 646 | n, |
cristy | 9d314ff | 2011-03-09 01:30:28 +0000 | [diff] [blame] | 647 | v; |
| 648 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 649 | /* |
| 650 | Assign most frequent color. |
| 651 | */ |
cristy | f856154 | 2012-01-24 00:26:46 +0000 | [diff] [blame] | 652 | k=0; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 653 | j=0; |
| 654 | count=0; |
cristy | f856154 | 2012-01-24 00:26:46 +0000 | [diff] [blame] | 655 | (void) ResetMagickMemory(histogram,0,NumberPaintBins* sizeof(*histogram)); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 656 | for (v=0; v < (ssize_t) width; v++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 657 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 658 | for (u=0; u < (ssize_t) width; u++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 659 | { |
cristy | 404d0da | 2012-12-20 13:04:30 +0000 | [diff] [blame] | 660 | n=(ssize_t) ScaleQuantumToChar(ClampToQuantum(GetPixelIntensity( |
| 661 | linear_image,p+GetPixelChannels(linear_image)*(u+k)))); |
cristy | f856154 | 2012-01-24 00:26:46 +0000 | [diff] [blame] | 662 | histogram[n]++; |
| 663 | if (histogram[n] > count) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 664 | { |
cristy | f856154 | 2012-01-24 00:26:46 +0000 | [diff] [blame] | 665 | j=k+u; |
| 666 | count=histogram[n]; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 667 | } |
| 668 | } |
cristy | 034ade9 | 2013-03-07 12:18:19 +0000 | [diff] [blame] | 669 | k+=(ssize_t) (linear_image->columns+width); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 670 | } |
cristy | 404d0da | 2012-12-20 13:04:30 +0000 | [diff] [blame] | 671 | for (i=0; i < (ssize_t) GetPixelChannels(linear_image); i++) |
cristy | f856154 | 2012-01-24 00:26:46 +0000 | [diff] [blame] | 672 | { |
cristy | 5a23c55 | 2013-02-13 14:34:28 +0000 | [diff] [blame] | 673 | PixelChannel channel=GetPixelChannelChannel(linear_image,i); |
| 674 | PixelTrait traits=GetPixelChannelTraits(linear_image,channel); |
| 675 | PixelTrait paint_traits=GetPixelChannelTraits(paint_image,channel); |
cristy | f856154 | 2012-01-24 00:26:46 +0000 | [diff] [blame] | 676 | if ((traits == UndefinedPixelTrait) || |
| 677 | (paint_traits == UndefinedPixelTrait)) |
| 678 | continue; |
cristy | 1eced09 | 2012-08-10 23:10:56 +0000 | [diff] [blame] | 679 | if (((paint_traits & CopyPixelTrait) != 0) || |
cristy | 883fde1 | 2013-04-08 00:50:13 +0000 | [diff] [blame] | 680 | (GetPixelReadMask(linear_image,p) == 0)) |
cristy | f856154 | 2012-01-24 00:26:46 +0000 | [diff] [blame] | 681 | { |
| 682 | SetPixelChannel(paint_image,channel,p[center+i],q); |
| 683 | continue; |
| 684 | } |
cristy | 404d0da | 2012-12-20 13:04:30 +0000 | [diff] [blame] | 685 | SetPixelChannel(paint_image,channel,p[j*GetPixelChannels(linear_image)+ |
| 686 | i],q); |
cristy | f856154 | 2012-01-24 00:26:46 +0000 | [diff] [blame] | 687 | } |
cristy | 404d0da | 2012-12-20 13:04:30 +0000 | [diff] [blame] | 688 | p+=GetPixelChannels(linear_image); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 689 | q+=GetPixelChannels(paint_image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 690 | } |
| 691 | if (SyncCacheViewAuthenticPixels(paint_view,exception) == MagickFalse) |
| 692 | status=MagickFalse; |
cristy | 404d0da | 2012-12-20 13:04:30 +0000 | [diff] [blame] | 693 | if (linear_image->progress_monitor != (MagickProgressMonitor) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 694 | { |
| 695 | MagickBooleanType |
| 696 | proceed; |
| 697 | |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 698 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 95338b3 | 2012-01-22 03:34:36 +0000 | [diff] [blame] | 699 | #pragma omp critical (MagickCore_OilPaintImage) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 700 | #endif |
cristy | 404d0da | 2012-12-20 13:04:30 +0000 | [diff] [blame] | 701 | proceed=SetImageProgress(linear_image,OilPaintImageTag,progress++, |
| 702 | linear_image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 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); |
cristy | 404d0da | 2012-12-20 13:04:30 +0000 | [diff] [blame] | 710 | linear_image=DestroyImage(linear_image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 711 | if (status == MagickFalse) |
| 712 | paint_image=DestroyImage(paint_image); |
| 713 | return(paint_image); |
| 714 | } |
| 715 | |
| 716 | /* |
| 717 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 718 | % % |
| 719 | % % |
| 720 | % % |
| 721 | % O p a q u e P a i n t I m a g e % |
| 722 | % % |
| 723 | % % |
| 724 | % % |
| 725 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 726 | % |
| 727 | % OpaquePaintImage() changes any pixel that matches color with the color |
| 728 | % defined by fill. |
| 729 | % |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 730 | % By default color must match a particular pixel color exactly. However, in |
| 731 | % many cases two colors may differ by a small amount. Fuzz defines how much |
| 732 | % tolerance is acceptable to consider two colors as the same. For example, |
| 733 | % set fuzz to 10 and the color red at intensities of 100 and 102 respectively |
| 734 | % are now interpreted as the same color. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 735 | % |
| 736 | % The format of the OpaquePaintImage method is: |
| 737 | % |
| 738 | % MagickBooleanType OpaquePaintImage(Image *image, |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 739 | % const PixelInfo *target,const PixelInfo *fill, |
cristy | 189e84c | 2011-08-27 18:08:53 +0000 | [diff] [blame] | 740 | % const MagickBooleanType invert,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 741 | % |
| 742 | % A description of each parameter follows: |
| 743 | % |
| 744 | % o image: the image. |
| 745 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 746 | % o target: the RGB value of the target color. |
| 747 | % |
| 748 | % o fill: the replacement color. |
| 749 | % |
| 750 | % o invert: paint any pixel that does not match the target color. |
| 751 | % |
cristy | 189e84c | 2011-08-27 18:08:53 +0000 | [diff] [blame] | 752 | % o exception: return any errors or warnings in this structure. |
| 753 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 754 | */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 755 | MagickExport MagickBooleanType OpaquePaintImage(Image *image, |
cristy | 189e84c | 2011-08-27 18:08:53 +0000 | [diff] [blame] | 756 | const PixelInfo *target,const PixelInfo *fill,const MagickBooleanType invert, |
| 757 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 758 | { |
| 759 | #define OpaquePaintImageTag "Opaque/Image" |
| 760 | |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 761 | CacheView |
| 762 | *image_view; |
| 763 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 764 | MagickBooleanType |
| 765 | status; |
| 766 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 767 | MagickOffsetType |
| 768 | progress; |
| 769 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 770 | PixelInfo |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 771 | zero; |
| 772 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 773 | ssize_t |
| 774 | y; |
| 775 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 776 | assert(image != (Image *) NULL); |
| 777 | assert(image->signature == MagickSignature); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 778 | assert(target != (PixelInfo *) NULL); |
| 779 | assert(fill != (PixelInfo *) NULL); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 780 | if (image->debug != MagickFalse) |
| 781 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | 574cc26 | 2011-08-05 01:23:58 +0000 | [diff] [blame] | 782 | if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 783 | return(MagickFalse); |
cristy | a6400b1 | 2013-03-15 12:20:18 +0000 | [diff] [blame] | 784 | if ((IsGrayColorspace(image->colorspace) != MagickFalse) && |
| 785 | (IsPixelInfoGray(fill) == MagickFalse)) |
cristy | 0c81d06 | 2013-04-21 15:22:02 +0000 | [diff] [blame] | 786 | (void) SetImageColorspace(image,sRGBColorspace,exception); |
cristy | 400275a | 2013-03-13 00:34:24 +0000 | [diff] [blame] | 787 | if ((fill->alpha_trait == BlendPixelTrait) && |
| 788 | (image->alpha_trait != BlendPixelTrait)) |
cristy | 0925019 | 2012-02-07 18:53:31 +0000 | [diff] [blame] | 789 | (void) SetImageAlpha(image,OpaqueAlpha,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 790 | /* |
| 791 | Make image color opaque. |
| 792 | */ |
| 793 | status=MagickTrue; |
| 794 | progress=0; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 795 | GetPixelInfo(image,&zero); |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 796 | image_view=AcquireAuthenticCacheView(image,exception); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 797 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 798 | #pragma omp parallel for schedule(static,4) shared(progress,status) \ |
cristy | 5e6b259 | 2012-12-19 14:08:11 +0000 | [diff] [blame] | 799 | magick_threads(image,image,image->rows,1) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 800 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 801 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 802 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 803 | PixelInfo |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 804 | pixel; |
| 805 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 806 | register Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 807 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 808 | |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 809 | register ssize_t |
| 810 | x; |
| 811 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 812 | if (status == MagickFalse) |
| 813 | continue; |
| 814 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 815 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 816 | { |
| 817 | status=MagickFalse; |
| 818 | continue; |
| 819 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 820 | pixel=zero; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 821 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 822 | { |
cristy | 803640d | 2011-11-17 02:11:32 +0000 | [diff] [blame] | 823 | GetPixelInfoPixel(image,q,&pixel); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 824 | if (IsFuzzyEquivalencePixelInfo(&pixel,target) != invert) |
cristy | 95f562a | 2012-01-01 20:49:11 +0000 | [diff] [blame] | 825 | SetPixelInfoPixel(image,fill,q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 826 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 827 | } |
| 828 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
| 829 | status=MagickFalse; |
| 830 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 831 | { |
| 832 | MagickBooleanType |
| 833 | proceed; |
| 834 | |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 835 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 95338b3 | 2012-01-22 03:34:36 +0000 | [diff] [blame] | 836 | #pragma omp critical (MagickCore_OpaquePaintImage) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 837 | #endif |
| 838 | proceed=SetImageProgress(image,OpaquePaintImageTag,progress++, |
| 839 | image->rows); |
| 840 | if (proceed == MagickFalse) |
| 841 | status=MagickFalse; |
| 842 | } |
| 843 | } |
| 844 | image_view=DestroyCacheView(image_view); |
| 845 | return(status); |
| 846 | } |
| 847 | |
| 848 | /* |
| 849 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 850 | % % |
| 851 | % % |
| 852 | % % |
| 853 | % T r a n s p a r e n t P a i n t I m a g e % |
| 854 | % % |
| 855 | % % |
| 856 | % % |
| 857 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 858 | % |
| 859 | % TransparentPaintImage() changes the opacity value associated with any pixel |
| 860 | % that matches color to the value defined by opacity. |
| 861 | % |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 862 | % By default color must match a particular pixel color exactly. However, in |
| 863 | % many cases two colors may differ by a small amount. Fuzz defines how much |
| 864 | % tolerance is acceptable to consider two colors as the same. For example, |
| 865 | % set fuzz to 10 and the color red at intensities of 100 and 102 respectively |
| 866 | % are now interpreted as the same color. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 867 | % |
| 868 | % The format of the TransparentPaintImage method is: |
| 869 | % |
| 870 | % MagickBooleanType TransparentPaintImage(Image *image, |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 871 | % const PixelInfo *target,const Quantum opacity, |
cristy | 189e84c | 2011-08-27 18:08:53 +0000 | [diff] [blame] | 872 | % const MagickBooleanType invert,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 873 | % |
| 874 | % A description of each parameter follows: |
| 875 | % |
| 876 | % o image: the image. |
| 877 | % |
| 878 | % o target: the target color. |
| 879 | % |
| 880 | % o opacity: the replacement opacity value. |
| 881 | % |
| 882 | % o invert: paint any pixel that does not match the target color. |
| 883 | % |
cristy | 189e84c | 2011-08-27 18:08:53 +0000 | [diff] [blame] | 884 | % o exception: return any errors or warnings in this structure. |
| 885 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 886 | */ |
| 887 | MagickExport MagickBooleanType TransparentPaintImage(Image *image, |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 888 | const PixelInfo *target,const Quantum opacity,const MagickBooleanType invert, |
| 889 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 890 | { |
| 891 | #define TransparentPaintImageTag "Transparent/Image" |
| 892 | |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 893 | CacheView |
| 894 | *image_view; |
| 895 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 896 | MagickBooleanType |
| 897 | status; |
| 898 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 899 | MagickOffsetType |
| 900 | progress; |
| 901 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 902 | PixelInfo |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 903 | zero; |
| 904 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 905 | ssize_t |
| 906 | y; |
| 907 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 908 | assert(image != (Image *) NULL); |
| 909 | assert(image->signature == MagickSignature); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 910 | assert(target != (PixelInfo *) NULL); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 911 | if (image->debug != MagickFalse) |
| 912 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | 574cc26 | 2011-08-05 01:23:58 +0000 | [diff] [blame] | 913 | if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 914 | return(MagickFalse); |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 915 | if (image->alpha_trait != BlendPixelTrait) |
cristy | 6324088 | 2011-08-05 19:05:27 +0000 | [diff] [blame] | 916 | (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 917 | /* |
| 918 | Make image color transparent. |
| 919 | */ |
| 920 | status=MagickTrue; |
| 921 | progress=0; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 922 | GetPixelInfo(image,&zero); |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 923 | image_view=AcquireAuthenticCacheView(image,exception); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 924 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 925 | #pragma omp parallel for schedule(static,4) shared(progress,status) \ |
cristy | 5e6b259 | 2012-12-19 14:08:11 +0000 | [diff] [blame] | 926 | magick_threads(image,image,image->rows,1) |
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); |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 1043 | if (image->alpha_trait != BlendPixelTrait) |
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 | 46ff267 | 2012-12-14 15:32:26 +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) \ |
cristy | 5e6b259 | 2012-12-19 14:08:11 +0000 | [diff] [blame] | 1053 | magick_threads(image,image,image->rows,1) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1054 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1055 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1056 | { |
| 1057 | MagickBooleanType |
| 1058 | match; |
| 1059 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1060 | PixelInfo |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1061 | pixel; |
| 1062 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1063 | register Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1064 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1065 | |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 1066 | register ssize_t |
| 1067 | x; |
| 1068 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1069 | if (status == MagickFalse) |
| 1070 | continue; |
| 1071 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 1072 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1073 | { |
| 1074 | status=MagickFalse; |
| 1075 | continue; |
| 1076 | } |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1077 | GetPixelInfo(image,&pixel); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1078 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1079 | { |
cristy | 803640d | 2011-11-17 02:11:32 +0000 | [diff] [blame] | 1080 | GetPixelInfoPixel(image,q,&pixel); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1081 | match=((pixel.red >= low->red) && (pixel.red <= high->red) && |
| 1082 | (pixel.green >= low->green) && (pixel.green <= high->green) && |
cristy | 14973ba | 2011-08-27 23:48:07 +0000 | [diff] [blame] | 1083 | (pixel.blue >= low->blue) && (pixel.blue <= high->blue)) ? MagickTrue : |
| 1084 | MagickFalse; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1085 | if (match != invert) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1086 | SetPixelAlpha(image,opacity,q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1087 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1088 | } |
| 1089 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
| 1090 | status=MagickFalse; |
| 1091 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 1092 | { |
| 1093 | MagickBooleanType |
| 1094 | proceed; |
| 1095 | |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 1096 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 95338b3 | 2012-01-22 03:34:36 +0000 | [diff] [blame] | 1097 | #pragma omp critical (MagickCore_TransparentPaintImageChroma) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1098 | #endif |
| 1099 | proceed=SetImageProgress(image,TransparentPaintImageTag,progress++, |
| 1100 | image->rows); |
| 1101 | if (proceed == MagickFalse) |
| 1102 | status=MagickFalse; |
| 1103 | } |
| 1104 | } |
| 1105 | image_view=DestroyCacheView(image_view); |
| 1106 | return(status); |
| 1107 | } |