-
-bool gdcmFile::WriteDcmImplVR (const char* fileName) {
- return WriteDcmImplVR (std::string (fileName));
-}
-
-/////////////////////////////////////////////////////////////////
-/**
- * \ingroup gdcmFile
- * \brief
- * @param fileName
- * @return int acts as a boolean
- */
-
-bool gdcmFile::WriteDcmExplVR (std::string fileName) {
- return WriteBase(fileName, ExplicitVR);
-}
-
-/////////////////////////////////////////////////////////////////
-/**
- * \ingroup gdcmFile
- * \brief Ecrit au format ACR-NEMA sur disque l'entete et les pixels
- * (a l'attention des logiciels cliniques
- * qui ne prennent en entrée QUE des images ACR ...
- * \warning si un header DICOM est fourni en entree,
- * les groupes < 0x0008 et les groupes impairs sont ignores)
- * \warning Aucun test n'est fait sur l'"Endiannerie" du processeur.
- * Ca fonctionnera correctement (?) sur processeur Intel
- * (Equivalent a IdDcmWrite)
- *
- * @param fileName
- * @return int acts as a boolean
- */
-
-bool gdcmFile::WriteAcr (std::string fileName) {
- return WriteBase(fileName, ACR);
-}
-
-/////////////////////////////////////////////////////////////////
-/**
- * \ingroup gdcmFile
- *
- * @param FileName
- * @param type
- *
- * @return int acts as a boolean
- */
-bool gdcmFile::WriteBase (std::string FileName, FileType type) {
-
- FILE * fp1;
- fp1 = fopen(FileName.c_str(),"wb");
- if (fp1 == NULL) {
- printf("Echec ouverture (ecriture) Fichier [%s] \n",FileName.c_str());
- return (false);
- }
-
- if ( (type == ImplicitVR) || (type == ExplicitVR) ) {
- char * filePreamble;
- // writing Dicom File Preamble
- filePreamble=(char*)calloc(128,1);
- fwrite(filePreamble,128,1,fp1);
- fwrite("DICM",4,1,fp1);
- }
-
- // --------------------------------------------------------------
- // Special Patch to allow gdcm to re-write ACR-LibIDO formated images
- //
- // if recognition code tells us we dealt with a LibIDO image
- // we reproduce on disk the switch between lineNumber and columnNumber
- // just before writting ...
-
- std::string rows, columns;
- if ( Header->GetFileType() == ACR_LIBIDO){
- rows = Header->GetPubElValByNumber(0x0028, 0x0010);
- columns = Header->GetPubElValByNumber(0x0028, 0x0011);
- Header->SetPubElValByNumber(columns, 0x0028, 0x0010);
- Header->SetPubElValByNumber(rows , 0x0028, 0x0011);
- }
- // ----------------- End of Special Patch ----------------
-
- Header->Write(fp1, type);
-
- // --------------------------------------------------------------
- // Special Patch to allow gdcm to re-write ACR-LibIDO formated images
- //
- // ...and we restore the Header to be Dicom Compliant again
- // just after writting
-
- if (Header->GetFileType() == ACR_LIBIDO){
- Header->SetPubElValByNumber(rows , 0x0028, 0x0010);
- Header->SetPubElValByNumber(columns, 0x0028, 0x0011);
- }
- // ----------------- End of Special Patch ----------------
-
- fwrite(PixelData, lgrTotale, 1, fp1);
- fclose (fp1);
- return(true);
+void File::ComputeJPEGFragmentInfo()
+{
+ // If you need to, look for comments of ComputeRLEInfo().
+ std::string ts = GetTransferSyntax();
+ if ( ! Global::GetTS()->IsJPEG(ts) )
+ {
+ return;
+ }
+
+ ReadAndSkipEncapsulatedBasicOffsetTable();
+
+ // Loop on the fragments[s] and store the parsed information in a
+ // JPEGInfo.
+ long fragmentLength;
+ while ( (fragmentLength = ReadTagLength(0xfffe, 0xe000)) != 0 )
+ {
+ 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 e )
+ {
+ //std::cerr << e << std::endl;
+ return false;
+ }
+ if ( itemTagGroup != testGroup || itemTagElem != testElem )
+ {
+ gdcmWarningMacro( "Wrong Item Tag found:"
+ << " We should have found tag ("
+ << std::hex << testGroup << "," << testElem << ")" << std::endl
+ << " but instead we encountered tag ("
+ << std::hex << itemTagGroup << "," << itemTagElem << ")"
+ << " at address: " << " 0x(" << (unsigned int)currentPosition << ")"
+ ) ;
+ 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) )
+ {
+ return 0;
+ }
+
+ //// Then read the associated Item Length
+ long currentPosition = Fp->tellg();
+ uint32_t itemLength = ReadInt32();
+ {
+ gdcmWarningMacro( "Basic Item Length is: "
+ << itemLength << std::endl
+ << " 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::ReadAndSkipEncapsulatedBasicOffsetTable()
+{
+ //// 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 *basicOffsetTableItemValue = new char[itemLength + 1];
+ Fp->read(basicOffsetTableItemValue, itemLength);
+
+#ifdef GDCM_DEBUG
+ for (unsigned int i=0; i < itemLength; i += 4 )
+ {
+ uint32_t individualLength = str2num( &basicOffsetTableItemValue[i],
+ uint32_t);
+ gdcmWarningMacro( "Read one length: " <<
+ std::hex << individualLength );
+ }
+#endif //GDCM_DEBUG
+
+ delete[] basicOffsetTableItemValue;
+ }
+}
+
+// These are the deprecated method that one day should be removed (after the next release)
+
+#ifndef GDCM_LEGACY_REMOVE
+/**
+ * \brief Constructor (DEPRECATED : temporaryly kept not to break the API)
+ * @param filename name of the file whose header we want to analyze
+ * @deprecated do not use any longer
+ */
+File::File( std::string const &filename )
+ :Document( )
+{
+ RLEInfo = new RLEFramesInfo;
+ JPEGInfo = new JPEGFragmentsInfo;
+
+ SetFileName( filename );
+ Load( ); // gdcm::Document is first Loaded, then the 'File part'