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 &#40;y = 0; y < src->h; y++&#41;
&#123;
	for &#40;x = 0; x < xend; x++&#41;
	&#123;
		ddata = &#40;&#40;Uint8 *&#41; des->pixels&#41; + y * dpitch + x;
		sdata = &#40;&#40;Uint8 *&#41; src->pixels&#41; + y * spitch + src->w-1 - x;
		*&#40;&#40;Uint8 *&#41; ddata&#41; = *&#40;&#40;Uint8 *&#41; sdata&#41;;


		ddata = &#40;&#40;Uint8 *&#41; des->pixels&#41; + y * dpitch + des->w-1 - x;
		sdata = &#40;&#40;Uint8 *&#41; src->pixels&#41; + y * spitch + x;
		*&#40;&#40;Uint8 *&#41; ddata&#41; = *&#40;&#40;Uint8 *&#41; sdata&#41;;
	&#125;
&#125;

if &#40;SDL_MUSTLOCK&#40;src&#41;&#41;
	SDL_UnlockSurface&#40;src&#41;;
if &#40;SDL_MUSTLOCK&#40;des&#41;&#41;
	SDL_UnlockSurface&#40;des&#41;;

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 &#40;SDL_MUSTLOCK&#40;src&#41;&#41;
	SDL_LockSurface&#40;src&#41;;
if &#40;SDL_MUSTLOCK&#40;des&#41;&#41;
	SDL_LockSurface&#40;des&#41;;

int x = 0, y = 0, xend = src->w;
int deltaD = des->pitch - xend, deltaS = src->pitch + xend ;

Uint8* ddata = &#40;Uint8 *&#41; des->pixels;
Uint8* sdata = &#40;Uint8 *&#41; src->pixels + src->w - 1;

for &#40;y = 0; y < src->h; y++&#41;
&#123;
	x=0;
#ifdef UNROLLOOP
	for &#40;; x < xend-3; x+=4&#41;
	&#123;
		ddata&#91;0&#93; = sdata&#91;3&#93;;
		ddata&#91;1&#93; = sdata&#91;2&#93;;
		ddata&#91;2&#93; = sdata&#91;1&#93;;
		ddata&#91;3&#93; = sdata&#91;0&#93;;
		ddata += 4;
		sdata -= 4;
	&#125;
#endif /* UNROLLOOP */
	for &#40;; x < xend; x++&#41;
	&#123;
		*ddata++ = *sdata--;
	&#125;

      	ddata +=  deltaD;
      	sdata +=  deltaS;
&#125;

if &#40;SDL_MUSTLOCK&#40;src&#41;&#41;
	SDL_UnlockSurface&#40;src&#41;;
if &#40;SDL_MUSTLOCK&#40;des&#41;&#41;
	SDL_UnlockSurface&#40;des&#41;;
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