#include "vvAnimatedGIFWriter.h" #include "clitkDD.h" #include "ximagif.h" #include #include #include #include #include #include //--------------------------------------------------------------------------- vtkStandardNewMacro(vvAnimatedGIFWriter); //--------------------------------------------------------------------------- vvAnimatedGIFWriter::vvAnimatedGIFWriter() { Rate = 5; Loops = 0; } //--------------------------------------------------------------------------- vvAnimatedGIFWriter::~vvAnimatedGIFWriter() { } //--------------------------------------------------------------------------- void vvAnimatedGIFWriter::Start() { // Create one volume with all slices RGBvolume = vtkSmartPointer::New(); RGBvolume->SetAppendAxis(2); RGBslices.clear(); } //--------------------------------------------------------------------------- void vvAnimatedGIFWriter::Write() { // get the data this->GetInput()->UpdateInformation(); int *wExtent = this->GetInput()->GetWholeExtent(); this->GetInput()->SetUpdateExtent(wExtent); this->GetInput()->Update(); RGBslices.push_back( vtkSmartPointer::New() ); RGBslices.back()->ShallowCopy(this->GetInput()); RGBvolume->AddInput(RGBslices.back()); } //--------------------------------------------------------------------------- void vvAnimatedGIFWriter::End() { RGBvolume->Update(); // Quantize to 8 bit colors vtkSmartPointer quant = vtkSmartPointer::New(); quant->SetNumberOfColors(256); quant->SetInput(RGBvolume->GetOutput()); quant->Update(); // Convert to 8 bit image vtkSmartPointer cast = vtkSmartPointer::New(); cast->SetInput( quant->GetOutput() ); cast->SetOutputScalarTypeToUnsignedChar(); cast->Update(); // Create a stack of CxImages DWORD width = cast->GetOutput()->GetExtent()[1]-cast->GetOutput()->GetExtent()[0]+1; DWORD height = cast->GetOutput()->GetExtent()[3]-cast->GetOutput()->GetExtent()[2]+1; std::vector cximages( RGBslices.size() ); for(unsigned int i=0; iCreateFromArray((BYTE *)cast->GetOutput()->GetScalarPointer(0,0,i), width, height, 8, width, false); cximages[i]->SetFrameDelay(100/Rate); cximages[i]->SetPalette((RGBQUAD*)(quant->GetLookupTable()->GetPointer(0))); // Swap r and b in LUT before setting it RGBQUAD *pal = cximages[i]->GetPalette(); for(unsigned int j=0; j<256; j++) std::swap(pal[j].rgbBlue, pal[j].rgbRed); } // Create gif FILE * pFile; pFile = fopen (this->FileName, "wb"); if(pFile==NULL) { vtkErrorMacro("Error in vvAnimatedGIFWriter::End: could not open " << this->FileName ); return; } CxImageGIF cximagegif; cximagegif.SetLoops(Loops); bool result = cximagegif.Encode(pFile,&(cximages[0]), (int)RGBslices.size(), true); // Cleanup fclose(pFile); for(unsigned int i=0; iSuperclass::PrintSelf(os, indent); }