Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Args = {...}
- if #Args < 2 then
- printError("Usage: Nfp2Bmp <InputFile> <OutputFile> [Background-Colour-Name]")
- printError("Usage: Nfp2Bmp <InputFile> <OutputFile> [Backg-R] [Back-G] [Back-B]")
- error()
- end
- local Colours = {
- {240, 240, 240}, -- colors.white
- {242, 178, 51}, -- colors.orange
- {229, 127, 216}, -- colors.magenta
- {153, 178, 242}, -- colors.lightBlue
- {222, 222, 108}, -- colors.yellow
- {127, 204, 25}, -- colors.lime
- {242, 178, 204}, -- colors.pink
- {76, 76, 76}, -- colors.gray
- {153, 153, 153}, -- colors.lightGray
- {76, 153, 178}, -- colors.cyan
- {178, 102, 229}, -- colors.purple
- {37, 49, 146}, -- colors.blue
- {127, 102, 76}, -- colors.brown
- {5, 122, 100}, -- colors.green
- {204, 76, 76}, -- colors.red
- {0, 0, 0}, -- colors.black
- }
- local ColourLookup = {
- ["0"] = 0,
- ["1"] = 1,
- ["2"] = 2,
- ["3"] = 3,
- ["4"] = 4,
- ["5"] = 5,
- ["6"] = 6,
- ["7"] = 7,
- ["8"] = 8,
- ["9"] = 9,
- ["a"] = 10,
- ["b"] = 11,
- ["c"] = 12,
- ["d"] = 13,
- ["e"] = 14,
- ["f"] = 15,
- [" "] = 16,
- }
- local Colour = {0, 0, 0}
- if #Args == 3 then
- --Actually log(x, 2) so colours.white = 1 => log(1, 2) = 0
- --Applies for other colours
- Colour = math.log(colours[Args[3]]) / math.log(2)
- elseif #Args == 5 then
- Colour = {tonumber(Args[3]), tonumber(Args[4]), tonumber(Args[5])}
- end
- table.insert(Colours, Colour)
- local Pixels = {}
- local Input = fs.open(Args[1], "r")
- local Width = 0
- local Height = 0
- local Line = Input.readLine()
- while Line do
- local Row = {}
- for X=1, Line:len() do
- Row[X] = ColourLookup[string.sub(Line, X, X)] or 16
- end
- Width = math.max(Width, #Row)
- table.insert(Pixels, Row)
- Line = Input.readLine()
- end
- Input.close()
- Height = #Pixels
- local Output = fs.open(Args[2], "wb")
- function Write(Number, Size)
- while Size > 0 do
- Output.write(Number % 256)
- Number = math.floor(Number / 256)
- Size = Size - 1
- end
- end
- --Bitmap header
- Output.write(66) --B
- Output.write(77) --M
- --How big are we?
- -- 54 for total headers + 256 * 4 for colour map
- Write(1078 + (Width * Height), 4)
- for _=1,4,1 do
- Output.write(0)
- end
- --When do we start the actual content?
- -- 54 for total headers + 256 * 4 for colour map
- Write(1078, 4)
- --DIB Header
- Write(40, 4)
- Write(Width, 4)
- Write(Height, 4)
- Write(1, 2) --Colour planes
- Write(8, 2)
- --Compression (none) (4 bytes)
- --Size (not needed if no compression) (4 bytes)
- --Resolution Horizontal (4 bytes) and Vertical (4 bytes)
- --Number of colours (4 bytes)
- --Number of important colours (4 bytes)
- for _=1,24,1 do
- Output.write(0)
- end
- --Colour table!
- for I=1, 256, 1 do
- local Colour = Colours[I]
- if Colour == nil then
- for _=1,4,1 do
- Output.write(0)
- end
- else
- Output.write(Colour[3]) --Goes BGR for silly reason
- Output.write(Colour[2])
- Output.write(Colour[1])
- Output.write(0)
- end
- end
- for Y = Height, 1, -1 do
- local Row = Pixels[Y]
- local Counter = 0
- for X = 1, Width, 1 do
- Counter = (Counter + 1) % 4
- Output.write(Row[X])
- end
- if Counter ~= 0 then
- for I=Counter, 3 do
- Output.write(0)
- end
- end
- end
- Output.close()
Advertisement
Add Comment
Please, Sign In to add comment