/*********************************************************

  QRTSumm is a post processor for QRT for the IBM PC/AT,
  etc.  It reads in the QRT .RAW file, and displays some
  heuristic information about the .RAW file and pallette
  used, presumably as a forerunner to a dithering program.

  QRTSumm is based upon the original QRTPost program by
  Steve Koren (I think...).

  Adapted for RAW output and ported to the IBM PC World
  (Turbo-C) from Amiga-Land by Aaron A. Collins, 1/30/90.

 *********************************************************/

#include <stdio.h>
#include <stdlib.h>

#define MAXXRES 640      /* max x resolution */

void main(argc,argv)
int argc;
char *argv[];
{
	int xres, yres, line;
	register int x;
	long r[MAXXRES], g[MAXXRES], b[MAXXRES];  /* color hit counters */
	FILE *in;
	int rlo, rhi, glo, ghi, blo, bhi;	/* low-high values */

	rlo = rhi = glo = ghi = blo = bhi = 0; /* no limits established yet */

	for (x = 0; x <= 255; x++)		/* Zero hit counter arrays */
		r[x] = g[x] = b[x] = 0L;

	if (argc != 2)
	{
		printf("Usage: %s InFile\n",argv[0]);
		exit(1);
	}

	/** open file **/

	if ((in = fopen(argv[1], "rb")) == NULL)
	{
		printf("Couldn't open file %s\n",argv[1]);
		exit(1);
	}

	/** load x and y resolution **/

	xres = yres = 0;

	xres  = ((unsigned int)fgetc(in));
	xres += ((unsigned int)(fgetc(in) << 8));

	yres  = ((unsigned int)fgetc(in));
	yres += ((unsigned int)(fgetc(in) << 8));

	/** print info **/

	printf("\nInput  file  = %s\n",   argv[1]);
	printf("X resolution = %d\n",     xres);
	printf("Y resolution = %d\n\n",   yres);

	printf("Processing Line:   0");

	while (!feof(in))
	{
		line  = ((int)fgetc(in));	/* read scan line number */
		line |= ((int)(fgetc(in) << 8));

		if (feof(in))			/* are we done? */
			break;

		printf("\b\b\b%3d", line);	/* disp. current line # */

		if (line >= yres)
		{
			printf("\nCorrupted data file - line #: %d!\n",line);
			fclose(in);			/* close file */
			exit(1);
		}

		for (x=0; x<xres; x++)                   /* read color data */
			r[((short)fgetc(in)) & 0xff]++;	 /* count red hits */

		for (x=0; x<xres; x++)
			g[((short)fgetc(in)) & 0xff]++;	 /* count green hits*/

		for (x=0; x<xres; x++)
			b[((short)fgetc(in)) & 0xff]++;	 /* count blue hits */

	}
	fclose(in);				/* close file */

	printf("\n\nPallette Information Summary:\n\n");

	for (x = 0; x <= 255; x++)	/* scan colors for lowest used */
	{
		if (!rlo)
			if (r[x] != 0L)
				rlo = x;  /* note lowest red value used */
		if (!glo)
			if (g[x] != 0L)
				glo = x;  /* note lowest grn value used */
		if (!blo)
			if (b[x] != 0L)
				blo = x;  /* note lowest blu value used */
	}
	for (x = 255; x >= 0; x--)	/* scan colors for lowest used */
	{
		if (!rhi)
			if (r[x] != 0L)
				rhi = x;  /* note highest red value used */
		if (!ghi)
			if (g[x] != 0L)
				ghi = x;  /* note highest grn value used */
		if (!bhi)
			if (b[x] != 0L)
				bhi = x;  /* note highest red value used */

	}

	printf("Highest color values used - R: %3d, G: %3d, B: %3d\n", rhi, ghi, bhi);
	printf("Lowest  color values used - R: %3d, G: %3d, B: %3d\n", rlo, glo, blo);
	exit(0);				/* normal successful exit */
}
