Page 1 of 1
Fast mirror image
Posted: Tue Aug 08, 2006 8:44 pm
by wing64
Hello guy
I use SDL for display image but when i need mirror my image i locked surface and for-loop width and height to swap pixel and draw to surface.
That's a problem it very slow. Anybody have a fast algorithm for mirror image please help me..... :-(
best regards
wing64
Posted: Tue Aug 08, 2006 9:57 pm
by tmator_
can you post your code ?
Posted: Thu Aug 10, 2006 1:05 pm
by wing64
Here this code for mirror surface
Code: Select all
if (SDL_MUSTLOCK(src))
SDL_LockSurface(src);
if (SDL_MUSTLOCK(des))
SDL_LockSurface(des);
Uint8* sdata = (Uint8*)src->pixels;
Uint16 spitch = src->pitch;
Uint8* ddata = (Uint8*)des->pixels;
Uint16 dpitch = des->pitch;
int x = 0, y = 0, xend = (src->w + 1) / 2;
for (y = 0; y < src->h; y++)
{
for (x = 0; x < xend; x++)
{
ddata = ((Uint8 *) des->pixels) + y * dpitch + x;
sdata = ((Uint8 *) src->pixels) + y * spitch + src->w-1 - x;
*((Uint8 *) ddata) = *((Uint8 *) sdata);
ddata = ((Uint8 *) des->pixels) + y * dpitch + des->w-1 - x;
sdata = ((Uint8 *) src->pixels) + y * spitch + x;
*((Uint8 *) ddata) = *((Uint8 *) sdata);
}
}
if (SDL_MUSTLOCK(src))
SDL_UnlockSurface(src);
if (SDL_MUSTLOCK(des))
SDL_UnlockSurface(des);
Posted: Thu Aug 24, 2006 4:35 am
by Npl
Having uneeded multiplications in your code is generally a bad idea, using forward differences solves that. Also its better to use a pointer that advances instead of calculating offsets, saves instructions.
Code: Select all
if (SDL_MUSTLOCK(src))
SDL_LockSurface(src);
if (SDL_MUSTLOCK(des))
SDL_LockSurface(des);
int x = 0, y = 0, xend = src->w;
int deltaD = des->pitch - xend, deltaS = src->pitch + xend ;
Uint8* ddata = (Uint8 *) des->pixels;
Uint8* sdata = (Uint8 *) src->pixels + src->w - 1;
for (y = 0; y < src->h; y++)
{
x=0;
#ifdef UNROLLOOP
for (; x < xend-3; x+=4)
{
ddata[0] = sdata[3];
ddata[1] = sdata[2];
ddata[2] = sdata[1];
ddata[3] = sdata[0];
ddata += 4;
sdata -= 4;
}
#endif /* UNROLLOOP */
for (; x < xend; x++)
{
*ddata++ = *sdata--;
}
ddata += deltaD;
sdata += deltaS;
}
if (SDL_MUSTLOCK(src))
SDL_UnlockSurface(src);
if (SDL_MUSTLOCK(des))
SDL_UnlockSurface(des);
generally the code in UNROLLOOP is not needed, but should be a bit faster. I dont think you could optimize much further without using assembly