File: codec.cc

package info (click to toggle)
xppaut 5.85-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 11,552 kB
  • ctags: 7,725
  • sloc: ansic: 80,633; cpp: 965; makefile: 265
file content (539 lines) | stat: -rw-r--r-- 11,726 bytes parent folder | download | duplicates (3)
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
/*
 *
 * Copyright (C) 1996 by Josh Osborne.
 * All rights reserved.
 *
 * This software may be freely copied, modified and redistributed without
 * fee for non-commerical purposes provided that this copyright notice is
 * preserved intact on all copies and modified copies.
 * 
 * There is no warranty or other guarantee of fitness of this software.
 * It is provided solely "as is". The author(s) disclaim(s) all
 * responsibility and liability with respect to this software's usage
 * or its effect upon hardware, computer systems, other software, or
 * anything else.
 *
 */


#include <iostream.h>
#include <assert.h>
#include "codec.h"
extern "C" {
#include "string.h"
}

codec::codec()
{
	this->cs = NULL;
}

void codec::start(chunkstream *cs, int w, int h, int nframes)
{
	// assert(this->cs == NULL);
	this->cs = cs;
	this->w = w;
	this->h = h;
	this->nframes = nframes;
}

int codec::need_prescan()
{
	return 0;
}

void codec::prescan(ppm *p, int framenum)
{
	// assert(this->need_prescan() == 0);
	// assert(NULL == "prescan called in codec that didn't want it");
	// assert(p == p && framenum == framenum); // Plicate -Wall
}

void codec::done()
{
}

rgb24_strf::rgb24_strf(chunkstream *cs, int w, int h)
  : chunk(cs, "rgb24 strf", 1)
{
	this->w = w;
	this->h = h;
}

cram16_strf::cram16_strf(chunkstream *cs, int w, int h)
  : chunk(cs, "cram16 strf", 1)
{
	this->w = w;
	this->h = h;
}

void rgb24_strf::WRITE()
{
	chunkstream *cs = this->cs;

	cs->wr_str("strf");
	cs->wr32(0);
	cs->wr32(4+4+4+4+2+2+4+4+4*4);
	cs->wr32(this->w);
	cs->wr32(this->h);
	cs->wr16(1);  // planes
	cs->wr16(24);  // bitcount
	cs->wr_str("RGB ");
	cs->wr32(3 * (this->w + (this->w & 1)) * this->h);
	// x&y pels per metere, hopefully "0" is "I donno"
	cs->wr32(0);
	cs->wr32(0);
	cs->wr32(0);  // clr_used
	cs->wr32(0);  // clr_important
}

void cram16_strf::WRITE()
{
	chunkstream *cs = this->cs;

	cs->wr_str("strf");
	cs->wr32(0);
	cs->wr32(4+4+4+4+2+2+4+4+4*4);
	cs->wr32(this->w);
	cs->wr32(this->h);
	cs->wr16(1);  // planes
	cs->wr16(16);  // bitcount
	cs->wr_str("CRAM");
	// This is the wrong size, but as there isn't a constant size does it
	// matter??
	cs->wr32(3 * (this->w + (this->w & 1)) * this->h);
	// x&y pels per metere, hopefully "0" is "I donno"
	cs->wr32(0);
	cs->wr32(0);
	cs->wr32(0);  // clr_used
	cs->wr32(0);  // clr_important
}

void rgb24::frame(ppm *p, int framenum)
{
	// assert(p->w() == this->w && p->h() == this->h);
	int sz = (p->w() + (p->w() & 1)) * p->h() * 3;
	unsigned char *bytes = new unsigned char[sz];
	unsigned char *pt = bytes;
	char fname[44];
	int row = p->h();
	sprintf(fname, "rgb24 frame#%d", framenum);

	while(row >= 0) {
		memcpy(pt, p->pixel_row(row--), p->w() * 3);
		pt += 3 * p->w();
		if (p->w() & 1) {
			pt++;
		}
	}

	data_chunk *f = new data_chunk(this->cs, 0 != framenum, fname,
	  "00db", sz, bytes);
	f->write();
	delete []bytes;
}

int count_colors(color *c, int max_nc, int nc, 
  const unsigned char *prow, int sx, int ex,
  unsigned char rm = 0xff, unsigned char gm = 0xff, unsigned bm = 0xff)
{
	color ct;
	int i;

	for(; sx < ex; sx++) {
		ct.r = *(prow + sx*3 + 0) & rm;
		ct.g = *(prow + sx*3 + 1) & gm;
		ct.b = *(prow + sx*3 + 2) & bm;

		for(i = 0; i < nc; i++) {
			if (ct == c[i]) {
				break;
			}
		}
		if (i == nc) {
		  // assert(nc++ < max_nc);
                  nc++;
			c[i].r = ct.r;
			c[i].g = ct.g;
			c[i].b = ct.b;
			c[i].cnt = 0;
		}

		c[i].cnt++;
	}

	return nc;
}

cram16::cram16()
{
	this->last_frame = NULL;
}

void cram16::frame(ppm *p, int framenum)
{
  // assert(p->w() % 4 == 0);
  // assert(p->h() % 4 == 0);
  // assert(p->w() == this->w && p->h() == this->h);
	// If every block were max size the frame would be max_sz bytes
	int max_sz = ((p->w() / 4) * (p->h() / 4)) * (9*2) + 2;
	unsigned char *bytes = new unsigned char[max_sz];
	unsigned char *bp = bytes;
	unsigned char *image_bits;
	int ncolors;
	color c[16];
	unsigned int pixel_bits;
	unsigned char p1, p2;

	int bc8 = 0, bc2 = 0, bc1 = 0, bc1fail = 0, bc0 = 0;
	// cerr << "alloc by " << max_sz << " at address " << (unsigned int) &bytes[0] << endl;

	for(int y = p->h() -1; y > 0; y -= 4) {
		for(int x = 0; x < p->w(); x += 4) {
			pixel_bits = 0;
			ncolors = 0;
			ncolors = count_colors(c, sizeof(c)/sizeof(color), ncolors,
			  p->pixel_row(y-0), x, x+4,
			  0xf0, 0xf0, 0xf8);
			ncolors = count_colors(c, sizeof(c)/sizeof(color), ncolors,
			  p->pixel_row(y-1), x, x+4,
			  0xf0, 0xf0, 0xf8);
			ncolors = count_colors(c, sizeof(c)/sizeof(color), ncolors,
			  p->pixel_row(y-2), x, x+4,
			  0xf0, 0xf0, 0xf8);
			ncolors = count_colors(c, sizeof(c)/sizeof(color), ncolors,
			  p->pixel_row(y-3), x, x+4,
			  0xf0, 0xf0, 0xf8);

			// Image bytes & block code go here...
			image_bits = bp;
			bp += 2;

			// Depending on ncolors we will want to decide if this block
			// gets 1, 2, or 8 colors

			if (ncolors > 2) {
				// 8 color block
				bc8++;

				// Many reference parms
				this->c8quad(bp, p, x + 0, y - 0, 1, p1, p2);
				pixel_bits |= p1;
				pixel_bits |= p2 << 4;
				this->c8quad(bp, p, x + 2, y - 0, 0, p1, p2);
				pixel_bits |= p1 << 2;
				pixel_bits |= p2 << 6;
				this->c8quad(bp, p, x + 0, y - 2, 0, p1, p2);
				pixel_bits |= p1 << 8;
				pixel_bits |= p2 << 12;
				this->c8quad(bp, p, x + 2, y - 2, 0, p1, p2);
				pixel_bits |= p1 << 10;
				pixel_bits |= p2 << 14;

				// assert(0 == (pixel_bits & 0x8000));
			} else if (ncolors == 2) {
				// 2 color block
				bc2++;

				this->c2block(bp, p, x, y, c, ncolors, pixel_bits);
			} else if (ncolors == 1) {
				// 1 color block
				bc1++;
				int c16 = 0x8000 | this->c24toc16(c);
				int code_msb = c16 >> 8;

				if (code_msb >= 0x88 || code_msb >= 0x80 && code_msb <= 0x83) {
					pixel_bits = c16;
				} else {
					// 1 color block using 2 color call
					bc1fail++;
					this->c2block(bp, p, x, y, c, 1, pixel_bits);
				}
			} else {
				// assert(NULL == "ncolors is zero?");
			}

			*image_bits++ = 0xff & pixel_bits;
			*image_bits++ = 0xff & pixel_bits >> 8;
			// assert(bp < bytes + max_sz);
		}
	}
	*bp++ = 0x00;
	*bp++ = 0x00;
	
	// assert(bp <= bytes + max_sz);

	// *every* arg is a reference parm
	int dealloc = 1;
	this->c0blocks(bytes, bp, bc0, dealloc);

	char fname[44];
	sprintf(fname, "cram16 frame#%d", framenum);

	cerr << fname << "\n 8 color blocks: " << bc8 << "\n 2 color blcoks: "
	  << bc2 << "\n 1 color blocks: " << bc1 << " (" << bc1fail 
	  << " didn't encode)\n 0 color blocks: "
	  << bc0 << endl;
	//	cerr << "alloc bytes1 address is " <<  (unsigned int)&bytes[0] << endl;
	data_chunk *f = new data_chunk(this->cs, 0 != framenum, fname,
	  "00dc", bp - bytes, bytes);
	f->write();
	//     cerr << "dealloc = " << dealloc << endl;
	//      cerr << "alloc bytes2 address is " <<  (unsigned int)&bytes[0] << endl;
	if (dealloc) {
	  //          cerr << "alloc by - delete at" << (unsigned int)&bytes[0] << endl;
		delete []bytes;
         
	}
}

void cram16::c0blocks(unsigned char *&fstart, unsigned char *&fend, int &count,
  int &dealloc)
{
	if (!this->last_frame) {
		// There was no last frame.
		dealloc = 0;
		this->last_frame = fstart;
		return;
	}

	unsigned char *lf = this->last_frame, *f = fstart;
	int ls, s;
	unsigned char *nf = new unsigned char[fend - fstart];
	unsigned char *nfstart = nf;
	int nsame = 0;
	//  cerr << "alloc nf " << fend-fstart << " at address " << (unsigned int)&nf[0] << endl;

	while(f < fend) {
		ls = this->csize(lf);
		s = this->csize(f);

		if (ls == s && !memcmp(lf, f, s) && nsame < 0x300) {
			count++;
			nsame++;
		} else {
			if (nsame) {
				// assert(nsame <= 0x300);
				nsame += 0x8400;
				*nf++ = nsame & 0xff;
				*nf++ = nsame >> 8;
				nsame = 0;
			}
			memcpy(nf, f, s);
			nf += s;
		}

		lf += ls;
		f += s;
		// assert(lf > this->last_frame);
	}
	if (nsame) {
		// assert(nsame <= 0x300);
		nsame += 0x8400;
		*nf++ = nsame & 0xff;
		*nf++ = nsame >> 8;
	}

	delete []this->last_frame;
	this->last_frame = fstart;

	dealloc = 1;
	fend = nf;
	fstart = nfstart;
}

// How big is the current cram16 chunk?
int cram16::csize(unsigned char *c)
{
	// Move us to the MSB
	c++;

	if (*c < 0x80) {
		// 2 color or 8 color cell
		if (*(c+2) >= 0x80) {
			// 8 colors
			return 2*9;
		} else {
			// 2 colors
			return 2*3;
		}
	} else if (*c >= 0x84 && *c <= 0x87) {
		// assert(NULL == "Block skip?");
	} else if (*c >= 0x88 || *c >= 0x80 && *c <= 0x83) {
		// Single color cell
		return 2;
	} else {
		// assert(NULL == "invalid block?");
	}

	// assert(NULL == "Not reached?");
	return -10000;
}

void cram16::c8quad(unsigned char *&bp, ppm *p, int x, int y, int setflag,
  unsigned char& p1, unsigned char& p2)
{
	color ca[4], c;
	int ncolors;
	const unsigned char *pp;

	p1 = p2 = 0;

	ncolors = count_colors(ca, sizeof(ca)/sizeof(color), ncolors = 0,
	  p->pixel_row(y-0), x, x+2,
	  0xf0, 0xf0, 0xf8);
	ncolors = count_colors(ca, sizeof(ca)/sizeof(color), ncolors,
	  p->pixel_row(y-1), x, x+2,
	  0xf0, 0xf0, 0xf8);

	int c0=0, c1=0;
	int c16, i, max;

	// c0 = most popular color (or first color)
	for(max = i = 0; i < ncolors; i++) {
		if (ca[i].cnt > max) {
			c1 = c0 = i;
			max = ca[i].cnt;
		}
	}
	// c1 = color most distant from most popular color
	for(max = i = 0; i < ncolors; i++) {
		int dt;
		if ((dt = ca[i].dist2(ca[c0])) > max) {
			c1 = i;
			max = dt;
		}
	}

	pp = p->pixel_row(y) + x*3;
	c.r = *pp++;
	c.g = *pp++;
	c.b = *pp++;
	if (c.dist2(ca[c0]) < c.dist2(ca[c1])) {
		p1 |= 1;
	}

	c.r = *pp++;
	c.g = *pp++;
	c.b = *pp++;
	if (c.dist2(ca[c0]) < c.dist2(ca[c1])) {
		p1 |= 2;
	}

	pp = p->pixel_row(y-1) + x*3;
	c.r = *pp++;
	c.g = *pp++;
	c.b = *pp++;
	if (c.dist2(ca[c0]) < c.dist2(ca[c1])) {
		p2 |= 1;
	}

	c.r = *pp++;
	c.g = *pp++;
	c.b = *pp++;
	if (c.dist2(ca[c0]) < c.dist2(ca[c1])) {
		// We can't allow the high bit to be set in a quad encoding,
		// so we can't simply: p2 |= 2; rather we must swap c0 and c1,
		// and all the bits we set baised on them.

		int ct = c0;
		c0 = c1;
		c1 = ct;

		p1 = 0x3 ^ p1;
		p2 = 0x1 ^ p2;
	}

	c16 = this->c24toc16(ca + c0);
	if (setflag) {
		c16 |= 0x8000;
	}
	*bp++ = 0xff & c16;
	*bp++ = 0xff & c16 >> 8;
	c16 = this->c24toc16(ca + c1);
	*bp++ = 0xff & c16;
	*bp++ = 0xff & c16 >> 8;
}

void cram16::c2block(unsigned char *&bp, ppm *p, int x, int y,
  color *ca, int ncolors,
  unsigned int& pixel_bits)
{
	color c;
	const unsigned char *pp;

	pixel_bits = 0;

	int c0=0, c1=0;
	int c16, i, max;

	// it is kind of silly to go through this at the moment -- we are
	// only used on blocks with 2 colors!

	// c0 = most popular color (or first color)
	for(max = i = 0; i < ncolors; i++) {
		if (ca[i].cnt > max) {
			c1 = c0 = i;
			max = ca[i].cnt;
		}
	}
	// c1 = color most distant from most popular color
	for(max = i = 0; i < ncolors; i++) {
		int dt;
		if ((dt = ca[i].dist2(ca[c0])) > max) {
			c1 = i;
			max = dt;
		}
	}

	if (ncolors != 1) {
		// assert(c1 != c0);
	}

	int yi, xi, m;

	for(yi = y, m = 1; yi > y-4; yi--) {
		pp = p->pixel_row(yi) + x*3;
		for(xi = x; xi < x+4; xi++, m <<= 1) {
			c.r = *pp++;
			c.g = *pp++;
			c.b = *pp++;
			if (c.dist2(ca[c0]) < c.dist2(ca[c1])) {
				pixel_bits |= m;
			}
		}
	}

	if (pixel_bits & 0x8000) {
		// We can't allow the high bit to be set in a 2 color
		// encoding.  We must swap c0 and c1, and invert all
		// image bits.

		int ct = c0;
		c0 = c1;
		c1 = ct;
		
		pixel_bits = 0xffff & (~pixel_bits);
	}

	c16 = this->c24toc16(ca + c0);
	// assert(0 == (c16 & 0x8000));
	*bp++ = 0xff & c16;
	*bp++ = 0xff & c16 >> 8;
	c16 = this->c24toc16(ca + c1);
	*bp++ = 0xff & c16;
	*bp++ = 0xff & c16 >> 8;
}

unsigned int cram16::c24toc16(color *c)
{
	return
	    ((c->r >> 4) << 11)
	  | ((c->g >> 4) << 6)
	  | ((c->b >> 3) << 0);
}