【VC开源代码栏目提醒】:网学会员在VC开源代码频道为大家收集整理了DIBSectionLite.cpp提供大家参考,希望对大家有所帮助!
// CDIBSectionLite.cpp : implementation file
//
#include "DIBSectionLite.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// Standard colours
PALETTEENTRY CDIBSectionLite::ms_StdColours[] = {
{ 0x00, 0x00, 0x00, 0 }, // First 2 will be palette for monochrome bitmaps
{ 0xFF, 0xFF, 0xFF, 0 },
{ 0x00, 0x00, 0xFF, 0 }, // First 16 will be palette for 16 colour bitmaps
{ 0x00, 0xFF, 0x00, 0 },
{ 0x00, 0xFF, 0xFF, 0 },
{ 0xFF, 0x00, 0x00, 0 },
{ 0xFF, 0x00, 0xFF, 0 },
{ 0xFF, 0xFF, 0x00, 0 },
{ 0x00, 0x00, 0x80, 0 },
{ 0x00, 0x80, 0x00, 0 },
{ 0x00, 0x80, 0x80, 0 },
{ 0x80, 0x00, 0x00, 0 },
{ 0x80, 0x00, 0x80, 0 },
{ 0x80, 0x80, 0x00, 0 },
{ 0x80, 0x80, 0x80, 0 },
{ 0xC0, 0xC0, 0xC0, 0 },
};
/////////////////////////////////////////////////////////////////////////////
// CDIBSectionLite static functions
// --- In?: nBitsPerPixel - bits per pixel
// --- Out :
// --- Returns :The number of colours for this colour depth
// --- Effect : Returns the number of color table entries given the number
// of bits per pixel of a bitmap
/*static*/ int CDIBSectionLite::NumColorEntries(int nBitsPerPixel)
{
int nColors = 0;
switch (nBitsPerPixel)
{
case 1: nColors = 2; break;
case 4: nColors = 16; break;
case 8: nColors = 256; break;
case 16:
case 24:
case 32: nColors = 0; break; // 16,24 or 32 bpp have no color table
default:
ASSERT(FALSE);
}
return nColors;
}
// --- In?: nWidth - image width in pixels
// nBitsPerPixel - bits per pixel
// --- Out :
// --- Returns : Returns the number of storage bytes needed for each scanline
// in the bitmap
// --- Effect :
/*static*/ int CDIBSectionLite::BytesPerLine(int nWidth, int nBitsPerPixel)
{
int nBytesPerLine = nWidth * nBitsPerPixel;
nBytesPerLine = ( (nBytesPerLine + 31) & (~31) ) / 8;
return nBytesPerLine;
}
// --- In?: palette - reference to a palette object which will be filled
// nNumColours - number of colour entries to fill
// --- Out :
// --- Returns : TRUE on success, false otherwise
// --- Effect : Creates a halftone color palette independant of screen color depth.
// palette will be filled with the colors, and nNumColours is the No.
// of colors to file. If nNumColoursis 0 or > 256, then 256 colors are used.
/*static*/ BOOL CDIBSectionLite::CreateHalftonePalette(CPalette& palette, int nNumColours)
{
palette.DeleteObject();
int nNumStandardColours = sizeof(ms_StdColours) / sizeof(ms_StdColours[0]);
int nIndex = 0;
int nNumEntries = nNumColours;
if (nNumEntries <= 0 || nNumEntries > 256)
nNumEntries = 256;
PALETTEINFO PalInfo;
PalInfo.palNumEntries = (WORD) nNumEntries;
// The standard colours (16)
for (int i = 0; i < nNumStandardColours; i++)
{
if (nIndex >= nNumEntries) break;
memcpy( &(PalInfo.palPalEntry[nIndex]), &(ms_StdColours[i]), sizeof(PALETTEENTRY) );
nIndex++;
}
// A colour cube (6 divisions = 216)
for (int blue = 0; blue <= 5; blue++)
for (int green = 0; green <= 5; green++)
for (int red = 0; red <= 5; red++)
{
if (nIndex >= nNumEntries)
break;
PalInfo.palPalEntry[nIndex].peRed = (BYTE) ((red*255)/5);
PalInfo.palPalEntry[nIndex].peGreen = (BYTE) ((green*255)/5);
PalInfo.palPalEntry[nIndex].peBlue = (BYTE) ((blue*255)/5);
PalInfo.palPalEntry[nIndex].peFlags = 0;
nIndex++;
}
// A grey scale (24 divisions)
for (int grey = 0; grey <= 23; grey++)
{
if (nIndex >= nNumEntries)
break;
PalInfo.palPalEntry[nIndex].peRed = (BYTE) (grey*255/23);
PalInfo.palPalEntry[nIndex].peGreen = (BYTE) (grey*255/23);
PalInfo.palPalEntry[nIndex].peBlue = (BYTE) (grey*255/23);
PalInfo.palPalEntry[nIndex].peFlags = 0;
nIndex++;
}
return palette.CreatePalette((LPLOGPALETTE) PalInfo);
}
/////////////////////////////////////////////////////////////////////////////
// CDIBSectionLite
CDIBSectionLite::CDIBSectionLite()
{
m_hBitmap = NULL;
m_bDither = FALSE;
m_hDrawDib = NULL;
DeleteObject(); // This will initialise to a known state - ie. empty
}
CDIBSectionLite::~CDIBSectionLite()
{
DeleteObject();
}
// --- In?:
// --- Out :
// --- Returns :
// --- Effect : Resets the object to an empty state, and frees all memory used.
void CDIBSectionLite::DeleteObject()
{
if (m_hBitmap)
::DeleteObject(m_hBitmap);
m_hBitmap = NULL;
m_ppvBits = NULL;
memset(&m_DIBinfo, 0, sizeof(m_DIBinfo));
if (m_hDrawDib)
DrawDibClose(m_hDrawDib);
m_hDrawDib = NULL;
m_iColorDataType = DIB_RGB_COLORS;
m_iColorTableSize = 0;
}
/////////////////////////////////////////////////////////////////////////////
// CDIBSectionLite diagnostics
#ifdef _DEBUG
void CDIBSectionLite::AssertValid() const
{
ASSERT(m_hBitmap);
DIBSECTION ds;
DWORD dwSize = GetObject( m_hBitmap, sizeof(DIBSECTION), &ds );
ASSERT(dwSize == sizeof(DIBSECTION));
ASSERT(0 <= m_iColorTableSize && m_iColorTableSize <= 256);
CObject::AssertValid();
}
void CDIBSectionLite::Dump(CDumpContext& dc) const
{
CObject::Dump(dc);
}
#endif //_DEBUG
// --- In?: bDither - whether or not dithering should be enabled
// --- Out :
// --- Returns : TRUE on success
// --- Effect : Turns dithering on by using the DrawDib functions instead of
// the GDI functions
BOOL CDIBSectionLite::SetDither(BOOL bDither)
{
BOOL bResult = TRUE;
// Check for a no-change op.
if ( (m_bDither == bDither) &&
((m_hDrawDib && bDither) || (!m_hDrawDib && !bDither)) )
return bResult;
if (m_hDrawDib != NULL)
{
DrawDibClose(m_hDrawDib);
m_hDrawDib = NULL;
}
if (bDither)
{
if ( (m_hDrawDib = DrawDibOpen()) == NULL )
bResult = FALSE;
}
m_bDither = (m_hDrawDib != NULL);
return bResult;
}
// --- In?:
// --- Out :
// --- Returns : TRUE if dithering is used
// --- Effect : Returns whether or not the DrawDib functions (and hence dithering)
// is being used.
BOOL CDIBSectionLite::GetDither()
{
return (m_bDither && (m_hDrawDib != NULL));
}
/////////////////////////////////////////////////////////////////////////////
// CDIBSectionLite operations
// --- In?: pDC - Pointer to a device context
// ptDest - point at which the topleft corner of the image is drawn
// bForceBackground - Specifies whether the palette is forced to be
// a background palette
// --- Out :
// --- Returns : TRUE on success
// --- Effect : Draws the image 1:1 on the device context
BOOL CDIBSectionLite::Draw(CDC* pDC, CPoint ptDest, BOOL bForceBackground /*=FALSE*/ )
{
if (!m_hBitmap)
return FALSE;
CSize size = GetSize();
CPoint SrcOrigin = CPoint(0,0);
BOOL bResult = FALSE;
if (GetDither() && GetBitCount() >= 8)
{
DrawDibSetPalette( m_hDrawDib, (HPALETTE)m_Palette);
DrawDibRealize( m_hDrawDib, pDC->GetSafeHdc(), bForceBackground);
bResult = DrawDibDraw(m_hDrawDib, pDC->GetSafeHdc(),
ptDest.x, ptDest.y, size.cx, size.cy,
GetBitmapInfoHeader(), GetDIBits(),
SrcOrigin.x, SrcOrigin.y, size.cx, size.cy,
0/*DDF_HALFTONE*/);
}
else
{
CPalette* pOldPalet
上一篇:
DibSection.cpp
下一篇:
谈谈初学者怎样突破英语音标