aboutsummaryrefslogtreecommitdiff
path: root/util.cpp
blob: 2fae9cc175daa6452e726fada40f0d6e0b52cce4 (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
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2002 The ScummVM project
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header$
 */

#include "stdafx.h"
#include "util.h"

// 8-bit alpha blending routines
int BlendCache[256][256];

int RGBMatch(byte *palette, int r, int g, int b)
{
	int i, bestidx = 0, besterr = 0xFFFFFF;
	int error = 0;

	for (i = 0;i < 256;i++) {
		byte *pal = palette + (i * 3);
		int r_diff = r - (int)*pal++; 
		int g_diff = g - (int)*pal++; 
		int b_diff = b - (int)*pal++; 
		r_diff *= r_diff; g_diff *= g_diff; b_diff *= b_diff;

		error = r_diff + g_diff + b_diff;
		if (error < besterr) {
			besterr = error;
			bestidx = i;
		}
	}
	return bestidx;
}

int Blend(int src, int dst, byte *palette)
{
	int r, g, b;
	int alpha = 128;	// Level of transparency [0-256]
	byte *srcpal = palette + (dst  * 3);
	byte *dstpal = palette + (src * 3);

	if (BlendCache[dst][src] > -1)
		return BlendCache[dst][src];

	r =  (*srcpal++ * alpha);
    r += (*dstpal++ * (256-alpha));
    r /= 256;

    g =  (*srcpal++ * alpha);
    g += (*dstpal++ * (256-alpha));
    g /= 256;

    b =  (*srcpal++ * alpha);
    b += (*dstpal++  * (256-alpha));
    b /= 256;
       
	return (BlendCache[dst][src] = RGBMatch(palette, r , g , b ));
}

void ClearBlendCache(byte *palette, int weight)
{
	for (int i = 0; i < 256; i++)
		for (int j = 0 ; j < 256 ; j++)			
//			BlendCache[i][j] = i;	// No alphablending
//			BlendCache[i][j] = j;	// 100% translucent
			BlendCache[i][j] = -1;	// Enable alphablending
}


#pragma mark -

/*
 * Print hexdump of the data passed in, 8 bytes a row
 */
void hexdump(const byte * data, int len)
{
	int i;
	byte c;
	while (len >= 8) {
		for (i = 0; i < 8; i++)
			printf("%02x ", data[i]);
		printf(" |");
		for (i = 0; i < 8; i++) {
			c = data[i];
			if (c < 32 || c > 127)
				c = '.';
			printf("%c", c);
		}
		printf("|\n");
		data += 8;
		len -= 8;
	}

	if (len <= 0) 
		return;

	for (i = 0; i < len; i++)
		printf("%02x ", data[i]);
	for (; i < 8; i++)
		printf("   ");
	printf(" |");
	for (i = 0; i < len; i++) {
		c = data[i];
		if (c < 32 || c > 127)
			c = '.';
		printf("%c", c);
	}
	for (; i < 8; i++)
		printf(" ");
	printf("|\n");
}


#pragma mark -


namespace ScummVM {

String::String(const char *str)
{
	_refCount = new int(1);
	_capacity = _len = strlen(str);
	_str = (char *)calloc(1, _capacity+1);
	memcpy(_str, str, _len+1);
}

String::String(const String &str)
{
	++(*str._refCount);

	_refCount = str._refCount;
	_capacity = str._capacity;
	_len = str._capacity;
	_str = str._str;
}

String::~String()
{
	decRefCount();
}

void String::decRefCount()
{
	--(*_refCount);
	if (*_refCount <= 0) {
		delete _refCount;
		if (_str)
			free(_str);
	}
}

String& String::operator  =(const char* str)
{
	int len = strlen(str);
	if (len > 0) {
		ensureCapacity(len, false);
		
		_len = len;
		memcpy(_str, str, _len + 1);
	} else if (_len > 0) {
		decRefCount();
		
		_refCount = new int(1);
		_capacity = 0;
		_len = 0;
		_str = 0;
	}
	return *this;
}

String& String::operator  =(const String& str)
{
	++(*str._refCount);

	decRefCount();
	
	_refCount = str._refCount;
	_capacity = str._capacity;
	_len = str._len;
	_str = str._str;

	return *this;
}

String& String::operator +=(const char* str)
{
	int len = strlen(str);
	if (len > 0) {
		ensureCapacity(_len + len, true);

		memcpy(_str + _len, str, len + 1);
		_len += len;
	}
	return *this;
}

String& String::operator +=(const String& str)
{
	int len = str._len;
	if (len > 0) {
		ensureCapacity(_len + len, true);

		memcpy(_str + _len, str._str, len + 1);
		_len += len;
	}
	return *this;
}

String& String::operator +=(char c)
{
	ensureCapacity(_len + 1, true);
	
	_str[_len++] = c;
	_str[_len] = 0;

	return *this;
}

void String::deleteLastChar() {
	if (_len > 0) {
		ensureCapacity(_len - 1, true);
		_str[--_len] = 0;
	}
}

void String::clear()
{
	if (_capacity) {
		decRefCount();
		
		_refCount = new int(1);
		_capacity = 0;
		_len = 0;
		_str = 0;
	}
}

void String::ensureCapacity(int new_len, bool keep_old)
{
	// If there is not enough space, or if we are not the only owner 
	// of the current data, then we have to reallocate it.
	if (new_len <= _capacity && *_refCount == 1)
		return;

	int		newCapacity = (new_len <= _capacity) ? _capacity : new_len + 32;
	char	*newStr = (char *)calloc(1, newCapacity+1);

	if (keep_old && _str)
		memcpy(newStr, _str, _len + 1);
	else
		_len = 0;

	decRefCount();
	
	_refCount = new int(1);
	_capacity = newCapacity;
	_str = newStr;
}

};	// End of namespace ScummVM