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/cache-private.h"
45 #include "MagickCore/colorspace-private.h"
46 #include "MagickCore/composite-private.h"
47 #include "MagickCore/enhance.h"
48 #include "MagickCore/image.h"
49 #include "MagickCore/list.h"
50 #include "MagickCore/log.h"
51 #include "MagickCore/monitor.h"
52 #include "MagickCore/monitor-private.h"
53 #include "MagickCore/option.h"
54 #include "MagickCore/pixel-accessor.h"
55 #include "MagickCore/pixel-private.h"
56 #include "MagickCore/resource_.h"
57 #include "MagickCore/string-private.h"
58 #include "MagickCore/thread-private.h"
59 #include "MagickCore/token.h"
60 #include "MagickCore/utility.h"
61 #include "MagickCore/version.h"
64 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
68 % C h a n n e l F x I m a g e %
72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
74 % ChannelFxImage() applies a channel expression to the specified image. The
75 % expression consists of one or more channels, either mnemonic or numeric (e.g.
76 % red, 1), separated by actions as follows:
78 % <=> exchange two channels (e.g. red<=>blue)
79 % => copy one channel to another channel (e.g. red=>green)
80 % = assign a constant value to a channel (e.g. red=50%)
81 % , write new image channels in the specified order (e.g. red, green)
82 % | add a new output image for the next set of channel operations
83 % ; move to the next input image for the source of channel data
85 % For example, to create 3 grayscale images from the red, green, and blue
86 % channels of an image, use:
88 % -channel-fx "red; green; blue"
90 % A channel without an operation symbol implies separate (i.e, semicolon).
92 % The format of the ChannelFxImage method is:
94 % Image *ChannelFxImage(const Image *image,const char *expression,
95 % ExceptionInfo *exception)
97 % A description of each parameter follows:
101 % o expression: A channel expression.
103 % o exception: return any errors or warnings in this structure.
115 static inline size_t MagickMin(const size_t x,const size_t y)
122 static MagickBooleanType ChannelImage(Image *destination_image,
123 const PixelChannel destination_channel,const ChannelFx channel_op,
124 const Image *source_image,const PixelChannel source_channel,
125 const Quantum pixel,ExceptionInfo *exception)
142 source_view=AcquireVirtualCacheView(source_image,exception);
143 destination_view=AcquireAuthenticCacheView(destination_image,exception);
144 height=MagickMin(source_image->rows,destination_image->rows);
145 width=MagickMin(source_image->columns,destination_image->columns);
146 #if defined(MAGICKCORE_OPENMP_SUPPORT)
147 #pragma omp parallel for schedule(static,4) shared(status) \
148 magick_threads(source_image,source_image,height,1)
150 for (y=0; y < (ssize_t) height; y++)
156 register const Quantum
165 if (status == MagickFalse)
167 p=GetCacheViewVirtualPixels(source_view,0,y,source_image->columns,1,
169 q=GetCacheViewAuthenticPixels(destination_view,0,y,
170 destination_image->columns,1,exception);
171 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
176 destination_traits=GetPixelChannelTraits(destination_image,
177 destination_channel);
178 source_traits=GetPixelChannelTraits(source_image,source_channel);
179 if ((destination_traits == UndefinedPixelTrait) ||
180 (source_traits == UndefinedPixelTrait))
182 for (x=0; x < (ssize_t) width; x++)
184 if (channel_op == AssignChannelOp)
185 SetPixelChannel(destination_image,destination_channel,pixel,q);
187 SetPixelChannel(destination_image,destination_channel,
188 GetPixelChannel(source_image,source_channel,p),q);
189 p+=GetPixelChannels(source_image);
190 q+=GetPixelChannels(destination_image);
192 if (SyncCacheViewAuthenticPixels(destination_view,exception) == MagickFalse)
195 destination_view=DestroyCacheView(destination_view);
196 source_view=DestroyCacheView(source_view);
200 MagickExport Image *ChannelFxImage(const Image *image,const char *expression,
201 ExceptionInfo *exception)
203 #define ChannelFxImageTag "ChannelFx/Image"
212 token[MaxTextExtent];
236 assert(image != (Image *) NULL);
237 assert(image->signature == MagickSignature);
238 if (image->debug != MagickFalse)
239 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
240 assert(exception != (ExceptionInfo *) NULL);
241 assert(exception->signature == MagickSignature);
243 destination_image=CloneImage(source_image,0,0,MagickTrue,exception);
244 if (destination_image == (Image *) NULL)
245 return((Image *) NULL);
246 if (expression == (const char *) NULL)
247 return(destination_image);
248 destination_channel=RedPixelChannel;
249 channel_mask=UndefinedChannel;
251 p=(char *) expression;
252 GetMagickToken(p,&p,token);
253 channel_op=ExtractChannelOp;
254 for (channels=0; *token != '\0'; )
263 Interpret channel expression.
265 delete_channel=MagickFalse;
270 delete_channel=MagickTrue;
271 GetMagickToken(p,&p,token);
276 destination_channel=(PixelChannel) ((ssize_t) destination_channel+1);
277 GetMagickToken(p,&p,token);
282 if (GetNextImageInList(source_image) != (Image *) NULL)
283 source_image=GetNextImageInList(source_image);
285 source_image=GetFirstImageInList(source_image);
286 GetMagickToken(p,&p,token);
294 SetPixelChannelMask(destination_image,channel_mask);
295 if ((channel_op == ExtractChannelOp) && (channels == 1))
296 (void) SetImageColorspace(destination_image,GRAYColorspace,exception);
297 status=SetImageStorageClass(destination_image,DirectClass,exception);
298 if (status == MagickFalse)
300 destination_image=DestroyImageList(destination_image);
301 return(destination_image);
303 canvas=CloneImage(source_image,0,0,MagickTrue,exception);
304 if (canvas == (Image *) NULL)
306 destination_image=DestroyImageList(destination_image);
307 return(destination_image);
309 AppendImageToList(&destination_image,canvas);
310 destination_image=GetLastImageInList(destination_image);
311 GetMagickToken(p,&p,token);
313 destination_channel=RedPixelChannel;
314 channel_mask=UndefinedChannel;
320 i=ParsePixelChannelOption(token);
323 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
324 "UnrecognizedChannelType","`%s'",token);
325 destination_image=DestroyImageList(destination_image);
326 return(destination_image);
328 source_channel=(PixelChannel) i;
329 channel_op=ExtractChannelOp;
330 GetMagickToken(p,&p,token);
333 channel_op=ExchangeChannelOp;
334 GetMagickToken(p,&p,token);
338 if (channel_op != ExchangeChannelOp)
339 channel_op=AssignChannelOp;
340 GetMagickToken(p,&p,token);
344 if (channel_op != ExchangeChannelOp)
345 channel_op=TransferChannelOp;
346 GetMagickToken(p,&p,token);
350 case AssignChannelOp:
352 pixel=StringToDoubleInterval(token,(double) QuantumRange+1.0);
353 GetMagickToken(p,&p,token);
356 case ExchangeChannelOp:
357 case TransferChannelOp:
359 i=ParsePixelChannelOption(token);
362 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
363 "UnrecognizedChannelType","`%s'",token);
364 destination_image=DestroyImageList(destination_image);
365 return(destination_image);
367 destination_channel=(PixelChannel) i;
368 switch (destination_channel)
370 case RedPixelChannel:
371 case GreenPixelChannel:
372 case BluePixelChannel:
373 case BlackPixelChannel:
374 case IndexPixelChannel:
376 case AlphaPixelChannel:
378 destination_image->alpha_trait=BlendPixelTrait;
381 case ReadMaskPixelChannel:
383 destination_image->read_mask=MagickTrue;
386 case WriteMaskPixelChannel:
388 destination_image->write_mask=MagickTrue;
391 case MetaPixelChannel:
394 (void) SetPixelMetaChannels(destination_image,(size_t) (i-
395 GetPixelChannels(destination_image)+1),exception);
399 channel_mask=(ChannelType) (channel_mask | ParseChannelOption(token));
400 if (((channels >= 1) || (destination_channel >= 1)) &&
401 (IsGrayColorspace(destination_image->colorspace) != MagickFalse))
402 (void) SetImageColorspace(destination_image,sRGBColorspace,exception);
403 GetMagickToken(p,&p,token);
409 status=ChannelImage(destination_image,destination_channel,channel_op,
410 source_image,source_channel,ClampToQuantum(pixel),exception);
411 if (status == MagickFalse)
413 destination_image=DestroyImageList(destination_image);
417 if (channel_op == ExchangeChannelOp)
419 status=ChannelImage(destination_image,source_channel,channel_op,
420 source_image,destination_channel,ClampToQuantum(pixel),exception);
421 if (status == MagickFalse)
423 destination_image=DestroyImageList(destination_image);
430 case ExtractChannelOp:
432 if (delete_channel == MagickFalse)
433 channel_mask=(ChannelType) (channel_mask |
434 (1 << destination_channel));
436 channel_mask=(ChannelType) (channel_mask &~
437 (1 << destination_channel));
438 destination_channel=(PixelChannel) (destination_channel+1);
444 status=SetImageProgress(source_image,ChannelFxImageTag,p-expression,
446 if (status == MagickFalse)
449 SetPixelChannelMask(destination_image,channel_mask);
450 if ((channel_op == ExtractChannelOp) && (channels == 1))
451 (void) SetImageColorspace(destination_image,GRAYColorspace,exception);
452 status=SetImageStorageClass(destination_image,DirectClass,exception);
453 if (status == MagickFalse)
455 destination_image=GetLastImageInList(destination_image);
456 return((Image *) NULL);
458 return(GetFirstImageInList(destination_image));
462 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
466 % C o m b i n e I m a g e s %
470 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
472 % CombineImages() combines one or more images into a single image. The
473 % grayscale value of the pixels of each image in the sequence is assigned in
474 % order to the specified channels of the combined image. The typical
475 % ordering would be image 1 => Red, 2 => Green, 3 => Blue, etc.
477 % The format of the CombineImages method is:
479 % Image *CombineImages(const Image *images,const ColorspaceType colorspace,
480 % ExceptionInfo *exception)
482 % A description of each parameter follows:
484 % o images: the image sequence.
486 % o colorspace: the image colorspace.
488 % o exception: return any errors or warnings in this structure.
491 MagickExport Image *CombineImages(const Image *image,
492 const ColorspaceType colorspace,ExceptionInfo *exception)
494 #define CombineImageTag "Combine/Image"
512 Ensure the image are the same size.
514 assert(image != (const Image *) NULL);
515 assert(image->signature == MagickSignature);
516 if (image->debug != MagickFalse)
517 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
518 assert(exception != (ExceptionInfo *) NULL);
519 assert(exception->signature == MagickSignature);
520 combine_image=CloneImage(image,0,0,MagickTrue,exception);
521 if (combine_image == (Image *) NULL)
522 return((Image *) NULL);
523 if (SetImageStorageClass(combine_image,DirectClass,exception) == MagickFalse)
525 combine_image=DestroyImage(combine_image);
526 return((Image *) NULL);
528 (void) SetImageColorspace(combine_image,colorspace,exception);
529 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
530 combine_image->alpha_trait=BlendPixelTrait;
536 combine_view=AcquireAuthenticCacheView(combine_image,exception);
537 for (y=0; y < (ssize_t) combine_image->rows; y++)
548 register const Quantum
557 if (status == MagickFalse)
559 pixels=GetCacheViewAuthenticPixels(combine_view,0,y,combine_image->columns,
561 if (pixels == (Quantum *) NULL)
567 for (i=0; i < (ssize_t) GetPixelChannels(combine_image); i++)
572 PixelChannel channel=GetPixelChannelChannel(combine_image,i);
573 PixelTrait traits=GetPixelChannelTraits(combine_image,channel);
574 if (traits == UndefinedPixelTrait)
576 if (next == (Image *) NULL)
578 image_view=AcquireVirtualCacheView(next,exception);
579 p=GetCacheViewVirtualPixels(image_view,0,y,next->columns,1,exception);
580 if (p == (const Quantum *) NULL)
583 for (x=0; x < (ssize_t) combine_image->columns; x++)
585 if (x < (ssize_t) next->columns)
587 q[i]=GetPixelGray(next,p);
588 p+=GetPixelChannels(next);
590 q+=GetPixelChannels(combine_image);
592 image_view=DestroyCacheView(image_view);
593 next=GetNextImageInList(next);
595 if (SyncCacheViewAuthenticPixels(combine_view,exception) == MagickFalse)
597 if (image->progress_monitor != (MagickProgressMonitor) NULL)
602 proceed=SetImageProgress(image,CombineImageTag,progress++,
603 combine_image->rows);
604 if (proceed == MagickFalse)
608 combine_view=DestroyCacheView(combine_view);
609 if (status == MagickFalse)
610 combine_image=DestroyImage(combine_image);
611 return(combine_image);
615 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
619 % G e t I m a g e A l p h a C h a n n e l %
623 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
625 % GetImageAlphaChannel() returns MagickFalse if the image alpha channel is
626 % not activated. That is, the image is RGB rather than RGBA or CMYK rather
629 % The format of the GetImageAlphaChannel method is:
631 % MagickBooleanType GetImageAlphaChannel(const Image *image)
633 % A description of each parameter follows:
635 % o image: the image.
638 MagickExport MagickBooleanType GetImageAlphaChannel(const Image *image)
640 assert(image != (const Image *) NULL);
641 if (image->debug != MagickFalse)
642 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
643 assert(image->signature == MagickSignature);
644 return(image->alpha_trait == BlendPixelTrait ? MagickTrue : MagickFalse);
648 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
652 % S e p a r a t e I m a g e %
656 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
658 % SeparateImage() separates a channel from the image and returns it as a
661 % The format of the SeparateImage method is:
663 % Image *SeparateImage(const Image *image,const ChannelType channel,
664 % ExceptionInfo *exception)
666 % A description of each parameter follows:
668 % o image: the image.
670 % o channel: the image channel.
672 % o exception: return any errors or warnings in this structure.
675 MagickExport Image *SeparateImage(const Image *image,
676 const ChannelType channel_type,ExceptionInfo *exception)
678 #define GetChannelBit(mask,bit) (((size_t) (mask) >> (size_t) (bit)) & 0x01)
679 #define SeparateImageTag "Separate/Image"
698 Initialize separate image attributes.
700 assert(image != (Image *) NULL);
701 assert(image->signature == MagickSignature);
702 if (image->debug != MagickFalse)
703 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
704 assert(exception != (ExceptionInfo *) NULL);
705 assert(exception->signature == MagickSignature);
706 separate_image=CloneImage(image,image->columns,image->rows,MagickTrue,
708 if (separate_image == (Image *) NULL)
709 return((Image *) NULL);
710 if (SetImageStorageClass(separate_image,DirectClass,exception) == MagickFalse)
712 separate_image=DestroyImage(separate_image);
713 return((Image *) NULL);
715 (void) SetImageColorspace(separate_image,GRAYColorspace,exception);
716 separate_image->alpha_trait=UndefinedPixelTrait;
722 image_view=AcquireVirtualCacheView(image,exception);
723 separate_view=AcquireAuthenticCacheView(separate_image,exception);
724 #if defined(MAGICKCORE_OPENMP_SUPPORT)
725 #pragma omp parallel for schedule(static,4) shared(progress,status) \
726 magick_threads(image,image,image->rows,1)
728 for (y=0; y < (ssize_t) image->rows; y++)
730 register const Quantum
739 if (status == MagickFalse)
741 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
742 q=QueueCacheViewAuthenticPixels(separate_view,0,y,separate_image->columns,1,
744 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
749 for (x=0; x < (ssize_t) image->columns; x++)
754 if (GetPixelReadMask(image,p) == 0)
756 p+=GetPixelChannels(image);
757 q+=GetPixelChannels(separate_image);
760 SetPixelChannel(separate_image,GrayPixelChannel,0,q);
761 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
763 PixelChannel channel=GetPixelChannelChannel(image,i);
764 PixelTrait traits=GetPixelChannelTraits(image,channel);
765 if ((traits == UndefinedPixelTrait) ||
766 (GetChannelBit(channel_type,channel) == 0))
768 SetPixelChannel(separate_image,GrayPixelChannel,p[i],q);
770 p+=GetPixelChannels(image);
771 q+=GetPixelChannels(separate_image);
773 if (SyncCacheViewAuthenticPixels(separate_view,exception) == MagickFalse)
775 if (image->progress_monitor != (MagickProgressMonitor) NULL)
780 #if defined(MAGICKCORE_OPENMP_SUPPORT)
781 #pragma omp critical (MagickCore_SeparateImage)
783 proceed=SetImageProgress(image,SeparateImageTag,progress++,image->rows);
784 if (proceed == MagickFalse)
788 separate_view=DestroyCacheView(separate_view);
789 image_view=DestroyCacheView(image_view);
790 if (status == MagickFalse)
791 separate_image=DestroyImage(separate_image);
792 return(separate_image);
796 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
800 % S e p a r a t e I m a g e s %
804 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
806 % SeparateImages() returns a separate grayscale image for each channel
809 % The format of the SeparateImages method is:
811 % Image *SeparateImages(const Image *image,ExceptionInfo *exception)
813 % A description of each parameter follows:
815 % o image: the image.
817 % o exception: return any errors or warnings in this structure.
820 MagickExport Image *SeparateImages(const Image *image,ExceptionInfo *exception)
829 assert(image != (Image *) NULL);
830 assert(image->signature == MagickSignature);
831 if (image->debug != MagickFalse)
832 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
833 images=NewImageList();
834 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
836 PixelChannel channel=GetPixelChannelChannel(image,i);
837 PixelTrait traits=GetPixelChannelTraits(image,channel);
838 if ((traits == UndefinedPixelTrait) ||
839 ((traits & UpdatePixelTrait) == 0))
841 separate_image=SeparateImage(image,(ChannelType) (1 << channel),exception);
842 if (separate_image != (Image *) NULL)
843 AppendImageToList(&images,separate_image);
845 if (images == (Image *) NULL)
846 images=SeparateImage(image,UndefinedChannel,exception);
851 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
855 % S e t I m a g e A l p h a C h a n n e l %
859 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
861 % SetImageAlphaChannel() activates, deactivates, resets, or sets the alpha
864 % The format of the SetImageAlphaChannel method is:
866 % MagickBooleanType SetImageAlphaChannel(Image *image,
867 % const AlphaChannelOption alpha_type,ExceptionInfo *exception)
869 % A description of each parameter follows:
871 % o image: the image.
873 % o alpha_type: The alpha channel type: ActivateAlphaChannel,
874 % CopyAlphaChannel, DeactivateAlphaChannel, ExtractAlphaChannel,
875 % OpaqueAlphaChannel, SetAlphaChannel, ShapeAlphaChannel, and
876 % TransparentAlphaChannel.
878 % o exception: return any errors or warnings in this structure.
882 static inline void FlattenPixelInfo(const Image *image,const PixelInfo *p,
883 const double alpha,const Quantum *q,const double beta,
895 Compose pixel p over pixel q with the given alpha.
897 Sa=QuantumScale*alpha;
898 Da=QuantumScale*beta,
899 gamma=Sa*(-Da)+Sa+Da;
900 gamma=PerceptibleReciprocal(gamma);
901 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
903 PixelChannel channel=GetPixelChannelChannel(image,i);
904 PixelTrait traits=GetPixelChannelTraits(image,channel);
905 if (traits == UndefinedPixelTrait)
909 case RedPixelChannel:
911 composite[i]=ClampToQuantum(gamma*MagickOver_((double) q[i],beta,
912 (double) p->red,alpha));
915 case GreenPixelChannel:
917 composite[i]=ClampToQuantum(gamma*MagickOver_((double) q[i],beta,
918 (double) p->green,alpha));
921 case BluePixelChannel:
923 composite[i]=ClampToQuantum(gamma*MagickOver_((double) q[i],beta,
924 (double) p->blue,alpha));
927 case BlackPixelChannel:
929 composite[i]=ClampToQuantum(gamma*MagickOver_((double) q[i],beta,
930 (double) p->black,alpha));
933 case AlphaPixelChannel:
935 composite[i]=ClampToQuantum(QuantumRange*(Sa*(-Da)+Sa+Da));
944 MagickExport MagickBooleanType SetImageAlphaChannel(Image *image,
945 const AlphaChannelOption alpha_type,ExceptionInfo *exception)
950 assert(image != (Image *) NULL);
951 if (image->debug != MagickFalse)
952 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
953 assert(image->signature == MagickSignature);
957 case ActivateAlphaChannel:
959 image->alpha_trait=BlendPixelTrait;
962 case BackgroundAlphaChannel:
971 Set transparent pixels to background color.
973 if (image->alpha_trait != BlendPixelTrait)
975 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
977 image_view=AcquireAuthenticCacheView(image,exception);
978 #if defined(MAGICKCORE_OPENMP_SUPPORT)
979 #pragma omp parallel for schedule(static,4) shared(status) \
980 magick_threads(image,image,image->rows,1)
982 for (y=0; y < (ssize_t) image->rows; y++)
990 if (status == MagickFalse)
992 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
994 if (q == (Quantum *) NULL)
999 for (x=0; x < (ssize_t) image->columns; x++)
1001 if (GetPixelAlpha(image,q) == TransparentAlpha)
1003 SetPixelInfoPixel(image,&image->background_color,q);
1004 SetPixelChannel(image,AlphaPixelChannel,TransparentAlpha,q);
1006 q+=GetPixelChannels(image);
1008 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1011 image_view=DestroyCacheView(image_view);
1014 case CopyAlphaChannel:
1015 case ShapeAlphaChannel:
1018 Copy pixel intensity to the alpha channel.
1020 status=CompositeImage(image,image,IntensityCompositeOp,MagickTrue,0,0,
1022 if (alpha_type == ShapeAlphaChannel)
1023 (void) LevelImageColors(image,&image->background_color,
1024 &image->background_color,MagickTrue,exception);
1027 case DeactivateAlphaChannel:
1029 image->alpha_trait=CopyPixelTrait;
1032 case ExtractAlphaChannel:
1034 status=CompositeImage(image,image,AlphaCompositeOp,MagickTrue,0,0,
1036 image->alpha_trait=CopyPixelTrait;
1039 case OpaqueAlphaChannel:
1041 status=SetImageAlpha(image,OpaqueAlpha,exception);
1044 case RemoveAlphaChannel:
1053 Remove transparency.
1055 if (image->alpha_trait != BlendPixelTrait)
1057 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1059 image_view=AcquireAuthenticCacheView(image,exception);
1060 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1061 #pragma omp parallel for schedule(static,4) shared(status) \
1062 magick_threads(image,image,image->rows,1)
1064 for (y=0; y < (ssize_t) image->rows; y++)
1072 if (status == MagickFalse)
1074 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
1076 if (q == (Quantum *) NULL)
1081 for (x=0; x < (ssize_t) image->columns; x++)
1083 FlattenPixelInfo(image,&image->background_color,
1084 image->background_color.alpha,q,(double)
1085 GetPixelAlpha(image,q),q);
1086 q+=GetPixelChannels(image);
1088 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1091 image_view=DestroyCacheView(image_view);
1092 image->alpha_trait=image->background_color.alpha_trait;
1095 case SetAlphaChannel:
1097 if (image->alpha_trait != BlendPixelTrait)
1098 status=SetImageAlpha(image,OpaqueAlpha,exception);
1101 case TransparentAlphaChannel:
1103 status=SetImageAlpha(image,TransparentAlpha,exception);
1106 case UndefinedAlphaChannel:
1109 if (status == MagickFalse)
1111 return(SyncImagePixelCache(image,exception));