www.gusucode.com > 精典源码Delphi130:顺某指纹考勤管理系统 > 精典源码Delphi130:顺某指纹考勤管理系统/11065顺某指纹考勤管理系统delphi/1.3cxy/FP/CGray8Dib.cpp

    // CGray8Dib.cpp

/**************************************************************************************************
                            Sample code for FPToolkit
                            Copyright Digital Persona, Inc. 1996-1998
/*************************************************************************************************/

#include "stdafx.h"
#include "CGray8Dib.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//----------------------------------------------------------------------
CGray8Dib::CGray8Dib(const CRect &rect, int ArX, int ArY) 
{
    // Make the bitmap to fit in the rectangle maintaining the aspect ratio
    if (ArX == 0 || ArY == 0) 
    {
        ArX = 291;
        ArY = 525;
    }

    int dx = rect.Width () & 0xFFFFFFFC;                //Adjust length of the scan lines, to be divisible by 4.
    int dy = dx * ArY / ArX;

    if (dy > rect.Height ())
    {
        dx = (rect.Height () * ArX / ArY) & 0xFFFFFFFC; //Adjust length of the scan lines, to be divisible by 4.
        dy = dx * ArY / ArX;
    }
  
    m_hPalette = NULL;

    m_lpBMIH = (LPBITMAPINFOHEADER) new char[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * m_nColorTableEntries];
  
    m_lpBMIH->biSize = sizeof(BITMAPINFOHEADER);
    m_lpBMIH->biWidth  = dx;
    m_lpBMIH->biHeight = dy;
    m_lpBMIH->biPlanes = 1;
    m_lpBMIH->biBitCount = m_nBitCount;
    m_lpBMIH->biCompression = 0;
    m_lpBMIH->biSizeImage = 0;
    m_lpBMIH->biXPelsPerMeter = 0;
    m_lpBMIH->biYPelsPerMeter = 0;
    m_lpBMIH->biClrUsed      = m_nColorTableEntries;
    m_lpBMIH->biClrImportant = m_nColorTableEntries;

    ComputeMetrics();
    MakeColorTable();
    MakePalette();
}

//----------------------------------------------------------------------
CGray8Dib::~CGray8Dib() 
{
  ::DeleteObject(m_hPalette);
    delete[] m_lpBMIH;
}

//----------------------------------------------------------------------
UINT CGray8Dib::UsePalette(CDC* pDC, bool bForeground) 
{
 
    HDC hdc = pDC->GetSafeHdc();
  ::SelectPalette(hdc, m_hPalette, bForeground == false);
    UINT n= ::RealizePalette(hdc);
    TRACE ("CGray8Dib::UsePalette() Foreground:%d ColorsChanged:%d\n", bForeground, n);
    return n;
}

//----------------------------------------------------------------------
BOOL CGray8Dib::Draw(CDC* pDC, int x, int y, const LPBYTE lpImage) 
{
  ::SelectPalette(pDC->GetSafeHdc(), m_hPalette, TRUE);
  //Colde bellow repositions bitmap bits to show bitmap correct way
  int size = m_lpBMIH->biHeight * m_lpBMIH->biWidth;
  LPBYTE temp = new BYTE[size];
  LPBYTE temp2 = new BYTE[size];
  int i;
  for(i = 0; i < size; i++)
  {
      temp[i] = lpImage[size - i];
  }
  for(int line = 0; line < m_lpBMIH->biHeight; line++)
  {
      for(int row = 0; row < m_lpBMIH->biWidth; row++)
      {
          temp2[line * m_lpBMIH->biWidth + row] = temp[line * m_lpBMIH->biWidth - row + m_lpBMIH->biWidth];
      }
  }
    SetDIBitsToDevice (pDC->GetSafeHdc(), x, y, 
                       m_lpBMIH->biWidth, m_lpBMIH->biHeight,
                       0, 0, 0, m_lpBMIH->biHeight, temp,
                       (LPBITMAPINFO) m_lpBMIH, DIB_RGB_COLORS);
    delete [] temp;
    delete [] temp2;
    return TRUE;
}

////////////////////////////////////////////////////////////////////////////////////////////
// helper functions

//----------------------------------------------------------------------
void CGray8Dib::ComputeMetrics() 
{
    DWORD dwBytes = ((DWORD) m_lpBMIH->biWidth * m_lpBMIH->biBitCount) / 32;

    if(((DWORD) m_lpBMIH->biWidth * m_lpBMIH->biBitCount) % 32)
        dwBytes++;

    m_dwSizeImage = dwBytes * 4 * m_lpBMIH->biHeight; // no compression

    m_lpvColorTable = (RGBQUAD*)((LPBYTE)m_lpBMIH + sizeof(BITMAPINFOHEADER)); // points inside m_lpBMIH.
}

//----------------------------------------------------------------------
void CGray8Dib::MakeColorTable() 
{
    for (int i = 0; i < m_nColorTableEntries; i++) 
    {
        BYTE k = (BYTE)i<<3;
            m_lpvColorTable[i].rgbRed      = k;  // Gray Scale !
            m_lpvColorTable[i].rgbGreen    = k;
            m_lpvColorTable[i].rgbBlue     = k;
            m_lpvColorTable[i].rgbReserved = 0;
    }
}   

//----------------------------------------------------------------------
// makes a logical palette (m_hPalette) from the DIB's color table
// this palette will be selected and realized prior to drawing the DIB
BOOL CGray8Dib::MakePalette() 
{
    char pLogPalMem[2 * sizeof(WORD) +  m_nColorTableEntries * sizeof(PALETTEENTRY)];
    LPLOGPALETTE pLogPal = (LPLOGPALETTE)pLogPalMem;

    pLogPal->palVersion = 0x300;
    pLogPal->palNumEntries = m_nColorTableEntries;

    for(int i = 0; i < m_nColorTableEntries; i++)
    {
        pLogPal->palPalEntry[i].peRed   = m_lpvColorTable[i].rgbRed;
        pLogPal->palPalEntry[i].peGreen = m_lpvColorTable[i].rgbGreen;
        pLogPal->palPalEntry[i].peBlue  = m_lpvColorTable[i].rgbBlue;
        pLogPal->palPalEntry[i].peFlags = 0;
    }

    m_hPalette = ::CreatePalette(pLogPal);

    return TRUE;
}   

//----------------------------------------------------------------------
CSize CGray8Dib::GetSize() const 
{
    return CSize (m_lpBMIH->biWidth, m_lpBMIH->biHeight);
}

//----------------------------------------------------------------------
// Leave there just 32 colors, i.e. divide every byte by 8.
void CGray8Dib::ReduceNumberOfColors (BYTE* pBuffer)
{
    for (DWORD i=0; i<m_dwSizeImage; i++, pBuffer++)
        *pBuffer = *pBuffer >> 3;
}