/*

	clip.c

	cl1.bat = cl /AL clip.c

Reads an 8 bit image and

(1)   clips and/or samples it as is,

(2)   rescales a b&w image from 0 - 63 and makes
      a color table from 64 - 255 with 6 reds, 8 greens and 4 blues

clip [infile] [irw] [icl] [outfile] [rw1] [cl1] [orw] [ocl] [sample] [/iop]

where:
   infile --- input file name with extension
   irws ----- number of rows in input image
   icls ----- number of columns in input image
   outfile -- output file name without extension
   rw1 ------ first row in input image to use in output image
   cl1 ------ first column in input image to use in output image
   orws ----- number of rows in output image
   ocls ----- number of columns in output image
   iop ------ /1 for clip and sample:
              /2 for grey scale compression to 64 and color above



*/
#include <stdio.h>
#include <string.h>

#define NUMRED 6
#define NUMGRN 6
#define NUMBLU 5
#define MAXPTS 25000
#define PAUSE printf("HIT ANY KEY TO CONTINUE.\n");getch()

unsigned char Buffer[MAXPTS];
int Red[NUMRED]={0,51,102,153,204,255};
int Grn[NUMGRN]={0,51,102,153,204,255};
int Blu[NUMBLU]={0,64,128,191,255};

main(int argc,char *argv[])

{
	long i,j,k,l,m,n,r,g,b;
	long irow,icol,orow,ocol,row1,col1;
	char infile[100],outname[100],string[100],cr=0xd;
	FILE *fpin,*fplbl,*fppal,*fpdat,*fppal2;
	long offset,sample=1;
	char iop;

	if(argc<10)
	{
		printf("Not enough parameters\n");
		print_format();
	}
	strcpy(infile,argv[1]);
	fpin=fopen(infile,"rb");
	if(!fpin)
		not_open(infile,"read");
	if(sscanf(argv[2],"%ld",&irow)<1)
		not_read("irow");
	if(sscanf(argv[3],"%ld",&icol)<1)
		not_read("icol");
	if(sscanf(argv[5],"%ld",&row1)<1)
		not_read("row1");
	if(sscanf(argv[6],"%ld",&col1)<1)
		not_read("col1");
	if(sscanf(argv[7],"%ld",&orow)<1)
		not_read("orow");
	if(sscanf(argv[8],"%ld",&ocol)<1)
		not_read("ocol");
	if(sscanf(argv[9],"%ld",&sample)<1)
		not_read("sample");
	if(argv[10][0]!='/')
		not_read("/iop");
	iop=argv[10][1];
	if(iop!='1'&&iop!='2')
		not_read("/iop");
	strcpy(outname,argv[4]);
	sprintf(string,"%s.lbl",outname);
	fplbl=fopen(string,"wt");
	if(!fplbl)
		not_open(string,"write");
	if(iop=='1')
	{
		strcpy(string,infile);
		for(k=0;k<strlen(string);k++)
			if(string[k]=='.')
				string[k]='\0';
		strcat(string,".pal");
		fppal=fopen(string,"rt");
		if(!fppal)
		{
			printf("Could not open '%s' to read.\n\n",string);
			PAUSE;
		}
		sprintf(string,"%s.pal",outname);
		fppal2=fopen(string,"wt");
		if(!fppal2)
		{
			printf("Could not open '%s' to write.\n\n",string);
			PAUSE;
		}
	}
	while(fgets(string,100,fppal))
		fprintf(fppal2,"%s",string);
	fprintf(fplbl,"FILE_TYPE          = IMAGE\n");
	fprintf(fplbl,"IMAGE_LINES        =  %6ld\n",orow/sample);
	fprintf(fplbl,"LINE_SAMPLES       =  %6ld\n",ocol/sample);
	fprintf(fplbl,"IMAGE_POINTER      = '%s.dat'\n",outname);
	if(iop=='2'||(fppal&&fppal2))
		fprintf(fplbl,"PAL_POINTER        = '%s.pal'\n",outname);
	fprintf(fplbl,"Source image       = '%s'\n",infile);
	fprintf(fplbl," Source_Image_Rows = %6ld\n",irow);
	fprintf(fplbl," Source_Image_Cols = %6ld\n",icol);
	fprintf(fplbl," Source_Image_Row1 = %6ld\n",row1);
	fprintf(fplbl," Source_Image_Col1 = %6ld\n",col1);
	if(sample!=1)
	{
		fprintf(fplbl," Sample_Rate       = %6ld\n",sample);
		fprintf(fplbl," OutPut_Rows       = %6ld\n",orow);
		fprintf(fplbl," OutPut_Cols       = %6ld\n",ocol);
	}
	fprintf(fplbl,"END\n");
	fclose(fplbl);
	fclose(fppal);
	fclose(fppal2);
	if(iop=='2')
	{
		sprintf(string,"%s.pal",outname);
		fppal=fopen(string,"wt");
		if(!fppal)
			not_open(string,"write");
		for(i=0;i<64;i++)
			fprintf(fppal,"%3ld  %3ld %3ld %3ld\n",i,i*4,i*4,i*4);
		n=64;
		for(r=0;r<NUMRED;r++)
			for(g=0;g<NUMGRN;g++)
				for(b=0;b<NUMBLU;b++)
					fprintf(fppal,"%3ld  %3d %3d %3d\n",n++,Red[r],Grn[g],Blu[b]);
		while(n<256)
			fprintf(fppal,"%3ld  255 255 255\n",n++);
		fclose(fppal);
	}
	sprintf(string,"%s.dat",outname);
	fpdat=fopen(string,"wb");
	if(!fpdat)
		not_open(string,"write");
	for(i=0;i<orow/sample;i++)
	{
		printf("%c%6ld of %d",cr,i+1,orow/sample);
		offset=(i*sample+row1)*icol+col1;	/* 1st pixel of output line */
		fseek(fpin,offset,SEEK_SET);
		fread((char *)Buffer,sizeof(char),(int)ocol,fpin);
		if(iop=='2')
			for(j=0;j<ocol/sample;j++)
				Buffer[j]=Buffer[j*sample]/4;
		else
			for(j=0;j<ocol/sample;j++)
				Buffer[j]=Buffer[j*sample];
		fwrite((char *)Buffer,sizeof(char),(int)(ocol/sample),fpdat);
	}
	printf("\n");
	fclose(fpdat);
	fclose(fpin);
}


/**********************************************************************
**
**
**
**********************************************************************/

int not_read(char *what)

{
	printf("Could not read '%s'\n",what);
	print_format();
}



/**********************************************************************
**
**
**
**********************************************************************/

int not_open(char *filename,char *action)

{
	printf("in not_open\n");
	printf("Could not open '%s' to %s.\n\n",filename,action);
	exit(0);
}



/**********************************************************************
**
**
**
**********************************************************************/

int print_format()

{
	printf("Command Line:\n");
	printf(
"clip [infil] [irw] [icl] [outfil] [rw1] [cl1] [orw] [ocl] [sample] [/iop]\n");
	printf("\nwhere:\n");
	printf("   infil --- input file name with extension\n");
	printf("   irw ----- number of rows in input image\n");
	printf("   icl ----- number of columns in input image\n");
	printf("   outfil -- output file name without extension\n");
	printf("   rw1 ----- first row in input image to use in output image\n");
	printf("   cl1 ----- first column in input image to use in output image\n");
	printf("   orw ----- number of rows to output from input image\n");
	printf("   ocl ----- number of columns to output from input image\n");
	printf("   sample -- OPTIONAL sample rate ( 2 will give orow/2 x ocol/2\n");
	printf("   iop ----- /1 for clip and sample:\n");
	printf(
		"             /2 for grey scale compression to 64 and color above\n");
	printf("Example:\n");
	printf("clip test1.dat 2000 4000 test2 1500 3500 500 500 2 /1\n");
	printf(
		"Will clip a 500x500 pixel piece out of the bottom right corner of\n");
	printf(
		"'test1.dat' (which is 2000 rows by 4000 columns), put it in \n");
	printf(
		"'test2.dat and reduce it to 250x250 pixels by sampling every\n");
	printf(
		"2nd pixel and every 2nd row.  It will also make 'test2.lbl' \n");
	printf(
		"and 'test2.pal'as needed.\n");
	exit(0);
}


                                                                                                                            