From 30869f88297faa5f696b47f4e18a26ff817cdf03 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Thu, 4 Aug 2022 20:48:28 +0100 Subject: img --- img/load_pnm.lua | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 img/load_pnm.lua (limited to 'img/load_pnm.lua') diff --git a/img/load_pnm.lua b/img/load_pnm.lua new file mode 100644 index 0000000..60cac8a --- /dev/null +++ b/img/load_pnm.lua @@ -0,0 +1,34 @@ +local perr = require("util").perr + +local function load_p1(fname) + + local file,err = fs.open(fname,"r") + perr(err,"fs.open") + local imgf = file.readAll() + --print(img) + assert(imgf:sub(1,2) == "P1","only P1 images are supported at the moment") + local width,height, cur = imgf:match("%s*(%d+)%s*(%d+)%s*()",3) + assert(width,"couldn't find image header") + + local img=setmetatable({},{__index=function(t,k) + if type(k) ~= "number" then return nil end + local r = {} + rawset(t,k,r) + return r + end}) + + -- img[y][x]. 1,1 is top left + for row=1,height do + for col =1,width do + local val,ncur = imgf:match("(%d+)%s*()",cur) + cur = ncur + img[row][col] = tonumber(val) + end + end + + img.width=width + img.height=height + return img +end + +return load_p1 -- cgit v1.2.3