GDCM 3.2.1

ConvertSingleBitTo8Bits.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 "vtkImageCast.h"
#include "vtkPointData.h"
#include "vtkBitArray.h"
#include "vtkUnsignedCharArray.h"
#include "vtkVersion.h"
int main(int argc, char *argv[])
{
if( argc < 3 )
{
return 1;
}
const char *filename = argv[1];
const char *outfilename = argv[2];
vtkGDCMImageReader *reader = vtkGDCMImageReader::New();
reader->SetFileName( filename );
reader->Update();
//reader->GetOutput()->Print( std::cout );
vtkDataArray* array = reader->GetOutput()->GetPointData()->GetScalars();
vtkBitArray *barray = vtkBitArray::SafeDownCast( array );
if( !barray ) return false;
vtkIdType nvalues = array->GetNumberOfTuples();
vtkUnsignedCharArray *uarray = vtkUnsignedCharArray::New();
uarray->SetNumberOfTuples( nvalues );
for(vtkIdType i = 0; i < nvalues; ++i)
{
uarray->SetValue( i, (unsigned char)barray->GetValue(i) );
}
vtkImageData *copy = vtkImageData::New();
// http://www.vtk.org/Wiki/VTK/VTK_6_Migration/Changes_to_Scalars_Manipulation_Functions#AllocateScalars.28.29
copy->SetExtent( reader->GetOutput()->GetExtent() );
#if (VTK_MAJOR_VERSION >= 6)
copy->AllocateScalars(VTK_UNSIGNED_CHAR, 3);
#else
copy->SetScalarType( VTK_UNSIGNED_CHAR );
copy->AllocateScalars();
#endif
//uarray->Print( std::cout );
//copy->GetPointData()->GetScalars()->Print( std::cout );
copy->GetPointData()->SetScalars( uarray );
uarray->Delete();
vtkGDCMImageWriter *writer = vtkGDCMImageWriter::New();
writer->SetFileName( outfilename );
//writer->SetInput( cast->GetOutput() );
#if (VTK_MAJOR_VERSION >= 6)
writer->SetInputData( copy );
#else
writer->SetInput( copy );
#endif
writer->SetImageFormat( reader->GetImageFormat() );
writer->SetMedicalImageProperties( reader->GetMedicalImageProperties() );
writer->SetDirectionCosines( reader->GetDirectionCosines() );
writer->SetShift( reader->GetShift() );
writer->SetScale( reader->GetScale() );
writer->SetFileDimensionality( reader->GetFileDimensionality( ) );
writer->Write();
reader->Delete();
copy->Delete();
writer->Delete();
return 0;
}
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::SetMedicalImageProperties
virtual void SetMedicalImageProperties(vtkMedicalImageProperties *)
vtkGDCMImageWriter::SetDirectionCosines
virtual void SetDirectionCosines(vtkMatrix4x4 *matrix)
vtkGDCMImageReader.h
vtkGDCMImageWriter.h