]> Creatis software - gdcm.git/blob - Example/WriteDicomAsMPEG.cxx
Fix mistypings
[gdcm.git] / Example / WriteDicomAsMPEG.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: WriteDicomAsMPEG.cxx,v $
5   Language:  C++
6   Date:      $Date: 2007/06/21 15:01:00 $
7   Version:   $Revision: 1.3 $
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 #include "gdcmFile.h"
20 #include "gdcmFileHelper.h"
21 #include "gdcmUtil.h"
22  
23
24 // Open a dicom file and compress it as JPEG stream
25 int main(int argc, char *argv[])
26 {
27   const int xsize = 352; //352x240 DirectClass 248kb 0.000u 0:01
28   const int ysize = 240;
29   const int zsize = 10;
30   const int samplesPerPixel = 3;
31   (void)argc;
32   (void)argv;
33
34    // Step 1 : Create the header of the image
35
36    GDCM_NAME_SPACE::File *fileToBuild = GDCM_NAME_SPACE::File::New();
37    std::ostringstream str;
38
39    // Set the image size
40    str.str("");
41    str << xsize;
42    fileToBuild->InsertEntryString(str.str(),0x0028,0x0011,"US"); // Columns
43    str.str("");
44    str << ysize;
45    fileToBuild->InsertEntryString(str.str(),0x0028,0x0010,"US"); // Rows
46
47    if(zsize>1)
48    {
49       str.str("");
50       str << zsize;
51       fileToBuild->InsertEntryString(str.str(),0x0028,0x0008,"IS"); // Number of Frames
52    }
53
54    // Set the pixel type
55    str.str("");
56    str << 8; //img.componentSize;
57    fileToBuild->InsertEntryString(str.str(),0x0028,0x0100,"US"); // Bits Allocated
58
59    str.str("");
60    str << 8; //img.componentUse;
61    fileToBuild->InsertEntryString(str.str(),0x0028,0x0101,"US"); // Bits Stored
62
63    str.str("");
64    str << 7; //( img.componentSize - 1 );
65    fileToBuild->InsertEntryString(str.str(),0x0028,0x0102,"US"); // High Bit
66
67    // Set the pixel representation
68    str.str("");
69    str << 0; //img.sign;
70    fileToBuild->InsertEntryString(str.str(),0x0028,0x0103,"US"); // Pixel Representation
71
72    // Set the samples per pixel
73    str.str("");
74    str << samplesPerPixel; //img.components;
75    fileToBuild->InsertEntryString(str.str(),0x0028,0x0002,"US"); // Samples per Pixel
76
77 // Step 2 : Create the output image
78 //   std::cout << "2...";
79 //   if( img.componentSize%8 > 0 )
80 //   {
81 //      img.componentSize += 8-img.componentSize%8;
82 //   }
83    size_t size = xsize * ysize * zsize
84                * samplesPerPixel /* * img.componentSize / 8*/;
85
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);
91    size = mpeg.tellg();
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) )
99      {
100      std::cerr << "Badddd" << std::endl;
101      }
102
103    fileToBuild->Delete();
104    fileH->Delete();
105
106    return 0;
107 }
108