back to scratko.xyz
aboutsummaryrefslogtreecommitdiff
path: root/img_handler.cpp
diff options
context:
space:
mode:
authorscratko <m@scratko.xyz>2024-11-18 02:45:54 +0300
committerscratko <m@scratko.xyz>2024-11-18 15:32:49 +0300
commit7fea2267e78de935af6010d5ac7300e51f471601 (patch)
tree42a417b778c6f826b84bcc6515c292bd862dcd99 /img_handler.cpp
parent22d4fdabf17aebebfcb73c7d86b5bbc81b6530f4 (diff)
downloadpicture-puzzle-7fea2267e78de935af6010d5ac7300e51f471601.tar.gz
picture-puzzle-7fea2267e78de935af6010d5ac7300e51f471601.tar.bz2
picture-puzzle-7fea2267e78de935af6010d5ac7300e51f471601.zip
Uploading custom images
Diffstat (limited to 'img_handler.cpp')
-rw-r--r--img_handler.cpp67
1 files changed, 67 insertions, 0 deletions
diff --git a/img_handler.cpp b/img_handler.cpp
new file mode 100644
index 0000000..8285de6
--- /dev/null
+++ b/img_handler.cpp
@@ -0,0 +1,67 @@
+#include "img_handler.hpp"
+#include "puzzle.hpp"
+
+#define STB_IMAGE_IMPLEMENTATION
+#define STB_IMAGE_WRITE_IMPLEMENTATION
+#define STB_IMAGE_RESIZE_IMPLEMENTATION
+#include "lib/stb_image.h"
+#include "lib/stb_image_resize2.h"
+#include "lib/stb_image_write.h"
+
+#include <stdlib.h>
+#include <filesystem>
+#include <stdio.h>
+#include <iostream>
+
+ImageHandler::ImageHandler(const char *p) : path_to_file(p) {
+ img_data = nullptr;
+ resized_data = nullptr;
+#if defined(_WIN32)
+ auto it = path_to_file.find_last_of('\\');
+#else
+ auto it = path_to_file.find_last_of('/');
+#endif
+ std::string file_name = path_to_file.substr(it + 1);
+ std::string fn_withot_ext =
+ file_name.substr(0, file_name.find_first_of('.'));
+#if defined(_WIN32)
+ path_to_save = "resources\\" + fn_withot_ext + "\\";
+#else
+ path_to_save = "resources/" + fn_withot_ext + '/';
+#endif
+ std::filesystem::create_directory(path_to_save);
+}
+
+void ImageHandler::load_img()
+{
+ img_data = stbi_load(path_to_file.c_str(), &in_width, &in_height,
+ &channel, 0);
+}
+
+void ImageHandler::resize_img()
+{
+ resized_data =
+ reinterpret_cast<unsigned char*>(malloc(in_width * in_height *
+ channel));
+ stbir_resize_uint8_linear(img_data, in_width, in_height, 0, resized_data,
+ width, height, 0, (stbir_pixel_layout) channel);
+}
+
+void ImageHandler::save_img()
+{
+ int x, y, i, j, k = 0;
+ for(i = 0; i < puzzles_per_side; ++i)
+ for(j = 0; j < puzzles_per_side; ++j, ++k) {
+ x = i * puzzle_size;
+ y = j * puzzle_size;
+ std::string tmp_name =
+ path_to_save +
+ std::to_string(k / puzzles_per_side) +
+ std::to_string(k % puzzles_per_side) + ".png";
+ stbi_write_png(tmp_name.c_str(), puzzle_size, puzzle_size, channel,
+ resized_data + channel * (x + y * width),
+ width * channel);
+ }
+ stbi_image_free(resized_data);
+ stbi_image_free(img_data);
+}