位图c语言
① 用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语言(C++)读取位图的像素点RGB信息
pData里面保存的就是一个一个的COLORREF结构,你只需要通过BITMAPINFOHEADER中的宽高等信息,计算位移,就可以读取某个点的RGB值了。
还有一个简单的办法,你之前已经有memBitmap这个CBitmap了,通过这个做更方便。通过SelectObject将memBitmap放到一个CDC中,直接使用函数GetPixel函数就可以获取指定某个点的RGB值了,这个不需要计算和位移。
③ c语言 位图操作
对于一个24位色的图,你直接就拿int(32位机器,长度32位)型二维数组存就行了。肯定能存这个24位数,没必要把颜色分开存。如果分开存你就用下面的结构体数组的方法吧。建议参考,十分方便实用。
RGB24使用24位来表示一个像素,RGB分量都用8位表示,取值范围为0-255。注意在内存中RGB各分量的排列顺序为:BGR BGR BGR…。通常可以使用RGBTRIPLE数据结构来操作一个像素,它的定义为:
typedef struct tagRGBTRIPLE
{BYTE rgbtBlue; // 蓝色分量
BYTE rgbtGreen; // 绿色分量
BYTE rgbtRed; // 红色分量
} RGBTRIPLE;
④ VC++里,c语言读取位图之前怎么找到图片的
File.OpenRead("image.bmp");
这里面放的就是文件的详细路径,如果省略路径只有文件名字的话,那么就表示该文件在当前程序的工作目录下,就是在你的工程文件夹里面的。
⑤ c语言如何打开位图
不像打开文本那样一个函数就可以 需要调用好多模块 自己查资料去吧
⑥ 如何用C语言读取位图的像素点RGB信息
可以自己去查一下BMP文件的文件结构,这种文件的格式最简单。当然,其他常用的图片格式也可以去查一下。查到了之后,就能根据文件,用fopen打开图片再fread读取了
⑦ BMP格式位图TC语言怎么显示
下面的是<<C & C++编程实例>>随书光盘上的代码,我在TC2. 0下编译通过. 它是利用了抖动技术显示了8bit和24bit的位图( 也就是256色和16M色位图),应该能满足你的需要. 不过,我想问下,你老师教过抖动显示吗? #include <stdio.h> #include <dos.h> #include <stdio.h> #include <conio.h> #define NoError 0 #define ErrorFileOpen 1 #define ErrorFileType 2 #define ErrorImageColor 3 typedef struct tagBITMAPFILEHEADER { unsigned int bfType; unsigned long bfSize; unsigned int bfReserved1; unsigned int bfReserved2; unsigned long bfoffBits; }BITMAPFILEHEADER; typedef struct tagBITMAPINFOHEADER { unsigned long biSize; unsigned long biWidth; unsigned long biHeight; unsigned int biPlanes; unsigned int biBitCount; unsigned long biCompression; unsigned long biSizeImage; unsigned long biXPelsPerMeter; unsigned long biYPelsPerMeter; unsigned long biClrUsed; unsigned long biClrImportant; } BITMAPINFOHEADER; typedef struct tagRGBQUAD { unsigned char rgbBlue; unsigned char rgbGreen; unsigned char rgbRed; unsigned char rgbReserved; } RGBQUAD; unsigned char PalReg[17]= { 0,1,2,3,4,5,6,7,8,9,10,11,12, 13,14,15,0}; unsigned char StandardPal[48]= { 0, 0, 0, 32, 0, 0, 0,32, 0, 32,32, 0, 0, 0,32, 32, 0,32, 0,32,32, 32,32, 32, 48, 48,48, 63, 0, 0, 0,63, 0, 63,63, 0, 0, 0,63, 63, 0,63, 0,63,63, 63,63,63, }; unsigned char LightnessMatrix [16][16]= { { 0,235,59,219,15,231,55,215,2, 232,56,217,12,229,52,213}, {128,64,187,123,143,79,183, 119,130,66,184,120,140,76,180, 116}, {33,192,16,251,47,207,31,247, 34,194,18,248,44,204,28,244}, {161,97,144,80,175,111,159,95, 162,98,146,82,172,108,156,92}, {8,225,48,208,5,239,63,223,10, 226,50,210,6,236,60,220}, {136,72,176,112,133,69,191, 127,138,74,178,114,134,70,188, 124}, {41,200,24,240,36,197,20,255, 42,202,26,242,38,198,22,252}, {169,105,152,88,164,100,148, 84,170,106,154,90,166,102,150, 86}, {3,233,57,216,13,228,53,212,1, 234,58,218,14,230,54,214}, {131,67,185,121,141,77,181, 117,129,65,186,122,142,78,182, 118}, {35,195,19,249,45,205,29,245, 32,193,17,250,46,206,30,246}, {163,99,147,83,173,109,157,93, 160,96,145,81,174,110,158,94}, {11,227,51,211,7,237,61,221,9, 224,49,209,4,238,62,222}, {139,75,179,115,135,71,189, 125,137,73,177,113,132,68,190, 126}, {43,203,27,243,39,199,23,253, 40,201,25,241,37,196,21,254}, {171,107,155,91,167,103,151, 87,168,104,153,89,165,101,149, 85}, }; unsigned char ColorTable[2][2][2]= {{{0,12},{10,14}},{{9,13},{11, 15}}}; unsigned char ColorMap[256][3]; int ShowBmp(char *FileName); int GetColor(unsigned char R,unsigned char G, unsigned char B,int X,int Y); void SetVideoMode(unsigned char Mode); void SetPalReg(unsigned char *palReg); void SetDacReg(unsigned char *DacReg, int Color, int Count); void PutPixel(int X, int Y, unsigned char Color); /* 主函数 */ void main (int argc, char *argv[]) { if(argc!=2) { printf("Usage:\tSHOW Filename.BMP\n"); exit(1); } ShowBmp(argv[1]); } /* 根据图像文件名,读取图像内容并利用抖动技术进行显示 */ int ShowBmp(char *FileName) { FILE *Fp; BITMAPFILEHEADER FileHead; BITMAPINFOHEADER InfoHead; RGBQUAD RGB; int N, W,Y,X,C,Color; unsigned char Buffer[4096]; Fp=fopen(FileName,"rb"); if (Fp==NULL) return(ErrorFileOpen); fread(&FileHead,sizeof( BITMAPFILEHEADER),1,Fp); if(FileHead.bfType!='BM') { fclose(Fp); return(ErrorFileType); } fread(&InfoHead,sizeof( BITMAPINFOHEADER),1,Fp); if(InfoHead.biBitCount!=8 && InfoHead.biBitCount!=24) { fclose(Fp); return(ErrorImageColor); } /* 设置显示模式和显示区域 */ SetVideoMode(0x12); SetPalReg(PalReg); SetDacReg(StandardPal,0,16); /* 对两种不同色彩数的图像分别进行处理 */ if(InfoHead.biBitCount==8) /* 256色 */ { for (N=0;N<256;N++) { fread(&RGB, sizeof(RGBQUAD),1,Fp); ColorMap[N][0]=RGB.rgbRed; ColorMap[N][1]=RGB.rgbGreen; ColorMap[N][2]=RGB.rgbBlue; } W=(InfoHead.biWidth+3)/4*4; for(Y=InfoHead.biHeight-1;Y>= 480;Y--) fread(Buffer,sizeof(unsigned char),W,Fp); for(;Y>0;Y--) { fread(Buffer,sizeof(unsigned char),W,Fp); for (X=0;X<InfoHead.biWidth && X<640;X++) { C=Buffer[X]; Color=GetColor(ColorMap[C][0], ColorMap[C][1],ColorMap[C][2], X,Y); PutPixel (X,Y,Color); } } } else /* 24bits真彩色 */ { W=(InfoHead.biWidth*3+3)/4*4; for(Y=InfoHead.biHeight-1;Y> 639;Y--) fread(Buffer,sizeof(unsigned char),W,Fp); for(;Y>=0;Y--) { fread(Buffer,sizeof(unsigned char),W,Fp); for(X=0;X<InfoHead.biWidth && X<640;X++) { C=X*3; Color=GetColor(Buffer[C+2], Buffer[C+1],Buffer[C],X,Y); PutPixel(X,Y,Color); } } } getch(); fclose(Fp); SetVideoMode(0x03); return(NoError); } int GetColor(unsigned char R, unsigned char G, unsigned char B, int X, int Y) { unsigned int L=LightnessMatrix[Y & 0x0F][X & 0x0F]; return(ColorTable[(unsigned int)R*256/255>L][(unsigned int)G*256/255>L][(unsigned int)B*256/255>L]); } void SetVideoMode(unsigned char Mode) { _AH=0x00; _AL=Mode; geninterrupt(0x10); } void SetPalReg(unsigned char *PalReg) { _ES=FP_SEG((unsigned char far*)PalReg); _DX=FP_OFF((unsigned char far*)PalReg); _AX=0x1002; geninterrupt(0x10); } void SetDacReg(unsigned char *DacReg,int Color,int Count) { _ES=FP_SEG((unsigned char far*)DacReg); _DX=FP_OFF((unsigned char far*)DacReg); _AX=0x1012; _BX=Color; _CX=Count; geninterrupt(0x10); } /* 在对应位置显示像素色彩 */ void PutPixel(int X, int Y, unsigned char Color) { _AH=0x0C; _AL=Color; _CX=X; _DX=Y; geninterrupt(0x10); } === 再给个实例链接,刚找到的: http://www.turinger.com/ article_view.asp?id=3
⑧ C语言如何生成位图文件
去了解位图的文件头结构和位图的数据结构,然后按照这些数据格式写成数据文件流保存成位图文件就可了。网上有比较多的C语言范例可以参考。
⑨ C语言操作位图
去找本《Visual C++ 数字图像处理》 (人民邮电出版社)
另外这本书还有其他基本配套的书,涵盖了所有的图像处理内容
⑩ c语言读取位图编程
我也遇到过类似的问题,位图数据读取到内存,然后在保存的文件中。
新位图文件,虽然能够显示但是图片内容中存在大量错误。
后来发现问题的关键在于
fopen(bmpname,"rb");
注意rb标志,不管是只读标志还是只写标志,或者读写标志后面要加+。
也就是rb+
不加+的标志是针对的文本读写的。
加+的标志是针对二进制数据读写的。
而位图文件是一种二进制数据。