1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-12-15 12:40:30 +00:00

Move raw data parsing to paintutils.drawImage

This commit is contained in:
MineRobber___T 2017-07-15 18:27:52 -04:00 committed by GitHub
parent 260bb2e4e1
commit 71d024a76c

View File

@ -9,48 +9,49 @@ for n=1,16 do
tColourLookup[ string.byte( "0123456789abcdef",n,n ) ] = 2^(n-1)
end
function loadImage( sPath, bLoadRawData )
local function parseLine( tImageArg, sLine )
local tLine = {}
for x=1,sLine:len() do
tLine[x] = tColourLookup[ string.byte(sLine,x,x) ] or 0
end
table.insert( tImageArg, tLine )
return tImageArg
end
function parseImage( sRawData )
if type( sRawData ) ~= "string" then
error ( "bad argument #1 (expected string, got " .. type( sRawData ) .. ")" )
end
local function split( str, delim ) -- modified split function from https://codea.io/talk/discussion/2118/split-a-string-by-return-newline
if string.find( str, delim ) == nil then return { str } end
local result, pat, lastpos = {}, "(.-)" .. delim .. "()", nil
for part, pos in string.gfind( str, pat ) do table.insert( result, part ); lastpos = pos; end
table.insert( result, string.sub( str, lastpos ) )
return result
end
local tImage = {}
for _, sLine in pairs( split( sPath, "\n" ) ) do -- read each line like original file handling did
tImage = parseLine( tImage, sLine )
end
return tImage
end
function loadImage( sPath )
if type( sPath ) ~= "string" then
error( "bad argument #1 (expected string, got " .. type( sPath ) .. ")", 2 )
end
if bLoadRawData ~= nil and type(bLoadRawData) ~= "boolean" then
error( "bad argument #2 (expected nil or boolean, got " .. type( sPath ) .. ")" )
end
local function parseLine( tImageArg, sLine )
local tLine = {}
for x=1,sLine:len() do
tLine[x] = tColourLookup[ string.byte(sLine,x,x) ] or 0
end
table.insert( tImageArg, tLine )
return tImageArg
end
if fs.exists( sPath ) and not bLoadRawData then
local tImage = {}
local file = io.open( sPath, "r" )
local sLine = file:read()
local sContent = ""
while sLine do
tImage = parseLine( tImage, sLine )
sContent = sContent .. sLine .. "\n"
sLine = file:read()
end
file:close()
return tImage
else
if bLoadRawData then
local function split( str, delim ) -- modified split function from https://codea.io/talk/discussion/2118/split-a-string-by-return-newline
if string.find( str, delim ) == nil then return { str } end
local result, pat, lastpos = {}, "(.-)" .. delim .. "()", nil
for part, pos in string.gfind( str, pat ) do table.insert( result, part ); lastpos = pos; end
table.insert( result, string.sub( str, lastpos ) )
return result
end
local tImage = {}
for _, sLine in pairs( split( sPath, "\n" ) ) do -- read each line like original file handling did
tImage = parseLine( tImage, sLine )
end
return tImage
end
return parseImage( sContent ) -- delegate parsing of images to parseImage function as suggested by @SquidDev in discussion of PR #378
end
return nil
end