Thursday, December 17, 2015

sdl - Where can I get correct pitch parameter for SDL_RenderReadPixels function in SDL2?


I have a texture created with SDL_TEXTUREACCESS_TARGET access and I want to get all its pixels with SDL_RenderReadPixels() function. One of the function parameters is pitch and I don't really know where can I can get it.


Texture is created with dimensions of a previously created surface, but function call with surface->pitch as pitch parameter generates EXC_BAD_ACCESS.


Texture and surface dimensions are 800x600, and surface->pitch returns 3200, which is strange for me, because I thought that pitch is the width of the texture in memory and expected surface->pitch to be something like 1024.



Answer



Pitch: the length of a row of pixels in bytes


Basically, if your texture/surface is 800px wide, and you have 4 bytes per pixel (32bit colour) then the pitch is 800x4 = 3200.


Use SDL_QueryTexture() to get texture format, the format can tell you how many bytes per pixel.




SDL_QueryTexture(texture, &format, NULL, NULL, NULL);
// Parse format with a switch

QueryTexture can also tell you the Access type, Width and Height.

SDL_PIXELFORMAT_RGB332 //8bit colour



...




SDL_PIXELFORMAT_RGB555 //15bit/16bit colour



...



SDL_PIXELFORMAT_ABGR8888 //32-bit colour



Note the last 3-4 numbers tell you how many bits are used for each colour, add them together to get the number of bits per pixel (bpp), then multiply bpp by width to get pitch.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...