# include "Ximage.h"

int SetChannel (int sock) {
  
  int Nchannel;
  Graphic *graphic;
  Section *section;
  KapaImageWidget *image;

  graphic = GetGraphic ();
  section = GetActiveSection();
  if (section->image == NULL) {
    section->image = InitImageWidget ();
    SetSectionSizes (section);
  }
  image = section->image;

  KiiScanMessage (sock, "%d", &Nchannel);

  if (Nchannel <  0) return (TRUE);
  if (Nchannel >= NCHANNELS) return (TRUE);
  
  image[0].image = &image[0].channel[Nchannel];
  SetColorScale (graphic, image);

  if (!USE_XWINDOW) return (TRUE);

  Remap (graphic, image);
  if (DEBUG) fprintf (stderr, "remapped image\n");
  Refresh ();
  if (DEBUG) fprintf (stderr, "refreshed\n");
  XFlush (graphic[0].display);

  return (TRUE);
}

int SetColormapFromPipe (int sock) {
  
  Graphic *graphic;
  Section *section;
  KapaImageWidget *image;
  char colormap[256];
  int status;

  graphic = GetGraphic ();
  section = GetActiveSection();
  if (section->image == NULL) {
    section->image = InitImageWidget ();
    SetSectionSizes (section);
  }
  image = section->image;

  KiiScanMessage (sock, "%s", colormap);

  status = SetColormap (colormap);
  if (!status) fprintf (stderr, "unknown colormap %s\n", colormap);

  SetColorScale (graphic, image);
  if (!USE_XWINDOW) return (TRUE);

  Remap (graphic, image);
  Refresh ();
  XFlush (graphic[0].display);

  return (TRUE);
}
