c语言读取bmp文件大小,c语言读取bmp文件格式(转载)

//BMP 头

typedef struct

{

char id[2];                                   //bmp 文件标志 "BM"

unsigned long fileSize;                    //文件大小

unsigned long reserved0;

unsigned long bitmapDataOffset;

unsigned long bitmapHeaderSize;

unsigned long width;                    //图像宽度

unsigned long height;                   //图像高度

unsigned short planes;

unsigned short bitsPerPixel;            //每个像素站多少位

unsigned long compression;            //是否压缩

unsigned long bitmapDataSize;

unsigned long hRes;

unsigned long vRes;

unsigned long colors;

unsigned long importantColors;

unsigned char palette[256][4];      //调色板数据,24位及以上像素没有该数据

} BMPHeader_t;

long GetFileSize(FILE* f)

{

long pos = ftell(f);

fseek(f,0,SEEK_END);

long len = ftell(f);

fseek(f,pos,SEEK_SET);

return len;

}

void __stdcall Com_Error(const char *fmt, ... )

{

char    com_errorMessage[4096];

va_list        argptr;

va_start (argptr,fmt);

vsprintf (com_errorMessage,fmt,argptr);

va_end (argptr);

MessageBox(NULL,com_errorMessage,"错误提示",MB_ICONERROR);

}

//*pic 返回 RGBA 类型像素数据,width 返回修正后的宽度,height返回高度

// name 输入的文件名称

bool LoadBMP( const char *name, byte **pic, int *width, int *height )

{

int        columns, rows, numPixels;

byte    *pixbuf;

int        row, column;

byte    *buf_p;

byte    *buffer;

int        length;

BMPHeader_t bmpHeader;

byte        *bmpRGBA;

*pic = NULL;

// load the file

FILE* pfile = fopen(name,"rb");

if(pfile == NULL)

{

Com_Error( "LoadBMP: Load BMP files failed (%s)/n", name );

return false;

}

length = GetFileSize(pfile);

buffer = (byte*) malloc(length*sizeof(byte));

fread(buffer,1,length,pfile);

if (!buffer)

{

Com_Error( "LoadBMP: Memory alloc failed (%s)/n", name );

return false;

}

buf_p = buffer;

bmpHeader.id[0] = *buf_p++;

bmpHeader.id[1] = *buf_p++;

bmpHeader.fileSize = ( * ( long * ) buf_p );

buf_p += 4;

bmpHeader.reserved0 = ( * ( long * ) buf_p );

buf_p += 4;

bmpHeader.bitmapDataOffset = ( * ( long * ) buf_p );

buf_p += 4;

bmpHeader.bitmapHeaderSize = ( * ( long * ) buf_p );

buf_p += 4;

bmpHeader.width = ( * ( long * ) buf_p );

buf_p += 4;

bmpHeader.height = ( * ( long * ) buf_p );

buf_p += 4;

bmpHeader.planes = ( * ( short * ) buf_p );

buf_p += 2;

bmpHeader.bitsPerPixel = ( * ( short * ) buf_p );

buf_p += 2;

bmpHeader.compression = ( * ( long * ) buf_p );

buf_p += 4;

bmpHeader.bitmapDataSize = ( * ( long * ) buf_p );

buf_p += 4;

bmpHeader.hRes = ( * ( long * ) buf_p );

buf_p += 4;

bmpHeader.vRes = ( * ( long * ) buf_p );

buf_p += 4;

bmpHeader.colors = ( * ( long * ) buf_p );

buf_p += 4;

bmpHeader.importantColors = ( * ( long * ) buf_p );

buf_p += 4;

memcpy( bmpHeader.palette, buf_p, sizeof( bmpHeader.palette ) );

if ( bmpHeader.bitsPerPixel == 8 )

buf_p += 1024;

if ( bmpHeader.id[0] != 'B' && bmpHeader.id[1] != 'M' )

{

Com_Error( "LoadBMP: only Windows-style BMP files supported (%s)/n", name );

return false;

}

if ( bmpHeader.fileSize != length )

{

Com_Error("LoadBMP: header size does not match file size (%d vs. %d) (%s)/n", bmpHeader.fileSize, length, name );

return false;

}

if ( bmpHeader.compression != 0 )

{

Com_Error("LoadBMP: only uncompressed BMP files supported (%s)/n", name );

return false;

}

if ( bmpHeader.bitsPerPixel < 8 )

{

Com_Error("LoadBMP: monochrome and 4-bit BMP files not supported (%s)/n", name );

return false;

}

columns = bmpHeader.width;

rows = bmpHeader.height;

if(bmpHeader.bitsPerPixel == 24 )

{

if( (columns & 3) != 0)                    //检查宽度是否为4倍数           columns = (columns & ~3) + 4;    //修正位图宽度值,对齐到4的倍数,不然图像会变形

}

if ( rows < 0 )

rows = -rows;

numPixels = columns * rows;

if ( width )

*width = columns;

if ( height )

*height = rows;

bmpRGBA = (byte*)malloc( numPixels * 4 );

*pic = bmpRGBA;

buf_p = buffer+54;

for ( row = rows-1; row >= 0; row--)

{

pixbuf = bmpRGBA + row*columns*4;

for ( column = 0; column < columns; column++ )

{

unsigned char red, green, blue, alpha;

int palIndex;

unsigned short shortPixel;

switch ( bmpHeader.bitsPerPixel )

{

case 8:

palIndex = *buf_p++;

*pixbuf++ = bmpHeader.palette[palIndex][2];

*pixbuf++ = bmpHeader.palette[palIndex][1];

*pixbuf++ = bmpHeader.palette[palIndex][0];

*pixbuf++ = 0xff;

break;

case 16:

shortPixel = * ( unsigned short * ) pixbuf;

pixbuf += 2;

*pixbuf++ = ( shortPixel & ( 31 << 10 ) ) >> 7;

*pixbuf++ = ( shortPixel & ( 31 << 5 ) ) >> 2;

*pixbuf++ = ( shortPixel & ( 31 ) ) << 3;

*pixbuf++ = 0xff;

break;

case 24:

blue = *buf_p++;

green = *buf_p++;

red = *buf_p++;

*pixbuf++ = red;

*pixbuf++ = green;

*pixbuf++ = blue;

*pixbuf++ = 255;

break;

case 32:

blue = *buf_p++;

green = *buf_p++;

red = *buf_p++;

alpha = *buf_p++;

*pixbuf++ = red;

*pixbuf++ = green;

*pixbuf++ = blue;

*pixbuf++ = alpha;

break;

default:

Com_Error("LoadBMP: illegal pixel_size '%d' in file '%s'/n", bmpHeader.bitsPerPixel, name );

return false;

}

}

}

free(buffer);

fclose(pfile);

return true;

}

//注意bmp文件是由左到右, 由下到上的循序存储的像素.每个像素颜色顺序为B G R (A) //BMP的行象素是4字节对齐的,不足的补0,比如有个图像每行宽度是63象素,//BMP文件存储时会每行存储64个字节,最后一个字节用0补齐void CDlg::OnPaint()   //测试图像{CPaintDC dc(this);static byte* pic = NULL;static int w,h;if(pic == NULL){    char spath[MAX_PATH];    _getcwd(spath, 200);    strcat(spath,"//res//b0000.bmp");    LoadBMP(spath,&pic,&w,&h);}int i,j;for(j=0;j

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值