Clone of mesa.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /* $Id: readpix.c,v 1.4 2000/10/16 21:24:39 brianp Exp $ */
  2. /*
  3. * glReadPixels and glCopyPixels test
  4. *
  5. * Brian Paul March 1, 2000 This file is in the public domain.
  6. */
  7. /*
  8. * $Log: readpix.c,v $
  9. * Revision 1.4 2000/10/16 21:24:39 brianp
  10. * use gray background, memset TempImage to white to help with debugging
  11. *
  12. * Revision 1.3 2000/03/31 01:01:31 brianp
  13. * tweaks to allow different read formats/types
  14. *
  15. * Revision 1.2 2000/03/23 19:47:25 brianp
  16. * added benchmarking
  17. *
  18. * Revision 1.1 2000/03/01 16:23:14 brianp
  19. * test glDraw/Read/CopyPixels()
  20. *
  21. */
  22. #include <assert.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <math.h>
  26. #include <GL/glut.h>
  27. #include "../util/readtex.c" /* a hack, I know */
  28. #define IMAGE_FILE "../images/girl.rgb"
  29. static int ImgWidth, ImgHeight;
  30. static GLenum ImgFormat;
  31. static GLubyte *Image = NULL;
  32. static int APosX, APosY; /* simple drawpixels */
  33. static int BPosX, BPosY; /* read/draw pixels */
  34. static int CPosX, CPosY; /* copypixels */
  35. static GLboolean DrawFront = GL_FALSE;
  36. static GLboolean ScaleAndBias = GL_FALSE;
  37. static GLboolean Benchmark = GL_FALSE;
  38. static GLubyte *TempImage = NULL;
  39. #if 0
  40. #define ReadFormat ImgFormat
  41. #define ReadType GL_UNSIGNED_BYTE
  42. #endif
  43. #if 1
  44. static GLenum ReadFormat = GL_RGBA;
  45. static GLenum ReadType = GL_UNSIGNED_BYTE;
  46. #endif
  47. #if 0
  48. static GLenum ReadFormat = GL_RGB;
  49. static GLenum ReadType = GL_UNSIGNED_SHORT_5_6_5;
  50. #endif
  51. #if 0
  52. static GLenum ReadFormat = GL_RGBA;
  53. static GLenum ReadType = GL_UNSIGNED_SHORT_1_5_5_5_REV;
  54. #endif
  55. #if 0
  56. static GLenum ReadFormat = GL_BGRA;
  57. static GLenum ReadType = GL_UNSIGNED_SHORT_5_5_5_1;
  58. #endif
  59. #if 0
  60. static GLenum ReadFormat = GL_BGRA;
  61. static GLenum ReadType = GL_UNSIGNED_SHORT_4_4_4_4_REV;
  62. #endif
  63. static void
  64. Reset( void )
  65. {
  66. APosX = 5; APosY = 20;
  67. BPosX = APosX + ImgWidth + 5; BPosY = 20;
  68. CPosX = BPosX + ImgWidth + 5; CPosY = 20;
  69. }
  70. static void
  71. PrintString(const char *s)
  72. {
  73. while (*s) {
  74. glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
  75. s++;
  76. }
  77. }
  78. static void
  79. SetupPixelTransfer(GLboolean invert)
  80. {
  81. if (invert) {
  82. glPixelTransferf(GL_RED_SCALE, -1.0);
  83. glPixelTransferf(GL_RED_BIAS, 1.0);
  84. glPixelTransferf(GL_GREEN_SCALE, -1.0);
  85. glPixelTransferf(GL_GREEN_BIAS, 1.0);
  86. glPixelTransferf(GL_BLUE_SCALE, -1.0);
  87. glPixelTransferf(GL_BLUE_BIAS, 1.0);
  88. }
  89. else {
  90. glPixelTransferf(GL_RED_SCALE, 1.0);
  91. glPixelTransferf(GL_RED_BIAS, 0.0);
  92. glPixelTransferf(GL_GREEN_SCALE, 1.0);
  93. glPixelTransferf(GL_GREEN_BIAS, 0.0);
  94. glPixelTransferf(GL_BLUE_SCALE, 1.0);
  95. glPixelTransferf(GL_BLUE_BIAS, 0.0);
  96. }
  97. }
  98. static void
  99. Display( void )
  100. {
  101. glClearColor(.3, .3, .3, 1);
  102. glClear( GL_COLOR_BUFFER_BIT );
  103. glRasterPos2i(5, ImgHeight+25);
  104. PrintString("f = toggle front/back s = toggle scale/bias b = benchmark");
  105. /* draw original image */
  106. glRasterPos2i(APosX, 5);
  107. PrintString("Original");
  108. glRasterPos2i(APosX, APosY);
  109. glEnable(GL_DITHER);
  110. SetupPixelTransfer(GL_FALSE);
  111. glDrawPixels(ImgWidth, ImgHeight, ImgFormat, GL_UNSIGNED_BYTE, Image);
  112. /* do readpixels, drawpixels */
  113. glRasterPos2i(BPosX, 5);
  114. PrintString("Read/DrawPixels");
  115. SetupPixelTransfer(ScaleAndBias);
  116. if (Benchmark) {
  117. GLint reads = 0;
  118. GLint endTime;
  119. GLint startTime = glutGet(GLUT_ELAPSED_TIME);
  120. GLdouble seconds, pixelsPerSecond;
  121. printf("Benchmarking...\n");
  122. do {
  123. glReadPixels(APosX, APosY, ImgWidth, ImgHeight,
  124. ReadFormat, ReadType, TempImage);
  125. reads++;
  126. endTime = glutGet(GLUT_ELAPSED_TIME);
  127. } while (endTime - startTime < 4000); /* 4 seconds */
  128. seconds = (double) (endTime - startTime) / 1000.0;
  129. pixelsPerSecond = reads * ImgWidth * ImgHeight / seconds;
  130. printf("Result: %d reads in %f seconds = %f pixels/sec\n",
  131. reads, seconds, pixelsPerSecond);
  132. Benchmark = GL_FALSE;
  133. }
  134. else {
  135. /* clear the temporary image to white (helpful for debugging */
  136. memset(TempImage, 255, ImgWidth * ImgHeight * 4);
  137. glReadPixels(APosX, APosY, ImgWidth, ImgHeight,
  138. ReadFormat, ReadType, TempImage);
  139. }
  140. glRasterPos2i(BPosX, BPosY);
  141. glDisable(GL_DITHER);
  142. SetupPixelTransfer(GL_FALSE);
  143. glDrawPixels(ImgWidth, ImgHeight, ReadFormat, ReadType, TempImage);
  144. /* do copypixels */
  145. glRasterPos2i(CPosX, 5);
  146. PrintString("CopyPixels");
  147. glRasterPos2i(CPosX, CPosY);
  148. glDisable(GL_DITHER);
  149. SetupPixelTransfer(ScaleAndBias);
  150. glCopyPixels(APosX, APosY, ImgWidth, ImgHeight, GL_COLOR);
  151. if (!DrawFront)
  152. glutSwapBuffers();
  153. }
  154. static void
  155. Reshape( int width, int height )
  156. {
  157. glViewport( 0, 0, width, height );
  158. glMatrixMode( GL_PROJECTION );
  159. glLoadIdentity();
  160. glOrtho( 0.0, width, 0.0, height, -1.0, 1.0 );
  161. glMatrixMode( GL_MODELVIEW );
  162. glLoadIdentity();
  163. }
  164. static void
  165. Key( unsigned char key, int x, int y )
  166. {
  167. (void) x;
  168. (void) y;
  169. switch (key) {
  170. case 'b':
  171. Benchmark = GL_TRUE;
  172. break;
  173. case 's':
  174. ScaleAndBias = !ScaleAndBias;
  175. break;
  176. case 'f':
  177. DrawFront = !DrawFront;
  178. if (DrawFront) {
  179. glDrawBuffer(GL_FRONT);
  180. glReadBuffer(GL_FRONT);
  181. }
  182. else {
  183. glDrawBuffer(GL_BACK);
  184. glReadBuffer(GL_BACK);
  185. }
  186. printf("glDrawBuffer(%s)\n", DrawFront ? "GL_FRONT" : "GL_BACK");
  187. break;
  188. case 27:
  189. exit(0);
  190. break;
  191. }
  192. glutPostRedisplay();
  193. }
  194. static void
  195. Init( GLboolean ciMode )
  196. {
  197. printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
  198. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  199. Image = LoadRGBImage( IMAGE_FILE, &ImgWidth, &ImgHeight, &ImgFormat );
  200. if (!Image) {
  201. printf("Couldn't read %s\n", IMAGE_FILE);
  202. exit(0);
  203. }
  204. if (ciMode) {
  205. /* Convert RGB image to grayscale */
  206. GLubyte *indexImage = malloc( ImgWidth * ImgHeight );
  207. GLint i;
  208. for (i=0; i<ImgWidth*ImgHeight; i++) {
  209. int gray = Image[i*3] + Image[i*3+1] + Image[i*3+2];
  210. indexImage[i] = gray / 3;
  211. }
  212. free(Image);
  213. Image = indexImage;
  214. ImgFormat = GL_COLOR_INDEX;
  215. for (i=0;i<255;i++) {
  216. float g = i / 255.0;
  217. glutSetColor(i, g, g, g);
  218. }
  219. }
  220. printf("Loaded %d by %d image\n", ImgWidth, ImgHeight );
  221. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  222. glPixelStorei(GL_UNPACK_ROW_LENGTH, ImgWidth);
  223. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  224. glPixelStorei(GL_PACK_ROW_LENGTH, ImgWidth);
  225. Reset();
  226. TempImage = (GLubyte *) malloc(ImgWidth * ImgHeight * 4 * sizeof(GLubyte));
  227. assert(TempImage);
  228. }
  229. int
  230. main( int argc, char *argv[] )
  231. {
  232. GLboolean ciMode = GL_FALSE;
  233. if (argc > 1 && strcmp(argv[1], "-ci")==0) {
  234. ciMode = GL_TRUE;
  235. }
  236. glutInit( &argc, argv );
  237. glutInitWindowPosition( 0, 0 );
  238. glutInitWindowSize( 750, 250 );
  239. if (ciMode)
  240. glutInitDisplayMode( GLUT_INDEX | GLUT_DOUBLE );
  241. else
  242. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
  243. glutCreateWindow(argv[0]);
  244. Init(ciMode);
  245. glutReshapeFunc( Reshape );
  246. glutKeyboardFunc( Key );
  247. glutDisplayFunc( Display );
  248. glutMainLoop();
  249. return 0;
  250. }