ReactOS  0.4.15-dev-1152-g6c94e4f
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 
13 /* FUNCTIONS ********************************************************/
14 
15 HBITMAP
17 {
18  BITMAPINFO bmi;
19  ZeroMemory(&bmi, sizeof(BITMAPINFO));
20  bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
21  bmi.bmiHeader.biWidth = width;
23  bmi.bmiHeader.biPlanes = 1;
24  bmi.bmiHeader.biBitCount = 24;
26  return CreateDIBSection(NULL, &bmi, DIB_RGB_COLORS, NULL, NULL, 0);
27 }
28 
29 HBITMAP
31 {
33  if (!ret)
34  return NULL;
35 
36  if (rgb)
37  {
39  HGDIOBJ hbmOld = SelectObject(hdc, ret);
40  RECT rc;
41  SetRect(&rc, 0, 0, width, height);
42  HBRUSH hbr = CreateSolidBrush(rgb);
43  FillRect(hdc, &rc, hbr);
44  DeleteObject(hbr);
45  SelectObject(hdc, hbmOld);
46  DeleteDC(hdc);
47  }
48 
49  return ret;
50 }
51 
52 int
54 {
55  BITMAP bm;
56  GetObject(hBitmap, sizeof(BITMAP), &bm);
57  return bm.bmWidth;
58 }
59 
60 int
62 {
63  BITMAP bm;
64  GetObject(hBitmap, sizeof(BITMAP), &bm);
65  return bm.bmHeight;
66 }
67 
69 {
70  CImage img;
71  img.Attach(hBitmap);
72  img.Save(FileName); // TODO: error handling
73  img.Detach();
74 
76  HANDLE hFind = FindFirstFile(FileName, &find);
77  if (hFind == INVALID_HANDLE_VALUE)
78  {
80  return FALSE;
81  }
82  FindClose(hFind);
83 
84  // update time and size
85  FILETIME ft;
86  FileTimeToLocalFileTime(&find.ftLastWriteTime, &ft);
88  fileSize = find.nFileSizeLow;
89 
90  // TODO: update hRes and vRes
91 
93 
94  isAFile = TRUE;
95  imageSaved = TRUE;
96  return TRUE;
97 }
98 
100 {
101  CString strText;
102  strText.Format(IDS_LOADERRORTEXT, (LPCTSTR) name);
103  CString strProgramName;
104  strProgramName.LoadString(IDS_PROGRAMNAME);
105  mainWindow.MessageBox(strText, strProgramName, MB_OK | MB_ICONEXCLAMATION);
106 }
107 
109 {
110  if (hBitmap == NULL)
111  {
112  COLORREF white = RGB(255, 255, 255);
114  registrySettings.BMPHeight, white);
115  if (hBitmap == NULL)
116  return FALSE;
117 
118  fileHPPM = fileVPPM = 2834;
119  ZeroMemory(&fileTime, sizeof(fileTime));
120  isFile = FALSE;
121  }
122  else
123  {
124  // update PPMs
125  HDC hScreenDC = GetDC(NULL);
126  fileHPPM = (int)(GetDeviceCaps(hScreenDC, LOGPIXELSX) * 1000 / 25.4);
127  fileVPPM = (int)(GetDeviceCaps(hScreenDC, LOGPIXELSY) * 1000 / 25.4);
128  ReleaseDC(NULL, hScreenDC);
129  }
130 
131  // update image
134 
135  // update fileSize
137 
138  // update filepathname
139  if (name && name[0])
141  else
143 
144  // set title
145  CString strTitle;
147  mainWindow.SetWindowText(strTitle);
148 
149  // update file info and recent
150  isAFile = isFile;
151  if (isAFile)
153 
154  imageSaved = TRUE;
155 
156  return hBitmap;
157 }
158 
160 {
161  // find the file
163  HANDLE hFind = FindFirstFile(name, &find);
164  if (hFind == INVALID_HANDLE_VALUE)
165  {
166  // does not exist
167  CStringW strText;
168  strText.Format(IDS_LOADERRORTEXT, name);
169  MessageBoxW(hwnd, strText, NULL, MB_ICONERROR);
170  return NULL;
171  }
172  DWORD dwFileSize = find.nFileSizeLow; // get file size
173  FindClose(hFind);
174 
175  // is file empty?
176  if (dwFileSize == 0)
177  {
178  if (fIsMainFile)
179  {
180  FILETIME ft;
181  FileTimeToLocalFileTime(&find.ftLastWriteTime, &ft);
184  }
185  }
186 
187  // load the image
188  CImage img;
189  img.Load(name);
190  HBITMAP hBitmap = img.Detach();
191 
192  if (hBitmap == NULL)
193  {
194  // cannot open
195  CStringW strText;
196  strText.Format(IDS_LOADERRORTEXT, name);
197  MessageBoxW(hwnd, strText, NULL, MB_ICONERROR);
198  return NULL;
199  }
200 
201  if (fIsMainFile)
202  {
203  FILETIME ft;
204  FileTimeToLocalFileTime(&find.ftLastWriteTime, &ft);
207  }
208 
209  return hBitmap;
210 }
BOOL isAFile
Definition: main.cpp:53
HBITMAP SetBitmapAndInfo(HBITMAP hBitmap, LPCTSTR name, DWORD dwFileSize, BOOL isFile)
Definition: dib.cpp:108
const DOCKBAR PVOID HWND HWND * hwnd
Definition: tooldock.h:22
GLint GLint GLsizei width
Definition: gl.h:1546
#define LOGPIXELSX
Definition: wingdi.h:717
const CHAR * LPCTSTR
Definition: xmlstorage.h:193
int fileSize
Definition: main.cpp:55
BITMAPINFOHEADER bmiHeader
Definition: wingdi.h:1475
#define RGB(r, g, b)
Definition: precomp.h:62
HDC WINAPI GetDC(_In_opt_ HWND)
#define TRUE
Definition: types.h:120
struct tagBITMAPINFOHEADER BITMAPINFOHEADER
#define IDS_PROGRAMNAME
Definition: definitions.h:203
DWORD dwFileSize
Definition: more.c:37
static HDC
Definition: imagelist.c:92
HGDIOBJ WINAPI SelectObject(_In_ HDC, _In_ HGDIOBJ)
Definition: dc.c:1499
#define INVALID_HANDLE_VALUE
Definition: compat.h:479
#define ZeroMemory
Definition: winbase.h:1648
BOOL WINAPI DeleteObject(_In_ HGDIOBJ)
void __cdecl Format(UINT nFormatID,...)
Definition: cstringt.h:716
HDC WINAPI CreateCompatibleDC(_In_opt_ HDC hdc)
int GetDIBHeight(HBITMAP hBitmap)
Definition: dib.cpp:61
BOOL SetWindowText(LPCTSTR lpszString)
Definition: atlwin.h:1232
BOOL WINAPI FileTimeToLocalFileTime(IN CONST FILETIME *lpFileTime, OUT LPFILETIME lpLocalFileTime)
Definition: time.c:221
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:146
int GetDIBWidth(HBITMAP hBitmap)
Definition: dib.cpp:53
SYSTEMTIME fileTime
Definition: main.cpp:58
#define SIZEOF(_ar)
Definition: calc.h:97
#define FALSE
Definition: types.h:117
void ClearHistory(void)
Definition: history.cpp:102
unsigned int BOOL
Definition: ntddk_ex.h:94
#define IDS_LOADERRORTEXT
Definition: definitions.h:238
int WINAPI ReleaseDC(_In_opt_ HWND, _In_ HDC)
DWORD biCompression
Definition: amvideo.idl:35
smooth NULL
Definition: ftsmooth.c:416
HBITMAP CreateDIBWithProperties(int width, int height)
Definition: dib.cpp:16
int fileHPPM
Definition: main.cpp:56
#define FindFirstFile
Definition: winbase.h:3622
void Insert(HBITMAP hbm)
Definition: history.cpp:108
DWORD BMPWidth
Definition: registry.h:18
HBITMAP CreateColorDIB(int width, int height, COLORREF rgb)
Definition: dib.cpp:30
BOOL WINAPI FileTimeToSystemTime(IN CONST FILETIME *lpFileTime, OUT LPSYSTEMTIME lpSystemTime)
Definition: time.c:188
int WINAPI MessageBoxW(_In_opt_ HWND, _In_opt_ LPCWSTR, _In_opt_ LPCWSTR, _In_ UINT)
GLint GLvoid * img
Definition: gl.h:1956
DWORD COLORREF
Definition: windef.h:300
#define PathFindFileName
Definition: shlwapi.h:911
unsigned long DWORD
Definition: ntddk_ex.h:95
#define GetObject
Definition: wingdi.h:4467
TCHAR filepathname[1000]
Definition: main.cpp:52
int ret
BOOL imageSaved
Definition: main.cpp:54
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:3661
HINSTANCE hProgInstance
Definition: main.cpp:50
BOOL SaveDIBToFile(HBITMAP hBitmap, LPTSTR FileName, HDC hDC)
Definition: dib.cpp:68
GLint GLint GLsizei GLsizei height
Definition: gl.h:1546
static HDC hDC
Definition: 3dtext.c:33
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:197
Definition: bl.h:1331
HBITMAP DoLoadImageFile(HWND hwnd, LPCTSTR name, BOOL fIsMainFile)
Definition: dib.cpp:159
HBRUSH WINAPI CreateSolidBrush(_In_ COLORREF)
#define MB_OK
Definition: winuser.h:784
void ShowFileLoadError(LPCTSTR name)
Definition: dib.cpp:99
static TAGID TAGID find
Definition: db.cpp:153
Definition: name.c:38
BOOL LoadString(_In_ UINT nID)
Definition: cstringt.h:554
#define LOGPIXELSY
Definition: wingdi.h:718
static HBITMAP
Definition: button.c:44
RegistrySettings registrySettings
Definition: main.cpp:30
#define DIB_RGB_COLORS
Definition: wingdi.h:366
int WINAPI FillRect(HDC, LPCRECT, HBRUSH)
int MessageBox(LPCTSTR lpszText, LPCTSTR lpszCaption=NULL, UINT nType=MB_OK)
Definition: atlwin.h:934
#define IDS_WINDOWTITLE
Definition: definitions.h:204
#define BI_RGB
Definition: precomp.h:47
static HBITMAP hBitmap
Definition: timezone.c:26
#define LoadString
Definition: winuser.h:5794
int fileVPPM
Definition: main.cpp:57
#define IDS_DEFAULTFILENAME
Definition: definitions.h:209
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