]> Creatis software - gdcm.git/blob - src/gdcmHeader.cxx
Still in a debugging status.
[gdcm.git] / src / gdcmHeader.cxx
1 // gdcmHeader.cxx
2 //-----------------------------------------------------------------------------
3 #include "gdcmHeader.h"
4 #include "gdcmGlobal.h"
5 #include "gdcmUtil.h"
6 #include "gdcmDebug.h"
7 #include "gdcmTS.h"
8 #include "gdcmValEntry.h"
9
10 #include <vector>
11
12 //-----------------------------------------------------------------------------
13 // Constructor / Destructor
14 /**
15  * \brief  Constructor 
16  * @param  InFilename name of the file whose header we want to analyze
17  * @param  exception_on_error whether we want to throw an exception or not
18  * @param  enable_sequences = true to allow the header 
19  *         to be parsed *inside* the SeQuences, when they have an actual length 
20  * @param  ignore_shadow = true if user wants to skip shadow groups 
21  *         during parsing, to save memory space
22  */
23 gdcmHeader::gdcmHeader(const char *InFilename, 
24                        bool exception_on_error,
25                        bool enable_sequences, 
26                        bool ignore_shadow):
27    gdcmDocument(InFilename,exception_on_error,enable_sequences,ignore_shadow)
28
29 /*
30    typedef struct {
31       guint32 totalSQlength;
32       guint32 alreadyParsedlength;
33    } pileElem;
34 */
35    
36    // for some ACR-NEMA images GrPixel, NumPixel is *not* 7fe0,0010
37    // We may encounter the 'RETired' (0x0028, 0x0200) tag
38    // (Image Location") . This Element contains the number of
39    // the group that contains the pixel data (hence the "Pixel Data"
40    // is found by indirection through the "Image Location").
41    // Inside the group pointed by "Image Location" the searched element
42    // is conventionally the element 0x0010 (when the norm is respected).
43    // When the "Image Location" is absent we default to group 0x7fe0.
44    
45    // This IS the right place for the code
46  
47    std::string ImageLocation = GetEntryByNumber(0x0028, 0x0200);
48    if ( ImageLocation == GDCM_UNFOUND ) { // Image Location
49       GrPixel = 0x7fe0;                   // default value
50    } else {
51       GrPixel = (guint16) atoi( ImageLocation.c_str() );
52    }   
53    if (GrPixel == 0xe07f) // sometimes Image Location value doesn't follow 
54       GrPixel = 0x7fe0;   // the supposed processor endianity. 
55                           // see gdcmData/cr172241.dcm      
56    if (GrPixel != 0x7fe0) 
57       // This is a kludge for old dirty Philips imager.
58       NumPixel = 0x1010;
59    else
60       NumPixel = 0x0010;
61    
62 }
63
64 /**
65  * \brief Constructor  
66  * @param exception_on_error whether we want to throw an exception or not
67  */
68 gdcmHeader::gdcmHeader(bool exception_on_error) :
69    gdcmDocument(exception_on_error)
70 {
71 }
72
73 /**
74  * \ingroup gdcmHeader
75  * \brief   Canonical destructor.
76  */
77 gdcmHeader::~gdcmHeader (void) {
78 }
79
80 //-----------------------------------------------------------------------------
81 // Print
82
83
84 //-----------------------------------------------------------------------------
85 // Public
86
87 /**
88  * \ingroup gdcmHeader
89  * \brief  This predicate, based on hopefully reasonable heuristics,
90  *         decides whether or not the current gdcmParser was properly parsed
91  *         and contains the mandatory information for being considered as
92  *         a well formed and usable Dicom/Acr File.
93  * @return true when gdcmParser is the one of a reasonable Dicom/Acr file,
94  *         false otherwise. 
95  */
96 bool gdcmHeader::IsReadable(void) {
97    if(!gdcmDocument::IsReadable()) {
98       return(false);
99    }
100    std::string res = GetEntryByNumber(0x0028, 0x0005);
101    if ( res != GDCM_UNFOUND && atoi(res.c_str()) > 4 ) 
102       return false; // Image Dimensions
103    if ( !GetDocEntryByNumber(0x0028, 0x0100) )
104       return false; // "Bits Allocated"
105    if ( !GetDocEntryByNumber(0x0028, 0x0101) )
106       return false; // "Bits Stored"
107    if ( !GetDocEntryByNumber(0x0028, 0x0102) )
108       return false; // "High Bit"
109    if ( !GetDocEntryByNumber(0x0028, 0x0103) )
110       return false; // "Pixel Representation" i.e. 'Sign'
111    return true;
112 }
113
114 /**
115  * \ingroup gdcmHeader
116  * \brief   Determines if the Transfer Syntax was already encountered
117  *          and if it corresponds to a JPEGBaseLineProcess1 one.
118  * @return  True when JPEGBaseLineProcess1found. False in all other cases.
119  */
120 bool gdcmHeader::IsJPEGBaseLineProcess1TransferSyntax(void) {
121    gdcmDocEntry* Element = GetDocEntryByNumber(0x0002, 0x0010);
122    if ( !Element )
123       return false;
124    LoadDocEntrySafe(Element);
125
126    std::string Transfer = ((gdcmValEntry *)Element)->GetValue();
127    if ( Transfer == "1.2.840.10008.1.2.4.50" )
128       return true;
129    return false;
130 }
131
132 /**
133  * \ingroup gdcmHeader
134  * \brief   Determines if the Transfer Syntax was already encountered
135  *          and if it corresponds to a JPEGExtendedProcess2-4 one.
136  * @return  True when JPEGExtendedProcess2-4 found. False in all other cases.
137  */
138 bool gdcmHeader::IsJPEGExtendedProcess2_4TransferSyntax(void) {
139    gdcmDocEntry* Element = GetDocEntryByNumber(0x0002, 0x0010);
140    if ( !Element )
141       return false;
142    LoadDocEntrySafe(Element);
143    return ( ((gdcmValEntry *)Element)->GetValue() == "1.2.840.10008.1.2.4.51" );
144 }
145
146 /**
147  * \ingroup gdcmHeader
148  * \brief   Determines if the Transfer Syntax was already encountered
149  *          and if it corresponds to a JPEGExtendeProcess3-5 one.
150  * @return  True when JPEGExtendedProcess3-5 found. False in all other cases.
151  */
152 bool gdcmHeader::IsJPEGExtendedProcess3_5TransferSyntax(void) {
153    gdcmDocEntry* Element = GetDocEntryByNumber(0x0002, 0x0010);
154    if ( !Element )
155       return false;
156    LoadDocEntrySafe(Element);
157
158    std::string Transfer = ((gdcmValEntry *)Element)->GetValue();
159    if ( Transfer == "1.2.840.10008.1.2.4.52" )
160       return true;
161    return false;
162 }
163
164 /**
165  * \ingroup gdcmHeader
166  * \brief   Determines if the Transfer Syntax was already encountered
167  *          and if it corresponds to a JPEGSpectralSelectionProcess6-8 one.
168  * @return  True when JPEGSpectralSelectionProcess6-8 found. False in all
169  *          other cases.
170  */
171 bool gdcmHeader::IsJPEGSpectralSelectionProcess6_8TransferSyntax(void) {
172    gdcmDocEntry* Element = GetDocEntryByNumber(0x0002, 0x0010);
173    if ( !Element )
174       return false;
175    LoadDocEntrySafe(Element);
176
177    std::string Transfer = ((gdcmValEntry *)Element)->GetValue();
178    if ( Transfer == "1.2.840.10008.1.2.4.53" )
179       return true;
180    return false;
181 }
182
183 /**
184  * \ingroup gdcmHeader
185  * \brief   Determines if the Transfer Syntax was already encountered
186  *          and if it corresponds to a RLE Lossless one.
187  * @return  True when RLE Lossless found. False in all
188  *          other cases.
189  */
190 bool gdcmHeader::IsRLELossLessTransferSyntax(void) {
191    gdcmDocEntry* Element = GetDocEntryByNumber(0x0002, 0x0010);
192    if ( !Element )
193       return false;
194    LoadDocEntrySafe(Element);
195
196    std::string Transfer = ((gdcmValEntry *)Element)->GetValue();
197    if ( Transfer == "1.2.840.10008.1.2.5" ) {
198       return true;
199     }
200    return false;
201 }
202
203 /**
204  * \ingroup gdcmHeader
205  * \brief  Determines if Transfer Syntax was already encountered
206  *          and if it corresponds to a JPEG Lossless one. 
207  * @return  True when RLE Lossless found. False in all
208  *          other cases. 
209  */
210 bool gdcmHeader::IsJPEGLossless(void) {
211    gdcmDocEntry* Element = GetDocEntryByNumber(0x0002, 0x0010);
212     // faire qq chose d'intelligent a la place de ça
213    if ( !Element )
214       return false;
215    LoadDocEntrySafe(Element);
216
217    const char * Transfert = ((gdcmValEntry *)Element)->GetValue().c_str();
218    
219    if ( memcmp(Transfert+strlen(Transfert)-2 ,"70",2)==0) return true;
220    if ( memcmp(Transfert+strlen(Transfert)-2 ,"55",2)==0) return true;
221    
222    if (((gdcmValEntry *)Element)->GetValue() == "1.2.840.10008.1.2.4.57")   return true;
223
224    return false;
225 }
226
227 /**
228  * \ingroup gdcmHeader
229  * \brief   Determines if the Transfer Syntax was already encountered
230  *          and if it corresponds to a JPEG2000 one
231  * @return  True when JPEG2000 (Lossly or LossLess) found. False in all
232  *          other cases.
233  */
234 bool gdcmHeader::IsJPEG2000(void) {
235    gdcmDocEntry* Element = GetDocEntryByNumber(0x0002, 0x0010);
236    if ( !Element )
237       return false;
238    LoadDocEntrySafe(Element);
239
240    std::string Transfer = ((gdcmValEntry *)Element)->GetValue();
241    if (    (Transfer == "1.2.840.10008.1.2.4.90") 
242         || (Transfer == "1.2.840.10008.1.2.4.91") )
243       return true;
244    return false;
245 }
246
247 /**
248  * \ingroup gdcmHeader
249  * \brief   Predicate for dicom version 3 file.
250  * @return  True when the file is a dicom version 3.
251  */
252 bool gdcmHeader::IsDicomV3(void) {
253    // Checking if Transfert Syntax exists is enough
254    // Anyway, it's to late check if the 'Preamble' was found ...
255    // And ... would it be a rich idea to check ?
256    // (some 'no Preamble' DICOM images exist !)
257    return (GetDocEntryByNumber(0x0002, 0x0010) != NULL);
258 }
259
260 /**
261  * \ingroup gdcmHeader
262  * \brief   Retrieve the number of columns of image.
263  * @return  The encountered size when found, 0 by default.
264  *          0 means the file is NOT USABLE. The caller will have to check
265  */
266 int gdcmHeader::GetXSize(void) {
267    std::string StrSize;
268    StrSize = GetEntryByNumber(0x0028,0x0011);
269    if (StrSize == GDCM_UNFOUND)
270       return 0;
271    return atoi(StrSize.c_str());
272 }
273
274 /**
275  * \ingroup gdcmHeader
276  * \brief   Retrieve the number of lines of image.
277  * \warning The defaulted value is 1 as opposed to gdcmHeader::GetXSize()
278  * @return  The encountered size when found, 1 by default 
279  *          (The ACR-MEMA file contains a Signal, not an Image).
280  */
281 int gdcmHeader::GetYSize(void) {
282    std::string StrSize = GetEntryByNumber(0x0028,0x0010);
283    if (StrSize != GDCM_UNFOUND)
284       return atoi(StrSize.c_str());
285    if ( IsDicomV3() )
286       return 0;
287    else
288       // The Rows (0028,0010) entry was optional for ACR/NEMA. It might
289       // hence be a signal (1d image). So we default to 1:
290       return 1;
291 }
292
293 /**
294  * \ingroup gdcmHeader
295  * \brief   Retrieve the number of planes of volume or the number
296  *          of frames of a multiframe.
297  * \warning When present we consider the "Number of Frames" as the third
298  *          dimension. When absent we consider the third dimension as
299  *          being the ACR-NEMA "Planes" tag content.
300  * @return  The encountered size when found, 1 by default (single image).
301  */
302 int gdcmHeader::GetZSize(void) {
303    // Both  DicomV3 and ACR/Nema consider the "Number of Frames"
304    // as the third dimension.
305    std::string StrSize = GetEntryByNumber(0x0028,0x0008);
306    if (StrSize != GDCM_UNFOUND)
307       return atoi(StrSize.c_str());
308
309    // We then consider the "Planes" entry as the third dimension 
310    StrSize = GetEntryByNumber(0x0028,0x0012);
311    if (StrSize != GDCM_UNFOUND)
312       return atoi(StrSize.c_str());
313    return 1;
314 }
315
316 /**
317  * \ingroup gdcmHeader
318  * \brief   Retrieve the number of Bits Stored (actually used)
319  *          (as opposite to number of Bits Allocated)
320  * @return  The encountered number of Bits Stored, 0 by default.
321  *          0 means the file is NOT USABLE. The caller has to check it !
322  */
323 int gdcmHeader::GetBitsStored(void) {  
324    std::string StrSize = GetEntryByNumber(0x0028,0x0101);
325    if (StrSize == GDCM_UNFOUND)
326       return 0;  // It's supposed to be mandatory
327                  // the caller will have to check
328    return atoi(StrSize.c_str());
329 }
330
331 /**
332  * \ingroup gdcmHeader
333  * \brief   Retrieve the number of Bits Allocated
334  *          (8, 12 -compacted ACR-NEMA files, 16, ...)
335  * @return  The encountered number of Bits Allocated, 0 by default.
336  *          0 means the file is NOT USABLE. The caller has to check it !
337  */
338 int gdcmHeader::GetBitsAllocated(void) {
339    std::string StrSize = GetEntryByNumber(0x0028,0x0100);
340    if (StrSize == GDCM_UNFOUND)
341       return 0; // It's supposed to be mandatory
342                 // the caller will have to check
343    return atoi(StrSize.c_str());
344 }
345
346 /**
347  * \ingroup gdcmHeader
348  * \brief   Retrieve the number of Samples Per Pixel
349  *          (1 : gray level, 3 : RGB -1 or 3 Planes-)
350  * @return  The encountered number of Samples Per Pixel, 1 by default.
351  *          (Gray level Pixels)
352  */
353 int gdcmHeader::GetSamplesPerPixel(void) {
354    std::string StrSize = GetEntryByNumber(0x0028,0x0002);
355    if (StrSize == GDCM_UNFOUND)
356       return 1; // Well, it's supposed to be mandatory ...
357                 // but sometimes it's missing : *we* assume Gray pixels
358    return atoi(StrSize.c_str());
359 }
360
361 /**
362  * \ingroup gdcmHeader
363  * \brief   Retrieve the Planar Configuration for RGB images
364  *          (0 : RGB Pixels , 1 : R Plane + G Plane + B Plane)
365  * @return  The encountered Planar Configuration, 0 by default.
366  */
367 int gdcmHeader::GetPlanarConfiguration(void) {
368    std::string StrSize = GetEntryByNumber(0x0028,0x0006);
369    if (StrSize == GDCM_UNFOUND)
370       return 0;
371    return atoi(StrSize.c_str());
372 }
373
374 /**
375  * \ingroup gdcmHeader
376  * \brief   Return the size (in bytes) of a single pixel of data.
377  * @return  The size in bytes of a single pixel of data; 0 by default
378  *          0 means the file is NOT USABLE; the caller will have to check        
379  */
380 int gdcmHeader::GetPixelSize(void) {
381    std::string PixelType = GetPixelType();
382    if (PixelType == "8U"  || PixelType == "8S")
383       return 1;
384    if (PixelType == "16U" || PixelType == "16S")
385       return 2;
386    if (PixelType == "32U" || PixelType == "32S")
387       return 4;
388    if (PixelType == "FD")
389       return 8;         
390    dbg.Verbose(0, "gdcmHeader::GetPixelSize: Unknown pixel type");
391    return 0;
392 }
393
394 /**
395  * \ingroup gdcmHeader
396  * \brief   Build the Pixel Type of the image.
397  *          Possible values are:
398  *          - 8U  unsigned  8 bit,
399  *          - 8S    signed  8 bit,
400  *          - 16U unsigned 16 bit,
401  *          - 16S   signed 16 bit,
402  *          - 32U unsigned 32 bit,
403  *          - 32S   signed 32 bit,
404  *          - FD floating double 64 bits (Not kosher DICOM, but so usefull!)
405  * \warning 12 bit images appear as 16 bit.
406  *          24 bit images appear as 8 bit
407  * @return  0S if nothing found. NOT USABLE file. The caller has to check
408  */
409 std::string gdcmHeader::GetPixelType(void) { 
410    std::string BitsAlloc = GetEntryByNumber(0x0028, 0x0100); // Bits Allocated
411    if (BitsAlloc == GDCM_UNFOUND) {
412       dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Bits Allocated");
413       BitsAlloc = std::string("16");
414    }
415    if (BitsAlloc == "64")            // )
416       return ("FD");
417    if (BitsAlloc == "12")            // It will be unpacked
418       BitsAlloc = std::string("16");
419    else if (BitsAlloc == "24")       // (in order no to be messed up
420       BitsAlloc = std::string("8");  // by old RGB images)
421      
422    std::string Signed = GetEntryByNumber(0x0028, 0x0103); // "Pixel Representation"
423    if (Signed == GDCM_UNFOUND) {
424       dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Pixel Representation");
425       BitsAlloc = std::string("0");
426    }
427    if (Signed == "0")
428       Signed = std::string("U");
429    else
430       Signed = std::string("S");
431
432    return( BitsAlloc + Signed);
433 }
434
435
436 /**
437  * \ingroup gdcmHeader
438  * \brief   Recover the offset (from the beginning of the file) 
439  *          of *image* pixels (not *icone image* pixels, if any !)
440  * @return Pixel Offset
441  */
442 size_t gdcmHeader::GetPixelOffset(void) { 
443       
444    gdcmDocEntry* PixelElement = GetDocEntryByNumber(GrPixel,NumPixel);
445  
446    if (PixelElement) {
447       return PixelElement->GetOffset();
448    } else {
449 #ifdef GDCM_DEBUG
450       std::cout << "Big trouble : Pixel Element ("
451                 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found"
452                 << std::endl;  
453 #endif //GDCM_DEBUG
454       return 0;
455    }     
456 }
457 // TODO : unify those two (previous one and next one)
458 /**
459  * \ingroup gdcmHeader
460  * \brief   Recover the pixel area length (in Bytes)
461  * @return Pixel Element Length, as stored in the header
462  *         (NOT the memory space necessary to hold the Pixels 
463  *          -in case of embeded compressed image-)
464  *         0 : NOT USABLE file. The caller has to check.
465  */
466 size_t gdcmHeader::GetPixelAreaLength(void) { 
467           
468    gdcmDocEntry* PixelElement = GetDocEntryByNumber(GrPixel,NumPixel);
469
470    if (PixelElement) {
471       return PixelElement->GetLength();
472    } else {
473 #ifdef GDCM_DEBUG
474       std::cout << "Big trouble : Pixel Element ("
475                 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found"
476                 << std::endl;
477 #endif //GDCM_DEBUG
478       return 0;
479    }
480 }
481
482 /**
483   * \ingroup gdcmHeader
484   * \brief tells us if LUT are used
485   * \warning Right now, 'Segmented xxx Palette Color Lookup Table Data'
486   *          are NOT considered as LUT, since nobody knows
487   *          how to deal with them
488   *          Please warn me if you know sbdy that *does* know ... jprx
489   * @return true if LUT Descriptors and LUT Tables were found 
490   */
491 bool gdcmHeader::HasLUT(void) {
492
493    // Check the presence of the LUT Descriptors, and LUT Tables    
494    // LutDescriptorRed    
495    if ( !GetDocEntryByNumber(0x0028,0x1101) )
496       return false;
497    // LutDescriptorGreen 
498    if ( !GetDocEntryByNumber(0x0028,0x1102) )
499       return false;
500    // LutDescriptorBlue 
501    if ( !GetDocEntryByNumber(0x0028,0x1103) )
502       return false;   
503    // Red Palette Color Lookup Table Data
504    if ( !GetDocEntryByNumber(0x0028,0x1201) )
505       return false; 
506    // Green Palette Color Lookup Table Data       
507    if ( !GetDocEntryByNumber(0x0028,0x1202) )
508       return false;
509    // Blue Palette Color Lookup Table Data      
510    if ( !GetDocEntryByNumber(0x0028,0x1203) )
511       return false;
512    // FIXME : (0x0028,0x3006) : LUT Data (CTX dependent)
513    //         NOT taken into account, but we don't know how to use it ...   
514    return true;
515 }
516
517 /**
518   * \ingroup gdcmHeader
519   * \brief gets the info from 0028,1101 : Lookup Table Desc-Red
520   *             else 0
521   * @return Lookup Table number of Bits , 0 by default
522   *          when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
523   * @ return bit number of each LUT item 
524   */
525 int gdcmHeader::GetLUTNbits(void) {
526    std::vector<std::string> tokens;
527    //int LutLength;
528    //int LutDepth;
529    int LutNbits;
530    //Just hope Lookup Table Desc-Red = Lookup Table Desc-Red = Lookup Table Desc-Blue
531    // Consistency already checked in GetLUTLength
532    std::string LutDescription = GetEntryByNumber(0x0028,0x1101);
533    if (LutDescription == GDCM_UNFOUND)
534       return 0;
535    tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
536    Tokenize (LutDescription, tokens, "\\");
537    //LutLength=atoi(tokens[0].c_str());
538    //LutDepth=atoi(tokens[1].c_str());
539    LutNbits=atoi(tokens[2].c_str());
540    tokens.clear();
541    return LutNbits;
542 }
543
544 /**
545   * \ingroup gdcmHeader
546   * \brief builts Red/Green/Blue/Alpha LUT from Header
547   *         when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
548   *          and (0028,1101),(0028,1102),(0028,1102)  
549   *            - xxx Palette Color Lookup Table Descriptor - are found
550   *          and (0028,1201),(0028,1202),(0028,1202) 
551   *            - xxx Palette Color Lookup Table Data - are found 
552   * \warning does NOT deal with :
553   *   0028 1100 Gray Lookup Table Descriptor (Retired)
554   *   0028 1221 Segmented Red Palette Color Lookup Table Data
555   *   0028 1222 Segmented Green Palette Color Lookup Table Data
556   *   0028 1223 Segmented Blue Palette Color Lookup Table Data 
557   *   no known Dicom reader deals with them :-(
558   * @return a RGBA Lookup Table 
559   */ 
560 unsigned char * gdcmHeader::GetLUTRGBA(void) {
561 // Not so easy : see 
562 // http://www.barre.nom.fr/medical/dicom2/limitations.html#Color%20Lookup%20Tables
563
564 //  if Photometric Interpretation # PALETTE COLOR, no LUT to be done
565    if (GetEntryByNumber(0x0028,0x0004) != "PALETTE COLOR ") {
566       return NULL;
567    }  
568    int lengthR, debR, nbitsR;
569    int lengthG, debG, nbitsG;
570    int lengthB, debB, nbitsB;
571    
572 // Get info from Lut Descriptors
573 // (the 3 LUT descriptors may be different)    
574    std::string LutDescriptionR = GetEntryByNumber(0x0028,0x1101);
575    if (LutDescriptionR == GDCM_UNFOUND)
576       return NULL;
577    std::string LutDescriptionG = GetEntryByNumber(0x0028,0x1102);
578    if (LutDescriptionG == GDCM_UNFOUND)
579       return NULL;   
580    std::string LutDescriptionB = GetEntryByNumber(0x0028,0x1103);
581    if (LutDescriptionB == GDCM_UNFOUND)
582       return NULL;
583       
584    std::vector<std::string> tokens;
585       
586    tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
587    Tokenize (LutDescriptionR, tokens, "\\");
588    lengthR=atoi(tokens[0].c_str()); // Red LUT length in Bytes
589    debR   =atoi(tokens[1].c_str()); // subscript of the first Lut Value
590    nbitsR =atoi(tokens[2].c_str()); // Lut item size (in Bits)
591    tokens.clear();
592    
593    tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
594    Tokenize (LutDescriptionG, tokens, "\\");
595    lengthG=atoi(tokens[0].c_str()); // Green LUT length in Bytes
596    debG   =atoi(tokens[1].c_str()); // subscript of the first Lut Value
597    nbitsG =atoi(tokens[2].c_str()); // Lut item size (in Bits)
598    tokens.clear();  
599    
600    tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
601    Tokenize (LutDescriptionB, tokens, "\\");
602    lengthB=atoi(tokens[0].c_str()); // Blue LUT length in Bytes
603    debB   =atoi(tokens[1].c_str()); // subscript of the first Lut Value
604    nbitsB =atoi(tokens[2].c_str()); // Lut item size (in Bits)
605    tokens.clear();
606  
607    // Load LUTs into memory, (as they were stored on disk)
608    unsigned char *lutR = (unsigned char *)
609                          GetEntryVoidAreaByNumber(0x0028,0x1201);
610    unsigned char *lutG = (unsigned char *)
611                          GetEntryVoidAreaByNumber(0x0028,0x1202);
612    unsigned char *lutB = (unsigned char *)
613                          GetEntryVoidAreaByNumber(0x0028,0x1203); 
614    
615    if (!lutR || !lutG || !lutB ) {
616       return NULL;
617    } 
618    // forge the 4 * 8 Bits Red/Green/Blue/Alpha LUT 
619    
620    unsigned char *LUTRGBA = new unsigned char[1024]; // 256 * 4 (R, G, B, Alpha) 
621    if (!LUTRGBA) {
622       return NULL;
623    }
624    memset(LUTRGBA, 0, 1024);
625    // Bits Allocated
626    int nb;
627    std::string str_nb = GetEntryByNumber(0x0028,0x0100);
628    if (str_nb == GDCM_UNFOUND ) {
629       nb = 16;
630    } else {
631       nb = atoi(str_nb.c_str() );
632    }  
633    int mult;
634
635    if (nbitsR==16 && nb==8) // when LUT item size is different than pixel size
636       mult=2;               // high byte must be = low byte 
637    else                     // See PS 3.3-2003 C.11.1.1.2 p 619
638       mult=1; 
639  
640    // if we get a black image, let's just remove the '+1'
641    // from 'i*mult+1' and check again 
642    // if it works, we shall have to check the 3 Palettes
643    // to see which byte is ==0 (first one, or second one)
644    // and fix the code
645    // We give up the checking to avoid some (useless ?)overhead 
646    // (optimistic asumption)
647    unsigned char *a;      
648    int i;
649
650    a = LUTRGBA+0;
651    for(i=0;i<lengthR;i++) {
652       *a = lutR[i*mult+1]; 
653       a+=4;       
654    }        
655    a = LUTRGBA+1;
656    for(i=0;i<lengthG;i++) {
657       *a = lutG[i*mult+1]; 
658       a+=4;       
659    }  
660    a = LUTRGBA+2;
661    for(i=0;i<lengthB;i++) {
662       *a = lutB[i*mult+1]; 
663       a+=4;       
664    }  
665    a = LUTRGBA+3;
666    for(i=0;i<256;i++) {
667       *a = 1; // Alpha component
668       a+=4; 
669    } 
670    
671    //How to free the now useless LUTs?
672    //free(LutR); free(LutB); free(LutG); // Seg Fault when used
673    return(LUTRGBA);   
674
675
676 /**
677  * \ingroup gdcmHeader
678  * \brief gets the info from 0002,0010 : Transfert Syntax and gdcmTS
679  *        else 1.
680  * @return the full Transfert Syntax Name (as oposite to Transfert Syntax UID)
681  */
682 std::string gdcmHeader::GetTransfertSyntaxName(void) { 
683    // use the gdcmTS (TS : Transfert Syntax)
684    std::string TransfertSyntax = GetEntryByNumber(0x0002,0x0010);
685    if (TransfertSyntax == GDCM_UNFOUND) {
686       dbg.Verbose(0, "gdcmHeader::GetTransfertSyntaxName: unfound Transfert Syntax (0002,0010)");
687       return "Uncompressed ACR-NEMA";
688    }
689    // we do it only when we need it
690    gdcmTS * ts = gdcmGlobal::GetTS();
691    std::string tsName=ts->GetValue(TransfertSyntax);
692    //delete ts; // Seg Fault when deleted ?!
693    return tsName;
694 }
695
696 /**
697  * \ingroup   gdcmHeader
698  * \brief Sets the Pixel Area size in the Header
699  *        --> not-for-rats function
700  * @param ImageDataSize new Pixel Area Size
701  *        warning : nothing else is checked
702  */
703 void gdcmHeader::SetImageDataSize(size_t ImageDataSize) {
704    std::string content1;
705    char car[20];
706
707    // Assumes DocEntry (GrPixel, NumPixel) is unique ...   
708    //\todo deal with multiplicity (see gdcmData/icone.dcm)
709    sprintf(car,"%d",ImageDataSize);
710  
711    gdcmDocEntry *a = GetDocEntryByNumber(GrPixel, NumPixel);
712    a->SetLength(ImageDataSize);
713
714    ImageDataSize+=8;
715    sprintf(car,"%d",ImageDataSize);
716    content1=car;
717    SetEntryByNumber(content1, GrPixel, NumPixel);
718 }
719
720
721 /**
722  * \ingroup   gdcmHeader
723  * \brief compares 2 Headers, according to DICOMDIR rules
724  *        --> not-for-rats function
725  * \warning does NOT work with ACR-NEMA files
726  * \todo find a trick to solve the pb (use RET fields ?)
727  * @param header 
728  * @return true if 'smaller'
729  */
730  bool gdcmHeader::operator<(gdcmHeader &header){
731    std::string s1,s2;
732
733    // Patient Name
734    s1=this->GetEntryByNumber(0x0010,0x0010);
735    s2=header.GetEntryByNumber(0x0010,0x0010);
736    if(s1 < s2)
737       return(true);
738    else if(s1 > s2)
739       return(false);
740    else
741    {
742       // Patient ID
743       s1=this->GetEntryByNumber(0x0010,0x0020);
744       s2=header.GetEntryByNumber(0x0010,0x0020);
745       if (s1 < s2)
746          return(true);
747       else if (s1 > s2)
748          return(1);
749       else
750       {
751          // Study Instance UID
752          s1=this->GetEntryByNumber(0x0020,0x000d);
753          s2=header.GetEntryByNumber(0x0020,0x000d);
754          if (s1 < s2)
755             return(true);
756          else if(s1 > s2)
757             return(false);
758          else
759          {
760             // Serie Instance UID
761             s1=this->GetEntryByNumber(0x0020,0x000e);
762             s2=header.GetEntryByNumber(0x0020,0x000e);
763             if (s1 < s2)
764                return(true);
765             else if(s1 > s2)
766                return(false);
767          }
768       }
769    }
770    return(false);
771 }
772 // Replaced by gdcmDocument::WriteEntry
773 /*
774 bool gdcmHeader::WriteEntry(gdcmDocEntry *tag, FILE *_fp,FileType type)
775 {
776 cout << " gdcmHeader::WriteEntry : entree " << endl;
777    guint32 length = tag->GetLength();
778                                                                                 
779    // The value of a tag MUST (see the DICOM norm) be an odd number of
780    // bytes. When this is not the case, pad with an additional byte:
781    if(length%2==1)
782    {
783   //    tag->SetValue(tag->GetValue()+"\0"); // to go on compiling
784       tag->SetLength(tag->GetReadLength()+1);
785    }
786                                                                                 
787    WriteEntryTagVRLength(tag, _fp, type);
788                                                                                 
789    // Pixels are never loaded in the element !
790    // we stop writting when Pixel are processed
791    // FIX : we loose trailing elements (RAB, right now)
792    guint16 el     = tag->GetElement();
793    guint16 group  = tag->GetGroup();
794    WriteEntryValue(tag, _fp, type);
795    return true;
796 }
797 */
798 //-----------------------------------------------------------------------------
799 // Protected
800
801 /**
802  * \ingroup   gdcmHeader
803  * \brief anonymize a Header (removes Patient's personal info)
804  *        (read the code to see which ones ...)
805  */
806 bool gdcmHeader::anonymizeHeader() {
807
808   gdcmDocEntry *patientNameHE = GetDocEntryByNumber (0x0010, 0x0010);
809  // gdcmDocEntry *patientIDHE   = GetDocEntryByNumber (0x0010, 0x0020); 
810     
811   ReplaceIfExistByNumber ("  ",0x0010, 0x2154); // Telephone   
812   ReplaceIfExistByNumber ("  ",0x0010, 0x1040); // Adress
813   ReplaceIfExistByNumber ("  ",0x0010, 0x0020); // Patient ID
814   
815   if (patientNameHE) {
816      std::string StudyInstanceUID =  GetEntryByNumber (0x0020, 0x000d);
817      if (StudyInstanceUID !=GDCM_UNFOUND)
818         ReplaceOrCreateByNumber(StudyInstanceUID, 0x0010, 0x0010);
819      else
820         ReplaceOrCreateByNumber(std::string("anonymised"), 0x0010, 0x0010);
821   }
822   
823   // Just for fun :-(
824   // (if any) remove or replace all the stuff that contains a Date
825   
826 //0008 0012 DA ID Instance Creation Date
827 //0008 0020 DA ID Study Date
828 //0008 0021 DA ID Series Date
829 //0008 0022 DA ID Acquisition Date
830 //0008 0023 DA ID Content Date
831 //0008 0024 DA ID Overlay Date
832 //0008 0025 DA ID Curve Date
833 //0008 002a DT ID Acquisition Datetime
834 //0018 9074 DT ACQ Frame Acquisition Datetime
835 //0018 9151 DT ACQ Frame Reference Datetime
836 //0018 a002 DT ACQ Contribution Date Time
837 //0020 3403 SH REL Modified Image Date (RET)
838 //0032 0032 DA SDY Study Verified Date
839 //0032 0034 DA SDY Study Read Date
840 //0032 1000 DA SDY Scheduled Study Start Date
841 //0032 1010 DA SDY Scheduled Study Stop Date
842 //0032 1040 DA SDY Study Arrival Date
843 //0032 1050 DA SDY Study Completion Date
844 //0038 001a DA VIS Scheduled Admission Date
845 //0038 001c DA VIS Scheduled Discharge Date
846 //0038 0020 DA VIS Admitting Date
847 //0038 0030 DA VIS Discharge Date
848 //0040 0002 DA PRC Scheduled Procedure Step Start Date
849 //0040 0004 DA PRC Scheduled Procedure Step End Date
850 //0040 0244 DA PRC Performed Procedure Step Start Date
851 //0040 0250 DA PRC Performed Procedure Step End Date
852 //0040 2004 DA PRC Issue Date of Imaging Service Request
853 //0040 4005 DT PRC Scheduled Procedure Step Start Date and Time
854 //0040 4011 DT PRC Expected Completion Date and Time
855 //0040 a030 DT PRC Verification Date Time
856 //0040 a032 DT PRC Observation Date Time
857 //0040 a120 DT PRC DateTime
858 //0040 a121 DA PRC Date
859 //0040 a13a DT PRC Referenced Datetime
860 //0070 0082 DA ??? Presentation Creation Date
861 //0100 0420 DT ??? SOP Autorization Date and Time
862 //0400 0105 DT ??? Digital Signature DateTime
863 //2100 0040 DA PJ Creation Date
864 //3006 0008 DA SSET Structure Set Date
865 //3008 0024 DA ??? Treatment Control Point Date
866 //3008 0054 DA ??? First Treatment Date
867 //3008 0056 DA ??? Most Recent Treatment Date
868 //3008 0162 DA ??? Safe Position Exit Date
869 //3008 0166 DA ??? Safe Position Return Date
870 //3008 0250 DA ??? Treatment Date
871 //300a 0006 DA RT RT Plan Date
872 //300a 022c DA RT Air Kerma Rate Reference Date
873 //300e 0004 DA RT Review Date
874  return true;  
875 }
876 //-----------------------------------------------------------------------------
877 // Private
878
879 //-----------------------------------------------------------------------------