/*
   Find edge of tape reel
*/

#define ABS(x) ( (x)<0 ? (-(x)) : (x) )
#define SQR(x) ( (x)*(x) )

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/times.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <signal.h>
#include <time.h>
#include <unistd.h>
#include <math.h>
#include <omp.h>
#include <errno.h>
#include <pthread.h>

#include <tiffio.h>

static int debug=1;

static int width,height;
static short depth,samplesperpixel,photometric;
static float xresolution,yresolution;
static float *picture;
static float *resultpicture;
static short *origpicture;
static tsize_t scanline;
static float maxsum;

static int framewidth,frameheight,framex,framey;

static int clock_tics;
static struct tms tms1;

static unsigned char *read_tiff(char *filename)
{
  TIFF *image;
  tdata_t buf;
  uint32 row,col;
  unsigned char *p8;
  unsigned char *picture;
  image = TIFFOpen(filename, "r");
  TIFFGetField(image, TIFFTAG_IMAGEWIDTH, &width);
  TIFFGetField(image, TIFFTAG_IMAGELENGTH, &height);
  TIFFGetField(image, TIFFTAG_BITSPERSAMPLE, &depth);
  TIFFGetField(image, TIFFTAG_SAMPLESPERPIXEL, &samplesperpixel);
  TIFFGetField(image, TIFFTAG_PHOTOMETRIC, &photometric);
  TIFFGetField(image, TIFFTAG_XRESOLUTION, &xresolution);
  TIFFGetField(image, TIFFTAG_YRESOLUTION, &yresolution);
  if(debug) fprintf(stderr,"width: %d                                \n", width);
  if(debug) fprintf(stderr,"height: %d\n", height);
  if(debug) fprintf(stderr,"depth: %d\n", depth);
  if(debug) fprintf(stderr,"samplesperpixel: %d\n", samplesperpixel);
  if(debug) fprintf(stderr,"xresolution: %g\n", xresolution);
  if(debug) fprintf(stderr,"yresolution: %g\n", yresolution);
  scanline=TIFFScanlineSize(image);
  picture = (unsigned char *) malloc(sizeof(*picture)*width*height*4);

  for(row=0; row<height; row++)
  {
//  progress("read",row,0,height-1);
    p8 = (unsigned char *) (((char *)picture)+scanline*row);
    TIFFReadScanline(image, (char *)p8, row, 0);
  }
  TIFFClose(image);
  return(picture);
}

static void write_tiff(char *filename,unsigned char *picture)
{
  int row,col,samp;
  TIFF *image;
  unsigned short *buf16,*p16;
  unsigned char *buf8,*p8;
  float p;
  
  image = TIFFOpen(filename, "w");

  TIFFSetField(image, TIFFTAG_IMAGEWIDTH, width);
  TIFFSetField(image, TIFFTAG_IMAGELENGTH, height);
  TIFFSetField(image, TIFFTAG_BITSPERSAMPLE, depth);
  TIFFSetField(image, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
  TIFFSetField(image, TIFFTAG_PHOTOMETRIC, photometric);
  TIFFSetField(image, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
  TIFFSetField(image, TIFFTAG_SAMPLESPERPIXEL, samplesperpixel);
  TIFFSetField(image, TIFFTAG_MINSAMPLEVALUE, 0);
  TIFFSetField(image, TIFFTAG_MAXSAMPLEVALUE, 255);
  TIFFSetField(image, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
  TIFFSetField(image, TIFFTAG_ROWSPERSTRIP, 1);
  TIFFSetField(image, TIFFTAG_IMAGEDESCRIPTION, "hole1");
  TIFFSetField(image, TIFFTAG_XRESOLUTION, xresolution);
  TIFFSetField(image, TIFFTAG_YRESOLUTION, yresolution);
  TIFFSetField(image, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
  for(row=0;row<height;row++)
  {
    buf8=picture+row*samplesperpixel*width;
    if(TIFFWriteScanline(image, buf8, row, 0)<0)
    {
      fprintf(stderr, "Error during TIFFWriteScanline row: %d\n", row);
    }
  }
  TIFFClose(image);
}

static void domask(unsigned char *im, unsigned char *mask)
{
  int row,col;
  unsigned char *pim,*pmask;
  for(row=0;row<height;row++)
    for(col=0;col<width;col++)
    {
      pim=im+(row*width+col)*samplesperpixel;
      pmask=mask+(row*width+col)*samplesperpixel;
      if(pmask[3]==255)
      {
	pim[3]=0;
      }
    }

}

main(int argc, char **argv)
{
  int i,j;
  unsigned char *im,*mask;

  im=read_tiff(argv[1]);
  mask=read_tiff(argv[2]);
  domask(im,mask);
  write_tiff(argv[3],im);
}
