1 /*
2     OpenUniverse 1.0
3     Copyright (C) 2000  Raul Alonso <amil@las.es>
4 
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9 
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 extern "C" {
23 #include "jpeglib.h"
24 }
25 #include "ou.h"
26 /*
27     Based on screenshot function from PyroTechnics v1.3
28 	http://www.ling.ed.ac.uk/~oliphant/pyro
29 
30     and on IJG sample code
ScreenShot(char * filename)31 */ void ScreenShot(char *filename)
32 {
33 	int i;
34 	unsigned char *pixels;
35 	struct jpeg_compress_struct cinfo;
36 	struct jpeg_error_mgr jerr;
37 	FILE *outfile;				/* target file */
38 	JSAMPROW row_pointer[1];	/* pointer to JSAMPLE row[s] */
39 	int row_stride;				/* physical row width in image buffer */
40 	char shotspath[100] = SHOTS_DIR;
41 	static char line[200];
42 
43 	cinfo.err = jpeg_std_error(&jerr);
44 	/* Now we can initialize the JPEG compression object. */
45 	fflush(stdout);
46 	jpeg_create_compress(&cinfo);
47 
48 	sprintf(line, "%s/%s", shotspath, filename);
49 	if ((outfile = fopen(line, "wb")) == NULL) {
50 		error("can't write screenshot\n");
51 		shutdown(1);
52 	}
53 
54 	jpeg_stdio_dest(&cinfo, outfile);
55 	cinfo.image_width = width;	/* image width and height, in pixels */
56 	cinfo.image_height = height;
57 	cinfo.input_components = 3;	/* # of color components per pixel */
58 	cinfo.in_color_space = JCS_RGB;	/* colorspace of input image */
59 	jpeg_set_defaults(&cinfo);
60 	jpeg_set_quality(&cinfo, jpeg_quality,
61 					 TRUE /* limit to baseline-JPEG values */ );
62 	jpeg_start_compress(&cinfo, TRUE);
63 
64 	pixels = (unsigned char *) malloc(height * width * 3);
65 	if (!pixels) {
66 		error("Failed to allocate memory");
67 		shutdown(1);
68 	}
69 	glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, pixels);
70 
71 	row_stride = width * 3;
72 	i = cinfo.image_height - 1;
73 	while (i >= 0) {
74 		/* jpeg_write_scanlines expects an array of pointers to scanlines.
75 		 * Here the array is only one element long, but you could pass
76 		 * more than one scanline at a time if that's more convenient.
77 		 */
78 		row_pointer[0] = &pixels[i * row_stride];
79 		(void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
80 		i--;
81 	}
82 	jpeg_finish_compress(&cinfo);
83 	fclose(outfile);
84 	jpeg_destroy_compress(&cinfo);
85 
86 	free(pixels);
87 }
88