]> Creatis software - gdcm.git/blob - Example/exGC.cxx
COMP: Finish patching gdcm for support for bcb6
[gdcm.git] / Example / exGC.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: exGC.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/07/06 15:49:31 $
7   Version:   $Revision: 1.4 $
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 typedef struct  // Maybe we should add it to gdcm ?
29 {
30    uint8_t r;
31    uint8_t g;
32    uint8_t b;
33 } rgb8_t;
34
35
36 // This small application, for a given Creatis user (G.C.)
37 // may be taken as an example
38
39 int main(int argc, char *argv[])
40 {
41
42    // we need a user friendly way for passign parameters on the command line !
43
44    std::cout << "------------------------------------------------" << std::endl;
45    std::cout << "Transforms a full gdcm-readable 'color' Dicom image "
46           << " (e.g Palette Color, YBR, 3-Planes RGB) "
47           << " into an 'RGB_Pixels' Dicom Image " << std::endl
48           << " Blacks out any 'grey' pixel (r=g=b) "
49           << " Blacks out any 'dark' pixel (r,g,b < threshold) " 
50           << " "            << std::endl;
51    std::cout << "------------------------------------------------" << std::endl;
52
53    if (argc < 3)
54    {
55       std::cerr << "Usage :" << std::endl << 
56       argv[0] << " input_dicom output_dicom threshold background" << std::endl;
57       return 1;
58    }
59
60    std::string fileName = argv[1];
61    std::string output   = argv[2];
62
63    int threshold = 0;
64    if (argc > 3)
65       threshold        = atoi( argv[3] );
66   
67    int background = 0;
68    if (argc > 4)
69       background        = atoi( argv[4] );
70
71 // ============================================================
72 //   Read the input image.
73 // ============================================================
74    // a gdcm::File contains all the Dicom Field but the Pixels Element
75
76    gdcm::File *f1= new gdcm::File( fileName );
77
78
79    std::cout << argv[1] << std::endl;
80
81    f1 = new gdcm::File( fileName );
82    if (!f1->IsReadable()) {
83        std::cerr << "Sorry, " << fileName <<"  not a gdcm-readable "
84                  << "DICOM / ACR File"
85                  <<std::endl;
86        return 0;
87    }
88    std::cout << " ... is readable " << std::endl;
89
90 /*
91    if (!f1->IsMonochrome()) {
92        std::cerr << "Sorry, " << fileName <<"  not a 'color' File "
93            << " "
94                  <<std::endl;
95        return 0;
96    }
97 */
98
99 // ============================================================
100 //   Load the pixels in memory.
101 // ============================================================
102
103    // We need a gdcm::FileHelper, since we want to load the pixels        
104    gdcm::FileHelper *fh1 = new gdcm::FileHelper(f1);
105
106    // (unit8_t DOESN'T mean it's mandatory for the image to be a 8 bits one) 
107
108    uint8_t *imageData = fh1->GetImageData();
109
110    if ( imageData == 0 )
111    {
112        std::cerr << "Sorry, Pixels of" << fileName <<"  are not "
113                  << " gdcm-readable."  << std::endl;
114        return 0;
115    }
116
117    // ------ User wants write a new image without shadow groups -------------
118    // ------                              without Sequences     -------------
119
120  
121    gdcm::FileHelper *copy = new gdcm::FileHelper( output );
122  
123    gdcm::DocEntry *d = f1->GetFirstEntry();
124    while(d)
125    {
126       // We skip SeqEntries, since user cannot do much with them
127       if ( !(dynamic_cast<gdcm::SeqEntry*>(d))
128       // We skip Shadow Groups, since nobody knows what they mean
129            && !( d->GetGroup()%2 ) )
130       { 
131
132          if ( gdcm::BinEntry *b = dynamic_cast<gdcm::BinEntry*>(d) )
133          {              
134             copy->GetFile()->InsertBinEntry( b->GetBinArea(),b->GetLength(),
135                                              b->GetGroup(),b->GetElement(),
136                                              b->GetVR() );
137          }
138          else if ( gdcm::ValEntry *v = dynamic_cast<gdcm::ValEntry*>(d) )
139          {   
140              copy->GetFile()->InsertValEntry( v->GetValue(),
141                                               v->GetGroup(),v->GetElement(),
142                                               v->GetVR() ); 
143          }
144          else
145          {
146           // We skip gdcm::SeqEntries
147          }
148       }
149       d = f1->GetNextEntry();
150    }
151
152    int imageSize = fh1->GetImageDataSize();
153 // Black up all 'grey' pixels
154    int i;
155    int n = 0;
156    for (i = 0; i<imageSize/3; i++)
157    {
158       if ( ((rgb8_t *)imageData)[i].r == ((rgb8_t *)imageData)[i].g
159          &&
160            ((rgb8_t *)imageData)[i].r == ((rgb8_t *)imageData)[i].b )
161       {
162          n++;
163          ((rgb8_t *)imageData)[i].r = (unsigned char)background;
164          ((rgb8_t *)imageData)[i].g = (unsigned char)background;
165          ((rgb8_t *)imageData)[i].b = (unsigned char)background;
166       }
167    }
168    
169     std::cout << n << " points put to black (within " 
170               << imageSize/3 << ")" << std::endl;
171
172    n = 0;
173    for (i = 0; i<imageSize/3; i++)
174    {
175    if ( ((rgb8_t *)imageData)[i].r < threshold
176      &&
177         ((rgb8_t *)imageData)[i].g < threshold
178      &&
179         ((rgb8_t *)imageData)[i].b < threshold )
180       {
181          n++;
182         ((rgb8_t *)imageData)[i].r = (unsigned char)background;
183         ((rgb8_t *)imageData)[i].g = (unsigned char)background;
184         ((rgb8_t *)imageData)[i].b = (unsigned char)background;  
185       }
186    }
187    
188    std::cout << n << " points put to black (within " 
189              << imageSize/3 << ")" << std::endl; 
190    // User knows the image is a 'color' one -RGB, YBR, Palette Color-
191    // and wants to write it as RGB
192    copy->SetImageData(imageData, imageSize);
193    copy->SetWriteModeToRGB();
194
195    copy->WriteDcmExplVR( output );
196
197    delete f1;
198    delete fh1;
199    delete copy;
200
201    exit (0);
202 }