Mercurial > hg > wm
comparison Meerwald/wm_xia_d.c @ 0:be303a3f5ea8
import
| author | Peter Meerwald <pmeerw@cosy.sbg.ac.at> | 
|---|---|
| date | Sun, 12 Aug 2007 13:14:34 +0200 | 
| parents | |
| children | acb6967ee76d | 
   comparison
  equal
  deleted
  inserted
  replaced
| -1:000000000000 | 0:be303a3f5ea8 | 
|---|---|
| 1 #include "wm.h" | |
| 2 #include "dwt.h" | |
| 3 #include "pgm.h" | |
| 4 #include "dwt_util.h" | |
| 5 | |
| 6 char *progname; | |
| 7 | |
| 8 void usage(void) { | |
| 9 fprintf(stderr, "usage: %s [-a n] [-e n] [-f n] [-F file] [-h] [-l n] [-o file] [-v n] -s file -i file file\n\n", progname); | |
| 10 fprintf(stderr, "\t-a n\t\talpha factor\n"); | |
| 11 fprintf(stderr, "\t-e n\t\twavelet filtering method\n"); | |
| 12 fprintf(stderr, "\t-f n\t\tfilter number\n"); | |
| 13 fprintf(stderr, "\t-F file\t\tfilter definition file\n"); | |
| 14 fprintf(stderr, "\t-h\t\tprint usage\n"); | |
| 15 fprintf(stderr, "\t-i file\t\toriginal image file\n"); | |
| 16 fprintf(stderr, "\t-l n\t\tdecomposition level\n"); | |
| 17 fprintf(stderr, "\t-o file\t\tfile for extracted watermark\n"); | |
| 18 fprintf(stderr, "\t-s file\t\toriginal signature file\n"); | |
| 19 fprintf(stderr, "\t-v n\t\tverbosity level\n"); | |
| 20 exit(0); | |
| 21 } | |
| 22 | |
| 23 int extract_subband(Image_tree s, Image_tree t, int name, double alpha, double watermark[], int n, int verbose) { | |
| 24 int i, j; | |
| 25 int w; | |
| 26 | |
| 27 w = 0; | |
| 28 for (i = 5; i < s->image->height-5; i++) | |
| 29 for (j = 5; j < s->image->width-5; j++) { | |
| 30 double input_coeff, orig_coeff; | |
| 31 | |
| 32 input_coeff = get_pixel(s->image, i, j); | |
| 33 orig_coeff = get_pixel(t->image, i, j); | |
| 34 if (fabs(orig_coeff) > 20.0) | |
| 35 watermark[w++] = (input_coeff - orig_coeff) / (alpha * pow(fabs(orig_coeff), 1.0 / 1.2)); | |
| 36 } | |
| 37 | |
| 38 if (verbose > 3) | |
| 39 fprintf(stderr, "%s: extracting %s%d, size %d x %d; extracted %d coeffs.\n", | |
| 40 progname, subband_name(name), s->level, s->image->width, s->image->height, w); | |
| 41 | |
| 42 return w; | |
| 43 } | |
| 44 | |
| 45 void write_mark(FILE *out, double watermark[], int n) { | |
| 46 int i; | |
| 47 | |
| 48 fprintf(out, "%d\n", n); | |
| 49 for (i = 0; i < n; i++) | |
| 50 fprintf(out, "%f\n", watermark[i]); | |
| 51 } | |
| 52 | |
| 53 int main(int argc, char *argv[]) { | |
| 54 | |
| 55 FILE *in = stdin; | |
| 56 FILE *out = stdout; | |
| 57 FILE *orig = NULL; | |
| 58 FILE *sig = NULL; | |
| 59 | |
| 60 gray **input_image; | |
| 61 gray **orig_image; | |
| 62 | |
| 63 char signature_name[MAXPATHLEN]; | |
| 64 char output_name[MAXPATHLEN] = "(stdout)"; | |
| 65 char input_name[MAXPATHLEN] = "(stdin)"; | |
| 66 char orig_name[MAXPATHLEN]; | |
| 67 | |
| 68 int c, w; | |
| 69 int i; | |
| 70 int n = 0; | |
| 71 int method = -1; | |
| 72 int filter = 0; | |
| 73 char filter_name[MAXPATHLEN] = ""; | |
| 74 | |
| 75 int level = 0, levels; | |
| 76 double alpha = 0.0; | |
| 77 | |
| 78 int in_rows, in_cols, in_format; | |
| 79 gray in_maxval; | |
| 80 int orig_rows, orig_cols, orig_format; | |
| 81 gray orig_maxval; | |
| 82 int rows, cols; | |
| 83 int row, col; | |
| 84 | |
| 85 double *watermark; | |
| 86 | |
| 87 Image_tree input_dwts, orig_dwts, p, q; | |
| 88 | |
| 89 int verbose = 0; | |
| 90 | |
| 91 progname = argv[0]; | |
| 92 | |
| 93 pgm_init(&argc, argv); wm_init2(); | |
| 94 | |
| 95 while ((c = getopt(argc, argv, "a:e:f:F:h?i:l:o:s:v:")) != EOF) { | |
| 96 switch (c) { | |
| 97 case 'a': | |
| 98 alpha = atof(optarg); | |
| 99 if (alpha <= 0.0) { | |
| 100 fprintf(stderr, "%s: alpha factor %f out of range\n", progname, alpha); | |
| 101 exit(1); | |
| 102 } | |
| 103 break; | |
| 104 case 'e': | |
| 105 method = atoi(optarg); | |
| 106 if (method < 0) { | |
| 107 fprintf(stderr, "%s: wavelet filtering method %d out of range\n", progname, method); | |
| 108 exit(1); | |
| 109 } | |
| 110 break; | |
| 111 case 'f': | |
| 112 filter = atoi(optarg); | |
| 113 if (filter <= 0) { | |
| 114 fprintf(stderr, "%s: filter number %d out of range\n", progname, filter); | |
| 115 exit(1); | |
| 116 } | |
| 117 break; | |
| 118 case 'F': | |
| 119 strcpy(filter_name, optarg); | |
| 120 break; | |
| 121 case 'h': | |
| 122 case '?': | |
| 123 usage(); | |
| 124 break; | |
| 125 case 'i': | |
| 126 if ((orig = fopen(optarg, "rb")) == NULL) { | |
| 127 fprintf(stderr, "%s: unable to open original image file %s\n", progname, optarg); | |
| 128 exit(1); | |
| 129 } | |
| 130 strcpy(orig_name, optarg); | |
| 131 break; | |
| 132 case 'l': | |
| 133 level = atoi(optarg); | |
| 134 if (level <= 0) { | |
| 135 fprintf(stderr, "%s: decomposition level %d out of range\n", progname, level); | |
| 136 exit(1); | |
| 137 } | |
| 138 break; | |
| 139 case 'o': | |
| 140 if ((out = fopen(optarg, "w")) == NULL) { | |
| 141 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg); | |
| 142 exit(1); | |
| 143 } | |
| 144 strcpy(output_name, optarg); | |
| 145 break; | |
| 146 case 's': | |
| 147 if ((sig = fopen(optarg, "r")) == NULL) { | |
| 148 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg); | |
| 149 exit(1); | |
| 150 } | |
| 151 strcpy(signature_name, optarg); | |
| 152 break; | |
| 153 case 'v': | |
| 154 verbose = atoi(optarg); | |
| 155 if (verbose < 0) { | |
| 156 fprintf(stderr, "%s: verbosity level %d out of range\n", progname, verbose); | |
| 157 exit(1); | |
| 158 } | |
| 159 break; | |
| 160 } | |
| 161 } | |
| 162 | |
| 163 argc -= optind; | |
| 164 argv += optind; | |
| 165 | |
| 166 if (argc > 1) { | |
| 167 usage(); | |
| 168 exit(1); | |
| 169 } | |
| 170 | |
| 171 if (argc == 1 && *argv[0] != '-') | |
| 172 if ((in = fopen(argv[0], "rb")) == NULL) { | |
| 173 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]); | |
| 174 exit(1); | |
| 175 } | |
| 176 else | |
| 177 strcpy(input_name, argv[0]); | |
| 178 | |
| 179 if (!orig) { | |
| 180 fprintf(stderr, "%s: original image file not specified, use -i file option\n", progname); | |
| 181 exit(1); | |
| 182 } | |
| 183 | |
| 184 if (sig) { | |
| 185 char line[32]; | |
| 186 fgets(line, sizeof(line), sig); | |
| 187 if (strspn(line, "XASG") >= 4) { | |
| 188 fscanf(sig, "%d\n", &n); | |
| 189 if (alpha == 0.0) | |
| 190 fscanf(sig, "%lf\n", &alpha); | |
| 191 else | |
| 192 fscanf(sig, "%*lf\n"); | |
| 193 if (level == 0) | |
| 194 fscanf(sig, "%d\n", &level); | |
| 195 else | |
| 196 fscanf(sig, "%*d\n"); | |
| 197 if (method < 0) | |
| 198 fscanf(sig, "%d\n", &method); | |
| 199 else | |
| 200 fscanf(sig, "%*d\n"); | |
| 201 if (filter == 0) | |
| 202 fscanf(sig, "%d\n", &filter); | |
| 203 else | |
| 204 fscanf(sig, "%*d\n"); | |
| 205 if (!strcmp(filter_name, "")) | |
| 206 fscanf(sig, "%[^\n\r]\n", &filter_name); | |
| 207 else | |
| 208 fscanf(sig, "%*[^\n\r]\n"); | |
| 209 } | |
| 210 else { | |
| 211 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name); | |
| 212 exit(1); | |
| 213 } | |
| 214 fclose(sig); | |
| 215 } | |
| 216 else { | |
| 217 fprintf(stderr, "%s: signature file not specified, use -s file option\n", progname); | |
| 218 exit(1); | |
| 219 } | |
| 220 | |
| 221 pgm_readpgminit(in, &in_cols, &in_rows, &in_maxval, &in_format); | |
| 222 pgm_readpgminit(orig, &orig_cols, &orig_rows, &orig_maxval, &orig_format); | |
| 223 | |
| 224 if (in_cols != orig_cols || in_rows != orig_rows) { | |
| 225 fprintf(stderr, "%s: input image %s does not match dimensions of original image %s\n", progname, input_name, orig_name); | |
| 226 exit(1); | |
| 227 } | |
| 228 | |
| 229 cols = in_cols; | |
| 230 rows = in_rows; | |
| 231 | |
| 232 input_image = pgm_allocarray(in_cols, in_rows); | |
| 233 orig_image = pgm_allocarray(orig_cols, orig_rows); | |
| 234 | |
| 235 for (row = 0; row < in_rows; row++) { | |
| 236 pgm_readpgmrow(in, input_image[row], in_cols, in_maxval, in_format); | |
| 237 pgm_readpgmrow(orig, orig_image[row], orig_cols, orig_maxval, orig_format); | |
| 238 } | |
| 239 | |
| 240 fclose(in); | |
| 241 fclose(orig); | |
| 242 | |
| 243 // complete decomposition | |
| 244 levels = find_deepest_level(cols, rows) - 1; | |
| 245 if (level > levels) { | |
| 246 fprintf(stderr, "%s: decomposition level %d not possible (max. %d), image size is %d x %d\n", progname, level, levels, cols, rows); | |
| 247 exit(1); | |
| 248 } | |
| 249 | |
| 250 init_dwt(cols, rows, filter_name, filter, level, method); | |
| 251 #ifdef POLLEN_STUFF | |
| 252 #include "pollen_stuff.xxx" | |
| 253 #endif | |
| 254 #ifdef PARAM_STUFF | |
| 255 #include "param_stuff.xxx" | |
| 256 #endif | |
| 257 | |
| 258 input_dwts = fdwt(input_image); | |
| 259 orig_dwts = fdwt(orig_image); | |
| 260 | |
| 261 watermark = malloc((rows * cols / 4.0) * sizeof(double)); | |
| 262 if (!watermark) { | |
| 263 fprintf(stderr, "%s: malloc() failed\n\n", progname); | |
| 264 exit(1); | |
| 265 } | |
| 266 | |
| 267 fprintf(out, "XAWM\n"); | |
| 268 fprintf(out, "%d\n", n); | |
| 269 fprintf(out, "%d\n", level * 3); | |
| 270 | |
| 271 p = input_dwts; | |
| 272 q = orig_dwts; | |
| 273 while (p->coarse && q->coarse) { | |
| 274 int size; | |
| 275 | |
| 276 size = extract_subband(p->horizontal, q->horizontal, HORIZONTAL, alpha, watermark, n, verbose); | |
| 277 write_mark(out, watermark, size); | |
| 278 size = extract_subband(p->vertical, q->vertical, VERTICAL, alpha, watermark, n, verbose); | |
| 279 write_mark(out, watermark, size); | |
| 280 size = extract_subband(p->diagonal, q->diagonal, DIAGONAL, alpha, watermark, n, verbose); | |
| 281 write_mark(out, watermark, size); | |
| 282 | |
| 283 p = p->coarse; | |
| 284 q = q->coarse; | |
| 285 } | |
| 286 | |
| 287 fclose(out); | |
| 288 | |
| 289 free(watermark); | |
| 290 | |
| 291 pgm_freearray(input_image, rows); | |
| 292 pgm_freearray(orig_image, rows); | |
| 293 | |
| 294 exit(0); | |
| 295 } | 
