16 bpp direct pixel access trouble

Hello!
I’ve written a function to do mirror blitting (left to right)
But this code works only 32 bpp color depth.
I can not find any information about how to do this with 16 bpp.
Please HELP!
Thank you!

Sincerelly yours,
Evgeny

// --------------------------------------------
SDL_Surface* mirror_hor(SDL_Surface* src){

SDL_Surface* temp = SDL_DisplayFormat(src);

int pTemp, pSrc;
int i, j;
int Bpp = temp->format->BytesPerPixel;

assert(Bpp = src->format->BytesPerPixel);

SDL_LockSurface(temp);
SDL_LockSurface(src);

pSrc = (int)src->pixels;

/* HOR MIRROR /
pTemp = (int)temp->pixels + (temp->w-1)Bpp;
for(i=0; i< src->h; i++){
for(j=0; j< src->w; j++){
memcpy((void
)pTemp, (void
)pSrc, Bpp);
pTemp-=Bpp;
pSrc+=Bpp;
}
pTemp+=temp->pitch*2;
}

SDL_UnlockSurface(temp);
SDL_UnlockSurface(src);

return temp;
}

“E$h” writes:

Hello!
I’ve written a function to do mirror blitting (left to right)
But this code works only 32 bpp color depth.
I can not find any information about how to do this with 16 bpp.
Here’s the slight modification of your function that should work
regardless of underlying bpp:—
SDL_Surface* mirror_hor(SDL_Surface* src)
{
char *pSrc, pTemp;
SDL_Surface
temp = SDL_CreateRGBSurface(SDL_SWSURFACE, src->w, src->h,
src->format->BitsPerPixel,
src->format->Rmask,
src->format->Gmask,
src->format->Bmask,
src->format->Amask);
int Bpp = temp->format->BytesPerPixel;
int i, j;

SDL_LockSurface(temp);
SDL_LockSurface(src);

pSrc = (char *)src->pixels; 

/* HOR MIRROR */
pTemp = (char *)temp->pixels + (temp->w-1)*Bpp;
for(i = 0; i < src->h; i++)
{
    for(j = 0; j < src->w; j++)
    {
        memcpy((void*)pTemp, (void*)pSrc, Bpp);

        pTemp -= Bpp;
        pSrc  += Bpp;
    }
    
    pTemp += temp->pitch * 2;
}

SDL_UnlockSurface(temp);
SDL_UnlockSurface(src);

return temp;

}

You might also want to take a look at getpixel and putpixel functions
from SDL documentation (chapter 2 - Graphics and Video).
There’s also SDL_rotozoom addon library but that might be an overkill.
Oh, and of course there’s OpenGL :wink:

hth,
– ed

Thank you very much!!!
It’s great!

Ed Sinjiashvili wrote:> Here’s the slight modification of your function that should work

regardless of underlying bpp:

You might also want to take a look at getpixel and putpixel functions
from SDL documentation (chapter 2 - Graphics and Video).
There’s also SDL_rotozoom addon library but that might be an overkill.
Oh, and of course there’s OpenGL :wink:

hth,
– ed