diff --git a/Graphics/gl2ppm.cpp b/Graphics/gl2ppm.cpp
index 99f29ad10fe0544cba85d8257aa964ad9cec3c66..565fc71e1b1f6a3fa73261cc5634c963204abb9d 100644
--- a/Graphics/gl2ppm.cpp
+++ b/Graphics/gl2ppm.cpp
@@ -3,35 +3,23 @@
 #include "GmshUI.h"
 
 void create_ppm(FILE *outfile, int width, int height){
-  unsigned char *red, *green, *blue;
-  register int x, y;
-  unsigned char   r, g, b;
-
-  red = (unsigned char *)malloc(height*width*sizeof(unsigned char));
-  green = (unsigned char *)malloc(height*width*sizeof(unsigned char));
-  blue = (unsigned char *)malloc(height*width*sizeof(unsigned char));
-
-  glReadPixels(0,0,width,height,GL_RED,GL_UNSIGNED_BYTE,red);
-  glReadPixels(0,0,width,height,GL_GREEN,GL_UNSIGNED_BYTE,green);
-  glReadPixels(0,0,width,height,GL_BLUE,GL_UNSIGNED_BYTE,blue);
+  unsigned char *pixels;
+  int i, row_stride;
+  
+  pixels=(unsigned char *)Malloc(height*width*3);
+  glReadPixels(0,0,width,height,GL_RGB,GL_UNSIGNED_BYTE,pixels);
 
   fprintf(outfile, "P6\n");
   fprintf(outfile, "%d %d\n", width, height);
   fprintf(outfile, "%d\n", 255);
 
-  for ( y = height-1; y >= 0; y-- ){
-    for ( x = 0; x < width; x++ ){
-      r = red[y*width+x];
-      g = green[y*width+x];
-      b = blue[y*width+x];
-      fwrite(&r, 1, 1, outfile);
-      fwrite(&g, 1, 1, outfile);
-      fwrite(&b, 1, 1, outfile);
-    }
+  row_stride = width * 3;
+  i = height-1;
+  while (i >= 0) {
+    fwrite(&pixels[i * row_stride], 1, row_stride, outfile);
+    i--;
   }
 
-  Free(red);
-  Free(green);
-  Free(blue);
+  Free(pixels);
 }