2012-10-09 103 views
0

我有应用程序,其中用户拖放图像,并且正在使用OpenGL重绘一些可处理的应用程序。一切正常。当用户想要保存他的图像时,它的工作原理是这样的:glReadPixels并保存到图像

glReadPixels -> NSBitmapImageRep -> NSData -> Write to file 

这也适用。几乎。有些图像不能正常工作。

例如:

png格式

,当我打开和保存此图像: enter image description here

我得到:
enter image description here

如果我开保存这张图片: enter image description here

我得到:
enter image description here

的.jpg

如果我打开和保存:
enter image description here

我得到:
enter image description here

当我打开和保存:
enter image description here

我得到:
enter image description here

所以有时图像保存得厉害。为什么会发生?


这是我NSBitmapImageRep如何分配:

NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL pixelsWide:_image_width pixelsHigh:_image_height bitsPerSample:8 samplesPerPixel:3 hasAlpha:NO isPlanar:NO colorSpaceName:NSDeviceRGBColorSpace bytesPerRow:3 * _image_width bitsPerPixel:0]; 

而且GL_PACK_ALIGNMENT不编辑。

+0

? – genpfault

+0

我还没有编辑GL_PACK_ALIGNMENT,我正在抓取RGB图像 – hockeyman

+0

我在glReadPixels前添加了'glPixelStorei(GL_PACK_ALIGNMENT,3);它没有影响任何东西 – hockeyman

回答

2

在发布像素数据之前,您必须告诉OpenGL如何“格式化”它。这意味着将所有GL_PACK_参数设置为→glPixelStore。对你来说最重要的是对齐参数。另外请记住,每个像素使用适当数量的组件编写文件,并从OpenGL中读取正确的组件。

+0

而且应该在哪里设置'glPixelStore'参数?在设置NSBitmapImageRep之前?或者在设置OpenGL上下文之后?或者在哪里? – hockeyman

+0

OpenGL是一个状态机。在事情发生之前,你就设置了状态。在你的情况下,在调用glReadPixels之前。 – datenwolf

+0

我在glReadPixels之前添加了'glPixelStorei(GL_PACK_ALIGNMENT,3);'它没有影响任何东西 – hockeyman

0

我有同样的

你只需要设置正确bytesPerRow是否使用默认的`4(字节)GL_PACK_ALIGNMENT`并试图抓住RGB(3个字节)的图像

bytesPerRow:(width*3+3)&~3