]> Creatis software - clitk.git/blob - vv/vvAnimatedGIFWriter.cxx
Save correct landmarks coordinates with transformation
[clitk.git] / vv / vvAnimatedGIFWriter.cxx
1 #include "vvAnimatedGIFWriter.h"
2 #include "clitkDD.h"
3
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>
13
14 #include "ximagif.h"
15
16 //---------------------------------------------------------------------------
17 vtkStandardNewMacro(vvAnimatedGIFWriter);
18
19 //---------------------------------------------------------------------------
20 vvAnimatedGIFWriter::vvAnimatedGIFWriter()
21 {
22   Rate = 5;
23   Loops = 0;
24   Dither = false;
25 }
26
27 //---------------------------------------------------------------------------
28 vvAnimatedGIFWriter::~vvAnimatedGIFWriter()
29 {
30 }
31
32 //---------------------------------------------------------------------------
33 void vvAnimatedGIFWriter::Start()
34 {
35   // Create one volume with all slices
36   RGBvolume = vtkSmartPointer<vtkImageAppend>::New();
37   RGBvolume->SetAppendAxis(2);
38   RGBslices.clear();
39 }
40
41 //---------------------------------------------------------------------------
42 void vvAnimatedGIFWriter::Write()
43 {
44   // get the data
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();
50 #else
51   this->UpdateInformation();
52   int *wExtent = this->GetInput()->GetInformation()->Get(vtkDataObject::DATA_EXTENT());
53   this->SetUpdateExtent(wExtent);
54   this->Update();
55 #endif
56
57   RGBslices.push_back( vtkSmartPointer<vtkImageData>::New() );
58   RGBslices.back()->ShallowCopy(this->GetInput());
59 #if VTK_MAJOR_VERSION <= 5
60   RGBvolume->AddInput(RGBslices.back());
61 #else
62   RGBvolume->AddInputData(RGBslices.back());
63 #endif
64 }
65
66 //---------------------------------------------------------------------------
67 void vvAnimatedGIFWriter::End()
68 {
69   RGBvolume->Update();
70
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());
76 #else
77   quant->SetInputConnection(RGBvolume->GetOutputPort());
78 #endif
79   quant->Update();
80
81   // Convert to 8 bit image
82   vtkSmartPointer<vtkImageCast> cast =  vtkSmartPointer<vtkImageCast>::New();
83 #if VTK_MAJOR_VERSION <= 5
84   cast->SetInput( quant->GetOutput() );
85 #else
86   cast->SetInputConnection( quant->GetOutputPort() );
87 #endif
88   cast->SetOutputScalarTypeToUnsignedChar();
89   cast->Update();
90
91   // Create palette for CxImage => Swap r and b in LUT
92   RGBQUAD pal[256];
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);
96
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);
104     if(Dither) {
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);
109     }
110     else {
111       cximages[i]->CreateFromArray((BYTE *)cast->GetOutput()->GetScalarPointer(0,0,i),
112                                    width, height, 8, width, false);
113       cximages[i]->SetPalette(pal);
114     }
115   }
116
117   // Create gif
118   FILE * pFile;
119   pFile = fopen (this->FileName, "wb");
120   if(pFile==NULL) {
121     vtkErrorMacro("Error in vvAnimatedGIFWriter::End: could not open " << this->FileName );
122     return;
123   }
124   CxImageGIF cximagegif;
125   cximagegif.SetLoops(Loops);
126   bool result = cximagegif.Encode(pFile,&(cximages[0]), (int)RGBslices.size(), true);
127
128   // Cleanup
129   fclose(pFile);
130   for(unsigned int i=0; i<RGBslices.size(); i++)
131     delete cximages[i];
132   if(!result) {
133     vtkErrorMacro("Error in CxImage: " << cximagegif.GetLastError() );
134   }
135 }
136
137 //---------------------------------------------------------------------------
138 void vvAnimatedGIFWriter::PrintSelf(ostream& os, vtkIndent indent)
139 {
140   this->Superclass::PrintSelf(os, indent);
141 }