]> Creatis software - gdcm.git/blob - Example/WriteDicomAsJPEG2000.cxx
d864ea17202ce43ced18be930c858459919cd298
[gdcm.git] / Example / WriteDicomAsJPEG2000.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: WriteDicomAsJPEG2000.cxx,v $
5   Language:  C++
6   Date:      $Date: 2007/06/21 15:01:00 $
7   Version:   $Revision: 1.4 $
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 // Open a dicom file and compress it as JPEG 2000 stream
24 int main(int argc, char *argv[])
25 {
26   if( argc < 2)
27     {
28     std::cerr << argv[0] << " inputfilename.dcm\n";
29     return 1;
30     }
31
32    std::string filename = argv[1];
33    std::string outfilename = "/tmp/bla.dcm";
34    if( argc >= 3 )
35      outfilename = argv[2];
36    int quality = 100;
37    if( argc >= 4 )
38      quality = atoi(argv[3]);
39    std::cerr << "Using quality: " << quality << std::endl;
40
41 // Step 1 : Create the header of the image
42    //GDCM_NAME_SPACE::File *f = new GDCM_NAME_SPACE::File();
43    // gdcm1.3 syntax. Sorry
44    GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
45    f->SetLoadMode ( GDCM_NAME_SPACE::LD_ALL ); // Load everything
46    f->SetFileName( filename );
47    f->Load();
48
49    //GDCM_NAME_SPACE::FileHelper *tested = new GDCM_NAME_SPACE::FileHelper( f );
50    // gdcm1.3 syntax. Sorry   
51    GDCM_NAME_SPACE::FileHelper *tested = GDCM_NAME_SPACE::FileHelper::New( f );
52    std::string PixelType = tested->GetFile()->GetPixelType();
53    int xsize = f->GetXSize();
54    int ysize = f->GetYSize();
55    int zsize = f->GetZSize();
56    //tested->Print( std::cout );
57
58    int samplesPerPixel = f->GetSamplesPerPixel();
59    size_t testedDataSize    = tested->GetImageDataSize();
60    uint8_t *testedImageData = tested->GetImageData();
61
62 // Step 1 : Create the header of the image
63
64 //   GDCM_NAME_SPACE::File *fileToBuild = new GDCM_NAME_SPACE::File();
65    // gdcm1.3 syntax. Sorry !
66    GDCM_NAME_SPACE::File *fileToBuild = GDCM_NAME_SPACE::File::New();
67    
68    std::ostringstream str;
69
70    // Set the image size
71    str.str("");
72    str << xsize;
73    //fileToBuild->InsertValEntry(str.str(),0x0028,0x0011); // Columns
74    // gdcm1.3 syntax. Sorry !
75    fileToBuild->InsertEntryString(str.str(),0x0028,0x0011, "US"); // Columns
76    str.str("");
77    str << ysize;
78    //fileToBuild->InsertValEntry(str.str(),0x0028,0x0010); // Rows
79    // gdcm1.3 syntax. Sorry !
80    fileToBuild->InsertEntryString(str.str(),0x0028,0x0010, "US"); // Rows
81
82    if(zsize>1)
83    {
84       str.str("");
85       str << zsize;
86       //fileToBuild->InsertValEntry(str.str(),0x0028,0x0008); // Number of Frames
87    // gdcm1.3 syntax. Sorry !
88       fileToBuild->InsertEntryString(str.str(),0x0028,0x0008, "IS"); // Number of Frames
89    }
90    int bitsallocated = f->GetBitsAllocated();
91    int bitsstored = f->GetBitsStored();
92    int highbit = f->GetHighBitPosition();
93    //std::string pixtype = f->GetPixelType();
94    int sign = f->IsSignedPixelData();
95
96    // Set the pixel type
97    str.str("");
98    str << bitsallocated;
99    //fileToBuild->InsertValEntry(str.str(),0x0028,0x0100); // Bits Allocated
100    // gdcm1.3 syntax. Sorry !
101    fileToBuild->InsertEntryString(str.str(),0x0028,0x0100,"US"); // Bits Allocated
102    
103    str.str("");
104    str << bitsstored;  
105    //fileToBuild->InsertValEntry(str.str(),0x0028,0x0101); // Bits Stored   
106    // gdcm1.3 syntax. Sorry !
107    fileToBuild->InsertEntryString(str.str(),0x0028,0x0101, "US"); // Bits Stored
108    str.str("");
109    str << highbit;
110    //fileToBuild->InsertValEntry(str.str(),0x0028,0x0102); // High Bit
111    // gdcm1.3 syntax. Sorry !
112    fileToBuild->InsertEntryString(str.str(),0x0028,0x0102, "US"); // High Bit
113
114    // Set the pixel representation
115    str.str("");
116    str << sign;
117    //fileToBuild->InsertValEntry(str.str(),0x0028,0x0103); // Pixel Representation
118    // gdcm1.3 syntax. Sorry !
119    fileToBuild->InsertEntryString(str.str(),0x0028,0x0103, "US"); // Pixel Representation
120
121
122    // Set the samples per pixel
123    str.str("");
124    str << samplesPerPixel; //img.components;
125    //fileToBuild->InsertValEntry(str.str(),0x0028,0x0002); // Samples per Pixel
126    // gdcm1.3 syntax. Sorry !
127    fileToBuild->InsertEntryString(str.str(),0x0028,0x0002, "US"); // Samples per Pixel
128
129 // Step 2 : Create the output image
130    //GDCM_NAME_SPACE::FileHelper *fileH = new GDCM_NAME_SPACE::FileHelper(fileToBuild);
131    // gdcm1.3 syntax. Sorry !
132    GDCM_NAME_SPACE::FileHelper *fileH = GDCM_NAME_SPACE::FileHelper::New(fileToBuild);
133    fileH->SetWriteTypeToJPEG2000(  );
134    fileH->SetImageData(testedImageData, testedDataSize);
135    if( !fileH->Write(outfilename) )
136      {
137      std::cerr << "write fails" << std::endl;
138      }
139
140    //delete f;
141    // gdcm1.3 syntax. Sorry !   
142    f->Delete();
143    //delete tested;
144    tested->Delete();
145    //delete fileToBuild;
146    fileToBuild->Delete();
147    //delete fileH;
148    fileH->Delete();
149
150    return 0;
151 }