annotate Meerwald/wang_common.c @ 15:ad1d224896c5

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

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