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);
One of
QImageconstructors takes raw image data. You can pass itpixelspointer from SDL_Surface structure:The last parameter will depend on the SDL_PixelFormat of your SDL_Surface.
Then you can simply make
QPixmapfromQImage:QPixmap::fromImage() reference