File: animation.cpp

package info (click to toggle)
povray 1%3A3.7.0.10-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 147,232 kB
  • sloc: cpp: 845,011; ansic: 122,118; sh: 34,204; pascal: 6,420; asm: 3,355; ada: 1,681; makefile: 1,389; cs: 879; awk: 590; perl: 245; xml: 95
file content (384 lines) | stat: -rw-r--r-- 10,129 bytes parent folder | download | duplicates (6)
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*******************************************************************************
 * animation.cpp
 *
 * ---------------------------------------------------------------------------
 * Persistence of Vision Ray Tracer ('POV-Ray') version 3.7.
 * Copyright 1991-2013 Persistence of Vision Raytracer Pty. Ltd.
 *
 * POV-Ray is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * POV-Ray is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * ---------------------------------------------------------------------------
 * POV-Ray is based on the popular DKB raytracer version 2.12.
 * DKBTrace was originally written by David K. Buck.
 * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
 * ---------------------------------------------------------------------------
 * $File: //depot/public/povray/3.x/source/base/animation/animation.cpp $
 * $Revision: #1 $
 * $Change: 6069 $
 * $DateTime: 2013/11/06 11:59:40 $
 * $Author: chrisc $
 *******************************************************************************/

#include <boost/scoped_ptr.hpp>

// configbase.h must always be the first POV file included within base *.cpp files
#include "base/configbase.h"
#include "base/image/image.h"
#include "base/image/png_pov.h"
#include "base/image/jpeg_pov.h"
#include "base/image/bmp.h"
#include "base/animation/animation.h"
//#include "base/animation/avi.h"
#include "base/animation/moov.h"
//#include "base/animation/mpeg.h"

// this must be the last file included
#include "base/povdebug.h"

namespace pov_base
{

Animation::Animation(FileType aftype, IStream *file, const ReadOptions& options) :
	fileType(aftype),
	inFile(file),
	outFile(NULL),
	readOptions(options)
{
	float seconds = 0.0f;

	currentFrame = 0;

	switch(fileType)
	{
		case AVI:
		//	state = Avi::ReadFileHeader(inFile, seconds, totalFrames, codec, width, height, readOptions, warnings);
			break;
		case MOV:
			state = Moov::ReadFileHeader(inFile, seconds, totalFrames, codec, width, height, readOptions, warnings);
			break;
		case MPEG:
		//	state = Mpeg::ReadFileHeader(inFile, seconds, totalFrames, codec, width, height, readOptions, warnings);
			break;
	}

	if(state == NULL)
		throw POV_EXCEPTION(kCannotHandleDataErr, "Cannot read animation file header in the specified format!");

	frameDuration = seconds / float(totalFrames);
}

Animation::Animation(FileType aftype, CodecType c, OStream *file, unsigned int w, unsigned int h, const WriteOptions& options) :
	fileType(aftype),
	inFile(NULL),
	outFile(file),
	width(w),
	height(h),
	writeOptions(options),
	codec(c)
{
	totalFrames = 0;
	frameDuration = 1.0f / options.framespersecond;

	blurMatrixRadius = 7;

	switch(fileType)
	{
		case AVI:
		//	state = Avi::WriteFileHeader(outFile, codec, width, height, writeOptions, warnings);
			break;
		case MOV:
			state = Moov::WriteFileHeader(outFile, codec, width, height, writeOptions, warnings);
			break;
		case MPEG:
		//	state = Mpeg::WriteFileHeader(outFile, codec, width, height, writeOptions, warnings);
			break;
	}

	if(state == NULL)
		throw POV_EXCEPTION(kCannotHandleDataErr, "Cannot write animation file with the specified format and codec!");

	// TODO FIXME - build blur matrix (this code only builds an identity matrix)
	for(size_t y = 0; y < 15; y++)
	{
		for(size_t x = 0; x < 15; x++)
			blurMatrix[x][y] = 0.0f;
	}
	blurMatrix[blurMatrixRadius + 1][blurMatrixRadius + 1] = 1.0f;
}

Animation::~Animation()
{
	if(outFile != NULL)
	{
		switch(fileType)
		{
			case AVI:
			//	Avi::FinishWriteFile(outFile, writeOptions, warnings, state);
				break;
			case MOV:
				Moov::FinishWriteFile(outFile, writeOptions, warnings, state);
				break;
			case MPEG:
			//	Mpeg::FinishWriteFile(outFile, writeOptions, warnings, state);
				break;
		}
	}
	else if(inFile != NULL)
	{
		switch(fileType)
		{
			case AVI:
			//	Avi::FinishReadFile(inFile, warnings, state);
				break;
			case MOV:
				Moov::FinishReadFile(inFile, warnings, state);
				break;
			case MPEG:
			//	Mpeg::FinishReadFile(inFile, warnings, state);
				break;
		}
	}

	state = NULL;
}

Animation *Animation::Open(FileType aftype, IStream *file, const ReadOptions& options) // reading only
{
	return new Animation(aftype, file, options);
}

Animation *Animation::Open(FileType aftype, CodecType codec, OStream *file, unsigned int w, unsigned int h, const WriteOptions& options) // writing only
{
	return new Animation(aftype, codec, file, w, h, options);
}

void Animation::AppendFrame(Image *image) // writing only - NOTE: This method reserves the right to *modify* the image passed to it!!! [trf]
{
	if(writeOptions.blurradius > 0.0f)
	{
		boost::scoped_ptr<Image> mask(Image::Create(image->GetWidth(), image->GetHeight(), Image::Bit_Map));
		float r, g, b, f, t;

		mask->FillBitValue(false);

		if(writeOptions.bluredgethreshold < 1.0f)
			ComputeBlurMask(*image, *mask.get());

		for(int y = 0; y < image->GetHeight(); y++)
		{
			for(int x = 0; x < image->GetWidth(); x++)
			{
				if(mask->GetBitValue(x, y) == true)
				{
					image->GetRGBFTValue(x, y, r, g, b, f, t);
					GetBlurredPixel(*image, x, y, r, g, b);
					image->SetRGBFTValue(x, y, r, g, b, f, t);
				}
			}
		}
	}

	WriteFrame(outFile, image);

	totalFrames++;
}

Image *Animation::ReadNextFrame() // reading only
{
	currentFrame++;

	return ReadFrame(inFile);
}

float Animation::GetLengthInSeconds() const
{
	return frameDuration * totalFrames;
}

unsigned int Animation::GetLengthInFrames() const
{
	return totalFrames;
}

unsigned int Animation::GetCurrentFrame() const // reading only
{
	return currentFrame;
}

void Animation::SetCurrentFrame(unsigned int frame) // reading only
{
	currentFrame = frame;
}

const vector<string>& Animation::GetWarnings() const
{
	return warnings;
}

void Animation::ClearWarnings()
{
	warnings.clear();
}

Image *Animation::ReadFrame(IStream *file)
{
	POV_LONG bytes = 0;
	Image *image = NULL;
	Image::ReadOptions options;

	options.defaultGamma = PowerLawGammaCurve::GetByDecodingGamma(readOptions.gamma);
	options.gammacorrect = readOptions.gammacorrect;
	options.itype = Image::RGBFT_Float;

	switch(fileType)
	{
		case AVI:
		//	Avi::PreReadFrame(file, currentFrame, bytes, codec, readOptions, warnings, state);
			break;
		case MOV:
			Moov::PreReadFrame(file, currentFrame, bytes, codec, readOptions, warnings, state);
			break;
	}

	POV_LONG prepos = file->tellg();

	switch(codec)
	{
		case PNGCodec:
			image = Png::Read(file, options);
			break;
		case BMPCodec:
			image = Bmp::Read(file, options);
			break;
		case JPEGCodec:
			image = Jpeg::Read(file, options);
			break;
		case MPEG1Codec:
		case MPEG2Codec:
		//	image = Mpeg::ReadFrame(file, currentFrame, codec, readOptions, warnings, state);
			break;
	}

	if(file->tellg() < (prepos + bytes))
		warnings.push_back("Frame decompressor read fewer bytes than expected.");
	else if(file->tellg() > (prepos + bytes))
		throw POV_EXCEPTION(kInvalidDataSizeErr, "Frame decompressor read more bytes than expected. The input file may be corrupted!");

	file->seekg(prepos + bytes, SEEK_END);

	switch(fileType)
	{
		case AVI:
		//	Avi::PostReadFrame(file, currentFrame, bytes, codec, readOptions, warnings, state);
			break;
		case MOV:
			Moov::PostReadFrame(file, currentFrame, bytes, codec, readOptions, warnings, state);
			break;
	}

	return image;
}

POV_LONG Animation::WriteFrame(OStream *file, const Image *image)
{
	Image::WriteOptions options;

	options.bpcc = writeOptions.bpcc;
	options.alphachannel = writeOptions.alphachannel;
	options.compress = writeOptions.compress;
	// options.gamma = writeOptions.gamma;
	options.encodingGamma = PowerLawGammaCurve::GetByEncodingGamma(writeOptions.gamma);

	switch(fileType)
	{
		case AVI:
		//	Avi::PreWriteFrame(file, writeOptions, warnings, state);
			break;
		case MOV:
			Moov::PreWriteFrame(file, writeOptions, warnings, state);
			break;
	}

	POV_LONG bytes = file->tellg();

	switch(codec)
	{
		case PNGCodec:
			Png::Write(file, image, options);
			break;
		case BMPCodec:
			Bmp::Write(file, image, options);
			break;
		case JPEGCodec:
			// TODO FIXME Jpeg::Write(file, image, options);
			break;
		case MPEG1Codec:
		case MPEG2Codec:
		//	Mpeg::WriteFrame(file, image, codec, writeOptions, warnings, state);
			break;
	}

	bytes = (file->tellg() - bytes);

	switch(fileType)
	{
		case AVI:
		//	Avi::PostWriteFrame(file, bytes, writeOptions, warnings, state);
			break;
		case MOV:
			Moov::PostWriteFrame(file, bytes, writeOptions, warnings, state);
			break;
	}

	return bytes;
}

void Animation::ComputeBlurMask(const Image& image, Image& mask)
{
	for(int y = 0; y < image.GetHeight(); y++)
	{
		for(int x = 0; x < image.GetWidth(); x++)
		{
			for(int yy = -1; yy <= 1; yy++)
			{
				for(int xx = -1; xx <= 1; xx++)
				{
					if(fabs(image.GetGrayValue(clip(x + xx, 0, int(image.GetWidth() - 1)), clip(y + yy, 0, int(image.GetHeight() - 1))) - image.GetGrayValue(x, y)) >= writeOptions.bluredgethreshold)
						mask.SetBitValue(x, y, true);
				}
			}
		}
	}
}

void Animation::GetBlurredPixel(const Image& image, unsigned int x, unsigned int y, float& red, float& green, float& blue)
{
	red = green = blue = 0.0f;

	for(int yy = -blurMatrixRadius; yy <= blurMatrixRadius; yy++)
	{
		for(int xx = -blurMatrixRadius; xx <= blurMatrixRadius; xx++)
		{
			float scale = blurMatrix[blurMatrixRadius + xx][blurMatrixRadius + yy];
			float r = 0.0f, g = 0.0f, b = 0.0f;

			image.GetRGBValue(clip(int(x) + xx, 0, int(image.GetWidth() - 1)), clip(int(y) + yy, 0, int(image.GetHeight() - 1)), r, g, b);

			red += r * scale;
			green += g * scale;
			blue += b * scale;
		}
	}
}

}