Skip to content

Add image(buffer, size) API#2961

Draft
RealEdwardS wants to merge 1 commit intof3d-app:masterfrom
RealEdwardS:add-image-function-api
Draft

Add image(buffer, size) API#2961
RealEdwardS wants to merge 1 commit intof3d-app:masterfrom
RealEdwardS:add-image-function-api

Conversation

@RealEdwardS
Copy link

Describe your changes

Add image(buffer, size) API

Requires vtk version that contains CanReadFile(stream) API (> 9.6)
https://gitlab.kitware.com/vtk/vtk/-/commit/675e97fa64a9563f6ec6f1fbf06aba84b2920ea8

Having a bit of trouble finding the right type conversions for Javascript bindings, but a review of what I have so far & feedback would be much appreciated!

Issue ticket number and link if any

Checklist for finalizing the PR

  • I have performed a self-review of my code
  • I have added tests for new features and bugfixes
  • I have added documentation for new features
  • If it is a modifying the libf3d API, I have updated bindings
  • If it is a modifying the .github/workflows/versions.json, I have updated docker_timestamp

Continuous integration

Please write a comment to run CI, eg: \ci fast.
See here for more info.

@github-actions
Copy link

You are modifying libf3d public API! ⚠️Please update bindings accordingly⚠️!
You can find them in their respective directories: c, python, java, webassembly.

.def(py::init<>())
.def(py::init<const std::filesystem::path&>())
.def(py::init<unsigned int, unsigned int, unsigned int, f3d::image::ChannelType>())
.def(py::init([](py::bytes buffer, size_t size){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.def(py::init([](py::bytes buffer, size_t size){
.def(py::init([](py::bytes buffer){

Unlike in C which uses a raw pointer, the length can and should be retrieved from the Python buffer object, see setImageBytes above.

I believe this also applies to Java and WebAssembly

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unlike in C which uses a raw pointer, the length can and should be retrieved from the Python buffer object, see setImageBytes above.

I believe this also applies to Java and WebAssembly

Would there ever be a situation where the user wants to read only part of the buffer?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would there ever be a situation where the user wants to read only part of the buffer?

I don't think so, in python I'd slice the buffer with buffer[:n] to keep only the first n bytes. IMO, if we were concerned about allowing to read only a part of a bigger buffer (for performance/memory reasons) we'd need to have something like f(buffer, start, end) or f(buffer, start, len).

From what I can tell this whole extra size/end parameter thing is only because the technical limitation in C were pointers represent the start of the memory region and nothing else. @Meakk am I missing something?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right, let's drop the size argument, otherwise it will look odd from Python

@mwestphal mwestphal requested review from Meakk and mwestphal March 22, 2026 06:49
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should not be commited, also do not configure in source to avoid this.

#include <sstream>
#include <string>
#include <unordered_map>
#include <iostream> //debug
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to remove

Comment on lines +287 to +297
while (reader != nullptr)
{
int score = reader->CanReadFile(stream);
if (score > bestScore)
{
bestReader = reader;
bestScore = score;
}

reader = collection->GetNextItem();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is no API to do this for you in VTK ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is no API to do this for you in VTK ?

I'm looking at vtkImageReader2 docs, and I don't see an API that does this

I'm also looking at the relevant commits in vtk and there is CanReadFile(stream) support for the different readers but not a function that consolidates and finds best reader based on the stream buffer

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I forgot to add it, ill take a look, but lets of course keep it here for now.

image& operator=(const image& img) noexcept;
image(image&& img) noexcept;
image& operator=(image&& img) noexcept;
image(std::byte* byte, std::size_t size);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

document separately and add doc about the behavior, espacially the exceptions

Copy link
Member

@mwestphal mwestphal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need some changes, lets focus on the C++ api first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants