# DuplicatePCDE.cxx

    /*=========================================================================

      Program: GDCM (Grassroots DICOM). A DICOM library
      Module:  $URL$

      Copyright (c) 2006-2009 Mathieu Malaterre
      All rights reserved.
      See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.

         This software is distributed WITHOUT ANY WARRANTY; without even
         the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
         PURPOSE.  See the above copyright notice for more information.

    =========================================================================*/
    #include "gdcmReader.h"
    #include "gdcmWriter.h"
    #include "gdcmItem.h"
    #include "gdcmImageReader.h"
    #include "gdcmSequenceOfItems.h"
    #include "gdcmFile.h"
    #include "gdcmTag.h"
    /*
      Usage:
      DuplicatePCDE gdcmData/D_CLUNIE_CT1_J2KI.dcm out.dcm

    aka:
    medical.nema.org/medical/dicom/DataSets/WG04/IMAGES/J2KI/CT1_J2KI

    See:
      gdcmConformanceTests/CT1_J2KI_DuplicatePCDE.dcm

    Original thread can be found at:

    http://groups.google.com/group/comp.protocols.dicom/browse_thread/thread/82f28c4db28963af

    Question:
    1.
      There is no restriction for a specific Private Creator Data Element
    (PCDE) to be unique within the same group, right ?
      Decoders of Private Data would have to handle the case where a PCDE
    would be repeated and should NOT stop on the first instance of a
    particular PCDE, right ?

      Eg. when searching for the tag associated with
    (0x0029,0x0010,"SIEMENS CSA HEADER") in the following (pseudo)
    dataset:

    (0029,0010) LO [SIEMENS CSA HEADER]                     #  18, 1
    PrivateCreator
    (0029,0011) LO [SIEMENS MEDCOM HEADER]                  #  22, 1
    PrivateCreator
    (0029,0012) LO [SIEMENS MEDCOM HEADER2]                 #  22, 1
    PrivateCreator
    (0029,0013) LO [SIEMENS CSA HEADER]                     #  18, 1
    PrivateCreator
    (0029,1008) CS [IMAGE NUM 4]                            #  12, 1
    CSAImageHeaderType
    (0029,1009) LO [20050723]                               #   8, 1
    CSAImageHeaderVersion
    (0029,1010) OB 53\56\31\30\04\03\02\01\38\00\00\00\4d
    \00\00\00\45\63\68\6f\4c\69... # 6788, 1 CSAImageHeaderInfo
    (0029,1018) CS [MR]                                     #   2, 1
    CSASeriesHeaderType
    (0029,1019) LO [20050723]                               #   8, 1
    CSASeriesHeaderVersion
    (0029,1020) OB 53\56\31\30\04\03\02\01\2c\00\00\00\4d
    \00\00\00\55\73\65\64\50\61... # 51520, 1 CSASeriesHeaderInfo
    (0029,1131) LO [4.0.163088300]                          #  14, 1
    PMTFInformation1
    (0029,1132) UL 32768                                    #   4, 1
    PMTFInformation2
    (0029,1133) UL 0                                        #   4, 1
    PMTFInformation3
    (0029,1134) CS [DB TO DICOM]                            #  12, 1
    PMTFInformation4
    (0029,1260) ?? 63\6f\6d\20                              #   4, 1
    Unknown Tag & Data
    (0029,1310) OB 53\56\31\30\04\03\02\01\38\00\00\00\4d
    \00\00\00\45\63\68\6f\4c\69... # 6788, 1 CSAImageHeaderInfo

      one should return two instances, correct ?

    Answer:
    I would say that this is covered in principle by the PS 3.5 7.1
    "The Data Elements ... shall occur at most once in a Data Set"
    rule, since the data element is defined by the tuple
    (private creator,gggg,ee) where xxee is the element
    number and xx is arbitrary and has no inherent meaning and
    does not serve to disambiguate the data element.

    E.g.:

    (0019,0030) Private Creator ID = "Smith"
    ...
    (0019,0032) Private Creator ID = "Smith"
    ...
    (0019,3015) Fractal Index = "32"
    ...
    (0019,3215) Fractal Index = "32"

    would be illegal because even though they are assigned different
    (completely arbitrary) blocks, with the same group, element
    number and private creator, (0019,3015) and (0019,3215) are the
    "same" data element.

    */

    int main(int argc, char *argv[])
    {
      if( argc < 3 )
        {
        std::cerr << argv[0] << " input.dcm output.dcm" << std::endl;
        return 1;
        }
      const char *filename = argv[1];
      const char *outfilename = argv[2];
      gdcm::Reader reader;
      reader.SetFileName( filename );
      if( !reader.Read() )
        {
        return 1;
        }

      gdcm::File &file = reader.GetFile();
      gdcm::DataSet &ds = file.GetDataSet();

      // Let's get all private element from group 0x9:
    /*
    (0009,0010) LO [GEMS_IDEN_01]                                     # 12,1 Private Creator
    (0009,1001) LO [GE_GENESIS_FF ]                                   # 14,1 Full fidelity
    (0009,1002) SH [CT01]                                             # 4,1 Suite id
    (0009,1004) SH [HiSpeed CT/i]                                     # 12,1 Product id
    (0009,1027) SL 862399669                                          # 4,1 Image actual date
    (0009,1030) SH (no value)                                         # 0,1 Service id
    (0009,1031) SH (no value)                                         # 0,1 Mobile location number
    (0009,10e6) SH [05]                                               # 2,1 Genesis Version - now
    (0009,10e7) UL 973283917                                          # 4,1 Exam Record checksum
    (0009,10e9) SL 862399669                                          # 4,1 Actual series data time stamp
    */
      gdcm::Tag start(0x0009,0x0);
      // Create a temporary duplicate dataset, since we cannot insert data element as we go over them (std::set
      // would reorganize itself as we go over it ...)
      gdcm::DataSet dup;
      gdcm::Tag new_private(0x0009,0x0);
      while (start.GetGroup() == 0x9 )
        {
        const gdcm::DataElement& de = ds.FindNextDataElement(start);
        const gdcm::Tag &t = de.GetTag();
        if( t.IsPrivateCreator() )
          {
          std::cout << t << std::endl;
          // Ok let's duplicate into the next available attribute:
          gdcm::DataElement duplicate = de;
          duplicate.GetTag().SetElement( t.GetElement() + 1 );
          dup.Insert( duplicate );
          new_private = duplicate.GetTag();
          }
        else if( t.IsPrivate() && !t.IsPrivateCreator() )
          {
          //std::cout << de << std::endl;
          std::string owner = ds.GetPrivateCreator( de.GetTag() );
          //std::cout << owner << std::endl;
          gdcm::DataElement duplicate = de;
          duplicate.GetTag().SetPrivateCreator( new_private );
          if( duplicate.GetByteValue() )
            {
            // Warning: when doing : duplicate = de, only the pointer to the ByteValue is passed
            // (to avoid large memory duplicate). We need to explicitely duplicate the bytevalue ourselves:
            duplicate.SetByteValue( de.GetByteValue()->GetPointer(), de.GetByteValue()->GetLength() );
            // Let's recognize the duplicated ASCII-type elements:
            if( duplicate.GetVR() & gdcm::VR::VRASCII )
              duplicate.GetByteValue()->Fill( 'X' );
            }
          dup.Insert( duplicate );
          }
        start = t;
        // move to next possible 'public' element
        start.SetElement( start.GetElement() + 1);
        }

      gdcm::DataSet::ConstIterator it = dup.Begin();
      for( ; it != dup.End(); ++it )
        {
        ds.Insert( *it );
        }

      gdcm::Writer w;
      w.SetFile( file );
      w.SetFileName( outfilename );
      if (!w.Write() )
        {
        return 1;
        }

      return 0;
    }