From e114381075aa1620c19fa09067bb1ee7b6ad1ace Mon Sep 17 00:00:00 2001 From: Patrick Chilton Date: Fri, 31 Mar 2023 17:42:09 +0200 Subject: [PATCH 1/2] Handle dtypes like =d correctly. --- src/pye57/libe57_wrapper.cpp | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/pye57/libe57_wrapper.cpp b/src/pye57/libe57_wrapper.cpp index beee197..853a9cc 100644 --- a/src/pye57/libe57_wrapper.cpp +++ b/src/pye57/libe57_wrapper.cpp @@ -266,6 +266,7 @@ PYBIND11_MODULE(libe57, m) { bool doScaling, size_t stride=0) { py::buffer_info info = np_array.request(); + const std::string dtype = info.format; if (info.ndim != 1) throw std::runtime_error("Incompatible buffer dimension!"); @@ -274,24 +275,24 @@ PYBIND11_MODULE(libe57, m) { new (&s) SourceDestBuffer(imf, pathName, static_cast(info.ptr), capacity, doConversion, doScaling, (stride == 0) ? sizeof(int8_t) : stride); else if (info.format == "B") new (&s) SourceDestBuffer(imf, pathName, static_cast(info.ptr), capacity, doConversion, doScaling, (stride == 0) ? sizeof(uint8_t) : stride); - else if (info.format == "h") + else if (dtype == "h" || dtype == "=h") new (&s) SourceDestBuffer(imf, pathName, static_cast(info.ptr), capacity, doConversion, doScaling, (stride == 0) ? sizeof(int16_t) : stride); - else if (info.format == "H") + else if (dtype == "H" || dtype == "=H") new (&s) SourceDestBuffer(imf, pathName, static_cast(info.ptr), capacity, doConversion, doScaling, (stride == 0) ? sizeof(uint16_t) : stride); - else if (info.format == "l") + else if (dtype == "l" || dtype == "=l") new (&s) SourceDestBuffer(imf, pathName, static_cast(info.ptr), capacity, doConversion, doScaling, (stride == 0) ? sizeof(int32_t) : stride); - else if (info.format == "L") + else if (dtype == "L" || dtype == "=L") new (&s) SourceDestBuffer(imf, pathName, static_cast(info.ptr), capacity, doConversion, doScaling, (stride == 0) ? sizeof(uint32_t) : stride); - else if (info.format == "q") + else if (dtype == "q" || dtype == "=q") new (&s) SourceDestBuffer(imf, pathName, static_cast(info.ptr), capacity, doConversion, doScaling, (stride == 0) ? sizeof(int64_t) : stride); - else if (info.format == "?") + else if (dtype == "?") new (&s) SourceDestBuffer(imf, pathName, static_cast(info.ptr), capacity, doConversion, doScaling, (stride == 0) ? sizeof(bool) : stride); - else if (info.format == "f") + else if (dtype == "f" || dtype == "=f") new (&s) SourceDestBuffer(imf, pathName, static_cast(info.ptr), capacity, doConversion, doScaling, (stride == 0) ? sizeof(float) : stride); - else if (info.format == "d") + else if (dtype == "d" || dtype == "=d") new (&s) SourceDestBuffer(imf, pathName, static_cast(info.ptr), capacity, doConversion, doScaling, (stride == 0) ? sizeof(double) : stride); else - throw py::value_error("Incompatible type (integers: bBhHlLq, bool: ?, floats: fd)"); + throw py::value_error("Incompatible type (integers: bBhHlLq, bool: ?, floats: fd), got: " + dtype); }, "destImageFile"_a, "pathName"_a, "b"_a, "capacity"_a, "doConversion"_a=false, "doScaling"_a=false, "stride"_a=0); // cls_SourceDestBuffer.def(py::init(), "destImageFile"_a, "pathName"_a, "b"_a, "capacity"_a, "doConversion"_a=false, "doScaling"_a=false, "stride"_a=sizeof(int8_t)); From 9b82078309f6702b4f24c6638052acecbac9ecbe Mon Sep 17 00:00:00 2001 From: Graham Knapp <32717635+dancergraham@users.noreply.github.com> Date: Sun, 5 May 2024 20:09:23 +0200 Subject: [PATCH 2/2] Add comments To explain the code and trigger the ci --- src/pye57/libe57_wrapper.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/pye57/libe57_wrapper.cpp b/src/pye57/libe57_wrapper.cpp index 853a9cc..f59692c 100644 --- a/src/pye57/libe57_wrapper.cpp +++ b/src/pye57/libe57_wrapper.cpp @@ -275,6 +275,8 @@ PYBIND11_MODULE(libe57, m) { new (&s) SourceDestBuffer(imf, pathName, static_cast(info.ptr), capacity, doConversion, doScaling, (stride == 0) ? sizeof(int8_t) : stride); else if (info.format == "B") new (&s) SourceDestBuffer(imf, pathName, static_cast(info.ptr), capacity, doConversion, doScaling, (stride == 0) ? sizeof(uint8_t) : stride); + // Handle fixed or native byte order from https://docs.python.org/3/library/struct.html + // Note - these may be platform dependent. Could they cause strange bugs on some platforms ? else if (dtype == "h" || dtype == "=h") new (&s) SourceDestBuffer(imf, pathName, static_cast(info.ptr), capacity, doConversion, doScaling, (stride == 0) ? sizeof(int16_t) : stride); else if (dtype == "H" || dtype == "=H")