GDCM: TestByteSwap.cxx

GDCM 3.2.10

TestByteSwap.cxx

This is a C++ example on how to use gdcm::ByteSwap

/*=========================================================================
Program: GDCM (Grassroots DICOM). A DICOM library
Copyright (c) 2006-2011 Mathieu Malaterre
All rights reserved.
See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "gdcmTypes.h"
#include "gdcmSwapCode.h"
#include "gdcmByteSwap.h"
#include <cstring> // memcpy
int myfunc()
{
char vl_str[4];
const char raw[] = "\000\000\000\004";
memcpy(vl_str, raw, 4);
uint32_t vl;
memcpy(&vl, vl_str, 4);
gdcm::ByteSwap<uint32_t>::SwapRangeFromSwapCodeIntoSystem(&vl, gdcm::SwapCode::BigEndian, 1);
if( vl != 0x00000004 )
{
std::cerr << std::hex << "vl: " << vl << std::endl;
return 1;
}
gdcm::ByteSwap<uint32_t>::SwapFromSwapCodeIntoSystem(vl, gdcm::SwapCode::LittleEndian);
if( vl != 0x00000004 )
{
std::cerr << std::hex << "vl: " << vl << std::endl;
return 1;
}
gdcm::ByteSwap<uint32_t>::SwapFromSwapCodeIntoSystem(vl, gdcm::SwapCode::BigEndian);
if( vl != 0x4000000 )
{
std::cerr << std::hex << "vl: " << vl << std::endl;
return 1;
}
return 0;
}
int TestByteSwap(int , char *[])
{
gdcm::SwapCode sc = gdcm::SwapCode::Unknown;
if ( gdcm::ByteSwap<uint16_t>::SystemIsBigEndian() )
{
sc = gdcm::SwapCode::BigEndian;
}
else if ( gdcm::ByteSwap<uint16_t>::SystemIsLittleEndian() )
{
sc = gdcm::SwapCode::LittleEndian;
}
if( sc == gdcm::SwapCode::Unknown )
{
std::cerr << "unk" << std::endl;
return 1;
}
//std::cout << "sc: " << sc << std::endl;
uint16_t t = 0x1234;
gdcm::ByteSwap<uint16_t>::SwapFromSwapCodeIntoSystem(t, sc);
if( sc == gdcm::SwapCode::BigEndian )
{
if( t != 0x3412 )
{
std::cerr << std::hex << "t: " << t << std::endl;
return 1;
}
// ok test pass rest value to old one
t = 0x1234;
}
else if ( sc == gdcm::SwapCode::LittleEndian )
{
if( t != 0x1234 )
{
std::cerr << std::hex << "t: " << t << std::endl;
return 1;
}
}
union { char n[2]; uint16_t tn; } u16;
memcpy(u16.n, &t, 2 );
gdcm::ByteSwap<uint16_t>::SwapRangeFromSwapCodeIntoSystem(&u16.tn, sc, 1);
uint16_t tn = u16.tn;
if( sc == gdcm::SwapCode::BigEndian )
{
if( tn != 0x3412 )
{
std::cerr << std::hex << "tn: " << tn << std::endl;
return 1;
}
// ok test pass rest value to old one
t = 0x1234;
}
else if ( sc == gdcm::SwapCode::LittleEndian )
{
if( tn != 0x1234 )
{
std::cerr << std::hex << "tn: " << tn << std::endl;
return 1;
}
}
gdcm::ByteSwap<uint16_t>::SwapRangeFromSwapCodeIntoSystem(&u16.tn, gdcm::SwapCode::BigEndian, 1);
tn = u16.tn;
if( sc == gdcm::SwapCode::LittleEndian )
{
if( tn != 0x3412 )
{
std::cerr << std::hex << "tn: " << tn << std::endl;
return 1;
}
}
else if ( sc == gdcm::SwapCode::BigEndian )
{
if( tn != 0x1234 )
{
std::cerr << std::hex << "tn: " << tn << std::endl;
return 1;
}
}
if( myfunc() )
{
return 1;
}
uint16_t array[] = { 0x1234 };
gdcm::ByteSwap<uint16_t>::SwapRangeFromSwapCodeIntoSystem(array,
gdcm::SwapCode::BigEndian,1);
if ( array[0] != 0x3412 )
{
std::cerr << std::hex << "array: " << array[0] << std::endl;
return 1;
}
return 0;
}
gdcm::ByteSwap::SystemIsBigEndian
static bool SystemIsBigEndian()
gdcm::ByteSwap::SystemIsLittleEndian
static bool SystemIsLittleEndian()
gdcm::ByteSwap::SwapRangeFromSwapCodeIntoSystem
static void SwapRangeFromSwapCodeIntoSystem(T *p, SwapCode const &sc, std::streamoff num)
gdcm::ByteSwap::SwapFromSwapCodeIntoSystem
static void SwapFromSwapCodeIntoSystem(T &p, SwapCode const &sc)
gdcm::SwapCode
SwapCode representation.
Definition gdcmSwapCode.h:27
gdcm::SwapCode::LittleEndian
@ LittleEndian
Definition gdcmSwapCode.h:31
gdcm::SwapCode::BigEndian
@ BigEndian
Definition gdcmSwapCode.h:32
gdcm::SwapCode::Unknown
@ Unknown
Definition gdcmSwapCode.h:30
gdcmByteSwap.h
gdcmSwapCode.h
gdcmTypes.h