changeset 18957:87cba451fd5e stable

__magick_read__.cc: fix png check for image class in older GM (bug #42834).
author Carnë Draug <carandraug@octave.org>
date Thu, 24 Jul 2014 17:37:45 +0100
parents ab5983dc0587
children a5add7b660ac
files libinterp/dldfcn/__magick_read__.cc
diffstat 1 files changed, 26 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/dldfcn/__magick_read__.cc	Thu Jul 24 11:11:57 2014 -0700
+++ b/libinterp/dldfcn/__magick_read__.cc	Thu Jul 24 17:37:45 2014 +0100
@@ -74,15 +74,32 @@
 static bool
 is_indexed (const Magick::Image& img)
 {
-  bool retval = false;
-  const std::string format = img.magick ();
-  if (img.classType () == Magick::PseudoClass
-      && format != "JPEG"
-      && (format != "PNG"
-          || const_cast<Magick::Image&> (img).attribute ("PNG:IHDR.color-type-orig") == "3"))
-    retval = true;
-
-  return retval;
+  bool indexed = (img.classType () == Magick::PseudoClass);
+  // Our problem until now is non-indexed images, being represented as indexed
+  // by GM. The following attempts educated guesses to undo this optimization.
+  if (indexed)
+    {
+      const std::string fmt = img.magick ();
+      if (fmt == "JPEG")
+        // The JPEG format does not support indexed images, but GM sometimes
+        // reports grayscale JPEG as indexed. Always false for JPEG.
+        indexed = false;
+      else if (fmt == "PNG")
+        {
+          // Newer versions of GM (at least does not happens with 1.3.16) will
+          // store values from the underlying library as image attributes. In
+          // the case of PNG files, this is libpng where an indexed image will
+          // always have a value of 3 for "color-type-orig". This property
+          // always has a value in libpng so if we get nothing, we assume this
+          // GM version does not store them and we have to go with whatever
+          // GM PseudoClass says.
+          const std::string color_type =
+            const_cast<Magick::Image&> (img).attribute ("PNG:IHDR.color-type-orig");
+          if (! color_type.empty() && color_type != "3")
+            indexed = false;
+        }
+    }
+  return indexed;
 }
 
 //  The depth from depth() is not always correct for us but seems to be the