SDL2 中的灰度图像 [英] Greyscale image in SDL2

查看:144
本文介绍了SDL2 中的灰度图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 uint8_t 数组,它代表一张灰度图片,其中每个像素是一个 uint8_t.我想使用 SDL2 库在窗口中显示它.

I have an array of uint8_t which represents a greyscale picture, where each pixel is one uint8_t. I would like to display this in a window using the SDL2 library.

我尝试通过执行以下操作从数组创建 SDL_Surface

I have tried to create an SDL_Surface from the array by doing

mSurface = SDL_CreateRGBSurfaceFrom(mData, mWidth, mHeight, 8, mWidth, 0xFF0000, 0xFF0000, 0xFF0000, 0xFF0000);

然而,问题在于,根据 SDL2 wiki "如果深度为 4 位或 8 位,则为表面分配一个空调色板".如果不是这样,那么我将能够告诉 SDL 每个像素是一个字节,并将该字节用于 R、G 和 B 值.

However, the problem is that when a depth of 8 bits is passed to SDL_CreateRGBSurfaceFrom (as I have done here), according to the SDL2 wiki "If depth is 4 or 8 bits, an empty palette is allocated for the surface" . If it wasn't for that, then I would be able to tell SDL that each pixel is one byte, and to use that byte for the R, G, and B values.

我想要每像素 8 位的深度,因为我的数据就是这样存储的,但我不想使用调色板.

I want a depth of 8 bits per pixel because thats how my data is stored, but I don't want to use a pallete.

有什么方法可以让 SDL 不假设我想要一个调色板,而只显示 r、g 和 b 掩码都设置为该字节的图像?

Is there any way to make SDL not assume I want a pallete, and just display the image with the r, g, and b masks all set to that byte?

我知道另一种解决方案是通过将每个字节复制三遍,将我的灰度图像转换为 RGB,然后显示它.但是,如果可能的话,我想避免这样做,因为所有复制都会很慢.

I understand that an alternative solution would be to convert my greyscale image into RGB by copying each byte three times, and then to display it. However, I would like to avoid doing that if possible because all that copying would be slow.

推荐答案

SDL_CreateRGBSurfaceFrom() 不处理 8 位真彩色格式.正如您所指出的,它为 8 位深度创建了一个空白调色板.最明显的做法是填充调色板,让它做它自己的事.

SDL_CreateRGBSurfaceFrom() does not handle 8-bit true color formats. As you noted, it creates a blank palette for 8-bit depths. The most obvious thing to do is to fill in the palette and just let it do its thing.

这是灰度调色板的一些代码:

Here's some code for a grayscale palette:

SDL_Color colors[256];
int i;

for(i = 0; i < 256; i++)
{
    colors[i].r = colors[i].g = colors[i].b = i;
}

SDL_SetPaletteColors(mSurface->format->palette, colors, 0, 256);

另外,一个经验法则:永远不要避免仅仅为了慢"而起作用的东西.避免太慢"的事情.您可能只有通过尝试才能知道什么时候太慢"了.

Also, a rule of thumb: Never avoid something that works just for being "slow". Do avoid things that are "too slow". You might only know when something is "too slow" by trying it out.

在这种情况下,您可能只加载此图像一次,然后您会体验到可以忽略不计的性能影响.

In this case, you might only be loading this image once and then after that you experience a negligible performance effect.

这篇关于SDL2 中的灰度图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆