]> Creatis software - gdcm.git/blob - Example/exColorToRGB.cxx
COMP: Finish patching gdcm for support for bcb6
[gdcm.git] / Example / exColorToRGB.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: exColorToRGB.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/07/06 15:49:31 $
7   Version:   $Revision: 1.2 $
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    gdcm::File *f1= new gdcm::File( fileName );
54
55
56    std::cout << argv[1] << std::endl;
57
58    f1 = new gdcm::File( fileName );
59    if (!f1->IsReadable()) {
60        std::cerr << "Sorry, " << fileName <<"  not a gdcm-readable "
61                  << "DICOM / ACR File"
62                  <<std::endl;
63        return 0;
64    }
65    std::cout << " ... is readable " << std::endl;
66
67 /*
68    if (!f1->IsMonochrome()) {
69        std::cerr << "Sorry, " << fileName <<"  not a 'color' File "
70                  << " "
71                  <<std::endl;
72        return 0;
73    }
74 */
75
76 // ============================================================
77 //   Load the pixels in memory.
78 // ============================================================
79
80    // We need a gdcm::FileHelper, since we want to load the pixels        
81    gdcm::FileHelper *fh1 = new gdcm::FileHelper(f1);
82
83    // (unit8_t DOESN'T mean it's mandatory for the image to be a 8 bits one) 
84
85    uint8_t *imageData = fh1->GetImageData();
86
87    if ( imageData == 0 )
88    {
89        std::cerr << "Sorry, Pixels of" << fileName <<"  are not "
90                  << " gdcm-readable."       << std::endl;
91        return 0;
92    }
93  
94
95
96
97    // ------ User wants write a new image without shadow groups -------------
98    // ------                              without Sequences     -------------
99
100  
101    gdcm::FileHelper *copy = new gdcm::FileHelper( output );
102  
103    gdcm::DocEntry *d = f1->GetFirstEntry();
104    while(d)
105    {
106       // We skip SeqEntries, since user cannot do much with them
107       if ( !(dynamic_cast<gdcm::SeqEntry*>(d))
108       // We skip Shadow Groups, since nobody knows what they mean
109            && !( d->GetGroup()%2 ) )
110       { 
111
112          if ( gdcm::BinEntry *b = dynamic_cast<gdcm::BinEntry*>(d) )
113          {              
114             copy->GetFile()->InsertBinEntry( b->GetBinArea(),b->GetLength(),
115                                              b->GetGroup(),b->GetElement(),
116                                              b->GetVR() );
117          }
118          else if ( gdcm::ValEntry *v = dynamic_cast<gdcm::ValEntry*>(d) )
119          {   
120              copy->GetFile()->InsertValEntry( v->GetValue(),
121                                               v->GetGroup(),v->GetElement(),
122                                               v->GetVR() ); 
123          }
124          else
125          {
126           // We skip gdcm::SeqEntries
127          }
128       }
129       d = f1->GetNextEntry();
130    }
131  
132    // User knows the image is a 'color' one -RGB, YBR, Palette Color-
133    // and wants to write it as RGB
134    copy->SetImageData(imageData, fh1->GetImageDataSize());
135    copy->SetWriteModeToRGB();
136
137    copy->WriteDcmExplVR( output );
138
139
140    delete f1;
141    delete fh1;
142    delete copy;
143
144    exit (0);
145 }
146