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