Skip to main content
GameDev.net gamedev.net
🔒 Locked

SDL_image aligned or unaligned?

Started by songuke Mar 17, 2008 at 12:03 AM 1 replies 900+ views
Original Post
songuke
songuke
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?
sprite_hound
sprite_hound
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] = ...;        }    }    //...
songuke
songuke
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. :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.