#include <conio.h>
#include <stdio.h>
#include <stdlib.h>

unsigned char   far *plane=(unsigned char far *)0xc0000000l;
unsigned char   plane0;
unsigned char   plane1;
unsigned char   plane2;
unsigned char   plane3;
unsigned char   buf[512]={
			0x49,0x49,0x2a,0x00,0x08,0x00,0x00,0x00,
			0x10,0x00,0xfe,0x00,0x04,0x00,0x01,0x00,
			0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
			0x03,0x00,0x01,0x00,0x00,0x00,0x80,0x02,
			0x00,0x00,0x01,0x01,0x03,0x00,0x01,0x00,
			0x00,0x00,0x90,0x01,0x00,0x00,0x02,0x01,
			0x03,0x00,0x01,0x00,0x00,0x00,0x04,0x00,
			0x00,0x00,0x03,0x01,0x03,0x00,0x01,0x00,
			0x00,0x00,0x01,0x00,0x00,0x00,0x06,0x01,
			0x03,0x00,0x01,0x00,0x00,0x00,0x03,0x00,
			0x00,0x00,0x0a,0x01,0x03,0x00,0x01,0x00,
			0x00,0x00,0x02,0x00,0x00,0x00,0x11,0x01,
			0x04,0x00,0x01,0x00,0x00,0x00,0x00,0x02,
			0x00,0x00,0x15,0x01,0x03,0x00,0x01,0x00,
			0x00,0x00,0x01,0x00,0x00,0x00,0x16,0x01,
			0x04,0x00,0x01,0x00,0x00,0x00,0x90,0x01,
			0x00,0x00,0x17,0x01,0x04,0x00,0x01,0x00,
			0x00,0x00,0x00,0xf4,0x01,0x00,0x19,0x01,
			0x03,0x00,0x01,0x00,0x00,0x00,0x0f,0x00,
			0x00,0x00,0x1a,0x01,0x05,0x00,0x01,0x00,
			0x00,0x00,0xf0,0x01,0x00,0x00,0x1b,0x01,
			0x05,0x00,0x01,0x00,0x00,0x00,0xf8,0x01,
			0x00,0x00,0x1c,0x01,0x03,0x00,0x01,0x00,
			0x00,0x00,0x01,0x00,0x00,0x00,0x40,0x01,
			0x03,0x00,0x30,0x00,0x00,0x00,0x00,0x01,
			0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
			0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
			0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
			0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
			0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
			0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
			0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
			0x00,0x00,0x00,0x00,0x7f,0x7f,0x7f,0x7f,
			0x00,0x00,0x00,0x00,0x7f,0x7f,0xa0,0xa0,
			0x40,0x40,0x00,0x00,0xff,0xff,0xff,0xff,
			0x00,0x00,0x00,0x00,0xff,0xff,0xff,0xff,
			0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
			0x7f,0x7f,0x7f,0x7f,0x7f,0x7f,0xa0,0xa0,
			0x40,0x40,0x00,0x00,0x00,0x00,0x00,0x00,
			0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
			0x00,0x00,0x7f,0x7f,0x00,0x00,0x7f,0x7f,
			0x00,0x00,0x7f,0x7f,0x00,0x00,0xa0,0xa0,
			0x40,0x40,0xff,0xff,0x00,0x00,0xff,0xff,
			0x00,0x00,0xff,0xff,0x00,0x00,0xff,0xff,
			0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
			0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
			0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
			0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
			0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
			0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
			0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
			0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
			0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
			0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
			0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
			0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
			0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
			0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
			0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
			0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
			0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
			0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
			0x4b,0x00,0x00,0x00,0x01,0x00,0x00,0x00,
			0x4b,0x00,0x00,0x00,0x01,0x00,0x00,0x00};


void main(int argc,char *argv[])
{
FILE            *fp;
int             i,t,r,g,b;
unsigned char   c1,c2,c;

	if(argc!=2){
	  printf("*****************************************\n");
	  printf("**  非圧縮16色TIFFセーバー  by N.Baba  **\n");
	  printf("*****************************************\n");
	  printf("TIFFS.EXE <savefilename>\n");
	  exit(0);
	  }

/*  パレットデータの読み取り  */
	for(i=0;i<16;i++){
	  outp(0xfd90,i);
	  b=(inp(0xfd92)>>4);
	  r=(inp(0xfd94)>>4);
	  g=(inp(0xfd96)>>4);
	  buf[256+i*2   ]=(unsigned char)(r|(r<<4));
	  buf[256+i*2+1 ]=(unsigned char)(r|(r<<4));
	  buf[256+i*2+32]=(unsigned char)(g|(g<<4));
	  buf[256+i*2+33]=(unsigned char)(g|(g<<4));
	  buf[256+i*2+64]=(unsigned char)(b|(b<<4));
	  buf[256+i*2+65]=(unsigned char)(b|(b<<4));
	  }

/*  保存  */
	fp=fopen(argv[1],"wb");
/*  ヘッダの保存  */
	fwrite(buf,1,512,fp);
/*  データの保存  */
	for(i=0;i<32000;i++){
	  for(t=0;t<8;t+=2){
	    plane[0xff81]=0x00;
	    plane0=plane[i];
	    plane[0xff81]=0x40;
	    plane1=plane[i];
	    plane[0xff81]=0x80;
	    plane2=plane[i];
	    plane[0xff81]=0xc0;
	    plane3=plane[i];

	    c1=((plane0&(0x1<<(7-t)))>>(7-t))|(((plane1&(0x1<<(7-t)))>>(7-t))<<1)|(((plane2&(0x1<<(7-t)))>>(7-t))<<2)|(((plane3&(0x1<<(7-t)))>>(7-t))<<3);
	    c2=((plane0&(0x1<<(6-t)))>>(6-t))|(((plane1&(0x1<<(6-t)))>>(6-t))<<1)|(((plane2&(0x1<<(6-t)))>>(6-t))<<2)|(((plane3&(0x1<<(6-t)))>>(6-t))<<3);
	    c=(c1|(c2<<4));
	    fwrite(&c,1,1,fp);
	    }
	  }
	fclose(fp);
}
