170 likes | 389 Views
12. 비트맵. 1. 비트맵의 종류. DDB 윈도우가 지원하는 비트맵 포맷은 두 가지 종류가 있다 . 1. DDB (Device Dependent Bitmap) 출력장치에 의존된다 . 이미지의 크기와 색상에 관한 기본적인 정보와 이미지 데이터로만 구성되어 있다 . 다양한 해상도의 장치에 광범위하게 사용되지 못하며 만들어진 장치 외의 다른 장치에서 출력하면 제대로 출력되지 못한다 . 2. DIB (Device Independent Bitmap) 비트맵은 장치에 독립적이기 때문에 제 모양대로 출력될 수 있다 .
E N D
12. 비트맵
1. 비트맵의 종류 • DDB • 윈도우가 지원하는 비트맵 포맷은 두 가지 종류가 있다. • 1. DDB (Device Dependent Bitmap) • 출력장치에 의존된다. • 이미지의 크기와 색상에 관한 기본적인 정보와 이미지 데이터로만 구성되어 있다. • 다양한 해상도의 장치에 광범위하게 사용되지 못하며 만들어진 장치 외의 다른 장치에서 출력하면 제대로 출력되지 못한다. • 2. DIB (Device Independent Bitmap) • 비트맵은 장치에 독립적이기 때문에 제 모양대로 출력될 수 있다. • DIB는 DDB에 비해 색상 테이블과 해상도 정보 등의 추가 정보를 가지므로 장치에 종속되지 않으며 활용 용도가 훨씬 광범위 하다. • 확장자가 bmp를 가지는 비트맵 파일들은 모두 DIB 포맷으로 저장된 파일이며 리소스 에디터에서 만들어주는 비트맵들도 모두 DIB이다. • dc에서 선택할 수 있는 비트맵은 DDB이다. • DIB는 직접 DC에 선택될 수 없기 때문에 프로그램에서 곧바로 사용하기가 어렵다.
1. 비트맵의 종류 • wind32에서 HBITMAP으로 지칭되는 비트맵 오브젝트는 DDB를 말한다. • DDB만이 DC에 선택될 수 있다. • 리소스 에디터에서 만들어지는 비트맵 리소스들은 모두 DIB이지만 이 리소스는 LoadBitmap함수에 의해 읽혀지면서 현재 비디오 모드와 호환되는 DDB로 변경된다. typedef struct tagBITMAP { LONG bmType; LONG bmWidth; LONG bmHeight; LONG bmWidthBytes; WORD bmPlanes; WORD bmBitsPixel; LPVOID bmBits; } BITMAP, *PBITMAP; (DDB포맷)
1. 비트맵의 종류 • BITMAP구조체 • bmType : 비트맵의 타입을 지정 ( 0으로 고정) • bmWidth, bmHeight : 비트맵의 폭과 높이를 픽셀 단위로 지정 • bmWidthBytes : 한 줄의 바이트 수, 비트맵은 WORD단위로 정렬되기 때문에 반드시 짝수여야만 한다. • bmPlanes : 색상수면의 수 (보통 1) • bmBitsPixel : 한 픽셀을 표현하기 위해 필요한 비트 수 ( 1:흑백, 4:16색, 8:256색, 256이면 트루컬러 비트맵) • bmBits 멤버는 비트맵의 실제 데이터, 즉 비트맵의 이미지 모양을 가지는 레스터 데이터에 대한 포인터. • HBITMAP CreateBitmap(int nWidth, int nHeight, UINT cPlanes, UNIT cBitsPerPel, CONST VOID * lpvBits); • HBITMAP CreateBitmapIndirect( CONST BITMAP * lpbm);
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc,hMemDC; PAINTSTRUCT ps ; RECT rect ; BYTE Bits[] = {0xc3,0xff,0xbd,0xff,0x66,0xff,0x5a,0xff,0x5a,0xff,0x66,0xff,0xbd,0xff,0xc3,0xff}; HBITMAP hBitmap, hOldBitmap; switch (message) { case WM_CREATE: case WM_PAINT: hdc = BeginPaint (hwnd, &ps) ; hMemDC = CreateCompatibleDC(hdc); hBitmap = CreateBitmap(8,8,1,1,Bits); hOldBitmap = (HBITMAP)SelectObject(hMemDC,hBitmap); BitBlt(hdc,2,2,8,8,hMemDC,0,0,SRCCOPY); SelectObject(hMemDC,hOldBitmap); DeleteDC(hMemDC); DeleteObject(hBitmap); EndPaint (hwnd, &ps) ; return 0 ; case WM_DESTROY: PostQuitMessage (0) ; return 0 ; } return DefWindowProc (hwnd, message, wParam, lParam) ; }
1. 비트맵의 종류 • ROP 모드
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc,hMemDC; PAINTSTRUCT ps ; RECT rect ; HBITMAP hBitmap, hOldBitmap; static HINSTANCE hInst; switch (message) { case WM_CREATE: hInst = ((LPCREATESTRUCT)lParam)->hInstance; return 0; case WM_PAINT: { hdc = BeginPaint (hwnd, &ps) ; hMemDC = CreateCompatibleDC(hdc); hBitmap = LoadBitmap(hInst,MAKEINTRESOURCE(IDB_BITMAP1)); hOldBitmap = (HBITMAP)SelectObject(hMemDC,hBitmap); HBRUSH hBrush = (HBRUSH)GetStockObject(BLACK_BRUSH); HBRUSH hOldBrush = (HBRUSH)SelectObject(hdc,hBrush); Ellipse(hdc,100,100,200,200); SelectObject(hdc,hOldBrush); BITMAP bm; GetObject(hBitmap,sizeof(BITMAP),&bm); BitBlt(hdc,100,100,bm.bmWidth,bm.bmHeight,hMemDC,0,0,SRCPAINT); SelectObject(hMemDC,hOldBitmap); DeleteDC(hMemDC); DeleteObject(hBitmap); EndPaint (hwnd, &ps) ; } return 0 ; case WM_DESTROY: PostQuitMessage (0) ; return 0 ; } return DefWindowProc (hwnd, message, wParam, lParam) ; }
1. 비트맵의 종류 • 확대 및 축소 • BOOL StretchBlt( • HDC hdcDest, // handle to destination DC • int nXOriginDest, // x-coord of destination upper-left corner • int nYOriginDest, // y-coord of destination upper-left corner • int nWidthDest, // width of destination rectangle • int nHeightDest, // height of destination rectangle • HDC hdcSrc, // handle to source DC • int nXOriginSrc, // x-coord of source upper-left corner • int nYOriginSrc, // y-coord of source upper-left corner • int nWidthSrc, // width of source rectangle • int nHeightSrc, // height of source rectangle • DWORD dwRop // raster operation code • );
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc,hMemDC; PAINTSTRUCT ps ; HBITMAP hBitmap, hOldBitmap; static HINSTANCE hInst; switch (message) { case WM_CREATE: hInst = ((LPCREATESTRUCT)lParam)->hInstance; return 0; case WM_PAINT: { hdc = BeginPaint (hwnd, &ps) ; hMemDC = CreateCompatibleDC(hdc); hBitmap = LoadBitmap(hInst,MAKEINTRESOURCE(IDB_BITMAP1)); hOldBitmap = (HBITMAP)SelectObject(hMemDC,hBitmap); BITMAP bm; GetObject(hBitmap,sizeof(BITMAP),&bm); StretchBlt(hdc,100,100,bm.bmWidth*2,bm.bmHeight*2,hMemDC,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY); SelectObject(hMemDC,hOldBitmap); DeleteDC(hMemDC); DeleteObject(hBitmap); EndPaint (hwnd, &ps) ; } return 0 ; case WM_DESTROY: PostQuitMessage (0) ; return 0 ; } return DefWindowProc (hwnd, message, wParam, lParam) ; }
1. 비트맵의 종류 • BOOL TransparentBlt(… , UINT crTransparent); • 마지막 인수로 지정한 컬러를 투명하게 처리한다. • DIB • DIB는 다양한 장치에 사용할 수 있도록 하기 위해 비트맵 출력에 대한 상세 정보를 포함하고 있다. BITMAPFILEHEADER 구조체 BITMAPINFOHEADER 구조체 RGBQUAD 구조체 배열 비트 정보
1. 비트맵의 종류 • BITMAPFILEHEADER 구조체 typedef struct tagBITMAPFILEHEADER { WORD bfType; DWORD bfSize; WORD bfReserved1; WORD bfReserved2; DWORD bfOffBits; } BITMAPFILEHEADER, *PBITMAPFILEHEADER;
1. 비트맵의 종류 • 이 구조체는 DIB가 디스크의 파일로 저장될 때만 사용되며 비트맵을 출력할 때는 사용되지 않는다. • 파일로 저장된 BMP파일에만 이 구조체가 있고 메모리상의 DIB에는 이 구조체가 없다. typedef struct tagBITMAPINFOHEADER{ DWORD biSize; LONG biWidth; LONG biHeight; WORD biPlanes; WORD biBitCount; DWORD biCompression; DWORD biSizeImage; LONG biXPelsPerMeter; LONG biYPelsPerMeter; DWORD biClrUsed; DWORD biClrImportant; } BITMAPINFOHEADER, *PBITMAPINFOHEADER;
1. 비트맵의 종류 int SetDIBitsToDevice( HDC hdc, // handle to DC int XDest, // x-coord of destination upper-left corner int YDest, // y-coord of destination upper-left corner DWORD dwWidth, // source rectangle width DWORD dwHeight, // source rectangle height int XSrc, // x-coord of source lower-left corner int YSrc, // y-coord of source lower-left corner UINT uStartScan, // first scan line in array UINT cScanLines, // number of scan lines CONST VOID*lpvBits, // array of DIB bits CONST BITMAPINFO*lpbmi, // bitmap information UINT fuColorUse // RGB or palette indexes );
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc,hMemDC; PAINTSTRUCT ps ; HBITMAP hBitmap, hOldBitmap; static BITMAPFILEHEADER * fh; static BITMAPINFOHEADER * ih; static int bx; static int by; static BYTE * pRaster; static HINSTANCE hInst; switch (message) { case WM_CREATE: hInst = ((LPCREATESTRUCT)lParam)->hInstance; return 0; case WM_LBUTTONDOWN: { OPENFILENAME OFN; char lpstrFile[256]=""; memset(&OFN,0,sizeof(OPENFILENAME)); OFN.lStructSize = sizeof(OPENFILENAME); OFN.hwndOwner = hwnd; OFN.lpstrFile = lpstrFile; OFN.lpstrFilter="Bitmap File(*.bmp)\0*.bmp\0"; OFN.nMaxFile = 256; if (GetOpenFileName(&OFN) != 0) { HANDLE hFile; DWORD FileSize, dwRead; hFile = CreateFile(lpstrFile,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL); if (hFile == INVALID_HANDLE_VALUE) { return 0; } FileSize = GetFileSize(hFile,NULL);
if (fh) free(fh); fh = (BITMAPFILEHEADER *)malloc(FileSize); ReadFile(hFile,fh,FileSize,&dwRead,NULL); CloseHandle(hFile); pRaster = (PBYTE)fh + fh->bfOffBits; ih = (BITMAPINFOHEADER *)((PBYTE)fh + sizeof(BITMAPFILEHEADER)); bx = ih->biWidth; by = ih->biHeight; InvalidateRect(hwnd,NULL,TRUE); } } return 0; case WM_PAINT: { hdc = BeginPaint (hwnd, &ps) ; if (fh) { SetDIBitsToDevice(hdc,0,0,bx,by,0,0,0,by,pRaster,(BITMAPINFO *)ih, DIB_RGB_COLORS); } EndPaint (hwnd, &ps) ; } return 0 ; case WM_DESTROY: PostQuitMessage (0) ; return 0 ; } return DefWindowProc (hwnd, message, wParam, lParam) ; }
1. 비트맵의 종류 • DIB로 변환 • DDB는 주로 프로그램 실행 중에 CreateBitmap나 CreateCompatibleBitmap함수로 만들어 진다. • 이 포맷을 파일로 저장하려면 DIB로 변환해 주어야 한다.