From 2a7d0b4ea1500e905bcc143cb738e06ac483c7c1 Mon Sep 17 00:00:00 2001 From: nihuini Date: Thu, 4 Mar 2021 15:18:12 +0800 Subject: [PATCH] fix imdecode --- highgui/src/highgui.cpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/highgui/src/highgui.cpp b/highgui/src/highgui.cpp index 473c481f..866f000c 100644 --- a/highgui/src/highgui.cpp +++ b/highgui/src/highgui.cpp @@ -177,7 +177,7 @@ bool imwrite(const String& filename, InputArray _img, const std::vector& pa return success; } -Mat imdecode(InputArray buf, int flags) +Mat imdecode(InputArray _buf, int flags) { int desired_channels = 0; if (flags == IMREAD_UNCHANGED) @@ -198,14 +198,19 @@ Mat imdecode(InputArray buf, int flags) return Mat(); } + Mat buf = _buf.getMat(); + + if (!buf.isContinuous()) + { + buf = buf.clone(); + } + + size_t buf_size = buf.cols * buf.rows * buf.elemSize(); + int w; int h; int c; -#if CV_VERSION_EPOCH == 2 - unsigned char* pixeldata = stbi_load_from_memory((const unsigned char*)buf.obj, (int)buf.total(), &w, &h, &c, desired_channels); -#else - unsigned char* pixeldata = stbi_load_from_memory((const unsigned char*)buf.getObj(), (int)buf.total(), &w, &h, &c, desired_channels); -#endif + unsigned char* pixeldata = stbi_load_from_memory((const unsigned char*)buf.data, buf_size, &w, &h, &c, desired_channels); if (!pixeldata) { // load failed