1 #include "vvAnimatedGIFWriter.h"
6 #include <vtkImageData.h>
7 #include <vtkImageQuantizeRGBToIndex.h>
8 #include <vtkImageAppend.h>
9 #include <vtkImageCast.h>
10 #include <vtkObjectFactory.h>
11 #include <vtkLookupTable.h>
13 //---------------------------------------------------------------------------
14 vtkStandardNewMacro(vvAnimatedGIFWriter);
16 //---------------------------------------------------------------------------
17 vvAnimatedGIFWriter::vvAnimatedGIFWriter()
23 //---------------------------------------------------------------------------
24 vvAnimatedGIFWriter::~vvAnimatedGIFWriter()
28 //---------------------------------------------------------------------------
29 void vvAnimatedGIFWriter::Start()
31 // Create one volume with all slices
32 RGBvolume = vtkSmartPointer<vtkImageAppend>::New();
33 RGBvolume->SetAppendAxis(2);
37 //---------------------------------------------------------------------------
38 void vvAnimatedGIFWriter::Write()
41 this->GetInput()->UpdateInformation();
42 int *wExtent = this->GetInput()->GetWholeExtent();
43 this->GetInput()->SetUpdateExtent(wExtent);
44 this->GetInput()->Update();
46 RGBslices.push_back( vtkSmartPointer<vtkImageData>::New() );
47 RGBslices.back()->ShallowCopy(this->GetInput());
48 RGBvolume->AddInput(RGBslices.back());
51 //---------------------------------------------------------------------------
52 void vvAnimatedGIFWriter::End()
56 // Quantize to 8 bit colors
57 vtkSmartPointer<vtkImageQuantizeRGBToIndex> quant = vtkSmartPointer<vtkImageQuantizeRGBToIndex>::New();
58 quant->SetNumberOfColors(256);
59 quant->SetInput(RGBvolume->GetOutput());
62 // Convert to 8 bit image
63 vtkSmartPointer<vtkImageCast> cast = vtkSmartPointer<vtkImageCast>::New();
64 cast->SetInput( quant->GetOutput() );
65 cast->SetOutputScalarTypeToUnsignedChar();
68 // Create a stack of CxImages
69 DWORD width = cast->GetOutput()->GetExtent()[1]-cast->GetOutput()->GetExtent()[0]+1;
70 DWORD height = cast->GetOutput()->GetExtent()[3]-cast->GetOutput()->GetExtent()[2]+1;
71 std::vector<CxImage*> cximages( RGBslices.size() );
72 for(unsigned int i=0; i<RGBslices.size(); i++) {
73 cximages[i] = new CxImage;
74 cximages[i]->CreateFromArray((BYTE *)cast->GetOutput()->GetScalarPointer(0,0,i),
75 width, height, 8, width, false);
76 cximages[i]->SetFrameDelay(100/Rate);
77 cximages[i]->SetPalette((RGBQUAD*)(quant->GetLookupTable()->GetPointer(0)));
82 pFile = fopen (this->FileName, "wb");
83 CxImageGIF cximagegif;
84 cximagegif.SetLoops(Loops);
85 bool result = cximagegif.Encode(pFile,&(cximages[0]), (int)RGBslices.size(), true);
89 for(unsigned int i=0; i<RGBslices.size(); i++)
92 vtkErrorMacro("Error in CxImage: " << cximagegif.GetLastError() );
96 //---------------------------------------------------------------------------
97 void vvAnimatedGIFWriter::PrintSelf(ostream& os, vtkIndent indent)
99 this->Superclass::PrintSelf(os, indent);