/* アンチエリアシング 2002/ 4/ 4 宍戸 輝光 */ #include LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); BOOL readBMP(LPCTSTR); void func(); HINSTANCE hInst; HWND hwMain; LPBYTE lpDIB=NULL,lpPixel,lpWrk; LPBITMAPINFO lpbiInfo; int iWidth,iHeight,iLength,iDiv=8000; int WINAPI WinMain (HINSTANCE hInstance,HINSTANCE hPrevInstance, PSTR szCmdLine,int iCmdShow){ MSG msg; WNDCLASS wndclass ; hInst=hInstance; /* プロセスのハンドルを保存 */ 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"; RegisterClass(&wndclass); hwMain=CreateWindow("CWindow","エイリアシング",WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,CW_USEDEFAULT,560,480,NULL,NULL,hInstance,NULL); DragAcceptFiles(hwMain,TRUE); /* ドラッグ&ドロップ受入 */ ShowWindow(hwMain,iCmdShow); /* ウインドウを表示 */ UpdateWindow(hwMain); /* 再描画 */ while (GetMessage(&msg,NULL,0,0)) { /* メッセージループ */ TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam ; } LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam) { HDROP hDrop; HDC hdc; PAINTSTRUCT ps; TCHAR lpszFn[MAX_PATH+1],lpszTmp[64]; static HWND hwBtn; int x,y,r,g,b; switch (iMsg) { case WM_CREATE: /* ボタン作成 */ hwBtn=CreateWindow("BUTTON","実行",WS_CHILD|WS_VISIBLE, 224,4,72,28,hwnd,(HMENU)0,hInst,NULL); return 0; case WM_DROPFILES: /* ファイルがドロップされた時の処理 */ hDrop=(HDROP)wParam; /* HDROPを取得 */ DragQueryFile(hDrop,0,lpszFn,256); /* ファイル名を取得 */ readBMP(lpszFn); DragFinish(hDrop); /* 終了処理 */ InvalidateRgn(hwnd,NULL,TRUE); UpdateWindow (hwnd); /* 再描画 */ return 0; case WM_MOUSEMOVE: /* マウスカーソルの座標取得 */ x=LOWORD(lParam)-4; y=iHeight-(HIWORD(lParam)-34); if (lpDIB==NULL || x<0 || x>=iWidth || y>=iHeight || y<0) return 0; r=lpPixel[x*3+y*iLength+2]; g=lpPixel[x*3+y*iLength+1]; b=lpPixel[x*3+y*iLength]; wsprintf(lpszTmp,"(%d,%d):%d,%d,%d",x,y,r,g,b); SetWindowText(hwMain,lpszTmp); return 0; case WM_COMMAND: if (LOWORD(wParam)==0 && lpDIB!=NULL) { /* 実行ボタン */ func(); InvalidateRgn(hwnd,NULL,FALSE); UpdateWindow(hwnd); } return 0; case WM_PAINT: hdc=BeginPaint(hwnd,&ps); if (lpDIB!=NULL) /* ビットマップが読み込まれていれば */ StretchDIBits(hdc,4,34,iWidth,iHeight, 0,0,iWidth,iHeight,lpPixel,lpbiInfo, DIB_RGB_COLORS,SRCCOPY); /* DIBを画面に描画 */ EndPaint(hwnd,&ps); return 0; case WM_DESTROY : if (lpDIB!=NULL) /* DIB用メモリ解放 */ GlobalFree(lpDIB); PostQuitMessage(0); return 0; } return DefWindowProc (hwnd, iMsg, wParam, lParam) ; } BOOL readBMP(LPCTSTR lpszFn) { /* ビットマップ読み込み */ DWORD offset,dummy; LPBYTE lpBuf,lpPix; LPBITMAPINFO lpWrkBM; HANDLE fh; if (lpDIB!=NULL) /* 以前確保したバッファを解放 */ GlobalFree(lpDIB); fh=CreateFile(lpszFn,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); lpWrkBM=(LPBITMAPINFO)(lpBuf+sizeof(BITMAPFILEHEADER)); offset=*(LPDWORD)(lpBuf+10); lpPix=lpBuf+offset; /* ビットマップバッファの先頭アドレス */ CloseHandle(fh); /* 24ビットフルカラービットマップでなければ無効 */ if (lpBuf[0]!='B' || lpWrkBM->bmiHeader.biBitCount!=24) { GlobalFree(lpBuf); MessageBox(hwMain,"このファイルは使えません。","エラー",MB_OK); return FALSE; } /* ビットマップの大きさ保存 */ iWidth=lpWrkBM->bmiHeader.biWidth; iHeight=lpWrkBM->bmiHeader.biHeight; if (iWidth % 4==0) /* バッファの1ラインの長さを計算 */ iLength=iWidth*3; else iLength=iWidth*3+(4-(iWidth*3) % 4); lpDIB=(LPBYTE)GlobalAlloc(GPTR,sizeof(BITMAPINFO)+iLength*iHeight*3); lpbiInfo=(LPBITMAPINFO)lpDIB; lpPixel=lpDIB+sizeof(BITMAPINFO); lpbiInfo->bmiHeader.biSize=sizeof(BITMAPINFOHEADER); lpbiInfo->bmiHeader.biWidth=iWidth; lpbiInfo->bmiHeader.biHeight=iHeight; lpbiInfo->bmiHeader.biPlanes=1; lpbiInfo->bmiHeader.biBitCount=24; lpbiInfo->bmiHeader.biCompression=BI_RGB; CopyMemory(lpPixel,lpPix,iLength*iHeight); GlobalFree(lpBuf); return TRUE; } void mix(int x,int y) { /* 指定ピクセルをぼかす */ int i,j,r=0,g=0,b=0,n=0; for (i=-1;i<=1;i++) for (j=-1;j<=1;j++) { if (x+j>=0 && x+j=0 && y+i=0 && j+l=0 && i+kiDiv) { mix(j,i); done=1; } } } } }