Index: trunk/Ohana/src/kii/picture/JPEGit24.c
===================================================================
--- trunk/Ohana/src/kii/picture/JPEGit24.c	(revision 5637)
+++ trunk/Ohana/src/kii/picture/JPEGit24.c	(revision 5700)
@@ -1,6 +1,12 @@
 # include "Ximage.h"
 # include "jpeglib.h"
-
-void JPEGit24 (Graphic *graphic, Layout *layout) {
+# include <png.h>
+# include "bDraw.h"
+
+# define WHITE_R 255
+# define WHITE_G 255
+# define WHITE_B 255
+
+int JPEGit24 (Graphic *graphic, Layout *layout) {
 
   struct jpeg_compress_struct cinfo;
@@ -8,17 +14,16 @@
   JSAMPROW row_pointer[1];	/* pointer to JSAMPLE row[s] */
   JSAMPLE *image_buffer;	/* Points to data for current line */
-
-  int i, j, ii, jj;
+  JSAMPLE *line_buffer;	        /* Points to data for current line */
+
+  int ii, i, j;
   int i_start, i_end, j_start, j_end;
   int dropback, extra;  /* this is a bit of a kludge... */
   int dx, dy, DX, DY;
+  int status, Nbytes, quality;
+  int expand_in, expand_out;
   double expand, Rx, Ry, X, Y;
-  int expand_in, expand_out;
-  unsigned char *out_pix, *out_pix2, *data;
-  unsigned char *in_pix,  *in_pix2;
+  unsigned char *out_pix, *in_pix, *in_pix_ref;
   unsigned char pixel1[256], pixel2[256], pixel3[256];
-  unsigned char pixvalue1, pixvalue2, pixvalue3;
-  unsigned long white;
-  unsigned char white1, white2, white3;
+  char filename[1024];
   FILE *f;
 
@@ -36,5 +41,5 @@
   f = fopen (filename, "w");
   if (f == (FILE *) NULL) {
-    fprintf (stderr, "failed to open %s for output\n", filename);
+    fprintf (stderr, "Kii: failed to open %s for output\n", filename);
     return (TRUE);
   }
@@ -55,17 +60,10 @@
   jpeg_start_compress (&cinfo, TRUE);
 
-  /* output line buffer */
-  ALLOCATE (image_buffer, JSAMPLE, 3*layout[0].picture.dx);
-
   /** cmap[i].pixel must be defined even if X is not used **/
   for (i = 0; i < 256; i++) { /* set up pixel array */
-    pixel1[i] = 0x0000ff & (layout[0].cmap[i].pixel >> 0);
-    pixel2[i] = 0x0000ff & (layout[0].cmap[i].pixel >> 8);
-    pixel3[i] = 0x0000ff & (layout[0].cmap[i].pixel >> 16);
-  }
-  white = layout[0].white;
-  white1 = 0x0000ff & (white >> 0);
-  white2 = 0x0000ff & (white >> 8);
-  white3 = 0x0000ff & (white >> 16);
+    pixel1[i] = layout[0].cmap[i].red >> 8;
+    pixel2[i] = layout[0].cmap[i].green >> 8;
+    pixel3[i] = layout[0].cmap[i].blue >> 8;
+  }
 
   expand = expand_in = expand_out = 1.0;
@@ -85,7 +83,6 @@
   dx = layout[0].picture.dx;
   dy = layout[0].picture.dy;
-  DX = layout[0].matrix[0].Naxis[0];
-  DY = layout[0].matrix[0].Naxis[1];
-  extra = 4 - (dx * 3) % 4;  /* why is this needed?? */
+  DX = layout[0].matrix.Naxis[0];
+  DY = layout[0].matrix.Naxis[1];
 
   /* X,Y are the image coordinates of the first image pixel */
@@ -119,13 +116,14 @@
   if ((i_end - i_start) % expand_out == 0) dropback = 0;
 
-  out_pix = image_buffer;
-  in_pix  = (unsigned char *) (layout[0].matrix[0].buffer) + DX*(int)MAX(Y,0) + (int)MAX(X,0);
+  /* output line buffer */
+  ALLOCATE (image_buffer, JSAMPLE, 3*dx*dy);
+  ALLOCATE (line_buffer, JSAMPLE, 3*dx);
+
+  in_pix_ref  = (unsigned char *) (layout[0].matrix.buffer) + DX*(int)MAX(Y,0) + (int)MAX(X,0);
 
   /********** below we do the mapping from buffer pixels (in) to picture pixels (out) **********/
 
-  /* we write the jpeg image data one row at a time */
-
   /**** fill in bottom area ****/
-  out_pix = image_buffer;
+  out_pix = line_buffer;
   for (i = 0; i < dx; i++, out_pix+=3) {
     out_pix[0] = WHITE_R;
@@ -134,13 +132,13 @@
   }
   for (j = 0; j < j_start; j++) {
-    row_pointer[0] = image_buffer;
-    (void) jpeg_write_scanlines (&cinfo, row_pointer, 1);
+    memcpy (&image_buffer[j*3*dx], line_buffer, 3*dx);
   }
   
   /*** fill in the image data region ***/
-  for (j = j_start; j < j_end; j+= expand_out, in_pix += expand_in*DX) {
+  for (j = j_start; j < j_end; j+= expand_out, in_pix_ref += expand_in*DX) {
     
     /* create one output image line */
-    out_pix = image_buffer;
+    in_pix = in_pix_ref;
+    out_pix = line_buffer;
 
     /**** fill in area to the left of the picture ****/
@@ -152,8 +150,10 @@
     
     /*** fill in the picture region ***/
-    for (i = i_start; i < i_end; i++, in_pix+=expand_in, out_pix+=3) {
-      out_pix[0] = pixel1[*in_pix];
-      out_pix[1] = pixel2[*in_pix];
-      out_pix[2] = pixel3[*in_pix];
+    for (i = i_start; i < i_end; i+=expand_out, in_pix+=expand_in) {
+      for (ii = 0; ii < expand_out; ii++, out_pix+=3) {
+	out_pix[0] = pixel1[*in_pix];
+	out_pix[1] = pixel2[*in_pix];
+	out_pix[2] = pixel3[*in_pix];
+      }
     }
     
@@ -167,11 +167,10 @@
     /* write out the image line expand_out times */
     for (i = 0; i < expand_out; i++) {
-      row_pointer[0] = image_buffer;
-      (void) jpeg_write_scanlines (&cinfo, row_pointer, 1);
+      memcpy (&image_buffer[(j + i)*3*dx], line_buffer, 3*dx);
     }
   }
 
   /**** fill in top area ****/
-  out_pix = image_buffer;
+  out_pix = line_buffer;
   for (i = 0; i < dx; i++, out_pix+=3) { 
     out_pix[0] = WHITE_R;
@@ -179,6 +178,43 @@
     out_pix[2] = WHITE_B;
   }
-  for (j = 0; j < (dy - j_end); j++) {
-    row_pointer[0] = image_buffer;
+  for (j = j_end; j < dy; j++) {
+    memcpy (&image_buffer[j*3*dx], line_buffer, 3*dx);
+  }
+
+
+  /* I need to write the overlay objects on the jpeg image.
+     if i can write / overwrite data in jpeg buffer, then do it here,
+     otherwise i need to create a temporary image buffer, then write the 
+     scanlines to that buffer */
+
+  {
+    int Npalette;
+    png_color *palette;
+    bDrawColor white, color;
+    bDrawBuffer *buffer;
+
+    palette = MakePNGPalette (&Npalette);
+
+    buffer = bDrawBufferCreate (dx, dy);
+    bDrawSetBuffer (buffer);
+    for (i = 0; i < NOVERLAYS; i++) {
+      if (OVERLAY[i]) bDrawOverlay (layout, i);
+    }
+
+    white = GetColorByName ("white");
+    for (j = 0; j < dy; j++) {
+      for (i = 0; i < dx; i++) {
+	color = buffer[0].pixels[j][i];
+	if (color == white) continue;
+	image_buffer[j*3*dx + 3*i + 0] = palette[color].red;
+	image_buffer[j*3*dx + 3*i + 1] = palette[color].green;
+	image_buffer[j*3*dx + 3*i + 2] = palette[color].blue;
+      }
+    }
+    bDrawBufferFree (buffer);
+  }
+
+  for (i = 0; i < dy; i++) {
+    row_pointer[0] = &image_buffer[i*3*dx];
     (void) jpeg_write_scanlines (&cinfo, row_pointer, 1);
   }
@@ -190,3 +226,2 @@
   return (TRUE);
 }
-
