Skip to content

Commit

Permalink
fix: IBA::to_OpenCV fails for ImageCache-backed images (#4013)
Browse files Browse the repository at this point in the history
We were using parallel_convert_image but not realizing that asking for
the IB::localpixels() would give us nullptr for an IC-backed image.

Instead, "wrap" the cv::Mat with an IB and then use IBA::copy(). This
handles both in-memory and IC-backed ImageBuf's, and also has many other
advantages (such as format conversion) we may wish to take advantage of
in the future.

Fixes #3800

Signed-off-by: Larry Gritz <[email protected]>
  • Loading branch information
lgritz authored Oct 14, 2023
1 parent 5731449 commit 1484315
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/libOpenImageIO/imagebufalgo_opencv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,12 +343,10 @@ ImageBufAlgo::to_OpenCV(cv::Mat& dst, const ImageBuf& src, ROI roi,

size_t pixelsize = dstSpecFormat.size() * chans;
size_t linestep = pixelsize * roi.width();
bool converted = parallel_convert_image(
chans, roi.width(), roi.height(), 1,
src.pixeladdr(roi.xbegin, roi.ybegin, roi.zbegin, roi.chbegin),
spec.format, spec.pixel_bytes(), spec.scanline_bytes(), 0, dst.ptr(),
dstSpecFormat, pixelsize, linestep, 0, -1, -1, nthreads);

// Make an IB that wraps the OpenCV buffer, then IBA:: copy to it
ImageBuf cvib(ImageSpec(roi.width(), roi.height(), chans, dstSpecFormat),
dst.ptr(), pixelsize, linestep, AutoStride);
bool converted = ImageBufAlgo::copy(cvib, src);
if (!converted) {
OIIO::pvt::errorfmt(
"to_OpenCV() was unable to convert source {} to cv::Mat of {}",
Expand Down

0 comments on commit 1484315

Please sign in to comment.