/* RGB各10ビットの32ビットDIB 2003/ 8/ 2 宍戸 輝光 */ #include LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); HINSTANCE hInst; HWND hwMain; LPBYTE lpDIB; LPBITMAPINFO lpbiInfo; LPDWORD lpBitFields; LPDWORD lpPixel; 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", "RGB各10ビットの32ビットDIB", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 540, 560, NULL, NULL, hInstance, NULL); /* ウインドウを表示 */ 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) { HDC hdc; PAINTSTRUCT ps; int x, y, r, g, b; int i, j; char astr[256]; switch (iMsg) { case WM_CREATE: /* DIB用バッファ確保 */ lpDIB = (LPBYTE)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFO) + 4 * 2 + 512 * 512 * 4); /* BITMAPINFOのポインタ設定 */ lpbiInfo = (LPBITMAPINFO)lpDIB; /* ビットフィールドのポインタ設定 */ lpBitFields = (LPDWORD)(lpbiInfo->bmiColors); /* ピクセル列のポインタ設定 */ lpPixel = (LPDWORD)(lpBitFields + 3); /* BITMAPINFO内のBITMAPINFOHEADER設定 */ lpbiInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); lpbiInfo->bmiHeader.biWidth = 512; lpbiInfo->bmiHeader.biHeight = -512; lpbiInfo->bmiHeader.biPlanes = 1; lpbiInfo->bmiHeader.biBitCount = 32; lpbiInfo->bmiHeader.biCompression = BI_BITFIELDS; /* R成分ビットフィールド */ lpBitFields[0] = 1023 << 20; /* G成分ビットフィールド */ lpBitFields[1] = 1023 << 10; /* B成分ビットフィールド */ lpBitFields[2] = 1023; /* 赤のグラデーション */ for (i = 0;i < 128;i++) { for (j = 0;j < 512;j++) { lpPixel[j + i * 512] = (j + 512) << 20; } } /* 緑のグラデーション */ for (i = 128;i < 256;i++) { for (j = 0;j < 512;j++) { lpPixel[j + i * 512] = (j + 512) << 10; } } /* 青のグラデーション */ for (i = 256;i < 384;i++) { for (j = 0;j < 512;j++) { lpPixel[j + i * 512] = (j + 512); } } /* 白黒のグラデーション */ for (i = 384;i < 512;i++) { for (j = 0;j < 512;j++) { r = g = b = (j + 512); lpPixel[j + i * 512] = (r << 20) + (g << 10) + b; } } return 0; case WM_MOUSEMOVE: /* マウスカーソルの座標取得 */ x = LOWORD(lParam); y = HIWORD(lParam); if (lpDIB == NULL || x < 0 || x >= 512 || y >= 512 || y < 0) { return 0; } /* ピクセルのRGB成分算出 */ r = (lpPixel[x + y * 512] & lpBitFields[0]) >> 20; g = (lpPixel[x + y * 512] & lpBitFields[1]) >> 10; b = lpPixel[x + y * 512] & lpBitFields[2]; wsprintf(astr, "(%03d,%03d) - %04d:%04d:%04d", x, y, r, g, b); SetWindowText(hwnd, astr); return 0; case WM_PAINT: hdc=BeginPaint(hwnd,&ps); /* DIBを画面に描画 */ StretchDIBits(hdc, 0, 0, 512, 512, 0, 0, 512, 512, lpPixel, lpbiInfo, DIB_RGB_COLORS, SRCCOPY); EndPaint(hwnd, &ps); return 0; case WM_DESTROY : if (lpDIB != NULL) { /* DIB用メモリ解放 */ HeapFree(GetProcessHeap(), 0, lpDIB); } PostQuitMessage(0); return 0; } return DefWindowProc (hwnd, iMsg, wParam, lParam) ; }