BMP图像加载实例(C语言)

bmp图像常被称为位图,这实际是对位图的误解,具体可见opengl superbible中对图像的说明。

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>

#ifdef _APPLE_
#include <glut/glut.h>
#else
#define FREEGLUT_STATIC
#include <GL/glut.h>
#endif

GLuint loadBMP_custom(const char * imagepath);

// Data read from the header of the BMP file
unsigned char header[54]; // Each BMP file begins by a 54-bytes header

unsigned int dataPos; // Position in the file where the actual data begins

unsigned int width, height;

unsigned int imageSize; // = width*height*3
// Actual RGB data
unsigned char * data;

int main(int argc, char **argv)
{
	FILE * file = fopen("1.bmp", "rb");
	if (!file)
	{
			printf("Image could not be openedn"); 
			return 0;
	}

	if ( fread(header, 1, 54, file)!=54 )
	{ // If not 54 bytes read :problem
		printf("Not a correct BMP filen");
		return 0;
	}

	if ( header[0]!='B' || header[1]!='M' )
	{
		printf("Not a correct BMP filen");
		return 0;
	}

	// Read ints from the byte array
	dataPos = *(int*)&(header[0x0A]);
	imageSize = *(int*)&(header[0x22]);
	width = *(int*)&(header[0x12]);
	height = *(int*)&(header[0x16]);

	printf("%d \t %d \t %d \t %d \n", dataPos, imageSize, width, height);
	printf("%d \t %d \t %d \t %d \n", &(header[0x0A]),&(header[0x22]),&(header[0x12]), &(header[0x16]));
	printf("%d \t %d \t %d \t %d \n", &(header[0x0A]),&(header[0x22]),(int *)&(header[0x12]), &(header[0x16]));
	printf("%d\t %d\t",*(int*)&(header[0x12]), *(char*)&(header[0x16]));

	getchar();
	return 0;
}





使用的图像:

测试结果如下:


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值