hockeyman
hockeyman

Reputation: 1183

glReadPixels and save to image

I have app, where user drags and drops image, and it is being redrawn with OpenGL for some aviable processing. Everything works. And when user wants to save his image it works like that:

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

This works too. Almost. With some images it is not working as it should work.

For example:

.png

when I open and save this image: enter image description here

I get:
enter image description here

And if I open and save this image: enter image description here

I get:
enter image description here

.jpg

If I open and save:
enter image description here

I get:
enter image description here

And when I open and save:
enter image description here

I get:
enter image description here

So sometimes images saves badly. Why is it happening?


This is how my NSBitmapImageRep is allocated:

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];

And GL_PACK_ALIGNMENT is not edited.

Upvotes: 0

Views: 1063

Answers (2)

Volodymyr B.
Volodymyr B.

Reputation: 3441

I had the same

You need just setup correct bytesPerRow

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

Upvotes: 0

datenwolf
datenwolf

Reputation: 162317

Before feting the pixel data you must tell OpenGL how to "format" it. This means setting all the GL_PACK_ parameters of →glPixelStore. Most important for you is the alignment parameter. Also keep in mind to write the files with the right number of components per pixels, and also read the right components from OpenGL.

Upvotes: 2

Related Questions