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