作者在 2008-04-10 01:34:50 发布以下内容
纯C代码下读写BMP文件代码,简单易懂。
/**//*************************************************************
** Copyright (c) 2007, Shanghai
**
** 文件名称:read_write_bmp_with_C.c
** 编译: Visual C++ / ANSI C / ISO C++
** 日期: 2007.10.24
**************************************************************/
#include "stdio.h"
#include "stdlib.h"
#define PIXPLINE 320
typedef struct tagRGBQUAD...{ //定义每个像素的数据类型
unsigned char rgbBlue;
unsigned char rgbGreen;
unsigned char rgbRed;
} RGBQUAD;
int bmp_read(unsigned char *image, int xsize, int ysize, char *filename) ...{
char fname_bmp[128];
sprintf(fname_bmp, "%s.bmp", filename);
FILE *fp;
if (!(fp = fopen(fname_bmp, "rb")))
return -1;
unsigned char header[54];
fread(header, sizeof(unsigned char), 54, fp);
fread(image, sizeof(unsigned char), (size_t)(long)xsize * ysize * 3, fp);
fclose(fp);
return 0;
}
int bmp_write(unsigned char *image, int xsize, int ysize, char *filename) ...{
unsigned char header[54] = ...{
0x42, 0x4d, 0, 0, 0, 0, 0, 0, 0, 0,
54, 0, 0, 0, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 24, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0
};
long file_size = (long)xsize * (long)ysize * 3 + 54;
header[2] = (unsigned char)(file_size &0x000000ff);
header[3] = (file_size >> 8) & 0x000000ff;
header[4] = (file_size >> 16) & 0x000000ff;
header[5] = (file_size >> 24) & 0x000000ff;
long width = xsize;
header[18] = width & 0x000000ff;
header[19] = (width >> 8) &0x000000ff;
header[20] = (width >> 16) &0x000000ff;
header[21] = (width >> 24) &0x000000ff;
long height = ysize;
header[22] = height &0x000000ff;
header[23] = (height >> 8) &0x000000ff;
header[24] = (height >> 16) &0x000000ff;
header[25] = (height >> 24) &0x000000ff;
char fname_bmp[128];
sprintf(fname_bmp, "%s.bmp", filename);
FILE *fp;
if (!(fp = fopen(fname_bmp, "wb")))
return -1;
fwrite(header, sizeof(unsigned char), 54, fp);
fwrite(image, sizeof(unsigned char), (size_t)(long)xsize * ysize * 3, fp);
fclose(fp);
return 0;
}
void clonebmp(unsigned char *image,int xsize,int ysize)
...{
bmp_read(image, xsize, ysize, "orgbmp"); //orgbmp为当前目录下的bmp文件名
bmp_write(image, xsize, ysize, "clone_bmp");//clone_bmp为克隆的bmp文件名
}
/**//****************************************************************************
* 名称:youwritetobmp()
* 功能:写入bmp文件
* 入口参数:RGBQUAD *pixarr ---- 要写入的像素数组指针, int xsize ---- 图像宽度, int ysize ---- 图像高度, char *filename --图像名称
* 出口参数:无
* 返回值:-1:错误 ;0:正确
****************************************************************************/
int youwritetobmp(RGBQUAD *pixarr, int xsize, int ysize, char *filename) ...{
unsigned char header[54] = ...{
0x42, 0x4d, 0, 0, 0, 0, 0, 0, 0, 0,
54, 0, 0, 0, 40, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 0, 24, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0
};
int i;
int j;
long file_size = (long)xsize * (long)ysize * 3 + 54;
header[2] = (unsigned char)(file_size &0x000000ff);
header[3] = (file_size >> 8) & 0x000000ff;
header[4] = (file_size >> 16) & 0x000000ff;
header[5] = (file_size >> 24) & 0x000000ff;
long width;
if(!(xsize%4)) width=xsize;
else width= xsize+(4-xsize%4); //如不是4的倍数,则转换成4的倍数
header[18] = width & 0x000000ff;
header[19] = (width >> 8) &0x000000ff;
header[20] = (width >> 16) &0x000000ff;
header[21] = (width >> 24) &0x000000ff;
long height = ysize;
header[22] = height &0x000000ff;
header[23] = (height >> 8) &0x000000ff;
header[24] = (height >> 16) &0x000000ff;
header[25] = (height >> 24) &0x000000ff;
char fname_bmp[128];
sprintf(fname_bmp, "%s.bmp", filename);
FILE *fp;
if (!(fp = fopen(fname_bmp, "wb")))
return -1;
fwrite(header, sizeof(unsigned char), 54, fp);
RGBQUAD zero=...{0,0,0}; //不足字节,用zero填充
for(j=0;j<ysize;j++)...{
if(!(xsize%4))...{
** Copyright (c) 2007, Shanghai
**
** 文件名称:read_write_bmp_with_C.c
** 编译: Visual C++ / ANSI C / ISO C++
** 日期: 2007.10.24
**************************************************************/
#include "stdio.h"
#include "stdlib.h"
#define PIXPLINE 320
typedef struct tagRGBQUAD...{ //定义每个像素的数据类型
unsigned char rgbBlue;
unsigned char rgbGreen;
unsigned char rgbRed;
} RGBQUAD;
int bmp_read(unsigned char *image, int xsize, int ysize, char *filename) ...{
char fname_bmp[128];
sprintf(fname_bmp, "%s.bmp", filename);
FILE *fp;
if (!(fp = fopen(fname_bmp, "rb")))
return -1;
unsigned char header[54];
fread(header, sizeof(unsigned char), 54, fp);
fread(image, sizeof(unsigned char), (size_t)(long)xsize * ysize * 3, fp);
fclose(fp);
return 0;
}
int bmp_write(unsigned char *image, int xsize, int ysize, char *filename) ...{
unsigned char header[54] = ...{
0x42, 0x4d, 0, 0, 0, 0, 0, 0, 0, 0,
54, 0, 0, 0, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 24, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0
};
long file_size = (long)xsize * (long)ysize * 3 + 54;
header[2] = (unsigned char)(file_size &0x000000ff);
header[3] = (file_size >> 8) & 0x000000ff;
header[4] = (file_size >> 16) & 0x000000ff;
header[5] = (file_size >> 24) & 0x000000ff;
long width = xsize;
header[18] = width & 0x000000ff;
header[19] = (width >> 8) &0x000000ff;
header[20] = (width >> 16) &0x000000ff;
header[21] = (width >> 24) &0x000000ff;
long height = ysize;
header[22] = height &0x000000ff;
header[23] = (height >> 8) &0x000000ff;
header[24] = (height >> 16) &0x000000ff;
header[25] = (height >> 24) &0x000000ff;
char fname_bmp[128];
sprintf(fname_bmp, "%s.bmp", filename);
FILE *fp;
if (!(fp = fopen(fname_bmp, "wb")))
return -1;
fwrite(header, sizeof(unsigned char), 54, fp);
fwrite(image, sizeof(unsigned char), (size_t)(long)xsize * ysize * 3, fp);
fclose(fp);
return 0;
}
void clonebmp(unsigned char *image,int xsize,int ysize)
...{
bmp_read(image, xsize, ysize, "orgbmp"); //orgbmp为当前目录下的bmp文件名
bmp_write(image, xsize, ysize, "clone_bmp");//clone_bmp为克隆的bmp文件名
}
/**//****************************************************************************
* 名称:youwritetobmp()
* 功能:写入bmp文件
* 入口参数:RGBQUAD *pixarr ---- 要写入的像素数组指针, int xsize ---- 图像宽度, int ysize ---- 图像高度, char *filename --图像名称
* 出口参数:无
* 返回值:-1:错误 ;0:正确
****************************************************************************/
int youwritetobmp(RGBQUAD *pixarr, int xsize, int ysize, char *filename) ...{
unsigned char header[54] = ...{
0x42, 0x4d, 0, 0, 0, 0, 0, 0, 0, 0,
54, 0, 0, 0, 40, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 0, 24, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0
};
int i;
int j;
long file_size = (long)xsize * (long)ysize * 3 + 54;
header[2] = (unsigned char)(file_size &0x000000ff);
header[3] = (file_size >> 8) & 0x000000ff;
header[4] = (file_size >> 16) & 0x000000ff;
header[5] = (file_size >> 24) & 0x000000ff;
long width;
if(!(xsize%4)) width=xsize;
else width= xsize+(4-xsize%4); //如不是4的倍数,则转换成4的倍数
header[18] = width & 0x000000ff;
header[19] = (width >> 8) &0x000000ff;
header[20] = (width >> 16) &0x000000ff;
header[21] = (width >> 24) &0x000000ff;
long height = ysize;
header[22] = height &0x000000ff;
header[23] = (height >> 8) &0x000000ff;
header[24] = (height >> 16) &0x000000ff;
header[25] = (height >> 24) &0x000000ff;
char fname_bmp[128];
sprintf(fname_bmp, "%s.bmp", filename);
FILE *fp;
if (!(fp = fopen(fname_bmp, "wb")))
return -1;
fwrite(header, sizeof(unsigned char), 54, fp);
RGBQUAD zero=...{0,0,0}; //不足字节,用zero填充
for(j=0;j<ysize;j++)...{
if(!(xsize%4))...{
文章评论,共0条
文章分类
文章归档
- 2022年06月(1)
- 2013年06月(1)
- 2013年01月(2)
- 2012年08月(4)
- 2012年07月(1)
- 2011年12月(2)
- 2011年08月(2)
- 2011年04月(2)
- 2011年01月(2)
- 2010年11月(1)
- 2010年10月(1)
- 2010年09月(5)
- 2010年08月(14)
- 2010年07月(18)
- 2010年06月(10)
- 2010年05月(3)
- 2010年01月(3)
- 2009年10月(8)
- 2009年09月(11)
- 2009年08月(7)
- 2009年07月(8)
- 2009年06月(7)
- 2009年05月(4)
- 2009年04月(1)
- 2009年03月(9)
- 2009年02月(6)
- 2009年01月(4)
- 2008年12月(11)
- 2008年11月(20)
- 2008年10月(37)
- 2008年09月(26)
- 2008年08月(17)
- 2008年07月(4)
- 2008年06月(21)
- 2008年05月(18)
- 2008年04月(31)
- 2007年08月(1)
- 2007年07月(1)
展开收起
最新评论
- 言察它季:将mpc, mpfr, gmp 等放到gcc目录下后,可以一键式构建配置 Configure...
- vfdff:~/.i18n文件: LANG=zh_CN.GB18030 LANGUAGE=en_U...
- qq372505855:很好的一篇文,希望能写个全局注入
- vfdff:http://ftp.gnu.org/gnu/gcc/gcc-4.7.2/
- vfdff:Angelo Graziosi schrieb: I want to flag that ...
- vfdff:D:\Program_Files\cygwin\bin\ash.exe 并输入:/bin/r...