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))
60 wasUpdated = 0; // will be set to 1 if user adds an entry
68 * @param exception_on_error
70 gdcmHeader::gdcmHeader(bool exception_on_error) {
71 SetMaxSizeLoadElementValue(MAX_SIZE_LOAD_ELEMENT_VALUE);
77 * \brief Canonical destructor.
79 gdcmHeader::~gdcmHeader (void) {
80 dicom_vr = (gdcmVR*)0;
81 Dicts = (gdcmDictSet*)0;
82 RefPubDict = (gdcmDict*)0;
83 RefShaDict = (gdcmDict*)0;
87 //-----------------------------------------------------------------------------
92 * \brief Prints the Header Entries (Dicom Elements)
93 * both from the H Table and the chained list
96 void gdcmHeader::PrintPubEntry(std::ostream & os) {
97 PubEntrySet.Print(os);
101 * \ingroup gdcmHeader
102 * \brief Prints The Dict Entries of THE public Dicom Dictionnry
105 void gdcmHeader::PrintPubDict(std::ostream & os) {
106 RefPubDict->Print(os);
109 //-----------------------------------------------------------------------------
112 * \ingroup gdcmHeader
113 * \brief This predicate, based on hopefully reasonable heuristics,
114 * decides whether or not the current gdcmHeader was properly parsed
115 * and contains the mandatory information for being considered as
116 * a well formed and usable Dicom/Acr File.
117 * @return true when gdcmHeader is the one of a reasonable Dicom/Acr file,
120 bool gdcmHeader::IsReadable(void) {
121 std::string res = GetEntryByNumber(0x0028, 0x0005);
122 if ( res != GDCM_UNFOUND && atoi(res.c_str()) > 4 ) {
123 return false; // Image Dimensions
126 if ( !GetHeaderEntryByNumber(0x0028, 0x0100) )
127 return false; // "Bits Allocated"
128 if ( !GetHeaderEntryByNumber(0x0028, 0x0101) )
129 return false; // "Bits Stored"
130 if ( !GetHeaderEntryByNumber(0x0028, 0x0102) )
131 return false; // "High Bit"
132 if ( !GetHeaderEntryByNumber(0x0028, 0x0103) )
133 return false; // "Pixel Representation"
138 * \ingroup gdcmHeader
139 * \brief Determines if the Transfer Syntax was already encountered
140 * and if it corresponds to a ImplicitVRLittleEndian one.
142 * @return True when ImplicitVRLittleEndian found. False in all other cases.
144 bool gdcmHeader::IsImplicitVRLittleEndianTransferSyntax(void) {
145 gdcmHeaderEntry *Element = GetHeaderEntryByNumber(0x0002, 0x0010);
148 LoadHeaderEntrySafe(Element);
150 std::string Transfer = Element->GetValue();
151 if ( Transfer == "1.2.840.10008.1.2" )
157 * \ingroup gdcmHeader
158 * \brief Determines if the Transfer Syntax was already encountered
159 * and if it corresponds to a ExplicitVRLittleEndian one.
161 * @return True when ExplicitVRLittleEndian found. False in all other cases.
163 bool gdcmHeader::IsExplicitVRLittleEndianTransferSyntax(void) {
164 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
167 LoadHeaderEntrySafe(Element);
169 std::string Transfer = Element->GetValue();
170 if ( Transfer == "1.2.840.10008.1.2.1" )
176 * \ingroup gdcmHeader
177 * \brief Determines if the Transfer Syntax was already encountered
178 * and if it corresponds to a DeflatedExplicitVRLittleEndian one.
180 * @return True when DeflatedExplicitVRLittleEndian found. False in all other cases.
182 bool gdcmHeader::IsDeflatedExplicitVRLittleEndianTransferSyntax(void) {
183 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
186 LoadHeaderEntrySafe(Element);
188 std::string Transfer = Element->GetValue();
189 if ( Transfer == "1.2.840.10008.1.2.1.99" )
195 * \ingroup gdcmHeader
196 * \brief Determines if the Transfer Syntax was already encountered
197 * and if it corresponds to a Explicit VR Big Endian one.
199 * @return True when big endian found. False in all other cases.
201 bool gdcmHeader::IsExplicitVRBigEndianTransferSyntax(void) {
202 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
205 LoadHeaderEntrySafe(Element);
207 std::string Transfer = Element->GetValue();
208 if ( Transfer == "1.2.840.10008.1.2.2" ) //1.2.2 ??? A verifier !
214 * \ingroup gdcmHeader
215 * \brief Determines if the Transfer Syntax was already encountered
216 * and if it corresponds to a JPEGBaseLineProcess1 one.
218 * @return True when JPEGBaseLineProcess1found. False in all other cases.
220 bool gdcmHeader::IsJPEGBaseLineProcess1TransferSyntax(void) {
221 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
224 LoadHeaderEntrySafe(Element);
226 std::string Transfer = Element->GetValue();
227 if ( Transfer == "1.2.840.10008.1.2.4.50" )
233 * \ingroup gdcmHeader
234 * \brief Determines if the Transfer Syntax was already encountered
235 * and if it corresponds to a JPEGExtendedProcess2-4 one.
237 * @return True when JPEGExtendedProcess2-4 found. False in all other cases.
239 bool gdcmHeader::IsJPEGExtendedProcess2_4TransferSyntax(void) {
240 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
243 LoadHeaderEntrySafe(Element);
245 std::string Transfer = Element->GetValue();
246 if ( Transfer == "1.2.840.10008.1.2.4.51" )
252 * \ingroup gdcmHeader
253 * \brief Determines if the Transfer Syntax was already encountered
254 * and if it corresponds to a JPEGExtendeProcess3-5 one.
256 * @return True when JPEGExtendedProcess3-5 found. False in all other cases.
258 bool gdcmHeader::IsJPEGExtendedProcess3_5TransferSyntax(void) {
259 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
262 LoadHeaderEntrySafe(Element);
264 std::string Transfer = Element->GetValue();
265 if ( Transfer == "1.2.840.10008.1.2.4.52" )
271 * \ingroup gdcmHeader
272 * \brief Determines if the Transfer Syntax was already encountered
273 * and if it corresponds to a JPEGSpectralSelectionProcess6-8 one.
275 * @return True when JPEGSpectralSelectionProcess6-8 found. False in all
278 bool gdcmHeader::IsJPEGSpectralSelectionProcess6_8TransferSyntax(void) {
279 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
282 LoadHeaderEntrySafe(Element);
284 std::string Transfer = Element->GetValue();
285 if ( Transfer == "1.2.840.10008.1.2.4.53" )
291 * \ingroup gdcmHeader
292 * \brief Determines if the Transfer Syntax was already encountered
293 * and if it corresponds to a RLE Lossless one.
295 * @return True when RLE Lossless found. False in all
298 bool gdcmHeader::IsRLELossLessTransferSyntax(void) {
299 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
302 LoadHeaderEntrySafe(Element);
304 std::string Transfer = Element->GetValue();
305 if ( Transfer == "1.2.840.10008.1.2.5" )
311 * \ingroup gdcmHeader
312 * \brief Determines if Transfer Syntax was already encountered
313 * and if it corresponds to a JPEG Lossless one.
315 * @return True when RLE Lossless found. False in all
318 bool gdcmHeader::IsJPEGLossless(void) {
319 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
320 // faire qq chose d'intelligent a la place de ça
323 LoadHeaderEntrySafe(Element);
325 const char * Transfert = Element->GetValue().c_str();
326 if ( memcmp(Transfert+strlen(Transfert)-2 ,"70",2)==0) return true;
327 if ( memcmp(Transfert+strlen(Transfert)-2 ,"55",2)==0) return true;
328 if (Element->GetValue() == "1.2.840.10008.1.2.4.57") return true;
334 * \ingroup gdcmHeader
335 * \brief Determines if the Transfer Syntax was already encountered
336 * and if it corresponds to a JPEG200 one.0
338 * @return True when JPEG2000 (Lossly or LossLess) found. False in all
341 bool gdcmHeader::IsJPEG2000(void) {
342 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
345 LoadHeaderEntrySafe(Element);
347 std::string Transfer = Element->GetValue();
348 if ( (Transfer == "1.2.840.10008.1.2.4.90")
349 || (Transfer == "1.2.840.10008.1.2.4.91") )
355 * \ingroup gdcmHeader
356 * \brief Predicate for dicom version 3 file.
357 * @return True when the file is a dicom version 3.
359 bool gdcmHeader::IsDicomV3(void) {
360 // Checking if Transfert Syntax exists is enough
361 return (GetHeaderEntryByNumber(0x0002, 0x0010) != NULL);
365 * \ingroup gdcmHeader
366 * \brief returns the File Type
367 * (ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown)
370 FileType gdcmHeader::GetFileType(void) {
375 * \ingroup gdcmHeader
376 * \brief Retrieve the number of columns of image.
377 * @return The encountered size when found, 0 by default.
378 * 0 means the file is NOT USABLE. The caller will have to check
380 int gdcmHeader::GetXSize(void) {
381 // We cannot check for "Columns" because the "Columns" tag is present
382 // both in IMG (0028,0011) and OLY (6000,0011) sections of the dictionary.
383 std::string StrSize = GetEntryByNumber(0x0028,0x0011);
384 if (StrSize == GDCM_UNFOUND)
386 return atoi(StrSize.c_str());
390 * \ingroup gdcmHeader
391 * \brief Retrieve the number of lines of image.
392 * \warning The defaulted value is 1 as opposed to gdcmHeader::GetXSize()
393 * @return The encountered size when found, 1 by default
394 * (The file contains a Signal, not an Image).
396 int gdcmHeader::GetYSize(void) {
397 // We cannot check for "Rows" because the "Rows" tag is present
398 // both in IMG (0028,0010) and OLY (6000,0010) sections of the dictionary.
399 std::string StrSize = GetEntryByNumber(0x0028,0x0010);
400 if (StrSize != GDCM_UNFOUND)
401 return atoi(StrSize.c_str());
405 // The Rows (0028,0010) entry was optional for ACR/NEMA. It might
406 // hence be a signal (1d image). So we default to 1:
411 * \ingroup gdcmHeader
412 * \brief Retrieve the number of planes of volume or the number
413 * of frames of a multiframe.
414 * \warning When present we consider the "Number of Frames" as the third
415 * dimension. When absent we consider the third dimension as
416 * being the "Planes" tag content.
417 * @return The encountered size when found, 1 by default (single image).
419 int gdcmHeader::GetZSize(void) {
420 // Both DicomV3 and ACR/Nema consider the "Number of Frames"
421 // as the third dimension.
422 std::string StrSize = GetEntryByNumber(0x0028,0x0008);
423 if (StrSize != GDCM_UNFOUND)
424 return atoi(StrSize.c_str());
426 // We then consider the "Planes" entry as the third dimension [we
427 // cannot retrieve by name since "Planes tag is present both in
428 // IMG (0028,0012) and OLY (6000,0012) sections of the dictionary].
429 StrSize = GetEntryByNumber(0x0028,0x0012);
430 if (StrSize != GDCM_UNFOUND)
431 return atoi(StrSize.c_str());
436 * \ingroup gdcmHeader
437 * \brief Retrieve the number of Bits Stored (actually used)
438 * (as opposite to number of Bits Allocated)
440 * @return The encountered number of Bits Stored, 0 by default.
441 * 0 means the file is NOT USABLE. The caller has to check it !
443 int gdcmHeader::GetBitsStored(void) {
444 std::string StrSize = GetEntryByNumber(0x0028,0x0101);
445 if (StrSize == GDCM_UNFOUND)
446 return 0; // It's supposed to be mandatory
447 // the caller will have to check
448 return atoi(StrSize.c_str());
452 * \ingroup gdcmHeader
453 * \brief Retrieve the number of Bits Allocated
454 * (8, 12 -compacted ACR-NEMA files, 16, ...)
456 * @return The encountered number of Bits Allocated, 0 by default.
457 * 0 means the file is NOT USABLE. The caller has to check it !
459 int gdcmHeader::GetBitsAllocated(void) {
460 std::string StrSize = GetEntryByNumber(0x0028,0x0100);
461 if (StrSize == GDCM_UNFOUND)
462 return 0; // It's supposed to be mandatory
463 // the caller will have to check
464 return atoi(StrSize.c_str());
468 * \ingroup gdcmHeader
469 * \brief Retrieve the number of Samples Per Pixel
470 * (1 : gray level, 3 : RGB -1 or 3 Planes-)
472 * @return The encountered number of Samples Per Pixel, 1 by default.
473 * (Gray level Pixels)
475 int gdcmHeader::GetSamplesPerPixel(void) {
476 std::string StrSize = GetEntryByNumber(0x0028,0x0002);
477 if (StrSize == GDCM_UNFOUND)
478 return 1; // Well, it's supposed to be mandatory ...
479 // but sometimes it's missing : we assume Gray pixels
480 return atoi(StrSize.c_str());
484 * \ingroup gdcmHeader
485 * \brief Retrieve the Planar Configuration for RGB images
486 * (0 : RGB Pixels , 1 : R Plane + G Plane + B Plane)
488 * @return The encountered Planar Configuration, 0 by default.
490 int gdcmHeader::GetPlanarConfiguration(void) {
491 std::string StrSize = GetEntryByNumber(0x0028,0x0006);
492 if (StrSize == GDCM_UNFOUND)
494 return atoi(StrSize.c_str());
498 * \ingroup gdcmHeader
499 * \brief Return the size (in bytes) of a single pixel of data.
500 * @return The size in bytes of a single pixel of data; 0 by default
501 * 0 means the file is NOT USABLE; the caller will have to check
503 int gdcmHeader::GetPixelSize(void) {
504 std::string PixelType = GetPixelType();
505 if (PixelType == "8U" || PixelType == "8S")
507 if (PixelType == "16U" || PixelType == "16S")
509 if (PixelType == "32U" || PixelType == "32S")
511 dbg.Verbose(0, "gdcmHeader::GetPixelSize: Unknown pixel type");
516 * \ingroup gdcmHeader
517 * \brief Build the Pixel Type of the image.
518 * Possible values are:
519 * - 8U unsigned 8 bit,
521 * - 16U unsigned 16 bit,
522 * - 16S signed 16 bit,
523 * - 32U unsigned 32 bit,
524 * - 32S signed 32 bit,
525 * \warning 12 bit images appear as 16 bit.
526 * \ 24 bit images appear as 8 bit
527 * @return 0S if nothing found. NOT USABLE file. The caller has to check
529 std::string gdcmHeader::GetPixelType(void) {
530 std::string BitsAlloc = GetEntryByNumber(0x0028, 0x0100); // Bits Allocated
531 if (BitsAlloc == GDCM_UNFOUND) {
532 dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Bits Allocated");
533 BitsAlloc = std::string("16");
535 if (BitsAlloc == "12") // It will be unpacked
536 BitsAlloc = std::string("16");
537 else if (BitsAlloc == "24") // (in order no to be messed up
538 BitsAlloc = std::string("8"); // by old RGB images)
540 std::string Signed = GetEntryByNumber(0x0028, 0x0103); // "Pixel Representation"
541 if (Signed == GDCM_UNFOUND) {
542 dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Pixel Representation");
543 BitsAlloc = std::string("0");
546 Signed = std::string("U");
548 Signed = std::string("S");
550 return( BitsAlloc + Signed);
554 * \ingroup gdcmHeader
555 * \brief Recover the offset (from the beginning of the file) of the pixels.
557 size_t gdcmHeader::GetPixelOffset(void) {
558 // If this file complies with the norm we should encounter the
559 // "Image Location" tag (0x0028, 0x0200). This tag contains the
560 // the group that contains the pixel data (hence the "Pixel Data"
561 // is found by indirection through the "Image Location").
562 // Inside the group pointed by "Image Location" the searched element
563 // is conventionally the element 0x0010 (when the norm is respected).
564 // When the "Image Location" is absent we default to group 0x7fe0.
567 std::string ImageLocation = GetEntryByNumber(0x0028, 0x0200);
569 if ( ImageLocation == GDCM_UNFOUND ) { // Image Location
572 grPixel = (guint16) atoi( ImageLocation.c_str() );
574 if (grPixel != 0x7fe0)
575 // This is a kludge for old dirty Philips imager.
580 gdcmHeaderEntry* PixelElement = GetHeaderEntryByNumber(grPixel,numPixel);
582 return PixelElement->GetOffset();
588 * \ingroup gdcmHeader
589 * \brief Recover the pixel area length (in Bytes)
590 * @return 0 by default. NOT USABLE file. The caller has to check.
592 size_t gdcmHeader::GetPixelAreaLength(void) {
593 // If this file complies with the norm we should encounter the
594 // "Image Location" tag (0x0028, 0x0200). This tag contains the
595 // the group that contains the pixel data (hence the "Pixel Data"
596 // is found by indirection through the "Image Location").
597 // Inside the group pointed by "Image Location" the searched element
598 // is conventionally the element 0x0010 (when the norm is respected).
599 // When the "Image Location" is absent we default to group 0x7fe0.
602 std::string ImageLocation = GetEntryByNumber(0x0028, 0x0200);
603 if ( ImageLocation == GDCM_UNFOUND ) {
606 grPixel = (guint16) atoi( ImageLocation.c_str() );
608 if (grPixel != 0x7fe0)
609 // This is a kludge for old dirty Philips imager.
614 gdcmHeaderEntry* PixelElement = GetHeaderEntryByNumber(grPixel,numPixel);
616 return PixelElement->GetLength();
622 * \ingroup gdcmHeader
623 * \brief tells us if LUT are used
624 * \warning Right now, 'Segmented xxx Palette Color Lookup Table Data'
625 * \ are NOT considered as LUT, since nobody knows
626 * \ how to deal with them
629 bool gdcmHeader::HasLUT(void) {
631 // Check the presence of the LUT Descriptors
632 if ( !GetHeaderEntryByNumber(0x0028,0x1101) )
634 // LutDescriptorGreen
635 if ( !GetHeaderEntryByNumber(0x0028,0x1102) )
638 if ( !GetHeaderEntryByNumber(0x0028,0x1103) )
642 if ( !GetHeaderEntryByNumber(0x0028,0x1201) )
644 if ( !GetHeaderEntryByNumber(0x0028,0x1202) )
646 if ( !GetHeaderEntryByNumber(0x0028,0x1203) )
652 * \ingroup gdcmHeader
653 * \brief gets the info from 0028,1101 : Lookup Table Desc-Red
655 * @return Lookup Table number of Bits , 0 by default
656 * \ when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
658 int gdcmHeader::GetLUTNbits(void) {
659 std::vector<std::string> tokens;
663 //Just hope Lookup Table Desc-Red = Lookup Table Desc-Red = Lookup Table Desc-Blue
664 // Consistency already checked in GetLUTLength
665 std::string LutDescription = GetEntryByNumber(0x0028,0x1101);
666 if (LutDescription == GDCM_UNFOUND)
668 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
669 Tokenize (LutDescription, tokens, "\\");
670 //LutLength=atoi(tokens[0].c_str());
671 //LutDepth=atoi(tokens[1].c_str());
672 LutNbits=atoi(tokens[2].c_str());
678 * \ingroup gdcmHeader
679 * \brief builts Red/Green/Blue/Alpha LUT from Header
680 * \ when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
681 * \ and (0028,1101),(0028,1102),(0028,1102)
682 * \ - xxx Palette Color Lookup Table Descriptor - are found
683 * \ and (0028,1201),(0028,1202),(0028,1202)
684 * \ - xxx Palette Color Lookup Table Data - are found
685 * \warning does NOT deal with :
686 * \ 0028 1100 Gray Lookup Table Descriptor (Retired)
687 * \ 0028 1221 Segmented Red Palette Color Lookup Table Data
688 * \ 0028 1222 Segmented Green Palette Color Lookup Table Data
689 * \ 0028 1223 Segmented Blue Palette Color Lookup Table Data
690 * \ no known Dicom reader deals with them :-(
691 * @return a RGBA Lookup Table
693 unsigned char * gdcmHeader::GetLUTRGBA(void) {
695 // http://www.barre.nom.fr/medical/dicom2/limitations.html#Color%20Lookup%20Tables
697 // if Photometric Interpretation # PALETTE COLOR, no LUT to be done
698 if (gdcmHeader::GetEntryByNumber(0x0028,0x0004) != "PALETTE COLOR ") {
701 int lengthR, debR, nbitsR;
702 int lengthG, debG, nbitsG;
703 int lengthB, debB, nbitsB;
705 // Get info from Lut Descriptors
706 // (the 3 LUT descriptors may be different)
707 std::string LutDescriptionR = GetEntryByNumber(0x0028,0x1101);
708 if (LutDescriptionR == GDCM_UNFOUND)
710 std::string LutDescriptionG = GetEntryByNumber(0x0028,0x1102);
711 if (LutDescriptionG == GDCM_UNFOUND)
713 std::string LutDescriptionB = GetEntryByNumber(0x0028,0x1103);
714 if (LutDescriptionB == GDCM_UNFOUND)
717 std::vector<std::string> tokens;
719 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
720 Tokenize (LutDescriptionR, tokens, "\\");
721 lengthR=atoi(tokens[0].c_str()); // Red LUT length in Bytes
722 debR =atoi(tokens[1].c_str()); // subscript of the first Lut Value
723 nbitsR =atoi(tokens[2].c_str()); // Lut item size (in Bits)
726 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
727 Tokenize (LutDescriptionG, tokens, "\\");
728 lengthG=atoi(tokens[0].c_str()); // Green LUT length in Bytes
729 debG =atoi(tokens[1].c_str());
730 nbitsG =atoi(tokens[2].c_str());
733 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
734 Tokenize (LutDescriptionB, tokens, "\\");
735 lengthB=atoi(tokens[0].c_str()); // Blue LUT length in Bytes
736 debB =atoi(tokens[1].c_str());
737 nbitsB =atoi(tokens[2].c_str());
740 // Load LUTs into memory, (as they were stored on disk)
741 unsigned char *lutR = (unsigned char *)
742 GetPubEntryVoidAreaByNumber(0x0028,0x1201);
743 unsigned char *lutG = (unsigned char *)
744 GetPubEntryVoidAreaByNumber(0x0028,0x1202);
745 unsigned char *lutB = (unsigned char *)
746 GetPubEntryVoidAreaByNumber(0x0028,0x1203);
748 if (!lutR || !lutG || !lutB ) {
751 // forge the 4 * 8 Bits Red/Green/Blue/Alpha LUT
753 unsigned char *LUTRGBA = (unsigned char *)calloc(1024,1); // 256 * 4 (R, G, B, Alpha)
757 memset(LUTRGBA, 0, 1024);
760 std::string str_nb = GetEntryByNumber(0x0028,0x0100);
761 if (str_nb == GDCM_UNFOUND ) {
764 nb = atoi(str_nb.c_str() );
768 if (nbitsR==16 && nb==8) // when LUT item size is different than pixel size
769 mult=2; // high byte must be = low byte
770 else // See PS 3.3-2003 C.11.1.1.2 p 619
773 // if we get a black image, let's just remove the '+1'
774 // from 'i*mult+1' and check again
775 // if it works, we shall have to check the 3 Palettes
776 // to see which byte is ==0 (first one, or second one)
778 // We give up the checking to avoid some overhead
783 for(i=0;i<lengthR;i++) {
788 for(i=0;i<lengthG;i++) {
793 for(i=0;i<lengthB;i++) {
799 *a = 1; // Alpha component
803 //How to free the now useless LUTs?
805 //free(LutR); free(LutB); free(LutG); // Seg Fault when used
810 * \ingroup gdcmHeader
811 * \brief gets the info from 0002,0010 : Transfert Syntax
813 * @return Transfert Syntax Name (as oposite to Transfert Syntax UID)
815 std::string gdcmHeader::GetTransfertSyntaxName(void) {
816 // use the gdcmTS (TS : Transfert Syntax)
817 std::string TransfertSyntax = GetEntryByNumber(0x0002,0x0010);
818 if (TransfertSyntax == GDCM_UNFOUND) {
819 dbg.Verbose(0, "gdcmHeader::GetTransfertSyntaxName: unfound Transfert Syntax (0002,0010)");
820 return "Uncompressed ACR-NEMA";
822 // we do it only when we need it
823 gdcmTS * ts = gdcmGlobal::GetTS();
824 std::string tsName=ts->GetValue(TransfertSyntax);
825 //delete ts; // Seg Fault when deleted ?!
830 * \ingroup gdcmHeader
831 * \brief Searches within the file Header for element value of
833 * @param tagName name of the searched element.
834 * @return Corresponding element value when it exists, and the string
835 * GDCM_UNFOUND ("gdcm::Unfound") otherwise.
837 std::string gdcmHeader::GetPubEntryByName(std::string tagName) {
838 gdcmDictEntry *dictEntry = RefPubDict->GetTagByName(tagName);
839 if( dictEntry == NULL)
842 return(GetEntryByNumber(dictEntry->GetGroup(),dictEntry->GetElement()));
846 * \ingroup gdcmHeader
847 * \brief Searches within the elements parsed with the file Header for
848 * the element value representation of a given tag.
850 * Obtaining the VR (Value Representation) might be needed by caller
851 * to convert the string typed content to caller's native type
852 * (think of C++ vs Python). The VR is actually of a higher level
853 * of semantics than just the native C++ type.
854 * @param tagName name of the searched element.
855 * @return Corresponding element value representation when it exists,
856 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
858 std::string gdcmHeader::GetPubEntryVRByName(std::string tagName) {
859 gdcmDictEntry *dictEntry = RefPubDict->GetTagByName(tagName);
860 if( dictEntry == NULL)
863 gdcmHeaderEntry* elem = GetHeaderEntryByNumber(dictEntry->GetGroup(),
864 dictEntry->GetElement());
865 return elem->GetVR();
869 * \ingroup gdcmHeader
870 * \brief Searches within the public dictionary for element value of
872 * @param group Group of the researched tag.
873 * @param element Element of the researched tag.
874 * @return Corresponding element value when it exists, and the string
875 * GDCM_UNFOUND ("gdcm::Unfound") otherwise.
877 std::string gdcmHeader::GetPubEntryByNumber(guint16 group, guint16 element) {
878 return PubEntrySet.GetEntryByNumber(group, element);
882 * \ingroup gdcmHeader
883 * \brief Searches within the public dictionary for element value
884 * representation of a given tag.
886 * Obtaining the VR (Value Representation) might be needed by caller
887 * to convert the string typed content to caller's native type
888 * (think of C++ vs Python). The VR is actually of a higher level
889 * of semantics than just the native C++ type.
890 * @param group Group of the researched tag.
891 * @param element Element of the researched tag.
892 * @return Corresponding element value representation when it exists,
893 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
895 std::string gdcmHeader::GetPubEntryVRByNumber(guint16 group, guint16 element) {
896 gdcmHeaderEntry* elem = GetHeaderEntryByNumber(group, element);
899 return elem->GetVR();
903 * \ingroup gdcmHeader
904 * \brief Accesses an existing gdcmHeaderEntry (i.e. a Dicom Element)
905 * in the PubHeaderEntrySet of this instance
906 * through tag name and modifies it's content with the given value.
907 * @param content new value to substitute with
908 * @param tagName name of the Header Entry (Dicom Element) to be modified
910 bool gdcmHeader::SetPubEntryByName(std::string content, std::string tagName) {
911 //return ( PubHeaderEntrySet.SetHeaderEntryByName (content, tagName) );
912 gdcmDictEntry *dictEntry = RefPubDict->GetTagByName(tagName);
913 if( dictEntry == NULL)
915 return(PubEntrySet.SetEntryByNumber(content,
916 dictEntry->GetGroup(),
917 dictEntry->GetElement()));
921 * \ingroup gdcmHeader
922 * \brief Accesses an existing gdcmHeaderEntry (i.e. a Dicom Element)
923 * in the PubHeaderEntrySet of this instance
924 * through it's (group, element) and modifies it's content with
926 * @param content new value to substitute with
927 * @param group group of the Dicom Element to modify
928 * @param element element of the Dicom Element to modify
930 bool gdcmHeader::SetPubEntryByNumber(std::string content, guint16 group,
933 //TODO : homogeneiser les noms : SetPubElValByNumber
934 // qui appelle PubHeaderEntrySet.SetHeaderEntryByNumber
935 // pourquoi pas SetPubHeaderEntryByNumber ??
937 return ( PubEntrySet.SetEntryByNumber (content, group, element) );
941 * \ingroup gdcmHeader
942 * \brief Accesses an existing gdcmHeaderEntry (i.e. a Dicom Element)
943 * in the PubHeaderEntrySet of this instance
944 * through it's (group, element) and modifies it's length with
946 * \warning Use with extreme caution.
947 * @param length new length to substitute with
948 * @param group group of the ElVal to modify
949 * @param element element of the ElVal to modify
950 * @return 1 on success, 0 otherwise.
953 bool gdcmHeader::SetPubEntryLengthByNumber(guint32 length, guint16 group,
955 return ( PubEntrySet.SetEntryLengthByNumber (length, group, element) );
959 * \ingroup gdcmHeader
960 * \brief Searches within Header Entries (Dicom Elements) parsed with
961 * the public and private dictionaries
962 * for the element value of a given tag.
963 * @param tagName name of the searched element.
964 * @return Corresponding element value when it exists,
965 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
967 std::string gdcmHeader::GetEntryByName(std::string tagName) {
968 return GetPubEntryByName(tagName);
972 * \ingroup gdcmHeader
973 * \brief Searches within Header Entries (Dicom Elements) parsed with
974 * the public and private dictionaries
975 * for the element value representation of a given tag.
977 * Obtaining the VR (Value Representation) might be needed by caller
978 * to convert the string typed content to caller's native type
979 * (think of C++ vs Python). The VR is actually of a higher level
980 * of semantics than just the native C++ type.
981 * @param tagName name of the searched element.
982 * @return Corresponding element value representation when it exists,
983 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
985 std::string gdcmHeader::GetEntryVRByName(std::string tagName) {
986 return GetPubEntryVRByName(tagName);
990 * \ingroup gdcmHeader
991 * \brief Searches within Header Entries (Dicom Elements) parsed with
992 * the public and private dictionaries
993 * for the element value representation of a given tag.
994 * @param group Group of the searched tag.
995 * @param element Element of the searched tag.
996 * @return Corresponding element value representation when it exists,
997 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
999 std::string gdcmHeader::GetEntryByNumber(guint16 group, guint16 element) {
1000 return GetPubEntryByNumber(group, element);
1004 * \ingroup gdcmHeader
1005 * \brief Searches within Header Entries (Dicom Elements) parsed with
1006 * the public and private dictionaries
1007 * for the element value representation of a given tag..
1009 * Obtaining the VR (Value Representation) might be needed by caller
1010 * to convert the string typed content to caller's native type
1011 * (think of C++ vs Python). The VR is actually of a higher level
1012 * of semantics than just the native C++ type.
1013 * @param group Group of the searched tag.
1014 * @param element Element of the searched tag.
1015 * @return Corresponding element value representation when it exists,
1016 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
1018 std::string gdcmHeader::GetEntryVRByNumber(guint16 group, guint16 element) {
1019 return GetPubEntryVRByNumber(group, element);
1023 * \ingroup gdcmHeader
1024 * \brief Sets the value (string) of the Header Entry (Dicom Element)
1025 * @param content string value of the Dicom Element
1026 * @param tagName name of the searched Dicom Element.
1027 * @return true when found
1029 bool gdcmHeader::SetEntryByName(std::string content,std::string tagName) {
1030 gdcmDictEntry *dictEntry = RefPubDict->GetTagByName(tagName);
1031 if( dictEntry == NULL)
1035 TagKey key = gdcmDictEntry::TranslateToKey(dictEntry->GetGroup(),
1036 dictEntry->GetElement());
1037 if ( PubEntrySet.GetTagHT().count(key) == 0 )
1039 int l = content.length();
1040 if(l%2) { // Odd length are padded with a space (020H).
1042 content = content + '\0';
1045 //tagHt[key]->SetValue(content);
1046 gdcmHeaderEntry * a;
1048 TagHeaderEntryHT::iterator p2;
1049 // DO NOT remove the following lines : they explain how the stuff works
1050 //p= tagHt.equal_range(key); // get a pair of iterators first-last synonym
1051 //p2=p.first; // iterator on the first synonym
1052 //a=p2->second; // H Table target column (2-nd col)
1054 a = ((PubEntrySet.GetTagHT().equal_range(key)).first)->second;
1055 a-> SetValue(content);
1056 std::string vr = a->GetVR();
1059 if( (vr == "US") || (vr == "SS") )
1061 else if( (vr == "UL") || (vr == "SL") )
1070 * \ingroup gdcmHeader
1071 * \brief opens the file
1072 * @param exception_on_error
1075 FILE *gdcmHeader::OpenFile(bool exception_on_error)
1076 throw(gdcmFileError) {
1077 fp=fopen(filename.c_str(),"rb");
1078 if(exception_on_error) {
1080 throw gdcmFileError("gdcmHeader::gdcmHeader(const char *, bool)");
1085 fread(&zero, (size_t)2, (size_t)1, fp);
1087 //ACR -- or DICOM with no Preamble --
1088 if( zero == 0x0008 || zero == 0x0800 || zero == 0x0002 || zero == 0x0200)
1092 fseek(fp, 126L, SEEK_CUR);
1094 fread(dicm, (size_t)4, (size_t)1, fp);
1095 if( memcmp(dicm, "DICM", 4) == 0 )
1099 dbg.Verbose(0, "gdcmHeader::gdcmHeader not DICOM/ACR", filename.c_str());
1102 dbg.Verbose(0, "gdcmHeader::gdcmHeader cannot open file", filename.c_str());
1108 * \ingroup gdcmHeader
1109 * \brief closes the file
1110 * @return TRUE if the close was successfull
1112 bool gdcmHeader::CloseFile(void) {
1113 int closed = fclose(fp);
1121 * \ingroup gdcmHeader
1122 * \brief Parses the header of the file but WITHOUT loading element values.
1124 void gdcmHeader::ParseHeader(bool exception_on_error) throw(gdcmFormatError) {
1125 gdcmHeaderEntry * newHeaderEntry = (gdcmHeaderEntry *)0;
1129 while ( (newHeaderEntry = ReadNextHeaderEntry()) ) {
1130 SkipHeaderEntry(newHeaderEntry);
1131 PubEntrySet.Add(newHeaderEntry);
1136 * \ingroup gdcmHeader
1138 * @param fp file pointer on an already open file
1139 * @param type file type ( ImplicitVR, ExplicitVR, ...)
1142 bool gdcmHeader::Write(FILE * fp, FileType type) {
1144 // TODO : move the following lines (and a lot of others, to be written)
1145 // to a future function CheckAndCorrectHeader
1147 if (type == ImplicitVR) {
1148 std::string implicitVRTransfertSyntax = "1.2.840.10008.1.2";
1149 ReplaceOrCreateByNumber(implicitVRTransfertSyntax,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(18, 0x0002, 0x0010);
1158 if (type == ExplicitVR) {
1159 std::string explicitVRTransfertSyntax = "1.2.840.10008.1.2.1";
1160 ReplaceOrCreateByNumber(explicitVRTransfertSyntax,0x0002, 0x0010);
1162 //FIXME Refer to standards on page 21, chapter 6.2 "Value representation":
1163 // values with a VR of UI shall be padded with a single trailing null
1164 // Dans le cas suivant on doit pader manuellement avec un 0
1166 PubEntrySet.SetEntryLengthByNumber(20, 0x0002, 0x0010);
1169 return PubEntrySet.Write(fp, type);
1174 * \brief Sets the Pixel Area size in the Header
1175 * --> not-for-rats function
1177 * \warning WARNING doit-etre etre publique ?
1178 * TODO : y aurait il un inconvenient à fusionner ces 2 fonctions
1180 * @param ImageDataSize new Pixel Area Size
1181 * warning : nothing else is checked
1183 void gdcmHeader::SetImageDataSize(size_t ImageDataSize) {
1184 std::string content1;
1186 // Assumes HeaderEntry (0x7fe0, 0x0010) exists ...
1187 sprintf(car,"%d",ImageDataSize);
1189 gdcmHeaderEntry *a = GetHeaderEntryByNumber(0x7fe0, 0x0010);
1190 a->SetLength(ImageDataSize);
1193 sprintf(car,"%d",ImageDataSize);
1195 SetPubEntryByNumber(content1, 0x7fe0, 0x0000);
1199 * \ingroup gdcmHeader
1200 * \brief Modifies the value of a given Header Entry (Dicom Element)
1201 * if it exists; Creates it with the given value if it doesn't
1202 * @param Value passed as a std::string
1207 bool gdcmHeader::ReplaceOrCreateByNumber(std::string Value,
1208 guint16 Group, guint16 Elem ) {
1210 if (CheckIfExistByNumber(Group, Elem) == 0) {
1211 gdcmHeaderEntry* a =NewHeaderEntryByNumber(Group, Elem);
1216 PubEntrySet.SetEntryByNumber(Value, Group, Elem);
1221 * \ingroup gdcmHeader
1222 * \brief Modifies the value of a given Header Entry (Dicom Element)
1223 * if it exists; Creates it with the given value if it doesn't
1224 * @param Value passed as a char*
1230 bool gdcmHeader::ReplaceOrCreateByNumber(char* Value, guint16 Group, guint16 Elem ) {
1232 gdcmHeaderEntry* nvHeaderEntry=NewHeaderEntryByNumber(Group, Elem);
1233 // TODO : check if fails
1234 PubEntrySet.Add(nvHeaderEntry);
1235 std::string v = Value;
1236 PubEntrySet.SetEntryByNumber(v, Group, Elem);
1241 * \ingroup gdcmHeader
1242 * \brief Set a new value if the invoked element exists
1243 * Seems to be useless !!!
1247 * \return integer acts as a boolean
1249 bool gdcmHeader::ReplaceIfExistByNumber(char* Value, guint16 Group, guint16 Elem ) {
1251 std::string v = Value;
1252 PubEntrySet.SetEntryByNumber(v, Group, Elem);
1257 * \ingroup gdcmHeader
1258 * \brief Swaps back the bytes of 4-byte long integer accordingly to
1260 * @return The properly swaped 32 bits integer.
1262 guint32 gdcmHeader::SwapLong(guint32 a) {
1267 a=( ((a<<24) & 0xff000000) | ((a<<8) & 0x00ff0000) |
1268 ((a>>8) & 0x0000ff00) | ((a>>24) & 0x000000ff) );
1272 a=( ((a<<16) & 0xffff0000) | ((a>>16) & 0x0000ffff) );
1276 a=( ((a<<8) & 0xff00ff00) | ((a>>8) & 0x00ff00ff) );
1279 dbg.Error(" gdcmHeader::SwapLong : unset swap code");
1286 * \ingroup gdcmHeader
1287 * \brief Swaps the bytes so they agree with the processor order
1288 * @return The properly swaped 16 bits integer.
1290 guint16 gdcmHeader::SwapShort(guint16 a) {
1291 if ( (sw==4321) || (sw==2143) )
1292 a =(((a<<8) & 0x0ff00) | ((a>>8)&0x00ff));
1296 //-----------------------------------------------------------------------------
1299 * \ingroup gdcmHeader
1304 gdcmHeaderEntry *gdcmHeader::GetHeaderEntryByNumber(guint16 Group, guint16 Elem) {
1305 gdcmHeaderEntry *HeaderEntry = PubEntrySet.GetHeaderEntryByNumber(Group, Elem);
1307 dbg.Verbose(1, "gdcmHeader::GetHeaderEntryByNumber",
1308 "failed to Locate gdcmHeaderEntry");
1315 * \ingroup gdcmHeader
1316 * \brief Searches within the Header Entries for a Dicom Element of
1318 * @param tagName name of the searched Dicom Element.
1319 * @return Corresponding Dicom Element when it exists, and NULL
1322 gdcmHeaderEntry *gdcmHeader::GetHeaderEntryByName(std::string tagName) {
1323 gdcmDictEntry *dictEntry = RefPubDict->GetTagByName(tagName);
1324 if( dictEntry == NULL)
1327 return(GetHeaderEntryByNumber(dictEntry->GetGroup(),dictEntry->GetElement()));
1331 * \ingroup gdcmHeader
1332 * \brief Checks if a given HeaderEntry (group,number)
1333 * \ exists in the Public HeaderEntrySet
1338 bool gdcmHeader::CheckIfExistByNumber(guint16 Group, guint16 Elem ) {
1339 return (PubEntrySet.CheckIfExistByNumber(Group, Elem)>0);
1343 * \ingroup gdcmHeader
1344 * \brief Gets (from Header) the offset of a 'non string' element value
1345 * \ (LoadElementValues has already be executed)
1348 * @return File Offset of the Element Value
1350 size_t gdcmHeader::GetPubEntryOffsetByNumber(guint16 Group, guint16 Elem) {
1351 gdcmHeaderEntry* Entry = GetHeaderEntryByNumber(Group, Elem);
1353 dbg.Verbose(1, "gdcmHeader::GetHeaderEntryByNumber",
1354 "failed to Locate gdcmHeaderEntry");
1357 return Entry->GetOffset();
1361 * \ingroup gdcmHeader
1362 * \brief Gets (from Header) a 'non string' element value
1363 * \ (LoadElementValues has already be executed)
1366 * @return Pointer to the 'non string' area
1368 void * gdcmHeader::GetPubEntryVoidAreaByNumber(guint16 Group, guint16 Elem) {
1369 gdcmHeaderEntry* Entry = GetHeaderEntryByNumber(Group, Elem);
1371 dbg.Verbose(1, "gdcmHeader::GetHeaderEntryByNumber",
1372 "failed to Locate gdcmHeaderEntry");
1375 return Entry->GetVoidArea();
1379 * \ingroup gdcmHeader
1380 * \brief Loads (from disk) the element content
1381 * when a string is not suitable
1383 void * gdcmHeader::LoadEntryVoidArea(guint16 Group, guint16 Elem) {
1384 gdcmHeaderEntry * Element= GetHeaderEntryByNumber(Group, Elem);
1387 size_t o =(size_t)Element->GetOffset();
1388 fseek(fp, o, SEEK_SET);
1389 int l=Element->GetLength();
1390 void * a = malloc(l);
1395 PubEntrySet.SetVoidAreaByNumber(a, Group, Elem);
1396 // TODO check the result
1397 size_t l2 = fread(a, 1, l ,fp);
1405 //-----------------------------------------------------------------------------
1408 * \ingroup gdcmHeader
1409 * \brief Loads the element values of all the Header Entries pointed in the
1410 * public Chained List.
1412 void gdcmHeader::LoadHeaderEntries(void) {
1414 for (ListTag::iterator i = GetPubListEntry().begin();
1415 i != GetPubListEntry().end();
1417 LoadHeaderEntry(*i);
1422 // Load 'non string' values
1423 std::string PhotometricInterpretation = GetPubEntryByNumber(0x0028,0x0004);
1424 if( PhotometricInterpretation == "PALETTE COLOR " ){
1425 LoadEntryVoidArea(0x0028,0x1200); // gray LUT
1426 LoadEntryVoidArea(0x0028,0x1201); // R LUT
1427 LoadEntryVoidArea(0x0028,0x1202); // G LUT
1428 LoadEntryVoidArea(0x0028,0x1203); // B LUT
1430 LoadEntryVoidArea(0x0028,0x1221); // Segmented Red Palette Color LUT Data
1431 LoadEntryVoidArea(0x0028,0x1222); // Segmented Green Palette Color LUT Data
1432 LoadEntryVoidArea(0x0028,0x1223); // Segmented Blue Palette Color LUT Data
1435 // --------------------------------------------------------------
1436 // Special Patch to allow gdcm to read ACR-LibIDO formated images
1438 // if recognition code tells us we deal with a LibIDO image
1439 // we switch lineNumber and columnNumber
1441 std::string RecCode;
1442 RecCode = GetPubEntryByNumber(0x0008, 0x0010); // recognition code
1443 if (RecCode == "ACRNEMA_LIBIDO_1.1" ||
1444 RecCode == "CANRME_AILIBOD1_1." ) {
1445 filetype = ACR_LIBIDO;
1446 std::string rows = GetPubEntryByNumber(0x0028, 0x0010);
1447 std::string columns = GetPubEntryByNumber(0x0028, 0x0011);
1448 SetPubEntryByNumber(columns, 0x0028, 0x0010);
1449 SetPubEntryByNumber(rows , 0x0028, 0x0011);
1451 // ----------------- End of Special Patch ----------------
1455 * \ingroup gdcmHeader
1456 * \brief Loads the element content if it's length is not bigger
1457 * than the value specified with
1458 * gdcmHeader::SetMaxSizeLoadElementValue()
1459 * @param ElVal Header Entry (Dicom Element) to be dealt with
1461 void gdcmHeader::LoadHeaderEntry(gdcmHeaderEntry * ElVal) {
1463 guint16 group = ElVal->GetGroup();
1464 std::string vr= ElVal->GetVR();
1465 guint32 length = ElVal->GetLength();
1466 bool SkipLoad = false;
1468 fseek(fp, (long)ElVal->GetOffset(), SEEK_SET);
1470 // the test was commented out to 'go inside' the SeQuences
1471 // we don't any longer skip them !
1473 // if( vr == "SQ" ) // (DO NOT remove this comment)
1476 // A SeQuence "contains" a set of Elements.
1477 // (fffe e000) tells us an Element is beginning
1478 // (fffe e00d) tells us an Element just ended
1479 // (fffe e0dd) tells us the current SeQuence just ended
1480 if( group == 0xfffe )
1484 ElVal->SetLength(0);
1485 ElVal->SetValue("gdcm::Skipped");
1489 // When the length is zero things are easy:
1490 if ( length == 0 ) {
1491 ElVal->SetValue("");
1495 // The elements whose length is bigger than the specified upper bound
1496 // are not loaded. Instead we leave a short notice of the offset of
1497 // the element content and it's length.
1498 if (length > MaxSizeLoadElementValue) {
1499 std::ostringstream s;
1500 s << "gdcm::NotLoaded.";
1501 s << " Address:" << (long)ElVal->GetOffset();
1502 s << " Length:" << ElVal->GetLength();
1503 s << " x(" << std::hex << ElVal->GetLength() << ")";
1504 ElVal->SetValue(s.str());
1508 // When integer(s) are expected, read and convert the following
1509 // n *(two or four bytes)
1510 // properly i.e. as integers as opposed to a strings.
1511 // Elements with Value Multiplicity > 1
1512 // contain a set of integers (not a single one)
1514 // Any compacter code suggested (?)
1515 if ( IsHeaderEntryAnInteger(ElVal) ) {
1517 std::ostringstream s;
1519 if (vr == "US" || vr == "SS") {
1521 NewInt = ReadInt16();
1524 for (int i=1; i < nbInt; i++) {
1526 NewInt = ReadInt16();
1531 } else if (vr == "UL" || vr == "SL") {
1533 NewInt = ReadInt32();
1536 for (int i=1; i < nbInt; i++) {
1538 NewInt = ReadInt32();
1543 #ifdef GDCM_NO_ANSI_STRING_STREAM
1544 s << std::ends; // to avoid oddities on Solaris
1545 #endif //GDCM_NO_ANSI_STRING_STREAM
1546 ElVal->SetValue(s.str());
1550 // We need an additional byte for storing \0 that is not on disk
1551 char* NewValue = (char*)malloc(length+1);
1553 dbg.Verbose(1, "LoadElementValue: Failed to allocate NewValue");
1556 NewValue[length]= 0;
1558 item_read = fread(NewValue, (size_t)length, (size_t)1, fp);
1559 if ( item_read != 1 ) {
1561 dbg.Verbose(1, "gdcmHeader::LoadElementValue","unread element value");
1562 ElVal->SetValue("gdcm::UnRead");
1565 ElVal->SetValue(NewValue);
1570 * \ingroup gdcmHeader
1571 * \brief Loads the element while preserving the current
1572 * underlying file position indicator as opposed to
1573 * to LoadHeaderEntry that modifies it.
1574 * @param entry Header Entry whose value shall be loaded.
1577 void gdcmHeader::LoadHeaderEntrySafe(gdcmHeaderEntry * entry) {
1578 long PositionOnEntry = ftell(fp);
1579 LoadHeaderEntry(entry);
1580 fseek(fp, PositionOnEntry, SEEK_SET);
1584 * \ingroup gdcmHeader
1586 * @param entry Header Entry whose value shall be loaded.
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 Dicom Element.
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 * entry) {
1789 SkipBytes(entry->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 // a SeQuence Element is beginning
1834 // Let's forget it's length
1835 // (we want to 'go inside')
1837 // Pb : *normaly* fffe|e000 is just a marker, its length *should be* zero
1838 // in gdcm-MR-PHILIPS-16-Multi-Seq.dcm we find lengthes as big as 28800
1839 // if we set the length to zero IsHeaderEntryAnInteger() breaks...
1840 // if we don't, we lost 28800 characters from the Header :-(
1842 else if(ElVal->GetGroup() == 0xfffe){
1843 // sometimes, length seems to be wrong
1844 FoundLength =0; // some more clever checking to be done !
1846 // only gdcm-MR-PHILIPS-16-Multi-Seq.dcm
1847 // causes troubles :-(
1850 ElVal->SetUsableLength(FoundLength);
1854 * \ingroup gdcmHeader
1855 * \brief Apply some heuristics to predict wether the considered
1856 * element value contains/represents an integer or not.
1857 * @param ElVal The element value on which to apply the predicate.
1858 * @return The result of the heuristical predicate.
1860 bool gdcmHeader::IsHeaderEntryAnInteger(gdcmHeaderEntry * ElVal) {
1861 guint16 element = ElVal->GetElement();
1862 guint16 group = ElVal->GetGroup();
1863 std::string vr = ElVal->GetVR();
1864 guint32 length = ElVal->GetLength();
1866 // When we have some semantics on the element we just read, and if we
1867 // a priori know we are dealing with an integer, then we shall be
1868 // able to swap it's element value properly.
1869 if ( element == 0 ) { // This is the group length of the group
1873 std::ostringstream s;
1874 s << "Erroneous Group Length element length on :" \
1875 << std::hex << group << " , " << element;
1876 dbg.Error("gdcmHeader::IsAnInteger",
1880 if ( (vr == "UL") || (vr == "US") || (vr == "SL") || (vr == "SS") )
1887 * \ingroup gdcmHeader
1892 guint32 gdcmHeader::FindHeaderEntryLengthOB(void) {
1893 // See PS 3.5-2001, section A.4 p. 49 on encapsulation of encoded pixel data.
1896 long PositionOnEntry = ftell(fp);
1897 bool FoundSequenceDelimiter = false;
1898 guint32 TotalLength = 0;
1901 while ( ! FoundSequenceDelimiter) {
1906 TotalLength += 4; // We even have to decount the group and element
1908 if ( g != 0xfffe && g!=0xb00c ) /*for bogus header */ {
1909 char msg[100]; // for sprintf. Sorry
1910 sprintf(msg,"wrong group (%04x) for an item sequence (%04x,%04x)\n",g, g,n);
1911 dbg.Verbose(1, "gdcmHeader::FindLengthOB: ",msg);
1915 if ( n == 0xe0dd || ( g==0xb00c && n==0x0eb6 ) ) /* for bogus header */
1916 FoundSequenceDelimiter = true;
1917 else if ( n != 0xe000 ){
1918 char msg[100]; // for sprintf. Sorry
1919 sprintf(msg,"wrong element (%04x) for an item sequence (%04x,%04x)\n",
1921 dbg.Verbose(1, "gdcmHeader::FindLengthOB: ",msg);
1925 ItemLength = ReadInt32();
1926 TotalLength += ItemLength + 4; // We add 4 bytes since we just read
1927 // the ItemLength with ReadInt32
1928 SkipBytes(ItemLength);
1930 fseek(fp, PositionOnEntry, SEEK_SET);
1935 * \ingroup gdcmHeader
1936 * \brief Reads a supposed to be 16 Bits integer
1937 * \ (swaps it depending on processor endianity)
1939 * @return integer acts as a boolean
1941 guint16 gdcmHeader::ReadInt16(void) {
1944 item_read = fread (&g, (size_t)2,(size_t)1, fp);
1945 if ( item_read != 1 ) {
1946 // dbg.Verbose(0, "gdcmHeader::ReadInt16", " Failed to read :");
1948 // dbg.Verbose(0, "gdcmHeader::ReadInt16", " End of File encountered");
1950 dbg.Verbose(0, "gdcmHeader::ReadInt16", " File Error");
1960 * \ingroup gdcmHeader
1961 * \brief Reads a supposed to be 32 Bits integer
1962 * \ (swaps it depending on processor endianity)
1966 guint32 gdcmHeader::ReadInt32(void) {
1969 item_read = fread (&g, (size_t)4,(size_t)1, fp);
1970 if ( item_read != 1 ) {
1971 //dbg.Verbose(0, "gdcmHeader::ReadInt32", " Failed to read :");
1973 // dbg.Verbose(0, "gdcmHeader::ReadInt32", " End of File encountered");
1975 dbg.Verbose(0, "gdcmHeader::ReadInt32", " File Error");
1985 * \ingroup gdcmHeader
1990 void gdcmHeader::SkipBytes(guint32 NBytes) {
1991 //FIXME don't dump the returned value
1992 (void)fseek(fp, (long)NBytes, SEEK_CUR);
1996 * \ingroup gdcmHeader
1999 void gdcmHeader::Initialise(void) {
2000 dicom_vr = gdcmGlobal::GetVR();
2001 dicom_ts = gdcmGlobal::GetTS();
2002 Dicts = gdcmGlobal::GetDicts();
2003 RefPubDict = Dicts->GetDefaultPubDict();
2004 RefShaDict = (gdcmDict*)0;
2008 * \ingroup gdcmHeader
2009 * \brief Discover what the swap code is (among little endian, big endian,
2010 * bad little endian, bad big endian).
2013 void gdcmHeader::CheckSwap()
2015 // Fourth semantics:
2017 // ---> Warning : This fourth field is NOT part
2018 // of the 'official' Dicom Dictionnary
2019 // and should NOT be used.
2020 // (Not defined for all the groups
2021 // may be removed in a future release)
2024 // META Meta Information
2036 // NMI Nuclear Medicine
2038 // BFS Basic Film Session
2039 // BFB Basic Film Box
2040 // BIB Basic Image Box
2055 // The only guaranted way of finding the swap code is to find a
2056 // group tag since we know it's length has to be of four bytes i.e.
2057 // 0x00000004. Finding the swap code in then straigthforward. Trouble
2058 // occurs when we can't find such group...
2060 guint32 x=4; // x : for ntohs
2061 bool net2host; // true when HostByteOrder is the same as NetworkByteOrder
2065 char deb[HEADER_LENGTH_TO_READ];
2067 // First, compare HostByteOrder and NetworkByteOrder in order to
2068 // determine if we shall need to swap bytes (i.e. the Endian type).
2073 //cout << net2host << endl;
2075 // The easiest case is the one of a DICOM header, since it possesses a
2076 // file preamble where it suffice to look for the string "DICM".
2077 lgrLue = fread(deb, 1, HEADER_LENGTH_TO_READ, fp);
2080 if(memcmp(entCur, "DICM", (size_t)4) == 0) {
2081 dbg.Verbose(1, "gdcmHeader::CheckSwap:", "looks like DICOM Version3");
2082 // Next, determine the value representation (VR). Let's skip to the
2083 // first element (0002, 0000) and check there if we find "UL"
2084 // - or "OB" if the 1st one is (0002,0001) -,
2085 // in which case we (almost) know it is explicit VR.
2086 // WARNING: if it happens to be implicit VR then what we will read
2087 // is the length of the group. If this ascii representation of this
2088 // length happens to be "UL" then we shall believe it is explicit VR.
2089 // FIXME: in order to fix the above warning, we could read the next
2090 // element value (or a couple of elements values) in order to make
2091 // sure we are not commiting a big mistake.
2092 // We need to skip :
2093 // * the 128 bytes of File Preamble (often padded with zeroes),
2094 // * the 4 bytes of "DICM" string,
2095 // * the 4 bytes of the first tag (0002, 0000),or (0002, 0001)
2096 // i.e. a total of 136 bytes.
2099 // Use gdcmHeader::dicom_vr to test all the possibilities
2100 // instead of just checking for UL, OB and UI !?
2101 if( (memcmp(entCur, "UL", (size_t)2) == 0) ||
2102 (memcmp(entCur, "OB", (size_t)2) == 0) ||
2103 (memcmp(entCur, "UI", (size_t)2) == 0) )
2105 filetype = ExplicitVR;
2106 dbg.Verbose(1, "gdcmHeader::CheckSwap:",
2107 "explicit Value Representation");
2109 filetype = ImplicitVR;
2110 dbg.Verbose(1, "gdcmHeader::CheckSwap:",
2111 "not an explicit Value Representation");
2115 dbg.Verbose(1, "gdcmHeader::CheckSwap:",
2116 "HostByteOrder != NetworkByteOrder");
2119 dbg.Verbose(1, "gdcmHeader::CheckSwap:",
2120 "HostByteOrder = NetworkByteOrder");
2123 // Position the file position indicator at first tag (i.e.
2124 // after the file preamble and the "DICM" string).
2126 fseek (fp, 132L, SEEK_SET);
2130 // Alas, this is not a DicomV3 file and whatever happens there is no file
2131 // preamble. We can reset the file position indicator to where the data
2132 // is (i.e. the beginning of the file).
2133 dbg.Verbose(1, "gdcmHeader::CheckSwap:", "not a DICOM Version3 file");
2136 // Our next best chance would be to be considering a 'clean' ACR/NEMA file.
2137 // By clean we mean that the length of the first tag is written down.
2138 // If this is the case and since the length of the first group HAS to be
2139 // four (bytes), then determining the proper swap code is straightforward.
2142 // We assume the array of char we are considering contains the binary
2143 // representation of a 32 bits integer. Hence the following dirty
2145 s = *((guint32 *)(entCur));
2165 dbg.Verbose(0, "gdcmHeader::CheckSwap:",
2166 "ACR/NEMA unfound swap info (time to raise bets)");
2169 // We are out of luck. It is not a DicomV3 nor a 'clean' ACR/NEMA file.
2170 // It is time for despaired wild guesses. So, let's assume this file
2171 // happens to be 'dirty' ACR/NEMA, i.e. the length of the group is
2172 // not present. Then the only info we have is the net2host one.
2182 * \ingroup gdcmHeader
2185 void gdcmHeader::SwitchSwapToBigEndian(void) {
2186 dbg.Verbose(1, "gdcmHeader::SwitchSwapToBigEndian",
2187 "Switching to BigEndian mode.");
2205 * \ingroup gdcmHeader
2210 void gdcmHeader::SetMaxSizeLoadElementValue(long NewSize) {
2213 if ((guint32)NewSize >= (guint32)0xffffffff) {
2214 MaxSizeLoadElementValue = 0xffffffff;
2217 MaxSizeLoadElementValue = NewSize;
2221 * \ingroup gdcmHeader
2222 * \brief Searches both the public and the shadow dictionary (when they
2223 * exist) for the presence of the DictEntry with given
2224 * group and element. The public dictionary has precedence on the
2226 * @param group group of the searched DictEntry
2227 * @param element element of the searched DictEntry
2228 * @return Corresponding DictEntry when it exists, NULL otherwise.
2230 gdcmDictEntry * gdcmHeader::GetDictEntryByNumber(guint16 group,
2232 gdcmDictEntry * found = (gdcmDictEntry*)0;
2233 if (!RefPubDict && !RefShaDict) {
2234 dbg.Verbose(0, "gdcmHeader::GetDictEntry",
2235 "we SHOULD have a default dictionary");
2238 found = RefPubDict->GetTagByNumber(group, element);
2243 found = RefShaDict->GetTagByNumber(group, element);
2251 * \ingroup gdcmHeader
2252 * \brief Searches both the public and the shadow dictionary (when they
2253 * exist) for the presence of the DictEntry with given name.
2254 * The public dictionary has precedence on the shadow one.
2255 * @param Name name of the searched DictEntry
2256 * @return Corresponding DictEntry when it exists, NULL otherwise.
2258 gdcmDictEntry * gdcmHeader::GetDictEntryByName(std::string Name) {
2259 gdcmDictEntry * found = (gdcmDictEntry*)0;
2260 if (!RefPubDict && !RefShaDict) {
2261 dbg.Verbose(0, "gdcmHeader::GetDictEntry",
2262 "we SHOULD have a default dictionary");
2265 found = RefPubDict->GetTagByName(Name);
2270 found = RefShaDict->GetTagByName(Name);
2278 * \ingroup gdcmHeader
2279 * \brief Read the next tag but WITHOUT loading it's value
2280 * @return On succes the newly created HeaderEntry, NULL on failure.
2282 gdcmHeaderEntry * gdcmHeader::ReadNextHeaderEntry(void) {
2285 gdcmHeaderEntry * NewElVal;
2291 // We reached the EOF (or an error occured) and header parsing
2292 // has to be considered as finished.
2293 return (gdcmHeaderEntry *)0;
2295 NewElVal = NewHeaderEntryByNumber(g, n);
2296 FindHeaderEntryVR(NewElVal);
2297 FindHeaderEntryLength(NewElVal);
2301 return (gdcmHeaderEntry *)0;
2303 NewElVal->SetOffset(ftell(fp));
2304 //if ( (g==0x7fe0) && (n==0x0010) )
2309 * \ingroup gdcmHeader
2310 * \brief Build a new Element Value from all the low level arguments.
2311 * Check for existence of dictionary entry, and build
2312 * a default one when absent.
2313 * @param Name Name of the underlying DictEntry
2315 gdcmHeaderEntry* gdcmHeader::NewHeaderEntryByName(std::string Name) {
2317 gdcmDictEntry * NewTag = GetDictEntryByName(Name);
2319 NewTag = new gdcmDictEntry(0xffff, 0xffff, "LO", "Unknown", Name);
2321 gdcmHeaderEntry* NewElVal = new gdcmHeaderEntry(NewTag);
2323 dbg.Verbose(1, "gdcmHeader::ObtainHeaderEntryByName",
2324 "failed to allocate gdcmHeaderEntry");
2325 return (gdcmHeaderEntry*)0;
2331 * \ingroup gdcmHeader
2332 * \brief Build a new Element Value from all the low level arguments.
2333 * Check for existence of dictionary entry, and build
2334 * a default one when absent.
2335 * @param Group group of the underlying DictEntry
2336 * @param Elem element of the underlying DictEntry
2338 gdcmHeaderEntry* gdcmHeader::NewHeaderEntryByNumber(guint16 Group, guint16 Elem) {
2339 // Find out if the tag we encountered is in the dictionaries:
2340 gdcmDictEntry * NewTag = GetDictEntryByNumber(Group, Elem);
2342 NewTag = new gdcmDictEntry(Group, Elem);
2344 gdcmHeaderEntry* NewElVal = new gdcmHeaderEntry(NewTag);
2346 dbg.Verbose(1, "gdcmHeader::NewHeaderEntryByNumber",
2347 "failed to allocate gdcmHeaderEntry");
2348 return (gdcmHeaderEntry*)0;
2354 * \ingroup gdcmHeader
2355 * \brief Small utility function that creates a new manually crafted
2356 * (as opposed as read from the file) gdcmHeaderEntry with user
2357 * specified name and adds it to the public tag hash table.
2358 * \note A fake TagKey is generated so the PubDict can keep it's coherence.
2359 * @param NewTagName The name to be given to this new tag.
2360 * @param VR The Value Representation to be given to this new tag.
2361 * @return The newly hand crafted Element Value.
2363 gdcmHeaderEntry* gdcmHeader::NewManualHeaderEntryToPubDict(std::string NewTagName,
2365 gdcmHeaderEntry* NewElVal = (gdcmHeaderEntry*)0;
2366 guint32 StuffGroup = 0xffff; // Group to be stuffed with additional info
2367 guint32 FreeElem = 0;
2368 gdcmDictEntry* NewEntry = (gdcmDictEntry*)0;
2370 FreeElem = PubEntrySet.GenerateFreeTagKeyInGroup(StuffGroup);
2371 if (FreeElem == UINT32_MAX) {
2372 dbg.Verbose(1, "gdcmHeader::NewManualElValToPubDict",
2373 "Group 0xffff in Public Dict is full");
2374 return (gdcmHeaderEntry*)0;
2376 NewEntry = new gdcmDictEntry(StuffGroup, FreeElem,
2377 VR, "GDCM", NewTagName);
2378 NewElVal = new gdcmHeaderEntry(NewEntry);
2379 PubEntrySet.Add(NewElVal);
2383 //-----------------------------------------------------------------------------