/* ビットマップの拡大・縮小 1999/ 2/23 宍戸 輝光 */ #include #include LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM); BOOL readBMP(char fn[]); void scale(int,int); HINSTANCE hInst; HWND hwnd; int iWidth,iHeight,iLength,iTime; BOOL loaded; LPBYTE lpBuf,lpBMP,lpWork; LPBITMAPINFO lpInfo; int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow){ MSG msg; WNDCLASSEX wndclass ; hInst=hInstance; /* プロセスのハンドルを保存 */ wndclass.cbSize = sizeof(wndclass); wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = WndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hInstance; wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION); wndclass.hCursor = LoadCursor (NULL, IDC_ARROW); wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wndclass.lpszMenuName = NULL; wndclass.lpszClassName = "CWindow"; wndclass.hIconSm = LoadIcon (NULL, IDI_APPLICATION); RegisterClassEx (&wndclass); hwnd = CreateWindow ("CWindow","ビットマップの回転処理",WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,CW_USEDEFAULT,560,480, NULL,NULL,hInstance,NULL); DragAcceptFiles(hwnd,TRUE); /* ドラッグ&ドロップ受入 */ ShowWindow (hwnd,iCmdShow); /* ウインドウを表示 */ UpdateWindow (hwnd); /* 再描画 */ while (GetMessage (&msg,NULL,0,0)) { /* メッセージループ */ TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam ; } LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam) { static OPENFILENAME ofn; static char lpszFn[256],lpszFt[256],lpszStr[64]; static HWND hwOpenB,hwS2B,hwS3B; HDROP hDrop; HDC hdc; PAINTSTRUCT ps; /* 「ファイルを開く」ダイアログ設定 */ ofn.lStructSize=sizeof(OPENFILENAME); ofn.hwndOwner=hwnd; ofn.hInstance=NULL; ofn.lpstrFilter= // フィルター "ビットマップ(*.bmp)\0*.bmp\0すべてのファイル(*.*)\0*.*\0\0"; ofn.lpstrCustomFilter=NULL; ofn.nMaxCustFilter=0; ofn.nFilterIndex=1; /* フィルターの初期位置 */ ofn.lpstrFile=lpszFn; /* ファイル名用文字列バッファ */ ofn.nMaxFile=255; /* 文字列バッファのサイズ */ ofn.lpstrFileTitle=lpszFt; /* タイトル用文字列バッファ */ ofn.nMaxFileTitle=255; /* 文字列バッファのサイズ */ ofn.lpstrInitialDir=NULL; ofn.lpstrTitle="読み込むファイルを選んでください"; /* タイトル */ ofn.Flags=OFN_FILEMUSTEXIST; ofn.nFileOffset=0; ofn.nFileExtension=0; ofn.lpstrDefExt=NULL; ofn.lCustData=0; ofn.lpfnHook=NULL; ofn.lpTemplateName=NULL; switch (iMsg) { case WM_CREATE: hwOpenB=CreateWindow("Button","読み込み", /* ボタン作成 */ WS_CHILD | WS_VISIBLE,16,8,96,32,hwnd, (HMENU)0,hInst,NULL); hwS2B=CreateWindow("Button","2/3", WS_CHILD | WS_VISIBLE,128,8,80,32,hwnd, (HMENU)1,hInst,NULL); hwS3B=CreateWindow("Button","1.5", WS_CHILD | WS_VISIBLE,224,8,80,32,hwnd, (HMENU)2,hInst,NULL); loaded=FALSE; /* 読み込みフラグリセット */ EnableWindow(hwS2B,FALSE); /* 2/3ボタン無効 */ EnableWindow(hwS3B,FALSE); /* 1.5ボタン無効 */ return 0; case WM_COMMAND: switch (LOWORD(wParam)) { case 0: /* 読み込みボタン */ if (!GetOpenFileName(&ofn)) /* ファイルを選択 */ return 0; if (readBMP(lpszFn)) { /* BMPファイル読み込み */ EnableWindow(hwS2B,TRUE); EnableWindow(hwS3B,TRUE); } else { EnableWindow(hwS2B,FALSE); EnableWindow(hwS3B,FALSE); } break; case 1: /* 2/3ボタン */ /* 処理中はボタン無効 */ EnableWindow(hwOpenB,FALSE); EnableWindow(hwS2B,FALSE); EnableWindow(hwS3B,FALSE); /* 縮小処理 */ scale((int)(iWidth*(2.0/3.0)),(int)(iHeight*(2.0/3.0))); /* 処理終了後ボタン有効 */ EnableWindow(hwOpenB,TRUE); EnableWindow(hwS2B,TRUE); EnableWindow(hwS3B,TRUE); break; case 2: /* 1.5ボタン */ /* 処理中はボタン無効 */ EnableWindow(hwOpenB,FALSE); EnableWindow(hwS2B,FALSE); EnableWindow(hwS3B,FALSE); /* 拡大処理 */ scale((int)(iWidth*1.5),(int)(iHeight*1.5)); /* 処理終了後ボタン有効 */ EnableWindow(hwOpenB,TRUE); EnableWindow(hwS2B,TRUE); EnableWindow(hwS3B,TRUE); } break; case WM_DROPFILES: /* ファイルがドロップされた時の処理 */ hDrop=(HDROP)wParam; /* HDROPを取得 */ DragQueryFile(hDrop,0,lpszFn,256); /* ファイル名を取得 */ if (readBMP(lpszFn)) {/* BMPファイル読み込み */ EnableWindow(hwS2B,TRUE); EnableWindow(hwS3B,TRUE); } else { EnableWindow(hwS2B,FALSE); EnableWindow(hwS3B,FALSE); } DragFinish(hDrop); /* 終了処理 */ return 0; case WM_PAINT: hdc=BeginPaint(hwnd,&ps); if (loaded) /* ビットマップが読み込まれていれば */ StretchDIBits(hdc,4,48,iWidth,iHeight, 0,0,iWidth,iHeight,lpBMP,lpInfo, DIB_RGB_COLORS,SRCCOPY); /* DIBを画面に描画 */ if (iTime>0) { /* 処理時間を表示 */ wsprintf(lpszStr,"処理時間%d ミリ秒 ",iTime); TextOut(hdc,344,8,lpszStr,lstrlen(lpszStr)); } EndPaint(hwnd,&ps); return 0; case WM_DESTROY : if (loaded) { /* DIBと作業用メモリ解放 */ GlobalFree(lpBuf); GlobalFree(lpWork); } PostQuitMessage(0); return 0; } return DefWindowProc (hwnd, iMsg, wParam, lParam) ; } BOOL readBMP(char fn[]) { /* ビットマップ読み込み */ char lpStr[280]; DWORD offset,dummy; HANDLE fh; if (loaded) { /* 以前確保したバッファを解放 */ GlobalFree(lpBuf); GlobalFree(lpWork); } fh=CreateFile(fn,GENERIC_READ,0,NULL,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,NULL); /* ファイルオープン */ lpBuf=(LPBYTE)GlobalAlloc (GPTR,GetFileSize(fh,NULL)); /* バッファ確保 */ ReadFile(fh,lpBuf,GetFileSize(fh,NULL),&dummy,NULL); lpInfo=(LPBITMAPINFO)(lpBuf+sizeof(BITMAPFILEHEADER)); offset=*(LPDWORD)(lpBuf+10); lpBMP=lpBuf+offset; /* ビットマップバッファの先頭アドレス */ CloseHandle(fh); /* 24ビットフルカラービットマップでなければ無効 */ if (lpBuf[0]!='B' || lpInfo->bmiHeader.biBitCount!=24) { GlobalFree(lpBuf); loaded=FALSE; SetWindowText(hwnd,"Error!!"); return FALSE; } /* ビットマップの大きさ保存 */ iWidth=lpInfo->bmiHeader.biWidth; iHeight=lpInfo->bmiHeader.biHeight; if ((iWidth*3) % 4==0) /* バッファの1ラインの長さを計算 */ iLength=iWidth*3; else iLength=iWidth*3+(4-(iWidth*3) % 4); lpWork=(LPBYTE)GlobalAlloc(GPTR,iLength*iHeight); wsprintf(lpStr,"%s %d*%dPixel %dBit",fn,iWidth,iHeight, lpInfo->bmiHeader.biBitCount); SetWindowText(hwnd,lpStr); /* ビットマップの情報表示 */ loaded=TRUE; /* 読み込みフラグセット */ InvalidateRgn(hwnd,NULL,FALSE); UpdateWindow (hwnd); /* 再描画 */ return TRUE; } void scale(int width,int height) { /* ビットマップの拡大・縮小処理 */ int i,j,xx,yy,iTime1; iTime1=GetTickCount(); /* 処理開始前の時間を記録 */ /* 作業用ビットマップをクリア */ FillMemory(lpWork,iLength*iHeight,0xff); for (i=0;i