-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
365 lines (290 loc) · 7.38 KB
/
Copy pathmain.cpp
File metadata and controls
365 lines (290 loc) · 7.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// This is viv, vmunix's image viewer. This is a program that I made out of spite for my beloved Dell Latitude D610 with Windows XP
// and Microsoft Visual C++ 2008. It was out of spite because freaking all of the image viewers that I already had installed sucked
// and I didn't feel like going online and downloading some closed-source slop. I remembered that there was an image thing in stb
// from nothings. And fast forward to now, this image viewer now has support for all the things that stb does, on top of being
// compatible with Windows XP and also having other things like double buffering and (soon) animated gif loading. -vmunix, 5/24/26
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <commdlg.h>
#include <stdio.h>
#define STB_IMAGE_IMPLEMENTATION
#define STBI_NO_THREAD_LOCALS
#include "stb_image.h"
#pragma comment(lib, "comdlg32.lib")
static HBITMAP gBitmap = NULL;
static int gImageWidth = 0;
static int gImageHeight = 0;
static HBRUSH gBackgroundBrush = NULL;
#define WM_LOAD_CMDLINE (WM_USER + 1)
static BOOL LoadImageFile(HWND hwnd, const char* filename)
{
int width;
int height;
int channels;
unsigned char* data = stbi_load(filename, &width, &height, &channels, 4);
if (!data)
{
MessageBoxA(hwnd,
stbi_failure_reason(),
"Failed to load image",
MB_ICONERROR);
return FALSE;
}
BITMAPINFO bmi;
ZeroMemory(&bmi, sizeof(bmi));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = width;
bmi.bmiHeader.biHeight = -height; // top-down bitmap
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biCompression = BI_RGB;
void* bits = NULL;
HDC hdc = GetDC(hwnd);
HBITMAP bitmap = CreateDIBSection(
hdc,
&bmi,
DIB_RGB_COLORS,
&bits,
NULL,
0);
ReleaseDC(hwnd, hdc);
if (!bitmap)
{
stbi_image_free(data);
MessageBoxA(hwnd,
"CreateDIBSection failed",
"Error",
MB_ICONERROR);
return FALSE;
}
// Convert RGBA -> BGRA
unsigned char* dst = (unsigned char*)bits;
int pixelCount = width * height;
int i;
for (i = 0; i < pixelCount; i++)
{
dst[i * 4 + 0] = data[i * 4 + 2];
dst[i * 4 + 1] = data[i * 4 + 1];
dst[i * 4 + 2] = data[i * 4 + 0];
dst[i * 4 + 3] = data[i * 4 + 3];
}
stbi_image_free(data);
if (gBitmap)
{
DeleteObject(gBitmap);
}
gBitmap = bitmap;
gImageWidth = width;
gImageHeight = height;
InvalidateRect(hwnd, NULL, TRUE);
return TRUE;
}
static void OpenImageDialog(HWND hwnd)
{
char filename[MAX_PATH];
ZeroMemory(filename, sizeof(filename));
OPENFILENAMEA ofn;
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFilter =
"Images\0*.png;*.jpg;*.jpeg;*.bmp;*.tga;*.gif\0"
"All Files\0*.*\0";
ofn.lpstrFile = filename;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
if (GetOpenFileNameA(&ofn))
{
LoadImageFile(hwnd, filename);
}
}
LRESULT CALLBACK WindowProc(HWND hwnd,
UINT msg,
WPARAM wParam,
LPARAM lParam)
{
switch (msg)
{
// Create the window
case WM_CREATE:
{
PostMessage(hwnd, WM_LOAD_CMDLINE, 0, 0);
return 0;
}
// If the window gets resized, then redraw the window
case WM_SIZE:
{
InvalidateRect(hwnd, NULL, TRUE);
UpdateWindow(hwnd);
return 0;
}
// Hopefully help save mine and yours eyes
case WM_ERASEBKGND:
{
return 1;
}
case WM_KEYDOWN:
{
if (wParam == 'L')
{
OpenImageDialog(hwnd);
}
return 0;
}
// This is where the image gets shown on the screen
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
RECT rc;
GetClientRect(hwnd, &rc);
int windowWidth = rc.right - rc.left;
int windowHeight = rc.bottom - rc.top;
// Create Back Buffer
HDC backDC = CreateCompatibleDC(hdc);
HBITMAP backBmp = CreateCompatibleBitmap(hdc, windowWidth, windowHeight);
HBITMAP oldBack = (HBITMAP)SelectObject(backDC, backBmp);
// Draw background onto back buffer
FillRect(backDC, &rc, gBackgroundBrush);
if (gBitmap)
{
HDC memDC = CreateCompatibleDC(hdc);
HBITMAP oldBmp = (HBITMAP)SelectObject(memDC, gBitmap);
float imageAspect = (float)gImageWidth / (float)gImageHeight;
float windowAspect = (float)windowWidth / (float)windowHeight;
int drawWidth, drawHeight;
if (windowAspect > imageAspect)
{
drawHeight = windowHeight;
drawWidth = (int)(windowHeight * imageAspect);
}
else
{
drawWidth = windowWidth;
drawHeight = (int)(windowWidth / imageAspect);
}
int x = (windowWidth - drawWidth) / 2;
int y = (windowHeight - drawHeight) / 2;
// Stretch image onto back buffer
SetStretchBltMode(backDC, HALFTONE);
SetBrushOrgEx(backDC, 0, 0, NULL);
StretchBlt(backDC,
x, y,
drawWidth, drawHeight,
memDC,
0, 0,
gImageWidth, gImageHeight,
SRCCOPY);
SelectObject(memDC, oldBmp);
DeleteDC(memDC);
}
// Flip the completed back buffer to the screen in oneshot niko
BitBlt(hdc, 0, 0, windowWidth, windowHeight, backDC, 0, 0, SRCCOPY);
// Clean up back buffer
SelectObject(backDC, oldBack);
DeleteObject(backBmp);
DeleteDC(backDC);
EndPaint(hwnd, &ps);
return 0;
}
case WM_LOAD_CMDLINE:
{
LPSTR cmd = GetCommandLineA();
if (*cmd == '"')
{
cmd++;
while (*cmd && *cmd != '"') cmd++;
if (*cmd == '"') cmd++;
}
else
{
while (*cmd && *cmd != ' ') cmd++;
}
while (*cmd == ' ') cmd++;
if (*cmd != '\0')
{
if (*cmd == '"')
{
cmd++;
char path[MAX_PATH];
int i = 0;
while (*cmd && *cmd != '"' && i < MAX_PATH - 1)
path[i++] = *cmd++;
path[i] = '\0';
LoadImageFile(hwnd, path);
}
else
{
LoadImageFile(hwnd, cmd);
}
}
else
{
OpenImageDialog(hwnd);
}
return 0;
}
// Demolish the window. Kaboom.
case WM_DESTROY:
{
if (gBitmap)
{
if (gBackgroundBrush)
{
DeleteObject(gBackgroundBrush);
gBackgroundBrush = NULL;
}
DeleteObject(gBitmap);
gBitmap = NULL;
}
PostQuitMessage(0);
return 0;
}
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
WNDCLASSA wc;
ZeroMemory(&wc, sizeof(wc));
gBackgroundBrush =
CreateSolidBrush(RGB(200, 200, 200));
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = "TEST";
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground =
gBackgroundBrush;
RegisterClassA(&wc);
HWND hwnd = CreateWindowA(
"TEST",
"TEST",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
800,
600,
NULL,
NULL,
hInstance,
NULL);
SetWindowTextA(hwnd, "viv"); // See compilation instructions if this is not working
if (!hwnd)
{
if (lpCmdLine && lpCmdLine[0] != '\0')
LoadImageFile(hwnd, lpCmdLine);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}