Mercurial > hg > wm
annotate Meerwald/wm_frid2_d.c @ 21:1c4ccd635a68 v0.6
ignore *.swp
author | Peter Meerwald-Stadler <pmeerw@pmeerw.net> |
---|---|
date | Sat, 28 Jan 2023 23:57:40 +0100 |
parents | 4987db85cfae |
children |
rev | line source |
---|---|
0 | 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 double normalization = 1024.0; | |
33 int seed, format; | |
34 | |
35 double alpha = 0.0; | |
36 double beta = 0.0; | |
37 | |
38 double correlation; | |
39 | |
40 gray **image; | |
41 double **coeffs; | |
42 | |
43 double mean, derivation, mult_factor; | |
44 int rows, cols; | |
45 | |
46 gray maxval; | |
47 int verbose = 0; | |
48 | |
49 progname = argv[0]; | |
50 | |
51 pgm_init(&argc, argv); wm_init2(); | |
52 | |
53 while ((c = getopt(argc, argv, "a:b:n:h?s:o:v:")) != EOF) { | |
54 switch (c) { | |
55 case 'a': | |
56 alpha = atof(optarg); | |
57 if (alpha <= 0.0) { | |
58 fprintf(stderr, "%s: alpha factor %f out of range\n", progname, alpha); | |
59 exit(1); | |
60 } | |
61 break; | |
62 case 'b': | |
63 beta = atof(optarg); | |
64 if (beta <= 0.0) { | |
65 fprintf(stderr, "%s: beta factor %f out of range\n", progname, beta); | |
66 exit(1); | |
67 } | |
68 break; | |
69 case 'n': | |
70 normalization = atof(optarg); | |
71 if (normalization < 0) { | |
72 fprintf(stderr, "%s: normalisation factor %f out of range\n", progname, normalization); | |
73 exit(1); | |
74 } | |
75 break; | |
76 case 'h': | |
77 case '?': | |
78 usage(); | |
79 break; | |
80 case 'o': | |
81 if ((out = fopen(optarg, "wb")) == NULL) { | |
82 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg); | |
83 exit(1); | |
84 } | |
85 strcpy(output_name, optarg); | |
86 break; | |
87 case 's': | |
88 if ((sig = fopen(optarg, "r")) == NULL) { | |
89 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg); | |
90 exit(1); | |
91 } | |
92 strcpy(signature_name, optarg); | |
93 break; | |
94 case 'v': | |
95 verbose = atoi(optarg); | |
96 if (verbose < 0) { | |
97 fprintf(stderr, "%s: verbosity level %d out of range\n", progname, verbose); | |
98 exit(1); | |
99 } | |
100 break; | |
101 } | |
102 } | |
103 | |
104 argc -= optind; | |
105 argv += optind; | |
106 | |
107 if (argc > 1) { | |
108 usage(); | |
109 exit(1); | |
110 } | |
111 | |
8 | 112 if (argc == 1 && *argv[0] != '-') { |
0 | 113 if ((in = fopen(argv[0], "rb")) == NULL) { |
114 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]); | |
115 exit(1); | |
116 } | |
117 else | |
118 strcpy(input_name, argv[0]); | |
8 | 119 } |
120 | |
0 | 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 | |
16
4987db85cfae
fix another scanf() warning
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
8
diff
changeset
|
129 fscanf(sig, "%*f\n"); |
4987db85cfae
fix another scanf() warning
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
8
diff
changeset
|
130 fscanf(sig, "%*f\n"); |
0 | 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 } |