C++编程 读取bmp图像 将每个点的R,G值输出
R值与B值分别以二维数组的方式输出合计两个二维数组原bmp图像中的黑色的点为边界回答优质会追加悬赏...
R值与B值分别以二维数组的方式输出
合计两个二维数组
原bmp图像中的黑色的点为边界
回答优质会追加悬赏 展开
合计两个二维数组
原bmp图像中的黑色的点为边界
回答优质会追加悬赏 展开
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励30(财富值+成长值)
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[64000];
unsigned short R[64000],[64000];
int x,y;
index=0;
for(x=0;x<bih.biWidth;x++)
{
for(y=0;y<bih.biHeight;y++)
{
if (GetDIBColor(x, y, &r, &g, &b) == 1)
{
R[i*bih.biWidth+j]=(unsigned short)r;
G[i*bih.biHeight+j]=(unsigned short)g;
printf("R[%d][%d]=%d ",&x,&y,r);
printf("G[%d][%d]=%d ",&x,&y,g);
}
else
printf("error[%d,%d].\n",&x,&y);
}
}
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[64000];
unsigned short R[64000],[64000];
int x,y;
index=0;
for(x=0;x<bih.biWidth;x++)
{
for(y=0;y<bih.biHeight;y++)
{
if (GetDIBColor(x, y, &r, &g, &b) == 1)
{
R[i*bih.biWidth+j]=(unsigned short)r;
G[i*bih.biHeight+j]=(unsigned short)g;
printf("R[%d][%d]=%d ",&x,&y,r);
printf("G[%d][%d]=%d ",&x,&y,g);
}
else
printf("error[%d,%d].\n",&x,&y);
}
}
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;
}
追问
各种报错...能帮忙调试一下吗
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询