summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorubq323 <ubq323@ubq323.website>2022-08-04 20:48:28 +0100
committerubq323 <ubq323@ubq323.website>2022-08-04 20:48:28 +0100
commit30869f88297faa5f696b47f4e18a26ff817cdf03 (patch)
tree683be7e8e709d2d792d39f5971adebdc08c985ad
parent0f09a861ff88da2f0aec4e515c2fad9658fe1e06 (diff)
img
-rw-r--r--img/load_pnm.dat1
-rw-r--r--img/load_pnm.lua34
-rw-r--r--util.lua7
3 files changed, 42 insertions, 0 deletions
diff --git a/img/load_pnm.dat b/img/load_pnm.dat
new file mode 100644
index 0000000..38e1154
--- /dev/null
+++ b/img/load_pnm.dat
@@ -0,0 +1 @@
+dep util
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
diff --git a/util.lua b/util.lua
new file mode 100644
index 0000000..109ac67
--- /dev/null
+++ b/util.lua
@@ -0,0 +1,7 @@
+local function perr(err,what)
+ if err then error(what..": "..err) end
+end
+
+return {
+ perr=perr,
+}