crgd12吧 关注:5贴子:104
  • 1回复贴,共1
#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文件创建纹理
bool 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);
// 这里是因为C中对结构体的内存对齐,所以要减去2,否则得不到正确的结果
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;
}
int main()
{
TEXTUREIMAGE image;
char * imgName = "a.bmp";
LoadBmp(imgName, &image);
printf("%d", image.rgbType);
return 0;
}


1楼2012-03-08 02:50回复
    FILE * pf = fopen("C:\\Users\\makefile\\Desktop\\a.bmp", "r");
    BITMAPFILEHEADER bmpFH;
    BITMAPINFOHEADER bmpIH;
    const unsigned long length = 1024 * 768 * 3;
    unsigned char res;
    unsigned long mm;
    fread(&bmpFH, sizeof(bmpFH), 1, pf);
    fread(&bmpIH, sizeof(bmpIH), 1, pf); rewind(pf);
    fseek(pf, 54L, 0);
    unsigned char * bmpData = (unsigned char *)malloc(sizeof(unsigned char) * length);
    fread(bmpData, length, 1, pf);
    fclose(pf);
    CDC * pDC = GetDC();
    for (int i = 0; i < 1024; ++i)
    {
    for (int j = 0; j < 768; ++j)
    {
    mm = 3 * j * 1024 + 3 * i;
    res = 0.299 * bmpData[mm+2] + 0.578 * bmpData[mm+1] + 0.114 * bmpData[mm];
    COLORREF colorrrefRGB = RGB(res, res, res);
    pDC->SetPixel(i, j, colorrrefRGB);
    }
    }
    free(bmpData);
    ReleaseDC(pDC);


    2楼2012-03-08 23:25
    回复