2 //-----------------------------------------------------------------------------
3 #include "gdcmHeader.h"
11 #include <netinet/in.h>
13 #include <cctype> // for isalpha
15 #ifdef GDCM_NO_ANSI_STRING_STREAM
17 # define ostringstream ostrstream
25 //-----------------------------------------------------------------------------
26 // Refer to gdcmHeader::CheckSwap()
27 const unsigned int gdcmHeader::HEADER_LENGTH_TO_READ = 256;
29 // Refer to gdcmHeader::SetMaxSizeLoadElementValue()
30 const unsigned int gdcmHeader::MAX_SIZE_LOAD_ELEMENT_VALUE = 4096;
32 //-----------------------------------------------------------------------------
33 // Constructor / Destructor
38 * @param exception_on_error
39 * @param enable_sequences = true to allow the header
40 * to be parsed *inside* the SeQuences,
41 * when they have an actual length
43 gdcmHeader::gdcmHeader(const char *InFilename,
44 bool exception_on_error,
45 bool enable_sequences ) {
51 SetMaxSizeLoadElementValue(MAX_SIZE_LOAD_ELEMENT_VALUE);
52 filename = InFilename;
54 if ( !OpenFile(exception_on_error))
64 * @param exception_on_error
66 gdcmHeader::gdcmHeader(bool exception_on_error) {
67 SetMaxSizeLoadElementValue(MAX_SIZE_LOAD_ELEMENT_VALUE);
73 * \brief Canonical destructor.
75 gdcmHeader::~gdcmHeader (void) {
76 dicom_vr = (gdcmVR*)0;
77 Dicts = (gdcmDictSet*)0;
78 RefPubDict = (gdcmDict*)0;
79 RefShaDict = (gdcmDict*)0;
83 //-----------------------------------------------------------------------------
91 void gdcmHeader::PrintPubEntry(std::ostream & os) {
92 PubEntrySet.Print(os);
100 void gdcmHeader::PrintPubDict(std::ostream & os) {
101 RefPubDict->Print(os);
104 //-----------------------------------------------------------------------------
107 * \ingroup gdcmHeader
108 * \brief This predicate, based on hopefully reasonable heuristics,
109 * decides whether or not the current gdcmHeader was properly parsed
110 * and contains the mandatory information for being considered as
111 * a well formed and usable image.
112 * @return true when gdcmHeader is the one of a reasonable Dicom file,
115 bool gdcmHeader::IsReadable(void) {
116 std::string res = GetEntryByNumber(0x0028, 0x0005);
117 if ( res != GDCM_UNFOUND && atoi(res.c_str()) > 4 ) {
118 return false; // Image Dimensions
121 if ( !GetHeaderEntryByNumber(0x0028, 0x0100) )
122 return false; // "Bits Allocated"
123 if ( !GetHeaderEntryByNumber(0x0028, 0x0101) )
124 return false; // "Bits Stored"
125 if ( !GetHeaderEntryByNumber(0x0028, 0x0102) )
126 return false; // "High Bit"
127 if ( !GetHeaderEntryByNumber(0x0028, 0x0103) )
128 return false; // "Pixel Representation"
133 * \ingroup gdcmHeader
134 * \brief Determines if the Transfer Syntax was already encountered
135 * and if it corresponds to a ImplicitVRLittleEndian one.
137 * @return True when ImplicitVRLittleEndian found. False in all other cases.
139 bool gdcmHeader::IsImplicitVRLittleEndianTransferSyntax(void) {
140 gdcmHeaderEntry *Element = GetHeaderEntryByNumber(0x0002, 0x0010);
143 LoadHeaderEntrySafe(Element);
145 std::string Transfer = Element->GetValue();
146 if ( Transfer == "1.2.840.10008.1.2" )
152 * \ingroup gdcmHeader
153 * \brief Determines if the Transfer Syntax was already encountered
154 * and if it corresponds to a ExplicitVRLittleEndian one.
156 * @return True when ExplicitVRLittleEndian found. False in all other cases.
158 bool gdcmHeader::IsExplicitVRLittleEndianTransferSyntax(void) {
159 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
162 LoadHeaderEntrySafe(Element);
164 std::string Transfer = Element->GetValue();
165 if ( Transfer == "1.2.840.10008.1.2.1" )
171 * \ingroup gdcmHeader
172 * \brief Determines if the Transfer Syntax was already encountered
173 * and if it corresponds to a DeflatedExplicitVRLittleEndian one.
175 * @return True when DeflatedExplicitVRLittleEndian found. False in all other cases.
177 bool gdcmHeader::IsDeflatedExplicitVRLittleEndianTransferSyntax(void) {
178 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
181 LoadHeaderEntrySafe(Element);
183 std::string Transfer = Element->GetValue();
184 if ( Transfer == "1.2.840.10008.1.2.1.99" )
190 * \ingroup gdcmHeader
191 * \brief Determines if the Transfer Syntax was already encountered
192 * and if it corresponds to a Explicit VR Big Endian one.
194 * @return True when big endian found. False in all other cases.
196 bool gdcmHeader::IsExplicitVRBigEndianTransferSyntax(void) {
197 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
200 LoadHeaderEntrySafe(Element);
202 std::string Transfer = Element->GetValue();
203 if ( Transfer == "1.2.840.10008.1.2.2" ) //1.2.2 ??? A verifier !
209 * \ingroup gdcmHeader
210 * \brief Determines if the Transfer Syntax was already encountered
211 * and if it corresponds to a JPEGBaseLineProcess1 one.
213 * @return True when JPEGBaseLineProcess1found. False in all other cases.
215 bool gdcmHeader::IsJPEGBaseLineProcess1TransferSyntax(void) {
216 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
219 LoadHeaderEntrySafe(Element);
221 std::string Transfer = Element->GetValue();
222 if ( Transfer == "1.2.840.10008.1.2.4.50" )
228 * \ingroup gdcmHeader
229 * \brief Determines if the Transfer Syntax was already encountered
230 * and if it corresponds to a JPEGExtendedProcess2-4 one.
232 * @return True when JPEGExtendedProcess2-4 found. False in all other cases.
234 bool gdcmHeader::IsJPEGExtendedProcess2_4TransferSyntax(void) {
235 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
238 LoadHeaderEntrySafe(Element);
240 std::string Transfer = Element->GetValue();
241 if ( Transfer == "1.2.840.10008.1.2.4.51" )
247 * \ingroup gdcmHeader
248 * \brief Determines if the Transfer Syntax was already encountered
249 * and if it corresponds to a JPEGExtendeProcess3-5 one.
251 * @return True when JPEGExtendedProcess3-5 found. False in all other cases.
253 bool gdcmHeader::IsJPEGExtendedProcess3_5TransferSyntax(void) {
254 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
257 LoadHeaderEntrySafe(Element);
259 std::string Transfer = Element->GetValue();
260 if ( Transfer == "1.2.840.10008.1.2.4.52" )
266 * \ingroup gdcmHeader
267 * \brief Determines if the Transfer Syntax was already encountered
268 * and if it corresponds to a JPEGSpectralSelectionProcess6-8 one.
270 * @return True when JPEGSpectralSelectionProcess6-8 found. False in all
273 bool gdcmHeader::IsJPEGSpectralSelectionProcess6_8TransferSyntax(void) {
274 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
277 LoadHeaderEntrySafe(Element);
279 std::string Transfer = Element->GetValue();
280 if ( Transfer == "1.2.840.10008.1.2.4.53" )
286 * \ingroup gdcmHeader
287 * \brief Determines if the Transfer Syntax was already encountered
288 * and if it corresponds to a RLE Lossless one.
290 * @return True when RLE Lossless found. False in all
293 bool gdcmHeader::IsRLELossLessTransferSyntax(void) {
294 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
297 LoadHeaderEntrySafe(Element);
299 std::string Transfer = Element->GetValue();
300 if ( Transfer == "1.2.840.10008.1.2.5" )
306 * \ingroup gdcmHeader
311 bool gdcmHeader::IsJPEGLossless(void) {
312 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
313 // faire qq chose d'intelligent a la place de ça
316 LoadHeaderEntrySafe(Element);
318 const char * Transfert = Element->GetValue().c_str();
319 if ( memcmp(Transfert+strlen(Transfert)-2 ,"70",2)==0) return true;
320 if ( memcmp(Transfert+strlen(Transfert)-2 ,"55",2)==0) return true;
321 if (Element->GetValue() == "1.2.840.10008.1.2.4.57") return true;
327 * \ingroup gdcmHeader
328 * \brief Determines if the Transfer Syntax was already encountered
329 * and if it corresponds to a JPEG200 one.0
331 * @return True when JPEG2000 (Lossly or LossLess) found. False in all
334 bool gdcmHeader::IsJPEG2000(void) {
335 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
338 LoadHeaderEntrySafe(Element);
340 std::string Transfer = Element->GetValue();
341 if ( (Transfer == "1.2.840.10008.1.2.4.90")
342 || (Transfer == "1.2.840.10008.1.2.4.91") )
348 * \ingroup gdcmHeader
349 * \brief Predicate for dicom version 3 file.
350 * @return True when the file is a dicom version 3.
352 bool gdcmHeader::IsDicomV3(void) {
353 if ( (filetype == ExplicitVR)
354 || (filetype == ImplicitVR) )
360 * \ingroup gdcmHeader
364 FileType gdcmHeader::GetFileType(void)
370 * \ingroup gdcmHeader
371 * \brief Retrieve the number of columns of image.
372 * @return The encountered size when found, 0 by default.
374 int gdcmHeader::GetXSize(void) {
375 // We cannot check for "Columns" because the "Columns" tag is present
376 // both in IMG (0028,0011) and OLY (6000,0011) sections of the dictionary.
377 std::string StrSize = GetEntryByNumber(0x0028,0x0011);
378 if (StrSize == GDCM_UNFOUND)
380 return atoi(StrSize.c_str());
384 * \ingroup gdcmHeader
385 * \brief Retrieve the number of lines of image.
386 * \warning The defaulted value is 1 as opposed to gdcmHeader::GetXSize()
387 * @return The encountered size when found, 1 by default.
389 int gdcmHeader::GetYSize(void) {
390 // We cannot check for "Rows" because the "Rows" tag is present
391 // both in IMG (0028,0010) and OLY (6000,0010) sections of the dictionary.
392 std::string StrSize = GetEntryByNumber(0x0028,0x0010);
393 if (StrSize != GDCM_UNFOUND)
394 return atoi(StrSize.c_str());
398 // The Rows (0028,0010) entry is optional for ACR/NEMA. It might
399 // hence be a signal (1d image). So we default to 1:
404 * \ingroup gdcmHeader
405 * \brief Retrieve the number of planes of volume or the number
406 * of frames of a multiframe.
407 * \warning When present we consider the "Number of Frames" as the third
408 * dimension. When absent we consider the third dimension as
409 * being the "Planes" tag content.
410 * @return The encountered size when found, 1 by default.
412 int gdcmHeader::GetZSize(void) {
413 // Both DicomV3 and ACR/Nema consider the "Number of Frames"
414 // as the third dimension.
415 std::string StrSize = GetEntryByNumber(0x0028,0x0008);
416 if (StrSize != GDCM_UNFOUND)
417 return atoi(StrSize.c_str());
419 // We then consider the "Planes" entry as the third dimension [we
420 // cannot retrieve by name since "Planes tag is present both in
421 // IMG (0028,0012) and OLY (6000,0012) sections of the dictionary].
422 StrSize = GetEntryByNumber(0x0028,0x0012);
423 if (StrSize != GDCM_UNFOUND)
424 return atoi(StrSize.c_str());
429 * \ingroup gdcmHeader
430 * \brief Retrieve the number of Bits Stored
431 * (as opposite to number of Bits Allocated)
433 * @return The encountered number of Bits Stored, 0 by default.
435 int gdcmHeader::GetBitsStored(void) {
436 std::string StrSize = GetEntryByNumber(0x0028,0x0101);
437 if (StrSize == GDCM_UNFOUND)
439 return atoi(StrSize.c_str());
443 * \ingroup gdcmHeader
444 * \brief Retrieve the number of Bits Allocated
445 * (8, 12 -compacted ACR-NEMA files, 16, ...)
447 * @return The encountered number of Bits Allocated, 0 by default.
449 int gdcmHeader::GetBitsAllocated(void) {
450 std::string StrSize = GetEntryByNumber(0x0028,0x0100);
451 if (StrSize == GDCM_UNFOUND)
453 return atoi(StrSize.c_str());
457 * \ingroup gdcmHeader
458 * \brief Retrieve the number of Samples Per Pixel
459 * (1 : gray level, 3 : RGB -1 or 3 Planes-)
461 * @return The encountered number of Samples Per Pixel, 1 by default.
463 int gdcmHeader::GetSamplesPerPixel(void) {
464 std::string StrSize = GetEntryByNumber(0x0028,0x0002);
465 if (StrSize == GDCM_UNFOUND)
466 return 1; // Well, it's supposed to be mandatory ...
467 return atoi(StrSize.c_str());
471 * \ingroup gdcmHeader
472 * \brief Retrieve the Planar Configuration for RGB images
473 * (0 : RGB Pixels , 1 : R Plane + G Plane + B Plane)
475 * @return The encountered Planar Configuration, 0 by default.
477 int gdcmHeader::GetPlanarConfiguration(void) {
478 std::string StrSize = GetEntryByNumber(0x0028,0x0006);
479 if (StrSize == GDCM_UNFOUND)
481 return atoi(StrSize.c_str());
485 * \ingroup gdcmHeader
486 * \brief Return the size (in bytes) of a single pixel of data.
487 * @return The size in bytes of a single pixel of data.
490 int gdcmHeader::GetPixelSize(void) {
491 std::string PixelType = GetPixelType();
492 if (PixelType == "8U" || PixelType == "8S")
494 if (PixelType == "16U" || PixelType == "16S")
496 if (PixelType == "32U" || PixelType == "32S")
498 dbg.Verbose(0, "gdcmHeader::GetPixelSize: Unknown pixel type");
503 * \ingroup gdcmHeader
504 * \brief Build the Pixel Type of the image.
505 * Possible values are:
506 * - 8U unsigned 8 bit,
508 * - 16U unsigned 16 bit,
509 * - 16S signed 16 bit,
510 * - 32U unsigned 32 bit,
511 * - 32S signed 32 bit,
512 * \warning 12 bit images appear as 16 bit.
513 * \ 24 bit images appear as 8 bit
516 std::string gdcmHeader::GetPixelType(void) {
517 std::string BitsAlloc = GetEntryByNumber(0x0028, 0x0100); // Bits Allocated
518 if (BitsAlloc == GDCM_UNFOUND) {
519 dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Bits Allocated");
520 BitsAlloc = std::string("16");
522 if (BitsAlloc == "12") // It will be unpacked
523 BitsAlloc = std::string("16");
524 else if (BitsAlloc == "24") // (in order no to be messed up
525 BitsAlloc = std::string("8"); // by old RGB images)
527 std::string Signed = GetEntryByNumber(0x0028, 0x0103); // "Pixel Representation"
528 if (Signed == GDCM_UNFOUND) {
529 dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Pixel Representation");
530 BitsAlloc = std::string("0");
533 Signed = std::string("U");
535 Signed = std::string("S");
537 return( BitsAlloc + Signed);
541 * \ingroup gdcmHeader
542 * \brief Recover the offset (from the beginning of the file) of the pixels.
544 size_t gdcmHeader::GetPixelOffset(void) {
545 // If this file complies with the norm we should encounter the
546 // "Image Location" tag (0x0028, 0x0200). This tag contains the
547 // the group that contains the pixel data (hence the "Pixel Data"
548 // is found by indirection through the "Image Location").
549 // Inside the group pointed by "Image Location" the searched element
550 // is conventionally the element 0x0010 (when the norm is respected).
551 // When the "Image Location" is absent we default to group 0x7fe0.
554 std::string ImageLocation = GetEntryByNumber(0x0028, 0x0200);
556 if ( ImageLocation == GDCM_UNFOUND ) { // Image Location
559 grPixel = (guint16) atoi( ImageLocation.c_str() );
561 if (grPixel != 0x7fe0)
562 // This is a kludge for old dirty Philips imager.
567 gdcmHeaderEntry* PixelElement = GetHeaderEntryByNumber(grPixel,numPixel);
569 return PixelElement->GetOffset();
575 * \ingroup gdcmHeader
576 * \brief Recover the pixel area length (in Bytes) .
578 size_t gdcmHeader::GetPixelAreaLength(void) {
579 // If this file complies with the norm we should encounter the
580 // "Image Location" tag (0x0028, 0x0200). This tag contains the
581 // the group that contains the pixel data (hence the "Pixel Data"
582 // is found by indirection through the "Image Location").
583 // Inside the group pointed by "Image Location" the searched element
584 // is conventionally the element 0x0010 (when the norm is respected).
585 // When the "Image Location" is absent we default to group 0x7fe0.
588 std::string ImageLocation = GetEntryByNumber(0x0028, 0x0200);
589 if ( ImageLocation == GDCM_UNFOUND ) {
592 grPixel = (guint16) atoi( ImageLocation.c_str() );
594 if (grPixel != 0x7fe0)
595 // This is a kludge for old dirty Philips imager.
600 gdcmHeaderEntry* PixelElement = GetHeaderEntryByNumber(grPixel,numPixel);
602 return PixelElement->GetLength();
608 * \ingroup gdcmHeader
609 * \brief tells us if LUT are used
610 * \warning Right now, Segmented xxx Palette Color Lookup Table Data
611 * \ are NOT considered as LUT, since nobody knows
612 *\ how to deal with them
613 * @return int acts as a Boolean
615 bool gdcmHeader::HasLUT(void) {
617 // Check the presence of the LUT Descriptors
618 if ( !GetHeaderEntryByNumber(0x0028,0x1101) )
620 // LutDescriptorGreen
621 if ( !GetHeaderEntryByNumber(0x0028,0x1102) )
624 if ( !GetHeaderEntryByNumber(0x0028,0x1103) )
628 if ( !GetHeaderEntryByNumber(0x0028,0x1201) )
630 if ( !GetHeaderEntryByNumber(0x0028,0x1202) )
632 if ( !GetHeaderEntryByNumber(0x0028,0x1203) )
638 * \ingroup gdcmHeader
639 * \brief gets the info from 0028,1101 : Lookup Table Desc-Red
641 * @return Lookup Table nBit
642 * \ when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
644 int gdcmHeader::GetLUTNbits(void) {
645 std::vector<std::string> tokens;
649 //Just hope Lookup Table Desc-Red = Lookup Table Desc-Red = Lookup Table Desc-Blue
650 // Consistency already checked in GetLUTLength
651 std::string LutDescription = GetEntryByNumber(0x0028,0x1101);
652 if (LutDescription == GDCM_UNFOUND)
654 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
655 Tokenize (LutDescription, tokens, "\\");
656 //LutLength=atoi(tokens[0].c_str());
657 //LutDepth=atoi(tokens[1].c_str());
658 LutNbits=atoi(tokens[2].c_str());
664 * \ingroup gdcmHeader
665 * \brief builts Red/Green/Blue/Alpha LUT from Header
666 * \ when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
667 * \ and (0028,1101),(0028,1102),(0028,1102)
668 * \ - xxx Palette Color Lookup Table Descriptor - are found
669 * \ and (0028,1201),(0028,1202),(0028,1202)
670 * \ - xxx Palette Color Lookup Table Data - are found
671 * \warning does NOT deal with :
672 * \ 0028 1100 Gray Lookup Table Descriptor (Retired)
673 * \ 0028 1221 Segmented Red Palette Color Lookup Table Data
674 * \ 0028 1222 Segmented Green Palette Color Lookup Table Data
675 * \ 0028 1223 Segmented Blue Palette Color Lookup Table Data
676 * \ no known Dicom reader deails with them :-(
677 * @return Lookup Table RGBA
679 unsigned char * gdcmHeader::GetLUTRGBA(void) {
681 // http://www.barre.nom.fr/medical/dicom2/limitations.html#Color%20Lookup%20Tables
682 // and OT-PAL-8-face.dcm
684 // if Photometric Interpretation # PALETTE COLOR, no LUT to be done
685 if (gdcmHeader::GetEntryByNumber(0x0028,0x0004) != "PALETTE COLOR ") {
689 int lengthR, debR, nbitsR;
690 int lengthG, debG, nbitsG;
691 int lengthB, debB, nbitsB;
693 // Get info from Lut Descriptors
694 // (the 3 LUT descriptors may be different)
695 std::string LutDescriptionR = GetEntryByNumber(0x0028,0x1101);
696 if (LutDescriptionR == GDCM_UNFOUND)
698 std::string LutDescriptionG = GetEntryByNumber(0x0028,0x1102);
699 if (LutDescriptionG == GDCM_UNFOUND)
701 std::string LutDescriptionB = GetEntryByNumber(0x0028,0x1103);
702 if (LutDescriptionB == GDCM_UNFOUND)
705 std::vector<std::string> tokens;
707 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
708 Tokenize (LutDescriptionR, tokens, "\\");
709 lengthR=atoi(tokens[0].c_str()); // Red LUT length in Bytes
710 debR =atoi(tokens[1].c_str()); // subscript of the first Lut Value
711 nbitsR =atoi(tokens[2].c_str()); // Lut item size (in Bits)
714 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
715 Tokenize (LutDescriptionG, tokens, "\\");
716 lengthG=atoi(tokens[0].c_str()); // Green LUT length in Bytes
717 debG =atoi(tokens[1].c_str());
718 nbitsG =atoi(tokens[2].c_str());
721 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
722 Tokenize (LutDescriptionB, tokens, "\\");
723 lengthB=atoi(tokens[0].c_str()); // Blue LUT length in Bytes
724 debB =atoi(tokens[1].c_str());
725 nbitsB =atoi(tokens[2].c_str());
728 // Load LUTs into memory, (as they were stored on disk)
729 unsigned char *lutR = (unsigned char *)
730 GetPubEntryVoidAreaByNumber(0x0028,0x1201);
731 unsigned char *lutG = (unsigned char *)
732 GetPubEntryVoidAreaByNumber(0x0028,0x1202);
733 unsigned char *lutB = (unsigned char *)
734 GetPubEntryVoidAreaByNumber(0x0028,0x1203);
736 if (!lutR || !lutG || !lutB ) {
739 // forge the 4 * 8 Bits Red/Green/Blue/Alpha LUT
741 unsigned char *LUTRGBA = (unsigned char *)calloc(1024,1); // 256 * 4 (R, G, B, Alpha)
745 memset(LUTRGBA, 0, 1024);
748 std::string str_nb = GetEntryByNumber(0x0028,0x0100);
749 if (str_nb == GDCM_UNFOUND ) {
752 nb = atoi(str_nb.c_str() );
756 if (nbitsR==16 && nb==8) // when LUT item size is different than pixel size
757 mult=2; // high byte must be = low byte
758 else // See PS 3.3-2003 C.11.1.1.2 p 619
761 // if we get a black image, let's just remove the '+1'
762 // from 'i*mult+1' and check again
763 // if it works, we shall have to check the 3 Palettes
764 // to see which byte is ==0 (first one, or second one)
766 // We give up the checking to avoid some overhead
771 for(i=0;i<lengthR;i++) {
776 for(i=0;i<lengthG;i++) {
781 for(i=0;i<lengthB;i++) {
787 *a = 1; // Alpha component
791 //How to free the now useless LUTs?
793 //free(LutR); free(LutB); free(LutG);
798 * \ingroup gdcmHeader
799 * \brief gets the info from 0002,0010 : Transfert Syntax
801 * @return Transfert Syntax Name (as oposite to Transfert Syntax UID)
803 std::string gdcmHeader::GetTransfertSyntaxName(void) {
804 // use the gdcmTS (TS : Transfert Syntax)
805 std::string TransfertSyntax = GetEntryByNumber(0x0002,0x0010);
806 if (TransfertSyntax == GDCM_UNFOUND) {
807 dbg.Verbose(0, "gdcmHeader::GetTransfertSyntaxName: unfound Transfert Syntax (0002,0010)");
808 return "Uncompressed ACR-NEMA";
810 // we do it only when we need it
811 gdcmTS * ts = gdcmGlobal::GetTS();
812 std::string tsName=ts->GetValue(TransfertSyntax);
813 //delete ts; // Seg Fault when deleted ?!
818 * \ingroup gdcmHeader
819 * \brief Searches within the public dictionary for element value of
821 * @param tagName name of the searched element.
822 * @return Corresponding element value when it exists, and the string
823 * GDCM_UNFOUND ("gdcm::Unfound") otherwise.
825 std::string gdcmHeader::GetPubEntryByName(std::string tagName) {
826 gdcmDictEntry *dictEntry = RefPubDict->GetTagByName(tagName);
827 if( dictEntry == NULL)
830 return(GetEntryByNumber(dictEntry->GetGroup(),dictEntry->GetElement()));
834 * \ingroup gdcmHeader
835 * \brief Searches within the elements parsed with the public dictionary for
836 * the element value representation of a given tag.
838 * Obtaining the VR (Value Representation) might be needed by caller
839 * to convert the string typed content to caller's native type
840 * (think of C++ vs Python). The VR is actually of a higher level
841 * of semantics than just the native C++ type.
842 * @param tagName name of the searched element.
843 * @return Corresponding element value representation when it exists,
844 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
846 std::string gdcmHeader::GetPubEntryVRByName(std::string tagName) {
847 gdcmDictEntry *dictEntry = RefPubDict->GetTagByName(tagName);
848 if( dictEntry == NULL)
851 gdcmHeaderEntry* elem = GetHeaderEntryByNumber(dictEntry->GetGroup(),
852 dictEntry->GetElement());
853 return elem->GetVR();
857 * \ingroup gdcmHeader
858 * \brief Searches within the public dictionary for element value of
860 * @param group Group of the researched tag.
861 * @param element Element of the researched tag.
862 * @return Corresponding element value when it exists, and the string
863 * GDCM_UNFOUND ("gdcm::Unfound") otherwise.
865 std::string gdcmHeader::GetPubEntryByNumber(guint16 group, guint16 element) {
866 return PubEntrySet.GetEntryByNumber(group, element);
870 * \ingroup gdcmHeader
871 * \brief Searches within the public dictionary for element value
872 * representation of a given tag.
874 * Obtaining the VR (Value Representation) might be needed by caller
875 * to convert the string typed content to caller's native type
876 * (think of C++ vs Python). The VR is actually of a higher level
877 * of semantics than just the native C++ type.
878 * @param group Group of the researched tag.
879 * @param element Element of the researched tag.
880 * @return Corresponding element value representation when it exists,
881 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
883 std::string gdcmHeader::GetPubEntryVRByNumber(guint16 group, guint16 element) {
884 gdcmHeaderEntry* elem = GetHeaderEntryByNumber(group, element);
887 return elem->GetVR();
891 * \ingroup gdcmHeader
892 * \brief Accesses an existing gdcmHeaderEntry in the PubHeaderEntrySet of this instance
893 * through tag name and modifies it's content with the given value.
894 * @param content new value to substitute with
895 * @param tagName name of the tag to be modified
897 bool gdcmHeader::SetPubEntryByName(std::string content, std::string tagName) {
898 //return ( PubHeaderEntrySet.SetHeaderEntryByName (content, tagName) );
899 gdcmDictEntry *dictEntry = RefPubDict->GetTagByName(tagName);
900 if( dictEntry == NULL)
902 return(PubEntrySet.SetEntryByNumber(content,
903 dictEntry->GetGroup(),
904 dictEntry->GetElement()));
908 * \ingroup gdcmHeader
909 * \brief Accesses an existing gdcmHeaderEntry (i.e. a Dicom Element)
910 * in the PubHeaderEntrySet of this instance
911 * through it's (group, element) and modifies it's content with
913 * @param content new value to substitute with
914 * @param group group of the Dicom Element to modify
915 * @param element element of the Dicom Element to modify
917 bool gdcmHeader::SetPubEntryByNumber(std::string content, guint16 group,
920 //TODO : homogeneiser les noms : SetPubElValByNumber
921 // qui appelle PubHeaderEntrySet.SetHeaderEntryByNumber
922 // pourquoi pas SetPubHeaderEntryByNumber ??
924 return ( PubEntrySet.SetEntryByNumber (content, group, element) );
928 * \ingroup gdcmHeader
929 * \brief Accesses an existing gdcmHeaderEntry in the PubHeaderEntrySet of this instance
930 * through it's (group, element) and modifies it's length with
932 * \warning Use with extreme caution.
933 * @param length new length to substitute with
934 * @param group group of the ElVal to modify
935 * @param element element of the ElVal to modify
936 * @return 1 on success, 0 otherwise.
939 bool gdcmHeader::SetPubEntryLengthByNumber(guint32 length, guint16 group,
941 return ( PubEntrySet.SetEntryLengthByNumber (length, group, element) );
945 * \ingroup gdcmHeader
946 * \brief Searches within elements parsed with the public dictionary
947 * and then within the elements parsed with the shadow dictionary
948 * for the element value of a given tag.
949 * @param tagName name of the searched element.
950 * @return Corresponding element value when it exists,
951 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
953 std::string gdcmHeader::GetEntryByName(std::string tagName) {
954 return GetPubEntryByName(tagName);
958 * \ingroup gdcmHeader
959 * \brief Searches within elements parsed with the public dictionary
960 * and then within the elements parsed with the shadow dictionary
961 * for the element value representation of a given tag.
963 * Obtaining the VR (Value Representation) might be needed by caller
964 * to convert the string typed content to caller's native type
965 * (think of C++ vs Python). The VR is actually of a higher level
966 * of semantics than just the native C++ type.
967 * @param tagName name of the searched element.
968 * @return Corresponding element value representation when it exists,
969 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
971 std::string gdcmHeader::GetEntryVRByName(std::string tagName) {
972 return GetPubEntryVRByName(tagName);
976 * \ingroup gdcmHeader
977 * \brief Searches within elements parsed with the public dictionary
978 * and then within the elements parsed with the shadow dictionary
979 * for the element value of a given tag.
980 * @param group Group of the searched tag.
981 * @param element Element of the searched tag.
982 * @return Corresponding element value representation when it exists,
983 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
985 std::string gdcmHeader::GetEntryByNumber(guint16 group, guint16 element) {
986 return GetPubEntryByNumber(group, element);
990 * \ingroup gdcmHeader
991 * \brief Searches within elements parsed with the public dictionary
992 * and then within the elements parsed with the shadow dictionary
993 * for the element value representation of a given tag.
995 * Obtaining the VR (Value Representation) might be needed by caller
996 * to convert the string typed content to caller's native type
997 * (think of C++ vs Python). The VR is actually of a higher level
998 * of semantics than just the native C++ type.
999 * @param group Group of the searched tag.
1000 * @param element Element of the searched tag.
1001 * @return Corresponding element value representation when it exists,
1002 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
1004 std::string gdcmHeader::GetEntryVRByNumber(guint16 group, guint16 element) {
1005 return GetPubEntryVRByNumber(group, element);
1009 * \ingroup gdcmHeader
1010 * \brief Sets the value (string) of the target Dicom Element
1011 * @param content string value of the Dicom Element
1012 * @param tagName name of the searched Dicom Element.
1013 * @return true when found
1015 bool gdcmHeader::SetEntryByName(std::string content,std::string tagName) {
1016 gdcmDictEntry *dictEntry = RefPubDict->GetTagByName(tagName);
1017 if( dictEntry == NULL)
1021 TagKey key = gdcmDictEntry::TranslateToKey(dictEntry->GetGroup(),
1022 dictEntry->GetElement());
1023 if ( PubEntrySet.GetTagHT().count(key) == 0 )
1025 int l = content.length();
1026 if(l%2) { // Odd length are padded with a space (020H).
1028 content = content + '\0';
1031 //tagHt[key]->SetValue(content);
1032 gdcmHeaderEntry * a;
1034 TagHeaderEntryHT::iterator p2;
1035 // DO NOT remove the following lines : they explain the stuff
1036 //p= tagHt.equal_range(key); // get a pair of iterators first-last synonym
1037 //p2=p.first; // iterator on the first synonym
1038 //a=p2->second; // H Table target column (2-nd col)
1041 a = ((PubEntrySet.GetTagHT().equal_range(key)).first)->second;
1043 a-> SetValue(content);
1045 //std::string vr = tagHt[key]->GetVR();
1046 std::string vr = a->GetVR();
1049 if( (vr == "US") || (vr == "SS") )
1051 else if( (vr == "UL") || (vr == "SL") )
1055 //tagHt[key]->SetLength(lgr);
1061 * \ingroup gdcmHeader
1063 * @param exception_on_error
1066 FILE *gdcmHeader::OpenFile(bool exception_on_error)
1067 throw(gdcmFileError) {
1068 fp=fopen(filename.c_str(),"rb");
1069 if(exception_on_error) {
1071 throw gdcmFileError("gdcmHeader::gdcmHeader(const char *, bool)");
1076 fread(&zero, (size_t)2, (size_t)1, fp);
1078 //ACR -- or DICOM with no Preamble
1079 if( zero == 0x0008 || zero == 0x0800 || zero == 0x0002 || zero == 0x0200)
1083 fseek(fp, 126L, SEEK_CUR);
1085 fread(dicm, (size_t)4, (size_t)1, fp);
1086 if( memcmp(dicm, "DICM", 4) == 0 )
1090 dbg.Verbose(0, "gdcmHeader::gdcmHeader not DICOM/ACR", filename.c_str());
1093 dbg.Verbose(0, "gdcmHeader::gdcmHeader cannot open file", filename.c_str());
1099 * \ingroup gdcmHeader
1101 * @return TRUE if the close was successfull
1103 bool gdcmHeader::CloseFile(void) {
1104 int closed = fclose(fp);
1112 * \ingroup gdcmHeader
1113 * \brief Parses the header of the file but WITHOUT loading element values.
1115 void gdcmHeader::ParseHeader(bool exception_on_error) throw(gdcmFormatError) {
1116 gdcmHeaderEntry * newHeaderEntry = (gdcmHeaderEntry *)0;
1120 while ( (newHeaderEntry = ReadNextHeaderEntry()) ) {
1121 SkipHeaderEntry(newHeaderEntry);
1122 PubEntrySet.Add(newHeaderEntry);
1127 * \ingroup gdcmHeader
1129 * @return integer, acts as a Boolean
1131 bool gdcmHeader::Write(FILE * fp, FileType type) {
1133 // TODO : move the following lines (and a lot of others, to be written)
1134 // to a future function CheckAndCorrectHeader
1136 if (type == ImplicitVR) {
1137 std::string implicitVRTransfertSyntax = "1.2.840.10008.1.2";
1138 ReplaceOrCreateByNumber(implicitVRTransfertSyntax,0x0002, 0x0010);
1140 //FIXME Refer to standards on page 21, chapter 6.2 "Value representation":
1141 // values with a VR of UI shall be padded with a single trailing null
1142 // Dans le cas suivant on doit pader manuellement avec un 0
1144 PubEntrySet.SetEntryLengthByNumber(18, 0x0002, 0x0010);
1147 if (type == ExplicitVR) {
1148 std::string explicitVRTransfertSyntax = "1.2.840.10008.1.2.1";
1149 ReplaceOrCreateByNumber(explicitVRTransfertSyntax,0x0002, 0x0010);
1151 //FIXME Refer to standards on page 21, chapter 6.2 "Value representation":
1152 // values with a VR of UI shall be padded with a single trailing null
1153 // Dans le cas suivant on doit pader manuellement avec un 0
1155 PubEntrySet.SetEntryLengthByNumber(20, 0x0002, 0x0010);
1158 return PubEntrySet.Write(fp, type);
1163 * \brief Sets the Pixel Area size in the Header
1164 * --> not-for-rats function
1166 * \warning WARNING doit-etre etre publique ?
1167 * TODO : y aurait il un inconvenient à fusionner ces 2 fonctions
1169 * @param ImageDataSize new Pixel Area Size
1170 * warning : nothing else is checked
1172 void gdcmHeader::SetImageDataSize(size_t ImageDataSize) {
1173 std::string content1;
1175 // Assumes HeaderEntry (0x7fe0, 0x0010) exists ...
1176 sprintf(car,"%d",ImageDataSize);
1178 gdcmHeaderEntry *a = GetHeaderEntryByNumber(0x7fe0, 0x0010);
1179 a->SetLength(ImageDataSize);
1182 sprintf(car,"%d",ImageDataSize);
1184 SetPubEntryByNumber(content1, 0x7fe0, 0x0000);
1188 * \ingroup gdcmHeader
1193 * \return integer acts as a boolean
1195 bool gdcmHeader::ReplaceOrCreateByNumber(std::string Value,
1196 guint16 Group, guint16 Elem ) {
1197 // TODO : FIXME JPRx
1199 // on (je) cree une HeaderEntry ne contenant pas de valeur
1200 // on l'ajoute au HeaderEntrySet
1201 // on affecte une valeur a cette HeaderEntry a l'interieur du HeaderEntrySet
1202 // --> devrait pouvoir etre fait + simplement ???
1203 if (CheckIfExistByNumber(Group, Elem) == 0) {
1204 gdcmHeaderEntry* a =NewHeaderEntryByNumber(Group, Elem);
1209 PubEntrySet.SetEntryByNumber(Value, Group, Elem);
1214 * \ingroup gdcmHeader
1215 * \brief Modify (or Creates if not found) an element
1216 * @param Value new value
1219 * \return integer acts as a boolean
1222 bool gdcmHeader::ReplaceOrCreateByNumber(char* Value, guint16 Group, guint16 Elem ) {
1224 gdcmHeaderEntry* nvHeaderEntry=NewHeaderEntryByNumber(Group, Elem);
1225 // TODO : check if fails
1226 PubEntrySet.Add(nvHeaderEntry);
1227 std::string v = Value;
1228 PubEntrySet.SetEntryByNumber(v, Group, Elem);
1233 * \ingroup gdcmHeader
1234 * \brief Set a new value if the invoked element exists
1235 * Seems to be useless !!!
1239 * \return integer acts as a boolean
1241 bool gdcmHeader::ReplaceIfExistByNumber(char* Value, guint16 Group, guint16 Elem ) {
1243 //gdcmHeaderEntry* HeaderEntry = PubHeaderEntrySet.GetElementByNumber(Group, Elem);
1244 std::string v = Value;
1245 PubEntrySet.SetEntryByNumber(v, Group, Elem);
1250 * \ingroup gdcmHeader
1251 * \brief Swaps back the bytes of 4-byte long integer accordingly to
1253 * @return The properly swaped 32 bits integer.
1255 guint32 gdcmHeader::SwapLong(guint32 a) {
1260 a=( ((a<<24) & 0xff000000) | ((a<<8) & 0x00ff0000) |
1261 ((a>>8) & 0x0000ff00) | ((a>>24) & 0x000000ff) );
1265 a=( ((a<<16) & 0xffff0000) | ((a>>16) & 0x0000ffff) );
1269 a=( ((a<<8) & 0xff00ff00) | ((a>>8) & 0x00ff00ff) );
1272 dbg.Error(" gdcmHeader::SwapLong : unset swap code");
1279 * \ingroup gdcmHeader
1280 * \brief Swaps the bytes so they agree with the processor order
1281 * @return The properly swaped 16 bits integer.
1283 guint16 gdcmHeader::SwapShort(guint16 a) {
1284 if ( (sw==4321) || (sw==2143) )
1285 a =(((a<<8) & 0x0ff00) | ((a>>8)&0x00ff));
1289 //-----------------------------------------------------------------------------
1292 * \ingroup gdcmHeader
1297 gdcmHeaderEntry *gdcmHeader::GetHeaderEntryByNumber(guint16 Group, guint16 Elem) {
1298 gdcmHeaderEntry *HeaderEntry = PubEntrySet.GetHeaderEntryByNumber(Group, Elem);
1300 dbg.Verbose(1, "gdcmHeader::GetHeaderEntryByNumber",
1301 "failed to Locate gdcmHeaderEntry");
1308 * \ingroup gdcmHeader
1309 * \brief Searches within the public dictionary for a Dicom Element of
1311 * @param tagName name of the searched Dicom Element.
1312 * @return Corresponding Dicom Element when it exists, and NULL
1315 gdcmHeaderEntry *gdcmHeader::GetHeaderEntryByName(std::string tagName) {
1316 gdcmDictEntry *dictEntry = RefPubDict->GetTagByName(tagName);
1317 if( dictEntry == NULL)
1320 return(GetHeaderEntryByNumber(dictEntry->GetGroup(),dictEntry->GetElement()));
1324 * \ingroup gdcmHeader
1325 * \brief Checks if a given HeaderEntry (group,number)
1326 * \ exists in the Public HeaderEntrySet
1329 * @return integer acts as a boolean
1331 bool gdcmHeader::CheckIfExistByNumber(guint16 Group, guint16 Elem ) {
1332 return (PubEntrySet.CheckIfExistByNumber(Group, Elem)>0);
1336 * \ingroup gdcmHeader
1337 * \brief Gets (from Header) the offset of a 'non string' element value
1338 * \ (LoadElementValue has already be executed)
1341 * @return File Offset of the Element Value
1343 size_t gdcmHeader::GetPubEntryOffsetByNumber(guint16 Group, guint16 Elem) {
1344 gdcmHeaderEntry* Entry = GetHeaderEntryByNumber(Group, Elem);
1346 dbg.Verbose(1, "gdcmHeader::GetHeaderEntryByNumber",
1347 "failed to Locate gdcmHeaderEntry");
1350 return Entry->GetOffset();
1354 * \ingroup gdcmHeader
1355 * \brief Gets (from Header) a 'non string' element value
1356 * \ (LoadElementValue has already be executed)
1359 * @return Pointer to the 'non string' area
1361 void * gdcmHeader::GetPubEntryVoidAreaByNumber(guint16 Group, guint16 Elem) {
1362 gdcmHeaderEntry* Entry = GetHeaderEntryByNumber(Group, Elem);
1364 dbg.Verbose(1, "gdcmHeader::GetHeaderEntryByNumber",
1365 "failed to Locate gdcmHeaderEntry");
1368 return Entry->GetVoidArea();
1372 * \ingroup gdcmHeader
1373 * \brief Loads (from disk) the element content
1374 * when a string is not suitable
1376 void * gdcmHeader::LoadEntryVoidArea(guint16 Group, guint16 Elem) {
1377 gdcmHeaderEntry * Element= GetHeaderEntryByNumber(Group, Elem);
1380 size_t o =(size_t)Element->GetOffset();
1381 fseek(fp, o, SEEK_SET);
1382 int l=Element->GetLength();
1383 void * a = malloc(l);
1388 PubEntrySet.SetVoidAreaByNumber(a, Group, Elem);
1389 // TODO check the result
1390 size_t l2 = fread(a, 1, l ,fp);
1398 //-----------------------------------------------------------------------------
1401 * \ingroup gdcmHeader
1402 * \brief Loads the element values of all the elements present in the
1403 * public tag based hash table.
1405 void gdcmHeader::LoadHeaderEntries(void) {
1408 // We don't use any longer the HashTable, since a lot a stuff is missing
1409 // when SeQuences were encountered
1411 //TagHeaderEntryHT ht = PubHeaderEntrySet.GetTagHT();
1412 //for (TagHeaderEntryHT::iterator tag = ht.begin(); tag != ht.end(); ++tag) {
1413 // LoadElementValue(tag->second);
1416 for (ListTag::iterator i = GetPubListEntry().begin();
1417 i != GetPubListEntry().end();
1419 LoadHeaderEntry(*i);
1424 // Load 'non string' values
1425 std::string PhotometricInterpretation = GetPubEntryByNumber(0x0028,0x0004);
1426 if( PhotometricInterpretation == "PALETTE COLOR " ){
1427 LoadEntryVoidArea(0x0028,0x1200); // gray LUT
1428 LoadEntryVoidArea(0x0028,0x1201); // R LUT
1429 LoadEntryVoidArea(0x0028,0x1202); // G LUT
1430 LoadEntryVoidArea(0x0028,0x1203); // B LUT
1432 LoadEntryVoidArea(0x0028,0x1221); // Segmented Red Palette Color LUT Data
1433 LoadEntryVoidArea(0x0028,0x1222); // Segmented Green Palette Color LUT Data
1434 LoadEntryVoidArea(0x0028,0x1223); // Segmented Blue Palette Color LUT Data
1437 // --------------------------------------------------------------
1438 // Special Patch to allow gdcm to read ACR-LibIDO formated images
1440 // if recognition code tells us we deal with a LibIDO image
1441 // we switch lineNumber and columnNumber
1443 std::string RecCode;
1444 RecCode = GetPubEntryByNumber(0x0008, 0x0010);
1445 if (RecCode == "ACRNEMA_LIBIDO_1.1" ||
1446 RecCode == "CANRME_AILIBOD1_1." ) {
1447 filetype = ACR_LIBIDO;
1448 std::string rows = GetPubEntryByNumber(0x0028, 0x0010);
1449 std::string columns = GetPubEntryByNumber(0x0028, 0x0011);
1450 SetPubEntryByNumber(columns, 0x0028, 0x0010);
1451 SetPubEntryByNumber(rows , 0x0028, 0x0011);
1453 // ----------------- End of Special Patch ----------------
1457 * \ingroup gdcmHeader
1458 * \brief Loads the element content if it's length is not bigger
1459 * than the value specified with
1460 * gdcmHeader::SetMaxSizeLoadElementValue()
1461 * @param ElVal string value of the Dicom Element
1463 void gdcmHeader::LoadHeaderEntry(gdcmHeaderEntry * ElVal) {
1465 guint16 group = ElVal->GetGroup();
1466 std::string vr= ElVal->GetVR();
1467 guint32 length = ElVal->GetLength();
1468 bool SkipLoad = false;
1470 fseek(fp, (long)ElVal->GetOffset(), SEEK_SET);
1472 // the test was commented out to 'go inside' the SeQuences
1473 // we don't any longer skip them !
1478 // A SeQuence "contains" a set of Elements.
1479 // (fffe e000) tells us an Element is beginning
1480 // (fffe e00d) tells us an Element just ended
1481 // (fffe e0dd) tells us the current SeQuence just ended
1482 if( group == 0xfffe )
1486 ElVal->SetLength(0);
1487 ElVal->SetValue("gdcm::Skipped");
1491 // When the length is zero things are easy:
1492 if ( length == 0 ) {
1493 ElVal->SetValue("");
1497 // The elements whose length is bigger than the specified upper bound
1498 // are not loaded. Instead we leave a short notice of the offset of
1499 // the element content and it's length.
1500 if (length > MaxSizeLoadElementValue) {
1501 std::ostringstream s;
1502 s << "gdcm::NotLoaded.";
1503 s << " Address:" << (long)ElVal->GetOffset();
1504 s << " Length:" << ElVal->GetLength();
1505 s << " x(" << std::hex << ElVal->GetLength() << ")";
1506 ElVal->SetValue(s.str());
1510 // When an integer is expected, read and convert the following two or
1511 // four bytes properly i.e. as an integer as opposed to a string.
1513 // Actually, elements with Value Multiplicity > 1
1514 // contain a set of integers (not a single one)
1515 // Any compacter code suggested (?)
1516 if ( IsHeaderEntryAnInteger(ElVal) ) {
1518 std::ostringstream s;
1520 if (vr == "US" || vr == "SS") {
1522 NewInt = ReadInt16();
1525 for (int i=1; i < nbInt; i++) {
1527 NewInt = ReadInt16();
1532 } else if (vr == "UL" || vr == "SL") {
1534 NewInt = ReadInt32();
1537 for (int i=1; i < nbInt; i++) {
1539 NewInt = ReadInt32();
1544 #ifdef GDCM_NO_ANSI_STRING_STREAM
1545 s << std::ends; // to avoid oddities on Solaris
1546 #endif //GDCM_NO_ANSI_STRING_STREAM
1547 ElVal->SetValue(s.str());
1551 // We need an additional byte for storing \0 that is not on disk
1552 char* NewValue = (char*)malloc(length+1);
1554 dbg.Verbose(1, "LoadElementValue: Failed to allocate NewValue");
1557 NewValue[length]= 0;
1559 item_read = fread(NewValue, (size_t)length, (size_t)1, fp);
1560 if ( item_read != 1 ) {
1562 dbg.Verbose(1, "gdcmHeader::LoadElementValue","unread element value");
1563 ElVal->SetValue("gdcm::UnRead");
1566 ElVal->SetValue(NewValue);
1571 * \ingroup gdcmHeader
1572 * \brief Loads the element while preserving the current
1573 * underlying file position indicator as opposed to
1574 * to LoadElementValue that modifies it.
1575 * @param ElVal Element whose value shall be loaded.
1578 void gdcmHeader::LoadHeaderEntrySafe(gdcmHeaderEntry * ElVal) {
1579 long PositionOnEntry = ftell(fp);
1580 LoadHeaderEntry(ElVal);
1581 fseek(fp, PositionOnEntry, SEEK_SET);
1585 * \ingroup gdcmHeader
1590 void gdcmHeader::FindHeaderEntryLength (gdcmHeaderEntry * ElVal) {
1591 guint16 element = ElVal->GetElement();
1592 guint16 group = ElVal->GetGroup();
1593 std::string vr = ElVal->GetVR();
1595 if( (element == 0x0010) && (group == 0x7fe0) ) {
1597 dbg.Verbose(2, "gdcmHeader::FindLength: ",
1598 "we reached 7fe0 0010");
1601 if ( (filetype == ExplicitVR) && ! ElVal->IsImplicitVr() ) {
1602 if ( (vr=="OB") || (vr=="OW") || (vr=="SQ") || (vr=="UN") ) {
1604 // The following reserved two bytes (see PS 3.5-2001, section
1605 // 7.1.2 Data element structure with explicit vr p27) must be
1606 // skipped before proceeding on reading the length on 4 bytes.
1607 fseek(fp, 2L, SEEK_CUR);
1609 guint32 length32 = ReadInt32();
1611 if ( (vr == "OB") && (length32 == 0xffffffff) ) {
1612 ElVal->SetLength(FindHeaderEntryLengthOB());
1615 FixHeaderEntryFoundLength(ElVal, length32);
1619 // Length is encoded on 2 bytes.
1620 length16 = ReadInt16();
1622 // We can tell the current file is encoded in big endian (like
1623 // Data/US-RGB-8-epicard) when we find the "Transfer Syntax" tag
1624 // and it's value is the one of the encoding of a big endian file.
1625 // In order to deal with such big endian encoded files, we have
1626 // (at least) two strategies:
1627 // * when we load the "Transfer Syntax" tag with value of big endian
1628 // encoding, we raise the proper flags. Then we wait for the end
1629 // of the META group (0x0002) among which is "Transfer Syntax",
1630 // before switching the swap code to big endian. We have to postpone
1631 // the switching of the swap code since the META group is fully encoded
1632 // in little endian, and big endian coding only starts at the next
1633 // group. The corresponding code can be hard to analyse and adds
1634 // many additional unnecessary tests for regular tags.
1635 // * the second strategy consists in waiting for trouble, that shall
1636 // appear when we find the first group with big endian encoding. This
1637 // is easy to detect since the length of a "Group Length" tag (the
1638 // ones with zero as element number) has to be of 4 (0x0004). When we
1639 // encounter 1024 (0x0400) chances are the encoding changed and we
1640 // found a group with big endian encoding.
1641 // We shall use this second strategy. In order to make sure that we
1642 // can interpret the presence of an apparently big endian encoded
1643 // length of a "Group Length" without committing a big mistake, we
1644 // add an additional check: we look in the already parsed elements
1645 // for the presence of a "Transfer Syntax" whose value has to be "big
1646 // endian encoding". When this is the case, chances are we have got our
1647 // hands on a big endian encoded file: we switch the swap code to
1648 // big endian and proceed...
1649 if ( (element == 0x0000) && (length16 == 0x0400) ) {
1650 if ( ! IsExplicitVRBigEndianTransferSyntax() ) {
1651 dbg.Verbose(0, "gdcmHeader::FindLength", "not explicit VR");
1656 SwitchSwapToBigEndian();
1657 // Restore the unproperly loaded values i.e. the group, the element
1658 // and the dictionary entry depending on them.
1659 guint16 CorrectGroup = SwapShort(ElVal->GetGroup());
1660 guint16 CorrectElem = SwapShort(ElVal->GetElement());
1661 gdcmDictEntry * NewTag = GetDictEntryByNumber(CorrectGroup,
1664 // This correct tag is not in the dictionary. Create a new one.
1665 NewTag = new gdcmDictEntry(CorrectGroup, CorrectElem);
1667 // FIXME this can create a memory leaks on the old entry that be
1668 // left unreferenced.
1669 ElVal->SetDictEntry(NewTag);
1672 // Heuristic: well some files are really ill-formed.
1673 if ( length16 == 0xffff) {
1675 //dbg.Verbose(0, "gdcmHeader::FindLength",
1676 // "Erroneous element length fixed.");
1677 // Actually, length= 0xffff means that we deal with
1678 // Unknown Sequence Length
1681 FixHeaderEntryFoundLength(ElVal, (guint32)length16);
1685 // Either implicit VR or a non DICOM conformal (see not below) explicit
1686 // VR that ommited the VR of (at least) this element. Farts happen.
1687 // [Note: according to the part 5, PS 3.5-2001, section 7.1 p25
1688 // on Data elements "Implicit and Explicit VR Data Elements shall
1689 // not coexist in a Data Set and Data Sets nested within it".]
1690 // Length is on 4 bytes.
1691 FixHeaderEntryFoundLength(ElVal, ReadInt32());
1696 * \ingroup gdcmHeader
1697 * \brief Find the value representation of the current tag.
1700 void gdcmHeader::FindHeaderEntryVR( gdcmHeaderEntry *ElVal) {
1701 if (filetype != ExplicitVR)
1707 char msg[100]; // for sprintf. Sorry
1709 long PositionOnEntry = ftell(fp);
1710 // Warning: we believe this is explicit VR (Value Representation) because
1711 // we used a heuristic that found "UL" in the first tag. Alas this
1712 // doesn't guarantee that all the tags will be in explicit VR. In some
1713 // cases (see e-film filtered files) one finds implicit VR tags mixed
1714 // within an explicit VR file. Hence we make sure the present tag
1715 // is in explicit VR and try to fix things if it happens not to be
1717 bool RealExplicit = true;
1719 lgrLue=fread (&VR, (size_t)2,(size_t)1, fp);
1721 vr = std::string(VR);
1723 // Assume we are reading a falsely explicit VR file i.e. we reached
1724 // a tag where we expect reading a VR but are in fact we read the
1725 // first to bytes of the length. Then we will interogate (through find)
1726 // the dicom_vr dictionary with oddities like "\004\0" which crashes
1727 // both GCC and VC++ implementations of the STL map. Hence when the
1728 // expected VR read happens to be non-ascii characters we consider
1729 // we hit falsely explicit VR tag.
1731 if ( (!isalpha(VR[0])) && (!isalpha(VR[1])) )
1732 RealExplicit = false;
1734 // CLEANME searching the dicom_vr at each occurence is expensive.
1735 // PostPone this test in an optional integrity check at the end
1736 // of parsing or only in debug mode.
1737 if ( RealExplicit && !dicom_vr->Count(vr) )
1738 RealExplicit= false;
1740 if ( RealExplicit ) {
1741 if ( ElVal->IsVRUnknown() ) {
1742 // When not a dictionary entry, we can safely overwrite the vr.
1746 if ( ElVal->GetVR() == vr ) {
1747 // The vr we just read and the dictionary agree. Nothing to do.
1750 // The vr present in the file and the dictionary disagree. We assume
1751 // the file writer knew best and use the vr of the file. Since it would
1752 // be unwise to overwrite the vr of a dictionary (since it would
1753 // compromise it's next user), we need to clone the actual DictEntry
1754 // and change the vr for the read one.
1755 gdcmDictEntry* NewTag = new gdcmDictEntry(ElVal->GetGroup(),
1756 ElVal->GetElement(),
1760 ElVal->SetDictEntry(NewTag);
1764 // We thought this was explicit VR, but we end up with an
1765 // implicit VR tag. Let's backtrack.
1767 sprintf(msg,"Falsely explicit vr file (%04x,%04x)\n",
1768 ElVal->GetGroup(),ElVal->GetElement());
1769 dbg.Verbose(1, "gdcmHeader::FindVR: ",msg);
1771 fseek(fp, PositionOnEntry, SEEK_SET);
1772 // When this element is known in the dictionary we shall use, e.g. for
1773 // the semantics (see the usage of IsAnInteger), the vr proposed by the
1774 // dictionary entry. Still we have to flag the element as implicit since
1775 // we know now our assumption on expliciteness is not furfilled.
1777 if ( ElVal->IsVRUnknown() )
1778 ElVal->SetVR("Implicit");
1779 ElVal->SetImplicitVr();
1783 * \ingroup gdcmHeader
1788 void gdcmHeader::SkipHeaderEntry(gdcmHeaderEntry * ElVal) {
1789 SkipBytes(ElVal->GetLength());
1793 * \ingroup gdcmHeader
1794 * \brief When the length of an element value is obviously wrong (because
1795 * the parser went Jabberwocky) one can hope improving things by
1796 * applying this heuristic.
1798 void gdcmHeader::FixHeaderEntryFoundLength(gdcmHeaderEntry * ElVal, guint32 FoundLength) {
1800 ElVal->SetReadLength(FoundLength); // will be updated only if a bug is found
1802 if ( FoundLength == 0xffffffff) {
1806 // Sorry for the patch!
1807 // XMedCom did the trick to read some nasty GE images ...
1808 else if (FoundLength == 13) {
1809 // The following 'if' will be removed when there is no more
1810 // images on Creatis HDs with a 13 length for Manufacturer...
1811 if ( (ElVal->GetGroup() != 0x0008) ||
1812 ( (ElVal->GetElement() != 0x0070) && (ElVal->GetElement() != 0x0080) ) ) {
1813 // end of remove area
1815 ElVal->SetReadLength(10); // a bug is to be fixed
1818 // to fix some garbage 'Leonardo' Siemens images
1819 // May be commented out to avoid overhead
1820 else if ( (ElVal->GetGroup() == 0x0009) &&
1821 ( (ElVal->GetElement() == 0x1113) || (ElVal->GetElement() == 0x1114) ) ){
1823 ElVal->SetReadLength(4); // a bug is to be fixed
1827 // to try to 'go inside' SeQuences (with length), and not to skip them
1828 else if ( ElVal->GetVR() == "SQ") {
1829 if (enableSequences) // only if the user does want to !
1833 ElVal->SetUsableLength(FoundLength);
1837 * \ingroup gdcmHeader
1838 * \brief Apply some heuristics to predict wether the considered
1839 * element value contains/represents an integer or not.
1840 * @param ElVal The element value on which to apply the predicate.
1841 * @return The result of the heuristical predicate.
1843 bool gdcmHeader::IsHeaderEntryAnInteger(gdcmHeaderEntry * ElVal) {
1844 guint16 element = ElVal->GetElement();
1845 guint16 group = ElVal->GetGroup();
1846 std::string vr = ElVal->GetVR();
1847 guint32 length = ElVal->GetLength();
1849 // When we have some semantics on the element we just read, and if we
1850 // a priori know we are dealing with an integer, then we shall be
1851 // able to swap it's element value properly.
1852 if ( element == 0 ) { // This is the group length of the group
1856 std::ostringstream s;
1857 s << "Erroneous Group Length element length on :" \
1858 << std::hex << group << " , " << element;
1859 dbg.Error("gdcmHeader::IsAnInteger",
1863 if ( (vr == "UL") || (vr == "US") || (vr == "SL") || (vr == "SS") )
1870 * \ingroup gdcmHeader
1875 guint32 gdcmHeader::FindHeaderEntryLengthOB(void) {
1876 // See PS 3.5-2001, section A.4 p. 49 on encapsulation of encoded pixel data.
1879 long PositionOnEntry = ftell(fp);
1880 bool FoundSequenceDelimiter = false;
1881 guint32 TotalLength = 0;
1884 while ( ! FoundSequenceDelimiter) {
1889 TotalLength += 4; // We even have to decount the group and element
1891 if ( g != 0xfffe && g!=0xb00c ) /*for bogus header */ {
1892 char msg[100]; // for sprintf. Sorry
1893 sprintf(msg,"wrong group (%04x) for an item sequence (%04x,%04x)\n",g, g,n);
1894 dbg.Verbose(1, "gdcmHeader::FindLengthOB: ",msg);
1898 if ( n == 0xe0dd || ( g==0xb00c && n==0x0eb6 ) ) /* for bogus header */
1899 FoundSequenceDelimiter = true;
1900 else if ( n != 0xe000 ){
1901 char msg[100]; // for sprintf. Sorry
1902 sprintf(msg,"wrong element (%04x) for an item sequence (%04x,%04x)\n",
1904 dbg.Verbose(1, "gdcmHeader::FindLengthOB: ",msg);
1908 ItemLength = ReadInt32();
1909 TotalLength += ItemLength + 4; // We add 4 bytes since we just read
1910 // the ItemLength with ReadInt32
1911 SkipBytes(ItemLength);
1913 fseek(fp, PositionOnEntry, SEEK_SET);
1918 * \ingroup gdcmHeader
1919 * \brief Reads a supposed to be 16 Bits integer
1920 * \ (swaps it depending on processor endianity)
1922 * @return integer acts as a boolean
1924 guint16 gdcmHeader::ReadInt16(void) {
1927 item_read = fread (&g, (size_t)2,(size_t)1, fp);
1928 if ( item_read != 1 ) {
1929 // dbg.Verbose(0, "gdcmHeader::ReadInt16", " Failed to read :");
1931 // dbg.Verbose(0, "gdcmHeader::ReadInt16", " End of File encountered");
1933 dbg.Verbose(0, "gdcmHeader::ReadInt16", " File Error");
1943 * \ingroup gdcmHeader
1944 * \brief Reads a supposed to be 32 Bits integer
1945 * \ (swaps it depending on processor endianity)
1949 guint32 gdcmHeader::ReadInt32(void) {
1952 item_read = fread (&g, (size_t)4,(size_t)1, fp);
1953 if ( item_read != 1 ) {
1954 //dbg.Verbose(0, "gdcmHeader::ReadInt32", " Failed to read :");
1956 // dbg.Verbose(0, "gdcmHeader::ReadInt32", " End of File encountered");
1958 dbg.Verbose(0, "gdcmHeader::ReadInt32", " File Error");
1968 * \ingroup gdcmHeader
1973 void gdcmHeader::SkipBytes(guint32 NBytes) {
1974 //FIXME don't dump the returned value
1975 (void)fseek(fp, (long)NBytes, SEEK_CUR);
1979 * \ingroup gdcmHeader
1982 void gdcmHeader::Initialise(void) {
1983 dicom_vr = gdcmGlobal::GetVR();
1984 dicom_ts = gdcmGlobal::GetTS();
1985 Dicts = gdcmGlobal::GetDicts();
1986 RefPubDict = Dicts->GetDefaultPubDict();
1987 RefShaDict = (gdcmDict*)0;
1991 * \ingroup gdcmHeader
1992 * \brief Discover what the swap code is (among little endian, big endian,
1993 * bad little endian, bad big endian).
1996 void gdcmHeader::CheckSwap()
1998 // Fourth semantics:
2000 // ---> Warning : This fourth field is NOT part
2001 // of the 'official' Dicom Dictionnary
2002 // and should NOT be used.
2003 // (Not defined for all the groups
2004 // may be removed in a future release)
2007 // META Meta Information
2019 // NMI Nuclear Medicine
2021 // BFS Basic Film Session
2022 // BFB Basic Film Box
2023 // BIB Basic Image Box
2038 // The only guaranted way of finding the swap code is to find a
2039 // group tag since we know it's length has to be of four bytes i.e.
2040 // 0x00000004. Finding the swap code in then straigthforward. Trouble
2041 // occurs when we can't find such group...
2043 guint32 x=4; // x : for ntohs
2044 bool net2host; // true when HostByteOrder is the same as NetworkByteOrder
2048 char deb[HEADER_LENGTH_TO_READ];
2050 // First, compare HostByteOrder and NetworkByteOrder in order to
2051 // determine if we shall need to swap bytes (i.e. the Endian type).
2056 //cout << net2host << endl;
2058 // The easiest case is the one of a DICOM header, since it possesses a
2059 // file preamble where it suffice to look for the string "DICM".
2060 lgrLue = fread(deb, 1, HEADER_LENGTH_TO_READ, fp);
2063 if(memcmp(entCur, "DICM", (size_t)4) == 0) {
2064 dbg.Verbose(1, "gdcmHeader::CheckSwap:", "looks like DICOM Version3");
2065 // Next, determine the value representation (VR). Let's skip to the
2066 // first element (0002, 0000) and check there if we find "UL"
2067 // - or "OB" if the 1st one is (0002,0001) -,
2068 // in which case we (almost) know it is explicit VR.
2069 // WARNING: if it happens to be implicit VR then what we will read
2070 // is the length of the group. If this ascii representation of this
2071 // length happens to be "UL" then we shall believe it is explicit VR.
2072 // FIXME: in order to fix the above warning, we could read the next
2073 // element value (or a couple of elements values) in order to make
2074 // sure we are not commiting a big mistake.
2075 // We need to skip :
2076 // * the 128 bytes of File Preamble (often padded with zeroes),
2077 // * the 4 bytes of "DICM" string,
2078 // * the 4 bytes of the first tag (0002, 0000),or (0002, 0001)
2079 // i.e. a total of 136 bytes.
2082 // Use gdcmHeader::dicom_vr to test all the possibilities
2083 // instead of just checking for UL, OB and UI !?
2084 if( (memcmp(entCur, "UL", (size_t)2) == 0) ||
2085 (memcmp(entCur, "OB", (size_t)2) == 0) ||
2086 (memcmp(entCur, "UI", (size_t)2) == 0) )
2088 filetype = ExplicitVR;
2089 dbg.Verbose(1, "gdcmHeader::CheckSwap:",
2090 "explicit Value Representation");
2092 filetype = ImplicitVR;
2093 dbg.Verbose(1, "gdcmHeader::CheckSwap:",
2094 "not an explicit Value Representation");
2098 dbg.Verbose(1, "gdcmHeader::CheckSwap:",
2099 "HostByteOrder != NetworkByteOrder");
2102 dbg.Verbose(1, "gdcmHeader::CheckSwap:",
2103 "HostByteOrder = NetworkByteOrder");
2106 // Position the file position indicator at first tag (i.e.
2107 // after the file preamble and the "DICM" string).
2109 fseek (fp, 132L, SEEK_SET);
2113 // Alas, this is not a DicomV3 file and whatever happens there is no file
2114 // preamble. We can reset the file position indicator to where the data
2115 // is (i.e. the beginning of the file).
2116 dbg.Verbose(1, "gdcmHeader::CheckSwap:", "not a DICOM Version3 file");
2119 // Our next best chance would be to be considering a 'clean' ACR/NEMA file.
2120 // By clean we mean that the length of the first tag is written down.
2121 // If this is the case and since the length of the first group HAS to be
2122 // four (bytes), then determining the proper swap code is straightforward.
2125 // We assume the array of char we are considering contains the binary
2126 // representation of a 32 bits integer. Hence the following dirty
2128 s = *((guint32 *)(entCur));
2148 dbg.Verbose(0, "gdcmHeader::CheckSwap:",
2149 "ACR/NEMA unfound swap info (time to raise bets)");
2152 // We are out of luck. It is not a DicomV3 nor a 'clean' ACR/NEMA file.
2153 // It is time for despaired wild guesses. So, let's assume this file
2154 // happens to be 'dirty' ACR/NEMA, i.e. the length of the group is
2155 // not present. Then the only info we have is the net2host one.
2165 * \ingroup gdcmHeader
2168 void gdcmHeader::SwitchSwapToBigEndian(void) {
2169 dbg.Verbose(1, "gdcmHeader::SwitchSwapToBigEndian",
2170 "Switching to BigEndian mode.");
2188 * \ingroup gdcmHeader
2193 void gdcmHeader::SetMaxSizeLoadElementValue(long NewSize) {
2196 if ((guint32)NewSize >= (guint32)0xffffffff) {
2197 MaxSizeLoadElementValue = 0xffffffff;
2200 MaxSizeLoadElementValue = NewSize;
2204 * \ingroup gdcmHeader
2205 * \brief Searches both the public and the shadow dictionary (when they
2206 * exist) for the presence of the DictEntry with given
2207 * group and element. The public dictionary has precedence on the
2209 * @param group group of the searched DictEntry
2210 * @param element element of the searched DictEntry
2211 * @return Corresponding DictEntry when it exists, NULL otherwise.
2213 gdcmDictEntry * gdcmHeader::GetDictEntryByNumber(guint16 group,
2215 gdcmDictEntry * found = (gdcmDictEntry*)0;
2216 if (!RefPubDict && !RefShaDict) {
2217 dbg.Verbose(0, "gdcmHeader::GetDictEntry",
2218 "we SHOULD have a default dictionary");
2221 found = RefPubDict->GetTagByNumber(group, element);
2226 found = RefShaDict->GetTagByNumber(group, element);
2234 * \ingroup gdcmHeader
2235 * \brief Searches both the public and the shadow dictionary (when they
2236 * exist) for the presence of the DictEntry with given name.
2237 * The public dictionary has precedence on the shadow one.
2238 * @param Name name of the searched DictEntry
2239 * @return Corresponding DictEntry when it exists, NULL otherwise.
2241 gdcmDictEntry * gdcmHeader::GetDictEntryByName(std::string Name) {
2242 gdcmDictEntry * found = (gdcmDictEntry*)0;
2243 if (!RefPubDict && !RefShaDict) {
2244 dbg.Verbose(0, "gdcmHeader::GetDictEntry",
2245 "we SHOULD have a default dictionary");
2248 found = RefPubDict->GetTagByName(Name);
2253 found = RefShaDict->GetTagByName(Name);
2261 * \ingroup gdcmHeader
2262 * \brief Read the next tag but WITHOUT loading it's value
2263 * @return On succes the newly created HeaderEntry, NULL on failure.
2265 gdcmHeaderEntry * gdcmHeader::ReadNextHeaderEntry(void) {
2268 gdcmHeaderEntry * NewElVal;
2274 // We reached the EOF (or an error occured) and header parsing
2275 // has to be considered as finished.
2276 return (gdcmHeaderEntry *)0;
2278 NewElVal = NewHeaderEntryByNumber(g, n);
2279 FindHeaderEntryVR(NewElVal);
2280 FindHeaderEntryLength(NewElVal);
2284 return (gdcmHeaderEntry *)0;
2286 NewElVal->SetOffset(ftell(fp));
2287 //if ( (g==0x7fe0) && (n==0x0010) )
2292 * \ingroup gdcmHeader
2293 * \brief Build a new Element Value from all the low level arguments.
2294 * Check for existence of dictionary entry, and build
2295 * a default one when absent.
2296 * @param Name Name of the underlying DictEntry
2298 gdcmHeaderEntry* gdcmHeader::NewHeaderEntryByName(std::string Name) {
2300 gdcmDictEntry * NewTag = GetDictEntryByName(Name);
2302 NewTag = new gdcmDictEntry(0xffff, 0xffff, "LO", "Unknown", Name);
2304 gdcmHeaderEntry* NewElVal = new gdcmHeaderEntry(NewTag);
2306 dbg.Verbose(1, "gdcmHeader::ObtainHeaderEntryByName",
2307 "failed to allocate gdcmHeaderEntry");
2308 return (gdcmHeaderEntry*)0;
2314 * \ingroup gdcmHeader
2315 * \brief Build a new Element Value from all the low level arguments.
2316 * Check for existence of dictionary entry, and build
2317 * a default one when absent.
2318 * @param Group group of the underlying DictEntry
2319 * @param Elem element of the underlying DictEntry
2321 gdcmHeaderEntry* gdcmHeader::NewHeaderEntryByNumber(guint16 Group, guint16 Elem) {
2322 // Find out if the tag we encountered is in the dictionaries:
2323 gdcmDictEntry * NewTag = GetDictEntryByNumber(Group, Elem);
2325 NewTag = new gdcmDictEntry(Group, Elem);
2327 gdcmHeaderEntry* NewElVal = new gdcmHeaderEntry(NewTag);
2329 dbg.Verbose(1, "gdcmHeader::NewHeaderEntryByNumber",
2330 "failed to allocate gdcmHeaderEntry");
2331 return (gdcmHeaderEntry*)0;
2337 * \ingroup gdcmHeader
2338 * \brief Small utility function that creates a new manually crafted
2339 * (as opposed as read from the file) gdcmHeaderEntry with user
2340 * specified name and adds it to the public tag hash table.
2341 * \note A fake TagKey is generated so the PubDict can keep it's coherence.
2342 * @param NewTagName The name to be given to this new tag.
2343 * @param VR The Value Representation to be given to this new tag.
2344 * @ return The newly hand crafted Element Value.
2346 gdcmHeaderEntry* gdcmHeader::NewManualHeaderEntryToPubDict(std::string NewTagName,
2348 gdcmHeaderEntry* NewElVal = (gdcmHeaderEntry*)0;
2349 guint32 StuffGroup = 0xffff; // Group to be stuffed with additional info
2350 guint32 FreeElem = 0;
2351 gdcmDictEntry* NewEntry = (gdcmDictEntry*)0;
2353 FreeElem = PubEntrySet.GenerateFreeTagKeyInGroup(StuffGroup);
2354 if (FreeElem == UINT32_MAX) {
2355 dbg.Verbose(1, "gdcmHeader::NewManualElValToPubDict",
2356 "Group 0xffff in Public Dict is full");
2357 return (gdcmHeaderEntry*)0;
2359 NewEntry = new gdcmDictEntry(StuffGroup, FreeElem,
2360 VR, "GDCM", NewTagName);
2361 NewElVal = new gdcmHeaderEntry(NewEntry);
2362 PubEntrySet.Add(NewElVal);
2366 //-----------------------------------------------------------------------------