-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecoder.py
More file actions
182 lines (154 loc) · 5.29 KB
/
Decoder.py
File metadata and controls
182 lines (154 loc) · 5.29 KB
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
from PIL import Image, ImageDraw
import struct
output = "binary_file.bin" # File to bin uncompressed bite code
Input = "kodim23.qoi" # .qoi file u want to decode
photosafe = "output.png" # file to safe .png
output = open(output, mode="wb")
f = open(Input, mode="rb")
class Pixel:
def __init__(self, r=0, g=0, b=0, a=255):
self.r = r
self.g = g
self.b = b
self.a = a
def getIndexPosition(self):
return (self.r * 3 + self.g * 5 + self.b * 7 + self.a * 11) % 64
def __str__(self):
return f"Pixel (R: {self.r}, G: {self.g}, B: {self.b}, A: {self.a})"
def wraparound(self):
if self.r > 255:
self.r -= 256
if self.r < 0:
self.r += 256
if self.b > 255:
self.b -= 256
if self.b < 0:
self.b += 256
if self.g > 255:
self.g -= 256
if self.g < 0:
self.g += 256
if self.a > 255:
self.a -= 256
if self.a < 0:
self.a += 256
def writeToBin(self, file):
binary_data = self.r.to_bytes(1, 'big')
file.write(binary_data)
binary_data = self.g.to_bytes(1, 'big')
file.write(binary_data)
binary_data = self.b.to_bytes(1, 'big')
file.write(binary_data)
binary_data = self.a.to_bytes(1, 'big')
file.write(binary_data)
# Reading file data with read() method
data = f.read()
header = data[0:14]
body = data[14:]
# Knowing the Type of our data
QOIF = header[0:4]
if QOIF != b'qoif':
f.close()
raise Exception("This is not a qoi file")
width = int.from_bytes(header[4:8], byteorder='big')
height = int.from_bytes(header[8:12], byteorder='big')
image = Image.new("RGBA", (width, height))
binary_data = width.to_bytes(4, 'big')
output.write(binary_data)
print()
binary_data = height.to_bytes(4, 'big')
output.write(binary_data)
draw = ImageDraw.Draw(image)
channels = int.from_bytes(header[12:13], byteorder='big')
colorspace = int.from_bytes(header[13:14], byteorder='big')
print(width, height)
print(body[:14].hex())
if channels == 3:
print("Channels: ", "RGB")
elif channels == 4:
print("Channels: ", "RGBA")
if colorspace == 0:
print("colorspace: ", "sRGB with linear alpha")
elif colorspace == 1:
print("colorspace: ", "all channels linear")
seenPixels = {}
for i in range(64):
seenPixels[i] = Pixel(0, 0, 0, 0)
index = 0
prevPixel = Pixel()
column = 0
row = 0
while index < len(body):
byte = body[index]
currentPixel = Pixel()
currentPixel.a = prevPixel.a
if byte == 254:
currentPixel.r = body[index + 1]
currentPixel.g = body[index + 2]
currentPixel.b = body[index + 3]
currentPixel.a = prevPixel.a
index += 3
elif byte == 255:
currentPixel.r = body[index + 1]
currentPixel.g = body[index + 2]
currentPixel.b = body[index + 3]
currentPixel.a = body[index + 4]
index += 4
elif byte < 64: # starts with 00
if byte in seenPixels:
currentPixel.r = seenPixels[byte].r
currentPixel.g = seenPixels[byte].g
currentPixel.b = seenPixels[byte].b
currentPixel.a = seenPixels[byte].a
else:
# Handle case when pixel index is not found
print("Pixel index not found in seenPixels dictionary.", index, byte)
elif byte < 128:
red_diff = ((byte >> 4) & 0b11) - 2
green_diff = ((byte >> 2) & 0b11) - 2
blue_diff = (byte & 0b11) - 2
currentPixel.r = red_diff + prevPixel.r
currentPixel.g = green_diff + prevPixel.g
currentPixel.b = blue_diff + prevPixel.b
currentPixel.a = prevPixel.a
elif byte < 192:
byte2 = body[index + 1]
index = index + 1
vg = (byte & 0x3f) - 32
currentPixel.r = prevPixel.r + vg - 8 + ((byte2 >> 4) & 0b1111)
currentPixel.g = prevPixel.g + vg
currentPixel.b = prevPixel.b + vg - 8 + ((byte2) & 0b1111)
else:
currentPixel.r = prevPixel.r
currentPixel.g = prevPixel.g
currentPixel.b = prevPixel.b
currentPixel.a = prevPixel.a
if body[index] >> 6 != 3:
print("IETS MIS")
run = body[index] & 0b111111
for i in range(run):
currentPixel.writeToBin(output)
if column < width and row < height:
image.putpixel((column, row), (currentPixel.r, currentPixel.g, currentPixel.b, currentPixel.a))
column += 1
if column >= width:
column = 0
row += 1
seenPixels[currentPixel.getIndexPosition()] = Pixel(currentPixel.r, currentPixel.g, currentPixel.b,
currentPixel.a)
currentPixel.wraparound()
currentPixel.writeToBin(output)
if column < width and row < height:
image.putpixel((column, row), (currentPixel.r, currentPixel.g, currentPixel.b, currentPixel.a))
column += 1
if column >= width:
column = 0
row += 1
seenPixels[currentPixel.getIndexPosition()] = Pixel(currentPixel.r, currentPixel.g, currentPixel.b, currentPixel.a)
prevPixel = Pixel(currentPixel.r, currentPixel.g, currentPixel.b, currentPixel.a)
index += 1
print(width, row, column)
image.show()
image.save(photosafe)
f.close()
output.close()