Difference between revisions of "ImageData:mapPixel"

(Two examples for the function.)
m (Added New feature template and default values)
(10 intermediate revisions by 6 users not shown)
Line 1: Line 1:
 
Transform an image by applying a function to every pixel.
 
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.
+
This function is a [https://en.wikipedia.org/wiki/Higher-order_function 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>
  
ImageData:mapPixel( pixelFunction )
+
In versions prior to [[11.0]], color component values were within the range of 0 to 255 instead of 0 to 1.
 +
== Function ==
 +
=== Synopsis ===
 +
<source lang="lua">
 +
ImageData:mapPixel( pixelFunction, x, y, width, height )
 
</source>
 
</source>
 
=== Arguments ===
 
=== Arguments ===
{{param|function|pixelFunction|Function parameter to apply to every pixel.}}
+
{{param|function|pixelFunction|Function to apply to every pixel.}}
 +
{{New feature|0.9.0|
 +
{{param|number|x (0)|The x-axis of the top-left corner of the area within the ImageData to apply the function to.}}
 +
{{param|number|y (0)|The y-axis of the top-left corner of the area within the ImageData to apply the function to.}}
 +
{{param|number|width (ImageData:getWidth())|The width of the area within the ImageData to apply the function to.}}
 +
{{param|number|height (ImageData:getHeight())|The height of the area within the ImageData to apply the function to.}}
 +
}}
 
=== Returns ===
 
=== Returns ===
 
Nothing.
 
Nothing.
 +
 
== Examples ==
 
== Examples ==
 
=== Brighten an image: ===
 
=== Brighten an image: ===
 
<source lang="lua">
 
<source lang="lua">
 
function brighten( x, y, r, g, b, a )
 
function brighten( x, y, r, g, b, a )
   r = r * 3
+
   r = math.min(r * 3, 1)
   g = g * 3
+
   g = math.min(g * 3, 1)
   b = b * 3
+
   b = math.min(b * 3, 1)
 
   return r,g,b,a
 
   return r,g,b,a
 
end
 
end
Line 36: Line 46:
 
<source lang="lua">
 
<source lang="lua">
 
function stripey( x, y, r, g, b, a )
 
function stripey( x, y, r, g, b, a )
   r = r * math.sin(x*100)*2
+
   r = math.min(r * math.sin(x*100)*2, 1)
   g = g * math.cos(x*150)*2
+
   g = math.min(g * math.cos(x*150)*2, 1)
   b = b * math.sin(x*50)*2
+
   b = math.min(b * math.sin(x*50)*2, 1)
 
   return r,g,b,a
 
   return r,g,b,a
 
end
 
end
Line 44: Line 54:
 
imageData:mapPixel( stripey )
 
imageData:mapPixel( stripey )
 
</source>
 
</source>
source: http://khason.net/blog/hlsl-pixel-shader-effects-tutorial/
+
source: http://khason.net/blog/hlsl-pixel-shader-effects-tutorial/ (broken 11/16. See [http://blogs.microsoft.co.il/tamir/2008/06/17/hlsl-pixel-shader-effects-tutorial/ blogs.microsoft.co.il] or [http://web.archive.org/web/20150515111551/http://khason.net/blog/hlsl-pixel-shader-effects-tutorial/ archive.org] mirrors.)
 +
 
 
== 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.}}
 +
{{#set:Since=000}}
 +
== Other Languages ==
 +
{{i18n|ImageData:mapPixel}}

Revision as of 08:50, 14 August 2019

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

See Also


Other Languages