mapPixel.lua 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. return {
  2. summary = 'Transform an Image by applying a function to every pixel.',
  3. description = [[
  4. Transforms pixels in the Image using a function.
  5. The callback function passed to this function will be called once for each pixel. For each
  6. pixel, the function will be called with its x and y coordinate and the red, green, blue, and
  7. alpha components of its color. Whatever the function returns will be used as the new color for
  8. the pixel.
  9. The callback function will potentially be called thousands of times, so it's best to keep the
  10. amount of code in there small and fast.
  11. ]],
  12. arguments = {
  13. callback = {
  14. type = 'function',
  15. description = 'The function that will be called for each pixel.'
  16. },
  17. x = {
  18. type = 'number',
  19. default = '0',
  20. description = 'The x coordinate of the upper-left corner of the area of the Image to affect.'
  21. },
  22. y = {
  23. type = 'number',
  24. default = '0',
  25. description = 'The y coordinate of the upper-left corner of the area of the Image to affect.'
  26. },
  27. w = {
  28. type = 'number',
  29. default = 'image:getWidth()',
  30. description = 'The width of the area to affect.'
  31. },
  32. h = {
  33. type = 'number',
  34. default = 'image:getHeight()',
  35. description = 'The height of the area to affect.'
  36. }
  37. },
  38. returns = {},
  39. variants = {
  40. {
  41. arguments = { 'callback', 'x', 'y', 'w', 'h' },
  42. returns = {}
  43. }
  44. },
  45. notes = [[
  46. The following texture formats are supported: `r8`, `rg8`, `rgba8`, `r16`, `rg16`, `rgba16`,
  47. `r32f`, `rg32f`, `rgba32f`.
  48. ]],
  49. examples = {
  50. {
  51. description = 'Convert an Image to grayscale.',
  52. code = [[
  53. image:mapPixel(function(x, y, r, g, b, a)
  54. local brightness = .21 * r + .72 * g + .07 * b
  55. return brightness, brightness, brightness, a
  56. end)
  57. ]]
  58. },
  59. {
  60. description = [[
  61. Efficient Image updates using FFI. Due to the low-level nature, this will be a lot faster,
  62. but it's specialized to the `rgba8` image format and risks crashing if used improperly.
  63. ]],
  64. code = [[
  65. local ffi = require 'ffi'
  66. function lovr.load()
  67. local w, h = 256, 256
  68. image = lovr.data.newImage(w, h)
  69. local pointer = ffi.cast('uint8_t*', image:getPointer())
  70. for y = 0, h - 1 do
  71. for x = 0, w - 1 do
  72. pointer[(y * w + x) * 4 + 0] = (x / w) * 255
  73. pointer[(y * w + x) * 4 + 1] = (y / h) * 255
  74. pointer[(y * w + x) * 4 + 2] = 255
  75. pointer[(y * w + x) * 4 + 3] = 255
  76. end
  77. end
  78. texture = lovr.graphics.newTexture(image)
  79. end
  80. function lovr.draw(pass)
  81. pass:fill(texture)
  82. end
  83. ]]
  84. }
  85. },
  86. related = {
  87. 'Image:setPixel',
  88. 'Image:getPixel',
  89. 'TextureFormat',
  90. 'Texture:setPixels'
  91. }
  92. }