像素在 SDL2 中的排列方式

How are pixels arranged in SDL2?

本文关键字:排列 方式 SDL2 像素      更新时间:2023-10-16

我有 2x2 图像

像素不应该这样排列吗?

1 2 // each number is a pixel
3 4

我在访问带有 x 和 y 的像素时遇到问题,因为当 x = 1 且 y = 0 时,我得到索引 2,但打印像素 4 的 RGB 值

所以它是像什么?

1 2 // each number is a pixel
4 3

这是我使用的代码

index = y + x * s->w;
c = s->format->palette->colors[index]; // c is an SDL_Color and s is an SDL_Surface*

我也使用它进行循环,并且仍然打印相同的

for (Uint8 i = *(Uint8 *)s->pixels; i < s->w*s->h; i++) {
    c = s->format->palette->colors[i];
    printf("%u %u %u %u n", i, c.r , c.g , c.b);
}

SDL_Surface SDL 文档中的结构定义

typedef struct SDL_Surface {
    Uint32 flags;                           /* Read-only */
    SDL_PixelFormat *format;                /* Read-only */
    int w, h;                               /* Read-only */
    Uint16 pitch;                           /* Read-only */
    void *pixels;                           /* Read-write */
    /* clipping information */
    SDL_Rect clip_rect;                     /* Read-only */
    /* Reference count -- used when freeing surface */
    int refcount;                           /* Read-mostly */
/* This structure also contains private fields not shown here */
} SDL_Surface;
Uint8 *pixel = (Uint8 *)s->pixels,*index;
index = &pixel[y + x * s->pitch];
c = s->format->palette->colors[*index];

使用@holyblackcat的建议让它做。