Difference between revisions of "ImageData:mapPixel"

Line 3: Line 3:
 
This function is a higher order function. It takes another function as a parameter, and calls it once for each pixel in the ImageData.
 
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 function parameter 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 parameter can return up to four number values, which become the new r, g, b and a values of the pixel. If the function returns fewer values, the remaining components are set to 0.
+
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 ==
 
=== Synopsis ===
 
 
<source lang="lua">
 
<source lang="lua">
 
function pixelFunction(x, y, r, g, b, a)
 
function pixelFunction(x, y, r, g, b, a)
Line 14: Line 12:
 
     return r, g, b, a
 
     return r, g, b, a
 
end
 
end
 +
</source>
  
 +
== Function ==
 +
=== Synopsis ===
 +
<source lang="lua">
 
ImageData:mapPixel( pixelFunction )
 
ImageData:mapPixel( pixelFunction )
 
</source>
 
</source>
 
=== Arguments ===
 
=== Arguments ===
{{param|function|pixelFunction|Function parameter to apply to every pixel.}}
+
{{param|function|pixelFunction|Function to apply to every pixel.}}
 
=== Returns ===
 
=== Returns ===
 
Nothing.
 
Nothing.
 +
 +
== Function ==
 +
{{newin|[[0.9.0]]|090|type=variant}}
 +
=== Synopsis ===
 +
<source lang="lua">
 +
ImageData:mapPixel( pixelFunction, x, y, width, height )
 +
</source>
 +
=== Arguments ===
 +
{{param|function|pixelFunction|Function to apply to every pixel.}}
 +
{{param|number|x|The x-axis of the top-left corner of the area within the ImageData to apply the function to.}}
 +
{{param|number|y|The y-axis of the top-left corner of the area within the ImageData to apply the function to.}}
 +
{{param|number|width|The width of the area within the ImageData to apply the function to.}}
 +
{{param|number|height|The height of the area within the ImageData to apply the function to.}}
 +
=== Returns ===
 +
Nothing.
 +
 
== Examples ==
 
== Examples ==
 
=== Brighten an image: ===
 
=== Brighten an image: ===

Revision as of 04:01, 15 June 2015

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

Function

Synopsis

ImageData:mapPixel( pixelFunction )

Arguments

function pixelFunction
Function to apply to every pixel.

Returns

Nothing.

Function

Available since LÖVE 0.9.0
This variant is not supported in earlier versions.

Synopsis

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

Arguments

function pixelFunction
Function to apply to every pixel.
number x
The x-axis of the top-left corner of the area within the ImageData to apply the function to.
number y
The y-axis of the top-left corner of the area within the ImageData to apply the function to.
number width
The width of the area within the ImageData to apply the function to.
number height
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, 255)
   g = math.min(g * 3, 255)
   b = math.min(b * 3, 255)
   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, 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

imageData:mapPixel( stripey )

source: http://khason.net/blog/hlsl-pixel-shader-effects-tutorial/

See Also


Other Languages