# ConvertSingleBitTo8Bits.cxx

    /*=========================================================================

      Program: GDCM (Grassroots DICOM). A DICOM library
      Module:  $URL$

      Copyright (c) 2006-2010 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 "gdcmTesting.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, barray->GetValue(i) );
        }

      vtkImageData *copy = vtkImageData::New();
      copy->SetScalarType( VTK_UNSIGNED_CHAR );
      copy->SetExtent( reader->GetOutput()->GetExtent() );
      copy->AllocateScalars();

      //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() );
      writer->SetInput( copy );
      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;
    }