Index: /trunk/source/visualization/OpenGL/History
===================================================================
--- /trunk/source/visualization/OpenGL/History	(revision 948)
+++ /trunk/source/visualization/OpenGL/History	(revision 949)
@@ -20,4 +20,8 @@
 History file for visualization/OpenGL
 -------------------------------------
+
+24th March 2009, Laurent Garnier
+ - G4OpenGLViewer : Add a fix and resizing when X/Y size was even (lack of precision
+   in a /2)
 
 16th March 2009, Laurent Garnier
Index: /trunk/source/visualization/OpenGL/src/G4OpenGLViewer.cc
===================================================================
--- /trunk/source/visualization/OpenGL/src/G4OpenGLViewer.cc	(revision 948)
+++ /trunk/source/visualization/OpenGL/src/G4OpenGLViewer.cc	(revision 949)
@@ -138,5 +138,5 @@
 void G4OpenGLViewer::ResizeGLView()
 {
-  printf("G4OpenGLViewer::ResizeGLView\n");
+  printf("G4OpenGLViewer::ResizeGLView %d %d\n",fWinSize_x,fWinSize_y);
   // Check size
   GLint dims[2];
@@ -150,7 +150,28 @@
     fWinSize_y = dims[1];
   }
-  int side = fWinSize_x;
+  GLsizei side = fWinSize_x;
   if (fWinSize_y < fWinSize_x) side = fWinSize_y;
-  glViewport((fWinSize_x - side) / 2, (fWinSize_y - side) / 2, side, side);  
+
+  // SPECIAL CASE if fWinSize_x is even (69 for example) 
+  // Ex : X: 69 Y: 26
+  // side = 26
+  // width / 2 = 21,5
+  // height / 2 = 0
+  // Should be fixed to closed : 21 0 for ex
+  // Then size must by change to :
+  // X:68 Y: 26
+
+  // SPECIAL CASE
+  if ((fWinSize_x - side)%2) {
+    fWinSize_x --;
+  }
+  if ((fWinSize_y - side)%2) {
+    fWinSize_y --;
+  }
+  
+  GLint width = (fWinSize_x - side) / 2;
+  GLint height = (fWinSize_y - side) / 2;
+  
+  glViewport(width, height, side, side);  
 }
 
Index: /trunk/source/visualization/OpenGL/src/G4OpenGLXViewer.cc
===================================================================
--- /trunk/source/visualization/OpenGL/src/G4OpenGLXViewer.cc	(revision 948)
+++ /trunk/source/visualization/OpenGL/src/G4OpenGLXViewer.cc	(revision 949)
@@ -459,5 +459,5 @@
 
 #ifdef G4DEBUG_VIS_OGL
-    printf("G4OpenGLXViewer::print Create pixmap old context:%d new context %d old winGL:%d \n",cx,pcx,fGLXWin);
+    printf("G4OpenGLXViewer::print Create pixmap old context:%d new context %d old winGL:%d  Size :%d %d\n",cx,pcx,fGLXWin,fWinSize_x, fWinSize_y);
 #endif
     cx=pcx;
@@ -473,4 +473,19 @@
       return;
     }
+
+    Drawable root;
+    int x, y;
+    unsigned int rw, rh,bw,rd;
+
+
+    XGetGeometry(dpy, pmap, &root, &x, &y, &rw, &rh, &bw, &rd); 
+#ifdef G4DEBUG_VIS_OGL
+    printf("G4OpenGLXViewer::print after creation Size :%d %d %d %d rh:%D bw:%d rd:%d\n",x,y,rw,rh, bw, rd);
+#endif
+    // Seems to be OK
+
+
+
+
     GLXPixmap glxpmap = glXCreateGLXPixmap (dpy, 
 					    pvi,
@@ -481,21 +496,23 @@
       return;
     }
+
+
     GLXDrawable tmp_win;
     tmp_win=fGLXWin;
     fGLXWin=glxpmap;
     
-    //
-    bool er1 = glXMakeCurrent (dpy,
-                               fGLXWin,  // pixmap win Id
-                               cx); // pixmap context Id
-        
-    glViewport (0, 0, fWinSize_x, fWinSize_y);
+    // Should be remove : Already done by SetView
+    //     bool er1 = glXMakeCurrent (dpy,
+    //                                fGLXWin,  // pixmap win Id
+    //                                cx); // pixmap context Id
+    
+    // should be remove because already done by SetView() 
+    //    glViewport (0, 0, fWinSize_x, fWinSize_y);
     
     //    InitializeGLView ();
     
     // clear the buffers and window.
+    SetView();
     ClearView ();
-    SetView();
-    //    FinishView ();
 
 #ifdef G4DEBUG_VIS_OGL
@@ -518,5 +535,5 @@
 
     // Restore state
-     stateManager->SetNewState(oldState);  
+    stateManager->SetNewState(oldState);  
 
 
@@ -535,8 +552,14 @@
  		    cx);
     printf("G4OpenGLXViewer::print Restored context:%d old winGL:%d \n",cx,fGLXWin);
-    printf("Error 1:%d 2:%d\n",er1,er2);
-    
-    //    glXDestroyContext(pcx);
-    //    glXDestroyGLXPixmap(glpixmap)
+    //    printf("Error 1:%d 2:%d\n",er1,er2);
+    
+    // Free print context 
+    XFreePixmap(dpy,pmap);
+    glXDestroyContext(dpy,pcx);
+    glXDestroyGLXPixmap(dpy,glxpmap);
+    
+    // Restore view in display context
+    SetView();
+    
   }
 
@@ -586,8 +609,11 @@
 GLubyte* G4OpenGLXViewer::grabPixelsX (int inColor, unsigned int width, unsigned int height) {
 #ifdef G4DEBUG_VIS_OGL
-  printf("G4OpenGLXViewer::grabPixelsX\n");
+  GLint viewport[4];
+  glGetIntegerv(GL_VIEWPORT, viewport);
+  printf("G4OpenGLXViewer::grabPixelsX %d %d     viewport %d %d %d %d\n",width,height,viewport[0],viewport[1],viewport[2],viewport[3]);
 #endif
 	 
-  GLubyte* buffer;
+  //  GLubyte* buffer;
+  GLint* bufferI;
   GLint swapbytes, lsbfirst, rowlength;
   GLint skiprows, skippixels, alignment;
@@ -603,8 +629,12 @@
   }
 	
+  unsigned char *buffer = new unsigned char [ size] ;
   buffer = new GLubyte[size];
+  bufferI = new int[size];
   if (buffer == NULL)
     return NULL;
-	
+  for (int y = 0; y<size; y++) {
+    buffer[y] = 0;
+  }
   glGetIntegerv (GL_UNPACK_SWAP_BYTES, &swapbytes);
   glGetIntegerv (GL_UNPACK_LSB_FIRST, &lsbfirst);
@@ -615,13 +645,14 @@
   glGetIntegerv (GL_UNPACK_ALIGNMENT, &alignment);
 	
-  glPixelStorei (GL_UNPACK_SWAP_BYTES, GL_FALSE);
-  glPixelStorei (GL_UNPACK_LSB_FIRST, GL_FALSE);
-  glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
-	
-  glPixelStorei (GL_UNPACK_SKIP_ROWS, 0);
-  glPixelStorei (GL_UNPACK_SKIP_PIXELS, 0);
-  glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
+//   glPixelStorei (GL_UNPACK_SWAP_BYTES, GL_FALSE);
+//   glPixelStorei (GL_UNPACK_LSB_FIRST, GL_FALSE);
+//   glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
+	
+//   glPixelStorei (GL_UNPACK_SKIP_ROWS, 0);
+//   glPixelStorei (GL_UNPACK_SKIP_PIXELS, 0);
+//   glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
 	
   glReadPixels (0, 0, (GLsizei)width, (GLsizei)height, format, GL_UNSIGNED_BYTE, (GLvoid*) buffer);
+  //  glReadPixels (0, 0, (GLsizei)width, (GLsizei)height, format, GL_INT, bufferI);
 	
   glXWaitGL (); //Wait for effects of all previous OpenGL commands to
@@ -630,16 +661,20 @@
 
 //   printf("-----------\n-----------\n-----------\n-----------\n-----------\n%d %d %d\n",width, height,size);
-//   char * pixels = new char [3 * width * height];
-//   glReadPixels (0, 0, (GLsizei)width, (GLsizei)height, GL_DEPTH_COMPONENT, GL_FLOAT, pixels);
-  
-//   for (int i = 0; i<size; i++) {
-//     if (pixels[i] != 0)
-//       printf("%d ",pixels[i]);
-//     else
-//       printf(" ");
-//   }
-
+  //  char * pixels = new char [3 * width * height];
+  //  glReadPixels (0, 0, (GLsizei)width, (GLsizei)height, GL_DEPTH_COMPONENT, GL_FLOAT, pixels);
+  
+  for (int y = 0; y<height*3; y++) {
+    printf("\n%d : ",y/3);
+    for (int x = 0; x<width*3; x++) {
+      printf("%d ",buffer[y*width+x]);
+  //     if (buffer[y*width+x] == 0)
+//         printf("");
+//       else  if (buffer[y*width+x] == 255)
+//         printf("0");
+     }
+  }
+  
 //   printf("-----------\n-----------\n-----------\n-----------\n-----------\n");
-
+  
   glPixelStorei (GL_UNPACK_SWAP_BYTES, swapbytes);
   glPixelStorei (GL_UNPACK_LSB_FIRST, lsbfirst);
@@ -665,6 +700,7 @@
 	
 #ifdef G4DEBUG_VIS_OGL
-    printf("G4OpenGLXViewer::generateEPSX\n");
-#endif
+  printf("G4OpenGLXViewer::generateEPSX\n");
+#endif
+  
 
   pixels = grabPixelsX (inColour, width, height);
@@ -723,5 +759,6 @@
   for (i = width*height*components; i>0; i--) {
     fprintf (fp, "%02hx ", *(curpix++));
-    if (++pos >= 32) {
+    if (++pos >= width) {
+      //    if (++pos >= 32) {
       fprintf (fp, "\n");
       pos = 0;
