Exemple #1
0
void save_files()
{
  char fn1[255], fn2[255];
  FILE *f1, *f2;
  gray **image1, **image2;
  
  image1 = pgm_allocarray(wid, hgt);
  image2 = pgm_allocarray(wid, hgt);
  

  for (int i=0; i<wid*hgt; i++)
    {
      image1[i/wid][i%wid] = (gray) data1[i];
      image2[i/wid][i%wid] = (gray) data2[i];
    }

  snprintf(fn1, 256, "%s_%+.1f_.pgm", fname1, angle_value->value());
  snprintf(fn2, 256, "%s_%+.1f_.pgm", fname2, angle_value->value());
  
  f1 = fopen(fn1, "w");
  f2 = fopen(fn2, "w");

  pgm_writepgm(f1, image1, wid, hgt, 65535, false);
  pgm_writepgm(f2, image2, wid, hgt, 65535, false);

  fclose(f1);
  fclose(f2);

  pgm_freearray(image1, hgt);
  pgm_freearray(image2, hgt);

  fl_beep(FL_BEEP_MESSAGE);
}
Exemple #2
0
pgm_t *pnm_pgm_alloc(pgm_t *pgm, int cols, int rows)
{
  if (!pgm)
    pgm = pnm_pgm_create();
  pgm->rows = rows;
  pgm->cols = cols;
  pgm->pixels = pgm_allocarray(cols, rows);
  return (pgm);
}
Exemple #3
0
void pgm_erode(
		gray  **pgm, 
		int     cols, 
		int     rows, 
		int     num
)		
{
  int     n, i, j;
  gray  **tmp_pgm;

  /* allocate space */
  tmp_pgm = pgm_allocarray(cols, rows);
  if (tmp_pgm == NULL)
    pm_error("no more memory.");

  for (n=0; n < num; n++) {

    /* copy pgm -> tmp_pgm */
    for (i=0; i < rows; i++)
      for (j=0; j < cols; j++)
	tmp_pgm[i][j] = pgm[i][j];
    
    /* dilate 1 pixel */
    for (i=1; i < rows-1; i++)
      for (j=1; j < cols-1; j++)
	if (tmp_pgm[i][j] == OBJECT &&
	    (tmp_pgm[i-1][j-1] == BACKGROUND ||
	     tmp_pgm[i-1][j  ] == BACKGROUND ||
	     tmp_pgm[i-1][j+1] == BACKGROUND ||
	     tmp_pgm[i  ][j-1] == BACKGROUND ||
	     tmp_pgm[i  ][j+1] == BACKGROUND ||
	     tmp_pgm[i+1][j-1] == BACKGROUND ||
	     tmp_pgm[i+1][j  ] == BACKGROUND ||
	     tmp_pgm[i+1][j+1] == BACKGROUND))
	  pgm[i][j] = BACKGROUND;

  } /* end for */

  pgm_freearray(tmp_pgm, rows);
  
} /* end pgm_erode() */
Exemple #4
0
int main(int argc, char *argv[]) {

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

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

  int i, c, w;
  int row;

  int n;

  double alpha = 0.0;
  double beta = 0.0;

  int filter = 0;
  int method = -1;
  int level = 0;
  char filter_name[MAXPATHLEN] = "";

  int verbose = 0;

  gray **image;
  Image_tree dwts;

  gray maxval;
  int rows, cols, format;

  double *watermark;

  progname = argv[0];

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

  while ((c = getopt(argc, argv, "a:b:e:f:F:h?o:s:v:")) != EOF) {
    switch (c) {
      case 'a':
        alpha = atof(optarg);
        if (alpha <= 0.0) {
          fprintf(stderr, "%s: alpha factor %f out of range\n", progname, alpha);
          exit(1);
        }
        break;
      case 'b':
        beta = atof(optarg);
        if (beta <= 0.0) {
          fprintf(stderr, "%s: beta factor %f out of range\n", progname, beta);
          exit(1);
        }
        break;
      case 'e':
        method = atoi(optarg);
        if (method < 0) {
          fprintf(stderr, "%s: wavelet filtering method %d out of range\n", progname, method);
          exit(1);
        }
        break;
      case 'f':
        filter = atoi(optarg);
        if (filter <= 0) {
          fprintf(stderr, "%s: filter number %d out of range\n", progname, filter);
          exit(1);
        }
        break;
      case 'F':
        strcpy(filter_name, optarg);
        break;
      case 'h':
      case '?':
        usage();
        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 '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[32];
    fgets(line, sizeof(line), sig);
    if (strspn(line, "WGSG") >= 4) {
      fscanf(sig, "%d\n", &n);
      if (alpha == 0.0)
        fscanf(sig, "%lf\n", &alpha);
      else
        fscanf(sig, "%*f\n");
      if (beta == 0.0)
        fscanf(sig, "%lf\n", &beta);
      else
        fscanf(sig, "%*f\n");
      if (method < 0)
        fscanf(sig, "%d\n", &method);
      else
        fscanf(sig, "%*d\n");
      if (filter == 0)
        fscanf(sig, "%d\n", &filter);
      else
        fscanf(sig, "%*d\n");
      if (!strcmp(filter_name, ""))
        fscanf(sig, "%[^\n\r]\n", filter_name);
      else
        fscanf(sig, "%*[^\n\r]\n");
    }
    else {
      fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name);
      exit(1);
    }
  }
  else {
    fprintf(stderr, "%s: signature file not specified, use -s file option\n", progname);
    exit(1);
  }

  watermark = malloc(n * sizeof(double));
  for (i = 0; i < n; i++) 
    fscanf(sig, "%lf\n", &watermark[i]);
  fclose(sig);

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

  image = pgm_allocarray(cols, rows);

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

  fclose(in);

  // complete decomposition
  level = find_deepest_level(cols, rows) - 1;

  // wavelet transform
  init_dwt(cols, rows, filter_name, filter, level, method); 
#ifdef POLLEN_STUFF
#include "pollen_stuff.c"
#endif
#ifdef PARAM_STUFF
#include "param_stuff.c"
#endif

  dwts = fdwt(image);

  // build tree for subband selection, calculate subband thresholds
  init_subbands(dwts);
  set_subbands_type_beta(HORIZONTAL, beta);
  set_subbands_type_beta(VERTICAL, beta);
  calc_subbands_threshold();

  w = 0;
  while (w < n) {
    Subband_data s;

    // select subband with max. threshold
    s = select_subband();
    if (verbose > 1)
      fprintf(stderr, "%s: selected subband %s%d, T=%lf, beta=%lf\n", progname, subband_name(s->type), s->level, s->T, s->beta);
 
    // watermark significant coefficients and set them selected
    // check is entire signature has been embedded
    c = select_subband_coeff(s);
    do {
      double p;
      if (c < 0) 
        // no more significant coefficients in subband
        break;

      p = get_subband_coeff(s, c);
      if (p < s->Cmax) {
        if (verbose > 2)
          fprintf(stderr, "%s: embedding sig. coeff. #%d (= %lf)\n  into %s%d coeff. #%d\n", 
            progname, w, watermark[w], subband_name(s->type), s->level, c);

        p = p + alpha * s->beta * s->T * watermark[w];
        set_subband_coeff(s, c, p);
        w++;
      }
      mark_subband_coeff(s, c);

      // select next significant coefficient
      c = select_subband_coeff_from(s, c);
    } while (w < n);

    // update subband threshold
    s->T /= 2.0;

  }

  free_subbands();

  free(watermark);

  idwt(dwts, image);

  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);
}
Exemple #5
0
int main(int argc, char *argv[]) {

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

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

  int c;
  int row, col;

  int n;

  double quality = 0.0;

  int filter = 0;
  int method = -1;
  int level = 0;
  char filter_name[MAXPATHLEN] = "";

  int seed;
  int verbose = 0;

  gray **image;
  Image_tree dwts;

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

  progname = argv[0];

  pgm_init(&argc, argv);

#ifdef __EMX__
  _fsetmode(in, "b");
  _fsetmode(out, "b");
#endif

  while ((c = getopt(argc, argv, "e:f:F:h?l:o:q:s:v:")) != EOF) {
    switch (c) {
      case 'e':
        method = atoi(optarg);
        if (method < 0) {
          fprintf(stderr, "%s: wavelet filtering method %d out of range\n", progname, method);
          exit(1);
        }
        break;
      case 'f':
        filter = atoi(optarg);
        if (filter <= 0) {
          fprintf(stderr, "%s: filter number %d out of range\n", progname, filter);
          exit(1);
        }
        break;
      case 'F':
        strcpy(filter_name, optarg);
        break;
      case 'h':
      case '?':
        usage();
        break;
      case 'l':
        level = atoi(optarg);
        if (level < 1) {
          fprintf(stderr, "%s: embedding level out of range\n", progname);
          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':
        quality = atoi(optarg);
        if (quality <= 0) {
          fprintf(stderr, "%s: quality factor %d out of range\n", progname, quality);
          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[32];
    fgets(line, sizeof(line), sig);
    if (strspn(line, "KDSG") >= 4) {
      fscanf(sig, "%d\n", &n);
      if (quality == 0.0)
        fscanf(sig, "%lf\n", &quality);
      else
        fscanf(sig, "%*f\n");
      if (method < 0)
        fscanf(sig, "%d\n", &method);
      else
        fscanf(sig, "%*d\n");
      if (filter == 0)
        fscanf(sig, "%d\n", &filter);
      else
        fscanf(sig, "%*d\n");
      if (!strcmp(filter_name, ""))
        fscanf(sig, "%[^\n\r]\n", filter_name);
      else
        fscanf(sig, "%*[^\n\r]\n");
      if (level == 0)
        fscanf(sig, "%d\n", &level);
      else
        fscanf(sig, "%*d\n");
      fscanf(sig, "%d\n", &seed);
      srandom(seed);
      n_signature = NBITSTOBYTES(nbit_signature);
      fread(signature, sizeof(char), n_signature, sig);
      fscanf(sig, "\n");
    }
    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);

  image = pgm_allocarray(cols, rows);

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

  fclose(in);

  // check watermark dimensions and decomposition level


  // decomposition of image
  init_dwt(cols, rows, filter_name, filter, level, method);
#ifdef POLLEN_STUFF
  {
    double alpha, beta;
    char *alpha_str = getenv("POLLEN_ALPHA"), *beta_str = getenv("POLLEN_BETA");
    
    if (alpha_str && beta_str) {
      alpha = atof(alpha_str);  
      beta = atof(beta_str);
     
      if (alpha < -M_PI || alpha >= M_PI) {
        fprintf(stderr, "%s: pollen - alpha %f out of range\n", progname, alpha);
        exit(1);
      }
      
      if (beta < -M_PI || beta >= M_PI) {
        fprintf(stderr, "%s: pollen - beta %f out of range\n", progname, beta);
        exit(1);
      }
      
      if (verbose > 7)
        fprintf(stderr, "%s: pollen - alpha %f, beta %f\n", progname, alpha, beta);
      
      dwt_pollen_filter(alpha, beta);
    }
  }
#endif

  dwts = fdwt(image);

  // create 'image' from binary watermark 

  // decomposition of watermark
  init_dwt(cols, rows, filter_name, filter, 1, method);
//  dwts = fdwt(watermark);

  // calculate mean value of image and set alpha

  // setup of contrast sensitivity matrix 

  // segment detail images at each level

  // calculate DFT of each segment

  // compute salience for each segment

  // calculate gamma or each detail image

  // embed watermark

  // reconstruction of watermarked image

  idwt(dwts, image);

  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);
}
int
main(int    argc,
     char * argv[]) {

    struct cmdlineInfo cmdline;
    FILE * ifP;
    int cols, rows;
    int median;
    enum medianMethod medianMethod;

    pgm_init(&argc, argv);

    parseCommandLine(argc, argv, &cmdline);
    
    ifP = pm_openr(cmdline.inputFileName);

    ccolso2 = cmdline.width / 2;
    crowso2 = cmdline.height / 2;

    pgm_readpgminit(ifP, &cols, &rows, &maxval, &format);
    pgm_writepgminit(stdout, cols, rows, maxval, forceplain);

    /* Allocate space for number of rows in mask size. */
    grays = pgm_allocarray(cols, cmdline.height);
    grayrow = pgm_allocrow(cols);

    /* Read in and write out initial rows that won't get changed. */
    for (row = 0; row < cmdline.height - 1; ++row) {
        pgm_readpgmrow(ifP, grays[row], cols, maxval, format);
        /* Write out the unchanged row. */
        if (row < crowso2)
            pgm_writepgmrow(stdout, grays[row], cols, maxval, forceplain);
    }

    median = (cmdline.height * cmdline.width) / 2;

    /* Choose which sort to run. */
    if (cmdline.type == MEDIAN_UNSPECIFIED) {
        if ((maxval / ((cmdline.width * cmdline.height) - 1)) < cmdline.cutoff)
            medianMethod = HISTOGRAM_SORT_MEDIAN;
        else
            medianMethod = SELECT_MEDIAN;
    } else
        medianMethod = cmdline.type;

    switch (medianMethod) {
    case SELECT_MEDIAN:
        selectMedian(ifP, cmdline.width, cmdline.height, cols, rows, median);
        break;
        
    case HISTOGRAM_SORT_MEDIAN:
        histogramSortMedian(ifP, cmdline.width, cmdline.height,
                            cols, rows, median);
        break;
    case MEDIAN_UNSPECIFIED:
        pm_error("INTERNAL ERROR: median unspecified");
    }
    
    pm_close(ifP);
    pm_close(stdout);

    pgm_freearray(grays, cmdline.height);
    pgm_freerow(grayrow);

    return 0;
}
int
main(int argc, const char ** argv) {

    FILE * ifP;
    gray ** grays;
    int argn;
    unsigned int rows, cols;
    unsigned int divisor;
    unsigned int * obuf;  /* malloced */
    const char * const usage = "[-d <val>] height width [asciifile]";

    pm_proginit(&argc, argv);

    rows = 0;  /* initial value */
    cols = 0;  /* initial value */
    divisor = 1; /* initial value */
    
    argn = 1;

    if ( argc < 3 || argc > 6 )
        pm_usage( usage );

    if ( argv[argn][0] == '-' )
    {
        if ( streq( argv[argn], "-d" ) )
        {
            if ( argc == argn + 1 )
                pm_usage( usage );
            if ( sscanf( argv[argn+1], "%u", &divisor ) != 1 )
                pm_usage( usage );
            argn += 2;
        }
        else
            pm_usage( usage );
    }

    if ( sscanf( argv[argn++], "%u", &rows ) != 1 )
        pm_usage( usage );
    if ( sscanf( argv[argn++], "%u", &cols ) != 1 )
        pm_usage( usage );
    if ( rows < 1 )
        pm_error( "height is less than 1" );
    if ( cols < 1 )
        pm_error( "width is less than 1" );

    if ( argc > argn + 1 )
        pm_usage( usage );

    if ( argc == argn + 1 )
        ifP = pm_openr(argv[argn]);
    else
        ifP = stdin;

    MALLOCARRAY(obuf, cols);
    if (obuf == NULL)
        pm_error("Unable to allocate memory for %u columns", cols);

    grays = pgm_allocarray(cols, rows);

    convertAsciiToPgm(ifP, cols, rows, divisor, maxval, grays, obuf);

    pm_close(ifP);

    pgm_writepgm(stdout, grays, cols, rows, maxval, 0);

    free(obuf);
    pgm_freearray(grays, rows);

    return 0;
}
/** write a scrap of the data,
  * return the filename,
  * if the file exist do nothing
  * argument fname is optional
  * the coordinates of the image part are geodata e.g. Gauss Krueger **/
QString
  GeoImage::part(float west, float north, float east, float south,
                 QString fname)
{
  if (fname.isEmpty()) {        //create output filname
    Q_ASSERT(contains("key"));
    QString dir = CleanUp::getTmpDir();
    fname.sprintf("%s/%s_%f_%f_%f_%f", 
		  dir.toLatin1().constData(), 
		  value("key").toLatin1().constData(),
                  west, north, east, south);
  }
  qDebug("#  GeoImage::part %s (%f, %f, %f, %f)", 
	 fname.toLatin1().constData(), west,
         north, east, south);
  QFile f(fname);
  if (f.exists())
    return fname;

  const void *data_p = data();        //get pointer to data
  Q_ASSERT(data_p);
  if (type_ == UNKNOWN)
    return 0;
  int dx, dy, i, j, rx1, ry1, rx2, ry2;
  picBBox(west, north, east, south, rx1, ry2, rx2, ry1);
  dx = rx2 - rx1 + 1;
  dy = ry2 - ry1 + 1;
  if (dx <= 0 || dy <= 0) {
    qDebug("#  (ERROR) GeoImage::part: (dx=%d=%d-%d || dy=%d=%d-%d)", dx, rx2,
           rx1, dy, ry2, ry1);
    throw ImageException(rx1,rx2,dx,ry1,ry2,dy); 
  }

  FILE *of = fopen(fname.toLatin1().constData(), "w");
  if (!of) {
    throw FileIOException(FileIOException::OPEN_FAILED,fname);
  }

  switch (type_) {
  case PBM:{
      bit *bpi, *bpo, **dat_b_out = pbm_allocarray(dx, dy);
      bit **dat_b = (bit **) data_p;
      for (i = 0; i < dy; i++) {
        bpi = dat_b[i + ry1] + rx1;
        bpo = dat_b_out[i];
        for (j = 0; j < dx; j++) {
          *bpo = *bpi;
          bpo++;
          bpi++;
        }
      }
      pbm_writepbm(of, (bit **) dat_b_out, dx, dy, 0);
      pbm_freearray(dat_b_out, dy);
    }
    break;
  case PGM:{
      gray *gpi, *gpo, **dat_g_out = pgm_allocarray(dx, dy);
      gray **dat_g = (gray **) data_p;
      gray maxval = 0;
      for (i = 0; i < dy; i++) {
        gpi = dat_g[i + ry1] + rx1;
        gpo = dat_g_out[i];
        for (j = 0; j < dx; j++) {
          *gpo = *gpi;
          if (*gpi > maxval)
            maxval = *gpi;
          gpo++;
          gpi++;
        }
      }
      pgm_writepgm(of, (gray **) dat_g_out, dx, dy, maxval, 0);
      pgm_freearray(dat_g_out, dy);
    }
    break;
  case PPM:{
      pixel *ppi, *ppo, **dat_p_out = ppm_allocarray(dx, dy);
      pixel **dat_p = (pixel **) data_p;
      pixval maxval = 255;      //! should be calculated
      for (i = 0; i < dy; i++) {
        ppi = dat_p[i + ry1] + rx1;
        ppo = dat_p_out[i];
        for (j = 0; j < dx; j++) {
          *ppo = *ppi;
          ppo++;
          ppi++;
        }
      }
      ppm_writeppm(of, (pixel **) dat_p_out, dx, dy, maxval, 0);
      ppm_freearray(dat_p_out, dy);
    }
    break;
  default:
    pfm_geo_set(geoWest(),geoNorth(),geoEast(),geoSouth());
    pfm_writepfm_region_type(of, data_, cols_, rows_, minval_, maxval_,
                             rx1, ry1, rx2, ry2, type_);
    break;
  }
  fclose(of);
  return fname;
}
Exemple #9
0
int main(int argc, char *argv[]) {

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

  gray **input_image;
  gray **orig_image;

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

  int c;
  int n = 0;
  int method = -1;
  int filter = 0;
  char filter_name[MAXPATHLEN] = "";

  int level = 0;
  double alpha = 0.0;

  int in_rows, in_cols, in_format;
  gray in_maxval;
  int orig_rows, orig_cols, orig_format;
  gray orig_maxval;
  int rows, cols;
  int row, col;

  Image_tree input_dwts;
  Image_tree orig_dwts;

  int verbose = 0;

  progname = argv[0];

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

  while ((c = getopt(argc, argv, "a:e:f:F:h?i:n:o:s:v:")) != EOF) {
    switch (c) {
      case 'a':
        alpha = atof(optarg);
        if (alpha <= 0.0) {
          fprintf(stderr, "%s: alpha factor %f out of range\n", progname, alpha);
          exit(1);
        }
        break;
      case 'e':
        method = atoi(optarg);
        if (method < 0) {
          fprintf(stderr, "%s: wavelet filtering method %d out of range\n", progname, method);
          exit(1);
        }
        break;
      case 'f':
        filter = atoi(optarg);
        if (filter <= 0) {
          fprintf(stderr, "%s: filter number %d out of range\n", progname, filter);
          exit(1);
        }
        break;
      case 'F':
        strcpy(filter_name, optarg);
        break;
      case 'h':
      case '?':
        usage();
        break;
      case 'i':
        if ((orig = fopen(optarg, "rb")) == NULL) {
          fprintf(stderr, "%s: unable to open original image file %s\n", progname, optarg);
          exit(1);
        }
        strcpy(orig_name, optarg);
        break;
      case 'n':
        n = atoi(optarg);
        if (n < 1 || n > 1000) {
          fprintf(stderr, "%s: watermark length %d out of range\n", progname, n);
          exit(1);
        }
        break;
      case 'o':
        if ((out = fopen(optarg, "w")) == NULL) {
          fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg);
          exit(1);
        }
        strcpy(output_name, optarg);
        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 (!orig) {
    fprintf(stderr, "%s: original image file not specified, use -i file option\n", progname);
    exit(1);
  }

  if (sig) {
    char line[32];
    fgets(line, sizeof(line), sig);
    if (strspn(line, "CVSG") >= 4) {
      fscanf(sig, "%d\n", &n);
      if (alpha == 0.0)
        fscanf(sig, "%lf\n", &alpha);
      else
        fscanf(sig, "%*f\n");
      if (method < 0)
        fscanf(sig, "%d\n", &method);
      else
        fscanf(sig, "%*d\n");
      if (filter == 0)
        fscanf(sig, "%d\n", &filter);
      else
        fscanf(sig, "%*d\n");
      if (!strcmp(filter_name, ""))
        fscanf(sig, "%[^\n\r]\n", filter_name);
      else
        fscanf(sig, "%*[^\n\r]\n");
    }
    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, &in_cols, &in_rows, &in_maxval, &in_format);
  pgm_readpgminit(orig, &orig_cols, &orig_rows, &orig_maxval, &orig_format);

  if (in_cols != orig_cols || in_rows != orig_rows) {
    fprintf(stderr, "%s: input image %s does not match dimensions of original image %s\n", progname, input_name, orig_name);
    exit(1);
  }

  cols = in_cols;
  rows = in_rows;

  input_image = pgm_allocarray(in_cols, in_rows);

  orig_image = pgm_allocarray(orig_cols, orig_rows);

  for (row = 0; row < in_rows; row++)
    pgm_readpgmrow(in, input_image[row], in_cols, in_maxval, in_format);

  fclose(in);

  for (row = 0; row < orig_rows; row++)
    pgm_readpgmrow(orig, orig_image[row], orig_cols, orig_maxval, orig_format);

  fclose(orig);

  level = 0;
  row = rows;
  col = cols;
  while (n < row * col / 4.0 && row >= 2 && col >= 2) {
    row /= 2;
    col /= 2;
    level++;
  }

  if (verbose >= 2) {
    fprintf(stderr, "%s: extracting from coarse image (x %d/y %d) at level %d\n", progname, col, row, level);
  }

  init_dwt(cols, rows, filter_name, filter, level, method);
#ifdef POLLEN_STUFF
#include "pollen_stuff.c"
#endif
#ifdef PARAM_STUFF
#include "param_stuff.c"
#endif

  input_dwts = fdwt(input_image);
  orig_dwts = fdwt(orig_image);

  fprintf(out, "CVWM\n");
  fprintf(out, "%d\n", n);

  {
    Image_tree p = input_dwts;
    Image_tree q = orig_dwts;
    Image input_img;
    Image orig_img;
    double input_med;
    double orig_med;
    double input_var;
    double orig_var;

    while (!p->image)
      p = p->coarse;

    while (!q->image)
      q = q->coarse;

    input_img = p->image;
    orig_img = q->image;

    input_med = 0.0;
    for (row = 0; row < input_img->height; row++)
      for (col = 0; col < input_img->width; col++)
        input_med += get_pixel(input_img, col, row);
    input_med /= (double) (input_img->height * input_img->width);

    orig_med = 0.0;
    for (row = 0; row < orig_img->height; row++)
      for (col = 0; col < orig_img->width; col++)
        orig_med += get_pixel(orig_img, col, row);
    orig_med /= (double) (orig_img->height * orig_img->width);

    orig_var = 0.0;
    for (row = 0; row < orig_img->height; row++)
      for (col = 0; col < orig_img->width; col++)
        orig_var += sqr(get_pixel(orig_img, col, row) - orig_med);
    orig_var /= (double) (orig_img->height * orig_img->width);

    input_var = 0.0;
    for (row = 0; row < input_img->height; row++)
      for (col = 0; col < input_img->width; col++)
        input_var += sqr(get_pixel(input_img, col, row) - input_med);
    input_var /= (double) (input_img->height * input_img->width);

    orig_var = sqrt(orig_var);
    input_var = sqrt(input_var);

    if (verbose > 3)
      fprintf(stderr, "%s: mean (input, orig): %f, %f,\n  variance (input, orig): %f, %f\n", progname, input_med, orig_med, input_var, orig_var);

    row = 0;
    col = 0;
    while (n > 0) {
      double input_pix;
      double orig_pix;
      double x;

      input_pix = get_pixel(input_img, col, row);
      orig_pix = get_pixel(orig_img, col, row);

      x = (((input_pix - input_med) * (orig_var / input_var) - (orig_pix / orig_med)) / (orig_pix - orig_med) - 1.0) / alpha;

      fprintf(out, "%f\n", x);

      if (++col == orig_img->width) { col = 0; row++; }
      n--;
    }
  }

  fclose(out);

  pgm_freearray(input_image, rows);
  pgm_freearray(orig_image, rows);

  exit(0);
}