Fix some bugs in Paint.
[reactos.git] / reactos / base / applications / paint / dib.c
1 /*
2 * PROJECT: PAINT for ReactOS
3 * LICENSE: LGPL
4 * FILE: dib.c
5 * PURPOSE: Some DIB related functions
6 * PROGRAMMERS: Benedikt Freisen
7 */
8
9 /* INCLUDES *********************************************************/
10
11 #include <windows.h>
12
13 /* FUNCTIONS ********************************************************/
14
15 HBITMAP CreateDIBWithProperties(int width, int height)
16 {
17 BITMAPINFO bitmapinfo;
18 bitmapinfo.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
19 bitmapinfo.bmiHeader.biWidth = width;
20 bitmapinfo.bmiHeader.biHeight = height;
21 bitmapinfo.bmiHeader.biPlanes = 1;
22 bitmapinfo.bmiHeader.biBitCount = 24;
23 bitmapinfo.bmiHeader.biCompression = BI_RGB;
24 bitmapinfo.bmiHeader.biSizeImage = 0;
25 bitmapinfo.bmiHeader.biXPelsPerMeter = 0;
26 bitmapinfo.bmiHeader.biYPelsPerMeter = 0;
27 bitmapinfo.bmiHeader.biClrUsed = 0;
28 bitmapinfo.bmiHeader.biClrImportant = 0;
29 return CreateDIBSection(NULL, &bitmapinfo, DIB_RGB_COLORS, NULL, NULL, 0);
30 }
31
32 int GetDIBWidth(HBITMAP hbm)
33 {
34 BITMAP bm;
35 GetObject(hbm, sizeof(BITMAP), &bm);
36 return bm.bmWidth;
37 }
38
39 int GetDIBHeight(HBITMAP hbm)
40 {
41 BITMAP bm;
42 GetObject(hbm, sizeof(BITMAP), &bm);
43 return bm.bmHeight;
44 }
45
46 void SaveDIBToFile(HBITMAP hbm, LPSTR name, HDC hdc)
47 {
48 BITMAP bm;
49 GetObject(hbm, sizeof(BITMAP), &bm);
50 BITMAPFILEHEADER bf;
51 BITMAPINFOHEADER bi;
52 int imgDataSize = bm.bmWidthBytes*bm.bmHeight;
53 bf.bfType = 0x4d42;
54 bf.bfSize = imgDataSize+52;
55 bf.bfReserved1 = 0;
56 bf.bfReserved2 = 0;
57 bf.bfOffBits = 54;
58 bi.biSize = 40;
59 bi.biWidth = bm.bmWidth;
60 bi.biHeight = bm.bmHeight;
61 bi.biPlanes = bm.bmPlanes;
62 bi.biBitCount = bm.bmBitsPixel;
63 bi.biCompression = BI_RGB;
64 bi.biSizeImage = 0;
65 bi.biXPelsPerMeter = 0;
66 bi.biYPelsPerMeter = 0;
67 bi.biClrUsed = 0;
68 bi.biClrImportant = 0;
69 int *buffer = HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS, imgDataSize);
70 GetDIBits(hdc, hbm, 0, bm.bmHeight, buffer, (LPBITMAPINFO)&bi, DIB_RGB_COLORS);
71 HANDLE f = CreateFile(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
72 int bytesWritten;
73 WriteFile(f, &bf, 14, (LPDWORD)&bytesWritten, NULL);
74 WriteFile(f, &bi, 40, (LPDWORD)&bytesWritten, NULL);
75 WriteFile(f, buffer, imgDataSize, (LPDWORD)&bytesWritten, NULL);
76 CloseHandle(f);
77 HeapFree(GetProcessHeap(), 0, buffer);
78 }
79
80 HBITMAP LoadDIBFromFile(LPSTR name)
81 {
82 HBITMAP bm;
83 BITMAPFILEHEADER bfh;
84 BITMAPINFO *bi;
85 VOID *data;
86 int bytesRead;
87 HANDLE f = CreateFile(name, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
88 ReadFile(f, &bfh, 14, (LPDWORD)&bytesRead, NULL);
89 if (bfh.bfType!=0x4d42)
90 {
91 CloseHandle(f);
92 return NULL;
93 }
94 bi = HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS, bfh.bfOffBits-14);
95 ReadFile(f, bi, bfh.bfOffBits-14, (LPDWORD)&bytesRead, NULL);
96 bm = CreateDIBSection(NULL, bi, DIB_RGB_COLORS, &data, NULL, 0);
97 ReadFile(f, data, bfh.bfSize-bfh.bfOffBits, (LPDWORD)&bytesRead, NULL);
98 CloseHandle(f);
99 HeapFree(GetProcessHeap(), 0, bi);
100 return bm;
101 }