aboutsummaryrefslogtreecommitdiff
path: root/engines/cge/bitmap.cpp
blob: 4630f8745631f79300727446c94edb1ee727ba58 (plain)
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
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program 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 2
 * of the License, or (at your option) any later version.

 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

/*
 * This code is based on original Soltys source code
 * Copyright (c) 1994-1995 Janus B. Wisniewski and L.K. Avalon
 */

#include "cge/bitmap.h"
#include "cge/cfile.h"
#include "cge/jbw.h"
#include "cge/vol.h"
#include "cge/cfile.h"
#include "cge/vga13h.h"
#include "common/system.h"

namespace CGE {

Dac *Bitmap::_pal = NULL;
#define MAXPATH  128

void Bitmap::init() {
	_pal = NULL;
}

void Bitmap::deinit() {
}

#pragma argsused
Bitmap::Bitmap(const char *fname, bool rem) : _m(NULL), _v(NULL) {
	char pat[MAXPATH];
	forceExt(pat, fname, ".VBM");

#if (BMP_MODE < 2)
	if (rem && PIC_FILE::exist(pat)) {
		PIC_FILE file(pat);
		if ((file._error == 0) && (!loadVBM(&file)))
			error("Bad VBM [%s]", fname);
	} else
#endif
	{
#if (BMP_MODE)
		ForceExt(pat, fname, ".BMP");
		PIC_FILE file(pat);
		if (file._error == 0) {
			if (loadBMP(&file)) {
				Code();
				if (rem) {
					free(_m);
					_m = NULL;
				}
			} else
				error("Bad BMP [%s]", fname);
		}
#else
		error("Bad VBM [%s]", fname);
#endif
	}
}


Bitmap::Bitmap(uint16 w, uint16 h, uint8 *map) : _w(w), _h(h), _m(map), _v(NULL) {
	if (map)
		code();
}


// following routine creates filled rectangle
// immediately as VGA video chunks, in near memory as fast as possible,
// especially for text line real time display
Bitmap::Bitmap(uint16 w, uint16 h, uint8 fill)
	: _w((w + 3) & ~3),                              // only full uint32 allowed!
	  _h(h),
	  _m(NULL) {
	uint16 dsiz = _w >> 2;                           // data size (1 plane line size)
	uint16 lsiz = 2 + dsiz + 2;                     // uint16 for line header, uint16 for gap
	uint16 psiz = _h * lsiz;                         // - last gape, but + plane trailer
	uint8 *v = new uint8[4 * psiz + _h * sizeof(*_b)];// the same for 4 planes
	                                                // + room for wash table
	if (v == NULL)
		error("No core");

	*(uint16 *) v = CPY | dsiz;                     // data chunk hader
	memset(v + 2, fill, dsiz);                      // data bytes
	*(uint16 *)(v + lsiz - 2) = SKP | ((SCR_WID / 4) - dsiz);  // gap

	// Replicate lines
	byte *destP;
	for (destP = v + lsiz; destP < (v + psiz); destP += lsiz)
		Common::copy(v, v + lsiz, destP);

	*(uint16 *)(v + psiz - 2) = EOI;                // plane trailer uint16

	// Repliccate planes
	for (destP = v + psiz; destP < (v + 4 * psiz); destP += psiz)
		Common::copy(v, v + psiz, destP);

	HideDesc *b = (HideDesc *)(v + 4 * psiz);
	b->skip = (SCR_WID - _w) >> 2;
	b->hide = _w >> 2;

	// Replicate across the entire table
	for (HideDesc *hdP = b + 1; hdP < (b + _h); ++hdP)
		*hdP = *b;
	
	b->skip = 0;                                    // fix the first entry
	_v = v;
	_b = b;
}


Bitmap::Bitmap(const Bitmap &bmp) : _w(bmp._w), _h(bmp._h), _m(NULL), _v(NULL) {
	uint8 *v0 = bmp._v;
	if (v0) {
		uint16 vsiz = (uint8 *)(bmp._b) - (uint8 *)(v0);
		uint16 siz = vsiz + _h * sizeof(HideDesc);
		uint8 *v1 = farnew(uint8, siz);
		if (v1 == NULL)
			error("No core");
		memcpy(v1, v0, siz);
		_b = (HideDesc *)((_v = v1) + vsiz);
	}
}


Bitmap::~Bitmap() {
	if (memType(_m) == FAR_MEM)
		free(_m);

	switch (memType(_v)) {
	case NEAR_MEM :
		delete[](uint8 *) _v;
		break;
	case FAR_MEM  :
		free(_v);
	default:
		warning("Unhandled MemType in Bitmap destructor");
		break;
		break;
	}
}


Bitmap &Bitmap::operator = (const Bitmap &bmp) {
	uint8 *v0 = bmp._v;
	_w = bmp._w;
	_h = bmp._h;
	_m = NULL;
	if (memType(_v) == FAR_MEM)
		free(_v);
	if (v0 == NULL)
		_v = NULL;
	else {
		uint16 vsiz = (uint8 *)bmp._b - (uint8 *)v0;
		uint16 siz = vsiz + _h * sizeof(HideDesc);
		uint8 *v1 = farnew(uint8, siz);
		if (v1 == NULL)
			error("No core");
		memcpy(v1, v0, siz);
		_b = (HideDesc *)((_v = v1) + vsiz);
	}
	return *this;
}


uint16 Bitmap::moveVmap(uint8 *buf) {
	if (_v) {
		uint16 vsiz = (uint8 *)_b - (uint8 *)_v;
		uint16 siz = vsiz + _h * sizeof(HideDesc);
		memcpy(buf, _v, siz);
		if (memType(_v) == FAR_MEM)
			free(_v);
		_b = (HideDesc *)((_v = buf) + vsiz);
		return siz;
	}
	return 0;
}


BMP_PTR Bitmap::code() {
	if (_m) {
		uint16 i, cnt;

		if (_v) {                                        // old X-map exists, so remove it
			switch (memType(_v)) {
			case NEAR_MEM :
				delete[](uint8 *) _v;
				break;
			case FAR_MEM  :
				free(_v);
				break;
			default:
				warning("Unhandled MemType in Bitmap::Code()");
				break;
			}
			_v = NULL;
		}

		while (true) {                                  // at most 2 times: for (V == NULL) & for allocated block;
			uint8 *im = _v + 2;
			uint16 *cp = (uint16 *) _v;
			int bpl;

			if (_v) {                                      // 2nd pass - fill the hide table
				for (i = 0; i < _h; i++) {
					_b[i].skip = 0xFFFF;
					_b[i].hide = 0x0000;
				}
			}
			for (bpl = 0; bpl < 4; bpl++) {              // once per each bitplane
				uint8 *bm = _m;
				bool skip = (bm[bpl] == TRANS);
				uint16 j;

				cnt = 0;
				for (i = 0; i < _h; i++) {                  // once per each line
					uint8 pix;
					for (j = bpl; j < _w; j += 4) {
						pix = bm[j];
						if (_v && pix != TRANS) {
							if (j < _b[i].skip)
								_b[i].skip = j;

							if (j >= _b[i].hide)
								_b[i].hide = j + 1;
						}
						if ((pix == TRANS) != skip || cnt >= 0x3FF0) { // end of block
							cnt |= (skip) ? SKP : CPY;
							if (_v)
								*cp = cnt;                          // store block description uint16

							cp = (uint16 *) im;
							im += 2;
							skip = (pix == TRANS);
							cnt = 0;
						}
						if (! skip) {
							if (_v)
								*im = pix;
							++ im;
						}
						++ cnt;
					}

					bm += _w;
					if (_w < SCR_WID) {
						if (skip) {
							cnt += (SCR_WID - j + 3) / 4;
						} else {
							cnt |= CPY;
							if (_v)
								*cp = cnt;

							cp = (uint16 *) im;
							im += 2;
							skip = true;
							cnt = (SCR_WID - j + 3) / 4;
						}
					}
				}
				if (cnt && ! skip) {
					cnt |= CPY;
					if (_v)
						*cp = cnt;

					cp = (uint16 *) im;
					im += 2;
				}
				if (_v)
					*cp = EOI;
				cp = (uint16 *) im;
				im += 2;
			}
			if (_v)
				break;

			uint16 sizV = (uint16)(im - 2 - _v);
			_v = farnew(uint8, sizV + _h * sizeof(*_b));
			if (!_v)
				error("No core");

			_b = (HideDesc *)(_v + sizV);
		}
		cnt = 0;
		for (i = 0; i < _h; i++) {
			if (_b[i].skip == 0xFFFF) {                    // whole line is skipped
				_b[i].skip = (cnt + SCR_WID) >> 2;
				cnt = 0;
			} else {
				uint16 s = _b[i].skip & ~3;
				uint16 h = (_b[i].hide + 3) & ~3;
				_b[i].skip = (cnt + s) >> 2;
				_b[i].hide = (h - s) >> 2;
				cnt = SCR_WID - h;
			}
		}
	}
	return this;
}


bool Bitmap::solidAt(int x, int y) {
	uint8 *m;
	uint16 r, n, n0;

	if ((x >= _w) || (y >= _h))
		return false;

	m = _v;
	r = static_cast<uint16>(x) % 4;
	n0 = (SCR_WID * y + x) / 4, n = 0;

	while (r) {
		uint16 w, t;

		w = *(uint16 *) m;
		m += 2;
		t = w & 0xC000;
		w &= 0x3FFF;

		switch (t) {
		case EOI :
			r--;
		case SKP :
			w = 0;
			break;
		case REP :
			w = 1;
			break;
		}
		m += w;
	}

	while (true) {
		uint16 w, t;

		w = *(uint16 *) m;
		m += 2;
		t = w & 0xC000;
		w &= 0x3FFF;

		if (n > n0)
			return false;

		n += w;
		switch (t) {
		case EOI :
			return false;
		case SKP :
			w = 0;
			break;
		case REP :
		case CPY :
			if (n - w <= n0 && n > n0)
				return true;
			break;
		}
		m += ((t == REP) ? 1 : w);
	}
}


bool Bitmap::saveVBM(XFile *f) {
	uint16 p = (_pal != NULL),
	       n = ((uint16)(((uint8 *)_b) - _v)) + _h * sizeof(HideDesc);
	if (f->_error == 0)
		f->write((uint8 *)&p, sizeof(p));

	if (f->_error == 0)
		f->write((uint8 *)&n, sizeof(n));

	if (f->_error == 0)
		f->write((uint8 *)&_w, sizeof(_w));

	if (f->_error == 0)
		f->write((uint8 *)&_h, sizeof(_h));

	if (f->_error == 0)
		if (p)
			f->write((uint8 *)_pal, 256 * 3);

	if (f->_error == 0)
		f->write(_v, n);

	return (f->_error == 0);
}


bool Bitmap::loadVBM(XFile *f) {
	uint16 p = 0, n = 0;
	if (f->_error == 0)
		f->read((uint8 *)&p, sizeof(p));

	if (f->_error == 0)
		f->read((uint8 *)&n, sizeof(n));

	if (f->_error == 0)
		f->read((uint8 *)&_w, sizeof(_w));

	if (f->_error == 0)
		f->read((uint8 *)&_h, sizeof(_h));

	if (f->_error == 0) {
		if (p) {
			if (_pal) {
				// Read in the palette
				byte palData[PAL_SIZ];
				f->read(palData, PAL_SIZ);
				
				const byte *srcP = palData;
				for (int idx = 0; idx < PAL_CNT; ++idx, srcP += 3) {
					_pal[idx]._r = *srcP;
					_pal[idx]._g = *(srcP + 1);
					_pal[idx]._b = *(srcP + 2);
				}
			} else
				f->seek(f->mark() + PAL_SIZ);
		}
	}
	if ((_v = farnew(uint8, n)) == NULL)
		return false;

	if (f->_error == 0)
		f->read(_v, n);

	_b = (HideDesc *)(_v + n - _h * sizeof(HideDesc));
	return (f->_error == 0);
}

bool Bitmap::loadBMP(XFile *f) {
  struct {
	   char BM[2];
	   union { int16 len; int32 len_; };
	   union { int16 _06; int32 _06_; };
	   union { int16 hdr; int32 hdr_; };
	   union { int16 _0E; int32 _0E_; };
	   union { int16 wid; int32 wid_; };
	   union { int16 hig; int32 hig_; };
	   union { int16 _1A; int32 _1A_; };
	   union { int16 _1E; int32 _1E_; };
	   union { int16 _22; int32 _22_; };
	   union { int16 _26; int32 _26_; };
	   union { int16 _2A; int32 _2A_; };
	   union { int16 _2E; int32 _2E_; };
	   union { int16 _32; int32 _32_; };
	 } hea;

  Bgr4 bpal[256];

  f->read((byte *)&hea, sizeof(hea));
  if (f->_error == 0) {
      if (hea.hdr == 0x436L) {
	  int16 i = (hea.hdr - sizeof(hea)) / sizeof(Bgr4);
	  f->read((byte *)&bpal, sizeof(bpal));
	  if (f->_error == 0) {
		if (_pal) {
			for (i = 0; i < 256; i ++) {
				_pal[i]._r = bpal[i]._R;
				_pal[i]._g = bpal[i]._G;
				_pal[i]._b = bpal[i]._B;
			}
			_pal = NULL;
		}
	      _h = hea.hig;
	      _w = hea.wid;
	      if ((_m = farnew(byte, _h * _w)) != NULL) {
		  int16 r = (4 - (hea.wid & 3)) % 4;
		  byte buf[3];
		  for (i = _h - 1; i >= 0; i--) {
		      f->read(_m + (_w * i), _w);
		      if (r && f->_error == 0)
				  f->read(buf, r);
		      if (f->_error)
				  break;
		  }
		  if (i < 0)
			  return true;
		}
	    }
	}
    }
  return false;
}

} // End of namespace CGE