annotate Meerwald/wm_bruyn_e.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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
1 #include "wm.h"
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
2 #include "signature.h"
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
3 #include "coord.h"
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
4 #include "gray.h"
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
5 #include "sort.h"
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
6 #include "bruyn_common.h"
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
7 #include "pgm.h"
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
8
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
9 char *progname;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
10
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
11 // prints out program's parameters
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
12 void usage(void) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
13 fprintf(stderr, "usage: %s [-b n] [-h] [-k] [-n n] [-o file] [-pP n] [-q n] [-tT n] [-v n] -s file file\n", progname);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
14 fprintf(stderr, "\t-b n\t\tblock size\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
15 fprintf(stderr, "\t-h\t\tprint usage\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
16 fprintf(stderr, "\t-k\t\tdisable block skipping\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
17 fprintf(stderr, "\t-n n\t\tnumber of signature bits to embed\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
18 fprintf(stderr, "\t-o file\t\toutput (watermarked) file\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
19 fprintf(stderr, "\t-p n\t\tpattern type for zone 1\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
20 fprintf(stderr, "\t-P n\t\tpattern type for zone 2\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
21 fprintf(stderr, "\t-q n\t\tsignature strength\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
22 fprintf(stderr, "\t-s file\t\tsignature to embed in input image\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
23 fprintf(stderr, "\t-t n\t\tthreshold for noise\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
24 fprintf(stderr, "\t-T n\t\tthreshold for slope\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
25 fprintf(stderr, "\t-v n\t\tverbosity level\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
26 exit(0);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
27 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
28
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
29 int main(int argc, char *argv[]) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
30 FILE *in = stdin;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
31 FILE *out = stdout;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
32 FILE *sig = NULL;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
33
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
34 gray** image;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
35 gray **block;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
36 gray **zone;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
37 gray **category1, **category2;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
38 gray maxval;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
39 double *slope;
8
f83ef905a63d fixing many warnings
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
40 int rows, cols, format;
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
41 int c;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
42 int i, j;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
43 int n;
20
bd669312f068 suppress warnings, fix link errors
Peter Meerwald-Stadler <pmeerw@pmeerw.net>
parents: 16
diff changeset
44 int row;
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
45 int n_block;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
46 int skipping = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
47
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
48 char signature_name[MAXPATHLEN];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
49 char input_name[MAXPATHLEN] = "(stdin)";
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
50 char output_name[MAXPATHLEN] = "(stdout)";
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
51
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
52 double quality = 0.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
53 double threshold_noise = 0.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
54 double threshold_slope = 0.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
55 int pattern1 = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
56 int pattern2 = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
57 int blocksize = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
58 int seed;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
59
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
60 int verbose = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
61
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
62 struct coords *coords;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
63
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
64 progname = argv[0];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
65
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
66 pgm_init(&argc, argv); wm_init();
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
67
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
68 // parse command line and set options
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
69 while ((c = getopt(argc, argv, "b:h?n:o:p:P:q:s:t:T:v:k")) != EOF) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
70 switch (c) {
20
bd669312f068 suppress warnings, fix link errors
Peter Meerwald-Stadler <pmeerw@pmeerw.net>
parents: 16
diff changeset
71 case 'k':
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
72 skipping = 1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
73 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
74 case 'h':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
75 case '?':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
76 usage();
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
77 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
78 case 'n':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
79 nbit_signature = atoi(optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
80 if (nbit_signature <= 0 || nbit_signature > NBITSIGNATURE) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
81 fprintf(stderr, "%s: invalid signature length %d\n", progname, nbit_signature);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
82 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
83 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
84 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
85 case 'o':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
86 if ((out = fopen(optarg, "wb")) == NULL) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
87 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
88 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
89 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
90 strcpy(output_name, optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
91 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
92 case 'p':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
93 pattern1 = atoi(optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
94 if (pattern1 <= 0 || pattern1 > NPATTERN) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
95 fprintf(stderr, "%s: pattern type out of range\n", progname);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
96 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
97 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
98 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
99 case 'P':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
100 pattern2 = atoi(optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
101 if (pattern2 <= 0 || pattern2 > 3) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
102 fprintf(stderr, "%s: pattern type out of range\n", progname);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
103 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
104 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
105 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
106 case 'q':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
107 quality = atof(optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
108 if (quality <= 0) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
109 fprintf(stderr, "%s: quality factor %f out of range\n", progname, quality);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
110 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
111 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
112 case 's':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
113 if ((sig = fopen(optarg, "r")) == NULL) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
114 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
115 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
116 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
117 strcpy(signature_name, optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
118 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
119 case 't':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
120 threshold_noise = atof(optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
121 if (threshold_noise <= 0) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
122 fprintf(stderr, "%s: noise threshold %f out of range\n", progname, threshold_noise);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
123 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
124 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
125 case 'T':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
126 threshold_slope = atof(optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
127 if (threshold_slope <= 0) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
128 fprintf(stderr, "%s: slope threshold %f out of range\n", progname, threshold_slope);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
129 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
130 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
131 case 'v':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
132 verbose = atoi(optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
133 if (verbose < 0) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
134 fprintf(stderr, "%s: verbosity level %d out of range\n",progname, verbose);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
135 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
136 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
137 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
138 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
139 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
140
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
141 argc -= optind;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
142 argv += optind;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
143
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
144 if (argc > 1) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
145 usage();
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
146 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
147 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
148
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
149 // open input image file or read from stdin
8
f83ef905a63d fixing many warnings
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
150 if (argc == 1 && *argv[0] != '-') {
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
151 if ((in = fopen(argv[0], "rb")) == NULL) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
152 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
153 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
154 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
155 else
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
156 strcpy(input_name, argv[0]);
8
f83ef905a63d fixing many warnings
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
157 }
f83ef905a63d fixing many warnings
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
158
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
159 // read signature file and set options
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
160 // command line options override signature file options
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
161 if (sig) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
162 char line[128];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
163 fgets(line, sizeof(line), sig);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
164 if (strspn(line, "BRSG") >= 4) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
165 if (nbit_signature == 0)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
166 fscanf(sig, "%d\n", &nbit_signature);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
167 else
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
168 fscanf(sig, "%*d\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
169 if (skipping == 0)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
170 fscanf(sig, "%d\n", &skipping);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
171 else
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
172 fscanf(sig, "%*d\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
173 if (pattern1 == 0)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
174 fscanf(sig, "%d\n", &pattern1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
175 else
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
176 fscanf(sig, "%*d\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
177 if (pattern2 == 0)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
178 fscanf(sig, "%d\n", &pattern2);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
179 else
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
180 fscanf(sig, "%*d\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
181 if (quality == 0.0)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
182 fscanf(sig, "%lf\n", &quality);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
183 else
16
4987db85cfae fix another scanf() warning
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 8
diff changeset
184 fscanf(sig, "%*f\n");
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
185 if (threshold_noise == 0.0)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
186 fscanf(sig, "%lf\n", &threshold_noise);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
187 else
16
4987db85cfae fix another scanf() warning
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 8
diff changeset
188 fscanf(sig, "%*f\n");
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
189 if (threshold_slope == 0.0)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
190 fscanf(sig, "%lf\n", &threshold_slope);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
191 else
16
4987db85cfae fix another scanf() warning
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 8
diff changeset
192 fscanf(sig, "%*f\n");
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
193 if (blocksize == 0)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
194 fscanf(sig, "%d\n", &blocksize);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
195 else
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
196 fscanf(sig, "%*d\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
197 fscanf(sig, "%d\n", &seed);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
198 srandom(seed);
20
bd669312f068 suppress warnings, fix link errors
Peter Meerwald-Stadler <pmeerw@pmeerw.net>
parents: 16
diff changeset
199 fread(signature, sizeof(char), NBITSTOBYTES(nbit_signature), sig);
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
200 fscanf(sig, "\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
201 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
202 else {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
203 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
204 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
205 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
206 fclose(sig);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
207 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
208 else {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
209 fprintf(stderr, "%s: signature file not specified, use -s file option\n", progname);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
210 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
211 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
212
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
213 if (pattern1 <= 0 || pattern2 <= 0 || pattern1 > NPATTERN || pattern2 > NPATTERN) {
8
f83ef905a63d fixing many warnings
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
214 fprintf(stderr, "%s: invalid pattern type specified\n", progname);
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
215 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
216 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
217
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
218 // read dimensions of input image file
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
219 pgm_readpgminit(in, &cols, &rows, &maxval, &format);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
220
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
221 // see if we can embed all signature bits
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
222 // we want at least half of the blocks untouched
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
223 if (((rows / blocksize) * (cols / blocksize)) < nbit_signature / 2) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
224 fprintf(stderr, "%s: image not large enough to embed %d bits of signature\n", progname, nbit_signature);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
225 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
226 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
227 n_block = blocksize * blocksize;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
228
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
229 // allocate structure to remember which blocks we already touched,
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
230 // allow plenty of room to skip over blocks
8
f83ef905a63d fixing many warnings
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
231 if ((coords = alloc_coords(nbit_signature * 16)) == NULL) {
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
232 fprintf(stderr, "%s: unable to allocate memory\n", progname);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
233 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
234 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
235
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
236 // read in input image file
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
237 image = pgm_allocarray(cols, rows);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
238 for (row = 0; row < rows; row++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
239 pgm_readpgmrow(in, image[row], cols, maxval, format);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
240
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
241 fclose(in);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
242
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
243 row = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
244
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
245 // allocate memory for one block
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
246 block = alloc_grays(blocksize, blocksize);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
247
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
248 // allocate memory for zone classification
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
249 zone = alloc_grays(blocksize, blocksize);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
250
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
251 // allocate memory for category classification
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
252 category1 = alloc_grays(blocksize, blocksize);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
253 category2 = alloc_grays(blocksize, blocksize);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
254
20
bd669312f068 suppress warnings, fix link errors
Peter Meerwald-Stadler <pmeerw@pmeerw.net>
parents: 16
diff changeset
255 // set up category classification array according to
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
256 // pattern type parameter
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
257 for (i = 0; i < blocksize; i++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
258 for (j = 0; j < blocksize; j++) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
259 category1[j][i] = lookup_pattern(pattern1, i, j);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
260 category2[j][i] = lookup_pattern(pattern2, i, j);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
261 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
262
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
263 // allocate memory for slope calculation
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
264 slope = malloc(sizeof(double) * n_block);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
265
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
266 // embed all the signature bits, one by one
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
267 n = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
268 while (n < nbit_signature) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
269 int xb;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
270 int yb;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
271 int blocktype;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
272 double smax;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
273 int alpha, beta_minus, beta_plus;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
274 double mean_1A, mean_1B, mean_2A, mean_2B, mean_1, mean_2;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
275 double mean__1A, mean__1B, mean__2A, mean__2B;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
276 int n_1A, n_1B, n_2A, n_2B, n_1, n_2;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
277 int var_1A, var_1B, var_2A, var_2B;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
278 int zone1_ok, zone2_ok;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
279
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
280 // find an unused block randomly, depending on seed
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
281 do {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
282 xb = random() % (cols / blocksize);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
283 yb = random() % (rows / blocksize);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
284 } while (add_coord(coords, xb, yb) < 0);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
285
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
286 // copy image block
20
bd669312f068 suppress warnings, fix link errors
Peter Meerwald-Stadler <pmeerw@pmeerw.net>
parents: 16
diff changeset
287 copy_grays_to_block(block, image, xb * blocksize, yb * blocksize, blocksize, blocksize);
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
288
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
289 if (verbose > 0)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
290 fprintf(stderr, "embedding bit #%d (= %d) in block at (%d/%d)\n", n, get_signature_bit(n), xb * blocksize, yb * blocksize);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
291 if (verbose > 8) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
292 print_grays(image, xb * blocksize, yb * blocksize, blocksize, blocksize);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
293 fprintf(stderr, "\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
294 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
295
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
296 // sort luminance values in block to represent increasing function F
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
297 sort_grays(block[0], n_block);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
298
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
299 if (verbose > 8) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
300 print_grays(block, 0, 0, blocksize, blocksize);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
301 fprintf(stderr, "\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
302 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
303
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
304 // calculate slopes of F and determine smax, the max. slope of F
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
305 // the index where smax occures is called alpha
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
306 alpha = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
307 smax = 0.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
308 for (i = 0; i < n_block - 1; i++) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
309 slope[i] = block[0][i + 1] - block[0][i];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
310 if (slope[i] > smax) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
311 smax = slope[i];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
312 alpha = i;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
313 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
314 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
315 slope[n_block - 1] = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
316
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
317 // block type classification
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
318 blocktype = BLOCKTYPE_UNKNOWN;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
319
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
320 if (smax < threshold_noise) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
321 // block has noise contrast
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
322
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
323 blocktype = BLOCKTYPE_NOISE;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
324 beta_minus = beta_plus = alpha;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
325 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
326 else {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
327 // block has progressive or hard contrast, let's find out...
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
328
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
329 beta_minus = alpha - 1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
330 while (beta_minus >= 0 && smax - slope[beta_minus] <= threshold_slope)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
331 beta_minus--;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
332
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
333 beta_plus = alpha + 1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
334 while (beta_plus < n_block && smax - slope[beta_plus] <= threshold_slope)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
335 beta_plus++;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
336
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
337 if (beta_minus + 1 == alpha && beta_plus - 1 == alpha)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
338 blocktype = BLOCKTYPE_HARD;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
339 else
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
340 blocktype = BLOCKTYPE_PROGRESSIVE;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
341 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
342
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
343 if (verbose > 1) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
344 fprintf(stderr, "blocktype: %d\n", blocktype);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
345 fprintf(stderr, "Smax = %lf, alpha = %d, beta- = %d, beta+ = %d\n", smax, alpha, beta_minus, beta_plus);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
346 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
347
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
348 // block pixel classification
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
349 for (i = 0; i < blocksize; i++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
350 for (j = 0; j < blocksize; j++) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
351 gray pixel = image[yb * blocksize + j][xb * blocksize + i];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
352 zone[j][i] = ZONE_VOID;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
353 switch (blocktype) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
354 case BLOCKTYPE_PROGRESSIVE:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
355 case BLOCKTYPE_HARD:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
356 if (pixel < block[0][beta_minus])
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
357 zone[j][i] = ZONE_1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
358 else if (pixel > block[0][beta_plus])
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
359 zone[j][i] = ZONE_2;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
360 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
361 case BLOCKTYPE_NOISE:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
362 if (pixel < block[0][n_block / 2])
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
363 zone[j][i] = ZONE_1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
364 else if (pixel > block[0][n_block / 2])
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
365 zone[j][i] = ZONE_2;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
366 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
367 default:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
368 fprintf(stderr, "%s: invalid block type\n", progname);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
369 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
370 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
371 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
372
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
373 if (verbose > 8) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
374 print_grays(zone, 0, 0, blocksize, blocksize);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
375 fprintf(stderr, "\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
376 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
377
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
378 // calculate mean values for zone/categories
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
379 mean_1A = mean_1B = mean_2A = mean_2B = mean_1 = mean_2 = 0.0;
8
f83ef905a63d fixing many warnings
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
380 mean__1A = mean__1B = mean__2A = mean__2B = 0.0;
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
381 n_1A = n_1B = n_2A = n_2B = n_1 = n_2 = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
382 for (i = 0; i < blocksize; i++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
383 for (j = 0; j < blocksize; j++) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
384 gray pixel = image[yb * blocksize + j][xb * blocksize + i];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
385 int pixel_zone = zone[j][i];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
386 int pixel_category = CATEGORY_VOID;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
387 if (pixel_zone == ZONE_1)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
388 pixel_category = category1[j][i];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
389 else if (pixel_zone == ZONE_2)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
390 pixel_category = category2[j][i];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
391
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
392 switch (pixel_zone | pixel_category) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
393 case CLASSIFICATION_1A:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
394 n_1++;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
395 n_1A++;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
396 mean_1A += pixel;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
397 mean_1 += pixel;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
398 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
399 case CLASSIFICATION_1B:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
400 n_1++;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
401 n_1B++;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
402 mean_1B += pixel;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
403 mean_1 += pixel;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
404 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
405 case CLASSIFICATION_2A:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
406 n_2++;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
407 n_2A++;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
408 mean_2A += pixel;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
409 mean_2 += pixel;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
410 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
411 case CLASSIFICATION_2B:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
412 n_2++;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
413 n_2B++;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
414 mean_2B += pixel;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
415 mean_2 += pixel;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
416 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
417 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
418 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
419
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
420 if (n_1 && n_1A && n_1B) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
421 mean_1 /= (double) n_1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
422 mean_1A /= (double) n_1A;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
423 mean_1B /= (double) n_1B;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
424 zone1_ok = 1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
425 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
426 else {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
427 mean_1 = mean_1A = mean_1B = 0.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
428 zone1_ok = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
429 if (verbose > 0)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
430 fprintf(stderr, "zone 1 unusable\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
431 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
432
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
433 if (n_2 && n_2A && n_2B) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
434 mean_2 /= (double) n_2;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
435 mean_2A /= (double) n_2A;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
436 mean_2B /= (double) n_2B;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
437 zone2_ok = 1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
438 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
439 else {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
440 mean_2 = mean_2A = mean_2B = 0.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
441 zone2_ok = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
442 if (verbose > 0)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
443 fprintf(stderr, "zone 2 unusable\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
444 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
445
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
446 if (!skipping && !zone1_ok && !zone2_ok) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
447 // pathological case - can it ever happen?
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
448 if (verbose > 0)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
449 fprintf(stderr, "block skipped\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
450 continue;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
451 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
452
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
453 if (verbose > 2) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
454 fprintf(stderr, "m_1 = %lf, m_1A = %lf, m_1B = %lf\n", mean_1, mean_1A, mean_1B);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
455 fprintf(stderr, "m_2 = %lf, m_2A = %lf, m_2B = %lf\n", mean_2, mean_2A, mean_2B);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
456 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
457
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
458 // calculate new mean values required by embedding rule
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
459 if (get_signature_bit(n)) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
460 if (zone1_ok) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
461 mean__1A = (mean_1 * (double) (n_1A + n_1B) + (double) n_1B * quality) / (double) (n_1A + n_1B);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
462 mean__1B = mean__1A - quality;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
463 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
464 if (zone2_ok) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
465 mean__2A = (mean_2 * (double) (n_2A + n_2B) + (double) n_2B * quality) / (double) (n_2A + n_2B);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
466 mean__2B = mean__2A - quality;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
467 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
468 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
469 else {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
470 if (zone1_ok) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
471 mean__1A = (mean_1 * (double) (n_1A + n_1B) - (double) n_1B * quality) / (double) (n_1A + n_1B);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
472 mean__1B = mean__1A + quality;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
473 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
474 if (zone2_ok) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
475 mean__2A = (mean_2 * (double) (n_2A + n_2B) - (double) n_2B * quality) / (double) (n_2A + n_2B);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
476 mean__2B = mean__2A + quality;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
477 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
478 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
479
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
480 // calculate luminance variations
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
481 if (zone1_ok) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
482 var_1A = rint(mean__1A - mean_1A);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
483 var_1B = rint(mean__1B - mean_1B);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
484 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
485 else var_1A = var_1B = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
486
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
487 if (zone2_ok) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
488 var_2A = rint(mean__2A - mean_2A);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
489 var_2B = rint(mean__2B - mean_2B);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
490 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
491 else var_2A = var_2B = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
492
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
493 if (verbose > 2) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
494 if (zone1_ok)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
495 fprintf(stderr, "m*_1A = %lf, m*_1B = %lf\n", mean__1A, mean__1B);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
496 if (zone2_ok)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
497 fprintf(stderr, "m*_2A = %lf, m*_2B = %lf\n", mean__2A, mean__2B);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
498 fprintf(stderr, "var %d %d %d %d\n", var_1A, var_1B, var_2A, var_2B);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
499 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
500
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
501 // apply luminance variations to image pixels
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
502 for (i = 0; i < blocksize; i++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
503 for (j = 0; j < blocksize; j++) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
504 int pixel = image[yb * blocksize + j][xb * blocksize + i];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
505 int pixel_zone = zone[j][i];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
506 int pixel_category = CATEGORY_VOID;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
507 if (pixel_zone == ZONE_1)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
508 pixel_category = category1[j][i];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
509 else if (pixel_zone == ZONE_2)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
510 pixel_category = category2[j][i];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
511
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
512 switch (pixel_zone | pixel_category) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
513 case CLASSIFICATION_1A:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
514 pixel = GRAYRANGE(pixel + var_1A);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
515 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
516 case CLASSIFICATION_1B:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
517 pixel = GRAYRANGE(pixel + var_1B);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
518 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
519 case CLASSIFICATION_2A:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
520 pixel = GRAYRANGE(pixel + var_2A);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
521 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
522 case CLASSIFICATION_2B:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
523 pixel = GRAYRANGE(pixel + var_2B);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
524 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
525 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
526 image[yb * blocksize + j][xb * blocksize + i] = pixel;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
527 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
528
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
529 n++;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
530 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
531
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
532 free_grays(category2);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
533 free_grays(category1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
534 free_grays(zone);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
535 free_grays(block);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
536
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
537 // write output image dimensions to output file
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
538 pgm_writepgminit(out, cols, rows, maxval, 0);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
539
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
540 // write output image
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
541 for (row = 0; row < rows; row++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
542 pgm_writepgmrow(out, image[row], cols, maxval, 0);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
543
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
544 fclose(out);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
545
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
546 pgm_freearray(image, rows);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
547
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
548 exit(0);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
549 }

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