求助bmp图像的识别
求助,用C语言读取bmp图像并显示,显示结果如下一般是出现了什么问题呀
排查后感觉是读取数据的问题
void getData1(FILE* fp, Point** Pix, long height, long width, int offset)
{
FILE* fpr;
errno_t err_pr;
FILE* fpg;
errno_t err_pg;
FILE* fpb;
errno_t err_pb;
int i, j = 0;
int stride;
unsigned char* pix = NULL;
fseek(fp, offset, SEEK_SET); //找到位图的数据区
stride = (24 * width + 31) / 8; //对齐,计算一行有多少个8位
stride = stride / 4 * 4; //取四的倍数 r,g,b,alph
//写入数组
pix = (unsigned char*)malloc(stride);
for (j = 0; j < height; j++)
{
fread(pix, 1, stride, fp);
for (i = 0; i < width; i++)
{
Pix[height - j - 1][i].r = pix[i * 3 + 2];
Pix[height - j - 1][i].g = pix[i * 3 + 1];
Pix[height - j - 1][i].b = pix[i * 3];
}
}
err_pr = fopen_s(&fpr, "bmpr1.txt", "w+");
err_pg = fopen_s(&fpg, "bmpg1.txt", "w+");
err_pb = fopen_s(&fpb, "bmpb1.txt", "w+");
for (i = 0; i < height; i++)
{
for (j = 0; j < width; j++)
{
fprintf(fpr, "%4d", Pix[i][j].r);
fprintf(fpg, "%4d", Pix[i][j].g);
fprintf(fpb, "%4d", Pix[i][j].b);
}
//fprintf(fpr, "\n");
//fprintf(fpg, "\n");
//fprintf(fpb, "\n");
}
}
这是main函数中的 调用
int main() {
int i, j;
ST_BITMAP* st_tp_bitmap = NULL;
U4 offset1;
U4 width1;
U4 height1;
U2 bitCount1;
FILE* fps;
errno_t err;
err = fopen_s(&fps, "adc_p0.bmp", "rb");//读取同目录下的image.bmp文件。
if (err)
{
printf("打开'image.bmp'失败!\n");
return -1;
}
unsigned short fileType;
fread(&fileType, 1, sizeof(unsigned short), fps);
if (fileType = 0x4d42)
{
printf("文件类型标识正确!");
printf("\n文件标识符:%d\n", fileType);
fread(&fileHeader, 1, sizeof(BITMAPFILEHEADER), fps);
showBmpHead(fileHeader);
fread(&infoHeader, 1, sizeof(BITMAPINFOHEADER), fps);
showBmpInfoHead(infoHeader);
width1 = infoHeader.biWidth;
height1 = infoHeader.biHeight;
bitCount1 = infoHeader.biBitCount;
offset1 = fileHeader.bfOffBits;
Point** pix;
//每一个像素申请内存
pix = (Point**)malloc(sizeof(Point*) * height1);
for (i = 0; i < height1; i++)
*(pix + i) = (Point*)malloc(sizeof(Point) * width1);
getData1(fps, pix, height1, width1, offset1);
do
{
// create bitmap, size:20x10 format:RGB555->16
// also support format RGB888->24 and RGBA8888->32
st_tp_bitmap = st_g_CreateBitmap(width1, height1, bitCount1);
if (st_tp_bitmap == NULL)
break;
// draw pixels on bitmap
for (i = 0; i < width1; i++) {
for (j = 0; j < height1; j++) {
vd_SetBitmapPixel(st_tp_bitmap, i, j, BMP_RGB24(&pix[i][j].r, &pix[i][j].g, &pix[i][j].b)); // red
//vd_SetBitmapPixel(st_tp_bitmap, i, j, BMP_RGB24(0, &pix[i][j].g, 0)); // green
//vd_SetBitmapPixel(st_tp_bitmap, i, j, BMP_RGB24(0, 0, &pix[i][j].b)); // blue
}
}
// save to file
vd_g_SaveBitmap(st_tp_bitmap, "test.bmp");
for (i = 0; i < height1; i++) {
for (j = 0; j < width1; j++)
printf("%4d", pix[i][j].r);
printf("\n");
}
printf("g的位图矩阵\n");
for (i = 0; i < height1; i++) {
for (j = 0; j < width1; j++)
printf("%4d", pix[i][j].g);
printf("\n");
}
printf("b的位图矩阵\n");
for (i = 0; i < height1; i++) {
for (j = 0; j < width1; j++)
printf("%4d", pix[i][j].b);
printf("\n");
}
} while (FALSE);