]> granicus.if.org Git - imagemagick/blobdiff - magick/enhance.c
(no commit message)
[imagemagick] / magick / enhance.c
index c0767353ca0ede50e6b1d37d60de0ddbbb7baecd..b375a931e8b5532525a62c2ee415d13bbbff4896 100644 (file)
@@ -17,7 +17,7 @@
 %                                 July 1992                                   %
 %                                                                             %
 %                                                                             %
-%  Copyright 1999-2009 ImageMagick Studio LLC, a non-profit organization      %
+%  Copyright 1999-2011 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  %
@@ -51,6 +51,7 @@
 #include "magick/enhance.h"
 #include "magick/exception.h"
 #include "magick/exception-private.h"
+#include "magick/fx.h"
 #include "magick/gem.h"
 #include "magick/geometry.h"
 #include "magick/histogram.h"
 #include "magick/monitor.h"
 #include "magick/monitor-private.h"
 #include "magick/option.h"
+#include "magick/pixel-private.h"
 #include "magick/quantum.h"
 #include "magick/quantum-private.h"
 #include "magick/resample.h"
 #include "magick/resample-private.h"
 #include "magick/statistic.h"
 #include "magick/string_.h"
+#include "magick/string-private.h"
 #include "magick/thread-private.h"
 #include "magick/token.h"
 #include "magick/xml-tree.h"
@@ -112,7 +115,7 @@ MagickExport MagickBooleanType AutoGammaImageChannel(Image *image,
     status;
 
   double
-    mean,junk,gamma,logmean;
+    mean,sans,gamma,logmean;
 
   logmean=log(0.5);
 
@@ -121,9 +124,8 @@ MagickExport MagickBooleanType AutoGammaImageChannel(Image *image,
       /*
         Apply gamma correction equally accross all given channels
       */
-      GetImageChannelMean(image, channel, &mean, &junk, &image->exception);
-      gamma = log(mean*QuantumScale)/logmean;
-      //return GammaImageChannel(image, channel, gamma);
+      (void) GetImageChannelMean(image,channel,&mean,&sans,&image->exception);
+      gamma=log(mean*QuantumScale)/logmean;
       return LevelImageChannel(image, channel,
                                0.0, (double)QuantumRange, gamma);
     }
@@ -134,43 +136,43 @@ MagickExport MagickBooleanType AutoGammaImageChannel(Image *image,
   status = MagickTrue;
   if ((channel & RedChannel) != 0)
     {
-      GetImageChannelMean(image, RedChannel, &mean, &junk, &image->exception);
-      gamma = log(mean*QuantumScale)/logmean;
-      //status = status && GammaImageChannel(image, RedChannel, gamma);
+      (void) GetImageChannelMean(image,RedChannel,&mean,&sans,
+        &image->exception);
+      gamma=log(mean*QuantumScale)/logmean;
       status = status && LevelImageChannel(image, RedChannel,
                                0.0, (double)QuantumRange, gamma);
     }
   if ((channel & GreenChannel) != 0)
     {
-      GetImageChannelMean(image, GreenChannel, &mean, &junk, &image->exception);
-      gamma = log(mean*QuantumScale)/logmean;
-      //status = status && GammaImageChannel(image, GreenChannel, gamma);
+      (void) GetImageChannelMean(image,GreenChannel,&mean,&sans,
+        &image->exception);
+      gamma=log(mean*QuantumScale)/logmean;
       status = status && LevelImageChannel(image, GreenChannel,
                                0.0, (double)QuantumRange, gamma);
     }
   if ((channel & BlueChannel) != 0)
     {
-      GetImageChannelMean(image, BlueChannel, &mean, &junk, &image->exception);
-      gamma = log(mean*QuantumScale)/logmean;
-      //status = status && GammaImageChannel(image, BlueChannel, gamma);
+      (void) GetImageChannelMean(image,BlueChannel,&mean,&sans,
+        &image->exception);
+      gamma=log(mean*QuantumScale)/logmean;
       status = status && LevelImageChannel(image, BlueChannel,
                                0.0, (double)QuantumRange, gamma);
     }
   if (((channel & OpacityChannel) != 0) &&
       (image->matte == MagickTrue))
     {
-      GetImageChannelMean(image, OpacityChannel, &mean, &junk, &image->exception);
-      gamma = log(mean*QuantumScale)/logmean;
-      //status = status && GammaImageChannel(image, OpacityChannel, gamma);
+      (void) GetImageChannelMean(image,OpacityChannel,&mean,&sans,
+        &image->exception);
+      gamma=log(mean*QuantumScale)/logmean;
       status = status && LevelImageChannel(image, OpacityChannel,
                                0.0, (double)QuantumRange, gamma);
     }
   if (((channel & IndexChannel) != 0) &&
       (image->colorspace == CMYKColorspace))
     {
-      GetImageChannelMean(image, IndexChannel, &mean, &junk, &image->exception);
-      gamma = log(mean*QuantumScale)/logmean;
-      //status = status && GammaImageChannel(image, IndexChannel, gamma);
+      (void) GetImageChannelMean(image,IndexChannel,&mean,&sans,
+        &image->exception);
+      gamma=log(mean*QuantumScale)/logmean;
       status = status && LevelImageChannel(image, IndexChannel,
                                0.0, (double)QuantumRange, gamma);
     }
@@ -226,6 +228,85 @@ MagickExport MagickBooleanType AutoLevelImageChannel(Image *image,
 %                                                                             %
 %                                                                             %
 %                                                                             %
+%     B r i g h t n e s s C o n t r a s t I m a g e                           %
+%                                                                             %
+%                                                                             %
+%                                                                             %
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%
+%  Use BrightnessContrastImage() to change the brightness and/or contrast of
+%  an image.  It converts the brightness and contrast parameters into slope
+%  and intercept and calls a polynomical function to apply to the image.
+%
+%  The format of the BrightnessContrastImage method is:
+%
+%      MagickBooleanType BrightnessContrastImage(Image *image,
+%        const double brightness,const double contrast)
+%      MagickBooleanType BrightnessContrastImageChannel(Image *image,
+%        const ChannelType channel,const double brightness,
+%        const double contrast)
+%
+%  A description of each parameter follows:
+%
+%    o image: the image.
+%
+%    o channel: the channel.
+%
+%    o brightness: the brightness percent (-100 .. 100).
+%
+%    o contrast: the contrast percent (-100 .. 100).
+%
+*/
+
+MagickExport MagickBooleanType BrightnessContrastImage(Image *image,
+  const double brightness,const double contrast)
+{
+  MagickBooleanType
+    status;
+
+  status=BrightnessContrastImageChannel(image,DefaultChannels,brightness,
+    contrast);
+  return(status);
+}
+
+MagickExport MagickBooleanType BrightnessContrastImageChannel(Image *image,
+  const ChannelType channel,const double brightness,const double contrast)
+{
+#define BrightnessContastImageTag  "BrightnessContast/Image"
+
+  double
+    alpha,
+    intercept,
+    coefficients[2],
+    slope;
+
+  MagickBooleanType
+    status;
+
+  /*
+    Compute slope and intercept.
+  */
+  assert(image != (Image *) NULL);
+  assert(image->signature == MagickSignature);
+  if (image->debug != MagickFalse)
+    (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
+  alpha=contrast;
+  slope=tan((double) (MagickPI*(alpha/100.0+1.0)/4.0));
+  if (slope < 0.0)
+    slope=0.0;
+  intercept=brightness/100.0+((100-brightness)/200.0)*(1.0-slope);
+  coefficients[0]=slope;
+  coefficients[1]=intercept;
+  status=FunctionImageChannel(image,channel,PolynomialFunction,2,coefficients,
+    &image->exception);
+  return(status);
+}
+\f
+/*
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%                                                                             %
+%                                                                             %
+%                                                                             %
 %     C o l o r D e c i s i o n L i s t I m a g e                             %
 %                                                                             %
 %                                                                             %
@@ -288,6 +369,9 @@ MagickExport MagickBooleanType ColorDecisionListImage(Image *image,
       saturation;
   } ColorCorrection;
 
+  CacheView
+    *image_view;
+
   char
     token[MaxTextExtent];
 
@@ -301,28 +385,27 @@ MagickExport MagickBooleanType ColorDecisionListImage(Image *image,
   ExceptionInfo
     *exception;
 
-  long
-    progress,
-    y;
-
   MagickBooleanType
     status;
 
+  MagickOffsetType
+    progress;
+
   PixelPacket
     *cdl_map;
 
-  register long
+  register ssize_t
     i;
 
+  ssize_t
+    y;
+
   XMLTreeInfo
     *cc,
     *ccc,
     *sat,
     *sop;
 
-  CacheView
-    *image_view;
-
   /*
     Allocate and initialize cdl maps.
   */
@@ -371,9 +454,9 @@ MagickExport MagickBooleanType ColorDecisionListImage(Image *image,
               GetMagickToken(p,&p,token);
             switch (i)
             {
-              case 0: color_correction.red.slope=atof(token); break;
-              case 1: color_correction.green.slope=atof(token); break;
-              case 2: color_correction.blue.slope=atof(token); break;
+              case 0: color_correction.red.slope=LocaleToDouble(token,(char **) NULL); break;
+              case 1: color_correction.green.slope=LocaleToDouble(token,(char **) NULL); break;
+              case 2: color_correction.blue.slope=LocaleToDouble(token,(char **) NULL); break;
             }
           }
         }
@@ -389,9 +472,9 @@ MagickExport MagickBooleanType ColorDecisionListImage(Image *image,
               GetMagickToken(p,&p,token);
             switch (i)
             {
-              case 0: color_correction.red.offset=atof(token); break;
-              case 1: color_correction.green.offset=atof(token); break;
-              case 2: color_correction.blue.offset=atof(token); break;
+              case 0: color_correction.red.offset=LocaleToDouble(token,(char **) NULL); break;
+              case 1: color_correction.green.offset=LocaleToDouble(token,(char **) NULL); break;
+              case 2: color_correction.blue.offset=LocaleToDouble(token,(char **) NULL); break;
             }
           }
         }
@@ -407,9 +490,9 @@ MagickExport MagickBooleanType ColorDecisionListImage(Image *image,
               GetMagickToken(p,&p,token);
             switch (i)
             {
-              case 0: color_correction.red.power=atof(token); break;
-              case 1: color_correction.green.power=atof(token); break;
-              case 2: color_correction.blue.power=atof(token); break;
+              case 0: color_correction.red.power=LocaleToDouble(token,(char **) NULL); break;
+              case 1: color_correction.green.power=LocaleToDouble(token,(char **) NULL); break;
+              case 2: color_correction.blue.power=LocaleToDouble(token,(char **) NULL); break;
             }
           }
         }
@@ -426,7 +509,7 @@ MagickExport MagickBooleanType ColorDecisionListImage(Image *image,
           content=GetXMLTreeContent(saturation);
           p=(const char *) content;
           GetMagickToken(p,&p,token);
-          color_correction.saturation=atof(token);
+          color_correction.saturation=LocaleToDouble(token,(char **) NULL);
         }
     }
   ccc=DestroyXMLTree(ccc);
@@ -459,18 +542,18 @@ MagickExport MagickBooleanType ColorDecisionListImage(Image *image,
   if (cdl_map == (PixelPacket *) NULL)
     ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
       image->filename);
-#if defined(_OPENMP) && (_OPENMP >= 200203)
-  #pragma omp parallel for
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
+  #pragma omp parallel for schedule(dynamic,4)
 #endif
-  for (i=0; i <= (long) MaxMap; i++)
+  for (i=0; i <= (ssize_t) MaxMap; i++)
   {
-    cdl_map[i].red=RoundToQuantum((MagickRealType) ScaleMapToQuantum((
+    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=RoundToQuantum((MagickRealType) ScaleMapToQuantum((
+    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=RoundToQuantum((MagickRealType) ScaleMapToQuantum((
+    cdl_map[i].blue=ClampToQuantum((MagickRealType) ScaleMapToQuantum((
       MagickRealType) (MaxMap*(pow(color_correction.blue.slope*i/MaxMap+
       color_correction.blue.offset,color_correction.blue.power)))));
   }
@@ -479,22 +562,22 @@ MagickExport MagickBooleanType ColorDecisionListImage(Image *image,
       /*
         Apply transfer function to colormap.
       */
-#if defined(_OPENMP) && (_OPENMP >= 200203)
-  #pragma omp parallel for shared(progress,status)
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
+  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
 #endif
-      for (i=0; i < (long) image->colors; i++)
+      for (i=0; i < (ssize_t) image->colors; i++)
       {
         double
           luma;
 
         luma=0.2126*image->colormap[i].red+0.7152*image->colormap[i].green+
           0.0722*image->colormap[i].blue;
-        image->colormap[i].red=RoundToQuantum(luma+color_correction.saturation*
+        image->colormap[i].red=ClampToQuantum(luma+color_correction.saturation*
           cdl_map[ScaleQuantumToMap(image->colormap[i].red)].red-luma);
-        image->colormap[i].green=RoundToQuantum(luma+
+        image->colormap[i].green=ClampToQuantum(luma+
           color_correction.saturation*cdl_map[ScaleQuantumToMap(
           image->colormap[i].green)].green-luma);
-        image->colormap[i].blue=RoundToQuantum(luma+color_correction.saturation*
+        image->colormap[i].blue=ClampToQuantum(luma+color_correction.saturation*
           cdl_map[ScaleQuantumToMap(image->colormap[i].blue)].blue-luma);
       }
     }
@@ -505,19 +588,19 @@ MagickExport MagickBooleanType ColorDecisionListImage(Image *image,
   progress=0;
   exception=(&image->exception);
   image_view=AcquireCacheView(image);
-#if defined(_OPENMP) && (_OPENMP >= 200203)
-  #pragma omp parallel for shared(progress,status)
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
+  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
 #endif
-  for (y=0; y < (long) image->rows; y++)
+  for (y=0; y < (ssize_t) image->rows; y++)
   {
     double
       luma;
 
-    register long
-      x;
-
     register PixelPacket
-      *__restrict q;
+      *restrict q;
+
+    register ssize_t
+      x;
 
     if (status == MagickFalse)
       continue;
@@ -527,15 +610,16 @@ MagickExport MagickBooleanType ColorDecisionListImage(Image *image,
         status=MagickFalse;
         continue;
       }
-    for (x=0; x < (long) image->columns; x++)
+    for (x=0; x < (ssize_t) image->columns; x++)
     {
-      luma=0.2126*q->red+0.7152*q->green+0.0722*q->blue;
-      q->red=RoundToQuantum(luma+color_correction.saturation*
-        (cdl_map[ScaleQuantumToMap(q->red)].red-luma));
-      q->green=RoundToQuantum(luma+color_correction.saturation*
-        (cdl_map[ScaleQuantumToMap(q->green)].green-luma));
-      q->blue=RoundToQuantum(luma+color_correction.saturation*
-        (cdl_map[ScaleQuantumToMap(q->blue)].blue-luma));
+      luma=0.2126*GetRedPixelComponent(q)+0.7152*GetGreenPixelComponent(q)+
+        0.0722*GetBluePixelComponent(q);
+      SetRedPixelComponent(q,ClampToQuantum(luma+color_correction.saturation*
+        (cdl_map[ScaleQuantumToMap(GetRedPixelComponent(q))].red-luma)));
+      SetGreenPixelComponent(q,ClampToQuantum(luma+color_correction.saturation*
+        (cdl_map[ScaleQuantumToMap(GetGreenPixelComponent(q))].green-luma)));
+      SetBluePixelComponent(q,ClampToQuantum(luma+color_correction.saturation*
+        (cdl_map[ScaleQuantumToMap(GetBluePixelComponent(q))].blue-luma)));
       q++;
     }
     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
@@ -545,7 +629,7 @@ MagickExport MagickBooleanType ColorDecisionListImage(Image *image,
         MagickBooleanType
           proceed;
 
-#if defined(_OPENMP) && (_OPENMP >= 200203)
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
   #pragma omp critical (MagickCore_ColorDecisionListImageChannel)
 #endif
         proceed=SetImageProgress(image,ColorDecisionListCorrectImageTag,
@@ -614,25 +698,28 @@ MagickExport MagickBooleanType ClutImageChannel(Image *image,
 {
 #define ClutImageTag  "Clut/Image"
 
+  CacheView
+    *clut_view,
+    *image_view;
+
   ExceptionInfo
     *exception;
 
-  long
-    adjust,
-    progress,
-    y;
-
   MagickBooleanType
     status;
 
+  MagickOffsetType
+    progress;
+
   MagickPixelPacket
-    zero;
+    *clut_map;
 
-  ResampleFilter
-    **resample_filter;
+  register ssize_t
+    i;
 
-  CacheView
-    *image_view;
+  ssize_t
+    adjust,
+    y;
 
   assert(image != (Image *) NULL);
   assert(image->signature == MagickSignature);
@@ -642,34 +729,47 @@ MagickExport MagickBooleanType ClutImageChannel(Image *image,
   assert(clut_image->signature == MagickSignature);
   if (SetImageStorageClass(image,DirectClass) == MagickFalse)
     return(MagickFalse);
+  clut_map=(MagickPixelPacket *) AcquireQuantumMemory(MaxMap+1UL,
+    sizeof(*clut_map));
+  if (clut_map == (MagickPixelPacket *) NULL)
+    ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
+      image->filename);
   /*
     Clut image.
   */
   status=MagickTrue;
   progress=0;
-  GetMagickPixelPacket(clut_image,&zero);
-  adjust=clut_image->interpolate == IntegerInterpolatePixel ? 0 : 1;
+  adjust=(ssize_t) (clut_image->interpolate == IntegerInterpolatePixel ? 0 : 1);
   exception=(&image->exception);
-  resample_filter=AcquireResampleFilterThreadSet(clut_image,MagickTrue,
-    exception);
+  clut_view=AcquireCacheView(clut_image);
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
+  #pragma omp parallel for schedule(dynamic,4)
+#endif
+  for (i=0; i <= (ssize_t) MaxMap; i++)
+  {
+    GetMagickPixelPacket(clut_image,clut_map+i);
+    (void) InterpolateMagickPixelPacket(clut_image,clut_view,
+      UndefinedInterpolatePixel,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);
-#if defined(_OPENMP) && (_OPENMP >= 200203)
-  #pragma omp parallel for shared(progress,status)
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
+  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
 #endif
-  for (y=0; y < (long) image->rows; y++)
+  for (y=0; y < (ssize_t) image->rows; y++)
   {
     MagickPixelPacket
       pixel;
 
     register IndexPacket
-      *__restrict indexes;
-
-    register long
-      id,
-      x;
+      *restrict indexes;
 
     register PixelPacket
-      *__restrict q;
+      *restrict q;
+
+    register ssize_t
+      x;
 
     if (status == MagickFalse)
       continue;
@@ -680,86 +780,37 @@ MagickExport MagickBooleanType ClutImageChannel(Image *image,
         continue;
       }
     indexes=GetCacheViewAuthenticIndexQueue(image_view);
-    pixel=zero;
-    id=GetOpenMPThreadId();
-    for (x=0; x < (long) image->columns; x++)
+    GetMagickPixelPacket(image,&pixel);
+    for (x=0; x < (ssize_t) image->columns; x++)
     {
-      /*
-        PROGRAMMERS WARNING:
-
-        Apply OpacityChannel BEFORE the color channels.  Do not re-order.
-
-        The handling special case 2 (coloring gray-scale), requires access to
-        the unmodified colors of the original image to determine the index
-        value.  As such alpha/matte channel handling must be performed BEFORE,
-        any of the color channels are modified.
-
-      */
+      SetMagickPixelPacket(image,q,indexes+x,&pixel);
+      if ((channel & RedChannel) != 0)
+        SetRedPixelComponent(q,ClampRedPixelComponent(clut_map+
+          ScaleQuantumToMap(GetRedPixelComponent(q))));
+      if ((channel & GreenChannel) != 0)
+        SetGreenPixelComponent(q,ClampGreenPixelComponent(clut_map+
+          ScaleQuantumToMap(GetGreenPixelComponent(q))));
+      if ((channel & BlueChannel) != 0)
+        SetBluePixelComponent(q,ClampBluePixelComponent(clut_map+
+          ScaleQuantumToMap(GetBluePixelComponent(q))));
       if ((channel & OpacityChannel) != 0)
         {
           if (clut_image->matte == MagickFalse)
-            {
-              /*
-                A gray-scale LUT replacement for an image alpha channel.
-              */
-              (void) ResamplePixelColor(resample_filter[id],QuantumScale*
-                (QuantumRange-q->opacity)*(clut_image->columns+adjust),
-                QuantumScale*(QuantumRange-q->opacity)*(clut_image->rows+
-                adjust),&pixel);
-              q->opacity=(Quantum) (QuantumRange-MagickPixelIntensityToQuantum(
-                &pixel));
-            }
+            SetOpacityPixelComponent(q,(QuantumRange-
+              MagickPixelIntensityToQuantum(clut_map+ScaleQuantumToMap(
+              (Quantum) GetAlphaPixelComponent(q)))));
           else
             if (image->matte == MagickFalse)
-              {
-                /*
-                  A greyscale image being colored by a LUT with transparency.
-                */
-                (void) ResamplePixelColor(resample_filter[id],QuantumScale*
-                  PixelIntensity(q)*(clut_image->columns-adjust),QuantumScale*
-                  PixelIntensity(q)*(clut_image->rows-adjust),&pixel);
-                q->opacity=RoundToQuantum(pixel.opacity);
-              }
+              SetOpacityPixelComponent(q,ClampOpacityPixelComponent(clut_map+
+                ScaleQuantumToMap((Quantum) MagickPixelIntensity(&pixel))));
             else
-              {
-                /*
-                  Direct alpha channel lookup.
-                */
-                (void) ResamplePixelColor(resample_filter[id],QuantumScale*
-                  q->opacity*(clut_image->columns-adjust),QuantumScale*
-                  q->opacity* (clut_image->rows-adjust),&pixel);
-                q->opacity=RoundToQuantum(pixel.opacity);
-              }
-        }
-      if ((channel & RedChannel) != 0)
-        {
-          (void) ResamplePixelColor(resample_filter[id],QuantumScale*q->red*
-            (clut_image->columns-adjust),QuantumScale*q->red*
-            (clut_image->rows-adjust),&pixel);
-          q->red=RoundToQuantum(pixel.red);
-        }
-      if ((channel & GreenChannel) != 0)
-        {
-          (void) ResamplePixelColor(resample_filter[id],QuantumScale*q->green*
-            (clut_image->columns-adjust),QuantumScale*q->green*
-            (clut_image->rows-adjust),&pixel);
-          q->green=RoundToQuantum(pixel.green);
-        }
-      if ((channel & BlueChannel) != 0)
-        {
-          (void) ResamplePixelColor(resample_filter[id],QuantumScale*q->blue*
-            (clut_image->columns-adjust),QuantumScale*q->blue*
-            (clut_image->rows-adjust),&pixel);
-          q->blue=RoundToQuantum(pixel.blue);
+              SetOpacityPixelComponent(q,ClampOpacityPixelComponent(
+                clut_map+ScaleQuantumToMap(GetOpacityPixelComponent(q))));
         }
       if (((channel & IndexChannel) != 0) &&
           (image->colorspace == CMYKColorspace))
-        {
-          (void) ResamplePixelColor(resample_filter[id],QuantumScale*indexes[x]*
-            (clut_image->columns-adjust),QuantumScale*indexes[x]*
-            (clut_image->rows-adjust),&pixel);
-          indexes[x]=RoundToQuantum(pixel.index);
-        }
+        SetIndexPixelComponent(indexes+x,ClampToQuantum((clut_map+(ssize_t)
+          GetIndexPixelComponent(indexes+x))->index));
       q++;
     }
     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
@@ -769,7 +820,7 @@ MagickExport MagickBooleanType ClutImageChannel(Image *image,
         MagickBooleanType
           proceed;
 
-#if defined(_OPENMP) && (_OPENMP >= 200203)
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
   #pragma omp critical (MagickCore_ClutImageChannel)
 #endif
         proceed=SetImageProgress(image,ClutImageTag,progress++,image->rows);
@@ -778,10 +829,7 @@ MagickExport MagickBooleanType ClutImageChannel(Image *image,
       }
   }
   image_view=DestroyCacheView(image_view);
-  resample_filter=DestroyResampleFilterThreadSet(resample_filter);
-  /*
-    Enable alpha channel if CLUT image could enable it.
-  */
+  clut_map=(MagickPixelPacket *) RelinquishMagickMemory(clut_map);
   if ((clut_image->matte != MagickFalse) && ((channel & OpacityChannel) != 0))
     (void) SetImageAlphaChannel(image,ActivateAlphaChannel);
   return(status);
@@ -832,7 +880,8 @@ static void Contrast(const int sign,Quantum *red,Quantum *green,Quantum *blue)
   saturation=0.0;
   brightness=0.0;
   ConvertRGBToHSB(*red,*green,*blue,&hue,&saturation,&brightness);
-  brightness+=0.5*sign*(0.5*(sin(MagickPI*(brightness-0.5))+1.0)-brightness);
+  brightness+=0.5*sign*(0.5*(sin((double) (MagickPI*(brightness-0.5)))+1.0)-
+    brightness);
   if (brightness > 1.0)
     brightness=1.0;
   else
@@ -846,24 +895,26 @@ MagickExport MagickBooleanType ContrastImage(Image *image,
 {
 #define ContrastImageTag  "Contrast/Image"
 
+  CacheView
+    *image_view;
+
   ExceptionInfo
     *exception;
 
   int
     sign;
 
-  long
-    progress,
-    y;
-
   MagickBooleanType
     status;
 
-  register long
+  MagickOffsetType
+    progress;
+
+  register ssize_t
     i;
 
-  CacheView
-    *image_view;
+  ssize_t
+    y;
 
   assert(image != (Image *) NULL);
   assert(image->signature == MagickSignature);
@@ -875,7 +926,7 @@ MagickExport MagickBooleanType ContrastImage(Image *image,
       /*
         Contrast enhance colormap.
       */
-      for (i=0; i < (long) image->colors; i++)
+      for (i=0; i < (ssize_t) image->colors; i++)
         Contrast(sign,&image->colormap[i].red,&image->colormap[i].green,
           &image->colormap[i].blue);
     }
@@ -886,16 +937,21 @@ MagickExport MagickBooleanType ContrastImage(Image *image,
   progress=0;
   exception=(&image->exception);
   image_view=AcquireCacheView(image);
-#if defined(_OPENMP) && (_OPENMP >= 200203)
-  #pragma omp parallel for shared(progress,status)
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
+  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
 #endif
-  for (y=0; y < (long) image->rows; y++)
+  for (y=0; y < (ssize_t) image->rows; y++)
   {
-    register long
-      x;
+    Quantum
+      blue,
+      green,
+      red;
 
     register PixelPacket
-      *__restrict q;
+      *restrict q;
+
+    register ssize_t
+      x;
 
     if (status == MagickFalse)
       continue;
@@ -905,9 +961,15 @@ MagickExport MagickBooleanType ContrastImage(Image *image,
         status=MagickFalse;
         continue;
       }
-    for (x=0; x < (long) image->columns; x++)
+    for (x=0; x < (ssize_t) image->columns; x++)
     {
-      Contrast(sign,&q->red,&q->green,&q->blue);
+      red=GetRedPixelComponent(q);
+      green=GetGreenPixelComponent(q);
+      blue=GetBluePixelComponent(q);
+      Contrast(sign,&red,&green,&blue);
+      SetRedPixelComponent(q,red);
+      SetGreenPixelComponent(q,green);
+      SetBluePixelComponent(q,blue);
       q++;
     }
     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
@@ -917,7 +979,7 @@ MagickExport MagickBooleanType ContrastImage(Image *image,
         MagickBooleanType
           proceed;
 
-#if defined(_OPENMP) && (_OPENMP >= 200203)
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
   #pragma omp critical (MagickCore_ContrastImage)
 #endif
         proceed=SetImageProgress(image,ContrastImageTag,progress++,image->rows);
@@ -952,7 +1014,7 @@ MagickExport MagickBooleanType ContrastImage(Image *image,
 %      MagickBooleanType ContrastStretchImage(Image *image,
 %        const char *levels)
 %      MagickBooleanType ContrastStretchImageChannel(Image *image,
-%        const unsigned long channel,const double black_point,
+%        const size_t channel,const double black_point,
 %        const double white_point)
 %
 %  A description of each parameter follows:
@@ -1014,30 +1076,32 @@ MagickExport MagickBooleanType ContrastStretchImageChannel(Image *image,
 #define MaxRange(color)  ((MagickRealType) ScaleQuantumToMap((Quantum) (color)))
 #define ContrastStretchImageTag  "ContrastStretch/Image"
 
+  CacheView
+    *image_view;
+
   double
     intensity;
 
   ExceptionInfo
     *exception;
 
-  long
-    progress,
-    y;
-
   MagickBooleanType
     status;
 
+  MagickOffsetType
+    progress;
+
   MagickPixelPacket
     black,
     *histogram,
     *stretch_map,
     white;
 
-  register long
+  register ssize_t
     i;
 
-  CacheView
-    *image_view;
+  ssize_t
+    y;
 
   /*
     Allocate histogram and stretch map.
@@ -1061,15 +1125,15 @@ MagickExport MagickBooleanType ContrastStretchImageChannel(Image *image,
   exception=(&image->exception);
   (void) ResetMagickMemory(histogram,0,(MaxMap+1)*sizeof(*histogram));
   image_view=AcquireCacheView(image);
-  for (y=0; y < (long) image->rows; y++)
+  for (y=0; y < (ssize_t) image->rows; y++)
   {
     register const PixelPacket
-      *__restrict p;
+      *restrict p;
 
     register IndexPacket
-      *__restrict indexes;
+      *restrict indexes;
 
-    register long
+    register ssize_t
       x;
 
     if (status == MagickFalse)
@@ -1082,7 +1146,7 @@ MagickExport MagickBooleanType ContrastStretchImageChannel(Image *image,
       }
     indexes=GetCacheViewAuthenticIndexQueue(image_view);
     if (channel == DefaultChannels)
-      for (x=0; x < (long) image->columns; x++)
+      for (x=0; x < (ssize_t) image->columns; x++)
       {
         Quantum
           intensity;
@@ -1095,19 +1159,20 @@ MagickExport MagickBooleanType ContrastStretchImageChannel(Image *image,
         p++;
       }
     else
-      for (x=0; x < (long) image->columns; x++)
+      for (x=0; x < (ssize_t) image->columns; x++)
       {
         if ((channel & RedChannel) != 0)
-          histogram[ScaleQuantumToMap(p->red)].red++;
+          histogram[ScaleQuantumToMap(GetRedPixelComponent(p))].red++;
         if ((channel & GreenChannel) != 0)
-          histogram[ScaleQuantumToMap(p->green)].green++;
+          histogram[ScaleQuantumToMap(GetGreenPixelComponent(p))].green++;
         if ((channel & BlueChannel) != 0)
-          histogram[ScaleQuantumToMap(p->blue)].blue++;
+          histogram[ScaleQuantumToMap(GetBluePixelComponent(p))].blue++;
         if ((channel & OpacityChannel) != 0)
-          histogram[ScaleQuantumToMap(p->opacity)].opacity++;
+          histogram[ScaleQuantumToMap(GetOpacityPixelComponent(p))].opacity++;
         if (((channel & IndexChannel) != 0) &&
             (image->colorspace == CMYKColorspace))
-          histogram[ScaleQuantumToMap(indexes[x])].index++;
+          histogram[ScaleQuantumToMap(GetIndexPixelComponent(
+            indexes+x))].index++;
         p++;
       }
   }
@@ -1119,7 +1184,7 @@ MagickExport MagickBooleanType ContrastStretchImageChannel(Image *image,
   if ((channel & RedChannel) != 0)
     {
       intensity=0.0;
-      for (i=0; i <= (long) MaxMap; i++)
+      for (i=0; i <= (ssize_t) MaxMap; i++)
       {
         intensity+=histogram[i].red;
         if (intensity > black_point)
@@ -1127,7 +1192,7 @@ MagickExport MagickBooleanType ContrastStretchImageChannel(Image *image,
       }
       black.red=(MagickRealType) i;
       intensity=0.0;
-      for (i=(long) MaxMap; i != 0; i--)
+      for (i=(ssize_t) MaxMap; i != 0; i--)
       {
         intensity+=histogram[i].red;
         if (intensity > ((double) image->columns*image->rows-white_point))
@@ -1140,7 +1205,7 @@ MagickExport MagickBooleanType ContrastStretchImageChannel(Image *image,
   if ((channel & GreenChannel) != 0)
     {
       intensity=0.0;
-      for (i=0; i <= (long) MaxMap; i++)
+      for (i=0; i <= (ssize_t) MaxMap; i++)
       {
         intensity+=histogram[i].green;
         if (intensity > black_point)
@@ -1148,7 +1213,7 @@ MagickExport MagickBooleanType ContrastStretchImageChannel(Image *image,
       }
       black.green=(MagickRealType) i;
       intensity=0.0;
-      for (i=(long) MaxMap; i != 0; i--)
+      for (i=(ssize_t) MaxMap; i != 0; i--)
       {
         intensity+=histogram[i].green;
         if (intensity > ((double) image->columns*image->rows-white_point))
@@ -1161,7 +1226,7 @@ MagickExport MagickBooleanType ContrastStretchImageChannel(Image *image,
   if ((channel & BlueChannel) != 0)
     {
       intensity=0.0;
-      for (i=0; i <= (long) MaxMap; i++)
+      for (i=0; i <= (ssize_t) MaxMap; i++)
       {
         intensity+=histogram[i].blue;
         if (intensity > black_point)
@@ -1169,7 +1234,7 @@ MagickExport MagickBooleanType ContrastStretchImageChannel(Image *image,
       }
       black.blue=(MagickRealType) i;
       intensity=0.0;
-      for (i=(long) MaxMap; i != 0; i--)
+      for (i=(ssize_t) MaxMap; i != 0; i--)
       {
         intensity+=histogram[i].blue;
         if (intensity > ((double) image->columns*image->rows-white_point))
@@ -1182,7 +1247,7 @@ MagickExport MagickBooleanType ContrastStretchImageChannel(Image *image,
   if ((channel & OpacityChannel) != 0)
     {
       intensity=0.0;
-      for (i=0; i <= (long) MaxMap; i++)
+      for (i=0; i <= (ssize_t) MaxMap; i++)
       {
         intensity+=histogram[i].opacity;
         if (intensity > black_point)
@@ -1190,7 +1255,7 @@ MagickExport MagickBooleanType ContrastStretchImageChannel(Image *image,
       }
       black.opacity=(MagickRealType) i;
       intensity=0.0;
-      for (i=(long) MaxMap; i != 0; i--)
+      for (i=(ssize_t) MaxMap; i != 0; i--)
       {
         intensity+=histogram[i].opacity;
         if (intensity > ((double) image->columns*image->rows-white_point))
@@ -1203,7 +1268,7 @@ MagickExport MagickBooleanType ContrastStretchImageChannel(Image *image,
   if (((channel & IndexChannel) != 0) && (image->colorspace == CMYKColorspace))
     {
       intensity=0.0;
-      for (i=0; i <= (long) MaxMap; i++)
+      for (i=0; i <= (ssize_t) MaxMap; i++)
       {
         intensity+=histogram[i].index;
         if (intensity > black_point)
@@ -1211,7 +1276,7 @@ MagickExport MagickBooleanType ContrastStretchImageChannel(Image *image,
       }
       black.index=(MagickRealType) i;
       intensity=0.0;
-      for (i=(long) MaxMap; i != 0; i--)
+      for (i=(ssize_t) MaxMap; i != 0; i--)
       {
         intensity+=histogram[i].index;
         if (intensity > ((double) image->columns*image->rows-white_point))
@@ -1224,17 +1289,17 @@ MagickExport MagickBooleanType ContrastStretchImageChannel(Image *image,
     Stretch the histogram to create the stretched image mapping.
   */
   (void) ResetMagickMemory(stretch_map,0,(MaxMap+1)*sizeof(*stretch_map));
-#if defined(_OPENMP) && (_OPENMP >= 200203)
-  #pragma omp parallel for shared(progress,status)
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
+  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
 #endif
-  for (i=0; i <= (long) MaxMap; i++)
+  for (i=0; i <= (ssize_t) MaxMap; i++)
   {
     if ((channel & RedChannel) != 0)
       {
-        if (i < (long) black.red)
+        if (i < (ssize_t) black.red)
           stretch_map[i].red=0.0;
         else
-          if (i > (long) white.red)
+          if (i > (ssize_t) white.red)
             stretch_map[i].red=(MagickRealType) QuantumRange;
           else
             if (black.red != white.red)
@@ -1243,10 +1308,10 @@ MagickExport MagickBooleanType ContrastStretchImageChannel(Image *image,
       }
     if ((channel & GreenChannel) != 0)
       {
-        if (i < (long) black.green)
+        if (i < (ssize_t) black.green)
           stretch_map[i].green=0.0;
         else
-          if (i > (long) white.green)
+          if (i > (ssize_t) white.green)
             stretch_map[i].green=(MagickRealType) QuantumRange;
           else
             if (black.green != white.green)
@@ -1256,10 +1321,10 @@ MagickExport MagickBooleanType ContrastStretchImageChannel(Image *image,
       }
     if ((channel & BlueChannel) != 0)
       {
-        if (i < (long) black.blue)
+        if (i < (ssize_t) black.blue)
           stretch_map[i].blue=0.0;
         else
-          if (i > (long) white.blue)
+          if (i > (ssize_t) white.blue)
             stretch_map[i].blue=(MagickRealType) QuantumRange;
           else
             if (black.blue != white.blue)
@@ -1269,10 +1334,10 @@ MagickExport MagickBooleanType ContrastStretchImageChannel(Image *image,
       }
     if ((channel & OpacityChannel) != 0)
       {
-        if (i < (long) black.opacity)
+        if (i < (ssize_t) black.opacity)
           stretch_map[i].opacity=0.0;
         else
-          if (i > (long) white.opacity)
+          if (i > (ssize_t) white.opacity)
             stretch_map[i].opacity=(MagickRealType) QuantumRange;
           else
             if (black.opacity != white.opacity)
@@ -1283,10 +1348,10 @@ MagickExport MagickBooleanType ContrastStretchImageChannel(Image *image,
     if (((channel & IndexChannel) != 0) &&
         (image->colorspace == CMYKColorspace))
       {
-        if (i < (long) black.index)
+        if (i < (ssize_t) black.index)
           stretch_map[i].index=0.0;
         else
-          if (i > (long) white.index)
+          if (i > (ssize_t) white.index)
             stretch_map[i].index=(MagickRealType) QuantumRange;
           else
             if (black.index != white.index)
@@ -1306,33 +1371,33 @@ MagickExport MagickBooleanType ContrastStretchImageChannel(Image *image,
       /*
         Stretch colormap.
       */
-#if defined(_OPENMP) && (_OPENMP >= 200203)
-  #pragma omp parallel for shared(progress,status)
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
+  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
 #endif
-      for (i=0; i < (long) image->colors; i++)
+      for (i=0; i < (ssize_t) image->colors; i++)
       {
         if ((channel & RedChannel) != 0)
           {
             if (black.red != white.red)
-              image->colormap[i].red=RoundToQuantum(stretch_map[
+              image->colormap[i].red=ClampToQuantum(stretch_map[
                 ScaleQuantumToMap(image->colormap[i].red)].red);
           }
         if ((channel & GreenChannel) != 0)
           {
             if (black.green != white.green)
-              image->colormap[i].green=RoundToQuantum(stretch_map[
+              image->colormap[i].green=ClampToQuantum(stretch_map[
                 ScaleQuantumToMap(image->colormap[i].green)].green);
           }
         if ((channel & BlueChannel) != 0)
           {
             if (black.blue != white.blue)
-              image->colormap[i].blue=RoundToQuantum(stretch_map[
+              image->colormap[i].blue=ClampToQuantum(stretch_map[
                 ScaleQuantumToMap(image->colormap[i].blue)].blue);
           }
         if ((channel & OpacityChannel) != 0)
           {
             if (black.opacity != white.opacity)
-              image->colormap[i].opacity=RoundToQuantum(stretch_map[
+              image->colormap[i].opacity=ClampToQuantum(stretch_map[
                 ScaleQuantumToMap(image->colormap[i].opacity)].opacity);
           }
       }
@@ -1342,19 +1407,19 @@ MagickExport MagickBooleanType ContrastStretchImageChannel(Image *image,
   */
   status=MagickTrue;
   progress=0;
-#if defined(_OPENMP) && (_OPENMP >= 200203)
-  #pragma omp parallel for shared(progress,status)
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
+  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
 #endif
-  for (y=0; y < (long) image->rows; y++)
+  for (y=0; y < (ssize_t) image->rows; y++)
   {
     register IndexPacket
-      *__restrict indexes;
-
-    register long
-      x;
+      *restrict indexes;
 
     register PixelPacket
-      *__restrict q;
+      *restrict q;
+
+    register ssize_t
+      x;
 
     if (status == MagickFalse)
       continue;
@@ -1365,37 +1430,38 @@ MagickExport MagickBooleanType ContrastStretchImageChannel(Image *image,
         continue;
       }
     indexes=GetCacheViewAuthenticIndexQueue(image_view);
-    for (x=0; x < (long) image->columns; x++)
+    for (x=0; x < (ssize_t) image->columns; x++)
     {
       if ((channel & RedChannel) != 0)
         {
           if (black.red != white.red)
-            q->red=RoundToQuantum(stretch_map[ScaleQuantumToMap(q->red)].red);
+            SetRedPixelComponent(q,ClampToQuantum(stretch_map[
+              ScaleQuantumToMap(GetRedPixelComponent(q))].red));
         }
       if ((channel & GreenChannel) != 0)
         {
           if (black.green != white.green)
-            q->green=RoundToQuantum(stretch_map[ScaleQuantumToMap(
-              q->green)].green);
+            SetGreenPixelComponent(q,ClampToQuantum(stretch_map[
+              ScaleQuantumToMap(GetGreenPixelComponent(q))].green));
         }
       if ((channel & BlueChannel) != 0)
         {
           if (black.blue != white.blue)
-            q->blue=RoundToQuantum(stretch_map[ScaleQuantumToMap(
-              q->blue)].blue);
+            SetBluePixelComponent(q,ClampToQuantum(stretch_map[
+              ScaleQuantumToMap(GetBluePixelComponent(q))].blue));
         }
       if ((channel & OpacityChannel) != 0)
         {
           if (black.opacity != white.opacity)
-            q->opacity=RoundToQuantum(stretch_map[ScaleQuantumToMap(
-              q->opacity)].opacity);
+            SetOpacityPixelComponent(q,ClampToQuantum(stretch_map[
+              ScaleQuantumToMap(GetOpacityPixelComponent(q))].opacity));
         }
       if (((channel & IndexChannel) != 0) &&
           (image->colorspace == CMYKColorspace))
         {
           if (black.index != white.index)
-            indexes[x]=(IndexPacket) RoundToQuantum(stretch_map[
-              ScaleQuantumToMap(indexes[x])].index);
+            SetIndexPixelComponent(indexes+x,ClampToQuantum(stretch_map[
+              ScaleQuantumToMap(GetIndexPixelComponent(indexes+x))].index));
         }
       q++;
     }
@@ -1406,7 +1472,7 @@ MagickExport MagickBooleanType ContrastStretchImageChannel(Image *image,
         MagickBooleanType
           proceed;
 
-#if defined(_OPENMP) && (_OPENMP >= 200203)
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
   #pragma omp critical (MagickCore_ContrastStretchImageChannel)
 #endif
         proceed=SetImageProgress(image,ContrastStretchImageTag,progress++,
@@ -1448,15 +1514,17 @@ MagickExport MagickBooleanType ContrastStretchImageChannel(Image *image,
 MagickExport Image *EnhanceImage(const Image *image,ExceptionInfo *exception)
 {
 #define Enhance(weight) \
-  mean=((MagickRealType) r->red+pixel.red)/2; \
-  distance=(MagickRealType) r->red-(MagickRealType) pixel.red; \
+  mean=((MagickRealType) GetRedPixelComponent(r)+pixel.red)/2; \
+  distance=(MagickRealType) GetRedPixelComponent(r)-(MagickRealType) pixel.red; \
   distance_squared=QuantumScale*(2.0*((MagickRealType) QuantumRange+1.0)+ \
      mean)*distance*distance; \
-  mean=((MagickRealType) r->green+pixel.green)/2; \
-  distance=(MagickRealType) r->green-(MagickRealType) pixel.green; \
+  mean=((MagickRealType) GetGreenPixelComponent(r)+pixel.green)/2; \
+  distance=(MagickRealType) GetGreenPixelComponent(r)-(MagickRealType) \
+    pixel.green; \
   distance_squared+=4.0*distance*distance; \
-  mean=((MagickRealType) r->blue+pixel.blue)/2; \
-  distance=(MagickRealType) r->blue-(MagickRealType) pixel.blue; \
+  mean=((MagickRealType) GetBluePixelComponent(r)+pixel.blue)/2; \
+  distance=(MagickRealType) GetBluePixelComponent(r)-(MagickRealType) \
+    pixel.blue; \
   distance_squared+=QuantumScale*(3.0*((MagickRealType) \
     QuantumRange+1.0)-1.0-mean)*distance*distance; \
   mean=((MagickRealType) r->opacity+pixel.opacity)/2; \
@@ -1466,31 +1534,33 @@ MagickExport Image *EnhanceImage(const Image *image,ExceptionInfo *exception)
   if (distance_squared < ((MagickRealType) QuantumRange*(MagickRealType) \
       QuantumRange/25.0f)) \
     { \
-      aggregate.red+=(weight)*r->red; \
-      aggregate.green+=(weight)*r->green; \
-      aggregate.blue+=(weight)*r->blue; \
-      aggregate.opacity+=(weight)*r->opacity; \
+      aggregate.red+=(weight)*GetRedPixelComponent(r); \
+      aggregate.green+=(weight)*GetGreenPixelComponent(r); \
+      aggregate.blue+=(weight)*GetBluePixelComponent(r); \
+      aggregate.opacity+=(weight)*GetOpacityPixelComponent(r); \
       total_weight+=(weight); \
     } \
   r++;
 #define EnhanceImageTag  "Enhance/Image"
 
+  CacheView
+    *enhance_view,
+    *image_view;
+
   Image
     *enhance_image;
 
-  long
-    progress,
-    y;
-
   MagickBooleanType
     status;
 
+  MagickOffsetType
+    progress;
+
   MagickPixelPacket
     zero;
 
-  CacheView
-    *enhance_view,
-    *image_view;
+  ssize_t
+    y;
 
   /*
     Initialize enhanced image attributes.
@@ -1521,19 +1591,19 @@ MagickExport Image *EnhanceImage(const Image *image,ExceptionInfo *exception)
   (void) ResetMagickMemory(&zero,0,sizeof(zero));
   image_view=AcquireCacheView(image);
   enhance_view=AcquireCacheView(enhance_image);
-#if defined(_OPENMP) && (_OPENMP >= 200203)
-  #pragma omp parallel for shared(progress,status)
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
+  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
 #endif
-  for (y=0; y < (long) image->rows; y++)
+  for (y=0; y < (ssize_t) image->rows; y++)
   {
     register const PixelPacket
-      *__restrict p;
-
-    register long
-      x;
+      *restrict p;
 
     register PixelPacket
-      *__restrict q;
+      *restrict q;
+
+    register ssize_t
+      x;
 
     /*
       Read another scan line.
@@ -1548,7 +1618,7 @@ MagickExport Image *EnhanceImage(const Image *image,ExceptionInfo *exception)
         status=MagickFalse;
         continue;
       }
-    for (x=0; x < (long) image->columns; x++)
+    for (x=0; x < (ssize_t) image->columns; x++)
     {
       MagickPixelPacket
         aggregate;
@@ -1563,7 +1633,7 @@ MagickExport Image *EnhanceImage(const Image *image,ExceptionInfo *exception)
         pixel;
 
       register const PixelPacket
-        *__restrict r;
+        *restrict r;
 
       /*
         Compute weighted average of target pixel color components.
@@ -1582,10 +1652,11 @@ MagickExport Image *EnhanceImage(const Image *image,ExceptionInfo *exception)
       Enhance(8.0); Enhance(20.0); Enhance(40.0); Enhance(20.0); Enhance(8.0);
       r=p+4*(image->columns+4);
       Enhance(5.0); Enhance(8.0); Enhance(10.0); Enhance(8.0); Enhance(5.0);
-      q->red=(Quantum) ((aggregate.red+(total_weight/2)-1)/total_weight);
-      q->green=(Quantum) ((aggregate.green+(total_weight/2)-1)/total_weight);
-      q->blue=(Quantum) ((aggregate.blue+(total_weight/2)-1)/total_weight);
-      q->opacity=(Quantum) ((aggregate.opacity+(total_weight/2)-1)/
+      SetRedPixelComponent(q,(aggregate.red+(total_weight/2)-1)/total_weight);
+      SetGreenPixelComponent(q,(aggregate.green+(total_weight/2)-1)/
+        total_weight);
+      SetBluePixelComponent(q,(aggregate.blue+(total_weight/2)-1)/total_weight);
+      SetOpacityPixelComponent(q,(aggregate.opacity+(total_weight/2)-1)/
         total_weight);
       p++;
       q++;
@@ -1597,7 +1668,7 @@ MagickExport Image *EnhanceImage(const Image *image,ExceptionInfo *exception)
         MagickBooleanType
           proceed;
 
-#if defined(_OPENMP) && (_OPENMP >= 200203)
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
   #pragma omp critical (MagickCore_EnhanceImage)
 #endif
         proceed=SetImageProgress(image,EnhanceImageTag,progress++,image->rows);
@@ -1647,16 +1718,18 @@ MagickExport MagickBooleanType EqualizeImageChannel(Image *image,
 {
 #define EqualizeImageTag  "Equalize/Image"
 
+  CacheView
+    *image_view;
+
   ExceptionInfo
     *exception;
 
-  long
-    progress,
-    y;
-
   MagickBooleanType
     status;
 
+  MagickOffsetType
+    progress;
+
   MagickPixelPacket
     black,
     *equalize_map,
@@ -1665,11 +1738,11 @@ MagickExport MagickBooleanType EqualizeImageChannel(Image *image,
     *map,
     white;
 
-  register long
+  register ssize_t
     i;
 
-  CacheView
-    *image_view;
+  ssize_t
+    y;
 
   /*
     Allocate and initialize histogram arrays.
@@ -1701,34 +1774,34 @@ MagickExport MagickBooleanType EqualizeImageChannel(Image *image,
   */
   (void) ResetMagickMemory(histogram,0,(MaxMap+1)*sizeof(*histogram));
   exception=(&image->exception);
-  for (y=0; y < (long) image->rows; y++)
+  for (y=0; y < (ssize_t) image->rows; y++)
   {
     register const IndexPacket
-      *__restrict indexes;
+      *restrict indexes;
 
     register const PixelPacket
-      *__restrict p;
+      *restrict p;
 
-    register long
+    register ssize_t
       x;
 
     p=GetVirtualPixels(image,0,y,image->columns,1,exception);
     if (p == (const PixelPacket *) NULL)
       break;
     indexes=GetVirtualIndexQueue(image);
-    for (x=0; x < (long) image->columns; x++)
+    for (x=0; x < (ssize_t) image->columns; x++)
     {
       if ((channel & RedChannel) != 0)
-        histogram[ScaleQuantumToMap(p->red)].red++;
+        histogram[ScaleQuantumToMap(GetRedPixelComponent(p))].red++;
       if ((channel & GreenChannel) != 0)
-        histogram[ScaleQuantumToMap(p->green)].green++;
+        histogram[ScaleQuantumToMap(GetGreenPixelComponent(p))].green++;
       if ((channel & BlueChannel) != 0)
-        histogram[ScaleQuantumToMap(p->blue)].blue++;
+        histogram[ScaleQuantumToMap(GetBluePixelComponent(p))].blue++;
       if ((channel & OpacityChannel) != 0)
-        histogram[ScaleQuantumToMap(p->opacity)].opacity++;
+        histogram[ScaleQuantumToMap(GetOpacityPixelComponent(p))].opacity++;
       if (((channel & IndexChannel) != 0) &&
           (image->colorspace == CMYKColorspace))
-        histogram[ScaleQuantumToMap(indexes[x])].index++;
+        histogram[ScaleQuantumToMap(GetIndexPixelComponent(indexes+x))].index++;
       p++;
     }
   }
@@ -1736,7 +1809,7 @@ MagickExport MagickBooleanType EqualizeImageChannel(Image *image,
     Integrate the histogram to get the equalization map.
   */
   (void) ResetMagickMemory(&intensity,0,sizeof(intensity));
-  for (i=0; i <= (long) MaxMap; i++)
+  for (i=0; i <= (ssize_t) MaxMap; i++)
   {
     if ((channel & RedChannel) != 0)
       intensity.red+=histogram[i].red;
@@ -1754,10 +1827,10 @@ MagickExport MagickBooleanType EqualizeImageChannel(Image *image,
   black=map[0];
   white=map[(int) MaxMap];
   (void) ResetMagickMemory(equalize_map,0,(MaxMap+1)*sizeof(*equalize_map));
-#if defined(_OPENMP) && (_OPENMP >= 200203)
-  #pragma omp parallel for shared(progress,status)
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
+  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
 #endif
-  for (i=0; i <= (long) MaxMap; i++)
+  for (i=0; i <= (ssize_t) MaxMap; i++)
   {
     if (((channel & RedChannel) != 0) && (white.red != black.red))
       equalize_map[i].red=(MagickRealType) ScaleMapToQuantum((MagickRealType)
@@ -1785,23 +1858,23 @@ MagickExport MagickBooleanType EqualizeImageChannel(Image *image,
       /*
         Equalize colormap.
       */
-#if defined(_OPENMP) && (_OPENMP >= 200203)
-  #pragma omp parallel for shared(progress,status)
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
+  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
 #endif
-      for (i=0; i < (long) image->colors; i++)
+      for (i=0; i < (ssize_t) image->colors; i++)
       {
         if (((channel & RedChannel) != 0) && (white.red != black.red))
-          image->colormap[i].red=RoundToQuantum(equalize_map[
+          image->colormap[i].red=ClampToQuantum(equalize_map[
             ScaleQuantumToMap(image->colormap[i].red)].red);
         if (((channel & GreenChannel) != 0) && (white.green != black.green))
-          image->colormap[i].green=RoundToQuantum(equalize_map[
+          image->colormap[i].green=ClampToQuantum(equalize_map[
             ScaleQuantumToMap(image->colormap[i].green)].green);
         if (((channel & BlueChannel) != 0) && (white.blue != black.blue))
-          image->colormap[i].blue=RoundToQuantum(equalize_map[
+          image->colormap[i].blue=ClampToQuantum(equalize_map[
             ScaleQuantumToMap(image->colormap[i].blue)].blue);
         if (((channel & OpacityChannel) != 0) &&
             (white.opacity != black.opacity))
-          image->colormap[i].opacity=RoundToQuantum(equalize_map[
+          image->colormap[i].opacity=ClampToQuantum(equalize_map[
             ScaleQuantumToMap(image->colormap[i].opacity)].opacity);
       }
     }
@@ -1812,19 +1885,19 @@ MagickExport MagickBooleanType EqualizeImageChannel(Image *image,
   progress=0;
   exception=(&image->exception);
   image_view=AcquireCacheView(image);
-#if defined(_OPENMP) && (_OPENMP >= 200203)
-  #pragma omp parallel for shared(progress,status)
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
+  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
 #endif
-  for (y=0; y < (long) image->rows; y++)
+  for (y=0; y < (ssize_t) image->rows; y++)
   {
     register IndexPacket
-      *__restrict indexes;
-
-    register long
-      x;
+      *restrict indexes;
 
     register PixelPacket
-      *__restrict q;
+      *restrict q;
+
+    register ssize_t
+      x;
 
     if (status == MagickFalse)
       continue;
@@ -1835,23 +1908,25 @@ MagickExport MagickBooleanType EqualizeImageChannel(Image *image,
         continue;
       }
     indexes=GetCacheViewAuthenticIndexQueue(image_view);
-    for (x=0; x < (long) image->columns; x++)
+    for (x=0; x < (ssize_t) image->columns; x++)
     {
       if (((channel & RedChannel) != 0) && (white.red != black.red))
-        q->red=RoundToQuantum(equalize_map[ScaleQuantumToMap(q->red)].red);
+        SetRedPixelComponent(q,ClampToQuantum(equalize_map[
+          ScaleQuantumToMap(GetRedPixelComponent(q))].red));
       if (((channel & GreenChannel) != 0) && (white.green != black.green))
-        q->green=RoundToQuantum(equalize_map[ScaleQuantumToMap(
-          q->green)].green);
+        SetGreenPixelComponent(q,ClampToQuantum(equalize_map[
+          ScaleQuantumToMap(GetGreenPixelComponent(q))].green));
       if (((channel & BlueChannel) != 0) && (white.blue != black.blue))
-        q->blue=RoundToQuantum(equalize_map[ScaleQuantumToMap(q->blue)].blue);
+        SetBluePixelComponent(q,ClampToQuantum(equalize_map[
+          ScaleQuantumToMap(GetBluePixelComponent(q))].blue));
       if (((channel & OpacityChannel) != 0) && (white.opacity != black.opacity))
-        q->opacity=RoundToQuantum(equalize_map[ScaleQuantumToMap(
-          q->opacity)].opacity);
+        SetOpacityPixelComponent(q,ClampToQuantum(equalize_map[
+          ScaleQuantumToMap(GetOpacityPixelComponent(q))].opacity));
       if ((((channel & IndexChannel) != 0) &&
           (image->colorspace == CMYKColorspace)) &&
           (white.index != black.index))
-        indexes[x]=RoundToQuantum(equalize_map[ScaleQuantumToMap(
-          indexes[x])].index);
+        SetIndexPixelComponent(indexes+x,ClampToQuantum(equalize_map[
+          ScaleQuantumToMap(GetIndexPixelComponent(indexes+x))].index));
       q++;
     }
     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
@@ -1861,7 +1936,7 @@ MagickExport MagickBooleanType EqualizeImageChannel(Image *image,
         MagickBooleanType
           proceed;
 
-#if defined(_OPENMP) && (_OPENMP >= 200203)
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
   #pragma omp critical (MagickCore_EqualizeImageChannel)
 #endif
         proceed=SetImageProgress(image,EqualizeImageTag,progress++,image->rows);
@@ -1896,7 +1971,7 @@ MagickExport MagickBooleanType EqualizeImageChannel(Image *image,
 %
 %  The format of the GammaImage method is:
 %
-%      MagickBooleanType GammaImage(Image *image,const double gamma)
+%      MagickBooleanType GammaImage(Image *image,const char *level)
 %      MagickBooleanType GammaImageChannel(Image *image,
 %        const ChannelType channel,const double gamma)
 %
@@ -1906,6 +1981,8 @@ MagickExport MagickBooleanType EqualizeImageChannel(Image *image,
 %
 %    o channel: the channel.
 %
+%    o level: the image gamma as a string (e.g. 1.6,1.2,1.0).
+%
 %    o gamma: the image gamma.
 %
 */
@@ -1954,24 +2031,26 @@ MagickExport MagickBooleanType GammaImageChannel(Image *image,
 {
 #define GammaCorrectImageTag  "GammaCorrect/Image"
 
+  CacheView
+    *image_view;
+
   ExceptionInfo
     *exception;
 
-  long
-    progress,
-    y;
-
   MagickBooleanType
     status;
 
+  MagickOffsetType
+    progress;
+
   Quantum
     *gamma_map;
 
-  register long
+  register ssize_t
     i;
 
-  CacheView
-    *image_view;
+  ssize_t
+    y;
 
   /*
     Allocate and initialize gamma maps.
@@ -1988,21 +2067,21 @@ MagickExport MagickBooleanType GammaImageChannel(Image *image,
       image->filename);
   (void) ResetMagickMemory(gamma_map,0,(MaxMap+1)*sizeof(*gamma_map));
   if (gamma != 0.0)
-#if defined(_OPENMP) && (_OPENMP >= 200203)
-  #pragma omp parallel for
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
+  #pragma omp parallel for schedule(dynamic,4)
 #endif
-    for (i=0; i <= (long) MaxMap; i++)
-      gamma_map[i]=RoundToQuantum((MagickRealType) ScaleMapToQuantum((
+    for (i=0; i <= (ssize_t) MaxMap; i++)
+      gamma_map[i]=ClampToQuantum((MagickRealType) ScaleMapToQuantum((
         MagickRealType) (MaxMap*pow((double) i/MaxMap,1.0/gamma))));
   if (image->storage_class == PseudoClass)
     {
       /*
         Gamma-correct colormap.
       */
-#if defined(_OPENMP) && (_OPENMP >= 200203)
-  #pragma omp parallel for shared(progress,status)
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
+  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
 #endif
-      for (i=0; i < (long) image->colors; i++)
+      for (i=0; i < (ssize_t) image->colors; i++)
       {
         if ((channel & RedChannel) != 0)
           image->colormap[i].red=gamma_map[
@@ -2032,19 +2111,19 @@ MagickExport MagickBooleanType GammaImageChannel(Image *image,
   progress=0;
   exception=(&image->exception);
   image_view=AcquireCacheView(image);
-#if defined(_OPENMP) && (_OPENMP >= 200203)
-  #pragma omp parallel for shared(progress,status)
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
+  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
 #endif
-  for (y=0; y < (long) image->rows; y++)
+  for (y=0; y < (ssize_t) image->rows; y++)
   {
     register IndexPacket
-      *__restrict indexes;
-
-    register long
-      x;
+      *restrict indexes;
 
     register PixelPacket
-      *__restrict q;
+      *restrict q;
+
+    register ssize_t
+      x;
 
     if (status == MagickFalse)
       continue;
@@ -2055,37 +2134,45 @@ MagickExport MagickBooleanType GammaImageChannel(Image *image,
         continue;
       }
     indexes=GetCacheViewAuthenticIndexQueue(image_view);
-    for (x=0; x < (long) image->columns; x++)
+    for (x=0; x < (ssize_t) image->columns; x++)
     {
       if (channel == DefaultChannels)
         {
-          q->red=gamma_map[ScaleQuantumToMap(q->red)];
-          q->green=gamma_map[ScaleQuantumToMap(q->green)];
-          q->blue=gamma_map[ScaleQuantumToMap(q->blue)];
+          SetRedPixelComponent(q,gamma_map[ScaleQuantumToMap(
+            GetRedPixelComponent(q))]);
+          SetGreenPixelComponent(q,gamma_map[ScaleQuantumToMap(
+            GetGreenPixelComponent(q))]);
+          SetBluePixelComponent(q,gamma_map[ScaleQuantumToMap(
+            GetBluePixelComponent(q))]);
         }
       else
         {
           if ((channel & RedChannel) != 0)
-            q->red=gamma_map[ScaleQuantumToMap(q->red)];
+            SetRedPixelComponent(q,gamma_map[ScaleQuantumToMap(
+              GetRedPixelComponent(q))]);
           if ((channel & GreenChannel) != 0)
-            q->green=gamma_map[ScaleQuantumToMap(q->green)];
+            SetGreenPixelComponent(q,gamma_map[ScaleQuantumToMap(
+              GetGreenPixelComponent(q))]);
           if ((channel & BlueChannel) != 0)
-            q->blue=gamma_map[ScaleQuantumToMap(q->blue)];
+            SetBluePixelComponent(q,gamma_map[ScaleQuantumToMap(
+              GetBluePixelComponent(q))]);
           if ((channel & OpacityChannel) != 0)
             {
               if (image->matte == MagickFalse)
-                q->opacity=gamma_map[ScaleQuantumToMap(q->opacity)];
+                SetOpacityPixelComponent(q,gamma_map[ScaleQuantumToMap(
+                  GetOpacityPixelComponent(q))]);
               else
-                q->opacity=(Quantum) QuantumRange-gamma_map[
-                  ScaleQuantumToMap((Quantum) (QuantumRange-q->opacity))];
+                SetOpacityPixelComponent(q,(Quantum) QuantumRange-gamma_map[
+                  ScaleQuantumToMap((Quantum) GetAlphaPixelComponent(q))]);
             }
         }
       q++;
     }
     if (((channel & IndexChannel) != 0) &&
         (image->colorspace == CMYKColorspace))
-      for (x=0; x < (long) image->columns; x++)
-        indexes[x]=gamma_map[ScaleQuantumToMap(indexes[x])];
+      for (x=0; x < (ssize_t) image->columns; x++)
+        SetIndexPixelComponent(indexes+x,gamma_map[ScaleQuantumToMap(
+          GetIndexPixelComponent(indexes+x))]);
     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
       status=MagickFalse;
     if (image->progress_monitor != (MagickProgressMonitor) NULL)
@@ -2093,7 +2180,7 @@ MagickExport MagickBooleanType GammaImageChannel(Image *image,
         MagickBooleanType
           proceed;
 
-#if defined(_OPENMP) && (_OPENMP >= 200203)
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
   #pragma omp critical (MagickCore_GammaImageChannel)
 #endif
         proceed=SetImageProgress(image,GammaCorrectImageTag,progress++,
@@ -2168,32 +2255,32 @@ MagickExport MagickBooleanType HaldClutImageChannel(Image *image,
       z;
   } HaldInfo;
 
+  CacheView
+    *hald_view,
+    *image_view;
+
   double
     width;
 
   ExceptionInfo
     *exception;
 
-  long
-    progress,
-    y;
-
   MagickBooleanType
     status;
 
+  MagickOffsetType
+    progress;
+
   MagickPixelPacket
     zero;
 
-  ResampleFilter
-    **resample_filter;
-
   size_t
     cube_size,
     length,
     level;
 
-  CacheView
-    *image_view;
+  ssize_t
+    y;
 
   assert(image != (Image *) NULL);
   assert(image->signature == MagickSignature);
@@ -2217,13 +2304,12 @@ MagickExport MagickBooleanType HaldClutImageChannel(Image *image,
   width=(double) hald_image->columns;
   GetMagickPixelPacket(hald_image,&zero);
   exception=(&image->exception);
-  resample_filter=AcquireResampleFilterThreadSet(hald_image,MagickTrue,
-    exception);
   image_view=AcquireCacheView(image);
-#if defined(_OPENMP) && (_OPENMP >= 200203)
-  #pragma omp parallel for shared(progress,status)
+  hald_view=AcquireCacheView(hald_image);
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
+  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
 #endif
-  for (y=0; y < (long) image->rows; y++)
+  for (y=0; y < (ssize_t) image->rows; y++)
   {
     double
       offset;
@@ -2239,14 +2325,13 @@ MagickExport MagickBooleanType HaldClutImageChannel(Image *image,
       pixel4;
 
     register IndexPacket
-      *__restrict indexes;
-
-    register long
-      id,
-      x;
+      *restrict indexes;
 
     register PixelPacket
-      *__restrict q;
+      *restrict q;
+
+    register ssize_t
+      x;
 
     if (status == MagickFalse)
       continue;
@@ -2256,48 +2341,51 @@ MagickExport MagickBooleanType HaldClutImageChannel(Image *image,
         status=MagickFalse;
         continue;
       }
-    indexes=GetCacheViewAuthenticIndexQueue(image_view);
+    indexes=GetCacheViewAuthenticIndexQueue(hald_view);
     pixel=zero;
     pixel1=zero;
     pixel2=zero;
     pixel3=zero;
     pixel4=zero;
-    id=GetOpenMPThreadId();
-    for (x=0; x < (long) image->columns; x++)
+    for (x=0; x < (ssize_t) image->columns; x++)
     {
-      point.x=QuantumScale*(level-1.0)*q->red;
-      point.y=QuantumScale*(level-1.0)*q->green;
-      point.z=QuantumScale*(level-1.0)*q->blue;
+      point.x=QuantumScale*(level-1.0)*GetRedPixelComponent(q);
+      point.y=QuantumScale*(level-1.0)*GetGreenPixelComponent(q);
+      point.z=QuantumScale*(level-1.0)*GetBluePixelComponent(q);
       offset=point.x+level*floor(point.y)+cube_size*floor(point.z);
       point.x-=floor(point.x);
       point.y-=floor(point.y);
       point.z-=floor(point.z);
-      (void) ResamplePixelColor(resample_filter[id],fmod(offset,width),
-        floor(offset/width),&pixel1);
-      (void) ResamplePixelColor(resample_filter[id],fmod(offset+level,width),
-        floor((offset+level)/width),&pixel2);
+      (void) InterpolateMagickPixelPacket(image,hald_view,
+        UndefinedInterpolatePixel,fmod(offset,width),floor(offset/width),
+        &pixel1,exception);
+      (void) InterpolateMagickPixelPacket(image,hald_view,
+        UndefinedInterpolatePixel,fmod(offset+level,width),floor((offset+level)/
+        width),&pixel2,exception);
       MagickPixelCompositeAreaBlend(&pixel1,pixel1.opacity,&pixel2,
         pixel2.opacity,point.y,&pixel3);
       offset+=cube_size;
-      (void) ResamplePixelColor(resample_filter[id],fmod(offset,width),
-        floor(offset/width),&pixel1);
-      (void) ResamplePixelColor(resample_filter[id],fmod(offset+level,width),
-        floor((offset+level)/width),&pixel2);
+      (void) InterpolateMagickPixelPacket(image,hald_view,
+        UndefinedInterpolatePixel,fmod(offset,width),floor(offset/width),
+        &pixel1,exception);
+      (void) InterpolateMagickPixelPacket(image,hald_view,
+        UndefinedInterpolatePixel,fmod(offset+level,width),floor((offset+level)/
+        width),&pixel2,exception);
       MagickPixelCompositeAreaBlend(&pixel1,pixel1.opacity,&pixel2,
         pixel2.opacity,point.y,&pixel4);
       MagickPixelCompositeAreaBlend(&pixel3,pixel3.opacity,&pixel4,
         pixel4.opacity,point.z,&pixel);
       if ((channel & RedChannel) != 0)
-        q->red=RoundToQuantum(pixel.red);
+        SetRedPixelComponent(q,ClampToQuantum(pixel.red));
       if ((channel & GreenChannel) != 0)
-        q->green=RoundToQuantum(pixel.green);
+        SetGreenPixelComponent(q,ClampToQuantum(pixel.green));
       if ((channel & BlueChannel) != 0)
-        q->blue=RoundToQuantum(pixel.blue);
+        SetBluePixelComponent(q,ClampToQuantum(pixel.blue));
       if (((channel & OpacityChannel) != 0) && (image->matte != MagickFalse))
-        q->opacity=RoundToQuantum(pixel.opacity);
+        SetOpacityPixelComponent(q,ClampToQuantum(pixel.opacity));
       if (((channel & IndexChannel) != 0) &&
           (image->colorspace == CMYKColorspace))
-        indexes[x]=RoundToQuantum(pixel.index);
+        SetIndexPixelComponent(indexes+x,ClampToQuantum(pixel.index));
       q++;
     }
     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
@@ -2307,7 +2395,7 @@ MagickExport MagickBooleanType HaldClutImageChannel(Image *image,
         MagickBooleanType
           proceed;
 
-#if defined(_OPENMP) && (_OPENMP >= 200203)
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
   #pragma omp critical (MagickCore_HaldClutImageChannel)
 #endif
         proceed=SetImageProgress(image,HaldClutImageTag,progress++,image->rows);
@@ -2315,8 +2403,8 @@ MagickExport MagickBooleanType HaldClutImageChannel(Image *image,
           status=MagickFalse;
       }
   }
+  hald_view=DestroyCacheView(hald_view);
   image_view=DestroyCacheView(image_view);
-  resample_filter=DestroyResampleFilterThreadSet(resample_filter);
   return(status);
 }
 \f
@@ -2447,41 +2535,34 @@ MagickExport MagickBooleanType LevelImage(Image *image,const char *levels)
 %             use 1.0 for purely linear stretching of image color values
 %
 */
-
-MagickExport MagickBooleanType LevelizeImage(Image *image,
-  const double black_point,const double white_point,const double gamma)
-{
-  MagickBooleanType
-    status;
-
-  status=LevelizeImageChannel(image,DefaultChannels,black_point,white_point,
-    gamma);
-  return(status);
-}
-
 MagickExport MagickBooleanType LevelImageChannel(Image *image,
   const ChannelType channel,const double black_point,const double white_point,
   const double gamma)
 {
 #define LevelImageTag  "Level/Image"
-#define LevelValue(x) (RoundToQuantum((MagickRealType) QuantumRange* \
-  pow(((double) (x)-black_point)/(white_point-black_point),1.0/gamma)))
+#define LevelQuantum(x) (ClampToQuantum((MagickRealType) QuantumRange* \
+  pow(scale*((double) (x)-black_point),1.0/gamma)))
+
+  CacheView
+    *image_view;
 
   ExceptionInfo
     *exception;
 
-  long
-    progress,
-    y;
-
   MagickBooleanType
     status;
 
-  register long
+  MagickOffsetType
+    progress;
+
+  register double
+    scale;
+
+  register ssize_t
     i;
 
-  CacheView
-    *image_view;
+  ssize_t
+    y;
 
   /*
     Allocate and initialize levels map.
@@ -2490,23 +2571,24 @@ MagickExport MagickBooleanType LevelImageChannel(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(_OPENMP) && (_OPENMP >= 200203)
-  #pragma omp parallel for shared(progress,status)
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
+  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
 #endif
-    for (i=0; i < (long) image->colors; i++)
+    for (i=0; i < (ssize_t) image->colors; i++)
     {
       /*
         Level colormap.
       */
       if ((channel & RedChannel) != 0)
-        image->colormap[i].red=LevelValue(image->colormap[i].red);
+        image->colormap[i].red=LevelQuantum(image->colormap[i].red);
       if ((channel & GreenChannel) != 0)
-        image->colormap[i].green=LevelValue(image->colormap[i].green);
+        image->colormap[i].green=LevelQuantum(image->colormap[i].green);
       if ((channel & BlueChannel) != 0)
-        image->colormap[i].blue=LevelValue(image->colormap[i].blue);
+        image->colormap[i].blue=LevelQuantum(image->colormap[i].blue);
       if ((channel & OpacityChannel) != 0)
-        image->colormap[i].opacity=LevelValue(image->colormap[i].opacity);
+        image->colormap[i].opacity=LevelQuantum(image->colormap[i].opacity);
       }
   /*
     Level image.
@@ -2515,19 +2597,19 @@ MagickExport MagickBooleanType LevelImageChannel(Image *image,
   progress=0;
   exception=(&image->exception);
   image_view=AcquireCacheView(image);
-#if defined(_OPENMP) && (_OPENMP >= 200203)
-  #pragma omp parallel for shared(progress,status)
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
+  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
 #endif
-  for (y=0; y < (long) image->rows; y++)
+  for (y=0; y < (ssize_t) image->rows; y++)
   {
     register IndexPacket
-      *__restrict indexes;
-
-    register long
-      x;
+      *restrict indexes;
 
     register PixelPacket
-      *__restrict q;
+      *restrict q;
+
+    register ssize_t
+      x;
 
     if (status == MagickFalse)
       continue;
@@ -2538,20 +2620,22 @@ MagickExport MagickBooleanType LevelImageChannel(Image *image,
         continue;
       }
     indexes=GetCacheViewAuthenticIndexQueue(image_view);
-    for (x=0; x < (long) image->columns; x++)
+    for (x=0; x < (ssize_t) image->columns; x++)
     {
       if ((channel & RedChannel) != 0)
-        q->red=LevelValue(q->red);
+        SetRedPixelComponent(q,LevelQuantum(GetRedPixelComponent(q)));
       if ((channel & GreenChannel) != 0)
-        q->green=LevelValue(q->green);
+        SetGreenPixelComponent(q,LevelQuantum(GetGreenPixelComponent(q)));
       if ((channel & BlueChannel) != 0)
-        q->blue=LevelValue(q->blue);
+        SetBluePixelComponent(q,LevelQuantum(GetBluePixelComponent(q)));
       if (((channel & OpacityChannel) != 0) &&
           (image->matte == MagickTrue))
-        q->opacity=LevelValue(q->opacity);
+        SetOpacityPixelComponent(q,QuantumRange-LevelQuantum(QuantumRange-
+          q->opacity));
       if (((channel & IndexChannel) != 0) &&
           (image->colorspace == CMYKColorspace))
-        indexes[x]=LevelValue(indexes[x]);
+        SetIndexPixelComponent(indexes+x,LevelQuantum(
+          GetIndexPixelComponent(indexes+x)));
       q++;
     }
     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
@@ -2561,7 +2645,7 @@ MagickExport MagickBooleanType LevelImageChannel(Image *image,
         MagickBooleanType
           proceed;
 
-#if defined(_OPENMP) && (_OPENMP >= 200203)
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
   #pragma omp critical (MagickCore_LevelImageChannel)
 #endif
         proceed=SetImageProgress(image,LevelImageTag,progress++,image->rows);
@@ -2615,30 +2699,44 @@ MagickExport MagickBooleanType LevelImageChannel(Image *image,
 %    o gamma: adjust gamma by this factor before mapping values.
 %
 */
+
+MagickExport MagickBooleanType LevelizeImage(Image *image,
+  const double black_point,const double white_point,const double gamma)
+{
+  MagickBooleanType
+    status;
+
+  status=LevelizeImageChannel(image,DefaultChannels,black_point,white_point,
+    gamma);
+  return(status);
+}
+
 MagickExport MagickBooleanType LevelizeImageChannel(Image *image,
   const ChannelType channel,const double black_point,const double white_point,
   const double gamma)
 {
 #define LevelizeImageTag  "Levelize/Image"
-#define LevelizeValue(x) (RoundToQuantum(((MagickRealType) \
+#define LevelizeValue(x) (ClampToQuantum(((MagickRealType) \
   pow((double)(QuantumScale*(x)),1.0/gamma))*(white_point-black_point)+ \
   black_point))
 
+  CacheView
+    *image_view;
+
   ExceptionInfo
     *exception;
 
-  long
-    progress,
-    y;
-
   MagickBooleanType
     status;
 
-  register long
+  MagickOffsetType
+    progress;
+
+  register ssize_t
     i;
 
-  CacheView
-    *image_view;
+  ssize_t
+    y;
 
   /*
     Allocate and initialize levels map.
@@ -2648,10 +2746,10 @@ MagickExport MagickBooleanType LevelizeImageChannel(Image *image,
   if (image->debug != MagickFalse)
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
   if (image->storage_class == PseudoClass)
-#if defined(_OPENMP) && (_OPENMP >= 200203)
-  #pragma omp parallel for shared(progress,status)
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
+  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
 #endif
-    for (i=0; i < (long) image->colors; i++)
+    for (i=0; i < (ssize_t) image->colors; i++)
     {
       /*
         Level colormap.
@@ -2672,19 +2770,19 @@ MagickExport MagickBooleanType LevelizeImageChannel(Image *image,
   progress=0;
   exception=(&image->exception);
   image_view=AcquireCacheView(image);
-#if defined(_OPENMP) && (_OPENMP >= 200203)
-  #pragma omp parallel for shared(progress,status)
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
+  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
 #endif
-  for (y=0; y < (long) image->rows; y++)
+  for (y=0; y < (ssize_t) image->rows; y++)
   {
     register IndexPacket
-      *__restrict indexes;
-
-    register long
-      x;
+      *restrict indexes;
 
     register PixelPacket
-      *__restrict q;
+      *restrict q;
+
+    register ssize_t
+      x;
 
     if (status == MagickFalse)
       continue;
@@ -2695,20 +2793,21 @@ MagickExport MagickBooleanType LevelizeImageChannel(Image *image,
         continue;
       }
     indexes=GetCacheViewAuthenticIndexQueue(image_view);
-    for (x=0; x < (long) image->columns; x++)
+    for (x=0; x < (ssize_t) image->columns; x++)
     {
       if ((channel & RedChannel) != 0)
-        q->red=LevelizeValue(q->red);
+        SetRedPixelComponent(q,LevelizeValue(GetRedPixelComponent(q)));
       if ((channel & GreenChannel) != 0)
-        q->green=LevelizeValue(q->green);
+        SetGreenPixelComponent(q,LevelizeValue(GetGreenPixelComponent(q)));
       if ((channel & BlueChannel) != 0)
-        q->blue=LevelizeValue(q->blue);
+        SetBluePixelComponent(q,LevelizeValue(GetBluePixelComponent(q)));
       if (((channel & OpacityChannel) != 0) &&
           (image->matte == MagickTrue))
-        q->opacity=LevelizeValue(q->opacity);
+        SetOpacityPixelComponent(q,LevelizeValue(GetOpacityPixelComponent(q)));
       if (((channel & IndexChannel) != 0) &&
           (image->colorspace == CMYKColorspace))
-        indexes[x]=LevelizeValue(indexes[x]);
+        SetIndexPixelComponent(indexes+x,LevelizeValue(
+          GetIndexPixelComponent(indexes+x)));
       q++;
     }
     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
@@ -2718,7 +2817,7 @@ MagickExport MagickBooleanType LevelizeImageChannel(Image *image,
         MagickBooleanType
           proceed;
 
-#if defined(_OPENMP) && (_OPENMP >= 200203)
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
   #pragma omp critical (MagickCore_LevelizeImageChannel)
 #endif
         proceed=SetImageProgress(image,LevelizeImageTag,progress++,image->rows);
@@ -2726,6 +2825,7 @@ MagickExport MagickBooleanType LevelizeImageChannel(Image *image,
           status=MagickFalse;
       }
   }
+  image_view=DestroyCacheView(image_view);
   return(status);
 }
 \f
@@ -2743,7 +2843,7 @@ MagickExport MagickBooleanType LevelizeImageChannel(Image *image,
 %  LevelImageColor() maps the given color to "black" and "white" values,
 %  linearly spreading out the colors, and level values on a channel by channel
 %  bases, as per LevelImage().  The given colors allows you to specify
-%  different level ranges for each of the color channels seperatally.
+%  different level ranges for each of the color channels separately.
 %
 %  If the boolean 'invert' is set true the image values will modifyed in the
 %  reverse direction. That is any existing "black" and "white" colors in the
@@ -2880,11 +2980,6 @@ MagickExport MagickBooleanType LinearStretchImage(Image *image,
   ExceptionInfo
     *exception;
 
-  long
-    black,
-    white,
-    y;
-
   MagickBooleanType
     status;
 
@@ -2892,8 +2987,10 @@ MagickExport MagickBooleanType LinearStretchImage(Image *image,
     *histogram,
     intensity;
 
-  MagickSizeType
-    number_pixels;
+  ssize_t
+    black,
+    white,
+    y;
 
   /*
     Allocate histogram and linear map.
@@ -2910,18 +3007,18 @@ MagickExport MagickBooleanType LinearStretchImage(Image *image,
   */
   (void) ResetMagickMemory(histogram,0,(MaxMap+1)*sizeof(*histogram));
   exception=(&image->exception);
-  for (y=0; y < (long) image->rows; y++)
+  for (y=0; y < (ssize_t) image->rows; y++)
   {
     register const PixelPacket
-      *__restrict p;
+      *restrict p;
 
-    register long
+    register ssize_t
       x;
 
     p=GetVirtualPixels(image,0,y,image->columns,1,exception);
     if (p == (const PixelPacket *) NULL)
       break;
-    for (x=(long) image->columns-1; x >= 0; x--)
+    for (x=(ssize_t) image->columns-1; x >= 0; x--)
     {
       histogram[ScaleQuantumToMap(PixelIntensityToQuantum(p))]++;
       p++;
@@ -2930,16 +3027,15 @@ MagickExport MagickBooleanType LinearStretchImage(Image *image,
   /*
     Find the histogram boundaries by locating the black and white point levels.
   */
-  number_pixels=(MagickSizeType) image->columns*image->rows;
   intensity=0.0;
-  for (black=0; black < (long) MaxMap; black++)
+  for (black=0; black < (ssize_t) MaxMap; black++)
   {
     intensity+=histogram[black];
     if (intensity >= black_point)
       break;
   }
   intensity=0.0;
-  for (white=(long) MaxMap; white != 0; white--)
+  for (white=(ssize_t) MaxMap; white != 0; white--)
   {
     intensity+=histogram[white];
     if (intensity >= white_point)
@@ -3061,6 +3157,9 @@ MagickExport MagickBooleanType ModulateImage(Image *image,const char *modulate)
 {
 #define ModulateImageTag  "Modulate/Image"
 
+  CacheView
+    *image_view;
+
   ColorspaceType
     colorspace;
 
@@ -3078,24 +3177,23 @@ MagickExport MagickBooleanType ModulateImage(Image *image,const char *modulate)
   GeometryInfo
     geometry_info;
 
-  long
-    progress,
-    y;
-
   MagickBooleanType
     status;
 
+  MagickOffsetType
+    progress;
+
   MagickStatusType
     flags;
 
-  register long
+  register ssize_t
     i;
 
-  CacheView
-    *image_view;
+  ssize_t
+    y;
 
   /*
-    Initialize gamma table.
+    Initialize modulate table.
   */
   assert(image != (Image *) NULL);
   assert(image->signature == MagickSignature);
@@ -3114,17 +3212,17 @@ MagickExport MagickBooleanType ModulateImage(Image *image,const char *modulate)
   colorspace=UndefinedColorspace;
   artifact=GetImageArtifact(image,"modulate:colorspace");
   if (artifact != (const char *) NULL)
-    colorspace=(ColorspaceType) ParseMagickOption(MagickColorspaceOptions,
+    colorspace=(ColorspaceType) ParseCommandOption(MagickColorspaceOptions,
       MagickFalse,artifact);
   if (image->storage_class == PseudoClass)
     {
       /*
         Modulate colormap.
       */
-#if defined(_OPENMP) && (_OPENMP >= 200203)
-  #pragma omp parallel for shared(progress,status)
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
+  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
 #endif
-      for (i=0; i < (long) image->colors; i++)
+      for (i=0; i < (ssize_t) image->colors; i++)
         switch (colorspace)
         {
           case HSBColorspace:
@@ -3158,16 +3256,21 @@ MagickExport MagickBooleanType ModulateImage(Image *image,const char *modulate)
   progress=0;
   exception=(&image->exception);
   image_view=AcquireCacheView(image);
-#if defined(_OPENMP) && (_OPENMP >= 200203)
-  #pragma omp parallel for shared(progress,status)
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
+  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
 #endif
-  for (y=0; y < (long) image->rows; y++)
+  for (y=0; y < (ssize_t) image->rows; y++)
   {
-    register long
-      x;
+    Quantum
+      blue,
+      green,
+      red;
 
     register PixelPacket
-      *__restrict q;
+      *restrict q;
+
+    register ssize_t
+      x;
 
     if (status == MagickFalse)
       continue;
@@ -3177,30 +3280,36 @@ MagickExport MagickBooleanType ModulateImage(Image *image,const char *modulate)
         status=MagickFalse;
         continue;
       }
-    for (x=0; x < (long) image->columns; x++)
+    for (x=0; x < (ssize_t) image->columns; x++)
     {
+      red=GetRedPixelComponent(q);
+      green=GetGreenPixelComponent(q);
+      blue=GetBluePixelComponent(q);
       switch (colorspace)
       {
         case HSBColorspace:
         {
           ModulateHSB(percent_hue,percent_saturation,percent_brightness,
-            &q->red,&q->green,&q->blue);
+            &red,&green,&blue);
           break;
         }
         case HSLColorspace:
         default:
         {
           ModulateHSL(percent_hue,percent_saturation,percent_brightness,
-            &q->red,&q->green,&q->blue);
+            &red,&green,&blue);
           break;
         }
         case HWBColorspace:
         {
           ModulateHWB(percent_hue,percent_saturation,percent_brightness,
-            &q->red,&q->green,&q->blue);
+            &red,&green,&blue);
           break;
         }
       }
+      SetRedPixelComponent(q,red);
+      SetGreenPixelComponent(q,green);
+      SetBluePixelComponent(q,blue);
       q++;
     }
     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
@@ -3210,7 +3319,7 @@ MagickExport MagickBooleanType ModulateImage(Image *image,const char *modulate)
         MagickBooleanType
           proceed;
 
-#if defined(_OPENMP) && (_OPENMP >= 200203)
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
   #pragma omp critical (MagickCore_ModulateImage)
 #endif
         proceed=SetImageProgress(image,ModulateImageTag,progress++,image->rows);
@@ -3268,21 +3377,23 @@ MagickExport MagickBooleanType NegateImageChannel(Image *image,
 {
 #define NegateImageTag  "Negate/Image"
 
+  CacheView
+    *image_view;
+
   ExceptionInfo
     *exception;
 
-  long
-    progress,
-    y;
-
   MagickBooleanType
     status;
 
-  register long
+  MagickOffsetType
+    progress;
+
+  register ssize_t
     i;
 
-  CacheView
-    *image_view;
+  ssize_t
+    y;
 
   assert(image != (Image *) NULL);
   assert(image->signature == MagickSignature);
@@ -3293,10 +3404,10 @@ MagickExport MagickBooleanType NegateImageChannel(Image *image,
       /*
         Negate colormap.
       */
-#if defined(_OPENMP) && (_OPENMP >= 200203)
-  #pragma omp parallel for shared(progress,status)
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
+  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
 #endif
-      for (i=0; i < (long) image->colors; i++)
+      for (i=0; i < (ssize_t) image->colors; i++)
       {
         if (grayscale != MagickFalse)
           if ((image->colormap[i].red != image->colormap[i].green) ||
@@ -3322,22 +3433,22 @@ MagickExport MagickBooleanType NegateImageChannel(Image *image,
   image_view=AcquireCacheView(image);
   if (grayscale != MagickFalse)
     {
-#if defined(_OPENMP) && (_OPENMP >= 200203)
-  #pragma omp parallel for shared(progress,status)
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
+  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
 #endif
-      for (y=0; y < (long) image->rows; y++)
+      for (y=0; y < (ssize_t) image->rows; y++)
       {
         MagickBooleanType
           sync;
 
         register IndexPacket
-          *__restrict indexes;
-
-        register long
-          x;
+          *restrict indexes;
 
         register PixelPacket
-          *__restrict q;
+          *restrict q;
+
+        register ssize_t
+          x;
 
         if (status == MagickFalse)
           continue;
@@ -3349,24 +3460,27 @@ MagickExport MagickBooleanType NegateImageChannel(Image *image,
             continue;
           }
         indexes=GetCacheViewAuthenticIndexQueue(image_view);
-        for (x=0; x < (long) image->columns; x++)
+        for (x=0; x < (ssize_t) image->columns; x++)
         {
-          if ((q->red != q->green) || (q->green != q->blue))
+          if ((GetRedPixelComponent(q) != GetGreenPixelComponent(q)) ||
+              (GetGreenPixelComponent(q) != GetBluePixelComponent(q)))
             {
               q++;
               continue;
             }
           if ((channel & RedChannel) != 0)
-            q->red=(Quantum) QuantumRange-q->red;
+            SetRedPixelComponent(q,QuantumRange-GetRedPixelComponent(q));
           if ((channel & GreenChannel) != 0)
-            q->green=(Quantum) QuantumRange-q->green;
+            SetGreenPixelComponent(q,QuantumRange-GetGreenPixelComponent(q));
           if ((channel & BlueChannel) != 0)
-            q->blue=(Quantum) QuantumRange-q->blue;
+            SetBluePixelComponent(q,QuantumRange-GetBluePixelComponent(q));
           if ((channel & OpacityChannel) != 0)
-            q->opacity=(Quantum) QuantumRange-q->opacity;
+            SetOpacityPixelComponent(q,QuantumRange-
+              GetOpacityPixelComponent(q));
           if (((channel & IndexChannel) != 0) &&
               (image->colorspace == CMYKColorspace))
-            indexes[x]=(IndexPacket) QuantumRange-indexes[x];
+            SetIndexPixelComponent(indexes+x,QuantumRange-
+              GetIndexPixelComponent(indexes+x));
           q++;
         }
         sync=SyncCacheViewAuthenticPixels(image_view,exception);
@@ -3377,7 +3491,7 @@ MagickExport MagickBooleanType NegateImageChannel(Image *image,
             MagickBooleanType
               proceed;
 
-#if defined(_OPENMP) && (_OPENMP >= 200203)
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
   #pragma omp critical (MagickCore_NegateImageChannel)
 #endif
             proceed=SetImageProgress(image,NegateImageTag,progress++,
@@ -3392,19 +3506,19 @@ MagickExport MagickBooleanType NegateImageChannel(Image *image,
   /*
     Negate image.
   */
-#if defined(_OPENMP) && (_OPENMP >= 200203)
-  #pragma omp parallel for shared(progress,status)
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
+  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
 #endif
-  for (y=0; y < (long) image->rows; y++)
+  for (y=0; y < (ssize_t) image->rows; y++)
   {
     register IndexPacket
-      *__restrict indexes;
-
-    register long
-      x;
+      *restrict indexes;
 
     register PixelPacket
-      *__restrict q;
+      *restrict q;
+
+    register ssize_t
+      x;
 
     if (status == MagickFalse)
       continue;
@@ -3415,19 +3529,20 @@ MagickExport MagickBooleanType NegateImageChannel(Image *image,
         continue;
       }
     indexes=GetCacheViewAuthenticIndexQueue(image_view);
-    for (x=0; x < (long) image->columns; x++)
+    for (x=0; x < (ssize_t) image->columns; x++)
     {
       if ((channel & RedChannel) != 0)
-        q->red=(Quantum) QuantumRange-q->red;
+        SetRedPixelComponent(q,QuantumRange-GetRedPixelComponent(q));
       if ((channel & GreenChannel) != 0)
-        q->green=(Quantum) QuantumRange-q->green;
+        SetGreenPixelComponent(q,QuantumRange-GetGreenPixelComponent(q));
       if ((channel & BlueChannel) != 0)
-        q->blue=(Quantum) QuantumRange-q->blue;
+        SetBluePixelComponent(q,QuantumRange-GetBluePixelComponent(q));
       if ((channel & OpacityChannel) != 0)
-        q->opacity=(Quantum) QuantumRange-q->opacity;
+        SetOpacityPixelComponent(q,QuantumRange-GetOpacityPixelComponent(q));
       if (((channel & IndexChannel) != 0) &&
           (image->colorspace == CMYKColorspace))
-        indexes[x]=(IndexPacket) QuantumRange-indexes[x];
+        SetIndexPixelComponent(indexes+x,QuantumRange-
+          GetIndexPixelComponent(indexes+x));
       q++;
     }
     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
@@ -3437,7 +3552,7 @@ MagickExport MagickBooleanType NegateImageChannel(Image *image,
         MagickBooleanType
           proceed;
 
-#if defined(_OPENMP) && (_OPENMP >= 200203)
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
   #pragma omp critical (MagickCore_NegateImageChannel)
 #endif
         proceed=SetImageProgress(image,NegateImageTag,progress++,image->rows);
@@ -3494,8 +3609,8 @@ MagickExport MagickBooleanType NormalizeImageChannel(Image *image,
     black_point,
     white_point;
 
-  black_point=(double) image->columns*image->rows*0.02;
-  white_point=(double) image->columns*image->rows*0.99;
+  black_point=(double) image->columns*image->rows*0.0015;
+  white_point=(double) image->columns*image->rows*0.9995;
   return(ContrastStretchImageChannel(image,channel,black_point,white_point));
 }
 \f
@@ -3535,9 +3650,10 @@ MagickExport MagickBooleanType NormalizeImageChannel(Image *image,
 %
 %    o sharpen: Increase or decrease image contrast.
 %
-%    o contrast: control the "shoulder" of the contast curve.
+%    o alpha: strength of the contrast, the larger the number the more
+%      'threshold-like' it becomes.
 %
-%    o midpoint: control the "toe" of the contast curve.
+%    o beta: midpoint of the function as a color value 0 to QuantumRange.
 %
 */
 
@@ -3569,24 +3685,26 @@ MagickExport MagickBooleanType SigmoidalContrastImageChannel(Image *image,
 {
 #define SigmoidalContrastImageTag  "SigmoidalContrast/Image"
 
+  CacheView
+    *image_view;
+
   ExceptionInfo
     *exception;
 
-  long
-    progress,
-    y;
-
   MagickBooleanType
     status;
 
+  MagickOffsetType
+    progress;
+
   MagickRealType
     *sigmoidal_map;
 
-  register long
+  register ssize_t
     i;
 
-  CacheView
-    *image_view;
+  ssize_t
+    y;
 
   /*
     Allocate and initialize sigmoidal maps.
@@ -3601,10 +3719,10 @@ MagickExport MagickBooleanType SigmoidalContrastImageChannel(Image *image,
     ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
       image->filename);
   (void) ResetMagickMemory(sigmoidal_map,0,(MaxMap+1)*sizeof(*sigmoidal_map));
-#if defined(_OPENMP) && (_OPENMP >= 200203)
-  #pragma omp parallel for shared(progress,status)
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
+  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
 #endif
-  for (i=0; i <= (long) MaxMap; i++)
+  for (i=0; i <= (ssize_t) MaxMap; i++)
   {
     if (sharpen != MagickFalse)
       {
@@ -3631,22 +3749,22 @@ MagickExport MagickBooleanType SigmoidalContrastImageChannel(Image *image,
       /*
         Sigmoidal-contrast enhance colormap.
       */
-#if defined(_OPENMP) && (_OPENMP >= 200203)
-  #pragma omp parallel for shared(progress,status)
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
+  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
 #endif
-      for (i=0; i < (long) image->colors; i++)
+      for (i=0; i < (ssize_t) image->colors; i++)
       {
         if ((channel & RedChannel) != 0)
-          image->colormap[i].red=RoundToQuantum(sigmoidal_map[
+          image->colormap[i].red=ClampToQuantum(sigmoidal_map[
             ScaleQuantumToMap(image->colormap[i].red)]);
         if ((channel & GreenChannel) != 0)
-          image->colormap[i].green=RoundToQuantum(sigmoidal_map[
+          image->colormap[i].green=ClampToQuantum(sigmoidal_map[
             ScaleQuantumToMap(image->colormap[i].green)]);
         if ((channel & BlueChannel) != 0)
-          image->colormap[i].blue=RoundToQuantum(sigmoidal_map[
+          image->colormap[i].blue=ClampToQuantum(sigmoidal_map[
             ScaleQuantumToMap(image->colormap[i].blue)]);
         if ((channel & OpacityChannel) != 0)
-          image->colormap[i].opacity=RoundToQuantum(sigmoidal_map[
+          image->colormap[i].opacity=ClampToQuantum(sigmoidal_map[
             ScaleQuantumToMap(image->colormap[i].opacity)]);
       }
     }
@@ -3657,19 +3775,19 @@ MagickExport MagickBooleanType SigmoidalContrastImageChannel(Image *image,
   progress=0;
   exception=(&image->exception);
   image_view=AcquireCacheView(image);
-#if defined(_OPENMP) && (_OPENMP >= 200203)
-  #pragma omp parallel for shared(progress,status)
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
+  #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
 #endif
-  for (y=0; y < (long) image->rows; y++)
+  for (y=0; y < (ssize_t) image->rows; y++)
   {
     register IndexPacket
-      *__restrict indexes;
-
-    register long
-      x;
+      *restrict indexes;
 
     register PixelPacket
-      *__restrict q;
+      *restrict q;
+
+    register ssize_t
+      x;
 
     if (status == MagickFalse)
       continue;
@@ -3680,20 +3798,24 @@ MagickExport MagickBooleanType SigmoidalContrastImageChannel(Image *image,
         continue;
       }
     indexes=GetCacheViewAuthenticIndexQueue(image_view);
-    for (x=0; x < (long) image->columns; x++)
+    for (x=0; x < (ssize_t) image->columns; x++)
     {
       if ((channel & RedChannel) != 0)
-        q->red=RoundToQuantum(sigmoidal_map[ScaleQuantumToMap(q->red)]);
+        SetRedPixelComponent(q,ClampToQuantum(sigmoidal_map[ScaleQuantumToMap(
+          GetRedPixelComponent(q))]));
       if ((channel & GreenChannel) != 0)
-        q->green=RoundToQuantum(sigmoidal_map[ScaleQuantumToMap(q->green)]);
+        SetGreenPixelComponent(q,ClampToQuantum(sigmoidal_map[ScaleQuantumToMap(
+          GetGreenPixelComponent(q))]));
       if ((channel & BlueChannel) != 0)
-        q->blue=RoundToQuantum(sigmoidal_map[ScaleQuantumToMap(q->blue)]);
+        SetBluePixelComponent(q,ClampToQuantum(sigmoidal_map[ScaleQuantumToMap(
+          GetBluePixelComponent(q))]));
       if ((channel & OpacityChannel) != 0)
-        q->opacity=RoundToQuantum(sigmoidal_map[ScaleQuantumToMap(q->opacity)]);
+        SetOpacityPixelComponent(q,ClampToQuantum(sigmoidal_map[
+          ScaleQuantumToMap(GetOpacityPixelComponent(q))]));
       if (((channel & IndexChannel) != 0) &&
           (image->colorspace == CMYKColorspace))
-        indexes[x]=(IndexPacket) RoundToQuantum(sigmoidal_map[
-          ScaleQuantumToMap(indexes[x])]);
+        SetIndexPixelComponent(indexes+x,ClampToQuantum(sigmoidal_map[
+          ScaleQuantumToMap(GetIndexPixelComponent(indexes+x))]));
       q++;
     }
     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
@@ -3703,7 +3825,7 @@ MagickExport MagickBooleanType SigmoidalContrastImageChannel(Image *image,
         MagickBooleanType
           proceed;
 
-#if defined(_OPENMP) && (_OPENMP >= 200203)
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
   #pragma omp critical (MagickCore_SigmoidalContrastImageChannel)
 #endif
         proceed=SetImageProgress(image,SigmoidalContrastImageTag,progress++,