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

Re: What does ImageData:getPixel return exactly?

Post by Buddy4point0 »

Boolsheet wrote: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)
Thank you very much! I had no idea that imagedata was read like that.

Here's the final working version my function in case it helps anyone else.

Code: Select all

function readImg(fileName)
	local iD = love.image.newImageData(fileName)
	local t = {img = love.graphics.newImage(fileName),map = {}}
	local width = t.img:getWidth()
	local height = t.img:getHeight()
	local temp

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

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

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

	return t
end
Last edited by Buddy4point0 on Sun Jan 16, 2011 10:35 am, edited 1 time in total.
╚»ßuddy4point0 ■
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 »

Buddy4point0 wrote: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.
But that's exactly what the writer of the article was saying: you don't create a collision system out of the blue, but you make it with a specific use (i.e. your game) in mind. Making it reusable is the second step, not the first.
Buddy4point0 wrote: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.
Lists are very weak in Lua, they disappear in a puff of smoke, for example:

Code: Select all

function afunc(...)
    print(...) -- ... is a list here. You can pass it to another function.
    print((...)) -- only the first element in the list is passed
    -- lists can be put into tables like this:
    atable = {...} 
    return 1, 2, 3 -- this function returns a list
end
Some explanation: http://www.lua.org/pil/5.1.html and http://www.lua.org/pil/5.2.html
Help us help you: attach a .love.
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 »

Robin wrote:
Buddy4point0 wrote: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.
Lists are very weak in Lua, they disappear in a puff of smoke, for example:

Code: Select all

function afunc(...)
    print(...) -- ... is a list here. You can pass it to another function.
    print((...)) -- only the first element in the list is passed
    -- lists can be put into tables like this:
    atable = {...} 
    return 1, 2, 3 -- this function returns a list
end
Some explanation: http://www.lua.org/pil/5.1.html and http://www.lua.org/pil/5.2.html
I'll read up on this and get back to you.
╚»ß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? (new questi

Post by Boolsheet »

Code: Select all

for w = 1, width, -1 do
Are you sure this is what you want?
It will never enter the loop unless width is 1 or lower.
Shallow indentations.
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? (new questi

Post by Robin »

Boolsheet wrote:

Code: Select all

for w = 1, width, -1 do
Are you sure this is what you want?
I think it was

Code: Select all

for w = 1, width do
before.

Buddy4point0: I think you should change it to this:

Code: Select all

for w = 0, width-1 do
Help us help you: attach a .love.
User avatar
Buddy4point0
Prole
Posts: 35
Joined: Wed Jan 12, 2011 9:29 pm
Location: Virginia

Re: What does ImageData:getPixel return exactly? (new questi

Post by Buddy4point0 »

Robin wrote:
Boolsheet wrote:

Code: Select all

for w = 1, width, -1 do
Are you sure this is what you want?
I think it was

Code: Select all

for w = 1, width do
before.

Buddy4point0: I think you should change it to this:

Code: Select all

for w = 0, width-1 do
So when reading images, say a 64x64 image, I would have to read pixels 0-63 across and 0-63 down?
╚»ßuddy4point0 ■
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? (new questi

Post by Robin »

Buddy4point0 wrote:So when reading images, say a 64x64 image, I would have to read pixels 0-63 across and 0-63 down?
Yeah, I think so.
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 59 guests