1 /*=========================================================================
2 
3   Program: GDCM (Grassroots DICOM). A DICOM library
4 
5   Copyright (c) 2006-2011 Mathieu Malaterre
6   All rights reserved.
7   See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
8 
9      This software is distributed WITHOUT ANY WARRANTY; without even
10      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11      PURPOSE.  See the above copyright notice for more information.
12 
13 =========================================================================*/
14 #include "gdcmReader.h"
15 #include "gdcmWriter.h"
16 #include "gdcmAttribute.h"
17 #include "gdcmFileDerivation.h"
18 #include "gdcmUIDGenerator.h"
19 
main(int argc,char * argv[])20 int main(int argc, char *argv[])
21 {
22   if( argc < 3 )
23   {
24     return 1;
25   }
26   const char * ref = argv[1];
27   const char * in  = argv[2];
28 
29   gdcm::Reader r1;
30   r1.SetFileName( ref );
31   if( !r1.Read() ) return 1;
32 
33   gdcm::Reader r2;
34   r2.SetFileName( in );
35   if( !r2.Read() ) return 1;
36 
37 
38   // Fix Spatial info:
39   gdcm::DataSet & ds1 = r1.GetFile().GetDataSet();
40   gdcm::File & file2 = r2.GetFile();
41   gdcm::DataSet & ds2 = file2.GetDataSet();
42   //gdcm::Attribute<0x8,0x8> img_type = { "ORIGINAL", "PRIMARY" };
43   ds2.Replace( ds1.GetDataElement( gdcm::Tag(0x0008,0x0008) ));
44   ds2.Replace( ds1.GetDataElement( gdcm::Tag(0x0020,0x0032) ));
45   ds2.Replace( ds1.GetDataElement( gdcm::Tag(0x0020,0x0037) ));
46   ds2.Replace( ds1.GetDataElement( gdcm::Tag(0x0018,0x0088) )); // Spacing between slices
47   ds2.Replace( ds1.GetDataElement( gdcm::Tag(0x0020,0x0013) )); // Instance Number
48   ds2.Replace( ds1.GetDataElement( gdcm::Tag(0x0018,0x5100) )); // Patient Position
49   ds2.Replace( ds1.GetDataElement( gdcm::Tag(0x0018,0x0050) )); // Slice Thickness
50   ds2.Replace( ds1.GetDataElement( gdcm::Tag(0x0008,0x0070) )); // Manufacturer
51   ds2.Replace( ds1.GetDataElement( gdcm::Tag(0x0018,0x0081) )); // Echo Time
52   ds2.Replace( ds1.GetDataElement( gdcm::Tag(0x0020,0x1041) )); // Slice Location
53 
54   gdcm::Attribute<0x8,0x16> sopclassuid;
55   sopclassuid.SetFromDataSet( ds1 );
56   gdcm::Attribute<0x8,0x18> sopinstanceuid;
57   sopinstanceuid.SetFromDataSet( ds1 );
58 
59   // Step 2: DERIVED object
60   gdcm::FileDerivation fd;
61   fd.AddReference( sopclassuid.GetValue(), sopinstanceuid.GetValue() );
62 
63   // http://dicom.nema.org/MEDICAL/dicom/current/output/chtml/part16/chapter_D.html#DCM_121321
64   // CID 7202 "Source Image Purposes of Reference"
65   // DCM 121321 "Mask image for image processing operation"
66   fd.SetPurposeOfReferenceCodeSequenceCodeValue( 121321 );
67   // CID 7203 "Image Derivation"
68   // DCM 113047 "Pixel by pixel mask"
69   fd.SetDerivationCodeSequenceCodeValue( 113047 );
70   fd.SetFile( file2 );
71   // If all Code Value are ok the filter will execute properly
72   if( !fd.Derive() )
73     {
74     std::cerr << "Sorry could not derive using input info" << std::endl;
75     return 1;
76     }
77 
78   gdcm::Writer w;
79   w.SetFile( r2.GetFile() );
80   w.SetFileName( "derived.dcm" );
81   if( !w.Write() )
82     {
83     return 1;
84     }
85 
86   return 0;
87 }
88