]> Creatis software - gdcm.git/blob - Example/exImageLighten.cxx
67d5a464351f3b4838cfcebd12d670c6f06e4add
[gdcm.git] / Example / exImageLighten.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: exImageLighten.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/07/19 15:19:25 $
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 int main(int argc, char *argv[])
29
30    std::cout << "-----------------------------------------------" << std::endl;
31    std::cout << "Removes from a full gdcm-readable  Dicom image"  << std::endl;
32    std::cout << " all the 'Shadow groups' and the 'Sequence' entries"
33           << " Warning :  'Compressed images' are uncompressed" 
34           << std::endl
35           << "         :  'YBR images' are converted into 'RGB images'"
36           << std::endl
37           << "         :  'RGB planes' are converted into 'RGB pixels'"
38           << std::endl
39           << "         :  'Palette Color images' are kept 'as is'"
40           << std::endl;
41    std::cout << "-----------------------------------------------" << std::endl;
42
43    if( argc < 3 )
44     {
45        std::cerr << "Usage " << argv[0]  << " Source image.dcm  " 
46                  << " Output image.dcm " << std::endl;
47        return 1;
48     }
49
50    std::string fileName       = argv[1];
51    std::string output         = argv[2];
52
53 // ============================================================
54 //   Read the input image.
55 // ============================================================
56
57    std::cout << argv[1] << std::endl;
58
59    gdcm::File *f = new gdcm::File();
60    f->SetLoadMode( 0x00000000);
61    f->SetFileName( fileName );
62    bool res = f->Load();        
63
64    if (!res) {
65        std::cerr << "Sorry, " << fileName <<"  not a gdcm-readable "
66                  << "DICOM / ACR File"
67                  <<std::endl;
68        return 0;
69    }
70    std::cout << " ... is readable " << std::endl;
71
72 // ============================================================
73 //   Read the Pixels
74 //
75 // ============================================================
76  
77    // Pixel Reading must be done here, to be sure 
78    // to load the Palettes Color (if any)
79
80    // First, create a gdcm::FileHelper
81    gdcm::FileHelper *fh = new gdcm::FileHelper(f);
82
83    // Load the pixels, DO NOT transform LUT (if any) into RGB Pixels 
84    uint8_t *imageDataRaw = fh->GetImageDataRaw();
85    // Get the image data size
86    size_t dataRawSize    = fh->GetImageDataRawSize();
87
88 // ============================================================
89 //   Create a new gdcm::Filehelper, to hold new image.
90 // ============================================================
91
92    gdcm::FileHelper *copy = new gdcm::FileHelper( );
93    copy->SetFileName( output );
94    copy->Load();
95
96 // ============================================================
97 //   Selective copy of the entries (including Pixel Element).
98 // ============================================================
99
100    gdcm::DocEntry *d = f->GetFirstEntry();
101  
102    d = f->GetFirstEntry();
103    while(d)
104    {
105       // We skip SeqEntries, since user cannot do much with them
106       if ( !(dynamic_cast<gdcm::SeqEntry*>(d))
107       // We skip Shadow Groups, since nobody knows what they mean
108            && !( d->GetGroup()%2 ) )
109       { 
110
111          if ( gdcm::BinEntry *b = dynamic_cast<gdcm::BinEntry*>(d) )
112          {              
113             copy->GetFile()->InsertBinEntry( b->GetBinArea(),b->GetLength(),
114                                              b->GetGroup(),b->GetElement(),
115                                              b->GetVR() );
116          }
117          else if ( gdcm::ValEntry *v = dynamic_cast<gdcm::ValEntry*>(d) )
118          {   
119              copy->GetFile()->InsertValEntry( v->GetValue(),
120                                               v->GetGroup(),v->GetElement(),
121                                               v->GetVR() ); 
122          }
123          else
124          {
125           // We skip gdcm::SeqEntries
126          }
127       }
128       d = f->GetNextEntry();
129    }
130    
131    // User wants to keep the Palette Color -if any- 
132    // and write the image as it was
133    copy->SetImageData(imageDataRaw, dataRawSize);
134    copy->SetWriteModeToRaw();
135    copy->WriteDcmExplVR( output );
136
137    std::cout << std::endl
138              << "------------------------------------------------------------"
139              << std::endl;
140    delete f;
141    delete fh;
142    delete copy;
143
144    exit (0);
145 }
146