1 /*=========================================================================
4 Module: $RCSfile: exColorToRGB.cxx,v $
6 Date: $Date: 2005/07/19 15:19:25 $
7 Version: $Revision: 1.4 $
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 #include <stdlib.h> // for exit
28 int main(int argc, char *argv[])
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 "
39 std::cerr << "Usage :" << std::endl <<
40 argv[0] << " input_dicom output_dicom" << std::endl;
44 std::string fileName = argv[1];
45 std::string output = argv[2];
48 // ============================================================
49 // Read the input image.
50 // ============================================================
51 // a gdcm::File contains all the Dicom Field but the Pixels Element
53 std::cout << argv[1] << std::endl;
55 gdcm::File *f = new gdcm::File();
56 f->SetLoadMode( 0x00000000);
57 f->SetFileName( fileName );
61 std::cerr << "Sorry, " << fileName <<" not a gdcm-readable "
66 std::cout << " ... is readable " << std::endl;
69 if (!f->IsMonochrome()) {
70 std::cerr << "Sorry, " << fileName <<" not a 'color' File "
77 // ============================================================
78 // Load the pixels in memory.
79 // ============================================================
81 // We need a gdcm::FileHelper, since we want to load the pixels
82 gdcm::FileHelper *fh = new gdcm::FileHelper(f);
84 // (unit8_t DOESN'T mean it's mandatory for the image to be a 8 bits one)
86 uint8_t *imageData = fh->GetImageData();
90 std::cerr << "Sorry, Pixels of" << fileName <<" are not "
91 << " gdcm-readable." << std::endl;
96 // ------ User wants write a new image without shadow groups -------------
97 // ------ without Sequences -------------
100 gdcm::FileHelper *copy = new gdcm::FileHelper( );
101 copy->SetFileName( output );
104 gdcm::DocEntry *d = f->GetFirstEntry();
107 // We skip SeqEntries, since user cannot do much with them
108 if ( !(dynamic_cast<gdcm::SeqEntry*>(d))
109 // We skip Shadow Groups, since nobody knows what they mean
110 && !( d->GetGroup()%2 ) )
113 if ( gdcm::BinEntry *b = dynamic_cast<gdcm::BinEntry*>(d) )
115 copy->GetFile()->InsertBinEntry( b->GetBinArea(),b->GetLength(),
116 b->GetGroup(),b->GetElement(),
119 else if ( gdcm::ValEntry *v = dynamic_cast<gdcm::ValEntry*>(d) )
121 copy->GetFile()->InsertValEntry( v->GetValue(),
122 v->GetGroup(),v->GetElement(),
127 // We skip gdcm::SeqEntries
130 d = f->GetNextEntry();
133 // User knows the image is a 'color' one -RGB, YBR, Palette Color-
134 // and wants to write it as RGB
135 copy->SetImageData(imageData, fh->GetImageDataSize());
136 copy->SetWriteModeToRGB();
138 copy->WriteDcmExplVR( output );