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-2015 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/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 destination_channel=RedPixelChannel;
243 channel_mask=UndefinedChannel;
245 p=(char *) expression;
246 GetMagickToken(p,&p,token);
247 channel_op=ExtractChannelOp;
248 for (channels=0; *token != '\0'; )
254 Interpret channel expression.
260 GetMagickToken(p,&p,token);
265 if (GetNextImageInList(source_image) != (Image *) NULL)
266 source_image=GetNextImageInList(source_image);
268 source_image=GetFirstImageInList(source_image);
269 GetMagickToken(p,&p,token);
277 (void) SetPixelChannelMask(destination_image,channel_mask);
278 if ((channel_op == ExtractChannelOp) && (channels == 1))
279 (void) SetImageColorspace(destination_image,GRAYColorspace,exception);
280 status=SetImageStorageClass(destination_image,DirectClass,exception);
281 if (status == MagickFalse)
283 destination_image=DestroyImageList(destination_image);
284 return(destination_image);
286 canvas=CloneImage(source_image,0,0,MagickTrue,exception);
287 if (canvas == (Image *) NULL)
289 destination_image=DestroyImageList(destination_image);
290 return(destination_image);
292 AppendImageToList(&destination_image,canvas);
293 destination_image=GetLastImageInList(destination_image);
294 GetMagickToken(p,&p,token);
296 destination_channel=RedPixelChannel;
297 channel_mask=UndefinedChannel;
303 i=ParsePixelChannelOption(token);
306 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
307 "UnrecognizedChannelType","`%s'",token);
308 destination_image=DestroyImageList(destination_image);
309 return(destination_image);
311 source_channel=(PixelChannel) i;
312 channel_op=ExtractChannelOp;
313 GetMagickToken(p,&p,token);
316 channel_op=ExchangeChannelOp;
317 GetMagickToken(p,&p,token);
321 if (channel_op != ExchangeChannelOp)
322 channel_op=AssignChannelOp;
323 GetMagickToken(p,&p,token);
327 if (channel_op != ExchangeChannelOp)
328 channel_op=TransferChannelOp;
329 GetMagickToken(p,&p,token);
333 case AssignChannelOp:
335 pixel=StringToDoubleInterval(token,(double) QuantumRange+1.0);
336 GetMagickToken(p,&p,token);
339 case ExchangeChannelOp:
340 case TransferChannelOp:
342 i=ParsePixelChannelOption(token);
345 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
346 "UnrecognizedChannelType","`%s'",token);
347 destination_image=DestroyImageList(destination_image);
348 return(destination_image);
350 destination_channel=(PixelChannel) i;
351 switch (destination_channel)
353 case RedPixelChannel:
354 case GreenPixelChannel:
355 case BluePixelChannel:
356 case BlackPixelChannel:
357 case IndexPixelChannel:
359 case AlphaPixelChannel:
361 destination_image->alpha_trait=BlendPixelTrait;
364 case ReadMaskPixelChannel:
366 destination_image->read_mask=MagickTrue;
369 case WriteMaskPixelChannel:
371 destination_image->write_mask=MagickTrue;
374 case MetaPixelChannel:
377 (void) SetPixelMetaChannels(destination_image,(size_t) (i-
378 GetPixelChannels(destination_image)+1),exception);
382 channel_mask=(ChannelType) (channel_mask | ParseChannelOption(token));
383 if (((channels >= 1) || (destination_channel >= 1)) &&
384 (IsGrayColorspace(destination_image->colorspace) != MagickFalse))
385 (void) SetImageColorspace(destination_image,sRGBColorspace,exception);
386 GetMagickToken(p,&p,token);
392 status=ChannelImage(destination_image,destination_channel,channel_op,
393 source_image,source_channel,ClampToQuantum(pixel),exception);
394 if (status == MagickFalse)
396 destination_image=DestroyImageList(destination_image);
400 if (channel_op == ExchangeChannelOp)
402 status=ChannelImage(destination_image,source_channel,channel_op,
403 source_image,destination_channel,ClampToQuantum(pixel),exception);
404 if (status == MagickFalse)
406 destination_image=DestroyImageList(destination_image);
413 case ExtractChannelOp:
415 channel_mask=(ChannelType) (channel_mask | (1 << destination_channel));
416 destination_channel=(PixelChannel) (destination_channel+1);
422 status=SetImageProgress(source_image,ChannelFxImageTag,p-expression,
424 if (status == MagickFalse)
427 (void) SetPixelChannelMask(destination_image,channel_mask);
428 if ((channel_op == ExtractChannelOp) && (channels == 1))
429 (void) SetImageColorspace(destination_image,GRAYColorspace,exception);
430 status=SetImageStorageClass(destination_image,DirectClass,exception);
431 if (status == MagickFalse)
433 destination_image=GetLastImageInList(destination_image);
434 return((Image *) NULL);
436 return(GetFirstImageInList(destination_image));
440 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
444 % C o m b i n e I m a g e s %
448 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
450 % CombineImages() combines one or more images into a single image. The
451 % grayscale value of the pixels of each image in the sequence is assigned in
452 % order to the specified channels of the combined image. The typical
453 % ordering would be image 1 => Red, 2 => Green, 3 => Blue, etc.
455 % The format of the CombineImages method is:
457 % Image *CombineImages(const Image *images,const ColorspaceType colorspace,
458 % ExceptionInfo *exception)
460 % A description of each parameter follows:
462 % o images: the image sequence.
464 % o colorspace: the image colorspace.
466 % o exception: return any errors or warnings in this structure.
469 MagickExport Image *CombineImages(const Image *image,
470 const ColorspaceType colorspace,ExceptionInfo *exception)
472 #define CombineImageTag "Combine/Image"
490 Ensure the image are the same size.
492 assert(image != (const Image *) NULL);
493 assert(image->signature == MagickCoreSignature);
494 if (image->debug != MagickFalse)
495 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
496 assert(exception != (ExceptionInfo *) NULL);
497 assert(exception->signature == MagickCoreSignature);
498 combine_image=CloneImage(image,0,0,MagickTrue,exception);
499 if (combine_image == (Image *) NULL)
500 return((Image *) NULL);
501 if (SetImageStorageClass(combine_image,DirectClass,exception) == MagickFalse)
503 combine_image=DestroyImage(combine_image);
504 return((Image *) NULL);
506 if ((colorspace == UndefinedColorspace) || (image->number_channels == 1))
507 (void) SetImageColorspace(combine_image,sRGBColorspace,exception);
509 (void) SetImageColorspace(combine_image,colorspace,exception);
510 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
511 combine_image->alpha_trait=BlendPixelTrait;
517 combine_view=AcquireAuthenticCacheView(combine_image,exception);
518 for (y=0; y < (ssize_t) combine_image->rows; y++)
529 register const Quantum
538 if (status == MagickFalse)
540 pixels=GetCacheViewAuthenticPixels(combine_view,0,y,combine_image->columns,
542 if (pixels == (Quantum *) NULL)
548 for (i=0; i < (ssize_t) GetPixelChannels(combine_image); i++)
553 PixelChannel channel=GetPixelChannelChannel(combine_image,i);
554 PixelTrait traits=GetPixelChannelTraits(combine_image,channel);
555 if (traits == UndefinedPixelTrait)
557 if (next == (Image *) NULL)
559 image_view=AcquireVirtualCacheView(next,exception);
560 p=GetCacheViewVirtualPixels(image_view,0,y,next->columns,1,exception);
561 if (p == (const Quantum *) NULL)
564 for (x=0; x < (ssize_t) combine_image->columns; x++)
566 if (x < (ssize_t) next->columns)
568 q[i]=GetPixelGray(next,p);
569 p+=GetPixelChannels(next);
571 q+=GetPixelChannels(combine_image);
573 image_view=DestroyCacheView(image_view);
574 next=GetNextImageInList(next);
576 if (SyncCacheViewAuthenticPixels(combine_view,exception) == MagickFalse)
578 if (image->progress_monitor != (MagickProgressMonitor) NULL)
583 proceed=SetImageProgress(image,CombineImageTag,progress++,
584 combine_image->rows);
585 if (proceed == MagickFalse)
589 combine_view=DestroyCacheView(combine_view);
590 if (status == MagickFalse)
591 combine_image=DestroyImage(combine_image);
592 return(combine_image);
596 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
600 % G e t I m a g e A l p h a C h a n n e l %
604 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
606 % GetImageAlphaChannel() returns MagickFalse if the image alpha channel is
607 % not activated. That is, the image is RGB rather than RGBA or CMYK rather
610 % The format of the GetImageAlphaChannel method is:
612 % MagickBooleanType GetImageAlphaChannel(const Image *image)
614 % A description of each parameter follows:
616 % o image: the image.
619 MagickExport MagickBooleanType GetImageAlphaChannel(const Image *image)
621 assert(image != (const Image *) NULL);
622 if (image->debug != MagickFalse)
623 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
624 assert(image->signature == MagickCoreSignature);
625 return(image->alpha_trait != UndefinedPixelTrait ? MagickTrue : MagickFalse);
629 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
633 % S e p a r a t e I m a g e %
637 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
639 % SeparateImage() separates a channel from the image and returns it as a
642 % The format of the SeparateImage method is:
644 % Image *SeparateImage(const Image *image,const ChannelType channel,
645 % ExceptionInfo *exception)
647 % A description of each parameter follows:
649 % o image: the image.
651 % o channel: the image channel.
653 % o exception: return any errors or warnings in this structure.
656 MagickExport Image *SeparateImage(const Image *image,
657 const ChannelType channel_type,ExceptionInfo *exception)
659 #define GetChannelBit(mask,bit) (((size_t) (mask) >> (size_t) (bit)) & 0x01)
660 #define SeparateImageTag "Separate/Image"
679 Initialize separate image attributes.
681 assert(image != (Image *) NULL);
682 assert(image->signature == MagickCoreSignature);
683 if (image->debug != MagickFalse)
684 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
685 assert(exception != (ExceptionInfo *) NULL);
686 assert(exception->signature == MagickCoreSignature);
687 separate_image=CloneImage(image,image->columns,image->rows,MagickTrue,
689 if (separate_image == (Image *) NULL)
690 return((Image *) NULL);
691 if (SetImageStorageClass(separate_image,DirectClass,exception) == MagickFalse)
693 separate_image=DestroyImage(separate_image);
694 return((Image *) NULL);
696 (void) SetImageColorspace(separate_image,GRAYColorspace,exception);
697 separate_image->alpha_trait=UndefinedPixelTrait;
703 image_view=AcquireVirtualCacheView(image,exception);
704 separate_view=AcquireAuthenticCacheView(separate_image,exception);
705 #if defined(MAGICKCORE_OPENMP_SUPPORT)
706 #pragma omp parallel for schedule(static,4) shared(progress,status) \
707 magick_threads(image,image,image->rows,1)
709 for (y=0; y < (ssize_t) image->rows; y++)
711 register const Quantum
720 if (status == MagickFalse)
722 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
723 q=QueueCacheViewAuthenticPixels(separate_view,0,y,separate_image->columns,1,
725 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
730 for (x=0; x < (ssize_t) image->columns; x++)
735 if (GetPixelReadMask(image,p) == 0)
737 SetPixelBackgoundColor(separate_image,q);
738 p+=GetPixelChannels(image);
739 q+=GetPixelChannels(separate_image);
742 SetPixelChannel(separate_image,GrayPixelChannel,0,q);
743 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
745 PixelChannel channel=GetPixelChannelChannel(image,i);
746 PixelTrait traits=GetPixelChannelTraits(image,channel);
747 if ((traits == UndefinedPixelTrait) ||
748 (GetChannelBit(channel_type,channel) == 0))
750 SetPixelChannel(separate_image,GrayPixelChannel,p[i],q);
752 p+=GetPixelChannels(image);
753 q+=GetPixelChannels(separate_image);
755 if (SyncCacheViewAuthenticPixels(separate_view,exception) == MagickFalse)
757 if (image->progress_monitor != (MagickProgressMonitor) NULL)
762 #if defined(MAGICKCORE_OPENMP_SUPPORT)
763 #pragma omp critical (MagickCore_SeparateImage)
765 proceed=SetImageProgress(image,SeparateImageTag,progress++,image->rows);
766 if (proceed == MagickFalse)
770 separate_view=DestroyCacheView(separate_view);
771 image_view=DestroyCacheView(image_view);
772 (void) SetImageChannelMask(separate_image,GrayChannel);
773 if (status == MagickFalse)
774 separate_image=DestroyImage(separate_image);
775 return(separate_image);
779 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
783 % S e p a r a t e I m a g e s %
787 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
789 % SeparateImages() returns a separate grayscale image for each channel
792 % The format of the SeparateImages method is:
794 % Image *SeparateImages(const Image *image,ExceptionInfo *exception)
796 % A description of each parameter follows:
798 % o image: the image.
800 % o exception: return any errors or warnings in this structure.
803 MagickExport Image *SeparateImages(const Image *image,ExceptionInfo *exception)
812 assert(image != (Image *) NULL);
813 assert(image->signature == MagickCoreSignature);
814 if (image->debug != MagickFalse)
815 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
816 images=NewImageList();
817 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
819 PixelChannel channel=GetPixelChannelChannel(image,i);
820 PixelTrait traits=GetPixelChannelTraits(image,channel);
821 if ((traits == UndefinedPixelTrait) ||
822 ((traits & UpdatePixelTrait) == 0))
824 separate_image=SeparateImage(image,(ChannelType) (1 << channel),exception);
825 if (separate_image != (Image *) NULL)
826 AppendImageToList(&images,separate_image);
828 if (images == (Image *) NULL)
829 images=SeparateImage(image,UndefinedChannel,exception);
834 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
838 % S e t I m a g e A l p h a C h a n n e l %
842 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
844 % SetImageAlphaChannel() activates, deactivates, resets, or sets the alpha
847 % The format of the SetImageAlphaChannel method is:
849 % MagickBooleanType SetImageAlphaChannel(Image *image,
850 % const AlphaChannelOption alpha_type,ExceptionInfo *exception)
852 % A description of each parameter follows:
854 % o image: the image.
856 % o alpha_type: The alpha channel type: ActivateAlphaChannel,
857 % AssociateAlphaChannel, CopyAlphaChannel, DeactivateAlphaChannel,
858 % DisassociateAlphaChannel, ExtractAlphaChannel, OffAlphaChannel,
859 % OnAlphaChannel, OpaqueAlphaChannel, SetAlphaChannel, ShapeAlphaChannel,
860 % and TransparentAlphaChannel.
862 % o exception: return any errors or warnings in this structure.
866 static inline void FlattenPixelInfo(const Image *image,const PixelInfo *p,
867 const double alpha,const Quantum *q,const double beta,
879 Compose pixel p over pixel q with the given alpha.
881 Sa=QuantumScale*alpha;
882 Da=QuantumScale*beta,
883 gamma=Sa*(-Da)+Sa+Da;
884 gamma=PerceptibleReciprocal(gamma);
885 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
887 PixelChannel channel=GetPixelChannelChannel(image,i);
888 PixelTrait traits=GetPixelChannelTraits(image,channel);
889 if (traits == UndefinedPixelTrait)
893 case RedPixelChannel:
895 composite[i]=ClampToQuantum(gamma*MagickOver_((double) q[i],beta,
896 (double) p->red,alpha));
899 case GreenPixelChannel:
901 composite[i]=ClampToQuantum(gamma*MagickOver_((double) q[i],beta,
902 (double) p->green,alpha));
905 case BluePixelChannel:
907 composite[i]=ClampToQuantum(gamma*MagickOver_((double) q[i],beta,
908 (double) p->blue,alpha));
911 case BlackPixelChannel:
913 composite[i]=ClampToQuantum(gamma*MagickOver_((double) q[i],beta,
914 (double) p->black,alpha));
917 case AlphaPixelChannel:
919 composite[i]=ClampToQuantum(QuantumRange*(Sa*(-Da)+Sa+Da));
928 MagickExport MagickBooleanType SetImageAlphaChannel(Image *image,
929 const AlphaChannelOption alpha_type,ExceptionInfo *exception)
940 assert(image != (Image *) NULL);
941 if (image->debug != MagickFalse)
942 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
943 assert(image->signature == MagickCoreSignature);
947 case ActivateAlphaChannel:
949 image->alpha_trait=BlendPixelTrait;
952 case AssociateAlphaChannel:
957 status=SetImageStorageClass(image,DirectClass,exception);
958 if (status == MagickFalse)
960 image_view=AcquireAuthenticCacheView(image,exception);
961 #if defined(MAGICKCORE_OPENMP_SUPPORT)
962 #pragma omp parallel for schedule(static,4) shared(status) \
963 magick_threads(image,image,image->rows,1)
965 for (y=0; y < (ssize_t) image->rows; y++)
973 if (status == MagickFalse)
975 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
977 if (q == (Quantum *) NULL)
982 for (x=0; x < (ssize_t) image->columns; x++)
991 if (GetPixelReadMask(image,q) == 0)
993 q+=GetPixelChannels(image);
996 Sa=QuantumScale*GetPixelAlpha(image,q);
998 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1000 PixelChannel channel=GetPixelChannelChannel(image,i);
1001 PixelTrait traits=GetPixelChannelTraits(image,channel);
1002 if ((traits & UpdatePixelTrait) == 0)
1004 q[i]=ClampToQuantum(gamma*q[i]);
1006 q+=GetPixelChannels(image);
1008 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1011 image_view=DestroyCacheView(image_view);
1012 image->alpha_trait=CopyPixelTrait;
1015 case BackgroundAlphaChannel:
1018 Set transparent pixels to background color.
1020 if (image->alpha_trait == UndefinedPixelTrait)
1022 status=SetImageStorageClass(image,DirectClass,exception);
1023 if (status == MagickFalse)
1025 image_view=AcquireAuthenticCacheView(image,exception);
1026 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1027 #pragma omp parallel for schedule(static,4) shared(status) \
1028 magick_threads(image,image,image->rows,1)
1030 for (y=0; y < (ssize_t) image->rows; y++)
1038 if (status == MagickFalse)
1040 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
1042 if (q == (Quantum *) NULL)
1047 for (x=0; x < (ssize_t) image->columns; x++)
1049 if (GetPixelAlpha(image,q) == TransparentAlpha)
1051 SetPixelViaPixelInfo(image,&image->background_color,q);
1052 SetPixelChannel(image,AlphaPixelChannel,TransparentAlpha,q);
1054 q+=GetPixelChannels(image);
1056 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1059 image_view=DestroyCacheView(image_view);
1062 case CopyAlphaChannel:
1063 case ShapeAlphaChannel:
1066 Copy pixel intensity to the alpha channel.
1068 image->alpha_trait=UpdatePixelTrait;
1069 status=CompositeImage(image,image,IntensityCompositeOp,MagickTrue,0,0,
1071 if (alpha_type == ShapeAlphaChannel)
1072 (void) LevelImageColors(image,&image->background_color,
1073 &image->background_color,MagickTrue,exception);
1076 case DeactivateAlphaChannel:
1078 if (image->alpha_trait == UndefinedPixelTrait)
1079 status=SetImageAlpha(image,OpaqueAlpha,exception);
1080 image->alpha_trait=CopyPixelTrait;
1083 case DisassociateAlphaChannel:
1088 status=SetImageStorageClass(image,DirectClass,exception);
1089 if (status == MagickFalse)
1091 image->alpha_trait=BlendPixelTrait;
1092 image_view=AcquireAuthenticCacheView(image,exception);
1093 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1094 #pragma omp parallel for schedule(static,4) shared(status) \
1095 magick_threads(image,image,image->rows,1)
1097 for (y=0; y < (ssize_t) image->rows; y++)
1105 if (status == MagickFalse)
1107 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
1109 if (q == (Quantum *) NULL)
1114 for (x=0; x < (ssize_t) image->columns; x++)
1123 if (GetPixelReadMask(image,q) == 0)
1125 q+=GetPixelChannels(image);
1128 Sa=QuantumScale*GetPixelAlpha(image,q);
1129 gamma=PerceptibleReciprocal(Sa);
1130 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1132 PixelChannel channel=GetPixelChannelChannel(image,i);
1133 PixelTrait traits=GetPixelChannelTraits(image,channel);
1134 if ((traits & UpdatePixelTrait) == 0)
1136 q[i]=ClampToQuantum(gamma*q[i]);
1138 q+=GetPixelChannels(image);
1140 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1143 image_view=DestroyCacheView(image_view);
1144 image->alpha_trait=UndefinedPixelTrait;
1147 case DiscreteAlphaChannel:
1149 image->alpha_trait=UpdatePixelTrait;
1152 case ExtractAlphaChannel:
1154 status=CompositeImage(image,image,AlphaCompositeOp,MagickTrue,0,0,
1156 image->alpha_trait=UndefinedPixelTrait;
1159 case OffAlphaChannel:
1161 image->alpha_trait=UndefinedPixelTrait;
1164 case OnAlphaChannel:
1166 if (image->alpha_trait == UndefinedPixelTrait)
1167 status=SetImageAlpha(image,OpaqueAlpha,exception);
1168 image->alpha_trait=BlendPixelTrait;
1171 case OpaqueAlphaChannel:
1173 status=SetImageAlpha(image,OpaqueAlpha,exception);
1176 case RemoveAlphaChannel:
1179 Remove transparency.
1181 if (image->alpha_trait == UndefinedPixelTrait)
1183 status=SetImageStorageClass(image,DirectClass,exception);
1184 if (status == MagickFalse)
1186 image_view=AcquireAuthenticCacheView(image,exception);
1187 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1188 #pragma omp parallel for schedule(static,4) shared(status) \
1189 magick_threads(image,image,image->rows,1)
1191 for (y=0; y < (ssize_t) image->rows; y++)
1199 if (status == MagickFalse)
1201 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
1203 if (q == (Quantum *) NULL)
1208 for (x=0; x < (ssize_t) image->columns; x++)
1210 FlattenPixelInfo(image,&image->background_color,
1211 image->background_color.alpha,q,(double)
1212 GetPixelAlpha(image,q),q);
1213 q+=GetPixelChannels(image);
1215 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1218 image_view=DestroyCacheView(image_view);
1219 image->alpha_trait=image->background_color.alpha_trait;
1222 case SetAlphaChannel:
1224 if (image->alpha_trait == UndefinedPixelTrait)
1225 status=SetImageAlpha(image,OpaqueAlpha,exception);
1228 case TransparentAlphaChannel:
1230 status=SetImageAlpha(image,TransparentAlpha,exception);
1233 case UndefinedAlphaChannel:
1236 if (status == MagickFalse)
1238 (void) SetPixelChannelMask(image,image->channel_mask);
1239 return(SyncImagePixelCache(image,exception));