ReactOS  0.4.15-dev-5130-gc1c1279
dib.cpp File Reference
#include "precomp.h"
#include <math.h>
Include dependency graph for dib.cpp:

Go to the source code of this file.

Macros

#define M_PI   3.14159265
 

Functions

HBITMAP CreateDIBWithProperties (int width, int height)
 
HBITMAP CreateColorDIB (int width, int height, COLORREF rgb)
 
int GetDIBWidth (HBITMAP hBitmap)
 
int GetDIBHeight (HBITMAP hBitmap)
 
BOOL SaveDIBToFile (HBITMAP hBitmap, LPTSTR FileName, HDC hDC)
 
void ShowFileLoadError (LPCTSTR name)
 
HBITMAP SetBitmapAndInfo (HBITMAP hBitmap, LPCTSTR name, DWORD dwFileSize, BOOL isFile)
 
HBITMAP DoLoadImageFile (HWND hwnd, LPCTSTR name, BOOL fIsMainFile)
 
HBITMAP Rotate90DegreeBlt (HDC hDC1, INT cx, INT cy, BOOL bRight)
 
HBITMAP SkewDIB (HDC hDC1, HBITMAP hbm, INT nDegree, BOOL bVertical)
 

Macro Definition Documentation

◆ M_PI

#define M_PI   3.14159265

Definition at line 249 of file dib.cpp.

Function Documentation

◆ CreateColorDIB()

HBITMAP CreateColorDIB ( int  width,
int  height,
COLORREF  rgb 
)

Definition at line 31 of file dib.cpp.

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 }
GLint GLint GLsizei width
Definition: gl.h:1546
static HDC
Definition: imagelist.c:92
HGDIOBJ WINAPI SelectObject(_In_ HDC, _In_ HGDIOBJ)
Definition: dc.c:1539
BOOL WINAPI DeleteObject(_In_ HGDIOBJ)
HDC WINAPI CreateCompatibleDC(_In_opt_ HDC hdc)
HBITMAP CreateDIBWithProperties(int width, int height)
Definition: dib.cpp:17
int ret
HDC hdc
Definition: main.c:9
GLint GLint GLsizei GLsizei height
Definition: gl.h:1546
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)
#define NULL
Definition: types.h:112
HBRUSH WINAPI CreateSolidBrush(_In_ COLORREF)
static HBITMAP
Definition: button.c:44
int WINAPI FillRect(HDC, LPCRECT, HBRUSH)

Referenced by SetBitmapAndInfo(), and SkewDIB().

◆ CreateDIBWithProperties()

HBITMAP CreateDIBWithProperties ( int  width,
int  height 
)

Definition at line 17 of file dib.cpp.

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 }
GLint GLint GLsizei width
Definition: gl.h:1546
BITMAPINFOHEADER bmiHeader
Definition: wingdi.h:1476
struct tagBITMAPINFOHEADER BITMAPINFOHEADER
#define ZeroMemory
Definition: winbase.h:1667
DWORD biCompression
Definition: amvideo.idl:35
GLint GLint GLsizei GLsizei height
Definition: gl.h:1546
HBITMAP WINAPI CreateDIBSection(HDC hDC, CONST BITMAPINFO *BitmapInfo, UINT Usage, VOID **Bits, HANDLE hSection, DWORD dwOffset)
Definition: bitmap.c:199
#define NULL
Definition: types.h:112
#define DIB_RGB_COLORS
Definition: wingdi.h:367
#define BI_RGB
Definition: precomp.h:47

Referenced by SelectionModel::CalculateBoundingBoxAndContents(), SelectionModel::CalculateContents(), CreateColorDIB(), ImageModel::Crop(), ImageModel::ImageModel(), Rotate90DegreeBlt(), and SelectionModel::ScaleContentsToFit().

◆ DoLoadImageFile()

HBITMAP DoLoadImageFile ( HWND  hwnd,
LPCTSTR  name,
BOOL  fIsMainFile 
)

Definition at line 160 of file dib.cpp.

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 }
HBITMAP SetBitmapAndInfo(HBITMAP hBitmap, LPCTSTR name, DWORD dwFileSize, BOOL isFile)
Definition: dib.cpp:109
#define TRUE
Definition: types.h:120
DWORD dwFileSize
Definition: more.c:40
#define INVALID_HANDLE_VALUE
Definition: compat.h:590
void __cdecl Format(UINT nFormatID,...)
Definition: cstringt.h:753
int WINAPI MessageBoxW(_In_opt_ HWND hWnd, _In_opt_ LPCWSTR lpText, _In_opt_ LPCWSTR lpCaption, _In_ UINT uType)
BOOL WINAPI FileTimeToLocalFileTime(IN CONST FILETIME *lpFileTime, OUT LPFILETIME lpLocalFileTime)
Definition: time.c:221
#define IDS_LOADERRORTEXT
Definition: resource.h:214
SYSTEMTIME fileTime
Definition: main.cpp:58
#define FindFirstFile
Definition: winbase.h:3643
BOOL WINAPI FileTimeToSystemTime(IN CONST FILETIME *lpFileTime, OUT LPSYSTEMTIME lpSystemTime)
Definition: time.c:188
GLint GLvoid * img
Definition: gl.h:1956
unsigned long DWORD
Definition: ntddk_ex.h:95
#define MB_ICONERROR
Definition: winuser.h:781
#define NULL
Definition: types.h:112
static TAGID TAGID find
Definition: db.cpp:155
Definition: name.c:38
static HBITMAP
Definition: button.c:44
_In_ LONG _In_ HWND hwnd
Definition: winddi.h:4022
static HBITMAP hBitmap
Definition: timezone.c:26
BOOL WINAPI FindClose(HANDLE hFindFile)
Definition: find.c:502

Referenced by _tWinMain(), CMainWindow::OnCommand(), and CMainWindow::OnDropFiles().

◆ GetDIBHeight()

int GetDIBHeight ( HBITMAP  hBitmap)

Definition at line 62 of file dib.cpp.

63 {
64  BITMAP bm;
65  GetObject(hBitmap, sizeof(BITMAP), &bm);
66  return bm.bmHeight;
67 }
#define GetObject
Definition: wingdi.h:4468
Definition: bl.h:1330
static HBITMAP hBitmap
Definition: timezone.c:26

Referenced by SelectionModel::DrawSelectionStretched(), ImageModel::GetHeight(), SelectionModel::InsertFromHBITMAP(), CMainWindow::InsertSelectionFromHBITMAP(), and SelectionModel::ScaleContentsToFit().

◆ GetDIBWidth()

int GetDIBWidth ( HBITMAP  hBitmap)

Definition at line 54 of file dib.cpp.

55 {
56  BITMAP bm;
57  GetObject(hBitmap, sizeof(BITMAP), &bm);
58  return bm.bmWidth;
59 }
#define GetObject
Definition: wingdi.h:4468
Definition: bl.h:1330
static HBITMAP hBitmap
Definition: timezone.c:26

Referenced by SelectionModel::DrawSelectionStretched(), ImageModel::GetWidth(), SelectionModel::InsertFromHBITMAP(), CMainWindow::InsertSelectionFromHBITMAP(), and SelectionModel::ScaleContentsToFit().

◆ Rotate90DegreeBlt()

HBITMAP Rotate90DegreeBlt ( HDC  hDC1,
INT  cx,
INT  cy,
BOOL  bRight 
)

Definition at line 213 of file dib.cpp.

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 }
static HDC
Definition: imagelist.c:92
HGDIOBJ WINAPI SelectObject(_In_ HDC, _In_ HGDIOBJ)
Definition: dc.c:1539
GLint GLint GLint GLint GLint x
Definition: gl.h:1548
HDC WINAPI CreateCompatibleDC(_In_opt_ HDC hdc)
DWORD GetPixel(LPDIRECTDRAWSURFACE7 Surface, UINT x, UINT y)
Definition: blt.cpp:2
int32_t INT
Definition: typedefs.h:58
_Out_opt_ int _Out_opt_ int * cy
Definition: commctrl.h:585
HBITMAP CreateDIBWithProperties(int width, int height)
Definition: dib.cpp:17
DWORD COLORREF
Definition: windef.h:300
_In_ ULONG _In_ ULONG rgb
Definition: winddi.h:3520
BOOL WINAPI DeleteDC(_In_ HDC)
#define NULL
Definition: types.h:112
GLint GLint GLint GLint GLint GLint y
Definition: gl.h:1548
_Out_opt_ int * cx
Definition: commctrl.h:585
static HBITMAP
Definition: button.c:44
BOOL WINAPI SetPixelV(_In_ HDC, _In_ int, _In_ int, _In_ COLORREF)

Referenced by ImageModel::RotateNTimes90Degrees(), and SelectionModel::RotateNTimes90Degrees().

◆ SaveDIBToFile()

BOOL SaveDIBToFile ( HBITMAP  hBitmap,
LPTSTR  FileName,
HDC  hDC 
)

Definition at line 69 of file dib.cpp.

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 }
BOOL isAFile
Definition: main.cpp:53
int fileSize
Definition: main.cpp:55
#define TRUE
Definition: types.h:120
#define INVALID_HANDLE_VALUE
Definition: compat.h:590
BOOL WINAPI FileTimeToLocalFileTime(IN CONST FILETIME *lpFileTime, OUT LPFILETIME lpLocalFileTime)
Definition: time.c:221
void SetMostRecentFile(LPCTSTR szPathName)
Definition: registry.cpp:187
SYSTEMTIME fileTime
Definition: main.cpp:58
#define FALSE
Definition: types.h:117
#define FindFirstFile
Definition: winbase.h:3643
BOOL WINAPI FileTimeToSystemTime(IN CONST FILETIME *lpFileTime, OUT LPSYSTEMTIME lpSystemTime)
Definition: time.c:188
GLint GLvoid * img
Definition: gl.h:1956
BOOL imageSaved
Definition: main.cpp:54
void ShowFileLoadError(LPCTSTR name)
Definition: dib.cpp:100
static TAGID TAGID find
Definition: db.cpp:155
RegistrySettings registrySettings
Definition: main.cpp:30
static HBITMAP hBitmap
Definition: timezone.c:26
BOOL WINAPI FindClose(HANDLE hFindFile)
Definition: find.c:502

Referenced by CMainWindow::OnCommand(), and ImageModel::SaveImage().

◆ SetBitmapAndInfo()

HBITMAP SetBitmapAndInfo ( HBITMAP  hBitmap,
LPCTSTR  name,
DWORD  dwFileSize,
BOOL  isFile 
)

Definition at line 109 of file dib.cpp.

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 }
BOOL isAFile
Definition: main.cpp:53
#define LOGPIXELSX
Definition: wingdi.h:718
int fileSize
Definition: main.cpp:55
#define RGB(r, g, b)
Definition: precomp.h:62
HDC WINAPI GetDC(_In_opt_ HWND)
#define TRUE
Definition: types.h:120
DWORD dwFileSize
Definition: more.c:40
static HDC
Definition: imagelist.c:92
#define ZeroMemory
Definition: winbase.h:1667
void __cdecl Format(UINT nFormatID,...)
Definition: cstringt.h:753
BOOL SetWindowText(LPCTSTR lpszString)
Definition: atlwin.h:1294
#define IDS_DEFAULTFILENAME
Definition: resource.h:185
DWORD BMPHeight
Definition: registry.h:17
void SetMostRecentFile(LPCTSTR szPathName)
Definition: registry.cpp:187
SYSTEMTIME fileTime
Definition: main.cpp:58
#define FALSE
Definition: types.h:117
void ClearHistory(void)
Definition: history.cpp:109
int WINAPI ReleaseDC(_In_opt_ HWND, _In_ HDC)
int fileHPPM
Definition: main.cpp:56
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
DWORD COLORREF
Definition: windef.h:300
#define _countof(array)
Definition: sndvol32.h:68
#define PathFindFileName
Definition: shlwapi.h:911
#define IDS_WINDOWTITLE
Definition: resource.h:180
TCHAR filepathname[1000]
Definition: main.cpp:52
BOOL imageSaved
Definition: main.cpp:54
int WINAPI GetDeviceCaps(_In_opt_ HDC, _In_ int)
ImageModel imageModel
Definition: main.cpp:32
#define GetFullPathName
Definition: winbase.h:3682
HINSTANCE hProgInstance
Definition: main.cpp:50
#define NULL
Definition: types.h:112
Definition: name.c:38
#define LOGPIXELSY
Definition: wingdi.h:719
RegistrySettings registrySettings
Definition: main.cpp:30
static HBITMAP hBitmap
Definition: timezone.c:26
#define LoadString
Definition: winuser.h:5809
int fileVPPM
Definition: main.cpp:57
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:31
CMainWindow mainWindow
Definition: main.cpp:63

Referenced by DoLoadImageFile(), and CMainWindow::OnCommand().

◆ ShowFileLoadError()

void ShowFileLoadError ( LPCTSTR  name)

Definition at line 100 of file dib.cpp.

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 }
const CHAR * LPCTSTR
Definition: xmlstorage.h:193
void __cdecl Format(UINT nFormatID,...)
Definition: cstringt.h:753
#define MB_ICONEXCLAMATION
Definition: winuser.h:779
#define IDS_LOADERRORTEXT
Definition: resource.h:214
#define IDS_PROGRAMNAME
Definition: resource.h:179
#define MB_OK
Definition: winuser.h:784
Definition: name.c:38
BOOL LoadString(_In_ UINT nID)
Definition: cstringt.h:591
int MessageBox(LPCTSTR lpszText, LPCTSTR lpszCaption=NULL, UINT nType=MB_OK)
Definition: atlwin.h:996
CMainWindow mainWindow
Definition: main.cpp:63

Referenced by SaveDIBToFile().

◆ SkewDIB()

HBITMAP SkewDIB ( HDC  hDC1,
HBITMAP  hbm,
INT  nDegree,
BOOL  bVertical 
)

Definition at line 252 of file dib.cpp.

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 }
#define abs(i)
Definition: fconv.c:206
#define RGB(r, g, b)
Definition: precomp.h:62
static HBITMAP CopyDIBImage(HBITMAP hbm, INT cx=0, INT cy=0)
Definition: dib.h:14
GLint dy
Definition: linetemp.h:97
#define INT
Definition: polytest.cpp:20
static HDC
Definition: imagelist.c:92
HGDIOBJ WINAPI SelectObject(_In_ HDC, _In_ HGDIOBJ)
Definition: dc.c:1539
GLint GLint GLint GLint GLint x
Definition: gl.h:1548
HDC WINAPI CreateCompatibleDC(_In_opt_ HDC hdc)
int32_t INT
Definition: typedefs.h:58
_Out_opt_ int _Out_opt_ int * cy
Definition: commctrl.h:585
HBITMAP CreateColorDIB(int width, int height, COLORREF rgb)
Definition: dib.cpp:31
#define M_PI
Definition: dib.cpp:249
int WINAPI GetObjectW(_In_ HANDLE h, _In_ int c, _Out_writes_bytes_opt_(c) LPVOID pv)
BOOL WINAPI DeleteDC(_In_ HDC)
_In_ HBITMAP hbm
Definition: ntgdi.h:2776
Definition: bl.h:1330
#define NULL
Definition: types.h:112
GLint GLint GLint GLint GLint GLint y
Definition: gl.h:1548
GLint dx
Definition: linetemp.h:97
_Out_opt_ int * cx
Definition: commctrl.h:585
static HBITMAP
Definition: button.c:44
_STLP_DECLSPEC complex< float > _STLP_CALL tan(const complex< float > &)
#define SRCCOPY
Definition: wingdi.h:333
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

Referenced by ImageModel::StretchSkew(), and SelectionModel::StretchSkew().