]> Creatis software - gdcm.git/blob - Example/exGC.cxx
* Rename the NO_SEQ, NO_SHADOW, NO_SHADOWSEQ to
[gdcm.git] / Example / exGC.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: exGC.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/08/30 14:40:28 $
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 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    std::cout << argv[1] << std::endl;
77
78    gdcm::File *f = new gdcm::File();
79    f->SetLoadMode( GDCM_LD_ALL);
80    f->SetFileName( fileName );
81    bool res = f->Load();        
82
83    if (!res) {
84        std::cerr << "Sorry, " << fileName <<"  not a gdcm-readable "
85                  << "DICOM / ACR File"
86                  <<std::endl;
87        return 0;
88    }
89    std::cout << " ... is readable " << std::endl;
90
91 /*
92    if (!f->IsMonochrome()) {
93        std::cerr << "Sorry, " << fileName <<"  not a 'color' File "
94            << " "
95                  <<std::endl;
96        return 0;
97    }
98 */
99
100 // ============================================================
101 //   Load the pixels in memory.
102 // ============================================================
103
104    // We need a gdcm::FileHelper, since we want to load the pixels        
105    gdcm::FileHelper *fh = new gdcm::FileHelper(f);
106
107    // (unit8_t DOESN'T mean it's mandatory for the image to be a 8 bits one) 
108
109    uint8_t *imageData = fh->GetImageData();
110
111    if ( imageData == 0 )
112    {
113        std::cerr << "Sorry, Pixels of" << fileName <<"  are not "
114                  << " gdcm-readable."  << std::endl;
115        return 0;
116    }
117
118    // ------ User wants write a new image without shadow groups -------------
119    // ------                              without Sequences     -------------
120
121  
122    gdcm::FileHelper *copy = new gdcm::FileHelper( );
123    copy->SetFileName( output );
124    copy->Load();
125  
126    gdcm::DocEntry *d = f->GetFirstEntry();
127    while(d)
128    {
129       // We skip SeqEntries, since user cannot do much with them
130       if ( !(dynamic_cast<gdcm::SeqEntry*>(d))
131       // We skip Shadow Groups, since nobody knows what they mean
132            && !( d->GetGroup()%2 ) )
133       { 
134
135          if ( gdcm::BinEntry *b = dynamic_cast<gdcm::BinEntry*>(d) )
136          {              
137             copy->GetFile()->InsertBinEntry( b->GetBinArea(),b->GetLength(),
138                                              b->GetGroup(),b->GetElement(),
139                                              b->GetVR() );
140          }
141          else if ( gdcm::ValEntry *v = dynamic_cast<gdcm::ValEntry*>(d) )
142          {   
143              copy->GetFile()->InsertValEntry( v->GetValue(),
144                                               v->GetGroup(),v->GetElement(),
145                                               v->GetVR() ); 
146          }
147          else
148          {
149           // We skip gdcm::SeqEntries
150          }
151       }
152       d = f->GetNextEntry();
153    }
154
155    int imageSize = fh->GetImageDataSize();
156 // Black up all 'grey' pixels
157    int i;
158    int n = 0;
159    for (i = 0; i<imageSize/3; i++)
160    {
161       if ( ((rgb8_t *)imageData)[i].r == ((rgb8_t *)imageData)[i].g
162          &&
163            ((rgb8_t *)imageData)[i].r == ((rgb8_t *)imageData)[i].b )
164       {
165          n++;
166          ((rgb8_t *)imageData)[i].r = (unsigned char)background;
167          ((rgb8_t *)imageData)[i].g = (unsigned char)background;
168          ((rgb8_t *)imageData)[i].b = (unsigned char)background;
169       }
170    }
171    
172     std::cout << n << " points put to black (within " 
173               << imageSize/3 << ")" << std::endl;
174
175    n = 0;
176    for (i = 0; i<imageSize/3; i++)
177    {
178    if ( ((rgb8_t *)imageData)[i].r < threshold
179      &&
180         ((rgb8_t *)imageData)[i].g < threshold
181      &&
182         ((rgb8_t *)imageData)[i].b < threshold )
183       {
184          n++;
185         ((rgb8_t *)imageData)[i].r = (unsigned char)background;
186         ((rgb8_t *)imageData)[i].g = (unsigned char)background;
187         ((rgb8_t *)imageData)[i].b = (unsigned char)background;  
188       }
189    }
190    
191    std::cout << n << " points put to black (within " 
192              << imageSize/3 << ")" << std::endl; 
193    // User knows the image is a 'color' one -RGB, YBR, Palette Color-
194    // and wants to write it as RGB
195    copy->SetImageData(imageData, imageSize);
196    copy->SetWriteModeToRGB();
197
198    copy->WriteDcmExplVR( output );
199
200    delete f;
201    delete fh;
202    delete copy;
203
204    exit (0);
205 }