1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "common/algorithm.h"
24 #include "common/endian.h"
25 #include "common/util.h"
26 #include "common/rect.h"
27 #include "common/textconsole.h"
28 #include "graphics/primitives.h"
29 #include "graphics/surface.h"
30 #include "graphics/conversion.h"
31 
32 namespace Graphics {
33 
34 template<typename T>
plotPoint(int x,int y,int color,void * data)35 static void plotPoint(int x, int y, int color, void *data) {
36 	Surface *s = (Surface *)data;
37 	if (x >= 0 && x < s->w && y >= 0 && y < s->h) {
38 		T *ptr = (T *)s->getBasePtr(x, y);
39 		*ptr = (T)color;
40 	}
41 }
42 
drawLine(int x0,int y0,int x1,int y1,uint32 color)43 void Surface::drawLine(int x0, int y0, int x1, int y1, uint32 color) {
44 	if (format.bytesPerPixel == 1)
45 		Graphics::drawLine(x0, y0, x1, y1, color, plotPoint<byte>, this);
46 	else if (format.bytesPerPixel == 2)
47 		Graphics::drawLine(x0, y0, x1, y1, color, plotPoint<uint16>, this);
48 	else if (format.bytesPerPixel == 4)
49 		Graphics::drawLine(x0, y0, x1, y1, color, plotPoint<uint32>, this);
50 	else
51 		error("Surface::drawLine: bytesPerPixel must be 1, 2, or 4");
52 }
53 
drawThickLine(int x0,int y0,int x1,int y1,int penX,int penY,uint32 color)54 void Surface::drawThickLine(int x0, int y0, int x1, int y1, int penX, int penY, uint32 color) {
55 	if (format.bytesPerPixel == 1)
56 		Graphics::drawThickLine(x0, y0, x1, y1, penX, penY, color, plotPoint<byte>, this);
57 	else if (format.bytesPerPixel == 2)
58 		Graphics::drawThickLine(x0, y0, x1, y1, penX, penY, color, plotPoint<uint16>, this);
59 	else if (format.bytesPerPixel == 4)
60 		Graphics::drawThickLine(x0, y0, x1, y1, penX, penY, color, plotPoint<uint32>, this);
61 	else
62 		error("Surface::drawThickLine: bytesPerPixel must be 1, 2, or 4");
63 }
64 
create(uint16 width,uint16 height,const PixelFormat & f)65 void Surface::create(uint16 width, uint16 height, const PixelFormat &f) {
66 	free();
67 
68 	w = width;
69 	h = height;
70 	format = f;
71 	pitch = w * format.bytesPerPixel;
72 
73 	if (width && height) {
74 		pixels = calloc(width * height, format.bytesPerPixel);
75 		assert(pixels);
76 	}
77 }
78 
free()79 void Surface::free() {
80 	::free(pixels);
81 	pixels = 0;
82 	w = h = pitch = 0;
83 	format = PixelFormat();
84 }
85 
init(uint16 width,uint16 height,uint16 newPitch,void * newPixels,const PixelFormat & f)86 void Surface::init(uint16 width, uint16 height, uint16 newPitch, void *newPixels, const PixelFormat &f) {
87 	w = width;
88 	h = height;
89 	pitch = newPitch;
90 	pixels = newPixels;
91 	format = f;
92 }
93 
copyFrom(const Surface & surf)94 void Surface::copyFrom(const Surface &surf) {
95 	create(surf.w, surf.h, surf.format);
96 	if (surf.pitch == pitch) {
97 		memcpy(pixels, surf.pixels, h * pitch);
98 	} else {
99 		const byte *src = (const byte *)surf.pixels;
100 		byte *dst = (byte *)pixels;
101 		for (int y = h; y > 0; --y) {
102 			memcpy(dst, src, w * format.bytesPerPixel);
103 			src += surf.pitch;
104 			dst += pitch;
105 		}
106 	}
107 }
108 
getSubArea(const Common::Rect & area)109 Surface Surface::getSubArea(const Common::Rect &area) {
110 	Common::Rect effectiveArea(area);
111 	effectiveArea.clip(w, h);
112 
113 	Surface subSurface;
114 	subSurface.w = effectiveArea.width();
115 	subSurface.h = effectiveArea.height();
116 	subSurface.pitch = pitch;
117 	subSurface.pixels = getBasePtr(area.left, area.top);
118 	subSurface.format = format;
119 	return subSurface;
120 }
121 
getSubArea(const Common::Rect & area) const122 const Surface Surface::getSubArea(const Common::Rect &area) const {
123 	Common::Rect effectiveArea(area);
124 	effectiveArea.clip(w, h);
125 
126 	Surface subSurface;
127 	subSurface.w = effectiveArea.width();
128 	subSurface.h = effectiveArea.height();
129 	subSurface.pitch = pitch;
130 	// We need to cast the const away here because a Surface always has a
131 	// pointer to modifiable pixel data.
132 	subSurface.pixels = const_cast<void *>(getBasePtr(area.left, area.top));
133 	subSurface.format = format;
134 	return subSurface;
135 }
136 
copyRectToSurface(const void * buffer,int srcPitch,int destX,int destY,int width,int height)137 void Surface::copyRectToSurface(const void *buffer, int srcPitch, int destX, int destY, int width, int height) {
138 	assert(buffer);
139 
140 	assert(destX >= 0 && destX < w);
141 	assert(destY >= 0 && destY < h);
142 	assert(height > 0 && destY + height <= h);
143 	assert(width > 0 && destX + width <= w);
144 
145 	// Copy buffer data to internal buffer
146 	const byte *src = (const byte *)buffer;
147 	byte *dst = (byte *)getBasePtr(destX, destY);
148 	for (int i = 0; i < height; i++) {
149 		memcpy(dst, src, width * format.bytesPerPixel);
150 		src += srcPitch;
151 		dst += pitch;
152 	}
153 }
154 
copyRectToSurface(const Graphics::Surface & srcSurface,int destX,int destY,const Common::Rect subRect)155 void Surface::copyRectToSurface(const Graphics::Surface &srcSurface, int destX, int destY, const Common::Rect subRect) {
156 	assert(srcSurface.format == format);
157 
158 	copyRectToSurface(srcSurface.getBasePtr(subRect.left, subRect.top), srcSurface.pitch, destX, destY, subRect.width(), subRect.height());
159 }
160 
hLine(int x,int y,int x2,uint32 color)161 void Surface::hLine(int x, int y, int x2, uint32 color) {
162 	// Clipping
163 	if (y < 0 || y >= h)
164 		return;
165 
166 	if (x2 < x)
167 		SWAP(x2, x);
168 
169 	if (x < 0)
170 		x = 0;
171 	if (x2 >= w)
172 		x2 = w - 1;
173 
174 	if (x2 < x)
175 		return;
176 
177 	if (format.bytesPerPixel == 1) {
178 		byte *ptr = (byte *)getBasePtr(x, y);
179 		memset(ptr, (byte)color, x2 - x + 1);
180 	} else if (format.bytesPerPixel == 2) {
181 		uint16 *ptr = (uint16 *)getBasePtr(x, y);
182 		Common::fill(ptr, ptr + (x2 - x + 1), (uint16)color);
183 	} else if (format.bytesPerPixel == 4) {
184 		uint32 *ptr = (uint32 *)getBasePtr(x, y);
185 		Common::fill(ptr, ptr + (x2 - x + 1), color);
186 	} else {
187 		error("Surface::hLine: bytesPerPixel must be 1, 2, or 4");
188 	}
189 }
190 
vLine(int x,int y,int y2,uint32 color)191 void Surface::vLine(int x, int y, int y2, uint32 color) {
192 	// Clipping
193 	if (x < 0 || x >= w)
194 		return;
195 
196 	if (y2 < y)
197 		SWAP(y2, y);
198 
199 	if (y < 0)
200 		y = 0;
201 	if (y2 >= h)
202 		y2 = h - 1;
203 
204 	if (format.bytesPerPixel == 1) {
205 		byte *ptr = (byte *)getBasePtr(x, y);
206 		while (y++ <= y2) {
207 			*ptr = (byte)color;
208 			ptr += pitch;
209 		}
210 	} else if (format.bytesPerPixel == 2) {
211 		uint16 *ptr = (uint16 *)getBasePtr(x, y);
212 		while (y++ <= y2) {
213 			*ptr = (uint16)color;
214 			ptr += pitch / 2;
215 		}
216 
217 	} else if (format.bytesPerPixel == 4) {
218 		uint32 *ptr = (uint32 *)getBasePtr(x, y);
219 		while (y++ <= y2) {
220 			*ptr = color;
221 			ptr += pitch / 4;
222 		}
223 	} else {
224 		error("Surface::vLine: bytesPerPixel must be 1, 2, or 4");
225 	}
226 }
227 
fillRect(Common::Rect r,uint32 color)228 void Surface::fillRect(Common::Rect r, uint32 color) {
229 	r.clip(w, h);
230 
231 	if (!r.isValidRect())
232 		return;
233 
234 	int width = r.width();
235 	int lineLen = width;
236 	int height = r.height();
237 	bool useMemset = true;
238 
239 	if (format.bytesPerPixel == 2) {
240 		lineLen *= 2;
241 		if ((uint16)color != ((color & 0xff) | (color & 0xff) << 8))
242 			useMemset = false;
243 	} else if (format.bytesPerPixel == 4) {
244 		useMemset = false;
245 	} else if (format.bytesPerPixel != 1) {
246 		error("Surface::fillRect: bytesPerPixel must be 1, 2, or 4");
247 	}
248 
249 	if (useMemset) {
250 		byte *ptr = (byte *)getBasePtr(r.left, r.top);
251 		while (height--) {
252 			memset(ptr, (byte)color, lineLen);
253 			ptr += pitch;
254 		}
255 	} else {
256 		if (format.bytesPerPixel == 2) {
257 			uint16 *ptr = (uint16 *)getBasePtr(r.left, r.top);
258 			while (height--) {
259 				Common::fill(ptr, ptr + width, (uint16)color);
260 				ptr += pitch / 2;
261 			}
262 		} else {
263 			uint32 *ptr = (uint32 *)getBasePtr(r.left, r.top);
264 			while (height--) {
265 				Common::fill(ptr, ptr + width, color);
266 				ptr += pitch / 4;
267 			}
268 		}
269 	}
270 }
271 
frameRect(const Common::Rect & r,uint32 color)272 void Surface::frameRect(const Common::Rect &r, uint32 color) {
273 	hLine(r.left, r.top, r.right - 1, color);
274 	hLine(r.left, r.bottom - 1, r.right - 1, color);
275 	vLine(r.left, r.top, r.bottom - 1, color);
276 	vLine(r.right - 1, r.top, r.bottom - 1, color);
277 }
278 
move(int dx,int dy,int height)279 void Surface::move(int dx, int dy, int height) {
280 	// Short circuit check - do we have to do anything anyway?
281 	if ((dx == 0 && dy == 0) || height <= 0)
282 		return;
283 
284 	if (format.bytesPerPixel != 1 && format.bytesPerPixel != 2 && format.bytesPerPixel != 4)
285 		error("Surface::move: bytesPerPixel must be 1, 2, or 4");
286 
287 	byte *src, *dst;
288 	int x, y;
289 
290 	// vertical movement
291 	if (dy > 0) {
292 		// move down - copy from bottom to top
293 		dst = (byte *)pixels + (height - 1) * pitch;
294 		src = dst - dy * pitch;
295 		for (y = dy; y < height; y++) {
296 			memcpy(dst, src, pitch);
297 			src -= pitch;
298 			dst -= pitch;
299 		}
300 	} else if (dy < 0) {
301 		// move up - copy from top to bottom
302 		dst = (byte *)pixels;
303 		src = dst - dy * pitch;
304 		for (y = -dy; y < height; y++) {
305 			memcpy(dst, src, pitch);
306 			src += pitch;
307 			dst += pitch;
308 		}
309 	}
310 
311 	// horizontal movement
312 	if (dx > 0) {
313 		// move right - copy from right to left
314 		dst = (byte *)pixels + (pitch - format.bytesPerPixel);
315 		src = dst - (dx * format.bytesPerPixel);
316 		for (y = 0; y < height; y++) {
317 			for (x = dx; x < w; x++) {
318 				if (format.bytesPerPixel == 1) {
319 					*dst-- = *src--;
320 				} else if (format.bytesPerPixel == 2) {
321 					*(uint16 *)dst = *(const uint16 *)src;
322 					src -= 2;
323 					dst -= 2;
324 				} else if (format.bytesPerPixel == 4) {
325 					*(uint32 *)dst = *(const uint32 *)src;
326 					src -= 4;
327 					dst -= 4;
328 				}
329 			}
330 			src += pitch + (pitch - dx * format.bytesPerPixel);
331 			dst += pitch + (pitch - dx * format.bytesPerPixel);
332 		}
333 	} else if (dx < 0)  {
334 		// move left - copy from left to right
335 		dst = (byte *)pixels;
336 		src = dst - (dx * format.bytesPerPixel);
337 		for (y = 0; y < height; y++) {
338 			for (x = -dx; x < w; x++) {
339 				if (format.bytesPerPixel == 1) {
340 					*dst++ = *src++;
341 				} else if (format.bytesPerPixel == 2) {
342 					*(uint16 *)dst = *(const uint16 *)src;
343 					src += 2;
344 					dst += 2;
345 				} else if (format.bytesPerPixel == 4) {
346 					*(uint32 *)dst = *(const uint32 *)src;
347 					src += 4;
348 					dst += 4;
349 				}
350 			}
351 			src += pitch - (pitch + dx * format.bytesPerPixel);
352 			dst += pitch - (pitch + dx * format.bytesPerPixel);
353 		}
354 	}
355 }
356 
convertToInPlace(const PixelFormat & dstFormat,const byte * palette)357 void Surface::convertToInPlace(const PixelFormat &dstFormat, const byte *palette) {
358 	// Do not convert to the same format and ignore empty surfaces.
359 	if (format == dstFormat || pixels == 0) {
360 		return;
361 	}
362 
363 	if (format.bytesPerPixel == 0 || format.bytesPerPixel > 4)
364 		error("Surface::convertToInPlace(): Can only convert from 1Bpp, 2Bpp, 3Bpp, and 4Bpp");
365 
366 	if (dstFormat.bytesPerPixel != 2 && dstFormat.bytesPerPixel != 4)
367 		error("Surface::convertToInPlace(): Can only convert to 2Bpp and 4Bpp");
368 
369 	// In case the surface data needs more space allocate it.
370 	if (dstFormat.bytesPerPixel > format.bytesPerPixel) {
371 		void *const newPixels = realloc(pixels, w * h * dstFormat.bytesPerPixel);
372 		if (!newPixels) {
373 			error("Surface::convertToInPlace(): Out of memory");
374 		}
375 		pixels = newPixels;
376 	}
377 
378 	// We take advantage of the fact that pitch is always w * format.bytesPerPixel.
379 	// This is assured by the logic of Surface::create.
380 
381 	// We need to handle 1 Bpp surfaces special here.
382 	if (format.bytesPerPixel == 1) {
383 		assert(palette);
384 
385 		for (int y = h; y > 0; --y) {
386 			const byte *srcRow = (const byte *)pixels + y * pitch - 1;
387 			byte *dstRow = (byte *)pixels + y * w * dstFormat.bytesPerPixel - dstFormat.bytesPerPixel;
388 
389 			for (int x = 0; x < w; x++) {
390 				byte index = *srcRow--;
391 				byte r = palette[index * 3];
392 				byte g = palette[index * 3 + 1];
393 				byte b = palette[index * 3 + 2];
394 
395 				uint32 color = dstFormat.RGBToColor(r, g, b);
396 
397 				if (dstFormat.bytesPerPixel == 2)
398 					*((uint16 *)dstRow) = color;
399 				else
400 					*((uint32 *)dstRow) = color;
401 
402 				dstRow -= dstFormat.bytesPerPixel;
403 			}
404 		}
405 	} else {
406 		crossBlit((byte *)pixels, (const byte *)pixels, w * dstFormat.bytesPerPixel, pitch, w, h, dstFormat, format);
407 	}
408 
409 	// In case the surface data got smaller, free up some memory.
410 	if (dstFormat.bytesPerPixel < format.bytesPerPixel) {
411 		void *const newPixels = realloc(pixels, w * h * dstFormat.bytesPerPixel);
412 		if (!newPixels) {
413 			error("Surface::convertToInPlace(): Freeing memory failed");
414 		}
415 		pixels = newPixels;
416 	}
417 
418 	// Update the surface specific data.
419 	format = dstFormat;
420 	pitch = w * dstFormat.bytesPerPixel;
421 }
422 
convertTo(const PixelFormat & dstFormat,const byte * palette) const423 Graphics::Surface *Surface::convertTo(const PixelFormat &dstFormat, const byte *palette) const {
424 	assert(pixels);
425 
426 	Graphics::Surface *surface = new Graphics::Surface();
427 
428 	// If the target format is the same, just copy
429 	if (format == dstFormat) {
430 		surface->copyFrom(*this);
431 		return surface;
432 	}
433 
434 	if (format.bytesPerPixel == 0 || format.bytesPerPixel > 4)
435 		error("Surface::convertTo(): Can only convert from 1Bpp, 2Bpp, 3Bpp, and 4Bpp");
436 
437 	if (dstFormat.bytesPerPixel < 2 || dstFormat.bytesPerPixel > 4)
438 		error("Surface::convertTo(): Can only convert to 2Bpp, 3Bpp and 4Bpp");
439 
440 	surface->create(w, h, dstFormat);
441 
442 	if (format.bytesPerPixel == 1) {
443 		// Converting from paletted to high color
444 		assert(palette);
445 
446 		for (int y = 0; y < h; y++) {
447 			const byte *srcRow = (const byte *)getBasePtr(0, y);
448 			byte *dstRow = (byte *)surface->getBasePtr(0, y);
449 
450 			for (int x = 0; x < w; x++) {
451 				byte index = *srcRow++;
452 				byte r = palette[index * 3];
453 				byte g = palette[index * 3 + 1];
454 				byte b = palette[index * 3 + 2];
455 
456 				uint32 color = dstFormat.RGBToColor(r, g, b);
457 
458 				if (dstFormat.bytesPerPixel == 2)
459 					*((uint16 *)dstRow) = color;
460 				else if (dstFormat.bytesPerPixel == 3)
461 					WRITE_UINT24(dstRow, color);
462 				else
463 					*((uint32 *)dstRow) = color;
464 
465 				dstRow += dstFormat.bytesPerPixel;
466 			}
467 		}
468 	} else {
469 		// Converting from high color to high color
470 		for (int y = 0; y < h; y++) {
471 			const byte *srcRow = (const byte *)getBasePtr(0, y);
472 			byte *dstRow = (byte *)surface->getBasePtr(0, y);
473 
474 			for (int x = 0; x < w; x++) {
475 				uint32 srcColor;
476 				if (format.bytesPerPixel == 2)
477 					srcColor = READ_UINT16(srcRow);
478 				else if (format.bytesPerPixel == 3)
479 					srcColor = READ_UINT24(srcRow);
480 				else
481 					srcColor = READ_UINT32(srcRow);
482 
483 				srcRow += format.bytesPerPixel;
484 
485 				// Convert that color to the new format
486 				byte r, g, b, a;
487 				format.colorToARGB(srcColor, a, r, g, b);
488 				uint32 color = dstFormat.ARGBToColor(a, r, g, b);
489 
490 				if (dstFormat.bytesPerPixel == 2)
491 					*((uint16 *)dstRow) = color;
492 				else if (dstFormat.bytesPerPixel == 3)
493 					WRITE_UINT24(dstRow, color);
494 				else
495 					*((uint32 *)dstRow) = color;
496 
497 				dstRow += dstFormat.bytesPerPixel;
498 			}
499 		}
500 	}
501 
502 	return surface;
503 }
504 
FloodFill(Graphics::Surface * surface,uint32 oldColor,uint32 fillColor,bool maskMode)505 FloodFill::FloodFill(Graphics::Surface *surface, uint32 oldColor, uint32 fillColor, bool maskMode) {
506 	_surface = surface;
507 	_oldColor = oldColor;
508 	_fillColor = fillColor;
509 	_w = surface->w;
510 	_h = surface->h;
511 
512 	_mask = nullptr;
513 	_maskMode = maskMode;
514 
515 	if (_maskMode) {
516 		_mask = new Graphics::Surface();
517 		_mask->create(_w, _h, Graphics::PixelFormat::createFormatCLUT8()); // Uses calloc()
518 	}
519 
520 	_visited = (byte *)calloc(_w * _h, 1);
521 }
522 
~FloodFill()523 FloodFill::~FloodFill() {
524 	while(!_queue.empty()) {
525 		Common::Point *p = _queue.front();
526 
527 		delete p;
528 		_queue.pop_front();
529 	}
530 
531 	free(_visited);
532 
533 	if (_mask)
534 		delete _mask;
535 }
536 
addSeed(int x,int y)537 void FloodFill::addSeed(int x, int y) {
538 	if (x >= 0 && x < _w && y >= 0 && y < _h) {
539 		if (!_visited[y * _w + x]) {
540 			_visited[y * _w + x] = 1;
541 			void *src = _surface->getBasePtr(x, y);
542 			void *dst;
543 			bool changed = false;
544 
545 			if (_maskMode)
546 				dst = _mask->getBasePtr(x, y);
547 			else
548 				dst = src;
549 
550 			if (_surface->format.bytesPerPixel == 1) {
551 				if (*((byte *)src) == _oldColor) {
552 					*((byte *)dst) = _maskMode ? 255 : _fillColor;
553 					changed = true;
554 				}
555 			} else if (_surface->format.bytesPerPixel == 2) {
556 				if (READ_UINT16(src) == _oldColor) {
557 					if (!_maskMode)
558 						WRITE_UINT16(src, _fillColor);
559 					else
560 						*((byte *)dst) = 255;
561 
562 					changed = true;
563 				}
564 			} else if (_surface->format.bytesPerPixel == 4) {
565 				if (READ_UINT32(src) == _oldColor) {
566 					if (!_maskMode)
567 						WRITE_UINT32(src, _fillColor);
568 					else
569 						*((byte *)dst) = 255;
570 
571 					changed = true;
572 				}
573 			} else {
574 				error("Unsupported bpp in FloodFill");
575 			}
576 
577 			if (changed) {
578 				Common::Point *pt = new Common::Point(x, y);
579 
580 				_queue.push_back(pt);
581 			}
582 		}
583 	}
584 }
585 
fill()586 void FloodFill::fill() {
587 	while (!_queue.empty()) {
588 		Common::Point *p = _queue.front();
589 		_queue.pop_front();
590 		addSeed(p->x    , p->y - 1);
591 		addSeed(p->x - 1, p->y    );
592 		addSeed(p->x    , p->y + 1);
593 		addSeed(p->x + 1, p->y    );
594 
595 		delete p;
596 	}
597 }
598 
fillMask()599 void FloodFill::fillMask() {
600 	_maskMode = true;
601 
602 	if (!_mask) {
603 		_mask = new Graphics::Surface();
604 		_mask->create(_w, _h, Graphics::PixelFormat::createFormatCLUT8()); // Uses calloc()
605 	}
606 
607 	fill();
608 }
609 
610 } // End of namespace Graphics
611