1 #include "vvAnimatedGIFWriter.h"
4 #include <vtkVersion.h>
5 #include <vtkStreamingDemandDrivenPipeline.h>
6 #include <vtkInformation.h>
7 #include <vtkImageData.h>
8 #include <vtkImageQuantizeRGBToIndex.h>
9 #include <vtkImageAppend.h>
10 #include <vtkImageCast.h>
11 #include <vtkObjectFactory.h>
12 #include <vtkLookupTable.h>
16 //---------------------------------------------------------------------------
17 vtkStandardNewMacro(vvAnimatedGIFWriter);
19 //---------------------------------------------------------------------------
20 vvAnimatedGIFWriter::vvAnimatedGIFWriter()
27 //---------------------------------------------------------------------------
28 vvAnimatedGIFWriter::~vvAnimatedGIFWriter()
32 //---------------------------------------------------------------------------
33 void vvAnimatedGIFWriter::Start()
35 // Create one volume with all slices
36 RGBvolume = vtkSmartPointer<vtkImageAppend>::New();
37 RGBvolume->SetAppendAxis(2);
41 //---------------------------------------------------------------------------
42 void vvAnimatedGIFWriter::Write()
45 #if VTK_MAJOR_VERSION <= 5
46 this->GetInput()->UpdateInformation();
47 int *wExtent = this->GetInput()->GetWholeExtent();
48 this->GetInput()->SetUpdateExtent(wExtent);
49 this->GetInput()->Update();
51 this->UpdateInformation();
52 int *wExtent = this->GetInput()->GetInformation()->Get(vtkDataObject::DATA_EXTENT());
53 this->SetUpdateExtent(wExtent);
57 RGBslices.push_back( vtkSmartPointer<vtkImageData>::New() );
58 RGBslices.back()->ShallowCopy(this->GetInput());
59 #if VTK_MAJOR_VERSION <= 5
60 RGBvolume->AddInput(RGBslices.back());
62 RGBvolume->AddInputData(RGBslices.back());
66 //---------------------------------------------------------------------------
67 void vvAnimatedGIFWriter::End()
71 // Quantize to 8 bit colors
72 vtkSmartPointer<vtkImageQuantizeRGBToIndex> quant = vtkSmartPointer<vtkImageQuantizeRGBToIndex>::New();
73 quant->SetNumberOfColors(256);
74 #if VTK_MAJOR_VERSION <= 5
75 quant->SetInput(RGBvolume->GetOutput());
77 quant->SetInputConnection(RGBvolume->GetOutputPort());
81 // Convert to 8 bit image
82 vtkSmartPointer<vtkImageCast> cast = vtkSmartPointer<vtkImageCast>::New();
83 #if VTK_MAJOR_VERSION <= 5
84 cast->SetInput( quant->GetOutput() );
86 cast->SetInputConnection( quant->GetOutputPort() );
88 cast->SetOutputScalarTypeToUnsignedChar();
91 // Create palette for CxImage => Swap r and b in LUT
93 memcpy(pal, (RGBQUAD*)(quant->GetLookupTable()->GetPointer(0)), sizeof(RGBQUAD)*256);
94 for(unsigned int j=0; j<256; j++)
95 std::swap(pal[j].rgbBlue, pal[j].rgbRed);
97 // Create a stack of CxImages
98 DWORD width = cast->GetOutput()->GetExtent()[1]-cast->GetOutput()->GetExtent()[0]+1;
99 DWORD height = cast->GetOutput()->GetExtent()[3]-cast->GetOutput()->GetExtent()[2]+1;
100 std::vector<CxImage*> cximages( RGBslices.size() );
101 for(unsigned int i=0; i<RGBslices.size(); i++) {
102 cximages[i] = new CxImage;
103 cximages[i]->SetFrameDelay(100/Rate);
105 cximages[i]->CreateFromArray((BYTE *)RGBvolume->GetOutput()->GetScalarPointer(0,0,i),
106 width, height, 24, width*3, false);
107 cximages[i]->SwapRGB2BGR();
108 cximages[i]->DecreaseBpp(8, true, pal);
111 cximages[i]->CreateFromArray((BYTE *)cast->GetOutput()->GetScalarPointer(0,0,i),
112 width, height, 8, width, false);
113 cximages[i]->SetPalette(pal);
119 pFile = fopen (this->FileName, "wb");
121 vtkErrorMacro("Error in vvAnimatedGIFWriter::End: could not open " << this->FileName );
124 CxImageGIF cximagegif;
125 cximagegif.SetLoops(Loops);
126 bool result = cximagegif.Encode(pFile,&(cximages[0]), (int)RGBslices.size(), true);
130 for(unsigned int i=0; i<RGBslices.size(); i++)
133 vtkErrorMacro("Error in CxImage: " << cximagegif.GetLastError() );
137 //---------------------------------------------------------------------------
138 void vvAnimatedGIFWriter::PrintSelf(ostream& os, vtkIndent indent)
140 this->Superclass::PrintSelf(os, indent);