void CL_PixelFillRenderer::fill_rect(const CL_Rect &dest, const CL_Colorf &primary_color)
{
	int dest_buffer_width = colorbuffer0.size.width;
	int dest_buffer_height = colorbuffer0.size.height;
	unsigned int *dest_data = colorbuffer0.data;

	int start_x = cl_max(dest.left, clip_rect.left);
	int end_x = cl_min(dest.right, clip_rect.right);
	int start_y = cl_max(dest.top, clip_rect.top);
	int end_y = cl_min(dest.bottom, clip_rect.bottom);
	if (start_x < end_x && start_y < end_y)
	{
		int dest_y = find_first_line_for_core(start_y, core, num_cores);

		int delta_x = start_x-dest.left;
		int delta_y = dest_y-dest.top;

		unsigned int *dest_line = dest_data+dest_y*dest_buffer_width+start_x;

		int line_length = end_x-start_x;
		int dest_line_incr = dest_buffer_width * num_cores;

		unsigned int sred = (unsigned int) (primary_color.r*255);
		unsigned int sgreen = (unsigned int) (primary_color.g*255);
		unsigned int sblue = (unsigned int) (primary_color.b*255);
		unsigned int salpha = (unsigned int) (primary_color.a*255);

		if (salpha == 255)
		{
			unsigned int color = (salpha<<24) + (sred<<16) + (sgreen<<8) + sblue;
			while (dest_y < end_y)
			{
				for (int x = 0; x < line_length; x++)
					dest_line[x] = color;

				dest_y += num_cores;
				dest_line += dest_line_incr;
			}
		}
		else
		{
			unsigned int pos_salpha = salpha*256/255;
			unsigned int neg_salpha = 256-salpha;
			while (dest_y < end_y)
			{
				for (int x = 0; x < line_length; x++)
				{
					#define alpha_component(a) (((a)&0xff000000)>>24)
					#define red_component(a) (((a)&0x00ff0000)>>16)
					#define green_component(a) (((a)&0x0000ff00)>>8)
					#define blue_component(a) ((a)&0x000000ff)

					unsigned int dest_color = dest_line[x];
					unsigned int dred = red_component(dest_color);
					unsigned int dgreen = green_component(dest_color);
					unsigned int dblue = blue_component(dest_color);
					unsigned int dalpha = alpha_component(dest_color);

					unsigned red = (dred * neg_salpha + sred * pos_salpha) >> 8;
					unsigned green = (dgreen * neg_salpha + sgreen * pos_salpha) >> 8;
					unsigned blue = (dblue * neg_salpha + sblue * pos_salpha) >> 8;
					unsigned alpha = (dalpha * neg_salpha + salpha * pos_salpha) >> 8;
					dest_line[x] = (alpha<<24) + (red<<16) + (green<<8) + blue;
				}

				dest_y += num_cores;
				dest_line += dest_line_incr;
			}
		}
	}
void PixelLineRenderer::draw_line(const LineSegment2 &line_dest, const Colorf &primary_color)
{
	// Clip the input line
	LineSegment2 line(line_dest);
	bool clipped;
	line.clip(clip_rect, clipped);
	if (!clipped)	// Off screen
		return;

	if (line.p.y > line.q.y)	// We draw top down
	{
		Vec2i t(line.p);
		line.p = line.q;
		line.q = t;
	}

	#define alpha_component(a) (((a)&0xff000000)>>24)
	#define red_component(a) (((a)&0x00ff0000)>>16)
	#define green_component(a) (((a)&0x0000ff00)>>8)
	#define blue_component(a) ((a)&0x000000ff)

	unsigned int sred = (unsigned int) (primary_color.r*255);
	unsigned int sgreen = (unsigned int) (primary_color.g*255);
	unsigned int sblue = (unsigned int) (primary_color.b*255);
	unsigned int salpha = (unsigned int) (primary_color.a*255);

	// Special case - horizontal line
	if (line.p.y == line.q.y)
	{
		int dest_y = find_first_line_for_core(line.p.y, core, num_cores);
		if (dest_y == line.p.y)	// Ensure correct line --- This might not be required
		{
			// Draw left to right
			if (line.p.x > line.q.x)
			{
				// Swap left and right
				int t = line.p.x;
				line.p.x = line.q.x;
				line.q.x = t;
			}

			if(dest_y>=dest_height)
			{
				//abort
				return;
			}

			unsigned int *dest_line = dest+dest_y*dest_width;
			if (salpha == 255)
			{
				unsigned int color = (salpha<<24) + (sred<<16) + (sgreen<<8) + sblue;
				for (int x = line.p.x; x < line.q.x; x++)
				{
					dest_line[x] = color;
				}
			}
			else
			{
				unsigned int pos_salpha = salpha*256/255;
				unsigned int neg_salpha = 256-salpha;

				for (int x = line.p.x; x < line.q.x; x++)
				{
					unsigned int dest_color = dest_line[x];
					unsigned int dred = red_component(dest_color);
					unsigned int dgreen = green_component(dest_color);
					unsigned int dblue = blue_component(dest_color);
					unsigned int dalpha = alpha_component(dest_color);

					unsigned red = (dred * neg_salpha + sred * pos_salpha) >> 8;
					unsigned green = (dgreen * neg_salpha + sgreen * pos_salpha) >> 8;
					unsigned blue = (dblue * neg_salpha + sblue * pos_salpha) >> 8;
					unsigned alpha = (dalpha * neg_salpha + salpha * pos_salpha) >> 8;
					dest_line[x] = (alpha<<24) + (red<<16) + (green<<8) + blue;
				}
			}
		}
	}