~ubuntu-branches/ubuntu/karmic/mjpegtools/karmic

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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
#ifndef __REFERENCE_FRAME_H__
#define __REFERENCE_FRAME_H__

// This file (C) 2004 Steven Boswell.  All rights reserved.
// Released to the public under the GNU General Public License.
// See the file COPYING for more information.

#include "config.h"
#include <assert.h>
#include "mjpeg_types.h"
#include "Status_t.h"
#include "TemplateLib.hh"

// Definitions for pixels, reference pixels, pixel allocators, and
// reference frames.



// A generic pixel.
// Provide the numeric type used to store pixel values, the dimension of
// the pixel type, and the numeric type to use in tolerance
// calculations.
template <class NUM, int DIM, class TOL>
class Pixel
{
public:
	typedef NUM Num_t;
		// The numeric type used to store pixel values.

	Pixel() {}
		// Default constructor.

	Pixel (const NUM a_atnVal[DIM]);
		// Initializing constructor.

	Pixel (const Pixel<NUM,DIM,TOL> &a_rOther);
		// Copy constructor.

	void MakeInvalid (void);
		// Make the pixel invalid.

	bool IsPixelInvalid (void) const;
		// Return whether the pixel is invalid.

	NUM &operator[] (int a_nDim) { return m_atnVal[a_nDim]; }
	NUM operator[] (int a_nDim) const { return m_atnVal[a_nDim]; }
		// Get the value of this pixel.

	static TOL MakeTolerance (NUM a_tnTolerance);
		// Turn an integer tolerance value into what's appropriate for
		// the pixel type.
	
	bool IsWithinTolerance (const Pixel<NUM,DIM,TOL> &a_rOther,
			TOL a_tnTolerance) const;
		// Return true if the two pixels are within the specified
		// tolerance.
		// a_tnTolerance must have been previously retrieved from
		// MakeTolerance().
	
	bool IsWithinTolerance (const Pixel<NUM,DIM,TOL> &a_rOther,
			TOL a_tnTolerance, TOL &a_rtnSAD) const;
		// Return true if the two pixels are within the specified
		// tolerance, and backpatch the sample-array-difference.
		// a_tnTolerance must have been previously retrieved from
		// MakeTolerance().

private:
	NUM m_atnVal[DIM];
		// The pixel value.
};



// Pixel types we use.
typedef Pixel<uint8_t,1,int32_t> PixelY;
typedef Pixel<uint8_t,2,int32_t> PixelCbCr;



// A reference pixel.  Used to accumulate values across several frames
// for any pixel deemed, via motion detection, to actually be the same
// pixel.
// Provide the numeric type for accumulated pixels (and tolerance
// calculations), the numeric type for pixels, the dimension of the
// pixel, and the class used to implement pixels.
template <class ACCUM_NUM, class PIXEL_NUM, int DIM,
	class PIXEL = Pixel<PIXEL_NUM,DIM,ACCUM_NUM> >
class ReferencePixel
{
public:
	typedef PIXEL Pixel_t;
		// Our pixel type.

	typedef ACCUM_NUM Tolerance_t;
		// Our tolerance value type.

	ReferencePixel();
		// Default constructor.

	~ReferencePixel();
		// Destructor.

	void Reset (void);
		// Reset ourselves, so that we may refer to a new pixel.

	void AddSample (const PIXEL &a_rPixel);
		// Incorporate another sample.

	const PIXEL &GetValue (void);
		// Return this pixel's value.

	void AddFrameReference (void) { ++m_nFrameReferences; }
		// Add another reference from a frame.

	void RemoveFrameReference (void) { --m_nFrameReferences; }
		// Remove an existing reference from a frame.
		// (Once all references are removed, the pixel is implicitly
		// unallocated.)

	int16_t GetFrameReferences (void) const
			{ return m_nFrameReferences; }
		// Return the number of frames that refer to this pixel.

private:
	ACCUM_NUM m_atSum[DIM];
		// The sum of the values of all pixels we've incorporated.

	ACCUM_NUM m_tCount;
		// The number of pixels we've incorporated.

	int16_t m_nFrameReferences;
		// The number of reference-frames that make use of this pixel.
		// A value of 0 means the pixel is not in use & therefore can
		// be allocated.

	PIXEL m_oPixel;
		// The value of this pixel, i.e. m_nSumY / m_nYCount.
		// Calculated on demand.
};



// Reference-pixel types we use.
typedef ReferencePixel<uint16_t,uint8_t,1,PixelY> ReferencePixelY;
typedef ReferencePixel<uint16_t,uint8_t,2,PixelCbCr> ReferencePixelCbCr;



// A class to allocate reference pixels.
// Parameterized by the type of reference pixel, and a numeric type that
// can hold the largest number of reference pixels to be allocated (i.e.
// big enough to hold the product of the frame's width & height).
template <class REFERENCEPIXEL, class FRAMESIZE>
class PixelAllocator
{
public:
	PixelAllocator();
		// Default constructor.

	~PixelAllocator();
		// Destructor.

	void Initialize (Status_t &a_reStatus, FRAMESIZE a_tnCount);
		// Initialize the pool to contain the given number of pixels.
		// This must be greater than or equal to the number of pixels
		// that can ever be in use at one time.

	REFERENCEPIXEL *Allocate (void);
		// Allocate another pixel.
		// (Note that not having a free pixel to allocate indicates
		// that the a_nCount parameter to Initialize() needed to be
		// bigger.)

private:
	FRAMESIZE m_tnCount;
		// The number of reference pixels in our pool.

	FRAMESIZE m_tnNext;
		// The index of the next pixel to try to allocate.
		// A pixel is considered unallocated if it has a zero
		// reference count.

	REFERENCEPIXEL *m_pPixels;
		// The pool of pixels.
};



// A reference frame.  Points to reference pixels, which may be shared
// across several reference frames, depending on what motion-detection
// determines about the pixel's lifetime.  Parameterized by the type of
// reference pixels to use, the numeric type to use for pixel indices,
// and a numeric type big enough to hold the product of the largest
// expected width & height.
template <class REFERENCEPIXEL, class PIXELINDEX, class FRAMESIZE>
class ReferenceFrame
{
public:
	ReferenceFrame (Status_t &a_reStatus, PIXELINDEX a_tnWidth,
			PIXELINDEX a_tnHeight);
		// Initializing constructor.

	REFERENCEPIXEL *GetPixel (PIXELINDEX a_tnX, PIXELINDEX a_tnY) const;
		// Get the pixel at this index (which may be NULL).

	REFERENCEPIXEL *GetPixel (FRAMESIZE a_tnI) const;
		// Get the pixel at this offset (which may be NULL).

	void SetPixel (PIXELINDEX a_tnX, PIXELINDEX a_tnY,
			REFERENCEPIXEL *a_pPixel);
		// Set the pixel at this index.  a_pPixel may be NULL.

	void SetPixel (FRAMESIZE a_tnI, REFERENCEPIXEL *a_pPixel);
		// Set the pixel at this offset.  a_pPixel may be NULL.

	void Reset (void);
		// Reset the frame, i.e. set all the pixels to NULL.

private:
	PIXELINDEX m_tnWidth, m_tnHeight;
		// The dimensions of the frame.

	REFERENCEPIXEL **m_ppPixels;
		// The reference pixels that make up this frame.
		// The contained pointers may be NULL, to mark pixels that still
		// need to be resolved.
};



// Reference-frame types we use.
typedef ReferenceFrame<ReferencePixelY, int16_t, int32_t>
	ReferenceFrameY;
typedef ReferenceFrame<ReferencePixelCbCr, int16_t, int32_t>
	ReferenceFrameCbCr;



// Initializing constructor.
template <class NUM, int DIM, class TOL>
Pixel<NUM,DIM,TOL>::Pixel (const NUM a_atnVal[DIM])
{
	// Store the values.
	for (int i = 0; i < DIM; ++i)
		m_atnVal[i] = a_atnVal[i];
}



// Copy constructor.
template <class NUM, int DIM, class TOL>
Pixel<NUM,DIM,TOL>::Pixel (const Pixel<NUM,DIM,TOL> &a_rOther)
{
	// Copy the values.
	for (int i = 0; i < DIM; ++i)
		m_atnVal[i] = a_rOther.m_atnVal[i];
}



// Make the pixel invalid.
template <class NUM, int DIM, class TOL>
void
Pixel<NUM,DIM,TOL>::MakeInvalid (void)
{
	// 0 isn't a valid value for pixels.
	m_atnVal[0] = 0;
}



// Return whether the pixel is invalid.
template <class NUM, int DIM, class TOL>
inline bool
Pixel<NUM,DIM,TOL>::IsPixelInvalid (void) const
{
	// Return whether the first pixel value is invalid.
	return m_atnVal[0] == 0;
}



// Turn an integer tolerance value into what's appropriate for
// the pixel type.
template <class NUM, int DIM, class TOL>
TOL
Pixel<NUM,DIM,TOL>::MakeTolerance (NUM a_tnTolerance)
{
	// The default is to just use the given number.
	return TOL (a_tnTolerance);
}



#if 0

// This is what I mean, but C++ templates can't do this.

// Turn an integer tolerance value into what's appropriate for
// the pixel type.
template <class NUM, class TOL>
TOL
Pixel<NUM,1,TOL>::MakeTolerance (NUM a_tnTolerance)
{
	// For a one-dimensional pixel, just use the given number.
	return TOL (a_tnTolerance);
}

#endif



// Turn an integer tolerance value into what's appropriate for
// the pixel type.
template <>
int32_t
PixelY::MakeTolerance (uint8_t a_tnTolerance)
{
	// For a one-dimensional pixel, just use the given number.
	return int32_t (a_tnTolerance);
}



#if 0

// This is what I mean, but C++ templates can't do this.

// Turn an integer tolerance value into what's appropriate for
// the pixel type.
template <class NUM, class TOL>
TOL
Pixel<NUM,2,TOL>::MakeTolerance (NUM a_tnTolerance)
{
	// For a two-dimensional pixel, use the square of the given number.
	return TOL (a_tnTolerance) * TOL (a_tnTolerance);
}

#endif



// Turn an integer tolerance value into what's appropriate for
// the pixel type.
template <>
int32_t
PixelCbCr::MakeTolerance (uint8_t a_tnTolerance)
{
	// For a two-dimensional pixel, use the square of the given number.
	return int32_t (a_tnTolerance) * int32_t (a_tnTolerance);
}

	

#if 0

// This is what I mean, but C++ templates can't do this.

// Return true if the two pixels are within the specified tolerance.
template <class NUM, class TOL>
bool
Pixel<NUM,1,TOL>::IsWithinTolerance
	(const Pixel<NUM,1,TOL> &a_rOther, TOL a_tnTolerance) const
{
	// Check to see if the absolute value of the difference between
	// the two pixels is within our tolerance value.
	return AbsoluteValue (TOL (m_atnVal[0])
		- TOL (a_rOther.m_atnVal[0])) <= a_tnTolerance;
}

#endif



// Return true if the two pixels are within the specified tolerance.
template <>
bool
PixelY::IsWithinTolerance (const PixelY &a_rOther,
	int32_t a_tnTolerance) const
{
	// Check to see if the absolute value of the difference between
	// the two pixels is within our tolerance value.
	return AbsoluteValue (int32_t (m_atnVal[0])
		- int32_t (a_rOther.m_atnVal[0])) <= a_tnTolerance;
}



// Return true if the two pixels are within the specified tolerance.
template <>
bool
PixelY::IsWithinTolerance (const PixelY &a_rOther,
	int32_t a_tnTolerance, int32_t &a_rtnSAD) const
{
	// Check to see if the absolute value of the difference between
	// the two pixels is within our tolerance value.
	a_rtnSAD = AbsoluteValue (int32_t (m_atnVal[0])
		- int32_t (a_rOther.m_atnVal[0]));
	return a_rtnSAD <= a_tnTolerance;
}



#if 0

// This is what I mean, but C++ templates can't do this.

// Return true if the two pixels are within the specified tolerance.
template <class NUM, class TOL>
bool
Pixel<NUM,2,TOL>::IsWithinTolerance
	(const Pixel<NUM,2,TOL> &a_rOther, TOL a_tnTolerance) const
{
	// Calculate the vector difference between the two pixels.
	TOL tnX = TOL (m_atnVal[0]) - TOL (a_rOther.m_atnVal[0]);
	TOL tnY = TOL (m_atnVal[1]) - TOL (a_rOther.m_atnVal[1]);

	// Check to see if the length of the vector difference is within
	// our tolerance value.  (Technically, we check the squares of
	// the values, but that's just as valid & much faster than
	// calculating a square root.)
	return tnX * tnX + tnY * tnY <= a_tnTolerance;
}

#endif



// Return true if the two pixels are within the specified tolerance.
template <>
bool
PixelCbCr::IsWithinTolerance
	(const PixelCbCr &a_rOther, int32_t a_tnTolerance) const
{
	// Calculate the vector difference between the two pixels.
	int32_t tnX = int32_t (m_atnVal[0])
		- int32_t (a_rOther.m_atnVal[0]);
	int32_t tnY = int32_t (m_atnVal[1])
		- int32_t (a_rOther.m_atnVal[1]);

	// Check to see if the length of the vector difference is within
	// our tolerance value.  (Technically, we check the squares of
	// the values, but that's just as valid & much faster than
	// calculating a square root.)
	return tnX * tnX + tnY * tnY <= a_tnTolerance;
}



// Return true if the two pixels are within the specified tolerance.
template <>
bool
PixelCbCr::IsWithinTolerance
	(const PixelCbCr &a_rOther, int32_t a_tnTolerance,
	int32_t &a_rtnSAD) const
{
	// Calculate the vector difference between the two pixels.
	int32_t tnX = int32_t (m_atnVal[0])
		- int32_t (a_rOther.m_atnVal[0]);
	int32_t tnY = int32_t (m_atnVal[1])
		- int32_t (a_rOther.m_atnVal[1]);

	// Calculate the sample-array-difference.
	a_rtnSAD = tnX * tnX + tnY * tnY;

	// Check to see if the length of the vector difference is within
	// our tolerance value.  (Technically, we check the squares of
	// the values, but that's just as valid & much faster than
	// calculating a square root.)
	return a_rtnSAD <= a_tnTolerance;
}



// Default constructor.
template <class ACCUM_NUM, class PIXEL_NUM, int DIM, class PIXEL>
ReferencePixel<ACCUM_NUM,PIXEL_NUM,DIM,PIXEL>::ReferencePixel()
{
	// Reset the pixel value.
	Reset();

	// No references from frames yet.
	m_nFrameReferences = 0;
}



// Destructor.
template <class ACCUM_NUM, class PIXEL_NUM, int DIM, class PIXEL>
ReferencePixel<ACCUM_NUM,PIXEL_NUM,DIM,PIXEL>::~ReferencePixel()
{
	// Make sure no frames refer to us.
	assert (m_nFrameReferences == 0);
}



// Reset ourselves, so that we may refer to a new pixel.
template <class ACCUM_NUM, class PIXEL_NUM, int DIM, class PIXEL>
void
ReferencePixel<ACCUM_NUM,PIXEL_NUM,DIM,PIXEL>::Reset (void)
{
	// Reset the accumulated sum.
	for (int i = 0; i < DIM; i++)
		m_atSum[i] = ACCUM_NUM (0);
	m_tCount = 0;

	// Get rid of any existing pixel value.
	m_oPixel.MakeInvalid();
}



// Incorporate another sample.
template <class ACCUM_NUM, class PIXEL_NUM, int DIM, class PIXEL>
void
ReferencePixel<ACCUM_NUM,PIXEL_NUM,DIM,PIXEL>::AddSample
	(const PIXEL &a_rPixel)
{
	// Make sure this pixel is in use.
	assert (m_nFrameReferences > 0);

	// TODO: detect & handle m_atSum[] overflowing.

	// HACK: do something cheesy to handle overflow.
	if (m_tCount >= 10)
	{
		for (int i = 0; i < DIM; i++)
			m_atSum[i] >>= 1;
		m_tCount >>= 1;
	}

	// Add the value to our accumulated sum.
	for (int i = 0; i < DIM; i++)
		m_atSum[i] += a_rPixel[i];
	++m_tCount;

	// Remember that the pixel value needs to be recalculated.
	m_oPixel.MakeInvalid();
}



// Return the pixel's value.
template <class ACCUM_NUM, class PIXEL_NUM, int DIM, class PIXEL>
const PIXEL &
ReferencePixel<ACCUM_NUM,PIXEL_NUM,DIM,PIXEL>::GetValue (void)
{
	// Make sure this pixel is in use.
	assert (m_nFrameReferences > 0);

	// If we have to recalculate the pixel value, do so.
	if (m_oPixel.IsPixelInvalid())
	{
		static float afDivisors[] = { 0.0f, 1.0f, 1.0f / 2.0f,
				1.0f / 3.0f, 1.0f / 4.0f, 1.0f / 5.0f, 1.0f / 6.0f,
				1.0f / 7.0f, 1.0f / 8.0f, 1.0f / 9.0f, 1.0f / 10.0f };
			// HACK: try to speed this up.

		// Calculate the pixel's value.
		if (m_tCount <= 10)
		{
			for (int i = 0; i < DIM; i++)
				m_oPixel[i] = PIXEL_NUM ((float (m_atSum[i])
					* afDivisors[m_tCount]) + 0.5f);
		}
		else
		{
			for (int i = 0; i < DIM; i++)
				m_oPixel[i] = PIXEL_NUM ((float (m_atSum[i])
					/ float (m_tCount)) + 0.5f);
		}
	}

	// Give our caller the pixel value.
	return m_oPixel;
}



// Default constructor.
template <class REFERENCEPIXEL, class FRAMESIZE>
PixelAllocator<REFERENCEPIXEL,FRAMESIZE>::PixelAllocator()
{
	// No pixels yet.
	m_tnCount = m_tnNext = 0;
	m_pPixels = NULL;
}



// Destructor.
template <class REFERENCEPIXEL, class FRAMESIZE>
PixelAllocator<REFERENCEPIXEL,FRAMESIZE>::~PixelAllocator()
{
	// Free up the pixel pool.  (The pixels themselves will verify that
	// there are no more references to them.)
	delete[] m_pPixels;
}



// Initialize the pool to contain the given number of pixels.
template <class REFERENCEPIXEL, class FRAMESIZE>
void
PixelAllocator<REFERENCEPIXEL,FRAMESIZE>::Initialize
	(Status_t &a_reStatus, FRAMESIZE a_tnCount)
{
	// Make sure they didn't start us off with an error.
	assert (a_reStatus == g_kNoError);

	// Make sure we haven't been initialized already.
	assert (m_pPixels == NULL);

	// Try to allocate a pool of the given number of pixels.
	m_pPixels = new REFERENCEPIXEL[a_tnCount];
	if (m_pPixels == NULL)
	{
		a_reStatus = g_kOutOfMemory;
		return;
	}

	// Remember that we allocated this many pixels.
	m_tnCount = a_tnCount;

	// Allocate the first pixel first.
	m_tnNext = 0;
}



// Allocate another pixel.
template <class REFERENCEPIXEL, class FRAMESIZE>
REFERENCEPIXEL *
PixelAllocator<REFERENCEPIXEL,FRAMESIZE>::Allocate (void)
{
	FRAMESIZE tnOrigNext;
		// The original value of the index of the next pixel to
		// allocate.  Used to detect when we've run through all of
		// them.
	REFERENCEPIXEL *pPixel;
		// The pixel we allocate.

	// Loop through the pixel pool, find an unallocated one, return it
	// to them.
	tnOrigNext = m_tnNext;
	for (;;)
	{
		// Get the next pixel.
		pPixel = m_pPixels + m_tnNext;
		m_tnNext = (m_tnNext + 1) % m_tnCount;

		// If this pixel is unallocated, reset it & return it.
		if (pPixel->GetFrameReferences() == 0)
		{
			pPixel->Reset();
			return pPixel;
		}

		// Make sure we haven't run out of pixels.  (Returning NULL
		// may cause a segmentation fault in our client, but that's
		// slightly better than an infinite loop.)
		if (m_tnNext == tnOrigNext)
		{
			assert (false);
			return NULL;
		}
	}
}



// Initializing constructor.
template <class REFERENCEPIXEL, class PIXELINDEX, class FRAMESIZE>
ReferenceFrame<REFERENCEPIXEL,PIXELINDEX,FRAMESIZE>::ReferenceFrame
	(Status_t &a_reStatus, PIXELINDEX a_tnWidth, PIXELINDEX a_tnHeight)
{
	FRAMESIZE tnPixels;
		// The total number of pixels referred to by this frame.
	FRAMESIZE i;
		// Used to loop through pixel references.

	// Make sure they didn't start us off with an error.
	assert (a_reStatus == g_kNoError);

	// Make sure the height and width are sane.
	assert (a_tnWidth > 0);
	assert (a_tnHeight > 0);

	// Allocate space for the frame's pixels.
	tnPixels = FRAMESIZE (a_tnWidth) * FRAMESIZE (a_tnHeight);
	m_ppPixels = new REFERENCEPIXEL * [tnPixels];
	if (m_ppPixels == NULL)
	{
		a_reStatus = g_kOutOfMemory;
		return;
	}

	// Remember our dimensions.
	m_tnWidth = a_tnWidth;
	m_tnHeight = a_tnHeight;

	// Initially, no pixels are referred to by the frame.
	for (i = 0; i < tnPixels; i++)
		m_ppPixels[i] = NULL;
}



// Get the pixel at this index (which may be NULL).
template <class REFERENCEPIXEL, class PIXELINDEX, class FRAMESIZE>
REFERENCEPIXEL *
ReferenceFrame<REFERENCEPIXEL,PIXELINDEX,FRAMESIZE>::GetPixel
	(PIXELINDEX a_tnX, PIXELINDEX a_tnY) const
{
	// Make sure the indices are within limits.
	assert (a_tnX >= PIXELINDEX (0) && a_tnX < m_tnWidth);
	assert (a_tnY >= PIXELINDEX (0) && a_tnY < m_tnHeight);

	// Easy enough.
	return m_ppPixels[FRAMESIZE (a_tnY) * FRAMESIZE (m_tnWidth)
		+ FRAMESIZE (a_tnX)];
}



// Get the pixel at this offset (which may be NULL).
template <class REFERENCEPIXEL, class PIXELINDEX, class FRAMESIZE>
REFERENCEPIXEL *
ReferenceFrame<REFERENCEPIXEL,PIXELINDEX,FRAMESIZE>::GetPixel
	(FRAMESIZE a_tnI) const
{
	// Make sure the offset is within limits.
	assert (a_tnI >= FRAMESIZE (0)
		&& a_tnI < FRAMESIZE (m_tnWidth) * FRAMESIZE (m_tnHeight));

	// Easy enough.
	return m_ppPixels[a_tnI];
}



// Set the pixel at this index.  a_pPixel may be NULL.
template <class REFERENCEPIXEL, class PIXELINDEX, class FRAMESIZE>
void
ReferenceFrame<REFERENCEPIXEL,PIXELINDEX,FRAMESIZE>::SetPixel
	(PIXELINDEX a_tnX, PIXELINDEX a_tnY, REFERENCEPIXEL *a_pPixel)
{
	// Make sure the indices are within limits.
	assert (a_tnX >= PIXELINDEX (0) && a_tnX < m_tnWidth);
	assert (a_tnY >= PIXELINDEX (0) && a_tnY < m_tnHeight);

	// Get the pixel of interest.
	REFERENCEPIXEL *&rpPixel = m_ppPixels[FRAMESIZE (a_tnY)
		* FRAMESIZE (m_tnWidth) + FRAMESIZE (a_tnX)];

	// If there's a pixel here already, remove our reference to it.
	if (rpPixel != NULL)
		rpPixel->RemoveFrameReference();

	// Store the new pixel here.
	rpPixel = a_pPixel;

	// If they stored a valid pixel, add our reference to it.
	if (rpPixel != NULL)
		rpPixel->AddFrameReference();
}



// Set the pixel at this offset.  a_pPixel may be NULL.
template <class REFERENCEPIXEL, class PIXELINDEX, class FRAMESIZE>
void
ReferenceFrame<REFERENCEPIXEL,PIXELINDEX,FRAMESIZE>::SetPixel
	(FRAMESIZE a_tnI, REFERENCEPIXEL *a_pPixel)
{
	// Make sure the offset is within limits.
	assert (a_tnI >= FRAMESIZE (0)
		&& a_tnI < FRAMESIZE (m_tnWidth) * FRAMESIZE (m_tnHeight));

	// Get the pixel of interest.
	REFERENCEPIXEL *&rpPixel = m_ppPixels[a_tnI];

	// If there's a pixel here already, remove our reference to it.
	if (rpPixel != NULL)
		rpPixel->RemoveFrameReference();

	// Store the new pixel here.
	rpPixel = a_pPixel;

	// If they stored a valid pixel, add our reference to it.
	if (rpPixel != NULL)
		rpPixel->AddFrameReference();
}



// Reset the frame (i.e. set all the pixels to NULL).
template <class REFERENCEPIXEL, class PIXELINDEX, class FRAMESIZE>
void
ReferenceFrame<REFERENCEPIXEL,PIXELINDEX,FRAMESIZE>::Reset (void)
{
	FRAMESIZE tnPixels;
		// The total number of pixels referred to by this frame.
	FRAMESIZE i;
		// Used to loop through pixels.

	// Loop through all pixels, set them to NULL.
	tnPixels = FRAMESIZE (m_tnWidth) * FRAMESIZE (m_tnHeight);
	for (i = 0; i < tnPixels; ++i)
	{
		// If there's a pixel here already, remove our reference to it,
		// then remove the pixel.
		if (m_ppPixels[i] != NULL)
		{
			m_ppPixels[i]->RemoveFrameReference();
			m_ppPixels[i] = NULL;
		}
	}
}



#endif // __REFERENCE_FRAME_H__