fyrkantis

Adpaint

Nov 4th, 2026 (edited)
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 92.27 KB | None | 0 0
  1. -- adpaint.lua (minified), built from the adpaint sources.
  2. package.preload["lib.cli"]=function(...)local a={}function a.isHelp(b)return b=="help"or b=="-h"or b=="--help"or b=="-?"end function a.registerCompletion(b)if shell.setCompletionFunction then shell.
  3. setCompletionFunction(shell.getRunningProgram(),b)end end function a.completion()local b,c=pcall(require,"cc.shell.completion")return b and c or nil end function a.addChoices(b,c)for e,d in ipairs(c)
  4. do b[#b+1]=d end return b end function a.setup(e,h,f)local c=("shell.run(%q, \"--register\") %s"):format("/"..shell.getRunningProgram(),e)local b for g,d in ipairs({"/startup.lua","/startup"})do if fs
  5. .exists(d)and not fs.isDir(d)then b=d;break end end if b then local d=a.readFile(b,"r")or""if d:find(e,1,true)then print("Already set up in "..b);return end local g=fs.open(b,"a")g.write((d:sub(-1)==
  6. "\n"or d=="")and c.."\n"or"\n"..c.."\n")g.close()else b=h local d=fs.open(b,"w")d.write(c.."\n")d.close()end print("Added completion to "..b..".")if f then print(f)end end function a.readFile(c,d)
  7. local b=fs.open(c,d or"rb")if not b then return nil end local e=b.readAll()or""b.close()return e end function a.yielder(c)local d=os.epoch("utc")return function()if os.epoch("utc")-d>2000 then os.
  8. queueEvent("adpaint_yield")while true do local b={os.pullEventRaw()}if b[1]=="adpaint_yield"then break elseif b[1]=="terminate"then error("Terminated",0)elseif c then c(b)end end d=os.epoch("utc")end
  9. end end return a end package.preload["lib.convert"]=function(...)local c=require("lib.formats.adimg")local D=require("lib.formats.decode")local e={}local a=math.floor e.detect=D.detect e.info=D.info
  10. function e.fitCells(b,h,g)local f=h local d=a(f*2*b.dh/b.dw/3+0.5)if d>g then d=g f=a(d*3*b.dw/b.dh/2+0.5)end return math.max(1,f),math.max(1,d)end local function V(G,w,b,j,H,I,l,J)local K=D.decoder(w
  11. .format)local f,k=w.w,w.h local C=false if w.format=="jpeg"and math.ceil(f/8)>=b and math.ceil(k/8)>=j then C=true f,k=math.ceil(f/8),math.ceil(k/8)end local p,q,r=c.rgb(H)local g,h,i,s={},{},{},{}for
  12. d=1,b*j do g[d],h[d],i[d],s[d]=0,0,0,0 end local t if f>=b and k>=j then local o={}for d=1,f do o[d]=a((d-1)*b/f)+1 end t=function(u,v,x,y,A)if u>=k then return end local B=a(u*j/k)*b for m=1,f do
  13. local d=B+o[m]local z=A[m]if z==255 then g[d]=g[d]+v[m];h[d]=h[d]+x[m];i[d]=i[d]+y[m]else local n=z/255 g[d]=g[d]+v[m]*n+p*(1-n)h[d]=h[d]+x[m]*n+q*(1-n)i[d]=i[d]+y[m]*n+r*(1-n)end s[d]=s[d]+1 end end
  14. else local m={}for n=0,j-1 do local d=a((n+0.5)*k/j)m[d]=m[d]or{}table.insert(m[d],n)end local u={}for d=1,b do u[d]=math.min(f,a((d-0.5)*f/b)+1)end t=function(y,z,A,B,E)local v=m[y]if not v then
  15. return end for L,F in ipairs(v)do for x=1,b do local n=u[x]local o=F*b+x local d=E[n]/255 g[o]=z[n]*d+p*(1-d)h[o]=A[n]*d+q*(1-d)i[o]=B[n]*d+r*(1-d)s[o]=1 end end end end local m=J and l and f>=b and k
  16. >=j and{}or nil local y if m then y={}for d=1,f do y[d]=a((d-1)*b/f)+1 end end if l then local L,z=t,0 t=function(A,n,u,v,B)if m and A<k then local M=a(A*j/k)*b for d=1,#n do local E=B[d]local x if E
  17. ==255 then x=n[d]*65536+u[d]*256+v[d]else local o=E/255 x=a(n[d]*o+p*(1-o)+0.5)*65536+a(u[d]*o+q*(1-o)+0.5)*256+a(v[d]*o+r*(1-o)+0.5)end local F=M+y[d]local o=m[F]if not o then o={};m[F]=o end o[x]=(o
  18. [x]or 0)+1 end end if not l.overflow then for d=1,#n do local E=B[d]local x if E==255 then x=n[d]*65536+u[d]*256+v[d]else local o=E/255 x=a(n[d]*o+p*(1-o)+0.5)*65536+a(u[d]*o+q*(1-o)+0.5)*256+a(v[d]*o
  19. +r*(1-o)+0.5)end local o=l[x]if not o then z=z+1 if z>4096 then l.overflow=true;m=nil;break end o=0 end l[x]=o+1 end end L(A,n,u,v,B)end end K.decode(G,t,I,C)for d=1,b*j do local n=s[d]if n>0 then g[d
  20. ],h[d],i[d]=g[d]/n,h[d]/n,i[d]/n else g[d],h[d],i[d]=p,q,r end end if m and not l.overflow then for d=1,b*j do local o=m[d]if o then local u,x,z,A,B=0,nil,math.huge,nil,-1 for v,n in pairs(o)do u=u+n
  21. end for n,v in pairs(o)do if v>B then A,B=n,v end if v*4>=u and l[n]<z then x,z=n,l[n]end end local n=x or A g[d],h[d],i[d]=c.rgb(n)end end end return g,h,i end local function W(m,n,o,i,j,h,p,t)if h==
  22. 1 then return m,n,o end local q,r,s={},{},{}for b=0,t-1 do for d=0,p-1 do local f,g if h==2 then f,g=i-1-d,b elseif h==3 then f,g=i-1-d,j-1-b elseif h==4 then f,g=d,j-1-b elseif h==5 then f,g=b,d
  23. elseif h==6 then f,g=b,j-1-d elseif h==7 then f,g=i-1-b,j-1-d else f,g=i-1-b,d end local k,l=g*i+f+1,b*p+d+1 q[l],r[l],s[l]=m[k],n[k],o[k]end end return q,r,s end local function J(g,h,i,f,j)local k=
  24. math.max(1,a(f/j))local b={}for d=1,f,k do b[#b+1]={g[d],h[d],i[d]}end return b end local function E(g,h,i,j,k,l)local b,d,f=g-j,h-k,i-l return 2*b*b+4*d*d+3*f*f end local function x(b)local d,f,g=c.
  25. rgb(b);return d*0.3+f*0.59+g*0.11 end local function X(s,t,u,v,w,o)local d=J(s,t,u,v,3000)local y=c.DEFAULT_PALETTE local j,k,l={},{},{}for b=0,15 do j[b],k[b],l[b]=c.rgb(y[b])end local f={}for b=1,#d
  26. do f[b]=math.huge end local h,p={},{}for z=1,w do local b,q=nil,math.huge for g=0,15 do if not p[g]then local i=0 for m=1,#d do local n=d[m]local r=E(n[1],n[2],n[3],j[g],k[g],l[g])i=i+(r<f[m]and r or f
  27. [m])end if i<q then b,q=g,i end end end p[b]=true h[#h+1]=b for g=1,#d do local i=d[g]local m=E(i[1],i[2],i[3],j[b],k[b],l[b])if m<f[g]then f[g]=m end end if o then o()end end table.sort(h)return h end
  28. local function K(b)local j,g={},{}for f,d in ipairs(b)do if not j[d]then j[d]=true;g[#g+1]=d end end b=g table.sort(b,function(d,f)return x(d)>x(f)end)local n=#b local d,k={},{}while#b+#d<16 do local h
  29. ,l=nil,-1 for i=0,15 do if not k[i]then local m=c.DEFAULT_PALETTE[i]local f=math.huge for p,o in ipairs(b)do f=math.min(f,c.dist(m,o))end for p,o in ipairs(d)do f=math.min(f,c.dist(m,o))end if f>l then
  30. h,l=i,f end end end k[h]=true d[#d+1]=c.DEFAULT_PALETTE[h]end table.sort(d,function(f,h)return x(f)>x(h)end)local h,i={},{}for f=1,16 do h[f-1]=b[f]or d[f-#b]if f<=n then i[f]=f-1 end end return h,i
  31. end local function Y(t,u,v,w,j,l)l=l or 16 local f=J(t,u,v,w,12000)local function m(b)local i,k,n,o=0,0,0,b[2]-b[1]+1 for g=b[1],b[2]do local d=f[g];i,k,n=i+d[1],k+d[2],n+d[3]end local q,r,s=i/o,k/o,n
  32. /o local d,g,h=0,0,0 for y=b[1],b[2]do local p=f[y]d,g,h=d+(p[1]-q)^2,g+(p[2]-r)^2,h+(p[3]-s)^2 end d,g,h=d*2,g*4,h*3 b.err=d+g+h b.ch=(d>=g and d>=h)and 1 or(g>=h and 2 or 3)b.mean={q,r,s}end local d
  33. ={{1,#f}}m(d[1])while#d<l do local h for g,b in ipairs(d)do if b[2]>b[1]and b.err>0 and(not h or b.err>d[h].err)then h=g end end if not h then break end local b=d[h]local k=b.ch local i={}for g=b[1],b
  34. [2]do i[#i+1]=f[g]end table.sort(i,function(g,n)return g[k]<n[k]end)for g=1,#i do f[b[1]+g-1]=i[g]end local p,g=b.mean[k],b[1]while g<b[2]-1 and f[g+1][k]<=p do g=g+1 end if g>=b[2]then g=b[2]-1 end
  35. local n,o={b[1],g},{g+1,b[2]}m(n);m(o)d[h]=n d[#d+1]=o if j then j()end end local b={}for h,g in ipairs(d)do b[#b+1]={g.mean[1],g.mean[2],g.mean[3]}end for y=1,4 do local n,i,o={},nil,-1 for g=1,#b do
  36. n[g]={0,0,0,0}end for p=1,#f do local h=f[p]local q,k=1,math.huge for r=1,#b do local g=b[r]local s=E(h[1],h[2],h[3],g[1],g[2],g[3])if s<k then q,k=r,s end end if k>o then i,o=h,k end local g=n[q]g[1]
  37. ,g[2],g[3],g[4]=g[1]+h[1],g[2]+h[2],g[3]+h[3],g[4]+1 if p%2000==0 and j then j()end end for h=1,#b do local g=n[h]if g[4]>0 then b[h]={g[1]/g[4],g[2]/g[4],g[3]/g[4]}elseif i then b[h]={i[1],i[2],i[3]}
  38. ;i=nil end end end local h={}for i,g in ipairs(b)do h[i]=a(g[1]+0.5)*65536+a(g[2]+0.5)*256+a(g[3]+0.5)end return K(h)end local function Z(o,k)local b={}for d,f in pairs(o)do if type(d)=="number"then b
  39. [#b+1]={d,f}end end table.sort(b,function(d,f)return d[2]>f[2]end)local d={}if#b<=k then for f,g in ipairs(b)do d[f]=g[1]end else local f={}for g=1,#b do f[g]=math.huge end local h=1 while#d<k do d[#d
  40. +1]=b[h][1]local i,j=nil,-1 for g,l in ipairs(b)do local m=c.dist(l[1],b[h][1])if m<f[g]then f[g]=m end local n=l[2]*math.sqrt(f[g])if n>j then i,j=g,n end end if not i or j<=0 then break end h=i end
  41. end return K(d)end local function _(L,aa,ab,M,N,ac,ad,O,y)local ae=#y local h=M*2 local q,r,s={},{},{}for b=0,15 do q[b],r[b],s[b]=c.rgb(ac[b])end local P={}local d,f,g={},{},{}for b=1,#L do d[b],f[b]
  42. ,g[b]=L[b],aa[b],ab[b]end local Q={0,1,h,h+1,2*h,2*h+1}local function n(b)return b<0 and 0 or b>255 and 255 or b end local t={}for R=0,N-1 do for S=0,M-1 do local T=R*3*h+S*2+1 local o,i={},0 local z=
  43. {}for v=1,6 do local m=T+Q[v]local C,F,G=n(d[m]),n(f[m]),n(g[m])local j,k,p,u=y[1],y[1],math.huge,math.huge for H=1,ae do local b=y[H]local w,A,B=C-q[b],F-r[b],G-s[b]local l=2*w*w+4*A*A+3*B*B t[v*16+b
  44. ]=l if l<p then k,u=j,p;j,p=b,l elseif l<u then k,u=b,l end end if not z[j]then z[j]=true;i=i+1;o[i]=j end if not z[k]then z[k]=true;i=i+1;o[i]=k end end local k,l,U=o[1],o[1],math.huge for j=1,i do
  45. local m=o[j]for A=j,i do local p=o[A]local b=0 for u=1,6 do local v,w=t[u*16+m],t[u*16+p]b=b+(v<w and v or w)end if b<U then k,l,U=m,p,b end end end for m=1,6 do local j=T+Q[m]local F,G,H=n(d[j]),n(f[
  46. j]),n(g[j])local p if ad then local u,v,w=F-q[k],G-r[k],H-s[k]local af=2*u*u+4*v*v+3*w*w u,v,w=F-q[l],G-r[l],H-s[l]p=(2*u*u+4*v*v+3*w*w)<af and l or k local A,B,C=(F-q[p])*0.85,(G-r[p])*0.85,(H-s[p])*
  47. 0.85 local I=S*2+(m-1)%2 local ag=R*3+a((m-1)/2)local ah=(m-1)%2 local ai=a((m-1)/2)if I+1<h then local b=j+1 d[b],f[b],g[b]=d[b]+A*7/16,f[b]+B*7/16,g[b]+C*7/16 end if ag+1<N*3 then local b=j+h d[b],f
  48. [b],g[b]=d[b]+A*5/16,f[b]+B*5/16,g[b]+C*5/16 if I>0 and not(ah==0 and ai<2)then b=j+h-1 d[b],f[b],g[b]=d[b]+A*3/16,f[b]+B*3/16,g[b]+C*3/16 end if I+1<h then b=j+h+1 d[b],f[b],g[b]=d[b]+A/16,f[b]+B/16,
  49. g[b]+C/16 end end else p=t[m*16+k]<=t[m*16+l]and k or l end P[j]=p end end if O then O()end end return P end function e.import(A,b)local d,H=e.info(A)if not d then return nil,H end local g,h=b.w,b.h if
  50. g and not h then h=select(2,e.fitCells(d,g,math.huge))end if not g or not h then g,h=e.fitCells(d,b.maxW or 51,b.maxH or 19)end local k,n=g*2,h*3 local l,o,B,C=c.fitRect(d.dw,d.dh,k,n,b.mode or
  51. "stretch",b.ax,b.ay)local s,t=l,o if d.orientation>=5 then s,t=o,l end local p=b.status or function()end p("Decoding "..d.format:upper().." "..d.w.."x"..d.h.."...")local u={}local I,f,i,j=pcall(V,A,d,
  52. s,t,b.background or 0xFFFFFF,b.yield,u,not b.dither)if not I then return nil,tostring(f)end f,i,j=W(f,i,j,s,t,d.orientation,l,o)if l~=k or o~=n or B~=0 or C~=0 then local L,M,N=c.rgb(b.background or
  53. 0xFFFFFF)local q,r,v={},{},{}for F=0,n-1 do local w=F-C for G=0,k-1 do local y=G-B local m=F*k+G+1 if y>=0 and w>=0 and y<l and w<o then local z=w*l+y+1 q[m],r[m],v[m]=f[z],i[z],j[z]else q[m],r[m],v[m
  54. ]=L,M,N end end end f,i,j=q,r,v end local w=c.DEFAULT_PALETTE local q,m=nil,nil local r=math.max(2,math.min(16,b.colours or 16))if b.optimise then p("Optimising palette...")if not u.overflow then q,m=
  55. Z(u,r)else q,m=Y(f,i,j,k*n,b.yield,r)end w=q elseif r<16 then p("Choosing colours...")m=X(f,i,j,k*n,r,b.yield)else m={}for v=0,15 do m[v+1]=v end end p("Quantising...")local v=c.new(g,h,15)v.px=_(f,i,
  56. j,g,h,w,b.dither,b.yield,m)v.palette=q return v end function e.convertFile(h,j)local b={colours=16,optimise=true,dither=true,mode="fit"}for d,f in pairs(j or{})do b[d]=f end local f=fs.open(h,"rb")if
  57. not f then return nil,"Cannot read "..h end local i=f.readAll()or""f.close()local d,k=e.info(i)if not d then return nil,k end for l,g in ipairs({"w","h"})do if b[g]then b[g]=math.max(1,math.min(512,a(
  58. b[g])))end end if not b.w and not b.h then local g,l=term.getSize()b.w,b.h=e.fitCells(d,b.fitW or g,b.fitH or l)elseif b.h and not b.w then b.w=math.max(1,a(b.h*3*d.dw/d.dh/2+0.5))end b.colours=math.
  59. max(2,math.min(16,a(b.colours)))local l,g,m=pcall(e.import,i,b)if not l then return nil,tostring(g)end if not g then return nil,tostring(m)end return g end return e end package.preload["lib.displays"]
  60. =function(...)local e=require("lib.formats.adimg")local a={}local f,g,j=math.floor,math.max,math.min a.SCALES={0.5,1,1.5,2,2.5,3,3.5,4,4.5,5}function a.isMonitor(b)if peripheral.hasType then return
  61. peripheral.hasType(b,"monitor")==true end return peripheral.getType(b)=="monitor"end function a.naturalLess(c,d)local function b(h)return(h:gsub("%d+",function(i)return("%012d"):format(tonumber(i))end
  62. ))end return b(c)<b(d)end function a.adjacentNames()local b={}for d,c in ipairs(redstone.getSides())do if a.isMonitor(c)then b[#b+1]=c end end return b end function a.networkNames()local d,b={},{}for h
  63. ,c in ipairs(redstone.getSides())do d[c]=true end for h,c in ipairs(peripheral.getNames())do if not d[c]and a.isMonitor(c)then b[#b+1]=c end end table.sort(b,a.naturalLess)return b end function a.find(
  64. c)local h,k={},{}local function i(b,l)local m=b=="term"or b=="terminal"if m then b="terminal"end local d=k[b]if d then if l then d.number=l end return true end if m then d={name=b,out=term.current(),
  65. isMonitor=false}elseif a.isMonitor(b)then d={name=b,out=peripheral.wrap(b),isMonitor=true}else return false end d.number=l h[#h+1]=d k[b]=d return true end for d,b in ipairs(c.names or{})do if not i(b
  66. .name,b.number)then return nil,"No monitor called "..b.name end end if c.adjacent then for d,b in ipairs(a.adjacentNames())do i(b)end end if c.network then for d,b in ipairs(a.networkNames())do i(b)
  67. end end if#h==0 then if(c.adjacent or c.network)and#(c.names or{})==0 or c.noTerminal then return nil,"No monitors found."end i("term")end return h,k end function a.number(b,c)if c then for d,h in
  68. ipairs(b)do if c[d]and not h.number then h.number=c[d]end end end local d=0 for i,h in ipairs(b)do if not h.number then h.number=d+1 end d=g(d,h.number)end return b end function a.describe(d)local b={
  69. }for h,c in ipairs(d)do b[#b+1]=c.number..": "..c.name end return table.concat(b,", ")end function a.measure(c)if not c.isMonitor then return end local b={}pcall(function()for k,d in ipairs(a.SCALES)
  70. do c.out.setTextScale(d)local h,i=c.out.getSize()b[#b+1]={d,h,i}end end)if#b==0 then b[1]={0.5,1,1}end c.sizes=b end function a.chooseScale(b,m,n,k)local h,l for t=#b.sizes,1,-1 do local c=b.sizes[t]
  71. local o,p=c[2],c[3]local q=j(m,o)*j(n,p)local r,s=1-q/(m*n),1-q/(o*p)local i,d if k=="bars"then i,d=r==0,-c[1]elseif k=="fill"then i,d=s==0,c[1]else i,d=true,2*r+s end if i and(not l or d<l-1e-9)then h
  72. ,l=c,d end end if not h then h=k=="bars"and b.sizes[1]or b.sizes[#b.sizes]end return h[1]end function a.textScale(c,d,h,i)for k=#c.sizes,1,-1 do local b=c.sizes[k]if b[2]>=d*(i or 1)and b[3]>=h+2 then
  73. return b[1]end end return 0.5 end function a.fitScale(c,d,h)local b=g(0.5,j(5,f(d*2+0.5)/2))while true do if c.getTextScale()~=b then c.setTextScale(b)end if b<=0.5 or h(c.getSize())then return b end b
  74. =b-0.5 end end function a.edgeColour(c)local d,k,l={},"f",-1 local function h(b)d[b]=(d[b]or 0)+1;if d[b]>l then k,l=b,d[b]end end for b=1,c.h do local i=c.rows[b][3]if b==1 or b==c.h then for m=1,c.w
  75. do h(i:sub(m,m))end else h(i:sub(1,1));h(i:sub(-1))end end return k end function a.drawImage(d,b,t)local h=d.out if not b.rows then e.rowsFromPixels(b)end if d.isMonitor then if not d.sizes then a.
  76. measure(d)end h.setTextScale(a.chooseScale(d,b.w,b.h,t or"compromise"))end local i,k=h.getSize()local c=window.create(h,1,1,i,k,false)e.applyPalette(c,b.palette)c.setBackgroundColour(2^e.HEX_TO_IDX[a.
  77. edgeColour(b)])c.clear()local l=f((i-b.w)/2)+1 local u=f((k-b.h)/2)+1 local q=l<1 and 1-l or 0 local r=b.composed or e.compose(b)b.composed=r for s=1,b.h do local m=u+s-1 if m>=1 and m<=k then local n
  78. =r[s]local o,p=q+1,q+i c.setCursorPos(g(1,l),m)c.blit(n[1]:sub(o,p),n[2]:sub(o,p),n[3]:sub(o,p))end end c.setVisible(true)end function a.drawText(c,d,l)local b=c.out local h=0 for k,i in ipairs(d)do h
  79. =g(h,#i)end if c.isMonitor then if not c.sizes then a.measure(c)end b.setTextScale(a.textScale(c,h,#d,2))end e.applyPalette(b)b.setBackgroundColour(colors.black)b.clear()local m,n=b.getSize()local o=f((
  80. n-#d)/2)+1 for i,k in ipairs(d)do b.setTextColour(i==1 and(l or colors.white)or colors.lightGray)b.setCursorPos(f((m-#k)/2)+1,o+i-1)b.write(k)end end function a.clear(b)e.applyPalette(b.out)b.out.
  81. setBackgroundColour(colors.black)b.out.setTextColour(colors.white)b.out.clear()b.out.setCursorPos(1,1)end function a.try(b,c,...)return(pcall(b,c,...))end return a end package.preload["lib.editor.app"
  82. ]=function(...)local f=require("lib.formats.adimg")local i=require("lib.ui")local d={}d.MODULES={"lib.editor.state","lib.editor.view","lib.editor.text","lib.editor.chrome","lib.editor.dialogs",
  83. "lib.editor.files","lib.editor.commands","lib.editor.input",}function d.run(j,c,k)local b=term.current()if c then local a=peripheral.wrap(c)if not(a and a.isColour and a.isColour())then printError(
  84. "No advanced monitor on side '"..tostring(c).."'");return end a.setTextScale(math.max(0.5,math.min(5,math.floor(k*2+0.5)/2)))b=a elseif not term.isColour()then printError(
  85. "adpaint needs an advanced computer (colour + mouse)");return end local a={display=b}a.W,a.H=b.getSize()a.win=window.create(b,1,1,a.W,a.H,true)local l=term.redirect(a.win)function a.onResize()a.W,a.H=
  86. b.getSize()a.win.reposition(1,1,a.W,a.H)for g,e in ipairs(a.docs)do a.clampView(e)end end a.U=i.new({win=a.win,monitor=c and peripheral.getName(b)or nil,draw=function(e)a.draw(e)end,area=function()
  87. return 2,a.viewH()end,onEvent=function(e)if e[1]=="file_transfer"then a.pendingFiles[#a.pendingFiles+1]=e[2]else a.onResize()end end,})for g,e in ipairs(d.MODULES)do require(e)(a)end local e if c then
  88. local g=b.getTextScale()local h=a.fitTextScale(g)if h<g then e="Text scale "..h.." (max)"end end local function g()a.computeUi()for m,h in ipairs(j)do a.openPath(h)end if#a.docs==0 then a.addDoc(a.
  89. blankDoc())a.setStatus(a.W>=44 and"Tip: drop image files here (.jpg, .png, or .bmp)"or"Tip: drop images here",6)end if e then a.setStatus(e,6)end a.cur=1 a.applyPalette()a.clampView(a.doc())while a.
  90. running do a.draw()a.handleEvent({os.pullEventRaw()})end end local h,m=pcall(g)term.redirect(l)f.applyPalette(b,f.DEFAULT_PALETTE)b.setBackgroundColour(colours.black)b.setTextColour(colours.white)b.
  91. clear()b.setCursorPos(1,1)if not h then printError(m)end end return d end package.preload["lib.editor.chrome"]=function(...)local m=require("lib.formats.adimg")local r,n=math.max,math.min local s={{
  92. "\27",-1,0},{"\24",0,-1},{"\25",0,1},{"\26",1,0}}local function f(...)local a={}for d,b in ipairs({...})do for e,c in ipairs(b)do a[#a+1]=c end end return a end return function(a)local c,b=a.U,a.U.col
  93. local d=c.blitAt a.hits={tabs={},tools={},settings={}}function a.swatchWidth()return a.W>=40 and 2 or 1 end local function t()local j,g=a.win,a.W j.setCursorPos(1,1)j.setBackgroundColour(2^b.dark);j.
  94. clearLine()local h={}d(1,1," = ",b.text,a.menuOpen and b.accent or b.bar)local e=5 for k,l in ipairs(a.docs)do local i=" "..a.docName(l)..(l.dirty and"*"or"").." "if e+#i+1>g-8 then break end local o=
  95. k==a.cur and b.bar or b.dark d(e,1,i,k==a.cur and b.text or b.dim,o)d(e+#i,1,"\215",b.dim,o)h[#h+1]={e,e+#i-1,k,e+#i}e=e+#i+2 end d(n(e,g-7),1,"+",b.text,b.dark)h.plus=n(e,g-7)local i=a.doc()local k=#
  96. i.undo>0 or(a.op~=nil and a.op.changed)d(g-5,1," \27 ",k and b.text or b.dim,b.bar)d(g-2,1," \26 ",#i.redo>0 and b.text or b.dim,b.bar)h.undo,h.redo=g-5,g-2 a.hits.tabs=h end local function u()local j
  97. ,k,h=a.win,a.W,a.sel local i=a.H-1 j.setCursorPos(1,i)j.setBackgroundColour(2^b.bar);j.clearLine()local l=a.swatchWidth()local o=a.palette()for e=0,15 do local g=" "local p=m.dist(o[e],0)>m.dist(o[e],
  98. 0xFFFFFF)and 15 or 0 if h then if e==h.box.fg then g="F"end if e==h.box.bg then g=g=="F"and"*"or"B"end else if e==a.primary then g="L"end if e==a.secondary then g=g=="L"and"*"or"R"end end d(1+e*l,i,(g
  99. .."  "):sub(1,l),p,e)end local e=16*l+2 a.hits.slot=nil if c.monitor then local p=h and{"F","B"}or{"L","R"}local g=" Sets "..p[a.touchSlot].." "if e+#g+8<=k then d(e,i,g,b.dark,b.hi)a.hits.slot={e,e+#
  100. g-1}e=e+#g+1 end end local p=a.doc()local q=" "..p.img.w.."x"..p.img.h.." "local g=k-#q+1 if g>e then d(g,i,q,b.text,b.dark)a.hits.size={g,k}else a.hits.size=nil end end local function k()if not(a.
  101. status and os.clock()<a.statusUntil)then return end local e=" "..a.status:sub(1,a.W-2).." "d(a.W-#e+1,a.viewH()+1,e,b.text,b.bar)end local function p()local i={}local h,g={},{}for j,e in ipairs(a.
  102. TOOLS)do h[#h+1]={e[3]..e[2],e[1],a.tool==e[1]}g[#g+1]={" "..e[3].." ",e[1],a.tool==e[1],0}end g[#g][4]=1 for j,e in ipairs({h,g})do e[#e+1]={"Zoom",a.toggleZoom,a.doc().zoom>1}e[#e+1]={a.showText and
  103. "Txt"or"txt",a.toggleText,a.showText}end c.drawRow(a.H,c.pickLayout(h,g),i)a.hits.tools=i end local function h(i,j)local e={}for l,g in ipairs(s)do e[#e+1]={j and(" "..g[1].." ")or g[1],function()i(g[
  104. 2],g[3])end,false,0}end e[#e][4]=1 return e end local function g(e,i,j,l,o)if o then return{{e},{" - ",j},{("%2d"):format(i)},{" + ",l,false,2}}end return{{e,nil,false,0},{"-",j,false,0},{tostring(i),
  105. nil,false,0},{"+",l}}end function a.minScreenWidth()return c.rowWidth(f(g("Size",16),g("Dither",8)))-1 end local function q()local l={}local e local i=a.sel if i then local j=function()i.box.bg=nil;a.
  106. textChanged()end e=c.pickLayout(f({{" No bg ",j,i.box.bg==nil},{" Chars ",a.charPicker},{" Delete ",a.deleteSelectedBox},{" Done ",a.deselect,false,2},{"Move"}},h(a.moveBox,true)),f({{"No bg",j,i.box.
  107. bg==nil},{"Chars",a.charPicker},{"Del",a.deleteSelectedBox},{"Done",a.deselect}},h(a.moveBox,false)))elseif a.tool=="brush"then local j,o=a.brushSize,a.dither e=c.pickLayout(f(g("Size",j,a.
  108. brushSmaller,a.brushBigger,true),g("Dither",o,a.ditherLess,a.ditherMore,true)),f(g("Size",j,a.brushSmaller,a.brushBigger),g("Dither",o,a.ditherLess,a.ditherMore)))elseif a.tool=="pan"then e=c.
  109. pickLayout(f({{c.monitor and"Tap a spot to centre it, or"or"Drag, or use"}},h(a.panStep,true)),f({{"Pan"}},h(a.panStep,true)),h(a.panStep,false))else local j=c.monitor and"Tap"or"Click"local o=({fill=
  110. j.." an area to fill it",pick=j.." to pick a colour",text=j.." to add or select a text box"})[a.tool]or""e={{o}}end c.drawRow(a.H-2,e,l)a.hits.settings=l end local function v()local j=a.menuItems local
  111. w=a.W local e=0 for l,i in ipairs(j)do e=r(e,#i[1]+#i[2]+3)end local l=e+2<=w if l then e=e+2 end for x,i in ipairs(j)do local o=l and((i[4]or" ").." "..i[1])or i[1]local y=" "..o..string.rep(" ",e-#o
  112. -#i[2]-2)..i[2].." "d(1,x+1,y,b.text,b.bar)end a.hits.menuWidth=e end local function l()local e=a.sel local o,w=e.box.x-1+e.col,e.box.y-1+e.line-1 local i,j=a.cellToScreen(o,w)if i>=1 and i<=a.W and j
  113. >=2 and j<=a.viewH()+1 then a.win.setCursorPos(i,j)a.win.setTextColour(2^e.box.fg)a.win.setCursorBlink(true)end end function a.draw(i)local e=a.win e.setVisible(false)if not a.doc()then e.
  114. setBackgroundColour(2^b.dark)e.clear()e.setCursorBlink(false)k()if i then i()end e.setVisible(true)return end a.drawCanvas()t()u()p()q()k()if a.menuOpen then v()end e.setCursorBlink(false)if a.sel and
  115. not a.menuOpen then l()end if i then i()end e.setVisible(true)end end end package.preload["lib.editor.commands"]=function(...)local d,e=math.max,math.min return function(a)function a.brushSmaller()a.
  116. brushSize=d(1,a.brushSize-1)end function a.brushBigger()a.brushSize=e(16,a.brushSize+1)end function a.ditherLess()a.dither=d(1,a.dither-1)end function a.ditherMore()a.dither=e(8,a.dither+1)end function
  117. a.toggleText()a.showText=not a.showText end function a.nextTab()a.cur=a.cur%#a.docs+1;a.applyPalette()end a.menuItems={{"New","^N",a.newCanvas,"\43"},{"Open...","^O",a.openDialog},{"Save","^S",
  118. function()a.saveDoc()end},{"Save as...","",function()a.saveDoc(nil,true)end},{"Import image...","",a.importDialog,"\6"},{"Resize canvas...","",a.resizeCanvas,"\172"},{"Scale image...","",a.scaleImage,
  119. "\3"},{"Edit colour L...","^E",a.editColour,"\127"},{"Reset colours","",a.resetPalette},{"Undo","^Z",function()a.undo()end,"\27"},{"Redo","^Y",function()a.undo(true)end,"\26"},{"Help","",a.showHelp,
  120. "\63"},{"Close tab","^W",function()a.closeDoc(a.cur)end,"\215"},{"Quit","^Q",a.quit},}if a.U.monitor then local b=1 for c,f in ipairs(a.menuItems)do if f[1]=="Reset colours"then b=c+1 end end table.
  121. insert(a.menuItems,b,{"Text scale...","",a.textScaleDialog})end local function c(b,f,g,h)return function()if a.U.mods.shift then a.pan(g,h)else a.pan(b,f)end end end local function b(f)return function(
  122. )a.tool=f end end a.SHORTCUTS={[keys.b]=b("brush"),[keys.f]=b("fill"),[keys.t]=b("text"),[keys.i]=b("pick"),[keys.p]=b("pan"),[keys.leftBracket]=a.brushSmaller,[keys.rightBracket]=a.brushBigger,[keys.
  123. comma]=a.ditherLess,[keys.period]=a.ditherMore,[keys.z]=a.toggleZoom,[keys.h]=a.toggleText,[keys.e]=a.editColour,[keys.tab]=a.nextTab,[keys.left]=c(-2,0,-8,0),[keys.right]=c(2,0,8,0),[keys.up]=c(0,-2,
  124. 0,-6),[keys.down]=c(0,2,0,6),}a.CTRL={[keys.n]=a.newCanvas,[keys.o]=a.openDialog,[keys.s]=function()a.saveDoc()end,[keys.w]=function()a.closeDoc(a.cur)end,[keys.q]=a.quit,[keys.z]=function()a.undo()
  125. end,[keys.y]=function()a.undo(true)end,[keys.e]=a.editColour,}end end package.preload["lib.editor.dialogs"]=function(...)local y=require("lib.formats.adimg")local z=require("lib.paint")local I=require(
  126. "lib.displays")local l,g,h=math.floor,math.max,math.min local J={"adpaint - .adi advertisement image editor","","Left click = primary colour, right = secondary.",
  127. "Middle drag / arrows / scroll (shift: sideways) pans.","Drag image files onto the window to import them.","","B brush  F fill  T text  I pick  P pan","E (or Ctrl+E) edit colour L, with live preview",
  128. "[ ] brush size  , . brush dither  Z zoom  H text layer","Ctrl: N new  O open  S save  W close tab  Q quit","      Z undo  Y redo  E edit colour L  Tab next tab",
  129. "Arrows at the top right: undo / redo.","","Text tool: click to add/select a box, type to edit,","drag or use the arrows to move. Palette L/R = text /","background. Chars inserts special characters.",
  130. "Monitors: tap 'Sets L' to switch L/R; Pan: tap a spot.","Click the size (bottom right) to resize or scale.","Run 'adpaint help' in the shell for command usage.",}local i={}for c,a in ipairs({{0x01,
  131. 0x08},{0x0B,0x0C},{0x0E,0x1F},{0x7F,0x7F},{0xA6,0xA6},{0xA9,0xB5},{0xB7,0xBF},{0xD7,0xD7},{0xF7,0xF8}})do for b=a[1],a[2]do i[#i+1]=b end end local A={"R","G","B"}local function B(a)a=a:gsub("^%s*#?",
  132. ""):gsub("%s+$","")if a:match("^%x%x%x%x%x%x$")then return tonumber(a,16)end if a:match("^%x%x%x$")then local b,c,d=a:sub(1,1),a:sub(2,2),a:sub(3,3)return tonumber(b..b..c..c..d..d,16)end end local
  133. function u(c,d,a)local b=c.prompt(d,tostring(a))if not b then return nil end return g(1,h(512,l(tonumber(b)or a)))end return function(a)local b,d=a.U,a.U.col local p=b.blitAt function a.showHelp()b.
  134. message(J)end function a.chooseScaleMode()local c=b.choose("New aspect ratio:",{{"Bars","fit"},{"Fill","fill"},{"Stretch","stretch"},{"Cancel","c"}})if c~="fit"and c~="fill"and c~="stretch"then return
  135. nil end if c=="stretch"then return c,0.5,0.5 end local e,f=b.chooseAnchor(c=="fit"and"Place image at:"or"Keep this part:",5)if not e then return nil end return c,e,f end local function C(j)local f=a.
  136. doc()a.deselect()a.beginOp("full")local c=f.img local e,k=j(c)c.px,c.w,c.h,c.pw,c.ph,c.text=e.px,e.w,e.h,e.pw,e.ph,k a.endOp()a.clampView(f)end function a.resizeCanvas()local c=a.doc().img local e=u(b
  137. ,"Canvas width (cells): ",c.w)if not e then return end local f=u(b,"Canvas height (cells): ",c.h)if not f then return end if e==c.w and f==c.h then return end local j,k=b.chooseAnchor("Keep image at:"
  138. )if not j then return end C(function(m)return z.resizeCanvas(m,e,f,j,k,a.secondary)end)a.setStatus(("Canvas is now %dx%d"):format(e,f))end function a.scaleImage()local c=a.doc().img local e=u(b,
  139. "Image width (cells): ",c.w)if not e then return end local k=g(1,l(c.h*e/c.w+0.5))local m=b.prompt("Image height (cells): ",tostring(k))if not m then return end local f=g(1,h(512,l(tonumber(m)or c.h))
  140. )if e==c.w and f==c.h then return end local j,n,o="stretch",0.5,0.5 if f~=k then j,n,o=a.chooseScaleMode()if not j then return end end C(function(q)return z.scale(q,e,f,j,n,o,a.secondary)end)a.
  141. setStatus(("Image scaled to %dx%d"):format(e,f))end function a.sizeMenu()local c=b.choose("Change size of:",{{"Canvas","canvas"},{"Image","image"},{"Cancel","c"}})if c=="canvas"then a.resizeCanvas()
  142. elseif c=="image"then a.scaleImage()end end function a.newCanvas()local c=b.prompt("Size WxH: ",a.NEW_W.."x"..a.NEW_H)if not c then return end local e,f=c:match("(%d+)%s*[xX, ]%s*(%d+)")a.addDoc(a.
  143. blankDoc(g(1,h(512,tonumber(e)or a.NEW_W)),g(1,h(512,tonumber(f)or a.NEW_H))))end function a.charPicker()if not a.sel then return end local c=1 local n,j,e b.modal(function()j={}e=g(1,h(16,l((a.W-4)/3
  144. )))local s=math.ceil(#i/e)local m=e*3+4 local o,q,t=b.panel(m,s+4)a.win.setCursorPos(o+2,q+1)a.win.setTextColour(2^d.text)a.win.write(("Insert character"):sub(1,m-4))for f,v in ipairs(i)do local r=o+2
  145. +((f-1)%e)*3 local k=q+3+l((f-1)/e)if k<=t then p(r,k," "..string.char(v).." ",f==c and d.dark or d.text,f==c and d.hi or d.dark)j[f]={r,k}end end n=b.bottomBar(("Character %d (0x%02X)"):format(i[c],i
  146. [c]),{{"Close","close"}})a.win.setCursorBlink(false)end,function(k,r,m,o)if k[1]=="terminate"then return true elseif r then if o==a.H then if b.hitButton(n,m)=="close"then return true end else for q,f
  147. in pairs(j)do if o==f[2]and m>=f[1]and m<=f[1]+2 then c=q a.textInsert(string.char(i[q]))end end end elseif k[1]=="key"then local f=k[2]if f==keys.enter or f==keys.numPadEnter then a.textInsert(string
  148. .char(i[c]))elseif f==keys.tab then return true elseif f==keys.left then c=g(1,c-1)elseif f==keys.right then c=h(#i,c+1)elseif f==keys.up then c=g(1,c-e)elseif f==keys.down then c=h(#i,c+e)end end end
  149. )end local function K(f,c,j)local e=b.prompt(("Colour %d hex RRGGBB: "):format(f),("%06x"):format(c),nil,function(e)j(B(e)or c)end)return e and B(e)end local function L(f,j)local k=a.palette()a.
  150. beginOp("full")local c={}for e=0,15 do c[e]=k[e]end c[f]=j a.doc().img.palette=c a.endOp()a.applyPalette()end function a.editColour()local q=a.win local m=a.primary local D=a.palette()[m]local c={y.
  151. rgb(D)}local f,e=1,nil local function r()return c[1]*65536+c[2]*256+c[3]end local function v(j)q.setPaletteColour(2^m,j)end a.computeUi(m)local E,s local function w(j,k)c[j]=g(0,h(255,c[j]+k));f=j;e=
  152. nil end local function M(j)f,e=j,nil local n=b.prompt(A[j].." (0-255): ",tostring(c[j]),nil,function(o)local k=tonumber(o)if k and k>=0 and k<=255 then local n={c[1],c[2],c[3]}n[j]=l(k)v(n[1]*65536+n[
  153. 2]*256+n[3])end end)local k=tonumber(n or"")if k then c[j]=g(0,h(255,l(k)))end end local x repeat v(r())x=b.modal(function()s={}local F=29 local j,t=b.panel(F,9)q.setCursorPos(j+2,t+1)q.setTextColour(
  154. 2^d.text)q.write(("Colour %d  #%06x"):format(m,r()))p(j+F-6,t+1,"    ",d.text,m)local function o(k,n,G,N,H)p(k,n,G,H and d.dark or d.text,H and d.hi or d.dark)s[#s+1]={k,n,k+#G-1,N}end for k=1,3 do
  155. local n=t+2+k p(j+2,n,A[k],d.text,d.bar)o(j+4,n,"<<",{k,-16})o(j+7,n,"<",{k,-1})o(j+9,n,(" %3d "):format(c[k]),{k,"type"},k==f)o(j+15,n,">",{k,1})o(j+17,n,">>",{k,16})local G=l(c[k]/255*7+0.5)p(j+20,n
  156. ,string.rep("\127",G)..string.rep(" ",7-G),d.text,d.dark)end o(j+2,t+7," Hex... ","hex")E=b.bottomBar("Edit colour L",{{"OK","ok"},{"Cancel","cancel"}})end,function(k,G,o,t)if k[1]=="terminate"then
  157. return true,false elseif G then if t==a.H then local j=b.hitButton(E,o)if j=="ok"then return true,true elseif j=="cancel"then return true,false end else for H,n in ipairs(s)do if t==n[2]and o>=n[1]and
  158. o<=n[3]then local j=n[4]if j=="hex"then local F=K(m,r(),v)if F then c={y.rgb(F)}end elseif j[2]=="type"then M(j[1])else w(j[1],j[2])end end end end elseif k[1]=="char"and k[2]:match("^%d$")then e=(e or
  159. "")..k[2]c[f]=h(255,tonumber(e))if#e>=3 then e=nil end elseif k[1]=="key"then local j=k[2]if j==keys.enter or j==keys.numPadEnter then return true,true elseif j==keys.up then f=(f-2)%3+1;e=nil elseif j
  160. ==keys.down or j==keys.tab then f=f%3+1;e=nil elseif j==keys.left then w(f,b.mods.shift and-16 or-1)elseif j==keys.right then w(f,b.mods.shift and 16 or 1)elseif j==keys.backspace then e=e and e:sub(1
  161. ,-2)or tostring(c[f]):sub(1,-2)c[f]=tonumber(e)or 0 end end return true,nil end)until x~=nil local j=r()if not x or j==D then a.applyPalette();return end L(m,j)end function a.resetPalette()if not a.
  162. doc().img.palette then return end a.beginOp("full")a.doc().img.palette=nil a.endOp()a.applyPalette()end local f=6 function a.screenFits()local c,e=a.display.getSize()return c>=a.minScreenWidth()and e
  163. >=f end function a.fitTextScale(c)local e=a.minScreenWidth()local j=I.fitScale(a.display,c,function(j,k)return j>=e and k>=f end)a.onResize()return j end function a.textScaleDialog()local j=b.prompt(
  164. "Text scale (0.5-5): ",tostring(a.display.getTextScale()))local c=tonumber(j or"")if not c then return end c=g(0.5,h(5,l(c*2+0.5)/2))local e=a.fitTextScale(c)settings.set("adpaint.text_scale",e)
  165. settings.save()if e<c then a.setStatus("Text scale "..e.." (max, saved)",6)else a.setStatus("Text scale "..e.." (saved as default)")end end end end package.preload["lib.editor.files"]=function(...)
  166. local k=require("lib.formats.adimg")local l=require("lib.cli")local z,m,h=math.floor,math.max,math.min return function(a)local c=a.U local e function a.completePath(b)return fs.complete(b,shell.dir(),
  167. true,true)end local function s(b)a.setStatus(b,60)a.draw()end function a.yielder()return l.yielder(function(b)if b[1]=="file_transfer"then a.pendingFiles[#a.pendingFiles+1]=b[2]end end)end function a.
  168. importData(f,i)e=e or require("lib.convert")local A=e.detect(i)if A=="adimg"then local b,g=k.parse(i,true)if not b then a.setStatus(f..": "..g);return end local d=a.newDoc(b,shell.resolve(fs.getName(f
  169. )))a.addDoc(d)return d end local d,B=e.info(i)if not d then a.setStatus(f..": "..tostring(B));return end local t=e.fitCells(d,a.W,a.viewH())a.setStatus(("%s: %dx%d %s"):format(fs.getName(f),d.dw,d.dh,
  170. d.format:upper()),30)local b=c.prompt("Width (cells): ",tostring(t))if not b then return end local u=m(1,h(512,tonumber(b)or t))local F,n=e.fitCells(d,u,math.huge)b=c.prompt("Height (cells): ",
  171. tostring(n))if not b then return end local v=m(1,h(512,tonumber(b)or n))local o,w,x="stretch",0.5,0.5 if v~=n then o,w,x=a.chooseScaleMode()if not o then return end end local g={{"Dither",true},{
  172. "None",false}}if d.indexed then g[1],g[2]=g[2],g[1]end g[3]={"Cancel","c"}local p=c.choose("Dithering:",g)if p==nil or p=="c"then return end local q=c.choose("Palette:",{{"Optimised",true},{"Default",
  173. false},{"Cancel","c"}})if q==nil or q=="c"then return end b=c.prompt("Colours to use (2-16): ","16")if not b then return end local C=m(2,h(16,z(tonumber(b)or 16)))s("Importing...")local D,j,y=pcall(e.
  174. import,i,{w=u,h=v,dither=p,optimise=q,colours=C,mode=o,ax=w,ay=x,background=a.palette()[a.secondary],yield=a.yielder(),status=function(j)s(j)end,})if not D then y=j;j=nil end if not j then a.setStatus(
  175. "Import failed: "..tostring(y),8);return end local E=fs.getName(f):gsub("%.[^%.]*$","")local r=a.newDoc(j,shell.resolve(E..".adi"))r.dirty=true a.addDoc(r)a.setStatus("Imported "..fs.getName(f))return
  176. r end function a.openPath(b)b=shell.resolve(b)for f,d in ipairs(a.docs)do if d.path==b then a.cur=f;a.applyPalette();return d end end if not fs.exists(b)or fs.isDir(b)then local d=a.blankDoc()d.path=b
  177. a.addDoc(d)a.setStatus("New file "..fs.getName(b))return d end local d=l.readFile(b)if not d then a.setStatus("Cannot read "..b);return end if d:sub(1,6)=="ADIMG "then local f,i=k.parse(d,true)if not f
  178. then a.setStatus(i);return end local g=a.newDoc(f,b)a.addDoc(g)return g end return a.importData(b,d)end function a.openDialog()local b=c.prompt("Open: ","",a.completePath)if b and b~=""then a.openPath(
  179. b)end end function a.importDialog()local b=c.prompt("Image file: ","",a.completePath)if b and b~=""then local d=l.readFile(shell.resolve(b))if d then a.importData(b,d)else a.setStatus("Cannot read "..
  180. b)end end end function a.saveDoc(d,g)d=d or a.doc()local b=d.path if g or not b then local f=c.prompt("Save as: ",b and("/"..b)or"untitled.adi",a.completePath)if not f or f==""then return false end b=
  181. shell.resolve(f)if fs.isReadOnly(b)then a.setStatus("Read-only location");return false end if fs.exists(b)and b~=d.path and not c.confirm(fs.getName(b).." exists. Overwrite?")then return false end end
  182. if a.sel and a.docs[a.cur]==d then a.deselect()end local f,i=k.save(d.img,b)if not f then a.setStatus("Save failed: "..tostring(i));return false end d.path,d.dirty=b,false a.setStatus("Saved "..fs.
  183. getName(b))return true end function a.closeDoc(b)local d=a.docs[b]if b==a.cur then a.deselect()end if d.dirty then a.cur=b;a.applyPalette()local f=c.choose("Save "..a.docName(d).."?",{{"Save","y"},{
  184. "Discard","n"},{"Cancel","c"}})if f=="y"then if not a.saveDoc(d)then return false end elseif f~="n"then return false end end table.remove(a.docs,b)if#a.docs==0 then if not a.running then return true
  185. end a.addDoc(a.blankDoc())end a.cur=h(a.cur,#a.docs)if b<a.cur then a.cur=a.cur-1 end a.applyPalette()return true end function a.quit()a.running=false while#a.docs>0 do if not a.closeDoc(#a.docs)then a
  186. .running=true;return end end end function a.handleFiles(d)for i,b in ipairs(d.getFiles())do local f=b.getName()local g=b.readAll()or""b.close()a.importData(f,g)end end end end package.preload[
  187. "lib.editor.input"]=function(...)local q=require("lib.formats.adimg")local f=require("lib.paint")local r,s=math.floor,math.min return function(a)local d=a.U local function t(k,m,n)local o=a.doc()local
  188. i=not a.zoomInfo(o).zw local g,h,c,e=a.screenToImage(m,n)if a.tool=="pan"and d.monitor then a.centreOn(g,h)return end if k==3 or a.tool=="pan"then a.drag={kind="pan",x=m,y=n}return end if d.monitor and
  189. a.tool=="pick"then k=a.touchSlot end local l=k==1 and a.primary or a.secondary local b=o.img if a.tool=="brush"then a.deselect()a.beginOp()local j,p=i and c or g,i and e or h f.stamp(b,j,p,l,i,a.
  190. brushSize,a.dither,a.setPx)a.drag={kind="brush",ux=j,uy=p,color=l,native=i}elseif a.tool=="fill"then a.deselect()if g<0 or h<0 or g>=b.pw or h>=b.ph then return end if i then g,h=select(2,f.
  191. cellMajority(b,c,e))end a.beginOp()f.floodFill(b,g,h,l,a.setPx)a.endOp()elseif a.tool=="pick"then local j=q.get(b,g,h)if j then if i then j=f.cellMajority(b,c,e)end if k==1 then a.primary=j else a.
  192. secondary=j end end elseif a.tool=="text"then if c<0 or e<0 or c>=b.w or e>=b.h then a.deselect();return end local j=f.boxAt(b,c,e)if j then local p=e+1-j.y+1 a.selectBox(j,p,s(#j.lines[p],c+1-j.x))
  193. else a.newBox(c,e)end a.drag={kind="text",cx=c,cy=e}end end local function n(c,e)local b=a.drag if not b then return end if b.kind=="pan"then a.pan(b.x-c,b.y-e)b.x,b.y=c,e return end local k,l,g,h=a.
  194. screenToImage(c,e)if b.kind=="brush"then local i,j=b.native and g or k,b.native and h or l if i~=b.ux or j~=b.uy then local m=a.doc().img f.line(b.ux,b.uy,i,j,function(o,p)f.stamp(m,o,p,b.color,b.
  195. native,a.brushSize,a.dither,a.setPx)end)b.ux,b.uy=i,j end elseif b.kind=="text"and a.sel then local i,j=g-b.cx,h-b.cy if i~=0 or j~=0 then a.moveBox(i,j)b.cx,b.cy=g,h end end end local function l()if a
  196. .drag and a.drag.kind=="brush"then a.endOp()end a.drag=nil end local function i(b,c)return b and c>=b[1]and c<=b[2]end local function m(j,b,c)local e,k=a.hits,a.H if a.menuOpen then a.menuOpen=false
  197. local g=a.menuItems[c-1]if g and b<=(e.menuWidth or 0)then g[3]()end return end if c==1 then local g=e.tabs if b<=3 then a.menuOpen=true;return end if b>=g.undo and b<=g.undo+2 then a.undo();return end
  198. if b>=g.redo and b<=g.redo+2 then a.undo(true);return end for o,h in ipairs(g)do if b>=h[1]and b<=h[2]then a.deselect();a.cur=h[3];a.applyPalette();return end if b==h[4]then a.closeDoc(h[3]);return end
  199. end if b==g.plus then a.addDoc(a.blankDoc())end elseif c==k-2 then for h,g in ipairs(e.settings)do if i(g,b)then g[3]();return end end elseif c==k-1 then if i(e.size,b)then a.sizeMenu();return end if i(
  200. e.slot,b)then a.touchSlot=3-a.touchSlot;return end if d.monitor then j=a.touchSlot end local g=r((b-1)/a.swatchWidth())if g>=0 and g<=15 then if a.sel then if j==1 then a.sel.box.fg=g else a.sel.box.
  201. bg=g end a.textChanged()elseif j==1 then a.primary=g else a.secondary=g end end elseif c==k then for h,g in ipairs(e.tools)do if i(g,b)then if type(g[3])=="function"then g[3]()else a.deselect();a.tool
  202. =g[3]end return end end else t(j,b,c)end end local function g(b)local c=d.mods if b==keys.leftCtrl or b==keys.rightCtrl then c.ctrl=true;return end if b==keys.leftShift or b==keys.rightShift then c.
  203. shift=true;return end if c.ctrl then if a.CTRL[b]then a.deselect();a.CTRL[b]()end return end if a.menuOpen then a.menuOpen=false;return end if a.sel then a.textKey(b)return end if a.SHORTCUTS[b]then a
  204. .SHORTCUTS[b]()end end function a.handleEvent(b)local c=b[1]local e=not d.monitor if c=="terminate"then a.quit()elseif c=="mouse_click"and e then m(b[2],b[3],b[4])elseif c=="mouse_drag"and e then n(b[
  205. 3],b[4])elseif c=="mouse_up"and e then l()elseif c=="monitor_touch"and d.monitor and b[2]==d.monitor then m(1,b[3],b[4]);l()elseif c=="mouse_scroll"and e then if b[4]>=2 and b[4]<=a.viewH()+1 then if d
  206. .mods.shift then a.pan(b[2]*3,0)else a.pan(0,b[2]*2)end end elseif c=="key"then g(b[2])elseif c=="key_up"then d.trackModifiers(b)elseif c=="char"and a.sel and not d.mods.ctrl then a.textInsert(b[2])
  207. elseif c=="paste"and a.sel then a.textInsert(b[2])elseif c=="file_transfer"then a.handleFiles(b[2])elseif c=="term_resize"or c=="monitor_resize"then a.onResize()if d.monitor and c=="monitor_resize"and
  208. not a.screenFits()and a.display.getTextScale()>0.5 then a.fitTextScale(a.display.getTextScale()-0.5)end end while#a.pendingFiles>0 do a.handleFiles(table.remove(a.pendingFiles,1))end end end end
  209. package.preload["lib.editor.state"]=function(...)local e=require("lib.formats.adimg")local b=require("lib.history")return function(a)a.docs,a.cur={},1 a.tool="brush"a.primary,a.secondary=15,0 a.
  210. brushSize,a.dither=1,8 a.showText=true a.running=true a.status,a.statusUntil=nil,0 a.menuOpen=false a.sel=nil a.drag=nil a.op=nil a.touchSlot=1 a.pendingFiles={}a.ZOOMS={{name="1x"},{name="6x",zw=3,zh
  211. =2},}a.NEW_W,a.NEW_H=18,24 a.TOOLS={{"brush","Brush","\7"},{"fill","Fill","\21"},{"text","Text","\20"},{"pick","Pick","\35"},{"pan","Pan","\18"},}function a.doc()return a.docs[a.cur]end function a.
  212. palette(c)c=c or a.doc()return c and c.img.palette or e.DEFAULT_PALETTE end function a.setStatus(c,d)a.status=c a.statusUntil=os.clock()+(d or 4)end function a.viewH()return a.H-4 end function a.
  213. computeUi(c)a.U.pickColours(a.palette(),c)end local function h()local i=a.palette()local f,g=math.huge,-1 for d=0,15 do local j,k,l=e.rgb(i[d])local c=j*0.3+k*0.59+l*0.11 if c<f then a.primary,f=d,c
  214. end if c>g then a.secondary,g=d,c end end end local c=nil function a.applyPalette()e.applyPalette(a.win,a.palette())a.computeUi()local d=a.doc()if d~=c then if c then c.primary,c.secondary=a.primary,a
  215. .secondary end c=d if d.primary then a.primary,a.secondary=d.primary,d.secondary else h()end end end function a.newDoc(d,f)return b.attach({img=d,path=f,dirty=false,zoom=1,vx=0,vy=0})end function a.
  216. docName(d)return d.path and fs.getName(d.path)or"untitled"end function a.addDoc(d)a.docs[#a.docs+1]=d a.cur=#a.docs a.sel=nil a.applyPalette()end function a.blankDoc(d,f)return a.newDoc(e.new(d or a.
  217. NEW_W,f or a.NEW_H,0))end function a.beginOp(d)a.op=b.begin(a.doc(),d)end function a.endOp()b.commit(a.op)a.op=nil end function a.setPx(d,f,g)b.set(a.op,d,f,g)end function a.textChanged()b.touch(a.op)
  218. end function a.undo(d)local f=a.doc()a.deselect()a.endOp()if not b.step(f,d)then a.setStatus(d and"Nothing to redo"or"Nothing to undo")return end a.applyPalette()end end end package.preload[
  219. "lib.editor.text"]=function(...)local g,f=math.max,math.min return function(b)function b.deselect()local a=b.sel if not a then return end local c=b.doc().img.text local d=true for h,e in ipairs(a.box.
  220. lines)do if e:find("%S")then d=false end end if d and not a.box.bg then for e,h in ipairs(c)do if h==a.box then table.remove(c,e);break end end end b.sel=nil b.endOp()end function b.selectBox(c,d,e)
  221. local a=b.sel if a and a.box==c then a.line,a.col=d,e;return end b.deselect()b.beginOp("text")b.sel={box=c,line=d,col=e}end function b.newBox(c,d)b.deselect()local a={x=c+1,y=d+1,fg=b.primary,bg=nil,
  222. lines={""}}b.beginOp("text")table.insert(b.doc().img.text,a)b.sel={box=a,line=1,col=0}end function b.textInsert(c)local a=b.sel local d=a.box local e=d.lines[a.line]d.lines[a.line]=e:sub(1,a.col)..c..
  223. e:sub(a.col+1)a.col=a.col+#c b.textChanged()end function b.textKey(d)local a=b.sel local c=a.box.lines local e=c[a.line]if d==keys.backspace then if a.col>0 then c[a.line]=e:sub(1,a.col-1)..e:sub(a.
  224. col+1);a.col=a.col-1 elseif a.line>1 then local h=c[a.line-1]a.col=#h c[a.line-1]=h..e table.remove(c,a.line);a.line=a.line-1 end b.textChanged()elseif d==keys.delete then if a.col<#e then c[a.line]=e
  225. :sub(1,a.col)..e:sub(a.col+2)elseif a.line<#c then c[a.line]=e..c[a.line+1];table.remove(c,a.line+1)end b.textChanged()elseif d==keys.enter or d==keys.numPadEnter then c[a.line]=e:sub(1,a.col)table.
  226. insert(c,a.line+1,e:sub(a.col+1))a.line,a.col=a.line+1,0 b.textChanged()elseif d==keys.left then a.col=g(0,a.col-1)elseif d==keys.right then a.col=f(#e,a.col+1)elseif d==keys.up and a.line>1 then a.
  227. line=a.line-1;a.col=f(a.col,#c[a.line])elseif d==keys.down and a.line<#c then a.line=a.line+1;a.col=f(a.col,#c[a.line])elseif d==keys.home then a.col=0 elseif d==keys["end"]then a.col=#e elseif d==
  228. keys.tab then b.deselect()else return false end return true end function b.deleteSelectedBox()if not b.sel then return end local a=b.doc().img.text for c,d in ipairs(a)do if d==b.sel.box then table.
  229. remove(a,c);break end end b.textChanged()b.sel=nil b.endOp()end function b.moveBox(d,e)local c=b.doc().img local a=b.sel.box a.x=g(1,f(c.w,a.x+d))a.y=g(1,f(c.h,a.y+e))b.textChanged()end end end package
  230. .preload["lib.editor.view"]=function(...)local b=require("lib.render")local f,j=math.floor,math.max return function(a)local d=a.U.col function a.zoomInfo(c)return a.ZOOMS[(c or a.doc()).zoom]end local
  231. function c(e)local g=a.zoomInfo(e)return{vx=e.vx,vy=e.vy,zw=g.zw,zh=g.zh,w=a.W,h=a.viewH()}end local function g(e,h)e.vx,e.vy=h.vx,h.vy end function a.screenToImage(e,h)return b.toImage(c(a.doc()),e-1
  232. ,h-2)end function a.cellToScreen(e,h)local i,k=b.cellToView(c(a.doc()),e,h)return i+1,k+2 end function a.clampView(e)g(e,b.clamp(c(e),e.img))end function a.setZoom(l,h,i)local e=a.doc()h,i=h or f(a.W/
  233. 2),i or f(a.viewH()/2)+2 local m=c(e)e.zoom=l local k=a.zoomInfo(e)g(e,b.zoom(m,e.img,k.zw,k.zh,h-1,i-2))end function a.toggleZoom()a.setZoom(a.doc().zoom%#a.ZOOMS+1)end function a.pan(h,i)local e=a.
  234. doc()g(e,b.pan(c(e),e.img,h,i))end function a.panStep(e,h)a.pan(e*j(2,f(a.W/4)),h*j(2,f(a.viewH()/4)))end function a.centreOn(h,i)local e=a.doc()g(e,b.centreOn(c(e),e.img,h,i))end function a.
  235. drawCanvas()local e=a.doc()b.draw(a.win,1,2,e.img,c(e),{text=a.showText,void={d.bar,d.void},contrast=d.text,frameColour=a.showText and function(h)return(a.sel and a.sel.box==h)and d.hi or d.dim end or
  236. nil,})end end end package.preload["lib.formats.adimg"]=function(...)local a={}a.VERSION=1 a.HEX="0123456789abcdef"a.B32="0123456789abcdefghijklmnopqrstuv"a.DEFAULT_PALETTE={[0]=0xF0F0F0,0xF2B233,
  237. 0xE57FD8,0x99B2F2,0xDEDE6C,0x7FCC19,0xF2B2CC,0x4C4C4C,0x999999,0x4C99B2,0xB266E5,0x3366CC,0x7F664C,0x57A64E,0xCC4C4C,0x111111,}local x,y=a.HEX,a.B32 local e,z,E,f=string.sub,string.char,string.byte,
  238. table.concat local h=math.floor local o,p,q={},{},{}local j,g={},{}local t={}for b=0,31 do local d=e(y,b+1,b+1)o[d]=z(128+b)p[d]=b q[b]=d local i,c=0,b while c>0 do i=i+c%2;c=h(c/2)end t[b]=i end for b
  239. =0,15 do local c=e(x,b+1,b+1)j[c]=b g[b]=c end a.B32_TO_CHAR,a.B32_TO_BITS,a.BITS_TO_B32=o,p,q a.HEX_TO_IDX,a.IDX_TO_HEX=j,g function a.new(d,i,c)c=c or 0 local b={w=d,h=i,pw=d*2,ph=i*3,px={},text={}}
  240. local k=b.px for l=1,b.pw*b.ph do k[l]=c end return b end function a.get(b,c,d)if c<0 or d<0 or c>=b.pw or d>=b.ph then return nil end return b.px[d*b.pw+c+1]end local function u(c,l,A,B)local d=B*3*l
  241. +A*2+1 local m,n=c[d],c[d+1]local r,s=c[d+l],c[d+l+1]local v,w=c[d+2*l],c[d+2*l+1]local i,k=w,w local b=0 if m~=i then k=m;b=b+1 end if n~=i then k=n;b=b+2 end if r~=i then k=r;b=b+4 end if s~=i then k
  242. =s;b=b+8 end if v~=i then k=v;b=b+16 end return b,k,i end a.encodeCell=u function a.rowsFromPixels(b)local d={}local n,r=b.px,b.pw for i=0,b.h-1 do local k,l,m={},{},{}for c=0,b.w-1 do local s,v,w=u(n
  243. ,r,c,i)k[c+1]=q[s];l[c+1]=g[v];m[c+1]=g[w]end d[i+1]={f(k),f(l),f(m)}end b.rows=d return d end function a.pixelsFromRows(b)local c,i={},b.w*2 b.pw,b.ph,b.px=i,b.h*3,c for s=0,b.h-1 do local r=b.rows[s
  244. +1]local v,w,A=r[1],r[2],r[3]for d=0,b.w-1 do local m=p[e(v,d+1,d+1)]or 0 local n=j[e(w,d+1,d+1)]or 0 local k=j[e(A,d+1,d+1)]or 15 local l=s*3*i+d*2+1 c[l]=m%2>=1 and n or k c[l+1]=m%4>=2 and n or k c
  245. [l+i]=m%8>=4 and n or k c[l+i+1]=m%16>=8 and n or k c[l+2*i]=m>=16 and n or k c[l+2*i+1]=k end end return c end function a.serialize(d)local i=a.rowsFromPixels(d)local b={"ADIMG "..a.VERSION,"size "..
  246. d.w.." "..d.h}if d.palette then local c={}for k=0,15 do c[#c+1]=string.format("%06x",d.palette[k])end b[#b+1]="palette "..f(c," ")end for l,c in ipairs(d.text)do b[#b+1]=string.format(
  247. "text %d %d %s %s %d",c.x,c.y,g[c.fg],c.bg and g[c.bg]or"-",#c.lines)for m,k in ipairs(c.lines)do b[#b+1]=k end end b[#b+1]="data"for k,c in ipairs(i)do b[#b+1]=c[1];b[#b+1]=c[2];b[#b+1]=c[3]end b[#b+
  248. 1]=""return f(b,"\n")end function a.parse(s,v)local d={}for b in(s:gsub("\r","").."\n"):gmatch("(.-)\n")do d[#d+1]=b end if not d[1]or not d[1]:match("^ADIMG %d+")then return nil,"not an ADIMG file"
  249. end local b={text={},rows={}}local c=2 while c<=#d do local i=d[c]local k=i:match("^(%S+)")if k=="size"then local l,m=i:match("^size (%d+) (%d+)")b.w,b.h=tonumber(l),tonumber(m)elseif k=="palette"then
  250. local m,l={},0 for n in i:gmatch("%x%x%x%x%x%x")do m[l]=tonumber(n,16);l=l+1 end if l==16 then b.palette=m end elseif k=="text"then local l,w,A,B,m=i:match("^text (%-?%d+) (%-?%d+) (%x) (%S) (%d+)")if
  251. not l then return nil,"bad text record on line "..c end local n={x=tonumber(l),y=tonumber(w),fg=j[A:lower()],bg=j[B:lower()],lines={}}for r=1,tonumber(m)do n.lines[r]=d[c+r]or""end b.text[#b.text+1]=n
  252. c=c+tonumber(m)elseif k=="data"then if not b.w then return nil,"missing size"end for r=1,b.h do local l,m,n=d[c+1]or"",d[c+2]or"",d[c+3]or""if#l~=b.w then l=(l..("0"):rep(b.w)):sub(1,b.w)end if#m~=b.w
  253. then m=(m..("0"):rep(b.w)):sub(1,b.w)end if#n~=b.w then n=(n..("f"):rep(b.w)):sub(1,b.w)end b.rows[r]={l,m,n}c=c+3 end break end c=c+1 end if not b.w then return nil,"missing size"end b.pw,b.ph=b.w*2,
  254. b.h*3 if v then a.pixelsFromRows(b)end return b end function a.save(c,d)local i=a.serialize(c)local b,k=fs.open(d,"wb")if not b then return nil,k end b.write(i)b.close()return true end function a.load(
  255. c,d)local b,i=fs.open(c,"rb")if not b then return nil,i end local k=b.readAll()or""b.close()return a.parse(k,d)end local function A(b,c,d,i)if b.bg then return g[b.bg]end return t[c]>3 and d or i end
  256. function a.compose(i,B)local l={}local v=i.rows for c=1,i.h do local b=v[c]l[c]={(b[1]:gsub(".",o)),b[2],b[3]}end if B==false or#i.text==0 then return l end local n={}for F,d in ipairs(i.text)do local
  257. r=1 for c,b in ipairs(d.lines)do if#b>r then r=#b end end for C,w in ipairs(d.lines)do local k=d.y+C-1 if k>=1 and k<=i.h then local b=n[k]if not b then local m=v[k]b={{},{},{},bits={}}for c=1,i.w do
  258. local s=e(m[1],c,c)b.bits[c]=p[s]or 0 b[1][c]=o[s]or" "b[2][c]=e(m[2],c,c);b[3][c]=e(m[3],c,c)end n[k]=b end for s=1,d.bg and r or#w do local c=d.x+s-1 local m=e(w,s,s)if m==""then m=" "end if c>=1 and
  259. c<=i.w and(m~=" "or d.bg)then local D=A(d,b.bits[c],b[2][c],b[3][c])b[1][c]=m;b[2][c]=g[d.fg];b[3][c]=D end end end end end for c,b in pairs(n)do l[c]={f(b[1]),f(b[2]),f(b[3])}end return l end function
  260. a.draw(m,b,d,i,c)b=b or term d,i=d or 1,i or 1 c=c or a.compose(m)for l=1,#c do local k=c[l]b.setCursorPos(d,i+l-1)b.blit(k[1],k[2],k[3])end end function a.applyPalette(d,b)b=b or a.DEFAULT_PALETTE for
  261. c=0,15 do d.setPaletteColour(2^c,b[c])end end function a.rgb(b)return h(b/65536)%256,h(b/256)%256,b%256 end function a.dist(m,n)local b,r,s=a.rgb(m)local c,v,w=a.rgb(n)local d,i,k=b-c,r-v,s-w local l=(
  262. b+c)/2 return(2+l/256)*d*d+4*i*i+(2+(255-l)/256)*k*k end function a.fitRect(l,m,d,i,k,r,s)if k~="fit"and k~="fill"then return d,i,0,0 end local n=(k=="fit"and math.min or math.max)(d/l,i/m)local b,c=
  263. math.max(1,h(l*n+0.5)),math.max(1,h(m*n+0.5))if k=="fit"then b,c=math.min(b,d),math.min(c,i)else b,c=math.max(b,d),math.max(c,i)end return b,c,h((d-b)*(r or 0.5)+0.5),h((i-c)*(s or 0.5)+0.5)end
  264. function a.copyText(i)local c={}for k,b in ipairs(i)do local d={}for l,m in ipairs(b.lines)do d[l]=m end c[k]={x=b.x,y=b.y,fg=b.fg,bg=b.bg,lines=d}end return c end return a end package.preload[
  265. "lib.formats.bmp"]=function(...)local q={}local h,i=string.byte,math.floor local function j(b,a)local c,d=h(b,a,a+1);return c+d*256 end local function c(b,a)local d,e,f,g=h(b,a,a+3);return d+e*256+f*
  266. 65536+g*16777216 end local function t(b,d)local a=c(b,d);if a>=2^31 then a=a-2^32 end;return a end local function u(b)if b:sub(1,2)~="BM"then return nil,"not a BMP"end local a={offset=c(b,11),hsize=c(
  267. b,15)}if a.hsize==12 then a.w,a.h,a.bpp,a.comp=j(b,19),j(b,21),j(b,25),0 else a.w,a.h,a.bpp,a.comp=t(b,19),t(b,23),j(b,29),c(b,31)a.ncolors=c(b,47)end a.topdown=a.h<0 a.h=math.abs(a.h)return a end
  268. function q.info(b)local a,d=u(b)if not a then return nil,d end return a.w,a.h end local function D(a)if a==0 then return 1,0 end local b=0 while a%2==0 do a=a/2;b=b+1 end return 2^b,a end function q.
  269. decode(d,E,v)local a,F=u(d)if not a then error(F)end local r,s,b=a.w,a.h,a.bpp if a.comp~=0 and a.comp~=3 and a.comp~=6 then error("BMP: compressed BMPs (RLE) are not supported")end local w={}if b<=8
  270. then local f=a.hsize==12 and 3 or 4 local k=(a.ncolors and a.ncolors>0)and a.ncolors or 2^b local g=15+a.hsize for e=0,k-1 do local l,m,n=h(d,g+e*f,g+e*f+2)w[e]={n or 0,m or 0,l or 0}end end local f if
  271. b==16 or b==32 then local e,g,k,l if a.comp==3 or a.comp==6 then local m=a.hsize>=52 and 55 or 15+a.hsize e,g,k=c(d,m),c(d,m+4),c(d,m+8)l=(a.hsize>=56)and c(d,m+12)or 0 elseif b==16 then e,g,k,l=
  272. 0x7C00,0x03E0,0x001F,0 else e,g,k,l=0xFF0000,0xFF00,0xFF,0 end f={}for n,m in ipairs({e,g,k,l})do local o,p=D(m)f[n]={m,o,p}end end local function k(g,e)if e[1]==0 then return nil end local l=i(g/e[2]
  273. )%(e[3]+1)return i(l*255/e[3]+0.5)end local G=i((r*b+31)/32)*4 local x,y,z,A={},{},{},{}for l=0,s-1 do local H=a.topdown and l or(s-1-l)local m=a.offset+l*G+1 for e=1,r do local n,o,p,B=0,0,0,255 if b
  274. ==24 then local g=m+(e-1)*3 p,o,n=h(d,g,g+2)elseif b==32 or b==16 then local g if b==32 then g=c(d,m+(e-1)*4)else g=j(d,m+(e-1)*2)end n,o,p=k(g,f[1]),k(g,f[2]),k(g,f[3])B=k(g,f[4])or 255 else local C=(
  275. e-1)*b local I=h(d,m+i(C/8))or 0 local J=i(I/2^(8-b-C%8))%2^b local g=w[J]or{0,0,0}n,o,p=g[1],g[2],g[3]end x[e],y[e],z[e],A[e]=n or 0,o or 0,p or 0,B end E(H,x,y,z,A)if v and l%16==0 then v()end end
  276. return r,s end return q end package.preload["lib.formats.decode"]=function(...)local a={}local g=string.byte a.DECODERS={png="lib.formats.png",jpeg="lib.formats.jpeg",bmp="lib.formats.bmp",}function a
  277. .detect(b)if b:sub(1,8)=="\137PNG\r\n\26\n"then return"png"end if g(b,1)==0xFF and g(b,2)==0xD8 then return"jpeg"end if b:sub(1,2)=="BM"then return"bmp"end if b:sub(1,6)=="ADIMG "then return"adimg"end
  278. if b:sub(1,4)=="RIFF"and b:sub(9,12)=="WEBP"then return"webp"end if b:sub(1,3)=="GIF"then return"gif"end return nil end function a.decoder(c)local b=a.DECODERS[c]return b and require(b)end function a.
  279. info(h)local c=a.detect(h)if not c then return nil,"unknown file format"end local i=a.decoder(c)if not i then return nil,c:upper().." files are not supported"end local d,e,f=i.info(h)if not d then
  280. return nil,e end local b={format=c,w=d,h=e,orientation=f and f.orientation or 1,indexed=f and f.indexed or false}if b.orientation>=5 then b.dw,b.dh=e,d else b.dw,b.dh=d,e end return b end function a.
  281. rows(c,e,f,h)local b=a.detect(c)local d=b and a.decoder(b)if not d then error(b and(b:upper().." files are not supported")or"unknown file format",2)end return d.decode(c,e,f,h)end return a end package
  282. .preload["lib.formats.inflate"]=function(...)local h={}local r,z=math.floor,string.byte local f={}for a=0,31 do f[a]=2^a end local G={3,4,5,6,7,8,9,10,11,13,15,17,19,23,27,31,35,43,51,59,67,83,99,115,
  283. 131,163,195,227,258}local H={0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0}local I={1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,
  284. 16385,24577}local J={0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13}local A={16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15}local function i(l,m)local a,c=0,{}for b=0,15 do c[b]=0
  285. end for d=0,m-1 do local b=l[d]or 0 c[b]=c[b]+1 if b>a then a=b end end local e={maxlen=a}if a==0 then return e end local g,j=0,{}c[0]=0 for b=1,a do g=(g+c[b-1])*2 j[b]=g end local o=f[a]for n=0,m-1
  286. do local b=l[n]or 0 if b>0 then local d=j[b];j[b]=d+1 local k=0 for p=1,b do k=k*2+d%2;d=r(d/2)end local p,q=n*16+b,f[b]for s=k,o-1,q do e[s]=p end end end return e end local s,B local function K()if
  287. not s then local a={}for b=0,143 do a[b]=8 end for b=144,255 do a[b]=9 end for b=256,279 do a[b]=7 end for b=280,287 do a[b]=8 end s=i(a,288)local b={}for c=0,29 do b[c]=5 end B=i(b,30)end return s,B
  288. end function h.inflate(C,j,L,l)local e,c=0,0 local M=#C local N,D=32768,32768+65536 local g,b={},0 local m=0 local function E(a)while c<a do local d=z(C,j)or 0 j=j+1 e=e+d*f[c]c=c+8 end end local
  289. function a(d)if d==0 then return 0 end if c<d then E(d)end local k=f[d]local n=e%k e=(e-n)/k c=c-d return n end local function t(o)local d=o.maxlen if c<d then E(d)end local k=o[e%f[d]]if not k then
  290. error("inflate: bad huffman code")end local n=k%16 local p=f[n]e=r(e/p)c=c-n return(k-n)/16 end local function u(o)local d=o and 0 or N local k=b-d if k<=0 then return end L(g,k)for n=1,d do g[n]=g[k+
  291. n]end b=d if l then l()end end repeat local O=a(1)local n=a(2)if n==0 then a(c%8)local d=a(16)a(16)for k=1,d do b=b+1;g[b]=a(8)if b>=D then u()end end elseif n==1 or n==2 then local v,w if n==1 then v
  292. ,w=K()else local o,x,P=a(5)+257,a(5)+1,a(4)+4 local y={}for d=1,19 do y[A[d]]=0 end for d=1,P do y[A[d]]=a(3)end local Q=i(y,19)local k,d={},0 while d<o+x do local p=t(Q)if p<16 then k[d]=p;d=d+1 else
  293. local q,F=0,0 if p==16 then if d==0 then error("inflate: bad repeat")end q,F=3+a(2),k[d-1]elseif p==17 then q=3+a(3)else q=11+a(7)end for R=1,q do k[d]=F;d=d+1 end end end local p,q={},{}for F=0,o-1 do
  294. p[F]=k[F]end for F=0,x-1 do q[F]=k[o+F]end v,w=i(p,o),i(q,x)end while true do local d=t(v)if d<256 then b=b+1;g[b]=d elseif d==256 then break else d=d-256 local k=G[d]+a(H[d])local o=t(w)+1 local x=I[
  295. o]+a(J[o])local p=b-x if p<0 then error("inflate: distance too far back")end for q=1,k do g[b+q]=g[p+q]end b=b+k end if b>=D then u()end m=m+1 if m>20000 then m=0;if l then l()end end end else error(
  296. "inflate: bad block type")end if j>M+8 then error("inflate: unexpected end of data")end until O==1 u(true)return j end function h.zlib(b,d,e)local a,c=z(b,1,2)if not a or a%16~=8 or(a*256+c)%31~=0 then
  297. error("zlib: bad header")end if r(c/32)%2==1 then error("zlib: preset dictionary not supported")end return h.inflate(b,3,d,e)end function h.zlibString(d)local a={}h.zlib(d,function(e,g)local b={}for c
  298. =1,g do b[c]=string.char(e[c])end a[#a+1]=table.concat(b)end)return table.concat(a)end return h end package.preload["lib.formats.jpeg"]=function(...)local L={}local c,e,ac,ad,ae=string.byte,math.floor
  299. ,math.cos,math.sqrt,math.pi local function m(b,a)local d,f=c(b,a,a+1);return d*256+f end local af={0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,12,19,26,33,40,48,41,34,27,20,13,6,7,14,21,28,35,42,49,56,57,
  300. 50,43,36,29,22,15,23,30,37,44,51,58,59,52,45,38,31,39,46,53,60,61,54,47,55,62,63,}local o={}for a=0,63 do o[a]=af[a+1]+1 end local g={}for a=0,32 do g[a]=2^a end local B={}for a=0,7 do for b=0,7 do B[
  301. a*8+b+1]=(a==0 and 1/ad(2)or 1)/2*ac((2*b+1)*a*ae/16)end end local function ag(h,b,l)if h:sub(b,b+5)~="Exif\0\0"then return nil end local d=b+6 local i=h:sub(d,d+1)=="II"local function a(j)local k,f=c(
  302. h,j,j+1);if not f then return 0 end;return i and k+f*256 or k*256+f end local function k(f)return i and a(f)+a(f+2)*65536 or a(f)*65536+a(f+2)end local j=d+k(d+4)local n=a(j)for p=0,n-1 do local f=j+2
  303. +p*12 if f+12>b+l then break end if a(f)==0x0112 then return a(f+8)end end end local function ah(d)if c(d,1)~=0xFF or c(d,2)~=0xD8 then return nil,"not a JPEG"end local a,f=3,{orientation=1}while a<#d
  304. do if c(d,a)~=0xFF then a=a+1 else local b=c(d,a+1)if b==0xFF then a=a+1 elseif b==0xD8 or b==0x01 or(b>=0xD0 and b<=0xD7)then a=a+2 elseif b==0xD9 then break else local h=m(d,a+2)if b==0xE1 then f.
  305. orientation=ag(d,a+4,h-2)or f.orientation end if b>=0xC0 and b<=0xCF and b~=0xC4 and b~=0xC8 and b~=0xCC then f.sof=b f.h,f.w=m(d,a+5),m(d,a+7)f.progressive=b==0xC2 return f end a=a+2+h end end end
  306. return nil,"JPEG: no frame header"end function L.info(b)local a,d=ah(b)if not a then return nil,d end return a.w,a.h,a end local function Q(h,i)local d={look={},maxcode={},mincode={},valptr={},vals=i}
  307. local b,f=0,1 for a=1,16 do d.valptr[a]=f;d.mincode[a]=b for n=1,h[a]do if a<=9 then local k=i[f]*16+a local j=b*g[9-a]for l=j,j+g[9-a]-1 do d.look[l]=k end end b=b+1;f=f+1 end d.maxcode[a]=h[a]>0 and
  308. b-1 or-1 b=b*2 end return d end function L.decode(b,ai,C,p)local R,S,T={},{},{}local a,t,U=nil,0,nil local h=3 local D,E,u local j,M local F=0 local G=0 local function N()G=G+1 if G>=512 then G=0;if C
  309. then C()end end end local function aj()if p then return{0}end local d={}for f=1,64 do d[f]=0 end return d end local function V(f,k,l)local i=k*f.bpl+l+1 local d=f.blocks[i]if not d then d=aj();f.
  310. blocks[i]=d end return d end local q={}local function ak(v)if v>=a.mcusY then return end for y,d in ipairs(j)do local k=q[y]or{}q[y]=k local r=R[d.tq]or{}local s=d.bpl*u for l=0,d.v-1 do local O=v*d.v
  311. +l for n=0,d.bpl-1 do local z=O*d.bpl+n+1 local i=d.blocks[z]d.blocks[z]=nil if p then local f=i and i[1]*(r[1]or 1)/8+128 or 128 k[l*s+n+1]=f elseif not i then for f=0,7 do local w=(l*8+f)*s+n*8 for x
  312. =1,8 do k[w+x]=128 end end else local w={}for P=0,7 do local f=P*8 local W=i[f+1]*r[f+1]local A=false for x=2,8 do if i[f+x]~=0 then A=true;break end end if not A then local x=W*B[1]for H=1,8 do w[f+H
  313. ]=x end else local H={}for x=1,8 do H[x]=i[f+x]*r[f+x]end for I=1,8 do local x=0 for J=0,7 do local K=H[J+1]if K~=0 then x=x+K*B[J*8+I]end end w[f+I]=x end end end for x=0,7 do local I=(l*8+x)*s+n*8
  314. for A=1,8 do local f=0 for H=0,7 do f=f+w[H*8+A]*B[H*8+x+1]end k[I+A]=f+128 end end end end N()end end local y,z,A,H={},{},{},{}local W=v*a.vmax*u local s=#j local X,Y=a.hmax,a.vmax local w=U if w==
  315. nil then local d=s==3 and j[1].id==82 and j[2].id==71 and j[3].id==66 w=(s==3 and not d)and 1 or 0 end for I=0,a.vmax*u-1 do local J=W+I if J>=E then break end local n,r={},{}for f,d in ipairs(j)do n[
  316. f]=e(I*d.v/Y)*d.bpl*u r[f]=d.h/X end for k=0,D-1 do local l=q[1][n[1]+e(k*r[1])+1]local d,f,i if s==1 then d,f,i=l,l,l else local K=q[2][n[2]+e(k*r[2])+1]local O=q[3][n[3]+e(k*r[3])+1]if w~=0 then
  317. local x,P=K-128,O-128 d=l+1.402*P f=l-0.344136*x-0.714136*P i=l+1.772*x else d,f,i=l,K,O end if s==4 then local x=q[4][n[4]+e(k*r[4])+1]/255 d,f,i=d*x,f*x,i*x end end d=e(d+0.5);f=e(f+0.5);i=e(i+0.5)y
  318. [k+1]=d<0 and 0 or d>255 and 255 or d z[k+1]=f<0 and 0 or f>255 and 255 or f A[k+1]=i<0 and 0 or i>255 and 255 or i H[k+1]=255 end ai(J,y,z,A,H)end if C then C()end end local function O(d)while F<d do
  319. ak(F);F=F+1 end end local function W(d)while d<#b do if c(b,d)==0xFF then local f=c(b,d+1)if f~=0 and not(f>=0xD0 and f<=0xD7)then return d end d=d+2 else d=d+1 end end return d end local function al(
  320. d,v,H,I,X,J)local k,f,w=0,0,false local function Y()while f<=16 do local i=0 if not w then i=c(b,d)if not i then w=true;i=0 elseif i==0xFF then local l=c(b,d+1)if l==0 then d=d+2 elseif l==0xFF then d
  321. =d+1;i=nil else w=true;i=0 end else d=d+1 end end if i then k=k*256+i;f=f+8 end end end local function r(i)if f<i then Y()end f=f-i local l=g[f]local n=e(k/l)k=k-n*l return n end local function x(i)if
  322. i==0 then return 0 end local l=r(i)if l<g[i-1]then l=l-g[i]+1 end return l end local function y(i)if f<16 then Y()end local s=e(k/g[f-16])local n=i.look[e(s/128)]if n then local l=n%16 f=f-l k=k%g[f]
  323. return(n-l)/16 end for l=10,16 do local z=e(s/g[16-l])if z<=i.maxcode[l]then f=f-l k=k%g[f]return i.vals[i.valptr[l]+z-i.mincode[l]]end end error("JPEG: bad huffman code")end local function Z()k,f=0,0
  324. if not w then while d<#b and not(c(b,d)==0xFF and c(b,d+1)~=0)do d=d+1 end end local i=c(b,d+1)if c(b,d)==0xFF and i and i>=0xD0 and i<=0xD7 then d=d+2 end w=false for n,l in ipairs(v)do l.pred=0 end
  325. end local i=0 local _=a.progressive local n if not _ then n=function(s,z)local aa=y(s.dct)s.pred=s.pred+x(aa)z[1]=s.pred local ab=s.act local l=1 while l<64 do local A=y(ab)local K,P=A%16,e(A/16)if K
  326. ==0 then if P<15 then break end l=l+16 else l=l+P local am=x(K)if not p and l<64 then z[o[l]]=am end l=l+1 end end end elseif H==0 then if X==0 then n=function(l,s)l.pred=l.pred+x(y(l.dct))s[1]=l.pred
  327. *g[J]end else n=function(s,l)if r(1)==1 then l[1]=l[1]+g[J]end end end elseif X==0 then n=function(K,P)if i>0 then i=i-1;return end local aa=K.act local l=H while l<=I do local A=y(aa)local z,s=A%16,e(
  328. A/16)if z==0 then if s<15 then i=g[s]-1 if s>0 then i=i+r(s)end break end l=l+16 else l=l+s if l<=63 then P[o[l]]=x(z)*g[J]else x(z)end l=l+1 end end end else local s=g[J]local function aa(z,A)local l
  329. =z[A]if r(1)==1 and e(math.abs(l)/s)%2==0 then z[A]=l>=0 and l+s or l-s end end n=function(am,z)local A=H if i>0 then i=i-1 for K=H,I do local l=o[K]if z[l]~=0 then aa(z,l)end end return end local an=
  330. am.act while A<=I do local ab=y(an)local K,l=ab%16,e(ab/16)if K==0 then if l<15 then i=g[l]-1 if l>0 then i=i+r(l)end l=64 end else K=r(1)==1 and s or-s end while A<=I do local P=o[A]A=A+1 if z[P]~=0
  331. then aa(z,P)else if l==0 then if K~=0 then z[P]=K end break end l=l-1 end end end end end local l=0 local A=not _ and#v==#j if#v==1 then local s=v[1]for z=0,s.bh-1 do for K=0,s.bw-1 do if t>0 and l>0
  332. and l%t==0 then Z();i=0 end n(s,V(s,z,K))l=l+1 end N()if A and(z+1)%s.v==0 then O((z+1)/s.v)end end else for z=0,a.mcusY-1 do for K=0,a.mcusX-1 do if t>0 and l>0 and l%t==0 then Z();i=0 end for ab,s in
  333. ipairs(v)do for P=0,s.v-1 do for aa=0,s.h-1 do n(s,V(s,z*s.v+P,K*s.h+aa))end end end l=l+1 end N()if A then O(z+1)end end end return W(d)end while h<#b do if c(b,h)~=0xFF then h=h+1 else local f=c(b,h
  334. +1)if f==0xFF then h=h+1 elseif f==0xD8 or f==0x01 or(f>=0xD0 and f<=0xD7)then h=h+2 elseif f==0xD9 then break else local v=m(b,h+2)local d=h+4 local i=h+2+v if f==0xDB then while d<i do local n,r=e(c(
  335. b,d)/16),c(b,d)%16 d=d+1 local l={}for s=0,63 do local k if n==0 then k=c(b,d);d=d+1 else k=m(b,d);d=d+2 end l[o[s]]=k end R[r]=l end elseif f==0xC4 then while d<i do local w,r=e(c(b,d)/16),c(b,d)%16
  336. local k,l={},0 for n=1,16 do k[n]=c(b,d+n);l=l+k[n]end local n={}for s=1,l do n[s]=c(b,d+16+s)end d=d+17+l if w==0 then S[r]=Q(k,n)else T[r]=Q(k,n)end end elseif f==0xDD then t=m(b,d)elseif f==0xEE and
  337. b:sub(d,d+4)=="Adobe"then U=c(b,d+11)elseif f==0xC0 or f==0xC1 or f==0xC2 then if c(b,d)~=8 then error("JPEG: only 8-bit precision supported")end a={progressive=f==0xC2,h=m(b,d+1),w=m(b,d+3)}if a.h==0
  338. then error("JPEG: DNL height not supported")end j,M={},{}local l=c(b,d+5)a.hmax,a.vmax=1,1 for s=0,l-1 do local n=d+6+s*3 local r=c(b,n+1)local k={id=c(b,n),h=e(r/16),v=r%16,tq=c(b,n+2),blocks={},pred
  339. =0}j[#j+1]=k M[k.id]=k a.hmax=math.max(a.hmax,k.h)a.vmax=math.max(a.vmax,k.v)end if l~=1 and l~=3 and l~=4 then error("JPEG: unsupported component count")end a.mcusX=math.ceil(a.w/(8*a.hmax))a.mcusY=
  340. math.ceil(a.h/(8*a.vmax))for n,k in ipairs(j)do k.bpl=a.mcusX*k.h k.bw=math.ceil(math.ceil(a.w*k.h/a.hmax)/8)k.bh=math.ceil(math.ceil(a.h*k.v/a.vmax)/8)end if l==1 then local k=j[1]k.h,k.v,a.hmax,a.
  341. vmax=1,1,1,1 a.mcusX,a.mcusY=k.bw,k.bh k.bpl=k.bw end u=p and 1 or 8 if p then D,E=math.ceil(a.w/8),math.ceil(a.h/8)else D,E=a.w,a.h end elseif f>=0xC3 and f<=0xCF and f~=0xC4 and f~=0xC8 and f~=0xCC
  342. then error("JPEG: unsupported type (lossless/arithmetic coded)")elseif f==0xDA then if not a then error("JPEG: scan before frame")end local n=c(b,d)local l={}for r=0,n-1 do local k=M[c(b,d+1+r*2)]
  343. local s=c(b,d+2+r*2)if not k then error("JPEG: bad scan component")end k.dct,k.act=S[e(s/16)],T[s%16]k.pred=0 l[#l+1]=k end local k=d+1+n*2 local r,w,s=c(b,k),c(b,k+1),c(b,k+2)if p and a.progressive
  344. and r>0 then h=W(i)else h=al(i,l,r,w,e(s/16),s%16)end i=nil end if i then h=i end end end end if not a then error("JPEG: no image data")end O(a.mcusY)return D,E end return L end package.preload[
  345. "lib.formats.png"]=function(...)local S=require("lib.formats.inflate")local E={}local o,a=string.byte,math.floor local function e(c,b)local d,f,g,h=o(c,b,b+3)return((d*256+f)*256+g)*256+h end local T=
  346. "\137PNG\r\n\26\n"local function K(c)if c:sub(1,8)~=T then return nil,"not a PNG"end local b,d=9,{}while b+8<=#c do local f=e(c,b)local g=c:sub(b+4,b+7)d[#d+1]={g,b+8,f}b=b+12+f if g=="IEND"then break
  347. end end return d end function E.info(c)local d,f=K(c)if not d then return nil,f end local b=d[1]if not b or b[1]~="IHDR"then return nil,"PNG: missing IHDR"end return e(c,b[2]),e(c,b[2]+4),{indexed=c:
  348. byte(b[2]+9)==3}end local U={[0]=1,[2]=3,[3]=1,[4]=2,[6]=4}local V={{0,0,8,8},{4,0,8,8},{0,4,4,8},{2,0,4,4},{0,2,2,4},{1,0,2,2},{0,1,1,2}}function E.decode(c,L,F)local G,W=K(c)if not G then error(W)
  349. end local r=G[1][2]local p,x=e(c,r),e(c,r+4)local f,g=o(c,r+8),o(c,r+9)local M=o(c,r+12)local y=U[g]if not y then error("PNG: bad colour type "..tostring(g))end local N,O,j={},{},nil local s={}for q,d
  350. in ipairs(G)do local h,b,i=d[1],d[2],d[3]if h=="PLTE"then for k=0,i/3-1 do local l,m,n=o(c,b+k*3,b+k*3+2)N[k]={l,m,n}end elseif h=="tRNS"then if g==3 then for k=0,i-1 do O[k]=o(c,b+k)end elseif g==0
  351. then j={e("\0\0"..c:sub(b,b+1),1)}elseif g==2 then j={e("\0\0"..c:sub(b,b+1),1),e("\0\0"..c:sub(b+2,b+3),1),e("\0\0"..c:sub(b+4,b+5),1)}end elseif h=="IDAT"then s[#s+1]=c:sub(b,b+i-1)end end s=table.
  352. concat(s)local H=f*y local z=math.max(1,a(H/8))local X=2^f-1 local Y=f<8 and 255/X or 1 local t={}if M==1 then for i,b in ipairs(V)do local d=a((p-b[1]+b[3]-1)/b[3])local h=a((x-b[2]+b[4]-1)/b[4])if d
  353. >0 and h>0 then t[#t+1]={b[1],b[2],b[3],b[4],d,h}end end else t[1]={0,0,1,1,p,x}end local u=M==1 and{}or nil local A,B,C,D={},{},{},{}local function Z(v,w,I)local J=w[5]local b={}if f==8 then b=v
  354. elseif f==16 then for d=1,J*y do b[d]=v[d*2-1]*256+v[d*2]end else local h=8/f local i=2^f local d=0 for k=1,#v do local l=v[k]for m=h-1,0,-1 do d=d+1 b[d]=a(l/2^(m*f))%i end end end local d=f==16 and 1
  355. /257 or Y for i=1,J do local k,l,m,n local h=(i-1)*y if g==3 then local P=b[i]local q=N[P]or{0,0,0}k,l,m,n=q[1],q[2],q[3],O[P]or 255 elseif g==0 then local q=b[i]n=(j and q==j[1])and 0 or 255 q=a(q*d+
  356. 0.5);k,l,m=q,q,q elseif g==4 then local q=a(b[h+1]*d+0.5)k,l,m,n=q,q,q,a(b[h+2]*d+0.5)elseif g==2 then local q,P,Q=b[h+1],b[h+2],b[h+3]n=(j and q==j[1]and P==j[2]and Q==j[3])and 0 or 255 k,l,m=a(q*d+
  357. 0.5),a(P*d+0.5),a(Q*d+0.5)else k,l,m,n=a(b[h+1]*d+0.5),a(b[h+2]*d+0.5),a(b[h+3]*d+0.5),a(b[h+4]*d+0.5)end if u then local q,P=w[1]+(i-1)*w[3],w[2]+I*w[4]u[P*p+q+1]=((n*256+k)*256+l)*256+m else A[i],B[
  358. i],C[i],D[i]=k,l,m,n end end if not u then L(I,A,B,C,D)end end local q=1 local k=t[q]local v=k and a((k[5]*H+7)/8)local h,l={},{}for b=1,v or 0 do h[b]=0 end local i,d,m=nil,0,0 local function _(aa,ab
  359. )for ac=1,ab do if not k then return end local b=aa[ac]if i==nil then i=b;d=0 else d=d+1 local n=d>z and l[d-z]or 0 if i==0 then elseif i==1 then b=(b+n)%256 elseif i==2 then b=(b+h[d])%256 elseif i==
  360. 3 then b=(b+a((n+h[d])/2))%256 elseif i==4 then local w=h[d]local I=d>z and h[d-z]or 0 local J=n+w-I local P,Q,R=math.abs(J-n),math.abs(J-w),math.abs(J-I)if P<=Q and P<=R then b=(b+n)%256 elseif Q<=R
  361. then b=(b+w)%256 else b=(b+I)%256 end else error("PNG: bad filter type "..i)end l[d]=b if d==v then Z(l,k,m)h,l=l,h i=nil m=m+1 if m>=k[6]then q=q+1;k=t[q];m=0 if k then v=a((k[5]*H+7)/8)h,l={},{}for w
  362. =1,v do h[w]=0 end end end end end end end S.zlib(s,_,F)if u then for w=0,x-1 do for n=1,p do local b=u[w*p+n]or 0 C[n]=b%256;b=a(b/256)B[n]=b%256;b=a(b/256)A[n]=b%256;D[n]=a(b/256)end L(w,A,B,C,D)if F
  363. and w%32==0 then F()end end end return p,x end return E end package.preload["lib.history"]=function(...)local h=require("lib.formats.adimg")local a={}a.LIMIT=64 function a.attach(b)b.undo,b.redo=b.
  364. undo or{},b.redo or{}return b end local function e(b)return{w=b.w,h=b.h,pw=b.pw,ph=b.ph,px=b.px,text=b.text,palette=b.palette}end local function i(b,c)b.w,b.h,b.pw,b.ph,b.px,b.text,b.palette=c.w,c.h,c
  365. .pw,c.ph,c.px,c.text,c.palette end function a.begin(c,d)local b={pix={},doc=c}if d=="text"then b.text=h.copyText(c.img.text)end if d=="full"then b.full=e(c.img);b.changed=true end return b end function
  366. a.set(b,d,c,f)local g=d.px[c]if g~=f then if b then if b.pix[c]==nil then b.pix[c]=g end b.changed=true end d.px[c]=f end end function a.touch(b)if b then b.changed=true end end function a.commit(c)if
  367. not(c and c.changed)then return false end local b=c.doc table.insert(b.undo,c)if#b.undo>a.LIMIT then table.remove(b.undo,1)end b.redo={}b.dirty=true return true end local function g(b,j)local c=j.img
  368. if b.full then local d=e(c);i(c,b.full);b.full=d end local f=c.px for d,k in pairs(b.pix)do b.pix[d]=f[d];f[d]=k end if b.text then local d=c.text;c.text=b.text;b.text=d end end function a.step(b,d)
  369. local f,j=d and b.redo or b.undo,d and b.undo or b.redo local c=table.remove(f)if not c then return false end g(c,b)table.insert(j,c)b.dirty=true return true end return a end package.preload[
  370. "lib.paint"]=function(...)local d=require("lib.formats.adimg")local a={}local b,g,h=math.floor,math.max,math.min local function n(c,e,f)c.px[e]=f end a.BAYER={0,8,2,10,12,4,14,6,3,11,1,9,15,7,13,5}
  371. local E=a.BAYER function a.ditherOn(c,e,f)return E[(e%4)*4+c%4+1]<f end local u=a.ditherOn function a.fixCell(e,o,p,f,q)if o<0 or p<0 or o>=e.w or p>=e.h then return end q=q or n local j,t=e.pw,e.px
  372. local i=p*3*j+o*2+1 local r={i,i+1,i+j,i+j+1,i+2*j,i+2*j+1}local c,s={},0 for l=1,6 do local k=t[r[l]]if not c[k]then c[k]=0;s=s+1 end c[k]=c[k]+1 end if s<=2 then return end if not c[f]then local k=-
  373. 1 for m,l in pairs(c)do if l>k then f,k=m,l end end end local k,v=nil,-1 for l=0,15 do local m=c[l]if m and l~=f and m>v then k,v=l,m end end local l=e.palette or d.DEFAULT_PALETTE for w=1,6 do local m
  374. =t[r[w]]if m~=f and m~=k then local x=d.dist(l[m],l[f])<=d.dist(l[m],l[k])and f or k q(e,r[w],x)end end end local v=a.fixCell local function w(c,f,i,j)for e in pairs(f)do v(c,e%c.w,b(e/c.w),i,j)end end
  375. function a.stamp(c,l,m,o,F,G,x,j)j=j or n local e=G local y,z=-b((e-1)/2),b(e/2)local k=(e%2==0)and 0.5 or 0 local H=(e/2)^2 local I=x<8 and e>2 local A=x*2 local J,K=l+0.5+k,m+0.5+k local L=(e/2+0.5)
  376. ^2 local function B(f,i)if not I then return A end local p=(f-J)^2+(i-K)^2 return A*g(0,1-p/L)end local p={}for q=y,z do for r=y,z do local C,D=r-k,q-k if e<=2 or C*C+D*D<=H then if F then local f,i=l
  377. +r,m+q if f>=0 and i>=0 and f<c.w and i<c.h then for s=i*3,i*3+2 do for t=f*2,f*2+1 do if u(t,s,B((t+0.5)/2,(s+0.5)/3))then j(c,s*c.pw+t+1,o)end end end p[i*c.w+f]=true end else local f,i=l+r,m+q if f
  378. >=0 and i>=0 and f<c.pw and i<c.ph and u(f,i,B(f+0.5,i+0.5))then j(c,i*c.pw+f+1,o)p[b(i/3)*c.w+b(f/2)]=true end end end end end w(c,p,o,j)end function a.line(c,e,i,j,o)local k,l=math.abs(i-c),-math.
  379. abs(j-e)local p,q=c<i and 1 or-1,e<j and 1 or-1 local f=k+l while true do o(c,e)if c==i and e==j then break end local m=2*f if m>=l then f=f+l;c=c+p end if m<=k then f=f+k;e=e+q end end end function a
  380. .floodFill(i,x,y,m,j)j=j or n local c,z,q=i.pw,i.ph,i.px local o=y*c+x local r=q[o+1]if r==m then return end local s,f,p={[o]=true},{o},{}while#f>0 do local e=f[#f];f[#f]=nil p[#p+1]=e local t=e%c
  381. local function k(l)if not s[l]and q[l+1]==r then s[l]=true;f[#f+1]=l end end if t>0 then k(e-1)end if t<c-1 then k(e+1)end if e>=c then k(e-c)end if e<c*(z-1)then k(e+c)end end local k={}for A,e in
  382. ipairs(p)do local l,t=e%c,b(e/c)j(i,e+1,m)k[b(t/3)*i.w+b(l/2)]=true end w(i,k,m,j)end function a.cellMajority(c,o,p)local j=p*3*c.pw+o*2 local f,k,l,i={},nil,-1,nil for q,m in ipairs({0,1,c.pw,c.pw+1,
  383. 2*c.pw,2*c.pw+1})do local e=c.px[j+m+1]f[e]=(f[e]or 0)+1 if f[e]>l then k,l,i=e,f[e],j+m end end return k,i%c.pw,b(i/c.pw)end function a.placePixels(e,l,m,o,p,q)local c=d.new(l,m,q)for j=0,c.ph-1 do
  384. local f=j-p*3 if f>=0 and f<e.ph then for k=0,c.pw-1 do local i=k-o*2 if i>=0 and i<e.pw then c.px[j*c.pw+k+1]=e.px[f*e.pw+i+1]end end end end return c end function a.anchorOffset(c,e,f)return b((c-e)
  385. *f+0.5)end function a.moveText(e,f,i,j,k)for l,c in ipairs(e)do c.x=g(1,h(j,c.x+f))c.y=g(1,h(k,c.y+i))end end function a.resizeCanvas(c,e,f,l,m,o)local i,j=a.anchorOffset(e,c.w,l),a.anchorOffset(f,c.h
  386. ,m)local p=a.placePixels(c,e,f,i,j,o)local k=d.copyText(c.text)a.moveText(k,i,j,e,f)return p,k end function a.scale(c,e,f,x,y,z,A)local k,p=e*2,f*3 local l,m,q,r=d.fitRect(c.pw,c.ph,k,p,x,y,z)local i=
  387. d.new(e,f,A)for s=0,p-1 do local j=s-r if j>=0 and j<m then local B=h(c.ph-1,b((j+0.5)*c.ph/m))for t=0,k-1 do local o=t-q if o>=0 and o<l then local C=h(c.pw-1,b((o+0.5)*c.pw/l))i.px[s*k+t+1]=c.px[B*c
  388. .pw+C+1]end end end end i.palette=c.palette for j=0,f-1 do for o=0,e-1 do v(i,o,j,nil)end end local o=d.copyText(c.text)for B,j in ipairs(o)do local s=(j.x-1)*2*l/c.pw+q local t=(j.y-1)*3*m/c.ph+r j.x
  389. =g(1,h(e,b(s/2+0.5)+1))j.y=g(1,h(f,b(t/3+0.5)+1))end return i,o end function a.boxWidth(e)local c=1 for i,f in ipairs(e.lines)do c=g(c,#f)end return c end local l=a.boxWidth function a.boxAt(k,e,f)
  390. local i=k.text for j=#i,1,-1 do local c=i[j]if e+1>=c.x and e+1<c.x+l(c)and f+1>=c.y and f+1<c.y+#c.lines then return c,j end end end function a.textMap(e)local m={}for q,c in ipairs(e.text)do for p,o
  391. in ipairs(c.lines)do local i=c.y+p-2 for j=1,(c.bg and l(c)or#o)do local k=c.x+j-2 local f=o:sub(j,j)if f==""then f=" "end if k>=0 and i>=0 and k<e.w and i<e.h and(f~=" "or c.bg)then m[i*e.w+k]={f,c.
  392. fg,c.bg}end end end end return m end return a end package.preload["lib.render"]=function(...)local u=require("lib.formats.adimg")local p=require("lib.paint")local c={}local a,h,i=math.floor,math.max,
  393. math.min local d=u.IDX_TO_HEX local j={}for b=0,31 do j[b]=string.char(128+b)end function c.toImage(b,e,f)if not b.zw then local g,k=b.vx/2+e,b.vy/3+f return g*2,k*3,g,k end local g,k=b.vx+a(e/b.zw),b
  394. .vy+a(f/b.zh)return g,k,a(g/2),a(k/3)end function c.cellToView(b,e,f)if b.zw then return(e*2-b.vx)*b.zw+b.zw,(f*3-b.vy)*b.zh+a(3*b.zh/2)end return e-b.vx/2,f-b.vy/3 end function c.clamp(b,e)local f=b.
  395. zw and a(b.w/b.zw)or b.w*2 local g=b.zh and a(b.h/b.zh)or b.h*3 b.vx=h(-f+2,i(b.vx,e.pw-2))b.vy=h(-g+3,i(b.vy,e.ph-3))if not b.zw then b.vx=a(b.vx/2)*2 b.vy=a(b.vy/3)*3 end return b end function c.
  396. zoom(b,n,e,k,f,g)local l,m=c.toImage(b,f,g)b.zw,b.zh=e,k if e then b.vx,b.vy=l-a(f/e),m-a(g/k)else b.vx,b.vy=l-f*2,m-g*3 end return c.clamp(b,n)end function c.pan(b,g,e,f)if b.zw then b.vx=b.vx+e*h(1,
  397. a(4/b.zw))b.vy=b.vy+f*h(1,a(3/b.zh))else b.vx,b.vy=b.vx+e*2,b.vy+f*3 end return c.clamp(b,g)end function c.centreOn(b,l,e,f)local g,k=a(b.w/2),a(b.h/2)if b.zw then b.vx,b.vy=e-a(g/b.zw),f-a(k/b.zh)
  398. else b.vx,b.vy=e-g*2,f-k*3 end return c.clamp(b,l)end local function C(e,k,s,t,v)local q={}local w,x=k.vx/2,k.vy/3 for r=0,k.h-1 do local f=x+r local l,m,n={},{},{}for b=0,k.w-1 do local g=w+b if g>=0
  399. and f>=0 and g<e.w and f<e.h then local o=s[f*e.w+g]if o then l[b+1]=o[1];m[b+1]=d[o[2]]n[b+1]=d[o[3]or p.cellMajority(e,g,f)]else local y,z,A=u.encodeCell(e.px,e.pw,g,f)l[b+1]=j[y];m[b+1]=d[z];n[b+1]
  400. =d[A]end else l[b+1]="\127";m[b+1]=t;n[b+1]=v end end q[r]={l,m,n}end return q end local function D(E,e,F,G,H)local r,s=e.zw,e.zh for I,f in ipairs(E.text)do local t=G(f)if t then local v=d[t]local w=((
  401. f.x-1)*2-e.vx)*r local x=((f.x-1+p.boxWidth(f))*2-e.vx)*r-1 local y=((f.y-1)*3-e.vy)*s local z=((f.y-1+#f.lines)*3-e.vy)*s-1 for m=h(0,y),i(e.h-1,z)do local g=F[m]local n,o=m==y,m==z for b=h(0,w),i(e.
  402. w-1,x)do local k,l=b==w,b==x if(k or l or n or o)and g[1][b+1]==" "then local q=((k or n)and 1 or 0)+((l or n)and 2 or 0)+(k and 4 or 0)+(l and 8 or 0)+((k or o)and 16 or 0)+((l or o)and 32 or 0)local
  403. A=g[3][b+1]local B=v==A and d[H]or v if q>=32 then g[1][b+1]=j[63-q];g[2][b+1]=A;g[3][b+1]=B else g[1][b+1]=j[q];g[2][b+1]=B end end end end end end end local function y(e,b,v,z,A,m)local n,o=b.zw,b.
  404. zh local g={}for w=0,b.h-1 do local k=b.vy+a(w/o)local q,r,s={},{},{}for f=0,b.w-1 do local l=b.vx+a(f/n)if l>=0 and k>=0 and l<e.pw and k<e.ph then local x=e.px[k*e.pw+l+1]local t=v[a(k/3)*e.w+a(l/2)
  405. ]if t and t[3]then x=t[3]end q[f+1]=" ";r[f+1]="0";s[f+1]=d[x]else q[f+1]="\127";r[f+1]=z;s[f+1]=A end end g[w]={q,r,s}end if m.frameColour then D(e,b,g,m.frameColour,m.contrast or 0)end for l,q in
  406. pairs(v)do local r,s=l%e.w,a(l/e.w)local f=(r*2-b.vx)*n+n local t=(s*3-b.vy)*o+a(3*o/2)local k=g[t]if k and f>=0 and f<b.w then k[1][f+1]=q[1];k[2][f+1]=d[q[2]]end end return g end function c.rows(f,e
  407. ,b)b=b or{}local k=b.text==false and{}or p.textMap(f)local l=b.void or{7,15}local m,n=d[l[1]],d[l[2]]local r=e.zw and y(f,e,k,m,n,b)or C(f,e,k,m,n)local o={}for q=0,e.h-1 do local g=r[q]o[q+1]={table.
  408. concat(g[1]),table.concat(g[2]),table.concat(g[3])}end return o end function c.draw(e,f,g,k,l,m)for n,b in ipairs(c.rows(k,l,m))do e.setCursorPos(f,g+n-1)e.blit(b[1],b[2],b[3])end end return c end
  409. package.preload["lib.ui"]=function(...)local r={}local i,h,m=math.floor,math.max,math.min local D="0123456789abcdef"local function E(a,b)local c,k,l=i(a/65536)%256,i(a/256)%256,a%256 local d,n,o=i(b/
  410. 65536)%256,i(b/256)%256,b%256 local e,f,g=c-d,k-n,l-o local j=(c+d)/2 return(2+j/256)*e*e+4*f*f+(2+(255-j)/256)*g*g end function r.new(k)local a={}local b=k.win local s=k.onEvent or function()end a.
  411. monitor=k.monitor local c={dark=15,bar=7,text=0,dim=8,accent=11,hi=4,void=7}a.col=c a.mods={ctrl=false,shift=false}function a.pickColours(l,n)local function d(o,p)local f,g=0,math.huge for e=0,15 do if
  412. e~=p and e~=n then local j=E(l[e],o)if j<g then f,g=e,j end end end return f end c.dark=d(0x111111)c.bar=d(0x3C3C3C,c.dark)c.text=d(0xF0F0F0,c.bar)c.dim=d(0x999999,c.bar)c.accent=d(0x3366CC,c.bar)c.hi
  413. =d(0xDEDE6C,c.bar)c.void=d(0x262626)end local function t(d)return D:sub(d+1,d+1)end function a.blitAt(e,f,d,g,j)b.setCursorPos(e,f)b.blit(d,string.rep(t(g),#d),string.rep(t(j),#d))end local n=a.blitAt
  414. function a.rowWidth(f)local d=0 for g,e in ipairs(f)do d=d+#e[1]+(e[4]or 1)end return d end function a.drawRow(g,o,j)local l=b.getSize()b.setCursorPos(1,g)b.setBackgroundColour(2^c.bar);b.clearLine()
  415. local d=1 for p,e in ipairs(o)do local f=e[1]if d>l then break end f=f:sub(1,l-d+1)if e[2]then n(d,g,f,e[3]and c.dark or c.text,e[3]and c.hi or c.dark)j[#j+1]={d,d+#f-1,e[2]}else n(d,g,f,c.text,c.bar)
  416. end d=d+#f+(e[4]or 1)end end function a.pickLayout(...)local f=b.getSize()local d={...}for g,e in ipairs(d)do if a.rowWidth(e)-1<=f then return e end end return d[#d]end function a.bottomButtons(e)
  417. local l,o=b.getSize()local f,d={},l+1 for j=#e,1,-1 do local g=" "..e[j][1].." "d=d-#g n(d,o,g,c.text,c.bar)f[#f+1]={d,d+#g-1,e[j][2]}d=d-1 end return f,d-1 end function a.bottomBar(d,g)local l,e=b.
  418. getSize()b.setCursorPos(1,e)b.setBackgroundColour(2^c.accent);b.setTextColour(2^c.text)b.clearLine()local j,f=a.bottomButtons(g)b.setCursorPos(1,e)b.setBackgroundColour(2^c.accent)if d then b.write(d:
  419. sub(1,h(0,f)))end return j,f end function a.hitButton(f,e)for g,d in ipairs(f)do if e>=d[1]and e<=d[2]then return d[3]end end end function a.panel(f,g)local p=b.getSize()local d,j=k.area()local l=h(1,
  420. i((p-f)/2)+1)local e=h(d,i((j-g)/2)+d)local o=d+j-1 b.setBackgroundColour(2^c.bar)for q=e,m(o,e+g-1)do b.setCursorPos(l,q);b.write(string.rep(" ",f))end return l,e,o end function a.asClick(d)if d[1]==
  421. "mouse_click"and not a.monitor then return d[2],d[3],d[4]end if d[1]=="monitor_touch"and a.monitor and d[2]==a.monitor then return 1,d[3],d[4]end end function a.trackModifiers(e)local d=e[2]if d==keys
  422. .leftCtrl or d==keys.rightCtrl then a.mods.ctrl=e[1]=="key"end if d==keys.leftShift or d==keys.rightShift then a.mods.shift=e[1]=="key"end end function a.modal(f,g)while true do k.draw(f)local d={os.
  423. pullEventRaw()}local e=d[1]if e=="key_up"then a.trackModifiers(d)elseif e=="file_transfer"or e=="term_resize"or e=="monitor_resize"then s(d)else if e=="key"then a.trackModifiers(d)end local j,l=g(d,a.
  424. asClick(d))if j then return l end end end end function a.prompt(F,G,u,v)os.queueEvent("adpaint_flush")while true do local d={os.pullEventRaw()}if d[1]=="adpaint_flush"then break elseif d[1]==
  425. "file_transfer"then s(d)elseif d[1]=="key"or d[1]=="key_up"then a.trackModifiers(d)end end local d=tostring(G or"")local e=#d local w=nil local f,j=nil,1 local function o()f,j=nil,1 if u and e==#d then
  426. local l,g=pcall(u,d)if l and type(g)=="table"and#g>0 then f=g end end end o()local x,y,z local function H()local J,A=b.getSize()local q x,q=a.bottomBar(nil,{{"OK","ok"},{"Cancel","cancel"}})local B=F:
  427. sub(1,h(0,q-8))b.write(B)local g=#B+1 local l=h(1,q-g+1)local p=h(0,e-l+1)b.setBackgroundColour(2^c.dark)b.write((d:sub(p+1,p+l)..string.rep(" ",l)):sub(1,l))if f then local C=g+#d-p local I=f[j]:sub(
  428. 1,h(0,g+l-C))b.setCursorPos(C,A)b.setTextColour(2^c.dim)b.write(I)end y,z=g,g+l-1 b.setCursorPos(g+e-p,A)b.setTextColour(2^c.text)b.setCursorBlink(true)end while true do if v and d~=w then v(d);w=d end
  429. local g=a.modal(H,function(l,A,q,B)local I,C=b.getSize()local p=l[1]if p=="terminate"then return true,{nil}elseif A and B==C then local g=a.hitButton(x,q)if g=="ok"then return true,{d}elseif g==
  430. "cancel"then return true,{nil}elseif f and q>=y and q<=z then d=d..f[j];e=#d;o()end elseif p=="char"or p=="paste"then d=d:sub(1,e)..l[2]..d:sub(e+1)e=e+#l[2]o()elseif p=="key"then local g=l[2]if g==
  431. keys.enter or g==keys.numPadEnter then return true,{d}elseif g==keys.backspace and e>0 then d=d:sub(1,e-1)..d:sub(e+1);e=e-1 elseif g==keys.delete then d=d:sub(1,e)..d:sub(e+2)elseif g==keys.left then
  432. e=h(0,e-1)elseif g==keys.right and f and e==#d then d=d..f[j];e=#d elseif g==keys.right then e=m(#d,e+1)elseif g==keys.home then e=0 elseif g==keys["end"]then e=#d elseif g==keys.tab and f then d=d..f
  433. [j];e=#d elseif g==keys.up and f then j=(j-2)%#f+1 elseif g==keys.down and f then j=j%#f+1 end if g~=keys.up and g~=keys.down then o()end end return true,false end)if g then return g[1]end end end
  434. function a.choose(l,e)local g return a.modal(function()g=a.bottomBar(l,e)end,function(d,o,p,q)local v,u=b.getSize()if d[1]=="terminate"then return true,nil elseif o and q==u then local f=a.hitButton(g
  435. ,p)if f~=nil then return true,f end elseif d[1]=="key"then if d[2]==keys.enter or d[2]==keys.numPadEnter then return true,e[1][2]end local f=keys.getName(d[2])for w,j in ipairs(e)do if f and#f==1 and j
  436. [1]:sub(1,1):lower()==f then return true,j[2]end end end end)end function a.confirm(d)return a.choose(d,{{"Yes",true},{"No",false}})==true end local x={"top left","top","top right","left","centre",
  437. "right","bottom left","bottom","bottom right"}function a.chooseAnchor(y,z)local d=z or 1 local p,l local e=a.modal(function()l={}local G,A=k.area()local e,f=5,A>=15 and 3 or 1 local q,B=3*e+2+4,3*f+2+
  438. 4 local u,v,C=a.panel(q,B)b.setCursorPos(u+2,v+1)b.setTextColour(2^c.text)b.write(y:sub(1,q-4))for g=1,9 do local o=u+2+((g-1)%3)*(e+1)local j=v+3+i((g-1)/3)*(f+1)local F=g==d and c.hi or c.dark for w
  439. =j,j+f-1 do if w<=C then n(o,w,string.rep(" ",e),c.text,F)end end l[g]={o,j,o+e-1,j+f-1}end p=a.bottomBar("Anchor: "..x[d],{{"OK","ok"},{"Cancel","cancel"}})end,function(f,u,g,j)local w,v=b.getSize()
  440. if f[1]=="terminate"then return true,false elseif u then if j==v then local e=a.hitButton(p,g)if e=="ok"then return true,true elseif e=="cancel"then return true,false end else local o=false for q,e in
  441. ipairs(l)do if g>=e[1]and g<=e[3]and j>=e[2]and j<=e[4]then if q==d then o=true end d=q end end if o then return true,true end end elseif f[1]=="key"then local e=f[2]if e==keys.enter or e==keys.
  442. numPadEnter then return true,true elseif e==keys.left and(d-1)%3>0 then d=d-1 elseif e==keys.right and(d-1)%3<2 then d=d+1 elseif e==keys.up and d>3 then d=d-3 elseif e==keys.down and d<7 then d=d+3
  443. end end end)if e then return((d-1)%3)/2,i((d-1)/3)/2 end end function a.wrap(q,l)local d={}for u,o in ipairs(q)do local e=o:match("^ *")if#e>=l then e=""end local f=e local j=true for g in o:gmatch(
  444. "%S+")do while#e+#g>l do if not j then d[#d+1]=f end local p=l-#e d[#d+1]=e..g:sub(1,p)g=g:sub(p+1)f,j=e,true end if j then f=e..g;j=false elseif#f+1+#g>l then d[#d+1]=f;f=e..g else f=f.." "..g end end
  445. d[#d+1]=j and""or f end return d end function a.message(v)local e=1 local q,j,f local function u()local d,g=b.getSize()j=a.wrap(v,h(1,d-2))f=h(1,g-1)e=h(1,m(e,#j-f+1))return d,g end local function g(d
  446. )e=e+d;u()end local function o()return h(1,i(f/3))end return a.modal(function()local p,y=u()b.setBackgroundColour(2^c.dark);b.setTextColour(2^c.text)for d=1,f do b.setCursorPos(1,d);b.clearLine()local
  447. l=j[e+d-1]if l then b.setCursorPos(2,d);b.write(l)end end local d=e+f-1>=#j local l=p>=26 and{{"Up","up"},{d and"End"or"More","down"},{"Close","close"}}or{{"\30","up"},{"\31","down"},{"\215","close"}}
  448. local w=#j<=f and""or(" %d-%d/%d"):format(e,m(#j,e+f-1),#j)q=a.bottomBar(w,l)end,function(l,w,y,z)local B,A=b.getSize()local p=l[1]if p=="terminate"then return true elseif w and z==A then local d=a.
  449. hitButton(q,y)if d=="close"then return true elseif d=="down"then g(o())elseif d=="up"then g(-o())end elseif p=="mouse_scroll"and not a.monitor then g(l[2])elseif p=="key"then local d=l[2]if d==keys.
  450. enter or d==keys.numPadEnter or d==keys.q or d==keys.backspace then if not l[3]then return true end elseif d==keys.down then g(1)elseif d==keys.up then g(-1)elseif d==keys.space then g(o())elseif d==
  451. keys.pageDown then g(f-1)elseif d==keys.pageUp then g(1-f)elseif d==keys.home then e=1 elseif d==keys["end"]then g(#j)end end end)end return a end return r end local k=fs.getDir(shell.
  452. getRunningProgram())package.path="/"..fs.combine(k,"?.lua")..";"..package.path local e=require("lib.cli")local i=
  453. [[
  454. Usage:
  455.  adpaint [file ...]   open .adi files in tabs; PNG,
  456.                       JPEG and BMP files are imported
  457.  adpaint -m <side> [-s <scale>] [file ...]
  458.                       paint on an advanced monitor;
  459.                       -s text scale 0.5-5 (default:
  460.                       setting adpaint.text_scale)
  461.  adpaint convert <image> [out.adi] [options]
  462.    -w, --width <n>    width in cells (default: fit
  463.                       this terminal)
  464.        --height <n>   height (default: keep aspect)
  465.    -c, --colours <n>  colours to use, 2-16 (16)
  466.    -p, --palette <p>  optimised (default) or default
  467.    -n, --no-dither    don't dither
  468.        --mode <m>     bars (default)/fill/stretch
  469.                       when the aspect ratio changes
  470.    -f, --force        overwrite out.adi
  471.  adpaint setup        filename completion at boot
  472.  adpaint help         this text
  473.  
  474. While adpaint runs, drag image files from your
  475. computer onto its window to import them.]]
  476. local l={["-w"]=true,["--width"]=true,["--height"]=true,["-c"]=true,["--colours"]=true,["--colors"]=true,["-s"]=true}local m={"--width","--height","--colours","--palette","--no-dither","--mode",
  477. "--force"}local function n(d,h,b,a)local c=e.completion()if not c then return{}end local f=a[#a]if l[f]then return{}end if f=="-m"then return c.peripheral(d,b,a,true)end if f=="--mode"then return c.
  478. choice(d,b,a,{"bars","fill","stretch"},true)end if f=="-p"or f=="--palette"then return c.choice(d,b,a,{"optimised","default"},true)end local j=a[2]=="convert"if b:sub(1,1)=="-"then return c.choice(d,b
  479. ,a,j and m or{"-m","-s"},true)end local g={}if h==1 then e.addChoices(g,c.choice(d,b,a,{"help","convert","setup"},true))end return e.addChoices(g,c.dirOrFile(d,b,a,true))end e.registerCompletion(n)
  480. local function o()e.setup("-- adpaint: filename completion","/startup/adpaint.lua","It is active now and after every reboot.")end local function p(h)local c={}local f,d,j local a=1 while a<=#h do local
  481. b=h[a]local g=h[a+1]if b=="-w"or b=="--width"then c.w=tonumber(g);a=a+2 elseif b=="--height"then c.h=tonumber(g);a=a+2 elseif b=="-c"or b=="--colours"or b=="--colors"then c.colours=tonumber(g);a=a+2
  482. elseif b=="-p"or b=="--palette"then c.optimise=(g or""):sub(1,1):lower()~="d";a=a+2 elseif b=="-n"or b=="--no-dither"then c.dither=false;a=a+1 elseif b=="--mode"then c.mode=(g or""):lower()if c.mode==
  483. "bars"then c.mode="fit"end if c.mode~="fit"and c.mode~="fill"and c.mode~="stretch"then printError("--mode must be bars, fill or stretch");return end a=a+2 elseif b=="-f"or b=="--force"then j=true;a=a+
  484. 1 elseif b:sub(1,1)=="-"then printError("Unknown option "..b);return elseif not f then f=b;a=a+1 elseif not d then d=b;a=a+1 else printError("Too many arguments");return end end if not f then print(i)
  485. ;return end local b=shell.resolve(f)if not fs.exists(b)or fs.isDir(b)then printError("No such file: "..f);return end d=d and shell.resolve(d)or(b:gsub("%.[^%./]*$","")..".adi")if fs.exists(d)and not j
  486. then write(fs.getName(d).." exists. Overwrite? (y/n) ")local g=read()if not g or g:lower():sub(1,1)~="y"then return end end local q=require("lib.convert")local r=require("lib.formats.adimg")c.yield=e.
  487. yielder()c.status=print local g,s=q.convertFile(b,c)if not g then printError(f..": "..tostring(s));return end local t,u=r.save(g,d)if not t then printError("Save failed: "..tostring(u));return end
  488. print(("Saved %s (%dx%d cells)"):format(d,g.w,g.h))end local b={...}local c=b[1]if c=="--register"then return end if e.isHelp(c)then textutils.pagedPrint(i)return end if c=="setup"then o();return end
  489. if c=="convert"then p({table.unpack(b,2)});return end settings.define("adpaint.text_scale",{description="Default text scale when adpaint runs on a monitor (-m).",default=0.5,type="number",})local d,g,
  490. f={},nil,settings.get("adpaint.text_scale")or 0.5 do local a=1 while a<=#b do if b[a]=="-m"then g=b[a+1];a=a+2 elseif b[a]=="-s"then f=tonumber(b[a+1])or f;a=a+2 else d[#d+1]=b[a];a=a+1 end end end
  491. require("lib.editor.app").run(d,g,f)
  492.  
Advertisement
Add Comment
Please, Sign In to add comment