/* (c) SPDsoft & GTIC 16 julio 1993*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "pict.h"
#ifdef THINK_C
#include <console.h>
#endif
#define MAX_PICT	32
#ifdef MYGETOPT
char getopt( int	argc, char	*argv[], char *format );
#endif

void	usage(char *name);
void	fatalError(char *name,char *str);

void	usage(char *name)
{
	fprintf(stderr,
	"usage: %s [-o output]{-s format -f first -l last }| files...\n",name);
	exit(-1);
}

void	fatalError(char *name,char *str)
{
	fprintf(stderr,
	"%s: Fatal Error: %s. bye...\n",name,str);
	exit(-2);
}

main(int argc,char *argv[])
{
	char *fname="mrgout.pict",format;
	char tmpStr[32];

	unsigned int first=0, last=0, NumOfSFrames;
	int FormatOpt=0, OutputOpt=0;
	char Verbose=0;


	Pict pictIn[MAX_PICT],pictOut;
	int	np,i,j,opcion;
	int	theError=0;
	int	ih=0, iw=0;
	unsigned long int subTotal;

	extern char     *optarg;
	extern int      optind,opterr;

#ifdef THINK_C
	argc = ccommand(&argv);
#endif
	while ( (opcion=getopt(argc,argv,"o:s:f:l:vh")) != EOF )
	{
		switch(opcion)
		{
			case 'o':	theError += ( *optarg==0x00 );
					OutputOpt=optind-1;
					break;
			case 's':	theError += ( *optarg==0x00 );
					FormatOpt=optind-1;
					break;
			case 'f':	theError += ( *optarg==0x00 );
					first = atoi(optarg);
					break;
			case 'l':	theError += ( *optarg==0x00 );
					last = atoi(optarg);
					break;
			case 'v':	printf("%s:v. 22 octubre 1993\n",argv[0]);
					Verbose=1;
					break;
			defatult:	
						usage(argv[0]);
		}
	}

	if ( theError!=0)  usage(argv[0]);

	if ( (FormatOpt!=0)&&((first<=0)||(last<=0)||(last<=first)))
	{
		fprintf(stderr,"Option s requires first & last > 0\n");
		usage(argv[0]);
	}

	if ( (FormatOpt!=0)&&(optind!=argc))
	{
		fprintf(stderr,"%s: Warning! Files ignored\n",argv[0]);
	}


	if( FormatOpt == 0 )
	{
		first=optind;
		last=argc-1;
	}

	NumOfSFrames=last-first+1;

	if ( NumOfSFrames==0 ) usage(argv[0]);

	if(Verbose)
	{
	printf("%s:output)\t%s\n",argv[0],OutputOpt!=0?argv[OutputOpt]:fname);
	printf("%s:NumFrames)\t%d\n",argv[0],NumOfSFrames);
	}

	for(i=first;i<=last;i++)
	{
		if( FormatOpt != 0 )
			sprintf(tmpStr,argv[FormatOpt],i);
		else
			sprintf(tmpStr,"%s",argv[i]);

		np=i-first;

		if(Verbose) printf("%d)\t%s\n",np,tmpStr);

		pictIn[np] = pict_open(tmpStr,P_READ,0,0,0,NULL);
		if(pictIn[np]->n_comp==1)
			fatalError(argv[0],"Not True Color!");

		if (iw==0)
		{
			iw=pictIn[np]->image_W;
			ih=pictIn[np]->image_H;
		}
		else
		{
			if((iw!=pictIn[np]->image_W)||
			(ih!=pictIn[np]->image_H))
			fatalError(argv[0],"Dim Error!");
		}
		if ((pictIn[np]->pixels =
				(U_char*)malloc(iw *pictIn[np]->n_comp))==NULL)
			fatalError(argv[0],"Buy more memory!");
	}
	pictOut = pict_open(OutputOpt!=0?argv[OutputOpt]:fname,P_WRITE,
				ih,iw,pictIn[0]->clr_mode,pictIn[0]->map);
	if ((pictOut->pixels =
		(U_char*)malloc(iw *pictIn[0]->n_comp))==NULL)
			fatalError(argv[0],"Buy more memory!");

	/******************************************************************/
	/* This is the real work */
	for(j=0;j<ih;j++)
	{
		for(np=0;np<NumOfSFrames;np++)
			pict_get_line(pictIn[np],pictIn[np]->pixels);

		for(i=0;i<iw*pictIn[0]->n_comp;i++)
		{
			subTotal = 0;
			for(np=0;np<NumOfSFrames;np++)
				subTotal += (unsigned long)*
						(pictIn[np]->pixels+i);

			subTotal /= NumOfSFrames;
			*(pictOut->pixels+i)=(unsigned char)subTotal;
		}/* columns */
		pict_put_line(pictOut,pictOut->pixels);
	}/* lines */

	/* End of real work */
	/******************************************************************/

	for(i=first;i<=last;i++)
	{
		pict_close(pictIn[i-first]);
		pict_free(pictIn[i-first]);
	}
	pict_close(pictOut);
	pict_free(pictOut);
}