【问题标题】:SDL accessing pixel data of SDL_SurfaceSDL 访问 SDL_Surface 的像素数据
【发布时间】:2018-11-28 00:55:01
【问题描述】:

我想控制加载图像的颜色,但在尝试备份像素数据时遇到了问题。我的代码如下所示:

Uint32* pixels, oriPixels;
SDL_Surface* image;

void BackupPixelData()
{
    pixels = (Uint32*)image->pixels;
    oriPixels = new Uint32[image->w * image->h];
    for (int i = 0; i < image->w * image->h; i++)
    {
        oriPixels[i] = pixels[i]; //This causes an access violation midway through
        *(oriPixels + i) = *(pixels + i); //Using this method does not cause any crash, but the image will have artifacts
    }
}

我可以通过将 oriPixels 更改为 Uint32 的向量来使代码工作,并且我没有遇到任何问题(可以使用 oriPixels 将图像恢复为原始颜色)。

如何正确加载像素数据?

图像是 32 位的。

【问题讨论】:

    标签: c++ sdl-2


    【解决方案1】:

    您必须考虑数据对齐。根据表面格式,可以填充行。 查看有关 SDL_Surface 的音高场的文档以获取更多详细信息https://wiki.libsdl.org/SDL_Surface

    因为内存缓冲区大小不是宽度 * 高度,而是实际间距 * 高度,所以您会遇到访问冲突。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-22
      • 2015-08-09
      • 2011-04-11
      • 2023-04-08
      • 1970-01-01
      相关资源
      最近更新 更多