2012-02-28 188 views
0

我試圖使用SDL_GetRGBA來顯示圖像像素的RGBA值,但是我無法讓它工作。這裏是我的代碼和輸出:SDL_GetRGBA遇到問題

#include <stdio.h> 
#include <SDL/SDL.h> 

int main() { 
    SDL_Surface *screen; 
    SDL_Surface *image; 

    SDL_Init(SDL_INIT_VIDEO); 

    screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE); 
    image = SDL_LoadBMP("testimg2.bmp"); 

    Uint32 i,j; 
    Uint8 r, g, b, a; 
    Uint8 *p = (Uint8 *) image->pixels; 
     for (i=0; i < 300; i++) { 

      for (j=0; j < 4; j++) 
       printf("i = %u, image->pixels[i*4+%u] = %u\n", i, j, p[i*4+j]); 

     SDL_GetRGBA(i, image->format, &b, &g, &r, &a); 
     printf("i = %u, r = %u, g = %u, b = %u, a = %u\n", i, r, g, b, a); 
    } 

    SDL_BlitSurface(image, NULL, screen, NULL); 
    SDL_Flip(screen); 
    SDL_Delay(2000); 

    SDL_FreeSurface(image); 
    SDL_Quit(); 
    return 0; 
} 

i = 0, image->pixels[i*4+0] = 0 
i = 0, image->pixels[i*4+1] = 0 
i = 0, image->pixels[i*4+2] = 255 
i = 0, image->pixels[i*4+3] = 133 
i = 0, r = 0, g = 0, b = 0, a = 255 
i = 1, image->pixels[i*4+0] = 0 
i = 1, image->pixels[i*4+1] = 0 
i = 1, image->pixels[i*4+2] = 255 
i = 1, image->pixels[i*4+3] = 140 
i = 1, r = 1, g = 0, b = 0, a = 255 
i = 2, image->pixels[i*4+0] = 0 
i = 2, image->pixels[i*4+1] = 0 
i = 2, image->pixels[i*4+2] = 255 
i = 2, image->pixels[i*4+3] = 132 
i = 2, r = 2, g = 0, b = 0, a = 255 
i = 3, image->pixels[i*4+0] = 0 
i = 3, image->pixels[i*4+1] = 0 
i = 3, image->pixels[i*4+2] = 255 
i = 3, image->pixels[i*4+3] = 118 
i = 3, r = 3, g = 0, b = 0, a = 255 
i = 4, image->pixels[i*4+0] = 0 
i = 4, image->pixels[i*4+1] = 0 
i = 4, image->pixels[i*4+2] = 255 
i = 4, image->pixels[i*4+3] = 114 
i = 4, r = 4, g = 0, b = 0, a = 255 
i = 5, image->pixels[i*4+0] = 0 
i = 5, image->pixels[i*4+1] = 0 
i = 5, image->pixels[i*4+2] = 255 
i = 5, image->pixels[i*4+3] = 115 
i = 5, r = 5, g = 0, b = 0, a = 255 

所以一切都似乎是正確的手動經歷的像素陣列逐字節的時候,但SDL_GetRGBA功能似乎返回不正確的值。我不確定我做錯了什麼。

回答

0

從SDL:

void SDL_GetRGBA(Uint32 pixel, SDL_PixelFormat *fmt, Uint8 *r, Uint8 *g, Uint8 *b, Uint8 *a); 

您有:

SDL_GetRGBA(i, image->format, &b, &g, &r, &a); 

你的藍,綠,紅的順序文件不匹配,這可能導致你的紅色在你的藍調,並你的紅色藍調。