Page 1 of 1

Using getPixel and setPixel

Posted: Sun Sep 26, 2010 11:37 pm
by Maholain
I was looking at the getPixel API and saw it's synopsis as this:

Code: Select all

r, g, b, a = ImageData:getPixel( x, y )
I was wondering, how would I compare what it returns in an if statement? I'm not sure what to do when it returns more than one thing.

Also, when I used setPixel in the update method, I got a 'attempt to access index 'ImageData'. Can someone tell me what is going wrong here?

Re: Using getPixel and setPixel

Posted: Mon Sep 27, 2010 5:57 am
by Robin

Code: Select all

local r, g, b, a = ImageData:getPixel(x, y)
-- compare r, g, b, a
-- OR
local t = {ImageData:getPixel(x, y)} -- d'oh!
-- compare t[1], t[2], t[3], t[4]

Re: Using getPixel and setPixel

Posted: Mon Sep 27, 2010 6:01 am
by bartbes
That second one is wrong

Code: Select all

local t = {ImageData:getPixel(x, y)}

Re: Using getPixel and setPixel

Posted: Mon Sep 27, 2010 4:20 pm
by Maholain
I'm trying to set the pixel below the cursor to say green if the pixel below my cursor is black.

Code: Select all

--In load
coverData = love.image.newImageData("overlay.png");
--in Update
if love.mouse.isDown("l") then
		local r, g, b, a = coverData:getPixel(love.mouse.getX(), love.mouse.getY());
		if (r,g,b,a == 0, 0, 0, 255) then
			coverData:setPixel(love.mouse.getX(), love.mouse.getY(), 40, 40, 40, 255);
		end
	end
It doesn't work and I'm wondering if anyone can provide me with a working sample.

Re: Using getPixel and setPixel

Posted: Mon Sep 27, 2010 4:31 pm
by thelinx
what am I thinking

Re: Using getPixel and setPixel

Posted: Mon Sep 27, 2010 4:51 pm
by vrld
thelinx wrote:dont look please
This won't work, because t is a different table than {0,0,0,255} even if t = {0,0,0,255}.
This should work:

Code: Select all

local r, g, b = coverData:getPixel(love.mouse.getX(), love.mouse.getY());
if r + g + b == 0 then
    coverData:setPixel(love.mouse.getX(), love.mouse.getY(), 40, 40, 40)
end
But expect it to be very, very slow...

Re: Using getPixel and setPixel

Posted: Mon Sep 27, 2010 4:52 pm
by Maholain
Thanks. I still can't get my initial idea to work though. I think it's because

Code: Select all

coverData:setPixel(love.mouse.getX(), love.mouse.getY(), 40, 40, 40, 255);
is only setting the pixel on coverData. I can't draw a ImageData though, so how can I show this coverData on the screen?

Re: Using getPixel and setPixel

Posted: Mon Sep 27, 2010 4:58 pm
by thelinx
Convert it into an image, draw that image.

Re: Using getPixel and setPixel

Posted: Mon Sep 27, 2010 5:07 pm
by Maholain
thelinx wrote:Convert it into an image, draw that image.
Neat! Thanks, problem solved. The code I used if anyone is interested:

Code: Select all

--This code will reveal what's behind it because of setPixel being set to invisible
function love.update(dt) 
	if love.mouse.isDown("l") then
		local r, g, b = coverData:getPixel(love.mouse.getX(), love.mouse.getY());
			if r + g + b == 0 then
				coverData:setPixel(love.mouse.getX(), love.mouse.getY(), 0, 0, 0, 0)
		end
	end
	imagesData = love.graphics.newImage(coverData);
end
function love.draw() 
	love.graphics.draw(imagesData, 0, 0);
end

Re: Using getPixel and setPixel

Posted: Mon Sep 27, 2010 6:38 pm
by leiradel
Maholain wrote:

Code: Select all

if (r,g,b,a == 0, 0, 0, 255) then
You can't compare multiple values like that AFAIK. Use

Code: Select all

if r == 0 and g == 0 and b == 0 and a == 255 then[/quote]

Summing r, g and b and comparing against zero is more efficient I think, but it only works for black (sum == 0) and white (sum == 765).

Cheers,

Andre