我想用我的代码加载并显示bmp 256色图像? [英] I want to load and display a bmp 256-colors-image with my code ?
本文介绍了我想用我的代码加载并显示bmp 256色图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我自己试着这样做。这里我的代码使用Dev-C ++:
I tried to do it myself. Here my code with Dev-C++ :
#include <iostream>
#include <fstream>
#include <glut.h>
using namespace std;
#pragma pack(push, 2 )
struct BitmapHeader // 14 byte
{
short sigNature;
unsigned long fileSize;
long reserved;
long dataOffset;
};
struct BitmapInfoheader // 40 byte
{
unsigned long infoSize;
unsigned long width;
unsigned long heigh;
unsigned short planes;
unsigned short bitCount;
unsigned long compression;
unsigned long imageSize;
long xPixelsPerMeter;
long yPixelsPerMeter;
unsigned long clrUsed;
unsigned long clrImportant;
};
#pragma pack(pop)
//--------------------------------------
struct BitmapHeader bmheader;
struct BitmapInfoheader bmInfo;
char* colorPlane;
char* imgData;
int loadBmImg()
{
char tt[20] ;
ifstream sf;
char word;
cout<<"Input filename of image : ";
cin.getline(tt,20);
sf.open(tt,ios::in|ios::binary);
if(sf.bad())
{
cout<<"Can't open the image!";
return 0;
}
sf.read((char*)&bmheader,sizeof(bmheader));
sf.read((char*)&bmInfo,sizeof(bmInfo));
colorPlane = new char[4*256]; //it's simple bitCount=8
imgData = new char[bmInfo.imageSize];
sf.read(colorPlane,4*256);
sf.read(imgData,bmInfo.imageSize);
sf.close();
}
//---------------------------------
void init()
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, bmInfo.width, 0, bmInfo.heigh, -1.0, 1.0);
}
void drawBmImage(void)
{
unsigned char r,g,b;
GLint R,G,B;
unsigned long int index;
glBegin (GL_POINTS);
for(int j=0; j<bmInfo.heigh; j++)
for(int i=0;i<bmInfo.width; i++)
{
index = (unsigned long)imgData[j*bmInfo.width+i];
r = colorPlane[4*index];
g = colorPlane[4*index+1];
b = colorPlane[4*index+2];
R = (unsigned short)r;
G = (unsigned short)g;
B = (unsigned short)b;
glColor3ub(R,G,B);
glVertex3i(i,j,0);
}
glEnd();
}
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
drawBmImage();
glFlush ();
}
int main(int argc, char** argv)
{
loadBmImg();
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (bmInfo.width, bmInfo.heigh);
glutInitWindowPosition (100, 100);
glutCreateWindow ("OpenGL");
init ();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
结果这里
看来我的颜色出错了。我该如何修复它们?
But the result here
It seems I get a mistake with the colors. How can I fix them ?
推荐答案
第一个已被建议:
The first was already suggested:
b = colorPlane[4*index];
g = colorPlane[4*index+1];
r = colorPlane[4*index+2];
这里有一些颜色和兼容性的改进:
Some more improvements for color and compatiblity are here:
sf.seekg(bmheader.dataOffset); // perform seek
imgData = new BYTE[bmInfo.imageSize];
sf.read((char*)imgData,bmInfo.imageSize);
并最终独立于宽度这很重要:
and finally to be independent from width this is important:
unsigned long stride = ((((bmInfo.width * bmInfo.bitCount) + 31) & ~31) >> 3);
for(int j=0; j<bmInfo.heigh; j++)
for(int i=0;i<bmInfo.width; i++)
{
index = (unsigned long)imgData[stride*j + i];
...
这篇关于我想用我的代码加载并显示bmp 256色图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文