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