// Bresenham simple line drawing
void raster_rgba_line(pb_rgba *pb, unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2, uint32_t color)
{
	int dx, dy;
	int i;
	int sdx, sdy, dxabs, dyabs;
	unsigned int x, y, px, py;

	dx = x2 - x1;      /* the horizontal distance of the line */
	dy = y2 - y1;      /* the vertical distance of the line */
	dxabs = abs(dx);
	dyabs = abs(dy);
	sdx = sgn(dx);
	sdy = sgn(dy);
	x = dyabs >> 1;
	y = dxabs >> 1;
	px = x1;
	py = y1;

	pb_rgba_set_pixel(pb, x1, y1, color);

	if (dxabs >= dyabs) /* the line is more horizontal than vertical */
	{
		for (i = 0; i<dxabs; i++)
		{
			y += dyabs;
			if (y >= (unsigned int)dxabs)
			{
				y -= dxabs;
				py += sdy;
			}
			px += sdx;
			pb_rgba_set_pixel(pb, px, py, color);
		}
	}
	else /* the line is more vertical than horizontal */
	{
		for (i = 0; i<dyabs; i++)
		{
			x += dxabs;
			if (x >= (unsigned int)dyabs)
			{
				x -= dyabs;
				px += sdx;
			}
			py += sdy;
			pb_rgba_set_pixel(pb, px, py, color);
		}
	}
}
void raster_rgba_vline_fade(pb_rgba *pb, int y1, uint32_t color1, int y2, uint32_t color2, int x)
{
	int ydiff = y2 - y1;
	if (ydiff == 0)
		return;

	int c1rd = GET_R(color1);
	int c1gr = GET_G(color1);
	int c1bl = GET_B(color1);

	int c2rd = GET_R(color2);
	int c2gr = GET_G(color2);
	int c2bl = GET_B(color2);

	int rdx = c2rd - c1rd;
	int gdx = c2gr - c1gr;
	int bdx = c2bl - c1bl;

	float factor = 0.0f;
	float factorStep = 1.0f / (float)ydiff;

	// draw each pixel in the span
	for (int y = y1; y < y2; y++) {
		int rd = c1rd + (int)(rdx*factor);
		int gr = c1gr + (int)(gdx*factor);
		int bl = c1bl + (int)(bdx*factor);
		int fg = RGBA(rd, gr, bl, 255);
		pb_rgba_set_pixel(pb, x, y, fg);

		factor += factorStep;
	}
}
Beispiel #3
0
void pb_rgba_cover_pixel(pb_rgba *pb, const unsigned int x, const unsigned int y, const uint32_t value)
{

	// Quick reject if the foreground pixel has both 0 opacity
	// and 0 for color component values
	if (0 == value) {
		return;
	}

	if (0 == GET_A(value)) {
		// Combine the colors, but don't
		// change the alpha of the background
	} else if (255 == GET_A(value)) {
		// The foreground opacity is full, so set
		// the color
		// and set the background alpha to full as well
		pb_rgba_set_pixel(pb, x, y, value);
	} else {
		// All other cases where doing a cover of something
		// other than full opacity
		uint8_t alpha = GET_A(value);
		int32_t dstPixel = pb_rgba_get_pixel(pb, x, y);


		int dstColor = RGBA(
			lerp255(GET_R(dstPixel), GET_R(value), alpha),
			lerp255(GET_G(dstPixel), GET_G(value), alpha), 
			lerp255(GET_B(dstPixel), GET_B(value), alpha), 
			lerp255(GET_A(dstPixel), GET_A(value), alpha)
		);
		pb_rgba_set_pixel(pb, x, y, dstColor);
		/*
		pix_rgba * B = (pix_rgba *)&pb->data[(y*(pb)->pixelpitch) + x];
		B->r = lerp255(B->r, GET_R(value), alpha);
		B->g = lerp255(B->g, GET_R(value), alpha);
		B->b = lerp255(B->b, GET_R(value), alpha);
		B->a = lerp255(B->a, GET_R(value), alpha);
		*/
	}
}