aboutsummaryrefslogtreecommitdiff
path: root/engines/touche/resource.cpp
blob: 467d2bed9023e73a7f50c93fc05d042aa07324d5 (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
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
/* 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.
 *
 */


#include "common/textconsole.h"

#include "audio/decoders/flac.h"
#include "audio/mixer.h"
#include "audio/decoders/mp3.h"
#include "audio/decoders/voc.h"
#include "audio/decoders/vorbis.h"
#include "audio/decoders/raw.h"
#include "audio/audiostream.h"

#include "touche/midi.h"
#include "touche/touche.h"
#include "touche/graphics.h"

namespace Touche {

enum {
	kCurrentSpeechDataVersion = 1,
	kSpeechDataFileHeaderSize = 4
};

struct CompressedSpeechFile {
	const char *filename;
	Audio::SeekableAudioStream *(*makeStream)(
			Common::SeekableReadStream *stream,
			DisposeAfterUse::Flag disposeAfterUse);
};

static const CompressedSpeechFile compressedSpeechFilesTable[] = {
#ifdef USE_FLAC
	{ "TOUCHE.SOF", Audio::makeFLACStream },
#endif
#ifdef USE_VORBIS
	{ "TOUCHE.SOG", Audio::makeVorbisStream },
#endif
#ifdef USE_MAD
	{ "TOUCHE.SO3", Audio::makeMP3Stream },
#endif
	{ 0, 0 }
};

void ToucheEngine::res_openDataFile() {
	if (!_fData.open("TOUCHE.DAT")) {
		error("Unable to open 'TOUCHE.DAT' for reading");
	}
	for (int i = 0; compressedSpeechFilesTable[i].filename; ++i) {
		if (_fSpeech[0].open(compressedSpeechFilesTable[i].filename)) {
			int version = _fSpeech[0].readUint16LE();
			if (version == kCurrentSpeechDataVersion) {
				_compressedSpeechData = i;
				return;
			}
			warning("Unhandled version %d for compressed sound file '%s'", version, compressedSpeechFilesTable[i].filename);
			return;
		}
	}
	// _fSpeech[0] opening/closing is driven by the scripts
	_fSpeech[1].open("OBJ");
	_compressedSpeechData = -1;
}

void ToucheEngine::res_closeDataFile() {
	_fData.close();
	_fSpeech[0].close();
	_fSpeech[1].close();
}

void ToucheEngine::res_allocateTables() {
	_fData.seek(64);
	uint32 textDataOffs = _fData.readUint32LE();
	uint32 textDataSize = _fData.readUint32LE();
	_textData = (uint8 *)malloc(textDataSize);
	if (!_textData) {
		error("Unable to allocate memory for text data");
	}
	_fData.seek(textDataOffs);
	_fData.read(_textData, textDataSize);

	_fData.seek(2);
	const int bw = _fData.readUint16LE();
	const int bh = _fData.readUint16LE();
	uint32 size = bw * bh;
	_backdropBuffer = (uint8 *)malloc(size);
	if (!_backdropBuffer) {
		error("Unable to allocate memory for backdrop buffer");
	}

	_menuKitData = (uint8 *)malloc(42 * 120);
	if (!_menuKitData) {
		error("Unable to allocate memory for menu kit data");
	}

	_convKitData = (uint8 *)malloc(152 * 80);
	if (!_convKitData) {
		error("Unable to allocate memory for conv kit data");
	}

	for (int i = 0; i < NUM_SEQUENCES; ++i) {
		_sequenceDataTable[i] = (uint8 *)malloc(16384);
		if (!_sequenceDataTable[i]) {
			error("Unable to allocate memory for sequence data %d", i);
		}
	}

	_programData = (uint8 *)malloc(kMaxProgramDataSize);
	if (!_programData) {
		error("Unable to allocate memory for program data");
	}

	_mouseData = (uint8 *)malloc(kCursorWidth * kCursorHeight);
	if (!_mouseData) {
		error("Unable to allocate memory for mouse data");
	}

	_iconData = (uint8 *)malloc(kIconWidth * kIconHeight);
	if (!_iconData) {
		error("Unable to allocate memory for object data");
	}

	memset(_spritesTable, 0, sizeof(_spritesTable));

	_offscreenBuffer = (uint8 *)malloc(kScreenWidth * kScreenHeight);
	if (!_offscreenBuffer) {
		error("Unable to allocate memory for offscreen buffer");
	}
}

void ToucheEngine::res_deallocateTables() {
	free(_textData);
	_textData = nullptr;

	free(_backdropBuffer);
	_backdropBuffer = nullptr;

	free(_menuKitData);
	_menuKitData = nullptr;

	free(_convKitData);
	_convKitData = nullptr;

	for (int i = 0; i < NUM_SEQUENCES; ++i) {
		free(_sequenceDataTable[i]);
		_sequenceDataTable[i] = nullptr;
	}

	free(_programData);
	_programData = nullptr;

	free(_mouseData);
	_mouseData = nullptr;

	free(_iconData);
	_iconData = nullptr;

	for (int i = 0; i < NUM_SPRITES; ++i) {
		free(_spritesTable[i].ptr);
		_spritesTable[i].ptr = nullptr;
	}

	free(_offscreenBuffer);
	_offscreenBuffer = nullptr;
}

uint32 ToucheEngine::res_getDataOffset(ResourceType type, int num, uint32 *size) {
	debugC(9, kDebugResource, "ToucheEngine::res_getDataOffset() type=%d num=%d", type, num);
	static const struct ResourceData {
		int offs;
		int count;
		int type;
	} dataTypesTable[] = {
		{ 0x048, 100, kResourceTypeRoomImage   },
		{ 0x228,  30, kResourceTypeSequence    },
		{ 0x2A0,  50, kResourceTypeSpriteImage },
		{ 0x390, 100, kResourceTypeIconImage   },
		{ 0x6B0,  80, kResourceTypeRoomInfo    },
		{ 0x908, 150, kResourceTypeProgram     },
		{ 0xB60,  50, kResourceTypeMusic       },
		{ 0xC28, 120, kResourceTypeSound       }
	};

	const ResourceData *rd = NULL;
	for (unsigned int i = 0; i < ARRAYSIZE(dataTypesTable); ++i) {
		if (dataTypesTable[i].type == type) {
			rd = &dataTypesTable[i];
			break;
		}
	}
	if (rd == NULL) {
		error("Invalid resource type %d", type);
	}
	if (num < 0 || num > rd->count) {
		error("Invalid resource number %d (type %d)", num, type);
	}
	_fData.seek(rd->offs + num * 4);
	uint32 offs = _fData.readUint32LE();
	assert(offs != 0);
	if (size) {
		uint32 nextOffs = _fData.readUint32LE();
		*size = nextOffs - offs;
	}
	return offs;
}

void ToucheEngine::res_loadSpriteImage(int num, uint8 *dst) {
	debugC(9, kDebugResource, "ToucheEngine::res_loadSpriteImage() num=%d", num);
	const uint32 offs = res_getDataOffset(kResourceTypeSpriteImage, num);
	_fData.seek(offs);
	_currentImageWidth = _fData.readUint16LE();
	_currentImageHeight = _fData.readUint16LE();
	for (int i = 0; i < _currentImageHeight; ++i) {
		res_decodeScanLineImageRLE(dst + _currentImageWidth * i, _currentImageWidth);
	}
}

void ToucheEngine::res_loadProgram(int num) {
	debugC(9, kDebugResource, "ToucheEngine::res_loadProgram() num=%d", num);
	const uint32 offs = res_getDataOffset(kResourceTypeProgram, num, &_programDataSize);
	_fData.seek(offs);
	assert(_programDataSize <= kMaxProgramDataSize);
	_fData.read(_programData, _programDataSize);
}

void ToucheEngine::res_decodeProgramData() {
	debugC(9, kDebugResource, "ToucheEngine::res_decodeProgramData()");

	uint8 *p;
	uint8 *programDataEnd = _programData + _programDataSize;

	p = _programData + READ_LE_UINT32(_programData + 32);
	_script.init(p);

	p = _programData + READ_LE_UINT32(_programData + 4);
	_programTextDataPtr = p;

	_programRectsTable.clear();
	p = _programData + READ_LE_UINT32(_programData + 20);
	while (p < programDataEnd) {
		int16 x = READ_LE_UINT16(p); p += 2;
		int16 y = READ_LE_UINT16(p); p += 2;
		int16 w = READ_LE_UINT16(p); p += 2;
		int16 h = READ_LE_UINT16(p); p += 2;
		_programRectsTable.push_back(Common::Rect(x, y, x + w, y + h));
		if (x == -1) {
			break;
		}
	}

	_programPointsTable.clear();
	p = _programData + READ_LE_UINT32(_programData + 24);
	while (p < programDataEnd) {
		ProgramPointData ppd;
		ppd.x = READ_LE_UINT16(p); p += 2;
		ppd.y = READ_LE_UINT16(p); p += 2;
		ppd.z = READ_LE_UINT16(p); p += 2;
		ppd.order = READ_LE_UINT16(p); p += 2;
		_programPointsTable.push_back(ppd);
		if (ppd.x == -1) {
			break;
		}
	}

	_programWalkTable.clear();
	p = _programData + READ_LE_UINT32(_programData + 28);
	while (p < programDataEnd) {
		ProgramWalkData pwd;
		pwd.point1 = READ_LE_UINT16(p); p += 2;
		if (pwd.point1 == -1) {
			break;
		}
		assert((uint16)pwd.point1 < _programPointsTable.size());
		pwd.point2 = READ_LE_UINT16(p); p += 2;
		assert((uint16)pwd.point2 < _programPointsTable.size());
		pwd.clippingRect = READ_LE_UINT16(p); p += 2;
		pwd.area1 = READ_LE_UINT16(p); p += 2;
		pwd.area2 = READ_LE_UINT16(p); p += 2;
		p += 12; // unused
		_programWalkTable.push_back(pwd);
	}

	_programAreaTable.clear();
	p = _programData + READ_LE_UINT32(_programData + 8);
	while (p < programDataEnd) {
		ProgramAreaData pad;
		int16 x = READ_LE_UINT16(p); p += 2;
		if (x == -1) {
			break;
		}
		int16 y = READ_LE_UINT16(p); p += 2;
		int16 w = READ_LE_UINT16(p); p += 2;
		int16 h = READ_LE_UINT16(p); p += 2;
		pad.area.r = Common::Rect(x, y, x + w, y + h);
		pad.area.srcX = READ_LE_UINT16(p); p += 2;
		pad.area.srcY = READ_LE_UINT16(p); p += 2;
		pad.id = READ_LE_UINT16(p); p += 2;
		pad.state = READ_LE_UINT16(p); p += 2;
		pad.animCount = READ_LE_UINT16(p); p += 2;
		pad.animNext = READ_LE_UINT16(p); p += 2;
		_programAreaTable.push_back(pad);
	}

	_programBackgroundTable.clear();
	p = _programData + READ_LE_UINT32(_programData + 12);
	while (p < programDataEnd) {
		ProgramBackgroundData pbd;
		int16 x = READ_LE_UINT16(p); p += 2;
		if (x == -1) {
			break;
		}
		int16 y = READ_LE_UINT16(p); p += 2;
		int16 w = READ_LE_UINT16(p); p += 2;
		int16 h = READ_LE_UINT16(p); p += 2;
		pbd.area.r = Common::Rect(x, y, x + w, y + h);
		pbd.area.srcX = READ_LE_UINT16(p); p += 2;
		pbd.area.srcY = READ_LE_UINT16(p); p += 2;
		pbd.type = READ_LE_UINT16(p); p += 2;
		pbd.offset = READ_LE_UINT16(p); p += 2;
		pbd.scaleMul = READ_LE_UINT16(p); p += 2;
		pbd.scaleDiv = READ_LE_UINT16(p); p += 2;
		_programBackgroundTable.push_back(pbd);
	}

	_programHitBoxTable.clear();
	p = _programData + READ_LE_UINT32(_programData + 16);
	while (p < programDataEnd) {
		ProgramHitBoxData phbd;
		phbd.item = READ_LE_UINT16(p); p += 2;
		if (phbd.item == 0) {
			break;
		}
		phbd.talk = READ_LE_UINT16(p); p += 2;
		phbd.state = READ_LE_UINT16(p); p += 2;
		phbd.str = READ_LE_UINT16(p); p += 2;
		phbd.defaultStr = READ_LE_UINT16(p); p += 2;
		for (int i = 0; i < 8; ++i) {
			phbd.actions[i] = READ_LE_UINT16(p); p += 2;
		}
		for (int i = 0; i < 2; ++i) {
			int16 x = READ_LE_UINT16(p); p += 2;
			int16 y = READ_LE_UINT16(p); p += 2;
			int16 w = READ_LE_UINT16(p); p += 2;
			int16 h = READ_LE_UINT16(p); p += 2;
			phbd.hitBoxes[i].left = x;
			phbd.hitBoxes[i].top = y;
			phbd.hitBoxes[i].right = x + w;
			phbd.hitBoxes[i].bottom = y + h;
		}
		p += 8; // unused
		_programHitBoxTable.push_back(phbd);
	}

	_programActionScriptOffsetTable.clear();
	p = _programData + READ_LE_UINT32(_programData + 36);
	while (p < programDataEnd) {
		ProgramActionScriptOffsetData pasod;
		pasod.object1 = READ_LE_UINT16(p); p += 2;
		if (pasod.object1 == 0) {
			break;
		}
		pasod.action = READ_LE_UINT16(p); p += 2;
		pasod.object2 = READ_LE_UINT16(p); p += 2;
		pasod.offset = READ_LE_UINT16(p); p += 2;
		_programActionScriptOffsetTable.push_back(pasod);
	}

	_programConversationTable.clear();
	int count = (READ_LE_UINT32(_programData + 44) - READ_LE_UINT32(_programData + 40)) / 6;
	assert(count >= 0);
	p = _programData + READ_LE_UINT32(_programData + 40);
	while (p < programDataEnd && count != 0) {
		ProgramConversationData pcd;
		pcd.num = READ_LE_UINT16(p); p += 2;
		pcd.offset = READ_LE_UINT16(p); p += 2;
		pcd.msg = READ_LE_UINT16(p); p += 2;
		_programConversationTable.push_back(pcd);
		--count;
	}

	_programKeyCharScriptOffsetTable.clear();
	p = _programData + READ_LE_UINT32(_programData + 44);
	while (p < programDataEnd) {
		ProgramKeyCharScriptOffsetData pksod;
		pksod.keyChar = READ_LE_UINT16(p); p += 2;
		if (pksod.keyChar == 0) {
			break;
		}
		pksod.offset = READ_LE_UINT16(p); p += 2;
		_programKeyCharScriptOffsetTable.push_back(pksod);
	}
}

void ToucheEngine::res_loadRoom(int num) {
	debugC(9, kDebugResource, "ToucheEngine::res_loadRoom() num=%d flag115=%d", num, _flagsTable[115]);

	debug(0, "Setting up room %d", num);

	const uint32 offsInfo = res_getDataOffset(kResourceTypeRoomInfo, num);
	_fData.seek(offsInfo);
	_fData.skip(2);
	const int roomImageNum = _fData.readUint16LE();
	_fData.skip(2);
	_fData.read(_paletteBuffer, 3 * 256);

	const uint32 offsImage = res_getDataOffset(kResourceTypeRoomImage, roomImageNum);
	_fData.seek(offsImage);
	res_loadBackdrop();

	bool updateScreenPalette = _flagsTable[115] == 0;

	// Workaround to what appears to be a scripting bug. Scripts 27 and 100 triggers
	// a palette fading just after loading a room. Catch this, so that only *one*
	// palette refresh occurs.
	if ((_currentEpisodeNum == 27 && _currentRoomNum == 56 && num == 34) ||
	    (_currentEpisodeNum == 100 && _currentRoomNum == 2 && num == 1)) {
		updateScreenPalette = false;
	}

	if (updateScreenPalette) {
		updatePalette();
	} else {
		setPalette(0, 255, 0, 0, 0);
	}

	_currentRoomNum = num;
	_updatedRoomAreasTable[0] = 1;

	_fullRedrawCounter = 1;
	_roomNeedRedraw = true;

	_sequenceEntryTable[5].sprNum = -1;
	_sequenceEntryTable[5].seqNum = -1;
	_sequenceEntryTable[6].sprNum = -1;
	_sequenceEntryTable[6].seqNum = -1;
}

void ToucheEngine::res_loadSprite(int num, int index) {
	debugC(9, kDebugResource, "ToucheEngine::res_loadSprite() num=%d index=%d", num, index);
	assert(index >= 0 && index < NUM_SEQUENCES);
	_sequenceEntryTable[index].sprNum = num;
	SpriteData *spr = &_spritesTable[index];
	const uint32 offs = res_getDataOffset(kResourceTypeSpriteImage, num);
	_fData.seek(offs);
	_currentImageWidth = _fData.readUint16LE();
	_currentImageHeight = _fData.readUint16LE();
	const uint32 size = _currentImageWidth * _currentImageHeight;
	if (size > spr->size) {
		debug(8, "Reallocating memory for sprite %d (index %d), %d bytes needed", num, index, size - spr->size);
		spr->size = size;

		uint8 *buffer = NULL;
		if (spr->ptr)
			buffer = (uint8 *)realloc(spr->ptr, size);

		if (!buffer) {
			// Free previously allocated sprite (when realloc failed)
			free(spr->ptr);

			buffer = (uint8 *)malloc(size);
		}

		if (!buffer)
			error("[ToucheEngine::res_loadSprite] Unable to reallocate memory for sprite %d (%d bytes)", num, size);

		spr->ptr = buffer;
	}
	for (int i = 0; i < _currentImageHeight; ++i) {
		res_decodeScanLineImageRLE(spr->ptr + _currentImageWidth * i, _currentImageWidth);
	}
	spr->bitmapWidth = _currentImageWidth;
	spr->bitmapHeight = _currentImageHeight;
	if (_flagsTable[268] == 0) {
		res_loadImageHelper(spr->ptr, _currentImageWidth, _currentImageHeight);
	}
	spr->w = _currentImageWidth;
	spr->h = _currentImageHeight;
}

void ToucheEngine::res_loadSequence(int num, int index) {
	debugC(9, kDebugResource, "ToucheEngine::res_loadSequence() num=%d index=%d", num, index);
	assert(index < NUM_SEQUENCES);
	_sequenceEntryTable[index].seqNum = num;
	const uint32 offs = res_getDataOffset(kResourceTypeSequence, num);
	_fData.seek(offs);
	_fData.read(_sequenceDataTable[index], 16000);
}

void ToucheEngine::res_decodeScanLineImageRLE(uint8 *dst, int lineWidth) {
	int w = 0;
	while (w < lineWidth) {
		uint8 code = _fData.readByte();
		if ((code & 0xC0) == 0xC0) {
			int len = code & 0x3F;
			uint8 color = _fData.readByte();
			memset(dst, color, len);
			dst += len;
			w += len;
		} else {
			*dst = code;
			++dst;
			++w;
		}
	}
}

void ToucheEngine::res_loadBackdrop() {
	debugC(9, kDebugResource, "ToucheEngine::res_loadBackdrop()");
	_currentBitmapWidth = _fData.readUint16LE();
	_currentBitmapHeight = _fData.readUint16LE();
	for (int i = 0; i < _currentBitmapHeight; ++i) {
		res_decodeScanLineImageRLE(_backdropBuffer + _currentBitmapWidth * i, _currentBitmapWidth);
	}
	_roomWidth = _currentBitmapWidth;
	for (int i = 0; i < _currentBitmapWidth; ++i) {
		if (_backdropBuffer[i] == 255) {
			_roomWidth = i;
			_backdropBuffer[i] = 0;
			break;
		}
	}
	// Workaround for bug #1751149 (original bitmap has a white pixel in its transparent area).
	if (_currentRoomNum == 8 && _currentBitmapWidth == 860) {
		_backdropBuffer[120 * _currentBitmapWidth + 734] = 0;
	}
}

void ToucheEngine::res_loadImage(int num, uint8 *dst) {
	debugC(9, kDebugResource, "ToucheEngine::res_loadImage() num=%d", num);
	const uint32 offsInfo = res_getDataOffset(kResourceTypeIconImage, num);
	_fData.seek(offsInfo);
	_currentImageWidth = _fData.readUint16LE();
	_currentImageHeight = _fData.readUint16LE();
	for (int i = 0; i < _currentImageHeight; ++i) {
		res_decodeScanLineImageRLE(dst + _currentImageWidth * i, _currentImageWidth);
	}
	res_loadImageHelper(dst, _currentImageWidth, _currentImageHeight);
}

void ToucheEngine::res_loadImageHelper(uint8 *imgData, int imgWidth, int imgHeight) {
	uint8 *p = imgData;
	for (_currentImageHeight = 0; _currentImageHeight < imgHeight; ++_currentImageHeight, p += imgWidth) {
		if (*p == 64 || *p == 255) {
			break;
		}
	}
	p = imgData;
	for (_currentImageWidth = 0; _currentImageWidth < imgWidth; ++_currentImageWidth, ++p) {
		if (*p == 64 || *p == 255) {
			break;
		}
	}
	if (_flagsTable[267] == 0) {
		for (int i = 0; i < imgWidth * imgHeight; ++i) {
			uint8 color = imgData[i];
			if (color != 0) {
				if (color < 64) {
					color += 192;
				} else {
					color = 0;
				}
			}
			imgData[i] = color;
		}
	}
}

void ToucheEngine::res_loadSound(int priority, int num) {
	debugC(9, kDebugResource, "ToucheEngine::res_loadSound() num=%d", num);
	if (priority >= 0) {
		uint32 size;
		const uint32 offs = res_getDataOffset(kResourceTypeSound, num, &size);
		Common::SeekableReadStream *datastream = SearchMan.createReadStreamForMember("TOUCHE.DAT");
		if (!datastream) {
			warning("res_loadSound: Could not open TOUCHE.DAT");
			return;
		}

		datastream->seek(offs);
		Audio::AudioStream *stream = Audio::makeVOCStream(datastream, Audio::FLAG_UNSIGNED, DisposeAfterUse::YES);
		if (stream) {
			_mixer->playStream(Audio::Mixer::kSFXSoundType, &_sfxHandle, stream);
		}
	}
}

void ToucheEngine::res_stopSound() {
	_mixer->stopHandle(_sfxHandle);
}

void ToucheEngine::res_loadMusic(int num) {
	debugC(9, kDebugResource, "ToucheEngine::res_loadMusic() num=%d", num);
	startMusic(num);
}

void ToucheEngine::res_loadSpeech(int num) {
	debugC(9, kDebugResource, "ToucheEngine::res_loadSpeech() num=%d", num);
	if (num == -1) {
		res_stopSpeech();
	} else {
		if (_compressedSpeechData < 0) { // uncompressed speech data
			if (_fSpeech[0].isOpen()) {
				_fSpeech[0].close();
			}
			char filename[10];
			sprintf(filename, "V%d", num);
			_fSpeech[0].open(filename);
		}
		if (_fSpeech[0].isOpen()) {
			_flagsTable[617] = num;
		}
	}
}

void ToucheEngine::res_loadSpeechSegment(int num) {
	debugC(9, kDebugResource, "ToucheEngine::res_loadSpeechSegment() num=%d", num);
	if (_talkTextMode != kTalkModeTextOnly && _flagsTable[617] != 0) {
		Audio::AudioStream *stream = 0;
		if (_compressedSpeechData < 0) { // uncompressed speech data
			int i = 0;
			if (num >= 750) {
				num -= 750;
				i = 1;
			}
			if (!_fSpeech[i].isOpen()) {
				return;
			}
			_fSpeech[i].seek(num * 8);
			uint32 offs = _fSpeech[i].readUint32LE();
			uint32 size = _fSpeech[i].readUint32LE();
			if (size == 0) {
				return;
			}
			_fSpeech[i].seek(offs);
			stream = Audio::makeVOCStream(&_fSpeech[i], Audio::FLAG_UNSIGNED);
		} else {
			if (num >= 750) {
				num -= 750;
				_fSpeech[0].seek(kSpeechDataFileHeaderSize);
			} else {
				assert(_flagsTable[617] > 0 && _flagsTable[617] < 140);
				_fSpeech[0].seek(kSpeechDataFileHeaderSize + _flagsTable[617] * 4);
			}
			uint32 dataOffs = _fSpeech[0].readUint32LE();
			if (dataOffs == 0) {
				return;
			}
			_fSpeech[0].seek(dataOffs + num * 8);
			uint32 offs = _fSpeech[0].readUint32LE();
			uint32 size = _fSpeech[0].readUint32LE();
			if (size == 0) {
				return;
			}
			_fSpeech[0].seek(offs);
			Common::SeekableReadStream *tmp = _fSpeech[0].readStream(size);
			if (tmp)
				stream = (compressedSpeechFilesTable[_compressedSpeechData].makeStream)(tmp, DisposeAfterUse::YES);
		}
		if (stream) {
			_speechPlaying = true;
			_mixer->playStream(Audio::Mixer::kSpeechSoundType, &_speechHandle, stream);
		}
	}
}

void ToucheEngine::res_stopSpeech() {
	debugC(9, kDebugResource, "ToucheEngine::res_stopSpeech()");
	_mixer->stopHandle(_speechHandle);
	_speechPlaying = false;
}

} // namespace Touche