Sign in to follow this  
rocklobster

SDL reading 24bit texture pixels

Recommended Posts

rocklobster    415
Hi guys,

I've recently been trying to generate a texture using a procedural method however i've ran into a problem. What i'm currently doing is looping through each pixel in a heightmap, and then summing up the pixels of x many textures by a certain weight (eg. If the heightmap pixel was fully white, then it's the highest point so a snow texture will be weighted strongly). The problem is at a certain point in this algorithm my getColourAt(int x, int y) breaks.

[CODE]
RGBColour TextureReader::getColourAt(int x, int y)
{
Uint8* p = (Uint8*)m_surface->pixels + y * m_surface->pitch + x * m_surface->format->BytesPerPixel;
Uint32 pixels = p[(y * m_surface->w) + x]; // format, &red, &green, &blue);
RGBColour rgb(red, green, blue);
return rgb;
}
[/CODE]

It breaks with x index of 109 and y index of 236. The textures i'm using are 256x256 24bit TGA textures. The only problem i've been able to narrow down to is that it might be something to do with 24bit texture and using an Uint32.

Just to clarify i've tested

[CODE]
Uint8* p = (Uint8*)m_surface->pixels + y * m_surface->pitch + x * m_surface->format->BytesPerPixel;
[/CODE]

Using Uint32* instead of Uint8* and it breaks at a different index. I've checked that the surface is valid and it is. I can view the bbp, format, width and height etc.

Thanks for any assistance.

- rocklobster

Share this post


Link to post
Share on other sites
Ectara    3097
First, since this is SDL, are you sure that the surface is an RGB24 surface, and not an RGBX32? Very carefully check the bitmasks for the channels, to ensure that's not the problem.

Second, what is this:
Uint32 pixels = p[(y * m_surface->w) + x]; // format, &red, &green, &blue);

Uint32 pixels = (Uint8)p[((int)y * (int)m_surface->w) + (int)x];

You are assigning a Uint8 to a Uint32. Where are red, green, and blue coming from? I get the feeling that this is not your real code.

Share this post


Link to post
Share on other sites
rocklobster    415
I've reworked my code from yesterday and i'm still getting some problems. I can get the image to draw, but the colors are usually just greyscale.
[CODE]
// my get colour at function
RGBColour SDLSurfaceReader::getColourAt(int x, int y)
{
Uint32 pixel;
Uint8 red, green, blue;
RGBColour rgb;
pixel = getPixel(m_surface, x, y); //getPixel function is from http://www.libsdl.org/cgi/docwiki.cgi/Pixel_Access

SDL_LockSurface(m_surface);
SDL_GetRGB(pixel, m_surface->format, &red, &green, &blue);
SDL_UnlockSurface(m_surface);
rgb.r = red;
rgb.b = blue;
rgb.g = green;
return rgb;
}

// main part of the code, recreating the surface. I'm trying to recreate it because i will eventually be blending multiple textures based on height and i just // wanted to make sure i was getting color values correctly first.

unsigned char* newTexture = new unsigned char[reader->m_surface->w * reader->m_surface->h * reader->m_surface->format->BytesPerPixel];

for (int y = 0; y < reader->m_surface->h; y++)
{
for (int x = 0; x < 3 * reader->m_surface->w; x += 3)
{
int index = (y * reader->m_surface->w) + x;
RGBColour colour = reader->getColourAt(x, y);
newTexture[index] = colour.r;
newTexture[index + 1] = colour.g;
newTexture[index + 2] = colour.b;
}
}

unsigned int id = m_renderer->createTextureFromData(newTexture, reader->m_surface->w, reader->m_surface->h, RGB);
[/CODE]

Share this post


Link to post
Share on other sites
rocklobster    415
Answer is here:
[url="http://stackoverflow.com/questions/10243009/convert-uint8-to-unsigned-char-rgb-values/10250809#10250809"]http://stackoverflow.com/questions/10243009/convert-uint8-to-unsigned-char-rgb-values/10250809#10250809[/url]

Share this post


Link to post
Share on other sites
rip-off    10976
SDL's website includes a [url="http://www.libsdl.org/docs/html/guidevideo.html"]getpixel() implementation[/url]. This will work on an arbitrary surface. Another option is to create a SDL surface with the required layout, and let SDL do the heavy lifting via SDL_BlitSurface() (or perhaps even use [url="http://sdl.beuc.net/sdl.wiki/SDL_ConvertSurface"]SDL_ConvertSurface[/url]() to combine these steps).

Now, if you are using OpenGL, you might like to steal* the implementation that is used in the [url="http://hg.libsdl.org/SDL/file/c787fb1b5699/test/testgl.c#l171"]SDL test suite[/url].

* [size="2"]It isn't actually stealing. The SDL test code is in the [url="http://hg.libsdl.org/SDL/file/c787fb1b5699/test/COPYING"]public domain[/url].[/size]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this