Mercurial > hg > wm
comparison Meerwald-dir/gen_koch_sig.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/gen_koch_sig.c@4987db85cfae |
| children |
comparison
equal
deleted
inserted
replaced
| 23:71dd4b96221b | 24:9f20bce6184e |
|---|---|
| 1 #include "wm.h" | |
| 2 #include "signature.h" | |
| 3 | |
| 4 char *progname; | |
| 5 | |
| 6 void usage(void) { | |
| 7 fprintf(stderr, "usage: %s [-l n] [-n n] [-o file] [-q n] [-s file] [-S n] file\n\n", progname); | |
| 8 fprintf(stderr, "\t-h\t\tprint usage\n"); | |
| 9 fprintf(stderr, "\t-l n\t\tsignature strength factor (default 5.0)\n"); | |
| 10 fprintf(stderr, "\t-n n\t\twatermark bit length\n"); | |
| 11 fprintf(stderr, "\t-o file\t\toutput file\n"); | |
| 12 fprintf(stderr, "\t-q n\t\tquantization (JPEG quality) factor (default 90)\n"); | |
| 13 fprintf(stderr, "\t-s file\t\tuse signature file's embedding information\n"); | |
| 14 fprintf(stderr, "\t-S n\t\tseed\n"); | |
| 15 exit(0); | |
| 16 } | |
| 17 | |
| 18 int main(int argc, char *argv[]) { | |
| 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 i; | |
| 29 int n = 0; | |
| 30 int nb; | |
| 31 int s = 0; | |
| 32 int q = 90; | |
| 33 double l = 5.0; | |
| 34 | |
| 35 progname = argv[0]; wm_init(); | |
| 36 | |
| 37 while ((c = getopt(argc, argv, "h?l:n:o:q:s:S:")) != EOF) { | |
| 38 switch (c) { | |
| 39 case 'h': | |
| 40 case '?': | |
| 41 usage(); | |
| 42 break; | |
| 43 case 'l': | |
| 44 l = atof(optarg); | |
| 45 if (l <= 0.0) { | |
| 46 fprintf(stderr, "%s: signature strength factor %f out of range\n", progname, l); | |
| 47 exit(1); | |
| 48 } | |
| 49 break; | |
| 50 case 'n': | |
| 51 n = atoi(optarg); | |
| 52 if (n < 1 || n > 1000) { | |
| 53 fprintf(stderr, "%s: watermark length %d out of range\n", progname, n); | |
| 54 exit(1); | |
| 55 } | |
| 56 break; | |
| 57 case 'o': | |
| 58 if ((out = fopen(optarg, "wb")) == NULL) { | |
| 59 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg); | |
| 60 exit(1); | |
| 61 } | |
| 62 strcpy(output_name, optarg); | |
| 63 break; | |
| 64 case 'q': | |
| 65 q = atoi(optarg); | |
| 66 if (q <= 0 || q > 100) { | |
| 67 fprintf(stderr, "%s: quantization factor %d out of range\n", progname, q); | |
| 68 exit(1); | |
| 69 } | |
| 70 break; | |
| 71 case 's': | |
| 72 if ((sig = fopen(optarg, "r")) == NULL) { | |
| 73 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg); | |
| 74 exit(1); | |
| 75 } | |
| 76 strcpy(signature_name, optarg); | |
| 77 break; | |
| 78 case 'S': | |
| 79 s = atoi(optarg); | |
| 80 break; | |
| 81 } | |
| 82 } | |
| 83 | |
| 84 argc -= optind; | |
| 85 argv += optind; | |
| 86 | |
| 87 if (argc > 1) { | |
| 88 usage(); | |
| 89 exit(1); | |
| 90 } | |
| 91 | |
| 92 if (argc == 1 && *argv[0] != '-') { | |
| 93 if ((in = fopen(argv[0], "rb")) == NULL) { | |
| 94 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]); | |
| 95 exit(1); | |
| 96 } | |
| 97 else | |
| 98 strcpy(input_name, argv[0]); | |
| 99 } | |
| 100 | |
| 101 if (s) | |
| 102 srandom(s); | |
| 103 else | |
| 104 srandom(time(NULL) * getpid()); | |
| 105 | |
| 106 if (sig) { | |
| 107 char line[128]; | |
| 108 fgets(line, sizeof(line), sig); | |
| 109 if (strspn(line, "KCSG") >= 4) { | |
| 110 if (n == 0) | |
| 111 fscanf(sig, "%d\n", &n); | |
| 112 else | |
| 113 fscanf(sig, "%*d\n"); | |
| 114 if (l == 0.0) | |
| 115 fscanf(sig, "%lf\n", &l); | |
| 116 else | |
| 117 fscanf(sig, "%*f\n"); | |
| 118 if (q == 0) | |
| 119 fscanf(sig, "%d\n", &q); | |
| 120 else | |
| 121 fscanf(sig, "%*d\n"); | |
| 122 } | |
| 123 else { | |
| 124 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name); | |
| 125 exit(1); | |
| 126 } | |
| 127 fclose(sig); | |
| 128 } | |
| 129 | |
| 130 if (n > 0) { | |
| 131 nb = fread(signature, sizeof(char), i = NBITSTOBYTES(n), in); | |
| 132 if (nb < i) { | |
| 133 fprintf(stderr, "%s: failed to read all %d signature bits from %s\n", progname, n, input_name); | |
| 134 exit(1); | |
| 135 } | |
| 136 } | |
| 137 else { | |
| 138 if (fscanf(in, "%128[^\n\r]", signature) == EOF) { | |
| 139 fprintf(stderr, "%s: failed to read signature bits from %s\n", progname, input_name); | |
| 140 exit(1); | |
| 141 } | |
| 142 nb = strlen(signature); | |
| 143 n = NBYTESTOBITS(nb); | |
| 144 fprintf(stderr, "%s: got %d signature bits\n", progname, n); | |
| 145 } | |
| 146 | |
| 147 fprintf(out, "KCSG\n"); | |
| 148 fprintf(out, "%d\n", n); | |
| 149 fprintf(out, "%f\n", l); | |
| 150 fprintf(out, "%d\n", q); | |
| 151 fprintf(out, "%ld\n", random()); | |
| 152 fwrite(signature, sizeof(char), nb, out); | |
| 153 fprintf(out, "\n"); | |
| 154 | |
| 155 fclose(out); | |
| 156 | |
| 157 exit(0); | |
| 158 } |
