X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Fscreen%2Fscreen-dump.cxx;h=ff3e9b71eaa519b166dcf9746f65d9ef1dd0bd15;hb=229837b14c8c31ec5d7c920e3c4dc7cba58e2419;hp=47a40f224b0c1283c757b873f1490fa883ff79e6;hpb=e2b4c0787a4398199ef2d35e05963acd58b7a04c;p=simgear.git diff --git a/simgear/screen/screen-dump.cxx b/simgear/screen/screen-dump.cxx index 47a40f22..ff3e9b71 100644 --- a/simgear/screen/screen-dump.cxx +++ b/simgear/screen/screen-dump.cxx @@ -12,10 +12,9 @@ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Library General Public License for more details. // -// You should have received a copy of the GNU Library General Public -// License along with this library; if not, write to the -// Free Software Foundation, Inc., 59 Temple Place - Suite 330, -// Boston, MA 02111-1307, USA. +// 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. // // $Id$ @@ -24,16 +23,18 @@ # include #endif -#ifdef HAVE_WINDOWS_H -# include +#ifdef WIN32 +# include #endif #include #include #include -#include -#include +#include + +#include +#include #include "screen-dump.hxx" @@ -51,6 +52,7 @@ bool sg_glWritePPMFile(const char *filename, GLubyte *buffer, int win_width, int ibuffer = (unsigned char *) malloc(win_width*win_height*RGB3); if ( (fp = fopen(filename, "wb")) == NULL ) { + free(ibuffer); printf("Warning: cannot open %s\n", filename); return false; } @@ -63,31 +65,26 @@ bool sg_glWritePPMFile(const char *filename, GLubyte *buffer, int win_width, int for (k = 0; k < RGB3; k++) ibuffer[q++] = (unsigned char) *(buffer + (pixelSize*((win_height-1-i)*win_width+j)+k)); - fwrite(ibuffer, sizeof(unsigned char), RGB3*win_width*win_height, fp); + int written = fwrite(ibuffer, sizeof(unsigned char), RGB3*win_width*win_height, fp); fclose(fp); free(ibuffer); - printf("wrote file (%d x %d pixels, %d bytes)\n", - win_width, win_height, RGB3*win_width*win_height); + if ( written != RGB3*win_width*win_height ) + { + printf("Warning: failed to write %s. File truncated.\n", filename); + return false; + } + + printf("wrote file '%s' (%d x %d pixels, %d bytes)\n", + filename, win_width, win_height, RGB3*win_width*win_height); return true; } -// dump the screen buffer to a ppm file +// dump the screen buffer to a png file, returns true on success bool sg_glDumpWindow(const char *filename, int win_width, int win_height) { - GLubyte *buffer; - bool result; - - buffer = (GLubyte *) malloc(win_width*win_height*RGBA); - - // read window contents from color buffer with glReadPixels - glFinish(); - glReadPixels(0, 0, win_width, win_height, - GL_RGBA, GL_UNSIGNED_BYTE, buffer); - result = sg_glWritePPMFile( filename, buffer, win_width, win_height, - GL_RGBA ); - free(buffer); - - return result; + osg::ref_ptr img(new osg::Image); + img->readPixels(0,0, win_width, win_height, GL_RGB, GL_UNSIGNED_BYTE); + return osgDB::writeImageFile(*img, filename); }