]> Creatis software - gdcm.git/blob - Example/PrintFile.cxx
Fix warnings on Readablity/Parsabiblity
[gdcm.git] / Example / PrintFile.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: PrintFile.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/11/15 10:40:45 $
7   Version:   $Revision: 1.71 $
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 "gdcmDataEntry.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 #include "gdcmOrientation.h"
31 #include <iostream>
32
33 // TODO : code factorization, for 'single file' an 'whole directory' processing
34
35 void ShowLutData(gdcm::File *f);
36
37      // Nothing is written yet to get LUT Data user friendly
38      // The following is to be moved into a PixelReadConvert method
39      // Let here, waiting for a clever idea on the way to do it.
40
41 void ShowLutData(gdcm::File *f)
42 {  
43    gdcm::SeqEntry *modLutSeq = f->GetSeqEntry(0x0028,0x3000);
44    if ( modLutSeq !=0 )
45    {
46       gdcm::SQItem *sqi= modLutSeq->GetFirstSQItem();
47       if ( sqi != 0 )
48       {
49          std::string lutDescriptor = sqi->GetEntryString(0x0028,0x3002);
50          if (   /*lutDescriptor   == GDCM_UNFOUND*/ 0 )
51          {
52             //gdcmWarningMacro( "LUT Descriptor is missing" );
53             std::cout << "LUT Descriptor is missing" << std::endl;
54             return;
55          }
56          int length;   // LUT length in Bytes
57          int deb;      // Subscript of the first Lut Value
58          int nbits;    // Lut item size (in Bits)
59
60          int nbRead;    // nb of items in LUT descriptor (must be = 3)
61
62          nbRead = sscanf( lutDescriptor.c_str(),
63                            "%d\\%d\\%d",
64                               &length, &deb, &nbits );
65          std::cout << "length " << length 
66                   << " deb " << deb 
67                   << " nbits " << nbits
68                   << std::endl;
69          if ( nbRead != 3 )
70          {
71                //gdcmWarningMacro( "Wrong LUT descriptor" );
72                std::cout << "Wrong LUT descriptor" << std::endl;
73          }
74          //LUT Data (CTX dependent)    
75          gdcm::DataEntry *b = sqi->GetDataEntry(0x0028,0x3006); 
76          if ( b != 0 )
77          { 
78             int BitsAllocated = f->GetBitsAllocated();
79             if ( BitsAllocated <= 8 )
80             { 
81                int mult;
82                if ( ( nbits == 16 ) && ( BitsAllocated == 8 ) )
83                {
84                // when LUT item size is different than pixel size
85                   mult = 2; // high byte must be = low byte
86                }
87                else
88                {
89                // See PS 3.3-2003 C.11.1.1.2 p 619
90                   mult = 1;
91                }
92                uint8_t *lut = b->GetBinArea();
93                for( int i=0; i < length; ++i )
94                {
95                   std::cout << i+deb << " : \t"
96                               << (int) (lut[i*mult + 1]) << std::endl;
97                }
98             }
99             else
100             {
101                uint16_t *lut = (uint16_t *)(b->GetBinArea());  
102                for( int i=0; i < length; ++i )
103                {
104                   std::cout << i+deb << " : \t"
105                               << (int) (((uint16_t *)lut)[i])
106                               << std::endl;
107                }             
108             }
109          }  
110          else
111             std::cout << "No LUT Data DataEntry (0x0028,0x3006) found?!? " 
112                         << std::endl;
113       }
114       else
115          std::cout << "No First SQ Item within (0x0028,0x3000) ?!? " 
116                      << std::endl;      
117    }
118    else
119       std::cout << "No LUT Data SeqEntry (0x0028,0x3000) found " 
120                   << std::endl;
121 }
122
123 int main(int argc, char *argv[])
124 {
125
126    START_USAGE(usage)
127    " \n PrintFile : \n                                                        ",
128    " Display the header of a ACR-NEMA/PAPYRUS/DICOM File                      ",
129    " usage: PrintFile {filein=inputFileName|dirin=inputDirectoryName}[level=n]",
130    "                       [forceload=listOfElementsToForceLoad]              ",
131    "                       [dict= privateDirectory]                           ",
132    "                       [ { [noshadowseq] | [noshadow][noseq] } ]          ",
133    "                       [debug] [warning]                                  ",
134    "      level = 0,1,2 : depending on the amount of details user wants to see",
135    "      listOfElementsToForceLoad : group-elem,g2-e2,... (in hexa, no space)",
136    "                                of Elements to load whatever their length ",
137    "      privateDirectory : source file full path name of Shadow Group elems ",
138    "      noshadowseq: user doesn't want to load Private Sequences            ",
139    "      noshadow   : user doesn't want to load Private groups (odd number)  ",
140    "      noseq      : user doesn't want to load Sequences                    ",
141    "      debug      : user wants to run the program in 'debug mode'          ",
142    "      warning    : user wants to be warned about any oddity in the File   ",
143    "      showlut :user wants to display the Palette Color (as an int array)  ",
144    FINISH_USAGE
145
146    // Initialize Arguments Manager   
147    gdcm::ArgMgr *am= new gdcm::ArgMgr(argc, argv);
148   
149    if (argc == 1 || am->ArgMgrDefined("usage") )
150    {
151       am->ArgMgrUsage(usage); // Display 'usage'
152       delete am;
153       return 1;
154    }
155
156    char *fileName = am->ArgMgrGetString("filein",(char *)0);
157    char *dirName  = am->ArgMgrGetString("dirin",(char *)0);
158
159    if ( (fileName == 0 && dirName == 0) ||
160         (fileName != 0 && dirName != 0) )
161    {
162       std::cerr << std::endl
163         << "Either 'filein=' or 'dirin=' must be present;" 
164         << std::endl << "Not both" << std::endl;
165       am->ArgMgrUsage(usage); // Display 'usage'  
166       delete am;
167       return 1;
168    }
169
170    if (am->ArgMgrDefined("debug"))
171       gdcm::Debug::DebugOn();
172
173    if (am->ArgMgrDefined("warning"))
174       gdcm::Debug::WarningOn();
175        
176    int loadMode = gdcm::LD_ALL;
177    if ( am->ArgMgrDefined("noshadowseq") )
178       loadMode |= gdcm::LD_NOSHADOWSEQ;
179    else 
180    {
181    if ( am->ArgMgrDefined("noshadow") )
182          loadMode |= gdcm::LD_NOSHADOW;
183       if ( am->ArgMgrDefined("noseq") )
184          loadMode |= gdcm::LD_NOSEQ;
185    }
186
187    int level = am->ArgMgrGetInt("level", 1);
188
189    int forceLoadNb;
190    uint16_t *elemsToForceLoad 
191                            = am->ArgMgrGetXInt16Enum("forceload", &forceLoadNb);
192
193    bool showlut = ( 0 != am->ArgMgrDefined("SHOWLUT") );
194
195    bool ddict = am->ArgMgrDefined("dict") ? true : false;
196    char *dict = 0;
197
198    if (ddict)
199    {
200      dict = am->ArgMgrGetString("dict",(char *)0);
201    }
202
203    /* if unused Param we give up */
204    if ( am->ArgMgrPrintUnusedLabels() )
205    {
206       am->ArgMgrUsage(usage);
207       delete am;
208       return 1;
209    } 
210
211    delete am;  // we don't need Argument Manager any longer
212
213    // ----------- End Arguments Manager ---------
214
215  
216    if (ddict)
217    {
218       gdcm::Global::GetDicts()->GetDefaultPubDict()->AddDict(dict);   
219    }
220
221    if ( fileName != 0 ) // ====== Deal with a single file ======
222    { 
223       gdcm::File *f = gdcm::File::New();
224       f->SetLoadMode(loadMode);
225       f->SetFileName( fileName );
226
227       for (int ri=0; ri<forceLoadNb; ri++)
228       {
229          f->AddForceLoadElement((uint32_t)elemsToForceLoad[2*ri], 
230                                 (uint32_t)elemsToForceLoad[2*ri+1] ); 
231       }
232
233       bool res = f->Load();
234       // gdcm::File::IsReadable() is no usable here, because we deal with
235       // any kind of gdcm-Parsable *document* 
236       // not only gdcm::File (as opposed to gdcm::DicomDir)
237       if ( !res )
238       {
239          std::cout << "Cannot process file [" << fileName << "]" << std::endl;
240          std::cout << "Either it doesn't exist, or it's read protected " 
241                    << std::endl;
242          std::cout << "or it's not a Dicom File, or its 'header' is bugged" 
243                    << std::endl;
244          std::cout << "use 'PrintFile filein=... debug' to try to guess the pb"
245                    << std::endl;
246          f->Delete();
247          return 0;
248       }
249
250       gdcm::FileHelper *fh = gdcm::FileHelper::New(f);
251       fh->SetPrintLevel( level );
252
253       fh->Print();
254
255       std::cout << "\n\n" << std::endl; 
256
257       std::cout <<std::endl;
258       std::cout <<" dataSize    " << fh->GetImageDataSize()    << std::endl;
259       std::cout <<" dataSizeRaw " << fh->GetImageDataRawSize() << std::endl;
260
261       int nX,nY,nZ,sPP,planarConfig;
262       std::string pixelType;
263       nX=f->GetXSize();
264       nY=f->GetYSize();
265       nZ=f->GetZSize();
266       std::cout << " DIMX=" << nX << " DIMY=" << nY << " DIMZ=" << nZ 
267                 << std::endl;
268
269       pixelType    = f->GetPixelType();
270       sPP          = f->GetSamplesPerPixel();
271       std::cout << " pixelType= ["            << pixelType 
272                 << "] SamplesPerPixel= ["     << sPP
273                 << "] ";
274
275       if (sPP == 3)
276       {
277          planarConfig = f->GetPlanarConfiguration();
278          std::cout << " PlanarConfiguration= [" << planarConfig 
279                 << "] "<< std::endl;
280       } 
281       std::cout << " PhotometricInterpretation= [" 
282                 << f->GetEntryString(0x0028,0x0004)
283                 << "] "<< std::endl;
284
285       int numberOfScalarComponents=f->GetNumberOfScalarComponents();
286       std::cout << " NumberOfScalarComponents = " << numberOfScalarComponents 
287                 <<std::endl
288                 << " LUT = " << (f->HasLUT() ? "TRUE" : "FALSE")
289                 << std::endl;
290
291       if ( f->GetDataEntry(0x0002,0x0010) )
292          if ( f->GetDataEntry(0x0002,0x0010)->IsNotLoaded() ) 
293          {
294             std::cout << "Transfer Syntax not loaded. " << std::endl
295                      << "Better you increase MAX_SIZE_LOAD_ELEMENT_VALUE"
296                   << std::endl;
297             f->Delete();
298             return 0;
299          }
300   
301       std::string transferSyntaxName = f->GetTransferSyntaxName();
302       std::cout << " TransferSyntaxName= [" << transferSyntaxName << "]" 
303                 << std::endl;
304       std::cout << " SwapCode= " << f->GetSwapCode() << std::endl;
305       std::cout << " ------" << std::endl;
306
307       //std::cout << "\n\n" << std::endl; 
308       //std::cout << "X spacing " << f->GetXSpacing() << std::endl;
309       //std::cout << "Y spacing " << f->GetYSpacing() << std::endl;
310       //std::cout << "Z spacing " << f->GetZSpacing() << std::endl;
311
312 //------------------------------
313       // Lets's get and print some usefull fields about 'Orientation'
314       // ------------------------------------------------------------
315
316       std::string strPatientPosition = 
317                                       f->GetEntryString(0x0018,0x5100);
318       if ( strPatientPosition != gdcm::GDCM_UNFOUND 
319         && strPatientPosition != "" )  
320             std::cout << "PatientPosition (0x0010,0x5100)= [" 
321                       << strPatientPosition << "]" << std::endl;
322  
323       std::string strViewPosition = 
324                                       f->GetEntryString(0x0018,0x5101);
325       if ( strViewPosition != gdcm::GDCM_UNFOUND 
326         && strViewPosition != "" )  
327             std::cout << "strViewPosition (0x0010,0x5101)= [" 
328                       << strViewPosition << "]" << std::endl;
329       
330      std::string strPatientOrientation = 
331                                       f->GetEntryString(0x0020,0x0020);
332       if ( strPatientOrientation != gdcm::GDCM_UNFOUND
333         && strPatientOrientation != "")  
334          std::cout << "PatientOrientation (0x0020,0x0020)= [" 
335                    << strPatientOrientation << "]" << std::endl;
336
337       std::string strImageOrientationPatient = 
338                                       f->GetEntryString(0x0020,0x0037);  
339       if ( strImageOrientationPatient != gdcm::GDCM_UNFOUND
340         && strImageOrientationPatient != "" )  
341          std::cout << "ImageOrientationPatient (0x0020,0x0037)= [" 
342                    << strImageOrientationPatient << "]" << std::endl;
343
344       std::string strImageOrientationRET = 
345                                       f->GetEntryString(0x0020,0x0035);
346       if ( strImageOrientationRET != gdcm::GDCM_UNFOUND
347         && strImageOrientationRET != "" )  
348          std::cout << "ImageOrientationRET (0x0020,0x0035)= [" 
349                    << strImageOrientationRET << "]" << std::endl;
350
351       // Let's compute 'user friendly' results about 'Orientation'
352       // ---------------------------------------------------------
353  
354       gdcm::Orientation o;
355
356       if ( strImageOrientationPatient != gdcm::GDCM_UNFOUND ||
357            strImageOrientationRET     != gdcm::GDCM_UNFOUND )
358       {
359   
360          gdcm::OrientationType orient = o.GetOrientationType( f );
361  
362          std::cout << "TypeOrientation = " << orient << " (-> " 
363                    << o.GetOrientationTypeString(orient) << " )" << std::endl;
364       }
365
366       std::string ori = o.GetOrientation ( f );
367       if (ori != "\\" )
368          std::cout << "Orientation [" << ori << "]" << std::endl;
369  
370 //------------------------------
371
372
373       // Display the LUT as an int array (for debugging purpose)
374       if ( f->HasLUT() && showlut )
375       {
376          uint8_t* lutrgba = fh->GetLutRGBA();
377          if ( lutrgba == 0 )
378          {
379             std::cout << "Lut RGBA (Palette Color) not built " << std::endl;
380  
381            // Nothing is written yet to get LUT Data user friendly
382            // The following is to be moved into a PixelRedaConvert method
383   
384             gdcm::SeqEntry *modLutSeq = f->GetSeqEntry(0x0028,0x3000);
385             if ( modLutSeq !=0 )
386             {
387                gdcm::SQItem *sqi= modLutSeq->GetFirstSQItem();
388                if ( !sqi )
389                {
390                   std::string lutDescriptor = sqi->GetEntryString(0x0028,0x3002);
391                   int length;   // LUT length in Bytes
392                   int deb;      // Subscript of the first Lut Value
393                   int nbits;    // Lut item size (in Bits)
394                   int nbRead;   // nb of items in LUT descriptor (must be = 3)
395
396                   nbRead = sscanf( lutDescriptor.c_str(),
397                                     "%d\\%d\\%d",
398                                      &length, &deb, &nbits );
399                   if ( nbRead != 3 )
400                   {
401                       //gdcmWarningMacro( "Wrong LUT descriptor" );
402                       std::cout << "Wrong LUT descriptor" << std::endl;
403                   }                                                  
404                   gdcm::DataEntry *b = sqi->GetDataEntry(0x0028,0x3006);
405                   if ( b != 0 )
406                   {
407                      if ( b->GetLength() != 0 )
408                      {
409                         std::cout << "---------------------------------------"
410                                << " We should never reach this point      "
411                                << std::endl;
412                         //LoadEntryBinArea(b);    //LUT Data (CTX dependent)
413                      }   
414                  }
415               }      
416             }
417             else
418                std::cout << "No LUT Data (0x0028,0x3000) found " << std::endl;
419         }
420          else
421          {
422             if ( fh->GetLutItemSize() == 8 )
423             {
424                for (int i=0;i<fh->GetLutItemNumber();i++)
425                   std::cout << i << " : \t"
426                          << (int)(lutrgba[i*4])   << " "
427                          << (int)(lutrgba[i*4+1]) << " "
428                          << (int)(lutrgba[i*4+2]) << std::endl;
429             }
430             else // LutItemSize assumed to be = 16
431             {
432                uint16_t* lutrgba16 = (uint16_t*)lutrgba;
433                for (int i=0;i<fh->GetLutItemNumber();i++)
434                   std::cout << i << " : \t"
435                          << (int)(lutrgba16[i*4])   << " "
436                          << (int)(lutrgba16[i*4+1]) << " "
437                          << (int)(lutrgba16[i*4+2]) << std::endl;
438             }
439          }
440       }
441       else if (showlut)
442       {
443          std::cout << "Try LUT Data "<< std::endl;
444          ShowLutData(f);
445       }
446
447       // Parsability of the gdcm::Document already checked, after Load() !
448       
449       if ( f->IsReadable() )
450       {
451          std::cout <<std::endl<<fileName<<" is Readable"<<std::endl;
452       }
453       else if ( f->GetSeqEntry(0x0041,0x1010) )
454       {
455          std::cout <<std::endl<<fileName<<" looks like a 'PAPYRUS image' file"
456                    <<std::endl;
457       }
458       else if ( f->GetSeqEntry(0x0004,0x1220) )
459       {
460          std::cout <<std::endl<<fileName<<" looks like a 'DICOMDIR file'"
461                    <<std::endl;
462       }
463       else 
464       {
465          std::cout <<std::endl<<fileName<<" doesn't look like an image file "
466              <<std::endl; 
467       }
468  
469       std::cout<<std::flush;
470       f->Delete();
471       delete fh;
472    }
473    else  // ====== Deal with a Directory ======
474    {
475       std::cout << "dirName [" << dirName << "]" << std::endl;
476       gdcm::DirList dirList(dirName,1); // gets recursively the file list
477       gdcm::DirListType fileList = dirList.GetFilenames();
478       gdcm::File *f;
479       bool res;
480
481       for( gdcm::DirListType::iterator it  = fileList.begin();
482                                  it != fileList.end();
483                                  ++it )
484       {
485          std::cout << std::endl<<" Start processing :[" << it->c_str() << "]"
486                    << std::endl;
487          f = gdcm::File::New();
488          f->SetLoadMode(loadMode);
489          f->SetFileName( it->c_str() );
490
491          for (int ri=0; ri<forceLoadNb; ri++)
492          {
493             printf("%04x,%04x\n",elemsToForceLoad[2*ri], 
494                                  elemsToForceLoad[2*ri+1]);
495             f->AddForceLoadElement((uint32_t)elemsToForceLoad[2*ri], 
496                                    (uint32_t)elemsToForceLoad[2*ri+1]); 
497          }
498          res = f->Load();
499
500          if ( !res )
501          {
502             std::cout << "Cannot process file [" << it->c_str() << "]" 
503                       << std::endl;
504             std::cout << "Either it doesn't exist, or it's read protected " 
505                       << std::endl;
506             std::cout << "or it's not a Dicom File, or its 'header' is bugged" 
507                       << std::endl;
508             std::cout << "use 'PrintFile filein=... debug' "
509                       << "to try to guess the pb"
510                       << std::endl;
511             f->Delete();
512             continue;
513          }
514
515          gdcm::FileHelper *fh = gdcm::FileHelper::New(f);
516          fh->SetPrintLevel( level );
517          fh->Print();
518
519 //------------------------------
520
521          // Lets's get and print some usefull fields about 'Orientation'
522          // ------------------------------------------------------------
523
524          std::string strPatientPosition = 
525                                        f->GetEntryString(0x0018,0x5100);
526          if ( strPatientPosition != gdcm::GDCM_UNFOUND 
527          && strPatientPosition != "" )  
528                std::cout << "PatientPosition (0x0010,0x5100)= [" 
529                         << strPatientPosition << "]" << std::endl;
530     
531          std::string strViewPosition = 
532                                        f->GetEntryString(0x0018,0x5101);
533          if ( strViewPosition != gdcm::GDCM_UNFOUND 
534          && strViewPosition != "" )  
535                std::cout << "strViewPosition (0x0010,0x5101)= [" 
536                         << strViewPosition << "]" << std::endl;
537          
538          std::string strPatientOrientation = 
539                                        f->GetEntryString(0x0020,0x0020);
540          if ( strPatientOrientation != gdcm::GDCM_UNFOUND
541          && strPatientOrientation != "")  
542             std::cout << "PatientOrientation (0x0020,0x0020)= [" 
543                      << strPatientOrientation << "]" << std::endl;
544
545          std::string strImageOrientationPatient = 
546                                        f->GetEntryString(0x0020,0x0037);  
547          if ( strImageOrientationPatient != gdcm::GDCM_UNFOUND
548          && strImageOrientationPatient != "" )  
549             std::cout << "ImageOrientationPatient (0x0020,0x0037)= [" 
550                      << strImageOrientationPatient << "]" << std::endl;
551
552          std::string strImageOrientationRET = 
553                                        f->GetEntryString(0x0020,0x0035);
554          if ( strImageOrientationRET != gdcm::GDCM_UNFOUND
555          && strImageOrientationRET != "" )
556          {
557             std::cout << "ImageOrientationRET (0x0020,0x0035)= [" 
558                      << strImageOrientationRET << "]" << std::endl;
559          }
560
561          // Let's compute 'user friendly' results about 'Orientation'
562          // ---------------------------------------------------------
563     
564          gdcm::Orientation o;
565
566          if ( strImageOrientationPatient != gdcm::GDCM_UNFOUND ||
567             strImageOrientationRET     != gdcm::GDCM_UNFOUND )
568          {
569      
570             gdcm::OrientationType orient = o.GetOrientationType( f );
571     
572             std::cout << "TypeOrientation = " << orient << " (-> " 
573                      << o.GetOrientationTypeString(orient) << " )" << std::endl;
574          }
575
576          std::string ori = o.GetOrientation ( f );
577          if (ori != "\\" )
578             std::cout << "Orientation [" << ori << "]" << std::endl;
579
580 //------------------------------- 
581
582         
583          if (f->IsReadable())
584             std::cout <<std::endl<<it->c_str()<<" is Readable"<<std::endl;
585          else
586             std::cout <<std::endl<<it->c_str()<<" is NOT Readable"<<std::endl;
587          std::cout << "\n\n" << std::endl;
588          f->Delete();
589          fh->Delete();
590       }
591       std::cout<<std::flush;
592    }
593    return 0;
594 }