From 8b184b092cebd6a94822ce85b238909e4c69f88f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C5=BEenan=20Zuki=C4=87?= Date: Wed, 23 Dec 2020 12:31:04 -0500 Subject: [PATCH] ENH: writing data file can be skipped in some circumstances --- src/PlusCommon/Tools/EditSequenceFile.cxx | 17 ++++++++++++++++- src/PlusCommon/vtkPlusSequenceIO.cxx | 8 ++++---- src/PlusCommon/vtkPlusSequenceIO.h | 4 ++-- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/PlusCommon/Tools/EditSequenceFile.cxx b/src/PlusCommon/Tools/EditSequenceFile.cxx index a6eec15d1..cb3326327 100644 --- a/src/PlusCommon/Tools/EditSequenceFile.cxx +++ b/src/PlusCommon/Tools/EditSequenceFile.cxx @@ -373,39 +373,53 @@ int main(int argc, char** argv) return EXIT_FAILURE; } + std::string fileExtension = vtksys::SystemTools::GetFilenameLastExtension(inputFileName); + bool headerOnlyUpdatePossible = (inputFileName == outputFileName) // file overwrite requested + && (igsioCommon::IsEqualInsensitive(fileExtension, ".mhd") || igsioCommon::IsEqualInsensitive(fileExtension, ".nhdr")) + && !useCompression; // hard to carry over compressed data size + bool headerOnlyOperation = false; // false by default + // Set operation if (strOperation.empty()) { operation = NO_OPERATION; LOG_INFO("No modification operation has been specified (specify --operation parameter to change the input sequence)."); + headerOnlyOperation = true; } else if (igsioCommon::IsEqualInsensitive(strOperation, "UPDATE_FRAME_FIELD_NAME")) { operation = UPDATE_FRAME_FIELD_NAME; + headerOnlyOperation = true; } else if (igsioCommon::IsEqualInsensitive(strOperation, "UPDATE_FRAME_FIELD_VALUE")) { operation = UPDATE_FRAME_FIELD_VALUE; + headerOnlyOperation = true; } else if (igsioCommon::IsEqualInsensitive(strOperation, "DELETE_FRAME_FIELD")) { operation = DELETE_FRAME_FIELD; + headerOnlyOperation = true; } else if (igsioCommon::IsEqualInsensitive(strOperation, "UPDATE_FIELD_NAME")) { operation = UPDATE_FIELD_NAME; + headerOnlyOperation = true; } else if (igsioCommon::IsEqualInsensitive(strOperation, "UPDATE_FIELD_VALUE")) { operation = UPDATE_FIELD_VALUE; + headerOnlyOperation = true; } else if (igsioCommon::IsEqualInsensitive(strOperation, "DELETE_FIELD")) { operation = DELETE_FIELD; + headerOnlyOperation = true; } else if (igsioCommon::IsEqualInsensitive(strOperation, "ADD_TRANSFORM")) { operation = ADD_TRANSFORM; + headerOnlyOperation = true; } else if (igsioCommon::IsEqualInsensitive(strOperation, "TRIM")) { @@ -780,7 +794,8 @@ int main(int argc, char** argv) // Save output file to file LOG_INFO("Save output sequence file to: " << outputFileName); - if (vtkPlusSequenceIO::Write(outputFileName, trackedFrameList, trackedFrameList->GetImageOrientation(), useCompression, operation != REMOVE_IMAGE_DATA) != PLUS_SUCCESS) + if (vtkPlusSequenceIO::Write(outputFileName, trackedFrameList, trackedFrameList->GetImageOrientation(), + useCompression, operation == REMOVE_IMAGE_DATA, headerOnlyUpdatePossible && headerOnlyOperation) != PLUS_SUCCESS) { LOG_ERROR("Couldn't write sequence file: " << outputFileName); return EXIT_FAILURE; diff --git a/src/PlusCommon/vtkPlusSequenceIO.cxx b/src/PlusCommon/vtkPlusSequenceIO.cxx index 698e5faaf..64978214b 100644 --- a/src/PlusCommon/vtkPlusSequenceIO.cxx +++ b/src/PlusCommon/vtkPlusSequenceIO.cxx @@ -13,25 +13,25 @@ #include //---------------------------------------------------------------------------- -igsioStatus vtkPlusSequenceIO::Write(const std::string& filename, vtkIGSIOTrackedFrameList* frameList, US_IMAGE_ORIENTATION orientationInFile/*=US_IMG_ORIENT_MF*/, bool useCompression/*=true*/, bool enableImageDataWrite/*=true*/) +igsioStatus vtkPlusSequenceIO::Write(const std::string& filename, vtkIGSIOTrackedFrameList* frameList, US_IMAGE_ORIENTATION orientationInFile/*=US_IMG_ORIENT_MF*/, bool useCompression/*=true*/, bool reduceImageDataToOnePixel/*=false*/, bool writeHeaderOnly/*=false*/) { std::string outputDirectory = ""; if (!vtksys::SystemTools::FileIsFullPath(filename)) { outputDirectory = vtkPlusConfig::GetInstance()->GetOutputDirectory(); } - return vtkIGSIOSequenceIO::Write(filename, outputDirectory, frameList, orientationInFile, useCompression, enableImageDataWrite); + return vtkIGSIOSequenceIO::Write(filename, outputDirectory, frameList, orientationInFile, useCompression, reduceImageDataToOnePixel, writeHeaderOnly); } //---------------------------------------------------------------------------- -igsioStatus vtkPlusSequenceIO::Write(const std::string& filename, igsioTrackedFrame* frame, US_IMAGE_ORIENTATION orientationInFile /*= US_IMG_ORIENT_MF*/, bool useCompression /*= true*/, bool enableImageDataWrite /*=true*/) +igsioStatus vtkPlusSequenceIO::Write(const std::string& filename, igsioTrackedFrame* frame, US_IMAGE_ORIENTATION orientationInFile /*= US_IMG_ORIENT_MF*/, bool useCompression /*= true*/, bool reduceImageDataToOnePixel/*=false*/, bool writeHeaderOnly/*=false*/) { std::string outputDirectory = ""; if (!vtksys::SystemTools::FileIsFullPath(filename)) { outputDirectory = vtkPlusConfig::GetInstance()->GetOutputDirectory(); } - return vtkIGSIOSequenceIO::Write(filename, outputDirectory, frame, orientationInFile, useCompression, enableImageDataWrite); + return vtkIGSIOSequenceIO::Write(filename, outputDirectory, frame, orientationInFile, useCompression, reduceImageDataToOnePixel, writeHeaderOnly); } //---------------------------------------------------------------------------- diff --git a/src/PlusCommon/vtkPlusSequenceIO.h b/src/PlusCommon/vtkPlusSequenceIO.h index 4326b8974..ab5d66078 100644 --- a/src/PlusCommon/vtkPlusSequenceIO.h +++ b/src/PlusCommon/vtkPlusSequenceIO.h @@ -18,10 +18,10 @@ class vtkPlusCommonExport vtkPlusSequenceIO : public vtkObject { public: /*! Write object contents into file */ - static igsioStatus Write(const std::string& filename, igsioTrackedFrame* frame, US_IMAGE_ORIENTATION orientationInFile = US_IMG_ORIENT_MF, bool useCompression = true, bool EnableImageDataWrite = true); + static igsioStatus Write(const std::string& filename, igsioTrackedFrame* frame, US_IMAGE_ORIENTATION orientationInFile = US_IMG_ORIENT_MF, bool useCompression = true, bool reduceImageDataToOnePixel = false, bool writeHeaderOnly = false); /*! Write object contents into file */ - static igsioStatus Write(const std::string& filename, vtkIGSIOTrackedFrameList* frameList, US_IMAGE_ORIENTATION orientationInFile = US_IMG_ORIENT_MF, bool useCompression = true, bool EnableImageDataWrite = true); + static igsioStatus Write(const std::string& filename, vtkIGSIOTrackedFrameList* frameList, US_IMAGE_ORIENTATION orientationInFile = US_IMG_ORIENT_MF, bool useCompression = true, bool reduceImageDataToOnePixel = false, bool writeHeaderOnly = false); /*! Read file contents into the object */ static igsioStatus Read(const std::string& filename, vtkIGSIOTrackedFrameList* frameList);