File: lua_utils.cpp

package info (click to toggle)
satdump 1.2.2%2Bgb79af48-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 81,648 kB
  • sloc: cpp: 276,768; ansic: 164,598; lisp: 1,219; sh: 283; xml: 106; makefile: 7
file content (177 lines) | stat: -rw-r--r-- 7,499 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "lua_utils.h"

#include "common/image/image.h"
#include "common/image/io.h"
#include "common/image/processing.h"
#include "common/image/image_lut.h"
#include "common/projection/projs/equirectangular.h"
#include "common/projection/sat_proj/sat_proj.h"
#include "logger.h"

namespace lua_utils
{
    sol::table mapJsonToLua(sol::state &lua, nlohmann::json json)
    {
        sol::table l = lua.create_table();
        char *ptr;
        long index;
        for (auto &el : json.items())
        {
            try
            {
                index = strtol(el.key().c_str(), &ptr, 10);
                if (*ptr == '\0')
                {
                    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
                {
                    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());
                }
            }
            catch (std::exception &)
            {
            }
        }
        return l;
    }

    void bindImageType(sol::state &lua, std::string name)
    {
        sol::usertype<image::Image> image_type = lua.new_usertype<image::Image>(
            name,
            sol::constructors<image::Image(), image::Image(int, size_t, size_t, int)>());

        image_type["clear"] = &image::Image::clear;

        image_type["get"] = [](image::Image &img, size_t i) -> int
        {
            if (i < img.size())
                return img.get(i);
            else
                return 0;
        };
        image_type["set"] = [](image::Image &img, size_t i, int x)
        {
            if (i < img.size())
                img.set(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;
        lua["image_equalize"] = &image::equalize;
        lua["image_white_balance"] = &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::brightness_contrast_old;
        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;

        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;

        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;
    }

    void bindImageTypes(sol::state &lua)
    {
        bindImageType(lua, "image_t");

        lua["image8_lut_jet"] = &image::LUT_jet<uint8_t>;
        lua["image16_lut_jet"] = &image::LUT_jet<uint16_t>;
    }

    void bindGeoTypes(sol::state &lua)
    {
        sol::usertype<geodetic::geodetic_coords_t> type = lua.new_usertype<geodetic::geodetic_coords_t>("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<satdump::SatelliteProjection> type = lua.new_usertype<satdump::SatelliteProjection>("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<geodetic::projection::EquirectangularProjection> type = lua.new_usertype<geodetic::projection::EquirectangularProjection>("EquirectangularProj");

        type["init"] = &geodetic::projection::EquirectangularProjection::init;
        type["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}; };
        type["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}; };
    }

    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); };
    }
};