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