What does ImageData:getPixel return exactly? (new questions)

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
Buddy4point0
Prole
Posts: 35
Joined: Wed Jan 12, 2011 9:29 pm
Location: Virginia

What does ImageData:getPixel return exactly? (new questions)

Post by Buddy4point0 »

I'm making a pixel perfect collision system to use in all my future games.
I like to make engines and resources for myself to use when I start using new interpreters and languages.

So what I need to know is, what exactly does ImageData:getPixel return?
I need to know so I can check the alpha values of pixels in the image to see if something would collide.

I read on the wiki that it returns "r, g, b, a" but is this information in a table, as a string, what?
Thanks in advance, and sorry for all the questions.
Last edited by Buddy4point0 on Sun Jan 16, 2011 7:34 am, edited 1 time in total.
╚»ßuddy4point0 ■
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: What does ImageData:getPixel return exactly?

Post by kikito »

Buddy4point0 wrote:I like to make engines and resources for myself to use when I start using new interpreters and languages.
I'll just leave this here. I totally agree with what it says. I have experienced it.
When I write def I mean function.
User avatar
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

Re: What does ImageData:getPixel return exactly?

Post by nevon »

If it says r,g,b,a then my guess would be 4 integers.

Easiest way to find out is to just give it a try.
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: What does ImageData:getPixel return exactly?

Post by Taehl »

I wouldn't do it that way if I were you: I'd keep a 2-dimensional sparse table which stored each 'pixel', then render it all with points or a spritebatch (depending on how big you're talking). I'm sure checking tables would be faster.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: What does ImageData:getPixel return exactly?

Post by Robin »

What the guys above me have said, and:
Buddy4point0 wrote:I read on the wiki that it returns "r, g, b, a" but is this information in a table, as a string, what?
Thanks in advance, and sorry for all the questions.
It is a list, which isn't a datatype or anything, but it means you can do:

Code: Select all

r, g, b, a = thingy:getPixel(x, y)
and

Code: Select all

print(thingy:getPixel(x, y)) -- prints the r, g, b, a values
Help us help you: attach a .love.
User avatar
thelinx
The Strongest
Posts: 857
Joined: Fri Sep 26, 2008 3:56 pm
Location: Sweden

Re: What does ImageData:getPixel return exactly?

Post by thelinx »

Or if you really want to store the values in an array:

Code: Select all

colortable = {thingy:getPixel(x,y)}
User avatar
Buddy4point0
Prole
Posts: 35
Joined: Wed Jan 12, 2011 9:29 pm
Location: Virginia

Re: What does ImageData:getPixel return exactly?

Post by Buddy4point0 »

kikito wrote:
Buddy4point0 wrote:I like to make engines and resources for myself to use when I start using new interpreters and languages.
I'll just leave this here. I totally agree with what it says. I have experienced it.
I read it, and I don't agree with you or the creator of the article at all.
To be more specific, I have to make a pixel perfect collision system for what I'm working on right now. So I figure I might as well make it re-useable.
Taehl wrote:I wouldn't do it that way if I were you: I'd keep a 2-dimensional sparse table which stored each 'pixel', then render it all with points or a spritebatch (depending on how big you're talking). I'm sure checking tables would be faster.
Could you emphasize what you mean? I don't really understand what you're talking about.
I'm not sure what a spritebatch is, but this is what I was planning on doing:
Make a function that does this:
  • Load the image.
  • Convert it to data (so I an use the pixel functions).
  • Create a table to store all the pixels in with a name according to the image name.
  • Read all the pixels and save the data in the table.
  • nil the image data.
Hopefully that would leave me with the image and a map that I could then use another function to read where it would collide.
Robin wrote:It is a list, which isn't a datatype or anything, but it means you can do:

Code: Select all

r, g, b, a = thingy:getPixel(x, y)
and

Code: Select all

print(thingy:getPixel(x, y)) -- prints the r, g, b, a values
Ok, I looked on the Lua 5.1 Reference Manualand I don't see anything about lists.
What are they and how would I use the information in it? If you don't feel like answering though, it's fine because I got it figured out thanks to Thelinx.
thelinx wrote:Or if you really want to store the values in an array:

Code: Select all

colortable = {thingy:getPixel(x,y)}
And this would return it as if I had typed
colortable = {r=255, g=255, b=255}?

That's perfect if that's what it does! Thanks sooo much!
Last edited by Buddy4point0 on Sun Jan 16, 2011 6:31 am, edited 1 time in total.
╚»ßuddy4point0 ■
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: What does ImageData:getPixel return exactly?

Post by TechnoCat »

Buddy4point0 wrote:
thelinx wrote:Or if you really want to store the values in an array:

Code: Select all

colortable = {thingy:getPixel(x,y)}
And this would return it as if I had typed
colortable = {r=255, g=255, b=255}?

That's perfect if that's what it does! Thanks sooo much!
That would result in colortable = {255,255,255,255} if the pixel was completely opaque and white.
Notice the table isn't keyed.
User avatar
Buddy4point0
Prole
Posts: 35
Joined: Wed Jan 12, 2011 9:29 pm
Location: Virginia

Re: What does ImageData:getPixel return exactly?

Post by Buddy4point0 »

So I've just written the function:

Code: Select all

function readImg(varName,fileName)
	local imageData = love.image.newImageData(fileName)

	local varName = {img = love.graphics.newImage(imageData),map = {}}

	local width = varName.img:getWidth()
	local height = varName.img:getHeight()
	local temp = {}

	for w = 1,width do
		varName.map[w] = {}

		for h = 1,height do
			temp = {imageData:getPixel(w,h)}

			if temp[4] > 0 then
				varName.map[w][h] = true
			else
				varName.map[w][h] = false
			end
		end
	end

	return varName
end
So when I call the function:

Code: Select all

readImg(player,"player.png")
I was hoping get:

Code: Select all

player = {}
player.img = (image)
player.map = {}
player.map[1][1] = true
player.map[1][2] = true
player.map[1][3] = true
ect...
But Love freezes and I get a runtime error. Any ideas why?

I uploaded what I have so far if that helps you guys help me.
╚»ßuddy4point0 ■
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: What does ImageData:getPixel return exactly?

Post by Boolsheet »

Your function is trying to get a pixel outside of the image. This throws an exception and love quits.
ImageData in love goes from 0, 0 to width-1, height-1. To stay inside the image you could change

Code: Select all

imageData:getPixel(w,h)
to

Code: Select all

imageData:getPixel(w-1,h-1)
Some other things:
Creating tables in Lua has a certain time and memory cost. Creating thousands of them in a loop might hurt the performance considerably.
About lists; they're mentioned here: http://www.lua.org/manual/5.1/manual.html#2.5

Your function overwrites the first argument (varName) of the function, it will never be used. You can drop that argument and just keep fileName alone, it will work fine.
It is possible to pass a table to a function, if that is what you meant to do:

Code: Select all

function change_size(obj)
    obj.size = 500
end

local map = {size = 0}
change_size(map)
print(map.size) -- will print 500
Shallow indentations.
Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests