Как мне получить изображение с моего EAGLLayer? - PullRequest
3 голосов
/ 24 ноября 2008

Я ищу способ получить содержимое моего opengl (как UIImage) и затем сохранить его в файл. Сейчас я делаю попытку glReadPixels, хотя я не уверен, что правильно делаю с тем, какой тип malloc мне следует делать. Я понимаю, что на OSX это GL_BGRA, но на iPhone это не работает ...

Ответы [ 3 ]

2 голосов
/ 24 ноября 2008

Все совместимые с OpenGL | ES GL-реализации должны поддерживать GL_RGBA в качестве параметра для glReadPixels.

Если ваш OpenGL | Es поддерживает

GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES 
Расширение

вы также можете запросить собственный формат. glReadPixels будет понимать этот формат как параметр для glReadPixels и позволяет напрямую получать данные пикселей в собственном формате. Обычно это немного быстрее, чем GL_RGBA.

Взгляните на заголовочный файл или запросите поддержку расширения через glGetString.

1 голос
/ 15 апреля 2011

Посмотрите «Технические вопросы и ответы QA1704 OpenGL ES View Snapshot», где Apple приводит работающий пример, в том числе «лучший способ» Нилса развернуть изображение. Он возвращает вас к вашему UIImage, и оттуда следует Nils для сохранения в виде файла.

1 голос
/ 26 июля 2010

Получить данные из представления OpenGL ES:

-(UIImage *) snapshot {
    NSInteger myDataLength = backingWidth * backingHeight * 4;
    // allocate array and read pixels into it.
    GLubyte *buffer = (GLubyte *) malloc(myDataLength);
    glReadPixels(0, 0, backingWidth, backingHeight, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
    // gl renders "upside down" so swap top to bottom into new array.
    // there's gotta be a better way, but this works.
    GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);
    for(int y = 0; y <backingHeight; y++)
    {
        for(int x = 0; x <backingWidth * 4; x++)
        {
            buffer2[((backingHeight - 1) - y) * backingWidth * 4 + x] = buffer[y * 4 * backingWidth + x];
        }
    }
    free(buffer);
    // make data provider with data.
    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL);
    // prep the ingredients
    int bitsPerComponent = 8;
    int bitsPerPixel = 32;
    int bytesPerRow = 4 * backingWidth;
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
    CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
    // make the cgimage
    CGImageRef imageRef = CGImageCreate(backingWidth, backingHeight, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
    // then make the uiimage from that
    UIImage *myImage = [UIImage imageWithCGImage:imageRef];
    return myImage;
}

Затем сохранить изображение:

-(void) saveImage:(UIImage *)image {}
    // Create paths to output images
    NSString  *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.png"];
    NSString  *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.jpg"];

    // Write a UIImage to JPEG with minimum compression (best quality)
    // The value 'image' must be a UIImage object
    // The value '1.0' represents image compression quality as value from 0.0 to 1.0
    [UIImageJPEGRepresentation(image, 1.0) writeToFile:jpgPath atomically:YES];

    // Write image to PNG
    [UIImagePNGRepresentation(image) writeToFile:pngPath atomically:YES];
}
...