如何用c语言读取图片
答案:2 悬赏:20 手机版
解决时间 2021-03-29 04:22
- 提问者网友:最爱你的唇
- 2021-03-28 13:31
如何用c语言读取图片
最佳答案
- 五星知识达人网友:英雄的欲望
- 2021-03-28 13:48
c语言读取图片原理:通过文件流的方式读入到Byte的二进制数组中,之后,使用图像分析算法将图像显示到屏幕上,要将数组中的值转换为像素。
参考代码如下:
//function definition
void ImageRead(AnsiString name,int &width,int &height,int *r,int *g,int *b)
{
//read image
FILE *fp;
if((fp=fopen(name.c_str(),"rb"))==NULL) {
printf("cannot open bmp.name
");
return ;
}
fread(&bfType,sizeof(WORD),1,fp);
if(bfType!=0x4d42) {//该值必需是0x4D42,也就是字符'BM'
printf("the input map is not bmp type");
return ;
}
fread(&bfSize,sizeof(DWORD),1,fp);
fread(&bfReserved1,sizeof(WORD),1,fp);
fread(&bfReserved2,sizeof(WORD),1,fp);
fread(&bfOffBits,sizeof(DWORD),1,fp);
fread(&bih,sizeof(BITMAPINFOHEADER),1,fp);
width=bih.biWidth ;
height=bih.biHeight;
if(width % 4 !=0)
width=width+ (4-width % 4);
DWORD size=width*(bih.biBitCount/8)*height;
unsigned char* pData=new unsigned char[size];
fread(pData,size,1,fp);
int bmWidthBytes=width*bih.biBitCount /8;
int bmBitsPixel=bih.biBitCount ;
int nBit=bmBitsPixel/8;
int z;
for (int y=0; y {
for (int x=0; x {
z=height-y-1;
*(r+(y*width+x))=pData[x*nBit+2+z*bmWidthBytes];
*(g+(y*width+x))=pData[x*nBit+1+z*bmWidthBytes];
*(b+(y*width+x))=pData[x*nBit+z*bmWidthBytes];
}
}
}
void ImageWrite(AnsiString name1,int *r,int *g,int *b)
{
//read image
FILE *fw;
fw=fopen(name1.c_str(),"wb");
fwrite(&bfType,sizeof(WORD),1,fw);
fwrite(&bfSize,sizeof(DWORD),1,fw);
fwrite(&bfReserved1,sizeof(WORD),1,fw);
fwrite(&bfReserved2,sizeof(WORD),1,fw);
fwrite(&bfOffBits,sizeof(DWORD),1,fw);
fwrite(&bih,sizeof(BITMAPINFOHEADER),1,fw);
int width=bih.biWidth ;
int height=bih.biHeight;
if(width % 4 !=0)
width=width+ (4-width % 4);
DWORD size=width*(bih.biBitCount/8)*height;
unsigned char* pData=new unsigned char[size];
int bmWidthBytes=width*bih.biBitCount /8;
int bmBitsPixel=bih.biBitCount ;
int nBit=bmBitsPixel/8;
int z;
for (int y=0; y {
for (int x=0; x {
z=height-y-1;
pData[x*nBit+2+z*bmWidthBytes]=*(r+(y*width+x));
pData[x*nBit+1+z*bmWidthBytes]=*(g+(y*width+x));
pData[x*nBit+z*bmWidthBytes]=*(b+(y*width+x));
}
}
fwrite(pData,size,1,fw);
}
参考代码如下:
//function definition
void ImageRead(AnsiString name,int &width,int &height,int *r,int *g,int *b)
{
//read image
FILE *fp;
if((fp=fopen(name.c_str(),"rb"))==NULL) {
printf("cannot open bmp.name
");
return ;
}
fread(&bfType,sizeof(WORD),1,fp);
if(bfType!=0x4d42) {//该值必需是0x4D42,也就是字符'BM'
printf("the input map is not bmp type");
return ;
}
fread(&bfSize,sizeof(DWORD),1,fp);
fread(&bfReserved1,sizeof(WORD),1,fp);
fread(&bfReserved2,sizeof(WORD),1,fp);
fread(&bfOffBits,sizeof(DWORD),1,fp);
fread(&bih,sizeof(BITMAPINFOHEADER),1,fp);
width=bih.biWidth ;
height=bih.biHeight;
if(width % 4 !=0)
width=width+ (4-width % 4);
DWORD size=width*(bih.biBitCount/8)*height;
unsigned char* pData=new unsigned char[size];
fread(pData,size,1,fp);
int bmWidthBytes=width*bih.biBitCount /8;
int bmBitsPixel=bih.biBitCount ;
int nBit=bmBitsPixel/8;
int z;
for (int y=0; y
for (int x=0; x
z=height-y-1;
*(r+(y*width+x))=pData[x*nBit+2+z*bmWidthBytes];
*(g+(y*width+x))=pData[x*nBit+1+z*bmWidthBytes];
*(b+(y*width+x))=pData[x*nBit+z*bmWidthBytes];
}
}
}
void ImageWrite(AnsiString name1,int *r,int *g,int *b)
{
//read image
FILE *fw;
fw=fopen(name1.c_str(),"wb");
fwrite(&bfType,sizeof(WORD),1,fw);
fwrite(&bfSize,sizeof(DWORD),1,fw);
fwrite(&bfReserved1,sizeof(WORD),1,fw);
fwrite(&bfReserved2,sizeof(WORD),1,fw);
fwrite(&bfOffBits,sizeof(DWORD),1,fw);
fwrite(&bih,sizeof(BITMAPINFOHEADER),1,fw);
int width=bih.biWidth ;
int height=bih.biHeight;
if(width % 4 !=0)
width=width+ (4-width % 4);
DWORD size=width*(bih.biBitCount/8)*height;
unsigned char* pData=new unsigned char[size];
int bmWidthBytes=width*bih.biBitCount /8;
int bmBitsPixel=bih.biBitCount ;
int nBit=bmBitsPixel/8;
int z;
for (int y=0; y
for (int x=0; x
z=height-y-1;
pData[x*nBit+2+z*bmWidthBytes]=*(r+(y*width+x));
pData[x*nBit+1+z*bmWidthBytes]=*(g+(y*width+x));
pData[x*nBit+z*bmWidthBytes]=*(b+(y*width+x));
}
}
fwrite(pData,size,1,fw);
}
全部回答
- 1楼网友:詩光轨車
- 2021-03-28 14:08
图片也是文件,就用fopen(),fread()等操作文件的函数就可以读了,但是只有你知道图片文件的确切格式,才可能正确读取
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯