DGtal  0.9.2
raw2HDF5.cpp
1 
30 #include <iostream>
32 #include "DGtal/base/Common.h"
33 #include "DGtal/helpers/StdDefs.h"
34 
35 #include <hdf5.h>
36 
37 #include "DGtal/images/ImageSelector.h"
38 #include "DGtal/images/ImageFactoryFromHDF5.h"
39 #include "DGtal/io/writers/VolWriter.h"
40 
41 #include "ConfigExamples.h"
43 
44 using namespace std;
45 using namespace DGtal;
46 
48 
49 #define DATASETNAME_3D "UInt8Array3D"
50 #define RANK_3D 3
51 
52 bool raw2HDF5_3D(char *rawFilename, int sizeX, int sizeY, int sizeZ, int sizeChunk, char *HDF5Filename)
53 {
54  trace.beginBlock("raw2HDF5_3D");
55 
56  trace.info() << "begin" << endl;
57 
58  hid_t file, dataset; // file and dataset handles
59  hid_t datatype, dataspace; // handles
60  hsize_t dimsf[RANK_3D]; // dataset dimensions
61  herr_t status;
62  DGtal::uint8_t *data;
63 
64  // compressed dataset
65  hid_t plist_id;
66  hsize_t cdims[RANK_3D];
67  // compressed dataset
68 
69  // Data and output buffer initialization.
70  FILE* fd = NULL;
71  fd = fopen(rawFilename, "rb");
72  if (fd == NULL)
73  {
74  trace.error() << " fopen error" << endl;
75  return false;
76  }
77  trace.info() << " open raw_file: " << rawFilename << " size_X: " << sizeX << " size_Y: " << sizeY << " size_Z: " << sizeZ << " size_CHUNK: " << sizeChunk << endl;
78 
79  data = (DGtal::uint8_t*)malloc(sizeZ*sizeY*sizeX * sizeof(DGtal::uint8_t));
80  if (data == NULL)
81  {
82  trace.error() << " malloc error" << endl;
83  fclose(fd);
84  return false;
85  }
86 
87  trace.info() << " begin read" << endl;
88  if (fread(data, 1, sizeZ*sizeY*sizeX, fd) != (unsigned)sizeZ*sizeY*sizeX)
89  {
90  trace.error() << " fread failed" << endl;
91  fclose(fd);
92  return false;
93  }
94  trace.info() << " end read" << endl;
95 
96  fclose(fd);
97 
98  /*
99  * Create a new file using H5F_ACC_TRUNC access,
100  * default file creation properties, and default file
101  * access properties.
102  */
103  file = H5Fcreate(HDF5Filename, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
104 
105  // Describe the size of the array and create the data space for fixed size dataset.
106  dimsf[0] = sizeZ;
107  dimsf[1] = sizeY;
108  dimsf[2] = sizeX;
109  dataspace = H5Screate_simple(RANK_3D, dimsf, NULL);
110 
111  // compressed dataset
112  plist_id = H5Pcreate(H5P_DATASET_CREATE);
113 
114  // Dataset must be chunked for compression.
115  cdims[0] = sizeChunk;
116  cdims[1] = sizeChunk;
117  cdims[2] = sizeChunk;
118  status = H5Pset_chunk(plist_id, RANK_3D, cdims);
119  if (status)
120  {
121  trace.error() << " H5Dchunck error" << std::endl;
122  free(data);
123  return false;
124  }
125 
126  // --> Compression levels :
127  // 0 No compression
128  // 1 Best compression speed; least compression
129  // 2 through 8 Compression improves; speed degrades
130  // 9 Best compression ratio; slowest speed
131  //
132  // Set ZLIB / DEFLATE Compression using compression level 6.
133  status = H5Pset_deflate(plist_id, 6);
134  if (status)
135  {
136  trace.error() << " H5Ddeflate error" << std::endl;
137  free(data);
138  return false;
139  }
140  // compressed dataset
141 
142  /*
143  * Define datatype for the data in the file.
144  */
145  datatype = H5Tcopy(H5T_NATIVE_UINT8);
146  status = H5Tset_order(datatype, H5T_ORDER_LE);
147  if (status)
148  {
149  trace.error() << " H5Dset_order error" << std::endl;
150  free(data);
151  return false;
152  }
153 
154  /*
155  * Create a new dataset within the file using defined dataspace and
156  * datatype and default dataset creation properties.
157  */
158  dataset = H5Dcreate2(file, DATASETNAME_3D, datatype, dataspace,
159  H5P_DEFAULT, /*H5P_DEFAULT*/plist_id, H5P_DEFAULT); // here to activate compressed dataset
160 
161  // Write the data to the dataset using default transfer properties.
162  trace.info() << " begin write hdf5_file: " << HDF5Filename << endl;
163  status = H5Dwrite(dataset, H5T_NATIVE_UINT8, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
164  if (status)
165  {
166  trace.error() << " H5Dwrite error" << std::endl;
167  free(data);
168  return false;
169  }
170  else
171  trace.info() << " end write hdf5_file" << endl;
172 
173  // Close/release resources.
174  H5Sclose(dataspace);
175  H5Tclose(datatype);
176  H5Dclose(dataset);
177  // compressed dataset
178  H5Pclose(plist_id);
179  // compressed dataset
180  H5Fclose(file);
181 
182  free(data);
183 
184  trace.info() << "end" << endl;
185 
186  trace.endBlock();
187  trace.info() << endl;
188 
189  return true;
190 }
191 
192 bool HDF5_3D2vol(char *HDF5Filename, char *volFileName)
193 {
194  trace.beginBlock("HDF5_3D2vol");
195 
197 
198  typedef ImageFactoryFromHDF5<Image> MyImageFactoryFromHDF5;
199  MyImageFactoryFromHDF5 factImage(HDF5Filename, DATASETNAME_3D);
200 
201  typedef MyImageFactoryFromHDF5::OutputImage OutputImage;
202 
203  OutputImage *volImage = factImage.requestImage( factImage.domain() );
204  bool res = VolWriter<OutputImage>::exportVol(volFileName, *volImage);
205  factImage.detachImage(volImage);
206 
207  trace.endBlock();
208 
209  return res;
210 }
211 
213 // Standard services - public :
214 
215 int main( int argc, char** argv )
216 {
217  if (argc==7 || argc==8)
218  {
219  raw2HDF5_3D(argv[1], atoi(argv[2]), atoi(argv[3]), atoi(argv[4]), atoi(argv[5]), argv[6]);
220 
221  if (argc==8) // TEMP_MT, just for test
222  HDF5_3D2vol(argv[6], argv[7]);
223  }
224  else
225  {
226  trace.info() << "A raw to HDF5 converter (first version, restriction: only 3D UInt8 data input file, HDF5 output file with ZLIB compression activated)" << endl;
227  trace.info() << "Usage: raw2HDF5 <raw_file> <size_X> <size_Y> <size_Z> <size_CHUNK> <hdf5_file>" << endl;
228  }
229 
230  return 0;
231 }
232 // //
void beginBlock(const std::string &keyword="")
Aim: implements association bewteen points lying in a digital domain and values.
Definition: Image.h:69
Trace trace
Definition: Common.h:130
Aim: Export a 3D Image using the Vol formats.
Definition: VolWriter.h:68
STL namespace.
double endBlock()
DGtal is the top-level namespace which contains all DGtal functions and types.
boost::uint8_t uint8_t
unsigned 8-bit integer.
Definition: BasicTypes.h:59
std::ostream & info()
std::ostream & error()
Aim: implements a factory from an HDF5 file.