]> granicus.if.org Git - imagemagick/blobdiff - MagickCore/enhance.c
(no commit message)
[imagemagick] / MagickCore / enhance.c
index 2561b8699ce805e83001c9772c118deb6c4ffda6..ca2399db09801811fd3630b38d4c4363c4bbfc66 100644 (file)
@@ -17,7 +17,7 @@
 %                                 July 1992                                   %
 %                                                                             %
 %                                                                             %
-%  Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization      %
+%  Copyright 1999-2012 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 "MagickCore/color.h"
 #include "MagickCore/color-private.h"
 #include "MagickCore/colorspace.h"
+#include "MagickCore/colorspace-private.h"
 #include "MagickCore/composite-private.h"
 #include "MagickCore/enhance.h"
 #include "MagickCore/exception.h"
 #include "MagickCore/exception-private.h"
 #include "MagickCore/fx.h"
 #include "MagickCore/gem.h"
+#include "MagickCore/gem-private.h"
 #include "MagickCore/geometry.h"
 #include "MagickCore/histogram.h"
 #include "MagickCore/image.h"
@@ -61,6 +63,7 @@
 #include "MagickCore/monitor.h"
 #include "MagickCore/monitor-private.h"
 #include "MagickCore/option.h"
+#include "MagickCore/pixel.h"
 #include "MagickCore/pixel-accessor.h"
 #include "MagickCore/quantum.h"
 #include "MagickCore/quantum-private.h"
@@ -72,6 +75,7 @@
 #include "MagickCore/thread-private.h"
 #include "MagickCore/token.h"
 #include "MagickCore/xml-tree.h"
+#include "MagickCore/xml-tree-private.h"
 \f
 /*
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -114,7 +118,7 @@ MagickExport MagickBooleanType AutoGammaImage(Image *image,
     i;
 
   log_mean=log(0.5);
-  if (image->sync != MagickFalse)
+  if (image->channel_mask == DefaultChannels)
     {
       /*
         Apply gamma correction equally across all given channels.
@@ -132,13 +136,17 @@ MagickExport MagickBooleanType AutoGammaImage(Image *image,
     ChannelType
       channel_mask;
 
+    PixelChannel
+      channel;
+
     PixelTrait
       traits;
 
-    traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
+    channel=GetPixelChannelMapChannel(image,i);
+    traits=GetPixelChannelMapTraits(image,channel);
     if ((traits & UpdatePixelTrait) == 0)
       continue;
-    channel_mask=SetPixelChannelMask(image,(ChannelType) i);
+    channel_mask=SetPixelChannelMask(image,(ChannelType) (1 << i));
     status=GetImageMean(image,&mean,&sans,exception);
     gamma=log(mean*QuantumScale)/log_mean;
     status&=LevelImage(image,0.0,(double) QuantumRange,gamma,exception);
@@ -177,7 +185,7 @@ MagickExport MagickBooleanType AutoGammaImage(Image *image,
 MagickExport MagickBooleanType AutoLevelImage(Image *image,
   ExceptionInfo *exception)
 {
-  return(MinMaxStretchImage(image,0.0,0.0,exception));
+  return(MinMaxStretchImage(image,0.0,0.0,1.0,exception));
 }
 \f
 /*
@@ -275,7 +283,7 @@ MagickExport MagickBooleanType BrightnessContrastImage(Image *image,
 %  The format of the ClutImage method is:
 %
 %      MagickBooleanType ClutImage(Image *image,Image *clut_image,
-%        ExceptionInfo *exception)
+%        const PixelInterpolateMethod method,ExceptionInfo *exception)
 %
 %  A description of each parameter follows:
 %
@@ -283,13 +291,13 @@ MagickExport MagickBooleanType BrightnessContrastImage(Image *image,
 %
 %    o clut_image: the color lookup table image for replacement color values.
 %
-%    o channel: the channel.
+%    o method: the pixel interpolation method.
 %
 %    o exception: return any errors or warnings in this structure.
 %
 */
 MagickExport MagickBooleanType ClutImage(Image *image,const Image *clut_image,
-  ExceptionInfo *exception)
+  const PixelInterpolateMethod method,ExceptionInfo *exception)
 {
 #define ClutImageTag  "Clut/Image"
 
@@ -297,20 +305,19 @@ MagickExport MagickBooleanType ClutImage(Image *image,const Image *clut_image,
     *clut_view,
     *image_view;
 
-  double
-    *clut_map;
-
   MagickBooleanType
     status;
 
   MagickOffsetType
     progress;
 
+  PixelInfo
+    *clut_map;
+
   register ssize_t
-    x;
+    i;
 
-  ssize_t
-    adjust,
+  ssize_t adjust,
     y;
 
   assert(image != (Image *) NULL);
@@ -321,9 +328,10 @@ MagickExport MagickBooleanType ClutImage(Image *image,const Image *clut_image,
   assert(clut_image->signature == MagickSignature);
   if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
     return(MagickFalse);
-  clut_map=(double *) AcquireQuantumMemory(MaxMap+1UL,GetPixelChannels(image)*
-    sizeof(*clut_map));
-  if (clut_map == (double *) NULL)
+  if (IsGrayColorspace(image->colorspace) != MagickFalse)
+    (void) TransformImageColorspace(image,sRGBColorspace,exception);
+  clut_map=(PixelInfo *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*clut_map));
+  if (clut_map == (PixelInfo *) NULL)
     ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
       image->filename);
   /*
@@ -332,28 +340,27 @@ MagickExport MagickBooleanType ClutImage(Image *image,const Image *clut_image,
   status=MagickTrue;
   progress=0;
   adjust=(ssize_t) (clut_image->interpolate == IntegerInterpolatePixel ? 0 : 1);
-  clut_view=AcquireCacheView(clut_image);
+  clut_view=AcquireVirtualCacheView(clut_image,exception);
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp parallel for schedule(dynamic,4)
+  #pragma omp parallel for schedule(static,4)
 #endif
-  for (x=0; x <= (ssize_t) MaxMap; x++)
+  for (i=0; i <= (ssize_t) MaxMap; i++)
   {
-    register ssize_t
-      i;
-
-    for (i=0; i < (ssize_t) GetPixelChannels(clut_image); i++)
-      (void) InterpolatePixelChannel(clut_image,clut_view,(PixelChannel) i,
-        UndefinedInterpolatePixel,QuantumScale*x*(clut_image->columns-adjust),
-        QuantumScale*x*(clut_image->rows-adjust),clut_map+x*
-        GetPixelChannels(clut_image)+i,exception);
+    GetPixelInfo(clut_image,clut_map+i);
+    (void) InterpolatePixelInfo(clut_image,clut_view,method,
+      QuantumScale*i*(clut_image->columns-adjust),QuantumScale*i*
+      (clut_image->rows-adjust),clut_map+i,exception);
   }
   clut_view=DestroyCacheView(clut_view);
-  image_view=AcquireCacheView(image);
+  image_view=AcquireAuthenticCacheView(image,exception);
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
+  #pragma omp parallel for schedule(static,4) shared(progress,status)
 #endif
   for (y=0; y < (ssize_t) image->rows; y++)
   {
+    PixelInfo
+      pixel;
+
     register Quantum
       *restrict q;
 
@@ -368,32 +375,21 @@ MagickExport MagickBooleanType ClutImage(Image *image,const Image *clut_image,
         status=MagickFalse;
         continue;
       }
+    GetPixelInfo(image,&pixel);
     for (x=0; x < (ssize_t) image->columns; x++)
     {
-      register ssize_t
-        i;
-
-      for (i=0; i < (ssize_t) GetPixelChannels(clut_image); i++)
-      {
-        PixelChannel
-          channel;
-
-        PixelTrait
-          clut_traits,
-          traits;
-
-        clut_traits=GetPixelChannelMapTraits(clut_image,(PixelChannel) i);
-        if (clut_traits == UndefinedPixelTrait)
-          continue;
-        channel=GetPixelChannelMapChannel(clut_image,(PixelChannel) i);
-        traits=GetPixelChannelMapTraits(clut_image,channel);
-        if (traits == UndefinedPixelTrait)
-          continue;
-        if ((traits & UpdatePixelTrait) == 0)
+      if (GetPixelMask(image,q) != 0)
+        {
+          q+=GetPixelChannels(image);
           continue;
-        q[channel]=ClampToQuantum(clut_map[ScaleQuantumToMap(q[channel])*
-          GetPixelChannels(clut_image)+channel]);
-      }
+        }
+      GetPixelInfoPixel(image,q,&pixel);
+      pixel.red=clut_map[ScaleQuantumToMap(pixel.red)].red;
+      pixel.green=clut_map[ScaleQuantumToMap(pixel.green)].green;
+      pixel.blue=clut_map[ScaleQuantumToMap(pixel.blue)].blue;
+      pixel.black=clut_map[ScaleQuantumToMap(pixel.black)].black;
+      pixel.alpha=clut_map[ScaleQuantumToMap(pixel.alpha)].alpha;
+      SetPixelInfoPixel(image,&pixel,q);
       q+=GetPixelChannels(image);
     }
     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
@@ -404,7 +400,7 @@ MagickExport MagickBooleanType ClutImage(Image *image,const Image *clut_image,
           proceed;
 
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp critical (MagickCore_ClutImage)
+        #pragma omp critical (MagickCore_ClutImage)
 #endif
         proceed=SetImageProgress(image,ClutImageTag,progress++,image->rows);
         if (proceed == MagickFalse)
@@ -412,7 +408,7 @@ MagickExport MagickBooleanType ClutImage(Image *image,const Image *clut_image,
       }
   }
   image_view=DestroyCacheView(image_view);
-  clut_map=(double *) RelinquishMagickMemory(clut_map);
+  clut_map=(PixelInfo *) RelinquishMagickMemory(clut_map);
   if ((clut_image->matte != MagickFalse) &&
       ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0))
     (void) SetImageAlphaChannel(image,ActivateAlphaChannel,exception);
@@ -507,7 +503,7 @@ MagickExport MagickBooleanType ColorDecisionListImage(Image *image,
   MagickOffsetType
     progress;
 
-  PixelPacket
+  PixelInfo
     *cdl_map;
 
   register ssize_t
@@ -572,19 +568,18 @@ MagickExport MagickBooleanType ColorDecisionListImage(Image *image,
             {
               case 0:
               {
-                color_correction.red.slope=InterpretLocaleValue(token,
-                  (char **) NULL);
+                color_correction.red.slope=StringToDouble(token,(char **) NULL);
                 break;
               }
               case 1:
               {
-                color_correction.green.slope=InterpretLocaleValue(token,
+                color_correction.green.slope=StringToDouble(token,
                   (char **) NULL);
                 break;
               }
               case 2:
               {
-                color_correction.blue.slope=InterpretLocaleValue(token,
+                color_correction.blue.slope=StringToDouble(token,
                   (char **) NULL);
                 break;
               }
@@ -605,19 +600,19 @@ MagickExport MagickBooleanType ColorDecisionListImage(Image *image,
             {
               case 0:
               {
-                color_correction.red.offset=InterpretLocaleValue(token,
+                color_correction.red.offset=StringToDouble(token,
                   (char **) NULL);
                 break;
               }
               case 1:
               {
-                color_correction.green.offset=InterpretLocaleValue(token,
+                color_correction.green.offset=StringToDouble(token,
                   (char **) NULL);
                 break;
               }
               case 2:
               {
-                color_correction.blue.offset=InterpretLocaleValue(token,
+                color_correction.blue.offset=StringToDouble(token,
                   (char **) NULL);
                 break;
               }
@@ -638,19 +633,18 @@ MagickExport MagickBooleanType ColorDecisionListImage(Image *image,
             {
               case 0:
               {
-                color_correction.red.power=InterpretLocaleValue(token,
-                  (char **) NULL);
+                color_correction.red.power=StringToDouble(token,(char **) NULL);
                 break;
               }
               case 1:
               {
-                color_correction.green.power=InterpretLocaleValue(token,
+                color_correction.green.power=StringToDouble(token,
                   (char **) NULL);
                 break;
               }
               case 2:
               {
-                color_correction.blue.power=InterpretLocaleValue(token,
+                color_correction.blue.power=StringToDouble(token,
                   (char **) NULL);
                 break;
               }
@@ -670,8 +664,7 @@ MagickExport MagickBooleanType ColorDecisionListImage(Image *image,
           content=GetXMLTreeContent(saturation);
           p=(const char *) content;
           GetMagickToken(p,&p,token);
-          color_correction.saturation=InterpretLocaleValue(token,
-            (char **) NULL);
+          color_correction.saturation=StringToDouble(token,(char **) NULL);
         }
     }
   ccc=DestroyXMLTree(ccc);
@@ -700,24 +693,24 @@ MagickExport MagickBooleanType ColorDecisionListImage(Image *image,
       (void) LogMagickEvent(TransformEvent,GetMagickModule(),
         "  color_correction.saturation: %g",color_correction.saturation);
     }
-  cdl_map=(PixelPacket *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*cdl_map));
-  if (cdl_map == (PixelPacket *) NULL)
+  cdl_map=(PixelInfo *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*cdl_map));
+  if (cdl_map == (PixelInfo *) NULL)
     ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
       image->filename);
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp parallel for schedule(dynamic,4)
+  #pragma omp parallel for schedule(static,4)
 #endif
   for (i=0; i <= (ssize_t) MaxMap; i++)
   {
-    cdl_map[i].red=ClampToQuantum((MagickRealType) ScaleMapToQuantum(
-      (MagickRealType) (MaxMap*(pow(color_correction.red.slope*i/MaxMap+
-      color_correction.red.offset,color_correction.red.power)))));
-    cdl_map[i].green=ClampToQuantum((MagickRealType) ScaleMapToQuantum(
-      (MagickRealType) (MaxMap*(pow(color_correction.green.slope*i/MaxMap+
-      color_correction.green.offset,color_correction.green.power)))));
-    cdl_map[i].blue=ClampToQuantum((MagickRealType) ScaleMapToQuantum(
-      (MagickRealType) (MaxMap*(pow(color_correction.blue.slope*i/MaxMap+
-      color_correction.blue.offset,color_correction.blue.power)))));
+    cdl_map[i].red=(MagickRealType) ScaleMapToQuantum((MagickRealType)
+      (MaxMap*(pow(color_correction.red.slope*i/MaxMap+
+      color_correction.red.offset,color_correction.red.power))));
+    cdl_map[i].green=(MagickRealType) ScaleMapToQuantum((MagickRealType)
+      (MaxMap*(pow(color_correction.green.slope*i/MaxMap+
+      color_correction.green.offset,color_correction.green.power))));
+    cdl_map[i].blue=(MagickRealType) ScaleMapToQuantum((MagickRealType)
+      (MaxMap*(pow(color_correction.blue.slope*i/MaxMap+
+      color_correction.blue.offset,color_correction.blue.power))));
   }
   if (image->storage_class == PseudoClass)
     {
@@ -725,7 +718,7 @@ MagickExport MagickBooleanType ColorDecisionListImage(Image *image,
         Apply transfer function to colormap.
       */
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
+      #pragma omp parallel for schedule(static,4) shared(progress,status)
 #endif
       for (i=0; i < (ssize_t) image->colors; i++)
       {
@@ -734,15 +727,15 @@ MagickExport MagickBooleanType ColorDecisionListImage(Image *image,
 
         luma=0.2126*image->colormap[i].red+0.7152*image->colormap[i].green+
           0.0722*image->colormap[i].blue;
-        image->colormap[i].red=ClampToQuantum(luma+
-          color_correction.saturation*cdl_map[ScaleQuantumToMap(
-          image->colormap[i].red)].red-luma);
-        image->colormap[i].green=ClampToQuantum(luma+
-          color_correction.saturation*cdl_map[ScaleQuantumToMap(
-          image->colormap[i].green)].green-luma);
-        image->colormap[i].blue=ClampToQuantum(luma+
-          color_correction.saturation*cdl_map[ScaleQuantumToMap(
-          image->colormap[i].blue)].blue-luma);
+        image->colormap[i].red=luma+color_correction.saturation*cdl_map[
+          ScaleQuantumToMap(ClampToQuantum(image->colormap[i].red))].red-
+          luma;
+        image->colormap[i].green=luma+color_correction.saturation*cdl_map[
+          ScaleQuantumToMap(ClampToQuantum(image->colormap[i].green))].green-
+          luma;
+        image->colormap[i].blue=luma+color_correction.saturation*cdl_map[
+          ScaleQuantumToMap(ClampToQuantum(image->colormap[i].blue))].blue-
+          luma;
       }
     }
   /*
@@ -750,9 +743,9 @@ MagickExport MagickBooleanType ColorDecisionListImage(Image *image,
   */
   status=MagickTrue;
   progress=0;
-  image_view=AcquireCacheView(image);
+  image_view=AcquireAuthenticCacheView(image,exception);
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
+  #pragma omp parallel for schedule(static,4) shared(progress,status)
 #endif
   for (y=0; y < (ssize_t) image->rows; y++)
   {
@@ -768,7 +761,7 @@ MagickExport MagickBooleanType ColorDecisionListImage(Image *image,
     if (status == MagickFalse)
       continue;
     q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
-    if (q == (const Quantum *) NULL)
+    if (q == (Quantum *) NULL)
       {
         status=MagickFalse;
         continue;
@@ -793,7 +786,7 @@ MagickExport MagickBooleanType ColorDecisionListImage(Image *image,
           proceed;
 
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp critical (MagickCore_ColorDecisionListImageChannel)
+        #pragma omp critical (MagickCore_ColorDecisionListImageChannel)
 #endif
         proceed=SetImageProgress(image,ColorDecisionListCorrectImageTag,
           progress++,image->rows);
@@ -802,7 +795,7 @@ MagickExport MagickBooleanType ColorDecisionListImage(Image *image,
       }
   }
   image_view=DestroyCacheView(image_view);
-  cdl_map=(PixelPacket *) RelinquishMagickMemory(cdl_map);
+  cdl_map=(PixelInfo *) RelinquishMagickMemory(cdl_map);
   return(status);
 }
 \f
@@ -836,7 +829,7 @@ MagickExport MagickBooleanType ColorDecisionListImage(Image *image,
 %
 */
 
-static void Contrast(const int sign,Quantum *red,Quantum *green,Quantum *blue)
+static void Contrast(const int sign,double *red,double *green,double *blue)
 {
   double
     brightness,
@@ -846,9 +839,9 @@ static void Contrast(const int sign,Quantum *red,Quantum *green,Quantum *blue)
   /*
     Enhance contrast: dark color become darker, light color become lighter.
   */
-  assert(red != (Quantum *) NULL);
-  assert(green != (Quantum *) NULL);
-  assert(blue != (Quantum *) NULL);
+  assert(red != (double *) NULL);
+  assert(green != (double *) NULL);
+  assert(blue != (double *) NULL);
   hue=0.0;
   saturation=0.0;
   brightness=0.0;
@@ -905,13 +898,13 @@ MagickExport MagickBooleanType ContrastImage(Image *image,
   */
   status=MagickTrue;
   progress=0;
-  image_view=AcquireCacheView(image);
+  image_view=AcquireAuthenticCacheView(image,exception);
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
+  #pragma omp parallel for schedule(static,4) shared(progress,status)
 #endif
   for (y=0; y < (ssize_t) image->rows; y++)
   {
-    Quantum
+    double
       blue,
       green,
       red;
@@ -925,20 +918,20 @@ MagickExport MagickBooleanType ContrastImage(Image *image,
     if (status == MagickFalse)
       continue;
     q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
-    if (q == (const Quantum *) NULL)
+    if (q == (Quantum *) NULL)
       {
         status=MagickFalse;
         continue;
       }
     for (x=0; x < (ssize_t) image->columns; x++)
     {
-      red=GetPixelRed(image,q);
-      green=GetPixelGreen(image,q);
-      blue=GetPixelBlue(image,q);
+      red=(double) GetPixelRed(image,q);
+      green=(double) GetPixelGreen(image,q);
+      blue=(double) GetPixelBlue(image,q);
       Contrast(sign,&red,&green,&blue);
-      SetPixelRed(image,red,q);
-      SetPixelGreen(image,green,q);
-      SetPixelBlue(image,blue,q);
+      SetPixelRed(image,ClampToQuantum(red),q);
+      SetPixelGreen(image,ClampToQuantum(green),q);
+      SetPixelBlue(image,ClampToQuantum(blue),q);
       q+=GetPixelChannels(image);
     }
     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
@@ -949,7 +942,7 @@ MagickExport MagickBooleanType ContrastImage(Image *image,
           proceed;
 
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp critical (MagickCore_ContrastImage)
+        #pragma omp critical (MagickCore_ContrastImage)
 #endif
         proceed=SetImageProgress(image,ContrastImageTag,progress++,image->rows);
         if (proceed == MagickFalse)
@@ -972,11 +965,11 @@ MagickExport MagickBooleanType ContrastImage(Image *image,
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
 %  ContrastStretchImage() is a simple image enhancement technique that attempts
-%  to improve the contrast in an image by `stretching' the range of intensity
+%  to improve the contrast in an image by 'stretching' the range of intensity
 %  values it contains to span a desired range of values. It differs from the
 %  more sophisticated histogram equalization in that it can only apply a
 %  linear scaling function to the image pixel values.  As a result the
-%  `enhancement' is less harsh.
+%  'enhancement' is less harsh.
 %
 %  The format of the ContrastStretchImage method is:
 %
@@ -1012,15 +1005,18 @@ MagickExport MagickBooleanType ContrastStretchImage(Image *image,
   MagickOffsetType
     progress;
 
-  MagickRealType
-    black[MaxPixelChannels],
+  double
+    *black,
     *histogram,
     *stretch_map,
-    white[MaxPixelChannels];
+    *white;
 
   register ssize_t
     i;
 
+  size_t
+    number_channels;
+
   ssize_t
     y;
 
@@ -1031,17 +1027,23 @@ MagickExport MagickBooleanType ContrastStretchImage(Image *image,
   assert(image->signature == MagickSignature);
   if (image->debug != MagickFalse)
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
-  histogram=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
-    GetPixelChannels(image)*sizeof(*histogram));
-  stretch_map=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
+  black=(double *) AcquireQuantumMemory(GetPixelChannels(image),sizeof(*black));
+  white=(double *) AcquireQuantumMemory(GetPixelChannels(image),sizeof(*white));
+  histogram=(double *) AcquireQuantumMemory(MaxMap+1UL,GetPixelChannels(image)*
+    sizeof(*histogram));
+  stretch_map=(double *) AcquireQuantumMemory(MaxMap+1UL,
     GetPixelChannels(image)*sizeof(*stretch_map));
-  if ((histogram == (MagickRealType *) NULL) ||
-      (stretch_map == (MagickRealType *) NULL))
+  if ((black == (double *) NULL) || (white == (double *) NULL) ||
+      (histogram == (double *) NULL) || (stretch_map == (double *) NULL))
     {
-      if (stretch_map != (MagickRealType *) NULL)
-        stretch_map=(MagickRealType *) RelinquishMagickMemory(stretch_map);
-      if (histogram != (MagickRealType *) NULL)
-        histogram=(MagickRealType *) RelinquishMagickMemory(histogram);
+      if (stretch_map != (double *) NULL)
+        stretch_map=(double *) RelinquishMagickMemory(stretch_map);
+      if (histogram != (double *) NULL)
+        histogram=(double *) RelinquishMagickMemory(histogram);
+      if (white != (double *) NULL)
+        white=(double *) RelinquishMagickMemory(white);
+      if (black != (double *) NULL)
+        black=(double *) RelinquishMagickMemory(black);
       ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
         image->filename);
     }
@@ -1051,7 +1053,7 @@ MagickExport MagickBooleanType ContrastStretchImage(Image *image,
   status=MagickTrue;
   (void) ResetMagickMemory(histogram,0,(MaxMap+1)*GetPixelChannels(image)*
     sizeof(*histogram));
-  image_view=AcquireCacheView(image);
+  image_view=AcquireVirtualCacheView(image,exception);
   for (y=0; y < (ssize_t) image->rows; y++)
   {
     register const Quantum
@@ -1074,25 +1076,19 @@ MagickExport MagickBooleanType ContrastStretchImage(Image *image,
         i;
 
       for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
-      {
-        PixelTrait
-          traits;
-
-        traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
-        if ((traits & UpdatePixelTrait) == 0)
-          continue;
         histogram[GetPixelChannels(image)*ScaleQuantumToMap(p[i])+i]++;
-      }
       p+=GetPixelChannels(image);
     }
   }
+  image_view=DestroyCacheView(image_view);
   /*
     Find the histogram boundaries by locating the black/white levels.
   */
+  number_channels=GetPixelChannels(image);
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
+  #pragma omp parallel for schedule(static,4) shared(progress,status)
 #endif
-  for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
+  for (i=0; i < (ssize_t) number_channels; i++)
   {
     double
       intensity;
@@ -1125,10 +1121,11 @@ MagickExport MagickBooleanType ContrastStretchImage(Image *image,
   */
   (void) ResetMagickMemory(stretch_map,0,(MaxMap+1)*GetPixelChannels(image)*
     sizeof(*stretch_map));
+  number_channels=GetPixelChannels(image);
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
+  #pragma omp parallel for schedule(static,4) shared(progress,status)
 #endif
-  for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
+  for (i=0; i < (ssize_t) number_channels; i++)
   {
     register ssize_t
       j;
@@ -1157,7 +1154,7 @@ MagickExport MagickBooleanType ContrastStretchImage(Image *image,
         Stretch-contrast colormap.
       */
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
+      #pragma omp parallel for schedule(static,4) shared(progress,status)
 #endif
       for (j=0; j < (ssize_t) image->colors; j++)
       {
@@ -1165,33 +1162,29 @@ MagickExport MagickBooleanType ContrastStretchImage(Image *image,
           {
             i=GetPixelChannelMapChannel(image,RedPixelChannel);
             if (black[i] != white[i])
-              image->colormap[i].red=ClampToQuantum(stretch_map[
-                GetPixelChannels(image)*ScaleQuantumToMap(
-                image->colormap[i].red)]+i);
+              image->colormap[j].red=stretch_map[GetPixelChannels(image)*
+                ScaleQuantumToMap(ClampToQuantum(image->colormap[j].red))]+i;
           }
         if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
           {
             i=GetPixelChannelMapChannel(image,GreenPixelChannel);
             if (black[i] != white[i])
-              image->colormap[i].red=ClampToQuantum(stretch_map[
-                GetPixelChannels(image)*ScaleQuantumToMap(
-                image->colormap[i].red)]+i);
+              image->colormap[j].green=stretch_map[GetPixelChannels(image)*
+                ScaleQuantumToMap(ClampToQuantum(image->colormap[j].green))]+i;
           }
         if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
           {
             i=GetPixelChannelMapChannel(image,BluePixelChannel);
             if (black[i] != white[i])
-              image->colormap[i].red=ClampToQuantum(stretch_map[
-                GetPixelChannels(image)*ScaleQuantumToMap(
-                image->colormap[i].red)]+i);
+              image->colormap[j].blue=stretch_map[GetPixelChannels(image)*
+                ScaleQuantumToMap(ClampToQuantum(image->colormap[j].blue))]+i;
           }
         if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
           {
             i=GetPixelChannelMapChannel(image,AlphaPixelChannel);
             if (black[i] != white[i])
-              image->colormap[i].red=ClampToQuantum(stretch_map[
-                GetPixelChannels(image)*ScaleQuantumToMap(
-                image->colormap[i].red)]+i);
+              image->colormap[j].alpha=stretch_map[GetPixelChannels(image)*
+                ScaleQuantumToMap(ClampToQuantum(image->colormap[j].alpha))]+i;
           }
       }
     }
@@ -1200,8 +1193,9 @@ MagickExport MagickBooleanType ContrastStretchImage(Image *image,
   */
   status=MagickTrue;
   progress=0;
+  image_view=AcquireAuthenticCacheView(image,exception);
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
+  #pragma omp parallel for schedule(static,4) shared(progress,status)
 #endif
   for (y=0; y < (ssize_t) image->rows; y++)
   {
@@ -1214,7 +1208,7 @@ MagickExport MagickBooleanType ContrastStretchImage(Image *image,
     if (status == MagickFalse)
       continue;
     q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
-    if (q == (const Quantum *) NULL)
+    if (q == (Quantum *) NULL)
       {
         status=MagickFalse;
         continue;
@@ -1224,17 +1218,25 @@ MagickExport MagickBooleanType ContrastStretchImage(Image *image,
       register ssize_t
         i;
 
+      if (GetPixelMask(image,q) != 0)
+        {
+          q+=GetPixelChannels(image);
+          continue;
+        }
       for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
       {
+        PixelChannel
+          channel;
+
         PixelTrait
           traits;
 
-        traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
-        if ((traits & UpdatePixelTrait) == 0)
+        channel=GetPixelChannelMapChannel(image,i);
+        traits=GetPixelChannelMapTraits(image,channel);
+        if (((traits & UpdatePixelTrait) == 0) || (black[i] == white[i]))
           continue;
-        if (black[i] != white[i])
-          q[i]=ClampToQuantum(stretch_map[GetPixelChannels(image)*
-            ScaleQuantumToMap(q[i])+i]);
+        q[i]=ClampToQuantum(stretch_map[GetPixelChannels(image)*
+          ScaleQuantumToMap(q[i])+i]);
       }
       q+=GetPixelChannels(image);
     }
@@ -1246,7 +1248,7 @@ MagickExport MagickBooleanType ContrastStretchImage(Image *image,
           proceed;
 
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp critical (MagickCore_ContrastStretchImage)
+        #pragma omp critical (MagickCore_ContrastStretchImage)
 #endif
         proceed=SetImageProgress(image,ContrastStretchImageTag,progress++,
           image->rows);
@@ -1255,7 +1257,9 @@ MagickExport MagickBooleanType ContrastStretchImage(Image *image,
       }
   }
   image_view=DestroyCacheView(image_view);
-  stretch_map=(MagickRealType *) RelinquishMagickMemory(stretch_map);
+  stretch_map=(double *) RelinquishMagickMemory(stretch_map);
+  white=(double *) RelinquishMagickMemory(white);
+  black=(double *) RelinquishMagickMemory(black);
   return(status);
 }
 \f
@@ -1287,8 +1291,9 @@ MagickExport MagickBooleanType ContrastStretchImage(Image *image,
 MagickExport Image *EnhanceImage(const Image *image,ExceptionInfo *exception)
 {
 #define EnhancePixel(weight) \
-  mean=((MagickRealType) r[i]+q[channel])/2.0; \
-  distance=(MagickRealType) r[i]-(MagickRealType) q[channel]; \
+  mean=((MagickRealType) r[i]+GetPixelChannel(enhance_image,channel,q))/2.0; \
+  distance=(MagickRealType) r[i]-(MagickRealType) GetPixelChannel( \
+    enhance_image,channel,q); \
   distance_squared=QuantumScale*(2.0*((MagickRealType) QuantumRange+1.0)+ \
     mean)*distance*distance; \
   if (distance_squared < ((MagickRealType) QuantumRange*(MagickRealType) \
@@ -1339,10 +1344,10 @@ MagickExport Image *EnhanceImage(const Image *image,ExceptionInfo *exception)
   */
   status=MagickTrue;
   progress=0;
-  image_view=AcquireCacheView(image);
-  enhance_view=AcquireCacheView(enhance_image);
+  image_view=AcquireVirtualCacheView(image,exception);
+  enhance_view=AcquireAuthenticCacheView(enhance_image,exception);
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
+  #pragma omp parallel for schedule(static,4) shared(progress,status)
 #endif
   for (y=0; y < (ssize_t) image->rows; y++)
   {
@@ -1368,12 +1373,18 @@ MagickExport Image *EnhanceImage(const Image *image,ExceptionInfo *exception)
         status=MagickFalse;
         continue;
       }
-    center=GetPixelChannels(image)*(2*(image->columns+4)+2);
+    center=(ssize_t) GetPixelChannels(image)*(2*(image->columns+4)+2);
     for (x=0; x < (ssize_t) image->columns; x++)
     {
       register ssize_t
         i;
 
+      if (GetPixelMask(image,p) != 0)
+        {
+          p+=GetPixelChannels(image);
+          q+=GetPixelChannels(enhance_image);
+          continue;
+        }
       for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
       {
         MagickRealType
@@ -1393,14 +1404,13 @@ MagickExport Image *EnhanceImage(const Image *image,ExceptionInfo *exception)
         register const Quantum
           *restrict r;
 
-        traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
-        if (traits == UndefinedPixelTrait)
-          continue;
-        channel=GetPixelChannelMapChannel(image,(PixelChannel) i);
+        channel=GetPixelChannelMapChannel(image,i);
+        traits=GetPixelChannelMapTraits(image,channel);
         enhance_traits=GetPixelChannelMapTraits(enhance_image,channel);
-        if (enhance_traits == UndefinedPixelTrait)
+        if ((traits == UndefinedPixelTrait) ||
+            (enhance_traits == UndefinedPixelTrait))
           continue;
-        q[channel]=p[center+i];
+        SetPixelChannel(enhance_image,channel,p[center+i],q);
         if ((enhance_traits & CopyPixelTrait) != 0)
           continue;
         /*
@@ -1408,7 +1418,7 @@ MagickExport Image *EnhanceImage(const Image *image,ExceptionInfo *exception)
         */
         aggregate=0.0;
         total_weight=0.0;
-        r=p+0*GetPixelChannels(image)*(image->columns+4);
+        r=p;
         EnhancePixel(5.0); EnhancePixel(8.0); EnhancePixel(10.0);
           EnhancePixel(8.0); EnhancePixel(5.0);
         r=p+1*GetPixelChannels(image)*(image->columns+4);
@@ -1423,7 +1433,8 @@ MagickExport Image *EnhanceImage(const Image *image,ExceptionInfo *exception)
         r=p+4*GetPixelChannels(image)*(image->columns+4);
         EnhancePixel(5.0); EnhancePixel(8.0); EnhancePixel(10.0);
           EnhancePixel(8.0); EnhancePixel(5.0);
-        q[channel]=ClampToQuantum(aggregate/total_weight);
+        SetPixelChannel(enhance_image,channel,ClampToQuantum(aggregate/
+          total_weight),q);
       }
       p+=GetPixelChannels(image);
       q+=GetPixelChannels(enhance_image);
@@ -1436,7 +1447,7 @@ MagickExport Image *EnhanceImage(const Image *image,ExceptionInfo *exception)
           proceed;
 
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp critical (MagickCore_EnhanceImage)
+        #pragma omp critical (MagickCore_EnhanceImage)
 #endif
         proceed=SetImageProgress(image,EnhanceImageTag,progress++,image->rows);
         if (proceed == MagickFalse)
@@ -1486,17 +1497,19 @@ MagickExport MagickBooleanType EqualizeImage(Image *image,
   MagickOffsetType
     progress;
 
-  PixelInfo
-    black,
+  MagickRealType
+    black[CompositePixelChannel],
     *equalize_map,
     *histogram,
-    intensity,
     *map,
-    white;
+    white[CompositePixelChannel];
 
   register ssize_t
     i;
 
+  size_t
+    number_channels;
+
   ssize_t
     y;
 
@@ -1507,27 +1520,32 @@ MagickExport MagickBooleanType EqualizeImage(Image *image,
   assert(image->signature == MagickSignature);
   if (image->debug != MagickFalse)
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
-  equalize_map=(PixelInfo *) AcquireQuantumMemory(MaxMap+1UL,
-    sizeof(*equalize_map));
-  histogram=(PixelInfo *) AcquireQuantumMemory(MaxMap+1UL,
-    sizeof(*histogram));
-  map=(PixelInfo *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*map));
-  if ((equalize_map == (PixelInfo *) NULL) ||
-      (histogram == (PixelInfo *) NULL) || (map == (PixelInfo *) NULL))
+  equalize_map=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
+    GetPixelChannels(image)*sizeof(*equalize_map));
+  histogram=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
+    GetPixelChannels(image)*sizeof(*histogram));
+  map=(MagickRealType *) AcquireQuantumMemory(MaxMap+1UL,
+    GetPixelChannels(image)*sizeof(*map));
+  if ((equalize_map == (MagickRealType *) NULL) ||
+      (histogram == (MagickRealType *) NULL) ||
+      (map == (MagickRealType *) NULL))
     {
-      if (map != (PixelInfo *) NULL)
-        map=(PixelInfo *) RelinquishMagickMemory(map);
-      if (histogram != (PixelInfo *) NULL)
-        histogram=(PixelInfo *) RelinquishMagickMemory(histogram);
-      if (equalize_map != (PixelInfo *) NULL)
-        equalize_map=(PixelInfo *) RelinquishMagickMemory(equalize_map);
+      if (map != (MagickRealType *) NULL)
+        map=(MagickRealType *) RelinquishMagickMemory(map);
+      if (histogram != (MagickRealType *) NULL)
+        histogram=(MagickRealType *) RelinquishMagickMemory(histogram);
+      if (equalize_map != (MagickRealType *) NULL)
+        equalize_map=(MagickRealType *) RelinquishMagickMemory(equalize_map);
       ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
         image->filename);
     }
   /*
     Form histogram.
   */
-  (void) ResetMagickMemory(histogram,0,(MaxMap+1)*sizeof(*histogram));
+  status=MagickTrue;
+  (void) ResetMagickMemory(histogram,0,(MaxMap+1)*GetPixelChannels(image)*
+    sizeof(*histogram));
+  image_view=AcquireVirtualCacheView(image,exception);
   for (y=0; y < (ssize_t) image->rows; y++)
   {
     register const Quantum
@@ -1536,114 +1554,125 @@ MagickExport MagickBooleanType EqualizeImage(Image *image,
     register ssize_t
       x;
 
-    p=GetVirtualPixels(image,0,y,image->columns,1,exception);
+    if (status == MagickFalse)
+      continue;
+    p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
     if (p == (const Quantum *) NULL)
-      break;
+      {
+        status=MagickFalse;
+        continue;
+      }
     for (x=0; x < (ssize_t) image->columns; x++)
     {
-      if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
-        histogram[ScaleQuantumToMap(GetPixelRed(image,p))].red++;
-      if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
-        histogram[ScaleQuantumToMap(GetPixelGreen(image,p))].green++;
-      if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
-        histogram[ScaleQuantumToMap(GetPixelBlue(image,p))].blue++;
-      if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
-          (image->colorspace == CMYKColorspace))
-        histogram[ScaleQuantumToMap(GetPixelBlack(image,p))].black++;
-      if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
-        histogram[ScaleQuantumToMap(GetPixelAlpha(image,p))].alpha++;
+      register ssize_t
+        i;
+
+      for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
+        histogram[GetPixelChannels(image)*ScaleQuantumToMap(p[i])+i]++;
       p+=GetPixelChannels(image);
     }
   }
+  image_view=DestroyCacheView(image_view);
   /*
     Integrate the histogram to get the equalization map.
   */
-  (void) ResetMagickMemory(&intensity,0,sizeof(intensity));
-  for (i=0; i <= (ssize_t) MaxMap; i++)
+  number_channels=GetPixelChannels(image);
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
+  #pragma omp parallel for schedule(static,4) shared(progress,status)
+#endif
+  for (i=0; i < (ssize_t) number_channels; i++)
   {
-    if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
-      intensity.red+=histogram[i].red;
-    if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
-      intensity.green+=histogram[i].green;
-    if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
-      intensity.blue+=histogram[i].blue;
-    if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
-        (image->colorspace == CMYKColorspace))
-      intensity.black+=histogram[i].black;
-    if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
-      intensity.alpha+=histogram[i].alpha;
-    map[i]=intensity;
+    MagickRealType
+      intensity;
+
+    register ssize_t
+      j;
+
+    intensity=0.0;
+    for (j=0; j <= (ssize_t) MaxMap; j++)
+    {
+      intensity+=histogram[GetPixelChannels(image)*j+i];
+      map[GetPixelChannels(image)*j+i]=intensity;
+    }
   }
-  black=map[0];
-  white=map[(int) MaxMap];
-  (void) ResetMagickMemory(equalize_map,0,(MaxMap+1)*sizeof(*equalize_map));
+  (void) ResetMagickMemory(equalize_map,0,(MaxMap+1)*GetPixelChannels(image)*
+    sizeof(*equalize_map));
+  number_channels=GetPixelChannels(image);
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
+  #pragma omp parallel for schedule(static,4) shared(progress,status)
 #endif
-  for (i=0; i <= (ssize_t) MaxMap; i++)
+  for (i=0; i < (ssize_t) number_channels; i++)
   {
-    if (((GetPixelRedTraits(image) & UpdatePixelTrait) != 0) &&
-        (white.red != black.red))
-      equalize_map[i].red=(MagickRealType) ScaleMapToQuantum((MagickRealType)
-        ((MaxMap*(map[i].red-black.red))/(white.red-black.red)));
-    if (((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0) &&
-        (white.green != black.green))
-      equalize_map[i].green=(MagickRealType) ScaleMapToQuantum((MagickRealType)
-        ((MaxMap*(map[i].green-black.green))/(white.green-black.green)));
-    if (((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0) &&
-        (white.blue != black.blue))
-      equalize_map[i].blue=(MagickRealType) ScaleMapToQuantum((MagickRealType)
-        ((MaxMap*(map[i].blue-black.blue))/(white.blue-black.blue)));
-    if ((((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
-        (image->colorspace == CMYKColorspace)) &&
-        (white.black != black.black))
-      equalize_map[i].black=(MagickRealType) ScaleMapToQuantum((MagickRealType)
-        ((MaxMap*(map[i].black-black.black))/(white.black-black.black)));
-    if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
-        (white.alpha != black.alpha))
-      equalize_map[i].alpha=(MagickRealType) ScaleMapToQuantum(
-        (MagickRealType) ((MaxMap*(map[i].alpha-black.alpha))/
-        (white.alpha-black.alpha)));
+    register ssize_t
+      j;
+
+    black[i]=map[i];
+    white[i]=map[GetPixelChannels(image)*MaxMap+i];
+    if (black[i] != white[i])
+      for (j=0; j <= (ssize_t) MaxMap; j++)
+        equalize_map[GetPixelChannels(image)*j+i]=(MagickRealType)
+          ScaleMapToQuantum((MagickRealType) ((MaxMap*(map[
+          GetPixelChannels(image)*j+i]-black[i]))/(white[i]-black[i])));
   }
-  histogram=(PixelInfo *) RelinquishMagickMemory(histogram);
-  map=(PixelInfo *) RelinquishMagickMemory(map);
+  histogram=(MagickRealType *) RelinquishMagickMemory(histogram);
+  map=(MagickRealType *) RelinquishMagickMemory(map);
   if (image->storage_class == PseudoClass)
     {
+      PixelChannel
+        channel;
+
+      register ssize_t
+        j;
+
       /*
         Equalize colormap.
       */
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
+      #pragma omp parallel for schedule(static,4) shared(progress,status)
 #endif
-      for (i=0; i < (ssize_t) image->colors; i++)
+      for (j=0; j < (ssize_t) image->colors; j++)
       {
-        if (((GetPixelRedTraits(image) & UpdatePixelTrait) != 0) &&
-            (white.red != black.red))
-          image->colormap[i].red=ClampToQuantum(equalize_map[
-            ScaleQuantumToMap(image->colormap[i].red)].red);
-        if (((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0) &&
-            (white.green != black.green))
-          image->colormap[i].green=ClampToQuantum(equalize_map[
-            ScaleQuantumToMap(image->colormap[i].green)].green);
-        if (((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0) &&
-            (white.blue != black.blue))
-          image->colormap[i].blue=ClampToQuantum(equalize_map[
-            ScaleQuantumToMap(image->colormap[i].blue)].blue);
-        if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
-            (white.alpha != black.alpha))
-          image->colormap[i].alpha=ClampToQuantum(equalize_map[
-            ScaleQuantumToMap(image->colormap[i].alpha)].alpha);
+        if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
+          {
+            channel=GetPixelChannelMapChannel(image,RedPixelChannel);
+            if (black[channel] != white[channel])
+              image->colormap[j].red=equalize_map[GetPixelChannels(image)*
+                ScaleQuantumToMap(ClampToQuantum(image->colormap[j].red))]+
+                channel;
+          }
+        if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
+          {
+            channel=GetPixelChannelMapChannel(image,GreenPixelChannel);
+            if (black[channel] != white[channel])
+              image->colormap[j].green=equalize_map[GetPixelChannels(image)*
+                ScaleQuantumToMap(ClampToQuantum(image->colormap[j].green))]+
+                channel;
+          }
+        if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
+          {
+            channel=GetPixelChannelMapChannel(image,BluePixelChannel);
+            if (black[channel] != white[channel])
+              image->colormap[j].blue=equalize_map[GetPixelChannels(image)*
+                ScaleQuantumToMap(ClampToQuantum(image->colormap[j].blue))]+
+                channel;
+          }
+        if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
+          {
+            channel=GetPixelChannelMapChannel(image,AlphaPixelChannel);
+            if (black[channel] != white[channel])
+              image->colormap[j].alpha=equalize_map[GetPixelChannels(image)*
+                ScaleQuantumToMap(ClampToQuantum(image->colormap[j].alpha))]+
+                channel;
+          }
       }
     }
   /*
     Equalize image.
   */
-  status=MagickTrue;
   progress=0;
-  exception=(&image->exception);
-  image_view=AcquireCacheView(image);
+  image_view=AcquireAuthenticCacheView(image,exception);
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
+  #pragma omp parallel for schedule(static,4) shared(progress,status)
 #endif
   for (y=0; y < (ssize_t) image->rows; y++)
   {
@@ -1656,34 +1685,36 @@ MagickExport MagickBooleanType EqualizeImage(Image *image,
     if (status == MagickFalse)
       continue;
     q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
-    if (q == (const Quantum *) NULL)
+    if (q == (Quantum *) NULL)
       {
         status=MagickFalse;
         continue;
       }
     for (x=0; x < (ssize_t) image->columns; x++)
     {
-      if (((GetPixelRedTraits(image) & UpdatePixelTrait) != 0) &&
-          (white.red != black.red))
-        SetPixelRed(image,ClampToQuantum(equalize_map[
-          ScaleQuantumToMap(GetPixelRed(image,q))].red),q);
-      if (((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0) &&
-          (white.green != black.green))
-        SetPixelGreen(image,ClampToQuantum(equalize_map[
-          ScaleQuantumToMap(GetPixelGreen(image,q))].green),q);
-      if (((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0) &&
-          (white.blue != black.blue))
-        SetPixelBlue(image,ClampToQuantum(equalize_map[
-          ScaleQuantumToMap(GetPixelBlue(image,q))].blue),q);
-      if ((((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
-          (image->colorspace == CMYKColorspace)) &&
-          (white.black != black.black))
-        SetPixelBlack(image,ClampToQuantum(equalize_map[
-          ScaleQuantumToMap(GetPixelBlack(image,q))].black),q);
-      if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
-          (white.alpha != black.alpha))
-        SetPixelAlpha(image,ClampToQuantum(equalize_map[
-          ScaleQuantumToMap(GetPixelAlpha(image,q))].alpha),q);
+      register ssize_t
+        i;
+
+      if (GetPixelMask(image,q) != 0)
+        {
+          q+=GetPixelChannels(image);
+          continue;
+        }
+      for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
+      {
+        PixelChannel
+          channel;
+
+        PixelTrait
+          traits;
+
+        channel=GetPixelChannelMapChannel(image,i);
+        traits=GetPixelChannelMapTraits(image,channel);
+        if (((traits & UpdatePixelTrait) == 0) || (black[i] == white[i]))
+          continue;
+        q[i]=ClampToQuantum(equalize_map[GetPixelChannels(image)*
+          ScaleQuantumToMap(q[i])+i]);
+      }
       q+=GetPixelChannels(image);
     }
     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
@@ -1694,7 +1725,7 @@ MagickExport MagickBooleanType EqualizeImage(Image *image,
           proceed;
 
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp critical (MagickCore_EqualizeImage)
+        #pragma omp critical (MagickCore_EqualizeImage)
 #endif
         proceed=SetImageProgress(image,EqualizeImageTag,progress++,image->rows);
         if (proceed == MagickFalse)
@@ -1702,7 +1733,7 @@ MagickExport MagickBooleanType EqualizeImage(Image *image,
       }
   }
   image_view=DestroyCacheView(image_view);
-  equalize_map=(PixelInfo *) RelinquishMagickMemory(equalize_map);
+  equalize_map=(MagickRealType *) RelinquishMagickMemory(equalize_map);
   return(status);
 }
 \f
@@ -1779,33 +1810,33 @@ MagickExport MagickBooleanType GammaImage(Image *image,const double gamma,
   (void) ResetMagickMemory(gamma_map,0,(MaxMap+1)*sizeof(*gamma_map));
   if (gamma != 0.0)
 #if defined(MAGICKCORE_OPENMP_SUPPORT) && (MaxMap > 256)
-  #pragma omp parallel for
+    #pragma omp parallel for
 #endif
     for (i=0; i <= (ssize_t) MaxMap; i++)
-      gamma_map[i]=ClampToQuantum((MagickRealType) ScaleMapToQuantum((
-        MagickRealType) (MaxMap*pow((double) i/MaxMap,1.0/gamma))));
+      gamma_map[i]=ScaleMapToQuantum((MagickRealType) (MaxMap*pow((double) i/
+        MaxMap,1.0/gamma)));
   if (image->storage_class == PseudoClass)
     {
       /*
         Gamma-correct colormap.
       */
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
+      #pragma omp parallel for schedule(static) shared(progress,status)
 #endif
       for (i=0; i < (ssize_t) image->colors; i++)
       {
         if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
-          image->colormap[i].red=gamma_map[
-            ScaleQuantumToMap(image->colormap[i].red)];
+          image->colormap[i].red=(MagickRealType) gamma_map[
+            ScaleQuantumToMap(ClampToQuantum(image->colormap[i].red))];
         if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
-          image->colormap[i].green=gamma_map[
-            ScaleQuantumToMap(image->colormap[i].green)];
+          image->colormap[i].green=(MagickRealType) gamma_map[
+            ScaleQuantumToMap(ClampToQuantum(image->colormap[i].green))];
         if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
-          image->colormap[i].blue=gamma_map[
-            ScaleQuantumToMap(image->colormap[i].blue)];
+          image->colormap[i].blue=(MagickRealType) gamma_map[
+            ScaleQuantumToMap(ClampToQuantum(image->colormap[i].blue))];
         if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
-          image->colormap[i].alpha=gamma_map[
-            ScaleQuantumToMap(image->colormap[i].alpha)];
+          image->colormap[i].alpha=(MagickRealType) gamma_map[
+            ScaleQuantumToMap(ClampToQuantum(image->colormap[i].alpha))];
       }
     }
   /*
@@ -1813,9 +1844,9 @@ MagickExport MagickBooleanType GammaImage(Image *image,const double gamma,
   */
   status=MagickTrue;
   progress=0;
-  image_view=AcquireCacheView(image);
+  image_view=AcquireAuthenticCacheView(image,exception);
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
+  #pragma omp parallel for schedule(static,4) shared(progress,status)
 #endif
   for (y=0; y < (ssize_t) image->rows; y++)
   {
@@ -1828,7 +1859,7 @@ MagickExport MagickBooleanType GammaImage(Image *image,const double gamma,
     if (status == MagickFalse)
       continue;
     q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
-    if (q == (const Quantum *) NULL)
+    if (q == (Quantum *) NULL)
       {
         status=MagickFalse;
         continue;
@@ -1838,14 +1869,24 @@ MagickExport MagickBooleanType GammaImage(Image *image,const double gamma,
       register ssize_t
         i;
 
+      if (GetPixelMask(image,q) != 0)
+        {
+          q+=GetPixelChannels(image);
+          continue;
+        }
       for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
       {
+        PixelChannel
+          channel;
+
         PixelTrait
           traits;
 
-        traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
-        if ((traits & UpdatePixelTrait) != 0)
-          q[i]=gamma_map[ScaleQuantumToMap(q[i])];
+        channel=GetPixelChannelMapChannel(image,i);
+        traits=GetPixelChannelMapTraits(image,channel);
+        if ((traits & UpdatePixelTrait) == 0)
+          continue;
+        q[i]=gamma_map[ScaleQuantumToMap(q[i])];
       }
       q+=GetPixelChannels(image);
     }
@@ -1857,7 +1898,7 @@ MagickExport MagickBooleanType GammaImage(Image *image,const double gamma,
           proceed;
 
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp critical (MagickCore_GammaImage)
+        #pragma omp critical (MagickCore_GammaImage)
 #endif
         proceed=SetImageProgress(image,GammaCorrectImageTag,progress++,
           image->rows);
@@ -1891,7 +1932,8 @@ MagickExport MagickBooleanType GammaImage(Image *image,const double gamma,
 %
 %  The format of the HaldClutImage method is:
 %
-%      MagickBooleanType HaldClutImage(Image *image,Image *hald_image)
+%      MagickBooleanType HaldClutImage(Image *image,Image *hald_image,
+%        ExceptionInfo *exception)
 %
 %  A description of each parameter follows:
 %
@@ -1899,6 +1941,8 @@ MagickExport MagickBooleanType GammaImage(Image *image,const double gamma,
 %
 %    o hald_image: the color lookup table image for replacement color values.
 %
+%    o exception: return any errors or warnings in this structure.
+%
 */
 
 static inline size_t MagickMin(const size_t x,const size_t y)
@@ -1909,7 +1953,7 @@ static inline size_t MagickMin(const size_t x,const size_t y)
 }
 
 MagickExport MagickBooleanType HaldClutImage(Image *image,
-  const Image *hald_image)
+  const Image *hald_image,ExceptionInfo *exception)
 {
 #define HaldClutImageTag  "Clut/Image"
 
@@ -1928,9 +1972,6 @@ MagickExport MagickBooleanType HaldClutImage(Image *image,
   double
     width;
 
-  ExceptionInfo
-    *exception;
-
   MagickBooleanType
     status;
 
@@ -1954,9 +1995,10 @@ MagickExport MagickBooleanType HaldClutImage(Image *image,
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
   assert(hald_image != (Image *) NULL);
   assert(hald_image->signature == MagickSignature);
-  exception=(&image->exception);
   if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
     return(MagickFalse);
+  if (IsGrayColorspace(image->colorspace) != MagickFalse)
+    (void) TransformImageColorspace(image,sRGBColorspace,exception);
   if (image->matte == MagickFalse)
     (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
   /*
@@ -1970,26 +2012,13 @@ MagickExport MagickBooleanType HaldClutImage(Image *image,
   cube_size=level*level;
   width=(double) hald_image->columns;
   GetPixelInfo(hald_image,&zero);
-  image_view=AcquireCacheView(image);
-  hald_view=AcquireCacheView(hald_image);
+  hald_view=AcquireVirtualCacheView(hald_image,exception);
+  image_view=AcquireAuthenticCacheView(image,exception);
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
+  #pragma omp parallel for schedule(static,4) shared(progress,status)
 #endif
   for (y=0; y < (ssize_t) image->rows; y++)
   {
-    double
-      offset;
-
-    HaldInfo
-      point;
-
-    PixelInfo
-      pixel,
-      pixel1,
-      pixel2,
-      pixel3,
-      pixel4;
-
     register Quantum
       *restrict q;
 
@@ -1999,18 +2028,26 @@ MagickExport MagickBooleanType HaldClutImage(Image *image,
     if (status == MagickFalse)
       continue;
     q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
-    if (q == (const Quantum *) NULL)
+    if (q == (Quantum *) NULL)
       {
         status=MagickFalse;
         continue;
       }
-    pixel=zero;
-    pixel1=zero;
-    pixel2=zero;
-    pixel3=zero;
-    pixel4=zero;
     for (x=0; x < (ssize_t) image->columns; x++)
     {
+      double
+        offset;
+
+      HaldInfo
+        point;
+
+      PixelInfo
+        pixel,
+        pixel1,
+        pixel2,
+        pixel3,
+        pixel4;
+
       point.x=QuantumScale*(level-1.0)*GetPixelRed(image,q);
       point.y=QuantumScale*(level-1.0)*GetPixelGreen(image,q);
       point.z=QuantumScale*(level-1.0)*GetPixelBlue(image,q);
@@ -2018,41 +2055,38 @@ MagickExport MagickBooleanType HaldClutImage(Image *image,
       point.x-=floor(point.x);
       point.y-=floor(point.y);
       point.z-=floor(point.z);
-      (void) InterpolatePixelInfo(image,hald_view,
-        UndefinedInterpolatePixel,fmod(offset,width),floor(offset/width),
-        &pixel1,exception);
-      (void) InterpolatePixelInfo(image,hald_view,
-        UndefinedInterpolatePixel,fmod(offset+level,width),floor((offset+level)/
-        width),&pixel2,exception);
-      CompositePixelInfoAreaBlend(&pixel1,pixel1.alpha,&pixel2,
-        pixel2.alpha,point.y,&pixel3);
+      pixel1=zero;
+      (void) InterpolatePixelInfo(image,hald_view,image->interpolate,
+        fmod(offset,width),floor(offset/width),&pixel1,exception);
+      pixel2=zero;
+      (void) InterpolatePixelInfo(image,hald_view,image->interpolate,
+        fmod(offset+level,width),floor((offset+level)/width),&pixel2,exception);
+      pixel3=zero;
+      CompositePixelInfoAreaBlend(&pixel1,pixel1.alpha,&pixel2,pixel2.alpha,
+        point.y,&pixel3);
       offset+=cube_size;
-      (void) InterpolatePixelInfo(image,hald_view,
-        UndefinedInterpolatePixel,fmod(offset,width),floor(offset/width),
-        &pixel1,exception);
-      (void) InterpolatePixelInfo(image,hald_view,
-        UndefinedInterpolatePixel,fmod(offset+level,width),floor((offset+level)/
-        width),&pixel2,exception);
-      CompositePixelInfoAreaBlend(&pixel1,pixel1.alpha,&pixel2,
-        pixel2.alpha,point.y,&pixel4);
-      CompositePixelInfoAreaBlend(&pixel3,pixel3.alpha,&pixel4,
-        pixel4.alpha,point.z,&pixel);
+      (void) InterpolatePixelInfo(image,hald_view,image->interpolate,
+        fmod(offset,width),floor(offset/width),&pixel1,exception);
+      (void) InterpolatePixelInfo(image,hald_view,image->interpolate,
+        fmod(offset+level,width),floor((offset+level)/width),&pixel2,exception);
+      pixel4=zero;
+      CompositePixelInfoAreaBlend(&pixel1,pixel1.alpha,&pixel2,pixel2.alpha,
+        point.y,&pixel4);
+      pixel=zero;
+      CompositePixelInfoAreaBlend(&pixel3,pixel3.alpha,&pixel4,pixel4.alpha,
+        point.z,&pixel);
       if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
-        SetPixelRed(image,
-          ClampToQuantum(pixel.red),q);
+        SetPixelRed(image,ClampToQuantum(pixel.red),q);
       if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
-        SetPixelGreen(image,
-          ClampToQuantum(pixel.green),q);
+        SetPixelGreen(image,ClampToQuantum(pixel.green),q);
       if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
-        SetPixelBlue(image,
-          ClampToQuantum(pixel.blue),q);
+        SetPixelBlue(image,ClampToQuantum(pixel.blue),q);
       if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
           (image->colorspace == CMYKColorspace))
-        SetPixelBlack(image,
-          ClampToQuantum(pixel.black),q);
-      if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) && (image->matte != MagickFalse))
-        SetPixelAlpha(image,
-          ClampToQuantum(pixel.alpha),q);
+        SetPixelBlack(image,ClampToQuantum(pixel.black),q);
+      if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
+          (image->matte != MagickFalse))
+        SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
       q+=GetPixelChannels(image);
     }
     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
@@ -2063,7 +2097,7 @@ MagickExport MagickBooleanType HaldClutImage(Image *image,
           proceed;
 
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp critical (MagickCore_HaldClutImage)
+        #pragma omp critical (MagickCore_HaldClutImage)
 #endif
         proceed=SetImageProgress(image,HaldClutImageTag,progress++,image->rows);
         if (proceed == MagickFalse)
@@ -2118,13 +2152,28 @@ MagickExport MagickBooleanType HaldClutImage(Image *image,
 %    o exception: return any errors or warnings in this structure.
 %
 */
-MagickExport MagickBooleanType LevelImage(Image *image,
-  const double black_point,const double white_point,const double gamma,
-  ExceptionInfo *exception)
+
+static inline MagickRealType LevelPixel(const double black_point,
+  const double white_point,const double gamma,const MagickRealType pixel)
+{
+  double
+    level_pixel,
+    scale;
+
+  if (pixel < black_point)
+    return(0.0);
+  if (pixel > white_point)
+    return((MagickRealType) QuantumRange);
+  scale=(white_point != black_point) ? 1.0/(white_point-black_point) : 1.0;
+  level_pixel=(MagickRealType) QuantumRange*pow(scale*((double) pixel-
+    black_point),1.0/gamma);
+  return(level_pixel);
+}
+
+MagickExport MagickBooleanType LevelImage(Image *image,const double black_point,
+  const double white_point,const double gamma,ExceptionInfo *exception)
 {
 #define LevelImageTag  "Level/Image"
-#define LevelQuantum(x) (ClampToQuantum((MagickRealType) QuantumRange* \
-  pow(scale*((double) (x)-black_point),1.0/gamma)))
 
   CacheView
     *image_view;
@@ -2135,9 +2184,6 @@ MagickExport MagickBooleanType LevelImage(Image *image,
   MagickOffsetType
     progress;
 
-  register double
-    scale;
-
   register ssize_t
     i;
 
@@ -2151,10 +2197,9 @@ MagickExport MagickBooleanType LevelImage(Image *image,
   assert(image->signature == MagickSignature);
   if (image->debug != MagickFalse)
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
-  scale=(white_point != black_point) ? 1.0/(white_point-black_point) : 1.0;
   if (image->storage_class == PseudoClass)
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
+    #pragma omp parallel for schedule(static,4) shared(progress,status)
 #endif
     for (i=0; i < (ssize_t) image->colors; i++)
     {
@@ -2162,22 +2207,26 @@ MagickExport MagickBooleanType LevelImage(Image *image,
         Level colormap.
       */
       if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
-        image->colormap[i].red=LevelQuantum(image->colormap[i].red);
+        image->colormap[i].red=(double) ClampToQuantum(LevelPixel(black_point,
+          white_point,gamma,image->colormap[i].red));
       if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
-        image->colormap[i].green=LevelQuantum(image->colormap[i].green);
+        image->colormap[i].green=(double) ClampToQuantum(LevelPixel(black_point,
+          white_point,gamma,image->colormap[i].green));
       if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
-        image->colormap[i].blue=LevelQuantum(image->colormap[i].blue);
+        image->colormap[i].blue=(double) ClampToQuantum(LevelPixel(black_point,
+          white_point,gamma,image->colormap[i].blue));
       if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
-        image->colormap[i].alpha=LevelQuantum(image->colormap[i].alpha);
+        image->colormap[i].alpha=(double) ClampToQuantum(LevelPixel(black_point,
+          white_point,gamma,image->colormap[i].alpha));
       }
   /*
     Level image.
   */
   status=MagickTrue;
   progress=0;
-  image_view=AcquireCacheView(image);
+  image_view=AcquireAuthenticCacheView(image,exception);
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
+  #pragma omp parallel for schedule(static,4) shared(progress,status)
 #endif
   for (y=0; y < (ssize_t) image->rows; y++)
   {
@@ -2190,25 +2239,36 @@ MagickExport MagickBooleanType LevelImage(Image *image,
     if (status == MagickFalse)
       continue;
     q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
-    if (q == (const Quantum *) NULL)
+    if (q == (Quantum *) NULL)
       {
         status=MagickFalse;
         continue;
       }
     for (x=0; x < (ssize_t) image->columns; x++)
     {
-      if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
-        SetPixelRed(image,LevelQuantum(GetPixelRed(image,q)),q);
-      if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
-        SetPixelGreen(image,LevelQuantum(GetPixelGreen(image,q)),q);
-      if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
-        SetPixelBlue(image,LevelQuantum(GetPixelBlue(image,q)),q);
-      if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
-          (image->matte == MagickTrue))
-        SetPixelAlpha(image,LevelQuantum(GetPixelAlpha(image,q)),q);
-      if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
-          (image->colorspace == CMYKColorspace))
-        SetPixelBlack(image,LevelQuantum(GetPixelBlack(image,q)),q);
+      register ssize_t
+        i;
+
+      if (GetPixelMask(image,q) != 0)
+        {
+          q+=GetPixelChannels(image);
+          continue;
+        }
+      for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
+      {
+        PixelChannel
+          channel;
+
+        PixelTrait
+          traits;
+
+        channel=GetPixelChannelMapChannel(image,i);
+        traits=GetPixelChannelMapTraits(image,channel);
+        if ((traits & UpdatePixelTrait) == 0)
+          continue;
+        q[i]=ClampToQuantum(LevelPixel(black_point,white_point,gamma,
+          (MagickRealType) q[i]));
+      }
       q+=GetPixelChannels(image);
     }
     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
@@ -2219,7 +2279,7 @@ MagickExport MagickBooleanType LevelImage(Image *image,
           proceed;
 
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp critical (MagickCore_LevelImage)
+        #pragma omp critical (MagickCore_LevelImage)
 #endif
         proceed=SetImageProgress(image,LevelImageTag,progress++,image->rows);
         if (proceed == MagickFalse)
@@ -2235,7 +2295,7 @@ MagickExport MagickBooleanType LevelImage(Image *image,
 %                                                                             %
 %                                                                             %
 %                                                                             %
-%     L e v e l i z e I m a g e C h a n n e l                                 %
+%     L e v e l i z e I m a g e                                               %
 %                                                                             %
 %                                                                             %
 %                                                                             %
@@ -2249,15 +2309,15 @@ MagickExport MagickBooleanType LevelImage(Image *image,
 %  LevelizeImage() can be called with by using a +level command line
 %  API option, or using a '!' on a -level or LevelImage() geometry string.
 %
-%  It can be used for example de-contrast a greyscale image to the exact
-%  levels specified.  Or by using specific levels for each channel of an image
-%  you can convert a gray-scale image to any linear color gradient, according
-%  to those levels.
+%  It can be used to de-contrast a greyscale image to the exact levels
+%  specified.  Or by using specific levels for each channel of an image you
+%  can convert a gray-scale image to any linear color gradient, according to
+%  those levels.
 %
 %  The format of the LevelizeImage method is:
 %
 %      MagickBooleanType LevelizeImage(Image *image,const double black_point,
-%        const double white_point,const double gamma)
+%        const double white_point,const double gamma,ExceptionInfo *exception)
 %
 %  A description of each parameter follows:
 %
@@ -2269,9 +2329,12 @@ MagickExport MagickBooleanType LevelImage(Image *image,
 %
 %    o gamma: adjust gamma by this factor before mapping values.
 %
+%    o exception: return any errors or warnings in this structure.
+%
 */
 MagickExport MagickBooleanType LevelizeImage(Image *image,
-  const double black_point,const double white_point,const double gamma)
+  const double black_point,const double white_point,const double gamma,
+  ExceptionInfo *exception)
 {
 #define LevelizeImageTag  "Levelize/Image"
 #define LevelizeValue(x) (ClampToQuantum(((MagickRealType) \
@@ -2281,9 +2344,6 @@ MagickExport MagickBooleanType LevelizeImage(Image *image,
   CacheView
     *image_view;
 
-  ExceptionInfo
-    *exception;
-
   MagickBooleanType
     status;
 
@@ -2305,7 +2365,7 @@ MagickExport MagickBooleanType LevelizeImage(Image *image,
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
   if (image->storage_class == PseudoClass)
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
+    #pragma omp parallel for schedule(static,4) shared(progress,status)
 #endif
     for (i=0; i < (ssize_t) image->colors; i++)
     {
@@ -2313,23 +2373,26 @@ MagickExport MagickBooleanType LevelizeImage(Image *image,
         Level colormap.
       */
       if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
-        image->colormap[i].red=LevelizeValue(image->colormap[i].red);
+        image->colormap[i].red=(double) LevelizeValue(
+          image->colormap[i].red);
       if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
-        image->colormap[i].green=LevelizeValue(image->colormap[i].green);
+        image->colormap[i].green=(double) LevelizeValue(
+          image->colormap[i].green);
       if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
-        image->colormap[i].blue=LevelizeValue(image->colormap[i].blue);
+        image->colormap[i].blue=(double) LevelizeValue(
+          image->colormap[i].blue);
       if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
-        image->colormap[i].alpha=LevelizeValue(image->colormap[i].alpha);
+        image->colormap[i].alpha=(double) LevelizeValue(
+          image->colormap[i].alpha);
     }
   /*
     Level image.
   */
   status=MagickTrue;
   progress=0;
-  exception=(&image->exception);
-  image_view=AcquireCacheView(image);
+  image_view=AcquireAuthenticCacheView(image,exception);
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
+  #pragma omp parallel for schedule(static,4) shared(progress,status)
 #endif
   for (y=0; y < (ssize_t) image->rows; y++)
   {
@@ -2342,25 +2405,35 @@ MagickExport MagickBooleanType LevelizeImage(Image *image,
     if (status == MagickFalse)
       continue;
     q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
-    if (q == (const Quantum *) NULL)
+    if (q == (Quantum *) NULL)
       {
         status=MagickFalse;
         continue;
       }
     for (x=0; x < (ssize_t) image->columns; x++)
     {
-      if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
-        SetPixelRed(image,LevelizeValue(GetPixelRed(image,q)),q);
-      if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
-        SetPixelGreen(image,LevelizeValue(GetPixelGreen(image,q)),q);
-      if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
-        SetPixelBlue(image,LevelizeValue(GetPixelBlue(image,q)),q);
-      if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
-          (image->colorspace == CMYKColorspace))
-        SetPixelBlack(image,LevelizeValue(GetPixelBlack(image,q)),q);
-      if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
-          (image->matte == MagickTrue))
-        SetPixelAlpha(image,LevelizeValue(GetPixelAlpha(image,q)),q);
+      register ssize_t
+        i;
+
+      if (GetPixelMask(image,q) != 0)
+        {
+          q+=GetPixelChannels(image);
+          continue;
+        }
+      for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
+      {
+        PixelChannel
+          channel;
+
+        PixelTrait
+          traits;
+
+        channel=GetPixelChannelMapChannel(image,i);
+        traits=GetPixelChannelMapTraits(image,channel);
+        if ((traits & UpdatePixelTrait) == 0)
+          continue;
+        q[i]=LevelizeValue(q[i]);
+      }
       q+=GetPixelChannels(image);
     }
     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
@@ -2371,7 +2444,7 @@ MagickExport MagickBooleanType LevelizeImage(Image *image,
           proceed;
 
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp critical (MagickCore_LevelizeImage)
+        #pragma omp critical (MagickCore_LevelizeImage)
 #endif
         proceed=SetImageProgress(image,LevelizeImageTag,progress++,image->rows);
         if (proceed == MagickFalse)
@@ -2408,7 +2481,7 @@ MagickExport MagickBooleanType LevelizeImage(Image *image,
 %
 %    MagickBooleanType LevelImageColors(Image *image,
 %      const PixelInfo *black_color,const PixelInfo *white_color,
-%      const MagickBooleanType invert)
+%      const MagickBooleanType invert,ExceptionInfo *exception)
 %
 %  A description of each parameter follows:
 %
@@ -2420,10 +2493,12 @@ MagickExport MagickBooleanType LevelizeImage(Image *image,
 %
 %    o invert: if true map the colors (levelize), rather than from (level)
 %
+%    o exception: return any errors or warnings in this structure.
+%
 */
 MagickExport MagickBooleanType LevelImageColors(Image *image,
   const PixelInfo *black_color,const PixelInfo *white_color,
-  const MagickBooleanType invert)
+  const MagickBooleanType invert,ExceptionInfo *exception)
 {
   ChannelType
     channel_mask;
@@ -2445,21 +2520,21 @@ MagickExport MagickBooleanType LevelImageColors(Image *image,
         {
           channel_mask=SetPixelChannelMask(image,RedChannel);
           status|=LevelImage(image,black_color->red,white_color->red,1.0,
-            &image->exception);
+            exception);
           (void) SetPixelChannelMask(image,channel_mask);
         }
       if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
         {
           channel_mask=SetPixelChannelMask(image,GreenChannel);
           status|=LevelImage(image,black_color->green,white_color->green,1.0,
-            &image->exception);
+            exception);
           (void) SetPixelChannelMask(image,channel_mask);
         }
       if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
         {
           channel_mask=SetPixelChannelMask(image,BlueChannel);
           status|=LevelImage(image,black_color->blue,white_color->blue,1.0,
-            &image->exception);
+            exception);
           (void) SetPixelChannelMask(image,channel_mask);
         }
       if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
@@ -2467,7 +2542,7 @@ MagickExport MagickBooleanType LevelImageColors(Image *image,
         {
           channel_mask=SetPixelChannelMask(image,BlackChannel);
           status|=LevelImage(image,black_color->black,white_color->black,1.0,
-            &image->exception);
+            exception);
           (void) SetPixelChannelMask(image,channel_mask);
         }
       if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
@@ -2475,7 +2550,7 @@ MagickExport MagickBooleanType LevelImageColors(Image *image,
         {
           channel_mask=SetPixelChannelMask(image,AlphaChannel);
           status|=LevelImage(image,black_color->alpha,white_color->alpha,1.0,
-            &image->exception);
+            exception);
           (void) SetPixelChannelMask(image,channel_mask);
         }
     }
@@ -2484,36 +2559,38 @@ MagickExport MagickBooleanType LevelImageColors(Image *image,
       if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
         {
           channel_mask=SetPixelChannelMask(image,RedChannel);
-          status|=LevelizeImage(image,black_color->red,white_color->red,1.0);
+          status|=LevelizeImage(image,black_color->red,white_color->red,1.0,
+            exception);
           (void) SetPixelChannelMask(image,channel_mask);
         }
       if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
         {
           channel_mask=SetPixelChannelMask(image,GreenChannel);
-          status|=LevelizeImage(image,black_color->green,white_color->green,
-            1.0);
+          status|=LevelizeImage(image,black_color->green,white_color->green,1.0,
+            exception);
           (void) SetPixelChannelMask(image,channel_mask);
         }
       if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
         {
           channel_mask=SetPixelChannelMask(image,BlueChannel);
-          status|=LevelizeImage(image,black_color->blue,white_color->blue,1.0);
+          status|=LevelizeImage(image,black_color->blue,white_color->blue,1.0,
+            exception);
           (void) SetPixelChannelMask(image,channel_mask);
         }
       if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
           (image->colorspace == CMYKColorspace))
         {
           channel_mask=SetPixelChannelMask(image,BlackChannel);
-          status|=LevelizeImage(image,black_color->black,white_color->black,
-            1.0);
+          status|=LevelizeImage(image,black_color->black,white_color->black,1.0,
+            exception);
           (void) SetPixelChannelMask(image,channel_mask);
         }
       if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
           (image->matte == MagickTrue))
         {
           channel_mask=SetPixelChannelMask(image,AlphaChannel);
-          status|=LevelizeImage(image,black_color->alpha,white_color->alpha,
-            1.0);
+          status|=LevelizeImage(image,black_color->alpha,white_color->alpha,1.0,
+            exception);
           (void) SetPixelChannelMask(image,channel_mask);
         }
     }
@@ -2537,7 +2614,8 @@ MagickExport MagickBooleanType LevelImageColors(Image *image,
 %  The format of the LinearStretchImage method is:
 %
 %      MagickBooleanType LinearStretchImage(Image *image,
-%        const double black_point,const double white_point)
+%        const double black_point,const double white_point,
+%        ExceptionInfo *exception)
 %
 %  A description of each parameter follows:
 %
@@ -2547,14 +2625,16 @@ MagickExport MagickBooleanType LevelImageColors(Image *image,
 %
 %    o white_point: the white point.
 %
+%    o exception: return any errors or warnings in this structure.
+%
 */
 MagickExport MagickBooleanType LinearStretchImage(Image *image,
-  const double black_point,const double white_point)
+  const double black_point,const double white_point,ExceptionInfo *exception)
 {
 #define LinearStretchImageTag  "LinearStretch/Image"
 
-  ExceptionInfo
-    *exception;
+  CacheView
+    *image_view;
 
   MagickBooleanType
     status;
@@ -2582,7 +2662,7 @@ MagickExport MagickBooleanType LinearStretchImage(Image *image,
     Form histogram.
   */
   (void) ResetMagickMemory(histogram,0,(MaxMap+1)*sizeof(*histogram));
-  exception=(&image->exception);
+  image_view=AcquireVirtualCacheView(image,exception);
   for (y=0; y < (ssize_t) image->rows; y++)
   {
     register const Quantum
@@ -2591,15 +2671,16 @@ MagickExport MagickBooleanType LinearStretchImage(Image *image,
     register ssize_t
       x;
 
-    p=GetVirtualPixels(image,0,y,image->columns,1,exception);
+    p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
     if (p == (const Quantum *) NULL)
       break;
-    for (x=(ssize_t) image->columns-1; x >= 0; x--)
+    for (x=0; x < (ssize_t) image->columns; x++)
     {
       histogram[ScaleQuantumToMap(GetPixelIntensity(image,p))]++;
       p+=GetPixelChannels(image);
     }
   }
+  image_view=DestroyCacheView(image_view);
   /*
     Find the histogram boundaries by locating the black and white point levels.
   */
@@ -2618,7 +2699,7 @@ MagickExport MagickBooleanType LinearStretchImage(Image *image,
       break;
   }
   histogram=(MagickRealType *) RelinquishMagickMemory(histogram);
-  status=LevelImage(image,(double) black,(double) white,1.0,&image->exception);
+  status=LevelImage(image,(double) black,(double) white,1.0,exception);
   return(status);
 }
 \f
@@ -2641,20 +2722,22 @@ MagickExport MagickBooleanType LinearStretchImage(Image *image,
 %
 %  The format of the ModulateImage method is:
 %
-%      MagickBooleanType ModulateImage(Image *image,const char *modulate)
+%      MagickBooleanType ModulateImage(Image *image,const char *modulate,
+%        ExceptionInfo *exception)
 %
 %  A description of each parameter follows:
 %
 %    o image: the image.
 %
-%    o modulate: Define the percent change in brightness, saturation, and
-%      hue.
+%    o modulate: Define the percent change in brightness, saturation, and hue.
+%
+%    o exception: return any errors or warnings in this structure.
 %
 */
 
 static void ModulateHSB(const double percent_hue,
-  const double percent_saturation,const double percent_brightness,
-  Quantum *red,Quantum *green,Quantum *blue)
+  const double percent_saturation,const double percent_brightness,double *red,
+  double *green,double *blue)
 {
   double
     brightness,
@@ -2664,9 +2747,9 @@ static void ModulateHSB(const double percent_hue,
   /*
     Increase or decrease color brightness, saturation, or hue.
   */
-  assert(red != (Quantum *) NULL);
-  assert(green != (Quantum *) NULL);
-  assert(blue != (Quantum *) NULL);
+  assert(red != (double *) NULL);
+  assert(green != (double *) NULL);
+  assert(blue != (double *) NULL);
   ConvertRGBToHSB(*red,*green,*blue,&hue,&saturation,&brightness);
   hue+=0.5*(0.01*percent_hue-1.0);
   while (hue < 0.0)
@@ -2679,8 +2762,8 @@ static void ModulateHSB(const double percent_hue,
 }
 
 static void ModulateHSL(const double percent_hue,
-  const double percent_saturation,const double percent_lightness,
-  Quantum *red,Quantum *green,Quantum *blue)
+  const double percent_saturation,const double percent_lightness,double *red,
+  double *green,double *blue)
 {
   double
     hue,
@@ -2690,9 +2773,9 @@ static void ModulateHSL(const double percent_hue,
   /*
     Increase or decrease color lightness, saturation, or hue.
   */
-  assert(red != (Quantum *) NULL);
-  assert(green != (Quantum *) NULL);
-  assert(blue != (Quantum *) NULL);
+  assert(red != (double *) NULL);
+  assert(green != (double *) NULL);
+  assert(blue != (double *) NULL);
   ConvertRGBToHSL(*red,*green,*blue,&hue,&saturation,&lightness);
   hue+=0.5*(0.01*percent_hue-1.0);
   while (hue < 0.0)
@@ -2704,7 +2787,7 @@ static void ModulateHSL(const double percent_hue,
   ConvertHSLToRGB(hue,saturation,lightness,red,green,blue);
 }
 
-static void ModulateHWB(const double percent_hue,const double percent_whiteness,  const double percent_blackness,Quantum *red,Quantum *green,Quantum *blue)
+static void ModulateHWB(const double percent_hue,const double percent_whiteness,  const double percent_blackness,double *red,double *green,double *blue)
 {
   double
     blackness,
@@ -2714,9 +2797,9 @@ static void ModulateHWB(const double percent_hue,const double percent_whiteness,
   /*
     Increase or decrease color blackness, whiteness, or hue.
   */
-  assert(red != (Quantum *) NULL);
-  assert(green != (Quantum *) NULL);
-  assert(blue != (Quantum *) NULL);
+  assert(red != (double *) NULL);
+  assert(green != (double *) NULL);
+  assert(blue != (double *) NULL);
   ConvertRGBToHWB(*red,*green,*blue,&hue,&whiteness,&blackness);
   hue+=0.5*(0.01*percent_hue-1.0);
   while (hue < 0.0)
@@ -2728,7 +2811,8 @@ static void ModulateHWB(const double percent_hue,const double percent_whiteness,
   ConvertHWBToRGB(hue,whiteness,blackness,red,green,blue);
 }
 
-MagickExport MagickBooleanType ModulateImage(Image *image,const char *modulate)
+MagickExport MagickBooleanType ModulateImage(Image *image,const char *modulate,
+  ExceptionInfo *exception)
 {
 #define ModulateImageTag  "Modulate/Image"
 
@@ -2746,9 +2830,6 @@ MagickExport MagickBooleanType ModulateImage(Image *image,const char *modulate)
     percent_hue,
     percent_saturation;
 
-  ExceptionInfo
-    *exception;
-
   GeometryInfo
     geometry_info;
 
@@ -2795,7 +2876,7 @@ MagickExport MagickBooleanType ModulateImage(Image *image,const char *modulate)
         Modulate colormap.
       */
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
+      #pragma omp parallel for schedule(static,4) shared(progress,status)
 #endif
       for (i=0; i < (ssize_t) image->colors; i++)
         switch (colorspace)
@@ -2829,14 +2910,13 @@ MagickExport MagickBooleanType ModulateImage(Image *image,const char *modulate)
   */
   status=MagickTrue;
   progress=0;
-  exception=(&image->exception);
-  image_view=AcquireCacheView(image);
+  image_view=AcquireAuthenticCacheView(image,exception);
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
+  #pragma omp parallel for schedule(static,4) shared(progress,status)
 #endif
   for (y=0; y < (ssize_t) image->rows; y++)
   {
-    Quantum
+    double
       blue,
       green,
       red;
@@ -2850,16 +2930,16 @@ MagickExport MagickBooleanType ModulateImage(Image *image,const char *modulate)
     if (status == MagickFalse)
       continue;
     q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
-    if (q == (const Quantum *) NULL)
+    if (q == (Quantum *) NULL)
       {
         status=MagickFalse;
         continue;
       }
     for (x=0; x < (ssize_t) image->columns; x++)
     {
-      red=GetPixelRed(image,q);
-      green=GetPixelGreen(image,q);
-      blue=GetPixelBlue(image,q);
+      red=(double) GetPixelRed(image,q);
+      green=(double) GetPixelGreen(image,q);
+      blue=(double) GetPixelBlue(image,q);
       switch (colorspace)
       {
         case HSBColorspace:
@@ -2882,9 +2962,9 @@ MagickExport MagickBooleanType ModulateImage(Image *image,const char *modulate)
           break;
         }
       }
-      SetPixelRed(image,red,q);
-      SetPixelGreen(image,green,q);
-      SetPixelBlue(image,blue,q);
+      SetPixelRed(image,ClampToQuantum(red),q);
+      SetPixelGreen(image,ClampToQuantum(green),q);
+      SetPixelBlue(image,ClampToQuantum(blue),q);
       q+=GetPixelChannels(image);
     }
     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
@@ -2895,7 +2975,7 @@ MagickExport MagickBooleanType ModulateImage(Image *image,const char *modulate)
           proceed;
 
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp critical (MagickCore_ModulateImage)
+        #pragma omp critical (MagickCore_ModulateImage)
 #endif
         proceed=SetImageProgress(image,ModulateImageTag,progress++,image->rows);
         if (proceed == MagickFalse)
@@ -2964,7 +3044,7 @@ MagickExport MagickBooleanType NegateImage(Image *image,
         Negate colormap.
       */
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
+      #pragma omp parallel for schedule(static) shared(progress,status)
 #endif
       for (i=0; i < (ssize_t) image->colors; i++)
       {
@@ -2988,11 +3068,11 @@ MagickExport MagickBooleanType NegateImage(Image *image,
   */
   status=MagickTrue;
   progress=0;
-  image_view=AcquireCacheView(image);
+  image_view=AcquireAuthenticCacheView(image,exception);
   if (grayscale != MagickFalse)
     {
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
+      #pragma omp parallel for schedule(static) shared(progress,status)
 #endif
       for (y=0; y < (ssize_t) image->rows; y++)
       {
@@ -3009,7 +3089,7 @@ MagickExport MagickBooleanType NegateImage(Image *image,
           continue;
         q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
           exception);
-        if (q == (const Quantum *) NULL)
+        if (q == (Quantum *) NULL)
           {
             status=MagickFalse;
             continue;
@@ -3019,19 +3099,25 @@ MagickExport MagickBooleanType NegateImage(Image *image,
           register ssize_t
             i;
 
-          if (IsPixelGray(image,q) != MagickFalse)
+          if ((GetPixelMask(image,q) != 0) ||
+              (IsPixelGray(image,q) != MagickFalse))
             {
               q+=GetPixelChannels(image);
               continue;
             }
           for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
           {
+            PixelChannel
+              channel;
+
             PixelTrait
               traits;
 
-            traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
-            if ((traits & UpdatePixelTrait) != 0)
-              q[i]=QuantumRange-q[i];
+            channel=GetPixelChannelMapChannel(image,i);
+            traits=GetPixelChannelMapTraits(image,channel);
+            if ((traits & UpdatePixelTrait) == 0)
+              continue;
+            q[i]=QuantumRange-q[i];
           }
           q+=GetPixelChannels(image);
         }
@@ -3044,7 +3130,7 @@ MagickExport MagickBooleanType NegateImage(Image *image,
               proceed;
 
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp critical (MagickCore_NegateImage)
+            #pragma omp critical (MagickCore_NegateImage)
 #endif
             proceed=SetImageProgress(image,NegateImageTag,progress++,
               image->rows);
@@ -3059,7 +3145,7 @@ MagickExport MagickBooleanType NegateImage(Image *image,
     Negate image.
   */
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
+  #pragma omp parallel for schedule(static) shared(progress,status)
 #endif
   for (y=0; y < (ssize_t) image->rows; y++)
   {
@@ -3072,7 +3158,7 @@ MagickExport MagickBooleanType NegateImage(Image *image,
     if (status == MagickFalse)
       continue;
     q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
-    if (q == (const Quantum *) NULL)
+    if (q == (Quantum *) NULL)
       {
         status=MagickFalse;
         continue;
@@ -3082,14 +3168,24 @@ MagickExport MagickBooleanType NegateImage(Image *image,
       register ssize_t
         i;
 
+      if (GetPixelMask(image,q) != 0)
+        {
+          q+=GetPixelChannels(image);
+          continue;
+        }
       for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
       {
+        PixelChannel
+          channel;
+
         PixelTrait
           traits;
 
-        traits=GetPixelChannelMapTraits(image,(PixelChannel) i);
-        if ((traits & UpdatePixelTrait) != 0)
-          q[i]=QuantumRange-q[i];
+        channel=GetPixelChannelMapChannel(image,i);
+        traits=GetPixelChannelMapTraits(image,channel);
+        if ((traits & UpdatePixelTrait) == 0)
+          continue;
+        q[i]=QuantumRange-q[i];
       }
       q+=GetPixelChannels(image);
     }
@@ -3101,7 +3197,7 @@ MagickExport MagickBooleanType NegateImage(Image *image,
           proceed;
 
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp critical (MagickCore_NegateImage)
+        #pragma omp critical (MagickCore_NegateImage)
 #endif
         proceed=SetImageProgress(image,NegateImageTag,progress++,image->rows);
         if (proceed == MagickFalse)
@@ -3123,8 +3219,9 @@ MagickExport MagickBooleanType NegateImage(Image *image,
 %                                                                             %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
-%  NormalizeImage() enhances the contrast of a color image by mapping the
-%  darkest 2 percent of all pixel to black and the brightest 1 percent to white.
+%  The NormalizeImage() method enhances the contrast of a color image by
+%  mapping the darkest 2 percent of all pixel to black and the brightest
+%  1 percent to white.
 %
 %  The format of the NormalizeImage method is:
 %
@@ -3172,7 +3269,8 @@ MagickExport MagickBooleanType NormalizeImage(Image *image,
 %  The format of the SigmoidalContrastImage method is:
 %
 %      MagickBooleanType SigmoidalContrastImage(Image *image,
-%        const MagickBooleanType sharpen,const char *levels)
+%        const MagickBooleanType sharpen,const char *levels,
+%        ExceptionInfo *exception)
 %
 %  A description of each parameter follows:
 %
@@ -3185,18 +3283,18 @@ MagickExport MagickBooleanType NormalizeImage(Image *image,
 %
 %    o beta: midpoint of the function as a color value 0 to QuantumRange.
 %
+%    o exception: return any errors or warnings in this structure.
+%
 */
 MagickExport MagickBooleanType SigmoidalContrastImage(Image *image,
-  const MagickBooleanType sharpen,const double contrast,const double midpoint)
+  const MagickBooleanType sharpen,const double contrast,const double midpoint,
+  ExceptionInfo *exception)
 {
 #define SigmoidalContrastImageTag  "SigmoidalContrast/Image"
 
   CacheView
     *image_view;
 
-  ExceptionInfo
-    *exception;
-
   MagickBooleanType
     status;
 
@@ -3226,20 +3324,52 @@ MagickExport MagickBooleanType SigmoidalContrastImage(Image *image,
       image->filename);
   (void) ResetMagickMemory(sigmoidal_map,0,(MaxMap+1)*sizeof(*sigmoidal_map));
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
+  #pragma omp parallel for schedule(static) shared(progress,status)
 #endif
   for (i=0; i <= (ssize_t) MaxMap; i++)
   {
     if (sharpen != MagickFalse)
       {
+#define sigmoidal(a,b,x)  (1/(1+exp((a)*((b)-(x)))))
+#if 1
+        /* Simpilified function scaling,
+         * with better 'contrast=0' or 'flatline' handling (greyscale)
+         */
+        double
+          u0 = sigmoidal(contrast,QuantumScale*midpoint,0.0),
+          u1 = sigmoidal(contrast,QuantumScale*midpoint,1.0);
+        sigmoidal_map[i]=(MagickRealType) ScaleMapToQuantum(
+           (MagickRealType)(MaxMap*(
+               (sigmoidal(contrast,QuantumScale*midpoint,(double)i/MaxMap)
+                  -(u0+u1)/2.0)/(u1-u0+MagickEpsilon)+0.5)   ));
+#else
+        /* Scaled sigmoidal formula...
+             (1/(1+exp(a*(b-u))) - 1/(1+exp(a))) /
+                     (1/(1+exp(a*(b-1)))/(1+exp(a)))) */
         sigmoidal_map[i]=(MagickRealType) ScaleMapToQuantum((MagickRealType)
           (MaxMap*((1.0/(1.0+exp(contrast*(midpoint/(double) QuantumRange-
           (double) i/MaxMap))))-(1.0/(1.0+exp(contrast*(midpoint/
           (double) QuantumRange)))))/((1.0/(1.0+exp(contrast*(midpoint/
           (double) QuantumRange-1.0))))-(1.0/(1.0+exp(contrast*(midpoint/
           (double) QuantumRange)))))+0.5));
+#endif
         continue;
       }
+#if 1
+    {
+      /* Inverse -- See
+         http://osdir.com/ml/video.image-magick.devel/2005-04/msg00006.html
+      */
+      double
+        min = sigmoidal(contrast,1.0,0.0),
+        max = sigmoidal(contrast,QuantumScale*midpoint,1.0),
+        xi  = min+(double)i/MaxMap*(max-min);
+      sigmoidal_map[i]=(MagickRealType) ScaleMapToQuantum(
+         (MagickRealType)(MaxMap*(
+             QuantumScale*midpoint-log((1-xi)/xi)/contrast) ));
+    }
+#else
+    /* expanded form of the above */
     sigmoidal_map[i]=(MagickRealType) ScaleMapToQuantum((MagickRealType)
       (MaxMap*(QuantumScale*midpoint-log((1.0-(1.0/(1.0+exp(midpoint/
       (double) QuantumRange*contrast))+((double) i/MaxMap)*((1.0/
@@ -3249,6 +3379,7 @@ MagickExport MagickBooleanType SigmoidalContrastImage(Image *image,
       ((double) i/MaxMap)*((1.0/(1.0+exp(contrast*(midpoint/
       (double) QuantumRange-1.0))))-(1.0/(1.0+exp(midpoint/
       (double) QuantumRange*contrast))))))/contrast)));
+#endif
   }
   if (image->storage_class == PseudoClass)
     {
@@ -3256,22 +3387,22 @@ MagickExport MagickBooleanType SigmoidalContrastImage(Image *image,
         Sigmoidal-contrast enhance colormap.
       */
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
+      #pragma omp parallel for schedule(static,4) shared(progress,status)
 #endif
       for (i=0; i < (ssize_t) image->colors; i++)
       {
         if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
-          image->colormap[i].red=ClampToQuantum(sigmoidal_map[
-            ScaleQuantumToMap(image->colormap[i].red)]);
+          image->colormap[i].red=sigmoidal_map[ScaleQuantumToMap(
+            ClampToQuantum(image->colormap[i].red))];
         if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
-          image->colormap[i].green=ClampToQuantum(sigmoidal_map[
-            ScaleQuantumToMap(image->colormap[i].green)]);
+          image->colormap[i].green=sigmoidal_map[ScaleQuantumToMap(
+            ClampToQuantum(image->colormap[i].green))];
         if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
-          image->colormap[i].blue=ClampToQuantum(sigmoidal_map[
-            ScaleQuantumToMap(image->colormap[i].blue)]);
+          image->colormap[i].blue=sigmoidal_map[ScaleQuantumToMap(
+            ClampToQuantum(image->colormap[i].blue))];
         if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
-          image->colormap[i].alpha=ClampToQuantum(sigmoidal_map[
-            ScaleQuantumToMap(image->colormap[i].alpha)]);
+          image->colormap[i].alpha=sigmoidal_map[ScaleQuantumToMap(
+            ClampToQuantum(image->colormap[i].alpha))];
       }
     }
   /*
@@ -3279,10 +3410,9 @@ MagickExport MagickBooleanType SigmoidalContrastImage(Image *image,
   */
   status=MagickTrue;
   progress=0;
-  exception=(&image->exception);
-  image_view=AcquireCacheView(image);
+  image_view=AcquireAuthenticCacheView(image,exception);
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
+  #pragma omp parallel for schedule(static,4) shared(progress,status)
 #endif
   for (y=0; y < (ssize_t) image->rows; y++)
   {
@@ -3295,29 +3425,35 @@ MagickExport MagickBooleanType SigmoidalContrastImage(Image *image,
     if (status == MagickFalse)
       continue;
     q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
-    if (q == (const Quantum *) NULL)
+    if (q == (Quantum *) NULL)
       {
         status=MagickFalse;
         continue;
       }
     for (x=0; x < (ssize_t) image->columns; x++)
     {
-      if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
-        SetPixelRed(image,ClampToQuantum(sigmoidal_map[ScaleQuantumToMap(
-          GetPixelRed(image,q))]),q);
-      if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
-        SetPixelGreen(image,ClampToQuantum(sigmoidal_map[ScaleQuantumToMap(
-          GetPixelGreen(image,q))]),q);
-      if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
-        SetPixelBlue(image,ClampToQuantum(sigmoidal_map[ScaleQuantumToMap(
-          GetPixelBlue(image,q))]),q);
-      if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
-          (image->colorspace == CMYKColorspace))
-        SetPixelBlack(image,ClampToQuantum(sigmoidal_map[ScaleQuantumToMap(
-          GetPixelBlack(image,q))]),q);
-      if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
-        SetPixelAlpha(image,ClampToQuantum(sigmoidal_map[ScaleQuantumToMap(
-          GetPixelAlpha(image,q))]),q);
+      register ssize_t
+        i;
+
+      if (GetPixelMask(image,q) != 0)
+        {
+          q+=GetPixelChannels(image);
+          continue;
+        }
+      for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
+      {
+        PixelChannel
+          channel;
+
+        PixelTrait
+          traits;
+
+        channel=GetPixelChannelMapChannel(image,i);
+        traits=GetPixelChannelMapTraits(image,channel);
+        if ((traits & UpdatePixelTrait) == 0)
+          continue;
+        q[i]=ClampToQuantum(sigmoidal_map[ScaleQuantumToMap(q[i])]);
+      }
       q+=GetPixelChannels(image);
     }
     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
@@ -3328,7 +3464,7 @@ MagickExport MagickBooleanType SigmoidalContrastImage(Image *image,
           proceed;
 
 #if defined(MAGICKCORE_OPENMP_SUPPORT)
-  #pragma omp critical (MagickCore_SigmoidalContrastImage)
+        #pragma omp critical (MagickCore_SigmoidalContrastImage)
 #endif
         proceed=SetImageProgress(image,SigmoidalContrastImageTag,progress++,
           image->rows);