Commit f135b395 authored by Tomáš Oberhuber's avatar Tomáš Oberhuber
Browse files

Adding conversion from .tnl file to image.

parent 5267ce8b
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -181,7 +181,11 @@ parseCommandLine( int argc, char* argv[],
         parse_error = true;
         continue;
      }
      
      if( strcmp( _option, "--help" ) == 0 )
      {
          config_description.printUsage( argv[ 0 ] );
          continue;
      }
      const char* option = _option + 2;
      const tnlConfigEntryBase* entry;
      if( ( entry = config_description.getEntry( option ) ) == NULL )
+19 −1
Original line number Diff line number Diff line
@@ -40,13 +40,31 @@ class tnlPGMImage : public tnlImage< Index >
                 const tnlGrid< 2, Real, Device, Index >& grid,
                 Vector& vector );
      
      template< typename Real,
                typename Device >
      bool openForWrite( const tnlString& fileName,
                         tnlGrid< 2, Real, Device, Index >& grid,
                         bool binary = true );
      
      template< typename Real,
                typename Device,
                typename Vector >
      bool write( const tnlGrid< 2, Real, Device, Index >& grid,
                  Vector& vector );

      
      void close();
      
      ~tnlPGMImage();
      
      protected:
         
         bool readHeader( FILE* file );
         bool readHeader();
         
         template< typename Real,
                   typename Device >
         bool writeHeader( const tnlGrid< 2, Real, Device, Index >& grid,
                           bool binary );
         
         bool binary;
         
+70 −6
Original line number Diff line number Diff line
@@ -31,11 +31,11 @@ tnlPGMImage() :
template< typename Index >
bool
tnlPGMImage< Index >::
readHeader( FILE* file )
readHeader()
{
   char magicNumber[ 3 ];
   magicNumber[ 2 ] = 0;
   if( fread( magicNumber, sizeof( char ), 2, file ) != 2 )
   if( fread( magicNumber, sizeof( char ), 2, this->file ) != 2 )
   {
      cerr << "Unable to read the magic number." << endl;
      return false;
@@ -49,7 +49,7 @@ readHeader( FILE* file )
      this->binary = true;

   char line[ 1024 ];
   while( fread( line, sizeof( char ), 1, file ) &&
   while( fread( line, sizeof( char ), 1, this->file ) &&
          ( line[ 0 ] == ' ' || line[ 0 ] == '\t' ||
            line[ 0 ] == '\r' || line[ 0 ] == '\n' ) );
   if( line[ 0 ] == '#' )
@@ -57,8 +57,8 @@ readHeader( FILE* file )
             line[ 0 ] != '\n' );
   else fseek( file, -1, SEEK_CUR );

   fscanf( file, "%d %d\n", &this->width, &this->height );
   fscanf( file, "%d\n", &this->maxColors );
   fscanf( this->file, "%d %d\n", &this->width, &this->height );
   fscanf( this->file, "%d\n", &this->maxColors );
   return true;   
}

@@ -74,7 +74,7 @@ openForRead( const tnlString& fileName )
      return false;
   }
   this->fileOpen = true;
   if( ! readHeader( this->file ) )
   if( ! readHeader() )
      return false;
   return true;
}
@@ -109,6 +109,70 @@ read( const tnlRegionOfInterest< Index > roi,
   return true;
}

template< typename Index >
   template< typename Real,
             typename Device >
bool
tnlPGMImage< Index >::
writeHeader( const tnlGrid< 2, Real, Device, Index >& grid,
             bool binary )
{
   if( binary )
      fprintf( file, "P5 \n" );
   else
      fprintf( file, "P2 \n" );
   fprintf( file, "\n# This file was generated by TNL (tnl-image-converter) \n\n" );
   fprintf( file, "%d %d 255 \n", grid.getDimensions().x(), grid.getDimensions().y() );
   return true;   
}

template< typename Index >
   template< typename Real,
             typename Device >
bool
tnlPGMImage< Index >::
openForWrite( const tnlString& fileName,
              tnlGrid< 2, Real, Device, Index >& grid,
              bool binary )
{
   this->file = fopen( fileName.getString(), "w" );
   if( ! this->file )
   {
      cerr << "Unable to open the file " << fileName << endl;
      return false;
   }
   this->fileOpen = true;
   if( ! writeHeader( grid, binary ) )
      return false;
   return true;
}

template< typename Index >
   template< typename Real,
             typename Device,
             typename Vector >
bool
tnlPGMImage< Index >::
write( const tnlGrid< 2, Real, Device, Index >& grid,
       Vector& vector )
{
   typedef tnlGrid< 2, Real, Device, Index > GridType;
   typedef typename GridType::CoordinatesType CoordinatesType;
   
   Index i, j;
   for( i = 0; i < grid.getDimensions().y(); i ++ )
      for( j = 0; j < grid.getDimensions().x(); j ++ )
      {
         Index cellIndex = grid.getCellIndex( CoordinatesType( j,
                                              grid.getDimensions().y() - 1 - i ) );

         int color = 255 * vector.getElement( cellIndex );
         if( this->binary ) putc( this->file, color );
         else fprintf( this->file, "%d ", &color );
      }
   return true;
}

template< typename Index >
void
tnlPGMImage< Index >::
+45 −24
Original line number Diff line number Diff line
@@ -25,7 +25,9 @@
void configSetup( tnlConfigDescription& config )
{
   config.addDelimiter( "General parameters" );
   config.addRequiredList < tnlString >( "input-images",  "Input files with images." );
   config.addList < tnlString >( "input-images",  "Input images for conversion to .tnl files." );
   config.addList < tnlString >( "input-files",   "Input .tnl files for conversion to images." );
   config.addEntry        < tnlString >( "image-format",  "Output images file format.", "png" );   
   config.addEntry        < tnlString >( "mesh-file",     "Mesh file.", "mesh.tnl" );
   config.addEntry        < bool >     ( "one-mesh-file", "Generate only one mesh file. All the images dimensions must be the same.", true );
   config.addEntry        < int >      ( "roi-top",       "Top (smaller number) line of the region of interest.", -1 );
@@ -35,16 +37,6 @@ void configSetup( tnlConfigDescription& config )
   config.addEntry        < bool >      ( "verbose",       "Set the verbosity of the program.", true );
}

template< typename Index >
bool resolveRoi( const tnlParameterContainer& parameters,
                 const tnlImage< Index >* image,
                 int& top,
                 int& bottom,
                 int& right,
                 int& left )
{
}

template< typename Index,
          typename Grid >
bool setGrid( const tnlRegionOfInterest< Index >& roi,
@@ -66,21 +58,10 @@ bool setGrid( const tnlRegionOfInterest< Index >& roi,
    return true;
}

template< typename Image,
          typename Grid,
          typename Vector >
bool readImage( const Image& image,
                const Grid& grid,
                Vector& vector )
{
    
}

bool processImages( const tnlParameterContainer& parameters )
{
    const tnlList< tnlString >& inputImages = parameters.getParameter< tnlList< tnlString > >( "input-images" );
    tnlString meshFile = parameters.getParameter< tnlString >( "mesh-file" );    
    
    bool verbose = parameters.getParameter< bool >( "verbose" );
    
    tnlGrid< 2, double, tnlHost, int > grid;
@@ -117,6 +98,37 @@ bool processImages( const tnlParameterContainer& parameters )
    }
}

bool processTNLFiles( const tnlParameterContainer& parameters )
{
   const tnlList< tnlString >& inputFiles = parameters.getParameter< tnlList< tnlString > >( "input-files" );
   const tnlString& imageFormat = parameters.getParameter< tnlString >( "image-format" );
   tnlString meshFile = parameters.getParameter< tnlString >( "mesh-file" );    
   bool verbose = parameters.getParameter< bool >( "verbose" );
    
   tnlGrid< 2, double, tnlHost, int > grid;
   if( ! grid.load( meshFile ) )
   {
      cerr << "I am not able to load the mesh file " << meshFile << "." << endl;
      return false;
   }
   tnlVector< double, tnlHost, int > vector;
   for( int i = 0; i < inputFiles.getSize(); i++ )
   {
      const tnlString& fileName = inputFiles[ i ];
      cout << "Processing file " << fileName << "... ";
      if( ! vector.load( fileName ) )
      {
         cerr << "I am not able to load data from a file " << fileName << "." << endl;
         return false;
      }
      if( imageFormat == "pgm" || imageFormat == "pgm-binary" || imageFormat == "pgm-ascii" )
      {
         tnlPGMImage< int > image;
         
      }
   }     
}

int main( int argc, char* argv[] )
{
   tnlParameterContainer parameters;
@@ -124,7 +136,16 @@ int main( int argc, char* argv[] )
   configSetup( conf_desc );
   if( ! parseCommandLine( argc, argv, conf_desc, parameters ) )
      return EXIT_FAILURE;
   if( ! processImages( parameters ) )
   if( ! parameters.checkParameter( "input-images" ) &&
       ! parameters.checkParameter( "input-files") )
   {
       cerr << "Neither input images nor input .tnl files are given." << endl;
       return EXIT_FAILURE;
   }
   if( parameters.checkParameter( "input-images" ) && ! processImages( parameters ) )
      return EXIT_FAILURE;
   if( parameters.checkParameter( "input-files" ) && ! processTNLFiles( parameters ) )
      return EXIT_FAILURE;

   return EXIT_SUCCESS;
}
 No newline at end of file