]> granicus.if.org Git - imagemagick/blobdiff - MagickCore/attribute.c
Update web pages
[imagemagick] / MagickCore / attribute.c
index 9ba5b79ba6da97b1f6650bf56a2ae915a1911df5..de44234422ebfd0e2f65858a57e1e27b0917b091 100644 (file)
 %                    MagickCore Get / Set Image Attributes                    %
 %                                                                             %
 %                              Software Design                                %
-%                                John Cristy                                  %
+%                                   Cristy                                    %
 %                                October 2002                                 %
 %                                                                             %
 %                                                                             %
-%  Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization      %
+%  Copyright 1999-2015 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  %
@@ -48,6 +48,7 @@
 #include "MagickCore/cache.h"
 #include "MagickCore/cache-private.h"
 #include "MagickCore/cache-view.h"
+#include "MagickCore/channel.h"
 #include "MagickCore/client.h"
 #include "MagickCore/color.h"
 #include "MagickCore/color-private.h"
@@ -144,7 +145,7 @@ MagickExport RectangleInfo GetImageBoundingBox(const Image *image,
     y;
 
   assert(image != (Image *) NULL);
-  assert(image->signature == MagickSignature);
+  assert(image->signature == MagickCoreSignature);
   if (image->debug != MagickFalse)
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
   bounds.width=0;
@@ -163,11 +164,13 @@ MagickExport RectangleInfo GetImageBoundingBox(const Image *image,
   GetPixelInfo(image,&target[1]);
   p=GetCacheViewVirtualPixels(image_view,(ssize_t) image->columns-1,0,1,1,
     exception);
-  GetPixelInfoPixel(image,p,&target[1]);
+  if (p != (const Quantum *) NULL)
+    GetPixelInfoPixel(image,p,&target[1]);
   GetPixelInfo(image,&target[2]);
   p=GetCacheViewVirtualPixels(image_view,0,(ssize_t) image->rows-1,1,1,
     exception);
-  GetPixelInfoPixel(image,p,&target[2]);
+  if (p != (const Quantum *) NULL)
+    GetPixelInfoPixel(image,p,&target[2]);
   status=MagickTrue;
   GetPixelInfo(image,&zero);
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
@@ -233,7 +236,7 @@ MagickExport RectangleInfo GetImageBoundingBox(const Image *image,
     }
   }
   image_view=DestroyCacheView(image_view);
-  if ((bounds.width == 0) || (bounds.height == 0))
+  if ((bounds.width == 0) && (bounds.height == 0))
     (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
       "GeometryDoesNotContainImage","`%s'",image->filename);
   else
@@ -277,7 +280,7 @@ MagickExport size_t GetImageDepth(const Image *image,ExceptionInfo *exception)
     status;
 
   register ssize_t
-    id;
+    i;
 
   size_t
     *current_depth,
@@ -291,7 +294,7 @@ MagickExport size_t GetImageDepth(const Image *image,ExceptionInfo *exception)
     Compute image depth.
   */
   assert(image != (Image *) NULL);
-  assert(image->signature == MagickSignature);
+  assert(image->signature == MagickCoreSignature);
   if (image->debug != MagickFalse)
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
   number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
@@ -300,13 +303,11 @@ MagickExport size_t GetImageDepth(const Image *image,ExceptionInfo *exception)
   if (current_depth == (size_t *) NULL)
     ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
   status=MagickTrue;
-  for (id=0; id < (ssize_t) number_threads; id++)
-    current_depth[id]=1;
-  if ((image->storage_class == PseudoClass) && (image->alpha_trait != BlendPixelTrait))
+  for (i=0; i < (ssize_t) number_threads; i++)
+    current_depth[i]=1;
+  if ((image->storage_class == PseudoClass) &&
+      (image->alpha_trait == UndefinedPixelTrait))
     {
-      register ssize_t
-        i;
-
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
       #pragma omp parallel for schedule(static,4) shared(status) \
         if ((image->colors) > 256) \
@@ -317,39 +318,37 @@ MagickExport size_t GetImageDepth(const Image *image,ExceptionInfo *exception)
         const int
           id = GetOpenMPThreadId();
 
-        if (status == MagickFalse)
-          continue;
         while (current_depth[id] < MAGICKCORE_QUANTUM_DEPTH)
         {
-          MagickStatusType
-            status;
+          MagickBooleanType
+            atDepth;
 
           QuantumAny
             range;
 
-          status=0;
+          atDepth=MagickTrue;
           range=GetQuantumRange(current_depth[id]);
-          if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
-            status|=ClampToQuantum(image->colormap[i].red) !=
-              ScaleAnyToQuantum(ScaleQuantumToAny(ClampToQuantum(
-              image->colormap[i].red),range),range);
-          if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
-            status|=ClampToQuantum(image->colormap[i].green) !=
-              ScaleAnyToQuantum(ScaleQuantumToAny(ClampToQuantum(
-              image->colormap[i].green),range),range);
-          if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
-            status|=ClampToQuantum(image->colormap[i].blue) !=
-              ScaleAnyToQuantum(ScaleQuantumToAny(ClampToQuantum(
-              image->colormap[i].blue),range),range);
-          if (status == 0)
+          if ((atDepth != MagickFalse) &&
+              (GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
+            if (IsPixelAtDepth(image->colormap[i].red,range) == MagickFalse)
+              atDepth=MagickFalse;
+          if ((atDepth != MagickFalse) &&
+              (GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
+            if (IsPixelAtDepth(image->colormap[i].green,range) == MagickFalse)
+              atDepth=MagickFalse;
+          if ((atDepth != MagickFalse) &&
+              (GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
+            if (IsPixelAtDepth(image->colormap[i].blue,range) == MagickFalse)
+              atDepth=MagickFalse;
+          if ((atDepth != MagickFalse))
             break;
           current_depth[id]++;
         }
       }
       depth=current_depth[0];
-      for (id=1; id < (ssize_t) number_threads; id++)
-        if (depth < current_depth[id])
-          depth=current_depth[id];
+      for (i=1; i < (ssize_t) number_threads; i++)
+        if (depth < current_depth[i])
+          depth=current_depth[i];
       current_depth=(size_t *) RelinquishMagickMemory(current_depth);
       return(depth);
     }
@@ -357,9 +356,6 @@ MagickExport size_t GetImageDepth(const Image *image,ExceptionInfo *exception)
 #if !defined(MAGICKCORE_HDRI_SUPPORT)
   if (QuantumRange <= MaxMap)
     {
-      register ssize_t
-        i;
-
       size_t
         *depth_map;
 
@@ -411,9 +407,6 @@ MagickExport size_t GetImageDepth(const Image *image,ExceptionInfo *exception)
           continue;
         for (x=0; x < (ssize_t) image->columns; x++)
         {
-          register ssize_t
-            i;
-
           if (GetPixelReadMask(image,p) == 0)
             {
               p+=GetPixelChannels(image);
@@ -425,7 +418,8 @@ MagickExport size_t GetImageDepth(const Image *image,ExceptionInfo *exception)
             PixelTrait traits=GetPixelChannelTraits(image,channel);
             if ((traits == UndefinedPixelTrait) ||
                 (channel == IndexPixelChannel) ||
-                (channel == ReadMaskPixelChannel) || (channel == MetaPixelChannel))
+                (channel == ReadMaskPixelChannel) ||
+                (channel == MetaPixelChannel))
               continue;
             if (depth_map[ScaleQuantumToMap(p[i])] > current_depth[id])
               current_depth[id]=depth_map[ScaleQuantumToMap(p[i])];
@@ -437,9 +431,9 @@ MagickExport size_t GetImageDepth(const Image *image,ExceptionInfo *exception)
       }
       image_view=DestroyCacheView(image_view);
       depth=current_depth[0];
-      for (id=1; id < (ssize_t) number_threads; id++)
-        if (depth < current_depth[id])
-          depth=current_depth[id];
+      for (i=1; i < (ssize_t) number_threads; i++)
+        if (depth < current_depth[i])
+          depth=current_depth[i];
       depth_map=(size_t *) RelinquishMagickMemory(depth_map);
       current_depth=(size_t *) RelinquishMagickMemory(current_depth);
       return(depth);
@@ -470,9 +464,6 @@ MagickExport size_t GetImageDepth(const Image *image,ExceptionInfo *exception)
       continue;
     for (x=0; x < (ssize_t) image->columns; x++)
     {
-      register ssize_t
-        i;
-
       if (GetPixelReadMask(image,p) == 0)
         {
           p+=GetPixelChannels(image);
@@ -509,9 +500,9 @@ MagickExport size_t GetImageDepth(const Image *image,ExceptionInfo *exception)
   }
   image_view=DestroyCacheView(image_view);
   depth=current_depth[0];
-  for (id=1; id < (ssize_t) number_threads; id++)
-    if (depth < current_depth[id])
-      depth=current_depth[id];
+  for (i=1; i < (ssize_t) number_threads; i++)
+    if (depth < current_depth[i])
+      depth=current_depth[i];
   current_depth=(size_t *) RelinquishMagickMemory(current_depth);
   return(depth);
 }
@@ -543,14 +534,6 @@ MagickExport size_t GetImageDepth(const Image *image,ExceptionInfo *exception)
 %      a maximum of MAGICKCORE_QUANTUM_DEPTH.
 %
 */
-
-static inline double MagickMin(const double x,const double y)
-{
-  if (x < y)
-    return(x);
-  return(y);
-}
-
 MagickExport size_t GetImageQuantumDepth(const Image *image,
   const MagickBooleanType constrain)
 {
@@ -585,76 +568,68 @@ MagickExport size_t GetImageQuantumDepth(const Image *image,
 %                                                                             %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
-%  GetImageType() returns the potential type of image:
+%  GetImageType() returns the type of image:
 %
 %        Bilevel         Grayscale        GrayscaleMatte
 %        Palette         PaletteMatte     TrueColor
 %        TrueColorMatte  ColorSeparation  ColorSeparationMatte
 %
-%  To ensure the image type matches its potential, use SetImageType():
-%
-%    (void) SetImageType(image,GetImageType(image));
-%
 %  The format of the GetImageType method is:
 %
-%      ImageType GetImageType(const Image *image,ExceptionInfo *exception)
+%      ImageType GetImageType(const Image *image)
 %
 %  A description of each parameter follows:
 %
 %    o image: the image.
 %
-%    o exception: return any errors or warnings in this structure.
-%
 */
-MagickExport ImageType GetImageType(const Image *image,ExceptionInfo *exception)
+MagickExport ImageType GetImageType(const Image *image)
 {
   assert(image != (Image *) NULL);
-  assert(image->signature == MagickSignature);
-  if (image->debug != MagickFalse)
-    (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
+  assert(image->signature == MagickCoreSignature);
   if (image->colorspace == CMYKColorspace)
     {
-      if (image->alpha_trait != BlendPixelTrait)
+      if (image->alpha_trait == UndefinedPixelTrait)
         return(ColorSeparationType);
-      return(ColorSeparationMatteType);
+      return(ColorSeparationAlphaType);
     }
-  if (IsImageMonochrome(image,exception) != MagickFalse)
+  if (IsImageMonochrome(image) != MagickFalse)
     return(BilevelType);
-  if (IsImageGray(image,exception) != MagickFalse)
+  if (IsImageGray(image) != MagickFalse)
     {
-      if (image->alpha_trait == BlendPixelTrait)
-        return(GrayscaleMatteType);
+      if (image->alpha_trait != UndefinedPixelTrait)
+        return(GrayscaleAlphaType);
       return(GrayscaleType);
     }
-  if (IsPaletteImage(image,exception) != MagickFalse)
+  if (IsPaletteImage(image) != MagickFalse)
     {
-      if (image->alpha_trait == BlendPixelTrait)
-        return(PaletteMatteType);
+      if (image->alpha_trait != UndefinedPixelTrait)
+        return(PaletteAlphaType);
       return(PaletteType);
     }
-  if (image->alpha_trait == BlendPixelTrait)
-    return(TrueColorMatteType);
+  if (image->alpha_trait != UndefinedPixelTrait)
+    return(TrueColorAlphaType);
   return(TrueColorType);
 }
-\f
+
 /*
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %                                                                             %
 %                                                                             %
 %                                                                             %
-%     I s I m a g e G r a y                                                   %
+%     I d e n t i f y I m a g e G r a y                                       %
 %                                                                             %
 %                                                                             %
 %                                                                             %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
-%  IsImageGray() returns MagickTrue if all the pixels in the image have the
-%  same red, green, and blue intensities.
+%  IdentifyImageGray() returns grayscale if all the pixels in the image have
+%  the same red, green, and blue intensities, and bi-level is the intensity is
+%  either 0 or QuantumRange. Otherwise undefined is returned.
 %
-%  The format of the IsImageGray method is:
+%  The format of the IdentifyImageGray method is:
 %
-%      MagickBooleanType IsImageGray(const Image *image,
-%        ExceptionInfo *exception)
+%      ImageType IdentifyImageGray(const Image *image,ExceptionInfo *exception)
 %
 %  A description of each parameter follows:
 %
@@ -663,7 +638,7 @@ MagickExport ImageType GetImageType(const Image *image,ExceptionInfo *exception)
 %    o exception: return any errors or warnings in this structure.
 %
 */
-MagickExport MagickBooleanType IsImageGray(const Image *image,
+MagickExport ImageType IdentifyImageGray(const Image *image,
   ExceptionInfo *exception)
 {
   CacheView
@@ -682,15 +657,14 @@ MagickExport MagickBooleanType IsImageGray(const Image *image,
     y;
 
   assert(image != (Image *) NULL);
-  assert(image->signature == MagickSignature);
+  assert(image->signature == MagickCoreSignature);
   if (image->debug != MagickFalse)
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
   if ((image->type == BilevelType) || (image->type == GrayscaleType) ||
-      (image->type == GrayscaleMatteType))
-    return(MagickTrue);
-  if ((IsGrayColorspace(image->colorspace) == MagickFalse) &&
-      (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse))
-    return(MagickFalse);
+      (image->type == GrayscaleAlphaType))
+    return(image->type);
+  if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
+    return(UndefinedType);
   type=BilevelType;
   image_view=AcquireVirtualCacheView(image,exception);
   for (y=0; y < (ssize_t) image->rows; y++)
@@ -714,33 +688,29 @@ MagickExport MagickBooleanType IsImageGray(const Image *image,
       break;
   }
   image_view=DestroyCacheView(image_view);
-  if (type == UndefinedType)
-    return(MagickFalse);
-  ((Image *) image)->colorspace=GRAYColorspace;
-  ((Image *) image)->type=type;
-  if ((type == GrayscaleType) && (image->alpha_trait == BlendPixelTrait))
-    ((Image *) image)->type=GrayscaleMatteType;
-  return(SyncImagePixelCache((Image *) image,exception));
+  if ((type == GrayscaleType) && (image->alpha_trait != UndefinedPixelTrait))
+    type=GrayscaleAlphaType;
+  return(type);
 }
-\f
+
 /*
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %                                                                             %
 %                                                                             %
 %                                                                             %
-%   I s I m a g e M o n o c h r o m e                                         %
+%   I d e n t i f y I m a g e M o n o c h r o m e                             %
 %                                                                             %
 %                                                                             %
 %                                                                             %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
-%  IsImageMonochrome() returns MagickTrue if all the pixels in the image have
-%  the same red, green, and blue intensities and the intensity is either
+%  IdentifyImageMonochrome() returns MagickTrue if all the pixels in the image
+%  have the same red, green, and blue intensities and the intensity is either
 %  0 or QuantumRange.
 %
-%  The format of the IsImageMonochrome method is:
+%  The format of the IdentifyImageMonochrome method is:
 %
-%      MagickBooleanType IsImageMonochrome(const Image *image,
+%      MagickBooleanType IdentifyImageMonochrome(const Image *image,
 %        ExceptionInfo *exception)
 %
 %  A description of each parameter follows:
@@ -750,7 +720,7 @@ MagickExport MagickBooleanType IsImageGray(const Image *image,
 %    o exception: return any errors or warnings in this structure.
 %
 */
-MagickExport MagickBooleanType IsImageMonochrome(const Image *image,
+MagickExport MagickBooleanType IdentifyImageMonochrome(const Image *image,
   ExceptionInfo *exception)
 {
   CacheView
@@ -769,13 +739,12 @@ MagickExport MagickBooleanType IsImageMonochrome(const Image *image,
     y;
 
   assert(image != (Image *) NULL);
-  assert(image->signature == MagickSignature);
+  assert(image->signature == MagickCoreSignature);
   if (image->debug != MagickFalse)
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
   if (image->type == BilevelType)
     return(MagickTrue);
-  if ((IsGrayColorspace(image->colorspace) == MagickFalse) &&
-      (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse))
+  if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
     return(MagickFalse);
   type=BilevelType;
   image_view=AcquireVirtualCacheView(image,exception);
@@ -797,11 +766,137 @@ MagickExport MagickBooleanType IsImageMonochrome(const Image *image,
       break;
   }
   image_view=DestroyCacheView(image_view);
-  if (type == UndefinedType)
-    return(MagickFalse);
-  ((Image *) image)->colorspace=GRAYColorspace;
-  ((Image *) image)->type=type;
-  return(SyncImagePixelCache((Image *) image,exception));
+  if (type == BilevelType)
+    return(MagickTrue);
+  return(MagickFalse);
+}
+
+/*
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%                                                                             %
+%                                                                             %
+%                                                                             %
+%   I d e n t i f y I m a g e T y p e                                         %
+%                                                                             %
+%                                                                             %
+%                                                                             %
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%
+%  IdentifyImageType() returns the potential type of image:
+%
+%        Bilevel         Grayscale        GrayscaleMatte
+%        Palette         PaletteMatte     TrueColor
+%        TrueColorMatte  ColorSeparation  ColorSeparationMatte
+%
+%  To ensure the image type matches its potential, use SetImageType():
+%
+%    (void) SetImageType(image,IdentifyImageType(image,exception),exception);
+%
+%  The format of the IdentifyImageType method is:
+%
+%      ImageType IdentifyImageType(const Image *image,ExceptionInfo *exception)
+%
+%  A description of each parameter follows:
+%
+%    o image: the image.
+%
+%    o exception: return any errors or warnings in this structure.
+%
+*/
+MagickExport ImageType IdentifyImageType(const Image *image,
+  ExceptionInfo *exception)
+{
+  assert(image != (Image *) NULL);
+  assert(image->signature == MagickCoreSignature);
+  if (image->debug != MagickFalse)
+    (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
+  if (image->colorspace == CMYKColorspace)
+    {
+      if (image->alpha_trait == UndefinedPixelTrait)
+        return(ColorSeparationType);
+      return(ColorSeparationAlphaType);
+    }
+  if (IdentifyImageMonochrome(image,exception) != MagickFalse)
+    return(BilevelType);
+  if (IdentifyImageGray(image,exception) != UndefinedType)
+    {
+      if (image->alpha_trait != UndefinedPixelTrait)
+        return(GrayscaleAlphaType);
+      return(GrayscaleType);
+    }
+  if (IdentifyPaletteImage(image,exception) != MagickFalse)
+    {
+      if (image->alpha_trait != UndefinedPixelTrait)
+        return(PaletteAlphaType);
+      return(PaletteType);
+    }
+  if (image->alpha_trait != UndefinedPixelTrait)
+    return(TrueColorAlphaType);
+  return(TrueColorType);
+}
+
+/*
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%                                                                             %
+%                                                                             %
+%                                                                             %
+%     I s I m a g e G r a y                                                   %
+%                                                                             %
+%                                                                             %
+%                                                                             %
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%
+%  IsImageGray() returns MagickTrue if the type of the image is grayscale or
+%  bi-level.
+%
+%  The format of the IsImageGray method is:
+%
+%      MagickBooleanType IsImageGray(const Image *image)
+%
+%  A description of each parameter follows:
+%
+%    o image: the image.
+%
+*/
+MagickExport MagickBooleanType IsImageGray(const Image *image)
+{
+  assert(image != (Image *) NULL);
+  assert(image->signature == MagickCoreSignature);
+  if ((image->type == BilevelType) || (image->type == GrayscaleType) ||
+      (image->type == GrayscaleAlphaType))
+    return(MagickTrue);
+  return(MagickFalse);
+}
+\f
+/*
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%                                                                             %
+%                                                                             %
+%                                                                             %
+%   I s I m a g e M o n o c h r o m e                                         %
+%                                                                             %
+%                                                                             %
+%                                                                             %
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%
+%  IsImageMonochrome() returns MagickTrue if type of the image is bi-level.
+%
+%  The format of the IsImageMonochrome method is:
+%
+%      MagickBooleanType IsImageMonochrome(const Image *image)
+%
+%  A description of each parameter follows:
+%
+%    o image: the image.
+%
+*/
+MagickExport MagickBooleanType IsImageMonochrome(const Image *image)
+{
+  assert(image != (Image *) NULL);
+  assert(image->signature == MagickCoreSignature);
+  if (image->type == BilevelType)
+    return(MagickTrue);
+  return(MagickFalse);
 }
 \f
 /*
@@ -851,10 +946,10 @@ MagickExport MagickBooleanType IsImageOpaque(const Image *image,
     Determine if image is opaque.
   */
   assert(image != (Image *) NULL);
-  assert(image->signature == MagickSignature);
+  assert(image->signature == MagickCoreSignature);
   if (image->debug != MagickFalse)
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
-  if (image->alpha_trait != BlendPixelTrait)
+  if (image->alpha_trait == UndefinedPixelTrait)
     return(MagickTrue);
   image_view=AcquireVirtualCacheView(image,exception);
   for (y=0; y < (ssize_t) image->rows; y++)
@@ -869,7 +964,7 @@ MagickExport MagickBooleanType IsImageOpaque(const Image *image,
       p+=GetPixelChannels(image);
     }
     if (x < (ssize_t) image->columns)
-     break;
+      break;
   }
   image_view=DestroyCacheView(image_view);
   return(y < (ssize_t) image->rows ? MagickFalse : MagickTrue);
@@ -922,7 +1017,7 @@ MagickExport MagickBooleanType SetImageDepth(Image *image,
   assert(image != (Image *) NULL);
   if (image->debug != MagickFalse)
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
-  assert(image->signature == MagickSignature);
+  assert(image->signature == MagickCoreSignature);
   if (depth >= MAGICKCORE_QUANTUM_DEPTH)
     {
       image->depth=depth;
@@ -942,16 +1037,16 @@ MagickExport MagickBooleanType SetImageDepth(Image *image,
       {
         if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
           image->colormap[i].red=(double) ScaleAnyToQuantum(ScaleQuantumToAny(
-            ClampToQuantum(image->colormap[i].red),range),range);
+            ClampPixel(image->colormap[i].red),range),range);
         if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
           image->colormap[i].green=(double) ScaleAnyToQuantum(ScaleQuantumToAny(
-            ClampToQuantum(image->colormap[i].green),range),range);
+            ClampPixel(image->colormap[i].green),range),range);
         if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
           image->colormap[i].blue=(double) ScaleAnyToQuantum(ScaleQuantumToAny(
-            ClampToQuantum(image->colormap[i].blue),range),range);
+            ClampPixel(image->colormap[i].blue),range),range);
         if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
           image->colormap[i].alpha=(double) ScaleAnyToQuantum(ScaleQuantumToAny(
-            ClampToQuantum(image->colormap[i].alpha),range),range);
+            ClampPixel(image->colormap[i].alpha),range),range);
       }
     }
   status=MagickTrue;
@@ -1016,7 +1111,8 @@ MagickExport MagickBooleanType SetImageDepth(Image *image,
             channel=GetPixelChannelChannel(image,i);
             traits=GetPixelChannelTraits(image,channel);
             if ((traits == UndefinedPixelTrait) ||
-                (channel == IndexPixelChannel) || (channel == ReadMaskPixelChannel))
+                (channel == IndexPixelChannel) ||
+                (channel == ReadMaskPixelChannel))
               continue;
             q[i]=depth_map[ScaleQuantumToMap(q[i])];
           }
@@ -1081,7 +1177,7 @@ MagickExport MagickBooleanType SetImageDepth(Image *image,
         if ((traits == UndefinedPixelTrait) || (channel == IndexPixelChannel) ||
             (channel == ReadMaskPixelChannel))
           continue;
-        q[i]=ScaleAnyToQuantum(ScaleQuantumToAny(q[i],range),range);
+        q[i]=ScaleAnyToQuantum(ScaleQuantumToAny(ClampPixel(q[i]),range),range);
       }
       q+=GetPixelChannels(image);
     }
@@ -1147,7 +1243,7 @@ MagickExport MagickBooleanType SetImageType(Image *image,const ImageType type,
   assert(image != (Image *) NULL);
   if (image->debug != MagickFalse)
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
-  assert(image->signature == MagickSignature);
+  assert(image->signature == MagickCoreSignature);
   status=MagickTrue;
   image_info=AcquireImageInfo();
   image_info->dither=image->dither;
@@ -1158,29 +1254,32 @@ MagickExport MagickBooleanType SetImageType(Image *image,const ImageType type,
   {
     case BilevelType:
     {
-      if (IsImageMonochrome(image,exception) == MagickFalse)
+      if (SetImageMonochrome(image,exception) == MagickFalse)
         {
+          status=TransformImageColorspace(image,GRAYColorspace,exception);
+          (void) NormalizeImage(image,exception);
           quantize_info=AcquireQuantizeInfo(image_info);
           quantize_info->number_colors=2;
           quantize_info->colorspace=GRAYColorspace;
           status=QuantizeImage(quantize_info,image,exception);
           quantize_info=DestroyQuantizeInfo(quantize_info);
         }
+      image->colors=2;
       image->alpha_trait=UndefinedPixelTrait;
       break;
     }
     case GrayscaleType:
     {
-      if (IsImageGray(image,exception) == MagickFalse)
+      if (SetImageGray(image,exception) == MagickFalse)
         status=TransformImageColorspace(image,GRAYColorspace,exception);
       image->alpha_trait=UndefinedPixelTrait;
       break;
     }
-    case GrayscaleMatteType:
+    case GrayscaleAlphaType:
     {
-      if (IsImageGray(image,exception) == MagickFalse)
+      if (SetImageGray(image,exception) == MagickFalse)
         status=TransformImageColorspace(image,GRAYColorspace,exception);
-      if (image->alpha_trait != BlendPixelTrait)
+      if (image->alpha_trait == UndefinedPixelTrait)
         (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
       break;
     }
@@ -1198,14 +1297,14 @@ MagickExport MagickBooleanType SetImageType(Image *image,const ImageType type,
       image->alpha_trait=UndefinedPixelTrait;
       break;
     }
-    case PaletteBilevelMatteType:
+    case PaletteBilevelAlphaType:
     {
       ChannelType
         channel_mask;
 
       if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
         status=TransformImageColorspace(image,sRGBColorspace,exception);
-      if (image->alpha_trait != BlendPixelTrait)
+      if (image->alpha_trait == UndefinedPixelTrait)
         (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
       channel_mask=SetImageChannelMask(image,AlphaChannel);
       (void) BilevelImage(image,(double) QuantumRange/2.0,exception);
@@ -1215,11 +1314,11 @@ MagickExport MagickBooleanType SetImageType(Image *image,const ImageType type,
       quantize_info=DestroyQuantizeInfo(quantize_info);
       break;
     }
-    case PaletteMatteType:
+    case PaletteAlphaType:
     {
       if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
         status=TransformImageColorspace(image,sRGBColorspace,exception);
-      if (image->alpha_trait != BlendPixelTrait)
+      if (image->alpha_trait == UndefinedPixelTrait)
         (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
       quantize_info=AcquireQuantizeInfo(image_info);
       quantize_info->colorspace=TransparentColorspace;
@@ -1236,13 +1335,13 @@ MagickExport MagickBooleanType SetImageType(Image *image,const ImageType type,
       image->alpha_trait=UndefinedPixelTrait;
       break;
     }
-    case TrueColorMatteType:
+    case TrueColorAlphaType:
     {
       if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
         status=TransformImageColorspace(image,sRGBColorspace,exception);
       if (image->storage_class != DirectClass)
         status=SetImageStorageClass(image,DirectClass,exception);
-      if (image->alpha_trait != BlendPixelTrait)
+      if (image->alpha_trait == UndefinedPixelTrait)
         (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
       break;
     }
@@ -1259,7 +1358,7 @@ MagickExport MagickBooleanType SetImageType(Image *image,const ImageType type,
       image->alpha_trait=UndefinedPixelTrait;
       break;
     }
-    case ColorSeparationMatteType:
+    case ColorSeparationAlphaType:
     {
       if (image->colorspace != CMYKColorspace)
         {
@@ -1269,7 +1368,7 @@ MagickExport MagickBooleanType SetImageType(Image *image,const ImageType type,
         }
       if (image->storage_class != DirectClass)
         status=SetImageStorageClass(image,DirectClass,exception);
-      if (image->alpha_trait != BlendPixelTrait)
+      if (image->alpha_trait == UndefinedPixelTrait)
         status=SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
       break;
     }
@@ -1277,7 +1376,9 @@ MagickExport MagickBooleanType SetImageType(Image *image,const ImageType type,
     case UndefinedType:
       break;
   }
-  image->type=type;
   image_info=DestroyImageInfo(image_info);
-  return(status);
+  if (status == MagickFalse)
+    return(status);
+  image->type=type;
+  return(MagickTrue);
 }