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