jig487

imageConverter

Aug 21st, 2021 (edited)
427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.82 KB | None | 0 0
  1. --[[
  2.  
  3. ### Image processing code from fyvushfinkle ###
  4.  
  5. ### HOW TO USE: ###
  6.  
  7. tArgs:
  8. [1] = name of input file
  9. [2] = name of output file
  10. [3] = width of image being converted
  11. [4] = height of image being converted
  12.  
  13. Example program use:
  14. imgC shrek shrekC 64 64
  15.  
  16. ]]
  17.  
  18. local tArgs = {...}
  19.  
  20. if tArgs[1] == nil then error("Missing input file name")
  21. elseif tArgs[2] == nil then error("Missing output file name")
  22. elseif tArgs[3] == nil then error("Missing image width")
  23. elseif tArgs[4] == nil then error("Missing image height")
  24. end
  25.  
  26. local file, err = fs.open(tArgs[1], "r")
  27. assert(file, err)
  28. --local file = fs.open("texture", "r")
  29.  
  30. local colorList = {
  31.     size = { x = tArgs[3], y = tArgs[4] },
  32. }
  33.  
  34. local W = tArgs[3] --16
  35. local H = tArgs[4] --16
  36.  
  37. --pixelSize used for drawing to screen. Not needed for raw image processing
  38. local pixelSize = tArgs[5] or 5
  39.  
  40. for y=1, H do
  41.     for x=1, W do
  42.         local r = file.readLine()
  43.         local g = file.readLine()
  44.         local b = file.readLine()
  45.         local a = file.readLine()
  46.        
  47.         if a == nil then
  48.             break
  49.         end
  50.        
  51.         local rgb = 0
  52.        
  53.         if a ~= 0 then
  54.            
  55.             r = bit.blshift(r, 16)
  56.             g = bit.blshift(g, 8)
  57.             rgb = bit.bor(bit.bor(r, g), b)
  58.         end
  59.        
  60.         colorList[y * W + x] = rgb
  61.     end    
  62. end
  63. file.close()
  64.  
  65. local file, err = fs.open(tArgs[2], "w")
  66. assert(file, err)
  67. file.write(textutils.serialize(colorList))
  68. file.close()
  69.  
  70.  
  71. --[[
  72. for y = 1, H do
  73.     for x = 1, W do
  74.         local col = colorList[y * W + x]
  75.         if col == nil then
  76.             break
  77.         end
  78.         --print(string.format("%x", col))
  79.         ar.fill(x * pixelSize, y * pixelSize, (x+1) * pixelSize, (y+1) * pixelSize, col)
  80.         --os.sleep(0.1)
  81.     end
  82. end
  83. ]]
Add Comment
Please, Sign In to add comment