/***********************************************************
*
*	This file is Copyright © 1990-1994 Dan Wesnor
*
*	This file and any executables resulting from compilation
*	of this file are freely distributable so long as the
*	above copyright statement is included intact.  This
*	statement of distributability is limited to this file
*	only, and does not include any other file in this
*	archive.
*
*	No gaurantees of usability are made for this file or
*	any executables generated from it.  Use at your own risk.
*
************************************************************
*
*	This program converts raw24 files as output by Magic
*	camera to QRT format.  Use it as an examlpe of how
*	to read or write raw24 files.
*
***********************************************************/

#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>

far char 	inbuff[8192*3],
			red[8192], green[8192], blue[8192];


void swapbytes(char *b)
{
	char	t;

	t = b[0];
	b[0] = b[1];
	b[1] = t;
}




main(int argc, char *argv[])
{
	int	x, y,
		i, j,
		ifh, ofh;
	short	sx, sy, line;

	if (argc != 3)
	{
		printf("usage:  arrt2qrt in out\n");
		exit(100);
	}

	ifh = open(argv[1], O_RDONLY);
	ofh = open(argv[2], O_WRONLY | O_CREAT);

	if ((ifh == -1) || (ofh == -1))
	{
		printf("arrt2qrt:  could not open files\n");
		exit(100);
	}

	read(ifh, &x, 4);
	read(ifh, &y, 4);

	printf("image is %dx%d\n", x, y);

	sx = x;
	sy = y;

	swapbytes((char *)&sx);
	swapbytes((char *)&sy);

	write(ofh, &sx, 2);
	write(ofh, &sy, 2);

	for (i=0; i<y; i++)
	{
		printf("working line %d\x0d", i);
		fflush(stdout);

		line = i;
		swapbytes((char *)&line);
		write(ofh, &line, 2);

		read(ifh, inbuff, x*3);

		for (j=0; j<x; j++)
		{
			red[j] = inbuff[j*3];
			green[j] = inbuff[j*3+1];
			blue[j] = inbuff[j*3+2];
		}

		write(ofh, red, x);
		write(ofh, green, x);
		write(ofh, blue, x);
	}

	close(ofh);
	close(ifh);

	printf("\ndone\n");
}


