-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainFrame.cpp
More file actions
443 lines (361 loc) · 15.1 KB
/
Copy pathMainFrame.cpp
File metadata and controls
443 lines (361 loc) · 15.1 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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#include "MainFrame.h"
#include "Codec.h"
#include "ResponsiveImageFrame.h"
#include <wx/wx.h>
#include <wx/filename.h>
#include <wx/aboutdlg.h>
#include <wx/textfile.h>
#include <wx/stdpaths.h>
//#include <wx/imaggif.h>
#ifdef __WXGTK__
#include "genzo.xpm"
#endif
#ifndef APP_VERSION
#define APP_VERSION "Unknown"
#endif
MainFrame::MainFrame(const wxString& title): wxFrame(nullptr, wxID_ANY, title) {
wxInitAllImageHandlers();
CreateMenuBar();
CreateControls();
SetupSizers();
CreateStatusBar();
wxLogStatus(strWelcome);
}
void MainFrame::CreateControls() {
panel = new wxPanel(this);
#if defined(__WXMSW__)
SetIcon(wxICON(IDI_ICON1));
#elif defined(__WXGTK__)
SetIcon(wxIcon(genzo_xpm));
#elif defined(__WXMAC__)
wxIcon macIcon;
if (macIcon.LoadFile(wxStandardPaths::Get().GetResourcesDir() + "genzo.icns", wxBITMAP_TYPE_ICON)) {
SetIcon(macIcon);
}
#endif
wxFont headlineFont(wxFontInfo(wxSize(0, 36)).Bold()), subheadlineFont(wxFontInfo(wxSize(0, 13)));;
headerText = new wxStaticText(panel, wxID_ANY, "Genzo Image Converter", wxPoint(0, 22), wxSize(500, -1), wxALIGN_CENTRE_HORIZONTAL);
headerText->SetFont(headlineFont);
subheaderText = new wxStaticText(panel, wxID_ANY, "Created by Dylan Aviles", wxPoint(0, 22), wxSize(400, -1), wxALIGN_CENTRE_HORIZONTAL);
subheaderText->SetFont(subheadlineFont);
textCtrlFileInputPath = new wxTextCtrl(panel, wxID_ANY, wxEmptyString,
wxDefaultPosition, wxDefaultSize,
wxTE_PROCESS_ENTER);
textCtrlFileInputPath->SetHint("Enter path to file here");
textCtrlFileInputPath->Bind(wxEVT_TEXT, &MainFrame::OnTextChange, this);
textCtrlFileInputPath->Bind(wxEVT_TEXT_ENTER, &MainFrame::OnPathEnter, this);
btnBrowse = new wxButton(panel, wxID_ANY, "Browse...");
btnBrowse->Bind(wxEVT_BUTTON, &MainFrame::OnButtonBrowseClick, this);
btnBrowse->SetFocus();
btnPreview = new wxButton(panel, wxID_ANY, "Preview Image");
btnPreview->Bind(wxEVT_BUTTON, &MainFrame::OnButtonPreviewClick, this);
btnConvert = new wxButton(panel, wxID_ANY, "Convert Image");
btnConvert->Bind(wxEVT_BUTTON, &MainFrame::OnButtonConvertClick, this);
btnConvert->Disable();
}
void MainFrame::SetupSizers() {
wxBoxSizer* mainSizer = new wxBoxSizer(wxVERTICAL);
mainSizer->Add(headerText, wxSizerFlags().CenterHorizontal());
mainSizer->AddSpacer(4);
mainSizer->Add(subheaderText, wxSizerFlags().CenterHorizontal());
mainSizer->AddSpacer(25);
wxBoxSizer* inputSizer = new wxBoxSizer(wxHORIZONTAL);
inputSizer->AddSpacer(5);
inputSizer->Add(textCtrlFileInputPath, wxSizerFlags().Proportion(1));
inputSizer->AddSpacer(17);
wxBoxSizer* buttonSizer = new wxBoxSizer(wxVERTICAL);
buttonSizer->Add(btnBrowse, wxSizerFlags().CenterHorizontal());
buttonSizer->AddSpacer(7);
buttonSizer->Add(btnPreview);
inputSizer->Add(buttonSizer);
mainSizer->Add(inputSizer, wxSizerFlags().Expand());
mainSizer->AddStretchSpacer(1);
mainSizer->AddSpacer(40);
mainSizer->Add(btnConvert, wxSizerFlags().Right());
mainSizer->AddSpacer(20);
wxGridSizer* outerSizer = new wxGridSizer(1);
outerSizer->Add(mainSizer, wxSizerFlags().Expand().Border(wxALL, 25).Expand());
panel->SetSizer(outerSizer);
outerSizer->SetSizeHints(this);
}
void MainFrame::CreateMenuBar() {
wxMenu* menuFile = new wxMenu;
wxMenu* menuHelp = new wxMenu;
menuFile->Append(wxID_OPEN, "&Open...\tCtrl-O", "Open an image file");
menuFile->Append(wxID_SAVEAS, "&Save/Convert...\tCtrl-S", "Convert and save the image");
menuFile->AppendSeparator();
menuFile->Append(wxID_EXIT, "E&xit\tAlt-X", "Quit the application");
wxMenuBar* menuBar = new wxMenuBar;
menuBar->Append(menuFile, "&File");
menuHelp->Append(wxID_ABOUT, "&About...\tF1", "Show information about this application");
menuBar->Append(menuHelp, "&Help");
SetMenuBar(menuBar);
Bind(wxEVT_MENU, &MainFrame::OnButtonBrowseClick, this, wxID_OPEN);
Bind(wxEVT_MENU, &MainFrame::OnButtonConvertClick, this, wxID_SAVEAS);
Bind(wxEVT_MENU, &MainFrame::OnExit, this, wxID_EXIT);
Bind(wxEVT_MENU, &MainFrame::OnAbout, this, wxID_ABOUT);
}
void MainFrame::OnExit(wxCommandEvent& event) {
Close(true);
}
void MainFrame::LoadImage(wxString filePath) {
if (!wxFileExists(filePath)) {
wxMessageBox("Cannot open file", "Error", wxOK | wxICON_ERROR);
return;
}
wxFileName fileName(filePath);
fileName.Normalize(wxPATH_NORM_ALL | wxPATH_NORM_ENV_VARS);
// TODO: Rework this if statement so I don't have 2 almost identical else statements!
// TODO: Rework the check for heif files to rely less on file extensions and more on file format! Other image types load w/o an extension!
if (fileName.GetExt() == "heic" || fileName.GetExt() == "heif" || fileName.GetExt() == "avif") {
img = Codec::LoadHEIFImage(filePath);
if (img.IsOk()) {
btnConvert->Enable();
}
else {
img.Destroy();
wxMessageBox("Failed to load image using libheif!", "Error", wxOK | wxICON_ERROR);
btnConvert->Disable();
return;
}
}
else if (fileName.GetExt() == "icns") {
img = Codec::LoadICNS(filePath);
if (img.IsOk()) {
btnConvert->Enable();
}
else {
img.Destroy();
btnConvert->Disable();
return;
}
}
else if (fileName.GetExt() == "jp2" || fileName.GetExt() == "jpf" || fileName.GetExt() == "jpx" || fileName.GetExt() == "j2c" || fileName.GetExt() == "j2k") {
img = Codec::LoadJP2(filePath);
if (img.IsOk()) {
btnConvert->Enable();
}
else {
img.Destroy();
btnConvert->Disable();
return;
}
}
else if (img.LoadFile(filePath, wxBITMAP_TYPE_ANY)) {
btnConvert->Enable();
}
else {
img.Destroy();
wxMessageBox("Failed to load image!", "Error", wxOK | wxICON_ERROR);
btnConvert->Disable();
return;
}
wxLogStatus(wxString::Format("Selected file: %s", filePath), _("Info"));
wxString size = wxString::Format("%d x %d", img.GetWidth(), img.GetHeight());
wxMessageBox(wxString::Format("Selected file: %s\nImage Size in Pixels: %s", filePath, size), _("Info"));
// TODO: Use this to show an image preview in the main window eventually
ShowStandaloneImage(img);
}
void MainFrame::OnButtonBrowseClick(wxCommandEvent& event) {
wxLogStatus("Select a file");
wxFileDialog openFileDialog(this, _("Open Image file"), "", "",
allSupportedFormats, wxFD_OPEN | wxFD_FILE_MUST_EXIST);
if (openFileDialog.ShowModal() == wxID_CANCEL) { wxLogStatus("Operation Cancelled"); return; }
wxString filePath = openFileDialog.GetPath();
wxLogStatus("Loading image, please wait...");
LoadImage(filePath);
textCtrlFileInputPath->ChangeValue(filePath);
}
void MainFrame::OnPathEnter(wxCommandEvent& event) {
LoadImage(textCtrlFileInputPath->GetValue());
}
void MainFrame::OnButtonConvertClick(wxCommandEvent& event) {
wxLogStatus("File Conversion Method Called!"); //TODO: Figure out what is needed to convert images to .dib bitmaps
wxString filePath = "";
wxFileDialog saveFileDialog(this, _("Save Image file"), "", "",
supportedFormats, wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
if (saveFileDialog.ShowModal() == wxID_OK) {
filePath = saveFileDialog.GetPath();
wxFileName fileName(filePath);
fileName.Normalize(wxPATH_NORM_ALL | wxPATH_NORM_ENV_VARS);
wxLogStatus(wxString::Format("Selected file: %s", filePath), _("Info"));
// TODO: considering adding a progress bar, maybe separating the conversion process into another thread to so the UI doesn't freeze during conversion?
if (img.IsOk()) {
wxLogStatus("Converting image, please wait...");
bool successful = false;
if (fileName.GetExt() == "heic" || fileName.GetExt() == "heif" || fileName.GetExt() == "avif") {
successful = Codec::SaveHEIFImage(img, filePath);
}
else if (fileName.GetExt() == "icns") {
successful = Codec::SaveICNS(img, filePath);
}
else if (img.SaveFile(filePath)) {
//wxLogMessage("Image successfully saved to %s", filePath);
successful = true;
}
else {
wxLogError("Failed to save image to '%s'. Check format support.", filePath);
}
if (successful) {
wxLogMessage("Image successfully saved to %s", filePath);
wxLogStatus("Image converted successfully! Saved to %s", filePath);
}
}
else {
wxLogError("Attempted to save an invalid image.");
}
// TODO: Figure out how to append file extension on Linux, does not happen by default for some reason
// TODO: Figure out how to preserve metadata during conversion
//wxMessageBox(wxString::Format("File converted and saved as: %s", filePath), _("Info"));
}
else {
wxLogStatus("Conversion Cancelled");
wxMessageBox("File conversion cancelled!", _("Info"));
}
}
void MainFrame::OnButtonPreviewClick(wxCommandEvent& event) {
wxString path = textCtrlFileInputPath->GetValue();
if (!img.IsOk() && path != "") {
LoadImage(path);
}
ShowStandaloneImage(img);
}
void MainFrame::OnTextChange(wxCommandEvent& event) {
if (!event.GetString().IsEmpty()) {
//TODO: Tweak this, file isn't necessarily loaded until it is!
//wxString msg = wxString::Format("Selected file: %s", event.GetString());
//wxLogStatus(msg, _("Info"));
btnConvert->Enable();
}
else {
wxLogStatus(strWelcome);
btnConvert->Disable();
}
}
void MainFrame::OnButtonAboutClick(wxCommandEvent& event) {
wxLogStatus("Copyright Info Might Go Here");
wxFrame* frame = new wxFrame(nullptr, wxID_ANY, "Image Viewer",
wxDefaultPosition, wxDefaultSize,
wxDEFAULT_FRAME_STYLE);
}
void MainFrame::OnAbout(wxCommandEvent& event) {
wxDialog aboutDialog(this, wxID_ANY, "About Genzo Image Converter",
wxDefaultPosition, wxSize(500, 450));
wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
wxFont headlineFont(wxFontInfo(wxSize(0, 18)).Bold());
wxStaticText* titleText = new wxStaticText(&aboutDialog, wxID_ANY,
"Genzo Image Converter",
wxDefaultPosition, wxDefaultSize,
wxALIGN_CENTRE_HORIZONTAL);
titleText->SetFont(headlineFont);
wxStaticText* subText = new wxStaticText(&aboutDialog, wxID_ANY,
"Version " APP_VERSION "\nCreated by Dylan Aviles",
wxDefaultPosition, wxDefaultSize,
wxALIGN_CENTRE_HORIZONTAL);
wxArrayString libraryOptions;
libraryOptions.Add("Genzo");
libraryOptions.Add("wxWidgets");
libraryOptions.Add("libheif");
libraryOptions.Add("aom");
libraryOptions.Add("dav1d");
libraryOptions.Add("kvazaar");
libraryOptions.Add("libjpeg-turbo");
libraryOptions.Add("libwebp");
libraryOptions.Add("openjpeg");
wxChoice* libChoice = new wxChoice(&aboutDialog, wxID_ANY, wxDefaultPosition, wxDefaultSize, libraryOptions);
libChoice->SetSelection(0);
wxTextCtrl* licenseTextBox = new wxTextCtrl(&aboutDialog, wxID_ANY, wxEmptyString,
wxDefaultPosition, wxSize(-1, 200),
wxTE_MULTILINE | wxTE_READONLY);
auto updateLicenseText = [licenseTextBox, this](int selection) {
wxString text, lib;
switch (selection) {
case 0:
text = "Genzo Image Converter is distributed under the Clear BSD License.\n\n"
"Copyright(c) 2026 Dylan Aviles\n"
"All rights reserved.\n\n" + clear_bsd;
break;
case 1:
text = wxWidgets;
break;
case 2:
lib = "libheif";
break;
case 3:
lib = "aom";
break;
case 4:
lib = "dav1d";
break;
case 5:
lib = "kvazaar";
break;
case 6:
lib = "libjpeg-turbo";
break;
case 7:
lib = "libwebp";
break;
case 8:
lib = "openjpeg";
break;
default:
break;
}
if (selection != 0 && selection != 1) {
text = LoadTextFromFile(wxFileName::GetPathSeparator() + wxString("third-party-licenses") + wxFileName::GetPathSeparator() + lib + wxString(".txt"));
}
if (text.IsEmpty()) {
text = "License information not available for the selected library. Please ensure license files are present in the \"third-party-licenses\" folder and try again.";
}
licenseTextBox->SetValue(text);
};
updateLicenseText(0);
libChoice->Bind(wxEVT_CHOICE, [updateLicenseText](wxCommandEvent& e) {
updateLicenseText(e.GetSelection());
});
wxButton* okButton = new wxButton(&aboutDialog, wxID_OK, "OK");
sizer->AddSpacer(15);
sizer->Add(titleText, wxSizerFlags().CenterHorizontal());
sizer->Add(subText, wxSizerFlags().CenterHorizontal().Border(wxTOP | wxBOTTOM, 10));
sizer->Add(new wxStaticText(&aboutDialog, wxID_ANY, "View License For:"), wxSizerFlags().Border(wxLEFT | wxTOP, 10));
sizer->Add(libChoice, wxSizerFlags().Expand().Border(wxLEFT | wxRIGHT | wxBOTTOM, 10));
sizer->Add(licenseTextBox, wxSizerFlags().Proportion(1).Expand().Border(wxLEFT | wxRIGHT | wxBOTTOM, 10));
sizer->Add(okButton, wxSizerFlags().CenterHorizontal().Border(wxBOTTOM, 15));
aboutDialog.SetSizer(sizer);
aboutDialog.Layout();
aboutDialog.CenterOnScreen();
aboutDialog.ShowModal();
}
wxString MainFrame::LoadTextFromFile(const wxString& filePath) {
wxString fileContents = "";
wxTextFile file;
if (file.Open(wxFileName(wxStandardPaths::Get().GetExecutablePath()).GetPath() + filePath)) {
for (size_t i = 0; i < file.GetLineCount(); ++i) {
fileContents << file.GetLine(i) << "\n";
}
file.Close();
}
else {
wxLogError("Could not open file: %s", wxFileName(wxStandardPaths::Get().GetExecutablePath()).GetPath() + filePath);
}
return fileContents;
}
// For testing purposes to ensure the images are loading properly, will integrate this with the UI in the future
/*
void MainFrame::ShowImagePopup(const wxImage& image) {
if (!image.IsOk()) {
wxMessageBox("The provided image is invalid or empty.", "Error", wxICON_ERROR | wxOK);
return;
}
wxFrame* frame = new wxFrame(nullptr, wxID_ANY, "Image Viewer",
wxDefaultPosition, wxDefaultSize,
wxDEFAULT_FRAME_STYLE);
wxBitmap bitmap(image);
wxStaticBitmap* imageCtrl = new wxStaticBitmap(frame, wxID_ANY, bitmap);
wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
sizer->Add(imageCtrl, 1, wxEXPAND | wxALL, 10);
frame->SetSizerAndFit(sizer);
frame->Show(true);
}
*/