xref: /reactos/base/applications/mspaint/dib.cpp (revision 84ccccab)
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     CString strText;
77     strText.Format(IDS_LOADERRORTEXT, (LPCTSTR) name);
78     CString strProgramName;
79     strProgramName.LoadString(IDS_PROGRAMNAME);
80     mainWindow.MessageBox(strText, strProgramName, MB_OK | MB_ICONEXCLAMATION);
81 }
82 
83 void
84 LoadDIBFromFile(HBITMAP * hBitmap, LPCTSTR name, LPSYSTEMTIME time, int *size, int *hRes, int *vRes)
85 {
86     using namespace Gdiplus;
87     Bitmap img(CStringW(name), FALSE);  // always use WCHAR string
88 
89     if (!hBitmap)
90     {
91         ShowFileLoadError(name);
92         return;
93     }
94 
95     img.GetHBITMAP(Color(255, 255, 255), hBitmap);
96 
97     // update time and size
98     HANDLE hFile =
99         CreateFile(name, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
100     if (hFile == INVALID_HANDLE_VALUE)
101     {
102         ShowFileLoadError(name);
103         return;
104     }
105 
106     if (time)
107     {
108         FILETIME ft;
109         GetFileTime(hFile, NULL, NULL, &ft);
110         FileTimeToSystemTime(&ft, time);
111     }
112     if (size)
113         *size = GetFileSize(hFile, NULL);
114 
115     // update hRes and vRes
116     *hRes = (int) (img.GetHorizontalResolution() * 1000 / 25.4);
117     *vRes = (int) (img.GetVerticalResolution() * 1000 / 25.4);
118 
119     CloseHandle(hFile);
120 }
121