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