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 EEE 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-2017 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 % https://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/channel.h"
46 #include "MagickCore/colorspace-private.h"
47 #include "MagickCore/composite-private.h"
48 #include "MagickCore/enhance.h"
49 #include "MagickCore/image.h"
50 #include "MagickCore/list.h"
51 #include "MagickCore/log.h"
52 #include "MagickCore/monitor.h"
53 #include "MagickCore/monitor-private.h"
54 #include "MagickCore/option.h"
55 #include "MagickCore/pixel-accessor.h"
56 #include "MagickCore/pixel-private.h"
57 #include "MagickCore/resource_.h"
58 #include "MagickCore/string-private.h"
59 #include "MagickCore/thread-private.h"
60 #include "MagickCore/token.h"
61 #include "MagickCore/utility.h"
62 #include "MagickCore/version.h"
65 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
69 % C h a n n e l F x I m a g e %
73 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
75 % ChannelFxImage() applies a channel expression to the specified image. The
76 % expression consists of one or more channels, either mnemonic or numeric (e.g.
77 % red, 1), separated by actions as follows:
79 % <=> exchange two channels (e.g. red<=>blue)
80 % => copy one channel to another channel (e.g. red=>green)
81 % = assign a constant value to a channel (e.g. red=50%)
82 % , write new image channels in the specified order (e.g. red, green)
83 % | add a new output image for the next set of channel operations
84 % ; move to the next input image for the source of channel data
86 % For example, to create 3 grayscale images from the red, green, and blue
87 % channels of an image, use:
89 % -channel-fx "red; green; blue"
91 % A channel without an operation symbol implies separate (i.e, semicolon).
93 % The format of the ChannelFxImage method is:
95 % Image *ChannelFxImage(const Image *image,const char *expression,
96 % ExceptionInfo *exception)
98 % A description of each parameter follows:
100 % o image: the image.
102 % o expression: A channel expression.
104 % o exception: return any errors or warnings in this structure.
116 static MagickBooleanType ChannelImage(Image *destination_image,
117 const PixelChannel destination_channel,const ChannelFx channel_op,
118 const Image *source_image,const PixelChannel source_channel,
119 const Quantum pixel,ExceptionInfo *exception)
136 source_view=AcquireVirtualCacheView(source_image,exception);
137 destination_view=AcquireAuthenticCacheView(destination_image,exception);
138 height=MagickMin(source_image->rows,destination_image->rows);
139 width=MagickMin(source_image->columns,destination_image->columns);
140 #if defined(MAGICKCORE_OPENMP_SUPPORT)
141 #pragma omp parallel for schedule(static,4) shared(status) \
142 magick_threads(source_image,source_image,height,1)
144 for (y=0; y < (ssize_t) height; y++)
150 register const Quantum
159 if (status == MagickFalse)
161 p=GetCacheViewVirtualPixels(source_view,0,y,source_image->columns,1,
163 q=GetCacheViewAuthenticPixels(destination_view,0,y,
164 destination_image->columns,1,exception);
165 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
170 destination_traits=GetPixelChannelTraits(destination_image,
171 destination_channel);
172 source_traits=GetPixelChannelTraits(source_image,source_channel);
173 if ((destination_traits == UndefinedPixelTrait) ||
174 (source_traits == UndefinedPixelTrait))
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[MagickPathExtent];
230 assert(image != (Image *) NULL);
231 assert(image->signature == MagickCoreSignature);
232 if (image->debug != MagickFalse)
233 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
234 assert(exception != (ExceptionInfo *) NULL);
235 assert(exception->signature == MagickCoreSignature);
237 destination_image=CloneImage(source_image,0,0,MagickTrue,exception);
238 if (destination_image == (Image *) NULL)
239 return((Image *) NULL);
240 if (expression == (const char *) NULL)
241 return(destination_image);
242 status=SetImageStorageClass(destination_image,DirectClass,exception);
243 if (status == MagickFalse)
245 destination_image=GetLastImageInList(destination_image);
246 return((Image *) NULL);
248 destination_channel=RedPixelChannel;
249 channel_mask=UndefinedChannel;
251 p=(char *) expression;
252 GetNextToken(p,&p,MagickPathExtent,token);
253 channel_op=ExtractChannelOp;
254 for (channels=0; *token != '\0'; )
260 Interpret channel expression.
266 GetNextToken(p,&p,MagickPathExtent,token);
271 if (GetNextImageInList(source_image) != (Image *) NULL)
272 source_image=GetNextImageInList(source_image);
274 source_image=GetFirstImageInList(source_image);
275 GetNextToken(p,&p,MagickPathExtent,token);
283 (void) SetPixelChannelMask(destination_image,channel_mask);
284 if ((channel_op == ExtractChannelOp) && (channels == 1))
286 (void) SetPixelMetaChannels(destination_image,0,exception);
287 (void) SetImageColorspace(destination_image,GRAYColorspace,
290 canvas=CloneImage(source_image,0,0,MagickTrue,exception);
291 if (canvas == (Image *) NULL)
293 destination_image=DestroyImageList(destination_image);
294 return(destination_image);
296 AppendImageToList(&destination_image,canvas);
297 destination_image=GetLastImageInList(destination_image);
298 status=SetImageStorageClass(destination_image,DirectClass,exception);
299 if (status == MagickFalse)
301 destination_image=GetLastImageInList(destination_image);
302 return((Image *) NULL);
304 GetNextToken(p,&p,MagickPathExtent,token);
306 destination_channel=RedPixelChannel;
307 channel_mask=UndefinedChannel;
313 i=ParsePixelChannelOption(token);
316 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
317 "UnrecognizedChannelType","`%s'",token);
318 destination_image=DestroyImageList(destination_image);
319 return(destination_image);
321 source_channel=(PixelChannel) i;
322 channel_op=ExtractChannelOp;
323 GetNextToken(p,&p,MagickPathExtent,token);
326 channel_op=ExchangeChannelOp;
327 GetNextToken(p,&p,MagickPathExtent,token);
331 if (channel_op != ExchangeChannelOp)
332 channel_op=AssignChannelOp;
333 GetNextToken(p,&p,MagickPathExtent,token);
337 if (channel_op != ExchangeChannelOp)
338 channel_op=TransferChannelOp;
339 GetNextToken(p,&p,MagickPathExtent,token);
343 case AssignChannelOp:
344 case ExchangeChannelOp:
345 case TransferChannelOp:
347 if (channel_op == AssignChannelOp)
348 pixel=StringToDoubleInterval(token,(double) QuantumRange+1.0);
351 i=ParsePixelChannelOption(token);
354 (void) ThrowMagickException(exception,GetMagickModule(),
355 OptionError,"UnrecognizedChannelType","`%s'",token);
356 destination_image=DestroyImageList(destination_image);
357 return(destination_image);
360 destination_channel=(PixelChannel) i;
361 if (i >= (ssize_t) GetPixelChannels(destination_image))
362 (void) SetPixelMetaChannels(destination_image,(size_t) (
363 destination_channel-GetPixelChannels(destination_image)+1),
365 if (image->colorspace != UndefinedColorspace)
366 switch (destination_channel)
368 case RedPixelChannel:
369 case GreenPixelChannel:
370 case BluePixelChannel:
371 case BlackPixelChannel:
372 case IndexPixelChannel:
374 case AlphaPixelChannel:
376 destination_image->alpha_trait=BlendPixelTrait;
379 case ReadMaskPixelChannel:
381 destination_image->read_mask=MagickTrue;
384 case WriteMaskPixelChannel:
386 destination_image->write_mask=MagickTrue;
389 case MetaPixelChannel:
392 (void) SetPixelMetaChannels(destination_image,(size_t) (
393 destination_channel-GetPixelChannels(destination_image)+1),
398 channel_mask=(ChannelType) (channel_mask | ParseChannelOption(token));
399 if (((channels >= 1) || (destination_channel >= 1)) &&
400 (IsGrayColorspace(destination_image->colorspace) != MagickFalse))
401 (void) SetImageColorspace(destination_image,sRGBColorspace,exception);
402 GetNextToken(p,&p,MagickPathExtent,token);
408 status=ChannelImage(destination_image,destination_channel,channel_op,
409 source_image,source_channel,ClampToQuantum(pixel),exception);
410 if (status == MagickFalse)
412 destination_image=DestroyImageList(destination_image);
416 if (channel_op == ExchangeChannelOp)
418 status=ChannelImage(destination_image,source_channel,channel_op,
419 source_image,destination_channel,ClampToQuantum(pixel),exception);
420 if (status == MagickFalse)
422 destination_image=DestroyImageList(destination_image);
429 case ExtractChannelOp:
431 channel_mask=(ChannelType) (channel_mask | (1 << destination_channel));
432 destination_channel=(PixelChannel) (destination_channel+1);
438 status=SetImageProgress(source_image,ChannelFxImageTag,p-expression,
440 if (status == MagickFalse)
443 (void) SetPixelChannelMask(destination_image,channel_mask);
444 if ((channel_op == ExtractChannelOp) && (channels == 1))
446 (void) SetPixelMetaChannels(destination_image,0,exception);
447 (void) SetImageColorspace(destination_image,GRAYColorspace,exception);
449 return(GetFirstImageInList(destination_image));
453 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
457 % C o m b i n e I m a g e s %
461 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
463 % CombineImages() combines one or more images into a single image. The
464 % grayscale value of the pixels of each image in the sequence is assigned in
465 % order to the specified channels of the combined image. The typical
466 % ordering would be image 1 => Red, 2 => Green, 3 => Blue, etc.
468 % The format of the CombineImages method is:
470 % Image *CombineImages(const Image *images,const ColorspaceType colorspace,
471 % ExceptionInfo *exception)
473 % A description of each parameter follows:
475 % o images: the image sequence.
477 % o colorspace: the image colorspace.
479 % o exception: return any errors or warnings in this structure.
482 MagickExport Image *CombineImages(const Image *image,
483 const ColorspaceType colorspace,ExceptionInfo *exception)
485 #define CombineImageTag "Combine/Image"
503 Ensure the image are the same size.
505 assert(image != (const Image *) NULL);
506 assert(image->signature == MagickCoreSignature);
507 if (image->debug != MagickFalse)
508 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
509 assert(exception != (ExceptionInfo *) NULL);
510 assert(exception->signature == MagickCoreSignature);
511 combine_image=CloneImage(image,0,0,MagickTrue,exception);
512 if (combine_image == (Image *) NULL)
513 return((Image *) NULL);
514 if (SetImageStorageClass(combine_image,DirectClass,exception) == MagickFalse)
516 combine_image=DestroyImage(combine_image);
517 return((Image *) NULL);
519 (void) SetImageColorspace(combine_image,colorspace == UndefinedColorspace ?
520 sRGBColorspace : colorspace,exception);
521 switch (combine_image->colorspace)
523 case UndefinedColorspace:
526 if (GetImageListLength(image) > 3)
527 combine_image->alpha_trait=BlendPixelTrait;
532 if (GetImageListLength(image) > 1)
533 combine_image->alpha_trait=BlendPixelTrait;
538 if (GetImageListLength(image) > 4)
539 combine_image->alpha_trait=BlendPixelTrait;
550 combine_view=AcquireAuthenticCacheView(combine_image,exception);
551 for (y=0; y < (ssize_t) combine_image->rows; y++)
562 register const Quantum
571 if (status == MagickFalse)
573 pixels=GetCacheViewAuthenticPixels(combine_view,0,y,combine_image->columns,
575 if (pixels == (Quantum *) NULL)
581 for (i=0; i < (ssize_t) GetPixelChannels(combine_image); i++)
586 PixelChannel channel=GetPixelChannelChannel(combine_image,i);
587 PixelTrait traits=GetPixelChannelTraits(combine_image,channel);
588 if (traits == UndefinedPixelTrait)
590 if (next == (Image *) NULL)
592 image_view=AcquireVirtualCacheView(next,exception);
593 p=GetCacheViewVirtualPixels(image_view,0,y,next->columns,1,exception);
594 if (p == (const Quantum *) NULL)
597 for (x=0; x < (ssize_t) combine_image->columns; x++)
599 if (x < (ssize_t) next->columns)
601 q[i]=GetPixelGray(next,p);
602 p+=GetPixelChannels(next);
604 q+=GetPixelChannels(combine_image);
606 image_view=DestroyCacheView(image_view);
607 next=GetNextImageInList(next);
609 if (SyncCacheViewAuthenticPixels(combine_view,exception) == MagickFalse)
611 if (image->progress_monitor != (MagickProgressMonitor) NULL)
616 proceed=SetImageProgress(image,CombineImageTag,progress++,
617 combine_image->rows);
618 if (proceed == MagickFalse)
622 combine_view=DestroyCacheView(combine_view);
623 if (status == MagickFalse)
624 combine_image=DestroyImage(combine_image);
625 return(combine_image);
629 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
633 % G e t I m a g e A l p h a C h a n n e l %
637 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
639 % GetImageAlphaChannel() returns MagickFalse if the image alpha channel is
640 % not activated. That is, the image is RGB rather than RGBA or CMYK rather
643 % The format of the GetImageAlphaChannel method is:
645 % MagickBooleanType GetImageAlphaChannel(const Image *image)
647 % A description of each parameter follows:
649 % o image: the image.
652 MagickExport MagickBooleanType GetImageAlphaChannel(const Image *image)
654 assert(image != (const Image *) NULL);
655 if (image->debug != MagickFalse)
656 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
657 assert(image->signature == MagickCoreSignature);
658 return(image->alpha_trait != UndefinedPixelTrait ? MagickTrue : MagickFalse);
662 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
666 % S e p a r a t e I m a g e %
670 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
672 % SeparateImage() separates a channel from the image and returns it as a
675 % The format of the SeparateImage method is:
677 % Image *SeparateImage(const Image *image,const ChannelType channel,
678 % ExceptionInfo *exception)
680 % A description of each parameter follows:
682 % o image: the image.
684 % o channel: the image channel.
686 % o exception: return any errors or warnings in this structure.
689 MagickExport Image *SeparateImage(const Image *image,
690 const ChannelType channel_type,ExceptionInfo *exception)
692 #define GetChannelBit(mask,bit) (((size_t) (mask) >> (size_t) (bit)) & 0x01)
693 #define SeparateImageTag "Separate/Image"
712 Initialize separate image attributes.
714 assert(image != (Image *) NULL);
715 assert(image->signature == MagickCoreSignature);
716 if (image->debug != MagickFalse)
717 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
718 assert(exception != (ExceptionInfo *) NULL);
719 assert(exception->signature == MagickCoreSignature);
720 separate_image=CloneImage(image,image->columns,image->rows,MagickTrue,
722 if (separate_image == (Image *) NULL)
723 return((Image *) NULL);
724 if (SetImageStorageClass(separate_image,DirectClass,exception) == MagickFalse)
726 separate_image=DestroyImage(separate_image);
727 return((Image *) NULL);
729 (void) SetImageColorspace(separate_image,GRAYColorspace,exception);
730 separate_image->alpha_trait=UndefinedPixelTrait;
736 image_view=AcquireVirtualCacheView(image,exception);
737 separate_view=AcquireAuthenticCacheView(separate_image,exception);
738 #if defined(MAGICKCORE_OPENMP_SUPPORT)
739 #pragma omp parallel for schedule(static,4) shared(progress,status) \
740 magick_threads(image,image,image->rows,1)
742 for (y=0; y < (ssize_t) image->rows; y++)
744 register const Quantum
753 if (status == MagickFalse)
755 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
756 q=QueueCacheViewAuthenticPixels(separate_view,0,y,separate_image->columns,1,
758 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
763 for (x=0; x < (ssize_t) image->columns; x++)
768 if (GetPixelWriteMask(image,p) == 0)
770 SetPixelBackgoundColor(separate_image,q);
771 p+=GetPixelChannels(image);
772 q+=GetPixelChannels(separate_image);
775 SetPixelChannel(separate_image,GrayPixelChannel,0,q);
776 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
778 PixelChannel channel=GetPixelChannelChannel(image,i);
779 PixelTrait traits=GetPixelChannelTraits(image,channel);
780 if ((traits == UndefinedPixelTrait) ||
781 (GetChannelBit(channel_type,channel) == 0))
783 SetPixelChannel(separate_image,GrayPixelChannel,p[i],q);
785 p+=GetPixelChannels(image);
786 q+=GetPixelChannels(separate_image);
788 if (SyncCacheViewAuthenticPixels(separate_view,exception) == MagickFalse)
790 if (image->progress_monitor != (MagickProgressMonitor) NULL)
795 #if defined(MAGICKCORE_OPENMP_SUPPORT)
796 #pragma omp critical (MagickCore_SeparateImage)
798 proceed=SetImageProgress(image,SeparateImageTag,progress++,image->rows);
799 if (proceed == MagickFalse)
803 separate_view=DestroyCacheView(separate_view);
804 image_view=DestroyCacheView(image_view);
805 (void) SetImageChannelMask(separate_image,DefaultChannels);
806 if (status == MagickFalse)
807 separate_image=DestroyImage(separate_image);
808 return(separate_image);
812 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
816 % S e p a r a t e I m a g e s %
820 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
822 % SeparateImages() returns a separate grayscale image for each channel
825 % The format of the SeparateImages method is:
827 % Image *SeparateImages(const Image *image,ExceptionInfo *exception)
829 % A description of each parameter follows:
831 % o image: the image.
833 % o exception: return any errors or warnings in this structure.
836 MagickExport Image *SeparateImages(const Image *image,ExceptionInfo *exception)
845 assert(image != (Image *) NULL);
846 assert(image->signature == MagickCoreSignature);
847 if (image->debug != MagickFalse)
848 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
849 images=NewImageList();
850 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
852 PixelChannel channel=GetPixelChannelChannel(image,i);
853 PixelTrait traits=GetPixelChannelTraits(image,channel);
854 if ((traits == UndefinedPixelTrait) || ((traits & UpdatePixelTrait) == 0))
856 separate_image=SeparateImage(image,(ChannelType) (1 << channel),exception);
857 if (separate_image != (Image *) NULL)
858 AppendImageToList(&images,separate_image);
860 if (images == (Image *) NULL)
861 images=SeparateImage(image,UndefinedChannel,exception);
866 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
870 % S e t I m a g e A l p h a C h a n n e l %
874 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
876 % SetImageAlphaChannel() activates, deactivates, resets, or sets the alpha
879 % The format of the SetImageAlphaChannel method is:
881 % MagickBooleanType SetImageAlphaChannel(Image *image,
882 % const AlphaChannelOption alpha_type,ExceptionInfo *exception)
884 % A description of each parameter follows:
886 % o image: the image.
888 % o alpha_type: The alpha channel type: ActivateAlphaChannel,
889 % AssociateAlphaChannel, CopyAlphaChannel, DeactivateAlphaChannel,
890 % DisassociateAlphaChannel, ExtractAlphaChannel, OffAlphaChannel,
891 % OnAlphaChannel, OpaqueAlphaChannel, SetAlphaChannel, ShapeAlphaChannel,
892 % and TransparentAlphaChannel.
894 % o exception: return any errors or warnings in this structure.
898 static inline void FlattenPixelInfo(const Image *image,const PixelInfo *p,
899 const double alpha,const Quantum *q,const double beta,
911 Compose pixel p over pixel q with the given alpha.
913 Sa=QuantumScale*alpha;
914 Da=QuantumScale*beta,
915 gamma=Sa*(-Da)+Sa+Da;
916 gamma=PerceptibleReciprocal(gamma);
917 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
919 PixelChannel channel=GetPixelChannelChannel(image,i);
920 PixelTrait traits=GetPixelChannelTraits(image,channel);
921 if (traits == UndefinedPixelTrait)
925 case RedPixelChannel:
927 composite[i]=ClampToQuantum(gamma*MagickOver_((double) q[i],beta,
928 (double) p->red,alpha));
931 case GreenPixelChannel:
933 composite[i]=ClampToQuantum(gamma*MagickOver_((double) q[i],beta,
934 (double) p->green,alpha));
937 case BluePixelChannel:
939 composite[i]=ClampToQuantum(gamma*MagickOver_((double) q[i],beta,
940 (double) p->blue,alpha));
943 case BlackPixelChannel:
945 composite[i]=ClampToQuantum(gamma*MagickOver_((double) q[i],beta,
946 (double) p->black,alpha));
949 case AlphaPixelChannel:
951 composite[i]=ClampToQuantum(QuantumRange*(Sa*(-Da)+Sa+Da));
960 MagickExport MagickBooleanType SetImageAlphaChannel(Image *image,
961 const AlphaChannelOption alpha_type,ExceptionInfo *exception)
972 assert(image != (Image *) NULL);
973 if (image->debug != MagickFalse)
974 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
975 assert(image->signature == MagickCoreSignature);
979 case ActivateAlphaChannel:
981 image->alpha_trait=BlendPixelTrait;
984 case AssociateAlphaChannel:
989 status=SetImageStorageClass(image,DirectClass,exception);
990 if (status == MagickFalse)
992 image_view=AcquireAuthenticCacheView(image,exception);
993 #if defined(MAGICKCORE_OPENMP_SUPPORT)
994 #pragma omp parallel for schedule(static,4) shared(status) \
995 magick_threads(image,image,image->rows,1)
997 for (y=0; y < (ssize_t) image->rows; y++)
1005 if (status == MagickFalse)
1007 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
1009 if (q == (Quantum *) NULL)
1014 for (x=0; x < (ssize_t) image->columns; x++)
1022 if (GetPixelWriteMask(image,q) == 0)
1024 q+=GetPixelChannels(image);
1027 gamma=QuantumScale*GetPixelAlpha(image,q);
1028 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1030 PixelChannel channel=GetPixelChannelChannel(image,i);
1031 PixelTrait traits=GetPixelChannelTraits(image,channel);
1032 if (channel == AlphaPixelChannel)
1034 if ((traits & UpdatePixelTrait) == 0)
1036 q[i]=ClampToQuantum(gamma*q[i]);
1038 q+=GetPixelChannels(image);
1040 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1043 image_view=DestroyCacheView(image_view);
1044 image->alpha_trait=CopyPixelTrait;
1047 case BackgroundAlphaChannel:
1050 Set transparent pixels to background color.
1052 if (image->alpha_trait == UndefinedPixelTrait)
1054 status=SetImageStorageClass(image,DirectClass,exception);
1055 if (status == MagickFalse)
1057 image_view=AcquireAuthenticCacheView(image,exception);
1058 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1059 #pragma omp parallel for schedule(static,4) shared(status) \
1060 magick_threads(image,image,image->rows,1)
1062 for (y=0; y < (ssize_t) image->rows; y++)
1070 if (status == MagickFalse)
1072 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
1074 if (q == (Quantum *) NULL)
1079 for (x=0; x < (ssize_t) image->columns; x++)
1081 if (GetPixelAlpha(image,q) == TransparentAlpha)
1083 SetPixelViaPixelInfo(image,&image->background_color,q);
1084 SetPixelChannel(image,AlphaPixelChannel,TransparentAlpha,q);
1086 q+=GetPixelChannels(image);
1088 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1091 image_view=DestroyCacheView(image_view);
1094 case CopyAlphaChannel:
1096 image->alpha_trait=UpdatePixelTrait;
1097 status=CompositeImage(image,image,IntensityCompositeOp,MagickTrue,0,0,
1101 case DeactivateAlphaChannel:
1103 if (image->alpha_trait == UndefinedPixelTrait)
1104 status=SetImageAlpha(image,OpaqueAlpha,exception);
1105 image->alpha_trait=CopyPixelTrait;
1108 case DisassociateAlphaChannel:
1113 status=SetImageStorageClass(image,DirectClass,exception);
1114 if (status == MagickFalse)
1116 image->alpha_trait=BlendPixelTrait;
1117 image_view=AcquireAuthenticCacheView(image,exception);
1118 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1119 #pragma omp parallel for schedule(static,4) shared(status) \
1120 magick_threads(image,image,image->rows,1)
1122 for (y=0; y < (ssize_t) image->rows; y++)
1130 if (status == MagickFalse)
1132 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
1134 if (q == (Quantum *) NULL)
1139 for (x=0; x < (ssize_t) image->columns; x++)
1148 if (GetPixelWriteMask(image,q) == 0)
1150 q+=GetPixelChannels(image);
1153 Sa=QuantumScale*GetPixelAlpha(image,q);
1154 gamma=PerceptibleReciprocal(Sa);
1155 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1157 PixelChannel channel=GetPixelChannelChannel(image,i);
1158 PixelTrait traits=GetPixelChannelTraits(image,channel);
1159 if (channel == AlphaPixelChannel)
1161 if ((traits & UpdatePixelTrait) == 0)
1163 q[i]=ClampToQuantum(gamma*q[i]);
1165 q+=GetPixelChannels(image);
1167 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1170 image_view=DestroyCacheView(image_view);
1171 image->alpha_trait=UndefinedPixelTrait;
1174 case DiscreteAlphaChannel:
1176 if (image->alpha_trait == UndefinedPixelTrait)
1177 status=SetImageAlpha(image,OpaqueAlpha,exception);
1178 image->alpha_trait=UpdatePixelTrait;
1181 case ExtractAlphaChannel:
1183 status=CompositeImage(image,image,AlphaCompositeOp,MagickTrue,0,0,
1185 image->alpha_trait=UndefinedPixelTrait;
1188 case OffAlphaChannel:
1190 image->alpha_trait=UndefinedPixelTrait;
1193 case OnAlphaChannel:
1195 if (image->alpha_trait == UndefinedPixelTrait)
1196 status=SetImageAlpha(image,OpaqueAlpha,exception);
1197 image->alpha_trait=BlendPixelTrait;
1200 case OpaqueAlphaChannel:
1202 status=SetImageAlpha(image,OpaqueAlpha,exception);
1205 case RemoveAlphaChannel:
1208 Remove transparency.
1210 if (image->alpha_trait == UndefinedPixelTrait)
1212 status=SetImageStorageClass(image,DirectClass,exception);
1213 if (status == MagickFalse)
1215 image_view=AcquireAuthenticCacheView(image,exception);
1216 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1217 #pragma omp parallel for schedule(static,4) shared(status) \
1218 magick_threads(image,image,image->rows,1)
1220 for (y=0; y < (ssize_t) image->rows; y++)
1228 if (status == MagickFalse)
1230 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
1232 if (q == (Quantum *) NULL)
1237 for (x=0; x < (ssize_t) image->columns; x++)
1239 FlattenPixelInfo(image,&image->background_color,
1240 image->background_color.alpha,q,(double)
1241 GetPixelAlpha(image,q),q);
1242 q+=GetPixelChannels(image);
1244 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1247 image_view=DestroyCacheView(image_view);
1248 image->alpha_trait=image->background_color.alpha_trait;
1251 case SetAlphaChannel:
1253 if (image->alpha_trait == UndefinedPixelTrait)
1254 status=SetImageAlpha(image,OpaqueAlpha,exception);
1257 case ShapeAlphaChannel:
1260 Set alpha channel by shape.
1262 status=SetImageStorageClass(image,DirectClass,exception);
1263 if (status == MagickFalse)
1265 image->alpha_trait=UpdatePixelTrait;
1266 (void) SetImageMask(image,WritePixelMask,image,exception);
1267 (void) LevelImageColors(image,&image->background_color,
1268 &image->background_color,MagickTrue,exception);
1269 (void) SetImageMask(image,WritePixelMask,(Image *) NULL,exception);
1272 case TransparentAlphaChannel:
1274 status=SetImageAlpha(image,TransparentAlpha,exception);
1277 case UndefinedAlphaChannel:
1280 if (status == MagickFalse)
1282 (void) SetPixelChannelMask(image,image->channel_mask);
1283 return(SyncImagePixelCache(image,exception));