㈠ c语言怎么读入一个bmp文件,并显示(一定要C语言,附代码)
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <malloc.h>
#define WIDTHBYTES(i) ((i+31)/32*4)
//#pragma warning(disable: 4996)
int main()
{
BITMAPFILEHEADER bf; //BMP文件头结构体
BITMAPINFOHEADER bi; //BMP信息头结构体
FILE* fp; //指向文件的指针
RGBQUAD *ipRGB; //
DWORD LineByte,ImgSize;
DWORD NumColors;
long Width;
unsigned char * * Imgdata;
int i,j;
char fileName[256];
//打开文件
printf("please enter filename:");
scanf("%s",fileName);
fp=fopen(fileName,"rb");
if(fp == NULL){
printf("Open file error!");
exit(0);
}
//读取信息头、文件头
fread(&bf,sizeof(BITMAPFILEHEADER),1,fp); //把指针fp所指向的文件的头信息写入bf(地址)
fread(&bi,sizeof(BITMAPINFOHEADER),1,fp);
LineByte=(DWORD)WIDTHBYTES(bi.biWidth*bi.biBitCount); //计算位图的实际宽度并确保它为32的倍数
ImgSize=(DWORD)LineByte*bi.biHeight;
if (bi.biClrUsed != 0 )
NumColors=(DWORD)bi.biClrUsed;
else
switch (bi.biBitCount)
{
case 1:NumColors=2;break;
case 4:NumColors=16;break;
case 8:NumColors=256;break;
case 24:NumColors=0;break;
}
//分配调色板内存
if(bi.biBitCount < 24){
ipRGB=(RGBQUAD *)malloc(NumColors*sizeof(RGBQUAD));
fread(ipRGB,sizeof(RGBQUAD),NumColors,fp);
}
Imgdata=new unsigned char*[bi.biHeight]; //声明一个指针数组
if(bi.biBitCount >= 24){
// fseek(fp, 4, SEEK_CUR);//sizeof(RGBQUAD)
Width = bi.biWidth*3;
} else{
Width = bi.biWidth;
}
for ( i=(bi.biHeight)-1;i>=0;i--)
Imgdata[i]=new unsigned char[Width]; //每个数组元素也是一个指针数组
for ( i=(bi.biHeight)-1;i>=0;i--)
for(j=0;j<Width;j++)
fread(&Imgdata[i][j],1,1,fp);//每次只读取一个1字节,存入数组
fclose(fp);
//写入另一个文件
fp=fopen("mybmp.bmp","wb");
fwrite(&bf,sizeof(BITMAPFILEHEADER),1,fp);
fwrite(&bi,sizeof(BITMAPINFOHEADER),1,fp);
if(bi.biBitCount < 24){
fwrite(ipRGB,sizeof(RGBQUAD),NumColors,fp);
}
for (i=(bi.biHeight)-1 ;i>=0;i--)
for (j=0 ;j<Width;j++) {
Imgdata[i][j] = 255 - Imgdata[i][j];
fwrite(&Imgdata[i][j],1,1,fp);
}
free(Imgdata);
fclose(fp);
return 0;
}
㈡ 如何用C语言编程来显示一个bmp文件(要源码 最好有注释 谢谢)
c语言读bmp文件的话,需要你理解bmp文件格式,这个你可以自己去网络一下,我这里有个在vc中实现的源码。
#include<windows.h>
#include<stdio.h>
#include<string.h>
#include<malloc.h>
unsignedchar*pBmpBuf;//读入图像数据的指针
intbmpWidth;//图像的宽
intbmpHeight;//图像的高
RGBQUAD*pColorTable;//颜色表指针
intbiBitCount;//图像类型,每像素位数
boolreadBmp(char*bmpName)
{
//二进制读方式打开指定的图像文件
FILE*fp=fopen(bmpName,"rb");
if(fp==0)return0;
//跳过位图文件头结构BITMAPFILEHEADER
fseek(fp,sizeof(BITMAPFILEHEADER),0);
//定义位图信息头结构变量,读取位图信息头进内存,存放在变量head中
BITMAPINFOHEADERhead;
fread(&head,sizeof(BITMAPINFOHEADER),1,fp);
//获取图像宽、高、每像素所占位数等信息
bmpWidth=head.biWidth;
bmpHeight=head.biHeight;
biBitCount=head.biBitCount;
//定义变量,计算图像每行像素所占的字节数(必须是4的倍数)
intlineByte=(bmpWidth*biBitCount/8+3)/4*4;
//灰度图像有颜色表,且颜色表表项为256
if(biBitCount==8){
//申请颜色表所需要的空间,读颜色表进内存
pColorTable=newRGBQUAD[256];
fread(pColorTable,sizeof(RGBQUAD),256,fp);
}
//申请位图数据所需要的空间,读位图数据进内存
pBmpBuf=newunsignedchar[lineByte*bmpHeight];
fread(pBmpBuf,1,lineByte*bmpHeight,fp);
//关闭文件
fclose(fp);
return1;
}
boolsaveBmp(char*bmpName,unsignedchar*imgBuf,intwidth,intheight,
intbiBitCount,RGBQUAD*pColorTable)
{
//如果位图数据指针为0,则没有数据传入,函数返回
if(!imgBuf)
return0;
//颜色表大小,以字节为单位,灰度图像颜色表为1024字节,彩色图像颜色表大小为0
intcolorTablesize=0;
if(biBitCount==8)
colorTablesize=1024;
//待存储图像数据每行字节数为4的倍数
intlineByte=(width*biBitCount/8+3)/4*4;
//以二进制写的方式打开文件
FILE*fp=fopen(bmpName,"wb");
if(fp==0)return0;
//申请位图文件头结构变量,填写文件头信息
BITMAPFILEHEADERfileHead;
fileHead.bfType=0x4D42;//bmp类型
//bfSize是图像文件4个组成部分之和
fileHead.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)
+colorTablesize+lineByte*height;
fileHead.bfReserved1=0;
fileHead.bfReserved2=0;
//bfOffBits是图像文件前3个部分所需空间之和
fileHead.bfOffBits=54+colorTablesize;
//写文件头进文件
fwrite(&fileHead,sizeof(BITMAPFILEHEADER),1,fp);
//申请位图信息头结构变量,填写信息头信息
BITMAPINFOHEADERhead;
head.biBitCount=biBitCount;
head.biClrImportant=0;
head.biClrUsed=0;
head.biCompression=0;
head.biHeight=height;
head.biPlanes=1;
head.biSize=40;
head.biSizeImage=lineByte*height;
head.biWidth=width;
head.biXPelsPerMeter=0;
head.biYPelsPerMeter=0;
//写位图信息头进内存
fwrite(&head,sizeof(BITMAPINFOHEADER),1,fp);
//如果灰度图像,有颜色表,写入文件
if(biBitCount==8)
fwrite(pColorTable,sizeof(RGBQUAD),256,fp);
//写位图数据进文件
fwrite(imgBuf,height*lineByte,1,fp);
//关闭文件
fclose(fp);
return1;
}
intmain()
{
charinFileName[90],outFileName[90];
printf("请输入原始位图文件的文件名:");
scanf("%s",inFileName);
printf("请输入加密程序产生的新位图文件的文件名:");
scanf("%s",outFileName);
//读入指定BMP文件进内存
readBmp(inFileName);
//输出图像的信息
printf("width=%d,height=%d,biBitCount=%d ",bmpWidth,bmpHeight,biBitCount);
//将图像数据存盘
saveBmp(outFileName,pBmpBuf,bmpWidth,bmpHeight,biBitCount,pColorTable);
//清除缓冲区,pBmpBuf和pColorTable是全局变量,在文件读入时申请的空间
delete[]pBmpBuf;
if(biBitCount==8)
delete[]pColorTable;
return0;
}
㈢ 如何用C语言读取bmp文件和pix文件
用fread读取bmp文件,分析bmp文件头,得出bmp的像素数据,然后访问和修改像素数据,就可以了。此问题如果你没有相关的图像处理类,必须自己去查找bmp文件的结构,按照bmp文件的结构自己编码来完成图像处理功能。
㈣ 如何在c语言 读取BMP图片的信息
使用opencv的库吧,安装很简单,处理图像的功能很强大
#include "cv.h"
#include "highgui.h"
int main( int argc, char** argv )
{
IplImage* pImg; //声明IplImage指针
//载入图像
if( argc == 2 &&
(pImg = cvLoadImage(“XXX.bmp”, 1)) != 0 )//这里bmp要放到工程文件夹下,否则写绝对路径,取像素值请用:pImg.ptr<Vec3b>(i)[j]
{
cvNamedWindow( "Image", 1 );//创建窗口
cvShowImage( "Image", pImg );//显示图像
cvWaitKey(0); //等待按键
cvDestroyWindow( "Image" );//销毁窗口
cvReleaseImage( &pImg ); //释放图像
return 0;
}
return -1;
}
㈤ 用C语言怎么读取BMP格式的图片
没有标准函数读。
需要根据BMP文件的结构定义,读出头部和每个颜色值。
struct
header
{
unsigned
short
int
bfType;
unsigned
int
bfSize;
unsigned
short
int
bfReserved1;
unsigned
short
int
bfReserved2;
unsigned
int
bfoffBits;
}__attribute__
((packed));
struct
tinfoheader
{
unsigned
int
biSize;
unsigned
int
biWidth;
unsigned
int
biHeight;
unsigned
short
int
biPlanes;
unsigned
short
int
biBitCount;
unsigned
int
biCompression;
unsigned
int
biSizeImage;
unsigned
int
biXPelsPerMeter;
unsigned
int
biYPelsPerMeter;
unsigned
int
biClrUsed;
unsigned
int
biClrImportant;
}__attribute__
((packed));
㈥ 请问如何用C语言读取bmp文件和pix文件
unsigned char *pix=new unsigned char[bm.widthpiexl*bm.height];
SetBitmapPiex(bitmap,bm.widthpiexl*bm.height,pix);
这样子便可以得到图像的像素参数,存放在pix数组内,
BMP文件由文件头、位图信息头、颜色信息和图形数据四部分组成。颜色信息包含图像所用到的颜色表,显示图像时需用到这个颜色表来生成调色板.我们需要做的是,新建PIX文件后,用WINHEX打开,准备好模板文件,提取BMP中的颜色信息,粘贴到PIX文件的头文件中,然后保存即可.可以用ddraw.h读详细资料请看msdn,希望能帮到你。
㈦ 怎样用c语言实现BMP读存
直接用fread读取就可以。你得知道bmp的格式,头文件存放的信息,读出出来就知道文件的大小,分辨率,大小端等。在读取每一位的颜色信息。注意bmp是按行倒叙存取,还有注意bmp有字节对齐的。
㈧ 用c语言读取24位位图bmp文件
可以使用C语言标准函数库中的fopen、fseek、fclose等系列函数来打开bmp位图文件,以及进行相应的处理,下面是一个demo,仅供参考。以下代码在vc6.0中编译通过。
#include<stdio.h>
#include<stdlib.h>
#//ThebmpFileHeaderlengthis14
#defineBM19778//TheASCIIcodeforBM
/*Testthefileisbmpfileornot*/
voidbmpFileTest(FILE*fpbmp);
/**/
voidbmpHeaderPartLength(FILE*fpbmp);
/**/
voidBmpWidthHeight(FILE*fpbmp);
//getr,g,bdata
voidbmpDataPart(FILE*fpbmp);
//
voidbmpoutput(FILE*fpout);
unsignedintOffSet=0;//
longwidth;//TheWidthoftheDataPart
longheight;//TheHeightoftheDataPart
unsignedcharr[2000][2000],output_r[2000][2000];
unsignedcharg[2000][2000],output_g[2000][2000];
unsignedcharb[2000][2000],output_b[2000][2000];
intmain(intargc,char*argv[])
{
/*Openbmpfile*/
unsignedchar*fp_temp;
FILE*fpbmp;
FILE*fpout;
fpbmp=fopen("1.bmp","rb");
if(fpbmp==NULL)
{
printf("Openbmpfailed!!! ");
return1;
}
fpout=fopen("out.bmp","wb+");
if(fpout==NULL)
{
printf("Openout.bmpfailed!!! ");
return1;
}
bmpFileTest(fpbmp);//Testthefileisbmpfileornot
bmpHeaderPartLength(fpbmp);//GetthelengthofHeaderPart
BmpWidthHeight(fpbmp);//
//
fseek(fpbmp,0L,SEEK_SET);
fseek(fpout,0L,SEEK_SET);
fp_temp=(unsignedchar*)malloc(OffSet);
fread(fp_temp,1,OffSet,fpbmp);
fwrite(fp_temp,1,OffSet,fpout);
bmpDataPart(fpbmp);//Reservethedatatofile
/*
如果您想对图片进行处理,请您再这里插入处理函数!!!!!!!!!!!!!!!!!!
*/
bmpoutput(fpout);
fclose(fpbmp);
fclose(fpout);
return0;
}
voidbmpoutput(FILE*fpout)
{
inti,j=0;
intstride;
unsignedchar*pixout=NULL;
stride=(24*width+31)/8;
stride=stride/4*4;
pixout=(unsignedchar*)malloc(stride);
fseek(fpout,OffSet,SEEK_SET);
for(j=0;j<height;j++)
{
for(i=0;i<width;i++)
{
pixout[i*3+2]=output_r[height-1-j][i];
pixout[i*3+1]=output_g[height-1-j][i];
pixout[i*3]=output_b[height-1-j][i];
}
fwrite(pixout,1,stride,fpout);
}
}
voidbmpDataPart(FILE*fpbmp)
{
inti,j=0;
intstride;
unsignedchar*pix=NULL;
FILE*fpr;
FILE*fpg;
FILE*fpb;
if((fpr=fopen("bmpr.txt","w+"))==NULL)
{
printf("Failedtoconstructfilebmpr.txt.!!!");
exit(1);
}
if((fpg=fopen("bmpg.txt","w+"))==NULL)
{
printf("Failedtoconstructfilebmpg.txt.!!!");
exit(1);
}
if((fpb=fopen("bmpb.txt","w+"))==NULL)
{
printf("Failedtoconstructfilebmpb.txt.!!!");
exit(1);
}
fseek(fpbmp,OffSet,SEEK_SET);
stride=(24*width+31)/8;
stride=stride/4*4;
pix=(unsignedchar*)malloc(stride);
for(j=0;j<height;j++)
{
fread(pix,1,stride,fpbmp);
for(i=0;i<width;i++)
{
r[height-1-j][i]=pix[i*3+2];
g[height-1-j][i]=pix[i*3+1];
b[height-1-j][i]=pix[i*3];
output_r[height-1-j][i]=pix[i*3+2];
output_g[height-1-j][i]=pix[i*3+1];
output_b[height-1-j][i]=pix[i*3];
}
}
for(i=0;i<height;i++)
{
for(j=0;j<width-1;j++)
{
fprintf(fpb,"%4d",b[i][j]);
fprintf(fpg,"%4d",g[i][j]);
fprintf(fpr,"%4d",r[i][j]);
}
fprintf(fpb,"%4d ",b[i][j]);
fprintf(fpg,"%4d ",g[i][j]);
fprintf(fpr,"%4d ",r[i][j]);
}
fclose(fpr);
fclose(fpg);
fclose(fpb);
}
voidbmpFileTest(FILE*fpbmp)
{
unsignedshortbfType=0;
fseek(fpbmp,0L,SEEK_SET);//seek_set起始位置
fread(&bfType,sizeof(char),2,fpbmp);
if(BM!=bfType)
{
printf("Thisfileisnotbmpfile.!!! ");
exit(1);
}
}
/**/
voidbmpHeaderPartLength(FILE*fpbmp)
{
fseek(fpbmp,10L,SEEK_SET);
fread(&OffSet,sizeof(char),4,fpbmp);
printf("TheHeaderPartisoflength%d. ",OffSet);
}
/**/
voidBmpWidthHeight(FILE*fpbmp)
{
fseek(fpbmp,18L,SEEK_SET);
fread(&width,sizeof(char),4,fpbmp);
fseek(fpbmp,22L,SEEK_SET);
fread(&height,sizeof(char),4,fpbmp);
printf("TheWidthofthebmpfileis%ld. ",width);
printf("TheHeightofthebmpfileis%ld. ",height);
}
㈨ c语言,怎样读取一个BMP图片
#ifndef IMAGE_H
#define IMAGE_H
void image_info(FILE* file);
void image_save(FILE *file);
void image_gray();
void image_binarization();
void image_opposite();
void image_channel(); //抽取RGB通道
void image_bright();//改变图像亮度
typedef struct BMP
{
//14字节
unsigned short bfType; //文件标识 2字节 必须为BM
unsigned int bfSize; //文件大小 4字节
unsigned short bfReserved1; //保留,每字节以"00"填写 2字节
unsigned short bfReserved2; //同上 2字节
unsigned int bfOffBits; //记录图像数据区的起始位置(图象数据相对于文件头字节的偏移量)。 4字节
//40字节
unsigned int biSize; //表示本结构的大小 4字节
int biWidth; //位图的宽度 4字节
int biHeight; //位图的高度 4字节
unsigned short biPlanes; //永远为1 , 2字节
unsigned short biBitCount; //位图的位数 分为1 4 8 16 24 32 2字节
unsigned int biCompression; //压缩说明 4字节
unsigned int biSizeImage; //表示位图数据区域的大小以字节为单位 4字节
int biXPelsPerMeter; //用象素/米表示的水平分辨率 4字节
int biYPelsPerMeter; //用象素/米表示的垂直分辨率 4字节
unsigned int biClrUsed; //位图使用的颜色索引数 4字节
unsigned int biClrImportant; //对图象显示有重要影响的颜色索引的数目 4字节
} BMP;
int line_byte;
unsigned char *imagedata;
extern BMP bmp;
extern int line_byte;
extern unsigned char *imagedata;
#endif
//image_rw.c文件
#include<stdio.h>
#include<stdlib.h>
#include"image.h"
void image_info(FILE *file)
{
int times=3; //输入文件名次数。
char bmp_name[10]; //文件名
printf("\nplease enter a file name for reading:");
do
{
if (times<3)
{
printf("\nplease enter a file name for reading again:");
}
fflush(stdin);
gets(bmp_name);
//printf("\n%s",bmp_name);
file=fopen(bmp_name,"rb+"); //打开一个文件进行读写操作。
--times;
if (file==NULL)
{
printf("\nerror opening %s for reading! ",bmp_name);
}
else
{
break;
}
}
while(times!=0);
if (times==0)
{
printf("\nsorry, shutdown!");
exit(1);
}
//读取图像信息
fseek(file,0L,0); //读取图像文件类型
fread(&bmp,sizeof(BMP),1,file);
printf("\n bmp tpye: %u",bmp.bfType);
printf("\n bmp size: %u",bmp.bfSize);
printf("\n bmp reserved1: %u",bmp.bfReserved1);
printf("\n bmp reserved2: %u",bmp.bfReserved2);
printf("\n bmp offBits: %u",bmp.bfOffBits);
printf("\n bmp bisize: %u",bmp.biSize);
printf("\n bmp biWidth: %d",bmp.biWidth);
printf("\n bmp biHeight: %d",bmp.biHeight);
printf("\n bmp biplans: %u",bmp.biPlanes);
printf("\n bmp biBitCount: %u",bmp.biBitCount);
printf("\n bmp biCompression: %u",bmp.biCompression);
printf("\n bmp biSizeImage: %u",bmp.biSizeImage);
printf("\n bmp biXPelsPerMeter: %d",bmp.biXPelsPerMeter);
printf("\n bmp biYPelsPerMeter: %d",bmp.biYPelsPerMeter);
printf("\n bmp biClrUsed: %u",bmp.biClrUsed);
printf("\n bmp biClrImportant: %u\n",bmp.biClrImportant);
line_byte=(bmp.biWidth*bmp.biBitCount/8+3)/4*4; //获得图像数据每行的数据个数
//printf("dfsa%u",bmp.line_byte);
//bmp.imagedata=NULL;
imagedata=(unsigned char*)malloc(bmp.biSizeImage);
fseek(file,(long)bmp.bfOffBits,0);
fread(imagedata,sizeof(unsigned char),bmp.biSizeImage,file);
fclose(file);
}
//保存图像
void image_save(FILE *file)
{
int times=3; //输入文件名次数。
char bmp_name[10]; //文件名
//int i; //记录数据区个数
printf("\nplease enter a file name for writeing:");
do
{
if (times<3)
{
printf("\nplease enter a file name for writeing again:");
}
fflush(stdin);
gets(bmp_name);
printf("\n%s",bmp_name);
file=fopen(bmp_name,"wb+"); //打开一个文件进行读写操作。
--times;
if (file==NULL)
{
printf("\nerror opening %s for writing",bmp_name);
}
else
{
break;
}
}
while(times!=0);
if (times==0)
{
printf("\nsorry, shutdown!");
exit(1);
}
//写文件头
printf("\n%s",bmp_name);
fseek(file,0L,0); //图像文件类型
fwrite(&(bmp.bfType),sizeof(short),1,file);
printf("\n bmp tpye: %d",bmp.bfType);
fseek(file,2L,0); //图像文件大小
fwrite(&(bmp.bfSize),sizeof(int),1,file);
printf("\n bmp size: %d",bmp.bfSize);
fseek(file,6L,0); //图像文件保留字1
fwrite(&(bmp.bfReserved1),sizeof(short),1,file);
printf("\n bmp reserved1: %d",bmp.bfReserved1);
fseek(file,8L,0); //图像文件保留字2
fwrite(&(bmp.bfReserved2),sizeof(short),1,file);
printf("\n bmp reserved2: %d",bmp.bfReserved2);
fseek(file,10L,0);//数据区的偏移量
fwrite(&(bmp.bfOffBits),sizeof(short),1,file);
printf("\n bmp offBits: %d",bmp.bfOffBits);
fseek(file,14L,0);//文件头结构大小
fwrite(&(bmp.biSize),sizeof(int),1,file);
printf("\n bmp bisize: %d",bmp.biSize);
fseek(file,18L,0);//图像的宽度
fwrite(&(bmp.biWidth),sizeof(int),1,file);
printf("\n bmp biWidth: %d",bmp.biWidth);
fseek(file,22L,0);//图像的高度
fwrite(&(bmp.biHeight),sizeof(int),1,file);
printf("\n bmp biHeight: %d",bmp.biHeight);
fseek(file,24L,0);//图像的面数
fwrite(&(bmp.biPlanes),sizeof(short),1,file);
printf("\n bmp biplans: %d",bmp.biPlanes);
fseek(file,28L,0);//图像一个像素的字节数
fwrite(&(bmp.biBitCount),sizeof(short),1,file);
printf("\n bmp biBitCount: %d",bmp.biBitCount);
fseek(file,30L,0);//图像压缩信息
fwrite(&(bmp.biCompression),sizeof(short),1,file);
printf("\n bmp biCompression: %d",bmp.biCompression);
fseek(file,34L,0);//图像数据区的大小
fwrite(&(bmp.biSizeImage),sizeof(int),1,file);
printf("\n bmp biSizeImage: %d",bmp.biSizeImage);
fseek(file,38L,0);//水平分辨率
fwrite(&(bmp.biXPelsPerMeter),sizeof(int),1,file);
printf("\n bmp biXPelsPerMeter: %d",bmp.biXPelsPerMeter);
fseek(file,42L,0);//垂直分辨率
fwrite(&(bmp.biYPelsPerMeter),sizeof(int),1,file);
printf("\n bmp biYPelsPerMeter: %d",bmp.biYPelsPerMeter);
fseek(file,46L,0);//颜色索引数
fwrite(&(bmp.biClrUsed),sizeof(int),1,file);
printf("\n bmp biClrUsed: %d",bmp.biClrUsed);
fseek(file,50L,0);//重要颜色索引数
fwrite(&(bmp.biClrImportant),sizeof(int),1,file);
printf("\n bmp biClrImportant: %d\n",bmp.biClrImportant);
fseek(file,(long)(bmp.bfOffBits),0);
fwrite(imagedata,sizeof(unsigned char),bmp.biSizeImage,file);
fclose(file);
}
//pixProcess.c文件
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include"image.h"
//灰度化
void image_gray()
{
int i,j;
unsigned char tmp;
for (i=0;i<bmp.biHeight;i++)
{
for (j=0;j<line_byte/3;j++)
{
tmp=0.11*(*(imagedata+i*line_byte+j*3+0))+0.59*(*(imagedata+i*line_byte+j*3+1))+0.3*(*(imagedata+i*line_byte+j*3+2));
imagedata[i*line_byte+j*3+0]=tmp;
imagedata[i*line_byte+j*3+1]=tmp;
imagedata[i*line_byte+j*3+2]=tmp;
//printf("\nnidsfh%d %d",i,j);
}
}
}
//二值化
void image_binarization()
{
int i,j;
for (i=0;i<bmp.biHeight;i++)
{
for (j=0;j<line_byte;j++)
{
if ((*(imagedata+i*line_byte+j))<128)
{
imagedata[i*line_byte+j]=0;
}
else
{
imagedata[i*line_byte+j]=255;
}
}
}
}
void image_opposite() //反相
{
int i,j;
for (i=0;i<bmp.biHeight;i++)
{
for (j=0;j<line_byte;j++)
{
imagedata[i*line_byte+j]=abs(255-imagedata[i*line_byte+j]);
}
}
}
void image_channel() //抽取RGB通道
{
int i,j;
char rgb;
printf("\nplease enter a char(r/g/b): ");
fflush(stdin);
scanf("%c",&rgb);
if (rgb=='b')
{
for (i=0;i<bmp.biHeight;i++)
{
for (j=0;j<line_byte/3;j++)
{
imagedata[i*line_byte+3*j+1]=0;
imagedata[i*line_byte+3*j+2]=0;
}
}
}
else if(rgb=='g')
{
for (i=0;i<bmp.biHeight;i++)
{
for (j=0;j<line_byte/3;j++)
{
imagedata[i*line_byte+3*j]=0;
imagedata[i*line_byte+3*j+2]=0;
}
}
}
else
{
for (i=0;i<bmp.biHeight;i++)
{
for (j=0;j<line_byte/3;j++)
{
imagedata[i*line_byte+3*j]=0;
imagedata[i*line_byte+3*j+1]=0;
}
}
}
}
void image_bright()//改变图像亮度
{
int level;
int i,j;
printf("\n please enter the level of brightness[-255 to 255] :");
fflush(stdin);
scanf("%d",&level);
for (i=0;i<bmp.biHeight;i++)
{
for (j=0;j<line_byte;j++)
{
if (level>=0)
{
if ((imagedata[i*line_byte+j]+level)>255)
imagedata[i*line_byte+j]=255;
else
imagedata[i*line_byte+j]+=level;
}
else
{
if ((imagedata[i*line_byte+j]-abs(level))<0)
imagedata[i*line_byte+j]=0;
else
imagedata[i*line_byte+j]+=level;
}
}
}
}
//void image_create() //创建一幅24位BMP图像文件。
//{
//main.c文件
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include"image.h"
BMP bmp;
int main()
{
FILE *file=NULL;
int choose;
char gono;
do
{
image_info(file); //imagedata已经分配了动态内存,但是没有释放
printf("\n 1.image_opposite");
printf("\n 2.image_gray");
printf("\n 3.image_binarization");
printf("\n 4.image_channel");
printf("\n 5.image_brightness");
//printf("6.image_opposite");
//printf("7.image_opposite");
printf("\nchoose your options:");
fflush(stdin);
scanf("%d",&choose);
switch(choose)
{
case 1:
image_opposite();
image_save(file);
free(imagedata);
break;
case 2:
image_gray();
image_save(file);
free(imagedata);
break;
case 3:
image_binarization();
image_save(file);
free(imagedata);
break;
case 4:
image_channel();
image_save(file);
free(imagedata);
break;
case 5:
image_bright();
image_save(file);
free(imagedata);
break;
default:
printf("\n wrong choose!");
}
printf("\nlet's go on?(y/n):");
fflush(stdin);
scanf("%c",&gono);
if (gono=='n')
{
printf("\nbye bye!");
break;
}
}
while(1);
return 0;
}
㈩ C语言读取bmp文件
你先看一下文件的打开方法,然后好好看看bmp的文件格式,文件头里的存储格式以及bmp图像数据的存储格式,最后显示你可以写文件显示也可以使用编程工具。