Index: trunk/Ohana/src/kii/picture/NewPicture.c
===================================================================
--- trunk/Ohana/src/kii/picture/NewPicture.c	(revision 5700)
+++ trunk/Ohana/src/kii/picture/NewPicture.c	(revision 5852)
@@ -1,44 +1,31 @@
 # include "Ximage.h"
-# define SCAN \
-  status = read (layout[0].Ximage, buffer, 16); \
-  buffer[16] = 0; \
-  sscanf (buffer, "%*s %d", &Nbytes); \
-  status = read (layout[0].Ximage, buffer, Nbytes); \
-  buffer[status] = 0; /* make the string easy to parse */ \
-  (DEBUG) && fprintf (stderr, "buffer: <%s>\n", buffer); \
 
 int NewPicture (Graphic *graphic, Layout *layout) {
 
   Header header;
-  char buffer[1024], *buff;
-  int status, bytes_left, Nbytes;
+  char *buff;
+  int status, bytes_left, socket;
  
-  fits_free_matrix (&layout[0].matrix);
-  sprintf (buffer, "NCOLOR: %6d ", layout[0].Npixels);
-  write (layout[0].Ximage, buffer, 16);
+  socket = layout[0].Ximage;
 
-  fcntl (layout[0].Ximage, F_SETFL, !O_NONBLOCK);  
+  KiiSendMessage (socket, "%d", layout[0].Npixels);
+
+  fcntl (socket, F_SETFL, !O_NONBLOCK);  
 
   header.Naxes = 2;
-  SCAN;
-  sscanf (buffer, "%d %d %d %d %lf %lf",
-	  &header.Naxis[0], &header.Naxis[1], &header.bitpix, 
-	  &header.unsign,   &header.bzero,    &header.bscale);
 
-  SCAN;
-  sscanf (buffer, "%lf %lf %lf %lf %d", &layout[0].zero, &layout[0].range, &layout[0].min,  &layout[0].max, &header.size);
+  KiiScanMessage (socket, "%d %d %d %d %lf %lf", &header.Naxis[0], &header.Naxis[1]);
+  KiiScanMessage (socket, "%d %d %d %d %lf %lf", &header.bitpix, &header.unsign, &header.bzero, &header.bscale);
+  KiiScanMessage (socket, "%lf %lf",  &layout[0].zero, &layout[0].range);
+  KiiScanMessage (socket, "%lf %lf %d", &layout[0].min,  &layout[0].max, &header.size);
+  KiiScanMessage (socket, "%lf %f %f %f %f", &layout[0].coords.crval1, &layout[0].coords.crpix1, &layout[0].coords.cdelt1, &layout[0].coords.pc1_1, &layout[0].coords.pc1_2);
+  KiiScanMessage (socket, "%lf %f %f %f %f", &layout[0].coords.crval2, &layout[0].coords.crpix2, &layout[0].coords.cdelt2, &layout[0].coords.pc2_1, &layout[0].coords.pc2_2);
+	  
+  KiiScanMessage (socket, "%s %s %s", layout[0].coords.ctype, layout[0].file, layout[0].buffer_name);
 
-  SCAN;
-  sscanf (buffer, "%lf %f %f %f %f", &layout[0].coords.crval1, &layout[0].coords.crpix1, &layout[0].coords.cdelt1, &layout[0].coords.pc1_1, &layout[0].coords.pc1_2);
-	  
-  SCAN;
-  sscanf (buffer, "%lf %f %f %f %f", &layout[0].coords.crval2, &layout[0].coords.crpix2, &layout[0].coords.cdelt2, &layout[0].coords.pc2_1, &layout[0].coords.pc2_2);
-	  
-  SCAN;
-  sscanf (buffer, "%s %s %s", layout[0].coords.ctype, layout[0].file, layout[0].buffer_name);
-
+  fits_free_matrix (&layout[0].matrix);
   fits_create_matrix (&header, &layout[0].matrix);
 
-  fcntl (layout[0].Ximage, F_SETFL, O_NONBLOCK);  
+  fcntl (socket, F_SETFL, O_NONBLOCK);  
 
   status = 1;
@@ -47,5 +34,5 @@
   layout[0].matrix.size = 0;
   while (bytes_left > 0) {
-    status = read (layout[0].Ximage, buff, bytes_left);
+    status = read (socket, buff, bytes_left);
     if (status == 0) {  /* No more pipe */
       fprintf (stderr, "error: pipe closed\n");
@@ -59,5 +46,5 @@
   }
 
-  (DEBUG) && fprintf (stderr, "read %d bytes\n", layout[0].matrix.size);
+  if (DEBUG) fprintf (stderr, "read %d bytes\n", layout[0].matrix.size);
   /* it it not obvious this condition should kill kii, but ... */
   if (layout[0].matrix.size != header.size) {  
@@ -69,7 +56,7 @@
 
   Remap (graphic, layout, &layout[0].matrix);
-  (DEBUG) && fprintf (stderr, "remapped image\n");
+  if (DEBUG) fprintf (stderr, "remapped image\n");
   Refresh (graphic, layout, 0);
-  (DEBUG) && fprintf (stderr, "refreshed\n");
+  if (DEBUG) fprintf (stderr, "refreshed\n");
   XFlush (graphic[0].display);
 
