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
42 *\TODO : may be we need one more bool,
43 * to allow skipping the private elements while parsing the header
44 * in order to save space
46 gdcmHeader::gdcmHeader(const char *InFilename,
47 bool exception_on_error,
48 bool enable_sequences ) {
54 SetMaxSizeLoadElementValue(MAX_SIZE_LOAD_ELEMENT_VALUE);
55 filename = InFilename;
57 if ( !OpenFile(exception_on_error))
67 * @param exception_on_error
69 gdcmHeader::gdcmHeader(bool exception_on_error) {
70 SetMaxSizeLoadElementValue(MAX_SIZE_LOAD_ELEMENT_VALUE);
76 * \brief Canonical destructor.
78 gdcmHeader::~gdcmHeader (void) {
79 dicom_vr = (gdcmVR*)0;
80 Dicts = (gdcmDictSet*)0;
81 RefPubDict = (gdcmDict*)0;
82 RefShaDict = (gdcmDict*)0;
86 //-----------------------------------------------------------------------------
91 * \brief Prints the Header Entries (Dicom Elements)
92 * both from the H Table and the chained list
95 void gdcmHeader::PrintPubEntry(std::ostream & os) {
96 PubEntrySet.Print(os);
100 * \ingroup gdcmHeader
101 * \brief Prints The Dict Entries of THE public Dicom Dictionnry
104 void gdcmHeader::PrintPubDict(std::ostream & os) {
105 RefPubDict->Print(os);
108 //-----------------------------------------------------------------------------
111 * \ingroup gdcmHeader
112 * \brief This predicate, based on hopefully reasonable heuristics,
113 * decides whether or not the current gdcmHeader was properly parsed
114 * and contains the mandatory information for being considered as
115 * a well formed and usable Dicom/Acr File.
116 * @return true when gdcmHeader is the one of a reasonable Dicom/Acr file,
119 bool gdcmHeader::IsReadable(void) {
120 std::string res = GetEntryByNumber(0x0028, 0x0005);
121 if ( res != GDCM_UNFOUND && atoi(res.c_str()) > 4 ) {
122 return false; // Image Dimensions
125 if ( !GetHeaderEntryByNumber(0x0028, 0x0100) )
126 return false; // "Bits Allocated"
127 if ( !GetHeaderEntryByNumber(0x0028, 0x0101) )
128 return false; // "Bits Stored"
129 if ( !GetHeaderEntryByNumber(0x0028, 0x0102) )
130 return false; // "High Bit"
131 if ( !GetHeaderEntryByNumber(0x0028, 0x0103) )
132 return false; // "Pixel Representation"
137 * \ingroup gdcmHeader
138 * \brief Determines if the Transfer Syntax was already encountered
139 * and if it corresponds to a ImplicitVRLittleEndian one.
141 * @return True when ImplicitVRLittleEndian found. False in all other cases.
143 bool gdcmHeader::IsImplicitVRLittleEndianTransferSyntax(void) {
144 gdcmHeaderEntry *Element = GetHeaderEntryByNumber(0x0002, 0x0010);
147 LoadHeaderEntrySafe(Element);
149 std::string Transfer = Element->GetValue();
150 if ( Transfer == "1.2.840.10008.1.2" )
156 * \ingroup gdcmHeader
157 * \brief Determines if the Transfer Syntax was already encountered
158 * and if it corresponds to a ExplicitVRLittleEndian one.
160 * @return True when ExplicitVRLittleEndian found. False in all other cases.
162 bool gdcmHeader::IsExplicitVRLittleEndianTransferSyntax(void) {
163 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
166 LoadHeaderEntrySafe(Element);
168 std::string Transfer = Element->GetValue();
169 if ( Transfer == "1.2.840.10008.1.2.1" )
175 * \ingroup gdcmHeader
176 * \brief Determines if the Transfer Syntax was already encountered
177 * and if it corresponds to a DeflatedExplicitVRLittleEndian one.
179 * @return True when DeflatedExplicitVRLittleEndian found. False in all other cases.
181 bool gdcmHeader::IsDeflatedExplicitVRLittleEndianTransferSyntax(void) {
182 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
185 LoadHeaderEntrySafe(Element);
187 std::string Transfer = Element->GetValue();
188 if ( Transfer == "1.2.840.10008.1.2.1.99" )
194 * \ingroup gdcmHeader
195 * \brief Determines if the Transfer Syntax was already encountered
196 * and if it corresponds to a Explicit VR Big Endian one.
198 * @return True when big endian found. False in all other cases.
200 bool gdcmHeader::IsExplicitVRBigEndianTransferSyntax(void) {
201 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
204 LoadHeaderEntrySafe(Element);
206 std::string Transfer = Element->GetValue();
207 if ( Transfer == "1.2.840.10008.1.2.2" ) //1.2.2 ??? A verifier !
213 * \ingroup gdcmHeader
214 * \brief Determines if the Transfer Syntax was already encountered
215 * and if it corresponds to a JPEGBaseLineProcess1 one.
217 * @return True when JPEGBaseLineProcess1found. False in all other cases.
219 bool gdcmHeader::IsJPEGBaseLineProcess1TransferSyntax(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.50" )
232 * \ingroup gdcmHeader
233 * \brief Determines if the Transfer Syntax was already encountered
234 * and if it corresponds to a JPEGExtendedProcess2-4 one.
236 * @return True when JPEGExtendedProcess2-4 found. False in all other cases.
238 bool gdcmHeader::IsJPEGExtendedProcess2_4TransferSyntax(void) {
239 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
242 LoadHeaderEntrySafe(Element);
244 std::string Transfer = Element->GetValue();
245 if ( Transfer == "1.2.840.10008.1.2.4.51" )
251 * \ingroup gdcmHeader
252 * \brief Determines if the Transfer Syntax was already encountered
253 * and if it corresponds to a JPEGExtendeProcess3-5 one.
255 * @return True when JPEGExtendedProcess3-5 found. False in all other cases.
257 bool gdcmHeader::IsJPEGExtendedProcess3_5TransferSyntax(void) {
258 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
261 LoadHeaderEntrySafe(Element);
263 std::string Transfer = Element->GetValue();
264 if ( Transfer == "1.2.840.10008.1.2.4.52" )
270 * \ingroup gdcmHeader
271 * \brief Determines if the Transfer Syntax was already encountered
272 * and if it corresponds to a JPEGSpectralSelectionProcess6-8 one.
274 * @return True when JPEGSpectralSelectionProcess6-8 found. False in all
277 bool gdcmHeader::IsJPEGSpectralSelectionProcess6_8TransferSyntax(void) {
278 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
281 LoadHeaderEntrySafe(Element);
283 std::string Transfer = Element->GetValue();
284 if ( Transfer == "1.2.840.10008.1.2.4.53" )
290 * \ingroup gdcmHeader
291 * \brief Determines if the Transfer Syntax was already encountered
292 * and if it corresponds to a RLE Lossless one.
294 * @return True when RLE Lossless found. False in all
297 bool gdcmHeader::IsRLELossLessTransferSyntax(void) {
298 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
301 LoadHeaderEntrySafe(Element);
303 std::string Transfer = Element->GetValue();
304 if ( Transfer == "1.2.840.10008.1.2.5" )
310 * \ingroup gdcmHeader
311 * \brief Determines if Transfer Syntax was already encountered
312 * and if it corresponds to a JPEG Lossless one.
314 * @return True when RLE Lossless found. False in all
317 bool gdcmHeader::IsJPEGLossless(void) {
318 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
319 // faire qq chose d'intelligent a la place de ça
322 LoadHeaderEntrySafe(Element);
324 const char * Transfert = Element->GetValue().c_str();
325 if ( memcmp(Transfert+strlen(Transfert)-2 ,"70",2)==0) return true;
326 if ( memcmp(Transfert+strlen(Transfert)-2 ,"55",2)==0) return true;
327 if (Element->GetValue() == "1.2.840.10008.1.2.4.57") return true;
333 * \ingroup gdcmHeader
334 * \brief Determines if the Transfer Syntax was already encountered
335 * and if it corresponds to a JPEG200 one.0
337 * @return True when JPEG2000 (Lossly or LossLess) found. False in all
340 bool gdcmHeader::IsJPEG2000(void) {
341 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
344 LoadHeaderEntrySafe(Element);
346 std::string Transfer = Element->GetValue();
347 if ( (Transfer == "1.2.840.10008.1.2.4.90")
348 || (Transfer == "1.2.840.10008.1.2.4.91") )
354 * \ingroup gdcmHeader
355 * \brief Predicate for dicom version 3 file.
356 * @return True when the file is a dicom version 3.
358 bool gdcmHeader::IsDicomV3(void) {
359 // Checking if Transfert Syntax exists is enough
360 return (GetHeaderEntryByNumber(0x0002, 0x0010) != NULL);
364 * \ingroup gdcmHeader
365 * \brief returns the File Type
366 * (ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown)
369 FileType gdcmHeader::GetFileType(void) {
374 * \ingroup gdcmHeader
375 * \brief Retrieve the number of columns of image.
376 * @return The encountered size when found, 0 by default.
377 * 0 means the file is NOT USABLE. The caller will have to check
379 int gdcmHeader::GetXSize(void) {
380 // We cannot check for "Columns" because the "Columns" tag is present
381 // both in IMG (0028,0011) and OLY (6000,0011) sections of the dictionary.
382 std::string StrSize = GetEntryByNumber(0x0028,0x0011);
383 if (StrSize == GDCM_UNFOUND)
385 return atoi(StrSize.c_str());
389 * \ingroup gdcmHeader
390 * \brief Retrieve the number of lines of image.
391 * \warning The defaulted value is 1 as opposed to gdcmHeader::GetXSize()
392 * @return The encountered size when found, 1 by default
393 * (The file contains a Signal, not an Image).
395 int gdcmHeader::GetYSize(void) {
396 // We cannot check for "Rows" because the "Rows" tag is present
397 // both in IMG (0028,0010) and OLY (6000,0010) sections of the dictionary.
398 std::string StrSize = GetEntryByNumber(0x0028,0x0010);
399 if (StrSize != GDCM_UNFOUND)
400 return atoi(StrSize.c_str());
404 // The Rows (0028,0010) entry was optional for ACR/NEMA. It might
405 // hence be a signal (1d image). So we default to 1:
410 * \ingroup gdcmHeader
411 * \brief Retrieve the number of planes of volume or the number
412 * of frames of a multiframe.
413 * \warning When present we consider the "Number of Frames" as the third
414 * dimension. When absent we consider the third dimension as
415 * being the "Planes" tag content.
416 * @return The encountered size when found, 1 by default (single image).
418 int gdcmHeader::GetZSize(void) {
419 // Both DicomV3 and ACR/Nema consider the "Number of Frames"
420 // as the third dimension.
421 std::string StrSize = GetEntryByNumber(0x0028,0x0008);
422 if (StrSize != GDCM_UNFOUND)
423 return atoi(StrSize.c_str());
425 // We then consider the "Planes" entry as the third dimension [we
426 // cannot retrieve by name since "Planes tag is present both in
427 // IMG (0028,0012) and OLY (6000,0012) sections of the dictionary].
428 StrSize = GetEntryByNumber(0x0028,0x0012);
429 if (StrSize != GDCM_UNFOUND)
430 return atoi(StrSize.c_str());
435 * \ingroup gdcmHeader
436 * \brief Retrieve the number of Bits Stored (actually used)
437 * (as opposite to number of Bits Allocated)
439 * @return The encountered number of Bits Stored, 0 by default.
440 * 0 means the file is NOT USABLE. The caller has to check it !
442 int gdcmHeader::GetBitsStored(void) {
443 std::string StrSize = GetEntryByNumber(0x0028,0x0101);
444 if (StrSize == GDCM_UNFOUND)
445 return 0; // It's supposed to be mandatory
446 // the caller will have to check
447 return atoi(StrSize.c_str());
451 * \ingroup gdcmHeader
452 * \brief Retrieve the number of Bits Allocated
453 * (8, 12 -compacted ACR-NEMA files, 16, ...)
455 * @return The encountered number of Bits Allocated, 0 by default.
456 * 0 means the file is NOT USABLE. The caller has to check it !
458 int gdcmHeader::GetBitsAllocated(void) {
459 std::string StrSize = GetEntryByNumber(0x0028,0x0100);
460 if (StrSize == GDCM_UNFOUND)
461 return 0; // It's supposed to be mandatory
462 // the caller will have to check
463 return atoi(StrSize.c_str());
467 * \ingroup gdcmHeader
468 * \brief Retrieve the number of Samples Per Pixel
469 * (1 : gray level, 3 : RGB -1 or 3 Planes-)
471 * @return The encountered number of Samples Per Pixel, 1 by default.
472 * (Gray level Pixels)
474 int gdcmHeader::GetSamplesPerPixel(void) {
475 std::string StrSize = GetEntryByNumber(0x0028,0x0002);
476 if (StrSize == GDCM_UNFOUND)
477 return 1; // Well, it's supposed to be mandatory ...
478 // but sometimes it's missing : we assume Gray pixels
479 return atoi(StrSize.c_str());
483 * \ingroup gdcmHeader
484 * \brief Retrieve the Planar Configuration for RGB images
485 * (0 : RGB Pixels , 1 : R Plane + G Plane + B Plane)
487 * @return The encountered Planar Configuration, 0 by default.
489 int gdcmHeader::GetPlanarConfiguration(void) {
490 std::string StrSize = GetEntryByNumber(0x0028,0x0006);
491 if (StrSize == GDCM_UNFOUND)
493 return atoi(StrSize.c_str());
497 * \ingroup gdcmHeader
498 * \brief Return the size (in bytes) of a single pixel of data.
499 * @return The size in bytes of a single pixel of data; 0 by default
500 * 0 means the file is NOT USABLE; the caller will have to check
502 int gdcmHeader::GetPixelSize(void) {
503 std::string PixelType = GetPixelType();
504 if (PixelType == "8U" || PixelType == "8S")
506 if (PixelType == "16U" || PixelType == "16S")
508 if (PixelType == "32U" || PixelType == "32S")
510 dbg.Verbose(0, "gdcmHeader::GetPixelSize: Unknown pixel type");
515 * \ingroup gdcmHeader
516 * \brief Build the Pixel Type of the image.
517 * Possible values are:
518 * - 8U unsigned 8 bit,
520 * - 16U unsigned 16 bit,
521 * - 16S signed 16 bit,
522 * - 32U unsigned 32 bit,
523 * - 32S signed 32 bit,
524 * \warning 12 bit images appear as 16 bit.
525 * \ 24 bit images appear as 8 bit
526 * @return 0S if nothing found. NOT USABLE file. The caller has to check
528 std::string gdcmHeader::GetPixelType(void) {
529 std::string BitsAlloc = GetEntryByNumber(0x0028, 0x0100); // Bits Allocated
530 if (BitsAlloc == GDCM_UNFOUND) {
531 dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Bits Allocated");
532 BitsAlloc = std::string("16");
534 if (BitsAlloc == "12") // It will be unpacked
535 BitsAlloc = std::string("16");
536 else if (BitsAlloc == "24") // (in order no to be messed up
537 BitsAlloc = std::string("8"); // by old RGB images)
539 std::string Signed = GetEntryByNumber(0x0028, 0x0103); // "Pixel Representation"
540 if (Signed == GDCM_UNFOUND) {
541 dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Pixel Representation");
542 BitsAlloc = std::string("0");
545 Signed = std::string("U");
547 Signed = std::string("S");
549 return( BitsAlloc + Signed);
553 * \ingroup gdcmHeader
554 * \brief Recover the offset (from the beginning of the file) of the pixels.
556 size_t gdcmHeader::GetPixelOffset(void) {
557 // If this file complies with the norm we should encounter the
558 // "Image Location" tag (0x0028, 0x0200). This tag contains the
559 // the group that contains the pixel data (hence the "Pixel Data"
560 // is found by indirection through the "Image Location").
561 // Inside the group pointed by "Image Location" the searched element
562 // is conventionally the element 0x0010 (when the norm is respected).
563 // When the "Image Location" is absent we default to group 0x7fe0.
566 std::string ImageLocation = GetEntryByNumber(0x0028, 0x0200);
568 if ( ImageLocation == GDCM_UNFOUND ) { // Image Location
571 grPixel = (guint16) atoi( ImageLocation.c_str() );
573 if (grPixel != 0x7fe0)
574 // This is a kludge for old dirty Philips imager.
579 gdcmHeaderEntry* PixelElement = GetHeaderEntryByNumber(grPixel,numPixel);
581 return PixelElement->GetOffset();
587 * \ingroup gdcmHeader
588 * \brief Recover the pixel area length (in Bytes)
589 * @return 0 by default. NOT USABLE file. The caller has to check.
591 size_t gdcmHeader::GetPixelAreaLength(void) {
592 // If this file complies with the norm we should encounter the
593 // "Image Location" tag (0x0028, 0x0200). This tag contains the
594 // the group that contains the pixel data (hence the "Pixel Data"
595 // is found by indirection through the "Image Location").
596 // Inside the group pointed by "Image Location" the searched element
597 // is conventionally the element 0x0010 (when the norm is respected).
598 // When the "Image Location" is absent we default to group 0x7fe0.
601 std::string ImageLocation = GetEntryByNumber(0x0028, 0x0200);
602 if ( ImageLocation == GDCM_UNFOUND ) {
605 grPixel = (guint16) atoi( ImageLocation.c_str() );
607 if (grPixel != 0x7fe0)
608 // This is a kludge for old dirty Philips imager.
613 gdcmHeaderEntry* PixelElement = GetHeaderEntryByNumber(grPixel,numPixel);
615 return PixelElement->GetLength();
621 * \ingroup gdcmHeader
622 * \brief tells us if LUT are used
623 * \warning Right now, 'Segmented xxx Palette Color Lookup Table Data'
624 * \ are NOT considered as LUT, since nobody knows
625 * \ how to deal with them
628 bool gdcmHeader::HasLUT(void) {
630 // Check the presence of the LUT Descriptors
631 if ( !GetHeaderEntryByNumber(0x0028,0x1101) )
633 // LutDescriptorGreen
634 if ( !GetHeaderEntryByNumber(0x0028,0x1102) )
637 if ( !GetHeaderEntryByNumber(0x0028,0x1103) )
641 if ( !GetHeaderEntryByNumber(0x0028,0x1201) )
643 if ( !GetHeaderEntryByNumber(0x0028,0x1202) )
645 if ( !GetHeaderEntryByNumber(0x0028,0x1203) )
651 * \ingroup gdcmHeader
652 * \brief gets the info from 0028,1101 : Lookup Table Desc-Red
654 * @return Lookup Table number of Bits , 0 by default
655 * \ when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
657 int gdcmHeader::GetLUTNbits(void) {
658 std::vector<std::string> tokens;
662 //Just hope Lookup Table Desc-Red = Lookup Table Desc-Red = Lookup Table Desc-Blue
663 // Consistency already checked in GetLUTLength
664 std::string LutDescription = GetEntryByNumber(0x0028,0x1101);
665 if (LutDescription == GDCM_UNFOUND)
667 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
668 Tokenize (LutDescription, tokens, "\\");
669 //LutLength=atoi(tokens[0].c_str());
670 //LutDepth=atoi(tokens[1].c_str());
671 LutNbits=atoi(tokens[2].c_str());
677 * \ingroup gdcmHeader
678 * \brief builts Red/Green/Blue/Alpha LUT from Header
679 * \ when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
680 * \ and (0028,1101),(0028,1102),(0028,1102)
681 * \ - xxx Palette Color Lookup Table Descriptor - are found
682 * \ and (0028,1201),(0028,1202),(0028,1202)
683 * \ - xxx Palette Color Lookup Table Data - are found
684 * \warning does NOT deal with :
685 * \ 0028 1100 Gray Lookup Table Descriptor (Retired)
686 * \ 0028 1221 Segmented Red Palette Color Lookup Table Data
687 * \ 0028 1222 Segmented Green Palette Color Lookup Table Data
688 * \ 0028 1223 Segmented Blue Palette Color Lookup Table Data
689 * \ no known Dicom reader deals with them :-(
690 * @return a RGBA Lookup Table
692 unsigned char * gdcmHeader::GetLUTRGBA(void) {
694 // http://www.barre.nom.fr/medical/dicom2/limitations.html#Color%20Lookup%20Tables
696 // if Photometric Interpretation # PALETTE COLOR, no LUT to be done
697 if (gdcmHeader::GetEntryByNumber(0x0028,0x0004) != "PALETTE COLOR ") {
700 int lengthR, debR, nbitsR;
701 int lengthG, debG, nbitsG;
702 int lengthB, debB, nbitsB;
704 // Get info from Lut Descriptors
705 // (the 3 LUT descriptors may be different)
706 std::string LutDescriptionR = GetEntryByNumber(0x0028,0x1101);
707 if (LutDescriptionR == GDCM_UNFOUND)
709 std::string LutDescriptionG = GetEntryByNumber(0x0028,0x1102);
710 if (LutDescriptionG == GDCM_UNFOUND)
712 std::string LutDescriptionB = GetEntryByNumber(0x0028,0x1103);
713 if (LutDescriptionB == GDCM_UNFOUND)
716 std::vector<std::string> tokens;
718 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
719 Tokenize (LutDescriptionR, tokens, "\\");
720 lengthR=atoi(tokens[0].c_str()); // Red LUT length in Bytes
721 debR =atoi(tokens[1].c_str()); // subscript of the first Lut Value
722 nbitsR =atoi(tokens[2].c_str()); // Lut item size (in Bits)
725 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
726 Tokenize (LutDescriptionG, tokens, "\\");
727 lengthG=atoi(tokens[0].c_str()); // Green LUT length in Bytes
728 debG =atoi(tokens[1].c_str());
729 nbitsG =atoi(tokens[2].c_str());
732 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
733 Tokenize (LutDescriptionB, tokens, "\\");
734 lengthB=atoi(tokens[0].c_str()); // Blue LUT length in Bytes
735 debB =atoi(tokens[1].c_str());
736 nbitsB =atoi(tokens[2].c_str());
739 // Load LUTs into memory, (as they were stored on disk)
740 unsigned char *lutR = (unsigned char *)
741 GetPubEntryVoidAreaByNumber(0x0028,0x1201);
742 unsigned char *lutG = (unsigned char *)
743 GetPubEntryVoidAreaByNumber(0x0028,0x1202);
744 unsigned char *lutB = (unsigned char *)
745 GetPubEntryVoidAreaByNumber(0x0028,0x1203);
747 if (!lutR || !lutG || !lutB ) {
750 // forge the 4 * 8 Bits Red/Green/Blue/Alpha LUT
752 unsigned char *LUTRGBA = (unsigned char *)calloc(1024,1); // 256 * 4 (R, G, B, Alpha)
756 memset(LUTRGBA, 0, 1024);
759 std::string str_nb = GetEntryByNumber(0x0028,0x0100);
760 if (str_nb == GDCM_UNFOUND ) {
763 nb = atoi(str_nb.c_str() );
767 if (nbitsR==16 && nb==8) // when LUT item size is different than pixel size
768 mult=2; // high byte must be = low byte
769 else // See PS 3.3-2003 C.11.1.1.2 p 619
772 // if we get a black image, let's just remove the '+1'
773 // from 'i*mult+1' and check again
774 // if it works, we shall have to check the 3 Palettes
775 // to see which byte is ==0 (first one, or second one)
777 // We give up the checking to avoid some overhead
782 for(i=0;i<lengthR;i++) {
787 for(i=0;i<lengthG;i++) {
792 for(i=0;i<lengthB;i++) {
798 *a = 1; // Alpha component
802 //How to free the now useless LUTs?
804 //free(LutR); free(LutB); free(LutG); // Seg Fault when used
809 * \ingroup gdcmHeader
810 * \brief gets the info from 0002,0010 : Transfert Syntax
812 * @return Transfert Syntax Name (as oposite to Transfert Syntax UID)
814 std::string gdcmHeader::GetTransfertSyntaxName(void) {
815 // use the gdcmTS (TS : Transfert Syntax)
816 std::string TransfertSyntax = GetEntryByNumber(0x0002,0x0010);
817 if (TransfertSyntax == GDCM_UNFOUND) {
818 dbg.Verbose(0, "gdcmHeader::GetTransfertSyntaxName: unfound Transfert Syntax (0002,0010)");
819 return "Uncompressed ACR-NEMA";
821 // we do it only when we need it
822 gdcmTS * ts = gdcmGlobal::GetTS();
823 std::string tsName=ts->GetValue(TransfertSyntax);
824 //delete ts; // Seg Fault when deleted ?!
829 * \ingroup gdcmHeader
830 * \brief Searches within the file Header for element value of
832 * @param tagName name of the searched element.
833 * @return Corresponding element value when it exists, and the string
834 * GDCM_UNFOUND ("gdcm::Unfound") otherwise.
836 std::string gdcmHeader::GetPubEntryByName(std::string tagName) {
837 gdcmDictEntry *dictEntry = RefPubDict->GetTagByName(tagName);
838 if( dictEntry == NULL)
841 return(GetEntryByNumber(dictEntry->GetGroup(),dictEntry->GetElement()));
845 * \ingroup gdcmHeader
846 * \brief Searches within the elements parsed with the file Header for
847 * the element value representation of a given tag.
849 * Obtaining the VR (Value Representation) might be needed by caller
850 * to convert the string typed content to caller's native type
851 * (think of C++ vs Python). The VR is actually of a higher level
852 * of semantics than just the native C++ type.
853 * @param tagName name of the searched element.
854 * @return Corresponding element value representation when it exists,
855 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
857 std::string gdcmHeader::GetPubEntryVRByName(std::string tagName) {
858 gdcmDictEntry *dictEntry = RefPubDict->GetTagByName(tagName);
859 if( dictEntry == NULL)
862 gdcmHeaderEntry* elem = GetHeaderEntryByNumber(dictEntry->GetGroup(),
863 dictEntry->GetElement());
864 return elem->GetVR();
868 * \ingroup gdcmHeader
869 * \brief Searches within the public dictionary for element value of
871 * @param group Group of the researched tag.
872 * @param element Element of the researched tag.
873 * @return Corresponding element value when it exists, and the string
874 * GDCM_UNFOUND ("gdcm::Unfound") otherwise.
876 std::string gdcmHeader::GetPubEntryByNumber(guint16 group, guint16 element) {
877 return PubEntrySet.GetEntryByNumber(group, element);
881 * \ingroup gdcmHeader
882 * \brief Searches within the public dictionary for element value
883 * representation of a given tag.
885 * Obtaining the VR (Value Representation) might be needed by caller
886 * to convert the string typed content to caller's native type
887 * (think of C++ vs Python). The VR is actually of a higher level
888 * of semantics than just the native C++ type.
889 * @param group Group of the researched tag.
890 * @param element Element of the researched tag.
891 * @return Corresponding element value representation when it exists,
892 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
894 std::string gdcmHeader::GetPubEntryVRByNumber(guint16 group, guint16 element) {
895 gdcmHeaderEntry* elem = GetHeaderEntryByNumber(group, element);
898 return elem->GetVR();
902 * \ingroup gdcmHeader
903 * \brief Accesses an existing gdcmHeaderEntry (i.e. a Dicom Element)
904 * in the PubHeaderEntrySet of this instance
905 * through tag name and modifies it's content with the given value.
906 * @param content new value to substitute with
907 * @param tagName name of the Header Entry (Dicom Element) to be modified
909 bool gdcmHeader::SetPubEntryByName(std::string content, std::string tagName) {
910 //return ( PubHeaderEntrySet.SetHeaderEntryByName (content, tagName) );
911 gdcmDictEntry *dictEntry = RefPubDict->GetTagByName(tagName);
912 if( dictEntry == NULL)
914 return(PubEntrySet.SetEntryByNumber(content,
915 dictEntry->GetGroup(),
916 dictEntry->GetElement()));
920 * \ingroup gdcmHeader
921 * \brief Accesses an existing gdcmHeaderEntry (i.e. a Dicom Element)
922 * in the PubHeaderEntrySet of this instance
923 * through it's (group, element) and modifies it's content with
925 * @param content new value to substitute with
926 * @param group group of the Dicom Element to modify
927 * @param element element of the Dicom Element to modify
929 bool gdcmHeader::SetPubEntryByNumber(std::string content, guint16 group,
932 //TODO : homogeneiser les noms : SetPubElValByNumber
933 // qui appelle PubHeaderEntrySet.SetHeaderEntryByNumber
934 // pourquoi pas SetPubHeaderEntryByNumber ??
936 return ( PubEntrySet.SetEntryByNumber (content, group, element) );
940 * \ingroup gdcmHeader
941 * \brief Accesses an existing gdcmHeaderEntry (i.e. a Dicom Element)
942 * in the PubHeaderEntrySet of this instance
943 * through it's (group, element) and modifies it's length with
945 * \warning Use with extreme caution.
946 * @param length new length to substitute with
947 * @param group group of the ElVal to modify
948 * @param element element of the ElVal to modify
949 * @return 1 on success, 0 otherwise.
952 bool gdcmHeader::SetPubEntryLengthByNumber(guint32 length, guint16 group,
954 return ( PubEntrySet.SetEntryLengthByNumber (length, group, element) );
958 * \ingroup gdcmHeader
959 * \brief Searches within Header Entries (Dicom Elements) parsed with
960 * the public and private dictionaries
961 * for the element value of a given tag.
962 * @param tagName name of the searched element.
963 * @return Corresponding element value when it exists,
964 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
966 std::string gdcmHeader::GetEntryByName(std::string tagName) {
967 return GetPubEntryByName(tagName);
971 * \ingroup gdcmHeader
972 * \brief Searches within Header Entries (Dicom Elements) parsed with
973 * the public and private dictionaries
974 * for the element value representation of a given tag.
976 * Obtaining the VR (Value Representation) might be needed by caller
977 * to convert the string typed content to caller's native type
978 * (think of C++ vs Python). The VR is actually of a higher level
979 * of semantics than just the native C++ type.
980 * @param tagName name of the searched element.
981 * @return Corresponding element value representation when it exists,
982 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
984 std::string gdcmHeader::GetEntryVRByName(std::string tagName) {
985 return GetPubEntryVRByName(tagName);
989 * \ingroup gdcmHeader
990 * \brief Searches within Header Entries (Dicom Elements) parsed with
991 * the public and private dictionaries
992 * for the element value representation of a given tag.
993 * @param group Group of the searched tag.
994 * @param element Element of the searched tag.
995 * @return Corresponding element value representation when it exists,
996 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
998 std::string gdcmHeader::GetEntryByNumber(guint16 group, guint16 element) {
999 return GetPubEntryByNumber(group, element);
1003 * \ingroup gdcmHeader
1004 * \brief Searches within Header Entries (Dicom Elements) parsed with
1005 * the public and private dictionaries
1006 * for the element value representation of a given tag..
1008 * Obtaining the VR (Value Representation) might be needed by caller
1009 * to convert the string typed content to caller's native type
1010 * (think of C++ vs Python). The VR is actually of a higher level
1011 * of semantics than just the native C++ type.
1012 * @param group Group of the searched tag.
1013 * @param element Element of the searched tag.
1014 * @return Corresponding element value representation when it exists,
1015 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
1017 std::string gdcmHeader::GetEntryVRByNumber(guint16 group, guint16 element) {
1018 return GetPubEntryVRByNumber(group, element);
1022 * \ingroup gdcmHeader
1023 * \brief Sets the value (string) of the Header Entry (Dicom Element)
1024 * @param content string value of the Dicom Element
1025 * @param tagName name of the searched Dicom Element.
1026 * @return true when found
1028 bool gdcmHeader::SetEntryByName(std::string content,std::string tagName) {
1029 gdcmDictEntry *dictEntry = RefPubDict->GetTagByName(tagName);
1030 if( dictEntry == NULL)
1034 TagKey key = gdcmDictEntry::TranslateToKey(dictEntry->GetGroup(),
1035 dictEntry->GetElement());
1036 if ( PubEntrySet.GetTagHT().count(key) == 0 )
1038 int l = content.length();
1039 if(l%2) { // Odd length are padded with a space (020H).
1041 content = content + '\0';
1044 //tagHt[key]->SetValue(content);
1045 gdcmHeaderEntry * a;
1047 TagHeaderEntryHT::iterator p2;
1048 // DO NOT remove the following lines : they explain how the stuff works
1049 //p= tagHt.equal_range(key); // get a pair of iterators first-last synonym
1050 //p2=p.first; // iterator on the first synonym
1051 //a=p2->second; // H Table target column (2-nd col)
1053 a = ((PubEntrySet.GetTagHT().equal_range(key)).first)->second;
1054 a-> SetValue(content);
1055 std::string vr = a->GetVR();
1058 if( (vr == "US") || (vr == "SS") )
1060 else if( (vr == "UL") || (vr == "SL") )
1069 * \ingroup gdcmHeader
1070 * \brief opens the file
1071 * @param exception_on_error
1074 FILE *gdcmHeader::OpenFile(bool exception_on_error)
1075 throw(gdcmFileError) {
1076 fp=fopen(filename.c_str(),"rb");
1077 if(exception_on_error) {
1079 throw gdcmFileError("gdcmHeader::gdcmHeader(const char *, bool)");
1084 fread(&zero, (size_t)2, (size_t)1, fp);
1086 //ACR -- or DICOM with no Preamble --
1087 if( zero == 0x0008 || zero == 0x0800 || zero == 0x0002 || zero == 0x0200)
1091 fseek(fp, 126L, SEEK_CUR);
1093 fread(dicm, (size_t)4, (size_t)1, fp);
1094 if( memcmp(dicm, "DICM", 4) == 0 )
1098 dbg.Verbose(0, "gdcmHeader::gdcmHeader not DICOM/ACR", filename.c_str());
1101 dbg.Verbose(0, "gdcmHeader::gdcmHeader cannot open file", filename.c_str());
1107 * \ingroup gdcmHeader
1108 * \brief closes the file
1109 * @return TRUE if the close was successfull
1111 bool gdcmHeader::CloseFile(void) {
1112 int closed = fclose(fp);
1120 * \ingroup gdcmHeader
1121 * \brief Parses the header of the file but WITHOUT loading element values.
1123 void gdcmHeader::ParseHeader(bool exception_on_error) throw(gdcmFormatError) {
1124 gdcmHeaderEntry * newHeaderEntry = (gdcmHeaderEntry *)0;
1128 while ( (newHeaderEntry = ReadNextHeaderEntry()) ) {
1129 SkipHeaderEntry(newHeaderEntry);
1130 PubEntrySet.Add(newHeaderEntry);
1135 * \ingroup gdcmHeader
1137 * @param fp file pointer on an already open file
1138 * @param type file type ( ImplicitVR, ExplicitVR, ...)
1141 bool gdcmHeader::Write(FILE * fp, FileType type) {
1143 // TODO : move the following lines (and a lot of others, to be written)
1144 // to a future function CheckAndCorrectHeader
1146 if (type == ImplicitVR) {
1147 std::string implicitVRTransfertSyntax = "1.2.840.10008.1.2";
1148 ReplaceOrCreateByNumber(implicitVRTransfertSyntax,0x0002, 0x0010);
1150 //FIXME Refer to standards on page 21, chapter 6.2 "Value representation":
1151 // values with a VR of UI shall be padded with a single trailing null
1152 // Dans le cas suivant on doit pader manuellement avec un 0
1154 PubEntrySet.SetEntryLengthByNumber(18, 0x0002, 0x0010);
1157 if (type == ExplicitVR) {
1158 std::string explicitVRTransfertSyntax = "1.2.840.10008.1.2.1";
1159 ReplaceOrCreateByNumber(explicitVRTransfertSyntax,0x0002, 0x0010);
1161 //FIXME Refer to standards on page 21, chapter 6.2 "Value representation":
1162 // values with a VR of UI shall be padded with a single trailing null
1163 // Dans le cas suivant on doit pader manuellement avec un 0
1165 PubEntrySet.SetEntryLengthByNumber(20, 0x0002, 0x0010);
1168 return PubEntrySet.Write(fp, type);
1173 * \brief Sets the Pixel Area size in the Header
1174 * --> not-for-rats function
1176 * \warning WARNING doit-etre etre publique ?
1177 * TODO : y aurait il un inconvenient à fusionner ces 2 fonctions
1179 * @param ImageDataSize new Pixel Area Size
1180 * warning : nothing else is checked
1182 void gdcmHeader::SetImageDataSize(size_t ImageDataSize) {
1183 std::string content1;
1185 // Assumes HeaderEntry (0x7fe0, 0x0010) exists ...
1186 sprintf(car,"%d",ImageDataSize);
1188 gdcmHeaderEntry *a = GetHeaderEntryByNumber(0x7fe0, 0x0010);
1189 a->SetLength(ImageDataSize);
1192 sprintf(car,"%d",ImageDataSize);
1194 SetPubEntryByNumber(content1, 0x7fe0, 0x0000);
1198 * \ingroup gdcmHeader
1199 * \brief Modifies the value of a given Header Entry (Dicom Element)
1200 * if it exists; Creates it with the given value if it doesn't
1201 * @param Value passed as a std::string
1206 bool gdcmHeader::ReplaceOrCreateByNumber(std::string Value,
1207 guint16 Group, guint16 Elem ) {
1209 if (CheckIfExistByNumber(Group, Elem) == 0) {
1210 gdcmHeaderEntry* a =NewHeaderEntryByNumber(Group, Elem);
1215 PubEntrySet.SetEntryByNumber(Value, Group, Elem);
1220 * \ingroup gdcmHeader
1221 * \brief Modifies the value of a given Header Entry (Dicom Element)
1222 * if it exists; Creates it with the given value if it doesn't
1223 * @param Value passed as a char*
1229 bool gdcmHeader::ReplaceOrCreateByNumber(char* Value, guint16 Group, guint16 Elem ) {
1231 gdcmHeaderEntry* nvHeaderEntry=NewHeaderEntryByNumber(Group, Elem);
1232 // TODO : check if fails
1233 PubEntrySet.Add(nvHeaderEntry);
1234 std::string v = Value;
1235 PubEntrySet.SetEntryByNumber(v, Group, Elem);
1240 * \ingroup gdcmHeader
1241 * \brief Set a new value if the invoked element exists
1242 * Seems to be useless !!!
1246 * \return integer acts as a boolean
1248 bool gdcmHeader::ReplaceIfExistByNumber(char* Value, guint16 Group, guint16 Elem ) {
1250 std::string v = Value;
1251 PubEntrySet.SetEntryByNumber(v, Group, Elem);
1256 * \ingroup gdcmHeader
1257 * \brief Swaps back the bytes of 4-byte long integer accordingly to
1259 * @return The properly swaped 32 bits integer.
1261 guint32 gdcmHeader::SwapLong(guint32 a) {
1266 a=( ((a<<24) & 0xff000000) | ((a<<8) & 0x00ff0000) |
1267 ((a>>8) & 0x0000ff00) | ((a>>24) & 0x000000ff) );
1271 a=( ((a<<16) & 0xffff0000) | ((a>>16) & 0x0000ffff) );
1275 a=( ((a<<8) & 0xff00ff00) | ((a>>8) & 0x00ff00ff) );
1278 dbg.Error(" gdcmHeader::SwapLong : unset swap code");
1285 * \ingroup gdcmHeader
1286 * \brief Swaps the bytes so they agree with the processor order
1287 * @return The properly swaped 16 bits integer.
1289 guint16 gdcmHeader::SwapShort(guint16 a) {
1290 if ( (sw==4321) || (sw==2143) )
1291 a =(((a<<8) & 0x0ff00) | ((a>>8)&0x00ff));
1295 //-----------------------------------------------------------------------------
1298 * \ingroup gdcmHeader
1303 gdcmHeaderEntry *gdcmHeader::GetHeaderEntryByNumber(guint16 Group, guint16 Elem) {
1304 gdcmHeaderEntry *HeaderEntry = PubEntrySet.GetHeaderEntryByNumber(Group, Elem);
1306 dbg.Verbose(1, "gdcmHeader::GetHeaderEntryByNumber",
1307 "failed to Locate gdcmHeaderEntry");
1314 * \ingroup gdcmHeader
1315 * \brief Searches within the Header Entries for a Dicom Element of
1317 * @param tagName name of the searched Dicom Element.
1318 * @return Corresponding Dicom Element when it exists, and NULL
1321 gdcmHeaderEntry *gdcmHeader::GetHeaderEntryByName(std::string tagName) {
1322 gdcmDictEntry *dictEntry = RefPubDict->GetTagByName(tagName);
1323 if( dictEntry == NULL)
1326 return(GetHeaderEntryByNumber(dictEntry->GetGroup(),dictEntry->GetElement()));
1330 * \ingroup gdcmHeader
1331 * \brief Checks if a given HeaderEntry (group,number)
1332 * \ exists in the Public HeaderEntrySet
1337 bool gdcmHeader::CheckIfExistByNumber(guint16 Group, guint16 Elem ) {
1338 return (PubEntrySet.CheckIfExistByNumber(Group, Elem)>0);
1342 * \ingroup gdcmHeader
1343 * \brief Gets (from Header) the offset of a 'non string' element value
1344 * \ (LoadElementValues has already be executed)
1347 * @return File Offset of the Element Value
1349 size_t gdcmHeader::GetPubEntryOffsetByNumber(guint16 Group, guint16 Elem) {
1350 gdcmHeaderEntry* Entry = GetHeaderEntryByNumber(Group, Elem);
1352 dbg.Verbose(1, "gdcmHeader::GetHeaderEntryByNumber",
1353 "failed to Locate gdcmHeaderEntry");
1356 return Entry->GetOffset();
1360 * \ingroup gdcmHeader
1361 * \brief Gets (from Header) a 'non string' element value
1362 * \ (LoadElementValues has already be executed)
1365 * @return Pointer to the 'non string' area
1367 void * gdcmHeader::GetPubEntryVoidAreaByNumber(guint16 Group, guint16 Elem) {
1368 gdcmHeaderEntry* Entry = GetHeaderEntryByNumber(Group, Elem);
1370 dbg.Verbose(1, "gdcmHeader::GetHeaderEntryByNumber",
1371 "failed to Locate gdcmHeaderEntry");
1374 return Entry->GetVoidArea();
1378 * \ingroup gdcmHeader
1379 * \brief Loads (from disk) the element content
1380 * when a string is not suitable
1382 void * gdcmHeader::LoadEntryVoidArea(guint16 Group, guint16 Elem) {
1383 gdcmHeaderEntry * Element= GetHeaderEntryByNumber(Group, Elem);
1386 size_t o =(size_t)Element->GetOffset();
1387 fseek(fp, o, SEEK_SET);
1388 int l=Element->GetLength();
1389 void * a = malloc(l);
1394 PubEntrySet.SetVoidAreaByNumber(a, Group, Elem);
1395 // TODO check the result
1396 size_t l2 = fread(a, 1, l ,fp);
1404 //-----------------------------------------------------------------------------
1407 * \ingroup gdcmHeader
1408 * \brief Loads the element values of all the Header Entries pointed in the
1409 * public Chained List.
1411 void gdcmHeader::LoadHeaderEntries(void) {
1413 for (ListTag::iterator i = GetPubListEntry().begin();
1414 i != GetPubListEntry().end();
1416 LoadHeaderEntry(*i);
1421 // Load 'non string' values
1422 std::string PhotometricInterpretation = GetPubEntryByNumber(0x0028,0x0004);
1423 if( PhotometricInterpretation == "PALETTE COLOR " ){
1424 LoadEntryVoidArea(0x0028,0x1200); // gray LUT
1425 LoadEntryVoidArea(0x0028,0x1201); // R LUT
1426 LoadEntryVoidArea(0x0028,0x1202); // G LUT
1427 LoadEntryVoidArea(0x0028,0x1203); // B LUT
1429 LoadEntryVoidArea(0x0028,0x1221); // Segmented Red Palette Color LUT Data
1430 LoadEntryVoidArea(0x0028,0x1222); // Segmented Green Palette Color LUT Data
1431 LoadEntryVoidArea(0x0028,0x1223); // Segmented Blue Palette Color LUT Data
1434 // --------------------------------------------------------------
1435 // Special Patch to allow gdcm to read ACR-LibIDO formated images
1437 // if recognition code tells us we deal with a LibIDO image
1438 // we switch lineNumber and columnNumber
1440 std::string RecCode;
1441 RecCode = GetPubEntryByNumber(0x0008, 0x0010); // recognition code
1442 if (RecCode == "ACRNEMA_LIBIDO_1.1" ||
1443 RecCode == "CANRME_AILIBOD1_1." ) {
1444 filetype = ACR_LIBIDO;
1445 std::string rows = GetPubEntryByNumber(0x0028, 0x0010);
1446 std::string columns = GetPubEntryByNumber(0x0028, 0x0011);
1447 SetPubEntryByNumber(columns, 0x0028, 0x0010);
1448 SetPubEntryByNumber(rows , 0x0028, 0x0011);
1450 // ----------------- End of Special Patch ----------------
1454 * \ingroup gdcmHeader
1455 * \brief Loads the element content if it's length is not bigger
1456 * than the value specified with
1457 * gdcmHeader::SetMaxSizeLoadElementValue()
1458 * @param ElVal Header Entry (Dicom Element) to be dealt with
1460 void gdcmHeader::LoadHeaderEntry(gdcmHeaderEntry * ElVal) {
1462 guint16 group = ElVal->GetGroup();
1463 std::string vr= ElVal->GetVR();
1464 guint32 length = ElVal->GetLength();
1465 bool SkipLoad = false;
1467 fseek(fp, (long)ElVal->GetOffset(), SEEK_SET);
1469 // the test was commented out to 'go inside' the SeQuences
1470 // we don't any longer skip them !
1472 // if( vr == "SQ" ) // (DO NOT remove this comment)
1475 // A SeQuence "contains" a set of Elements.
1476 // (fffe e000) tells us an Element is beginning
1477 // (fffe e00d) tells us an Element just ended
1478 // (fffe e0dd) tells us the current SeQuence just ended
1479 if( group == 0xfffe )
1483 ElVal->SetLength(0);
1484 ElVal->SetValue("gdcm::Skipped");
1488 // When the length is zero things are easy:
1489 if ( length == 0 ) {
1490 ElVal->SetValue("");
1494 // The elements whose length is bigger than the specified upper bound
1495 // are not loaded. Instead we leave a short notice of the offset of
1496 // the element content and it's length.
1497 if (length > MaxSizeLoadElementValue) {
1498 std::ostringstream s;
1499 s << "gdcm::NotLoaded.";
1500 s << " Address:" << (long)ElVal->GetOffset();
1501 s << " Length:" << ElVal->GetLength();
1502 s << " x(" << std::hex << ElVal->GetLength() << ")";
1503 ElVal->SetValue(s.str());
1507 // When integer(s) are expected, read and convert the following
1508 // n *(two or four bytes)
1509 // properly i.e. as integers as opposed to a strings.
1510 // Elements with Value Multiplicity > 1
1511 // contain a set of integers (not a single one)
1513 // Any compacter code suggested (?)
1514 if ( IsHeaderEntryAnInteger(ElVal) ) {
1516 std::ostringstream s;
1518 if (vr == "US" || vr == "SS") {
1520 NewInt = ReadInt16();
1523 for (int i=1; i < nbInt; i++) {
1525 NewInt = ReadInt16();
1530 } else if (vr == "UL" || vr == "SL") {
1532 NewInt = ReadInt32();
1535 for (int i=1; i < nbInt; i++) {
1537 NewInt = ReadInt32();
1542 #ifdef GDCM_NO_ANSI_STRING_STREAM
1543 s << std::ends; // to avoid oddities on Solaris
1544 #endif //GDCM_NO_ANSI_STRING_STREAM
1545 ElVal->SetValue(s.str());
1549 // We need an additional byte for storing \0 that is not on disk
1550 char* NewValue = (char*)malloc(length+1);
1552 dbg.Verbose(1, "LoadElementValue: Failed to allocate NewValue");
1555 NewValue[length]= 0;
1557 item_read = fread(NewValue, (size_t)length, (size_t)1, fp);
1558 if ( item_read != 1 ) {
1560 dbg.Verbose(1, "gdcmHeader::LoadElementValue","unread element value");
1561 ElVal->SetValue("gdcm::UnRead");
1564 ElVal->SetValue(NewValue);
1569 * \ingroup gdcmHeader
1570 * \brief Loads the element while preserving the current
1571 * underlying file position indicator as opposed to
1572 * to LoadHeaderEntry that modifies it.
1573 * @param entry Header Entry whose value shall be loaded.
1576 void gdcmHeader::LoadHeaderEntrySafe(gdcmHeaderEntry * entry) {
1577 long PositionOnEntry = ftell(fp);
1578 LoadHeaderEntry(entry);
1579 fseek(fp, PositionOnEntry, SEEK_SET);
1583 * \ingroup gdcmHeader
1585 * @param entry Header Entry whose value shall be loaded.
1589 void gdcmHeader::FindHeaderEntryLength (gdcmHeaderEntry * ElVal) {
1590 guint16 element = ElVal->GetElement();
1591 guint16 group = ElVal->GetGroup();
1592 std::string vr = ElVal->GetVR();
1594 if( (element == 0x0010) && (group == 0x7fe0) ) {
1596 dbg.Verbose(2, "gdcmHeader::FindLength: ",
1597 "we reached 7fe0 0010");
1600 if ( (filetype == ExplicitVR) && ! ElVal->IsImplicitVr() ) {
1601 if ( (vr=="OB") || (vr=="OW") || (vr=="SQ") || (vr=="UN") ) {
1603 // The following reserved two bytes (see PS 3.5-2001, section
1604 // 7.1.2 Data element structure with explicit vr p27) must be
1605 // skipped before proceeding on reading the length on 4 bytes.
1606 fseek(fp, 2L, SEEK_CUR);
1608 guint32 length32 = ReadInt32();
1610 if ( (vr == "OB") && (length32 == 0xffffffff) ) {
1611 ElVal->SetLength(FindHeaderEntryLengthOB());
1614 FixHeaderEntryFoundLength(ElVal, length32);
1618 // Length is encoded on 2 bytes.
1619 length16 = ReadInt16();
1621 // We can tell the current file is encoded in big endian (like
1622 // Data/US-RGB-8-epicard) when we find the "Transfer Syntax" tag
1623 // and it's value is the one of the encoding of a big endian file.
1624 // In order to deal with such big endian encoded files, we have
1625 // (at least) two strategies:
1626 // * when we load the "Transfer Syntax" tag with value of big endian
1627 // encoding, we raise the proper flags. Then we wait for the end
1628 // of the META group (0x0002) among which is "Transfer Syntax",
1629 // before switching the swap code to big endian. We have to postpone
1630 // the switching of the swap code since the META group is fully encoded
1631 // in little endian, and big endian coding only starts at the next
1632 // group. The corresponding code can be hard to analyse and adds
1633 // many additional unnecessary tests for regular tags.
1634 // * the second strategy consists in waiting for trouble, that shall
1635 // appear when we find the first group with big endian encoding. This
1636 // is easy to detect since the length of a "Group Length" tag (the
1637 // ones with zero as element number) has to be of 4 (0x0004). When we
1638 // encounter 1024 (0x0400) chances are the encoding changed and we
1639 // found a group with big endian encoding.
1640 // We shall use this second strategy. In order to make sure that we
1641 // can interpret the presence of an apparently big endian encoded
1642 // length of a "Group Length" without committing a big mistake, we
1643 // add an additional check: we look in the already parsed elements
1644 // for the presence of a "Transfer Syntax" whose value has to be "big
1645 // endian encoding". When this is the case, chances are we have got our
1646 // hands on a big endian encoded file: we switch the swap code to
1647 // big endian and proceed...
1648 if ( (element == 0x0000) && (length16 == 0x0400) ) {
1649 if ( ! IsExplicitVRBigEndianTransferSyntax() ) {
1650 dbg.Verbose(0, "gdcmHeader::FindLength", "not explicit VR");
1655 SwitchSwapToBigEndian();
1656 // Restore the unproperly loaded values i.e. the group, the element
1657 // and the dictionary entry depending on them.
1658 guint16 CorrectGroup = SwapShort(ElVal->GetGroup());
1659 guint16 CorrectElem = SwapShort(ElVal->GetElement());
1660 gdcmDictEntry * NewTag = GetDictEntryByNumber(CorrectGroup,
1663 // This correct tag is not in the dictionary. Create a new one.
1664 NewTag = new gdcmDictEntry(CorrectGroup, CorrectElem);
1666 // FIXME this can create a memory leaks on the old entry that be
1667 // left unreferenced.
1668 ElVal->SetDictEntry(NewTag);
1671 // Heuristic: well some files are really ill-formed.
1672 if ( length16 == 0xffff) {
1674 //dbg.Verbose(0, "gdcmHeader::FindLength",
1675 // "Erroneous element length fixed.");
1676 // Actually, length= 0xffff means that we deal with
1677 // Unknown Sequence Length
1680 FixHeaderEntryFoundLength(ElVal, (guint32)length16);
1684 // Either implicit VR or a non DICOM conformal (see not below) explicit
1685 // VR that ommited the VR of (at least) this element. Farts happen.
1686 // [Note: according to the part 5, PS 3.5-2001, section 7.1 p25
1687 // on Data elements "Implicit and Explicit VR Data Elements shall
1688 // not coexist in a Data Set and Data Sets nested within it".]
1689 // Length is on 4 bytes.
1690 FixHeaderEntryFoundLength(ElVal, ReadInt32());
1695 * \ingroup gdcmHeader
1696 * \brief Find the Value Representation of the current Dicom Element.
1699 void gdcmHeader::FindHeaderEntryVR( gdcmHeaderEntry *ElVal) {
1700 if (filetype != ExplicitVR)
1706 char msg[100]; // for sprintf. Sorry
1708 long PositionOnEntry = ftell(fp);
1709 // Warning: we believe this is explicit VR (Value Representation) because
1710 // we used a heuristic that found "UL" in the first tag. Alas this
1711 // doesn't guarantee that all the tags will be in explicit VR. In some
1712 // cases (see e-film filtered files) one finds implicit VR tags mixed
1713 // within an explicit VR file. Hence we make sure the present tag
1714 // is in explicit VR and try to fix things if it happens not to be
1716 bool RealExplicit = true;
1718 lgrLue=fread (&VR, (size_t)2,(size_t)1, fp);
1720 vr = std::string(VR);
1722 // Assume we are reading a falsely explicit VR file i.e. we reached
1723 // a tag where we expect reading a VR but are in fact we read the
1724 // first to bytes of the length. Then we will interogate (through find)
1725 // the dicom_vr dictionary with oddities like "\004\0" which crashes
1726 // both GCC and VC++ implementations of the STL map. Hence when the
1727 // expected VR read happens to be non-ascii characters we consider
1728 // we hit falsely explicit VR tag.
1730 if ( (!isalpha(VR[0])) && (!isalpha(VR[1])) )
1731 RealExplicit = false;
1733 // CLEANME searching the dicom_vr at each occurence is expensive.
1734 // PostPone this test in an optional integrity check at the end
1735 // of parsing or only in debug mode.
1736 if ( RealExplicit && !dicom_vr->Count(vr) )
1737 RealExplicit= false;
1739 if ( RealExplicit ) {
1740 if ( ElVal->IsVRUnknown() ) {
1741 // When not a dictionary entry, we can safely overwrite the VR.
1745 if ( ElVal->GetVR() == vr ) {
1746 // The VR we just read and the dictionary agree. Nothing to do.
1749 // The VR present in the file and the dictionary disagree. We assume
1750 // the file writer knew best and use the VR of the file. Since it would
1751 // be unwise to overwrite the VR of a dictionary (since it would
1752 // compromise it's next user), we need to clone the actual DictEntry
1753 // and change the VR for the read one.
1754 gdcmDictEntry* NewTag = new gdcmDictEntry(ElVal->GetGroup(),
1755 ElVal->GetElement(),
1759 ElVal->SetDictEntry(NewTag);
1763 // We thought this was explicit VR, but we end up with an
1764 // implicit VR tag. Let's backtrack.
1766 sprintf(msg,"Falsely explicit vr file (%04x,%04x)\n",
1767 ElVal->GetGroup(),ElVal->GetElement());
1768 dbg.Verbose(1, "gdcmHeader::FindVR: ",msg);
1770 fseek(fp, PositionOnEntry, SEEK_SET);
1771 // When this element is known in the dictionary we shall use, e.g. for
1772 // the semantics (see the usage of IsAnInteger), the VR proposed by the
1773 // dictionary entry. Still we have to flag the element as implicit since
1774 // we know now our assumption on expliciteness is not furfilled.
1776 if ( ElVal->IsVRUnknown() )
1777 ElVal->SetVR("Implicit");
1778 ElVal->SetImplicitVr();
1782 * \ingroup gdcmHeader
1787 void gdcmHeader::SkipHeaderEntry(gdcmHeaderEntry * entry) {
1788 SkipBytes(entry->GetLength());
1792 * \ingroup gdcmHeader
1793 * \brief When the length of an element value is obviously wrong (because
1794 * the parser went Jabberwocky) one can hope improving things by
1795 * applying this heuristic.
1797 void gdcmHeader::FixHeaderEntryFoundLength(gdcmHeaderEntry * ElVal, guint32 FoundLength) {
1799 ElVal->SetReadLength(FoundLength); // will be updated only if a bug is found
1801 if ( FoundLength == 0xffffffff) {
1805 // Sorry for the patch!
1806 // XMedCom did the trick to read some nasty GE images ...
1807 else if (FoundLength == 13) {
1808 // The following 'if' will be removed when there is no more
1809 // images on Creatis HDs with a 13 length for Manufacturer...
1810 if ( (ElVal->GetGroup() != 0x0008) ||
1811 ( (ElVal->GetElement() != 0x0070) && (ElVal->GetElement() != 0x0080) ) ) {
1812 // end of remove area
1814 ElVal->SetReadLength(10); // a bug is to be fixed
1817 // to fix some garbage 'Leonardo' Siemens images
1818 // May be commented out to avoid overhead
1819 else if ( (ElVal->GetGroup() == 0x0009) &&
1820 ( (ElVal->GetElement() == 0x1113) || (ElVal->GetElement() == 0x1114) ) ){
1822 ElVal->SetReadLength(4); // a bug is to be fixed
1826 // to try to 'go inside' SeQuences (with length), and not to skip them
1827 else if ( ElVal->GetVR() == "SQ") {
1828 if (enableSequences) // only if the user does want to !
1832 // a SeQuence Element is beginning
1833 // Let's forget it's length
1834 // (we want to 'go inside')
1836 // Pb : *normaly* fffe|e000 is just a marker, its length *should be* zero
1837 // in gdcm-MR-PHILIPS-16-Multi-Seq.dcm we find lengthes as big as 28800
1838 // if we set the length to zero IsHeaderEntryAnInteger() breaks...
1839 // if we don't, we lost 28800 characters from the Header :-(
1841 else if(ElVal->GetGroup() == 0xfffe){
1842 // sometimes, length seems to be wrong
1843 FoundLength =0; // some more clever checking to be done !
1845 // only gdcm-MR-PHILIPS-16-Multi-Seq.dcm
1846 // causes troubles :-(
1849 ElVal->SetUsableLength(FoundLength);
1853 * \ingroup gdcmHeader
1854 * \brief Apply some heuristics to predict wether the considered
1855 * element value contains/represents an integer or not.
1856 * @param ElVal The element value on which to apply the predicate.
1857 * @return The result of the heuristical predicate.
1859 bool gdcmHeader::IsHeaderEntryAnInteger(gdcmHeaderEntry * ElVal) {
1860 guint16 element = ElVal->GetElement();
1861 guint16 group = ElVal->GetGroup();
1862 std::string vr = ElVal->GetVR();
1863 guint32 length = ElVal->GetLength();
1865 // When we have some semantics on the element we just read, and if we
1866 // a priori know we are dealing with an integer, then we shall be
1867 // able to swap it's element value properly.
1868 if ( element == 0 ) { // This is the group length of the group
1872 std::ostringstream s;
1873 s << "Erroneous Group Length element length on :" \
1874 << std::hex << group << " , " << element;
1875 dbg.Error("gdcmHeader::IsAnInteger",
1879 if ( (vr == "UL") || (vr == "US") || (vr == "SL") || (vr == "SS") )
1886 * \ingroup gdcmHeader
1891 guint32 gdcmHeader::FindHeaderEntryLengthOB(void) {
1892 // See PS 3.5-2001, section A.4 p. 49 on encapsulation of encoded pixel data.
1895 long PositionOnEntry = ftell(fp);
1896 bool FoundSequenceDelimiter = false;
1897 guint32 TotalLength = 0;
1900 while ( ! FoundSequenceDelimiter) {
1905 TotalLength += 4; // We even have to decount the group and element
1907 if ( g != 0xfffe && g!=0xb00c ) /*for bogus header */ {
1908 char msg[100]; // for sprintf. Sorry
1909 sprintf(msg,"wrong group (%04x) for an item sequence (%04x,%04x)\n",g, g,n);
1910 dbg.Verbose(1, "gdcmHeader::FindLengthOB: ",msg);
1914 if ( n == 0xe0dd || ( g==0xb00c && n==0x0eb6 ) ) /* for bogus header */
1915 FoundSequenceDelimiter = true;
1916 else if ( n != 0xe000 ){
1917 char msg[100]; // for sprintf. Sorry
1918 sprintf(msg,"wrong element (%04x) for an item sequence (%04x,%04x)\n",
1920 dbg.Verbose(1, "gdcmHeader::FindLengthOB: ",msg);
1924 ItemLength = ReadInt32();
1925 TotalLength += ItemLength + 4; // We add 4 bytes since we just read
1926 // the ItemLength with ReadInt32
1927 SkipBytes(ItemLength);
1929 fseek(fp, PositionOnEntry, SEEK_SET);
1934 * \ingroup gdcmHeader
1935 * \brief Reads a supposed to be 16 Bits integer
1936 * \ (swaps it depending on processor endianity)
1938 * @return integer acts as a boolean
1940 guint16 gdcmHeader::ReadInt16(void) {
1943 item_read = fread (&g, (size_t)2,(size_t)1, fp);
1944 if ( item_read != 1 ) {
1945 // dbg.Verbose(0, "gdcmHeader::ReadInt16", " Failed to read :");
1947 // dbg.Verbose(0, "gdcmHeader::ReadInt16", " End of File encountered");
1949 dbg.Verbose(0, "gdcmHeader::ReadInt16", " File Error");
1959 * \ingroup gdcmHeader
1960 * \brief Reads a supposed to be 32 Bits integer
1961 * \ (swaps it depending on processor endianity)
1965 guint32 gdcmHeader::ReadInt32(void) {
1968 item_read = fread (&g, (size_t)4,(size_t)1, fp);
1969 if ( item_read != 1 ) {
1970 //dbg.Verbose(0, "gdcmHeader::ReadInt32", " Failed to read :");
1972 // dbg.Verbose(0, "gdcmHeader::ReadInt32", " End of File encountered");
1974 dbg.Verbose(0, "gdcmHeader::ReadInt32", " File Error");
1984 * \ingroup gdcmHeader
1989 void gdcmHeader::SkipBytes(guint32 NBytes) {
1990 //FIXME don't dump the returned value
1991 (void)fseek(fp, (long)NBytes, SEEK_CUR);
1995 * \ingroup gdcmHeader
1998 void gdcmHeader::Initialise(void) {
1999 dicom_vr = gdcmGlobal::GetVR();
2000 dicom_ts = gdcmGlobal::GetTS();
2001 Dicts = gdcmGlobal::GetDicts();
2002 RefPubDict = Dicts->GetDefaultPubDict();
2003 RefShaDict = (gdcmDict*)0;
2007 * \ingroup gdcmHeader
2008 * \brief Discover what the swap code is (among little endian, big endian,
2009 * bad little endian, bad big endian).
2012 void gdcmHeader::CheckSwap()
2014 // Fourth semantics:
2016 // ---> Warning : This fourth field is NOT part
2017 // of the 'official' Dicom Dictionnary
2018 // and should NOT be used.
2019 // (Not defined for all the groups
2020 // may be removed in a future release)
2023 // META Meta Information
2035 // NMI Nuclear Medicine
2037 // BFS Basic Film Session
2038 // BFB Basic Film Box
2039 // BIB Basic Image Box
2054 // The only guaranted way of finding the swap code is to find a
2055 // group tag since we know it's length has to be of four bytes i.e.
2056 // 0x00000004. Finding the swap code in then straigthforward. Trouble
2057 // occurs when we can't find such group...
2059 guint32 x=4; // x : for ntohs
2060 bool net2host; // true when HostByteOrder is the same as NetworkByteOrder
2064 char deb[HEADER_LENGTH_TO_READ];
2066 // First, compare HostByteOrder and NetworkByteOrder in order to
2067 // determine if we shall need to swap bytes (i.e. the Endian type).
2072 //cout << net2host << endl;
2074 // The easiest case is the one of a DICOM header, since it possesses a
2075 // file preamble where it suffice to look for the string "DICM".
2076 lgrLue = fread(deb, 1, HEADER_LENGTH_TO_READ, fp);
2079 if(memcmp(entCur, "DICM", (size_t)4) == 0) {
2080 dbg.Verbose(1, "gdcmHeader::CheckSwap:", "looks like DICOM Version3");
2081 // Next, determine the value representation (VR). Let's skip to the
2082 // first element (0002, 0000) and check there if we find "UL"
2083 // - or "OB" if the 1st one is (0002,0001) -,
2084 // in which case we (almost) know it is explicit VR.
2085 // WARNING: if it happens to be implicit VR then what we will read
2086 // is the length of the group. If this ascii representation of this
2087 // length happens to be "UL" then we shall believe it is explicit VR.
2088 // FIXME: in order to fix the above warning, we could read the next
2089 // element value (or a couple of elements values) in order to make
2090 // sure we are not commiting a big mistake.
2091 // We need to skip :
2092 // * the 128 bytes of File Preamble (often padded with zeroes),
2093 // * the 4 bytes of "DICM" string,
2094 // * the 4 bytes of the first tag (0002, 0000),or (0002, 0001)
2095 // i.e. a total of 136 bytes.
2098 // Use gdcmHeader::dicom_vr to test all the possibilities
2099 // instead of just checking for UL, OB and UI !?
2100 if( (memcmp(entCur, "UL", (size_t)2) == 0) ||
2101 (memcmp(entCur, "OB", (size_t)2) == 0) ||
2102 (memcmp(entCur, "UI", (size_t)2) == 0) )
2104 filetype = ExplicitVR;
2105 dbg.Verbose(1, "gdcmHeader::CheckSwap:",
2106 "explicit Value Representation");
2108 filetype = ImplicitVR;
2109 dbg.Verbose(1, "gdcmHeader::CheckSwap:",
2110 "not an explicit Value Representation");
2114 dbg.Verbose(1, "gdcmHeader::CheckSwap:",
2115 "HostByteOrder != NetworkByteOrder");
2118 dbg.Verbose(1, "gdcmHeader::CheckSwap:",
2119 "HostByteOrder = NetworkByteOrder");
2122 // Position the file position indicator at first tag (i.e.
2123 // after the file preamble and the "DICM" string).
2125 fseek (fp, 132L, SEEK_SET);
2129 // Alas, this is not a DicomV3 file and whatever happens there is no file
2130 // preamble. We can reset the file position indicator to where the data
2131 // is (i.e. the beginning of the file).
2132 dbg.Verbose(1, "gdcmHeader::CheckSwap:", "not a DICOM Version3 file");
2135 // Our next best chance would be to be considering a 'clean' ACR/NEMA file.
2136 // By clean we mean that the length of the first tag is written down.
2137 // If this is the case and since the length of the first group HAS to be
2138 // four (bytes), then determining the proper swap code is straightforward.
2141 // We assume the array of char we are considering contains the binary
2142 // representation of a 32 bits integer. Hence the following dirty
2144 s = *((guint32 *)(entCur));
2164 dbg.Verbose(0, "gdcmHeader::CheckSwap:",
2165 "ACR/NEMA unfound swap info (time to raise bets)");
2168 // We are out of luck. It is not a DicomV3 nor a 'clean' ACR/NEMA file.
2169 // It is time for despaired wild guesses. So, let's assume this file
2170 // happens to be 'dirty' ACR/NEMA, i.e. the length of the group is
2171 // not present. Then the only info we have is the net2host one.
2181 * \ingroup gdcmHeader
2184 void gdcmHeader::SwitchSwapToBigEndian(void) {
2185 dbg.Verbose(1, "gdcmHeader::SwitchSwapToBigEndian",
2186 "Switching to BigEndian mode.");
2204 * \ingroup gdcmHeader
2209 void gdcmHeader::SetMaxSizeLoadElementValue(long NewSize) {
2212 if ((guint32)NewSize >= (guint32)0xffffffff) {
2213 MaxSizeLoadElementValue = 0xffffffff;
2216 MaxSizeLoadElementValue = NewSize;
2220 * \ingroup gdcmHeader
2221 * \brief Searches both the public and the shadow dictionary (when they
2222 * exist) for the presence of the DictEntry with given
2223 * group and element. The public dictionary has precedence on the
2225 * @param group group of the searched DictEntry
2226 * @param element element of the searched DictEntry
2227 * @return Corresponding DictEntry when it exists, NULL otherwise.
2229 gdcmDictEntry * gdcmHeader::GetDictEntryByNumber(guint16 group,
2231 gdcmDictEntry * found = (gdcmDictEntry*)0;
2232 if (!RefPubDict && !RefShaDict) {
2233 dbg.Verbose(0, "gdcmHeader::GetDictEntry",
2234 "we SHOULD have a default dictionary");
2237 found = RefPubDict->GetTagByNumber(group, element);
2242 found = RefShaDict->GetTagByNumber(group, element);
2250 * \ingroup gdcmHeader
2251 * \brief Searches both the public and the shadow dictionary (when they
2252 * exist) for the presence of the DictEntry with given name.
2253 * The public dictionary has precedence on the shadow one.
2254 * @param Name name of the searched DictEntry
2255 * @return Corresponding DictEntry when it exists, NULL otherwise.
2257 gdcmDictEntry * gdcmHeader::GetDictEntryByName(std::string Name) {
2258 gdcmDictEntry * found = (gdcmDictEntry*)0;
2259 if (!RefPubDict && !RefShaDict) {
2260 dbg.Verbose(0, "gdcmHeader::GetDictEntry",
2261 "we SHOULD have a default dictionary");
2264 found = RefPubDict->GetTagByName(Name);
2269 found = RefShaDict->GetTagByName(Name);
2277 * \ingroup gdcmHeader
2278 * \brief Read the next tag but WITHOUT loading it's value
2279 * @return On succes the newly created HeaderEntry, NULL on failure.
2281 gdcmHeaderEntry * gdcmHeader::ReadNextHeaderEntry(void) {
2284 gdcmHeaderEntry * NewElVal;
2290 // We reached the EOF (or an error occured) and header parsing
2291 // has to be considered as finished.
2292 return (gdcmHeaderEntry *)0;
2294 NewElVal = NewHeaderEntryByNumber(g, n);
2295 FindHeaderEntryVR(NewElVal);
2296 FindHeaderEntryLength(NewElVal);
2300 return (gdcmHeaderEntry *)0;
2302 NewElVal->SetOffset(ftell(fp));
2303 //if ( (g==0x7fe0) && (n==0x0010) )
2308 * \ingroup gdcmHeader
2309 * \brief Build a new Element Value from all the low level arguments.
2310 * Check for existence of dictionary entry, and build
2311 * a default one when absent.
2312 * @param Name Name of the underlying DictEntry
2314 gdcmHeaderEntry* gdcmHeader::NewHeaderEntryByName(std::string Name) {
2316 gdcmDictEntry * NewTag = GetDictEntryByName(Name);
2318 NewTag = new gdcmDictEntry(0xffff, 0xffff, "LO", "Unknown", Name);
2320 gdcmHeaderEntry* NewElVal = new gdcmHeaderEntry(NewTag);
2322 dbg.Verbose(1, "gdcmHeader::ObtainHeaderEntryByName",
2323 "failed to allocate gdcmHeaderEntry");
2324 return (gdcmHeaderEntry*)0;
2330 * \ingroup gdcmHeader
2331 * \brief Build a new Element Value from all the low level arguments.
2332 * Check for existence of dictionary entry, and build
2333 * a default one when absent.
2334 * @param Group group of the underlying DictEntry
2335 * @param Elem element of the underlying DictEntry
2337 gdcmHeaderEntry* gdcmHeader::NewHeaderEntryByNumber(guint16 Group, guint16 Elem) {
2338 // Find out if the tag we encountered is in the dictionaries:
2339 gdcmDictEntry * NewTag = GetDictEntryByNumber(Group, Elem);
2341 NewTag = new gdcmDictEntry(Group, Elem);
2343 gdcmHeaderEntry* NewElVal = new gdcmHeaderEntry(NewTag);
2345 dbg.Verbose(1, "gdcmHeader::NewHeaderEntryByNumber",
2346 "failed to allocate gdcmHeaderEntry");
2347 return (gdcmHeaderEntry*)0;
2353 * \ingroup gdcmHeader
2354 * \brief Small utility function that creates a new manually crafted
2355 * (as opposed as read from the file) gdcmHeaderEntry with user
2356 * specified name and adds it to the public tag hash table.
2357 * \note A fake TagKey is generated so the PubDict can keep it's coherence.
2358 * @param NewTagName The name to be given to this new tag.
2359 * @param VR The Value Representation to be given to this new tag.
2360 * @ return The newly hand crafted Element Value.
2362 gdcmHeaderEntry* gdcmHeader::NewManualHeaderEntryToPubDict(std::string NewTagName,
2364 gdcmHeaderEntry* NewElVal = (gdcmHeaderEntry*)0;
2365 guint32 StuffGroup = 0xffff; // Group to be stuffed with additional info
2366 guint32 FreeElem = 0;
2367 gdcmDictEntry* NewEntry = (gdcmDictEntry*)0;
2369 FreeElem = PubEntrySet.GenerateFreeTagKeyInGroup(StuffGroup);
2370 if (FreeElem == UINT32_MAX) {
2371 dbg.Verbose(1, "gdcmHeader::NewManualElValToPubDict",
2372 "Group 0xffff in Public Dict is full");
2373 return (gdcmHeaderEntry*)0;
2375 NewEntry = new gdcmDictEntry(StuffGroup, FreeElem,
2376 VR, "GDCM", NewTagName);
2377 NewElVal = new gdcmHeaderEntry(NewEntry);
2378 PubEntrySet.Add(NewElVal);
2382 //-----------------------------------------------------------------------------