我可以得到第一個像素,但是如何獲得其他像素?用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 */
}
}
你有C的例子,你想要做什麼,或像素格式的描述?我不熟悉SDL知道如何處理原始像素數據。 – nemequ