Advertisement
Guest User

HLTIPaint (Paint2HTLI)

a guest
Oct 23rd, 2016
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.17 KB | None | 0 0
  1.  
  2. --This is the very basic libHLTI API. 2 functions, but they work!
  3. --Since HLTI is actually just many Lua tables, we can use it like this:
  4. local libHLTI = {}
  5. libHLTI.GetValues = function(file)
  6.     if not fs.exists(file) then
  7.         error("HLTI file doesn't exist.")
  8.     end
  9.  
  10.     local file = fs.open(file,'r')
  11.     local values = textutils.unserialize(file.readAll())
  12.     if not values then
  13.         error("HLTI image is misconfigured/corrupted.")
  14.     end
  15.    
  16.     file.close()
  17.     return values
  18. end
  19.  
  20. libHLTI.SetValue = function(file, key, value)
  21.     if not fs.exists(file) then
  22.         error("HLTI file doesn't exist.")
  23.     end
  24.  
  25.     local currentValues = libHLTI.GetValues(file)
  26.     currentValues[key] = value
  27.    
  28.     local file = fs.open(file, 'w')
  29.     file.write(textutils.serialize(currentValues))
  30.     file.close()
  31. end
  32.  
  33. -- Determines if the file exists, and can be edited on this computer (tooken from paint)
  34. local tArgs = {...}
  35. if #tArgs == 0 then
  36.     print("Usage: hltipaint <path> <width> <height>")
  37.     return
  38. end
  39. local sPath = shell.resolve(tArgs[1])
  40. local bReadOnly = fs.isReadOnly(sPath)
  41. if fs.exists(sPath) and fs.isDir(sPath) then
  42.     print("Cannot edit a directory.")
  43.     return
  44. end
  45.  
  46. local canvas = {}
  47.  
  48. ---------------
  49. -- Functions -- tooken from /rom/programs/paint
  50. ---------------
  51.  
  52. local function getCanvasPixel( x, y )
  53.     if canvas[y] then
  54.         return canvas[y][x]
  55.     end
  56.     return nil
  57. end
  58.  
  59. --[[
  60.     Converts a colour value to a text character
  61.     params: colour = the number to convert to a hex value
  62.     returns: a string representing the chosen colour
  63. ]]
  64. local function getCharOf( colour )
  65.     -- Incorrect values always convert to nil
  66.     if type(colour) == "number" then
  67.         local value = math.floor( math.log(colour) / math.log(2) ) + 1
  68.         if value >= 1 and value <= 16 then
  69.             return string.sub( "0123456789abcdef", value, value )
  70.         end
  71.     end
  72.     return " "
  73. end
  74.  
  75. --[[
  76.     Converts a text character to colour value
  77.     params: char = the char (from string.byte) to convert to number
  78.     returns: the colour number of the hex value
  79. ]]
  80. local tColourLookup = {}
  81. for n=1,16 do
  82.     tColourLookup[ string.byte( "0123456789abcdef",n,n ) ] = 2^(n-1)
  83. end
  84. local function getColourOf( char )
  85.     -- Values not in the hex table are transparent (canvas coloured)
  86.     return tColourLookup[char]
  87. end
  88.  
  89. print("Converting...")
  90. os.loadAPI("libHLTI")
  91. local h = fs.open("out.hlti", "w")
  92. h.write("{}")
  93. h.close()
  94.  
  95. local w = tArgs[2]
  96. local h = tArgs[3]
  97. if w == nil then w,h = term.getSize() end
  98.  
  99. libHLTI.SetValue("out.hlti", "HLTIVer", 1)
  100. libHLTI.SetValue("out.hlti", "Width", w)
  101. libHLTI.SetValue("out.hlti", "Height", h)
  102.  
  103. local sectionCount = 0
  104.  
  105. local file = fs.open(sPath, "r")
  106. local sLine = file.readLine()
  107. local y = 1
  108. while sLine do
  109.     for x=1,w do
  110.         term.setCursorPos(1,1)
  111.         print("Section " .. sectionCount .. " | Pixel X" .. x .. " Y" .. y)
  112.         color = getColourOf(string.byte(sLine,x,x))
  113.         if color == colors.transparent or color == 0 then color = colors.black end
  114.         libHLTI.SetValue("out.hlti", "Section" .. sectionCount, {Type="Color", X=x, Y=y, BackgroundColor=color})
  115.         sectionCount = sectionCount + 1
  116.         sleep(0)
  117.     end
  118.     y = y + 1
  119.     sLine = file.readLine()
  120. end
  121. file.close()
  122.  
  123. libHLTI.SetValue("out.hlti", "Sections", sectionCount)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement