Description
Is your feature request related to a problem? Please describe.
The current OpenImageIO ImageInput
and ImageOutput
APIs are quite C-like with their type erasure through void*
and TypeDesc
. Since e.g. reading is already irrespective of the native bitdepth of the image performing conversion on read I believe it would be time to add more C++-like APIs to wrap the existing functionality (not replacing it to keep backwards compatibility). This should greatly help with preventing errors when using the API and e.g. passing an incorrectly sized buffer.
Describe the solution you'd like
An example of the current ImageInput::read_image
signature could be converted to the following:
template <typename T>
class ImageInputT
{
bool read_image (int subimage, int miplevel,
int chbegin, int chend,
span<T> data,
stride_t xstride=AutoStride,
stride_t ystride=AutoStride,
stride_t zstride=AutoStride,
std::optional<std::function<bool (span<std::byte>, float>)> progress_callback = std::nullopt,
std::optional<span<std::byte>> progress_callback _data = std::nullopt)
{
// Convert to types expected by ImageInput::read_image() but make this opaque to the user
}
}
All the above proposed items are in the C++17 standard which is now the default.
With this, I would then also propose to make the docs more C++-like to e.g. convert the 'simple read' example to this:
#include <OpenImageIO/imageio.h>
using namespace OIIO;
void simple_read()
{
using bit_depth = uint8_t;
std::string filename = "tahoe.tif";
auto input_ptr= ImageInputT<bit_depth>::open(filename);
if (!input_ptr)
return;
const ImageSpec &spec = input_ptr->spec();
int xres = spec.width;
int yres = spec.height;
int nchannels = spec.nchannels;
auto pixels = std::vector<bit_depth>(xres * yres * nchannels);
input_ptr->read_image(0, 0, 0, nchannels, span<bit_depth>(pixels.begin(), pixels.end()));
// Input gets implicitly deleted on scope exit, to explicitly close call input_ptr->close()
}
The 'old' way should definitely still be kept as an additional section in the documentation
I know this feature request is fairly opinionated but I truly do believe that this is the way forward to adopt to more modern standards. By wrapping ImageInput backwards compatibility would be kept but we provide better interfaces for new users of OpenImageIO.
I was unable to find similar feature requests in the list of currently open issues but if this is a duplicate feel free to let me know. I am happy to hear your opinions