2 //-----------------------------------------------------------------------------
3 #include "gdcmHeader.h"
7 #include <cctype> // for isalpha
12 //-----------------------------------------------------------------------------
13 // Refer to gdcmHeader::CheckSwap()
14 //const unsigned int gdcmHeader::HEADER_LENGTH_TO_READ = 256;
16 // Refer to gdcmHeader::SetMaxSizeLoadEntry()
17 //const unsigned int gdcmHeader::MAX_SIZE_LOAD_ELEMENT_VALUE = 4096;
19 //-----------------------------------------------------------------------------
20 // Constructor / Destructor
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 *\TODO : may be we need one more bool,
30 * to allow skipping the private elements while parsing the header
31 * in order to save space
33 gdcmHeader::gdcmHeader(const char *InFilename,
34 bool exception_on_error,
35 bool enable_sequences ):
36 gdcmParser(InFilename,exception_on_error,enable_sequences)
43 * @param exception_on_error
45 gdcmHeader::gdcmHeader(bool exception_on_error) :
46 gdcmParser(exception_on_error)
52 * \brief Canonical destructor.
54 gdcmHeader::~gdcmHeader (void) {
57 //-----------------------------------------------------------------------------
60 //-----------------------------------------------------------------------------
64 * \brief This predicate, based on hopefully reasonable heuristics,
65 * decides whether or not the current gdcmParser was properly parsed
66 * and contains the mandatory information for being considered as
67 * a well formed and usable Dicom/Acr File.
68 * @return true when gdcmParser is the one of a reasonable Dicom/Acr file,
71 bool gdcmHeader::IsReadable(void)
73 if(!gdcmParser::IsReadable())
76 std::string res = GetEntryByNumber(0x0028, 0x0005);
77 if ( res != GDCM_UNFOUND && atoi(res.c_str()) > 4 )
78 return false; // Image Dimensions
80 if ( !GetHeaderEntryByNumber(0x0028, 0x0100) )
81 return false; // "Bits Allocated"
82 if ( !GetHeaderEntryByNumber(0x0028, 0x0101) )
83 return false; // "Bits Stored"
84 if ( !GetHeaderEntryByNumber(0x0028, 0x0102) )
85 return false; // "High Bit"
86 if ( !GetHeaderEntryByNumber(0x0028, 0x0103) )
87 return false; // "Pixel Representation"
93 * \brief Determines if the Transfer Syntax was already encountered
94 * and if it corresponds to a JPEGBaseLineProcess1 one.
96 * @return True when JPEGBaseLineProcess1found. False in all other cases.
98 bool gdcmHeader::IsJPEGBaseLineProcess1TransferSyntax(void) {
99 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
102 LoadHeaderEntrySafe(Element);
104 std::string Transfer = Element->GetValue();
105 if ( Transfer == "1.2.840.10008.1.2.4.50" )
111 * \ingroup gdcmHeader
112 * \brief Determines if the Transfer Syntax was already encountered
113 * and if it corresponds to a JPEGExtendedProcess2-4 one.
115 * @return True when JPEGExtendedProcess2-4 found. False in all other cases.
117 bool gdcmHeader::IsJPEGExtendedProcess2_4TransferSyntax(void) {
118 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
121 LoadHeaderEntrySafe(Element);
123 std::string Transfer = Element->GetValue();
124 if ( Transfer == "1.2.840.10008.1.2.4.51" )
130 * \ingroup gdcmHeader
131 * \brief Determines if the Transfer Syntax was already encountered
132 * and if it corresponds to a JPEGExtendeProcess3-5 one.
134 * @return True when JPEGExtendedProcess3-5 found. False in all other cases.
136 bool gdcmHeader::IsJPEGExtendedProcess3_5TransferSyntax(void) {
137 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
140 LoadHeaderEntrySafe(Element);
142 std::string Transfer = Element->GetValue();
143 if ( Transfer == "1.2.840.10008.1.2.4.52" )
149 * \ingroup gdcmHeader
150 * \brief Determines if the Transfer Syntax was already encountered
151 * and if it corresponds to a JPEGSpectralSelectionProcess6-8 one.
153 * @return True when JPEGSpectralSelectionProcess6-8 found. False in all
156 bool gdcmHeader::IsJPEGSpectralSelectionProcess6_8TransferSyntax(void) {
157 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
160 LoadHeaderEntrySafe(Element);
162 std::string Transfer = Element->GetValue();
163 if ( Transfer == "1.2.840.10008.1.2.4.53" )
169 * \ingroup gdcmHeader
170 * \brief Determines if the Transfer Syntax was already encountered
171 * and if it corresponds to a RLE Lossless one.
173 * @return True when RLE Lossless found. False in all
176 bool gdcmHeader::IsRLELossLessTransferSyntax(void) {
177 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
180 LoadHeaderEntrySafe(Element);
182 std::string Transfer = Element->GetValue();
183 if ( Transfer == "1.2.840.10008.1.2.5" )
189 * \ingroup gdcmHeader
190 * \brief Determines if Transfer Syntax was already encountered
191 * and if it corresponds to a JPEG Lossless one.
193 * @return True when RLE Lossless found. False in all
196 bool gdcmHeader::IsJPEGLossless(void) {
197 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
198 // faire qq chose d'intelligent a la place de ça
201 LoadHeaderEntrySafe(Element);
203 const char * Transfert = Element->GetValue().c_str();
204 if ( memcmp(Transfert+strlen(Transfert)-2 ,"70",2)==0) return true;
205 if ( memcmp(Transfert+strlen(Transfert)-2 ,"55",2)==0) return true;
206 if (Element->GetValue() == "1.2.840.10008.1.2.4.57") return true;
212 * \ingroup gdcmHeader
213 * \brief Determines if the Transfer Syntax was already encountered
214 * and if it corresponds to a JPEG200 one.0
216 * @return True when JPEG2000 (Lossly or LossLess) found. False in all
219 bool gdcmHeader::IsJPEG2000(void) {
220 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
223 LoadHeaderEntrySafe(Element);
225 std::string Transfer = Element->GetValue();
226 if ( (Transfer == "1.2.840.10008.1.2.4.90")
227 || (Transfer == "1.2.840.10008.1.2.4.91") )
233 * \ingroup gdcmHeader
234 * \brief Predicate for dicom version 3 file.
235 * @return True when the file is a dicom version 3.
237 bool gdcmHeader::IsDicomV3(void) {
238 // Checking if Transfert Syntax exists is enough
239 return (GetHeaderEntryByNumber(0x0002, 0x0010) != NULL);
243 * \ingroup gdcmHeader
244 * \brief Retrieve the number of columns of image.
245 * @return The encountered size when found, 0 by default.
246 * 0 means the file is NOT USABLE. The caller will have to check
248 int gdcmHeader::GetXSize(void) {
249 // We cannot check for "Columns" because the "Columns" tag is present
250 // both in IMG (0028,0011) and OLY (6000,0011) sections of the dictionary.
251 std::string StrSize = GetEntryByNumber(0x0028,0x0011);
252 if (StrSize == GDCM_UNFOUND)
254 return atoi(StrSize.c_str());
258 * \ingroup gdcmHeader
259 * \brief Retrieve the number of lines of image.
260 * \warning The defaulted value is 1 as opposed to gdcmHeader::GetXSize()
261 * @return The encountered size when found, 1 by default
262 * (The file contains a Signal, not an Image).
264 int gdcmHeader::GetYSize(void) {
265 // We cannot check for "Rows" because the "Rows" tag is present
266 // both in IMG (0028,0010) and OLY (6000,0010) sections of the dictionary.
267 std::string StrSize = GetEntryByNumber(0x0028,0x0010);
268 if (StrSize != GDCM_UNFOUND)
269 return atoi(StrSize.c_str());
273 // The Rows (0028,0010) entry was optional for ACR/NEMA. It might
274 // hence be a signal (1d image). So we default to 1:
279 * \ingroup gdcmHeader
280 * \brief Retrieve the number of planes of volume or the number
281 * of frames of a multiframe.
282 * \warning When present we consider the "Number of Frames" as the third
283 * dimension. When absent we consider the third dimension as
284 * being the "Planes" tag content.
285 * @return The encountered size when found, 1 by default (single image).
287 int gdcmHeader::GetZSize(void) {
288 // Both DicomV3 and ACR/Nema consider the "Number of Frames"
289 // as the third dimension.
290 std::string StrSize = GetEntryByNumber(0x0028,0x0008);
291 if (StrSize != GDCM_UNFOUND)
292 return atoi(StrSize.c_str());
294 // We then consider the "Planes" entry as the third dimension [we
295 // cannot retrieve by name since "Planes tag is present both in
296 // IMG (0028,0012) and OLY (6000,0012) sections of the dictionary].
297 StrSize = GetEntryByNumber(0x0028,0x0012);
298 if (StrSize != GDCM_UNFOUND)
299 return atoi(StrSize.c_str());
304 * \ingroup gdcmHeader
305 * \brief Retrieve the number of Bits Stored (actually used)
306 * (as opposite to number of Bits Allocated)
308 * @return The encountered number of Bits Stored, 0 by default.
309 * 0 means the file is NOT USABLE. The caller has to check it !
311 int gdcmHeader::GetBitsStored(void) {
312 std::string StrSize = GetEntryByNumber(0x0028,0x0101);
313 if (StrSize == GDCM_UNFOUND)
314 return 0; // It's supposed to be mandatory
315 // the caller will have to check
316 return atoi(StrSize.c_str());
320 * \ingroup gdcmHeader
321 * \brief Retrieve the number of Bits Allocated
322 * (8, 12 -compacted ACR-NEMA files, 16, ...)
324 * @return The encountered number of Bits Allocated, 0 by default.
325 * 0 means the file is NOT USABLE. The caller has to check it !
327 int gdcmHeader::GetBitsAllocated(void) {
328 std::string StrSize = GetEntryByNumber(0x0028,0x0100);
329 if (StrSize == GDCM_UNFOUND)
330 return 0; // It's supposed to be mandatory
331 // the caller will have to check
332 return atoi(StrSize.c_str());
336 * \ingroup gdcmHeader
337 * \brief Retrieve the number of Samples Per Pixel
338 * (1 : gray level, 3 : RGB -1 or 3 Planes-)
340 * @return The encountered number of Samples Per Pixel, 1 by default.
341 * (Gray level Pixels)
343 int gdcmHeader::GetSamplesPerPixel(void) {
344 std::string StrSize = GetEntryByNumber(0x0028,0x0002);
345 if (StrSize == GDCM_UNFOUND)
346 return 1; // Well, it's supposed to be mandatory ...
347 // but sometimes it's missing : we assume Gray pixels
348 return atoi(StrSize.c_str());
352 * \ingroup gdcmHeader
353 * \brief Retrieve the Planar Configuration for RGB images
354 * (0 : RGB Pixels , 1 : R Plane + G Plane + B Plane)
356 * @return The encountered Planar Configuration, 0 by default.
358 int gdcmHeader::GetPlanarConfiguration(void) {
359 std::string StrSize = GetEntryByNumber(0x0028,0x0006);
360 if (StrSize == GDCM_UNFOUND)
362 return atoi(StrSize.c_str());
366 * \ingroup gdcmHeader
367 * \brief Return the size (in bytes) of a single pixel of data.
368 * @return The size in bytes of a single pixel of data; 0 by default
369 * 0 means the file is NOT USABLE; the caller will have to check
371 int gdcmHeader::GetPixelSize(void) {
372 std::string PixelType = GetPixelType();
373 if (PixelType == "8U" || PixelType == "8S")
375 if (PixelType == "16U" || PixelType == "16S")
377 if (PixelType == "32U" || PixelType == "32S")
379 dbg.Verbose(0, "gdcmHeader::GetPixelSize: Unknown pixel type");
384 * \ingroup gdcmHeader
385 * \brief Build the Pixel Type of the image.
386 * Possible values are:
387 * - 8U unsigned 8 bit,
389 * - 16U unsigned 16 bit,
390 * - 16S signed 16 bit,
391 * - 32U unsigned 32 bit,
392 * - 32S signed 32 bit,
393 * \warning 12 bit images appear as 16 bit.
394 * \ 24 bit images appear as 8 bit
395 * @return 0S if nothing found. NOT USABLE file. The caller has to check
397 std::string gdcmHeader::GetPixelType(void) {
398 std::string BitsAlloc = GetEntryByNumber(0x0028, 0x0100); // Bits Allocated
399 if (BitsAlloc == GDCM_UNFOUND) {
400 dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Bits Allocated");
401 BitsAlloc = std::string("16");
403 if (BitsAlloc == "12") // It will be unpacked
404 BitsAlloc = std::string("16");
405 else if (BitsAlloc == "24") // (in order no to be messed up
406 BitsAlloc = std::string("8"); // by old RGB images)
408 std::string Signed = GetEntryByNumber(0x0028, 0x0103); // "Pixel Representation"
409 if (Signed == GDCM_UNFOUND) {
410 dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Pixel Representation");
411 BitsAlloc = std::string("0");
414 Signed = std::string("U");
416 Signed = std::string("S");
418 return( BitsAlloc + Signed);
422 * \ingroup gdcmHeader
423 * \brief Recover the offset (from the beginning of the file) of the pixels.
425 size_t gdcmHeader::GetPixelOffset(void) {
426 // If this file complies with the norm we should encounter the
427 // "Image Location" tag (0x0028, 0x0200). This tag contains the
428 // the group that contains the pixel data (hence the "Pixel Data"
429 // is found by indirection through the "Image Location").
430 // Inside the group pointed by "Image Location" the searched element
431 // is conventionally the element 0x0010 (when the norm is respected).
432 // When the "Image Location" is absent we default to group 0x7fe0.
435 std::string ImageLocation = GetEntryByNumber(0x0028, 0x0200);
437 if ( ImageLocation == GDCM_UNFOUND ) { // Image Location
440 grPixel = (guint16) atoi( ImageLocation.c_str() );
442 if (grPixel != 0x7fe0)
443 // This is a kludge for old dirty Philips imager.
448 gdcmHeaderEntry* PixelElement = GetHeaderEntryByNumber(grPixel,numPixel);
450 return PixelElement->GetOffset();
456 * \ingroup gdcmHeader
457 * \brief Recover the pixel area length (in Bytes)
458 * @return 0 by default. NOT USABLE file. The caller has to check.
460 size_t gdcmHeader::GetPixelAreaLength(void) {
461 // If this file complies with the norm we should encounter the
462 // "Image Location" tag (0x0028, 0x0200). This tag contains the
463 // the group that contains the pixel data (hence the "Pixel Data"
464 // is found by indirection through the "Image Location").
465 // Inside the group pointed by "Image Location" the searched element
466 // is conventionally the element 0x0010 (when the norm is respected).
467 // When the "Image Location" is absent we default to group 0x7fe0.
470 std::string ImageLocation = GetEntryByNumber(0x0028, 0x0200);
471 if ( ImageLocation == GDCM_UNFOUND ) {
474 grPixel = (guint16) atoi( ImageLocation.c_str() );
476 if (grPixel != 0x7fe0)
477 // This is a kludge for old dirty Philips imager.
482 gdcmHeaderEntry* PixelElement = GetHeaderEntryByNumber(grPixel,numPixel);
484 return PixelElement->GetLength();
490 * \ingroup gdcmHeader
491 * \brief tells us if LUT are used
492 * \warning Right now, 'Segmented xxx Palette Color Lookup Table Data'
493 * \ are NOT considered as LUT, since nobody knows
494 * \ how to deal with them
497 bool gdcmHeader::HasLUT(void) {
499 // Check the presence of the LUT Descriptors
500 if ( !GetHeaderEntryByNumber(0x0028,0x1101) )
502 // LutDescriptorGreen
503 if ( !GetHeaderEntryByNumber(0x0028,0x1102) )
506 if ( !GetHeaderEntryByNumber(0x0028,0x1103) )
510 if ( !GetHeaderEntryByNumber(0x0028,0x1201) )
512 if ( !GetHeaderEntryByNumber(0x0028,0x1202) )
514 if ( !GetHeaderEntryByNumber(0x0028,0x1203) )
520 * \ingroup gdcmHeader
521 * \brief gets the info from 0028,1101 : Lookup Table Desc-Red
523 * @return Lookup Table number of Bits , 0 by default
524 * \ when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
526 int gdcmHeader::GetLUTNbits(void) {
527 std::vector<std::string> tokens;
531 //Just hope Lookup Table Desc-Red = Lookup Table Desc-Red = Lookup Table Desc-Blue
532 // Consistency already checked in GetLUTLength
533 std::string LutDescription = GetEntryByNumber(0x0028,0x1101);
534 if (LutDescription == GDCM_UNFOUND)
536 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
537 Tokenize (LutDescription, tokens, "\\");
538 //LutLength=atoi(tokens[0].c_str());
539 //LutDepth=atoi(tokens[1].c_str());
540 LutNbits=atoi(tokens[2].c_str());
546 * \ingroup gdcmHeader
547 * \brief builts Red/Green/Blue/Alpha LUT from Header
548 * \ when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
549 * \ and (0028,1101),(0028,1102),(0028,1102)
550 * \ - xxx Palette Color Lookup Table Descriptor - are found
551 * \ and (0028,1201),(0028,1202),(0028,1202)
552 * \ - xxx Palette Color Lookup Table Data - are found
553 * \warning does NOT deal with :
554 * \ 0028 1100 Gray Lookup Table Descriptor (Retired)
555 * \ 0028 1221 Segmented Red Palette Color Lookup Table Data
556 * \ 0028 1222 Segmented Green Palette Color Lookup Table Data
557 * \ 0028 1223 Segmented Blue Palette Color Lookup Table Data
558 * \ no known Dicom reader deals with them :-(
559 * @return a RGBA Lookup Table
561 unsigned char * gdcmHeader::GetLUTRGBA(void) {
563 // http://www.barre.nom.fr/medical/dicom2/limitations.html#Color%20Lookup%20Tables
565 // if Photometric Interpretation # PALETTE COLOR, no LUT to be done
566 if (GetEntryByNumber(0x0028,0x0004) != "PALETTE COLOR ") {
569 int lengthR, debR, nbitsR;
570 int lengthG, debG, nbitsG;
571 int lengthB, debB, nbitsB;
573 // Get info from Lut Descriptors
574 // (the 3 LUT descriptors may be different)
575 std::string LutDescriptionR = GetEntryByNumber(0x0028,0x1101);
576 if (LutDescriptionR == GDCM_UNFOUND)
578 std::string LutDescriptionG = GetEntryByNumber(0x0028,0x1102);
579 if (LutDescriptionG == GDCM_UNFOUND)
581 std::string LutDescriptionB = GetEntryByNumber(0x0028,0x1103);
582 if (LutDescriptionB == GDCM_UNFOUND)
585 std::vector<std::string> tokens;
587 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
588 Tokenize (LutDescriptionR, tokens, "\\");
589 lengthR=atoi(tokens[0].c_str()); // Red LUT length in Bytes
590 debR =atoi(tokens[1].c_str()); // subscript of the first Lut Value
591 nbitsR =atoi(tokens[2].c_str()); // Lut item size (in Bits)
594 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
595 Tokenize (LutDescriptionG, tokens, "\\");
596 lengthG=atoi(tokens[0].c_str()); // Green LUT length in Bytes
597 debG =atoi(tokens[1].c_str());
598 nbitsG =atoi(tokens[2].c_str());
601 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
602 Tokenize (LutDescriptionB, tokens, "\\");
603 lengthB=atoi(tokens[0].c_str()); // Blue LUT length in Bytes
604 debB =atoi(tokens[1].c_str());
605 nbitsB =atoi(tokens[2].c_str());
608 // Load LUTs into memory, (as they were stored on disk)
609 unsigned char *lutR = (unsigned char *)
610 GetEntryVoidAreaByNumber(0x0028,0x1201);
611 unsigned char *lutG = (unsigned char *)
612 GetEntryVoidAreaByNumber(0x0028,0x1202);
613 unsigned char *lutB = (unsigned char *)
614 GetEntryVoidAreaByNumber(0x0028,0x1203);
616 if (!lutR || !lutG || !lutB ) {
619 // forge the 4 * 8 Bits Red/Green/Blue/Alpha LUT
621 unsigned char *LUTRGBA = (unsigned char *)calloc(1024,1); // 256 * 4 (R, G, B, Alpha)
625 memset(LUTRGBA, 0, 1024);
628 std::string str_nb = GetEntryByNumber(0x0028,0x0100);
629 if (str_nb == GDCM_UNFOUND ) {
632 nb = atoi(str_nb.c_str() );
636 if (nbitsR==16 && nb==8) // when LUT item size is different than pixel size
637 mult=2; // high byte must be = low byte
638 else // See PS 3.3-2003 C.11.1.1.2 p 619
641 // if we get a black image, let's just remove the '+1'
642 // from 'i*mult+1' and check again
643 // if it works, we shall have to check the 3 Palettes
644 // to see which byte is ==0 (first one, or second one)
646 // We give up the checking to avoid some overhead
651 for(i=0;i<lengthR;i++) {
656 for(i=0;i<lengthG;i++) {
661 for(i=0;i<lengthB;i++) {
667 *a = 1; // Alpha component
671 //How to free the now useless LUTs?
673 //free(LutR); free(LutB); free(LutG); // Seg Fault when used
678 * \ingroup gdcmHeader
679 * \brief gets the info from 0002,0010 : Transfert Syntax
681 * @return Transfert Syntax Name (as oposite to Transfert Syntax UID)
683 std::string gdcmHeader::GetTransfertSyntaxName(void) {
684 // use the gdcmTS (TS : Transfert Syntax)
685 std::string TransfertSyntax = GetEntryByNumber(0x0002,0x0010);
686 if (TransfertSyntax == GDCM_UNFOUND) {
687 dbg.Verbose(0, "gdcmHeader::GetTransfertSyntaxName: unfound Transfert Syntax (0002,0010)");
688 return "Uncompressed ACR-NEMA";
690 // we do it only when we need it
691 gdcmTS * ts = gdcmGlobal::GetTS();
692 std::string tsName=ts->GetValue(TransfertSyntax);
693 //delete ts; // Seg Fault when deleted ?!
699 * \brief Sets the Pixel Area size in the Header
700 * --> not-for-rats function
702 * \warning WARNING doit-etre etre publique ?
703 * TODO : y aurait il un inconvenient à fusionner ces 2 fonctions
705 * @param ImageDataSize new Pixel Area Size
706 * warning : nothing else is checked
708 void gdcmHeader::SetImageDataSize(size_t ImageDataSize) {
709 std::string content1;
711 // Assumes HeaderEntry (0x7fe0, 0x0010) exists ...
712 sprintf(car,"%d",ImageDataSize);
714 gdcmHeaderEntry *a = GetHeaderEntryByNumber(0x7fe0, 0x0010);
715 a->SetLength(ImageDataSize);
718 sprintf(car,"%d",ImageDataSize);
720 SetEntryByNumber(content1, 0x7fe0, 0x0000);
723 //-----------------------------------------------------------------------------
726 //-----------------------------------------------------------------------------
729 //-----------------------------------------------------------------------------