Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

readpix.c 6.5KB

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