Mercurial > hg > wm
annotate Meerwald/frid2_common.c @ 22:d8551fb39a5e default tip
Added tag v0.6 for changeset 1c4ccd635a68
author | Peter Meerwald-Stadler <pmeerw@pmeerw.net> |
---|---|
date | Sat, 28 Jan 2023 23:57:51 +0100 |
parents | bd669312f068 |
children |
rev | line source |
---|---|
0 | 1 #include "frid2_common.h" |
20
bd669312f068
suppress warnings, fix link errors
Peter Meerwald-Stadler <pmeerw@pmeerw.net>
parents:
0
diff
changeset
|
2 #include "signature-utils.h" |
0 | 3 #include "wm.h" |
4 | |
5 extern char *progname; | |
6 | |
7 void embed_low_freq(double **dcts, int cols, int rows, double alpha, int verbose) { | |
8 int n; | |
9 int row, col, dir; | |
10 | |
11 n = 0; | |
12 row = col = 0; | |
13 dir = 1; | |
14 while (n < nbit_signature) { | |
15 double d, x; | |
16 int embed; | |
17 int out; | |
18 | |
19 col -= dir; | |
20 row += dir; | |
21 if (col < 0) { dir = -1; col = 0; } | |
22 if (col >= cols) { dir = 1; col = cols - 1; row += 2; } | |
23 if (row < 0) { dir = 1; row = 0; } | |
24 if (row >= rows) { dir = -1; row = rows - 1; col += 2; } | |
25 | |
26 d = dcts[row][col]; | |
27 if (fabs(d) <= 1.0) { | |
28 if (verbose > 3) | |
29 fprintf(stderr, "%s: bit #%d - skipped (%d/%d)\n", progname, n, col, row); | |
30 continue; | |
31 } | |
32 | |
33 embed = 2 * get_signature_bit(n) - 1; | |
34 | |
35 x = (d > 0.0) ? 1.0 : -1.0; | |
36 out = 1; | |
37 while (fabs(x) < fabs(d)) { | |
38 x *= FORWARD_STEP(alpha); | |
39 out =- out; | |
40 } | |
41 | |
42 if (out != embed) { | |
43 if (fabs(d - x) < fabs(d - x * BACKWARD_STEP(alpha))) | |
44 x *= FORWARD_STEP(alpha); | |
45 else | |
20
bd669312f068
suppress warnings, fix link errors
Peter Meerwald-Stadler <pmeerw@pmeerw.net>
parents:
0
diff
changeset
|
46 x *= BACKWARD_STEP(alpha); |
0 | 47 } |
48 | |
49 d = (x + x * BACKWARD_STEP(alpha)) / 2.0; | |
50 | |
51 if (verbose > 3) | |
52 fprintf(stderr, "%s: embedding bit #%d (= %d) at (%d/%d): %f -> %f\n", progname, n, get_signature_bit(n), col, row, dcts[row][col], d); | |
53 | |
54 dcts[row][col] = d; | |
55 | |
56 n++; | |
20
bd669312f068
suppress warnings, fix link errors
Peter Meerwald-Stadler <pmeerw@pmeerw.net>
parents:
0
diff
changeset
|
57 } |
bd669312f068
suppress warnings, fix link errors
Peter Meerwald-Stadler <pmeerw@pmeerw.net>
parents:
0
diff
changeset
|
58 } |
0 | 59 |
60 void embed_med_freq(double **dcts, int cols, int rows, double gamma, int seed, int verbose) { | |
61 // select mid-frequency (30%) coefficients | |
62 int start = (int) (0.35 * rows * cols + 0.5); | |
63 int end = (int) (0.65 * rows * cols + 0.5); | |
64 | |
65 double *vector; | |
66 int x = 0, y = 0, dir = 1; | |
20
bd669312f068
suppress warnings, fix link errors
Peter Meerwald-Stadler <pmeerw@pmeerw.net>
parents:
0
diff
changeset
|
67 int i, j; |
0 | 68 |
69 vector = malloc((end - start) * sizeof(double)); | |
70 for (i = 0; i < (end - start); i++) | |
71 vector[i] = 0.0; | |
20
bd669312f068
suppress warnings, fix link errors
Peter Meerwald-Stadler <pmeerw@pmeerw.net>
parents:
0
diff
changeset
|
72 |
0 | 73 // create pseudo-random vector |
74 srandom(seed); | |
75 for (i = 0; i < nbit_signature; i++) { | |
76 if (get_signature_bit(i)) | |
77 random(); | |
78 for (j = 0; j < (end - start); j++) | |
79 vector[j] += (double) (random() & RAND_MAX) / (double) RAND_MAX - 0.5; | |
80 if (!get_signature_bit(i)) | |
81 random(); | |
82 } | |
83 | |
84 for (i = 0; i < (end - start); i++) | |
85 vector[i] /= sqrt(nbit_signature); | |
86 | |
87 for (i = 0; i < end; i++) { | |
88 // zig-zag scan | |
89 x -= dir; | |
90 y += dir; | |
91 if (x < 0) { dir = -1; x = 0; } | |
92 if (x >= cols) { dir = 1; x = cols - 1; y += 2; } | |
93 if (y < 0) { dir = 1; y = 0; } | |
94 if (y >= rows) { dir = -1; y = rows - 1; x += 2; } | |
95 | |
96 // embed vector | |
97 if ((i - start) >= 0) { | |
98 // fprintf(stderr, "%d/%d: %f -> %f\n", x, y, dcts[y][x], dcts[y][x] + gamma * vector[i - start]); | |
99 dcts[y][x] += gamma * vector[i - start]; | |
100 } | |
20
bd669312f068
suppress warnings, fix link errors
Peter Meerwald-Stadler <pmeerw@pmeerw.net>
parents:
0
diff
changeset
|
101 } |
0 | 102 |
103 free(vector); | |
104 } | |
105 | |
106 double detect_low_freq(double **dcts, int cols, int rows, double alpha, double beta, int verbose) { | |
107 int n; | |
108 int row, col, dir; | |
109 double sum1, sum2; | |
110 | |
111 n = 0; | |
112 row = col = 0; | |
113 dir = 1; | |
114 sum1 = sum2 = 0.0; | |
115 while (n < nbit_signature1) { | |
116 double d, x; | |
117 int detect; | |
118 int out; | |
119 | |
120 col -= dir; | |
121 row += dir; | |
122 if (col < 0) { dir = -1; col = 0; } | |
123 if (col >= cols) { dir = 1; col = cols - 1; row += 2; } | |
124 if (row < 0) { dir = 1; row = 0; } | |
125 if (row >= rows) { dir = -1; row = rows - 1; col += 2; } | |
126 | |
127 d = dcts[row][col]; | |
128 if (fabs(d) <= 1.0) { | |
129 if (verbose > 3) | |
130 fprintf(stderr, "%s: bit #%d - skipped (%d/%d)\n", progname, n, col, row); | |
131 continue; | |
132 } | |
133 | |
134 detect = 2 * get_signature1_bit(n) - 1; | |
135 | |
136 x = (d > 0.0) ? 1.0 : -1.0; | |
137 out = 1; | |
138 while (fabs(x) < fabs(d)) { | |
139 x *= FORWARD_STEP(alpha); | |
140 out =- out; | |
141 } | |
142 | |
143 if (verbose > 3) | |
144 fprintf(stderr, "%s: detected bit #%d (= %d) at (%d/%d): %f\n", progname, n, out > 0 ? 1 : 0, col, row, d); | |
145 | |
146 set_signature2_bit(n, out > 0 ? 1 : 0); | |
147 sum1 += pow(fabs(d), beta) * out * detect; | |
148 sum2 += pow(fabs(d), beta); | |
149 | |
150 n++; | |
20
bd669312f068
suppress warnings, fix link errors
Peter Meerwald-Stadler <pmeerw@pmeerw.net>
parents:
0
diff
changeset
|
151 } |
0 | 152 |
153 return sum1 / sum2; | |
154 } | |
155 | |
156 double detect_med_freq(double **dcts, int cols, int rows, int seed, int verbose) { | |
157 int i, j, k; | |
158 int start= (int) (0.35 * rows * cols + 0.5); | |
159 int end = (int) (.65 * rows * cols + 0.5); | |
160 | |
161 int sum, sum1, sum2; | |
162 int x = 0, y = 0, dir = 1; | |
163 double *vector; | |
164 int startx, starty, startdir; | |
165 double corr[2]; | |
166 double correlation; | |
167 | |
20
bd669312f068
suppress warnings, fix link errors
Peter Meerwald-Stadler <pmeerw@pmeerw.net>
parents:
0
diff
changeset
|
168 // locate start positions |
0 | 169 for (i = 0; i < start; i++) { |
170 x -= dir; | |
171 y += dir; | |
172 if (x < 0) { dir = -1; x = 0; } | |
173 if (x >= cols) { dir = 1; x = cols - 1; y += 2; } | |
174 if (y < 0) { dir = 1; y = 0; } | |
175 if (y >= rows) { dir = -1; y = rows - 1; x += 2; } | |
20
bd669312f068
suppress warnings, fix link errors
Peter Meerwald-Stadler <pmeerw@pmeerw.net>
parents:
0
diff
changeset
|
176 } |
0 | 177 |
178 // save start positions | |
20
bd669312f068
suppress warnings, fix link errors
Peter Meerwald-Stadler <pmeerw@pmeerw.net>
parents:
0
diff
changeset
|
179 startx = x; |
0 | 180 starty = y; |
181 startdir = dir; | |
182 srandom(seed); | |
183 | |
184 vector = malloc((end - start) * sizeof(double)); | |
185 | |
186 for (i = 0; i < nbit_signature1; i++) { | |
187 | |
188 for (j = 0; j <= (end - start); j++) | |
189 vector[j] = (double) (random() & RAND_MAX) / (double) RAND_MAX - 0.5; | |
20
bd669312f068
suppress warnings, fix link errors
Peter Meerwald-Stadler <pmeerw@pmeerw.net>
parents:
0
diff
changeset
|
190 |
0 | 191 for (j = 0; j <= 1; j++) { |
192 x = startx; | |
193 y = starty; | |
194 dir = startdir; | |
195 corr[j] = 0; | |
196 | |
20
bd669312f068
suppress warnings, fix link errors
Peter Meerwald-Stadler <pmeerw@pmeerw.net>
parents:
0
diff
changeset
|
197 for (k = 0; start + k < end; k++) { |
0 | 198 x -= dir; |
199 y += dir; | |
200 if (x < 0) { dir = -1; x = 0; } | |
201 if (x >= cols) { dir = 1; x = cols - 1; y += 2; } | |
202 if (y < 0) { dir = 1; y = 0; } | |
203 if (y >= rows) { dir = -1; y = rows - 1; x += 2; } | |
204 corr[j] += dcts[y][x] * vector[k + j]; | |
205 } | |
20
bd669312f068
suppress warnings, fix link errors
Peter Meerwald-Stadler <pmeerw@pmeerw.net>
parents:
0
diff
changeset
|
206 } |
0 | 207 |
208 set_signature2_bit(i, (corr[0] >= corr[1]) ? 0 : 1); | |
209 } | |
210 | |
211 sum = 0; sum1 = 0; sum2 = 0; | |
212 for (i = 0; i < nbit_signature1; i++) { | |
213 sum += get_signature1_bit(i) * get_signature2_bit(i); | |
214 sum1 += get_signature1_bit(i) * get_signature1_bit(i); | |
215 sum2 += get_signature2_bit(i) * get_signature2_bit(i); | |
216 } | |
217 correlation = (double) sum / (sqrt(sum1) * sqrt(sum2)); | |
218 | |
20
bd669312f068
suppress warnings, fix link errors
Peter Meerwald-Stadler <pmeerw@pmeerw.net>
parents:
0
diff
changeset
|
219 return correlation; |
0 | 220 } |