/***********************************************************
*
*	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 file combines two images into a single image which
*	may be viewed with red/blue 3D glasses (not included).
*	To generate stereo pairs, use MC to generate two images
*	in which everything is the same but is moved slighty to
*	the side in one of these.  Then run them through this
*	program, then through arrt2qrt (found elsewhere in this
*	archive), and finally, use ADPro (or some other program,
*	also not included) to generate a viewable picture.
*
************************************************************/

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

far unsigned char lbuff[8192*3], rbuff[8192*3], obuff[8192*3];


main(int argc, char *argv[])
{
	int	lfh, rfh, ofh,
		xsize, ysize,
		x, y;

	if (argc != 4)
	{
		printf("usage: combine leftfile rightfile outfile\n");
		exit(10);
	}

	lfh = open(argv[1], O_RDONLY);
	rfh = open(argv[2], O_RDONLY);
	ofh = open(argv[3], O_WRONLY | O_CREAT, 0644);

	read(lfh, &xsize, sizeof(int));
	read(lfh, &ysize, sizeof(int));

	read(rfh, &x, sizeof(int));
	read(rfh, &y, sizeof(int));

	if ((x != xsize) || (y != ysize))
	{
		printf("image sizes do not match");
		exit(10);
	}

	write(ofh, &xsize, sizeof(int));
	write(ofh, &ysize, sizeof(int));

	for (y=0; y<ysize; y++)
	{
		read(lfh, lbuff, xsize*3);
		read(rfh, rbuff, xsize*3);

		for (x=0; x<xsize; x++)
		{
			obuff[x*3] = (lbuff[x*3]+lbuff[x*3+1]+lbuff[x*3+2])/3;
			obuff[x*3+1] = 0;
			obuff[x*3+2] = (rbuff[x*3]+rbuff[x*3+1]+rbuff[x*3+2])/3;
		}

		write(ofh, obuff, xsize*3);
	}

	close(lfh);
	close(rfh);
	close(ofh);
}
		
	
