c++ - 如何确定索引模式 SDL_Surface 是否具有透明度?

标签 c++ transparency sdl indexed-image

我有我一直在使用的代码来加载 SDL_Surface 并将其转换为 OpenGL 纹理,但是我意识到它们只适用于 RGB(A) 表面。我需要将支持扩展到索引模式图像(有或没有透明度)。

最初,我正在研究 ::SDL_SetColorKey(),但它似乎只适用于 SDL blits。我已经阅读了 SDL_SurfaceSDL_PixelFormatSDL_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);

因此,问题是:如何确定传递给此例程的索引模式图像是否具有透明度?

最佳答案

索引模式的典型做法是拥有一个完整的 32 位 RGBA 调色板,这样每个索引颜色槽就有 8 位 alpha。或者,您可以只将某个(范围的)调色板索引定义为透明。

OpenGL 支持后者,通过 GL_PIXEL_MAP_I_TO_A通过 <a href="http://www.talisman.org/opengl-1.1/Reference/glPixelMap.html" rel="noreferrer noopener nofollow">glPixelMap()</a> 访问的表.参见 <a href="http://www.talisman.org/opengl-1.1/Reference/glPixelTransfer.html" rel="noreferrer noopener nofollow">glPixelTransfer()</a>用于翻译逻辑的描述。

关于c++ - 如何确定索引模式 SDL_Surface 是否具有透明度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1846856/

相关文章:

c++ - 从c++中的 vector 生成随机数

css - 使类(class)的背景颜色透明?

c++ - 无法删除 SDL Poll Events 中的多个元素

c - 使用 C 中的 SDL 2.0,如何使矩形淡入淡出?

c++ - TEXTUREACCESSes 的解释?

c++ - 从IP地址查找MAC地址

c++ - 为什么这个模拟中的摩擦力会使物体以不稳定的方式运行?

c++ - 将不同的变量分配给来自 cin C++ 的输入

Python:为什么 plot_surface 是透明的?

css - 透明图像未出现在菜单中