【问题标题】:How do I determine if an indexed mode SDL_Surface has transparency or not?如何确定索引模式 SDL_Surface 是否具有透明度?
【发布时间】:2009-12-04 13:10:44
【问题描述】:

我一直在使用代码来加载 SDL_Surfaces 并将其转换为 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);

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

【问题讨论】:

    标签: c++ transparency sdl indexed-image


    【解决方案1】:

    为索引模式完成的典型方法是使用完整的 32 位 RGBA 调色板,因此每个索引颜色槽有 8 位 alpha。或者,您可以将某个(范围)调色板索引定义为透明的。

    OpenGL 支持后者,通过@987654321@ 访问的GL_PIXEL_MAP_I_TO_A 表。翻译逻辑的描述见@987654322@

    【讨论】:

    • 有什么方法可以直接从图像文件(.gif、.ico)创建的 SDL_Surface 结构中推断出透明度? (比如背景色、颜色键、调色板中的第n个颜色……?)
    猜你喜欢
    • 2013-01-24
    • 2014-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-07
    • 1970-01-01
    • 2018-07-07
    • 1970-01-01
    相关资源
    最近更新 更多