Crash when I try to do a vertical flip of a surface

hi…

first, please excuse me for mistakes and gramatika, I don’t speek well
english ;)!

but I hope, that you’ll can understand me :smiley:

So, I explain to you my problem

I create a fonction to flip vertically one surface, but with some surface, my
appilication crash (when I try to blit this surface on the screen)

my fonction is :

SL_Surface::Basic_Flip_Vertical(SDL_Surface* surf)
{
int w = surf->w;
int h = surf->h;
Uint32 temp = new Uint32[hw];

if(SDL_MUSTLOCK(surf))if(SDL_LockSurface(surf)<0)printf("Impossible de 

verouiller la surface\n");

for(int y=0,x;y<=h;y++){	for(x=0;x<=w;x++){
	temp[(y*w)+x] = Basic_GetPixel(surf,x,h-y);
}}
for(y=0;y<=h;y++){			for(x=0;x<=w;x++){
	Basic_PutPixel(surf,x,y,temp[(y*w)+x]);
}}
if(SDL_MUSTLOCK(surf[surf_number]))SDL_UnlockSurface(surf);
delete temp;

}

what do you think about it?

i take Basic_GetPixel and Basic_PutPixel on sdl.org

please help me :frowning:

It’s y < h, not y <= h. The h lines in an image go from 0 to h-1.

Besides, you can do it without using temp[], or by using a temp[] to hold
a single line. Just go swapping line i and h-i-1 while i < h/2.

Ing. Gabriel Gambetta
ARTech - GeneXus Development Team
ggambett at artech.com.uy> ----- Original Message -----

From: loicus [mailto:loicus_2000@yahoo.fr]
Sent: Mi?rcoles, 25 de Febrero de 2004 04:09 p.m.
To: sdl at libsdl.org
Subject: [SDL] crash when I try to do a vertical flip of a surface

hi…

first, please excuse me for mistakes and gramatika, I don’t speek well
english ;)!

but I hope, that you’ll can understand me :smiley:

So, I explain to you my problem

I create a fonction to flip vertically one surface, but with some surface, my
appilication crash (when I try to blit this surface on the screen)

my fonction is :

SL_Surface::Basic_Flip_Vertical(SDL_Surface* surf)
{
int w = surf->w;
int h = surf->h;
Uint32 temp = new Uint32[hw];

if(SDL_MUSTLOCK(surf))if(SDL_LockSurface(surf)<0)printf(“Impossible de
verouiller la surface\n”);

for(int y=0,x;y<=h;y++){ for(x=0;x<=w;x++){
temp[(yw)+x] = Basic_GetPixel(surf,x,h-y);
}}
for(y=0;y<=h;y++){ for(x=0;x<=w;x++){
Basic_PutPixel(surf,x,y,temp[(y
w)+x]);
}}
if(SDL_MUSTLOCK(surf[surf_number]))SDL_UnlockSurface(surf);
delete temp;
}

what do you think about it?

i take Basic_GetPixel and Basic_PutPixel on sdl.org

please help me :frowning:


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

hi…

first, please excuse me for mistakes and gramatika, I don’t speek well
english ;)!

but I hope, that you’ll can understand me :smiley:

So, I explain to you my problem

I create a fonction to flip vertically one surface, but with some surface,
my appilication crash (when I try to blit this surface on the screen)

my fonction is :

SL_Surface::Basic_Flip_Vertical(SDL_Surface* surf)
{
int w = surf->w;
int h = surf->h;
Uint32 temp = new Uint32[hw];

if(SDL_MUSTLOCK(surf))if(SDL_LockSurface(surf)<0)printf(“Impossible de
verouiller la surface\n”);

for(int y=0,x;y<=h;y++){ for(x=0;x<=w;x++){
temp[(yw)+x] = Basic_GetPixel(surf,x,h-y);
}}
for(y=0;y<=h;y++){ for(x=0;x<=w;x++){
Basic_PutPixel(surf,x,y,temp[(y
w)+x]);
}}
if(SDL_MUSTLOCK(surf[surf_number]))SDL_UnlockSurface(surf);
delete temp;
}

what do you think about it?

i take Basic_GetPixel and Basic_PutPixel on sdl.org

please help me :frowning:

You are reading (and writing) to memory outside of the SDL surface and the
block you allocate.

Imagine this is your ‘temp’, corresponding to a surface that is width=7,
height=3:

0123456 width=7
0*******
1*******
2*******
height=3

In your loops :

for(int y=0; y<=h; y++)
    for(int x=0; x<=w; x++)
        temp[(y*w)+x] = Basic_GetPixel(surf, x, h-y);

you are making (x, y) take the values (0…7, 0…3) which will cause weird edge
effects in the case of x, and probably segfaults in the case of y as you step
off the end.

Try:

for(int y=0; y<h; y++)		                    /*note the '<'*/
    for(int x=0; x<w; x++)                              /*note the '<'*/
        temp[(y*w)+x] = Basic_GetPixel(surf, x, h-y-1); /*note the -1 */

and don’t forget to change the ranges for the other loops.

Here’s a tip: mentally check the boundary conditions:

when y is 0 the offset (h-y-1) is 3-0-1 = 2 CORRECT!
when y is h-1 (in this case 2) the offset (h-y-1) is 3-2-1 = 0 CORRECT!

Good Luck,

cheers,
John Popplewell.

Note: I am assuming that inside Basic_GetPixel() you have this calculation:
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;On Wednesday 25 February 2004 7:09 pm, loicus wrote:


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl