WIP bmp reading

This commit is contained in:
Mael Rouxel-Labbé 2022-11-25 01:04:45 +01:00
parent b2f3eedb2f
commit 0f0bd3ff6d
4 changed files with 55 additions and 20 deletions

View File

@ -69,26 +69,55 @@ read_vtk_image_data(vtkImageData* vtk_image, Image_3::Own owning = Image_3::OWN_
image->ty = static_cast<float>(offset[1]); image->ty = static_cast<float>(offset[1]);
image->tz = static_cast<float>(offset[2]); image->tz = static_cast<float>(offset[2]);
image->endianness = ::_getEndianness(); image->endianness = ::_getEndianness();
int vtk_type = vtk_image->GetScalarType(); int vtk_type = vtk_image->GetScalarType();
if(vtk_type == VTK_SIGNED_CHAR) vtk_type = VTK_CHAR; if(vtk_type == VTK_SIGNED_CHAR) vtk_type = VTK_CHAR;
if(vtk_type < 0 || vtk_type > VTK_DOUBLE) if(vtk_type < 0 || vtk_type > VTK_DOUBLE) vtk_type = VTK_DOUBLE;
vtk_type = VTK_DOUBLE; const VTK_to_ImageIO_type_mapper& imageio_type = VTK_to_ImageIO_type[vtk_type];
const VTK_to_ImageIO_type_mapper& imageio_type =
VTK_to_ImageIO_type[vtk_type];
image->wdim = imageio_type.wdim; image->wdim = imageio_type.wdim;
image->wordKind = imageio_type.wordKind; image->wordKind = imageio_type.wordKind;
image->sign = imageio_type.sign; image->sign = imageio_type.sign;
const int cn = vtk_image->GetNumberOfScalarComponents();
if (!vtk_image->GetPointData() || !vtk_image->GetPointData()->GetScalars()) { if (!vtk_image->GetPointData() || !vtk_image->GetPointData()->GetScalars()) {
::_freeImage(image); ::_freeImage(image);
return Image_3(); 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]); CGAL_assertion(vtk_image->GetPointData()->GetScalars()->GetNumberOfTuples() == dims[0]*dims[1]*dims[2]);
if(owning == Image_3::OWN_THE_DATA) { if(owning == Image_3::OWN_THE_DATA) {
image->data = ::ImageIO_alloc(dims[0]*dims[1]*dims[2]*image->wdim); int dims_n = dims[0]*dims[1]*dims[2];
std::cerr << "GetNumberOfTuples()=" << vtk_image->GetPointData()->GetScalars()->GetNumberOfTuples() image->data = ::ImageIO_alloc(dims_n * image->wdim);
<< "\nimage->size()=" << dims[0]*dims[1]*dims[2] std::cerr << "GetNumberOfTuples() = " << vtk_image->GetPointData()->GetScalars()->GetNumberOfTuples() << "\n"
<< "\nwdim=" << image->wdim << '\n'; << "components = " << cn << "\n"
vtk_image->GetPointData()->GetScalars()->ExportToVoidPointer(image->data); << "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<char*>(vtk_image->GetPointData()->GetScalars()->GetVoidPointer(0));
char* dest = static_cast<char*>(image->data);
for(int i=0; i<dims_n; ++i)
{
// multiply by image->wdim 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 { } else {
image->data = vtk_image->GetPointData()->GetScalars()->GetVoidPointer(0); image->data = vtk_image->GetPointData()->GetScalars()->GetVoidPointer(0);
} }

View File

@ -87,7 +87,7 @@ public:
protected: protected:
Image_shared_ptr image_ptr; 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); bool private_read(_image* im, Own own_the_data = OWN_THE_DATA);
public: public:

View File

@ -14,7 +14,7 @@ public:
/// The default-constructor. The object is invalid until a call to `read()`. /// The default-constructor. The object is invalid until a call to `read()`.
Image_3(); Image_3();
/// Open an 3D image file. /// Open a 3D image file.
/// ///
/// Returns `true` if the file was sucessfully loaded. /// Returns `true` if the file was sucessfully loaded.
bool read(const char* file); bool read(const char* file);

View File

@ -1321,14 +1321,23 @@ Image* Io_image_plugin::createDCMImage(QString dirname)
bool is_dcm = false; bool is_dcm = false;
bool is_bmp = false; bool is_bmp = false;
std::vector<boost::filesystem::path> paths;
vtkStringArray* files = vtkStringArray::New(); vtkStringArray* files = vtkStringArray::New();
boost::filesystem::path p(dirname.toUtf8().data()); boost::filesystem::path p(dirname.toUtf8().data());
for(boost::filesystem::directory_entry& x : boost::filesystem::directory_iterator(p)){ for(boost::filesystem::directory_entry& x : boost::filesystem::directory_iterator(p)){
std::string s(x.path().extension().string()); std::string s(x.path().extension().string());
if(s == std::string(".dcm") || (s == std::string(".DCM"))){ is_dcm = true;} 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;} if(s == std::string(".bmp") || (s == std::string(".BMP"))){ is_bmp = true; CGAL_assertion(!is_dcm); }
std::cout << x.path().string() << std::endl; paths.push_back(x.path());
files->InsertNextValue(x.path().string()); }
// 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){ if(is_dcm){
@ -1369,17 +1378,14 @@ Image* Io_image_plugin::createDCMImage(QString dirname)
smoother->Update(); smoother->Update();
auto vtk_image = smoother->GetOutput(); auto vtk_image = smoother->GetOutput();
vtk_image->Print(std::cerr); 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 = CGAL::IO::read_vtk_image_data(vtk_image); // copy the
// image data // image data
std::cout << "B" << std::endl;
} }
#else #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); CGAL_USE(dirname);
#endif #endif
return image; return image;