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-2013 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/colorspace-private.h"
45 #include "MagickCore/image.h"
46 #include "MagickCore/list.h"
47 #include "MagickCore/log.h"
48 #include "MagickCore/monitor.h"
49 #include "MagickCore/monitor-private.h"
50 #include "MagickCore/option.h"
51 #include "MagickCore/pixel-accessor.h"
52 #include "MagickCore/resource_.h"
53 #include "MagickCore/string-private.h"
54 #include "MagickCore/thread-private.h"
55 #include "MagickCore/token.h"
56 #include "MagickCore/utility.h"
57 #include "MagickCore/version.h"
60 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
64 % C h a n n e l F x I m a g e %
68 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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:
74 % <=> exchange two channels (e.g. red<=>blue)
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)
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
81 % For example, to create 3 grayscale images from the red, green, and blue
82 % channels of an image, use:
84 % -channel-fx "red; green; blue"
86 % A channel without an operation symbol implies separate (i.e, semicolon).
88 % The format of the ChannelFxImage method is:
90 % Image *ChannelFxImage(const Image *image,const char *expression,
91 % ExceptionInfo *exception)
93 % A description of each parameter follows:
97 % o expression: A channel expression.
99 % o exception: return any errors or warnings in this structure.
111 static inline size_t MagickMin(const size_t x,const size_t y)
118 static MagickBooleanType ChannelImage(Image *destination_image,
119 const PixelChannel destination_channel,const ChannelFx channel_op,
120 const Image *source_image,const PixelChannel source_channel,
121 const Quantum pixel,ExceptionInfo *exception)
138 source_view=AcquireVirtualCacheView(source_image,exception);
139 destination_view=AcquireAuthenticCacheView(destination_image,exception);
140 height=MagickMin(source_image->rows,destination_image->rows);
141 width=MagickMin(source_image->columns,destination_image->columns);
142 #if defined(MAGICKCORE_OPENMP_SUPPORT)
143 #pragma omp parallel for schedule(static,4) shared(status) \
144 magick_threads(source_image,source_image,height,1)
146 for (y=0; y < (ssize_t) height; y++)
152 register const Quantum
161 if (status == MagickFalse)
163 p=GetCacheViewVirtualPixels(source_view,0,y,source_image->columns,1,
165 q=GetCacheViewAuthenticPixels(destination_view,0,y,
166 destination_image->columns,1,exception);
167 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
172 destination_traits=GetPixelChannelTraits(destination_image,
173 destination_channel);
174 source_traits=GetPixelChannelTraits(source_image,source_channel);
175 if ((destination_traits == UndefinedPixelTrait) ||
176 (source_traits == UndefinedPixelTrait))
178 for (x=0; x < (ssize_t) width; x++)
180 if (channel_op == AssignChannelOp)
181 SetPixelChannel(destination_image,destination_channel,pixel,q);
183 SetPixelChannel(destination_image,destination_channel,
184 GetPixelChannel(source_image,source_channel,p),q);
185 p+=GetPixelChannels(source_image);
186 q+=GetPixelChannels(destination_image);
188 if (SyncCacheViewAuthenticPixels(destination_view,exception) == MagickFalse)
191 destination_view=DestroyCacheView(destination_view);
192 source_view=DestroyCacheView(source_view);
196 MagickExport Image *ChannelFxImage(const Image *image,const char *expression,
197 ExceptionInfo *exception)
199 #define ChannelFxImageTag "ChannelFx/Image"
208 token[MaxTextExtent];
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);
239 destination_image=CloneImage(source_image,0,0,MagickTrue,exception);
240 if (destination_image == (Image *) NULL)
241 return((Image *) NULL);
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'; )
259 Interpret channel expression.
261 delete_channel=MagickFalse;
266 delete_channel=MagickTrue;
267 GetMagickToken(p,&p,token);
272 destination_channel=(PixelChannel) ((ssize_t) destination_channel+1);
273 GetMagickToken(p,&p,token);
278 if (GetNextImageInList(source_image) != (Image *) NULL)
279 source_image=GetNextImageInList(source_image);
281 source_image=GetFirstImageInList(source_image);
282 GetMagickToken(p,&p,token);
290 SetPixelChannelMask(destination_image,channel_mask);
291 if ((channel_op == ExtractChannelOp) && (channels == 1))
292 (void) SetImageColorspace(destination_image,GRAYColorspace,exception);
293 status=SetImageStorageClass(destination_image,DirectClass,exception);
294 if (status == MagickFalse)
296 destination_image=DestroyImageList(destination_image);
297 return(destination_image);
299 canvas=CloneImage(source_image,0,0,MagickTrue,exception);
300 if (canvas == (Image *) NULL)
302 destination_image=DestroyImageList(destination_image);
303 return(destination_image);
305 AppendImageToList(&destination_image,canvas);
306 destination_image=GetLastImageInList(destination_image);
307 GetMagickToken(p,&p,token);
309 destination_channel=RedPixelChannel;
310 channel_mask=UndefinedChannel;
316 i=ParsePixelChannelOption(token);
319 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
320 "UnrecognizedChannelType","`%s'",token);
321 destination_image=DestroyImageList(destination_image);
322 return(destination_image);
324 source_channel=(PixelChannel) i;
325 channel_op=ExtractChannelOp;
326 GetMagickToken(p,&p,token);
329 channel_op=ExchangeChannelOp;
330 GetMagickToken(p,&p,token);
334 if (channel_op != ExchangeChannelOp)
335 channel_op=AssignChannelOp;
336 GetMagickToken(p,&p,token);
340 if (channel_op != ExchangeChannelOp)
341 channel_op=TransferChannelOp;
342 GetMagickToken(p,&p,token);
346 case AssignChannelOp:
348 pixel=StringToDoubleInterval(token,(double) QuantumRange+1.0);
349 GetMagickToken(p,&p,token);
352 case ExchangeChannelOp:
353 case TransferChannelOp:
355 i=ParsePixelChannelOption(token);
358 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
359 "UnrecognizedChannelType","`%s'",token);
360 destination_image=DestroyImageList(destination_image);
361 return(destination_image);
363 destination_channel=(PixelChannel) i;
364 switch (destination_channel)
366 case RedPixelChannel:
367 case GreenPixelChannel:
368 case BluePixelChannel:
369 case BlackPixelChannel:
370 case IndexPixelChannel:
372 case AlphaPixelChannel:
374 destination_image->alpha_trait=BlendPixelTrait;
377 case ReadMaskPixelChannel:
379 destination_image->read_mask=MagickTrue;
382 case WriteMaskPixelChannel:
384 destination_image->write_mask=MagickTrue;
387 case MetaPixelChannel:
390 (void) SetPixelMetaChannels(destination_image,(size_t) (i-
391 GetPixelChannels(destination_image)+1),exception);
395 channel_mask=(ChannelType) (channel_mask | ParseChannelOption(token));
396 if (((channels >= 1) || (destination_channel >= 1)) &&
397 (IsGrayColorspace(destination_image->colorspace) != MagickFalse))
398 (void) SetImageColorspace(destination_image,sRGBColorspace,exception);
399 GetMagickToken(p,&p,token);
405 status=ChannelImage(destination_image,destination_channel,channel_op,
406 source_image,source_channel,ClampToQuantum(pixel),exception);
407 if (status == MagickFalse)
409 destination_image=DestroyImageList(destination_image);
413 if (channel_op == ExchangeChannelOp)
415 status=ChannelImage(destination_image,source_channel,channel_op,
416 source_image,destination_channel,ClampToQuantum(pixel),exception);
417 if (status == MagickFalse)
419 destination_image=DestroyImageList(destination_image);
426 case ExtractChannelOp:
428 if (delete_channel == MagickFalse)
429 channel_mask=(ChannelType) (channel_mask |
430 (1 << destination_channel));
432 channel_mask=(ChannelType) (channel_mask &~
433 (1 << destination_channel));
434 destination_channel=(PixelChannel) (destination_channel+1);
440 status=SetImageProgress(source_image,ChannelFxImageTag,p-expression,
442 if (status == MagickFalse)
445 SetPixelChannelMask(destination_image,channel_mask);
446 if ((channel_op == ExtractChannelOp) && (channels == 1))
447 (void) SetImageColorspace(destination_image,GRAYColorspace,exception);
448 status=SetImageStorageClass(destination_image,DirectClass,exception);
449 if (status == MagickFalse)
451 destination_image=GetLastImageInList(destination_image);
452 return((Image *) NULL);
454 return(GetFirstImageInList(destination_image));
458 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
462 % C o m b i n e I m a g e s %
466 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
468 % CombineImages() combines one or more images into a single image. The
469 % grayscale value of the pixels of each image in the sequence is assigned in
470 % order to the specified channels of the combined image. The typical
471 % ordering would be image 1 => Red, 2 => Green, 3 => Blue, etc.
473 % The format of the CombineImages method is:
475 % Image *CombineImages(const Image *images,const ColorspaceType colorspace,
476 % ExceptionInfo *exception)
478 % A description of each parameter follows:
480 % o images: the image sequence.
482 % o colorspace: the image colorspace.
484 % o exception: return any errors or warnings in this structure.
487 MagickExport Image *CombineImages(const Image *image,
488 const ColorspaceType colorspace,ExceptionInfo *exception)
490 #define CombineImageTag "Combine/Image"
508 Ensure the image are the same size.
510 assert(image != (const Image *) NULL);
511 assert(image->signature == MagickSignature);
512 if (image->debug != MagickFalse)
513 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
514 assert(exception != (ExceptionInfo *) NULL);
515 assert(exception->signature == MagickSignature);
516 combine_image=CloneImage(image,0,0,MagickTrue,exception);
517 if (combine_image == (Image *) NULL)
518 return((Image *) NULL);
519 if (SetImageStorageClass(combine_image,DirectClass,exception) == MagickFalse)
521 combine_image=DestroyImage(combine_image);
522 return((Image *) NULL);
524 (void) SetImageColorspace(combine_image,colorspace,exception);
525 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
526 combine_image->alpha_trait=BlendPixelTrait;
532 combine_view=AcquireAuthenticCacheView(combine_image,exception);
533 for (y=0; y < (ssize_t) combine_image->rows; y++)
544 register const Quantum
553 if (status == MagickFalse)
555 pixels=GetCacheViewAuthenticPixels(combine_view,0,y,combine_image->columns,
557 if (pixels == (Quantum *) NULL)
563 for (i=0; i < (ssize_t) GetPixelChannels(combine_image); i++)
568 PixelChannel channel=GetPixelChannelChannel(combine_image,i);
569 PixelTrait traits=GetPixelChannelTraits(combine_image,channel);
570 if (traits == UndefinedPixelTrait)
572 if (next == (Image *) NULL)
574 image_view=AcquireVirtualCacheView(next,exception);
575 p=GetCacheViewVirtualPixels(image_view,0,y,next->columns,1,exception);
576 if (p == (const Quantum *) NULL)
579 for (x=0; x < (ssize_t) combine_image->columns; x++)
581 if (x < (ssize_t) next->columns)
583 q[i]=GetPixelGray(next,p);
584 p+=GetPixelChannels(next);
586 q+=GetPixelChannels(combine_image);
588 image_view=DestroyCacheView(image_view);
589 next=GetNextImageInList(next);
591 if (SyncCacheViewAuthenticPixels(combine_view,exception) == MagickFalse)
593 if (image->progress_monitor != (MagickProgressMonitor) NULL)
598 proceed=SetImageProgress(image,CombineImageTag,progress++,
599 combine_image->rows);
600 if (proceed == MagickFalse)
604 combine_view=DestroyCacheView(combine_view);
605 if (status == MagickFalse)
606 combine_image=DestroyImage(combine_image);
607 return(combine_image);
611 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
615 % S e p a r a t e I m a g e %
619 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
621 % SeparateImage() separates a channel from the image and returns it as a
624 % The format of the SeparateImage method is:
626 % Image *SeparateImage(const Image *image,const ChannelType channel,
627 % ExceptionInfo *exception)
629 % A description of each parameter follows:
631 % o image: the image.
633 % o channel: the image channel.
635 % o exception: return any errors or warnings in this structure.
638 MagickExport Image *SeparateImage(const Image *image,
639 const ChannelType channel_type,ExceptionInfo *exception)
641 #define GetChannelBit(mask,bit) (((size_t) (mask) >> (size_t) (bit)) & 0x01)
642 #define SeparateImageTag "Separate/Image"
661 Initialize separate image attributes.
663 assert(image != (Image *) NULL);
664 assert(image->signature == MagickSignature);
665 if (image->debug != MagickFalse)
666 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
667 assert(exception != (ExceptionInfo *) NULL);
668 assert(exception->signature == MagickSignature);
669 separate_image=CloneImage(image,image->columns,image->rows,MagickTrue,
671 if (separate_image == (Image *) NULL)
672 return((Image *) NULL);
673 if (SetImageStorageClass(separate_image,DirectClass,exception) == MagickFalse)
675 separate_image=DestroyImage(separate_image);
676 return((Image *) NULL);
678 (void) SetImageColorspace(separate_image,GRAYColorspace,exception);
679 separate_image->alpha_trait=UndefinedPixelTrait;
685 image_view=AcquireVirtualCacheView(image,exception);
686 separate_view=AcquireAuthenticCacheView(separate_image,exception);
687 #if defined(MAGICKCORE_OPENMP_SUPPORT)
688 #pragma omp parallel for schedule(static,4) shared(progress,status) \
689 magick_threads(image,image,image->rows,1)
691 for (y=0; y < (ssize_t) image->rows; y++)
693 register const Quantum
702 if (status == MagickFalse)
704 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
705 q=QueueCacheViewAuthenticPixels(separate_view,0,y,separate_image->columns,1,
707 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
712 for (x=0; x < (ssize_t) image->columns; x++)
717 if (GetPixelReadMask(image,p) == 0)
719 p+=GetPixelChannels(image);
720 q+=GetPixelChannels(separate_image);
723 SetPixelChannel(separate_image,GrayPixelChannel,0,q);
724 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
726 PixelChannel channel=GetPixelChannelChannel(image,i);
727 PixelTrait traits=GetPixelChannelTraits(image,channel);
728 if ((traits == UndefinedPixelTrait) ||
729 (GetChannelBit(channel_type,channel) == 0))
731 SetPixelChannel(separate_image,GrayPixelChannel,p[i],q);
733 p+=GetPixelChannels(image);
734 q+=GetPixelChannels(separate_image);
736 if (SyncCacheViewAuthenticPixels(separate_view,exception) == MagickFalse)
738 if (image->progress_monitor != (MagickProgressMonitor) NULL)
743 #if defined(MAGICKCORE_OPENMP_SUPPORT)
744 #pragma omp critical (MagickCore_SeparateImage)
746 proceed=SetImageProgress(image,SeparateImageTag,progress++,image->rows);
747 if (proceed == MagickFalse)
751 separate_view=DestroyCacheView(separate_view);
752 image_view=DestroyCacheView(image_view);
753 if (status == MagickFalse)
754 separate_image=DestroyImage(separate_image);
755 return(separate_image);
759 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
763 % S e p a r a t e I m a g e s %
767 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
769 % SeparateImages() returns a separate grayscale image for each channel
772 % The format of the SeparateImages method is:
774 % Image *SeparateImages(const Image *image,ExceptionInfo *exception)
776 % A description of each parameter follows:
778 % o image: the image.
780 % o exception: return any errors or warnings in this structure.
783 MagickExport Image *SeparateImages(const Image *image,ExceptionInfo *exception)
792 assert(image != (Image *) NULL);
793 assert(image->signature == MagickSignature);
794 if (image->debug != MagickFalse)
795 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
796 images=NewImageList();
797 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
799 PixelChannel channel=GetPixelChannelChannel(image,i);
800 PixelTrait traits=GetPixelChannelTraits(image,channel);
801 if ((traits == UndefinedPixelTrait) ||
802 ((traits & UpdatePixelTrait) == 0))
804 separate_image=SeparateImage(image,(ChannelType) (1 << channel),exception);
805 if (separate_image != (Image *) NULL)
806 AppendImageToList(&images,separate_image);
808 if (images == (Image *) NULL)
809 images=SeparateImage(image,UndefinedChannel,exception);