用c语言读取24位位图bmp文件 如何用C语言来显示一张24位真色彩的BMP图片

作者&投稿:糜吕 (若有异议请与网页底部的电邮联系)

可以使用C语言标准函数库中的fopen、fseek、fclose等系列函数来打开bmp位图文件,以及进行相应的处理,下面是一个demo,仅供参考。以下代码在vc6.0中编译通过。


#include <stdio.h>
#include <stdlib.h>
#define BITMAPFILEHEADERLENGTH 14   // The bmp FileHeader length is 14
#define BM 19778                    // The ASCII code for BM
/* Test the file is bmp file or not */
void bmpFileTest(FILE* fpbmp);
/* To get the OffSet of header to data part */
void bmpHeaderPartLength(FILE* fpbmp);
/* To get the width and height of the bmp file */
void BmpWidthHeight(FILE* fpbmp);
//get r,g,b data
void bmpDataPart(FILE* fpbmp);
// output data to corresponding txt file
void bmpoutput(FILE *fpout);
unsigned int OffSet = 0;    // OffSet from Header part to Data Part
long width ;          // The Width of the Data Part
long height ;         // The Height of the Data Part
unsigned char r[2000][2000],output_r[2000][2000];
unsigned char g[2000][2000],output_g[2000][2000];
unsigned char b[2000][2000],output_b[2000][2000];
int main(int argc, char* argv[])
{
     /* Open bmp file */
unsigned char *fp_temp;
     FILE *fpbmp;
     FILE *fpout;
     fpbmp= fopen("1.bmp", "rb");
     if (fpbmp == NULL)
     {
 printf("Open bmp failed!!!
");
 return 1;
     }
     fpout= fopen("out.bmp", "wb+");
     if (fpout == NULL)
     {
 printf("Open out.bmp failed!!!
");
 return 1;
     }
     
     bmpFileTest(fpbmp);                //Test the file is bmp file or not
     bmpHeaderPartLength(fpbmp);        //Get the length of Header Part
     BmpWidthHeight(fpbmp);             //Get the width and width of the Data Part
     
     
//
fseek(fpbmp, 0L, SEEK_SET);
fseek(fpout, 0L, SEEK_SET);
 
fp_temp=(unsigned char *)malloc(OffSet);
         fread(fp_temp, 1, OffSet, fpbmp);
fwrite(fp_temp,1,OffSet,fpout);
     
bmpDataPart(fpbmp);                //Reserve the data to file 
     
/*
 
 
 如果您想对图片进行处理,请您再这里插入处理函数!!!!!!!!!!!!!!!!!!
 
*/
bmpoutput(fpout);
fclose(fpbmp);
fclose(fpout);
         return 0;
}
void bmpoutput(FILE* fpout)
{
         int i, j=0;
         int stride;
unsigned char* pixout=NULL;
   
stride=(24*width+31)/8;
stride=stride/4*4;
pixout=(unsigned char *)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);
}
}
void bmpDataPart(FILE* fpbmp)
{
         int i, j=0;
int stride;
unsigned char* pix=NULL;
FILE* fpr;
         FILE* fpg;
FILE* fpb;
     
     if((fpr=fopen("bmpr.txt","w+")) == NULL)
     {
    printf("Failed to construct file bmpr.txt.!!!");
exit(1);
     }
     if((fpg=fopen("bmpg.txt","w+")) == NULL)
     {
 printf("Failed to construct file bmpg.txt.!!!");
 exit(1);
     }
if((fpb=fopen("bmpb.txt","w+")) == NULL)
     {
printf("Failed to construct file bmpb.txt.!!!");
exit(1);
     }
 
     fseek(fpbmp, OffSet, SEEK_SET);
stride=(24*width+31)/8;
stride=stride/4*4;
pix=(unsigned char *)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);
 
}
void bmpFileTest(FILE* fpbmp)
{     
     unsigned short bfType = 0;
 
     fseek(fpbmp, 0L, SEEK_SET);//seek_set 起始位置
     fread(&bfType, sizeof(char), 2, fpbmp);
     if (BM != bfType)
     {
 printf("This file is not bmp file.!!!
");
 exit(1);
     }
}
/* To get the OffSet of header to data part */
void bmpHeaderPartLength(FILE* fpbmp)
{
     fseek(fpbmp, 10L, SEEK_SET);
     fread(&OffSet, sizeof(char), 4, fpbmp);  
     printf("The Header Part is of length %d.
", OffSet);
}
/* To get the width and height of the bmp file */
void BmpWidthHeight(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("The Width of the bmp file is %ld.
", width);
     printf("The Height of the bmp file is %ld.
", height);
}


如果横向的一行叫做一个“扫描线”的话
那么每条扫描线的第一个字节一定是对齐到4字节边界的
就比如,假设一条扫描线21个像素,那么一条扫描线就是21*3=63字节
实际上每条扫描线会占用64字节,因为下一条扫描线的第一个字节要对齐到4字节边界
(所谓对齐到边界,就是 除4余0)

注意:以上是凭印象说的。文件里保存和内存里保存是不是一样的不太确定……

最简单的解决方案,你要加载的BMP都确定宽是4的倍数就可以了

行像素错了一位,导致图像倾斜了。

~

用C语言编写程序处理图片bmp文件 1.读取图片的宽度,高度,每个像素所需...
答:include <windows.h>//读bmp图片需要两个结构#pragma pack(push, enter_defBM, 1) //指定内存对齐单位为1。typedef struct tagBmpFileHeader{WORD bfType; // 文件类型 BMDWORD bfSize; // 文件大小WORD bfReserved1; // 保留字WORD bfReserved2; // 保留字DWORD bfOffBits; //...

怎么样在c语言中显示bmp图片,我要完整正确的程序,急!
答:其他回答 单纯靠c语言是难以实现的老兄,要么用API,或者用MFC chenweitiang | 发布于2013-07-26 举报| 评论(1) 4 5 为您推荐: c 显示bmp图片 c语言怎么显示图像 c语言排序函数 c语言像素点 c语言BMP输出 c语言 bmp图像剪裁 读取bmp文件的代码 libbmp bmp图片读取 ...

C语言读写图片文件问题
答:BMP位图文件有它的属性,我们如果不读取他的属性的话,无法对它的操作,所以BMP位图文件包括1.位图文件头(记录位图文件的特征.到真正图像数据的偏 移量.文件大小等等信息)2.位图信息头(记录的就是位图的大小,每个像素占的位 数,是否压缩等等信息)3.调色板(如果位图信息头里面的每个像素的位数小于24 位...

c语言调用图片
答:图片也是一个文件,1.你是要打开图片吗?(把图像显示出来?)2.还是只需要图片文件。如果是1,那么你需要看.bmp的编码方式和C库的图像类函数 如果是2,那么你就可以用fopen,fread,fwrite,fprintf,fscanf等调用即可。

如何用c语言printf输出bmp图片的像素信息。
答:每像素位数//---//读图像的位图数据、宽、高、颜色表及每像素位数等数据进内存,存放在相应的全局变量中bool readBmp(char *bmpName) { FILE *fp=fopen(bmpName,"rb");//二进制读方式打开指定的图像文件 if(fp==0) return 0; //跳过位图文件头结构BITMAPFILEHEADER fseek(fp,...

如何用C语言程序从bmp格式的图片中读取图片的灰度值?
答:注意raw格式图像要以只读二进制流的形式打开if(!fp){printf("ERROR!\n");}unsigned char *pData=new unsigned char[256*256]; //注意:raw图像用无符号char型读入fread(pData,sizeof(unsigned char),(256*256),fp); //fread具体用法见msdnfclose(fp); //取消fp指针指向int value[256]={0...

如何用C语言程序从bmp格式的图片中读取图片的灰度值
答:打开bmp文件,把前面三部分的字节总数给固定下来,逐个字符读取,然后读取数据实体部分,输出就可以了。2、例程:include <stdio.h>#include <stdlib.h>#pragma pack(2)/*定义WORD为两个字节的类型*/typedef unsigned short WORD;/*定义DWORD为e四个字节的类型*/typedef unsigned long DWORD;/*位图...

C语言位移,图片处理
答:24位bmp for(i = 0; i < 800 * 480; i++){ arr[i * 4] = buf[i * 3];arr[i * 4 + 1] = buf[i * 3 + 1];arr[i * 4 + 2] = buf[i * 3 + 2];arr[i * 4 + 3] = 0; //不知道你要写什么符号.} ...

怎么使用C语言在显示器上显示出BMP图片
答:include <windows.h> int main(int argc, char* argv[]){ HANDLE h;HDC dc1,dc2;BITMAP bmp;dc1=GetDC(0);//得到屏幕DC h=LoadImage(NULL,"c:\\image.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);dc2=CreateCompatibleDC(dc1);SelectObject(dc2,h);GetObject(h,sizeof(bmp),&bmp);BitBlt(...

用C语言怎么将一张BMP格式的图片识别并改变颜色?
答:你说用c语言是说直接用c的语法但是不用API吗? 如果是的话你需要对BMP格式进行剖析 分析BMP的格式,这个我没有办法 但是用Windows API的话我可以给点思路你.LoadBitmap //获取图片文件句柄 GetObject //传入一个BITMAP结构 获取位图的信息 比如:长宽高 GetBitmapBits//获取像素点 //对保存像素点缓冲...