Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define SCREEN_WIDTH 8*00
- #define SCREEN_HEIGHT 600
- #define SCREEN_PATH 16
- #include <iostream>
- #include <windows.h>
- #include <GL/glut.h>
- #include <GL/glu.h>
- #include <vector>
- #include <ctime>
- #include <thread>
- #include <turbojpeg.h>
- //#include <opencv2/core/core.hpp>
- //#include <opencv2/highgui/highgui.hpp>
- //using namespace cv;
- using namespace std;
- //unsigned char* data;
- int w1 = 0;
- int h1 = 0;
- int t = 0;
- HDC hdc;
- HBITMAP hBitmap;
- BITMAPINFO MyBMInfo;
- BYTE* lpPixels;
- GLubyte* buffer;
- GLubyte* buffer_rgb;
- HDC hCaptureDC;
- tjhandle TurboJCompress;
- float max_clocks = 0;
- extern GLuint LoadTexture();
- extern HBITMAP GetScreenBmp( HDC hdc);
- extern GLuint LoadTexture();
- extern void timer(int value);
- extern void CaptureScreen();
- void reshape(int w, int h)
- {
- w1 = w;
- h1 = h;
- glViewport(0, 0, w, h);
- }
- void orthogonalStart()
- {
- glMatrixMode(GL_PROJECTION);
- glPushMatrix();
- glLoadIdentity();
- gluOrtho2D(-w1/2., w1/2., -h1/2., h1/2.);
- glMatrixMode(GL_MODELVIEW);
- }
- void orthogonalEnd()
- {
- glMatrixMode(GL_PROJECTION);
- glPopMatrix();
- glMatrixMode(GL_MODELVIEW);
- }
- GLuint texture = 0;
- void background()
- {
- glBindTexture( GL_TEXTURE_2D, texture );
- orthogonalStart();
- // texture width/height
- const int iw = MyBMInfo.bmiHeader.biWidth;
- const int ih = MyBMInfo.bmiHeader.biHeight;
- glPushMatrix();
- glTranslatef( -iw/2, -ih/2, 0 );
- glBegin(GL_QUADS);
- glTexCoord2i(0,0); glVertex2i(0, 0);
- glTexCoord2i(1,0); glVertex2i(iw, 0);
- glTexCoord2i(1,1); glVertex2i(iw, ih);
- glTexCoord2i(0,1); glVertex2i(0, ih);
- glEnd();
- glPopMatrix();
- orthogonalEnd();
- }
- void display()
- {
- glClearColor (1.0,0.0,0.0,1.0);
- glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glLoadIdentity();
- glEnable( GL_TEXTURE_2D );
- texture = LoadTexture();
- background();
- //gluLookAt (0, 0, 5, 0, 0, 0, 0, 1, 0);
- glutSwapBuffers();
- }
- GLuint LoadTexture()
- {
- //data = (unsigned char*)malloc(sizeof(unsigned char)*800*600*4);
- //unsigned char *data = new unsigned char[width*height*3];
- glGenTextures( 1, &texture );
- glBindTexture( GL_TEXTURE_2D, texture );
- glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
- glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
- //even better quality, but this will do for now.
- glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_NEAREST);
- glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_NEAREST);
- //to the edge of our shape.
- glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
- glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
- //Generate the texture
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, MyBMInfo.bmiHeader.biWidth, MyBMInfo.bmiHeader.biHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, lpPixels);
- return texture; //return whether it was successful
- }
- HBITMAP GetScreenBmp() {
- // Get screen dimensions
- int nScreenWidth = GetSystemMetrics(SM_CXSCREEN);
- int nScreenHeight = GetSystemMetrics(SM_CYSCREEN);
- // Create compatible DC, create a compatible bitmap and copy the screen using BitBlt()
- hCaptureDC = CreateCompatibleDC(hdc);
- hBitmap = CreateCompatibleBitmap(hdc, nScreenWidth, nScreenHeight);
- HGDIOBJ hOld = SelectObject(hCaptureDC, hBitmap);
- BitBlt(hCaptureDC,0,0,nScreenWidth, nScreenHeight, hdc,0,0,SRCCOPY|CAPTUREBLT);
- SelectObject(hCaptureDC, hOld); // always select the previously selected object once done
- //DeleteDC(hCaptureDC);
- }
- int main(int argc, char **argv)
- {
- //const int WindowWidth = glutGet(GLUT_WINDOW_WIDTH);
- //const int WindowHeight = glutGet(GLUT_WINDOW_HEIGHT);
- //const float PixelperMeter = 1000.f*(float)glutGet(GLUT_SCREEN_HEIGHT)/glutGet(GLUT_SCREEN_HEIGHT_MM);
- TurboJCompress = tjInitCompress();
- hdc = GetDC(0);
- GetScreenBmp();
- MyBMInfo = {0};
- MyBMInfo.bmiHeader.biSize = sizeof(MyBMInfo.bmiHeader);
- // Get the BITMAPINFO structure from the bitmap
- if(0 == GetDIBits(hdc, hBitmap, 0, 0, NULL, &MyBMInfo, DIB_RGB_COLORS)) {
- cout << "error" << endl;
- }
- // create the bitmap buffer
- lpPixels = new BYTE[MyBMInfo.bmiHeader.biSizeImage];
- buffer = new GLubyte[MyBMInfo.bmiHeader.biSizeImage];
- buffer_rgb = new GLubyte[MyBMInfo.bmiHeader.biWidth*MyBMInfo.bmiHeader.biHeight*3];
- // Better do this here - the original bitmap might have BI_BITFILEDS, which makes it
- // necessary to read the color table - you might not want this.
- MyBMInfo.bmiHeader.biCompression = BI_RGB;
- std::thread updatePixels(CaptureScreen);
- //updatePixels.join();
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
- glutInitWindowSize(MyBMInfo.bmiHeader.biWidth, MyBMInfo.bmiHeader.biHeight);
- glutCreateWindow("Aspect Ratio");
- glutDisplayFunc(display);
- glutReshapeFunc(reshape);
- glutTimerFunc(0, timer, 0);
- glutMainLoop();
- DeleteObject(hBitmap);
- ReleaseDC(NULL, hdc);
- delete[] lpPixels;
- return 0;
- }
- void timer(int value) {
- glutPostRedisplay();
- glutTimerFunc(t,timer,0);
- }
- void CaptureScreen() {
- while(true) {
- clock_t t = clock();
- HGDIOBJ hOld = SelectObject(hCaptureDC, hBitmap);
- BitBlt(hCaptureDC,0,0,MyBMInfo.bmiHeader.biWidth, MyBMInfo.bmiHeader.biHeight, hdc,0,0,SRCCOPY|CAPTUREBLT);
- SelectObject(hCaptureDC, hOld);
- //GLubyte* Pixels = new GLubyte[ 3 * MyBMInfo.bmiHeader.biWidth * MyBMInfo.bmiHeader.biHeight ];
- // glReadBuffer(GL_FRONT_AND_BACK);
- //glPixelStorei(GL_PACK_ALIGNMENT, 1);
- //glReadPixels( 0, 0, MyBMInfo.bmiHeader.biWidth, MyBMInfo.bmiHeader.biHeight, GL_RGBA, GL_UNSIGNED_BYTE, lpPixels );
- //glPixelStorei(GL_PACK_ALIGNMENT, 1);
- clock_t t0 = clock();
- if(0 == GetDIBits(hdc, hBitmap, 0, MyBMInfo.bmiHeader.biHeight, (LPVOID)buffer, &MyBMInfo, DIB_RGB_COLORS)) {
- cout << "error2" << endl;
- }
- clock_t t1 = clock();
- int rgb_counter = 0;
- for(DWORD i = 0; i < MyBMInfo.bmiHeader.biSizeImage; i+=4) {
- GLubyte temp = buffer[i]; //B
- buffer[i] = buffer[i+2];
- buffer[i+2] = temp;
- buffer_rgb[rgb_counter++] = buffer[i];
- buffer_rgb[rgb_counter++] = buffer[i+1];
- buffer_rgb[rgb_counter++] = buffer[i+2];
- }
- clock_t t2 = clock();
- GLubyte* compressed_buffer = nullptr;
- unsigned long int compressed_buffer_size = 0;
- if(int error = tjCompress2(TurboJCompress, buffer_rgb,
- MyBMInfo.bmiHeader.biWidth, 0, MyBMInfo.bmiHeader.biHeight,
- TJPF_RGB, &compressed_buffer, &compressed_buffer_size, TJSAMP_444, 75, TJFLAG_FASTDCT) != 0) {
- cout << "Error: " << error << " " << tjGetErrorStr() << endl;
- }
- clock_t t3 = clock();
- memcpy(lpPixels, buffer, MyBMInfo.bmiHeader.biSizeImage);
- /*float temp = (t1 - t)/(float)CLOCKS_PER_SEC;
- if(temp > max_clocks)
- max_clocks = temp;*/
- printf("BitBlt: %.3f GetDIBits: %.3f Reverse: %.3f compress: %.3f compress_percent: %.2f full: %.3f\n",
- (t0-t)/(float)CLOCKS_PER_SEC,
- (t1-t0)/(float)CLOCKS_PER_SEC,
- (t2-t1)/(float)CLOCKS_PER_SEC,
- (t3-t2)/(float)CLOCKS_PER_SEC,
- rgb_counter/(double)compressed_buffer_size,
- (t3-t)/(float)CLOCKS_PER_SEC);
- //cout << temp << " max: " << max_clocks << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement