diff --git a/Core/Code/IO/mitkItkPictureWrite.cpp b/Core/Code/IO/mitkItkPictureWrite.cpp index a796fc5c21..312e7e7f74 100644 --- a/Core/Code/IO/mitkItkPictureWrite.cpp +++ b/Core/Code/IO/mitkItkPictureWrite.cpp @@ -1,164 +1,181 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include "mitkItkPictureWrite.h" #include #include #include #include #include /** Set the filenames to the specified writer in dependace on the number of images passed in */ template< class WriterType > void SetOutputNames( typename WriterType::Pointer writer, const std::string& baseFileName, unsigned int numberOfImages ) { if( numberOfImages > 1 ) { itk::NumericSeriesFileNames::Pointer numericFileNameWriter = itk::NumericSeriesFileNames::New(); std::string finalFileName = baseFileName; std::string::size_type pos = baseFileName.find_last_of(".",baseFileName.length()-1); if(pos==std::string::npos) finalFileName.append(".%d.png"); else finalFileName.insert(pos,".%d"); - std::cout << "Filename: " << finalFileName << std::endl; + MITK_DEBUG << "Filename: " << finalFileName; numericFileNameWriter->SetEndIndex(numberOfImages); numericFileNameWriter->SetSeriesFormat(finalFileName.c_str()); numericFileNameWriter->Modified(); writer->SetFileNames(numericFileNameWriter->GetFileNames()); } // if the given image is an 2D-png image, do not use the numericFileNameWriter // to generate the name, since it alters the fileName given as parameter else { writer->SetFileName( baseFileName.c_str() ); } } template < typename TPixel, unsigned int VImageDimension > void _mitkItkPictureWrite(itk::Image< TPixel, VImageDimension >* itkImage, const std::string& fileName) { typedef itk::Image< TPixel, VImageDimension > TImageType; typedef itk::Image UCharOutputImage3DType; typedef itk::Image ShortOutputImage3DType; typedef itk::Image OutputImage2D_8bitType; typedef itk::Image OutputImage2D_16bitType; typedef itk::ImageSeriesWriter UCharWriterType; typedef itk::ImageSeriesWriter ShortWriterType; typedef itk::RescaleIntensityImageFilter UCharRescalerFilterType; typedef itk::RescaleIntensityImageFilter ShortRescalerFilterType; // get the size info size_t inputTypeSize = sizeof(TPixel); size_t supportedOutputMaxSize = 1; // default value 8bit // the PNG and TIFF formats can handle up-to 16-bit images if( fileName.find(".png") != std::string::npos || fileName.find(".tif") != std::string::npos ) { supportedOutputMaxSize = 2; } // get the dimension info unsigned int numberOfImages = 1; if( itkImage->GetImageDimension() > 2) numberOfImages = itkImage->GetLargestPossibleRegion().GetSize()[2]; typename ShortRescalerFilterType::Pointer sh_rescaler = ShortRescalerFilterType::New(); sh_rescaler->SetInput( itkImage ); sh_rescaler->SetOutputMinimum( 0 ); sh_rescaler->SetOutputMaximum( 65535 ); typename UCharRescalerFilterType::Pointer rescaler = UCharRescalerFilterType::New(); rescaler->SetInput( itkImage ); rescaler->SetOutputMinimum( 0 ); rescaler->SetOutputMaximum( 255 ); - // - if( inputTypeSize == 1) - { - UCharWriterType::Pointer writer = UCharWriterType::New(); - SetOutputNames( writer, fileName, numberOfImages ); - writer->SetInput( rescaler->GetOutput() ); - writer->Update(); - } - // 16bit 16bit possible - else if ( inputTypeSize == supportedOutputMaxSize && supportedOutputMaxSize == 2 ) - { - ShortWriterType::Pointer writer = ShortWriterType::New(); - SetOutputNames( writer, fileName, numberOfImages ); - writer->SetInput( sh_rescaler->GetOutput() ); - writer->Update(); - } - // rescaling to maximum of supported format - else + try { - if( supportedOutputMaxSize == 2) + // input is 8 bit + if( inputTypeSize == 1) + { + UCharWriterType::Pointer writer = UCharWriterType::New(); + SetOutputNames( writer, fileName, numberOfImages ); + writer->SetInput( rescaler->GetOutput() ); + writer->Update(); + } + // input pixel type is 16bit -> writer can handle 16bit images + else if ( inputTypeSize == supportedOutputMaxSize && supportedOutputMaxSize == 2 ) { - typename ShortWriterType::Pointer writer = ShortWriterType::New(); + ShortWriterType::Pointer writer = ShortWriterType::New(); SetOutputNames( writer, fileName, numberOfImages ); - writer->SetInput(sh_rescaler->GetOutput() ); + writer->SetInput( sh_rescaler->GetOutput() ); writer->Update(); } + // rescaling input to maximum of supported format else { - typename UCharWriterType::Pointer writer = UCharWriterType::New(); - SetOutputNames( writer, fileName, numberOfImages ); - writer->SetInput( rescaler->GetOutput() ); - writer->Update(); + if( supportedOutputMaxSize == 2) + { + typename ShortWriterType::Pointer writer = ShortWriterType::New(); + SetOutputNames( writer, fileName, numberOfImages ); + writer->SetInput(sh_rescaler->GetOutput() ); + writer->Update(); + } + else + { + typename UCharWriterType::Pointer writer = UCharWriterType::New(); + SetOutputNames( writer, fileName, numberOfImages ); + writer->SetInput( rescaler->GetOutput() ); + writer->Update(); + } } } + catch(const itk::ExceptionObject& e) + { + MITK_ERROR << "ITK Exception occured: " << e.what(); + mitkThrow() << "Caught ITK exception while writing image with scalar type \n" << e.what(); + } } template < typename TPixel, unsigned int VImageDimension > void _mitkItkPictureWriteComposite(itk::Image< TPixel, VImageDimension >* itkImage, const std::string& fileName) { typedef itk::Image< TPixel, VImageDimension > TImageType; + typedef itk::Image< TPixel, 2 > TImageType2D; + + typedef itk::ImageSeriesWriter< TImageType, TImageType2D > WriterType; + typename WriterType::Pointer writer = WriterType::New(); - typedef itk::ImageFileWriter< TImageType > WriterType; - typename WriterType::Pointer simpleWriter = WriterType::New(); + // get the dimension info + unsigned int numberOfImages = 1; + if( itkImage->GetImageDimension() > 2) + numberOfImages = itkImage->GetLargestPossibleRegion().GetSize()[2]; + + // create output name(s) + SetOutputNames( writer, fileName, numberOfImages ); - simpleWriter->SetFileName( fileName ); - simpleWriter->SetInput( itkImage ); + writer->SetInput( itkImage ); try { - simpleWriter->Update(); + writer->Update(); } - catch( itk::ExceptionObject &e) + catch(const itk::ExceptionObject &e) { - std::cerr << "Caught exception while writing image with composite type: \n" << e.what(); + MITK_ERROR << "ITK Exception occured: " << e.what(); + mitkThrow() << "Caught ITK exception while writing image with composite type \n" << e.what(); } } #define InstantiateAccessFunction__mitkItkPictureWrite(pixelType, dim) \ template MITK_CORE_EXPORT void _mitkItkPictureWrite(itk::Image*, const std::string&); #define InstantiateAccessFunction__mitkItkPictureWriteComposite(pixelType, dim) \ template MITK_CORE_EXPORT void _mitkItkPictureWriteComposite(itk::Image*, const std::string&); InstantiateAccessFunction(_mitkItkPictureWrite) InstantiateAccessFunctionForFixedPixelType( _mitkItkPictureWriteComposite, MITK_ACCESSBYITK_PIXEL_TYPES_SEQ MITK_ACCESSBYITK_COMPOSITE_PIXEL_TYPES_SEQ)