This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
HPWebcamAble's profile picture

[Solved] Load a paint image from a string

Started by HPWebcamAble, 10 July 2015 - 05:39 PM
HPWebcamAble #1
Posted 10 July 2015 - 07:39 PM
How do you LOAD a paint utils image from a string?



--# If this was a standalone file, it would work, but I only want to use a single file.
local image = [[	   fffffff
	   f44444f
	   f44444f
	   f44444f
	   fffffff
fffffffffffffffffffff
f44444fffffffff44444f
f44444fffffffff44444f
f44444fffffffff44444f
8888888fffffff8888888
	   ffffffffffffff
	   f44444ff44444f
	   f44444ff44444f
	   f44444ff44444f
	   fffffff8888888]]

paintutils.loadImage( ? )

'paintutils.loadImage' only accepts a file path, not a string.

Would the easiest way be temporarily saving the string to file to let paintutils read it?
( Note that I don't want to save the table that is created, I prefer to saving the string. For reasons )
Edited on 10 July 2015 - 06:14 PM
TYKUHN2 #2
Posted 10 July 2015 - 07:46 PM
Is there a particular reason you wish to load from a string? I only ask to determine what solutions may be worth your time.
HPWebcamAble #3
Posted 10 July 2015 - 07:55 PM
Is there a particular reason you wish to load from a string? I only ask to determine what solutions may be worth your time.

Well I want to display it in a program, but without having more than 1 file.


While I was waiting for a reply, I just had my program save the string to a temporary file, load the image, then delete the file.
It works, but I'm still wondering if there is a better way.
Lyqyd #4
Posted 10 July 2015 - 07:56 PM
Look at the paintutils API and rework the loading function to create a new version for your program that loads from a string.
TYKUHN2 #5
Posted 10 July 2015 - 08:01 PM
You could convert the string into the numerical forms and put it into a table with the paintutils format.

As far as I know paintutils just makes a table and thats all. So for that last line of your string the value would be

{0, 0, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 256, 256, 256, 256, 256, 256, 256}

Again not sure if it is worth the time using this particular solution.
HPWebcamAble #6
Posted 10 July 2015 - 08:13 PM
Look at the paintutils API and rework the loading function to create a new version for your program that loads from a string.

Don't know why I didn't think of that :P/>
It should be really easy.


You could convert the string into the numerical forms and put it into a table with the paintutils format.

As far as I know paintutils just makes a table and thats all. So for that last line of your string the value would be

{0, 0, 32768, 32768, 32768, 32768, 32768, 32768, 32768, 256, 256, 256, 256, 256, 256, 256}

Again not sure if it is worth the time using this particular solution.

Well, I didn't want to use the table because when it is first created (using textutils.serialize), it's about 50 lines, so if I ever edited the original, I'd need to manually reformat the outputed table.
Also, the string version is much small without having to do anything to it, plus you can edit it pretty easily.
Edited on 10 July 2015 - 06:13 PM
Bomb Bloke #7
Posted 11 July 2015 - 12:50 AM
Truth be told, paintutils.drawImage() is a bit slower than it has to be. I somewhat recently wrote a somewhat more efficient renderer, here's a version that'll do what you want:

Spoiler
local image = [[           fffffff
           f44444f
           f44444f
           f44444f
           fffffff
fffffffffffffffffffff
f44444fffffffff44444f
f44444fffffffff44444f
f44444fffffffff44444f
8888888fffffff8888888
           ffffffffffffff
           f44444ff44444f
           f44444ff44444f
           f44444ff44444f
           fffffff8888888]]

-- ComputerCraft builds prior to 1.74 lack term.blit(). This function implements a substitute:
local function checkTerm(terminal)
	if not terminal.blit then terminal.blit = function(_, _, backCol)
		local counter, lastChar = 1, backCol:sub(1, 1)

		for i = 2, #backCol do if backCol:sub(i, i) ~= lastChar then
			terminal.setBackgroundColour(bit.blshift(1, colourNum[lastChar]))
			terminal.write(string.rep(" ", counter))
			counter, lastChar = 1, backCol:sub(i, i)
		else counter = counter + 1 end end

		terminal.setBackgroundColour(bit.blshift(1, colourNum[lastChar]))
		terminal.write(string.rep(" ", counter))
	end end
end

local function convertImage(image)
	local results, position = {}, 1
	
	while position <= #image do
		results[#results + 1] = {}
		local skip, chars = 0, {}
		
		while image:sub(position, position) ~= "\n" and position <= #image do
			local thisChar = image:sub(position, position)
			
			if thisChar == " " then
				if #chars > 0 then
					results[#results][#results[#results] + 1] = table.concat(chars)
					chars = {}
				end
				
				skip = skip + 1
			else
				if skip > 0 then
					results[#results][#results[#results] + 1] = skip
					skip = 0
				end
				
				chars[#chars + 1] = thisChar
			end
			
			position = position + 1
		end
		
		if skip > 0 then
			results[#results][#results[#results] + 1] = skip
		else
			results[#results][#results[#results] + 1] = table.concat(chars)
		end
		
		position = position + 1
	end

	return results
end

local function drawImage(image, x, y)
	for i = 1, #image do
		local curX = x
		
		for j = 1, #image[i] do
			local thisVal = image[i][j]
			if type(thisVal) == "number" then
				curX = curX + thisVal
			else
				term.setCursorPos(curX, y + i - 1)
				term.blit(thisVal, thisVal, thisVal)
				curX = curX + #thisVal
			end
		end
	end
end

term.setBackgroundColour(colours.lightBlue)
term.clear()
checkTerm(term)

image = convertImage(image)
drawImage(image, 1, 1)
Edited on 10 July 2015 - 10:53 PM
HPWebcamAble #8
Posted 11 July 2015 - 01:44 AM
Truth be told, paintutils.drawImage() is a bit slower than it has to be. I somewhat recently wrote a somewhat more efficient renderer, here's a version that'll do what you want:

I'm not rendering this image more than once when it appears, and it isn't the end of the world if it flickers when drawing. I think paintutils will work for now

Although, I hadn't thought of term.blit, still not used to having it. Which I technically don't, as I use the DW20 1.7.10 pack, which still hasn't updated CC yet.
It would be really easy to draw the image with term.blit though. I actually add text over the image after paintutils draws it, and term.blit could take care of that when drawing the image.
Edited on 10 July 2015 - 11:45 PM