From 559c3639fc5b255b36a951f352db1addead9f78f Mon Sep 17 00:00:00 2001 From: malaterre Date: Wed, 3 Nov 2004 20:52:12 +0000 Subject: [PATCH] ENH: /binary_write/ gdcm source. Now even on big endian we are writting little endian. This should -heopfully- fix some tests --- ChangeLog | 4 ++++ src/gdcmBinEntry.cxx | 5 +++-- src/gdcmCommon.h | 5 +++-- src/gdcmDefaultDicts.cxx.in | 4 ++-- src/gdcmDicomDir.cxx | 23 +++++++++++++++-------- src/gdcmDocEntry.cxx | 34 +++++++++++++++++----------------- src/gdcmFile.cxx | 12 ++++++------ src/gdcmSQItem.cxx | 17 ++++++++++++----- src/gdcmSeqEntry.cxx | 10 +++++----- src/gdcmUtil.cxx | 9 +++++++-- src/gdcmUtil.h | 11 +++++++++-- src/gdcmValEntry.cxx | 12 ++++++------ 12 files changed, 89 insertions(+), 57 deletions(-) diff --git a/ChangeLog b/ChangeLog index 07c83360..58061aac 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2004-11-03 Mathieu Malaterre + * /binary_write/ gdcm source. Now even on big endian we are writting + little endian. This should -heopfully- fix some tests + 2004-11-03 Mathieu Malaterre * Now the dictionary is compiled into gdcm lib. This is a default behavior, thus any dic file specified is picked before failback to diff --git a/src/gdcmBinEntry.cxx b/src/gdcmBinEntry.cxx index b560cc2c..f6645e7b 100644 --- a/src/gdcmBinEntry.cxx +++ b/src/gdcmBinEntry.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmBinEntry.cxx,v $ Language: C++ - Date: $Date: 2004/10/22 03:05:40 $ - Version: $Revision: 1.34 $ + Date: $Date: 2004/11/03 20:52:12 $ + Version: $Revision: 1.35 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -111,6 +111,7 @@ void BinEntry::Write(std::ofstream* fp, FileType filetype) { // there is a 'non string' LUT, overlay, etc fp->write ( (char*)binArea, lgr ); // Elem value + //assert( strlen((char*)binArea) == lgr ); } else diff --git a/src/gdcmCommon.h b/src/gdcmCommon.h index e180d3cb..5d1c9b10 100644 --- a/src/gdcmCommon.h +++ b/src/gdcmCommon.h @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmCommon.h,v $ Language: C++ - Date: $Date: 2004/11/02 03:10:32 $ - Version: $Revision: 1.37 $ + Date: $Date: 2004/11/03 20:52:13 $ + Version: $Revision: 1.38 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -81,6 +81,7 @@ typedef unsigned int uint32_t; #endif #include +#include namespace gdcm { diff --git a/src/gdcmDefaultDicts.cxx.in b/src/gdcmDefaultDicts.cxx.in index 63db2001..727be345 100644 --- a/src/gdcmDefaultDicts.cxx.in +++ b/src/gdcmDefaultDicts.cxx.in @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmDefaultDicts.cxx.in,v $ Language: C++ - Date: $Date: 2004/11/03 19:35:49 $ - Version: $Revision: 1.2 $ + Date: $Date: 2004/11/03 20:52:13 $ + Version: $Revision: 1.3 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or diff --git a/src/gdcmDicomDir.cxx b/src/gdcmDicomDir.cxx index 5c264bf2..b7f4cb00 100644 --- a/src/gdcmDicomDir.cxx +++ b/src/gdcmDicomDir.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmDicomDir.cxx,v $ Language: C++ - Date: $Date: 2004/10/25 04:47:43 $ - Version: $Revision: 1.76 $ + Date: $Date: 2004/11/03 20:52:13 $ + Version: $Revision: 1.77 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -340,6 +340,7 @@ void DicomDir::SetEndMethodArgDelete(Method* method) bool DicomDir::WriteDicomDir(std::string const& fileName) { + int i; uint16_t sq[4] = { 0x0004, 0x1220, 0xffff, 0xffff }; uint16_t sqt[4]= { 0xfffe, 0xe0dd, 0xffff, 0xffff }; @@ -347,20 +348,23 @@ bool DicomDir::WriteDicomDir(std::string const& fileName) std::ios::out | std::ios::binary); if( !fp ) { - printf("Failed to open(write) File [%s] \n", fileName.c_str()); + dbg.Verbose(2, "Failed to open(write) File: ", fileName.c_str()); return false; } - uint8_t filePreamble[128]; + char filePreamble[128]; memset(filePreamble, 0, 128); - fp->write((char*)filePreamble, 128); - fp->write("DICM",4); + fp->write(filePreamble, 128); //FIXME + binary_write( *fp, "DICM"); DicomDirMeta *ptrMeta = GetDicomDirMeta(); ptrMeta->Write(fp, ExplicitVR); // force writing 0004|1220 [SQ ], that CANNOT exist within DicomDirMeta - fp->write((char*)&sq[0],8); + for(i=0;i<4;++i) + { + binary_write(*fp, sq[i]); + } for(ListDicomDirPatient::iterator cc = Patients.begin(); cc != Patients.end(); @@ -370,7 +374,10 @@ bool DicomDir::WriteDicomDir(std::string const& fileName) } // force writing Sequence Delimitation Item - fp->write((char*)&sqt[0],8); // fffe e0dd ffff ffff + for(i=0;i<4;++i) + { + binary_write(*fp, sqt[i]); // fffe e0dd ffff ffff + } fp->close(); return true; diff --git a/src/gdcmDocEntry.cxx b/src/gdcmDocEntry.cxx index dce3028c..0777f253 100644 --- a/src/gdcmDocEntry.cxx +++ b/src/gdcmDocEntry.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmDocEntry.cxx,v $ Language: C++ - Date: $Date: 2004/10/22 03:05:41 $ - Version: $Revision: 1.28 $ + Date: $Date: 2004/11/03 20:52:13 $ + Version: $Revision: 1.29 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -137,9 +137,9 @@ void DocEntry::Write(std::ofstream* fp, FileType filetype) // // ----------- Writes the common part // - fp->write ((char*) &group,(size_t)2 ); //group - fp->write ( (char*)&el, (size_t)2 ); //element - + binary_write( *fp, group); //group + binary_write( *fp, el); //element + if ( filetype == ExplicitVR ) { // Special case of delimiters: @@ -155,8 +155,8 @@ void DocEntry::Write(std::ofstream* fp, FileType filetype) // TODO : verify if the Sequence Delimitor Item was forced during Parsing - int ff = 0xffffffff; - fp->write ((char*)&ff,(size_t)4 ); + uint32_t ff = 0xffffffff; + binary_write(*fp, ff); return; } @@ -167,32 +167,32 @@ void DocEntry::Write(std::ofstream* fp, FileType filetype) { // Unknown was 'written' // deal with Little Endian - fp->write ( (char*)&shortLgr,(size_t)2 ); - fp->write ( (char*)&z, (size_t)2 ); + binary_write(*fp, shortLgr); + binary_write(*fp, z); } else { - fp->write (vr.c_str(),(size_t)2 ); - + binary_write(*fp, vr); + assert( vr.size() == 2 ); if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") || (vr == "UN") ) { - fp->write ( (char*)&z, (size_t)2 ); + binary_write(*fp, z); if (vr == "SQ") { // we set SQ length to ffffffff // and we shall write a Sequence Delimitor Item // at the end of the Sequence! - fp->write ( (char*)&ffff,(size_t)4 ); + binary_write(*fp, ffff); } else { - fp->write ( (char*)&lgr,(size_t)4 ); + binary_write(*fp, lgr); } } else { - fp->write ( (char*)&shortLgr,(size_t)2 ); + binary_write(*fp, shortLgr); } } } @@ -200,11 +200,11 @@ void DocEntry::Write(std::ofstream* fp, FileType filetype) { if (vr == "SQ") { - fp->write ( (char*)&ffff,(size_t)4 ); + binary_write(*fp, ffff); } else { - fp->write ( (char*)&lgr,(size_t)4 ); + binary_write(*fp, lgr); } } } diff --git a/src/gdcmFile.cxx b/src/gdcmFile.cxx index f044feb1..0b9fe2db 100644 --- a/src/gdcmFile.cxx +++ b/src/gdcmFile.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmFile.cxx,v $ Language: C++ - Date: $Date: 2004/10/28 03:10:58 $ - Version: $Revision: 1.150 $ + Date: $Date: 2004/11/03 20:52:13 $ + Version: $Revision: 1.151 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -558,7 +558,7 @@ bool File::WriteRawData(std::string const & fileName) std::ofstream fp1(fileName.c_str(), std::ios::out | std::ios::binary ); if (!fp1) { - printf("Fail to open (write) file [%s] \n", fileName.c_str()); + dbg.Verbose(2, "Fail to open (write) file:", fileName.c_str()); return false; } fp1.write((char*)Pixel_Data, ImageDataSize); @@ -634,16 +634,16 @@ bool File::WriteBase (std::string const & fileName, FileType type) std::ios::out | std::ios::binary); if (fp1 == NULL) { - printf("Failed to open (write) File [%s] \n", fileName.c_str()); + dbg.Verbose(2, "Failed to open (write) File: " , fileName.c_str()); return false; } if ( type == ImplicitVR || type == ExplicitVR ) { // writing Dicom File Preamble - uint8_t filePreamble[128]; + char filePreamble[128]; memset(filePreamble, 0, 128); - fp1->write((char*)filePreamble, 128); + fp1->write(filePreamble, 128); fp1->write("DICM", 4); } diff --git a/src/gdcmSQItem.cxx b/src/gdcmSQItem.cxx index 329a5a6d..33299697 100644 --- a/src/gdcmSQItem.cxx +++ b/src/gdcmSQItem.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmSQItem.cxx,v $ Language: C++ - Date: $Date: 2004/10/28 03:10:58 $ - Version: $Revision: 1.33 $ + Date: $Date: 2004/11/03 20:52:13 $ + Version: $Revision: 1.34 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -98,12 +98,16 @@ SQItem::~SQItem() */ void SQItem::Write(std::ofstream* fp, FileType filetype) { + int i; uint16_t item[4] = { 0xfffe, 0xe000, 0xffff, 0xffff }; uint16_t itemt[4]= { 0xfffe, 0xe00d, 0xffff, 0xffff }; //we force the writting of an 'Item' Start Element // because we want to write the Item as a 'no Length' item - fp->write((char*)&item[0],8); // fffe e000 ffff ffff + for(i=0;i<4;++i) + { + binary_write( *fp, item[i]); // fffe e000 ffff ffff + } for (ListDocEntry::iterator i = DocEntries.begin(); i != DocEntries.end(); @@ -129,8 +133,11 @@ void SQItem::Write(std::ofstream* fp, FileType filetype) //we force the writting of an 'Item Delimitation' item // because we wrote the Item as a 'no Length' item - fp->write((char*)&itemt[0],8); // fffe e000 ffff ffff - + for(i=0;i<4;++i) + { + binary_write( *fp, itemt[i]); // fffe e000 ffff ffff + } + } //----------------------------------------------------------------------------- diff --git a/src/gdcmSeqEntry.cxx b/src/gdcmSeqEntry.cxx index 88e79a62..b5d452c7 100644 --- a/src/gdcmSeqEntry.cxx +++ b/src/gdcmSeqEntry.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmSeqEntry.cxx,v $ Language: C++ - Date: $Date: 2004/10/25 03:03:45 $ - Version: $Revision: 1.33 $ + Date: $Date: 2004/11/03 20:52:13 $ + Version: $Revision: 1.34 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -138,9 +138,9 @@ void SeqEntry::Write(std::ofstream* fp, FileType filetype) // we force the writting of a Sequence Delimitation item // because we wrote the Sequence as a 'no Length' sequence - fp->write ( (char*)&seq_term_gr,(size_t)2 ); - fp->write ( (char*)&seq_term_el,(size_t)2 ); - fp->write ( (char*)&seq_term_lg,(size_t)4 ); + binary_write(*fp, seq_term_gr); + binary_write(*fp, seq_term_el); + binary_write(*fp, seq_term_lg); } //----------------------------------------------------------------------------- diff --git a/src/gdcmUtil.cxx b/src/gdcmUtil.cxx index 889ece20..f8dbe313 100644 --- a/src/gdcmUtil.cxx +++ b/src/gdcmUtil.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmUtil.cxx,v $ Language: C++ - Date: $Date: 2004/10/28 23:10:25 $ - Version: $Revision: 1.58 $ + Date: $Date: 2004/11/03 20:52:13 $ + Version: $Revision: 1.59 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -225,6 +225,11 @@ std::ostream& binary_write(std::ostream& os, const char* val) return os.write(val, strlen(val)); } +std::ostream& binary_write(std::ostream& os, std::string const & val) +{ + return os.write(val.c_str(), val.size()); +} + } // end namespace gdcm diff --git a/src/gdcmUtil.h b/src/gdcmUtil.h index 6668b803..0d3334fe 100644 --- a/src/gdcmUtil.h +++ b/src/gdcmUtil.h @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmUtil.h,v $ Language: C++ - Date: $Date: 2004/10/27 22:58:06 $ - Version: $Revision: 1.38 $ + Date: $Date: 2004/11/03 20:52:13 $ + Version: $Revision: 1.39 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -48,7 +48,14 @@ public: static std::string NormalizePath(std::string const & name); static std::string GetPath(std::string const &fullName); static std::string GetName(std::string const &fullName); + }; + template + std::ostream& binary_write(std::ostream& os, const T& val); + std::ostream& binary_write(std::ostream& os, const uint16_t& val); + std::ostream& binary_write(std::ostream& os, const uint32_t& val); + std::ostream& binary_write(std::ostream& os, const char* val); + std::ostream& binary_write(std::ostream& os, std::string const & val); } // end namespace gdcm //----------------------------------------------------------------------------- #endif diff --git a/src/gdcmValEntry.cxx b/src/gdcmValEntry.cxx index 9f337bde..6293e5dc 100644 --- a/src/gdcmValEntry.cxx +++ b/src/gdcmValEntry.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmValEntry.cxx,v $ Language: C++ - Date: $Date: 2004/10/22 03:05:42 $ - Version: $Revision: 1.31 $ + Date: $Date: 2004/11/03 20:52:13 $ + Version: $Revision: 1.32 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -200,8 +200,7 @@ void ValEntry::Write(std::ofstream* fp, FileType filetype) for (unsigned int i=0; iwrite ( (char*)ptr,(size_t)2); + binary_write( *fp, val_uint16); } tokens.clear(); return; @@ -218,14 +217,15 @@ void ValEntry::Write(std::ofstream* fp, FileType filetype) for (unsigned int i=0; iwrite ( (char*)ptr,(size_t)4 ); + binary_write( *fp, val_uint32); } tokens.clear(); return; } fp->write (GetValue().c_str(), (size_t)lgr ); // Elem value +// assert( lgr == GetValue().size() ); // FIXME ????? +// dbg.Assert(2, lgr == strlen(GetValue().c_str()), "Should be equal" ); } //----------------------------------------------------------------------------- -- 2.45.1