这个程序是原来帮别人写个位图迷宫的一部分


主要是对2位的黑白图片读取到数组中,再显示出来,最后再搜索迷宫(未做迷宫搜索);
bmp图片格式就不多介绍了,百度下就有了。
图片为50*50的位图
效果图如下:
-----------------------------------------------------------------------------
----------------------------------------------------------------------------------
源代码:
#include "stdio.h"
#include "Windows.h"
#include "stdlib.h"
#include "math.h"
int Ma[200][200];
typedefstruct bmpMaze
{
char *bmp;//图像指针
char *bmpName;//图像路径
int width;//宽
int height;//图像的高
int biBitCount;//图像类型,每像素位数
int lineByte;
int datasize;
BITMAPINFOHEADER head;
BITMAPFILEHEADER file;
RGBQUAD color[2];
}bmpMaze;
int readBmp(bmpMaze *maze)
{
int i=0;
FILE *fp=fopen(maze->bmpName,"rb");
if(fp==NULL)
{
printf("File is not found !!\n");
return 0;
}
fread(&(maze->file), sizeof(BITMAPFILEHEADER), 1,fp);
fread(&(maze->head), sizeof(BITMAPINFOHEADER), 1,fp);
fread(&(maze->color), 8, 1,fp);
maze->width = maze->head.biWidth;
maze->height = maze->head.biHeight;
maze->biBitCount = maze->head.biBitCount;
maze->lineByte=(maze->width+8-maze->width%8)/8;
if(maze->width%8==0)
maze->lineByte--;
maze->datasize=maze->file.bfSize-maze->file.bfOffBits-2;
maze->bmp=(char *)malloc(maze->datasize*sizeof(char));
fread(maze->bmp,maze->datasize,1,fp);
fclose(fp);
return 1;
}
void MazeToArray(bmpMaze maze)
{
//将图片像素信息读取到Ma数组中
int status,i=0,x,y,n=8,j=0,line;
line=maze.datasize/maze.height;
for(x=maze.height-1;x>=0;x--)
{
for(y=0;y<maze.width;y++)
{
//获取这个点是黑的还是白的
status=maze.bmp[i]&(int)pow(2,n-1);
if(!status)
Ma[x][y]=0;
else
Ma[x][y]=1;
n--;
if(n==0)
{
n=8;
i++;
}
}
i=i+(line-maze.lineByte+1);
n=8;
}
}
void printMaze(bmpMaze maze)
{
int x=0,y=0;
for(x=0;x<maze.height;x++)
{
for(y=0;y<maze.width;y++)
printf("%d",Ma[x][y]);
printf("\n");
}
}
void main()
{
bmpMaze maze;
maze.bmpName="test.bmp";
readBmp(&maze);
MazeToArray(maze);
printMaze(maze);
}
本文介绍如何使用C语言读取2位黑白BMP位图并将其显示,针对50*50像素的位图进行操作,虽然未涉及迷宫搜索功能,但提供了源代码实现。
1万+

被折叠的 条评论
为什么被折叠?



