1 /*=========================================================================
4 Module: $RCSfile: MergeDICOMRaw.cxx,v $
6 Date: $Date: 2011/03/29 07:35:57 $
7 Version: $Revision: 1.4 $
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 =========================================================================*/
19 #include "gdcmFileHelper.h"
20 #include "gdcmCommon.h"
21 #include "gdcmDebug.h"
23 #include "gdcmDocEntry.h"
24 #include "gdcmDataEntry.h"
29 * Take a template DICOM and merge in the Pixel Data from a RAW file
31 int main(int argc, char *argv[])
33 GDCM_NAME_SPACE::File *f;
37 std::cerr << "Usage :" << argv[0] << " input.dcm inputrawfile" << std::endl;
38 std::cerr << " Ex: " << argv[0] << " template.dcm data.raw" << std::endl;
41 std::string fileName = argv[1];
43 // ============================================================
44 // Read the input image.
45 // ============================================================
47 f = GDCM_NAME_SPACE::File::New( );
49 //f->SetLoadMode(GDCM_NAME_SPACE::LD_NOSEQ | GDCM_NAME_SPACE::LD_NOSHADOW);
50 f->SetFileName( fileName );
54 GDCM_NAME_SPACE::Debug::DebugOn();
55 if( GDCM_NAME_SPACE::Debug::GetDebugFlag() )
57 std::cout << "---------------------------------------------" << std::endl;
59 std::cout << "---------------------------------------------" << std::endl;
62 std::cerr << "Sorry, " << fileName << " not a gdcm-readable "
68 std::cout << " ... is readable " << std::endl;
70 // (Find the dicom tag, and) extract the string
72 // Read in the input data as a file:
73 const char *inputraw = argv[2];
74 std::ifstream i(inputraw);
77 std::cerr << "Problem opening file: " << inputraw << std::endl;
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) )
88 std::cerr << "Problem reading raw data" << std::endl;
92 std::ostringstream str;
97 f->InsertEntryString(str.str(),0x0028,0x0011,"US"); // Columns
101 f->InsertEntryString(str.str(),0x0028,0x0010,"US"); // Rows
105 f->InsertEntryString(str.str(),0x0028,0x0008, "IS"); // Number of Frames
107 // Set the pixel type
109 str << pixelsize * 8;
110 f->InsertEntryString(str.str(),0x0028,0x0100,"US"); // Bits Allocated
111 f->InsertEntryString(str.str(),0x0028,0x0101,"US"); // Bits Stored
114 str << ( pixelsize * 8 ) - 1;
115 f->InsertEntryString(str.str(),0x0028,0x0102,"US"); // High Bit
117 // Set the pixel representation
119 str << "0"; // Unsigned
120 f->InsertEntryString(str.str(),0x0028,0x0103,"US"); // Pixel Representation
122 // Set the samples per pixel
124 str << 1; // grayscale
125 f->InsertEntryString(str.str(),0x0028,0x0002,"US"); // Samples per Pixel
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
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
139 // for UltrasoundMultiframeImageStorage we may need also:
140 // (0028,0009) AT (0018,1063) # 4, 1 FrameIncrementPointer
142 // Pixel Aspect Ratio
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) )
153 std::cerr<< "-------------------------------\n"
154 << "Error when writting the file " << fileOut << "\n"
155 << "No file written\n";