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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * glReadPixels and glCopyPixels test
  3. *
  4. * Brian Paul March 1, 2000 This file is in the public domain.
  5. */
  6. #include <assert.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <math.h>
  10. #include <GL/glut.h>
  11. #include "../util/readtex.c" /* a hack, I know */
  12. #define IMAGE_FILE "../images/girl.rgb"
  13. static int ImgWidth, ImgHeight;
  14. static GLenum ImgFormat;
  15. static GLubyte *Image = NULL;
  16. static int APosX, APosY; /* simple drawpixels */
  17. static int BPosX, BPosY; /* read/draw pixels */
  18. static int CPosX, CPosY; /* copypixels */
  19. static GLboolean DrawFront = GL_FALSE;
  20. static GLboolean ScaleAndBias = GL_FALSE;
  21. static GLboolean Benchmark = GL_FALSE;
  22. static GLubyte *TempImage = NULL;
  23. #if 0
  24. #define ReadFormat ImgFormat
  25. #define ReadType GL_UNSIGNED_BYTE
  26. #endif
  27. #if 1
  28. static GLenum ReadFormat = GL_RGBA;
  29. static GLenum ReadType = GL_UNSIGNED_BYTE;
  30. #endif
  31. #if 0
  32. static GLenum ReadFormat = GL_RGB;
  33. static GLenum ReadType = GL_UNSIGNED_SHORT_5_6_5;
  34. #endif
  35. #if 0
  36. static GLenum ReadFormat = GL_RGBA;
  37. static GLenum ReadType = GL_UNSIGNED_SHORT_1_5_5_5_REV;
  38. #endif
  39. #if 0
  40. static GLenum ReadFormat = GL_BGRA;
  41. static GLenum ReadType = GL_UNSIGNED_SHORT_5_5_5_1;
  42. #endif
  43. #if 0
  44. static GLenum ReadFormat = GL_BGRA;
  45. static GLenum ReadType = GL_UNSIGNED_SHORT_4_4_4_4_REV;
  46. #endif
  47. static void
  48. Reset( void )
  49. {
  50. APosX = 5; APosY = 20;
  51. BPosX = APosX + ImgWidth + 5; BPosY = 20;
  52. CPosX = BPosX + ImgWidth + 5; CPosY = 20;
  53. }
  54. static void
  55. PrintString(const char *s)
  56. {
  57. while (*s) {
  58. glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
  59. s++;
  60. }
  61. }
  62. static void
  63. SetupPixelTransfer(GLboolean invert)
  64. {
  65. if (invert) {
  66. glPixelTransferf(GL_RED_SCALE, -1.0);
  67. glPixelTransferf(GL_RED_BIAS, 1.0);
  68. glPixelTransferf(GL_GREEN_SCALE, -1.0);
  69. glPixelTransferf(GL_GREEN_BIAS, 1.0);
  70. glPixelTransferf(GL_BLUE_SCALE, -1.0);
  71. glPixelTransferf(GL_BLUE_BIAS, 1.0);
  72. }
  73. else {
  74. glPixelTransferf(GL_RED_SCALE, 1.0);
  75. glPixelTransferf(GL_RED_BIAS, 0.0);
  76. glPixelTransferf(GL_GREEN_SCALE, 1.0);
  77. glPixelTransferf(GL_GREEN_BIAS, 0.0);
  78. glPixelTransferf(GL_BLUE_SCALE, 1.0);
  79. glPixelTransferf(GL_BLUE_BIAS, 0.0);
  80. }
  81. }
  82. static void
  83. Display( void )
  84. {
  85. glClearColor(.3, .3, .3, 1);
  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. /* clear the temporary image to white (helpful for debugging */
  120. memset(TempImage, 255, ImgWidth * ImgHeight * 4);
  121. glReadPixels(APosX, APosY, ImgWidth, ImgHeight,
  122. ReadFormat, ReadType, TempImage);
  123. }
  124. glRasterPos2i(BPosX, BPosY);
  125. glDisable(GL_DITHER);
  126. SetupPixelTransfer(GL_FALSE);
  127. glDrawPixels(ImgWidth, ImgHeight, ReadFormat, ReadType, TempImage);
  128. /* do copypixels */
  129. glRasterPos2i(CPosX, 5);
  130. PrintString("CopyPixels");
  131. glRasterPos2i(CPosX, CPosY);
  132. glDisable(GL_DITHER);
  133. SetupPixelTransfer(ScaleAndBias);
  134. glCopyPixels(APosX, APosY, ImgWidth, ImgHeight, GL_COLOR);
  135. if (!DrawFront)
  136. glutSwapBuffers();
  137. else
  138. glFinish();
  139. }
  140. static void
  141. Reshape( int width, int height )
  142. {
  143. glViewport( 0, 0, width, height );
  144. glMatrixMode( GL_PROJECTION );
  145. glLoadIdentity();
  146. glOrtho( 0.0, width, 0.0, height, -1.0, 1.0 );
  147. glMatrixMode( GL_MODELVIEW );
  148. glLoadIdentity();
  149. }
  150. static void
  151. Key( unsigned char key, int x, int y )
  152. {
  153. (void) x;
  154. (void) y;
  155. switch (key) {
  156. case 'b':
  157. Benchmark = GL_TRUE;
  158. break;
  159. case 's':
  160. ScaleAndBias = !ScaleAndBias;
  161. break;
  162. case 'f':
  163. DrawFront = !DrawFront;
  164. if (DrawFront) {
  165. glDrawBuffer(GL_FRONT);
  166. glReadBuffer(GL_FRONT);
  167. }
  168. else {
  169. glDrawBuffer(GL_BACK);
  170. glReadBuffer(GL_BACK);
  171. }
  172. printf("glDrawBuffer(%s)\n", DrawFront ? "GL_FRONT" : "GL_BACK");
  173. break;
  174. case 27:
  175. exit(0);
  176. break;
  177. }
  178. glutPostRedisplay();
  179. }
  180. static void
  181. Init( GLboolean ciMode )
  182. {
  183. printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
  184. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  185. Image = LoadRGBImage( IMAGE_FILE, &ImgWidth, &ImgHeight, &ImgFormat );
  186. if (!Image) {
  187. printf("Couldn't read %s\n", IMAGE_FILE);
  188. exit(0);
  189. }
  190. if (ciMode) {
  191. /* Convert RGB image to grayscale */
  192. GLubyte *indexImage = (GLubyte *) malloc( ImgWidth * ImgHeight );
  193. GLint i;
  194. for (i=0; i<ImgWidth*ImgHeight; i++) {
  195. int gray = Image[i*3] + Image[i*3+1] + Image[i*3+2];
  196. indexImage[i] = gray / 3;
  197. }
  198. free(Image);
  199. Image = indexImage;
  200. ImgFormat = GL_COLOR_INDEX;
  201. for (i=0;i<255;i++) {
  202. float g = i / 255.0;
  203. glutSetColor(i, g, g, g);
  204. }
  205. }
  206. printf("Loaded %d by %d image\n", ImgWidth, ImgHeight );
  207. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  208. glPixelStorei(GL_UNPACK_ROW_LENGTH, ImgWidth);
  209. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  210. glPixelStorei(GL_PACK_ROW_LENGTH, ImgWidth);
  211. Reset();
  212. TempImage = (GLubyte *) malloc(ImgWidth * ImgHeight * 4 * sizeof(GLubyte));
  213. assert(TempImage);
  214. }
  215. int
  216. main( int argc, char *argv[] )
  217. {
  218. GLboolean ciMode = GL_FALSE;
  219. if (argc > 1 && strcmp(argv[1], "-ci")==0) {
  220. ciMode = GL_TRUE;
  221. }
  222. glutInit( &argc, argv );
  223. glutInitWindowPosition( 0, 0 );
  224. glutInitWindowSize( 750, 250 );
  225. if (ciMode)
  226. glutInitDisplayMode( GLUT_INDEX | GLUT_DOUBLE );
  227. else
  228. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
  229. glutCreateWindow(argv[0]);
  230. Init(ciMode);
  231. glutReshapeFunc( Reshape );
  232. glutKeyboardFunc( Key );
  233. glutDisplayFunc( Display );
  234. glutMainLoop();
  235. return 0;
  236. }