# include "Ximage.h"

void CreatePicture (Layout *layout, Graphic *graphic) {

  int i, j, extra;
  unsigned char *c;
  unsigned int *l;
  unsigned int start, start1, start2, start3;

  start = layout[0].white;

  switch (graphic[0].Nbits) {
  case 8:
    REALLOCATE (layout[0].picture.data, char, layout[0].picture.dx*layout[0].picture.dy);
    c = (unsigned char *) layout[0].picture.data;
    for (i = 0; i < (layout[0].picture.dx*layout[0].picture.dy); i++, c++)
      *c = start;
    layout[0].picture.pix = XCreateImage (graphic[0].display, graphic[0].visual, graphic[0].depth, ZPixmap, 0, 
					  layout[0].picture.data, layout[0].picture.dx, layout[0].picture.dy, 8, 0);
    break;

  case 16:
    REALLOCATE (layout[0].picture.data, char, 2*layout[0].picture.dy*layout[0].picture.dx);
    c = (unsigned char *) layout[0].picture.data;
    start1 = 0x0000ff & (start);
    start2 = 0x0000ff & (start >> 8);
    for (i = 0; i < layout[0].picture.dy; i++) {
      for (j = 0; j < layout[0].picture.dx; j++, c+=2) {
	c[0] = start1;
	c[1] = start2;
      }
    }
    layout[0].picture.pix = XCreateImage (graphic[0].display, graphic[0].visual, graphic[0].depth, ZPixmap, 0, 
					  layout[0].picture.data, layout[0].picture.dx, layout[0].picture.dy, 16, 0);
    break;

  case 24:
    extra = 4 - (layout[0].picture.dx * 3) % 4;
    REALLOCATE (layout[0].picture.data, char, layout[0].picture.dy*(3*layout[0].picture.dx+extra));
    c = (unsigned char *) layout[0].picture.data;
    start1 = 0x0000ff & (start);
    start2 = 0x0000ff & (start >> 8);
    start3 = 0x0000ff & (start >> 16);
    for (i = 0; i < layout[0].picture.dy; i++) {
      for (j = 0; j < layout[0].picture.dx; j++, c+=3) {
	c[0] = start1;
	c[1] = start2;
	c[2] = start3;
      }
      c+=extra;
    }
    layout[0].picture.pix = XCreateImage (graphic[0].display, graphic[0].visual, graphic[0].depth, ZPixmap, 0, 
					  layout[0].picture.data, layout[0].picture.dx, layout[0].picture.dy, 24, 0);
    break;

  case 32:
    ALLOCATE (layout[0].picture.data, char, (4*layout[0].picture.dx*layout[0].picture.dy + 32));
    memset (layout[0].picture.data, 0xbd, 4*layout[0].picture.dx*layout[0].picture.dy + 30);
    l = (unsigned int *) layout[0].picture.data;
    for (i = 0; i < (layout[0].picture.dx*layout[0].picture.dy); i++, l++)
      *l = start;
    layout[0].picture.pix = XCreateImage (graphic[0].display, graphic[0].visual, graphic[0].depth, ZPixmap, 0, 
					  layout[0].picture.data, layout[0].picture.dx, layout[0].picture.dy, 32, 0);
    break;
  }
    
}
