I know How to convert a PNG image to grayscale and without losing the transparency on C#, how to do it on Lua or cpp?
Asked
Active
Viewed 305 times
-2
-
In the unlikely event that the library you're using doesn't have a built-in filter for this, the formula is trivial. Please show the code you're having trouble with. – stark Aug 06 '20 at 01:43
1 Answers
0
PNG is a compressed format so you can't load that without some sort of library. BMP is significantly easier to load into memory, for example: https://bitbucket.org/itraykov/utils/src/master/io/bmp.lua
Now as for your original question how to convert color to black and white, the easiest way is to use the RGB average:
local r, g, b = getColor()
local bw = (r + g + b)/3
setColor(bw, bw, bw)
Of course you need to change getColor/setColor depending on the library that you are using. Additional checks are necessary for images with transparency or an alpha channel.

2dengine
- 89
- 1
- 3
-
Most filters don't do equal weights. https://stackoverflow.com/q/17615963/1216776 – stark Aug 06 '20 at 17:24
-