VS2013中OpenGL读取BMP文件到内存试过可以用--用最原始到办法可以做到

#include<GLTools.h>
#include <GL/glut.h>
#include"windows.h"
#include <stdio.h>

#include <stdlib.h>


#define FileName "SeaAndTree.bmp"


static GLint    ImageWidth;  //BMP图像宽

static GLint    ImageHeight; //BMP图像高

static GLint    PixelLength;  //图像数据长度

static GLubyte* PixelData;

void ReadBmpFile()

{
    // 打开文件

    FILE* pFile;
        
   pFile=_fsopen("SeaAndTree.bmp", "rb", 64);

   if (pFile == 0)

       MessageBox(NULL, L"file", L"fileshibai", MB_OK);

    // 读取图象的宽高信息

    fseek(pFile, 0x0012, SEEK_SET);

    fread(&ImageWidth, sizeof(ImageWidth), 1, pFile);

    fread(&ImageHeight, sizeof(ImageHeight), 1, pFile);

    if (ImageWidth == 0)

        MessageBox(NULL, L"ImageWidth", L"ImageWidth", MB_OK);

    // 计算像素数据长度,不足4位补齐4位
    PixelLength = ImageWidth * 3;

    while (PixelLength % 4 != 0)

        ++PixelLength;

    PixelLength *= ImageHeight;


    if (PixelLength == 0)

        MessageBox(NULL, L"PixelLength", L"PixelLength", MB_OK);

    // 读取像素数据

    PixelData = (GLubyte*)malloc(100);

    //if (PixelData == 0)

        //MessageBox(NULL, L"file", L"PixelData", MB_OK);


    fseek(pFile, 54, SEEK_SET);  //跳过文件头和信息头

    fread(PixelData, PixelLength, 1, pFile);


    // 关闭文件

    fclose(pFile);

}

void display(void)

{

    glClear(GL_COLOR_BUFFER_BIT);
    glPixelZoom(0.5f, 0.5f);

    glRasterPos2f(-0.5, -0.5);

    // 绘制像素

    glDrawPixels(ImageWidth, ImageHeight,
        GL_BGR_EXT, GL_UNSIGNED_BYTE, PixelData);


    glutSwapBuffers();

}

int main(int argc, char* argv[])

{

    ReadBmpFile();   //读取BMP图像数据到内存,并获取图像的宽高信息
// 初始化GLUT并运行

    gltSetWorkingDirectory(argv[0]);

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);

    glutInitWindowPosition(100, 100);

    glutInitWindowSize(ImageWidth, ImageHeight);
    
    glutCreateWindow(FileName);

    glutDisplayFunc(&display);

    glutMainLoop();
    // 释放内存   

    free(PixelData);

    return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值