]> Creatis software - gdcm.git/blob - Example/TestFromScratch.cxx
ENH: Thanks to Benoit example I was able to rewrite the damn thing, hopefully it...
[gdcm.git] / Example / TestFromScratch.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestFromScratch.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/12/10 16:48:37 $
7   Version:   $Revision: 1.2 $
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 "gdcmHeader.h"
19 #include "gdcmFile.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    dbg.SetDebug(-1);
40    // Doesn't link properly:
41    //gdcm::Debug::GetReference().SetDebug(1);
42
43    std::string filename = argv[1];
44    //gdcm::File *f1 = new gdcm::File( "/home/malaterre/Creatis/gdcmData/012345.002.050.dcm" );
45    gdcm::File *f1 = new gdcm::File( filename );
46    gdcm::Header *h1 = f1->GetHeader();
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::Header *h2 = new gdcm::Header();
55
56    h1->Initialize();
57    gdcm::DocEntry* d = h1->GetNextEntry();
58
59    // Copy of the header content
60    while(d)
61    {
62       if ( gdcm::BinEntry* b = dynamic_cast<gdcm::BinEntry*>(d) )
63       {
64          // We skip bin entries
65       }
66       else if ( gdcm::ValEntry* v = dynamic_cast<gdcm::ValEntry*>(d) )
67       {   
68           h2->ReplaceOrCreateByNumber( 
69                               v->GetValue(),
70                               v->GetGroup(), 
71                               v->GetElement(),
72                               v->GetVR() ); 
73       }
74       else
75       {
76        // We skip pb of SQ recursive exploration
77       }
78
79       d = h1->GetNextEntry();
80    }
81    //h2->Print( std::cout );
82
83    gdcm::File *f2 = new gdcm::File( h2 );
84    f2->SetImageData(imageData, dataSize);
85
86    f2->SetWriteTypeToDcmExplVR();
87    f2->Write( "output.dcm" );
88
89    delete f1;
90    delete f2;
91    delete h2;
92
93    return 0;
94 }