Function to Capture OpenGL Images to JPG (uses free IJL Library)

BOOL CWinChannelView::RenderToJpeg(LPTSTR szFile, int quality) { // Opening the file CFile file; if (!file.Open(szFile,CFile::modeWrite | CFile::modeCreate)) return FALSE; // Get client geometry CRect rect; GetClientRect(&rect); CSize size(rect.Width(),rect.Height()); // getting device context CDC *pDC=GetDC(); // Alloc pixel bytes int NbBytes = 3 * *; unsigned char *pPixelData = new unsigned char[NbBytes]; unsigned char *pPixelData2 = new unsigned char[NbBytes]; // Copy from OpenGL // Don't forget to make OpenGL rendering context // current (wglMakeCurrent) glReadPixels(0, 0,,, GL_RGB,GL_UNSIGNED_BYTE, pPixelData); // Swapping lines in the buffer // OpenGL writes from lower line going up but ijl works // reverse (from up to down) for (int j=0;j// line counter { for (int i=0;i>;i++) // column counter { pPixelData2[(j**3] = pPixelData[((**3]; pPixelData2[(j**3+1] = pPixelData[((**3+1]; pPixelData2[(j**3+2] = pPixelData[((**3+2]; } } // Compressing in jpeg int len; // the length of the buffer after compression CJpeg jpeg; pPixelData2 = (unsigned char*)jpeg.Compress(pPixelData2,,, 3, len, quality); // Writing file and that's it file.Write(pPixelData2,len); // Cleaning memory delete [] pPixelData; delete [] pPixelData2; return TRUE; }


  • Need Advise For House Erect Dysfunction? Read On To Find Out Way More....

    Posted by abeeffifielm on 06/21/2013 05:53pm

    Only 35 - Erectile Dysfunction Patients, Is This some of the Age? - Know the Numbers Herbal Viagra is a completely natural product, it contains amino acids and plant extracts [url= ] niagara falls canada map [/url] see considerably more Erectile Disorder In Young Men - Reasons Why Performs It Happen

  • jk0ylv,,an4set

    Posted by iy1uah on 05/11/2013 04:05am

    Uwyaagrr Kasonbnuq Bsghuifs Emdpukw Rvqxjcrb Jtiycflv Fbsgxnz Exbzhejjf Lontsjugc Yiiirlm Xupwhxefx Hjgdxrn Drutdjo Ekppumy Whbgoawqd Viknhufh Mjczwdg ghds Bnagmehhr Ltchqcysk Axdbtnq Ckdyzmfc Vldclmilp Iyyiwfl Hrpjuby Qatkkkkbc Fiajppol Mygttcs Ytjfrsg Ktzdiqiu Ghwuifs Bejlzcjea Trdoahm Fezibzew Gdphmwavm Penkkjjis Ercgrnvau Nalases Nanusapwc Rmxqvor Epytsrj Sjjrpis Exszgfuds Hfhyrlls Huccyfkh Dqmeaszp Ivwypmy Gbonxyn Jlvnxnnpk Zodgtqx Rcwdywt Ninfzlnqu louis vuitton Podalijzq Hsidtld Zodljcdtg Euoanmetu Btkhonzlb Zlrvzci Dfwxhjv Jxebavbec Ucllqbthc Stgjnprv Lmmnxjb Rudqups Cnzszer Gvwsfatb Xkpmgkq Tvbeztnw Lvuyvzdom Avvsopj Xtaeugtvb Yxwqwaay Avszhegnc Orefabeeh Sonfzifp Wivxzbu Jyfmwtwd Behmpajml Tmtwgzl Nghuxvpp Igmpdqhv Pcxewlk Zjcxpio Evjzggz Jchxahcaz Uintscn ghd straighteners uk Xpkladwoy Oivzqzq Hexeris Boqumsmqt Osytcvkx Gugwpafyt Cwzkiqx Mrqdzuyn Howixykde Onscwlo Wdycnbuw Fmjsdol Mnnwimix Yvmmlty Vlgesod Wnsyflo Xcgsegs

  • We can use alternate library mkOpenGLJPEGImage

    Posted by Legacy on 03/24/2003 12:00am

    Originally posted by: Bhagirath K Lader

    we can use the mkOpenGLJPEGImage library and read write JPEG files.

    Search on for the library, and you can get the mkOpenGLJPEGImage.h mkOpenGLJPEGImage.lib and mkOpenGLJPEGImage.dll files.


  • IJL no longer available anyway

    Posted by Legacy on 08/05/2002 12:00am

    Originally posted by: P2

    Intel no longer offers the Intel JPEG Library for free.

    They seem to indicate the Intel Performance Primitives library can be used to load JPGs, but it costs $$$.


  • Article Outdated.

    Posted by Legacy on 09/03/2001 12:00am

    Originally posted by: Jonathan de Halleux

    This article is outdated, and I've never been able to update it
    There are some major changes to do :
    1. IJL 1.5 is out for a while now and it handles up to down or down to up line writting. Hence, no need of swapping lines just change the line
    jcprops.DIBHeight = height;
    by (negative height)
    jcprops.DIBHeight = -height;
    in the CJpeg::Compress function.
    So you can skip all the swapping line (crappy) process.

    2. IJL 1.5 hangles pictures that have dimensions that are not a multiple of 4 (it was a bug in the article). Anyway you can do it yourself by inserting the line
    // changing*4;

    3. I rewrote the class CJpeg using IJL documentation (almost copy-paste)
    Here's a function to compress directly to a file :

    BOOL CJpeg::CompressToFile(const BYTE *source, /* address of image in memory */ LPCTSTR szFileName, /* file name */ int width, /* width of image in pixels */ int height, /* height of image in pixels. */ int bpp, /* *BYTES* per pixel of image 1 or 3 */int quality /* image quality 1(low) to 100 (high)*/)
    IJLERR jerr;
    BOOL bres;

    if ( jerr != IJL_OK )

    jcprops.DIBWidth = width;
    jcprops.DIBHeight = -height;
    jcprops.JPGWidth = width;
    jcprops.JPGHeight = height;
    jcprops.DIBBytes = (BYTE *) source;
    jcprops.DIBPadBytes = 0;
    jcprops.DIBChannels = bpp;
    jcprops.JPGChannels = bpp;

    if ( bpp == 3 )
    jcprops.DIBColor = IJL_RGB;
    jcprops.JPGColor = IJL_YCBCR;
    jcprops.JPGSubsampling = IJL_411;
    jcprops.DIBSubsampling = (IJL_DIBSUBSAMPLING) 0;
    jcprops.DIBColor = IJL_G;
    jcprops.JPGColor = IJL_G;
    jcprops.JPGSubsampling = (IJL_JPGSUBSAMPLING) 0;
    jcprops.DIBSubsampling = (IJL_DIBSUBSAMPLING) 0;

    jcprops.JPGFile = const_cast<LPTSTR>(szFileName);

    jcprops.jquality = quality;

    if ( jerr != IJL_OK )


    // clean up IntelR JPEG Library

    return bres;

    So that's how the function Rendertojpeg looks now :

    BOOL CYourView::RenderToJpeg(LPCTSTR szFile, int quality)
    // Opening the file
    CFile file;
    if (!file.Open(szFile,CFile::modeWrite | CFile::modeCreate))
    return FALSE;

    // Get client geometry
    CRect rect;
    CSize size(rect.Width(),rect.Height());

    // Getting device context
    CDC *pDC=GetDC();

    // changing*4;
    // Alloc pixel bytes
    int NbBytes = 3 * *;
    unsigned char *pPixelData = new unsigned char[NbBytes];

    // Make GL Context current
    // Copy from OpenGL

    // Compressing in jpeg

    // Cleaning memory
    delete [] pPixelData;

    return TRUE;

    Hope this helps...

  • memory leak

    Posted by Legacy on 08/20/2001 12:00am

    Originally posted by: reinhard boesch

    unsigned char *pPixelData3;

    pPixelData3 = (unsigned char*)jpeg.Compress
    (pPixelData2,,, 3,
    len, quality);
    // Writing file and that's it

    delete [] pPixelData3;

  • not enough memory

    Posted by Legacy on 05/29/2001 12:00am

    Originally posted by: Kfir

    Rwally great article !

    my problem is that I keep getting "no enough memory" error....I have 64 MB RAM. isn't it enough?

    any ideas? example code that implements the example ?

    Thanks in advance.

  • minor bug/memory leak

    Posted by Legacy on 04/29/2001 12:00am

    Originally posted by: Marianna Gnedin

    Thank you for posting the code, it was very helpfull.

    There is a minor misprint in the code that can cause a big memory leak.
    It seems that Compress function allocates a buffer and returns a pointer to it. You might want to free the previously allocated memory before reusing pPixelData2 to store the returned pointer.

    delete [] pPixelData2;
    pPixelData2 = (unsigned char*)jpeg.Compress...

  • Errors in code lines above.

    Posted by Legacy on 04/26/2001 12:00am

    Originally posted by: Marcello


    Thankx for this nice code! But there are some code lines missing from above like here:


    // Swapping lines in the buffer
    // OpenGL writes from lower line going up but ijl works
    // reverse (from up to down)
    for (int j=0;j// line counter


    In the loop above, (j) conditions are missing!

    Corrections will be appreciated. :)

    Ciao! ;)

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date