/*		Image Enhancement Programme	*/
/*		S.D.Price 			*/
/*		July 98				*/
/*		Compiled using North C          */
/*		By Steve Hawtin                 */


#include <stdio.h>

#define MAXWIDTH 	1024

main(argc,argv)
int 	argc;
char 	*argv[];

{	


	FILE	 *fpsrc,*fpout;
	float	 const;
	short	 count=0,count2=0,width,height,oldheight,pad,loop,count3;
	unsigned char	input,char1=0,widthlsb,widthmsb,heightlsb,heightmsb,outnme[32];
	unsigned char	value,tempval[2];
	unsigned char	botval[3][MAXWIDTH],mark,mark2;

/* ----------- Get command line args and check for problems, open files. ----------------- */


	if(argc!=2&&argc!=3)
	{
		puts("\nFormat:\nHam32k <Inputfile> [Outputfile]\n\n");
		exit(1);
	}
		
	fpsrc=fopen(argv[1],"r");
	if(fpsrc==(FILE *)NULL)
	{
		puts("\nCant find input file.\n");
		exit(1);
	}
	
	if(argc==2){
		strcpy(outnme,argv[1]);
		strcat(outnme,".32k");}
	else{
		strcpy(outnme,argv[2]);}

	fpout=fopen(outnme,"r");
	if(fpout!=(FILE *)NULL)
	{
		puts("\nOutput file already exists!\n");
		exit(1);
	}
	fpout=fopen(outnme,"w");
	if(fpout==(FILE *)NULL)
	{
		puts("\nI cant open the output file!\n");
		exit(1);
	}
	

/*-------------------------- Begining of main part of program. ------------------------------------*/

	puts("\nImage enhancement programme,\nBy SDPrice 1998\n");  /* Prog start banner */

	for(count=0;count!=18;count++)
	{	char1=fgetc(fpsrc); 		/* Read in and output first part of file header.   */
		fputc(char1,fpout);}
	
	widthlsb=fgetc(fpsrc);               /* Read in width LeastSignificantByte  */
	widthmsb=fgetc(fpsrc);               /* Read in width MostSB                */
	width=widthlsb+widthmsb*256;         /* Calculate value of width            */
	if(width>MAXWIDTH){
		printf("\nSorry this release can only handle files up to %d wide.",MAXWIDTH);
		exit(1);}

	fputc(widthlsb,fpout);            /* Output the width value to           */
	fputc(widthmsb,fpout);            /* File header of the output file.     */

	char1=fgetc(fpsrc);         /* Read in and                            */
	fputc(char1,fpout);         /* Output the next 2 bytes of the header. */
	char1=fgetc(fpsrc); 
	fputc(char1,fpout);

	heightlsb=fgetc(fpsrc);     /* Now read in and convert the height value.   */
	heightmsb=fgetc(fpsrc); 

	height=(heightlsb+heightmsb*256)*2;  /* Times the height value by 2 before outputting */
	oldheight=height/2;                  /* Added later to speed up loop.      */
	heightmsb=height/256;                /* Reconvert to LSB and MSB   before             */
	heightlsb=height%256;
	fputc(heightlsb,fpout);              /* Outputting to the out file.                   */
	fputc(heightmsb,fpout);
	printf("Output will be called %s\n",outnme);
	printf("BMP file.       Original dimensions: %d X %d\n",width,oldheight);   
	printf("                New dimensions: %d X %d\n",width,height);
	puts("File conversion will take some time.\nPercent complete =   0%\b\b\b\b");
	
	pad=4-((width*3)%4);         /* Calculate no. of padding 0's per line required.  */
	pad=pad&3;

	/*  Now at pos 24, data starts at 109.   Read in and output rest of header.  */
	for(count=24;count<54;++count)
	{	char1=fgetc(fpsrc); 		/* Read in and output last part of file header.   */
		fputc(char1,fpout);
	}
	
/* ---------------------------------- Begin processing. ----------------------------------------- */

for(loop=0;loop<oldheight;++loop)   /*  Loop for all lines.   */
{
	mark=0;
	mark2=1;
	for(count=0;count<width;++count)   /*  Repeat for 1 whole line   */
	{
		for(count2=0;count2<3;++count2)    /* Loop 3 times for R G B   */ 
		{
			value=fgetc(fpsrc);		/* Read in the R G B values. */
			tempval[mark]=value&240;		/* Split into top and bottom pixels */
			tempval[mark2]=(((value&8) && value<240) ? tempval[mark]+16 : tempval[mark]); 
			fputc(tempval[0],fpout);                /* Output top row pixel. */
			botval[count2][count]=tempval[1];
		}
		mark=!mark;
		mark2=!mark2;
	}
/*  Deal with the padding at the end of each line.   */
	for(count3=0;count3<pad;++count3){
		char1=fgetc(fpsrc);
		if(char1!=0){
			puts("\nBug in program!\n\n");  /* Padding is always a zero! */
			exit(1);}
		fputc(0,fpout);
	}
/* Write out the bottom row of pixels  */
	for(count=0;count<width;++count){           /* Loop till end of line.  */
		fputc(botval[0][count],fpout);      /* Output Red,     */
		fputc(botval[1][count],fpout);      /* Green,          */
		fputc(botval[2][count],fpout);      /* and Blue pixels */
	}
	for(count3=0;count3<pad;++count3)    /* Finaly write out the padding  */
		fputc(0,fpout);	
	
	const=(100.0/(float)oldheight)*((float)loop+1);
	printf("%3d\b\b\b",(unsigned)const);  /* Completion report. */
	fflush(stdout);
}

	fclose(fpout);
	fclose(fpsrc);
	
	puts("\nAll finnished!\n\n");
}


