-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecode.c
130 lines (121 loc) · 2.95 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
/*
Decoder for lossless image compression
Copyright 2024 Ahmet Inan <[email protected]>
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
FILE *open_img(const char *name, int *width, int *height, int *channels)
{
if (name[0] == '-' && !name[1])
name = "/dev/stdin";
FILE *file = fopen(name, "r");
if (!file) {
fprintf(stderr, "could not open \"%s\" file for reading\n", name);
return 0;
}
char magic[3];
uint16_t dimensions[2];
if (3 != fread(magic, 1, 3, file) || EOF == (*channels = fgetc(file)) || 2 != fread(dimensions, 2, 2, file)) {
fprintf(stderr, "EOF while reading from \"%s\"\n", name);
fclose(file);
return 0;
}
if (*channels != 1 && *channels != 3) {
fprintf(stderr, "only one or three channels supported\n");
fclose(file);
return 0;
}
*width = dimensions[0] + 1;
*height = dimensions[1] + 1;
return file;
}
FILE *open_pnm(const char *name, int width, int height, int channels)
{
if (name[0] == '-' && !name[1])
name = "/dev/stdout";
FILE *file = fopen(name, "w");
if (!file) {
fprintf(stderr, "could not open \"%s\" file for writing\n", name);
return 0;
}
int number = channels == 1 ? 5 : 6;
if (!fprintf(file, "P%d %d %d 255\n", number, width, height)) {
fprintf(stderr, "could not write to file \"%s\"\n", name);
fclose(file);
}
return file;
}
long leb128(FILE *file)
{
long byte, shift = 0, value = 0;
while ((byte = fgetc(file)) >= 128) {
value |= (byte & 127) << shift;
shift += 7;
}
if (byte < 0)
return byte;
return value | (byte << shift);
}
void deinterleave(long mixed, int *values, int count)
{
for (int j = 0; j < count; ++j) {
values[j] = 0;
long m = mixed >> j;
for (int i = 0; m; ++i, m >>= count)
values[j] |= (m & 1L) << i;
}
}
int sgn_int(int x)
{
return (x >> 1) ^ -(x & 1);
}
int main(int argc, char **argv)
{
if (argc != 3)
return 1;
int width, height, channels;
FILE *ifile = open_img(argv[1], &width, &height, &channels);
if (!ifile)
return 1;
FILE *ofile = open_pnm(argv[2], width, height, channels);
if (!ofile) {
fclose(ifile);
return 1;
}
uint8_t *line = calloc(width, channels);
long limit = channels == 3 ? 255 : 4095;
long total = (long)width * (long)height;
for (long i = 0; i < total;) {
long mixed = leb128(ifile);
if (mixed < 0)
goto eof;
int diff[4];
deinterleave(mixed, diff, channels + 1);
for (int c = 0; c < channels; ++c)
diff[c] = sgn_int(diff[c]);
long count = diff[channels];
if (count == limit)
count += leb128(ifile);
for (++count; count--; ++i) {
for (int c = 0; c < channels; ++c) {
int pred = line[(i % width) * channels + c];
// if (i % width)
// pred = (pred + line[(i % width - 1) * channels + c] + 1) / 2;
int value = diff[c] + pred;
line[(i % width) * channels + c] = value;
fputc(value, ofile);
}
}
}
free(line);
fclose(ifile);
fclose(ofile);
return 0;
eof:
fprintf(stderr, "EOF while reading from \"%s\"\n", argv[1]);
free(line);
fclose(ifile);
fclose(ofile);
return 1;
}