-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathencode.c
189 lines (180 loc) · 4.46 KB
/
encode.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
/*
Encoder for lossless image compression
Copyright 2024 Ahmet Inan <[email protected]>
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
FILE *open_pnm(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;
}
int letter = fgetc(file);
int number = fgetc(file);
if ('P' != letter || ('5' != number && '6' != number)) {
fprintf(stderr, "file \"%s\" neither P5 nor P6 image\n", name);
fclose(file);
return 0;
}
*channels = number == '5' ? 1 : 3;
int integer[3];
int byte = fgetc(file);
if (EOF == byte)
goto eof;
for (int i = 0; i < 3; i++) {
while ('#' == (byte = fgetc(file)))
while ('\n' != (byte = fgetc(file)))
if (EOF == byte)
goto eof;
while ((byte < '0') || ('9' < byte))
if (EOF == (byte = fgetc(file)))
goto eof;
char str[16];
for (int n = 0; n < 16; n++) {
if (('0' <= byte) && (byte <= '9') && n < 15) {
str[n] = byte;
if (EOF == (byte = fgetc(file)))
goto eof;
} else {
str[n] = 0;
break;
}
}
integer[i] = atoi(str);
}
if (!(integer[0] && integer[1] && integer[2])) {
fprintf(stderr, "could not read image file \"%s\"\n", name);
fclose(file);
return 0;
}
if (integer[2] != 255) {
fprintf(stderr, "cant read \"%s\", only 8 bit per channel SRGB supported\n", name);
fclose(file);
return 0;
}
*width = integer[0];
*height = integer[1];
return file;
eof:
fclose(file);
fprintf(stderr, "EOF while reading from \"%s\"\n", name);
return 0;
}
FILE *open_img(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;
}
uint16_t dimensions[2] = { width - 1, height - 1 };
if (3 != fwrite("IMG", 1, 3, file) || channels != fputc(channels, file) || 2 != fwrite(dimensions, 2, 2, file)) {
fprintf(stderr, "could not write to file \"%s\"\n", name);
fclose(file);
}
return file;
}
void leb128(FILE *file, long value)
{
while (value >= 128) {
fputc((value & 127) | 128, file);
value >>= 7;
}
fputc(value, file);
}
long interleave(const int *values, int count)
{
long mixed = 0;
for (int j = 0; j < count; ++j) {
int v = values[j];
for (int i = j; v; i += count, v >>= 1)
mixed |= (v & 1L) << i;
}
return mixed;
}
int uns_int(int x)
{
return (x << 1) ^ (x >> 31);
}
int main(int argc, char **argv)
{
if (argc != 3)
return 1;
int width, height, channels;
FILE *ifile = open_pnm(argv[1], &width, &height, &channels);
if (!ifile)
return 1;
fprintf(stderr, "width = %d, height = %d, channels = %d\n", width, height, channels);
if (width > 65536 || height > 65536) {
fprintf(stderr, "max supported width or height is 65536\n");
fclose(ifile);
return 1;
}
FILE *ofile = open_img(argv[2], width, height, channels);
if (!ofile) {
fclose(ifile);
return 1;
}
uint8_t *line = calloc(width, channels);
int prev[4] = { 0, 0, 0, 0 };
long count = 0, limit = channels == 3 ? 255 : 4095;
long total = (long)width * (long)height;
for (long i = 0; i < total; ++i) {
int diff[3], equal = 1;
for (int c = 0; c < channels; ++c) {
int value = fgetc(ifile);
if (value < 0)
goto eof;
int pred = line[(i % width) * channels + c];
// if (i % width)
// pred = (pred + line[(i % width - 1) * channels + c] + 1) / 2;
diff[c] = uns_int(value - pred);
line[(i % width) * channels + c] = value;
equal &= diff[c] == prev[c];
}
if (!i) {
for (int c = 0; c < channels; ++c)
prev[c] = diff[c];
} else if (equal) {
++count;
} else if (count < limit) {
prev[channels] = count;
leb128(ofile, interleave(prev, channels + 1));
for (int c = 0; c < channels; ++c)
prev[c] = diff[c];
count = 0;
} else {
prev[channels] = limit;
leb128(ofile, interleave(prev, channels + 1));
leb128(ofile, count - limit);
for (int c = 0; c < channels; ++c)
prev[c] = diff[c];
count = 0;
}
}
if (count < limit) {
prev[channels] = count;
leb128(ofile, interleave(prev, channels + 1));
} else {
prev[channels] = limit;
leb128(ofile, interleave(prev, channels + 1));
leb128(ofile, count - limit);
}
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;
}