|
///////////////////////////////////////////////////////////////////////////////
//
// CDIBUse
// -------
// Handle a DIB
//
////Holger Kloos, 2003/////////////////////////////////////////////////////////
#include "stdafx.h"
#include "DIBUse.h"
// CDIBUse
// -------
CDIBUse::CDIBUse()
{
m_hDIB = NULL;
}
CDIBUse::~CDIBUse()
{
FreeDIB();
}
void CDIBUse::FreeDIB()
{
if (m_hDIB)
{
GlobalFree(m_hDIB);
m_hDIB = NULL;
}
}
BOOL CDIBUse::SetDIB(HDIB hDIB)
{
FreeDIB();
m_hDIB = hDIB;
return m_hDIB != NULL;
}
BOOL CDIBUse::Detach()
{
if (!m_hDIB)
return FALSE;
m_hDIB = NULL;
return TRUE;
}
BOOL CDIBUse::ConvertToBMP(const BITMAPINFO* pDIB, CDC& rDC, CBitmap& rBMP) const
{
const BITMAPINFOHEADER& bih = pDIB->bmiHeader;
if (rBMP.m_hObject)
rBMP.DeleteObject();
if (!rBMP.CreateCompatibleBitmap(&rDC, bih.biWidth, bih.biHeight))
return FALSE;
rBMP.SetBitmapDimension(bih.biWidth, bih.biHeight);
const BYTE* pData = (BYTE*)pDIB->bmiColors + CalcPaletteSize(bih.biBitCount);
return SetDIBits(rDC, rBMP, 0, bih.biHeight, pData, pDIB, DIB_RGB_COLORS);
}
BOOL CDIBUse::ConvertToBMP(CDC& rDC, CBitmap& rBMP) const
{
BITMAPINFO* pDIB = m_hDIB ? (BITMAPINFO*)GlobalLock(m_hDIB) : NULL;
if (!pDIB)
return FALSE;
BOOL bRes = ConvertToBMP(pDIB, rDC, rBMP);
GlobalUnlock(m_hDIB);
return bRes;
}
int CDIBUse::CalcPaletteSize(WORD nColBits) const
{
switch (nColBits)
{
case 1:
return (2 * sizeof(RGBQUAD));
case 4:
return (16 * sizeof(RGBQUAD));
case 8:
return (256 * sizeof(RGBQUAD));
default:
return 0; // a 24 bitcount DIB has no color table
}
}
BOOL CDIBUse::FillFileHeader(BITMAPFILEHEADER& rHeader) const
{
if (!IsValid())
return FALSE;
BITMAPINFO* pDIB = (BITMAPINFO*)GlobalLock(m_hDIB);
if (!pDIB)
return FALSE;
rHeader.bfType = MARK_BMP;
rHeader.bfSize = GlobalSize (m_hDIB) + sizeof (BITMAPFILEHEADER);
rHeader.bfReserved1 = 0;
rHeader.bfReserved2 = 0;
rHeader.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + pDIB->bmiHeader.biSize + CalcPaletteSize(pDIB->bmiHeader.biBitCount);
GlobalUnlock(m_hDIB);
return TRUE;
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.