逛奔的蜗牛

我不聪明,但我会很努力

   ::  :: 新随笔 ::  ::  :: 管理 ::
这个代码写得有些阿格里拉,但也不失为一种方法
 
#ifndef _BMPLOAD_H_
#define _BMPLOAD_H_
#include <iostream>
#include <stdio.h>
using namespace std;
 
/*------------- 
 类型定义
 --------------*/
// 纹理图像结构
typedef struct {
         int imgWidth; // 纹理宽度
         int imgHeight; // 纹理高度
         unsigned int rgbType; // 每个象素对应的字节数,3:24位图,4:带alpha通道的24位图
         unsigned char *data; // 纹理数据
} TEXTUREIMAGE;
 
// BMP文件头
typedef struct {
         unsigned short bfType; // 文件类型
         unsigned long bfSize; // 文件大小
         unsigned short bfReserved1; // 保留位
         unsigned short bfReserved2; // 保留位
         unsigned long bfOffBits; // 数据偏移位置
} BMPFILEHEADER;
 
// BMP信息头
typedef struct {
         unsigned long biSize; // 此结构大小
         long biWidth; // 图像宽度
         long biHeight; // 图像高度
         unsigned short biPlanes; // 调色板数量
         unsigned short biBitCount; // 每个象素对应的位数,24:24位图,32:带alpha通道的24位图
         unsigned long biCompression; // 压缩
         unsigned long biSizeImage; // 图像大小
         long biXPelsPerMeter; // 横向分辨率
         long biYPelsPerMeter; // 纵向分辨率
         unsigned long biClrUsed; // 颜色使用数
         unsigned long biClrImportant; // 重要颜色数
} BMPINFOHEADER;
 
// 读取BMP文件创建纹理
GLboolean LoadBmp(char *filename, TEXTUREIMAGE *textureImg) {
         int i, j;
         FILE *file;
         BMPFILEHEADER bmpFile;
         BMPINFOHEADER bmpInfo;
         int pixel_size;
 
         // 初始化纹理数据
         textureImg->imgWidth = 0;
         textureImg->imgHeight = 0;
         textureImg->rgbType = 0;
         if (textureImg->data != NULL) {
                  delete []textureImg->data;
         }
 
         // 打开文件
         file = fopen(filename, "rb");
         if (file == NULL) {
                  cout << "Open File Error" <<endl;
                  return false;
         }
 
         // 获取文件头
         rewind(file);
         fread(&bmpFile, sizeof(BMPFILEHEADER)-2, 1, file);
         //因为C语言对结构按四位对齐,所以不能直接用sizeof(BMPFILEHEADER)
         fread(&bmpInfo, sizeof(BMPINFOHEADER), 1, file);
         // 验证文件类型
         if (bmpFile.bfType != 0x4D42) {
                 cout << "File Type Error" <<endl;
                 fclose(file);
                 return false;
         }
 
         // 获取图像色彩数
         pixel_size = bmpInfo.biBitCount >> 3;
         // 读取文件数据
         textureImg->data = new unsigned char[bmpInfo.biWidth * bmpInfo.biHeight * pixel_size];
         if (textureImg->data == NULL) {
                  fclose(file);
                  return false;
         }

         rewind(file);
         fseek(file, 54L, 0);
         for (i = 0; i < bmpInfo.biHeight; i++) {
                  for (j = 0; j < bmpInfo.biWidth; j++) {
                           // 红色分量
                           fread(textureImg->data + (i * bmpInfo.biWidth + j) * pixel_size + 2,
                                     sizeof(unsigned char), 1, file);
                           // 绿色分量
                           fread(textureImg->data + (i * bmpInfo.biWidth + j) * pixel_size + 1,
                                     sizeof(unsigned char), 1, file);
                           // 蓝色分量
                           fread(textureImg->data + (i * bmpInfo.biWidth + j) * pixel_size + 0,
                                   sizeof(unsigned char), 1, file);
                           // Alpha分量
                           if (pixel_size == 4) {
                                    fread(textureImg->data + (i * bmpInfo.biWidth + j) * pixel_size + 3,
                                    sizeof(unsigned char), 1, file);
                           }
                  }
         }
         // 记录图像相关参数
         textureImg->imgWidth = bmpInfo.biWidth;
         textureImg->imgHeight = bmpInfo.biHeight;
         textureImg->rgbType = pixel_size;
         fclose(file);
         return false;
}
 
#endif
posted on 2010-12-17 17:33 逛奔的蜗牛 阅读(367) 评论(0)  编辑 收藏 引用 所属分类: OpenGL

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理