Example of image format transformation from raw to HDF5.
#include <iostream>
#include "DGtal/base/Common.h"
#include "DGtal/helpers/StdDefs.h"
#include <hdf5.h>
#include "DGtal/images/ImageSelector.h"
#include "DGtal/images/ImageFactoryFromHDF5.h"
#include "DGtal/io/writers/VolWriter.h"
#include "ConfigExamples.h"
using namespace std;
#define DATASETNAME_3D "UInt8Array3D"
#define RANK_3D 3
bool raw2HDF5_3D(
char *rawFilename,
int sizeX,
int sizeY,
int sizeZ,
int sizeChunk,
char *HDF5Filename)
{
hid_t file, dataset;
hid_t datatype, dataspace;
herr_t status;
hid_t plist_id;
FILE* fd = NULL;
fd = fopen(rawFilename, "rb");
if (fd == NULL)
{
return false;
}
trace.
info() <<
" open raw_file: " << rawFilename <<
" size_X: " << sizeX <<
" size_Y: " << sizeY <<
" size_Z: " << sizeZ <<
" size_CHUNK: " << sizeChunk << endl;
if (data == NULL)
{
fclose(fd);
return false;
}
if (fread(data, 1, sizeZ*sizeY*sizeX, fd) != (unsigned)sizeZ*sizeY*sizeX)
{
fclose(fd);
return false;
}
fclose(fd);
file = H5Fcreate(HDF5Filename, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
dimsf[0] = sizeZ;
dimsf[1] = sizeY;
dimsf[2] = sizeX;
dataspace = H5Screate_simple(
RANK_3D, dimsf, NULL);
plist_id = H5Pcreate(H5P_DATASET_CREATE);
cdims[0] = sizeChunk;
cdims[1] = sizeChunk;
cdims[2] = sizeChunk;
status = H5Pset_chunk(plist_id,
RANK_3D, cdims);
if (status)
{
free(data);
return false;
}
status = H5Pset_deflate(plist_id, 6);
if (status)
{
trace.
error() <<
" H5Ddeflate error" << std::endl;
free(data);
return false;
}
datatype = H5Tcopy(H5T_NATIVE_UINT8);
status = H5Tset_order(datatype, H5T_ORDER_LE);
if (status)
{
trace.
error() <<
" H5Dset_order error" << std::endl;
free(data);
return false;
}
H5P_DEFAULT, plist_id, H5P_DEFAULT);
trace.
info() <<
" begin write hdf5_file: " << HDF5Filename << endl;
status = H5Dwrite(dataset, H5T_NATIVE_UINT8, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
if (status)
{
free(data);
return false;
}
else
trace.
info() <<
" end write hdf5_file" << endl;
H5Sclose(dataspace);
H5Tclose(datatype);
H5Dclose(dataset);
H5Pclose(plist_id);
H5Fclose(file);
free(data);
return true;
}
bool HDF5_3D2vol(
char *HDF5Filename,
char *volFileName)
{
typedef ImageSelector<Z3i::Domain, DGtal::uint8_t>::Type
Image;
typedef ImageFactoryFromHDF5<Image> MyImageFactoryFromHDF5;
typedef MyImageFactoryFromHDF5::OutputImage OutputImage;
OutputImage *volImage = factImage.requestImage( factImage.domain() );
bool res = VolWriter<OutputImage>::exportVol(volFileName, *volImage);
factImage.detachImage(volImage);
return res;
}
int main(
int argc,
char** argv )
{
if (argc==7 || argc==8)
{
raw2HDF5_3D(argv[1], atoi(argv[2]), atoi(argv[3]), atoi(argv[4]), atoi(argv[5]), argv[6]);
if (argc==8)
}
else
{
trace.
info() <<
"A raw to HDF5 converter (first version, restriction: only 3D UInt8 data input file, HDF5 output file with ZLIB compression activated)" << endl;
trace.
info() <<
"Usage: raw2HDF5 <raw_file> <size_X> <size_Y> <size_Z> <size_CHUNK> <hdf5_file>" << endl;
}
return 0;
}
void beginBlock(const std::string &keyword="")
DGtal is the top-level namespace which contains all DGtal functions and types.
boost::uint8_t uint8_t
unsigned 8-bit integer.
bool HDF5_3D2vol(char *HDF5Filename, char *volFileName)
bool raw2HDF5_3D(char *rawFilename, int sizeX, int sizeY, int sizeZ, int sizeChunk, char *HDF5Filename)
int main(int argc, char **argv)
ImageContainerBySTLVector< Domain, Value > Image