使用GL_RGB格式捕获Mac OS屏幕

4
我正在使用glgrab代码尝试抓取Mac屏幕的全屏截图。然而,我希望位图数据以GL_RGB格式呈现。也就是说,每个像素的格式应该是:
0x00RRGGBB
原始代码指定了GL_BGRA格式。但是,将其更改为GL_RGB会给我一个完全空白的结果。我正在使用的完整源代码是:
CGImageRef grabViaOpenGL(CGDirectDisplayID display, CGRect srcRect)
{
    CGContextRef bitmap;
    CGImageRef image;
    void * data;
    long bytewidth;
    GLint width, height;
    long bytes;
    CGColorSpaceRef cSpace = CGColorSpaceCreateWithName (kCGColorSpaceGenericRGB);

    CGLContextObj    glContextObj;
    CGLPixelFormatObj pixelFormatObj ;
    GLint            numPixelFormats ;
    //CGLPixelFormatAttribute
    int attribs[] =
    {
//      kCGLPFAClosestPolicy,
        kCGLPFAFullScreen,
        kCGLPFADisplayMask,
        NULL,    /* Display mask bit goes here */
        kCGLPFAColorSize, 24,
        kCGLPFAAlphaSize, 0,
        kCGLPFADepthSize, 32,
        kCGLPFASupersample,
        NULL
    } ;


    if ( display == kCGNullDirectDisplay )
        display = CGMainDisplayID();
    attribs[2] = CGDisplayIDToOpenGLDisplayMask(display);


    /* Build a full-screen GL context */
    CGLChoosePixelFormat( (CGLPixelFormatAttribute*) attribs, &pixelFormatObj, &numPixelFormats );
    if ( pixelFormatObj == NULL )    // No full screen context support
    {
        // GL didn't find any suitable pixel formats. Try again without the supersample bit:
        attribs[10] = NULL;
        CGLChoosePixelFormat( (CGLPixelFormatAttribute*) attribs, &pixelFormatObj, &numPixelFormats );
        if (pixelFormatObj == NULL)
        {
            qDebug("Unable to find an openGL pixel format that meets constraints");
            return NULL;
        }
    }
    CGLCreateContext( pixelFormatObj, NULL, &glContextObj ) ;
    CGLDestroyPixelFormat( pixelFormatObj ) ;
    if ( glContextObj == NULL )
    {
        qDebug("Unable to create OpenGL context");
        return NULL;
    }


    CGLSetCurrentContext( glContextObj ) ;
    CGLSetFullScreen( glContextObj ) ;


    glReadBuffer(GL_FRONT);


    width = srcRect.size.width;
    height = srcRect.size.height;


    bytewidth = width * 4; // Assume 4 bytes/pixel for now
    bytewidth = (bytewidth + 3) & ~3; // Align to 4 bytes
    bytes = bytewidth * height; // width * height

    /* Build bitmap context */
    data = malloc(height * bytewidth);
    if ( data == NULL )
    {
        CGLSetCurrentContext( NULL );
        CGLClearDrawable( glContextObj ); // disassociate from full screen
        CGLDestroyContext( glContextObj ); // and destroy the context
        qDebug("OpenGL drawable clear failed");
        return NULL;
    }
    bitmap = CGBitmapContextCreate(data, width, height, 8, bytewidth,
                                   cSpace, kCGImageAlphaNoneSkipFirst /* XRGB */);
    CFRelease(cSpace);


    /* Read framebuffer into our bitmap */
    glFinish(); /* Finish all OpenGL commands */
    glPixelStorei(GL_PACK_ALIGNMENT, 4); /* Force 4-byte alignment */
    glPixelStorei(GL_PACK_ROW_LENGTH, 0);
    glPixelStorei(GL_PACK_SKIP_ROWS, 0);
    glPixelStorei(GL_PACK_SKIP_PIXELS, 0);

    /*
     * Fetch the data in XRGB format, matching the bitmap context.
     */
    glReadPixels((GLint)srcRect.origin.x, (GLint)srcRect.origin.y, width, height,
                 GL_RGB,
#ifdef __BIG_ENDIAN__
                 GL_UNSIGNED_INT_8_8_8_8_REV, // for PPC
#else
                 GL_UNSIGNED_INT_8_8_8_8, // for Intel! http://lists.apple.com/archives/quartz-dev/2006/May/msg00100.html
#endif
                 data);
    /*
     * glReadPixels generates a quadrant I raster, with origin in the lower left
     * This isn't a problem for signal processing routines such as compressors,
     * as they can simply use a negative 'advance' to move between scanlines.
     * CGImageRef and CGBitmapContext assume a quadrant III raster, though, so we need to
     * invert it. Pixel reformatting can also be done here.
     */
    swizzleBitmap(data, bytewidth, height);


    /* Make an image out of our bitmap; does a cheap vm_copy of the bitmap */
    image = CGBitmapContextCreateImage(bitmap);

    /* Get rid of bitmap */
    CFRelease(bitmap);
    free(data);


    /* Get rid of GL context */
    CGLSetCurrentContext( NULL );
    CGLClearDrawable( glContextObj ); // disassociate from full screen
    CGLDestroyContext( glContextObj ); // and destroy the context

    /* Returned image has a reference count of 1 */
    return image;
}

我完全不懂OpenGL,所以希望能得到一些指导。谢谢!

更新:

经过一些实验,我已经缩小了我的问题范围。我的问题是,虽然我不需要alpha分量,但我希望每个像素都被打包到4字节的边界上。现在,当我在glReadPixels调用中指定GL_RGB或GL_BGR格式时,我得到的位图数据被打包成3字节块。当我指定GL_RGBAGL_BGRA时,我得到四字节块,但总是最后一个alpha分量。

然后我尝试改变传递给

bitmap = CGBitmapContextCreate(data, width, height, 8, bytewidth,cSpace, kCGImageAlphaNoneSkipFirst /* XRGB */);

的值,然而,任何AlphaNoneSkipFirstAlphaNoneSkipLast的变化都不能将alpha通道放在像素字节块的开始位置。

有什么想法吗?

3个回答

2

我不是Mac用户,但如果你可以获取RGBA数据并需要XRGB,是否可以通过将每个像素向下位移八个位来实现?

foreach( unsigned int* RGBA_pixel, pixbuf )
{
    (*RGBA_pixel) = (*RGBA_pixel) >> 8;
}

是的,但这样肯定会非常慢吧?如果屏幕数据是1440x900,那就有1296000个像素,每个都必须单独移动。一个更聪明和恶意的黑客技巧是在像素数组的开头添加一个字节,并将所有内容视为XRGB... - Thomi
不是真的。根据草稿估算,假设单周期位移,应该在2GHz CPU上花费不到1毫秒的时间。 - genpfault

1

尝试使用GL_UNSIGNED_BYTE而不是GL_UNSIGNED_INT_8_8_8_8_REV/GL_UNSIGNED_INT_8_8_8_8

虽然看起来你想要的是GL_RGBA,那么它应该可以与8_8_8_8_REV或8_8_8_8一起使用。


0

当我使用GL_BGRA时,数据被预先交换了位置,这一点已经得到确认,因为当我在窗口中显示结果时,颜色看起来是正确的。

如果您想要我创建的项目,请与我联系。希望这可以帮助到您。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接