Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --saves a 24 bit uncompressed bmp based
- --on a 2d table of {r,g,b} indices
- --(each channel's value should be no
- --more than 255, of course)
- function bmp24(map,path)
- --parse parameters:
- if type(map) ~= 'table' then
- return nil,'Map parameter is not table'
- end
- if path and type(path) ~= 'string' then
- return nil,'Path parameter exists, but is not string'
- end
- path=path or '/'..shell.dir()..'/untitled.bmp'
- --idiot check to see if rows are same len:
- local idiot=false
- local temp=0
- local temp2=math.huge
- for iy,mr in ipairs(map) do
- if #mr > temp then temp=#mr end
- if temp2 < temp then
- idiot=true break end
- temp2=#mr
- end
- if idiot then --if idiot then add magenta to missing columns
- for iy,mr in ipairs(map) do
- while #mr < temp do
- mr[#mr+1]={255,0,255}
- end
- end
- end
- --open file and write header
- local function littleEnd(num)
- if num==0 then return {'00'} end
- local bEnd=string.format("%x",num)
- if #bEnd%2 == 1 then bEnd='0'..bEnd end
- local lEnd={}
- for i=1,#bEnd,2 do lEnd[#lEnd+1]=bEnd:sub(i,i+1) end
- local fl=0
- for i=1,math.floor(#lEnd/2) do
- fl=#lEnd-i+1
- lEnd[i],lEnd[fl]=lEnd[fl],lEnd[i]
- end
- return lEnd
- end
- local function fillTab(tab,to)
- while #tab < to do tab[#tab+1]='00' end
- return tab
- end
- local file=io.open(path,'wb')
- --file signature:2 bytes, big
- file:write(66)--"B"
- file:write(77)--"M"
- local rowPadding=(4-(3*#map[1])%4)%4 --wow i probably did that badly but i dont care
- local fileSize=54+(3*#map[1]*#map)+(rowPadding*#map)
- --file size: 4 bytes, little
- for i,v in ipairs(fillTab(littleEnd(fileSize),4)) do--lol
- file:write(tonumber(v,16)) end
- --reserved: 4 bytes, doesn't matter it's just zero lol
- for i=1,4 do file:write(0) end
- --bitmap data offset: 4 bytes, little
- file:write(54)
- for i=1,3 do file:write(0) end
- --infoheader size: 4 bytes, little
- file:write(40)
- for i=1,3 do file:write(0) end
- --image width: 4 bytes, little
- for i,v in ipairs(fillTab(littleEnd(#map[1]),4)) do
- file:write(tonumber(v,16)) end
- --image height: 4 bytes, little
- for i,v in ipairs(fillTab(littleEnd(#map),4)) do
- file:write(tonumber(v,16)) end
- --number of planes: 2 bytes, little
- file:write(1)
- file:write(0)
- --number of bits per pixel: 2 bytes, little
- file:write(24)
- file:write(0)
- --compression type: 4 bytes, little
- --compressed image size: 4 bytes, little
- --x pixels per meter: 4 bytes, little
- --y pixels per meter: 4 bytes, little
- --used colors:4 bytes, little
- --important colors: 4 bytes, little
- for i=1,24 do file:write(0) end
- --write pixel data
- for iy=#map,1,-1 do
- for ix=1,#map[iy] do
- for ic=3,1,-1 do
- file:write(map[iy][ix][ic])
- end
- end
- if rowPadding > 0 then
- for i=1,rowPadding do
- file:write(0)
- end
- end
- end
- file:close()
- return true --it's finally over
- end
- --hopefully the below line works with require if uncommented
- --return bmp24
- --[[
- --i used below for testing
- local bmap={}
- local count=255
- local op=-1
- local res={16,16}
- for iy=1,res[2] do
- bmap[#bmap+1]={}
- for ix=1,res[1] do
- bmap[iy][#bmap[iy]+1]={count,count,count}
- count=count+op
- end
- --this line tests the uneven rows failsafe
- if iy==res[2] then bmap[iy][#bmap[iy]+1]={255,0,0} end
- bmap[#bmap+1]=row
- end
- bmp24(bmap)
- --]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement