Skip to content

Commit 372a104

Browse files
committed
add chroma.cpp Python bindings
1 parent 63db2fe commit 372a104

File tree

9 files changed

+57
-7
lines changed

9 files changed

+57
-7
lines changed

include/nanogui/chroma.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121

2222
NAMESPACE_BEGIN(nanogui)
2323

24-
Matrix3f chroma_to_rec709_matrix(const std::array<Vector2f, 4>& chroma);
24+
extern NANOGUI_EXPORT Matrix3f chroma_to_rec709_matrix(const std::array<Vector2f, 4>& chroma);
2525

26-
std::array<nanogui::Vector2f, 4> chroma_from_wp_primaries(int wp_primaries);
27-
std::string_view wp_primaries_to_string(int wp_primaties);
26+
extern NANOGUI_EXPORT std::array<nanogui::Vector2f, 4> chroma_from_wp_primaries(int wp_primaries);
27+
extern NANOGUI_EXPORT std::string_view wp_primaries_to_string(int wp_primaties);
2828

2929
// Partial implementation of https://www.itu.int/rec/T-REC-H.273-202407-I/en
3030
NAMESPACE_BEGIN(ituth273)
@@ -44,10 +44,12 @@ enum class ColorPrimaries : uint8_t {
4444
Weird = 22, // The spec says "No corresponding industry specification identified"
4545
};
4646

47-
std::string_view to_string(const ColorPrimaries primaries);
48-
std::array<nanogui::Vector2f, 4> chroma(const ColorPrimaries primaries);
47+
extern NANOGUI_EXPORT std::string_view to_string(const ColorPrimaries primaries);
48+
extern NANOGUI_EXPORT std::array<nanogui::Vector2f, 4> chroma(const ColorPrimaries primaries);
4949

50-
ColorPrimaries from_wp_primaries(int wp_primaries);
50+
extern NANOGUI_EXPORT ColorPrimaries from_wp_primaries(int wp_primaries);
51+
52+
extern NANOGUI_EXPORT ColorPrimaries from_screen(const Screen *screen);
5153

5254
NAMESPACE_END(ituth273)
5355

src/chroma.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
#include <nanogui/chroma.h>
1313
#include <nanogui/vector.h>
14+
#include <nanogui/screen.h>
15+
#include <nanogui/opengl.h>
1416

1517
#include <array>
1618
#include <stdexcept>
@@ -315,6 +317,10 @@ ColorPrimaries from_wp_primaries(int wp_primaries) {
315317
throw std::invalid_argument{"Unknown wp color primaries: " + std::to_string(wp_primaries)};
316318
}
317319

320+
ColorPrimaries from_screen(const Screen *screen) {
321+
return from_wp_primaries(glfwGetWindowPrimaries(screen->glfw_window()));
322+
}
323+
318324
NAMESPACE_END(ituth273)
319325

320326
NAMESPACE_END(nanogui)

src/python/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ nanobind_add_module(nanogui-python MODULE
3232
render.cpp
3333
quad.cpp
3434
vector.cpp
35+
chroma.cpp
3536
python.h
3637
py_doc.h)
3738

src/python/chroma.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifdef NANOGUI_PYTHON
2+
3+
#include "python.h"
4+
#include <nanogui/chroma.h>
5+
#include <nanobind/stl/array.h>
6+
7+
using namespace nanogui::ituth273;
8+
9+
void register_chroma(nb::module_ &m_) {
10+
auto m = m_.def_submodule("ituth273");
11+
12+
nb::enum_<ColorPrimaries>(m, "ColorPrimaries")
13+
.value("BT709", ColorPrimaries::BT709)
14+
.value("Unspecified", ColorPrimaries::Unspecified)
15+
.value("BT470M", ColorPrimaries::BT470M)
16+
.value("BT470BG", ColorPrimaries::BT470BG)
17+
.value("SMTPE170M", ColorPrimaries::SMPTE170M)
18+
.value("SMTP240M", ColorPrimaries::SMPTE240M)
19+
.value("Film", ColorPrimaries::Film)
20+
.value("BT2020", ColorPrimaries::BT2020)
21+
.value("SMTPE428", ColorPrimaries::SMPTE428)
22+
.value("SMTPE431", ColorPrimaries::SMPTE431)
23+
.value("SMTPE432", ColorPrimaries::SMPTE432)
24+
.value("Weird", ColorPrimaries::Weird);
25+
26+
27+
m.def("chroma_to_rec709_matrix", &chroma_to_rec709_matrix);
28+
m.def("chroma", &chroma);
29+
m.def("from_screen", &from_screen);
30+
};
31+
32+
#endif

src/python/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ extern void register_misc(nb::module_ &m);
5555
extern void register_nanovg(nb::module_ &m);
5656
extern void register_render(nb::module_ &m);
5757
extern void register_quad(nb::module_ &m);
58+
extern void register_chroma(nb::module_ &m);
5859

5960
#if defined(__APPLE__) || defined(__linux__)
6061
static void (*sigint_handler_prev)(int) = nullptr;
@@ -160,6 +161,7 @@ NB_MODULE(nanogui_ext, m_) {
160161
register_nanovg(m);
161162
register_render(m);
162163
register_quad(m);
164+
register_chroma(m);
163165
}
164166

165167
#endif

src/python/py_doc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2004,6 +2004,8 @@ static const char *__doc_nanogui_Screen_mouse_button_callback_event = R"doc()doc
20042004

20052005
static const char *__doc_nanogui_Screen_mouse_pos = R"doc(Return the last observed mouse position value)doc";
20062006

2007+
static const char *__doc_nanogui_Screen_mouse_motion_event_f = R"doc(Like mouse_motion_event(), but also capture fractional motion)doc";
2008+
20072009
static const char *__doc_nanogui_Screen_move_window_to_front = R"doc()doc";
20082010

20092011
static const char *__doc_nanogui_Screen_nvg_context = R"doc(Return a pointer to the underlying NanoVG draw context)doc";

src/python/vector.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ void register_vector(nb::module_ &m) {
163163
register_vector_type<Vector3f>(m, "Vector3f");
164164
register_vector_type<Vector4f>(m, "Vector4f");
165165

166+
register_matrix_type<Matrix3f>(m, "Matrix3f");
166167
register_matrix_type<Matrix4f>(m, "Matrix4f")
167168
.def_static("translate", &Matrix4f::translate)
168169
.def_static(

src/python/widget.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@ void register_widget(nb::module_ &m) {
235235
.def("pixel_format", &Screen::pixel_format, D(Screen, pixel_format))
236236
.def("component_format", &Screen::component_format, D(Screen, component_format))
237237
.def("nvg_flush", &Screen::nvg_flush, D(Screen, nvg_flush))
238+
.def("mouse_motion_event_f", &Screen::mouse_motion_event_f, "p"_a, "rel"_a,
239+
"button"_a, "modifiers"_a, D(Screen, mouse_motion_event_f))
238240
#if defined(NANOGUI_USE_METAL)
239241
.def("metal_layer", &Screen::metal_layer)
240242
.def("metal_texture", &Screen::metal_texture)

src/screen.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,9 @@ Screen::Screen(const Vector2i &size, const std::string &caption, bool resizable,
291291
if (env_sdr_white != nullptr)
292292
m_display_sdr_white_level_override = display_sdr_white_level = std::stof(env_sdr_white);
293293

294-
m_wants_color_management = display_primaries != 1 || display_transfer_function != 10 || display_sdr_white_level != 80.0f;
294+
m_wants_color_management = display_primaries != 1 ||
295+
display_transfer_function != 10 ||
296+
display_sdr_white_level != 80.0f;
295297

296298
glfwGetFramebufferSize(m_glfw_window, &m_fbsize[0], &m_fbsize[1]);
297299

0 commit comments

Comments
 (0)