Guest User

Paintcan[WIP]

a guest
May 17th, 2016
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.99 KB | None | 0 0
  1. --[[
  2. first letter denotes variable type
  3.  
  4. f = function
  5. t = table
  6. i = number
  7. s = string
  8. b = boolean
  9. v = changing or unknown variable
  10.  
  11. "next, var" is the same as "pairs(var)" but faster
  12. ]]
  13.  
  14. print(" ")
  15. print(" ")
  16. print(" ")
  17. --localization--
  18. local fInsert = table.insert
  19. local fRemove = table.remove
  20. local fConcat = table.concat
  21. local fFloor = math.floor
  22. local fChar = string.char
  23. local fSort = table.sort
  24. local fMax = math.max
  25.  
  26.  
  27. --variables--
  28. local tHex = {[0]="0",[1]="1",[2]="2",[3]="3",[4]="4",[5]="5",[6]="6",[7]="7",[8]="8",[9]="9",[10]="a",[11]="b",[12]="c",[13]="d",[14]="e",[15]="f",[16]=" "}
  29. local tDec = {["0"]=0,["1"]=1,["2"]=2,["3"]=3,["4"]=4,["5"]=5,["6"]=6,["7"]=7,["8"]=8,["9"]=9,["a"]=10,["b"]=11,["c"]=12,["d"]=13,["e"]=14,["f"]=15,[" "]=16}
  30. local tMTFTable = {} --this will be a table of all bytes 0-255
  31.  
  32.  
  33. --basic functions--
  34.  
  35. local function fSetBits(tData, sCurrBit, sThisBit, bInternal)--converts frequency tree to bits for huffman
  36.     local tSet
  37.  
  38.     sCurrBit = sCurrBit or ""
  39.     sThisBit = sThisBit or "0"
  40.    
  41.     local tSolution = {}
  42.     if type(tData.contains)=="table"    then
  43.         tSet = fSetBits(tData.contains[1],sCurrBit..(bInternal and sThisBit or ""),1,true)
  44.         for k,v in next,tSet  do
  45.             tSolution[k] = v
  46.         end
  47.         tSet = fSetBits(tData.contains[2],sCurrBit..(bInternal and sThisBit or ""),0,true)
  48.         for k,v in next,tSet  do
  49.             tSolution[k] = v
  50.         end
  51.     else
  52.         tSolution[tData.contains]=sCurrBit..sThisBit
  53.     end
  54.     return tSolution
  55. end
  56.  
  57. local function fTblHexToDec(tData) --converts all hex characters to decimal numbers
  58.     for k,v in ipairs(tData) do
  59.         tData[k] = tDec[v]
  60.     end
  61.     return tData
  62. end
  63.  
  64. local function fTblCharToDec(tData) --converts all text to decimal in a table
  65.     for k,v in next, tData do
  66.         tData[k] = v:byte()
  67.     end
  68.     return tData
  69. end
  70.  
  71. --next two reverse the above two
  72. local function fTblDecToHex(tData)
  73.     for k,v in next, tData do
  74.         tData[k] = tHex[v]
  75.     end
  76.     return tData
  77. end
  78.  
  79. local function fTblDecToChar(tData)
  80.     for k,v in ipairs(tData) do
  81.         tData[k] = fChar(v)
  82.     end
  83.     return tData
  84. end
  85.  
  86. local function fShallowCopy(tData) --makes a shallow copy of a table changing pointers
  87.     local tOutput = {}
  88.     for k,v in ipairs(tData) do
  89.         tOutput[k] = v
  90.     end
  91.     return tOutput
  92. end
  93.  
  94. local function fIsEqual(tA,tB) --checks if tables contain the same data
  95.     for i=1,#tA do
  96.         if tA[i]~=tB[i] then
  97.             return false
  98.         end
  99.     end
  100.     return #tA==#tB
  101. end
  102.  
  103. local function fLexTblSort(tA,tB) --sorter for tables
  104.     for i=1,#tA do
  105.         if tA[i]~=tB[i] then
  106.             return tA[i]<tB[i]
  107.         end
  108.     end
  109.     return false
  110. end
  111.  
  112. local function fDecToBin(iNum)--convert base 10 to base 2
  113.     if iNum==0 then
  114.         return "00000000" --0 needs a special handle
  115.     end
  116.     local tBinary = {}
  117.     while iNum > 0 do
  118.         fInsert(tBinary,1,iNum%2)
  119.         iNum=fFloor(iNum/2)
  120.     end
  121.     return (#tBinary%8>0 and ("0"):rep(8-#tBinary%8) or "")..fConcat(tBinary)
  122. end
  123.  
  124.  
  125. --BWT functions--
  126. local function fBWT(tData)
  127.  
  128.     --setup--
  129.     local iSize = #tData
  130.     local tSolution = {}
  131.     local tSolved = {}
  132.    
  133.    
  134.     --key table--
  135.     for n=1,iSize do
  136.         tData[iSize] = fRemove(tData,1)
  137.         tSolution[n] = fShallowCopy(tData)
  138.     end
  139.     table.sort(tSolution,fLexTblSort)
  140.    
  141.    
  142.     --encode output--
  143.     for i=1,iSize do
  144.         tSolved[i] = tSolution[i][iSize]
  145.     end
  146.    
  147.    
  148.     --finalize--
  149.     for i=1,iSize do
  150.         if fIsEqual(tSolution[i],tData) then
  151.             return i,tSolved
  152.         end
  153.     end
  154.     return false
  155. end
  156.  
  157. local function fUnBWT(iPointer,tData)
  158.  
  159.     --setup--
  160.     local tSolution = {}
  161.     local iLen = #tData
  162.    
  163.    
  164.     --decode--
  165.     for _=1,iLen do
  166.         for i=1,iLen do
  167.             if not tSolution[i] then
  168.                 tSolution[i] = {} --prevent an error
  169.             end
  170.             fInsert(tSolution[i],1,tData[i]) --insert each entry into each column
  171.         end
  172.         table.sort(tSolution,fLexTblSort) --sort after each set of adds
  173.     end
  174.    
  175.    
  176.     --finalize--
  177.     return tSolution[iPointer]
  178. end
  179.  
  180.  
  181. --MTF functions--
  182. local function fFlushMTF() --reset the MTF table for handling
  183.     for i=0,255 do
  184.         tMTFTable[i+1] = i
  185.     end
  186. end
  187.  
  188. local function fMTF(tData)
  189.  
  190.     --setup--
  191.     local tSolution = {}
  192.     fFlushMTF()
  193.    
  194.    
  195.     --encode--
  196.     for _,iNum in next,tData do
  197.         for i=1,256 do
  198.             if tMTFTable[i]==iNum then --if bits are the same
  199.                 fInsert(tMTFTable,1,fRemove(tMTFTable,i)) --move bit to front
  200.                 fInsert(tSolution,i) --insert bit position
  201.                 break --next entry
  202.             end
  203.         end
  204.     end
  205.    
  206.    
  207.     --finalize--
  208.     return tSolution
  209. end
  210.  
  211. local function fUnMTF(tData)
  212.  
  213.     --setup--
  214.     local tSolution = {}
  215.     fFlushMTF()
  216.    
  217.    
  218.     --decode--
  219.     print(tMTFTable[1])
  220.     print(tMTFTable[2])
  221.     print(tMTFTable[3])
  222.     print(tMTFTable[4])
  223.     for _,iNum in next,tData do
  224.         fInsert(tMTFTable,1,fRemove(tMTFTable,iNum)) --move bit to front
  225.         fInsert(tSolution,tMTFTable[1]) --record bit
  226.     end
  227.    
  228.    
  229.     --finalize--
  230.     return tSolution
  231. end
  232.  
  233.  
  234. --String RLE functions--
  235. local function fSRLE(tData)
  236.    
  237.     --setup--
  238.     local tSolution = {}
  239.    
  240.     --encode--
  241.     while #tData>0 do
  242.         fInsert(tSolution,tData[1])
  243.         if fRemove(tData,1) == tData[1] then--if theres a run
  244.             local iEnc = tData[1]
  245.             local iCount = 1
  246.             while tData[1]==iEnc and iCount<255 do --count how long the run is
  247.                 fRemove(tData,1)
  248.                 iCount = iCount + 1
  249.             end
  250.             fInsert(tSolution,#tSolution,iCount)
  251.             fInsert(tSolution,#tSolution,iCount) --insert count identifier
  252.         end
  253.     end
  254.    
  255.    
  256.     --finalize--
  257.     return tSolution
  258. end
  259.  
  260. local function fUnSRLE(tData)
  261.  
  262.     --setup--
  263.     local tSolution = {}
  264.    
  265.    
  266.     --decode--
  267.     while #tData>0 do
  268.         if tData[1]==tData[2] then --we found a counter so now add it
  269.             fRemove(tData,1)
  270.             local iCount = fRemove(tData,1)
  271.             local iChar = fRemove(tData,1)
  272.             for _=1,iCount do
  273.                 fInsert(tSolution,iChar)
  274.             end
  275.         else
  276.             fInsert(tSolution,fRemove(tData,1))
  277.         end
  278.     end
  279.    
  280.    
  281.     --finalize--
  282.     return tSolution
  283. end
  284.  
  285.  
  286. --Color RLE functions--
  287. local function fCRLE(tData)
  288.  
  289.     --setup--
  290.     local tSolution = {}
  291.     local sInter = ""
  292.     local iLast = -1
  293.    
  294.     --encode--
  295.     for _,iNum in next, tData do
  296.         if iNum==iLast then
  297.             sInter=sInter.."0"
  298.         else
  299.             if iNum==16 then
  300.                 sInter = sInter.."11"
  301.             else
  302.                 sInter=sInter.."1"..fDecToBin(iNum):sub(4,8)
  303.             end
  304.         end
  305.         iLast = iNum
  306.     end
  307.     sInter = sInter:sub(2,#sInter)
  308.     if #sInter%8~=0 then
  309.         sInter = sInter..("1"):rep(8-#sInter%8)
  310.     end
  311.     while #sInter>=8 do
  312.         fInsert(tSolution,tonumber(sInter:sub(1,8),2))
  313.         sInter = sInter:sub(9,#sInter)
  314.     end
  315.    
  316.    
  317.     --finalize--
  318.     return tSolution
  319. end
  320.  
  321. local function fUnCRLE(iLength, tData)
  322.    
  323.     --setup--
  324.     local sInter = ""
  325.     local tSolution = {}
  326.    
  327.     for _,v in next,tData do
  328.         sInter = sInter..fDecToBin(v)
  329.     end
  330.     tSolution[1] = tonumber(sInter:sub(1,5),2)
  331.     iLength = iLength - 1
  332.     sInter = sInter:sub(6,#sInter)
  333.    
  334.     --decode--
  335.     while iLength>0 do
  336.         iLength = iLength - 1
  337.         if sInter:sub(1,1)=="0" then
  338.             fInsert(tSolution,tSolution[#tSolution])
  339.             sInter = sInter:sub(2,#sInter)
  340.         else
  341.             if sInter:sub(2,2)=="1" then
  342.                 fInsert(tSolution,16)
  343.                 sInter = sInter:sub(3,#sInter)
  344.             else
  345.                 fInsert(tSolution,tonumber(sInter:sub(2,6),2))
  346.                 sInter = sInter:sub(7,#sInter)
  347.             end
  348.         end
  349.     end
  350.    
  351.     --finalize--
  352.     return tSolution
  353. end
  354.  
  355. --Huffman functions--
  356. local function fHuffman(tData)
  357.  
  358.     --setup--
  359.     local tFreq = {}
  360.     local tTree = {}
  361.     local tKey = {}
  362.     local sInter = ""
  363.     local tSolution = {}
  364.     local iMaxSize, iCount = 0, 0
  365.    
  366.    
  367.     --key table--
  368.     for _,v in next, tData do
  369.         tFreq[v] = tFreq[v] and tFreq[v]+1 or 1
  370.     end
  371.     for k,v in next,tFreq do
  372.         iCount = iCount + 1
  373.         fInsert(tTree,{freq=v,contains=k,depth=0})
  374.     end
  375.     while #tTree>1 do
  376.         fSort(tTree, function(a,b)
  377.             return a.freq==b.freq and a.depth<b.depth or a.freq<b.freq
  378.         end)
  379.         fInsert(tTree,{depth=fMax(tTree[1].depth,tTree[2].depth)+1,freq=tTree[1].freq+tTree[2].freq,contains={tTree[1],tTree[2]}})
  380.         fRemove(tTree,1)
  381.         fRemove(tTree,1)
  382.     end
  383.     tKey = fSetBits(tTree[1])
  384.    
  385.    
  386.     --encode--
  387.     sInter = fDecToBin(iCount)
  388.     for k,v in next, tKey do
  389.         sInter = sInter..fDecToBin(#v)
  390.     end
  391.     for k,v in next, tKey do
  392.         sInter = sInter..fDecToBin(k)..v
  393.     end
  394.     for _,v in next, tData do
  395.         sInter = sInter..tKey[v]
  396.     end
  397.     sInter = ("0"):rep(8 - ((#sInter%8)+1)).."1"..sInter
  398.     while #sInter>=8 do
  399.         fInsert(tSolution,tonumber(sInter:sub(1,8),2))
  400.         sInter = sInter:sub(9,#sInter)
  401.     end
  402.    
  403.     --finalize--
  404.     return tSolution
  405. end
  406.  
  407. local function fUnHuffman(tData)
  408.    
  409.     --setup--
  410.     local sInter = ""
  411.     local sBuild = ""
  412.     local tSolution = {}
  413.     local tKeys = {}
  414.     local iCount
  415.    
  416.    
  417.     --key table--
  418.     for k,v in next, tData do
  419.         sInter = sInter..fDecToBin(v)
  420.     end
  421.     while sInter:sub(1,1)=="0" do
  422.         sInter = sInter:sub(2,#sInter)
  423.     end
  424.     iCount = tonumber(sInter:sub(2,9),2)
  425.     sInter = sInter:sub(10,#sInter)
  426.     for i=1,iCount do
  427.         tKeys[i] = tonumber(sInter:sub(1,8),2)
  428.         sInter = sInter:sub(9,#sInter)
  429.     end
  430.     for i=1,iCount do
  431.         tKeys[sInter:sub(9,8+tKeys[i])] = tonumber(sInter:sub(1,8),2)
  432.         sInter = sInter:sub(tKeys[i]+9,#sInter)
  433.         tKeys[i] = nil
  434.     end
  435.    
  436.    
  437.     --decode--
  438.     while #sInter>0 do
  439.         sBuild = sBuild..sInter:sub(1,1)
  440.         if tKeys[sBuild] then
  441.             fInsert(tSolution,tKeys[sBuild])
  442.             sBuild = ""
  443.         end
  444.         sInter = sInter:sub(2,#sInter)
  445.     end
  446.    
  447.    
  448.     --finalize--
  449.     return tSolution
  450. end
  451.  
  452. --paintcan header functions--
  453. function fSetHeader(tData,iLength,iWidth,bAnim,tCutPoints,iFrames)
  454.    
  455.     --setup--
  456.     local sInter
  457.     local sHeader = fDecToBin(iLength)
  458.    
  459.    
  460.     --encode--
  461.     sHeader = (bAnim and "1" or "0")..fDecToBin(#sHeader/8):sub(6,8)..fDecToBin(#fDecToBin(iWidth)/2):sub(4,8)..sHeader..fDecToBin(iWidth)
  462.     if bAnim then
  463.         sInter = fDecToBin(iFrames)
  464.         sHeader = sHeader..fDecToBin(#sInter/8)..sInter
  465.     end
  466.     for _,v in next,tCutPoints do
  467.         sInter = fDecToBin(v[1])
  468.         sHeader = sHeader..fDecToBin(#sInter/8)..sInter
  469.         sInter = fDecToBin(v[2])
  470.         sHeader = sHeader..(iLength>255 and fDecToBin(#sInter/8) or "")..sInter
  471.     end
  472.     for i=#sHeader,8,-8 do
  473.         fInsert(tData,1,tonumber(sHeader:sub(i-7,i),2))
  474.     end
  475.    
  476.    
  477.     --finalize--
  478.     return tData
  479. end
  480.  
  481. function fGetHeader(tData)
  482.  
  483.     --setup--
  484.     local sBytes
  485.     local tCutPoints = {}
  486.     local iLength,iWidth,bAnim,iFrames
  487.    
  488.    
  489.     --decode--
  490.     sBytes = fDecToBin(fRemove(tData,1))
  491.     bAnim = sBytes:sub(1,1)=="1"
  492.     iLength = tonumber(sBytes:sub(2,4),2)
  493.     iWidth = tonumber(sBytes:sub(5,8),2)
  494.     sBytes = ""
  495.     for i=1,iLength do
  496.         sBytes = sBytes..fDecToBin(fRemove(tData,1))
  497.     end
  498.     iLength = tonumber(sBytes,2)
  499.     sBytes = ""
  500.     for i=1,iWidth do
  501.         sBytes = sBytes..fDecToBin(fRemove(tData,1))
  502.     end
  503.     iWidth = tonumber(sBytes,2)
  504.     if bAnim then
  505.         local cut = fDecToBin(fRemove(tData,1))
  506.         iFrames = tonumber(cut,2)
  507.         sBytes = ""
  508.         for i=1,iFrames do
  509.             sBytes = sBytes..fDecToBin(fRemove(tData,1))
  510.         end
  511.         iFrames = tonumber(sBytes,2)
  512.     end
  513.     for i=1,3+(bAnim and (iFrames-1)*3 or 0) do
  514.         fInsert(tCutPoints,{})
  515.         local cut=fDecToBin(fRemove(tData,1))
  516.         tCutPoints[#tCutPoints][1] = tonumber(cut,2)
  517.         sBytes = ""
  518.         for i=1,tCutPoints[#tCutPoints][1] do
  519.             sBytes = sBytes..fDecToBin(fRemove(tData,1))
  520.         end
  521.         tCutPoints[#tCutPoints][1] = tonumber(sBytes,2)
  522.        
  523.         if iLength>255 then
  524.             tCutPoints[#tCutPoints][2] = tonumber(fDecToBin(fRemove(tData,1)),2)
  525.             sBytes = ""
  526.             for i=1,tCutPoints[#tCutPoints][2] do
  527.                 sBytes = sBytes..fDecToBin(fRemove(tData,1))
  528.             end
  529.             tCutPoints[#tCutPoints][2] = tonumber(sBytes,2)
  530.         else
  531.             local cut=fDecToBin(fRemove(tData,1))
  532.             tCutPoints[#tCutPoints][2] = tonumber(cut,2)
  533.         end
  534.     end
  535.    
  536.     --finalize--
  537.     return tData,iLength,iWidth,bAnim,tCutPoints,iFrames
  538. end
  539.  
  540.  
  541. --API functions--
  542. function encode(bAnim,...)
  543.  
  544.     --setup--
  545.     local tInter = {}
  546.     local tHuff = {}
  547.     local tArg = {}
  548.     local tCutPoints = {}
  549.     local iLength, iWidth, iCut
  550.     local sOutput = ""
  551.    
  552.    
  553.     --encode--
  554.     if not bAnim then
  555.         tArg = {...}
  556.     else
  557.         for _,v in next,{...} do
  558.             fInsert(tArg,v[1])
  559.             fInsert(tArg,v[2])
  560.             fInsert(tArg,v[3])
  561.         end
  562.     end
  563.     iLength = #tArg[1]
  564.     iWidth = #tArg[1][1]
  565.     for k,v in next, tArg do
  566.         tInter = {}
  567.         for x,y in next,v do
  568.             for i,t in next, y do
  569.                 fInsert(tInter,t)
  570.             end
  571.         end
  572.         tArg[k] = tInter
  573.     end
  574.     tInter = {}
  575.     for k,v in next, tArg do
  576.         fInsert(tCutPoints,{})
  577.         v = fShallowCopy(v)
  578.         if k%3==0 then
  579.             v = fTblCharToDec(v)
  580.             v = fMTF(v)
  581.             iCut, v = fBWT(v)
  582.             tCutPoints[k][2] = iCut
  583.             v = fSRLE(v)
  584.         else
  585.             v = fTblHexToDec(v)
  586.             iCut, v = fBWT(v)
  587.             tCutPoints[k][2] = iCut
  588.             v = fCRLE(v)
  589.         end
  590.         for x,y in next, v do
  591.             fInsert(tInter,y)
  592.         end
  593.         tCutPoints[k][1] = #tInter
  594.     end
  595.     tInter = fSetHeader(tInter,iLength,iWidth,bAnim,tCutPoints,#({...}))
  596.     tInter = fSRLE(tInter)
  597.     tInter = fMTF(tInter)
  598.     tHuff = fHuffman(tInter)
  599.     if #tHuff<#tInter then
  600.         tInter = tHuff
  601.         fInsert(tInter,1)
  602.     else
  603.         fInsert(tInter,0)
  604.     end
  605.     tInter = fTblDecToChar(tInter)
  606.    
  607.     for k,v in next, tInter do
  608.         sOutput = sOutput..v
  609.     end
  610.    
  611.    
  612.     --finalize--
  613.     return sOutput
  614. end
  615.  
  616. function decode(sData)
  617.    
  618.     --setup--
  619.     local tOutput = {}
  620.     local tFrames = {}
  621.     local iLength,bAnim,tCutPoints,iFrames
  622.    
  623.    
  624.     --decode--
  625.     for i=1,#sData do
  626.         tOutput[i] = sData:sub(1,1)
  627.     end
  628.     tOutput = fTblCharToDec(tOutput)
  629.     tOutput = fRemove(tOutput,1)==1 and tOutput or fUnHuffman(tOutput)
  630.     tOutput = fUnMTF(tOutput)
  631.     tOutput = fUnSRLE(tOutput)
  632.     tOutput,iLength,bAnim,tCutPoints,iFrames = fGetHeader(tOutput)
  633.    
  634. end
Advertisement
Add Comment
Please, Sign In to add comment