From 616fe6757e5e27ce6ebb48e9e3bf3b42ab15be1c Mon Sep 17 00:00:00 2001 From: malaterre Date: Wed, 3 Nov 2004 18:08:55 +0000 Subject: [PATCH] ENH: Now the dictionary is compiled into gdcm lib. This is a default behavior, thus any dic file specified is picked before failback to the one comiled into lib --- CMakeLists.txt | 2 +- Dicts/CMakeLists.txt | 94 ++++++++++++++++++++++------- Dicts/gdcmDefaultDict.h.in | 1 - src/CMakeLists.txt | 3 +- src/gdcmDefaultDicts.cxx.in | 114 ++++++++++++++++++++++++++++++++++++ src/gdcmDicomDirElement.cxx | 108 +++++++++++++++++++++------------- src/gdcmDicomDirElement.h | 9 ++- src/gdcmDict.cxx | 44 ++++++++------ src/gdcmTS.cxx | 37 +++++++----- src/gdcmVR.cxx | 51 +++++++++------- 10 files changed, 338 insertions(+), 125 deletions(-) delete mode 100644 Dicts/gdcmDefaultDict.h.in create mode 100644 src/gdcmDefaultDicts.cxx.in diff --git a/CMakeLists.txt b/CMakeLists.txt index e8d8e351..fce4b7ad 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,8 +30,8 @@ MARK_AS_ADVANCED(LIBRARY_OUTPUT_PATH EXECUTABLE_OUTPUT_PATH) #----------------------------------------------------------------------------- # Build directory on which many applications depend SUBDIRS( - src Dicts + src ) #----------------------------------------------------------------------------- # Example subdir: diff --git a/Dicts/CMakeLists.txt b/Dicts/CMakeLists.txt index 708d6630..2d63e0b4 100644 --- a/Dicts/CMakeLists.txt +++ b/Dicts/CMakeLists.txt @@ -13,41 +13,68 @@ INSTALL_FILES(${GDCM_DATA_DIR} FILES # Since CONFIGURE_FILE do a 'copy if different' # We have to write the file here since is contains 'DICOM_DICTIONARY' # Which is a variable afterward... -#FILE(WRITE "/tmp/output.h.in" "@DICOM_DICTIONARY@" ) - -# Following code contributing by Andy Cedilnik -SET(INFILE "dicomV3.dic") - -FILE(READ "${INFILE}" ENT) +# Following code contributing by Andy Cedilnik (Kitware) +FILE(READ "dicomV3.dic" ENT) STRING(REGEX REPLACE "\n" ";" ENT "${ENT}") -SET(DICOM_DICTIONARY - "void FillDefault()\n{" -) +SET(DICOM_DATA_DICTIONARY "") FOREACH(line ${ENT}) STRING(REGEX REPLACE -# "^([0-9a-f][0-9a-f][0-9a-f][0-9a-f]) ([0-9a-f][0-9a-f][0-9a-f][0-9a-f]) ([A-Z]+) ([A-Z]+) (.*)([ \(RET\)]*)$" - # FIXME ? is not properly used, just luck "^([0-9a-f][0-9a-f][0-9a-f][0-9a-f]) ([0-9a-f][0-9a-f][0-9a-f][0-9a-f]) ([A-Z]+) ([A-Z?]+) (.*)$" -# " { \\1, \\2, \"\\3\" , \"\\4\" , \"\\5\", \"\\6\"}," nline "${line}") -# " { \\1, \\2, \"\\3\" , \"\\4\" , \"\\5\"}," nline "${line}") - " AddNewEntry( DictEntry ( 0x\\1, 0x\\2, \"\\3\" , \"\\4\" , \"\\5\"));" nline "${line}") - SET(DICOM_DICTIONARY "${DICOM_DICTIONARY}\n${nline}") + " 0x\\1, 0x\\2, \"\\3\" , \"\\4\" , \"\\5\", " nline "${line}") + SET(DICOM_DATA_DICTIONARY "${DICOM_DATA_DICTIONARY}\n${nline}") +ENDFOREACH(line) + +SET(DICOM_DATA_DICTIONARY "${DICOM_DATA_DICTIONARY} \n 0,0,0,0,0") + +FILE(READ "dicomTS.dic" ENT_TS) +STRING(REGEX REPLACE "\n" ";" ENT_TS "${ENT_TS}") +SET(DICOM_TS_DICTIONARY "") + +FOREACH(line ${ENT_TS}) + STRING(REGEX REPLACE + "^([0-9.]+) +(.*)$" + " ts[\"\\1\"] = \"\\2\"; " nline "${line}") + SET(DICOM_TS_DICTIONARY "${DICOM_TS_DICTIONARY}\n${nline}") +ENDFOREACH(line) + +FILE(READ "dicomVR.dic" ENT_VR) +STRING(REGEX REPLACE ";" "/" ENT_VR "${ENT_VR}") # CMake doesn't like ';' +STRING(REGEX REPLACE "\n" ";" ENT_VR "${ENT_VR}") +SET(DICOM_VR_DICTIONARY "") + +FOREACH(line ${ENT_VR}) + STRING(REGEX REPLACE + "^([A-Z][A-Z]) (.*)/ +//.*$" + " vr[\"\\1\"] = \"\\2\"; " nline "${line}") + SET(DICOM_VR_DICTIONARY "${DICOM_VR_DICTIONARY}\n${nline}") ENDFOREACH(line) -SET(DICOM_DICTIONARY "${DICOM_DICTIONARY} \n }") +FILE(READ "DicomDir.dic" ENT_DIR) +STRING(REGEX REPLACE "\\\\" "\\\\\\\\" ENT_DIR "${ENT_DIR}") +STRING(REGEX REPLACE ";" "/" ENT_DIR "${ENT_DIR}") # CMake doesn't like ';' +STRING(REGEX REPLACE "\n" ";" ENT_DIR "${ENT_DIR}") +SET(DICOM_DIR_DICTIONARY "") + +FOREACH(line ${ENT_DIR}) + STRING(REGEX REPLACE + "^(metaElem|patientElem|studyElem|serieElem|imageElem) *([a-f0-9]+) ([a-f0-9]+) [\"](.*)[\"](.*)$" + " \"\\1\" , 0x\\2 , 0x\\3 , \"\\4\", \\5" nline "${line}") + SET(DICOM_DIR_DICTIONARY "${DICOM_DIR_DICTIONARY}\n${nline}") +ENDFOREACH(line) + +SET(DICOM_DIR_DICTIONARY "${DICOM_DIR_DICTIONARY} \n 0,0,0,0") # See above -CONFIGURE_FILE("${CMAKE_CURRENT_SOURCE_DIR}/gdcmDefaultDict.h.in" "${GDCM_BINARY_DIR}/gdcmDefaultDict.h") +CONFIGURE_FILE("${GDCM_SOURCE_DIR}/src/gdcmDefaultDicts.cxx.in" "${GDCM_BINARY_DIR}/src/gdcmDefaultDicts.cxx" IMMEDIATE) -#MESSAGE("${DICOM_DICTIONARY}") -FILE(READ "dicomV3VM2004.dic" ENT) -STRING(REGEX REPLACE "\n" ";" ENT "${ENT}") +FILE(READ "dicomV3VM2004.dic" ENT2) +STRING(REGEX REPLACE "\n" ";" ENT2 "${ENT2}") SET(DICOM_DICTIONARY2 "") -FOREACH(line ${ENT}) +FOREACH(line ${ENT2}) STRING(REGEX REPLACE # "^([0-9a-f][0-9a-f][0-9a-f][0-9a-f]) ([0-9a-f][0-9a-f][0-9a-f][0-9a-f]) ([A-Z]+) ([A-Z]+) (.*)([ \(RET\)]*)$" "^([0-9a-f][0-9a-f][0-9a-f][0-9a-f]) ([0-9a-f][0-9a-f][0-9a-f][0-9a-f]) ([A-Z]+) ([0-9]|[1]-[0-9n]|[2]-[2][n]|[3]-[3][n]) (.*)$" @@ -56,5 +83,28 @@ FOREACH(line ${ENT}) SET(DICOM_DICTIONARY2 "${DICOM_DICTIONARY2}\n${nline}") ENDFOREACH(line) -#MESSAGE("${DICOM_DICTIONARY2}") + +#FILE(READ "04_06PU.txt" ENT3) +FILE(READ "extract.txt" ENT3) + +STRING(REGEX REPLACE "\n" ";" ENT3 "${ENT3}") +SET(DICOM_DICTIONARY3 "") + +FOREACH(line ${ENT3}) + STRING(REGEX REPLACE +# "^[(]([0-9a-fA-F]+),([0-9a-fA-F]+)[)] (.*) ([A-ZA-Z]*) ([[0-3][-][1-3n]*|[1-]*[0-9]+]*) ?([RET|Retired]*)$" + "^[(]([0-9a-fA-F]+),([0-9a-fA-F]+)[)] +(.*) +([A-ZA-Z [[0-3][-][1-3n]*|[1-]*[0-9]+]]*)([ RET]*)$" + " { \\1 , \\2, \\3, \\4, \\5 }," nline "${line}") + STRING(COMPARE NOTEQUAL "${line}" "${nline}" add) + IF(add) + #SET(DICOM_DICTIONARY3 "${DICOM_DICTIONARY3}\n${nline}") + MESSAGE("${nline}") + ELSE(add) + MESSAGE("WRONG:${nline}") + ENDIF(add) +ENDFOREACH(line) + + +# Txt generation: +# ~/Software/xpdf-3.00-linux/pdftotext -f 9 -l 81 -raw -nopgbrk 04_06PU.PDF 04_06PU-3.txt diff --git a/Dicts/gdcmDefaultDict.h.in b/Dicts/gdcmDefaultDict.h.in deleted file mode 100644 index 083dee8b..00000000 --- a/Dicts/gdcmDefaultDict.h.in +++ /dev/null @@ -1 +0,0 @@ -@DICOM_DICTIONARY@ diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 55ab9e21..5cfd632f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -4,7 +4,7 @@ SUBDIRS(jpeg) # "jpeglib.h" is defined here: INCLUDE_DIRECTORIES( - ${GDCM_SOURCE_DIR}/ + ${GDCM_SOURCE_DIR}/src ${GDCM_BINARY_DIR}/ #for gdcmConfigure.h ) @@ -47,6 +47,7 @@ SET(libgdcm_la_SOURCES gdcmUtil.cxx gdcmValEntry.cxx gdcmVR.cxx + ${GDCM_BINARY_DIR}/src/gdcmDefaultDicts.cxx ) ADD_LIBRARY(gdcm ${libgdcm_la_SOURCES} ) diff --git a/src/gdcmDefaultDicts.cxx.in b/src/gdcmDefaultDicts.cxx.in new file mode 100644 index 00000000..1b54f0b1 --- /dev/null +++ b/src/gdcmDefaultDicts.cxx.in @@ -0,0 +1,114 @@ +/*========================================================================= + + Program: gdcm + Module: $RCSfile: gdcmDefaultDicts.cxx.in,v $ + Language: C++ + Date: $Date: 2004/11/03 18:08:56 $ + Version: $Revision: 1.1 $ + + Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de + l'Image). All rights reserved. See Doc/License.txt or + http://www.creatis.insa-lyon.fr/Public/Gdcm/License.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 notices for more information. + +=========================================================================*/ + +#ifndef GDCMDEFAULTDICTS_H +#define GDCMDEFAULTDICTS_H + +#include "gdcmDict.h" +#include "gdcmDictEntry.h" +#include "gdcmTS.h" +#include "gdcmVR.h" +#include "gdcmDicomDirElement.h" + +namespace gdcm +{ + +typedef struct +{ + uint16_t group; + uint16_t element; + char *vr; + char *fourth; + char *name; +} DICT_ENTRY; + +static DICT_ENTRY datadir[] = { +@DICOM_DATA_DICTIONARY@ +}; + +void FillDefaultDataDict(Dict *d) +{ + unsigned int i = 0; + DICT_ENTRY n = datadir[i]; + while( n.name != 0 ) + { + const DictEntry e( n.group, n.element, n.vr, n.fourth, n.name); + d->AddNewEntry( e ); + n = datadir[++i]; + } +} + +void FillDefaultTSDict(TSHT & ts) +{ +@DICOM_TS_DICTIONARY@ +} + +void FillDefaultVRDict(VRHT & vr) +{ +@DICOM_VR_DICTIONARY@ +} + +typedef struct +{ + char *type; + unsigned short group; + unsigned short element; + char *value; +} ELEMENT; + +static ELEMENT dataelement[] = { +@DICOM_DIR_DICTIONARY@ +}; + +void FillDefaultDIRDict(DicomDirElement *dde) +{ + unsigned int i = 0; + ELEMENT e = dataelement[i]; + Element elem; + while( e.type != 0 ) + { + elem.Group = e.group; + elem.Elem = e.element; + elem.Value = e.value; + if( strcmp(e.type, "metaElem" ) == 0 ) + { + dde->GetDicomDirMetaElements().push_back(elem); + } + else if( strcmp(e.type, "patientElem" ) == 0 ) + { + dde->GetDicomDirPatientElements().push_back(elem); + } + else if( strcmp(e.type, "studyElem" ) == 0 ) + { + dde->GetDicomDirStudyElements().push_back(elem); + } + else if( strcmp(e.type, "serieElem" ) == 0 ) + { + dde->GetDicomDirSerieElements().push_back(elem); + } + else if( strcmp(e.type, "imageElem" ) == 0 ) + { + dde->GetDicomDirImageElements().push_back(elem); + } + e = dataelement[++i]; + } +} + +} //end gdcm namespace +#endif + diff --git a/src/gdcmDicomDirElement.cxx b/src/gdcmDicomDirElement.cxx index aefcd12d..65c05e51 100644 --- a/src/gdcmDicomDirElement.cxx +++ b/src/gdcmDicomDirElement.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmDicomDirElement.cxx,v $ Language: C++ - Date: $Date: 2004/10/12 04:35:44 $ - Version: $Revision: 1.20 $ + Date: $Date: 2004/11/03 18:08:56 $ + Version: $Revision: 1.21 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -23,9 +23,10 @@ #include #include + namespace gdcm { - +void FillDefaultDIRDict(DicomDirElement *dde); //----------------------------------------------------------------------------- // Constructor / Destructor @@ -35,57 +36,51 @@ namespace gdcm */ DicomDirElement::DicomDirElement() { - std::string filename = DictSet::BuildDictPath() + std::string(DICT_ELEM); + std::string filename = DictSet::BuildDictPath() + DICT_ELEM; std::ifstream from(filename.c_str()); - dbg.Error(!from, "DicomDirElement::DicomDirElement: can't open dictionary", - filename.c_str()); - - char buff[1024]; - std::string type; - Element elem; - - while (!from.eof()) + if(!from) { - from >> std::ws; - from.getline(buff, 1024, ' '); - type = buff; + dbg.Verbose(2, + "DicomDirElement::DicomDirElement: can't open dictionary", + filename.c_str()); + FillDefaultDIRDict( this ); + } + else + { + char buff[1024]; + std::string type; + Element elem; - if( (type=="metaElem") || (type=="patientElem") || - (type=="studyElem") || (type=="serieElem") || - (type=="imageElem") ) + while (!from.eof()) { - from >> std::hex >> elem.Group >> elem.Elem; - - from >> std::ws; - from.getline(buff, 1024, '"'); from >> std::ws; - from.getline(buff, 1024, '"'); - elem.Value = buff; + from.getline(buff, 1024, ' '); + type = buff; - if( type == "metaElem" ) + if( (type=="metaElem") || (type=="patientElem") || + (type=="studyElem") || (type=="serieElem") || + (type=="imageElem") ) { - DicomDirMetaList.push_back(elem); + from >> std::hex >> elem.Group >> elem.Elem; + + from >> std::ws; + from.getline(buff, 1024, '"'); + from >> std::ws; + from.getline(buff, 1024, '"'); + elem.Value = buff; + + AddNewEntry(type, elem); } - else if( type == "patientElem" ) + else { - DicomDirPatientList.push_back(elem); - } - else if( type == "studyElem" ) - { - DicomDirStudyList.push_back(elem); - } - else if( type == "serieElem" ) - { - DicomDirSerieList.push_back(elem); - } - else if( type == "imageElem" ) - { - DicomDirImageList.push_back(elem); + dbg.Error("DicomDirElement::DicomDirElement: Error parsing file", + filename.c_str()); + dbg.Error("Type", type.c_str(), " is not registered as valid" ); } + from.getline(buff, 1024, '\n'); } - from.getline(buff, 1024, '\n'); - } from.close(); + } } /** @@ -157,6 +152,35 @@ void DicomDirElement::Print(std::ostream &os) //----------------------------------------------------------------------------- // Public +bool DicomDirElement::AddNewEntry(std::string const & type, + Element const & elem) +{ + if( type == "metaElem" ) + { + DicomDirMetaList.push_back(elem); + } + else if( type == "patientElem" ) + { + DicomDirPatientList.push_back(elem); + } + else if( type == "studyElem" ) + { + DicomDirStudyList.push_back(elem); + } + else if( type == "serieElem" ) + { + DicomDirSerieList.push_back(elem); + } + else if( type == "imageElem" ) + { + DicomDirImageList.push_back(elem); + } + else + { + return false; + } + return true; +} //----------------------------------------------------------------------------- // Protected diff --git a/src/gdcmDicomDirElement.h b/src/gdcmDicomDirElement.h index 9c739802..07bf7db3 100644 --- a/src/gdcmDicomDirElement.h +++ b/src/gdcmDicomDirElement.h @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmDicomDirElement.h,v $ Language: C++ - Date: $Date: 2004/10/25 04:08:20 $ - Version: $Revision: 1.14 $ + Date: $Date: 2004/11/03 18:08:56 $ + Version: $Revision: 1.15 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -102,6 +102,11 @@ public: ListDicomDirImageElem const & GetDicomDirImageElements() const { return DicomDirImageList; }; + /** + * Public method to add an element + */ + bool AddNewEntry(std::string const & type, Element const & elem); + private: /// Elements chained list, related to the MetaElements of DICOMDIR ListDicomDirMetaElem DicomDirMetaList; diff --git a/src/gdcmDict.cxx b/src/gdcmDict.cxx index 7155f6ef..051d44de 100644 --- a/src/gdcmDict.cxx +++ b/src/gdcmDict.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmDict.cxx,v $ Language: C++ - Date: $Date: 2004/10/28 19:53:28 $ - Version: $Revision: 1.50 $ + Date: $Date: 2004/11/03 18:08:56 $ + Version: $Revision: 1.51 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -26,7 +26,7 @@ namespace gdcm { - +void FillDefaultDataDict(Dict *d); //----------------------------------------------------------------------------- // Constructor / Destructor /** @@ -42,25 +42,31 @@ Dict::Dict(std::string const & filename) TagName name; std::ifstream from( filename.c_str() ); - dbg.Error(!from, "Dict::Dict: can't open dictionary", - filename.c_str()); - - while (!from.eof()) + if( !from ) { - from >> std::hex; - from >> group; - from >> element; - from >> vr; - from >> fourth; - from >> std::ws; //remove white space - std::getline(from, name); - - DictEntry newEntry(group, element, vr, fourth, name); - AddNewEntry(newEntry); + dbg.Verbose(2,"Dict::Dict: can't open dictionary", filename.c_str()); + // Using default embeded one: + FillDefaultDataDict( this ); } - from.close(); + else + { + while (!from.eof()) + { + from >> std::hex; + from >> group; + from >> element; + from >> vr; + from >> fourth; + from >> std::ws; //remove white space + std::getline(from, name); + + const DictEntry newEntry(group, element, vr, fourth, name); + AddNewEntry(newEntry); + } + from.close(); - Filename = filename; + Filename = filename; + } } /** diff --git a/src/gdcmTS.cxx b/src/gdcmTS.cxx index 15645c5f..26c672df 100644 --- a/src/gdcmTS.cxx +++ b/src/gdcmTS.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmTS.cxx,v $ Language: C++ - Date: $Date: 2004/10/27 01:32:15 $ - Version: $Revision: 1.27 $ + Date: $Date: 2004/11/03 18:08:56 $ + Version: $Revision: 1.28 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -27,29 +27,36 @@ namespace gdcm { +void FillDefaultTSDict(TSHT & ts); //----------------------------------------------------------------------------- // Constructor / Destructor TS::TS() { std::string filename = DictSet::BuildDictPath() + DICT_TS; std::ifstream from(filename.c_str()); - dbg.Error(!from, "TS::TS: can't open dictionary", filename.c_str()); - - TSKey key; - TSAtr name; - - while (!from.eof()) + if( !from ) { - from >> key; - from >> std::ws; - std::getline(from, name); - - if(key != "") + dbg.Verbose(2, "TS::TS: can't open dictionary", filename.c_str()); + FillDefaultTSDict( TsMap ); + } + else + { + TSKey key; + TSAtr name; + + while (!from.eof()) { - TsMap[key] = name; + from >> key; + from >> std::ws; + std::getline(from, name); + + if(key != "") + { + TsMap[key] = name; + } } + from.close(); } - from.close(); } //----------------------------------------------------------------------------- diff --git a/src/gdcmVR.cxx b/src/gdcmVR.cxx index 17c85d4f..02eb2c89 100644 --- a/src/gdcmVR.cxx +++ b/src/gdcmVR.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmVR.cxx,v $ Language: C++ - Date: $Date: 2004/10/27 22:31:12 $ - Version: $Revision: 1.21 $ + Date: $Date: 2004/11/03 18:08:56 $ + Version: $Revision: 1.22 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -26,6 +26,7 @@ namespace gdcm { +void FillDefaultVRDict(VRHT & vr); //----------------------------------------------------------------------------- /** * \brief Constructor @@ -34,30 +35,36 @@ VR::VR() { std::string filename = DictSet::BuildDictPath() + DICT_VR; std::ifstream from(filename.c_str()); - dbg.Error(!from, "VR::VR: can't open dictionary", filename.c_str()); - - char buff[1024]; - VRKey key; - VRAtr name; - - while (!from.eof()) + if(!from) { - from >> std::ws; - from.getline(buff, 1024, ' '); - key = buff; - from >> std::ws; - from.getline(buff, 1024, ';'); - name = buff; - - from >> std::ws; - from.getline(buff, 1024, '\n'); - - if(key != "") + dbg.Verbose(2, "VR::VR: can't open dictionary", filename.c_str()); + FillDefaultVRDict(vr); + } + else + { + char buff[1024]; + VRKey key; + VRAtr name; + + while (!from.eof()) { - vr[key] = name; + from >> std::ws; + from.getline(buff, 1024, ' '); + key = buff; + from >> std::ws; + from.getline(buff, 1024, ';'); + name = buff; + + from >> std::ws; + from.getline(buff, 1024, '\n'); + + if(key != "") + { + vr[key] = name; + } } + from.close(); } - from.close(); } //----------------------------------------------------------------------------- -- 2.45.1