#include "lua_utils.h" #include "common/image/image.h" #include "common/projection/projs/equirectangular.h" #include "common/projection/sat_proj/sat_proj.h" namespace lua_utils { sol::table mapJsonToLua(sol::state &lua, nlohmann::json json) { sol::table l = lua.create_table(); for (auto &el : json.items()) { try { try { int index = std::stoi(el.key()); if (el.value().is_number_integer()) l[index] = el.value().get(); else if (el.value().is_number()) l[index] = el.value().get(); else if (el.value().is_string()) l[index] = el.value().get(); else l[index] = mapJsonToLua(lua, el.value()); } catch (std::exception) { if (el.value().is_number_integer()) l[el.key()] = el.value().get(); else if (el.value().is_number()) l[el.key()] = el.value().get(); else if (el.value().is_string()) l[el.key()] = el.value().get(); else l[el.key()] = mapJsonToLua(lua, el.value()); } } catch (std::exception) { } } return l; } template void bindImageType(sol::state &lua, std::string name) { sol::usertype> image_type = lua.new_usertype>( name, sol::constructors(), image::Image(size_t, size_t, int)>()); image_type["clear"] = &image::Image::clear; image_type["get"] = [](image::Image &img, int i) { return img[i]; }; image_type["set"] = [](image::Image &img, int i, T x) { img[i] = img.clamp(x); }; image_type["depth"] = &image::Image::depth; image_type["width"] = &image::Image::width; image_type["height"] = &image::Image::height; image_type["channels"] = &image::Image::channels; image_type["size"] = &image::Image::size; image_type["to_rgb"] = &image::Image::to_rgb; image_type["to_rgba"] = &image::Image::to_rgba; image_type["fill_color"] = &image::Image::fill_color; image_type["fill"] = &image::Image::fill; image_type["mirror"] = &image::Image::mirror; image_type["equalize"] = &image::Image::equalize; image_type["white_balance"] = &image::Image::white_balance; // CROP / CROP-TO image_type["resize"] = &image::Image::resize; image_type["resize_to"] = &image::Image::resize_to; image_type["resize_bilinear"] = &image::Image::resize_bilinear; image_type["brightness_contrast_old"] = &image::Image::brightness_contrast_old; image_type["linear_invert"] = &image::Image::linear_invert; image_type["simple_despeckle"] = &image::Image::simple_despeckle; image_type["median_blur"] = &image::Image::median_blur; image_type["draw_pixel"] = &image::Image::draw_pixel; image_type["draw_line"] = &image::Image::draw_line; image_type["draw_circle"] = &image::Image::draw_circle; image_type["draw_image"] = &image::Image::draw_image; // image_type["draw_text"] = &image::Image::draw_text; image_type["load_png"] = (void(image::Image::*)(std::string, bool))(&image::Image::load_png); image_type["save_png"] = &image::Image::save_png; image_type["load_jpeg"] = (void(image::Image::*)(std::string))(&image::Image::load_jpeg); image_type["save_jpeg"] = &image::Image::save_jpeg; image_type["load_img"] = (void(image::Image::*)(std::string))(&image::Image::load_img); image_type["save_img"] = &image::Image::save_img; } void bindImageTypes(sol::state &lua) { bindImageType(lua, "image8"); bindImageType(lua, "image16"); lua["image8_lut_jet"] = &image::LUT_jet; } void bindGeoTypes(sol::state &lua) { sol::usertype type = lua.new_usertype("geodetic_coords_t", sol::constructors< geodetic::geodetic_coords_t(), geodetic::geodetic_coords_t(double, double, double), geodetic::geodetic_coords_t(double, double, double, bool)>()); type["toDegs"] = &geodetic::geodetic_coords_t::toDegs; type["toRads"] = &geodetic::geodetic_coords_t::toRads; type["lat"] = &geodetic::geodetic_coords_t::lat; type["lon"] = &geodetic::geodetic_coords_t::lon; type["alt"] = &geodetic::geodetic_coords_t::alt; } void bindSatProjType(sol::state &lua) { sol::usertype type = lua.new_usertype("satproj_t"); type["img_size_x"] = &satdump::SatelliteProjection::img_size_x; type["img_size_y"] = &satdump::SatelliteProjection::img_size_y; type["gcp_spacing_x"] = &satdump::SatelliteProjection::gcp_spacing_x; type["gcp_spacing_y"] = &satdump::SatelliteProjection::gcp_spacing_y; type["get_position"] = &satdump::SatelliteProjection::get_position; } void bindEquProjType(sol::state &lua) { sol::usertype type = lua.new_usertype("EquirectangularProj"); type["init"] = &geodetic::projection::EquirectangularProjection::init; type["forward"] = [](geodetic::projection::EquirectangularProjection &equ, double lon, double lat) -> std::pair { int x2, y2; equ.forward(lon, lat, x2, y2); return {x2, y2}; }; type["reverse"] = [](geodetic::projection::EquirectangularProjection &equ, int x, int y) -> std::pair { float lon, lat; equ.reverse(x, y, lon, lat); return {lon, lat}; }; } };