DGtal  1.5.beta
testGenericWriter.cpp
Go to the documentation of this file.
1 
31 #include <iostream>
32 #include "DGtal/base/Common.h"
33 #include "DGtal/images/ImageContainerBySTLVector.h"
34 #include "DGtal/io/readers/GenericReader.h"
35 #include "DGtal/io/writers/GenericWriter.h"
36 #include "DGtal/helpers/StdDefs.h"
37 #include "ConfigTest.h"
38 
40 
41 using namespace std;
42 using namespace DGtal;
43 
45 // Functions for testing class GenericWriter.
47 
52 {
53  unsigned int nbok = 0;
54  unsigned int nb = 0;
61 
62  Image3D32bits an32bitsImage3D (DGtal::Z3i::Domain(DGtal::Z3i::Point(0,0,0), DGtal::Z3i::Point(4,4,4)));
63  for(unsigned int i =0; i< 5; i++){
64  for(unsigned int j =0; j< 5; j++){
65  for(unsigned int k =0; k< 5; k++){
66  an32bitsImage3D.setValue(DGtal::Z3i::Point(k,j,i), 250000*i*j*k);
67  }
68  }
69  }
70  Image3D64bits an64bitsImage3D (DGtal::Z3i::Domain(DGtal::Z3i::Point(0,0,0), DGtal::Z3i::Point(4,4,4)));
71  for(unsigned int i =0; i< 5; i++){
72  for(unsigned int j =0; j< 5; j++){
73  for(unsigned int k =0; k< 5; k++){
74  an64bitsImage3D.setValue(DGtal::Z3i::Point(k,j,i), 250000*i*j*k);
75  }
76  }
77  }
78  Image2D32bits an32bitsImage2D (DGtal::Z2i::Domain(DGtal::Z2i::Point(0,0), DGtal::Z2i::Point(4,4)));
79  for(unsigned int i =0; i< 5; i++){
80  for(unsigned int j =0; j< 5; j++){
81  an32bitsImage2D.setValue(DGtal::Z2i::Point(j,i), 250000*i*j*4);
82  }
83  }
84 
85  Image2DColor anColorImage2D (DGtal::Z2i::Domain(DGtal::Z2i::Point(0,0), DGtal::Z2i::Point(255,255)));
86 
87 
88  for(unsigned int i =0; i<= 255; i++){
89  for(unsigned int j =0; j<= 255; j++){
90  anColorImage2D.setValue(DGtal::Z2i::Point(i,j),DGtal::Color(i,j,(i+j)%255));
91  }
92  }
93 
94 
95  trace.beginBlock ( "Testing block ..." );
96  std::string filenameImage1 = testPath + "samples/cat10.pgm3d";
97  trace.info() << "Reading 3D image ... ";
98  Image3D anImportedImage1 = DGtal::GenericReader<Image3D>::import(filenameImage1);
99  trace.info() <<"[done]" << std::endl;
100  trace.info() << "Testing writing PGM3D ... ";
101  bool ok1 = anImportedImage1>> "testGenericWriter.pgm3d";
102  trace.info() <<"[done]" << std::endl;
103 
104  bool okh5 = true;
105 #ifdef WITH_HDF5
106  trace.info() << "Testing writing HDF5 3D ... ";
107  okh5 = anImportedImage1 >> "testGenericWriter.h5";
108  trace.info() <<"[done]" << std::endl;
109  trace.info() << "Testing writing HDF5 3D (bis) ... ";
110  okh5 = okh5 && DGtal::GenericWriter<Image3D>::exportFile("testGenericWriter_bis.h5", anImportedImage1, "/UInt8Array3D");
111  trace.info() <<"[done]" << std::endl;
112 #endif
113 
114  trace.info() << "Testing writing vol ... ";
115  bool ok2 = anImportedImage1 >> "testGenericWriter.vol";
116  trace.info() <<"[done]" << std::endl;
117  trace.info() << "Testing writing longvol ... ";
118  bool ok2bis = an64bitsImage3D >> "testGenericWriter.longvol";
119  trace.info() <<"[done]" << std::endl;
120  trace.info() << "Testing writing raw ... ";
121  bool ok3 = anImportedImage1 >>"testGenericWriter.raw";
122  trace.info() <<"[done]" << std::endl;
123  trace.info() << "Testing writing raw (bis) ... ";
124  bool ok3bis = DGtal::GenericWriter<Image3D>::exportFile( "testGenericWriter.raw", anImportedImage1);
125  trace.info() <<"[done]" << std::endl;
126  bool okITK= true;
127 #ifdef WITH_ITK
128  for (auto ext: ITK_IO_IMAGE_EXT){
129  trace.info() << "Testing writing ITK (" << ext << ") ... ";
130  if( ext != std::string("gz") )
131  {
132  okITK &= anImportedImage1 >> (std::string("testGenericWriter.") + ext);
133  }
134  trace.info() <<"[done]" << std::endl;
135  }
136  trace.info() << "Testing writing ITK (.nii.gz) ... ";
137  okITK &= anImportedImage1 >> "testGenericWriter.nii.gz";
138  trace.info() <<"[done]" << std::endl;
139 #endif
140 
141  std::string filenameImage2 = testPath + "samples/contourS.pgm";
142 
143  trace.info() << "Reading 2D image ... ";
144  Image2D anImportedImage2 = DGtal::GenericReader<Image2D>::import(filenameImage2);
145  trace.info() <<"[done]" << std::endl;
146  trace.info() << "Testing writing pgm ... ";
147  bool ok4 = anImportedImage2 >> "testGenericWriter.pgm";
148  trace.info() <<"[done]" << std::endl;
149  trace.info() << "Testing writing raw ... ";
150  bool ok5 = anImportedImage2 >> "testGenericWriter.raw";
151  bool ok6 = DGtal::GenericWriter<Image3D32bits>::exportFile( "testGenericWriter32bits3D.raw", an32bitsImage3D);
152  bool ok7 = DGtal::GenericWriter<Image2D32bits>::exportFile( "testGenericWriter32bits2D.raw", an32bitsImage2D);
153  trace.info() <<"[done]" << std::endl;
154  trace.info() << "Testing writing color image writer ... ";
155  bool ok8 = DGtal::GenericWriter<Image2DColor>::exportFile( "testGenericWriterColorImage.ppm", anColorImage2D);
156  ok8 = ok8 && DGtal::GenericWriter<Image2DColor>::exportFile( "testGenericWriterColorImage.png", anColorImage2D);
157  trace.info() <<"[done]" << std::endl;
158  nbok += ((ok1 && okh5 && ok2 & ok2bis && ok3 && ok3bis && ok4 && ok5 && ok6 && ok7 && ok8 && okITK) ? 1 : 0);
159  nb++;
160  trace.info() << "(" << nbok << "/" << nb << ") "
161  << "true == true" << std::endl;
162  trace.endBlock();
163 
164  return nbok == nb;
165 }
166 
168 // Standard services - public :
169 
170 int main( int argc, char** argv )
171 {
172  trace.beginBlock ( "Testing class GenericWriter" );
173  trace.info() << "Args:";
174  for ( int i = 0; i < argc; ++i )
175  trace.info() << " " << argv[ i ];
176  trace.info() << endl;
177 
178  bool res = testGenericWriter(); // && ... other tests
179  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
180  trace.endBlock();
181  return res ? 0 : 1;
182 }
183 // //
Structure representing an RGB triple with alpha component.
Definition: Color.h:68
void beginBlock(const std::string &keyword="")
std::ostream & emphase()
std::ostream & info()
double endBlock()
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Definition: Common.h:153
const auto ITK_IO_IMAGE_EXT
Definition: ITKIOTrait.h:48
static TContainer import(const std::string &filename, std::vector< unsigned int > dimSpace=std::vector< unsigned int >())
static bool exportFile(const std::string &filename, const TContainer &anImage, const TFunctor &aFunctor=TFunctor())
int main(int argc, char **argv)
bool testGenericWriter()