/*   COMBINE TWO 24-BIT IMAGES    */


#include<stdio.h>
#include<stdlib.h>
#include<math.h>


/* Functions */
long getImageInfo(FILE*, long, int);
void copyImageInfo(FILE*, FILE*);


void  main(void)
{

  FILE		       *bmpfile1, *bmpfile2, *bmpOutput, *rawdata;
  unsigned char        *pChar1, *pChar2, *pCharO, dummy1, dummy2, dummyO;
  int		       r, r1, r2, c, c1, c2;
  long                 j=0L;
  int                  nbits1, nbits2;
  int                  i;
  unsigned char	       store4byte2[4];

  /* Initialization */
  dummy1 = '0';   pChar1 = &dummy1;
  dummy2 = '0';   pChar2 = &dummy2;
  dummyO = '0';   pCharO = &dummyO;


  printf("ONLY 24 BIT RGB BMPs CAN BE PROCESSED.!!!\n");
  printf("IMAGE SIZES AND FORMATS MUST BE SAME.!!!\n");
  printf("Reading bmpin1.bmp & bmpin2.bmp\n bmpin1.bmp is the main BMP.\n");

  if ((bmpfile1 = fopen("bmpin1.bmp", "rb")) == NULL)
  {
	printf("Can not open bmpin1.bmp\n");
	exit(0);
  }

  if ((bmpfile2 = fopen("bmpin2.bmp", "rb")) == NULL)
  {
	printf("Can not open bmpin2.bmp\n");
	exit(0);
  }

  if ((bmpOutput = fopen("bmp24out.bmp", "wb")) == NULL)
  {
	printf("Can not open bmp24out.bmp\n");
	exit(0);
  }

  if ((rawdata = fopen("rawdat24.txt", "w")) == NULL)
  {
	printf("Can not open rawdat24.txt");
	exit(0);
  }

  /* Get Info of the Files and Compare */

  c1 = (int)get24bitImageInfo(bmpfile1, 18, 4);
  c2 = (int)get24bitImageInfo(bmpfile2, 18, 4);
  printf("Width of File 1: \t\t%d\n", c1);
  printf("Width of File 2: \t\t%d\n", c2);

  r1 = (int)get24bitImageInfo(bmpfile1, 22, 4);
  r2 = (int)get24bitImageInfo(bmpfile2, 22, 4);
  printf("Height of File 1: \t%d\n", r1);
  printf("Height of File 2: \t%d\n", r2);
  if ( (r1 != r2)||(c1 != c2) )
  {
	printf("Rows or cloumns don't match. Must enter same size BMPs.");
	exit(0);
  }

  nbits1 = (int)get24bitImageInfo(bmpfile1, 28, 2);
  printf("Bits/pixel of File 1: \t%d\n", nbits1);
  nbits2 = (int)get24bitImageInfo(bmpfile2, 28, 2);
  printf("Bits/pixel of File 2: \t%d\n", nbits2);
  if (nbits1 != nbits2)
  {
	printf("File formats don't match. Must have same formatted BMPs");
	exit(0);
  }

  copy24bitImageInfo(bmpfile1, bmpOutput);

  /* Beginning of the Raster Data is set */
  fseek(bmpfile1, (int)(54), SEEK_SET);
  fseek(bmpfile2, (int)(54), SEEK_SET);
  fseek(bmpOutput, (int)(54), SEEK_SET);

  getch();

  /*/////////////////////////*/
  /*//// BMP PROCESSING /////*/


  for(r=0; r<r1; r++)
  {
    for(c=0; c<c1; c++)
    {
	for(i=0;i<3;i++)
	{

		fread(pChar1, sizeof(char), 1, bmpfile1);

		/* Copy main image to output */
		*pCharO = *pChar1;
		fwrite(pCharO, sizeof(char), 1, bmpOutput);

		fread(pChar2, sizeof(char), 1, bmpfile2);
		store4byte2[i] = *pChar2;

		j = j + 1L;
	}


	/* Copy the non-white of image 2 to output */

	if( (store4byte2[0] != 255) || (store4byte2[1] != 255) || (store4byte2[2] != 255) )
	{
		j = j - 3L;

		fseek(bmpOutput, (54L+j), SEEK_SET);
		for(i=0; i<3; i++)
		{
			fwrite(&store4byte2[i], sizeof(char), 1, bmpOutput);
			j = j + 1L;
		}
	 }


    }
  }

  fclose(bmpOutput);
  fclose(bmpfile1);
  fclose(bmpfile2);
  fclose(rawdata);

} /* end of main */


long getImageInfo(FILE* inputFile, long offset, int numberOfChars)
{

  unsigned char		*ptrC, dummy;
  long			value = 0L;
  int			i;


  dummy = '0';   ptrC = &dummy;

  fseek(inputFile, offset, SEEK_SET);

  for(i=1; i<=numberOfChars; i++)
  {
    fread(ptrC, sizeof(char), 1, inputFile);
    /* Byte by byte reading */
    value = (long)(value + (*ptrC)*(pow(256, (i-1))));
  }
  return(value);

} /* end of getImageInfo */


void copyImageInfo(FILE* inputFile, FILE* outputFile)
{

  /* copies inputFile's header and info header data to outputFile */

  unsigned char		*ptrC, dummy;
  int			i;


  dummy = '0';  ptrC = &dummy;

  fseek(inputFile, 0L, SEEK_SET);
  fseek(outputFile, 0L, SEEK_SET);

  for(i=0; i<54; i++)
  {
    fread(ptrC, sizeof(char), 1, inputFile);
    fwrite(ptrC, sizeof(char), 1, outputFile);
  }

} /* end of copyImageInfo */

