-2

I have a opengl application that create one texture in format unsigned char*, and i gotta save this texture in one image file, but a don't know how to do. can someone help me?

this is my creation of this texture:

static unsigned char* pDepthTexBuf;

and this is my code that use this texture:

glBindTexture(GL_TEXTURE_2D, depthTexID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texWidth, texHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, pDepthTexBuf);

but how i can save this texture "pDepthTexBuf" in image file?

user3651443
  • 29
  • 1
  • 11
  • 1
    "this is my creation of this texture" This is a declaration of an `unsigned char*` variable. A texture is an array of data. You need to allocate it somewhere and fill it with something, I hope you are doing that. "how i can save this texture" You want to use an image format library such as libjpeg. Google 'libjpeg tutorial'. – n. m. could be an AI Jul 03 '14 at 21:14
  • Downvoted for the demonstration of zero research. There's literally dozens of image formats and their specification documents. There are equally many existing libraries to deal with these formats. SO is not a free-of-charge code supermarket. – enhzflep Jul 03 '14 at 22:10
  • excuse me for my lack of detail, i am creating a kinect application, it create a texture (unsigned char* format) pixel by pixel from a DepthMetaData variable (class from OpenNI library), in a large function. I can create a video from this texture, but i have save a image from this video (like a photo) and i don't know this, by this i am looking from one method to convert this texture for a photo image. – user3651443 Jul 04 '14 at 01:08

2 Answers2

1

This is a very complicated question... I suggest referring to other public examples, like this one: http://www.andrewewhite.net/wordpress/2008/09/02/very-simple-jpeg-writer-in-c-c/

Basically, you need to integrate an image library, and then use whatever hooks it supports to save your data.

Yeraze
  • 3,269
  • 4
  • 28
  • 42
1

The simplest approach is probably to use a library like OpenCV, which has some very easy to use mechanisms for turning byte arrays of RGB data into image files.

You can see an example of reading the OpenGL image buffer and storing it as a PNG file here. Saving a JPG may be as simple as changing the extension of the output file.

// Create an OpenCV matrix of the appropriate size and depth
cv::Mat img(windowSize.y, windowSize.x, CV_8UC3);
glPixelStorei(GL_PACK_ALIGNMENT, (img.step & 3) ? 1 : 4);
glPixelStorei(GL_PACK_ROW_LENGTH, img.step / img.elemSize());
// Fetch the pixels as BGR byte values 
glReadPixels(0, 0, img.cols, img.rows, GL_BGR, GL_UNSIGNED_BYTE, img.data);

// Image files use Y = down, so we need to flip the image on the X axis
cv::flip(img, img, 0);

static int counter = 0;
static char buffer[128];
sprintf(buffer, "screenshot%05i.png", counter++);
// write the image file
bool success = cv::imwrite(buffer, img);
if (!success) {
  throw std::runtime_error("Failed to write image");
}
Jherico
  • 28,584
  • 8
  • 61
  • 87