From a6400b1cfc3594644a0f02f3d77d920092f078ee Mon Sep 17 00:00:00 2001
From: cristy <urban-warrior@git.imagemagick.org>
Date: Fri, 15 Mar 2013 12:20:18 +0000
Subject: [PATCH]

---
 MagickCore/annotate.c  |  2 ++
 MagickCore/cache.c     |  3 +++
 MagickCore/composite.c |  2 ++
 MagickCore/decorate.c  |  3 +++
 MagickCore/distort.c   |  3 +++
 MagickCore/draw.c      |  4 ++++
 MagickCore/enhance.c   | 12 ++++++++----
 MagickCore/fx.c        | 12 ++++++++++++
 MagickCore/image.c     |  3 +++
 MagickCore/paint.c     |  5 +++++
 MagickCore/threshold.c |  4 ++--
 11 files changed, 47 insertions(+), 6 deletions(-)

diff --git a/MagickCore/annotate.c b/MagickCore/annotate.c
index 6ab5204be..97ebf05e4 100644
--- a/MagickCore/annotate.c
+++ b/MagickCore/annotate.c
@@ -290,6 +290,8 @@ MagickExport MagickBooleanType AnnotateImage(Image *image,
     }
   if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
     return(MagickFalse);
+  if (IsGrayColorspace(image->colorspace) != MagickFalse)
+    (void) TransformImageColorspace(image,sRGBColorspace,exception);
   status=MagickTrue;
   for (i=0; textlist[i] != (char *) NULL; i++)
   {
diff --git a/MagickCore/cache.c b/MagickCore/cache.c
index dd55b72cc..1f2df2f08 100644
--- a/MagickCore/cache.c
+++ b/MagickCore/cache.c
@@ -4829,6 +4829,9 @@ MagickPrivate VirtualPixelMethod SetPixelCacheVirtualMethod(Image *image,
         if ((image->background_color.alpha_trait == BlendPixelTrait) &&
             (image->alpha_trait != BlendPixelTrait))
           (void) SetCacheAlphaChannel(image,OpaqueAlpha,exception);
+        if ((IsPixelInfoGray(&image->background_color) == MagickFalse) &&
+            (IsGrayColorspace(image->colorspace) != MagickFalse))
+          (void) TransformImageColorspace(image,sRGBColorspace,exception);
         break;
       }
       case TransparentVirtualPixelMethod:
diff --git a/MagickCore/composite.c b/MagickCore/composite.c
index 7183a0f69..d1b49b2df 100644
--- a/MagickCore/composite.c
+++ b/MagickCore/composite.c
@@ -580,6 +580,8 @@ MagickExport MagickBooleanType CompositeImage(Image *image,
   composite_image=CloneImage(composite,0,0,MagickTrue,exception);
   if (composite_image == (const Image *) NULL)
     return(MagickFalse);
+  if (IsGrayColorspace(image->colorspace) != MagickFalse)
+    (void) SetImageColorspace(image,sRGBColorspace,exception);
   (void) SetImageColorspace(composite_image,image->colorspace,exception);
   if ((image->alpha_trait == BlendPixelTrait) &&
       (composite_image->alpha_trait != BlendPixelTrait))
diff --git a/MagickCore/decorate.c b/MagickCore/decorate.c
index 3bc9d8294..884a74228 100644
--- a/MagickCore/decorate.c
+++ b/MagickCore/decorate.c
@@ -230,6 +230,9 @@ MagickExport Image *FrameImage(const Image *image,const FrameInfo *frame_info,
       frame_image=DestroyImage(frame_image);
       return((Image *) NULL);
     }
+  if ((IsPixelInfoGray(&frame_image->border_color) == MagickFalse) &&
+      (IsGrayColorspace(frame_image->colorspace) != MagickFalse))
+    (void) SetImageColorspace(frame_image,sRGBColorspace,exception);
   if ((frame_image->matte_color.alpha_trait == BlendPixelTrait) &&
       (frame_image->alpha_trait != BlendPixelTrait))
     (void) SetImageAlpha(frame_image,OpaqueAlpha,exception);
diff --git a/MagickCore/distort.c b/MagickCore/distort.c
index 58f7a2248..08f6a02c9 100644
--- a/MagickCore/distort.c
+++ b/MagickCore/distort.c
@@ -2290,6 +2290,9 @@ MagickExport Image *DistortImage(const Image *image,DistortImageMethod method,
       distort_image=DestroyImage(distort_image);
       return((Image *) NULL);
     }
+  if ((IsPixelInfoGray(&distort_image->background_color) == MagickFalse) &&
+      (IsGrayColorspace(distort_image->colorspace) != MagickFalse))
+    (void) TransformImageColorspace(distort_image,sRGBColorspace,exception);
   if (distort_image->background_color.alpha_trait == BlendPixelTrait)
     distort_image->alpha_trait=BlendPixelTrait;
   distort_image->page.x=geometry.x;
diff --git a/MagickCore/draw.c b/MagickCore/draw.c
index 0a9f976a3..b43cb3458 100644
--- a/MagickCore/draw.c
+++ b/MagickCore/draw.c
@@ -4145,6 +4145,10 @@ MagickExport MagickBooleanType DrawPrimitive(Image *image,
         draw_info->affine.rx,draw_info->affine.ry,draw_info->affine.sy,
         draw_info->affine.tx,draw_info->affine.ty);
     }
+  if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
+      ((IsPixelGray(&draw_info->fill) == MagickFalse) ||
+       (IsPixelGray(&draw_info->stroke) == MagickFalse)))
+    (void) TransformImageColorspace(image,sRGBColorspace,exception);
   status=MagickTrue;
   x=(ssize_t) ceil(primitive_info->point.x-0.5);
   y=(ssize_t) ceil(primitive_info->point.y-0.5);
diff --git a/MagickCore/enhance.c b/MagickCore/enhance.c
index 262c66bd5..d8c9d1d90 100644
--- a/MagickCore/enhance.c
+++ b/MagickCore/enhance.c
@@ -324,6 +324,8 @@ MagickExport MagickBooleanType ClutImage(Image *image,const Image *clut_image,
   assert(clut_image->signature == MagickSignature);
   if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
     return(MagickFalse);
+  if (IsGrayColorspace(image->colorspace) != MagickFalse)
+    (void) TransformImageColorspace(image,sRGBColorspace,exception);
   clut_map=(PixelInfo *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*clut_map));
   if (clut_map == (PixelInfo *) NULL)
     ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
@@ -1952,6 +1954,8 @@ MagickExport MagickBooleanType HaldClutImage(Image *image,
   assert(hald_image->signature == MagickSignature);
   if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
     return(MagickFalse);
+  if (IsGrayColorspace(image->colorspace) != MagickFalse)
+    (void) TransformImageColorspace(image,sRGBColorspace,exception);
   if (image->alpha_trait != BlendPixelTrait)
     (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
   /*
@@ -2304,6 +2308,8 @@ MagickExport MagickBooleanType LevelizeImage(Image *image,
   assert(image->signature == MagickSignature);
   if (image->debug != MagickFalse)
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
+  if (IsGrayColorspace(image->colorspace) != MagickFalse)
+    (void) SetImageColorspace(image,sRGBColorspace,exception);
   if (image->storage_class == PseudoClass)
     for (i=0; i < (ssize_t) image->colors; i++)
     {
@@ -2311,14 +2317,12 @@ MagickExport MagickBooleanType LevelizeImage(Image *image,
         Level colormap.
       */
       if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
-        image->colormap[i].red=(double) LevelizeValue(
-          image->colormap[i].red);
+        image->colormap[i].red=(double) LevelizeValue(image->colormap[i].red);
       if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
         image->colormap[i].green=(double) LevelizeValue(
           image->colormap[i].green);
       if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
-        image->colormap[i].blue=(double) LevelizeValue(
-          image->colormap[i].blue);
+        image->colormap[i].blue=(double) LevelizeValue(image->colormap[i].blue);
       if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
         image->colormap[i].alpha=(double) LevelizeValue(
           image->colormap[i].alpha);
diff --git a/MagickCore/fx.c b/MagickCore/fx.c
index eca389a17..0412cf85f 100644
--- a/MagickCore/fx.c
+++ b/MagickCore/fx.c
@@ -306,6 +306,8 @@ MagickExport Image *AddNoiseImage(const Image *image,const NoiseType noise_type,
       noise_image=DestroyImage(noise_image);
       return((Image *) NULL);
     }
+  if (IsGrayColorspace(image->colorspace) != MagickFalse)
+    (void) TransformImageColorspace(noise_image,sRGBColorspace,exception);
   /*
     Add noise in each row.
   */
@@ -698,6 +700,9 @@ MagickExport Image *ColorizeImage(const Image *image,const char *blend,
       colorize_image=DestroyImage(colorize_image);
       return((Image *) NULL);
     }
+  if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
+      (IsPixelGray(&colorize) != MagickFalse))
+    (void) SetImageColorspace(colorize_image,sRGBColorspace,exception);
   if ((colorize_image->alpha_trait != BlendPixelTrait) &&
       (colorize->alpha_trait == BlendPixelTrait))
     (void) SetImageAlpha(colorize_image,OpaqueAlpha,exception);
@@ -4260,6 +4265,8 @@ MagickExport Image *ShadowImage(const Image *image,const double alpha,
   clone_image=CloneImage(image,0,0,MagickTrue,exception);
   if (clone_image == (Image *) NULL)
     return((Image *) NULL);
+  if (IsGrayColorspace(image->colorspace) != MagickFalse)
+    (void) TransformImageColorspace(clone_image,sRGBColorspace,exception);
   (void) SetImageVirtualPixelMethod(clone_image,TransparentVirtualPixelMethod,
     exception);
   border_info.width=(size_t) floor(2.0*sigma+0.5);
@@ -4553,6 +4560,8 @@ MagickExport MagickBooleanType SolarizeImage(Image *image,
   assert(image->signature == MagickSignature);
   if (image->debug != MagickFalse)
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
+  if (IsGrayColorspace(image->colorspace) != MagickFalse)
+    (void) TransformImageColorspace(image,sRGBColorspace,exception);
   if (image->storage_class == PseudoClass)
     {
       register ssize_t
@@ -5237,6 +5246,9 @@ MagickExport Image *TintImage(const Image *image,const char *blend,
       tint_image=DestroyImage(tint_image);
       return((Image *) NULL);
     }
+  if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
+      (IsPixelGray(&tint) == MagickFalse))
+    (void) SetImageColorspace(tint_image,sRGBColorspace,exception);
   if (blend == (const char *) NULL)
     return(tint_image);
   /*
diff --git a/MagickCore/image.c b/MagickCore/image.c
index e5cbb0805..55974729b 100644
--- a/MagickCore/image.c
+++ b/MagickCore/image.c
@@ -2363,6 +2363,9 @@ MagickExport MagickBooleanType SetImageBackgroundColor(Image *image,
   assert(image->signature == MagickSignature);
   if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
     return(MagickFalse);
+  if ((IsPixelInfoGray(&image->background_color) == MagickFalse) &&
+      (IsGrayColorspace(image->colorspace) != MagickFalse))
+    (void) TransformImageColorspace(image,sRGBColorspace,exception);
   if ((image->background_color.alpha_trait == BlendPixelTrait) &&
       (image->alpha_trait != BlendPixelTrait))
     (void) SetImageAlpha(image,OpaqueAlpha,exception);
diff --git a/MagickCore/paint.c b/MagickCore/paint.c
index d22a7402e..e25342ee4 100644
--- a/MagickCore/paint.c
+++ b/MagickCore/paint.c
@@ -172,6 +172,8 @@ MagickExport MagickBooleanType FloodfillPaintImage(Image *image,
     return(MagickFalse);
   if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
     return(MagickFalse);
+  if (IsGrayColorspace(image->colorspace) != MagickFalse)
+    (void) TransformImageColorspace(image,sRGBColorspace,exception);
   if ((image->alpha_trait != BlendPixelTrait) &&
       (draw_info->fill.alpha_trait == BlendPixelTrait))
     (void) SetImageAlpha(image,OpaqueAlpha,exception);
@@ -778,6 +780,9 @@ MagickExport MagickBooleanType OpaquePaintImage(Image *image,
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
   if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
     return(MagickFalse);
+  if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
+      (IsPixelInfoGray(fill) == MagickFalse))
+    (void) TransformImageColorspace(image,sRGBColorspace,exception);
   if ((fill->alpha_trait == BlendPixelTrait) &&
       (image->alpha_trait != BlendPixelTrait))
     (void) SetImageAlpha(image,OpaqueAlpha,exception);
diff --git a/MagickCore/threshold.c b/MagickCore/threshold.c
index 178304c9e..20f0c6a83 100644
--- a/MagickCore/threshold.c
+++ b/MagickCore/threshold.c
@@ -419,7 +419,7 @@ MagickExport MagickBooleanType BilevelImage(Image *image,const double threshold,
   if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
     return(MagickFalse);
   if (IsGrayColorspace(image->colorspace) != MagickFalse)
-    (void) TransformImageColorspace(image,RGBColorspace,exception);
+    (void) TransformImageColorspace(image,sRGBColorspace,exception);
   /*
     Bilevel threshold image.
   */
@@ -556,7 +556,7 @@ MagickExport MagickBooleanType BlackThresholdImage(Image *image,
   if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
     return(MagickFalse);
   if (IsGrayColorspace(image->colorspace) != MagickFalse)
-    (void) TransformImageColorspace(image,RGBColorspace,exception);
+    (void) TransformImageColorspace(image,sRGBColorspace,exception);
   GetPixelInfo(image,&threshold);
   flags=ParseGeometry(thresholds,&geometry_info);
   threshold.red=geometry_info.rho;
-- 
2.40.0