MantisBT - ITK
View Issue Details
0006159ITKpublic2007-12-13 13:252007-12-14 10:58
sebastienfricker 
Bill Lorensen 
normalminoralways
closedfixed 
 
 
0006159: itkTIFFImageIO does not close opened file
It is not possible to overwrite a TIFF file in-place, ie read a file at the beginning of a pipeline and write to the same file at the end.
The reason is that the file reader never closes the file it reads.

In addition, when the writer fails to write, it does so silently.
See the following code in Itktiffimageio.cxx, in the
TIFFImageIO::InternalWrite function:

 TIFF *tif = TIFFOpen(m_FileName.c_str(), "w");
 if ( !tif )
   {
   itkDebugMacro( << "Returning" );
   return;
   }
Example code:

//DEFINE IMAGE TYPES

const unsigned int Dimension = 2;

typedef unsigned short PixelType;

typedef double InternalPixelType;

typedef itk::Image< PixelType, Dimension > ImageType;

typedef itk::Image< InternalPixelType, Dimension > InternalImageType;

            

//READ INPUT IMAGE

typedef itk::ImageFileReader< ImageType > ImageReaderType;

ImageReaderType::Pointer inputImageReader = ImageReaderType::New();

      

inputImageReader->SetFileName( fileName );

inputImageReader->Update();

ImageType::Pointer inputImage = inputImageReader->GetOutput();

 

 

//RESAMPLE IMAGE

//Define filter, create one instance

typedef itk::ResampleImageFilter< InternalImageType, InternalImageType > ResampleFilterType;

ResampleFilterType::Pointer resampler = ResampleFilterType::New();

 

resampler->SetInput( inputImage );

      

//MORE CODE HERE TO SET RESAMPLER PARAMETERS …

      

 

//CAST IMAGE TO OUTPUT TYPE

typedef itk::CastImageFilter<

                             InternalImageType,

                             ImageType > CastFilterType;

CastFilterType::Pointer caster = CastFilterType::New();

 

caster->SetInput( resampler->GetOutput() );

 

//WRITE OUTPUT FILE

typedef itk::ImageFileWriter< ImageType > WriterType;

WriterType::Pointer writer = WriterType::New();

      

writer->SetFileName( fileName );

writer->SetInput( caster->GetOutput() );

writer->Update();

 
No tags attached.
Issue History
2007-12-13 13:25sebastienfrickerNew Issue
2007-12-13 13:34Bill LorensenStatusnew => assigned
2007-12-13 13:34Bill LorensenAssigned To => Bill Lorensen
2007-12-13 13:36Bill LorensenNote Added: 0009868
2007-12-13 13:36Bill LorensenStatusassigned => acknowledged
2007-12-13 15:09Bill LorensenStatusacknowledged => resolved
2007-12-13 15:09Bill LorensenResolutionopen => fixed
2007-12-13 15:09Bill LorensenNote Added: 0009869
2007-12-14 10:58Bill LorensenStatusresolved => closed
2007-12-14 10:58Bill LorensenNote Added: 0009890

Notes
(0009868)
Bill Lorensen   
2007-12-13 13:36   
I ran itkTIFFImageIOTest with a tiff file specified as the input and output. The program reported a cryptic error:
TIFFOpen: garf.tif: Cannot open.
(0009869)
Bill Lorensen   
2007-12-13 15:09   
Placed m_InternalImage->Clean() after reads. This close the file and does some internal cleanup. Also replaced a DebugMacro with an exception if a file cannot be opend for writing.

See:
http://www.itk.org/cgi-bin/viewcvs.cgi/Code/IO/itkTIFFImageIO.cxx?rev=1.56&root=Insight&sortby=date&view=log [^]
(0009890)
Bill Lorensen   
2007-12-14 10:58   
Reporter confirmed a positive result of the bug fix.

rom Sébastien Fricker <sebastien.fricker@phaseview.net>
to Bill Lorensen <bill.lorensen@gmail.com>,
cc Xabier Artaechevarria Artieda <xabiarta@unav.es>,
insight-users@itk.org,
date Dec 14, 2007 10:50 AM
subject RE: [Insight-users] Overwriting TIFF files
    
hide details 10:50 AM (7 minutes ago)
    
    
    
Reply
    
    

Bill,

I tested it and it works perfectly now, so you can close the bug issue.

Thanks a lot!

 

Sebastien