1 /*=========================================================================
4 Module: $RCSfile: TestFromScratch.cxx,v $
6 Date: $Date: 2004/12/10 15:50:04 $
7 Version: $Revision: 1.1 $
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.
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.
17 =========================================================================*/
18 #include "gdcmHeader.h"
20 #include "gdcmDictEntry.h"
21 #include "gdcmDocEntry.h"
22 #include "gdcmValEntry.h"
23 #include "gdcmDebug.h"
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.
29 int main(int argc, char *argv[])
33 std::cerr << "Usage: " << argv[0] << " Image.dcm" << std::endl;
37 // Doesn't seems to do anything:
39 // Doesn't link properly:
40 //gdcm::Debug::GetReference().SetDebug(1);
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();
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();
52 // Hopefully default to something
53 gdcm::Header *h2 = new gdcm::Header();
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)
60 if (tag->second->GetVR().find("SQ") == 0)
62 // skip DICOM SeQuence, otherwise following cast will crash
65 const std::string &name = tag->second->GetName();
66 const std::string &value = ((gdcm::ValEntry*)(tag->second))->GetValue();
68 && value.find( "gdcm::NotLoaded" ) != 0
69 && value.find( "gdcm::Binary" ) != 0
70 && value.find( "gdcm::Loaded" ) != 0 )
72 //std::cout << name << "," << value << std::endl;
73 gdcm::DictEntry *dictEntry = h2->GetPubDict()->GetDictEntryByName(name);
74 h2->ReplaceOrCreateByNumber( value, dictEntry->GetGroup(), dictEntry->GetElement());
78 h2->ReplaceOrCreateByNumber( imageData, dataSize, 0x7fe0, 0x0010, "PXL" );
79 h2->Print( std::cout );
81 gdcm::File *f2 = new gdcm::File( h2 );
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);
87 f2->WriteDcmExplVR( "output.dcm" );