1 /*=========================================================================
4 Module: $RCSfile: RawToDicom.cxx,v $
6 Date: $Date: 2007/09/20 11:15:54 $
7 Version: $Revision: 1.14 $
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;
64 int main(int argc, char *argv[])
67 " \n RawToDicom : \n ",
68 " Writes a Dicom file from a Raw File ",
69 " usage: RawToDicom filein=inputFileName ",
70 " fileout=outputFileName ",
72 " lines=nb of Lines, ",
73 " [frames = nb of Frames] //defaulted to 1 ",
74 " pixeltype={8U|8S|16U|16S|32U|32S} ",
75 " [{b|l}] b:BigEndian,l:LittleEndian default : l ",
76 " [samples = {1|3}} //(1:Gray,3:RGB) defaulted to 1",
78 " [studyid = ] [patientname = Patient's name] ",
81 " monochrome1 = user wants MONOCHROME1 photom. interp. (0=white) ",
82 " studyUID : *aware* user wants to add the serie ",
83 " to an already existing study ",
84 " debug : developper wants to run the program in 'debug mode' ",
88 // ------------ Initialize Arguments Manager ----------------
89 GDCM_NAME_SPACE::ArgMgr *am= new GDCM_NAME_SPACE::ArgMgr(argc, argv);
91 if (argc == 1 || am->ArgMgrDefined("usage") )
93 am->ArgMgrUsage(usage); // Display 'usage'
98 const char *inputFileName = am->ArgMgrGetString("filein");
99 const char *outputFileName = am->ArgMgrGetString("fileout");
101 const char *patientName = am->ArgMgrGetString("patientname");
103 int nX = am->ArgMgrWantInt("rows", usage);
104 int nY = am->ArgMgrWantInt("lines", usage);
105 int nZ = am->ArgMgrGetInt("frames", 1);
106 int samplesPerPixel = am->ArgMgrGetInt("samples", 1);
108 int b = am->ArgMgrDefined("b");
109 int l = am->ArgMgrDefined("l");
111 char *pixelType = am->ArgMgrWantString("pixeltype", usage);
113 bool monochrome1 = ( 0 != am->ArgMgrDefined("monochrome1") );
115 if (am->ArgMgrDefined("debug"))
116 GDCM_NAME_SPACE::Debug::DebugOn();
117 bool userDefinedStudy = ( 0 != am->ArgMgrDefined("studyUID") );
119 const char *studyUID;
120 if (userDefinedStudy)
121 studyUID = am->ArgMgrGetString("studyUID");
123 // not described *on purpose* in the Usage !
124 bool userDefinedSerie = ( 0 != am->ArgMgrDefined("serieUID") );
126 const char *serieUID;
128 serieUID = am->ArgMgrGetString("serieUID");
130 /* if unused Param we give up */
131 if ( am->ArgMgrPrintUnusedLabels() )
133 am->ArgMgrUsage(usage);
138 delete am; // we don't need Argument Manager any longer
140 // ----------- End Arguments Manager ---------
142 /// \TODO Deal with all the images of a directory
145 std::ifstream *Fp = new std::ifstream(inputFileName, std::ios::in | std::ios::binary);
148 std::cout << "Cannot open file: " << inputFileName;
154 bool bigEndian = GDCM_NAME_SPACE::Util::IsCurrentProcessorBigEndian();
156 std::string strPixelType(pixelType);
160 if (strPixelType == "8S")
165 else if (strPixelType == "8U")
170 else if (strPixelType == "16S")
175 else if (strPixelType == "16U")
180 else if (strPixelType == "32S")
185 else if (strPixelType == "32U")
192 std::cout << "Wrong 'pixeltype' (" << strPixelType << ")" << std::endl;
196 std::string strStudyUID;
197 std::string strSerieUID;
199 if (userDefinedStudy)
200 strSerieUID = studyUID;
202 strStudyUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
204 if (userDefinedStudy)
205 strSerieUID = serieUID;
207 strStudyUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
210 int dataSize = nX*nY*nZ*pixelSize*samplesPerPixel;
211 uint8_t *pixels = new uint8_t[dataSize];
213 Fp->read((char*)pixels, (size_t)dataSize);
215 if ( pixelSize !=1 && ( (l && bigEndian) || (b && ! bigEndian) ) )
217 ConvertSwapZone(pixelSize, pixels, dataSize);
220 // Create an empty FileHelper
222 GDCM_NAME_SPACE::FileHelper *fileH = GDCM_NAME_SPACE::FileHelper::New();
224 // Get the (empty) image header.
225 GDCM_NAME_SPACE::File *fileToBuild = fileH->GetFile();
227 // 'Study Instance UID'
228 // The user is allowed to create his own Study,
229 // keeping the same 'Study Instance UID' for various images
230 // The user may add images to a 'Manufacturer Study',
231 // adding new Series to an already existing Study
233 fileToBuild->InsertEntryString(strStudyUID,0x0020,0x000d,"UI"); // Study UID
235 // 'Serie Instance UID'
236 // The user is allowed to create his own Series,
237 // keeping the same 'Serie Instance UID' for various images
238 // The user shouldn't add any image to a 'Manufacturer Serie'
239 // but there is no way no to prevent him for doing that
241 fileToBuild->InsertEntryString(strSerieUID,0x0020,0x000e,"UI"); // Serie UID
243 std::ostringstream str;
245 // Set the image size
248 fileToBuild->InsertEntryString(str.str(),0x0028,0x0011, "US"); // Columns
251 fileToBuild->InsertEntryString(str.str(),0x0028,0x0010, "US"); // Rows
255 fileToBuild->InsertEntryString(str.str(),0x0028,0x0008, "IS"); // Number of Frames
257 // Set the pixel type
261 fileToBuild->InsertEntryString(str.str(),0x0028,0x0100, "US"); // Bits Allocated
265 fileToBuild->InsertEntryString(str.str(),0x0028,0x0101, "US"); // Bits Stored
268 str << ( pixelSize*8 - 1 );
269 fileToBuild->InsertEntryString(str.str(),0x0028,0x0102, "US"); // High Bit
273 fileToBuild->InsertEntryString(str.str(),0x0028,0x0103, "US"); // Pixel Representation
276 str << samplesPerPixel;
278 // If you deal with a Serie of images, as slices of a volume,
279 // it up to you to tell gdcm, for each image, what are the values of :
281 // 0020 0032 DS 3 Image Position (Patient)
282 // 0020 0037 DS 6 Image Orientation (Patient)
284 fileToBuild->InsertEntryString(str.str(),0x0028,0x0002, "US"); // Samples per Pixel
286 if (strlen(patientName) != 0)
287 fileToBuild->InsertEntryString(patientName,0x0010,0x0010, "PN"); // Patient's Name
291 fileH->SetPhotometricInterpretationToMonochrome1();
293 // Set the image Pixel Data
294 fileH->SetImageData(pixels,dataSize);
296 // Set the writting mode and write the image
297 fileH->SetWriteModeToRaw();
299 // Write a DICOM Explicit VR file
300 fileH->SetWriteTypeToDcmExplVR();
302 if( !fileH->Write(outputFileName ) )
304 std::cout << "Failed for [" << outputFileName << "]\n"
305 << " File is unwrittable\n";