2012-09-06 1 views
0

첫 번째 픽셀을 얻을 수 있지만 다른 픽셀을 얻는 방법은 무엇입니까?Vala/Genie를 사용하여 SDL Surface의 픽셀 색상을 얻으십시오.

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 Wiki의 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

수행하려는 내용이나 픽셀 형식에 대한 설명이 있습니까? 원시 픽셀 데이터를 처리하는 방법을 알기에는 SDL에 익숙하지 않습니다. – nemequ

답변

1

나는 답을 알고 있다고 생각합니다. 포인터에 대해 배웠고 get_rgba 메서드를 사용하지 않고 메모리에서 직접 가져 오려고합니다. 제 경우에는 알파 픽셀을 얻지 못할 것입니다. BytesPerPixel 변수가 1,2, 또는 3을 제공 할 때 알기 쉬운 알파 채널이 없다고 생각합니다. 그리고 BytesPerPixel이 나에게 4를 줄 때 나는 알파 채널 값을 취하기 위해 네 번째 바이트를 취합니다. 픽셀 크기가 4 인 경우 구조는 RGBA (빨강, 녹색, 파랑, 알파)입니다.

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

알리가 어떤 질문에도 대답하려고하기 때문에 ** 네메와에게 감사드립니다 **. Gracias Nemequ 개인 메모를 추가하십시오. – txasatonga

관련 문제