]> Creatis software - gdcm.git/blob - Example/PrintFile.cxx
PrintFile can now print *all* the file within a given directory.
[gdcm.git] / Example / PrintFile.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: PrintFile.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/08/28 17:02:34 $
7   Version:   $Revision: 1.47 $
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 #include "gdcmFile.h"
19 #include "gdcmSeqEntry.h"
20 #include "gdcmSQItem.h"
21 #include "gdcmBinEntry.h"
22
23 #include "gdcmFileHelper.h"
24 #include "gdcmDebug.h"
25 #include "gdcmDirList.h"
26
27 #include "gdcmArgMgr.h"
28
29 #include <iostream>
30
31
32 void ShowLutData(gdcm::File *f);
33
34 void ShowLutData(gdcm::File *f)
35 {
36      // Nothing is written yet to get LUT Data user friendly
37      // The following is to be moved into a PixelReadConvert method
38      // Let here, waiting for a clever idea on the way to do it.
39   
40       gdcm::SeqEntry *modLutSeq = f->GetSeqEntry(0x0028,0x3000);
41       if ( modLutSeq !=0 )
42       {
43          gdcm::SQItem *sqi= modLutSeq->GetFirstSQItem();
44          if ( sqi != 0 )
45          {
46             std::string lutDescriptor = sqi->GetEntryValue(0x0028,0x3002);
47            if (   /*lutDescriptor   == GDCM_UNFOUND*/ 0 )
48            {
49               //gdcmWarningMacro( "LUT Descriptor is missing" );
50               std::cout << "LUT Descriptor is missing" << std::endl;
51               return;
52             }
53             int length;   // LUT length in Bytes
54             int deb;      // Subscript of the first Lut Value
55             int nbits;    // Lut item size (in Bits)
56
57             int nbRead;    // nb of items in LUT descriptor (must be = 3)
58
59             nbRead = sscanf( lutDescriptor.c_str(),
60                               "%d\\%d\\%d",
61                                &length, &deb, &nbits );
62            std::cout << "length " << length 
63                      << " deb " << deb 
64                      << " nbits " << nbits
65                      << std::endl;
66             if ( nbRead != 3 )
67             {
68                 //gdcmWarningMacro( "Wrong LUT descriptor" );
69                 std::cout << "Wrong LUT descriptor" << std::endl;
70             }
71             //LUT Data (CTX dependent)    
72             gdcm::BinEntry *b = sqi->GetBinEntry(0x0028,0x3006); 
73             if ( b != 0 )
74             { 
75                int BitsAllocated = f->GetBitsAllocated();
76                if ( BitsAllocated <= 8 )
77                { 
78                   int mult;
79                   if ( ( nbits == 16 ) && ( BitsAllocated == 8 ) )
80                   {
81                   // when LUT item size is different than pixel size
82                      mult = 2; // high byte must be = low byte
83                   }
84                   else
85                   {
86                   // See PS 3.3-2003 C.11.1.1.2 p 619
87                      mult = 1;
88                   }
89                   uint8_t *lut = b->GetBinArea();
90                   for( int i=0; i < length; ++i )
91                   {
92                      std::cout << i+deb << " : \t"
93                                << (int) (lut[i*mult + 1]) << std::endl;
94                   }
95                }
96                else
97                {
98                   uint16_t *lut = (uint16_t *)(b->GetBinArea());  
99                   for( int i=0; i < length; ++i )
100                   {
101                      std::cout << i+deb << " : \t"
102                                << (int) (((uint16_t *)lut)[i])
103                                << std::endl;
104                   }             
105                }
106             }  
107             else
108                std::cout << "No LUT Data BinEntry (0x0028,0x3006) found?!? " 
109                          << std::endl;
110          }
111          else
112             std::cout << "No First SQ Item within (0x0028,0x3000) ?!? " 
113                       << std::endl;      
114       }
115       else
116          std::cout << "No LUT Data SeqEntry (0x0028,0x3000) found " 
117                    << std::endl;
118    }
119
120 int main(int argc, char *argv[])
121 {
122
123    START_USAGE(usage)
124    " \n PrintFile : \n                                                       ",
125    " Display the header of a ACR-NEMA/PAPYRUS/DICOM File                     ",
126    " usage: PrintFile {filein=inputFileName|dirin=inputDirectoryName}[level=n]", 
127    "                       [ { [noshadowseq] | [noshadow][noseq] } ] [debug]  ",
128    "        level = 0,1,2 : depending on the amount of details user wants to see",
129    "        noshadowseq: user doesn't want to load Private Sequences          ",
130    "        noshadow   : user doesn't want to load Private groups (odd number)",
131    "        noseq      : user doesn't want to load Sequences                  ",
132    "        debug      : user wants to run the program in 'debug mode'        ",
133    "        showlut :user wants to display the Palette Color (as an int array)",
134    FINISH_USAGE
135
136    // Initialize Arguments Manager   
137    gdcm::ArgMgr *am= new gdcm::ArgMgr(argc, argv);
138   
139    if (argc == 1 || am->ArgMgrDefined("usage") )
140    {
141       am->ArgMgrUsage(usage); // Display 'usage'
142       delete am;
143       return 0;
144    }
145
146    char *fileName = am->ArgMgrGetString("filein",(char *)0);
147    char *dirName  = am->ArgMgrGetString("dirin",(char *)0);
148
149    if ( (fileName == 0 && dirName == 0)
150         ||
151         (fileName != 0 && dirName != 0) )
152    {
153        std::cout <<std::endl
154                  << "Either 'filein=' or 'dirin=' must be present;" 
155                  << std::endl << "Not both" << std::endl;
156        am->ArgMgrUsage(usage); // Display 'usage'  
157        delete am;
158        return 0;
159  }
160
161    if (am->ArgMgrDefined("debug"))
162       gdcm::Debug::DebugOn();
163  
164    int loadMode = 0x00000000;
165    if ( am->ArgMgrDefined("noshadowseq") )
166       loadMode |= NO_SHADOWSEQ;
167    else 
168    {
169    if ( am->ArgMgrDefined("noshadow") )
170          loadMode |= NO_SHADOW;
171       if ( am->ArgMgrDefined("noseq") )
172          loadMode |= NO_SEQ;
173    }
174
175    int level = am->ArgMgrGetInt("level", 2);
176
177    bool showlut = ( 0 != am->ArgMgrDefined("SHOWLUT") );
178
179    /* if unused Param we give up */
180    if ( am->ArgMgrPrintUnusedLabels() )
181    {
182       am->ArgMgrUsage(usage);
183       delete am;
184       return 0;
185    } 
186
187    delete am;  // we don't need Argument Manager any longer
188
189    // ----------- End Arguments Manager ---------
190
191    if ( fileName != 0 ) // ====== Deal with a single file ======
192    { 
193       // gdcm::File::IsReadable() is no usable here, because we deal with
194       // any kind of gdcm-Parsable *document* 
195       // not only gdcm::File (as opposed to gdcm::DicomDir)
196
197       gdcm::File *f = new gdcm::File();
198       f->SetLoadMode(loadMode);
199       f->SetFileName( fileName );
200       bool res = f->Load();
201
202       if ( !res )
203       {
204          std::cout << "Cannot process file [" << fileName << "]" << std::endl;
205          std::cout << "Either it doesn't exist, or it's read protected " 
206                    << std::endl;
207          std::cout << "or it's not a Dicom File, or its 'header' is bugged" 
208                    << std::endl;
209          std::cout << "use 'PrintFile filein=... debug' to try to guess the pb"
210                    << std::endl;
211          delete f;
212          return 0;
213       }
214
215       gdcm::FileHelper *fh = new gdcm::FileHelper(f);
216       fh->SetPrintLevel( level );
217
218       fh->Print();
219
220       std::cout << "\n\n" << std::endl; 
221
222       std::cout <<std::endl;
223       std::cout <<" dataSize    " << fh->GetImageDataSize()    << std::endl;
224       std::cout <<" dataSizeRaw " << fh->GetImageDataRawSize() << std::endl;
225
226       int nX,nY,nZ,sPP,planarConfig;
227       std::string pixelType;
228       nX=f->GetXSize();
229       nY=f->GetYSize();
230       nZ=f->GetZSize();
231       std::cout << " DIMX=" << nX << " DIMY=" << nY << " DIMZ=" << nZ << std::endl;
232
233       pixelType    = f->GetPixelType();
234       sPP          = f->GetSamplesPerPixel();
235       planarConfig = f->GetPlanarConfiguration();
236
237       std::cout << " pixelType= ["            << pixelType 
238                 << "] SamplesPerPixel= ["     << sPP
239                 << "] PlanarConfiguration= [" << planarConfig 
240                 << "] "<< std::endl 
241                 << " PhotometricInterpretation= [" 
242                                 << f->GetEntryValue(0x0028,0x0004)
243                 << "] "<< std::endl;
244
245       int numberOfScalarComponents=f->GetNumberOfScalarComponents();
246       std::cout << " NumberOfScalarComponents = " << numberOfScalarComponents 
247                 <<std::endl
248                 << " LUT = " << (f->HasLUT() ? "TRUE" : "FALSE")
249                 << std::endl;
250
251       if ( f->GetEntryValue(0x0002,0x0010) == gdcm::GDCM_NOTLOADED ) 
252       {
253          std::cout << "Transfer Syntax not loaded. " << std::endl
254                    << "Better you increase MAX_SIZE_LOAD_ELEMENT_VALUE"
255                 << std::endl;
256          return 0;
257       }
258   
259       std::string transferSyntaxName = f->GetTransferSyntaxName();
260       std::cout << " TransferSyntaxName= [" << transferSyntaxName << "]" 
261                 << std::endl;
262       std::cout << " SwapCode= " << f->GetSwapCode() << std::endl;
263
264       //std::cout << "\n\n" << std::endl; 
265       //std::cout << "X spacing " << f->GetXSpacing() << std::endl;
266       //std::cout << "Y spacing " << f->GetYSpacing() << std::endl;
267       //std::cout << "Z spacing " << f->GetZSpacing() << std::endl;
268
269       // Display the LUT as an int array (for debugging purpose)
270       if ( f->HasLUT() && showlut )
271       {
272          uint8_t* lutrgba = fh->GetLutRGBA();
273          if ( lutrgba == 0 )
274          {
275             std::cout << "Lut RGBA (Palette Color) not built " << std::endl;
276  
277            // Nothing is written yet to get LUT Data user friendly
278            // The following is to be moved into a PixelRedaConvert method
279   
280             gdcm::SeqEntry *modLutSeq = f->GetSeqEntry(0x0028,0x3000);
281             if ( modLutSeq !=0 )
282             {
283                gdcm::SQItem *sqi= modLutSeq->GetFirstSQItem();
284                if ( !sqi )
285                {
286                std::string lutDescriptor = sqi->GetEntryValue(0x0028,0x3002);
287                   int length;   // LUT length in Bytes
288                   int deb;      // Subscript of the first Lut Value
289                   int nbits;    // Lut item size (in Bits)
290                   int nbRead;    // nb of items in LUT descriptor (must be = 3)
291
292                   nbRead = sscanf( lutDescriptor.c_str(),
293                                     "%d\\%d\\%d",
294                                      &length, &deb, &nbits );
295                   if ( nbRead != 3 )
296                   {
297                       //gdcmWarningMacro( "Wrong LUT descriptor" );
298                       std::cout << "Wrong LUT descriptor" << std::endl;
299                   }                                                  
300                   gdcm::BinEntry *b = sqi->GetBinEntry(0x0028,0x3006);
301                   if ( b != 0 )
302                   {
303                      if ( b->GetLength() != 0 )
304                      {
305                         std::cout << "---------------------------------------"
306                                << " We should never reach this point      "
307                                << std::endl;
308                         //LoadEntryBinArea(b);    //LUT Data (CTX dependent)
309                      }   
310                  }
311               }      
312             }
313             else
314                std::cout << "No LUT Data (0x0028,0x3000) found " << std::endl;
315         }
316          else
317          {
318             if ( fh->GetLutItemSize() == 8 )
319             {
320                for (int i=0;i<fh->GetLutItemNumber();i++)
321                   std::cout << i << " : \t"
322                          << (int)(lutrgba[i*4])   << " "
323                          << (int)(lutrgba[i*4+1]) << " "
324                          << (int)(lutrgba[i*4+2]) << std::endl;
325             }
326             else // LutItemSize assumed to be = 16
327             {
328                uint16_t* lutrgba16 = (uint16_t*)lutrgba;
329                for (int i=0;i<fh->GetLutItemNumber();i++)
330                   std::cout << i << " : \t"
331                          << (int)(lutrgba16[i*4])   << " "
332                          << (int)(lutrgba16[i*4+1]) << " "
333                          << (int)(lutrgba16[i*4+2]) << std::endl;
334             }
335          }
336       }
337       else if (showlut)
338       {
339          std::cout << "Try LUT Data "<< std::endl;
340          ShowLutData(f);
341       }
342      
343       if (f->IsReadable())
344          std::cout <<std::endl<<fileName<<" is Readable"<<std::endl;
345       else
346          std::cout <<std::endl<<fileName<<" is NOT Readable"<<std::endl;
347       std::cout<<std::flush;
348       delete f;
349       delete fh;
350       return 0;
351    }
352    else  // ====== Deal with a Directory ======
353    {
354       std::cout << "dirName [" << dirName << "]" << std::endl;
355       gdcm::DirList dirList(dirName,1); // gets recursively the file list
356       gdcm::DirListType fileList = dirList.GetFilenames();
357       gdcm::File *f;
358       bool res;
359       for( gdcm::DirListType::iterator it  = fileList.begin();
360                                  it != fileList.end();
361                                  ++it )
362       {
363          f = new gdcm::File();
364          f->SetLoadMode(loadMode);
365          f->SetFileName( it->c_str() );
366          res = f->Load();
367
368          if ( !res )
369          {
370             std::cout << "Cannot process file [" << it->c_str() << "]" 
371                       << std::endl;
372             std::cout << "Either it doesn't exist, or it's read protected " 
373                       << std::endl;
374             std::cout << "or it's not a Dicom File, or its 'header' is bugged" 
375                       << std::endl;
376             std::cout << "use 'PrintFile filein=... debug' to try to guess the pb"
377                    << std::endl;
378             delete f;
379             continue;
380          }
381
382          gdcm::FileHelper *fh = new gdcm::FileHelper(f);
383          fh->SetPrintLevel( level );
384
385          fh->Print();
386
387          if (f->IsReadable())
388             std::cout <<std::endl<<it->c_str()<<" is Readable"<<std::endl;
389          else
390             std::cout <<std::endl<<it->c_str()<<" is NOT Readable"<<std::endl;
391          std::cout << "\n\n" << std::endl;
392          delete f;
393          delete fh;
394       }
395       std::cout<<std::flush;
396    }
397 }