Image:mapPixel
EditTransforms pixels in the Image using a function.
The callback function passed to this function will be called once for each pixel. For each pixel, the function will be called with its x and y coordinate and the red, green, blue, and alpha components of its color. Whatever the function returns will be used as the new color for the pixel.
The callback function will potentially be called thousands of times, so it's best to keep the amount of code in there small and fast.
Image:mapPixel(callback, x, y, w, h)
Arguments
Name | Type | Default | Description |
callback | function | The function that will be called for each pixel. | |
x | number | 0 | The x coordinate of the upper-left corner of the area of the Image to affect. |
y | number | 0 | The y coordinate of the upper-left corner of the area of the Image to affect. |
w | number | image:getWidth() | The width of the area to affect. |
h | number | image:getHeight() | The height of the area to affect. |
Returns
Nothing
Notes
The following texture formats are supported: r8
, rg8
, rgba8
, r16
, rg16
, rgba16
, r32f
, rg32f
, rgba32f
.
Examples
Convert an Image to grayscale.
image:mapPixel(function(x, y, r, g, b, a)
local brightness = .21 * r + .72 * g + .07 * b
return brightness, brightness, brightness, a
end)
Efficient Image updates using FFI. Due to the low-level nature, this will be a lot faster, but it's specialized to the rgba8
image format and risks crashing if used improperly.
local ffi = require 'ffi'
function lovr.load()
local w, h = 256, 256
image = lovr.data.newImage(w, h)
local pointer = ffi.cast('uint8_t*', image:getPointer())
for y = 0, h - 1 do
for x = 0, w - 1 do
pointer[(y * w + x) * 4 + 0] = (x / w) * 255
pointer[(y * w + x) * 4 + 1] = (y / h) * 255
pointer[(y * w + x) * 4 + 2] = 255
pointer[(y * w + x) * 4 + 3] = 255
end
end
texture = lovr.graphics.newTexture(image)
end
function lovr.draw(pass)
pass:fill(texture)
end