/*
    OpenUniverse 1.0
    Copyright (C) 2000  Raul Alonso <amil@las.es>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include <stdio.h>
#include <stdlib.h>
extern "C" {
#include "jpeglib.h"
}
#include "ou.h"
/* 
    Based on screenshot function from PyroTechnics v1.3
	http://www.ling.ed.ac.uk/~oliphant/pyro

    and on IJG sample code	
*/ void ScreenShot(char *filename)
{
	int i;
	unsigned char *pixels;
	struct jpeg_compress_struct cinfo;
	struct jpeg_error_mgr jerr;
	FILE *outfile;				/* target file */
	JSAMPROW row_pointer[1];	/* pointer to JSAMPLE row[s] */
	int row_stride;				/* physical row width in image buffer */
	char shotspath[100] = SHOTS_DIR;
	static char line[200];

	cinfo.err = jpeg_std_error(&jerr);
	/* Now we can initialize the JPEG compression object. */
	fflush(stdout);
	jpeg_create_compress(&cinfo);

	sprintf(line, "%s/%s", shotspath, filename);
	if ((outfile = fopen(line, "wb")) == NULL) {
		error("can't write screenshot\n");
		shutdown(1);
	}

	jpeg_stdio_dest(&cinfo, outfile);
	cinfo.image_width = width;	/* image width and height, in pixels */
	cinfo.image_height = height;
	cinfo.input_components = 3;	/* # of color components per pixel */
	cinfo.in_color_space = JCS_RGB;	/* colorspace of input image */
	jpeg_set_defaults(&cinfo);
	jpeg_set_quality(&cinfo, jpeg_quality,
					 TRUE /* limit to baseline-JPEG values */ );
	jpeg_start_compress(&cinfo, TRUE);

	pixels = (unsigned char *) malloc(height * width * 3);
	if (!pixels) {
		error("Failed to allocate memory");
		shutdown(1);
	}
	glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, pixels);

	row_stride = width * 3;
	i = cinfo.image_height - 1;
	while (i >= 0) {
		/* jpeg_write_scanlines expects an array of pointers to scanlines.
		 * Here the array is only one element long, but you could pass
		 * more than one scanline at a time if that's more convenient.
		 */
		row_pointer[0] = &pixels[i * row_stride];
		(void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
		i--;
	}
	jpeg_finish_compress(&cinfo);
	fclose(outfile);
	jpeg_destroy_compress(&cinfo);

	free(pixels);
}
