2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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 %
13 % MagickCore Image Channel Methods %
20 % Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 #include "MagickCore/studio.h"
44 #include "MagickCore/image.h"
45 #include "MagickCore/list.h"
46 #include "MagickCore/log.h"
47 #include "MagickCore/monitor.h"
48 #include "MagickCore/monitor-private.h"
49 #include "MagickCore/option.h"
50 #include "MagickCore/pixel-accessor.h"
51 #include "MagickCore/string-private.h"
52 #include "MagickCore/token.h"
53 #include "MagickCore/utility.h"
54 #include "MagickCore/version.h"
57 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
61 % C h a n n e l F x I m a g e %
65 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
67 % ChannelFxImage() applies a channel expression to the specified image. The
68 % expression consists of one or more channels, either mnemonic or numeric (e.g.
69 % red, 1), separated by actions as follows:
71 % <=> exchange two channels (e.g. red<=>blue)
72 % => copy one channel to another channel (e.g. red=>green)
73 % = assign a constant value to a channel (e.g. red=50%)
74 % , write new image channels in the specified order (e.g. red, green)
75 % | add a new output image for the next set of channel operations
76 % ; move to the next input image for the source of channel data
78 % For example, to create 3 grayscale images from the red, green, and blue
79 % channels of an image, use:
81 % -channel-fx "red; green; blue"
83 % A channel without an operation symbol implies separate (i.e, semicolon).
85 % The format of the ChannelFxImage method is:
87 % Image *ChannelFxImage(const Image *image,const char *expression,
88 % ExceptionInfo *exception)
90 % A description of each parameter follows:
94 % o expression: A channel expression.
96 % o exception: return any errors or warnings in this structure.
108 static inline size_t MagickMin(const size_t x,const size_t y)
115 static MagickBooleanType ChannelImage(Image *destination_image,
116 const PixelChannel destination_channel,const ChannelFx channel_op,
117 const Image *source_image,const PixelChannel source_channel,
118 const Quantum pixel,ExceptionInfo *exception)
134 source_view=AcquireCacheView(source_image);
135 destination_view=AcquireCacheView(destination_image);
136 height=MagickMin(source_image->rows,destination_image->rows);
137 #if defined(MAGICKCORE_OPENMP_SUPPORT)
138 #pragma omp parallel for schedule(static) shared(status)
140 for (y=0; y < (ssize_t) height; y++)
146 register const Quantum
158 if (status == MagickFalse)
160 p=GetCacheViewVirtualPixels(source_view,0,y,source_image->columns,1,
162 q=GetCacheViewAuthenticPixels(destination_view,0,y,
163 destination_image->columns,1,exception);
164 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
169 destination_traits=GetPixelChannelMapTraits(destination_image,
170 destination_channel);
171 source_traits=GetPixelChannelMapTraits(source_image,source_channel);
172 if ((destination_traits == UndefinedPixelTrait) ||
173 (source_traits == UndefinedPixelTrait))
175 width=MagickMin(source_image->columns,destination_image->columns);
176 for (x=0; x < (ssize_t) width; x++)
178 if (channel_op == AssignChannelOp)
179 SetPixelChannel(destination_image,destination_channel,pixel,q);
181 SetPixelChannel(destination_image,destination_channel,
182 GetPixelChannel(source_image,source_channel,p),q);
183 p+=GetPixelChannels(source_image);
184 q+=GetPixelChannels(destination_image);
186 if (SyncCacheViewAuthenticPixels(destination_view,exception) == MagickFalse)
189 destination_view=DestroyCacheView(destination_view);
190 source_view=DestroyCacheView(source_view);
194 MagickExport Image *ChannelFxImage(const Image *image,const char *expression,
195 ExceptionInfo *exception)
197 #define ChannelFxImageTag "ChannelFx/Image"
206 token[MaxTextExtent];
230 assert(image != (Image *) NULL);
231 assert(image->signature == MagickSignature);
232 if (image->debug != MagickFalse)
233 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
234 assert(exception != (ExceptionInfo *) NULL);
235 assert(exception->signature == MagickSignature);
237 destination_image=CloneImage(source_image,0,0,MagickTrue,exception);
238 if (destination_image == (Image *) NULL)
239 return((Image *) NULL);
240 if (destination_image->colorspace == GRAYColorspace)
241 destination_image->colorspace=RGBColorspace;
242 if (expression == (const char *) NULL)
243 return(destination_image);
244 destination_channel=RedPixelChannel;
245 channel_mask=UndefinedChannel;
247 p=(char *) expression;
248 GetMagickToken(p,&p,token);
249 channel_op=ExtractChannelOp;
250 for (channels=0; *token != '\0'; )
256 Interpret channel expression.
260 destination_channel=(PixelChannel) ((ssize_t) destination_channel+1);
261 GetMagickToken(p,&p,token);
265 if (GetNextImageInList(source_image) != (Image *) NULL)
266 source_image=GetNextImageInList(source_image);
268 source_image=GetFirstImageInList(source_image);
269 GetMagickToken(p,&p,token);
276 SetPixelChannelMapMask(destination_image,channel_mask);
277 if ((channel_op == ExtractChannelOp) && (destination_channel == 1))
278 (void) SetImageColorspace(destination_image,GRAYColorspace,exception);
279 status=SetImageStorageClass(destination_image,DirectClass,exception);
280 if (status == MagickFalse)
282 destination_image=DestroyImageList(destination_image);
283 return(destination_image);
285 canvas=CloneImage(source_image,0,0,MagickTrue,exception);
286 if (canvas == (Image *) NULL)
288 destination_image=DestroyImageList(destination_image);
289 return(destination_image);
291 if (canvas->colorspace == GRAYColorspace)
292 canvas->colorspace=RGBColorspace;
293 AppendImageToList(&destination_image,canvas);
294 destination_image=GetLastImageInList(destination_image);
295 GetMagickToken(p,&p,token);
297 destination_channel=RedPixelChannel;
298 channel_mask=UndefinedChannel;
300 i=ParsePixelChannelOption(token);
303 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
304 "UnrecognizedChannelType","`%s'",token);
305 destination_image=DestroyImageList(destination_image);
306 return(destination_image);
308 source_channel=(PixelChannel) i;
309 channel_op=ExtractChannelOp;
310 GetMagickToken(p,&p,token);
313 channel_op=ExchangeChannelOp;
314 GetMagickToken(p,&p,token);
318 if (channel_op != ExchangeChannelOp)
319 channel_op=AssignChannelOp;
320 GetMagickToken(p,&p,token);
324 if (channel_op != ExchangeChannelOp)
325 channel_op=TransferChannelOp;
326 GetMagickToken(p,&p,token);
330 case AssignChannelOp:
332 pixel=StringToDoubleInterval(token,(double) QuantumRange+1.0);
333 GetMagickToken(p,&p,token);
336 case ExchangeChannelOp:
337 case TransferChannelOp:
339 i=ParsePixelChannelOption(token);
342 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
343 "UnrecognizedChannelType","`%s'",token);
344 destination_image=DestroyImageList(destination_image);
345 return(destination_image);
347 destination_channel=(PixelChannel) i;
348 channel_mask=(ChannelType) (channel_mask | ParseChannelOption(token));
349 if (LocaleCompare(token,"gray") == 0)
350 (void) SetImageColorspace(destination_image,GRAYColorspace,exception);
351 if ((LocaleCompare(token,"black") == 0) ||
352 (LocaleCompare(token,"c") == 0) ||
353 (LocaleCompare(token,"cyan") == 0) ||
354 (LocaleCompare(token,"k") == 0) ||
355 (LocaleCompare(token,"m") == 0) ||
356 (LocaleCompare(token,"magenta") == 0) ||
357 (LocaleCompare(token,"y") == 0) ||
358 (LocaleCompare(token,"yellow") == 0))
359 (void) SetImageColorspace(destination_image,CMYKColorspace,exception);
360 if ((LocaleCompare(token,"Cb") == 0) ||
361 (LocaleCompare(token,"Cr") == 0))
362 (void) SetImageColorspace(destination_image,YCbCrColorspace,
364 if (LocaleCompare(token,"alpha") == 0)
365 (void) SetImageAlpha(destination_image,OpaqueAlpha,exception);
366 if (i >= (ssize_t) GetPixelChannels(destination_image))
367 (void) SetPixelMetaChannels(destination_image,(size_t) (i-
368 GetPixelChannels(destination_image)+1),exception);
369 GetMagickToken(p,&p,token);
375 status=ChannelImage(destination_image,destination_channel,channel_op,
376 source_image,source_channel,ClampToQuantum(pixel),exception);
377 if (status == MagickFalse)
379 destination_image=DestroyImageList(destination_image);
383 if (channel_op == ExchangeChannelOp)
385 status=ChannelImage(destination_image,source_channel,channel_op,
386 source_image,destination_channel,ClampToQuantum(pixel),exception);
387 if (status == MagickFalse)
389 destination_image=DestroyImageList(destination_image);
396 case ExtractChannelOp:
398 channel_mask=(ChannelType) (channel_mask | (1 << destination_channel));
399 destination_channel=(PixelChannel) (destination_channel+1);
405 status=SetImageProgress(source_image,ChannelFxImageTag,p-expression,
407 if (status == MagickFalse)
410 SetPixelChannelMapMask(destination_image,channel_mask);
411 if ((channel_op == ExtractChannelOp) && (destination_channel == 1))
412 (void) SetImageColorspace(destination_image,GRAYColorspace,exception);
413 status=SetImageStorageClass(destination_image,DirectClass,exception);
414 if (status == MagickFalse)
416 destination_image=GetLastImageInList(destination_image);
417 return((Image *) NULL);
419 return(GetFirstImageInList(destination_image));
423 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
427 % C o m b i n e I m a g e s %
431 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
433 % CombineImages() combines one or more images into a single image. The
434 % grayscale value of the pixels of each image in the sequence is assigned in
435 % order to the specified channels of the combined image. The typical
436 % ordering would be image 1 => Red, 2 => Green, 3 => Blue, etc.
438 % The format of the CombineImages method is:
440 % Image *CombineImages(const Image *image,ExceptionInfo *exception)
442 % A description of each parameter follows:
444 % o image: the image.
446 % o exception: return any errors or warnings in this structure.
449 MagickExport Image *CombineImages(const Image *image,ExceptionInfo *exception)
451 #define CombineImageTag "Combine/Image"
469 Ensure the image are the same size.
471 assert(image != (const Image *) NULL);
472 assert(image->signature == MagickSignature);
473 if (image->debug != MagickFalse)
474 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
475 assert(exception != (ExceptionInfo *) NULL);
476 assert(exception->signature == MagickSignature);
477 combine_image=CloneImage(image,0,0,MagickTrue,exception);
478 if (combine_image == (Image *) NULL)
479 return((Image *) NULL);
480 if (SetImageStorageClass(combine_image,DirectClass,exception) == MagickFalse)
482 combine_image=DestroyImage(combine_image);
483 return((Image *) NULL);
485 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
486 combine_image->matte=MagickTrue;
492 combine_view=AcquireCacheView(combine_image);
493 for (y=0; y < (ssize_t) combine_image->rows; y++)
504 register const Quantum
513 if (status == MagickFalse)
515 pixels=GetCacheViewAuthenticPixels(combine_view,0,y,combine_image->columns,
517 if (pixels == (Quantum *) NULL)
523 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
535 if (next == (Image *) NULL)
537 channel=GetPixelChannelMapChannel(image,i);
538 traits=GetPixelChannelMapTraits(image,channel);
539 combine_traits=GetPixelChannelMapTraits(combine_image,channel);
540 if ((traits == UndefinedPixelTrait) ||
541 (combine_traits == UndefinedPixelTrait))
543 image_view=AcquireCacheView(next);
544 p=GetCacheViewVirtualPixels(image_view,0,y,next->columns,1,exception);
545 if (p == (const Quantum *) NULL)
548 for (x=0; x < (ssize_t) combine_image->columns; x++)
550 if (x < (ssize_t) image->columns)
552 q[i]=GetPixelGray(image,p);
553 p+=GetPixelChannels(image);
555 q+=GetPixelChannels(combine_image);
557 image_view=DestroyCacheView(image_view);
558 next=GetNextImageInList(next);
559 if (SyncCacheViewAuthenticPixels(combine_view,exception) == MagickFalse)
561 if (image->progress_monitor != (MagickProgressMonitor) NULL)
566 proceed=SetImageProgress(image,CombineImageTag,progress++,
567 combine_image->rows);
568 if (proceed == MagickFalse)
573 combine_view=DestroyCacheView(combine_view);
574 if (status == MagickFalse)
575 combine_image=DestroyImage(combine_image);
576 return(combine_image);
580 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
584 % S e p a r a t e I m a g e %
588 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
590 % SeparateImage() separates a channel from the image and returns it as a
593 % The format of the SeparateImage method is:
595 % Image *SeparateImage(const Image *image,const ChannelType channel,
596 % ExceptionInfo *exception)
598 % A description of each parameter follows:
600 % o image: the image.
602 % o channel: the image channel.
604 % o exception: return any errors or warnings in this structure.
607 MagickExport Image *SeparateImage(const Image *image,
608 const ChannelType channel_type,ExceptionInfo *exception)
610 #define GetChannelBit(mask,bit) (((size_t) (mask) >> (size_t) (bit)) & 0x01)
611 #define SeparateImageTag "Separate/Image"
630 Initialize spread image attributes.
632 assert(image != (Image *) NULL);
633 assert(image->signature == MagickSignature);
634 if (image->debug != MagickFalse)
635 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
636 assert(exception != (ExceptionInfo *) NULL);
637 assert(exception->signature == MagickSignature);
638 separate_image=CloneImage(image,image->columns,image->rows,MagickTrue,
640 if (separate_image == (Image *) NULL)
641 return((Image *) NULL);
642 if (SetImageStorageClass(separate_image,DirectClass,exception) == MagickFalse)
644 separate_image=DestroyImage(separate_image);
645 return((Image *) NULL);
647 separate_image->colorspace=GRAYColorspace;
653 image_view=AcquireCacheView(image);
654 separate_view=AcquireCacheView(separate_image);
655 #if defined(MAGICKCORE_OPENMP_SUPPORT)
656 #pragma omp parallel for schedule(static) shared(progress,status)
658 for (y=0; y < (ssize_t) image->rows; y++)
660 register const Quantum
669 if (status == MagickFalse)
671 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
672 q=QueueCacheViewAuthenticPixels(separate_view,0,y,separate_image->columns,1,
674 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
679 for (x=0; x < (ssize_t) image->columns; x++)
684 if (GetPixelMask(image,p) != 0)
686 p+=GetPixelChannels(image);
687 q+=GetPixelChannels(separate_image);
690 SetPixelChannel(separate_image,GrayPixelChannel,0,q);
691 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
699 channel=GetPixelChannelMapChannel(image,i);
700 traits=GetPixelChannelMapTraits(image,channel);
701 if ((traits == UndefinedPixelTrait) ||
702 (GetChannelBit(channel_type,channel) == 0))
704 SetPixelChannel(separate_image,GrayPixelChannel,p[i],q);
706 p+=GetPixelChannels(image);
707 q+=GetPixelChannels(separate_image);
709 if (SyncCacheViewAuthenticPixels(separate_view,exception) == MagickFalse)
711 if (image->progress_monitor != (MagickProgressMonitor) NULL)
716 #if defined(MAGICKCORE_OPENMP_SUPPORT)
717 #pragma omp critical (MagickCore_SeparateImage)
719 proceed=SetImageProgress(image,SeparateImageTag,progress++,image->rows);
720 if (proceed == MagickFalse)
724 separate_view=DestroyCacheView(separate_view);
725 image_view=DestroyCacheView(image_view);
726 return(separate_image);
730 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
734 % S e p a r a t e I m a g e s %
738 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
740 % SeparateImages() returns a separate grayscale image for each channel
743 % The format of the SeparateImages method is:
745 % Image *SeparateImages(const Image *image,ExceptionInfo *exception)
747 % A description of each parameter follows:
749 % o image: the image.
751 % o exception: return any errors or warnings in this structure.
754 MagickExport Image *SeparateImages(const Image *image,ExceptionInfo *exception)
763 assert(image != (Image *) NULL);
764 assert(image->signature == MagickSignature);
765 if (image->debug != MagickFalse)
766 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
767 images=NewImageList();
768 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
776 channel=GetPixelChannelMapChannel(image,i);
777 traits=GetPixelChannelMapTraits(image,channel);
778 if ((traits == UndefinedPixelTrait) ||
779 ((traits & UpdatePixelTrait) == 0))
781 separate_image=SeparateImage(image,(ChannelType) (1 << channel),exception);
782 if (separate_image != (Image *) NULL)
783 AppendImageToList(&images,separate_image);