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 AlphaPixelChannel:
368 destination_image->alpha_trait=BlendPixelTrait;
371 case ReadMaskPixelChannel:
373 destination_image->read_mask=MagickTrue;
376 case WriteMaskPixelChannel:
378 destination_image->write_mask=MagickTrue;
384 channel_mask=(ChannelType) (channel_mask | ParseChannelOption(token));
385 if (((channels >= 1) || (destination_channel >= 1)) &&
386 (IsGrayColorspace(destination_image->colorspace) != MagickFalse))
387 (void) SetImageColorspace(destination_image,sRGBColorspace,exception);
388 if (i >= (ssize_t) GetPixelChannels(destination_image))
389 (void) SetPixelMetaChannels(destination_image,(size_t) (i-
390 GetPixelChannels(destination_image)+1),exception);
391 GetMagickToken(p,&p,token);
397 status=ChannelImage(destination_image,destination_channel,channel_op,
398 source_image,source_channel,ClampToQuantum(pixel),exception);
399 if (status == MagickFalse)
401 destination_image=DestroyImageList(destination_image);
405 if (channel_op == ExchangeChannelOp)
407 status=ChannelImage(destination_image,source_channel,channel_op,
408 source_image,destination_channel,ClampToQuantum(pixel),exception);
409 if (status == MagickFalse)
411 destination_image=DestroyImageList(destination_image);
418 case ExtractChannelOp:
420 if (delete_channel == MagickFalse)
421 channel_mask=(ChannelType) (channel_mask &~
422 (1 << destination_channel));
424 channel_mask=(ChannelType) (channel_mask |
425 (1 << destination_channel));
426 destination_channel=(PixelChannel) (destination_channel+1);
432 status=SetImageProgress(source_image,ChannelFxImageTag,p-expression,
434 if (status == MagickFalse)
437 SetPixelChannelMask(destination_image,channel_mask);
438 if ((channel_op == ExtractChannelOp) && (channels == 1))
439 (void) SetImageColorspace(destination_image,GRAYColorspace,exception);
440 status=SetImageStorageClass(destination_image,DirectClass,exception);
441 if (status == MagickFalse)
443 destination_image=GetLastImageInList(destination_image);
444 return((Image *) NULL);
446 return(GetFirstImageInList(destination_image));
450 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
454 % C o m b i n e I m a g e s %
458 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
460 % CombineImages() combines one or more images into a single image. The
461 % grayscale value of the pixels of each image in the sequence is assigned in
462 % order to the specified channels of the combined image. The typical
463 % ordering would be image 1 => Red, 2 => Green, 3 => Blue, etc.
465 % The format of the CombineImages method is:
467 % Image *CombineImages(const Image *images,const ColorspaceType colorspace,
468 % ExceptionInfo *exception)
470 % A description of each parameter follows:
472 % o images: the image sequence.
474 % o colorspace: the image colorspace.
476 % o exception: return any errors or warnings in this structure.
479 MagickExport Image *CombineImages(const Image *image,
480 const ColorspaceType colorspace,ExceptionInfo *exception)
482 #define CombineImageTag "Combine/Image"
500 Ensure the image are the same size.
502 assert(image != (const Image *) NULL);
503 assert(image->signature == MagickSignature);
504 if (image->debug != MagickFalse)
505 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
506 assert(exception != (ExceptionInfo *) NULL);
507 assert(exception->signature == MagickSignature);
508 combine_image=CloneImage(image,0,0,MagickTrue,exception);
509 if (combine_image == (Image *) NULL)
510 return((Image *) NULL);
511 if (SetImageStorageClass(combine_image,DirectClass,exception) == MagickFalse)
513 combine_image=DestroyImage(combine_image);
514 return((Image *) NULL);
516 (void) SetImageColorspace(combine_image,colorspace,exception);
517 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
518 combine_image->alpha_trait=BlendPixelTrait;
524 combine_view=AcquireAuthenticCacheView(combine_image,exception);
525 for (y=0; y < (ssize_t) combine_image->rows; y++)
536 register const Quantum
545 if (status == MagickFalse)
547 pixels=GetCacheViewAuthenticPixels(combine_view,0,y,combine_image->columns,
549 if (pixels == (Quantum *) NULL)
555 for (i=0; i < (ssize_t) GetPixelChannels(combine_image); i++)
560 PixelChannel channel=GetPixelChannelChannel(combine_image,i);
561 PixelTrait traits=GetPixelChannelTraits(combine_image,channel);
562 if (traits == UndefinedPixelTrait)
564 if (next == (Image *) NULL)
566 image_view=AcquireVirtualCacheView(next,exception);
567 p=GetCacheViewVirtualPixels(image_view,0,y,next->columns,1,exception);
568 if (p == (const Quantum *) NULL)
571 for (x=0; x < (ssize_t) combine_image->columns; x++)
573 if (x < (ssize_t) next->columns)
575 q[i]=GetPixelGray(next,p);
576 p+=GetPixelChannels(next);
578 q+=GetPixelChannels(combine_image);
580 image_view=DestroyCacheView(image_view);
581 next=GetNextImageInList(next);
583 if (SyncCacheViewAuthenticPixels(combine_view,exception) == MagickFalse)
585 if (image->progress_monitor != (MagickProgressMonitor) NULL)
590 proceed=SetImageProgress(image,CombineImageTag,progress++,
591 combine_image->rows);
592 if (proceed == MagickFalse)
596 combine_view=DestroyCacheView(combine_view);
597 if (status == MagickFalse)
598 combine_image=DestroyImage(combine_image);
599 return(combine_image);
603 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
607 % S e p a r a t e I m a g e %
611 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
613 % SeparateImage() separates a channel from the image and returns it as a
616 % The format of the SeparateImage method is:
618 % Image *SeparateImage(const Image *image,const ChannelType channel,
619 % ExceptionInfo *exception)
621 % A description of each parameter follows:
623 % o image: the image.
625 % o channel: the image channel.
627 % o exception: return any errors or warnings in this structure.
630 MagickExport Image *SeparateImage(const Image *image,
631 const ChannelType channel_type,ExceptionInfo *exception)
633 #define GetChannelBit(mask,bit) (((size_t) (mask) >> (size_t) (bit)) & 0x01)
634 #define SeparateImageTag "Separate/Image"
653 Initialize separate image attributes.
655 assert(image != (Image *) NULL);
656 assert(image->signature == MagickSignature);
657 if (image->debug != MagickFalse)
658 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
659 assert(exception != (ExceptionInfo *) NULL);
660 assert(exception->signature == MagickSignature);
661 separate_image=CloneImage(image,image->columns,image->rows,MagickTrue,
663 if (separate_image == (Image *) NULL)
664 return((Image *) NULL);
665 if (SetImageStorageClass(separate_image,DirectClass,exception) == MagickFalse)
667 separate_image=DestroyImage(separate_image);
668 return((Image *) NULL);
670 (void) SetImageColorspace(separate_image,GRAYColorspace,exception);
671 separate_image->alpha_trait=UndefinedPixelTrait;
677 image_view=AcquireVirtualCacheView(image,exception);
678 separate_view=AcquireAuthenticCacheView(separate_image,exception);
679 #if defined(MAGICKCORE_OPENMP_SUPPORT)
680 #pragma omp parallel for schedule(static,4) shared(progress,status) \
681 magick_threads(image,image,image->rows,1)
683 for (y=0; y < (ssize_t) image->rows; y++)
685 register const Quantum
694 if (status == MagickFalse)
696 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
697 q=QueueCacheViewAuthenticPixels(separate_view,0,y,separate_image->columns,1,
699 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
704 for (x=0; x < (ssize_t) image->columns; x++)
709 if (GetPixelReadMask(image,p) == 0)
711 p+=GetPixelChannels(image);
712 q+=GetPixelChannels(separate_image);
715 SetPixelChannel(separate_image,GrayPixelChannel,0,q);
716 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
718 PixelChannel channel=GetPixelChannelChannel(image,i);
719 PixelTrait traits=GetPixelChannelTraits(image,channel);
720 if ((traits == UndefinedPixelTrait) ||
721 (GetChannelBit(channel_type,channel) == 0))
723 SetPixelChannel(separate_image,GrayPixelChannel,p[i],q);
725 p+=GetPixelChannels(image);
726 q+=GetPixelChannels(separate_image);
728 if (SyncCacheViewAuthenticPixels(separate_view,exception) == MagickFalse)
730 if (image->progress_monitor != (MagickProgressMonitor) NULL)
735 #if defined(MAGICKCORE_OPENMP_SUPPORT)
736 #pragma omp critical (MagickCore_SeparateImage)
738 proceed=SetImageProgress(image,SeparateImageTag,progress++,image->rows);
739 if (proceed == MagickFalse)
743 separate_view=DestroyCacheView(separate_view);
744 image_view=DestroyCacheView(image_view);
745 if (status == MagickFalse)
746 separate_image=DestroyImage(separate_image);
747 return(separate_image);
751 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
755 % S e p a r a t e I m a g e s %
759 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
761 % SeparateImages() returns a separate grayscale image for each channel
764 % The format of the SeparateImages method is:
766 % Image *SeparateImages(const Image *image,ExceptionInfo *exception)
768 % A description of each parameter follows:
770 % o image: the image.
772 % o exception: return any errors or warnings in this structure.
775 MagickExport Image *SeparateImages(const Image *image,ExceptionInfo *exception)
784 assert(image != (Image *) NULL);
785 assert(image->signature == MagickSignature);
786 if (image->debug != MagickFalse)
787 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
788 images=NewImageList();
789 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
791 PixelChannel channel=GetPixelChannelChannel(image,i);
792 PixelTrait traits=GetPixelChannelTraits(image,channel);
793 if ((traits == UndefinedPixelTrait) ||
794 ((traits & UpdatePixelTrait) == 0))
796 separate_image=SeparateImage(image,(ChannelType) (1 << channel),exception);
797 if (separate_image != (Image *) NULL)
798 AppendImageToList(&images,separate_image);
800 if (images == (Image *) NULL)
801 images=SeparateImage(image,UndefinedChannel,exception);