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 * size.cx * size.cy; 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, size.cx, size.cy, 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>size.cx;i++) // column counter { pPixelData2[(j*size.cx+i)*3] = pPixelData[((size.cy-1-j)*size.cx+i)*3]; pPixelData2[(j*size.cx+i)*3+1] = pPixelData[((size.cy-1-j)*size.cx+i)*3+1]; pPixelData2[(j*size.cx+i)*3+2] = pPixelData[((size.cy-1-j)*size.cx+i)*3+2]; } } // Compressing in jpeg int len; // the length of the buffer after compression CJpeg jpeg; pPixelData2 = (unsigned char*)jpeg.Compress(pPixelData2, size.cx, size.cy, 3, len, quality); // Writing file and that's it file.Write(pPixelData2,len); // Cleaning memory delete [] pPixelData; delete [] pPixelData2; return TRUE; }


Comments

  • 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=http://rxhealth.pw ] niagara falls canada map [/url] see considerably more Erectile Disorder In Young Men - Reasons Why Performs It Happen

    Reply
  • jk0ylv,http://hoganitonoutlet.net,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 http://cheapghd-uksale.info 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 http://louisvuittonsac-on.com 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 http://ghdukstraighteners.info

    Reply
  • 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 google.com for the library, and you can get the mkOpenGLJPEGImage.h mkOpenGLJPEGImage.lib and mkOpenGLJPEGImage.dll files.

    regards
    Bhagirath

    Reply
  • 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 $$$.

    Buggah.

    Reply
  • 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 size.cx
    size.cx=(size.cx/4)*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)*/)
    {
    JPEG_CORE_PROPERTIES jcprops;
    IJLERR jerr;
    BOOL bres;

    bres=TRUE;
    __try
    {
    jerr=ijlInit(&jcprops);
    if ( jerr != IJL_OK )
    {
    bres=FALSE;
    __leave;
    }

    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;
    }
    else
    {
    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;


    jerr=ijlWrite(&jcprops,IJL_JFILE_WRITEWHOLEIMAGE);
    if ( jerr != IJL_OK )
    {
    bres=FALSE;
    __leave;
    }

    }

    __finally
    {
    // clean up IntelR JPEG Library
    ijlFree(&jcprops);
    }

    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;
    GetClientRect(&rect);
    CSize size(rect.Width(),rect.Height());

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

    // changing size.cx
    size.cx=(size.cx/4)*4;
    // Alloc pixel bytes
    int NbBytes = 3 * size.cx * size.cy;
    unsigned char *pPixelData = new unsigned char[NbBytes];

    // Make GL Context current
    VERIFY(wglMakeCurrent(pDC->m_hDC,m_hGLRC));
    // Copy from OpenGL
    glReadPixels(0,0,size.cx,size.cy,GL_RGB,GL_UNSIGNED_BYTE,pPixelData);

    // Compressing in jpeg
    CPGLJpeg::CompressToFile(pPixelData,szFile,size.cx,size.cy,3,quality);

    // Cleaning memory
    delete [] pPixelData;

    return TRUE;
    }

    Hope this helps...

    Reply
  • 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,
    size.cx, size.cy, 3,
    len, quality);
    // Writing file and that's it
    file.Write(pPixelData3,len);

    delete [] pPixelData3;

    Reply
  • 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.

    Reply
  • 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...

    Reply
  • Errors in code lines above.

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

    Originally posted by: Marcello

    Hi,

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

    <pre>

    // 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
    {

    </pre>

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

    Corrections will be appreciated. :)

    Ciao! ;)

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

Top White Papers and Webcasts

  • IBM Worklight is a mobile application development platform that lets you extend your business to mobile devices. It is designed to provide an open, comprehensive platform to build, run and manage HTML5, hybrid and native mobile apps.

  • New IT trends to support worker mobility — such as VDI and BYOD — are quickly gaining interest and adoption. But just as with any new trend, there are concerns and pitfalls to avoid.  Download this paper to learn the most important considerations to keep in mind for your VDI project.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds