From 71d024a76cf881caa171d861c1ebc87e946487ef Mon Sep 17 00:00:00 2001 From: MineRobber___T Date: Sat, 15 Jul 2017 18:27:52 -0400 Subject: [PATCH] Move raw data parsing to paintutils.drawImage --- .../computercraft/lua/rom/apis/paintutils.lua | 61 ++++++++++--------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/src/main/resources/assets/computercraft/lua/rom/apis/paintutils.lua b/src/main/resources/assets/computercraft/lua/rom/apis/paintutils.lua index 1d9238cc2..facdb1b43 100644 --- a/src/main/resources/assets/computercraft/lua/rom/apis/paintutils.lua +++ b/src/main/resources/assets/computercraft/lua/rom/apis/paintutils.lua @@ -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