#include #include #include"resource.h" #include"hensu.h" #include"bmp_io.h" #include"clip_board.h" #include"HSY.h" LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); ATOM InitApp(HINSTANCE); BOOL InitInstance(HINSTANCE,int); LRESULT CALLBACK MyDlgProc(HWND hDlg,UINT msg,WPARAM wp,LPARAM lp); char szClassName[]="HSY"; int iWidth,iHeight; HINSTANCE hInst; int WINAPI WinMain(HINSTANCE hCurInst,HINSTANCE hPrevInst, LPSTR lpsCmdLine,int nCmdShow) { MSG msg; BOOL bRet; hInst=hCurInst; 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, "第8章 色を変える", 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; } enum COLOR {RGB,H,S,Y}; LPDWORD lpPixel,lpPixelRGB,lpPixelShow; double *hue,*hue2,*sat,*sat2,*Y1,*Y2,*C1,*C2; LRESULT CALLBACK WndProc(HWND hWnd,UINT msg,WPARAM wp,LPARAM lp) { static BMP_IO mybmp; static LPBITMAPINFO lpbiInfo; HDROP hDrop; char szFileName[256]; HMENU hMenu; MENUITEMINFO mi; static double w,h; static enum COLOR color; HDC hdc; PAINTSTRUCT ps; switch(msg){ case WM_CREATE: DragAcceptFiles(hWnd,TRUE); w=1; h=1; color=RGB; 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; if(lpPixelShow!=NULL) HeapFree(GetProcessHeap(),0,lpPixelShow); if(hue!=NULL) HeapFree(GetProcessHeap(),0,hue); lpPixelShow=(LPDWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,2*iWidth*iHeight*sizeof(DWORD)); lpPixelRGB=lpPixelShow+iWidth*iHeight; hue=(double *)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,8*iWidth*iHeight*sizeof(double)); sat =hue+iWidth*iHeight; Y1 =hue+iWidth*iHeight*2; C1 =hue+iWidth*iHeight*3; C2 =hue+iWidth*iHeight*4; hue2=hue+iWidth*iHeight*5; sat2=hue+iWidth*iHeight*6; Y2 =hue+iWidth*iHeight*7; CopyMemory(lpPixelShow,lpPixel,iWidth*iHeight*4); CopyMemory(lpPixelRGB,lpPixel,iWidth*iHeight*4); w=1; h=1; color=RGB; InvalidateRect(hWnd,NULL,TRUE); break; case WM_PAINT: hdc=BeginPaint(hWnd,&ps); StretchDIBits(hdc,0,0,(int)(w*iWidth),(int)(h*iHeight), 0,0,iWidth,iHeight,lpPixelShow,lpbiInfo, DIB_RGB_COLORS,SRCCOPY); EndPaint(hWnd,&ps); break; case WM_COMMAND: switch(LOWORD(wp)){ case IDM_COPY: CopyDIB(hWnd,lpPixelShow); break; case IDM_PASTE: if(!mybmp.PasteDIBSection(hWnd)) break; lpbiInfo=&mybmp.biInfo; lpPixel=mybmp.lpPixel; iWidth=mybmp.iWidth; iHeight=mybmp.iHeight; if(lpPixelShow!=NULL) HeapFree(GetProcessHeap(),0,lpPixelShow); if(hue!=NULL) HeapFree(GetProcessHeap(),0,hue); lpPixelShow=(LPDWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,2*iWidth*iHeight*sizeof(DWORD)); lpPixelRGB=lpPixelShow+iWidth*iHeight; hue=(double *)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,8*iWidth*iHeight*sizeof(double)); sat =hue+iWidth*iHeight; Y1 =hue+iWidth*iHeight*2; C1 =hue+iWidth*iHeight*3; C2 =hue+iWidth*iHeight*4; hue2=hue+iWidth*iHeight*5; sat2=hue+iWidth*iHeight*6; Y2 =hue+iWidth*iHeight*7; CopyMemory(lpPixelShow,lpPixel,iWidth*iHeight*4); CopyMemory(lpPixelRGB,lpPixel,iWidth*iHeight*4); w=1; h=1; color=RGB; break; case IDM_H: color=H; rgb_to_yc(lpPixelRGB,Y1,C1,C2); c_to_sh(C1,C2,sat,hue); hue_image(sat,hue,113.2,lpPixelShow); //113.2=赤 break; case IDM_S: color=S; rgb_to_yc(lpPixelRGB,Y1,C1,C2); c_to_sh(C1,C2,sat,hue); sat_image(sat,lpPixelShow); break; case IDM_Y: color=Y; rgb_to_yc(lpPixelRGB,Y1,C1,C2); y_image(Y1,lpPixelShow); break; case IDM_RGB: color=RGB; CopyMemory(lpPixelShow,lpPixelRGB,iWidth*iHeight*4); break; case IDM_ADJUST: color=RGB; DialogBox(hInst,"MYDLG",hWnd,(DLGPROC)MyDlgProc); break; case IDM_ORIGINAL: color=RGB; CopyMemory(lpPixelShow,lpPixel,iWidth*iHeight*4); CopyMemory(lpPixelRGB,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; mi.fState=MFS_UNCHECKED; SetMenuItemInfo(hMenu,IDM_RGB,FALSE,&mi); SetMenuItemInfo(hMenu,IDM_H,FALSE,&mi); SetMenuItemInfo(hMenu,IDM_S,FALSE,&mi); SetMenuItemInfo(hMenu,IDM_Y,FALSE,&mi); switch(color){ case RGB: mi.fState=MFS_CHECKED; SetMenuItemInfo(hMenu,IDM_RGB,FALSE,&mi); break; case H: mi.fState=MFS_CHECKED; SetMenuItemInfo(hMenu,IDM_H,FALSE,&mi); break; case S: mi.fState=MFS_CHECKED; SetMenuItemInfo(hMenu,IDM_S,FALSE,&mi); break; case Y: mi.fState=MFS_CHECKED; SetMenuItemInfo(hMenu,IDM_Y,FALSE,&mi); break; } if(IsClipboardFormatAvailable(CF_DIB)){ mi.fState=MFS_ENABLED; SetMenuItemInfo(hMenu,IDM_PASTE,FALSE,&mi); }else{ mi.fState=MFS_GRAYED; SetMenuItemInfo(hMenu,IDM_PASTE,FALSE,&mi); } 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_CLOSE: HeapFree(GetProcessHeap(),0,lpPixelShow); HeapFree(GetProcessHeap(),0,hue); DestroyWindow(hWnd); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd,msg,wp,lp); } return 0; } LRESULT CALLBACK MyDlgProc(HWND hDlg,UINT msg,WPARAM wp,LPARAM lp) { static HWND hStaticH,hStaticS,hStaticY; static HWND hSliderH,hSliderS,hSliderY; static int h,s,y; char buf[8]; switch(msg){ case WM_INITDIALOG: hStaticH=GetDlgItem(hDlg,IDC_STATIC_H); hStaticS=GetDlgItem(hDlg,IDC_STATIC_S); hStaticY=GetDlgItem(hDlg,IDC_STATIC_Y); hSliderH=GetDlgItem(hDlg,IDC_SLIDER_H); hSliderS=GetDlgItem(hDlg,IDC_SLIDER_S); hSliderY=GetDlgItem(hDlg,IDC_SLIDER_Y); SendMessage(hSliderH,TBM_SETRANGE,(WPARAM)TRUE,(LPARAM)MAKELONG(-180,180)); SendMessage(hSliderH,TBM_SETPOS,(WPARAM)TRUE,(LPARAM)0); SendMessage(hSliderS,TBM_SETRANGE,(WPARAM)TRUE,(LPARAM)MAKELONG(-100,100)); SendMessage(hSliderS,TBM_SETPOS,(WPARAM)TRUE,(LPARAM)0); SendMessage(hSliderY,TBM_SETRANGE,(WPARAM)TRUE,(LPARAM)MAKELONG(-100,100)); SendMessage(hSliderY,TBM_SETPOS,(WPARAM)TRUE,(LPARAM)0); wsprintf(buf,"%d 度",0); SetWindowText(hStaticH,buf); wsprintf(buf,"%d %%",0); SetWindowText(hStaticS,buf); SetWindowText(hStaticY,buf); h=0; s=0; y=0; rgb_to_yc(lpPixelRGB,Y1,C1,C2); c_to_sh(C1,C2,sat,hue); CopyMemory(lpPixelShow,lpPixelRGB,iWidth*iHeight*4); InvalidateRect(GetParent(hDlg),NULL,FALSE); return TRUE; case WM_HSCROLL: if((HWND)lp==hSliderH){ h=(int)SendMessage(hSliderH,TBM_GETPOS,0,0); wsprintf(buf,"%d 度",h); SetWindowText(hStaticH,buf); }else if((HWND)lp==hSliderS){ s=(int)SendMessage(hSliderS,TBM_GETPOS,0,0); wsprintf(buf,"%d %%",s); SetWindowText(hStaticS,buf); }else if((HWND)lp==hSliderY){ y=(int)SendMessage(hSliderY,TBM_GETPOS,0,0); wsprintf(buf,"%d %%",y); SetWindowText(hStaticY,buf); } tran_hsy(hue,sat,Y1,hue2,sat2,Y2,h,s,y); sh_to_c(sat2,hue2,C1,C2); yc_to_rgb(Y2,C1,C2,lpPixelShow); InvalidateRect(GetParent(hDlg),NULL,TRUE); return TRUE; case WM_COMMAND: switch(LOWORD(wp)){ case IDOK: CopyMemory(lpPixelRGB,lpPixelShow,iWidth*iHeight*4); EndDialog(hDlg,IDOK); return TRUE; case IDCANCEL: CopyMemory(lpPixelShow,lpPixelRGB,iWidth*iHeight*4); EndDialog(hDlg,IDCANCEL); return TRUE; } return FALSE; } return FALSE; }