]> Creatis software - gdcm.git/blob - Example/PrintFile.cxx
* Minor coding-style clean up
[gdcm.git] / Example / PrintFile.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: PrintFile.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/10/18 08:35:43 $
7   Version:   $Revision: 1.63 $
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 'sigle 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] } ] [debug]  ",
133    "      level = 0,1,2 : depending on the amount of details user wants to see",
134    "      listOfElementsToForceLoad : group-elem,g2-e2,... (in hexa, no space)",
135    "                                of Elements to load whatever their length ",
136    "      privateDirectory : source file full path name of Shadow Group elems ",
137    "      noshadowseq: user doesn't want to load Private Sequences            ",
138    "      noshadow   : user doesn't want to load Private groups (odd number)  ",
139    "      noseq      : user doesn't want to load Sequences                    ",
140    "      debug      : user wants to run the program in 'debug mode'          ",
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 0;
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         ||
159         (fileName != 0 && dirName != 0) )
160    {
161        std::cout <<std::endl
162                  << "Either 'filein=' or 'dirin=' must be present;" 
163                  << std::endl << "Not both" << std::endl;
164        am->ArgMgrUsage(usage); // Display 'usage'  
165        delete am;
166        return 0;
167  }
168
169    if (am->ArgMgrDefined("debug"))
170       gdcm::Debug::DebugOn();
171  
172    int loadMode = gdcm::LD_ALL;
173    if ( am->ArgMgrDefined("noshadowseq") )
174       loadMode |= gdcm::LD_NOSHADOWSEQ;
175    else 
176    {
177    if ( am->ArgMgrDefined("noshadow") )
178          loadMode |= gdcm::LD_NOSHADOW;
179       if ( am->ArgMgrDefined("noseq") )
180          loadMode |= gdcm::LD_NOSEQ;
181    }
182
183    int level = am->ArgMgrGetInt("level", 1);
184
185    int forceLoadNb;
186    uint16_t *elemsToForceLoad 
187                            = am->ArgMgrGetXInt16Enum("forceload", &forceLoadNb);
188
189    bool showlut = ( 0 != am->ArgMgrDefined("SHOWLUT") );
190
191    bool ddict = am->ArgMgrDefined("dict") ? true : false;
192    char *dict = 0;
193
194    if (ddict)
195    {
196      dict = am->ArgMgrGetString("dict",(char *)0);
197    }
198
199    /* if unused Param we give up */
200    if ( am->ArgMgrPrintUnusedLabels() )
201    {
202       am->ArgMgrUsage(usage);
203       delete am;
204       return 0;
205    } 
206
207    delete am;  // we don't need Argument Manager any longer
208
209    // ----------- End Arguments Manager ---------
210
211  
212    if (ddict)
213    {
214       gdcm::Global::GetDicts()->GetDefaultPubDict()->AddDict(dict);   
215    }
216
217    if ( fileName != 0 ) // ====== Deal with a single file ======
218    { 
219       // gdcm::File::IsReadable() is no usable here, because we deal with
220       // any kind of gdcm-Parsable *document* 
221       // not only gdcm::File (as opposed to gdcm::DicomDir)
222
223       gdcm::File *f = new gdcm::File();
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       if ( !res )
235       {
236          std::cout << "Cannot process file [" << fileName << "]" << std::endl;
237          std::cout << "Either it doesn't exist, or it's read protected " 
238                    << std::endl;
239          std::cout << "or it's not a Dicom File, or its 'header' is bugged" 
240                    << std::endl;
241          std::cout << "use 'PrintFile filein=... debug' to try to guess the pb"
242                    << std::endl;
243          delete f;
244          return 0;
245       }
246
247       gdcm::FileHelper *fh = new gdcm::FileHelper(f);
248       fh->SetPrintLevel( level );
249
250       fh->Print();
251
252       std::cout << "\n\n" << std::endl; 
253
254       std::cout <<std::endl;
255       std::cout <<" dataSize    " << fh->GetImageDataSize()    << std::endl;
256       std::cout <<" dataSizeRaw " << fh->GetImageDataRawSize() << std::endl;
257
258       int nX,nY,nZ,sPP,planarConfig;
259       std::string pixelType;
260       nX=f->GetXSize();
261       nY=f->GetYSize();
262       nZ=f->GetZSize();
263       std::cout << " DIMX=" << nX << " DIMY=" << nY << " DIMZ=" << nZ 
264                 << std::endl;
265
266       pixelType    = f->GetPixelType();
267       sPP          = f->GetSamplesPerPixel();
268       planarConfig = f->GetPlanarConfiguration();
269
270       std::cout << " pixelType= ["            << pixelType 
271                 << "] SamplesPerPixel= ["     << sPP
272                 << "] PlanarConfiguration= [" << planarConfig 
273                 << "] "<< std::endl 
274                 << " PhotometricInterpretation= [" 
275                                 << f->GetEntryString(0x0028,0x0004)
276                 << "] "<< std::endl;
277
278       int numberOfScalarComponents=f->GetNumberOfScalarComponents();
279       std::cout << " NumberOfScalarComponents = " << numberOfScalarComponents 
280                 <<std::endl
281                 << " LUT = " << (f->HasLUT() ? "TRUE" : "FALSE")
282                 << std::endl;
283
284       if ( f->GetDataEntry(0x0002,0x0010) )
285          if ( f->GetDataEntry(0x0002,0x0010)->IsNotLoaded() ) 
286          {
287             std::cout << "Transfer Syntax not loaded. " << std::endl
288                      << "Better you increase MAX_SIZE_LOAD_ELEMENT_VALUE"
289                   << std::endl;
290             return 0;
291          }
292   
293       std::string transferSyntaxName = f->GetTransferSyntaxName();
294       std::cout << " TransferSyntaxName= [" << transferSyntaxName << "]" 
295                 << std::endl;
296       std::cout << " SwapCode= " << f->GetSwapCode() << std::endl;
297       std::cout << " ------" << std::endl;
298
299       //std::cout << "\n\n" << std::endl; 
300       //std::cout << "X spacing " << f->GetXSpacing() << std::endl;
301       //std::cout << "Y spacing " << f->GetYSpacing() << std::endl;
302       //std::cout << "Z spacing " << f->GetZSpacing() << std::endl;
303
304 //------------------------------
305       // Lets's get and print some usefull fields about 'Orientation'
306       // ------------------------------------------------------------
307
308       std::string strPatientPosition = 
309                                       f->GetEntryString(0x0018,0x5100);
310       if ( strPatientPosition != gdcm::GDCM_UNFOUND 
311         && strPatientPosition != "" )  
312             std::cout << "PatientPosition (0x0010,0x5100)= [" 
313                       << strPatientPosition << "]" << std::endl;
314  
315       std::string strViewPosition = 
316                                       f->GetEntryString(0x0018,0x5101);
317       if ( strViewPosition != gdcm::GDCM_UNFOUND 
318         && strViewPosition != "" )  
319             std::cout << "strViewPosition (0x0010,0x5101)= [" 
320                       << strViewPosition << "]" << std::endl;
321       
322      std::string strPatientOrientation = 
323                                       f->GetEntryString(0x0020,0x0020);
324       if ( strPatientOrientation != gdcm::GDCM_UNFOUND
325         && strPatientOrientation != "")  
326          std::cout << "PatientOrientation (0x0020,0x0020)= [" 
327                    << strPatientOrientation << "]" << std::endl;
328
329       std::string strImageOrientationPatient = 
330                                       f->GetEntryString(0x0020,0x0037);  
331       if ( strImageOrientationPatient != gdcm::GDCM_UNFOUND
332         && strImageOrientationPatient != "" )  
333          std::cout << "ImageOrientationPatient (0x0020,0x0037)= [" 
334                    << strImageOrientationPatient << "]" << std::endl;
335
336       std::string strImageOrientationRET = 
337                                       f->GetEntryString(0x0020,0x0035);
338       if ( strImageOrientationRET != gdcm::GDCM_UNFOUND
339         && strImageOrientationRET != "" )  
340          std::cout << "ImageOrientationRET (0x0020,0x0035)= [" 
341                    << strImageOrientationRET << "]" << std::endl;
342
343       // Let's compute 'user friendly' results about 'Orientation'
344       // ---------------------------------------------------------
345  
346       gdcm::Orientation o;
347
348       if ( strImageOrientationPatient != gdcm::GDCM_UNFOUND ||
349            strImageOrientationRET     != gdcm::GDCM_UNFOUND )
350       {
351   
352          gdcm::OrientationType orient = o.GetOrientationType( f );
353  
354          std::cout << "TypeOrientation = " << orient << " (-> " 
355                    << o.GetOrientationTypeString(orient) << " )" << std::endl;
356       }
357
358       std::string ori = o.GetOrientation ( f );
359       if (ori != "\\" )
360          std::cout << "Orientation [" << ori << "]" << std::endl;
361  
362 //------------------------------
363
364
365       // Display the LUT as an int array (for debugging purpose)
366       if ( f->HasLUT() && showlut )
367       {
368          uint8_t* lutrgba = fh->GetLutRGBA();
369          if ( lutrgba == 0 )
370          {
371             std::cout << "Lut RGBA (Palette Color) not built " << std::endl;
372  
373            // Nothing is written yet to get LUT Data user friendly
374            // The following is to be moved into a PixelRedaConvert method
375   
376             gdcm::SeqEntry *modLutSeq = f->GetSeqEntry(0x0028,0x3000);
377             if ( modLutSeq !=0 )
378             {
379                gdcm::SQItem *sqi= modLutSeq->GetFirstSQItem();
380                if ( !sqi )
381                {
382                std::string lutDescriptor = sqi->GetEntryString(0x0028,0x3002);
383                   int length;   // LUT length in Bytes
384                   int deb;      // Subscript of the first Lut Value
385                   int nbits;    // Lut item size (in Bits)
386                   int nbRead;   // nb of items in LUT descriptor (must be = 3)
387
388                   nbRead = sscanf( lutDescriptor.c_str(),
389                                     "%d\\%d\\%d",
390                                      &length, &deb, &nbits );
391                   if ( nbRead != 3 )
392                   {
393                       //gdcmWarningMacro( "Wrong LUT descriptor" );
394                       std::cout << "Wrong LUT descriptor" << std::endl;
395                   }                                                  
396                   gdcm::DataEntry *b = sqi->GetDataEntry(0x0028,0x3006);
397                   if ( b != 0 )
398                   {
399                      if ( b->GetLength() != 0 )
400                      {
401                         std::cout << "---------------------------------------"
402                                << " We should never reach this point      "
403                                << std::endl;
404                         //LoadEntryBinArea(b);    //LUT Data (CTX dependent)
405                      }   
406                  }
407               }      
408             }
409             else
410                std::cout << "No LUT Data (0x0028,0x3000) found " << std::endl;
411         }
412          else
413          {
414             if ( fh->GetLutItemSize() == 8 )
415             {
416                for (int i=0;i<fh->GetLutItemNumber();i++)
417                   std::cout << i << " : \t"
418                          << (int)(lutrgba[i*4])   << " "
419                          << (int)(lutrgba[i*4+1]) << " "
420                          << (int)(lutrgba[i*4+2]) << std::endl;
421             }
422             else // LutItemSize assumed to be = 16
423             {
424                uint16_t* lutrgba16 = (uint16_t*)lutrgba;
425                for (int i=0;i<fh->GetLutItemNumber();i++)
426                   std::cout << i << " : \t"
427                          << (int)(lutrgba16[i*4])   << " "
428                          << (int)(lutrgba16[i*4+1]) << " "
429                          << (int)(lutrgba16[i*4+2]) << std::endl;
430             }
431          }
432       }
433       else if (showlut)
434       {
435          std::cout << "Try LUT Data "<< std::endl;
436          ShowLutData(f);
437       }
438
439       //if( !f->gdcm::Document::IsReadable())
440       // Try downcast to please MSVC
441      if ( !((gdcm::Document *)f)->IsReadable() )
442      {
443          std::cout <<std::endl<<fileName<<" is NOT 'gdcm parsable'"<<std::endl;
444       }
445      
446       if (f->IsReadable())
447          std::cout <<std::endl<<fileName<<" is Readable"<<std::endl;
448       else if ( f->GetSeqEntry(0x0041,0x1010) )
449       {
450          std::cout <<std::endl<<fileName<<" looks like a 'PAPYRUS image' file"
451                    <<std::endl;
452       }
453       else if ( f->GetSeqEntry(0x0004,0x1220) )
454       {
455          std::cout <<std::endl<<fileName<<" looks like a 'DICOMDIR file'"
456                    <<std::endl;
457       }
458       else 
459       {
460          std::cout <<std::endl<<fileName<<" doesn't look like an image file "
461              <<std::endl; 
462       }
463  
464       std::cout<<std::flush;
465       delete f;
466       delete fh;
467       return 0;
468    }
469    else  // ====== Deal with a Directory ======
470    {
471       std::cout << "dirName [" << dirName << "]" << std::endl;
472       gdcm::DirList dirList(dirName,1); // gets recursively the file list
473       gdcm::DirListType fileList = dirList.GetFilenames();
474       gdcm::File *f;
475       bool res;
476       for( gdcm::DirListType::iterator it  = fileList.begin();
477                                  it != fileList.end();
478                                  ++it )
479       {
480          std::cout << std::endl<<" Start processing :[" << it->c_str() << "]"
481                    << std::endl;
482          f = new gdcm::File();
483          f->SetLoadMode(loadMode);
484          f->SetFileName( it->c_str() );
485
486          for (int ri=0; ri<forceLoadNb; ri++)
487          {
488             printf("%04x,%04x\n",elemsToForceLoad[2*ri], 
489                                  elemsToForceLoad[2*ri+1]);
490             f->AddForceLoadElement((uint32_t)elemsToForceLoad[2*ri], 
491                                    (uint32_t)elemsToForceLoad[2*ri+1]); 
492          }
493          res = f->Load();
494
495          if ( !res )
496          {
497             std::cout << "Cannot process file [" << it->c_str() << "]" 
498                       << std::endl;
499             std::cout << "Either it doesn't exist, or it's read protected " 
500                       << std::endl;
501             std::cout << "or it's not a Dicom File, or its 'header' is bugged" 
502                       << std::endl;
503             std::cout << "use 'PrintFile filein=... debug' "
504                       << "to try to guess the pb"
505                       << std::endl;
506             delete f;
507             continue;
508          }
509
510          gdcm::FileHelper *fh = new gdcm::FileHelper(f);
511          fh->SetPrintLevel( level );
512          fh->Print();
513
514 //------------------------------
515
516       // Lets's get and print some usefull fields about 'Orientation'
517       // ------------------------------------------------------------
518
519       std::string strPatientPosition = 
520                                       f->GetEntryString(0x0018,0x5100);
521       if ( strPatientPosition != gdcm::GDCM_UNFOUND 
522         && strPatientPosition != "" )  
523             std::cout << "PatientPosition (0x0010,0x5100)= [" 
524                       << strPatientPosition << "]" << std::endl;
525  
526       std::string strViewPosition = 
527                                       f->GetEntryString(0x0018,0x5101);
528       if ( strViewPosition != gdcm::GDCM_UNFOUND 
529         && strViewPosition != "" )  
530             std::cout << "strViewPosition (0x0010,0x5101)= [" 
531                       << strViewPosition << "]" << std::endl;
532       
533      std::string strPatientOrientation = 
534                                       f->GetEntryString(0x0020,0x0020);
535       if ( strPatientOrientation != gdcm::GDCM_UNFOUND
536         && strPatientOrientation != "")  
537          std::cout << "PatientOrientation (0x0020,0x0020)= [" 
538                    << strPatientOrientation << "]" << std::endl;
539
540       std::string strImageOrientationPatient = 
541                                       f->GetEntryString(0x0020,0x0037);  
542       if ( strImageOrientationPatient != gdcm::GDCM_UNFOUND
543         && strImageOrientationPatient != "" )  
544          std::cout << "ImageOrientationPatient (0x0020,0x0037)= [" 
545                    << strImageOrientationPatient << "]" << std::endl;
546
547       std::string strImageOrientationRET = 
548                                       f->GetEntryString(0x0020,0x0035);
549       if ( strImageOrientationRET != gdcm::GDCM_UNFOUND
550         && strImageOrientationRET != "" )  
551          std::cout << "ImageOrientationRET (0x0020,0x0035)= [" 
552                    << strImageOrientationRET << "]" << std::endl;
553
554       // Let's compute 'user friendly' results about 'Orientation'
555       // ---------------------------------------------------------
556  
557       gdcm::Orientation o;
558
559       if ( strImageOrientationPatient != gdcm::GDCM_UNFOUND ||
560            strImageOrientationRET     != gdcm::GDCM_UNFOUND )
561       {
562   
563          gdcm::OrientationType orient = o.GetOrientationType( f );
564  
565          std::cout << "TypeOrientation = " << orient << " (-> " 
566                    << o.GetOrientationTypeString(orient) << " )" << std::endl;
567       }
568
569       std::string ori = o.GetOrientation ( f );
570       if (ori != "\\" )
571          std::cout << "Orientation [" << ori << "]" << std::endl;
572
573 //------------------------------- 
574
575         
576          if (f->IsReadable())
577             std::cout <<std::endl<<it->c_str()<<" is Readable"<<std::endl;
578          else
579             std::cout <<std::endl<<it->c_str()<<" is NOT Readable"<<std::endl;
580          std::cout << "\n\n" << std::endl;
581          delete f;
582          delete fh;
583       }
584       std::cout<<std::flush;
585    }
586 }