问题
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 to Windows .bmp using
SDL_SaveBMP()
- .bmp to QImage
- QImage to QPixmap
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
Any better suggestion?
Edit: 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);
回答1:
One of QImage constructors takes raw image data. You can pass it pixels
pointer from SDL_Surface structure:
SDL_Surface* surf = /* get surface */
QImage img(static_cast<uchar*>(surf->pixels), surf->w, surf->h, QImage::Format_RGB32);
The last parameter will depend on the SDL_PixelFormat of your SDL_Surface.
Then you can simply make QPixmap
from QImage
:
QPixmap = QPixmap::fromImage(img);
QPixmap::fromImage() reference
来源:https://stackoverflow.com/questions/11261275/efficient-conversion-from-sdl-surface-to-qpixmap