提问人:ibe 提问时间:4/7/2016 更新时间:4/8/2016 访问量:745
识别 SDL 中的像素数据格式
Identify pixel data format in SDL
问:
在OS X上运行,我使用库在OpenGL中加载了一个纹理(使用返回)。似乎颜色通道已被交换,即我必须在 中设置为像素格式参数。SDL_Image
IMG_LOAD()
SDL_Surface*
GL_BGRA
glTexImage2D()
有没有办法确定正确的数据格式(BGRA或RGBA等),而不仅仅是简单地编译和检查纹理?SDL 交换这些颜色通道的原因是什么?
答:
1赞
Andreas
4/8/2016
#1
是的。以下链接包含如何确定每个组件的通道偏移的代码示例: http://wiki.libsdl.org/SDL_PixelFormat#Code_Examples
从网站:
SDL_PixelFormat *fmt;
SDL_Surface *surface;
Uint32 temp, pixel;
Uint8 red, green, blue, alpha;
.
.
.
fmt = surface->format;
SDL_LockSurface(surface);
pixel = *((Uint32*)surface->pixels);
SDL_UnlockSurface(surface);
/* Get Red component */
temp = pixel & fmt->Rmask; /* Isolate red component */
temp = temp >> fmt->Rshift; /* Shift it down to 8-bit */
temp = temp << fmt->Rloss; /* Expand to a full 8-bit number */
red = (Uint8)temp;
您应该能够按值对 Xmasks 进行排序。然后,您可以确定它是 RGBA 还是 BGRA。如果 Xmask == 0,则颜色通道不存在。
我不知道为什么会发生掉期。
编辑:从Xshift更改为Xmask,因为后者可用于确定颜色通道的位置和存在。
评论
1赞
Andon M. Coleman
4/8/2016
“我不知道为什么会发生互换。”字节顺序很有可能。不知道这里涉及什么版本的 OS X,甚至不知道涉及什么处理器架构,但这是该平台上比其他平台上更常见的问题。
评论