Page 1 of 3
Problem with MapPixel...
Posted: Wed Mar 24, 2010 2:34 am
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.
Re: Problem with MapPixel...
Posted: Wed Mar 24, 2010 3:02 am
by bmelts
Both those examples work fine for me. What sort of errors are you getting?
from left to right: unmodified, mapPixel called with brighten, mapPixel called with stripey
Re: Problem with MapPixel...
Posted: Wed Mar 24, 2010 3:34 am
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.
Re: Problem with MapPixel...
Posted: Wed Mar 24, 2010 3:48 am
by bmelts
Here is the code in my main.lua, and attached is the working .love file.
Re: Problem with MapPixel...
Posted: Wed Mar 24, 2010 3:58 am
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.
Re: Problem with MapPixel...
Posted: Wed Mar 24, 2010 4:36 am
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 ).
Re: Problem with MapPixel...
Posted: Wed Mar 24, 2010 4:44 am
by Jasoco
Well, isn't MapPixel only used at load? Can it even be used during the game?
Re: Problem with MapPixel...
Posted: Wed Mar 24, 2010 4:45 am
by ljdp
If you put it in update yeah.
Re: Problem with MapPixel...
Posted: Wed Mar 24, 2010 4:51 am
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...
Re: Problem with MapPixel...
Posted: Wed Mar 24, 2010 4:57 am
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