0

I can get the first pixel but how to get the others?

def get_alpha (x:uint8,y:uint8): uchar
        r:uchar
        g:uchar
        b:uchar
        a:uchar
        dire:uint32*
        r=0
        g=0
        b=0
        a=0
        Image.do_lock()
        dire=Image.pixels
        Image.format.get_rgba (*dire, ref r,ref g,ref b,ref a)
        Image.unlock()
        print "En x= %d y=%d ,%d %d %d %d",x,y,r,g,b,a
        return a

This is the c code of SDL Wiki for get a pixel. I need just for (3 and 4 bytes). I see that I can do surface.pixels+y *surface.pitch*surface.format.BytesPerPixel to go to the position but i have problems whit this. The first positions are good but the last positions of a all white surface gives me other colors. I think my measure is not good.

// Nota, código obtenido del wiki de SDL
// http://www.libsdl.org/cgi/docwiki.cgi/Pixel_20Access
Uint32 getpixel(SDL_Surface *surface, int x, int y)
{
    int bpp = surface->format->BytesPerPixel;
    /* Here p is the address to the pixel we want to retrieve */
    Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;

    switch(bpp) {
    case 1:
        return *p;

    case 2:
        return *(Uint16 *)p;

    case 3:
        if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
            return p[0] << 16 | p[1] << 8 | p[2];
        else
            return p[0] | p[1] << 8 | p[2] << 16;

    case 4:
        return *(Uint32 *)p;

    default:
        return 0;       /* shouldn't happen, but avoids warnings */
    }
} 
txasatonga
  • 419
  • 2
  • 11
  • Do you have a C example of what you're trying to do, or a description of the pixel formats? I'm not familiar enough with SDL to know how to address the raw pixel data. – nemequ Sep 07 '12 at 02:45

1 Answers1

1

I think I know the answer. I have learn something about pointers and without using get_rgba method I try to take them directly from memory. In my case I wont to get alpha pixel. When the variable BytesPerPixel gives me 1,2,or 3 i think there's not posible alpha channel. And when BytesPerPixel gives me 4 I take the forth byte for take alpha channels value. When the pixel size is 4 the structure is RGBA (Red,Green,Blue,Alpha).

def get_alpha (x:uint8,y:uint8): uchar
    dire:uint8*
    var alpha=0

    Control.Imagen_0.do_lock()
    dire=Control.Imagen_0.pixels // this is the first point of Image 
    dire+= (y*Control.Imagen_0.pitch)+ (x*Control.Imagen_0.format.BytesPerPixel) // this calculates the point x,y in memory

    Control.Imagen_0.unlock()
    case Control.Imagen_0.format.BytesPerPixel
        when 1,2,3
            alpha=255
        when 4
            dire+=+3  // go to alpha channel byte
            alpha=*dire // take from memory point
    return alpha 
txasatonga
  • 419
  • 2
  • 11
  • I want to say **Thanks to nemequ** because alwais is there trying to answer any question. Gracias Nemequ personas como tu aportan seguridad. – txasatonga Sep 07 '12 at 18:49