-
Notifications
You must be signed in to change notification settings - Fork 0
/
playground.py
264 lines (239 loc) · 8.05 KB
/
playground.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
import qoi
import numpy as np
import matplotlib.pyplot as plt
import os
def qoi_encode(mat: np.ndarray, path, debug=False):
height, width = mat.shape[0], mat.shape[1]
# Flatten image matrix
mat = mat.reshape((width * height, 3))
with open(path, 'wb') as f:
# Write qoi image header
header = qoi.pack_qoi_header(width, height)
f.write(header)
# Encode image
px_len = width * height
px = np.zeros((3,), dtype=np.uint8) # current pixel
px_pre = px # previous pixel
run = 0 # run length
mat_pos = 0 # image matrix index position
index_table = np.zeros((64, 3), dtype=np.uint8)
# Start encoding
for mat_pos in range(px_len):
px = mat[mat_pos]
if np.array_equal(px, px_pre) and mat_pos != 0 and run < 62:
run += 1
# Write qoi end marker
if mat_pos == px_len - 1:
f.write(qoi.pack_qoi_op_run(run))
else:
# Write run chunk
if run != 0:
f.write(qoi.pack_qoi_op_run(run))
# Clean run after write run chunk
run = 0
# Process current pixel
r = int(px[0]) # int(pixel value) to avoid overflow.
g = int(px[1])
b = int(px[2])
index_pos = qoi.qoi_color_hash(r, g, b)
if np.array_equal(index_table[index_pos], px):
f.write(qoi.pack_qoi_op_index(index_pos))
else:
dr = r - int(px_pre[0])
dg = g - int(px_pre[1])
db = b - int(px_pre[2])
drg = dr - dg
dbg = db - dg
if (dr > -3 and dr < 2)\
and (dg > -3 and dg < 2)\
and (db > -3 and db < 2):
f.write(qoi.pack_qoi_op_diff(dr, dg, db))
elif (dg > -33 and dg < 32)\
and (drg > -9 and drg < 8)\
and (dbg > -9 and dbg < 8):
f.write(qoi.pack_qoi_op_luma(drg, dg, dbg))
else:
f.write(qoi.pack_qoi_op_rgb(r, g, b))
index_table[index_pos] = px
px_pre = px
f.write(qoi.END_MARKER)
return 1
def read_sign_byte(byte, bit):
if bit == 2:
mask = 0x02
if (byte & mask) >> 1 == 1:
byte = -1 - (~byte & 0x03)
elif bit == 4:
mask = 0x08
if (byte & mask) >> 3 == 1:
byte = -1 - (~byte & 0x0f)
elif bit == 6:
mask = 0x20
if (byte & mask) >> 5 == 1:
byte = -1 - (~byte & 0x3f)
else:
return None
return byte
def decode_rgb(f, index_table):
r = int.from_bytes(f.read(1), 'big')
g = int.from_bytes(f.read(1), 'big')
b = int.from_bytes(f.read(1), 'big')
px = np.asarray([r, g, b], dtype=np.uint8)
# Assign index table
index_pos = qoi.qoi_color_hash(r, g, b)
index_table[index_pos] = px
return px
def decode_diff(buffer, px, debug=False):
dr = (buffer & 0x30) >> 4
dr = read_sign_byte(dr, 2)
dg = (buffer & 0x0c) >> 2
dg = read_sign_byte(dg, 2)
db = (buffer & 0x03)
db = read_sign_byte(db, 2)
px = (px + np.asarray([dr, dg, db])).astype(np.uint8)
if debug:
print('diff')
print(bin(buffer))
print(dr, dg, db)
return px
def decode_luma(f, buffer, px, debug=False):
dg = (buffer & 0x3f)
dg = read_sign_byte(dg, 6)
another_buffer = int.from_bytes(f.read(1), 'big')
drg = (another_buffer & 0xf0) >> 4
drg = read_sign_byte(drg, 4)
dbg = another_buffer & 0x0f
dbg = read_sign_byte(dbg, 4)
dr = drg + dg
db = dbg + dg
px = (px + np.asarray([dr, dg, db])).astype(np.uint8)
if debug:
print('luma')
print(bin(another_buffer))
print(drg, dg, dbg)
print(dr, dg, db)
print(px.dtype)
return px
def decode_index(buffer, index_table):
index_pos = buffer & 0x3f
px = index_table[index_pos]
return px
def qoi_decode(path, debug=False):
# Check if path is valid
if not os.path.isfile(path):
return None
with open(path, 'rb') as f:
# Read qoi image header
buffer = f.read(qoi.QOI_HEADER.size)
header = qoi.Qoi_header()
header.read_buffer(buffer)
if debug:
header.show_attribute()
# Decode qoi chunks
px_len = header.width * header.height
px = np.zeros((3,), dtype=np.uint8)
mat = np.zeros((px_len, 3), dtype=np.uint8) # flattened image matrix
mat_pos = 0
run = 0
index_table = np.zeros((64, 3), dtype=np.uint8)
# Start decoding
while mat_pos < px_len:
# Decode run chunk
if run != 0:
run -= 1
mat[mat_pos] = px
mat_pos += 1
continue
buffer = int.from_bytes(f.read(1), 'big')
if buffer == qoi.QOI_TAG_RGB:
px = decode_rgb(f, index_table)
elif buffer == qoi.QOI_TAG_RGBA:
pass
else:
tag = (buffer & 0xc0)
if tag == qoi.QOI_TAG_RUN:
run = (buffer & 0x3f) + 1
run -= 1
elif tag == qoi.QOI_TAG_DIFF:
px = decode_diff(buffer, px)
elif tag == qoi.QOI_TAG_LUMA:
px = decode_luma(f, buffer, px)
elif tag == qoi.QOI_TAG_INDEX:
px = decode_index(buffer, index_table)
mat[mat_pos] = px
mat_pos += 1
return mat.reshape((header.height, header.width, 3))
def main():
# img = plt.imread('./data/background.jpg')
img = plt.imread('./data/background_small.jpg')
# img = img[:200, :200, :]
# np.save('./output/test', img) # Save image matrix.
# plt.imsave('./output/test.png', img) # Save image in png.
path = './output/test.qoi'
# Encode image into qoi format
qoi_encode(img, path)
# Decode qoi image
mat = qoi_decode(path)
plt.subplot(211)
plt.imshow(img)
plt.subplot(212)
plt.imshow(mat)
plt.show()
def decode_test(write_case=False, show_case=True):
# Doing a decode test
path = './output/case.qoi'
if write_case:
path = './output/case0.qoi'
write_test_case(path)
mat = qoi_decode(path, True)
if show_case:
plt.imshow(mat)
plt.show()
np.save('./output/case', mat)
def write_test_case(path):
"""Write a qoi image for code test."""
width, height = 10, 3
# Flatten image matrix
with open(path, 'wb') as f:
# Writh qoi image header
header = qoi.pack_qoi_header(width, height)
f.write(header)
# Write qoi chunks
data = qoi.pack_qoi_op_rgb(78, 88, 98)
f.write(data)
# Run chunk
data = qoi.pack_qoi_op_run(9)
f.write(data)
# rgb, diff, luma chunks
data = qoi.pack_qoi_op_rgb(60, 60, 60)
f.write(data)
data = qoi.pack_qoi_op_diff(-2, -2, -2)
f.write(data)
data = qoi.pack_qoi_op_luma(-3, -30, -5)
f.write(data)
# rgb, index
data = qoi.pack_qoi_op_rgb(133, 154, 96)
index_pos = qoi.qoi_color_hash(133, 154, 96)
f.write(data)
data = qoi.pack_qoi_op_diff(1, 0, 1)
f.write(data)
data = qoi.pack_qoi_op_index(index_pos)
f.write(data)
data = qoi.pack_qoi_op_luma(2, 28, 5)
f.write(data)
data = qoi.pack_qoi_op_rgb(255, 255, 255)
f.write(data)
data = qoi.pack_qoi_op_run(12)
f.write(data)
f.write(qoi.END_MARKER)
def encode_test():
mat = np.load('./output/case.npy')
path = './output/case.qoi'
qoi_encode(mat, path, True)
with open(path, 'rb') as f:
bytes = f.read(48)
print(bytes)
if __name__ == "__main__":
main()
# encode_test()
# decode_test(write_case=False, show_case=True)