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