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 | % % |
| 20 | % Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization % |
| 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 | c1119af | 2012-04-16 15:28:23 +0000 | [diff] [blame] | 44 | #include "MagickCore/colorspace-private.h" |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 45 | #include "MagickCore/image.h" |
| 46 | #include "MagickCore/list.h" |
| 47 | #include "MagickCore/log.h" |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 48 | #include "MagickCore/monitor.h" |
| 49 | #include "MagickCore/monitor-private.h" |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 50 | #include "MagickCore/option.h" |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 51 | #include "MagickCore/pixel-accessor.h" |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 52 | #include "MagickCore/resource_.h" |
cristy | 8748f56 | 2012-03-08 13:58:34 +0000 | [diff] [blame] | 53 | #include "MagickCore/string-private.h" |
cristy | 16881e6 | 2012-05-06 14:41:29 +0000 | [diff] [blame] | 54 | #include "MagickCore/thread-private.h" |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 55 | #include "MagickCore/token.h" |
cristy | d04e7bf | 2012-03-03 19:19:12 +0000 | [diff] [blame] | 56 | #include "MagickCore/utility.h" |
| 57 | #include "MagickCore/version.h" |
| 58 | |
| 59 | /* |
| 60 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 61 | % % |
| 62 | % % |
| 63 | % % |
cristy | 5f257b2 | 2012-03-07 00:27:29 +0000 | [diff] [blame] | 64 | % 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] | 65 | % % |
| 66 | % % |
| 67 | % % |
| 68 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 69 | % |
cristy | 5f257b2 | 2012-03-07 00:27:29 +0000 | [diff] [blame] | 70 | % ChannelFxImage() applies a channel expression to the specified image. The |
| 71 | % expression consists of one or more channels, either mnemonic or numeric (e.g. |
| 72 | % red, 1), separated by actions as follows: |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 73 | % |
| 74 | % <=> exchange two channels (e.g. red<=>blue) |
cristy | 7ea920d | 2012-03-16 01:08:01 +0000 | [diff] [blame] | 75 | % => copy one channel to another channel (e.g. red=>green) |
| 76 | % = assign a constant value to a channel (e.g. red=50%) |
| 77 | % , write new image channels in the specified order (e.g. red, green) |
cristy | 3c21361 | 2012-03-16 01:14:48 +0000 | [diff] [blame] | 78 | % | add a new output image for the next set of channel operations |
| 79 | % ; move to the next input image for the source of channel data |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 80 | % |
cristy | 7ea920d | 2012-03-16 01:08:01 +0000 | [diff] [blame] | 81 | % For example, to create 3 grayscale images from the red, green, and blue |
| 82 | % channels of an image, use: |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 83 | % |
cristy | 3c21361 | 2012-03-16 01:14:48 +0000 | [diff] [blame] | 84 | % -channel-fx "red; green; blue" |
cristy | 7ea920d | 2012-03-16 01:08:01 +0000 | [diff] [blame] | 85 | % |
| 86 | % A channel without an operation symbol implies separate (i.e, semicolon). |
cristy | d04e7bf | 2012-03-03 19:19:12 +0000 | [diff] [blame] | 87 | % |
cristy | 5f257b2 | 2012-03-07 00:27:29 +0000 | [diff] [blame] | 88 | % The format of the ChannelFxImage method is: |
cristy | d04e7bf | 2012-03-03 19:19:12 +0000 | [diff] [blame] | 89 | % |
cristy | 5f257b2 | 2012-03-07 00:27:29 +0000 | [diff] [blame] | 90 | % Image *ChannelFxImage(const Image *image,const char *expression, |
| 91 | % ExceptionInfo *exception) |
cristy | d04e7bf | 2012-03-03 19:19:12 +0000 | [diff] [blame] | 92 | % |
| 93 | % A description of each parameter follows: |
| 94 | % |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 95 | % o image: the image. |
cristy | d04e7bf | 2012-03-03 19:19:12 +0000 | [diff] [blame] | 96 | % |
| 97 | % o expression: A channel expression. |
| 98 | % |
| 99 | % o exception: return any errors or warnings in this structure. |
| 100 | % |
| 101 | */ |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 102 | |
| 103 | typedef enum |
| 104 | { |
| 105 | ExtractChannelOp, |
cristy | 8748f56 | 2012-03-08 13:58:34 +0000 | [diff] [blame] | 106 | AssignChannelOp, |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 107 | ExchangeChannelOp, |
| 108 | TransferChannelOp |
cristy | 5f257b2 | 2012-03-07 00:27:29 +0000 | [diff] [blame] | 109 | } ChannelFx; |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 110 | |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 111 | static inline size_t MagickMin(const size_t x,const size_t y) |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 112 | { |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 113 | if (x < y) |
| 114 | return(x); |
| 115 | return(y); |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 116 | } |
| 117 | |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 118 | static MagickBooleanType ChannelImage(Image *destination_image, |
cristy | 8748f56 | 2012-03-08 13:58:34 +0000 | [diff] [blame] | 119 | const PixelChannel destination_channel,const ChannelFx channel_op, |
cristy | 3bb378a | 2012-03-08 14:38:52 +0000 | [diff] [blame] | 120 | const Image *source_image,const PixelChannel source_channel, |
cristy | 8748f56 | 2012-03-08 13:58:34 +0000 | [diff] [blame] | 121 | const Quantum pixel,ExceptionInfo *exception) |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 122 | { |
| 123 | CacheView |
| 124 | *source_view, |
| 125 | *destination_view; |
| 126 | |
| 127 | MagickBooleanType |
| 128 | status; |
| 129 | |
| 130 | size_t |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 131 | height, |
| 132 | width; |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 133 | |
| 134 | ssize_t |
| 135 | y; |
| 136 | |
| 137 | status=MagickTrue; |
cristy | db07095 | 2012-04-20 14:33:00 +0000 | [diff] [blame] | 138 | source_view=AcquireVirtualCacheView(source_image,exception); |
| 139 | destination_view=AcquireAuthenticCacheView(destination_image,exception); |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 140 | height=MagickMin(source_image->rows,destination_image->rows); |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 141 | width=MagickMin(source_image->columns,destination_image->columns); |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 142 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 143 | #pragma omp parallel for schedule(static) shared(status) \ |
cristy | 4ee2b0c | 2012-05-15 00:30:35 +0000 | [diff] [blame] | 144 | dynamic_number_threads(source_image,width,height,1) |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 145 | #endif |
| 146 | for (y=0; y < (ssize_t) height; y++) |
| 147 | { |
cristy | 4ee6040 | 2012-03-11 02:01:20 +0000 | [diff] [blame] | 148 | PixelTrait |
| 149 | destination_traits, |
| 150 | source_traits; |
| 151 | |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 152 | register const Quantum |
| 153 | *restrict p; |
| 154 | |
| 155 | register Quantum |
| 156 | *restrict q; |
| 157 | |
| 158 | register ssize_t |
| 159 | x; |
| 160 | |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 161 | if (status == MagickFalse) |
| 162 | continue; |
| 163 | p=GetCacheViewVirtualPixels(source_view,0,y,source_image->columns,1, |
| 164 | exception); |
cristy | 4ee6040 | 2012-03-11 02:01:20 +0000 | [diff] [blame] | 165 | q=GetCacheViewAuthenticPixels(destination_view,0,y, |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 166 | destination_image->columns,1,exception); |
| 167 | if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL)) |
| 168 | { |
| 169 | status=MagickFalse; |
| 170 | continue; |
| 171 | } |
cristy | 4ee6040 | 2012-03-11 02:01:20 +0000 | [diff] [blame] | 172 | destination_traits=GetPixelChannelMapTraits(destination_image, |
| 173 | destination_channel); |
| 174 | source_traits=GetPixelChannelMapTraits(source_image,source_channel); |
| 175 | if ((destination_traits == UndefinedPixelTrait) || |
| 176 | (source_traits == UndefinedPixelTrait)) |
| 177 | continue; |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 178 | for (x=0; x < (ssize_t) width; x++) |
| 179 | { |
cristy | 8748f56 | 2012-03-08 13:58:34 +0000 | [diff] [blame] | 180 | if (channel_op == AssignChannelOp) |
| 181 | SetPixelChannel(destination_image,destination_channel,pixel,q); |
| 182 | else |
cristy | 4ee6040 | 2012-03-11 02:01:20 +0000 | [diff] [blame] | 183 | SetPixelChannel(destination_image,destination_channel, |
| 184 | GetPixelChannel(source_image,source_channel,p),q); |
| 185 | p+=GetPixelChannels(source_image); |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 186 | q+=GetPixelChannels(destination_image); |
| 187 | } |
| 188 | if (SyncCacheViewAuthenticPixels(destination_view,exception) == MagickFalse) |
| 189 | status=MagickFalse; |
| 190 | } |
| 191 | destination_view=DestroyCacheView(destination_view); |
| 192 | source_view=DestroyCacheView(source_view); |
| 193 | return(status); |
| 194 | } |
| 195 | |
cristy | 5f257b2 | 2012-03-07 00:27:29 +0000 | [diff] [blame] | 196 | MagickExport Image *ChannelFxImage(const Image *image,const char *expression, |
| 197 | ExceptionInfo *exception) |
cristy | d04e7bf | 2012-03-03 19:19:12 +0000 | [diff] [blame] | 198 | { |
cristy | 5f257b2 | 2012-03-07 00:27:29 +0000 | [diff] [blame] | 199 | #define ChannelFxImageTag "ChannelFx/Image" |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 200 | |
cristy | 5f257b2 | 2012-03-07 00:27:29 +0000 | [diff] [blame] | 201 | ChannelFx |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 202 | channel_op; |
| 203 | |
cristy | fe88ede | 2012-03-22 00:34:48 +0000 | [diff] [blame] | 204 | ChannelType |
| 205 | channel_mask; |
| 206 | |
cristy | 4ee6040 | 2012-03-11 02:01:20 +0000 | [diff] [blame] | 207 | char |
| 208 | token[MaxTextExtent]; |
| 209 | |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 210 | const char |
| 211 | *p; |
| 212 | |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 213 | const Image |
| 214 | *source_image; |
| 215 | |
cristy | 8748f56 | 2012-03-08 13:58:34 +0000 | [diff] [blame] | 216 | double |
| 217 | pixel; |
| 218 | |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 219 | Image |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 220 | *destination_image; |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 221 | |
cristy | 4ee6040 | 2012-03-11 02:01:20 +0000 | [diff] [blame] | 222 | MagickBooleanType |
| 223 | status; |
| 224 | |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 225 | PixelChannel |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 226 | source_channel, |
| 227 | destination_channel; |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 228 | |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 229 | ssize_t |
| 230 | channels; |
| 231 | |
| 232 | assert(image != (Image *) NULL); |
| 233 | assert(image->signature == MagickSignature); |
| 234 | if (image->debug != MagickFalse) |
| 235 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 236 | assert(exception != (ExceptionInfo *) NULL); |
| 237 | assert(exception->signature == MagickSignature); |
| 238 | source_image=image; |
| 239 | destination_image=CloneImage(source_image,0,0,MagickTrue,exception); |
| 240 | if (destination_image == (Image *) NULL) |
| 241 | return((Image *) NULL); |
cristy | 72a31a9 | 2012-05-03 14:21:18 +0000 | [diff] [blame] | 242 | if (IsGrayColorspace(image->colorspace) != MagickFalse) |
cristy | b09db11 | 2012-07-11 12:04:31 +0000 | [diff] [blame] | 243 | (void) TransformImageColorspace((Image *) image,RGBColorspace,exception); |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 244 | if (expression == (const char *) NULL) |
| 245 | return(destination_image); |
| 246 | destination_channel=RedPixelChannel; |
cristy | fe88ede | 2012-03-22 00:34:48 +0000 | [diff] [blame] | 247 | channel_mask=UndefinedChannel; |
cristy | 8748f56 | 2012-03-08 13:58:34 +0000 | [diff] [blame] | 248 | pixel=0.0; |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 249 | p=(char *) expression; |
| 250 | GetMagickToken(p,&p,token); |
cristy | 4ee6040 | 2012-03-11 02:01:20 +0000 | [diff] [blame] | 251 | channel_op=ExtractChannelOp; |
cristy | 09ef5be | 2012-03-13 13:01:27 +0000 | [diff] [blame] | 252 | for (channels=0; *token != '\0'; ) |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 253 | { |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 254 | ssize_t |
| 255 | i; |
| 256 | |
| 257 | /* |
| 258 | Interpret channel expression. |
| 259 | */ |
| 260 | if (*token == ',') |
| 261 | { |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 262 | destination_channel=(PixelChannel) ((ssize_t) destination_channel+1); |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 263 | GetMagickToken(p,&p,token); |
| 264 | } |
| 265 | if (*token == '|') |
| 266 | { |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 267 | if (GetNextImageInList(source_image) != (Image *) NULL) |
| 268 | source_image=GetNextImageInList(source_image); |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 269 | else |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 270 | source_image=GetFirstImageInList(source_image); |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 271 | GetMagickToken(p,&p,token); |
| 272 | } |
| 273 | if (*token == ';') |
| 274 | { |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 275 | Image |
| 276 | *canvas; |
| 277 | |
cristy | fe88ede | 2012-03-22 00:34:48 +0000 | [diff] [blame] | 278 | SetPixelChannelMapMask(destination_image,channel_mask); |
| 279 | if ((channel_op == ExtractChannelOp) && (destination_channel == 1)) |
| 280 | (void) SetImageColorspace(destination_image,GRAYColorspace,exception); |
cristy | 4ee6040 | 2012-03-11 02:01:20 +0000 | [diff] [blame] | 281 | status=SetImageStorageClass(destination_image,DirectClass,exception); |
| 282 | if (status == MagickFalse) |
| 283 | { |
cristy | fc68ef5 | 2012-03-11 23:33:15 +0000 | [diff] [blame] | 284 | destination_image=DestroyImageList(destination_image); |
| 285 | return(destination_image); |
cristy | 4ee6040 | 2012-03-11 02:01:20 +0000 | [diff] [blame] | 286 | } |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 287 | canvas=CloneImage(source_image,0,0,MagickTrue,exception); |
| 288 | if (canvas == (Image *) NULL) |
| 289 | { |
cristy | fc68ef5 | 2012-03-11 23:33:15 +0000 | [diff] [blame] | 290 | destination_image=DestroyImageList(destination_image); |
| 291 | return(destination_image); |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 292 | } |
cristy | 72a31a9 | 2012-05-03 14:21:18 +0000 | [diff] [blame] | 293 | if (IsGrayColorspace(canvas->colorspace) != MagickFalse) |
cristy | b09db11 | 2012-07-11 12:04:31 +0000 | [diff] [blame] | 294 | (void) TransformImageColorspace(canvas,RGBColorspace,exception); |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 295 | AppendImageToList(&destination_image,canvas); |
| 296 | destination_image=GetLastImageInList(destination_image); |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 297 | GetMagickToken(p,&p,token); |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 298 | channels=0; |
| 299 | destination_channel=RedPixelChannel; |
cristy | fe88ede | 2012-03-22 00:34:48 +0000 | [diff] [blame] | 300 | channel_mask=UndefinedChannel; |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 301 | } |
| 302 | i=ParsePixelChannelOption(token); |
| 303 | if (i < 0) |
| 304 | { |
| 305 | (void) ThrowMagickException(exception,GetMagickModule(),OptionError, |
anthony | e5b3965 | 2012-04-21 05:37:29 +0000 | [diff] [blame] | 306 | "UnrecognizedChannelType","'%s'",token); |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 307 | destination_image=DestroyImageList(destination_image); |
cristy | fc68ef5 | 2012-03-11 23:33:15 +0000 | [diff] [blame] | 308 | return(destination_image); |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 309 | } |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 310 | source_channel=(PixelChannel) i; |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 311 | channel_op=ExtractChannelOp; |
| 312 | GetMagickToken(p,&p,token); |
| 313 | if (*token == '<') |
| 314 | { |
| 315 | channel_op=ExchangeChannelOp; |
| 316 | GetMagickToken(p,&p,token); |
| 317 | } |
| 318 | if (*token == '=') |
cristy | 8748f56 | 2012-03-08 13:58:34 +0000 | [diff] [blame] | 319 | { |
| 320 | if (channel_op != ExchangeChannelOp) |
| 321 | channel_op=AssignChannelOp; |
| 322 | GetMagickToken(p,&p,token); |
| 323 | } |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 324 | if (*token == '>') |
| 325 | { |
| 326 | if (channel_op != ExchangeChannelOp) |
| 327 | channel_op=TransferChannelOp; |
| 328 | GetMagickToken(p,&p,token); |
| 329 | } |
cristy | 8748f56 | 2012-03-08 13:58:34 +0000 | [diff] [blame] | 330 | switch (channel_op) |
| 331 | { |
| 332 | case AssignChannelOp: |
| 333 | { |
cristy | 41db174 | 2012-03-09 00:57:52 +0000 | [diff] [blame] | 334 | pixel=StringToDoubleInterval(token,(double) QuantumRange+1.0); |
cristy | 3bb378a | 2012-03-08 14:38:52 +0000 | [diff] [blame] | 335 | GetMagickToken(p,&p,token); |
cristy | 8748f56 | 2012-03-08 13:58:34 +0000 | [diff] [blame] | 336 | break; |
| 337 | } |
| 338 | case ExchangeChannelOp: |
| 339 | case TransferChannelOp: |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 340 | { |
| 341 | i=ParsePixelChannelOption(token); |
| 342 | if (i < 0) |
| 343 | { |
| 344 | (void) ThrowMagickException(exception,GetMagickModule(),OptionError, |
anthony | e5b3965 | 2012-04-21 05:37:29 +0000 | [diff] [blame] | 345 | "UnrecognizedChannelType","'%s'",token); |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 346 | destination_image=DestroyImageList(destination_image); |
cristy | fc68ef5 | 2012-03-11 23:33:15 +0000 | [diff] [blame] | 347 | return(destination_image); |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 348 | } |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 349 | destination_channel=(PixelChannel) i; |
cristy | aa2c16c | 2012-03-25 22:21:35 +0000 | [diff] [blame] | 350 | channel_mask=(ChannelType) (channel_mask | ParseChannelOption(token)); |
cristy | 4b4a93a | 2012-03-21 23:15:56 +0000 | [diff] [blame] | 351 | if (LocaleCompare(token,"gray") == 0) |
cristy | fe88ede | 2012-03-22 00:34:48 +0000 | [diff] [blame] | 352 | (void) SetImageColorspace(destination_image,GRAYColorspace,exception); |
cristy | 542dd0b | 2012-03-18 19:10:09 +0000 | [diff] [blame] | 353 | if ((LocaleCompare(token,"black") == 0) || |
| 354 | (LocaleCompare(token,"c") == 0) || |
| 355 | (LocaleCompare(token,"cyan") == 0) || |
| 356 | (LocaleCompare(token,"k") == 0) || |
| 357 | (LocaleCompare(token,"m") == 0) || |
| 358 | (LocaleCompare(token,"magenta") == 0) || |
| 359 | (LocaleCompare(token,"y") == 0) || |
| 360 | (LocaleCompare(token,"yellow") == 0)) |
cristy | 322d07d | 2012-03-18 21:17:23 +0000 | [diff] [blame] | 361 | (void) SetImageColorspace(destination_image,CMYKColorspace,exception); |
cristy | eef2176 | 2012-03-18 19:12:58 +0000 | [diff] [blame] | 362 | if ((LocaleCompare(token,"Cb") == 0) || |
| 363 | (LocaleCompare(token,"Cr") == 0)) |
cristy | 322d07d | 2012-03-18 21:17:23 +0000 | [diff] [blame] | 364 | (void) SetImageColorspace(destination_image,YCbCrColorspace, |
| 365 | exception); |
| 366 | if (LocaleCompare(token,"alpha") == 0) |
| 367 | (void) SetImageAlpha(destination_image,OpaqueAlpha,exception); |
cristy | 25d5448 | 2012-03-18 22:33:54 +0000 | [diff] [blame] | 368 | if (i >= (ssize_t) GetPixelChannels(destination_image)) |
cristy | dfdb19e | 2012-03-21 22:22:24 +0000 | [diff] [blame] | 369 | (void) SetPixelMetaChannels(destination_image,(size_t) (i- |
| 370 | GetPixelChannels(destination_image)+1),exception); |
cristy | 3bb378a | 2012-03-08 14:38:52 +0000 | [diff] [blame] | 371 | GetMagickToken(p,&p,token); |
cristy | 8748f56 | 2012-03-08 13:58:34 +0000 | [diff] [blame] | 372 | break; |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 373 | } |
cristy | fe88ede | 2012-03-22 00:34:48 +0000 | [diff] [blame] | 374 | default: |
cristy | 3c21361 | 2012-03-16 01:14:48 +0000 | [diff] [blame] | 375 | break; |
cristy | 7ea920d | 2012-03-16 01:08:01 +0000 | [diff] [blame] | 376 | } |
cristy | 3bb378a | 2012-03-08 14:38:52 +0000 | [diff] [blame] | 377 | status=ChannelImage(destination_image,destination_channel,channel_op, |
| 378 | source_image,source_channel,ClampToQuantum(pixel),exception); |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 379 | if (status == MagickFalse) |
| 380 | { |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 381 | destination_image=DestroyImageList(destination_image); |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 382 | break; |
| 383 | } |
cristy | 4ee6040 | 2012-03-11 02:01:20 +0000 | [diff] [blame] | 384 | channels++; |
cristy | 8748f56 | 2012-03-08 13:58:34 +0000 | [diff] [blame] | 385 | if (channel_op == ExchangeChannelOp) |
| 386 | { |
cristy | 4ee6040 | 2012-03-11 02:01:20 +0000 | [diff] [blame] | 387 | status=ChannelImage(destination_image,source_channel,channel_op, |
| 388 | source_image,destination_channel,ClampToQuantum(pixel),exception); |
cristy | 8748f56 | 2012-03-08 13:58:34 +0000 | [diff] [blame] | 389 | if (status == MagickFalse) |
| 390 | { |
| 391 | destination_image=DestroyImageList(destination_image); |
| 392 | break; |
| 393 | } |
cristy | 4ee6040 | 2012-03-11 02:01:20 +0000 | [diff] [blame] | 394 | channels++; |
cristy | 8748f56 | 2012-03-08 13:58:34 +0000 | [diff] [blame] | 395 | } |
cristy | fe88ede | 2012-03-22 00:34:48 +0000 | [diff] [blame] | 396 | switch (channel_op) |
| 397 | { |
| 398 | case ExtractChannelOp: |
| 399 | { |
cristy | aa2c16c | 2012-03-25 22:21:35 +0000 | [diff] [blame] | 400 | channel_mask=(ChannelType) (channel_mask | (1 << destination_channel)); |
| 401 | destination_channel=(PixelChannel) (destination_channel+1); |
cristy | fe88ede | 2012-03-22 00:34:48 +0000 | [diff] [blame] | 402 | break; |
| 403 | } |
| 404 | default: |
| 405 | break; |
| 406 | } |
cristy | 5f257b2 | 2012-03-07 00:27:29 +0000 | [diff] [blame] | 407 | status=SetImageProgress(source_image,ChannelFxImageTag,p-expression, |
cristy | ab272ac | 2012-03-04 22:08:07 +0000 | [diff] [blame] | 408 | strlen(expression)); |
| 409 | if (status == MagickFalse) |
| 410 | break; |
cristy | a15140f | 2012-03-04 01:21:15 +0000 | [diff] [blame] | 411 | } |
cristy | fe88ede | 2012-03-22 00:34:48 +0000 | [diff] [blame] | 412 | SetPixelChannelMapMask(destination_image,channel_mask); |
| 413 | if ((channel_op == ExtractChannelOp) && (destination_channel == 1)) |
| 414 | (void) SetImageColorspace(destination_image,GRAYColorspace,exception); |
cristy | 4ee6040 | 2012-03-11 02:01:20 +0000 | [diff] [blame] | 415 | status=SetImageStorageClass(destination_image,DirectClass,exception); |
| 416 | if (status == MagickFalse) |
| 417 | { |
| 418 | destination_image=GetLastImageInList(destination_image); |
| 419 | return((Image *) NULL); |
| 420 | } |
cristy | 09ef5be | 2012-03-13 13:01:27 +0000 | [diff] [blame] | 421 | return(GetFirstImageInList(destination_image)); |
cristy | d04e7bf | 2012-03-03 19:19:12 +0000 | [diff] [blame] | 422 | } |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 423 | |
| 424 | /* |
| 425 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 426 | % % |
| 427 | % % |
| 428 | % % |
| 429 | % C o m b i n e I m a g e s % |
| 430 | % % |
| 431 | % % |
| 432 | % % |
| 433 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 434 | % |
| 435 | % CombineImages() combines one or more images into a single image. The |
| 436 | % grayscale value of the pixels of each image in the sequence is assigned in |
| 437 | % order to the specified channels of the combined image. The typical |
| 438 | % ordering would be image 1 => Red, 2 => Green, 3 => Blue, etc. |
| 439 | % |
| 440 | % The format of the CombineImages method is: |
| 441 | % |
cristy | 46f354c | 2012-07-04 13:31:29 +0000 | [diff] [blame] | 442 | % Image *CombineImages(const Image *images,const ColorspaceType colorspace, |
| 443 | % ExceptionInfo *exception) |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 444 | % |
| 445 | % A description of each parameter follows: |
| 446 | % |
cristy | 46f354c | 2012-07-04 13:31:29 +0000 | [diff] [blame] | 447 | % o images: the image sequence. |
| 448 | % |
| 449 | % o colorspace: the image colorspace. |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 450 | % |
| 451 | % o exception: return any errors or warnings in this structure. |
| 452 | % |
| 453 | */ |
cristy | 46f354c | 2012-07-04 13:31:29 +0000 | [diff] [blame] | 454 | MagickExport Image *CombineImages(const Image *image, |
| 455 | const ColorspaceType colorspace,ExceptionInfo *exception) |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 456 | { |
| 457 | #define CombineImageTag "Combine/Image" |
| 458 | |
| 459 | CacheView |
| 460 | *combine_view; |
| 461 | |
| 462 | Image |
| 463 | *combine_image; |
| 464 | |
| 465 | MagickBooleanType |
| 466 | status; |
| 467 | |
| 468 | MagickOffsetType |
| 469 | progress; |
| 470 | |
| 471 | ssize_t |
| 472 | y; |
| 473 | |
| 474 | /* |
| 475 | Ensure the image are the same size. |
| 476 | */ |
| 477 | assert(image != (const Image *) NULL); |
| 478 | assert(image->signature == MagickSignature); |
| 479 | if (image->debug != MagickFalse) |
| 480 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 481 | assert(exception != (ExceptionInfo *) NULL); |
| 482 | assert(exception->signature == MagickSignature); |
| 483 | combine_image=CloneImage(image,0,0,MagickTrue,exception); |
| 484 | if (combine_image == (Image *) NULL) |
| 485 | return((Image *) NULL); |
| 486 | if (SetImageStorageClass(combine_image,DirectClass,exception) == MagickFalse) |
| 487 | { |
| 488 | combine_image=DestroyImage(combine_image); |
| 489 | return((Image *) NULL); |
| 490 | } |
cristy | c1119af | 2012-04-16 15:28:23 +0000 | [diff] [blame] | 491 | if (IsGrayColorspace(image->colorspace) != MagickFalse) |
cristy | 46f354c | 2012-07-04 13:31:29 +0000 | [diff] [blame] | 492 | (void) SetImageColorspace(combine_image,RGBColorspace,exception); |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 493 | if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) |
| 494 | combine_image->matte=MagickTrue; |
| 495 | /* |
| 496 | Combine images. |
| 497 | */ |
| 498 | status=MagickTrue; |
| 499 | progress=0; |
cristy | db07095 | 2012-04-20 14:33:00 +0000 | [diff] [blame] | 500 | combine_view=AcquireAuthenticCacheView(combine_image,exception); |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 501 | for (y=0; y < (ssize_t) combine_image->rows; y++) |
| 502 | { |
| 503 | CacheView |
| 504 | *image_view; |
| 505 | |
| 506 | const Image |
| 507 | *next; |
| 508 | |
| 509 | Quantum |
| 510 | *pixels; |
| 511 | |
| 512 | register const Quantum |
| 513 | *restrict p; |
| 514 | |
| 515 | register Quantum |
| 516 | *restrict q; |
| 517 | |
| 518 | register ssize_t |
| 519 | i; |
| 520 | |
| 521 | if (status == MagickFalse) |
| 522 | continue; |
| 523 | pixels=GetCacheViewAuthenticPixels(combine_view,0,y,combine_image->columns, |
| 524 | 1,exception); |
| 525 | if (pixels == (Quantum *) NULL) |
| 526 | { |
| 527 | status=MagickFalse; |
| 528 | continue; |
| 529 | } |
| 530 | next=image; |
cristy | c1119af | 2012-04-16 15:28:23 +0000 | [diff] [blame] | 531 | for (i=0; i < (ssize_t) GetPixelChannels(combine_image); i++) |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 532 | { |
| 533 | PixelChannel |
| 534 | channel; |
| 535 | |
| 536 | PixelTrait |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 537 | traits; |
| 538 | |
| 539 | register ssize_t |
| 540 | x; |
| 541 | |
| 542 | if (next == (Image *) NULL) |
| 543 | continue; |
cristy | c1119af | 2012-04-16 15:28:23 +0000 | [diff] [blame] | 544 | channel=GetPixelChannelMapChannel(combine_image,i); |
| 545 | traits=GetPixelChannelMapTraits(combine_image,channel); |
| 546 | if (traits == UndefinedPixelTrait) |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 547 | continue; |
cristy | db07095 | 2012-04-20 14:33:00 +0000 | [diff] [blame] | 548 | image_view=AcquireVirtualCacheView(next,exception); |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 549 | p=GetCacheViewVirtualPixels(image_view,0,y,next->columns,1,exception); |
| 550 | if (p == (const Quantum *) NULL) |
| 551 | continue; |
| 552 | q=pixels; |
| 553 | for (x=0; x < (ssize_t) combine_image->columns; x++) |
| 554 | { |
cristy | f3381ae | 2012-07-13 16:30:09 +0000 | [diff] [blame] | 555 | if (x < (ssize_t) next->columns) |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 556 | { |
cristy | f3381ae | 2012-07-13 16:30:09 +0000 | [diff] [blame] | 557 | q[i]=GetPixelGray(next,p); |
| 558 | p+=GetPixelChannels(next); |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 559 | } |
| 560 | q+=GetPixelChannels(combine_image); |
| 561 | } |
| 562 | image_view=DestroyCacheView(image_view); |
| 563 | next=GetNextImageInList(next); |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 564 | } |
cristy | f3381ae | 2012-07-13 16:30:09 +0000 | [diff] [blame] | 565 | if (SyncCacheViewAuthenticPixels(combine_view,exception) == MagickFalse) |
| 566 | status=MagickFalse; |
| 567 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 568 | { |
| 569 | MagickBooleanType |
| 570 | proceed; |
| 571 | |
| 572 | proceed=SetImageProgress(image,CombineImageTag,progress++, |
| 573 | combine_image->rows); |
| 574 | if (proceed == MagickFalse) |
| 575 | status=MagickFalse; |
| 576 | } |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 577 | } |
| 578 | combine_view=DestroyCacheView(combine_view); |
| 579 | if (status == MagickFalse) |
| 580 | combine_image=DestroyImage(combine_image); |
cristy | 46f354c | 2012-07-04 13:31:29 +0000 | [diff] [blame] | 581 | (void) TransformImageColorspace(combine_image,colorspace,exception); |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 582 | return(combine_image); |
| 583 | } |
| 584 | |
| 585 | /* |
| 586 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 587 | % % |
| 588 | % % |
| 589 | % % |
| 590 | % S e p a r a t e I m a g e % |
| 591 | % % |
| 592 | % % |
| 593 | % % |
| 594 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 595 | % |
| 596 | % SeparateImage() separates a channel from the image and returns it as a |
| 597 | % grayscale image. |
| 598 | % |
| 599 | % The format of the SeparateImage method is: |
| 600 | % |
| 601 | % Image *SeparateImage(const Image *image,const ChannelType channel, |
| 602 | % ExceptionInfo *exception) |
| 603 | % |
| 604 | % A description of each parameter follows: |
| 605 | % |
| 606 | % o image: the image. |
| 607 | % |
| 608 | % o channel: the image channel. |
| 609 | % |
| 610 | % o exception: return any errors or warnings in this structure. |
| 611 | % |
| 612 | */ |
| 613 | MagickExport Image *SeparateImage(const Image *image, |
| 614 | const ChannelType channel_type,ExceptionInfo *exception) |
| 615 | { |
| 616 | #define GetChannelBit(mask,bit) (((size_t) (mask) >> (size_t) (bit)) & 0x01) |
| 617 | #define SeparateImageTag "Separate/Image" |
| 618 | |
| 619 | CacheView |
| 620 | *image_view, |
| 621 | *separate_view; |
| 622 | |
| 623 | Image |
| 624 | *separate_image; |
| 625 | |
| 626 | MagickBooleanType |
| 627 | status; |
| 628 | |
| 629 | MagickOffsetType |
| 630 | progress; |
| 631 | |
| 632 | ssize_t |
| 633 | y; |
| 634 | |
| 635 | /* |
| 636 | Initialize spread image attributes. |
| 637 | */ |
| 638 | assert(image != (Image *) NULL); |
| 639 | assert(image->signature == MagickSignature); |
| 640 | if (image->debug != MagickFalse) |
| 641 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 642 | assert(exception != (ExceptionInfo *) NULL); |
| 643 | assert(exception->signature == MagickSignature); |
| 644 | separate_image=CloneImage(image,image->columns,image->rows,MagickTrue, |
| 645 | exception); |
| 646 | if (separate_image == (Image *) NULL) |
| 647 | return((Image *) NULL); |
| 648 | if (SetImageStorageClass(separate_image,DirectClass,exception) == MagickFalse) |
| 649 | { |
| 650 | separate_image=DestroyImage(separate_image); |
| 651 | return((Image *) NULL); |
| 652 | } |
cristy | a06ee30 | 2012-04-29 20:56:04 +0000 | [diff] [blame] | 653 | separate_image->matte=MagickFalse; |
cristy | aa7a225 | 2012-08-15 12:19:00 +0000 | [diff] [blame] | 654 | (void) SetImageColorspace(separate_image,GRAYColorspace,exception); |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 655 | /* |
| 656 | Separate image. |
| 657 | */ |
| 658 | status=MagickTrue; |
| 659 | progress=0; |
cristy | db07095 | 2012-04-20 14:33:00 +0000 | [diff] [blame] | 660 | image_view=AcquireVirtualCacheView(image,exception); |
| 661 | separate_view=AcquireAuthenticCacheView(separate_image,exception); |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 662 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 663 | #pragma omp parallel for schedule(static) shared(progress,status) \ |
cristy | 4ee2b0c | 2012-05-15 00:30:35 +0000 | [diff] [blame] | 664 | dynamic_number_threads(image,image->columns,image->rows,1) |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 665 | #endif |
| 666 | for (y=0; y < (ssize_t) image->rows; y++) |
| 667 | { |
| 668 | register const Quantum |
| 669 | *restrict p; |
| 670 | |
| 671 | register Quantum |
| 672 | *restrict q; |
| 673 | |
| 674 | register ssize_t |
| 675 | x; |
| 676 | |
| 677 | if (status == MagickFalse) |
| 678 | continue; |
| 679 | p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception); |
| 680 | q=QueueCacheViewAuthenticPixels(separate_view,0,y,separate_image->columns,1, |
| 681 | exception); |
| 682 | if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL)) |
| 683 | { |
| 684 | status=MagickFalse; |
| 685 | continue; |
| 686 | } |
| 687 | for (x=0; x < (ssize_t) image->columns; x++) |
| 688 | { |
| 689 | register ssize_t |
| 690 | i; |
| 691 | |
| 692 | if (GetPixelMask(image,p) != 0) |
| 693 | { |
| 694 | p+=GetPixelChannels(image); |
| 695 | q+=GetPixelChannels(separate_image); |
| 696 | continue; |
| 697 | } |
| 698 | SetPixelChannel(separate_image,GrayPixelChannel,0,q); |
| 699 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
| 700 | { |
cristy | aa7a225 | 2012-08-15 12:19:00 +0000 | [diff] [blame] | 701 | double |
| 702 | pixel; |
| 703 | |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 704 | PixelChannel |
| 705 | channel; |
| 706 | |
| 707 | PixelTrait |
| 708 | traits; |
| 709 | |
| 710 | channel=GetPixelChannelMapChannel(image,i); |
| 711 | traits=GetPixelChannelMapTraits(image,channel); |
| 712 | if ((traits == UndefinedPixelTrait) || |
| 713 | (GetChannelBit(channel_type,channel) == 0)) |
| 714 | continue; |
cristy | aa7a225 | 2012-08-15 12:19:00 +0000 | [diff] [blame] | 715 | pixel=p[i]; |
| 716 | if (IssRGBColorspace(image->colorspace) != MagickFalse) |
| 717 | pixel=InversesRGBCompandor(pixel); |
| 718 | SetPixelChannel(separate_image,GrayPixelChannel,ClampToQuantum(pixel), |
| 719 | q); |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 720 | } |
| 721 | p+=GetPixelChannels(image); |
| 722 | q+=GetPixelChannels(separate_image); |
| 723 | } |
| 724 | if (SyncCacheViewAuthenticPixels(separate_view,exception) == MagickFalse) |
| 725 | status=MagickFalse; |
| 726 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 727 | { |
| 728 | MagickBooleanType |
| 729 | proceed; |
| 730 | |
| 731 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 732 | #pragma omp critical (MagickCore_SeparateImage) |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 733 | #endif |
| 734 | proceed=SetImageProgress(image,SeparateImageTag,progress++,image->rows); |
| 735 | if (proceed == MagickFalse) |
| 736 | status=MagickFalse; |
| 737 | } |
| 738 | } |
| 739 | separate_view=DestroyCacheView(separate_view); |
| 740 | image_view=DestroyCacheView(image_view); |
| 741 | return(separate_image); |
| 742 | } |
| 743 | |
| 744 | /* |
| 745 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 746 | % % |
| 747 | % % |
| 748 | % % |
| 749 | % S e p a r a t e I m a g e s % |
| 750 | % % |
| 751 | % % |
| 752 | % % |
| 753 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 754 | % |
| 755 | % SeparateImages() returns a separate grayscale image for each channel |
| 756 | % specified. |
| 757 | % |
| 758 | % The format of the SeparateImages method is: |
| 759 | % |
cristy | dfdb19e | 2012-03-21 22:22:24 +0000 | [diff] [blame] | 760 | % Image *SeparateImages(const Image *image,ExceptionInfo *exception) |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 761 | % |
| 762 | % A description of each parameter follows: |
| 763 | % |
| 764 | % o image: the image. |
| 765 | % |
| 766 | % o exception: return any errors or warnings in this structure. |
| 767 | % |
| 768 | */ |
cristy | dfdb19e | 2012-03-21 22:22:24 +0000 | [diff] [blame] | 769 | MagickExport Image *SeparateImages(const Image *image,ExceptionInfo *exception) |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 770 | { |
| 771 | Image |
| 772 | *images, |
| 773 | *separate_image; |
| 774 | |
| 775 | register ssize_t |
| 776 | i; |
| 777 | |
| 778 | assert(image != (Image *) NULL); |
| 779 | assert(image->signature == MagickSignature); |
| 780 | if (image->debug != MagickFalse) |
| 781 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 782 | images=NewImageList(); |
| 783 | for (i=0; i < (ssize_t) GetPixelChannels(image); i++) |
| 784 | { |
| 785 | PixelChannel |
| 786 | channel; |
| 787 | |
| 788 | PixelTrait |
| 789 | traits; |
| 790 | |
| 791 | channel=GetPixelChannelMapChannel(image,i); |
| 792 | traits=GetPixelChannelMapTraits(image,channel); |
| 793 | if ((traits == UndefinedPixelTrait) || |
| 794 | ((traits & UpdatePixelTrait) == 0)) |
| 795 | continue; |
| 796 | separate_image=SeparateImage(image,(ChannelType) (1 << channel),exception); |
| 797 | if (separate_image != (Image *) NULL) |
| 798 | AppendImageToList(&images,separate_image); |
| 799 | } |
cristy | e5ef2ce | 2012-04-29 21:14:32 +0000 | [diff] [blame] | 800 | if (images == (Image *) NULL) |
| 801 | images=SeparateImage(image,UndefinedChannel,exception); |
cristy | 0c64372 | 2012-03-05 19:18:42 +0000 | [diff] [blame] | 802 | return(images); |
| 803 | } |