如何确定索引模式 SDL_Surface 是否具有透明度?
我已经得到了用于加载 SDL_Surface 并将其转换为 OpenGL 纹理的代码,但是我意识到它们仅适用于 RGB(A) 表面。我需要将支持扩展到索引模式图像(有或没有透明度)。
最初,我正在研究 ::SDL_SetColorKey()
,但它似乎只适用于 SDL blit。我已经阅读了 SDL_Surface
、SDL_PixelFormat
和 SDL_Color
,然后开始绘制以下内容(//# 是伪代码)
SDL_Surface *pSurf(::IMG_Load(image));
::SDL_LockSurface(pSurf);
Uint32 bytesPerPixel(pSurf->format->bytesPerPixel);
GLenum pixelFormat;
Uint8 *pixelData(pSurf->pixels);
bool allocated(false); // pixelData isn't allocated
if(pSurf->format->palette != 0) // indexed mode image
{
//# Determine transparency. // HOW?
//# bytesPerPixel = 3 or 4 depending on transparency being present;
//# pixelFormat = GL_RGB or GL_RGBA depending on bytesPerPixel;
Uint32 blockSize(pSurf->w * pSurf->h * bytesPerPixel);
pixelData = new Uint8[blockSize];
allocated = true;
//# traverse pSurf->pixels, look up pSurf->format->palette references and copy
// colors into pixelData;
}
else
{
//# Determine pixelFormat based on bytesPerPixel and pSurf->format->Rmask
// (GL_RGB(A) or GL_BGR(A)).
}
//# Pass bytesPerPixel, pixelFormat and pixelData to OpenGL (generate texture,
// set texture parameters, glTexImage2D etc).
if(allocated)
{
delete[] pixelData;
pixelData = 0;
}
::SDL_UnlockSurface(pSurf);
::SDL_FreeSurface(pSurf);
: ,问题是:如何确定传递给此例程的索引模式图像是否具有透明度?
I've got code I've been working with to load and convert SDL_Surface
s to OpenGL textures, however I've realized that they only work with RGB(A) surfaces. I need to extend the support onto indexed mode images (with or without transparency).
Initially, I was looking into ::SDL_SetColorKey()
, however it seems to only work with SDL blits. I've read up on SDL_Surface
, SDL_PixelFormat
and SDL_Color
, and then begun to sketch up the following (//# is pseudocode):
SDL_Surface *pSurf(::IMG_Load(image));
::SDL_LockSurface(pSurf);
Uint32 bytesPerPixel(pSurf->format->bytesPerPixel);
GLenum pixelFormat;
Uint8 *pixelData(pSurf->pixels);
bool allocated(false); // pixelData isn't allocated
if(pSurf->format->palette != 0) // indexed mode image
{
//# Determine transparency. // HOW?
//# bytesPerPixel = 3 or 4 depending on transparency being present;
//# pixelFormat = GL_RGB or GL_RGBA depending on bytesPerPixel;
Uint32 blockSize(pSurf->w * pSurf->h * bytesPerPixel);
pixelData = new Uint8[blockSize];
allocated = true;
//# traverse pSurf->pixels, look up pSurf->format->palette references and copy
// colors into pixelData;
}
else
{
//# Determine pixelFormat based on bytesPerPixel and pSurf->format->Rmask
// (GL_RGB(A) or GL_BGR(A)).
}
//# Pass bytesPerPixel, pixelFormat and pixelData to OpenGL (generate texture,
// set texture parameters, glTexImage2D etc).
if(allocated)
{
delete[] pixelData;
pixelData = 0;
}
::SDL_UnlockSurface(pSurf);
::SDL_FreeSurface(pSurf);
So, the question is: how can I determine if the indexed mode image I'm passing to this routine has transparency?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
索引模式的典型做法是拥有完整的 32 位 RGBA 调色板,因此每个索引颜色槽有 8 位 alpha。或者,您可以将某个(范围)调色板索引定义为透明。
OpenGL 支持后者,通过
glPixelMap()
。请参阅glPixelTransfer()
了解说明的翻译逻辑。The typical way it's done for indexed mode is either to have a full 32-bit RGBA palette, so you have 8 bits of alpha per indexed color slot. Or, you can just define a certain (range of) palette index as being transparent.
OpenGL supports the latter, through the
GL_PIXEL_MAP_I_TO_A
table accessed throughglPixelMap()
. SeeglPixelTransfer()
for a description of the translation logic.