#include #include #include"bmp_io.h" #include"hensu.h" #include"nichishori.h" #include"rinkaku.h" LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); ATOM InitApp(HINSTANCE); BOOL InitInstance(HINSTANCE,int); char szClassName[]="template"; int iWidth,iHeight; int WINAPI WinMain(HINSTANCE hCurInst,HINSTANCE hPrevInst, LPSTR lpsCmdLine,int nCmdShow) { MSG msg; BOOL bRet; if(!InitApp(hCurInst)) return FALSE; if(!InitInstance(hCurInst,nCmdShow)) return FALSE; while((bRet=GetMessage(&msg,NULL,0,0))!=0){ if(bRet==-1){ MessageBox(NULL,"GetMessageエラー","Error",MB_OK); break; }else{ TranslateMessage(&msg); DispatchMessage(&msg); } } return (int)msg.wParam; } ATOM InitApp(HINSTANCE hInst) { WNDCLASSEX wc; wc.cbSize=sizeof(WNDCLASSEX); wc.style=CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc=WndProc; wc.cbClsExtra=0; wc.cbWndExtra=0; wc.hInstance=hInst; wc.hIcon=(HICON)LoadImage(NULL, MAKEINTRESOURCE(IDI_APPLICATION), IMAGE_ICON, 0,0, LR_DEFAULTSIZE|LR_SHARED); wc.hCursor=(HCURSOR)LoadImage(NULL, MAKEINTRESOURCE(IDC_ARROW), IMAGE_CURSOR, 0,0, LR_DEFAULTSIZE|LR_SHARED); wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH); wc.lpszMenuName=NULL; wc.lpszClassName=(LPCTSTR)szClassName; wc.hIconSm=(HICON)LoadImage(NULL, MAKEINTRESOURCE(IDI_APPLICATION), IMAGE_ICON, 0,0, LR_DEFAULTSIZE|LR_SHARED); return RegisterClassEx(&wc); } BOOL InitInstance(HINSTANCE hInst,int nCmdShow) { HWND hWnd; hWnd=CreateWindow(szClassName, "第4章 輪郭を抜き出す", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInst, NULL); if(!hWnd) return FALSE; ShowWindow(hWnd,nCmdShow); UpdateWindow(hWnd); return TRUE; } LRESULT CALLBACK WndProc(HWND hWnd,UINT msg,WPARAM wp,LPARAM lp) { static BMP_IO mybmp; static LPBITMAPINFO lpbiInfo; static LPDWORD lpPixel,lpPixel2,lpPixel3; long hist[256]; static long hist2[256]; static int py=275; int i; HPEN hPen,hOldPen; RECT rc={300,0,800,250}; char text[64],text2[64]; static char *info="8ビットBMPファイルをドロップして下さい\n\n" "↑↓:赤いラインが上下します\nEnter:2値化 or 2値化直前に戻します\n\n" "F1:グラディエント F2:ラプラシアン\nF3:プレウィッツ F4:原画像\n\n" "ファイルドロップ → レベル調整&オペレータ設定\n→ 輪郭抽出 → 2値化"; HDC hdc; PAINTSTRUCT ps; static double amp=1; static int ope=1; HDROP hDrop; char szFileName[256]; switch(msg){ case WM_CREATE: DragAcceptFiles(hWnd,TRUE); break; case WM_DROPFILES: hDrop=(HDROP)wp; DragQueryFile(hDrop,0,szFileName,sizeof(szFileName)); DragFinish(hDrop); if(!mybmp.bmp_in(szFileName,hWnd)) break; lpbiInfo=&mybmp.biInfo; lpPixel=mybmp.lpPixel; iWidth=mybmp.iWidth; iHeight=mybmp.iHeight; lpPixel2=(LPDWORD)HeapAlloc(GetProcessHeap(),0,2*iWidth*iHeight*4); lpPixel3=lpPixel2+iWidth*iHeight; CopyMemory(lpPixel2,lpPixel,iWidth*iHeight*4); CopyMemory(lpPixel3,lpPixel,iWidth*iHeight*4); histgram(lpPixel2,hist); histsmooth(hist,hist2); InvalidateRect(hWnd,NULL,TRUE); break; case WM_PAINT: hdc=BeginPaint(hWnd,&ps); for(i=0;i<256;i++){ MoveToEx(hdc,0,300+i,NULL); LineTo(hdc,hist2[i]/2,300+i); } DrawText(hdc,info,-1,&rc,DT_WORDBREAK); if(py>=300 && py<300+256){ sprintf(text,"濃度値%d: %d個(%lf%%)", py-300,hist2[py-300],(double)hist2[py-300]/(iWidth*iHeight)*100.0); TextOut(hdc,300,230,text,(int)strlen(text)); } sprintf(text2,"レベル調整(←→) = %lf オペレータ(1 or 2 or 3) = %d",amp,ope); TextOut(hdc,300,210,text2,(int)strlen(text2)); StretchDIBits(hdc,0,0,256,256, 0,0,iWidth,iHeight,lpPixel3,lpbiInfo, DIB_RGB_COLORS,SRCCOPY); hPen=CreatePen(PS_SOLID,1,RGB(255,0,0)); hOldPen=(HPEN)SelectObject(hdc,hPen); MoveToEx(hdc,0,py,NULL); LineTo(hdc,2048,py); SelectObject(hdc,hOldPen); DeleteObject(hPen); EndPaint(hWnd,&ps); break; case WM_KEYDOWN: switch(wp){ case VK_F1: gradient(lpPixel,lpPixel2,amp,ope); histgram(lpPixel2,hist); histsmooth(hist,hist2); CopyMemory(lpPixel3,lpPixel2,iWidth*iHeight*4); break; case VK_F2: laplacian(lpPixel,lpPixel2,amp,ope); histgram(lpPixel2,hist); histsmooth(hist,hist2); CopyMemory(lpPixel3,lpPixel2,iWidth*iHeight*4); break; case VK_F3: mytemplate(lpPixel,lpPixel2,amp); histgram(lpPixel2,hist); histsmooth(hist,hist2); CopyMemory(lpPixel3,lpPixel2,iWidth*iHeight*4); break; case VK_F4: CopyMemory(lpPixel2,lpPixel,iWidth*iHeight*4); histgram(lpPixel2,hist); histsmooth(hist,hist2); CopyMemory(lpPixel3,lpPixel2,iWidth*iHeight*4); break; case VK_UP: py--; break; case VK_DOWN: py++; break; case VK_LEFT: amp-=0.1; break; case VK_RIGHT: amp+=0.1; break; case '1': ope=1; break; case '2': ope=2; break; case '3': ope=3; break; case VK_RETURN: if(py>=300 && py<300+256) threshold(lpPixel2,lpPixel3,py-300,2); else CopyMemory(lpPixel3,lpPixel2,iWidth*iHeight*4); break; } InvalidateRect(hWnd,NULL,TRUE); break; case WM_CLOSE: HeapFree(GetProcessHeap(),0,lpPixel2); DestroyWindow(hWnd); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd,msg,wp,lp); } return 0; }