Quantcast
Viewing all articles
Browse latest Browse all 12583

I love to know about how window application display a 256-color image?

Hi everybody , I'm just a student , I am studying about image processing. I wrote a small program to display a 8/24-bit of color-depth bitmap file. It worked very well with 24-bit but not good with 8-bit bitmap. I'm using window 7 and dev-c++ to write the program. Could you tell me what window applications do with the color-pallete of an 8-bit bitmap image and could I fix my program to display as well as MSpaint? Thanks!


#include <GL/gl.h>
#include <GL/glut.h>
#include <iostream>
#include <fstream>

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 = NULL;
	char* imgData  = NULL;

int loadBmImg()
{
	char tt[20] ;
	ifstream sf;
	char word;
	cout<<"Input a filename : ";
	cin.getline(tt,20);
	sf.open(tt,ios::in|ios::binary);

	if(sf.bad())
	{
		 cout<<"Can't open the file!";
		 return 0;
	}
		sf.read((char*)&bmheader,sizeof(bmheader));
		sf.read((char*)&bmInfo,sizeof(bmInfo));
		imgData = new char[bmInfo.imageSize];
		if(bmInfo.bitCount<=8)
		{
			colorPlane = new char[4*256];
			sf.read(colorPlane,4*256);
		}
		sf.read(imgData,bmInfo.imageSize);
	sf.close();
}

//---------------------------------

void init(void) 
{
    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;
	short padding = 0;
	
	glBegin (GL_POINTS);
	if(bmInfo.bitCount==8)
	{
		padding = (bmInfo.imageSize-(bmInfo.heigh*bmInfo.width))/bmInfo.heigh; // padding in byte
    	for(GLint j=0; j<bmInfo.heigh; j++)
   			for(GLint i=0;i<bmInfo.width; i++)
   			{
   				index = (unsigned long)imgData[j*(bmInfo.width+padding)+i];
   				r = colorPlane[4*index+2];
   				g = colorPlane[4*index+1];
   				b = colorPlane[4*index];
				R = ((unsigned short)r);
   				G = ((unsigned short)g);
   				B = ((unsigned short)b);
   				glColor3ub(R,G,B);
   				glVertex3i(i,j,0);
   			}
   }
   	else if(bmInfo.bitCount==24)
   	{
   		padding = (bmInfo.imageSize-(3*bmInfo.heigh*bmInfo.width))/bmInfo.heigh; // padding in byte
   		for(GLint j=0;j<bmInfo.heigh;j++)
   			for(GLint i=0;i<bmInfo.width;i++)
   			{
   				index = 3*(j*bmInfo.width+i)+j*padding;
   				r = imgData[index+2];
   				g = imgData[index+1];
   				b = imgData[index+0];
   				R = (unsigned short)r;
   				G = (unsigned short)g;
   				B = (unsigned short)b;
   				glColor3ub(R,G,B);
   				glVertex3i(i,j,0);
   			}
   }
   	glEnd();
	cout<<"Padding = "<<padding<<endl;	
}

void display(void)
{
   glClear (GL_COLOR_BUFFER_BIT);
   drawBmImage();
   glFlush ();
}


int main(int argc, char** argv)
{
   loadBmImg();
   			cout.setf(ios::showbase);
		//----------------------------------------------------------
		cout<<"BitMapFileHeader : "<<endl;
		cout<<"SignNature : "<<hex<<bmheader.sigNature<<endl;
		cout<<"Size of File : "<<hex<<bmheader.fileSize<<" = "<<dec<<bmheader.fileSize<<" bytes "<<endl;
		cout<<"Data Offset : "<<hex<<bmheader.dataOffset<<endl;
		//----------------------------------------------------------
		cout<<"BitMapInfoHeader : "<<endl;
		cout<<"Size of Info header : "<<hex<<bmInfo.infoSize<<endl;
		cout<<"Width of the Image : "<<hex<<bmInfo.width<<" = "<<dec<<bmInfo.width<<endl;
		cout<<"Heigh of the Image : "<<hex<<bmInfo.heigh<<" = "<<dec<<bmInfo.heigh<<endl;
		cout<<"Number of Plane color : "<<hex<<bmInfo.planes<<endl;
		cout<<"Number of bits per pixel : "<<hex<<bmInfo.bitCount<<endl;
		cout<<"Compression : "<<hex<<bmInfo.compression<<endl;
		cout<<"Size of the Data : "<<hex<<bmInfo.imageSize<<endl;
		cout<<"Number of pixels per meter of width : "<<hex<<bmInfo.xPixelsPerMeter<<" = "<<dec<<bmInfo.xPixelsPerMeter<<endl;
		cout<<"Number of pixels per meter of heigh : "<<hex<<bmInfo.yPixelsPerMeter<<" = "<<dec<<bmInfo.yPixelsPerMeter<<endl;
		cout<<"Number of colors in use : "<<hex<<bmInfo.clrUsed<<endl;
		cout<<"Number of important colors : "<<hex<<bmInfo.clrImportant<<endl;

   glutInit(&argc, argv);
   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); 
   glutInitWindowSize (bmInfo.width, bmInfo.heigh); 
   glutInitWindowPosition (100, 100);
   glutCreateWindow ("OpenGL");
   init ();
   glutDisplayFunc(display); 
   glutMainLoop();
   return 0;
}



Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>