Q_GUI_EXPORT QImage qt_gl_read_framebuffer(const QSize &size, bool alpha_format, bool include_alpha)
{
    int w = size.width();
    int h = size.height();

    QOpenGLFunctions *funcs = QOpenGLContext::currentContext()->functions();
    while (funcs->glGetError());

#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
    QImage img(size, (alpha_format && include_alpha) ? QImage::Format_ARGB32_Premultiplied
                                                     : QImage::Format_RGB32);
#ifdef QT_OPENGL_ES
    GLint fmt = GL_BGRA_EXT;
#else
    GLint fmt = GL_BGRA;
#endif
    funcs->glReadPixels(0, 0, w, h, fmt, GL_UNSIGNED_BYTE, img.bits());
    if (!funcs->glGetError())
        return img.mirrored();
#endif

    QImage rgbaImage(size, (alpha_format && include_alpha) ? QImage::Format_RGBA8888_Premultiplied
                                                           : QImage::Format_RGBX8888);
    funcs->glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, rgbaImage.bits());
    return rgbaImage.mirrored();
}
Пример #2
0
QImage gl_read_framebuffer(const QRect &rect) {
    QSize size = rect.size();
    int x = rect.x();
    int y = rect.y();

    while (glGetError());

    QImage img(size, QImage::Format_RGB32);
    GLint fmt = GL_BGRA_EXT;
    glReadPixels(x, y, size.width(), size.height(), fmt, GL_UNSIGNED_BYTE, img.bits());
    if (!glGetError())
        return img.mirrored();

    QImage rgbaImage(size, QImage::Format_RGBX8888);
    glReadPixels(x, y, size.width(), size.height(), GL_RGBA, GL_UNSIGNED_BYTE, rgbaImage.bits());
    return rgbaImage.mirrored();
}