0
|
1 #include "wm.h"
|
|
2 #include "dct.h"
|
|
3 #include "pgm.h"
|
|
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;
|
|
28 int row, col;
|
|
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;
|
|
42 int rows, cols, colors, format;
|
|
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
|
|
86 if (argc == 1 && *argv[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]);
|
|
93
|
|
94 if (sig) {
|
|
95 char line[32];
|
|
96 fgets(line, sizeof(line), sig);
|
|
97 if (strspn(line, "CXSG") >= 4) {
|
|
98 fscanf(sig, "%d\n", &n);
|
|
99 if (alpha == 0.0)
|
|
100 fscanf(sig, "%lf\n", &alpha);
|
|
101 else
|
|
102 fscanf(sig, "%*lf\n");
|
|
103 fscanf(sig, "%*lf\n");
|
|
104 fscanf(sig, "%*lf\n");
|
|
105 }
|
|
106 else {
|
|
107 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name);
|
|
108 exit(1);
|
|
109 }
|
|
110 }
|
|
111 else {
|
|
112 fprintf(stderr, "%s: signature file not specified, use -s file option\n", progname);
|
|
113 exit(1);
|
|
114 }
|
|
115
|
|
116 pgm_readpgminit(in, &cols, &rows, &maxval, &format);
|
|
117
|
|
118 init_dct_NxN(cols, rows);
|
|
119
|
|
120 dcts = alloc_coeffs(cols, rows);
|
|
121 input_image = pgm_allocarray(cols, rows);
|
|
122
|
|
123 for (row = 0; row < rows; row++)
|
|
124 pgm_readpgmrow(in, input_image[row], cols, maxval, format);
|
|
125
|
|
126 fclose(in);
|
|
127
|
|
128 output_image = pgm_allocarray(cols, rows);
|
|
129
|
|
130 fdct_NxN(input_image, dcts);
|
|
131
|
|
132 largest = malloc((n + 1) * sizeof(double));
|
|
133 select_largest_coeffs(dcts[0], cols * rows, n+1, largest);
|
|
134 threshold = largest[0];
|
|
135 free(largest);
|
|
136
|
|
137 j = 0;
|
|
138 for (i = 0; i < n; i++) {
|
|
139 double v;
|
|
140
|
|
141 while (dcts[j / cols][j % cols] < threshold) j++;
|
|
142
|
|
143 fscanf(sig, "%lf\n", &v);
|
|
144 dcts[j / cols][j % cols] *= (1.0 + alpha * v);
|
|
145 j++;
|
|
146 }
|
|
147
|
|
148 idct_NxN(dcts, output_image);
|
|
149 free_coeffs(dcts);
|
|
150
|
|
151 pgm_writepgminit(out, cols, rows, maxval, 0);
|
|
152
|
|
153 for (row = 0; row < rows; row++)
|
|
154 pgm_writepgmrow(out, output_image[row], cols, maxval, 0);
|
|
155
|
|
156 fclose(out);
|
|
157
|
|
158 fclose(sig);
|
|
159
|
|
160 pgm_freearray(output_image, rows);
|
|
161 pgm_freearray(input_image, rows);
|
|
162
|
|
163 exit(0);
|
|
164 }
|