GDCM 3.2.1

ConvertMultiFrameToSingleFrame.cxx
/*=========================================================================
Program: GDCM (Grassroots DICOM). A DICOM library
Copyright (c) 2006-2011 Mathieu Malaterre
All rights reserved.
See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkGDCMImageReader.h"
#include "vtkGDCMImageWriter.h"
#include "vtkImageData.h"
#include "vtkStringArray.h"
#include "vtkVersion.h"
#include "gdcmTesting.h"
#include "gdcmFilenameGenerator.h"
int main(int argc, char *argv[])
{
std::string filename;
if( argc <= 1 )
{
const char *directory = gdcm::Testing::GetDataRoot();
if(!directory) return 1;
std::string file = std::string(directory) + "/US-PAL-8-10x-echo.dcm";
filename = file;
}
else
{
filename = argv[1];
}
std::cout << "file: " << filename << std::endl;
vtkGDCMImageReader *reader = vtkGDCMImageReader::New();
reader->SetFileName( filename.c_str() );
reader->Update();
//reader->GetOutput()->Print( std::cout );
int dims[3];
reader->GetOutput()->GetDimensions( dims );
std::ostringstream os;
os << "singleframe";
os << "%04d.dcm";
gdcm::FilenameGenerator fg;
fg.SetPattern( os.str().c_str() );
unsigned int nfiles = dims[2];
fg.SetNumberOfFilenames( nfiles );
bool b = fg.Generate();
if( !b )
{
std::cerr << "FilenameGenerator::Generate() failed" << std::endl;
return 1;
}
if( !fg.GetNumberOfFilenames() )
{
std::cerr << "FilenameGenerator::Generate() failed somehow..." << std::endl;
return 1;
}
// By default write them as Secondary Capture (for portability)
vtkGDCMImageWriter *writer = vtkGDCMImageWriter::New();
vtkStringArray *filenames = vtkStringArray::New();
for(unsigned int i = 0; i < fg.GetNumberOfFilenames(); ++i)
{
filenames->InsertNextValue( fg.GetFilename(i) );
}
assert( filenames->GetNumberOfValues() == (int)fg.GetNumberOfFilenames() );
writer->SetFileNames( filenames );
filenames->Delete();
writer->SetFileDimensionality( 2 );
#if (VTK_MAJOR_VERSION >= 6)
writer->SetInputConnection( reader->GetOutputPort() );
#else
writer->SetInput( reader->GetOutput() );
#endif
writer->SetImageFormat( reader->GetImageFormat() );
writer->Write();
reader->Delete();
writer->Delete();
return 0;
}
gdcm::FilenameGenerator
FilenameGenerator.
Definition gdcmFilenameGenerator.h:37
gdcm::FilenameGenerator::Generate
bool Generate()
Generate (return success)
gdcm::FilenameGenerator::SetPattern
void SetPattern(const char *pattern)
Set/Get pattern.
Definition gdcmFilenameGenerator.h:47
gdcm::FilenameGenerator::SetNumberOfFilenames
void SetNumberOfFilenames(SizeType nfiles)
Set/Get the number of filenames to generate.
gdcm::FilenameGenerator::GetNumberOfFilenames
SizeType GetNumberOfFilenames() const
gdcm::FilenameGenerator::GetFilename
const char * GetFilename(SizeType n) const
Get a particular filename (call after Generate)
gdcm::Testing::GetDataRoot
static const char * GetDataRoot()
Return the GDCM DATA ROOT.
vtkGDCMImageReader
Definition vtkGDCMImageReader.h:105
vtkGDCMImageReader::New
static vtkGDCMImageReader * New()
vtkGDCMImageWriter
Definition vtkGDCMImageWriter.h:50
vtkGDCMImageWriter::Write
virtual void Write()
vtkGDCMImageWriter::New
static vtkGDCMImageWriter * New()
vtkGDCMImageWriter::SetFileNames
virtual void SetFileNames(vtkStringArray *)
gdcmFilenameGenerator.h
gdcmTesting.h
vtkGDCMImageReader.h
vtkGDCMImageWriter.h