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