遇到SDL_GetRGBA问题

时间:2012-02-28 06:23:51

标签: sdl

我正在尝试使用SDL_GetRGBA来显示图像像素的RGBA值,但是我无法使其工作。这是我的代码和输出:

#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函数似乎返回了不正确的值。我不确定我做错了什么。

1 个答案:

答案 0 :(得分: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);

您的蓝绿红色订单与文档不符,这可能会导致您的红色处于蓝调状态,红色处于红色状态。