1 /*=========================================================================
4 Module: $RCSfile: exGC.cxx,v $
6 Date: $Date: 2005/04/20 11:25:35 $
7 Version: $Revision: 1.3 $
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.
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.
17 =========================================================================*/
20 #include "gdcmFileHelper.h"
21 #include "gdcmDocument.h"
22 #include "gdcmValEntry.h"
23 #include "gdcmBinEntry.h"
24 #include "gdcmSeqEntry.h"
26 typedef struct // Maybe we should add it to gdcm ?
34 // This small application, for a given Creatis user (G.C.)
35 // may be taken as an example
37 int main(int argc, char *argv[])
40 // we need a user friendly way for passign parameters on the command line !
42 std::cout << "------------------------------------------------" << std::endl;
43 std::cout << "Transforms a full gdcm-readable 'color' Dicom image "
44 << " (e.g Palette Color, YBR, 3-Planes RGB) "
45 << " into an 'RGB_Pixels' Dicom Image " << std::endl
46 << " Blacks out any 'grey' pixel (r=g=b) "
47 << " Blacks out any 'dark' pixel (r,g,b < threshold) "
49 std::cout << "------------------------------------------------" << std::endl;
53 std::cerr << "Usage :" << std::endl <<
54 argv[0] << " input_dicom output_dicom threshold background" << std::endl;
58 std::string fileName = argv[1];
59 std::string output = argv[2];
63 threshold = atoi( argv[3] );
67 background = atoi( argv[4] );
69 // ============================================================
70 // Read the input image.
71 // ============================================================
72 // a gdcm::File contains all the Dicom Field but the Pixels Element
74 gdcm::File *f1= new gdcm::File( fileName );
77 std::cout << argv[1] << std::endl;
79 f1 = new gdcm::File( fileName );
80 if (!f1->IsReadable()) {
81 std::cerr << "Sorry, " << fileName <<" not a gdcm-readable "
86 std::cout << " ... is readable " << std::endl;
89 if (!f1->IsMonochrome()) {
90 std::cerr << "Sorry, " << fileName <<" not a 'color' File "
97 // ============================================================
98 // Load the pixels in memory.
99 // ============================================================
101 // We need a gdcm::FileHelper, since we want to load the pixels
102 gdcm::FileHelper *fh1 = new gdcm::FileHelper(f1);
104 // (unit8_t DOESN'T mean it's mandatory for the image to be a 8 bits one)
106 uint8_t *imageData = fh1->GetImageData();
108 if ( imageData == 0 )
110 std::cerr << "Sorry, Pixels of" << fileName <<" are not "
111 << " gdcm-readable." << std::endl;
115 // ------ User wants write a new image without shadow groups -------------
116 // ------ without Sequences -------------
119 gdcm::FileHelper *copy = new gdcm::FileHelper( output );
121 gdcm::DocEntry *d = f1->GetFirstEntry();
124 // We skip SeqEntries, since user cannot do much with them
125 if ( !(dynamic_cast<gdcm::SeqEntry*>(d))
126 // We skip Shadow Groups, since nobody knows what they mean
127 && !( d->GetGroup()%2 ) )
130 if ( gdcm::BinEntry *b = dynamic_cast<gdcm::BinEntry*>(d) )
132 copy->GetFile()->InsertBinEntry( b->GetBinArea(),b->GetLength(),
133 b->GetGroup(),b->GetElement(),
136 else if ( gdcm::ValEntry *v = dynamic_cast<gdcm::ValEntry*>(d) )
138 copy->GetFile()->InsertValEntry( v->GetValue(),
139 v->GetGroup(),v->GetElement(),
144 // We skip gdcm::SeqEntries
147 d = f1->GetNextEntry();
150 int imageSize = fh1->GetImageDataSize();
151 // Black up all 'grey' pixels
154 for (i = 0; i<imageSize/3; i++)
156 if ( ((rgb8_t *)imageData)[i].r == ((rgb8_t *)imageData)[i].g
158 ((rgb8_t *)imageData)[i].r == ((rgb8_t *)imageData)[i].b )
161 ((rgb8_t *)imageData)[i].r = (unsigned char)background;
162 ((rgb8_t *)imageData)[i].g = (unsigned char)background;
163 ((rgb8_t *)imageData)[i].b = (unsigned char)background;
167 std::cout << n << " points put to black (within "
168 << imageSize/3 << ")" << std::endl;
171 for (i = 0; i<imageSize/3; i++)
173 if ( ((rgb8_t *)imageData)[i].r < threshold
175 ((rgb8_t *)imageData)[i].g < threshold
177 ((rgb8_t *)imageData)[i].b < threshold )
180 ((rgb8_t *)imageData)[i].r = (unsigned char)background;
181 ((rgb8_t *)imageData)[i].g = (unsigned char)background;
182 ((rgb8_t *)imageData)[i].b = (unsigned char)background;
186 std::cout << n << " points put to black (within "
187 << imageSize/3 << ")" << std::endl;
188 // User knows the image is a 'color' one -RGB, YBR, Palette Color-
189 // and wants to write it as RGB
190 copy->SetImageData(imageData, imageSize);
191 copy->SetWriteModeToRGB();
193 copy->WriteDcmExplVR( output );