2012-03-20 59 views
0

我跟着本教程(http://www.bit-101.com/blog/?p=1861)注意到,多次保存相同的图像后,质量缓慢下降。OpenGL ES 2.0失去图像质量

除了内存泄漏,这里出了什么问题?它应该为每个像素拉4个字节(rgba)。如果每个像素都被占据,那么损失在哪里?

----------------- 编辑 -----------------

我节省了每当有一个顶点位置变换时,都会从像素数据中获取新图像,然后将这个更改后的图像加载到我的纹理缓冲区中,并重置顶点/索引缓冲区。这样我就可以保持我的变化持续下去,并最终减少颤音。见我的其他SO问题:OpenGL ES 2.0 Vertex Transformation Algorithms


----------------- 编辑 ---------------- -

之前

Before multiple saves

enter image description here


下面是本教程的代码:

-(UIImage *) glToUIImage { 
    NSInteger myDataLength = 320 * 480 * 4; 

    // allocate array and read pixels into it. 
    GLubyte *buffer = (GLubyte *) malloc(myDataLength); 
    glReadPixels(0, 0, 320, 480, 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 < 480; y++) 
    { 
     for(int x = 0; x < 320 * 4; x++) 
     { 
      buffer2[(479 - y) * 320 * 4 + x] = buffer[y * 4 * 320 + x]; 
     } 
    } 

    // make data provider with data. 
    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL); 

    // prep the ingredients 
    int bitsPerComponent = 8; 
    int bitsPerPixel = 32; 
    int bytesPerRow = 4 * 320; 
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB(); 
    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault; 
    CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault; 

    // make the cgimage 
    CGImageRef imageRef = CGImageCreate(320, 480, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent); 

    // then make the uiimage from that 
    UIImage *myImage = [UIImage imageWithCGImage:imageRef]; 
    return myImage; 
} 

-(void)captureToPhotoAlbum { 
    UIImage *image = [self glToUIImage]; 
    UIImageWriteToSavedPhotosAlbum(image, self, nil, nil); 
} 
+0

当您说“多次”时,是指在同一图像上多次运行此代码,或从相册加载图像然后再保存它?如果是后者,那么我猜你会看到JPEG压缩(我对iOS不熟悉,所以我不能确定图像是以JPEG格式写入的)。 – 2012-03-20 17:31:04

+0

每次存在顶点位置转换时,我都会从像素数据中保存一个新图像,然后将这个更改后的图像加载到我的纹理缓冲区中,并重置顶点/索引缓冲区。这样我就可以保持我的变化持续下去,并最终减少颤音。看到我的其他SO问题:http://stackoverflow.com/questions/9777393/opengl-es-2-0-vertex-transformation-algorithms – 2012-03-20 17:42:27

+0

这是关键信息 - 你应该把它添加到你的问题。 – 2012-03-20 17:49:50

回答

1

每次渲染改变的图像,它是(necessartly)被重采样 - 也就是说,转换为位图,其中的原始像素(纹理元素)与屏幕网格对齐,而不是1:1。这一定是有损的,因为你已经失去了原始图像的一些细节,所以如果你再次扭曲图像,与用不同参数变换原始图像相比,你会得到更糟糕的结果。

+0

对不起......这里是我上面的评论:“每次有顶点位置转换时,我都会从像素数据中保存一个新图像,然后将这个更改后的图像加载到我的纹理缓冲区中,并重置顶点/索引缓冲区。这样我就可以保持我的变化持续下去,并最终减少颤音。“ ...我在该代码中的原则应用不是将图像保存到库 – 2012-03-20 18:14:38

+0

顺便说一句,谢谢,我会研究重新采样。 – 2012-03-20 18:15:01

+1

我已经更新了我的答案以澄清一点。重新取样不是*保存*的人为因素,因为我错误地指出了,但是完全是绘制图像。如果您转换转换后的图像,而不是再次从原始图像开始,您将始终失去质量。 – 2012-03-20 18:23:55