From 7669f09d4408c8f5eed93c8eef1ad5717d1407c7 Mon Sep 17 00:00:00 2001 From: Wenzel Jakob Date: Wed, 30 Oct 2013 22:23:58 +0100 Subject: [PATCH] improved Bitmap python bindings, added a Bitmap::getChannelName function --- include/mitsuba/core/bitmap.h | 3 +++ src/libcore/bitmap.cpp | 27 +++++++++++++++++++++++++++ src/libpython/core.cpp | 22 +++++++++++++++++++++- 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/include/mitsuba/core/bitmap.h b/include/mitsuba/core/bitmap.h index 8c9d8173..861aab2d 100644 --- a/include/mitsuba/core/bitmap.h +++ b/include/mitsuba/core/bitmap.h @@ -356,6 +356,9 @@ public: /// Return whether this image has matching width and height inline bool isSquare() const { return m_size.x == m_size.y; } + /// Return a string representation of the name of a channel + std::string getChannelName(int channelIndex) const; + /// Return whether this image has an alpha channel inline bool hasAlpha() const { return diff --git a/src/libcore/bitmap.cpp b/src/libcore/bitmap.cpp index aaa8dadc..21f53232 100644 --- a/src/libcore/bitmap.cpp +++ b/src/libcore/bitmap.cpp @@ -349,6 +349,33 @@ size_t Bitmap::getBufferSize() const { return bytesPerRow * (size_t) m_size.y; } +std::string Bitmap::getChannelName(int idx) const { + Assert(idx < m_channelCount); + char name = '\0'; + + switch (m_pixelFormat) { + case ELuminance: name = 'L'; break; + case ELuminanceAlpha: name = "YA"[idx]; break; + case ERGBA: + case ERGB: name = "RGBA"[idx]; break; + case EXYZA: + case EXYZ: name = "XYZA"[idx]; break; + case ESpectrumAlphaWeight: + case ESpectrumAlpha: + if (idx == m_channelCount-1) + return m_pixelFormat == ESpectrumAlpha ? "A" : "W"; + else if (idx == m_channelCount-2 && m_pixelFormat == ESpectrumAlphaWeight) + return "A"; + case ESpectrum: + std::pair coverage = Spectrum::getBinCoverage(idx); + return formatString("%.2f-%.2fnm", coverage.first, coverage.second); + default: + Log(EError, "Unknown pixel format!"); + } + + return std::string(1, name); +} + void Bitmap::updateChannelCount() { switch (m_pixelFormat) { case ELuminance: m_channelCount = 1; break; diff --git a/src/libpython/core.cpp b/src/libpython/core.cpp index e75f5be8..6277ca4a 100644 --- a/src/libpython/core.cpp +++ b/src/libpython/core.cpp @@ -145,7 +145,6 @@ struct path_to_python_str { } }; - struct TSpectrum_to_Spectrum { static PyObject* convert(const TSpectrum &spectrum) { return bp::incref(bp::object(Spectrum(spectrum)).ptr()); @@ -777,6 +776,7 @@ void export_core() { .def("getBytesPerComponent", &Bitmap::getBytesPerComponent) .def("getBytesPerPixel", &Bitmap::getBytesPerPixel) .def("getBufferSize", &Bitmap::getBufferSize) + .def("getChannelName", &Bitmap::getChannelName) .def("getPixel", &Bitmap::getPixel, BP_RETURN_VALUE) .def("setPixel", &Bitmap::setPixel) .def("drawHLine", &Bitmap::drawHLine) @@ -788,6 +788,7 @@ void export_core() { .def("convert", &bitmap_convert_2, BP_RETURN_VALUE) .def("convert", &bitmap_convert_3, BP_RETURN_VALUE) .def("convert", &bitmap_convert_4, BP_RETURN_VALUE) + .def("rotateFlip", &Bitmap::rotateFlip, BP_RETURN_VALUE) .def("fromByteArray", &bitmap_fromByteArray) .def("toByteArray", &bitmap_toByteArray_1) .def("toByteArray", &bitmap_toByteArray_2); @@ -827,6 +828,25 @@ void export_core() { .value("EAuto", Bitmap::EAuto) .export_values(); + bp::enum_("ERotateFlipType") + .value("ERotateNoneFlipNone", Bitmap::ERotateNoneFlipNone) + .value("ERotate180FlipXY", Bitmap::ERotate180FlipXY) + .value("ERotate90FlipNone", Bitmap::ERotate90FlipNone) + .value("ERotate270FlipXY", Bitmap::ERotate270FlipXY) + .value("ERotate180FlipNone", Bitmap::ERotate180FlipNone) + .value("ERotateNoneFlipXY", Bitmap::ERotateNoneFlipXY) + .value("ERotate270FlipNone", Bitmap::ERotate270FlipNone) + .value("ERotate90FlipXY", Bitmap::ERotate90FlipXY) + .value("ERotateNoneFlipX", Bitmap::ERotateNoneFlipX) + .value("ERotate180FlipY", Bitmap::ERotate180FlipY) + .value("ERotate90FlipX", Bitmap::ERotate90FlipX) + .value("ERotate270FlipY", Bitmap::ERotate270FlipY) + .value("ERotate180FlipX", Bitmap::ERotate180FlipX) + .value("ERotateNoneFlipY", Bitmap::ERotateNoneFlipY) + .value("ERotate270FlipX", Bitmap::ERotate270FlipX) + .value("ERotate90FlipY", Bitmap::ERotate90FlipY) + .export_values(); + BP_SETSCOPE(coreModule); BP_CLASS(FileResolver, Object, bp::init<>())