@@ -40,6 +40,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
40
40
#include " version.h"
41
41
#include " util/hex.h"
42
42
#include " util/sha1.h"
43
+ #include " util/png.h"
43
44
#include < algorithm>
44
45
#include < cstdio>
45
46
@@ -497,6 +498,43 @@ int ModApiUtil::l_colorspec_to_colorstring(lua_State *L)
497
498
return 0 ;
498
499
}
499
500
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
+
500
538
void ModApiUtil::Initialize (lua_State *L, int top)
501
539
{
502
540
API_FCT (log );
@@ -532,6 +570,9 @@ void ModApiUtil::Initialize(lua_State *L, int top)
532
570
API_FCT (get_version);
533
571
API_FCT (sha1);
534
572
API_FCT (colorspec_to_colorstring);
573
+ API_FCT (colorspec_to_bytes);
574
+
575
+ API_FCT (encode_png);
535
576
536
577
LuaSettings::create (L, g_settings, g_settings_path);
537
578
lua_setfield (L, top, " settings" );
@@ -557,6 +598,7 @@ void ModApiUtil::InitializeClient(lua_State *L, int top)
557
598
API_FCT (get_version);
558
599
API_FCT (sha1);
559
600
API_FCT (colorspec_to_colorstring);
601
+ API_FCT (colorspec_to_bytes);
560
602
}
561
603
562
604
void ModApiUtil::InitializeAsync (lua_State *L, int top)
@@ -585,6 +627,7 @@ void ModApiUtil::InitializeAsync(lua_State *L, int top)
585
627
API_FCT (get_version);
586
628
API_FCT (sha1);
587
629
API_FCT (colorspec_to_colorstring);
630
+ API_FCT (colorspec_to_bytes);
588
631
589
632
LuaSettings::create (L, g_settings, g_settings_path);
590
633
lua_setfield (L, top, " settings" );