]> Creatis software - gdcm.git/blob - Example/TestFromScratch.cxx
ENH: Adding an example that only work on linux, not on Win32. It shows how to create...
[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 15:50:04 $
7   Version:   $Revision: 1.1 $
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 "gdcmValEntry.h"
23 #include "gdcmDebug.h"
24
25 // The whole purpose of this example is to create a DICOM from scratch
26 // well almost the only thing we allow use is to copy the string entry
27 // From the previous image read.
28
29 int main(int argc, char *argv[])
30 {
31    if( argc < 2 )
32    {
33       std::cerr << "Usage: " << argv[0] << " Image.dcm" << std::endl;
34       return 1;
35    }
36
37    // Doesn't seems to do anything:
38    dbg.SetDebug(-1);
39    // Doesn't link properly:
40    //gdcm::Debug::GetReference().SetDebug(1);
41
42    std::string filename = argv[1];
43    //gdcm::File *f1 = new gdcm::File( "/home/malaterre/Creatis/gdcmData/012345.002.050.dcm" );
44    gdcm::File *f1 = new gdcm::File( filename );
45    gdcm::Header *h1 = f1->GetHeader();
46
47    int dataSize = f1->GetImageDataSize();
48    std::cout << "DataSize:      " << dataSize << std::endl;
49    // Since we know the image is 16bits:
50    uint8_t* imageData = f1->GetImageData();
51  
52    // Hopefully default to something
53    gdcm::Header *h2 = new gdcm::Header();
54
55    const gdcm::TagDocEntryHT &nameHt = h1->GetTagHT();
56    //const gdcm::TagDocEntryHT &nameHt = h2->GetTagHT(); seems to be empty ??
57    for (gdcm::TagDocEntryHT::const_iterator tag = nameHt.begin(); tag != nameHt.end(); ++tag)
58    {
59       //Copy string only:
60       if (tag->second->GetVR().find("SQ") == 0)
61       {
62          // skip DICOM SeQuence, otherwise following cast will crash
63          continue;
64       }
65       const std::string &name = tag->second->GetName();
66       const std::string &value = ((gdcm::ValEntry*)(tag->second))->GetValue();
67       if( name != "unkn"
68        && value.find( "gdcm::NotLoaded" ) != 0
69        && value.find( "gdcm::Binary" )    != 0
70        && value.find( "gdcm::Loaded" )    != 0 )
71       {
72         //std::cout << name << "," << value << std::endl;
73         gdcm::DictEntry *dictEntry = h2->GetPubDict()->GetDictEntryByName(name);
74         h2->ReplaceOrCreateByNumber( value, dictEntry->GetGroup(), dictEntry->GetElement());
75       }
76    }
77
78    h2->ReplaceOrCreateByNumber( imageData, dataSize, 0x7fe0, 0x0010, "PXL" );
79    h2->Print( std::cout );
80
81    gdcm::File *f2 = new gdcm::File( h2 );
82  
83    // SetImageData does not work because you need to have called
84    // somwhow ReplaceOrCreateByNumber before with the proper group/element
85    f2->SetImageData(imageData, dataSize);
86
87    f2->WriteDcmExplVR( "output.dcm" );
88
89    delete f1;
90    delete f2;
91    delete h2;
92
93    return 0;
94 }