#include "wm.h"
#include "dct.h"
#include "signature.h"
#include "coord.h"
#include "pgm.h"

char *progname;

void usage(void) {
  fprintf(stderr, "usage: %s [-h] [-l n] [-o file] [-q n] [-v n] -s file file\n", progname);
  fprintf(stderr, "\t-h\t\tprint usage\n");
  fprintf(stderr, "\t-l n\t\tsignature robustness factor\n");
  fprintf(stderr, "\t-o file\t\toutput (watermarked) file\n");
  fprintf(stderr, "\t-q n\t\tquantization (JPEG quality) factor\n");
  fprintf(stderr, "\t-s file\t\tsignature to embed in input image\n");
  fprintf(stderr, "\t-v n\t\tverbosity level\n");
  exit(0);
}

int main(int argc, char *argv[]) {

  FILE *in = stdin;
  FILE *out = stdout;
  FILE *sig = NULL;

  char signature_name[MAXPATHLEN];
  char input_name[MAXPATHLEN] = "(stdin)";
  char output_name[MAXPATHLEN] = "(stdout)";

  int c;
  int n;

  int seed;
  int verbose = 0;

  int rows, cols, format;
  gray maxval;
  int row;

  int quantization = 0;
  double quality = 0.0;

  struct coords *coords;

  gray **image;
  double **dcts;

  progname = argv[0]; 

  pgm_init(&argc, argv); wm_init();

  while ((c = getopt(argc, argv, "h?i:l:o:q:s:v:")) != EOF) {
    switch (c) {
      case 'h':
      case '?':
        usage();
        break;
      case 'l':
        quality = atof(optarg);
        if (quality <= 0.0) {
          fprintf(stderr, "%s: signature strength factor %f out of range\n", progname, quality);
          exit(1);
        }
        break;
      case 'o':
        if ((out = fopen(optarg, "wb")) == NULL) {
          fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg);
          exit(1);
        }
        strcpy(output_name, optarg);
        break;
      case 'q':
        quantization = atoi(optarg);
        if (quantization <= 0 || quantization > 100) {
          fprintf(stderr, "%s: quantization factor %d out of range\n", progname, quantization);
          exit(1);
        }
        break;
      case 's':
        if ((sig = fopen(optarg, "r")) == NULL) {
          fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg);
          exit(1);
        }
        strcpy(signature_name, optarg);
        break;
      case 'v':
        verbose = atoi(optarg);
        if (verbose < 0) {
          fprintf(stderr, "%s: verbosity level %d out of range\n", progname, verbose);
          exit(1);
        }
        break;
    }
  }

  argc -= optind;
  argv += optind;

  if (argc > 1) {
    usage();
    exit(1);
  }

  if (argc == 1 && *argv[0] != '-') {
    if ((in = fopen(argv[0], "rb")) == NULL) {
      fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]);
      exit(1);
    }
    else
      strcpy(input_name, argv[0]);
  }

  if (sig) {
    char line[128];
    fgets(line, sizeof(line), sig);
    if (strspn(line, "KCSG") >= 4) {
      fscanf(sig, "%d\n", &nbit_signature);
      if (quality == 0.0)
        fscanf(sig, "%lf\n", &quality);
      else
        fscanf(sig, "%*lf\n");
      if (quantization == 0)
        fscanf(sig, "%d\n", &quantization);
      else
        fscanf(sig, "%*d\n");
      fscanf(sig, "%d\n", &seed);
      n_signature = NBITSTOBYTES(nbit_signature);
      fread(signature, sizeof(char), n_signature, sig);
      fscanf(sig, "\n");
      srandom(seed);
    }
    else {
      fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name);
      exit(1);
    }
    fclose(sig);
  }
  else {
    fprintf(stderr, "%s: signature file not specified, use -s file option\n", progname);
    exit(1);
  }

  pgm_readpgminit(in, &cols, &rows, &maxval, &format);

  if (cols % NJPEG) {
    fprintf(stderr, "%s: image width %d not a multiple of %d\n", progname, cols, NJPEG);
    exit(1);
  }

  if (rows % NJPEG) {
    fprintf(stderr, "%s: image height %d not a multiple of %d\n", progname, rows, NJPEG);
    exit(1);
  }

  if ((cols * rows) / (NJPEG * NJPEG) < nbit_signature) {
    fprintf(stderr, "%s: image not large enough to embed %d bits of signature\n", progname, nbit_signature);
    exit(1);
  }

  init_dct_8x8();
  init_quantum_JPEG_lumin(quantization);

  dcts = alloc_coeffs_8x8();

  if ((coords = alloc_coords(nbit_signature)) == NULL) {
    fprintf(stderr, "%s: unable to allocate memory\n", progname);
    exit(1);
  }

  image = pgm_allocarray(cols, rows);

  for (row = 0; row < rows; row++)
    pgm_readpgmrow(in, image[row], cols, maxval, format);

  fclose(in);

  // embedding signature bits by modifying two coefficient relationship,
  // one bit for each block
  n = 0;
  while (n < nbit_signature) {
    int xb;
    int yb;
    int c1, c2;
    double v1, v2;
    double w1, w2;
    double diff, abs_diff;

    // randomly select a block, check to get distinct blocks
    // (don't watermark a block twice)
    do {
      xb = random() % (cols / NJPEG);
      yb = random() % (rows / NJPEG);
    } while (add_coord(coords, xb, yb) < 0);

    // do the forward 8x8 DCT of that block
    fdct_block_8x8(image, xb * NJPEG, yb * NJPEG, dcts);

    // randomly select two distinct coefficients from block
    // only accept coefficients in the middle frequency range
    do {
      c1 = (random() % (NJPEG * NJPEG - 2)) + 1;
      c2 = (random() % (NJPEG * NJPEG - 2)) + 1;
    } while (c1 == c2 || !is_middle_frequency_coeff_8x8(c1) || !is_middle_frequency_coeff_8x8(c2));

    // quantize block according to quantization quality parameter
    quantize_8x8(dcts);

    if (verbose > 0)
      fprintf(stderr, "%d: quantized DCT block (x %d/y %d), modifying (x %d/y %d), (x %d/y %d) for %s\n", n, xb * NJPEG, yb * NJPEG, c1 % NJPEG, c1 / NJPEG, c2 % NJPEG, c2 / NJPEG, get_signature_bit(n) ? "HIGH" : "LOW");
    if (verbose > 5)
      print_coeffs_8x8(dcts);

    v1 = dcts[c1 / NJPEG][c1 % NJPEG];
    v2 = dcts[c2 / NJPEG][c2 % NJPEG];

    diff = fabs(v1) - fabs(v2);
    abs_diff = (fabs(diff) + quality) / 2.0;

    // modify coefficient's relationship to embed signature bit
    // using mean square error to minimize error
    if (get_signature_bit(n)) {
      if (diff < quality) {
        // we have to impose the relationship, does not occur naturally
        w1 = (v1 > 0.0) ? (v1 + abs_diff) : (v1 - abs_diff);
        w2 = (v2 > 0.0) ? (v2 - abs_diff) : (v2 + abs_diff);
      }
      else {
        w1 = v1;
        w2 = v2;
      }
    }
    else {
      if (diff > -quality) {
        // force the relationship
        w1 = (v1 > 0.0) ? (v1 - abs_diff) : (v1 + abs_diff);
        w2 = (v2 > 0.0) ? (v2 + abs_diff) : (v2 - abs_diff);
      }
      else {
        w1 = v1;
        w2 = v2;
      }
    }

    if (verbose > 1)
      fprintf(stderr, "  %f -> %f, %f -> %f\n", v1, w1, v2, w2);

    // put the changed coefficients back to black
    dcts[c1 / NJPEG][c1 % NJPEG] = w1;
    dcts[c2 / NJPEG][c2 % NJPEG] = w2;

    // the obvious :-)
    dequantize_8x8(dcts);

    // do the inverse DCT on the modified 8x8 block
    idct_block_8x8(dcts, image, xb * NJPEG, yb * NJPEG);

    n++;
  }

  free_coeffs(dcts);

  pgm_writepgminit(out, cols, rows, maxval, 0);
  for (row = 0; row < rows; row++)
    pgm_writepgmrow(out, image[row], cols, maxval, 0);

  fclose(out);

  pgm_freearray(image, rows);

  exit(0);
}
