Clone of mesa.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

readpix.c 5.6KB

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