📄 cdib.cpp
字号:
// CDIBSectionLite.cpp : implementation file
//
// General purpose DIBsection wrapper class for Win9x, NT 4.0, W2K and WinCE.
//
// Author : Chris Maunder (cmaunder@mail.com)
// Date : 17 May 1999
//
// Copyright ?Dundas Software Ltd. 1999, All Rights Reserved
//
// This code may be used in compiled form in any way you desire. This
// file may be redistributed unmodified by any means PROVIDING it is
// not sold for profit without the authors written consent, and
// providing that this notice and the authors name is included. If
// the source code in this file is used in any commercial application
// then a simple email would be nice.
//
// This file is provided "as is" with no expressed or implied warranty.
// The author accepts no liability for any damage, in any form, caused
// by this code. Use it at your own risk and as with all code expect bugs!
// It's been tested but I'm not perfect.
//
// Please use and enjoy. Please let me know of any bugs/mods/improvements
// that you have found/implemented and I will fix/incorporate them into this
// file.
//
// History : 25 May 1999 - First release
// 4 Jun 1999 - Fixed SetBitmap bug
// 4 May 2000 - 16 or 32 bit compression bug fix (Jim Miller <jam@3dfx.com>)
// Bug fix in Save() (saving 4 bytes too many - Tadeusz Dracz)
// 12 Dec 2000 - KennyG@magenic.com - Massive MFC extraxtion, remove that bloat!
//
//#include <stdafx.h>
#include <windows.h>
#include "CDIB.h"
#define CDIB_DEBUG_MSG 1
#define CDIB_ERROR_MSG 1
// Standard colors
RGBQUAD CDIB::ms_StdColors[] = {
{ 0x00, 0x00, 0x00, 0 }, // System palette - first 10 colors
{ 0x80, 0x00, 0x00, 0 },
{ 0x00, 0x80, 0x00, 0 },
{ 0x80, 0x80, 0x00, 0 },
{ 0x00, 0x00, 0x80, 0 },
{ 0x80, 0x00, 0x80, 0 },
{ 0x00, 0x80, 0x80, 0 },
{ 0xC0, 0xC0, 0xC0, 0 },
{ 0xC0, 0xDC, 0xC0, 0 },
{ 0xA6, 0xCA, 0xF0, 0 },
{ 0x2C, 0x00, 0x00, 0 },
{ 0x56, 0x00, 0x00, 0 },
{ 0x87, 0x00, 0x00, 0 },
{ 0xC0, 0x00, 0x00, 0 },
{ 0xFF, 0x00, 0x00, 0 },
{ 0x00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 0x5F, 0x5F, 0x5F, 0 },
{ 0x69, 0x69, 0x69, 0 },
{ 0x72, 0x72, 0x72, 0 },
{ 0x7D, 0x7D, 0x7D, 0 },
{ 0x92, 0x92, 0x92, 0 },
{ 0x9D, 0x9D, 0x9D, 0 },
{ 0xA8, 0xA8, 0xA8, 0 },
{ 0xB4, 0xB4, 0xB4, 0 },
{ 0xCC, 0xCC, 0xCC, 0 },
{ 0xD8, 0xD8, 0xD8, 0 },
{ 0xE5, 0xE5, 0xE5, 0 },
{ 0xF2, 0xF2, 0xF2, 0 },
{ 0xFF, 0xFF, 0xFF, 0 },
{ 0xFF, 0xFB, 0xF0, 0 }, // System palette - last 10 colors
{ 0xA0, 0xA0, 0xA4, 0 },
{ 0x80, 0x80, 0x80, 0 },
{ 0xFF, 0x00, 0x00, 0 },
{ 0x00, 0xFF, 0x00, 0 },
{ 0xFF, 0xFF, 0x00, 0 },
{ 0x00, 0x00, 0xFF, 0 },
{ 0xFF, 0x00, 0xFF, 0 },
{ 0x00, 0xFF, 0xFF, 0 },
{ 0xFF, 0xFF, 0xFF, 0 },
};
/////////////////////////////////////////////////////////////////////////////
// CE DIBSection global functions
#ifdef _WIN32_WCE
UINT CEGetDIBColorTable(HDC hdc, UINT uStartIndex, UINT cEntries, RGBQUAD *pColors);
#endif
/////////////////////////////////////////////////////////////////////////////
// CDIB static functions
//
// --- In?: nBitsPerPixel - bits per pixel
// nCompression - type of compression
// --- Out :
// --- Returns :The number of colors for this color depth
// --- Effect : Returns the number of color table entries given the number
// of bits per pixel of a bitmap
/*static*/ int CDIB::NumColorEntries(int nBitsPerPixel, int nCompression)
{
int nColors = 0;
switch (nBitsPerPixel)
{
case 1:
nColors = 2;
break;
#ifdef _WIN32_WCE
case 2:
nColors = 4;
break; // winCE only
#endif
case 4:
nColors = 16;
break;
case 8:
nColors = 256;
break;
case 24:
nColors = 0;
break;
case 16:
case 32:
if (nCompression == BI_BITFIELDS)
nColors = 3; // 16 or 32 bpp have 3 colors(masks) in the color table if bitfield compression
else
nColors = 0; // 16 or 32 bpp have no color table if no bitfield compression
break;
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 CDIB::BytesPerLine(int nWidth, int nBitsPerPixel)
{
return ( (nWidth * nBitsPerPixel + 31) & (~31) ) / 8;
}
#ifndef DIBSECTION_NO_PALETTE
// --- In?: palette - reference to a palette object which will be filled
// nNumColors - number of color 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 nNumColors is the No.
// of colors to file. If nNumColorsis 0 or > 256, then 256 colors are used.
/*static*/
BOOL CDIB::CreateHalftonePalette(HPALETTE hPal, int nNumColors)
{
::DeleteObject(hPal);
// Sanity check on requested number of colours.
if (nNumColors <= 0 || nNumColors > 256)
nNumColors = 256;
else if (nNumColors <= 2)
nNumColors = 2;
else if (nNumColors <= 16)
nNumColors = 16;
else if (nNumColors <= 256)
nNumColors = 256;
PALETTEINFO pi;
pi.palNumEntries = (WORD) nNumColors;
if (nNumColors == 2)
{
// According to the MS article "The Palette Manager: How and Why"
// monochrome palettes not really needed (will use B&W)
pi.palPalEntry[0].peRed = ms_StdColors[0].rgbRed;
pi.palPalEntry[0].peGreen = ms_StdColors[0].rgbGreen;
pi.palPalEntry[0].peBlue = ms_StdColors[0].rgbBlue;
pi.palPalEntry[0].peFlags = 0;
pi.palPalEntry[1].peRed = ms_StdColors[255].rgbRed;
pi.palPalEntry[1].peGreen = ms_StdColors[255].rgbGreen;
pi.palPalEntry[1].peBlue = ms_StdColors[255].rgbBlue;
pi.palPalEntry[1].peFlags = 0;
}
else if (nNumColors == 16)
{
// According to the MS article "The Palette Manager: How and Why"
// 4-bit palettes not really needed (will use VGA palette)
for (int i = 0; i < 8; i++)
{
pi.palPalEntry[i].peRed = ms_StdColors[i].rgbRed;
pi.palPalEntry[i].peGreen = ms_StdColors[i].rgbGreen;
pi.palPalEntry[i].peBlue = ms_StdColors[i].rgbBlue;
pi.palPalEntry[i].peFlags = 0;
}
for (i = 8; i < 16; i++)
{
pi.palPalEntry[i].peRed = ms_StdColors[248+i].rgbRed;
pi.palPalEntry[i].peGreen = ms_StdColors[248+i].rgbGreen;
pi.palPalEntry[i].peBlue = ms_StdColors[248+i].rgbBlue;
pi.palPalEntry[i].peFlags = 0;
}
}
else // if (nNumColors == 256)
{
// Fill palette with full halftone palette
for (int i = 0; i < 256; i++)
{
pi.palPalEntry[i].peRed = ms_StdColors[i].rgbRed;
pi.palPalEntry[i].peGreen = ms_StdColors[i].rgbGreen;
pi.palPalEntry[i].peBlue = ms_StdColors[i].rgbBlue;
pi.palPalEntry[i].peFlags = 0;
}
}
hPal = ::CreatePalette((LPLOGPALETTE) pi);
return (hPal != NULL);
}
#endif // DIBSECTION_NO_PALETTE
/////////////////////////////////////////////////////////////////////////////
// CDIB
CDIB::CDIB()
{
// Just in case...
ASSERT(sizeof(ms_StdColors) / sizeof(ms_StdColors[0]) == 256);
m_hBitmap = NULL;
m_hOldBitmap = NULL;
#ifndef DIBSECTION_NO_MEMDC_REUSE
m_bReuseMemDC = FALSE;
#endif
#ifndef DIBSECTION_NO_PALETTE
m_hOldPal = NULL;
#endif
}
CDIB::~CDIB()
{
// Unselect the bitmap out of the memory DC before deleting bitmap
ReleaseMemoryDC(TRUE);
if (m_hBitmap)
::DeleteObject(m_hBitmap);
m_hBitmap = NULL;
m_ppvBits = NULL;
#ifndef DIBSECTION_NO_PALETTE
DeleteObject((HGDIOBJ) m_hPal);
#endif
memset(&m_DIBinfo, 0, sizeof(m_DIBinfo));
m_iColorDataType = DIB_RGB_COLORS;
m_iColorTableSize = 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -