mirror of
https://git.minetest.land/VoxeLibre/VoxeLibre.git
synced 2024-11-29 22:11:10 +01:00
Use HSP for RGB-to-BW8 conversion
This commit is contained in:
parent
5b79bc6fb7
commit
2112637faf
1 changed files with 8 additions and 4 deletions
12
init.lua
12
init.lua
|
@ -107,11 +107,15 @@ function image:encode_data_r8g8b8_to_bw8_raw()
|
||||||
local raw_pixels = {}
|
local raw_pixels = {}
|
||||||
for _, row in ipairs(self.pixels) do
|
for _, row in ipairs(self.pixels) do
|
||||||
for _, pixel in ipairs(row) do
|
for _, pixel in ipairs(row) do
|
||||||
-- see <https://alienryderflex.com/saturation.html>
|
-- the HSP RGB to brightness formula is
|
||||||
|
-- sqrt( 0.299 r² + .587 g² + .114 b² )
|
||||||
|
-- see <https://alienryderflex.com/hsp.html>
|
||||||
local gray = math.floor(
|
local gray = math.floor(
|
||||||
0.299 * pixel[1] +
|
math.sqrt(
|
||||||
0.587 * pixel[2] +
|
0.299 * pixel[1]^2 +
|
||||||
0.114 * pixel[3]
|
0.587 * pixel[2]^2 +
|
||||||
|
0.114 * pixel[3]^2
|
||||||
|
) + 0.5
|
||||||
)
|
)
|
||||||
local raw_pixel = string.char(gray)
|
local raw_pixel = string.char(gray)
|
||||||
raw_pixels[#raw_pixels + 1] = raw_pixel
|
raw_pixels[#raw_pixels + 1] = raw_pixel
|
||||||
|
|
Loading…
Reference in a new issue