75         m_hDC = CreateCompatibleDC(NULL);
 
   78                 printf(
"ERROR: couldn't allocate DC\n");
 
   85         bminfo.bmiHeader.biSize = 
sizeof(BITMAPINFOHEADER);
 
   86         bminfo.bmiHeader.biWidth = width;
 
   87         bminfo.bmiHeader.biHeight = height;
 
   88         bminfo.bmiHeader.biPlanes = 1;
 
   89         bminfo.bmiHeader.biBitCount = 24;
 
   90         bminfo.bmiHeader.biCompression = BI_RGB;
 
   91         bminfo.bmiHeader.biSizeImage = 0; 
 
   92         bminfo.bmiHeader.biXPelsPerMeter = 1;
 
   93         bminfo.bmiHeader.biYPelsPerMeter = 1;
 
   94         bminfo.bmiHeader.biClrUsed = 0;
 
   95         bminfo.bmiHeader.biClrImportant = 0;
 
   97         m_hBmp = CreateDIBSection(m_hDC, &bminfo, DIB_RGB_COLORS, (
void**)&m_pPixels, NULL, 0);
 
  100                 printf(
"ERROR: couldn't allocate Bitmap\n");
 
  108         m_hBmpOld = (HBITMAP)SelectObject(m_hDC, m_hBmp);
 
  110         PIXELFORMATDESCRIPTOR pfd = { 
 
  111                 sizeof(PIXELFORMATDESCRIPTOR),   
 
  133         iPixelFormat = ChoosePixelFormat(m_hDC, &pfd); 
 
  136         BOOL bResult = SetPixelFormat(m_hDC, iPixelFormat, &pfd);
 
  139                 printf(
"ERROR: SetPixelFormat failed\n");
 
  141                 SelectObject(m_hDC, m_hBmpOld);
 
  142                 DeleteObject(m_hBmp);
 
  149         m_hGLRC = wglCreateContext(m_hDC);
 
  152                 printf(
"ERROR: couldn't allocate HGLRC\n");
 
  154                 SelectObject(m_hDC, m_hBmpOld);
 
  155                 DeleteObject(m_hBmp);
 
  162         wglMakeCurrent(m_hDC, m_hGLRC);
 
  174                 wglMakeCurrent(NULL, NULL);
 
  176                 wglDeleteContext(m_hGLRC);
 
  179                 SelectObject(m_hDC, m_hBmpOld);
 
  182                 DeleteObject(m_hBmp);
 
  193                 wglMakeCurrent(m_hDC, m_hGLRC);
 
  198         wglMakeCurrent(NULL, NULL);
 
  206         if (pImage->
width != m_nWidth || pImage->
height != m_nHeight)
 
  208                 printf(
"ERROR: CGLContext::GetImage: Image dimensions do not match (%d, %d) vs. (%d, %d)\n", pImage->
width, pImage->
height, m_nWidth, m_nHeight);
 
  216                 unsigned char *in = m_pPixels + 3 * m_nWidth * (m_nHeight-1);
 
  217                 unsigned char *out = pImage->
pixels;
 
  219                 for (
int y = 0; y < m_nHeight; y++, in -= 2*3*m_nWidth)
 
  221                         for (
int x = 0; x < m_nWidth; x++, out += 3, in += 3)
 
  232                 unsigned char *in = m_pPixels + 3 * m_nWidth * (m_nHeight-1);
 
  233                 unsigned char *out = pImage->
pixels;
 
  235                 for (
int y = 0; y < m_nHeight; y++, in -= 2*3*m_nWidth)
 
  237                         for (
int x = 0; x < m_nWidth; x++, out += 3, in += 3)
 
  239                                 *out = (in[0] + in[1] + in[2]) / 3;
 
  253 #include <qapplication.h> 
  259 class MyQGLContext : 
public QGLContext
 
  262         MyQGLContext(
const QGLFormat &format, QPaintDevice *device)
 
  263         : QGLContext(format, device)
 
  266         virtual ~MyQGLContext()
 
  277 : m_pApp(NULL), m_pPixmap(NULL), m_pGLContext(NULL)
 
  285 static char app_name[] = 
"IVT_APPLICATION\0";
 
  286 static int my_argc = 1;
 
  287 static char *my_argv[2] = { app_name, NULL};
 
  294                 m_pApp = 
new QApplication(my_argc, my_argv);
 
  297         m_pPixmap = 
new QPixmap(width, height);
 
  299         QGLFormat f = QGLFormat::defaultFormat();
 
  302         m_pGLContext = 
new MyQGLContext(f, m_pPixmap);
 
  304         m_pGLContext->create((QGLContext*)shareContext);
 
  305         if (!m_pGLContext->isValid())
 
  307                 printf(
"ERROR: CGLContext::CreateContext: couldn't create GLContext!\n");
 
  311         if (shareContext != NULL && !m_pGLContext->isSharing())
 
  313                 printf(
"WARNING: CGLContext::CreateContext: sharing of GLContext failed!\n");
 
  316         m_pGLContext->makeCurrent();
 
  353                 m_pGLContext->makeCurrent();
 
  361                 m_pGLContext->clearCurrent();
 
  371         int width = m_pPixmap->width();
 
  372         int height = m_pPixmap->height();
 
  374         if (pImage->
width != width || pImage->
height != height)
 
  376                 printf(
"ERROR: CGLContext::GetImage: Image dimensions do not match (%d, %d) vs. (%d, %d)\n", pImage->
width, pImage->
height, width, height);
 
  381         m_pGLContext->swapBuffers();
 
  385                 glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, pImage->
pixels);
 
  387                 unsigned char *tmp = 
new unsigned char[3*width];
 
  389                 for (
int i = 0; i < (height/2); i++)
 
  391                         memcpy(tmp, &pImage->
pixels[3*i*width], 3*width);
 
  392                         memcpy(&pImage->
pixels[3*i*width], &pImage->
pixels[3*(height - i - 1)*width], 3*width);
 
  393                         memcpy(&pImage->
pixels[3*(height - i - 1)*width], tmp, 3*width);
 
  400                 glReadPixels(0, 0, width, height, GL_LUMINANCE, GL_UNSIGNED_BYTE, pImage->
pixels);
 
  402                 unsigned char *tmp = 
new unsigned char[width];
 
  404                 for (
int i = 0; i < (height/2); i++)
 
  406                         memcpy(tmp, &pImage->
pixels[i*width], width);
 
  407                         memcpy(&pImage->
pixels[i*width], &pImage->
pixels[(height - i - 1)*width], width);
 
  408                         memcpy(&pImage->
pixels[(height - i - 1)*width], tmp, width);
 
  430         void* CocoaCreateGLContext(
int width, 
int height, 
unsigned char* buffer);
 
  431         void CocoaDeleteGLContext(
void* ptr);
 
  432         void CocoaMakeCurrentGLContext(
void* ptr);
 
  433         void CocoaDoneCurrentGLContext(
void* ptr);
 
  434         void CocoaSwapBuffersGLContext(
void* ptr);
 
  438 : m_pGLContext(NULL), m_pBuffer(NULL)
 
  453         m_pBuffer = 
new unsigned char[width*height*4];
 
  455         m_pGLContext = CocoaCreateGLContext(width, height, m_pBuffer);
 
  475                 CocoaDeleteGLContext(m_pGLContext);
 
  488                 CocoaMakeCurrentGLContext(m_pGLContext);
 
  496                 CocoaDoneCurrentGLContext(m_pGLContext);
 
  506         if (pImage->
width != m_nWidth || pImage->
height != m_nHeight)
 
  508                 printf(
"error: CGLContext::GetImage: image dimensions do not match dimensions of the GL context!\n");
 
  513         CocoaSwapBuffersGLContext(m_pGLContext);
 
  517                 glReadPixels(0, 0, m_nWidth, m_nHeight, GL_RGB, GL_UNSIGNED_BYTE, pImage->
pixels);
 
  519                 unsigned char *pixels = pImage->
pixels;
 
  520                 unsigned char *tmp = 
new unsigned char[3*m_nWidth];
 
  522                 for (
int i = 0; i < (m_nHeight/2); i++)
 
  524                         memcpy(tmp, &pixels[3*i*m_nWidth], 3*m_nWidth);
 
  525                         memcpy(&pixels[3*i*m_nWidth], &pixels[3*(m_nHeight - i - 1)*m_nWidth], 3*m_nWidth);
 
  526                         memcpy(&pixels[3*(m_nHeight - i - 1)*m_nWidth], tmp, 3*m_nWidth);
 
  533                 glReadPixels(0, 0, m_nWidth, m_nHeight, GL_LUMINANCE, GL_UNSIGNED_BYTE, pImage->
pixels);
 
  535                 unsigned char *tmp = 
new unsigned char[m_nWidth];
 
  537                 for (
int i = 0; i < (m_nHeight/2); i++)
 
  539                         memcpy(tmp, &pImage->
pixels[i*m_nWidth], m_nWidth);
 
  540                         memcpy(&pImage->
pixels[i*m_nWidth], &pImage->
pixels[(m_nHeight - i - 1)*m_nWidth], m_nWidth);
 
  541                         memcpy(&pImage->
pixels[(m_nHeight - i - 1)*m_nWidth], tmp, m_nWidth);
 
  560 : m_pXDisplay(NULL), m_glxpixmap(0), m_glxcontext(0)
 
  571         m_pXDisplay = XOpenDisplay(NULL);
 
  574                 printf(
"CGLContext::CreateContext: couldn't open XServer connection.\n");
 
  578         if (glXQueryExtension(m_pXDisplay, NULL, NULL) == False)
 
  580                 printf(
"CGLContext::CreateContext: no OpenGL support.\n");
 
  587         int attrlist[] = {GLX_RGBA, GLX_DOUBLEBUFFER, GLX_DEPTH_SIZE, 16, None};
 
  589         XVisualInfo *vi = glXChooseVisual(m_pXDisplay, DefaultScreen(m_pXDisplay), attrlist);
 
  592                 printf(
"CGLContext::CreateContext: couldn't find a suitable Visual\n");
 
  596         m_pFrontBuffer = XCreatePixmap(m_pXDisplay, RootWindow(m_pXDisplay, vi->screen), m_nWidth, m_nHeight, vi->depth);
 
  601                 printf(
"CGLContext::CreateContext: XCreatePixmap failed.\n");
 
  605         m_glxpixmap = glXCreateGLXPixmap(m_pXDisplay, vi, m_pFrontBuffer);
 
  606         if (m_glxpixmap == None)
 
  610                 printf(
"CGLContext::CreateContext: glXCreateGLXPixmap failed.\n");
 
  615                 m_glxcontext = glXCreateContext(m_pXDisplay, vi, (GLXContext)shareContext, False);
 
  617                 m_glxcontext = glXCreateContext(m_pXDisplay, vi, 0, False);
 
  619         if (m_glxcontext == NULL)
 
  623                 printf(
"CGLContext::CreateContext: glXCreateContext failed.\n");
 
  644         if (m_glxcontext == glXGetCurrentContext())
 
  645                 glXMakeCurrent(m_pXDisplay, None, NULL);
 
  647         if (m_glxpixmap != None) {
 
  648                 glXDestroyGLXPixmap(m_pXDisplay, m_glxpixmap);
 
  653         if (m_pFrontBuffer) {
 
  654                 XFreePixmap(m_pXDisplay, m_pFrontBuffer);
 
  661                 glXDestroyContext(m_pXDisplay, m_glxcontext);
 
  667                 XCloseDisplay(m_pXDisplay);
 
  678                 if (m_glxcontext != glXGetCurrentContext())
 
  679                         glXMakeCurrent(m_pXDisplay, m_glxpixmap, m_glxcontext);
 
  687         glXMakeCurrent(m_pXDisplay, None, NULL);
 
  697         if (pImage->
width != m_nWidth || pImage->
height != m_nHeight)
 
  699                 printf(
"ERROR: CGLContext::GetImage: Image dimensions do not match (%d, %d) vs. (%d, %d)\n", pImage->
width, pImage->
height, m_nWidth, m_nHeight);
 
  704         glXSwapBuffers(m_pXDisplay, m_glxpixmap);
 
  708                 glReadPixels(0, 0, m_nWidth, m_nHeight, GL_RGB, GL_UNSIGNED_BYTE, pImage->
pixels);
 
  710                 unsigned char *tmp = 
new unsigned char[3*m_nWidth];
 
  712                 for (
int i = 0; i < (m_nHeight/2); i++)
 
  714                         memcpy(tmp, &pImage->
pixels[3*i*m_nWidth], 3*m_nWidth);
 
  715                         memcpy(&pImage->
pixels[3*i*m_nWidth], &pImage->
pixels[3*(m_nHeight - i - 1)*m_nWidth], 3*m_nWidth);
 
  716                         memcpy(&pImage->
pixels[3*(m_nHeight - i - 1)*m_nWidth], tmp, 3*m_nWidth);
 
  723                 glReadPixels(0, 0, m_nWidth, m_nHeight, GL_LUMINANCE, GL_UNSIGNED_BYTE, pImage->
pixels);
 
  725                 unsigned char *tmp = 
new unsigned char[m_nWidth];
 
  727                 for (
int i = 0; i < (m_nHeight/2); i++)
 
  729                         memcpy(tmp, &pImage->
pixels[i*m_nWidth], m_nWidth);
 
  730                         memcpy(&pImage->
pixels[i*m_nWidth], &pImage->
pixels[(m_nHeight - i - 1)*m_nWidth], m_nWidth);
 
  731                         memcpy(&pImage->
pixels[(m_nHeight - i - 1)*m_nWidth], tmp, m_nWidth);
 
bool CreateContext(int width, int height, void *shareContext=0)
bool GetImage(CByteImage *pImage)
ImageType type
The type of the image. 
int width
The width of the image in pixels. 
int height
The height of the image in pixels. 
unsigned char * pixels
The pointer to the the pixels. 
Data structure for the representation of 8-bit grayscale images and 24-bit RGB (or HSV) color images ...