Mercurial > hg > wm
annotate Meerwald-dir/wm_cox_e.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_e.c@4987db85cfae |
children |
rev | line source |
---|---|
0 | 1 #include "wm.h" |
2 #include "dct.h" | |
24
9f20bce6184e
move directories, support netpbm 11
Peter Meerwald-Stadler <pmeerw@pmeerw.net>
parents:
16
diff
changeset
|
3 #include "netpbm/pgm.h" |
0 | 4 #include "sort.h" |
5 | |
6 char *progname; | |
7 | |
8 void usage(void) { | |
9 fprintf(stderr, "usage: %s [-a n] [-h] [-o file] -s file file\n\n", progname); | |
10 fprintf(stderr, "\t-a n\t\talpha factor/embedding strength\n"); | |
11 fprintf(stderr, "\t-h\t\tprint usage\n"); | |
12 fprintf(stderr, "\t-o file\t\toutput (watermarked) file\n"); | |
13 fprintf(stderr, "\t-s file\t\tsignature to embed in input image\n"); | |
14 exit(0); | |
15 } | |
16 | |
17 int main(int argc, char *argv[]) { | |
18 | |
19 FILE *in = stdin; | |
20 FILE *out = stdout; | |
21 FILE *sig = NULL; | |
22 | |
23 char output_name[MAXPATHLEN] = "(stdout)"; | |
24 char input_name[MAXPATHLEN] = "(stdin)"; | |
25 char signature_name[MAXPATHLEN]; | |
26 | |
27 int c; | |
8 | 28 int row; |
0 | 29 int i,j; |
30 | |
31 int n; | |
32 | |
33 double alpha = 0.0; | |
34 double threshold; | |
35 | |
36 double *largest; | |
37 gray **input_image; | |
38 gray **output_image; | |
39 double **dcts; | |
40 | |
41 gray maxval; | |
8 | 42 int rows, cols, format; |
0 | 43 |
44 progname = argv[0]; | |
45 | |
46 pgm_init(&argc, argv); wm_init(); | |
47 | |
48 while ((c = getopt(argc, argv, "a:h?o:s:")) != EOF) { | |
49 switch (c) { | |
50 case 'a': | |
51 alpha = atof(optarg); | |
52 if (alpha <= 0.0) { | |
53 fprintf(stderr, "%s: alpha factor %f out of range\n", progname, alpha); | |
54 exit(1); | |
55 } | |
56 break; | |
57 case 'h': | |
58 case '?': | |
59 usage(); | |
60 break; | |
61 case 'o': | |
62 if ((out = fopen(optarg, "wb")) == NULL) { | |
63 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg); | |
64 exit(1); | |
65 } | |
66 strcpy(output_name, optarg); | |
67 break; | |
68 case 's': | |
69 if ((sig = fopen(optarg, "r")) == NULL) { | |
70 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg); | |
71 exit(1); | |
72 } | |
73 strcpy(signature_name, optarg); | |
74 break; | |
75 } | |
76 } | |
77 | |
78 argc -= optind; | |
79 argv += optind; | |
80 | |
81 if (argc > 1) { | |
82 usage(); | |
83 exit(1); | |
84 } | |
85 | |
8 | 86 if (argc == 1 && *argv[0] != '-') { |
0 | 87 if ((in = fopen(argv[0], "rb")) == NULL) { |
88 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]); | |
89 exit(1); | |
90 } | |
91 else | |
92 strcpy(input_name, argv[0]); | |
8 | 93 } |
94 | |
0 | 95 if (sig) { |
96 char line[32]; | |
97 fgets(line, sizeof(line), sig); | |
98 if (strspn(line, "CXSG") >= 4) { | |
99 fscanf(sig, "%d\n", &n); | |
100 if (alpha == 0.0) | |
101 fscanf(sig, "%lf\n", &alpha); | |
102 else | |
16
4987db85cfae
fix another scanf() warning
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
8
diff
changeset
|
103 fscanf(sig, "%*f\n"); |
4987db85cfae
fix another scanf() warning
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
8
diff
changeset
|
104 fscanf(sig, "%*f\n"); |
4987db85cfae
fix another scanf() warning
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
8
diff
changeset
|
105 fscanf(sig, "%*f\n"); |
0 | 106 } |
107 else { | |
108 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name); | |
109 exit(1); | |
110 } | |
111 } | |
112 else { | |
113 fprintf(stderr, "%s: signature file not specified, use -s file option\n", progname); | |
114 exit(1); | |
115 } | |
116 | |
117 pgm_readpgminit(in, &cols, &rows, &maxval, &format); | |
118 | |
119 init_dct_NxN(cols, rows); | |
120 | |
121 dcts = alloc_coeffs(cols, rows); | |
122 input_image = pgm_allocarray(cols, rows); | |
123 | |
124 for (row = 0; row < rows; row++) | |
125 pgm_readpgmrow(in, input_image[row], cols, maxval, format); | |
126 | |
127 fclose(in); | |
128 | |
129 output_image = pgm_allocarray(cols, rows); | |
130 | |
131 fdct_NxN(input_image, dcts); | |
132 | |
133 largest = malloc((n + 1) * sizeof(double)); | |
134 select_largest_coeffs(dcts[0], cols * rows, n+1, largest); | |
135 threshold = largest[0]; | |
136 free(largest); | |
137 | |
138 j = 0; | |
139 for (i = 0; i < n; i++) { | |
140 double v; | |
141 | |
142 while (dcts[j / cols][j % cols] < threshold) j++; | |
143 | |
144 fscanf(sig, "%lf\n", &v); | |
145 dcts[j / cols][j % cols] *= (1.0 + alpha * v); | |
146 j++; | |
147 } | |
148 | |
149 idct_NxN(dcts, output_image); | |
150 free_coeffs(dcts); | |
151 | |
152 pgm_writepgminit(out, cols, rows, maxval, 0); | |
153 | |
154 for (row = 0; row < rows; row++) | |
155 pgm_writepgmrow(out, output_image[row], cols, maxval, 0); | |
156 | |
157 fclose(out); | |
158 | |
159 fclose(sig); | |
160 | |
161 pgm_freearray(output_image, rows); | |
162 pgm_freearray(input_image, rows); | |
163 | |
164 exit(0); | |
165 } |