summaryrefslogtreecommitdiff
path: root/img
diff options
context:
space:
mode:
authorubq323 <ubq323@ubq323.website>2022-08-04 22:48:50 +0100
committerubq323 <ubq323@ubq323.website>2022-08-04 22:48:50 +0100
commit39b4e4f86c010ee84f5357e4490a6cfab49bedc9 (patch)
tree75e9b4e3ad45cd2b0b3ebc32e98e5d444914bf8f /img
parent0005960474d5b92afaecd3da95b4774e30421e2e (diff)
pnm
Diffstat (limited to 'img')
-rw-r--r--img/load_pnm.lua19
1 files changed, 11 insertions, 8 deletions
diff --git a/img/load_pnm.lua b/img/load_pnm.lua
index 86d2a67..d3f5873 100644
--- a/img/load_pnm.lua
+++ b/img/load_pnm.lua
@@ -13,8 +13,10 @@
-- function nextpixel(imgf,cur,depth) -> pixel, ncur
-- where pixel is {r,g,b}, with r,g,b in [0,1]
+local pixel_mt = {__tostring=function(s) return string.format("rgb(%f,%f,%f)",s[1],s[2],s[3]) end}
+
local function np_p1(imgf,cur,depth)
- local val,ncur = imgf:match("[01]%s*()",cur)
+ local val,ncur = imgf:match("([01])%s*()",cur)
val = tonumber(val)
assert(val,"bad image format at "..cur)
local p = (1-val) -- 1 is black (0), 0 is white (1)
@@ -81,12 +83,11 @@ local function parse(imgf)
width,height = tonumber(width),tonumber(height)
assert(width,"couldn't find image dimensions")
-
-
-- by 'depth' i mean 'maximum value'
local depth = 1
if has_depth then
- local ndepth,ncur = imgf:match("(%d+)%s*()")
+ local ndepth,ncur = imgf:match("(%d+)%s*()",cur)
+ ndepth=tonumber(ndepth)
assert(ndepth,"couldn't find image depth (max pixel value), in a format that needs it")
depth=ndepth
cur=ncur
@@ -106,13 +107,15 @@ local function parse(imgf)
while cur <= #imgf do
local npixel,ncur = nextpixel(imgf,cur,depth)
+ setmetatable(npixel,pixel_mt)
img[y][x] = npixel
cur = ncur
- if y > width then
- y = 1
- x = x + 1
+ x = x + 1
+ if x > width then
+ x = 1
+ y = y + 1
end
- if x > height then
+ if y > height then
break
end
end