/*********************************************************

  QRTTarga is a post processor for QRT for the IBM PC/AT,
  etc.  It reads in the QRT file, and spits out a Targa (R)
  24 bits / pixel (16 million color BGR) format file which
  can be then be further postprocessed by another program,
  such as the Stone Soup Group's PICLAB (simply superb!).

  QRTTarga is based upon the original QRTPost program by
  Steve Koren (I think...).

  Adapted for Targa output and ported to the IBM PC World
  (Turbo-C) from Amiga-Land by Aaron A. Collins, 1/30/90.
	  
 *********************************************************/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAXXRES 640      /* max x resolution */

unsigned char r[MAXXRES], g[MAXXRES], b[MAXXRES];

void main(argc,argv)
int argc;
char *argv[];
{
	int xres, yres, xhi, yhi, line;
	register int x;
	char *index, inname[80], outname[80];
	FILE *in, *out;

	if (argc != 2)
	{
		printf("Usage: %s File[.RAW]\n",argv[0]);
		exit(1);
	}

	/** open files **/

	strcpy(inname, argv[1]);	  /* assume basename only initially */
	strcpy(outname, argv[1]);	  /* save original name */

	if ((in = fopen(argv[1], "rb")) == NULL)  /* try w/supplied ext. */
	{
		strcat(inname, ".RAW");		  /* then try w/.RAW ext. */
		if ((in = fopen(inname, "rb")) == NULL)
		{
			printf("Couldn't open file %s[.RAW]\n", argv[1]);
			exit(1);
		}
		strcat(outname, ".TGA");  /* if OK, it WAS a basename only! */
	}
	else	/* if successful in opening w/ supplied name... */
	{
		if ((index = strchr(outname, '.')) != NULL) /* find the dot */
			strcpy(index, ".TGA");    /* dot found=overwrite ext*/
		else	strcat(outname, ".TGA");
	}
	
	if ((out = fopen(outname, "wb")) == NULL)
	{
		printf("Couldn't open file %s\n", outname);
		fclose(in);
		exit(1);
	}

	/** write 1st part of standard Targa header **/

	for (x = 0; x < 12; x++)	/* 00, 00, 02, then 9 00's... */
		if (x == 2)
			fputc(x, out);
		else	fputc(0, out);

	/** load x and y resolution, and rewrite as rest of Targa hdr **/

	fputc(xres = fgetc(in), out);
	fputc(xhi = fgetc(in), out);
	xres += ((unsigned int) xhi) << 8;

	fputc(yres = fgetc(in), out);
	fputc(yhi = fgetc(in), out);
	yres += ((unsigned int) yhi) << 8;

	fputc(24, out);		/* 24 bits/pixel (16 million colors!) */
	fputc(32, out);		/* Not sure about this one, "out of 32" ? */
	
	/** print raw file info out to user **/

	printf("\nInput  file   = %s\n", inname);
	printf("Output  file  = %s\n\n", outname);
	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))
		break;

		if (line >= yres)
		{
			printf("Corrupt data file at line: %d.\n",line);
			break;
		}

		printf("\b\b\b%3d", line);	/* disp. current line # */

		fread(r, 1, xres, in);

/*		for (x = 0; x < xres; x++)	* do red color data */
/*			r[x] = (unsigned char) (fgetc(in) & 0xff); */

		fread(g, 1, xres, in);

/*		for (x = 0; x < xres; x++)	* do green color data */
/*			g[x] = (unsigned char) (fgetc(in) & 0xff); */

		fread(b, 1, xres, in);

/*		for (x = 0; x < xres; x++)	* do blue color data */
/*			b[x] = (unsigned char) (fgetc(in) & 0xff); */

		for (x = 0; x < xres; x++)	/* now write out data */
		{
			fputc(b[x], out);	/* Note odd BGR format! */
			fputc(g[x], out);
			fputc(r[x], out);
		}
	}
	printf("\n");
	fclose(in);                                 /* close files */
	fclose(out);
}
