Problem with MapPixel...

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
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Problem with MapPixel...

Post by Jasoco »

I can't seem to get ImageData:MapPixel to work. Can anyone offer sample code of how it is supposed to work?

My game crashes when I try to use the examples from the Wiki.
User avatar
bmelts
Party member
Posts: 380
Joined: Fri Jan 30, 2009 3:16 am
Location: Wiscönsin
Contact:

Re: Problem with MapPixel...

Post by bmelts »

Both those examples work fine for me. What sort of errors are you getting?
Image
from left to right: unmodified, mapPixel called with brighten, mapPixel called with stripey
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Problem with MapPixel...

Post by Jasoco »

Can you give me the exact code that you used for that? I get NO ERROR as usual. Just a crash without warning. Not even in the console. I just need the exact code so I can see what I am doing wrong.
User avatar
bmelts
Party member
Posts: 380
Joined: Fri Jan 30, 2009 3:16 am
Location: Wiscönsin
Contact:

Re: Problem with MapPixel...

Post by bmelts »

Here is the code in my main.lua, and attached is the working .love file.
Attachments
mapPixel.love
(10.97 KiB) Downloaded 171 times
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Problem with MapPixel...

Post by Jasoco »

I see what I did wrong. I was trying to apply it to an Image instead of Image Data then making the Image from that Data.

Thanks.
User avatar
ljdp
Party member
Posts: 209
Joined: Sat Jan 03, 2009 1:04 pm
Contact:

Re: Problem with MapPixel...

Post by ljdp »

mapPixel() can be slow. Even when using math.min... :?
Looking at anjo's example I did some profiling with a larger image:

Code: Select all

-- Took 0.725 seconds. (512x512 png)
function stripey( x, y, r, g, b, a )
   r = math.min(r * math.sin(x*100)*2, 255)
   g = math.min(g * math.cos(x*150)*2, 255)
   b = math.min(b * math.sin(x*50)*2, 255)
   return r,g,b,a
end

-- 0.920% decrease
function stripey2( x, y, r, g, b, a )
	local min = math.min
	local sin = math.sin
	local cos = math.cos
   r = min(r * sin(x*100)*2, 255)
   g = min(g * cos(x*150)*2, 255)
   b = min(b * sin(x*50)*2, 255)
   return r,g,b,a
end

-- 0.900% decrease
function stripey3( x, y, r, g, b, a )
	local min = math.min
	local sin = math.sin
	local cos = math.cos
   local r = min(r * sin(x*100)*2, 255)
   local g = min(g * cos(x*150)*2, 255)
   local b = min(b * sin(x*50)*2, 255)
   return r,g,b,a
end

-- 0.799% decrease
local min = math.min
local sin = math.sin
local cos = math.cos
function stripey4( x, y, r, g, b, a )
   local r = min(r * sin(x*100)*2, 255)
   local g = min(g * cos(x*150)*2, 255)
   local b = min(b * sin(x*50)*2, 255)
   return r,g,b,a
end

-- 0.578% decrease
local sin = math.sin
local cos = math.cos
function stripey5( x, y, r, g, b, a )
   local r = r * sin(x*100)*2
	if r>255 then r=255 end
   local g = g * cos(x*150)*2
	if g>255 then g=255 end
   local b = b * sin(x*50)*2
	if b>255 then b=255 end
   return r,g,b,a
end

-- 0.167 seconds
function nothing( x, y, r, g, b, a )
	return r,g,b,a
end
Last stripey is over 40% faster!
But even iterating over each pixel is too slow for any real-time pixel mapping, on a macbook pro 2.4Ghz i'd get about 6fps with mapPixel( nothing ).
Last edited by ljdp on Wed Mar 24, 2010 4:45 am, edited 1 time in total.
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Problem with MapPixel...

Post by Jasoco »

Well, isn't MapPixel only used at load? Can it even be used during the game?
User avatar
ljdp
Party member
Posts: 209
Joined: Sat Jan 03, 2009 1:04 pm
Contact:

Re: Problem with MapPixel...

Post by ljdp »

If you put it in update yeah.
User avatar
bmelts
Party member
Posts: 380
Joined: Fri Jan 30, 2009 3:16 am
Location: Wiscönsin
Contact:

Re: Problem with MapPixel...

Post by bmelts »

oh, heh, I typoed and put sin instead of cos in part of stripey. Shame on me.

That said - yeah, math.min is a slow function. How would this profile in comparison to stripey5?

Code: Select all

local sin = math.sin
local cos = math.cos
function stripey6( x, y, r, g, b, a )
   local r = r * sin(x*100)*2
   r = r > 255 and r or 255
   local g = g * cos(x*150)*2
   g = g > 255 and g or 255
   local b = b * sin(x*50)*2
   b = b > 255 and b or 255
   return r,g,b,a
end
(I don't know if it's actually any faster, but it's cleaner imo.)

EDIT: Missed Jasoco's latest post. You can call any LÖVE function any time, it's just a matter of whether it's effective to do so (example: drawing functions called outside of love.draw are not, because they're not going to actually be shown). So, you could hypothetically map ImageData pixels every frame in, say, love.update. There are some neat special effects you could do. The one issue, of course, being that it's not the fastest function in the world...
User avatar
ljdp
Party member
Posts: 209
Joined: Sat Jan 03, 2009 1:04 pm
Contact:

Re: Problem with MapPixel...

Post by ljdp »

0.573% descrease.
But I get a different looking image...
Instead seeing black bands I see white bands.

edit:
lol it should be
r <= 255 not r > 255

Code: Select all

local sin = math.sin
local cos = math.cos
function stripey6( x, y, r, g, b, a )
   local r = r * sin(x*100)*2
   r = r <= 255 and r or 255
   local g = g * cos(x*150)*2
   g = g <= 255 and g or 255
   local b = b * sin(x*50)*2
   b = b <= 255 and b or 255
   return r,g,b,a
end
Post Reply

Who is online

Users browsing this forum: Amazon [Bot] and 7 guests