Jump to content
  • Advertisement
Sign in to follow this  
songuke

SDL_image aligned or unaligned?

This topic is 3720 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys, SDL_image library provided IMG_load function that help load images. I loaded a bitmap 256x256 and compare with another load of 257x257. I saw the 257x257 bitmap does not render correctly (the image was distorted and unable to see). I thought the SDL_image resulted with SDL_surface with the array of internal pixels (SDL_surface->pixels) is an aligned image (with additional bytes padded at each row if the width % 4 != 0). Am I right?

Share this post


Link to post
Share on other sites
Advertisement
If you want to access the image pixels you need to take into account the pitch of the image (which might be what you mean). The pitch is always at least as big as the width, but I don't think you should be counting on it being any particular number. So IIRC to access a particular pixel it's y * pitch / bpp + x, like so:


SDL_Surface* s;
//...
int pitch = s->pitch;
int bpp = s->format->BytesPerPixel;
unsigned int *sPix = (unsigned int*)s->pixels;
for (int j = 0; j!= s->h; ++j) {
for (int i = 0; i != s->w; ++i) {
sPix[j * pitch / bpp + i] = ...;
}
}
//...


Share this post


Link to post
Share on other sites
I see, so pitch = bytes per pixel (e.g. 3 for RGB) * width + a number of padded bytes, isn't it? :)

I did recalculate the padded width. Dunno it was supported in the SDL_surface structure. :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!