#include <stdlib.h>
#include <stdio.h>
#include "giflib.h"
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <X11/Xatom.h>

#define CMAPS 1
#define REDBLUE 0
#define SMALL 0
#define GREY 0
#define FULL 0
#define Boolean int



#define MAXCOLORS 65536

extern Colormap cmap[CMAPS], dmap;
extern int *staticmap[CMAPS];
extern int lowcolor, hicolor; 
extern Boolean staticvis, colorvis;
extern int screenint, screendepth;
extern Visual *visual;
extern Display *myDisplay;
extern Window toplevelWin;

extern void dynamicpix(int, XColor *), staticpix(int, XColor *);

void (*storepix)(int cmapindex, XColor *pcolor);

void initialize(int CTlength, color *CT)
{
  XColor pcolor;
  int i, j, p, range, ncolors;
  int alloc;
  Window win = toplevelWin;

  range = 0; /* nocturne */

  screenint = DefaultScreen(myDisplay);
  screendepth = DefaultDepth(myDisplay, screenint);

  visual = DefaultVisual(myDisplay, screenint);
  dmap = XDefaultColormap(myDisplay, screenint);

  staticvis = (StaticGray == visual->class || StaticColor == visual->class
	       || TrueColor == visual->class);
  colorvis = !(StaticGray == visual->class || GrayScale == visual->class);

  /* this assumes we want a lot of colors. */
  range = 255;
  ncolors = 256;
  hicolor = 255;
  lowcolor = 0;
  
  pcolor.flags = DoRed | DoGreen | DoBlue;

  if (staticvis) {
    alloc = AllocNone;
    storepix = staticpix;
    range *= 2;
    lowcolor = 0;
    hicolor = range;
  }
  else {
    alloc = AllocAll;
    storepix = dynamicpix;
  }


  /* COMMENT OUT A LOT OF THIS !!! */
  cmap[GREY] = XCreateColormap(myDisplay, win, visual, alloc);
  if (staticvis) 
    staticmap[GREY] = (int *) calloc(range+1, sizeof(*staticmap[GREY]));
  for (i = 0; i <= range; ++i) {
    pcolor.red = pcolor.blue = 0;
    pcolor.green = 65535;
    pcolor.pixel = hicolor - i;
    storepix(GREY, &pcolor);
  }

  if (colorvis) {
    cmap[i] = XCreateColormap(myDisplay, win, visual, alloc);
    if (staticvis) 
      staticmap[i] = (int *) calloc(range+1, sizeof(*staticmap[i]));

    p = lowcolor;

    printf("CTlength %i\n", CTlength);
    for (j = 0; j < CTlength; ++j) {
      pcolor.red =   (CT[j].red * 65535) / 255;
      pcolor.green = (CT[j].grn * 65535) / 255;
      pcolor.blue =  (CT[j].blu * 65535) / 255;
      pcolor.pixel = p++;
      /* printf("%i Pixel %i: %i %i %i\n", i, p, pcolor.red, pcolor.green, pcolor.blue); */
      storepix(0, &pcolor);
    }

      pcolor.blue = pcolor.red =   0;
      pcolor.green = 65535;
    while ((pcolor.pixel = p++) <= hicolor)
      storepix(0, &pcolor);
  } else {
    cmap[i] = cmap[GREY];
    if (staticvis) staticmap[i] = staticmap[GREY];
  }

  if (!staticvis) 
    for (pcolor.pixel = 0; pcolor.pixel < lowcolor; pcolor.pixel++) {
      XQueryColor(myDisplay, dmap, &pcolor);
      for (i = 0; i < CMAPS; ++i)
	XStoreColor(myDisplay, cmap[i], &pcolor);
    }
}

void dynamicpix(int n, XColor *pcolor)
{
  XStoreColor(myDisplay, cmap[n], pcolor);
}

void staticpix(int n, XColor *pcolor)
{
  int i = pcolor->pixel;

  XAllocColor(myDisplay, cmap[n], pcolor);

  staticmap[n][i] = pcolor->pixel;
}
