cleaned up jpeg screenshot code

git-svn-id: svn://svn.code.sf.net/p/actiongame/code/trunk/ac@3846 e016b1c5-ed68-44aa-a2f7-31cac88cffde
This commit is contained in:
Lee Salzman
2008-12-03 07:31:13 +00:00
parent 20baa85461
commit 9dac9599a5
+40 -59
View File
@@ -143,75 +143,56 @@ VARP(jpegquality, 10, 70, 100);
void jpeg_screenshot(char *imagepath)
{
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
JSAMPROW row_pointer[1];
JSAMPLE *image_buffer;
int row_stride;
if(!imagepath[0])
{
static string buf;
time_t rawtime;
struct tm * timeinfo;
char buffer [80];
time (&rawtime);
timeinfo = localtime (&rawtime);
strftime (buffer,80,"%Y%b%d_%H%M%S",timeinfo);
struct tm *timeinfo;
char buffer[80];
time(&rawtime);
timeinfo = localtime(&rawtime);
if(!timeinfo) return;
int len = strftime(buffer, sizeof(buffer), "%Y%b%d_%H%M%S", timeinfo);
if(!len || len>=(int)sizeof(buffer)) return;
s_sprintf(buf)("screenshots/%s.jpg", buffer);
imagepath = buf;
}
FILE *jpegfile;
FILE *jpegfile = fopen(imagepath, "wb");
if(!jpegfile) return;
if((jpegfile = fopen(imagepath, "wb")) != NULL)
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
jpeg_stdio_dest(&cinfo, jpegfile);
int row_stride = 3*screen->w;
uchar *pixels = new uchar[row_stride*screen->h];
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glReadPixels(0, 0, screen->w, screen->h, GL_RGB, GL_UNSIGNED_BYTE, pixels);
cinfo.image_width = screen->w;
cinfo.image_height = screen->h;
cinfo.input_components = 3;
cinfo.in_color_space = JCS_RGB;
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, jpegquality, TRUE);
jpeg_start_compress(&cinfo, TRUE);
while(cinfo.next_scanline < cinfo.image_height)
{
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
jpeg_stdio_dest(&cinfo, jpegfile);
GLubyte *pixels = new GLubyte[ 3*scr_w*scr_h*sizeof(GLubyte)];
GLubyte *flip = new GLubyte[ 3*scr_w*scr_h*sizeof(GLubyte)];
image_buffer = pixels;
if (pixels!=NULL && flip!=NULL)
{
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glReadBuffer(GL_BACK);
glReadPixels(0, 0, scr_w, scr_h, GL_RGB, GL_UNSIGNED_BYTE, pixels);
cinfo.image_width = scr_w;
cinfo.image_height = scr_h;
cinfo.input_components = 3;
cinfo.in_color_space = JCS_RGB;
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, jpegquality, TRUE);
jpeg_start_compress(&cinfo, TRUE);
for (int y=0;y<scr_h;y++)
{
for (int x=0;x<scr_w;x++)
{
flip[(y*scr_w+x)*3] = pixels[((scr_h-1-y)*scr_w+x)*3];
flip[(y*scr_w+x)*3+1] = pixels[((scr_h-1-y)*scr_w+x)*3+1];
flip[(y*scr_w+x)*3+2] = pixels[((scr_h-1-y)*scr_w+x)*3+2];
}
}
row_stride = cinfo.image_width * 3;
while (cinfo.next_scanline < cinfo.image_height)
{
row_pointer[0] = &flip[cinfo.next_scanline * row_stride];
(void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
}
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
delete[] pixels;
delete[] flip;
}
fclose(jpegfile);
JSAMPROW row_pointer = &pixels[(cinfo.image_height-cinfo.next_scanline-1) * row_stride];
(void) jpeg_write_scanlines(&cinfo, &row_pointer, 1);
}
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
delete[] pixels;
fclose(jpegfile);
}
void bmp_screenshot(char *imagepath)