]> Creatis software - clitk.git/blob - vv/vvAnimatedGIFWriter.cxx
Added animated GIF video snapshot (CLITK_EXPERIMENTAL yet)
[clitk.git] / vv / vvAnimatedGIFWriter.cxx
1 #include "vvAnimatedGIFWriter.h"
2 #include "clitkDD.h"
3
4 #include "ximagif.h"
5
6 #include <vtkImageData.h>
7 #include <vtkImageQuantizeRGBToIndex.h>
8 #include <vtkImageAppend.h>
9 #include <vtkImageCast.h>
10 #include <vtkObjectFactory.h>
11 #include <vtkLookupTable.h>
12
13 //---------------------------------------------------------------------------
14 vtkStandardNewMacro(vvAnimatedGIFWriter);
15
16 //---------------------------------------------------------------------------
17 vvAnimatedGIFWriter::vvAnimatedGIFWriter()
18 {
19   Rate = 5;
20   Loops = 0;
21 }
22
23 //---------------------------------------------------------------------------
24 vvAnimatedGIFWriter::~vvAnimatedGIFWriter()
25 {
26 }
27
28 //---------------------------------------------------------------------------
29 void vvAnimatedGIFWriter::Start()
30 {
31   // Create one volume with all slices
32   RGBvolume = vtkSmartPointer<vtkImageAppend>::New();
33   RGBvolume->SetAppendAxis(2);
34   RGBslices.clear();
35 }
36
37 //---------------------------------------------------------------------------
38 void vvAnimatedGIFWriter::Write()
39 {
40   // get the data
41   this->GetInput()->UpdateInformation();
42   int *wExtent = this->GetInput()->GetWholeExtent();
43   this->GetInput()->SetUpdateExtent(wExtent);
44   this->GetInput()->Update();
45
46   RGBslices.push_back( vtkSmartPointer<vtkImageData>::New() );
47   RGBslices.back()->ShallowCopy(this->GetInput());
48   RGBvolume->AddInput(RGBslices.back());
49 }
50
51 //---------------------------------------------------------------------------
52 void vvAnimatedGIFWriter::End()
53 {
54   RGBvolume->Update();
55
56   // Quantize to 8 bit colors
57   vtkSmartPointer<vtkImageQuantizeRGBToIndex> quant = vtkSmartPointer<vtkImageQuantizeRGBToIndex>::New();
58   quant->SetNumberOfColors(256);
59   quant->SetInput(RGBvolume->GetOutput());
60   quant->Update();
61
62   // Convert to 8 bit image
63   vtkSmartPointer<vtkImageCast> cast =  vtkSmartPointer<vtkImageCast>::New();
64   cast->SetInput( quant->GetOutput() );
65   cast->SetOutputScalarTypeToUnsignedChar();
66   cast->Update();
67
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)));
78   }
79
80   // Create gif
81   FILE * pFile;
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);
86
87   // Cleanup
88   fclose(pFile);
89   for(unsigned int i=0; i<RGBslices.size(); i++)
90     delete cximages[i];
91   if(!result) {
92     vtkErrorMacro("Error in CxImage: " << cximagegif.GetLastError() );
93   }
94 }
95
96 //---------------------------------------------------------------------------
97 void vvAnimatedGIFWriter::PrintSelf(ostream& os, vtkIndent indent)
98 {
99   this->Superclass::PrintSelf(os, indent);
100 }