scummvm/engines/cge/bitmap.cpp

491 lines
11 KiB
C++
Raw Normal View History

/* 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
*/
2011-06-13 11:57:24 +02:00
#include "cge/bitmap.h"
#include "cge/cfile.h"
#include "cge/jbw.h"
#include "cge/vol.h"
#include "cge/cfile.h"
#include "cge/vga13h.h"
2011-06-11 09:33:17 +02:00
#include "common/system.h"
2011-06-10 22:57:09 +02:00
namespace CGE {
2011-07-01 08:37:40 +02:00
Dac *Bitmap::_pal = NULL;
#define MAXPATH 128
2011-06-29 16:13:17 +02:00
void Bitmap::init() {
2011-06-30 08:30:23 +02:00
_pal = NULL;
}
2011-06-29 16:13:17 +02:00
void Bitmap::deinit() {
}
#pragma argsused
2011-06-29 16:13:17 +02:00
Bitmap::Bitmap(const char *fname, bool rem) : _m(NULL), _v(NULL) {
2011-06-13 11:57:24 +02:00
char pat[MAXPATH];
ForceExt(pat, fname, ".VBM");
#if (BMP_MODE < 2)
2011-06-30 08:30:23 +02:00
if (rem && PIC_FILE::exist(pat)) {
2011-06-13 11:57:24 +02:00
PIC_FILE file(pat);
2011-07-01 08:37:40 +02:00
if ((file._error == 0) && (!loadVBM(&file)))
2011-06-13 11:57:24 +02:00
error("Bad VBM [%s]", fname);
} else
#endif
{
2011-06-13 11:57:24 +02:00
#if (BMP_MODE)
ForceExt(pat, fname, ".BMP");
PIC_FILE file(pat);
2011-07-01 08:37:40 +02:00
if (file._error == 0) {
2011-06-30 08:30:23 +02:00
if (loadBMP(&file)) {
2011-06-13 11:57:24 +02:00
Code();
if (rem) {
2011-06-29 16:13:17 +02:00
free(_m);
_m = NULL;
2011-06-13 11:57:24 +02:00
}
} else
error("Bad BMP [%s]", fname);
}
2011-06-13 11:57:24 +02:00
#else
error("Bad VBM [%s]", fname);
#endif
}
}
2011-06-29 16:13:17 +02:00
Bitmap::Bitmap(uint16 w, uint16 h, uint8 *map) : _w(w), _h(h), _m(map), _v(NULL) {
2011-06-13 11:57:24 +02:00
if (map)
2011-06-30 08:30:23 +02:00
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
2011-06-29 16:13:17 +02:00
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)
2011-06-13 11:57:24 +02:00
uint16 lsiz = 2 + dsiz + 2; // uint16 for line header, uint16 for gap
2011-06-29 16:13:17 +02:00
uint16 psiz = _h * lsiz; // - last gape, but + plane trailer
uint8 *v = new uint8[4 * psiz + _h * sizeof(*_b)];// the same for 4 planes
2011-06-13 11:57:24 +02:00
// + 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
memcpy(v + lsiz, v, psiz - lsiz); // tricky replicate lines
*(uint16 *)(v + psiz - 2) = EOI; // plane trailer uint16
memcpy(v + psiz, v, 3 * psiz); // tricky replicate planes
HideDesc *b = (HideDesc *)(v + 4 * psiz);
2011-06-29 16:13:17 +02:00
b->skip = (SCR_WID - _w) >> 2;
b->hide = _w >> 2;
memcpy(b + 1, b, (_h - 1) * sizeof(*b)); // tricky fill entire table
2011-06-13 11:57:24 +02:00
b->skip = 0; // fix the first entry
2011-06-29 16:13:17 +02:00
_v = v;
_b = b;
}
2011-06-29 16:13:17 +02:00
Bitmap::Bitmap(const Bitmap &bmp) : _w(bmp._w), _h(bmp._h), _m(NULL), _v(NULL) {
uint8 *v0 = bmp._v;
2011-06-13 11:57:24 +02:00
if (v0) {
2011-06-29 16:13:17 +02:00
uint16 vsiz = (uint8 *)(bmp._b) - (uint8 *)(v0);
uint16 siz = vsiz + _h * sizeof(HideDesc);
2011-06-13 11:57:24 +02:00
uint8 *v1 = farnew(uint8, siz);
if (v1 == NULL)
error("No core");
memcpy(v1, v0, siz);
2011-06-29 16:13:17 +02:00
_b = (HideDesc *)((_v = v1) + vsiz);
2011-06-13 11:57:24 +02:00
}
}
2011-06-29 16:13:17 +02:00
Bitmap::~Bitmap(void) {
if (MemType(_m) == FAR_MEM)
free(_m);
2011-06-29 16:13:17 +02:00
switch (MemType(_v)) {
2011-06-13 11:57:24 +02:00
case NEAR_MEM :
2011-06-29 16:13:17 +02:00
delete[](uint8 *) _v;
2011-06-13 11:57:24 +02:00
break;
case FAR_MEM :
2011-06-29 16:13:17 +02:00
free(_v);
default:
warning("Unhandled MemType in Bitmap destructor");
break;
2011-06-13 11:57:24 +02:00
break;
}
}
2011-06-29 16:13:17 +02:00
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);
2011-06-13 11:57:24 +02:00
if (v0 == NULL)
2011-06-29 16:13:17 +02:00
_v = NULL;
2011-06-13 11:57:24 +02:00
else {
2011-06-29 16:13:17 +02:00
uint16 vsiz = (uint8 *)bmp._b - (uint8 *)v0;
uint16 siz = vsiz + _h * sizeof(HideDesc);
2011-06-13 11:57:24 +02:00
uint8 *v1 = farnew(uint8, siz);
if (v1 == NULL)
error("No core");
memcpy(v1, v0, siz);
2011-06-29 16:13:17 +02:00
_b = (HideDesc *)((_v = v1) + vsiz);
2011-06-13 11:57:24 +02:00
}
return *this;
}
2011-06-30 08:30:23 +02:00
uint16 Bitmap::moveVmap(uint8 *buf) {
2011-06-29 16:13:17 +02:00
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);
2011-06-13 11:57:24 +02:00
return siz;
}
return 0;
}
2011-06-30 08:30:23 +02:00
BMP_PTR Bitmap::code(void) {
2011-06-29 16:13:17 +02:00
if (_m) {
2011-06-13 11:57:24 +02:00
uint16 i, cnt;
2011-06-29 16:13:17 +02:00
if (_v) { // old X-map exists, so remove it
switch (MemType(_v)) {
2011-06-13 11:57:24 +02:00
case NEAR_MEM :
2011-06-29 16:13:17 +02:00
delete[](uint8 *) _v;
2011-06-13 11:57:24 +02:00
break;
case FAR_MEM :
2011-06-29 16:13:17 +02:00
free(_v);
2011-06-13 11:57:24 +02:00
break;
default:
warning("Unhandled MemType in Bitmap::Code()");
break;
2011-06-13 11:57:24 +02:00
}
2011-06-29 16:13:17 +02:00
_v = NULL;
2011-06-13 11:57:24 +02:00
}
2011-06-13 11:57:24 +02:00
while (true) { // at most 2 times: for (V == NULL) & for allocated block;
2011-06-29 16:13:17 +02:00
uint8 *im = _v + 2;
uint16 *cp = (uint16 *) _v;
2011-06-13 11:57:24 +02:00
int bpl;
2011-06-29 16:13:17 +02:00
if (_v) { // 2nd pass - fill the hide table
for (i = 0; i < _h; i++) {
_b[i].skip = 0xFFFF;
_b[i].hide = 0x0000;
2011-06-13 11:57:24 +02:00
}
}
for (bpl = 0; bpl < 4; bpl++) { // once per each bitplane
2011-06-29 16:13:17 +02:00
uint8 *bm = _m;
2011-06-13 11:57:24 +02:00
bool skip = (bm[bpl] == TRANS);
uint16 j;
cnt = 0;
2011-06-29 16:13:17 +02:00
for (i = 0; i < _h; i++) { // once per each line
2011-06-13 11:57:24 +02:00
uint8 pix;
2011-06-29 16:13:17 +02:00
for (j = bpl; j < _w; j += 4) {
2011-06-13 11:57:24 +02:00
pix = bm[j];
2011-06-29 16:13:17 +02:00
if (_v && pix != TRANS) {
if (j < _b[i].skip)
_b[i].skip = j;
2011-06-13 11:57:24 +02:00
2011-06-29 16:13:17 +02:00
if (j >= _b[i].hide)
_b[i].hide = j + 1;
2011-06-13 11:57:24 +02:00
}
if ((pix == TRANS) != skip || cnt >= 0x3FF0) { // end of block
cnt |= (skip) ? SKP : CPY;
2011-06-29 16:13:17 +02:00
if (_v)
2011-06-13 11:57:24 +02:00
*cp = cnt; // store block description uint16
cp = (uint16 *) im;
im += 2;
skip = (pix == TRANS);
cnt = 0;
}
if (! skip) {
2011-06-29 16:13:17 +02:00
if (_v)
2011-06-13 11:57:24 +02:00
*im = pix;
++ im;
}
++ cnt;
}
2011-06-29 16:13:17 +02:00
bm += _w;
if (_w < SCR_WID) {
2011-06-13 11:57:24 +02:00
if (skip) {
cnt += (SCR_WID - j + 3) / 4;
} else {
cnt |= CPY;
2011-06-29 16:13:17 +02:00
if (_v)
2011-06-13 11:57:24 +02:00
*cp = cnt;
cp = (uint16 *) im;
im += 2;
skip = true;
cnt = (SCR_WID - j + 3) / 4;
}
}
}
if (cnt && ! skip) {
cnt |= CPY;
2011-06-29 16:13:17 +02:00
if (_v)
2011-06-13 11:57:24 +02:00
*cp = cnt;
cp = (uint16 *) im;
im += 2;
}
2011-06-29 16:13:17 +02:00
if (_v)
2011-06-13 11:57:24 +02:00
*cp = EOI;
cp = (uint16 *) im;
im += 2;
}
2011-06-29 16:13:17 +02:00
if (_v)
2011-06-13 11:57:24 +02:00
break;
2011-06-29 16:13:17 +02:00
uint16 sizV = (uint16)(im - 2 - _v);
_v = farnew(uint8, sizV + _h * sizeof(*_b));
if (!_v)
2011-06-13 11:57:24 +02:00
error("No core");
2011-06-29 16:13:17 +02:00
_b = (HideDesc *)(_v + sizV);
}
2011-06-13 11:57:24 +02:00
cnt = 0;
2011-06-29 16:13:17 +02:00
for (i = 0; i < _h; i++) {
if (_b[i].skip == 0xFFFF) { // whole line is skipped
_b[i].skip = (cnt + SCR_WID) >> 2;
2011-06-13 11:57:24 +02:00
cnt = 0;
} else {
2011-06-29 16:13:17 +02:00
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;
2011-06-13 11:57:24 +02:00
cnt = SCR_WID - h;
}
}
}
2011-06-13 11:57:24 +02:00
return this;
}
2011-06-30 08:30:23 +02:00
bool Bitmap::solidAt(int x, int y) {
2011-06-13 11:57:24 +02:00
uint8 *m;
uint16 r, n, n0;
2011-06-29 16:13:17 +02:00
if ((x >= _w) || (y >= _h))
2011-06-13 11:57:24 +02:00
return false;
2011-06-29 16:13:17 +02:00
m = _v;
2011-06-13 11:57:24 +02:00
r = x % 4;
n0 = (SCR_WID * y + x) / 4, n = 0;
2011-06-13 11:57:24 +02:00
while (r) {
uint16 w, t;
2011-06-13 11:57:24 +02:00
w = *(uint16 *) m;
m += 2;
t = w & 0xC000;
w &= 0x3FFF;
2011-06-13 11:57:24 +02:00
switch (t) {
case EOI :
2011-06-29 16:13:17 +02:00
r--;
2011-06-13 11:57:24 +02:00
case SKP :
w = 0;
break;
case REP :
w = 1;
break;
}
m += w;
}
2011-06-13 11:57:24 +02:00
while (true) {
uint16 w, t;
w = *(uint16 *) m;
2011-06-13 11:57:24 +02:00
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);
}
}
2011-07-01 08:37:40 +02:00
bool Bitmap::saveVBM(XFile *f) {
2011-06-30 08:30:23 +02:00
uint16 p = (_pal != NULL),
2011-06-29 16:13:17 +02:00
n = ((uint16)(((uint8 *)_b) - _v)) + _h * sizeof(HideDesc);
2011-07-01 08:37:40 +02:00
if (f->_error == 0)
2011-06-30 08:30:23 +02:00
f->write((uint8 *)&p, sizeof(p));
2011-06-13 11:57:24 +02:00
2011-07-01 08:37:40 +02:00
if (f->_error == 0)
2011-06-30 08:30:23 +02:00
f->write((uint8 *)&n, sizeof(n));
2011-07-01 08:37:40 +02:00
if (f->_error == 0)
2011-06-30 08:30:23 +02:00
f->write((uint8 *)&_w, sizeof(_w));
2011-07-01 08:37:40 +02:00
if (f->_error == 0)
2011-06-30 08:30:23 +02:00
f->write((uint8 *)&_h, sizeof(_h));
2011-07-01 08:37:40 +02:00
if (f->_error == 0)
2011-06-13 11:57:24 +02:00
if (p)
2011-06-30 08:30:23 +02:00
f->write((uint8 *)_pal, 256 * 3);
2011-07-01 08:37:40 +02:00
if (f->_error == 0)
2011-06-30 08:30:23 +02:00
f->write(_v, n);
2011-06-13 11:57:24 +02:00
2011-07-01 08:37:40 +02:00
return (f->_error == 0);
}
2011-07-01 08:37:40 +02:00
bool Bitmap::loadVBM(XFile *f) {
2011-06-13 11:57:24 +02:00
uint16 p = 0, n = 0;
2011-07-01 08:37:40 +02:00
if (f->_error == 0)
2011-06-30 08:30:23 +02:00
f->read((uint8 *)&p, sizeof(p));
2011-07-01 08:37:40 +02:00
if (f->_error == 0)
2011-06-30 08:30:23 +02:00
f->read((uint8 *)&n, sizeof(n));
2011-07-01 08:37:40 +02:00
if (f->_error == 0)
2011-06-30 08:30:23 +02:00
f->read((uint8 *)&_w, sizeof(_w));
2011-07-01 08:37:40 +02:00
if (f->_error == 0)
2011-06-30 08:30:23 +02:00
f->read((uint8 *)&_h, sizeof(_h));
2011-06-13 11:57:24 +02:00
2011-07-01 08:37:40 +02:00
if (f->_error == 0) {
2011-06-13 11:57:24 +02:00
if (p) {
2011-06-30 08:30:23 +02:00
if (_pal) {
// Read in the palette
byte palData[PAL_SIZ];
2011-06-30 08:30:23 +02:00
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
2011-06-30 08:30:23 +02:00
f->seek(f->mark() + PAL_SIZ);
2011-06-13 11:57:24 +02:00
}
}
2011-06-29 16:13:17 +02:00
if ((_v = farnew(uint8, n)) == NULL)
2011-06-13 11:57:24 +02:00
return false;
2011-07-01 08:37:40 +02:00
if (f->_error == 0)
2011-06-30 08:30:23 +02:00
f->read(_v, n);
2011-06-29 16:13:17 +02:00
_b = (HideDesc *)(_v + n - _h * sizeof(HideDesc));
2011-07-01 08:37:40 +02:00
return (f->_error == 0);
2011-06-13 11:57:24 +02:00
}
2011-07-01 08:37:40 +02:00
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];
2011-06-30 08:30:23 +02:00
f->read((byte *)&hea, sizeof(hea));
2011-07-01 08:37:40 +02:00
if (f->_error == 0) {
if (hea.hdr == 0x436L) {
int16 i = (hea.hdr - sizeof(hea)) / sizeof(BGR4);
2011-06-30 08:30:23 +02:00
f->read((byte *)&bpal, sizeof(bpal));
2011-07-01 08:37:40 +02:00
if (f->_error == 0) {
2011-06-30 08:30:23 +02:00
if (_pal) {
for (i = 0; i < 256; i ++) {
2011-07-01 08:37:40 +02:00
_pal[i]._r = bpal[i].R;
_pal[i]._g = bpal[i].G;
_pal[i]._b = bpal[i].B;
2011-06-30 08:30:23 +02:00
}
_pal = NULL;
}
2011-06-29 16:13:17 +02:00
_h = hea.hig;
_w = hea.wid;
if ((_m = farnew(byte, _h * _w)) != NULL) {
int16 r = (4 - (hea.wid & 3)) % 4;
byte buf[3]; int i;
2011-06-29 16:13:17 +02:00
for (i = _h - 1; i >= 0; i--) {
2011-06-30 08:30:23 +02:00
f->read(_m + (_w * i), _w);
2011-07-01 08:37:40 +02:00
if (r && f->_error == 0)
2011-06-30 08:30:23 +02:00
f->read(buf, r);
2011-07-01 08:37:40 +02:00
if (f->_error)
break;
}
if (i < 0)
return true;
}
}
}
}
return false;
}
2011-06-10 22:57:09 +02:00
} // End of namespace CGE