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))
62 wasUpdated = 0; // will be set to 1 if user adds an entry
63 printLevel = 1; // 'Heavy' header print by default
69 * @param exception_on_error
71 gdcmHeader::gdcmHeader(bool exception_on_error) {
72 SetMaxSizeLoadElementValue(MAX_SIZE_LOAD_ELEMENT_VALUE);
78 * \brief Canonical destructor.
80 gdcmHeader::~gdcmHeader (void) {
81 dicom_vr = (gdcmVR*)0;
82 Dicts = (gdcmDictSet*)0;
83 RefPubDict = (gdcmDict*)0;
84 RefShaDict = (gdcmDict*)0;
88 //-----------------------------------------------------------------------------
93 * \brief Prints the Header Entries (Dicom Elements)
94 * both from the H Table and the chained list
97 void gdcmHeader::PrintPubEntry(std::ostream & os) {
102 * \ingroup gdcmHeader
103 * \brief Prints The Dict Entries of THE public Dicom Dictionnry
106 void gdcmHeader::PrintPubDict(std::ostream & os) {
107 RefPubDict->Print(os);
110 //-----------------------------------------------------------------------------
113 * \ingroup gdcmHeader
114 * \brief This predicate, based on hopefully reasonable heuristics,
115 * decides whether or not the current gdcmHeader was properly parsed
116 * and contains the mandatory information for being considered as
117 * a well formed and usable Dicom/Acr File.
118 * @return true when gdcmHeader is the one of a reasonable Dicom/Acr file,
121 bool gdcmHeader::IsReadable(void) {
122 std::string res = GetEntryByNumber(0x0028, 0x0005);
123 if ( res != GDCM_UNFOUND && atoi(res.c_str()) > 4 ) {
124 return false; // Image Dimensions
127 if ( !GetHeaderEntryByNumber(0x0028, 0x0100) )
128 return false; // "Bits Allocated"
129 if ( !GetHeaderEntryByNumber(0x0028, 0x0101) )
130 return false; // "Bits Stored"
131 if ( !GetHeaderEntryByNumber(0x0028, 0x0102) )
132 return false; // "High Bit"
133 if ( !GetHeaderEntryByNumber(0x0028, 0x0103) )
134 return false; // "Pixel Representation"
139 * \ingroup gdcmHeader
140 * \brief Determines if the Transfer Syntax was already encountered
141 * and if it corresponds to a ImplicitVRLittleEndian one.
143 * @return True when ImplicitVRLittleEndian found. False in all other cases.
145 bool gdcmHeader::IsImplicitVRLittleEndianTransferSyntax(void) {
146 gdcmHeaderEntry *Element = GetHeaderEntryByNumber(0x0002, 0x0010);
149 LoadHeaderEntrySafe(Element);
151 std::string Transfer = Element->GetValue();
152 if ( Transfer == "1.2.840.10008.1.2" )
158 * \ingroup gdcmHeader
159 * \brief Determines if the Transfer Syntax was already encountered
160 * and if it corresponds to a ExplicitVRLittleEndian one.
162 * @return True when ExplicitVRLittleEndian found. False in all other cases.
164 bool gdcmHeader::IsExplicitVRLittleEndianTransferSyntax(void) {
165 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
168 LoadHeaderEntrySafe(Element);
170 std::string Transfer = Element->GetValue();
171 if ( Transfer == "1.2.840.10008.1.2.1" )
177 * \ingroup gdcmHeader
178 * \brief Determines if the Transfer Syntax was already encountered
179 * and if it corresponds to a DeflatedExplicitVRLittleEndian one.
181 * @return True when DeflatedExplicitVRLittleEndian found. False in all other cases.
183 bool gdcmHeader::IsDeflatedExplicitVRLittleEndianTransferSyntax(void) {
184 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
187 LoadHeaderEntrySafe(Element);
189 std::string Transfer = Element->GetValue();
190 if ( Transfer == "1.2.840.10008.1.2.1.99" )
196 * \ingroup gdcmHeader
197 * \brief Determines if the Transfer Syntax was already encountered
198 * and if it corresponds to a Explicit VR Big Endian one.
200 * @return True when big endian found. False in all other cases.
202 bool gdcmHeader::IsExplicitVRBigEndianTransferSyntax(void) {
203 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
206 LoadHeaderEntrySafe(Element);
208 std::string Transfer = Element->GetValue();
209 if ( Transfer == "1.2.840.10008.1.2.2" ) //1.2.2 ??? A verifier !
215 * \ingroup gdcmHeader
216 * \brief Determines if the Transfer Syntax was already encountered
217 * and if it corresponds to a JPEGBaseLineProcess1 one.
219 * @return True when JPEGBaseLineProcess1found. False in all other cases.
221 bool gdcmHeader::IsJPEGBaseLineProcess1TransferSyntax(void) {
222 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
225 LoadHeaderEntrySafe(Element);
227 std::string Transfer = Element->GetValue();
228 if ( Transfer == "1.2.840.10008.1.2.4.50" )
234 * \ingroup gdcmHeader
235 * \brief Determines if the Transfer Syntax was already encountered
236 * and if it corresponds to a JPEGExtendedProcess2-4 one.
238 * @return True when JPEGExtendedProcess2-4 found. False in all other cases.
240 bool gdcmHeader::IsJPEGExtendedProcess2_4TransferSyntax(void) {
241 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
244 LoadHeaderEntrySafe(Element);
246 std::string Transfer = Element->GetValue();
247 if ( Transfer == "1.2.840.10008.1.2.4.51" )
253 * \ingroup gdcmHeader
254 * \brief Determines if the Transfer Syntax was already encountered
255 * and if it corresponds to a JPEGExtendeProcess3-5 one.
257 * @return True when JPEGExtendedProcess3-5 found. False in all other cases.
259 bool gdcmHeader::IsJPEGExtendedProcess3_5TransferSyntax(void) {
260 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
263 LoadHeaderEntrySafe(Element);
265 std::string Transfer = Element->GetValue();
266 if ( Transfer == "1.2.840.10008.1.2.4.52" )
272 * \ingroup gdcmHeader
273 * \brief Determines if the Transfer Syntax was already encountered
274 * and if it corresponds to a JPEGSpectralSelectionProcess6-8 one.
276 * @return True when JPEGSpectralSelectionProcess6-8 found. False in all
279 bool gdcmHeader::IsJPEGSpectralSelectionProcess6_8TransferSyntax(void) {
280 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
283 LoadHeaderEntrySafe(Element);
285 std::string Transfer = Element->GetValue();
286 if ( Transfer == "1.2.840.10008.1.2.4.53" )
292 * \ingroup gdcmHeader
293 * \brief Determines if the Transfer Syntax was already encountered
294 * and if it corresponds to a RLE Lossless one.
296 * @return True when RLE Lossless found. False in all
299 bool gdcmHeader::IsRLELossLessTransferSyntax(void) {
300 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
303 LoadHeaderEntrySafe(Element);
305 std::string Transfer = Element->GetValue();
306 if ( Transfer == "1.2.840.10008.1.2.5" )
312 * \ingroup gdcmHeader
313 * \brief Determines if Transfer Syntax was already encountered
314 * and if it corresponds to a JPEG Lossless one.
316 * @return True when RLE Lossless found. False in all
319 bool gdcmHeader::IsJPEGLossless(void) {
320 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
321 // faire qq chose d'intelligent a la place de ça
324 LoadHeaderEntrySafe(Element);
326 const char * Transfert = Element->GetValue().c_str();
327 if ( memcmp(Transfert+strlen(Transfert)-2 ,"70",2)==0) return true;
328 if ( memcmp(Transfert+strlen(Transfert)-2 ,"55",2)==0) return true;
329 if (Element->GetValue() == "1.2.840.10008.1.2.4.57") return true;
335 * \ingroup gdcmHeader
336 * \brief Determines if the Transfer Syntax was already encountered
337 * and if it corresponds to a JPEG200 one.0
339 * @return True when JPEG2000 (Lossly or LossLess) found. False in all
342 bool gdcmHeader::IsJPEG2000(void) {
343 gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
346 LoadHeaderEntrySafe(Element);
348 std::string Transfer = Element->GetValue();
349 if ( (Transfer == "1.2.840.10008.1.2.4.90")
350 || (Transfer == "1.2.840.10008.1.2.4.91") )
356 * \ingroup gdcmHeader
357 * \brief Predicate for dicom version 3 file.
358 * @return True when the file is a dicom version 3.
360 bool gdcmHeader::IsDicomV3(void) {
361 // Checking if Transfert Syntax exists is enough
362 return (GetHeaderEntryByNumber(0x0002, 0x0010) != NULL);
366 * \ingroup gdcmHeader
367 * \brief returns the File Type
368 * (ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown)
371 FileType gdcmHeader::GetFileType(void) {
376 * \ingroup gdcmHeader
377 * \brief Retrieve the number of columns of image.
378 * @return The encountered size when found, 0 by default.
379 * 0 means the file is NOT USABLE. The caller will have to check
381 int gdcmHeader::GetXSize(void) {
382 // We cannot check for "Columns" because the "Columns" tag is present
383 // both in IMG (0028,0011) and OLY (6000,0011) sections of the dictionary.
384 std::string StrSize = GetEntryByNumber(0x0028,0x0011);
385 if (StrSize == GDCM_UNFOUND)
387 return atoi(StrSize.c_str());
391 * \ingroup gdcmHeader
392 * \brief Retrieve the number of lines of image.
393 * \warning The defaulted value is 1 as opposed to gdcmHeader::GetXSize()
394 * @return The encountered size when found, 1 by default
395 * (The file contains a Signal, not an Image).
397 int gdcmHeader::GetYSize(void) {
398 // We cannot check for "Rows" because the "Rows" tag is present
399 // both in IMG (0028,0010) and OLY (6000,0010) sections of the dictionary.
400 std::string StrSize = GetEntryByNumber(0x0028,0x0010);
401 if (StrSize != GDCM_UNFOUND)
402 return atoi(StrSize.c_str());
406 // The Rows (0028,0010) entry was optional for ACR/NEMA. It might
407 // hence be a signal (1d image). So we default to 1:
412 * \ingroup gdcmHeader
413 * \brief Retrieve the number of planes of volume or the number
414 * of frames of a multiframe.
415 * \warning When present we consider the "Number of Frames" as the third
416 * dimension. When absent we consider the third dimension as
417 * being the "Planes" tag content.
418 * @return The encountered size when found, 1 by default (single image).
420 int gdcmHeader::GetZSize(void) {
421 // Both DicomV3 and ACR/Nema consider the "Number of Frames"
422 // as the third dimension.
423 std::string StrSize = GetEntryByNumber(0x0028,0x0008);
424 if (StrSize != GDCM_UNFOUND)
425 return atoi(StrSize.c_str());
427 // We then consider the "Planes" entry as the third dimension [we
428 // cannot retrieve by name since "Planes tag is present both in
429 // IMG (0028,0012) and OLY (6000,0012) sections of the dictionary].
430 StrSize = GetEntryByNumber(0x0028,0x0012);
431 if (StrSize != GDCM_UNFOUND)
432 return atoi(StrSize.c_str());
437 * \ingroup gdcmHeader
438 * \brief Retrieve the number of Bits Stored (actually used)
439 * (as opposite to number of Bits Allocated)
441 * @return The encountered number of Bits Stored, 0 by default.
442 * 0 means the file is NOT USABLE. The caller has to check it !
444 int gdcmHeader::GetBitsStored(void) {
445 std::string StrSize = GetEntryByNumber(0x0028,0x0101);
446 if (StrSize == GDCM_UNFOUND)
447 return 0; // It's supposed to be mandatory
448 // the caller will have to check
449 return atoi(StrSize.c_str());
453 * \ingroup gdcmHeader
454 * \brief Retrieve the number of Bits Allocated
455 * (8, 12 -compacted ACR-NEMA files, 16, ...)
457 * @return The encountered number of Bits Allocated, 0 by default.
458 * 0 means the file is NOT USABLE. The caller has to check it !
460 int gdcmHeader::GetBitsAllocated(void) {
461 std::string StrSize = GetEntryByNumber(0x0028,0x0100);
462 if (StrSize == GDCM_UNFOUND)
463 return 0; // It's supposed to be mandatory
464 // the caller will have to check
465 return atoi(StrSize.c_str());
469 * \ingroup gdcmHeader
470 * \brief Retrieve the number of Samples Per Pixel
471 * (1 : gray level, 3 : RGB -1 or 3 Planes-)
473 * @return The encountered number of Samples Per Pixel, 1 by default.
474 * (Gray level Pixels)
476 int gdcmHeader::GetSamplesPerPixel(void) {
477 std::string StrSize = GetEntryByNumber(0x0028,0x0002);
478 if (StrSize == GDCM_UNFOUND)
479 return 1; // Well, it's supposed to be mandatory ...
480 // but sometimes it's missing : we assume Gray pixels
481 return atoi(StrSize.c_str());
485 * \ingroup gdcmHeader
486 * \brief Retrieve the Planar Configuration for RGB images
487 * (0 : RGB Pixels , 1 : R Plane + G Plane + B Plane)
489 * @return The encountered Planar Configuration, 0 by default.
491 int gdcmHeader::GetPlanarConfiguration(void) {
492 std::string StrSize = GetEntryByNumber(0x0028,0x0006);
493 if (StrSize == GDCM_UNFOUND)
495 return atoi(StrSize.c_str());
499 * \ingroup gdcmHeader
500 * \brief Return the size (in bytes) of a single pixel of data.
501 * @return The size in bytes of a single pixel of data; 0 by default
502 * 0 means the file is NOT USABLE; the caller will have to check
504 int gdcmHeader::GetPixelSize(void) {
505 std::string PixelType = GetPixelType();
506 if (PixelType == "8U" || PixelType == "8S")
508 if (PixelType == "16U" || PixelType == "16S")
510 if (PixelType == "32U" || PixelType == "32S")
512 dbg.Verbose(0, "gdcmHeader::GetPixelSize: Unknown pixel type");
517 * \ingroup gdcmHeader
518 * \brief Build the Pixel Type of the image.
519 * Possible values are:
520 * - 8U unsigned 8 bit,
522 * - 16U unsigned 16 bit,
523 * - 16S signed 16 bit,
524 * - 32U unsigned 32 bit,
525 * - 32S signed 32 bit,
526 * \warning 12 bit images appear as 16 bit.
527 * \ 24 bit images appear as 8 bit
528 * @return 0S if nothing found. NOT USABLE file. The caller has to check
530 std::string gdcmHeader::GetPixelType(void) {
531 std::string BitsAlloc = GetEntryByNumber(0x0028, 0x0100); // Bits Allocated
532 if (BitsAlloc == GDCM_UNFOUND) {
533 dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Bits Allocated");
534 BitsAlloc = std::string("16");
536 if (BitsAlloc == "12") // It will be unpacked
537 BitsAlloc = std::string("16");
538 else if (BitsAlloc == "24") // (in order no to be messed up
539 BitsAlloc = std::string("8"); // by old RGB images)
541 std::string Signed = GetEntryByNumber(0x0028, 0x0103); // "Pixel Representation"
542 if (Signed == GDCM_UNFOUND) {
543 dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Pixel Representation");
544 BitsAlloc = std::string("0");
547 Signed = std::string("U");
549 Signed = std::string("S");
551 return( BitsAlloc + Signed);
555 * \ingroup gdcmHeader
556 * \brief Recover the offset (from the beginning of the file) of the pixels.
558 size_t gdcmHeader::GetPixelOffset(void) {
559 // If this file complies with the norm we should encounter the
560 // "Image Location" tag (0x0028, 0x0200). This tag contains the
561 // the group that contains the pixel data (hence the "Pixel Data"
562 // is found by indirection through the "Image Location").
563 // Inside the group pointed by "Image Location" the searched element
564 // is conventionally the element 0x0010 (when the norm is respected).
565 // When the "Image Location" is absent we default to group 0x7fe0.
568 std::string ImageLocation = GetEntryByNumber(0x0028, 0x0200);
570 if ( ImageLocation == GDCM_UNFOUND ) { // Image Location
573 grPixel = (guint16) atoi( ImageLocation.c_str() );
575 if (grPixel != 0x7fe0)
576 // This is a kludge for old dirty Philips imager.
581 gdcmHeaderEntry* PixelElement = GetHeaderEntryByNumber(grPixel,numPixel);
583 return PixelElement->GetOffset();
589 * \ingroup gdcmHeader
590 * \brief Recover the pixel area length (in Bytes)
591 * @return 0 by default. NOT USABLE file. The caller has to check.
593 size_t gdcmHeader::GetPixelAreaLength(void) {
594 // If this file complies with the norm we should encounter the
595 // "Image Location" tag (0x0028, 0x0200). This tag contains the
596 // the group that contains the pixel data (hence the "Pixel Data"
597 // is found by indirection through the "Image Location").
598 // Inside the group pointed by "Image Location" the searched element
599 // is conventionally the element 0x0010 (when the norm is respected).
600 // When the "Image Location" is absent we default to group 0x7fe0.
603 std::string ImageLocation = GetEntryByNumber(0x0028, 0x0200);
604 if ( ImageLocation == GDCM_UNFOUND ) {
607 grPixel = (guint16) atoi( ImageLocation.c_str() );
609 if (grPixel != 0x7fe0)
610 // This is a kludge for old dirty Philips imager.
615 gdcmHeaderEntry* PixelElement = GetHeaderEntryByNumber(grPixel,numPixel);
617 return PixelElement->GetLength();
623 * \ingroup gdcmHeader
624 * \brief tells us if LUT are used
625 * \warning Right now, 'Segmented xxx Palette Color Lookup Table Data'
626 * \ are NOT considered as LUT, since nobody knows
627 * \ how to deal with them
630 bool gdcmHeader::HasLUT(void) {
632 // Check the presence of the LUT Descriptors
633 if ( !GetHeaderEntryByNumber(0x0028,0x1101) )
635 // LutDescriptorGreen
636 if ( !GetHeaderEntryByNumber(0x0028,0x1102) )
639 if ( !GetHeaderEntryByNumber(0x0028,0x1103) )
643 if ( !GetHeaderEntryByNumber(0x0028,0x1201) )
645 if ( !GetHeaderEntryByNumber(0x0028,0x1202) )
647 if ( !GetHeaderEntryByNumber(0x0028,0x1203) )
653 * \ingroup gdcmHeader
654 * \brief gets the info from 0028,1101 : Lookup Table Desc-Red
656 * @return Lookup Table number of Bits , 0 by default
657 * \ when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
659 int gdcmHeader::GetLUTNbits(void) {
660 std::vector<std::string> tokens;
664 //Just hope Lookup Table Desc-Red = Lookup Table Desc-Red = Lookup Table Desc-Blue
665 // Consistency already checked in GetLUTLength
666 std::string LutDescription = GetEntryByNumber(0x0028,0x1101);
667 if (LutDescription == GDCM_UNFOUND)
669 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
670 Tokenize (LutDescription, tokens, "\\");
671 //LutLength=atoi(tokens[0].c_str());
672 //LutDepth=atoi(tokens[1].c_str());
673 LutNbits=atoi(tokens[2].c_str());
679 * \ingroup gdcmHeader
680 * \brief builts Red/Green/Blue/Alpha LUT from Header
681 * \ when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
682 * \ and (0028,1101),(0028,1102),(0028,1102)
683 * \ - xxx Palette Color Lookup Table Descriptor - are found
684 * \ and (0028,1201),(0028,1202),(0028,1202)
685 * \ - xxx Palette Color Lookup Table Data - are found
686 * \warning does NOT deal with :
687 * \ 0028 1100 Gray Lookup Table Descriptor (Retired)
688 * \ 0028 1221 Segmented Red Palette Color Lookup Table Data
689 * \ 0028 1222 Segmented Green Palette Color Lookup Table Data
690 * \ 0028 1223 Segmented Blue Palette Color Lookup Table Data
691 * \ no known Dicom reader deals with them :-(
692 * @return a RGBA Lookup Table
694 unsigned char * gdcmHeader::GetLUTRGBA(void) {
696 // http://www.barre.nom.fr/medical/dicom2/limitations.html#Color%20Lookup%20Tables
698 // if Photometric Interpretation # PALETTE COLOR, no LUT to be done
699 if (GetEntryByNumber(0x0028,0x0004) != "PALETTE COLOR ") {
702 int lengthR, debR, nbitsR;
703 int lengthG, debG, nbitsG;
704 int lengthB, debB, nbitsB;
706 // Get info from Lut Descriptors
707 // (the 3 LUT descriptors may be different)
708 std::string LutDescriptionR = GetEntryByNumber(0x0028,0x1101);
709 if (LutDescriptionR == GDCM_UNFOUND)
711 std::string LutDescriptionG = GetEntryByNumber(0x0028,0x1102);
712 if (LutDescriptionG == GDCM_UNFOUND)
714 std::string LutDescriptionB = GetEntryByNumber(0x0028,0x1103);
715 if (LutDescriptionB == GDCM_UNFOUND)
718 std::vector<std::string> tokens;
720 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
721 Tokenize (LutDescriptionR, tokens, "\\");
722 lengthR=atoi(tokens[0].c_str()); // Red LUT length in Bytes
723 debR =atoi(tokens[1].c_str()); // subscript of the first Lut Value
724 nbitsR =atoi(tokens[2].c_str()); // Lut item size (in Bits)
727 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
728 Tokenize (LutDescriptionG, tokens, "\\");
729 lengthG=atoi(tokens[0].c_str()); // Green LUT length in Bytes
730 debG =atoi(tokens[1].c_str());
731 nbitsG =atoi(tokens[2].c_str());
734 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
735 Tokenize (LutDescriptionB, tokens, "\\");
736 lengthB=atoi(tokens[0].c_str()); // Blue LUT length in Bytes
737 debB =atoi(tokens[1].c_str());
738 nbitsB =atoi(tokens[2].c_str());
741 // Load LUTs into memory, (as they were stored on disk)
742 unsigned char *lutR = (unsigned char *)
743 GetPubEntryVoidAreaByNumber(0x0028,0x1201);
744 unsigned char *lutG = (unsigned char *)
745 GetPubEntryVoidAreaByNumber(0x0028,0x1202);
746 unsigned char *lutB = (unsigned char *)
747 GetPubEntryVoidAreaByNumber(0x0028,0x1203);
749 if (!lutR || !lutG || !lutB ) {
752 // forge the 4 * 8 Bits Red/Green/Blue/Alpha LUT
754 unsigned char *LUTRGBA = (unsigned char *)calloc(1024,1); // 256 * 4 (R, G, B, Alpha)
758 memset(LUTRGBA, 0, 1024);
761 std::string str_nb = GetEntryByNumber(0x0028,0x0100);
762 if (str_nb == GDCM_UNFOUND ) {
765 nb = atoi(str_nb.c_str() );
769 if (nbitsR==16 && nb==8) // when LUT item size is different than pixel size
770 mult=2; // high byte must be = low byte
771 else // See PS 3.3-2003 C.11.1.1.2 p 619
774 // if we get a black image, let's just remove the '+1'
775 // from 'i*mult+1' and check again
776 // if it works, we shall have to check the 3 Palettes
777 // to see which byte is ==0 (first one, or second one)
779 // We give up the checking to avoid some overhead
784 for(i=0;i<lengthR;i++) {
789 for(i=0;i<lengthG;i++) {
794 for(i=0;i<lengthB;i++) {
800 *a = 1; // Alpha component
804 //How to free the now useless LUTs?
806 //free(LutR); free(LutB); free(LutG); // Seg Fault when used
811 * \ingroup gdcmHeader
812 * \brief gets the info from 0002,0010 : Transfert Syntax
814 * @return Transfert Syntax Name (as oposite to Transfert Syntax UID)
816 std::string gdcmHeader::GetTransfertSyntaxName(void) {
817 // use the gdcmTS (TS : Transfert Syntax)
818 std::string TransfertSyntax = GetEntryByNumber(0x0002,0x0010);
819 if (TransfertSyntax == GDCM_UNFOUND) {
820 dbg.Verbose(0, "gdcmHeader::GetTransfertSyntaxName: unfound Transfert Syntax (0002,0010)");
821 return "Uncompressed ACR-NEMA";
823 // we do it only when we need it
824 gdcmTS * ts = gdcmGlobal::GetTS();
825 std::string tsName=ts->GetValue(TransfertSyntax);
826 //delete ts; // Seg Fault when deleted ?!
831 * \ingroup gdcmHeader
832 * \brief Searches within the file Header for element value of
834 * @param tagName name of the searched element.
835 * @return Corresponding element value when it exists, and the string
836 * GDCM_UNFOUND ("gdcm::Unfound") otherwise.
838 std::string gdcmHeader::GetPubEntryByName(std::string tagName) {
839 gdcmDictEntry *dictEntry = RefPubDict->GetTagByName(tagName);
840 if( dictEntry == NULL)
843 return(GetEntryByNumber(dictEntry->GetGroup(),dictEntry->GetElement()));
847 * \ingroup gdcmHeader
848 * \brief Searches within the elements parsed with the file Header for
849 * the element value representation of a given tag.
851 * Obtaining the VR (Value Representation) might be needed by caller
852 * to convert the string typed content to caller's native type
853 * (think of C++ vs Python). The VR is actually of a higher level
854 * of semantics than just the native C++ type.
855 * @param tagName name of the searched element.
856 * @return Corresponding element value representation when it exists,
857 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
859 std::string gdcmHeader::GetPubEntryVRByName(std::string tagName) {
860 gdcmDictEntry *dictEntry = RefPubDict->GetTagByName(tagName);
861 if( dictEntry == NULL)
864 gdcmHeaderEntry* elem = GetHeaderEntryByNumber(dictEntry->GetGroup(),
865 dictEntry->GetElement());
866 return elem->GetVR();
871 * \ingroup gdcmHeader
872 * \brief Searches within the public dictionary for element value
873 * representation of a given tag.
875 * Obtaining the VR (Value Representation) might be needed by caller
876 * to convert the string typed content to caller's native type
877 * (think of C++ vs Python). The VR is actually of a higher level
878 * of semantics than just the native C++ type.
879 * @param group Group of the researched tag.
880 * @param element Element of the researched tag.
881 * @return Corresponding element value representation when it exists,
882 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
884 std::string gdcmHeader::GetPubEntryVRByNumber(guint16 group, guint16 element) {
885 gdcmHeaderEntry* elem = GetHeaderEntryByNumber(group, element);
888 return elem->GetVR();
892 * \ingroup gdcmHeader
893 * \brief Accesses an existing gdcmHeaderEntry (i.e. a Dicom Element)
894 * in the PubHeaderEntrySet of this instance
895 * through tag name and modifies it's content with the given value.
896 * @param content new value to substitute with
897 * @param tagName name of the Header Entry (Dicom Element) to be modified
899 bool gdcmHeader::SetPubEntryByName(std::string content, std::string tagName) {
900 //return ( PubHeaderEntrySet.SetHeaderEntryByName (content, tagName) );
901 gdcmDictEntry *dictEntry = RefPubDict->GetTagByName(tagName);
902 if( dictEntry == NULL)
904 return(SetEntryByNumber(content,
905 dictEntry->GetGroup(),
906 dictEntry->GetElement()));
910 * \ingroup gdcmHeader
911 * \brief Accesses an existing gdcmHeaderEntry (i.e. a Dicom Element)
912 * through it's (group, element) and modifies it's content with
914 * @param content new value to substitute with
915 * @param group group of the Dicom Element to modify
916 * @param element element of the Dicom Element to modify
918 bool gdcmHeader::SetPubEntryByNumber(std::string content,
921 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
922 if ( ! tagHT.count(key))
924 int l = content.length();
925 if(l%2) { // Odd length are padded with a space (020H).
927 content = content + '\0';
930 //tagHT[key]->SetValue(content);
933 TagHeaderEntryHT::iterator p2;
934 // DO NOT remove the following lines : they explain the stuff
935 //p= tagHT.equal_range(key); // get a pair of iterators first-last synonym
936 //p2=p.first; // iterator on the first synonym
937 //a=p2->second; // H Table target column (2-nd col)
940 a = ((tagHT.equal_range(key)).first)->second;
942 a-> SetValue(content);
944 //std::string vr = tagHT[key]->GetVR();
945 std::string vr = a->GetVR();
948 if( (vr == "US") || (vr == "SS") )
950 else if( (vr == "UL") || (vr == "SL") )
954 //tagHT[key]->SetLength(lgr);
960 * \ingroup gdcmHeader
961 * \brief Accesses an existing gdcmHeaderEntry (i.e. a Dicom Element)
962 * through it's (group, element) and modifies it's content with
964 * \warning Don't use any longer : use SetPubEntryByNumber
965 * @param content new value to substitute with
966 * @param group group of the Dicom Element to modify
967 * @param element element of the Dicom Element to modify
969 bool gdcmHeader::SetEntryByNumber(std::string content,
972 return SetPubEntryByNumber(content, group, element);
976 * \ingroup gdcmHeader
977 * \brief Accesses an existing gdcmHeaderEntry (i.e. a Dicom Element)
978 * in the PubHeaderEntrySet of this instance
979 * through it's (group, element) and modifies it's length with
981 * \warning Use with extreme caution.
982 * @param length new length to substitute with
983 * @param group group of the ElVal to modify
984 * @param element element of the ElVal to modify
985 * @return 1 on success, 0 otherwise.
988 bool gdcmHeader::SetPubEntryLengthByNumber(guint32 length,
989 guint16 group, guint16 element) {
990 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
991 if ( ! tagHT.count(key))
993 if (length%2) length++; // length must be even
994 //tagHT[key]->SetLength(length);
995 ( ((tagHT.equal_range(key)).first)->second )->SetLength(length);
1001 * \ingroup gdcmHeader
1002 * \brief Accesses an existing gdcmHeaderEntry (i.e. a Dicom Element)
1003 * in the PubHeaderEntrySet of this instance
1004 * through it's (group, element) and modifies it's length with
1006 * \warning Don't use any longer : use SetPubEntryLengthByNumber
1007 * @param length new length to substitute with
1008 * @param group group of the ElVal to modify
1009 * @param element element of the ElVal to modify
1010 * @return 1 on success, 0 otherwise.
1013 bool gdcmHeader::SetEntryLengthByNumber(guint32 length,
1014 guint16 group, guint16 element) {
1015 return SetPubEntryLengthByNumber( length, group,element);
1018 * \ingroup gdcmHeader
1019 * \brief Searches within Header Entries (Dicom Elements) parsed with
1020 * the public and private dictionaries
1021 * for the element value of a given tag.
1022 * \warning Don't use any longer : use GetPubEntryByName
1023 * @param tagName name of the searched element.
1024 * @return Corresponding element value when it exists,
1025 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
1027 std::string gdcmHeader::GetEntryByName(std::string tagName) {
1028 return GetPubEntryByName(tagName);
1032 * \ingroup gdcmHeader
1033 * \brief Searches within Header Entries (Dicom Elements) parsed with
1034 * the public and private dictionaries
1035 * for the element value representation of a given tag.
1037 * Obtaining the VR (Value Representation) might be needed by caller
1038 * to convert the string typed content to caller's native type
1039 * (think of C++ vs Python). The VR is actually of a higher level
1040 * of semantics than just the native C++ type.
1041 * @param tagName name of the searched element.
1042 * @return Corresponding element value representation when it exists,
1043 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
1045 std::string gdcmHeader::GetEntryVRByName(std::string tagName) {
1046 return GetPubEntryVRByName(tagName);
1050 * \ingroup gdcmHeader
1051 * \brief Searches within Header Entries (Dicom Elements) parsed with
1052 * the public and private dictionaries
1053 * for the element value representation of a given tag.
1054 * @param group Group of the searched tag.
1055 * @param element Element of the searched tag.
1056 * @return Corresponding element value representation when it exists,
1057 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
1059 std::string gdcmHeader::GetEntryByNumber(guint16 group, guint16 element) {
1060 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
1061 if ( ! tagHT.count(key))
1062 return GDCM_UNFOUND;
1063 return tagHT.find(key)->second->GetValue();
1067 * \ingroup gdcmHeader
1068 * \brief Searches within the public dictionary for element value of
1070 * @param group Group of the researched tag.
1071 * @param element Element of the researched tag.
1072 * @return Corresponding element value when it exists, and the string
1073 * GDCM_UNFOUND ("gdcm::Unfound") otherwise.
1075 std::string gdcmHeader::GetPubEntryByNumber(guint16 group, guint16 element) {
1076 return GetEntryByNumber(group, element);
1081 * \ingroup gdcmHeader
1082 * \brief Searches within Header Entries (Dicom Elements) parsed with
1083 * the public and private dictionaries
1084 * for the element value representation of a given tag..
1086 * Obtaining the VR (Value Representation) might be needed by caller
1087 * to convert the string typed content to caller's native type
1088 * (think of C++ vs Python). The VR is actually of a higher level
1089 * of semantics than just the native C++ type.
1090 * @param group Group of the searched tag.
1091 * @param element Element of the searched tag.
1092 * @return Corresponding element value representation when it exists,
1093 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
1095 std::string gdcmHeader::GetEntryVRByNumber(guint16 group, guint16 element) {
1096 return GetPubEntryVRByNumber(group, element);
1100 * \ingroup gdcmHeader
1101 * \brief Sets the value (string) of the Header Entry (Dicom Element)
1102 * @param content string value of the Dicom Element
1103 * @param tagName name of the searched Dicom Element.
1104 * @return true when found
1106 bool gdcmHeader::SetEntryByName(std::string content,std::string tagName) {
1107 gdcmDictEntry *dictEntry = RefPubDict->GetTagByName(tagName);
1108 if( dictEntry == NULL)
1112 TagKey key = gdcmDictEntry::TranslateToKey(dictEntry->GetGroup(),
1113 dictEntry->GetElement());
1114 if ( GetPubEntry().count(key) == 0 )
1116 int l = content.length();
1117 if(l%2) { // Odd length are padded with a space (020H).
1119 content = content + '\0';
1122 //tagHt[key]->SetValue(content);
1123 gdcmHeaderEntry * a;
1125 TagHeaderEntryHT::iterator p2;
1126 // DO NOT remove the following lines : they explain how the stuff works
1127 //p= tagHt.equal_range(key); // get a pair of iterators first-last synonym
1128 //p2=p.first; // iterator on the first synonym
1129 //a=p2->second; // H Table target column (2-nd col)
1131 a = ((GetPubEntry().equal_range(key)).first)->second;
1132 a-> SetValue(content);
1133 std::string vr = a->GetVR();
1136 if( (vr == "US") || (vr == "SS") )
1138 else if( (vr == "UL") || (vr == "SL") )
1147 * \ingroup gdcmHeader
1148 * \brief opens the file
1149 * @param exception_on_error
1152 FILE *gdcmHeader::OpenFile(bool exception_on_error)
1153 throw(gdcmFileError) {
1154 fp=fopen(filename.c_str(),"rb");
1155 if(exception_on_error) {
1157 throw gdcmFileError("gdcmHeader::gdcmHeader(const char *, bool)");
1162 fread(&zero, (size_t)2, (size_t)1, fp);
1164 //ACR -- or DICOM with no Preamble --
1165 if( zero == 0x0008 || zero == 0x0800 || zero == 0x0002 || zero == 0x0200)
1169 fseek(fp, 126L, SEEK_CUR);
1171 fread(dicm, (size_t)4, (size_t)1, fp);
1172 if( memcmp(dicm, "DICM", 4) == 0 )
1176 dbg.Verbose(0, "gdcmHeader::gdcmHeader not DICOM/ACR", filename.c_str());
1179 dbg.Verbose(0, "gdcmHeader::gdcmHeader cannot open file", filename.c_str());
1185 * \ingroup gdcmHeader
1186 * \brief closes the file
1187 * @return TRUE if the close was successfull
1189 bool gdcmHeader::CloseFile(void) {
1190 int closed = fclose(fp);
1198 * \ingroup gdcmHeader
1199 * \brief Parses the header of the file but WITHOUT loading element values.
1201 void gdcmHeader::ParseHeader(bool exception_on_error) throw(gdcmFormatError) {
1202 gdcmHeaderEntry * newHeaderEntry = (gdcmHeaderEntry *)0;
1206 while ( (newHeaderEntry = ReadNextHeaderEntry()) ) {
1207 SkipHeaderEntry(newHeaderEntry);
1208 Add(newHeaderEntry);
1215 * \ingroup gdcmHeader
1217 * @param fp file pointer on an already open file
1218 * @param type type of the File to be written
1219 * (ACR-NEMA, ExplicitVR, ImplicitVR)
1220 * @return always "True" ?!
1222 bool gdcmHeader::Write(FILE * fp, FileType type) {
1225 // TODO The stuff has been rewritten using the chained list instead
1227 // so we could remove the GroupHT from the gdcmHeader
1231 // TODO : move the following lines (and a lot of others, to be written)
1232 // to a future function CheckAndCorrectHeader
1235 // Comment pourrait-on savoir si le DcmHeader vient d'un fichier DicomV3 ou non
1236 // (FileType est un champ de gdcmHeader ...)
1237 // WARNING : Si on veut ecrire du DICOM V3 a partir d'un DcmHeader ACR-NEMA
1239 // a moins de se livrer a un tres complique ajout des champs manquants.
1240 // faire un CheckAndCorrectHeader (?)
1243 if (type == ImplicitVR) {
1244 std::string implicitVRTransfertSyntax = "1.2.840.10008.1.2";
1245 ReplaceOrCreateByNumber(implicitVRTransfertSyntax,0x0002, 0x0010);
1247 //FIXME Refer to standards on page 21, chapter 6.2 "Value representation":
1248 // values with a VR of UI shall be padded with a single trailing null
1249 // Dans le cas suivant on doit pader manuellement avec un 0
1251 SetEntryLengthByNumber(18, 0x0002, 0x0010);
1254 if (type == ExplicitVR) {
1255 std::string explicitVRTransfertSyntax = "1.2.840.10008.1.2.1";
1256 ReplaceOrCreateByNumber(explicitVRTransfertSyntax,0x0002, 0x0010);
1258 //FIXME Refer to standards on page 21, chapter 6.2 "Value representation":
1259 // values with a VR of UI shall be padded with a single trailing null
1260 // Dans le cas suivant on doit pader manuellement avec un 0
1262 SetEntryLengthByNumber(20, 0x0002, 0x0010);
1266 if ( (type == ImplicitVR) || (type == ExplicitVR) )
1267 UpdateGroupLength(false,type);
1269 UpdateGroupLength(true,ACR);
1271 WriteEntries(type, fp);
1277 * \brief Sets the Pixel Area size in the Header
1278 * --> not-for-rats function
1280 * \warning WARNING doit-etre etre publique ?
1281 * TODO : y aurait il un inconvenient à fusionner ces 2 fonctions
1283 * @param ImageDataSize new Pixel Area Size
1284 * warning : nothing else is checked
1286 void gdcmHeader::SetImageDataSize(size_t ImageDataSize) {
1287 std::string content1;
1289 // Assumes HeaderEntry (0x7fe0, 0x0010) exists ...
1290 sprintf(car,"%d",ImageDataSize);
1292 gdcmHeaderEntry *a = GetHeaderEntryByNumber(0x7fe0, 0x0010);
1293 a->SetLength(ImageDataSize);
1296 sprintf(car,"%d",ImageDataSize);
1298 SetPubEntryByNumber(content1, 0x7fe0, 0x0000);
1302 * \ingroup gdcmHeader
1303 * \brief Modifies the value of a given Header Entry (Dicom Element)
1304 * if it exists; Creates it with the given value if it doesn't
1305 * @param Value passed as a std::string
1310 bool gdcmHeader::ReplaceOrCreateByNumber(std::string Value,
1311 guint16 Group, guint16 Elem ) {
1313 if (CheckIfExistByNumber(Group, Elem) == 0) {
1314 gdcmHeaderEntry* a =NewHeaderEntryByNumber(Group, Elem);
1319 SetPubEntryByNumber(Value, Group, Elem);
1324 * \ingroup gdcmHeader
1325 * \brief Modifies the value of a given Header Entry (Dicom Element)
1326 * if it exists; Creates it with the given value if it doesn't
1327 * @param Value passed as a char*
1333 bool gdcmHeader::ReplaceOrCreateByNumber(char* Value, guint16 Group, guint16 Elem ) {
1335 gdcmHeaderEntry* nvHeaderEntry=NewHeaderEntryByNumber(Group, Elem);
1336 // TODO : check if fails
1338 std::string v = Value;
1339 SetEntryByNumber(v, Group, Elem);
1344 * \ingroup gdcmHeader
1345 * \brief Set a new value if the invoked element exists
1346 * Seems to be useless !!!
1352 bool gdcmHeader::ReplaceIfExistByNumber(char* Value, guint16 Group, guint16 Elem ) {
1354 std::string v = Value;
1355 SetEntryByNumber(v, Group, Elem);
1360 * \ingroup gdcmHeader
1361 * \brief Swaps back the bytes of 4-byte long integer accordingly to
1363 * @return The properly swaped 32 bits integer.
1365 guint32 gdcmHeader::SwapLong(guint32 a) {
1370 a=( ((a<<24) & 0xff000000) | ((a<<8) & 0x00ff0000) |
1371 ((a>>8) & 0x0000ff00) | ((a>>24) & 0x000000ff) );
1375 a=( ((a<<16) & 0xffff0000) | ((a>>16) & 0x0000ffff) );
1379 a=( ((a<<8) & 0xff00ff00) | ((a>>8) & 0x00ff00ff) );
1382 dbg.Error(" gdcmHeader::SwapLong : unset swap code");
1389 * \ingroup gdcmHeader
1390 * \brief Swaps the bytes so they agree with the processor order
1391 * @return The properly swaped 16 bits integer.
1393 guint16 gdcmHeader::SwapShort(guint16 a) {
1394 if ( (sw==4321) || (sw==2143) )
1395 a =(((a<<8) & 0x0ff00) | ((a>>8)&0x00ff));
1399 //-----------------------------------------------------------------------------
1403 * \ingroup gdcmHeader
1404 * \brief retrieves a Dicom Element (the first one) using (group, element)
1405 * \ warning (group, element) IS NOT an identifier inside the Dicom Header
1406 * if you think it's NOT UNIQUE, check the count number
1407 * and use iterators to retrieve ALL the Dicoms Elements within
1408 * a given couple (group, element)
1409 * @param group Group number of the searched Dicom Element
1410 * @param element Element number of the searched Dicom Element
1413 gdcmHeaderEntry* gdcmHeader::GetHeaderEntryByNumber(guint16 group, guint16 element) {
1414 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
1415 if ( ! tagHT.count(key))
1417 return tagHT.find(key)->second;
1422 * \ingroup gdcmHeader
1423 * \brief Searches within the Header Entries for a Dicom Element of
1425 * @param tagName name of the searched Dicom Element.
1426 * @return Corresponding Dicom Element when it exists, and NULL
1429 gdcmHeaderEntry *gdcmHeader::GetHeaderEntryByName(std::string tagName) {
1430 gdcmDictEntry *dictEntry = RefPubDict->GetTagByName(tagName);
1431 if( dictEntry == NULL)
1434 return(GetHeaderEntryByNumber(dictEntry->GetGroup(),dictEntry->GetElement()));
1438 * \ingroup gdcmHeader
1439 * \brief Checks if a given Dicom Element exists
1440 * \ within the H table
1441 * @param group Group number of the searched Dicom Element
1442 * @param element Element number of the searched Dicom Element
1443 * @return number of occurences
1445 int gdcmHeader::CheckIfExistByNumber(guint16 group, guint16 element ) {
1446 std::string key = gdcmDictEntry::TranslateToKey(group, element );
1447 return (tagHT.count(key));
1451 * \ingroup gdcmHeader
1452 * \brief Gets (from Header) the offset of a 'non string' element value
1453 * \ (LoadElementValues has already be executed)
1456 * @return File Offset of the Element Value
1458 size_t gdcmHeader::GetPubEntryOffsetByNumber(guint16 Group, guint16 Elem) {
1459 gdcmHeaderEntry* Entry = GetHeaderEntryByNumber(Group, Elem);
1461 dbg.Verbose(1, "gdcmHeader::GetHeaderEntryByNumber",
1462 "failed to Locate gdcmHeaderEntry");
1465 return Entry->GetOffset();
1469 * \ingroup gdcmHeader
1470 * \brief Gets (from Header) a 'non string' element value
1471 * \ (LoadElementValues has already be executed)
1474 * @return Pointer to the 'non string' area
1476 void * gdcmHeader::GetPubEntryVoidAreaByNumber(guint16 Group, guint16 Elem) {
1477 gdcmHeaderEntry* Entry = GetHeaderEntryByNumber(Group, Elem);
1479 dbg.Verbose(1, "gdcmHeader::GetHeaderEntryByNumber",
1480 "failed to Locate gdcmHeaderEntry");
1483 return Entry->GetVoidArea();
1487 * \ingroup gdcmHeader
1488 * \brief Loads (from disk) the element content
1489 * when a string is not suitable
1491 void * gdcmHeader::LoadEntryVoidArea(guint16 Group, guint16 Elem) {
1492 gdcmHeaderEntry * Element= GetHeaderEntryByNumber(Group, Elem);
1495 size_t o =(size_t)Element->GetOffset();
1496 fseek(fp, o, SEEK_SET);
1497 int l=Element->GetLength();
1498 void * a = malloc(l);
1503 SetVoidAreaByNumber(a, Group, Elem);
1504 // TODO check the result
1505 size_t l2 = fread(a, 1, l ,fp);
1513 //-----------------------------------------------------------------------------
1516 * \ingroup gdcmHeader
1517 * \brief Loads the element values of all the Header Entries pointed in the
1518 * public Chained List.
1520 void gdcmHeader::LoadHeaderEntries(void) {
1522 for (ListTag::iterator i = GetPubListEntry().begin();
1523 i != GetPubListEntry().end();
1525 LoadHeaderEntry(*i);
1530 // Load 'non string' values
1531 std::string PhotometricInterpretation = GetPubEntryByNumber(0x0028,0x0004);
1532 if( PhotometricInterpretation == "PALETTE COLOR " ){
1533 LoadEntryVoidArea(0x0028,0x1200); // gray LUT
1534 LoadEntryVoidArea(0x0028,0x1201); // R LUT
1535 LoadEntryVoidArea(0x0028,0x1202); // G LUT
1536 LoadEntryVoidArea(0x0028,0x1203); // B LUT
1538 LoadEntryVoidArea(0x0028,0x1221); // Segmented Red Palette Color LUT Data
1539 LoadEntryVoidArea(0x0028,0x1222); // Segmented Green Palette Color LUT Data
1540 LoadEntryVoidArea(0x0028,0x1223); // Segmented Blue Palette Color LUT Data
1543 // --------------------------------------------------------------
1544 // Special Patch to allow gdcm to read ACR-LibIDO formated images
1546 // if recognition code tells us we deal with a LibIDO image
1547 // we switch lineNumber and columnNumber
1549 std::string RecCode;
1550 RecCode = GetPubEntryByNumber(0x0008, 0x0010); // recognition code
1551 if (RecCode == "ACRNEMA_LIBIDO_1.1" ||
1552 RecCode == "CANRME_AILIBOD1_1." ) {
1553 filetype = ACR_LIBIDO;
1554 std::string rows = GetPubEntryByNumber(0x0028, 0x0010);
1555 std::string columns = GetPubEntryByNumber(0x0028, 0x0011);
1556 SetPubEntryByNumber(columns, 0x0028, 0x0010);
1557 SetPubEntryByNumber(rows , 0x0028, 0x0011);
1559 // ----------------- End of Special Patch ----------------
1563 * \ingroup gdcmHeader
1564 * \brief Loads the element content if it's length is not bigger
1565 * than the value specified with
1566 * gdcmHeader::SetMaxSizeLoadElementValue()
1567 * @param ElVal Header Entry (Dicom Element) to be dealt with
1569 void gdcmHeader::LoadHeaderEntry(gdcmHeaderEntry * ElVal) {
1571 guint16 group = ElVal->GetGroup();
1572 std::string vr= ElVal->GetVR();
1573 guint32 length = ElVal->GetLength();
1574 bool SkipLoad = false;
1576 fseek(fp, (long)ElVal->GetOffset(), SEEK_SET);
1578 // the test was commented out to 'go inside' the SeQuences
1579 // we don't any longer skip them !
1581 // if( vr == "SQ" ) // (DO NOT remove this comment)
1584 // A SeQuence "contains" a set of Elements.
1585 // (fffe e000) tells us an Element is beginning
1586 // (fffe e00d) tells us an Element just ended
1587 // (fffe e0dd) tells us the current SeQuence just ended
1588 if( group == 0xfffe )
1592 ElVal->SetLength(0);
1593 ElVal->SetValue("gdcm::Skipped");
1597 // When the length is zero things are easy:
1598 if ( length == 0 ) {
1599 ElVal->SetValue("");
1603 // The elements whose length is bigger than the specified upper bound
1604 // are not loaded. Instead we leave a short notice of the offset of
1605 // the element content and it's length.
1606 if (length > MaxSizeLoadElementValue) {
1607 std::ostringstream s;
1608 s << "gdcm::NotLoaded.";
1609 s << " Address:" << (long)ElVal->GetOffset();
1610 s << " Length:" << ElVal->GetLength();
1611 s << " x(" << std::hex << ElVal->GetLength() << ")";
1612 ElVal->SetValue(s.str());
1616 // When integer(s) are expected, read and convert the following
1617 // n *(two or four bytes)
1618 // properly i.e. as integers as opposed to a strings.
1619 // Elements with Value Multiplicity > 1
1620 // contain a set of integers (not a single one)
1622 // Any compacter code suggested (?)
1623 if ( IsHeaderEntryAnInteger(ElVal) ) {
1625 std::ostringstream s;
1627 if (vr == "US" || vr == "SS") {
1629 NewInt = ReadInt16();
1632 for (int i=1; i < nbInt; i++) {
1634 NewInt = ReadInt16();
1639 } else if (vr == "UL" || vr == "SL") {
1641 NewInt = ReadInt32();
1644 for (int i=1; i < nbInt; i++) {
1646 NewInt = ReadInt32();
1651 #ifdef GDCM_NO_ANSI_STRING_STREAM
1652 s << std::ends; // to avoid oddities on Solaris
1653 #endif //GDCM_NO_ANSI_STRING_STREAM
1654 ElVal->SetValue(s.str());
1658 // We need an additional byte for storing \0 that is not on disk
1659 char* NewValue = (char*)malloc(length+1);
1661 dbg.Verbose(1, "LoadElementValue: Failed to allocate NewValue");
1664 NewValue[length]= 0;
1666 item_read = fread(NewValue, (size_t)length, (size_t)1, fp);
1667 if ( item_read != 1 ) {
1669 dbg.Verbose(1, "gdcmHeader::LoadElementValue","unread element value");
1670 ElVal->SetValue("gdcm::UnRead");
1673 ElVal->SetValue(NewValue);
1678 * \ingroup gdcmHeader
1679 * \brief Loads the element while preserving the current
1680 * underlying file position indicator as opposed to
1681 * to LoadHeaderEntry that modifies it.
1682 * @param entry Header Entry whose value shall be loaded.
1685 void gdcmHeader::LoadHeaderEntrySafe(gdcmHeaderEntry * entry) {
1686 long PositionOnEntry = ftell(fp);
1687 LoadHeaderEntry(entry);
1688 fseek(fp, PositionOnEntry, SEEK_SET);
1692 * \ingroup gdcmHeader
1694 * @param ElVal Header Entry whose length of the value shall be loaded.
1698 void gdcmHeader::FindHeaderEntryLength (gdcmHeaderEntry * ElVal) {
1699 guint16 element = ElVal->GetElement();
1700 guint16 group = ElVal->GetGroup();
1701 std::string vr = ElVal->GetVR();
1703 if( (element == 0x0010) && (group == 0x7fe0) ) {
1705 dbg.Verbose(2, "gdcmHeader::FindLength: ",
1706 "we reached 7fe0 0010");
1709 if ( (filetype == ExplicitVR) && ! ElVal->IsImplicitVr() ) {
1710 if ( (vr=="OB") || (vr=="OW") || (vr=="SQ") || (vr=="UN") ) {
1712 // The following reserved two bytes (see PS 3.5-2001, section
1713 // 7.1.2 Data element structure with explicit vr p27) must be
1714 // skipped before proceeding on reading the length on 4 bytes.
1715 fseek(fp, 2L, SEEK_CUR);
1717 guint32 length32 = ReadInt32();
1719 if ( (vr == "OB") && (length32 == 0xffffffff) ) {
1720 ElVal->SetLength(FindHeaderEntryLengthOB());
1723 FixHeaderEntryFoundLength(ElVal, length32);
1727 // Length is encoded on 2 bytes.
1728 length16 = ReadInt16();
1730 // We can tell the current file is encoded in big endian (like
1731 // Data/US-RGB-8-epicard) when we find the "Transfer Syntax" tag
1732 // and it's value is the one of the encoding of a big endian file.
1733 // In order to deal with such big endian encoded files, we have
1734 // (at least) two strategies:
1735 // * when we load the "Transfer Syntax" tag with value of big endian
1736 // encoding, we raise the proper flags. Then we wait for the end
1737 // of the META group (0x0002) among which is "Transfer Syntax",
1738 // before switching the swap code to big endian. We have to postpone
1739 // the switching of the swap code since the META group is fully encoded
1740 // in little endian, and big endian coding only starts at the next
1741 // group. The corresponding code can be hard to analyse and adds
1742 // many additional unnecessary tests for regular tags.
1743 // * the second strategy consists in waiting for trouble, that shall
1744 // appear when we find the first group with big endian encoding. This
1745 // is easy to detect since the length of a "Group Length" tag (the
1746 // ones with zero as element number) has to be of 4 (0x0004). When we
1747 // encounter 1024 (0x0400) chances are the encoding changed and we
1748 // found a group with big endian encoding.
1749 // We shall use this second strategy. In order to make sure that we
1750 // can interpret the presence of an apparently big endian encoded
1751 // length of a "Group Length" without committing a big mistake, we
1752 // add an additional check: we look in the already parsed elements
1753 // for the presence of a "Transfer Syntax" whose value has to be "big
1754 // endian encoding". When this is the case, chances are we have got our
1755 // hands on a big endian encoded file: we switch the swap code to
1756 // big endian and proceed...
1757 if ( (element == 0x0000) && (length16 == 0x0400) ) {
1758 if ( ! IsExplicitVRBigEndianTransferSyntax() ) {
1759 dbg.Verbose(0, "gdcmHeader::FindLength", "not explicit VR");
1764 SwitchSwapToBigEndian();
1765 // Restore the unproperly loaded values i.e. the group, the element
1766 // and the dictionary entry depending on them.
1767 guint16 CorrectGroup = SwapShort(ElVal->GetGroup());
1768 guint16 CorrectElem = SwapShort(ElVal->GetElement());
1769 gdcmDictEntry * NewTag = GetDictEntryByNumber(CorrectGroup,
1772 // This correct tag is not in the dictionary. Create a new one.
1773 NewTag = Dicts->NewVirtualDictEntry(CorrectGroup, CorrectElem);
1775 // FIXME this can create a memory leaks on the old entry that be
1776 // left unreferenced.
1777 ElVal->SetDictEntry(NewTag);
1780 // Heuristic: well some files are really ill-formed.
1781 if ( length16 == 0xffff) {
1783 //dbg.Verbose(0, "gdcmHeader::FindLength",
1784 // "Erroneous element length fixed.");
1785 // Actually, length= 0xffff means that we deal with
1786 // Unknown Sequence Length
1789 FixHeaderEntryFoundLength(ElVal, (guint32)length16);
1793 // Either implicit VR or a non DICOM conformal (see not below) explicit
1794 // VR that ommited the VR of (at least) this element. Farts happen.
1795 // [Note: according to the part 5, PS 3.5-2001, section 7.1 p25
1796 // on Data elements "Implicit and Explicit VR Data Elements shall
1797 // not coexist in a Data Set and Data Sets nested within it".]
1798 // Length is on 4 bytes.
1799 FixHeaderEntryFoundLength(ElVal, ReadInt32());
1804 * \ingroup gdcmHeader
1805 * \brief Find the Value Representation of the current Dicom Element.
1808 void gdcmHeader::FindHeaderEntryVR( gdcmHeaderEntry *ElVal) {
1809 if (filetype != ExplicitVR)
1815 char msg[100]; // for sprintf. Sorry
1817 long PositionOnEntry = ftell(fp);
1818 // Warning: we believe this is explicit VR (Value Representation) because
1819 // we used a heuristic that found "UL" in the first tag. Alas this
1820 // doesn't guarantee that all the tags will be in explicit VR. In some
1821 // cases (see e-film filtered files) one finds implicit VR tags mixed
1822 // within an explicit VR file. Hence we make sure the present tag
1823 // is in explicit VR and try to fix things if it happens not to be
1825 bool RealExplicit = true;
1827 lgrLue=fread (&VR, (size_t)2,(size_t)1, fp);
1829 vr = std::string(VR);
1831 // Assume we are reading a falsely explicit VR file i.e. we reached
1832 // a tag where we expect reading a VR but are in fact we read the
1833 // first to bytes of the length. Then we will interogate (through find)
1834 // the dicom_vr dictionary with oddities like "\004\0" which crashes
1835 // both GCC and VC++ implementations of the STL map. Hence when the
1836 // expected VR read happens to be non-ascii characters we consider
1837 // we hit falsely explicit VR tag.
1839 if ( (!isalpha(VR[0])) && (!isalpha(VR[1])) )
1840 RealExplicit = false;
1842 // CLEANME searching the dicom_vr at each occurence is expensive.
1843 // PostPone this test in an optional integrity check at the end
1844 // of parsing or only in debug mode.
1845 if ( RealExplicit && !dicom_vr->Count(vr) )
1846 RealExplicit= false;
1848 if ( RealExplicit ) {
1849 if ( ElVal->IsVRUnknown() ) {
1850 // When not a dictionary entry, we can safely overwrite the VR.
1854 if ( ElVal->GetVR() == vr ) {
1855 // The VR we just read and the dictionary agree. Nothing to do.
1858 // The VR present in the file and the dictionary disagree. We assume
1859 // the file writer knew best and use the VR of the file. Since it would
1860 // be unwise to overwrite the VR of a dictionary (since it would
1861 // compromise it's next user), we need to clone the actual DictEntry
1862 // and change the VR for the read one.
1863 gdcmDictEntry* NewTag = Dicts->NewVirtualDictEntry(ElVal->GetGroup(),
1864 ElVal->GetElement(),
1868 ElVal->SetDictEntry(NewTag);
1872 // We thought this was explicit VR, but we end up with an
1873 // implicit VR tag. Let's backtrack.
1875 sprintf(msg,"Falsely explicit vr file (%04x,%04x)\n",
1876 ElVal->GetGroup(),ElVal->GetElement());
1877 dbg.Verbose(1, "gdcmHeader::FindVR: ",msg);
1879 fseek(fp, PositionOnEntry, SEEK_SET);
1880 // When this element is known in the dictionary we shall use, e.g. for
1881 // the semantics (see the usage of IsAnInteger), the VR proposed by the
1882 // dictionary entry. Still we have to flag the element as implicit since
1883 // we know now our assumption on expliciteness is not furfilled.
1885 if ( ElVal->IsVRUnknown() )
1886 ElVal->SetVR("Implicit");
1887 ElVal->SetImplicitVr();
1891 * \ingroup gdcmHeader
1892 * \brief Skip a given Header Entry
1893 * \warning NOT end user intended method !
1897 void gdcmHeader::SkipHeaderEntry(gdcmHeaderEntry * entry) {
1898 SkipBytes(entry->GetLength());
1902 * \ingroup gdcmHeader
1903 * \brief When the length of an element value is obviously wrong (because
1904 * the parser went Jabberwocky) one can hope improving things by
1905 * applying this heuristic.
1907 void gdcmHeader::FixHeaderEntryFoundLength(gdcmHeaderEntry * ElVal, guint32 FoundLength) {
1909 ElVal->SetReadLength(FoundLength); // will be updated only if a bug is found
1911 if ( FoundLength == 0xffffffff) {
1915 // Sorry for the patch!
1916 // XMedCom did the trick to read some nasty GE images ...
1917 else if (FoundLength == 13) {
1918 // The following 'if' will be removed when there is no more
1919 // images on Creatis HDs with a 13 length for Manufacturer...
1920 if ( (ElVal->GetGroup() != 0x0008) ||
1921 ( (ElVal->GetElement() != 0x0070) && (ElVal->GetElement() != 0x0080) ) ) {
1922 // end of remove area
1924 ElVal->SetReadLength(10); // a bug is to be fixed
1927 // to fix some garbage 'Leonardo' Siemens images
1928 // May be commented out to avoid overhead
1929 else if ( (ElVal->GetGroup() == 0x0009) &&
1930 ( (ElVal->GetElement() == 0x1113) || (ElVal->GetElement() == 0x1114) ) ){
1932 ElVal->SetReadLength(4); // a bug is to be fixed
1936 // to try to 'go inside' SeQuences (with length), and not to skip them
1937 else if ( ElVal->GetVR() == "SQ") {
1938 if (enableSequences) // only if the user does want to !
1942 // a SeQuence Element is beginning
1943 // Let's forget it's length
1944 // (we want to 'go inside')
1946 // Pb : *normaly* fffe|e000 is just a marker, its length *should be* zero
1947 // in gdcm-MR-PHILIPS-16-Multi-Seq.dcm we find lengthes as big as 28800
1948 // if we set the length to zero IsHeaderEntryAnInteger() breaks...
1949 // if we don't, we lost 28800 characters from the Header :-(
1951 else if(ElVal->GetGroup() == 0xfffe){
1952 // sometimes, length seems to be wrong
1953 FoundLength =0; // some more clever checking to be done !
1955 // only gdcm-MR-PHILIPS-16-Multi-Seq.dcm
1956 // causes troubles :-(
1959 ElVal->SetUsableLength(FoundLength);
1963 * \ingroup gdcmHeader
1964 * \brief Apply some heuristics to predict wether the considered
1965 * element value contains/represents an integer or not.
1966 * @param ElVal The element value on which to apply the predicate.
1967 * @return The result of the heuristical predicate.
1969 bool gdcmHeader::IsHeaderEntryAnInteger(gdcmHeaderEntry * ElVal) {
1970 guint16 element = ElVal->GetElement();
1971 guint16 group = ElVal->GetGroup();
1972 std::string vr = ElVal->GetVR();
1973 guint32 length = ElVal->GetLength();
1975 // When we have some semantics on the element we just read, and if we
1976 // a priori know we are dealing with an integer, then we shall be
1977 // able to swap it's element value properly.
1978 if ( element == 0 ) { // This is the group length of the group
1982 std::ostringstream s;
1983 s << "Erroneous Group Length element length on :" \
1984 << std::hex << group << " , " << element;
1985 dbg.Error("gdcmHeader::IsAnInteger",
1989 if ( (vr == "UL") || (vr == "US") || (vr == "SL") || (vr == "SS") )
1996 * \ingroup gdcmHeader
2001 guint32 gdcmHeader::FindHeaderEntryLengthOB(void) {
2002 // See PS 3.5-2001, section A.4 p. 49 on encapsulation of encoded pixel data.
2005 long PositionOnEntry = ftell(fp);
2006 bool FoundSequenceDelimiter = false;
2007 guint32 TotalLength = 0;
2010 while ( ! FoundSequenceDelimiter) {
2015 TotalLength += 4; // We even have to decount the group and element
2017 if ( g != 0xfffe && g!=0xb00c ) /*for bogus header */ {
2018 char msg[100]; // for sprintf. Sorry
2019 sprintf(msg,"wrong group (%04x) for an item sequence (%04x,%04x)\n",g, g,n);
2020 dbg.Verbose(1, "gdcmHeader::FindLengthOB: ",msg);
2024 if ( n == 0xe0dd || ( g==0xb00c && n==0x0eb6 ) ) /* for bogus header */
2025 FoundSequenceDelimiter = true;
2026 else if ( n != 0xe000 ){
2027 char msg[100]; // for sprintf. Sorry
2028 sprintf(msg,"wrong element (%04x) for an item sequence (%04x,%04x)\n",
2030 dbg.Verbose(1, "gdcmHeader::FindLengthOB: ",msg);
2034 ItemLength = ReadInt32();
2035 TotalLength += ItemLength + 4; // We add 4 bytes since we just read
2036 // the ItemLength with ReadInt32
2037 SkipBytes(ItemLength);
2039 fseek(fp, PositionOnEntry, SEEK_SET);
2044 * \ingroup gdcmHeader
2045 * \brief Reads a supposed to be 16 Bits integer
2046 * \ (swaps it depending on processor endianity)
2048 * @return integer acts as a boolean
2050 guint16 gdcmHeader::ReadInt16(void) {
2053 item_read = fread (&g, (size_t)2,(size_t)1, fp);
2054 if ( item_read != 1 ) {
2055 // dbg.Verbose(0, "gdcmHeader::ReadInt16", " Failed to read :");
2057 // dbg.Verbose(0, "gdcmHeader::ReadInt16", " End of File encountered");
2059 dbg.Verbose(0, "gdcmHeader::ReadInt16", " File Error");
2069 * \ingroup gdcmHeader
2070 * \brief Reads a supposed to be 32 Bits integer
2071 * \ (swaps it depending on processor endianity)
2075 guint32 gdcmHeader::ReadInt32(void) {
2078 item_read = fread (&g, (size_t)4,(size_t)1, fp);
2079 if ( item_read != 1 ) {
2080 //dbg.Verbose(0, "gdcmHeader::ReadInt32", " Failed to read :");
2082 // dbg.Verbose(0, "gdcmHeader::ReadInt32", " End of File encountered");
2084 dbg.Verbose(0, "gdcmHeader::ReadInt32", " File Error");
2094 * \ingroup gdcmHeader
2099 void gdcmHeader::SkipBytes(guint32 NBytes) {
2100 //FIXME don't dump the returned value
2101 (void)fseek(fp, (long)NBytes, SEEK_CUR);
2105 * \ingroup gdcmHeader
2108 void gdcmHeader::Initialise(void) {
2109 dicom_vr = gdcmGlobal::GetVR();
2110 dicom_ts = gdcmGlobal::GetTS();
2111 Dicts = gdcmGlobal::GetDicts();
2112 RefPubDict = Dicts->GetDefaultPubDict();
2113 RefShaDict = (gdcmDict*)0;
2117 * \ingroup gdcmHeader
2118 * \brief Discover what the swap code is (among little endian, big endian,
2119 * bad little endian, bad big endian).
2122 void gdcmHeader::CheckSwap()
2124 // Fourth semantics:
2126 // ---> Warning : This fourth field is NOT part
2127 // of the 'official' Dicom Dictionnary
2128 // and should NOT be used.
2129 // (Not defined for all the groups
2130 // may be removed in a future release)
2133 // META Meta Information
2145 // NMI Nuclear Medicine
2147 // BFS Basic Film Session
2148 // BFB Basic Film Box
2149 // BIB Basic Image Box
2164 // The only guaranted way of finding the swap code is to find a
2165 // group tag since we know it's length has to be of four bytes i.e.
2166 // 0x00000004. Finding the swap code in then straigthforward. Trouble
2167 // occurs when we can't find such group...
2169 guint32 x=4; // x : for ntohs
2170 bool net2host; // true when HostByteOrder is the same as NetworkByteOrder
2174 char deb[HEADER_LENGTH_TO_READ];
2176 // First, compare HostByteOrder and NetworkByteOrder in order to
2177 // determine if we shall need to swap bytes (i.e. the Endian type).
2182 //cout << net2host << endl;
2184 // The easiest case is the one of a DICOM header, since it possesses a
2185 // file preamble where it suffice to look for the string "DICM".
2186 lgrLue = fread(deb, 1, HEADER_LENGTH_TO_READ, fp);
2189 if(memcmp(entCur, "DICM", (size_t)4) == 0) {
2190 dbg.Verbose(1, "gdcmHeader::CheckSwap:", "looks like DICOM Version3");
2191 // Next, determine the value representation (VR). Let's skip to the
2192 // first element (0002, 0000) and check there if we find "UL"
2193 // - or "OB" if the 1st one is (0002,0001) -,
2194 // in which case we (almost) know it is explicit VR.
2195 // WARNING: if it happens to be implicit VR then what we will read
2196 // is the length of the group. If this ascii representation of this
2197 // length happens to be "UL" then we shall believe it is explicit VR.
2198 // FIXME: in order to fix the above warning, we could read the next
2199 // element value (or a couple of elements values) in order to make
2200 // sure we are not commiting a big mistake.
2201 // We need to skip :
2202 // * the 128 bytes of File Preamble (often padded with zeroes),
2203 // * the 4 bytes of "DICM" string,
2204 // * the 4 bytes of the first tag (0002, 0000),or (0002, 0001)
2205 // i.e. a total of 136 bytes.
2208 // Use gdcmHeader::dicom_vr to test all the possibilities
2209 // instead of just checking for UL, OB and UI !?
2210 if( (memcmp(entCur, "UL", (size_t)2) == 0) ||
2211 (memcmp(entCur, "OB", (size_t)2) == 0) ||
2212 (memcmp(entCur, "UI", (size_t)2) == 0) )
2214 filetype = ExplicitVR;
2215 dbg.Verbose(1, "gdcmHeader::CheckSwap:",
2216 "explicit Value Representation");
2218 filetype = ImplicitVR;
2219 dbg.Verbose(1, "gdcmHeader::CheckSwap:",
2220 "not an explicit Value Representation");
2224 dbg.Verbose(1, "gdcmHeader::CheckSwap:",
2225 "HostByteOrder != NetworkByteOrder");
2228 dbg.Verbose(1, "gdcmHeader::CheckSwap:",
2229 "HostByteOrder = NetworkByteOrder");
2232 // Position the file position indicator at first tag (i.e.
2233 // after the file preamble and the "DICM" string).
2235 fseek (fp, 132L, SEEK_SET);
2239 // Alas, this is not a DicomV3 file and whatever happens there is no file
2240 // preamble. We can reset the file position indicator to where the data
2241 // is (i.e. the beginning of the file).
2242 dbg.Verbose(1, "gdcmHeader::CheckSwap:", "not a DICOM Version3 file");
2245 // Our next best chance would be to be considering a 'clean' ACR/NEMA file.
2246 // By clean we mean that the length of the first tag is written down.
2247 // If this is the case and since the length of the first group HAS to be
2248 // four (bytes), then determining the proper swap code is straightforward.
2251 // We assume the array of char we are considering contains the binary
2252 // representation of a 32 bits integer. Hence the following dirty
2254 s = *((guint32 *)(entCur));
2274 dbg.Verbose(0, "gdcmHeader::CheckSwap:",
2275 "ACR/NEMA unfound swap info (time to raise bets)");
2278 // We are out of luck. It is not a DicomV3 nor a 'clean' ACR/NEMA file.
2279 // It is time for despaired wild guesses. So, let's assume this file
2280 // happens to be 'dirty' ACR/NEMA, i.e. the length of the group is
2281 // not present. Then the only info we have is the net2host one.
2291 * \ingroup gdcmHeader
2294 void gdcmHeader::SwitchSwapToBigEndian(void) {
2295 dbg.Verbose(1, "gdcmHeader::SwitchSwapToBigEndian",
2296 "Switching to BigEndian mode.");
2314 * \ingroup gdcmHeader
2319 void gdcmHeader::SetMaxSizeLoadElementValue(long NewSize) {
2322 if ((guint32)NewSize >= (guint32)0xffffffff) {
2323 MaxSizeLoadElementValue = 0xffffffff;
2326 MaxSizeLoadElementValue = NewSize;
2330 * \ingroup gdcmHeader
2331 * \brief Searches both the public and the shadow dictionary (when they
2332 * exist) for the presence of the DictEntry with given
2333 * group and element. The public dictionary has precedence on the
2335 * @param group group of the searched DictEntry
2336 * @param element element of the searched DictEntry
2337 * @return Corresponding DictEntry when it exists, NULL otherwise.
2339 gdcmDictEntry * gdcmHeader::GetDictEntryByNumber(guint16 group,
2341 gdcmDictEntry * found = (gdcmDictEntry*)0;
2342 if (!RefPubDict && !RefShaDict) {
2343 dbg.Verbose(0, "gdcmHeader::GetDictEntry",
2344 "we SHOULD have a default dictionary");
2347 found = RefPubDict->GetTagByNumber(group, element);
2352 found = RefShaDict->GetTagByNumber(group, element);
2360 * \ingroup gdcmHeader
2361 * \brief Searches both the public and the shadow dictionary (when they
2362 * exist) for the presence of the DictEntry with given name.
2363 * The public dictionary has precedence on the shadow one.
2364 * @param Name name of the searched DictEntry
2365 * @return Corresponding DictEntry when it exists, NULL otherwise.
2367 gdcmDictEntry * gdcmHeader::GetDictEntryByName(std::string Name) {
2368 gdcmDictEntry * found = (gdcmDictEntry*)0;
2369 if (!RefPubDict && !RefShaDict) {
2370 dbg.Verbose(0, "gdcmHeader::GetDictEntry",
2371 "we SHOULD have a default dictionary");
2374 found = RefPubDict->GetTagByName(Name);
2379 found = RefShaDict->GetTagByName(Name);
2387 * \ingroup gdcmHeader
2388 * \brief Read the next tag but WITHOUT loading it's value
2389 * @return On succes the newly created HeaderEntry, NULL on failure.
2391 gdcmHeaderEntry * gdcmHeader::ReadNextHeaderEntry(void) {
2394 gdcmHeaderEntry * NewElVal;
2400 // We reached the EOF (or an error occured) and header parsing
2401 // has to be considered as finished.
2402 return (gdcmHeaderEntry *)0;
2404 NewElVal = NewHeaderEntryByNumber(g, n);
2405 FindHeaderEntryVR(NewElVal);
2406 FindHeaderEntryLength(NewElVal);
2410 return (gdcmHeaderEntry *)0;
2412 NewElVal->SetOffset(ftell(fp));
2413 //if ( (g==0x7fe0) && (n==0x0010) )
2418 * \ingroup gdcmHeader
2419 * \brief Build a new Element Value from all the low level arguments.
2420 * Check for existence of dictionary entry, and build
2421 * a default one when absent.
2422 * @param Name Name of the underlying DictEntry
2424 gdcmHeaderEntry* gdcmHeader::NewHeaderEntryByName(std::string Name) {
2426 gdcmDictEntry * NewTag = GetDictEntryByName(Name);
2428 NewTag = Dicts->NewVirtualDictEntry(0xffff, 0xffff, "LO", "Unknown", Name);
2430 gdcmHeaderEntry* NewElVal = new gdcmHeaderEntry(NewTag);
2432 dbg.Verbose(1, "gdcmHeader::ObtainHeaderEntryByName",
2433 "failed to allocate gdcmHeaderEntry");
2434 return (gdcmHeaderEntry*)0;
2440 * \ingroup gdcmHeader
2441 * \brief Build a new Element Value from all the low level arguments.
2442 * Check for existence of dictionary entry, and build
2443 * a default one when absent.
2444 * @param Group group of the underlying DictEntry
2445 * @param Elem element of the underlying DictEntry
2447 gdcmHeaderEntry* gdcmHeader::NewHeaderEntryByNumber(guint16 Group, guint16 Elem) {
2448 // Find out if the tag we encountered is in the dictionaries:
2449 gdcmDictEntry * NewTag = GetDictEntryByNumber(Group, Elem);
2451 NewTag = Dicts->NewVirtualDictEntry(Group, Elem);
2453 gdcmHeaderEntry* NewElVal = new gdcmHeaderEntry(NewTag);
2455 dbg.Verbose(1, "gdcmHeader::NewHeaderEntryByNumber",
2456 "failed to allocate gdcmHeaderEntry");
2457 return (gdcmHeaderEntry*)0;
2463 * \ingroup gdcmHeader
2464 * \brief Small utility function that creates a new manually crafted
2465 * (as opposed as read from the file) gdcmHeaderEntry with user
2466 * specified name and adds it to the public tag hash table.
2467 * \note A fake TagKey is generated so the PubDict can keep it's coherence.
2468 * @param NewTagName The name to be given to this new tag.
2469 * @param VR The Value Representation to be given to this new tag.
2470 * @return The newly hand crafted Element Value.
2472 gdcmHeaderEntry* gdcmHeader::NewManualHeaderEntryToPubDict(std::string NewTagName,
2474 gdcmHeaderEntry* NewElVal = (gdcmHeaderEntry*)0;
2475 guint32 StuffGroup = 0xffff; // Group to be stuffed with additional info
2476 guint32 FreeElem = 0;
2477 gdcmDictEntry* NewEntry = (gdcmDictEntry*)0;
2479 FreeElem = GenerateFreeTagKeyInGroup(StuffGroup);
2480 if (FreeElem == UINT32_MAX) {
2481 dbg.Verbose(1, "gdcmHeader::NewManualElValToPubDict",
2482 "Group 0xffff in Public Dict is full");
2483 return (gdcmHeaderEntry*)0;
2485 NewEntry = Dicts->NewVirtualDictEntry(StuffGroup, FreeElem,
2486 VR, "GDCM", NewTagName);
2487 NewElVal = new gdcmHeaderEntry(NewEntry);
2492 //-----------------------------------------------------------------------------