]> Creatis software - gdcm.git/blob - Example/TestFromScratch.cxx
* src/ : rename some methods on Entry (SetXxx, InsertXxx) to have a better
[gdcm.git] / Example / TestFromScratch.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestFromScratch.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/25 15:44:22 $
7   Version:   $Revision: 1.14 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*/
18 #include "gdcmFile.h"
19 #include "gdcmFileHelper.h"
20 #include "gdcmDictEntry.h"
21 #include "gdcmDocEntry.h"
22 #include "gdcmBinEntry.h"
23 #include "gdcmValEntry.h"
24 #include "gdcmDebug.h"
25
26 // The whole purpose of this example is to create a DICOM from scratch
27 // well almost the only thing we allow use is to copy the string entry
28 // From the previous image read.
29
30 int main(int argc, char *argv[])
31 {
32    if( argc < 2 )
33    {
34       std::cerr << "Usage: " << argv[0] << " Image.dcm" << std::endl;
35       return 1;
36    }
37
38    // Doesn't seems to do anything:
39    gdcm::Debug::SetDebugOn();
40
41    // Doesn't link properly:
42    //gdcm::Debug::GetReference().SetDebug(1);
43
44    std::string filename = argv[1];
45    gdcm::FileHelper *f1 = new gdcm::FileHelper( filename );
46    gdcm::File *h1 = f1->GetFile();
47
48    int dataSize = f1->GetImageDataSize();
49    std::cout << "DataSize:      " << dataSize << std::endl;
50    // Since we know the image is 16bits:
51    uint8_t* imageData = f1->GetImageData();
52  
53    // Hopefully default to something
54    gdcm::File *h2 = new gdcm::File();
55
56    // Copy of the header content
57    gdcm::DocEntry* d = h1->GetFirstEntry();
58    while(d)
59    {
60       if ( gdcm::ValEntry* v = dynamic_cast<gdcm::ValEntry*>(d) )
61       {   
62          // Do not bother with field from private dict
63          if( v->GetName() != "gdcm::Unknown" )
64          {  
65             h2->InsertValEntry( v->GetValue(),
66                                 v->GetGroup(),v->GetElement(),
67                                 v->GetVR() ); 
68          }
69       }
70       //else
71          // We skip pb of SQ recursive exploration
72          // We skip bin entries
73
74       d = h1->GetNextEntry();
75    }
76    h2->Print( std::cout );
77
78    gdcm::FileHelper *f2 = new gdcm::FileHelper( h2 );
79    f2->SetImageData(imageData, dataSize);
80
81    f2->SetWriteTypeToDcmExplVR();
82    f2->Write( "output.dcm" );
83
84    delete f1;
85    delete f2;
86    delete h2;
87
88    return 0;
89 }