Archived

This topic is now archived and is closed to further replies.

Alpha value in rgb

This topic is 5149 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

what is wrong with this? what i am trying to do is get rid of the black parts of images by converting the rgb value of an images to rgba but it wont work, my code is below
	LoadBMP(char* filename, GLfloat minFilter, GLfloat maxFilter)
	{
	FILE* file;									//The file pointer

	BITMAPFILEHEADER	file_header;			//The bitmap file header

	BITMAPINFOHEADER	info_header;
	int					imageIdx = 0;			//Image index counter

	unsigned char		tempRGB;				//Swap variable


	//Open filename in read only binary mode

	file= fopen(filename, "rb");
	if(file==NULL)
		{
		return false;
		}

	//Read the bitmap file header

	fread(&file_header, sizeof(BITMAPFILEHEADER), 1, file);
	
	//Confirm that this is a bitmap by checking for the universal bitmap id

	if(file_header.bfType != BITMAP_ID)
		{
		fclose(file);
		return false;
		}

	//Read the bitmap information header in

	fread(&info_header, sizeof(BITMAPINFOHEADER), 1, file);

	//Advance the file pointer to the beginning of the bitmap data

	fseek(file, file_header.bfOffBits, SEEK_SET);

	//Allocate the bitmap image data

	data= new unsigned char [info_header.biSizeImage];

	//Confirm memory allocation

	if(!data)
		{
		free(data);
		fclose(file);
		return false;
		}

	//Read in the bitmap image data

	fread(data, 1, info_header.biSizeImage, file);

	//Make sure bitmap image data was read

	if(data==NULL)
		{
		fclose(file);
		return false;
		}
	
	width = info_header.biWidth;
	height= info_header.biWidth;
	bpp	  = info_header.biBitCount;

	//Swap the R and B values to get RGB since the bitmap color format is in BGR

	for(imageIdx = 0; imageIdx<(int)info_header.biSizeImage; imageIdx+=3)
		{
		tempRGB			= data[imageIdx];
		data[imageIdx]  = data[imageIdx + 2];
		data[imageIdx+2]= tempRGB;
		}
		
		int w = width;int h = height;
  
  // this the old image with rgb data

  // Alloc new buffer for rgba imageuchar 

  unsigned char *rgba_data = new unsigned char[w*h*4]; 
  // this will be the new image with alpha channel

  for (int x=0; x<w; x++){  for(int y=0; y<h; y++)
    {    // Copy the rgb values from rgb_data to rgba_data    

    int p =y*w+h; 
    
    rgba_data[p*4+0] = data[p*3+0];
        rgba_data[p*4+1] = data[p*3+1];
            rgba_data[p*4+2] = data[p*3+2];    // Now set alpha channel full visible by default    

            unsigned char alpha = 255;    // If color is full black then set alpha to invisible (zero)       

            if (data[p*3+0]==0 && data[p*3+1]==0 && data[p*3+2]==0) alpha = 0;    rgba_data[p*4+3] = alpha;  }}

	//Close the file and return the bitmap image data

	fclose(file);

	//Build A Texture From The Data

	glGenTextures(1, &ID);						//Generate OpenGL texture IDs


	glBindTexture(GL_TEXTURE_2D, ID);			//Bind the texture to a texture object 

	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);	//Filtering for if texture is bigger than should be

	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, maxFilter);	//Filtering for if texture is smaller than it should be

	
	if(bpp==32)
		bpp=GL_RGBA;

	if(bpp==24)									//Was the TGA 24 bpp?

		bpp=GL_RGB;							

	glTexImage2D(GL_TEXTURE_2D, 0, bpp, width, height, 0, bpp, GL_UNSIGNED_BYTE, data);

	return true;
	}

Share this post


Link to post
Share on other sites
quote:
Original post by Brother Bob
quote:
Original post by stefu
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, 4, GL_UNSIGNED_BYTE, rgba_data);

4 is not a valid format. I assume you meant GL_RGBA.


I think he meant

glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, rgba_data);



[edited by - Dave Hunt on November 11, 2003 2:24:20 AM]

Share this post


Link to post
Share on other sites
ok problem, when i uses stefu one i can see the quads but no images (when i know the images work when i just used a the normal loadbmp function) but when i try dave hunt''s one i get nothing, just a black screen, not even any quads, ahs anyonew got any suggesitons?

Share this post


Link to post
Share on other sites
ok if i use the source below (something i made up on my own using code guven to my us guys i get the images back, no change witht he black pixels, and will a blue tinge over the images (the tinge gets darker as the slpha value is increased)
does anyone know what i am doing wrong, please i really need help with this

#include "Texture.h"

#define BITMAP_ID 0x4D42

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

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

//- DEFINITIONS ----------------------------------------------------//

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

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


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

//- bool LoadTGA(char*, GLfloat, GLfloat) --------------------------//

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

//- Description: This function loads a truevision TARGA into the -//

//- TEXTURE class object that it represents. -//

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

//- Big thanks to NeHe for this one -//

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

bool TEXTURE::
LoadTGA(char* filename, GLfloat minFilter, GLfloat maxFilter)
{
GLubyte TGAheader[12]={0,0,2,0,0,0,0,0,0,0,0,0}; //Uncompressed TGA header

GLubyte TGAcompare[12]; //Used to compare TGA header

GLubyte header[6]; //The first six useful bytes from the header

GLuint bytesPerPixel; //Holds the bpp of the TGA

GLuint imageSize; //Used to store image size while in RAM

GLuint temp; //Temp variable

GLuint type=GL_RGBA; //Set the default OpenGL mode to RBGA (32 BPP)


FILE* file = fopen(filename, "rb"); // Open The TGA File


if(file==NULL || // Does File Even Exist?

fread(TGAcompare,1,sizeof(TGAcompare),file)!=sizeof(TGAcompare) || // Are There 12 Bytes To Read?

memcmp(TGAheader,TGAcompare,sizeof(TGAheader))!=0 || // Does The Header Match What We Want?

fread(header,1,sizeof(header),file)!=sizeof(header)) // If So Read Next 6 Header Bytes

{
if(file==NULL) // Did The File Even Exist? *Added Jim Strong*

{
return false;
}
else
{
fclose(file); // If anything failed, close the file

return false;
}
}

width = header[1] * 256 + header[0]; // Determine The TGA Width (highbyte*256+lowbyte)

height= header[3] * 256 + header[2]; // Determine The TGA Height (highbyte*256+lowbyte)


if(width <=0 || // Is The Width Less Than Or Equal To Zero

height<=0 || // Is The Height Less Than Or Equal To Zero

(header[4]!=24 && header[4]!=32)) // Is The TGA 24 or 32 Bit?

{
fclose(file); // If Anything Failed, Close The File

return false;
}

bpp = header[4]; // Grab The TGA''s Bits Per Pixel (24 or 32)

bytesPerPixel= bpp/8; // Divide By 8 To Get The Bytes Per Pixel

imageSize = width*height*bytesPerPixel; // Calculate The Memory Required For The TGA Data


data= new GLubyte [imageSize]; // Reserve Memory To Hold The TGA Data


if(data==NULL || // Does The Storage Memory Exist?

fread(data, 1, imageSize, file)!=imageSize) // Does The Image Size Match The Memory Reserved?

{
if(data!=NULL) // Was Image Data Loaded

free(data); // If So, Release The Image Data


fclose(file); // Close The File

return false; // Return False

}

for(GLuint i=0; i<int(imageSize); i+=bytesPerPixel) // Loop Through The Image Data

{ // Swaps The 1st And 3rd Bytes (''R''ed and ''B''lue)

temp =data[i]; // Temporarily Store The Value At Image Data ''i''

data[i] = data[i + 2]; // Set The 1st Byte To The Value Of The 3rd Byte

data[i+2]= temp; // Set The 3rd Byte To The Value In ''temp'' (1st Byte Value)

}

fclose (file); //Close the file


// Build A Texture From The Data

glGenTextures(1, &ID); //Generate OpenGL texture IDs


glBindTexture(GL_TEXTURE_2D, ID); //Bind the texture to a texture object

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter); //Filtering for if texture is bigger than should be

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, maxFilter); //Filtering for if texture is smaller than it should be


if(bpp==24) //Was the TGA 24 bpp?

type=GL_RGB;

glTexImage2D(GL_TEXTURE_2D, 0, type, width, height, 0, type, GL_UNSIGNED_BYTE, data);

return true;
}

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

//- bool LoadBMP(char*, GLfloat, GLfloat) --------------------------//

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

//- Description: This function loads a Windows Bitmap into the -//

//- TEXTURE class object that it represents. -//

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

//- Big thanks to NeHe for this one -//

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

bool TEXTURE::
LoadBMP(char* filename, GLfloat minFilter, GLfloat maxFilter)
{
FILE* file; //The file pointer

BITMAPFILEHEADER file_header; //The bitmap file header

BITMAPINFOHEADER info_header;
int imageIdx = 0; //Image index counter

unsigned char tempRGB; //Swap variable


//Open filename in read only binary mode

file= fopen(filename, "rb");
if(file==NULL)
{
return false;
}

//Read the bitmap file header

fread(&file_header, sizeof(BITMAPFILEHEADER), 1, file);

//Confirm that this is a bitmap by checking for the universal bitmap id

if(file_header.bfType != BITMAP_ID)
{
fclose(file);
return false;
}

//Read the bitmap information header in

fread(&info_header, sizeof(BITMAPINFOHEADER), 1, file);

//Advance the file pointer to the beginning of the bitmap data

fseek(file, file_header.bfOffBits, SEEK_SET);

//Allocate the bitmap image data

data= new unsigned char [info_header.biSizeImage];

//Confirm memory allocation

if(!data)
{
free(data);
fclose(file);
return false;
}

//Read in the bitmap image data

fread(data, 1, info_header.biSizeImage, file);

//Make sure bitmap image data was read

if(data==NULL)
{
fclose(file);
return false;
}

width = info_header.biWidth;
height= info_header.biWidth;
bpp = info_header.biBitCount;
unsigned char alpha=100;
//Swap the R and B values to get RGB since the bitmap color format is in BGR

for(imageIdx = 0; imageIdx<(int)info_header.biSizeImage; imageIdx+=3)
{
tempRGB = data[imageIdx];
data[imageIdx] = data[imageIdx + 2];
data[imageIdx+2]= tempRGB;

if (data[imageIdx]==0 && data[imageIdx+1]==0 && data[imageIdx+2]==0)
alpha = 0;
data[imageIdx+3] = alpha;
}


//Close the file and return the bitmap image data

fclose(file);

//Build A Texture From The Data

glGenTextures(1, &ID); //Generate OpenGL texture IDs


glBindTexture(GL_TEXTURE_2D, ID); //Bind the texture to a texture object


glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter); //Filtering for if texture is bigger than should be

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, maxFilter); //Filtering for if texture is smaller than it should be


if(bpp==32)
bpp=GL_RGBA;

if(bpp==24) //Was the TGA 24 bpp?

bpp=GL_RGB;
glTexImage2D(GL_TEXTURE_2D, 0, bpp, width, height, 0, bpp, GL_UNSIGNED_BYTE, data);


return true;
}

Share this post


Link to post
Share on other sites
Go back to your original code with my suggested change.

Where you have:


int p =y*w+h;


Change it to:


int p =y*w+x; // note the 'x' in place of the 'h'


Your new code is trying to treat a w*h*3 memory buffer as a w*h*4 buffer. Also, once you've set alpha to 0, it never gets set back to 100.


[edited by - Dave Hunt on November 11, 2003 10:35:36 AM]

Share this post


Link to post
Share on other sites