aboutsummaryrefslogtreecommitdiff
path: root/engines/gob/video_v6.cpp
blob: aae5d3ac129dceb51aaac53d016140bdb54d8ff3 (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
/* 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.
 *
 * $URL$
 * $Id$
 *
 */

#include "common/endian.h"
#include "common/savefile.h"
#include "graphics/dither.h"

#include "gob/gob.h"
#include "gob/video.h"
#include "gob/util.h"
#include "gob/indeo3.h"

namespace Gob {

Video_v6::Video_v6(GobEngine *vm) : Video_v2(vm) {
}

void Video_v6::init(const char *target) {
	initOSD();

	if (loadPalLUT(target))
		return;

	buildPalLUT();

	savePalLUT(target);
}

bool Video_v6::loadPalLUT(const char *target) {
	if (target[0] == '\0')
		return false;

	char *pltSave = new char[strlen(target) + 5];

	strcpy(pltSave, target);
	strcat(pltSave, ".plt");

	Common::InSaveFile *saveFile;

	Common::SaveFileManager *saveMan = g_system->getSavefileManager();
	if (!(saveFile = saveMan->openForLoading(pltSave))) {
		delete[] pltSave;
		return false;
	}

	drawOSDText("Loading palette table");

	bool loaded = _palLUT->load(*saveFile);

	delete[] pltSave;
	delete saveFile;

	return loaded;
}

bool Video_v6::savePalLUT(const char *target) {
	if (target[0] == '\0')
		return false;

	char *pltSave = new char[strlen(target) + 5];

	strcpy(pltSave, target);
	strcat(pltSave, ".plt");

	Common::OutSaveFile *saveFile;

	Common::SaveFileManager *saveMan = g_system->getSavefileManager();
	if (!(saveFile = saveMan->openForSaving(pltSave))) {
		delete[] pltSave;
		return false;
	}

	drawOSDText("Saving palette table");

	_palLUT->save(*saveFile);

	delete[] pltSave;
	delete saveFile;

	return true;
}

void Video_v6::buildPalLUT() {
	char text[30];

	_palLUT->setPalette(_ditherPalette, Graphics::PaletteLUT::kPaletteYUV, 8, 0);

	for (int i = 0; (i < 64) && !_vm->shouldQuit(); i++) {
		sprintf(text, "Building palette table: %02d/63", i);
		drawOSDText(text);
		_palLUT->buildNext();
		_vm->_util->processInput();
	}
}

char Video_v6::spriteUncompressor(byte *sprBuf, int16 srcWidth, int16 srcHeight,
	    int16 x, int16 y, int16 transp, SurfaceDesc *destDesc) {
	if (!destDesc)
		return 1;

	_vm->validateVideoMode(destDesc->_vidMode);

	if ((sprBuf[0] == 1) && (sprBuf[1] == 3)) {
		drawPacked(sprBuf, x, y, destDesc);
		return 1;
	}

	warning("Urban Stub: spriteUncompressor(), sprBuf[0,1] = %d,%d",
			sprBuf[0], sprBuf[1]);
	return 1;
}

void Video_v6::fillRect(SurfaceDesc *dest,
		int16 left, int16 top, int16 right, int16 bottom, int16 color) {

	if (!(color & 0xFF00)) {
		Video::fillRect(dest, left, top, right, bottom, color);
		return;
	}

	if (!(color & 0x0100)) {
		Video::fillRect(dest, left, top, right, bottom, color);
		return;
	}

	if (_doRangeClamp) {
		if (left > right)
			SWAP(left, right);
		if (top > bottom)
			SWAP(top, bottom);

		if ((left >= dest->getWidth()) || (right < 0) ||
		    (top >= dest->getHeight()) || (bottom < 0))
			return;

		left = CLIP(left, (int16) 0, (int16) (dest->getWidth() - 1));
		top = CLIP(top, (int16) 0, (int16) (dest->getHeight() - 1));
		right = CLIP(right, (int16) 0, (int16) (dest->getWidth() - 1));
		bottom = CLIP(bottom, (int16) 0, (int16) (dest->getHeight() - 1));
	}

	byte strength = 16 - (((uint16) color) >> 12);
	shadeRect(dest, left, top, right, bottom, color, strength);
}

void Video_v6::shadeRect(SurfaceDesc *dest,
		int16 left, int16 top, int16 right, int16 bottom, byte color, byte strength) {

	int width  = right  - left + 1;
	int height = bottom - top  + 1;
	int dWidth = dest->getWidth();
	byte *vidMem = dest->getVidMem() + dWidth * top + left;
	byte sY, sU, sV;

	_palLUT->getEntry(color, sY, sU, sV);

	Graphics::SierraLight *dither =
		new Graphics::SierraLight(width, _palLUT);

	for (int i = 0; i < height; i++) {
		byte *d = vidMem;

		for (int j = 0; j < width; j++) {
			byte dY, dU, dV;
			byte dC = *d;

			_palLUT->getEntry(dC, dY, dU, dV);

			dY = CLIP<int>(sY + (dY >> 2), 0, 255);
			*d++ = dither->dither(dY, sU, sV, j);
		}

		dither->nextLine();
		vidMem += dWidth;
	}

	delete dither;
}

void Video_v6::drawPacked(const byte *sprBuf, int16 x, int16 y, SurfaceDesc *surfDesc) {
	const byte *data = sprBuf + 2;

	int16 width = READ_LE_UINT16(data);
	int16 height = READ_LE_UINT16(data + 2);
	data += 4;

	const byte *srcData = data;
	byte *uncBuf = 0;

	if (*srcData++ != 0) {
		uint32 size = READ_LE_UINT32(data);

		uncBuf = new byte[size];

		//sub_4F020(data, buf);
		warning("Urban Stub: drawPacked: sub_4F020(data, uncBuf)");

		srcData = uncBuf;
	}

	drawYUVData(srcData, surfDesc, width, height, x, y);

	delete[] uncBuf;
}

void Video_v6::drawYUVData(const byte *srcData, SurfaceDesc *destDesc,
		int16 width, int16 height, int16 x, int16 y) {

	int16 dataWidth = width;
	int16 dataHeight = height;

	if (dataWidth & 0xF)
		dataWidth = (dataWidth & 0xFFF0) + 16;
	if (dataHeight & 0xF)
		dataHeight = (dataHeight & 0xFFF0) + 16;

	const byte *dataY = srcData;
	const byte *dataU = dataY +  (dataWidth * dataHeight);
	const byte *dataV = dataU + ((dataWidth * dataHeight) >> 4);

	drawYUV(destDesc, x, y, dataWidth, dataHeight, width, height, dataY, dataU, dataV);

}

void Video_v6::drawYUV(SurfaceDesc *destDesc, int16 x, int16 y,
		int16 dataWidth, int16 dataHeight, int16 width, int16 height,
		const byte *dataY, const byte *dataU, const byte *dataV) {

	byte *vidMem = destDesc->getVidMem() + y * destDesc->getWidth() + x;

	if ((x + width - 1) >= destDesc->getWidth())
		width = destDesc->getWidth() - x;
	if ((y + height - 1) >= destDesc->getHeight())
		height = destDesc->getHeight() - y;

	Graphics::SierraLight *dither =
		new Graphics::SierraLight(width, _palLUT);

	for (int i = 0; i < height; i++) {
		byte *dest = vidMem;
		const byte *srcY = dataY +  i       *  dataWidth;
		const byte *srcU = dataU + (i >> 2) * (dataWidth >> 2);
		const byte *srcV = dataV + (i >> 2) * (dataWidth >> 2);

		for (int j = 0; j < (width >> 2); j++, srcU++, srcV++) {
			for (int n = 0; n < 4; n++, dest++, srcY++) {
				byte dY = *srcY << 1, dU = *srcU << 1, dV = *srcV << 1;

				*dest = (dY == 0) ? 0 : dither->dither(dY, dU, dV, j * 4 + n);
			}
		}

		dither->nextLine();
		vidMem += destDesc->getWidth();
	}

	delete dither;
}	

const byte Video_v6::_ditherPalette[768] = {
	0x00,0x80,0x80,0x26,0x6B,0xC0,0x4B,0x56,0x4B,0x71,0x41,0x8B,
	0x0E,0xC0,0x76,0x34,0xAB,0xB6,0x59,0x96,0x41,0xBE,0x81,0x81,
	0xCF,0x77,0x75,0xC1,0x9B,0x6C,0x7F,0x81,0x81,0x7F,0x81,0x81,
	0x0A,0x8C,0x8A,0x0A,0xB0,0x79,0x0E,0x9D,0x76,0x0E,0x79,0x76,
	0x10,0x77,0x75,0x09,0x7B,0x8B,0x16,0x74,0xA6,0x16,0x74,0xA6,
	0x12,0x91,0x93,0x13,0xB5,0x72,0x1B,0x9E,0x6D,0x1A,0x7A,0x6D,
	0x1C,0x71,0x6C,0x1B,0x72,0x8C,0x1B,0x71,0xAE,0x1B,0x71,0xAE,
	0x21,0x93,0x93,0x21,0xB8,0x6F,0x27,0xA3,0x64,0x27,0x7F,0x65,
	0x29,0x69,0x68,0x28,0x69,0x8E,0x23,0x6F,0xB7,0x2A,0x69,0xB2,
	0x36,0x94,0x92,0x35,0xB9,0x6E,0x35,0xA6,0x5D,0x34,0x80,0x5D,
	0x39,0x61,0x6B,0x3A,0x60,0x8F,0x34,0x6E,0xB9,0x39,0x60,0xB5,
	0x46,0x93,0x95,0x48,0xB6,0x6F,0x45,0xA6,0x5C,0x47,0x80,0x5C,
	0x4B,0x56,0x6D,0x4B,0x56,0x91,0x47,0x6F,0xB7,0x4A,0x57,0xB5,
	0x5A,0x94,0x92,0x59,0xB9,0x6E,0x59,0xA6,0x5D,0x59,0x82,0x5D,
	0x5B,0x4D,0x6D,0x5A,0x4E,0x92,0x5B,0x6F,0xB7,0x5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,0x93,0x5A,0x81,0xA7,0x5A,0x5D,0xA7,
	0x47,0x81,0x81,0x47,0xA5,0x80,0x47,0x92,0x6F,0x47,0x6E,0x6F,
	0x46,0x5D,0x81,0x46,0x6F,0x95,0x49,0x81,0xA5,0x48,0x5D,0xA5,
	0x32,0x81,0x81,0x33,0xA7,0x81,0x35,0x95,0x6E,0x34,0x6F,0x6F,
	0x36,0x62,0x7E,0x35,0x6E,0x93,0x34,0x80,0xA5,0x36,0x62,0xA4,
	0x23,0x81,0x81,0x23,0xA5,0x80,0x20,0x94,0x6F,0x22,0x6F,0x6D,
	0x26,0x6B,0x7E,0x21,0x6F,0x93,0x22,0x82,0xA7,0x25,0x6C,0xA4,
	0x0E,0x81,0x81,0x0F,0xA5,0x81,0x13,0x91,0x73,0x15,0x75,0x71,
	0x14,0x75,0x7D,0x11,0x77,0x93,0x15,0x7D,0xA1,0x16,0x74,0xA1,
	0x00,0x80,0x80,0x07,0xA0,0x7B,0x05,0x8F,0x7D,0x09,0x7B,0x7A,
	0x07,0x7D,0x7B,0x07,0x7C,0x8C,0x0E,0x78,0x98,0x0E,0x78,0x98,
	0x7F,0x81,0x81,0x80,0x83,0x81,0xF7,0x7C,0x84,0x9E,0x83,0x80,
	0x7F,0x81,0x81,0x4B,0x56,0xFE,0x93,0x2D,0x17,0xDE,0x03,0x95,
	0x1C,0xFE,0x6C,0x67,0xD4,0xEA,0xAF,0xAB,0x03,0xFA,0x81,0x81
};

} // End of namespace Gob