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