Get and Put Pixel 32

Hello,

I already successful to copy a surface. But, i want to ask about this logic:

// ===========================================
// function
void put_pixel32( SDL_Surface *surface, int x, int y, Uint32 pixel )
{
//Convert the pixels to 32 bit
Uint32 *pixels = (Uint32 *)surface->pixels;

//Set the pixel
pixels[ ( y * surface->w ) + x ] = pixel;

}

Uint32 get_pixel32( SDL_Surface *surface, int x, int y )
{
//Convert the pixels to 32 bit
Uint32 *pixels = (Uint32 *)surface->pixels;

//Get the requested pixel
return pixels[ ( y * surface->w ) + x ];

}
// ====================================
// i get this function’s code from LazyFoo.net

// in main function
SDL_Surface *newSurface = NULL;
baru = SDL_CreateRGBSurface(SDL_SRCALPHA|SDL_SRCCOLORKEY|SDL_HWSURFACE,
oldSurface->w , ButtonSurface->h
,screen->format->BitsPerPixel,temp_->Rmask,temp_->Gmask,temp_->Bmask,temp_->Amask);

    if ( SDL_MUSTLOCK(newSurface))
    {
        SDL_LockSurface( newSurface);
    }

    for(int x=0; x< newSurface->w ; x++)
    {
        for(int y=0; y< (newSurface->h/2); y++)
        {
        //for(int y=0; y< newSurface->h; y++){         //<---- will

error
put_pixel32(newSurface,x,y,get_pixel32(oldSurface,x,y));

        }
    }

    if ( SDL_MUSTLOCK(newSurface))
    {
        SDL_UnlockSurface( newSurface);
    }


// ========================================

I still don’t understand , why condition for h must divide by 2 in 'y’
looping.–
View this message in context: http://sdl.5483.n7.nabble.com/Get-and-Put-Pixel-32-tp36541.html
Sent from the SDL mailing list archive at Nabble.com.

Sorry my Fault,

i set screen surface as format to 16 bit, the function need 32 bit.–
View this message in context: http://sdl.5483.n7.nabble.com/Get-and-Put-Pixel-32-tp36541p36542.html
Sent from the SDL mailing list archive at Nabble.com.

Also, when calculating an index into surface->pixels, use surface->pitch
instead of surface->w. The pitch is the number of bytes in a scanline. If
you’re using Uint32*, then I believe you would change surface->w to
surface->pitch/32:

pixels[ ( y * surface->pitch/32 ) + x ];

To make it more generic, you could use a Uint8* pointer into the pixels,
multiply your x by surface->format->BytesPerPixel, and use the bare pitch:

pixels[ ( y * surface->pitch ) + x*surface->format->BytesPerPixel ];

Jonny DOn Fri, Aug 30, 2013 at 4:44 AM, Krofz wrote:

Sorry my Fault,

i set screen surface as format to 16 bit, the function need 32 bit.


View this message in context:
http://sdl.5483.n7.nabble.com/Get-and-Put-Pixel-32-tp36541p36542.html
Sent from the SDL mailing list archive at Nabble.com.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Sure you don’t mean pitch/4?

2013/9/4, Jonathan Dearborn :> Also, when calculating an index into surface->pixels, use surface->pitch

instead of surface->w. The pitch is the number of bytes in a scanline. If
you’re using Uint32*, then I believe you would change surface->w to
surface->pitch/32:

pixels[ ( y * surface->pitch/32 ) + x ];

To make it more generic, you could use a Uint8* pointer into the pixels,
multiply your x by surface->format->BytesPerPixel, and use the bare pitch:

pixels[ ( y * surface->pitch ) + x*surface->format->BytesPerPixel ];

Jonny D

On Fri, Aug 30, 2013 at 4:44 AM, Krofz wrote:

Sorry my Fault,

i set screen surface as format to 16 bit, the function need 32 bit.


View this message in context:
http://sdl.5483.n7.nabble.com/Get-and-Put-Pixel-32-tp36541p36542.html
Sent from the SDL mailing list archive at Nabble.com.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Probably!On Wed, Sep 4, 2013 at 7:01 PM, Sik the hedgehog <sik.the.hedgehog at gmail.com wrote:

Sure you don’t mean pitch/4?

2013/9/4, Jonathan Dearborn <@Jonathan_Dearborn>:

Also, when calculating an index into surface->pixels, use surface->pitch
instead of surface->w. The pitch is the number of bytes in a scanline.
If
you’re using Uint32*, then I believe you would change surface->w to
surface->pitch/32:

pixels[ ( y * surface->pitch/32 ) + x ];

To make it more generic, you could use a Uint8* pointer into the pixels,
multiply your x by surface->format->BytesPerPixel, and use the bare
pitch:

pixels[ ( y * surface->pitch ) + x*surface->format->BytesPerPixel ];

Jonny D

On Fri, Aug 30, 2013 at 4:44 AM, Krofz wrote:

Sorry my Fault,

i set screen surface as format to 16 bit, the function need 32 bit.


View this message in context:
http://sdl.5483.n7.nabble.com/Get-and-Put-Pixel-32-tp36541p36542.html
Sent from the SDL mailing list archive at Nabble.com.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Thank you Jonathan and Sik.

it’s work using “pixels[ ( y * surface->pitch/4 ) + x ] = pixel;”

Could someone tell more about pitch?

i see my newSurface->pitch have 800 as value
width and height = 200 , it’s from ( 200 + 200 ) * 2 bytes?

from Wiki , pitch = the length of a row of pixels in bytes (read-only)–
View this message in context: http://sdl.5483.n7.nabble.com/Get-and-Put-Pixel-32-tp36541p36666.html
Sent from the SDL mailing list archive at Nabble.com.

Pitch is the distance in bytes between two lines in the bitmap.

The reason you’re supposed to use this value is that this distance may
not be the same as the length of all pixels put together. Sometimes
lines may have padding at the end, and you need to take this into
account. Using the pitch solves this issue.

Also, as a bonus using that value from the variable is going to be
faster than calculating it every time :stuck_out_tongue:

2013/9/5, Krofz :> Thank you Jonathan and Sik.

it’s work using “pixels[ ( y * surface->pitch/4 ) + x ] = pixel;”

Could someone tell more about pitch?

i see my newSurface->pitch have 800 as value
width and height = 200 , it’s from ( 200 + 200 ) * 2 bytes?

from Wiki , pitch = the length of a row of pixels in bytes (read-only)


View this message in context:
http://sdl.5483.n7.nabble.com/Get-and-Put-Pixel-32-tp36541p36666.html
Sent from the SDL mailing list archive at Nabble.com.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Yeah, as further clarification, the pitch is similar to the width, but more
specific to the actual storage used for your pixels. Say if you have a 24
bit (3 bytes per pixel) surface that is 11 pixels wide, one row of pixels
might technically only need 33 bytes of memory. If you happen to be
running on hardware that uses a 4-byte word, the actual memory might be
padded to make (a pitch of) 36 bytes for more efficient access. This is
especially important for SIMD operations. For reference:

Jonny DOn Thu, Sep 5, 2013 at 3:23 AM, Sik the hedgehog <sik.the.hedgehog at gmail.com wrote:

Pitch is the distance in bytes between two lines in the bitmap.

The reason you’re supposed to use this value is that this distance may
not be the same as the length of all pixels put together. Sometimes
lines may have padding at the end, and you need to take this into
account. Using the pitch solves this issue.

Also, as a bonus using that value from the variable is going to be
faster than calculating it every time :stuck_out_tongue:

2013/9/5, Krofz :

Thank you Jonathan and Sik.

it’s work using “pixels[ ( y * surface->pitch/4 ) + x ] = pixel;”

Could someone tell more about pitch?

i see my newSurface->pitch have 800 as value
width and height = 200 , it’s from ( 200 + 200 ) * 2 bytes?

from Wiki , pitch = the length of a row of pixels in bytes (read-only)


View this message in context:
http://sdl.5483.n7.nabble.com/Get-and-Put-Pixel-32-tp36541p36666.html
Sent from the SDL mailing list archive at Nabble.com.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

There is some putPixel code in SDL_gfx that you can use as a reference
as well:
http://sourceforge.net/p/sdlgfx/code/91/tree/SDL_gfxPrimitives.cOn 9/5/2013 6:36 AM, Jonathan Dearborn wrote:

Yeah, as further clarification, the pitch is similar to the width, but
more specific to the actual storage used for your pixels. Say if you
have a 24 bit (3 bytes per pixel) surface that is 11 pixels wide, one
row of pixels might technically only need 33 bytes of memory. If you
happen to be running on hardware that uses a 4-byte word, the actual
memory might be padded to make (a pitch of) 36 bytes for more
efficient access. This is especially important for SIMD operations.
For reference: http://en.wikipedia.org/wiki/Data_structure_alignment

Jonny D

On Thu, Sep 5, 2013 at 3:23 AM, Sik the hedgehog <sik.the.hedgehog at gmail.com <mailto:sik.the.hedgehog at gmail.com>> wrote:

Pitch is the distance in bytes between two lines in the bitmap.

The reason you're supposed to use this value is that this distance may
*not* be the same as the length of all pixels put together. Sometimes
lines may have padding at the end, and you need to take this into
account. Using the pitch solves this issue.

Also, as a bonus using that value from the variable is going to be
faster than calculating it every time :P

2013/9/5, Krofz <andriesuaktiwa at gmail.com
<mailto:andriesuaktiwa at gmail.com>>:
> Thank you Jonathan and Sik.
>
> it's work using  "pixels[ ( y * surface->pitch/4 ) + x ] = pixel;"
>
> Could someone tell more about pitch?
>
> i see my newSurface->pitch have 800 as value
> width and height = 200 , it's from ( 200 + 200 ) * 2 bytes?
>
> from Wiki , pitch = the length of a row of pixels in bytes
(read-only)
>
>
>
> --
> View this message in context:
>
http://sdl.5483.n7.nabble.com/Get-and-Put-Pixel-32-tp36541p36666.html
> Sent from the SDL mailing list archive at Nabble.com.
> _______________________________________________
> SDL mailing list
> SDL at lists.libsdl.org <mailto:SDL at lists.libsdl.org>
> http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
>
_______________________________________________
SDL mailing list
SDL at lists.libsdl.org <mailto:SDL at lists.libsdl.org>
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

@Sik : i like to see the ‘faster’ word :stuck_out_tongue:

@Jonathan : Thanks for the link ! :smiley:

@Andreas : SDL_gfx is nice ! , i like use it for drawing line and ellipse.
:)–
View this message in context: http://sdl.5483.n7.nabble.com/Get-and-Put-Pixel-32-tp36541p36726.html
Sent from the SDL mailing list archive at Nabble.com.

@Sik : i like to see the ‘faster’ word :stuck_out_tongue:

@Jonathan : Thanks for the link ! :smiley:

@Andreas : SDL_gfx is nice ! , i like use it for drawing line and ellipse.
:slight_smile:

*btw, i don’t know how to Reply LOL–
View this message in context: http://sdl.5483.n7.nabble.com/Get-and-Put-Pixel-32-tp36541p36727.html
Sent from the SDL mailing list archive at Nabble.com.