Efficient conversion from SDL_Surface to QPixmap

余生长醉 提交于 2019-12-22 18:48:25

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!