aboutsummaryrefslogtreecommitdiff
path: root/engines/sci/gfx/gfx_tools.cpp
blob: db6fa7f834e3a43ee869ccb10c2c17a23876a0c7 (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
/* 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 "sci/sci_memory.h"
#include "sci/gfx/gfx_tools.h"

namespace Sci {

rect_t gfx_rect_fullscreen = {0, 0, 320, 200};

void gfx_clip_box_basic(rect_t *box, int maxx, int maxy) {
	if (box->x < 0)
		box->x = 0;

	if (box->y < 0)
		box->y = 0;

	if (box->x + box->xl > maxx)
		box->xl = maxx - box->x + 1;

	if (box->y + box->yl > maxy)
		box->yl = maxy - box->y + 1;
}

gfx_mode_t *gfx_new_mode(int xfact, int yfact, const Graphics::PixelFormat &format, int palette, int flags) {
	gfx_mode_t *mode = (gfx_mode_t *)sci_malloc(sizeof(gfx_mode_t));

	mode->xfact = xfact;
	mode->yfact = yfact;
	mode->bytespp = format.bytesPerPixel;
	
	// FIXME: I am not sure whether the following assignments are quite right.
	// The only code using these are the built-in scalers of the SCI engine.
	// And those are pretty weird, so I am not sure I interpreted them correctly.
	// They also seem somewhat inefficient and and should probably just be
	// replaced resp. rewritten from scratch.
	mode->red_mask = format.ARGBToColor(0, 0xFF, 0, 0);
	mode->green_mask = format.ARGBToColor(0, 0, 0xFF, 0);
	mode->blue_mask = format.ARGBToColor(0, 0, 0, 0xFF);
	mode->alpha_mask = format.ARGBToColor(0xFF, 0, 0, 0);
	mode->red_shift = format.rLoss;
	mode->green_shift = format.gLoss;
	mode->blue_shift = format.bLoss;
	mode->alpha_shift = format.aLoss;
	mode->flags = flags;

	if (palette) {
		mode->palette = (gfx_palette_t *)sci_malloc(sizeof(gfx_palette_t));
		mode->palette->max_colors_nr = palette;
		mode->palette->colors = (gfx_palette_color_t *)sci_calloc(sizeof(gfx_palette_color_t), palette); // Initialize with empty entries
	} else
		mode->palette = NULL;

	return mode;
}

void gfx_free_mode(gfx_mode_t *mode) {
	if (mode->palette) {
		free(mode->palette->colors);
		free(mode->palette);
	}
	free(mode);
}

void gfx_copy_pixmap_box_i(gfx_pixmap_t *dest, gfx_pixmap_t *src, rect_t box) {
	int width, height;
	int offset;

	if ((dest->index_xl != src->index_xl) || (dest->index_yl != src->index_yl))
		return;

	gfx_clip_box_basic(&box, dest->index_xl, dest->index_yl);

	if (box.xl <= 0 || box.yl <= 0)
		return;

	height = box.yl;
	width = box.xl;

	offset = box.x + (box.y * dest->index_xl);

	while (height--) {
		memcpy(dest->index_data + offset, src->index_data + offset, width);
		offset += dest->index_xl;
	}
}

gfx_pixmap_t *gfx_clone_pixmap(gfx_pixmap_t *pxm, gfx_mode_t *mode) {
	gfx_pixmap_t *clone = (gfx_pixmap_t *)sci_malloc(sizeof(gfx_pixmap_t));
	*clone = *pxm;
	clone->index_data = NULL;
	clone->colors = NULL;
	clone->data = NULL;
	gfx_pixmap_alloc_data(clone, mode);

	memcpy(clone->data, pxm->data, clone->data_size);
	if (clone->alpha_map) {
		clone->alpha_map = (byte *) sci_malloc(clone->xl * clone->yl);
		memcpy(clone->alpha_map, pxm->alpha_map, clone->xl * clone->yl);
	}

	return clone;
}

gfx_pixmap_t *gfx_new_pixmap(int xl, int yl, int resid, int loop, int cel) {
	gfx_pixmap_t *pxm = (gfx_pixmap_t *)sci_malloc(sizeof(gfx_pixmap_t));

	pxm->alpha_map = NULL;
	pxm->data = NULL;
	pxm->internal.info = NULL;
	pxm->colors = NULL;
	pxm->internal.handle = 0;

	pxm->index_xl = xl;
	pxm->index_yl = yl;

	pxm->ID = resid;
	pxm->loop = loop;
	pxm->cel = cel;

	pxm->index_data = NULL;

	pxm->flags = 0;

	pxm->color_key = 0xff;

	return pxm;
}

void gfx_free_pixmap(gfx_driver_t *driver, gfx_pixmap_t *pxm) {
	if (driver) {
		if (driver->mode->palette && pxm->flags & GFX_PIXMAP_FLAG_PALETTE_ALLOCATED
		        && !(pxm->flags & GFX_PIXMAP_FLAG_DONT_UNALLOCATE_PALETTE) && !(pxm->flags & GFX_PIXMAP_FLAG_EXTERNAL_PALETTE)) {
			int i;
			int error = 0;

			GFXDEBUG("UNALLOCATING %d\n", pxm->colors_nr);
			for (i = 0; i < pxm->colors_nr; i++)
				if (gfx_free_color(driver->mode->palette, pxm->colors + i))
					error++;

			if (error) {
				GFXWARN("%d errors occured while freeing %d colors of pixmap with ID %06x/%d/%d\n",
				        error, pxm->colors_nr, pxm->ID, pxm->loop, pxm->cel);
			}
		}
	}

	if (pxm->index_data)
		free(pxm->index_data);

	if (pxm->alpha_map)
		free(pxm->alpha_map);

	if (pxm->data)
		free(pxm->data);

	if (pxm->colors && !(pxm->flags & GFX_PIXMAP_FLAG_EXTERNAL_PALETTE))
		free(pxm->colors);

	free(pxm);
}

gfx_pixmap_t *gfx_pixmap_alloc_index_data(gfx_pixmap_t *pixmap) {
	int size;

	if (pixmap->index_data) {
		GFXWARN("Attempt to allocate pixmap index data twice!\n");
		return pixmap;
	}

	size = pixmap->index_xl * pixmap->index_yl;
	if (!size)
		size = 1;

	pixmap->index_data = (byte*)sci_malloc(size);

	memset(pixmap->index_data, 0, size);

	return pixmap;
}

gfx_pixmap_t *gfx_pixmap_free_index_data(gfx_pixmap_t *pixmap) {
	if (!pixmap->index_data) {
		GFXWARN("Attempt to free pixmap index data twice!\n");
		return pixmap;
	}

	free(pixmap->index_data);
	pixmap->index_data = NULL;
	return pixmap;
}

gfx_pixmap_t *gfx_pixmap_alloc_data(gfx_pixmap_t *pixmap, gfx_mode_t *mode) {
	int size;

	if (pixmap->data) {
		GFXWARN("Attempt to allocate pixmap data twice!\n");
		return pixmap;
	}

	if (pixmap->flags & GFX_PIXMAP_FLAG_SCALED_INDEX) {
		pixmap->xl = pixmap->index_xl;
		pixmap->yl = pixmap->index_yl;
	} else {
		pixmap->xl = pixmap->index_xl * mode->xfact;
		pixmap->yl = pixmap->index_yl * mode->yfact;
	}

	size = pixmap->xl * pixmap->yl * mode->bytespp;
	if (!size)
		size = 1;

	pixmap->data = (byte*)sci_malloc(pixmap->data_size = size);
	return pixmap;
}

gfx_pixmap_t *gfx_pixmap_free_data(gfx_pixmap_t *pixmap) {
	if (!pixmap->data) {
		GFXWARN("Attempt to free pixmap data twice!\n");
		return pixmap;
	}

	free(pixmap->data);
	pixmap->data = NULL;

	return pixmap;
}

int gfx_alloc_color(gfx_palette_t *pal, gfx_pixmap_color_t *color) {
	int i;
	int dr, dg, db;
	int bestdelta = 1 + ((0x100 * 0x100) * 3);
	int bestcolor = -1;
	int firstfree = -1;

	if (pal == NULL)
		return GFX_OK;

	if (pal->max_colors_nr <= 0) {
		GFXERROR("Palette has zero or less color entries!\n");
		return GFX_ERROR;
	}


	if (color->global_index != GFX_COLOR_INDEX_UNMAPPED) {
#if 0
		GFXDEBUG("Attempt to allocate color twice: index 0x%d (%02x/%02x/%02x)!\n",
		         color->global_index, color->r, color->g, color->b);
#endif
		return GFX_OK;
	}

	for (i = 0; i < pal->max_colors_nr; i++) {
		gfx_palette_color_t *pal_color = pal->colors + i;

		if (pal_color->lockers) {
			int delta;

			dr = abs(pal_color->r - color->r);
			dg = abs(pal_color->g - color->g);
			db = abs(pal_color->b - color->b);

			if (dr == 0 && dg == 0 && db == 0) {
				color->global_index = i;
				return GFX_OK;
			}

			delta = (dr * dr) + (dg * dg) + (db * db);
			if (delta < bestdelta) {
				bestdelta = delta;
				bestcolor = i;
			}
		} else
			if (firstfree == -1)
				firstfree = i;
	}

	if (firstfree != -1) {
		pal->colors[firstfree].r = color->r;
		pal->colors[firstfree].g = color->g;
		pal->colors[firstfree].b = color->b;
		pal->colors[firstfree].lockers = 1;
		color->global_index = firstfree;

		return 42; // positive value to indicate that this color still needs to be set
	}

	color->global_index = bestcolor;

	//GFXWARN("Out of palette colors- doing approximated mapping");
	return GFX_OK;
}

int gfx_free_color(gfx_palette_t *pal, gfx_pixmap_color_t *color) {
	gfx_palette_color_t *palette_color = pal->colors + color->global_index;

	if (!pal)
		return GFX_OK;

	if (color->global_index == GFX_COLOR_INDEX_UNMAPPED) {
		GFXWARN("Attempt to free unmapped color %02x/%02x/%02x!\n", color->r, color->g, color->b);
		BREAKPOINT();
		return GFX_ERROR;
	}

	if (color->global_index >= pal->max_colors_nr) {
		GFXERROR("Attempt to free invalid color index %d (%02x/%02x/%02x)\n", color->global_index, color->r, color->g, color->b);
		return GFX_ERROR;
	}

	if (!palette_color->lockers) {
		GFXERROR("Attempt to free unused color index %d (%02x/%02x/%02x)\n", color->global_index, color->r, color->g, color->b);
		return GFX_ERROR;
	}

	if (palette_color->lockers != GFX_COLOR_SYSTEM)
		--(palette_color->lockers);

	color->global_index = GFX_COLOR_INDEX_UNMAPPED;

	return GFX_OK;
}

gfx_pixmap_t *gfx_pixmap_scale_index_data(gfx_pixmap_t *pixmap, gfx_mode_t *mode) {
	byte *old_data, *new_data, *initial_new_data;
	byte *linestart;
	int linewidth;
	int xl, yl;
	int i, yc;
	int xfact = mode->xfact;
	int yfact = mode->yfact;

	if (xfact == 1 && yfact == 1)
		return pixmap;

	if (!pixmap)
		return NULL;

	if (pixmap->flags & GFX_PIXMAP_FLAG_SCALED_INDEX)
		return pixmap; // Already done

	old_data = pixmap->index_data;

	if (!old_data) {
		GFXERROR("Attempt to scale index data without index data!\n");
		return pixmap;
	}

	xl = pixmap->index_xl;
	yl = pixmap->index_yl;
	linewidth = xfact * xl;
	initial_new_data = new_data = (byte *)sci_malloc(linewidth * yfact * yl);

	for (yc = 0; yc < yl; yc++) {

		linestart = new_data;

		if (xfact == 1) {
			memcpy(new_data, old_data, linewidth);
			new_data += linewidth;
			old_data += linewidth;
		} else for (i = 0; i < xl; i++) {
				byte fillc = *old_data++;
				memset(new_data, fillc, xfact);
				new_data += xfact;
			}

		for (i = 1; i < yfact; i++) {
			memcpy(new_data, linestart, linewidth);
			new_data += linewidth;
		}
	}

	free(pixmap->index_data);
	pixmap->index_data = initial_new_data;

	pixmap->flags |= GFX_PIXMAP_FLAG_SCALED_INDEX;

	pixmap->index_xl = linewidth;
	pixmap->index_yl *= yfact;

	return pixmap;
}

} // End of namespace Sci