- 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;
-
- // 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) {
- filetype = TrueDicom;
- dbg.Verbose(1, "gdcmHeader::CheckSwap:", "looks like DICOM Version3");
- } else {
- filetype = Unknown;
- dbg.Verbose(1, "gdcmHeader::CheckSwap:", "not a DICOM Version3 file");
- }
-
- if(filetype == TrueDicom) {
- // Next, determine the value representation (VR). Let's skip to the
- // first element (0002, 0000) and check there if we find "UL", 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),
- // i.e. a total of 136 bytes.
- entCur = deb + 136;
- if(memcmp(entCur, "UL", (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 TrueDicom
-
- // 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).
- 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;
- s = str2num(entCur, guint32);
-
- 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.
- if (! net2host )
- sw = 0;
- else
- sw = 4321;
- return;
-}
-
-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;
-}
-
-void gdcmHeader::GetPixels(size_t lgrTotale, void* _Pixels) {
- size_t pixelsOffset;
- pixelsOffset = GetPixelOffset();
- fseek(fp, pixelsOffset, SEEK_SET);
- fread(_Pixels, 1, lgrTotale, fp);
-}
-
-
-
-/**
- * \ingroup gdcmHeader
- * \brief Find the value representation of the current tag.
- */
-void gdcmHeader::FindVR( ElValue *ElVal) {
- if (filetype != ExplicitVR)
- return;
-
- char VR[3];
- string vr;
- int lgrLue;
- long PositionOnEntry = ftell(fp);
- // Warning: we believe this is explicit VR (Value Representation) because
- // we used a heuristic that found "UL" in the first tag. Alas this
- // doesn't guarantee that all the tags will be in explicit VR. In some
- // cases (see e-film filtered files) one finds implicit VR tags mixed
- // within an explicit VR file. Hence we make sure the present tag
- // is in explicit VR and try to fix things if it happens not to be
- // the case.
- bool RealExplicit = true;
-
- lgrLue=fread (&VR, (size_t)2,(size_t)1, fp);
- VR[2]=0;
- vr = string(VR);
-
- // Assume we are reading a falsely explicit VR file i.e. we reached
- // a tag where we expect reading a VR but are in fact we read the
- // first to bytes of the length. Then we will interogate (through find)
- // the dicom_vr dictionary with oddities like "\004\0" which crashes
- // both GCC and VC++ implementations of the STL map. Hence when the
- // expected VR read happens to be non-ascii characters we consider
- // we hit falsely explicit VR tag.
-
- if ( (!isalpha(VR[0])) && (!isalpha(VR[1])) )
- RealExplicit = false;
-
- // CLEANME searching the dicom_vr at each occurence is expensive.
- // PostPone this test in an optional integrity check at the end
- // of parsing or only in debug mode.
- if ( RealExplicit && !dicom_vr->count(vr) )
- RealExplicit = false;
-
- if ( RealExplicit ) {
- if ( ElVal->IsVrUnknown() ) {
- // When not a dictionary entry, we can safely overwrite the vr.
- ElVal->SetVR(vr);
- return;
- }
- if ( ElVal->GetVR() == vr ) {
- // The vr we just read and the dictionary agree. Nothing to do.
- return;
- }
- // The vr present in the file and the dictionary disagree. We assume
- // the file writer knew best and use the vr of the file. Since it would
- // be unwise to overwrite the vr of a dictionary (since it would
- // compromise it's next user), we need to clone the actual DictEntry
- // and change the vr for the read one.
- gdcmDictEntry* NewTag = new gdcmDictEntry(ElVal->GetGroup(),
- ElVal->GetElement(),
- vr,
- "FIXME",
- ElVal->GetName());
- ElVal->SetDictEntry(NewTag);
- return;
- }
-
- // We thought this was explicit VR, but we end up with an
- // implicit VR tag. Let's backtrack.
- dbg.Verbose(1, "gdcmHeader::FindVR:", "Falsely explicit vr file");
- fseek(fp, PositionOnEntry, SEEK_SET);
- // When this element is known in the dictionary we shall use, e.g. for
- // the semantics (see the usage of IsAnInteger), the vr proposed by the
- // dictionary entry. Still we have to flag the element as implicit since
- // we know now our assumption on expliciteness is not furfilled.
- // avoid .
- if ( ElVal->IsVrUnknown() )
- ElVal->SetVR("Implicit");
- ElVal->SetImplicitVr();