BMP是英文Bitmap(位图)的简写,它是Windows操作系统中的标准图像文件格式,能够被多种Windows应用程序所支持。随着Windows操作系统的流行与丰富的Windows应用程序的开发,BMP位图格式理所当然地被广泛应用。这种格式的特点是包含的图像信息较丰富,几乎不进行压缩,但由此导致了它与生俱来的缺点–占用磁盘空间过大。所以,目前BMP在单机上比较流行。
给BMP图片添加水印的方法有很多,本示例采用直接读写文件方式来添加水印。
示例效果:
原始图片
运行程序:
[xsw@xsw BMP_stady]$ ./a.out
格式:./a.out <1.bmp> <2.bmp>
[xsw@xsw BMP_stady]$ ./a.out watermark.bmp 2.bmp
图片宽:500
图片高:312
请输入水印添加的位置(x:0~500 y:0~312):600 200
[BMP_add_Watermark line 117]超出显示边线,请重新输入
请输入水印添加的位置(x:0~500 y:0~312):100 100
[xsw@xsw BMP_stady]$
程序示例
/**********************添加水印************************************
**形参:
** char *new_bmp ---新生成的图片
** char *bmp_file ---需要添加水印的图片
** int num --- 要添加几个汉字水印
** int size --- 汉字大小(等高宽)
**返回值:0 成功,其他值---错误
*******************************************************************/
int BMP_add_Watermark(char *new_bmp,char *befor_bmp,int num,int size)
{
FILE *fp[2];
fp[0]=fopen(befor_bmp,"rb");
if(fp[0]==NULL)
{
printf("[%s line %d]文件打开失败",__FUNCTION__,__LINE__);
return 1;
}
fp[1]=fopen(new_bmp,"w+b");
if(fp[1]==NULL)
{
printf("[%s line %d]文件打开或创建失败",__FUNCTION__,__LINE__);
return 2;
}
BMP_HEADER bmp_head;
BMP_INFO bmp_info;
fread(&bmp_head,sizeof(BMP_HEADER),1,fp[0]);//读取头数据
fwrite(&bmp_head,sizeof(BMP_HEADER),1,fp[1]);//头数据写入到新的文件中
fread(&bmp_info,sizeof(BMP_INFO),1,fp[0]);//读取位图数据
fwrite(&bmp_info,sizeof(BMP_INFO),1,fp[1]);//位图数据写入到新的文件中
if(bmp_head.bfType!=0x4d42)
{
printf("[%s line %d]图片格式错误\n",__FUNCTION__,__LINE__);
return 3;
}
printf("\t图片宽:%d\n",bmp_info.biWidth);
printf("\t图片宽:%d\n",bmp_info.biHeight);
int one_line_size=bmp_info.biWidth*3;//一行的字节数
while(one_line_size%4!=0)one_line_size++;//字节数不是4的倍数补全
unsigned char one_line_buff[one_line_size];
int i;
/*复制图片*/
for(i=0;ibmp_info.biWidth-num*size || y>bmp_info.biHeight-size)
{
printf("\n\t[%s line %d]超出显示边线,请重新输入\n",__FUNCTION__,__LINE__);
goto pp;
}
int j,k;
int y1=y;
unsigned char data=0;
int rgb=0xff0000;//设置水印的颜色
int cnt=0;
for(k=0;k;k++)>;i++)>
主函数
int main(int argc,char **argv)
{
if(argc!=3)
{
printf("格式:./a.out <1.bmp> <2.bmp>\n");
return 0;
}
int stat=0;
char buff[20];
stat=BMP_add_Watermark(argv[1],argv[2],4,24);
if(stat==0)
{
snprintf(buff,sizeof(buff),"eog %s",argv[1]);//字符串拼接
system(buff);//创建进程
}
else printf("[%s line %d] err %d\n",__FUNCTION__,__LINE__,stat);
return 0;
}
BMP格式结构体
#pragma pack(1) /* 必须在结构体定义之前使用,这是为了让结构体中各成员按1字节对齐*/
/*图片头*/
typedef struct BitMapFileHEADER
{
unsigned short bfType; //保存图片类型。 'BM'
unsigned long bfSize; //图片文件的总大小,以字节为单位(3-6字节,低位在前)
unsigned short bfReserved1;//位图文件保留字,必须为0(7-8字节)
unsigned short bfReserved2;//位图文件保留字,必须为0(9-10字节)
unsigned long bfOffBits; //RGB数据偏移地址,位图数据的起始位置,以相对于位图(11-14字节,低位在前)//文件头的偏移量表示,以字节为单位
}BMP_HEADER;
/*图片信息*/
typedef struct BitMapFileInfo{
unsigned long biSize; //本结构所占用字节数(15-18字节)
unsigned long biWidth; //位图的宽度,以像素为单位(19-22字节)
unsigned long biHeight; //位图的高度,以像素为单位(23-26字节)
unsigned short biPlanes; //目标设备的级别,必须为1(27-28字节)
unsigned short biBitCount; //每个像素所需的位数,必须是1(双色)(29-30字节),4(16色),8(256色)16(高彩色)或24(真彩色)之一
unsigned long biCompression;//位图压缩类型,必须是0(不压缩),(31-34字节)
//1(BI_RLE8压缩类型)或2(BI_RLE4压缩类型)之一
unsigned long biSizeImage; //位图的大小(其中包含了为了补齐行数是4的倍数而添加的空字节),以字节为单位(35-38字节)
unsigned long biXPelsPerMeter;//位图水平分辨率,每米像素数(39-42字节)
unsigned long biYPelsPerMeter;//位图垂直分辨率,每米像素数(43-46字节)
unsigned long biClrUsed; //位图实际使用的颜色表中的颜色数(47-50字节)
unsigned long biClrImportant; //位图显示过程中重要的颜色数(51-54字节)
}BMP_INFO;
汉字取模点阵
//牛转乾坤取模
const unsigned char font_buff[][24*24/8]=
{
{0x00,0x00,0x00,0x00,0x10,0x00,0x00,0x0C,0x00,0x00,0x0C,0x00,0x00,0x08,0x00,0x01,0x88,0x00,0x01,0x88,0x00,0x01,0x8F,0xC0,0x03,0xFC,0x00,0x02,0x08,0x00,0x04,0x08,0x00,0x00,0x08,0x7C,
0x00,0x7F,0xFE,0x3F,0xC8,0x00,0x00,0x08,0x00,0x00,0x08,0x00,0x00,0x08,0x00,0x00,0x08,0x00,0x00,0x08,0x00,0x00,0x08,0x00,0x00,0x08,0x00,0x00,0x08,0x00,0x00,0x08,0x00,0x00,0x00,0x00},/*"牛",0*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x01,0x81,0x80,0x01,0x01,0x80,0x03,0x01,0x00,0x02,0xE1,0x00,0x1F,0x81,0xF0,0x04,0x0F,0x00,0x05,0x82,0x00,0x09,0x02,0x1C,0x09,0xC7,0xF8,
0x1F,0x3A,0x00,0x01,0x06,0x00,0x01,0xC4,0xE0,0x0F,0x07,0x70,0x39,0x00,0x40,0x01,0x00,0x80,0x01,0x04,0x80,0x01,0x03,0x00,0x01,0x01,0x80,0x01,0x01,0x80,0x00,0x00,0x80,0x00,0x00,0x00},/*"转",1*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x83,0x00,0x01,0x02,0x00,0x01,0xE6,0x00,0x0F,0x84,0x00,0x01,0x04,0x20,0x02,0x05,0xC0,0x03,0xEA,0x00,0x1C,0x70,0x00,0x19,0x60,0x00,
0x0E,0x41,0x80,0x09,0xCF,0x00,0x0F,0x02,0x00,0x01,0x24,0x00,0x01,0xC4,0x00,0x7F,0x08,0x04,0x21,0x08,0x04,0x01,0x0C,0x04,0x03,0x06,0x3C,0x01,0x03,0xF8,0x00,0x00,0x00,0x00,0x00,0x00},/*"乾",2*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x00,0x06,0x01,0x80,0x06,0x01,0x00,0x06,0x01,0x00,0x06,0x01,0x18,0x06,0x01,0xFC,0x06,0x7F,0x0C,0x07,0xA1,0x0C,0x1E,0x33,0xE8,0x06,0x37,0x18,
0x06,0x11,0x18,0x06,0x91,0x10,0x07,0x1F,0xF0,0x1C,0x11,0x00,0x30,0x01,0x00,0x00,0x01,0x00,0x00,0x01,0x00,0x00,0x01,0x00,0x00,0x01,0x00,0x00,0x01,0x00,0x00,0x01,0x00,0x00,0x01,0x00},/*"坤",3*/
};
查看添加水印后的图片
审核编辑:汤梓红
全部0条评论
快来发表一下你的评论吧 !