DGtal 1.3.0
Loading...
Searching...
No Matches
Macros | Functions
raw2HDF5.cpp File Reference

A raw to HDF5 converter example (first version, restriction: only 3D UInt8 data input file, HDF5 output file with ZLIB compression activated) More...

#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"

Go to the source code of this file.

Macros

#define DATASETNAME_3D   "UInt8Array3D"
 
#define RANK_3D   3
 

Functions

bool raw2HDF5_3D (char *rawFilename, int sizeX, int sizeY, int sizeZ, int sizeChunk, char *HDF5Filename)
 
bool HDF5_3D2vol (char *HDF5Filename, char *volFileName)
 
int main (int argc, char **argv)
 

Detailed Description

A raw to HDF5 converter example (first version, restriction: only 3D UInt8 data input file, HDF5 output file with ZLIB compression activated)

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Author
Martial Tola (marti.nosp@m.al.t.nosp@m.ola@l.nosp@m.iris.nosp@m..cnrs.nosp@m..fr ) Laboratoire d'InfoRmatique en Image et Systèmes d'information - LIRIS (CNRS, UMR 5205), CNRS, France
Date
2013/08/28

This file is part of the DGtal library.

Definition in file raw2HDF5.cpp.

Macro Definition Documentation

◆ DATASETNAME_3D

#define DATASETNAME_3D   "UInt8Array3D"

Definition at line 56 of file raw2HDF5.cpp.

◆ RANK_3D

#define RANK_3D   3
Examples
images/raw2HDF5.cpp.

Definition at line 57 of file raw2HDF5.cpp.

Function Documentation

◆ HDF5_3D2vol()

bool HDF5_3D2vol ( char *  HDF5Filename,
char *  volFileName 
)
Examples
images/raw2HDF5.cpp.

Definition at line 199 of file raw2HDF5.cpp.

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}
Aim: implements a factory from an HDF5 file.
Aim: implements association bewteen points lying in a digital domain and values.
Definition: Image.h:70
void beginBlock(const std::string &keyword="")
double endBlock()
Trace trace
Definition: Common.h:154
#define DATASETNAME_3D
Definition: raw2HDF5.cpp:56
static bool exportVol(const std::string &filename, const Image &aImage, const bool compressed=true, const Functor &aFunctor=Functor())

References DGtal::Trace::beginBlock(), DATASETNAME_3D, DGtal::Trace::endBlock(), DGtal::VolWriter< TImage, TFunctor >::exportVol(), and DGtal::trace.

Referenced by main().

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 222 of file raw2HDF5.cpp.

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}
std::ostream & info()
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

References HDF5_3D2vol(), DGtal::Trace::info(), raw2HDF5_3D(), and DGtal::trace.

◆ raw2HDF5_3D()

bool raw2HDF5_3D ( char *  rawFilename,
int  sizeX,
int  sizeY,
int  sizeZ,
int  sizeChunk,
char *  HDF5Filename 
)
Examples
images/raw2HDF5.cpp.

Definition at line 59 of file raw2HDF5.cpp.

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}
std::ostream & error()
boost::uint8_t uint8_t
unsigned 8-bit integer.
Definition: BasicTypes.h:59
#define RANK_3D
Definition: raw2HDF5.cpp:57

References DGtal::Trace::beginBlock(), DATASETNAME_3D, DGtal::Trace::endBlock(), DGtal::Trace::error(), DGtal::Trace::info(), RANK_3D, and DGtal::trace.

Referenced by main().