]> Creatis software - gdcm.git/blob - src/gdcmHeader.cxx
upgrade GdcmHeaderEntry Print Method for DICOMDIR
[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 // Refer to gdcmHeader::CheckSwap()
14 //const unsigned int gdcmHeader::HEADER_LENGTH_TO_READ = 256;
15
16 // Refer to gdcmHeader::SetMaxSizeLoadEntry()
17 //const unsigned int gdcmHeader::MAX_SIZE_LOAD_ELEMENT_VALUE = 4096;
18
19 //-----------------------------------------------------------------------------
20 // Constructor / Destructor
21 /**
22  * \ingroup gdcmHeader
23  * \brief   
24  * @param   InFilename
25  * @param   exception_on_error
26  * @param   enable_sequences = true to allow the header 
27  *          to be parsed *inside* the SeQuences, 
28  *          when they have an actual length 
29  * @param   ignore_shadow = true if user wants to skip shadow groups 
30             during parsing, to save memory space
31  *\TODO : may be we need one more bool, 
32  *         to allow skipping the private elements while parsing the header
33  *         in order to save space         
34  */
35 gdcmHeader::gdcmHeader(const char *InFilename, 
36                        bool exception_on_error,
37                        bool enable_sequences, 
38                        bool ignore_shadow):
39    gdcmParser(InFilename,exception_on_error,enable_sequences,ignore_shadow)
40 {
41 }
42
43 /**
44  * \ingroup gdcmHeader
45  * \brief   
46  * @param   exception_on_error
47  */
48 gdcmHeader::gdcmHeader(bool exception_on_error) :
49    gdcmParser(exception_on_error)
50 {
51 }
52
53 /**
54  * \ingroup gdcmHeader
55  * \brief   Canonical destructor.
56  */
57 gdcmHeader::~gdcmHeader (void) {
58 }
59
60 //-----------------------------------------------------------------------------
61 // Print
62
63 //-----------------------------------------------------------------------------
64 // Public
65 /**
66  * \ingroup gdcmHeader
67  * \brief  This predicate, based on hopefully reasonable heuristics,
68  *         decides whether or not the current gdcmParser was properly parsed
69  *         and contains the mandatory information for being considered as
70  *         a well formed and usable Dicom/Acr File.
71  * @return true when gdcmParser is the one of a reasonable Dicom/Acr file,
72  *         false otherwise. 
73  */
74 bool gdcmHeader::IsReadable(void) {
75    if(!gdcmParser::IsReadable())
76       return(false);
77
78    std::string res = GetEntryByNumber(0x0028, 0x0005);
79    if ( res != GDCM_UNFOUND && atoi(res.c_str()) > 4 ) 
80       return false; // Image Dimensions
81    if ( !GetHeaderEntryByNumber(0x0028, 0x0100) )
82       return false; // "Bits Allocated"
83    if ( !GetHeaderEntryByNumber(0x0028, 0x0101) )
84       return false; // "Bits Stored"
85    if ( !GetHeaderEntryByNumber(0x0028, 0x0102) )
86       return false; // "High Bit"
87    if ( !GetHeaderEntryByNumber(0x0028, 0x0103) )
88       return false; // "Pixel Representation"
89    return true;
90 }
91
92 /**
93  * \ingroup gdcmHeader
94  * \brief   Determines if the Transfer Syntax was already encountered
95  *          and if it corresponds to a JPEGBaseLineProcess1 one.
96  *
97  * @return  True when JPEGBaseLineProcess1found. False in all other cases.
98  */
99 bool gdcmHeader::IsJPEGBaseLineProcess1TransferSyntax(void) {
100    gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
101    if ( !Element )
102       return false;
103    LoadHeaderEntrySafe(Element);
104
105    std::string Transfer = Element->GetValue();
106    if ( Transfer == "1.2.840.10008.1.2.4.50" )
107       return true;
108    return false;
109 }
110
111 /**
112  * \ingroup gdcmHeader
113  * \brief   Determines if the Transfer Syntax was already encountered
114  *          and if it corresponds to a JPEGExtendedProcess2-4 one.
115  *
116  * @return  True when JPEGExtendedProcess2-4 found. False in all other cases.
117  */
118 bool gdcmHeader::IsJPEGExtendedProcess2_4TransferSyntax(void) {
119    gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
120    if ( !Element )
121       return false;
122    LoadHeaderEntrySafe(Element);
123    return ( Element->GetValue() == "1.2.840.10008.1.2.4.51" );
124 }
125
126 /**
127  * \ingroup gdcmHeader
128  * \brief   Determines if the Transfer Syntax was already encountered
129  *          and if it corresponds to a JPEGExtendeProcess3-5 one.
130  *
131  * @return  True when JPEGExtendedProcess3-5 found. False in all other cases.
132  */
133 bool gdcmHeader::IsJPEGExtendedProcess3_5TransferSyntax(void) {
134    gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
135    if ( !Element )
136       return false;
137    LoadHeaderEntrySafe(Element);
138
139    std::string Transfer = Element->GetValue();
140    if ( Transfer == "1.2.840.10008.1.2.4.52" )
141       return true;
142    return false;
143 }
144
145 /**
146  * \ingroup gdcmHeader
147  * \brief   Determines if the Transfer Syntax was already encountered
148  *          and if it corresponds to a JPEGSpectralSelectionProcess6-8 one.
149  *
150  * @return  True when JPEGSpectralSelectionProcess6-8 found. False in all
151  *          other cases.
152  */
153 bool gdcmHeader::IsJPEGSpectralSelectionProcess6_8TransferSyntax(void) {
154    gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
155    if ( !Element )
156       return false;
157    LoadHeaderEntrySafe(Element);
158
159    std::string Transfer = Element->GetValue();
160    if ( Transfer == "1.2.840.10008.1.2.4.53" )
161       return true;
162    return false;
163 }
164
165 /**
166  * \ingroup gdcmHeader
167  * \brief   Determines if the Transfer Syntax was already encountered
168  *          and if it corresponds to a RLE Lossless one.
169  *
170  * @return  True when RLE Lossless found. False in all
171  *          other cases.
172  */
173 bool gdcmHeader::IsRLELossLessTransferSyntax(void) {
174    gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
175    if ( !Element )
176       return false;
177    LoadHeaderEntrySafe(Element);
178
179    std::string Transfer = Element->GetValue();
180    if ( Transfer == "1.2.840.10008.1.2.5" ) {
181       return true;
182     }
183    return false;
184 }
185
186 /**
187  * \ingroup gdcmHeader
188  * \brief  Determines if Transfer Syntax was already encountered
189  *          and if it corresponds to a JPEG Lossless one. 
190  *
191  * @return  True when RLE Lossless found. False in all
192  *          other cases. 
193  */
194 bool gdcmHeader::IsJPEGLossless(void) {
195    gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
196     // faire qq chose d'intelligent a la place de ça
197    if ( !Element )
198       return false;
199    LoadHeaderEntrySafe(Element);
200
201    const char * Transfert = Element->GetValue().c_str();
202    if ( memcmp(Transfert+strlen(Transfert)-2 ,"70",2)==0) return true;
203    if ( memcmp(Transfert+strlen(Transfert)-2 ,"55",2)==0) return true;
204    if (Element->GetValue() == "1.2.840.10008.1.2.4.57")   return true;
205
206    return false;
207 }
208
209 /**
210  * \ingroup gdcmHeader
211  * \brief   Determines if the Transfer Syntax was already encountered
212  *          and if it corresponds to a JPEG200 one.0
213  *
214  * @return  True when JPEG2000 (Lossly or LossLess) found. False in all
215  *          other cases.
216  */
217 bool gdcmHeader::IsJPEG2000(void) {
218    gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
219    if ( !Element )
220       return false;
221    LoadHeaderEntrySafe(Element);
222
223    std::string Transfer = Element->GetValue();
224    if (    (Transfer == "1.2.840.10008.1.2.4.90") 
225         || (Transfer == "1.2.840.10008.1.2.4.91") )
226       return true;
227    return false;
228 }
229
230 /**
231  * \ingroup gdcmHeader
232  * \brief   Predicate for dicom version 3 file.
233  * @return  True when the file is a dicom version 3.
234  */
235 bool gdcmHeader::IsDicomV3(void) {
236    // Checking if Transfert Syntax exists is enough
237    return (GetHeaderEntryByNumber(0x0002, 0x0010) != NULL);
238 }
239
240 /**
241  * \ingroup gdcmHeader
242  * \brief   Retrieve the number of columns of image.
243  * @return  The encountered size when found, 0 by default.
244  *          0 means the file is NOT USABLE. The caller will have to check
245  */
246 int gdcmHeader::GetXSize(void) {
247    std::string StrSize;
248    StrSize = GetEntryByNumber(0x0028,0x0011);
249    if (StrSize == GDCM_UNFOUND)
250       return 0;
251    return atoi(StrSize.c_str());
252 }
253
254 /**
255  * \ingroup gdcmHeader
256  * \brief   Retrieve the number of lines of image.
257  * \warning The defaulted value is 1 as opposed to gdcmHeader::GetXSize()
258  * @return  The encountered size when found, 1 by default 
259  *          (The file contains a Signal, not an Image).
260  */
261 int gdcmHeader::GetYSize(void) {
262    std::string StrSize = GetEntryByNumber(0x0028,0x0010);
263    if (StrSize != GDCM_UNFOUND)
264       return atoi(StrSize.c_str());
265    if ( IsDicomV3() )
266       return 0;
267    else
268       // The Rows (0028,0010) entry was optional for ACR/NEMA. It might
269       // hence be a signal (1d image). So we default to 1:
270       return 1;
271 }
272
273 /**
274  * \ingroup gdcmHeader
275  * \brief   Retrieve the number of planes of volume or the number
276  *          of frames of a multiframe.
277  * \warning When present we consider the "Number of Frames" as the third
278  *          dimension. When absent we consider the third dimension as
279  *          being the "Planes" tag content.
280  * @return  The encountered size when found, 1 by default (single image).
281  */
282 int gdcmHeader::GetZSize(void) {
283    // Both  DicomV3 and ACR/Nema consider the "Number of Frames"
284    // as the third dimension.
285    std::string StrSize = GetEntryByNumber(0x0028,0x0008);
286    if (StrSize != GDCM_UNFOUND)
287       return atoi(StrSize.c_str());
288
289    // We then consider the "Planes" entry as the third dimension [we
290    // cannot retrieve by name since "Planes tag is present both in
291    // IMG (0028,0012) and OLY (6000,0012) sections of the dictionary]. 
292    StrSize = GetEntryByNumber(0x0028,0x0012);
293    if (StrSize != GDCM_UNFOUND)
294       return atoi(StrSize.c_str());
295    return 1;
296 }
297
298 /**
299  * \ingroup gdcmHeader
300  * \brief   Retrieve the number of Bits Stored (actually used)
301  *          (as opposite to number of Bits Allocated)
302  * 
303  * @return  The encountered number of Bits Stored, 0 by default.
304  *          0 means the file is NOT USABLE. The caller has to check it !
305  */
306 int gdcmHeader::GetBitsStored(void) { 
307    std::string StrSize = GetEntryByNumber(0x0028,0x0101);
308    if (StrSize == GDCM_UNFOUND)
309       return 0;  // It's supposed to be mandatory
310                  // the caller will have to check
311    return atoi(StrSize.c_str());
312 }
313
314 /**
315  * \ingroup gdcmHeader
316  * \brief   Retrieve the number of Bits Allocated
317  *          (8, 12 -compacted ACR-NEMA files, 16, ...)
318  * 
319  * @return  The encountered number of Bits Allocated, 0 by default.
320  *          0 means the file is NOT USABLE. The caller has to check it !
321  */
322 int gdcmHeader::GetBitsAllocated(void) { 
323    std::string StrSize = GetEntryByNumber(0x0028,0x0100);
324    if (StrSize == GDCM_UNFOUND)
325       return 0; // It's supposed to be mandatory
326                 // the caller will have to check
327    return atoi(StrSize.c_str());
328 }
329
330 /**
331  * \ingroup gdcmHeader
332  * \brief   Retrieve the number of Samples Per Pixel
333  *          (1 : gray level, 3 : RGB -1 or 3 Planes-)
334  * 
335  * @return  The encountered number of Samples Per Pixel, 1 by default.
336  *          (Gray level Pixels)
337  */
338 int gdcmHeader::GetSamplesPerPixel(void) { 
339    std::string StrSize = GetEntryByNumber(0x0028,0x0002);
340    if (StrSize == GDCM_UNFOUND)
341       return 1; // Well, it's supposed to be mandatory ...
342                 // but sometimes it's missing : we assume Gray pixels
343    return atoi(StrSize.c_str());
344 }
345
346 /**
347  * \ingroup gdcmHeader
348  * \brief   Retrieve the Planar Configuration for RGB images
349  *          (0 : RGB Pixels , 1 : R Plane + G Plane + B Plane)
350  * 
351  * @return  The encountered Planar Configuration, 0 by default.
352  */
353 int gdcmHeader::GetPlanarConfiguration(void) { 
354    std::string StrSize = GetEntryByNumber(0x0028,0x0006);
355    if (StrSize == GDCM_UNFOUND)
356       return 0;
357    return atoi(StrSize.c_str());
358 }
359
360 /**
361  * \ingroup gdcmHeader
362  * \brief   Return the size (in bytes) of a single pixel of data.
363  * @return  The size in bytes of a single pixel of data; 0 by default
364  *          0 means the file is NOT USABLE; the caller will have to check        
365  */
366 int gdcmHeader::GetPixelSize(void) {
367    std::string PixelType = GetPixelType();
368    if (PixelType == "8U"  || PixelType == "8S")
369       return 1;
370    if (PixelType == "16U" || PixelType == "16S")
371       return 2;
372    if (PixelType == "32U" || PixelType == "32S")
373       return 4;
374    dbg.Verbose(0, "gdcmHeader::GetPixelSize: Unknown pixel type");
375    return 0;
376 }
377
378 /**
379  * \ingroup gdcmHeader
380  * \brief   Build the Pixel Type of the image.
381  *          Possible values are:
382  *          - 8U  unsigned  8 bit,
383  *          - 8S    signed  8 bit,
384  *          - 16U unsigned 16 bit,
385  *          - 16S   signed 16 bit,
386  *          - 32U unsigned 32 bit,
387  *          - 32S   signed 32 bit,
388  * \warning 12 bit images appear as 16 bit.
389  * \        24 bit images appear as 8 bit
390  * @return  0S if nothing found. NOT USABLE file. The caller has to check
391  */
392 std::string gdcmHeader::GetPixelType(void) {
393    std::string BitsAlloc = GetEntryByNumber(0x0028, 0x0100); // Bits Allocated
394    if (BitsAlloc == GDCM_UNFOUND) {
395       dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Bits Allocated");
396       BitsAlloc = std::string("16");
397    }
398    if (BitsAlloc == "12")            // It will be unpacked
399       BitsAlloc = std::string("16");
400    else if (BitsAlloc == "24")       // (in order no to be messed up
401       BitsAlloc = std::string("8");  // by old RGB images)
402      
403    std::string Signed = GetEntryByNumber(0x0028, 0x0103); // "Pixel Representation"
404    if (Signed == GDCM_UNFOUND) {
405       dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Pixel Representation");
406       BitsAlloc = std::string("0");
407    }
408    if (Signed == "0")
409       Signed = std::string("U");
410    else
411       Signed = std::string("S");
412
413    return( BitsAlloc + Signed);
414 }
415
416 /**
417  * \ingroup gdcmHeader
418  * \brief   Recover the offset (from the beginning of the file) 
419  * \        of *image* pixels (not *icone image* pixels, if any !)
420  */
421 size_t gdcmHeader::GetPixelOffset(void) {
422    // We may encounter the 'RETired' (0x0028, 0x0200) tag
423    // (Image Location") . This Element contains the number of
424    // the group that contains the pixel data (hence the "Pixel Data"
425    // is found by indirection through the "Image Location").
426    // Inside the group pointed by "Image Location" the searched element
427    // is conventionally the element 0x0010 (when the norm is respected).
428    // When the "Image Location" is absent we default to group 0x7fe0.
429    //
430    // If the element (0x0088,0x0200) 'icone image sequence' is found
431    // (grPixel,numPixel) is stored twice : the first one for the icon
432    // the second one for the image ...
433    // pb : sometimes , (0x0088,0x0200) exists, but doesn't contain *anything*
434    // see gdcmData/MxTwinLossLess.dcm ...
435    guint16 grPixel;
436    guint16 numPixel;
437    std::string ImageLocation = GetEntryByNumber(0x0028, 0x0200);
438
439    if ( ImageLocation == GDCM_UNFOUND ) { // Image Location
440       grPixel = 0x7fe0;                   // default value
441    } else {
442       grPixel = (guint16) atoi( ImageLocation.c_str() );
443    }
444    
445    if (grPixel == 0xe07f) // sometimes Image Location value doesn't follow 
446       grPixel = 0x7fe0;   // the supposed processor endianity. 
447                           // see gdcmData/cr172241.dcm
448       
449    if (grPixel != 0x7fe0) 
450       // This is a kludge for old dirty Philips imager.
451       numPixel = 0x1010;
452    else
453       numPixel = 0x0010;
454       
455    IterHT it = GetHeaderEntrySameNumber(grPixel,numPixel);          
456    //std::string icone = GetEntryByNumber(0x0088,0x0200); //icone image sequence
457    TagKey key = gdcmDictEntry::TranslateToKey(grPixel,numPixel);
458    gdcmHeaderEntry* PixelElement;
459   
460    if (tagHT.count(key) == 1)   
461       PixelElement = (it.first)->second;
462    else
463       PixelElement = (++it.first)->second;
464    
465    if (PixelElement) {
466       return PixelElement->GetOffset();
467    }
468    else
469       return 0;
470 }
471 // TODO : unify those two (previous one and next one)
472 /**
473  * \ingroup gdcmHeader
474  * \brief   Recover the pixel area length (in Bytes)
475  *  @return 0 by default. NOT USABLE file. The caller has to check.
476  */
477 size_t gdcmHeader::GetPixelAreaLength(void) {
478    // If this file complies with the norm we should encounter the
479    // "Image Location" tag (0x0028,  0x0200). This tag contains the
480    // the group that contains the pixel data (hence the "Pixel Data"
481    // is found by indirection through the "Image Location").
482    // Inside the group pointed by "Image Location" the searched element
483    // is conventionally the element 0x0010 (when the norm is respected).
484    // When the "Image Location" is absent we default to group 0x7fe0.
485    guint16 grPixel;
486    guint16 numPixel;
487    std::string ImageLocation = GetEntryByNumber(0x0028, 0x0200);
488    if ( ImageLocation == GDCM_UNFOUND ) { // Image Location
489       grPixel = 0x7fe0;                   // default value
490    } else {
491       grPixel = (guint16) atoi( ImageLocation.c_str() );
492    }
493    if (grPixel == 0xe07f) // sometimes group doesn't follow 
494       grPixel = 0x7fe0;   // the supposed processor endianity. see cr172241.dcm
495       
496    if (grPixel != 0x7fe0)
497       // This is a kludge for old dirty Philips imager.
498       numPixel = 0x1010;
499    else
500       numPixel = 0x0010;
501               
502    IterHT it = GetHeaderEntrySameNumber(grPixel,numPixel);          
503    //std::string icone = GetEntryByNumber(0x0088,0x0200); //icone image sequence
504    TagKey key = gdcmDictEntry::TranslateToKey(grPixel,numPixel);
505    gdcmHeaderEntry* PixelElement;
506   
507    if (tagHT.count(key) == 1)   
508       PixelElement = (it.first)->second;
509    else
510       PixelElement = (++it.first)->second;
511    
512    if (PixelElement)
513       return PixelElement->GetLength();
514    else {
515       std::cout << "Big trouble : Pixel Element ("
516                 << std::hex << grPixel<<","<< numPixel<< ") NOT found" 
517                 << std::endl;
518       return 0;
519    }
520 }
521
522 /**
523   * \ingroup gdcmHeader
524   * \brief tells us if LUT are used
525   * \warning Right now, 'Segmented xxx Palette Color Lookup Table Data'
526   * \        are NOT considered as LUT, since nobody knows
527   * \        how to deal with them
528   * @return a Boolean 
529   */
530 bool gdcmHeader::HasLUT(void) {
531
532    // Check the presence of the LUT Descriptors 
533    
534    // LutDescriptorRed    
535    if ( !GetHeaderEntryByNumber(0x0028,0x1101) )
536       return false;
537    // LutDescriptorGreen 
538    if ( !GetHeaderEntryByNumber(0x0028,0x1102) )
539       return false;
540    // LutDescriptorBlue 
541    if ( !GetHeaderEntryByNumber(0x0028,0x1103) )
542       return false;
543       
544    //  It is not enough :
545    //  we check also 
546    
547    // Red Palette Color Lookup Table Data
548    if ( !GetHeaderEntryByNumber(0x0028,0x1201) )
549       return false; 
550    // Green Palette Color Lookup Table Data       
551    if ( !GetHeaderEntryByNumber(0x0028,0x1202) )
552       return false;
553    // Blue Palette Color Lookup Table Data      
554    if ( !GetHeaderEntryByNumber(0x0028,0x1203) )
555       return false;   
556    return true;
557 }
558
559 /**
560   * \ingroup gdcmHeader
561   * \brief gets the info from 0028,1101 : Lookup Table Desc-Red
562   * \           else 0
563   * @return Lookup Table number of Bits , 0 by default
564   * \       when (0028,0004),Photometric Interpretation = [PALETTE COLOR ] 
565   */
566 int gdcmHeader::GetLUTNbits(void) {
567    std::vector<std::string> tokens;
568    //int LutLength;
569    //int LutDepth;
570    int LutNbits;
571    //Just hope Lookup Table Desc-Red = Lookup Table Desc-Red = Lookup Table Desc-Blue
572    // Consistency already checked in GetLUTLength
573    std::string LutDescription = GetEntryByNumber(0x0028,0x1101);
574    if (LutDescription == GDCM_UNFOUND)
575       return 0;
576    tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
577    Tokenize (LutDescription, tokens, "\\");
578    //LutLength=atoi(tokens[0].c_str());
579    //LutDepth=atoi(tokens[1].c_str());
580    LutNbits=atoi(tokens[2].c_str());
581    tokens.clear();
582    return LutNbits;
583 }
584
585 /**
586   * \ingroup gdcmHeader
587   * \brief builts Red/Green/Blue/Alpha LUT from Header
588   * \       when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
589   * \        and (0028,1101),(0028,1102),(0028,1102)  
590   * \          - xxx Palette Color Lookup Table Descriptor - are found
591   * \        and (0028,1201),(0028,1202),(0028,1202) 
592   * \          - xxx Palette Color Lookup Table Data - are found 
593   * \warning does NOT deal with :
594   * \ 0028 1100 Gray Lookup Table Descriptor (Retired)
595   * \ 0028 1221 Segmented Red Palette Color Lookup Table Data
596   * \ 0028 1222 Segmented Green Palette Color Lookup Table Data
597   * \ 0028 1223 Segmented Blue Palette Color Lookup Table Data 
598   * \ no known Dicom reader deals with them :-(
599   * @return a RGBA Lookup Table 
600   */ 
601 unsigned char * gdcmHeader::GetLUTRGBA(void) {
602 // Not so easy : see 
603 // http://www.barre.nom.fr/medical/dicom2/limitations.html#Color%20Lookup%20Tables
604
605 //  if Photometric Interpretation # PALETTE COLOR, no LUT to be done
606    if (GetEntryByNumber(0x0028,0x0004) != "PALETTE COLOR ") {
607         return NULL;
608    }  
609    int lengthR, debR, nbitsR;
610    int lengthG, debG, nbitsG;
611    int lengthB, debB, nbitsB;
612    
613 // Get info from Lut Descriptors
614 // (the 3 LUT descriptors may be different)    
615    std::string LutDescriptionR = GetEntryByNumber(0x0028,0x1101);
616    if (LutDescriptionR == GDCM_UNFOUND)
617       return NULL;
618    std::string LutDescriptionG = GetEntryByNumber(0x0028,0x1102);
619    if (LutDescriptionG == GDCM_UNFOUND)
620       return NULL;   
621    std::string LutDescriptionB = GetEntryByNumber(0x0028,0x1103);
622    if (LutDescriptionB == GDCM_UNFOUND)
623       return NULL;
624       
625    std::vector<std::string> tokens;
626       
627    tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
628    Tokenize (LutDescriptionR, tokens, "\\");
629    lengthR=atoi(tokens[0].c_str()); // Red LUT length in Bytes
630    debR   =atoi(tokens[1].c_str()); // subscript of the first Lut Value
631    nbitsR =atoi(tokens[2].c_str()); // Lut item size (in Bits)
632    tokens.clear();
633    
634    tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
635    Tokenize (LutDescriptionG, tokens, "\\");
636    lengthG=atoi(tokens[0].c_str()); // Green LUT length in Bytes
637    debG   =atoi(tokens[1].c_str()); // subscript of the first Lut Value
638    nbitsG =atoi(tokens[2].c_str()); // Lut item size (in Bits)
639    tokens.clear();  
640    
641    tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
642    Tokenize (LutDescriptionB, tokens, "\\");
643    lengthB=atoi(tokens[0].c_str()); // Blue LUT length in Bytes
644    debB   =atoi(tokens[1].c_str()); // subscript of the first Lut Value
645    nbitsB =atoi(tokens[2].c_str()); // Lut item size (in Bits)
646    tokens.clear();
647  
648    // Load LUTs into memory, (as they were stored on disk)
649    unsigned char *lutR = (unsigned char *)
650                          GetEntryVoidAreaByNumber(0x0028,0x1201);
651    unsigned char *lutG = (unsigned char *)
652                          GetEntryVoidAreaByNumber(0x0028,0x1202);
653    unsigned char *lutB = (unsigned char *)
654                          GetEntryVoidAreaByNumber(0x0028,0x1203); 
655    
656    if (!lutR || !lutG || !lutB ) {
657         return NULL;
658    } 
659    // forge the 4 * 8 Bits Red/Green/Blue/Alpha LUT 
660    
661    unsigned char *LUTRGBA = (unsigned char *)calloc(1024,1); // 256 * 4 (R, G, B, Alpha) 
662    if (!LUTRGBA) {
663       return NULL;
664    }
665    memset(LUTRGBA, 0, 1024);
666         // Bits Allocated
667    int nb;
668    std::string str_nb = GetEntryByNumber(0x0028,0x0100);
669    if (str_nb == GDCM_UNFOUND ) {
670       nb = 16;
671    } else {
672       nb = atoi(str_nb.c_str() );
673    }  
674    int mult;
675
676    if (nbitsR==16 && nb==8) // when LUT item size is different than pixel size
677       mult=2;               // high byte must be = low byte 
678    else                     // See PS 3.3-2003 C.11.1.1.2 p 619
679       mult=1; 
680  
681    // if we get a black image, let's just remove the '+1'
682    // from 'i*mult+1' and check again 
683    // if it works, we shall have to check the 3 Palettes
684    // to see which byte is ==0 (first one, or second one)
685    // and fix the code
686    // We give up the checking to avoid some (useless ?)overhead 
687    // (optimistic asumption)
688    unsigned char *a;      
689    int i;
690
691    a = LUTRGBA+0;
692    for(i=0;i<lengthR;i++) {
693       *a = lutR[i*mult+1]; 
694       a+=4;       
695    }        
696    a = LUTRGBA+1;
697    for(i=0;i<lengthG;i++) {
698       *a = lutG[i*mult+1]; 
699       a+=4;       
700    }  
701    a = LUTRGBA+2;
702    for(i=0;i<lengthB;i++) {
703       *a = lutB[i*mult+1]; 
704       a+=4;       
705    }  
706    a = LUTRGBA+3;
707    for(i=0;i<256;i++) {
708       *a = 1; // Alpha component
709       a+=4; 
710    } 
711    
712    //How to free the now useless LUTs?
713    //free(LutR); free(LutB); free(LutG); // Seg Fault when used
714    return(LUTRGBA);   
715
716
717 /**
718  * \ingroup gdcmHeader
719  * \brief gets the info from 0002,0010 : Transfert Syntax
720  * \      else 1.
721  * @return Transfert Syntax Name (as oposite to Transfert Syntax UID)
722  */
723 std::string gdcmHeader::GetTransfertSyntaxName(void) { 
724    // use the gdcmTS (TS : Transfert Syntax)
725    std::string TransfertSyntax = GetEntryByNumber(0x0002,0x0010);
726    if (TransfertSyntax == GDCM_UNFOUND) {
727       dbg.Verbose(0, "gdcmHeader::GetTransfertSyntaxName: unfound Transfert Syntax (0002,0010)");
728       return "Uncompressed ACR-NEMA";
729    }
730    // we do it only when we need it
731    gdcmTS * ts = gdcmGlobal::GetTS();
732    std::string tsName=ts->GetValue(TransfertSyntax);
733    //delete ts; // Seg Fault when deleted ?!
734    return tsName;
735 }
736
737 /**
738  * \ingroup   gdcmFile
739  * \brief Sets the Pixel Area size in the Header
740  *        --> not-for-rats function
741  * 
742  * \warning WARNING doit-etre etre publique ? 
743  * TODO : y aurait il un inconvenient à fusionner ces 2 fonctions
744  *
745  * @param ImageDataSize new Pixel Area Size
746  *        warning : nothing else is checked
747  */
748 void gdcmHeader::SetImageDataSize(size_t ImageDataSize) {
749    std::string content1;
750    char car[20];        
751    // Assumes HeaderEntry (0x7fe0, 0x0010) exists ...
752    // TODO define private members PixelGroupNumber, PicxelElementNumber
753    //      update them, use them (only necessary for ACR-NEMA, not DICOM)       
754    sprintf(car,"%d",ImageDataSize);
755  
756    gdcmHeaderEntry *a = GetHeaderEntryByNumber(0x7fe0, 0x0010);
757    a->SetLength(ImageDataSize);
758                 
759    ImageDataSize+=8;
760    sprintf(car,"%d",ImageDataSize);
761    content1=car;        
762    SetEntryByNumber(content1, 0x7fe0, 0x0000);
763 }
764
765 //-----------------------------------------------------------------------------
766 // Protected
767
768 //-----------------------------------------------------------------------------
769 // Private
770
771 //-----------------------------------------------------------------------------