#include #include"bmp_io.h" BMP_IO::BMP_IO() { lpPixel=NULL; } BMP_IO::~BMP_IO() { DeleteObject(hdcBMP); } BOOL BMP_IO::bmp_in(LPCTSTR file_name,HWND hWnd) { LPBITMAPFILEHEADER lpbmpfh; int iFileSize,iOffset; HANDLE fhBMP; DWORD dwRead; fhBMP=CreateFile(file_name,GENERIC_READ,0,NULL, OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL); if(fhBMP==INVALID_HANDLE_VALUE){ MessageBox(hWnd,"ファイルが開けません","エラー",MB_OK); return FALSE; } iFileSize=GetFileSize(fhBMP,NULL); lpBMP=(LPBYTE)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,iFileSize); ReadFile(fhBMP,lpBMP,iFileSize,&dwRead,NULL); CloseHandle(fhBMP); lpbmpfh=(LPBITMAPFILEHEADER)(lpBMP); lpbiBMPInfo=(LPBITMAPINFO)(lpBMP+sizeof(BITMAPFILEHEADER)); BitCount=lpbiBMPInfo->bmiHeader.biBitCount; if(lpbmpfh->bfType!=('M'<<8)+'B' || (BitCount!=8 && BitCount!=24)){ MessageBox(hWnd,"8ビット or 24ビットBMPファイルしか読み込めません","エラー",MB_OK); HeapFree(GetProcessHeap(),0,lpBMP); return FALSE; } iOffset=lpbmpfh->bfOffBits; lpBMPPixel=lpBMP+iOffset; dwColors=(LPDWORD)lpbiBMPInfo->bmiColors; iWidth=lpbiBMPInfo->bmiHeader.biWidth; iHeight=lpbiBMPInfo->bmiHeader.biHeight; biInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER); biInfo.bmiHeader.biWidth=iWidth; biInfo.bmiHeader.biHeight=iHeight; biInfo.bmiHeader.biPlanes=1; biInfo.bmiHeader.biBitCount=32; biInfo.bmiHeader.biCompression=BI_RGB; if(lpPixel!=NULL) DeleteObject(hdcBMP); hdc=GetDC(hWnd); hBMP=CreateDIBSection(hdc,&biInfo,DIB_RGB_COLORS, (LPVOID *)(&lpPixel),NULL,0); hdcBMP=CreateCompatibleDC(hdc); SelectObject(hdcBMP,hBMP); ReleaseDC(hWnd,hdc); DeleteObject(hBMP); switch(BitCount){ case 8: for(y=0;ybmiHeader.biBitCount; switch(BitCount){ case 8: lpBMPPixel=(LPBYTE)(lpBMP+sizeof(BITMAPINFO)+sizeof(RGBQUAD)*255); break; case 24: lpBMPPixel=(LPBYTE)(lpBMP+sizeof(BITMAPINFO)); break; default: MessageBox(hWnd,"8ビット or 24ビットBMPしか読み込めません","エラー",MB_OK); return FALSE; } dwColors=(LPDWORD)lpbiBMPInfo->bmiColors; iWidth=lpbiBMPInfo->bmiHeader.biWidth; iHeight=lpbiBMPInfo->bmiHeader.biHeight; biInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER); biInfo.bmiHeader.biWidth=iWidth; biInfo.bmiHeader.biHeight=iHeight; biInfo.bmiHeader.biPlanes=1; biInfo.bmiHeader.biBitCount=32; biInfo.bmiHeader.biCompression=BI_RGB; if(lpPixel!=NULL) DeleteObject(hdcBMP); hdc=GetDC(hWnd); hBMP=CreateDIBSection(hdc,&biInfo,DIB_RGB_COLORS, (LPVOID *)(&lpPixel),NULL,0); hdcBMP=CreateCompatibleDC(hdc); SelectObject(hdcBMP,hBMP); ReleaseDC(hWnd,hdc); DeleteObject(hBMP); switch(BitCount){ case 8: for(y=0;y