2012-02-28 148 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); 

你的蓝,绿,红的顺序文件不匹配,这可能导致你的红色在你的蓝调,并你的红色蓝调。