PDA

View Full Version : Bitmap Transparency?



hetunt
02-01-2010, 03:28 AM
Alright, I basically want to read through a bitmap image pixel by pixel, and if the pixel is a certain color, change that pixels alpha value. I'm not quite sure how to do this. I have tried glReadPixels and glDrawPixels, but it isn't even reading them correctly. Here's the code:

<div class="ubbcode-block"><div class="ubbcode-header">Click to reveal.. <input type="button" class="form-button" value="Show me!" onclick="toggle_spoiler(this, 'Yikes, my eyes!', 'Show me!')" />]<div style="display: none;"> GLbyte glDrawPixel[4] = {0,0,0,1},glReadPixel[3] = {0,0,0};
int a,b;

glGenTextures(1,glTexture);
glPixelStorei(GL_UNPACK_ALIGNMENT,4);
glBindTexture(GL_TEXTURE_2D,*glTexture);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);
for(a = 0;a < iWidth;++a)
{
for(b = 0;b < iHeight;++b)
{
glReadPixels(a,b,1,1,GL_RGB,GL_UNSIGNED_BYTE,glRea dPixel);
if(glReadPixel[0] == 0 &amp;&amp;glReadPixel[1] == 1 &amp;&amp; glReadPixel[2] == 1)
{
glRasterPos2f(a,b);
glDrawPixels(1,1,GL_RGBA,GL_UNSIGNED_BYTE,glDrawPi xel);
}
}
}
glTexImage2D(GL_TEXTURE_2D,0,4,iWidth,iHeight,0,GL _RGB,GL_UNSIGNED_BYTE,data);
glBindTexture(GL_TEXTURE_2D,0);[/QUOTE]</div>

Yeah, I really don't know what I'm doing as I've never tried this before. Any help would be appreciated!

strattonbrazil
02-01-2010, 09:32 AM
What's your end goal? This might be just for your example, but you're reading in bytes and you're checking for a color of (0,1,1), which is almost pitch black. Is this what you want? Not something like (0, 255, 255)?

Calling glDrawPixels over and over is incredibly slow. I'm sure it would be at least 100x faster to just render this into a back buffer or FBO using a shader to do the conversion (only changing the alpha that goes out). If you want to speed things up, read back the entire buffer in one call, and draw it all back out in one call. I don't see any reason here why'd you

What's the error you're getting? When you say it isn't being read correctly, are you getting just zeros? If you read in a buffer of just red colors, do you get (255 0 0) (255 0 0) etc. or something else?

What's the texture stuff for? Did you want to write this out to a texture? If that's the case, you should definitely read it all back in one call, do your conversion stuff in C/C++, and send it to the texture using glTexImage2D.

hetunt
02-01-2010, 03:01 PM
Well, I'm trying to read through the image before it is created to check if the color is cyan, and if it is, give it an alpha transparency value. I need to do this so that cyan is the transparent color for the bitmaps I'm using. Or something similar.

Yeah, it is quite slow, but I'm doing it before it enters the drawing loop so it does it all in one go.

I'm just getting all zeros, and nothing else, and I really have no idea why.

Yes, I did want to write this to a texture so that I could print it to the screen later, after it was created.

Edit: Actually, I thought this out and decided to create a mask along with the actual texture while loading the bmp itself. It worked, so thanks for the help!