/*---------------------------------------------------------------------------*/
/*  2-17-2006 rbg2tiff.c                                                     */
/* [Usage]  a.out R.raw G.raw B.raw Color.tif H W                            */
/*---------------------------------------------------------------------------*/
#include<stdlib.h>
#include<stdio.h>

                         /* Abort the process and print error message        */
Abort(const char *ErrMsg) { printf("\n%s\n",ErrMsg); exit(0); }

/*---------------------------------------------------------------------------*/
/* The byte order used within the Tiff file                                  */
/* Little endian: from least significant byte to most significant byte       */
/* Big endian   : otherwise                                                  */
/*---------------------------------------------------------------------------*/
enum tagByteOrder{Little,Big};
typedef enum tagByteOrder ByteOrder;

/*get the byte order which the system use*/
ByteOrder GetSystemOrder()
{ 	union tagDword
	{
          long Long;
	  char Byte[4];
	} Dword;

	Dword.Long=1;
	if (Dword.Byte[0]==1)
		return Little;
	else
		return Big;
}

/*write a long integer*/
WriteLong(FILE* hFile,long nValue,ByteOrder eByteOrder)
{	int i;
	char *pByte;
	if (eByteOrder==Little)
	{
		fwrite(&nValue,4,1,hFile);
		return;
	}
	pByte=(char *) &nValue;
	for (i=3;i>=0;--i)
		fwrite(pByte+i,1,1,hFile);
}

/*write a long integer*/
WriteShort(FILE* hFile,short nValue,ByteOrder eByteOrder)
{	int i;
	char * pByte;
	if (eByteOrder==Little)
	{
		fwrite(&nValue,2,1,hFile);
		return;
	}
	pByte=(char *)&nValue;
	for (i=1;i>=0;--i)
		fwrite(pByte+i,1,1,hFile);
}



/* line 64 */
char Header[]={
	0x49,0x49,0x2a,0x00,0x08,0x00,0x00,0x00,0x0c,0x00,
	0x00,0x01,0x03,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x01,0x01,0x03,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x02,0x01,0x03,0x00,0x03,0x00,0x00,0x00,0x9E,0x00,0x00,0x00,
	0x03,0x01,0x03,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x00,0x00,
	0x06,0x01,0x03,0x00,0x01,0x00,0x00,0x00,0x02,0x00,0x00,0x00,
	0x11,0x01,0x04,0x00,0x01,0x00,0x00,0x00,0xB1,0x00,0x00,0x00,              
	0x15,0x01,0x03,0x00,0x01,0x00,0x00,0x00,0x03,0x00,0x00,0x00,
	0x16,0x01,0x03,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x17,0x01,0x04,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x1A,0x01,0x05,0x00,0x01,0x00,0x00,0x00,0xA4,0x00,0x00,0x00,
	0x1B,0x01,0x05,0x00,0x01,0x00,0x00,0x00,0xAC,0x00,0x00,0x00,
	0x28,0x01,0x03,0x00,0x01,0x00,0x00,0x00,0x02,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,
	0x08,0x00,0x08,0x00,0x08,0x00,
	0x80,0xC3,0xC9,0x01,0x10,0x27,0x00,0x00,0x80,0xC3,0xC9,0x01,0x10,0x27,0x00,0x00};


main(int argc, char *argv[])
{ 	FILE *hTiff,*hRed,*hGreen,*hBlue;
	short nWidth,nHeight;
	long nLength,i;
	char RGB[3];
	long nCount;
	ByteOrder eByteOrder;

	eByteOrder=GetSystemOrder();
	if (argc !=7)
	{  printf("\ntiff2raw Red Green Blue OutputFile Height Width\n");
	   return;
	}
	hRed=fopen(argv[1],"rb");
	hGreen=fopen(argv[2],"rb");
	hBlue=fopen(argv[3],"rb");
    
    if (!hRed || !hGreen || !hBlue)
		Abort("Can not open the Source files.");

	hTiff=fopen(argv[4],"wb");
	if (!hTiff)
		Abort("Can not create the output file!");


	/*Get Width and Height from arguments*/
	nHeight=atoi(argv[5]);
	nWidth=atoi(argv[6]);
	
	nLength=nWidth*nHeight;

	/*write the header*/
	fwrite(Header,1,0xb1,hTiff);
	fseek(hTiff,18,SEEK_SET);
	WriteShort(hTiff,nWidth,eByteOrder);
	fseek(hTiff,30,SEEK_SET);
	WriteShort(hTiff,nHeight,eByteOrder);
	fseek(hTiff,102,SEEK_SET);
        WriteShort(hTiff,nHeight,eByteOrder);
	fseek(hTiff,114,SEEK_SET);
	WriteLong(hTiff,nLength,eByteOrder);



/* Line 128 */
/*---------------------------------------------------------------------------*/
/* Write to the RGB tiff data file                                           */
/*---------------------------------------------------------------------------*/
	fseek(hTiff,0,SEEK_END);
	for (i=0;i<nLength;++i)
	{ nCount=fread(RGB,1,1,hRed);
	  if (nCount!=1) 
            Abort("The size of Source files don't match the user input one");
	  nCount=fread(RGB+1,1,1,hGreen);
	  if (nCount!=1)
            Abort("The size of Source files don't match the user input one");
          nCount=fread(RGB+2,1,1,hBlue);
	  if (nCount!=1)
            Abort("The size of Source files don't match the user input one");
    	  fwrite(RGB,1,3,hTiff);
	}

	nCount=fread(RGB,1,1,hRed);
	if (nCount!=0)
          Abort("The size of Source files don't match the user input one");
	nCount=fread(RGB+1,1,1,hGreen);
	if (nCount!=0)
          Abort("The size of Source files don't match the user input one");
	nCount=fread(RGB+2,1,1,hBlue);
	if (nCount!=0)
          Abort("The size of Source files don't match the user input one");

	fclose(hTiff);
	fclose(hRed);
	fclose(hGreen);
	fclose(hBlue);
}
