LDDestroier

NFT Extras (beta)

Dec 13th, 2017
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.28 KB | None | 0 0
  1. --[[
  2.  nft extra functions
  3.  pastebin get J11BaaDy nfte
  4.  
  5. list of extra functions:
  6.  nft.flipY(table image)
  7.     returns 'image' stretched vertically
  8.  nft.flipX(table image)
  9.     returns 'image' stretched horizontally
  10.  nft.grayOut(table image)
  11.     converts 'image' to grayscale
  12.  nft.lighten(table image)
  13.     returns 'image' but with brighter colors, suitable for transitions
  14.  nft.darken(table image)
  15.     returns 'image' but with darker colors, suitable for transitions
  16.  nft.getSize(table image)
  17.     returns the x and y sizes of 'image'
  18.  nft.stretchImage(table image, number stretchedX, number stretchedY)
  19.     returns 'image', but stretched to fit sizes 'stretchedX' and 'stretchedY'.
  20.  nft.merge(table {image1,x,y}, table {image2,x,y}, ...)
  21.     returns an image composed of image1 layered upon image2 layered upon image3 and so on.
  22.  nft.fullScreen(number color, terminal object)
  23.     returns a fullscreen picture made solidly of 'color'. sets size to current terminal or 'object' if defined.
  24.  nft.unloadImage(table image)
  25.     returns a string NFP picture made from 'image'.
  26.  nft.autocrop(table image)
  27.     returns 'image' but with the blank space cut off from the left and top.
  28.  nft.drawImageBlit(table image, number x, number y)
  29.     draws 'image' at 'x' and 'y' using term.blit. As such, it does not support transparency. You will want to merge images together before rendering.
  30.  nft.drawImageBlitCenter(table image, number x, number y, terminal object)
  31.     draws 'image' centered around 'x' and 'y'. if 'x' and 'y' aren't defined, they default to half the screen width and height respectively. 'object' can be defined to use its screen size instead.
  32.  nft.centerWithBlankSpace(table image, number x, number y, terminal object)
  33.     returns 'image', but with extra blank space to center it to 'x' and 'y'. if 'x' and 'y' aren't defined, they default to half the screen width and height respectively. 'object' can be defined to use its screen size instead.
  34. --]]
  35.  
  36. local explode = function(div,str)
  37.     if (div=='') then return false end
  38.     local pos,arr = 0,{}
  39.     for st,sp in function() return string.find(str,div,pos,true) end do
  40.         arr[#arr+1] = str:sub(pos,st-1)
  41.         pos = sp + 1
  42.     end
  43.     arr[#arr+1] = str:sub(pos)
  44.     return arr
  45. end
  46. local blitcolors = {
  47.     [" "] = allowZero and 0 or nil,
  48.     ["0"] = colors.white,
  49.     ["1"] = colors.orange,
  50.     ["2"] = colors.magenta,
  51.     ["3"] = colors.lightBlue,
  52.     ["4"] = colors.yellow,
  53.     ["5"] = colors.lime,
  54.     ["6"] = colors.pink,
  55.     ["7"] = colors.gray,
  56.     ["8"] = colors.lightGray,
  57.     ["9"] = colors.cyan,
  58.     ["a"] = colors.purple,
  59.     ["b"] = colors.blue,
  60.     ["c"] = colors.brown,
  61.     ["d"] = colors.green,
  62.     ["e"] = colors.red,
  63.     ["f"] = colors.black,
  64. }
  65. local BTC = function(_color,allowZero) --Blit To Color
  66.     if _color == nil then return nil end
  67.     return blitcolors[_color]
  68. end
  69.  
  70. local importFromNFT = function(input)
  71.     local tinput = explode("\n",input)
  72.     local tcol,bcol
  73.     local cx --represents the x position in the picture
  74.     local sx --represents the x position in the file
  75.     local charoutput,txtoutput,bgoutput = {}
  76.     for y = 1, #tinput do
  77.         charoutput[y], txtoutput[y], bgoutput[y] = {}, {}, {}
  78.         tcol,bcol = colors.white,colors.black
  79.         cx, sx = 1, 0
  80.         while sx < #tinput[y] do
  81.             sx = sx + 1
  82.             if tinput[y]:sub(sx,sx) == "\30" then
  83.                 bcol = BTC(tinput[y]:sub(sx+1,sx+1))
  84.                 sx = sx + 1
  85.             elseif tinput[y]:sub(sx,sx) == "\31" then
  86.                 tcol = BTC(tinput[y]:sub(sx+1,sx+1))
  87.                 sx = sx + 1
  88.             else
  89.                 if tcol and bcol then
  90.                     charoutput[y][cx] = tinput[y]:sub(sx,sx)
  91.                     txtoutput[y][cx] = tcol
  92.                     bgoutput[y][cx] = bcol
  93.                 end
  94.                 cx = cx + 1
  95.             end
  96.         end
  97.     end
  98.     return charoutput, txtoutput, bgoutput
  99. end
  100.  
  101. local exportToNFT = function(charinput, txtinput, bginput)
  102.     local input = {}
  103.     for y = 1, #bginput do
  104.         for x = 1, #bginput[y] do
  105.             input[#input+1] = {
  106.                 x = x,
  107.                 y = y,
  108.                 b = bginput[y][x],
  109.                 t = txtinput[y][x],
  110.                 c = charinput[y][x],
  111.             }
  112.         end
  113.     end
  114.     local doop = {}
  115.     local p = input
  116.     local pheight = 0
  117.     local pwidth = 0
  118.     local nftoutput = "\30 \31f"
  119.     for a = 1, #p do
  120.         if p[a].y > pheight then
  121.             pheight = p[a].y
  122.         end
  123.         if p[a].x > pwidth then
  124.             pwidth = p[a].x
  125.         end
  126.     end
  127.     for k,v in pairs(p) do
  128.         if not doop[v.y] then doop[v.y] = {} end
  129.         doop[v.y][v.x] = {}
  130.         doop[v.y][v.x]["char"] = v.c
  131.         doop[v.y][v.x]["text"] = CTB(v.t) or " "
  132.         doop[v.y][v.x]["back"] = CTB(v.b) or " "
  133.     end
  134.     for y = 1, pheight do
  135.         if doop[y] then
  136.             for x = 1, pwidth do
  137.                 local nWriteBackground
  138.                 local nWriteColor
  139.                 if doop[y][x] then
  140.                     if doop[y][x]["back"] ~= nWriteBackground then
  141.                         nWriteBackground = b
  142.                         nftoutput = nftoutput.."\30"..doop[y][x]["back"]
  143.                     end
  144.                     if doop[y][x]["text"] ~= nWriteColor then
  145.                         nWriteColor = c
  146.                         nftoutput = nftoutput.."\31"..doop[y][x]["text"]
  147.                     end
  148.                     nftoutput = nftoutput..doop[y][x]["char"]
  149.                     if (not doop[y][x+1]) and x ~= pwidth then
  150.                         nftoutput = nftoutput.."\30".." "
  151.                     end
  152.                 else
  153.                     nftoutput = nftoutput.." "
  154.                 end
  155.             end
  156.         end
  157.         if y ~= pheight then
  158.             nftoutput = nftoutput.."\n\30 \31f"
  159.         end
  160.     end
  161.     return nftoutput
  162. end
  163.  
  164. local round = function(x) return x + 0.5 - (x + 0.5) % 1 end
  165. local deepCopy = function(tbl) return {table.unpack(tbl)} end
  166. local bcol, ccol = {
  167. [0]=" ",[colors.white]="0",[colors.orange]="1",[colors.magenta]="2",[colors.lightBlue]="3",[colors.yellow]="4",[colors.lime]="5",[colors.pink]="6",[colors.gray]="7",[colors.lightGray]="8",[colors.cyan]="9",[colors.purple]="a",[colors.blue]="b",[colors.brown]="c",[colors.green]="d",[colors.red]="e",[colors.black]="f"}, {}
  168. for k,v in pairs(bcol) do ccol[v] = k end
  169. local flipY = function(image)
  170.     local output = {}
  171.     for y = #image, 1, -1 do output[#output+1] = image[y] end
  172.     return output
  173. end
  174. nft.flipY = funtion(image)
  175.     local c,t,b = importFromNFT(image)
  176.     return exportToNFT(flipY(c),flipY(t),flipY(b))
  177. end
  178.  
  179. local flipX = function(image)
  180.     local output,sizeX = {}, 0
  181.     for y = 1, #image do sizeX = math.max(sizeX,#image[y]) end
  182.     for y = 1, #image do
  183.         output[y] = {}
  184.         for x = 1, sizeX do output[y][x] = deepCopy(image)[y][sizeX-(x-1)] or 0 end
  185.     end
  186.     return output
  187. end
  188. nft.flipX = function(image)
  189.     local c,t,b = importFromNFT(image)
  190.     return exportToNFT(flipX(c),flipX(t),flipX(b))
  191. end
  192.  
  193. local grayOut = function(image)
  194.     local output,grays = {},{[0] = 0,[colors.white]=colors.white,[colors.orange]=colors.lightGray,[colors.magenta]=colors.lightGray,[colors.lightBlue]=colors.lightGray,[colors.yellow]=colors.white,[colors.lime]=colors.lightGray,[colors.pink]=colors.lightGray,[colors.gray]=colors.gray,[colors.lightGray]=colors.lightGray,[colors.cyan]=colors.lightGray,[colors.purple]=colors.gray,[colors.blue]=colors.gray,[colors.brown]=colors.gray,[colors.green]=colors.lightGray,[colors.red]=colors.gray,[colors.black]=colors.black}
  195.     for y = 1, #image do
  196.         output[y] = {}
  197.         for x = 1, #image[y] do output[y][x] = grays[image[y][x]] or image[y][x] end
  198.     end
  199.     return output
  200. end
  201. nft.grayOut = function(image)
  202.     local c,t,b = importFromNFT(image)
  203.     return exportToNFT(grayOut(c),grayOut(t),grayOut(b))
  204. end
  205.  
  206. local lighten = function(image)
  207.     local output,lighters = {},{[0] = 0,[colors.white] = colors.white,[colors.orange] = colors.yellow,[colors.magenta] = colors.pink,[colors.lightBlue] = colors.white,[colors.yellow] = colors.white,[colors.lime] = colors.white,[colors.pink] = colors.white,[colors.gray] = colors.lightGray,[colors.lightGray] = colors.white,[colors.cyan] = colors.lightBlue,[colors.purple] = colors.magenta,[colors.blue] = colors.lightBlue,[colors.brown] = colors.orange,[colors.green] = colors.lime,[colors.red] = colors.magenta,[colors.black] = colors.gray}
  208.     for y = 1, #image do
  209.         output[y] = {}
  210.         for x = 1, #image[y] do output[y][x] = lighters[image[y][x]] or image[y][x] end
  211.     end
  212.     return output
  213. end
  214. nft.lighten = function(image)
  215.     local c,t,b = importFromNFT(image)
  216.     return exportToNFT(lighten(c),lighten(t),lighten(b))
  217. end
  218.  
  219. local darken = function(image)
  220.     local output, darkers = {},{[0]=0,[colors.white]=colors.lightGray,[colors.orange]=colors.brown,[colors.magenta]=colors.purple,[colors.lightBlue]=colors.cyan,[colors.yellow]=colors.orange,[colors.lime]=colors.green,[colors.pink]=colors.magenta,[colors.gray]=colors.black,[colors.lightGray]=colors.gray,[colors.cyan]=colors.blue,[colors.purple]=colors.gray,[colors.blue]=colors.gray,[colors.brown]=colors.black,[colors.green]=colors.gray,[colors.red]=colors.gray,[colors.black]=colors.black}
  221.     for y = 1, #image do
  222.         output[y] = {}
  223.         for x = 1, #image[y] do output[y][x] = darkers[image[y][x]] or image[y][x] end
  224.     end
  225.     return output
  226. end
  227. nft.darken = function(image)
  228.     local c,t,b = importFromNFT(image)
  229.     return exportToNFT(darken(c),darken(t),darken(b))
  230. end
  231.  
  232. nft.getSize = function(nimage)
  233.     local image = importFromNFT(nimage)
  234.     local xsize = 0
  235.     if type(image) ~= "table" then return 0,0 end
  236.     for y = 1, #image do xsize = math.max(xsize,#image[y]) end
  237.     return xsize, #image
  238. end
  239.  
  240. local stretchImage = function(_image,sx,sy)
  241.     local image,output = deepCopy(_image), {}
  242.     if sx == 0 or sy == 0 then return {{}} end
  243.     if sx < 0 then image = nft.flipX(image) end
  244.     if sy < 0 then image = nft.flipY(image) end
  245.     sx,sy = round(math.abs(sx)), round(math.abs(sy))
  246.     if sx == 0 or sy == 0 then return {{}} end
  247.     local imX,imY = nft.getSize(image)
  248.     local xcur,ycur
  249.     for y = 1, sy do
  250.         output[y] = {}
  251.         for x = 1, sx do
  252.             output[y][x] = image[math.max(1,round((y/sy)*imY))][math.max(1,round((x/sx)*imX))] or nil
  253.         end
  254.     end
  255.     return output
  256. end
  257. nft.stretchImage = function(image,sx,sy)
  258.     local c,t,b = importFromNFT(image)
  259.     return exportToNFT(stretchImage(c,sx,sy),stretchImage(t,sx,sy),stretchImage(b,sx,sy))
  260. end
  261.  
  262. nft.drawImageBlit = function(image,ix,iy)
  263.     if (type(ix) ~= "number") or (type(iy) ~= "number") then
  264.         error("Expected image, x, y", 2)
  265.     else
  266.         local c,t,b = importFromNFT(image)
  267.         ix, iy = round(ix), round(iy)
  268.         local buffc,bufft,buffb
  269.         for y = 1, #image do
  270.             buffc,bufft,buffb = "","",""
  271.             for x = 1, #image[y] do
  272.                 buffc = buffc..bcol[c[y][x]]
  273.                 bufft = bufft..bcol[t[y][x]]
  274.                 buffb = buffb..bcol[b[y][x]]
  275.             end
  276.             term.setCursorPos(ix,iy+(y-1))
  277.             term.blit(buffc,bufft,buffb)
  278.         end
  279.     end
  280. end
  281.  
  282. local centerWithBlankSpace = function(_image,ix,iy,object)
  283.     local image,scX,scY = deepCopy(_image)
  284.     if object then scX,scY = object.getSize() else scX,scY = term.getSize() end
  285.     local imgXsize,imgYsize = paintutils.getSize(image)
  286.     if imgXsize == 0 or imgYsize == 0 then return {{}} end
  287.     local incX,incY = math.floor((ix or (scX/2)) - (imgXsize/2)), math.floor((iy or (scY/2)) - (imgYsize/2))
  288.     local output = {}
  289.     for y = 1, imgYsize + incY do
  290.         output[y] = {}
  291.         for x = 1, imgXsize + incX do
  292.             if (x > incX) and (y > incY) then
  293.                 output[y][x] = image[y-incY][x-incX] or 0
  294.             else
  295.                 output[y][x] = 0
  296.             end
  297.         end
  298.     end
  299.     return output
  300. end
  301. nft.centerWithBlankSpace = function(image,ix,iy,object)
  302.     local c,t,b = importFromNFT(image)
  303.     return exportToNFT(centerWithBlankSpace(c,ix,iy,object),centerWithBlankSpace(t,ix,iy,object),centerWithBlankSpace(b,ix,iy,object))
  304. end
  305.  
  306. nft.drawImageBlitCenter = function(image,ix,iy,object)
  307.     local scX,scY
  308.     if object then scX,scY = object.getSize() else scX,scY = term.getSize() end
  309.     local imgXsize,imgYsize = nft.getSize(image)
  310.     return nft.drawImageBlit(image, (ix or (scX/2)) - (imgXsize/2), (iy or (scY/2)) - (imgYsize/2))
  311. end
  312.  
  313. local merge = function(...)
  314.     local output,arg,imgXsize,imgYsize,image,xdiff,ydiff = {}, table.pack(...), 0, 0
  315.     for a = 1, #arg do
  316.         local x,y = nft.getSize(arg[a][1])
  317.         if not (x == 0 or y == 0) then
  318.             x, y = x+arg[a][2], y+arg[a][3]
  319.             imgXsize = math.max(imgXsize,x)
  320.             imgYsize = math.max(imgYsize,y)
  321.         end
  322.     end
  323.     for a = #arg, 1, -1 do
  324.         image,xdiff,ydiff = table.unpack(arg[a])
  325.         xdiff, ydiff = xdiff - 1, ydiff - 1
  326.         for y = 1, imgYsize do
  327.             output[y] = output[y] or {}
  328.             for x = 1, imgXsize do
  329.                 output[y][x] = output[y][x] or 0
  330.                 if image[y-ydiff] then
  331.                     if image[y-ydiff][x-xdiff] and (image[y-ydiff][x-xdiff] ~= 0) then
  332.                         output[y][x] = image[y-ydiff][x-xdiff]
  333.                     end
  334.                 end
  335.             end
  336.         end
  337.     end
  338.     return output
  339. end
  340. nft.merge = function(...)
  341.     --uuhhhh
  342. end
  343. nft.fullScreen = function(bgcol,object)
  344.     local scr_x,scr_y = (object or term).getSize()
  345.     local output,l = {},{}
  346.     bgcol = bgcol or colors.black
  347.     for x = 1, scr_x do l[x] = bgcol end
  348.     for y = 1, scr_y do output[y] = l end
  349.     return output
  350. end
  351. nft.unloadImage = function(image)
  352.     local output = ""
  353.     for y = 1, #image do
  354.         for x = 1, #image[y] do output = output..bcol[image[y][x]] end
  355.         if y < #image then output = output.."\n" end
  356.     end
  357.     return output
  358. end
  359. nft.autocrop = function(image)
  360.     local output,top,leftings = {},1,{}
  361.     local isTopped = false
  362.     for a = 1, #image do
  363.         if (not isTopped) and (#image[a] > 0) then
  364.             top = a
  365.             isTopped = true
  366.         end
  367.         if isTopped then
  368.             output[#output+1] = deepCopy(image[a])
  369.         end
  370.     end
  371.     for y = 1, #output do
  372.         leftings[y] = 0
  373.         for x = 1, #output[y] do
  374.             if (output[y][x] == 0) or (output[y][x] == nil) then
  375.                 leftings[y] = leftings[y] + 1
  376.             else
  377.                 break
  378.             end
  379.         end
  380.     end
  381.     local left = math.min(table.unpack(leftings))
  382.     for y = 1, #output do
  383.         for l = 1, left do table.remove(output[y],1) end
  384.     end
  385.     return output
  386. end
Add Comment
Please, Sign In to add comment