From: srit Date: Wed, 17 Feb 2010 20:52:29 +0000 (+0000) Subject: XVI IO X-Git-Tag: v1.2.0~809 X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=commitdiff_plain;h=a76308ba9b5add63fcce2632a3683ce64b0702c5;p=clitk.git XVI IO --- diff --git a/cmake/common.cmake b/cmake/common.cmake index d661d56..018ced2 100644 --- a/cmake/common.cmake +++ b/cmake/common.cmake @@ -27,7 +27,7 @@ FOREACH(GGO_FILE ${ALL_GGO_FILES}) #========================================================= #Set a reasonable build mode default if the user hasn't set any if (NOT CMAKE_BUILD_TYPE) - set(CMAKE_BUILD_TYPE RelWithDebInfo) + set(CMAKE_BUILD_TYPE Release) endif (NOT CMAKE_BUILD_TYPE) #========================================================= diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index e5f0698..9f4700e 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -13,12 +13,16 @@ SET(clitkCommon_SRC clitkListOfPair.cxx clitkTimer.cxx clitkImageCommon.cxx + clitkIO.cxx clitkVoxImageIO.cxx clitkVoxImageIOFactory.cxx clitkVfImageIO.cxx clitkVfImageIOFactory.cxx clitkHisImageIO.cxx clitkHisImageIOFactory.cxx + clitkXdrImageIOReader.cxx + clitkXdrImageIOWriter.cxx + clitkXdrImageIOFactory.cxx clitkOrientation.cxx vvImage.cxx clitkImageToImageGenericFilter.cxx diff --git a/common/clitkHisImageIO.cxx b/common/clitkHisImageIO.cxx index 009d282..1ee1995 100755 --- a/common/clitkHisImageIO.cxx +++ b/common/clitkHisImageIO.cxx @@ -81,7 +81,6 @@ void clitk::HisImageIO::ReadImageInformation() { SetDimensions(1, brx-ulx+1); if (nrframes>1) SetDimensions(2, nrframes); - file.close(); } //// //-------------------------------------------------------------------- @@ -97,7 +96,6 @@ bool clitk::HisImageIO::CanReadFile(const char* FileNameToRead) //-------------------------------------------------------------------- // Read Image Content void clitk::HisImageIO::Read(void * buffer) { - // open file std::ifstream file(m_FileName.c_str(), std::ios::in | std::ios::binary); if ( file.fail() ) @@ -117,10 +115,18 @@ void clitk::HisImageIO::Read(void * buffer) { << file.gcount() << " bytes. The current state is: " << file.rdstate()); } + +//-------------------------------------------------------------------- +bool clitk::HisImageIO::CanWriteFile(const char* FileNameToWrite) +{ std::string filename(FileNameToWrite); + std::string filenameext = GetExtension(filename); + if (filenameext != std::string("his")) return false; + return true; +} //-------------------------------------------------------------------- -// Write Image Information -void clitk::HisImageIO::WriteImageInformation(bool keepOfStream) +// Write Image +void clitk::HisImageIO::Write(const void* buffer) { std::ofstream file(m_FileName.c_str(), std::ios::out | std::ios::binary); if ( file.fail() ) @@ -168,28 +174,8 @@ void clitk::HisImageIO::WriteImageInformation(bool keepOfStream) itkExceptionMacro(<< "Unsupported field type"); break; } + file.write(szHeader, m_HeaderSize); - file.close(); -} - -//-------------------------------------------------------------------- -// Write Image Information -bool clitk::HisImageIO::CanWriteFile(const char* FileNameToWrite) -{ - std::string filename(FileNameToWrite); - std::string filenameext = GetExtension(filename); - if (filenameext != std::string("his")) return false; - return true; -} - -//-------------------------------------------------------------------- -// Write Image -void clitk::HisImageIO::Write(const void * buffer) -{ - std::ofstream file(m_FileName.c_str(), std::ios::out | std::ios::binary | std::ios::ate); - if ( file.fail() ) - itkGenericExceptionMacro(<< "Could not open file (for writing): " << m_FileName); - file.write((const char *)buffer, GetImageSizeInBytes()); file.close(); } //// diff --git a/common/clitkHisImageIO.h b/common/clitkHisImageIO.h index dba5ae3..7ddb691 100755 --- a/common/clitkHisImageIO.h +++ b/common/clitkHisImageIO.h @@ -41,7 +41,7 @@ namespace clitk { virtual void Read(void * buffer); /*-------- This part of the interfaces deals with writing data. ----- */ - virtual void WriteImageInformation(bool keepOfStream); + virtual void WriteImageInformation(bool keepOfStream) { ; } virtual void WriteImageInformation() { WriteImageInformation(false); } virtual bool CanWriteFile(const char* filename); virtual void Write(const void* buffer); diff --git a/common/clitkIO.h b/common/clitkIO.h index 268d064..39b63ad 100644 --- a/common/clitkIO.h +++ b/common/clitkIO.h @@ -11,27 +11,13 @@ -------------------------------------------------------------------*/ -// std include -#include -#include - -// clitk include -#include "clitkCommon.h" -#include "clitkImageCommon.h" -#include "clitkVoxImageIO.h" -#include "clitkVoxImageIOFactory.h" -#include "clitkHisImageIO.h" -#include "clitkHisImageIOFactory.h" -#include "clitkVfImageIO.h" -#include "clitkVfImageIOFactory.h" - //-------------------------------------------------------------------- // CLITK_INIT -#define CLITK_INIT \ - itk::ImageIOFactory::RegisterBuiltInFactories(); \ - clitk::VoxImageIOFactory::RegisterOneFactory(); \ - clitk::VfImageIOFactory::RegisterOneFactory(); \ - clitk::HisImageIOFactory::RegisterOneFactory(); +#define CLITK_INIT clitk::RegisterClitkFactories(); + +namespace clitk { + void RegisterClitkFactories(); +} #endif /* end #define CLITKIO_H */ diff --git a/vv/nkitkXDRImageIO.h b/common/clitkXdrImageIO.h similarity index 51% rename from vv/nkitkXDRImageIO.h rename to common/clitkXdrImageIO.h index 6d7279b..69b4953 100644 --- a/vv/nkitkXDRImageIO.h +++ b/common/clitkXdrImageIO.h @@ -1,8 +1,8 @@ -#ifndef NKITKXDRIMAGEIO_H -#define NKITKXDRIMAGEIO_H +#ifndef CLITKXDRIMAGEIO_H +#define CLITKXDRIMAGEIO_H /** - * @file nkitkXDRImageIO.h + * @file clitkXdrImageIO.h * @author Simon Rit * @date Sun Jun 1 22:07:45 2008 * @@ -14,21 +14,24 @@ // itk include #include "itkImageIOBase.h" -namespace nkitk { +#define AVSerror(v) itkGenericExceptionMacro(<< "Error in clitk::XdrImageIO. Message:" << v); +#define AVSwarning(v) itkWarningMacro(<< "Error in clitk::XdrImageIO. Message:" << v); + +namespace clitk { //==================================================================== -// Class for reading XDR Image file format -class XDRImageIO: public itk::ImageIOBase +// Class for reading xdr Image file format +class XdrImageIO: public itk::ImageIOBase { public: /** Standard class typedefs. */ - typedef XDRImageIO Self; + typedef XdrImageIO Self; typedef itk::ImageIOBase Superclass; typedef itk::SmartPointer Pointer; typedef signed short int PixelType; typedef enum {UNIFORM,RECTILINEAR,IRREGULAR} AVSType; - XDRImageIO():Superclass() { + XdrImageIO():Superclass() { forcenoswap = 0; } @@ -36,7 +39,7 @@ public: itkNewMacro(Self); /** Run-time type information (and related methods). */ - itkTypeMacro(XDRImageIO, ImageIOBase); + itkTypeMacro(XdrImageIO, ImageIOBase); /*-------- This part of the interface deals with reading data. ------ */ virtual int ReadImageInformationWithError(); @@ -46,30 +49,25 @@ public: virtual bool CanReadFile( const char* FileNameToRead ); /*-------- This part of the interfaces deals with writing data. ----- */ - virtual void WriteImageInformation(bool keepOfStream) { - ; - } - virtual void WriteImageInformation() { - WriteImageInformation(false); - } - virtual bool CanWriteFile(const char* filename) { - return false; - } - virtual void Write(const void* buffer) { - ; - } + virtual void WriteImageInformation(bool keepOfStream) {;} + virtual void WriteImageInformation() { WriteImageInformation(false); } + virtual bool CanWriteFile(const char* FileNameToWrite); + virtual void Write(const void* buffer); protected: void ITKError(std::string funcName, int msgID); + void WriteImage(const char* file, char* headerinfo, char* headerfile, int raw, + int offset, char bLittleEndian, int iNkiCompression, + int wcoords, int append, int getsize, char *tobuffer, const void* data); int m_HeaderSize; int forcenoswap; -}; // end class XDRImageIO +}; // end class XdrImageIO } // end namespace // explicit template instantiation -template class itk::CreateObjectFunction; +template class itk::CreateObjectFunction; -#endif /* end #define NKITKXDRIMAGEIO_H */ +#endif /* end #define CLITKXDRIMAGEIO_H */ diff --git a/common/clitkXdrImageIOFactory.cxx b/common/clitkXdrImageIOFactory.cxx new file mode 100644 index 0000000..cbf7da8 --- /dev/null +++ b/common/clitkXdrImageIOFactory.cxx @@ -0,0 +1,28 @@ +#ifndef CLITKXDRIMAGEIOFACTORY_CXX +#define CLITKXDRIMAGEIOFACTORY_CXX + +/** + * @file clitkXdrImageIOFactory.cxx + * @author Simon Rit + * @date Sun Jun 1 22:11:20 2008 + * + * @brief + * + * + */ + +#include "clitkXdrImageIOFactory.h" + +//==================================================================== +clitk::XdrImageIOFactory::XdrImageIOFactory() +{ + this->RegisterOverride("itkImageIOBase", + "XdrImageIO", + "Xdr Image IO", + 1, + itk::CreateObjectFunction::New()); +} + + +#endif /* end #define CLITKXDRIMAGEIOFACTORY_CXX */ + diff --git a/vv/nkitkXDRImageIOFactory.h b/common/clitkXdrImageIOFactory.h similarity index 66% rename from vv/nkitkXDRImageIOFactory.h rename to common/clitkXdrImageIOFactory.h index 1c135b4..d8d226a 100644 --- a/vv/nkitkXDRImageIOFactory.h +++ b/common/clitkXdrImageIOFactory.h @@ -1,8 +1,8 @@ -#ifndef NKITKXDRIMAGEIOFACTORY_H -#define NKITKXDRIMAGEIOFACTORY_H +#ifndef CLITKXDRIMAGEIOFACTORY_H +#define CLITKXDRIMAGEIOFACTORY_H /** - * @file nkitkXDRImageIOFactory.h + * @file clitkXdrImageIOFactory.h * @author Simon Rit * @date Sun Jun 1 22:09:56 2008 * @@ -11,23 +11,23 @@ * */ -// nkitk include -#include "nkitkXDRImageIO.h" +// clitk include +#include "clitkXdrImageIO.h" // itk include #include "itkImageIOBase.h" #include "itkObjectFactoryBase.h" #include "itkVersion.h" -namespace nkitk { +namespace clitk { //==================================================================== // Factory for reading XDR Image file format -class XDRImageIOFactory: public itk::ObjectFactoryBase +class XdrImageIOFactory: public itk::ObjectFactoryBase { public: /** Standard class typedefs. */ - typedef XDRImageIOFactory Self; + typedef XdrImageIOFactory Self; typedef itk::ObjectFactoryBase Superclass; typedef itk::SmartPointer Pointer; typedef itk::SmartPointer ConstPointer; @@ -38,14 +38,14 @@ public: } const char* GetDescription(void) const { - return "XDR ImageIO Factory, allows the loading of XDR images into insight"; + return "Xdr ImageIO Factory, allows the loading of Xdr images into insight"; } /** Method for class instantiation. */ itkFactorylessNewMacro(Self); /** Run-time type information (and related methods). */ - itkTypeMacro(XDRImageIOFactory, ObjectFactoryBase); + itkTypeMacro(XdrImageIOFactory, ObjectFactoryBase); /** Register one factory of this type */ static void RegisterOneFactory(void) { @@ -53,17 +53,17 @@ public: } protected: - XDRImageIOFactory(); - ~XDRImageIOFactory() {}; - typedef XDRImageIOFactory myProductType; + XdrImageIOFactory(); + ~XdrImageIOFactory() {}; + typedef XdrImageIOFactory myProductType; const myProductType* m_MyProduct; private: - XDRImageIOFactory(const Self&); //purposely not implemented + XdrImageIOFactory(const Self&); //purposely not implemented void operator=(const Self&); //purposely not implemented }; } // end namespace -#endif /* end #define NKITKXDRIMAGEIOFACTORY_H */ +#endif /* end #define CLITKXDRIMAGEIOFACTORY_H */ diff --git a/vv/nkitkXDRImageIOReader.cxx b/common/clitkXdrImageIOReader.cxx similarity index 67% rename from vv/nkitkXDRImageIOReader.cxx rename to common/clitkXdrImageIOReader.cxx index f24ab5a..1b47fea 100644 --- a/vv/nkitkXDRImageIOReader.cxx +++ b/common/clitkXdrImageIOReader.cxx @@ -1,8 +1,5 @@ -#ifndef NKITKXDRIMAGEIO_CXX -#define NKITKXDRIMAGEIO_CXX - /** - * @file nkitkXDRImageIO.cxx + * @file clitkXdrImageIO.cxx * @author Simon Rit * @date Sun Jun 1 22:12:20 2008 * @@ -11,7 +8,7 @@ * */ -#include "nkitkXDRImageIO.h" +#include "clitkXdrImageIO.h" // std include #include @@ -20,7 +17,6 @@ //defines #define MAXDIM 5 -#define AVSerror(v) std::cerr << "Error in nkitk::XDRImageIO. Message:" << v << std::endl; #ifdef _WIN32 #ifdef memicmp #undef memicmp @@ -87,55 +83,11 @@ When Who What 20080414 lsp+mgw __sun__ doesn't know */ -/************************************************************************/ -/* MODULE DOCUMENTATION */ -/************************************************************************/ -/* - READ_XDR Read XDR file (may be compressed) into field - name output field handle - file_expression name of file - numerical_expression start XDR data in file offset (default 0) - NOTE: compressed XDR data may not be part of a larger file - - READ_XDR_HEADER Get entry from xdr header - %name Output = string block - file_expression file name default extension '' - %string_expression name of element to load - - READ_XDR_PREVIEW Read and downsize XDR file (for bitmap) - name output field handle - file_expression name of file - numerical_expression start XDR data in file offset (default 0) - NOTE: this command is not supported for compressed XDR files - - ENUM_XDR_HEADER - %name Output = string block - file_expression file name default extension '' - numerical_expression number of element to find name of -*/ -/************************************************************************/ -/* INCLUDE FILES */ -/************************************************************************/ - #include #include #include #include #include -#if 0 -#ifndef __sun__ -#include -#endif -#include "mbavs2q.h" - -#ifndef QUIRT -#include -#include -#include -#else -#include "mbfield.h" -#endif -#endif /************************************************************************/ /* DEFINES, ENUMERATED TYPES AND CONSTANTS */ @@ -186,7 +138,7 @@ const char* gl_ErrorMsg[] = { "XDR file reading error", "Out of memory", "Decompression failed", - "Format not handled by nkitkXDRImageIO (RECTILINEAR or IRREGULAR field)" + "Format not handled by clitkXdrImageIO (RECTILINEAR or IRREGULAR field)" }; @@ -237,109 +189,6 @@ static const unsigned long CRC32_table[256] = { }; -/************************************************************************/ -/* PROTOTYPES */ -/************************************************************************/ -#if 0 -int Rxdr_compute(AVSfield** ppOut, char* pszFileName, int iOffset); - -int RxdrPreview_compute(AVSfield** ppOut, char* pszFileName, int iOffset); - -int RxdrHeader_compute(char *pszOut, char *pszFileName, char *pszEntry); - -int RxdrEnum_compute(char *pszOut, char *pszFileName, int iEntry); - -/******************************************************************************/ -/* AVS/QUIRT INTERFACE */ -/******************************************************************************/ - -void Rxdr_desc(void) -{ - int param; - - /* Set the module name and type */ - AVSset_module_name("XDR reader", MODULE_DATA); - - /* Create output ports for the resulting fields */ - AVScreate_output_port("Output", "field"); - - /* declare widgets */ - QUIRT_NEXT_PARAMETER_FILE(""); - param = AVSadd_parameter("FileName", "string", "/data/AVS/", NULL, ""); - AVSconnect_widget(param, "browser"); - AVSadd_parameter_prop(param, "height", "integer", 8); - - param = AVSadd_parameter("Offset", "integer", 0, 0, INT_UNBOUND); - AVSconnect_widget(param, "typein_integer"); - - AVSset_compute_proc(CF Rxdr_compute); -} -AVS_TO_QUIRT(READ_XDR, Rxdr_desc); - -void RxdrPreview_desc(void) -{ - int param; - - /* Set the module name and type */ - AVSset_module_name("XDR preview reader", MODULE_DATA); - - /* Create output ports for the resulting fields */ - AVScreate_output_port("Output", "field"); - - /* declare widgets */ - QUIRT_NEXT_PARAMETER_FILE(""); - param = AVSadd_parameter("FileName", "string", "/data/AVS/", NULL, ""); - AVSconnect_widget(param, "browser"); - AVSadd_parameter_prop(param, "height", "integer", 8); - - param = AVSadd_parameter("Offset", "integer", 0, 0, INT_UNBOUND); - AVSconnect_widget(param, "typein_integer"); - - AVSset_compute_proc(CF RxdrPreview_compute); -} -AVS_TO_QUIRT(READ_XDR_PREVIEW, RxdrPreview_desc); - -void RxdrHeader_desc(void) -{ /* Set the module name and type */ - AVSset_module_name("Read XDR header", MODULE_DATA); - - /* Create output ports for the resulting fields */ - AVSadd_parameter("Output", "string_block", "", NULL, ""); - - QUIRT_NEXT_PARAMETER_FILE(""); - AVSadd_parameter("FileName", "string", "", NULL, ""); - - AVSadd_parameter("Entry", "string", "", NULL, ""); - - AVSset_compute_proc(CF RxdrHeader_compute); -} -AVS_TO_QUIRT(READ_XDR_HEADER, RxdrHeader_desc); - -void RxdrEnum_desc(void) -{ /* Set the module name and type */ - AVSset_module_name("Enumerate XDR header", MODULE_DATA); - - /* Create output ports for the resulting fields */ - AVSadd_parameter("Output", "string_block", "", NULL, ""); - - QUIRT_NEXT_PARAMETER_FILE(""); - AVSadd_parameter("FileName", "string", "", NULL, ""); - - AVSadd_parameter("Entry", "integer", 0, INT_UNBOUND, INT_UNBOUND); - - AVSset_compute_proc(CF RxdrEnum_compute); -} -AVS_TO_QUIRT(ENUM_XDR_HEADER, RxdrEnum_desc); - -#ifndef QUIRT -AVSinit_modules(void) -{ - AVSmodule_from_desc( (int_desc_func)Rxdr_desc ); - AVSmodule_from_desc( (int_desc_func)RxdrHeader_desc ); - AVSmodule_from_desc( (int_desc_func)RxdrEnum_desc ); -} -#endif -#endif /************************************************************************/ /* MODULE FUNCTIONS */ /************************************************************************/ @@ -392,42 +241,6 @@ static char *scan_header(const char *file, const char *name, int offset, int rem return NULL; } -/* help routine, enumerates XDR file for names of keyword entrys, returns NULL - if not found, else returns pointer to start of line upto '=' -*/ -// //Commented out because it does not seem to be needed for vv -//static char *enum_header(char *file, int iEntry, int offset) -//{ -// int i, count; -// static char temp[512]; -// FILE *f; -// char *p; -// -// if ((f = fopen(file, "rt")) == NULL) return NULL; -// if (offset) fseek(f, offset, SEEK_SET); -// -// count = 0; -// -// for (i=0; i<200; ) -// { -// if (fgets(temp, 500, f) == NULL ) break; /* end of file */ -// -// if (temp[0] == 12 ) break; /* ^L end of header */ -// if (temp[0] != '#') i++; /* The first 200 non comment lines must be read before data is opened. */ -// if ((p = strchr(temp+1, '=')) == NULL) continue; /* no '=' */ -// if (count++ != iEntry) continue; /* no match */ -// -// *p=0; /* match, end at = */ -// -// fclose (f); -// return temp; -// } -// -// fclose(f); -// return NULL; -//} - - static int get_nki_compressed_size(FILE *f) { NKI_MODE2 Header; @@ -864,23 +677,17 @@ static int nki_private_decompress(short int *dest, signed char *src, int size) //==================================================================== // Read image information (copied from XDRreader) -int nkitk::XDRImageIO::ReadImageInformationWithError() +int clitk::XdrImageIO::ReadImageInformationWithError() { int offset=0; itk::Vector dim; - int veclen=1; //, data=AVS_TYPE_BYTE, field=UNIFORM; -#if 0 - int iNkiCompression = 0; -#endif - int total=1/*, datasize=0, iNumRead, HeaderSize*/; + int veclen=1; + int total=1; unsigned int coords=0,i,j,ndim,nspace; char temp[512]; FILE *fstream; char *c; -#if 0 - char *buff; - AVSfield FieldTemplate; -#endif + long swap_test = 0x1000000; /* first byte is 1 when low-endian */ forcenoswap=0; char *file = const_cast(m_FileName.c_str()); @@ -1028,15 +835,15 @@ int nkitk::XDRImageIO::ReadImageInformationWithError() //==================================================================== // Read image information (copied from XDRreader) -void nkitk::XDRImageIO::ReadImageInformation() { +void clitk::XdrImageIO::ReadImageInformation() { int result = ReadImageInformationWithError(); - if (result) ITKError("nkitk::XDRImageIO::ReadImageInformation",result); + if (result) ITKError("clitk::XdrImageIO::ReadImageInformation",result); } //==================================================================== -// Read Image Content (copied from XDRreader) -int nkitk::XDRImageIO::ReadWithError(void * buffer) +// Read Image Content (copied from Xdr reader) +int clitk::XdrImageIO::ReadWithError(void * buffer) { //AVSINT dim[5]; int /*ndim,*/ nspace/*, veclen=1, data=AVS_TYPE_BYTE, field=UNIFORM*/; int iNkiCompression = 0; @@ -1076,16 +883,6 @@ int nkitk::XDRImageIO::ReadWithError(void * buffer) if (c) HeaderSize = 32768; else HeaderSize = 2048; -#if 0 - FIELDdefault(&FieldTemplate); - FieldTemplate.ndim = ndim; - FieldTemplate.nspace = nspace; - FieldTemplate.veclen = veclen; - FieldTemplate.type = data; - FieldTemplate.uniform = field; - FieldTemplate.size = datasize; -#endif - fstream = fopen(file, "rb"); if (fstream == NULL) return ER_XDR_OPEN; @@ -1130,18 +927,6 @@ int nkitk::XDRImageIO::ReadWithError(void * buffer) if (i==iNumRead) return ER_XDR_NOCTRLL; -#if 0 - if (*output) AVSfield_free(*output); - *output = (AVSfield *) AVSfield_alloc(&FieldTemplate, dim); - if (*output == NULL) - { - fclose(fstream); - return ER_OUTOFMEMORY; - } - - total *= datasize * veclen; - coords *= sizeof(float); -#endif total = GetImageSizeInBytes(); //We add casts because the resulting quantity can be negative. @@ -1175,10 +960,6 @@ int nkitk::XDRImageIO::ReadWithError(void * buffer) if (!pCompressed) { fclose(fstream); -#if 0 - if (*output) AVSfield_free(*output); - *output = NULL; -#endif return ER_OUTOFMEMORY; } @@ -1186,20 +967,12 @@ int nkitk::XDRImageIO::ReadWithError(void * buffer) if (fread( (void *)pCompressed, 1, iSize, fstream ) != iSize) { fclose(fstream); -#if 0 - if (*output) AVSfield_free(*output); - *output = NULL; -#endif return ER_XDR_READ; } if (!nki_private_decompress((short*)buffer, pCompressed, iSize)) { fclose(fstream); -#if 0 - if (*output) AVSfield_free(*output); - *output = NULL; -#endif return ER_DECOMPRESSION; } @@ -1216,10 +989,6 @@ int nkitk::XDRImageIO::ReadWithError(void * buffer) if (fread( (void *)buffer, 1, total, fstream ) != total) { fclose(fstream); -#if 0 - if (*output) AVSfield_free(*output); - *output = NULL; -#endif return ER_XDR_READ; } } @@ -1274,391 +1043,30 @@ int nkitk::XDRImageIO::ReadWithError(void * buffer) } READ_COORDS: -#if 0 - if (coords) /* expect AVS coordinates ? */ - { - if (fread( (*output)->points, 1, coords, fstream ) == coords) - { /* swap data if read-ok and required (xdr is low-endian) */ - - if (!(*(char *)(&swap_test)) && !forcenoswap) - { - c = (char *)(*output)->points; - for (i=0; iMAXDIM) return ER_XDR_NDIM; - nspace = ndim; - - /* defaults for dimensions and downsize */ - - for (i=0; i200000L) return ER_XDR_DIM; - - total *= dim[i]; - coords += dim[i]; - } - - c = scan_header(file, "nspace", offset, 1); - if (c) nspace = atoi(c); - if (nspace<1 || ndim > MAXDIM) return ER_XDR_NSPACE; - - c = scan_header(file, "veclen", offset, 1); - if (c) veclen = atoi(c); - if (veclen<0 || veclen>100) return ER_XDR_VECLEN; - - c = scan_header(file, "data", offset, 1); - - if (c) - { - if (memicmp(c, "byte", 4) == 0) data=AVS_TYPE_BYTE, datasize=1; - else if (memicmp(c, "short", 5) == 0) data=AVS_TYPE_SHORT, datasize=2; - else if (memicmp(c, "int" , 3) == 0) data=AVS_TYPE_INTEGER,datasize=4; - else if (memicmp(c, "real", 4) == 0) data=AVS_TYPE_REAL, datasize=4; - else if (memicmp(c, "float", 5) == 0) data=AVS_TYPE_REAL, datasize=4; - else if (memicmp(c, "double",6) == 0) data=AVS_TYPE_DOUBLE, datasize=8; - - else if (memicmp(c, "xdr_byte", 8) == 0) data=AVS_TYPE_BYTE, datasize=1, forcenoswap=0; - else if (memicmp(c, "xdr_short", 9) == 0) data=AVS_TYPE_SHORT, datasize=2, forcenoswap=0; - else if (memicmp(c, "xdr_int" , 7) == 0) data=AVS_TYPE_INTEGER,datasize=4, forcenoswap=0; - else if (memicmp(c, "xdr_real", 8) == 0) data=AVS_TYPE_REAL, datasize=4, forcenoswap=0; - else if (memicmp(c, "xdr_float", 9) == 0) data=AVS_TYPE_REAL, datasize=4, forcenoswap=0; - else if (memicmp(c, "xdr_double",10)== 0) data=AVS_TYPE_DOUBLE, datasize=8, forcenoswap=0; - else return ER_XDR_DATA; - } - - c = scan_header(file, "field", offset, 1); - if (c) - { - if (memicmp(c, "unifo", 5) == 0) field=UNIFORM, coords=nspace*2; - else if (memicmp(c, "recti", 5) == 0) field=RECTILINEAR; - else if (memicmp(c, "irreg", 5) == 0) field=IRREGULAR, coords=total*nspace; - else return ER_XDR_FIELD; - } - else - coords=0; - - c = scan_header(file, "nki_compression", offset, 1); - if (c) iNkiCompression = atoi(c); - if (iNkiCompression) - { - fclose(fstream); - return ER_ILLCOMMFUNCT; - } - - FIELDdefault(&FieldTemplate); - FieldTemplate.ndim = ndim; - FieldTemplate.nspace = nspace; - FieldTemplate.veclen = veclen; - FieldTemplate.type = data; - FieldTemplate.uniform = field; - FieldTemplate.size = datasize; - - fstream = fopen(file, "rb"); - - if (fstream == NULL) - return ER_XDR_OPEN; - - buff = (char *)malloc(8192); - if (buff == NULL) - { - return ER_OUTOFMEMORY; - } - memset(buff, 0, 8192); - fseek(fstream, offset, SEEK_SET); - - while (1) - { - if (fgets(temp, 500, fstream) == NULL ) - return ER_XDR_NOCTRLL; /* end of file */ - - if (temp[0] == 10) continue; - - if (temp[0] == 12) - { - fseek(fstream, -2, SEEK_CUR); - break; - } /* ^L end of header */ - - if (temp[0] != '#') break; - } - start = ftell(fstream); - - iNumRead = fread(buff, 1, 8192, fstream); - if (iNumRead < 1) - { - free(buff); - fclose(fstream); - return ER_XDR_READ; - } - - for (i=0; i=3) - { - if (dim[0]==dim[1]) sliceax = 2; - else sliceax = 1; - } - - total = 1; - - for (i=0; ifield_data); - - buff = (char *)malloc(dim[0] * datasize * veclen); - - for (i=0; ifield_data; - for (i=0; ifield_data; - for (i=0; ifield_data; - for (i=0; ipoints, coords ) == coords) - { if (!(*(char *)(&swap_test)) && !forcenoswap) - { c = (char *)(*output)->points; - for (i=0; i + * @date Sun Jun 1 22:12:20 2008 + * + * @brief + * + * + */ + +#include "clitkXdrImageIO.h" +#include "clitkCommon.h" + +#include + +//From mbfield.h +#ifndef unix +//#define _read readfix +#endif +#define AVSINT ptrdiff_t +#define AVS_ERROR +#define AVS_OK + +//From portdefs.h +#ifndef __LARGE__ +# if defined(__GNUC__) || defined(unix) + typedef long long Q_INT64; + typedef unsigned long long Q_UINT64; +# define Q_INT64_CONST(x) (x##ll) +# define Q_UINT64_CONST(x) (x##llu) /* gcc also allows ull */ + /* When using MINGW with MS(V)CRT DLL, use MS format modifier. */ +# ifdef __MSVCRT__ +# define Q_INT64_FORMAT "I64" +# else +# define Q_INT64_FORMAT "L" +# endif +# elif defined(__BORLANDC__) || defined(__WATCOMC__) || defined(_MSC_VER) + typedef __int64 Q_INT64; + typedef unsigned __int64 Q_UINT64; +# define Q_INT64_CONST(x) (x##i64) +# define Q_UINT64_CONST(x) (x##ui64) /* i64u is not allowed! */ +# ifdef _MSC_VER +# define Q_INT64_FORMAT "I64" +# else +# define Q_INT64_FORMAT "L" +# endif +# else +# error No 64 bit integers known for this compiler, edit portdefs.h. +# endif +#endif + +bool clitk::XdrImageIO::CanWriteFile(const char* FileNameToWrite) +{ std::string filename(FileNameToWrite); + std::string filenameext = GetExtension(filename); + if (filenameext != std::string("xdr")) return false; + return true; +} + +void clitk::XdrImageIO::Write(const void* buffer) +{ char *s = ""; + WriteImage( m_FileName.c_str(), s, s, 0, -1, 0, 2, 0, 0, 0, 0, buffer); +} + +/************************************************************************/ +/* */ +/* file : AVS_WXDR.C */ +/* */ +/* purpose : AVS module for writing XDR and RAW files */ +/* */ +/* author : Lambert Zijp (based on a true story by */ +/* Marcel van Herk) */ +/* */ +/* date : 19970716 */ +/* */ +/* portability: AVS requires sizeof(void *)==sizeof(int) */ +/* This module assumes sizeof(int)>=4 */ +/* */ +/* notes : This module has been integrated in QUIRT */ +/* */ +/************************************************************************/ +/* Updates: +When Who What +19980212 ljz Creation (i.e. Removed from 'mbfield4.c') +19980304 ljz Added QUIRT_NEXT_PARAMETER_FILE +19980310 ljz Fix: XDRs were written in text-mode when + quirt_init() is not called +19980319 ljz Undone last change: _fmode is set in DllEntryPoint +19980408 tp Moved M$-specific includes; + removed AVS portability anyway +19980611 mvh Added offset to write_raw (write into file) +20000214 lsp Check file handle returned by open before use +20000313 nd Two AVSerror messages added. +20000322 lsp Matched compute func declaration with desc func +20000630 ljz Added WRITE_RAW_LE, to write little-endian pixel-data +20000806 lsp Compute func now 'int', conformal to ANSI C++ +20000821 ljz Added NkiCompression parameter to WRITE_XDR. If used, + mode 2 is recommended. +20000504 mvh Added write coordinates in header option to WRITE_XDR +20010720 mvh Added option to write to stdout: pass "" as filename +20010723 mvh Made it working by redefining write; setmode does not work +20010725 bb Changed include order of windows.h and mbavs2q.h + for MS6 compiler. +20020124 mvh+kg The datatype in the header is now e.g. "xdr_integer" + The datatypes as "integer" were intended as having native + byte order, while we wrote bigendian (high byte first). Our + older files read therefore OK on HP and SUN but not on linux. +20020124 mvh+kg On AVS5 xdr_byte is illegal. Replace only that one by 'byte' +20030311 bb Added check on write errors. Disc full errors during a + write have nasty side-effects for other programs' open files. + Added check on file handle to prevent closing of stdout. +20030430 mvh Added append mode for WRITE_XDR +20030717 ljz Added support for NkiCompressionModes 3 and 4 (4 is recommended) +20040426 mvh ELEKTA NKI-XVI0.1 RELEASE +20040910 mvh Write uncompressed and warn if the compress malloc fails, layout +20040920 mvh Fixed the above option (failed in append mode) +20040924 mvh Fixed warn in option +20041117 mvh ELEKTA NKI-XVI0.1g RELEASE +20050302 ljz Merant tracker ID #1867: Check on bad compression-ratio causing + access violation. +20050308 ljz+mvh ELEKTA NKI-XVI0.1j RELEASE +20050411 mvh Fails on I178_s1_4H.3d mode 1: compression fails and written file corrupted +20060903 mvh Added WRITE_MEM_XDR and QUERY_MEM_XDR +20070330 mvh WRITE_XDR failed on G:\20605104_hypo lung\20605104_incl corr+doseab3.PACK +20071015 mvh ELEKTA NKI-XVI3.08 RELEASE +20080411 lsp+mw NULL->0 in WriteImage() ; __sun__ doesn't know +20080825 mgw Corrected slash in sys/stat.h include +20081031 lsp+mvh ELEKTA NKI-XVI4.15 RELEASE +20081119 lsp+sr Removed tell(f) call for Linux +20081203 lsp __sun__ -> unix +20090114 mvh ELEKTA NKI-XVI4.22 RELEASE +20090529 lsp+sr Work around the 64 MB limitation of write() in Windows +20090802 mvh ELEKTA NKI-XVI4.29 RELEASE +20091209 lsp Added WRITE_MEM_RAW +20091214 lsp 64 bits adaptations: pass high address of buffer as well, + replaced int by AVSINT when requested, process buffer in chunks + to be able to interpret return value of write() (signed int) correctly, + replaced out-of-range constants 0xc0 and 0x80 by their signed equivalents +20091216 lsp+ljz Prepared compression for more than 4294967295 (UINT_MAX) shorts + by using bigger NKI_MODE2_64BITS struct (backwards compatible) +20091216 lsp Disabled #define write for clarity: checked_write() is used throughout + Use more space for pCompressed to be able to call WRITE_XDR with compression + on small fields (<10 pixels for mode 2 or 4, <2 pixels for mode 1 and 3) +*/ + +/************************************************************************/ +/* MODULE DOCUMENTATION */ +/************************************************************************/ +/* +AVS Modules Lambert Zijp XDR writer & RAW writer + +NAME + XDR writer - Module for writing XDR and RAW files + +SUMMARY + Name XDR writer + + Availability : specify in which module libraries + + Source AVS_WXDR (C++ interface) + + Type Render / Output + + QUIRT name WRITE_XDR & WRITE_RAW + + Inputs Input field = field + + Outputs none + + Parameters Name Type Default Min Max + File name string + Header info string (WRITE_XDR only!) + Header file string (WRITE_XDR only!) + File Offset integer (WRITE_RAW only!) + NKI compression integer (WRITE_XDR only!) + Coords in header boolean (WRITE_XDR only!) + Append boolean (WRITE_XDR only!) + +DESCRIPTION + WRITE_RAW: No header, no coordinates. + Data only are written (high byte first); optionally into an + existing file (if you specify an offset) + + WRITE_RAW_LE: No header, no coordinates. + Data only are written (low byte first); + optionally into an existing file (if you specify an offset) + + WRITE_XDR: Successively is written to file: + - The string '#AVS wants ...'. + - The optional Header info + - The contents of the optional Header file + - An ascii description of the Input field + - Optionally the coordinates (coord%axis%[%pixel%]=%coord%) + (%axis% is 1 based, %pixel% 0 based, %coord% is float) + - Two bytes containing ascii character 0x0c + - The Data in binary (high byte first). + Or, if NKI_Compression is greater than zero, compressed data. + - The Coordinates in binary IEEE float (high byte first) + +INPUTS + Input field (Required; field) + Describe input here.... + +PARAMETERS + File name + A string described here.... + + Header info + A string described here.... + + Header file + A string described here.... + + File offset + An integer (default -1). If set, WRITE_RAW writes the data + into an existing file. + + NKI compression + An integer described here.... + + Write coordinates in header + A boolean described here.... + + Append to file + A boolean described here.... + +OUTPUTS + none + +PORTABILITY + QUIRT, C++ + +LIBRARIES + (optional): in which AVS libraries available + +TYPE + Render / Output + (optional): further specify type of Render / Output module + +FILE FORMATS + (optional): which type of files are read|written + +EXAMPLE + The following network shows how ..... + + READ IMA + | + | + XDR WRITER + | | + | | + +LIMITATIONS + (optional): describe limitations here + +RELATED MODULES + Modules that can provide input: + ..... + Modules that could be used in place of XDR writer: + ..... + Modules that can take output: + ..... + +RELATED FILES + (optional): The following files + {are needed for|are output from|give examples of|further document} + XDR writer: + ..... + + +QUIRT MANUAL + WRITE_XDR AVS module XDR writer + numerical_expression Input field = field + string_expression name of xdr file to create + string_expression extra header info (default "") + string_expression text file with more header info (default none) + numerical_expression NKI compression (default 0) + numerical_expression Coordinates in header (boolean, default 0) + numerical_expression Append to file (boolean, default 0) + + WRITE_RAW AVS module Raw writer + numerical_expression Input field = field + string_expression name of file to write + numerical_expression Offset; if not -1 (default) writes into file at offset + + WRITE_RAW_LE AVS module Raw writer little endian + numerical_expression Input field = field + string_expression name of file to write + numerical_expression Offset; if not -1 (default) writes into file at offset + + WRITE_MEM_XDR + numerical_expression Input field = field + numerical_expression point to buffer to write xdr data into (use QUERY_MEM_XDR to find length) + string_expression extra header info (default "") + string_expression text file with more header info (default none) + numerical_expression NKI compression (default 0) + numerical_expression Coordinates in header (boolean, default 0) + + QUERY_MEM_XDR + numerical_expression Input field = field + name recieves required length for data + string_expression extra header info (default "") + string_expression text file with more header info (default none) + numerical_expression NKI compression (default 0) + numerical_expression Coordinates in header (boolean, default 0) + +Release 1.0 19980212 Lambert Zijp XDR writer & RAW writer +*/ +/************************************************************************/ +/* INCLUDE FILES */ +/************************************************************************/ + +#include +#include +#include +#include +#include +#ifndef unix +#include +#endif +#include +#include + +#include + +#ifdef WIN32 +// don't use min() and max() macros indirectly defined by windows.h, +// but use portable std::min() and std:max() instead +#ifndef NOMINMAX +#define NOMINMAX +#endif +#include +#endif + +/************************************************************************/ +/* DEFINES, ENUMERATED TYPES AND CONSTANTS */ +/************************************************************************/ + +#undef fileno +#define fileno _fileno + +#undef close +#define close _close + +#undef open +#define open _open + +#undef lseek +#define lseek _lseek + +#undef creat +#define creat _creat + +#pragma pack (1) + +// Fields with data size>8GB (having UINT_MAX short pixels) cannot be compressed using +// NKI_MODE2 struct because iOrgSize has type "unsigned int". In that case use NKI_MODE2_64BITS. +// The type of structure is indicated as follows: +// +// iOrgSize==0: NKI_MODE2_64BITS +// otherwise : NKI_MODE2 +// +// Compression modes 1 and 3 (without CRCs) only use the first 2 members (iOrgSize and iMode). + +typedef struct +{ + unsigned int iOrgSize; /* in pixels (i.e. shorts) */ + unsigned int iMode; /* 1, 2, 3 or 4 */ + unsigned int iCompressedSize; /* in bytes, excluding header */ + unsigned int iOrgCRC; /* CRC of the data (no coords etc) */ + unsigned int iCompressedCRC; /* CRC of the compressed data, excluding this header */ +} NKI_MODE2; + +typedef struct +{ + unsigned int iOrgSize; /* in pixels (i.e. shorts) */ + unsigned int iMode; /* 1, 2, 3 or 4 */ + unsigned int iCompressedSize; /* in bytes, excluding header */ + unsigned int iOrgCRC; /* CRC of the data (no coords etc) */ + unsigned int iCompressedCRC; /* CRC of the compressed data, excluding this header */ + unsigned int iPad; /* unused */ + Q_UINT64 i64OrgSize; /* used for more than UINT_MAX pixels, indicated by iOrgSize==0 (0-vector not compressed) */ + Q_UINT64 i64CompressedSize; /* value in BYTES, used for more than UINT_MAX PIXELS, indicated by iCompressedSize==0 */ + Q_UINT64 i64Future1; + Q_UINT64 i64Future2; +} NKI_MODE2_64BITS; + +#pragma pack () + +// Changed next to static function in stead of macro so it can +// have a return value to check in the calling function. +// It could be made inline as well, but there is no real time +// punishment from the extra layer of function calls. + +// note: some compilers do not like comments ending in a backslash. +// so use macro functions to exclude. + + +/************************************************************************/ +/* GLOBAL VARIABLES */ +/************************************************************************/ + +static const unsigned long CRC32_table[256] = { + 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, + 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, + 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, + 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, + 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, + 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, + 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, + 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, + 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, + 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, + 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106, + 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, + 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, + 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, + 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, + 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, + 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, + 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, + 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, + 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, + 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, + 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, + 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, + 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, + 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, + 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, + 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, + 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, + 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, + 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, + 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, + 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, + 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, + 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, + 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, + 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, + 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, + 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, + 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, + 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, + 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, + 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, + 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d +}; + +/************************************************************************/ +/* MODULE FUNCTIONS */ +/************************************************************************/ + +#ifdef __WATCOMC__ +_WCRTLINK +#endif +int writefix(int file, const void *buf, unsigned int count) +{ int i, j, k, total=0; + + for (i=0; i16384) j=16384; + + k=_write(file, (char *)buf+i, j); + if (k < 0) return k; + + total += k; + + if (k != j) break; + } + + return total; +} + + +/* + Version of write() that takes special action in case of + standard output. Based on commented out macro above. + This function overloads the (or stdio.h for old style C++) + write() function. +*/ + +// Like the original macro, we do /not/ want writefix from mbfield.c. +#ifdef write +#undef write +#endif +static int wxdr_write(int handle, const void * buf, unsigned len) +{ + // if (handle == 1) // stdout + if (handle == fileno(stdout)) + { +#ifdef WIN32 + // Behave as C standard library write(): return number of bytes + // written or -1 and errno set on error. + fflush(stdout); + DWORD dwBytesWritten; + if (!WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), buf, len, + &dwBytesWritten, NULL)) + { + // There is no simple 1-to-1 mapping between GetLastError() + // values that WriteFile() can return (quite a lot) and the two + // errno values that write() can return. So return EACCES in + // almost all cases. + switch (GetLastError()) + { case ERROR_INVALID_HANDLE: + errno = EBADF ; break; + default: + errno = EACCES; break; + } + return -1; + } + else + return (int)dwBytesWritten; // May still be < len! + // And... write() may write a maximum of UINT_MAX-1 bytes, whereas + // WriteFile() may write UINT_MAX bytes at once. But since + // int(UINT_MAX) == -1 this will pose an actual problem in the + // (far?) future. +#else // !WIN32 + const int oldmode = setmode(handle, O_BINARY); + const int iBytesWritten = write(handle, buf, len); + const int saveerrno = errno; // setmode() may change errno. + if (oldmode != -1) setmode(handle, oldmode); + errno = saveerrno; + return iBytesWritten; +#endif // !WIN32 + } + else + return _write(handle, buf, len); +} + +/* + Checked write(). + Behaves like win32 WriteFile() and returns a Boolean to indicate + success or failure, where failure almost invariably means disc full. + + !!! SIDE EFFECT !!! + + In case of failure, this function issues an AVS error message + and closes the file (if handle != 1). It is up to the calling + function to return the AVS_ERROR state and before that do things + like close other files, free memory etc. This way, there is less + chance of erroneously duplicated code, like in: + written = write(f, temp, strlen(temp)); + if (written == -1 || written != strlen(temp)) + { AVSerror(...); + if (f != fileno(stdout)) close(f); + return AVS_ERROR; + } + written = write(f, buf, buflength) + if (written == -1 || written != strlen(temp)) { + // oops, wrong length copy'n'pasted + + If more elaborate error handling is needed then the calling + functuon should use the (overloaded) write() and act on its return + value (and the value of errno) accordingly. + + It does /not/ close stdout. + + !!! SIDE EFFECT !!! + + Note that checked_write() takes a size_t for len, whereas write() takes + an unsigned int of 4 bytes. On a 64 bits OS a size_t will be an 8 byte integer, + enabling more than UINT_MAX bytes to write at once. +*/ +static BOOL checked_write(int handle, const void * buf, size_t len, char **buffer) +{ if (buffer && !handle) + { memcpy(*buffer, buf, len); + (*buffer) += len; + return TRUE; + } + if (buffer && handle) + { (*buffer) += len; + return TRUE; + } + else + { for(int i=0; i<2; i++) + { int byteswritten; + size_t remaining; + int chunksize; + + //If write fails, test if not related to big buffer problem + //Bug report http://support.microsoft.com/kb/899149 entitled + //"You cannot call the fwrite function to write to a buffer + // that is larger than 64 MB in Visual C++ 2005, + // in Visual C++ .NET 2003, or in Visual C++ .NET 2002" + // NB: same thing for write function in binary mode + if (i==0) + { remaining = len; + // call wxdr_write (for handle!=fileno(stdout) a wrapper for write) several times + // to interpret the signed 32-bit return value correctly + while (remaining>0) + { chunksize = (int)std::min(remaining, (size_t)INT_MAX); + byteswritten = wxdr_write(handle, buf, chunksize); + if (byteswritten == chunksize) + remaining -= chunksize; + else + break; // try writefix in the next round + } + if (remaining == 0) + return TRUE; + } + else + { remaining = len; + // call writefix (in mbfield.c) several times to interpret the signed 32-bit + // return value correctly. writefix uses chunks of 16384 bytes + while (remaining>0) + { chunksize = (int)std::min(remaining, (size_t)INT_MAX); + byteswritten = writefix(handle, buf, chunksize); + if (byteswritten == chunksize) + remaining -= chunksize; + else + break; // even writefix failed: return error + } + if (remaining == 0) + return TRUE; + } + } + // Note: file is open in binary mode, no need to compensate + // for a value of byteswritten > len due to \n -> \r\n conversions. + // (write() on a text stream is implementation dependent.) + if (handle != fileno(stdout)) close(handle); + AVSerror("Avs_wxdr: write failed, disk full?"); + return FALSE; + } +} + +/* coder for NKI private compressed pixel data + arguments: dest = (in) points to area where compressed destination data is written (byte) + src = (in) points to uncompressed source data (short) + npixels = (in) number of pixels to compress + + The return value is the number of bytes in the compressed data (maximal 3*npixels+10, typical 0.52*npixels) + + if iMode == 1 then + - The first 4 bytes contain the number of short-int pixels + - The following 4 bytes contain iMode=1 + - The rest is the compressed image + + if iMode == 2 then + - The first 4 bytes contain the number of short-int pixels + - The following 4 bytes contain iMode=2 + - The following 4 bytes contain the size of the compressed image (in bytes) + - The following 4 bytes contain the CRC of the original image + - The following 4 bytes contain the CRC of the compressed image + - The rest is the compressed image + - The compressed size will be even (padded by a zero if necessary). + + if iMode == 3 then + - The first 4 bytes contain the number of short-int pixels + - The following 4 bytes contain iMode=3 + - The rest is the compressed image, including 4 bit differences + + if iMode == 4 then + - The first 4 bytes contain the number of short-int pixels + - The following 4 bytes contain iMode=4 + - The following 4 bytes contain the size of the compressed image (in bytes) + - The following 4 bytes contain the CRC of the original image + - The following 4 bytes contain 0 + - The rest is the compressed image, including 4 bit differences + - The compressed size will be even (padded by a zero if necessary). + + iMode 1 and iMode 2 are identical, except for the CRC data that is included for iMode 2 + iMode 3 and iMode 4 are identical, except for the CRC data that is included for iMode 4 +*/ + +// optimized settings for the 4 bit run compressor (mode 3 and 4) + +#define MINZEROS 5 // shortest RLE (2 byte overhead, but breaks 4bit run) +#define MIN4BIT 6 // shortest 4 bit run (6 bytes compressed to 5 bytes) + +// This internal routine converts an 8 bit difference string into a 4 bit one +static signed char *recompress4bit(int n, signed char *dest) +{ signed char *p, *q; + int val; + + n = n & 0xfe; + dest -= n; + p = dest; + val = (((int)p[0])<<4) | (p[1]&15); + p += 2; + *dest++ = -0x40; // 192 (0xc0) does not fit between -128..127: maps to -64 (0x40) in 2's complement + *dest++ = (signed char)n; + q = dest++; + n -= 2; + while(n>0) + { *dest++ = (signed char)((((int)p[0])<<4) | (p[1]&15)); + p += 2; + n -= 2; + } + q[0] = (signed char)val; + + return dest; +} + + +static size_t nki_private_compress(signed char *dest, short int *src, size_t npixels, int iMode) +{ unsigned long iCRC; + unsigned long iCRC2; + unsigned int iHeaderSize=8; // value for iMode==1 and iMode==3 + register int val; + size_t i,j; + NKI_MODE2* pHeader = (NKI_MODE2*)dest; + NKI_MODE2_64BITS* pHeader_64bits = (NKI_MODE2_64BITS*)dest; + size_t iBufferSize; + + iBufferSize = (npixels / 2) * 3; // Buffer is sizeof(NKI_MODE2_64BITS) + 10 bytes larger + + /* Up till now only Mode=1 .. 4 are supported */ + if ((iMode < 1) || (iMode > 4)) + return 0; + + /* Create the header */ + pHeader->iMode = iMode; + + if (sizeof(int*)>sizeof(int) && npixels>UINT_MAX) // On a 64 bits OS we want to store files>4GB + { pHeader_64bits->iOrgSize = 0; // This indicates>4GB file (0-vector is not compressed) + pHeader_64bits->i64OrgSize = npixels; + iHeaderSize = sizeof(NKI_MODE2_64BITS); + dest += sizeof(NKI_MODE2_64BITS); + } + else + { pHeader->iOrgSize = (unsigned int)(npixels & UINT_MAX); // store 32 bit number as first member + + if (iMode==2 || iMode==4) + iHeaderSize = sizeof(NKI_MODE2); + dest += iHeaderSize; + } + + /* Create the compressed image */ + + if (iMode == 1) + { *(short int *)dest = *src; + dest+=2; + + npixels--; + + do + { val = src[1] - src[0]; + src++; + + if (val == 0) /* run length-encode zero differences */ + { for (i=2;; i++) + { if (i>=npixels || src[i-1]!=src[-1] || i==256) + { if (i==2) + *dest++=0; + else + { *dest++ = -128; // hexadecimal 0x80 + *dest++ = (signed char)(i-1); + npixels -= (i-2); + src += (i-2); + } + break; + } + } + } + else if (val >= -64 && val <= 63) /* small difference coded as one byte */ + { *dest = (signed char)val; + dest++; + } + else if (val >= -0x3F00 && val <= 0x3EFF) /* large differences coded as two bytes */ + { dest[0] = (signed char)((val>>8) ^ 0x40); + dest[1] = (signed char)val; + dest+=2; + } + else /* if very large differences code abs val as three bytes */ + { *dest++ = 0x7F; + *dest++ = (signed char)(src[0]>>8); + *dest++ = (signed char)(src[0]); + } + /* Are we beyond the allocated memory? */ + if ((size_t)(dest - (signed char*)pHeader) > iBufferSize) + return 0; + } + while (--npixels); + } + + else if (iMode == 2) + { iCRC = 0; + iCRC2 = 0; + + *(short int *)dest = val = *src; + iCRC2 = CRC32_table[(unsigned char)iCRC2 ^ (unsigned char) val ] ^ ((iCRC2 >> 8)); + iCRC2 = CRC32_table[(unsigned char)iCRC2 ^ (unsigned char)(val>>8)] ^ ((iCRC2 >> 8)); + iCRC = CRC32_table[(unsigned char)iCRC ^ (unsigned char) val ] ^ ((iCRC >> 8)); + iCRC = CRC32_table[(unsigned char)iCRC ^ (unsigned char)(val>>8)] ^ ((iCRC >> 8)); + dest+=2; + npixels--; + + do + { val = src[1] - src[0]; + src++; + iCRC = CRC32_table[(unsigned char)iCRC ^ (unsigned char) src[0] ] ^ ((iCRC >> 8)); + iCRC = CRC32_table[(unsigned char)iCRC ^ (unsigned char)(src[0]>>8)] ^ ((iCRC >> 8)); + + if (val == 0) /* run length-encode zero differences */ + { for (i=2;; i++) + { if (i>=npixels || src[i-1]!=src[-1] || i==256) + { if (i==2) + { *dest++=0; + iCRC2 = CRC32_table[(unsigned char)iCRC2 ^ 0 ] ^ ((iCRC2 >> 8)); + } + else + { *dest++ = -128; // hexadecimal 0x80 + iCRC2 = CRC32_table[(unsigned char)iCRC2 ^ 0x80 ] ^ ((iCRC2 >> 8)); + *dest++ = (signed char)(i-1); + iCRC2 = CRC32_table[(unsigned char)iCRC2 ^ (i-1)] ^ ((iCRC2 >> 8)); + npixels -= (i-2); + + for (j=0; j> 8)); + iCRC = CRC32_table[(unsigned char)iCRC ^ (unsigned char)(src[0]>>8)] ^ ((iCRC >> 8)); + } + } + break; + } + } + } + else if (val >= -64 && val <= 63) /* small difference coded as one byte */ + { *dest = (signed char)val; + iCRC2 = CRC32_table[(unsigned char)iCRC2 ^ (unsigned char)val ] ^ ((iCRC2 >> 8)); + dest++; + } + else if (val >= -0x3F00 && val <= 0x3EFF) /* large differences coded as two bytes */ + { dest[0] = (signed char)((val>>8) ^ 0x40); + iCRC2 = CRC32_table[(unsigned char)iCRC2 ^ (unsigned char)dest[0] ] ^ ((iCRC2 >> 8)); + dest[1] = (signed char)val; + iCRC2 = CRC32_table[(unsigned char)iCRC2 ^ (unsigned char)val ] ^ ((iCRC2 >> 8)); + dest+=2; + } + else /* if very large differences code abs val as three bytes */ + { dest[0] = 0x7F; + iCRC2 = CRC32_table[(unsigned char)iCRC2 ^ 0x7f ] ^ ((iCRC2 >> 8)); + val = src[0]; + dest[1] = (signed char)(val>>8); + iCRC2 = CRC32_table[(unsigned char)iCRC2 ^ (unsigned char)(val>>8)] ^ ((iCRC2 >> 8)); + dest[2] = (signed char)val; + iCRC2 = CRC32_table[(unsigned char)iCRC2 ^ (unsigned char)val ] ^ ((iCRC2 >> 8)); + dest+=3; + } + /* Are we beyond the allocated memory? */ + if ((size_t)(dest - (signed char*)pHeader) > iBufferSize) + return 0; + } + while (--npixels); + + if ((dest - (signed char*)pHeader - iHeaderSize)iCompressedSize = + (unsigned int)(dest - (signed char*)pHeader - iHeaderSize); + else // store 64 bit number in extended structure + pHeader_64bits->i64CompressedSize = dest - (signed char*)pHeader -iHeaderSize; + + /* Pad it to get an even length */ + if (pHeader->iCompressedSize & 1) + { *dest++ = 0; + iCRC2 = CRC32_table[(unsigned char)iCRC2 ^ 0] ^ ((iCRC2 >> 8)); + pHeader->iCompressedSize++; + } + + pHeader->iOrgCRC = iCRC; + pHeader->iCompressedCRC = iCRC2; + } + + /* Create the compressed image - compressor with added 4 bit run */ + + else if (iMode == 3) + { int n4bit=0; + *(short int *)dest = *src; + dest+=2; + npixels--; + + do + { val = src[1] - src[0]; + src++; + + if (val == 0) /* run length-encode zero differences */ + { for (i=2;; i++) + { if (i>=npixels || src[i-1]!=src[-1] || i==256) + { if (i<=MINZEROS) /* too short run -> write zeros */ + { for (j=0; j=254) /* maximum length 4 bit run */ + { dest = recompress4bit(n4bit, dest); + n4bit = 0; + } + } + } + else + { if (n4bit>=MIN4BIT) /* end (and write) 4 bit run */ + dest = recompress4bit(n4bit, dest); + + n4bit=0; + *dest++ = -128; // hexadecimal 0x80 + *dest++ = (signed char)(i-1); + } + + npixels -= (i-2); + src += (i-2); + break; + } + } + } + else if (val >= -63 && val <= 63) /* small difference coded as one byte */ + { if (val >= -8 && val <= 7) + { *dest++ = (signed char)val; + n4bit++; + + if(n4bit>=254) /* maximum length 4 bit run */ + { dest = recompress4bit(n4bit, dest); + n4bit=0; + } + } + else if(n4bit>=MIN4BIT) /* end and write 4 bit run */ + { j = val; + dest = recompress4bit(n4bit, dest); + n4bit=0; + *dest++ = (signed char)j; + } + else + { *dest++ = (signed char)val; /* end 4 bit run */ + n4bit = 0; + } + } + else if (val >= -0x3F00 && val <= 0x3EFF) /* large differences coded as two bytes */ + { j = val; + + if(n4bit>=MIN4BIT) /* end (and write) 4 bit run */ + dest = recompress4bit(n4bit, dest); + + n4bit=0; + dest[0] = (signed char)((j>>8) ^ 0x40); + dest[1] = (signed char)j; + dest+=2; + } + else /* if very large differences code abs val as three bytes */ + { j = src[0]; + + if(n4bit>=MIN4BIT) /* end (and write) 4 bit run */ + dest = recompress4bit(n4bit, dest); + + n4bit=0; + *dest++ = 0x7F; + *dest++ = (signed char)(j>>8); + *dest++ = (signed char)j; + } + /* Are we beyond the allocated memory? */ + if ((size_t)(dest - (signed char*)pHeader) > iBufferSize) + return 0; + } + while (--npixels); + } + + /* Create the compressed image - compressor with added 4 bit run and CRC */ + + else if (iMode == 4) + { int n4bit=0; + iCRC = 0; + + *(short int *)dest = val = *src; + iCRC = CRC32_table[(unsigned char)iCRC ^ (unsigned char) val ] ^ ((iCRC >> 8)); + iCRC = CRC32_table[(unsigned char)iCRC ^ (unsigned char)(val>>8)] ^ ((iCRC >> 8)); + dest+=2; + npixels--; + + do + { val = src[1] - src[0]; + src++; + iCRC = CRC32_table[(unsigned char)iCRC ^ (unsigned char) src[0] ] ^ ((iCRC >> 8)); + iCRC = CRC32_table[(unsigned char)iCRC ^ (unsigned char)(src[0]>>8)] ^ ((iCRC >> 8)); + + if (val == 0) /* run length-encode zero differences */ + { for (i=2;; i++) + { if (i>=npixels || src[i-1]!=src[-1] || i==256) + { if (i<=MINZEROS) /* too short run -> write zeros */ + { for (j=0; j=254) /* maximum length 4 bit run */ + { dest = recompress4bit(n4bit, dest); + n4bit = 0; + } + } + } + else + { if (n4bit>=MIN4BIT) /* end (and write) 4 bit run */ + dest = recompress4bit(n4bit, dest); + + n4bit=0; + *dest++ = -128; // hexadecimal 0x80 + *dest++ = (signed char)(i-1); + } + + npixels -= (i-2); + for (j=0; j> 8)); + iCRC = CRC32_table[(unsigned char)iCRC ^ (unsigned char)(src[0]>>8)] ^ ((iCRC >> 8)); + } + break; + } + } + } + else if (val >= -63 && val <= 63) /* small difference coded as one byte */ + { if (val >= -8 && val <= 7) + { *dest++ = (signed char)val; + n4bit++; + + if(n4bit>=254) /* maximum length 4 bit run */ + { dest = recompress4bit(n4bit, dest); + n4bit=0; + } + } + else if(n4bit>=MIN4BIT) /* end and write 4 bit run */ + { j = val; + dest = recompress4bit(n4bit, dest); + n4bit=0; + *dest++ = (signed char)j; + } + else + { *dest++ = (signed char)val; /* end 4 bit run */ + n4bit = 0; + } + } + else if (val >= -0x3F00 && val <= 0x3EFF) /* large differences coded as two bytes */ + { j = val; + + if(n4bit>=MIN4BIT) /* end (and write) 4 bit run */ + dest = recompress4bit(n4bit, dest); + + n4bit=0; + dest[0] = (signed char)((j>>8) ^ 0x40); + dest[1] = (signed char)j; + dest+=2; + } + else /* if very large differences code abs val as three bytes */ + { j = src[0]; + + if(n4bit>=MIN4BIT) /* end (and write) 4 bit run */ + dest = recompress4bit(n4bit, dest); + + n4bit=0; + *dest++ = 0x7F; + *dest++ = (signed char)(j>>8); + *dest++ = (signed char)j; + } + /* Are we beyond the allocated memory? */ + if ((size_t)(dest - (signed char*)pHeader) > iBufferSize) + return 0; + } + while (--npixels); + + if ((dest - (signed char*)pHeader - iHeaderSize)iCompressedSize = + (unsigned int)(dest - (signed char*)pHeader - iHeaderSize); + else // store 64 bit number in extended structure + { pHeader_64bits->iCompressedSize = 0; + pHeader_64bits->i64CompressedSize = dest - (signed char*)pHeader -iHeaderSize; + } + + /* Pad it to get an even length */ + if (pHeader->iCompressedSize & 1) + { *dest++ = 0; + pHeader->iCompressedSize++; + } + + pHeader->iOrgCRC = iCRC; + pHeader->iCompressedCRC = 0; + } + + return dest - (signed char*)pHeader; +} + + +void clitk::XdrImageIO::WriteImage(const char* file, char* headerinfo, char* headerfile, int raw, + int offset, char bLittleEndian, int iNkiCompression, + int wcoords, int append, int getsize, char *tobuffer, const void* data) +{ AVSINT total=1; + int i; + AVSINT coords=0; + int f=0; + char temp[256]; + char *c; + char cSwap; + FILE *fp; + long swap_test = 0x1000000; + signed char* pCompressed = NULL; + size_t FilePos=0; + char **buffer = NULL; + int len=0; + char *buf2; + size_t slen; + + if (bLittleEndian) + swap_test = 0x00000001; + + if (getsize) + { swap_test = 0xffffffff; // never swap to save time + buffer = (char **) &len; + f = 1; + } + + if (tobuffer) + { buf2 = (char *)tobuffer; + buffer = &buf2; + f = 0; + } + + for (i=0; i 0) && + (GetComponentType() == itk::ImageIOBase::SHORT) && + (GetPixelType() == itk::ImageIOBase::SCALAR)) + { pCompressed = (signed char *)malloc((total/2) * 3 + sizeof(NKI_MODE2_64BITS) + 10); + if (pCompressed==NULL) + { iNkiCompression = 0; + AVSwarning("Avs_wxdr: not enough memory to compress data, saving uncompressed"); + } + } + + if (!(tobuffer || getsize)) + { if (offset != -1) + { f = open(file, O_RDWR, 0); + if (f < 0) + { + AVSerror("Avs_wxdr: Opening " << file << "failed.\n" << strerror(errno)); + free(pCompressed); + return AVS_ERROR; + } + lseek(f, offset, SEEK_SET); + } + else + { if (strlen(file)==0) + f = fileno(stdout); + else + { if (append) + f = open(file, O_RDWR | O_APPEND, 0); + else + f = creat(file, S_IWRITE | S_IREAD); + } + + if (f < 0) + { AVSerror("Avs_wxdr: Creating " << file << " failed.\n" << strerror(errno)); + free(pCompressed); + return AVS_ERROR; + } + } + } + + if (!raw) + { sprintf(temp, "# AVS wants to have the first line starting with its name\n"); + slen = strlen(temp); + + if (!checked_write(f, temp, slen, buffer)) + { free(pCompressed); + return AVS_ERROR; + } + FilePos += slen; + + slen = strlen(headerinfo); + if (slen && !checked_write(f, headerinfo, slen, buffer)) + { free(pCompressed); + return AVS_ERROR; + } + FilePos += slen; + + if (!checked_write(f, "\n", 1, buffer)) + { free(pCompressed); + return AVS_ERROR; + } + FilePos++; + + if (strlen(headerfile)) + { fp = fopen(headerfile, "rt"); + if (fp) + { for (;;) + { if (fgets(temp, 255, fp) == NULL) break; + slen = strlen(temp); + if (!checked_write(f, temp, slen, buffer)) + { fclose(fp); + free(pCompressed); + return AVS_ERROR; + } + FilePos += slen; + } + fclose(fp); + if (!checked_write(f, "\n", 1, buffer)) + { free(pCompressed); + return AVS_ERROR; + } + FilePos++; + } + } + + sprintf(temp, "ndim=%d\n", GetNumberOfDimensions()); + slen = strlen(temp); + if (!checked_write(f, temp, slen, buffer)) + { free(pCompressed); + return AVS_ERROR; + } + FilePos += slen; + } + + for (i=0; iuniform) + //{ case UNIFORM : + strcpy(temp, "field=uniform\n"); + coords = GetNumberOfDimensions() * 2; + // break; + // case RECTILINEAR : strcpy(temp, "field=rectilinear\n"); + // break; + // case IRREGULAR : strcpy(temp, "field=irregular\n"); + // coords = total * input->nspace; + // break; + // default : if (f != fileno(stdout)) close(f); + // free(pCompressed); + // return; + //} + + if (!raw) + { if (!checked_write(f, temp, strlen(temp), buffer)) + { free(pCompressed); + return AVS_ERROR; + } + + if ((iNkiCompression > 0) && + (GetComponentType() == itk::ImageIOBase::SHORT) && + (GetPixelType() == itk::ImageIOBase::SCALAR)) + { sprintf(temp, "nki_compression=%d", iNkiCompression); + if (!checked_write(f, temp, strlen(temp), buffer)) + { free(pCompressed); + return AVS_ERROR; + } + } + + temp[0] = temp[1] = 12; + if (!checked_write(f, temp, 2, buffer)) + { free(pCompressed); + return AVS_ERROR; + } + } + + total *= GetPixelSize(); + + if ((!raw) && (iNkiCompression > 0) && + (GetComponentType() == itk::ImageIOBase::SHORT) && + (GetPixelType() == itk::ImageIOBase::SCALAR)) + { size_t iCompressedLength; + + iCompressedLength = nki_private_compress(pCompressed, + (short int *)(data), total/2, iNkiCompression); + + if (iCompressedLength > 0) + { if (!checked_write(f, pCompressed, iCompressedLength, buffer)) + { free(pCompressed); + return AVS_ERROR; + } + free(pCompressed); + goto WRITE_COORDS; + } + + /* Compressionratio was poor: let's write uncompressed */ + iNkiCompression = 0; + total /= 2; + free(pCompressed); + pCompressed = NULL; + _lseeki64(f, (unsigned int)FilePos, SEEK_SET); // use _lseeki64 just in case header size > UINT_MAX bytes + goto ONCE_AGAIN; + } + + /* swap data if required (xdr is low-endian) */ + + if (!(*(char *)(&swap_test))) + { if (GetComponentSize()==2) + { c = (char *)data; + for (i=0; i - * @date Sun Jun 1 22:11:20 2008 - * - * @brief - * - * - */ - -#include "nkitkXDRImageIOFactory.h" - -//==================================================================== -nkitk::XDRImageIOFactory::XDRImageIOFactory() -{ - this->RegisterOverride("itkImageIOBase", - "XDRImageIO", - "XDR Image IO", - 1, - itk::CreateObjectFunction::New()); -} - - -#endif /* end #define NKITKXDRIMAGEIOFACTORY_CXX */ - diff --git a/vv/vv.cxx b/vv/vv.cxx index 9084295..fe19cc3 100644 --- a/vv/vv.cxx +++ b/vv/vv.cxx @@ -21,6 +21,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . =========================================================================*/ + #include #include #include @@ -29,15 +30,19 @@ along with this program. If not, see . #include #include -#include "clitkCommon.h" +#include "clitkIO.h" #include "vvMainWindow.h" -#include "vvInit.h" #include "vvConstants.h" //------------------------------------------------------------------------------ int main( int argc, char** argv ) { - initialize_IO(); +#ifdef _WIN32 + HWND hWnd = GetConsoleWindow(); + ShowWindow( hWnd, SW_HIDE ); +#endif + + CLITK_INIT; QApplication app( argc, argv ); Q_INIT_RESOURCE(vvIcons); diff --git a/vv/vvMainWindow.cxx b/vv/vvMainWindow.cxx index c252b6f..f55ebea 100644 --- a/vv/vvMainWindow.cxx +++ b/vv/vvMainWindow.cxx @@ -3,8 +3,8 @@ Program: vv Module: $RCSfile: vvMainWindow.cxx,v $ Language: C++ - Date: $Date: 2010/02/09 10:28:07 $ - Version: $Revision: 1.10 $ + Date: $Date: 2010/02/17 20:52:39 $ + Version: $Revision: 1.11 $ Author : Pierre Seroul (pierre.seroul@gmail.com) Copyright (C) 200COLUMN_IMAGE_NAME @@ -94,6 +94,8 @@ #define COLUMN_RELOAD_IMAGE 6 #define COLUMN_IMAGE_NAME 7 +#define EXTENSIONS "Images ( *.bmp *.png *.jpeg *.jpg *.tif *.mhd *.hdr *.vox *.his *.xdr)" + /*Data Tree values 0,Qt::UserRole full filename 1,Qt::CheckStateRole checkbutton UL View @@ -532,7 +534,7 @@ vvMainWindow::~vvMainWindow() { //------------------------------------------------------------------------------ void vvMainWindow::MergeImages() { - QString Extensions = "Images ( *.bmp *.png *.jpeg *.jpg *.tif *.mhd *.hdr *.vox)"; + QString Extensions = EXTENSIONS; Extensions += ";;All Files (*)"; QStringList files = QFileDialog::getOpenFileNames(this,tr("Merge Images"),mInputPathName,Extensions); if (files.isEmpty()) @@ -596,7 +598,7 @@ void vvMainWindow::MergeImages() { //------------------------------------------------------------------------------ void vvMainWindow::MergeImagesWithTime() { - QString Extensions = "Images ( *.bmp *.png *.jpeg *.jpg *.tif *.mhd *.hdr *.vox)"; + QString Extensions = EXTENSIONS; Extensions += ";;All Files (*)"; QStringList files = QFileDialog::getOpenFileNames(this,tr("Merge Images With Time"),mInputPathName,Extensions); if (files.isEmpty()) @@ -702,7 +704,7 @@ void vvMainWindow::OpenDicom() { //------------------------------------------------------------------------------ void vvMainWindow::OpenImages() { - QString Extensions = "Images ( *.bmp *.png *.jpeg *.jpg *.tif *.mhd *.hdr *.vox)"; + QString Extensions = EXTENSIONS; Extensions += ";;All Files (*)"; QStringList files = QFileDialog::getOpenFileNames(this,tr("Load Images"),mInputPathName,Extensions); @@ -726,7 +728,7 @@ void vvMainWindow::OpenRecentImage() //------------------------------------------------------------------------------ void vvMainWindow::OpenImageWithTime() { - QString Extensions = "Images ( *.bmp *.png *.jpeg *.jpg *.tif *.mhd *.hdr *.vox)"; + QString Extensions = EXTENSIONS; Extensions += ";;All Files (*)"; QStringList files = QFileDialog::getOpenFileNames(this,tr("Load Images With Time"),mInputPathName,Extensions); @@ -1819,7 +1821,7 @@ void vvMainWindow::SelectOverlayImage() { return; } - QString Extensions = "Images ( *.bmp *.png *.jpeg *.jpg *.tif *.mhd *.hdr *.vox)"; + QString Extensions = EXTENSIONS; Extensions += ";;All Files (*)"; QString file = QFileDialog::getOpenFileName(this,tr("Load Overlay image"),mInputPathName,Extensions); if (!file.isEmpty()) @@ -1912,7 +1914,7 @@ void vvMainWindow::AddFusionImage() return; } - QString Extensions = "Images ( *.bmp *.png *.jpeg *.jpg *.tif *.mhd *.hdr *.vox)"; + QString Extensions = EXTENSIONS; Extensions += ";;All Files (*)"; QString file = QFileDialog::getOpenFileName(this,tr("Load Fusion image"),mInputPathName,Extensions); if (!file.isEmpty()) diff --git a/vv/vvs.cxx b/vv/vvs.cxx index f23054b..fb76c42 100644 --- a/vv/vvs.cxx +++ b/vv/vvs.cxx @@ -26,7 +26,6 @@ along with this program. If not, see . #include "vvImageReader.h" #include "vvMeshReader.h" -#include "vvInit.h" #include "vvs.h"