CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Xeon's Avatar
    Xeon is offline Elite Member Power Poster
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    4,195

    Can the Intel JPEG Library help me to load and save JPEG files? Thanks!

    Thanks!

    [b]............[b]
    "Hell is calling for you!" - Rufus, from Valkyrie Profile 2 : Silmeria

    "I'm getting tired of you devils.....finishing strike......Final Blast!" - Arngrim, from Valkyrie Profile 2 : Silmeria

  2. #2
    Join Date
    Oct 2000
    Location
    India
    Posts
    4,620

    Re: Can the Intel JPEG Library help me to load and save JPEG files? Thanks!

    Hi Xeonboy,

    I know that it can load jpeg files, but i dont know if you can save it. If you are interested in getting some really complex source for reading and writing jpeg files, let me know about it. I will send you the source code. All Luck.

    If it helps and if you want to, you can Rate it.
    Visit http://www.geocities.com/contactgirish/homepage.html for some VC++ Links & Notes.All Luck,
    V.Girish

    p.s. If you visit my site, please sign the guest book there. Thank you.
    All luck and have a great day.

    Regards,
    V.Girish

    Visit www.geocities.com/contactgirish for Source code, Tutorials, FAQs and Downloads.

  3. #3
    Join Date
    Mar 2001
    Location
    Singapore
    Posts
    57

    Re: Can the Intel JPEG Library help me to load and save JPEG files? Thanks!

    Hi, the following is what i had for writing and saving JPG using IJL lib version 1.5 (i think).
    Some code does quite specific thing for me, so you will to alter to suit your needs.

    hope it helps.
    Lorba


    #include "IJL.h"

    /*--------------------------------------------------------------
    Save a JPG File
    --------------------------------------------------------------*/
    BOOL CImageWriter::Save_JPG ( void )
    {
    // For correct operation of the T2A macro, see MFC Tech Note 59
    USES_CONVERSION;

    /*--------------------------------------------------------------
    | Validating before saving --------------------------------------------------------------*/
    if ( ! mDest || ! mDest->GetLayer(0)->Valid() )
    return SetErrorMsg (RIFERR_INVALID_LAYER);

    if ( mDest->GetNumLayers() != 1 )
    return SetErrorMsg (RIFERR_NEED_FLATTEN);
    HDIB hDib = mDest->GetLayer(0)->GetHDIB ();


    /*--------------------------------------------------------------
    | Keep a local copy of the filename for usage with the IJL |
    | functions. Note that since IJL does not support Unicode we |
    | must perform the Unicode to Ascii conversion |
    --------------------------------------------------------------*/
    char lpszAsciiFileName [MAX_PATH];
    TCHAR pszFileName [_MAX_PATH];
    _tcscpy (pszFileName, mFilepath);
    strcpy (lpszAsciiFileName, T2A(pszFileName));
    ASSERT (strlen(lpszAsciiFileName));

    // Get a pointer to the DIB memory
    LPBITMAPINFOHEADER lpBI = (LPBITMAPINFOHEADER) ::GlobalLock((HGLOBAL) hDib);
    if ( lpBI == NULL )
    return SetErrorMsg (RIFERR_CANT_LOCK_MEM);

    if ( ! IS_WIN30_DIB(lpBI) )
    {
    ::GlobalUnlock((HGLOBAL) hDib);
    return SetErrorMsg (RIFERR_INV_INTERNAL_FORMAT);
    }

    if ( lpBI->biBitCount != 24 )
    {
    ::GlobalUnlock((HGLOBAL) hDib);
    return SetErrorMsg (RIFERR_MUST_BE_24BIT);
    }

    if ( lpBI->biCompression != BI_RGB )
    {
    ::GlobalUnlock((HGLOBAL) hDib);
    return SetErrorMsg (RIFERR_MUST_NOT_BE_COMPRESS);
    }

    // Init the IJL
    JPEG_CORE_PROPERTIES image;
    ZeroMemory (&image, sizeof(JPEG_CORE_PROPERTIES));
    if ( ijlInit(&image) != IJL_OK )
    {
    ::GlobalUnlock((HGLOBAL) hDib);
    return SetErrorMsg ("Can't initialize Intel JPEG library!");
    }

    // Setup the "image" settings
    image.JPGFile = (LPTSTR) lpszAsciiFileName;
    // LPTSTR cast is required since the IJL header files are prototyped
    // to use a LPTSTR even through Intel have currently only released
    // Ascii versions of IJL as of v1.1
    image.jquality = imgwParam.JpgQuality;// [0...100], best is 100, LIB's default is 75
    image.DIBWidth = lpBI->biWidth;
    image.DIBHeight = lpBI->biHeight;
    image.JPGWidth = lpBI->biWidth;
    image.JPGHeight = lpBI->biHeight;

    // Get a pointer to the DIB bits
    BYTE* pImageData = ((BYTE*)lpBI) + lpBI->biSize;

    // Allocate some memory to save the Dib bits into
    BYTE* pBmp = new BYTE[lpBI->biWidth*lpBI->biHeight*3];
    image.DIBBytes = pBmp;

    // Copy the DIB bits from the DIB into the user buffer
    for ( LONG j=0; j<lpBI->biHeight; j++ )
    {
    int nDepthOutOffset = j*lpBI->biWidth*3;
    int nDepthInOffset = j*WIDTHBYTES(lpBI->biWidth*24);
    for ( LONG i=0; i<lpBI->biWidth; i++ )
    {
    int nInOffset = nDepthInOffset + i*3;
    int nOutOffset = nDepthOutOffset + i*3;

    pBmp[nOutOffset] = pImageData[nInOffset];
    pBmp[nOutOffset+1] = pImageData[nInOffset+1];
    pBmp[nOutOffset+2] = pImageData[nInOffset+2];
    }
    }

    ::GlobalUnlock((HGLOBAL)hDib);

    // Call the IJL to write the Jpeg to file
    if ( ijlWrite(&image, IJL_JFILE_WRITEWHOLEIMAGE) != IJL_OK )
    {
    SetErrorMsg ("[Intel JPEG library] Error writing jpeg image!!");
    delete [] pBmp;
    return FALSE;
    }

    // Finished with IJL
    if ( ijlFree(&image) != IJL_OK )
    SetErrorMsg ("Error freeing Intel JPEG library!");

    delete [] pBmp;
    return TRUE;
    }


    /*--------------------------------------------------------------
    | Reads a JPEG file
    --------------------------------------------------------------*/
    BOOL CImageReader::Read_JPG ( void )
    {
    // For correct operation of the T2A macro, see MFC Tech Note 59
    USES_CONVERSION;

    // Keep a local copy of the filename for usage with the IJL functions.
    // Note that since IJL does not support Unicode we must perform the
    // Unicode to Ascii conversion
    char lpszAsciiFileName[MAX_PATH];
    TCHAR pszFileName[_MAX_PATH];
    _tcscpy(pszFileName, mFilepath);

    strcpy(lpszAsciiFileName, T2A(pszFileName));
    ASSERT(strlen(lpszAsciiFileName));

    // Use the IJL to load up the jpeg
    JPEG_CORE_PROPERTIES image;
    ZeroMemory (&image, sizeof(JPEG_CORE_PROPERTIES));

    // Init the IJL
    if ( ijlInit(&image) != IJL_OK )
    {
    TRACE(_T("Cannot initialize Intel JPEG library!\n"));
    return FALSE;
    }

    // Read in the Jpeg file parameters
    image.JPGFile = (LPTSTR) lpszAsciiFileName; // LPTSTR cast is required since the IJL
    // header files are prototyped to use a
    // LPTSTR even through Intel have currently
    // only released Ascii versions of IJL as of v1.1
    if ( ijlRead(&image, IJL_JFILE_READPARAMS) != IJL_OK )
    {
    TRACE(_T("Cannot read JPEG file header from %s file!\n"), image.JPGFile);
    ijlFree(&image);
    return FALSE;
    }

    // Allocate memory for the image
    DWORD dwImageSize = image.JPGWidth*image.JPGHeight*image.DIBChannels;
    BYTE* pImageData = new BYTE[dwImageSize];

    // Call the IJL to load the Jpeg from file
    image.DIBWidth = image.JPGWidth;
    image.DIBHeight = image.JPGHeight;
    image.DIBBytes = pImageData;
    if ( ijlRead(&image, IJL_JFILE_READWHOLEIMAGE) != IJL_OK )
    {
    TRACE(_T("Cannot read image pImageData from %s file!\n"), image.JPGFile);
    delete [] pImageData;
    ijlFree (&image);
    return FALSE;
    }

    // Finished with IJL
    if ( ijlFree(&image) != IJL_OK )
    TRACE(_T("Cannot free Intel JPEG library!\n"));


    // Initialise
    mWid = image.DIBWidth;
    mHgt = image.DIBHeight;
    mBits = 24;
    mColours = GetNumColour (mBits);
    mScanlineWidth = (mWid*3+((4-((mWid*3)&3))&3));
    mImageSize = mHgt*mScanlineWidth;

    LPBYTE pBmp;

    // Allocate image buffer
    if ( ! (pBmp = new BYTE [mImageSize]) )
    {
    delete [] pImageData;
    return FALSE;
    }

    LPBYTE pTgt = pBmp;
    LPBYTE pSrc = pImageData;
    DWORD SrcWidth = image.JPGWidth*3;
    int Step = (image.JPGHeight > 20) ? (image.JPGHeight / 20) : image.JPGHeight;

    for ( DWORD j=0; j<(DWORD)image.JPGHeight; j++, pTgt+=mScanlineWidth, pSrc+=SrcWidth )
    {
    memcpy (pTgt, pSrc, SrcWidth);
    if ( ! (j % Step) )
    SetProgress (j, image.JPGHeight);
    }

    delete [] pImageData;

    /*----------------------
    | Add Layer |
    ----------------------*/
    BITMAPINFOHEADER& bih = mBmi.bmiHeader;
    PrepareBMPHeader (mWid, mHgt, 24);

    BOOL bSuccess = AddLayer (pBmp, mBmi, bih.biSize);

    if ( bSuccess )
    {
    CDib* Layer = mDest->GetLayer(0);

    Layer->SetOriginalBpp (24);
    Layer->SetOriginalNumColour (COLBPP_24);
    strcpy (mCompressionType, "Lossy Compressed");
    strcpy (mFormatName, "Jpeg");
    }

    delete [] pBmp;
    return bSuccess;
    }





  4. #4
    Xeon's Avatar
    Xeon is offline Elite Member Power Poster
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    4,195

    Re: Can the Intel JPEG Library help me to load and save JPEG files? Thanks!


    Ho ho ho, thanks! Those words touched my heart! How sweet! Now pal.....please don't make me cry! :-( :::::::
    (::::::: == tears)

    Thanks! :-D

    [b]............[b]
    "Hell is calling for you!" - Rufus, from Valkyrie Profile 2 : Silmeria

    "I'm getting tired of you devils.....finishing strike......Final Blast!" - Arngrim, from Valkyrie Profile 2 : Silmeria

  5. #5
    Xeon's Avatar
    Xeon is offline Elite Member Power Poster
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    4,195

    Re: Can the Intel JPEG Library help me to load and save JPEG files? Thanks!


    Hi there! I would like to ask how do I add and integrate the code into my program? Do I just need to include the library's .h file and link it with the library to use it? Thanks!!!!!! :-D

    [b]............[b]
    "Hell is calling for you!" - Rufus, from Valkyrie Profile 2 : Silmeria

    "I'm getting tired of you devils.....finishing strike......Final Blast!" - Arngrim, from Valkyrie Profile 2 : Silmeria

  6. #6
    Xeon's Avatar
    Xeon is offline Elite Member Power Poster
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    4,195

    Re: Can the Intel JPEG Library help me to load and save JPEG files? Thanks!


    Hi there, Lorba! I coded the following and add them as member functions into my SDI-based document class. Even though I've added the ijl.h header file and link it with the ijl15.lib library file, the compiler gave me 43 errors and among the very first of the errors is : mDest is an undeclared identifier! And all of the erros have something to do with undeclared identifiers! Help!
    #include "IJL.h"

    /*--------------------------------------------------------------
    Save a JPG File
    --------------------------------------------------------------*/
    BOOL CMyJPGDoc::Save_JPG ( void )
    {
    // For correct operation of the T2A macro, see MFC Tech Note 59
    USES_CONVERSION;

    /*--------------------------------------------------------------
    | Validating before saving --------------------------------------------------------------*/
    if ( ! mDest || ! mDest->GetLayer(0)->Valid() )
    return SetErrorMsg (RIFERR_INVALID_LAYER);

    if ( mDest->GetNumLayers() != 1 )
    return SetErrorMsg (RIFERR_NEED_FLATTEN);
    HDIB hDib = mDest->GetLayer(0)->GetHDIB ();


    /*--------------------------------------------------------------
    | Keep a local copy of the filename for usage with the IJL |
    | functions. Note that since IJL does not support Unicode we |
    | must perform the Unicode to Ascii conversion |
    --------------------------------------------------------------*/
    char lpszAsciiFileName [MAX_PATH];
    TCHAR pszFileName [_MAX_PATH];
    _tcscpy (pszFileName, mFilepath);
    strcpy (lpszAsciiFileName, T2A(pszFileName));
    ASSERT (strlen(lpszAsciiFileName));

    // Get a pointer to the DIB memory
    LPBITMAPINFOHEADER lpBI = (LPBITMAPINFOHEADER) ::GlobalLock((HGLOBAL) hDib);
    if ( lpBI == NULL )
    return SetErrorMsg (RIFERR_CANT_LOCK_MEM);

    if ( ! IS_WIN30_DIB(lpBI) )
    {
    ::GlobalUnlock((HGLOBAL) hDib);
    return SetErrorMsg (RIFERR_INV_INTERNAL_FORMAT);
    }

    if ( lpBI->biBitCount != 24 )
    {
    ::GlobalUnlock((HGLOBAL) hDib);
    return SetErrorMsg (RIFERR_MUST_BE_24BIT);
    }

    if ( lpBI->biCompression != BI_RGB )
    {
    ::GlobalUnlock((HGLOBAL) hDib);
    return SetErrorMsg (RIFERR_MUST_NOT_BE_COMPRESS);
    }

    // Init the IJL
    JPEG_CORE_PROPERTIES image;
    ZeroMemory (&image, sizeof(JPEG_CORE_PROPERTIES));
    if ( ijlInit(&image) != IJL_OK )
    {
    ::GlobalUnlock((HGLOBAL) hDib);
    return SetErrorMsg ("Can't initialize Intel JPEG library!");
    }

    // Setup the "image" settings
    image.JPGFile = (LPTSTR) lpszAsciiFileName;
    // LPTSTR cast is required since the IJL header files are prototyped
    // to use a LPTSTR even through Intel have currently only released
    // Ascii versions of IJL as of v1.1
    image.jquality = imgwParam.JpgQuality;// [0...100], best is 100, LIB's default is 75
    image.DIBWidth = lpBI->biWidth;
    image.DIBHeight = lpBI->biHeight;
    image.JPGWidth = lpBI->biWidth;
    image.JPGHeight = lpBI->biHeight;

    // Get a pointer to the DIB bits
    BYTE* pImageData = ((BYTE*)lpBI) + lpBI->biSize;

    // Allocate some memory to save the Dib bits into
    BYTE* pBmp = new BYTE[lpBI->biWidth*lpBI->biHeight*3];
    image.DIBBytes = pBmp;

    // Copy the DIB bits from the DIB into the user buffer
    for ( LONG j=0; j<lpBI->biHeight; j++ )
    {
    int nDepthOutOffset = j*lpBI->biWidth*3;
    int nDepthInOffset = j*WIDTHBYTES(lpBI->biWidth*24);
    for ( LONG i=0; i<lpBI->biWidth; i++ )
    {
    int nInOffset = nDepthInOffset + i*3;
    int nOutOffset = nDepthOutOffset + i*3;

    pBmp[nOutOffset] = pImageData[nInOffset];
    pBmp[nOutOffset+1] = pImageData[nInOffset+1];
    pBmp[nOutOffset+2] = pImageData[nInOffset+2];
    }
    }

    ::GlobalUnlock((HGLOBAL)hDib);

    // Call the IJL to write the Jpeg to file
    if ( ijlWrite(&image, IJL_JFILE_WRITEWHOLEIMAGE) != IJL_OK )
    {
    SetErrorMsg ("[Intel JPEG library] Error writing jpeg image!!");
    delete [] pBmp;
    return FALSE;
    }

    // Finished with IJL
    if ( ijlFree(&image) != IJL_OK )
    SetErrorMsg ("Error freeing Intel JPEG library!");

    delete [] pBmp;
    return TRUE;
    }


    /*--------------------------------------------------------------
    | Reads a JPEG file
    --------------------------------------------------------------*/
    BOOL CMyJPGDoc::Read_JPG ( void )
    {
    // For correct operation of the T2A macro, see MFC Tech Note 59
    USES_CONVERSION;

    // Keep a local copy of the filename for usage with the IJL functions.
    // Note that since IJL does not support Unicode we must perform the
    // Unicode to Ascii conversion
    char lpszAsciiFileName[MAX_PATH];
    TCHAR pszFileName[_MAX_PATH];
    _tcscpy(pszFileName, mFilepath);

    strcpy(lpszAsciiFileName, T2A(pszFileName));
    ASSERT(strlen(lpszAsciiFileName));

    // Use the IJL to load up the jpeg
    JPEG_CORE_PROPERTIES image;
    ZeroMemory (&image, sizeof(JPEG_CORE_PROPERTIES));

    // Init the IJL
    if ( ijlInit(&image) != IJL_OK )
    {
    TRACE(_T("Cannot initialize Intel JPEG library!\n"));
    return FALSE;
    }

    // Read in the Jpeg file parameters
    image.JPGFile = (LPTSTR) lpszAsciiFileName; // LPTSTR cast is required since the IJL
    // header files are prototyped to use a
    // LPTSTR even through Intel have currently
    // only released Ascii versions of IJL as of v1.1
    if ( ijlRead(&image, IJL_JFILE_READPARAMS) != IJL_OK )
    {
    TRACE(_T("Cannot read JPEG file header from %s file!\n"), image.JPGFile);
    ijlFree(&image);
    return FALSE;
    }

    // Allocate memory for the image
    DWORD dwImageSize = image.JPGWidth*image.JPGHeight*image.DIBChannels;
    BYTE* pImageData = new BYTE[dwImageSize];

    // Call the IJL to load the Jpeg from file
    image.DIBWidth = image.JPGWidth;
    image.DIBHeight = image.JPGHeight;
    image.DIBBytes = pImageData;
    if ( ijlRead(&image, IJL_JFILE_READWHOLEIMAGE) != IJL_OK )
    {
    TRACE(_T("Cannot read image pImageData from %s file!\n"), image.JPGFile);
    delete [] pImageData;
    ijlFree (&image);
    return FALSE;
    }

    // Finished with IJL
    if ( ijlFree(&image) != IJL_OK )
    TRACE(_T("Cannot free Intel JPEG library!\n"));


    // Initialise
    mWid = image.DIBWidth;
    mHgt = image.DIBHeight;
    mBits = 24;
    mColours = GetNumColour (mBits);
    mScanlineWidth = (mWid*3+((4-((mWid*3)&3))&3));
    mImageSize = mHgt*mScanlineWidth;

    LPBYTE pBmp;

    // Allocate image buffer
    if ( ! (pBmp = new BYTE [mImageSize]) )
    {
    delete [] pImageData;
    return FALSE;
    }

    LPBYTE pTgt = pBmp;
    LPBYTE pSrc = pImageData;
    DWORD SrcWidth = image.JPGWidth*3;
    int Step = (image.JPGHeight > 20) ? (image.JPGHeight / 20) : image.JPGHeight;

    for ( DWORD j=0; j<(DWORD)image.JPGHeight; j++, pTgt+=mScanlineWidth, pSrc+=SrcWidth )
    {
    memcpy (pTgt, pSrc, SrcWidth);
    if ( ! (j % Step) )
    SetProgress (j, image.JPGHeight);
    }

    delete [] pImageData;

    /*----------------------
    | Add Layer |
    ----------------------*/
    BITMAPINFOHEADER& bih = mBmi.bmiHeader;
    PrepareBMPHeader (mWid, mHgt, 24);

    BOOL bSuccess = AddLayer (pBmp, mBmi, bih.biSize);

    if ( bSuccess )
    {
    CDib* Layer = mDest->GetLayer(0);

    Layer->SetOriginalBpp (24);
    Layer->SetOriginalNumColour (COLBPP_24);
    strcpy (mCompressionType, "Lossy Compressed");
    strcpy (mFormatName, "Jpeg");
    }

    delete [] pBmp;
    return bSuccess;
    }




    [b]............[b]
    "Hell is calling for you!" - Rufus, from Valkyrie Profile 2 : Silmeria

    "I'm getting tired of you devils.....finishing strike......Final Blast!" - Arngrim, from Valkyrie Profile 2 : Silmeria

  7. #7
    Xeon's Avatar
    Xeon is offline Elite Member Power Poster
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    4,195

    Re: Can the Intel JPEG Library help me to load and save JPEG files? Thanks!

    Hi there, Girish pal! Ah-yes! I would like the source code for reading and writing JPEG files so that I can load/save JPEG files when the user clicks on the Open and Save menu item respectively! :-D

    Thanks a lot!!!!!

    [b]............[b]
    "Hell is calling for you!" - Rufus, from Valkyrie Profile 2 : Silmeria

    "I'm getting tired of you devils.....finishing strike......Final Blast!" - Arngrim, from Valkyrie Profile 2 : Silmeria

  8. #8
    Join Date
    Mar 2001
    Location
    Singapore
    Posts
    57

    Re: Can the Intel JPEG Library help me to load and save JPEG files? Thanks!

    HI, think u missed out what i typed in the last mail. The code is wholesale from my own program and thus, most would make no sense if you just cut and paste and compile.

    There are a lot of error checking inside, which you can ignore. Just take out the library's initialising/reading functions and the core "for" loop, you will have to do it your way.

    Basically, the library decode the image data into a pixel array, which is 3*width*height in size.
    What i did was merely copy it into my DIB class. Same goes for saving the jpg. You supply the lib with the uncompressed 24bits pixel array and it compress and save it for you.

    Hope this helps! Best of luck!
    lorba


  9. #9
    Xeon's Avatar
    Xeon is offline Elite Member Power Poster
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    4,195

    Re: Can the Intel JPEG Library help me to load and save JPEG files? Thanks!


    Hi there Lorba! Right now, I've made some great progress. I've successfully integrated parts of the JPEGView example included with the Intel JPEG Library into my code, mainly the code in OnOpenDocument() and SaveAsJPEG(). The thing I'm trying to do now is to show the opened JPEG file. That's why I must investigate the code in the view class, whereby after the user clicks the Open button in the file open dialog, the JPEG file is show onto the view. Hope u can tell me where's the code that SHOWS the opened JPEG file onto the view! Thanks!

    [b]............[b]
    "Hell is calling for you!" - Rufus, from Valkyrie Profile 2 : Silmeria

    "I'm getting tired of you devils.....finishing strike......Final Blast!" - Arngrim, from Valkyrie Profile 2 : Silmeria

  10. #10
    Join Date
    Mar 2001
    Location
    Singapore
    Posts
    57

    Re: Can the Intel JPEG Library help me to load and save JPEG files? Thanks!

    Again. the code attached is wholesale from my program... but it should work for just some minor chances... I assume u packed your image data into DIB... if u are not sure about that... there are quite a few good examples in the code area.

    I've actually got the code from some of these examples too.


    BOOL CDib:raw ( CDC* dc, const CRect* rcDst, const CRect* rcSrc, CPalette* pPal )
    {
    CRect DCRect(Rect());
    if ( rcDst )
    DCRect = *rcDst;

    CRect DibRect (Rect());
    if ( rcSrc )
    DibRect = *rcSrc;

    CPalette pal;
    pal.Attach(m_Pal);
    CPalette* pPalette = NULL;

    int Swap = DibRect.top;
    DibRect.top = DibRect.bottom+1;
    DibRect.bottom = Swap+1;

    if ( pPal )
    pPalette = pPal;
    else
    pPalette = &pal;

    BOOL bSuccess = :rawDIB(dc->m_hDC, &DCRect, m_hDib, &DibRect, pPalette);
    pal.Detach();
    return bSuccess;
    }


    /*-------------------------------
    | Macro |
    -------------------------------*/

    /* DIB Macros
    */
    #define IS_WIN30_DIB(lpbi) ((*(LPDWORD)(lpbi)) == sizeof(BITMAPINFOHEADER))
    #define RECTWIDTH(lpRect) ((lpRect)->right - (lpRect)->left)
    #define RECTHEIGHT(lpRect) ((lpRect)->bottom - (lpRect)->top)

    /* WIDTHBYTES performs DWORD-aligning of DIB scanlines. The "bits" parameter is the bit
    count for the scanline (biWidth * biBitCount), and this macro returns the number of
    DWORD-aligned bytes needed to hold those bits.
    */
    #define WIDTHBYTES(bits) (((bits) + 31) / 32 * 4)


    BOOL WINAPI DrawDIB ( HDC hDC,
    LPRECT lpDCRect,
    HDIB hDIB,
    LPRECT lpDIBRect,
    CPalette* pPal )
    {
    /* Determine whether to stretch */
    if ( (RECTWIDTH(lpDCRect) == RECTWIDTH(lpDIBRect) ) &&
    (RECTHEIGHT(lpDCRect) == RECTHEIGHT(lpDIBRect) ) )
    {
    return PaintDIB(hDC,lpDCRect,hDIB,lpDIBRect,pPal,FALSE);
    }
    else
    {
    return PaintDIB(hDC,lpDCRect,hDIB,lpDIBRect,pPal,TRUE);
    }
    }


    BOOL WINAPI PaintDIB ( HDC hDC,
    LPRECT lpDCRect,
    HDIB hDIB,
    LPRECT lpDIBRect,
    CPalette* pPal,
    BOOL bStretch )
    {
    LPSTR lpDIBHdr; // Pointer to BITMAPINFOHEADER
    LPSTR lpDIBBits; // Pointer to DIB bits
    BOOL bSuccess=FALSE; // Success/fail flag
    HPALETTE hPal=NULL; // Our DIB's palette
    HPALETTE hOldPal=NULL; // Previous palette

    /* Check for valid DIB handle */
    if ( hDIB == NULL )
    return FALSE;

    /* Lock down the DIB, and get a pointer to the beginning of the bit
    * buffer
    */
    lpDIBHdr = (LPSTR) ::GlobalLock((HGLOBAL) hDIB);
    lpDIBBits = ::FindDIBBits(lpDIBHdr);

    // Get the DIB's palette, then select it into DC
    if ( pPal != NULL )
    {
    hPal = (HPALETTE) pPal->m_hObject;

    hOldPal = ::SelectPalette(hDC, hPal, FALSE);
    ::RealizePalette(hDC);
    }

    /* Microsoft version always uses ::SetStretchBltMode(hDC, COLORONCOLOR); */
    /* Make sure to use the stretching mode best for color pictures */
    ::SetStretchBltMode(hDC, COLORONCOLOR);

    // Determine whether to call StretchDIBits() or SetDIBitsToDevice() */
    if ( bStretch == FALSE )
    bSuccess = ::SetDIBitsToDevice( hDC, // hDC
    lpDCRect->left, // DestX
    lpDCRect->top, // DestY
    RECTWIDTH(lpDCRect), // nDestWidth
    RECTHEIGHT(lpDCRect), // nDestHeight
    lpDIBRect->left, // SrcX
    (int)DIBHeight(lpDIBHdr) -
    lpDIBRect->top -
    RECTHEIGHT(lpDIBRect), // SrcY
    0, // nStartScan
    (WORD)DIBHeight(lpDIBHdr), // nNumScans
    lpDIBBits, // lpBits
    (LPBITMAPINFO)lpDIBHdr, // lpBitsInfo
    DIB_RGB_COLORS); // wUsage
    else
    {
    bSuccess = ::StretchDIBits ( hDC, // hDC
    lpDCRect->left, // DestX
    lpDCRect->top, // DestY
    RECTWIDTH(lpDCRect), // nDestWidth
    RECTHEIGHT(lpDCRect), // nDestHeight
    lpDIBRect->left, // SrcX
    lpDIBRect->top, // SrcY
    RECTWIDTH(lpDIBRect), // wSrcWidth
    RECTHEIGHT(lpDIBRect), // wSrcHeight
    lpDIBBits, // lpBits
    (LPBITMAPINFO)lpDIBHdr, // lpBitsInfo
    DIB_RGB_COLORS, // wUsage
    SRCCOPY); // dwROP
    }
    ::GlobalUnlock((HGLOBAL) hDIB);

    /* Reselect old palette */
    if ( hOldPal != NULL )
    {
    ::SelectPalette(hDC, hOldPal,FALSE);
    ::RealizePalette(hDC);
    }

    return bSuccess;
    }






Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured