c读取bmp图片文件像素值不符问题
读取bmp文件之后,查看读取后的像素值,发现和实际不符。读取的图片的位深度为8bit。
读和写图片都能成功运行,就是读取出来的数据不符。
用软件查看的图片像素值:11
在vs2010调试时查看的图片像素值:10
一下为读取图片和保存图片的代码:
#ifndef _DATA_H_
#define _DATA_H_
#pragma pack(1)
typedef struct BITMAPFILEHEADER
{
unsigned short bfType;
unsigned long bfSize;
unsigned short bfReserved1;
unsigned short bfReserved2;
unsigned long bfOffBits;
} BITMAPFILEHEADER;
typedef struct BITMAPINFOHEADER
{
unsigned long biSize;
long biWidth;
long biHeight;
unsigned short biPlanes;
unsigned short biBitCount;
unsigned long biCompression;
unsigned long biSizeImage;
long biXPelsPerMeter;
long biYPelsPerMeter;
unsigned long biClrUsed;
unsigned long biClrImportant;
}BITMAPINFOHEADER;
#pragma pack()
typedef struct RGBQUAD
{
unsigned char rgbBlue;
unsigned char rgbGreen;
unsigned char rgbRed;
unsigned char rgbReserved;
}RGBQUAD;
typedef struct BMP
{
int biWidth;
int biHeight;
unsigned short biBitCount;
RGBQUAD *pColorTable;
unsigned char *pBmpBuf;
}BMP;
#endif
#include "data_info.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
int readBmp( char *bmpName,BMP * p);
int saveBmp( char *bmpName,BMP * p);
void main()
{
int a,b;
BMP *p;
p=(BMP *)malloc(sizeof(BMP));
a=readBmp("peppers.bmp",p);
b=saveBmp("HAPPY.bmp",p);
system("pause");
}
int readBmp( char *bmpName,BMP * p_im){
BITMAPINFOHEADER head;
int bmpWidth;
int bmpHeight;
int lineByte;
int biBitCount;
FILE *fp=fopen(bmpName,"rb");
if(fp==0) return 0;
fseek(fp, sizeof(BITMAPFILEHEADER),0);
//fread(&fileHead, sizeof(BITMAPFILEHEADER), 1,fp);
fread(&head, sizeof(BITMAPINFOHEADER), 1,fp);
bmpWidth = head.biWidth;
bmpHeight= head.biHeight;
biBitCount = head.biBitCount;
lineByte=(bmpWidth * biBitCount/8+3)/4*4;
if(biBitCount==8){
p_im->pColorTable=(RGBQUAD *)malloc( sizeof(RGBQUAD)*256);
fread(p_im->pColorTable,sizeof(RGBQUAD),256,fp);
}
p_im->pBmpBuf=(unsigned char*)malloc(lineByte*bmpHeight*sizeof(unsigned char));
fread(p_im->pBmpBuf,1,lineByte * bmpHeight,fp);
p_im->biBitCount=biBitCount;
p_im->biHeight=bmpHeight;
p_im->biWidth=bmpWidth;
fclose(fp);
return 1;
}
int saveBmp( char *bmpName,BMP * p)
{
FILE *fw;
BITMAPFILEHEADER fileHead;
BITMAPINFOHEADER head;
int lineByte=0;
int colorTablesize=0;
if(!p->pBmpBuf)
return 0;
if(p->biBitCount==8) colorTablesize=1024;
lineByte=(p->biWidth * p->biBitCount/8+3)/4*4;
fw=fopen(bmpName,"wb");
if(fw==0) return 0;
fileHead.bfType = 0x4D42;
fileHead.bfSize= sizeof(BITMAPFILEHEADER)+ sizeof(BITMAPINFOHEADER)+ colorTablesize +lineByte*(p->biHeight);
fileHead.bfReserved1 = 0;
fileHead.bfReserved2 = 0;
fileHead.bfOffBits=54+colorTablesize;
fwrite(&fileHead, sizeof(BITMAPFILEHEADER),1, fw);
head.biBitCount=p->biBitCount;
head.biClrImportant=0;
head.biClrUsed=0;
head.biCompression=0;
head.biHeight=p->biHeight;
head.biPlanes=1;
head.biSize=40;
head.biSizeImage=lineByte*(p->biWidth);
head.biWidth=p->biWidth;
head.biXPelsPerMeter=0;
head.biYPelsPerMeter=0;
fwrite(&head, sizeof(BITMAPINFOHEADER),1, fw);
if(p->biBitCount==8)
fwrite(p->pColorTable, sizeof(RGBQUAD),256, fw);
fwrite(p->pBmpBuf, (p->biHeight)*lineByte, 1, fw);
fclose(fw);
return 1;
}
求教各位,先谢谢了。
bmp图片 像素值不符 c
[解决办法]
BMP文件 有一些是 倒着来的。
也就是从左下角的数据在最开始。