Skip to content

Commit ea96100

Browse files
committed
Refactor: Add error handling
1 parent 697d249 commit ea96100

File tree

3 files changed

+72
-69
lines changed

3 files changed

+72
-69
lines changed

dll_src/geo_utils.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include <cstddef>
55
#include <cstdint>
66
#include <map>
7-
#include <mutex>
87
#include <unordered_map>
98
#include <vector>
109

@@ -16,7 +15,7 @@ class GeoMap {
1615
constexpr GeoMap() noexcept = default;
1716
constexpr ~GeoMap() noexcept = default;
1817

19-
constexpr void resize(std::size_t hash, std::size_t idx, std::size_t num, std::uint32_t mode) noexcept {
18+
constexpr void resize(std::size_t hash, std::size_t idx, std::size_t num, std::uint32_t mode) {
2019
if (mode == 0u) {
2120
clear(hash);
2221
return;
@@ -32,7 +31,7 @@ class GeoMap {
3231
list.resize(num);
3332
}
3433

35-
auto &block_map = list[idx];
34+
auto &block_map = list.at(idx);
3635

3736
if (mode == 1u || block_map.size() == 1)
3837
return;

dll_src/main.cpp

Lines changed: 68 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -95,85 +95,91 @@ cleanup_geo(const Param &param, const Input &input) {
9595

9696
int
9797
process_motion_blur(lua_State *L) {
98-
constexpr std::size_t prev_pos = 7;
98+
try {
99+
constexpr std::size_t prev_pos = 7;
99100

100-
Obj obj(L);
101-
const auto param = obj.get_param();
102-
auto input = obj.get_input(param.ext);
101+
Obj obj(L);
102+
const auto param = obj.get_param();
103+
auto input = obj.get_input(param.ext);
103104

104-
if (input.obj_idx >= input.obj_num)
105-
return 0;
106-
107-
const bool on = param.is_valid && (param.ext || input.frame);
108-
const bool save_ed = param.geo_cache == 2u;
109-
const bool save_st = param.geo_cache == 1u || (save_ed && (input.frame - 1) < param.ext);
110-
std::uint32_t req_smp = 0u;
105+
if (input.obj_idx >= input.obj_num)
106+
return 0;
111107

112-
geo_map.resize(input.obj_id, input.obj_idx, input.obj_num, param.geo_cache);
108+
const bool on = param.is_valid && (param.ext || input.frame);
109+
const bool save_ed = param.geo_cache == 2u;
110+
const bool save_st = param.geo_cache == 1u || (save_ed && (input.frame - 1) < param.ext);
111+
std::uint32_t req_smp = 0u;
113112

114-
if (save_st)
115-
geo_map.overwrite(input.obj_id, input.obj_idx, input.frame + 1, input.geo_curr);
113+
geo_map.resize(input.obj_id, input.obj_idx, input.obj_num, param.geo_cache);
116114

117-
if (on) {
118-
const float amt = param.shutter_angle / 360.0f;
115+
if (save_st)
116+
geo_map.overwrite(input.obj_id, input.obj_idx, input.frame + 1, input.geo_curr);
119117

120-
std::array<Vec2<float>, 2> margin{};
121-
Vec2<float> delta_res{};
122-
std::uint32_t smp = 0u;
118+
if (on) {
119+
const float amt = param.shutter_angle / 360.0f;
123120

124-
if (param.geo_cache && !input.frame)
125-
set_init_geo(param.ext, input);
121+
std::array<Vec2<float>, 2> margin{};
122+
Vec2<float> delta_res{};
123+
std::uint32_t smp = 0u;
126124

127-
auto delta = calc_delta(param, input, (save_ed && input.frame) ? prev_pos : input.frame);
125+
if (param.geo_cache && !input.frame)
126+
set_init_geo(param.ext, input);
128127

129-
if (delta.is_moved()) {
130-
margin = calc_size(delta, amt, input);
131-
delta_res = margin[0] + margin[1];
132-
req_smp = static_cast<std::uint32_t>(delta_res.norm(2));
133-
smp = std::min(req_smp, param.smp_lim - 1u);
134-
}
128+
auto delta = calc_delta(param, input, (save_ed && input.frame) ? prev_pos : input.frame);
135129

136-
if (smp) {
137-
auto init_htm = delta.calc_htm(amt, smp, true);
138-
auto drift = delta.calc_drift(amt, smp);
139-
140-
Vec2<float> res_new = input.res;
141-
Vec2<float> pivot_new = input.pivot;
142-
if (param.resize) {
143-
auto c_new = input.geo_curr.get_center() + (margin[0] - margin[1]) * 0.5f;
144-
obj.resize(margin, c_new);
145-
pivot_new = input.tf_curr.get_center() + c_new;
146-
res_new += delta_res;
130+
if (delta.is_moved()) {
131+
margin = calc_size(delta, amt, input);
132+
delta_res = margin[0] + margin[1];
133+
req_smp = static_cast<std::uint32_t>(delta_res.norm(2));
134+
smp = std::min(req_smp, param.smp_lim - 1u);
147135
}
148136

149-
Vec2<float> ps_pivot = res_new * 0.5f + pivot_new;
150-
std::vector<float> constants = {
151-
init_htm(0, 0), init_htm(0, 1), init_htm(0, 2), 0.0f,
152-
init_htm(1, 0), init_htm(1, 1), init_htm(1, 2), 0.0f,
153-
init_htm(2, 0), init_htm(2, 1), init_htm(2, 2), 0.0f,
154-
drift.get_x(), drift.get_y(), res_new.get_x(), res_new.get_y(),
155-
ps_pivot.get_x(), ps_pivot.get_y(), static_cast<float>(smp), param.mix};
156-
157-
obj.pixel_shader(param.shader_name, constants);
137+
if (smp) {
138+
auto init_htm = delta.calc_htm(amt, smp, true);
139+
auto drift = delta.calc_drift(amt, smp);
140+
141+
Vec2<float> res_new = input.res;
142+
Vec2<float> pivot_new = input.pivot;
143+
if (param.resize) {
144+
auto c_new = input.geo_curr.get_center() + (margin[0] - margin[1]) * 0.5f;
145+
obj.resize(margin, c_new);
146+
pivot_new = input.tf_curr.get_center() + c_new;
147+
res_new += delta_res;
148+
}
149+
150+
Vec2<float> ps_pivot = res_new * 0.5f + pivot_new;
151+
std::vector<float> constants = {
152+
init_htm(0, 0), init_htm(0, 1), init_htm(0, 2), 0.0f,
153+
init_htm(1, 0), init_htm(1, 1), init_htm(1, 2), 0.0f,
154+
init_htm(2, 0), init_htm(2, 1), init_htm(2, 2), 0.0f,
155+
drift.get_x(), drift.get_y(), res_new.get_x(), res_new.get_y(),
156+
ps_pivot.get_x(), ps_pivot.get_y(), static_cast<float>(smp), param.mix};
157+
158+
obj.pixel_shader(param.shader_name, constants);
159+
}
158160
}
159-
}
160161

161-
if (save_ed)
162-
geo_map.write(input.obj_id, input.obj_idx, prev_pos, input.geo_curr);
162+
if (save_ed)
163+
geo_map.write(input.obj_id, input.obj_idx, prev_pos, input.geo_curr);
163164

164-
cleanup_geo(param, input);
165+
cleanup_geo(param, input);
165166

166-
if (param.print_info) {
167-
std::string info = std::format(
168-
"[INFO]\n"
169-
"Object ID : {}\n"
170-
"Index : {}\n"
171-
"Required Samples: {}",
172-
input.obj_id, input.obj_idx, req_smp);
173-
obj.print(info);
174-
}
167+
if (param.print_info) {
168+
std::string info = std::format(
169+
"[INFO]\n"
170+
"Object ID : {}\n"
171+
"Index : {}\n"
172+
"Required Samples: {}",
173+
input.obj_id, input.obj_idx, req_smp);
174+
obj.print(info);
175+
}
175176

176-
return 0;
177+
return 0;
178+
} catch (const std::exception &e) {
179+
return luaL_error(L, "Runtime Error: %s", e.what());
180+
} catch (...) {
181+
return luaL_error(L, "Unknown Exception occurred");
182+
}
177183
}
178184

179185
static luaL_Reg functions[] = {{"process_motion_blur", process_motion_blur}, {nullptr, nullptr}};

scripts/ObjectMotionBlur_LK_template.anm2

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
${SHADER_MOTION_BLUR}
1616
]]
1717

18-
local gv = obj.getvalue
19-
20-
if (not pcall(gv, "cx")) then
18+
if (not pcall(obj.getvalue, "cx")) then
2119
debug_print("AviUtl ExEdit2 beta12 or later is required.")
2220
return
2321
end
@@ -41,7 +39,7 @@ local resize = tobool(_0.resize, c1 ~= 0) c1 = nil
4139
local geo_cache = tonumber(_0.geo_cache or s1) s1 = nil
4240
local cache_ctrl = tonumber(_0.cache_ctrl or s2) s2 = nil
4341
local obj_id = math.max(tonumber(_0.object_id) or t6, 0) t6 = nil -- beta 11a
44-
local mix = math.min(math.max(tonumber(_0.mix) or t5, 0.0), 100.0) * 0.01 t5 = nil
42+
local mix = tonumber(_0.mix) or t5 t5 = nil
4543
local print_info = tobool(_0.print_info, c2 ~= 0) c2 = nil
4644
_0 = nil
4745

0 commit comments

Comments
 (0)