2012-09-06 45 views
0

我可以得到第一個像素,但是如何獲得其他像素?用Vala /精靈獲得SDL表面的像素顏色

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 

這是SDL維基的C代碼得到一個像素。我只需要(3和4字節)。我看到我可以做surface.pixels + y * surface.pitch * surface.format.BytesPerPixel去到位置,但我有問題。第一個位置很好,但全白色表面的最後位置給了我其他顏色。我認爲我的措施不好。

// 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 */ 
    } 
} 
+0

你有C的例子,你想要做什麼,或像素格式的描述?我不熟悉SDL知道如何處理原始像素數據。 – nemequ

回答

1

我想我知道答案。我已經學習了一些關於指針的知識,並且不使用get_rgba方法,我試圖直接從內存中獲取它們。在我的情況下,我不會得到alpha像素。當變量BytesPerPixel給我1,2或3時,我認爲沒有可能的alpha通道。當BytesPerPixel給我4的時候,我把第四個字節取爲alpha通道值。當像素大小爲4時,結構爲RGBA(紅色,綠色,藍色,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 
+0

我想說**感謝nemequ **,因爲alwais有試圖回答任何問題。 Gracias Nemequ角色como tu aportan seguridad。 – txasatonga