-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqok.c
366 lines (317 loc) · 7.96 KB
/
qok.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#include <inttypes.h>
#include <stdio.h>
#include <byteswap.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
struct image
{
uint8_t *data;
uint32_t width;
uint32_t height;
uint8_t channels;
};
void read_ppm(const char * filename, struct image *img)
{
FILE *f = fopen(filename, "r");
fscanf(f, "P6 %d %d 255\n", &img->width, &img->height);
img->channels = 3;
img->data = malloc(img->width * img->height * img->channels);
fread(&img->data[0], sizeof(uint8_t), img->width * img->height * img->channels, f);
fclose(f);
}
void write_ppm(const struct image *img, const char * filename)
{
FILE *f = fopen(filename, "w");
fprintf(f, "P6 %d %d 255\n", img->width, img->height);
uint8_t * data = img->data;
for (int i = 0; i < img->width * img->height; i++) {
fwrite(&data[0], sizeof(uint8_t), 3, f);
data += img->channels;
}
fclose(f);
}
struct qoi_header
{
char magic[4]; // magic bytes "qoif"
uint32_t width; // image width in pixels (BE)
uint32_t height; // image height in pixels (BE)
uint8_t channels; // 3 = RGB, 4 = RGBA
uint8_t colorspace; // 0 = sRGB with linear alpha
// 1 = all channels linear
};
// struct has gaps/is padded!
// static_assert(sizeof(struct qoi_header) == 11);
struct pixel4
{
uint8_t r, g, b, a;
};
static struct pixel4 lookup[64];
_Bool pixel4Equal(struct pixel4 a,struct pixel4 b)
{
return a.r == b.r && a.g == b.g && a.b == b.b && a.a == b.a;
}
uint32_t index_position(struct pixel4 p)
{
return (p.r * 3u + p.g * 5u + p.b * 7u + p.a * 11u) % 64u;
}
union QOI_OP
{
uint8_t u8;
struct
{
unsigned int value : 6;
unsigned int op : 2;
};
};
// fun fact: sizeof(QOI_OP) == sizeof(unsigned int) == 4
union QOI_OP_DIFF
{
uint8_t u8;
struct
{
unsigned int db : 2;
unsigned int dg : 2;
unsigned int dr : 2;
unsigned int op : 2;
};
};
union QOI_OP_LUMA_2
{
uint8_t u8;
struct
{
unsigned int db_dg : 4;
unsigned int dr_dg : 4;
};
};
void encode_qok(struct image * img, FILE * f)
{
memset(&lookup[0], 0, sizeof(lookup));
struct qoi_header header = {
.magic = {'q','o','i','f'},
.width = __bswap_32(img->width),
.height = __bswap_32(img->height),
.channels = 3,
.colorspace = 0
};
fwrite(&header.magic, sizeof(header.magic), 1, f);
fwrite(&header.width, sizeof(header.width), 1, f);
fwrite(&header.height, sizeof(header.height), 1, f);
fwrite(&header.channels, sizeof(header.channels), 1, f);
fwrite(&header.colorspace, sizeof(header.colorspace), 1, f);
uint32_t datasize = img->width * img->height * header.channels;
uint32_t index = 0;
struct pixel4 p = { 0, 0, 0, 255 };
uint8_t * raw_image = img->data;
for (;;)
{
struct pixel4 c = { 0, 0, 0, 255 };
int32_t run = 0;
for(; index < datasize; run++)
{
c.r = raw_image[index++];
c.g = raw_image[index++];
c.b = raw_image[index++];
if (header.channels == 4) { c.a = raw_image[index++]; };
if (!pixel4Equal(p,c)) break;
}
for (; run > 0; run -= 62)
{
union QOI_OP qoiOp = {
.op = 0b11,
.value = (run < 62 ? run : 62) - 1
};
fwrite(&qoiOp.u8, sizeof(uint8_t), 1, f);
}
if (index >= datasize) break;
int32_t lookup_index = index_position(c);
int32_t dr = (int32_t)c.r - (int32_t)p.r;
int32_t dg = (int32_t)c.g - (int32_t)p.g;
int32_t db = (int32_t)c.b - (int32_t)p.b;
int32_t dr_dg = dr - dg;
int32_t db_dg = db - dg;
if (pixel4Equal(lookup[lookup_index], c))
{ // QOI_OP_INDEX
union QOI_OP qoiOp = {
.op = 0b00,
.value = lookup_index
};
fwrite(&qoiOp.u8, sizeof(uint8_t), 1, f);
}
else if ( // QOI_OP_DIFF
c.a == p.a &&
dr >= -2 && dr < +2 &&
dg >= -2 && dg < +2 &&
db >= -2 && db < +2)
{
union QOI_OP_DIFF qoiOpDiff = {
.op = 0b01,
.dr = dr+2,
.dg = dg+2,
.db = db+2
};
fwrite(&qoiOpDiff.u8, sizeof(uint8_t), 1, f);
}
else if ( // QOI_OP_LUMA
c.a == p.a &&
dg >= -32 && dg < +32 &&
dr_dg >= -8 && dr_dg < +8 &&
db_dg >= -8 && db_dg < +8)
{
union QOI_OP qoiOp = {
.op = 0b10,
.value = dg+32
};
fwrite(&qoiOp.u8, sizeof(uint8_t), 1, f);
union QOI_OP_LUMA_2 qoiOp2 = {
.dr_dg = dr_dg+8,
.db_dg = db_dg+8
};
fwrite(&qoiOp2.u8, sizeof(uint8_t), 1, f);
}
else if (header.channels == 4)
{
// only channel = 3 supported
assert(0);
}
else {
fwrite(&(uint8_t){254}, sizeof(uint8_t), 1, f);
fwrite(&c.r, sizeof(uint8_t), 1, f);
fwrite(&c.g, sizeof(uint8_t), 1, f);
fwrite(&c.b, sizeof(uint8_t), 1, f);
}
lookup[index_position(c)] = c;
p = c;
}
uint8_t end_of_file[8] = { 0, 0, 0, 0, 0, 0, 0, 1 };
fwrite(&end_of_file[0], sizeof(uint8_t), 8, f);
}
void decode_qok(FILE * f, struct image * img) {
memset(&lookup[0], 0, sizeof(lookup));
struct qoi_header header = { 0 };
fread(&header.magic, sizeof(header.magic), 1, f);
fread(&header.width, sizeof(header.width), 1, f);
fread(&header.height, sizeof(header.height), 1, f);
fread(&header.channels, sizeof(header.channels), 1, f);
fread(&header.colorspace, sizeof(header.colorspace), 1, f);
header.width = __bswap_32 (header.width);
header.height = __bswap_32 (header.height);
uint32_t npixels = header.width * header.height;
uint8_t * raw_image = malloc(npixels * header.channels);
assert(strncmp(header.magic, "qoif", 4) == 0 && "invalid file type");
uint32_t index = 0;
struct pixel4 p = { 0, 0, 0, 255 };
while (index < npixels * header.channels)
{
uint8_t byte1;
fread(&byte1, sizeof(uint8_t), 1, f);
uint32_t run = 1;
switch (byte1)
{
case 254: // QOI_OP_RGB
fread(&p, sizeof(uint8_t), 3, f);
break;
case 255: // QOI_OP_RGBA
fread(&p, sizeof(uint8_t), 4, f);
break;
default:
uint32_t op1 = (byte1 >> 6);
switch (op1)
{
case 0: // QOI_OP_INDEX
{
union QOI_OP qoiOp = { .u8 = byte1 };
assert(qoiOp.op == 0);
p = lookup[qoiOp.value];
break;
}
case 1: // QOI_OP_DIFF
{
union QOI_OP_DIFF qoiOpDiff = { .u8 = byte1 };
p.r = (int32_t)p.r + (int32_t)qoiOpDiff.dr - 2;
p.g = (int32_t)p.g + (int32_t)qoiOpDiff.dg - 2;
p.b = (int32_t)p.b + (int32_t)qoiOpDiff.db - 2;
break;
}
case 2: // QOI_OP_LUMA
{
union QOI_OP qoiOp1 = { .u8 = byte1 };
union QOI_OP_LUMA_2 qoiOp2 = { .u8 = 0 };
fread(&qoiOp2.u8, sizeof(uint8_t), 1, f);
int32_t dg = (int32_t)qoiOp1.value - 32;
int32_t dr = (int32_t)qoiOp2.dr_dg - 8 + dg;
int32_t db = (int32_t)qoiOp2.db_dg - 8 + dg;
p.r = (int32_t)p.r + dr;
p.g = (int32_t)p.g + dg;
p.b = (int32_t)p.b + db;
break;
}
case 3: // QOI_OP_RUN
{
union QOI_OP qoiOp = { .u8 = byte1 };
run = qoiOp.value + 1;
assert(qoiOp.value < 63);
break;
}
default:
assert(0 && "invalid chunk");
}
}
for (int32_t j = 0; j < run; j++)
{
switch (header.channels)
{
case 3:
raw_image[index++] = p.r;
raw_image[index++] = p.g;
raw_image[index++] = p.b;
break;
case 4:
raw_image[index++] = p.r;
raw_image[index++] = p.g;
raw_image[index++] = p.b;
raw_image[index++] = p.a;
break;
}
}
lookup[index_position(p)] = p;
}
{
uint8_t end_of_file[8];
fread(&end_of_file[0], sizeof(uint8_t), 8, f);
uint8_t match[8] = { 0, 0, 0, 0, 0, 0, 0, 1 };
for (int j = 0; j < 8; j++)
assert(end_of_file[j] == match[j]);
}
img->data = raw_image;
img->width = header.width;
img->height = header.height;
img->channels = header.channels;
}
int main(int argc, const char * argv[]) {
assert(argc == 4 && "wrong cmg args");
if (strcmp(argv[1], "decode") == 0)
{
FILE * f = fopen(argv[2], "rb");
struct image img = {};
decode_qok(f, &img);
fclose(f);
write_ppm(&img, argv[3]);
free(img.data);
}
else if (strcmp(argv[1], "encode") == 0)
{
struct image img = {};
read_ppm(argv[2], &img);
FILE * f = fopen(argv[3], "wb");
encode_qok(&img, f);
fclose(f);
free(img.data);
}
else
{
assert(0 && "invalid option");
}
return 0;
}