]> granicus.if.org Git - imagemagick/blobdiff - MagickCore/channel.c
The 8bim profile will be updated when the iptc profile is changed.
[imagemagick] / MagickCore / channel.c
index 28ae9b27c04c08fd44399d5594ebc705d781b8ca..57cde8f3d0b3915aa75bce27d60ee9135156dfe9 100644 (file)
 %                      MagickCore Image Channel Methods                       %
 %                                                                             %
 %                              Software Design                                %
-%                                John Cristy                                  %
+%                                   Cristy                                    %
 %                               December 2003                                 %
 %                                                                             %
 %                                                                             %
-%  Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization      %
+%  Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization      %
 %  dedicated to making software imaging solutions freely available.           %
 %                                                                             %
 %  You may not use this file except in compliance with the License.  You may  %
   Include declarations.
 */
 #include "MagickCore/studio.h"
+#include "MagickCore/cache-private.h"
+#include "MagickCore/channel.h"
+#include "MagickCore/colorspace-private.h"
+#include "MagickCore/composite-private.h"
+#include "MagickCore/enhance.h"
 #include "MagickCore/image.h"
 #include "MagickCore/list.h"
 #include "MagickCore/log.h"
 #include "MagickCore/monitor-private.h"
 #include "MagickCore/option.h"
 #include "MagickCore/pixel-accessor.h"
+#include "MagickCore/pixel-private.h"
+#include "MagickCore/resource_.h"
 #include "MagickCore/string-private.h"
+#include "MagickCore/thread-private.h"
 #include "MagickCore/token.h"
 #include "MagickCore/utility.h"
 #include "MagickCore/version.h"
 %  red, 1), separated by actions as follows:
 %
 %    <=>     exchange two channels (e.g. red<=>blue)
-%    =>      transfer a channel to another (e.g. red=>green)
-%    =       assign a constant (e.g. red=50%)
-%    ,       separate channel operations (e.g. red, green)
-%    |       read channels from next input image (e.g. red | green)
-%    ;       write channels to next output image (e.g. red; green; blue)
+%    =>      copy one channel to another channel (e.g. red=>green)
+%    =       assign a constant value to a channel (e.g. red=50%)
+%    ,       write new image channels in the specified order (e.g. red, green)
+%    |       add a new output image for the next set of channel operations
+%    ;       move to the next input image for the source of channel data
 %
-%  A channel without a operation symbol implies extract. For example, to create
-%  3 grayscale images from the red, green, and blue channels of an image, use:
+%  For example, to create 3 grayscale images from the red, green, and blue
+%  channels of an image, use:
 %
 %    -channel-fx "red; green; blue"
 %
+%  A channel without an operation symbol implies separate (i.e, semicolon).
+%
 %  The format of the ChannelFxImage method is:
 %
 %      Image *ChannelFxImage(const Image *image,const char *expression,
@@ -123,17 +133,20 @@ static MagickBooleanType ChannelImage(Image *destination_image,
     status;
 
   size_t
-    height;
+    height,
+    width;
 
   ssize_t
     y;
 
   status=MagickTrue;
-  source_view=AcquireCacheView(source_image);
-  destination_view=AcquireCacheView(destination_image);
+  source_view=AcquireVirtualCacheView(source_image,exception);
+  destination_view=AcquireAuthenticCacheView(destination_image,exception);
   height=MagickMin(source_image->rows,destination_image->rows);
+  width=MagickMin(source_image->columns,destination_image->columns);
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp parallel for schedule(static) shared(status)
+  #pragma omp parallel for schedule(static,4) shared(status) \
+    magick_threads(source_image,source_image,height,1)
 #endif
   for (y=0; y < (ssize_t) height; y++)
   {
@@ -150,9 +163,6 @@ static MagickBooleanType ChannelImage(Image *destination_image,
     register ssize_t
       x;
 
-    size_t
-      width;
-
     if (status == MagickFalse)
       continue;
     p=GetCacheViewVirtualPixels(source_view,0,y,source_image->columns,1,
@@ -164,13 +174,12 @@ static MagickBooleanType ChannelImage(Image *destination_image,
         status=MagickFalse;
         continue;
       }
-    destination_traits=GetPixelChannelMapTraits(destination_image,
+    destination_traits=GetPixelChannelTraits(destination_image,
       destination_channel);
-    source_traits=GetPixelChannelMapTraits(source_image,source_channel);
+    source_traits=GetPixelChannelTraits(source_image,source_channel);
     if ((destination_traits == UndefinedPixelTrait) ||
         (source_traits == UndefinedPixelTrait))
       continue;
-    width=MagickMin(source_image->columns,destination_image->columns);
     for (x=0; x < (ssize_t) width; x++)
     {
       if (channel_op == AssignChannelOp)
@@ -197,6 +206,9 @@ MagickExport Image *ChannelFxImage(const Image *image,const char *expression,
   ChannelFx
     channel_op;
 
+  ChannelType
+    channel_mask;
+
   char
     token[MaxTextExtent];
 
@@ -235,11 +247,12 @@ MagickExport Image *ChannelFxImage(const Image *image,const char *expression,
   if (expression == (const char *) NULL)
     return(destination_image);
   destination_channel=RedPixelChannel;
+  channel_mask=UndefinedChannel;
   pixel=0.0;
   p=(char *) expression;
   GetMagickToken(p,&p,token);
   channel_op=ExtractChannelOp;
-  for (channels=0; *p != '\0'; )
+  for (channels=0; *token != '\0'; )
   {
     ssize_t
       i;
@@ -247,54 +260,60 @@ MagickExport Image *ChannelFxImage(const Image *image,const char *expression,
     /*
       Interpret channel expression.
     */
-    if (*token == ',')
+    switch (*token)
+    {
+      case ',':
       {
-        destination_channel=(PixelChannel) ((ssize_t) destination_channel+1);
         GetMagickToken(p,&p,token);
+        break;
       }
-    if (*token == '|')
+      case '|':
       {
         if (GetNextImageInList(source_image) != (Image *) NULL)
           source_image=GetNextImageInList(source_image);
         else
           source_image=GetFirstImageInList(source_image);
         GetMagickToken(p,&p,token);
+        break;
       }
-    if (*token == ';')
+      case ';':
       {
         Image
           *canvas;
 
+        SetPixelChannelMask(destination_image,channel_mask);
+        if ((channel_op == ExtractChannelOp) && (channels == 1))
+          (void) SetImageColorspace(destination_image,GRAYColorspace,exception);
         status=SetImageStorageClass(destination_image,DirectClass,exception);
         if (status == MagickFalse)
           {
-            destination_image=GetLastImageInList(destination_image);
-            return((Image *) NULL);
-          }
-        if ((channel_op == ExtractChannelOp) && (channels == 1))
-          {
-            destination_image->colorspace=GRAYColorspace;
-            InitializePixelChannelMap(destination_image);
+            destination_image=DestroyImageList(destination_image);
+            return(destination_image);
           }
         canvas=CloneImage(source_image,0,0,MagickTrue,exception);
         if (canvas == (Image *) NULL)
           {
-            destination_image=GetLastImageInList(destination_image);
-            return((Image *) NULL);
+            destination_image=DestroyImageList(destination_image);
+            return(destination_image);
           }
         AppendImageToList(&destination_image,canvas);
         destination_image=GetLastImageInList(destination_image);
         GetMagickToken(p,&p,token);
         channels=0;
         destination_channel=RedPixelChannel;
+        channel_mask=UndefinedChannel;
+        break;
       }
+      default:
+        break;
+    }
     i=ParsePixelChannelOption(token);
     if (i < 0)
       {
         (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
           "UnrecognizedChannelType","`%s'",token);
         destination_image=DestroyImageList(destination_image);
-        break;
+        return(destination_image);
       }
     source_channel=(PixelChannel) i;
     channel_op=ExtractChannelOp;
@@ -333,15 +352,50 @@ MagickExport Image *ChannelFxImage(const Image *image,const char *expression,
             (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
               "UnrecognizedChannelType","`%s'",token);
             destination_image=DestroyImageList(destination_image);
-            break;
+            return(destination_image);
           }
         destination_channel=(PixelChannel) i;
+        switch (destination_channel)
+        {
+          case RedPixelChannel:
+          case GreenPixelChannel:
+          case BluePixelChannel:
+          case BlackPixelChannel:
+          case IndexPixelChannel:
+            break;
+          case AlphaPixelChannel:
+          {
+            destination_image->alpha_trait=BlendPixelTrait;
+            break;
+          }
+          case ReadMaskPixelChannel:
+          {
+            destination_image->read_mask=MagickTrue;
+            break;
+          }
+          case WriteMaskPixelChannel:
+          {
+            destination_image->write_mask=MagickTrue;
+            break;
+          }
+          case MetaPixelChannel:
+          default:
+          {
+            (void) SetPixelMetaChannels(destination_image,(size_t) (i-
+              GetPixelChannels(destination_image)+1),exception);
+            break;
+          }
+        }
+        channel_mask=(ChannelType) (channel_mask | ParseChannelOption(token));
+        if (((channels >= 1)  || (destination_channel >= 1)) &&
+            (IsGrayColorspace(destination_image->colorspace) != MagickFalse))
+          (void) SetImageColorspace(destination_image,sRGBColorspace,exception);
         GetMagickToken(p,&p,token);
         break;
       }
-      case ExtractChannelOp:
+      default:
         break;
-     }
+    }
     status=ChannelImage(destination_image,destination_channel,channel_op,
       source_image,source_channel,ClampToQuantum(pixel),exception);
     if (status == MagickFalse)
@@ -361,23 +415,32 @@ MagickExport Image *ChannelFxImage(const Image *image,const char *expression,
           }
         channels++;
       }
+    switch (channel_op)
+    {
+      case ExtractChannelOp:
+      {
+        channel_mask=(ChannelType) (channel_mask | (1 << destination_channel));
+        destination_channel=(PixelChannel) (destination_channel+1);
+        break;
+      }
+      default:
+        break;
+    }
     status=SetImageProgress(source_image,ChannelFxImageTag,p-expression,
       strlen(expression));
     if (status == MagickFalse)
       break;
   }
+  SetPixelChannelMask(destination_image,channel_mask);
+  if ((channel_op == ExtractChannelOp) && (channels == 1))
+    (void) SetImageColorspace(destination_image,GRAYColorspace,exception);
   status=SetImageStorageClass(destination_image,DirectClass,exception);
   if (status == MagickFalse)
     {
       destination_image=GetLastImageInList(destination_image);
       return((Image *) NULL);
     }
-  if ((channel_op == ExtractChannelOp) && (channels == 1))
-    {
-      destination_image->colorspace=GRAYColorspace;
-      InitializePixelChannelMap(destination_image);
-    }
-  return(destination_image);
+  return(GetFirstImageInList(destination_image));
 }
 \f
 /*
@@ -398,16 +461,20 @@ MagickExport Image *ChannelFxImage(const Image *image,const char *expression,
 %
 %  The format of the CombineImages method is:
 %
-%      Image *CombineImages(const Image *image,ExceptionInfo *exception)
+%      Image *CombineImages(const Image *images,const ColorspaceType colorspace,
+%        ExceptionInfo *exception)
 %
 %  A description of each parameter follows:
 %
-%    o image: the image.
+%    o images: the image sequence.
+%
+%    o colorspace: the image colorspace.
 %
 %    o exception: return any errors or warnings in this structure.
 %
 */
-MagickExport Image *CombineImages(const Image *image,ExceptionInfo *exception)
+MagickExport Image *CombineImages(const Image *image,
+  const ColorspaceType colorspace,ExceptionInfo *exception)
 {
 #define CombineImageTag  "Combine/Image"
 
@@ -443,14 +510,15 @@ MagickExport Image *CombineImages(const Image *image,ExceptionInfo *exception)
       combine_image=DestroyImage(combine_image);
       return((Image *) NULL);
     }
+  (void) SetImageColorspace(combine_image,colorspace,exception);
   if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
-    combine_image->matte=MagickTrue;
+    combine_image->alpha_trait=BlendPixelTrait;
   /*
     Combine images.
   */
   status=MagickTrue;
   progress=0;
-  combine_view=AcquireCacheView(combine_image);
+  combine_view=AcquireAuthenticCacheView(combine_image,exception);
   for (y=0; y < (ssize_t) combine_image->rows; y++)
   {
     CacheView
@@ -481,55 +549,46 @@ MagickExport Image *CombineImages(const Image *image,ExceptionInfo *exception)
         continue;
       }
     next=image;
-    for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
+    for (i=0; i < (ssize_t) GetPixelChannels(combine_image); i++)
     {
-      PixelChannel
-        channel;
-
-      PixelTrait
-        combine_traits,
-        traits;
-
       register ssize_t
         x;
 
-      if (next == (Image *) NULL)
+      PixelChannel channel=GetPixelChannelChannel(combine_image,i);
+      PixelTrait traits=GetPixelChannelTraits(combine_image,channel);
+      if (traits == UndefinedPixelTrait)
         continue;
-      channel=GetPixelChannelMapChannel(image,i);
-      traits=GetPixelChannelMapTraits(image,channel);
-      combine_traits=GetPixelChannelMapTraits(combine_image,channel);
-      if ((traits == UndefinedPixelTrait) ||
-          (combine_traits == UndefinedPixelTrait))
+      if (next == (Image *) NULL)
         continue;
-      image_view=AcquireCacheView(next);
+      image_view=AcquireVirtualCacheView(next,exception);
       p=GetCacheViewVirtualPixels(image_view,0,y,next->columns,1,exception);
       if (p == (const Quantum *) NULL)
         continue;
       q=pixels;
       for (x=0; x < (ssize_t) combine_image->columns; x++)
       {
-        if (x < (ssize_t) image->columns)
+        if (x < (ssize_t) next->columns)
           {
-            q[i]=GetPixelGray(image,p);
-            p+=GetPixelChannels(image);
+            q[i]=GetPixelGray(next,p);
+            p+=GetPixelChannels(next);
           }
         q+=GetPixelChannels(combine_image);
       }
       image_view=DestroyCacheView(image_view);
       next=GetNextImageInList(next);
-      if (SyncCacheViewAuthenticPixels(combine_view,exception) == MagickFalse)
-        status=MagickFalse;
-      if (image->progress_monitor != (MagickProgressMonitor) NULL)
-        {
-          MagickBooleanType
-            proceed;
-
-          proceed=SetImageProgress(image,CombineImageTag,progress++,
-            combine_image->rows);
-          if (proceed == MagickFalse)
-            status=MagickFalse;
-        }
     }
+    if (SyncCacheViewAuthenticPixels(combine_view,exception) == MagickFalse)
+      status=MagickFalse;
+    if (image->progress_monitor != (MagickProgressMonitor) NULL)
+      {
+        MagickBooleanType
+          proceed;
+
+        proceed=SetImageProgress(image,CombineImageTag,progress++,
+          combine_image->rows);
+        if (proceed == MagickFalse)
+          status=MagickFalse;
+      }
   }
   combine_view=DestroyCacheView(combine_view);
   if (status == MagickFalse)
@@ -542,6 +601,39 @@ MagickExport Image *CombineImages(const Image *image,ExceptionInfo *exception)
 %                                                                             %
 %                                                                             %
 %                                                                             %
+%   G e t I m a g e A l p h a C h a n n e l                                   %
+%                                                                             %
+%                                                                             %
+%                                                                             %
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%
+%  GetImageAlphaChannel() returns MagickFalse if the image alpha channel is
+%  not activated.  That is, the image is RGB rather than RGBA or CMYK rather
+%  than CMYKA.
+%
+%  The format of the GetImageAlphaChannel method is:
+%
+%      MagickBooleanType GetImageAlphaChannel(const Image *image)
+%
+%  A description of each parameter follows:
+%
+%    o image: the image.
+%
+*/
+MagickExport MagickBooleanType GetImageAlphaChannel(const Image *image)
+{
+  assert(image != (const Image *) NULL);
+  if (image->debug != MagickFalse)
+    (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
+  assert(image->signature == MagickSignature);
+  return(image->alpha_trait == BlendPixelTrait ? MagickTrue : MagickFalse);
+}
+\f
+/*
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%                                                                             %
+%                                                                             %
+%                                                                             %
 %     S e p a r a t e I m a g e                                               %
 %                                                                             %
 %                                                                             %
@@ -588,7 +680,7 @@ MagickExport Image *SeparateImage(const Image *image,
     y;
 
   /*
-    Initialize spread image attributes.
+    Initialize separate image attributes.
   */
   assert(image != (Image *) NULL);
   assert(image->signature == MagickSignature);
@@ -605,16 +697,18 @@ MagickExport Image *SeparateImage(const Image *image,
       separate_image=DestroyImage(separate_image);
       return((Image *) NULL);
     }
-  separate_image->colorspace=GRAYColorspace;
+  (void) SetImageColorspace(separate_image,GRAYColorspace,exception);
+  separate_image->alpha_trait=UndefinedPixelTrait;
   /*
     Separate image.
   */
   status=MagickTrue;
   progress=0;
-  image_view=AcquireCacheView(image);
-  separate_view=AcquireCacheView(separate_image);
+  image_view=AcquireVirtualCacheView(image,exception);
+  separate_view=AcquireAuthenticCacheView(separate_image,exception);
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp parallel for schedule(static) shared(progress,status)
+  #pragma omp parallel for schedule(static,4) shared(progress,status) \
+    magick_threads(image,image,image->rows,1)
 #endif
   for (y=0; y < (ssize_t) image->rows; y++)
   {
@@ -642,8 +736,9 @@ MagickExport Image *SeparateImage(const Image *image,
       register ssize_t
         i;
 
-      if (GetPixelMask(image,p) != 0)
+      if (GetPixelReadMask(image,p) == 0)
         {
+          SetPixelBackgoundColor(separate_image,q);
           p+=GetPixelChannels(image);
           q+=GetPixelChannels(separate_image);
           continue;
@@ -651,14 +746,8 @@ MagickExport Image *SeparateImage(const Image *image,
       SetPixelChannel(separate_image,GrayPixelChannel,0,q);
       for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
       {
-        PixelChannel
-          channel;
-
-        PixelTrait
-          traits;
-
-        channel=GetPixelChannelMapChannel(image,i);
-        traits=GetPixelChannelMapTraits(image,channel);
+        PixelChannel channel=GetPixelChannelChannel(image,i);
+        PixelTrait traits=GetPixelChannelTraits(image,channel);
         if ((traits == UndefinedPixelTrait) ||
             (GetChannelBit(channel_type,channel) == 0))
           continue;
@@ -675,7 +764,7 @@ MagickExport Image *SeparateImage(const Image *image,
           proceed;
 
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp critical (MagickCore_SeparateImage)
+        #pragma omp critical (MagickCore_SeparateImage)
 #endif
         proceed=SetImageProgress(image,SeparateImageTag,progress++,image->rows);
         if (proceed == MagickFalse)
@@ -684,6 +773,8 @@ MagickExport Image *SeparateImage(const Image *image,
   }
   separate_view=DestroyCacheView(separate_view);
   image_view=DestroyCacheView(image_view);
+  if (status == MagickFalse)
+    separate_image=DestroyImage(separate_image);
   return(separate_image);
 }
 \f
@@ -703,8 +794,7 @@ MagickExport Image *SeparateImage(const Image *image,
 %
 %  The format of the SeparateImages method is:
 %
-%      MagickBooleanType SeparateImages(const Image *image,
-%        ExceptionInfo *exception)
+%      Image *SeparateImages(const Image *image,ExceptionInfo *exception)
 %
 %  A description of each parameter follows:
 %
@@ -729,14 +819,8 @@ MagickExport Image *SeparateImages(const Image *image,ExceptionInfo *exception)
   images=NewImageList();
   for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
   {
-    PixelChannel
-      channel;
-
-    PixelTrait
-      traits;
-
-    channel=GetPixelChannelMapChannel(image,i);
-    traits=GetPixelChannelMapTraits(image,channel);
+    PixelChannel channel=GetPixelChannelChannel(image,i);
+    PixelTrait traits=GetPixelChannelTraits(image,channel);
     if ((traits == UndefinedPixelTrait) ||
         ((traits & UpdatePixelTrait) == 0))
       continue;
@@ -744,5 +828,271 @@ MagickExport Image *SeparateImages(const Image *image,ExceptionInfo *exception)
     if (separate_image != (Image *) NULL)
       AppendImageToList(&images,separate_image);
   }
+  if (images == (Image *) NULL)
+    images=SeparateImage(image,UndefinedChannel,exception);
   return(images);
 }
+\f
+/*
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%                                                                             %
+%                                                                             %
+%                                                                             %
+%   S e t I m a g e A l p h a C h a n n e l                                   %
+%                                                                             %
+%                                                                             %
+%                                                                             %
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%
+%  SetImageAlphaChannel() activates, deactivates, resets, or sets the alpha
+%  channel.
+%
+%  The format of the SetImageAlphaChannel method is:
+%
+%      MagickBooleanType SetImageAlphaChannel(Image *image,
+%        const AlphaChannelOption alpha_type,ExceptionInfo *exception)
+%
+%  A description of each parameter follows:
+%
+%    o image: the image.
+%
+%    o alpha_type:  The alpha channel type: ActivateAlphaChannel,
+%      CopyAlphaChannel, DeactivateAlphaChannel, ExtractAlphaChannel,
+%      OpaqueAlphaChannel, SetAlphaChannel, ShapeAlphaChannel, and
+%      TransparentAlphaChannel.
+%
+%    o exception: return any errors or warnings in this structure.
+%
+*/
+
+static inline void FlattenPixelInfo(const Image *image,const PixelInfo *p,
+  const double alpha,const Quantum *q,const double beta,
+  Quantum *composite)
+{
+  double
+    Da,
+    gamma,
+    Sa;
+
+  register ssize_t
+    i;
+
+  /*
+    Compose pixel p over pixel q with the given alpha.
+  */
+  Sa=QuantumScale*alpha;
+  Da=QuantumScale*beta,
+  gamma=Sa*(-Da)+Sa+Da;
+  gamma=PerceptibleReciprocal(gamma);
+  for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
+  {
+    PixelChannel channel=GetPixelChannelChannel(image,i);
+    PixelTrait traits=GetPixelChannelTraits(image,channel);
+    if (traits == UndefinedPixelTrait)
+      continue;
+    switch (channel)
+    {
+      case RedPixelChannel:
+      {
+        composite[i]=ClampToQuantum(gamma*MagickOver_((double) q[i],beta,
+          (double) p->red,alpha));
+        break;
+      }
+      case GreenPixelChannel:
+      {
+        composite[i]=ClampToQuantum(gamma*MagickOver_((double) q[i],beta,
+          (double) p->green,alpha));
+        break;
+      }
+      case BluePixelChannel:
+      {
+        composite[i]=ClampToQuantum(gamma*MagickOver_((double) q[i],beta,
+          (double) p->blue,alpha));
+        break;
+      }
+      case BlackPixelChannel:
+      {
+        composite[i]=ClampToQuantum(gamma*MagickOver_((double) q[i],beta,
+          (double) p->black,alpha));
+        break;
+      }
+      case AlphaPixelChannel:
+      {
+        composite[i]=ClampToQuantum(QuantumRange*(Sa*(-Da)+Sa+Da));
+        break;
+      }
+      default:
+        break;
+    }
+  }
+}
+
+MagickExport MagickBooleanType SetImageAlphaChannel(Image *image,
+  const AlphaChannelOption alpha_type,ExceptionInfo *exception)
+{
+  MagickBooleanType
+    status;
+
+  assert(image != (Image *) NULL);
+  if (image->debug != MagickFalse)
+    (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
+  assert(image->signature == MagickSignature);
+  status=MagickTrue;
+  switch (alpha_type)
+  {
+    case ActivateAlphaChannel:
+    {
+      image->alpha_trait=BlendPixelTrait;
+      break;
+    }
+    case BackgroundAlphaChannel:
+    {
+      CacheView
+        *image_view;
+
+      ssize_t
+        y;
+
+      /*
+        Set transparent pixels to background color.
+      */
+      if (image->alpha_trait != BlendPixelTrait)
+        break;
+      if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
+        break;
+      image_view=AcquireAuthenticCacheView(image,exception);
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
+      #pragma omp parallel for schedule(static,4) shared(status) \
+        magick_threads(image,image,image->rows,1)
+#endif
+      for (y=0; y < (ssize_t) image->rows; y++)
+      {
+        register Quantum
+          *restrict q;
+
+        register ssize_t
+          x;
+
+        if (status == MagickFalse)
+          continue;
+        q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
+          exception);
+        if (q == (Quantum *) NULL)
+          {
+            status=MagickFalse;
+            continue;
+          }
+        for (x=0; x < (ssize_t) image->columns; x++)
+        {
+          if (GetPixelAlpha(image,q) == TransparentAlpha)
+            {
+              SetPixelInfoPixel(image,&image->background_color,q);
+              SetPixelChannel(image,AlphaPixelChannel,TransparentAlpha,q);
+            }
+          q+=GetPixelChannels(image);
+        }
+        if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
+          status=MagickFalse;
+      }
+      image_view=DestroyCacheView(image_view);
+      return(status);
+    }
+    case CopyAlphaChannel:
+    case ShapeAlphaChannel:
+    {
+      /*
+        Copy pixel intensity to the alpha channel.
+      */
+      status=CompositeImage(image,image,IntensityCompositeOp,MagickTrue,0,0,
+        exception);
+      if (alpha_type == ShapeAlphaChannel)
+        (void) LevelImageColors(image,&image->background_color,
+          &image->background_color,MagickTrue,exception);
+      break;
+    }
+    case DeactivateAlphaChannel:
+    {
+      image->alpha_trait=CopyPixelTrait;
+      break;
+    }
+    case ExtractAlphaChannel:
+    {
+      status=CompositeImage(image,image,AlphaCompositeOp,MagickTrue,0,0,
+        exception);
+      image->alpha_trait=CopyPixelTrait;
+      break;
+    }
+    case OpaqueAlphaChannel:
+    {
+      status=SetImageAlpha(image,OpaqueAlpha,exception);
+      break;
+    }
+    case RemoveAlphaChannel:
+    {
+      CacheView
+        *image_view;
+
+      ssize_t
+        y;
+
+      /*
+        Remove transparency.
+      */
+      if (image->alpha_trait != BlendPixelTrait)
+        break;
+      if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
+        break;
+      image_view=AcquireAuthenticCacheView(image,exception);
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
+      #pragma omp parallel for schedule(static,4) shared(status) \
+        magick_threads(image,image,image->rows,1)
+#endif
+      for (y=0; y < (ssize_t) image->rows; y++)
+      {
+        register Quantum
+          *restrict q;
+
+        register ssize_t
+          x;
+
+        if (status == MagickFalse)
+          continue;
+        q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
+          exception);
+        if (q == (Quantum *) NULL)
+          {
+            status=MagickFalse;
+            continue;
+          }
+        for (x=0; x < (ssize_t) image->columns; x++)
+        {
+          FlattenPixelInfo(image,&image->background_color,
+            image->background_color.alpha,q,(double)
+            GetPixelAlpha(image,q),q);
+          q+=GetPixelChannels(image);
+        }
+        if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
+          status=MagickFalse;
+      }
+      image_view=DestroyCacheView(image_view);
+      image->alpha_trait=image->background_color.alpha_trait;
+      return(status);
+    }
+    case SetAlphaChannel:
+    {
+      if (image->alpha_trait != BlendPixelTrait)
+        status=SetImageAlpha(image,OpaqueAlpha,exception);
+      break;
+    }
+    case TransparentAlphaChannel:
+    {
+      status=SetImageAlpha(image,TransparentAlpha,exception);
+      break;
+    }
+    case UndefinedAlphaChannel:
+      break;
+  }
+  if (status == MagickFalse)
+    return(status);
+  return(SyncImagePixelCache(image,exception));
+}