]> Creatis software - gdcm.git/blob - Example/exGC.cxx
* Minor coding-style clean up
[gdcm.git] / Example / exGC.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: exGC.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/10/18 08:35:44 $
7   Version:   $Revision: 1.9 $
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 "gdcmDataEntry.h"
23 #include "gdcmSeqEntry.h"
24
25 #include <stdlib.h> // for exit
26
27 typedef struct  // Maybe we should add it to gdcm ?
28 {
29    uint8_t r;
30    uint8_t g;
31    uint8_t b;
32 } rgb8_t;
33
34
35 // This small application, for a given Creatis user (G.C.)
36 // may be taken as an example
37
38 int main(int argc, char *argv[])
39 {
40
41    // we need a user friendly way for passign parameters on the command line !
42
43    std::cout << "------------------------------------------------" << std::endl;
44    std::cout << "Transforms a full gdcm-readable 'color' Dicom image "
45           << " (e.g Palette Color, YBR, 3-Planes RGB) "
46           << " into an 'RGB_Pixels' Dicom Image " << std::endl
47           << " Blacks out any 'grey' pixel (r=g=b) "
48           << " Blacks out any 'dark' pixel (r,g,b < threshold) " 
49           << " "            << std::endl;
50    std::cout << "------------------------------------------------" << std::endl;
51
52    if (argc < 3)
53    {
54       std::cerr << "Usage :" << std::endl << 
55       argv[0] << " input_dicom output_dicom threshold background" << std::endl;
56       return 1;
57    }
58
59    std::string fileName = argv[1];
60    std::string output   = argv[2];
61
62    int threshold = 0;
63    if (argc > 3)
64       threshold        = atoi( argv[3] );
65   
66    int background = 0;
67    if (argc > 4)
68       background        = atoi( argv[4] );
69
70 // ============================================================
71 //   Read the input image.
72 // ============================================================
73    // a gdcm::File contains all the Dicom Field but the Pixels Element
74
75    std::cout << argv[1] << std::endl;
76
77    gdcm::File *f = new gdcm::File();
78    f->SetLoadMode( gdcm::LD_ALL);
79    f->SetFileName( fileName );
80    bool res = f->Load();        
81
82    if (!res) {
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 (!f->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 *fh = new gdcm::FileHelper(f);
105
106    // (unit8_t DOESN'T mean it's mandatory for the image to be a 8 bits one) 
107
108    uint8_t *imageData = fh->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( );
122    copy->SetFileName( output );
123    copy->Load();
124  
125    gdcm::DocEntry *d = f->GetFirstEntry();
126    while(d)
127    {
128       // We skip SeqEntries, since user cannot do much with them
129       if ( !(dynamic_cast<gdcm::SeqEntry*>(d))
130       // We skip Shadow Groups, since nobody knows what they mean
131            && !( d->GetGroup()%2 ) )
132       { 
133
134          if ( gdcm::DataEntry *de = dynamic_cast<gdcm::DataEntry *>(d) )
135          {              
136             copy->GetFile()->InsertEntryBinArea( de->GetBinArea(),de->GetLength(),
137                                                  de->GetGroup(),de->GetElement(),
138                                                  de->GetVR() );
139          }
140          else
141          {
142           // We skip gdcm::SeqEntries
143          }
144       }
145       d = f->GetNextEntry();
146    }
147
148    int imageSize = fh->GetImageDataSize();
149 // Black up all 'grey' pixels
150    int i;
151    int n = 0;
152    for (i = 0; i<imageSize/3; i++)
153    {
154       if ( ((rgb8_t *)imageData)[i].r == ((rgb8_t *)imageData)[i].g
155          &&
156            ((rgb8_t *)imageData)[i].r == ((rgb8_t *)imageData)[i].b )
157       {
158          n++;
159          ((rgb8_t *)imageData)[i].r = (unsigned char)background;
160          ((rgb8_t *)imageData)[i].g = (unsigned char)background;
161          ((rgb8_t *)imageData)[i].b = (unsigned char)background;
162       }
163    }
164    
165     std::cout << n << " points put to black (within " 
166               << imageSize/3 << ")" << std::endl;
167
168    n = 0;
169    for (i = 0; i<imageSize/3; i++)
170    {
171    if ( ((rgb8_t *)imageData)[i].r < threshold
172      &&
173         ((rgb8_t *)imageData)[i].g < threshold
174      &&
175         ((rgb8_t *)imageData)[i].b < threshold )
176       {
177          n++;
178         ((rgb8_t *)imageData)[i].r = (unsigned char)background;
179         ((rgb8_t *)imageData)[i].g = (unsigned char)background;
180         ((rgb8_t *)imageData)[i].b = (unsigned char)background;  
181       }
182    }
183    
184    std::cout << n << " points put to black (within " 
185              << imageSize/3 << ")" << std::endl; 
186    // User knows the image is a 'color' one -RGB, YBR, Palette Color-
187    // and wants to write it as RGB
188    copy->SetImageData(imageData, imageSize);
189    copy->SetWriteModeToRGB();
190
191    copy->WriteDcmExplVR( output );
192
193    delete f;
194    delete fh;
195    delete copy;
196
197    exit (0);
198 }