用c++编写的读取bmp图中任意一像素点的RGB值的代码

谢谢... 谢谢 展开
 我来答
百度网友e9042513a
推荐于2016-05-11 · TA获得超过6984个赞
知道大有可为答主
回答量:2388
采纳率:0%
帮助的人:3464万
展开全部
控制台 读取BMP图片文件 输出指定点的颜色分量

#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);

while(1)
{
printf("input the X:");
scanf("%d", &x);
if (x < 0)
break;
printf("input the Y:");
scanf("%d", &y);

if (GetDIBColor(x, y, &r, &g, &b) == 1)
printf("(%d, %d): r:%d, g:%d, b:%d\n", x, y, (int)r, (int)g, (int)b);
else
printf("input error.\n");
}

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;
}

参考资料: 原创

辛文琴元枫
2019-04-01 · TA获得超过4024个赞
知道大有可为答主
回答量:3216
采纳率:30%
帮助的人:211万
展开全部
控制台
读取BMP图片文件
输出指定点的颜色分量
#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);
while(1)
{
printf("input
the
X:");
scanf("%d",
&x);
if
(x
<
0)
break;
printf("input
the
Y:");
scanf("%d",
&y);
if
(GetDIBColor(x,
y,
&r,
&g,
&b)
==
1)
printf("(%d,
%d):
r:%d,
g:%d,
b:%d\n",
x,
y,
(int)r,
(int)g,
(int)b);
else
printf("input
error.\n");
}
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;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式