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

How to overlay one paintutils image over another

Started by LDDestroier, 11 April 2016 - 01:06 PM
LDDestroier #1
Posted 11 April 2016 - 03:06 PM
I'm trying to write a function to overlay one picture (using the paintutils.loadImage format, not PAIN) over another, to reduce flickering. Here's what I got so far:
SpoilerOverlays img1 over img2

function mixImages(img1,img2)
local output = img2
for a = 1, #img1 do
  if img1[a] then
   for b = 1, #img1[a] do
    if img1[a] then
	 if img1[a][b] then
	  if not output[a] then output[a] = {} end
	  output[a][b] = img1[a][b]
	 end
    end
   end
  end
end
return output
end

But, it seems to erase all pixels in output that are left of the corresponding pixels in img1. I'm doing this for a game. Help?
Bomb Bloke #2
Posted 11 April 2016 - 03:40 PM
Here's the source of paintutils.drawImage() - note that #tImage and #tLine don't evaluate as "the highest index in those tables", but rather as "the highest index before the first nil value is encountered".

More to the point, also note that "leave this pixel blank" isn't always represented by a nil value, but also by the number 0.
LDDestroier #3
Posted 11 April 2016 - 04:08 PM
Never mind, I found out how.

function mixImages(img1,img2)
    local output = img1
    for a = 1, #img2 do
	    if img2[a] then
		    for b = 1, #img2[a] do
			    if not output[a] then output[a] = {} end
			    if not img1[a] then
				    output[a][b] = img2[a][b]
			    elseif (not img1[a][b]) or (img1[a][b] == 0) then
				    output[a][b] = img2[a][b]
			    end
		    end
	    end
    end
    return output
end