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 channel_mask=(ChannelType) (channel_mask | ParseChannelOption(token));
365 if (((channels >= 1) || (destination_channel >= 1)) &&
366 (IsGrayColorspace(destination_image->colorspace) != MagickFalse))
367 (void) SetImageColorspace(destination_image,sRGBColorspace,exception);
368 if (LocaleCompare(token,"alpha") == 0)
369 (void) SetImageAlpha(destination_image,OpaqueAlpha,exception);
370 if (i >= (ssize_t) GetPixelChannels(destination_image))
371 (void) SetPixelMetaChannels(destination_image,(size_t) (i-
372 GetPixelChannels(destination_image)+1),exception);
373 GetMagickToken(p,&p,token);
379 status=ChannelImage(destination_image,destination_channel,channel_op,
380 source_image,source_channel,ClampToQuantum(pixel),exception);
381 if (status == MagickFalse)
383 destination_image=DestroyImageList(destination_image);
387 if (channel_op == ExchangeChannelOp)
389 status=ChannelImage(destination_image,source_channel,channel_op,
390 source_image,destination_channel,ClampToQuantum(pixel),exception);
391 if (status == MagickFalse)
393 destination_image=DestroyImageList(destination_image);
400 case ExtractChannelOp:
402 if (delete_channel == MagickFalse)
403 channel_mask=(ChannelType) (channel_mask &~
404 (1 << destination_channel));
406 channel_mask=(ChannelType) (channel_mask |
407 (1 << destination_channel));
408 destination_channel=(PixelChannel) (destination_channel+1);
414 status=SetImageProgress(source_image,ChannelFxImageTag,p-expression,
416 if (status == MagickFalse)
419 SetPixelChannelMask(destination_image,channel_mask);
420 if ((channel_op == ExtractChannelOp) && (channels == 1))
421 (void) SetImageColorspace(destination_image,GRAYColorspace,exception);
422 status=SetImageStorageClass(destination_image,DirectClass,exception);
423 if (status == MagickFalse)
425 destination_image=GetLastImageInList(destination_image);
426 return((Image *) NULL);
428 return(GetFirstImageInList(destination_image));
432 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
436 % C o m b i n e I m a g e s %
440 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
442 % CombineImages() combines one or more images into a single image. The
443 % grayscale value of the pixels of each image in the sequence is assigned in
444 % order to the specified channels of the combined image. The typical
445 % ordering would be image 1 => Red, 2 => Green, 3 => Blue, etc.
447 % The format of the CombineImages method is:
449 % Image *CombineImages(const Image *images,const ColorspaceType colorspace,
450 % ExceptionInfo *exception)
452 % A description of each parameter follows:
454 % o images: the image sequence.
456 % o colorspace: the image colorspace.
458 % o exception: return any errors or warnings in this structure.
461 MagickExport Image *CombineImages(const Image *image,
462 const ColorspaceType colorspace,ExceptionInfo *exception)
464 #define CombineImageTag "Combine/Image"
482 Ensure the image are the same size.
484 assert(image != (const Image *) NULL);
485 assert(image->signature == MagickSignature);
486 if (image->debug != MagickFalse)
487 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
488 assert(exception != (ExceptionInfo *) NULL);
489 assert(exception->signature == MagickSignature);
490 combine_image=CloneImage(image,0,0,MagickTrue,exception);
491 if (combine_image == (Image *) NULL)
492 return((Image *) NULL);
493 if (SetImageStorageClass(combine_image,DirectClass,exception) == MagickFalse)
495 combine_image=DestroyImage(combine_image);
496 return((Image *) NULL);
498 (void) SetImageColorspace(combine_image,colorspace,exception);
499 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
500 combine_image->alpha_trait=BlendPixelTrait;
506 combine_view=AcquireAuthenticCacheView(combine_image,exception);
507 for (y=0; y < (ssize_t) combine_image->rows; y++)
518 register const Quantum
527 if (status == MagickFalse)
529 pixels=GetCacheViewAuthenticPixels(combine_view,0,y,combine_image->columns,
531 if (pixels == (Quantum *) NULL)
537 for (i=0; i < (ssize_t) GetPixelChannels(combine_image); i++)
542 PixelChannel channel=GetPixelChannelChannel(combine_image,i);
543 PixelTrait traits=GetPixelChannelTraits(combine_image,channel);
544 if (traits == UndefinedPixelTrait)
546 if (next == (Image *) NULL)
548 image_view=AcquireVirtualCacheView(next,exception);
549 p=GetCacheViewVirtualPixels(image_view,0,y,next->columns,1,exception);
550 if (p == (const Quantum *) NULL)
553 for (x=0; x < (ssize_t) combine_image->columns; x++)
555 if (x < (ssize_t) next->columns)
557 q[i]=GetPixelGray(next,p);
558 p+=GetPixelChannels(next);
560 q+=GetPixelChannels(combine_image);
562 image_view=DestroyCacheView(image_view);
563 next=GetNextImageInList(next);
565 if (SyncCacheViewAuthenticPixels(combine_view,exception) == MagickFalse)
567 if (image->progress_monitor != (MagickProgressMonitor) NULL)
572 proceed=SetImageProgress(image,CombineImageTag,progress++,
573 combine_image->rows);
574 if (proceed == MagickFalse)
578 combine_view=DestroyCacheView(combine_view);
579 if (status == MagickFalse)
580 combine_image=DestroyImage(combine_image);
581 return(combine_image);
585 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
589 % S e p a r a t e I m a g e %
593 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
595 % SeparateImage() separates a channel from the image and returns it as a
598 % The format of the SeparateImage method is:
600 % Image *SeparateImage(const Image *image,const ChannelType channel,
601 % ExceptionInfo *exception)
603 % A description of each parameter follows:
605 % o image: the image.
607 % o channel: the image channel.
609 % o exception: return any errors or warnings in this structure.
612 MagickExport Image *SeparateImage(const Image *image,
613 const ChannelType channel_type,ExceptionInfo *exception)
615 #define GetChannelBit(mask,bit) (((size_t) (mask) >> (size_t) (bit)) & 0x01)
616 #define SeparateImageTag "Separate/Image"
635 Initialize separate image attributes.
637 assert(image != (Image *) NULL);
638 assert(image->signature == MagickSignature);
639 if (image->debug != MagickFalse)
640 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
641 assert(exception != (ExceptionInfo *) NULL);
642 assert(exception->signature == MagickSignature);
643 separate_image=CloneImage(image,image->columns,image->rows,MagickTrue,
645 if (separate_image == (Image *) NULL)
646 return((Image *) NULL);
647 if (SetImageStorageClass(separate_image,DirectClass,exception) == MagickFalse)
649 separate_image=DestroyImage(separate_image);
650 return((Image *) NULL);
652 (void) SetImageColorspace(separate_image,GRAYColorspace,exception);
653 separate_image->alpha_trait=UndefinedPixelTrait;
659 image_view=AcquireVirtualCacheView(image,exception);
660 separate_view=AcquireAuthenticCacheView(separate_image,exception);
661 #if defined(MAGICKCORE_OPENMP_SUPPORT)
662 #pragma omp parallel for schedule(static,4) shared(progress,status) \
663 magick_threads(image,image,image->rows,1)
665 for (y=0; y < (ssize_t) image->rows; y++)
667 register const Quantum
676 if (status == MagickFalse)
678 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
679 q=QueueCacheViewAuthenticPixels(separate_view,0,y,separate_image->columns,1,
681 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
686 for (x=0; x < (ssize_t) image->columns; x++)
691 if (GetPixelReadMask(image,p) == 0)
693 p+=GetPixelChannels(image);
694 q+=GetPixelChannels(separate_image);
697 SetPixelChannel(separate_image,GrayPixelChannel,0,q);
698 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
700 PixelChannel channel=GetPixelChannelChannel(image,i);
701 PixelTrait traits=GetPixelChannelTraits(image,channel);
702 if ((traits == UndefinedPixelTrait) ||
703 (GetChannelBit(channel_type,channel) == 0))
705 SetPixelChannel(separate_image,GrayPixelChannel,p[i],q);
707 p+=GetPixelChannels(image);
708 q+=GetPixelChannels(separate_image);
710 if (SyncCacheViewAuthenticPixels(separate_view,exception) == MagickFalse)
712 if (image->progress_monitor != (MagickProgressMonitor) NULL)
717 #if defined(MAGICKCORE_OPENMP_SUPPORT)
718 #pragma omp critical (MagickCore_SeparateImage)
720 proceed=SetImageProgress(image,SeparateImageTag,progress++,image->rows);
721 if (proceed == MagickFalse)
725 separate_view=DestroyCacheView(separate_view);
726 image_view=DestroyCacheView(image_view);
727 if (status == MagickFalse)
728 separate_image=DestroyImage(separate_image);
729 return(separate_image);
733 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
737 % S e p a r a t e I m a g e s %
741 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
743 % SeparateImages() returns a separate grayscale image for each channel
746 % The format of the SeparateImages method is:
748 % Image *SeparateImages(const Image *image,ExceptionInfo *exception)
750 % A description of each parameter follows:
752 % o image: the image.
754 % o exception: return any errors or warnings in this structure.
757 MagickExport Image *SeparateImages(const Image *image,ExceptionInfo *exception)
766 assert(image != (Image *) NULL);
767 assert(image->signature == MagickSignature);
768 if (image->debug != MagickFalse)
769 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
770 images=NewImageList();
771 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
773 PixelChannel channel=GetPixelChannelChannel(image,i);
774 PixelTrait traits=GetPixelChannelTraits(image,channel);
775 if ((traits == UndefinedPixelTrait) ||
776 ((traits & UpdatePixelTrait) == 0))
778 separate_image=SeparateImage(image,(ChannelType) (1 << channel),exception);
779 if (separate_image != (Image *) NULL)
780 AppendImageToList(&images,separate_image);
782 if (images == (Image *) NULL)
783 images=SeparateImage(image,UndefinedChannel,exception);