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