reformat if statements, add comments on the HDF5 macros

This commit is contained in:
Li-Ta Lo 2021-01-15 15:05:52 -07:00
parent 31aa467068
commit 081f8aaea8
2 changed files with 25 additions and 3 deletions

@ -28,18 +28,24 @@ void ImageReaderHDF5::Read()
const auto fieldName = this->PointFieldName.c_str();
if (!H5IMis_image(fileid, fieldName))
{
throw vtkm::io::ErrorIO{ "Not an HDF5 image file" };
}
hsize_t width, height, nplanes;
hssize_t npals;
char interlace[16];
if (H5IMget_image_info(fileid, fieldName, &width, &height, &nplanes, interlace, &npals) < 0)
{
throw vtkm::io ::ErrorIO{ "Can not get image info" };
}
// We don't use the H5IMread_image() since it only supports 8 bit pixel.
hid_t did;
if ((did = H5Dopen2(fileid, fieldName, H5P_DEFAULT)) < 0)
{
throw vtkm::io::ErrorIO{ "Can not open image dataset" };
}
if (strncmp(interlace, "INTERLACE_PIXEL", 15) != 0)
{
@ -81,9 +87,13 @@ void ImageReaderHDF5::Read()
{
vtkm::Id hdfIndex = static_cast<vtkm::Id>(yIndex * width + xIndex);
if (type_size == 1)
{
portal.Set(vtkmIndex, vtkm::io::RGBPixel_8(buffer.data(), hdfIndex).ToVec4f());
}
else
{
portal.Set(vtkmIndex, vtkm::io::RGBPixel_16(buffer.data(), hdfIndex).ToVec4f());
}
vtkmIndex++;
}
}

@ -17,7 +17,9 @@
namespace
{
// FIXME: why does HDF5 want to call H5open() for something macro?
// This trait is written in an unusual way since HDF5 calls H5Open() in macros
// like H5T_NATIVE_UCHAR. This makes it NOT a compile time constant. We need to
// make the trait a runtime evaluated function object.
template <typename PixelType>
struct hdf5_type_trait
{
@ -31,7 +33,6 @@ struct hdf5_type_trait<vtkm::io::RGBPixel_8>
template <>
struct hdf5_type_trait<vtkm::io::RGBPixel_16>
{
// static constexpr auto type_id = H5T_NATIVE_UINT16;
auto operator()() { return H5T_NATIVE_UINT16; }
};
} //namespace
@ -79,24 +80,33 @@ herr_t ImageWriterHDF5::WriteToFile(vtkm::Id width, vtkm::Id height, const Color
// Create a HDF5 DataSet
if (H5LTmake_dataset(
this->fileid, dset_name, 3, dims, hdf5_type_trait<PixelType>{}(), imageData.data()) < 0)
{
return -1;
}
/* Attach the CLASS attribute */
if (H5LTset_attribute_string(fileid, dset_name, "CLASS", IMAGE_CLASS) < 0)
{
return -1;
}
/* Attach the VERSION attribute */
if (H5LTset_attribute_string(fileid, dset_name, "IMAGE_VERSION", IMAGE_VERSION) < 0)
{
return -1;
}
/* Attach the IMAGE_SUBCLASS attribute */
if (H5LTset_attribute_string(fileid, dset_name, "IMAGE_SUBCLASS", "IMAGE_TRUECOLOR") < 0)
{
return -1;
}
/* Attach the INTERLACE_MODE attribute. This attributes is only for true color images */
if (H5LTset_attribute_string(fileid, dset_name, "INTERLACE_MODE", "INTERLACE_PIXEL") < 0)
{
return -1;
}
return 0;
}
@ -104,7 +114,9 @@ void ImageWriterHDF5::Write(vtkm::Id width, vtkm::Id height, const ColorArrayTyp
{
this->fileid = H5Fcreate(this->FileName.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
if (this->fileid < 0)
{
throw vtkm::io::ErrorIO{ "Can not create HDF5 image file" };
}
switch (this->Depth)
{