Guest User

imgtest

a guest
Jul 19th, 2018
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.99 KB | None | 0 0
  1. _G.package = {}
  2.  
  3. _G.package.cpath = ""
  4. _G.package.loaded = {}
  5. _G.package.loadlib = function() error("NotImplemented: package.loadlib") end
  6. _G.package.path = table.concat({
  7.     "?",
  8.     "?.lua",
  9.     "?/init.lua",
  10.     "/lib/?",
  11.     "/lib/?.lua",
  12.     "/lib/?/init.lua",
  13.     "/rom/apis/?",
  14.     "/rom/apis/?.lua",
  15.     "/rom/apis/?/init.lua",
  16.     "/rom/apis/turtle/?",
  17.     "/rom/apis/turtle/?.lua",
  18.     "/rom/apis/turtle/?/init.lua",
  19.     "/rom/apis/command/?",
  20.     "/rom/apis/command/?.lua",
  21.     "/rom/apis/command/?/init.lua",
  22. }, ";")
  23. _G.package.preload = {}
  24. _G.package.seeall = function(module) error("NotImplemented: package.seeall") end
  25. _G.module = function(m) error("NotImplemented: module") end
  26.  
  27. local _package_path_loader = function(name)
  28.    
  29.     local fname = name:gsub("%.", "/")
  30.    
  31.     for pattern in package.path:gmatch("[^;]+") do
  32.        
  33.         local fpath = pattern:gsub("%?", fname)
  34.        
  35.         if fs.exists(fpath) and not fs.isDir(fpath) then
  36.            
  37.             local apienv = {}
  38.             setmetatable(apienv, {__index = _G})
  39.            
  40.             local apifunc, err = loadfile(fpath)
  41.             local ok
  42.            
  43.             if apifunc then
  44.                 --setfenv(apifunc, apienv)
  45.                 ok, err = pcall(apifunc)
  46.             end
  47.            
  48.             if not apifunc or not ok then
  49.                 error("error loading module '" .. name .. "' from file '" .. fpath .. "'\n\t" .. err)
  50.             end
  51.            
  52.             local api = {}
  53.             if type(err) == "table" then
  54.               api = err
  55.             end
  56.             for k,v in pairs( apienv ) do
  57.                 api[k] = v
  58.             end
  59.            
  60.             return api
  61.         end
  62.     end
  63. end
  64.  
  65. _G.package.loaders = {
  66.     function(name)
  67.         if package.preload[name] then
  68.             return package.preload[name]
  69.         else
  70.             return "\tno field package.preload['" .. name .. "']"
  71.         end
  72.     end,
  73.    
  74.     function(name)
  75.         local _errors = {}
  76.        
  77.         local fname = name:gsub("%.", "/")
  78.        
  79.         for pattern in package.path:gmatch("[^;]+") do
  80.            
  81.             local fpath = pattern:gsub("%?", fname)
  82.             if fs.exists(fpath) and not fs.isDir(fpath) then
  83.                 return _package_path_loader
  84.             else
  85.                 table.insert(_errors, "\tno file '" .. fpath .. "'")
  86.             end
  87.         end
  88.        
  89.         return table.concat(_errors, "\n")
  90.     end
  91. }
  92.  
  93. _G.require = function(name)
  94.     if package.loaded[name] then
  95.         return package.loaded[name]
  96.     end
  97.    
  98.     local _errors = {}
  99.    
  100.     for _, searcher in pairs(package.loaders) do
  101.         local loader = searcher(name)
  102.         if type(loader) == "function" then
  103.             local res = loader(name)
  104.             if res ~= nil then
  105.                 package.loaded[name] = res
  106.             end
  107.            
  108.             if package.loaded[name] == nil then
  109.                 package.loaded[name] = true
  110.             end
  111.            
  112.             return package.loaded[name]
  113.         elseif type(loader) == "string" then
  114.             table.insert(_errors, loader)
  115.         end
  116.     end
  117.    
  118.     error("module '" .. name .. "' not found:\n" .. table.concat(_errors, "\n"))
  119. end
  120.  
  121. local deflate = require("deflatelua")
  122. local requiredDeflateVersion = "0.3.20111128"
  123.  
  124. if (deflate._VERSION ~= requiredDeflateVersion) then
  125.     error("Incorrect deflate version: must be "..requiredDeflateVersion..", not "..deflate._VERSION)
  126. end
  127.  
  128. local function bsRight(num, pow)
  129.     return math.floor(num / 2^pow)
  130. end
  131.  
  132. local function bsLeft(num, pow)
  133.     return math.floor(num * 2^pow)
  134. end
  135.  
  136. local function bytesToNum(bytes)
  137.     local n = 0
  138.     for k,v in ipairs(bytes) do
  139.         n = bsLeft(n, 8) + v
  140.     end
  141.     if (n > 2147483647) then
  142.         return (n - 4294967296)
  143.     else
  144.         return n
  145.     end
  146.     n = (n > 2147483647) and (n - 4294967296) or n
  147.     return n
  148. end
  149.  
  150. local function readByte(stream)
  151.     return stream.read()
  152. end
  153.  
  154. local function readInt(stream, bps)
  155.     local bytes = {}
  156.     bps = bps or 4
  157.     for i=1,bps do
  158.         bytes[i] = readByte(stream)
  159.     end
  160.     return bytesToNum(bytes)
  161. end
  162.  
  163. local function readChar(stream, num)
  164.     num = num or 1
  165.     local bytes = 0
  166.     for i = 0, num - 1, 1 do
  167.         bytes = bytes + (readByte(stream) * (256 ^ i))
  168.     end
  169. end
  170.  
  171. local function getDataIHDR(stream, length)
  172.     local data = {}
  173.     data["width"] = readInt(stream)
  174.     data["height"] = readInt(stream)
  175.     data["bitDepth"] = readByte(stream)
  176.     data["colorType"] = readByte(stream)
  177.     data["compression"] = readByte(stream)
  178.     data["filter"] = readByte(stream)
  179.     data["interlace"] = readByte(stream)
  180.     return data
  181. end
  182.  
  183. local function getDataIDAT(stream, length, oldData)
  184.     local data = {}
  185.     if (oldData == nil) then
  186.         data.data = readChar(stream, length)
  187.     else
  188.         data.data = oldData.data .. readChar(stream, length)
  189.     end
  190.     return data
  191. end
  192.  
  193. local function getDataPLTE(stream, length)
  194.     local data = {}
  195.     data["numColors"] = math.floor(length/3)
  196.     data["colors"] = {}
  197.     for i = 1, data["numColors"] do
  198.         data.colors[i] = {
  199.             R = readByte(stream),
  200.             G = readByte(stream),
  201.             B = readByte(stream)
  202.         }
  203.     end
  204.     return data
  205. end
  206.  
  207. local function extractChunkData(stream)
  208.     local chunkData = {}
  209.     local length
  210.     local type
  211.     local crc
  212.  
  213.     while type ~= "IEND" do
  214.         length = readInt(stream)
  215.         type = readChar(stream, 4)
  216.         if (type == "IHDR") then
  217.             chunkData[type] = getDataIHDR(stream, length)
  218.         elseif (type == "IDAT") then
  219.             chunkData[type] = getDataIDAT(stream, length, chunkData[type])
  220.         elseif (type == "PLTE") then
  221.             chunkData[type] = getDataPLTE(stream, length)
  222.         else
  223.             readChar(stream, length)
  224.         end
  225.         crc = readChar(stream, 4)
  226.     end
  227.  
  228.     return chunkData
  229. end
  230.  
  231. local function makePixel(stream, depth, colorType, palette)
  232.     local bps = math.floor(depth/8) --bits per sample
  233.     local pixelData = { R = 0, G = 0, B = 0, A = 0 }
  234.     local grey
  235.     local index
  236.     local color
  237.  
  238.     if colorType == 0 then
  239.         grey = readInt(stream, bps)
  240.         pixelData.R = grey
  241.         pixelData.G = grey
  242.         pixelData.B = grey
  243.         pixelData.A = 255
  244.     elseif colorType == 2 then
  245.         pixelData.R = readInt(stream, bps)
  246.         pixelData.G = readInt(stream, bps)
  247.         pixelData.B = readInt(stream, bps)
  248.         pixelData.A = 255
  249.     elseif colorType == 3 then
  250.         index = readInt(stream, bps)+1
  251.         color = palette.colors[index]
  252.         pixelData.R = color.R
  253.         pixelData.G = color.G
  254.         pixelData.B = color.B
  255.         pixelData.A = 255
  256.     elseif colorType == 4 then
  257.         grey = readInt(stream, bps)
  258.         pixelData.R = grey
  259.         pixelData.G = grey
  260.         pixelData.B = grey
  261.         pixelData.A = readInt(stream, bps)
  262.     elseif colorType == 6 then
  263.         pixelData.R = readInt(stream, bps)
  264.         pixelData.G = readInt(stream, bps)
  265.         pixelData.B = readInt(stream, bps)
  266.         pixelData.A = readInt(stream, bps)
  267.     end
  268.  
  269.     return pixelData
  270. end
  271.  
  272. local function bitFromColorType(colorType)
  273.     if colorType == 0 then return 1 end
  274.     if colorType == 2 then return 3 end
  275.     if colorType == 3 then return 1 end
  276.     if colorType == 4 then return 2 end
  277.     if colorType == 6 then return 4 end
  278.     error 'Invalid colortype'
  279. end
  280.  
  281. local function paethPredict(a, b, c)
  282.     local p = a + b - c
  283.     local varA = math.abs(p - a)
  284.     local varB = math.abs(p - b)
  285.     local varC = math.abs(p - c)
  286.  
  287.     if varA <= varB and varA <= varC then
  288.         return a
  289.     elseif varB <= varC then
  290.         return b
  291.     else
  292.         return c
  293.     end
  294. end
  295.  
  296. local function filterType1(curPixel, lastPixel)
  297.     local lastByte
  298.     local newPixel = {}
  299.     for fieldName, curByte in pairs(curPixel) do
  300.         lastByte = lastPixel and lastPixel[fieldName] or 0
  301.         newPixel[fieldName] = (curByte + lastByte) % 256
  302.     end
  303.     return newPixel
  304. end
  305.  
  306. local prevPixelRow = {}
  307. local function getPixelRow(stream, depth, colorType, palette, length)
  308.     local pixelRow = {}
  309.     local bpp = math.floor(depth/8) * bitFromColorType(colorType)
  310.     local bpl = bpp*length
  311.     local filterType = readByte(stream)
  312.  
  313.     if filterType == 0 then
  314.         for x = 1, length do
  315.             pixelRow[x] = makePixel(stream, depth, colorType, palette)
  316.         end
  317.     elseif filterType == 1 then
  318.         local curPixel
  319.         local lastPixel
  320.         local newPixel
  321.         local lastByte
  322.         for x = 1, length do
  323.             curPixel = makePixel(stream, depth, colorType, palette)
  324.             lastPixel = prevPixelRow[pixelNum]
  325.             newPixel = {}
  326.             for fieldName, curByte in pairs(curPixel) do
  327.                 lastByte = lastPixel and lastPixel[fieldName] or 0
  328.                 newPixel[fieldName] = (curByte + lastByte) % 256
  329.             end
  330.             pixelRow[x] = newPixel
  331.         end
  332.     else
  333.         error("Unsupported filter type: " .. tostring(filterType))
  334.     end
  335.     prevPixelRow = pixelRow
  336.  
  337.     return pixelRow
  338. end
  339.  
  340.  
  341. local function pngImage(path, progCallback, verbose, memSave)
  342.     local stream = io.open(path, "rb")
  343.     local chunkData
  344.     local imStr
  345.     local width = 0
  346.     local height = 0
  347.     local depth = 0
  348.     local colorType = 0
  349.     local output = {}
  350.     local pixels = {}
  351.     local StringStream
  352.     local function printV(msg)
  353.         if (verbose) then
  354.             print(msg)
  355.         end
  356.     end
  357. --print(readChar(stream, 8))
  358.     if readChar(stream, 8) ~= "\137\080\078\071\013\010\026\010" then
  359.         --error "Not a png"
  360.     end
  361.  
  362.     printV("Parsing Chunks...")
  363.     chunkData = extractChunkData(stream)
  364.  
  365.     width = chunkData.IHDR.width
  366.     height = chunkData.IHDR.height
  367.     depth = chunkData.IHDR.bitDepth
  368.     colorType = chunkData.IHDR.colorType
  369.  
  370.     printV("Deflating...")
  371.     deflate.inflate_zlib {
  372.         input = chunkData.IDAT.data,
  373.         output = function(byte)
  374.             output[#output+1] = string.char(byte)
  375.         end,
  376.         disable_crc = true
  377.     }
  378.     StringStream = {
  379.         str = table.concat(output),
  380.         read = function(self, num)
  381.             local toreturn = self.str:sub(1, num)
  382.             self.str = self.str:sub(num + 1, self.str:len())
  383.             return toreturn
  384.         end  
  385.     }
  386.  
  387.     printV("Creating pixelmap...")
  388.     for i = 1, height do
  389.         local pixelRow = getPixelRow(StringStream, depth, colorType, chunkData.PLTE, width)
  390.         if progCallback ~= nil then
  391.             progCallback(i, height, pixelRow)
  392.         end
  393.         if not memSave then
  394.             pixels[i] = pixelRow
  395.         end
  396.     end
  397.  
  398.     printV("Done.")
  399.     return {
  400.         width = width,
  401.         height = height,
  402.         depth = depth,
  403.         colorType = colorType,
  404.         pixels = pixels
  405.     }
  406. end
  407.  
  408. local img = pngImage("skin.png",nil,true)
  409. print(img.width)
  410. print(img.height)
Advertisement
Add Comment
Please, Sign In to add comment