RGB控制字符屏幕显示

#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/fb.h>
#include <sys/mman.h>
#include <unistd.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

struct fb_var_screeninfo info;
unsigned char * p_fd = NULL;

typedef struct __color
{
	unsigned char b;
	unsigned char g;
	unsigned char r;
	unsigned char null;
}color;

typedef union 
{
	color rgb;
	unsigned int l;
}col;

int draw_point(int x0, int y0, col c)
{
	if((x0 < 0) || (x0 > info.xres_virtual))
		return -1;
	if((y0 < 0) || (y0 > info.yres_virtual))
		return -1;

	unsigned int * p = (unsigned int *)(p_fd + (y0 * info.xres_virtual + x0) * 4);
	*p = c.l;

	return 0;
}

int draw_h_line(int x0, int y0, int len, col c)
{
	int i = 0;
	for(i = 0; i < len; i++)
	{
		draw_point(x0 + i, y0, c);	
	}

	return 0;
}

int draw_v_line(int x0, int y0, int len, col c)
{
	int i = 0;
	for(i = 0; i < len; i++)
	{
		draw_point(x0, y0 + i, c);	
	}

	return 0;
}

int clr_bg()
{
	int i = 0;
	col  c;
	c.l = 0xffffff;
	for(i = 0; i < 600; i++)
	{
		draw_h_line(0, i, info.xres, c);	
	}
}

#define PI 3.1415926

int draw_circle(int x0, int y0, int r, col c)
{
	double si = 0;
	unsigned int x = 0;
	unsigned int y = 0;
	for(si = 0; si < 360; si+=0.1)
	{
		x = x0 + r * cos(2 * PI * si / 360);
		y = y0 + r * sin(2 * PI * si / 360);
		draw_point(x, y, c);
	}
	return 0;
}

#pragma pack(2)
typedef struct _tag_bmp_file_head{
    unsigned short	file_type;			// 位图文件的类型,必须为BMP (2个字节)
    unsigned int	file_size;			// 位图文件的大小,以字节为单位 (4个字节)
    unsigned short	file_reserved1;		// 位图文件保留字,必须为0 (2个字节)
    unsigned short	file_reserved2;		// 位图文件保留字,必须为0 (2个字节)
    unsigned int	file_offset_bits;	// 位图数据的起始位置,以相对于位图 (4个字节)
}bmp_file_head;

typedef struct _tag_bmp_info_head{
	unsigned int info_size;     		// 本结构所占用字节数  (4个字节)
	unsigned int bit_width;      		// 位图的宽度,以像素为单位(4个字节)
	unsigned int bit_height;     		// 位图的高度,以像素为单位(4个字节)
	unsigned short bit_planes;    		// 目标设备的级别,必须为1(2个字节)
	unsigned short bits_per_pixel; 		// 每个像素所需的位数,必须是1(双色)、// 4(16色)、8(256色)、
										// 24(真彩色)或32(增强真彩色)之一 (2个字节)
	unsigned int bit_compression;		// 位图压缩类型,必须是 0(不压缩)、 1(BI_RLE8 
										// 压缩类型)或2(BI_RLE4压缩类型)之一 ) (4个字节)
	unsigned int bit_sizeImage;     	// 位图的大小,以字节为单位(4个字节)
	unsigned int bit_xpels_per_meter;  	// 位图水平分辨率,每米像素数(4个字节)
	unsigned int bit_ypels_per_meter;   // 位图垂直分辨率,每米像素数(4个字节)
	unsigned int bit_clr_used;			// 位图实际使用的颜色表中的颜色数(4个字节)
	unsigned int bit_clr_important;		// 位图显示过程中重要的颜色数(4个字节)
}bmp_info_head;

#pragma pack(4)

int show_bmp(const char * pathname, int x0, int y0)
{
	int fd = open(pathname, O_RDWR);
	if(fd < 0)
	{
		perror("open bmp failed\n");
		return -1;
	}

	bmp_file_head fhead;
	bmp_info_head ihead;
//	printf("size fhead = %ld\n", sizeof fhead);
	read(fd, &fhead, sizeof(fhead));
	read(fd, &ihead, sizeof(ihead));
//	printf("w = %d h = %d\n", ihead.biWidth, ihead.biHeight);

	int size = ihead.bit_height * ihead.bit_width * 3;
	unsigned char * data = malloc(size);
	read(fd, data, size);
	unsigned char * p = data;

	int i = 0;
	int j = 0;
	for(j = 0; j < ihead.bit_height; j++)  // y
	{
		for(i = 0; i < ihead.bit_width; i++)  // x
		{
			col c;
			c.rgb.b = *p++;
			c.rgb.g = *p++;
			c.rgb.r = *p++;
#if 0
			c.rgb.b = data[(j * ihead.biWidth + i) * 3];
			c.rgb.g = data[(j * ihead.biWidth + i) * 3 + 1];
			c.rgb.r = data[(j * ihead.biWidth + i) * 3 + 2];
#endif
			draw_point(x0 + i, y0 + ihead.bit_height - j - 1, c);
		}
	}

	free(data);
	close(fd);
}

#if 0
extern unsigned char font[][8];
int draw_a_ascii(char ch, int x0, int y0, col c)
{
	int i = 0;
	int j = 0;
	unsigned char * data = NULL;

	switch(ch)
	{
		case '0' ... '9':
			printf("ch = %c\n", ch);
			data = font[ch - 0x30];
			break;
		case 'A' ... 'Z':
			data = font[ch - 65 + 11];
			break;
		case 'a' ... 'z':
			data = font[ch - 97 + 37];
			break;
		case '.':
			data = font[10];
			break;
		default:
			data = font[26 + 26 + 11 + 1];
			break;
	}

	for(j = 0; j < 8; j++)
	{
		unsigned char tmp = data[j];
		for(i = 0; i < 8; i++)
		{
			 if(0x80 & (tmp << i))	
			 {
				draw_point(x0 + i, y0 + j, c); 
			 }
		}
	}
	return 0;
}

int draw_str(const char * str, int x0, int y0, col c)
{
	int len = strlen(str);
	int i = 0;
	int x = x0;
	int y = y0;
	for(i = 0; i < len; i++)
	{
		switch(str[i])
		{
			case '\n':
				y += 9;
				x = x0;
				break;
			case ' ':
				x += 9;
			default:
				draw_a_ascii(str[i], x, y, c);	
				x += 9;
				break;
		}
	}

	return 0;
}
#endif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值