-
Notifications
You must be signed in to change notification settings - Fork 3
/
decode.c
241 lines (232 loc) · 6.65 KB
/
decode.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
Decoder for lossless image compression based on the discrete wavelet transformation
Copyright 2021 Ahmet Inan <[email protected]>
*/
#include "hilbert.h"
#include "cdf53.h"
#include "utils.h"
#include "pnm.h"
#include "rle.h"
#include "vli.h"
#include "bits.h"
#include "bytes.h"
void transformation(int *out, int *in, int N0, int W, int H, int SO, int SI, int SW, int CH)
{
int W2 = (W + 1) / 2, H2 = (H + 1) / 2;
if (W2 >= N0 && H2 >= N0)
transformation(out, in, N0, W2, H2, SO, SI, SW, CH);
icdf53(out, in, H, SO * SW, SI * SW, W * CH);
for (int j = 0; j < H; ++j)
for (int i = 0; i < W * CH; ++i)
in[(SW * j + i) * SI] = out[(SW * j + i) * SO];
for (int j = 0; j < H; ++j) {
icdf53(out + SO * SW * j, in + SI * SW * j, W, SO * CH, SI * CH, CH);
for (int i = 0; i < W * CH; ++i)
in[(SW * j + i) * SI] = out[(SW * j + i) * SO];
}
}
void reconstruction(int *output, int *input, int *missing, int *widths, int *heights, int *lengths, int levels, int channels)
{
int width = widths[levels];
int height = heights[levels];
int total = width * height;
for (int y = 0; y < heights[0]; ++y) {
for (int x = 0; x < widths[0]; ++x) {
for (int chan = 0; chan < channels; ++chan) {
int v = input[chan * total];
output[channels * (width * y + x) + chan] = v;
}
++input;
}
}
for (int l = 0; l < levels; ++l) {
for (int i = 0; i < lengths[l + 1] * lengths[l + 1]; ++i) {
struct position pos = hilbert(lengths[l + 1], i);
if ((pos.x >= widths[l] || pos.y >= heights[l]) && pos.x < widths[l + 1] && pos.y < heights[l + 1]) {
for (int chan = 0; chan < channels; ++chan) {
int v = input[chan * total];
int m = missing[chan * levels + l] - 2;
if (m >= 0) {
int bias = 1 << m;
if (v < 0)
v -= bias;
else if (v > 0)
v += bias;
}
output[channels * (width * pos.y + pos.x) + chan] = v;
}
++input;
}
}
}
}
int decode_plane(struct rle_reader *rle, int *val, int num, int plane)
{
int int_bits = sizeof(int) * 8;
int sgn_pos = int_bits - 1;
int sig_pos = int_bits - 2;
int ref_pos = int_bits - 3;
int sig_mask = 1 << sig_pos;
int ref_mask = 1 << ref_pos;
for (int i = 0; i < num; ++i) {
if (!(val[i] & ref_mask)) {
int bit = get_rle(rle);
if (bit < 0)
return bit;
val[i] |= bit << plane;
if (bit) {
int sgn = rle_get_bit(rle);
if (sgn < 0)
return sgn;
val[i] |= (sgn << sgn_pos) | sig_mask;
}
}
}
for (int i = 0; i < num; ++i) {
if (val[i] & ref_mask) {
int bit = rle_get_bit(rle);
if (bit < 0)
return bit;
val[i] |= bit << plane;
} else if (val[i] & sig_mask) {
val[i] ^= sig_mask | ref_mask;
}
}
return 0;
}
void process(int *val, int num)
{
int int_bits = sizeof(int) * 8;
int sgn_pos = int_bits - 1;
int sig_pos = int_bits - 2;
int ref_pos = int_bits - 3;
int sgn_mask = 1 << sgn_pos;
int sig_mask = 1 << sig_pos;
int ref_mask = 1 << ref_pos;
for (int i = 0; i < num; ++i) {
val[i] &= ~(sig_mask | ref_mask);
if (val[i] & sgn_mask)
val[i] = -(val[i] ^ sgn_mask);
}
}
int decode_root(struct vli_reader *vli, int *val, int num)
{
int cnt = get_vli(vli);
if (cnt < 0)
return cnt;
for (int i = 0; cnt && i < num; ++i) {
int ret = vli_read_bits(vli, val + i, cnt);
if (ret)
return ret;
if (val[i] && (ret = vli_get_bit(vli)))
val[i] = -val[i];
if (ret < 0)
return ret;
}
return 0;
}
int main(int argc, char **argv)
{
if (argc != 3) {
fprintf(stderr, "usage: %s input.dwt output.pnm\n", argv[0]);
return 1;
}
struct bytes_reader *bytes = bytes_reader(argv[1]);
if (!bytes)
return 1;
int letter = get_byte(bytes);
if (letter != 'W')
return 1;
int number = get_byte(bytes);
if (number != '5' && number != '6')
return 1;
int color = number == '6';
int width, height;
if (read_bytes(bytes, &width, 2) || read_bytes(bytes, &height, 2))
return 1;
++width;
++height;
int min_len = 8;
if (width < min_len || height < min_len)
return 1;
struct bits_reader *bits = bits_reader(bytes);
struct vli_reader *vli = vli_reader(bits);
int lengths[16], pixels[16], widths[16], heights[16];
int levels = compute_lengths(lengths, pixels, widths, heights, width, height, min_len);
int total = width * height;
int channels = color ? 3 : 1;
int *buffer = malloc(sizeof(int) * channels * total);
for (int i = 0; i < channels * total; ++i)
buffer[i] = 0;
for (int chan = 0; chan < channels; ++chan)
if (decode_root(vli, buffer + chan * total, pixels[0]))
return 1;
int planes[channels];
for (int chan = 0; chan < channels; ++chan)
if ((planes[chan] = get_vli(vli)) < 0)
return 1;
int planes_max = 0;
for (int chan = 0; chan < channels; ++chan)
if (planes_max < planes[chan])
planes_max = planes[chan];
int maximum = levels > planes_max ? levels : planes_max;
int layers_max = 2 * maximum - 1;
int missing[channels * levels];
for (int chan = 0; chan < channels; ++chan)
for (int i = 0; i < levels; ++i)
missing[chan * levels + i] = planes[chan];
struct rle_reader *rle = rle_reader(vli);
if (planes_max == planes[0]) {
int num = pixels[1] - pixels[0];
if (decode_plane(rle, buffer + pixels[0], num, planes[0] - 1))
goto end;
--missing[0];
}
for (int layers = 0; layers < layers_max; ++layers) {
for (int l = 0, *buf = buffer + pixels[0],
num = pixels[l + 1] - pixels[l];
l < levels && l <= layers + 1; buf += num, ++l,
num = pixels[l + 1] - pixels[l]) {
for (int chan = 0; chan < 1; ++chan) {
int plane = planes_max - 1 - (layers + 1 - l);
if (plane < 0 || plane >= planes[chan])
continue;
if (decode_plane(rle, buf + chan * total, num, plane))
goto end;
--missing[chan * levels + l];
}
}
for (int l = 0, *buf = buffer + pixels[0],
num = pixels[l + 1] - pixels[l];
l < levels && l <= layers; buf += num, ++l,
num = pixels[l + 1] - pixels[l]) {
for (int chan = 1; chan < channels; ++chan) {
int plane = planes_max - 1 - (layers - l);
if (plane < 0 || plane >= planes[chan])
continue;
if (decode_plane(rle, buf + chan * total, num, plane))
goto end;
--missing[chan * levels + l];
}
}
}
end:
delete_rle_reader(rle);
delete_vli_reader(vli);
close_bits_reader(bits);
close_bytes_reader(bytes);
for (int chan = 0; chan < channels; ++chan)
process(buffer + chan * total + pixels[0], total - pixels[0]);
struct image *image = new_image(width, height, channels);
int *temp = malloc(sizeof(int) * channels * total);
reconstruction(temp, buffer, missing, widths, heights, lengths, levels, channels);
transformation(image->buffer, temp, min_len, width, height, 1, 1, width * channels, channels);
free(buffer);
free(temp);
if (color)
rgb_from_ycocg(image);
if (!write_pnm(argv[2], image))
return 1;
delete_image(image);
return 0;
}