SquidDev

NfpToBmp.lua

Jul 13th, 2014
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.12 KB | None | 0 0
  1. local Args = {...}
  2.  
  3. if #Args < 2 then
  4.     printError("Usage: Nfp2Bmp <InputFile> <OutputFile> [Background-Colour-Name]")
  5.     printError("Usage: Nfp2Bmp <InputFile> <OutputFile> [Backg-R] [Back-G] [Back-B]")
  6.     error()
  7. end
  8.  
  9. local Colours = {
  10.     {240, 240, 240},    -- colors.white
  11.     {242, 178, 51},     -- colors.orange
  12.     {229, 127, 216},    -- colors.magenta
  13.     {153, 178, 242},    -- colors.lightBlue
  14.     {222, 222, 108},    -- colors.yellow
  15.     {127, 204, 25},     -- colors.lime
  16.     {242, 178, 204},    -- colors.pink
  17.     {76, 76, 76},       -- colors.gray
  18.     {153, 153, 153},    -- colors.lightGray
  19.     {76, 153, 178},     -- colors.cyan
  20.     {178, 102, 229},    -- colors.purple
  21.     {37, 49, 146},      -- colors.blue
  22.     {127, 102, 76},     -- colors.brown
  23.     {5, 122, 100},      -- colors.green
  24.     {204, 76, 76},      -- colors.red
  25.     {0, 0, 0},          -- colors.black
  26. }
  27.  
  28. local ColourLookup = {
  29.     ["0"] = 0,
  30.     ["1"] = 1,
  31.     ["2"] = 2,
  32.     ["3"] = 3,
  33.     ["4"] = 4,
  34.     ["5"] = 5,
  35.     ["6"] = 6,
  36.     ["7"] = 7,
  37.     ["8"] = 8,
  38.     ["9"] = 9,
  39.     ["a"] = 10,
  40.     ["b"] = 11,
  41.     ["c"] = 12,
  42.     ["d"] = 13,
  43.     ["e"] = 14,
  44.     ["f"] = 15,
  45.     [" "] = 16,
  46. }
  47.  
  48.  
  49. local Colour = {0, 0, 0}
  50. if #Args == 3 then
  51.     --Actually log(x, 2) so colours.white = 1 => log(1, 2) = 0
  52.     --Applies for other colours
  53.     Colour = math.log(colours[Args[3]]) / math.log(2)
  54. elseif #Args == 5 then
  55.     Colour = {tonumber(Args[3]), tonumber(Args[4]), tonumber(Args[5])}
  56. end
  57.  
  58. table.insert(Colours, Colour)
  59.  
  60.  
  61. local Pixels = {}
  62.  
  63. local Input = fs.open(Args[1], "r")
  64.  
  65. local Width = 0
  66. local Height = 0
  67.  
  68. local Line = Input.readLine()
  69. while Line do
  70.     local Row = {}
  71.  
  72.     for X=1, Line:len() do
  73.         Row[X] = ColourLookup[string.sub(Line, X, X)] or 16
  74.     end
  75.  
  76.     Width = math.max(Width, #Row)
  77.  
  78.     table.insert(Pixels, Row)
  79.     Line = Input.readLine()
  80. end
  81.  
  82. Input.close()
  83.  
  84. Height = #Pixels
  85.  
  86.  
  87. local Output = fs.open(Args[2], "wb")
  88.  
  89. function Write(Number, Size)
  90.     while Size > 0 do
  91.         Output.write(Number % 256)
  92.  
  93.         Number = math.floor(Number / 256)
  94.         Size = Size - 1
  95.     end
  96. end
  97.  
  98.  
  99. --Bitmap header
  100.  
  101. Output.write(66)    --B
  102. Output.write(77)    --M
  103.  
  104. --How big are we?
  105. -- 54 for total headers + 256 * 4 for colour map
  106. Write(1078 + (Width * Height), 4)
  107.  
  108. for _=1,4,1 do
  109.     Output.write(0)
  110. end
  111.  
  112. --When do we start the actual content?
  113. -- 54 for total headers + 256 * 4 for colour map
  114. Write(1078, 4)
  115.  
  116. --DIB Header
  117. Write(40, 4)
  118.  
  119. Write(Width, 4)
  120. Write(Height, 4)
  121.  
  122. Write(1, 2) --Colour planes
  123.  
  124. Write(8, 2)
  125. --Compression (none) (4 bytes)
  126. --Size (not needed if no compression) (4 bytes)
  127. --Resolution Horizontal (4 bytes) and Vertical (4 bytes)
  128. --Number of colours (4 bytes)
  129. --Number of important colours (4 bytes)
  130. for _=1,24,1 do
  131.     Output.write(0)
  132. end
  133.  
  134. --Colour table!
  135. for I=1, 256, 1 do
  136.     local Colour = Colours[I]
  137.  
  138.     if Colour == nil then
  139.         for _=1,4,1 do
  140.             Output.write(0)
  141.         end
  142.     else
  143.         Output.write(Colour[3])     --Goes BGR for silly reason
  144.         Output.write(Colour[2])
  145.         Output.write(Colour[1])
  146.         Output.write(0)
  147.     end
  148. end
  149.  
  150. for Y = Height, 1, -1 do
  151.     local Row = Pixels[Y]
  152.     local Counter = 0
  153.  
  154.     for X = 1, Width, 1 do
  155.         Counter = (Counter + 1) % 4
  156.         Output.write(Row[X])
  157.     end
  158.  
  159.     if Counter ~= 0 then
  160.         for I=Counter, 3 do
  161.             Output.write(0)
  162.         end
  163.     end
  164. end
  165.  
  166. Output.close()
Advertisement
Add Comment
Please, Sign In to add comment