SquidDev

BitmapToFont.lua

Jul 12th, 2014
414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.59 KB | None | 0 0
  1. os.unloadAPI("ImagesAPI")
  2. if not ImagesAPI then
  3.     os.loadAPI(fs.combine(fs.getDir(shell.getRunningProgram()), "ImagesAPI"))
  4. end
  5.  
  6. local Args = {...}
  7. if #Args < 2 then
  8.     error("BitmapToFont.lua <Bitmap> <Font>")
  9. end
  10.  
  11. local Version = 1
  12.  
  13. local function ColoursEqual(A, B)
  14.     return (A[0] == B[0] and A[1] == B[1] and A[2] == B[2])
  15. end
  16.  
  17. local BinFile = ImagesAPI.BinaryFile:new(Args[1])
  18. local Parser = ImagesAPI.BitmapParser:new(BinFile)
  19.  
  20. local Characters = {}
  21.  
  22. local CharacterWidth = 0
  23. local CharacterHeight = 0
  24.  
  25. local Background = Parser.Pixels[1][1]
  26. local Black = {0, 0, 0}
  27.  
  28. local LastCharacterCode = 31
  29.  
  30.  
  31. local function ParseCharacter(StartX, StartY)
  32.     local Character = {}
  33.     local MaxX = Parser.Width
  34.  
  35.     for Y = StartY, Parser.Height, 1 do
  36.         if ColoursEqual(Parser.Pixels[Y][StartX], Background) then
  37.             CharacterWidth = math.max(MaxX - StartX, CharacterWidth)
  38.             CharacterHeight = math.max(Y - StartY, CharacterHeight)
  39.  
  40.  
  41.             return Character, MaxX - StartX
  42.         end
  43.  
  44.         local Row = {}
  45.        
  46.         for X = StartX, MaxX, 1 do
  47.             local Colour = Parser.Pixels[Y][X]
  48.  
  49.             if ColoursEqual(Colour, Background) then
  50.                 MaxX = X
  51.                 break
  52.             elseif ColoursEqual(Colour, Black) then
  53.                 table.insert(Row, 0)
  54.             else
  55.                 table.insert(Row, 1)
  56.             end
  57.         end
  58.  
  59.         table.insert(Character, Row)
  60.     end
  61. end
  62.  
  63. local Y = 1
  64. while Y <= Parser.Height do
  65.     local X = 1
  66.     local HadCharacter = false
  67.  
  68.     while X <= Parser.Width do
  69.         local Colour = Parser.Pixels[Y][X]
  70.  
  71.         if ColoursEqual(Black, Colour) then
  72.             LastCharacterCode = LastCharacterCode + 1
  73.             local Pix, Width = ParseCharacter(X, Y)
  74.  
  75.             Characters[LastCharacterCode] = Pix
  76.             X = X + Width
  77.  
  78.             HadCharacter = true
  79.         end
  80.  
  81.         X = X + 1
  82.     end
  83.  
  84.     if HadCharacter then
  85.         Y = Y + CharacterHeight
  86.     end
  87.     Y = Y + 1
  88. end
  89.  
  90. local Output = fs.open(Args[2], "wb")
  91. Output.write(Version)
  92. Output.write(CharacterWidth)
  93. Output.write(CharacterHeight)
  94.  
  95. for Character, Pixels in pairs(Characters) do
  96.     Output.write(Character)
  97.  
  98.     local Width = #Pixels[1]
  99.     local Padding = math.floor((CharacterWidth - Width) / 2)
  100.  
  101.     local Count = 0
  102.     local Byte = 0
  103.     for Y = 1, CharacterHeight, 1 do
  104.         os.queueEvent("a")
  105.         os.pullEvent()
  106.  
  107.         local Row = Pixels[Y]
  108.         for X = 1, CharacterWidth do
  109.             X = X - Padding
  110.            
  111.             if Row==nil then
  112.                 break
  113.             end
  114.             local Cell = Row[X]
  115.  
  116.             if Cell == nil then
  117.                 Cell = 0
  118.             end
  119.  
  120.             Byte = (Byte * 2) + Cell
  121.             Count = Count + 1
  122.  
  123.             if Count == 8 then
  124.                 Output.write(Byte)
  125.                 Count = 0
  126.                 Byte = 0
  127.             end
  128.         end
  129.     end
  130.  
  131.     if Count ~= 0 then
  132.         Output.write(Byte * math.pow(2, 8 - Count))
  133.     end
  134. end
  135.  
  136. Output.close()
Advertisement
Add Comment
Please, Sign In to add comment