【发布时间】:2018-01-02 17:30:17
【问题描述】:
我有 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 循环并且仍然打印相同的内容
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 文档中的 SDL_Surface 结构定义
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;
【问题讨论】:
-
我已经编辑了代码
-
您应该使用
pixels访问像素,colors看起来像一个颜色表。 -
文档说,“因此,要确定 8 位表面中像素的颜色:我们从表面->像素读取颜色索引,然后使用该索引从表面->格式->调色板->颜色。"
-
您的报价就是答案。您应该从
->pixels获取索引,而不是计算它。 -
'for (Uint8 i = *(Uint8 *)s->pixels; i w * s->h; i++) { c = s->format->palette->颜色[i]; printf("%u %u %u %u \n", i, c.r , c.g , c.b); }' 打印相同