aboutsummaryrefslogtreecommitdiff
path: root/games/devtest/mods
diff options
context:
space:
mode:
authorhecks <42101236+hecktest@users.noreply.github.com>2021-07-29 05:10:10 +0200
committerGitHub <noreply@github.com>2021-07-29 05:10:10 +0200
commit80d12dbedb67191a5eb3e4f3c36b04baed1f8afb (patch)
treeca8346be45852e0c08f444f8a8eb6c0cdfd37e39 /games/devtest/mods
parent2866918f3293c486609ff46ad0bfa5ce833aaaf2 (diff)
downloadminetest-80d12dbedb67191a5eb3e4f3c36b04baed1f8afb.tar.gz
minetest-80d12dbedb67191a5eb3e4f3c36b04baed1f8afb.tar.bz2
minetest-80d12dbedb67191a5eb3e4f3c36b04baed1f8afb.zip
Add a simple PNG image encoder with Lua API (#11485)
* Add a simple PNG image encoder with Lua API Add ColorSpec to RGBA converter Make a safety wrapper for the encoder Create devtest examples Co-authored-by: hecktest <> Co-authored-by: sfan5 <sfan5@live.de>
Diffstat (limited to 'games/devtest/mods')
-rw-r--r--games/devtest/mods/testnodes/textures.lua75
1 files changed, 75 insertions, 0 deletions
diff --git a/games/devtest/mods/testnodes/textures.lua b/games/devtest/mods/testnodes/textures.lua
index f6e6a0c2a..4652007d9 100644
--- a/games/devtest/mods/testnodes/textures.lua
+++ b/games/devtest/mods/testnodes/textures.lua
@@ -65,3 +65,78 @@ for a=1,#alphas do
})
end
+-- Generate PNG textures
+
+local function mandelbrot(w, h, iterations)
+ local r = {}
+ for y=0, h-1 do
+ for x=0, w-1 do
+ local re = (x - w/2) * 4/w
+ local im = (y - h/2) * 4/h
+ -- zoom in on a nice view
+ re = re / 128 - 0.23
+ im = im / 128 - 0.82
+
+ local px, py = 0, 0
+ local i = 0
+ while px*px + py*py <= 4 and i < iterations do
+ px, py = px*px - py*py + re, 2 * px * py + im
+ i = i + 1
+ end
+ r[w*y+x+1] = i / iterations
+ end
+ end
+ return r
+end
+
+local function gen_checkers(w, h, tile)
+ local r = {}
+ for y=0, h-1 do
+ for x=0, w-1 do
+ local hori = math.floor(x / tile) % 2 == 0
+ local vert = math.floor(y / tile) % 2 == 0
+ r[w*y+x+1] = hori ~= vert and 1 or 0
+ end
+ end
+ return r
+end
+
+local fractal = mandelbrot(512, 512, 128)
+local checker = gen_checkers(512, 512, 32)
+
+local floor = math.floor
+local abs = math.abs
+local data_mb = {}
+local data_ck = {}
+for i=1, #fractal do
+ data_mb[i] = {
+ r = floor(fractal[i] * 255),
+ g = floor(abs(fractal[i] * 2 - 1) * 255),
+ b = floor(abs(1 - fractal[i]) * 255),
+ a = 255,
+ }
+ data_ck[i] = checker[i] > 0 and "#F80" or "#000"
+end
+
+local textures_path = minetest.get_modpath( minetest.get_current_modname() ) .. "/textures/"
+minetest.safe_file_write(
+ textures_path .. "testnodes_generated_mb.png",
+ minetest.encode_png(512,512,data_mb)
+)
+minetest.safe_file_write(
+ textures_path .. "testnodes_generated_ck.png",
+ minetest.encode_png(512,512,data_ck)
+)
+
+minetest.register_node("testnodes:generated_png_mb", {
+ description = S("Generated Mandelbrot PNG Test Node"),
+ tiles = { "testnodes_generated_mb.png" },
+
+ groups = { dig_immediate = 2 },
+})
+minetest.register_node("testnodes:generated_png_ck", {
+ description = S("Generated Checker PNG Test Node"),
+ tiles = { "testnodes_generated_ck.png" },
+
+ groups = { dig_immediate = 2 },
+})