# SDL_CalculatePitch possible errror?

Heya all,

Well I was looking at SDL 1.2.5 sources and I found something I’m not sure
to understand :

Here is the SDL_CalculatePitch internal function to compute the pitch of a
given surface :

Uint16 SDL_CalculatePitch(SDL_Surface *surface)
{
Uint16 pitch;

``````   /* Surface should be 4-byte aligned for speed */
pitch = surface->w*surface->format->BytesPerPixel;
switch (surface->format->BitsPerPixel) {
case 1:
pitch = (pitch+7)/8;
break;
case 4:
pitch = (pitch+1)/2;
break;
default:
break;
}
pitch = (pitch + 3) & ~3;       /* 4-byte aligning */
return(pitch);
``````

}

Well, the switch is based on the surface->format->BitsPerPixel value which
can be one of [8,16,24,32]
But the two cases are on [1,4] which then would correspond instead to
surface->format->BytesPerPixel

So I was wondering if it was an error or if I was wrong somewhere

Bye

• Sylvain Hellegouarch
"Lawouach"

Heya all,

Well I was looking at SDL 1.2.5 sources and I found something I’m not sure
to understand :

Here is the SDL_CalculatePitch internal function to compute the pitch of a
given surface :

Uint16 SDL_CalculatePitch(SDL_Surface *surface)
{
Uint16 pitch;

``````   /* Surface should be 4-byte aligned for speed */
pitch = surface->w*surface->format->BytesPerPixel;
switch (surface->format->BitsPerPixel) {
case 1:
pitch = (pitch+7)/8;
break;
case 4:
pitch = (pitch+1)/2;
break;
default:
break;
}
pitch = (pitch + 3) & ~3;       /* 4-byte aligning */
return(pitch);
``````

}

Well, the switch is based on the surface->format->BitsPerPixel value which
can be one of [8,16,24,32]
But the two cases are on [1,4] which then would correspond instead to
surface->format->BytesPerPixel

So I was wondering if it was an error or if I was wrong somewhere

I guess 8, 15, 16, 24 and 32 are all dword aligned. And 1 and 4 need extra
alignment?

Bye

• Sylvain Hellegouarch
"Lawouach"

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

----- Original Message -----
From: shellegouarch@programmationworld.com (sylvain HELLEGOUARCH)
To:
Sent: Friday, April 04, 2003 6:58 PM
Subject: [SDL] SDL_CalculatePitch possible errror ?

Uint16 SDL_CalculatePitch(SDL_Surface *surface)
{
Uint16 pitch;

``````   /* Surface should be 4-byte aligned for speed */
pitch = surface->w*surface->format->BytesPerPixel;
switch (surface->format->BitsPerPixel) {
case 1:
pitch = (pitch+7)/8;
break;
case 4:
pitch = (pitch+1)/2;
break;
default:
break;
}
pitch = (pitch + 3) & ~3;       /* 4-byte aligning */
return(pitch);
``````

}

Well, the switch is based on the surface->format->BitsPerPixel
value which can be one of [8,16,24,32]
But the two cases are on [1,4] which then would correspond instead
to surface->format->BytesPerPixel

So I was wondering if it was an error or if I was wrong somewhere

I guess 8, 15, 16, 24 and 32 are all dword aligned. And 1 and 4 need
extra alignment?

Yes you are partly right. The dword alignment is done for every depth
after the switch. The less than byte per pixel depths need those extra
calculations to make the pitch correct.

Example you have 1 bit per pixel surface as 1 pixel wide. So it’s pitch
is (1+7)/8 = 1, which is correct. for 4 bit per pixel it would be
(1+1)/2, which also is correct.On Friday 04 April 2003 20:44, Olaf van der Spek wrote:

----- Original Message -----
From: “sylvain HELLEGOUARCH”
To:
Sent: Friday, April 04, 2003 6:58 PM
Subject: [SDL] SDL_CalculatePitch possible errror ?