/******************************************************************************

   This file is a part of the

             COMPLEX PSEUDO-RAW SAR IMAGE PROCESSING TOOLBOX

   Copyright (C) 2017 Rémy Abergel (Remy.Abergel AT parisdescartes.fr), Loïc
   Denis (Loic.Denis AT univ-st-etienne.fr), Saïd Ladjal (Said.Ladjal AT
   telecom-paristech.fr) and Florence Tupin (Florence.Tupin AT
   telecom-paristech.fr).

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.

******************************************************************************/

/*
   Compilation (using gcc):
   ========================

   gcc -O3 tiffmerge.c tiffread.c tiffwrite.c -ltiff -o tiffmerge


   Ddescription:
   =============

   Merge two monopage TIFF image into a stack of two TIFF images (in float
   precision).

*/


#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>

#define ASSERT_ALLOC(cmd) if(NULL == (cmd)) { printf("Not enough memory\n"); exit(EXIT_FAILURE); }

static void display_usage()
{
  printf("\nMerge two (monopage) TIFF images into a stack of two TIFF images (float precision).\n\n");
  printf("Usage: tiffmerge [-v] in1 in2 out\n\n");
  printf("   -v      : enable verbose mode\n");
  printf("   in1     : first input image (TIFF monopage)\n");
  printf("   in2     : second input image (TIFF monopage)\n");
  printf("   out     : output image (a stack of 2 TIFF images in float precision)\n\n");
}


int main(int argc, char **argv)
{
  char *filename_in1 = NULL,*filename_in2 = NULL,*filename_out = NULL,vflag = 0;
  double *in1,*in2;
  int i,nx1,nx2,ny1,ny2;

  /******************************************/
  /*            arguments parser            */
  /******************************************/

  /* main parser */
  for(i=1;i<argc;i++) {
    if (strcmp(argv[i],"-v") == 0) vflag = 1;
    else if (NULL == filename_in1) filename_in1 = argv[i];
    else if (NULL == filename_in2) filename_in2 = argv[i];
    else if (NULL == filename_out) filename_out = argv[i];
    else { display_usage(); printf("Error: could not retrieve properly input arguments.\n\n"); return EXIT_FAILURE; }
  }

  /* check consistency */
  if ((NULL == filename_in1) || (NULL == filename_in2) || (NULL == filename_out)) {
    display_usage();
    if (NULL == filename_in1) printf("Error: input 'in1' is missing\n\n");
    else if (NULL == filename_in2) printf("Error: input 'in2' is missing\n\n");
    else  printf("Error: output 'out' is missing\n\n");
    return EXIT_FAILURE;
  }

  /************************************************************/
  /* Open TIFF Image & get its graylevels in double precision */
  /************************************************************/
  if(vflag) {
    printf("\nLoad first input image\n");
    printf("======================\n");
  }
  if(EXIT_FAILURE == load_monopage_tiff_into_double(&in1,&nx1,&ny1,filename_in1,vflag,0)) {
    if(vflag) printf("Free memory and exit the program.\n\n");
    else printf("An error occured (try verbose mode using option -v to get more informations)\n");
    return EXIT_FAILURE;
  }
  if(vflag) printf("\n");

  if(vflag) {
    printf("Load second input image\n");
    printf("=======================\n");
  }
  if(EXIT_FAILURE == load_monopage_tiff_into_double(&in2,&nx2,&ny2,filename_in2,vflag,0)) {
    if(vflag) printf("Free memory and exit the program.\n\n");
    else printf("An error occured (try verbose mode using option -v to get more informations)\n");
    return EXIT_FAILURE;
  }
  if(vflag) printf("\n");



  /********************************/
  /* Write output as a TIFF Image */
  /********************************/
  if(vflag) {
    printf("Save output as a stack of two TIFF images in float precision\n");
    printf("============================================================\n");
  }

  if (EXIT_FAILURE == save_2pages_striped_tiff_float_from_double(filename_out,in1,in2,nx1,ny1,vflag)) {
    if (vflag) printf("free memory and exit the program.\n\n");
    else printf("An error occured (try verbose mode using option -v to get more informations)\n");
    free(in1); free(in2);
    return EXIT_FAILURE;
  }

  /* free memory */
  if(vflag) {
    printf("free memory : in progress");
    free(in1); free(in2);
    fflush(stdout);
  }

  if(vflag) {
    printf("\33[2K\rfree memory : done\n\n");
    fflush(stdout);
  }

  return EXIT_SUCCESS;
}
