]> Creatis software - gdcm.git/blob - Example/RawToDicom.cxx
User is allowed to give a "patient name"
[gdcm.git] / Example / RawToDicom.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: RawToDicom.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/12/16 16:38:24 $
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
19 /**
20  * Writes a Dicom file from a Raw File
21  * User has to supply parameters. 
22  * 
23  */
24 #include "gdcmFile.h"
25 #include "gdcmFileHelper.h"
26 #include "gdcmDebug.h"
27 #include "gdcmArgMgr.h"
28
29 #include <iostream>
30 #include <sstream>
31
32
33
34 int main(int argc, char *argv[])
35 {
36
37    START_USAGE(usage)
38    " \n RawToDicom : \n                                                       ",
39    " Writes a Dicom file from a Raw File                                      ",
40    " usage: RawToDicom filein=inputFileName                                   ",
41    "                   fileout=outputFileName                                 ",
42    "                   rows=nb of Rows                                        ",
43    "                   lines=nb of Lines,                                     ",
44    "                   pixeltype={8U|8S|16U|16S}                              ",
45    "                   [frames = nb of Frames] //defaulted to 1               ",
46    "                   [samples = {1|3}}       //defaulted to 1(1:Gray,3:RGB) ",
47    "                   [patientname = Patient's name]                         ",
48    "                   [debug]                                                ",
49    "                                                                          ",
50    "      debug      : user wants to run the program in 'debug mode'          ",
51    FINISH_USAGE
52    
53
54    // Initialize Arguments Manager   
55    gdcm::ArgMgr *am= new gdcm::ArgMgr(argc, argv);
56   
57    if (argc == 1 || am->ArgMgrDefined("usage") )
58    {
59       am->ArgMgrUsage(usage); // Display 'usage'
60       delete am;
61       return 1;
62    }
63
64    char *inputFileName  = am->ArgMgrGetString("filein",(char *)0);
65    char *outputFileName = am->ArgMgrGetString("fileout",(char *)0);   
66    //char *dirName        = am->ArgMgrGetString("dirin",(char *)0);
67    
68    char *patientName = am->ArgMgrGetString("patientname",(char *)0);
69    
70    int nX = am->ArgMgrWantInt("rows", usage);
71    int nY = am->ArgMgrWantInt("lines", usage);
72    int nZ = am->ArgMgrGetInt("frames", 1);
73    int samplesPerPixel = am->ArgMgrGetInt("samples", 1);
74    
75    
76    char *pixelType = am->ArgMgrWantString("pixeltype", usage);
77    
78    if (am->ArgMgrDefined("debug"))
79       gdcm::Debug::DebugOn();
80
81    /* if unused Param we give up */
82    if ( am->ArgMgrPrintUnusedLabels() )
83    {
84       am->ArgMgrUsage(usage);
85       delete am;
86       return 1;
87    } 
88
89    delete am;  // we don't need Argument Manager any longer
90
91    // ----------- End Arguments Manager ---------
92    
93   
94  // Read the Raw file  
95    std::ifstream *Fp = new std::ifstream(inputFileName, std::ios::in | std::ios::binary);
96    if ( ! *Fp )
97    {   
98       std::cout << "Cannot open file: " << inputFileName;
99       delete Fp;
100       Fp = 0;
101       return 0;
102    }  
103
104    std::string strPixelType(pixelType);
105    int pixelSign;
106    int pixelSize;
107    
108    if (strPixelType == "8S")
109    {
110       pixelSize = 1;
111       pixelSign = 0;
112    }
113    else  if (strPixelType == "8U")
114    {
115       pixelSize = 1;
116       pixelSign = 1;
117    }
118    else  if (strPixelType == "16S")
119    {
120       pixelSize = 2;
121       pixelSign = 0;
122    }   
123    else  if (strPixelType == "16U")
124    {
125       pixelSize = 2;
126       pixelSign = 1;
127    }      
128
129    int dataSize =  nX*nY*nZ*pixelSize*samplesPerPixel;
130    uint8_t *pixels = new uint8_t[dataSize];
131    
132    Fp->read((char*)pixels, (size_t)dataSize);
133   
134
135 // Create an empty FileHelper
136
137    gdcm::FileHelper *fileH = gdcm::FileHelper::New();
138  
139  // Get the (empty) image header.  
140    gdcm::File *fileToBuild = fileH->GetFile();
141    std::ostringstream str;
142
143    // Set the image size
144    str.str("");
145    str << nX;
146    fileToBuild->InsertEntryString(str.str(),0x0028,0x0011); // Columns
147    str.str("");
148    str << nY;
149    fileToBuild->InsertEntryString(str.str(),0x0028,0x0010); // Rows
150    
151    str.str("");
152    str << nZ;
153    fileToBuild->InsertEntryString(str.str(),0x0028,0x0008); // Number of Frames
154
155    // Set the pixel type
156    
157    str.str("");
158    str << pixelSize*8;
159    fileToBuild->InsertEntryString(str.str(),0x0028,0x0100); // Bits Allocated
160
161    str.str("");
162    str << pixelSize*8;
163    fileToBuild->InsertEntryString(str.str(),0x0028,0x0101); // Bits Stored
164
165    str.str("");
166    str << ( pixelSize*8 - 1 );
167    fileToBuild->InsertEntryString(str.str(),0x0028,0x0102); // High Bit
168
169    str.str("");
170    str << pixelSign;
171    fileToBuild->InsertEntryString(str.str(),0x0028,0x0103); // Pixel Representation
172
173    str.str("");
174    str << samplesPerPixel;
175    fileToBuild->InsertEntryString(str.str(),0x0028,0x0002); // Samples per Pixel
176
177    if (strlen(patientName) != 0)
178       fileToBuild->InsertEntryString(patientName,0x0010,0x0010); // Patient's Name
179
180 // Set the image Pixel Data
181    fileH->SetImageData(pixels,dataSize);
182
183 // Set the writting mode and write the image
184    fileH->SetWriteModeToRaw();
185
186  // Write a DICOM Explicit VR file
187    fileH->SetWriteTypeToDcmExplVR();
188
189    if( !fileH->Write(outputFileName ) )
190    {
191       std::cout << "Failed for [" << outputFileName << "]\n"
192                 << "           File is unwrittable\n";
193    }
194
195    fileH->Delete();
196
197    delete[] pixels;
198    return 1;
199 }