]> Creatis software - gdcm.git/blob - src/gdcmHeader.cxx
doxygenation
[gdcm.git] / src / gdcmHeader.cxx
1 // gdcmHeader.cxx
2 //-----------------------------------------------------------------------------
3 #include "gdcmHeader.h"
4
5 #include <stdio.h>
6 #include <cerrno>
7 #include <cctype>    // for isalpha
8
9 #include "gdcmUtil.h"
10 #include "gdcmTS.h"
11
12
13 //-----------------------------------------------------------------------------
14 // Constructor / Destructor
15 /**
16  * \ingroup gdcmHeader
17  * \brief   
18  * @param   InFilename
19  * @param   exception_on_error
20  * @param   enable_sequences = true to allow the header 
21  *          to be parsed *inside* the SeQuences, 
22  *          when they have an actual length 
23  * @param   ignore_shadow = true if user wants to skip shadow groups 
24  *           during parsing, to save memory space         
25  */
26 gdcmHeader::gdcmHeader(const char *InFilename, 
27                        bool exception_on_error,
28                        bool enable_sequences, 
29                        bool ignore_shadow):
30    gdcmParser(InFilename,exception_on_error,enable_sequences,ignore_shadow)
31
32    
33    // for some ACR-NEMA images GrPixel, NumPixel is *not* 7fe0,0010
34    // We may encounter the 'RETired' (0x0028, 0x0200) tag
35    // (Image Location") . This Element contains the number of
36    // the group that contains the pixel data (hence the "Pixel Data"
37    // is found by indirection through the "Image Location").
38    // Inside the group pointed by "Image Location" the searched element
39    // is conventionally the element 0x0010 (when the norm is respected).
40    // When the "Image Location" is absent we default to group 0x7fe0.
41    
42    // This IS the right place for the code
43  
44       std::string ImageLocation = GetEntryByNumber(0x0028, 0x0200);
45       if ( ImageLocation == GDCM_UNFOUND ) { // Image Location
46          GrPixel = 0x7fe0;                   // default value
47       } else {
48          GrPixel = (guint16) atoi( ImageLocation.c_str() );
49       }   
50       if (GrPixel == 0xe07f) // sometimes Image Location value doesn't follow 
51          GrPixel = 0x7fe0;   // the supposed processor endianity. 
52                              // see gdcmData/cr172241.dcm      
53       if (GrPixel != 0x7fe0) 
54          // This is a kludge for old dirty Philips imager.
55          NumPixel = 0x1010;
56       else
57          NumPixel = 0x0010;
58          
59       TagKey key = gdcmDictEntry::TranslateToKey(GrPixel, NumPixel);
60       countGrPixel = GetEntry().count(key);
61 }
62
63 /**
64  * \ingroup gdcmHeader
65  * \brief Constructor  
66  * @param   exception_on_error
67  */
68 gdcmHeader::gdcmHeader(bool exception_on_error) :
69    gdcmParser(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 // see gdcmParser.cxx
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(!gdcmParser::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 ( !GetHeaderEntryByNumber(0x0028, 0x0100) )
104       return false; // "Bits Allocated"
105    if ( !GetHeaderEntryByNumber(0x0028, 0x0101) )
106       return false; // "Bits Stored"
107    if ( !GetHeaderEntryByNumber(0x0028, 0x0102) )
108       return false; // "High Bit"
109    if ( !GetHeaderEntryByNumber(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    gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
122    if ( !Element )
123       return false;
124    LoadHeaderEntrySafe(Element);
125
126    std::string Transfer = 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    gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
140    if ( !Element )
141       return false;
142    LoadHeaderEntrySafe(Element);
143    return ( 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    gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
154    if ( !Element )
155       return false;
156    LoadHeaderEntrySafe(Element);
157
158    std::string Transfer = 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    gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
173    if ( !Element )
174       return false;
175    LoadHeaderEntrySafe(Element);
176
177    std::string Transfer = 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    gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
192    if ( !Element )
193       return false;
194    LoadHeaderEntrySafe(Element);
195
196    std::string Transfer = 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    gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
212     // faire qq chose d'intelligent a la place de ça
213    if ( !Element )
214       return false;
215    LoadHeaderEntrySafe(Element);
216
217    const char * Transfert = Element->GetValue().c_str();
218    if ( memcmp(Transfert+strlen(Transfert)-2 ,"70",2)==0) return true;
219    if ( memcmp(Transfert+strlen(Transfert)-2 ,"55",2)==0) return true;
220    if (Element->GetValue() == "1.2.840.10008.1.2.4.57")   return true;
221
222    return false;
223 }
224
225 /**
226  * \ingroup gdcmHeader
227  * \brief   Determines if the Transfer Syntax was already encountered
228  *          and if it corresponds to a JPEG2000 one
229  * @return  True when JPEG2000 (Lossly or LossLess) found. False in all
230  *          other cases.
231  */
232 bool gdcmHeader::IsJPEG2000(void) {
233    gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
234    if ( !Element )
235       return false;
236    LoadHeaderEntrySafe(Element);
237
238    std::string Transfer = Element->GetValue();
239    if (    (Transfer == "1.2.840.10008.1.2.4.90") 
240         || (Transfer == "1.2.840.10008.1.2.4.91") )
241       return true;
242    return false;
243 }
244
245 /**
246  * \ingroup gdcmHeader
247  * \brief   Predicate for dicom version 3 file.
248  * @return  True when the file is a dicom version 3.
249  */
250 bool gdcmHeader::IsDicomV3(void) {
251    // Checking if Transfert Syntax exists is enough
252    // Anyway, it's to late check if the 'Preamble' was found ...
253    // And ... would it be a rich idea to check ?
254    // (some 'no Preamble' DICOM images exist !)
255    return (GetHeaderEntryByNumber(0x0002, 0x0010) != NULL);
256 }
257
258 /**
259  * \ingroup gdcmHeader
260  * \brief   Retrieve the number of columns of image.
261  * @return  The encountered size when found, 0 by default.
262  *          0 means the file is NOT USABLE. The caller will have to check
263  */
264 int gdcmHeader::GetXSize(void) {
265    std::string StrSize;
266    StrSize = GetEntryByNumber(0x0028,0x0011);
267    if (StrSize == GDCM_UNFOUND)
268       return 0;
269    return atoi(StrSize.c_str());
270 }
271
272 /**
273  * \ingroup gdcmHeader
274  * \brief   Retrieve the number of lines of image.
275  * \warning The defaulted value is 1 as opposed to gdcmHeader::GetXSize()
276  * @return  The encountered size when found, 1 by default 
277  *          (The ACR-MEMA file contains a Signal, not an Image).
278  */
279 int gdcmHeader::GetYSize(void) {
280    std::string StrSize = GetEntryByNumber(0x0028,0x0010);
281    if (StrSize != GDCM_UNFOUND)
282       return atoi(StrSize.c_str());
283    if ( IsDicomV3() )
284       return 0;
285    else
286       // The Rows (0028,0010) entry was optional for ACR/NEMA. It might
287       // hence be a signal (1d image). So we default to 1:
288       return 1;
289 }
290
291 /**
292  * \ingroup gdcmHeader
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 gdcmHeader::GetZSize(void) {
301    // Both  DicomV3 and ACR/Nema consider the "Number of Frames"
302    // as the third dimension.
303    std::string StrSize = GetEntryByNumber(0x0028,0x0008);
304    if (StrSize != GDCM_UNFOUND)
305       return atoi(StrSize.c_str());
306
307    // We then consider the "Planes" entry as the third dimension 
308    StrSize = GetEntryByNumber(0x0028,0x0012);
309    if (StrSize != GDCM_UNFOUND)
310       return atoi(StrSize.c_str());
311    return 1;
312 }
313
314 /**
315  * \ingroup gdcmHeader
316  * \brief   Retrieve the number of Bits Stored (actually used)
317  *          (as opposite to number of Bits Allocated)
318  * @return  The encountered number of Bits Stored, 0 by default.
319  *          0 means the file is NOT USABLE. The caller has to check it !
320  */
321 int gdcmHeader::GetBitsStored(void) {  
322    std::string StrSize = GetEntryByNumber(0x0028,0x0101);
323    if (StrSize == GDCM_UNFOUND)
324       return 0;  // It's supposed to be mandatory
325                  // the caller will have to check
326    return atoi(StrSize.c_str());
327 }
328
329 /**
330  * \ingroup gdcmHeader
331  * \brief   Retrieve the number of Bits Allocated
332  *          (8, 12 -compacted ACR-NEMA files, 16, ...)
333  * @return  The encountered number of Bits Allocated, 0 by default.
334  *          0 means the file is NOT USABLE. The caller has to check it !
335  */
336 int gdcmHeader::GetBitsAllocated(void) {
337    std::string StrSize = GetEntryByNumber(0x0028,0x0100);
338    if (StrSize == GDCM_UNFOUND)
339       return 0; // It's supposed to be mandatory
340                 // the caller will have to check
341    return atoi(StrSize.c_str());
342 }
343
344 /**
345  * \ingroup gdcmHeader
346  * \brief   Retrieve the number of Samples Per Pixel
347  *          (1 : gray level, 3 : RGB -1 or 3 Planes-)
348  * @return  The encountered number of Samples Per Pixel, 1 by default.
349  *          (Gray level Pixels)
350  */
351 int gdcmHeader::GetSamplesPerPixel(void) {
352    std::string StrSize = GetEntryByNumber(0x0028,0x0002);
353    if (StrSize == GDCM_UNFOUND)
354       return 1; // Well, it's supposed to be mandatory ...
355                 // but sometimes it's missing : *we* assume Gray pixels
356    return atoi(StrSize.c_str());
357 }
358
359 /**
360  * \ingroup gdcmHeader
361  * \brief   Retrieve the Planar Configuration for RGB images
362  *          (0 : RGB Pixels , 1 : R Plane + G Plane + B Plane)
363  * @return  The encountered Planar Configuration, 0 by default.
364  */
365 int gdcmHeader::GetPlanarConfiguration(void) {
366    std::string StrSize = GetEntryByNumber(0x0028,0x0006);
367    if (StrSize == GDCM_UNFOUND)
368       return 0;
369    return atoi(StrSize.c_str());
370 }
371
372 /**
373  * \ingroup gdcmHeader
374  * \brief   Return the size (in bytes) of a single pixel of data.
375  * @return  The size in bytes of a single pixel of data; 0 by default
376  *          0 means the file is NOT USABLE; the caller will have to check        
377  */
378 int gdcmHeader::GetPixelSize(void) {
379    std::string PixelType = GetPixelType();
380    if (PixelType == "8U"  || PixelType == "8S")
381       return 1;
382    if (PixelType == "16U" || PixelType == "16S")
383       return 2;
384    if (PixelType == "32U" || PixelType == "32S")
385       return 4;
386    if (PixelType == "FD")
387       return 8;         
388    dbg.Verbose(0, "gdcmHeader::GetPixelSize: Unknown pixel type");
389    return 0;
390 }
391
392 /**
393  * \ingroup gdcmHeader
394  * \brief   Build the Pixel Type of the image.
395  *          Possible values are:
396  *          - 8U  unsigned  8 bit,
397  *          - 8S    signed  8 bit,
398  *          - 16U unsigned 16 bit,
399  *          - 16S   signed 16 bit,
400  *          - 32U unsigned 32 bit,
401  *          - 32S   signed 32 bit,
402  *          - FD floating double 64 bits (Not kosher DICOM, but so usefull!)
403  * \warning 12 bit images appear as 16 bit.
404  * \        24 bit images appear as 8 bit
405  * @return  0S if nothing found. NOT USABLE file. The caller has to check
406  */
407 std::string gdcmHeader::GetPixelType(void) { 
408    std::string BitsAlloc = GetEntryByNumber(0x0028, 0x0100); // Bits Allocated
409    if (BitsAlloc == GDCM_UNFOUND) {
410       dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Bits Allocated");
411       BitsAlloc = std::string("16");
412    }
413    if (BitsAlloc == "64")            // )
414       return ("FD");
415    if (BitsAlloc == "12")            // It will be unpacked
416       BitsAlloc = std::string("16");
417    else if (BitsAlloc == "24")       // (in order no to be messed up
418       BitsAlloc = std::string("8");  // by old RGB images)
419      
420    std::string Signed = GetEntryByNumber(0x0028, 0x0103); // "Pixel Representation"
421    if (Signed == GDCM_UNFOUND) {
422       dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Pixel Representation");
423       BitsAlloc = std::string("0");
424    }
425    if (Signed == "0")
426       Signed = std::string("U");
427    else
428       Signed = std::string("S");
429
430    return( BitsAlloc + Signed);
431 }
432
433
434 /**
435  * \ingroup gdcmHeader
436  * \brief   Recover the offset (from the beginning of the file) 
437  * \        of *image* pixels (not *icone image* pixels, if any !)
438  * @return Pixel Offset
439  */
440 size_t gdcmHeader::GetPixelOffset(void) { 
441    //
442    // If the element (0x0088,0x0200) 'icone image sequence' is found
443    // (grPixel,numPixel) is stored twice : the first one for the icon
444    // the second one for the image ...
445    // pb : sometimes , (0x0088,0x0200) exists, but doesn't contain *anything*
446    // see gdcmData/MxTwinLossLess.dcm ...
447
448    //std::string icone = GetEntryByNumber(0x0088,0x0200); //icone image sequence
449       
450    IterHT it = GetHeaderEntrySameNumber(GrPixel,NumPixel);          
451    TagKey key = gdcmDictEntry::TranslateToKey(GrPixel,NumPixel);
452    gdcmHeaderEntry* PixelElement;
453    if (countGrPixel == 1)   
454       PixelElement = (it.first)->second;
455    else {
456       PixelElement = (++it.first)->second; // hope there are no more than 2 !
457    } 
458    if (PixelElement) {
459       return PixelElement->GetOffset();
460    } else {
461       return 0;
462       std::cout << "Big trouble : Pixel Element ("
463                 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found"
464                 << std::endl;  
465    }     
466 }
467 // TODO : unify those two (previous one and next one)
468 /**
469  * \ingroup gdcmHeader
470  * \brief   Recover the pixel area length (in Bytes)
471  * @return Pixel Element Length, as stored in the header
472  *         (NOT the memory space necessary to hold the Pixels 
473  *          - in case of embeded compressed image-)
474  *         0 : NOT USABLE file. The caller has to check.
475  */
476 size_t gdcmHeader::GetPixelAreaLength(void) { 
477           
478    IterHT it = GetHeaderEntrySameNumber(GrPixel,NumPixel);          
479    TagKey key = gdcmDictEntry::TranslateToKey(GrPixel,NumPixel);
480    gdcmHeaderEntry* PixelElement;
481   
482   if (countGrPixel==1)  
483       PixelElement = (it.first)->second;
484    else
485       PixelElement = (++it.first)->second;
486
487    if (PixelElement) {
488       return PixelElement->GetLength();
489    } else {
490       std::cout << "Big trouble : Pixel Element ("
491                 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found"
492                 << std::endl;
493       return 0;
494    }
495 }
496
497 /**
498   * \ingroup gdcmHeader
499   * \brief tells us if LUT are used
500   * \warning Right now, 'Segmented xxx Palette Color Lookup Table Data'
501   * \        are NOT considered as LUT, since nobody knows
502   * \        how to deal with them
503   * \        Please warn me if you know sbdy that *does* know ... jprx
504   * @return true if LUT Descriptors and LUT Tables were found 
505   */
506 bool gdcmHeader::HasLUT(void) {
507
508    // Check the presence of the LUT Descriptors, and LUT Tables    
509    // LutDescriptorRed    
510    if ( !GetHeaderEntryByNumber(0x0028,0x1101) )
511       return false;
512    // LutDescriptorGreen 
513    if ( !GetHeaderEntryByNumber(0x0028,0x1102) )
514       return false;
515    // LutDescriptorBlue 
516    if ( !GetHeaderEntryByNumber(0x0028,0x1103) )
517       return false;   
518    // Red Palette Color Lookup Table Data
519    if ( !GetHeaderEntryByNumber(0x0028,0x1201) )
520       return false; 
521    // Green Palette Color Lookup Table Data       
522    if ( !GetHeaderEntryByNumber(0x0028,0x1202) )
523       return false;
524    // Blue Palette Color Lookup Table Data      
525    if ( !GetHeaderEntryByNumber(0x0028,0x1203) )
526       return false;   
527    return true;
528 }
529
530 /**
531   * \ingroup gdcmHeader
532   * \brief gets the info from 0028,1101 : Lookup Table Desc-Red
533   * \           else 0
534   * @return Lookup Table number of Bits , 0 by default
535   * \       when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
536   * @ return bit number of each LUT item 
537   */
538 int gdcmHeader::GetLUTNbits(void) {
539    std::vector<std::string> tokens;
540    //int LutLength;
541    //int LutDepth;
542    int LutNbits;
543    //Just hope Lookup Table Desc-Red = Lookup Table Desc-Red = Lookup Table Desc-Blue
544    // Consistency already checked in GetLUTLength
545    std::string LutDescription = GetEntryByNumber(0x0028,0x1101);
546    if (LutDescription == GDCM_UNFOUND)
547       return 0;
548    tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
549    Tokenize (LutDescription, tokens, "\\");
550    //LutLength=atoi(tokens[0].c_str());
551    //LutDepth=atoi(tokens[1].c_str());
552    LutNbits=atoi(tokens[2].c_str());
553    tokens.clear();
554    return LutNbits;
555 }
556
557 /**
558   * \ingroup gdcmHeader
559   * \brief builts Red/Green/Blue/Alpha LUT from Header
560   * \       when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
561   * \        and (0028,1101),(0028,1102),(0028,1102)  
562   * \          - xxx Palette Color Lookup Table Descriptor - are found
563   * \        and (0028,1201),(0028,1202),(0028,1202) 
564   * \          - xxx Palette Color Lookup Table Data - are found 
565   * \warning does NOT deal with :
566   * \ 0028 1100 Gray Lookup Table Descriptor (Retired)
567   * \ 0028 1221 Segmented Red Palette Color Lookup Table Data
568   * \ 0028 1222 Segmented Green Palette Color Lookup Table Data
569   * \ 0028 1223 Segmented Blue Palette Color Lookup Table Data 
570   * \ no known Dicom reader deals with them :-(
571   * @return a RGBA Lookup Table 
572   */ 
573 unsigned char * gdcmHeader::GetLUTRGBA(void) {
574 // Not so easy : see 
575 // http://www.barre.nom.fr/medical/dicom2/limitations.html#Color%20Lookup%20Tables
576
577 //  if Photometric Interpretation # PALETTE COLOR, no LUT to be done
578    if (GetEntryByNumber(0x0028,0x0004) != "PALETTE COLOR ") {
579         return NULL;
580    }  
581    int lengthR, debR, nbitsR;
582    int lengthG, debG, nbitsG;
583    int lengthB, debB, nbitsB;
584    
585 // Get info from Lut Descriptors
586 // (the 3 LUT descriptors may be different)    
587    std::string LutDescriptionR = GetEntryByNumber(0x0028,0x1101);
588    if (LutDescriptionR == GDCM_UNFOUND)
589       return NULL;
590    std::string LutDescriptionG = GetEntryByNumber(0x0028,0x1102);
591    if (LutDescriptionG == GDCM_UNFOUND)
592       return NULL;   
593    std::string LutDescriptionB = GetEntryByNumber(0x0028,0x1103);
594    if (LutDescriptionB == GDCM_UNFOUND)
595       return NULL;
596       
597    std::vector<std::string> tokens;
598       
599    tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
600    Tokenize (LutDescriptionR, tokens, "\\");
601    lengthR=atoi(tokens[0].c_str()); // Red LUT length in Bytes
602    debR   =atoi(tokens[1].c_str()); // subscript of the first Lut Value
603    nbitsR =atoi(tokens[2].c_str()); // Lut item size (in Bits)
604    tokens.clear();
605    
606    tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
607    Tokenize (LutDescriptionG, tokens, "\\");
608    lengthG=atoi(tokens[0].c_str()); // Green LUT length in Bytes
609    debG   =atoi(tokens[1].c_str()); // subscript of the first Lut Value
610    nbitsG =atoi(tokens[2].c_str()); // Lut item size (in Bits)
611    tokens.clear();  
612    
613    tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
614    Tokenize (LutDescriptionB, tokens, "\\");
615    lengthB=atoi(tokens[0].c_str()); // Blue LUT length in Bytes
616    debB   =atoi(tokens[1].c_str()); // subscript of the first Lut Value
617    nbitsB =atoi(tokens[2].c_str()); // Lut item size (in Bits)
618    tokens.clear();
619  
620    // Load LUTs into memory, (as they were stored on disk)
621    unsigned char *lutR = (unsigned char *)
622                          GetEntryVoidAreaByNumber(0x0028,0x1201);
623    unsigned char *lutG = (unsigned char *)
624                          GetEntryVoidAreaByNumber(0x0028,0x1202);
625    unsigned char *lutB = (unsigned char *)
626                          GetEntryVoidAreaByNumber(0x0028,0x1203); 
627    
628    if (!lutR || !lutG || !lutB ) {
629         return NULL;
630    } 
631    // forge the 4 * 8 Bits Red/Green/Blue/Alpha LUT 
632    
633    unsigned char *LUTRGBA = (unsigned char *)calloc(1024,1); // 256 * 4 (R, G, B, Alpha) 
634    if (!LUTRGBA) {
635       return NULL;
636    }
637    memset(LUTRGBA, 0, 1024);
638         // Bits Allocated
639    int nb;
640    std::string str_nb = GetEntryByNumber(0x0028,0x0100);
641    if (str_nb == GDCM_UNFOUND ) {
642       nb = 16;
643    } else {
644       nb = atoi(str_nb.c_str() );
645    }  
646    int mult;
647
648    if (nbitsR==16 && nb==8) // when LUT item size is different than pixel size
649       mult=2;               // high byte must be = low byte 
650    else                     // See PS 3.3-2003 C.11.1.1.2 p 619
651       mult=1; 
652  
653    // if we get a black image, let's just remove the '+1'
654    // from 'i*mult+1' and check again 
655    // if it works, we shall have to check the 3 Palettes
656    // to see which byte is ==0 (first one, or second one)
657    // and fix the code
658    // We give up the checking to avoid some (useless ?)overhead 
659    // (optimistic asumption)
660    unsigned char *a;      
661    int i;
662
663    a = LUTRGBA+0;
664    for(i=0;i<lengthR;i++) {
665       *a = lutR[i*mult+1]; 
666       a+=4;       
667    }        
668    a = LUTRGBA+1;
669    for(i=0;i<lengthG;i++) {
670       *a = lutG[i*mult+1]; 
671       a+=4;       
672    }  
673    a = LUTRGBA+2;
674    for(i=0;i<lengthB;i++) {
675       *a = lutB[i*mult+1]; 
676       a+=4;       
677    }  
678    a = LUTRGBA+3;
679    for(i=0;i<256;i++) {
680       *a = 1; // Alpha component
681       a+=4; 
682    } 
683    
684    //How to free the now useless LUTs?
685    //free(LutR); free(LutB); free(LutG); // Seg Fault when used
686    return(LUTRGBA);   
687
688
689 /**
690  * \ingroup gdcmHeader
691  * \brief gets the info from 0002,0010 : Transfert Syntax
692  * \      else 1.
693  * @return Transfert Syntax Name (as oposite to Transfert Syntax UID)
694  */
695 std::string gdcmHeader::GetTransfertSyntaxName(void) { 
696    // use the gdcmTS (TS : Transfert Syntax)
697    std::string TransfertSyntax = GetEntryByNumber(0x0002,0x0010);
698    if (TransfertSyntax == GDCM_UNFOUND) {
699       dbg.Verbose(0, "gdcmHeader::GetTransfertSyntaxName: unfound Transfert Syntax (0002,0010)");
700       return "Uncompressed ACR-NEMA";
701    }
702    // we do it only when we need it
703    gdcmTS * ts = gdcmGlobal::GetTS();
704    std::string tsName=ts->GetValue(TransfertSyntax);
705    //delete ts; // Seg Fault when deleted ?!
706    return tsName;
707 }
708
709 /**
710  * \ingroup   gdcmHeader
711  * \brief Sets the Pixel Area size in the Header
712  *        --> not-for-rats function
713  * @param ImageDataSize new Pixel Area Size
714  *        warning : nothing else is checked
715  */
716 void gdcmHeader::SetImageDataSize(size_t ImageDataSize) {
717    std::string content1;
718    char car[20];
719         
720    // Assumes HeaderEntry (GrPixel, NumPixel) is unique ...   
721    // TODO deal with multiplicity (see gdcmData/icone.dcm)      
722    sprintf(car,"%d",ImageDataSize);
723  
724    gdcmHeaderEntry *a = GetHeaderEntryByNumber(GrPixel, NumPixel);
725    a->SetLength(ImageDataSize);
726                 
727    ImageDataSize+=8;
728    sprintf(car,"%d",ImageDataSize);
729    content1=car;        
730    SetEntryByNumber(content1, GrPixel, NumPixel);
731 }
732
733
734 /**
735  * \ingroup   gdcmHeader
736  * \brief compares 2 Headers, according to DICOMDIR rules
737  *        --> not-for-rats function
738  * \warning does NOT work with ACR-NEMA files
739  * \todo find a trick to solve the pb (use RET fields ?)
740  * @param header 
741  * @return true if 'smaller'
742  */
743  bool gdcmHeader::operator<(gdcmHeader &header){
744    std::string s1,s2;
745
746    // Patient Name
747    s1=this->GetEntryByNumber(0x0010,0x0010);
748    s2=header.GetEntryByNumber(0x0010,0x0010);
749    if(s1 < s2)
750            return(true);
751    else if(s1 > s2)
752            return(false);
753    else
754    {
755       // Patient ID
756       s1=this->GetEntryByNumber(0x0010,0x0020);
757       s2=header.GetEntryByNumber(0x0010,0x0020);
758       if (s1 < s2)
759               return(true);
760       else if (s1 > s2)
761          return(1);
762       else
763       {
764               // Study Instance UID
765          s1=this->GetEntryByNumber(0x0020,0x000d);
766          s2=header.GetEntryByNumber(0x0020,0x000d);
767          if (s1 < s2)
768                  return(true);
769          else if(s1 > s2)
770                  return(false);
771          else
772          {
773                  // Serie Instance UID          
774             s1=this->GetEntryByNumber(0x0020,0x000e);
775             s2=header.GetEntryByNumber(0x0020,0x000e);
776             if (s1 < s2)
777                return(true);
778             else if(s1 > s2)
779                return(false);
780          }
781       }
782    }
783    return(false);
784 }
785
786 //-----------------------------------------------------------------------------
787 // Protected
788
789 //-----------------------------------------------------------------------------
790 // Private
791
792 //-----------------------------------------------------------------------------