--- ../dillo/src/png.c	Mon Dec  4 21:48:19 2000
+++ src/png.c	Tue Dec  5 00:01:18 2000
@@ -23,6 +23,7 @@
 #include "web.h"
 #include "cache.h"
 #include "dicache.h"
+#include "prefs.h"
 
 enum prog_state {
    IS_finished, IS_init, IS_nextdata
@@ -119,6 +120,7 @@ Png_datainfo_callback(png_structp png_pt
    gint bit_depth;
    gint interlace_type;
    guint i;
+   double gamma;
 
 #if defined(VERBOSE)
    g_print("Png_datainfo_callback:\n");
@@ -152,6 +154,12 @@ Png_datainfo_callback(png_structp png_pt
       png_set_strip_16(png_ptr);
    }
 
+   /* Get and set gamma information. Beware: gamma correction 2.2 will
+      only work on PC's. todo: select screen gamma correction for other 
+      platforms. */
+   if (png_get_gAMA(png_ptr, info_ptr, &gamma))
+      png_set_gamma(png_ptr, 2.2, gamma);
+
    /* Convert gray scale to RGB */
    if (color_type == PNG_COLOR_TYPE_GRAY || 
        color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
@@ -216,17 +224,44 @@ static void 
                       png->image_data + (row_num * png->rowbytes), 0, row_num);
       break;
    case 4:
-      for (i = 0; i < png->width; i++) {
-         gint p;
-
-         for (p = 0; p < 3; p++) {
-            png->linebuf[3 * i + p] =
-                png->image_data[row_num * png->rowbytes + (4 * i + p)];
-         }
-      /* todo: use alpha channel */
-      }
-      a_Dicache_write(png->Image, png->DicEntryKey, png->linebuf, 0, row_num);
-      break;
+     {
+        /* todo: get the backgound color from the parent 
+	   of the image widget -- Livio.                 */
+        gint a, bg_red, bg_green, bg_blue;
+        guchar *pl = png->linebuf;
+        guchar *data = png->image_data + (row_num * png->rowbytes);
+
+	/* maybe change prefs.bg_color to `a_Dw_widget_get_bg_color`, 
+	   when background colors are correctly implementated */
+	bg_blue  = (prefs.bg_color) & 0xFF;
+	bg_green = (prefs.bg_color>>8) & 0xFF;
+	bg_red   = (prefs.bg_color>>16) & 0xFF;
+	 
+	for (i = 0; i < png->width; i++) {	   
+	   a = *(data+3);
+	   
+	   if (a == 255){
+	      *(pl++) =  *(data++);
+	      *(pl++) =  *(data++);
+	      *(pl++) =  *(data++);
+	      data++;
+	   }
+	   else if (a == 0){
+	      *(pl++) = bg_red;
+	      *(pl++) = bg_green;
+	      *(pl++) = bg_blue;
+	      data += 4;
+	   }
+	   else {
+	      png_composite(*(pl++), *(data++), a, bg_red);
+	      png_composite(*(pl++), *(data++), a, bg_green);
+	      png_composite(*(pl++), *(data++), a, bg_blue);
+	      data++;
+	   }
+	}
+	a_Dicache_write(png->Image, png->DicEntryKey, png->linebuf, 0, row_num);
+	break;
+     }
    default:
       g_print("Png_datarow_callback: unexpected number of channels = %d\n",
               png->channels);
