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#include <stdio.h>
2
+ #include <stdlib.h>
3
+ #include <stdbool.h>
4
+
5
+ // GLEW must be included before GLFW
6
+ #define GLEW_STATIC
7
+ #include <GL/glew.h>
8
+ #include <GLFW/glfw3.h>
9
+
10
+ // STB Image for loading images
11
+ #define STB_IMAGE_IMPLEMENTATION
12
+ #include "stb_image.h"
13
+
14
+ // Shader utilities
15
+ void check_shader_error(GLuint shader, const char* type);
16
+ GLuint load_shader(const char* vertex_path, const char* fragment_path);
17
+ GLuint load_texture(const char* path, int* width, int* height);
18
+
19
+ // Window and input handling
20
+ void framebuffer_size_callback(GLFWwindow* window, int width, int height);
21
+ void process_input(GLFWwindow* window);
22
+
23
+ // Settings
24
+ int window_width = 512;
25
+ int window_height = 512;
26
+
27
+ // PixelOE parameters (adjustable in real-time)
28
+ int pixel_size = 6;
29
+ int thickness = 3;
30
+ float avg_scale = 10.0f;
31
+ float dist_scale = 3.0f;
32
+ bool show_original = false;
33
+
34
+ int main(int argc, char** argv) {
35
+ // Check command line arguments
36
+ printf("=== start checking command arguments === \n");
37
+ if (argc < 3) {
38
+ printf("Usage: %s <image_path>\n", argv[0]);
39
+ return 1;
40
+ }
41
+
42
+ printf("=== checking command arguments success === \n");
43
+
44
+ const char* image_path = argv[1];
45
+ bool show_FPS = argv[2];
46
+
47
+ // region Windows Management
48
+ // ********** windows management **********
49
+ // not important thing. just for setting up the window
50
+
51
+ // Initialize GLFW
52
+ if (!glfwInit()) {
53
+ fprintf(stderr, "Failed to initialize GLFW\n");
54
+ return -1;
55
+ }
56
+
57
+ // Configure GLFW
58
+ glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
59
+ glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
60
+ glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
61
+
62
+ // Create window
63
+ GLFWwindow* window = glfwCreateWindow(window_width, window_height, "PixelOE Demo", NULL, NULL);
64
+ if (window == NULL) {
65
+ fprintf(stderr, "Failed to create GLFW window\n");
66
+ glfwTerminate();
67
+ return -1;
68
+ }
69
+ glfwMakeContextCurrent(window);
70
+ glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
71
+
72
+ // Initialize GLEW
73
+ glewExperimental = GL_TRUE;
74
+ if (glewInit() != GLEW_OK) {
75
+ fprintf(stderr, "Failed to initialize GLEW\n");
76
+ glfwTerminate();
77
+ return -1;
78
+ }
79
+
80
+ // Load image texture
81
+ int image_width, image_height;
82
+ GLuint texture = load_texture(image_path, &image_width, &image_height);
83
+ if (texture == 0) {
84
+ fprintf(stderr, "Failed to load image: %s\n", image_path);
85
+ glfwTerminate();
86
+ return -1;
87
+ }
88
+
89
+ // Set window size based on image dimensions (with reasonable limits)
90
+ window_width = image_width < 1200 ? image_width : 1200;
91
+ window_height = image_height < 900 ? image_height : 900;
92
+ glfwSetWindowSize(window, window_width, window_height);
93
+
94
+ // endregion
95
+ // ********** windows management **********
96
+
97
+ // Load shaders
98
+ printf("=== start loading shaders === \n");
99
+
100
+ GLuint shader_program = load_shader("shader.vert", "shader.frag");
101
+ if (shader_program == 0) {
102
+ glfwTerminate();
103
+ return -1;
104
+ }
105
+
106
+ printf("=== loading shaders success === \n");
107
+
108
+ // Set up quad vertices for rendering
109
+ // vert list.it will be used in `shader.vert`
110
+ // send 4 verts to GPU
111
+ // then just show the texture
112
+ float vertices[] = {
113
+ // positions // texture coords
114
+ -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, // top left
115
+ -1.0f, -1.0f, 0.0f, 0.0f, 1.0f, // bottom left
116
+ 1.0f, -1.0f, 0.0f, 1.0f, 1.0f, // bottom right
117
+ 1.0f, 1.0f, 0.0f, 1.0f, 0.0f // top right
118
+ };
119
+
120
+ // use verts to draw 2 triangles
121
+ // fill all quad
122
+ unsigned int indices[] = {
123
+ 0, 1, 2, // first triangle
124
+ 0, 2, 3 // second triangle
125
+ };
126
+
127
+ // Create buffers
128
+ GLuint VAO, VBO, EBO;
129
+ glGenVertexArrays(1, &VAO);
130
+ glGenBuffers(1, &VBO);
131
+ glGenBuffers(1, &EBO);
132
+
133
+ glBindVertexArray(VAO);
134
+
135
+ glBindBuffer(GL_ARRAY_BUFFER, VBO);
136
+ glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
137
+
138
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
139
+ glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
140
+
141
+ // Position attribute
142
+ glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
143
+ glEnableVertexAttribArray(0);
144
+
145
+ // Texture coordinate attribute
146
+ glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
147
+ glEnableVertexAttribArray(1);
148
+
149
+ // Use shader program
150
+ glUseProgram(shader_program);
151
+
152
+ // Set texture sampler
153
+ glUniform1i(glGetUniformLocation(shader_program, "inputTexture"), 0);
154
+
155
+ // Print controls
156
+ printf("\nControls:\n");
157
+ printf(" Arrow Up/Down: Adjust pixel size (%d)\n", pixel_size);
158
+ printf(" Arrow Left/Right: Adjust thickness (%d)\n", thickness);
159
+ printf(" A/Z: Adjust avgScale (%.1f)\n", avg_scale);
160
+ printf(" S/X: Adjust distScale (%.1f)\n", dist_scale);
161
+ printf(" O: Toggle original/processed view\n");
162
+ printf(" ESC: Quit\n\n");
163
+
164
+ // Render loop
165
+
166
+ double lastTime = glfwGetTime();
167
+ int frameCount = 0;
168
+
169
+ while (!glfwWindowShouldClose(window)) {
170
+
171
+ if (show_FPS == 1)
172
+ {
173
+ // calculate FPS
174
+ double currentTime = glfwGetTime();
175
+ frameCount++;
176
+
177
+ // refresh FPS every second
178
+ if (currentTime - lastTime >= 1.0) {
179
+ // print FPS
180
+ printf("FPS: %d\n", frameCount);
181
+
182
+ // reset frameCount
183
+ frameCount = 0;
184
+ lastTime = currentTime;
185
+ }
186
+ }
187
+
188
+ // Process input
189
+ process_input(window);
190
+
191
+ // Get current frame buffer size
192
+ glfwGetFramebufferSize(window, &window_width, &window_height);
193
+
194
+ // Clear the screen
195
+ glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
196
+ glClear(GL_COLOR_BUFFER_BIT);
197
+
198
+ // Use shader program
199
+ glUseProgram(shader_program);
200
+
201
+ // Update uniforms
202
+ glUniform1i(glGetUniformLocation(shader_program, "pixelSize"), pixel_size);
203
+ glUniform1i(glGetUniformLocation(shader_program, "thickness"), thickness);
204
+ glUniform1f(glGetUniformLocation(shader_program, "avgScale"), avg_scale);
205
+ glUniform1f(glGetUniformLocation(shader_program, "distScale"), dist_scale);
206
+ glUniform2f(glGetUniformLocation(shader_program, "resolution"), (float)window_width, (float)window_height);
207
+ glUniform1i(glGetUniformLocation(shader_program, "showOriginal"), show_original);
208
+
209
+ // Bind texture
210
+ glActiveTexture(GL_TEXTURE0);
211
+ glBindTexture(GL_TEXTURE_2D, texture);
212
+
213
+ // Draw quad
214
+ glBindVertexArray(VAO);
215
+ glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
216
+
217
+ // Swap buffers and poll events
218
+ glfwSwapBuffers(window);
219
+ glfwPollEvents();
220
+ }
221
+
222
+ // Clean up
223
+ glDeleteVertexArrays(1, &VAO);
224
+ glDeleteBuffers(1, &VBO);
225
+ glDeleteBuffers(1, &EBO);
226
+ glDeleteProgram(shader_program);
227
+ glDeleteTextures(1, &texture);
228
+
229
+ glfwTerminate();
230
+ return 0;
231
+ }
232
+
233
+ void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
234
+ (void)window; // Avoid unused parameter warning
235
+ glViewport(0, 0, width, height);
236
+ }
237
+
238
+ // control functions
239
+ void process_input(GLFWwindow* window) {
240
+ static bool keys_pressed[512] = {false};
241
+
242
+ // Handle key presses with cooldown
243
+ if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
244
+ glfwSetWindowShouldClose(window, true);
245
+
246
+ // Adjust pixel size
247
+ if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS && !keys_pressed[GLFW_KEY_UP]) {
248
+ pixel_size = (pixel_size < 32) ? pixel_size + 1 : pixel_size;
249
+ printf("Pixel size: %d\n", pixel_size);
250
+ keys_pressed[GLFW_KEY_UP] = true;
251
+ }
252
+ if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_RELEASE)
253
+ keys_pressed[GLFW_KEY_UP] = false;
254
+
255
+ if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS && !keys_pressed[GLFW_KEY_DOWN]) {
256
+ pixel_size = (pixel_size > 1) ? pixel_size - 1 : pixel_size;
257
+ printf("Pixel size: %d\n", pixel_size);
258
+ keys_pressed[GLFW_KEY_DOWN] = true;
259
+ }
260
+ if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_RELEASE)
261
+ keys_pressed[GLFW_KEY_DOWN] = false;
262
+
263
+ // Adjust thickness
264
+ if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS && !keys_pressed[GLFW_KEY_RIGHT]) {
265
+ thickness = (thickness < 6) ? thickness + 1 : thickness;
266
+ printf("Thickness: %d\n", thickness);
267
+ keys_pressed[GLFW_KEY_RIGHT] = true;
268
+ }
269
+ if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_RELEASE)
270
+ keys_pressed[GLFW_KEY_RIGHT] = false;
271
+
272
+ if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS && !keys_pressed[GLFW_KEY_LEFT]) {
273
+ thickness = (thickness > 0) ? thickness - 1 : thickness;
274
+ printf("Thickness: %d\n", thickness);
275
+ keys_pressed[GLFW_KEY_LEFT] = true;
276
+ }
277
+ if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_RELEASE)
278
+ keys_pressed[GLFW_KEY_LEFT] = false;
279
+
280
+ // Adjust avgScale
281
+ if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS && !keys_pressed[GLFW_KEY_A]) {
282
+ avg_scale += 0.5f;
283
+ printf("Avg scale: %.1f\n", avg_scale);
284
+ keys_pressed[GLFW_KEY_A] = true;
285
+ }
286
+ if (glfwGetKey(window, GLFW_KEY_A) == GLFW_RELEASE)
287
+ keys_pressed[GLFW_KEY_A] = false;
288
+
289
+ if (glfwGetKey(window, GLFW_KEY_Z) == GLFW_PRESS && !keys_pressed[GLFW_KEY_Z]) {
290
+ avg_scale -= 0.5f;
291
+ printf("Avg scale: %.1f\n", avg_scale);
292
+ keys_pressed[GLFW_KEY_Z] = true;
293
+ }
294
+ if (glfwGetKey(window, GLFW_KEY_Z) == GLFW_RELEASE)
295
+ keys_pressed[GLFW_KEY_Z] = false;
296
+
297
+ // Adjust distScale
298
+ if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS && !keys_pressed[GLFW_KEY_S]) {
299
+ dist_scale += 0.5f;
300
+ printf("Dist scale: %.1f\n", dist_scale);
301
+ keys_pressed[GLFW_KEY_S] = true;
302
+ }
303
+ if (glfwGetKey(window, GLFW_KEY_S) == GLFW_RELEASE)
304
+ keys_pressed[GLFW_KEY_S] = false;
305
+
306
+ if (glfwGetKey(window, GLFW_KEY_X) == GLFW_PRESS && !keys_pressed[GLFW_KEY_X]) {
307
+ dist_scale -= 0.5f;
308
+ printf("Dist scale: %.1f\n", dist_scale);
309
+ keys_pressed[GLFW_KEY_X] = true;
310
+ }
311
+ if (glfwGetKey(window, GLFW_KEY_X) == GLFW_RELEASE)
312
+ keys_pressed[GLFW_KEY_X] = false;
313
+
314
+ // Toggle original/processed view
315
+ if (glfwGetKey(window, GLFW_KEY_O) == GLFW_PRESS && !keys_pressed[GLFW_KEY_O]) {
316
+ show_original = !show_original;
317
+ printf("Showing %s\n", show_original ? "original" : "pixelized");
318
+ keys_pressed[GLFW_KEY_O] = true;
319
+ }
320
+ if (glfwGetKey(window, GLFW_KEY_O) == GLFW_RELEASE)
321
+ keys_pressed[GLFW_KEY_O] = false;
322
+ }
323
+
324
+ GLuint load_shader(const char* vertex_path, const char* fragment_path) {
325
+ // Read vertex shader from file
326
+ FILE* vertex_file = fopen(vertex_path, "r");
327
+ if (!vertex_file) {
328
+ fprintf(stderr, "Failed to open vertex shader file: %s\n", vertex_path);
329
+ return 0;
330
+ }
331
+
332
+ fseek(vertex_file, 0, SEEK_END);
333
+ long vertex_size = ftell(vertex_file);
334
+ fseek(vertex_file, 0, SEEK_SET);
335
+
336
+ char* vertex_source = (char*)malloc(vertex_size + 1);
337
+ fread(vertex_source, 1, vertex_size, vertex_file);
338
+ vertex_source[vertex_size] = '\0';
339
+ fclose(vertex_file);
340
+
341
+ // Read fragment shader from file
342
+ FILE* fragment_file = fopen(fragment_path, "r");
343
+ if (!fragment_file) {
344
+ fprintf(stderr, "Failed to open fragment shader file: %s\n", fragment_path);
345
+ free(vertex_source);
346
+ return 0;
347
+ }
348
+
349
+ fseek(fragment_file, 0, SEEK_END);
350
+ long fragment_size = ftell(fragment_file);
351
+ fseek(fragment_file, 0, SEEK_SET);
352
+
353
+ char* fragment_source = (char*)malloc(fragment_size + 1);
354
+ fread(fragment_source, 1, fragment_size, fragment_file);
355
+ fragment_source[fragment_size] = '\0';
356
+ fclose(fragment_file);
357
+
358
+ // Create and compile vertex shader
359
+ GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER);
360
+ glShaderSource(vertex_shader, 1, (const char**)&vertex_source, NULL);
361
+ glCompileShader(vertex_shader);
362
+ check_shader_error(vertex_shader, "VERTEX");
363
+
364
+ // Create and compile fragment shader
365
+ GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
366
+ glShaderSource(fragment_shader, 1, (const char**)&fragment_source, NULL);
367
+ glCompileShader(fragment_shader);
368
+ check_shader_error(fragment_shader, "FRAGMENT");
369
+
370
+ // Create and link shader program
371
+ GLuint program = glCreateProgram();
372
+ glAttachShader(program, vertex_shader);
373
+ glAttachShader(program, fragment_shader);
374
+ glLinkProgram(program);
375
+
376
+ // Check for linking errors
377
+ GLint success;
378
+ glGetProgramiv(program, GL_LINK_STATUS, &success);
379
+ if (!success) {
380
+ char info_log[512];
381
+ glGetProgramInfoLog(program, 512, NULL, info_log);
382
+ fprintf(stderr, "Shader program linking failed: %s\n", info_log);
383
+ return 0;
384
+ }
385
+
386
+ // Clean up
387
+ glDeleteShader(vertex_shader);
388
+ glDeleteShader(fragment_shader);
389
+ free(vertex_source);
390
+ free(fragment_source);
391
+
392
+ return program;
393
+ }
394
+
395
+ void check_shader_error(GLuint shader, const char* type) {
396
+ GLint success;
397
+ glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
398
+ if (!success) {
399
+ char info_log[512];
400
+ glGetShaderInfoLog(shader, 512, NULL, info_log);
401
+ fprintf(stderr, "%s shader compilation failed: %s\n", type, info_log);
402
+ }
403
+ }
404
+
405
+ GLuint load_texture(const char* path, int* width, int* height) {
406
+ // Generate texture
407
+ GLuint texture_id;
408
+ glGenTextures(1, &texture_id);
409
+
410
+ // Load image
411
+ int channels;
412
+ unsigned char* data = stbi_load(path, width, height, &channels, 0);
413
+ if (!data) {
414
+ fprintf(stderr, "Failed to load image: %s\n", path);
415
+ return 0;
416
+ }
417
+
418
+ // Determine format based on number of channels
419
+ GLenum format;
420
+ if (channels == 1)
421
+ format = GL_RED;
422
+ else if (channels == 3)
423
+ format = GL_RGB;
424
+ else if (channels == 4)
425
+ format = GL_RGBA;
426
+ else {
427
+ fprintf(stderr, "Unsupported image format (channels: %d)\n", channels);
428
+ stbi_image_free(data);
429
+ return 0;
430
+ }
431
+
432
+ // Bind and configure texture
433
+ glBindTexture(GL_TEXTURE_2D, texture_id);
434
+ glTexImage2D(GL_TEXTURE_2D, 0, format, *width, *height, 0, format, GL_UNSIGNED_BYTE, data);
435
+ glGenerateMipmap(GL_TEXTURE_2D);
436
+
437
+ // Set texture parameters
438
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
439
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
440
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
441
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
442
+
443
+ // Free image data
444
+ stbi_image_free(data);
445
+
446
+ return texture_id;
447
+ }