[Insight-users] Is this a bug in itkRawImageIO.txx

Karthik Krishnan karthik.krishnan at kitware.com
Sat Dec 2 18:42:43 EST 2006


Hi Eugene,

Please log a bug report at http://www.itk.org/Bug/ and assign it to me.

Thanks
-karthik

On 11/16/06, Eugene Guo <yguo2006 at gmail.com> wrote:
>
> Hi,
> I just read through the code of class itkRawImageIO defined in
> Insight/Code/IO/itkRawImageIO.txx
> and notice that there maybe a bug which led to my problem when testing
> itkRawImageIO:
>
> the following code is extracted from method itkRawImageIO::Write
>
> /********************************************************/
>
>   // Actually do the writing
>   //
>   if ( m_FileType == ASCII )
>     {
> ...
>
>    }
>   else //binary
>     {
>
>     const unsigned long numberOfBytes      = this->GetImageSizeInBytes();
>     const unsigned long numberOfComponents =
> this->GetImageSizeInComponents();
>
> #define itkWriteRawBytesAfterSwappingMacro(StrongType, WeakType) \
>       ( this->GetComponentType() ==  WeakType ) \
>       { \
>       typedef ByteSwapper< StrongType > InternalByteSwapperType; \
>       if ( m_ByteOrder == LittleEndian ) \
>         { \
>         char * tempBuffer = new char[ numberOfBytes ]; \
>         memcpy( tempBuffer, buffer , numberOfBytes ); \
>         InternalByteSwapperType::SwapRangeFromSystemToLittleEndian( \
>           (StrongType *)tempBuffer, numberOfComponents ); \
>         file.write( tempBuffer, numberOfBytes ); \
>         delete [] tempBuffer; \
>         } \
>       else if ( m_ByteOrder == BigEndian ) \
>         { \
>         char * tempBuffer = new char[ numberOfBytes ]; \
>         memcpy( tempBuffer, buffer , numberOfBytes ); \
>         InternalByteSwapperType::SwapRangeFromSystemToBigEndian( \
>           (StrongType *)tempBuffer, numberOfComponents ); \
>         file.write( tempBuffer, numberOfBytes ); \
>         delete [] tempBuffer; \
>         } \
>       else \
>         { \
>         file.write(static_cast<const char*>(buffer), numberOfBytes ); \
> //*** may not be executed when this->GetComponentType() ==UCHAR
>         } \
>       }
>
>     // Swap bytes if necessary
>     if itkWriteRawBytesAfterSwappingMacro( unsigned short, USHORT )
>     else if itkWriteRawBytesAfterSwappingMacro( short, SHORT )
>     else if itkWriteRawBytesAfterSwappingMacro( unsigned int, UINT )
>     else if itkWriteRawBytesAfterSwappingMacro( int, INT )
>     else if itkWriteRawBytesAfterSwappingMacro( long, LONG )
>     else if itkWriteRawBytesAfterSwappingMacro( unsigned long, ULONG )
>     else if itkWriteRawBytesAfterSwappingMacro( float, FLOAT )
>     else if itkWriteRawBytesAfterSwappingMacro( double, DOUBLE )
>     }
> ....
>
> /****************************************************************/
>
>
>
> Here the third file.write may not be executed at all. The swapping macro
> only work for those data type longer than two bytes. For my case, I was
> trying to write 3D volume in unsigned char (this->GetComponentType() ==
> UCHAR), which is not covered by those swapping macro defined. Therefore the
> file.write was never carried out.
>
> To fix it, I copied the file.write line to the end of *** if
> itkWriteRawBytesAfterSwappingMacro *** statements, which now looks like:
>
> /**************************************************************/
>
> ......
>
>     // Swap bytes if necessary
>     if itkWriteRawBytesAfterSwappingMacro( unsigned short, USHORT )
>     else if itkWriteRawBytesAfterSwappingMacro( short, SHORT )
>     else if itkWriteRawBytesAfterSwappingMacro( unsigned int, UINT )
>     else if itkWriteRawBytesAfterSwappingMacro( int, INT )
>     else if itkWriteRawBytesAfterSwappingMacro( long, LONG )
>     else if itkWriteRawBytesAfterSwappingMacro( unsigned long, ULONG )
>     else if itkWriteRawBytesAfterSwappingMacro( float, FLOAT )
>     else if itkWriteRawBytesAfterSwappingMacro( double, DOUBLE )
>  else file.write(static_cast<const char*>(buffer), numberOfBytes );
> //*******************New line added
>
> ....
>
> /*******************************************************************/
>
> Now my problem seems solved.
>
>  Is this a correct way to do it, or there is another way to solve it
> without touching the source code?
>
> Thanks
>
>
>
> Eugene
>
> _______________________________________________
> Insight-users mailing list
> Insight-users at itk.org
> http://www.itk.org/mailman/listinfo/insight-users
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://public.kitware.com/pipermail/insight-users/attachments/20061202/14e75ec9/attachment.html


More information about the Insight-users mailing list