-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
179 lines (145 loc) · 5.11 KB
/
Copy pathmain.cpp
File metadata and controls
179 lines (145 loc) · 5.11 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
#include <iostream>
#include <cstdlib>
#include <chrono>
#include "imgui/imgui.h"
#include "glad/glad.h"
#include "GLFW/glfw3.h"
#include "imgui/backends/imgui_impl_glfw.h"
#include "imgui/backends/imgui_impl_opengl3.h"
#include "Ditherer.h"
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow* window);
static const int START_WINDOW_WIDTH = 500;
static const int START_WINDOW_HEIGHT = 700;
int main(int argc, char* argv[])
{
// init opengl
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
GLFWwindow* window = glfwCreateWindow(START_WINDOW_WIDTH, START_WINDOW_HEIGHT, "Ditherer", NULL, NULL);
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
// init imgui
ImGui::CreateContext();
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init("#version 330");
// create ditherer
std::vector<std::unique_ptr<Dithering::IDitherer>> ditherers((int)Dithering::Ditherer::COUNT);
ditherers[0].reset(new Dithering::JJNDitherer(true));
std::vector<const char*> ditherers_names((int)Dithering::Ditherer::COUNT);
for (int i = 0; i < (int)Dithering::Ditherer::COUNT; ++i)
{
ditherers_names[i] = ditherers[i]->get_name().c_str();
}
// prepare for loop
auto current_ditherer_id = 0;
auto current_palette_id = 0;
char input_file_path[256];
input_file_path[0] = '\0';
char output_file_path[256];
output_file_path[0] = '\0';
auto current_threads_count = 1;
std::string dither_time = "Dithering took: 0 milliseconds";
std::vector<const char*> palettes_names(ditherers[current_ditherer_id]->get_palettes_count());
{
const auto& current_ditherer_palettes_names = ditherers[current_ditherer_id]->get_palettes_names();
for (int i = 0; i < (int)current_ditherer_palettes_names.size(); ++i)
{
palettes_names[i] = current_ditherer_palettes_names[i].c_str();
}
}
// main loop
while (!glfwWindowShouldClose(window))
{
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
processInput(window);
ImGui::SetNextWindowPos(ImVec2(0.0f, 0.0f), ImGuiCond_Always);
ImGui::SetNextWindowSize(ImVec2((float)START_WINDOW_WIDTH, (float)START_WINDOW_HEIGHT), ImGuiCond_Always);
ImGui::Begin("Ditherer", nullptr, ImGuiWindowFlags_::ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_::ImGuiWindowFlags_NoResize);
ImGui::Text("Choose ditherer");
if (ImGui::ListBox("Ditherer", ¤t_ditherer_id, ditherers_names.data(), (int)ditherers_names.size()))
{
palettes_names.resize(ditherers[current_ditherer_id]->get_palettes_count());
const auto& current_ditherer_palettes_names = ditherers[current_ditherer_id]->get_palettes_names();
for (int i = 0; i < current_ditherer_palettes_names.size(); ++i)
{
palettes_names[i] = current_ditherer_palettes_names[i].c_str();
}
}
ImGui::Text("Choose palette");
ImGui::ListBox("Palette", ¤t_palette_id, palettes_names.data(), (int)palettes_names.size());
ImGui::Text("Choose files");
ImGui::InputText("Input file", input_file_path, 100);
ImGui::InputText("Output file", output_file_path, 100);
ImGui::Text("Threads count");
ImGui::InputInt("Input threads count", ¤t_threads_count, 1, 5);
current_threads_count = std::clamp(current_threads_count, 1, 128);
ImGui::Spacing();
ImGui::Spacing();
if (ImGui::Button("Dither"))
{
try
{
Dithering::Image input_image;
if (!input_image.openImage(input_file_path))
{
break;
}
auto rows = 0;
auto columns = 0;
input_image.getSize(rows, columns);
Dithering::Image output_image;
if (!output_image.createImage(output_file_path, rows, columns))
{
break;
}
const auto start_time = std::chrono::high_resolution_clock::now();
if (!ditherers[current_ditherer_id]->process(input_image, output_image, current_palette_id, current_threads_count))
{
break;
}
dither_time = "Dithering took: " +
std::to_string(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - start_time).count()) +
" milliseconds";
output_image.saveImage();
}
catch (...)
{
std::cout << "There was error while dithering" << std::endl;
dither_time = "Dithering took: 0 milliseconds";
}
}
ImGui::Text(dither_time.c_str());
ImGui::End();
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glfwSwapBuffers(window);
glfwPollEvents();
}
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
void processInput(GLFWwindow* window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
{
glfwSetWindowShouldClose(window, 1);
}
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}