Pixels

Hi everyone,
I’m an SDL newbie and pretty much a graphics programming newbiw altogether.
what’s the fastest way to place pixels on a surface that isn’t the screen surface? The put_pixel function in the doc’s is pretty slow and cumbersome if you’re going to be placing 40,000 more pixels in one run.
I’ve been trying something along this line:

Uint32 *p = (Uint32 *)surface->pixels;
Uint32 color = SDL_MapRGB(/stuff/);
int x, y, width=surface->w;
int offset = x * y + width;

for(i=0; i<a_bunch_of_pixels; i++)
{
p[offset] = some_color;
// go on to alter the values of x and y
offset = x*y+width;
}

After tha I’m blitting surface to the screen by,
SDL_BlitSurface(surface, NULL, screen, NULL);
like in the BMP example.

Any constructive criticism would be much appreciated.
THanks,
Chip

Hi everyone,
I’m an SDL newbie and pretty much a graphics programming newbiw
altogether.
what’s the fastest way to place pixels on a surface that isn’t the
screen surface? The put_pixel function in the doc’s is pretty slow and
cumbersome if you’re going to be placing 40,000 more pixels in one run.
I’ve been trying something along this line:
?
Uint32 *p = (Uint32 )surface->pixels;
Uint32 color = SDL_MapRGB(/stuff/);
int x, y, width=surface->w;
int offset = x * y?+ width;
?
for(i=0; i<a_bunch_of_pixels; i++)
{
??? p[offset]? = some_color;
??? // go on to alter the values of x and y
??? offset = x
y+width;
}

If you really want speed, you should remove the multiply from the inner
loop.

You’ll probably instead want to do something like this, breaking out the
x and y:

int y_offset = 0;

 for ( y = 0  ;  y < image.height  ; x++)
 {
for ( x = 0  ;  x < image.width  ; x++)
{
    // process pixel x,y

    image[ y_offset + x ] =  theNewPixelColor;
}

y_offset += image.width;  // rather than (y_offset = y * image.width)
 }

So instead of a multiply and an add for every pixel, you instead are
doing an add per pixel and another add per row… this ends up being
hella faster than the alternate.

I have a lot of stuff I’ve explained out with relations to 2-d engines
at this site:

http://www.cis.rit.edu/~jerry/Software/demo/

It’s not focused at SDL, but rather, It’s about basic graphics concepts
and effects.

In my paint program, 4p
http://www.cis.rit.edu/~jerry/Software/4p
I actually do the case that you’re talking about (from a non-sdl surface
to a sdl surface).
Look in the file “src/jsui/gfx_base.c”, you’ll see this process in
there. (warning: i haven’t cleaned up the source or commented it very
well yet… sorry about that.)
Also, if you look in “src/jsui/page.c”, in the function
"Page_Blit_Image", there is sample code there
that blits between two non-SDL surfaces, possibly different sizes, etc…

Hope this helps!

-jOn Sunday, October 21, 2001, at 02:30 AM, Chip Collier wrote:


Scott “Jerry” Lawrence
@Scott_Jerry_Lawrence

For all of your Cheese Weasel needs… http://www.cheeseweasel.com

what’s the fastest way to place pixels on a surface that isn’t the
screen surface? The put_pixel function in the doc’s is pretty slow and
cumbersome if you’re going to be placing 40,000 more pixels in one run.
I’ve been trying something along this line:

Uint32 *p = (Uint32 *)surface->pixels;
Uint32 color = SDL_MapRGB(/stuff/);
int x, y, width=surface->w;
int offset = x * y + width;

for(i=0; i<a_bunch_of_pixels; i++)
{
p[offset] = some_color;
// go on to alter the values of x and y
offset = xy+width;
}
I’ll just point out that your code is NOT correct (would not produce the
result expected) :
the correct formula to calculate the offset for a pixel is
offset = y
width + x;
or even better
offset = y*pitch + x;

if you want speed and you can compute the offset for a pixel with the offset
of the previous pixel (for example, you can use a formula like
x = x + xinc
y = y + yinc
I’ll advise you to include the base adress of the image in the offset and to
calculate the total offset increment outside the loop. ex:

offset_inc = surface->pitchyinc + xinc;
int offset = (int)surface->pixels + y
surface->pitch + x;
for (;:wink: {
*offset = some_color;
offset += offset_inc;
}

Note that in the above examples, I’ve assumed you work with 8bpp surfaces.
If you work with different bpp’s, you should correct the formula by multiply
the x value by the BytesPerPixels of the surface…

I hope it helps,
Ged.