Hi,

Because color adjustment of the primary layer requires
administrative/exclusive level, it's a bit too difficult to use them
normally.  So, I thought of adding this as a --dfb: option. Also,
setting of the gamma ramp is now "visual format blind".

Several new options are available:

For gamma control: 'gamma_all', 'gamma_red', 'gamma_green', and
'gamma_blue'.

They're self-explanatory.  The range of values is from 0 - 1000 (gamma
0.0 - 10.0), where the normal (gamma 1.0) is 100.

For color adjustment: 'brightness', 'contrast', and 'saturation'. The
range of values is from 0 - 255, where 128 is the normal value.

The gamma and brightness control are okay.  Contrast and saturation are
okay at high values, but pretty much becomes useless at low levels.

Quickstart:  To filter out the green and blue channels, try:

df_dok --dfb:gamma_green=10,gamma_blue=10

which should result in a red tinted display. If nothing changed, then
your card's DAC is hardwired to truecolor.

Patch is against the latest CVS version as of this writing.

Tony
diff -Naur DirectFB-cvs/src/core/fbdev/fbdev.c DirectFB-coloradjust/src/core/fbdev/fbdev.c
--- DirectFB-cvs/src/core/fbdev/fbdev.c	2002-11-27 06:44:01.000000000 +0000
+++ DirectFB-coloradjust/src/core/fbdev/fbdev.c	2002-11-27 06:57:13.000000000 +0000
@@ -26,6 +26,7 @@
 
 #include "config.h"
 
+#include <math.h>
 #include <unistd.h>
 #include <stdlib.h>
 #include <errno.h>
@@ -1616,17 +1617,83 @@
      return DFB_OK;
 }
 
+#define DAC_WIDTH  256
+
+static signed short
+dfb_fbdev_calc_brightness(signed short color, signed short bright)
+{
+     color += bright - 128;
+     
+     if (color > DAC_WIDTH - 1)
+	  color = DAC_WIDTH - 1;
+     if (color < 0)
+	  color = 0;
+
+     return color;
+}
+
+static signed short
+dfb_fbdev_calc_saturation(signed short color, signed short sat)
+{
+     if (sat > 128) {
+	  float gray = ((float)sat - 128.0)/128.0;
+	  float col = 1.0 - gray;
+
+	  color = (signed short)(((float)color - 128.0 * gray)/col);
+     }
+     else if (sat < 128) {
+	  float col = (float)sat/128.0;
+	  float gray = 1.0 - col;
+
+	  color = (signed short)(((float)color * col) + 
+				 (128.0 * gray));
+     }
+
+     if (color > DAC_WIDTH - 1)
+	  color = DAC_WIDTH - 1;
+     if (color < 0)
+	  color = 0;
+
+     return color;
+}
+
+static signed short
+dfb_fbdev_calc_contrast(signed short color, signed short contrast)
+{
+     if (contrast > 128) {
+	  signed short c = contrast - 128;
+
+	  color = ((color + c/2)/c) * c;
+     }
+     else if (contrast < 128) {
+	  float c = (float)contrast/128.0;
+
+	  color = (signed short)((float)color * c);
+     }
+     if (color > DAC_WIDTH - 1)
+	  color = DAC_WIDTH - 1;
+     if (color < 0)
+	  color = 0;
+     
+     return color;
+}
+
 /*
  * some fbdev drivers use the palette as gamma ramp in >8bpp modes, to have
  * correct colors, the gamme ramp has to be initialized.
+ *
+ * Assumes 8-bit DAC
  */
-
-static __u16 dfb_fbdev_calc_gamma(int n, int max)
+static __u16 dfb_fbdev_calc_gamma(int n, int gamma, int max)
 {
-    int ret = 65535.0 * ((float)((float)n/(max)));
-    if (ret > 65535) ret = 65535;
-    if (ret <     0) ret =     0;
-    return ret;
+     int ret = (int) (pow((double)n/(double)max, 100.0/(double)gamma) *
+		      (double)max + 0.5);
+     ret = (int)((float)DAC_WIDTH * ((float)ret/(float)max));
+     if (ret > 255)
+	  ret = 255;
+     if (ret < 0)
+	  ret = 0;
+     return ret;
 }
 
 static DFBResult dfb_fbdev_set_gamma_ramp( DFBSurfacePixelFormat format )
@@ -1636,9 +1703,9 @@
      int red_size   = 0;
      int green_size = 0;
      int blue_size  = 0;
-     int red_max    = 0;
-     int green_max  = 0;
-     int blue_max   = 0;
+     int gamma_red  = 0;
+     int gamma_blue = 0;
+     int gamma_green = 0;
 
      struct fb_cmap *cmap;
 
@@ -1670,45 +1737,42 @@
                return DFB_OK;
      }
 
-     /*
-      * ++Tony: The gamma ramp must be set differently if in DirectColor,
-      *         ie, to mimic TrueColor, index == color[index].
-      */
-     if (dfb_fbdev->shared->fix.visual == FB_VISUAL_DIRECTCOLOR) {
-          red_max   = 65536 / (256/red_size);
-          green_max = 65536 / (256/green_size);
-	  blue_max  = 65536 / (256/blue_size);
-     }
-     else {
-         red_max   = red_size;
-         green_max = green_size;
-         blue_max  = blue_size;
-     }
-
      /* assume green to have most weight */
      dfb_fbdev->shared->temp_cmap.len = green_size;
 
      cmap = &dfb_fbdev->shared->current_cmap;
 
-     for (i = 0; i < red_size; i++)
-          cmap->red[i] = dfb_fbdev_calc_gamma( i, red_max );
-
-     for (i = 0; i < green_size; i++)
-          cmap->green[i] = dfb_fbdev_calc_gamma( i, green_max );
-
-     for (i = 0; i < blue_size; i++)
-          cmap->blue[i] = dfb_fbdev_calc_gamma( i, blue_max );
-
-     /* ++Tony: Some drivers use the upper byte, some use the lower */
-     if (dfb_fbdev->shared->fix.visual == FB_VISUAL_DIRECTCOLOR) {
-         for (i = 0; i < red_size; i++)
-	     cmap->red[i] |= cmap->red[i] << 8;
-
-         for (i = 0; i < green_size; i++)
-             cmap->green[i] |= cmap->green[i] << 8;
-
-         for (i = 0; i < blue_size; i++)
-             cmap->blue[i] |= cmap->blue[i] << 8;
+     if (dfb_config->gamma_all != -1) {
+	  gamma_red   = dfb_config->gamma_all;
+	  gamma_green = dfb_config->gamma_all;
+	  gamma_blue  = dfb_config->gamma_all;
+     }
+     else {
+	  gamma_red   = dfb_config->gamma_red;
+	  gamma_green = dfb_config->gamma_green;
+	  gamma_blue  = dfb_config->gamma_blue;
+     }
+
+     for (i = 0; i < red_size; i++) {
+          cmap->red[i] = dfb_fbdev_calc_gamma( i, gamma_red, red_size );
+	  cmap->red[i] = dfb_fbdev_calc_brightness( cmap->red[i], dfb_config->brightness );
+	  cmap->red[i] = dfb_fbdev_calc_saturation( cmap->red[i], dfb_config->saturation );
+	  cmap->red[i] = dfb_fbdev_calc_contrast( cmap->red[i], dfb_config->contrast );
+	  cmap->red[i] |= cmap->red[i] << 8;
+     }
+     for (i = 0; i < green_size; i++) {
+          cmap->green[i] = dfb_fbdev_calc_gamma( i, gamma_green, green_size );
+	  cmap->green[i] = dfb_fbdev_calc_brightness( cmap->green[i], dfb_config->brightness );
+	  cmap->green[i] = dfb_fbdev_calc_saturation( cmap->green[i], dfb_config->saturation );
+	  cmap->green[i] = dfb_fbdev_calc_contrast( cmap->green[i], dfb_config->contrast );
+	  cmap->green[i] |= cmap->green[i] << 8;
+     }
+     for (i = 0; i < blue_size; i++) {
+          cmap->blue[i] = dfb_fbdev_calc_gamma( i, gamma_blue, blue_size );
+	  cmap->blue[i] = dfb_fbdev_calc_brightness( cmap->blue[i], dfb_config->brightness );
+	  cmap->blue[i] = dfb_fbdev_calc_saturation( cmap->blue[i], dfb_config->saturation );
+	  cmap->blue[i] = dfb_fbdev_calc_contrast( cmap->blue[i], dfb_config->contrast );
+	  cmap->blue[i] |= cmap->blue[i] << 8;
      }
 
      if (ioctl( dfb_fbdev->fd, FBIOPUTCMAP, cmap ) < 0) {
@@ -1727,6 +1791,9 @@
      int red_val;
      int green_val;
      int blue_val;
+     int gamma_red  = 0;
+     int gamma_blue = 0;
+     int gamma_green = 0;
      int i = 0;
 
      struct fb_cmap cmap;
@@ -1744,13 +1811,23 @@
      cmap.blue   = (__u16*)alloca( 2 * 256 );
      cmap.transp = (__u16*)alloca( 2 * 256 );
 
+     if (dfb_config->gamma_all != -1) {
+	  gamma_red   = dfb_config->gamma_all;
+	  gamma_green = dfb_config->gamma_all;
+	  gamma_blue  = dfb_config->gamma_all;
+     }
+     else {
+	  gamma_red   = dfb_config->gamma_red;
+	  gamma_green = dfb_config->gamma_green;
+	  gamma_blue  = dfb_config->gamma_blue;
+     }
 
      for (red_val = 0; red_val  < 8 ; red_val++) {
           for (green_val = 0; green_val  < 8 ; green_val++) {
                for (blue_val = 0; blue_val  < 4 ; blue_val++) {
-                    cmap.red[i]    = dfb_fbdev_calc_gamma( red_val, 7 );
-                    cmap.green[i]  = dfb_fbdev_calc_gamma( green_val, 7 );
-                    cmap.blue[i]   = dfb_fbdev_calc_gamma( blue_val, 3 );
+                    cmap.red[i]    = dfb_fbdev_calc_gamma( red_val, gamma_red, 7 );
+                    cmap.green[i]  = dfb_fbdev_calc_gamma( green_val, gamma_green, 7 );
+                    cmap.blue[i]   = dfb_fbdev_calc_gamma( blue_val, gamma_blue, 3 );
                     cmap.transp[i] = (i ? 0x2000 : 0xffff);
                     i++;
                }
diff -Naur DirectFB-cvs/src/misc/conf.c DirectFB-coloradjust/src/misc/conf.c
--- DirectFB-cvs/src/misc/conf.c	2002-11-11 15:32:30.000000000 +0000
+++ DirectFB-coloradjust/src/misc/conf.c	2002-11-27 07:17:49.000000000 +0000
@@ -117,6 +117,20 @@
     "Allow translucent windows\n"
     "  videoram-limit=<amount>        "
     "Limit amount of Video RAM in kb\n"
+    "  brightness=<amount>            "
+    "Set brightness (0-255, 128 normal)\n"
+    "  saturation=<amount>            "
+    "Set saturation(0-255, 128 normal)\n"
+    "  contrast=<amount>              "
+    "Set contrast (0-255, 128 normal)\n"
+    "  gamma_all=<gamma>              "
+    "Set gamma of all channels = gamma/100 (0-1000)\n"
+    "  gamma_red=<gamma>              "
+    "Set gamma of red channel = gamma/100 (0-1000)\n"
+    "  gamma_green=<gamma>            "
+    "Set gamma of green channel = gamma/100 (0-1000)\n"
+    "  gamma_blue=<gamma>             "
+    "Set gamma of blue channel = gamma/100 (0-1000)\n"
     "  matrox-tv-standard=[pal|ntsc]  "
     "Matrox TV standard (default=pal)\n"
     "  [no-]matrox-sgram              "
@@ -249,6 +263,13 @@
      dfb_config->mouse_motion_compression = true;
      dfb_config->window_policy            = -1;
      dfb_config->buffer_mode              = -1;
+     dfb_config->brightness               = 128;
+     dfb_config->saturation               = 128;
+     dfb_config->contrast                 = 128;
+     dfb_config->gamma_all                = -1;
+     dfb_config->gamma_red                = 100;
+     dfb_config->gamma_green              = 100;
+     dfb_config->gamma_blue               = 100;
 
      sigemptyset( &dfb_config->dont_catch );
 
@@ -399,6 +420,153 @@
                return DFB_INVARG;
           }
      } else
+     if (strcmp (name, "brightness" ) == 0) {
+          if (value) {
+               int brightness;
+               
+               if (sscanf( value, "%d", &brightness ) < 1) {
+                    ERRORMSG("DirectFB/Config 'brightness': Could not parse value!\n");
+                    return DFB_INVARG;
+               }
+
+               if (brightness < 0)
+                    brightness = 0;
+	       if (brightness > 255)
+		    brightness = 255;
+               
+               dfb_config->brightness = (signed short) brightness;
+          }
+          else {
+               ERRORMSG("DirectFB/Config 'brightness': No value specified!\n");
+               return DFB_INVARG;
+          }
+     } else
+     if (strcmp (name, "saturation" ) == 0) {
+          if (value) {
+               int saturation;
+               
+               if (sscanf( value, "%d", &saturation ) < 1) {
+                    ERRORMSG("DirectFB/Config 'saturation': Could not parse value!\n");
+                    return DFB_INVARG;
+               }
+
+               if (saturation < 0)
+                    saturation = 0;
+	       if (saturation > 255)
+		    saturation = 255;
+               
+               dfb_config->saturation = (signed short) saturation;
+          }
+          else {
+               ERRORMSG("DirectFB/Config 'saturation': No value specified!\n");
+               return DFB_INVARG;
+          }
+     } else
+     if (strcmp (name, "contrast" ) == 0) {
+          if (value) {
+               int contrast;
+               
+               if (sscanf( value, "%d", &contrast ) < 1) {
+                    ERRORMSG("DirectFB/Config 'contrast': Could not parse value!\n");
+                    return DFB_INVARG;
+               }
+
+               if (contrast < 0)
+                    contrast = 0;
+	       if (contrast > 255)
+		    contrast = 255;
+               
+               dfb_config->contrast = (signed short) contrast;
+          }
+          else {
+               ERRORMSG("DirectFB/Config 'contrast': No value specified!\n");
+               return DFB_INVARG;
+          }
+     } else
+     if (strcmp (name, "gamma_all" ) == 0) {
+          if (value) {
+               int gamma;
+               
+               if (sscanf( value, "%d", &gamma ) < 1) {
+                    ERRORMSG("DirectFB/Config 'gamma_all': Could not parse value!\n");
+                    return DFB_INVARG;
+               }
+
+               if (gamma < 0)
+                    gamma = 0;
+	       if (gamma > 1000)
+		    gamma = 1000;
+               
+               dfb_config->gamma_all = (signed short) gamma;
+          }
+          else {
+               ERRORMSG("DirectFB/Config 'gamma_all': No value specified!\n");
+               return DFB_INVARG;
+          }
+     } else
+     if (strcmp (name, "gamma_red" ) == 0) {
+          if (value) {
+               int gamma;
+               
+               if (sscanf( value, "%d", &gamma ) < 1) {
+                    ERRORMSG("DirectFB/Config 'gamma_red': Could not parse value!\n");
+                    return DFB_INVARG;
+               }
+
+               if (gamma < 0)
+                    gamma = 0;
+	       if (gamma > 1000)
+		    gamma = 1000;
+               
+               dfb_config->gamma_red = (signed short) gamma;
+          }
+          else {
+               ERRORMSG("DirectFB/Config 'gamma_red': No value specified!\n");
+               return DFB_INVARG;
+          }
+     } else
+     if (strcmp (name, "gamma_green" ) == 0) {
+          if (value) {
+               int gamma;
+               
+               if (sscanf( value, "%d", &gamma ) < 1) {
+                    ERRORMSG("DirectFB/Config 'gamma_green': Could not parse value!\n");
+                    return DFB_INVARG;
+               }
+
+               if (gamma < 0)
+                    gamma = 0;
+	       if (gamma > 1000)
+		    gamma = 1000;
+               
+               dfb_config->gamma_green = (signed short) gamma;
+          }
+          else {
+               ERRORMSG("DirectFB/Config 'gamma_green': No value specified!\n");
+               return DFB_INVARG;
+          }
+     } else
+     if (strcmp (name, "gamma_blue" ) == 0) {
+          if (value) {
+               int gamma;
+               
+               if (sscanf( value, "%d", &gamma ) < 1) {
+                    ERRORMSG("DirectFB/Config 'gamma_blue': Could not parse value!\n");
+                    return DFB_INVARG;
+               }
+
+               if (gamma < 0)
+                    gamma = 0;
+	       if (gamma > 1000)
+		    gamma = 1000;
+               
+               dfb_config->gamma_blue = (signed short) gamma;
+          }
+          else {
+               ERRORMSG("DirectFB/Config 'gamma_blue': No value specified!\n");
+               return DFB_INVARG;
+          }
+     } else
      if (strcmp (name, "quiet" ) == 0) {
           dfb_config->quiet = true;
      } else
diff -Naur DirectFB-cvs/src/misc/conf.h DirectFB-coloradjust/src/misc/conf.h
--- DirectFB-cvs/src/misc/conf.h	2002-10-22 20:29:07.000000000 +0000
+++ DirectFB-coloradjust/src/misc/conf.h	2002-11-27 06:57:59.000000000 +0000
@@ -104,6 +104,20 @@
      int       videoram_limit;                    /* limit amount of video
                                                      memory used by DirectFB */
 
+     signed short brightness;                     /* brightness control */
+
+     signed short saturation;                     /* saturation control */
+
+     signed short contrast;                       /* contrast control */
+
+     int          gamma_all;                      /* gamma control all components */
+     
+     int          gamma_red;                      /* red gamma control */
+     
+     int          gamma_green;                    /* green gamma control */
+
+     int          gamma_blue;                     /* blue gamma control */
+
      sigset_t  dont_catch;                        /* don't catch these signals */
 
      char     *screenshot_dir;                    /* dump screen content into

Reply via email to