Guest User

imgtest

a guest
Jul 25th, 2018
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.42 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. function safeString(text)
  122.   local newText = {}
  123.   text = tostring(text)
  124.   for i = 1, #text do
  125.     local val = text:byte(i)
  126.     newText[i] = (val > 31 and val < 127) and val or 63
  127.   end
  128.   return string.char(table.unpack(newText))
  129. end
  130.  
  131. local oldPrint, oldBlit = print, term.blit
  132.  
  133. function print(text)
  134.   return oldPrint(safeString(text))
  135. end
  136.  
  137. function term.blit(text, fg, bg)
  138.   return oldBlit(safeString(text), fg, bg)
  139. end
  140.  
  141. local deflate = require("deflatelua")
  142. local requiredDeflateVersion = "0.3.20111128"
  143.  
  144. if (deflate._VERSION ~= requiredDeflateVersion) then
  145.     error("Incorrect deflate version: must be "..requiredDeflateVersion..", not "..deflate._VERSION)
  146. end
  147.  
  148. local function bsRight(num, pow)
  149.     return math.floor(num / 2^pow)
  150. end
  151.  
  152. local function bsLeft(num, pow)
  153.     return math.floor(num * 2^pow)
  154. end
  155.  
  156. local function bytesToNum(bytes)
  157.     local n = 0
  158.     for k,v in ipairs(bytes) do
  159.         n = bsLeft(n, 8) + v
  160.     end
  161.     if (n > 2147483647) then
  162.         return (n - 4294967296)
  163.     else
  164.         return n
  165.     end
  166.     n = (n > 2147483647) and (n - 4294967296) or n
  167.     return n
  168. end
  169.  
  170. local function readByte(stream)
  171.     return stream:read()
  172. end
  173.  
  174. local function readInt(stream, bps)
  175.     local bytes = {}
  176.     bps = bps or 4
  177.     for i=1,bps do
  178.         bytes[i] = readByte(stream)
  179.     end
  180.     return bytesToNum(bytes)
  181. end
  182.  
  183. local function readChar(stream, num)
  184.     num = num or 1
  185.     local bytes = {}
  186.     for i = 1, num do
  187.         bytes[i] = string.char(readByte(stream))
  188.     end
  189.     return table.concat(bytes)
  190. end
  191.  
  192. local function getDataIHDR(stream, length)
  193.     local data = {}
  194.     data["width"] = readInt(stream)
  195.     data["height"] = readInt(stream)
  196.     data["bitDepth"] = readByte(stream)
  197.     data["colorType"] = readByte(stream)
  198.     data["compression"] = readByte(stream)
  199.     data["filter"] = readByte(stream)
  200.     data["interlace"] = readByte(stream)
  201.     return data
  202. end
  203.  
  204. local function getDataIDAT(stream, length, oldData)
  205.     local data = {}
  206.     if (oldData == nil) then
  207.         data.data = readChar(stream, length)
  208.     else
  209.         data.data = oldData.data .. readChar(stream, length)
  210.     end
  211.     return data
  212. end
  213.  
  214. local function getDataPLTE(stream, length)
  215.     local data = {}
  216.     data["numColors"] = math.floor(length/3)
  217.     data["colors"] = {}
  218.     for i = 1, data["numColors"] do
  219.         data.colors[i] = {
  220.             R = readByte(stream),
  221.             G = readByte(stream),
  222.             B = readByte(stream)
  223.         }
  224.     end
  225.     return data
  226. end
  227.  
  228. local function extractChunkData(stream)
  229.     local chunkData = {}
  230.     local length
  231.     local type
  232.     local crc
  233.  
  234.     while type ~= "IEND" do
  235.         length = readInt(stream)
  236.         type = readChar(stream, 4)
  237.         if (type == "IHDR") then
  238.             chunkData[type] = getDataIHDR(stream, length)
  239.         elseif (type == "IDAT") then
  240.             chunkData[type] = getDataIDAT(stream, length, chunkData[type])
  241.         elseif (type == "PLTE") then
  242.             chunkData[type] = getDataPLTE(stream, length)
  243.         else
  244.             readChar(stream, length)
  245.         end
  246.         crc = readChar(stream, 4)
  247.     end
  248.  
  249.     return chunkData
  250. end
  251.  
  252. local function makePixel(stream, depth, colorType, palette)
  253.     local bps = math.floor(depth/8) --bits per sample
  254.     local pixelData = { R = 0, G = 0, B = 0, A = 0 }
  255.     local grey
  256.     local index
  257.     local color
  258.  
  259.     if colorType == 0 then
  260.         grey = readInt(stream, bps)
  261.         pixelData.R = grey
  262.         pixelData.G = grey
  263.         pixelData.B = grey
  264.         pixelData.A = 255
  265.     elseif colorType == 2 then
  266.         pixelData.R = readInt(stream, bps)
  267.         pixelData.G = readInt(stream, bps)
  268.         pixelData.B = readInt(stream, bps)
  269.         pixelData.A = 255
  270.     elseif colorType == 3 then
  271.         index = readInt(stream, bps)+1
  272.         color = palette.colors[index]
  273.         pixelData.R = color.R
  274.         pixelData.G = color.G
  275.         pixelData.B = color.B
  276.         pixelData.A = 255
  277.     elseif colorType == 4 then
  278.         grey = readInt(stream, bps)
  279.         pixelData.R = grey
  280.         pixelData.G = grey
  281.         pixelData.B = grey
  282.         pixelData.A = readInt(stream, bps)
  283.     elseif colorType == 6 then
  284.         pixelData.R = readInt(stream, bps)
  285.         pixelData.G = readInt(stream, bps)
  286.         pixelData.B = readInt(stream, bps)
  287.         pixelData.A = readInt(stream, bps)
  288.     end
  289.  
  290.     return pixelData
  291. end
  292.  
  293. local function bitFromColorType(colorType)
  294.     if colorType == 0 then return 1 end
  295.     if colorType == 2 then return 3 end
  296.     if colorType == 3 then return 1 end
  297.     if colorType == 4 then return 2 end
  298.     if colorType == 6 then return 4 end
  299.     error 'Invalid colortype'
  300. end
  301.  
  302. local function paethPredict(a, b, c)
  303.     local p = a + b - c
  304.     local varA = math.abs(p - a)
  305.     local varB = math.abs(p - b)
  306.     local varC = math.abs(p - c)
  307.  
  308.     if varA <= varB and varA <= varC then
  309.         return a
  310.     elseif varB <= varC then
  311.         return b
  312.     else
  313.         return c
  314.     end
  315. end
  316.  
  317. local function filterType1(curPixel, lastPixel)
  318.     local lastByte
  319.     local newPixel = {}
  320.     for fieldName, curByte in pairs(curPixel) do
  321.         lastByte = lastPixel and lastPixel[fieldName] or 0
  322.         newPixel[fieldName] = (curByte + lastByte) % 256
  323.     end
  324.     return newPixel
  325. end
  326.  
  327. local prevPixelRow = {}
  328. local function getPixelRow(stream, depth, colorType, palette, length)
  329.     local pixelRow = {}
  330.     local bpp = math.floor(depth/8) * bitFromColorType(colorType)
  331.     local bpl = bpp*length
  332.     local filterType = readByte(stream)
  333.  
  334.     if filterType == 0 then
  335.         for x = 1, length do
  336.             pixelRow[x] = makePixel(stream, depth, colorType, palette)
  337.         end
  338.     elseif filterType == 1 then
  339.         local curPixel
  340.         local lastPixel
  341.         local newPixel
  342.         local lastByte
  343.         for x = 1, length do
  344.             curPixel = makePixel(stream, depth, colorType, palette)
  345.             lastPixel = prevPixelRow[pixelNum]
  346.             newPixel = {}
  347.             for fieldName, curByte in pairs(curPixel) do
  348.                 lastByte = lastPixel and lastPixel[fieldName] or 0
  349.                 newPixel[fieldName] = (curByte + lastByte) % 256
  350.             end
  351.             pixelRow[x] = newPixel
  352.         end
  353.     else
  354.         error("Unsupported filter type: " .. tostring(filterType))
  355.     end
  356.     prevPixelRow = pixelRow
  357.  
  358.     return pixelRow
  359. end
  360.  
  361.  
  362. local function pngImage(path, progCallback, verbose, memSave)
  363.     local stream = io.open(path, "rb")
  364.     local chunkData
  365.     local imStr
  366.     local width = 0
  367.     local height = 0
  368.     local depth = 0
  369.     local colorType = 0
  370.     local output = {}
  371.     local pixels = {}
  372.     local StringStream
  373.     local function printV(msg)
  374.         if (verbose) then
  375.             print(msg)
  376.         end
  377.     end
  378.    
  379.     if readChar(stream, 8) ~= "\137\080\078\071\013\010\026\010" then
  380.         error "Not a png"
  381.     end
  382.  
  383.     printV("Parsing Chunks...")
  384.     chunkData = extractChunkData(stream)
  385.  
  386.     width = chunkData.IHDR.width
  387.     height = chunkData.IHDR.height
  388.     depth = chunkData.IHDR.bitDepth
  389.     colorType = chunkData.IHDR.colorType
  390.  
  391.     printV("Deflating...")
  392.     deflate.inflate_zlib {
  393.         input = chunkData.IDAT.data,
  394.         output = function(byte)
  395.             output[#output+1] = string.char(byte)
  396.         end,
  397.         disable_crc = true
  398.     }
  399.     StringStream = {
  400.         str = table.concat(output),
  401.         read = function(self, num)
  402.             local toreturn = self.str:sub(1, num)
  403.             self.str = self.str:sub(num + 1, self.str:len())
  404.             return toreturn
  405.         end  
  406.     }
  407.  
  408.     printV("Creating pixelmap...")
  409.     for i = 1, height do
  410.         local pixelRow = getPixelRow(StringStream, depth, colorType, chunkData.PLTE, width)
  411.         if progCallback ~= nil then
  412.             progCallback(i, height, pixelRow)
  413.         end
  414.         if not memSave then
  415.             pixels[i] = pixelRow
  416.         end
  417.     end
  418.  
  419.     printV("Done.")
  420.     return {
  421.         width = width,
  422.         height = height,
  423.         depth = depth,
  424.         colorType = colorType,
  425.         pixels = pixels
  426.     }
  427. end
  428.  
  429. local img = pngImage("Texas Final.png",nil,true)
  430. print(img.width)
  431. print(img.height)
Advertisement
Add Comment
Please, Sign In to add comment