From: tbaudier Date: Tue, 10 Jul 2018 13:27:03 +0000 (+0200) Subject: Add clitkChangeDicomTag tool X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=commitdiff_plain;h=ae82902c0da980e348d998c453f6c83505780d1a;p=clitk.git Add clitkChangeDicomTag tool --- diff --git a/common/clitkChangeDicomTagGenericFilter.h b/common/clitkChangeDicomTagGenericFilter.h new file mode 100644 index 0000000..9780446 --- /dev/null +++ b/common/clitkChangeDicomTagGenericFilter.h @@ -0,0 +1,112 @@ +/*========================================================================= + Program: vv http://www.creatis.insa-lyon.fr/rio/vv + + Authors belong to: + - University of LYON http://www.universite-lyon.fr/ + - Léon Bérard cancer center http://www.centreleonberard.fr + - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the copyright notices for more information. + + It is distributed under dual licence + + - BSD See included LICENSE.txt file + - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html +===========================================================================**/ +#ifndef clitkChangeDicomTagGenericFilter_h +#define clitkChangeDicomTagGenericFilter_h + +/* ================================================= + * @file clitkChangeDicomTagGenericFilter.h + * @author + * @date + * + * @brief + * + ===================================================*/ + + +// clitk include +#include "clitkChangeDicomTag_ggo.h" + +//itk include +#include "itkGDCMImageIO.h" +#include "itkMetaDataDictionary.h" +#include "itkGDCMSeriesFileNames.h" +#include +#include + +namespace clitk +{ + template + class ITK_EXPORT ChangeDicomTagGenericFilter : public itk::LightObject + { + public: + //---------------------------------------- + // ITK + //---------------------------------------- + typedef ChangeDicomTagGenericFilter Self; + typedef itk::LightObject Superclass; + typedef itk::SmartPointer Pointer; + typedef itk::SmartPointer ConstPointer; + + // Method for creation through the object factory + itkNewMacro(Self); + + // Run-time type information (and related methods) + itkTypeMacro( ChangeDicomTagGenericFilter, LightObject ); + + + //---------------------------------------- + // Typedefs + //---------------------------------------- + + + //---------------------------------------- + // Set & Get + //---------------------------------------- + void SetArgsInfo(const args_info_type & a) + { + m_ArgsInfo=a; + m_Verbose=m_ArgsInfo.verbose_flag; + } + + + //---------------------------------------- + // Update + //---------------------------------------- + void Update(); + + protected: + + //---------------------------------------- + // Constructor & Destructor + //---------------------------------------- + ChangeDicomTagGenericFilter(); + ~ChangeDicomTagGenericFilter() {}; + + + //---------------------------------------- + // Templated members + //---------------------------------------- + void UpdateWithDimAndPixelType(); + + + //---------------------------------------- + // Data members + //---------------------------------------- + args_info_type m_ArgsInfo; + bool m_Verbose; + + }; + + +} // end namespace clitk + +#ifndef ITK_MANUAL_INSTANTIATION +#include "clitkChangeDicomTagGenericFilter.txx" +#endif + +#endif // #define clitkChangeDicomTagGenericFilter_h diff --git a/common/clitkChangeDicomTagGenericFilter.txx b/common/clitkChangeDicomTagGenericFilter.txx new file mode 100644 index 0000000..0bb9882 --- /dev/null +++ b/common/clitkChangeDicomTagGenericFilter.txx @@ -0,0 +1,122 @@ +/*========================================================================= + Program: vv http://www.creatis.insa-lyon.fr/rio/vv + + Authors belong to: + - University of LYON http://www.universite-lyon.fr/ + - Léon Bérard cancer center http://www.centreleonberard.fr + - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the copyright notices for more information. + + It is distributed under dual licence + + - BSD See included LICENSE.txt file + - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html +===========================================================================**/ +#ifndef clitkChangeDicomTagGenericFilter_txx +#define clitkChangeDicomTagGenericFilter_txx + +/* ================================================= + * @file clitkChangeDicomTagGenericFilter.txx + * @author + * @date + * + * @brief + * + ===================================================*/ + +// clitk +#include "clitkResampleImageWithOptionsFilter.h" +#if GDCM_MAJOR_VERSION >= 2 +#include "gdcmUIDGenerator.h" +#include +#include +#include +#include +#include +#include +#else +#include "gdcmFile.h" +#include "gdcmUtil.h" +#endif + + +namespace clitk +{ + + +//----------------------------------------------------------- +// Constructor +//----------------------------------------------------------- +template +ChangeDicomTagGenericFilter::ChangeDicomTagGenericFilter() +{ + m_Verbose=false; +} + + +//----------------------------------------------------------- +// Update +//----------------------------------------------------------- +template +void ChangeDicomTagGenericFilter::Update() +{ + UpdateWithDimAndPixelType(); +} + +//------------------------------------------------------------------- +// Update with the number of dimensions and the pixeltype read from +// the dicom files. The MHD files may be resampled to match the +// dicom spacing (and number of slices). Rounding errors in resampling +// are handled by removing files when generating the output dicom +// series. +//------------------------------------------------------------------- +template +void +ChangeDicomTagGenericFilter::UpdateWithDimAndPixelType() +{ + //Read the dicom file + gdcm::Reader reader; + reader.SetFileName( m_ArgsInfo.input_arg ); + reader.Read(); + gdcm::File &fileOutput = reader.GetFile(); + gdcm::DataSet &dsOutput = fileOutput.GetDataSet(); + const unsigned int ptr_len = 42; + char *ptr = new char[ptr_len]; + memset(ptr,0,ptr_len); + + unsigned int numberOfKeysGiven=m_ArgsInfo.key_given; + // For all input tag + for (unsigned int i = 0; i < numberOfKeysGiven; i++) { + // Split the tag element separated with '|' in 2 parts + char* copy = (char*)m_ArgsInfo.key_arg[i]; + std::vector v; + char* chars_array = strtok(copy, "|"); + while(chars_array) { + v.push_back(chars_array); + chars_array = strtok(NULL, "|"); + } + std::stringstream str0(v[0]), str1(v[1]); + int first, second; + str0 >> first; + str1 >> second; + + //Change the tag element by the corresponding value + gdcm::DataElement tagDE( gdcm::Tag(first, second) ); + std::string value( m_ArgsInfo.tag_arg[i] ); + tagDE.SetByteValue(value.c_str(), (uint32_t)strlen(value.c_str())); + dsOutput.Insert(tagDE); + } + + //Write the output dicom file + gdcm::Writer w; + w.SetFile( fileOutput ); + w.SetFileName( m_ArgsInfo.output_arg ); + w.Write(); +} + +}//end clitk + +#endif //#define clitkChangeDicomTagGenericFilter_txx diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 2111f11..f7a2f36 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -100,6 +100,11 @@ if(CLITK_BUILD_TOOLS) target_link_libraries(clitkWriteDicomSeries clitkCommon ) set(TOOLS_INSTALL ${TOOLS_INSTALL} clitkWriteDicomSeries) + WRAP_GGO(clitkChangeDicomTag_GGO_C clitkChangeDicomTag.ggo) + add_executable(clitkChangeDicomTag clitkChangeDicomTag.cxx ${clitkChangeDicomTag_GGO_C}) + target_link_libraries(clitkChangeDicomTag clitkCommon ) + set(TOOLS_INSTALL ${TOOLS_INSTALL} clitkChangeDicomTag) + WRAP_GGO(clitkGateSimulation2Dicom_GGO_C clitkGateSimulation2Dicom.ggo) add_executable(clitkGateSimulation2Dicom clitkGateSimulation2Dicom.cxx ${clitkGateSimulation2Dicom_GGO_C}) target_link_libraries(clitkGateSimulation2Dicom clitkCommon ) diff --git a/tools/clitkChangeDicomTag.cxx b/tools/clitkChangeDicomTag.cxx new file mode 100644 index 0000000..17b65ed --- /dev/null +++ b/tools/clitkChangeDicomTag.cxx @@ -0,0 +1,53 @@ +/*========================================================================= + Program: vv http://www.creatis.insa-lyon.fr/rio/vv + + Authors belong to: + - University of LYON http://www.universite-lyon.fr/ + - Léon Bérard cancer center http://www.centreleonberard.fr + - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the copyright notices for more information. + + It is distributed under dual licence + + - BSD See included LICENSE.txt file + - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html +===========================================================================**/ + +/* ================================================= + * @file clitkChangeDicomTag.cxx + * @author TB + * @date 07/18 + * + * @brief Change dicom tag value + * + ===================================================*/ + + +// clitk +#include "clitkChangeDicomTag_ggo.h" +#include "clitkIO.h" +#include "clitkChangeDicomTagGenericFilter.h" +#include "clitkCommon.h" + +//-------------------------------------------------------------------- +int main(int argc, char * argv[]) +{ + + // Init command line + GGO(clitkChangeDicomTag, args_info); + CLITK_INIT; + + // Filter + typedef clitk::ChangeDicomTagGenericFilter FilterType; + FilterType::Pointer genericFilter = FilterType::New(); + + genericFilter->SetArgsInfo(args_info); + genericFilter->Update(); + + return EXIT_SUCCESS; +}// end main + +//-------------------------------------------------------------------- diff --git a/tools/clitkChangeDicomTag.ggo b/tools/clitkChangeDicomTag.ggo new file mode 100644 index 0000000..cd97cba --- /dev/null +++ b/tools/clitkChangeDicomTag.ggo @@ -0,0 +1,12 @@ +#File clitkChangeDicomTag.ggo +package "clitkChangeDicomTag" +version "1.0" +description "Change Dicom tag -k value to -t" + +option "config" - "Config file" string no +option "verbose" v "Verbose" flag off + +option "input" i "Input dicom file" string yes +option "output" o "Output dicom file" string yes +option "key" k "Keys of tags to modify" string no multiple default="0008|103e" +option "tag" t "Tags values" string no multiple default="MIDPOSITION"