ffbc33f3359fcdfb8d4c53744550827512b7014f
[reactos.git] / reactos / base / applications / mspaint / dib.cpp
1 /*
2 * PROJECT: PAINT for ReactOS
3 * LICENSE: LGPL
4 * FILE: base/applications/mspaint/dib.cpp
5 * PURPOSE: Some DIB related functions
6 * PROGRAMMERS: Benedikt Freisen
7 */
8
9 /* INCLUDES *********************************************************/
10
11 #include "precomp.h"
12
13 /* FUNCTIONS ********************************************************/
14
15 HBITMAP
16 CreateDIBWithProperties(int width, int height)
17 {
18 BITMAPINFO bmi;
19 ZeroMemory(&bmi, sizeof(BITMAPINFO));
20 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
21 bmi.bmiHeader.biWidth = width;
22 bmi.bmiHeader.biHeight = height;
23 bmi.bmiHeader.biPlanes = 1;
24 bmi.bmiHeader.biBitCount = 24;
25 bmi.bmiHeader.biCompression = BI_RGB;
26 return CreateDIBSection(NULL, &bmi, DIB_RGB_COLORS, NULL, NULL, 0);
27 }
28
29 int
30 GetDIBWidth(HBITMAP hBitmap)
31 {
32 BITMAP bm;
33 GetObject(hBitmap, sizeof(BITMAP), &bm);
34 return bm.bmWidth;
35 }
36
37 int
38 GetDIBHeight(HBITMAP hBitmap)
39 {
40 BITMAP bm;
41 GetObject(hBitmap, sizeof(BITMAP), &bm);
42 return bm.bmHeight;
43 }
44
45 void
46 SaveDIBToFile(HBITMAP hBitmap, LPTSTR FileName, HDC hDC, LPSYSTEMTIME time, int *size, int hRes, int vRes)
47 {
48 CImage img;
49 img.Attach(hBitmap);
50 img.Save(FileName); // TODO: error handling
51 img.Detach();
52
53 // update time and size
54
55 HANDLE hFile =
56 CreateFile(FileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
57 if (hFile == INVALID_HANDLE_VALUE)
58 return;
59
60 if (time)
61 {
62 FILETIME ft;
63 GetFileTime(hFile, NULL, NULL, &ft);
64 FileTimeToSystemTime(&ft, time);
65 }
66 if (size)
67 *size = GetFileSize(hFile, NULL);
68
69 // TODO: update hRes and vRes
70
71 CloseHandle(hFile);
72 }
73
74 void ShowFileLoadError(LPCTSTR name)
75 {
76 TCHAR programname[20];
77 TCHAR loaderrortext[100];
78 TCHAR temptext[500];
79 LoadString(hProgInstance, IDS_PROGRAMNAME, programname, SIZEOF(programname));
80 LoadString(hProgInstance, IDS_LOADERRORTEXT, loaderrortext, SIZEOF(loaderrortext));
81 _stprintf(temptext, loaderrortext, name);
82 mainWindow.MessageBox(temptext, programname, MB_OK | MB_ICONEXCLAMATION);
83 }
84
85 void
86 LoadDIBFromFile(HBITMAP * hBitmap, LPCTSTR name, LPSYSTEMTIME time, int *size, int *hRes, int *vRes)
87 {
88 CImage img;
89 img.Load(name); // TODO: error handling
90
91 if (!hBitmap)
92 {
93 ShowFileLoadError(name);
94 return;
95 }
96
97 *hBitmap = img.Detach();
98
99 // update time and size
100 HANDLE hFile =
101 CreateFile(name, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
102 if (hFile == INVALID_HANDLE_VALUE)
103 {
104 ShowFileLoadError(name);
105 return;
106 }
107
108 if (time)
109 {
110 FILETIME ft;
111 GetFileTime(hFile, NULL, NULL, &ft);
112 FileTimeToSystemTime(&ft, time);
113 }
114 if (size)
115 *size = GetFileSize(hFile, NULL);
116
117 // TODO: update hRes and vRes
118
119 CloseHandle(hFile);
120 }