各位大哥帮帮忙看看

时间:2021-11-03 19:58:30
我的程序是读BMP文件,数据读出是负数为什么
#include <stdio.h>
#include <malloc.h>
#include <afx.h>
struct PIXMAP
{
int rowbytes;
int height;
int size;
char * 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;
    char * data;
    char * 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=(char *)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);*/
}

5 个解决方案

#1


你把char * data;
定义为unsigned int * data;
就行了!

#2


不行啊

#3


这个代码,可以执行,各位可以考下去,找一幅BMP(每像素24位)图象执行一下看看

#4


由于Bmp文件除了文件头信息可能是负数之外,其他的数据都为正数,即是0-255,所以你在定义数据结构的时候,一定要注意读出的数据应该是无符号型的,否则它读出的数据就会出现负数,如255就会读成-1,把程序改成如下就可以了。
#include <stdio.h>
#include <malloc.h>
#include <afx.h>
struct PIXMAP
{
int rowbytes;
int height;
int size;
char * 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 int g=NULL;
unsigned int b=NULL;
unsigned int * data;
char * 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);

p=pixmap.add;
r=*p++;
g=*p++;
b=*p++;
printf("%d,%d,%d\n",r,g,b);

#5


谢谢,不过我曾经改过,不行,还是负数

#1


你把char * data;
定义为unsigned int * data;
就行了!

#2


不行啊

#3


这个代码,可以执行,各位可以考下去,找一幅BMP(每像素24位)图象执行一下看看

#4


由于Bmp文件除了文件头信息可能是负数之外,其他的数据都为正数,即是0-255,所以你在定义数据结构的时候,一定要注意读出的数据应该是无符号型的,否则它读出的数据就会出现负数,如255就会读成-1,把程序改成如下就可以了。
#include <stdio.h>
#include <malloc.h>
#include <afx.h>
struct PIXMAP
{
int rowbytes;
int height;
int size;
char * 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 int g=NULL;
unsigned int b=NULL;
unsigned int * data;
char * 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);

p=pixmap.add;
r=*p++;
g=*p++;
b=*p++;
printf("%d,%d,%d\n",r,g,b);

#5


谢谢,不过我曾经改过,不行,还是负数