Skip to content

Commit

Permalink
Update ITK Image Writer to use new temporary file API
Browse files Browse the repository at this point in the history
  • Loading branch information
nyoungbq committed Apr 19, 2024
1 parent 3f44f31 commit 357826f
Showing 1 changed file with 36 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ bool Is2DFormat(const fs::path& fileName)
template <typename PixelT, uint32 Dimensions>
Result<> WriteAsOneFile(itk::Image<PixelT, Dimensions>& image, const fs::path& filePath /*, const IFilter::MessageHandler& messanger*/)
{
fs::path tempPath(fmt::format("{}/{}", fs::temp_directory_path().string(), filePath.filename().string()));
AtomicFile atomicFile(filePath);

Check failure on line 53 in src/Plugins/ITKImageProcessing/src/ITKImageProcessing/Filters/ITKImageWriter.cpp

View workflow job for this annotation

GitHub Actions / build (windows-2022, v142)

'nx::core::AtomicFile::AtomicFile(const std::string &)': cannot convert argument 1 from 'const std::filesystem::path' to 'const std::string &' [D:\a\simplnx\simplnx\build\Plugins\ITKImageProcessing\ITKImageProcessing.vcxproj]

Check failure on line 53 in src/Plugins/ITKImageProcessing/src/ITKImageProcessing/Filters/ITKImageWriter.cpp

View workflow job for this annotation

GitHub Actions / build (windows-2022, v143)

'nx::core::AtomicFile::AtomicFile': no overloaded function could convert all the argument types [D:\a\simplnx\simplnx\build\Plugins\ITKImageProcessing\ITKImageProcessing.vcxproj]

Check failure on line 53 in src/Plugins/ITKImageProcessing/src/ITKImageProcessing/Filters/ITKImageWriter.cpp

View workflow job for this annotation

GitHub Actions / build (windows-2022, v143)

error count exceeds 100; stopping compilation [D:\a\simplnx\simplnx\build\Plugins\ITKImageProcessing\ITKImageProcessing.vcxproj]
auto tempPath = atomicFile.tempFilePath();
try
{
using ImageType = itk::Image<PixelT, Dimensions>;
Expand All @@ -65,57 +66,57 @@ Result<> WriteAsOneFile(itk::Image<PixelT, Dimensions>& image, const fs::path& f
writer->Update();
} catch(const itk::ExceptionObject& err)
{
// Handle errors from the writer deleting the directory
fs::remove(tempPath);

return MakeErrorResult(-21011, fmt::format("ITK exception was thrown while writing output file: {}", err.GetDescription()));
}

fs::rename(tempPath, filePath);
if(!atomicFile.commit())
{
return atomicFile.getResult();
}
return {};
}

template <typename PixelT, uint32 Dimensions>
Result<> WriteAs2DStack(itk::Image<PixelT, Dimensions>& image, uint32 z_size, const fs::path& filePath, uint64 indexOffset)
{
// write to temp directory
auto namesGenerator = itk::NumericSeriesFileNames::New();
fs::path parentPath = fs::temp_directory_path();
fs::path fileName = filePath.stem();
fs::path extension = filePath.extension();
std::string format = fmt::format("{}/{}%03d{}", parentPath.string(), fileName.string(), extension.string());
namesGenerator->SetSeriesFormat(format);
namesGenerator->SetIncrementIndex(1);
namesGenerator->SetStartIndex(indexOffset);
namesGenerator->SetEndIndex(z_size - 1);

// generate all the files in that new directory
try
// Create list of AtomicFiles
std::vector<std::unique_ptr<AtomicFile>> atomicFiles = {};

for(uint64 index = indexOffset; index < (z_size - 1); index++)
{
using InputImageType = itk::Image<PixelT, Dimensions>;
using OutputImageType = itk::Image<PixelT, Dimensions - 1>;
using SeriesWriterType = itk::ImageSeriesWriter<InputImageType, OutputImageType>;
auto writer = SeriesWriterType::New();
writer->SetInput(&image);
writer->SetFileNames(namesGenerator->GetFileNames());
writer->UseCompressionOn();
writer->Update();
} catch(const itk::ExceptionObject& err)
atomicFiles.emplace_back(
std::make_unique<AtomicFile>((fs::absolute(fmt::format("{}/{}{:03d}{}", filePath.parent_path().string(), filePath.stem().string(), index, filePath.extension().string())))));
}
{
// Handle errors from the writer deleting the directory
for(const auto& name : namesGenerator->GetFileNames())
std::vector<std::string> fileNames = {};
for(const auto& atomicFile : atomicFiles)
{
fs::remove(name);
fileNames.emplace_back(atomicFile->tempFilePath().string());
}

return MakeErrorResult(-21011, fmt::format("ITK exception was thrown while writing output file: {}", err.GetDescription()));
// generate all the files in that new directory
try
{
using InputImageType = itk::Image<PixelT, Dimensions>;
using OutputImageType = itk::Image<PixelT, Dimensions - 1>;
using SeriesWriterType = itk::ImageSeriesWriter<InputImageType, OutputImageType>;
auto writer = SeriesWriterType::New();
writer->SetInput(&image);
writer->SetFileNames(fileNames);
writer->UseCompressionOn();
writer->Update();
} catch(const itk::ExceptionObject& err)
{
return MakeErrorResult(-21011, fmt::format("ITK exception was thrown while writing output file: {}", err.GetDescription()));
}
}

// Move all the files from the new directory to the users actual directory
for(const auto& name : namesGenerator->GetFileNames())
for(const auto& atomicFile : atomicFiles)
{
fs::path tempFile(name);
fs::rename(tempFile, {fmt::format("{}/{}", filePath.parent_path().string(), tempFile.filename().string())});
if(!atomicFile->commit())
{
return atomicFile->getResult();
}
}

return {};
Expand Down

0 comments on commit 357826f

Please sign in to comment.