考验
zds1 2001-12-17 09:54:38 我的程序是读BMP文件,数据读出是负数为什么,我把数据改为unsigned int也不行
#include <stdio.h>
#include <malloc.h>
#include <afx.h>
struct PIXMAP
{
int rowbytes;
int height;
int size;
unsigned int * add;
};
struct IMAGE
{
int width;
int height;
int size;
char * address;
char head;
};
void main()
{
unsigned int width,height,size=NULL;
unsigned int r=NULL;
unsigned g=NULL;
unsigned int b=NULL;
unsigned int * data;
unsigned int * p=NULL;
char *y,*u,*v;
FILE * fp=NULL;
char path[20];
BITMAPFILEHEADER *head;
BITMAPINFOHEADER *info;
struct PIXMAP pixmap;
struct IMAGE plane;
printf("请输入文件路径\n");
scanf("%s",path);
if((fp=fopen(path,"rb+"))==NULL)
{
printf("空文件\n");
return;
}
else
{
head=(BITMAPFILEHEADER *)malloc(sizeof(BITMAPFILEHEADER));
info=(BITMAPINFOHEADER *)malloc(sizeof(BITMAPFILEHEADER));
fread(head,sizeof(BITMAPFILEHEADER),1,fp);
printf("%d\n",head->bfOffBits);
printf("%d\n",head->bfSize);
fread(info,sizeof(BITMAPINFOHEADER),1,fp);
width=info->biWidth;
height=info->biHeight;
printf("%d\n",info->biBitCount);
pixmap.height=height;
pixmap.size=head->bfSize-head->bfOffBits;
data=(unsigned int *)malloc(pixmap.size);
printf("%d\n",pixmap.size);
y=(char *)malloc(pixmap.size);
u=(char *)malloc(pixmap.size/4);
v=(char *)malloc(pixmap.size/4);
fread(data,pixmap.size,1,fp);
pixmap.add=data;
unsigned int g=*data;
printf("%d\n",g);
fclose(fp);
}
/* if(info->biBitCount==24)
{
pixmap.rowbytes=width*3;
for(int i=0;i<info->biHeight;i++)
{
pixmap.add+=i*pixmap.rowbytes;
for(int j=0;j<info->biWidth;j++)
{*/
p=pixmap.add;
r=*p++;
g=*p++;
b=*p++;
printf("%d,%d,%d\n",r,g,b);
// }
// }
// }
/* if(info->biBitCount==32)
{
}
else
{
}
free(data);
free(y);
free(u);
free(v);
free(head);
free(info);*/
}