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-2018 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) shared(status) \
142 magick_number_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 CompositeMaskPixelChannel:
381 destination_image->channels|=CompositeMaskChannel;
384 case ReadMaskPixelChannel:
386 destination_image->channels|=ReadMaskChannel;
389 case WriteMaskPixelChannel:
391 destination_image->channels|=WriteMaskChannel;
394 case MetaPixelChannel:
397 (void) SetPixelMetaChannels(destination_image,(size_t) (
398 destination_channel-GetPixelChannels(destination_image)+1),
403 channel_mask=(ChannelType) (channel_mask | ParseChannelOption(token));
404 if (((channels >= 1) || (destination_channel >= 1)) &&
405 (IsGrayColorspace(destination_image->colorspace) != MagickFalse))
406 (void) SetImageColorspace(destination_image,sRGBColorspace,exception);
407 GetNextToken(p,&p,MagickPathExtent,token);
413 status=ChannelImage(destination_image,destination_channel,channel_op,
414 source_image,source_channel,ClampToQuantum(pixel),exception);
415 if (status == MagickFalse)
417 destination_image=DestroyImageList(destination_image);
421 if (channel_op == ExchangeChannelOp)
423 status=ChannelImage(destination_image,source_channel,channel_op,
424 source_image,destination_channel,ClampToQuantum(pixel),exception);
425 if (status == MagickFalse)
427 destination_image=DestroyImageList(destination_image);
434 case ExtractChannelOp:
436 channel_mask=(ChannelType) (channel_mask |
437 (1UL << destination_channel));
438 destination_channel=(PixelChannel) (destination_channel+1);
444 status=SetImageProgress(source_image,ChannelFxImageTag,p-expression,
446 if (status == MagickFalse)
449 (void) SetPixelChannelMask(destination_image,channel_mask);
450 if ((channel_op == ExtractChannelOp) && (channels == 1))
452 (void) SetPixelMetaChannels(destination_image,0,exception);
453 (void) SetImageColorspace(destination_image,GRAYColorspace,exception);
455 return(GetFirstImageInList(destination_image));
459 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
463 % C o m b i n e I m a g e s %
467 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
469 % CombineImages() combines one or more images into a single image. The
470 % grayscale value of the pixels of each image in the sequence is assigned in
471 % order to the specified channels of the combined image. The typical
472 % ordering would be image 1 => Red, 2 => Green, 3 => Blue, etc.
474 % The format of the CombineImages method is:
476 % Image *CombineImages(const Image *images,const ColorspaceType colorspace,
477 % ExceptionInfo *exception)
479 % A description of each parameter follows:
481 % o images: the image sequence.
483 % o colorspace: the image colorspace.
485 % o exception: return any errors or warnings in this structure.
488 MagickExport Image *CombineImages(const Image *image,
489 const ColorspaceType colorspace,ExceptionInfo *exception)
491 #define CombineImageTag "Combine/Image"
509 Ensure the image are the same size.
511 assert(image != (const Image *) NULL);
512 assert(image->signature == MagickCoreSignature);
513 if (image->debug != MagickFalse)
514 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
515 assert(exception != (ExceptionInfo *) NULL);
516 assert(exception->signature == MagickCoreSignature);
517 combine_image=CloneImage(image,0,0,MagickTrue,exception);
518 if (combine_image == (Image *) NULL)
519 return((Image *) NULL);
520 if (SetImageStorageClass(combine_image,DirectClass,exception) == MagickFalse)
522 combine_image=DestroyImage(combine_image);
523 return((Image *) NULL);
525 if (colorspace != UndefinedColorspace)
526 (void) SetImageColorspace(combine_image,colorspace,exception);
528 if (fabs(image->gamma-1.0) <= MagickEpsilon)
529 (void) SetImageColorspace(combine_image,RGBColorspace,exception);
531 (void) SetImageColorspace(combine_image,sRGBColorspace,exception);
532 switch (combine_image->colorspace)
534 case UndefinedColorspace:
537 if (GetImageListLength(image) > 3)
538 combine_image->alpha_trait=BlendPixelTrait;
541 case LinearGRAYColorspace:
544 if (GetImageListLength(image) > 1)
545 combine_image->alpha_trait=BlendPixelTrait;
550 if (GetImageListLength(image) > 4)
551 combine_image->alpha_trait=BlendPixelTrait;
562 combine_view=AcquireAuthenticCacheView(combine_image,exception);
563 for (y=0; y < (ssize_t) combine_image->rows; y++)
574 register const Quantum
583 if (status == MagickFalse)
585 pixels=GetCacheViewAuthenticPixels(combine_view,0,y,combine_image->columns,
587 if (pixels == (Quantum *) NULL)
593 for (i=0; i < (ssize_t) GetPixelChannels(combine_image); i++)
598 PixelChannel channel = GetPixelChannelChannel(combine_image,i);
599 PixelTrait traits = GetPixelChannelTraits(combine_image,channel);
600 if (traits == UndefinedPixelTrait)
602 if (next == (Image *) NULL)
604 image_view=AcquireVirtualCacheView(next,exception);
605 p=GetCacheViewVirtualPixels(image_view,0,y,next->columns,1,exception);
606 if (p == (const Quantum *) NULL)
609 for (x=0; x < (ssize_t) combine_image->columns; x++)
611 if (x < (ssize_t) next->columns)
613 q[i]=GetPixelGray(next,p);
614 p+=GetPixelChannels(next);
616 q+=GetPixelChannels(combine_image);
618 image_view=DestroyCacheView(image_view);
619 next=GetNextImageInList(next);
621 if (SyncCacheViewAuthenticPixels(combine_view,exception) == MagickFalse)
623 if (image->progress_monitor != (MagickProgressMonitor) NULL)
628 proceed=SetImageProgress(image,CombineImageTag,progress++,
629 combine_image->rows);
630 if (proceed == MagickFalse)
634 combine_view=DestroyCacheView(combine_view);
635 if (status == MagickFalse)
636 combine_image=DestroyImage(combine_image);
637 return(combine_image);
641 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
645 % G e t I m a g e A l p h a C h a n n e l %
649 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
651 % GetImageAlphaChannel() returns MagickFalse if the image alpha channel is
652 % not activated. That is, the image is RGB rather than RGBA or CMYK rather
655 % The format of the GetImageAlphaChannel method is:
657 % MagickBooleanType GetImageAlphaChannel(const Image *image)
659 % A description of each parameter follows:
661 % o image: the image.
664 MagickExport MagickBooleanType GetImageAlphaChannel(const Image *image)
666 assert(image != (const Image *) NULL);
667 if (image->debug != MagickFalse)
668 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
669 assert(image->signature == MagickCoreSignature);
670 return(image->alpha_trait != UndefinedPixelTrait ? MagickTrue : MagickFalse);
674 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
678 % S e p a r a t e I m a g e %
682 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
684 % SeparateImage() separates a channel from the image and returns it as a
687 % The format of the SeparateImage method is:
689 % Image *SeparateImage(const Image *image,const ChannelType channel,
690 % ExceptionInfo *exception)
692 % A description of each parameter follows:
694 % o image: the image.
696 % o channel: the image channel.
698 % o exception: return any errors or warnings in this structure.
701 MagickExport Image *SeparateImage(const Image *image,
702 const ChannelType channel_type,ExceptionInfo *exception)
704 #define GetChannelBit(mask,bit) (((size_t) (mask) >> (size_t) (bit)) & 0x01)
705 #define SeparateImageTag "Separate/Image"
724 Initialize separate image attributes.
726 assert(image != (Image *) NULL);
727 assert(image->signature == MagickCoreSignature);
728 if (image->debug != MagickFalse)
729 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
730 assert(exception != (ExceptionInfo *) NULL);
731 assert(exception->signature == MagickCoreSignature);
732 separate_image=CloneImage(image,0,0,MagickTrue,exception);
733 if (separate_image == (Image *) NULL)
734 return((Image *) NULL);
735 if (SetImageStorageClass(separate_image,DirectClass,exception) == MagickFalse)
737 separate_image=DestroyImage(separate_image);
738 return((Image *) NULL);
740 separate_image->alpha_trait=UndefinedPixelTrait;
741 (void) SetImageColorspace(separate_image,GRAYColorspace,exception);
742 separate_image->gamma=image->gamma;
748 image_view=AcquireVirtualCacheView(image,exception);
749 separate_view=AcquireAuthenticCacheView(separate_image,exception);
750 #if defined(MAGICKCORE_OPENMP_SUPPORT)
751 #pragma omp parallel for schedule(static) shared(progress,status) \
752 magick_number_threads(image,image,image->rows,1)
754 for (y=0; y < (ssize_t) image->rows; y++)
756 register const Quantum
765 if (status == MagickFalse)
767 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
768 q=QueueCacheViewAuthenticPixels(separate_view,0,y,separate_image->columns,1,
770 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
775 for (x=0; x < (ssize_t) image->columns; x++)
780 SetPixelChannel(separate_image,GrayPixelChannel,(Quantum) 0,q);
781 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
783 PixelChannel channel = GetPixelChannelChannel(image,i);
784 PixelTrait traits = GetPixelChannelTraits(image,channel);
785 if ((traits == UndefinedPixelTrait) ||
786 (GetChannelBit(channel_type,channel) == 0))
788 SetPixelChannel(separate_image,GrayPixelChannel,p[i],q);
790 p+=GetPixelChannels(image);
791 q+=GetPixelChannels(separate_image);
793 if (SyncCacheViewAuthenticPixels(separate_view,exception) == MagickFalse)
795 if (image->progress_monitor != (MagickProgressMonitor) NULL)
800 #if defined(MAGICKCORE_OPENMP_SUPPORT)
801 #pragma omp critical (MagickCore_SeparateImage)
803 proceed=SetImageProgress(image,SeparateImageTag,progress++,image->rows);
804 if (proceed == MagickFalse)
808 separate_view=DestroyCacheView(separate_view);
809 image_view=DestroyCacheView(image_view);
810 (void) SetImageChannelMask(separate_image,DefaultChannels);
811 if (status == MagickFalse)
812 separate_image=DestroyImage(separate_image);
813 return(separate_image);
817 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
821 % S e p a r a t e I m a g e s %
825 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
827 % SeparateImages() returns a separate grayscale image for each channel
830 % The format of the SeparateImages method is:
832 % Image *SeparateImages(const Image *image,ExceptionInfo *exception)
834 % A description of each parameter follows:
836 % o image: the image.
838 % o exception: return any errors or warnings in this structure.
841 MagickExport Image *SeparateImages(const Image *image,ExceptionInfo *exception)
850 assert(image != (Image *) NULL);
851 assert(image->signature == MagickCoreSignature);
852 if (image->debug != MagickFalse)
853 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
854 images=NewImageList();
855 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
857 PixelChannel channel = GetPixelChannelChannel(image,i);
858 PixelTrait traits = GetPixelChannelTraits(image,channel);
859 if ((traits == UndefinedPixelTrait) || ((traits & UpdatePixelTrait) == 0))
861 separate_image=SeparateImage(image,(ChannelType) (1UL << channel),
863 if (separate_image != (Image *) NULL)
864 AppendImageToList(&images,separate_image);
866 if (images == (Image *) NULL)
867 images=SeparateImage(image,UndefinedChannel,exception);
872 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
876 % S e t I m a g e A l p h a C h a n n e l %
880 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
882 % SetImageAlphaChannel() activates, deactivates, resets, or sets the alpha
885 % The format of the SetImageAlphaChannel method is:
887 % MagickBooleanType SetImageAlphaChannel(Image *image,
888 % const AlphaChannelOption alpha_type,ExceptionInfo *exception)
890 % A description of each parameter follows:
892 % o image: the image.
894 % o alpha_type: The alpha channel type: ActivateAlphaChannel,
895 % AssociateAlphaChannel, CopyAlphaChannel, DeactivateAlphaChannel,
896 % DisassociateAlphaChannel, ExtractAlphaChannel, OffAlphaChannel,
897 % OnAlphaChannel, OpaqueAlphaChannel, SetAlphaChannel, ShapeAlphaChannel,
898 % and TransparentAlphaChannel.
900 % o exception: return any errors or warnings in this structure.
904 static inline void FlattenPixelInfo(const Image *image,const PixelInfo *p,
905 const double alpha,const Quantum *q,const double beta,
917 Compose pixel p over pixel q with the given alpha.
919 Sa=QuantumScale*alpha;
920 Da=QuantumScale*beta,
921 gamma=Sa*(-Da)+Sa+Da;
922 gamma=PerceptibleReciprocal(gamma);
923 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
925 PixelChannel channel = GetPixelChannelChannel(image,i);
926 PixelTrait traits = GetPixelChannelTraits(image,channel);
927 if (traits == UndefinedPixelTrait)
931 case RedPixelChannel:
933 composite[i]=ClampToQuantum(gamma*MagickOver_((double) q[i],beta,
934 (double) p->red,alpha));
937 case GreenPixelChannel:
939 composite[i]=ClampToQuantum(gamma*MagickOver_((double) q[i],beta,
940 (double) p->green,alpha));
943 case BluePixelChannel:
945 composite[i]=ClampToQuantum(gamma*MagickOver_((double) q[i],beta,
946 (double) p->blue,alpha));
949 case BlackPixelChannel:
951 composite[i]=ClampToQuantum(gamma*MagickOver_((double) q[i],beta,
952 (double) p->black,alpha));
955 case AlphaPixelChannel:
957 composite[i]=ClampToQuantum(QuantumRange*(Sa*(-Da)+Sa+Da));
966 MagickExport MagickBooleanType SetImageAlphaChannel(Image *image,
967 const AlphaChannelOption alpha_type,ExceptionInfo *exception)
978 assert(image != (Image *) NULL);
979 if (image->debug != MagickFalse)
980 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
981 assert(image->signature == MagickCoreSignature);
985 case ActivateAlphaChannel:
987 image->alpha_trait=BlendPixelTrait;
990 case AssociateAlphaChannel:
995 status=SetImageStorageClass(image,DirectClass,exception);
996 if (status == MagickFalse)
998 image_view=AcquireAuthenticCacheView(image,exception);
999 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1000 #pragma omp parallel for schedule(static) shared(status) \
1001 magick_number_threads(image,image,image->rows,1)
1003 for (y=0; y < (ssize_t) image->rows; y++)
1011 if (status == MagickFalse)
1013 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
1015 if (q == (Quantum *) NULL)
1020 for (x=0; x < (ssize_t) image->columns; x++)
1028 gamma=QuantumScale*GetPixelAlpha(image,q);
1029 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1031 PixelChannel channel = GetPixelChannelChannel(image,i);
1032 PixelTrait traits = GetPixelChannelTraits(image,channel);
1033 if (channel == AlphaPixelChannel)
1035 if ((traits & UpdatePixelTrait) == 0)
1037 q[i]=ClampToQuantum(gamma*q[i]);
1039 q+=GetPixelChannels(image);
1041 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1044 image_view=DestroyCacheView(image_view);
1045 image->alpha_trait=CopyPixelTrait;
1048 case BackgroundAlphaChannel:
1051 Set transparent pixels to background color.
1053 if (image->alpha_trait == UndefinedPixelTrait)
1055 status=SetImageStorageClass(image,DirectClass,exception);
1056 if (status == MagickFalse)
1058 image_view=AcquireAuthenticCacheView(image,exception);
1059 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1060 #pragma omp parallel for schedule(static) shared(status) \
1061 magick_number_threads(image,image,image->rows,1)
1063 for (y=0; y < (ssize_t) image->rows; y++)
1071 if (status == MagickFalse)
1073 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
1075 if (q == (Quantum *) NULL)
1080 for (x=0; x < (ssize_t) image->columns; x++)
1082 if (GetPixelAlpha(image,q) == TransparentAlpha)
1084 SetPixelViaPixelInfo(image,&image->background_color,q);
1085 SetPixelChannel(image,AlphaPixelChannel,TransparentAlpha,q);
1087 q+=GetPixelChannels(image);
1089 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1092 image_view=DestroyCacheView(image_view);
1095 case CopyAlphaChannel:
1097 image->alpha_trait=UpdatePixelTrait;
1098 status=CompositeImage(image,image,IntensityCompositeOp,MagickTrue,0,0,
1102 case DeactivateAlphaChannel:
1104 if (image->alpha_trait == UndefinedPixelTrait)
1105 status=SetImageAlpha(image,OpaqueAlpha,exception);
1106 image->alpha_trait=CopyPixelTrait;
1109 case DisassociateAlphaChannel:
1114 status=SetImageStorageClass(image,DirectClass,exception);
1115 if (status == MagickFalse)
1117 image->alpha_trait=BlendPixelTrait;
1118 image_view=AcquireAuthenticCacheView(image,exception);
1119 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1120 #pragma omp parallel for schedule(static) shared(status) \
1121 magick_number_threads(image,image,image->rows,1)
1123 for (y=0; y < (ssize_t) image->rows; y++)
1131 if (status == MagickFalse)
1133 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
1135 if (q == (Quantum *) NULL)
1140 for (x=0; x < (ssize_t) image->columns; x++)
1149 Sa=QuantumScale*GetPixelAlpha(image,q);
1150 gamma=PerceptibleReciprocal(Sa);
1151 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1153 PixelChannel channel = GetPixelChannelChannel(image,i);
1154 PixelTrait traits = GetPixelChannelTraits(image,channel);
1155 if (channel == AlphaPixelChannel)
1157 if ((traits & UpdatePixelTrait) == 0)
1159 q[i]=ClampToQuantum(gamma*q[i]);
1161 q+=GetPixelChannels(image);
1163 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1166 image_view=DestroyCacheView(image_view);
1167 image->alpha_trait=UndefinedPixelTrait;
1170 case DiscreteAlphaChannel:
1172 if (image->alpha_trait == UndefinedPixelTrait)
1173 status=SetImageAlpha(image,OpaqueAlpha,exception);
1174 image->alpha_trait=UpdatePixelTrait;
1177 case ExtractAlphaChannel:
1179 status=CompositeImage(image,image,AlphaCompositeOp,MagickTrue,0,0,
1181 image->alpha_trait=UndefinedPixelTrait;
1184 case OffAlphaChannel:
1186 image->alpha_trait=UndefinedPixelTrait;
1189 case OnAlphaChannel:
1191 if (image->alpha_trait == UndefinedPixelTrait)
1192 status=SetImageAlpha(image,OpaqueAlpha,exception);
1193 image->alpha_trait=BlendPixelTrait;
1196 case OpaqueAlphaChannel:
1198 status=SetImageAlpha(image,OpaqueAlpha,exception);
1201 case RemoveAlphaChannel:
1204 Remove transparency.
1206 if (image->alpha_trait == UndefinedPixelTrait)
1208 status=SetImageStorageClass(image,DirectClass,exception);
1209 if (status == MagickFalse)
1211 image_view=AcquireAuthenticCacheView(image,exception);
1212 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1213 #pragma omp parallel for schedule(static) shared(status) \
1214 magick_number_threads(image,image,image->rows,1)
1216 for (y=0; y < (ssize_t) image->rows; y++)
1224 if (status == MagickFalse)
1226 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
1228 if (q == (Quantum *) NULL)
1233 for (x=0; x < (ssize_t) image->columns; x++)
1235 FlattenPixelInfo(image,&image->background_color,
1236 image->background_color.alpha,q,(double) GetPixelAlpha(image,q),q);
1237 q+=GetPixelChannels(image);
1239 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1242 image_view=DestroyCacheView(image_view);
1243 image->alpha_trait=image->background_color.alpha_trait;
1246 case SetAlphaChannel:
1248 if (image->alpha_trait == UndefinedPixelTrait)
1249 status=SetImageAlpha(image,OpaqueAlpha,exception);
1252 case ShapeAlphaChannel:
1255 Set alpha channel by shape.
1257 status=SetImageStorageClass(image,DirectClass,exception);
1258 if (status == MagickFalse)
1260 image->alpha_trait=UpdatePixelTrait;
1261 (void) SetImageMask(image,WritePixelMask,image,exception);
1262 (void) LevelImageColors(image,&image->background_color,
1263 &image->background_color,MagickTrue,exception);
1264 (void) SetImageMask(image,WritePixelMask,(Image *) NULL,exception);
1267 case TransparentAlphaChannel:
1269 status=SetImageAlpha(image,TransparentAlpha,exception);
1272 case UndefinedAlphaChannel:
1275 if (status == MagickFalse)
1277 (void) SetPixelChannelMask(image,image->channel_mask);
1278 return(SyncImagePixelCache(image,exception));