1 /*=========================================================================
4 Module: $RCSfile: RawToDicom.cxx,v $
6 Date: $Date: 2006/07/26 17:39:54 $
7 Version: $Revision: 1.7 $
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 =========================================================================*/
20 * Writes a Dicom file from a Raw File
21 * User has to supply parameters.
25 #include "gdcmFileHelper.h"
26 #include "gdcmDebug.h"
28 #include "gdcmArgMgr.h"
33 void ConvertSwapZone(int pixelSize, void *Raw, size_t RawSize);
35 void ConvertSwapZone(int pixelSize, void *Raw, size_t RawSize)
40 uint16_t *im16 = (uint16_t*)Raw;
41 for( i = 0; i < RawSize / 2; i++ )
43 im16[i]= (im16[i] >> 8) | (im16[i] << 8 );
46 else if ( pixelSize == 4 )
51 uint32_t *im32 = (uint32_t*)Raw;
53 for( i = 0; i < RawSize / 4; i++ )
55 low = im32[i] & 0x0000ffff; // 3412
58 im32[i] = ( s32 << 16 ) | high;
66 int main(int argc, char *argv[])
69 " \n RawToDicom : \n ",
70 " Writes a Dicom file from a Raw File ",
71 " usage: RawToDicom filein=inputFileName ",
72 " fileout=outputFileName ",
74 " lines=nb of Lines, ",
75 " pixeltype={8U|8S|16U|16S|32U|32S} ",
76 " [{b|l}] b:BigEndian,l:LittleEndian default : l ",
77 " [frames = nb of Frames] //defaulted to 1 ",
78 " [samples = {1|3}} //defaulted to 1(1:Gray,3:RGB) ",
79 " [patientname = Patient's name] ",
82 " debug : developper wants to run the program in 'debug mode' ",
86 // Initialize Arguments Manager
87 gdcm::ArgMgr *am= new gdcm::ArgMgr(argc, argv);
89 if (argc == 1 || am->ArgMgrDefined("usage") )
91 am->ArgMgrUsage(usage); // Display 'usage'
96 const char *inputFileName = am->ArgMgrGetString("filein");
97 const char *outputFileName = am->ArgMgrGetString("fileout");
99 const char *patientName = am->ArgMgrGetString("patientname");
101 int nX = am->ArgMgrWantInt("rows", usage);
102 int nY = am->ArgMgrWantInt("lines", usage);
103 int nZ = am->ArgMgrGetInt("frames", 1);
104 int samplesPerPixel = am->ArgMgrGetInt("samples", 1);
106 int b = am->ArgMgrDefined("b");
107 int l = am->ArgMgrDefined("l");
109 char *pixelType = am->ArgMgrWantString("pixeltype", usage);
111 if (am->ArgMgrDefined("debug"))
112 gdcm::Debug::DebugOn();
114 /* if unused Param we give up */
115 if ( am->ArgMgrPrintUnusedLabels() )
117 am->ArgMgrUsage(usage);
122 delete am; // we don't need Argument Manager any longer
124 // ----------- End Arguments Manager ---------
128 std::ifstream *Fp = new std::ifstream(inputFileName, std::ios::in | std::ios::binary);
131 std::cout << "Cannot open file: " << inputFileName;
137 bool bigEndian = gdcm::Util::IsCurrentProcessorBigEndian();
139 std::string strPixelType(pixelType);
143 if (strPixelType == "8S")
148 else if (strPixelType == "8U")
153 else if (strPixelType == "16S")
158 else if (strPixelType == "16U")
163 else if (strPixelType == "32S")
168 else if (strPixelType == "32U")
175 std::cout << "Wrong 'pixeltype' (" << strPixelType << ")" << std::endl;
179 int dataSize = nX*nY*nZ*pixelSize*samplesPerPixel;
180 uint8_t *pixels = new uint8_t[dataSize];
182 Fp->read((char*)pixels, (size_t)dataSize);
184 if ( pixelSize !=1 && ( (l && bigEndian) || (b && ! bigEndian) ) )
186 ConvertSwapZone(pixelSize, pixels, dataSize);
190 // Create an empty FileHelper
192 gdcm::FileHelper *fileH = gdcm::FileHelper::New();
194 // Get the (empty) image header.
195 gdcm::File *fileToBuild = fileH->GetFile();
198 // If you want to use this program as a template to create
199 // a 'Single Study UID - Single Serie UID' file set
200 // keep the following lines out of the loop
202 // 'Study Instance UID'
203 // The user is allowed to create his own Study,
204 // keeping the same 'Study Instance UID' for various images
205 // The user may add images to a 'Manufacturer Study',
206 // adding new Series to an already existing Study
207 std::string studyUID = gdcm::Util::CreateUniqueUID();
208 fileToBuild->InsertEntryString(studyUID, 0x0020,0x000d,"UI");
210 // 'Serie Instance UID'
211 // The user is allowed to create his own Series,
212 // keeping the same 'Serie Instance UID' for various images
213 // The user shouldn't add any image to a 'Manufacturer Serie'
214 // but there is no way no to prevent him for doing that
215 std::string serieUID = gdcm::Util::CreateUniqueUID();
216 fileToBuild->InsertEntryString(serieUID, 0x0020,0x000e,"UI");
218 // end of 'keep out of loop lines
220 std::ostringstream str;
222 // Set the image size
225 fileToBuild->InsertEntryString(str.str(),0x0028,0x0011, "US"); // Columns
228 fileToBuild->InsertEntryString(str.str(),0x0028,0x0010, "US"); // Rows
232 fileToBuild->InsertEntryString(str.str(),0x0028,0x0008, "IS"); // Number of Frames
234 // Set the pixel type
238 fileToBuild->InsertEntryString(str.str(),0x0028,0x0100, "US"); // Bits Allocated
242 fileToBuild->InsertEntryString(str.str(),0x0028,0x0101, "US"); // Bits Stored
245 str << ( pixelSize*8 - 1 );
246 fileToBuild->InsertEntryString(str.str(),0x0028,0x0102, "US"); // High Bit
250 fileToBuild->InsertEntryString(str.str(),0x0028,0x0103, "US"); // Pixel Representation
253 str << samplesPerPixel;
255 // If you deal with a Serie of images, it up to you to tell gdcm,
256 // for each image, what are the values of :
257 // 0020 0032 DS 3 Image Position (Patient)
258 // 0020 0037 DS 6 Image Orientation (Patient)
260 fileToBuild->InsertEntryString(str.str(),0x0028,0x0002, "US"); // Samples per Pixel
262 if (strlen(patientName) != 0)
263 fileToBuild->InsertEntryString(patientName,0x0010,0x0010, "PN"); // Patient's Name
266 // Set the image Pixel Data
267 fileH->SetImageData(pixels,dataSize);
269 // Set the writting mode and write the image
270 fileH->SetWriteModeToRaw();
272 // Write a DICOM Explicit VR file
273 fileH->SetWriteTypeToDcmExplVR();
275 if( !fileH->Write(outputFileName ) )
277 std::cout << "Failed for [" << outputFileName << "]\n"
278 << " File is unwrittable\n";