1/*
2 * Copyright 2021, Haiku, Inc.
3 * Distributed under the terms of the MIT License.
4 */
5
6
7#include "graphics.h"
8
9
10RasBuf32 gFramebuf = {NULL, 0, 0, 0};
11
12
13void
14Clear(RasBuf32 vb, uint32_t c)
15{
16	vb.stride -= vb.width;
17	for (; vb.height > 0; vb.height--) {
18		for (int x = 0; x < vb.width; x++) {
19			*vb.colors = c;
20			vb.colors++;
21		}
22		vb.colors += vb.stride;
23	}
24}
25
26
27template <typename Color>
28RasBuf<Color>
29RasBuf<Color>::Clip(int x, int y, int w, int h) const
30{
31	RasBuf<Color> vb = *this;
32	if (x < 0) {w += x; x = 0;}
33	if (y < 0) {h += y; y = 0;}
34	if (x + w > vb.width) {w = vb.width - x;}
35	if (y + h > vb.height) {h = vb.height - y;}
36	if (w > 0 && h > 0) {
37		vb.colors += y*vb.stride + x;
38		vb.width = w;
39		vb.height = h;
40	} else {
41		vb.colors = 0;
42		vb.width = 0;
43		vb.height = 0;
44	}
45	return vb;
46}
47
48template class RasBuf<uint8>;
49template class RasBuf<uint32>;
50
51
52RasBuf8
53Font::ThisGlyph(uint32 ch)
54{
55	if (ch < firstChar || ch >= firstChar + charCnt)
56		return ThisGlyph(' ');
57
58	RasBuf8 rb;
59	rb.colors = data + (ch - firstChar) * charWidth * charHeight;
60	rb.stride = charWidth;
61	rb.width = charWidth;
62	rb.height = charHeight;
63	return rb;
64}
65
66
67void
68BlitMaskRgb(RasBuf32 dst, RasBuf8 src, int32 x, int32 y, uint32_t c)
69{
70	int dstW, dstH;
71	uint32_t dc, a;
72	dstW = dst.width; dstH = dst.height;
73	dst = dst.Clip(x, y, src.width, src.height);
74	src = src.Clip(-x, -y, dstW, dstH);
75	dst.stride -= dst.width;
76	src.stride -= src.width;
77	for (; dst.height > 0; dst.height--) {
78		for (x = dst.width; x > 0; x--) {
79			dc = *dst.colors;
80			a = *src.colors;
81			if (a != 0) {
82				*dst.colors =
83					  ((((dc >>  0) % 0x100) * (0xff - a) / 0xff
84						+ ((c >>  0) % 0x100) * a / 0xff) <<  0)
85					+ ((((dc >>  8) % 0x100) * (0xff - a) / 0xff
86						+ ((c >>  8) % 0x100) * a / 0xff) <<  8)
87					+ ((((dc >> 16) % 0x100) * (0xff - a) / 0xff
88						+ ((c >> 16) % 0x100) * a / 0xff) << 16)
89					+ (dc & 0xff000000);
90			}
91			dst.colors++;
92			src.colors++;
93		}
94		dst.colors += dst.stride;
95		src.colors += src.stride;
96	}
97}
98