This my current and correct version of copyImageDataForOpenGL
- (BOOL)copyImageDataForOpenGL:(CGImageRef)image {
if (pngData) {
free(pngData);
}
pngData = (GLubyte*)malloc(width * height * channels);
const int rowSize = width * channels;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
NSUInteger bytesPerPixel = channels;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(pngData, width, height,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextClearRect(context, CGRectMake(0, 0, width, height));
CGContextTranslateCTM(context, 0,0);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
[ARRenderer flipRGBData: pngData bytesPerRow:bytesPerRow height:height];
return YES;
}
where [ARRenderer flipRGBData]
is defined as
+ (void)flipRGBData:(GLubyte*)data bytesPerRow:(int)bytesPerRow height:(int)height {
GLubyte* swp = (GLubyte*)malloc(bytesPerRow);
for (int h = 0; h < height / 2; h++) {
memcpy(swp, data + (height - 1 - h) * bytesPerRow, bytesPerRow);
memcpy(data + (height - 1 - h) * bytesPerRow, data + h * bytesPerRow, bytesPerRow);
memcpy(data + h * bytesPerRow, swp, bytesPerRow);
}
free(swp);
}