- * \brief When the length of an element value is obviously wrong (because
- * the parser went Jabberwocky) one can hope improving things by
- * applying this heuristic.
- */
-void gdcmHeader::FixFoundLength(gdcmElValue * ElVal, guint32 FoundLength) {
- if ( FoundLength == 0xffffffff)
- FoundLength = 0;
- ElVal->SetLength(FoundLength);
-}
-
-guint32 gdcmHeader::FindLengthOB(void) {
- // See PS 3.5-2001, section A.4 p. 49 on encapsulation of encoded pixel data.
- guint16 g;
- guint16 n;
- long PositionOnEntry = ftell(fp);
- bool FoundSequenceDelimiter = false;
- guint32 TotalLength = 0;
- guint32 ItemLength;
-
- while ( ! FoundSequenceDelimiter) {
- g = ReadInt16();
- n = ReadInt16();
- if (errno == 1)
- return 0;
- TotalLength += 4; // We even have to decount the group and element
- if ( g != 0xfffe ) {
- dbg.Verbose(1, "gdcmHeader::FindLengthOB: ",
- "wrong group for an item sequence.");
- errno = 1;
- return 0;
- }
- if ( n == 0xe0dd )
- FoundSequenceDelimiter = true;
- else if ( n != 0xe000) {
- dbg.Verbose(1, "gdcmHeader::FindLengthOB: ",
- "wrong element for an item sequence.");
- errno = 1;
- return 0;
- }
- ItemLength = ReadInt32();
- TotalLength += ItemLength + 4; // We add 4 bytes since we just read
- // the ItemLength with ReadInt32
- SkipBytes(ItemLength);
- }
- fseek(fp, PositionOnEntry, SEEK_SET);
- return TotalLength;
-}
-
-void gdcmHeader::FindLength(gdcmElValue * ElVal) {
- guint16 element = ElVal->GetElement();
- string vr = ElVal->GetVR();
- guint16 length16;
-
- if ( (filetype == ExplicitVR) && ! ElVal->IsImplicitVr() ) {
-
- if ( (vr=="OB") || (vr=="OW") || (vr=="SQ") || (vr=="UN") ) {
- // The following reserved two bytes (see PS 3.5-2001, section
- // 7.1.2 Data element structure with explicit vr p27) must be
- // skipped before proceeding on reading the length on 4 bytes.
- fseek(fp, 2L, SEEK_CUR);
- guint32 length32 = ReadInt32();
- if ( (vr == "OB") && (length32 == 0xffffffff) ) {
- ElVal->SetLength(FindLengthOB());
- return;
- }
- FixFoundLength(ElVal, length32);
- return;
- }
-
- // Length is encoded on 2 bytes.
- length16 = ReadInt16();
-
- // We can tell the current file is encoded in big endian (like
- // Data/US-RGB-8-epicard) when we find the "Transfer Syntax" tag
- // and it's value is the one of the encoding of a big endian file.
- // In order to deal with such big endian encoded files, we have
- // (at least) two strategies:
- // * when we load the "Transfer Syntax" tag with value of big endian
- // encoding, we raise the proper flags. Then we wait for the end
- // of the META group (0x0002) among which is "Transfer Syntax",
- // before switching the swap code to big endian. We have to postpone
- // the switching of the swap code since the META group is fully encoded
- // in little endian, and big endian coding only starts at the next
- // group. The corresponding code can be hard to analyse and adds
- // many additional unnecessary tests for regular tags.
- // * the second strategy consists in waiting for trouble, that shall
- // appear when we find the first group with big endian encoding. This
- // is easy to detect since the length of a "Group Length" tag (the
- // ones with zero as element number) has to be of 4 (0x0004). When we
- // encouter 1024 (0x0400) chances are the encoding changed and we
- // found a group with big endian encoding.
- // We shall use this second strategy. In order to make sure that we
- // can interpret the presence of an apparently big endian encoded
- // length of a "Group Length" without committing a big mistake, we
- // add an additional check: we look in the allready parsed elements
- // for the presence of a "Transfer Syntax" whose value has to be "big
- // endian encoding". When this is the case, chances are we have got our
- // hands on a big endian encoded file: we switch the swap code to
- // big endian and proceed...
- if ( (element == 0x000) && (length16 == 0x0400) ) {
- if ( ! IsExplicitVRBigEndianTransferSyntax() ) {
- dbg.Verbose(0, "gdcmHeader::FindLength", "not explicit VR");
- errno = 1;
- return;
- }
- length16 = 4;
- SwitchSwapToBigEndian();
- // Restore the unproperly loaded values i.e. the group, the element
- // and the dictionary entry depending on them.
- guint16 CorrectGroup = SwapShort(ElVal->GetGroup());
- guint16 CorrectElem = SwapShort(ElVal->GetElement());
- gdcmDictEntry * NewTag = GetDictEntryByKey(CorrectGroup, CorrectElem);
- if (!NewTag) {
- // This correct tag is not in the dictionary. Create a new one.
- NewTag = new gdcmDictEntry(CorrectGroup, CorrectElem);
- }
- // FIXME this can create a memory leaks on the old entry that be
- // left unreferenced.
- ElVal->SetDictEntry(NewTag);
- }
-
- // Heuristic: well some files are really ill-formed.
- if ( length16 == 0xffff) {
- length16 = 0;
- dbg.Verbose(0, "gdcmHeader::FindLength",
- "Erroneous element length fixed.");
- }
- FixFoundLength(ElVal, (guint32)length16);
- return;
- }
-
- // Either implicit VR or a non DICOM conformal (see not below) explicit
- // VR that ommited the VR of (at least) this element. Farts happen.
- // [Note: according to the part 5, PS 3.5-2001, section 7.1 p25
- // on Data elements "Implicit and Explicit VR Data Elements shall
- // not coexist in a Data Set and Data Sets nested within it".]
- // Length is on 4 bytes.
- FixFoundLength(ElVal, ReadInt32());
-}
-
-/**
- * \ingroup gdcmHeader
- * \brief Swaps back the bytes of 4-byte long integer accordingly to
- * processor order.
- *
- * @return The suggested integer.
- */
-guint32 gdcmHeader::SwapLong(guint32 a) {
- // FIXME: il pourrait y avoir un pb pour les entiers negatifs ...
- switch (sw) {
- case 0 :
- break;
- case 4321 :
- a=( ((a<<24) & 0xff000000) | ((a<<8) & 0x00ff0000) |
- ((a>>8) & 0x0000ff00) | ((a>>24) & 0x000000ff) );
- break;
-
- case 3412 :
- a=( ((a<<16) & 0xffff0000) | ((a>>16) & 0x0000ffff) );
- break;
-
- case 2143 :
- a=( ((a<<8) & 0xff00ff00) | ((a>>8) & 0x00ff00ff) );
- break;
- default :
- dbg.Error(" gdcmHeader::SwapLong : unset swap code");
- a=0;
- }
- return(a);
-}
-
-/**
- * \ingroup gdcmHeader
- * \brief Swaps the bytes so they agree with the processor order
- * @return The properly swaped 16 bits integer.
- */
-guint16 gdcmHeader::SwapShort(guint16 a) {
- if ( (sw==4321) || (sw==2143) )
- a =(((a<<8) & 0x0ff00) | ((a>>8)&0x00ff));
- return (a);
-}
-
-void gdcmHeader::SkipBytes(guint32 NBytes) {
- //FIXME don't dump the returned value
- (void)fseek(fp, (long)NBytes, SEEK_CUR);
-}
-
-void gdcmHeader::SkipElementValue(gdcmElValue * ElVal) {
- SkipBytes(ElVal->GetLength());
-}
-
-void gdcmHeader::SetMaxSizeLoadElementValue(long NewSize) {
- if (NewSize < 0)
- return;
- if ((guint32)NewSize >= (guint32)0xffffffff) {
- MaxSizeLoadElementValue = 0xffffffff;
- return;
- }
- MaxSizeLoadElementValue = NewSize;
-}
-
-/**
- * \ingroup gdcmHeader
- * \brief Loads the element content if it's length is not bigger
- * than the value specified with
- * gdcmHeader::SetMaxSizeLoadElementValue()
- */
-void gdcmHeader::LoadElementValue(gdcmElValue * ElVal) {
- size_t item_read;
- guint16 group = ElVal->GetGroup();
- guint16 elem = ElVal->GetElement();
- string vr = ElVal->GetVR();
- guint32 length = ElVal->GetLength();
- bool SkipLoad = false;
-
- fseek(fp, (long)ElVal->GetOffset(), SEEK_SET);
-
- // Sequences not treated yet !
- //
- // Ne faudrait-il pas au contraire trouver immediatement
- // une maniere 'propre' de traiter les sequences (vr = SQ)
- // car commencer par les ignorer risque de conduire a qq chose
- // qui pourrait ne pas etre generalisable
- //
- if( vr == "SQ" )
- SkipLoad = true;
-
- // Heuristic : a sequence "contains" a set of tags (called items). It looks
- // like the last tag of a sequence (the one that terminates the sequence)
- // has a group of 0xfffe (with a dummy length).
- if( group == 0xfffe )
- SkipLoad = true;
-
- // The group length doesn't represent data to be loaded in memory, since
- // each element of the group shall be loaded individualy.
- if( elem == 0 )
- //SkipLoad = true; // modif sauvage JPR
- // On charge la longueur du groupe
- // quand l'element 0x0000 est présent !
-
- if ( SkipLoad ) {
- // FIXME the following skip is not necessary
- SkipElementValue(ElVal);
- ElVal->SetLength(0);
- ElVal->SetValue("gdcm::Skipped");
- return;
- }
-
- // When the length is zero things are easy:
- if ( length == 0 ) {
- ElVal->SetValue("");
- return;
- }
-
- // The elements whose length is bigger than the specified upper bound
- // are not loaded. Instead we leave a short notice of the offset of
- // the element content and it's length.
- if (length > MaxSizeLoadElementValue) {
- ostringstream s;
- s << "gdcm::NotLoaded.";
- s << " Address:" << (long)ElVal->GetOffset();
- s << " Length:" << ElVal->GetLength();
- ElVal->SetValue(s.str());
- return;
- }
-
- // When an integer is expected, read and convert the following two or
- // four bytes properly i.e. as an integer as opposed to a string.
-
- // pour les elements de Value Multiplicity > 1
- // on aura en fait une serie d'entiers
-
- // on devrait pouvoir faire + compact (?)
-
- if ( IsAnInteger(ElVal) ) {
- guint32 NewInt;
- ostringstream s;
- int nbInt;
- if (vr == "US" || vr == "SS") {
- nbInt = length / 2;
- NewInt = ReadInt16();
- s << NewInt;
- if (nbInt > 1) {
- for (int i=1; i < nbInt; i++) {
- s << '\\';
- NewInt = ReadInt16();
- s << NewInt;
- //printf("%s\n", s.str().c_str());
- }
- }
-
- } else if (vr == "UL" || vr == "SL") {
- nbInt = length / 4;
- NewInt = ReadInt32();
- s << NewInt;
- if (nbInt > 1) {
- for (int i=1; i < nbInt; i++) {
- s << '\\';
- NewInt = ReadInt32();
- s << NewInt;
- }
- }
- }
- ElVal->SetValue(s.str());
- return;
- }
-
- // FIXME The exact size should be length if we move to strings or whatever
- char* NewValue = (char*)malloc(length+1);
- if( !NewValue) {
- dbg.Verbose(1, "LoadElementValue: Failed to allocate NewValue");
- return;
- }
- NewValue[length]= 0;
-
- item_read = fread(NewValue, (size_t)length, (size_t)1, fp);
- if ( item_read != 1 ) {
- free(NewValue);
- dbg.Verbose(1, "gdcmHeader::LoadElementValue","unread element value");
- ElVal->SetValue("gdcm::UnRead");
- return;
- }
- ElVal->SetValue(NewValue);
-}
-
-/**
- * \ingroup gdcmHeader
- * \brief Loads the element while preserving the current
- * underlying file position indicator as opposed to
- * to LoadElementValue that modifies it.
- * @param ElVal Element whose value shall be loaded.
- * @return
- */
-void gdcmHeader::LoadElementValueSafe(gdcmElValue * ElVal) {
- long PositionOnEntry = ftell(fp);
- LoadElementValue(ElVal);
- fseek(fp, PositionOnEntry, SEEK_SET);
-}
-
-
-guint16 gdcmHeader::ReadInt16(void) {
- guint16 g;
- size_t item_read;
- item_read = fread (&g, (size_t)2,(size_t)1, fp);
- errno = 0;
- if ( item_read != 1 ) {
- dbg.Verbose(1, "gdcmHeader::ReadInt16", " File read error");
- errno = 1;
- return 0;
- }
- g = SwapShort(g);
- return g;
-}
-
-guint32 gdcmHeader::ReadInt32(void) {
- guint32 g;
- size_t item_read;
- item_read = fread (&g, (size_t)4,(size_t)1, fp);
- errno = 0;
- if ( item_read != 1 ) {
- dbg.Verbose(1, "gdcmHeader::ReadInt32", " File read error");
- errno = 1;
- return 0;
- }
- g = SwapLong(g);
- return g;
-}
-
-/**
- * \ingroup gdcmHeader
- * \brief Build a new Element Value from all the low level arguments.
- * Check for existence of dictionary entry, and build
- * a default one when absent.
- * @param Group group of the underlying DictEntry
- * @param Elem element of the underlying DictEntry