Advertisement
Guest User

Untitled

a guest
Feb 25th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.24 KB | None | 0 0
  1. #define SCREEN_WIDTH  8*00
  2. #define SCREEN_HEIGHT 600
  3. #define SCREEN_PATH   16
  4. #include <iostream>
  5. #include <windows.h>
  6. #include <GL/glut.h>
  7. #include <GL/glu.h>
  8. #include <vector>
  9. #include <ctime>
  10. #include <thread>
  11. #include <turbojpeg.h>
  12. //#include <opencv2/core/core.hpp>
  13. //#include <opencv2/highgui/highgui.hpp>
  14.  
  15. //using namespace cv;
  16. using namespace std;
  17. //unsigned char* data;
  18.  
  19. int w1 = 0;
  20. int h1 = 0;
  21.  
  22. int t = 0;
  23. HDC hdc;
  24. HBITMAP hBitmap;
  25. BITMAPINFO MyBMInfo;
  26.  
  27. BYTE* lpPixels;
  28. GLubyte* buffer;
  29. GLubyte* buffer_rgb;
  30.  
  31. HDC hCaptureDC;
  32.  
  33. tjhandle TurboJCompress;
  34.  
  35. float max_clocks = 0;
  36.  
  37. extern GLuint LoadTexture();
  38. extern HBITMAP GetScreenBmp( HDC hdc);
  39. extern GLuint LoadTexture();
  40. extern void timer(int value);
  41. extern void CaptureScreen();
  42.  
  43. void reshape(int w, int h)
  44. {
  45.     w1 = w;
  46.     h1 = h;
  47.     glViewport(0, 0, w, h);
  48. }
  49.  
  50. void orthogonalStart()
  51. {
  52.     glMatrixMode(GL_PROJECTION);
  53.     glPushMatrix();
  54.     glLoadIdentity();
  55.     gluOrtho2D(-w1/2., w1/2., -h1/2., h1/2.);
  56.     glMatrixMode(GL_MODELVIEW);
  57. }
  58.  
  59. void orthogonalEnd()
  60. {
  61.     glMatrixMode(GL_PROJECTION);
  62.     glPopMatrix();
  63.     glMatrixMode(GL_MODELVIEW);
  64. }
  65.  
  66. GLuint texture = 0;
  67. void background()
  68. {
  69.     glBindTexture( GL_TEXTURE_2D, texture );
  70.  
  71.     orthogonalStart();
  72.  
  73.     // texture width/height
  74.     const int iw = MyBMInfo.bmiHeader.biWidth;
  75.     const int ih = MyBMInfo.bmiHeader.biHeight;
  76.  
  77.     glPushMatrix();
  78.     glTranslatef( -iw/2, -ih/2, 0 );
  79.     glBegin(GL_QUADS);
  80.         glTexCoord2i(0,0); glVertex2i(0, 0);
  81.         glTexCoord2i(1,0); glVertex2i(iw, 0);
  82.         glTexCoord2i(1,1); glVertex2i(iw, ih);
  83.         glTexCoord2i(0,1); glVertex2i(0, ih);
  84.     glEnd();
  85.     glPopMatrix();
  86.  
  87.     orthogonalEnd();
  88. }
  89.  
  90. void display()
  91. {
  92.  
  93.  
  94.     glClearColor (1.0,0.0,0.0,1.0);
  95.     glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  96.     glLoadIdentity();
  97.     glEnable( GL_TEXTURE_2D );
  98.  
  99.     texture = LoadTexture();
  100.  
  101.  
  102.     background();
  103.  
  104.     //gluLookAt (0, 0, 5, 0, 0, 0, 0, 1, 0);
  105.  
  106.     glutSwapBuffers();
  107. }
  108.  
  109. GLuint LoadTexture()
  110. {
  111.  
  112.     //data = (unsigned char*)malloc(sizeof(unsigned char)*800*600*4);
  113.     //unsigned char *data = new unsigned char[width*height*3];
  114.     glGenTextures( 1, &texture );
  115.     glBindTexture( GL_TEXTURE_2D, texture );
  116.     glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
  117.     glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
  118.  
  119.     //even better quality, but this will do for now.
  120.     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_NEAREST);
  121.     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_NEAREST);
  122.  
  123.     //to the edge of our shape.
  124.     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
  125.     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
  126.  
  127.     //Generate the texture
  128.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, MyBMInfo.bmiHeader.biWidth, MyBMInfo.bmiHeader.biHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, lpPixels);
  129.     return texture; //return whether it was successful
  130. }
  131.  
  132. HBITMAP GetScreenBmp() {
  133.     // Get screen dimensions
  134.     int nScreenWidth = GetSystemMetrics(SM_CXSCREEN);
  135.     int nScreenHeight = GetSystemMetrics(SM_CYSCREEN);
  136.  
  137.     // Create compatible DC, create a compatible bitmap and copy the screen using BitBlt()
  138.     hCaptureDC  = CreateCompatibleDC(hdc);
  139.     hBitmap = CreateCompatibleBitmap(hdc, nScreenWidth, nScreenHeight);
  140.     HGDIOBJ hOld = SelectObject(hCaptureDC, hBitmap);
  141.     BitBlt(hCaptureDC,0,0,nScreenWidth, nScreenHeight, hdc,0,0,SRCCOPY|CAPTUREBLT);
  142.  
  143.     SelectObject(hCaptureDC, hOld); // always select the previously selected object once done
  144.     //DeleteDC(hCaptureDC);
  145. }
  146.  
  147.  
  148. int main(int argc, char **argv)
  149. {
  150.     //const int WindowWidth = glutGet(GLUT_WINDOW_WIDTH);
  151.     //const int WindowHeight = glutGet(GLUT_WINDOW_HEIGHT);
  152.     //const float PixelperMeter = 1000.f*(float)glutGet(GLUT_SCREEN_HEIGHT)/glutGet(GLUT_SCREEN_HEIGHT_MM);
  153.  
  154.     TurboJCompress = tjInitCompress();
  155.  
  156.     hdc = GetDC(0);
  157.     GetScreenBmp();
  158.  
  159.     MyBMInfo = {0};
  160.     MyBMInfo.bmiHeader.biSize = sizeof(MyBMInfo.bmiHeader);
  161.  
  162.     // Get the BITMAPINFO structure from the bitmap
  163.     if(0 == GetDIBits(hdc, hBitmap, 0, 0, NULL, &MyBMInfo, DIB_RGB_COLORS)) {
  164.         cout << "error" << endl;
  165.     }
  166.  
  167.     // create the bitmap buffer
  168.     lpPixels = new BYTE[MyBMInfo.bmiHeader.biSizeImage];
  169.     buffer = new GLubyte[MyBMInfo.bmiHeader.biSizeImage];
  170.     buffer_rgb = new GLubyte[MyBMInfo.bmiHeader.biWidth*MyBMInfo.bmiHeader.biHeight*3];
  171.     // Better do this here - the original bitmap might have BI_BITFILEDS, which makes it
  172.     // necessary to read the color table - you might not want this.
  173.     MyBMInfo.bmiHeader.biCompression = BI_RGB;
  174.  
  175.     std::thread updatePixels(CaptureScreen);
  176.     //updatePixels.join();
  177.  
  178.     glutInit(&argc, argv);
  179.     glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
  180.  
  181.     glutInitWindowSize(MyBMInfo.bmiHeader.biWidth, MyBMInfo.bmiHeader.biHeight);
  182.     glutCreateWindow("Aspect Ratio");
  183.  
  184.     glutDisplayFunc(display);
  185.     glutReshapeFunc(reshape);
  186.     glutTimerFunc(0, timer, 0);
  187.     glutMainLoop();
  188.  
  189.     DeleteObject(hBitmap);
  190.     ReleaseDC(NULL, hdc);
  191.     delete[] lpPixels;
  192.     return 0;
  193. }
  194.  
  195. void timer(int value) {
  196.     glutPostRedisplay();
  197.     glutTimerFunc(t,timer,0);
  198. }
  199.  
  200. void CaptureScreen() {
  201.     while(true) {
  202.         clock_t t = clock();
  203.  
  204.         HGDIOBJ hOld = SelectObject(hCaptureDC, hBitmap);
  205.         BitBlt(hCaptureDC,0,0,MyBMInfo.bmiHeader.biWidth, MyBMInfo.bmiHeader.biHeight, hdc,0,0,SRCCOPY|CAPTUREBLT);
  206.         SelectObject(hCaptureDC, hOld);
  207.         //GLubyte* Pixels = new GLubyte[ 3 * MyBMInfo.bmiHeader.biWidth * MyBMInfo.bmiHeader.biHeight ];
  208.        // glReadBuffer(GL_FRONT_AND_BACK);
  209.         //glPixelStorei(GL_PACK_ALIGNMENT, 1);
  210.         //glReadPixels( 0, 0, MyBMInfo.bmiHeader.biWidth, MyBMInfo.bmiHeader.biHeight, GL_RGBA, GL_UNSIGNED_BYTE, lpPixels );
  211.         //glPixelStorei(GL_PACK_ALIGNMENT, 1);
  212.  
  213.  
  214.         clock_t t0 = clock();
  215.  
  216.  
  217.         if(0 == GetDIBits(hdc, hBitmap, 0, MyBMInfo.bmiHeader.biHeight, (LPVOID)buffer, &MyBMInfo, DIB_RGB_COLORS)) {
  218.             cout << "error2" << endl;
  219.         }
  220.  
  221.         clock_t t1 = clock();
  222.  
  223.         int rgb_counter = 0;
  224.         for(DWORD i = 0; i < MyBMInfo.bmiHeader.biSizeImage; i+=4) {
  225.                 GLubyte temp = buffer[i]; //B
  226.                 buffer[i] = buffer[i+2];
  227.                 buffer[i+2] = temp;
  228.                 buffer_rgb[rgb_counter++] = buffer[i];
  229.                 buffer_rgb[rgb_counter++] = buffer[i+1];
  230.                 buffer_rgb[rgb_counter++] = buffer[i+2];
  231.         }
  232.  
  233.         clock_t t2 = clock();
  234.  
  235.         GLubyte* compressed_buffer = nullptr;
  236.         unsigned long int compressed_buffer_size = 0;
  237.  
  238.         if(int error = tjCompress2(TurboJCompress, buffer_rgb,
  239.                 MyBMInfo.bmiHeader.biWidth, 0, MyBMInfo.bmiHeader.biHeight,
  240.                 TJPF_RGB, &compressed_buffer, &compressed_buffer_size, TJSAMP_444, 75, TJFLAG_FASTDCT) != 0) {
  241.             cout << "Error: " << error << " " << tjGetErrorStr() << endl;
  242.         }
  243.  
  244.         clock_t t3 = clock();
  245.  
  246.         memcpy(lpPixels, buffer, MyBMInfo.bmiHeader.biSizeImage);
  247.  
  248.         /*float temp = (t1 - t)/(float)CLOCKS_PER_SEC;
  249.         if(temp > max_clocks)
  250.             max_clocks = temp;*/
  251.         printf("BitBlt: %.3f GetDIBits: %.3f Reverse: %.3f compress: %.3f compress_percent: %.2f full: %.3f\n",
  252.                 (t0-t)/(float)CLOCKS_PER_SEC,
  253.                 (t1-t0)/(float)CLOCKS_PER_SEC,
  254.                 (t2-t1)/(float)CLOCKS_PER_SEC,
  255.                 (t3-t2)/(float)CLOCKS_PER_SEC,
  256.                 rgb_counter/(double)compressed_buffer_size,
  257.                 (t3-t)/(float)CLOCKS_PER_SEC);
  258.         //cout << temp << " max: " << max_clocks << endl;
  259.     }
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement