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

发布于 2024-08-13 02:25:39 字数 1416 浏览 9 评论 0原文

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

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

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

I've got code I've been working with to load and convert SDL_Surfaces 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若无相欠,怎会相见 2024-08-20 02:25:39

索引模式的典型做法是拥有完整的 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 through glPixelMap(). See glPixelTransfer() for a description of the translation logic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文