diff --git a/Core/Code/IO/mitkItkPictureWrite.cpp b/Core/Code/IO/mitkItkPictureWrite.cpp index 9cc888da5f..a796fc5c21 100644 --- a/Core/Code/IO/mitkItkPictureWrite.cpp +++ b/Core/Code/IO/mitkItkPictureWrite.cpp @@ -1,167 +1,164 @@ /*=================================================================== 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 -template < typename TPixel, unsigned int VImageDimension > -void _mitkItkPictureWrite(itk::Image< TPixel, VImageDimension >* itkImage, const std::string& fileName) +/** 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 ) { - typedef itk::Image< TPixel, VImageDimension > TImageType; - - typedef itk::Image OutputImage3DType; - typedef itk::Image OutputImage2DType; - typename itk::RescaleIntensityImageFilter::Pointer rescaler = itk::RescaleIntensityImageFilter::New(); - rescaler->SetInput(itkImage); - rescaler->SetOutputMinimum(0); - rescaler->SetOutputMaximum(255); - itk::ImageSeriesWriter::Pointer writer = itk::ImageSeriesWriter::New(); - - // Fix initialize the numberOfSlices to one as default - // test if image has dimension >2 to set the number of slices according to the value of GetSize()[2] - int numberOfSlices = 1; - if( VImageDimension > 2 ) + if( numberOfImages > 1 ) { itk::NumericSeriesFileNames::Pointer numericFileNameWriter = itk::NumericSeriesFileNames::New(); - numberOfSlices = itkImage->GetLargestPossibleRegion().GetSize()[2]; - std::string finalFileName = fileName; - std::string::size_type pos = fileName.find_last_of(".",fileName.length()-1); + 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"); - numericFileNameWriter->SetEndIndex(numberOfSlices); + std::cout << "Filename: " << finalFileName << std::endl; + + 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(fileName); + 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; } - writer->SetInput( rescaler->GetOutput() ); - writer->Update(); + // 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 + { + 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(); + } + } } 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::ImageFileWriter< TImageType > WriterType; typename WriterType::Pointer simpleWriter = WriterType::New(); simpleWriter->SetFileName( fileName ); simpleWriter->SetInput( itkImage ); try { simpleWriter->Update(); } catch( itk::ExceptionObject &e) { std::cerr << "Caught 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) -// typedef itk::Image, 2> itkImageRGBUC2; -// template <> void _mitkItkImageWrite, 2>(itkImageRGBUC2* itkImage, const std::string& fileName) -// { -// typedef itkImageRGBUC2 TImageType; -// -// itk::ImageFileWriter::Pointer writer = itk::ImageFileWriter::New(); -// writer->SetInput( itkImage ); -// writer->SetFileName( fileName.c_str() ); -// writer->Update(); -// }; -// -// typedef itk::Image, 3> itkImageRGBUC3; -// template <> void _mitkItkImageWrite, 3>(itkImageRGBUC3* itkImage, const std::string& fileName) -// { -// typedef itkImageRGBUC3 TImageType; -// -// itk::ImageFileWriter::Pointer writer = itk::ImageFileWriter::New(); -// writer->SetInput( itkImage ); -// writer->SetFileName( fileName.c_str() ); -// writer->Update(); -// }; -// -// typedef itk::Image, 3> itkImageDTIF3; -// template <> void _mitkItkImageWrite, 3>(itkImageDTIF3* itkImage, const std::string& fileName) -// { -// typedef itkImageDTIF3 TImageType; -// -// itk::ImageFileWriter::Pointer writer = itk::ImageFileWriter::New(); -// writer->SetInput( itkImage ); -// writer->SetFileName( fileName.c_str() ); -// writer->Update(); -// }; -// -// typedef itk::Image, 3> itkImageDTID3; -// template <> void _mitkItkImageWrite, 3>(itkImageDTID3* itkImage, const std::string& fileName) -// { -// typedef itkImageDTID3 TImageType; -// -// itk::ImageFileWriter::Pointer writer = itk::ImageFileWriter::New(); -// writer->SetInput( itkImage ); -// writer->SetFileName( fileName.c_str() ); -// writer->Update(); -// }; -// -// typedef itk::Image, 2> itkImageDTIF2; -// template <> void _mitkItkImageWrite, 2>(itkImageDTIF2* itkImage, const std::string& fileName) -// { -// typedef itkImageDTIF2 TImageType; -// -// itk::ImageFileWriter::Pointer writer = itk::ImageFileWriter::New(); -// writer->SetInput( itkImage ); -// writer->SetFileName( fileName.c_str() ); -// writer->Update(); -// }; -// -// typedef itk::Image, 2> itkImageDTID2; -// template <> void _mitkItkImageWrite, 2>(itkImageDTID2* itkImage, const std::string& fileName) -// { -// typedef itkImageDTID2 TImageType; -// -// itk::ImageFileWriter::Pointer writer = itk::ImageFileWriter::New(); -// writer->SetInput( itkImage ); -// writer->SetFileName( fileName.c_str() ); -// writer->Update(); -// }; -