[Insight-users] writing large files with VTKImageIO...possible bug?

Luis Ibanez luis.ibanez at kitware.com
Tue Oct 20 18:21:02 EDT 2009


Hi Lyubomir,

Thanks for pointing this out.

You are correct in that a duplicate of the image buffer
is created here, and that its presence may be an issue
for large images.

However, your suggested solution raise the concern
that the ByteSwapper will be modifying the pixel buffer
of the image that we are writing to disk.

This is in general a violation of the data pipeline behavior
of managing the output of a filter as a const object.

It becomes particularly important if you had a pipeline
such as:

  writer->SetInput( filterA->GetOutput() );
  filterB->SetInput( filterA->GeOutput() );

In this case, the action of the writer will corrupt the
pixel buffer of filterA's image output, and will pass
incorrect information as input to filterB.

...one option for maintaining your suggested solution
could be to revert the potential swapping after writing
the image... but still that requires to assume that no
other filter is accessing that output image of filterA
before we finish unswapping.

One potential option is for you to consider the use
of the MetaImage file format. This format does not
unswap the data, it simply write down the endiannes
in the file. Additionally it provides support for streaming,
which can be useful if you are dealing with very large
images....


    Please let us know if you would like to consider
    these options,


           Thanks


                 Luis


-----------------------------------------------------------------------
2009/10/20 Lyubomir G. Zagorchev <Lyubomir.G.Zagorchev at dartmouth.edu>:
>
>
> When writing binary files with VTKImageIO the buffer is being copied into a temporary array which creates memory issues with large files. I am running the CVS version of ITK.... Could you please verify that? A possible fix is pasted below.
>
> Thanks,
>
> Lyubomir
>
>
>
> void VTKImageIO::Write(const void* buffer)
> {
> .....
> .....
>  // Write the actual pixel data
>  if ( m_FileType == ASCII )
>    {
>    this->WriteBufferAsASCII(file, buffer, this->GetComponentType(),
>                             this->GetImageSizeInComponents());
>    }
>  else //binary
>    {
>    int size = this->GetComponentSize();
>    typedef ::size_t   BufferSizeType;
>    const BufferSizeType numbytes = static_cast<BufferSizeType>( this->GetImageSizeInBytes() );
>    //char * tempmemory=new char[numbytes];
>    //memcpy(tempmemory,buffer,numbytes);
>    //switch( size )
>    //  {
>    //  case 2:
>    //    {
>    //    ByteSwapper<short>::SwapRangeFromSystemToBigEndian(reinterpret_cast<short *>(tempmemory), static_cast<BufferSizeType>(this->GetImageSizeInComponents()) );
>    //    }
>    //    break;
>    //  case 4:
>    //    {
>    //    ByteSwapper<float>::SwapRangeFromSystemToBigEndian(reinterpret_cast<float *>(tempmemory), static_cast<BufferSizeType>(this->GetImageSizeInComponents()) );
>    //    }
>    //    break;
>    //  case 8:
>    //    {
>    //    ByteSwapper<double>::SwapRangeFromSystemToBigEndian(reinterpret_cast<double *>(tempmemory), static_cast<BufferSizeType>(this->GetImageSizeInComponents()) );
>    //    }
>    //    break;
>    //  }
>    //        file.write(static_cast<const char*>(tempmemory), static_cast<std::streamsize>(this->GetImageSizeInBytes()));
>    //delete [] tempmemory;
>
>
>    switch( size )
>      {
>      case 2:
>        {
>        ByteSwapper<short>::SwapRangeFromSystemToBigEndian((short *)(buffer), static_cast<BufferSizeType>(this->GetImageSizeInComponents()) );
>        }
>        break;
>      case 4:
>        {
>        ByteSwapper<float>::SwapRangeFromSystemToBigEndian((float *)(buffer), static_cast<BufferSizeType>(this->GetImageSizeInComponents()) );
>        }
>        break;
>      case 8:
>        {
>        ByteSwapper<double>::SwapRangeFromSystemToBigEndian((double *)(buffer), static_cast<BufferSizeType>(this->GetImageSizeInComponents()) );
>        }
>        break;
>      }
>            file.write(static_cast<const char*>(buffer), static_cast<std::streamsize>(this->GetImageSizeInBytes()));
>    }
> }
> _____________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the ITK FAQ at: http://www.itk.org/Wiki/ITK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.itk.org/mailman/listinfo/insight-users
>


More information about the Insight-users mailing list