-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathOCIF.lua
More file actions
executable file
·196 lines (160 loc) · 6.44 KB
/
OCIF.lua
File metadata and controls
executable file
·196 lines (160 loc) · 6.44 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
local args = {...}
local image = args[1]
local bit32 = require("bit32")
require("advancedLua")
local unicode = require("unicode")
local fs = require("filesystem")
local color = require("color")
--------------------------------------------------------------------------------
local module = {}
local OCIFSignature = "OCIF"
local encodingMethods = {
load = {},
save = {}
}
--------------------------------------------------------------------------------
local function writeByteArrayToFile(file, byteArray)
for i = 1, #byteArray do
file:write(string.char(byteArray[i]))
end
end
local function readNumberFromFile(file, countOfBytes)
local byteArray = {}
for i = 1, countOfBytes do
table.insert(byteArray, string.byte(file:read(1)))
end
return bit32.byteArrayToNumber(byteArray)
end
--------------------------------------------------------------------------------
encodingMethods.save[5] = function(file, picture)
for i = 1, #picture[3] do
file:write(string.char(color.to8Bit(picture[3][i])))
file:write(string.char(color.to8Bit(picture[4][i])))
file:write(string.char(math.floor(picture[5][i] * 255)))
writeByteArrayToFile(file, {string.byte(picture[6][i], 1, 6)})
end
end
encodingMethods.load[5] = function(file, picture)
picture[1] = readNumberFromFile(file, 2)
picture[2] = readNumberFromFile(file, 2)
for i = 1, image.getWidth(picture) * image.getHeight(picture) do
table.insert(picture[3], color.to24Bit(string.byte(file:read(1))))
table.insert(picture[4], color.to24Bit(string.byte(file:read(1))))
table.insert(picture[5], string.byte(file:read(1)) / 255)
table.insert(picture[6], fs.readUnicodeChar(file))
end
end
--------------------------------------------------------------------------------
encodingMethods.save[6] = function(file, picture)
-- Grouping picture by it's alphas, symbols and colors
local groupedPicture = image.group(picture, true)
-- Writing 1 byte for alphas array size
file:write(string.char(table.size(groupedPicture)))
for alpha in pairs(groupedPicture) do
-- Writing 1 byte for current alpha value
file:write(string.char(math.floor(alpha * 255)))
-- Writing 2 bytes for symbols array size
writeByteArrayToFile(file, bit32.numberToFixedSizeByteArray(table.size(groupedPicture[alpha]), 2))
for symbol in pairs(groupedPicture[alpha]) do
-- Writing N bytes for current unicode symbol value
writeByteArrayToFile(file, { string.byte(symbol, 1, 6) })
-- Writing 1 byte for backgrounds array size
file:write(string.char(table.size(groupedPicture[alpha][symbol])))
for background in pairs(groupedPicture[alpha][symbol]) do
-- Writing 1 byte for background color value (compressed by color)
file:write(string.char(background))
-- Writing 1 byte for foregrounds array size
file:write(string.char(table.size(groupedPicture[alpha][symbol][background])))
for foreground in pairs(groupedPicture[alpha][symbol][background]) do
-- Writing 1 byte for foreground color value (compressed by color)
file:write(string.char(foreground))
-- Writing 1 byte for y array size
file:write(string.char(table.size(groupedPicture[alpha][symbol][background][foreground])))
for y in pairs(groupedPicture[alpha][symbol][background][foreground]) do
-- Writing 1 byte for current y value
file:write(string.char(y))
-- Writing 1 byte for x array size
file:write(string.char(#groupedPicture[alpha][symbol][background][foreground][y]))
for x = 1, #groupedPicture[alpha][symbol][background][foreground][y] do
file:write(string.char(groupedPicture[alpha][symbol][background][foreground][y][x]))
end
end
end
end
end
end
end
encodingMethods.load[6] = function(file, picture)
picture[1] = string.byte(file:read(1))
picture[2] = string.byte(file:read(1))
local currentAlpha, currentSymbol, currentBackground, currentForeground, currentY, currentX
local alphaSize, symbolSize, backgroundSize, foregroundSize, ySize, xSize
alphaSize = string.byte(file:read(1))
for alpha = 1, alphaSize do
currentAlpha = string.byte(file:read(1)) / 255
symbolSize = readNumberFromFile(file, 2)
for symbol = 1, symbolSize do
currentSymbol = fs.readUnicodeChar(file)
backgroundSize = string.byte(file:read(1))
for background = 1, backgroundSize do
currentBackground = color.to24Bit(string.byte(file:read(1)))
foregroundSize = string.byte(file:read(1))
for foreground = 1, foregroundSize do
currentForeground = color.to24Bit(string.byte(file:read(1)))
ySize = string.byte(file:read(1))
for y = 1, ySize do
currentY = string.byte(file:read(1))
xSize = string.byte(file:read(1))
for x = 1, xSize do
currentX = string.byte(file:read(1))
image.set(picture, currentX, currentY, currentBackground, currentForeground, currentAlpha, currentSymbol)
end
end
end
end
end
end
end
--------------------------------------------------------------------------------
function module.load(path)
local file, reason = io.open(path, "rb")
if file then
local readedSignature = file:read(#OCIFSignature)
if readedSignature == OCIFSignature then
local encodingMethod = string.byte(file:read(1))
if encodingMethods.load[encodingMethod] then
local picture = {1, 1, {}, {}, {}, {}}
encodingMethods.load[encodingMethod](file, picture)
file:close()
return picture
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 module.save(path, picture, encodingMethod)
encodingMethod = encodingMethod or 6
local file, reason = io.open(path, "wb")
if file then
if encodingMethods.save[encodingMethod] then
file:write(OCIFSignature, string.char(encodingMethod), string.char(picture[1]), string.char(picture[2]))
encodingMethods.save[encodingMethod](file, picture)
file:close()
return true
else
file:close()
return false, "Failed to save file as OCIF image: encoding method \"" .. tostring(encodingMethod) .. "\" is not supported"
end
else
return false, "Failed to open file for writing: " .. tostring(reason)
end
end
--------------------------------------------------------------------------------
return module