Advertisement

dump gl to jpeg

Started by November 01, 2000 10:26 AM
1 comment, last by rohar 24 years ago
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
          rohar@accesscomm.ca
	  Published under the 'do what thou wilt' license
*/
extern "C" {
#include <jpeglib.h>

#include <jerror.h>
}

int
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);

	jpeg_create_compress(&cinfo);
	jpeg_stdio_dest(&cinfo,outfile);
	cinfo.image_width = width;
	cinfo.image_height = height;
	cinfo.input_components = 3;
	cinfo.in_color_space = JCS_RGB;
	jpeg_set_defaults(&cinfo);

	if (quality >0)
		jpeg_set_quality(&cinfo,quality,TRUE);

	jpeg_simple_progression(&cinfo);

	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);
  }

  jpeg_finish_compress(&cinfo);
  jpeg_destroy_compress(&cinfo);
  free(row);

  fclose(outfile);
  return 0;



}

        
Edited by - rohar on 11/1/00 10:40:51 AM Edited by - rohar on 11/2/00 10:28:56 AM
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?
Advertisement
there is a copy of the vc project at
http://www.caferegina.com/file-storage/
(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

This topic is closed to new replies.

Advertisement