Jump to content

  • Log In with Google      Sign In   
  • Create Account


SDL reading 24bit texture pixels


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 rocklobster   Members   -  Reputation: 415

Like
1Likes
Like

Posted 19 April 2012 - 05:50 AM

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.

    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;
    }

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

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

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

Sponsor:

#2 Ectara   Crossbones+   -  Reputation: 2916

Like
0Likes
Like

Posted 19 April 2012 - 09:04 AM

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.

#3 rocklobster   Members   -  Reputation: 415

Like
0Likes
Like

Posted 19 April 2012 - 09:21 AM

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.
// 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);


#4 jellyfishchris   Members   -  Reputation: 300

Like
0Likes
Like

Posted 20 April 2012 - 08:43 AM

Can anybody solve this. Ive been thinking it over and cant think of anything =/

#5 rocklobster   Members   -  Reputation: 415

Like
0Likes
Like

Posted 20 April 2012 - 11:35 AM

Answer is here:
http://stackoverflow.com/questions/10243009/convert-uint8-to-unsigned-char-rgb-values/10250809#10250809

#6 rip-off   Moderators   -  Reputation: 8113

Like
0Likes
Like

Posted 21 April 2012 - 12:53 PM

SDL's website includes a getpixel() implementation. 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 SDL_ConvertSurface() to combine these steps).

Now, if you are using OpenGL, you might like to steal* the implementation that is used in the SDL test suite.

* It isn't actually stealing. The SDL test code is in the public domain.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS