Archived

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

altering images data

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

does anyone know which part of the source below would i need to edit so that all when it loading the bmp file and finds a byte that is black it will make it transparent, it is mentioned on the bottom of nehe''s masking tut but he never explains how to do it... than guys
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;
		}

	//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
Hope this is the ansder you''re looking for...

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

// Check for black pixel
if(data[imageIdx+0]+data[imageIdx+1]+data[imageIdx+2]==0)
{
// Pixel is black and you can do whatever you want to it. If you''re doing alpha blending, just set the alpha to zero for this pixel.
}
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
If you have RGB data and you want to make it RGBA do this.. (I am writing this from scratch, so think, there might be mistakes..)


int w = image_width;
int h = image_height;

uchar rgb_data[w*h*3]; // this the old image with rgb data


// Alloc new buffer for rgba image

uchar *rgba_data = new uchar[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; // item position in data

rtgba_data[p*4+0] = rgb_data[p*3+0];
rtgba_data[p*4+1] = rgb_data[p*3+1];
rtgba_data[p*4+2] = rgb_data[p*3+2];

// Now set alpha channel full visible by default

uchar alpha = 255;

// If color is full black then set alpha to invisible (zero)

if (rgb_data[p*3+0]==0 && rgb_data[p*3+1]==0 && rgb_data[p*3+2]==0) alpha = 0;
rtgba_data[p*4+3] = alpha;
}
}
// end


This is it basically.. you might want to define what is black differently.. for example
if (rgb_data[p*3+0]<=10 && rgb_data[p*3+1]<=10 && rgb_data[p*3+2]<=10) alpha = 0;


Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Gresc
is this the only way to not showq black bytes... is there a simpleuer way to just not draw the black parts


Iirc no.. You can use GL_ONE blending, but it makes non-full-white colors lighter.. and only full black fully transparent.

Believe me, this is very simple way.. just write it once and it is done It is done quicker than you propably find out that there is or there is not a simpler way..

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
i am sorry to makes such a stoopid and newbish post like this but would someone be able to substitute that piece of code into the LoadBMP function Gresc gave us, I am having real trouble with this. thanx guys

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Anonymous Poster
i am sorry to makes such a stoopid and newbish post like this but would someone be able to substitute that piece of code into the LoadBMP function Gresc gave us, I am having real trouble with this. thanx guys

Geez, how lazy are newbies getting these days? They expect to be spoonfed...

Share this post


Link to post
Share on other sites
This is the problem with tutorials. Certain people think cutting and pasting is a substitute for learning how to program. Well, it''s not. If you don''t understand how the tutorial works, perhaps you should start with something more basic.

Closed.

Share this post


Link to post
Share on other sites

This topic is 5150 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.

Guest
This topic is now closed to further replies.