#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 save_bmp(const char * pathname, int x0, int y0, int w, int h)
{
int fd = open(pathname, O_WRONLY | O_CREAT, 0666);
if(fd < 0)
{
perror("open bmp2 failed:") ;
return 1;
}
bmp_file_head fhead = {
.file_type = 0x4d42,
.file_size = w * h * 3 + 54,
.file_reserved1 = 0,
.file_reserved2 = 0,
.file_offset_bits = 54
};
bmp_info_head ihead = {
.info_size = 40,
.bit_width = w,
.bit_height = h,
.bit_planes = 1,
.bits_per_pixel = 24,
.bit_compression = 0,
.bit_sizeImage = w * h * 3,
.bit_xpels_per_meter = 0,
.bit_ypels_per_meter = 0,
.bit_clr_used = 0,
.bit_clr_important = 0
};
write(fd, &fhead, sizeof(fhead));
write(fd, &ihead, sizeof(ihead));
unsigned char * clr = malloc(ihead.bit_sizeImage);
unsigned char * pdst = clr;
int i = 0;
int j = 0;
for(j = 0; j < ihead.bit_height; j++)
{
unsigned char * psrc = p_fd + ((ihead.bit_height - j - 1) * info.xres_virtual) * info.bits_per_pixel / 8;
for(i = 0; i < ihead.bit_width; i++)
{
*pdst++ = *psrc++;
*pdst++ = *psrc++;
*pdst++ = *psrc++;
psrc++;
}
}
write(fd, clr, ihead.bit_sizeImage);
free(clr);
close(fd);
return 0;
}
int save_mem()
{
p_mem = malloc(info.xres_virtual * info.yres_virtual * info.bits_per_pixel / 8);
if(NULL == p_mem)
{
return -1;
}
unsigned int * psrc = (unsigned int *)p_fd;
int i = 0;
int j = 0;
for(j = 0; j < info.yres; j++)
{
for(i = 0; i < info.xres; i++)
{
*(p_mem + j * info.xres_virtual + i) =
*(psrc + j * info.xres_virtual + i);
}
}
return 0;
}
int copy_mem(int x0, int y0, int w, int h)
{
//p_mem -> p_fd;
unsigned int * p_dst = (unsigned int *)p_fd;
int i = 0;
int j = 0;
for(j = 0; j < h; j++)
{
for(i = 0; i < w; i++)
{
*(p_dst + ((y0 + j) * info.xres_virtual + x0 + i)) =
*(p_mem + ((y0 + j) * info.xres_virtual + x0 + i));
}
}
}
RGB实现字符覆盖操作(部分重新覆盖)
最新推荐文章于 2024-11-12 15:18:48 发布