Difference between revisions of "ImageData:mapPixel"

(Add missing function description, explanation based on reading source)
 
(Two examples for the function.)
Line 21: Line 21:
 
=== Returns ===
 
=== Returns ===
 
Nothing.
 
Nothing.
 +
== Examples ==
 +
=== Brighten an image: ===
 +
<source lang="lua">
 +
function brighten( x, y, r, g, b, a )
 +
  r = r * 3
 +
  g = g * 3
 +
  b = b * 3
 +
  return r,g,b,a
 +
end
 +
 +
imageData:mapPixel( brighten )
 +
</source>
 +
=== Add colored stripes to an image: ===
 +
<source lang="lua">
 +
function stripey( x, y, r, g, b, a )
 +
  r = r * math.sin(x*100)*2
 +
  g = g * math.cos(x*150)*2
 +
  b = b * math.sin(x*50)*2
 +
  return r,g,b,a
 +
end
 +
 +
imageData:mapPixel( stripey )
 +
</source>
 +
source: http://khason.net/blog/hlsl-pixel-shader-effects-tutorial/
 
== See Also ==
 
== See Also ==
 
* [[parent::ImageData]]
 
* [[parent::ImageData]]
 
[[Category:Functions]]
 
[[Category:Functions]]
 
{{#set:Description=Transform an image by applying a function to every pixel.}}
 
{{#set:Description=Transform an image by applying a function to every pixel.}}

Revision as of 03:12, 27 February 2010

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 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.

Function

Synopsis

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

ImageData:mapPixel( pixelFunction )

Arguments

function pixelFunction
Function parameter to apply to every pixel.

Returns

Nothing.

Examples

Brighten an image:

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

imageData:mapPixel( brighten )

Add colored stripes to an image:

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

imageData:mapPixel( stripey )

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

See Also