1 /*=========================================================================
4 Module: $RCSfile: WriteDicomAsMPEG.cxx,v $
6 Date: $Date: 2007/06/21 15:01:00 $
7 Version: $Revision: 1.3 $
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 #include "gdcmFileHelper.h"
24 // Open a dicom file and compress it as JPEG stream
25 int main(int argc, char *argv[])
27 const int xsize = 352; //352x240 DirectClass 248kb 0.000u 0:01
28 const int ysize = 240;
30 const int samplesPerPixel = 3;
34 // Step 1 : Create the header of the image
36 GDCM_NAME_SPACE::File *fileToBuild = GDCM_NAME_SPACE::File::New();
37 std::ostringstream str;
42 fileToBuild->InsertEntryString(str.str(),0x0028,0x0011,"US"); // Columns
45 fileToBuild->InsertEntryString(str.str(),0x0028,0x0010,"US"); // Rows
51 fileToBuild->InsertEntryString(str.str(),0x0028,0x0008,"IS"); // Number of Frames
56 str << 8; //img.componentSize;
57 fileToBuild->InsertEntryString(str.str(),0x0028,0x0100,"US"); // Bits Allocated
60 str << 8; //img.componentUse;
61 fileToBuild->InsertEntryString(str.str(),0x0028,0x0101,"US"); // Bits Stored
64 str << 7; //( img.componentSize - 1 );
65 fileToBuild->InsertEntryString(str.str(),0x0028,0x0102,"US"); // High Bit
67 // Set the pixel representation
70 fileToBuild->InsertEntryString(str.str(),0x0028,0x0103,"US"); // Pixel Representation
72 // Set the samples per pixel
74 str << samplesPerPixel; //img.components;
75 fileToBuild->InsertEntryString(str.str(),0x0028,0x0002,"US"); // Samples per Pixel
77 // Step 2 : Create the output image
78 // std::cout << "2...";
79 // if( img.componentSize%8 > 0 )
81 // img.componentSize += 8-img.componentSize%8;
83 size_t size = xsize * ysize * zsize
84 * samplesPerPixel /* * img.componentSize / 8*/;
86 uint8_t *imageData = new uint8_t[size];
87 GDCM_NAME_SPACE::FileHelper *fileH = GDCM_NAME_SPACE::FileHelper::New(fileToBuild);
88 //fileH->SetImageData(imageData,size);
89 std::ifstream mpeg("/tmp/ts.mpg");
90 mpeg.seekg(0, std::ios::end);
92 std::cerr << "Size MPEG:" << size << std::endl;
93 mpeg.seekg(0, std::ios::beg);
94 mpeg.read((char*)imageData, size);
95 fileH->SetImageData(imageData, size);
96 fileH->SetWriteTypeToJPEG( );
97 std::string outfilename = "/tmp/ts.dcm";
98 if( !fileH->Write(outfilename) )
100 std::cerr << "Badddd" << std::endl;
103 fileToBuild->Delete();