DGtal  1.0.0
testGenericWriter.cpp
Go to the documentation of this file.
1 
30 #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;
60 
61  Image3D32bits an32bitsImage3D (DGtal::Z3i::Domain(DGtal::Z3i::Point(0,0,0), DGtal::Z3i::Point(4,4,4)));
62  for(unsigned int i =0; i< 5; i++){
63  for(unsigned int j =0; j< 5; j++){
64  for(unsigned int k =0; k< 5; k++){
65  an32bitsImage3D.setValue(DGtal::Z3i::Point(k,j,i), 250000*i*j*k);
66  }
67  }
68  }
69  Image2D32bits an32bitsImage2D (DGtal::Z2i::Domain(DGtal::Z2i::Point(0,0), DGtal::Z2i::Point(4,4)));
70  for(unsigned int i =0; i< 5; i++){
71  for(unsigned int j =0; j< 5; j++){
72  an32bitsImage2D.setValue(DGtal::Z2i::Point(j,i), 250000*i*j*4);
73  }
74  }
75 
76  Image2DColor anColorImage2D (DGtal::Z2i::Domain(DGtal::Z2i::Point(0,0), DGtal::Z2i::Point(255,255)));
77 
78 
79  for(unsigned int i =0; i<= 255; i++){
80  for(unsigned int j =0; j<= 255; j++){
81  anColorImage2D.setValue(DGtal::Z2i::Point(i,j),DGtal::Color(i,j,(i+j)%255));
82  }
83  }
84 
85 
86  trace.beginBlock ( "Testing block ..." );
87  std::string filenameImage1 = testPath + "samples/cat10.pgm3d";
88  trace.info() << "Reading 3D image ... ";
89  Image3D anImportedImage1 = DGtal::GenericReader<Image3D>::import(filenameImage1);
90  trace.info() <<"[done]" << std::endl;
91  trace.info() << "Testing writing PGM3D ... ";
92  bool ok1 = anImportedImage1>> "testGenericWriter.pgm3d";
93  trace.info() <<"[done]" << std::endl;
94 
95  bool okh5 = true;
96  bool okh5bis = true;
97 #ifdef WITH_HDF5
98  trace.info() << "Testing writing HDF5 3D ... ";
99  okh5 = anImportedImage1 >> "testGenericWriter.h5";
100  trace.info() <<"[done]" << std::endl;
101  trace.info() << "Testing writing HDF5 3D (bis) ... ";
102  okh5bis = DGtal::GenericWriter<Image3D>::exportFile("testGenericWriter_bis.h5", anImportedImage1, "/UInt8Array3D");
103  trace.info() <<"[done]" << std::endl;
104 #endif
105 
106  trace.info() << "Testing writing vol ... ";
107  bool ok2 = anImportedImage1 >> "testGenericWriter.vol";
108  trace.info() <<"[done]" << std::endl;
109  trace.info() << "Testing writing raw ... ";
110  bool ok3 = anImportedImage1 >>"testGenericWriter.raw";
111  trace.info() <<"[done]" << std::endl;
112  trace.info() << "Testing writing raw (bis) ... ";
113  bool ok3bis = DGtal::GenericWriter<Image3D>::exportFile( "testGenericWriter.raw", anImportedImage1);
114  trace.info() <<"[done]" << std::endl;
115 
116  std::string filenameImage2 = testPath + "samples/contourS.pgm";
117 
118  trace.info() << "Reading 2D image ... ";
119  Image2D anImportedImage2 = DGtal::GenericReader<Image2D>::import(filenameImage2);
120  trace.info() <<"[done]" << std::endl;
121  trace.info() << "Testing writing pgm ... ";
122  bool ok4 = anImportedImage2 >> "testGenericWriter.pgm";
123  trace.info() <<"[done]" << std::endl;
124  trace.info() << "Testing writing raw ... ";
125  bool ok5 = anImportedImage2 >> "testGenericWriter.raw";
126  trace.info() <<"[done]" << std::endl;
127  bool ok6 = DGtal::GenericWriter<Image3D32bits>::exportFile( "testGenericWriter32bits3D.raw", an32bitsImage3D);
128  trace.info() <<"[done]" << std::endl;
129  bool ok7 = DGtal::GenericWriter<Image2D32bits>::exportFile( "testGenericWriter32bits2D.raw", an32bitsImage2D);
130  trace.info() <<"[done]" << std::endl;
131  bool ok8 = DGtal::GenericWriter<Image2DColor>::exportFile( "testGenericWriterColorImage.ppm", anColorImage2D);
132  trace.info() <<"[done]" << std::endl;
133 #ifdef WITH_MAGICK
134  bool ok9 = DGtal::GenericWriter<Image2DColor>::exportFile( "testGenericWriterColorImage.png", anColorImage2D);
135 #else
136  bool ok9 = true;
137 #endif
138 
139  nbok += ((ok1 && okh5 && okh5bis && ok2 && ok3 && ok3bis && ok4 && ok5 && ok6 && ok7 && ok8 && ok9) ? 1 : 0);
140  nb++;
141  trace.info() << "(" << nbok << "/" << nb << ") "
142  << "true == true" << std::endl;
143  trace.endBlock();
144 
145  return nbok == nb;
146 }
147 
149 // Standard services - public :
150 
151 int main( int argc, char** argv )
152 {
153  trace.beginBlock ( "Testing class GenericWriter" );
154  trace.info() << "Args:";
155  for ( int i = 0; i < argc; ++i )
156  trace.info() << " " << argv[ i ];
157  trace.info() << endl;
158 
159  bool res = testGenericWriter(); // && ... other tests
160  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
161  trace.endBlock();
162  return res ? 0 : 1;
163 }
164 // //
void beginBlock(const std::string &keyword="")
Trace trace
Definition: Common.h:144
double endBlock()
static TContainer import(const std::string &filename, std::vector< unsigned int > dimSpace=std::vector< unsigned int >())
bool testGenericWriter()
std::ostream & emphase()
DGtal is the top-level namespace which contains all DGtal functions and types.
static bool exportFile(const std::string &filename, const TContainer &anImage, const TFunctor &aFunctor=TFunctor())
std::ostream & info()
Structure representing an RGB triple with alpha component.
Definition: Color.h:66
int main(int argc, char **argv)