+
+/**
+ * \brief Parse pixel data from disk of [multi-]fragment Jpeg encoding.
+ * Compute the jpeg extra information (fragment[s] offset[s] and
+ * length) and store it[them] in \ref JPEGInfo for later pixel
+ * retrieval usage.
+ */
+void File::ComputeJPEGFragmentInfo()
+{
+ // If you need to, look for comments of ComputeRLEInfo().
+ std::string ts = GetTransferSyntax();
+ if ( ! Global::GetTS()->IsJPEG(ts) )
+ {
+ return;
+ }
+
+ ReadEncapsulatedBasicOffsetTable();
+
+ // Loop on the fragments[s] and store the parsed information in a
+ // JPEGInfo.
+ long fragmentLength;
+ int i=0;
+ uint32_t sum = 0;
+ while ( (fragmentLength = ReadTagLength(0xfffe, 0xe000)) != 0 )
+ {
+ // Since we have read the basic offset table, let's check the value were correct
+ // or else produce a warning:
+ // A.4 Transfer syntaxes for encapsulation of encoded pixel data:
+ // When the Item Value is present, the Basic Offset Table Item Value shall contain
+ // concatenated 32-bit unsigned integer values that are byte offsets to the first
+ // byte of the Item Tag of the first fragment for each frame in the Sequence of
+ // Items. These offsets are measured from the first byte of the first Item Tag
+ // following the Basic Offset Table item (See Table A.4-2).
+
+ if ( BasicOffsetTableItemValue )
+ {
+ // If a BasicOffsetTableItemValue was read
+ uint32_t individualLength = BasicOffsetTableItemValue[i];
+ //assert( individualLength == sum ); // Seems like 00191113.dcm is off by one ??
+ if( individualLength != sum )
+ {
+ gdcmWarningMacro( "BasicOffsetTableItemValue differs from the fragment lenght:" <<
+ individualLength << " != " << sum );
+ }
+ sum += fragmentLength + 8;
+ i++;
+ }
+
+ long fragmentOffset = Fp->tellg();
+ // Store the collected info
+ JPEGFragment *newFragment = new JPEGFragment;
+ newFragment->SetOffset(fragmentOffset);
+ newFragment->SetLength(fragmentLength);
+ JPEGInfo->AddFragment(newFragment);
+
+ SkipBytes(fragmentLength);
+ }
+
+ // Make sure that we encounter a 'Sequence Delimiter Item'
+ // at the end of the item :
+ if ( !ReadTag(0xfffe, 0xe0dd) )
+ {
+ gdcmWarningMacro( "No sequence delimiter item at end of JPEG item sequence");
+ }
+}
+
+/**
+ * \brief Assuming the internal file pointer \ref Document::Fp
+ * is placed at the beginning of a tag, check whether this
+ * tag is (TestGroup, TestElem).
+ * \warning On success the internal file pointer \ref Document::Fp
+ * is modified to point after the tag.
+ * On failure (i.e. when the tag wasn't the expected tag
+ * (TestGroup, TestElem) the internal file pointer
+ * \ref Document::Fp is restored to it's original position.
+ * @param testGroup The expected group of the tag.
+ * @param testElem The expected Element of the tag.
+ * @return True on success, false otherwise.
+ */
+bool File::ReadTag(uint16_t testGroup, uint16_t testElem)
+{
+ long positionOnEntry = Fp->tellg();
+ long currentPosition = Fp->tellg(); // On debugging purposes
+
+ // Read the Item Tag group and element, and make
+ // sure they are what we expected:
+ uint16_t itemTagGroup;
+ uint16_t itemTagElem;
+ try
+ {
+ itemTagGroup = ReadInt16();
+ itemTagElem = ReadInt16();
+ }
+ catch ( FormatError )
+ {
+ gdcmErrorMacro( "Can not read tag for "
+ << " We should have found tag ("
+ << DictEntry::TranslateToKey(testGroup,testElem) << ")"
+ ) ;
+
+ return false;
+ }
+ if ( itemTagGroup != testGroup || itemTagElem != testElem )
+ {
+ // in order not to pollute output we don't warn on 'delimitors'
+ if (itemTagGroup != 0xfffe || testGroup != 0xfffe )
+ gdcmWarningMacro( "Wrong Item Tag found:"
+ << " We should have found tag ("
+ << DictEntry::TranslateToKey(testGroup,testElem) << ")" << std::endl
+ << " but instead we encountered tag ("
+ << DictEntry::TranslateToKey(itemTagGroup,itemTagElem) << ")"
+ << " at address: " << " 0x(" << std::hex
+ << (unsigned int)currentPosition << std::dec << ")"
+ ) ;
+ Fp->seekg(positionOnEntry, std::ios::beg);
+
+ return false;
+ }
+ return true;
+}
+
+/**
+ * \brief Assuming the internal file pointer \ref Document::Fp
+ * is placed at the beginning of a tag (TestGroup, TestElement),
+ * read the length associated to the Tag.
+ * \warning On success the internal file pointer \ref Document::Fp
+ * is modified to point after the tag and it's length.
+ * On failure (i.e. when the tag wasn't the expected tag
+ * (TestGroup, TestElement) the internal file pointer
+ * \ref Document::Fp is restored to it's original position.
+ * @param testGroup The expected Group of the tag.
+ * @param testElem The expected Element of the tag.
+ * @return On success returns the length associated to the tag. On failure
+ * returns 0.
+ */
+uint32_t File::ReadTagLength(uint16_t testGroup, uint16_t testElem)
+{
+
+ if ( !ReadTag(testGroup, testElem) )
+ {
+ // Avoid polutting output
+ if ( testGroup != 0xfffe )
+ gdcmErrorMacro( "ReadTag did not succeed for ("
+ << DictEntry::TranslateToKey(testGroup,testElem)
+ << ")..." );
+ return 0;
+ }
+
+ //// Then read the associated Item Length
+ long currentPosition = Fp->tellg();
+ uint32_t itemLength = ReadInt32();
+ gdcmDebugMacro( "Basic Item Length is: " << itemLength
+ << " at address: " << std::hex << (unsigned int)currentPosition);
+ return itemLength;
+}
+
+/**
+ * \brief When parsing the Pixel Data of an encapsulated file, read
+ * the basic offset table (when present, and BTW dump it).
+ */
+void File::ReadEncapsulatedBasicOffsetTable()
+{
+ //// Read the Basic Offset Table Item Tag length...
+ uint32_t itemLength = ReadTagLength(0xfffe, 0xe000);
+
+ // When present, read the basic offset table itself.
+ // Notes: - since the presence of this basic offset table is optional
+ // we can't rely on it for the implementation, and we will simply
+ // trash it's content (when present).
+ // - still, when present, we could add some further checks on the
+ // lengths, but we won't bother with such fuses for the time being.
+ if ( itemLength != 0 )
+ {
+ char *charBasicOffsetTableItemValue = new char[itemLength];
+ Fp->read(charBasicOffsetTableItemValue, itemLength);
+ unsigned int nbEntries = itemLength/4;
+ assert( nbEntries*4 == itemLength); // Make sure this is a multiple
+ BasicOffsetTableItemValue = new uint32_t[nbEntries];
+
+ for (unsigned int i=0; i < nbEntries; i++ )
+ {
+ BasicOffsetTableItemValue[i] = *((uint32_t*)(&charBasicOffsetTableItemValue[4*i]));
+#if defined(GDCM_WORDS_BIGENDIAN) || defined(GDCM_FORCE_BIGENDIAN_EMULATION)
+ uint32_t val = BasicOffsetTableItemValue[i];
+ BasicOffsetTableItemValue[i]
+ = ( (val<<24) | ((val<<8) & 0x00ff0000) |
+ ( (val>>8) & 0x0000ff00) | (val>>24) );
+#endif
+ gdcmDebugMacro( "Read one length for: " <<
+ std::hex << BasicOffsetTableItemValue[i] );
+ }
+
+ delete[] charBasicOffsetTableItemValue;
+ }
+}
+
+// These are the deprecated method that one day should be removed (after the next release)
+
+//#ifndef GDCM_LEGACY_REMOVE
+/*
+ * \ brief Loader. (DEPRECATED : temporaryly kept not to break the API)
+ * @ param fileName file to be open for parsing
+ * @ return false if file cannot be open or no swap info was found,
+ * or no tag was found.
+ * @ deprecated Use the Load() [ + SetLoadMode() ] + SetFileName() functions instead
+ */
+ /*
+bool File::Load( std::string const &fileName )
+{
+ GDCM_LEGACY_REPLACED_BODY(File::Load(std::string), "1.2",
+ File::Load());
+ SetFileName( fileName );
+ if ( ! this->Document::Load( ) )
+ return false;
+
+ return DoTheLoadingJob( );
+}
+*/
+//#endif
+
+//-----------------------------------------------------------------------------
+// Print
+