C++读取24位位图的RGB信息,求大神帮忙。。。。。 100
我在D盘根目录有一个24位的位图AAA.bmp(200x200像素的),怎么能将它装载到程序里,然后读出每一个像素的RGB读出来放进数组buffer[200*200*3]...
我在D盘根目录有一个24位的位图 AAA.bmp (200x200像素的),怎么能将它装载到程序里,然后读出每一个像素的RGB读出来放进数组buffer[200*200*3]里面,求大神帮忙。不要用MFC,我是新手,只会点WIN32的编程,要能编译通过的,说思路的就不要了,成了之后再追加100.
展开
2个回答
展开全部
这是从别人的程序改来的,不过应该能用。
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef unsigned long DWORD;
typedef int BOOL;
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef struct tagBITMAPINFOHEADER{
DWORD biSize;
long biWidth;
long biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
long biXPelsPerMeter;
long biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} BITMAPINFOHEADER;
int ReadBmp(const char* szFileName);
int GetDIBColor(int X, int Y, BYTE *r, BYTE *g, BYTE *b);
BITMAPINFOHEADER bih;
BYTE *Buffer = NULL;
long LineByteWidth;
int main(void)
{
int x, y;
BYTE r, g, b;
int n;
char szfilename[255] = "c:\\1.bmp";
if (ReadBmp(szfilename) == 0)
{
printf("failure to read file %s", szfilename);
return 1;
}
printf("Width: %ld\n", bih.biWidth);
printf("Height: %ld\n", bih.biHeight);
printf("BitCount: %d\n\n", (int)bih.biBitCount);
int bmpbuffer[200*200*3];
while(1)
{
int x,y,index;
index=0;
for(x=0;x<200;x++)
{
for(y=0;y<200;y++)
{
if (GetDIBColor(x, y, &r, &g, &b) == 1)
bmpbuffer[index*3+1]=(int)r;
bmpbuffer[index*3+2]=(int)g;
bmpbuffer[index*3+3]=(int)b;
else
printf("error[%d,%d].\n",&x,&y);
bmpbuffer[index*3+1]=0;
bmpbuffer[index*3+2]=0;
bmpbuffer[index*3+3]=0;
index++;
}
}
free(Buffer);
return 0;
}
int ReadBmp(const char* szFileName)
{
FILE *file;
WORD bfh[7];
long dpixeladd;
if (NULL == (file = fopen(szFileName, "rb")))
{
return 0;
}
printf("%s\n", szFileName);
fread(&bfh, sizeof(WORD), 7, file);
if (bfh[0] != (WORD)(((WORD)'B')|('M'<<8)))
{
fclose(file);
return 0;
}
fread(&bih, sizeof(BITMAPINFOHEADER), 1, file);
if (bih.biBitCount < 24)
{
fclose(file);
return 0;
}
dpixeladd = bih.biBitCount / 8;
LineByteWidth = bih.biWidth * (dpixeladd);
if ((LineByteWidth % 4) != 0)
LineByteWidth += 4 - (LineByteWidth % 4);
if ((Buffer = (BYTE*)malloc(sizeof(BYTE)* LineByteWidth * bih.biHeight)) != NULL)
{
fread(Buffer, LineByteWidth * bih.biHeight, 1, file);
fclose(file);
return 1;
}
fclose(file);
return 0;
}
int GetDIBColor(int X, int Y, BYTE *r, BYTE *g, BYTE *b)
{
int dpixeladd;
BYTE *ptr;
if (X < 0 || X >= bih.biWidth || Y < 0 || Y >= bih.biHeight)
{
return 0;
}
dpixeladd = bih.biBitCount / 8;
ptr = Buffer + X * dpixeladd + (bih.biHeight - 1 - Y) * LineByteWidth;
*b = *ptr;
*g = *(ptr + 1);
*r = *(ptr + 2);
return 1;
}
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef unsigned long DWORD;
typedef int BOOL;
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef struct tagBITMAPINFOHEADER{
DWORD biSize;
long biWidth;
long biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
long biXPelsPerMeter;
long biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} BITMAPINFOHEADER;
int ReadBmp(const char* szFileName);
int GetDIBColor(int X, int Y, BYTE *r, BYTE *g, BYTE *b);
BITMAPINFOHEADER bih;
BYTE *Buffer = NULL;
long LineByteWidth;
int main(void)
{
int x, y;
BYTE r, g, b;
int n;
char szfilename[255] = "c:\\1.bmp";
if (ReadBmp(szfilename) == 0)
{
printf("failure to read file %s", szfilename);
return 1;
}
printf("Width: %ld\n", bih.biWidth);
printf("Height: %ld\n", bih.biHeight);
printf("BitCount: %d\n\n", (int)bih.biBitCount);
int bmpbuffer[200*200*3];
while(1)
{
int x,y,index;
index=0;
for(x=0;x<200;x++)
{
for(y=0;y<200;y++)
{
if (GetDIBColor(x, y, &r, &g, &b) == 1)
bmpbuffer[index*3+1]=(int)r;
bmpbuffer[index*3+2]=(int)g;
bmpbuffer[index*3+3]=(int)b;
else
printf("error[%d,%d].\n",&x,&y);
bmpbuffer[index*3+1]=0;
bmpbuffer[index*3+2]=0;
bmpbuffer[index*3+3]=0;
index++;
}
}
free(Buffer);
return 0;
}
int ReadBmp(const char* szFileName)
{
FILE *file;
WORD bfh[7];
long dpixeladd;
if (NULL == (file = fopen(szFileName, "rb")))
{
return 0;
}
printf("%s\n", szFileName);
fread(&bfh, sizeof(WORD), 7, file);
if (bfh[0] != (WORD)(((WORD)'B')|('M'<<8)))
{
fclose(file);
return 0;
}
fread(&bih, sizeof(BITMAPINFOHEADER), 1, file);
if (bih.biBitCount < 24)
{
fclose(file);
return 0;
}
dpixeladd = bih.biBitCount / 8;
LineByteWidth = bih.biWidth * (dpixeladd);
if ((LineByteWidth % 4) != 0)
LineByteWidth += 4 - (LineByteWidth % 4);
if ((Buffer = (BYTE*)malloc(sizeof(BYTE)* LineByteWidth * bih.biHeight)) != NULL)
{
fread(Buffer, LineByteWidth * bih.biHeight, 1, file);
fclose(file);
return 1;
}
fclose(file);
return 0;
}
int GetDIBColor(int X, int Y, BYTE *r, BYTE *g, BYTE *b)
{
int dpixeladd;
BYTE *ptr;
if (X < 0 || X >= bih.biWidth || Y < 0 || Y >= bih.biHeight)
{
return 0;
}
dpixeladd = bih.biBitCount / 8;
ptr = Buffer + X * dpixeladd + (bih.biHeight - 1 - Y) * LineByteWidth;
*b = *ptr;
*g = *(ptr + 1);
*r = *(ptr + 2);
return 1;
}
更多追问追答
追答
看来是tagBITMAPINFOHEADER的问题 是不是命名冲突啊 你试试在tagBITMAPINFOHEADER
后面加几个1变成tagBITMAPINFOHEADER111。我这里一时没有C++环境,没法测试。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询