#include "cii.h"
#include "cii←matrix.h"
#include "ciihtd.h"
#include <fcntl.h>
#include <stdio.h>
#include <malloc.h>
#include <string.h>

void exit();

/* GLOBAL VARIABLES */
static int sSizeBuffer = 2550;
static int fSizeBuffer = 3328;
static char* bufferBasePointer = (char*)0;
/* END OF GLOBAL VARIABLES */

extern CII←Handle
CII←TestDevice() {
  float zero = 0.0;
  float one = 1.0;
  CII←RectangleRep bigrect = { 0.0, 0.0, 99999999.0, 99999999.0 };
  CII←Handle h;
  CII←HTDParamRep pr;
  CII←RasterRep raster;
  float matrix[6];
  if (!bufferBasePointer) {
    bufferBasePointer = (char*)malloc(sSizeBuffer*fSizeBuffer/8);
  };
  raster.sMinBox = 0;
  raster.fMinBox = 0;
  raster.sMaxBox = sSizeBuffer;
  raster.fMaxBox = fSizeBuffer;
  raster.bitsPerSample = 1;
  raster.bitsPerLine = fSizeBuffer;
  raster.basePointer = (void*)bufferBasePointer;
  raster.bitIndex = 0;
  raster.ref = (void*)0;
  
  CII←HTD←SetDefaults(&pr);
  pr.sSizeDevice = sSizeBuffer;
  pr.fSizeDevice = fSizeBuffer;
  pr.raster = &raster;
  pr.surfaceUnitsPerInch.x = 300.0;
  pr.surfaceUnitsPerInch.y = 300.0;
  
  CII←HTD←Create(&pr, &h);
  CII←GetMatrix(h, matrix);
  printf("Context created\n");
  CIU←ApplyPreScale(300.0/72.0, matrix);
  CII←SetMatrix(h, matrix);
  CII←SetGray(h, &one); /* white */
  CII←MaskRectangle(h, &bigrect);
  CII←SetGray(h, &zero); /* black */
  printf("Matrix = [ %g %g %g %g %g %g ]\n",
  	(double)matrix[0],
  	(double)matrix[1],
  	(double)matrix[2],
  	(double)matrix[3],
  	(double)matrix[4],
  	(double)matrix[5]
  	);
  return(h);
}

static void
writeresult(char* filename)
{
  int fd;
  char headerbuf[256];
  if (!bufferBasePointer) {
    printf("writeresult failed - no buffer");
    exit(1);
  };
  fd = open(filename, O←WRONLY+O←CREAT, 0666);
  sprintf(headerbuf, "P4\n%d\n%d\n", fSizeBuffer, sSizeBuffer);
  if (1>fd) {printf("open failed"); exit(1);}
  if (1>write(fd, headerbuf, strlen(headerbuf)))
     {printf("write failed: "); printf(filename); exit(1);}
  if (1>write(fd, bufferBasePointer, sSizeBuffer*fSizeBuffer/8))
     {printf("write failed: "); printf(filename); exit(1);}
  if (0!=close(fd)) {printf("close failed"); exit(1);}
  printf("wrote "); printf(filename); printf("\n");
}

static void graytest () {
CII←Handle h;
CII←RectangleRep rect;
float half = 0.5;
rect.x = 0.0;
rect.y = 0.0;
rect.w = 10.0;
rect.h = 10.0;
h = CII←TestDevice();
CII←SetGray(h, &half);
CII←MaskRectangle(h, &rect);
CII←Destroy(h);
}

extern int
main() { int i;
  (void)BonsaiCIIInit();
  graytest();
  writeresult("graytest.pbm");
  
  (void)XR←run←ciiicecream();
  writeresult("ciiicecream.pbm");
  
  (void)XR←run←ciimaskprogramexamples();
  writeresult("ciimaskprogramexamples.pbm");
  
  (void)XR←run←ciitest();
  writeresult("ciitest.pbm");
  
  (void)XR←run←ciitestbitmaps();
  writeresult("ciitestbitmaps.pbm");
  
  (void)XR←run←ciitestboxes();
  writeresult("ciitestboxes.pbm");
  
  (void)XR←run←ciitestbufferedseparationimage();
  writeresult("ciitestbufferedseparationimage.pbm");
  
  (void)XR←run←ciitestbuildclip();
  writeresult("ciitestbuildclip.pbm");
  
  (void)XR←run←ciitestbuildclip2();
  writeresult("ciitestbuildclip2.pbm");
  
  (void)XR←run←ciitesthalftone();
  writeresult("ciitesthalftone.pbm");
  
  (void)XR←run←ciitestimage();
  writeresult("ciitestimage.pbm");
  
  (void)XR←run←ciitestimage3();
  writeresult("ciitestimage3.pbm");
  
  (void)XR←run←ciitestmaskstreambits();
  writeresult("ciitestmaskstreambits.pbm");
  
  (void)XR←run←ciitestpcl5printmodel();
  writeresult("ciitestpcl5printmodel.pbm");
  
  (void)XR←run←ciitesttransfer();
  writeresult("ciitesttransfer.pbm");
  
  (void)XR←run←ciitestcleanup();
  writeresult("ciitestcleanup.pbm");
  
  exit(0);
}