about summary refs log tree commit diff
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2008-07-17 02:02:23 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2008-07-17 02:02:23 +0000
commitcaa414a5f66e6eca80400f0138b13f032ff17357 (patch)
tree6fea309d30f0eb50fb2c2f55b5ae4acc67b86b8f
parent02c85a2f540819e745fb1ed0358f8840a312e1b4 (diff)
downloadnetpbm-mirror-caa414a5f66e6eca80400f0138b13f032ff17357.tar.gz
netpbm-mirror-caa414a5f66e6eca80400f0138b13f032ff17357.tar.xz
netpbm-mirror-caa414a5f66e6eca80400f0138b13f032ff17357.zip
Fix gamma correction
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@679 9d0c8265-081b-0410-96cb-a4ca84ce46f8
-rw-r--r--converter/other/pngtopnm.c59
-rw-r--r--doc/HISTORY2
2 files changed, 33 insertions, 28 deletions
diff --git a/converter/other/pngtopnm.c b/converter/other/pngtopnm.c
index 3197eb01..86db5a47 100644
--- a/converter/other/pngtopnm.c
+++ b/converter/other/pngtopnm.c
@@ -35,6 +35,7 @@
 #endif                               /*  2 for warnings (1 == error) */
 
 #include <math.h>
+#include <float.h>
 #include <png.h>    /* includes zlib.h and setjmp.h */
 #define VERSION "2.37.4 (5 December 1999) +netpbm"
 
@@ -242,8 +243,8 @@ gamma_correct(png_uint_16 const v,
               float       const g) {
 
     if (g != -1.0)
-        return (png_uint_16) (pow ((double) v / maxval, 
-                                   (1.0 / g)) * maxval + 0.5);
+        return (png_uint_16) ROUNDU(pow((double) v / maxval, (1.0 / g)) *
+                                    maxval);
     else
         return v;
 }
@@ -566,30 +567,32 @@ setupGammaCorrection(png_struct * const png_ptr,
     if (displaygamma == -1.0)
         *totalgammaP = -1.0;
     else {
-        if (info_ptr->valid & PNG_INFO_gAMA) {
-            if (displaygamma != info_ptr->gamma) {
-                png_set_gamma(png_ptr, displaygamma, info_ptr->gamma);
-                *totalgammaP =
-                    (double) info_ptr->gamma * (double) displaygamma;
-                /* in case of gamma-corrections, sBIT's as in the
-                   PNG-file are not valid anymore 
-                */
-                info_ptr->valid &= ~PNG_INFO_sBIT;
-                if (verbose)
-                    pm_message("image gamma is %4.2f, "
-                               "converted for display gamma of %4.2f",
-                               info_ptr->gamma, displaygamma);
-            }
+        float imageGamma;
+        if (info_ptr->valid & PNG_INFO_gAMA)
+            imageGamma = info_ptr->gamma;
+        else {
+            if (verbose)
+                pm_message("PNG doesn't specify image gamma.  Assuming 1.0");
+            imageGamma = 1.0;
+        }
+
+        if (fabs(displaygamma * imageGamma - 1.0) < .01) {
+            *totalgammaP = -1.0;
+            if (verbose)
+                pm_message("image gamma %4.2f matches "
+                           "display gamma %4.2f.  No conversion.",
+                           imageGamma, displaygamma);
         } else {
-            if (displaygamma != info_ptr->gamma) {
-                png_set_gamma (png_ptr, displaygamma, 1.0);
-                *totalgammaP = (double) displaygamma;
-                info_ptr->valid &= ~PNG_INFO_sBIT;
-                if (verbose)
-                    pm_message("image gamma assumed 1.0, "
-                               "converted for display gamma of %4.2f",
-                               displaygamma);
-            }
+            png_set_gamma(png_ptr, displaygamma, imageGamma);
+            *totalgammaP = imageGamma * displaygamma;
+            /* in case of gamma-corrections, sBIT's as in the
+               PNG-file are not valid anymore 
+            */
+            info_ptr->valid &= ~PNG_INFO_sBIT;
+            if (verbose)
+                pm_message("image gamma is %4.2f, "
+                           "converted for display gamma of %4.2f",
+                           imageGamma, displaygamma);
         }
     }
 }
@@ -1039,9 +1042,9 @@ convertpng(FILE *             const ifp,
 
     if (verbose)
         /* Note that some of info_ptr is not defined until png_read_end() 
-       completes.  That's because it comes from chunks that are at the
-       end of the stream.
-    */
+           completes.  That's because it comes from chunks that are at the
+           end of the stream.
+        */
         dump_png_info(info_ptr);
 
     if (mtime)
diff --git a/doc/HISTORY b/doc/HISTORY
index 47add239..3245a0d9 100644
--- a/doc/HISTORY
+++ b/doc/HISTORY
@@ -22,6 +22,8 @@ not yet  BJH  Release 10.44.00
 
               pamflip: fix bug: garbage output for PBMs, since 10.42.
 
+              pngtopnm: fix gamma correction.
+
               pngtopnm: fix bug: when background is color, output
               should be PPM.