]> Creatis software - gdcm.git/blob - Example/exColorToRGB.cxx
Some more examples
[gdcm.git] / Example / exColorToRGB.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: exColorToRGB.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/02/09 14:39:48 $
7   Version:   $Revision: 1.1 $
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 "gdcmDocument.h"
22 #include "gdcmValEntry.h"
23 #include "gdcmBinEntry.h"
24 #include "gdcmSeqEntry.h"
25
26
27 int main(int argc, char *argv[])
28 {
29
30    std::cout << "------------------------------------------------" << std::endl;
31    std::cout << "Tranforms a full gdcm-readable 'color' Dicom image "
32           << " (e.g Palette Color, YBR, 3-Planes RGB) "
33           << " into an 'RGB_Pixels' Dicom Image "
34           << " "            << std::endl;
35
36    if (argc < 3)
37    {
38       std::cerr << "Usage :" << std::endl << 
39       argv[0] << " input_dicom output_dicom" << std::endl;
40       return 1;
41    }
42
43    std::string fileName = argv[1];
44    std::string output   = argv[2];
45
46
47 // ============================================================
48 //   Read the input image.
49 // ============================================================
50    // a gdcm::File contains all the Dicom Field but the Pixels Element
51
52    gdcm::File *f1= new gdcm::File( fileName );
53
54
55    std::cout << argv[1] << std::endl;
56
57    f1 = new gdcm::File( fileName );
58    if (!f1->IsReadable()) {
59        std::cerr << "Sorry, " << fileName <<"  not a gdcm-readable "
60                  << "DICOM / ACR File"
61                  <<std::endl;
62        return 0;
63    }
64    std::cout << " ... is readable " << std::endl;
65
66 /*
67    if (!f1->IsMonochrome()) {
68        std::cerr << "Sorry, " << fileName <<"  not a 'color' File "
69                  << " "
70                  <<std::endl;
71        return 0;
72    }
73 */
74
75 // ============================================================
76 //   Load the pixels in memory.
77 // ============================================================
78
79    // We need a gdcm::FileHelper, since we want to load the pixels        
80    gdcm::FileHelper *fh1 = new gdcm::FileHelper(f1);
81
82    // (unit8_t DOESN'T mean it's mandatory for the image to be a 8 bits one) 
83
84    uint8_t *imageData = fh1->GetImageData();
85
86    if ( imageData == 0 )
87    {
88        std::cerr << "Sorry, Pixels of" << fileName <<"  are not "
89                  << " gdcm-readable."       << std::endl;
90        return 0;
91    }
92  
93
94
95
96    // ------ User wants write a new image without shadow groups -------------
97    // ------                              without Sequences     -------------
98
99  
100    gdcm::FileHelper *copy = new gdcm::FileHelper( output );
101  
102    gdcm::DocEntry *d = f1->GetFirstEntry();
103    while(d)
104    {
105       // We skip SeqEntries, since user cannot do much with them
106       if ( !(dynamic_cast<gdcm::SeqEntry*>(d))
107       // We skip Shadow Groups, since nobody knows what they mean
108            && !( d->GetGroup()%2 ) )
109       { 
110
111          if ( gdcm::BinEntry *b = dynamic_cast<gdcm::BinEntry*>(d) )
112          {              
113             copy->GetFile()->InsertBinEntry( b->GetBinArea(),b->GetLength(),
114                                              b->GetGroup(),b->GetElement(),
115                                              b->GetVR() );
116          }
117          else if ( gdcm::ValEntry *v = dynamic_cast<gdcm::ValEntry*>(d) )
118          {   
119              copy->GetFile()->InsertValEntry( v->GetValue(),
120                                               v->GetGroup(),v->GetElement(),
121                                               v->GetVR() ); 
122          }
123          else
124          {
125           // We skip gdcm::SeqEntries
126          }
127       }
128       d = f1->GetNextEntry();
129    }
130  
131    // User knows the image is a 'color' one -RGB, YBR, Palette Color-
132    // and wants to write it as RGB
133    copy->SetImageData(imageData, fh1->GetImageDataSize());
134    copy->SetWriteModeToRGB();
135
136    copy->WriteDcmExplVR( output );
137
138
139    delete f1;
140    delete fh1;
141    delete copy;
142
143    exit (0);
144 }
145