update for HEAD-2003050101
[reactos.git] / apps / tests / dibtest / dibtest.c
index 81d532b..3ed2ebf 100644 (file)
 #include <windows.h>
+#include <stdio.h>
+#include <stdlib.h>
 
-extern BOOL STDCALL GdiDllInitialize(HANDLE hInstance, DWORD Event, LPVOID Reserved);
+#define CELL_SIZE 10
 
-void __stdcall Test1BPP (HDC Desktop)
+static RGBQUAD Colors[] =
 {
-  HDC TestDC;
-  HPEN WhitePen;
-  HBITMAP DIB1;
-  BITMAPINFOHEADER BitInf;
-  PBITMAPINFO BitPalInf;
-  DWORD bmiSize = sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 2;
-
-  // Create a 1BPP DIB
-  BitPalInf = (PBITMAPINFO)malloc(bmiSize);
-  BitInf.biSize = sizeof(BITMAPINFOHEADER);
-  BitInf.biWidth = 50;
-  BitInf.biHeight = -50; // it's top down (since BI_RGB is used, the sign is operative of direction)
-  BitInf.biPlanes = 1;
-  BitInf.biBitCount = 1;
-  BitInf.biCompression = BI_RGB;
-  BitInf.biSizeImage = 0;
-  BitInf.biXPelsPerMeter = 0;
-  BitInf.biYPelsPerMeter = 0;
-  BitInf.biClrUsed = 0;
-  BitInf.biClrImportant = 0;
-  BitPalInf->bmiHeader = BitInf;
-  BitPalInf->bmiColors[1].rgbBlue = 255;
-  BitPalInf->bmiColors[1].rgbGreen = 255;
-  BitPalInf->bmiColors[1].rgbRed = 255;
-  BitPalInf->bmiColors[1].rgbReserved = 255;
-  BitPalInf->bmiColors[0].rgbBlue = 0;
-  BitPalInf->bmiColors[0].rgbGreen = 0;
-  BitPalInf->bmiColors[0].rgbRed = 0;
-  BitPalInf->bmiColors[0].rgbReserved = 0;
-
-  DIB1 = (HBITMAP) CreateDIBSection(NULL, BitPalInf, DIB_RGB_COLORS, NULL, NULL, 0);
-  TestDC = CreateCompatibleDC(NULL);
-  SelectObject(TestDC, DIB1);
-
-  // Draw a white rectangle on the 1BPP DIB
-  WhitePen = CreatePen(PS_SOLID, 1, RGB(255, 255, 255));
-  SelectObject(TestDC, WhitePen);
-  Rectangle(TestDC, 0, 0, 40, 40);
-
-  // Blt the 1BPP DIB to the display
-  BitBlt(Desktop, 0, 0, 50, 50, TestDC, 0, 0, SRCCOPY);
-
-  free(BitPalInf);
-// Rectangle(Desktop, 50, 50, 200, 200);
+  { 0x00, 0x00, 0x00, 0x00 }, // black
+  { 0x00, 0x00, 0x80, 0x00 }, // red
+  { 0x00, 0x80, 0x00, 0x00 }, // green
+  { 0x00, 0x80, 0x80, 0x00 }, // brown
+  { 0x80, 0x00, 0x00, 0x00 }, // blue
+  { 0x80, 0x00, 0x80, 0x00 }, // magenta
+  { 0x80, 0x80, 0x00, 0x00 }, // cyan
+  { 0x80, 0x80, 0x80, 0x00 }, // dark gray
+  { 0xc0, 0xc0, 0xc0, 0x00 }, // light gray
+  { 0x00, 0x00, 0xFF, 0x00 }, // bright red
+  { 0x00, 0xFF, 0x00, 0x00 }, // bright green
+  { 0x00, 0xFF, 0xFF, 0x00 }, // bright yellow
+  { 0xFF, 0x00, 0x00, 0x00 }, // bright blue
+  { 0xFF, 0x00, 0xFF, 0x00 }, // bright magenta
+  { 0xFF, 0xFF, 0x00, 0x00 }, // bright cyan
+  { 0xFF, 0xFF, 0xFF, 0x00 }  // bright white
+};
+
+LRESULT WINAPI MainWndProc(HWND, UINT, WPARAM, LPARAM);
+
+int WINAPI 
+WinMain(HINSTANCE hInstance,
+       HINSTANCE hPrevInstance,
+       LPSTR lpszCmdLine,
+       int nCmdShow)
+{
+  WNDCLASS wc;
+  MSG msg;
+  HWND hWnd;
+
+  wc.lpszClassName = "DibTestClass";
+  wc.lpfnWndProc = MainWndProc;
+  wc.style = CS_VREDRAW | CS_HREDRAW;
+  wc.hInstance = hInstance;
+  wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
+  wc.hCursor = LoadCursor(NULL, IDC_ARROW);
+  wc.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
+  wc.lpszMenuName = NULL;
+  wc.cbClsExtra = 0;
+  wc.cbWndExtra = 0;
+  if (RegisterClass(&wc) == 0)
+    {
+      fprintf(stderr, "RegisterClass failed (last error 0x%X)\n",
+             GetLastError());
+      return(1);
+    }
+
+  hWnd = CreateWindow("DibTestClass",
+                     "DIB Test",
+                     WS_OVERLAPPEDWINDOW,
+                     CW_USEDEFAULT,
+                     CW_USEDEFAULT,
+                      25 * CELL_SIZE + 5,
+                      25 * CELL_SIZE + 20,
+                     NULL,
+                     NULL,
+                     hInstance,
+                     NULL);
+  if (hWnd == NULL)
+    {
+      fprintf(stderr, "CreateWindow failed (last error 0x%X)\n",
+             GetLastError());
+      return(1);
+    }
+
+  ShowWindow(hWnd, nCmdShow);
+
+  while(GetMessage(&msg, NULL, 0, 0))
+  {
+    TranslateMessage(&msg);
+    DispatchMessage(&msg);
+  }
+
+  return msg.wParam;
 }
 
-void DIBTest(void)
+static void PaintCells(HDC WindowDC, WORD BitCount1, WORD BitCount2,
+                       int XDest, int YDest)
 {
-  HDC  Desktop;
+  HBRUSH Brush;
+  RECT Rect;
+  UINT row, col;
+  BITMAPINFO *BitmapInfo;
+  HBITMAP DIB1, DIB2;
+  HDC DC1, DC2;
 
-  // Set up a DC called Desktop that accesses DISPLAY
-  Desktop = CreateDCA("DISPLAY", NULL, NULL, NULL);
-  if(Desktop == NULL) {
-       printf("Can't create desktop\n");
-    return;
+  BitmapInfo = malloc(sizeof(BITMAPINFO) + 15 * sizeof(RGBQUAD));
+  BitmapInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
+  BitmapInfo->bmiHeader.biWidth = 4 * CELL_SIZE + 9;
+  BitmapInfo->bmiHeader.biHeight = -(4 * CELL_SIZE + 9); // it's top down (since BI_RGB is used, the sign is operative of direction)
+  BitmapInfo->bmiHeader.biPlanes = 1;
+  BitmapInfo->bmiHeader.biBitCount = BitCount1;
+  BitmapInfo->bmiHeader.biCompression = BI_RGB;
+  BitmapInfo->bmiHeader.biSizeImage = 0;
+  BitmapInfo->bmiHeader.biXPelsPerMeter = 0;
+  BitmapInfo->bmiHeader.biYPelsPerMeter = 0;
+  BitmapInfo->bmiHeader.biClrUsed = 16;
+  BitmapInfo->bmiHeader.biClrImportant = 16;
+  for (col = 0; col < 16; col++) {
+    BitmapInfo->bmiColors[col] = Colors[col];
   }
+  DIB1 = CreateDIBSection(NULL, BitmapInfo, DIB_RGB_COLORS, NULL, NULL, 0);
+  DC1 = CreateCompatibleDC(NULL);
+  SelectObject(DC1, DIB1);
 
-  // 1BPP Test
-  Test1BPP(Desktop);
+  BitmapInfo->bmiHeader.biBitCount = BitCount2;
+  DIB2 = CreateDIBSection(NULL, BitmapInfo, DIB_RGB_COLORS, NULL, NULL, 0);
+  DC2 = CreateCompatibleDC(NULL);
+  SelectObject(DC2, DIB2);
+  free(BitmapInfo);
 
-  Sleep(50000);
+  /* Now paint on the first bitmap */
+  for (row = 0; row < 4; row++)
+    {
+    for (col = 0; col < 4; col++)
+      {
+      Brush = CreateSolidBrush(RGB(Colors[4 * row + col].rgbRed,
+                                   Colors[4 * row + col].rgbGreen,
+                                   Colors[4 * row + col].rgbBlue));
+      Rect.left = CELL_SIZE * col + 5;
+      Rect.top = CELL_SIZE * row + 5;
+      Rect.right = Rect.left + CELL_SIZE;
+      Rect.bottom = Rect.top + CELL_SIZE;
+      FillRect(DC1, &Rect, Brush);
+      DeleteObject(Brush);
+      }
+    }
 
-  // Free up everything
-  DeleteDC(Desktop);
+  /* Copy the first bitmap to the second */
+  BitBlt(DC2, 4, 4, 4 * CELL_SIZE, 4 * CELL_SIZE, DC1, 5, 5, SRCCOPY);
+
+  /* Show results on screen */
+  BitBlt(WindowDC, XDest, YDest, 4 * CELL_SIZE, 4 * CELL_SIZE, DC2, 4, 4, SRCCOPY);
 }
 
-int main(int argc, char* argv[])
+LRESULT CALLBACK MainWndProc(HWND Wnd, UINT msg, WPARAM wParam, LPARAM lParam)
 {
-  printf("Entering DIBTest..\n");
+  PAINTSTRUCT ps;
+  HDC WindowDC;
+
+  switch(msg)
+    {
+    case WM_PAINT:
+      WindowDC = BeginPaint(Wnd, &ps);
+
+      PaintCells(WindowDC, 4,  4,  0,              0);
+      PaintCells(WindowDC, 4,  8,  5  * CELL_SIZE, 0);
+      PaintCells(WindowDC, 4,  16, 10 * CELL_SIZE, 0);
+      PaintCells(WindowDC, 4,  24, 15 * CELL_SIZE, 0);
+      PaintCells(WindowDC, 4,  32, 20 * CELL_SIZE, 0);
+
+      PaintCells(WindowDC, 8,  4,  0,              5  * CELL_SIZE);
+      PaintCells(WindowDC, 8,  8,  5  * CELL_SIZE, 5  * CELL_SIZE);
+      PaintCells(WindowDC, 8,  16, 10 * CELL_SIZE, 5  * CELL_SIZE);
+      PaintCells(WindowDC, 8,  24, 15 * CELL_SIZE, 5  * CELL_SIZE);
+      PaintCells(WindowDC, 8,  32, 20 * CELL_SIZE, 5  * CELL_SIZE);
+
+      PaintCells(WindowDC, 16, 4,  0,              10 * CELL_SIZE);
+      PaintCells(WindowDC, 16, 8,  5  * CELL_SIZE, 10 * CELL_SIZE);
+      PaintCells(WindowDC, 16, 16, 10 * CELL_SIZE, 10 * CELL_SIZE);
+      PaintCells(WindowDC, 16, 24, 15 * CELL_SIZE, 10 * CELL_SIZE);
+      PaintCells(WindowDC, 16, 32, 20 * CELL_SIZE, 10 * CELL_SIZE);
+
+      PaintCells(WindowDC, 24, 4,  0,              15 * CELL_SIZE);
+      PaintCells(WindowDC, 24, 8,  5  * CELL_SIZE, 15 * CELL_SIZE);
+      PaintCells(WindowDC, 24, 16, 10 * CELL_SIZE, 15 * CELL_SIZE);
+      PaintCells(WindowDC, 24, 24, 15 * CELL_SIZE, 15 * CELL_SIZE);
+      PaintCells(WindowDC, 24, 32, 20 * CELL_SIZE, 15 * CELL_SIZE);
+
+      PaintCells(WindowDC, 32, 4,  0,              20 * CELL_SIZE);
+      PaintCells(WindowDC, 32, 8,  5  * CELL_SIZE, 20 * CELL_SIZE);
+      PaintCells(WindowDC, 32, 16, 10 * CELL_SIZE, 20 * CELL_SIZE);
+      PaintCells(WindowDC, 32, 24, 15 * CELL_SIZE, 20 * CELL_SIZE);
+      PaintCells(WindowDC, 32, 32, 20 * CELL_SIZE, 20 * CELL_SIZE);
+
+      EndPaint(Wnd, &ps);
+      break;
+
+    case WM_DESTROY:
+      PostQuitMessage(0);
+      break;
 
-  GdiDllInitialize (NULL, DLL_PROCESS_ATTACH, NULL);
-  DIBTest();
+    default:
+      return DefWindowProc(Wnd, msg, wParam, lParam);
+    }
 
   return 0;
 }