]> Creatis software - gdcm.git/blob - Example/WriteDicomAsJPEG.cxx
upgrade
[gdcm.git] / Example / WriteDicomAsJPEG.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: WriteDicomAsJPEG.cxx,v $
5   Language:  C++
6   Date:      $Date: 2007/08/21 15:10:04 $
7   Version:   $Revision: 1.15 $
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 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 = GDCM_NAME_SPACE::File::New();
43    f->SetLoadMode ( GDCM_NAME_SPACE::LD_ALL ); // Load everything
44    f->SetFileName( filename );
45    f->Load();
46
47    GDCM_NAME_SPACE::FileHelper *tested = GDCM_NAME_SPACE::FileHelper::New( f );
48    std::string PixelType = tested->GetFile()->GetPixelType();
49    int xsize = f->GetXSize();
50    int ysize = f->GetYSize();
51    int zsize = f->GetZSize();
52    //tested->Print( std::cout );
53
54    int samplesPerPixel = f->GetSamplesPerPixel();
55    size_t testedDataSize    = tested->GetImageDataSize();
56    std::cerr << "testedDataSize:" << testedDataSize << std::endl;
57    uint8_t *testedImageData = tested->GetImageData();
58
59 // Step 1 : Create the header of the image
60
61    GDCM_NAME_SPACE::File *fileToBuild = GDCM_NAME_SPACE::File::New();
62    std::ostringstream str;
63
64    // Set the image size
65    str.str("");
66    str << xsize;
67    fileToBuild->InsertEntryString(str.str(),0x0028,0x0011,"US"); // Columns
68    str.str("");
69    str << ysize;
70    fileToBuild->InsertEntryString(str.str(),0x0028,0x0010,"US"); // Rows
71  
72
73    if(zsize>1)
74    {
75       str.str("");
76       str << zsize;
77       fileToBuild->InsertEntryString(str.str(),0x0028,0x0008,"IS"); // Number of Frames
78    }
79    int bitsallocated = f->GetBitsAllocated();
80    int bitsstored = f->GetBitsStored();
81    int highbit = f->GetHighBitPosition();
82    //std::string pixtype = f->GetPixelType();
83    int sign = f->IsSignedPixelData();
84
85    // Set the pixel type
86    str.str("");
87    str << bitsallocated;
88    fileToBuild->InsertEntryString(str.str(),0x0028,0x0100,"US");// Bits Allocated
89    str.str("");
90    str << bitsstored;
91    fileToBuild->InsertEntryString(str.str(),0x0028,0x0101,"US");  // Bits Stored
92
93    str.str("");
94    str << highbit;
95    fileToBuild->InsertEntryString(str.str(),0x0028,0x0102,"US"); // High Bit
96
97    // Set the pixel representation
98    str.str("");
99    str << sign;
100    fileToBuild->InsertEntryString(str.str(),0x0028,0x0103,"US"); // Pixel Representation
101
102    // Set the samples per pixel
103    str.str("");
104    str << samplesPerPixel; //img.components;
105    fileToBuild->InsertEntryString(str.str(),0x0028,0x0002,"US"); // Samples per Pixel
106
107 // Step 2 : Create the output image
108    size_t size = xsize * ysize * zsize
109                * samplesPerPixel  * bitsallocated / 8;
110
111    GDCM_NAME_SPACE::FileHelper *fileH = GDCM_NAME_SPACE::FileHelper::New(fileToBuild);
112    assert( size == testedDataSize );
113    fileH->SetWriteTypeToJPEG(  );
114    fileH->SetImageData(testedImageData, testedDataSize);
115    if( !fileH->Write(outfilename) )
116      {
117      std::cerr << "write fails" << std::endl;
118      }
119
120    f->Delete();
121    tested->Delete();
122    fileToBuild->Delete();
123    fileH->Delete();
124
125    return 0;
126 }
127