Guest User

Untitled

a guest
Feb 12th, 2013
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.12 KB | None | 0 0
  1. colorRGB = {
  2.     {r = 221, g = 221, b = 221, code = "0"},
  3.     {r = 219, g = 125, b = 62, code = "1"},
  4.     {r = 179, g = 80, b = 188, code = "2"},
  5.     {r = 107, g = 138, b = 201, code = "3"},
  6.     {r = 177, g = 166, b = 39, code = "4"},
  7.     {r = 65, g = 174, b = 56, code = "5"},
  8.     {r = 208, g = 132, b = 153, code = "6"},
  9.     {r = 64, g = 64, b = 64, code = "7"},
  10.     {r = 154, g = 161, b = 161, code = "8"},
  11.     {r = 46, g = 110, b = 137, code = "9"},
  12.     {r = 126, g = 61, b = 181, code = "a"},
  13.     {r = 46, g = 56, b = 141, code = "b"},
  14.     {r = 79, g = 50, b = 31, code = "c"},
  15.     {r = 53, g = 70, b = 27, code = "d"},
  16.     {r = 150, g = 52, b = 48, code = "e"},
  17.     {r = 25, g = 22, b = 22, code = "f"}
  18. }
  19.  
  20. function readWORD(str, offset)
  21.     local loByte = str:byte(offset)
  22.     local hiByte = str:byte(offset + 1)
  23.     return hiByte * 256 + loByte
  24. end
  25.  
  26. function readDWORD(str, offset)
  27.     local loWord = readWORD(str, offset)
  28.     local hiWord = readWORD(str, offset + 2)
  29.     return hiWord * 65536 + loWord
  30. end
  31.  
  32. function get4BitColor(r, g, b)
  33.     for k, v in ipairs(colorRGB) do
  34.         -- print(r .. "/" .. v.r .. " " .. g .. "/" .. v.g .. " " .. b .. "/" .. v.b)
  35.  
  36.         local lookVel = 10
  37.  
  38.         if r > v.r - lookVel and r < v.r + lookVel and g > v.g - lookVel and g < v.g + lookVel and b > v.b - lookVel and b < v.b + lookVel then
  39.             return v.code
  40.         end
  41.     end
  42.  
  43.     return " "
  44. end
  45.  
  46. function loadBitmap(path)
  47.     local pixels = {}
  48.  
  49.     local f = assert(io.open(path, "rb"))
  50.     local bytecode = ""
  51.     local byte = f:read()
  52.  
  53.     while byte ~= nil do
  54.         bytecode = bytecode .. string.char(byte)
  55.         byte = f:read()
  56.     end
  57.  
  58.     f:close()
  59.  
  60.     -- Parse BITMAPFILEHEADER
  61.     local offset = 1
  62.     local bfType = readWORD(bytecode, offset)
  63.     if(bfType ~= 0x4D42) then
  64.         print("Not a bitmap file (Invalid BMP magic value)")
  65.         error()
  66.     end
  67.     local offBits = readWORD(bytecode, offset + 10)
  68.  
  69.     -- Parse BITMAPINFOHEADER
  70.     offset = 15; -- BITMAPFILEHEADER is 14 bytes long
  71.     local width = readDWORD(bytecode, offset + 4)
  72.     local height = readDWORD(bytecode, offset + 8)
  73.     local bitCount = readWORD(bytecode, offset + 14)
  74.     local compression = readDWORD(bytecode, offset + 16)
  75.  
  76.     if(bitCount ~= 24) then
  77.         print("Only 24-bit bitmaps supported (Is " .. bitCount .. "bpp)")
  78.         error()
  79.     end
  80.  
  81.     if(compression ~= 0) then
  82.         print("Only uncompressed bitmaps supported (Compression type is " .. compression .. ")")
  83.         error()
  84.     end
  85.  
  86.     print("Found width " .. width .. "*" .. height)
  87.  
  88.     -- Parse image
  89.     for y = height - 1, 0, -1 do
  90.         offset = offBits + (width * bitCount / 8) * y + 1
  91.  
  92.         for x = 1, width do
  93.             if type(pixels[x]) ~= "table" then
  94.                 pixels[x] = {}
  95.             end
  96.  
  97.             local b = bytecode:byte(offset)
  98.             local g = bytecode:byte(offset + 1)
  99.             local r = bytecode:byte(offset + 2)
  100.             offset = offset + 3
  101.  
  102.             -- print("RGB: " .. r .. " " .. g .. " " .. b)
  103.  
  104.             pixels[x][height - y] = get4BitColor(r, g, b)
  105.         end
  106.     end
  107.  
  108.     -- Generate the image
  109.     local tmpF = io.open("_tmp.temp", "w")
  110.    
  111.     for y = 1, height do
  112.         for x = 1, width do
  113.             tmpF:write(pixels[x][y])
  114.         end
  115.  
  116.         tmpF:write("\n")
  117.     end
  118.  
  119.     tmpF:close()
  120.  
  121.     if paintutils then
  122.         local img = paintutils.loadImage("_tmp.temp")
  123.         fs.delete("_tmp.temp")
  124.  
  125.         return img
  126.     end
  127. end
Advertisement
Add Comment
Please, Sign In to add comment