0

I'm using following code for reading on image from OpenGL ES scene:

- (UIImage *)drawableToCGImage
{
CGRect myRect = self.bounds;
NSInteger myDataLength = myRect.size.width * myRect.size.height * 4;

glFinish();
glPixelStorei(GL_PACK_ALIGNMENT, 4);

int width = myRect.size.width;
int height = myRect.size.height;

GLubyte *buffer = (GLubyte *) malloc(myDataLength);
GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer2);
for(int y1 = 0; y1 < height; y1++) {
    for(int x1 = 0; x1 < width * 4; x1++) {
        buffer[(height - 1 - y1) * width * 4 + x1] = buffer2[y1 * 4 * width + x1];
    }
}

free(buffer2);

CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer, myDataLength, NULL);
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * myRect.size.width;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
CGImageRef imageRef = CGImageCreate(myRect.size.width, myRect.size.height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
CGColorSpaceRelease(colorSpaceRef);
CGDataProviderRelease(provider);
UIImage *image = [UIImage imageWithCGImage:imageRef];

CGImageRelease(imageRef);

return image;
}

It works perfectly for iPad and old iPhone versions, but I noticed that on iPhone 6 (both device and simulators) it looks like monochrome glitches. What could it be?

Also, here is my code for CAEAGLLayer properties:

eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
                                @YES, kEAGLDrawablePropertyRetainedBacking,
                                kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];

Could somebody shed the light on this crazy magic, please?

8
  • Are you using UIImageWriteToSavedPhotosAlbum() to preview your image? I've found that can have weird effects when saving an image generated from a bitmap context without first being converted a png or jpeg format. Try using UIImagePNGRepresentation() before outputting your image. Also you should probably specify kCGImageAlphaPremultipliedLast for the bitmap info.
    – Hamish
    Feb 23, 2016 at 17:01
  • @originaluser2 I'm using debugger for previewing images (Quick Look feature). I've just tried to save it as PNG, but result is the same. kCGImageAlphaPremultipliedLast also didn't do the thing.
    – Sashucity
    Feb 24, 2016 at 9:07
  • Showing the result might be helpful in finding what is going on.. Feb 24, 2016 at 10:48
  • @MaticOblak I can't upload an image, but here is a link – dl.dropboxusercontent.com/u/53403968/glitched.png
    – Sashucity
    Feb 24, 2016 at 11:57
  • Interesting result. Pinpointing the issue might help. You have 3 parts that may causing this. First is the pixel data you receive are incorrect which may be easiest to test if you create a new texture from the data you get from reading the pixels and then check the frame snapshot to see the texture if it is correct. Second is row swapping which can be checked by using the buffer2 when generating the image. If none of these are causing the issue then the image generating must be the cause. Maybe try kCGBitmapByteOrder32Big | kCGImageAlphaLast at least this is what I am using... Feb 24, 2016 at 12:13

1 Answer 1

0

Thanks to @MaticOblak, I've figured out the problem.

Buffer was filled incorrectly, because float values of rect size were not correctly rounded (yeah, only for iPhone 6 dimension). Instead integer values should be used.

UPD: my issue was fixed with the following code:

GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);

int width = viewport[2];
int height = viewport[3];
1
  • Please add the fixed line(s) Feb 24, 2016 at 12:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.