Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/doc/imagebuf.rst
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ Getting and setting pixel values

.. doxygenfunction:: OIIO::ImageBuf::get_pixels(ROI, const image_span<T>&) const
.. doxygenfunction:: OIIO::ImageBuf::get_pixels(ROI, TypeDesc, const image_span<std::byte>&) const
.. doxygenfunction:: OIIO::ImageBuf::set_pixels(ROI, const image_span<T>&)
.. doxygenfunction:: OIIO::ImageBuf::set_pixels(ROI, TypeDesc, const image_span<std::byte>&)

|

Expand Down
24 changes: 24 additions & 0 deletions src/include/OpenImageIO/imagebuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,30 @@ class OIIO_API ImageBuf {
/// itself. Return true if the operation could be completed, otherwise
/// return false.

/// Set the rectangle of pixels within the ROI to the values in the
/// `buffer`.
///
/// @param roi
/// The region of interest to copy into. A default
/// uninitialized ROI means the entire image.
/// @param buffer
/// An `image_span` delineating the extent of the safely
/// accessible memory where the results should be copied from.
/// @returns
/// Return true if the operation could be completed,
/// otherwise return false.
///
template<typename T> bool set_pixels(ROI roi, const image_span<T> buffer)
{
return set_pixels(roi, TypeDescFromC<T>::value(),
as_image_span_bytes(buffer));
}

/// Base case of set_pixels: copy from an image_span of generic bytes.
/// The requested data type is supplied by `format`.
bool set_pixels(ROI roi, TypeDesc format,
const image_span<const std::byte>& buffer);

/// Set the rectangle of pixels within the ROI to the values in the
/// `buffer`.
///
Expand Down
35 changes: 30 additions & 5 deletions src/libOpenImageIO/imagebuf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2773,8 +2773,8 @@ set_pixels_(ImageBuf& buf, ROI roi, const void* data_, stride_t xstride,


bool
ImageBuf::set_pixels(ROI roi, TypeDesc format, const void* data,
stride_t xstride, stride_t ystride, stride_t zstride)
ImageBuf::set_pixels(ROI roi, TypeDesc format,
const image_span<const std::byte>& buffer)
{
if (!initialized()) {
errorfmt("Cannot set_pixels() on an uninitialized ImageBuf");
Expand All @@ -2784,17 +2784,42 @@ ImageBuf::set_pixels(ROI roi, TypeDesc format, const void* data,
roi = this->roi();
roi.chend = std::min(roi.chend, nchannels());

ImageSpec::auto_stride(xstride, ystride, zstride, format.size(),
roi.nchannels(), roi.width(), roi.height());
if (buffer.chanstride() != stride_t(format.size())) {
errorfmt(
"set_pixels does not currently support image_span source with non-contiguous channels");
return false;
}
if (size_t(roi.nchannels()) > buffer.nchannels()
|| size_t(roi.width()) > buffer.width()
|| size_t(roi.height()) > buffer.height()
|| size_t(roi.depth()) > buffer.depth()) {
errorfmt(
"set_pixels source image_span was not big enough for the specified ROI.");
return false;
}

bool ok;
OIIO_DISPATCH_TYPES2(ok, "set_pixels", set_pixels_, spec().format, format,
*this, roi, data, xstride, ystride, zstride);
*this, roi, buffer.data(), buffer.xstride(),
buffer.ystride(), buffer.zstride());
return ok;
}



bool
ImageBuf::set_pixels(ROI roi, TypeDesc format, const void* data,
stride_t xstride, stride_t ystride, stride_t zstride)
{
image_span<const std::byte> s(reinterpret_cast<const std::byte*>(data),
roi.nchannels(), roi.width(), roi.height(),
roi.depth(), format.size(), xstride, ystride,
zstride);
return set_pixels(roi, format, s);
}



bool
ImageBuf::set_pixels(ROI roi, TypeDesc format, cspan<std::byte> buffer,
const void* buforigin, stride_t xstride, stride_t ystride,
Expand Down
9 changes: 5 additions & 4 deletions src/python/py_imagebuf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,11 @@ ImageBuf_from_buffer(const py::buffer& buffer)
spec.depth = depth;
spec.full_depth = depth;
ib.reset(spec, InitializePixels::No);
auto bufspan = cspan_from_buffer(info.ptr, format, nchans, width, height,
depth, xstride, ystride, zstride);
ib.set_pixels(get_roi(spec), format, bufspan, nullptr, xstride, ystride,
zstride);
image_span<const std::byte> bufspan(reinterpret_cast<std::byte*>(info.ptr),
nchans, width, height, depth,
format.size(), xstride, ystride,
zstride, format.size());
ib.set_pixels(get_roi(spec), format, bufspan);
return ib;
}

Expand Down
Loading