1

I am using this to make video of my application

I currently use

glReadPixels(x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data);

to read the frame buffer and then use

-(UIImage *) createARGBImageFromRGBAImage: (UIImage*)image
{
    CGSize dimensions = [image size];

    NSUInteger bytesPerPixel = 4;
    NSUInteger bytesPerRow = bytesPerPixel * dimensions.width;
    NSUInteger bitsPerComponent = 8;

    unsigned char *rgba = malloc(bytesPerPixel * dimensions.width * dimensions.height);
    unsigned char *argb = malloc(bytesPerPixel * dimensions.width * dimensions.height);

    CGColorSpaceRef colorSpace = NULL;
    CGContextRef context = NULL;

    colorSpace = CGColorSpaceCreateDeviceRGB();
    context = CGBitmapContextCreate(rgba, dimensions.width, dimensions.height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrderDefault); // kCGBitmapByteOrder32Big
    CGContextDrawImage(context, CGRectMake(0, 0, dimensions.width, dimensions.height), [image CGImage]);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);

    for (int x = 0; x < dimensions.width; x++) {
        for (int y = 0; y < dimensions.height; y++) {
            NSUInteger offset = ((dimensions.width * y) + x) * bytesPerPixel;
            argb[offset + 0] = rgba[offset + 3];
            argb[offset + 1] = rgba[offset + 0];
            argb[offset + 2] = rgba[offset + 1];
            argb[offset + 3] = rgba[offset + 2];
        }
    }

    colorSpace = CGColorSpaceCreateDeviceRGB();
    context = CGBitmapContextCreate(argb, dimensions.width, dimensions.height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrderDefault); // kCGBitmapByteOrder32Big
    CGImageRef imageRef = CGBitmapContextCreateImage(context);
    image = [UIImage imageWithCGImage: imageRef];
    CGImageRelease(imageRef);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);

    free(rgba);
    free(argb);

    return image;
}

to do the conversion but it appears to be slow.

Suggest some faster ways

genpfault
  • 51,148
  • 11
  • 85
  • 139
anuj
  • 1,010
  • 2
  • 11
  • 26
  • 1
    See this answer http://stackoverflow.com/questions/8771471/converting-rgba-to-argb-glreadpixels-avassetwriter – David Berry Apr 22 '14 at 19:58
  • I am noob. Please provide an example of Render the picture to a texture (using a FBO with that texture as color attachment). Next render that texture to another framebuffer, with a swizzling fragment shader: – anuj Apr 22 '14 at 20:23
  • Another possibility is to use the [Accelerate Framework](https://developer.apple.com/library/ios/documentation/Performance/Reference/vImage_conversion/Reference/reference.html#//apple_ref/doc/uid/TP40005488) – David Berry Apr 22 '14 at 20:30
  • Can you not make this by setting the correct context byte order? From another post: To get BGRA image you use kCGBitmapByteOrder32Little combined with kCGImageAlphaFirst or kCGImageAlphaPremultipliedFirst or kCGImageAlphaNoneSkipFirst. – Matic Oblak Apr 23 '14 at 06:57

1 Answers1

1
#import <Accelerate/Accelerate.h> ////import into your class file.


-(UIImage *) createARGBImageFromRGBAImage: (UIImage*)image 
{   //CGSize size = CGSizeMake(320, 480);
    CGSize dimensions = CGSizeMake(320, 480);
    NSUInteger bytesPerPixel = 4;
    NSUInteger bytesPerRow = bytesPerPixel * dimensions.width;
    NSUInteger bitsPerComponent = 8;

    unsigned char *rgba = malloc(bytesPerPixel * dimensions.width * dimensions.height);
    unsigned char *argb = malloc(bytesPerPixel * dimensions.width * dimensions.height);

    CGColorSpaceRef colorSpace = NULL;
    CGContextRef context = NULL;

    colorSpace = CGColorSpaceCreateDeviceRGB();
    context = CGBitmapContextCreate(rgba, dimensions.width, dimensions.height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrderDefault); // kCGBitmapByteOrder32Big
    CGContextDrawImage(context, CGRectMake(0, 0, dimensions.width, dimensions.height), [image CGImage]);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);

    const vImage_Buffer src = { rgba, dimensions.height, dimensions.width, bytesPerRow };
    const vImage_Buffer dis = { rgba, dimensions.height, dimensions.width, bytesPerRow };
    const uint8_t map[4] = {3,0,1,2};
    vImagePermuteChannels_ARGB8888(&src, &dis, map, kvImageNoFlags);


    /*for (int x = 0; x < dimensions.width; x++) {
        for (int y = 0; y < dimensions.height; y++) {
            NSUInteger offset = ((dimensions.width * y) + x) * bytesPerPixel;
            argb[offset + 0] = rgba[offset + 3];
            argb[offset + 1] = rgba[offset + 0];
            argb[offset + 2] = rgba[offset + 1];
            argb[offset + 3] = rgba[offset + 2];
        }
    }*/




    colorSpace = CGColorSpaceCreateDeviceRGB();
    context = CGBitmapContextCreate(dis.data, dimensions.width, dimensions.height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrderDefault); // kCGBitmapByteOrder32Big
    CGImageRef imageRef = CGBitmapContextCreateImage(context);
    image = [UIImage imageWithCGImage: imageRef];
    CGImageRelease(imageRef);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);

    free(rgba);
    free(argb);

    return image;
}
rahul_send89
  • 943
  • 8
  • 19