-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqoi.py
318 lines (239 loc) · 8.54 KB
/
qoi.py
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
###CONSTANTS###
QOI_SRGB = 0
QOI_LINEAR = 1
QOI_OP_INDEX = 0x00 # 00xxxxxx
QOI_OP_DIFF = 0x40 # 01xxxxxx
QOI_OP_LUMA = 0x80 # 10xxxxxx
QOI_OP_RUN = 0xc0 # 11xxxxxx
QOI_OP_RGB = 0xfe # 11111110
QOI_OP_RGBA = 0xff # 11111111
QOI_MASK_2 = 0xc0 # 11000000
QOI_MAGIC = 1903126886 # Magic...
QOI_HEADER_SIZE = 14
QOI_PIXELS_MAX = 400000000
qoi_padding = [0,0,0,0,0,0,0,1]
def QOI_COLOR_HASH(C: tuple):
(r,g,b,a) = C
return (r*3 + g*5 + b*7 + a*11)
def qoi_write_32(bytes: bytearray, v: int):
#write 4 bytes from v to bytes
#return the opostition in bytes (p)
i = len(bytes)
bytes.append(0)
bytes.append(0)
bytes.append(0)
bytes.append(0)
bytes[i ] = (0xff000000 & v) >> 24
bytes[i+1] = (0x00ff0000 & v) >> 16
bytes[i+2] = (0x0000ff00 & v) >> 8
bytes[i+3] = (0x000000ff & v)
class qoi_desc():
def __init__(self, width: int, height: int, channels: int , colorspace: int) -> None:
self.width = width
self.height = height
self.channels = channels
self.colorspace = colorspace
def qoi_encode(data: bytearray, desc: qoi_desc) -> bytearray:
#if the image is invalid then we return with None
if (data == None or desc == None or
desc.width == 0 or desc.height == 0 or
desc.channels < 3 or desc.channels > 4 or
desc.colorspace > 1 or
desc.height >= QOI_PIXELS_MAX / desc.width):
return None
maxsize = desc.width * desc.height * (desc.channels + 1) + QOI_HEADER_SIZE + len(qoi_padding)
p = 0
#bytes and index
#bytes is a maxsize long list of bits,
#this is where the converted image will be
bytes = bytearray()
#index is a 64 long list of bytes
index = [(0,0,0,0) for _ in range(64)]
#writer the desc header to bytes
qoi_write_32(bytes, QOI_MAGIC) #4 bytes
qoi_write_32(bytes, desc.width)
qoi_write_32(bytes, desc.height)
bytes.append(desc.channels)
bytes.append(desc.colorspace) #1 byte
pixels = data # bytearray()
run = 0
px_prev = (0,0,0,255)
px = px_prev
px_len = desc.width * desc.height * desc.channels
px_end = px_len - desc.channels
channels = desc.channels
px_pos = 0
while(px_pos < px_len):
#if we have 4 channels or 3
if (channels == 4):
# we read 4 bytes (4 8 bit chunks)
px = (pixels[px_pos ],
pixels[px_pos+1],
pixels[px_pos+2],
pixels[px_pos+3])
else:
# we read 3 bytes
px = (pixels[px_pos ],
pixels[px_pos+1],
pixels[px_pos+2],
255)
#Method 1: runs
# if current px and prev px are the same we record a QOI_OP_RUN
if px == px_prev:
run += 1
if (run == 62 or px_pos == px_end):
bytes.append(QOI_OP_RUN | (run-1))
run = 0
else:
#Method 2: index
# if the current pixel has been recoded in index, then we record a QOI_OP_INDEX
# If we are in a run then close it and record a QOI_OP_RUN
if (run > 0):
bytes.append(QOI_OP_RUN | (run-1))
run = 0
index_pos = QOI_COLOR_HASH(px) % 64
if (index[index_pos] == px):
bytes.append(QOI_OP_INDEX | index_pos)
else:
# Method 3: color diff
# if we can record the color diff of prev px and curr px then we do
# and record a QOI_OP_DIFF
#save px to index
index[index_pos] = px
# if the alpha channels match then we try to record the diff
if (px[3] == px_prev[3]):
vr = px[0] - px_prev[0]
vg = px[1] - px_prev[1]
vb = px[2] - px_prev[2]
vg_r = vr - vg
vg_b = vb - vg
# Normal DIFF
if (vr > -3 and vr < 2 and
vg > -3 and vg < 2 and
vb > -3 and vb < 2):
towrite = QOI_OP_DIFF | (vr + 2) << 4 | (vg + 2) << 2 | (vb + 2)
bytes.append(towrite)
# Luma DIFF
elif (vg_r > -9 and vg_r < 8 and
vg > -33 and vg < 32 and
vg_b > -9 and vg_b < 8):
bytes.append(QOI_OP_LUMA | (vg + 32))
bytes.append((vg_r + 8) << 4 | (vg_b + 8))
# NORMAL RGB
else:
bytes.append(QOI_OP_RGB)
bytes.append(px[0])
bytes.append(px[1])
bytes.append(px[2])
# NORMAL RGBA
else:
bytes.append(QOI_OP_RGBA)
bytes.append(px[0])
bytes.append(px[1])
bytes.append(px[2])
bytes.append(px[3])
px_prev = px
px_pos += channels
#padding
for x in qoi_padding:
bytes.append(x)
return bytes
def bti(b: bytearray) -> int:
return int(b.hex(), 16)
def qoi_decode(data: bytearray, size: int, channels: int) -> bytearray:
# read the desc from data
# desc is the first 14 bytes
p = 4
desc = qoi_desc(
bti(data[p:p+4]),
bti(data[p+4:p+8]),
data[p+8],
data[p+9]
)
p = 14
if (data == None or (channels != 0 and channels != 3 and channels != 4) or
size < QOI_HEADER_SIZE + len(qoi_padding) or
desc.width == 0 or desc.height == 0 or
desc.channels < 3 or desc.channels > 4 or
desc.colorspace > 1 or
data[0:4] != b'qoif' or
desc.height >= QOI_PIXELS_MAX / desc.width):
return None
index = [(0,0,0,0) for _ in range(64)]
px = (0,0,0,255)
run = 0
bytes = data
if (channels == 0):
channels = desc.channels
px_len = desc.width * desc.height * channels;
pixels = bytearray(px_len)
chunks_len = size - len(qoi_padding);
px_pos = 0
while(px_pos < px_len):
if (run > 0):
run -= 1
elif (p < chunks_len):
b1 = bytes[p]
p += 1
#QOI_OP_RGB
if b1 == QOI_OP_RGB:
r = bytes[p]
p += 1
g = bytes[p]
p += 1
b = bytes[p]
p += 1
px = (r,g,b,255)
elif b1 == QOI_OP_RGBA:
r = bytes[p]
p += 1
g = bytes[p]
p += 1
b = bytes[p]
p += 1
a = bytes[p]
p += 1
px = (r,g,b,a)
elif ((b1 & QOI_MASK_2) == QOI_OP_INDEX):
px = index[b1]
elif ((b1 & QOI_MASK_2) == QOI_OP_DIFF):
(r,g,b,a) = px
r = (r + ((b1 >> 4) & 0x03) - 2) % 256
g = (g + ((b1 >> 2) & 0x03) - 2) % 256
b = (b + ( b1 & 0x03) - 2) % 256
px = (r,g,b,a)
elif ((b1 & QOI_MASK_2) == QOI_OP_LUMA):
b2 = bytes[p]
p += 1
(r,g,b,a) = px
vg = (b1 & 0x3f) - 32
r = (r + vg - 8 + ((b2 >> 4) & 0x0f)) % 256
g = (g + vg ) % 256
b = (b + vg - 8 + (b2 & 0x0f)) % 256
px = (r,g,b,a)
elif ((b1 & QOI_MASK_2) == QOI_OP_RUN):
run = (b1 & 0x3f)
index[QOI_COLOR_HASH(px) % 64] = px
(r,g,b,a) = px
if (channels == 4):
pixels[px_pos + 0] = r
pixels[px_pos + 1] = g
pixels[px_pos + 2] = b
pixels[px_pos + 3] = a
else:
pixels[px_pos + 0] = r
pixels[px_pos + 1] = g
pixels[px_pos + 2] = b
px_pos += channels
return pixels
def main():
filepath = ""
outpath = ""
with open(filepath, "rb+") as f:
bytes = f.read()
data = bytes
w = qoi_encode(data, qoi_desc(628,655,3,0))
with open(outpath, "wb+") as f:
f.write(w)
if __name__ == "__main__":
main()