forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvtkAVIWriter.cxx
288 lines (248 loc) · 8.81 KB
/
vtkAVIWriter.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*=========================================================================
Program: Visualization Toolkit
Module: vtkAVIWriter.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkWindows.h"
#include "vtkAVIWriter.h"
#include "vtkImageData.h"
#include "vtkObjectFactory.h"
#include "vtkErrorCode.h"
#ifdef _MSC_VER
#pragma warning (push, 3)
#endif
#include <vfw.h>
#ifdef _MSC_VER
#pragma warning (pop)
#endif
class vtkAVIWriterInternal
{
public:
PAVISTREAM Stream;
PAVISTREAM StreamCompressed;
PAVIFILE AVIFile;
LPBITMAPINFOHEADER lpbi; // pointer to BITMAPINFOHEADER
HANDLE hDIB; // handle to DIB, temp handle
};
//---------------------------------------------------------------------------
vtkStandardNewMacro(vtkAVIWriter);
//---------------------------------------------------------------------------
vtkAVIWriter::vtkAVIWriter()
{
this->Internals = new vtkAVIWriterInternal;
this->Internals->Stream = NULL;
this->Internals->StreamCompressed = NULL;
this->Internals->AVIFile = NULL;
this->Time = 0;
this->Quality = 2;
this->Rate = 15;
this->Internals->hDIB = NULL; // handle to DIB, temp handle
this->PromptCompressionOptions = 0;
this->CompressorFourCC = NULL;
this->SetCompressorFourCC("msvc");
}
//---------------------------------------------------------------------------
vtkAVIWriter::~vtkAVIWriter()
{
if (this->Internals->AVIFile)
{
this->End();
}
delete this->Internals;
this->SetCompressorFourCC(NULL);
}
//---------------------------------------------------------------------------
void vtkAVIWriter::Start()
{
// Error checking
this->Error = 1;
if ( this->GetInput() == NULL )
{
vtkErrorMacro(<<"Write:Please specify an input!");
this->SetErrorCode(vtkGenericMovieWriter::NoInputError);
return;
}
if (!this->FileName)
{
vtkErrorMacro(<<"Write:Please specify a FileName");
this->SetErrorCode(vtkErrorCode::NoFileNameError);
return;
}
// Fill in image information.
this->GetInput()->UpdateInformation();
int *wExtent = this->GetInput()->GetWholeExtent();
this->GetInput()->SetUpdateExtent(wExtent);
LONG hr;
AVISTREAMINFO strhdr;
AVIFileInit();
// opens AVIFile library
hr = AVIFileOpen(&this->Internals->AVIFile, this->FileName,
OF_WRITE | OF_CREATE, 0L);
if (hr != 0)
{
vtkErrorMacro("Unable to open " << this->FileName);
this->SetErrorCode(vtkErrorCode::CannotOpenFileError);
return;
}
// Fill in the header for the video stream....
// The video stream will run in 15ths of a second....
memset(&strhdr, 0, sizeof(strhdr));
strhdr.fccType = streamtypeVIDEO;// stream type
strhdr.fccHandler = 0;
strhdr.dwScale = 1;
strhdr.dwRate = this->Rate;
strhdr.dwQuality = (DWORD) -1;
strhdr.dwSuggestedBufferSize = (wExtent[1] - wExtent[0] + 1)*
(wExtent[3] - wExtent[2] + 1)*3;
SetRect(&strhdr.rcFrame, 0, 0, wExtent[1] - wExtent[0] + 1,
wExtent[3] - wExtent[2] + 1);
// And create the stream;
AVIFileCreateStream(this->Internals->AVIFile, // file pointer
&this->Internals->Stream, // returned stream pointer
&strhdr); // stream header
// do not want to display this dialog
AVICOMPRESSOPTIONS opts;
AVICOMPRESSOPTIONS FAR * aopts[1] = {&opts};
memset(&opts, 0, sizeof(opts));
// need to setup opts
opts.fccType = 0;
char fourcc[4] = {' ', ' ', ' ', ' '};
if (this->CompressorFourCC)
{
memcpy(fourcc, this->CompressorFourCC, strlen(this->CompressorFourCC));
}
opts.fccHandler=mmioFOURCC(fourcc[0], fourcc[1], fourcc[2], fourcc[3]);
switch (this->GetQuality())
{
case 0:
opts.dwQuality = 2500;
break;
case 1:
opts.dwQuality = 5000;
break;
default:
opts.dwQuality = 10000;
break;
}
opts.dwBytesPerSecond = 0;
opts.dwFlags = AVICOMPRESSF_VALID;
if (this->PromptCompressionOptions)
{
if (!AVISaveOptions(NULL, 0,
1, &this->Internals->Stream,
(LPAVICOMPRESSOPTIONS FAR *) &aopts))
{
vtkErrorMacro("Unable to save " << this->FileName);
return;
}
}
if (AVIMakeCompressedStream(&this->Internals->StreamCompressed,
this->Internals->Stream,
&opts, NULL) != AVIERR_OK)
{
vtkErrorMacro("Unable to compress " << this->FileName);
this->SetErrorCode(vtkGenericMovieWriter::CanNotCompress);
return;
}
DWORD dwLen; // size of memory block
int dataWidth = (((wExtent[1] - wExtent[0] + 1)*3+3)/4)*4;
dwLen = sizeof(BITMAPINFOHEADER) + dataWidth*(wExtent[3] - wExtent[2] + 1);
this->Internals->hDIB = ::GlobalAlloc(GHND, dwLen);
this->Internals->lpbi = (LPBITMAPINFOHEADER) ::GlobalLock(this->Internals->hDIB);
this->Internals->lpbi->biSize = sizeof(BITMAPINFOHEADER);
this->Internals->lpbi->biWidth = wExtent[1] - wExtent[0] + 1;
this->Internals->lpbi->biHeight = wExtent[3] - wExtent[2] + 1;
this->Internals->lpbi->biPlanes = 1;
this->Internals->lpbi->biBitCount = 24;
this->Internals->lpbi->biCompression = BI_RGB;
this->Internals->lpbi->biClrUsed = 0;
this->Internals->lpbi->biClrImportant = 0;
this->Internals->lpbi->biSizeImage = dataWidth*(wExtent[3] - wExtent[2] + 1);
if (AVIStreamSetFormat(this->Internals->StreamCompressed, 0,
this->Internals->lpbi, this->Internals->lpbi->biSize))
{
vtkErrorMacro("Unable to format " << this->FileName << " Most likely this means that the video compression scheme you seleted could not handle the data. Try selecting a different compression scheme." );
this->SetErrorCode(vtkGenericMovieWriter::CanNotFormat);
return;
}
this->Error = 0;
this->Time = 0;
}
//---------------------------------------------------------------------------
void vtkAVIWriter::Write()
{
if (this->Error)
{
return;
}
// get the data
this->GetInput()->UpdateInformation();
int *wExtent = this->GetInput()->GetWholeExtent();
this->GetInput()->SetUpdateExtent(wExtent);
this->GetInput()->Update();
// get the pointer to the data
unsigned char *ptr =
(unsigned char *)(this->GetInput()->GetScalarPointer());
int dataWidth = (((wExtent[1] - wExtent[0] + 1)*3+3)/4)*4;
int srcWidth = (wExtent[1] - wExtent[0] + 1)*3;
// copy the data to the clipboard
unsigned char *dest
= (unsigned char *)this->Internals->lpbi + this->Internals->lpbi->biSize;
int i,j;
for (i = 0; i < this->Internals->lpbi->biHeight; i++)
{
for (j = 0; j < this->Internals->lpbi->biWidth; j++)
{
*dest++ = ptr[2];
*dest++ = ptr[1];
*dest++ = *ptr;
ptr += 3;
}
dest = dest + (dataWidth - srcWidth);
}
AVIStreamWrite(this->Internals->StreamCompressed, // stream pointer
this->Time, // time of this frame
1, // number to write
(LPBYTE) this->Internals->lpbi + // pointer to data
this->Internals->lpbi->biSize,
this->Internals->lpbi->biSizeImage, // size of this frame
AVIIF_KEYFRAME, // flags....
NULL, NULL);
this->Time++;
}
//---------------------------------------------------------------------------
void vtkAVIWriter::End()
{
::GlobalUnlock(this->Internals->hDIB);
if (this->Internals->Stream)
{
AVIStreamClose(this->Internals->Stream);
this->Internals->Stream = NULL;
}
if (this->Internals->StreamCompressed)
{
AVIStreamClose(this->Internals->StreamCompressed);
this->Internals->StreamCompressed = NULL;
}
if (this->Internals->AVIFile)
{
AVIFileClose(this->Internals->AVIFile);
this->Internals->AVIFile = NULL;
}
AVIFileExit(); // releases AVIFile library
}
//---------------------------------------------------------------------------
void vtkAVIWriter::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "Rate: " << this->Rate << endl;
os << indent << "Quality: " << this->Quality << endl;
os << indent << "PromptCompressionOptions: " << (this->GetPromptCompressionOptions() ? "on":"off") << endl;
os << indent << "CompressorFourCC: "
<< (this->CompressorFourCC ? this->CompressorFourCC : "(None)") << endl;
}