Advertisement
jckuri

opengl-capture.c

Jul 6th, 2013
777
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.05 KB | None | 0 0
  1. // This C code captures the screen of Macs and shows it in an OpenGL window through the function glDrawPixels
  2. // gcc -o opengl-capture opengl-capture.c -framework Carbon -framework OpenGL -framework GLUT
  3.  
  4. #include <OpenGL/gl.h>
  5. #include <GLUT/glut.h>
  6.  
  7. #include </System/Library/Frameworks/ApplicationServices.framework/Frameworks/CoreGraphics.framework/Headers/CoreGraphics.h>
  8. #include <objc/objc-runtime.h>
  9.  
  10. unsigned int window_width = 800, window_height = 600;
  11.  
  12. int* getMainScreenSize() {
  13.  CGRect r = CGDisplayBounds(kCGDirectMainDisplay);
  14.  int *size=malloc(sizeof(int)*2);
  15.  size[0]=r.size.width;
  16.  size[1]=r.size.height;
  17.  return size;
  18. }
  19.  
  20. int* getAllScreensSize() {
  21.  CGImageRef img = CGWindowListCreateImage(CGRectInfinite, kCGWindowListOptionOnScreenOnly, kCGNullWindowID, kCGWindowImageDefault);
  22.  int *size=malloc(sizeof(int)*2);
  23.  size[0]=CGImageGetWidth(img);
  24.  size[1]=CGImageGetHeight(img);
  25.  return size;
  26. }
  27.  
  28. CGImageRef captureImage(CGRect r) {
  29.  return CGWindowListCreateImage(r, kCGWindowListOptionOnScreenOnly, kCGNullWindowID, kCGWindowImageDefault);
  30. }
  31.  
  32. unsigned char* macPixelsToRGB(CGImageRef image,unsigned char *pixelData) {
  33.  int bytesPerPixel=CGImageGetBitsPerPixel(image)/8;
  34.  int bytesPerRow=CGImageGetBytesPerRow(image);
  35.  int imageWidth=CGImageGetWidth(image);
  36.  int imageHeight=CGImageGetHeight(image);
  37.  int x,y,m,p;
  38.  unsigned char *rgb=(unsigned char*)malloc(imageWidth*imageHeight*3);
  39.  for(y=0; y<imageHeight; y++) {
  40.   for(x=0; x<imageWidth; x++) {
  41.    m=y*bytesPerRow+x*bytesPerPixel;
  42.    p=(y*imageWidth+x)*3;
  43.    rgb[p+0]=pixelData[m+2]/2;
  44.    rgb[p+1]=pixelData[m+1]/2;
  45.    rgb[p+2]=pixelData[m+0]/2;
  46.    //rgb[p+0]=(int)(127.0*(double)y/(double)imageHeight);
  47.    //rgb[p+1]=0;
  48.    //rgb[p+2]=0;
  49.   }
  50.  }
  51.  return rgb;
  52. }
  53.  
  54. unsigned char* getImageBuffer(CGImageRef image) {
  55.  if(image == NULL) {
  56.   fprintf(stderr,"CGWindowListCreateImage failed!\n");
  57.   return NULL;
  58.  }
  59.  CGDataProviderRef provider = CGImageGetDataProvider(image);
  60.  CFDataRef dataRef = CGDataProviderCopyData(provider);
  61.  unsigned char *pixelData=(unsigned char*)CFDataGetBytePtr(dataRef);
  62.  return macPixelsToRGB(image,pixelData);
  63. }
  64.  
  65. unsigned char* captureScreenRegion(int x,int y,int width,int height) {
  66.  CGRect r;
  67.  r.origin.x = x;
  68.  r.origin.y = y;
  69.  r.size.width = width;
  70.  r.size.height = height;
  71.  return getImageBuffer(captureImage(r));
  72. }
  73.  
  74. unsigned char* captureTheWholeScreen() {
  75.  return getImageBuffer(captureImage(CGRectInfinite));
  76. }
  77.  
  78. void display() {
  79.  unsigned char *pixels=captureScreenRegion(0,0,window_width,window_height);
  80.  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  81.  glRasterPos2f(-1,1);
  82.  glPixelZoom( 1, -1 );
  83.  glDrawPixels(window_width,window_height,GL_RGB,GL_BYTE,pixels);
  84.  free(pixels);
  85.  glutSwapBuffers();
  86. }
  87.  
  88. int main(int argc, char** argv) {
  89.  glutInit(&argc, argv);
  90.  glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  91.  glutInitWindowSize(window_width, window_height);
  92.  glutCreateWindow("OpenGL glDrawPixels demo");
  93.  glutDisplayFunc(display);
  94.  glEnable(GL_DEPTH_TEST);
  95.  glClearColor(0.0, 0.0, 0.0, 1.0);
  96.  glutMainLoop();
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement