Skip to content

Commit 80d12db

Browse files
hecktestsfan5
andauthoredJul 29, 2021
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>
1 parent 2866918 commit 80d12db

File tree

9 files changed

+278
-1
lines changed

9 files changed

+278
-1
lines changed
 

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ src/test_config.h
8787
src/cmake_config.h
8888
src/cmake_config_githash.h
8989
src/unittest/test_world/world.mt
90+
games/devtest/mods/testnodes/textures/testnodes_generated_*.png
9091
/locale/
9192
.directory
9293
*.cbp

‎builtin/game/misc.lua

+39
Original file line numberDiff line numberDiff line change
@@ -290,3 +290,42 @@ function core.dynamic_add_media(filepath, callback)
290290
end
291291
return true
292292
end
293+
294+
295+
-- PNG encoder safety wrapper
296+
297+
local o_encode_png = core.encode_png
298+
function core.encode_png(width, height, data, compression)
299+
if type(width) ~= "number" then
300+
error("Incorrect type for 'width', expected number, got " .. type(width))
301+
end
302+
if type(height) ~= "number" then
303+
error("Incorrect type for 'height', expected number, got " .. type(height))
304+
end
305+
306+
local expected_byte_count = width * height * 4;
307+
308+
if type(data) ~= "table" and type(data) ~= "string" then
309+
error("Incorrect type for 'height', expected table or string, got " .. type(height));
Has a conversation. Original line has a conversation.
310+
end
311+
312+
local data_length = type(data) == "table" and #data * 4 or string.len(data);
313+
314+
if data_length ~= expected_byte_count then
315+
error(string.format(
316+
"Incorrect length of 'data', width and height imply %d bytes but %d were provided",
317+
expected_byte_count,
318+
data_length
319+
))
320+
end
321+
322+
if type(data) == "table" then
323+
local dataBuf = {}
324+
for i = 1, #data do
325+
dataBuf[i] = core.colorspec_to_bytes(data[i])
326+
end
327+
data = table.concat(dataBuf)
328+
end
329+
330+
return o_encode_png(width, height, data, compression or 6)
331+
end

‎doc/lua_api.txt

+18-1
Original file line numberDiff line numberDiff line change
@@ -4611,6 +4611,23 @@ Utilities
46114611
* `minetest.colorspec_to_colorstring(colorspec)`: Converts a ColorSpec to a
46124612
ColorString. If the ColorSpec is invalid, returns `nil`.
46134613
* `colorspec`: The ColorSpec to convert
4614+
* `minetest.colorspec_to_bytes(colorspec)`: Converts a ColorSpec to a raw
4615+
string of four bytes in an RGBA layout, returned as a string.
4616+
* `colorspec`: The ColorSpec to convert
4617+
* `minetest.encode_png(width, height, data, [compression])`: Encode a PNG
4618+
image and return it in string form.
4619+
* `width`: Width of the image
4620+
* `height`: Height of the image
4621+
* `data`: Image data, one of:
4622+
* array table of ColorSpec, length must be width*height
4623+
* string with raw RGBA pixels, length must be width*height*4
4624+
* `compression`: Optional zlib compression level, number in range 0 to 9.
4625+
The data is one-dimensional, starting in the upper left corner of the image
4626+
and laid out in scanlines going from left to right, then top to bottom.
4627+
Please note that it's not safe to use string.char to generate raw data,
4628+
use `colorspec_to_bytes` to generate raw RGBA values in a predictable way.
4629+
The resulting PNG image is always 32-bit. Palettes are not supported at the moment.
4630+
You may use this to procedurally generate textures during server init.
46144631

46154632
Logging
46164633
-------
@@ -7631,7 +7648,7 @@ Used by `minetest.register_node`.
76317648
leveled_max = 127,
76327649
-- Maximum value for `leveled` (0-127), enforced in
76337650
-- `minetest.set_node_level` and `minetest.add_node_level`.
7634-
-- Values above 124 might causes collision detection issues.
7651+
-- Values above 124 might causes collision detection issues.
76357652

76367653
liquid_range = 8,
76377654
-- Maximum distance that flowing liquid nodes can spread around

‎games/devtest/mods/testnodes/textures.lua

+75
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,78 @@ for a=1,#alphas do
6565
})
6666
end
6767

68+
-- Generate PNG textures
69+
70+
local function mandelbrot(w, h, iterations)
71+
local r = {}
72+
for y=0, h-1 do
73+
for x=0, w-1 do
74+
local re = (x - w/2) * 4/w
75+
local im = (y - h/2) * 4/h
76+
-- zoom in on a nice view
77+
re = re / 128 - 0.23
78+
im = im / 128 - 0.82
79+
80+
local px, py = 0, 0
81+
local i = 0
82+
while px*px + py*py <= 4 and i < iterations do
83+
px, py = px*px - py*py + re, 2 * px * py + im
84+
i = i + 1
85+
end
86+
r[w*y+x+1] = i / iterations
87+
end
88+
end
89+
return r
90+
end
91+
92+
local function gen_checkers(w, h, tile)
93+
local r = {}
94+
for y=0, h-1 do
95+
for x=0, w-1 do
96+
local hori = math.floor(x / tile) % 2 == 0
97+
local vert = math.floor(y / tile) % 2 == 0
98+
r[w*y+x+1] = hori ~= vert and 1 or 0
99+
end
100+
end
101+
return r
102+
end
103+
104+
local fractal = mandelbrot(512, 512, 128)
105+
local checker = gen_checkers(512, 512, 32)
106+
107+
local floor = math.floor
108+
local abs = math.abs
109+
local data_mb = {}
110+
local data_ck = {}
111+
for i=1, #fractal do
112+
data_mb[i] = {
113+
r = floor(fractal[i] * 255),
114+
g = floor(abs(fractal[i] * 2 - 1) * 255),
115+
b = floor(abs(1 - fractal[i]) * 255),
116+
a = 255,
117+
}
118+
data_ck[i] = checker[i] > 0 and "#F80" or "#000"
119+
end
120+
121+
local textures_path = minetest.get_modpath( minetest.get_current_modname() ) .. "/textures/"
122+
minetest.safe_file_write(
123+
textures_path .. "testnodes_generated_mb.png",
124+
minetest.encode_png(512,512,data_mb)
125+
)
126+
minetest.safe_file_write(
127+
textures_path .. "testnodes_generated_ck.png",
128+
minetest.encode_png(512,512,data_ck)
129+
)
130+
131+
minetest.register_node("testnodes:generated_png_mb", {
132+
description = S("Generated Mandelbrot PNG Test Node"),
133+
tiles = { "testnodes_generated_mb.png" },
134+
135+
groups = { dig_immediate = 2 },
136+
})
137+
minetest.register_node("testnodes:generated_png_ck", {
138+
description = S("Generated Checker PNG Test Node"),
139+
tiles = { "testnodes_generated_ck.png" },
140+
141+
groups = { dig_immediate = 2 },
142+
})

‎src/script/lua_api/l_util.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
4040
#include "version.h"
4141
#include "util/hex.h"
4242
#include "util/sha1.h"
43+
#include "util/png.h"
4344
#include <algorithm>
4445
#include <cstdio>
4546

@@ -497,6 +498,43 @@ int ModApiUtil::l_colorspec_to_colorstring(lua_State *L)
497498
return 0;
498499
}
499500

501+
// colorspec_to_bytes(colorspec)
502+
int ModApiUtil::l_colorspec_to_bytes(lua_State *L)
503+
{
504+
NO_MAP_LOCK_REQUIRED;
505+
506+
video::SColor color(0);
507+
if (read_color(L, 1, &color)) {
508+
u8 colorbytes[4] = {
509+
(u8) color.getRed(),
510+
(u8) color.getGreen(),
511+
(u8) color.getBlue(),
512+
(u8) color.getAlpha(),
513+
};
514+
lua_pushlstring(L, (const char*) colorbytes, 4);
515+
return 1;
516+
}
517+
518+
return 0;
519+
}
520+
521+
// encode_png(w, h, data, level)
522+
int ModApiUtil::l_encode_png(lua_State *L)
523+
{
524+
NO_MAP_LOCK_REQUIRED;
525+
526+
// The args are already pre-validated on the lua side.
527+
u32 width = readParam<int>(L, 1);
528+
u32 height = readParam<int>(L, 2);
529+
const char *data = luaL_checklstring(L, 3, NULL);
530+
s32 compression = readParam<int>(L, 4);
531+
532+
std::string out = encodePNG((const u8*)data, width, height, compression);
533+
534+
lua_pushlstring(L, out.data(), out.size());
535+
return 1;
536+
}
537+
500538
void ModApiUtil::Initialize(lua_State *L, int top)
501539
{
502540
API_FCT(log);
@@ -532,6 +570,9 @@ void ModApiUtil::Initialize(lua_State *L, int top)
532570
API_FCT(get_version);
533571
API_FCT(sha1);
534572
API_FCT(colorspec_to_colorstring);
573+
API_FCT(colorspec_to_bytes);
574+
575+
API_FCT(encode_png);
535576

536577
LuaSettings::create(L, g_settings, g_settings_path);
537578
lua_setfield(L, top, "settings");
@@ -557,6 +598,7 @@ void ModApiUtil::InitializeClient(lua_State *L, int top)
557598
API_FCT(get_version);
558599
API_FCT(sha1);
559600
API_FCT(colorspec_to_colorstring);
601+
API_FCT(colorspec_to_bytes);
560602
}
561603

562604
void ModApiUtil::InitializeAsync(lua_State *L, int top)
@@ -585,6 +627,7 @@ void ModApiUtil::InitializeAsync(lua_State *L, int top)
585627
API_FCT(get_version);
586628
API_FCT(sha1);
587629
API_FCT(colorspec_to_colorstring);
630+
API_FCT(colorspec_to_bytes);
588631

589632
LuaSettings::create(L, g_settings, g_settings_path);
590633
lua_setfield(L, top, "settings");

‎src/script/lua_api/l_util.h

+6
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ class ModApiUtil : public ModApiBase
104104
// colorspec_to_colorstring(colorspec)
105105
static int l_colorspec_to_colorstring(lua_State *L);
106106

107+
// colorspec_to_bytes(colorspec)
108+
static int l_colorspec_to_bytes(lua_State *L);
109+
110+
// encode_png(w, h, data, level)
111+
static int l_encode_png(lua_State *L);
112+
107113
public:
108114
static void Initialize(lua_State *L, int top);
109115
static void InitializeAsync(lua_State *L, int top);

‎src/util/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ set(UTIL_SRCS
1515
${CMAKE_CURRENT_SOURCE_DIR}/string.cpp
1616
${CMAKE_CURRENT_SOURCE_DIR}/srp.cpp
1717
${CMAKE_CURRENT_SOURCE_DIR}/timetaker.cpp
18+
${CMAKE_CURRENT_SOURCE_DIR}/png.cpp
1819
PARENT_SCOPE)

‎src/util/png.cpp

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
Minetest
3+
Copyright (C) 2021 hecks
4+
5+
This program is free software; you can redistribute it and/or modify
6+
it under the terms of the GNU Lesser General Public License as published by
7+
the Free Software Foundation; either version 2.1 of the License, or
8+
(at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public License along
16+
with this program; if not, write to the Free Software Foundation, Inc.,
17+
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*/
19+
20+
#include "png.h"
21+
#include <string>
22+
#include <sstream>
23+
#include <zlib.h>
24+
#include <cassert>
25+
#include "util/serialize.h"
26+
#include "serialization.h"
27+
#include "irrlichttypes.h"
28+
29+
static void writeChunk(std::ostringstream &target, const std::string &chunk_str)
30+
{
31+
assert(chunk_str.size() >= 4);
32+
assert(chunk_str.size() - 4 < U32_MAX);
33+
writeU32(target, chunk_str.size() - 4); // Write length minus the identifier
34+
target << chunk_str;
35+
writeU32(target, crc32(0,(const u8*)chunk_str.data(), chunk_str.size()));
36+
}
37+
38+
std::string encodePNG(const u8 *data, u32 width, u32 height, s32 compression)
39+
{
40+
auto file = std::ostringstream(std::ios::binary);
41+
file << "\x89PNG\r\n\x1a\n";
42+
43+
{
44+
auto IHDR = std::ostringstream(std::ios::binary);
45+
IHDR << "IHDR";
46+
writeU32(IHDR, width);
47+
writeU32(IHDR, height);
48+
// 8 bpp, color type 6 (RGBA)
49+
IHDR.write("\x08\x06\x00\x00\x00", 5);
50+
writeChunk(file, IHDR.str());
51+
}
52+
53+
{
54+
auto IDAT = std::ostringstream(std::ios::binary);
55+
IDAT << "IDAT";
56+
auto scanlines = std::ostringstream(std::ios::binary);
57+
for(u32 i = 0; i < height; i++) {
58+
scanlines.write("\x00", 1); // Null predictor
59+
scanlines.write((const char*) data + width * 4 * i, width * 4);
60+
}
61+
compressZlib(scanlines.str(), IDAT, compression);
62+
writeChunk(file, IDAT.str());
63+
}
64+
65+
file.write("\x00\x00\x00\x00IEND\xae\x42\x60\x82", 12);
66+
67+
return file.str();
68+
}

‎src/util/png.h

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Minetest
3+
Copyright (C) 2021 hecks
4+
5+
This program is free software; you can redistribute it and/or modify
6+
it under the terms of the GNU Lesser General Public License as published by
7+
the Free Software Foundation; either version 2.1 of the License, or
8+
(at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public License along
16+
with this program; if not, write to the Free Software Foundation, Inc.,
17+
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*/
19+
20+
#pragma once
21+
22+
#include <string>
23+
#include "irrlichttypes.h"
24+
25+
/* Simple PNG encoder. Encodes an RGBA image with no predictors.
26+
Returns a binary string. */
27+
std::string encodePNG(const u8 *data, u32 width, u32 height, s32 compression);

0 commit comments

Comments
 (0)
Please sign in to comment.