cristy | d04e7bf | 2012-03-03 19:19:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % % |
| 6 | % CCCC H H AAA N N N N EEEEE L % |
| 7 | % C H H A A NN N NN N E L % |
| 8 | % C HHHHH AAAAA N N N N N N RRR L % |
| 9 | % C H H A A N NN N NN E L % |
| 10 | % CCCC H H A A N N N N EEEEE LLLLL % |
| 11 | % % |
| 12 | % % |
| 13 | % MagickCore Image Channel Methods % |
| 14 | % % |
| 15 | % Software Design % |
| 16 | % John Cristy % |
| 17 | % December 2003 % |
| 18 | % % |
| 19 | % % |
cristy | fe676ee | 2013-11-18 13:03:38 +0000 | [diff] [blame] | 20 | % Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization % |
cristy | d04e7bf | 2012-03-03 19:19:12 +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 | /* |
| 41 | Include declarations. |
| 42 | */ |
| 43 | #include "MagickCore/studio.h" |
cristy | 8c8c916 | 2013-05-27 10:14:21 +0000 | [diff] [blame] | 44 | #include "MagickCore/cache-private.h" |
cristy | c1119af | 2012-04-16 15:28:23 +0000 | [diff] [blame] | 45 | #include "MagickCore/colorspace-private.h" |
cristy | 8c8c916 | 2013-05-27 10:14:21 +0000 | [diff] [blame] | 46 | #include "MagickCore/composite-private.h" |
| 47 | #include "MagickCore/enhance.h" |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 48 | #include "MagickCore/image.h" |
| 49 | #include "MagickCore/list.h" |
| 50 | #include "MagickCore/log.h" |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 51 | #include "MagickCore/monitor.h" |
| 52 | #include "MagickCore/monitor-private.h" |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 53 | #include "MagickCore/option.h" |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 54 | #include "MagickCore/pixel-accessor.h" |
cristy | 8c8c916 | 2013-05-27 10:14:21 +0000 | [diff] [blame] | 55 | #include "MagickCore/pixel-private.h" |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 56 | #include "MagickCore/resource_.h" |
cristy | 8748f56 | 2012-03-08 13:58:34 +0000 | [diff] [blame] | 57 | #include "MagickCore/string-private.h" |
cristy | 16881e6 | 2012-05-06 14:41:29 +0000 | [diff] [blame] | 58 | #include "MagickCore/thread-private.h" |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 59 | #include "MagickCore/token.h" |
cristy | d04e7bf | 2012-03-03 19:19:12 +0000 | [diff] [blame] | 60 | #include "MagickCore/utility.h" |
| 61 | #include "MagickCore/version.h" |
| 62 | |
| 63 | /* |
| 64 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 65 | % % |
| 66 | % % |
| 67 | % % |
cristy | 5f257b2 | 2012-03-07 00:27:29 +0000 | [diff] [blame] | 68 | % C h a n n e l F x I m a g e % |
cristy | d04e7bf | 2012-03-03 19:19:12 +0000 | [diff] [blame] | 69 | % % |
| 70 | % % |
| 71 | % % |
| 72 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 73 | % |
cristy | 5f257b2 | 2012-03-07 00:27:29 +0000 | [diff] [blame] | 74 | % ChannelFxImage() applies a channel expression to the specified image. The |
| 75 | % expression consists of one or more channels, either mnemonic or numeric (e.g. |
| 76 | % red, 1), separated by actions as follows: |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 77 | % |
| 78 | % <=> exchange two channels (e.g. red<=>blue) |
cristy | 7ea920d | 2012-03-16 01:08:01 +0000 | [diff] [blame] | 79 | % => copy one channel to another channel (e.g. red=>green) |
| 80 | % = assign a constant value to a channel (e.g. red=50%) |
| 81 | % , write new image channels in the specified order (e.g. red, green) |
cristy | 3c21361 | 2012-03-16 01:14:48 +0000 | [diff] [blame] | 82 | % | add a new output image for the next set of channel operations |
| 83 | % ; move to the next input image for the source of channel data |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 84 | % |
cristy | 7ea920d | 2012-03-16 01:08:01 +0000 | [diff] [blame] | 85 | % For example, to create 3 grayscale images from the red, green, and blue |
| 86 | % channels of an image, use: |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 87 | % |
cristy | 3c21361 | 2012-03-16 01:14:48 +0000 | [diff] [blame] | 88 | % -channel-fx "red; green; blue" |
cristy | 7ea920d | 2012-03-16 01:08:01 +0000 | [diff] [blame] | 89 | % |
| 90 | % A channel without an operation symbol implies separate (i.e, semicolon). |
cristy | d04e7bf | 2012-03-03 19:19:12 +0000 | [diff] [blame] | 91 | % |
cristy | 5f257b2 | 2012-03-07 00:27:29 +0000 | [diff] [blame] | 92 | % The format of the ChannelFxImage method is: |
cristy | d04e7bf | 2012-03-03 19:19:12 +0000 | [diff] [blame] | 93 | % |
cristy | 5f257b2 | 2012-03-07 00:27:29 +0000 | [diff] [blame] | 94 | % Image *ChannelFxImage(const Image *image,const char *expression, |
| 95 | % ExceptionInfo *exception) |
cristy | d04e7bf | 2012-03-03 19:19:12 +0000 | [diff] [blame] | 96 | % |
| 97 | % A description of each parameter follows: |
| 98 | % |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 99 | % o image: the image. |
cristy | d04e7bf | 2012-03-03 19:19:12 +0000 | [diff] [blame] | 100 | % |
| 101 | % o expression: A channel expression. |
| 102 | % |
| 103 | % o exception: return any errors or warnings in this structure. |
| 104 | % |
| 105 | */ |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 106 | |
| 107 | typedef enum |
| 108 | { |
| 109 | ExtractChannelOp, |
cristy | 8748f56 | 2012-03-08 13:58:34 +0000 | [diff] [blame] | 110 | AssignChannelOp, |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 111 | ExchangeChannelOp, |
| 112 | TransferChannelOp |
cristy | 5f257b2 | 2012-03-07 00:27:29 +0000 | [diff] [blame] | 113 | } ChannelFx; |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 114 | |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 115 | static inline size_t MagickMin(const size_t x,const size_t y) |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 116 | { |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 117 | if (x < y) |
| 118 | return(x); |
| 119 | return(y); |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 120 | } |
| 121 | |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 122 | static MagickBooleanType ChannelImage(Image *destination_image, |
cristy | 8748f56 | 2012-03-08 13:58:34 +0000 | [diff] [blame] | 123 | const PixelChannel destination_channel,const ChannelFx channel_op, |
cristy | 3bb378a | 2012-03-08 14:38:52 +0000 | [diff] [blame] | 124 | const Image *source_image,const PixelChannel source_channel, |
cristy | 8748f56 | 2012-03-08 13:58:34 +0000 | [diff] [blame] | 125 | const Quantum pixel,ExceptionInfo *exception) |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 126 | { |
| 127 | CacheView |
| 128 | *source_view, |
| 129 | *destination_view; |
| 130 | |
| 131 | MagickBooleanType |
| 132 | status; |
| 133 | |
| 134 | size_t |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 135 | height, |
| 136 | width; |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 137 | |
| 138 | ssize_t |
| 139 | y; |
| 140 | |
| 141 | status=MagickTrue; |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 142 | source_view=AcquireVirtualCacheView(source_image,exception); |
| 143 | destination_view=AcquireAuthenticCacheView(destination_image,exception); |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 144 | height=MagickMin(source_image->rows,destination_image->rows); |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 145 | width=MagickMin(source_image->columns,destination_image->columns); |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 146 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 9a5a52f | 2012-10-09 14:40:31 +0000 | [diff] [blame] | 147 | #pragma omp parallel for schedule(static,4) shared(status) \ |
cristy | 5e6b259 | 2012-12-19 14:08:11 +0000 | [diff] [blame] | 148 | magick_threads(source_image,source_image,height,1) |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 149 | #endif |
| 150 | for (y=0; y < (ssize_t) height; y++) |
| 151 | { |
cristy | 4ee6040 | 2012-03-11 02:01:20 +0000 | [diff] [blame] | 152 | PixelTrait |
| 153 | destination_traits, |
| 154 | source_traits; |
| 155 | |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 156 | register const Quantum |
| 157 | *restrict p; |
| 158 | |
| 159 | register Quantum |
| 160 | *restrict q; |
| 161 | |
| 162 | register ssize_t |
| 163 | x; |
| 164 | |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 165 | if (status == MagickFalse) |
| 166 | continue; |
| 167 | p=GetCacheViewVirtualPixels(source_view,0,y,source_image->columns,1, |
| 168 | exception); |
cristy | 4ee6040 | 2012-03-11 02:01:20 +0000 | [diff] [blame] | 169 | q=GetCacheViewAuthenticPixels(destination_view,0,y, |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 170 | destination_image->columns,1,exception); |
| 171 | if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL)) |
| 172 | { |
| 173 | status=MagickFalse; |
| 174 | continue; |
| 175 | } |
cristy | cf1296e | 2012-08-26 23:40:49 +0000 | [diff] [blame] | 176 | destination_traits=GetPixelChannelTraits(destination_image, |
cristy | 4ee6040 | 2012-03-11 02:01:20 +0000 | [diff] [blame] | 177 | destination_channel); |
cristy | cf1296e | 2012-08-26 23:40:49 +0000 | [diff] [blame] | 178 | source_traits=GetPixelChannelTraits(source_image,source_channel); |
cristy | 4ee6040 | 2012-03-11 02:01:20 +0000 | [diff] [blame] | 179 | if ((destination_traits == UndefinedPixelTrait) || |
| 180 | (source_traits == UndefinedPixelTrait)) |
| 181 | continue; |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 182 | for (x=0; x < (ssize_t) width; x++) |
| 183 | { |
cristy | 8748f56 | 2012-03-08 13:58:34 +0000 | [diff] [blame] | 184 | if (channel_op == AssignChannelOp) |
| 185 | SetPixelChannel(destination_image,destination_channel,pixel,q); |
| 186 | else |
cristy | 4ee6040 | 2012-03-11 02:01:20 +0000 | [diff] [blame] | 187 | SetPixelChannel(destination_image,destination_channel, |
| 188 | GetPixelChannel(source_image,source_channel,p),q); |
| 189 | p+=GetPixelChannels(source_image); |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 190 | q+=GetPixelChannels(destination_image); |
| 191 | } |
| 192 | if (SyncCacheViewAuthenticPixels(destination_view,exception) == MagickFalse) |
| 193 | status=MagickFalse; |
| 194 | } |
| 195 | destination_view=DestroyCacheView(destination_view); |
| 196 | source_view=DestroyCacheView(source_view); |
| 197 | return(status); |
| 198 | } |
| 199 | |
cristy | 6398ec7 | 2013-11-28 02:00:27 +0000 | [diff] [blame^] | 200 | #if defined(__cplusplus) || defined(c_plusplus) |
| 201 | extern "C" { |
| 202 | #endif |
| 203 | |
cristy | 5f257b2 | 2012-03-07 00:27:29 +0000 | [diff] [blame] | 204 | MagickExport Image *ChannelFxImage(const Image *image,const char *expression, |
| 205 | ExceptionInfo *exception) |
cristy | d04e7bf | 2012-03-03 19:19:12 +0000 | [diff] [blame] | 206 | { |
cristy | 5f257b2 | 2012-03-07 00:27:29 +0000 | [diff] [blame] | 207 | #define ChannelFxImageTag "ChannelFx/Image" |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 208 | |
cristy | 5f257b2 | 2012-03-07 00:27:29 +0000 | [diff] [blame] | 209 | ChannelFx |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 210 | channel_op; |
| 211 | |
cristy | fe88ede | 2012-03-22 00:34:48 +0000 | [diff] [blame] | 212 | ChannelType |
| 213 | channel_mask; |
| 214 | |
cristy | 4ee6040 | 2012-03-11 02:01:20 +0000 | [diff] [blame] | 215 | char |
| 216 | token[MaxTextExtent]; |
| 217 | |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 218 | const char |
| 219 | *p; |
| 220 | |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 221 | const Image |
| 222 | *source_image; |
| 223 | |
cristy | 8748f56 | 2012-03-08 13:58:34 +0000 | [diff] [blame] | 224 | double |
| 225 | pixel; |
| 226 | |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 227 | Image |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 228 | *destination_image; |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 229 | |
cristy | 4ee6040 | 2012-03-11 02:01:20 +0000 | [diff] [blame] | 230 | MagickBooleanType |
| 231 | status; |
| 232 | |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 233 | PixelChannel |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 234 | source_channel, |
| 235 | destination_channel; |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 236 | |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 237 | ssize_t |
| 238 | channels; |
| 239 | |
| 240 | assert(image != (Image *) NULL); |
| 241 | assert(image->signature == MagickSignature); |
| 242 | if (image->debug != MagickFalse) |
| 243 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 244 | assert(exception != (ExceptionInfo *) NULL); |
| 245 | assert(exception->signature == MagickSignature); |
| 246 | source_image=image; |
| 247 | destination_image=CloneImage(source_image,0,0,MagickTrue,exception); |
| 248 | if (destination_image == (Image *) NULL) |
| 249 | return((Image *) NULL); |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 250 | if (expression == (const char *) NULL) |
| 251 | return(destination_image); |
| 252 | destination_channel=RedPixelChannel; |
cristy | fe88ede | 2012-03-22 00:34:48 +0000 | [diff] [blame] | 253 | channel_mask=UndefinedChannel; |
cristy | 8748f56 | 2012-03-08 13:58:34 +0000 | [diff] [blame] | 254 | pixel=0.0; |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 255 | p=(char *) expression; |
| 256 | GetMagickToken(p,&p,token); |
cristy | 4ee6040 | 2012-03-11 02:01:20 +0000 | [diff] [blame] | 257 | channel_op=ExtractChannelOp; |
cristy | 09ef5be | 2012-03-13 13:01:27 +0000 | [diff] [blame] | 258 | for (channels=0; *token != '\0'; ) |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 259 | { |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 260 | ssize_t |
| 261 | i; |
| 262 | |
| 263 | /* |
| 264 | Interpret channel expression. |
| 265 | */ |
cristy | 68bc257 | 2013-05-16 01:09:13 +0000 | [diff] [blame] | 266 | switch (*token) |
| 267 | { |
cristy | 68bc257 | 2013-05-16 01:09:13 +0000 | [diff] [blame] | 268 | case ',': |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 269 | { |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 270 | GetMagickToken(p,&p,token); |
cristy | 68bc257 | 2013-05-16 01:09:13 +0000 | [diff] [blame] | 271 | break; |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 272 | } |
cristy | 68bc257 | 2013-05-16 01:09:13 +0000 | [diff] [blame] | 273 | case '|': |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 274 | { |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 275 | if (GetNextImageInList(source_image) != (Image *) NULL) |
| 276 | source_image=GetNextImageInList(source_image); |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 277 | else |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 278 | source_image=GetFirstImageInList(source_image); |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 279 | GetMagickToken(p,&p,token); |
cristy | 68bc257 | 2013-05-16 01:09:13 +0000 | [diff] [blame] | 280 | break; |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 281 | } |
cristy | 68bc257 | 2013-05-16 01:09:13 +0000 | [diff] [blame] | 282 | case ';': |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 283 | { |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 284 | Image |
| 285 | *canvas; |
| 286 | |
cristy | cf1296e | 2012-08-26 23:40:49 +0000 | [diff] [blame] | 287 | SetPixelChannelMask(destination_image,channel_mask); |
cristy | 68bc257 | 2013-05-16 01:09:13 +0000 | [diff] [blame] | 288 | if ((channel_op == ExtractChannelOp) && (channels == 1)) |
cristy | fe88ede | 2012-03-22 00:34:48 +0000 | [diff] [blame] | 289 | (void) SetImageColorspace(destination_image,GRAYColorspace,exception); |
cristy | 4ee6040 | 2012-03-11 02:01:20 +0000 | [diff] [blame] | 290 | status=SetImageStorageClass(destination_image,DirectClass,exception); |
| 291 | if (status == MagickFalse) |
| 292 | { |
cristy | fc68ef5 | 2012-03-11 23:33:15 +0000 | [diff] [blame] | 293 | destination_image=DestroyImageList(destination_image); |
| 294 | return(destination_image); |
cristy | 4ee6040 | 2012-03-11 02:01:20 +0000 | [diff] [blame] | 295 | } |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 296 | canvas=CloneImage(source_image,0,0,MagickTrue,exception); |
| 297 | if (canvas == (Image *) NULL) |
| 298 | { |
cristy | fc68ef5 | 2012-03-11 23:33:15 +0000 | [diff] [blame] | 299 | destination_image=DestroyImageList(destination_image); |
| 300 | return(destination_image); |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 301 | } |
| 302 | AppendImageToList(&destination_image,canvas); |
| 303 | destination_image=GetLastImageInList(destination_image); |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 304 | GetMagickToken(p,&p,token); |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 305 | channels=0; |
| 306 | destination_channel=RedPixelChannel; |
cristy | fe88ede | 2012-03-22 00:34:48 +0000 | [diff] [blame] | 307 | channel_mask=UndefinedChannel; |
cristy | 68bc257 | 2013-05-16 01:09:13 +0000 | [diff] [blame] | 308 | break; |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 309 | } |
cristy | 68bc257 | 2013-05-16 01:09:13 +0000 | [diff] [blame] | 310 | default: |
| 311 | break; |
| 312 | } |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 313 | i=ParsePixelChannelOption(token); |
| 314 | if (i < 0) |
| 315 | { |
| 316 | (void) ThrowMagickException(exception,GetMagickModule(),OptionError, |
cristy | efe601c | 2013-01-05 17:51:12 +0000 | [diff] [blame] | 317 | "UnrecognizedChannelType","`%s'",token); |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 318 | destination_image=DestroyImageList(destination_image); |
cristy | fc68ef5 | 2012-03-11 23:33:15 +0000 | [diff] [blame] | 319 | return(destination_image); |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 320 | } |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 321 | source_channel=(PixelChannel) i; |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 322 | channel_op=ExtractChannelOp; |
| 323 | GetMagickToken(p,&p,token); |
| 324 | if (*token == '<') |
| 325 | { |
| 326 | channel_op=ExchangeChannelOp; |
| 327 | GetMagickToken(p,&p,token); |
| 328 | } |
| 329 | if (*token == '=') |
cristy | 8748f56 | 2012-03-08 13:58:34 +0000 | [diff] [blame] | 330 | { |
| 331 | if (channel_op != ExchangeChannelOp) |
| 332 | channel_op=AssignChannelOp; |
| 333 | GetMagickToken(p,&p,token); |
| 334 | } |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 335 | if (*token == '>') |
| 336 | { |
| 337 | if (channel_op != ExchangeChannelOp) |
| 338 | channel_op=TransferChannelOp; |
| 339 | GetMagickToken(p,&p,token); |
| 340 | } |
cristy | 8748f56 | 2012-03-08 13:58:34 +0000 | [diff] [blame] | 341 | switch (channel_op) |
| 342 | { |
| 343 | case AssignChannelOp: |
| 344 | { |
cristy | 41db174 | 2012-03-09 00:57:52 +0000 | [diff] [blame] | 345 | pixel=StringToDoubleInterval(token,(double) QuantumRange+1.0); |
cristy | 3bb378a | 2012-03-08 14:38:52 +0000 | [diff] [blame] | 346 | GetMagickToken(p,&p,token); |
cristy | 8748f56 | 2012-03-08 13:58:34 +0000 | [diff] [blame] | 347 | break; |
| 348 | } |
| 349 | case ExchangeChannelOp: |
| 350 | case TransferChannelOp: |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 351 | { |
| 352 | i=ParsePixelChannelOption(token); |
| 353 | if (i < 0) |
| 354 | { |
| 355 | (void) ThrowMagickException(exception,GetMagickModule(),OptionError, |
cristy | efe601c | 2013-01-05 17:51:12 +0000 | [diff] [blame] | 356 | "UnrecognizedChannelType","`%s'",token); |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 357 | destination_image=DestroyImageList(destination_image); |
cristy | fc68ef5 | 2012-03-11 23:33:15 +0000 | [diff] [blame] | 358 | return(destination_image); |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 359 | } |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 360 | destination_channel=(PixelChannel) i; |
cristy | 0481714 | 2013-05-20 17:14:03 +0000 | [diff] [blame] | 361 | switch (destination_channel) |
| 362 | { |
cristy | ed27bb7 | 2013-05-20 17:24:07 +0000 | [diff] [blame] | 363 | case RedPixelChannel: |
| 364 | case GreenPixelChannel: |
| 365 | case BluePixelChannel: |
| 366 | case BlackPixelChannel: |
| 367 | case IndexPixelChannel: |
| 368 | break; |
cristy | 0481714 | 2013-05-20 17:14:03 +0000 | [diff] [blame] | 369 | case AlphaPixelChannel: |
| 370 | { |
| 371 | destination_image->alpha_trait=BlendPixelTrait; |
| 372 | break; |
| 373 | } |
| 374 | case ReadMaskPixelChannel: |
| 375 | { |
| 376 | destination_image->read_mask=MagickTrue; |
| 377 | break; |
| 378 | } |
| 379 | case WriteMaskPixelChannel: |
| 380 | { |
| 381 | destination_image->write_mask=MagickTrue; |
| 382 | break; |
| 383 | } |
cristy | ed27bb7 | 2013-05-20 17:24:07 +0000 | [diff] [blame] | 384 | case MetaPixelChannel: |
cristy | 0481714 | 2013-05-20 17:14:03 +0000 | [diff] [blame] | 385 | default: |
cristy | ed27bb7 | 2013-05-20 17:24:07 +0000 | [diff] [blame] | 386 | { |
| 387 | (void) SetPixelMetaChannels(destination_image,(size_t) (i- |
| 388 | GetPixelChannels(destination_image)+1),exception); |
cristy | 0481714 | 2013-05-20 17:14:03 +0000 | [diff] [blame] | 389 | break; |
cristy | ed27bb7 | 2013-05-20 17:24:07 +0000 | [diff] [blame] | 390 | } |
cristy | 0481714 | 2013-05-20 17:14:03 +0000 | [diff] [blame] | 391 | } |
cristy | aa2c16c | 2012-03-25 22:21:35 +0000 | [diff] [blame] | 392 | channel_mask=(ChannelType) (channel_mask | ParseChannelOption(token)); |
cristy | 68bc257 | 2013-05-16 01:09:13 +0000 | [diff] [blame] | 393 | if (((channels >= 1) || (destination_channel >= 1)) && |
| 394 | (IsGrayColorspace(destination_image->colorspace) != MagickFalse)) |
| 395 | (void) SetImageColorspace(destination_image,sRGBColorspace,exception); |
cristy | 3bb378a | 2012-03-08 14:38:52 +0000 | [diff] [blame] | 396 | GetMagickToken(p,&p,token); |
cristy | 8748f56 | 2012-03-08 13:58:34 +0000 | [diff] [blame] | 397 | break; |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 398 | } |
cristy | fe88ede | 2012-03-22 00:34:48 +0000 | [diff] [blame] | 399 | default: |
cristy | 3c21361 | 2012-03-16 01:14:48 +0000 | [diff] [blame] | 400 | break; |
cristy | 7ea920d | 2012-03-16 01:08:01 +0000 | [diff] [blame] | 401 | } |
cristy | 3bb378a | 2012-03-08 14:38:52 +0000 | [diff] [blame] | 402 | status=ChannelImage(destination_image,destination_channel,channel_op, |
| 403 | source_image,source_channel,ClampToQuantum(pixel),exception); |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 404 | if (status == MagickFalse) |
| 405 | { |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 406 | destination_image=DestroyImageList(destination_image); |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 407 | break; |
| 408 | } |
cristy | 4ee6040 | 2012-03-11 02:01:20 +0000 | [diff] [blame] | 409 | channels++; |
cristy | 8748f56 | 2012-03-08 13:58:34 +0000 | [diff] [blame] | 410 | if (channel_op == ExchangeChannelOp) |
| 411 | { |
cristy | 4ee6040 | 2012-03-11 02:01:20 +0000 | [diff] [blame] | 412 | status=ChannelImage(destination_image,source_channel,channel_op, |
| 413 | source_image,destination_channel,ClampToQuantum(pixel),exception); |
cristy | 8748f56 | 2012-03-08 13:58:34 +0000 | [diff] [blame] | 414 | if (status == MagickFalse) |
| 415 | { |
| 416 | destination_image=DestroyImageList(destination_image); |
| 417 | break; |
| 418 | } |
cristy | 4ee6040 | 2012-03-11 02:01:20 +0000 | [diff] [blame] | 419 | channels++; |
cristy | 8748f56 | 2012-03-08 13:58:34 +0000 | [diff] [blame] | 420 | } |
cristy | fe88ede | 2012-03-22 00:34:48 +0000 | [diff] [blame] | 421 | switch (channel_op) |
| 422 | { |
| 423 | case ExtractChannelOp: |
| 424 | { |
cristy | 9dbae2c | 2013-05-27 20:12:53 +0000 | [diff] [blame] | 425 | channel_mask=(ChannelType) (channel_mask | (1 << destination_channel)); |
cristy | aa2c16c | 2012-03-25 22:21:35 +0000 | [diff] [blame] | 426 | destination_channel=(PixelChannel) (destination_channel+1); |
cristy | fe88ede | 2012-03-22 00:34:48 +0000 | [diff] [blame] | 427 | break; |
| 428 | } |
| 429 | default: |
| 430 | break; |
| 431 | } |
cristy | 5f257b2 | 2012-03-07 00:27:29 +0000 | [diff] [blame] | 432 | status=SetImageProgress(source_image,ChannelFxImageTag,p-expression, |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 433 | strlen(expression)); |
| 434 | if (status == MagickFalse) |
| 435 | break; |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 436 | } |
cristy | cf1296e | 2012-08-26 23:40:49 +0000 | [diff] [blame] | 437 | SetPixelChannelMask(destination_image,channel_mask); |
cristy | 68bc257 | 2013-05-16 01:09:13 +0000 | [diff] [blame] | 438 | if ((channel_op == ExtractChannelOp) && (channels == 1)) |
cristy | fe88ede | 2012-03-22 00:34:48 +0000 | [diff] [blame] | 439 | (void) SetImageColorspace(destination_image,GRAYColorspace,exception); |
cristy | 4ee6040 | 2012-03-11 02:01:20 +0000 | [diff] [blame] | 440 | status=SetImageStorageClass(destination_image,DirectClass,exception); |
| 441 | if (status == MagickFalse) |
| 442 | { |
| 443 | destination_image=GetLastImageInList(destination_image); |
| 444 | return((Image *) NULL); |
| 445 | } |
cristy | 09ef5be | 2012-03-13 13:01:27 +0000 | [diff] [blame] | 446 | return(GetFirstImageInList(destination_image)); |
cristy | d04e7bf | 2012-03-03 19:19:12 +0000 | [diff] [blame] | 447 | } |
cristy | 6398ec7 | 2013-11-28 02:00:27 +0000 | [diff] [blame^] | 448 | |
| 449 | #if defined(__cplusplus) || defined(c_plusplus) |
| 450 | } |
| 451 | #endif |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 452 | |
| 453 | /* |
| 454 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 455 | % % |
| 456 | % % |
| 457 | % % |
| 458 | % C o m b i n e I m a g e s % |
| 459 | % % |
| 460 | % % |
| 461 | % % |
| 462 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 463 | % |
| 464 | % CombineImages() combines one or more images into a single image. The |
| 465 | % grayscale value of the pixels of each image in the sequence is assigned in |
| 466 | % order to the specified channels of the combined image. The typical |
| 467 | % ordering would be image 1 => Red, 2 => Green, 3 => Blue, etc. |
| 468 | % |
| 469 | % The format of the CombineImages method is: |
| 470 | % |
cristy | 46f354c | 2012-07-04 13:31:29 +0000 | [diff] [blame] | 471 | % Image *CombineImages(const Image *images,const ColorspaceType colorspace, |
| 472 | % ExceptionInfo *exception) |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 473 | % |
| 474 | % A description of each parameter follows: |
| 475 | % |
cristy | 46f354c | 2012-07-04 13:31:29 +0000 | [diff] [blame] | 476 | % o images: the image sequence. |
| 477 | % |
| 478 | % o colorspace: the image colorspace. |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 479 | % |
| 480 | % o exception: return any errors or warnings in this structure. |
| 481 | % |
| 482 | */ |
cristy | 6398ec7 | 2013-11-28 02:00:27 +0000 | [diff] [blame^] | 483 | #if defined(__cplusplus) || defined(c_plusplus) |
| 484 | extern "C" { |
| 485 | #endif |
| 486 | |
cristy | 46f354c | 2012-07-04 13:31:29 +0000 | [diff] [blame] | 487 | MagickExport Image *CombineImages(const Image *image, |
| 488 | const ColorspaceType colorspace,ExceptionInfo *exception) |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 489 | { |
| 490 | #define CombineImageTag "Combine/Image" |
| 491 | |
| 492 | CacheView |
| 493 | *combine_view; |
| 494 | |
| 495 | Image |
| 496 | *combine_image; |
| 497 | |
| 498 | MagickBooleanType |
| 499 | status; |
| 500 | |
| 501 | MagickOffsetType |
| 502 | progress; |
| 503 | |
| 504 | ssize_t |
| 505 | y; |
| 506 | |
| 507 | /* |
| 508 | Ensure the image are the same size. |
| 509 | */ |
| 510 | assert(image != (const Image *) NULL); |
| 511 | assert(image->signature == MagickSignature); |
| 512 | if (image->debug != MagickFalse) |
| 513 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 514 | assert(exception != (ExceptionInfo *) NULL); |
| 515 | assert(exception->signature == MagickSignature); |
| 516 | combine_image=CloneImage(image,0,0,MagickTrue,exception); |
| 517 | if (combine_image == (Image *) NULL) |
| 518 | return((Image *) NULL); |
| 519 | if (SetImageStorageClass(combine_image,DirectClass,exception) == MagickFalse) |
| 520 | { |
| 521 | combine_image=DestroyImage(combine_image); |
| 522 | return((Image *) NULL); |
| 523 | } |
cristy | 74f3f94 | 2013-05-01 11:11:28 +0000 | [diff] [blame] | 524 | (void) SetImageColorspace(combine_image,colorspace,exception); |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 525 | if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 526 | combine_image->alpha_trait=BlendPixelTrait; |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 527 | /* |
| 528 | Combine images. |
| 529 | */ |
| 530 | status=MagickTrue; |
| 531 | progress=0; |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 532 | combine_view=AcquireAuthenticCacheView(combine_image,exception); |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 533 | for (y=0; y < (ssize_t) combine_image->rows; y++) |
| 534 | { |
| 535 | CacheView |
| 536 | *image_view; |
| 537 | |
| 538 | const Image |
| 539 | *next; |
| 540 | |
| 541 | Quantum |
| 542 | *pixels; |
| 543 | |
| 544 | register const Quantum |
| 545 | *restrict p; |
| 546 | |
| 547 | register Quantum |
| 548 | *restrict q; |
| 549 | |
| 550 | register ssize_t |
| 551 | i; |
| 552 | |
| 553 | if (status == MagickFalse) |
| 554 | continue; |
| 555 | pixels=GetCacheViewAuthenticPixels(combine_view,0,y,combine_image->columns, |
| 556 | 1,exception); |
| 557 | if (pixels == (Quantum *) NULL) |
| 558 | { |
| 559 | status=MagickFalse; |
| 560 | continue; |
| 561 | } |
| 562 | next=image; |
cristy | c1119af | 2012-04-16 15:28:23 +0000 | [diff] [blame] | 563 | for (i=0; i < (ssize_t) GetPixelChannels(combine_image); i++) |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 564 | { |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 565 | register ssize_t |
| 566 | x; |
| 567 | |
cristy | 5a23c55 | 2013-02-13 14:34:28 +0000 | [diff] [blame] | 568 | PixelChannel channel=GetPixelChannelChannel(combine_image,i); |
| 569 | PixelTrait traits=GetPixelChannelTraits(combine_image,channel); |
cristy | c1119af | 2012-04-16 15:28:23 +0000 | [diff] [blame] | 570 | if (traits == UndefinedPixelTrait) |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 571 | continue; |
cristy | 5a23c55 | 2013-02-13 14:34:28 +0000 | [diff] [blame] | 572 | if (next == (Image *) NULL) |
| 573 | continue; |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 574 | image_view=AcquireVirtualCacheView(next,exception); |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 575 | p=GetCacheViewVirtualPixels(image_view,0,y,next->columns,1,exception); |
| 576 | if (p == (const Quantum *) NULL) |
| 577 | continue; |
| 578 | q=pixels; |
| 579 | for (x=0; x < (ssize_t) combine_image->columns; x++) |
| 580 | { |
cristy | f3381ae | 2012-07-13 16:30:09 +0000 | [diff] [blame] | 581 | if (x < (ssize_t) next->columns) |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 582 | { |
cristy | f3381ae | 2012-07-13 16:30:09 +0000 | [diff] [blame] | 583 | q[i]=GetPixelGray(next,p); |
| 584 | p+=GetPixelChannels(next); |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 585 | } |
| 586 | q+=GetPixelChannels(combine_image); |
| 587 | } |
| 588 | image_view=DestroyCacheView(image_view); |
| 589 | next=GetNextImageInList(next); |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 590 | } |
cristy | f3381ae | 2012-07-13 16:30:09 +0000 | [diff] [blame] | 591 | if (SyncCacheViewAuthenticPixels(combine_view,exception) == MagickFalse) |
| 592 | status=MagickFalse; |
| 593 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 594 | { |
| 595 | MagickBooleanType |
| 596 | proceed; |
| 597 | |
| 598 | proceed=SetImageProgress(image,CombineImageTag,progress++, |
| 599 | combine_image->rows); |
| 600 | if (proceed == MagickFalse) |
| 601 | status=MagickFalse; |
| 602 | } |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 603 | } |
| 604 | combine_view=DestroyCacheView(combine_view); |
| 605 | if (status == MagickFalse) |
| 606 | combine_image=DestroyImage(combine_image); |
| 607 | return(combine_image); |
| 608 | } |
cristy | 6398ec7 | 2013-11-28 02:00:27 +0000 | [diff] [blame^] | 609 | |
| 610 | #if defined(__cplusplus) || defined(c_plusplus) |
| 611 | } |
| 612 | #endif |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 613 | |
| 614 | /* |
| 615 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 616 | % % |
| 617 | % % |
| 618 | % % |
cristy | 8c8c916 | 2013-05-27 10:14:21 +0000 | [diff] [blame] | 619 | % G e t I m a g e A l p h a C h a n n e l % |
| 620 | % % |
| 621 | % % |
| 622 | % % |
| 623 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 624 | % |
| 625 | % GetImageAlphaChannel() returns MagickFalse if the image alpha channel is |
| 626 | % not activated. That is, the image is RGB rather than RGBA or CMYK rather |
| 627 | % than CMYKA. |
| 628 | % |
| 629 | % The format of the GetImageAlphaChannel method is: |
| 630 | % |
| 631 | % MagickBooleanType GetImageAlphaChannel(const Image *image) |
| 632 | % |
| 633 | % A description of each parameter follows: |
| 634 | % |
| 635 | % o image: the image. |
| 636 | % |
| 637 | */ |
| 638 | MagickExport MagickBooleanType GetImageAlphaChannel(const Image *image) |
| 639 | { |
| 640 | assert(image != (const Image *) NULL); |
| 641 | if (image->debug != MagickFalse) |
| 642 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"..."); |
| 643 | assert(image->signature == MagickSignature); |
| 644 | return(image->alpha_trait == BlendPixelTrait ? MagickTrue : MagickFalse); |
| 645 | } |
| 646 | |
| 647 | /* |
| 648 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 649 | % % |
| 650 | % % |
| 651 | % % |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 652 | % S e p a r a t e I m a g e % |
| 653 | % % |
| 654 | % % |
| 655 | % % |
| 656 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 657 | % |
| 658 | % SeparateImage() separates a channel from the image and returns it as a |
| 659 | % grayscale image. |
| 660 | % |
| 661 | % The format of the SeparateImage method is: |
| 662 | % |
| 663 | % Image *SeparateImage(const Image *image,const ChannelType channel, |
| 664 | % ExceptionInfo *exception) |
| 665 | % |
| 666 | % A description of each parameter follows: |
| 667 | % |
| 668 | % o image: the image. |
| 669 | % |
| 670 | % o channel: the image channel. |
| 671 | % |
| 672 | % o exception: return any errors or warnings in this structure. |
| 673 | % |
| 674 | */ |
cristy | 6398ec7 | 2013-11-28 02:00:27 +0000 | [diff] [blame^] | 675 | #if defined(__cplusplus) || defined(c_plusplus) |
| 676 | extern "C" { |
| 677 | #endif |
| 678 | |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 679 | MagickExport Image *SeparateImage(const Image *image, |
| 680 | const ChannelType channel_type,ExceptionInfo *exception) |
| 681 | { |
| 682 | #define GetChannelBit(mask,bit) (((size_t) (mask) >> (size_t) (bit)) & 0x01) |
| 683 | #define SeparateImageTag "Separate/Image" |
| 684 | |
| 685 | CacheView |
| 686 | *image_view, |
| 687 | *separate_view; |
| 688 | |
| 689 | Image |
| 690 | *separate_image; |
| 691 | |
| 692 | MagickBooleanType |
| 693 | status; |
| 694 | |
| 695 | MagickOffsetType |
| 696 | progress; |
| 697 | |
| 698 | ssize_t |
| 699 | y; |
| 700 | |
| 701 | /* |
cristy | 8fdeeb3 | 2013-03-31 21:15:31 +0000 | [diff] [blame] | 702 | Initialize separate image attributes. |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 703 | */ |
| 704 | assert(image != (Image *) NULL); |
| 705 | assert(image->signature == MagickSignature); |
| 706 | if (image->debug != MagickFalse) |
| 707 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 708 | assert(exception != (ExceptionInfo *) NULL); |
| 709 | assert(exception->signature == MagickSignature); |
| 710 | separate_image=CloneImage(image,image->columns,image->rows,MagickTrue, |
| 711 | exception); |
| 712 | if (separate_image == (Image *) NULL) |
| 713 | return((Image *) NULL); |
| 714 | if (SetImageStorageClass(separate_image,DirectClass,exception) == MagickFalse) |
| 715 | { |
| 716 | separate_image=DestroyImage(separate_image); |
| 717 | return((Image *) NULL); |
| 718 | } |
cristy | aa7a225 | 2012-08-15 12:19:00 +0000 | [diff] [blame] | 719 | (void) SetImageColorspace(separate_image,GRAYColorspace,exception); |
cristy | 9b8205c | 2013-04-16 11:16:19 +0000 | [diff] [blame] | 720 | separate_image->alpha_trait=UndefinedPixelTrait; |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 721 | /* |
| 722 | Separate image. |
| 723 | */ |
| 724 | status=MagickTrue; |
| 725 | progress=0; |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 726 | image_view=AcquireVirtualCacheView(image,exception); |
| 727 | separate_view=AcquireAuthenticCacheView(separate_image,exception); |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 728 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 9a5a52f | 2012-10-09 14:40:31 +0000 | [diff] [blame] | 729 | #pragma omp parallel for schedule(static,4) shared(progress,status) \ |
cristy | 5e6b259 | 2012-12-19 14:08:11 +0000 | [diff] [blame] | 730 | magick_threads(image,image,image->rows,1) |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 731 | #endif |
| 732 | for (y=0; y < (ssize_t) image->rows; y++) |
| 733 | { |
| 734 | register const Quantum |
| 735 | *restrict p; |
| 736 | |
| 737 | register Quantum |
| 738 | *restrict q; |
| 739 | |
| 740 | register ssize_t |
| 741 | x; |
| 742 | |
| 743 | if (status == MagickFalse) |
| 744 | continue; |
| 745 | p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception); |
| 746 | q=QueueCacheViewAuthenticPixels(separate_view,0,y,separate_image->columns,1, |
| 747 | exception); |
| 748 | if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL)) |
| 749 | { |
| 750 | status=MagickFalse; |
| 751 | continue; |
| 752 | } |
| 753 | for (x=0; x < (ssize_t) image->columns; x++) |
| 754 | { |
| 755 | register ssize_t |
| 756 | i; |
| 757 | |
cristy | 883fde1 | 2013-04-08 00:50:13 +0000 | [diff] [blame] | 758 | if (GetPixelReadMask(image,p) == 0) |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 759 | { |
cristy | c3a5802 | 2013-10-09 23:22:42 +0000 | [diff] [blame] | 760 | SetPixelBackgoundColor(separate_image,q); |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 761 | p+=GetPixelChannels(image); |
| 762 | q+=GetPixelChannels(separate_image); |
| 763 | continue; |
| 764 | } |
| 765 | SetPixelChannel(separate_image,GrayPixelChannel,0,q); |
| 766 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
| 767 | { |
cristy | 5a23c55 | 2013-02-13 14:34:28 +0000 | [diff] [blame] | 768 | PixelChannel channel=GetPixelChannelChannel(image,i); |
| 769 | PixelTrait traits=GetPixelChannelTraits(image,channel); |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 770 | if ((traits == UndefinedPixelTrait) || |
| 771 | (GetChannelBit(channel_type,channel) == 0)) |
| 772 | continue; |
cristy | 9b8205c | 2013-04-16 11:16:19 +0000 | [diff] [blame] | 773 | SetPixelChannel(separate_image,GrayPixelChannel,p[i],q); |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 774 | } |
| 775 | p+=GetPixelChannels(image); |
| 776 | q+=GetPixelChannels(separate_image); |
| 777 | } |
| 778 | if (SyncCacheViewAuthenticPixels(separate_view,exception) == MagickFalse) |
| 779 | status=MagickFalse; |
| 780 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 781 | { |
| 782 | MagickBooleanType |
| 783 | proceed; |
| 784 | |
| 785 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 786 | #pragma omp critical (MagickCore_SeparateImage) |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 787 | #endif |
| 788 | proceed=SetImageProgress(image,SeparateImageTag,progress++,image->rows); |
| 789 | if (proceed == MagickFalse) |
| 790 | status=MagickFalse; |
| 791 | } |
| 792 | } |
| 793 | separate_view=DestroyCacheView(separate_view); |
| 794 | image_view=DestroyCacheView(image_view); |
cristy | 1c2f48d | 2012-12-14 01:20:55 +0000 | [diff] [blame] | 795 | if (status == MagickFalse) |
| 796 | separate_image=DestroyImage(separate_image); |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 797 | return(separate_image); |
| 798 | } |
cristy | 6398ec7 | 2013-11-28 02:00:27 +0000 | [diff] [blame^] | 799 | |
| 800 | #if defined(__cplusplus) || defined(c_plusplus) |
| 801 | } |
| 802 | #endif |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 803 | |
| 804 | /* |
| 805 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 806 | % % |
| 807 | % % |
| 808 | % % |
| 809 | % S e p a r a t e I m a g e s % |
| 810 | % % |
| 811 | % % |
| 812 | % % |
| 813 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 814 | % |
| 815 | % SeparateImages() returns a separate grayscale image for each channel |
| 816 | % specified. |
| 817 | % |
| 818 | % The format of the SeparateImages method is: |
| 819 | % |
cristy | dfdb19e | 2012-03-21 22:22:24 +0000 | [diff] [blame] | 820 | % Image *SeparateImages(const Image *image,ExceptionInfo *exception) |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 821 | % |
| 822 | % A description of each parameter follows: |
| 823 | % |
| 824 | % o image: the image. |
| 825 | % |
| 826 | % o exception: return any errors or warnings in this structure. |
| 827 | % |
| 828 | */ |
cristy | 6398ec7 | 2013-11-28 02:00:27 +0000 | [diff] [blame^] | 829 | #if defined(__cplusplus) || defined(c_plusplus) |
| 830 | extern "C" { |
| 831 | #endif |
| 832 | |
cristy | dfdb19e | 2012-03-21 22:22:24 +0000 | [diff] [blame] | 833 | MagickExport Image *SeparateImages(const Image *image,ExceptionInfo *exception) |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 834 | { |
| 835 | Image |
| 836 | *images, |
| 837 | *separate_image; |
| 838 | |
| 839 | register ssize_t |
| 840 | i; |
| 841 | |
| 842 | assert(image != (Image *) NULL); |
| 843 | assert(image->signature == MagickSignature); |
| 844 | if (image->debug != MagickFalse) |
| 845 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 846 | images=NewImageList(); |
| 847 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
| 848 | { |
cristy | 5a23c55 | 2013-02-13 14:34:28 +0000 | [diff] [blame] | 849 | PixelChannel channel=GetPixelChannelChannel(image,i); |
| 850 | PixelTrait traits=GetPixelChannelTraits(image,channel); |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 851 | if ((traits == UndefinedPixelTrait) || |
| 852 | ((traits & UpdatePixelTrait) == 0)) |
| 853 | continue; |
| 854 | separate_image=SeparateImage(image,(ChannelType) (1 << channel),exception); |
| 855 | if (separate_image != (Image *) NULL) |
| 856 | AppendImageToList(&images,separate_image); |
| 857 | } |
cristy | e5ef2ce | 2012-04-29 21:14:32 +0000 | [diff] [blame] | 858 | if (images == (Image *) NULL) |
| 859 | images=SeparateImage(image,UndefinedChannel,exception); |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 860 | return(images); |
| 861 | } |
cristy | 6398ec7 | 2013-11-28 02:00:27 +0000 | [diff] [blame^] | 862 | |
| 863 | #if defined(__cplusplus) || defined(c_plusplus) |
| 864 | } |
| 865 | #endif |
cristy | 8c8c916 | 2013-05-27 10:14:21 +0000 | [diff] [blame] | 866 | |
| 867 | /* |
| 868 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 869 | % % |
| 870 | % % |
| 871 | % % |
| 872 | % S e t I m a g e A l p h a C h a n n e l % |
| 873 | % % |
| 874 | % % |
| 875 | % % |
| 876 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 877 | % |
| 878 | % SetImageAlphaChannel() activates, deactivates, resets, or sets the alpha |
| 879 | % channel. |
| 880 | % |
| 881 | % The format of the SetImageAlphaChannel method is: |
| 882 | % |
| 883 | % MagickBooleanType SetImageAlphaChannel(Image *image, |
| 884 | % const AlphaChannelOption alpha_type,ExceptionInfo *exception) |
| 885 | % |
| 886 | % A description of each parameter follows: |
| 887 | % |
| 888 | % o image: the image. |
| 889 | % |
| 890 | % o alpha_type: The alpha channel type: ActivateAlphaChannel, |
| 891 | % CopyAlphaChannel, DeactivateAlphaChannel, ExtractAlphaChannel, |
| 892 | % OpaqueAlphaChannel, SetAlphaChannel, ShapeAlphaChannel, and |
| 893 | % TransparentAlphaChannel. |
| 894 | % |
| 895 | % o exception: return any errors or warnings in this structure. |
| 896 | % |
| 897 | */ |
| 898 | |
| 899 | static inline void FlattenPixelInfo(const Image *image,const PixelInfo *p, |
| 900 | const double alpha,const Quantum *q,const double beta, |
| 901 | Quantum *composite) |
| 902 | { |
| 903 | double |
| 904 | Da, |
| 905 | gamma, |
| 906 | Sa; |
| 907 | |
| 908 | register ssize_t |
| 909 | i; |
| 910 | |
| 911 | /* |
| 912 | Compose pixel p over pixel q with the given alpha. |
| 913 | */ |
| 914 | Sa=QuantumScale*alpha; |
| 915 | Da=QuantumScale*beta, |
| 916 | gamma=Sa*(-Da)+Sa+Da; |
| 917 | gamma=PerceptibleReciprocal(gamma); |
| 918 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
| 919 | { |
| 920 | PixelChannel channel=GetPixelChannelChannel(image,i); |
| 921 | PixelTrait traits=GetPixelChannelTraits(image,channel); |
| 922 | if (traits == UndefinedPixelTrait) |
| 923 | continue; |
| 924 | switch (channel) |
| 925 | { |
| 926 | case RedPixelChannel: |
| 927 | { |
| 928 | composite[i]=ClampToQuantum(gamma*MagickOver_((double) q[i],beta, |
| 929 | (double) p->red,alpha)); |
| 930 | break; |
| 931 | } |
| 932 | case GreenPixelChannel: |
| 933 | { |
| 934 | composite[i]=ClampToQuantum(gamma*MagickOver_((double) q[i],beta, |
| 935 | (double) p->green,alpha)); |
| 936 | break; |
| 937 | } |
| 938 | case BluePixelChannel: |
| 939 | { |
| 940 | composite[i]=ClampToQuantum(gamma*MagickOver_((double) q[i],beta, |
| 941 | (double) p->blue,alpha)); |
| 942 | break; |
| 943 | } |
| 944 | case BlackPixelChannel: |
| 945 | { |
| 946 | composite[i]=ClampToQuantum(gamma*MagickOver_((double) q[i],beta, |
| 947 | (double) p->black,alpha)); |
| 948 | break; |
| 949 | } |
| 950 | case AlphaPixelChannel: |
| 951 | { |
| 952 | composite[i]=ClampToQuantum(QuantumRange*(Sa*(-Da)+Sa+Da)); |
| 953 | break; |
| 954 | } |
| 955 | default: |
| 956 | break; |
| 957 | } |
| 958 | } |
| 959 | } |
| 960 | |
cristy | 6398ec7 | 2013-11-28 02:00:27 +0000 | [diff] [blame^] | 961 | #if defined(__cplusplus) || defined(c_plusplus) |
| 962 | extern "C" { |
| 963 | #endif |
| 964 | |
cristy | 8c8c916 | 2013-05-27 10:14:21 +0000 | [diff] [blame] | 965 | MagickExport MagickBooleanType SetImageAlphaChannel(Image *image, |
| 966 | const AlphaChannelOption alpha_type,ExceptionInfo *exception) |
| 967 | { |
| 968 | MagickBooleanType |
| 969 | status; |
| 970 | |
| 971 | assert(image != (Image *) NULL); |
| 972 | if (image->debug != MagickFalse) |
| 973 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"..."); |
| 974 | assert(image->signature == MagickSignature); |
| 975 | status=MagickTrue; |
| 976 | switch (alpha_type) |
| 977 | { |
| 978 | case ActivateAlphaChannel: |
| 979 | { |
| 980 | image->alpha_trait=BlendPixelTrait; |
| 981 | break; |
| 982 | } |
| 983 | case BackgroundAlphaChannel: |
| 984 | { |
| 985 | CacheView |
| 986 | *image_view; |
| 987 | |
| 988 | ssize_t |
| 989 | y; |
| 990 | |
| 991 | /* |
| 992 | Set transparent pixels to background color. |
| 993 | */ |
| 994 | if (image->alpha_trait != BlendPixelTrait) |
| 995 | break; |
| 996 | if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse) |
| 997 | break; |
| 998 | image_view=AcquireAuthenticCacheView(image,exception); |
| 999 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 1000 | #pragma omp parallel for schedule(static,4) shared(status) \ |
| 1001 | magick_threads(image,image,image->rows,1) |
| 1002 | #endif |
| 1003 | for (y=0; y < (ssize_t) image->rows; y++) |
| 1004 | { |
| 1005 | register Quantum |
| 1006 | *restrict q; |
| 1007 | |
| 1008 | register ssize_t |
| 1009 | x; |
| 1010 | |
| 1011 | if (status == MagickFalse) |
| 1012 | continue; |
| 1013 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1, |
| 1014 | exception); |
| 1015 | if (q == (Quantum *) NULL) |
| 1016 | { |
| 1017 | status=MagickFalse; |
| 1018 | continue; |
| 1019 | } |
| 1020 | for (x=0; x < (ssize_t) image->columns; x++) |
| 1021 | { |
| 1022 | if (GetPixelAlpha(image,q) == TransparentAlpha) |
| 1023 | { |
| 1024 | SetPixelInfoPixel(image,&image->background_color,q); |
| 1025 | SetPixelChannel(image,AlphaPixelChannel,TransparentAlpha,q); |
| 1026 | } |
| 1027 | q+=GetPixelChannels(image); |
| 1028 | } |
| 1029 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
| 1030 | status=MagickFalse; |
| 1031 | } |
| 1032 | image_view=DestroyCacheView(image_view); |
| 1033 | return(status); |
| 1034 | } |
| 1035 | case CopyAlphaChannel: |
| 1036 | case ShapeAlphaChannel: |
| 1037 | { |
| 1038 | /* |
| 1039 | Copy pixel intensity to the alpha channel. |
| 1040 | */ |
| 1041 | status=CompositeImage(image,image,IntensityCompositeOp,MagickTrue,0,0, |
| 1042 | exception); |
| 1043 | if (alpha_type == ShapeAlphaChannel) |
| 1044 | (void) LevelImageColors(image,&image->background_color, |
| 1045 | &image->background_color,MagickTrue,exception); |
| 1046 | break; |
| 1047 | } |
| 1048 | case DeactivateAlphaChannel: |
| 1049 | { |
| 1050 | image->alpha_trait=CopyPixelTrait; |
| 1051 | break; |
| 1052 | } |
| 1053 | case ExtractAlphaChannel: |
| 1054 | { |
| 1055 | status=CompositeImage(image,image,AlphaCompositeOp,MagickTrue,0,0, |
| 1056 | exception); |
| 1057 | image->alpha_trait=CopyPixelTrait; |
| 1058 | break; |
| 1059 | } |
| 1060 | case OpaqueAlphaChannel: |
| 1061 | { |
| 1062 | status=SetImageAlpha(image,OpaqueAlpha,exception); |
| 1063 | break; |
| 1064 | } |
| 1065 | case RemoveAlphaChannel: |
| 1066 | { |
| 1067 | CacheView |
| 1068 | *image_view; |
| 1069 | |
| 1070 | ssize_t |
| 1071 | y; |
| 1072 | |
| 1073 | /* |
| 1074 | Remove transparency. |
| 1075 | */ |
| 1076 | if (image->alpha_trait != BlendPixelTrait) |
| 1077 | break; |
| 1078 | if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse) |
| 1079 | break; |
| 1080 | image_view=AcquireAuthenticCacheView(image,exception); |
| 1081 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 1082 | #pragma omp parallel for schedule(static,4) shared(status) \ |
| 1083 | magick_threads(image,image,image->rows,1) |
| 1084 | #endif |
| 1085 | for (y=0; y < (ssize_t) image->rows; y++) |
| 1086 | { |
| 1087 | register Quantum |
| 1088 | *restrict q; |
| 1089 | |
| 1090 | register ssize_t |
| 1091 | x; |
| 1092 | |
| 1093 | if (status == MagickFalse) |
| 1094 | continue; |
| 1095 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1, |
| 1096 | exception); |
| 1097 | if (q == (Quantum *) NULL) |
| 1098 | { |
| 1099 | status=MagickFalse; |
| 1100 | continue; |
| 1101 | } |
| 1102 | for (x=0; x < (ssize_t) image->columns; x++) |
| 1103 | { |
| 1104 | FlattenPixelInfo(image,&image->background_color, |
| 1105 | image->background_color.alpha,q,(double) |
| 1106 | GetPixelAlpha(image,q),q); |
| 1107 | q+=GetPixelChannels(image); |
| 1108 | } |
| 1109 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
| 1110 | status=MagickFalse; |
| 1111 | } |
| 1112 | image_view=DestroyCacheView(image_view); |
| 1113 | image->alpha_trait=image->background_color.alpha_trait; |
| 1114 | return(status); |
| 1115 | } |
| 1116 | case SetAlphaChannel: |
| 1117 | { |
| 1118 | if (image->alpha_trait != BlendPixelTrait) |
| 1119 | status=SetImageAlpha(image,OpaqueAlpha,exception); |
| 1120 | break; |
| 1121 | } |
| 1122 | case TransparentAlphaChannel: |
| 1123 | { |
| 1124 | status=SetImageAlpha(image,TransparentAlpha,exception); |
| 1125 | break; |
| 1126 | } |
| 1127 | case UndefinedAlphaChannel: |
| 1128 | break; |
| 1129 | } |
| 1130 | if (status == MagickFalse) |
| 1131 | return(status); |
| 1132 | return(SyncImagePixelCache(image,exception)); |
| 1133 | } |
cristy | 6398ec7 | 2013-11-28 02:00:27 +0000 | [diff] [blame^] | 1134 | |
| 1135 | #if defined(__cplusplus) || defined(c_plusplus) |
| 1136 | } |
| 1137 | #endif |