0

I use SDL_image to load an image to a SDL_Surface. How can I get all this data into a vector <int> pixels?

I have tried:

Uint32 GetPixel(SDL_Surface *img, int x, int y) {
SDL_LockSurface(img);

//Convert the pixels to 32 bit
Uint32 *pixels = (Uint32 *)img->pixels;

//Get the requested pixel
return pixels[(y * img->w) + x];

SDL_LockSurface(img);
}

And I wonder why I get strange results... with a 3x3 Image with a black pixel in the center i get:

[0] (0, 0): 255, 255, 255
[1] (0, 1): 255, 255, 255
[2] (0, 2): 255, 255, 255
[3] (1, 0): 255, 255, 255
[4] (1, 1): 255, 0, 0               //WHY IS THIS NOT 0,0,0?
[5] (1, 2): 255, 255, 255
[6] (2, 0): 255, 255, 255
[7] (2, 1): 255, 255, 255
[8] (2, 2): 255, 255, 255
Ledii
  • 261
  • 1
  • 3
  • 13
  • I want pixel[0][0] to go to index 0. [0][1] in index 1, and [1][0] into 16 visa versa.. I need the data for a Neural Network – Ledii Jul 01 '14 at 21:43
  • You can click [edit] to clarify your question. – Drew Dormann Jul 01 '14 at 21:55
  • http://gamedev.stackexchange.com/questions/62705/how-do-i-access-the-pixels-of-an-sdl-2-texture – didierc Jul 01 '14 at 22:33
  • Code after your return statement won't execute. You probably want to copy the value to a temporary value and then unlock the surface before returning the temporary value. – Retired Ninja Jul 01 '14 at 22:34
  • You need to take the `pitch` value in account when reading pixels. – didierc Jul 01 '14 at 22:35
  • Check the pixel format. If you loaded a bmp file it's probably 24bpp, so indexing the way you are is incorrect. A short complete example showing how you create the surface would help quite a bit. – Retired Ninja Jul 01 '14 at 23:34
  • thank you, i completly overlooked that. i'll fix my return – Ledii Jul 04 '14 at 19:26

0 Answers0