What does ImageData:getPixel return exactly? (new questions)
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- Buddy4point0
- Prole
- Posts: 35
- Joined: Wed Jan 12, 2011 9:29 pm
- Location: Virginia
What does ImageData:getPixel return exactly? (new questions)
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.
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 ■
- 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?
I'll just leave this here. I totally agree with what it says. I have experienced it.Buddy4point0 wrote:I like to make engines and resources for myself to use when I start using new interpreters and languages.
When I write def I mean function.
- 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?
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.
Easiest way to find out is to just give it a try.
- 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?
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+.
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: What does ImageData:getPixel return exactly?
What the guys above me have said, and:
and
It is a list, which isn't a datatype or anything, but it means you can do: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.
Code: Select all
r, g, b, a = thingy:getPixel(x, y)
Code: Select all
print(thingy:getPixel(x, y)) -- prints the r, g, b, a values
Help us help you: attach a .love.
Re: What does ImageData:getPixel return exactly?
Or if you really want to store the values in an array:
Code: Select all
colortable = {thingy:getPixel(x,y)}
- Buddy4point0
- Prole
- Posts: 35
- Joined: Wed Jan 12, 2011 9:29 pm
- Location: Virginia
Re: What does ImageData:getPixel return exactly?
I read it, and I don't agree with you or the creator of the article at all.kikito wrote:I'll just leave this here. I totally agree with what it says. I have experienced it.Buddy4point0 wrote:I like to make engines and resources for myself to use when I start using new interpreters and languages.
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.
Could you emphasize what you mean? I don't really understand what you're talking about.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.
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.
Ok, I looked on the Lua 5.1 Reference Manualand I don't see anything about lists.Robin wrote:It is a list, which isn't a datatype or anything, but it means you can do:andCode: Select all
r, g, b, a = thingy:getPixel(x, y)
Code: Select all
print(thingy:getPixel(x, y)) -- prints the r, g, b, a values
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.
And this would return it as if I had typedthelinx wrote:Or if you really want to store the values in an array:Code: Select all
colortable = {thingy:getPixel(x,y)}
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 ■
- TechnoCat
- Inner party member
- Posts: 1611
- Joined: Thu Jul 30, 2009 12:31 am
- Location: Milwaukee, WI
- Contact:
Re: What does ImageData:getPixel return exactly?
That would result in colortable = {255,255,255,255} if the pixel was completely opaque and white.Buddy4point0 wrote:And this would return it as if I had typedthelinx wrote:Or if you really want to store the values in an array:Code: Select all
colortable = {thingy:getPixel(x,y)}
colortable = {r=255, g=255, b=255}?
That's perfect if that's what it does! Thanks sooo much!
Notice the table isn't keyed.
- Buddy4point0
- Prole
- Posts: 35
- Joined: Wed Jan 12, 2011 9:29 pm
- Location: Virginia
Re: What does ImageData:getPixel return exactly?
So I've just written the function:
So when I call the function:
I was hoping get:
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.
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
Code: Select all
readImg(player,"player.png")
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...
I uploaded what I have so far if that helps you guys help me.
╚»ßuddy4point0 ■
Re: What does ImageData:getPixel return exactly?
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
to
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:
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)
Code: Select all
imageData:getPixel(w-1,h-1)
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.
Who is online
Users browsing this forum: Bing [Bot] and 9 guests