Mercurial > hg > wm
comparison Meerwald/wm_frid2_d.c @ 0:be303a3f5ea8
import
| author | Peter Meerwald <pmeerw@cosy.sbg.ac.at> |
|---|---|
| date | Sun, 12 Aug 2007 13:14:34 +0200 |
| parents | |
| children | f83ef905a63d |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:be303a3f5ea8 |
|---|---|
| 1 #include "wm.h" | |
| 2 #include "dct.h" | |
| 3 #include "pgm.h" | |
| 4 #include "signature.h" | |
| 5 #include "frid2_common.h" | |
| 6 | |
| 7 char *progname; | |
| 8 | |
| 9 void usage(void) { | |
| 10 fprintf(stderr, "usage: %s [-a n] [-h] [-b n] [-n n] [-o file] [-v n] -s file file\n\n", progname); | |
| 11 fprintf(stderr, "\t-a n\t\talpha factor low freq. embedding strength\n"); | |
| 12 fprintf(stderr, "\t-b n\t\tbeta factor for weighted correlation (default 0)\n"); | |
| 13 fprintf(stderr, "\t-h\t\tprint usage\n"); | |
| 14 fprintf(stderr, "\t-n n\t\tnormalisation factor (default 1024.0)\n"); | |
| 15 fprintf(stderr, "\t-o file\t\tfile for extracted watermark information\n"); | |
| 16 fprintf(stderr, "\t-s file\t\toriginal signature file\n"); | |
| 17 fprintf(stderr, "\t-v n\t\tverbosity level\n"); | |
| 18 exit(0); | |
| 19 } | |
| 20 | |
| 21 int main(int argc, char *argv[]) { | |
| 22 FILE *in = stdin; | |
| 23 FILE *out = stdout; | |
| 24 FILE *sig = NULL; | |
| 25 | |
| 26 char output_name[MAXPATHLEN] = "(stdout)"; | |
| 27 char input_name[MAXPATHLEN] = "(stdin)"; | |
| 28 char signature_name[MAXPATHLEN]; | |
| 29 | |
| 30 int c; | |
| 31 int row, col; | |
| 32 int n; | |
| 33 double normalization = 1024.0; | |
| 34 int seed, format; | |
| 35 | |
| 36 double alpha = 0.0; | |
| 37 double beta = 0.0; | |
| 38 | |
| 39 double correlation; | |
| 40 | |
| 41 gray **image; | |
| 42 double **coeffs; | |
| 43 | |
| 44 double mean, derivation, mult_factor; | |
| 45 int rows, cols; | |
| 46 | |
| 47 gray maxval; | |
| 48 int verbose = 0; | |
| 49 | |
| 50 progname = argv[0]; | |
| 51 | |
| 52 pgm_init(&argc, argv); wm_init2(); | |
| 53 | |
| 54 while ((c = getopt(argc, argv, "a:b:n:h?s:o:v:")) != EOF) { | |
| 55 switch (c) { | |
| 56 case 'a': | |
| 57 alpha = atof(optarg); | |
| 58 if (alpha <= 0.0) { | |
| 59 fprintf(stderr, "%s: alpha factor %f out of range\n", progname, alpha); | |
| 60 exit(1); | |
| 61 } | |
| 62 break; | |
| 63 case 'b': | |
| 64 beta = atof(optarg); | |
| 65 if (beta <= 0.0) { | |
| 66 fprintf(stderr, "%s: beta factor %f out of range\n", progname, beta); | |
| 67 exit(1); | |
| 68 } | |
| 69 break; | |
| 70 case 'n': | |
| 71 normalization = atof(optarg); | |
| 72 if (normalization < 0) { | |
| 73 fprintf(stderr, "%s: normalisation factor %f out of range\n", progname, normalization); | |
| 74 exit(1); | |
| 75 } | |
| 76 break; | |
| 77 case 'h': | |
| 78 case '?': | |
| 79 usage(); | |
| 80 break; | |
| 81 case 'o': | |
| 82 if ((out = fopen(optarg, "wb")) == NULL) { | |
| 83 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg); | |
| 84 exit(1); | |
| 85 } | |
| 86 strcpy(output_name, optarg); | |
| 87 break; | |
| 88 case 's': | |
| 89 if ((sig = fopen(optarg, "r")) == NULL) { | |
| 90 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg); | |
| 91 exit(1); | |
| 92 } | |
| 93 strcpy(signature_name, optarg); | |
| 94 break; | |
| 95 case 'v': | |
| 96 verbose = atoi(optarg); | |
| 97 if (verbose < 0) { | |
| 98 fprintf(stderr, "%s: verbosity level %d out of range\n", progname, verbose); | |
| 99 exit(1); | |
| 100 } | |
| 101 break; | |
| 102 } | |
| 103 } | |
| 104 | |
| 105 argc -= optind; | |
| 106 argv += optind; | |
| 107 | |
| 108 if (argc > 1) { | |
| 109 usage(); | |
| 110 exit(1); | |
| 111 } | |
| 112 | |
| 113 if (argc == 1 && *argv[0] != '-') | |
| 114 if ((in = fopen(argv[0], "rb")) == NULL) { | |
| 115 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]); | |
| 116 exit(1); | |
| 117 } | |
| 118 else | |
| 119 strcpy(input_name, argv[0]); | |
| 120 | |
| 121 if (sig) { | |
| 122 char line[32]; | |
| 123 fgets(line, sizeof(line), sig); | |
| 124 if (strspn(line, "FR2SG") >= 5) { | |
| 125 fscanf(sig, "%d\n", &nbit_signature1); | |
| 126 if (alpha == 0.0) | |
| 127 fscanf(sig, "%lf\n", &alpha); | |
| 128 else | |
| 129 fscanf(sig, "%*lf\n"); | |
| 130 fscanf(sig, "%*lf\n"); | |
| 131 fscanf(sig, "%d\n", &seed); | |
| 132 n_signature1 = NBITSTOBYTES(nbit_signature1); | |
| 133 fread(signature1, sizeof(char), n_signature1, sig); | |
| 134 fscanf(sig, "\n"); | |
| 135 srandom(seed); | |
| 136 } | |
| 137 else { | |
| 138 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name); | |
| 139 exit(1); | |
| 140 } | |
| 141 fclose(sig); | |
| 142 } | |
| 143 else { | |
| 144 fprintf(stderr, "%s: signature file not specified, use -s file option\n", progname); | |
| 145 exit(1); | |
| 146 } | |
| 147 | |
| 148 pgm_readpgminit(in, &cols, &rows, &maxval, &format); | |
| 149 image = pgm_allocarray(cols, rows); | |
| 150 for (row = 0; row < rows; row++) | |
| 151 pgm_readpgmrow(in, image[row], cols, maxval, format); | |
| 152 fclose(in); | |
| 153 | |
| 154 // calculate mean malue | |
| 155 mean = 0.0; | |
| 156 for (row = 0; row < rows; row++) | |
| 157 for (col = 0; col < cols; col++) | |
| 158 mean += image[row][col]; | |
| 159 | |
| 160 mean /= cols * rows; | |
| 161 | |
| 162 // calculate derivation | |
| 163 derivation = 0.0; | |
| 164 for (row = 0; row < rows; row++) | |
| 165 for (col = 0; col < cols; col++) | |
| 166 derivation += sqr(image[row][col] - mean); | |
| 167 | |
| 168 derivation = sqrt(derivation / (cols * rows - 1)); | |
| 169 mult_factor = normalization / (sqrt(cols * rows) * derivation); | |
| 170 | |
| 171 if (verbose > 5) | |
| 172 fprintf(stderr, "%s: mean %f, derivation %f, mult_factor %f\n", progname, mean, derivation, mult_factor); | |
| 173 | |
| 174 // normalize image | |
| 175 coeffs = alloc_coeffs(cols, rows); | |
| 176 for (row = 0; row < rows; row++) | |
| 177 for (col = 0; col < cols; col++) | |
| 178 coeffs[row][col] = (image[row][col] - mean) * mult_factor; | |
| 179 | |
| 180 if (rows == cols) { | |
| 181 init_dct_NxN(cols, rows); | |
| 182 fdct_inplace_NxN(coeffs); | |
| 183 } | |
| 184 // else { | |
| 185 // init_dct_NxM(cols, rows); | |
| 186 // fdct_NxM(coeffs); | |
| 187 // } | |
| 188 | |
| 189 | |
| 190 fprintf(out, "FR2WM\n"); | |
| 191 | |
| 192 fprintf(out, "%d\n", nbit_signature1); | |
| 193 correlation = detect_low_freq(coeffs, cols, rows, alpha, beta, verbose); | |
| 194 if (verbose > 2) | |
| 195 fprintf(stderr, "low_freq correlation: %f\n", correlation); | |
| 196 fwrite(signature2, sizeof(char), NBITSTOBYTES(nbit_signature1), out); | |
| 197 fprintf(out, "\n"); | |
| 198 | |
| 199 fprintf(out, "%d\n", nbit_signature1); | |
| 200 correlation = detect_med_freq(coeffs, cols, rows, seed, verbose); | |
| 201 if (verbose > 2) | |
| 202 fprintf(stderr, "med_freq correlation: %f\n", correlation); | |
| 203 fwrite(signature2, sizeof(char), NBITSTOBYTES(nbit_signature1), out); | |
| 204 fprintf(out, "\n"); | |
| 205 | |
| 206 fclose(out); | |
| 207 | |
| 208 free_coeffs(coeffs); | |
| 209 pgm_freearray(image, rows); | |
| 210 | |
| 211 exit(0); | |
| 212 } |
