File: roq.cpp

package info (click to toggle)
dhewm3 1.5.1~pre%2Bgit20200905%2Bdfsg-1
  • links: PTS, VCS
  • area: contrib
  • in suites: bullseye
  • size: 21,664 kB
  • sloc: cpp: 408,868; ansic: 1,188; objc: 1,034; python: 330; sh: 94; makefile: 11
file content (859 lines) | stat: -rw-r--r-- 24,349 bytes parent folder | download | duplicates (5)
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
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
/*
===========================================================================

Doom 3 GPL Source Code
Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.

This file is part of the Doom 3 GPL Source Code ("Doom 3 Source Code").

Doom 3 Source Code is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Doom 3 Source Code 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 General Public License for more details.

You should have received a copy of the GNU General Public License
along with Doom 3 Source Code.  If not, see <http://www.gnu.org/licenses/>.

In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code.  If not, please request a copy in writing from id Software at the address below.

If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.

===========================================================================
*/

#include "sys/platform.h"
#include "framework/FileSystem.h"
#include "framework/Session.h"

#include "tools/compilers/roqvq/roq.h"
#include "tools/compilers/roqvq/codec.h"

roq		*theRoQ;				// current roq file

roq::roq( void )
{
	image = 0;
	quietMode = false;
	encoder = 0;
	previousSize = 0;
	lastFrame = false;
	dataStuff=false;
}

roq::~roq( void )
{
	if (image) delete image;
	if (encoder) delete encoder;
	return;
}

void roq::EncodeQuietly( bool which )
{
	quietMode = which;
}

bool roq::IsQuiet( void )
{
	return quietMode;
}

bool roq::IsLastFrame( void )
{
	return lastFrame;
}

bool roq::Scaleable( void )
{
	return paramFile->IsScaleable();
}

bool roq::ParamNoAlpha( void )
{
	return paramFile->NoAlpha();
}

bool roq::MakingVideo( void )
{
	return true;	//paramFile->timecode];
}

bool roq::SearchType( void )
{
	return	paramFile->SearchType();
}

bool roq::HasSound( void )
{
	return	paramFile->HasSound();
}

int roq::PreviousFrameSize( void )
{
	return	previousSize;
}

int roq::FirstFrameSize( void )
{
	return paramFile->FirstFrameSize();
}

int roq::NormalFrameSize( void )
{
	return	paramFile->NormalFrameSize();
}

const char * roq::CurrentFilename( void )
{
	return currentFile.c_str();
}

void roq::EncodeStream( const char *paramInputFile )
{
	int		onFrame;
	idStr	f0, f1, f2;
	int		morestuff;

	onFrame = 1;

	encoder = new codec;
	paramFile = new roqParam;
	paramFile->numInputFiles = 0;

	paramFile->InitFromFile( paramInputFile );

	if (!paramFile->NumberOfFrames()) {
		return;
	}

	InitRoQFile( paramFile->outputFilename);

	numberOfFrames = paramFile->NumberOfFrames();

	if (paramFile->NoAlpha()==true) common->Printf("encodeStream: eluding alpha\n");

	f0 = "";
	f1 = paramFile->GetNextImageFilename();
	if (( paramFile->MoreFrames() == true )) {
		f2 = paramFile->GetNextImageFilename();
	}
	morestuff = numberOfFrames;

	while( morestuff ) {
		LoadAndDisplayImage( f1 );

		if (onFrame==1) {
			encoder->SparseEncode();
//			WriteLossless();
		} else {
			if (!strcmp( f0, f1 ) && strcmp( f1, f2) ) {
				WriteHangFrame();
			} else {
				encoder->SparseEncode();
			}
		}

		onFrame++;
		f0 = f1;
		f1 = f2;
		if (paramFile->MoreFrames() == true) {
			f2 = paramFile->GetNextImageFilename();
		}
		morestuff--;
		session->UpdateScreen();
	}

//	if (numberOfFrames != 1) {
//		if (image->hasAlpha() && paramFile->NoAlpha()==false) {
//			lastFrame = true;
//			encoder->SparseEncode();
//		} else {
//			WriteLossless();
//		}
//	}
	CloseRoQFile();
}

void roq::Write16Word( word *aWord, idFile *stream )
{
	byte	a, b;

	a = *aWord & 0xff;
	b = *aWord >> 8;

	stream->Write( &a, 1 );
	stream->Write( &b, 1 );
}

void roq::Write32Word( unsigned int *aWord, idFile *stream )
{
	byte	a, b, c, d;

	a = *aWord & 0xff;
	b = (*aWord >> 8) & 0xff;
	c = (*aWord >> 16) & 0xff;
	d = (*aWord >> 24) & 0xff;

	stream->Write( &a, 1 );
	stream->Write( &b, 1 );
	stream->Write( &c, 1 );
	stream->Write( &d, 1 );
}

int roq::SizeFile( idFile *ftosize )
{
	return ftosize->Length();
}

#if 0
/* Expanded data destination object for stdio output */

typedef struct {
  struct jpeg_destination_mgr pub; /* public fields */

  byte* outfile;		/* target stream */
  int	size;
} my_destination_mgr;

typedef my_destination_mgr * my_dest_ptr;


/*
 * Initialize destination --- called by jpeg_start_compress
 * before any data is actually written.
 */

void roq::JPEGInitDestination (j_compress_ptr cinfo) {
  my_dest_ptr dest = (my_dest_ptr) cinfo->dest;

  dest->pub.next_output_byte = dest->outfile;
  dest->pub.free_in_buffer = dest->size;
}


/*
 * Empty the output buffer --- called whenever buffer fills up.
 *
 * In typical applications, this should write the entire output buffer
 * (ignoring the current state of next_output_byte & free_in_buffer),
 * reset the pointer & count to the start of the buffer, and return true
 * indicating that the buffer has been dumped.
 *
 * In applications that need to be able to suspend compression due to output
 * overrun, a FALSE return indicates that the buffer cannot be emptied now.
 * In this situation, the compressor will return to its caller (possibly with
 * an indication that it has not accepted all the supplied scanlines).  The
 * application should resume compression after it has made more room in the
 * output buffer.  Note that there are substantial restrictions on the use of
 * suspension --- see the documentation.
 *
 * When suspending, the compressor will back up to a convenient restart point
 * (typically the start of the current MCU). next_output_byte & free_in_buffer
 * indicate where the restart point will be if the current call returns FALSE.
 * Data beyond this point will be regenerated after resumption, so do not
 * write it out when emptying the buffer externally.
 */

boolean roq::JPEGEmptyOutputBuffer (j_compress_ptr cinfo) {
  return true;
}


/*
 * Compression initialization.
 * Before calling this, all parameters and a data destination must be set up.
 *
 * We require a write_all_tables parameter as a failsafe check when writing
 * multiple datastreams from the same compression object.  Since prior runs
 * will have left all the tables marked sent_table=true, a subsequent run
 * would emit an abbreviated stream (no tables) by default.  This may be what
 * is wanted, but for safety's sake it should not be the default behavior:
 * programmers should have to make a deliberate choice to emit abbreviated
 * images.  Therefore the documentation and examples should encourage people
 * to pass write_all_tables=true; then it will take active thought to do the
 * wrong thing.
 */

void roq::JPEGStartCompress (j_compress_ptr cinfo, bool write_all_tables) {
  if (cinfo->global_state != CSTATE_START)
	ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);

  if (write_all_tables)
	jpeg_suppress_tables(cinfo, FALSE);	/* mark all tables to be written */

  /* (Re)initialize error mgr and destination modules */
  (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
  (*cinfo->dest->init_destination) (cinfo);
  /* Perform master selection of active modules */
  jinit_compress_master(cinfo);
  /* Set up for the first pass */
  (*cinfo->master->prepare_for_pass) (cinfo);
  /* Ready for application to drive first pass through jpeg_write_scanlines
   * or jpeg_write_raw_data.
   */
  cinfo->next_scanline = 0;
  cinfo->global_state = (cinfo->raw_data_in ? CSTATE_RAW_OK : CSTATE_SCANNING);
}


/*
 * Write some scanlines of data to the JPEG compressor.
 *
 * The return value will be the number of lines actually written.
 * This should be less than the supplied num_lines only in case that
 * the data destination module has requested suspension of the compressor,
 * or if more than image_height scanlines are passed in.
 *
 * Note: we warn about excess calls to jpeg_write_scanlines() since
 * this likely signals an application programmer error.  However,
 * excess scanlines passed in the last valid call are *silently* ignored,
 * so that the application need not adjust num_lines for end-of-image
 * when using a multiple-scanline buffer.
 */

JDIMENSION roq::JPEGWriteScanlines (j_compress_ptr cinfo, JSAMPARRAY scanlines, JDIMENSION num_lines) {
  JDIMENSION row_ctr, rows_left;

  if (cinfo->global_state != CSTATE_SCANNING)
	ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  if (cinfo->next_scanline >= cinfo->image_height)
	WARNMS(cinfo, JWRN_TOO_MUCH_DATA);

  /* Call progress monitor hook if present */
  if (cinfo->progress != NULL) {
	cinfo->progress->pass_counter = (long) cinfo->next_scanline;
	cinfo->progress->pass_limit = (long) cinfo->image_height;
	(*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
  }

  /* Give master control module another chance if this is first call to
   * jpeg_write_scanlines.  This lets output of the frame/scan headers be
   * delayed so that application can write COM, etc, markers between
   * jpeg_start_compress and jpeg_write_scanlines.
   */
  if (cinfo->master->call_pass_startup)
	(*cinfo->master->pass_startup) (cinfo);

  /* Ignore any extra scanlines at bottom of image. */
  rows_left = cinfo->image_height - cinfo->next_scanline;
  if (num_lines > rows_left)
	num_lines = rows_left;

  row_ctr = 0;
  (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, num_lines);
  cinfo->next_scanline += row_ctr;
  return row_ctr;
}

/*
 * Terminate destination --- called by jpeg_finish_compress
 * after all data has been written.  Usually needs to flush buffer.
 *
 * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
 * application must deal with any cleanup that should happen even
 * for error exit.
 */

static int hackSize;

void roq::JPEGTermDestination (j_compress_ptr cinfo) {
  my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
  size_t datacount = dest->size - dest->pub.free_in_buffer;
  hackSize = datacount;
}


/*
 * Prepare for output to a stdio stream.
 * The caller must have already opened the stream, and is responsible
 * for closing it after finishing compression.
 */

void roq::JPEGDest (j_compress_ptr cinfo, byte* outfile, int size) {
  my_dest_ptr dest;

  /* The destination object is made permanent so that multiple JPEG images
   * can be written to the same file without re-executing jpeg_stdio_dest.
   * This makes it dangerous to use this manager and a different destination
   * manager serially with the same JPEG object, because their private object
   * sizes may be different.  Caveat programmer.
   */
  if (cinfo->dest == NULL) {	/* first time for this JPEG object? */
	cinfo->dest = (struct jpeg_destination_mgr *)
	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
				  sizeof(my_destination_mgr));
  }

  dest = (my_dest_ptr) cinfo->dest;
  dest->pub.init_destination = JPEGInitDestination;
  dest->pub.empty_output_buffer = JPEGEmptyOutputBuffer;
  dest->pub.term_destination = JPEGTermDestination;
  dest->outfile = outfile;
  dest->size = size;
}

void roq::WriteLossless( void ) {

	word direct;
	uint directdw;

	if (!dataStuff) {
		InitRoQPatterns();
		dataStuff=true;
	}
	direct = RoQ_QUAD_JPEG;
	Write16Word( &direct, RoQFile);

	/* This struct contains the JPEG compression parameters and pointers to
	* working space (which is allocated as needed by the JPEG library).
	* It is possible to have several such structures, representing multiple
	* compression/decompression processes, in existence at once.  We refer
	* to any one struct (and its associated working data) as a "JPEG object".
	*/
	struct jpeg_compress_struct cinfo;
	/* This struct represents a JPEG error handler.  It is declared separately
	* because applications often want to supply a specialized error handler
	* (see the second half of this file for an example).  But here we just
	* take the easy way out and use the standard error handler, which will
	* print a message on stderr and call exit() if compression fails.
	* Note that this struct must live as long as the main JPEG parameter
	* struct, to avoid dangling-pointer problems.
	*/
	struct jpeg_error_mgr jerr;
	/* More stuff */
	JSAMPROW row_pointer[1];	/* pointer to JSAMPLE row[s] */
	int row_stride;		/* physical row width in image buffer */
	byte *out;

	/* Step 1: allocate and initialize JPEG compression object */

	/* We have to set up the error handler first, in case the initialization
	* step fails.  (Unlikely, but it could happen if you are out of memory.)
	* This routine fills in the contents of struct jerr, and returns jerr's
	* address which we place into the link field in cinfo.
	*/
	cinfo.err = jpeg_std_error(&jerr);
	/* Now we can initialize the JPEG compression object. */
	jpeg_create_compress(&cinfo);

	/* Step 2: specify data destination (eg, a file) */
	/* Note: steps 2 and 3 can be done in either order. */

	/* Here we use the library-supplied code to send compressed data to a
	* stdio stream.  You can also write your own code to do something else.
	* VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
	* requires it in order to write binary files.
	*/
	out = (byte *)Mem_Alloc(image->pixelsWide()*image->pixelsHigh()*4);
	JPEGDest(&cinfo, out, image->pixelsWide()*image->pixelsHigh()*4);

	/* Step 3: set parameters for compression */

	/* First we supply a description of the input image.
	* Four fields of the cinfo struct must be filled in:
	*/
	cinfo.image_width = image->pixelsWide();	/* image width and height, in pixels */
	cinfo.image_height = image->pixelsHigh();
	cinfo.input_components = 4;		/* # of color components per pixel */
	cinfo.in_color_space = JCS_RGB;	/* colorspace of input image */
	/* Now use the library's routine to set default compression parameters.
	* (You must set at least cinfo.in_color_space before calling this,
	* since the defaults depend on the source color space.)
	*/
	jpeg_set_defaults(&cinfo);
	/* Now you can set any non-default parameters you wish to.
	* Here we just illustrate the use of quality (quantization table) scaling:
	*/
	jpeg_set_quality(&cinfo, paramFile->JpegQuality(), true /* limit to baseline-JPEG values */);

	/* Step 4: Start compressor */

	/* true ensures that we will write a complete interchange-JPEG file.
	* Pass true unless you are very sure of what you're doing.
	*/
	JPEGStartCompress(&cinfo, true);

	/* Step 5: while (scan lines remain to be written) */
	/*           jpeg_write_scanlines(...); */

	/* Here we use the library's state variable cinfo.next_scanline as the
	* loop counter, so that we don't have to keep track ourselves.
	* To keep things simple, we pass one scanline per call; you can pass
	* more if you wish, though.
	*/
	row_stride = image->pixelsWide() * 4;	/* JSAMPLEs per row in image_buffer */

	byte *pixbuf = image->bitmapData();
	while (cinfo.next_scanline < cinfo.image_height) {
		/* jpeg_write_scanlines expects an array of pointers to scanlines.
		 * Here the array is only one element long, but you could pass
		 * more than one scanline at a time if that's more convenient.
		 */
		row_pointer[0] = &pixbuf[((cinfo.image_height-1)*row_stride)-cinfo.next_scanline * row_stride];
		(void) JPEGWriteScanlines(&cinfo, row_pointer, 1);
	}

	/* Step 6: Finish compression */

	jpeg_finish_compress(&cinfo);
	/* After finish_compress, we can close the output file. */

	directdw = hackSize;
	common->Printf("writeLossless: writing %d bytes to RoQ_QUAD_JPEG\n", hackSize);
	Write32Word( &directdw, RoQFile );
	direct = 0;		// flags
	Write16Word( &direct, RoQFile );

	RoQFile->Write( out, hackSize );
	Mem_Free(out);

	/* Step 7: release JPEG compression object */

	/* This is an important step since it will release a good deal of memory. */
	jpeg_destroy_compress(&cinfo);

	/* And we're done! */
	encoder->SetPreviousImage( "first frame", image );
}
#endif

void roq::InitRoQFile( const char *RoQFilename )
{
	word i;
	static int finit = 0;

	if (!finit) {
		finit++;
		common->Printf("initRoQFile: %s\n", RoQFilename);
		RoQFile = fileSystem->OpenFileWrite( RoQFilename );
//		chmod(RoQFilename, S_IREAD|S_IWRITE|S_ISUID|S_ISGID|0070|0007 );
		if ( !RoQFile ) {
			common->Error("Unable to open output file %s.\n", RoQFilename);
		}

		i = RoQ_ID;
		Write16Word( &i, RoQFile );

		i = 0xffff;
		Write16Word( &i, RoQFile );
		Write16Word( &i, RoQFile );

		// to retain exact file format write out 32 for new roq's
		// on loading this will be noted and converted to 1000 / 30
		// as with any new sound dump avi demos we need to playback
		// at the speed the sound engine dumps the audio
		i = 30;						// framerate
		Write16Word( &i, RoQFile );
	}
	roqOutfile = RoQFilename;
}

void roq::InitRoQPatterns( void )
{
uint j;
word direct;

	direct = RoQ_QUAD_INFO;
	Write16Word( &direct, RoQFile );

	j = 8;

	Write32Word( &j, RoQFile );
	common->Printf("initRoQPatterns: outputting %d bytes to RoQ_INFO\n", j);
	direct = image->hasAlpha();
	if (ParamNoAlpha() == true) direct = 0;

	Write16Word( &direct, RoQFile );

	direct = image->pixelsWide();
	Write16Word( &direct, RoQFile );
	direct = image->pixelsHigh();
	Write16Word( &direct, RoQFile );
	direct = 8;
	Write16Word( &direct, RoQFile );
	direct = 4;
	Write16Word( &direct, RoQFile );
}

void roq::CloseRoQFile( void )
{
	common->Printf("closeRoQFile: closing RoQ file\n");
	fileSystem->CloseFile( RoQFile );
}

void roq::WriteHangFrame( void )
{
uint j;
word direct;
	common->Printf("*******************************************************************\n");
	direct = RoQ_QUAD_HANG;
	Write16Word( &direct, RoQFile);
	j = 0;
	Write32Word( &j, RoQFile);
	direct = 0;
	Write16Word( &direct, RoQFile);
}

void roq::WriteCodeBookToStream( byte *codebook, int csize, word cflags )
{
uint j;
word direct;

	if (!csize) {
		common->Printf("writeCodeBook: false VQ DATA!!!!\n");
		return;
	}

	direct = RoQ_QUAD_CODEBOOK;

	Write16Word( &direct, RoQFile);

	j = csize;

	Write32Word( &j, RoQFile);
	common->Printf("writeCodeBook: outputting %d bytes to RoQ_QUAD_CODEBOOK\n", j);

	direct = cflags;
	Write16Word( &direct, RoQFile);

	RoQFile->Write( codebook, j );
}

void roq::WriteCodeBook( byte *codebook )
{
	memcpy( codes, codebook, 4096 );
}

void roq::WriteFrame( quadcel *pquad )
{
word action, direct;
int	onCCC, onAction, i, code;
uint j;
byte *cccList;
bool *use2, *use4;
int dx,dy,dxMean,dyMean,index2[256],index4[256], dimension;

	cccList = (byte *)Mem_Alloc( numQuadCels * 8);					// maximum length
	use2 = (bool *)Mem_Alloc(256*sizeof(bool));
	use4 = (bool *)Mem_Alloc(256*sizeof(bool));

	for(i=0;i<256;i++) {
		use2[i] = false;
		use4[i] = false;
	}

	action = 0;
	j = onAction = 0;
	onCCC = 2;											// onAction going to go at zero

	dxMean = encoder->MotMeanX();
	dyMean = encoder->MotMeanY();

	if (image->hasAlpha()) dimension = 10; else dimension = 6;

	for (i=0; i<numQuadCels; i++) {
	if ( pquad[i].size && pquad[i].size < 16 ) {
		switch( pquad[i].status ) {
			case	SLD:
				use4[pquad[i].patten[0]] = true;
				use2[codes[dimension*256+(pquad[i].patten[0]*4)+0]] = true;
				use2[codes[dimension*256+(pquad[i].patten[0]*4)+1]] = true;
				use2[codes[dimension*256+(pquad[i].patten[0]*4)+2]] = true;
				use2[codes[dimension*256+(pquad[i].patten[0]*4)+3]] = true;
				break;
			case	PAT:
				use4[pquad[i].patten[0]] = true;
				use2[codes[dimension*256+(pquad[i].patten[0]*4)+0]] = true;
				use2[codes[dimension*256+(pquad[i].patten[0]*4)+1]] = true;
				use2[codes[dimension*256+(pquad[i].patten[0]*4)+2]] = true;
				use2[codes[dimension*256+(pquad[i].patten[0]*4)+3]] = true;
				break;
			case	CCC:
				use2[pquad[i].patten[1]] = true;
				use2[pquad[i].patten[2]] = true;
				use2[pquad[i].patten[3]] = true;
				use2[pquad[i].patten[4]] = true;
		}
	}
	}

	if (!dataStuff) {
		dataStuff=true;
		InitRoQPatterns();
		if (image->hasAlpha()) i = 3584; else i = 2560;
		WriteCodeBookToStream( codes, i, 0 );
		for(i=0;i<256;i++) {
			index2[i] = i;
			index4[i] = i;
		}
	} else {
		j = 0;
		for(i=0;i<256;i++) {
			if (use2[i]) {
				index2[i] = j;
				for(dx=0;dx<dimension;dx++) cccList[j*dimension+dx] = codes[i*dimension+dx];
				j++;
			}
		}
		code = j*dimension;
		direct = j;
		common->Printf("writeFrame: really used %d 2x2 cels\n", j);
		j = 0;
		for(i=0;i<256;i++) {
			if (use4[i]) {
				index4[i] = j;
				for(dx=0;dx<4;dx++) cccList[j*4+code+dx] = index2[codes[i*4+(dimension*256)+dx]];
				j++;
			}
		}
		code += j*4;
		direct = (direct<<8) + j;
		common->Printf("writeFrame: really used %d 4x4 cels\n", j);
		if (image->hasAlpha()) i = 3584; else i = 2560;
		if ( code == i || j == 256) {
			WriteCodeBookToStream( codes, i, 0 );
		} else {
			WriteCodeBookToStream( cccList, code, direct );
		}
	}

	action = 0;
	j = onAction = 0;

	for (i=0; i<numQuadCels; i++) {
	if ( pquad[i].size && pquad[i].size < 16 ) {
		code = -1;
		switch( pquad[i].status ) {
			case	DEP:
				code = 3;
				break;
			case	SLD:
				code = 2;
				cccList[onCCC++] = index4[pquad[i].patten[0]];
				break;
			case	MOT:
				code = 0;
				break;
			case	FCC:
				code = 1;
				dx = ((pquad[i].domain >> 8  )) - 128 - dxMean + 8;
				dy = ((pquad[i].domain & 0xff)) - 128 - dyMean + 8;
				if (dx>15 || dx<0 || dy>15 || dy<0 ) {
					common->Error("writeFrame: FCC error %d,%d mean %d,%d at %d,%d,%d rmse %f\n", dx,dy, dxMean, dyMean,pquad[i].xat,pquad[i].yat,pquad[i].size, pquad[i].snr[FCC] );
				}
				cccList[onCCC++] = (dx<<4)+dy;
				break;
			case	PAT:
				code = 2;
				cccList[onCCC++] = index4[pquad[i].patten[0]];
				break;
			case	CCC:
				code = 3;
				cccList[onCCC++] = index2[pquad[i].patten[1]];
				cccList[onCCC++] = index2[pquad[i].patten[2]];
				cccList[onCCC++] = index2[pquad[i].patten[3]];
				cccList[onCCC++] = index2[pquad[i].patten[4]];
				break;
			case	DEAD:
				common->Error("dead cels in picture\n");
				break;
		}
		if (code == -1) {
			common->Error( "writeFrame: an error occurred writing the frame\n");
		}

		action = (action<<2)|code;
		j++;
		if (j == 8) {
			j = 0;
			cccList[onAction+0] = (action & 0xff);
			cccList[onAction+1] = ((action >> 8) & 0xff);
			onAction = onCCC;
			onCCC += 2;
		}
	}
	}

	if (j) {
		action <<= ((8-j)*2);
		cccList[onAction+0] = (action & 0xff);
		cccList[onAction+1] = ((action >> 8) & 0xff);
	}

	direct = RoQ_QUAD_VQ;

	Write16Word( &direct, RoQFile);

	j = onCCC;
	Write32Word( &j, RoQFile);

	direct  = dyMean;
	direct &= 0xff;
	direct += (dxMean<<8);		// flags

	Write16Word( &direct, RoQFile);

	common->Printf("writeFrame: outputting %d bytes to RoQ_QUAD_VQ\n", j);

	previousSize = j;

	RoQFile->Write( cccList, onCCC );

	Mem_Free( cccList );
	Mem_Free( use2 );
	Mem_Free( use4 );
}

//
// load a frame, create a window (if neccesary) and display the frame
//
void roq::LoadAndDisplayImage( const char * filename )
{
	if (image) delete image;

	common->Printf("loadAndDisplayImage: %s\n", filename);

	currentFile = filename;

	image = new NSBitmapImageRep( filename );

	numQuadCels  = ((image->pixelsWide() & 0xfff0)*(image->pixelsHigh() & 0xfff0))/(MINSIZE*MINSIZE);
	numQuadCels += numQuadCels/4 + numQuadCels/16;

//	if (paramFile->deltaFrames] == true && cleared == false && [image isPlanar] == false) {
//		cleared = true;
//		imageData = [image data];
//		memset( imageData, 0, image->pixelsWide()*image->pixelsHigh()*[image samplesPerPixel]);
//	}

	if (!quietMode) common->Printf("loadAndDisplayImage: %dx%d\n", image->pixelsWide(), image->pixelsHigh());
}

void roq::MarkQuadx( int xat, int yat, int size, float cerror, int choice ) {
}

NSBitmapImageRep* roq::CurrentImage( void )
{
	return	image;
}

int roq::NumberOfFrames( void ) {
	return numberOfFrames;
}

void RoQFileEncode_f( const idCmdArgs &args ) {
	if ( args.Argc() != 2 ) {
		common->Printf( "Usage: roq <paramfile>\n" );
		return;
	}
	theRoQ = new roq;
	int		startMsec = Sys_Milliseconds();
	theRoQ->EncodeStream( args.Argv( 1 ) );
	int		stopMsec = Sys_Milliseconds();
	common->Printf( "total encoding time: %i second\n", ( stopMsec - startMsec ) / 1000 );

}