From a9d19fae1c9a01045875387284a450db3d88bc96 Mon Sep 17 00:00:00 2001 From: Nabiel Kandiel Date: Thu, 30 May 2024 21:36:24 -0400 Subject: [PATCH 01/16] began implementation --- plugins/usd/module/vtkF3DUSDImporter.cxx | 4 + vtkext/private/module/vtkF3DEXRReader.cxx | 91 +++++++++++++++++++---- vtkext/private/module/vtkF3DEXRReader.h | 58 +++++++++++++++ 3 files changed, 139 insertions(+), 14 deletions(-) diff --git a/plugins/usd/module/vtkF3DUSDImporter.cxx b/plugins/usd/module/vtkF3DUSDImporter.cxx index 8c742e576a..42c35d987a 100644 --- a/plugins/usd/module/vtkF3DUSDImporter.cxx +++ b/plugins/usd/module/vtkF3DUSDImporter.cxx @@ -872,10 +872,12 @@ class vtkF3DUSDImporter::vtkInternals const std::string& assetPath = path.GetAssetPath(); std::string ext = assetPath.substr(assetPath.find_last_of('.')); reader.TakeReference(vtkImageReader2Factory::CreateImageReader2FromExtension(ext.c_str())); + std::cout << "HEREHRHEHRE " << ext << std::endl; #endif if (!reader) { + std::cout << "HEREHRHEHRE is null" << std::endl; // cannot read the image file return nullptr; } @@ -886,6 +888,7 @@ class vtkF3DUSDImporter::vtkInternals if (!asset) { // cannot get USD asset + std::cout << "HEREHRHEHRE no asset" << std::endl; return nullptr; } @@ -893,6 +896,7 @@ class vtkF3DUSDImporter::vtkInternals if (!buffer) { + std::cout << "HEREHRHEHRE no buffer" << std::endl; // buffer invalid return nullptr; } diff --git a/vtkext/private/module/vtkF3DEXRReader.cxx b/vtkext/private/module/vtkF3DEXRReader.cxx index 4eb4f28dff..6fe8bb2e03 100644 --- a/vtkext/private/module/vtkF3DEXRReader.cxx +++ b/vtkext/private/module/vtkF3DEXRReader.cxx @@ -10,6 +10,7 @@ #include #include +#include #include #include @@ -30,7 +31,7 @@ void vtkF3DEXRReader::PrintSelf(ostream& os, vtkIndent indent) //------------------------------------------------------------------------------ void vtkF3DEXRReader::ExecuteInformation() { - // XXX: Needed because of VTK initialize file pattern in the constructor for some reasons + // XXX: Needed because of VTK initialize file pattern in the constructor for some reasons delete[] this->FilePattern; this->FilePattern = nullptr; @@ -38,13 +39,13 @@ void vtkF3DEXRReader::ExecuteInformation() this->ComputeInternalFileName(this->DataExtent[4]); if (this->InternalFileName == nullptr || this->InternalFileName[0] == '\0') { - return; + // If we have no file then maybe we have the file in memory + if (!this->MemoryBuffer) + return; } - try + auto execute = [&](Imf::RgbaInputFile& file) { - Imf::RgbaInputFile file(this->InternalFileName); - Imath::Box2i dw = file.dataWindow(); this->DataExtent[0] = dw.min.x; this->DataExtent[1] = dw.max.x; @@ -56,6 +57,21 @@ void vtkF3DEXRReader::ExecuteInformation() { throw std::runtime_error("only RGB and RGBA channels are supported"); } + }; + + try + { + if (this->MemoryBuffer) + { + MemStream memoryStream("EXRmemoryStream", this->MemoryBuffer, this->MemoryBufferLength); + Imf::RgbaInputFile file = Imf::RgbaInputFile(memoryStream); + execute(file); + } + else + { + Imf::RgbaInputFile file(this->InternalFileName); + execute(file); + } } catch (const std::exception& e) { @@ -65,13 +81,14 @@ void vtkF3DEXRReader::ExecuteInformation() this->SetNumberOfScalarComponents(3); this->SetDataScalarTypeToFloat(); - + std::cout << "made it ExecuteInformation\n"; this->vtkImageReader::ExecuteInformation(); } //------------------------------------------------------------------------------ int vtkF3DEXRReader::CanReadFile(const char* fname) { + std::cout << "can read\n"; // get the magic number by reading in a file vtksys::ifstream ifs(fname, vtksys::ifstream::in); @@ -95,10 +112,12 @@ int vtkF3DEXRReader::CanReadFile(const char* fname) //------------------------------------------------------------------------------ void vtkF3DEXRReader::ExecuteDataWithInformation(vtkDataObject* output, vtkInformation* outInfo) { + std::cout << "execute with info exr\n"; vtkImageData* data = this->AllocateOutputData(output, outInfo); if (this->UpdateExtentIsEmpty(outInfo, output)) { + std::cout << "ret 1\n"; return; } @@ -106,23 +125,18 @@ void vtkF3DEXRReader::ExecuteDataWithInformation(vtkDataObject* output, vtkInfor if (!scalars) { vtkErrorMacro(<< "Could not find expected scalar array"); + std::cout << "ret 2\n"; return; } - + std::cout << "setting pix\n"; scalars->SetName("Pixels"); float* dataPtr = scalars->GetPointer(0); - try + auto execute = [&](Imf::RgbaInputFile& file) { - assert(this->InternalFileName); - Imf::setGlobalThreadCount(std::thread::hardware_concurrency()); - Imf::RgbaInputFile file(this->InternalFileName); - Imf::Array2D pixels(this->GetHeight(), this->GetWidth()); - file.setFrameBuffer(&pixels[0][0], 1, this->GetWidth()); file.readPixels(this->DataExtent[2], this->DataExtent[3]); - for (int y = this->GetHeight() - 1; y >= 0; y--) { for (int x = 0; x < this->GetWidth(); x++) @@ -134,6 +148,25 @@ void vtkF3DEXRReader::ExecuteDataWithInformation(vtkDataObject* output, vtkInfor dataPtr += 3; } } + }; + + try + { + assert(this->InternalFileName); + Imf::setGlobalThreadCount(std::thread::hardware_concurrency()); + + if (this->MemoryBuffer) + { + MemStream memoryStream("EXRmemoryStream", this->MemoryBuffer, this->MemoryBufferLength); + Imf::RgbaInputFile file = Imf::RgbaInputFile(memoryStream); + execute(file); + } + else + { + Imf::RgbaInputFile file(this->InternalFileName); + execute(file); + } + std::cout << "also made it\n"; } catch (const std::exception& e) { @@ -142,6 +175,24 @@ void vtkF3DEXRReader::ExecuteDataWithInformation(vtkDataObject* output, vtkInfor } } +//------------------------------------------------------------------------------ +/** + * Read from memory instead of file + */ +void vtkF3DEXRReader::SetMemoryBuffer(const void* buff) +{ + this->MemoryBuffer = buff; +} + +//------------------------------------------------------------------------------ +/** + * Specify the in memory image buffer length. + */ +void vtkF3DEXRReader::SetMemoryBufferLength(vtkIdType buflen) +{ + this->MemoryBufferLength = buflen; +} + //------------------------------------------------------------------------------ int vtkF3DEXRReader::GetWidth() const { @@ -153,3 +204,15 @@ int vtkF3DEXRReader::GetHeight() const { return this->DataExtent[3] - this->DataExtent[2] + 1; } + +//------------------------------------------------------------------------------ +bool vtkF3DEXRReader::MemStream::read(char c[], int n) +{ + if (pos + n <= buflen) + { + memcpy(c, buffer + pos, n); + pos += n; + return true; + } + return false; +} \ No newline at end of file diff --git a/vtkext/private/module/vtkF3DEXRReader.h b/vtkext/private/module/vtkF3DEXRReader.h index 5edabf8222..6d58ccaeb7 100644 --- a/vtkext/private/module/vtkF3DEXRReader.h +++ b/vtkext/private/module/vtkF3DEXRReader.h @@ -3,6 +3,8 @@ #include "vtkImageReader.h" +#include + class vtkF3DEXRReader : public vtkImageReader { public: @@ -31,6 +33,16 @@ class vtkF3DEXRReader : public vtkImageReader return "OpenEXR"; } + /** + * Read from memory instead of file + */ + void SetMemoryBuffer(const void* buff) override; + + /** + * Specify the in memory image buffer length. + */ + void SetMemoryBufferLength(vtkIdType buflen) override; + protected: vtkF3DEXRReader(); ~vtkF3DEXRReader() override; @@ -44,6 +56,52 @@ class vtkF3DEXRReader : public vtkImageReader private: vtkF3DEXRReader(const vtkF3DEXRReader&) = delete; void operator=(const vtkF3DEXRReader&) = delete; + + /** + * Class to treat file contents in memory like it were still in a file. + */ + class MemStream : public Imf::IStream + { + public: + MemStream(const char* name, const void* buffer, vtkIdType buflen) + : Imf::IStream(name) + , buffer(buffer) + , buflen(buflen) + , pos(0) + { + } + + bool read(char c[], int n) override; + + /** + * returns the current reading position, in bytes, from the beginning of the file. + * The next read() call will begin reading at the indicated position + */ + uint64_t tellg() override + { + return pos; + } + + /** + * sets the current reading position to pos bytes from the beginning of the "file" + */ + void seekg(uint64_t new_pos) override + { + pos = new_pos; + } + + /** + * clears any error flags (we dont have to worry about this) + */ + void clear() override + { + } + + private: + const void* buffer; + vtkIdType buflen; + uint64_t pos; + }; }; #endif From e38c1117785492c76c329bb0d047cda6c8a8a053 Mon Sep 17 00:00:00 2001 From: Nabiel Kandiel Date: Fri, 31 May 2024 17:42:55 -0400 Subject: [PATCH 02/16] remove debug --- plugins/usd/module/vtkF3DUSDImporter.cxx | 4 ---- vtkext/private/module/vtkF3DEXRReader.cxx | 7 ------- 2 files changed, 11 deletions(-) diff --git a/plugins/usd/module/vtkF3DUSDImporter.cxx b/plugins/usd/module/vtkF3DUSDImporter.cxx index 42c35d987a..8c742e576a 100644 --- a/plugins/usd/module/vtkF3DUSDImporter.cxx +++ b/plugins/usd/module/vtkF3DUSDImporter.cxx @@ -872,12 +872,10 @@ class vtkF3DUSDImporter::vtkInternals const std::string& assetPath = path.GetAssetPath(); std::string ext = assetPath.substr(assetPath.find_last_of('.')); reader.TakeReference(vtkImageReader2Factory::CreateImageReader2FromExtension(ext.c_str())); - std::cout << "HEREHRHEHRE " << ext << std::endl; #endif if (!reader) { - std::cout << "HEREHRHEHRE is null" << std::endl; // cannot read the image file return nullptr; } @@ -888,7 +886,6 @@ class vtkF3DUSDImporter::vtkInternals if (!asset) { // cannot get USD asset - std::cout << "HEREHRHEHRE no asset" << std::endl; return nullptr; } @@ -896,7 +893,6 @@ class vtkF3DUSDImporter::vtkInternals if (!buffer) { - std::cout << "HEREHRHEHRE no buffer" << std::endl; // buffer invalid return nullptr; } diff --git a/vtkext/private/module/vtkF3DEXRReader.cxx b/vtkext/private/module/vtkF3DEXRReader.cxx index 6fe8bb2e03..e51bf46955 100644 --- a/vtkext/private/module/vtkF3DEXRReader.cxx +++ b/vtkext/private/module/vtkF3DEXRReader.cxx @@ -81,14 +81,12 @@ void vtkF3DEXRReader::ExecuteInformation() this->SetNumberOfScalarComponents(3); this->SetDataScalarTypeToFloat(); - std::cout << "made it ExecuteInformation\n"; this->vtkImageReader::ExecuteInformation(); } //------------------------------------------------------------------------------ int vtkF3DEXRReader::CanReadFile(const char* fname) { - std::cout << "can read\n"; // get the magic number by reading in a file vtksys::ifstream ifs(fname, vtksys::ifstream::in); @@ -112,12 +110,10 @@ int vtkF3DEXRReader::CanReadFile(const char* fname) //------------------------------------------------------------------------------ void vtkF3DEXRReader::ExecuteDataWithInformation(vtkDataObject* output, vtkInformation* outInfo) { - std::cout << "execute with info exr\n"; vtkImageData* data = this->AllocateOutputData(output, outInfo); if (this->UpdateExtentIsEmpty(outInfo, output)) { - std::cout << "ret 1\n"; return; } @@ -125,10 +121,8 @@ void vtkF3DEXRReader::ExecuteDataWithInformation(vtkDataObject* output, vtkInfor if (!scalars) { vtkErrorMacro(<< "Could not find expected scalar array"); - std::cout << "ret 2\n"; return; } - std::cout << "setting pix\n"; scalars->SetName("Pixels"); float* dataPtr = scalars->GetPointer(0); @@ -166,7 +160,6 @@ void vtkF3DEXRReader::ExecuteDataWithInformation(vtkDataObject* output, vtkInfor Imf::RgbaInputFile file(this->InternalFileName); execute(file); } - std::cout << "also made it\n"; } catch (const std::exception& e) { From 2e0ab33d26ffa39879a0e6410bbc1f118a422496 Mon Sep 17 00:00:00 2001 From: Nabiel Kandiel Date: Fri, 31 May 2024 18:50:08 -0400 Subject: [PATCH 03/16] clean up file --- vtkext/private/module/vtkF3DEXRReader.cxx | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/vtkext/private/module/vtkF3DEXRReader.cxx b/vtkext/private/module/vtkF3DEXRReader.cxx index e51bf46955..4a916b8eb1 100644 --- a/vtkext/private/module/vtkF3DEXRReader.cxx +++ b/vtkext/private/module/vtkF3DEXRReader.cxx @@ -31,7 +31,7 @@ void vtkF3DEXRReader::PrintSelf(ostream& os, vtkIndent indent) //------------------------------------------------------------------------------ void vtkF3DEXRReader::ExecuteInformation() { - // XXX: Needed because of VTK initialize file pattern in the constructor for some reasons + // XXX: Needed because of VTK initialize file pattern in the constructor for some reasons delete[] this->FilePattern; this->FilePattern = nullptr; @@ -81,6 +81,7 @@ void vtkF3DEXRReader::ExecuteInformation() this->SetNumberOfScalarComponents(3); this->SetDataScalarTypeToFloat(); + this->vtkImageReader::ExecuteInformation(); } @@ -123,14 +124,17 @@ void vtkF3DEXRReader::ExecuteDataWithInformation(vtkDataObject* output, vtkInfor vtkErrorMacro(<< "Could not find expected scalar array"); return; } + scalars->SetName("Pixels"); float* dataPtr = scalars->GetPointer(0); auto execute = [&](Imf::RgbaInputFile& file) { Imf::Array2D pixels(this->GetHeight(), this->GetWidth()); + file.setFrameBuffer(&pixels[0][0], 1, this->GetWidth()); file.readPixels(this->DataExtent[2], this->DataExtent[3]); + for (int y = this->GetHeight() - 1; y >= 0; y--) { for (int x = 0; x < this->GetWidth(); x++) @@ -208,4 +212,4 @@ bool vtkF3DEXRReader::MemStream::read(char c[], int n) return true; } return false; -} \ No newline at end of file +} From 35e9c9f28df607a4bb4f30373476558662716dfd Mon Sep 17 00:00:00 2001 From: Nabiel Kandiel Date: Fri, 31 May 2024 20:54:39 -0400 Subject: [PATCH 04/16] fixed CI issues --- vtkext/private/module/vtkF3DEXRReader.cxx | 6 +++--- vtkext/private/module/vtkF3DEXRReader.h | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/vtkext/private/module/vtkF3DEXRReader.cxx b/vtkext/private/module/vtkF3DEXRReader.cxx index 4a916b8eb1..94b3d0ebb4 100644 --- a/vtkext/private/module/vtkF3DEXRReader.cxx +++ b/vtkext/private/module/vtkF3DEXRReader.cxx @@ -185,9 +185,9 @@ void vtkF3DEXRReader::SetMemoryBuffer(const void* buff) /** * Specify the in memory image buffer length. */ -void vtkF3DEXRReader::SetMemoryBufferLength(vtkIdType buflen) +void vtkF3DEXRReader::SetMemoryBufferLength(vtkIdType bufferLen) { - this->MemoryBufferLength = buflen; + this->MemoryBufferLength = bufferLen; } //------------------------------------------------------------------------------ @@ -205,7 +205,7 @@ int vtkF3DEXRReader::GetHeight() const //------------------------------------------------------------------------------ bool vtkF3DEXRReader::MemStream::read(char c[], int n) { - if (pos + n <= buflen) + if (pos + n <= bufflen) { memcpy(c, buffer + pos, n); pos += n; diff --git a/vtkext/private/module/vtkF3DEXRReader.h b/vtkext/private/module/vtkF3DEXRReader.h index 6d58ccaeb7..45346736b5 100644 --- a/vtkext/private/module/vtkF3DEXRReader.h +++ b/vtkext/private/module/vtkF3DEXRReader.h @@ -63,10 +63,10 @@ class vtkF3DEXRReader : public vtkImageReader class MemStream : public Imf::IStream { public: - MemStream(const char* name, const void* buffer, vtkIdType buflen) + MemStream(const char* name, const void* buff, vtkIdType bufferLen) : Imf::IStream(name) - , buffer(buffer) - , buflen(buflen) + , buffer(static_cast(buff)) + , bufflen(static_cast(bufferLen)) , pos(0) { } @@ -98,8 +98,8 @@ class vtkF3DEXRReader : public vtkImageReader } private: - const void* buffer; - vtkIdType buflen; + const char* buffer; + size_t bufflen; uint64_t pos; }; }; From 5f9ae1a7189436c070e12f1d29a76561b74d296c Mon Sep 17 00:00:00 2001 From: Nabiel Kandiel Date: Sun, 2 Jun 2024 02:00:33 -0400 Subject: [PATCH 05/16] moved class to cpp file and added unit tests --- testing/data/Rec709.exr | 3 + vtkext/private/module/Testing/CMakeLists.txt | 3 +- .../module/Testing/TestF3DEXRMemReader.cxx | 54 ++++++++++++++ vtkext/private/module/vtkF3DEXRReader.cxx | 72 +++++++++++++++---- vtkext/private/module/vtkF3DEXRReader.h | 48 ------------- 5 files changed, 117 insertions(+), 63 deletions(-) create mode 100644 testing/data/Rec709.exr create mode 100644 vtkext/private/module/Testing/TestF3DEXRMemReader.cxx diff --git a/testing/data/Rec709.exr b/testing/data/Rec709.exr new file mode 100644 index 0000000000..ce1412e961 --- /dev/null +++ b/testing/data/Rec709.exr @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:15028cbf7bf0efa43738890af715ec550d26ae9e32a1020d0f4e7c785d3a370e +size 908168 diff --git a/vtkext/private/module/Testing/CMakeLists.txt b/vtkext/private/module/Testing/CMakeLists.txt index f98d493b38..9dbe869d28 100644 --- a/vtkext/private/module/Testing/CMakeLists.txt +++ b/vtkext/private/module/Testing/CMakeLists.txt @@ -17,7 +17,8 @@ endif() if(F3D_MODULE_EXR) list(APPEND test_sources TestF3DEXRReader.cxx - TestF3DEXRReaderInvalid.cxx) + TestF3DEXRReaderInvalid.cxx + TestF3DEXRMemReader.cxx) endif() if(VTK_VERSION VERSION_LESS_EQUAL 9.1.0) diff --git a/vtkext/private/module/Testing/TestF3DEXRMemReader.cxx b/vtkext/private/module/Testing/TestF3DEXRMemReader.cxx new file mode 100644 index 0000000000..b269f66db2 --- /dev/null +++ b/vtkext/private/module/Testing/TestF3DEXRMemReader.cxx @@ -0,0 +1,54 @@ +#include +#include + +#include "vtkF3DEXRReader.h" + +#include +#include +#include + +bool readFileToVector(const std::string& filename, std::vector& buffer) +{ + std::ifstream file(filename, std::ios::binary | std::ios::ate); + if (!file) + { + return false; + } + + std::streamsize size = file.tellg(); + file.seekg(0, std::ios::beg); + + buffer.resize(size); + + if (file.read(buffer.data(), size)) + return true; + return false; +} + +int TestF3DEXRMemReader(int argc, char* argv[]) +{ + vtkNew reader; + + std::string actual_filename = std::string(argv[1]) + "data/Rec709.exr"; + std::string filename = "readFromMem.exr"; + reader->SetFileName(filename.c_str()); + std::vector buff; + readFileToVector(actual_filename, buff); + reader->SetMemoryBuffer(buff.data()); + reader->SetMemoryBufferLength(buff.size()); + reader->Update(); + + reader->Print(cout); + + vtkImageData* img = reader->GetOutput(); + + int* dims = img->GetDimensions(); + + if (dims[0] != 610 && dims[1] != 406) + { + std::cerr << "Incorrect EXR image size." << dims[0] << ":" << dims[1] << std::endl; + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} diff --git a/vtkext/private/module/vtkF3DEXRReader.cxx b/vtkext/private/module/vtkF3DEXRReader.cxx index 94b3d0ebb4..bc7a0d3cca 100644 --- a/vtkext/private/module/vtkF3DEXRReader.cxx +++ b/vtkext/private/module/vtkF3DEXRReader.cxx @@ -8,12 +8,68 @@ #include "vtksys/FStream.hxx" #include +#include #include #include #include #include +/** + * Class to treat file contents in memory like it were still in a file. + */ +class MemStream : public Imf::IStream +{ +public: + MemStream(const char* name, const void* buff, vtkIdType bufferLen) + : Imf::IStream(name) + , buffer(static_cast(buff)) + , bufflen(static_cast(bufferLen)) + , pos(0) + { + } + + bool read(char c[], int n) override + { + if (pos + n <= bufflen) + { + memcpy(c, buffer + pos, n); + pos += n; + return true; + } + return false; + } + + /** + * returns the current reading position, in bytes, from the beginning of the file. + * The next read() call will begin reading at the indicated position + */ + uint64_t tellg() override + { + return pos; + } + + /** + * sets the current reading position to pos bytes from the beginning of the "file" + */ + void seekg(uint64_t new_pos) override + { + pos = new_pos; + } + + /** + * clears any error flags (we dont have to worry about this) + */ + void clear() override + { + } + +private: + const char* buffer; + size_t bufflen; + uint64_t pos; +}; + vtkStandardNewMacro(vtkF3DEXRReader); //------------------------------------------------------------------------------ @@ -178,7 +234,7 @@ void vtkF3DEXRReader::ExecuteDataWithInformation(vtkDataObject* output, vtkInfor */ void vtkF3DEXRReader::SetMemoryBuffer(const void* buff) { - this->MemoryBuffer = buff; + MemoryBuffer = buff; } //------------------------------------------------------------------------------ @@ -187,7 +243,7 @@ void vtkF3DEXRReader::SetMemoryBuffer(const void* buff) */ void vtkF3DEXRReader::SetMemoryBufferLength(vtkIdType bufferLen) { - this->MemoryBufferLength = bufferLen; + MemoryBufferLength = bufferLen; } //------------------------------------------------------------------------------ @@ -201,15 +257,3 @@ int vtkF3DEXRReader::GetHeight() const { return this->DataExtent[3] - this->DataExtent[2] + 1; } - -//------------------------------------------------------------------------------ -bool vtkF3DEXRReader::MemStream::read(char c[], int n) -{ - if (pos + n <= bufflen) - { - memcpy(c, buffer + pos, n); - pos += n; - return true; - } - return false; -} diff --git a/vtkext/private/module/vtkF3DEXRReader.h b/vtkext/private/module/vtkF3DEXRReader.h index 45346736b5..be87206357 100644 --- a/vtkext/private/module/vtkF3DEXRReader.h +++ b/vtkext/private/module/vtkF3DEXRReader.h @@ -3,8 +3,6 @@ #include "vtkImageReader.h" -#include - class vtkF3DEXRReader : public vtkImageReader { public: @@ -56,52 +54,6 @@ class vtkF3DEXRReader : public vtkImageReader private: vtkF3DEXRReader(const vtkF3DEXRReader&) = delete; void operator=(const vtkF3DEXRReader&) = delete; - - /** - * Class to treat file contents in memory like it were still in a file. - */ - class MemStream : public Imf::IStream - { - public: - MemStream(const char* name, const void* buff, vtkIdType bufferLen) - : Imf::IStream(name) - , buffer(static_cast(buff)) - , bufflen(static_cast(bufferLen)) - , pos(0) - { - } - - bool read(char c[], int n) override; - - /** - * returns the current reading position, in bytes, from the beginning of the file. - * The next read() call will begin reading at the indicated position - */ - uint64_t tellg() override - { - return pos; - } - - /** - * sets the current reading position to pos bytes from the beginning of the "file" - */ - void seekg(uint64_t new_pos) override - { - pos = new_pos; - } - - /** - * clears any error flags (we dont have to worry about this) - */ - void clear() override - { - } - - private: - const char* buffer; - size_t bufflen; - uint64_t pos; - }; }; #endif From c4a65f034b4d32bfd383f2490e23ea2f1b5cc73e Mon Sep 17 00:00:00 2001 From: Nabiel Kandiel Date: Sun, 2 Jun 2024 02:03:15 -0400 Subject: [PATCH 06/16] add comment with image file source --- vtkext/private/module/Testing/TestF3DEXRMemReader.cxx | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/vtkext/private/module/Testing/TestF3DEXRMemReader.cxx b/vtkext/private/module/Testing/TestF3DEXRMemReader.cxx index b269f66db2..6dd8adee66 100644 --- a/vtkext/private/module/Testing/TestF3DEXRMemReader.cxx +++ b/vtkext/private/module/Testing/TestF3DEXRMemReader.cxx @@ -6,7 +6,10 @@ #include #include #include - +/** + * image file taken from + * https://github.com/AcademySoftwareFoundation/openexr/blob/370db2835843ac75f85e1386c05455f26a6ff58c/website/test_images/Chromaticities/Rec709.rst + */ bool readFileToVector(const std::string& filename, std::vector& buffer) { std::ifstream file(filename, std::ios::binary | std::ios::ate); From 6d66a8fa71d0a9519a90a78489808cafbae63825 Mon Sep 17 00:00:00 2001 From: Nabiel Kandiel Date: Sun, 2 Jun 2024 08:00:19 -0400 Subject: [PATCH 07/16] remove unused function --- vtkext/private/module/vtkF3DEXRReader.cxx | 7 ------- 1 file changed, 7 deletions(-) diff --git a/vtkext/private/module/vtkF3DEXRReader.cxx b/vtkext/private/module/vtkF3DEXRReader.cxx index bc7a0d3cca..adef84c304 100644 --- a/vtkext/private/module/vtkF3DEXRReader.cxx +++ b/vtkext/private/module/vtkF3DEXRReader.cxx @@ -57,13 +57,6 @@ class MemStream : public Imf::IStream pos = new_pos; } - /** - * clears any error flags (we dont have to worry about this) - */ - void clear() override - { - } - private: const char* buffer; size_t bufflen; From 69dc5e8e6bb617bd501e84123d74bd0011c4d7b8 Mon Sep 17 00:00:00 2001 From: Nabiel Kandiel Date: Mon, 3 Jun 2024 01:39:46 -0400 Subject: [PATCH 08/16] add integration tests --- application/testing/CMakeLists.txt | 3 +++ testing/baselines/TestEXRReadMemory.png | 3 +++ testing/data/sphere8.usdz | 3 +++ 3 files changed, 9 insertions(+) create mode 100644 testing/baselines/TestEXRReadMemory.png create mode 100644 testing/data/sphere8.usdz diff --git a/application/testing/CMakeLists.txt b/application/testing/CMakeLists.txt index 624507453e..8529b741bc 100644 --- a/application/testing/CMakeLists.txt +++ b/application/testing/CMakeLists.txt @@ -677,6 +677,9 @@ if(F3D_PLUGIN_BUILD_USD) # https://gitlab.kitware.com/vtk/vtk/-/merge_requests/8224 if(VTK_VERSION VERSION_GREATER_EQUAL 9.0.20210805) # for embedded material support + if(F3D_MODULE_EXR) + f3d_test(NAME TestEXRReadMemory DATA sphere8.usdz --load-plugins=usd DEFAULT_LIGHTS) + endif() f3d_test(NAME TestUSDZAnimated DATA AnimatedCube.usdz ARGS --load-plugins=usd --animation-time=0.3) f3d_test(NAME TestUSDZRigged DATA RiggedSimple.usdz ARGS --load-plugins=usd --animation-time=0.3) diff --git a/testing/baselines/TestEXRReadMemory.png b/testing/baselines/TestEXRReadMemory.png new file mode 100644 index 0000000000..6e30136b34 --- /dev/null +++ b/testing/baselines/TestEXRReadMemory.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:068c4a7428c7eeb961d5502905a642b67c85a796eb830930468b0cca780dafc4 +size 8970 diff --git a/testing/data/sphere8.usdz b/testing/data/sphere8.usdz new file mode 100644 index 0000000000..5add0f8ca3 --- /dev/null +++ b/testing/data/sphere8.usdz @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e37ad350e719ad4b8dbf41176c787fee330bd293352d0863855117f51e504c51 +size 807801 From d15f04477d3705204511cfbf42c005a18e7640a2 Mon Sep 17 00:00:00 2001 From: Nabiel Kandiel Date: Mon, 3 Jun 2024 04:06:32 -0400 Subject: [PATCH 09/16] add ARGS in cmake test --- application/testing/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/testing/CMakeLists.txt b/application/testing/CMakeLists.txt index 8529b741bc..6d202c1762 100644 --- a/application/testing/CMakeLists.txt +++ b/application/testing/CMakeLists.txt @@ -678,7 +678,7 @@ if(F3D_PLUGIN_BUILD_USD) # https://gitlab.kitware.com/vtk/vtk/-/merge_requests/8224 if(VTK_VERSION VERSION_GREATER_EQUAL 9.0.20210805) # for embedded material support if(F3D_MODULE_EXR) - f3d_test(NAME TestEXRReadMemory DATA sphere8.usdz --load-plugins=usd DEFAULT_LIGHTS) + f3d_test(NAME TestEXRReadMemory DATA sphere8.usdz ARGS --load-plugins=usd DEFAULT_LIGHTS) endif() f3d_test(NAME TestUSDZAnimated DATA AnimatedCube.usdz ARGS --load-plugins=usd --animation-time=0.3) f3d_test(NAME TestUSDZRigged DATA RiggedSimple.usdz ARGS --load-plugins=usd --animation-time=0.3) From 88622bcee106e46f494ab739e17c4db682e02511 Mon Sep 17 00:00:00 2001 From: Nabiel Kandiel Date: Wed, 12 Jun 2024 19:39:41 -0400 Subject: [PATCH 10/16] default initialize value --- vtkext/private/module/vtkF3DEXRReader.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vtkext/private/module/vtkF3DEXRReader.cxx b/vtkext/private/module/vtkF3DEXRReader.cxx index adef84c304..2b45847cfc 100644 --- a/vtkext/private/module/vtkF3DEXRReader.cxx +++ b/vtkext/private/module/vtkF3DEXRReader.cxx @@ -60,7 +60,7 @@ class MemStream : public Imf::IStream private: const char* buffer; size_t bufflen; - uint64_t pos; + uint64_t pos{ 0 }; }; vtkStandardNewMacro(vtkF3DEXRReader); From 4ea85647cd8ee68bf308b6dc0cfc8690b21efc8f Mon Sep 17 00:00:00 2001 From: Nabiel Kandiel Date: Fri, 14 Jun 2024 18:51:18 -0400 Subject: [PATCH 11/16] add license for testing data --- testing/data/DATA_LICENSES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/testing/data/DATA_LICENSES.md b/testing/data/DATA_LICENSES.md index 4e79d32600..b5d2dae2f6 100644 --- a/testing/data/DATA_LICENSES.md +++ b/testing/data/DATA_LICENSES.md @@ -53,6 +53,7 @@ - world*: VTK Data: BSD-3-Clause - 16bits.*: @bisechen: [CC0](https://creativecommons.org/publicdomain/zero/1.0/) - (ノಠ益ಠ )ノ.vtp: VTK Data: BSD-3-Clause +- Rec709.exr: [Copyright 2006 Industrial Light & Magic](https://github.com/AcademySoftwareFoundation/openexr/blob/370db2835843ac75f85e1386c05455f26a6ff58c/website/test_images/Chromaticities/Rec709.rst): BSD-3-Clause All other datasets are licensed under the BSD-3-Clause F3D license. From a9a6c81e7fdd2edcfd106643ea0b0f7cfa8e3c27 Mon Sep 17 00:00:00 2001 From: Nabiel Kandiel Date: Fri, 14 Jun 2024 19:05:45 -0400 Subject: [PATCH 12/16] change return for test --- vtkext/private/module/Testing/TestF3DEXRMemReader.cxx | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/vtkext/private/module/Testing/TestF3DEXRMemReader.cxx b/vtkext/private/module/Testing/TestF3DEXRMemReader.cxx index 6dd8adee66..a7fe9a5a46 100644 --- a/vtkext/private/module/Testing/TestF3DEXRMemReader.cxx +++ b/vtkext/private/module/Testing/TestF3DEXRMemReader.cxx @@ -20,12 +20,8 @@ bool readFileToVector(const std::string& filename, std::vector& buffer) std::streamsize size = file.tellg(); file.seekg(0, std::ios::beg); - buffer.resize(size); - - if (file.read(buffer.data(), size)) - return true; - return false; + return file.read(buffer.data(), size) ? true : false; } int TestF3DEXRMemReader(int argc, char* argv[]) From ef769771db9630cda623f0ce64b5b57713ef7b6b Mon Sep 17 00:00:00 2001 From: Nabiel Kandiel Date: Wed, 19 Jun 2024 21:58:57 -0400 Subject: [PATCH 13/16] update formatting --- vtkext/private/module/vtkF3DEXRReader.cxx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/vtkext/private/module/vtkF3DEXRReader.cxx b/vtkext/private/module/vtkF3DEXRReader.cxx index 2b45847cfc..43d4228292 100644 --- a/vtkext/private/module/vtkF3DEXRReader.cxx +++ b/vtkext/private/module/vtkF3DEXRReader.cxx @@ -90,7 +90,9 @@ void vtkF3DEXRReader::ExecuteInformation() { // If we have no file then maybe we have the file in memory if (!this->MemoryBuffer) + { return; + } } auto execute = [&](Imf::RgbaInputFile& file) From 82e2b400b40acfacb50440579e7cf50c02d2a2e8 Mon Sep 17 00:00:00 2001 From: Nabiel Kandiel Date: Fri, 21 Jun 2024 23:15:23 -0400 Subject: [PATCH 14/16] update tests --- application/testing/CMakeLists.txt | 2 +- testing/baselines/TestEXRReadMemory.png | 4 ++-- testing/data/small.usdz | 3 +++ testing/data/sphere8.usdz | 3 --- 4 files changed, 6 insertions(+), 6 deletions(-) create mode 100644 testing/data/small.usdz delete mode 100644 testing/data/sphere8.usdz diff --git a/application/testing/CMakeLists.txt b/application/testing/CMakeLists.txt index 6d202c1762..ac6347b839 100644 --- a/application/testing/CMakeLists.txt +++ b/application/testing/CMakeLists.txt @@ -678,7 +678,7 @@ if(F3D_PLUGIN_BUILD_USD) # https://gitlab.kitware.com/vtk/vtk/-/merge_requests/8224 if(VTK_VERSION VERSION_GREATER_EQUAL 9.0.20210805) # for embedded material support if(F3D_MODULE_EXR) - f3d_test(NAME TestEXRReadMemory DATA sphere8.usdz ARGS --load-plugins=usd DEFAULT_LIGHTS) + f3d_test(NAME TestEXRReadMemory DATA small.usdz ARGS --load-plugins=usd DEFAULT_LIGHTS RESOLUTION 128,128) endif() f3d_test(NAME TestUSDZAnimated DATA AnimatedCube.usdz ARGS --load-plugins=usd --animation-time=0.3) f3d_test(NAME TestUSDZRigged DATA RiggedSimple.usdz ARGS --load-plugins=usd --animation-time=0.3) diff --git a/testing/baselines/TestEXRReadMemory.png b/testing/baselines/TestEXRReadMemory.png index 6e30136b34..3491aaa776 100644 --- a/testing/baselines/TestEXRReadMemory.png +++ b/testing/baselines/TestEXRReadMemory.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:068c4a7428c7eeb961d5502905a642b67c85a796eb830930468b0cca780dafc4 -size 8970 +oid sha256:704c2a3f892e071a30a6e2c4edaa3c4c107080931cbcde406a9c06927f77a787 +size 145875 diff --git a/testing/data/small.usdz b/testing/data/small.usdz new file mode 100644 index 0000000000..032d3da132 --- /dev/null +++ b/testing/data/small.usdz @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:db6cd84120a9e61b18b05edcb6f825994da59a5878229658648e76af546f5514 +size 103799 diff --git a/testing/data/sphere8.usdz b/testing/data/sphere8.usdz deleted file mode 100644 index 5add0f8ca3..0000000000 --- a/testing/data/sphere8.usdz +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:e37ad350e719ad4b8dbf41176c787fee330bd293352d0863855117f51e504c51 -size 807801 From 767eec26f57077ae1ccfd1d56cb255ee95cb0368 Mon Sep 17 00:00:00 2001 From: Nabiel Kandiel Date: Sat, 22 Jun 2024 20:57:46 -0400 Subject: [PATCH 15/16] update tests --- application/testing/CMakeLists.txt | 2 +- testing/baselines/TestEXRReadMemory.png | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/application/testing/CMakeLists.txt b/application/testing/CMakeLists.txt index ac6347b839..6e849a0d56 100644 --- a/application/testing/CMakeLists.txt +++ b/application/testing/CMakeLists.txt @@ -678,7 +678,7 @@ if(F3D_PLUGIN_BUILD_USD) # https://gitlab.kitware.com/vtk/vtk/-/merge_requests/8224 if(VTK_VERSION VERSION_GREATER_EQUAL 9.0.20210805) # for embedded material support if(F3D_MODULE_EXR) - f3d_test(NAME TestEXRReadMemory DATA small.usdz ARGS --load-plugins=usd DEFAULT_LIGHTS RESOLUTION 128,128) + f3d_test(NAME TestEXRReadMemory DATA small.usdz ARGS --load-plugins=usd DEFAULT_LIGHTS) endif() f3d_test(NAME TestUSDZAnimated DATA AnimatedCube.usdz ARGS --load-plugins=usd --animation-time=0.3) f3d_test(NAME TestUSDZRigged DATA RiggedSimple.usdz ARGS --load-plugins=usd --animation-time=0.3) diff --git a/testing/baselines/TestEXRReadMemory.png b/testing/baselines/TestEXRReadMemory.png index 3491aaa776..ffe28c6f8a 100644 --- a/testing/baselines/TestEXRReadMemory.png +++ b/testing/baselines/TestEXRReadMemory.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:704c2a3f892e071a30a6e2c4edaa3c4c107080931cbcde406a9c06927f77a787 -size 145875 +oid sha256:4f910864b9340e6cd52290610c62494bbbbcc18ab1dafb923e6c26a5a0e809ff +size 50088 From 0f910f8fed176ae94a3ddf3c410dcca5b03e832c Mon Sep 17 00:00:00 2001 From: Nabiel Kandiel Date: Wed, 26 Jun 2024 19:40:59 -0400 Subject: [PATCH 16/16] Cleanup --- testing/data/DATA_LICENSES.md | 1 + vtkext/private/module/vtkF3DEXRReader.cxx | 33 +++++------------------ vtkext/private/module/vtkF3DEXRReader.h | 10 ------- 3 files changed, 8 insertions(+), 36 deletions(-) diff --git a/testing/data/DATA_LICENSES.md b/testing/data/DATA_LICENSES.md index b5d2dae2f6..96258d1ba4 100644 --- a/testing/data/DATA_LICENSES.md +++ b/testing/data/DATA_LICENSES.md @@ -54,6 +54,7 @@ - 16bits.*: @bisechen: [CC0](https://creativecommons.org/publicdomain/zero/1.0/) - (ノಠ益ಠ )ノ.vtp: VTK Data: BSD-3-Clause - Rec709.exr: [Copyright 2006 Industrial Light & Magic](https://github.com/AcademySoftwareFoundation/openexr/blob/370db2835843ac75f85e1386c05455f26a6ff58c/website/test_images/Chromaticities/Rec709.rst): BSD-3-Clause +- small.usdz: Copyright USD contributors: CC-BY 4.0 All other datasets are licensed under the BSD-3-Clause F3D license. diff --git a/vtkext/private/module/vtkF3DEXRReader.cxx b/vtkext/private/module/vtkF3DEXRReader.cxx index 43d4228292..20c319fa97 100644 --- a/vtkext/private/module/vtkF3DEXRReader.cxx +++ b/vtkext/private/module/vtkF3DEXRReader.cxx @@ -25,7 +25,6 @@ class MemStream : public Imf::IStream : Imf::IStream(name) , buffer(static_cast(buff)) , bufflen(static_cast(bufferLen)) - , pos(0) { } @@ -86,13 +85,10 @@ void vtkF3DEXRReader::ExecuteInformation() // Setup filename to read the header this->ComputeInternalFileName(this->DataExtent[4]); - if (this->InternalFileName == nullptr || this->InternalFileName[0] == '\0') + if ((this->InternalFileName == nullptr || this->InternalFileName[0] == '\0') && + !this->MemoryBuffer) { - // If we have no file then maybe we have the file in memory - if (!this->MemoryBuffer) - { - return; - } + return; } auto execute = [&](Imf::RgbaInputFile& file) @@ -201,7 +197,6 @@ void vtkF3DEXRReader::ExecuteDataWithInformation(vtkDataObject* output, vtkInfor try { - assert(this->InternalFileName); Imf::setGlobalThreadCount(std::thread::hardware_concurrency()); if (this->MemoryBuffer) @@ -212,6 +207,10 @@ void vtkF3DEXRReader::ExecuteDataWithInformation(vtkDataObject* output, vtkInfor } else { + if (!(this->InternalFileName)) + { + throw std::invalid_argument("Not filename in EXR Reader when no Memory Buffer is set."); + } Imf::RgbaInputFile file(this->InternalFileName); execute(file); } @@ -223,24 +222,6 @@ void vtkF3DEXRReader::ExecuteDataWithInformation(vtkDataObject* output, vtkInfor } } -//------------------------------------------------------------------------------ -/** - * Read from memory instead of file - */ -void vtkF3DEXRReader::SetMemoryBuffer(const void* buff) -{ - MemoryBuffer = buff; -} - -//------------------------------------------------------------------------------ -/** - * Specify the in memory image buffer length. - */ -void vtkF3DEXRReader::SetMemoryBufferLength(vtkIdType bufferLen) -{ - MemoryBufferLength = bufferLen; -} - //------------------------------------------------------------------------------ int vtkF3DEXRReader::GetWidth() const { diff --git a/vtkext/private/module/vtkF3DEXRReader.h b/vtkext/private/module/vtkF3DEXRReader.h index be87206357..5edabf8222 100644 --- a/vtkext/private/module/vtkF3DEXRReader.h +++ b/vtkext/private/module/vtkF3DEXRReader.h @@ -31,16 +31,6 @@ class vtkF3DEXRReader : public vtkImageReader return "OpenEXR"; } - /** - * Read from memory instead of file - */ - void SetMemoryBuffer(const void* buff) override; - - /** - * Specify the in memory image buffer length. - */ - void SetMemoryBufferLength(vtkIdType buflen) override; - protected: vtkF3DEXRReader(); ~vtkF3DEXRReader() override;