#include #include"resource.h" #include"bmp_io.h" #include"hensu.h" #include"noise_clear.h" #include"noise.h" #include"clip_board.h" #include #include 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="MYMENU"; 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, "第5章 雑音を除く", 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,lpPixel4; HDROP hDrop; char szFileName[256]; static int wx,wy; static double w,h; HMENU hMenu; MENUITEMINFO mi; HDC hdc; PAINTSTRUCT ps; switch(msg){ case WM_CREATE: DragAcceptFiles(hWnd,TRUE); srand((unsigned int)time(NULL)); wx=0; wy=0; w=1; h=1; 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(),HEAP_ZERO_MEMORY,3*iWidth*iHeight*4); lpPixel3=lpPixel2+iWidth*iHeight; lpPixel4=lpPixel3+iWidth*iHeight; CopyMemory(lpPixel2,lpPixel,iWidth*iHeight*4); CopyMemory(lpPixel3,lpPixel,iWidth*iHeight*4); CopyMemory(lpPixel4,lpPixel,iWidth*iHeight*4); wx=0; wy=0; w=1; h=1; InvalidateRect(hWnd,NULL,TRUE); break; case WM_PAINT: hdc=BeginPaint(hWnd,&ps); StretchDIBits(hdc,wx,wy,(int)(w*iWidth),(int)(h*iHeight), 0,0,iWidth,iHeight,lpPixel3,lpbiInfo, DIB_RGB_COLORS,SRCCOPY); EndPaint(hWnd,&ps); break; case WM_COMMAND: switch(LOWORD(wp)){ case IDM_COPY: CopyDIB(hWnd,lpPixel3); break; case IDM_NGRAY: noise(lpPixel2); CopyMemory(lpPixel3,lpPixel2,iWidth*iHeight*4); CopyMemory(lpPixel4,lpPixel2,iWidth*iHeight*4); break; case IDM_NMONO: noise_mono(lpPixel2); CopyMemory(lpPixel3,lpPixel2,iWidth*iHeight*4); CopyMemory(lpPixel4,lpPixel2,iWidth*iHeight*4); break; case IDM_NCIDOU: smooth(lpPixel3,lpPixel4); CopyMemory(lpPixel3,lpPixel4,iWidth*iHeight*4); break; case IDM_NCMEDIAN: median(lpPixel3,lpPixel4); CopyMemory(lpPixel3,lpPixel4,iWidth*iHeight*4); break; case IDM_NCERO: erosion(lpPixel3,lpPixel4); CopyMemory(lpPixel3,lpPixel4,iWidth*iHeight*4); break; case IDM_NCDILA: dilation(lpPixel3,lpPixel4); CopyMemory(lpPixel3,lpPixel4,iWidth*iHeight*4); break; case IDM_NCES: edge_smooth(lpPixel3,lpPixel4); CopyMemory(lpPixel3,lpPixel4,iWidth*iHeight*4); break; case IDM_BEFORE_NC: CopyMemory(lpPixel3,lpPixel2,iWidth*iHeight*4); break; case IDM_ORIGINAL: CopyMemory(lpPixel2,lpPixel,iWidth*iHeight*4); CopyMemory(lpPixel3,lpPixel,iWidth*iHeight*4); CopyMemory(lpPixel4,lpPixel,iWidth*iHeight*4); break; case IDM_ZOOM1: w=1; h=1; break; case IDM_ZOOM2: w=2; h=2; break; case IDM_ZOOM3: w=3; h=3; break; case IDM_ZOOM4: w=4; h=4; break; case IDM_ZOOM02: w=1.0/2; h=1.0/2; break; case IDM_ZOOM03: w=1.0/3; h=1.0/3; break; case IDM_ZOOM04: w=1.0/4; h=1.0/4; break; } InvalidateRect(hWnd,NULL,TRUE); break; case WM_INITMENU: hMenu=GetMenu(hWnd); mi.cbSize=sizeof(MENUITEMINFO); mi.fMask=MIIM_STATE; if(w==1){ mi.fState=MFS_CHECKED; SetMenuItemInfo(hMenu,IDM_ZOOM1,FALSE,&mi); }else{ mi.fState=MFS_UNCHECKED; SetMenuItemInfo(hMenu,IDM_ZOOM1,FALSE,&mi); } if(w==2){ mi.fState=MFS_CHECKED; SetMenuItemInfo(hMenu,IDM_ZOOM2,FALSE,&mi); }else{ mi.fState=MFS_UNCHECKED; SetMenuItemInfo(hMenu,IDM_ZOOM2,FALSE,&mi); } if(w==3){ mi.fState=MFS_CHECKED; SetMenuItemInfo(hMenu,IDM_ZOOM3,FALSE,&mi); }else{ mi.fState=MFS_UNCHECKED; SetMenuItemInfo(hMenu,IDM_ZOOM3,FALSE,&mi); } if(w==4){ mi.fState=MFS_CHECKED; SetMenuItemInfo(hMenu,IDM_ZOOM4,FALSE,&mi); }else{ mi.fState=MFS_UNCHECKED; SetMenuItemInfo(hMenu,IDM_ZOOM4,FALSE,&mi); } if(w==1.0/2){ mi.fState=MFS_CHECKED; SetMenuItemInfo(hMenu,IDM_ZOOM02,FALSE,&mi); }else{ mi.fState=MFS_UNCHECKED; SetMenuItemInfo(hMenu,IDM_ZOOM02,FALSE,&mi); } if(w==1.0/3){ mi.fState=MFS_CHECKED; SetMenuItemInfo(hMenu,IDM_ZOOM03,FALSE,&mi); }else{ mi.fState=MFS_UNCHECKED; SetMenuItemInfo(hMenu,IDM_ZOOM03,FALSE,&mi); } if(w==1.0/4){ mi.fState=MFS_CHECKED; SetMenuItemInfo(hMenu,IDM_ZOOM04,FALSE,&mi); }else{ mi.fState=MFS_UNCHECKED; SetMenuItemInfo(hMenu,IDM_ZOOM04,FALSE,&mi); } DrawMenuBar(hWnd); break; case WM_KEYDOWN: switch(LOWORD(wp)){ case VK_UP: wy++; break; case VK_DOWN: wy--; break; case VK_LEFT: wx++; break; case VK_RIGHT: wx--; 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; }