Advertisement
KaychenHH

OCIF.lua

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