从 SDL_Surface 到 QPixmap 的高效转换 [英] Efficient conversion from SDL_Surface to QPixmap
问题描述
我需要提取像素信息并填充要在我的 Qt 程序中使用的 QPixmap.
I need to extract pixel information and populate a QPixmap to be used in my Qt program.
我目前分两步执行此操作.
I am currently doing this in a 2-step process.
- SDL_Surface 到 Windows .bmp 使用
SDL_SaveBMP()
- .bmp 到 QImage
- QImage 到 QPixmap
我将中间 .bmp 存储在内存中.但总的来说,我对这种方法并不满意,因为它涉及与 .bmp 相关的文件格式转换开销
I am storing the intermediate .bmp in memory. But overall, I am not happy with this approach because it involves file-format conversion overheads related to .bmp
有更好的建议吗?
分享最终的工作代码,来自 jrok 的回答加上尝试像素格式
Sharing the final working code, from jrok's answer plus experimenting with pixel formats
SDL_Surface *screen = ... /* Whatever surface you want to copy from */
SDL_Surface *surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
screen->w, screen->h,
24, rmask, gmask, bmask, amask);
SDL_BlitSurface(screen,NULL, surface,NULL);
QImage img(static_cast<uchar*>(surface->pixels),
surface->w, surface->h, QImage::Format_RGB888);
推荐答案
QImage
构造函数 获取原始图像数据.您可以从 SDL_Surface 结构中传递 pixels
指针:
SDL_Surface* surf = /* get surface */
QImage img(static_cast<uchar*>(surf->pixels), surf->w, surf->h, QImage::Format_RGB32);
最后一个参数将取决于您的 SDL_Surface 的 SDL_PixelFormat.
The last parameter will depend on the SDL_PixelFormat of your SDL_Surface.
然后你可以简单地从 QImage
制作 QPixmap
:
Then you can simply make QPixmap
from QImage
:
QPixmap = QPixmap::fromImage(img);
这篇关于从 SDL_Surface 到 QPixmap 的高效转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!