forked from kevinkk525/GUI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.lua
443 lines (354 loc) · 15.7 KB
/
image.lua
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
local color = require("color")
local filesystem = require("filesystem_adv")
local unicode = require("unicode")
--------------------------------------------------------------------------------
local image = {}
local OCIFSignature = "OCIF"
local encodingMethodsLoad = {}
local encodingMethodsSave = {}
--------------------------------------------------------------------------------
local function tableSize(t)
local size = 0
for key in pairs(t) do
size = size + 1
end
return size
end
local function group(picture, compressColors)
local groupedPicture, x, y, background, foreground = {}, 1, 1
for i = 3, #picture, 4 do
if compressColors then
background, foreground = color.to8Bit(picture[i]), color.to8Bit(picture[i + 1])
if i % 603 == 0 then
computer.pullSignal(0)
end
else
background, foreground = picture[i], picture[i + 1]
end
groupedPicture[picture[i + 2]] = groupedPicture[picture[i + 2]] or {}
groupedPicture[picture[i + 2]][picture[i + 3]] = groupedPicture[picture[i + 2]][picture[i + 3]] or {}
groupedPicture[picture[i + 2]][picture[i + 3]][background] = groupedPicture[picture[i + 2]][picture[i + 3]][background] or {}
groupedPicture[picture[i + 2]][picture[i + 3]][background][foreground] = groupedPicture[picture[i + 2]][picture[i + 3]][background][foreground] or {}
groupedPicture[picture[i + 2]][picture[i + 3]][background][foreground][y] = groupedPicture[picture[i + 2]][picture[i + 3]][background][foreground][y] or {}
table.insert(groupedPicture[picture[i + 2]][picture[i + 3]][background][foreground][y], x)
x = x + 1
if x > picture[1] then
x, y = 1, y + 1
end
end
return groupedPicture
end
encodingMethodsSave[5] = function(file, picture)
file:writeBytes(
bit32.rshift(picture[1], 8),
bit32.band(picture[2], 0xFF)
)
for i = 3, #picture, 4 do
file:writeBytes(
color.to8Bit(picture[i]),
color.to8Bit(picture[i + 1]),
math.floor(picture[i + 2] * 255)
)
file:write(picture[i + 3])
end
end
encodingMethodsLoad[5] = function(file, picture)
picture[1] = file:readBytes(2)
picture[2] = file:readBytes(2)
for i = 1, image.getWidth(picture) * image.getHeight(picture) do
table.insert(picture, color.to24Bit(file:readBytes(1)))
table.insert(picture, color.to24Bit(file:readBytes(1)))
table.insert(picture, file:readBytes(1) / 255)
table.insert(picture, file:readUnicodeChar())
end
end
encodingMethodsSave[6] = function(file, picture)
-- Grouping picture by it's alphas, symbols and colors
local groupedPicture = group(picture, true)
-- Writing 1 byte per image width and height
file:writeBytes(
picture[1],
picture[2]
)
-- Writing 1 byte for alphas array size
file:writeBytes(tableSize(groupedPicture))
for alpha in pairs(groupedPicture) do
local symbolsSize = tableSize(groupedPicture[alpha])
file:writeBytes(
-- Writing 1 byte for current alpha value
math.floor(alpha * 255),
-- Writing 2 bytes for symbols array size
bit32.rshift(symbolsSize, 8),
bit32.band(symbolsSize, 0xFF)
)
for symbol in pairs(groupedPicture[alpha]) do
-- Writing current unicode symbol value
file:write(symbol)
-- Writing 1 byte for backgrounds array size
file:writeBytes(tableSize(groupedPicture[alpha][symbol]))
for background in pairs(groupedPicture[alpha][symbol]) do
file:writeBytes(
-- Writing 1 byte for background color value (compressed by color)
background,
-- Writing 1 byte for foregrounds array size
tableSize(groupedPicture[alpha][symbol][background])
)
for foreground in pairs(groupedPicture[alpha][symbol][background]) do
file:writeBytes(
-- Writing 1 byte for foreground color value (compressed by color)
foreground,
-- Writing 1 byte for y array size
tableSize(groupedPicture[alpha][symbol][background][foreground])
)
for y in pairs(groupedPicture[alpha][symbol][background][foreground]) do
file:writeBytes(
-- Writing 1 byte for current y value
y,
-- Writing 1 byte for x array size
#groupedPicture[alpha][symbol][background][foreground][y]
)
for x = 1, #groupedPicture[alpha][symbol][background][foreground][y] do
file:writeBytes(groupedPicture[alpha][symbol][background][foreground][y][x])
end
end
end
end
end
end
end
encodingMethodsLoad[6] = function(file, picture)
picture[1] = file:readBytes(1)
picture[2] = file:readBytes(1)
local currentAlpha, currentSymbol, currentBackground, currentForeground, currentY
local alphaSize, symbolSize, backgroundSize, foregroundSize, ySize, xSize
alphaSize = file:readBytes(1)
for alpha = 1, alphaSize do
currentAlpha = file:readBytes(1) / 255
symbolSize = file:readBytes(2)
for symbol = 1, symbolSize do
currentSymbol = file:readUnicodeChar()
backgroundSize = file:readBytes(1)
for background = 1, backgroundSize do
currentBackground = color.to24Bit(file:readBytes(1))
foregroundSize = file:readBytes(1)
for foreground = 1, foregroundSize do
currentForeground = color.to24Bit(file:readBytes(1))
ySize = file:readBytes(1)
for y = 1, ySize do
currentY = file:readBytes(1)
xSize = file:readBytes(1)
for x = 1, xSize do
image.set(
picture,
file:readBytes(1),
currentY,
currentBackground,
currentForeground,
currentAlpha,
currentSymbol
)
end
end
end
end
end
end
end
--------------------------------------------------------------------------------
function image.getIndex(x, y, width)
return 4 * (width * (y - 1) + x) - 1
end
function image.create(width, height, background, foreground, alpha, symbol, random)
local picture = { width, height }
for i = 1, width * height do
table.insert(picture, random and math.random(0x0, 0xFFFFFF) or (background or 0x0))
table.insert(picture, random and math.random(0x0, 0xFFFFFF) or (foreground or 0x0))
table.insert(picture, alpha or 0x0)
table.insert(picture, random and string.char(math.random(65, 90)) or (symbol or " "))
end
return picture
end
function image.copy(picture)
local newPicture = {}
for i = 1, #picture do
newPicture[i] = picture[i]
end
return newPicture
end
function image.save(path, picture, encodingMethod)
encodingMethod = encodingMethod or 6
local file, reason = filesystem.open(path, "wb")
if file then
if encodingMethodsSave[encodingMethod] then
file:write(OCIFSignature, string.char(encodingMethod))
local result, reason = xpcall(encodingMethodsSave[encodingMethod], debug.traceback, file, picture)
file:close()
if result then
return true
else
return false, "Failed to save OCIF image: " .. tostring(reason)
end
else
file:close()
return false, "Failed to save OCIF image: encoding method \"" .. tostring(encodingMethod) .. "\" is not supported"
end
else
return false, "Failed to open file for writing: " .. tostring(reason)
end
end
function image.load(path)
local file, reason = filesystem.open(path, "rb")
if file then
local readedSignature = file:readString(#OCIFSignature)
if readedSignature == OCIFSignature then
local encodingMethod = file:readBytes(1)
if encodingMethodsLoad[encodingMethod] then
local picture = {}
local result, reason = xpcall(encodingMethodsLoad[encodingMethod], debug.traceback, file, picture)
file:close()
if result then
return picture
else
return false, "Failed to load OCIF image: " .. tostring(reason)
end
else
file:close()
return false, "Failed to load OCIF image: encoding method \"" .. tostring(encodingMethod) .. "\" is not supported"
end
else
file:close()
return false, "Failed to load OCIF image: binary signature \"" .. tostring(readedSignature) .. "\" is not valid"
end
else
return false, "Failed to open file \"" .. tostring(path) .. "\" for reading: " .. tostring(reason)
end
end
-------------------------------------------------------------------------------
function image.toString(picture)
local charArray = {
string.format("%02X", picture[1]),
string.format("%02X", picture[2])
}
for i = 3, #picture, 4 do
table.insert(charArray, string.format("%02X", color.to8Bit(picture[i])))
table.insert(charArray, string.format("%02X", color.to8Bit(picture[i + 1])))
table.insert(charArray, string.format("%02X", math.floor(picture[i + 2] * 255)))
table.insert(charArray, picture[i + 3])
if i % 603 == 0 then
computer.pullSignal(0)
end
end
return table.concat(charArray)
end
function image.fromString(pictureString)
local picture = {
tonumber("0x" .. unicode.sub(pictureString, 1, 2)),
tonumber("0x" .. unicode.sub(pictureString, 3, 4)),
}
for i = 5, unicode.len(pictureString), 7 do
table.insert(picture, color.to24Bit(tonumber("0x" .. unicode.sub(pictureString, i, i + 1))))
table.insert(picture, color.to24Bit(tonumber("0x" .. unicode.sub(pictureString, i + 2, i + 3))))
table.insert(picture, tonumber("0x" .. unicode.sub(pictureString, i + 4, i + 5)) / 255)
table.insert(picture, unicode.sub(pictureString, i + 6, i + 6))
end
return picture
end
--------------------------------------------------------------------------------
function image.set(picture, x, y, background, foreground, alpha, symbol)
local index = image.getIndex(x, y, picture[1])
picture[index], picture[index + 1], picture[index + 2], picture[index + 3] = background, foreground, alpha, symbol
return picture
end
function image.get(picture, x, y)
local index = image.getIndex(x, y, picture[1])
return picture[index], picture[index + 1], picture[index + 2], picture[index + 3]
end
function image.getSize(picture)
return picture[1], picture[2]
end
function image.getWidth(picture)
return picture[1]
end
function image.getHeight(picture)
return picture[2]
end
function image.transform(picture, newWidth, newHeight)
local newPicture, stepWidth, stepHeight, background, foreground, alpha, symbol = { newWidth, newHeight }, picture[1] / newWidth, picture[2] / newHeight
local x, y = 1, 1
for j = 1, newHeight do
for i = 1, newWidth do
background, foreground, alpha, symbol = image.get(picture, math.floor(x), math.floor(y))
table.insert(newPicture, background)
table.insert(newPicture, foreground)
table.insert(newPicture, alpha)
table.insert(newPicture, symbol)
x = x + stepWidth
end
x, y = 1, y + stepHeight
end
return newPicture
end
function image.crop(picture, fromX, fromY, width, height)
if fromX >= 1 and fromY >= 1 and fromX + width - 1 <= picture[1] and fromY + height - 1 <= picture[2] then
local newPicture, background, foreground, alpha, symbol = { width, height }
for y = fromY, fromY + height - 1 do
for x = fromX, fromX + width - 1 do
background, foreground, alpha, symbol = image.get(picture, x, y)
table.insert(newPicture, background)
table.insert(newPicture, foreground)
table.insert(newPicture, alpha)
table.insert(newPicture, symbol)
end
end
return newPicture
else
return false, "Failed to crop image: target coordinates are out of source range"
end
end
function image.flipHorizontally(picture)
local newPicture, background, foreground, alpha, symbol = { picture[1], picture[2] }
for y = 1, picture[2] do
for x = picture[1], 1, -1 do
background, foreground, alpha, symbol = image.get(picture, x, y)
table.insert(newPicture, background)
table.insert(newPicture, foreground)
table.insert(newPicture, alpha)
table.insert(newPicture, symbol)
end
end
return newPicture
end
function image.flipVertically(picture)
local newPicture, background, foreground, alpha, symbol = { picture[1], picture[2] }
for y = picture[2], 1, -1 do
for x = 1, picture[1] do
background, foreground, alpha, symbol = image.get(picture, x, y)
table.insert(newPicture, background)
table.insert(newPicture, foreground)
table.insert(newPicture, alpha)
table.insert(newPicture, symbol)
end
end
return newPicture
end
function image.expand(picture, fromTop, fromBottom, fromLeft, fromRight, background, foreground, alpha, symbol)
local newPicture = image.create(picture[1] + fromRight + fromLeft, picture[2] + fromTop + fromBottom, background, foreground, alpha, symbol)
for y = 1, picture[2] do
for x = 1, picture[1] do
image.set(newPicture, x + fromLeft, y + fromTop, image.get(picture, x, y))
end
end
return newPicture
end
function image.blend(picture, blendColor, transparency)
local newPicture = { picture[1], picture[2] }
for i = 3, #picture, 4 do
table.insert(newPicture, color.blend(picture[i], blendColor, transparency))
table.insert(newPicture, color.blend(picture[i + 1], blendColor, transparency))
table.insert(newPicture, picture[i + 2])
table.insert(newPicture, picture[i + 3])
end
return newPicture
end
--------------------------------------------------------------------------------
return image