John Qualis
John Qualis

Reputation: 1743

opengl pixel data to jpeg

Any C++ examples available to convert raw pixel data obtained from glReadPixels to JPEG format and back?

Upvotes: 2

Views: 2308

Answers (3)

Kos
Kos

Reputation: 72299

Use an external library for that.

I'll recommend DevIL, your number one Swiss Army Knife for handling image files. You'll just need to

  • create an RGB image in memory via DevIL,
  • call glReadPixels to fill your DevIL image with pixels read from the GL framebuffer,
  • call ilSaveImage("foo.jpg") to save the file. You can also use bmp, png and a handful more - the format will get autodetected from the file name.

Simple.

Upvotes: 1

BЈовић
BЈовић

Reputation: 64273

You can use ImageMagick library to convert raw data to the jpeg image data, and opposite. Using the same library, you can convert jpeg image data into raw (RGB) data.

Upvotes: 2

mpenkov
mpenkov

Reputation: 21904

I'm not sure if OpenGL has support for dealing with JPEG images. It's not what the library is really for.

Once you've got access to the pixel data, you should be able to use easily OpenCV to write the image to JPEG (or any other format), though. Here's some pseudo-code to get you going.

/*
 * On Linux, compile with:
 *
 * g++ -Wall -ggdb -I. -I/usr/include/opencv -L /usr/lib -lm -lcv -lhighgui -lcvaux filename.cpp -o filename.out
 */

#include <cv.h>    
#include <highgui.h>

/*
 * Your image dimensions.
 */
int width;
int height;

CvSize size = cvSize(width, height);

/*
 * Create 3-channel image, unsigned 8-bit per channel.
 */
IplImage *image = cvCreateImage(size, IPL_DEPTH_8U, 3);

for (int i = 0; i < width; ++i)
for (int j = 0; j < height; ++j)
{
    unsigned int r;
    unsigned int g;
    unsigned int b;

    /*
     * Call glReadPixels, grab your RGB data.
     * Keep in mind that OpenCV stores things in BGR order.
     */
    CvScalar bgr = cvScalar(b, g, r);
    cvSet2D(image, i, j, bgr);
}

cvSaveImage("filename.jpg", image);
cvReleaseImage(&image);

Other libraries for dealing with JPEG also exist, if you look around.

Upvotes: 0

Related Questions