-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.spwn
213 lines (176 loc) · 7.05 KB
/
lib.spwn
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
type @qoi
BLACK_COLOR = { r: 0, g: 0, b: 0, a: 255 }
BLACK_COLOR_TRANSPARENT = { r: 0, g: 0, b: 0, a: 0 }
impl @qoi {
decode: (path: @string, debug: @bool = false, spam_debug: @bool = false) -> @qoi {
start_time = $.time()
let bytes = $.readfile(path, "bin")
byte_count = bytes.length
start_process_time = $.time()
if debug {
$.print("Bytes: {}b".fmt(byte_count))
$.print("Read file: {}s".fmt(start_process_time - start_time))
}
next_bytes = (n: @number&>=1){
let these_bytes = []
for i in 0..n {
these_bytes.push(bytes.shift())
}
return these_bytes
}
size = (n: @number&>=1, be: @bool = true) {
let current_value = 0
let these_bytes = next_bytes(n)
for i in 0..n {
current_value += these_bytes[i] * 2^((n-i-1)*8 if be else i*8)
}
return current_value
}
if next_bytes(4) != [113,111,105,102] {
throw "Magic bytes not found"
}
width = size(4)
height = size(4)
let image = [ [ BLACK_COLOR_TRANSPARENT ] * height ] * width // setting to transparent black is what makes the most sense
channels = next_bytes(1)[0]
if ![3,4].contains(channels) {
throw "Invalid channels byte"
}
colorspace = next_bytes(1)[0]
if ![0,1].contains(colorspace) {
throw "Invalid colorspace byte"
}
let previous_color = BLACK_COLOR
let previous_colors = [ null ] * 64
previous_colors[@qoi::hash_color(0,0,0,255)] = BLACK_COLOR
previous_colors[@qoi::hash_color(0,0,0,0)] = BLACK_COLOR_TRANSPARENT
let x = 0
let y = 0
increment_position = () {
x += 1
if x >= width { y += 1; x = 0 }
}
decrement_position = () {
x -= 1
if x < 0 { y -= 1; x = width - 1 }
}
while x < width && y < height {
if bytes.length <= 0 {
if debug { $.print("Decoding ended prematurely") }
break
}
head_byte = next_bytes(1)[0]
match head_byte {
==254|==255: () { // [QOI_OP_RGB+QOI_OP_RGBA] set color (rgb+rgba)
if spam_debug { $.print("QOI_OP_RGB" if head_byte == 254 else "QOI_OP_RGBA") }
[ r, g, b ] = next_bytes(3)
a = next_bytes(1)[0] if head_byte == 255 else previous_color.a
image[x][y] = { r, g, b, a }
previous_colors[@qoi::hash_color(r,g,b,a)] = { r, g, b, a }
} (),
>=192&<=253: () { // [QOI_OP_RUN] run length
if spam_debug { $.print("QOI_OP_RUN") }
let length = head_byte - 191 // +1 bias
let ended = false
if spam_debug { $.print(" ",length) }
for i in 0..length {
image[x][y] = previous_color
increment_position()
if y >= height {
decrement_position()
ended = true
break
}
}
if !ended {
decrement_position() // it gets incremented too much
}
} (),
>=128&<=191: () { // [QOI_OP_LUMA] luma difference
if spam_debug { $.print("QOI_OP_LUMA") }
let diff_green = head_byte - 160 // -32 bias
drdgdbdg = next_bytes(1)[0]
// -8 bias
dr_dg = $.floor(drdgdbdg / 2^4 - 8)
db_dg = drdgdbdg % 2^4 - 8
r = (previous_color.r + diff_green + dr_dg) % 256
g = (previous_color.g + diff_green) % 256
b = (previous_color.b + diff_green + db_dg) % 256
if spam_debug { $.print(" ",diff_green," ",dr_dg," ",db_dg) }
image[x][y] = {
r,
g,
b,
a: previous_color.a,
}
previous_colors[@qoi::hash_color(r,g,b,previous_color.a)] = { r, g, b, a: previous_color.a }
} (),
>=64&<=127: () { // [QOI_OP_DIFF] color difference
if spam_debug { $.print("QOI_OP_DIFF") }
let current_byte = head_byte - 2^6
// -2 bias
let dr = -2
let dg = -2
let db = -2
helper = (exp: @number, &var: @number) {
if current_byte >= 2^exp {
current_byte -= 2^exp
var += 2^(exp % 2)
}
}
helper(5, dr)
helper(4, dr)
helper(3, dg)
helper(2, dg)
helper(1, db)
helper(0, db)
if spam_debug { $.print(" ",dr," ",dg," ",db) }
r = (previous_color.r + dr) % 256
g = (previous_color.g + dg) % 256
b = (previous_color.b + db) % 256
image[x][y] = {
r,
g,
b,
a: previous_color.a,
}
previous_colors[@qoi::hash_color(r,g,b,previous_color.a)] = { r, g, b, a: previous_color.a }
} (),
>=0&<=63: () { // [QOI_OP_INDEX]
if spam_debug {
$.print("QOI_OP_INDEX")
$.print(" {}".fmt(head_byte))
}
image[x][y] = previous_colors[head_byte]
} (),
}
if image[x][y] != null {
previous_color = image[x][y]
} else {
$.print("got color null at index [{}/{}] {}".fmt([x,y,head_byte]))
if head_byte >= 64 { throw "{}".fmt(head_byte)}
throw "null"
}
if spam_debug { $.print(" ", image[x][y]) }
increment_position()
}
if debug {
processing_time = $.time() - start_process_time
$.print("Finished processing: {}s".fmt(processing_time))
$.print("Width: {}".fmt(width))
$.print("Height: {}".fmt(height))
$.print("Bytes/second: {}b/s".fmt(byte_count/processing_time))
}
return @qoi::{
image,
width,
height,
}
},
encode: (self) -> [@number] {
throw "bro seriously thought encoder was ready"
},
hash_color: (r: @number, g: @number, b: @number, a: @number = 255) {
return (r * 3 + g * 5 + b * 7 + a * 11) % 64
},
}