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