使用GL_RGB格式获取Mac OS屏幕

时间:2010-01-25 12:33:57

标签: c++ macos opengl

我正在使用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 个答案:

答案 0 :(得分:2)

我不是Mac家伙,但是如果你能获得RGBA数据并且想要XRGB,那么你不能将每个像素按位移位8位吗?

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

答案 1 :(得分: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。

答案 2 :(得分:0)

当我使用GL_BGRA时,数据会被预先调整返回,这是因为当我在窗口中显示结果时,颜色看起来是正确的。

如果您想要我创建的项目,请与我联系。希望这会有所帮助。