comparison Meerwald/wm_cox_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
5 char *progname;
6
7 void usage(void) {
8 fprintf(stderr, "usage: %s [-a n] [-h] [-n n] [-o file] [-s file] [-v n] -i file file\n\n", progname);
9 fprintf(stderr, "\t-a n\t\talpha factor (default 0.3)\n");
10 fprintf(stderr, "\t-h\t\tprint usage\n");
11 fprintf(stderr, "\t-i file\t\toriginal image file\n");
12 fprintf(stderr, "\t-n n\t\twatermark length (default 100)\n");
13 fprintf(stderr, "\t-o file\t\textracted signature file\n");
14 fprintf(stderr, "\t-s file\t\toriginal signature file\n");
15 fprintf(stderr, "\t-v n\t\tverbosity level\n");
16 exit(0);
17 }
18
19 int main(int argc, char *argv[]) {
20
21 FILE *in = stdin;
22 FILE *out = stdout;
23 FILE *orig = NULL;
24 FILE *sig = NULL;
25
26 gray **input_image;
27 gray **orig_image;
28
29 char signature_name[MAXPATHLEN];
30 char output_name[MAXPATHLEN] = "(stdout)";
31 char input_name[MAXPATHLEN] = "(stdin)";
32 char orig_name[MAXPATHLEN];
33
34 int c;
35 int r;
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, col;
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 if (!orig) {
133 fprintf(stderr, "%s: original image file not specified, use -i file option\n", progname);
134 exit(1);
135 }
136
137 if (sig) {
138 char line[32];
139 fgets(line, sizeof(line), sig);
140 if (strspn(line, "CXSG") >= 4) {
141 fscanf(sig, "%d\n", &n);
142 if (warn_a)
143 fscanf(sig, "%lf\n", &a);
144 else
145 fscanf(sig, "%*lf\n");
146 fscanf(sig, "%*lf\n");
147 fscanf(sig, "%*lf\n");
148 }
149 else {
150 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name);
151 exit(1);
152 }
153 fclose(sig);
154 }
155 else {
156 if (warn_a)
157 fprintf(stderr, "%s: warning - alpha factor not specified, using default %f\n", progname, a);
158 if (warn_n)
159 fprintf(stderr, "%s: warning - watermark length not specified, using default %d\n", progname, n);
160 }
161
162 pgm_readpgminit(in, &in_cols, &in_rows, &in_maxval, &in_format);
163 pgm_readpgminit(orig, &orig_cols, &orig_rows, &orig_maxval, &orig_format);
164
165 if (in_cols != orig_cols || in_rows != orig_rows) {
166 fprintf(stderr, "%s: input image %s does not match dimensions of original image %s\n", progname, input_name, orig_name);
167 exit(1);
168 }
169
170 cols = in_cols;
171 rows = in_rows;
172
173 init_dct_NxN(cols, rows);
174
175 input_image = pgm_allocarray(in_cols, in_rows);
176
177 orig_image = pgm_allocarray(orig_cols, orig_rows);
178
179 for (row = 0; row < in_rows; row++)
180 pgm_readpgmrow(in, input_image[row], in_cols, in_maxval, in_format);
181
182 fclose(in);
183
184 for (row = 0; row < orig_rows; row++)
185 pgm_readpgmrow(orig, orig_image[row], orig_cols, orig_maxval, orig_format);
186
187 fclose(orig);
188
189 input_dcts = alloc_coeffs(cols, rows);
190 orig_dcts = alloc_coeffs(cols, rows);
191
192 fdct_NxN(input_image, input_dcts);
193 fdct_NxN(orig_image, orig_dcts);
194
195 largest = malloc((n + 1) * sizeof(double));
196 select_largest_coeffs(orig_dcts[0], cols * rows, n+1, largest);
197 threshold = largest[0];
198 free(largest);
199
200 fprintf(out, "CXWM\n");
201 fprintf(out, "%d\n", n);
202
203 j = 0;
204 for (i = 0; i < n; i++) {
205 double d, o, p;
206
207 while ((o = orig_dcts[j / cols][j % cols]) < threshold) j++;
208
209 p = input_dcts[j / cols][j % cols];
210
211 d = (p / o - 1.0) / a;
212 if (verbose >= 1)
213 fprintf(stderr, "input %f orig %f alpha %f d %f\n", p, o, a, d);
214 fprintf(out, "%f\n", d);
215 j++;
216 }
217
218 fclose(out);
219
220 free_coeffs(input_dcts);
221 free_coeffs(orig_dcts);
222
223 pgm_freearray(input_image, rows);
224 pgm_freearray(orig_image, rows);
225
226 exit(0);
227 }

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