1 /*=========================================================================
4 Module: $RCSfile: gdcmHeader.cxx,v $
6 Date: $Date: 2004/06/20 18:08:47 $
7 Version: $Revision: 1.165 $
9 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10 l'Image). All rights reserved. See Doc/License.txt or
11 http://www.creatis.insa-lyon.fr/Public/Gdcm/License.htm for details.
13 This software is distributed WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE. See the above copyright notices for more information.
17 =========================================================================*/
19 #include "gdcmHeader.h"
20 #include "gdcmGlobal.h"
22 #include "gdcmDebug.h"
24 #include "gdcmValEntry.h"
28 //-----------------------------------------------------------------------------
29 // Constructor / Destructor
32 * @param InFilename name of the file whose header we want to analyze
33 * @param exception_on_error whether we want to throw an exception or not
34 * @param enable_sequences = true to allow the header
35 * to be parsed *inside* the SeQuences, when they have an actual length
36 * @param ignore_shadow = true if user wants to skip shadow groups
37 * during parsing, to save memory space
39 gdcmHeader::gdcmHeader(const char *InFilename,
40 bool exception_on_error,
41 bool enable_sequences,
43 gdcmDocument(InFilename,exception_on_error,enable_sequences,ignore_shadow)
47 guint32 totalSQlength;
48 guint32 alreadyParsedlength;
52 // for some ACR-NEMA images GrPixel, NumPixel is *not* 7fe0,0010
53 // We may encounter the 'RETired' (0x0028, 0x0200) tag
54 // (Image Location") . This Element contains the number of
55 // the group that contains the pixel data (hence the "Pixel Data"
56 // is found by indirection through the "Image Location").
57 // Inside the group pointed by "Image Location" the searched element
58 // is conventionally the element 0x0010 (when the norm is respected).
59 // When the "Image Location" is absent we default to group 0x7fe0.
61 // This IS the right place for the code
63 std::string ImageLocation = GetEntryByNumber(0x0028, 0x0200);
64 if ( ImageLocation == GDCM_UNFOUND ) { // Image Location
65 GrPixel = 0x7fe0; // default value
67 GrPixel = (guint16) atoi( ImageLocation.c_str() );
69 if (GrPixel == 0xe07f) // sometimes Image Location value doesn't follow
70 GrPixel = 0x7fe0; // the supposed processor endianity.
71 // see gdcmData/cr172241.dcm
72 if (GrPixel != 0x7fe0)
73 // This is a kludge for old dirty Philips imager.
82 * @param exception_on_error whether we want to throw an exception or not
84 gdcmHeader::gdcmHeader(bool exception_on_error) :
85 gdcmDocument(exception_on_error)
91 * \brief Canonical destructor.
93 gdcmHeader::~gdcmHeader (void) {
96 //-----------------------------------------------------------------------------
100 //-----------------------------------------------------------------------------
104 * \brief This predicate, based on hopefully reasonable heuristics,
105 * decides whether or not the current gdcmHeader was properly parsed
106 * and contains the mandatory information for being considered as
107 * a well formed and usable Dicom/Acr File.
108 * @return true when gdcmHeader is the one of a reasonable Dicom/Acr file,
111 bool gdcmHeader::IsReadable(void) {
112 if(!gdcmDocument::IsReadable()) {
115 std::string res = GetEntryByNumber(0x0028, 0x0005);
116 if ( res != GDCM_UNFOUND && atoi(res.c_str()) > 4 )
117 return false; // Image Dimensions
118 if ( !GetDocEntryByNumber(0x0028, 0x0100) )
119 return false; // "Bits Allocated"
120 if ( !GetDocEntryByNumber(0x0028, 0x0101) )
121 return false; // "Bits Stored"
122 if ( !GetDocEntryByNumber(0x0028, 0x0102) )
123 return false; // "High Bit"
124 if ( !GetDocEntryByNumber(0x0028, 0x0103) )
125 return false; // "Pixel Representation" i.e. 'Sign'
130 * \brief Retrieve the number of columns of image.
131 * @return The encountered size when found, 0 by default.
132 * 0 means the file is NOT USABLE. The caller will have to check
134 int gdcmHeader::GetXSize(void) {
136 StrSize = GetEntryByNumber(0x0028,0x0011);
137 if (StrSize == GDCM_UNFOUND)
139 return atoi(StrSize.c_str());
143 * \ingroup gdcmHeader
144 * \brief Retrieve the number of lines of image.
145 * \warning The defaulted value is 1 as opposed to gdcmHeader::GetXSize()
146 * @return The encountered size when found, 1 by default
147 * (The ACR-MEMA file contains a Signal, not an Image).
149 int gdcmHeader::GetYSize(void) {
150 std::string StrSize = GetEntryByNumber(0x0028,0x0010);
151 if (StrSize != GDCM_UNFOUND)
152 return atoi(StrSize.c_str());
156 // The Rows (0028,0010) entry was optional for ACR/NEMA. It might
157 // hence be a signal (1d image). So we default to 1:
162 * \ingroup gdcmHeader
163 * \brief Retrieve the number of planes of volume or the number
164 * of frames of a multiframe.
165 * \warning When present we consider the "Number of Frames" as the third
166 * dimension. When absent we consider the third dimension as
167 * being the ACR-NEMA "Planes" tag content.
168 * @return The encountered size when found, 1 by default (single image).
170 int gdcmHeader::GetZSize(void) {
171 // Both DicomV3 and ACR/Nema consider the "Number of Frames"
172 // as the third dimension.
173 std::string StrSize = GetEntryByNumber(0x0028,0x0008);
174 if (StrSize != GDCM_UNFOUND)
175 return atoi(StrSize.c_str());
177 // We then consider the "Planes" entry as the third dimension
178 StrSize = GetEntryByNumber(0x0028,0x0012);
179 if (StrSize != GDCM_UNFOUND)
180 return atoi(StrSize.c_str());
185 * \ingroup gdcmHeader
186 * \brief Retrieve the number of Bits Stored (actually used)
187 * (as opposite to number of Bits Allocated)
188 * @return The encountered number of Bits Stored, 0 by default.
189 * 0 means the file is NOT USABLE. The caller has to check it !
191 int gdcmHeader::GetBitsStored(void) {
192 std::string StrSize = GetEntryByNumber(0x0028,0x0101);
193 if (StrSize == GDCM_UNFOUND)
194 return 0; // It's supposed to be mandatory
195 // the caller will have to check
196 return atoi(StrSize.c_str());
200 * \ingroup gdcmHeader
201 * \brief Retrieve the number of Bits Allocated
202 * (8, 12 -compacted ACR-NEMA files, 16, ...)
203 * @return The encountered number of Bits Allocated, 0 by default.
204 * 0 means the file is NOT USABLE. The caller has to check it !
206 int gdcmHeader::GetBitsAllocated(void) {
207 std::string StrSize = GetEntryByNumber(0x0028,0x0100);
208 if (StrSize == GDCM_UNFOUND)
209 return 0; // It's supposed to be mandatory
210 // the caller will have to check
211 return atoi(StrSize.c_str());
215 * \ingroup gdcmHeader
216 * \brief Retrieve the number of Samples Per Pixel
217 * (1 : gray level, 3 : RGB -1 or 3 Planes-)
218 * @return The encountered number of Samples Per Pixel, 1 by default.
219 * (Gray level Pixels)
221 int gdcmHeader::GetSamplesPerPixel(void) {
222 std::string StrSize = GetEntryByNumber(0x0028,0x0002);
223 if (StrSize == GDCM_UNFOUND)
224 return 1; // Well, it's supposed to be mandatory ...
225 // but sometimes it's missing : *we* assume Gray pixels
226 return atoi(StrSize.c_str());
230 * \ingroup gdcmHeader
231 * \brief Retrieve the Planar Configuration for RGB images
232 * (0 : RGB Pixels , 1 : R Plane + G Plane + B Plane)
233 * @return The encountered Planar Configuration, 0 by default.
235 int gdcmHeader::GetPlanarConfiguration(void) {
236 std::string StrSize = GetEntryByNumber(0x0028,0x0006);
237 if (StrSize == GDCM_UNFOUND)
239 return atoi(StrSize.c_str());
243 * \ingroup gdcmHeader
244 * \brief Return the size (in bytes) of a single pixel of data.
245 * @return The size in bytes of a single pixel of data; 0 by default
246 * 0 means the file is NOT USABLE; the caller will have to check
248 int gdcmHeader::GetPixelSize(void) {
249 std::string PixelType = GetPixelType();
250 if (PixelType == "8U" || PixelType == "8S")
252 if (PixelType == "16U" || PixelType == "16S")
254 if (PixelType == "32U" || PixelType == "32S")
256 if (PixelType == "FD")
258 dbg.Verbose(0, "gdcmHeader::GetPixelSize: Unknown pixel type");
263 * \ingroup gdcmHeader
264 * \brief Build the Pixel Type of the image.
265 * Possible values are:
266 * - 8U unsigned 8 bit,
268 * - 16U unsigned 16 bit,
269 * - 16S signed 16 bit,
270 * - 32U unsigned 32 bit,
271 * - 32S signed 32 bit,
272 * - FD floating double 64 bits (Not kosher DICOM, but so usefull!)
273 * \warning 12 bit images appear as 16 bit.
274 * 24 bit images appear as 8 bit
275 * @return 0S if nothing found. NOT USABLE file. The caller has to check
277 std::string gdcmHeader::GetPixelType(void) {
278 std::string BitsAlloc = GetEntryByNumber(0x0028, 0x0100); // Bits Allocated
279 if (BitsAlloc == GDCM_UNFOUND) {
280 dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Bits Allocated");
281 BitsAlloc = std::string("16");
283 if (BitsAlloc == "64") // )
285 if (BitsAlloc == "12") // It will be unpacked
286 BitsAlloc = std::string("16");
287 else if (BitsAlloc == "24") // (in order no to be messed up
288 BitsAlloc = std::string("8"); // by old RGB images)
290 std::string Signed = GetEntryByNumber(0x0028, 0x0103); // "Pixel Representation"
291 if (Signed == GDCM_UNFOUND) {
292 dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Pixel Representation");
293 BitsAlloc = std::string("0");
296 Signed = std::string("U");
298 Signed = std::string("S");
300 return( BitsAlloc + Signed);
305 * \ingroup gdcmHeader
306 * \brief Recover the offset (from the beginning of the file)
307 * of *image* pixels (not *icone image* pixels, if any !)
308 * @return Pixel Offset
310 size_t gdcmHeader::GetPixelOffset(void) {
312 gdcmDocEntry* PixelElement = GetDocEntryByNumber(GrPixel,NumPixel);
315 return PixelElement->GetOffset();
318 std::cout << "Big trouble : Pixel Element ("
319 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found"
325 // TODO : unify those two (previous one and next one)
327 * \ingroup gdcmHeader
328 * \brief Recover the pixel area length (in Bytes)
329 * @return Pixel Element Length, as stored in the header
330 * (NOT the memory space necessary to hold the Pixels
331 * -in case of embeded compressed image-)
332 * 0 : NOT USABLE file. The caller has to check.
334 size_t gdcmHeader::GetPixelAreaLength(void) {
336 gdcmDocEntry* PixelElement = GetDocEntryByNumber(GrPixel,NumPixel);
339 return PixelElement->GetLength();
342 std::cout << "Big trouble : Pixel Element ("
343 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found"
351 * \ingroup gdcmHeader
352 * \brief tells us if LUT are used
353 * \warning Right now, 'Segmented xxx Palette Color Lookup Table Data'
354 * are NOT considered as LUT, since nobody knows
355 * how to deal with them
356 * Please warn me if you know sbdy that *does* know ... jprx
357 * @return true if LUT Descriptors and LUT Tables were found
359 bool gdcmHeader::HasLUT(void) {
361 // Check the presence of the LUT Descriptors, and LUT Tables
363 if ( !GetDocEntryByNumber(0x0028,0x1101) )
365 // LutDescriptorGreen
366 if ( !GetDocEntryByNumber(0x0028,0x1102) )
369 if ( !GetDocEntryByNumber(0x0028,0x1103) )
371 // Red Palette Color Lookup Table Data
372 if ( !GetDocEntryByNumber(0x0028,0x1201) )
374 // Green Palette Color Lookup Table Data
375 if ( !GetDocEntryByNumber(0x0028,0x1202) )
377 // Blue Palette Color Lookup Table Data
378 if ( !GetDocEntryByNumber(0x0028,0x1203) )
380 // FIXME : (0x0028,0x3006) : LUT Data (CTX dependent)
381 // NOT taken into account, but we don't know how to use it ...
386 * \ingroup gdcmHeader
387 * \brief gets the info from 0028,1101 : Lookup Table Desc-Red
389 * @return Lookup Table number of Bits , 0 by default
390 * when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
391 * @ return bit number of each LUT item
393 int gdcmHeader::GetLUTNbits(void) {
394 std::vector<std::string> tokens;
398 //Just hope Lookup Table Desc-Red = Lookup Table Desc-Red = Lookup Table Desc-Blue
399 // Consistency already checked in GetLUTLength
400 std::string LutDescription = GetEntryByNumber(0x0028,0x1101);
401 if (LutDescription == GDCM_UNFOUND)
403 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
404 Tokenize (LutDescription, tokens, "\\");
405 //LutLength=atoi(tokens[0].c_str());
406 //LutDepth=atoi(tokens[1].c_str());
407 LutNbits=atoi(tokens[2].c_str());
413 * \ingroup gdcmHeader
414 * \brief builts Red/Green/Blue/Alpha LUT from Header
415 * when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
416 * and (0028,1101),(0028,1102),(0028,1102)
417 * - xxx Palette Color Lookup Table Descriptor - are found
418 * and (0028,1201),(0028,1202),(0028,1202)
419 * - xxx Palette Color Lookup Table Data - are found
420 * \warning does NOT deal with :
421 * 0028 1100 Gray Lookup Table Descriptor (Retired)
422 * 0028 1221 Segmented Red Palette Color Lookup Table Data
423 * 0028 1222 Segmented Green Palette Color Lookup Table Data
424 * 0028 1223 Segmented Blue Palette Color Lookup Table Data
425 * no known Dicom reader deals with them :-(
426 * @return a RGBA Lookup Table
428 unsigned char * gdcmHeader::GetLUTRGBA(void) {
430 // http://www.barre.nom.fr/medical/dicom2/limitations.html#Color%20Lookup%20Tables
432 // if Photometric Interpretation # PALETTE COLOR, no LUT to be done
433 if (GetEntryByNumber(0x0028,0x0004) != "PALETTE COLOR ") {
436 int lengthR, debR, nbitsR;
437 int lengthG, debG, nbitsG;
438 int lengthB, debB, nbitsB;
440 // Get info from Lut Descriptors
441 // (the 3 LUT descriptors may be different)
442 std::string LutDescriptionR = GetEntryByNumber(0x0028,0x1101);
443 if (LutDescriptionR == GDCM_UNFOUND)
445 std::string LutDescriptionG = GetEntryByNumber(0x0028,0x1102);
446 if (LutDescriptionG == GDCM_UNFOUND)
448 std::string LutDescriptionB = GetEntryByNumber(0x0028,0x1103);
449 if (LutDescriptionB == GDCM_UNFOUND)
452 std::vector<std::string> tokens;
454 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
455 Tokenize (LutDescriptionR, tokens, "\\");
456 lengthR=atoi(tokens[0].c_str()); // Red LUT length in Bytes
457 debR =atoi(tokens[1].c_str()); // subscript of the first Lut Value
458 nbitsR =atoi(tokens[2].c_str()); // Lut item size (in Bits)
461 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
462 Tokenize (LutDescriptionG, tokens, "\\");
463 lengthG=atoi(tokens[0].c_str()); // Green LUT length in Bytes
464 debG =atoi(tokens[1].c_str()); // subscript of the first Lut Value
465 nbitsG =atoi(tokens[2].c_str()); // Lut item size (in Bits)
468 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
469 Tokenize (LutDescriptionB, tokens, "\\");
470 lengthB=atoi(tokens[0].c_str()); // Blue LUT length in Bytes
471 debB =atoi(tokens[1].c_str()); // subscript of the first Lut Value
472 nbitsB =atoi(tokens[2].c_str()); // Lut item size (in Bits)
475 // Load LUTs into memory, (as they were stored on disk)
476 unsigned char *lutR = (unsigned char *)
477 GetEntryVoidAreaByNumber(0x0028,0x1201);
478 unsigned char *lutG = (unsigned char *)
479 GetEntryVoidAreaByNumber(0x0028,0x1202);
480 unsigned char *lutB = (unsigned char *)
481 GetEntryVoidAreaByNumber(0x0028,0x1203);
483 if (!lutR || !lutG || !lutB ) {
486 // forge the 4 * 8 Bits Red/Green/Blue/Alpha LUT
488 unsigned char *LUTRGBA = new unsigned char[1024]; // 256 * 4 (R, G, B, Alpha)
492 memset(LUTRGBA, 0, 1024);
495 std::string str_nb = GetEntryByNumber(0x0028,0x0100);
496 if (str_nb == GDCM_UNFOUND ) {
499 nb = atoi(str_nb.c_str() );
503 if (nbitsR==16 && nb==8) // when LUT item size is different than pixel size
504 mult=2; // high byte must be = low byte
505 else // See PS 3.3-2003 C.11.1.1.2 p 619
508 // if we get a black image, let's just remove the '+1'
509 // from 'i*mult+1' and check again
510 // if it works, we shall have to check the 3 Palettes
511 // to see which byte is ==0 (first one, or second one)
513 // We give up the checking to avoid some (useless ?)overhead
514 // (optimistic asumption)
519 for(i=0;i<lengthR;i++) {
524 for(i=0;i<lengthG;i++) {
529 for(i=0;i<lengthB;i++) {
535 *a = 1; // Alpha component
539 //How to free the now useless LUTs?
540 //free(LutR); free(LutB); free(LutG); // Seg Fault when used
545 * \brief Accesses the info from 0002,0010 : Transfert Syntax and gdcmTS
547 * @return The full Transfert Syntax Name (as opposed to Transfert Syntax UID)
549 std::string gdcmHeader::GetTransfertSyntaxName(void) {
550 // use the gdcmTS (TS : Transfert Syntax)
551 std::string TransfertSyntax = GetEntryByNumber(0x0002,0x0010);
552 if (TransfertSyntax == GDCM_UNFOUND) {
553 dbg.Verbose(0, "gdcmHeader::GetTransfertSyntaxName:"
554 " unfound Transfert Syntax (0002,0010)");
555 return "Uncompressed ACR-NEMA";
557 // we do it only when we need it
558 gdcmTS * ts = gdcmGlobal::GetTS();
559 std::string tsName=ts->GetValue(TransfertSyntax);
560 //delete ts; /// \todo Seg Fault when deleted ?!
565 * \brief Sets the Pixel Area size in the Header
566 * --> not-for-rats function
567 * @param ImageDataSize new Pixel Area Size
568 * warning : nothing else is checked
570 void gdcmHeader::SetImageDataSize(size_t ImageDataSize) {
571 std::string content1;
574 sprintf(car,"%d",ImageDataSize);
576 gdcmDocEntry *a = GetDocEntryByNumber(GrPixel, NumPixel);
577 a->SetLength(ImageDataSize);
580 sprintf(car,"%d",ImageDataSize);
582 SetEntryByNumber(content1, GrPixel, NumPixel);
585 //-----------------------------------------------------------------------------
589 * \brief anonymize a Header (removes Patient's personal info)
590 * (read the code to see which ones ...)
592 bool gdcmHeader::anonymizeHeader() {
594 gdcmDocEntry *patientNameHE = GetDocEntryByNumber (0x0010, 0x0010);
596 ReplaceIfExistByNumber (" ",0x0010, 0x2154); // Telephone
597 ReplaceIfExistByNumber (" ",0x0010, 0x1040); // Adress
598 ReplaceIfExistByNumber (" ",0x0010, 0x0020); // Patient ID
601 std::string StudyInstanceUID = GetEntryByNumber (0x0020, 0x000d);
602 if (StudyInstanceUID !=GDCM_UNFOUND)
603 ReplaceOrCreateByNumber(StudyInstanceUID, 0x0010, 0x0010);
605 ReplaceOrCreateByNumber(std::string("anonymised"), 0x0010, 0x0010);
609 // (if any) remove or replace all the stuff that contains a Date
611 //0008 0012 DA ID Instance Creation Date
612 //0008 0020 DA ID Study Date
613 //0008 0021 DA ID Series Date
614 //0008 0022 DA ID Acquisition Date
615 //0008 0023 DA ID Content Date
616 //0008 0024 DA ID Overlay Date
617 //0008 0025 DA ID Curve Date
618 //0008 002a DT ID Acquisition Datetime
619 //0018 9074 DT ACQ Frame Acquisition Datetime
620 //0018 9151 DT ACQ Frame Reference Datetime
621 //0018 a002 DT ACQ Contribution Date Time
622 //0020 3403 SH REL Modified Image Date (RET)
623 //0032 0032 DA SDY Study Verified Date
624 //0032 0034 DA SDY Study Read Date
625 //0032 1000 DA SDY Scheduled Study Start Date
626 //0032 1010 DA SDY Scheduled Study Stop Date
627 //0032 1040 DA SDY Study Arrival Date
628 //0032 1050 DA SDY Study Completion Date
629 //0038 001a DA VIS Scheduled Admission Date
630 //0038 001c DA VIS Scheduled Discharge Date
631 //0038 0020 DA VIS Admitting Date
632 //0038 0030 DA VIS Discharge Date
633 //0040 0002 DA PRC Scheduled Procedure Step Start Date
634 //0040 0004 DA PRC Scheduled Procedure Step End Date
635 //0040 0244 DA PRC Performed Procedure Step Start Date
636 //0040 0250 DA PRC Performed Procedure Step End Date
637 //0040 2004 DA PRC Issue Date of Imaging Service Request
638 //0040 4005 DT PRC Scheduled Procedure Step Start Date and Time
639 //0040 4011 DT PRC Expected Completion Date and Time
640 //0040 a030 DT PRC Verification Date Time
641 //0040 a032 DT PRC Observation Date Time
642 //0040 a120 DT PRC DateTime
643 //0040 a121 DA PRC Date
644 //0040 a13a DT PRC Referenced Datetime
645 //0070 0082 DA ??? Presentation Creation Date
646 //0100 0420 DT ??? SOP Autorization Date and Time
647 //0400 0105 DT ??? Digital Signature DateTime
648 //2100 0040 DA PJ Creation Date
649 //3006 0008 DA SSET Structure Set Date
650 //3008 0024 DA ??? Treatment Control Point Date
651 //3008 0054 DA ??? First Treatment Date
652 //3008 0056 DA ??? Most Recent Treatment Date
653 //3008 0162 DA ??? Safe Position Exit Date
654 //3008 0166 DA ??? Safe Position Return Date
655 //3008 0250 DA ??? Treatment Date
656 //300a 0006 DA RT RT Plan Date
657 //300a 022c DA RT Air Kerma Rate Reference Date
658 //300e 0004 DA RT Review Date
661 //-----------------------------------------------------------------------------
664 //-----------------------------------------------------------------------------