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