Advertisement
nonogamer9

teest

Feb 9th, 2025 (edited)
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.99 KB | None | 0 0
  1. local function readBMP(filename)
  2. local file = fs.open(filename, "rb")
  3. if not file then
  4. return nil, "Failed to open file"
  5. end
  6.  
  7. local header = file.read(54)
  8. local width = string.byte(header, 19) + string.byte(header, 20) * 256
  9. local height = string.byte(header, 23) + string.byte(header, 24) * 256
  10.  
  11. local pixels = {}
  12. for y = height, 1, -1 do
  13. pixels[y] = {}
  14. for x = 1, width do
  15. local b = file.read(1)
  16. local g = file.read(1)
  17. local r = file.read(1)
  18. pixels[y][x] = {r = string.byte(r), g = string.byte(g), b = string.byte(b)}
  19. end
  20. if y % 10 == 0 then
  21. os.queueEvent("yield")
  22. os.pullEvent("yield")
  23. end
  24. end
  25.  
  26. file.close()
  27. return {width = width, height = height, pixels = pixels}
  28. end
  29.  
  30. local function resizeImage(image, newWidth, newHeight)
  31. local resized = {width = newWidth, height = newHeight, pixels = {}}
  32. for y = 1, newHeight do
  33. resized.pixels[y] = {}
  34. for x = 1, newWidth do
  35. local srcX = math.floor(x * image.width / newWidth)
  36. local srcY = math.floor(y * image.height / newHeight)
  37. resized.pixels[y][x] = image.pixels[srcY][srcX]
  38. end
  39. if y % 10 == 0 then
  40. os.queueEvent("yield")
  41. os.pullEvent("yield")
  42. end
  43. end
  44. return resized
  45. end
  46.  
  47. local function rgbToComputerCraftColor(r, g, b)
  48. local colors = {
  49. [0x1] = {240, 240, 240}, [0x2] = {242, 178, 51}, [0x4] = {229, 127, 216},
  50. [0x8] = {153, 178, 242}, [0x10] = {222, 222, 108}, [0x20] = {127, 204, 25},
  51. [0x40] = {242, 178, 204}, [0x80] = {76, 76, 76}, [0x100] = {153, 153, 153},
  52. [0x200] = {76, 153, 178}, [0x400] = {178, 102, 229}, [0x800] = {51, 102, 204},
  53. [0x1000] = {127, 102, 76}, [0x2000] = {87, 166, 78}, [0x4000] = {204, 76, 76},
  54. [0x8000] = {17, 17, 17}
  55. }
  56.  
  57. local closestColor = 0x1
  58. local minDifference = math.huge
  59.  
  60. for code, rgb in pairs(colors) do
  61. local difference = math.sqrt((r - rgb[1])^2 + (g - rgb[2])^2 + (b - rgb[3])^2)
  62. if difference < minDifference then
  63. minDifference = difference
  64. closestColor = code
  65. end
  66. end
  67.  
  68. return closestColor
  69. end
  70.  
  71. local function displayBMP(image, output)
  72. local width, height = output.getSize()
  73. local resizedImage = resizeImage(image, width, height)
  74.  
  75. for y = 1, height do
  76. for x = 1, width do
  77. local pixel = resizedImage.pixels[y][x]
  78. local color = rgbToComputerCraftColor(pixel.r, pixel.g, pixel.b)
  79. output.setCursorPos(x, y)
  80. output.setBackgroundColor(color)
  81. output.write(" ")
  82. end
  83. if y % 5 == 0 then
  84. os.queueEvent("yield")
  85. os.pullEvent("yield")
  86. end
  87. end
  88. end
  89.  
  90. local function displayBMPMode2(image, output)
  91. local width, height = output.getSize()
  92. local resizedImage = resizeImage(image, width * 2, height * 2)
  93.  
  94. local charSet = {" ", "\131", "\132", "\133", "\135", "\136", "\137", "\138", "\139", "\140"}
  95.  
  96. local colors = {
  97. [0x1] = {240, 240, 240}, [0x2] = {242, 178, 51}, [0x4] = {229, 127, 216},
  98. [0x8] = {153, 178, 242}, [0x10] = {222, 222, 108}, [0x20] = {127, 204, 25},
  99. [0x40] = {242, 178, 204}, [0x80] = {76, 76, 76}, [0x100] = {153, 153, 153},
  100. [0x200] = {76, 153, 178}, [0x400] = {178, 102, 229}, [0x800] = {51, 102, 204},
  101. [0x1000] = {127, 102, 76}, [0x2000] = {87, 166, 78}, [0x4000] = {204, 76, 76},
  102. [0x8000] = {17, 17, 17}
  103. }
  104.  
  105. local function getNearestColor(r, g, b)
  106. local closestColor = 0x1
  107. local minDifference = math.huge
  108. for code, rgb in pairs(colors) do
  109. local difference = math.abs(r - rgb[1]) + math.abs(g - rgb[2]) + math.abs(b - rgb[3])
  110. if difference < minDifference then
  111. minDifference = difference
  112. closestColor = code
  113. end
  114. end
  115. return closestColor
  116. end
  117.  
  118. local function blendColors(c1, c2)
  119. return {
  120. r = (c1.r + c2.r) / 2,
  121. g = (c1.g + c2.g) / 2,
  122. b = (c1.b + c2.b) / 2
  123. }
  124. end
  125.  
  126. for y = 1, height do
  127. for x = 1, width do
  128. local tl = resizedImage.pixels[y*2-1][x*2-1]
  129. local tr = resizedImage.pixels[y*2-1][x*2]
  130. local bl = resizedImage.pixels[y*2][x*2-1]
  131. local br = resizedImage.pixels[y*2][x*2]
  132.  
  133. local avgColor = blendColors(blendColors(tl, tr), blendColors(bl, br))
  134. local intensity = (avgColor.r + avgColor.g + avgColor.b) / 3
  135. local pattern = math.floor(intensity / 28.4) + 1
  136.  
  137. local bgColor = getNearestColor(tl.r, tl.g, tl.b)
  138. local fgColor = getNearestColor(br.r, br.g, br.b)
  139.  
  140. if bgColor == fgColor then
  141. if pattern > 5 then
  142. fgColor = getNearestColor(math.min(br.r + 30, 255), math.min(br.g + 30, 255), math.min(br.b + 30, 255))
  143. else
  144. bgColor = getNearestColor(math.max(tl.r - 30, 0), math.max(tl.g - 30, 0), math.max(tl.b - 30, 0))
  145. end
  146. end
  147.  
  148. output.setCursorPos(x, y)
  149. output.setBackgroundColor(bgColor)
  150. output.setTextColor(fgColor)
  151. output.write(charSet[pattern])
  152. end
  153. if y % 5 == 0 then
  154. os.queueEvent("yield")
  155. os.pullEvent("yield")
  156. end
  157. end
  158. end
  159.  
  160. local function displayBMPCraftOSPC(image, output, colorMode)
  161. local screenWidth, screenHeight = output.getSize()
  162. local imageWidth, imageHeight = image.width, image.height
  163.  
  164. -- Store the original palette and graphics mode
  165. local originalPalette = {}
  166. for i = 0, 255 do
  167. originalPalette[i] = {term.getPaletteColor(i)}
  168. end
  169. local originalGraphicsMode = term.getGraphicsMode()
  170. -- Store the original text colors
  171. local originalBackgroundColor = term.getBackgroundColor()
  172. local originalTextColor = term.getTextColor()
  173.  
  174. term.setGraphicsMode(colorMode)
  175.  
  176. if colorMode == 2 then
  177. -- Preserve first 16 colors, set up custom palette for the rest
  178. -- Define a color cube palette with more distinct colors
  179. local cubeSize = 5 -- Number of steps for each color component (R, G, B)
  180. for i = 16, 255 do
  181. local index = i - 16
  182. local rIndex = math.floor(index / (cubeSize * cubeSize))
  183. local gIndex = math.floor((index % (cubeSize * cubeSize)) / cubeSize)
  184. local bIndex = index % cubeSize
  185.  
  186. local r = math.floor(rIndex * 255 / (cubeSize - 1))
  187. local g = math.floor(gIndex * 255 / (cubeSize - 1))
  188. local b = math.floor(bIndex * 255 / (cubeSize - 1))
  189. term.setPaletteColor(i, r / 255, g / 255, b / 255)
  190. end
  191. end
  192.  
  193. local function adjustBrightness(value)
  194. return math.min(255, math.max(0, math.floor(value * 1.2))) -- Increase brightness by 20%
  195. end
  196.  
  197. local function mapColor(r, g, b)
  198. r, g, b = adjustBrightness(r), adjustBrightness(g), adjustBrightness(b)
  199. if colorMode == 2 then
  200. -- Map to the custom color cube palette
  201. local cubeSize = 5
  202. local rIndex = math.floor(r / (255 / (cubeSize - 1)))
  203. local gIndex = math.floor(g / (255 / (cubeSize - 1)))
  204. local bIndex = math.floor(b / (255 / (cubeSize - 1)))
  205.  
  206. return 16 + rIndex * cubeSize * cubeSize + gIndex * cubeSize + bIndex
  207. else
  208. return rgbToComputerCraftColor(r, g, b)
  209. end
  210. end
  211.  
  212. for y = 0, screenHeight * 9 - 1 do
  213. for x = 0, screenWidth * 6 - 1 do
  214. local sourceX = math.floor(x * imageWidth / (screenWidth * 6))
  215. local sourceY = math.floor(y * imageHeight / (screenHeight * 9))
  216.  
  217. local pixel = image.pixels[sourceY + 1][sourceX + 1]
  218. local color = mapColor(pixel.r, pixel.g, pixel.b)
  219.  
  220. output.setPixel(x, y, color)
  221. end
  222. if y % 10 == 0 then
  223. os.queueEvent("yield")
  224. os.pullEvent("yield")
  225. end
  226. end
  227.  
  228. print("Image displayed. Press any key to exit.")
  229. os.pullEvent("key")
  230.  
  231. -- Restore the original palette and graphics mode
  232. term.setGraphicsMode(originalGraphicsMode)
  233. for i = 0, 255 do
  234. term.setPaletteColor(i, table.unpack(originalPalette[i]))
  235. end
  236. -- Restore the original text colors
  237. term.setBackgroundColor(originalBackgroundColor)
  238. term.setTextColor(originalTextColor)
  239.  
  240. -- Clear the screen and reset cursor position
  241. term.setBackgroundColor(colors.black)
  242. term.setTextColor(colors.white)
  243. term.clear()
  244. term.setCursorPos(1, 1)
  245. end
  246.  
  247. local function downloadBMP(url, filename)
  248. if not http then
  249. error("HTTP API is not enabled. Enable it in ComputerCraft.cfg")
  250. end
  251.  
  252. print("Downloading " .. url .. "...")
  253. local response = http.get(url, nil, true)
  254. if not response then
  255. error("Failed to download file")
  256. end
  257.  
  258. local file = fs.open(filename, "wb")
  259. local content = response.readAll()
  260. for i = 1, #content, 8192 do
  261. file.write(content:sub(i, i + 8191))
  262. os.queueEvent("yield")
  263. os.pullEvent("yield")
  264. end
  265. file.close()
  266. response.close()
  267. print("Downloaded as " .. filename)
  268. end
  269.  
  270. print("CC-BMP : An Enhanced BMP Image Viewer For ComputerCraft Coded By nonogamer9!")
  271. print("Enter a local filename or URL:")
  272. local input = read()
  273.  
  274. local filename = input
  275. if string.sub(input, 1, 4) == "http" then
  276. filename = "downloaded.bmp"
  277. downloadBMP(input, filename)
  278. end
  279.  
  280. local image, err = readBMP(filename)
  281. if not image then
  282. print("Error reading BMP file:", err)
  283. return
  284. end
  285.  
  286. print("Select display mode:")
  287. print("1. Basic ComputerCraft screen")
  288. print("2. Enhanced Mode (pseudo-resolution and colors)")
  289. print("3. CraftOS-PC 16-color Graphics Mode")
  290. print("4. CraftOS-PC 256-color Graphics Mode")
  291. print("5. Monitor")
  292.  
  293. local mode = tonumber(read())
  294.  
  295. local output
  296. if mode == 5 then
  297. output = peripheral.find("monitor")
  298. if not output then
  299. print("No monitor found")
  300. return
  301. end
  302. else
  303. output = term
  304. end
  305.  
  306. if mode == 1 then
  307. displayBMP(image, output)
  308. elseif mode == 2 then
  309. displayBMPMode2(image, output)
  310. elseif mode == 3 then
  311. if term.setGraphicsMode then
  312. displayBMPCraftOSPC(image, output, 1) -- 16-color mode
  313. else
  314. print("CraftOS-PC Graphics Mode not supported")
  315. end
  316. elseif mode == 4 then
  317. if term.setGraphicsMode then
  318. displayBMPCraftOSPC(image, output, 2) -- 256-color mode
  319. else
  320. print("CraftOS-PC Graphics Mode not supported")
  321. end
  322. elseif mode == 5 then
  323. displayBMP(image, output)
  324. else
  325. print("Invalid mode selected")
  326. end
  327.  
  328. print("Image displayed")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement