+
+/**
+ * \ingroup gdcmHeader
+ * \brief
+ *
+ * @return
+ */
+ guint32 gdcmHeader::FindHeaderEntryLengthOB(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 && g!=0xb00c ) /*for bogus header */ {
+ char msg[100]; // for sprintf. Sorry
+ sprintf(msg,"wrong group (%04x) for an item sequence (%04x,%04x)\n",g, g,n);
+ dbg.Verbose(1, "gdcmHeader::FindLengthOB: ",msg);
+ errno = 1;
+ return 0;
+ }
+ if ( n == 0xe0dd || ( g==0xb00c && n==0x0eb6 ) ) /* for bogus header */
+ FoundSequenceDelimiter = true;
+ else if ( n != 0xe000 ){
+ char msg[100]; // for sprintf. Sorry
+ sprintf(msg,"wrong element (%04x) for an item sequence (%04x,%04x)\n",
+ n, g,n);
+ dbg.Verbose(1, "gdcmHeader::FindLengthOB: ",msg);
+ 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;
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Reads a supposed to be 16 Bits integer
+ * \ (swaps it depending on processor endianity)
+ *
+ * @return integer acts as a boolean
+ */
+guint16 gdcmHeader::ReadInt16(void) {
+ guint16 g;
+ size_t item_read;
+ item_read = fread (&g, (size_t)2,(size_t)1, fp);
+ if ( item_read != 1 ) {
+ // dbg.Verbose(0, "gdcmHeader::ReadInt16", " Failed to read :");
+ // if(feof(fp))
+ // dbg.Verbose(0, "gdcmHeader::ReadInt16", " End of File encountered");
+ if(ferror(fp))
+ dbg.Verbose(0, "gdcmHeader::ReadInt16", " File Error");
+ errno = 1;
+ return 0;
+ }
+ errno = 0;
+ g = SwapShort(g);
+ return g;
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Reads a supposed to be 32 Bits integer
+ * \ (swaps it depending on processor endianity)
+ *
+ * @return
+ */
+guint32 gdcmHeader::ReadInt32(void) {
+ guint32 g;
+ size_t item_read;
+ item_read = fread (&g, (size_t)4,(size_t)1, fp);
+ if ( item_read != 1 ) {
+ //dbg.Verbose(0, "gdcmHeader::ReadInt32", " Failed to read :");
+ //if(feof(fp))
+ // dbg.Verbose(0, "gdcmHeader::ReadInt32", " End of File encountered");
+ if(ferror(fp))
+ dbg.Verbose(0, "gdcmHeader::ReadInt32", " File Error");
+ errno = 1;
+ return 0;
+ }
+ errno = 0;
+ g = SwapLong(g);
+ return g;
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief
+ *
+ * @return
+ */
+void gdcmHeader::SkipBytes(guint32 NBytes) {
+ //FIXME don't dump the returned value
+ (void)fseek(fp, (long)NBytes, SEEK_CUR);
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief
+ */
+void gdcmHeader::Initialise(void) {
+ dicom_vr = gdcmGlobal::GetVR();
+ dicom_ts = gdcmGlobal::GetTS();
+ Dicts = gdcmGlobal::GetDicts();
+ RefPubDict = Dicts->GetDefaultPubDict();
+ RefShaDict = (gdcmDict*)0;
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Discover what the swap code is (among little endian, big endian,
+ * bad little endian, bad big endian).
+ *
+ */
+void gdcmHeader::CheckSwap()
+{
+ // Fourth semantics:
+ //
+ // ---> Warning : This fourth field is NOT part
+ // of the 'official' Dicom Dictionnary
+ // and should NOT be used.
+ // (Not defined for all the groups
+ // may be removed in a future release)
+ //
+ // CMD Command
+ // META Meta Information
+ // DIR Directory
+ // ID
+ // PAT Patient
+ // ACQ Acquisition
+ // REL Related
+ // IMG Image
+ // SDY Study
+ // VIS Visit
+ // WAV Waveform
+ // PRC
+ // DEV Device
+ // NMI Nuclear Medicine
+ // MED
+ // BFS Basic Film Session
+ // BFB Basic Film Box
+ // BIB Basic Image Box
+ // BAB
+ // IOB
+ // PJ
+ // PRINTER
+ // RT Radio Therapy
+ // DVH
+ // SSET
+ // RES Results
+ // CRV Curve
+ // OLY Overlays
+ // PXL Pixels
+ // DL Delimiters
+ //
+
+ // The only guaranted way of finding the swap code is to find a
+ // group tag since we know it's length has to be of four bytes i.e.
+ // 0x00000004. Finding the swap code in then straigthforward. Trouble
+ // occurs when we can't find such group...
+ guint32 s;
+ guint32 x=4; // x : for ntohs
+ bool net2host; // true when HostByteOrder is the same as NetworkByteOrder
+
+ int lgrLue;
+ char * entCur;
+ char deb[HEADER_LENGTH_TO_READ];
+
+ // First, compare HostByteOrder and NetworkByteOrder in order to
+ // determine if we shall need to swap bytes (i.e. the Endian type).
+ if (x==ntohs(x))
+ net2host = true;
+ else
+ net2host = false;
+ //cout << net2host << endl;
+
+ // The easiest case is the one of a DICOM header, since it possesses a
+ // file preamble where it suffice to look for the string "DICM".
+ lgrLue = fread(deb, 1, HEADER_LENGTH_TO_READ, fp);
+
+ entCur = deb + 128;
+ if(memcmp(entCur, "DICM", (size_t)4) == 0) {
+ dbg.Verbose(1, "gdcmHeader::CheckSwap:", "looks like DICOM Version3");
+ // Next, determine the value representation (VR). Let's skip to the
+ // first element (0002, 0000) and check there if we find "UL"
+ // - or "OB" if the 1st one is (0002,0001) -,
+ // in which case we (almost) know it is explicit VR.
+ // WARNING: if it happens to be implicit VR then what we will read
+ // is the length of the group. If this ascii representation of this
+ // length happens to be "UL" then we shall believe it is explicit VR.
+ // FIXME: in order to fix the above warning, we could read the next
+ // element value (or a couple of elements values) in order to make
+ // sure we are not commiting a big mistake.
+ // We need to skip :
+ // * the 128 bytes of File Preamble (often padded with zeroes),
+ // * the 4 bytes of "DICM" string,
+ // * the 4 bytes of the first tag (0002, 0000),or (0002, 0001)
+ // i.e. a total of 136 bytes.
+ entCur = deb + 136;
+ // FIXME
+ // Use gdcmHeader::dicom_vr to test all the possibilities
+ // instead of just checking for UL, OB and UI !?
+ if( (memcmp(entCur, "UL", (size_t)2) == 0) ||
+ (memcmp(entCur, "OB", (size_t)2) == 0) ||
+ (memcmp(entCur, "UI", (size_t)2) == 0) )
+ {
+ filetype = ExplicitVR;
+ dbg.Verbose(1, "gdcmHeader::CheckSwap:",
+ "explicit Value Representation");
+ } else {
+ filetype = ImplicitVR;
+ dbg.Verbose(1, "gdcmHeader::CheckSwap:",
+ "not an explicit Value Representation");
+ }
+ if (net2host) {
+ sw = 4321;
+ dbg.Verbose(1, "gdcmHeader::CheckSwap:",
+ "HostByteOrder != NetworkByteOrder");
+ } else {
+ sw = 0;
+ dbg.Verbose(1, "gdcmHeader::CheckSwap:",
+ "HostByteOrder = NetworkByteOrder");
+ }
+
+ // Position the file position indicator at first tag (i.e.
+ // after the file preamble and the "DICM" string).
+ rewind(fp);
+ fseek (fp, 132L, SEEK_SET);
+ return;
+ } // End of DicomV3
+
+ // Alas, this is not a DicomV3 file and whatever happens there is no file
+ // preamble. We can reset the file position indicator to where the data
+ // is (i.e. the beginning of the file).
+ dbg.Verbose(1, "gdcmHeader::CheckSwap:", "not a DICOM Version3 file");
+ rewind(fp);
+
+ // Our next best chance would be to be considering a 'clean' ACR/NEMA file.
+ // By clean we mean that the length of the first tag is written down.
+ // If this is the case and since the length of the first group HAS to be
+ // four (bytes), then determining the proper swap code is straightforward.
+
+ entCur = deb + 4;
+ // We assume the array of char we are considering contains the binary
+ // representation of a 32 bits integer. Hence the following dirty
+ // trick :
+ s = *((guint32 *)(entCur));
+
+ switch (s) {
+ case 0x00040000 :
+ sw = 3412;
+ filetype = ACR;
+ return;
+ case 0x04000000 :
+ sw = 4321;
+ filetype = ACR;
+ return;
+ case 0x00000400 :
+ sw = 2143;
+ filetype = ACR;
+ return;
+ case 0x00000004 :
+ sw = 0;
+ filetype = ACR;
+ return;
+ default :
+ dbg.Verbose(0, "gdcmHeader::CheckSwap:",
+ "ACR/NEMA unfound swap info (time to raise bets)");
+ }
+
+ // We are out of luck. It is not a DicomV3 nor a 'clean' ACR/NEMA file.
+ // It is time for despaired wild guesses. So, let's assume this file
+ // happens to be 'dirty' ACR/NEMA, i.e. the length of the group is
+ // not present. Then the only info we have is the net2host one.
+ filetype = Unknown;
+ if (! net2host )
+ sw = 0;
+ else
+ sw = 4321;
+ return;
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief
+ */
+void gdcmHeader::SwitchSwapToBigEndian(void) {
+ dbg.Verbose(1, "gdcmHeader::SwitchSwapToBigEndian",
+ "Switching to BigEndian mode.");
+ if ( sw == 0 ) {
+ sw = 4321;
+ return;
+ }
+ if ( sw == 4321 ) {
+ sw = 0;
+ return;
+ }
+ if ( sw == 3412 ) {
+ sw = 2143;
+ return;
+ }
+ if ( sw == 2143 )
+ sw = 3412;
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief
+ * @param NewSize
+ * @return
+ */
+void gdcmHeader::SetMaxSizeLoadElementValue(long NewSize) {
+ if (NewSize < 0)
+ return;
+ if ((guint32)NewSize >= (guint32)0xffffffff) {
+ MaxSizeLoadElementValue = 0xffffffff;
+ return;
+ }
+ MaxSizeLoadElementValue = NewSize;
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Searches both the public and the shadow dictionary (when they
+ * exist) for the presence of the DictEntry with given
+ * group and element. The public dictionary has precedence on the
+ * shadow one.
+ * @param group group of the searched DictEntry
+ * @param element element of the searched DictEntry
+ * @return Corresponding DictEntry when it exists, NULL otherwise.
+ */
+gdcmDictEntry * gdcmHeader::GetDictEntryByNumber(guint16 group,
+ guint16 element) {
+ gdcmDictEntry * found = (gdcmDictEntry*)0;
+ if (!RefPubDict && !RefShaDict) {
+ dbg.Verbose(0, "gdcmHeader::GetDictEntry",
+ "we SHOULD have a default dictionary");
+ }
+ if (RefPubDict) {
+ found = RefPubDict->GetTagByNumber(group, element);
+ if (found)
+ return found;
+ }
+ if (RefShaDict) {
+ found = RefShaDict->GetTagByNumber(group, element);
+ if (found)
+ return found;
+ }
+ return found;
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Searches both the public and the shadow dictionary (when they
+ * exist) for the presence of the DictEntry with given name.
+ * The public dictionary has precedence on the shadow one.
+ * @param Name name of the searched DictEntry
+ * @return Corresponding DictEntry when it exists, NULL otherwise.
+ */
+gdcmDictEntry * gdcmHeader::GetDictEntryByName(std::string Name) {
+ gdcmDictEntry * found = (gdcmDictEntry*)0;
+ if (!RefPubDict && !RefShaDict) {
+ dbg.Verbose(0, "gdcmHeader::GetDictEntry",
+ "we SHOULD have a default dictionary");
+ }
+ if (RefPubDict) {
+ found = RefPubDict->GetTagByName(Name);
+ if (found)
+ return found;
+ }
+ if (RefShaDict) {
+ found = RefShaDict->GetTagByName(Name);
+ if (found)
+ return found;
+ }
+ return found;
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Read the next tag but WITHOUT loading it's value
+ * @return On succes the newly created HeaderEntry, NULL on failure.
+ */
+gdcmHeaderEntry * gdcmHeader::ReadNextHeaderEntry(void) {
+
+ guint16 g,n;
+ gdcmHeaderEntry * NewElVal;
+
+ g = ReadInt16();
+ n = ReadInt16();
+
+ if (errno == 1)
+ // We reached the EOF (or an error occured) and header parsing
+ // has to be considered as finished.
+ return (gdcmHeaderEntry *)0;
+
+ NewElVal = NewHeaderEntryByNumber(g, n);
+ FindHeaderEntryVR(NewElVal);
+ FindHeaderEntryLength(NewElVal);
+
+ if (errno == 1) {
+ // Call it quits
+ return (gdcmHeaderEntry *)0;
+ }
+ NewElVal->SetOffset(ftell(fp));
+ //if ( (g==0x7fe0) && (n==0x0010) )
+ return NewElVal;
+}
+
+/**
+ * \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 Name Name of the underlying DictEntry
+ */
+gdcmHeaderEntry* gdcmHeader::NewHeaderEntryByName(std::string Name) {
+
+ gdcmDictEntry * NewTag = GetDictEntryByName(Name);
+ if (!NewTag)
+ NewTag = new gdcmDictEntry(0xffff, 0xffff, "LO", "Unknown", Name);
+
+ gdcmHeaderEntry* NewElVal = new gdcmHeaderEntry(NewTag);
+ if (!NewElVal) {
+ dbg.Verbose(1, "gdcmHeader::ObtainHeaderEntryByName",
+ "failed to allocate gdcmHeaderEntry");
+ return (gdcmHeaderEntry*)0;
+ }
+ return NewElVal;
+}
+
+/**
+ * \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
+ */
+gdcmHeaderEntry* gdcmHeader::NewHeaderEntryByNumber(guint16 Group, guint16 Elem) {
+ // Find out if the tag we encountered is in the dictionaries:
+ gdcmDictEntry * NewTag = GetDictEntryByNumber(Group, Elem);
+ if (!NewTag)
+ NewTag = new gdcmDictEntry(Group, Elem);
+
+ gdcmHeaderEntry* NewElVal = new gdcmHeaderEntry(NewTag);
+ if (!NewElVal) {
+ dbg.Verbose(1, "gdcmHeader::NewHeaderEntryByNumber",
+ "failed to allocate gdcmHeaderEntry");
+ return (gdcmHeaderEntry*)0;
+ }
+ return NewElVal;
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Small utility function that creates a new manually crafted
+ * (as opposed as read from the file) gdcmHeaderEntry with user
+ * specified name and adds it to the public tag hash table.
+ * \note A fake TagKey is generated so the PubDict can keep it's coherence.
+ * @param NewTagName The name to be given to this new tag.
+ * @param VR The Value Representation to be given to this new tag.
+ * @ return The newly hand crafted Element Value.
+ */
+gdcmHeaderEntry* gdcmHeader::NewManualHeaderEntryToPubDict(std::string NewTagName,
+ std::string VR) {
+ gdcmHeaderEntry* NewElVal = (gdcmHeaderEntry*)0;
+ guint32 StuffGroup = 0xffff; // Group to be stuffed with additional info
+ guint32 FreeElem = 0;
+ gdcmDictEntry* NewEntry = (gdcmDictEntry*)0;
+
+ FreeElem = PubEntrySet.GenerateFreeTagKeyInGroup(StuffGroup);
+ if (FreeElem == UINT32_MAX) {
+ dbg.Verbose(1, "gdcmHeader::NewManualElValToPubDict",
+ "Group 0xffff in Public Dict is full");
+ return (gdcmHeaderEntry*)0;
+ }
+ NewEntry = new gdcmDictEntry(StuffGroup, FreeElem,
+ VR, "GDCM", NewTagName);
+ NewElVal = new gdcmHeaderEntry(NewEntry);
+ PubEntrySet.Add(NewElVal);
+ return NewElVal;
+}
+
+//-----------------------------------------------------------------------------