ImageData:mapPixel

Transform an image by applying a function to every pixel.

This function is a higher-order function. It takes another function as a parameter, and calls it once for each pixel in the ImageData.

The passed function is called with six parameters for each pixel in turn. The parameters are numbers that represent the x and y coordinates of the pixel and its red, green, blue and alpha values. The function should return the new red, green, blue, and alpha values for that pixel.

function pixelFunction(x, y, r, g, b, a)
    -- template for defining your own pixel mapping function
    -- perform computations giving the new values for r, g, b and a
    -- ...
    return r, g, b, a
end

In versions prior to 11.0, color component values were within the range of 0 to 255 instead of 0 to 1.

Function

Synopsis

ImageData:mapPixel( pixelFunction, x, y, width, height )

Arguments

function pixelFunction
Function to apply to every pixel.
Available since LÖVE 0.9.0
number x (0)
The x-axis of the top-left corner of the area within the ImageData to apply the function to.
number y (0)
The y-axis of the top-left corner of the area within the ImageData to apply the function to.
number width (ImageData:getWidth())
The width of the area within the ImageData to apply the function to.
number height (ImageData:getHeight())
The height of the area within the ImageData to apply the function to.

Returns

Nothing.

Examples

Brighten an image:

function brighten( x, y, r, g, b, a )
   r = math.min(r * 3, 1)
   g = math.min(g * 3, 1)
   b = math.min(b * 3, 1)
   return r,g,b,a
end

imageData:mapPixel( brighten )

Add colored stripes to an image:

function stripey( x, y, r, g, b, a )
   r = math.min(r * math.sin(x*100)*2, 1)
   g = math.min(g * math.cos(x*150)*2, 1)
   b = math.min(b * math.sin(x*50)*2, 1)
   return r,g,b,a
end

imageData:mapPixel( stripey )

source: http://khason.net/blog/hlsl-pixel-shader-effects-tutorial/ (broken 11/16. See blogs.microsoft.co.il or archive.org mirrors.)

Tint grayscaled image:

function tint ( x, y, r, g, b, a )
   local c = globalTintColor -- color as {r=r, g=g, b=b}
   local t = (r+g+b)/3 -- grayscale value in range [0, 1]
   if t < 0.5 then
      r = 2*t*c.r
      g = 2*t*c.g
      b = 2*t*c.b
   else
      r = 2*(t + c.r - t*c.r) - 1
      g = 2*(t + c.g - t*c.g) - 1
      b = 2*(t + c.b - t*c.b) - 1
   end
   return r,g,b,a
end


imageData:mapPixel( tint )

See Also


Other Languages