From 9dac9599a50b9e81241274a4b51262eeb1607fca Mon Sep 17 00:00:00 2001 From: Lee Salzman Date: Wed, 3 Dec 2008 07:31:13 +0000 Subject: [PATCH] cleaned up jpeg screenshot code git-svn-id: svn://svn.code.sf.net/p/actiongame/code/trunk/ac@3846 e016b1c5-ed68-44aa-a2f7-31cac88cffde --- source/src/main.cpp | 99 ++++++++++++++++++--------------------------- 1 file changed, 40 insertions(+), 59 deletions(-) diff --git a/source/src/main.cpp b/source/src/main.cpp index 89ad97c7..de43e48d 100644 --- a/source/src/main.cpp +++ b/source/src/main.cpp @@ -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