annotate Meerwald-dir/cmp_ppm.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/cmp_ppm.c@bd669312f068
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
1 #include "wm.h"
24
9f20bce6184e move directories, support netpbm 11
Peter Meerwald-Stadler <pmeerw@pmeerw.net>
parents: 20
diff changeset
2 #include "netpbm/ppm.h"
10
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
3
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
4 #define LUMINANCE 1
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
5 #define RED 2
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
6 #define GREEN 4
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
7 #define BLUE 8
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
8
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
9 char *progname;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
10
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
11 void usage(void) {
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
12 fprintf(stderr, "usage: %s -c name [-C] [-h] [-m n] [-o file] [-pP] -i file file\n\n", progname);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
13 fprintf(stderr, "\t-c name\t\tcolor component (default luminance)\n");
11
a5249f7d45f7 improve cmp_ppm usage help
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 10
diff changeset
14 fprintf(stderr, "\t\t\teg. red, green, blue, luminance\n");
10
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
15 fprintf(stderr, "\t-C\t\tprint PSNR value only\n");
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
16 fprintf(stderr, "\t-h\t\tprint usage\n");
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
17 fprintf(stderr, "\t-i file\t\toriginal image file\n");
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
18 fprintf(stderr, "\t-m n\t\tmultiplication factor (default 16)\n");
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
19 fprintf(stderr, "\t-o file\t\toutput file\n");
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
20 fprintf(stderr, "\t-p\t\tprint PSNR, RMS and MSE\n");
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
21 fprintf(stderr, "\t-P\t\tonly print PSNR, RMS and MSE, no difference image\n");
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
22 exit(0);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
23 }
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
24
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
25 int main(int argc, char *argv[]) {
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
26
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
27 FILE *in = stdin;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
28 FILE *out = stdout;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
29 FILE *orig = NULL;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
30
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
31 pixel **input_image;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
32 pixel **orig_image;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
33
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
34 char output_name[MAXPATHLEN] = "(stdout)";
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
35 char input_name[MAXPATHLEN] = "(stdin)";
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
36 char orig_name[MAXPATHLEN];
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
37
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
38 int in_cols, in_rows, in_format;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
39 pixval in_maxval;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
40 int orig_cols, orig_rows, orig_format;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
41 pixval orig_maxval;
20
bd669312f068 suppress warnings, fix link errors
Peter Meerwald-Stadler <pmeerw@pmeerw.net>
parents: 11
diff changeset
42 int cols, rows;
10
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
43 pixval maxval;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
44 int col, row;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
45
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
46 int c;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
47
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
48 double error = 0.0;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
49 int print_psnr = 0;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
50 int print_psnr_only = 0;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
51 int print_psnr_value_only = 0;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
52
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
53 int m = 16;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
54 int component = 0;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
55 int component_default = LUMINANCE;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
56 int min, max;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
57
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
58 progname = argv[0];
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
59
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
60 ppm_init(&argc, argv);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
61
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
62 #ifdef __EMX__
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
63 _fsetmode(in, "b");
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
64 _fsetmode(out, "b");
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
65 #endif
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
66
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
67 while ((c = getopt(argc, argv, "Cc:h?i:m:o:pP")) != EOF) {
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
68 switch (c) {
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
69 case 'C':
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
70 print_psnr_value_only = 1;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
71 print_psnr_only = 1;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
72 break;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
73 case 'c':
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
74 if (!strcasecmp(optarg, "RED") || toupper(*optarg) == 'R')
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
75 component |= RED;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
76 else if (!strcasecmp(optarg, "GREEN") || toupper(*optarg) == 'G')
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
77 component |= GREEN;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
78 else if (!strcasecmp(optarg, "BLUE") || toupper(*optarg) == 'B')
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
79 component |= BLUE;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
80 else if (!strcasecmp(optarg, "LUMINANCE") || toupper(*optarg) == 'L')
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
81 component |= LUMINANCE;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
82 else
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
83 fprintf(stderr, "%s: unknown color component %s\n", progname, optarg);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
84 break;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
85 case 'h':
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
86 case '?':
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
87 usage();
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
88 break;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
89 case 'i':
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
90 if ((orig = fopen(optarg, "rb")) == NULL) {
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
91 fprintf(stderr, "%s: unable to open original image file %s\n", progname, optarg);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
92 exit(1);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
93 }
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
94 strcpy(orig_name, optarg);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
95 break;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
96 case 'm':
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
97 m = atoi(optarg);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
98 if (m <= 0) {
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
99 fprintf(stderr, "%s: multiplication factor %d out of range\n", progname, m);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
100 exit(1);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
101 }
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
102 break;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
103 case 'o':
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
104 if ((out = fopen(optarg, "wb")) == NULL) {
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
105 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
106 exit(1);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
107 }
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
108 strcpy(output_name, optarg);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
109 break;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
110 case 'p':
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
111 print_psnr = 1;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
112 break;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
113 case 'P':
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
114 print_psnr_only = 1;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
115 break;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
116 }
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
117 }
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
118
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
119 argc -= optind;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
120 argv += optind;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
121
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
122 if (argc > 1) {
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
123 usage();
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
124 exit(1);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
125 }
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
126
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
127 if (argc == 1 && *argv[0] != '-') {
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
128 if ((in = fopen(argv[0], "rb")) == NULL) {
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
129 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
130 exit(1);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
131 }
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
132 else
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
133 strcpy(input_name, argv[0]);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
134 }
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
135
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
136 if (!orig) {
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
137 fprintf(stderr, "%s: original image file not specified, use -i file option\n", progname);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
138 exit(1);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
139 }
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
140
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
141 if (!component) {
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
142 if (component_default)
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
143 component = component_default;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
144 else {
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
145 fprintf(stderr, "%s: color component(s) to compare not specified, use -c name option\n", progname);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
146 exit(1);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
147 }
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
148 }
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
149
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
150 if (component & LUMINANCE && component & (RED | GREEN | BLUE)) {
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
151 fprintf(stderr, "%s: unable to compare luminance AND color component\n", progname);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
152 exit(1);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
153 }
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
154
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
155 ppm_readppminit(in, &in_cols, &in_rows, &in_maxval, &in_format);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
156
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
157 ppm_readppminit(orig, &orig_cols, &orig_rows, &orig_maxval, &orig_format);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
158
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
159 if (in_cols != orig_cols || in_rows != orig_rows) {
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
160 fprintf(stderr, "%s: input image %s does not match dimensions of original image %s\n", progname, input_name, orig_name);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
161 exit(1);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
162 }
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
163
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
164 cols = in_cols;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
165 rows = in_rows;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
166 maxval = in_maxval;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
167
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
168 input_image = ppm_allocarray(cols, rows);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
169
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
170 for (row = 0; row < rows; row++)
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
171 ppm_readppmrow(in, input_image[row], cols, in_maxval, in_format);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
172
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
173 fclose(in);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
174
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
175 orig_image = ppm_allocarray(cols, rows);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
176
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
177 for (row = 0; row < rows; row++)
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
178 ppm_readppmrow(orig, orig_image[row], cols, orig_maxval, orig_format);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
179
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
180 fclose(orig);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
181
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
182 if (component & LUMINANCE)
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
183 min = max = abs(PPM_LUMIN(input_image[0][0]) - PPM_LUMIN(orig_image[0][0]));
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
184 else {
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
185 if (component & RED)
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
186 min = max = abs(PPM_GETR(input_image[0][0]) - PPM_GETR(orig_image[0][0]));
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
187 else if (component & GREEN)
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
188 min = max = abs(PPM_GETG(input_image[0][0]) - PPM_GETG(orig_image[0][0]));
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
189 else if (component & BLUE)
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
190 min = max = abs(PPM_GETB(input_image[0][0]) - PPM_GETB(orig_image[0][0]));
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
191 else
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
192 min = max = 0;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
193 }
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
194
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
195 for (row = 0; row < rows; row++) {
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
196 pixel *pi = input_image[row];
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
197 pixel *po = orig_image[row];
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
198
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
199 for (col = 0; col < cols; col++) {
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
200 int diff=0;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
201
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
202 if (component & LUMINANCE) {
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
203 pixval l;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
204 diff = abs(PPM_LUMIN(*pi) - PPM_LUMIN(*po));
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
205 error += sqr(PPM_LUMIN(*pi) - PPM_LUMIN(*po));
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
206 l = PIXELRANGE(ROUND(abs(PPM_LUMIN(*pi) - PPM_LUMIN(*po)) * m));
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
207 PPM_ASSIGN(*pi, l, l, l);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
208 }
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
209 else {
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
210 if (component & RED) {
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
211 diff = abs(PPM_GETR(*pi) - PPM_GETR(*po));
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
212 error += sqr(PPM_GETR(*pi) - PPM_GETR(*po));
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
213 PPM_PUTR(*pi, PIXELRANGE(abs(PPM_GETR(*pi) - PPM_GETR(*po)) * m));
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
214 }
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
215 else
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
216 PPM_PUTR(*pi, 0);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
217 if (component & GREEN) {
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
218 diff = abs(PPM_GETG(*pi) - PPM_GETG(*po));
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
219 error += sqr(PPM_GETG(*pi) - PPM_GETG(*po));
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
220 PPM_PUTG(*pi, PIXELRANGE(abs(PPM_GETG(*pi) - PPM_GETG(*po)) * m));
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
221 }
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
222 else
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
223 PPM_PUTG(*pi, 0);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
224 if (component & BLUE) {
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
225 diff = abs(PPM_GETB(*pi) - PPM_GETB(*po));
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
226 error += sqr(PPM_GETB(*pi) - PPM_GETB(*po));
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
227 PPM_PUTB(*pi, PIXELRANGE(abs(PPM_GETB(*pi) - PPM_GETB(*po)) * m));
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
228 }
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
229 else
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
230 PPM_PUTB(*pi, 0);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
231 }
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
232
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
233 if (diff < min) min = diff;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
234 if (diff > max) max = diff;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
235 pi++;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
236 po++;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
237 }
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
238 }
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
239
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
240 if (!print_psnr_only) {
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
241 ppm_writeppminit(out, cols, rows, maxval, 0);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
242 for (row = 0; row < rows; row++)
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
243 ppm_writeppmrow(out, input_image[row], cols, maxval, 0);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
244
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
245 fclose(out);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
246 }
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
247
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
248 ppm_freearray(input_image, rows);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
249 ppm_freearray(orig_image, rows);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
250
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
251 if (print_psnr || print_psnr_only) {
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
252 double mse = error / (double) (cols * rows);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
253 double rmse = sqrt(mse);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
254 double psnr = 20.0 * log(255.0 / rmse) / log(10.0);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
255 FILE *print = print_psnr_only ? out : stderr;
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
256 if (!print_psnr_value_only) {
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
257 if (mse > 0.0)
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
258 fprintf(print, "PSNR: %lf dB\n", psnr);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
259 else
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
260 fprintf(print, "PSNR: inf\n");
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
261 fprintf(print, "RMS: %lf\n", rmse);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
262 fprintf(print, "MSE: %lf\n", mse);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
263 fprintf(print, "dmin, dmax: %d, %d\n", min, max);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
264 }
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
265 else
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
266 fprintf(print, "%lf\n", mse > 0.0 ? psnr : 100.0);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
267 }
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
268
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
269 exit(0);
aa7b4c3a7313 add cmp_ppm
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
270 }

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