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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 <string.h>
  11. #include <GL/glut.h>
  12. #include "readtex.h"
  13. #define IMAGE_FILE "../images/girl.rgb"
  14. static int ImgWidth, ImgHeight;
  15. static int WinWidth, WinHeight;
  16. static GLenum ImgFormat;
  17. static GLubyte *Image = NULL;
  18. static int APosX, APosY; /* simple drawpixels */
  19. static int BPosX, BPosY; /* read/draw pixels */
  20. static int CPosX, CPosY; /* copypixels */
  21. static GLboolean DrawFront = GL_FALSE;
  22. static GLboolean ScaleAndBias = GL_FALSE;
  23. static GLboolean Benchmark = GL_FALSE;
  24. static GLboolean Triangle = GL_FALSE;
  25. static GLubyte *TempImage = NULL;
  26. #define COMBO 1
  27. #if COMBO == 0
  28. #define ReadFormat ImgFormat
  29. #define ReadType GL_UNSIGNED_BYTE
  30. #elif COMBO == 1
  31. static GLenum ReadFormat = GL_RGBA;
  32. static GLenum ReadType = GL_UNSIGNED_BYTE;
  33. #elif COMBO == 2
  34. static GLenum ReadFormat = GL_RGB;
  35. static GLenum ReadType = GL_UNSIGNED_BYTE;
  36. #elif COMBO == 3
  37. static GLenum ReadFormat = GL_RGB;
  38. static GLenum ReadType = GL_UNSIGNED_SHORT_5_6_5;
  39. #elif COMBO == 4
  40. static GLenum ReadFormat = GL_RGBA;
  41. static GLenum ReadType = GL_UNSIGNED_SHORT_1_5_5_5_REV;
  42. #elif COMBO == 5
  43. static GLenum ReadFormat = GL_BGRA;
  44. static GLenum ReadType = GL_UNSIGNED_SHORT_5_5_5_1;
  45. #elif COMBO == 6
  46. static GLenum ReadFormat = GL_BGRA;
  47. static GLenum ReadType = GL_UNSIGNED_SHORT_4_4_4_4_REV;
  48. #elif COMBO == 7
  49. static GLenum ReadFormat = GL_RGBA;
  50. static GLenum ReadType = GL_HALF_FLOAT_ARB;
  51. #undef GL_OES_read_format
  52. #endif
  53. static void
  54. Reset( void )
  55. {
  56. APosX = 5; APosY = 20;
  57. BPosX = APosX + ImgWidth + 5; BPosY = 20;
  58. CPosX = BPosX + ImgWidth + 5; CPosY = 20;
  59. }
  60. static void
  61. PrintString(const char *s)
  62. {
  63. while (*s) {
  64. glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
  65. s++;
  66. }
  67. }
  68. static void
  69. SetupPixelTransfer(GLboolean invert)
  70. {
  71. if (invert) {
  72. glPixelTransferf(GL_RED_SCALE, -1.0);
  73. glPixelTransferf(GL_RED_BIAS, 1.0);
  74. glPixelTransferf(GL_GREEN_SCALE, -1.0);
  75. glPixelTransferf(GL_GREEN_BIAS, 1.0);
  76. glPixelTransferf(GL_BLUE_SCALE, -1.0);
  77. glPixelTransferf(GL_BLUE_BIAS, 1.0);
  78. }
  79. else {
  80. glPixelTransferf(GL_RED_SCALE, 1.0);
  81. glPixelTransferf(GL_RED_BIAS, 0.0);
  82. glPixelTransferf(GL_GREEN_SCALE, 1.0);
  83. glPixelTransferf(GL_GREEN_BIAS, 0.0);
  84. glPixelTransferf(GL_BLUE_SCALE, 1.0);
  85. glPixelTransferf(GL_BLUE_BIAS, 0.0);
  86. }
  87. }
  88. /**
  89. * Exercise Pixel Pack parameters by reading the image in four pieces.
  90. */
  91. static void
  92. ComplexReadPixels(GLint x, GLint y, GLsizei width, GLsizei height,
  93. GLenum format, GLenum type, GLvoid *pixels)
  94. {
  95. const GLsizei width0 = width / 2;
  96. const GLsizei width1 = width - width0;
  97. const GLsizei height0 = height / 2;
  98. const GLsizei height1 = height - height0;
  99. glPixelStorei(GL_PACK_ROW_LENGTH, width);
  100. /* lower-left quadrant */
  101. glReadPixels(x, y, width0, height0, format, type, pixels);
  102. /* lower-right quadrant */
  103. glPixelStorei(GL_PACK_SKIP_PIXELS, width0);
  104. glReadPixels(x + width0, y, width1, height0, format, type, pixels);
  105. /* upper-left quadrant */
  106. glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
  107. glPixelStorei(GL_PACK_SKIP_ROWS, height0);
  108. glReadPixels(x, y + height0, width0, height1, format, type, pixels);
  109. /* upper-right quadrant */
  110. glPixelStorei(GL_PACK_SKIP_PIXELS, width0);
  111. glPixelStorei(GL_PACK_SKIP_ROWS, height0);
  112. glReadPixels(x + width0, y + height0, width1, height1, format, type, pixels);
  113. /* restore defaults */
  114. glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
  115. glPixelStorei(GL_PACK_SKIP_ROWS, 0);
  116. glPixelStorei(GL_PACK_ROW_LENGTH, ImgWidth);
  117. }
  118. static void
  119. Display( void )
  120. {
  121. glClearColor(.3, .3, .3, 1);
  122. glClear( GL_COLOR_BUFFER_BIT );
  123. glRasterPos2i(5, ImgHeight+25);
  124. PrintString("f = toggle front/back s = toggle scale/bias b = benchmark");
  125. /* draw original image */
  126. glRasterPos2i(APosX, 5);
  127. PrintString("Original");
  128. if (!Triangle) {
  129. glRasterPos2i(APosX, APosY);
  130. glEnable(GL_DITHER);
  131. SetupPixelTransfer(GL_FALSE);
  132. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  133. glDrawPixels(ImgWidth, ImgHeight, ImgFormat, GL_UNSIGNED_BYTE, Image);
  134. }
  135. else {
  136. float z = 0;
  137. glViewport(APosX, APosY, ImgWidth, ImgHeight);
  138. glMatrixMode( GL_PROJECTION );
  139. glLoadIdentity();
  140. glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
  141. glDisable(GL_CULL_FACE);
  142. /* Red should never be seen
  143. */
  144. glBegin(GL_POLYGON);
  145. glColor3f(1,0,0);
  146. glVertex3f(-2, -2, z);
  147. glVertex3f(-2, 2, z);
  148. glVertex3f(2, 2, z);
  149. glVertex3f(2, -2, z);
  150. glEnd();
  151. /* Blue background
  152. */
  153. glBegin(GL_POLYGON);
  154. glColor3f(.5,.5,1);
  155. glVertex3f(-1, -1, z);
  156. glVertex3f(-1, 1, z);
  157. glVertex3f(1, 1, z);
  158. glVertex3f(1, -1, z);
  159. glEnd();
  160. /* Triangle
  161. */
  162. glBegin(GL_TRIANGLES);
  163. glColor3f(.8,0,0);
  164. glVertex3f(-0.9, -0.9, z);
  165. glColor3f(0,.9,0);
  166. glVertex3f( 0.9, -0.9, z);
  167. glColor3f(0,0,.7);
  168. glVertex3f( 0.0, 0.9, z);
  169. glEnd();
  170. glColor3f(1,1,1);
  171. glViewport( 0, 0, WinWidth, WinHeight );
  172. glMatrixMode( GL_PROJECTION );
  173. glLoadIdentity();
  174. glOrtho( 0.0, WinWidth, 0.0, WinHeight, -1.0, 1.0 );
  175. }
  176. /* might try alignment=4 here for testing */
  177. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  178. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  179. /* do readpixels, drawpixels */
  180. glRasterPos2i(BPosX, 5);
  181. PrintString("Read/DrawPixels");
  182. SetupPixelTransfer(ScaleAndBias);
  183. if (Benchmark) {
  184. GLint reads = 0;
  185. GLint endTime;
  186. GLint startTime = glutGet(GLUT_ELAPSED_TIME);
  187. GLdouble seconds, mpixels, mpixelsPerSecond;
  188. printf("Benchmarking...\n");
  189. do {
  190. glReadPixels(APosX, APosY, ImgWidth, ImgHeight,
  191. ReadFormat, ReadType, TempImage);
  192. reads++;
  193. endTime = glutGet(GLUT_ELAPSED_TIME);
  194. } while (endTime - startTime < 4000); /* 4 seconds */
  195. seconds = (double) (endTime - startTime) / 1000.0;
  196. mpixels = reads * (ImgWidth * ImgHeight / (1000.0 * 1000.0));
  197. mpixelsPerSecond = mpixels / seconds;
  198. printf("Result: %d reads in %f seconds = %f Mpixels/sec\n",
  199. reads, seconds, mpixelsPerSecond);
  200. Benchmark = GL_FALSE;
  201. }
  202. else {
  203. /* clear the temporary image to white (helpful for debugging */
  204. memset(TempImage, 255, ImgWidth * ImgHeight * 4);
  205. #if 1
  206. glReadPixels(APosX, APosY, ImgWidth, ImgHeight,
  207. ReadFormat, ReadType, TempImage);
  208. (void) ComplexReadPixels;
  209. #else
  210. /* you might use this when debugging */
  211. ComplexReadPixels(APosX, APosY, ImgWidth, ImgHeight,
  212. ReadFormat, ReadType, TempImage);
  213. #endif
  214. }
  215. glRasterPos2i(BPosX, BPosY);
  216. glDisable(GL_DITHER);
  217. SetupPixelTransfer(GL_FALSE);
  218. glDrawPixels(ImgWidth, ImgHeight, ReadFormat, ReadType, TempImage);
  219. /* do copypixels */
  220. glRasterPos2i(CPosX, 5);
  221. PrintString("CopyPixels");
  222. glRasterPos2i(CPosX, CPosY);
  223. glDisable(GL_DITHER);
  224. SetupPixelTransfer(ScaleAndBias);
  225. glCopyPixels(APosX, APosY, ImgWidth, ImgHeight, GL_COLOR);
  226. if (!DrawFront)
  227. glutSwapBuffers();
  228. else
  229. glFinish();
  230. }
  231. static void
  232. Reshape( int width, int height )
  233. {
  234. WinWidth = width;
  235. WinHeight = height;
  236. glViewport( 0, 0, width, height );
  237. glMatrixMode( GL_PROJECTION );
  238. glLoadIdentity();
  239. glOrtho( 0.0, width, 0.0, height, -1.0, 1.0 );
  240. glMatrixMode( GL_MODELVIEW );
  241. glLoadIdentity();
  242. }
  243. static void
  244. Key( unsigned char key, int x, int y )
  245. {
  246. (void) x;
  247. (void) y;
  248. switch (key) {
  249. case 'b':
  250. Benchmark = GL_TRUE;
  251. break;
  252. case 't':
  253. Triangle = !Triangle;
  254. break;
  255. case 's':
  256. ScaleAndBias = !ScaleAndBias;
  257. break;
  258. case 'f':
  259. DrawFront = !DrawFront;
  260. if (DrawFront) {
  261. glDrawBuffer(GL_FRONT);
  262. glReadBuffer(GL_FRONT);
  263. }
  264. else {
  265. glDrawBuffer(GL_BACK);
  266. glReadBuffer(GL_BACK);
  267. }
  268. printf("glDrawBuffer(%s)\n", DrawFront ? "GL_FRONT" : "GL_BACK");
  269. break;
  270. case 27:
  271. exit(0);
  272. break;
  273. }
  274. glutPostRedisplay();
  275. }
  276. static void
  277. Init( GLboolean ciMode )
  278. {
  279. GLboolean have_read_format = GL_FALSE;
  280. printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
  281. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  282. Image = LoadRGBImage( IMAGE_FILE, &ImgWidth, &ImgHeight, &ImgFormat );
  283. if (!Image) {
  284. printf("Couldn't read %s\n", IMAGE_FILE);
  285. exit(0);
  286. }
  287. if (ciMode) {
  288. /* Convert RGB image to grayscale */
  289. GLubyte *indexImage = (GLubyte *) malloc( ImgWidth * ImgHeight );
  290. GLint i;
  291. for (i=0; i<ImgWidth*ImgHeight; i++) {
  292. int gray = Image[i*3] + Image[i*3+1] + Image[i*3+2];
  293. indexImage[i] = gray / 3;
  294. }
  295. free(Image);
  296. Image = indexImage;
  297. ImgFormat = GL_COLOR_INDEX;
  298. for (i=0;i<255;i++) {
  299. float g = i / 255.0;
  300. glutSetColor(i, g, g, g);
  301. }
  302. }
  303. #ifdef GL_OES_read_format
  304. if ( glutExtensionSupported( "GL_OES_read_format" ) ) {
  305. glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_TYPE_OES, (GLint *) &ReadType);
  306. glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES, (GLint *) &ReadFormat);
  307. have_read_format = GL_TRUE;
  308. }
  309. #endif
  310. printf( "GL_OES_read_format %ssupported. "
  311. "Using type / format = 0x%04x / 0x%04x\n",
  312. (have_read_format) ? "" : "not ",
  313. ReadType, ReadFormat );
  314. printf("Loaded %d by %d image\n", ImgWidth, ImgHeight );
  315. glPixelStorei(GL_UNPACK_ROW_LENGTH, ImgWidth);
  316. glPixelStorei(GL_PACK_ROW_LENGTH, ImgWidth);
  317. Reset();
  318. /* allocate large TempImage to store and image data type, plus an
  319. * extra 1KB in case we're tinkering with pack alignment.
  320. */
  321. TempImage = (GLubyte *) malloc(ImgWidth * ImgHeight * 4 * 4
  322. + 1000);
  323. assert(TempImage);
  324. }
  325. int
  326. main( int argc, char *argv[] )
  327. {
  328. GLboolean ciMode = GL_FALSE;
  329. glutInitWindowSize( 750, 250 );
  330. glutInit( &argc, argv );
  331. if (argc > 1 && strcmp(argv[1], "-ci")==0) {
  332. ciMode = GL_TRUE;
  333. }
  334. if (ciMode)
  335. glutInitDisplayMode( GLUT_INDEX | GLUT_DOUBLE );
  336. else
  337. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
  338. glutCreateWindow(argv[0]);
  339. Init(ciMode);
  340. glutReshapeFunc( Reshape );
  341. glutKeyboardFunc( Key );
  342. glutDisplayFunc( Display );
  343. glutMainLoop();
  344. return 0;
  345. }