From 2fc09af39b364b82ddf930be3fee40ca192218ff Mon Sep 17 00:00:00 2001 From: Simon Rit Date: Tue, 23 Aug 2011 13:51:46 +0200 Subject: [PATCH] Added compression option to clitkImageConvert --- common/clitkImageCommon.h | 4 ++-- common/clitkImageCommon.txx | 20 ++++--------------- common/clitkImageToImageGenericFilterBase.cxx | 11 +++++++++- common/clitkImageToImageGenericFilterBase.h | 3 +++ tools/clitkImageConvert.cxx | 1 + tools/clitkImageConvert.ggo | 1 + 6 files changed, 21 insertions(+), 19 deletions(-) diff --git a/common/clitkImageCommon.h b/common/clitkImageCommon.h index fbddb20..32bba49 100644 --- a/common/clitkImageCommon.h +++ b/common/clitkImageCommon.h @@ -58,11 +58,11 @@ typename ImageType::Pointer readImage(const std::string & filename, const bool v template typename ImageType::Pointer readImage(const std::vector & filenames, const bool verbose=false); template -void writeImage(const typename ImageType::Pointer image, const std::string & filename, const bool verbose=false); +void writeImage(const typename ImageType::Pointer image, const std::string & filename, const bool verbose=false, const bool compression=false); // template // void writeConstImage(const typename ImageType::ConstPointer image, const std::string & filename, const bool verbose=false); template -void writeImage(const ImageType* image, const std::string & filename, const bool verbose=false); +void writeImage(const ImageType* image, const std::string & filename, const bool verbose=false, const bool compression=false); //-------------------------------------------------------------------- // Read/print image header diff --git a/common/clitkImageCommon.txx b/common/clitkImageCommon.txx index 5aa3b2e..a07c66f 100644 --- a/common/clitkImageCommon.txx +++ b/common/clitkImageCommon.txx @@ -188,34 +188,22 @@ typename ImageType::Pointer readImage(const std::vector & filenames //-------------------------------------------------------------------- template -void writeImage(const typename ImageType::Pointer image, const std::string & filename, const bool verbose) +void writeImage(const typename ImageType::Pointer image, const std::string & filename, const bool verbose, const bool compression) { - typedef itk::ImageFileWriter WriterType; - typename WriterType::Pointer writer = WriterType::New(); - writer->SetFileName(filename.c_str()); - writer->SetInput(image); - if (verbose) { - std::cout << "Writing [" << filename << "] ... " << std::endl; - } - try { - writer->Update(); - } catch( itk::ExceptionObject & err ) { - std::cerr << "Exception while writing image [" << filename << "]" << std::endl; - std::cerr << err << std::endl; - exit(-1); - } + return writeImage(image.GetPointer(), filename, verbose, compression); } //-------------------------------------------------------------------- //-------------------------------------------------------------------- template -void writeImage(const ImageType* image, const std::string & filename, const bool verbose) +void writeImage(const ImageType* image, const std::string & filename, const bool verbose, const bool compression) { typedef itk::ImageFileWriter WriterType; typename WriterType::Pointer writer = WriterType::New(); writer->SetFileName(filename.c_str()); writer->SetInput(image); + writer->SetUseCompression(compression); if (verbose) { std::cout << "Writing [" << filename << "] ... " << std::endl; } diff --git a/common/clitkImageToImageGenericFilterBase.cxx b/common/clitkImageToImageGenericFilterBase.cxx index 8b0505d..5f9cfac 100644 --- a/common/clitkImageToImageGenericFilterBase.cxx +++ b/common/clitkImageToImageGenericFilterBase.cxx @@ -36,6 +36,7 @@ clitk::ImageToImageGenericFilterBase::ImageToImageGenericFilterBase(std::string m_FailOnImageTypeError = true; m_ReadOnDisk = true; m_WriteOnDisk = true; + m_WriteCompression = false; // m_LastError = ""; // StopOnErrorOn(); SetFilterBase(NULL); @@ -72,6 +73,14 @@ void clitk::ImageToImageGenericFilterBase::EnableWriteOnDisk(bool b) //-------------------------------------------------------------------- +//-------------------------------------------------------------------- +void clitk::ImageToImageGenericFilterBase::EnableWriteCompression(bool b) +{ + m_WriteCompression = b; +} +//-------------------------------------------------------------------- + + //-------------------------------------------------------------------- void clitk::ImageToImageGenericFilterBase::SetInputFilename(const std::string & filename) { @@ -321,7 +330,7 @@ template void clitk::ImageToImageGenericFilterBase::SetNextOutput(typename ImageType::Pointer output) { if (m_WriteOnDisk && m_OutputFilenames.size()) { - clitk::writeImage(output, m_OutputFilenames.front(), m_IOVerbose); + clitk::writeImage(output, m_OutputFilenames.front(), m_IOVerbose, m_WriteCompression); m_OutputFilenames.pop_front(); } if (m_InputVVImages.size()) //We assume that if a vv image is set as input, we want one as the output diff --git a/common/clitkImageToImageGenericFilterBase.h b/common/clitkImageToImageGenericFilterBase.h index 039b850..03b4f4a 100644 --- a/common/clitkImageToImageGenericFilterBase.h +++ b/common/clitkImageToImageGenericFilterBase.h @@ -62,6 +62,7 @@ namespace clitk { void SetInputFilenames(const std::vector & filenames); void EnableReadOnDisk(bool b); void EnableWriteOnDisk(bool b); + void EnableWriteCompression(bool b); void SetOutputFilename(const std::string & filename); void AddOutputFilename(const std::string filename); void SetOutputFilenames(const std::vector & filenames); @@ -94,6 +95,8 @@ namespace clitk { protected: bool m_ReadOnDisk; bool m_WriteOnDisk; + bool m_WriteCompression; + /// Call this function to dispatch an output towards the correct sink template void SetNextOutput(typename ImageType::Pointer output); diff --git a/tools/clitkImageConvert.cxx b/tools/clitkImageConvert.cxx index 7b14021..297b637 100644 --- a/tools/clitkImageConvert.cxx +++ b/tools/clitkImageConvert.cxx @@ -56,6 +56,7 @@ int main(int argc, char * argv[]) filter->SetInputFilenames(l); filter->SetIOVerbose(args_info.verbose_flag); filter->SetOutputFilename(args_info.output_arg); + filter->EnableWriteCompression(args_info.compression_flag); if (args_info.type_given) filter->SetOutputPixelType(args_info.type_arg); // Go ! diff --git a/tools/clitkImageConvert.ggo b/tools/clitkImageConvert.ggo index 22e3291..395b435 100644 --- a/tools/clitkImageConvert.ggo +++ b/tools/clitkImageConvert.ggo @@ -8,3 +8,4 @@ option "input" i "Input image filename" string no multiple option "output" o "Output image filename" string yes option "type" t "Output type (float, ushort ...)" string no option "verbose" v "Verbose" flag off +option "compression" c "Compress output" flag off -- 2.47.1