]> Creatis software - clitk.git/blob - vv/vvAnimatedGIFWriter.cxx
Merge branch 'master' of git.creatis.insa-lyon.fr:clitk
[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     // Swap r and b in LUT before setting it
79     RGBQUAD *pal = cximages[i]->GetPalette();
80     for(unsigned int j=0; j<256; j++)
81       std::swap(pal[j].rgbBlue, pal[j].rgbRed);
82   }
83
84   // Create gif
85   FILE * pFile;
86   pFile = fopen (this->FileName, "wb");
87   if(pFile==NULL) {
88     vtkErrorMacro("Error in vvAnimatedGIFWriter::End: could not open " << this->FileName );
89     return;
90   }
91   CxImageGIF cximagegif;
92   cximagegif.SetLoops(Loops);
93   bool result = cximagegif.Encode(pFile,&(cximages[0]), (int)RGBslices.size(), true);
94
95   // Cleanup
96   fclose(pFile);
97   for(unsigned int i=0; i<RGBslices.size(); i++)
98     delete cximages[i];
99   if(!result) {
100     vtkErrorMacro("Error in CxImage: " << cximagegif.GetLastError() );
101   }
102 }
103
104 //---------------------------------------------------------------------------
105 void vvAnimatedGIFWriter::PrintSelf(ostream& os, vtkIndent indent)
106 {
107   this->Superclass::PrintSelf(os, indent);
108 }