satdump/src-core/common/lua/lua_utils.cpp

177 lines
6.8 KiB
C++
Raw Normal View History

2022-12-28 00:41:17 +01:00
#include "lua_utils.h"
2023-01-09 15:13:25 +01:00
#include "common/image/image.h"
2024-05-09 16:43:43 +02:00
#include "common/image/io.h"
#include "common/image/processing.h"
#include "common/image/image_lut.h"
2023-01-09 15:13:25 +01:00
#include "common/projection/projs/equirectangular.h"
#include "common/projection/sat_proj/sat_proj.h"
2023-12-08 18:37:13 +01:00
#include "logger.h"
2023-01-09 15:13:25 +01:00
2022-12-28 00:41:17 +01:00
namespace lua_utils
{
sol::table mapJsonToLua(sol::state &lua, nlohmann::json json)
{
sol::table l = lua.create_table();
char *ptr;
long index;
2022-12-28 00:41:17 +01:00
for (auto &el : json.items())
{
try
{
index = strtol(el.key().c_str(), &ptr, 10);
2023-12-08 18:37:13 +01:00
if (*ptr == '\0')
2022-12-31 00:25:17 +01:00
{
if (el.value().is_number_integer())
l[index] = el.value().get<int>();
else if (el.value().is_number())
l[index] = el.value().get<double>();
else if (el.value().is_string())
l[index] = el.value().get<std::string>();
else
l[index] = mapJsonToLua(lua, el.value());
}
else
2022-12-31 00:25:17 +01:00
{
if (el.value().is_number_integer())
l[el.key()] = el.value().get<int>();
else if (el.value().is_number())
l[el.key()] = el.value().get<double>();
else if (el.value().is_string())
l[el.key()] = el.value().get<std::string>();
else
l[el.key()] = mapJsonToLua(lua, el.value());
}
2022-12-28 00:41:17 +01:00
}
2023-12-08 18:37:13 +01:00
catch (std::exception &)
2022-12-28 00:41:17 +01:00
{
}
}
return l;
}
2023-01-09 15:13:25 +01:00
void bindImageType(sol::state &lua, std::string name)
{
2024-06-01 22:41:55 -04:00
//TODOXP
/*
2024-05-09 01:16:08 +02:00
sol::usertype<image::Image> image_type = lua.new_usertype<image::Image>(
2023-01-09 15:13:25 +01:00
name,
2024-06-01 22:41:55 -04:00
sol::constructors<image::Image(), image::Image(int, size_t, size_t, int)>(),
"clear", &image::Image::clear,
"get", [](image::Image& img, size_t i) -> int
{
if (i < img.size())
return img.get(i);
else
return 0;
},
"set", [](image::Image& img, size_t i, int x)
{
if (i < img.size())
img.set(i, img.clamp(x));
},
"depth", &image::Image::depth,
"width", &image::Image::width,
"height", &image::Image::height,
"channels", &image::Image::channels,
"size", &image::Image::size,
"to_rgb", &image::Image::to_rgb,
"to_rgba", &image::Image::to_rgba,
"fill_color", &image::Image::fill_color,
"fill", &image::Image::fill,
"mirror", &image::Image::mirror,
"resize", &image::Image::resize,
"resize_to", &image::Image::resize_to,
"resize_bilinear", &image::Image::resize_bilinear,
"draw_pixel", &image::Image::draw_pixel,
"draw_line", &image::Image::draw_line,
"draw_circle", &image::Image::draw_circle,
"draw_image", &image::Image::draw_image);
*/
2023-01-09 15:13:25 +01:00
2024-05-09 16:43:43 +02:00
lua["image_equalize"] = &image::equalize;
lua["image_white_balance"] = &image::white_balance;
lua["image_linear_invert"] = &image::linear_invert;
lua["image_equalize"] = &image::equalize;
lua["image_simple_despeckle"] = &image::simple_despeckle;
lua["image_median_blur"] = &image::median_blur;
lua["image_despeckle"] = &image::kuwahara_filter;
2023-01-09 15:13:25 +01:00
2024-05-09 16:43:43 +02:00
lua["image_load_png"] = (void (*)(image::Image &, std::string, bool))(&image::load_png);
lua["image_save_png"] = &image::save_png;
lua["image_load_jpeg"] = (void (*)(image::Image &, std::string))(&image::load_jpeg);
lua["image_save_jpeg"] = &image::save_jpeg;
lua["image_load_img"] = (void (*)(image::Image &, std::string))(&image::load_img);
lua["image_save_img"] = &image::save_img;
2023-01-09 15:13:25 +01:00
}
void bindImageTypes(sol::state &lua)
{
2024-05-09 16:43:43 +02:00
bindImageType(lua, "image_t");
2024-05-09 16:43:43 +02:00
lua["image8_lut_jet"] = &image::LUT_jet<uint8_t>;
lua["image16_lut_jet"] = &image::LUT_jet<uint16_t>;
2023-01-09 15:13:25 +01:00
}
void bindGeoTypes(sol::state &lua)
{
2024-06-01 22:41:55 -04:00
//TODOXP
/*
2023-01-09 15:13:25 +01:00
sol::usertype<geodetic::geodetic_coords_t> type = lua.new_usertype<geodetic::geodetic_coords_t>("geodetic_coords_t",
2024-06-01 22:41:55 -04:00
sol::constructors<geodetic::geodetic_coords_t(), geodetic::geodetic_coords_t(double, double, double), geodetic::geodetic_coords_t(double, double, double, bool)>(),
"toDegs", &geodetic::geodetic_coords_t::toDegs,
"toRads", &geodetic::geodetic_coords_t::toRads,
"lat", &geodetic::geodetic_coords_t::lat,
"lon", &geodetic::geodetic_coords_t::lon,
"alt", &geodetic::geodetic_coords_t::alt);
*/
2023-01-09 15:13:25 +01:00
}
void bindSatProjType(sol::state &lua)
{
2024-06-01 22:41:55 -04:00
//TODOXP
/*
sol::usertype<satdump::SatelliteProjection> type = lua.new_usertype<satdump::SatelliteProjection>("satproj_t",
"img_size_x", &satdump::SatelliteProjection::img_size_x,
"img_size_y", &satdump::SatelliteProjection::img_size_y,
"gcp_spacing_x", &satdump::SatelliteProjection::gcp_spacing_x,
"gcp_spacing_y", &satdump::SatelliteProjection::gcp_spacing_y,
"get_position", &satdump::SatelliteProjection::get_position);*/
2023-01-09 15:13:25 +01:00
}
void bindEquProjType(sol::state &lua)
{
2024-06-01 22:41:55 -04:00
//TODOXP
/*
sol::usertype<geodetic::projection::EquirectangularProjection> type = lua.new_usertype<geodetic::projection::EquirectangularProjection>("EquirectangularProj",
"init", &geodetic::projection::EquirectangularProjection::init,
"forward", [](geodetic::projection::EquirectangularProjection& equ, double lon, double lat) -> std::pair<int, int>
{
int x2, y2;
equ.forward(lon, lat, x2, y2);
return { x2, y2 };
},
"reverse", [](geodetic::projection::EquirectangularProjection& equ, int x, int y) -> std::pair<float, float>
{
float lon, lat;
equ.reverse(x, y, lon, lat);
return { lon, lat };
});*/
2023-01-09 15:13:25 +01:00
}
2023-12-08 18:37:13 +01:00
void bindLogger(sol::state &lua)
{
lua["ltrace"] = [](std::string log)
{ logger->trace(log); };
lua["ldebug"] = [](std::string log)
{ logger->debug(log); };
lua["linfo"] = [](std::string log)
{ logger->info(log); };
lua["lwarn"] = [](std::string log)
{ logger->warn(log); };
lua["lerror"] = [](std::string log)
{ logger->error(log); };
lua["lcritical"] = [](std::string log)
{ logger->critical(log); };
}
2022-12-28 00:41:17 +01:00
};