ReactOS 0.4.15-dev-5893-g1bb4167
dib.cpp
Go to the documentation of this file.
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#include "precomp.h"
10#include <math.h>
11
13float g_xDpi = 96;
14float g_yDpi = 96;
16
17/* FUNCTIONS ********************************************************/
18
19// Convert DPI (dots per inch) into PPM (pixels per meter)
20float PpmFromDpi(float dpi)
21{
22 return dpi / 0.0254; // 1 DPI is 0.0254 meter.
23}
24
27{
28 BITMAPINFO bmi;
29 ZeroMemory(&bmi, sizeof(BITMAPINFO));
30 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
33 bmi.bmiHeader.biPlanes = 1;
34 bmi.bmiHeader.biBitCount = 24;
36 return CreateDIBSection(NULL, &bmi, DIB_RGB_COLORS, NULL, NULL, 0);
37}
38
41{
43 if (!ret)
44 return NULL;
45
46 if (rgb)
47 {
49 HGDIOBJ hbmOld = SelectObject(hdc, ret);
50 RECT rc;
51 SetRect(&rc, 0, 0, width, height);
52 HBRUSH hbr = CreateSolidBrush(rgb);
53 FillRect(hdc, &rc, hbr);
54 DeleteObject(hbr);
55 SelectObject(hdc, hbmOld);
57 }
58
59 return ret;
60}
61
62int
64{
65 BITMAP bm;
66 GetObject(hBitmap, sizeof(BITMAP), &bm);
67 return bm.bmWidth;
68}
69
70int
72{
73 BITMAP bm;
74 GetObject(hBitmap, sizeof(BITMAP), &bm);
75 return bm.bmHeight;
76}
77
79{
81 img.Attach(hBitmap);
82 img.SaveDx(FileName, GUID_NULL, g_xDpi, g_yDpi); // TODO: error handling
83 img.Detach();
84
87 if (hFind == INVALID_HANDLE_VALUE)
88 {
90 return FALSE;
91 }
92 FindClose(hFind);
93
94 // update time and size
95 FILETIME ft;
96 FileTimeToLocalFileTime(&find.ftLastWriteTime, &ft);
98 fileSize = find.nFileSizeLow;
99
100 // TODO: update hRes and vRes
101
103
104 isAFile = TRUE;
106 return TRUE;
107}
108
110{
111 CString strText;
113 CString strProgramName;
114 strProgramName.LoadString(IDS_PROGRAMNAME);
115 mainWindow.MessageBox(strText, strProgramName, MB_OK | MB_ICONEXCLAMATION);
116}
117
119{
120 if (hBitmap == NULL)
121 {
122 COLORREF white = RGB(255, 255, 255);
125 if (hBitmap == NULL)
126 return FALSE;
127
128 HDC hScreenDC = GetDC(NULL);
129 g_xDpi = GetDeviceCaps(hScreenDC, LOGPIXELSX);
130 g_yDpi = GetDeviceCaps(hScreenDC, LOGPIXELSY);
131 ReleaseDC(NULL, hScreenDC);
132
133 ZeroMemory(&fileTime, sizeof(fileTime));
134 }
135
136 // update image
139
140 // update fileSize
142
143 // update filepathname
144 if (name && name[0])
146 else
148
149 // set title
150 CString strTitle;
152 mainWindow.SetWindowText(strTitle);
153
154 // update file info and recent
155 isAFile = isFile;
156 if (isAFile)
158
160
161 return hBitmap;
162}
163
165{
166 // find the file
168 HANDLE hFind = FindFirstFile(name, &find);
169 if (hFind == INVALID_HANDLE_VALUE)
170 {
171 // does not exist
172 CStringW strText;
173 strText.Format(IDS_LOADERRORTEXT, name);
174 MessageBoxW(hwnd, strText, NULL, MB_ICONERROR);
175 return NULL;
176 }
177 DWORD dwFileSize = find.nFileSizeLow; // get file size
178 FindClose(hFind);
179
180 // is file empty?
181 if (dwFileSize == 0)
182 {
183 if (fIsMainFile)
184 {
185 FILETIME ft;
186 FileTimeToLocalFileTime(&find.ftLastWriteTime, &ft);
189 }
190 }
191
192 // load the image
194 img.LoadDx(name, &g_xDpi, &g_yDpi);
195
196 if (g_xDpi <= 0)
197 g_xDpi = 96;
198 if (g_yDpi <= 0)
199 g_yDpi = 96;
200
201 HBITMAP hBitmap = img.Detach();
202
203 if (hBitmap == NULL)
204 {
205 // cannot open
206 CStringW strText;
207 strText.Format(IDS_LOADERRORTEXT, name);
208 MessageBoxW(hwnd, strText, NULL, MB_ICONERROR);
209 return NULL;
210 }
211
212 if (fIsMainFile)
213 {
214 FILETIME ft;
215 FileTimeToLocalFileTime(&find.ftLastWriteTime, &ft);
218 }
219
220 return hBitmap;
221}
222
224{
226 if (!hbm2)
227 return NULL;
228
230 HGDIOBJ hbm2Old = SelectObject(hDC2, hbm2);
231 if (bRight)
232 {
233 for (INT y = 0; y < cy; ++y)
234 {
235 for (INT x = 0; x < cx; ++x)
236 {
237 COLORREF rgb = GetPixel(hDC1, x, y);
238 SetPixelV(hDC2, cy - (y + 1), x, rgb);
239 }
240 }
241 }
242 else
243 {
244 for (INT y = 0; y < cy; ++y)
245 {
246 for (INT x = 0; x < cx; ++x)
247 {
248 COLORREF rgb = GetPixel(hDC1, x, y);
249 SetPixelV(hDC2, y, cx - (x + 1), rgb);
250 }
251 }
252 }
253 SelectObject(hDC2, hbm2Old);
254 DeleteDC(hDC2);
255 return hbm2;
256}
257
258#ifndef M_PI
259 #define M_PI 3.14159265
260#endif
261
262HBITMAP SkewDIB(HDC hDC1, HBITMAP hbm, INT nDegree, BOOL bVertical)
263{
264 if (nDegree == 0)
265 return CopyDIBImage(hbm);
266
267 const double eTan = tan(abs(nDegree) * M_PI / 180);
268
269 BITMAP bm;
270 GetObjectW(hbm, sizeof(bm), &bm);
271 INT cx = bm.bmWidth, cy = bm.bmHeight, dx = 0, dy = 0;
272 if (bVertical)
273 dy = INT(cx * eTan);
274 else
275 dx = INT(cy * eTan);
276
277 if (dx == 0 && dy == 0)
278 return CopyDIBImage(hbm);
279
280 HBITMAP hbmNew = CreateColorDIB(cx + dx, cy + dy, RGB(255, 255, 255));
281 if (!hbmNew)
282 return NULL;
283
285 HGDIOBJ hbm2Old = SelectObject(hDC2, hbmNew);
286 if (bVertical)
287 {
288 for (INT x = 0; x < cx; ++x)
289 {
290 INT delta = INT(x * eTan);
291 if (nDegree > 0)
292 BitBlt(hDC2, x, (dy - delta), 1, cy, hDC1, x, 0, SRCCOPY);
293 else
294 BitBlt(hDC2, x, delta, 1, cy, hDC1, x, 0, SRCCOPY);
295 }
296 }
297 else
298 {
299 for (INT y = 0; y < cy; ++y)
300 {
301 INT delta = INT(y * eTan);
302 if (nDegree > 0)
303 BitBlt(hDC2, (dx - delta), y, cx, 1, hDC1, 0, y, SRCCOPY);
304 else
305 BitBlt(hDC2, delta, y, cx, 1, hDC1, 0, y, SRCCOPY);
306 }
307 }
308 SelectObject(hDC2, hbm2Old);
309 DeleteDC(hDC2);
310 return hbmNew;
311}
static HDC hDC
Definition: 3dtext.c:33
_STLP_DECLSPEC complex< float > _STLP_CALL tan(const complex< float > &)
static HBITMAP CopyDIBImage(HBITMAP hbm, INT cx=0, INT cy=0)
Definition: dib.h:14
#define IDS_DEFAULTFILENAME
Definition: resource.h:186
#define IDS_LOADERRORTEXT
Definition: resource.h:215
#define IDS_PROGRAMNAME
Definition: resource.h:180
#define IDS_WINDOWTITLE
Definition: resource.h:181
DWORD GetPixel(LPDIRECTDRAWSURFACE7 Surface, UINT x, UINT y)
Definition: blt.cpp:2
BOOL LoadString(_In_ UINT nID)
Definition: cstringt.h:591
void __cdecl Format(UINT nFormatID,...)
Definition: cstringt.h:753
int MessageBox(LPCTSTR lpszText, LPCTSTR lpszCaption=NULL, UINT nType=MB_OK)
Definition: atlwin.h:996
BOOL SetWindowText(LPCTSTR lpszString)
Definition: atlwin.h:1294
void Insert(HBITMAP hbm)
Definition: history.cpp:115
void ClearHistory(void)
Definition: history.cpp:109
DWORD BMPWidth
Definition: registry.h:20
void SetMostRecentFile(LPCTSTR szPathName)
Definition: registry.cpp:229
DWORD BMPHeight
Definition: registry.h:19
static TAGID TAGID find
Definition: db.cpp:155
HBITMAP CreateDIBWithProperties(int width, int height)
Definition: dib.cpp:26
HBITMAP SetBitmapAndInfo(HBITMAP hBitmap, LPCTSTR name, DWORD dwFileSize, BOOL isFile)
Definition: dib.cpp:118
HBITMAP Rotate90DegreeBlt(HDC hDC1, INT cx, INT cy, BOOL bRight)
Definition: dib.cpp:223
float g_xDpi
Definition: dib.cpp:13
HBITMAP SkewDIB(HDC hDC1, HBITMAP hbm, INT nDegree, BOOL bVertical)
Definition: dib.cpp:262
BOOL SaveDIBToFile(HBITMAP hBitmap, LPTSTR FileName, HDC hDC)
Definition: dib.cpp:78
float g_yDpi
Definition: dib.cpp:14
int GetDIBHeight(HBITMAP hBitmap)
Definition: dib.cpp:71
float PpmFromDpi(float dpi)
Definition: dib.cpp:20
HBITMAP DoLoadImageFile(HWND hwnd, LPCTSTR name, BOOL fIsMainFile)
Definition: dib.cpp:164
INT fileSize
Definition: dib.cpp:12
SYSTEMTIME fileTime
Definition: dib.cpp:15
#define M_PI
Definition: dib.cpp:259
void ShowFileLoadError(LPCTSTR name)
Definition: dib.cpp:109
HBITMAP CreateColorDIB(int width, int height, COLORREF rgb)
Definition: dib.cpp:40
int GetDIBWidth(HBITMAP hBitmap)
Definition: dib.cpp:63
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
static HBITMAP hBitmap
Definition: timezone.c:26
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
BOOL WINAPI FindClose(HANDLE hFindFile)
Definition: find.c:502
BOOL WINAPI FileTimeToSystemTime(IN CONST FILETIME *lpFileTime, OUT LPSYSTEMTIME lpSystemTime)
Definition: time.c:188
BOOL WINAPI FileTimeToLocalFileTime(IN CONST FILETIME *lpFileTime, OUT LPFILETIME lpLocalFileTime)
Definition: time.c:221
static VOID NTAPI BitBlt(_In_ ULONG Left, _In_ ULONG Top, _In_ ULONG Width, _In_ ULONG Height, _In_reads_bytes_(Delta *Height) PUCHAR Buffer, _In_ ULONG BitsPerPixel, _In_ ULONG Delta)
Definition: common.c:49
#define BI_RGB
Definition: precomp.h:47
#define RGB(r, g, b)
Definition: precomp.h:62
#define abs(i)
Definition: fconv.c:206
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
pKey DeleteObject()
GLint GLvoid * img
Definition: gl.h:1956
GLint GLint GLint GLint GLint x
Definition: gl.h:1548
GLint GLint GLint GLint GLint GLint y
Definition: gl.h:1548
GLint GLint GLsizei GLsizei height
Definition: gl.h:1546
GLint GLint GLsizei width
Definition: gl.h:1546
BOOL imageSaved
Definition: main.cpp:18
ImageModel imageModel
Definition: history.cpp:11
RegistrySettings registrySettings
Definition: registry.cpp:15
HINSTANCE hProgInstance
Definition: main.cpp:15
CMainWindow mainWindow
Definition: main.cpp:21
TCHAR filepathname[MAX_LONG_PATH]
Definition: main.cpp:16
BOOL isAFile
Definition: main.cpp:17
#define GUID_NULL
Definition: ks.h:106
GLint dy
Definition: linetemp.h:97
GLint dx
Definition: linetemp.h:97
HDC hdc
Definition: main.c:9
static HBITMAP
Definition: button.c:44
static HDC
Definition: imagelist.c:92
DWORD dwFileSize
Definition: more.c:40
_In_ HBITMAP hbm
Definition: ntgdi.h:2776
#define INT
Definition: polytest.cpp:20
_Out_opt_ int _Out_opt_ int * cy
Definition: commctrl.h:586
_Out_opt_ int * cx
Definition: commctrl.h:585
#define PathFindFileName
Definition: shlwapi.h:911
#define _countof(array)
Definition: sndvol32.h:68
Definition: bl.h:1331
Definition: name.c:39
USHORT biBitCount
Definition: precomp.h:37
ULONG biCompression
Definition: precomp.h:38
BITMAPINFOHEADER bmiHeader
Definition: wingdi.h:1476
int32_t INT
Definition: typedefs.h:58
int ret
HBITMAP WINAPI CreateDIBSection(HDC hDC, CONST BITMAPINFO *BitmapInfo, UINT Usage, VOID **Bits, HANDLE hSection, DWORD dwOffset)
Definition: bitmap.c:199
#define dpi
Definition: sysparams.c:23
#define ZeroMemory
Definition: winbase.h:1670
#define FindFirstFile
Definition: winbase.h:3653
#define GetFullPathName
Definition: winbase.h:3692
_In_ ULONG _In_ ULONG rgb
Definition: winddi.h:3521
_In_ LONG _In_ HWND hwnd
Definition: winddi.h:4023
DWORD COLORREF
Definition: windef.h:300
#define DIB_RGB_COLORS
Definition: wingdi.h:367
int WINAPI GetObjectW(_In_ HANDLE h, _In_ int c, _Out_writes_bytes_opt_(c) LPVOID pv)
int WINAPI GetDeviceCaps(_In_opt_ HDC, _In_ int)
#define LOGPIXELSY
Definition: wingdi.h:719
HGDIOBJ WINAPI SelectObject(_In_ HDC, _In_ HGDIOBJ)
Definition: dc.c:1539
HDC WINAPI CreateCompatibleDC(_In_opt_ HDC hdc)
#define SRCCOPY
Definition: wingdi.h:333
BOOL WINAPI SetPixelV(_In_ HDC, _In_ int, _In_ int, _In_ COLORREF)
#define LOGPIXELSX
Definition: wingdi.h:718
int WINAPI FillRect(HDC, LPCRECT, HBRUSH)
#define GetObject
Definition: wingdi.h:4468
HBRUSH WINAPI CreateSolidBrush(_In_ COLORREF)
BOOL WINAPI DeleteDC(_In_ HDC)
int WINAPI ReleaseDC(_In_opt_ HWND, _In_ HDC)
int WINAPI MessageBoxW(_In_opt_ HWND hWnd, _In_opt_ LPCWSTR lpText, _In_opt_ LPCWSTR lpCaption, _In_ UINT uType)
#define MB_ICONERROR
Definition: winuser.h:781
HDC WINAPI GetDC(_In_opt_ HWND)
#define MB_ICONEXCLAMATION
Definition: winuser.h:779
#define MB_OK
Definition: winuser.h:784
#define LoadString
Definition: winuser.h:5809
BOOL WINAPI SetRect(_Out_ LPRECT, _In_ int, _In_ int, _In_ int, _In_ int)
const CHAR * LPCTSTR
Definition: xmlstorage.h:193
CHAR * LPTSTR
Definition: xmlstorage.h:192