Skip to main content
gamedev.net
🔒 Locked

Binary Image

Started by Illasera 1 reply 950+ views
Original Post
Illasera
Illasera
Hey, While searching, I noticed really few topics about this category and most of them left unresolved. How do you display binary image(Black & White , Monochrome and many other names, same meaning) with OpenGL? I am trying the following method : I posted only the relevant code.


void BMP_Texture::Convert_Monochrome()
{
	int Num_Of_Bytes = ((Image_Width * Image_Height) / 8);

	int Cell = 0;
	int Bit  = 0;
	int Current_Pixel = 0;

	unsigned char* New_Pixels = new unsigned char[Num_Of_Bytes];
	memset(New_Pixels, 0, Num_Of_Bytes);
	fread(Image_Data, sizeof(unsigned char), (Image_Height * Image_Width * Image_BPP), Image_File);

	for(int j = 0; j < Image_Height * Image_Width * Image_BPP; j+= 3)
	{
		Cell = Current_Pixel / 8;
		Bit  = Current_Pixel % 8;
		if(Image_Data[j] + Image_Data[j + 1] + Image_Data[j + 2])
			New_Pixels[Cell] |= (1 << Bit);

		Current_Pixel++;

	}

	delete[] Image_Data;
	Image_Data = New_Pixels;
	
	Image_BPP	 =  1;
	Image_Format =  GL_COLOR_INDEX;
	Image_Store  =  GL_BITMAP;

	fflush(Image_File);
	fclose(Image_File);

	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
	glPixelStorei(GL_UNPACK_LSB_FIRST, 1);

	Generate_Image();
	Destroy_Image();

	return;

};


void Base_Texture::Generate_Image()
{
	bool			GenerateMipMap;
	unsigned int	ImageMinFilter;
	unsigned int	ImageMagFilter;

	switch(Image_Quality)
	{
		case FAST_QUALITY:
			{
				GenerateMipMap = false;
				ImageMinFilter = GL_NEAREST;
				ImageMagFilter = GL_NEAREST;
			}break;
		case FINE_QUALITY:
			{
				GenerateMipMap = true;
				ImageMinFilter = GL_NEAREST_MIPMAP_NEAREST;
				ImageMagFilter = GL_LINEAR;
			}break;
		case GREAT_QUALITY:
			{
				GenerateMipMap = true;
				ImageMinFilter = GL_LINEAR_MIPMAP_LINEAR;
				ImageMagFilter = GL_LINEAR;
			}break;
		default:
			{
				GenerateMipMap = false;
				ImageMinFilter = GL_NEAREST;
				ImageMagFilter = GL_NEAREST;
			}break;
	}

	glEnable(GL_TEXTURE_2D);

	glGenTextures(1, &Image_ID);
	glBindTexture(GL_TEXTURE_2D, Image_ID);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, ImageMinFilter);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, ImageMagFilter);
	glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GenerateMipMap);
	glTexImage2D(GL_TEXTURE_2D, 0, Image_BPP, Image_Width, Image_Height, 0, Image_Format, Image_Store, Image_Data);

	glDisable(GL_TEXTURE_2D);
};



I tried using the specific attitude that OpenGL for Binary Images (although really low on details) from the documentation of glTexImage2D with GL_BITMAP. Yet no results (The image isn't been displayed) using supporting tool of "GlIntercept" reported nothing wrong = something is wrong yet hidden. Thanks in advance.
Brother Bob
Brother Bob
I suggest you make the image a normal luminance image instead of a bitmap. Use GL_UNSIGNED_CHAR type and GL_LUMINANCE format. Less problem to actually get it to work, have the ability to make it gray scale instead of binary if you desire, and the internal format is most likely to end up with a luminance-type anyway in practice.
Illasera
Illasera
Quote:
Original post by Brother Bob
I suggest you make the image a normal luminance image instead of a bitmap. Use GL_UNSIGNED_CHAR type and GL_LUMINANCE format. Less problem to actually get it to work, have the ability to make it gray scale instead of binary if you desire, and the internal format is most likely to end up with a luminance-type anyway in practice.


Already done but thanks :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.