Commit 260e337e authored by Ondrej's avatar Ondrej
Browse files

Fixing #1 - tnl-dicom-reader, tnl-image-converter

parent cfcd049e
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -104,7 +104,7 @@ getImage( const int imageIdx,
            Index cellIndex = grid.getCellIndex( CoordinatesType( j - roi.getLeft(),
                                                                  roi.getBottom() - 1 - i ) );
            Uint16 col = imageData[ position ];
            vector.setElement( cellIndex, ( Real ) col / ( Real ) this->getMaxColorValue() );
            vector.setElement( cellIndex, ( Real ) col / ( Real ) 65535 );
            cout << vector.getElement( cellIndex ) << " ";
         }
         position++;
+2 −1
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@
#include <core/tnlString.h>
#include <core/images/tnlImage.h>
#include <core/images/tnlRegionOfInterest.h>
#include <fstream>

template< typename Index = int >
class tnlPGMImage : public tnlImage< Index >
@@ -70,7 +71,7 @@ class tnlPGMImage : public tnlImage< Index >
         
         IndexType maxColors;
         
         FILE* file;
         fstream file;
         
         bool fileOpen;
};
+48 −28
Original line number Diff line number Diff line
@@ -34,8 +34,8 @@ tnlPGMImage< Index >::
readHeader()
{
   char magicNumber[ 3 ];
   magicNumber[ 2 ] = 0;
   if( fread( magicNumber, sizeof( char ), 2, this->file ) != 2 )
   this -> file >> magicNumber;
   if( this -> file.fail() )
   {
      cerr << "Unable to read the magic number." << endl;
      return false;
@@ -48,17 +48,18 @@ readHeader()
   if( strcmp( magicNumber, "P5" ) == 0 )
      this->binary = true;

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

   fscanf( this->file, "%d %d\n", &this->width, &this->height );
   fscanf( this->file, "%d\n", &this->maxColors );
   char character;
   this -> file.get(character);
   while ( ! this -> file.eof() and ( character == ' ' || character == '\t' || character == '\r' || character == '\n') )
   {
	this -> file.get(character);
	if ( character == '#' )
		while (! this -> file.eof() && ( character != '\n' ) )
			this -> file.get( character );
   }
   this -> file.unget();
   
   this -> file >> this -> width >> this -> height >> this -> maxColors;
   return true;   
}

@@ -68,7 +69,10 @@ tnlPGMImage< Index >::
openForRead( const tnlString& fileName )
{
   this->close();
   this->file = fopen( fileName.getString(), "r" );
   if ( this -> binary )
   	this->file.open( fileName.getString(), fstream::in | fstream::binary);
   else 
	this->file.open( fileName.getString(), fstream::in );
   if( ! this->file )
   {
      cerr << "Unable to open the file " << fileName << endl;
@@ -98,8 +102,13 @@ read( const tnlRegionOfInterest< Index > roi,
      for( j = 0; j < this->width; j ++ )
      {
         int col;
         if( this->binary ) col = getc( this->file );
         else fscanf( this->file, "%d", &col );
	 unsigned char col_aux;
         if( this->binary ) 
	 {
		this -> file >> col_aux;
		col = (int)col_aux;
	 }
         else this -> file >> col;
         if( roi.isIn( i, j ) )
         {
            Index cellIndex = grid.getCellIndex( CoordinatesType( j - roi.getLeft(),
@@ -119,11 +128,11 @@ writeHeader( const tnlGrid< 2, Real, Device, Index >& grid,
             bool binary )
{
   if( binary )
      fprintf( file, "P5 \n" );
      this->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() );
      this->file << "P2\n";
   this->file << "# This file was generated by TNL (tnl-image-converter)\n";
   this->file << grid.getDimensions().x() << ' '<< grid.getDimensions().y() << '\n' << "255\n";
   return true;   
}

@@ -137,8 +146,11 @@ openForWrite( const tnlString& fileName,
              bool binary )
{
   this->close();
   this->file = fopen( fileName.getString(), "w" );
   if( ! this->file )
   if( binary )
        this->file.open( fileName.getString(), fstream::out | fstream::binary);
   else 
        this->file.open( fileName.getString(), fstream::out);
   if( this->file.fail() )
   {
      cerr << "Unable to open the file " << fileName << endl;
      return false;
@@ -164,15 +176,23 @@ write( const tnlGrid< 2, Real, Device, Index >& grid,
   
   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 ) );
         unsigned char color = 255 * vector.getElement( cellIndex );
         if ( ! this -> binary )
	 {
	     int color_aux = (int)color;
	     this->file << color_aux;
             this->file << ' ';
	 }
	 else this->file << color;
      }
      
         char color = 255 * vector.getElement( cellIndex );
         //cout << color << " " << endl;
         if( this->binary ) putc( color, this->file );
         else fprintf( this->file, "%d ", &color );
      if ( ! this -> binary )
        this->file << '\n';
   }
   return true;
}
@@ -183,7 +203,7 @@ tnlPGMImage< Index >::
close()
{
   if( this->fileOpen )
      fclose( file );
      this->file.close();
   this->fileOpen = false;
}

+5 −2
Original line number Diff line number Diff line
@@ -163,7 +163,10 @@ bool processTNLFiles( const tnlParameterContainer& parameters )
         tnlString outputFileName( fileName );
         RemoveFileExtension( outputFileName );
         outputFileName += ".pgm";
	 if ( imageFormat == "pgm" || imageFormat == "pgm-binary")
         	image.openForWrite( outputFileName, grid, true );
	 if ( imageFormat == "pgm-ascii" )
         	image.openForWrite( outputFileName, grid, false );
         image.write( grid, vector );
         image.close();
         continue;