1 #include"os_predef.h"
2 #include"os_gl.h"
3 
4 #include<stdio.h>
5 
PyMOLReadPixels(GLint x,GLint y,GLsizei width,GLsizei height,GLenum format,GLenum type,GLvoid * pixels)6 void PyMOLReadPixels(GLint x,
7                      GLint y,
8                      GLsizei width,
9                      GLsizei height, GLenum format, GLenum type, GLvoid * pixels)
10 {
11 
12   /* special "safe" version of glReadPixels for buggy OpenGL implementations */
13 
14   GLint swapbytes, lsbfirst, rowlength;
15   GLint skiprows, skippixels, alignment;
16 
17   /* Save current pixel store state. */
18   glGetIntegerv(GL_PACK_SWAP_BYTES, &swapbytes);
19   glGetIntegerv(GL_PACK_LSB_FIRST, &lsbfirst);
20   glGetIntegerv(GL_PACK_ROW_LENGTH, &rowlength);
21   glGetIntegerv(GL_PACK_SKIP_ROWS, &skiprows);
22   glGetIntegerv(GL_PACK_SKIP_PIXELS, &skippixels);
23   glGetIntegerv(GL_PACK_ALIGNMENT, &alignment);
24 
25   /* Set desired pixel store state. */
26   glPixelStorei(GL_PACK_SWAP_BYTES, GL_FALSE);
27   glPixelStorei(GL_PACK_LSB_FIRST, GL_FALSE);
28   glPixelStorei(GL_PACK_ROW_LENGTH, 0);
29   glPixelStorei(GL_PACK_SKIP_ROWS, 0);
30   glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
31   glPixelStorei(GL_PACK_ALIGNMENT, 1);
32 
33   /* call glFlush & glFinish first to avoid full system crash on buggy
34    * ATI Radeon drivers, such as Radeon 7000 & 9000 series.  (calling
35    * both is probably redundant and ultra-paranoid, but so what?) */
36   glFlush();
37   glFinish();
38 
39   /* now get the pixels */
40   glReadPixels(x, y, width, height, format, type, pixels);
41 
42   /* now flush once again just to be extra sure that we don't encounter
43    * the dreaded ATI driver bug system freeze-up */
44   glFlush();
45   glFinish();
46 
47   /* and then estore current pixel store state. */
48   glPixelStorei(GL_PACK_SWAP_BYTES, swapbytes);
49   glPixelStorei(GL_PACK_LSB_FIRST, lsbfirst);
50   glPixelStorei(GL_PACK_ROW_LENGTH, rowlength);
51   glPixelStorei(GL_PACK_SKIP_ROWS, skiprows);
52   glPixelStorei(GL_PACK_SKIP_PIXELS, skippixels);
53   glPixelStorei(GL_PACK_ALIGNMENT, alignment);
54 
55 }
56 
PyMOLDrawPixels(GLsizei width,GLsizei height,GLenum format,GLenum type,const GLvoid * pixels)57 void PyMOLDrawPixels(GLsizei width,
58                      GLsizei height, GLenum format, GLenum type, const GLvoid * pixels)
59 {
60 
61   /* special "safe" version of glDrawPixels for buggy OpenGL implementations */
62 
63   GLint swapbytes, lsbfirst, rowlength;
64   GLint skiprows, skippixels, alignment;
65 
66   /* Save current pixel store state. */
67   glGetIntegerv(GL_UNPACK_SWAP_BYTES, &swapbytes);
68   glGetIntegerv(GL_UNPACK_LSB_FIRST, &lsbfirst);
69   glGetIntegerv(GL_UNPACK_ROW_LENGTH, &rowlength);
70   glGetIntegerv(GL_UNPACK_SKIP_ROWS, &skiprows);
71   glGetIntegerv(GL_UNPACK_SKIP_PIXELS, &skippixels);
72   glGetIntegerv(GL_UNPACK_ALIGNMENT, &alignment);
73 
74   /* Set desired pixel store state. */
75   glPixelStorei(GL_UNPACK_SWAP_BYTES, GL_FALSE);
76   glPixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE);
77   glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
78   glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
79   glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
80   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
81 
82   glDrawPixels(width, height, format, type, pixels);
83 
84   /* Restore current pixel store state. */
85   glPixelStorei(GL_UNPACK_SWAP_BYTES, swapbytes);
86   glPixelStorei(GL_UNPACK_LSB_FIRST, lsbfirst);
87   glPixelStorei(GL_UNPACK_ROW_LENGTH, rowlength);
88   glPixelStorei(GL_UNPACK_SKIP_ROWS, skiprows);
89   glPixelStorei(GL_UNPACK_SKIP_PIXELS, skippixels);
90   glPixelStorei(GL_UNPACK_ALIGNMENT, alignment);
91 
92 }
93 
PyMOLCheckOpenGLErr(const char * pos)94 int PyMOLCheckOpenGLErr(const char *pos)
95 {
96   int flag = 0;
97   GLenum glerr = glGetError();
98   while(glerr != GL_NO_ERROR) {
99     printf("OpenGL-Error: Where? %s: glerr=%d\n", pos, glerr);
100     glerr = glGetError();
101     flag = 1;
102   }
103   return flag;
104 }
105