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.

copypix.c 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /**
  2. * glCopyPixels test
  3. *
  4. * Brian Paul
  5. * 14 Sep 2007
  6. */
  7. #define GL_GLEXT_PROTOTYPES
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <GL/glut.h>
  12. #include "readtex.h"
  13. #define IMAGE_FILE "../images/arch.rgb"
  14. static int ImgWidth, ImgHeight;
  15. static GLenum ImgFormat;
  16. static GLubyte *Image = NULL;
  17. static int WinWidth = 800, WinHeight = 800;
  18. static int Xpos, Ypos;
  19. static int Scissor = 0;
  20. static float Xzoom, Yzoom;
  21. static GLboolean DrawFront = GL_FALSE;
  22. static GLboolean Dither = GL_TRUE;
  23. static void Reset( void )
  24. {
  25. Xpos = Ypos = 20;
  26. Scissor = 0;
  27. Xzoom = Yzoom = 1.0;
  28. }
  29. static void Display( void )
  30. {
  31. const int dx = (WinWidth - ImgWidth) / 2;
  32. const int dy = (WinHeight - ImgHeight) / 2;
  33. if (DrawFront) {
  34. glDrawBuffer(GL_FRONT);
  35. glReadBuffer(GL_FRONT);
  36. }
  37. else {
  38. glDrawBuffer(GL_BACK);
  39. glReadBuffer(GL_BACK);
  40. }
  41. glClear( GL_COLOR_BUFFER_BIT );
  42. /* draw original image */
  43. glWindowPos2iARB(dx, dy);
  44. glDrawPixels(ImgWidth, ImgHeight, ImgFormat, GL_UNSIGNED_BYTE, Image);
  45. if (Scissor)
  46. glEnable(GL_SCISSOR_TEST);
  47. /* draw copy */
  48. glPixelZoom(Xzoom, Yzoom);
  49. glWindowPos2iARB(Xpos, Ypos);
  50. glCopyPixels(dx, dy, ImgWidth, ImgHeight, GL_COLOR);
  51. glPixelZoom(1, 1);
  52. glDisable(GL_SCISSOR_TEST);
  53. if (DrawFront)
  54. glFinish();
  55. else
  56. glutSwapBuffers();
  57. }
  58. static void Reshape( int width, int height )
  59. {
  60. WinWidth = width;
  61. WinHeight = height;
  62. glViewport( 0, 0, width, height );
  63. glMatrixMode( GL_PROJECTION );
  64. glLoadIdentity();
  65. glOrtho( 0.0, width, 0.0, height, 0.0, 2.0 );
  66. glMatrixMode( GL_MODELVIEW );
  67. glLoadIdentity();
  68. glScissor(width/4, height/4, width/2, height/2);
  69. }
  70. static void Key( unsigned char key, int x, int y )
  71. {
  72. (void) x;
  73. (void) y;
  74. switch (key) {
  75. case ' ':
  76. Reset();
  77. break;
  78. case 'd':
  79. Dither = !Dither;
  80. if (Dither)
  81. glEnable(GL_DITHER);
  82. else
  83. glDisable(GL_DITHER);
  84. break;
  85. case 's':
  86. Scissor = !Scissor;
  87. break;
  88. case 'x':
  89. Xzoom -= 0.1;
  90. break;
  91. case 'X':
  92. Xzoom += 0.1;
  93. break;
  94. case 'y':
  95. Yzoom -= 0.1;
  96. break;
  97. case 'Y':
  98. Yzoom += 0.1;
  99. break;
  100. case 'f':
  101. DrawFront = !DrawFront;
  102. printf("glDrawBuffer(%s)\n", DrawFront ? "GL_FRONT" : "GL_BACK");
  103. break;
  104. case 27:
  105. exit(0);
  106. break;
  107. }
  108. glutPostRedisplay();
  109. }
  110. static void SpecialKey( int key, int x, int y )
  111. {
  112. const int step = (glutGetModifiers() & GLUT_ACTIVE_SHIFT) ? 10 : 1;
  113. (void) x;
  114. (void) y;
  115. switch (key) {
  116. case GLUT_KEY_UP:
  117. Ypos += step;
  118. break;
  119. case GLUT_KEY_DOWN:
  120. Ypos -= step;
  121. break;
  122. case GLUT_KEY_LEFT:
  123. Xpos -= step;
  124. break;
  125. case GLUT_KEY_RIGHT:
  126. Xpos += step;
  127. break;
  128. }
  129. glutPostRedisplay();
  130. }
  131. static void Init( GLboolean ciMode, const char *filename )
  132. {
  133. printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
  134. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  135. Image = LoadRGBImage( filename, &ImgWidth, &ImgHeight, &ImgFormat );
  136. if (!Image) {
  137. printf("Couldn't read %s\n", filename);
  138. exit(0);
  139. }
  140. if (ciMode) {
  141. /* Convert RGB image to grayscale */
  142. GLubyte *indexImage = (GLubyte *) malloc( ImgWidth * ImgHeight );
  143. GLint i;
  144. for (i=0; i<ImgWidth*ImgHeight; i++) {
  145. int gray = Image[i*3] + Image[i*3+1] + Image[i*3+2];
  146. indexImage[i] = gray / 3;
  147. }
  148. free(Image);
  149. Image = indexImage;
  150. ImgFormat = GL_COLOR_INDEX;
  151. for (i=0;i<255;i++) {
  152. float g = i / 255.0;
  153. glutSetColor(i, g, g, g);
  154. }
  155. }
  156. printf("Loaded %d by %d image\n", ImgWidth, ImgHeight );
  157. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  158. glPixelStorei(GL_UNPACK_ROW_LENGTH, ImgWidth);
  159. Reset();
  160. }
  161. static void Usage(void)
  162. {
  163. printf("Keys:\n");
  164. printf(" SPACE Reset Parameters\n");
  165. printf(" Up/Down Move image up/down (SHIFT for large step)\n");
  166. printf(" Left/Right Move image left/right (SHIFT for large step)\n");
  167. printf(" x Decrease X-axis PixelZoom\n");
  168. printf(" X Increase X-axis PixelZoom\n");
  169. printf(" y Decrease Y-axis PixelZoom\n");
  170. printf(" Y Increase Y-axis PixelZoom\n");
  171. printf(" s Toggle GL_SCISSOR_TEST\n");
  172. printf(" f Toggle front/back buffer drawing\n");
  173. printf(" ESC Exit\n");
  174. }
  175. int main( int argc, char *argv[] )
  176. {
  177. GLboolean ciMode = GL_FALSE;
  178. const char *filename = IMAGE_FILE;
  179. int i = 1;
  180. if (argc > i && strcmp(argv[i], "-ci")==0) {
  181. ciMode = GL_TRUE;
  182. i++;
  183. }
  184. if (argc > i) {
  185. filename = argv[i];
  186. }
  187. glutInit( &argc, argv );
  188. glutInitWindowPosition( 0, 0 );
  189. glutInitWindowSize( WinWidth, WinHeight );
  190. if (ciMode)
  191. glutInitDisplayMode( GLUT_INDEX | GLUT_DOUBLE );
  192. else
  193. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE);
  194. glutCreateWindow(argv[0]);
  195. Init(ciMode, filename);
  196. Usage();
  197. glutReshapeFunc( Reshape );
  198. glutKeyboardFunc( Key );
  199. glutSpecialFunc( SpecialKey );
  200. glutDisplayFunc( Display );
  201. glutMainLoop();
  202. return 0;
  203. }