From 0f0bd3ff6d5ac4d6d1cef09510978d44b4cc8a3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mael=20Rouxel-Labb=C3=A9?= Date: Fri, 25 Nov 2022 01:04:45 +0100 Subject: [PATCH] WIP bmp reading --- .../include/CGAL/IO/read_vtk_image_data.h | 47 +++++++++++++++---- CGAL_ImageIO/include/CGAL/Image_3.h | 2 +- Mesh_3/doc/Mesh_3/CGAL/Image_3.h | 2 +- .../Plugins/Mesh_3/Io_image_plugin.cpp | 24 ++++++---- 4 files changed, 55 insertions(+), 20 deletions(-) diff --git a/CGAL_ImageIO/include/CGAL/IO/read_vtk_image_data.h b/CGAL_ImageIO/include/CGAL/IO/read_vtk_image_data.h index 3b7efa6c4df..f230bc32bc3 100644 --- a/CGAL_ImageIO/include/CGAL/IO/read_vtk_image_data.h +++ b/CGAL_ImageIO/include/CGAL/IO/read_vtk_image_data.h @@ -69,26 +69,55 @@ read_vtk_image_data(vtkImageData* vtk_image, Image_3::Own owning = Image_3::OWN_ image->ty = static_cast(offset[1]); image->tz = static_cast(offset[2]); image->endianness = ::_getEndianness(); + int vtk_type = vtk_image->GetScalarType(); if(vtk_type == VTK_SIGNED_CHAR) vtk_type = VTK_CHAR; - if(vtk_type < 0 || vtk_type > VTK_DOUBLE) - vtk_type = VTK_DOUBLE; - const VTK_to_ImageIO_type_mapper& imageio_type = - VTK_to_ImageIO_type[vtk_type]; + if(vtk_type < 0 || vtk_type > VTK_DOUBLE) vtk_type = VTK_DOUBLE; + const VTK_to_ImageIO_type_mapper& imageio_type = VTK_to_ImageIO_type[vtk_type]; image->wdim = imageio_type.wdim; image->wordKind = imageio_type.wordKind; image->sign = imageio_type.sign; + + const int cn = vtk_image->GetNumberOfScalarComponents(); + if (!vtk_image->GetPointData() || !vtk_image->GetPointData()->GetScalars()) { ::_freeImage(image); return Image_3(); } + + // If there is more than a scalar per point, vtk_image->data is not immediately + // interpretable in Image_3->data + CGAL_assertion(owning == Image_3::OWN_THE_DATA || cn == 1); + CGAL_assertion(vtk_image->GetPointData()->GetScalars()->GetNumberOfTuples() == dims[0]*dims[1]*dims[2]); + if(owning == Image_3::OWN_THE_DATA) { - image->data = ::ImageIO_alloc(dims[0]*dims[1]*dims[2]*image->wdim); - std::cerr << "GetNumberOfTuples()=" << vtk_image->GetPointData()->GetScalars()->GetNumberOfTuples() - << "\nimage->size()=" << dims[0]*dims[1]*dims[2] - << "\nwdim=" << image->wdim << '\n'; - vtk_image->GetPointData()->GetScalars()->ExportToVoidPointer(image->data); + int dims_n = dims[0]*dims[1]*dims[2]; + image->data = ::ImageIO_alloc(dims_n * image->wdim); + std::cerr << "GetNumberOfTuples() = " << vtk_image->GetPointData()->GetScalars()->GetNumberOfTuples() << "\n" + << "components = " << cn << "\n" + << "wdim = " << image->wdim << "\n" + << "image->size() = " << dims_n << std::endl; + + if(cn == 1) { + vtk_image->GetPointData()->GetScalars()->ExportToVoidPointer(image->data); + } else { + std::cerr << "Warning: input has " << cn << " components; only the value of the first component will be used." << std::endl; + + // cast the data void pointers to make it possible to do pointer arithmetic + char* src = static_cast(vtk_image->GetPointData()->GetScalars()->GetVoidPointer(0)); + char* dest = static_cast(image->data); + + for(int i=0; iwdim because we casted to char* and not the actual data type + memcpy(dest + image->wdim*i, src + cn*image->wdim*i, image->wdim * sizeof(char)); + + // Check that we are not discarding useful data + CGAL_assertion(*(src + cn*image->wdim*i) == *(src + cn*image->wdim*i + 1)); + CGAL_assertion(*(src + cn*image->wdim*i) == *(src + cn*image->wdim*i + 2)); + } + } } else { image->data = vtk_image->GetPointData()->GetScalars()->GetVoidPointer(0); } diff --git a/CGAL_ImageIO/include/CGAL/Image_3.h b/CGAL_ImageIO/include/CGAL/Image_3.h index f6b186ef36c..e653e9de83b 100644 --- a/CGAL_ImageIO/include/CGAL/Image_3.h +++ b/CGAL_ImageIO/include/CGAL/Image_3.h @@ -87,7 +87,7 @@ public: protected: Image_shared_ptr image_ptr; - // implementation in src/CGAL_ImageIO/Image_3.cpp + // implementation in Image_3_impl.h bool private_read(_image* im, Own own_the_data = OWN_THE_DATA); public: diff --git a/Mesh_3/doc/Mesh_3/CGAL/Image_3.h b/Mesh_3/doc/Mesh_3/CGAL/Image_3.h index 415638914a4..3f5bce1ddcb 100644 --- a/Mesh_3/doc/Mesh_3/CGAL/Image_3.h +++ b/Mesh_3/doc/Mesh_3/CGAL/Image_3.h @@ -14,7 +14,7 @@ public: /// The default-constructor. The object is invalid until a call to `read()`. Image_3(); - /// Open an 3D image file. + /// Open a 3D image file. /// /// Returns `true` if the file was sucessfully loaded. bool read(const char* file); diff --git a/Polyhedron/demo/Polyhedron/Plugins/Mesh_3/Io_image_plugin.cpp b/Polyhedron/demo/Polyhedron/Plugins/Mesh_3/Io_image_plugin.cpp index 671850655d6..d13f4f8080f 100644 --- a/Polyhedron/demo/Polyhedron/Plugins/Mesh_3/Io_image_plugin.cpp +++ b/Polyhedron/demo/Polyhedron/Plugins/Mesh_3/Io_image_plugin.cpp @@ -1321,14 +1321,23 @@ Image* Io_image_plugin::createDCMImage(QString dirname) bool is_dcm = false; bool is_bmp = false; + std::vector paths; vtkStringArray* files = vtkStringArray::New(); boost::filesystem::path p(dirname.toUtf8().data()); for(boost::filesystem::directory_entry& x : boost::filesystem::directory_iterator(p)){ std::string s(x.path().extension().string()); - if(s == std::string(".dcm") || (s == std::string(".DCM"))){ is_dcm = true;} - if(s == std::string(".bmp") || (s == std::string(".BMP"))){ is_bmp = true;} - std::cout << x.path().string() << std::endl; - files->InsertNextValue(x.path().string()); + if(s == std::string(".dcm") || (s == std::string(".DCM"))){ is_dcm = true; CGAL_assertion(!is_bmp); } + if(s == std::string(".bmp") || (s == std::string(".BMP"))){ is_bmp = true; CGAL_assertion(!is_dcm); } + paths.push_back(x.path()); + } + + // directory_iterator does not guarantee a sorted order + std::sort(std::begin(paths), std::end(paths)); + + for(const boost::filesystem::path& p : paths) + { + std::cout << p.string() << std::endl; + files->InsertNextValue(p.string()); } if(is_dcm){ @@ -1369,17 +1378,14 @@ Image* Io_image_plugin::createDCMImage(QString dirname) smoother->Update(); auto vtk_image = smoother->GetOutput(); vtk_image->Print(std::cerr); - image = new Image; - std::cout << "A" << std::endl; + image = new Image; *image = CGAL::IO::read_vtk_image_data(vtk_image); // copy the // image data - - std::cout << "B" << std::endl; } #else - CGAL::Three::Three::warning("You need VTK to read a DCM file"); + CGAL::Three::Three::warning("You need VTK to read DCM/BMP files"); CGAL_USE(dirname); #endif return image;