]> Creatis software - gdcm.git/blob - Example/exImageLighten.cxx
Fix mistypings
[gdcm.git] / Example / exImageLighten.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: exImageLighten.cxx,v $
5   Language:  C++
6   Date:      $Date: 2007/05/23 14:18:05 $
7   Version:   $Revision: 1.10 $
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 int main(int argc, char *argv[])
28
29    std::cout << "-----------------------------------------------" << std::endl;
30    std::cout << "Removes from a full gdcm-readable  Dicom image"  << std::endl;
31    std::cout << " all the 'Shadow groups' and the 'Sequence' entries"
32           << " Warning :  'Compressed images' are uncompressed" 
33           << std::endl
34           << "         :  'YBR images' are converted into 'RGB images'"
35           << std::endl
36           << "         :  'RGB planes' are converted into 'RGB pixels'"
37           << std::endl
38           << "         :  'Palette Color images' are kept 'as is'"
39           << std::endl;
40    std::cout << "-----------------------------------------------" << std::endl;
41
42    if( argc < 3 )
43     {
44        std::cerr << "Usage " << argv[0]  << " Source image.dcm  " 
45                  << " Output image.dcm " << std::endl;
46        return 1;
47     }
48
49    std::string fileName       = argv[1];
50    std::string output         = argv[2];
51
52 // ============================================================
53 //   Read the input image.
54 // ============================================================
55
56    std::cout << argv[1] << std::endl;
57
58    GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
59    f->SetLoadMode( GDCM_NAME_SPACE::LD_ALL);
60    f->SetFileName( fileName );
61    bool res = f->Load();        
62
63    if (!res) {
64        std::cerr << "Sorry, " << fileName <<"  not a gdcm-readable "
65                  << "DICOM / ACR File"
66                  <<std::endl;
67        f->Delete();
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_NAME_SPACE::FileHelper
81    GDCM_NAME_SPACE::FileHelper *fh = GDCM_NAME_SPACE::FileHelper::New(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_NAME_SPACE::Filehelper, to hold new image.
90 // ============================================================
91
92    GDCM_NAME_SPACE::FileHelper *copy = GDCM_NAME_SPACE::FileHelper::New( );
93    copy->SetFileName( output );
94    copy->Load();
95
96 // ============================================================
97 //   Selective copy of the entries (including Pixel Element).
98 // ============================================================
99
100    GDCM_NAME_SPACE::DocEntry *d = f->GetFirstEntry();
101    while(d)
102    {
103       // We skip SeqEntries, since user cannot do much with them
104       if ( !(dynamic_cast<GDCM_NAME_SPACE::SeqEntry*>(d))
105       // We skip Shadow Groups, since nobody knows what they mean
106            && !( d->GetGroup()%2 ) )
107       { 
108
109          if ( GDCM_NAME_SPACE::DataEntry *de = dynamic_cast<GDCM_NAME_SPACE::DataEntry *>(d) )
110          {              
111             copy->GetFile()->InsertEntryBinArea( de->GetBinArea(),de->GetLength(),
112                                                  de->GetGroup(),de->GetElement(),
113                                                  de->GetVR() );
114          }
115          else
116          {
117           // We skip GDCM_NAME_SPACE::SeqEntries
118          }
119       }
120       d = f->GetNextEntry();
121    }
122    
123    // User wants to keep the Palette Color -if any- 
124    // and write the image as it was
125    copy->SetImageData(imageDataRaw, dataRawSize);
126    copy->SetWriteModeToRaw();
127    copy->WriteDcmExplVR( output );
128
129    std::cout << std::endl
130              << "------------------------------------------------------------"
131              << std::endl;
132
133    f->Delete();
134    fh->Delete();
135    copy->Delete();
136
137    exit (0);
138 }
139