Page 1 of 1

Bit Masking Help

Posted: Fri Apr 30, 2010 6:23 pm
by Maevik
I am trying to write an algorithm to create an SDL_Surface and build on it a white silhouette of the image from another SDL_Surface (source). I iterate through each pixel in the SDL_Surface and grab the R G and B values of that pixel. If the RGB is equal to transparency key, I draw a pixel of the transparent color, otherwise I draw a white pixel. The code that grabs the pixel color is not working and since this is my first attempt at working with bit masking, I think it's time to ask for help. Here is the code I am using, this is what is between the locking and unlocking of the two surfaces.

Code: Select all

	for( int i = 0 ; i < w ; ++i )
	{
		for( int j = 0 ; j < h ; ++j )
		{
			//Get the format pointers for each surface
			SDL_PixelFormat* fmt = source->format;
			SDL_PixelFormat* dfmt = flashSurface->format;

                        //Make a pointer I can use to manipulate data
			char* pixdata = NULL;
			char* destdata = NULL;

                        //Grab the frame buffers
			pixdata = (char*)source->pixels;
			destdata = (char*)flashSurface->pixels;

                        //Locate the data within the buffer ( i=x , j=y )
			pixdata = pixdata + ( j * source->pitch ) + ( i * fmt->BytesPerPixel );
			destdata = destdata + ( j * flashSurface->pitch ) + ( i * dfmt->BytesPerPixel );

                        //Here is where I use the Bit Masking to aquire the RGB values from the buffer.  I don't think this is working correctly
			Uint8 tr = (Uint8)(( *pixdata & fmt->Rmask ) >> fmt->Rshift );
			Uint8 tg = (Uint8)(( *pixdata & fmt->Gmask ) >> fmt->Gshift );
			Uint8 tb = (Uint8)(( *pixdata & fmt->Bmask ) >> fmt->Bshift );


                        //Output the values captured to the debug log: ALWAYS OUTPUTS "R: 0  G: 0  B: 0"
			stringstream temp;
			temp << "R: " << (int)tr << "  G: " << (int)tg << "  B: " << (int)tb; 
			Log->print( temp.str() );

			if( ( tr == 85 ) && ( tg == 255 ) && ( tb == 0 ) )   //If the RGB is the color key, keep the color so it draws transparent
			{
				color = SDL_MapRGB( fmt , tr , tg , tb );
				memcpy( destdata , &color , dfmt->BytesPerPixel );

			}else                                                                 //Otherwise, draw a white pixel
				memcpy( destdata , &white , dfmt->BytesPerPixel );
		}
	}

Re: Bit Masking Help

Posted: Fri Apr 30, 2010 7:48 pm
by Ginto8
I presume that this is for highlighting? Well, if you don't want to do an SDL_FillRect before blitting, then you can take a solid white texture (made white by SDL_FillRect), and then blitting on a surface completely made of the color key color, without setting the SDL_COLORKEY (IIRC that's what it is) bit. That's the most efficient, but if you're doing non-rectangular shapes, I don't know how to help you.