]> Creatis software - gdcm.git/blob - src/gdcmHeader.cxx
2aedf3cda06c7f36dd799bdbc5080e20fb9b40b3
[gdcm.git] / src / gdcmHeader.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmHeader.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/05 16:30:50 $
7   Version:   $Revision: 1.219 $
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
19 #include "gdcmHeader.h"
20 #include "gdcmGlobal.h"
21 #include "gdcmUtil.h"
22 #include "gdcmDebug.h"
23 #include "gdcmTS.h"
24 #include "gdcmValEntry.h"
25 #include <stdio.h> //sscanf
26
27 #include <vector>
28
29 namespace gdcm 
30 {
31 //-----------------------------------------------------------------------------
32 // Constructor / Destructor
33 /**
34  * \brief  Constructor 
35  * @param  filename name of the file whose header we want to analyze
36  */
37 Header::Header( std::string const & filename ):
38             Document( filename )
39 {    
40    // for some ACR-NEMA images GrPixel, NumPixel is *not* 7fe0,0010
41    // We may encounter the 'RETired' (0x0028, 0x0200) tag
42    // (Image Location") . This Element contains the number of
43    // the group that contains the pixel data (hence the "Pixel Data"
44    // is found by indirection through the "Image Location").
45    // Inside the group pointed by "Image Location" the searched element
46    // is conventionally the element 0x0010 (when the norm is respected).
47    // When the "Image Location" is absent we default to group 0x7fe0.
48    // Note: this IS the right place for the code
49  
50    // Image Location
51    const std::string &imgLocation = GetEntryByNumber(0x0028, 0x0200);
52    if ( imgLocation == GDCM_UNFOUND )
53    {
54       // default value
55       GrPixel = 0x7fe0;
56    }
57    else
58    {
59       GrPixel = (uint16_t) atoi( imgLocation.c_str() );
60    }   
61
62    // sometimes Image Location value doesn't follow 
63    // the supposed processor endianity. 
64    // see gdcmData/cr172241.dcm      
65    if ( GrPixel == 0xe07f )
66    {
67       GrPixel = 0x7fe0;
68    }
69
70    if ( GrPixel != 0x7fe0 )
71    {
72       // This is a kludge for old dirty Philips imager.
73       NumPixel = 0x1010;
74    }
75    else
76    {
77       NumPixel = 0x0010;
78    }
79
80    // Now, we know GrPixel and NumPixel.
81    // Let's create a VirtualDictEntry to allow a further VR modification
82    // and force VR to match with BitsAllocated.
83
84    DocEntry* entry = GetDocEntryByNumber(GrPixel, NumPixel); 
85    if ( entry != 0 )
86    {
87
88       std::string PixelVR;
89       // 8 bits allocated is a 'O Bytes' , as well as 24 (old ACR-NEMA RGB)
90       // more than 8 (i.e 12, 16) is a 'O Words'
91       if ( GetBitsAllocated() == 8 || GetBitsAllocated() == 24 ) 
92          PixelVR = "OB";
93       else
94          PixelVR = "OW";
95
96       DictEntry* newEntry = NewVirtualDictEntry(
97                              GrPixel, NumPixel,
98                              PixelVR, "PXL", "Pixel Data");
99  
100       entry->SetDictEntry( newEntry );
101    } 
102
103 }
104
105 /**
106  * \brief Constructor used when we want to generate dicom files from scratch
107  */
108 Header::Header()
109            :Document()
110 {
111    InitializeDefaultHeader();
112 }
113
114 /**
115  * \brief   Canonical destructor.
116  */
117 Header::~Header ()
118 {
119 }
120
121 /**
122  * \brief Performs some consistency checking on various 'File related' 
123  *       (as opposed to 'DicomDir related') entries 
124  *       then writes in a file all the (Dicom Elements) included the Pixels 
125  * @param fp file pointer on an already open file
126  * @param filetype Type of the File to be written 
127  *          (ACR-NEMA, ExplicitVR, ImplicitVR)
128  */
129 bool Header::Write(std::string fileName, FileType filetype)
130 {
131    std::ofstream* fp = new std::ofstream(fileName.c_str(), 
132                                          std::ios::out | std::ios::binary);
133    if (*fp == NULL)
134    {
135       dbg.Verbose(2, "Failed to open (write) File: " , fileName.c_str());
136       return false;
137    }
138
139    // Bits Allocated
140    if ( GetEntryByNumber(0x0028,0x0100) ==  "12")
141    {
142       SetEntryByNumber("16", 0x0028,0x0100);
143    }
144
145   /// \todo correct 'Pixel group' Length if necessary
146
147    int i_lgPix = GetEntryLengthByNumber(GrPixel, NumPixel);
148    if (i_lgPix != -2)
149    {
150       // no (GrPixel, NumPixel) element
151       std::string s_lgPix = Util::Format("%d", i_lgPix+12);
152       s_lgPix = Util::DicomString( s_lgPix.c_str() );
153       ReplaceOrCreateByNumber(s_lgPix,GrPixel, 0x0000);
154    }
155
156    // FIXME : should be nice if we could move it to File
157    //         (or in future gdcmPixelData class)
158
159    // Drop Palette Color, if necessary
160    
161    if ( GetEntryByNumber(0x0028,0x0002).c_str()[0] == '3' )
162    {
163       // if SamplesPerPixel = 3, sure we don't need any LUT !   
164       // Drop 0028|1101, 0028|1102, 0028|1103
165       // Drop 0028|1201, 0028|1202, 0028|1203
166
167       DocEntry* e = GetDocEntryByNumber(0x0028,0x01101);
168       if (e)
169       {
170          RemoveEntryNoDestroy(e);
171       }
172       e = GetDocEntryByNumber(0x0028,0x1102);
173       if (e)
174       {
175          RemoveEntryNoDestroy(e);
176       }
177       e = GetDocEntryByNumber(0x0028,0x1103);
178       if (e)
179       {
180          RemoveEntryNoDestroy(e);
181       }
182       e = GetDocEntryByNumber(0x0028,0x01201);
183       if (e)
184       {
185          RemoveEntryNoDestroy(e);
186       }
187       e = GetDocEntryByNumber(0x0028,0x1202);
188       if (e)
189       {
190          RemoveEntryNoDestroy(e);
191       }
192       e = GetDocEntryByNumber(0x0028,0x1203);
193       if (e)
194       {
195           RemoveEntryNoDestroy(e);
196       }
197    }
198    Document::WriteContent(fp,filetype);
199
200    fp->close();
201    delete fp;
202
203    return true;
204 }
205
206 //-----------------------------------------------------------------------------
207 // Print
208
209
210 //-----------------------------------------------------------------------------
211 // Public
212
213 /**
214  * \brief  This predicate, based on hopefully reasonable heuristics,
215  *         decides whether or not the current Header was properly parsed
216  *         and contains the mandatory information for being considered as
217  *         a well formed and usable Dicom/Acr File.
218  * @return true when Header is the one of a reasonable Dicom/Acr file,
219  *         false otherwise. 
220  */
221 bool Header::IsReadable()
222 {
223    if( !Document::IsReadable() )
224    {
225       return false;
226    }
227
228    const std::string &res = GetEntryByNumber(0x0028, 0x0005);
229    if ( res != GDCM_UNFOUND && atoi(res.c_str()) > 4 )
230    {
231       return false; // Image Dimensions
232    }
233    if ( !GetDocEntryByNumber(0x0028, 0x0100) )
234    {
235       return false; // "Bits Allocated"
236    }
237    if ( !GetDocEntryByNumber(0x0028, 0x0101) )
238    {
239       return false; // "Bits Stored"
240    }
241    if ( !GetDocEntryByNumber(0x0028, 0x0102) )
242    {
243       return false; // "High Bit"
244    }
245    if ( !GetDocEntryByNumber(0x0028, 0x0103) )
246    {
247       return false; // "Pixel Representation" i.e. 'Sign'
248    }
249
250    return true;
251 }
252
253 /**
254  * \brief   Retrieve the number of columns of image.
255  * @return  The encountered size when found, 0 by default.
256  *          0 means the file is NOT USABLE. The caller will have to check
257  */
258 int Header::GetXSize()
259 {
260    const std::string &strSize = GetEntryByNumber(0x0028,0x0011);
261    if ( strSize == GDCM_UNFOUND )
262    {
263       return 0;
264    }
265
266    return atoi( strSize.c_str() );
267 }
268
269 /**
270  * \brief   Retrieve the number of lines of image.
271  * \warning The defaulted value is 1 as opposed to Header::GetXSize()
272  * @return  The encountered size when found, 1 by default 
273  *          (The ACR-NEMA file contains a Signal, not an Image).
274  */
275 int Header::GetYSize()
276 {
277    const std::string &strSize = GetEntryByNumber(0x0028,0x0010);
278    if ( strSize != GDCM_UNFOUND )
279    {
280       return atoi( strSize.c_str() );
281    }
282    if ( IsDicomV3() )
283    {
284       return 0;
285    }
286
287    // The Rows (0028,0010) entry was optional for ACR/NEMA. It might
288    // hence be a signal (1D image). So we default to 1:
289    return 1;
290 }
291
292 /**
293  * \brief   Retrieve the number of planes of volume or the number
294  *          of frames of a multiframe.
295  * \warning When present we consider the "Number of Frames" as the third
296  *          dimension. When absent we consider the third dimension as
297  *          being the ACR-NEMA "Planes" tag content.
298  * @return  The encountered size when found, 1 by default (single image).
299  */
300 int Header::GetZSize()
301 {
302    // Both  DicomV3 and ACR/Nema consider the "Number of Frames"
303    // as the third dimension.
304    const std::string &strSize = GetEntryByNumber(0x0028,0x0008);
305    if ( strSize != GDCM_UNFOUND )
306    {
307       return atoi( strSize.c_str() );
308    }
309
310    // We then consider the "Planes" entry as the third dimension 
311    const std::string &strSize2 = GetEntryByNumber(0x0028,0x0012);
312    if ( strSize2 != GDCM_UNFOUND )
313    {
314       return atoi( strSize2.c_str() );
315    }
316
317    return 1;
318 }
319
320 /**
321   * \brief gets the info from 0028,0030 : Pixel Spacing
322   *             else 1.0
323   * @return X dimension of a pixel
324   */
325 float Header::GetXSpacing()
326 {
327    float xspacing, yspacing;
328    const std::string &strSpacing = GetEntryByNumber(0x0028,0x0030);
329
330    if ( strSpacing == GDCM_UNFOUND )
331    {
332       dbg.Verbose(0, "Header::GetXSpacing: unfound Pixel Spacing (0028,0030)");
333       return 1.;
334    }
335
336    int nbValues;
337    if( ( nbValues = sscanf( strSpacing.c_str(), 
338          "%f\\%f", &yspacing, &xspacing)) != 2 )
339    {
340       // if single value is found, xspacing is defaulted to yspacing
341       if ( nbValues == 1 )
342       {
343          xspacing = yspacing;
344       }
345
346       if ( xspacing == 0.0 )
347          xspacing = 1.0;
348
349       return xspacing;
350
351    }
352
353    // to avoid troubles with David Clunie's-like images
354    if ( xspacing == 0. && yspacing == 0.)
355       return 1.;
356
357    if ( xspacing == 0.)
358    {
359       dbg.Verbose(0, "Header::GetXSpacing: gdcmData/CT-MONO2-8-abdo.dcm problem");
360       // seems to be a bug in the header ...
361       nbValues = sscanf( strSpacing.c_str(), "%f\\0\\%f", &yspacing, &xspacing);
362       assert( nbValues == 2 );
363    }
364
365    return xspacing;
366 }
367
368 /**
369   * \brief gets the info from 0028,0030 : Pixel Spacing
370   *             else 1.0
371   * @return Y dimension of a pixel
372   */
373 float Header::GetYSpacing()
374 {
375    float yspacing = 1.;
376    std::string strSpacing = GetEntryByNumber(0x0028,0x0030);
377   
378    if ( strSpacing == GDCM_UNFOUND )
379    {
380       dbg.Verbose(0, "Header::GetYSpacing: unfound Pixel Spacing (0028,0030)");
381       return 1.;
382     }
383
384    // if sscanf cannot read any float value, it won't affect yspacing
385    sscanf( strSpacing.c_str(), "%f", &yspacing);
386
387    if ( yspacing == 0.0 )
388       yspacing = 1.0;
389
390    return yspacing;
391
392
393 /**
394  * \brief gets the info from 0018,0088 : Space Between Slices
395  *                 else from 0018,0050 : Slice Thickness
396  *                 else 1.0
397  * @return Z dimension of a voxel-to be
398  */
399 float Header::GetZSpacing()
400 {
401    // Spacing Between Slices : distance entre le milieu de chaque coupe
402    // Les coupes peuvent etre :
403    //   jointives     (Spacing between Slices = Slice Thickness)
404    //   chevauchantes (Spacing between Slices < Slice Thickness)
405    //   disjointes    (Spacing between Slices > Slice Thickness)
406    // Slice Thickness : epaisseur de tissus sur laquelle est acquis le signal
407    //   ca interesse le physicien de l'IRM, pas le visualisateur de volumes ...
408    //   Si le Spacing Between Slices est absent, 
409    //   on suppose que les coupes sont jointives
410    
411    const std::string &strSpacingBSlices = GetEntryByNumber(0x0018,0x0088);
412
413    if ( strSpacingBSlices == GDCM_UNFOUND )
414    {
415       dbg.Verbose(0, "Header::GetZSpacing: unfound StrSpacingBSlices");
416       const std::string &strSliceThickness = GetEntryByNumber(0x0018,0x0050);       
417       if ( strSliceThickness == GDCM_UNFOUND )
418       {
419          return 1.;
420       }
421       else
422       {
423          // if no 'Spacing Between Slices' is found, 
424          // we assume slices join together
425          // (no overlapping, no interslice gap)
426          // if they don't, we're fucked up
427          return (float)atof( strSliceThickness.c_str() );
428       }
429    }
430    //else
431    return (float)atof( strSpacingBSlices.c_str() );
432 }
433
434 /**
435  *\brief gets the info from 0028,1052 : Rescale Intercept
436  * @return Rescale Intercept
437  */
438 float Header::GetRescaleIntercept()
439 {
440    float resInter = 0.;
441    /// 0028 1052 DS IMG Rescale Intercept
442    const std::string &strRescInter = GetEntryByNumber(0x0028,0x1052);
443    if ( strRescInter != GDCM_UNFOUND )
444    {
445       if( sscanf( strRescInter.c_str(), "%f", &resInter) != 1 )
446       {
447          // bug in the element 0x0028,0x1052
448          dbg.Verbose(0, "Header::GetRescaleIntercept: Rescale Slope "
449                         "is empty");
450       }
451    }
452
453    return resInter;
454 }
455
456 /**
457  *\brief   gets the info from 0028,1053 : Rescale Slope
458  * @return Rescale Slope
459  */
460 float Header::GetRescaleSlope()
461 {
462    float resSlope = 1.;
463    //0028 1053 DS IMG Rescale Slope
464    std::string strRescSlope = GetEntryByNumber(0x0028,0x1053);
465    if ( strRescSlope != GDCM_UNFOUND )
466    {
467       if( sscanf( strRescSlope.c_str(), "%f", &resSlope) != 1)
468       {
469          // bug in the element 0x0028,0x1053
470          dbg.Verbose(0, "Header::GetRescaleSlope: Rescale Slope is empty");
471       }
472    }
473
474    return resSlope;
475 }
476
477 /**
478  * \brief This function is intended to user who doesn't want 
479  *   to have to manage a LUT and expects to get an RBG Pixel image
480  *   (or a monochrome one ...) 
481  * \warning to be used with GetImagePixels()
482  * @return 1 if Gray level, 3 if Color (RGB, YBR or PALETTE COLOR)
483  */
484 int Header::GetNumberOfScalarComponents()
485 {
486    if ( GetSamplesPerPixel() == 3 )
487    {
488       return 3;
489    }
490       
491    // 0028 0100 US IMG Bits Allocated
492    // (in order no to be messed up by old RGB images)
493    if ( GetEntryByNumber(0x0028,0x0100) == "24" )
494    {
495       return 3;
496    }
497        
498    std::string strPhotometricInterpretation = GetEntryByNumber(0x0028,0x0004);
499
500    if ( ( strPhotometricInterpretation == "PALETTE COLOR ") )
501    {
502       if ( HasLUT() )// PALETTE COLOR is NOT enough
503       {
504          return 3;
505       }
506       else
507       {
508          return 1;
509       }
510    }
511
512    // beware of trailing space at end of string      
513    // DICOM tags are never of odd length
514    if ( strPhotometricInterpretation == GDCM_UNFOUND   || 
515         Util::DicomStringEqual(strPhotometricInterpretation, "MONOCHROME1") ||
516         Util::DicomStringEqual(strPhotometricInterpretation, "MONOCHROME2") )
517    {
518       return 1;
519    }
520    else
521    {
522       // we assume that *all* kinds of YBR are dealt with
523       return 3;
524    }
525 }
526
527 /**
528  * \brief This function is intended to user that DOESN'T want 
529  *  to get RGB pixels image when it's stored as a PALETTE COLOR image
530  *   - the (vtk) user is supposed to know how deal with LUTs - 
531  * \warning to be used with GetImagePixelsRaw()
532  * @return 1 if Gray level, 3 if Color (RGB or YBR - NOT 'PALETTE COLOR' -)
533  */
534 int Header::GetNumberOfScalarComponentsRaw()
535 {
536    // 0028 0100 US IMG Bits Allocated
537    // (in order no to be messed up by old RGB images)
538    if ( Header::GetEntryByNumber(0x0028,0x0100) == "24" )
539    {
540       return 3;
541    }
542
543    // we assume that *all* kinds of YBR are dealt with
544    return GetSamplesPerPixel();
545 }
546
547 //
548 // --------------  Remember ! ----------------------------------
549 //
550 // Image Position Patient                              (0020,0032):
551 // If not found (ACR_NEMA) we try Image Position       (0020,0030)
552 // If not found (ACR-NEMA), we consider Slice Location (0020,1041)
553 //                                   or Location       (0020,0050) 
554 // as the Z coordinate, 
555 // 0. for all the coordinates if nothing is found
556
557 // \todo find a way to inform the caller nothing was found
558 // \todo How to tell the caller a wrong number of values was found?
559 //
560 // ---------------------------------------------------------------
561 //
562
563 /**
564  * \brief gets the info from 0020,0032 : Image Position Patient
565  *                 else from 0020,0030 : Image Position (RET)
566  *                 else 0.
567  * @return up-left image corner X position
568  */
569 float Header::GetXOrigin()
570 {
571    float xImPos, yImPos, zImPos;  
572    std::string strImPos = GetEntryByNumber(0x0020,0x0032);
573
574    if ( strImPos == GDCM_UNFOUND )
575    {
576       dbg.Verbose(0, "Header::GetXImagePosition: unfound Image "
577                      "Position Patient (0020,0032)");
578       strImPos = GetEntryByNumber(0x0020,0x0030); // For ACR-NEMA images
579       if ( strImPos == GDCM_UNFOUND )
580       {
581          dbg.Verbose(0, "Header::GetXImagePosition: unfound Image "
582                         "Position (RET) (0020,0030)");
583          /// \todo How to tell the caller nothing was found ?
584          return 0.;
585       }
586    }
587
588    if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3 )
589    {
590       return 0.;
591    }
592
593    return xImPos;
594 }
595
596 /**
597  * \brief gets the info from 0020,0032 : Image Position Patient
598  *                 else from 0020,0030 : Image Position (RET)
599  *                 else 0.
600  * @return up-left image corner Y position
601  */
602 float Header::GetYOrigin()
603 {
604    float xImPos, yImPos, zImPos;
605    std::string strImPos = GetEntryByNumber(0x0020,0x0032);
606
607    if ( strImPos == GDCM_UNFOUND)
608    {
609       dbg.Verbose(0, "Header::GetYImagePosition: unfound Image "
610                      "Position Patient (0020,0032)");
611       strImPos = GetEntryByNumber(0x0020,0x0030); // For ACR-NEMA images
612       if ( strImPos == GDCM_UNFOUND )
613       {
614          dbg.Verbose(0, "Header::GetYImagePosition: unfound Image "
615                         "Position (RET) (0020,0030)");
616          /// \todo How to tell the caller nothing was found ?
617          return 0.;
618       }  
619    }
620
621    if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3 )
622    {
623       return 0.;
624    }
625
626    return yImPos;
627 }
628
629 /**
630  * \brief gets the info from 0020,0032 : Image Position Patient
631  *                 else from 0020,0030 : Image Position (RET)
632  *                 else from 0020,1041 : Slice Location
633  *                 else from 0020,0050 : Location
634  *                 else 0.
635  * @return up-left image corner Z position
636  */
637 float Header::GetZOrigin()
638 {
639    float xImPos, yImPos, zImPos; 
640    std::string strImPos = GetEntryByNumber(0x0020,0x0032);
641
642    if ( strImPos != GDCM_UNFOUND )
643    {
644       if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3)
645       {
646          dbg.Verbose(0, "Header::GetZImagePosition: wrong Image "
647                         "Position Patient (0020,0032)");
648          return 0.;  // bug in the element 0x0020,0x0032
649       }
650       else
651       {
652          return zImPos;
653       }
654    }
655
656    strImPos = GetEntryByNumber(0x0020,0x0030); // For ACR-NEMA images
657    if ( strImPos != GDCM_UNFOUND )
658    {
659       if( sscanf( strImPos.c_str(), 
660           "%f\\%f\\%f", &xImPos, &yImPos, &zImPos ) != 3 )
661       {
662          dbg.Verbose(0, "Header::GetZImagePosition: wrong Image Position (RET) (0020,0030)");
663          return 0.;  // bug in the element 0x0020,0x0032
664       }
665       else
666       {
667          return zImPos;
668       }
669    }
670
671    std::string strSliceLocation = GetEntryByNumber(0x0020,0x1041); // for *very* old ACR-NEMA images
672    if ( strSliceLocation != GDCM_UNFOUND )
673    {
674       if( sscanf( strSliceLocation.c_str(), "%f", &zImPos) != 1)
675       {
676          dbg.Verbose(0, "Header::GetZImagePosition: wrong Slice Location (0020,1041)");
677          return 0.;  // bug in the element 0x0020,0x1041
678       }
679       else
680       {
681          return zImPos;
682       }
683    }
684    dbg.Verbose(0, "Header::GetZImagePosition: unfound Slice Location (0020,1041)");
685
686    std::string strLocation = GetEntryByNumber(0x0020,0x0050);
687    if ( strLocation != GDCM_UNFOUND )
688    {
689       if( sscanf( strLocation.c_str(), "%f", &zImPos) != 1)
690       {
691          dbg.Verbose(0, "Header::GetZImagePosition: wrong Location (0020,0050)");
692          return 0.;  // bug in the element 0x0020,0x0050
693       }
694       else
695       {
696          return zImPos;
697       }
698    }
699    dbg.Verbose(0, "Header::GetYImagePosition: unfound Location (0020,0050)");  
700
701    return 0.; // Hopeless
702 }
703
704 /**
705  * \brief gets the info from 0020,0013 : Image Number else 0.
706  * @return image number
707  */
708 int Header::GetImageNumber()
709 {
710    // The function i atoi() takes the address of an area of memory as
711    // parameter and converts the string stored at that location to an integer
712    // using the external decimal to internal binary conversion rules. This may
713    // be preferable to sscanf() since atoi() is a much smaller, simpler and
714    // faster function. sscanf() can do all possible conversions whereas
715    // atoi() can only do single decimal integer conversions.
716    //0020 0013 IS REL Image Number
717    std::string strImNumber = GetEntryByNumber(0x0020,0x0013);
718    if ( strImNumber != GDCM_UNFOUND )
719    {
720       return atoi( strImNumber.c_str() );
721    }
722    return 0;   //Hopeless
723 }
724
725 /**
726  * \brief gets the info from 0008,0060 : Modality
727  * @return Modality Type
728  */
729 ModalityType Header::GetModality()
730 {
731    // 0008 0060 CS ID Modality
732    std::string strModality = GetEntryByNumber(0x0008,0x0060);
733    if ( strModality != GDCM_UNFOUND )
734    {
735            if ( strModality.find("AU") < strModality.length()) return AU;
736       else if ( strModality.find("AS") < strModality.length()) return AS;
737       else if ( strModality.find("BI") < strModality.length()) return BI;
738       else if ( strModality.find("CF") < strModality.length()) return CF;
739       else if ( strModality.find("CP") < strModality.length()) return CP;
740       else if ( strModality.find("CR") < strModality.length()) return CR;
741       else if ( strModality.find("CT") < strModality.length()) return CT;
742       else if ( strModality.find("CS") < strModality.length()) return CS;
743       else if ( strModality.find("DD") < strModality.length()) return DD;
744       else if ( strModality.find("DF") < strModality.length()) return DF;
745       else if ( strModality.find("DG") < strModality.length()) return DG;
746       else if ( strModality.find("DM") < strModality.length()) return DM;
747       else if ( strModality.find("DS") < strModality.length()) return DS;
748       else if ( strModality.find("DX") < strModality.length()) return DX;
749       else if ( strModality.find("ECG") < strModality.length()) return ECG;
750       else if ( strModality.find("EPS") < strModality.length()) return EPS;
751       else if ( strModality.find("FA") < strModality.length()) return FA;
752       else if ( strModality.find("FS") < strModality.length()) return FS;
753       else if ( strModality.find("HC") < strModality.length()) return HC;
754       else if ( strModality.find("HD") < strModality.length()) return HD;
755       else if ( strModality.find("LP") < strModality.length()) return LP;
756       else if ( strModality.find("LS") < strModality.length()) return LS;
757       else if ( strModality.find("MA") < strModality.length()) return MA;
758       else if ( strModality.find("MR") < strModality.length()) return MR;
759       else if ( strModality.find("NM") < strModality.length()) return NM;
760       else if ( strModality.find("OT") < strModality.length()) return OT;
761       else if ( strModality.find("PT") < strModality.length()) return PT;
762       else if ( strModality.find("RF") < strModality.length()) return RF;
763       else if ( strModality.find("RG") < strModality.length()) return RG;
764       else if ( strModality.find("RTDOSE")   < strModality.length()) return RTDOSE;
765       else if ( strModality.find("RTIMAGE")  < strModality.length()) return RTIMAGE;
766       else if ( strModality.find("RTPLAN")   < strModality.length()) return RTPLAN;
767       else if ( strModality.find("RTSTRUCT") < strModality.length()) return RTSTRUCT;
768       else if ( strModality.find("SM") < strModality.length()) return SM;
769       else if ( strModality.find("ST") < strModality.length()) return ST;
770       else if ( strModality.find("TG") < strModality.length()) return TG;
771       else if ( strModality.find("US") < strModality.length()) return US;
772       else if ( strModality.find("VF") < strModality.length()) return VF;
773       else if ( strModality.find("XA") < strModality.length()) return XA;
774       else if ( strModality.find("XC") < strModality.length()) return XC;
775
776       else
777       {
778          /// \todo throw error return value ???
779          /// specified <> unknow in our database
780          return Unknow;
781       }
782    }
783
784    return Unknow;
785 }
786
787 /**
788  * \brief   Retrieve the number of Bits Stored (actually used)
789  *          (as opposite to number of Bits Allocated)
790  * @return  The encountered number of Bits Stored, 0 by default.
791  *          0 means the file is NOT USABLE. The caller has to check it !
792  */
793 int Header::GetBitsStored()
794 {
795    std::string strSize = GetEntryByNumber( 0x0028, 0x0101 );
796    if ( strSize == GDCM_UNFOUND )
797    {
798       dbg.Verbose(0, "Header::GetBitsStored: this is supposed to "
799                      "be mandatory");
800       return 0;  // It's supposed to be mandatory
801                  // the caller will have to check
802    }
803    return atoi( strSize.c_str() );
804 }
805
806 /**
807  * \brief   Retrieve the high bit position.
808  * \warning The method defaults to 0 when information is absent.
809  *          The responsability of checking this value is left to the caller.
810  * @return  The high bit positin when present. 0 when absent.
811  */
812 int Header::GetHighBitPosition()
813 {
814    std::string strSize = GetEntryByNumber( 0x0028, 0x0102 );
815    if ( strSize == GDCM_UNFOUND )
816    {
817       dbg.Verbose(0, "Header::GetHighBitPosition: this is supposed "
818                      "to be mandatory");
819       return 0;
820    }
821    return atoi( strSize.c_str() );
822 }
823
824 /**
825  * \brief   Check wether the pixels are signed or UNsigned data.
826  * \warning The method defaults to false (UNsigned) when information is absent.
827  *          The responsability of checking this value is left to the caller.
828  * @return  True when signed, false when UNsigned
829  */
830 bool Header::IsSignedPixelData()
831 {
832    std::string strSize = GetEntryByNumber( 0x0028, 0x0103 );
833    if ( strSize == GDCM_UNFOUND )
834    {
835       dbg.Verbose(0, "Header::IsSignedPixelData: this is supposed "
836                      "to be mandatory");
837       return false;
838    }
839    int sign = atoi( strSize.c_str() );
840    if ( sign == 0 ) 
841    {
842       return false;
843    }
844    return true;
845 }
846
847 /**
848  * \brief   Retrieve the number of Bits Allocated
849  *          (8, 12 -compacted ACR-NEMA files, 16, ...)
850  * @return  The encountered number of Bits Allocated, 0 by default.
851  *          0 means the file is NOT USABLE. The caller has to check it !
852  */
853 int Header::GetBitsAllocated()
854 {
855    std::string strSize = GetEntryByNumber(0x0028,0x0100);
856    if ( strSize == GDCM_UNFOUND )
857    {
858       dbg.Verbose(0, "Header::GetBitsStored: this is supposed to "
859                      "be mandatory");
860       return 0; // It's supposed to be mandatory
861                 // the caller will have to check
862    }
863    return atoi( strSize.c_str() );
864 }
865
866 /**
867  * \brief   Retrieve the number of Samples Per Pixel
868  *          (1 : gray level, 3 : RGB -1 or 3 Planes-)
869  * @return  The encountered number of Samples Per Pixel, 1 by default.
870  *          (Gray level Pixels)
871  */
872 int Header::GetSamplesPerPixel()
873 {
874    const std::string& strSize = GetEntryByNumber(0x0028,0x0002);
875    if ( strSize == GDCM_UNFOUND )
876    {
877       dbg.Verbose(0, "Header::GetBitsStored: this is supposed to "
878                      "be mandatory");
879       return 1; // Well, it's supposed to be mandatory ...
880                 // but sometimes it's missing : *we* assume Gray pixels
881    }
882    return atoi( strSize.c_str() );
883 }
884
885 /**
886  * \brief   Check wether this a monochrome picture or not by accessing
887  *          the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
888  * @return  true when "MONOCHROME1" or "MONOCHROME2". False otherwise.
889  */
890 bool Header::IsMonochrome()
891 {
892    const std::string& PhotometricInterp = GetEntryByNumber( 0x0028, 0x0004 );
893    if (  Util::DicomStringEqual(PhotometricInterp, "MONOCHROME1")
894       || Util::DicomStringEqual(PhotometricInterp, "MONOCHROME2") )
895    {
896       return true;
897    }
898    if ( PhotometricInterp == GDCM_UNFOUND )
899    {
900       dbg.Verbose(0, "Header::IsMonochrome: absent Photometric "
901                      "Interpretation");
902    }
903    return false;
904 }
905
906 /**
907  * \brief   Check wether this a "PALETTE COLOR" picture or not by accessing
908  *          the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
909  * @return  true when "PALETTE COLOR". False otherwise.
910  */
911 bool Header::IsPaletteColor()
912 {
913    std::string PhotometricInterp = GetEntryByNumber( 0x0028, 0x0004 );
914    if (   PhotometricInterp == "PALETTE COLOR " )
915    {
916       return true;
917    }
918    if ( PhotometricInterp == GDCM_UNFOUND )
919    {
920       dbg.Verbose(0, "Header::IsPaletteColor: absent Photometric "
921                      "Interpretation");
922    }
923    return false;
924 }
925
926 /**
927  * \brief   Check wether this a "YBR_FULL" color picture or not by accessing
928  *          the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
929  * @return  true when "YBR_FULL". False otherwise.
930  */
931 bool Header::IsYBRFull()
932 {
933    std::string PhotometricInterp = GetEntryByNumber( 0x0028, 0x0004 );
934    if (   PhotometricInterp == "YBR_FULL" )
935    {
936       return true;
937    }
938    if ( PhotometricInterp == GDCM_UNFOUND )
939    {
940       dbg.Verbose(0, "Header::IsYBRFull: absent Photometric "
941                      "Interpretation");
942    }
943    return false;
944 }
945
946 /**
947  * \brief   Retrieve the Planar Configuration for RGB images
948  *          (0 : RGB Pixels , 1 : R Plane + G Plane + B Plane)
949  * @return  The encountered Planar Configuration, 0 by default.
950  */
951 int Header::GetPlanarConfiguration()
952 {
953    std::string strSize = GetEntryByNumber(0x0028,0x0006);
954    if ( strSize == GDCM_UNFOUND )
955    {
956       return 0;
957    }
958    return atoi( strSize.c_str() );
959 }
960
961 /**
962  * \brief   Return the size (in bytes) of a single pixel of data.
963  * @return  The size in bytes of a single pixel of data; 0 by default
964  *          0 means the file is NOT USABLE; the caller will have to check
965  */
966 int Header::GetPixelSize()
967 {
968    // 0028 0100 US IMG Bits Allocated
969    // (in order no to be messed up by old RGB images)
970    //   if (Header::GetEntryByNumber(0x0028,0x0100) == "24")
971    //      return 3;
972
973    std::string pixelType = GetPixelType();
974    if ( pixelType ==  "8U" || pixelType == "8S" )
975    {
976       return 1;
977    }
978    if ( pixelType == "16U" || pixelType == "16S")
979    {
980       return 2;
981    }
982    if ( pixelType == "32U" || pixelType == "32S")
983    {
984       return 4;
985    }
986    if ( pixelType == "FD" )
987    {
988       return 8;
989    }
990    dbg.Verbose(0, "Header::GetPixelSize: Unknown pixel type");
991    return 0;
992 }
993
994 /**
995  * \brief   Build the Pixel Type of the image.
996  *          Possible values are:
997  *          - 8U  unsigned  8 bit,
998  *          - 8S    signed  8 bit,
999  *          - 16U unsigned 16 bit,
1000  *          - 16S   signed 16 bit,
1001  *          - 32U unsigned 32 bit,
1002  *          - 32S   signed 32 bit,
1003  *          - FD floating double 64 bits (Not kosher DICOM, but so usefull!)
1004  * \warning 12 bit images appear as 16 bit.
1005  *          24 bit images appear as 8 bit
1006  * @return  0S if nothing found. NOT USABLE file. The caller has to check
1007  */
1008 std::string Header::GetPixelType()
1009 {
1010    std::string bitsAlloc = GetEntryByNumber(0x0028, 0x0100); // Bits Allocated
1011    if ( bitsAlloc == GDCM_UNFOUND )
1012    {
1013       dbg.Verbose(0, "Header::GetPixelType: unfound Bits Allocated");
1014       bitsAlloc = "16";
1015    }
1016
1017    if ( bitsAlloc == "64" )
1018    {
1019       return "FD";
1020    }
1021    else if ( bitsAlloc == "12" )
1022    {
1023       // It will be unpacked
1024       bitsAlloc = "16";
1025    }
1026    else if ( bitsAlloc == "24" )
1027    {
1028       // (in order no to be messed up
1029       bitsAlloc = "8";  // by old RGB images)
1030    }
1031
1032    std::string sign = GetEntryByNumber(0x0028, 0x0103);//"Pixel Representation"
1033
1034    if (sign == GDCM_UNFOUND )
1035    {
1036       dbg.Verbose(0, "Header::GetPixelType: unfound Pixel Representation");
1037       bitsAlloc = "0";
1038    }
1039    else if ( sign == "0" )
1040    {
1041       sign = "U";
1042    }
1043    else
1044    {
1045       sign = "S";
1046    }
1047    return bitsAlloc + sign;
1048 }
1049
1050
1051 /**
1052  * \brief   Recover the offset (from the beginning of the file) 
1053  *          of *image* pixels (not *icone image* pixels, if any !)
1054  * @return Pixel Offset
1055  */
1056 size_t Header::GetPixelOffset()
1057 {
1058    DocEntry* pxlElement = GetDocEntryByNumber(GrPixel,NumPixel);
1059    if ( pxlElement )
1060    {
1061       return pxlElement->GetOffset();
1062    }
1063    else
1064    {
1065 #ifdef GDCM_DEBUG
1066       std::cout << "Big trouble : Pixel Element ("
1067                 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found"
1068                 << std::endl;  
1069 #endif //GDCM_DEBUG
1070       return 0;
1071    }
1072 }
1073
1074 /// \todo TODO : unify those two (previous one and next one)
1075 /**
1076  * \brief   Recover the pixel area length (in Bytes)
1077  * @return Pixel Element Length, as stored in the header
1078  *         (NOT the memory space necessary to hold the Pixels 
1079  *          -in case of embeded compressed image-)
1080  *         0 : NOT USABLE file. The caller has to check.
1081  */
1082 size_t Header::GetPixelAreaLength()
1083 {
1084    DocEntry* pxlElement = GetDocEntryByNumber(GrPixel,NumPixel);
1085    if ( pxlElement )
1086    {
1087       return pxlElement->GetLength();
1088    }
1089    else
1090    {
1091 #ifdef GDCM_DEBUG
1092       std::cout << "Big trouble : Pixel Element ("
1093                 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found"
1094                 << std::endl;
1095 #endif //GDCM_DEBUG
1096       return 0;
1097    }
1098 }
1099
1100 /**
1101   * \brief tells us if LUT are used
1102   * \warning Right now, 'Segmented xxx Palette Color Lookup Table Data'
1103   *          are NOT considered as LUT, since nobody knows
1104   *          how to deal with them
1105   *          Please warn me if you know sbdy that *does* know ... jprx
1106   * @return true if LUT Descriptors and LUT Tables were found 
1107   */
1108 bool Header::HasLUT()
1109 {
1110    // Check the presence of the LUT Descriptors, and LUT Tables    
1111    // LutDescriptorRed    
1112    if ( !GetDocEntryByNumber(0x0028,0x1101) )
1113    {
1114       return false;
1115    }
1116    // LutDescriptorGreen 
1117    if ( !GetDocEntryByNumber(0x0028,0x1102) )
1118    {
1119       return false;
1120    }
1121    // LutDescriptorBlue 
1122    if ( !GetDocEntryByNumber(0x0028,0x1103) )
1123    {
1124       return false;
1125    }
1126    // Red Palette Color Lookup Table Data
1127    if ( !GetDocEntryByNumber(0x0028,0x1201) )
1128    {
1129       return false;
1130    }
1131    // Green Palette Color Lookup Table Data       
1132    if ( !GetDocEntryByNumber(0x0028,0x1202) )
1133    {
1134       return false;
1135    }
1136    // Blue Palette Color Lookup Table Data      
1137    if ( !GetDocEntryByNumber(0x0028,0x1203) )
1138    {
1139       return false;
1140    }
1141
1142    // FIXME : (0x0028,0x3006) : LUT Data (CTX dependent)
1143    //         NOT taken into account, but we don't know how to use it ...   
1144    return true;
1145 }
1146
1147 /**
1148   * \brief gets the info from 0028,1101 : Lookup Table Desc-Red
1149   *             else 0
1150   * @return Lookup Table number of Bits , 0 by default
1151   *          when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
1152   * @ return bit number of each LUT item 
1153   */
1154 int Header::GetLUTNbits()
1155 {
1156    std::vector<std::string> tokens;
1157    int lutNbits;
1158
1159    //Just hope Lookup Table Desc-Red = Lookup Table Desc-Red
1160    //                                = Lookup Table Desc-Blue
1161    // Consistency already checked in GetLUTLength
1162    std::string lutDescription = GetEntryByNumber(0x0028,0x1101);
1163    if ( lutDescription == GDCM_UNFOUND )
1164    {
1165       return 0;
1166    }
1167
1168    tokens.clear(); // clean any previous value
1169    Util::Tokenize ( lutDescription, tokens, "\\" );
1170    //LutLength=atoi(tokens[0].c_str());
1171    //LutDepth=atoi(tokens[1].c_str());
1172
1173    lutNbits = atoi( tokens[2].c_str() );
1174    tokens.clear();
1175
1176    return lutNbits;
1177 }
1178
1179 /**
1180  * \brief Accesses the info from 0002,0010 : Transfert Syntax and TS
1181  *        else 1.
1182  * @return The full Transfert Syntax Name (as opposed to Transfert Syntax UID)
1183  */
1184 std::string Header::GetTransfertSyntaxName()
1185 {
1186    // use the TS (TS : Transfert Syntax)
1187    std::string transfertSyntax = GetEntryByNumber(0x0002,0x0010);
1188
1189    if ( transfertSyntax == GDCM_NOTLOADED )
1190    {
1191       std::cout << "Transfert Syntax not loaded. " << std::endl
1192                << "Better you increase MAX_SIZE_LOAD_ELEMENT_VALUE"
1193                << std::endl;
1194       return "Uncompressed ACR-NEMA";
1195    }
1196    if ( transfertSyntax == GDCM_UNFOUND )
1197    {
1198       dbg.Verbose(0, "Header::GetTransfertSyntaxName:"
1199                      " unfound Transfert Syntax (0002,0010)");
1200       return "Uncompressed ACR-NEMA";
1201    }
1202
1203    while ( ! isdigit((unsigned char)transfertSyntax[transfertSyntax.length()-1]) )
1204    {
1205       transfertSyntax.erase(transfertSyntax.length()-1, 1);
1206    }
1207    // we do it only when we need it
1208    TS* ts         = Global::GetTS();
1209    std::string tsName = ts->GetValue( transfertSyntax );
1210
1211    //delete ts; /// \todo Seg Fault when deleted ?!
1212    return tsName;
1213 }
1214
1215 //-----------------------------------------------------------------------------
1216 // Protected
1217
1218 /**
1219  * \brief anonymize a Header (removes Patient's personal info)
1220  *        (read the code to see which ones ...)
1221  */
1222 bool Header::AnonymizeHeader()
1223 {
1224    // If exist, replace by spaces
1225    SetEntryByNumber ("  ",0x0010, 0x2154); // Telephone   
1226    SetEntryByNumber ("  ",0x0010, 0x1040); // Adress
1227    SetEntryByNumber ("  ",0x0010, 0x0020); // Patient ID
1228
1229    DocEntry* patientNameHE = GetDocEntryByNumber (0x0010, 0x0010);
1230   
1231    if ( patientNameHE ) // we replace it by Study Instance UID (why not)
1232    {
1233       std::string studyInstanceUID =  GetEntryByNumber (0x0020, 0x000d);
1234       if ( studyInstanceUID != GDCM_UNFOUND )
1235       {
1236          ReplaceOrCreateByNumber(studyInstanceUID, 0x0010, 0x0010);
1237       }
1238       else
1239       {
1240          ReplaceOrCreateByNumber("anonymised", 0x0010, 0x0010);
1241       }
1242    }
1243
1244   // Just for fun :-(
1245   // (if any) remove or replace all the stuff that contains a Date
1246
1247 //0008 0012 DA ID Instance Creation Date
1248 //0008 0020 DA ID Study Date
1249 //0008 0021 DA ID Series Date
1250 //0008 0022 DA ID Acquisition Date
1251 //0008 0023 DA ID Content Date
1252 //0008 0024 DA ID Overlay Date
1253 //0008 0025 DA ID Curve Date
1254 //0008 002a DT ID Acquisition Datetime
1255 //0018 9074 DT ACQ Frame Acquisition Datetime
1256 //0018 9151 DT ACQ Frame Reference Datetime
1257 //0018 a002 DT ACQ Contribution Date Time
1258 //0020 3403 SH REL Modified Image Date (RET)
1259 //0032 0032 DA SDY Study Verified Date
1260 //0032 0034 DA SDY Study Read Date
1261 //0032 1000 DA SDY Scheduled Study Start Date
1262 //0032 1010 DA SDY Scheduled Study Stop Date
1263 //0032 1040 DA SDY Study Arrival Date
1264 //0032 1050 DA SDY Study Completion Date
1265 //0038 001a DA VIS Scheduled Admission Date
1266 //0038 001c DA VIS Scheduled Discharge Date
1267 //0038 0020 DA VIS Admitting Date
1268 //0038 0030 DA VIS Discharge Date
1269 //0040 0002 DA PRC Scheduled Procedure Step Start Date
1270 //0040 0004 DA PRC Scheduled Procedure Step End Date
1271 //0040 0244 DA PRC Performed Procedure Step Start Date
1272 //0040 0250 DA PRC Performed Procedure Step End Date
1273 //0040 2004 DA PRC Issue Date of Imaging Service Request
1274 //0040 4005 DT PRC Scheduled Procedure Step Start Date and Time
1275 //0040 4011 DT PRC Expected Completion Date and Time
1276 //0040 a030 DT PRC Verification Date Time
1277 //0040 a032 DT PRC Observation Date Time
1278 //0040 a120 DT PRC DateTime
1279 //0040 a121 DA PRC Date
1280 //0040 a13a DT PRC Referenced Datetime
1281 //0070 0082 DA ??? Presentation Creation Date
1282 //0100 0420 DT ??? SOP Autorization Date and Time
1283 //0400 0105 DT ??? Digital Signature DateTime
1284 //2100 0040 DA PJ Creation Date
1285 //3006 0008 DA SSET Structure Set Date
1286 //3008 0024 DA ??? Treatment Control Point Date
1287 //3008 0054 DA ??? First Treatment Date
1288 //3008 0056 DA ??? Most Recent Treatment Date
1289 //3008 0162 DA ??? Safe Position Exit Date
1290 //3008 0166 DA ??? Safe Position Return Date
1291 //3008 0250 DA ??? Treatment Date
1292 //300a 0006 DA RT RT Plan Date
1293 //300a 022c DA RT Air Kerma Rate Reference Date
1294 //300e 0004 DA RT Review Date
1295
1296    return true;
1297 }
1298
1299 /**
1300   * \brief gets the info from 0020,0037 : Image Orientation Patient
1301   * @param iop adress of the (6)float aray to receive values
1302   * @return cosines of image orientation patient
1303   */
1304 void Header::GetImageOrientationPatient( float iop[6] )
1305 {
1306    std::string strImOriPat;
1307    //iop is supposed to be float[6]
1308    iop[0] = iop[1] = iop[2] = iop[3] = iop[4] = iop[5] = 0.;
1309
1310    // 0020 0037 DS REL Image Orientation (Patient)
1311    if ( (strImOriPat = GetEntryByNumber(0x0020,0x0037)) != GDCM_UNFOUND )
1312    {
1313       if( sscanf( strImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f", 
1314           &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6 )
1315       {
1316          dbg.Verbose(0, "Header::GetImageOrientationPatient: "
1317                         "wrong Image Orientation Patient (0020,0037)");
1318       }
1319    }
1320    //For ACR-NEMA
1321    // 0020 0035 DS REL Image Orientation (RET)
1322    else if ( (strImOriPat = GetEntryByNumber(0x0020,0x0035)) != GDCM_UNFOUND )
1323    {
1324       if( sscanf( strImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f", 
1325           &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6 )
1326       {
1327          dbg.Verbose(0, "Header::GetImageOrientationPatient: "
1328                         "wrong Image Orientation Patient (0020,0035)");
1329       }
1330    }
1331 }
1332
1333 /**
1334  * \brief Initialize a default DICOM header that should contain all the
1335  *        field require by other reader. DICOM standard does not 
1336  *        explicitely defines those fields, heuristic has been choosen.
1337  *        This is not perfect as we are writting a CT image...
1338  */
1339 void Header::InitializeDefaultHeader()
1340 {
1341    typedef struct
1342    {
1343       const char *value;
1344       uint16_t group;
1345       uint16_t elem;
1346    } DICOM_DEFAULT_VALUE;
1347
1348    std::string date = Util::GetCurrentDate();
1349    std::string time = Util::GetCurrentTime();
1350    std::string uid = Util::CreateUniqueUID();
1351    std::string uidMedia = uid;
1352    std::string uidClass = uid + ".1";
1353    std::string uidInst  = uid + ".10";
1354    std::string uidStudy = uid + ".100";
1355    std::string uidSerie = uid + ".1000";
1356
1357    static DICOM_DEFAULT_VALUE defaultvalue[] = {
1358     { "146 ",                      0x0002, 0x0000}, // Meta Element Group Length // FIXME: how to recompute ?
1359     { "1.2.840.10008.5.1.4.1.1.2", 0x0002, 0x0002}, // Media Storage SOP Class UID (CT Image Storage)
1360     { uidClass.c_str(),            0x0002, 0x0003}, // Media Storage SOP Instance UID
1361     { uidClass.c_str(),            0x0002, 0x0012}, // META Implementation Class UID
1362     { "GDCM",                      0x0002, 0x0016}, // Source Application Entity Title
1363
1364     { date.c_str(),                0x0008, 0x0012}, // Instance Creation Date
1365     { time.c_str(),                0x0008, 0x0013}, // Instance Creation Time
1366     { "1.2.840.10008.5.1.4.1.1.2", 0x0008, 0x0016}, // SOP Class UID
1367     { uidInst.c_str(),             0x0008, 0x0018}, // SOP Instance UID
1368     { "CT",                        0x0008, 0x0060}, // Modality    
1369     { "GDCM",                      0x0008, 0x0070}, // Manufacturer
1370     { "GDCM",                      0x0008, 0x0080}, // Institution Name
1371     { "http://www-creatis.insa-lyon.fr/Public/Gdcm", 0x0008, 0x0081},  // Institution Address
1372
1373     { "GDCM",                      0x0010, 0x0010}, // Patient's Name
1374     { "GDCMID",                    0x0010, 0x0020}, // Patient ID
1375
1376     { uidStudy.c_str(),            0x0020, 0x000d}, // Study Instance UID
1377     { uidSerie.c_str(),            0x0020, 0x000e}, // Series Instance UID
1378     { "1",                         0x0020, 0x0010}, // StudyID
1379     { "1",                         0x0020, 0x0011}, // SeriesNumber
1380
1381     { "1",                         0x0028, 0x0002}, // Samples per pixel 1 or 3
1382     { "MONOCHROME1",               0x0028, 0x0004}, // photochromatic interpretation
1383     { "0",                         0x0028, 0x0010}, // nbRows
1384     { "0",                         0x0028, 0x0011}, // nbCols
1385     { "8",                         0x0028, 0x0100}, // BitsAllocated 8 or 12 or 16
1386     { "8",                         0x0028, 0x0101}, // BitsStored    <= BitsAllocated
1387     { "7",                         0x0028, 0x0102}, // HighBit       <= BitsAllocated - 1
1388     { "0",                         0x0028, 0x0103}, // Pixel Representation 0(unsigned) or 1(signed)
1389     { 0, 0, 0 }
1390    };
1391
1392    // default value
1393    // Special case this is the image (not a string)
1394    GrPixel = 0x7fe0;
1395    NumPixel = 0x0010;
1396    ReplaceOrCreateByNumber(0, 0, GrPixel, NumPixel);
1397
1398    // All remaining strings:
1399    unsigned int i = 0;
1400    DICOM_DEFAULT_VALUE current = defaultvalue[i];
1401    while( current.value )
1402    {
1403       ReplaceOrCreateByNumber(current.value, current.group, current.elem);
1404       current = defaultvalue[++i];
1405    }
1406 }
1407
1408
1409 //-----------------------------------------------------------------------------
1410 // Private
1411
1412 //-----------------------------------------------------------------------------
1413
1414 } // end namespace gdcm