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.
Problem with MapPixel...
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: Problem with MapPixel...
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
from left to right: unmodified, mapPixel called with brighten, mapPixel called with stripey
- Jasoco
- Inner party member
- Posts: 3727
- Joined: Mon Jun 22, 2009 9:35 am
- Location: Pennsylvania, USA
- Contact:
Re: Problem with MapPixel...
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...
Here is the code in my main.lua, and attached is the working .love file.
- Attachments
-
- mapPixel.love
- (10.97 KiB) Downloaded 171 times
- Jasoco
- Inner party member
- Posts: 3727
- Joined: Mon Jun 22, 2009 9:35 am
- Location: Pennsylvania, USA
- Contact:
Re: Problem with MapPixel...
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.
Thanks.
Re: Problem with MapPixel...
mapPixel() can be slow. Even when using math.min...
Looking at anjo's example I did some profiling with a larger image:
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 ).
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
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.
- Jasoco
- Inner party member
- Posts: 3727
- Joined: Mon Jun 22, 2009 9:35 am
- Location: Pennsylvania, USA
- Contact:
Re: Problem with MapPixel...
Well, isn't MapPixel only used at load? Can it even be used during the game?
Re: Problem with MapPixel...
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?(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...
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
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...
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
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
Who is online
Users browsing this forum: Amazon [Bot], Bing [Bot], Google [Bot] and 9 guests