]> Creatis software - gdcm.git/blob - Example/MergeDICOMRaw.cxx
upgrades for 4DSplitter
[gdcm.git] / Example / MergeDICOMRaw.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: MergeDICOMRaw.cxx,v $
5   Language:  C++
6   Date:      $Date: 2011/03/29 07:35:57 $
7   Version:   $Revision: 1.4 $
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 "gdcmCommon.h"
21 #include "gdcmDebug.h"
22 #include "gdcmUtil.h"
23 #include "gdcmDocEntry.h"
24 #include "gdcmDataEntry.h"
25
26 #include <iomanip>
27
28 /*
29  * Take a template DICOM and merge in the Pixel Data from a RAW file
30  */
31 int main(int argc, char *argv[])
32 {  
33    GDCM_NAME_SPACE::File *f;
34  
35    if( argc < 3 )
36    {
37       std::cerr << "Usage :" << argv[0] << " input.dcm inputrawfile" << std::endl;
38       std::cerr << "  Ex: " << argv[0] << " template.dcm data.raw" << std::endl;
39       return 1;
40    }
41    std::string fileName = argv[1];
42    
43 // ============================================================
44 //   Read the input image.
45 // ============================================================
46
47    f = GDCM_NAME_SPACE::File::New( );
48
49    //f->SetLoadMode(GDCM_NAME_SPACE::LD_NOSEQ | GDCM_NAME_SPACE::LD_NOSHADOW);
50    f->SetFileName( fileName );
51       
52    bool res = f->Load();  
53
54    GDCM_NAME_SPACE::Debug::DebugOn();
55    if( GDCM_NAME_SPACE::Debug::GetDebugFlag() )
56    {
57       std::cout << "---------------------------------------------" << std::endl;
58       f->Print();
59       std::cout << "---------------------------------------------" << std::endl;
60    }
61    if (!res) {
62        std::cerr << "Sorry, " << fileName << " not a gdcm-readable "
63            << "DICOM / ACR File"
64            << std::endl;
65       f->Delete();
66       return 1;
67    }
68    std::cout << " ... is readable " << std::endl;
69
70      // (Find the dicom tag, and) extract the string
71      
72    // Read in the input data as a file:
73    const char *inputraw = argv[2];
74    std::ifstream i(inputraw);
75    if( !i )
76    {
77       std::cerr << "Problem opening file: " << inputraw << std::endl;
78       f->Delete();
79       return 1;
80    }
81    const unsigned int dims[3] = { 256, 364, 100 };
82    unsigned int pixelsize = 2; // 16bits => 2 bytes
83    unsigned long length = dims[0] * dims[1] * dims[2] * pixelsize;
84    uint8_t *pixeldata = new uint8_t[length];
85    memset(pixeldata,0,length);
86    if( ! i.read((char*)pixeldata, length) )
87      {
88       std::cerr << "Problem reading raw data" << std::endl;
89      }
90    i.close();
91
92    std::ostringstream str;
93
94    // Set the image size
95    str.str("");
96    str << dims[0];
97    f->InsertEntryString(str.str(),0x0028,0x0011,"US"); // Columns
98
99    str.str("");
100    str << dims[1];
101    f->InsertEntryString(str.str(),0x0028,0x0010,"US"); // Rows
102
103    str.str("");
104    str << dims[2];
105    f->InsertEntryString(str.str(),0x0028,0x0008, "IS"); // Number of Frames
106
107    // Set the pixel type
108    str.str("");
109    str << pixelsize * 8;
110    f->InsertEntryString(str.str(),0x0028,0x0100,"US"); // Bits Allocated
111    f->InsertEntryString(str.str(),0x0028,0x0101,"US"); // Bits Stored
112
113    str.str("");
114    str << ( pixelsize * 8 ) - 1;
115    f->InsertEntryString(str.str(),0x0028,0x0102,"US"); // High Bit
116
117    // Set the pixel representation
118    str.str("");
119    str << "0"; // Unsigned
120    f->InsertEntryString(str.str(),0x0028,0x0103,"US"); // Pixel Representation
121
122    // Set the samples per pixel
123    str.str("");
124    str << 1; // grayscale
125    f->InsertEntryString(str.str(),0x0028,0x0002,"US"); // Samples per Pixel
126
127
128    GDCM_NAME_SPACE::FileHelper *fh = GDCM_NAME_SPACE::FileHelper::New(f);
129    // Convert Media Storage SOP Class if needed
130    std::string mssop = f->GetEntryString(0x0002,0x0002);
131    // See http://www.toshiba-europe.com/medical/Materials/PDF/Dicom/MIIUS0026EA.pdf
132    if ( GDCM_NAME_SPACE::Util::DicomStringEqual(mssop, "1.2.392.200036.9116.7.8.1.1.1") ) // Toshiba US Private Data Storage
133      {
134      fh->InsertEntryString("1.2.840.10008.5.1.4.1.1.3.1",0x0002,0x0002,"UI"); // Media Storage SOP Class UID
135      fh->InsertEntryString("1.2.840.10008.5.1.4.1.1.3.1",0x0008,0x0016,"UI"); // SOP Class UID
136      }
137
138   // TODO
139   // for UltrasoundMultiframeImageStorage we may need also:
140   // (0028,0009) AT (0018,1063)  #   4, 1 FrameIncrementPointer
141   // and
142   // Pixel Aspect Ratio
143
144
145
146    fh->SetContentType(GDCM_NAME_SPACE::UNMODIFIED_PIXELS_IMAGE);
147    fh->SetImageData(pixeldata,length);
148    fh->SetWriteModeToRaw(); // no LUT, no compression.
149    fh->SetWriteTypeToDcmExplVR();
150    const char fileOut[] = "out.dcm";
151    if( !fh->Write(fileOut) )
152    {
153       std::cerr<< "-------------------------------\n"
154                    << "Error when writting the file " << fileOut << "\n"
155                 << "No file written\n";
156    }
157
158    fh->Delete();
159    f->Delete();
160    return 0;
161 }
162
163