From 59e4d8ccd6db2869dbb785aa317bc3036465757c Mon Sep 17 00:00:00 2001
From: sam <sam@92316355-f0b4-4df1-b90c-862c8a59935f>
Date: Sun, 28 Sep 2008 17:01:56 +0000
Subject: [PATCH] Add a --gamma command to modify the global gamma value. This
 is a nasty hack that will hopefully disappear in the future. The default
 gamma is now 2.2 again (sRGB approximation).

git-svn-id: file:///srv/caca.zoy.org/var/lib/svn/libpipi/trunk@2846 92316355-f0b4-4df1-b90c-862c8a59935f
---
 pipi/context.c | 11 +++++++++++
 pipi/pipi.h    |  1 +
 pipi/pixels.c  | 22 +++++++++++++++-------
 src/pipi.c     |  8 ++++++++
 4 files changed, 35 insertions(+), 7 deletions(-)

diff --git a/pipi/context.c b/pipi/context.c
index 6fa08fa..c490655 100644
--- a/pipi/context.c
+++ b/pipi/context.c
@@ -71,6 +71,17 @@ int pipi_command(pipi_context_t *ctx, char const *cmd, ...)
         pipi_save(ctx->images[ctx->nimages], file);
         pipi_free(ctx->images[ctx->nimages]);
     }
+    else if(!strcmp(cmd, "gamma"))
+    {
+        char const *val;
+        va_list ap;
+
+        va_start(ap, cmd);
+        val = va_arg(ap, char const *);
+        va_end(ap);
+
+        pipi_set_gamma(atof(val));
+    }
     else if(!strcmp(cmd, "dither"))
     {
         pipi_image_t *src, *dst;
diff --git a/pipi/pipi.h b/pipi/pipi.h
index 3a6c187..d0f400d 100644
--- a/pipi/pipi.h
+++ b/pipi/pipi.h
@@ -118,6 +118,7 @@ extern pipi_image_t *pipi_copy(pipi_image_t *);
 extern void pipi_free(pipi_image_t *);
 extern int pipi_save(pipi_image_t *, const char *);
 
+extern void pipi_set_gamma(double);
 extern pipi_pixels_t *pipi_getpixels(pipi_image_t *, pipi_format_t);
 extern int pipi_get_image_width(pipi_image_t *img);
 extern int pipi_get_image_height(pipi_image_t *img);
diff --git a/pipi/pixels.c b/pipi/pixels.c
index 29ee571..fc09a19 100644
--- a/pipi/pixels.c
+++ b/pipi/pixels.c
@@ -28,14 +28,14 @@
 #include "pipi.h"
 #include "pipi_internals.h"
 
-#define GAMMA 1.0
-
 static void init_tables(void);
 
+static double global_gamma = 2.2;
+static int done = 0;
+
 static float u8tof32_table[256];
 static inline float u8tof32(uint8_t p) { return u8tof32_table[(int)p]; }
 
-
 /* Return a direct pointer to an image's pixels. */
 pipi_pixels_t *pipi_getpixels(pipi_image_t *img, pipi_format_t type)
 {
@@ -152,7 +152,7 @@ pipi_pixels_t *pipi_getpixels(pipi_image_t *img, pipi_format_t type)
 
                     if(p < 0.) d = 0.;
                     else if(p > 1.) d = 255;
-                    else d = (int)(255.999 * pow(p, 1. / GAMMA));
+                    else d = (int)(255.999 * pow(p, 1. / global_gamma));
 
                     dest[4 * (y * img->w + x) + i] = d;
 
@@ -188,7 +188,7 @@ pipi_pixels_t *pipi_getpixels(pipi_image_t *img, pipi_format_t type)
 
                     if(p < 0.) d = 0.;
                     else if(p > 1.) d = 255;
-                    else d = (int)(255.999 * pow(p, 1. / GAMMA));
+                    else d = (int)(255.999 * pow(p, 1. / global_gamma));
 
                     dest[3 * (y * img->w + x) + i] = d;
 
@@ -251,17 +251,25 @@ pipi_pixels_t *pipi_getpixels(pipi_image_t *img, pipi_format_t type)
     return &img->p[type];
 }
 
+void pipi_set_gamma(double g)
+{
+    if(g > 0.)
+    {
+        global_gamma = g;
+        done = 0;
+    }
+}
 
 static void init_tables(void)
 {
-    static int done = 0;
     int i;
 
     if(done)
         return;
 
     for(i = 0; i < 256; i++)
-        u8tof32_table[i] = pow((double)i / 255., GAMMA);
+        u8tof32_table[i] = pow((double)i / 255., global_gamma);
 
     done = 1;
 }
+
diff --git a/src/pipi.c b/src/pipi.c
index 230744c..0b1bc28 100644
--- a/src/pipi.c
+++ b/src/pipi.c
@@ -33,6 +33,14 @@ int main(int argc, char *argv[])
                 return EXIT_FAILURE;
             argv++;
         }
+        else if(!strcmp(argv[0], "--gamma"))
+        {
+            if(argv[1] == NULL)
+                return EXIT_FAILURE;
+            if(pipi_command(ctx, "gamma", argv[1]) != 0)
+                return EXIT_FAILURE;
+            argv++;
+        }
         else if(!strcmp(argv[0], "--scale"))
         {
             if(argv[1] == NULL)