프로그래밍

GetPixel 함수 대체 (GetPixel Alternative)

friday13th 2011. 6. 22. 11:56



GetDC는 가능하나 GetPixel 함수를 쓸수었을때 DC의 DDB를 DIB로 바꾸어 픽셀 색상 정보를 가져옴.
MyGetPixel 사용


DIB.h

#pragma once
 
HANDLE DDBToDIB(CBitmap &bitmap, DWORD dwCompression, CPalette *pPal);
BOOL WriteDIB(LPTSTR szFileName, HANDLE hDIB);
HANDLE OpenDIB(HWND hWNd, RECT &rtRect );
COLORREF GetDIBPixel(HANDLE hDIB, int x, int y);
void CloseDIB(HANDLE hDIB);
COLORREF MyGetPixel(HWND hWnd, int x, int y);


DIB.cpp

#include "stdafx.h"
#include "DIB.h"
HANDLE DDBToDIB(CBitmap &bitmap, DWORD dwCompression, CPalette *pPal)
{
 BITMAP       bm;
 BITMAPINFOHEADER bi;
 LPBITMAPINFOHEADER  lpbi;
 DWORD       dwLen;
 HANDLE       hDIB;
 HANDLE        handle;
 HDC        hDC;
 HPALETTE      hPal;

 // Return Value : A HANDLE to the attached Windows GDI object
 ASSERT( bitmap.GetSafeHandle() );
 // The function has no arg for bitfields
 if( dwCompression == BI_BITFIELDS )
  return NULL;
 // If a palette has not been supplied use defaul palette
 hPal = (HPALETTE) pPal->GetSafeHandle();
 if (hPal==NULL)
  hPal = (HPALETTE) GetStockObject(DEFAULT_PALETTE);
 // Get bitmap information
 bitmap.GetObject(sizeof(bm),(LPSTR)&bm);
 // Initialize the bitmapinfoheader ( Bitmap Information )
 bi.biSize      = sizeof(BITMAPINFOHEADER);             // 비트맵 헤더크기
 bi.biWidth      = bm.bmWidth;                           // 비트맵의 가로 크기
 bi.biHeight   = bm.bmHeight;                          // 비트맵의 세로 크기
 bi.biPlanes   = 1;                                    // Plane 수 (1로 설정)
 bi.biBitCount  = bm.bmPlanes * bm.bmBitsPixel;         // 한 픽셀당 비트수
 bi.biCompression = dwCompression;                        // 압축 유무
 bi.biSizeImage  = 0;                                    // 그림 데이터 크기
 bi.biXPelsPerMeter = 0;                                    // 한 픽셀당 가로 미터
 bi.biYPelsPerMeter = 0;                                    // 한 픽셀당 세로 미터
 bi.biClrUsed  = 0;                                    // 그림에서 실제 사용되는 컬러수
 bi.biClrImportant = 0;                                    // 중요하게 사용되는 컬러
 // Compute the size of the  infoheader and the color table
 int nColors = (1 << bi.biBitCount);
 if( nColors > 256 )
  nColors = 0;
 dwLen  = bi.biSize + nColors * sizeof(RGBQUAD);
 // We need a device context to get the DIB from
 hDC = ::GetDC(NULL);
 hPal = SelectPalette(hDC,hPal,FALSE);
 RealizePalette(hDC);
 // Allocate enough memory to hold bitmapinfoheader and color table
 hDIB = GlobalAlloc(GMEM_FIXED,dwLen);
 if (!hDIB){
  SelectPalette(hDC,hPal,FALSE);
  ::ReleaseDC(NULL,hDC);
  return NULL;
 }
 lpbi = (LPBITMAPINFOHEADER)hDIB;
 *lpbi = bi;
 // Call GetDIBits with a NULL lpBits param, so the device driver
 // will calculate the biSizeImage field
 GetDIBits(hDC, (HBITMAP)bitmap.GetSafeHandle(), 0L, (DWORD)bi.biHeight,
  (LPBYTE)NULL, (LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS);
 bi = *lpbi;
 // If the driver did not fill in the biSizeImage field, then compute it
 // Each scan line of the image is aligned on a DWORD (32bit) boundary
 if (bi.biSizeImage == 0){
  bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31) & ~31) / 8)
   * bi.biHeight;
  // If a compression scheme is used the result may infact be larger
  // Increase the size to account for this.
  if (dwCompression != BI_RGB)
   bi.biSizeImage = (bi.biSizeImage * 3) / 2;
 }
 // Realloc the buffer so that it can hold all the bits
 dwLen += bi.biSizeImage;
 if (handle = GlobalReAlloc(hDIB, dwLen, GMEM_MOVEABLE))
  hDIB = handle;
 else{
  GlobalFree(hDIB);
  // Reselect the original palette
  SelectPalette(hDC,hPal,FALSE);
  ::ReleaseDC(NULL,hDC);
  return NULL;
 }
 // Get the bitmap bits
 lpbi = (LPBITMAPINFOHEADER)hDIB;
 // FINALLY get the DIB
 BOOL bGotBits = GetDIBits( hDC, (HBITMAP)bitmap.GetSafeHandle(),
  0L,    // Start scan line
  (DWORD)bi.biHeight,  // # of scan lines
  (LPBYTE)lpbi    // address for bitmap bits
  + (bi.biSize + nColors * sizeof(RGBQUAD)),
  (LPBITMAPINFO)lpbi,  // address of bitmapinfo
  (DWORD)DIB_RGB_COLORS);  // Use RGB for color table
 if( !bGotBits )
 {
  GlobalFree(hDIB);
  SelectPalette(hDC,hPal,FALSE);
  ::ReleaseDC(NULL,hDC);
  return NULL;
 }
 SelectPalette(hDC,hPal,FALSE);
 ::ReleaseDC(NULL,hDC);
 return hDIB;
}
BOOL WriteDIB(LPTSTR szFileName, HANDLE hDIB)
{
 BITMAPFILEHEADER      hdr;
 LPBITMAPINFOHEADER    lpbi;
 if (!hDIB)
  return FALSE;
 CFile file;
 if (!file.Open(szFileName, CFile::modeWrite | CFile::modeCreate))
  return FALSE;
 lpbi = (LPBITMAPINFOHEADER)hDIB;
 // 한 픽셀당 비트수를 왼쪽으로 1 이동 (shift)
 int nColors = 1 << lpbi->biBitCount;
 // Fill in the fields of the file header ( Bitmap file header )
 hdr.bfType        = ((WORD) ('M' << 8) | 'B');         // is always "BM"  
 hdr.bfSize        = GlobalSize (hDIB) + sizeof(hdr);   // 비트맵 파일의 전체 크기
 hdr.bfReserved1   = 0;                                 // 예약변수 (0으로 설정)
 hdr.bfReserved2   = 0;                                 // 예약변수 (0으로 설정)
 // 파일에서 비트맵 데이터가 있는 위치
 hdr.bfOffBits     = (DWORD)(sizeof(hdr) + lpbi->biSize + nColors * sizeof(RGBQUAD)); 
 // Write the file header
 file.Write(&hdr, sizeof(hdr));
 // Write the DIB header and the bits
 file.Write(lpbi, GlobalSize(hDIB));
 file.Close();
 return TRUE;
}

HANDLE OpenDIB(HWND hWNd, RECT &rtRect)
{
 if (hWNd==NULL)
  return NULL;
 HDC hDC = GetDC(hWNd);
 CDC dc;
 dc.Attach(hDC);
 CDC *pDC = &dc;
 CRect Rect = rtRect;
 CBitmap bitmap;
 CDC MemDC;
 MemDC.CreateCompatibleDC(pDC);
 bitmap.CreateCompatibleBitmap(pDC, Rect.Width(),Rect.Height());
 CBitmap *pOldBitmap = MemDC.SelectObject(&bitmap);
 MemDC.BitBlt(0,0 , Rect.Width(),Rect.Height(), pDC, Rect.left, Rect.top, SRCCOPY);
 CPalette m_Pal;
 HANDLE hDIB = DDBToDIB(bitmap, BI_RGB, &m_Pal);
 //  if (hDIB == NULL) {
 //   // AfxMessageBox("bitmap을 DIB으로 convert할 수 없습니다.", MB_ICONSTOP);
 //   return 0 ;
 //  }
 return hDIB;

}
COLORREF GetDIBPixel(HANDLE hDIB, int x, int y)
{
 LPBITMAPINFOHEADER    lpbi;
 lpbi = (LPBITMAPINFOHEADER)hDIB;
 BYTE *pBit = ((BYTE*)hDIB )+ sizeof(BITMAPINFOHEADER) + 4;
 int pitch  = ( (((lpbi->biWidth*lpbi->biBitCount)+31)/32)*4 );
 BYTE* pPixel = (BYTE*) ( LPBYTE( pBit )+( (lpbi->biHeight-y-1) *pitch)+(( (x) *lpbi->biBitCount)/8) );
 COLORREF color=RGB(pPixel[2], pPixel[1], pPixel[0]);
 return color;
}
void CloseDIB(HANDLE hDIB)
{
 if (hDIB)
  GlobalFree(hDIB);
}

COLORREF MyGetPixel(HWND hWnd, int x, int y)
{
 CRect rt(x,y,x+1,y+1); 
 HANDLE hDIB = OpenDIB(hWnd, rt);
 if (hDIB==NULL)
  return 0;
 COLORREF retColor = GetDIBPixel(hDIB, 0, 0);
 CloseDIB(hDIB);
 return retColor;
}