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
Fast mirror image
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);
kjdflkdfdfsdf
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.
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
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);