• Advertisement
Sign in to follow this  

1 bit bitmap loading

This topic is 4342 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i want to load 1 bit bitmap to an unsigned int array[]...But something is wrong..the image i get in my array is not same as the filei.Can you see whats wrong with it?.I also calculated padding vb...just try this function with a 1 bit bitmap you will see what i mean
bool NGLoad1BitBitmap(char* BitmapName)
{

	//BİR Bitmap Dosyası FILEHEADER/INFOHEADER/PALETTE /BITLER den oluşur
	BITMAPINFOHEADER InfoHeader;
	BITMAPFILEHEADER FileHeader;
	unsigned int* BitmapData;
	ifstream MyBitmapFile(BitmapName,ios::binary);
	if(!MyBitmapFile)    //Couldn't Open File
	{
		cout<<"\nCouldn't Open File!!";
		return false;
	}
	else
	{
		MyBitmapFile.read(reinterpret_cast<char*>(&FileHeader),sizeof(BITMAPFILEHEADER));
		if(FileHeader.bfType!=0x4D42)
		{
			cout<<"File is not a Bitmap File!";
		}
		MyBitmapFile.read(reinterpret_cast<char*>(&InfoHeader),sizeof(BITMAPINFOHEADER));

		/* Deneme ba&#351;lang&#305;ç  */
	
		unsigned int PalletteValues[2];
		MyBitmapFile.read(reinterpret_cast<char*>(&PalletteValues),sizeof(unsigned int)*2);
	
		int TellG=MyBitmapFile.tellg();

		MyBitmapFile.seekg(FileHeader.bfOffBits,ios::beg);

		int TellG2=MyBitmapFile.tellg();

		BitmapData=new unsigned int[InfoHeader.biWidth*InfoHeader.biHeight];
		memset(reinterpret_cast<void*>(BitmapData),0,InfoHeader.biWidth*InfoHeader.biHeight*sizeof(unsigned int));
		int iNumPaddedBits = (32-(InfoHeader.biWidth * 1)) % (4*8);
		int iNumPaddedBytes = iNumPaddedBits/8;

		for(unsigned int h=0;h<InfoHeader.biHeight;++h)
		{
			for(unsigned int w=0;w<InfoHeader.biWidth;w+=8)
			{
				unsigned char Byte;
				MyBitmapFile.read(reinterpret_cast<char*>(&Byte),sizeof(unsigned char));
				int i=InfoHeader.biWidth-w;
				if(i>8)
				{
					i=8;
				}
				for(int cntr2=0;cntr2<i;++cntr2)
				{
					int hInverted=(InfoHeader.biHeight-1)-h;
					BitmapData[ (w+cntr2)+hInverted*InfoHeader.biWidth]=PalletteValues[(Byte>>cntr2)&0x01];
				}
			}
			if( iNumPaddedBytes != 0 )
			{
				unsigned char skip[4];
				MyBitmapFile.read(reinterpret_cast<char*>(skip),iNumPaddedBytes);
			}// End of if reading padded bytes
		}

	}
	// FOR SHOWING WHAT WE LOAD TO OUR ARRAY.. unfortunately what we load is wrong..
	for(int cntr1=0;cntr1<(InfoHeader.biWidth*InfoHeader.biHeight);++cntr1)
	{
		cout<<cntr1<<". element : "<<*(BitmapData+cntr1)<<'\n';
	}
	for(int s1=0;s1<InfoHeader.biHeight;++s1)
	{
		for(int s2=0;s2<InfoHeader.biWidth;++s2)
		{
			if(*(BitmapData+(s1*InfoHeader.biWidth)+s2)==0)
			{
				cout<<0;
			}
			else 
			{
				cout<<1;
			}
		}
		cout<<endl;
	}
	return true;
}

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement