2012-10-09 106 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