Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- ComputerCraft Screen Resolution: 51 x 19
- -- Out of this, we should be only using 51 x 18, leaving one row at the top or bottom
- -- This leaves us with 918 pixels in total to store.
- -- Since a computercraft screen is 624x354 large...
- -- We can assume that each ComputerCraft "pixel" is actually 12.235294117647 x 18.631578947368 large.
- -- Or not... Each screen character/pixel is actually 6x9.
- -- What this means that we have 51*6 x 19*9 screens... in other words, 306 x 171.
- -- Therefore, when we want to display pictures, we have to first fit it to the same aspect ratio as 306 x 171, then squeeze it down to 51 x 19.
- os.loadAPI("corefunc")
- function GetCoords(number,max_y) -- the maximum of Y (number of rows) is irrelevant to us. We only need to know how many columns are there.
- local x = math.ceil(number/max_y)
- local y = number % max_y
- if y == 0 then
- y = max_y
- end
- return x,y
- end
- function TransCord(x,y,max_y) -- the maximum of X (number of rows) is once again, irrelevant to us. We only need to know how many columns are there.
- return ((x*max_y)+y)
- end
- -- These two functions are going to be important to us later on.
- function PrintBwImage(bits,max_y) -- Black and white, 1-bit image
- term.clear()
- for key,value in ipairs(bits) do
- local pos_y,pos_x = GetCoords(key,max_y)
- term.setCursorPos(pos_x,pos_y)
- if value == true then
- term.setBackgroundColor(colors.white)
- else
- term.setBackgroundColor(colors.black)
- end
- term.write(" ")
- end
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- end
- function PrintCdImage(hexes,max_y) -- Coloured image. Unfortunately, ComputerCraft uses 16-bit integers for the 16 colours. Very wasteful, but fortunately I found a way to get around it.
- term.clear()
- for key,value in ipairs(hexes) do
- local pos_y,pos_x = GetCoords(key,max_y)
- term.setCursorPos(pos_x,pos_y)
- term.setBackgroundColor(math.pow(2,value))
- term.write(" ")
- end
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- end
- -- Here come "interlaced" images, which have backgrounds and foregrounds!
- local grey = '\127'
- function PrintGrImage(bits,max_y) -- Greyscale dithered image, with black, white, light grey and dakr grey.
- term.clear()
- local length = math.ceil(#bits/2)
- for i=1,length do
- local pos_y,pos_x = GetCoords(i,max_y)
- term.setCursorPos(pos_x,pos_y)
- if bits[(i*2)-1] == true then
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- else
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- end
- if bits[i*2] == true then
- term.write(grey)
- else
- term.write(" ")
- end
- end
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- end
- function PrintCsImage(hexes,max_y) -- Colour-dithered image, for lack of better term. Allows us to get more complex "colour combinations" by having... nevermind.
- term.clear()
- local length = math.ceil(#hexes/2)
- for i=1,length do
- local pos_y,pos_x = GetCoords(i,max_y)
- term.setCursorPos(pos_x,pos_y)
- term.setBackgroundColor(math.pow(2,hexes[(i*2)-1]))
- term.setTextColor(math.pow(2,hexes[i*2]))
- term.write(grey)
- end
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- end
- function OpenImage(path) -- This will not display it yet, just load its bytes into the memory and buffer it. Keep that in mind.
- local h = fs.open(path, "rb")
- if h == nil then
- h.close()
- return nil
- end
- local buf = { }
- buf.bytes = { }
- local c = h.read() -- We find out what mode the thing is.
- if c == nil or c < 128 then
- return nil
- else buf.mode = (c - 128)
- end
- buf.width = h.read()
- buf.height = h.read()
- local i = 1
- while true do
- c = h.read()
- if c == nil then
- h.close()
- break
- else
- buf.bytes[i] = c
- end
- end
- return buf
- end
- function DisplayImage(buf)
- if buf == nil then
- return nil
- end
- local pixel_buffer = { }
- local coloured = (buf.mode % 2 == 1)
- local dithered = (buf.mode >= 2)
- local pixels = { }
- if coloured == true then
- for key,value in ipairs(buf.bytes) do
- pixels[(key*2)-1],pixels[key*2] = splitHex(value)
- end
- else
- byte2bit(buf.bytes,pixels)
- end
- if dithered == true then
- if coloured == true then
- PrintCsImage(pixels,buf.width)
- return true
- else
- PrintGrImage(pixels,buf.width)
- return true
- end
- else
- if coloured == true then
- PrintCdImage(pixels,buf.width)
- return true
- else
- PrintBwImage(pixels,buf.width)
- return true
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement