annotate Meerwald/wang_common.c @ 23:71dd4b96221b

fix some compiler warning
author Peter Meerwald-Stadler <pmeerw@pmeerw.net>
date Fri, 20 Dec 2024 12:53:41 +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 "dwt_util.h"
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
2 #include "wang_common.h"
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
3
20
bd669312f068 suppress warnings, fix link errors
Peter Meerwald-Stadler <pmeerw@pmeerw.net>
parents: 8
diff changeset
4 Subband_data *subbands;
bd669312f068 suppress warnings, fix link errors
Peter Meerwald-Stadler <pmeerw@pmeerw.net>
parents: 8
diff changeset
5 int n_subbands;
bd669312f068 suppress warnings, fix link errors
Peter Meerwald-Stadler <pmeerw@pmeerw.net>
parents: 8
diff changeset
6
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
7 void init_subbands(Image_tree tree) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
8 int levels = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
9 int i;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
10 Image_tree p = tree;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
11
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
12 // determine # of detail subbands
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
13 while (p->coarse != NULL) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
14 levels++;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
15 p = p->coarse;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
16 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
17
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
18 // there are 3 detail subbands per level
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
19 n_subbands = 3 * levels;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
20
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
21 // allocate memory for subband data
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
22 subbands = malloc(n_subbands * sizeof(Subband_data));
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
23
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
24 p = tree;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
25 i = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
26 while (p->coarse != NULL) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
27 subbands[i++] = alloc_subband(HORIZONTAL, p->horizontal);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
28 subbands[i++] = alloc_subband(VERTICAL, p->vertical);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
29 subbands[i++] = alloc_subband(DIAGONAL, p->diagonal);
20
bd669312f068 suppress warnings, fix link errors
Peter Meerwald-Stadler <pmeerw@pmeerw.net>
parents: 8
diff changeset
30
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
31 p = p->coarse;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
32 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
33
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
34 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
35
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
36 Subband_data alloc_subband(int type, Image_tree tree) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
37 int i;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
38 Subband_data p = malloc(sizeof(struct Subband_data_struct));
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
39
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
40 p->T = 0.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
41 p->beta = 0.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
42 p->Cmax = 0.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
43 p->tree = tree;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
44 p->level = tree->level;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
45 p->width = tree->image->width;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
46 p->height = tree->image->height;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
47 p->size = p->height * p->width;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
48 p->image = tree->image;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
49 p->type = type;
20
bd669312f068 suppress warnings, fix link errors
Peter Meerwald-Stadler <pmeerw@pmeerw.net>
parents: 8
diff changeset
50
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
51 p->selected = malloc(p->height * sizeof(char *));
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
52 p->selected[0] = calloc(p->size, sizeof(char));
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
53 for (i = 1; i < p->height; i++)
20
bd669312f068 suppress warnings, fix link errors
Peter Meerwald-Stadler <pmeerw@pmeerw.net>
parents: 8
diff changeset
54 p->selected[i] = &(p->selected[0][i * p->width]);
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
55
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
56 return p;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
57 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
58
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
59 void set_subband_beta(Subband_data subband, double beta) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
60 subband->beta = beta;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
61 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
62
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
63 void set_subbands_beta(double beta) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
64 int i;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
65
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
66 for (i = 0; i < n_subbands; i++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
67 set_subband_beta(subbands[i], beta);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
68 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
69
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
70 void set_subbands_type_beta(int type, double beta) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
71 int i;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
72
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
73 for (i = 0; i < n_subbands; i++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
74 if (subbands[i]->type == type)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
75 set_subband_beta(subbands[i], beta);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
76 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
77
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
78 void calc_subband_threshold(Subband_data subband) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
79 double max;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
80 int i, j;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
81
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
82 max = fabs(get_pixel(subband->image, 0, 0));
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
83 for (i = 0; i < subband->height; i++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
84 for (j = 0; j < subband->width; j++) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
85 Pixel p = fabs(get_pixel(subband->image, i, j));
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
86 if (p > max)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
87 max = p;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
88 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
89
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
90 subband->Cmax = max;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
91 subband->T = max / 2.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
92 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
93
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
94 void calc_subbands_threshold() {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
95 int i;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
96
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
97 for (i = 0; i < n_subbands; i++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
98 calc_subband_threshold(subbands[i]);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
99 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
100
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
101 Subband_data select_subband() {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
102 int max = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
103 int i;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
104
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
105 for (i = 0; i < n_subbands; i++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
106 if ((subbands[i]->beta * subbands[i]->T) > (subbands[max]->beta * subbands[max]->T))
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
107 max = i;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
108
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
109 return subbands[max];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
110 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
111
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
112 int subband_coeff_isselected(Subband_data subband, int coeff) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
113 return subband->selected[0][coeff];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
114 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
115
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
116 Pixel get_subband_coeff(Subband_data subband, int coeff) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
117 return subband->image->data[coeff];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
118 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
119
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
120 void set_subband_coeff(Subband_data subband, int coeff, Pixel data) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
121 subband->image->data[coeff] = data;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
122 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
123
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
124 int select_subband_coeff_from(Subband_data subband, int from) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
125 int i;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
126
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
127 for (i = from; i < subband->size; i++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
128 if (!subband_coeff_isselected(subband, i) &&
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
129 get_subband_coeff(subband, i) > subband->T)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
130 return i;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
131
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
132 return -1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
133 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
134
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
135 int select_subband_coeff(Subband_data subband) {
8
f83ef905a63d fixing many warnings
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
136 return select_subband_coeff_from(subband, 0);
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
137 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
138
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
139 void mark_subband_coeff(Subband_data subband, int coeff) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
140 subband->selected[0][coeff] = 1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
141 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
142
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
143 void free_subband(Subband_data subband) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
144 free(subband->selected[0]);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
145 free(subband->selected);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
146 free(subband);
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 void free_subbands() {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
150 int i;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
151
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
152 for (i = 0; i < n_subbands; i++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
153 free_subband(subbands[i]);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
154
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
155 free(subbands);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
156 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
157
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
158 #define LARGE DBL_MAX
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
159
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
160 Pixel figure_orig_coeff(double T, double alpha, double beta, Pixel coeff) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
161 int p, p_min = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
162 double dist_min = LARGE;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
163 double sign = (coeff >= 0) ? 1.0 : -1.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
164
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
165 for (p = 1; p < 1.0 / (2.0 * alpha); p++) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
166 double dist, delta;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
167
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
168 delta = (1.0 + 2.0 * p * alpha) * T;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
169 dist = fabs(delta - fabs(coeff));
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
170
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
171 if (dist < dist_min) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
172 dist_min = dist;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
173 p_min = p;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
174 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
175 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
176
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
177 if (!p_min) p_min = 1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
178 return sign * (1.0 + 2.0 * p_min * alpha) * T;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
179 }

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