[Insight-developers] ByteSwapper seems strange

Miller, James V (Research) millerjv@crd.ge.com
Mon, 3 Mar 2003 12:05:54 -0500


The compiler will automatically create a version of ByteSwapper
where the SwapFromSystemToBigEndian() method is either a no-op
or actually performs a swap.

Your code only needs to check the endianness of the file and 
it can blindly call the appropriate swap method.  For instance, 
in the RawImageIO class, the code that reads the file looks 
like (where m_ByteOrder is the byte order in the file)

  // Swap bytes if necessary
  if ( m_ByteOrder == LittleEndian )
    {
    ByteSwapperType::SwapRangeFromSystemToLittleEndian(
                    (ComponentType *)buffer, this->GetImageSizeInComponents() );
    }
  else if ( m_ByteOrder == BigEndian )
    {
    ByteSwapperType::SwapRangeFromSystemToBigEndian(
                    (ComponentType *)buffer, this->GetImageSizeInComponents() );
    }




> -----Original Message-----
> From: Kent Williams [mailto:kent@mail.psychiatry.uiowa.edu]
> Sent: Monday, March 03, 2003 11:46 AM
> To: Insight Developers List
> Subject: [Insight-developers] ByteSwapper seems strange
> 
> 
> I have noticed some annoying traits of the itk::ByteSwapper 
> template class, 
> and wonder if there's a rationale at work here that escapes me.
> 
> The essence of the class comes down to two sorts of methods:
> 
> 1. SystemIsBigendian and SystemIsLittleEndian
> 2. SwapFromSystemToBigEndian and SwapFromSystemToLittleEndian
> 
> When you read a file that can be either Little- or Big-Endian 
> you have to 
> decide whether or not the current computer is Little- or 
> Big-Endian.  Then, 
> if the file has the opposite Endian-ness from the current 
> system, you want to 
> perform the necessary byte swapping.
> 
> But since you don't have SwapFromBigEndianToSystem or 
> SwapFromLittleEndianToSystem, you have to write this 
> wrong-looking code:
> 
> if ( ByteSwapper<int>::SystemIsBigEndian() && FileIsLittleEndian )
> 	{
> 	ByteSwapper<T>SwapFromSystemToLittleEndian(PointerToTypeT);
> 	}
> else if ( ByteSwapper<int>::SystemIsLittleEndian() && 
> FileIsBigEndian )
> 	{
> 	ByteSwapper<T>SwapFromSystemToBigEndian(PointerToTypeT);
> 	}
> 
> which seems a bit absurd.
> 
> What I've always done in the past is more like this:
> 
> enum Endian { LittleEndian, BigEndian };
> 
> Endian FileEndianNess = GetFileEndianNess(f);
> Endian SystemEndianNess = BytesSwapper<int>::GetSystemEndianNess();
> 
> if ( FileEndianNess != SystemEndianNess )
> 	ByteSwapper<T>Swap(PointerToTypeT);
> 
> Now I realize that changing the class completely at this 
> point would require 
> some careful editing of multiple files and extensive testing, 
> something no 
> one is presumably contracted to do.  I only noticed that the 
> ByteSwapper 
> seemed obtuse when I discovered that the Analyze FileIO seems 
> to get things 
> exactly backwards, and yet seems to do the correct thing.
> 
> Has anyone noticed this before? Does someone with more brains 
> for Software 
> Engineering than I (and I'm not being sarcastic!) want to 
> suggest a way out?
> 
> 
> _______________________________________________
> Insight-developers mailing list
> Insight-developers@public.kitware.com
> http://public.kitware.com/mailman/listinfo/insight-developers
>