comparison Meerwald-dir/wm_cox_d.c @ 24:9f20bce6184e v0.7

move directories, support netpbm 11
author Peter Meerwald-Stadler <pmeerw@pmeerw.net>
date Fri, 20 Dec 2024 13:08:59 +0100
parents Meerwald/wm_cox_d.c@4987db85cfae
children
comparison
equal deleted inserted replaced
23:71dd4b96221b 24:9f20bce6184e
1 #include "wm.h"
2 #include "dct.h"
3 #include "netpbm/pgm.h"
4 #include "sort.h"
5
6 char *progname;
7
8 void usage(void) {
9 fprintf(stderr, "usage: %s [-a n] [-h] [-n n] [-o file] [-s file] [-v n] -i file file\n\n", progname);
10 fprintf(stderr, "\t-a n\t\talpha factor (default 0.3)\n");
11 fprintf(stderr, "\t-h\t\tprint usage\n");
12 fprintf(stderr, "\t-i file\t\toriginal image file\n");
13 fprintf(stderr, "\t-n n\t\twatermark length (default 100)\n");
14 fprintf(stderr, "\t-o file\t\textracted signature file\n");
15 fprintf(stderr, "\t-s file\t\toriginal signature file\n");
16 fprintf(stderr, "\t-v n\t\tverbosity level\n");
17 exit(0);
18 }
19
20 int main(int argc, char *argv[]) {
21
22 FILE *in = stdin;
23 FILE *out = stdout;
24 FILE *orig = NULL;
25 FILE *sig = NULL;
26
27 gray **input_image;
28 gray **orig_image;
29
30 char signature_name[MAXPATHLEN];
31 char output_name[MAXPATHLEN] = "(stdout)";
32 char input_name[MAXPATHLEN] = "(stdin)";
33 char orig_name[MAXPATHLEN];
34
35 int c;
36 int i, j;
37 int n = 100;
38
39 double a = 0.3;
40
41 int warn_n = 1;
42 int warn_a = 1;
43
44 int in_rows, in_cols, in_format;
45 gray in_maxval;
46 int orig_rows, orig_cols, orig_format;
47 gray orig_maxval;
48 int rows, cols;
49 int row;
50
51 double threshold;
52 double *largest;
53
54 double **input_dcts;
55 double **orig_dcts;
56
57 int verbose = 0;
58
59 progname = argv[0];
60
61 pgm_init(&argc, argv); wm_init2();
62
63 while ((c = getopt(argc, argv, "h?i:n:o:s:v:")) != EOF) {
64 switch (c) {
65 case 'a':
66 a = atof(optarg);
67 if (a <= 0.0) {
68 fprintf(stderr, "%s: alpha factor %f out of range\n", progname, a);
69 exit(1);
70 }
71 warn_a = 0;
72 break;
73 case 'h':
74 case '?':
75 usage();
76 break;
77 case 'i':
78 if ((orig = fopen(optarg, "rb")) == NULL) {
79 fprintf(stderr, "%s: unable to open original image file %s\n", progname, optarg);
80 exit(1);
81 }
82 strcpy(orig_name, optarg);
83 break;
84 case 'n':
85 n = atoi(optarg);
86 if (n < 1 || n > 1000) {
87 fprintf(stderr, "%s: watermark length %d out of range\n", progname, n);
88 exit(1);
89 }
90 warn_n = 0;
91 break;
92 case 'o':
93 if ((out = fopen(optarg, "w")) == NULL) {
94 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg);
95 exit(1);
96 }
97 strcpy(output_name, optarg);
98 break;
99 case 's':
100 if ((sig = fopen(optarg, "r")) == NULL) {
101 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg);
102 exit(1);
103 }
104 strcpy(signature_name, optarg);
105 break;
106 case 'v':
107 verbose = atoi(optarg);
108 if (verbose < 0) {
109 fprintf(stderr, "%s: verbosity level %d out of range\n", progname, verbose);
110 exit(1);
111 }
112 break;
113 }
114 }
115
116 argc -= optind;
117 argv += optind;
118
119 if (argc > 1) {
120 usage();
121 exit(1);
122 }
123
124 if (argc == 1 && *argv[0] != '-') {
125 if ((in = fopen(argv[0], "rb")) == NULL) {
126 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]);
127 exit(1);
128 }
129 else
130 strcpy(input_name, argv[0]);
131 }
132
133 if (!orig) {
134 fprintf(stderr, "%s: original image file not specified, use -i file option\n", progname);
135 exit(1);
136 }
137
138 if (sig) {
139 char line[32];
140 fgets(line, sizeof(line), sig);
141 if (strspn(line, "CXSG") >= 4) {
142 fscanf(sig, "%d\n", &n);
143 if (warn_a)
144 fscanf(sig, "%lf\n", &a);
145 else
146 fscanf(sig, "%*f\n");
147 fscanf(sig, "%*f\n");
148 fscanf(sig, "%*f\n");
149 }
150 else {
151 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name);
152 exit(1);
153 }
154 fclose(sig);
155 }
156 else {
157 if (warn_a)
158 fprintf(stderr, "%s: warning - alpha factor not specified, using default %f\n", progname, a);
159 if (warn_n)
160 fprintf(stderr, "%s: warning - watermark length not specified, using default %d\n", progname, n);
161 }
162
163 pgm_readpgminit(in, &in_cols, &in_rows, &in_maxval, &in_format);
164 pgm_readpgminit(orig, &orig_cols, &orig_rows, &orig_maxval, &orig_format);
165
166 if (in_cols != orig_cols || in_rows != orig_rows) {
167 fprintf(stderr, "%s: input image %s does not match dimensions of original image %s\n", progname, input_name, orig_name);
168 exit(1);
169 }
170
171 cols = in_cols;
172 rows = in_rows;
173
174 init_dct_NxN(cols, rows);
175
176 input_image = pgm_allocarray(in_cols, in_rows);
177
178 orig_image = pgm_allocarray(orig_cols, orig_rows);
179
180 for (row = 0; row < in_rows; row++)
181 pgm_readpgmrow(in, input_image[row], in_cols, in_maxval, in_format);
182
183 fclose(in);
184
185 for (row = 0; row < orig_rows; row++)
186 pgm_readpgmrow(orig, orig_image[row], orig_cols, orig_maxval, orig_format);
187
188 fclose(orig);
189
190 input_dcts = alloc_coeffs(cols, rows);
191 orig_dcts = alloc_coeffs(cols, rows);
192
193 fdct_NxN(input_image, input_dcts);
194 fdct_NxN(orig_image, orig_dcts);
195
196 largest = malloc((n + 1) * sizeof(double));
197 select_largest_coeffs(orig_dcts[0], cols * rows, n+1, largest);
198 threshold = largest[0];
199 free(largest);
200
201 fprintf(out, "CXWM\n");
202 fprintf(out, "%d\n", n);
203
204 j = 0;
205 for (i = 0; i < n; i++) {
206 double d, o, p;
207
208 while ((o = orig_dcts[j / cols][j % cols]) < threshold) j++;
209
210 p = input_dcts[j / cols][j % cols];
211
212 d = (p / o - 1.0) / a;
213 if (verbose >= 1)
214 fprintf(stderr, "input %f orig %f alpha %f d %f\n", p, o, a, d);
215 fprintf(out, "%f\n", d);
216 j++;
217 }
218
219 fclose(out);
220
221 free_coeffs(input_dcts);
222 free_coeffs(orig_dcts);
223
224 pgm_freearray(input_image, rows);
225 pgm_freearray(orig_image, rows);
226
227 exit(0);
228 }

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.