]> Creatis software - gdcm.git/blob - Example/WriteDicomAsJPEG.cxx
Fix mistypings
[gdcm.git] / Example / WriteDicomAsJPEG.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: WriteDicomAsJPEG.cxx,v $
5   Language:  C++
6   Date:      $Date: 2007/08/29 08:13:40 $
7   Version:   $Revision: 1.20 $
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    //tested->Print( std::cout );
58
59    int samplesPerPixel = f->GetSamplesPerPixel();
60    size_t testedDataSize    = tested->GetImageDataRawSize(); // Raw : Don't convert gray pixels+LUT to RBG pixels
61    uint8_t *testedImageData = tested->GetImageDataRaw();
62    
63    if( GDCM_NAME_SPACE::Debug::GetDebugFlag() ) { 
64       tested->Print( std::cout );
65       std::cout << "-------------------------------------------------------------------------------" << std::endl;
66    }
67 // Step 1 : Create the header of the new file
68    GDCM_NAME_SPACE::File *fileToBuild = GDCM_NAME_SPACE::File::New();
69    std::ostringstream str;
70
71    // Set the image size
72    str.str("");
73    str << xsize;
74    fileToBuild->InsertEntryString(str.str(),0x0028,0x0011,"US"); // Columns
75    str.str("");
76    str << ysize;
77    fileToBuild->InsertEntryString(str.str(),0x0028,0x0010,"US"); // Rows
78
79    if(zsize>1)
80    {
81       str.str("");
82       str << zsize;
83       fileToBuild->InsertEntryString(str.str(),0x0028,0x0008,"IS"); // Number of Frames
84    }
85
86    int bitsallocated = f->GetBitsAllocated();
87    int bitsstored    = f->GetBitsStored();
88    int highbit       = f->GetHighBitPosition();
89    //std::string pixtype = f->GetPixelType();
90    int sign = f->IsSignedPixelData();
91
92    // Set the pixel type
93    str.str("");
94    str << bitsallocated;
95    fileToBuild->InsertEntryString(str.str(),0x0028,0x0100,"US");// Bits Allocated
96    str.str("");
97    str << bitsstored;
98    fileToBuild->InsertEntryString(str.str(),0x0028,0x0101,"US");  // Bits Stored
99
100    str.str("");
101    str << highbit;
102    fileToBuild->InsertEntryString(str.str(),0x0028,0x0102,"US"); // High Bit
103
104    // Set the pixel representation
105    str.str("");
106    str << sign;
107    fileToBuild->InsertEntryString(str.str(),0x0028,0x0103,"US"); // Pixel Representation
108
109    // Set the samples per pixel
110    str.str("");
111    str << samplesPerPixel; //img.components;
112    fileToBuild->InsertEntryString(str.str(),0x0028,0x0002,"US"); // Samples per Pixel
113
114 // The image may be displayed uncorectly if these fields are missing
115
116    // Set the Pixel Aspect Ratio, if any
117    std::string par = f->GetEntryString(0x0028,0x0034);
118    std::cerr <<"Pixel Aspect Ratio [" << par << "]" << std::endl;
119    if ( par != GDCM_NAME_SPACE::GDCM_UNFOUND )
120       fileToBuild->InsertEntryString(par,0x0028,0x0034,"IS"); // Pixel Aspect Ratio
121
122    // Set the Modality, if any
123    std::string modality = f->GetEntryString(0x0008,0x0060);
124    std::cerr <<"Modality [" << modality << "]" << std::endl;
125    if ( modality != GDCM_NAME_SPACE::GDCM_UNFOUND )
126       fileToBuild->InsertEntryString(modality,0x0008,0x0060,"CS"); // Modality
127
128    // Set the Media Storage SOP Class UID, if any
129    std::string mssop = f->GetEntryString(0x0002,0x0002);
130    std::cerr <<"Media Storage SOP Class UID [" << mssop << "]" << std::endl;
131    if ( mssop != GDCM_NAME_SPACE::GDCM_UNFOUND )
132       fileToBuild->InsertEntryString(mssop,0x0002,0x0002,"UI"); // Media Storage SOP Class UID
133
134    // This one is mandatory to deal with Pixel Aspect Ratio, in ultrasound images !
135    // Set the SOP Class UID, if any
136    std::string sop = f->GetEntryString(0x0008,0x0016);
137    std::cerr <<"SOP Class UID [" << sop << "]" << std::endl;
138    if ( sop != GDCM_NAME_SPACE::GDCM_UNFOUND )
139       fileToBuild->InsertEntryString(sop,0x0008,0x0016,"UI"); // SOP Class UID
140      
141 // Step 2 : Create the output image
142    size_t size = xsize * ysize * zsize
143                * samplesPerPixel  * bitsallocated / 8;
144
145    GDCM_NAME_SPACE::FileHelper *fileH = GDCM_NAME_SPACE::FileHelper::New(fileToBuild);
146
147    // Consider that pixels are unmodified
148    fileH->SetContentType(GDCM_NAME_SPACE::UNMODIFIED_PIXELS_IMAGE);
149    std::cerr << "xsize " << xsize << " ysize " << ysize << " zsize " << zsize << " samplesPerPixel " << samplesPerPixel
150              << " bitsallocated " << bitsallocated << std::endl;
151    std::cerr << "size " << size << " testedDataSize " << testedDataSize <<
152                  std::endl;
153    assert(abs (size-testedDataSize) <= 1 );
154    fileH->SetWriteTypeToJPEG(  );
155    //fileH->SetImageData(testedImageData, testedDataSize);
156
157    // SetUserData will ensure the compression
158    fileH->SetUserData(testedImageData, testedDataSize);
159    if( !fileH->Write(outfilename) )
160      {
161      std::cerr << "write fails" << std::endl;
162      }
163    
164    f->Delete();
165    tested->Delete();
166    fileToBuild->Delete();
167    fileH->Delete();
168
169    return 0;
170 }
171