Petsox

OCIF.lua

Aug 22nd, 2022
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.63 KB | Source Code | 0 0
  1.  
  2. local args = {...}
  3. local image = args[1]
  4.  
  5. local bit32 = require("bit32")
  6. require("advancedLua")
  7. local unicode = require("unicode")
  8. local fs = require("filesystem")
  9. local color = require("color")
  10.  
  11. --------------------------------------------------------------------------------
  12.  
  13. local module = {}
  14. local OCIFSignature = "OCIF"
  15. local encodingMethods = {
  16. load = {},
  17. save = {}
  18. }
  19.  
  20. --------------------------------------------------------------------------------
  21.  
  22. local function writeByteArrayToFile(file, byteArray)
  23. for i = 1, #byteArray do
  24. file:write(string.char(byteArray[i]))
  25. end
  26. end
  27.  
  28. local function readNumberFromFile(file, countOfBytes)
  29. local byteArray = {}
  30. for i = 1, countOfBytes do
  31. table.insert(byteArray, string.byte(file:read(1)))
  32. end
  33.  
  34. return bit32.byteArrayToNumber(byteArray)
  35. end
  36.  
  37. --------------------------------------------------------------------------------
  38.  
  39. encodingMethods.save[5] = function(file, picture)
  40. for i = 1, #picture[3] do
  41. file:write(string.char(color.to8Bit(picture[3][i])))
  42. file:write(string.char(color.to8Bit(picture[4][i])))
  43. file:write(string.char(math.floor(picture[5][i] * 255)))
  44. writeByteArrayToFile(file, {string.byte(picture[6][i], 1, 6)})
  45. end
  46. end
  47.  
  48. encodingMethods.load[5] = function(file, picture)
  49. picture[1] = readNumberFromFile(file, 2)
  50. picture[2] = readNumberFromFile(file, 2)
  51.  
  52. for i = 1, image.getWidth(picture) * image.getHeight(picture) do
  53. table.insert(picture[3], color.to24Bit(string.byte(file:read(1))))
  54. table.insert(picture[4], color.to24Bit(string.byte(file:read(1))))
  55. table.insert(picture[5], string.byte(file:read(1)) / 255)
  56. table.insert(picture[6], fs.readUnicodeChar(file))
  57. end
  58. end
  59.  
  60. --------------------------------------------------------------------------------
  61.  
  62. encodingMethods.save[6] = function(file, picture)
  63. -- Grouping picture by it's alphas, symbols and colors
  64. local groupedPicture = image.group(picture, true)
  65. -- Writing 1 byte for alphas array size
  66. file:write(string.char(table.size(groupedPicture)))
  67.  
  68. for alpha in pairs(groupedPicture) do
  69. -- Writing 1 byte for current alpha value
  70. file:write(string.char(math.floor(alpha * 255)))
  71. -- Writing 2 bytes for symbols array size
  72. writeByteArrayToFile(file, bit32.numberToFixedSizeByteArray(table.size(groupedPicture[alpha]), 2))
  73.  
  74. for symbol in pairs(groupedPicture[alpha]) do
  75. -- Writing N bytes for current unicode symbol value
  76. writeByteArrayToFile(file, { string.byte(symbol, 1, 6) })
  77. -- Writing 1 byte for backgrounds array size
  78. file:write(string.char(table.size(groupedPicture[alpha][symbol])))
  79.  
  80. for background in pairs(groupedPicture[alpha][symbol]) do
  81. -- Writing 1 byte for background color value (compressed by color)
  82. file:write(string.char(background))
  83. -- Writing 1 byte for foregrounds array size
  84. file:write(string.char(table.size(groupedPicture[alpha][symbol][background])))
  85.  
  86. for foreground in pairs(groupedPicture[alpha][symbol][background]) do
  87. -- Writing 1 byte for foreground color value (compressed by color)
  88. file:write(string.char(foreground))
  89. -- Writing 1 byte for y array size
  90. file:write(string.char(table.size(groupedPicture[alpha][symbol][background][foreground])))
  91.  
  92. for y in pairs(groupedPicture[alpha][symbol][background][foreground]) do
  93. -- Writing 1 byte for current y value
  94. file:write(string.char(y))
  95. -- Writing 1 byte for x array size
  96. file:write(string.char(#groupedPicture[alpha][symbol][background][foreground][y]))
  97.  
  98. for x = 1, #groupedPicture[alpha][symbol][background][foreground][y] do
  99. file:write(string.char(groupedPicture[alpha][symbol][background][foreground][y][x]))
  100. end
  101. end
  102. end
  103. end
  104. end
  105. end
  106. end
  107.  
  108. encodingMethods.load[6] = function(file, picture)
  109. picture[1] = string.byte(file:read(1))
  110. picture[2] = string.byte(file:read(1))
  111.  
  112. local currentAlpha, currentSymbol, currentBackground, currentForeground, currentY, currentX
  113. local alphaSize, symbolSize, backgroundSize, foregroundSize, ySize, xSize
  114.  
  115. alphaSize = string.byte(file:read(1))
  116.  
  117. for alpha = 1, alphaSize do
  118. currentAlpha = string.byte(file:read(1)) / 255
  119. symbolSize = readNumberFromFile(file, 2)
  120.  
  121. for symbol = 1, symbolSize do
  122. currentSymbol = fs.readUnicodeChar(file)
  123. backgroundSize = string.byte(file:read(1))
  124.  
  125. for background = 1, backgroundSize do
  126. currentBackground = color.to24Bit(string.byte(file:read(1)))
  127. foregroundSize = string.byte(file:read(1))
  128.  
  129. for foreground = 1, foregroundSize do
  130. currentForeground = color.to24Bit(string.byte(file:read(1)))
  131. ySize = string.byte(file:read(1))
  132.  
  133. for y = 1, ySize do
  134. currentY = string.byte(file:read(1))
  135. xSize = string.byte(file:read(1))
  136.  
  137. for x = 1, xSize do
  138. currentX = string.byte(file:read(1))
  139. image.set(picture, currentX, currentY, currentBackground, currentForeground, currentAlpha, currentSymbol)
  140. end
  141. end
  142. end
  143. end
  144. end
  145. end
  146. end
  147.  
  148. --------------------------------------------------------------------------------
  149.  
  150. function module.load(path)
  151. local file, reason = io.open(path, "rb")
  152. if file then
  153. local readedSignature = file:read(#OCIFSignature)
  154. if readedSignature == OCIFSignature then
  155. local encodingMethod = string.byte(file:read(1))
  156. if encodingMethods.load[encodingMethod] then
  157. local picture = {1, 1, {}, {}, {}, {}}
  158. encodingMethods.load[encodingMethod](file, picture)
  159.  
  160. file:close()
  161. return picture
  162. else
  163. file:close()
  164. return false, "Failed to load OCIF image: encoding method \"" .. tostring(encodingMethod) .. "\" is not supported"
  165. end
  166. else
  167. file:close()
  168. return false, "Failed to load OCIF image: binary signature \"" .. tostring(readedSignature) .. "\" is not valid"
  169. end
  170. else
  171. return false, "Failed to open file \"" .. tostring(path) .. "\" for reading: " .. tostring(reason)
  172. end
  173. end
  174.  
  175. function module.save(path, picture, encodingMethod)
  176. encodingMethod = encodingMethod or 6
  177.  
  178. local file, reason = io.open(path, "wb")
  179. if file then
  180. if encodingMethods.save[encodingMethod] then
  181. file:write(OCIFSignature, string.char(encodingMethod), string.char(picture[1]), string.char(picture[2]))
  182. encodingMethods.save[encodingMethod](file, picture)
  183.  
  184. file:close()
  185. return true
  186. else
  187. file:close()
  188. return false, "Failed to save file as OCIF image: encoding method \"" .. tostring(encodingMethod) .. "\" is not supported"
  189. end
  190. else
  191. return false, "Failed to open file for writing: " .. tostring(reason)
  192. end
  193. end
  194. --------------------------------------------------------------------------------
  195.  
  196. return module
Add Comment
Please, Sign In to add comment