0
\$\begingroup\$

People have asked questions on fast pixel manipulation, but I'm looking to read the RGB color values of each pixel.

I've heard of SDL_RenderReadPixels but in the docs it says:

WARNING: This is a very slow operation, and should not be used frequently.

Is there an SDL2 function that returns an array of all the pixels of a texture or an SDL2 routine that I can use to loop over each pixel?

\$\endgroup\$
6
\$\begingroup\$

SDL2's "texture" rendering API is designed for accelerated graphics in mind; this is where you upload textures to your graphics hardware beforehand, and then render that texture by referring to it.

This is why textures are write-only, and can only be written to if:

  • the texture was created with the SDL_TEXTUREACCESS_STREAMING flag
  • you lock the texture before writing using SDL_LockTexture

So if you want to do pixel manipulation, you should do it in-memory, using a plain pixel buffer (or sometimes an SDL_Surface). Then when you're done, create a texture out of the buffer. If you do this repeatedly with the same texture, use a texture with the aforementioned SDL_TEXTUREACCESS_STREAMING flag. This is still very slow though because it's unaccelerated.

For efficient pixel manipulation, the standard practice is to write shader code.

\$\endgroup\$
  • \$\begingroup\$ I'm not looking to manipulate the pixels, just read them. \$\endgroup\$ – Viliami Sep 26 '16 at 4:40
  • 4
    \$\begingroup\$ That is unfortunately also cataloged as manipulation. Data has to go back and forth between CPU and GPU. The GPU simply does not provide direct CPU access to its memory contents. It does not work that way. \$\endgroup\$ – rlam12 Sep 26 '16 at 5:00
  • 1
    \$\begingroup\$ @Viliami - it might be helpful if you said what you are actually trying to do rather than how you are trying to do it; i.e. why you are reading the pixels. I say this because there may be another, more efficient, way of getting the same result. \$\endgroup\$ – Maximus Minimus Sep 26 '16 at 9:13
  • \$\begingroup\$ @LeComteduMerde-fou I am comparing all the pixel values on the SDL window to an image, to see how similar the images are, used for something like this: rogeralsing.com/2008/12/07/… \$\endgroup\$ – Viliami Sep 27 '16 at 1:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.