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 "gdcmAnonymizer.h"
15 #include "gdcmWriter.h"
16 #include "gdcmUIDGenerator.h"
17 #include "gdcmFile.h"
18 #include "gdcmTag.h"
19 #include "gdcmSystem.h"
20 
21 #include "magic.h" // libmagic, API to file command line tool
22 
23 /*
24  * Let say you want to encapsulate a file type that is not defined in DICOM (exe, zip, png)
25  * PNG is a bad example, unless it contains transparency (which has been deprecated).
26  * It will take care of dispatching each chunk to an appropriate data item (pretty much like
27  * WaveformData)
28  *
29  * Usage:
30  * ./EncapsulateFileInRawData large_input_file.exe large_input_file.dcm
31  */
32 
33 // TODO:
34 // $ file -bi /tmp/gdcm-2.1.0.pdf
main(int argc,char * argv[])35 int main(int argc, char *argv[])
36 {
37   if( argc < 3 )
38     {
39     std::cerr << argv[0] << " inputfile output.dcm" << std::endl;
40     return 1;
41     }
42   const char *filename = argv[1];
43   const char *outfilename = argv[2];
44 
45   if( !gdcm::System::FileExists( filename ) ) return 1;
46 
47   size_t s = gdcm::System::FileSize(filename);
48   if( !s ) return 1;
49 
50   magic_t cookie = magic_open(MAGIC_NONE);
51   const char * file_type = magic_file(cookie, filename);
52   if( !file_type ) return 1;
53   magic_close(cookie);
54 
55   gdcm::Writer w;
56   gdcm::File &file = w.GetFile();
57   //gdcm::DataSet &ds = file.GetDataSet();
58   //w.SetCheckFileMetaInformation( true );
59   w.SetFileName( outfilename );
60 
61   file.GetHeader().SetDataSetTransferSyntax( gdcm::TransferSyntax::ImplicitVRLittleEndian );
62 
63   gdcm::Anonymizer anon;
64   anon.SetFile( file );
65 
66   gdcm::MediaStorage ms = gdcm::MediaStorage::RawDataStorage;
67 
68   gdcm::UIDGenerator gen;
69   anon.Replace( gdcm::Tag(0x0008,0x16), ms.GetString() );
70   std::cout << ms.GetString() << std::endl;
71   anon.Replace( gdcm::Tag(0x0008,0x18), gen.Generate() );
72 
73 
74   if (!w.Write() )
75     {
76     std::cerr << "Could not write: " << outfilename << std::endl;
77     return 1;
78     }
79 
80   return 0;
81 }
82