Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


dump gl to jpeg

This topic is 6437 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been working on this little project and got this to work. It uses the libjpeg library from http://www.ijg.org/ and will dump whatever you have in your rendering window to a jpeg. I have tried it by adding it into a keypress in the tutorials, and it works ok. Any feedback or enhancements?
/*        gl2jpeg v0.001 Nov2000
          Author: Robert Rohatensky
	  Published under the 'do what thou wilt' license
extern "C" {
#include <jpeglib.h>

#include <jerror.h>

writejpeg(char *filename, char *description,
  int x, int y, int width, int height, int quality)
  FILE *outfile;
  struct jpeg_compress_struct cinfo;
  struct jpeg_error_mgr jerr;
  char comment[255];
  volatile JSAMPROW row = 0;
  JSAMPROW rowptr[1];
  JDIMENSION nlines;
  int i;

  outfile = fopen(filename, "wb");
  if (outfile == NULL) {
    return 1;

	cinfo.err = jpeg_std_error(&jerr);

	cinfo.image_width = width;
	cinfo.image_height = height;
	cinfo.input_components = 3;
	cinfo.in_color_space = JCS_RGB;

	if (quality >0)


	row = (JSAMPROW)calloc(1,cinfo.image_width * cinfo.input_components
							* sizeof(JSAMPLE));

	rowptr[0] = row;

	jpeg_start_compress(&cinfo, TRUE);

	sprintf(comment, "CREATOR: gl2jpeg v0.001 using jlib %d %s", JPEG_LIB_VERSION, description);

	jpeg_write_marker(&cinfo,JPEG_COM,(unsigned char *)comment,
						(unsigned int)strlen(comment));

  glPixelStorei(GL_PACK_ALIGNMENT, 1);

  for (i=0;i<height;i++) {
	  glReadPixels(x,height-1-i,width,1,GL_RGB,GL_UNSIGNED_BYTE, row);
	  nlines = jpeg_write_scanlines(&cinfo,rowptr, 1);


  return 0;


Edited by - rohar on 11/1/00 10:40:51 AM Edited by - rohar on 11/2/00 10:28:56 AM

Share this post

Link to post
Share on other sites
Do you have a homepage were I could download a working demo?

I have problems with linking it... the linker couldn''t find some functions
but I used the libjpeg library (maybe a wrong version?)... how get you it to work?

Share this post

Link to post
Share on other sites
there is a copy of the vc project at
(you have to register with just an email address)

I built my own libjpeg.lib from the source @ http://www.ijg.org
for vc, but the code works fine with the default libjpeg on my RH6.2 system.

I have this working with the linux/GLX too, I''ll put a copy of that there too.

Are you having the problem, because of not wrapping the includes with extern "C" {..}?

You have to do that with vc, or change all of the library source files to .cpp from .c

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!