diff --git a/matplotlibcpp.h b/matplotlibcpp.h index 93a72be..1766c11 100644 --- a/matplotlibcpp.h +++ b/matplotlibcpp.h @@ -804,7 +804,7 @@ bool hist(const std::vector& y, long bins=10,std::string color="b", #ifndef WITHOUT_NUMPY namespace detail { -inline void imshow(void *ptr, const NPY_TYPES type, const int rows, const int columns, const int colors, const std::map &keywords, PyObject** out) +inline void imshow(void *ptr, const NPY_TYPES type, const int rows, const int columns, const int colors, const std::map &keywords, PyObject** out, const std::vector& extent = {}) { assert(type == NPY_UINT8 || type == NPY_FLOAT); assert(colors == 1 || colors == 3 || colors == 4); @@ -818,6 +818,19 @@ inline void imshow(void *ptr, const NPY_TYPES type, const int rows, const int co // construct keyword args PyObject* kwargs = PyDict_New(); + + // kwargs needs the extent + if (!extent.empty() && extent.size() == 4) { + PyObject* extentList = PyList_New(extent.size()); + for(size_t i = 0; i < extent.size(); ++i) { + PyList_SetItem(extentList, i, PyFloat_FromDouble(extent.at(i))); + } + + PyDict_SetItemString(kwargs, "extent", extentList); + Py_DECREF(extentList); + } + + // take care of the remaining keywords for(std::map::const_iterator it = keywords.begin(); it != keywords.end(); ++it) { PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str())); @@ -836,18 +849,18 @@ inline void imshow(void *ptr, const NPY_TYPES type, const int rows, const int co } // namespace detail -inline void imshow(const unsigned char *ptr, const int rows, const int columns, const int colors, const std::map &keywords = {}, PyObject** out = nullptr) +inline void imshow(const unsigned char *ptr, const int rows, const int columns, const int colors, const std::map &keywords = {}, PyObject** out = nullptr, const std::vector& extent = {}) { - detail::imshow((void *) ptr, NPY_UINT8, rows, columns, colors, keywords, out); + detail::imshow((void *) ptr, NPY_UINT8, rows, columns, colors, keywords, out, extent); } -inline void imshow(const float *ptr, const int rows, const int columns, const int colors, const std::map &keywords = {}, PyObject** out = nullptr) +inline void imshow(const float *ptr, const int rows, const int columns, const int colors, const std::map &keywords = {}, PyObject** out = nullptr, const std::vector& extent = {}) { - detail::imshow((void *) ptr, NPY_FLOAT, rows, columns, colors, keywords, out); + detail::imshow((void *) ptr, NPY_FLOAT, rows, columns, colors, keywords, out, extent); } #ifdef WITH_OPENCV -void imshow(const cv::Mat &image, const std::map &keywords = {}) +inline void imshow(const cv::Mat &image, const std::map &keywords = {}, PyObject** out = nullptr, const std::vector& extent = {}) { // Convert underlying type of matrix, if needed cv::Mat image2; @@ -873,7 +886,7 @@ void imshow(const cv::Mat &image, const std::map &keyw cv::cvtColor(image2, image2, CV_BGRA2RGBA); } - detail::imshow(image2.data, npy_type, image2.rows, image2.cols, image2.channels(), keywords); + detail::imshow(image2.data, npy_type, image2.rows, image2.cols, image2.channels(), keywords, out, extent); } #endif // WITH_OPENCV #endif // WITHOUT_NUMPY